dwm-meillo

annotate event.c @ 85:0a6472e22039

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