aewl

annotate event.c @ 261:d6fd632d861c

implement multi-tag selection through button3 click on the specific tag
author Anselm R.Garbe <arg@10ksloc.org>
date Fri, 11 Aug 2006 18:37:41 +0200
parents 7a11e4312b17
children d659a2dce2b5
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 #include <stdlib.h>
garbeam@5 7 #include <X11/keysym.h>
garbeam@13 8 #include <X11/Xatom.h>
garbeam@5 9
arg@146 10 /* static */
arg@114 11
arg@114 12 typedef struct {
arg@114 13 unsigned long mod;
arg@114 14 KeySym keysym;
arg@114 15 void (*func)(Arg *arg);
arg@114 16 Arg arg;
arg@114 17 } Key;
arg@114 18
arg@146 19 KEYS
garbeam@75 20
arg@160 21 #define CLEANMASK(mask) (mask & ~(NUMLOCKMASK | LockMask))
garbeam@5 22
garbeam@77 23 static void
garbeam@77 24 movemouse(Client *c)
garbeam@77 25 {
garbeam@77 26 int x1, y1, ocx, ocy, di;
garbeam@77 27 unsigned int dui;
garbeam@77 28 Window dummy;
arg@123 29 XEvent ev;
garbeam@77 30
arg@115 31 ocx = c->x;
arg@115 32 ocy = c->y;
arg@148 33 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
arg@123 34 None, cursor[CurMove], CurrentTime) != GrabSuccess)
garbeam@77 35 return;
garbeam@77 36 XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
garbeam@77 37 for(;;) {
arg@148 38 XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
garbeam@77 39 switch (ev.type) {
garbeam@77 40 default: break;
garbeam@77 41 case Expose:
garbeam@77 42 handler[Expose](&ev);
garbeam@77 43 break;
garbeam@77 44 case MotionNotify:
garbeam@79 45 XSync(dpy, False);
arg@115 46 c->x = ocx + (ev.xmotion.x - x1);
arg@115 47 c->y = ocy + (ev.xmotion.y - y1);
arg@99 48 resize(c, False, TopLeft);
garbeam@77 49 break;
garbeam@77 50 case ButtonRelease:
garbeam@77 51 XUngrabPointer(dpy, CurrentTime);
garbeam@77 52 return;
garbeam@77 53 }
garbeam@77 54 }
garbeam@77 55 }
garbeam@77 56
garbeam@77 57 static void
garbeam@77 58 resizemouse(Client *c)
garbeam@77 59 {
garbeam@77 60 int ocx, ocy;
arg@99 61 Corner sticky;
arg@123 62 XEvent ev;
garbeam@77 63
arg@115 64 ocx = c->x;
arg@115 65 ocy = c->y;
arg@148 66 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
garbeam@77 67 None, cursor[CurResize], CurrentTime) != GrabSuccess)
garbeam@77 68 return;
arg@115 69 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
garbeam@77 70 for(;;) {
arg@148 71 XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
garbeam@77 72 switch(ev.type) {
garbeam@77 73 default: break;
garbeam@77 74 case Expose:
garbeam@77 75 handler[Expose](&ev);
garbeam@77 76 break;
garbeam@77 77 case MotionNotify:
garbeam@79 78 XSync(dpy, False);
arg@115 79 c->w = abs(ocx - ev.xmotion.x);
arg@115 80 c->h = abs(ocy - ev.xmotion.y);
arg@115 81 c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
arg@115 82 c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
arg@105 83 if(ocx <= ev.xmotion.x)
arg@105 84 sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
arg@105 85 else
arg@105 86 sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
arg@99 87 resize(c, True, sticky);
garbeam@77 88 break;
garbeam@77 89 case ButtonRelease:
garbeam@77 90 XUngrabPointer(dpy, CurrentTime);
garbeam@77 91 return;
garbeam@77 92 }
garbeam@77 93 }
garbeam@77 94 }
garbeam@73 95
garbeam@73 96 static void
garbeam@18 97 buttonpress(XEvent *e)
garbeam@18 98 {
garbeam@73 99 int x;
garbeam@73 100 Arg a;
arg@123 101 Client *c;
garbeam@18 102 XButtonPressedEvent *ev = &e->xbutton;
garbeam@18 103
garbeam@73 104 if(barwin == ev->window) {
garbeam@80 105 switch(ev->button) {
arg@253 106 default:
garbeam@80 107 x = 0;
arg@178 108 for(a.i = 0; a.i < ntags; a.i++) {
garbeam@80 109 x += textw(tags[a.i]);
garbeam@80 110 if(ev->x < x) {
arg@261 111 if(ev->button == Button3) {
arg@261 112 tsel[a.i] = True;
arg@261 113 arrange(NULL);
arg@261 114 drawall();
arg@261 115 }
arg@261 116 else
arg@261 117 view(&a);
arg@252 118 return;
garbeam@80 119 }
garbeam@73 120 }
arg@253 121 if(ev->button == Button1)
arg@253 122 viewprev(&a);
arg@253 123 else if(ev->button == Button3)
arg@253 124 viewnext(&a);
garbeam@80 125 break;
garbeam@80 126 case Button4:
arg@239 127 viewprev(&a);
garbeam@80 128 break;
garbeam@80 129 case Button5:
arg@239 130 viewnext(&a);
garbeam@80 131 break;
garbeam@73 132 }
garbeam@73 133 }
garbeam@58 134 else if((c = getclient(ev->window))) {
arg@238 135 higher(c);
arg@143 136 focus(c);
garbeam@18 137 switch(ev->button) {
garbeam@18 138 default:
garbeam@18 139 break;
garbeam@18 140 case Button1:
arg@238 141 if(!c->ismax && (arrange == dofloat || c->isfloat))
arg@238 142 movemouse(c);
arg@238 143 break;
arg@238 144 case Button2:
arg@248 145 zoom(NULL);
garbeam@18 146 break;
garbeam@18 147 case Button3:
arg@238 148 if(!c->ismax && (arrange == dofloat || c->isfloat))
arg@99 149 resizemouse(c);
garbeam@18 150 break;
garbeam@18 151 }
garbeam@18 152 }
garbeam@18 153 }
garbeam@18 154
garbeam@18 155 static void
garbeam@5 156 configurerequest(XEvent *e)
garbeam@5 157 {
arg@123 158 Client *c;
garbeam@5 159 XConfigureRequestEvent *ev = &e->xconfigurerequest;
arg@164 160 XEvent synev;
garbeam@5 161 XWindowChanges wc;
arg@164 162 unsigned long newmask;
garbeam@5 163
garbeam@18 164 if((c = getclient(ev->window))) {
garbeam@29 165 gravitate(c, True);
arg@195 166 if(ev->value_mask & CWX)
arg@195 167 c->x = ev->x;
arg@195 168 if(ev->value_mask & CWY)
arg@195 169 c->y = ev->y;
arg@195 170 if(ev->value_mask & CWWidth)
arg@195 171 c->w = ev->width;
arg@195 172 if(ev->value_mask & CWHeight)
arg@195 173 c->h = ev->height;
garbeam@29 174 if(ev->value_mask & CWBorderWidth)
arg@164 175 c->border = ev->border_width;
garbeam@29 176 gravitate(c, False);
arg@164 177 wc.x = c->x;
arg@164 178 wc.y = c->y;
arg@164 179 wc.width = c->w;
arg@164 180 wc.height = c->h;
arg@164 181 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
arg@164 182 if(newmask)
arg@164 183 XConfigureWindow(dpy, c->win, newmask, &wc);
arg@164 184 else {
arg@164 185 synev.type = ConfigureNotify;
arg@164 186 synev.xconfigure.display = dpy;
arg@164 187 synev.xconfigure.event = c->win;
arg@164 188 synev.xconfigure.window = c->win;
arg@164 189 synev.xconfigure.x = c->x;
arg@164 190 synev.xconfigure.y = c->y;
arg@164 191 synev.xconfigure.width = c->w;
arg@164 192 synev.xconfigure.height = c->h;
arg@164 193 synev.xconfigure.border_width = c->border;
arg@164 194 synev.xconfigure.above = None;
arg@164 195 /* Send synthetic ConfigureNotify */
arg@164 196 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
arg@164 197 }
arg@195 198 XSync(dpy, False);
arg@196 199 if(c->isfloat)
arg@196 200 resize(c, False, TopLeft);
arg@196 201 else
arg@196 202 arrange(NULL);
garbeam@5 203 }
arg@164 204 else {
arg@164 205 wc.x = ev->x;
arg@164 206 wc.y = ev->y;
arg@164 207 wc.width = ev->width;
arg@164 208 wc.height = ev->height;
arg@164 209 wc.border_width = ev->border_width;
arg@164 210 wc.sibling = ev->above;
arg@164 211 wc.stack_mode = ev->detail;
arg@164 212 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
arg@195 213 XSync(dpy, False);
arg@164 214 }
garbeam@5 215 }
garbeam@5 216
garbeam@5 217 static void
garbeam@5 218 destroynotify(XEvent *e)
garbeam@5 219 {
garbeam@5 220 Client *c;
garbeam@5 221 XDestroyWindowEvent *ev = &e->xdestroywindow;
garbeam@5 222
garbeam@11 223 if((c = getclient(ev->window)))
garbeam@11 224 unmanage(c);
garbeam@5 225 }
garbeam@5 226
garbeam@5 227 static void
garbeam@5 228 enternotify(XEvent *e)
garbeam@5 229 {
arg@123 230 Client *c;
garbeam@5 231 XCrossingEvent *ev = &e->xcrossing;
garbeam@5 232
arg@232 233 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
garbeam@5 234 return;
garbeam@5 235
arg@161 236 if((c = getclient(ev->window)) || (c = getctitle(ev->window)))
garbeam@13 237 focus(c);
arg@239 238 else if(ev->window == root) {
garbeam@31 239 issel = True;
arg@239 240 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
arg@239 241 drawall();
arg@239 242 }
garbeam@5 243 }
garbeam@5 244
garbeam@5 245 static void
garbeam@5 246 expose(XEvent *e)
garbeam@5 247 {
arg@123 248 Client *c;
garbeam@5 249 XExposeEvent *ev = &e->xexpose;
garbeam@5 250
garbeam@5 251 if(ev->count == 0) {
garbeam@70 252 if(barwin == ev->window)
garbeam@74 253 drawstatus();
garbeam@75 254 else if((c = getctitle(ev->window)))
garbeam@74 255 drawtitle(c);
garbeam@5 256 }
garbeam@5 257 }
garbeam@5 258
garbeam@5 259 static void
garbeam@76 260 keypress(XEvent *e)
garbeam@76 261 {
arg@138 262 static unsigned int len = sizeof(key) / sizeof(key[0]);
garbeam@76 263 unsigned int i;
garbeam@76 264 KeySym keysym;
arg@123 265 XKeyEvent *ev = &e->xkey;
garbeam@76 266
garbeam@76 267 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
garbeam@76 268 for(i = 0; i < len; i++)
arg@160 269 if(keysym == key[i].keysym &&
arg@160 270 CLEANMASK(key[i].mod) == CLEANMASK(ev->state)) {
garbeam@76 271 if(key[i].func)
garbeam@76 272 key[i].func(&key[i].arg);
garbeam@76 273 return;
garbeam@76 274 }
garbeam@76 275 }
garbeam@76 276
garbeam@76 277 static void
garbeam@76 278 leavenotify(XEvent *e)
garbeam@76 279 {
garbeam@76 280 XCrossingEvent *ev = &e->xcrossing;
garbeam@76 281
arg@239 282 if((ev->window == root) && !ev->same_screen) {
arg@239 283 issel = False;
arg@239 284 drawall();
arg@239 285 }
garbeam@76 286 }
garbeam@76 287
garbeam@76 288 static void
garbeam@5 289 maprequest(XEvent *e)
garbeam@5 290 {
arg@123 291 static XWindowAttributes wa;
garbeam@5 292 XMapRequestEvent *ev = &e->xmaprequest;
garbeam@5 293
garbeam@5 294 if(!XGetWindowAttributes(dpy, ev->window, &wa))
garbeam@5 295 return;
garbeam@5 296
garbeam@5 297 if(wa.override_redirect) {
garbeam@5 298 XSelectInput(dpy, ev->window,
garbeam@5 299 (StructureNotifyMask | PropertyChangeMask));
garbeam@5 300 return;
garbeam@5 301 }
garbeam@5 302
garbeam@10 303 if(!getclient(ev->window))
garbeam@10 304 manage(ev->window, &wa);
garbeam@5 305 }
garbeam@5 306
garbeam@5 307 static void
garbeam@5 308 propertynotify(XEvent *e)
garbeam@5 309 {
arg@123 310 Client *c;
arg@123 311 Window trans;
garbeam@5 312 XPropertyEvent *ev = &e->xproperty;
garbeam@5 313
garbeam@5 314 if(ev->state == PropertyDelete)
garbeam@5 315 return; /* ignore */
garbeam@5 316
garbeam@13 317 if((c = getclient(ev->window))) {
garbeam@77 318 if(ev->atom == wmatom[WMProtocols]) {
garbeam@75 319 c->proto = getproto(c->win);
garbeam@30 320 return;
garbeam@30 321 }
garbeam@13 322 switch (ev->atom) {
garbeam@13 323 default: break;
garbeam@13 324 case XA_WM_TRANSIENT_FOR:
garbeam@53 325 XGetTransientForHint(dpy, c->win, &trans);
garbeam@80 326 if(!c->isfloat && (c->isfloat = (trans != 0)))
garbeam@53 327 arrange(NULL);
garbeam@13 328 break;
garbeam@13 329 case XA_WM_NORMAL_HINTS:
garbeam@74 330 setsize(c);
garbeam@13 331 break;
garbeam@13 332 }
garbeam@77 333 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
garbeam@74 334 settitle(c);
garbeam@74 335 drawtitle(c);
garbeam@13 336 }
garbeam@13 337 }
garbeam@5 338 }
garbeam@5 339
garbeam@5 340 static void
garbeam@5 341 unmapnotify(XEvent *e)
garbeam@5 342 {
garbeam@5 343 Client *c;
garbeam@5 344 XUnmapEvent *ev = &e->xunmap;
garbeam@5 345
garbeam@10 346 if((c = getclient(ev->window)))
garbeam@10 347 unmanage(c);
garbeam@5 348 }
garbeam@76 349
garbeam@84 350 /* extern */
garbeam@76 351
garbeam@76 352 void (*handler[LASTEvent]) (XEvent *) = {
garbeam@76 353 [ButtonPress] = buttonpress,
garbeam@76 354 [ConfigureRequest] = configurerequest,
garbeam@76 355 [DestroyNotify] = destroynotify,
garbeam@76 356 [EnterNotify] = enternotify,
garbeam@76 357 [LeaveNotify] = leavenotify,
garbeam@76 358 [Expose] = expose,
garbeam@76 359 [KeyPress] = keypress,
garbeam@76 360 [MapRequest] = maprequest,
garbeam@76 361 [PropertyNotify] = propertynotify,
garbeam@76 362 [UnmapNotify] = unmapnotify
garbeam@76 363 };
garbeam@76 364
garbeam@76 365 void
garbeam@76 366 grabkeys()
garbeam@76 367 {
arg@138 368 static unsigned int len = sizeof(key) / sizeof(key[0]);
garbeam@76 369 unsigned int i;
garbeam@76 370 KeyCode code;
garbeam@76 371
garbeam@76 372 for(i = 0; i < len; i++) {
garbeam@76 373 code = XKeysymToKeycode(dpy, key[i].keysym);
garbeam@76 374 XGrabKey(dpy, code, key[i].mod, root, True,
garbeam@76 375 GrabModeAsync, GrabModeAsync);
arg@160 376 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
arg@160 377 GrabModeAsync, GrabModeAsync);
arg@146 378 XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK, root, True,
arg@146 379 GrabModeAsync, GrabModeAsync);
arg@146 380 XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK | LockMask, root, True,
arg@146 381 GrabModeAsync, GrabModeAsync);
garbeam@76 382 }
garbeam@76 383 }