diff options
Diffstat (limited to 'src/twm.c')
-rw-r--r-- | src/twm.c | 109 |
1 files changed, 80 insertions, 29 deletions
@@ -62,6 +62,8 @@ in this Software without prior written authorization from The Open Group. #include <stdio.h> #include <signal.h> #include <fcntl.h> +#include <stdarg.h> + #include "twm.h" #include "iconmgr.h" #include "add_window.h" @@ -76,6 +78,7 @@ in this Software without prior written authorization from The Open Group. #include "parse.h" #include "session.h" #include "version.h" + #include <X11/Xproto.h> #include <X11/Xatom.h> #include <X11/SM/SMlib.h> @@ -116,7 +119,7 @@ ScreenInfo **ScreenList; /* structures for each screen */ ScreenInfo *Scr = NULL; /* the cur and prev screens */ int PreviousScreen; /* last screen that we were on */ int FirstScreen; /* TRUE ==> first screen of display */ -static Bool PrintErrorMessages = False; /* controls error messages */ +static int verbose = 1; /* controls error messages */ static int RedirectError; /* TRUE ==> another window manager running */ static int TwmErrorHandler(Display *dpy, XErrorEvent *event); /* for settting RedirectError */ static int CatchRedirectError(Display *dpy, XErrorEvent *event); /* for everything else */ @@ -305,7 +308,7 @@ main(int argc, char *argv[]) case 'v': /* -verbose */ if (!brief_opt(argv[i], "verbose")) usage(); - PrintErrorMessages = True; + verbose++; continue; case 'c': /* -clientId */ if (!brief_opt(argv[i], "clientId")) @@ -324,7 +327,7 @@ main(int argc, char *argv[]) case 'q': /* -quiet */ if (!brief_opt(argv[i], "quiet")) usage(); - PrintErrorMessages = False; + --verbose; continue; } } @@ -379,16 +382,11 @@ main(int argc, char *argv[]) if (!(dpy = XtOpenDisplay(appContext, display_name, "twm", "twm", NULL, 0, &zero, NULL))) { - fprintf(stderr, "%s: unable to open display \"%s\"\n", - ProgramName, XDisplayName(display_name)); - exit(EXIT_FAILURE); + twmError("unable to open display \"%s\"", XDisplayName(display_name)); } if (fcntl(ConnectionNumber(dpy), F_SETFD, 1) == -1) { - fprintf(stderr, - "%s: unable to mark display connection as close-on-exec\n", - ProgramName); - exit(EXIT_FAILURE); + twmError("unable to mark display connection as close-on-exec"); } if (restore_filename) @@ -425,10 +423,7 @@ main(int argc, char *argv[]) /* for simplicity, always allocate NumScreens ScreenInfo struct pointers */ ScreenList = calloc((size_t) NumScreens, sizeof(ScreenInfo *)); if (ScreenList == NULL) { - fprintf(stderr, - "%s: Unable to allocate memory for screen list, exiting.\n", - ProgramName); - exit(EXIT_FAILURE); + twmError("Unable to allocate memory for screen list, exiting"); } numManaged = 0; PreviousScreen = DefaultScreen(dpy); @@ -438,8 +433,7 @@ main(int argc, char *argv[]) /* Ignore print screens to avoid that users accidentally warp on a * print screen (which are not visible on video displays) */ if ((!NoPrintscreens) && IsPrintScreen(XScreenOfDisplay(dpy, scrnum))) { - fprintf(stderr, "%s: skipping print screen %d\n", - ProgramName, scrnum); + twmWarning("skipping print screen %d", scrnum); continue; } #endif /* XPRINT */ @@ -457,12 +451,13 @@ main(int argc, char *argv[]) XSetErrorHandler(TwmErrorHandler); if (RedirectError) { - fprintf(stderr, "%s: another window manager is already running.", - ProgramName); - if (MultiScreen && NumScreens > 0) - fprintf(stderr, " on screen %d?\n", scrnum); - else - fprintf(stderr, "?\n"); + if (MultiScreen && NumScreens > 0) { + twmWarning("another window manager is already running." + " on screen %d?\n", scrnum); + } + else { + twmWarning("another window manager is already running."); + } continue; } @@ -471,9 +466,9 @@ main(int argc, char *argv[]) /* Note: ScreenInfo struct is calloc'ed to initialize to zero. */ Scr = ScreenList[scrnum] = calloc(1, sizeof(ScreenInfo)); if (Scr == NULL) { - fprintf(stderr, - "%s: unable to allocate memory for ScreenInfo structure for screen %d.\n", - ProgramName, scrnum); + twmWarning + ("unable to allocate memory for ScreenInfo structure for screen %d.", + scrnum); continue; } @@ -706,9 +701,10 @@ main(int argc, char *argv[]) } /* for */ if (numManaged == 0) { - if (MultiScreen && NumScreens > 0) - fprintf(stderr, "%s: unable to find any unmanaged %sscreens.\n", - ProgramName, NoPrintscreens ? "" : "video "); + if (MultiScreen && NumScreens > 0) { + twmError("unable to find any unmanaged %sscreens.\n", + NoPrintscreens ? "" : "video "); + } exit(EXIT_FAILURE); } @@ -993,7 +989,7 @@ TwmErrorHandler(Display *dpy2, XErrorEvent *event) LastErrorEvent = *event; ErrorOccurred = True; - if (PrintErrorMessages && /* don't be too obnoxious */ + if ((verbose > 1) && /* don't be too obnoxious */ event->error_code != BadWindow && /* watch for dead puppies */ (event->request_code != X_GetGeometry && /* of all styles */ event->error_code != BadDrawable)) @@ -1009,3 +1005,58 @@ CatchRedirectError(Display *dpy2 _X_UNUSED, XErrorEvent *event) ErrorOccurred = True; return 0; } + +void +twmError(const char *format, ...) +{ + va_list ap; + + va_start(ap, format); + fprintf(stderr, "%s: error: ", ProgramName); + vfprintf(stderr, format, ap); + fputc('\n', stderr); + va_end(ap); + exit(EXIT_FAILURE); +} + +void +twmWarning(const char *format, ...) +{ + if (verbose > 0) { + va_list ap; + + va_start(ap, format); + fprintf(stderr, "%s: warning: ", ProgramName); + vfprintf(stderr, format, ap); + fputc('\n', stderr); + va_end(ap); + } +} + +void +twmVerbose(const char *format, ...) +{ + if (verbose > 1) { + va_list ap; + + va_start(ap, format); + fprintf(stderr, "%s: warning: ", ProgramName); + vfprintf(stderr, format, ap); + fputc('\n', stderr); + va_end(ap); + } +} + +void +twmMessage(const char *format, ...) +{ + va_list ap; + + va_start(ap, format); + printf("%s: ", ProgramName); + vprintf(format, ap); + putc('\n', stdout); + va_end(ap); + + fflush(stdout); +} |