aewl

annotate client.c @ 725:d99be681d502

handling WM_STATE seems to make DnD in gtk/qt apps working, well let's handle this in dwm as well
author Anselm R. Garbe <arg@suckless.org>
date Sun, 28 Jan 2007 20:29:41 +0100
parents d3876aa79292
children 8997e28553a8
rev   line source
arg@644 1 /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
garbeam@5 2 * See LICENSE file for license details.
garbeam@5 3 */
garbeam@76 4 #include "dwm.h"
garbeam@10 5 #include <stdlib.h>
garbeam@5 6 #include <string.h>
garbeam@5 7 #include <X11/Xatom.h>
garbeam@32 8 #include <X11/Xutil.h>
garbeam@5 9
garbeam@76 10 /* static functions */
garbeam@50 11
garbeam@26 12 static void
arg@461 13 detachstack(Client *c) {
arg@446 14 Client **tc;
arg@446 15 for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
arg@446 16 *tc = c->snext;
arg@446 17 }
arg@446 18
arg@446 19 static void
arg@461 20 grabbuttons(Client *c, Bool focused) {
arg@372 21 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
arg@372 22
arg@452 23 if(focused) {
arg@372 24 XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
arg@372 25 GrabModeAsync, GrabModeSync, None, None);
arg@372 26 XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
arg@372 27 GrabModeAsync, GrabModeSync, None, None);
arg@372 28 XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
arg@372 29 GrabModeAsync, GrabModeSync, None, None);
arg@372 30 XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
arg@372 31 GrabModeAsync, GrabModeSync, None, None);
arg@372 32
arg@372 33 XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
arg@372 34 GrabModeAsync, GrabModeSync, None, None);
arg@372 35 XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
arg@372 36 GrabModeAsync, GrabModeSync, None, None);
arg@372 37 XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
arg@372 38 GrabModeAsync, GrabModeSync, None, None);
arg@372 39 XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
arg@372 40 GrabModeAsync, GrabModeSync, None, None);
arg@372 41
arg@372 42 XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
arg@372 43 GrabModeAsync, GrabModeSync, None, None);
arg@372 44 XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
arg@372 45 GrabModeAsync, GrabModeSync, None, None);
arg@372 46 XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
arg@372 47 GrabModeAsync, GrabModeSync, None, None);
arg@372 48 XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
arg@372 49 GrabModeAsync, GrabModeSync, None, None);
arg@372 50 }
arg@372 51 else
arg@372 52 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
arg@372 53 GrabModeAsync, GrabModeSync, None, None);
arg@318 54 }
arg@318 55
arg@725 56 static void
arg@725 57 setclientstate(Client *c, long state) {
arg@725 58 long data[] = {state, None};
arg@725 59 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
arg@725 60 PropModeReplace, (unsigned char *)data, 2);
arg@725 61 }
arg@725 62
garbeam@76 63 static int
arg@461 64 xerrordummy(Display *dsply, XErrorEvent *ee) {
garbeam@76 65 return 0;
garbeam@5 66 }
garbeam@5 67
garbeam@76 68 /* extern functions */
garbeam@20 69
garbeam@20 70 void
arg@491 71 configure(Client *c) {
arg@491 72 XEvent synev;
arg@491 73
arg@491 74 synev.type = ConfigureNotify;
arg@491 75 synev.xconfigure.display = dpy;
arg@491 76 synev.xconfigure.event = c->win;
arg@491 77 synev.xconfigure.window = c->win;
arg@491 78 synev.xconfigure.x = c->x;
arg@491 79 synev.xconfigure.y = c->y;
arg@491 80 synev.xconfigure.width = c->w;
arg@491 81 synev.xconfigure.height = c->h;
arg@491 82 synev.xconfigure.border_width = c->border;
arg@491 83 synev.xconfigure.above = None;
arg@491 84 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
arg@491 85 }
arg@491 86
arg@491 87 void
arg@461 88 focus(Client *c) {
arg@709 89 if(c && !isvisible(c))
arg@239 90 return;
arg@712 91 if(sel && sel != c) {
arg@712 92 grabbuttons(sel, False);
arg@712 93 XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
arg@318 94 }
arg@400 95 if(c) {
arg@711 96 detachstack(c);
arg@711 97 c->snext = stack;
arg@711 98 stack = c;
arg@711 99 grabbuttons(c, True);
arg@714 100 }
arg@714 101 sel = c;
arg@714 102 drawstatus();
arg@716 103 if(!selscreen)
arg@714 104 return;
arg@715 105 if(c) {
arg@711 106 XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
arg@711 107 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
arg@400 108 }
arg@712 109 else
arg@400 110 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
garbeam@13 111 }
garbeam@13 112
garbeam@76 113 Client *
arg@461 114 getclient(Window w) {
garbeam@76 115 Client *c;
arg@123 116
garbeam@76 117 for(c = clients; c; c = c->next)
garbeam@76 118 if(c->win == w)
garbeam@76 119 return c;
garbeam@76 120 return NULL;
garbeam@76 121 }
garbeam@76 122
garbeam@76 123 void
arg@461 124 killclient(Arg *arg) {
garbeam@76 125 if(!sel)
garbeam@76 126 return;
arg@157 127 if(sel->proto & PROTODELWIN)
garbeam@77 128 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
garbeam@76 129 else
garbeam@76 130 XKillClient(dpy, sel->win);
garbeam@76 131 }
garbeam@76 132
garbeam@76 133 void
arg@461 134 manage(Window w, XWindowAttributes *wa) {
arg@431 135 Client *c;
arg@123 136 Window trans;
garbeam@5 137
garbeam@5 138 c = emallocz(sizeof(Client));
arg@178 139 c->tags = emallocz(ntags * sizeof(Bool));
garbeam@5 140 c->win = w;
arg@687 141 c->x = wa->x;
arg@687 142 c->y = wa->y;
arg@687 143 c->w = wa->width;
arg@115 144 c->h = wa->height;
arg@708 145 if(c->w == sw && c->h == sh) {
arg@708 146 c->border = 0;
arg@708 147 c->x = sx;
arg@708 148 c->y = sy;
arg@708 149 }
arg@708 150 else {
arg@708 151 c->border = BORDERPX;
arg@718 152 if(c->x + c->w + 2 * c->border > wax + waw)
arg@718 153 c->x = wax + waw - c->w - 2 * c->border;
arg@718 154 if(c->y + c->h + 2 * c->border > way + wah)
arg@718 155 c->y = way + wah - c->h - 2 * c->border;
arg@708 156 if(c->x < wax)
arg@708 157 c->x = wax;
arg@708 158 if(c->y < way)
arg@708 159 c->y = way;
arg@708 160 }
arg@639 161 updatesizehints(c);
garbeam@75 162 c->proto = getproto(c->win);
garbeam@26 163 XSelectInput(dpy, c->win,
arg@127 164 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
garbeam@53 165 XGetTransientForHint(dpy, c->win, &trans);
arg@372 166 grabbuttons(c, False);
arg@715 167 XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
arg@500 168 updatetitle(c);
arg@431 169 settags(c, getclient(trans));
garbeam@80 170 if(!c->isfloat)
arg@549 171 c->isfloat = trans || c->isfixed;
arg@381 172 if(clients)
arg@381 173 clients->prev = c;
arg@381 174 c->next = clients;
arg@446 175 c->snext = stack;
arg@446 176 stack = clients = c;
arg@687 177 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
arg@270 178 XMapWindow(dpy, c->win);
arg@725 179 setclientstate(c, NormalState);
arg@261 180 if(isvisible(c))
garbeam@51 181 focus(c);
arg@533 182 arrange();
garbeam@94 183 }
garbeam@94 184
garbeam@94 185 void
arg@708 186 resize(Client *c, Bool sizehints) {
arg@163 187 XWindowChanges wc;
garbeam@18 188
arg@721 189 if(c->w <= 0 || c->h <= 0)
arg@721 190 return;
arg@129 191 if(sizehints) {
garbeam@52 192 if(c->incw)
arg@115 193 c->w -= (c->w - c->basew) % c->incw;
garbeam@52 194 if(c->inch)
arg@115 195 c->h -= (c->h - c->baseh) % c->inch;
arg@129 196 if(c->minw && c->w < c->minw)
arg@129 197 c->w = c->minw;
arg@129 198 if(c->minh && c->h < c->minh)
arg@129 199 c->h = c->minh;
arg@129 200 if(c->maxw && c->w > c->maxw)
arg@129 201 c->w = c->maxw;
arg@129 202 if(c->maxh && c->h > c->maxh)
arg@129 203 c->h = c->maxh;
garbeam@52 204 }
arg@708 205 if(c->w == sw && c->h == sh)
arg@708 206 c->border = 0;
arg@708 207 else
arg@708 208 c->border = BORDERPX;
arg@465 209 /* offscreen appearance fixes */
arg@718 210 if(c->x > sw)
arg@718 211 c->x = sw - c->w - 2 * c->border;
arg@718 212 if(c->y > sh)
arg@718 213 c->y = sh - c->h - 2 * c->border;
arg@708 214 if(c->x + c->w + 2 * c->border < sx)
arg@517 215 c->x = sx;
arg@708 216 if(c->y + c->h + 2 * c->border < sy)
arg@708 217 c->y = sy;
arg@163 218 wc.x = c->x;
arg@163 219 wc.y = c->y;
arg@163 220 wc.width = c->w;
arg@163 221 wc.height = c->h;
arg@708 222 wc.border_width = c->border;
arg@452 223 XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
arg@630 224 configure(c);
garbeam@79 225 XSync(dpy, False);
garbeam@18 226 }
garbeam@18 227
garbeam@76 228 void
arg@639 229 updatesizehints(Client *c) {
arg@123 230 long msize;
garbeam@76 231 XSizeHints size;
arg@123 232
garbeam@76 233 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
garbeam@76 234 size.flags = PSize;
garbeam@76 235 c->flags = size.flags;
garbeam@76 236 if(c->flags & PBaseSize) {
garbeam@76 237 c->basew = size.base_width;
garbeam@76 238 c->baseh = size.base_height;
garbeam@76 239 }
garbeam@76 240 else
garbeam@76 241 c->basew = c->baseh = 0;
garbeam@76 242 if(c->flags & PResizeInc) {
garbeam@76 243 c->incw = size.width_inc;
garbeam@76 244 c->inch = size.height_inc;
garbeam@76 245 }
garbeam@76 246 else
garbeam@76 247 c->incw = c->inch = 0;
garbeam@76 248 if(c->flags & PMaxSize) {
garbeam@76 249 c->maxw = size.max_width;
garbeam@76 250 c->maxh = size.max_height;
garbeam@76 251 }
garbeam@76 252 else
garbeam@76 253 c->maxw = c->maxh = 0;
garbeam@76 254 if(c->flags & PMinSize) {
garbeam@76 255 c->minw = size.min_width;
garbeam@76 256 c->minh = size.min_height;
garbeam@76 257 }
garbeam@76 258 else
garbeam@76 259 c->minw = c->minh = 0;
arg@550 260 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh &&
arg@550 261 c->maxw == c->minw && c->maxh == c->minh);
garbeam@76 262 }
garbeam@76 263
garbeam@76 264 void
arg@461 265 updatetitle(Client *c) {
arg@123 266 char **list = NULL;
arg@377 267 int n;
garbeam@76 268 XTextProperty name;
garbeam@76 269
garbeam@76 270 name.nitems = 0;
garbeam@76 271 c->name[0] = 0;
garbeam@77 272 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
garbeam@76 273 if(!name.nitems)
garbeam@76 274 XGetWMName(dpy, c->win, &name);
garbeam@76 275 if(!name.nitems)
garbeam@76 276 return;
garbeam@76 277 if(name.encoding == XA_STRING)
arg@581 278 strncpy(c->name, (char *)name.value, sizeof c->name);
garbeam@76 279 else {
garbeam@76 280 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
garbeam@76 281 && n > 0 && *list)
garbeam@76 282 {
arg@581 283 strncpy(c->name, *list, sizeof c->name);
garbeam@76 284 XFreeStringList(list);
garbeam@76 285 }
garbeam@76 286 }
garbeam@76 287 XFree(name.value);
garbeam@10 288 }
garbeam@10 289
garbeam@10 290 void
arg@461 291 unmanage(Client *c) {
arg@450 292 Client *nc;
arg@450 293
arg@472 294 /* The server grab construct avoids race conditions. */
garbeam@10 295 XGrabServer(dpy);
garbeam@75 296 XSetErrorHandler(xerrordummy);
arg@400 297 detach(c);
arg@448 298 detachstack(c);
arg@400 299 if(sel == c) {
arg@450 300 for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
arg@450 301 focus(nc);
arg@400 302 }
garbeam@18 303 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
arg@725 304 setclientstate(c, WithdrawnState);
arg@178 305 free(c->tags);
garbeam@10 306 free(c);
garbeam@79 307 XSync(dpy, False);
garbeam@74 308 XSetErrorHandler(xerror);
garbeam@10 309 XUngrabServer(dpy);
arg@533 310 arrange();
garbeam@10 311 }