From 7ab11dfa666ee9341ddd0eb82e3e6d0117e42e8f Mon Sep 17 00:00:00 2001 From: Richard Grenville Date: Wed, 12 Sep 2012 09:08:15 +0800 Subject: [PATCH] Debug: Enhanced debugging capability - Change all #if DEBUG_XXX directives to #ifdef, thus making it possible to directly enable debugging options with CFLAGS (-DDEBUG_XXX). - Print timestamp before event debugging messages. --- compton.c | 41 ++++++++++++++++++------------- compton.h | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 91 insertions(+), 23 deletions(-) diff --git a/compton.c b/compton.c index a281b31b3..20fc8f841 100644 --- a/compton.c +++ b/compton.c @@ -10,14 +10,14 @@ #include "compton.h" -#if DEBUG_EVENTS -static int window_get_name(Window w, char **name); -#endif - /** * Shared */ +#ifdef DEBUG_EVENTS +struct timeval time_start = { 0, 0 }; +#endif + win *list; fade *fades; Display *dpy; @@ -955,7 +955,7 @@ paint_all(Display *dpy, XserverRegion region) { region = get_screen_region(dpy); } -#if MONITOR_REPAINT +#ifdef MONITOR_REPAINT root_buffer = root_picture; #else if (!root_buffer) { @@ -973,14 +973,14 @@ paint_all(Display *dpy, XserverRegion region) { XFixesSetPictureClipRegion(dpy, root_picture, 0, 0, region); -#if MONITOR_REPAINT +#ifdef MONITOR_REPAINT XRenderComposite( dpy, PictOpSrc, black_picture, None, root_picture, 0, 0, 0, 0, 0, 0, root_width, root_height); #endif -#if DEBUG_REPAINT +#ifdef DEBUG_REPAINT printf("paint:"); #endif @@ -1017,7 +1017,7 @@ paint_all(Display *dpy, XserverRegion region) { dpy, draw, format, CPSubwindowMode, &pa); } -#if DEBUG_REPAINT +#ifdef DEBUG_REPAINT printf(" 0x%x", w->id); #endif @@ -1077,7 +1077,7 @@ paint_all(Display *dpy, XserverRegion region) { t = w; } -#if DEBUG_REPAINT +#ifdef DEBUG_REPAINT printf("\n"); fflush(stdout); #endif @@ -1212,7 +1212,7 @@ repair_win(Display *dpy, win *w) { w->damaged = 1; } -#if DEBUG_WINTYPE +#ifdef DEBUG_WINTYPE static const char * wintype_name(wintype type) { const char *t; @@ -1357,7 +1357,7 @@ map_win(Display *dpy, Window id, w->a.map_state = IsViewable; w->window_type = determine_wintype(dpy, w->id, w->id); -#if DEBUG_WINTYPE +#ifdef DEBUG_WINTYPE printf("window 0x%x type %s\n", w->id, wintype_name(w->window_type)); #endif @@ -1729,7 +1729,7 @@ restack_win(Display *dpy, win *w, Window new_above) { w->next = *prev; *prev = w; -#if DEBUG_RESTACK +#ifdef DEBUG_RESTACK { const char *desc; char *window_name; @@ -2087,7 +2087,7 @@ expose_root(Display *dpy, Window root, XRectangle *rects, int nrects) { add_damage(dpy, region); } -#if DEBUG_EVENTS || DEBUG_RESTACK +#if defined(DEBUG_EVENTS) || defined(DEBUG_RESTACK) static int window_get_name(Window w, char **name) { Atom prop = XInternAtom(dpy, "_NET_WM_NAME", False); Atom utf8_type = XInternAtom(dpy, "UTF8_STRING", False); @@ -2104,7 +2104,7 @@ static int window_get_name(Window w, char **name) { &leftover, (unsigned char **) &data))) { if (BadWindow == ret) return 0; - set_ignore(dpy, NextRequest(dpy)); + set_ignore(dpy, NextRequest(dpy)); printf("Window %#010lx: _NET_WM_NAME unset, falling back to WM_NAME.\n", w); if (!XFetchName(dpy, w, &data)) return 0; @@ -2113,7 +2113,9 @@ static int window_get_name(Window w, char **name) { *name = (char *) data; return 1; } +#endif +#ifdef DEBUG_EVENTS static int ev_serial(XEvent *ev) { if ((ev->type & 0x7f) != KeymapNotify) { @@ -2232,7 +2234,7 @@ ev_create_notify(XCreateWindowEvent *ev) { inline static void ev_configure_notify(XConfigureEvent *ev) { -#if DEBUG_EVENTS +#ifdef DEBUG_EVENTS printf("{ send_event: %d, above: %#010lx, override_redirect: %d }\n", ev->send_event, ev->above, ev->override_redirect); #endif configure_win(dpy, ev); @@ -2357,7 +2359,7 @@ static void ev_shape_notify(XShapeEvent *ev) { inline static void ev_handle(XEvent *ev) { -#if DEBUG_EVENTS +#ifdef DEBUG_EVENTS Window w; char *window_name; Bool to_free = False; @@ -2367,7 +2369,7 @@ ev_handle(XEvent *ev) { discard_ignore(dpy, ev->xany.serial); } -#if DEBUG_EVENTS +#ifdef DEBUG_EVENTS w = ev_window(ev); window_name = "(Failed to get title)"; if (w) { @@ -2377,6 +2379,7 @@ ev_handle(XEvent *ev) { to_free = (Bool) window_get_name(w, &window_name); } if (ev->type != damage_event + XDamageNotify) { + print_timestamp(); printf("event %10.10s serial %#010x window %#010lx \"%s\"\n", ev_name(ev), ev_serial(ev), w, window_name); } @@ -2616,6 +2619,10 @@ main(int argc, char **argv) { double shadow_green = 0.0; double shadow_blue = 0.0; +#ifdef DEBUG_EVENTS + gettimeofday(&time_start, NULL); +#endif + for (i = 0; i < NUM_WINTYPES; ++i) { win_type_fade[i] = False; win_type_shadow[i] = False; diff --git a/compton.h b/compton.h index 21cccd826..451ea3d53 100644 --- a/compton.h +++ b/compton.h @@ -28,11 +28,19 @@ #endif #define CAN_DO_USABLE 0 -#define DEBUG_REPAINT 0 -#define DEBUG_EVENTS 0 -#define DEBUG_RESTACK 0 -#define DEBUG_WINTYPE 0 -#define MONITOR_REPAINT 0 + +// Debug options, enable them using -D in CFLAGS +// #define DEBUG_REPAINT 1 +// #define DEBUG_EVENTS 1 +// #define DEBUG_RESTACK 1 +// #define DEBUG_WINTYPE 1 +// #define MONITOR_REPAINT 1 + +#ifdef DEBUG_EVENTS +// For printing timestamps +#include +extern struct timeval time_start; +#endif #define OPAQUE 0xffffffff #define REGISTER_PROP "_NET_WM_CM_S" @@ -173,6 +181,55 @@ static inline Bool array_wid_exists(const Window *arr, return False; } +#ifdef DEBUG_EVENTS +/* + * Subtracting two struct timeval values. + * + * Taken from glibc manual. + * + * Subtract the `struct timeval' values X and Y, + * storing the result in RESULT. + * Return 1 if the difference is negative, otherwise 0. */ +int timeval_subtract (result, x, y) + struct timeval *result, *x, *y; +{ + /* Perform the carry for the later subtraction by updating y. */ + if (x->tv_usec < y->tv_usec) { + int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1; + y->tv_usec -= 1000000 * nsec; + y->tv_sec += nsec; + } + if (x->tv_usec - y->tv_usec > 1000000) { + int nsec = (x->tv_usec - y->tv_usec) / 1000000; + y->tv_usec += 1000000 * nsec; + y->tv_sec -= nsec; + } + + /* Compute the time remaining to wait. + tv_usec is certainly positive. */ + result->tv_sec = x->tv_sec - y->tv_sec; + result->tv_usec = x->tv_usec - y->tv_usec; + + /* Return 1 if result is negative. */ + return x->tv_sec < y->tv_sec; +} + +/** + * Print time passed since program starts execution. + * + * Used for debugging. + */ +static inline void print_timestamp(void) { + struct timeval tm, diff; + + if (gettimeofday(&tm, NULL)) + return; + + timeval_subtract(&diff, &tm, &time_start); + printf("[ %5ld.%02ld ] ", diff.tv_sec, diff.tv_usec / 10000); +} +#endif + static int get_time_in_milliseconds(); @@ -338,7 +395,11 @@ error(Display *dpy, XErrorEvent *ev); static void expose_root(Display *dpy, Window root, XRectangle *rects, int nrects); -#if DEBUG_EVENTS +#if defined(DEBUG_EVENTS) || defined(DEBUG_RESTACK) +static int window_get_name(Window w, char **name); +#endif + +#ifdef DEBUG_EVENTS static int ev_serial(XEvent *ev);