Misc: Code cleanup & debug code changes

- Merge @daBrado's focus event debugging code. Thanks!

- Use macro to reduce code redundancy in various functions.

- Move focus event validation from ev_focus_out() to a separate
  function.

- Add logic in ev_handle() to increase the chance of successful window
  name detection if compton is not reading window names normally (i.e.
  if there's no --shadow-exclude), when DEBUG_EVENTS is on.
This commit is contained in:
Richard Grenville 2012-11-03 05:51:40 +08:00
parent 66d3f30978
commit a0b0ff5d0a
2 changed files with 105 additions and 114 deletions

View File

@ -10,6 +10,12 @@
#include "compton.h" #include "compton.h"
#define MSTR_(s) #s
#define MSTR(s) MSTR_(s)
// Use #s here to prevent macro expansion
#define CASESTRRET(s) case s: return #s
/** /**
* Shared * Shared
*/ */
@ -2651,99 +2657,49 @@ error(Display *dpy, XErrorEvent *ev) {
exit(1); exit(1);
} }
#define CASESTRRET2(s) case s: name = #s; break
o = ev->error_code - xfixes_error; o = ev->error_code - xfixes_error;
switch (o) { switch (o) {
case BadRegion: CASESTRRET2(BadRegion);
name = "BadRegion";
break;
default:
break;
} }
o = ev->error_code - damage_error; o = ev->error_code - damage_error;
switch (o) { switch (o) {
case BadDamage: CASESTRRET2(BadDamage);
name = "BadDamage";
break;
default:
break;
} }
o = ev->error_code - render_error; o = ev->error_code - render_error;
switch (o) { switch (o) {
case BadPictFormat: CASESTRRET2(BadPictFormat);
name = "BadPictFormat"; CASESTRRET2(BadPicture);
break; CASESTRRET2(BadPictOp);
case BadPicture: CASESTRRET2(BadGlyphSet);
name = "BadPicture"; CASESTRRET2(BadGlyph);
break;
case BadPictOp:
name = "BadPictOp";
break;
case BadGlyphSet:
name = "BadGlyphSet";
break;
case BadGlyph:
name = "BadGlyph";
break;
default:
break;
} }
switch (ev->error_code) { switch (ev->error_code) {
case BadAccess: CASESTRRET2(BadAccess);
name = "BadAccess"; CASESTRRET2(BadAlloc);
break; CASESTRRET2(BadAtom);
case BadAlloc: CASESTRRET2(BadColor);
name = "BadAlloc"; CASESTRRET2(BadCursor);
break; CASESTRRET2(BadDrawable);
case BadAtom: CASESTRRET2(BadFont);
name = "BadAtom"; CASESTRRET2(BadGC);
break; CASESTRRET2(BadIDChoice);
case BadColor: CASESTRRET2(BadImplementation);
name = "BadColor"; CASESTRRET2(BadLength);
break; CASESTRRET2(BadMatch);
case BadCursor: CASESTRRET2(BadName);
name = "BadCursor"; CASESTRRET2(BadPixmap);
break; CASESTRRET2(BadRequest);
case BadDrawable: CASESTRRET2(BadValue);
name = "BadDrawable"; CASESTRRET2(BadWindow);
break;
case BadFont:
name = "BadFont";
break;
case BadGC:
name = "BadGC";
break;
case BadIDChoice:
name = "BadIDChoice";
break;
case BadImplementation:
name = "BadImplementation";
break;
case BadLength:
name = "BadLength";
break;
case BadMatch:
name = "BadMatch";
break;
case BadName:
name = "BadName";
break;
case BadPixmap:
name = "BadPixmap";
break;
case BadRequest:
name = "BadRequest";
break;
case BadValue:
name = "BadValue";
break;
case BadWindow:
name = "BadWindow";
break;
} }
#undef CASESTRRET2
print_timestamp(); print_timestamp();
printf("error %d (%s) request %d minor %d serial %lu\n", printf("error %d (%s) request %d minor %d serial %lu\n",
ev->error_code, name, ev->request_code, ev->error_code, name, ev->request_code,
@ -2888,34 +2844,22 @@ ev_serial(XEvent *ev) {
return NextRequest(ev->xany.display); return NextRequest(ev->xany.display);
} }
static char * static const char *
ev_name(XEvent *ev) { ev_name(XEvent *ev) {
static char buf[128]; static char buf[128];
switch (ev->type & 0x7f) { switch (ev->type & 0x7f) {
case FocusIn: CASESTRRET(FocusIn);
return "FocusIn"; CASESTRRET(FocusOut);
case FocusOut: CASESTRRET(CreateNotify);
return "FocusOut"; CASESTRRET(ConfigureNotify);
case CreateNotify: CASESTRRET(DestroyNotify);
return "CreateNotify"; CASESTRRET(MapNotify);
case ConfigureNotify: CASESTRRET(UnmapNotify);
return "ConfigureNotify"; CASESTRRET(ReparentNotify);
case DestroyNotify: CASESTRRET(CirculateNotify);
return "DestroyNotify"; CASESTRRET(Expose);
case MapNotify: CASESTRRET(PropertyNotify);
return "Map"; CASESTRRET(ClientMessage);
case UnmapNotify:
return "Unmap";
case ReparentNotify:
return "Reparent";
case CirculateNotify:
return "Circulate";
case Expose:
return "Expose";
case PropertyNotify:
return "PropertyNotify";
case ClientMessage:
return "ClientMessage";
default: default:
if (ev->type == damage_event + XDamageNotify) { if (ev->type == damage_event + XDamageNotify) {
return "Damage"; return "Damage";
@ -2969,14 +2913,61 @@ ev_window(XEvent *ev) {
return 0; return 0;
} }
} }
static inline const char *
ev_focus_mode_name(XFocusChangeEvent* ev) {
switch (ev->mode) {
CASESTRRET(NotifyNormal);
CASESTRRET(NotifyWhileGrabbed);
CASESTRRET(NotifyGrab);
CASESTRRET(NotifyUngrab);
}
return "Unknown";
}
static inline const char *
ev_focus_detail_name(XFocusChangeEvent* ev) {
switch (ev->detail) {
CASESTRRET(NotifyAncestor);
CASESTRRET(NotifyVirtual);
CASESTRRET(NotifyInferior);
CASESTRRET(NotifyNonlinear);
CASESTRRET(NotifyNonlinearVirtual);
CASESTRRET(NotifyPointer);
CASESTRRET(NotifyPointerRoot);
CASESTRRET(NotifyDetailNone);
}
return "Unknown";
}
static inline void
ev_focus_report(XFocusChangeEvent* ev) {
printf(" { mode: %s, detail: %s }\n", ev_focus_mode_name(ev),
ev_focus_detail_name(ev));
}
#endif #endif
/** /**
* Events * Events
*/ */
inline static bool
ev_focus_accept(XFocusChangeEvent *ev) {
return ev->mode == NotifyGrab
|| (ev->mode == NotifyNormal
&& (ev->detail == NotifyNonlinear
|| ev->detail == NotifyNonlinearVirtual));
}
inline static void inline static void
ev_focus_in(XFocusChangeEvent *ev) { ev_focus_in(XFocusChangeEvent *ev) {
#ifdef DEBUG_EVENTS
ev_focus_report(ev);
#endif
win *w = find_win(dpy, ev->window); win *w = find_win(dpy, ev->window);
// To deal with events sent from windows just destroyed // To deal with events sent from windows just destroyed
@ -2987,14 +2978,12 @@ ev_focus_in(XFocusChangeEvent *ev) {
inline static void inline static void
ev_focus_out(XFocusChangeEvent *ev) { ev_focus_out(XFocusChangeEvent *ev) {
if (ev->mode == NotifyGrab #ifdef DEBUG_EVENTS
|| (ev->mode == NotifyNormal ev_focus_report(ev);
&& (ev->detail == NotifyNonlinear #endif
|| ev->detail == NotifyNonlinearVirtual))) {
; if (!ev_focus_accept(ev))
} else {
return; return;
}
win *w = find_win(dpy, ev->window); win *w = find_win(dpy, ev->window);
@ -3214,7 +3203,9 @@ ev_handle(XEvent *ev) {
if (w && w->name) if (w && w->name)
window_name = w->name; window_name = w->name;
else else if (!(w && w->client_win
&& (to_free = (Bool) wid_get_name(dpy, w->client_win,
&window_name))))
to_free = (Bool) wid_get_name(dpy, wid, &window_name); to_free = (Bool) wid_get_name(dpy, wid, &window_name);
} }
} }

View File

@ -1009,7 +1009,7 @@ win_get_class(Display *dpy, win *w);
static int static int
ev_serial(XEvent *ev); ev_serial(XEvent *ev);
static char * static const char *
ev_name(XEvent *ev); ev_name(XEvent *ev);
static Window static Window