From 5e10dc08a3c98f5e34c57be2fb67cc1e6157061c Mon Sep 17 00:00:00 2001 From: hippoz Date: Mon, 2 Nov 2020 02:02:09 +0200 Subject: [PATCH] organize code --- wm.c | 79 ++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 48 insertions(+), 31 deletions(-) diff --git a/wm.c b/wm.c index 4464dbf..6147e83 100644 --- a/wm.c +++ b/wm.c @@ -2,22 +2,61 @@ #include #include +#include #define MODMASK Mod1Mask - #define MAX(a, b) ((a) > (b) ? (a) : (b)) -int main() { - Display* dpy; - XWindowAttributes attr; +static Display* dpy; +static XWindowAttributes attr; +static XButtonEvent start; - // Used to store the state of the cursor when resizing - XButtonEvent start; +void handle_event_keypress(XEvent *e) { + if (e->xkey.subwindow != None) { + KeySym keysym = XkbKeycodeToKeysym(dpy, e->xkey.keycode, 0, 0); + if (keysym == XK_q) { + XKillClient(dpy, e->xkey.subwindow); + } else if (keysym == XK_1) { + XRaiseWindow(dpy, e->xkey.subwindow); + } + } +} + +void handle_event_buttonpress(XEvent *e) { + if (e->xkey.subwindow != None) { + XGetWindowAttributes(dpy, e->xbutton.subwindow, &attr); + start = e->xbutton; + } +} + +void handle_event_motionnotify(XEvent *e) { + if (start.subwindow != None) { + int xdiff = e->xbutton.x_root - start.x_root; + int ydiff = e->xbutton.y_root - start.y_root; + XMoveResizeWindow(dpy, start.subwindow, + attr.x + (start.button==1 ? xdiff : 0), + attr.y + (start.button==1 ? ydiff : 0), + MAX(1, attr.width + (start.button==3 ? xdiff : 0)), + MAX(1, attr.height + (start.button==3 ? ydiff : 0))); + } +} + +void handle_event_buttonrelease(XEvent *e) { + start.subwindow = None; +} + +static void (*event_handlers[LASTEvent])(XEvent *e) = { + [KeyPress] = handle_event_keypress, + [ButtonPress] = handle_event_buttonpress, + [ButtonRelease] = handle_event_buttonrelease, + [MotionNotify] = handle_event_motionnotify +}; + +int main() { + if(!(dpy = XOpenDisplay(0x0))) return 1; XEvent ev; - if(!(dpy = XOpenDisplay(0x0))) return 1; - XGrabKey(dpy, XKeysymToKeycode(dpy, XStringToKeysym("F1")), MODMASK, DefaultRootWindow(dpy), True, GrabModeAsync, GrabModeAsync); XGrabKey(dpy, XKeysymToKeycode(dpy, XStringToKeysym("Q")), MODMASK, DefaultRootWindow(dpy), True, GrabModeAsync, GrabModeAsync); XGrabButton(dpy, 1, MODMASK, DefaultRootWindow(dpy), True, ButtonPressMask|ButtonReleaseMask|PointerMotionMask, GrabModeAsync, GrabModeAsync, None, None); @@ -27,29 +66,7 @@ int main() { for (;;) { XNextEvent(dpy, &ev); - - if (ev.type == KeyPress && ev.xkey.subwindow != None) { - KeySym keysym = XkbKeycodeToKeysym(dpy, ev.xkey.keycode, 0, 0); - if (keysym == XK_q) { - XKillClient(dpy, ev.xkey.subwindow); - } else { - XRaiseWindow(dpy, ev.xkey.subwindow); - } - } else if (ev.type == ButtonPress && ev.xbutton.subwindow != None) { - XGetWindowAttributes(dpy, ev.xbutton.subwindow, &attr); - start = ev.xbutton; - } else if (ev.type == MotionNotify && start.subwindow != None) { - int xdiff = ev.xbutton.x_root - start.x_root; - int ydiff = ev.xbutton.y_root - start.y_root; - XMoveResizeWindow(dpy, start.subwindow, - attr.x + (start.button==1 ? xdiff : 0), - attr.y + (start.button==1 ? ydiff : 0), - MAX(1, attr.width + (start.button==3 ? xdiff : 0)), - MAX(1, attr.height + (start.button==3 ? ydiff : 0))); - } else if (ev.type == ButtonRelease) { - start.subwindow = None; - } + if (event_handlers[ev.type]) event_handlers[ev.type](&ev); } - return 0; } \ No newline at end of file