dwm-meillo

annotate event.c @ 75:f08271b7cb20

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