+/* Callbacks */
+void process_event(int type, XEvent *ev, win_t *root)
+{
+ Display *dpy = root->sys->dpy;
+ Window rid = root->sys->xid;
+
+ /* Common data for all these events ... */
+ win_t *win = NULL; ptr_t ptr; mod_t mod;
+ if (type == KeyPress || type == KeyRelease ||
+ type == ButtonPress || type == ButtonRelease ||
+ type == MotionNotify) {
+ Window xid = ev->xkey.subwindow ?: rid;
+ win = win_find(dpy,xid);
+ ptr = x2ptr(ev);
+ mod = x2mod(ev->xkey.state, type==KeyRelease||type==ButtonRelease);
+ }
+
+ /* Split based on event */
+ //printf("event: %d\n", type);
+ if (type == KeyPress) {
+ while (XCheckTypedEvent(dpy, KeyPress, ev));
+ KeySym sym = XKeycodeToKeysym(dpy, ev->xkey.keycode, 0);
+ wm_handle_key(win, x2key(sym), mod, ptr);
+ }
+ else if (type == KeyRelease) {
+ //printf("release: %d\n", type);
+ }
+ else if (type == ButtonPress) {
+ wm_handle_key(win, x2btn(ev->xbutton.button), mod, ptr);
+ XGrabPointer(dpy, win->sys->xid, True, PointerMotionMask|ButtonReleaseMask,
+ GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
+ }
+ else if (type == ButtonRelease) {
+ XUngrabPointer(dpy, CurrentTime);
+ wm_handle_key(win, x2btn(ev->xbutton.button), mod, ptr);
+ }
+ else if (type == MotionNotify) {
+ while (XCheckTypedEvent(dpy, MotionNotify, ev));
+ wm_handle_ptr(win, ptr);
+ }
+ else if (type == ConfigureNotify) {
+ //printf("configure: %d\n", type);
+ }
+ else if (type == MapNotify) {
+ //printf("map: %d\n", type);
+ }
+ else if (type == DestroyNotify) {
+ //printf("destory: %d\n", type);
+ win_remove(win_find(dpy,ev->xdestroywindow.window));
+ }
+ else if (type == UnmapNotify) {
+ //printf("unmap: %d\n", type);
+ wm_remove(win_find(dpy,ev->xmap.window));
+ }
+ else if (type == ConfigureRequest) {
+ XConfigureRequestEvent *cre = &ev->xconfigurerequest;
+ XWindowChanges wc = {
+ .x = cre->x, .y = cre->y,
+ .width = cre->width, .height = cre->height,
+ };
+ printf("configure_req: %d - %x, (0x%lx) %dx%d @ %d,%d\n",
+ type, (int)cre->window, cre->value_mask,
+ cre->height, cre->width, cre->x, cre->y);
+ XConfigureWindow(dpy, cre->window, cre->value_mask, &wc);
+ }
+ else if (type == MapRequest) {
+ printf("map_req: %d\n", type);
+ wm_insert(win_find(dpy,ev->xmaprequest.window));
+ XMapWindow(dpy,ev->xmaprequest.window);
+ }
+ else {
+ printf("unknown event: %d\n", type);
+ }
+}
+