Skip to content

Commit

Permalink
Replace EV_HANDLER macro with templates
Browse files Browse the repository at this point in the history
Signed-off-by: Tin Švagelj <tin.svagelj@live.com>
  • Loading branch information
Caellian committed Apr 12, 2024
1 parent 00466c3 commit cfdf5ac
Showing 1 changed file with 95 additions and 65 deletions.
160 changes: 95 additions & 65 deletions src/display-x11.cc
Original file line number Diff line number Diff line change
Expand Up @@ -423,16 +423,30 @@ bool display_output_x11::main_loop_wait(double t) {
return true;
}

#define EV_HANDLER(name) \
bool _conky_ev_handle_##name(conky::display_output_x11 *surface, \
Display *display, XEvent &ev, bool *consumed, \
void **cookie)
#define NOOP_EV_HANDLER(name) \
EV_HANDLER(name) { return false; }
enum x_event_handler {
XINPUT_MOTION,
MOUSE_INPUT,
PROPERTY_NOTIFY,

EXPOSE,
REPARENT,
CONFIGURE,
BORDER_CROSSING,
DAMAGE,
};

template <x_event_handler handler>
bool handle_event(conky::display_output_x11 *surface, Display *display,
XEvent &ev, bool *consumed, void **cookie) {
return false;
}

#ifdef OWN_WINDOW
#ifdef BUILD_XINPUT
EV_HANDLER(xinput_motion) {
template <>
bool handle_event<x_event_handler::XINPUT_MOTION>(
conky::display_output_x11 *surface, Display *display, XEvent &ev,
bool *consumed, void **cookie) {
if (ev.type != GenericEvent || ev.xcookie.extension != window.xi_opcode)
return false;

Expand Down Expand Up @@ -490,7 +504,10 @@ EV_HANDLER(xinput_motion) {
return true;
}
#endif /* BUILD_XINPUT */
EV_HANDLER(mouse_input) {
template <>
bool handle_event<x_event_handler::MOUSE_INPUT>(
conky::display_output_x11 *surface, Display *display, XEvent &ev,
bool *consumed, void **cookie) {
if (ev.type != ButtonPress && ev.type != ButtonRelease &&
ev.type != MotionNotify)
return false;
Expand Down Expand Up @@ -554,52 +571,20 @@ EV_HANDLER(mouse_input) {
return true;
}

EV_HANDLER(reparent) {
template <>
bool handle_event<x_event_handler::REPARENT>(conky::display_output_x11 *surface,
Display *display, XEvent &ev,
bool *consumed, void **cookie) {
if (ev.type != ReparentNotify) return false;

if (own_window.get(*state)) { set_transparent_background(window.window); }
return true;
}
#else /* OWN_WINDOW */
NOOP_EV_HANDLER(reparent)
#endif /* OWN_WINDOW */

EV_HANDLER(property_notify) {
if (ev.type != PropertyNotify) return false;

if (ev.xproperty.state == PropertyNewValue) {
get_x11_desktop_info(ev.xproperty.display, ev.xproperty.atom);
}

#ifdef USE_ARGB
if (have_argb_visual) return true;
#endif

if (ev.xproperty.atom == ATOM(_XROOTPMAP_ID) ||
ev.xproperty.atom == ATOM(_XROOTMAP_ID)) {
if (forced_redraw.get(*state)) {
draw_stuff();
next_update_time = get_time();
need_to_update = 1;
}
}
return true;
}

EV_HANDLER(expose) {
if (ev.type != Expose) return false;

XRectangle r;
r.x = ev.xexpose.x;
r.y = ev.xexpose.y;
r.width = ev.xexpose.width;
r.height = ev.xexpose.height;
XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
XSync(display, False);
return true;
}

EV_HANDLER(configure) {
template <>
bool handle_event<x_event_handler::CONFIGURE>(
conky::display_output_x11 *surface, Display *display, XEvent &ev,
bool *consumed, void **cookie) {
if (ev.type != ConfigureNotify) return false;

if (own_window.get(*state)) {
Expand Down Expand Up @@ -643,8 +628,53 @@ EV_HANDLER(configure) {

return true;
}
#endif /* OWN_WINDOW */

template <>
bool handle_event<x_event_handler::PROPERTY_NOTIFY>(
conky::display_output_x11 *surface, Display *display, XEvent &ev,
bool *consumed, void **cookie) {
if (ev.type != PropertyNotify) return false;

if (ev.xproperty.state == PropertyNewValue) {
get_x11_desktop_info(ev.xproperty.display, ev.xproperty.atom);
}

#ifdef USE_ARGB
if (have_argb_visual) return true;
#endif

if (ev.xproperty.atom == ATOM(_XROOTPMAP_ID) ||
ev.xproperty.atom == ATOM(_XROOTMAP_ID)) {
if (forced_redraw.get(*state)) {
draw_stuff();
next_update_time = get_time();
need_to_update = 1;
}
}
return true;
}

template <>
bool handle_event<x_event_handler::EXPOSE>(conky::display_output_x11 *surface,
Display *display, XEvent &ev,
bool *consumed, void **cookie) {
if (ev.type != Expose) return false;

EV_HANDLER(border_crossing) {
XRectangle r;
r.x = ev.xexpose.x;
r.y = ev.xexpose.y;
r.width = ev.xexpose.width;
r.height = ev.xexpose.height;
XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
XSync(display, False);
return true;
}

template <>
bool handle_event<x_event_handler::BORDER_CROSSING>(
conky::display_output_x11 *surface, Display *display, XEvent &ev,
bool *consumed, void **cookie) {
if (ev.type != EnterNotify && ev.type != LeaveNotify) return false;
if (window.xi_opcode != 0) return true; // handled by mouse_input already

Expand All @@ -665,7 +695,10 @@ EV_HANDLER(border_crossing) {
}

#ifdef BUILD_XDAMAGE
EV_HANDLER(damage) {
template <>
bool handle_event<x_event_handler::DAMAGE>(conky::display_output_x11 *surface,
Display *display, XEvent &ev,
bool *consumed, void **cookie) {
if (ev.type != x11_stuff.event_base + XDamageNotify) return false;

auto *dev = reinterpret_cast<XDamageNotifyEvent *>(&ev);
Expand All @@ -675,33 +708,30 @@ EV_HANDLER(damage) {
x11_stuff.part);
return true;
}
#else
NOOP_EV_HANDLER(damage)
#endif /* BUILD_XDAMAGE */

/// Handles all events conky can receive.
///
/// @return true if event should move input focus to conky
bool process_event(conky::display_output_x11 *surface, Display *display,
XEvent ev, bool *consumed, void **cookie) {
#define HANDLE_EV(handler) \
if (_conky_ev_handle_##handler(surface, display, ev, consumed, cookie)) \
return true
#define HANDLE_EV(event) \
if (handle_event<x_event_handler::event>(surface, display, ev, consumed, \
cookie)) { \
return true; \
}

#ifdef BUILD_XINPUT
// handles enter & leave events better
HANDLE_EV(xinput_motion);
#endif /* BUILD_XINPUT */
HANDLE_EV(mouse_input);
HANDLE_EV(property_notify);
HANDLE_EV(XINPUT_MOTION)
HANDLE_EV(MOUSE_INPUT)
HANDLE_EV(PROPERTY_NOTIFY)

// only accept remaining events if they're sent to Conky.
if (ev.xany.window != window.window) return false;

HANDLE_EV(expose);
HANDLE_EV(reparent);
HANDLE_EV(border_crossing);
HANDLE_EV(damage);
HANDLE_EV(EXPOSE)
HANDLE_EV(REPARENT)
HANDLE_EV(BORDER_CROSSING)
HANDLE_EV(DAMAGE)

// event not handled
return false;
Expand Down

0 comments on commit cfdf5ac

Please sign in to comment.