dwm-meillo

annotate event.c @ 231:f7e87c894b5e

added zoom on Mod1-Button1 on managed clients only (there is no moving possible)
author Anselm R.Garbe <arg@10ksloc.org>
date Wed, 09 Aug 2006 18:37:33 +0200
parents b92bbc2487c9
children 98e9901b1dbb
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) {
garbeam@80 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) {
garbeam@80 111 view(&a);
garbeam@80 112 break;
garbeam@80 113 }
garbeam@73 114 }
garbeam@80 115 break;
garbeam@80 116 case Button4:
arg@149 117 viewnext(&a);
garbeam@80 118 break;
garbeam@80 119 case Button5:
arg@149 120 viewprev(&a);
garbeam@80 121 break;
garbeam@73 122 }
garbeam@73 123 }
garbeam@58 124 else if((c = getclient(ev->window))) {
arg@143 125 focus(c);
garbeam@18 126 switch(ev->button) {
garbeam@18 127 default:
garbeam@18 128 break;
garbeam@18 129 case Button1:
arg@133 130 if(!c->ismax && (arrange == dofloat || c->isfloat)) {
arg@99 131 higher(c);
arg@99 132 movemouse(c);
arg@99 133 }
arg@231 134 else
arg@231 135 zoom(NULL);
garbeam@18 136 break;
garbeam@18 137 case Button2:
garbeam@26 138 lower(c);
garbeam@18 139 break;
garbeam@18 140 case Button3:
arg@133 141 if(!c->ismax && (arrange == dofloat || c->isfloat)) {
arg@99 142 higher(c);
arg@99 143 resizemouse(c);
arg@99 144 }
garbeam@18 145 break;
garbeam@18 146 }
garbeam@18 147 }
garbeam@18 148 }
garbeam@18 149
garbeam@18 150 static void
garbeam@5 151 configurerequest(XEvent *e)
garbeam@5 152 {
arg@123 153 Client *c;
garbeam@5 154 XConfigureRequestEvent *ev = &e->xconfigurerequest;
arg@164 155 XEvent synev;
garbeam@5 156 XWindowChanges wc;
arg@164 157 unsigned long newmask;
garbeam@5 158
garbeam@18 159 if((c = getclient(ev->window))) {
garbeam@29 160 gravitate(c, True);
arg@195 161 if(ev->value_mask & CWX)
arg@195 162 c->x = ev->x;
arg@195 163 if(ev->value_mask & CWY)
arg@195 164 c->y = ev->y;
arg@195 165 if(ev->value_mask & CWWidth)
arg@195 166 c->w = ev->width;
arg@195 167 if(ev->value_mask & CWHeight)
arg@195 168 c->h = ev->height;
garbeam@29 169 if(ev->value_mask & CWBorderWidth)
arg@164 170 c->border = ev->border_width;
garbeam@29 171 gravitate(c, False);
arg@164 172 wc.x = c->x;
arg@164 173 wc.y = c->y;
arg@164 174 wc.width = c->w;
arg@164 175 wc.height = c->h;
arg@164 176 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
arg@164 177 if(newmask)
arg@164 178 XConfigureWindow(dpy, c->win, newmask, &wc);
arg@164 179 else {
arg@164 180 synev.type = ConfigureNotify;
arg@164 181 synev.xconfigure.display = dpy;
arg@164 182 synev.xconfigure.event = c->win;
arg@164 183 synev.xconfigure.window = c->win;
arg@164 184 synev.xconfigure.x = c->x;
arg@164 185 synev.xconfigure.y = c->y;
arg@164 186 synev.xconfigure.width = c->w;
arg@164 187 synev.xconfigure.height = c->h;
arg@164 188 synev.xconfigure.border_width = c->border;
arg@164 189 synev.xconfigure.above = None;
arg@164 190 /* Send synthetic ConfigureNotify */
arg@164 191 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
arg@164 192 }
arg@195 193 XSync(dpy, False);
arg@196 194 if(c->isfloat)
arg@196 195 resize(c, False, TopLeft);
arg@196 196 else
arg@196 197 arrange(NULL);
garbeam@5 198 }
arg@164 199 else {
arg@164 200 wc.x = ev->x;
arg@164 201 wc.y = ev->y;
arg@164 202 wc.width = ev->width;
arg@164 203 wc.height = ev->height;
arg@164 204 wc.border_width = ev->border_width;
arg@164 205 wc.sibling = ev->above;
arg@164 206 wc.stack_mode = ev->detail;
arg@164 207 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
arg@195 208 XSync(dpy, False);
arg@164 209 }
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 {
arg@123 225 Client *c;
garbeam@5 226 XCrossingEvent *ev = &e->xcrossing;
garbeam@5 227
arg@143 228 if(ev->detail == NotifyInferior)
garbeam@5 229 return;
garbeam@5 230
arg@161 231 if((c = getclient(ev->window)) || (c = getctitle(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 {
arg@123 240 Client *c;
garbeam@5 241 XExposeEvent *ev = &e->xexpose;
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 {
arg@138 254 static unsigned int len = sizeof(key) / sizeof(key[0]);
garbeam@76 255 unsigned int i;
garbeam@76 256 KeySym keysym;
arg@123 257 XKeyEvent *ev = &e->xkey;
garbeam@76 258
garbeam@76 259 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
garbeam@76 260 for(i = 0; i < len; i++)
arg@160 261 if(keysym == key[i].keysym &&
arg@160 262 CLEANMASK(key[i].mod) == CLEANMASK(ev->state)) {
garbeam@76 263 if(key[i].func)
garbeam@76 264 key[i].func(&key[i].arg);
garbeam@76 265 return;
garbeam@76 266 }
garbeam@76 267 }
garbeam@76 268
garbeam@76 269 static void
garbeam@76 270 leavenotify(XEvent *e)
garbeam@76 271 {
garbeam@76 272 XCrossingEvent *ev = &e->xcrossing;
garbeam@76 273
garbeam@76 274 if((ev->window == root) && !ev->same_screen)
garbeam@76 275 issel = True;
garbeam@76 276 }
garbeam@76 277
garbeam@76 278 static void
garbeam@5 279 maprequest(XEvent *e)
garbeam@5 280 {
arg@123 281 static XWindowAttributes wa;
garbeam@5 282 XMapRequestEvent *ev = &e->xmaprequest;
garbeam@5 283
garbeam@5 284 if(!XGetWindowAttributes(dpy, ev->window, &wa))
garbeam@5 285 return;
garbeam@5 286
garbeam@5 287 if(wa.override_redirect) {
garbeam@5 288 XSelectInput(dpy, ev->window,
garbeam@5 289 (StructureNotifyMask | PropertyChangeMask));
garbeam@5 290 return;
garbeam@5 291 }
garbeam@5 292
garbeam@10 293 if(!getclient(ev->window))
garbeam@10 294 manage(ev->window, &wa);
garbeam@5 295 }
garbeam@5 296
garbeam@5 297 static void
garbeam@5 298 propertynotify(XEvent *e)
garbeam@5 299 {
arg@123 300 Client *c;
arg@123 301 Window trans;
garbeam@5 302 XPropertyEvent *ev = &e->xproperty;
garbeam@5 303
garbeam@5 304 if(ev->state == PropertyDelete)
garbeam@5 305 return; /* ignore */
garbeam@5 306
garbeam@13 307 if((c = getclient(ev->window))) {
garbeam@77 308 if(ev->atom == wmatom[WMProtocols]) {
garbeam@75 309 c->proto = getproto(c->win);
garbeam@30 310 return;
garbeam@30 311 }
garbeam@13 312 switch (ev->atom) {
garbeam@13 313 default: break;
garbeam@13 314 case XA_WM_TRANSIENT_FOR:
garbeam@53 315 XGetTransientForHint(dpy, c->win, &trans);
garbeam@80 316 if(!c->isfloat && (c->isfloat = (trans != 0)))
garbeam@53 317 arrange(NULL);
garbeam@13 318 break;
garbeam@13 319 case XA_WM_NORMAL_HINTS:
garbeam@74 320 setsize(c);
garbeam@13 321 break;
garbeam@13 322 }
garbeam@77 323 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
garbeam@74 324 settitle(c);
garbeam@74 325 drawtitle(c);
garbeam@13 326 }
garbeam@13 327 }
garbeam@5 328 }
garbeam@5 329
garbeam@5 330 static void
garbeam@5 331 unmapnotify(XEvent *e)
garbeam@5 332 {
garbeam@5 333 Client *c;
garbeam@5 334 XUnmapEvent *ev = &e->xunmap;
garbeam@5 335
garbeam@10 336 if((c = getclient(ev->window)))
garbeam@10 337 unmanage(c);
garbeam@5 338 }
garbeam@76 339
garbeam@84 340 /* extern */
garbeam@76 341
garbeam@76 342 void (*handler[LASTEvent]) (XEvent *) = {
garbeam@76 343 [ButtonPress] = buttonpress,
garbeam@76 344 [ConfigureRequest] = configurerequest,
garbeam@76 345 [DestroyNotify] = destroynotify,
garbeam@76 346 [EnterNotify] = enternotify,
garbeam@76 347 [LeaveNotify] = leavenotify,
garbeam@76 348 [Expose] = expose,
garbeam@76 349 [KeyPress] = keypress,
garbeam@76 350 [MapRequest] = maprequest,
garbeam@76 351 [PropertyNotify] = propertynotify,
garbeam@76 352 [UnmapNotify] = unmapnotify
garbeam@76 353 };
garbeam@76 354
garbeam@76 355 void
garbeam@76 356 grabkeys()
garbeam@76 357 {
arg@138 358 static unsigned int len = sizeof(key) / sizeof(key[0]);
garbeam@76 359 unsigned int i;
garbeam@76 360 KeyCode code;
garbeam@76 361
garbeam@76 362 for(i = 0; i < len; i++) {
garbeam@76 363 code = XKeysymToKeycode(dpy, key[i].keysym);
garbeam@76 364 XGrabKey(dpy, code, key[i].mod, root, True,
garbeam@76 365 GrabModeAsync, GrabModeAsync);
arg@160 366 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
arg@160 367 GrabModeAsync, GrabModeAsync);
arg@146 368 XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK, root, True,
arg@146 369 GrabModeAsync, GrabModeAsync);
arg@146 370 XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK | LockMask, root, True,
arg@146 371 GrabModeAsync, GrabModeAsync);
garbeam@76 372 }
garbeam@76 373 }