dwm-meillo

annotate event.c @ 158:4e42dfc0f61f

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