dwm-meillo

annotate event.c @ 76:4bd49f404f10

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