dwm-meillo

annotate client.c @ 689:cbec08a54a15

implemented new color scheme accordingly to Sanders proposal
author Anselm R. Garbe <arg@suckless.org>
date Sun, 14 Jan 2007 22:37:34 +0100
parents fd2ea58b0b94
children 399f08187c27
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
garbeam@76 56 static int
arg@461 57 xerrordummy(Display *dsply, XErrorEvent *ee) {
garbeam@76 58 return 0;
garbeam@5 59 }
garbeam@5 60
garbeam@76 61 /* extern functions */
garbeam@20 62
garbeam@20 63 void
arg@491 64 configure(Client *c) {
arg@491 65 XEvent synev;
arg@491 66
arg@491 67 synev.type = ConfigureNotify;
arg@491 68 synev.xconfigure.display = dpy;
arg@491 69 synev.xconfigure.event = c->win;
arg@491 70 synev.xconfigure.window = c->win;
arg@491 71 synev.xconfigure.x = c->x;
arg@491 72 synev.xconfigure.y = c->y;
arg@491 73 synev.xconfigure.width = c->w;
arg@491 74 synev.xconfigure.height = c->h;
arg@491 75 synev.xconfigure.border_width = c->border;
arg@491 76 synev.xconfigure.above = None;
arg@491 77 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
arg@491 78 }
arg@491 79
arg@491 80 void
arg@461 81 focus(Client *c) {
arg@400 82 Client *old;
arg@286 83
arg@464 84 if(!issel || (c && !isvisible(c)))
arg@239 85 return;
arg@319 86 if(!sel)
arg@319 87 sel = c;
arg@319 88 else if(sel != c) {
arg@400 89 old = sel;
arg@319 90 sel = c;
arg@400 91 if(old) {
arg@400 92 grabbuttons(old, False);
arg@647 93 drawclient(old);
arg@400 94 }
arg@318 95 }
arg@400 96 if(c) {
arg@446 97 detachstack(c);
arg@446 98 c->snext = stack;
arg@446 99 stack = c;
arg@400 100 grabbuttons(c, True);
arg@647 101 drawclient(c);
arg@400 102 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
arg@400 103 }
arg@400 104 else
arg@400 105 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
garbeam@13 106 }
garbeam@13 107
garbeam@76 108 Client *
arg@461 109 getclient(Window w) {
garbeam@76 110 Client *c;
arg@123 111
garbeam@76 112 for(c = clients; c; c = c->next)
garbeam@76 113 if(c->win == w)
garbeam@76 114 return c;
garbeam@76 115 return NULL;
garbeam@76 116 }
garbeam@76 117
garbeam@76 118 void
arg@461 119 killclient(Arg *arg) {
garbeam@76 120 if(!sel)
garbeam@76 121 return;
arg@157 122 if(sel->proto & PROTODELWIN)
garbeam@77 123 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
garbeam@76 124 else
garbeam@76 125 XKillClient(dpy, sel->win);
garbeam@76 126 }
garbeam@76 127
garbeam@76 128 void
arg@461 129 manage(Window w, XWindowAttributes *wa) {
arg@431 130 Client *c;
arg@123 131 Window trans;
garbeam@5 132
garbeam@5 133 c = emallocz(sizeof(Client));
arg@178 134 c->tags = emallocz(ntags * sizeof(Bool));
garbeam@5 135 c->win = w;
arg@546 136 c->border = 0;
arg@687 137 c->x = wa->x;
arg@687 138 c->y = wa->y;
arg@687 139 c->w = wa->width;
arg@115 140 c->h = wa->height;
arg@639 141 updatesizehints(c);
arg@502 142 if(c->x + c->w + 2 * BORDERPX > sw)
arg@502 143 c->x = sw - c->w - 2 * BORDERPX;
arg@517 144 if(c->x < sx)
arg@517 145 c->x = sx;
arg@502 146 if(c->y + c->h + 2 * BORDERPX > sh)
arg@517 147 c->y = sh - c->h - 2 * BORDERPX;
arg@163 148 if(c->h != sh && c->y < bh)
arg@314 149 c->y = bh;
garbeam@75 150 c->proto = getproto(c->win);
garbeam@26 151 XSelectInput(dpy, c->win,
arg@127 152 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
garbeam@53 153 XGetTransientForHint(dpy, c->win, &trans);
arg@372 154 grabbuttons(c, False);
arg@500 155 updatetitle(c);
arg@431 156 settags(c, getclient(trans));
garbeam@80 157 if(!c->isfloat)
arg@549 158 c->isfloat = trans || c->isfixed;
arg@381 159 if(clients)
arg@381 160 clients->prev = c;
arg@381 161 c->next = clients;
arg@446 162 c->snext = stack;
arg@446 163 stack = clients = c;
arg@687 164 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
arg@270 165 XMapWindow(dpy, c->win);
arg@261 166 if(isvisible(c))
garbeam@51 167 focus(c);
arg@533 168 arrange();
garbeam@94 169 }
garbeam@94 170
garbeam@94 171 void
arg@461 172 resize(Client *c, Bool sizehints, Corner sticky) {
arg@123 173 int bottom = c->y + c->h;
arg@123 174 int right = c->x + c->w;
arg@163 175 XWindowChanges wc;
garbeam@18 176
arg@129 177 if(sizehints) {
garbeam@52 178 if(c->incw)
arg@115 179 c->w -= (c->w - c->basew) % c->incw;
garbeam@52 180 if(c->inch)
arg@115 181 c->h -= (c->h - c->baseh) % c->inch;
arg@129 182 if(c->minw && c->w < c->minw)
arg@129 183 c->w = c->minw;
arg@129 184 if(c->minh && c->h < c->minh)
arg@129 185 c->h = c->minh;
arg@129 186 if(c->maxw && c->w > c->maxw)
arg@129 187 c->w = c->maxw;
arg@129 188 if(c->maxh && c->h > c->maxh)
arg@129 189 c->h = c->maxh;
garbeam@52 190 }
arg@105 191 if(sticky == TopRight || sticky == BotRight)
arg@115 192 c->x = right - c->w;
arg@105 193 if(sticky == BotLeft || sticky == BotRight)
arg@115 194 c->y = bottom - c->h;
arg@465 195 /* offscreen appearance fixes */
arg@517 196 if(c->x + c->w < sx)
arg@517 197 c->x = sx;
arg@465 198 if(c->y + c->h < bh)
arg@465 199 c->y = bh;
arg@465 200 if(c->x > sw)
arg@465 201 c->x = sw - c->w;
arg@465 202 if(c->y > sh)
arg@465 203 c->y = sh - c->h;
arg@163 204 wc.x = c->x;
arg@163 205 wc.y = c->y;
arg@163 206 wc.width = c->w;
arg@163 207 wc.height = c->h;
arg@164 208 if(c->w == sw && c->h == sh)
arg@164 209 wc.border_width = 0;
arg@164 210 else
arg@502 211 wc.border_width = BORDERPX;
arg@452 212 XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
arg@630 213 configure(c);
garbeam@79 214 XSync(dpy, False);
garbeam@18 215 }
garbeam@18 216
garbeam@76 217 void
arg@639 218 updatesizehints(Client *c) {
arg@123 219 long msize;
garbeam@76 220 XSizeHints size;
arg@123 221
garbeam@76 222 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
garbeam@76 223 size.flags = PSize;
garbeam@76 224 c->flags = size.flags;
garbeam@76 225 if(c->flags & PBaseSize) {
garbeam@76 226 c->basew = size.base_width;
garbeam@76 227 c->baseh = size.base_height;
garbeam@76 228 }
garbeam@76 229 else
garbeam@76 230 c->basew = c->baseh = 0;
garbeam@76 231 if(c->flags & PResizeInc) {
garbeam@76 232 c->incw = size.width_inc;
garbeam@76 233 c->inch = size.height_inc;
garbeam@76 234 }
garbeam@76 235 else
garbeam@76 236 c->incw = c->inch = 0;
garbeam@76 237 if(c->flags & PMaxSize) {
garbeam@76 238 c->maxw = size.max_width;
garbeam@76 239 c->maxh = size.max_height;
garbeam@76 240 }
garbeam@76 241 else
garbeam@76 242 c->maxw = c->maxh = 0;
garbeam@76 243 if(c->flags & PMinSize) {
garbeam@76 244 c->minw = size.min_width;
garbeam@76 245 c->minh = size.min_height;
garbeam@76 246 }
garbeam@76 247 else
garbeam@76 248 c->minw = c->minh = 0;
arg@550 249 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh &&
arg@550 250 c->maxw == c->minw && c->maxh == c->minh);
garbeam@76 251 }
garbeam@76 252
garbeam@76 253 void
arg@461 254 updatetitle(Client *c) {
arg@123 255 char **list = NULL;
arg@377 256 int n;
garbeam@76 257 XTextProperty name;
garbeam@76 258
garbeam@76 259 name.nitems = 0;
garbeam@76 260 c->name[0] = 0;
garbeam@77 261 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
garbeam@76 262 if(!name.nitems)
garbeam@76 263 XGetWMName(dpy, c->win, &name);
garbeam@76 264 if(!name.nitems)
garbeam@76 265 return;
garbeam@76 266 if(name.encoding == XA_STRING)
arg@581 267 strncpy(c->name, (char *)name.value, sizeof c->name);
garbeam@76 268 else {
garbeam@76 269 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
garbeam@76 270 && n > 0 && *list)
garbeam@76 271 {
arg@581 272 strncpy(c->name, *list, sizeof c->name);
garbeam@76 273 XFreeStringList(list);
garbeam@76 274 }
garbeam@76 275 }
garbeam@76 276 XFree(name.value);
garbeam@10 277 }
garbeam@10 278
garbeam@10 279 void
arg@461 280 unmanage(Client *c) {
arg@450 281 Client *nc;
arg@450 282
arg@472 283 /* The server grab construct avoids race conditions. */
garbeam@10 284 XGrabServer(dpy);
garbeam@75 285 XSetErrorHandler(xerrordummy);
arg@400 286 detach(c);
arg@448 287 detachstack(c);
arg@400 288 if(sel == c) {
arg@450 289 for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
arg@450 290 focus(nc);
arg@400 291 }
garbeam@18 292 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
arg@178 293 free(c->tags);
garbeam@10 294 free(c);
garbeam@79 295 XSync(dpy, False);
garbeam@74 296 XSetErrorHandler(xerror);
garbeam@10 297 XUngrabServer(dpy);
arg@533 298 arrange();
garbeam@10 299 }