aewl

annotate event.c @ 78:0d71fb80b592

changing XFlush into XSync
author Anselm R. Garbe <garbeam@wmii.de>
date Sat, 15 Jul 2006 18:11:14 +0200
parents 38c8f7f7d401
children aabebd6e61f3
rev   line source
garbeam@5 1 /*
garbeam@5 2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
garbeam@5 3 * See LICENSE file for license details.
garbeam@5 4 */
garbeam@76 5 #include "dwm.h"
garbeam@5 6
garbeam@5 7 #include <stdlib.h>
garbeam@5 8 #include <X11/keysym.h>
garbeam@13 9 #include <X11/Xatom.h>
garbeam@5 10
garbeam@73 11 #define ButtonMask (ButtonPressMask | ButtonReleaseMask)
garbeam@73 12 #define MouseMask (ButtonMask | PointerMotionMask)
garbeam@73 13
garbeam@75 14 /********** CUSTOMIZE **********/
garbeam@75 15
garbeam@75 16 const char *term[] = {
garbeam@75 17 "urxvtc", "-tr", "+sb", "-bg", "black", "-fg", "white", "-fn",
garbeam@75 18 "-*-terminus-medium-*-*-*-13-*-*-*-*-*-iso10646-*",NULL
garbeam@75 19 };
garbeam@75 20 const char *browse[] = { "firefox", NULL };
garbeam@75 21 const char *xlock[] = { "xlock", NULL };
garbeam@75 22
garbeam@75 23 Key key[] = {
garbeam@75 24 /* modifier key function arguments */
garbeam@75 25 { Mod1Mask, XK_Return, zoom, { 0 } },
garbeam@75 26 { Mod1Mask, XK_k, focusprev, { 0 } },
garbeam@75 27 { Mod1Mask, XK_j, focusnext, { 0 } },
garbeam@75 28 { Mod1Mask, XK_m, maximize, { 0 } },
garbeam@75 29 { Mod1Mask, XK_0, view, { .i = Tscratch } },
garbeam@75 30 { Mod1Mask, XK_1, view, { .i = Tdev } },
garbeam@75 31 { Mod1Mask, XK_2, view, { .i = Twww } },
garbeam@75 32 { Mod1Mask, XK_3, view, { .i = Twork } },
garbeam@75 33 { Mod1Mask, XK_space, dotile, { 0 } },
garbeam@75 34 { Mod1Mask|ShiftMask, XK_space, dofloat, { 0 } },
garbeam@75 35 { Mod1Mask|ShiftMask, XK_0, replacetag, { .i = Tscratch } },
garbeam@75 36 { Mod1Mask|ShiftMask, XK_1, replacetag, { .i = Tdev } },
garbeam@75 37 { Mod1Mask|ShiftMask, XK_2, replacetag, { .i = Twww } },
garbeam@75 38 { Mod1Mask|ShiftMask, XK_3, replacetag, { .i = Twork } },
garbeam@75 39 { Mod1Mask|ShiftMask, XK_c, killclient, { 0 } },
garbeam@75 40 { Mod1Mask|ShiftMask, XK_q, quit, { 0 } },
garbeam@75 41 { Mod1Mask|ShiftMask, XK_Return, spawn, { .argv = term } },
garbeam@75 42 { Mod1Mask|ShiftMask, XK_w, spawn, { .argv = browse } },
garbeam@75 43 { Mod1Mask|ShiftMask, XK_l, spawn, { .argv = xlock } },
garbeam@75 44 { ControlMask, XK_0, appendtag, { .i = Tscratch } },
garbeam@75 45 { ControlMask, XK_1, appendtag, { .i = Tdev } },
garbeam@75 46 { ControlMask, XK_2, appendtag, { .i = Twww } },
garbeam@75 47 { ControlMask, XK_3, appendtag, { .i = Twork } },
garbeam@75 48 };
garbeam@75 49
garbeam@75 50 /********** CUSTOMIZE **********/
garbeam@75 51
garbeam@76 52 /* static functions */
garbeam@5 53
garbeam@77 54 static void
garbeam@77 55 movemouse(Client *c)
garbeam@77 56 {
garbeam@77 57 XEvent ev;
garbeam@77 58 int x1, y1, ocx, ocy, di;
garbeam@77 59 unsigned int dui;
garbeam@77 60 Window dummy;
garbeam@77 61
garbeam@77 62 ocx = c->x;
garbeam@77 63 ocy = c->y;
garbeam@77 64 if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
garbeam@77 65 None, cursor[CurMove], CurrentTime) != GrabSuccess)
garbeam@77 66 return;
garbeam@77 67 XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
garbeam@77 68 for(;;) {
garbeam@77 69 XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
garbeam@77 70 switch (ev.type) {
garbeam@77 71 default: break;
garbeam@77 72 case Expose:
garbeam@77 73 handler[Expose](&ev);
garbeam@77 74 break;
garbeam@77 75 case MotionNotify:
garbeam@77 76 XFlush(dpy);
garbeam@77 77 c->x = ocx + (ev.xmotion.x - x1);
garbeam@77 78 c->y = ocy + (ev.xmotion.y - y1);
garbeam@77 79 resize(c, False);
garbeam@77 80 break;
garbeam@77 81 case ButtonRelease:
garbeam@77 82 XUngrabPointer(dpy, CurrentTime);
garbeam@77 83 return;
garbeam@77 84 }
garbeam@77 85 }
garbeam@77 86 }
garbeam@77 87
garbeam@77 88 static void
garbeam@77 89 resizemouse(Client *c)
garbeam@77 90 {
garbeam@77 91 XEvent ev;
garbeam@77 92 int ocx, ocy;
garbeam@77 93
garbeam@77 94 ocx = c->x;
garbeam@77 95 ocy = c->y;
garbeam@77 96 if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
garbeam@77 97 None, cursor[CurResize], CurrentTime) != GrabSuccess)
garbeam@77 98 return;
garbeam@77 99 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
garbeam@77 100 for(;;) {
garbeam@77 101 XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
garbeam@77 102 switch(ev.type) {
garbeam@77 103 default: break;
garbeam@77 104 case Expose:
garbeam@77 105 handler[Expose](&ev);
garbeam@77 106 break;
garbeam@77 107 case MotionNotify:
garbeam@77 108 XFlush(dpy);
garbeam@77 109 c->w = abs(ocx - ev.xmotion.x);
garbeam@77 110 c->h = abs(ocy - ev.xmotion.y);
garbeam@77 111 c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
garbeam@77 112 c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
garbeam@77 113 resize(c, True);
garbeam@77 114 break;
garbeam@77 115 case ButtonRelease:
garbeam@77 116 XUngrabPointer(dpy, CurrentTime);
garbeam@77 117 return;
garbeam@77 118 }
garbeam@77 119 }
garbeam@77 120 }
garbeam@73 121
garbeam@73 122 static void
garbeam@18 123 buttonpress(XEvent *e)
garbeam@18 124 {
garbeam@73 125 int x;
garbeam@73 126 Arg a;
garbeam@18 127 XButtonPressedEvent *ev = &e->xbutton;
garbeam@18 128 Client *c;
garbeam@18 129
garbeam@73 130 if(barwin == ev->window) {
garbeam@78 131 x = 0;
garbeam@73 132 for(a.i = 0; a.i < TLast; a.i++) {
garbeam@73 133 x += textw(tags[a.i]);
garbeam@73 134 if(ev->x < x) {
garbeam@73 135 view(&a);
garbeam@73 136 break;
garbeam@73 137 }
garbeam@73 138 }
garbeam@73 139 }
garbeam@58 140 else if((c = getclient(ev->window))) {
garbeam@75 141 if(arrange == dotile && !c->dofloat)
garbeam@73 142 return;
garbeam@74 143 higher(c);
garbeam@18 144 switch(ev->button) {
garbeam@18 145 default:
garbeam@18 146 break;
garbeam@18 147 case Button1:
garbeam@75 148 movemouse(c);
garbeam@18 149 break;
garbeam@18 150 case Button2:
garbeam@26 151 lower(c);
garbeam@18 152 break;
garbeam@18 153 case Button3:
garbeam@75 154 resizemouse(c);
garbeam@18 155 break;
garbeam@18 156 }
garbeam@18 157 }
garbeam@18 158 }
garbeam@18 159
garbeam@18 160 static void
garbeam@5 161 configurerequest(XEvent *e)
garbeam@5 162 {
garbeam@5 163 XConfigureRequestEvent *ev = &e->xconfigurerequest;
garbeam@5 164 XWindowChanges wc;
garbeam@5 165 Client *c;
garbeam@5 166
garbeam@5 167 ev->value_mask &= ~CWSibling;
garbeam@18 168 if((c = getclient(ev->window))) {
garbeam@29 169 gravitate(c, True);
garbeam@5 170 if(ev->value_mask & CWX)
garbeam@20 171 c->x = ev->x;
garbeam@5 172 if(ev->value_mask & CWY)
garbeam@20 173 c->y = ev->y;
garbeam@5 174 if(ev->value_mask & CWWidth)
garbeam@20 175 c->w = ev->width;
garbeam@5 176 if(ev->value_mask & CWHeight)
garbeam@20 177 c->h = ev->height;
garbeam@29 178 if(ev->value_mask & CWBorderWidth)
garbeam@55 179 c->border = 1;
garbeam@29 180 gravitate(c, False);
garbeam@53 181 resize(c, True);
garbeam@5 182 }
garbeam@5 183
garbeam@5 184 wc.x = ev->x;
garbeam@5 185 wc.y = ev->y;
garbeam@5 186 wc.width = ev->width;
garbeam@5 187 wc.height = ev->height;
garbeam@25 188 wc.border_width = 1;
garbeam@5 189 wc.sibling = None;
garbeam@5 190 wc.stack_mode = Above;
garbeam@5 191 ev->value_mask &= ~CWStackMode;
garbeam@5 192 ev->value_mask |= CWBorderWidth;
garbeam@5 193 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
garbeam@5 194 XFlush(dpy);
garbeam@5 195 }
garbeam@5 196
garbeam@5 197 static void
garbeam@5 198 destroynotify(XEvent *e)
garbeam@5 199 {
garbeam@5 200 Client *c;
garbeam@5 201 XDestroyWindowEvent *ev = &e->xdestroywindow;
garbeam@5 202
garbeam@11 203 if((c = getclient(ev->window)))
garbeam@11 204 unmanage(c);
garbeam@5 205 }
garbeam@5 206
garbeam@5 207 static void
garbeam@5 208 enternotify(XEvent *e)
garbeam@5 209 {
garbeam@5 210 XCrossingEvent *ev = &e->xcrossing;
garbeam@5 211 Client *c;
garbeam@5 212
garbeam@5 213 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
garbeam@5 214 return;
garbeam@5 215
garbeam@13 216 if((c = getclient(ev->window)))
garbeam@13 217 focus(c);
garbeam@26 218 else if(ev->window == root)
garbeam@31 219 issel = True;
garbeam@5 220 }
garbeam@5 221
garbeam@5 222 static void
garbeam@5 223 expose(XEvent *e)
garbeam@5 224 {
garbeam@5 225 XExposeEvent *ev = &e->xexpose;
garbeam@21 226 Client *c;
garbeam@5 227
garbeam@5 228 if(ev->count == 0) {
garbeam@70 229 if(barwin == ev->window)
garbeam@74 230 drawstatus();
garbeam@75 231 else if((c = getctitle(ev->window)))
garbeam@74 232 drawtitle(c);
garbeam@5 233 }
garbeam@5 234 }
garbeam@5 235
garbeam@5 236 static void
garbeam@76 237 keypress(XEvent *e)
garbeam@76 238 {
garbeam@76 239 XKeyEvent *ev = &e->xkey;
garbeam@76 240 static unsigned int len = key ? sizeof(key) / sizeof(key[0]) : 0;
garbeam@76 241 unsigned int i;
garbeam@76 242 KeySym keysym;
garbeam@76 243
garbeam@76 244 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
garbeam@76 245 for(i = 0; i < len; i++)
garbeam@76 246 if((keysym == key[i].keysym) && (key[i].mod == ev->state)) {
garbeam@76 247 if(key[i].func)
garbeam@76 248 key[i].func(&key[i].arg);
garbeam@76 249 return;
garbeam@76 250 }
garbeam@76 251 }
garbeam@76 252
garbeam@76 253 static void
garbeam@76 254 leavenotify(XEvent *e)
garbeam@76 255 {
garbeam@76 256 XCrossingEvent *ev = &e->xcrossing;
garbeam@76 257
garbeam@76 258 if((ev->window == root) && !ev->same_screen)
garbeam@76 259 issel = True;
garbeam@76 260 }
garbeam@76 261
garbeam@76 262 static void
garbeam@5 263 maprequest(XEvent *e)
garbeam@5 264 {
garbeam@5 265 XMapRequestEvent *ev = &e->xmaprequest;
garbeam@5 266 static XWindowAttributes wa;
garbeam@5 267
garbeam@5 268 if(!XGetWindowAttributes(dpy, ev->window, &wa))
garbeam@5 269 return;
garbeam@5 270
garbeam@5 271 if(wa.override_redirect) {
garbeam@5 272 XSelectInput(dpy, ev->window,
garbeam@5 273 (StructureNotifyMask | PropertyChangeMask));
garbeam@5 274 return;
garbeam@5 275 }
garbeam@5 276
garbeam@10 277 if(!getclient(ev->window))
garbeam@10 278 manage(ev->window, &wa);
garbeam@5 279 }
garbeam@5 280
garbeam@5 281 static void
garbeam@5 282 propertynotify(XEvent *e)
garbeam@5 283 {
garbeam@5 284 XPropertyEvent *ev = &e->xproperty;
garbeam@53 285 Window trans;
garbeam@5 286 Client *c;
garbeam@5 287
garbeam@5 288 if(ev->state == PropertyDelete)
garbeam@5 289 return; /* ignore */
garbeam@5 290
garbeam@13 291 if((c = getclient(ev->window))) {
garbeam@77 292 if(ev->atom == wmatom[WMProtocols]) {
garbeam@75 293 c->proto = getproto(c->win);
garbeam@30 294 return;
garbeam@30 295 }
garbeam@13 296 switch (ev->atom) {
garbeam@13 297 default: break;
garbeam@13 298 case XA_WM_TRANSIENT_FOR:
garbeam@53 299 XGetTransientForHint(dpy, c->win, &trans);
garbeam@75 300 if(!c->dofloat && (c->dofloat = (trans != 0)))
garbeam@53 301 arrange(NULL);
garbeam@13 302 break;
garbeam@13 303 case XA_WM_NORMAL_HINTS:
garbeam@74 304 setsize(c);
garbeam@13 305 break;
garbeam@13 306 }
garbeam@77 307 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
garbeam@74 308 settitle(c);
garbeam@74 309 drawtitle(c);
garbeam@13 310 }
garbeam@13 311 }
garbeam@5 312 }
garbeam@5 313
garbeam@5 314 static void
garbeam@5 315 unmapnotify(XEvent *e)
garbeam@5 316 {
garbeam@5 317 Client *c;
garbeam@5 318 XUnmapEvent *ev = &e->xunmap;
garbeam@5 319
garbeam@10 320 if((c = getclient(ev->window)))
garbeam@10 321 unmanage(c);
garbeam@5 322 }
garbeam@76 323
garbeam@76 324 /* extern functions */
garbeam@76 325
garbeam@76 326 void (*handler[LASTEvent]) (XEvent *) = {
garbeam@76 327 [ButtonPress] = buttonpress,
garbeam@76 328 [ConfigureRequest] = configurerequest,
garbeam@76 329 [DestroyNotify] = destroynotify,
garbeam@76 330 [EnterNotify] = enternotify,
garbeam@76 331 [LeaveNotify] = leavenotify,
garbeam@76 332 [Expose] = expose,
garbeam@76 333 [KeyPress] = keypress,
garbeam@76 334 [MapRequest] = maprequest,
garbeam@76 335 [PropertyNotify] = propertynotify,
garbeam@76 336 [UnmapNotify] = unmapnotify
garbeam@76 337 };
garbeam@76 338
garbeam@76 339 void
garbeam@76 340 grabkeys()
garbeam@76 341 {
garbeam@76 342 static unsigned int len = key ? sizeof(key) / sizeof(key[0]) : 0;
garbeam@76 343 unsigned int i;
garbeam@76 344 KeyCode code;
garbeam@76 345
garbeam@76 346 for(i = 0; i < len; i++) {
garbeam@76 347 code = XKeysymToKeycode(dpy, key[i].keysym);
garbeam@76 348 XUngrabKey(dpy, code, key[i].mod, root);
garbeam@76 349 XGrabKey(dpy, code, key[i].mod, root, True,
garbeam@76 350 GrabModeAsync, GrabModeAsync);
garbeam@76 351 }
garbeam@76 352 }