aewl

annotate aewl.c @ 769:49aa8ccceefa

cleanups in domax and more
author meillo@marmaro.de
date Fri, 05 Dec 2008 21:42:47 +0100
parents a1c6805aa018
children 33cec4282120
rev   line source
arg@644 1 /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
meillo@759 2 * (C)opyright MMVIII markus schnalke <meillo at marmaro dot de>
garbeam@0 3 * See LICENSE file for license details.
meillo@754 4 *
meillo@754 5 * dynamic window manager is designed like any other X client as well. It is
meillo@754 6 * driven through handling X events. In contrast to other X clients, a window
meillo@754 7 * manager selects for SubstructureRedirectMask on the root window, to receive
meillo@754 8 * events about window (dis-)appearance. Only one X connection at a time is
meillo@754 9 * allowed to select for this event mask.
meillo@754 10 *
meillo@754 11 * Calls to fetch an X event from the event queue are blocking. Due reading
meillo@754 12 * status text from standard input, a select()-driven main loop has been
meillo@754 13 * implemented which selects for reads on the X connection and STDIN_FILENO to
meillo@754 14 * handle all data smoothly. The event handlers of dwm are organized in an
meillo@754 15 * array which is accessed whenever a new event has been fetched. This allows
meillo@754 16 * event dispatching in O(1) time.
meillo@754 17 *
meillo@754 18 * Each child of the root window is called a client, except windows which have
meillo@754 19 * set the override_redirect flag. Clients are organized in a global
meillo@754 20 * doubly-linked client list, the focus history is remembered through a global
meillo@766 21 * stack list. [...]
meillo@754 22 *
meillo@754 23 * Keys and tagging rules are organized as arrays and defined in the config.h
meillo@754 24 * file. These arrays are kept static in event.o and tag.o respectively,
meillo@754 25 * because no other part of dwm needs access to them. The current mode is
meillo@754 26 * represented by the arrange() function pointer, which wether points to
meillo@761 27 * domax() or dotile().
meillo@754 28 *
meillo@754 29 * To understand everything else, start reading main.c:main().
meillo@766 30 *
meillo@766 31 * -- and now about aewl --
meillo@766 32 *
meillo@766 33 * aewl is a stripped down dwm. It stated as a patchset, but finally forked off
meillo@766 34 * completely. The reason for this was the increasing gap between my wish to
meillo@766 35 * stay where dwm was, and dwm direction to go further. Further more did I
meillo@766 36 * always use only a small subset of dwm's features, so condencing dwm had been
meillo@766 37 * my wish for a long time.
meillo@766 38 *
meillo@766 39 * In aewl clients are either tagged or not (only one tag). Visible are either
meillo@766 40 * all tagged clients or all without the tag.
garbeam@0 41 */
garbeam@0 42
meillo@766 43
garbeam@59 44 #include <errno.h>
arg@619 45 #include <locale.h>
garbeam@0 46 #include <stdio.h>
meillo@754 47 #include <stdarg.h>
garbeam@0 48 #include <stdlib.h>
garbeam@57 49 #include <string.h>
garbeam@59 50 #include <unistd.h>
arg@138 51 #include <sys/select.h>
meillo@754 52 #include <sys/types.h>
meillo@754 53 #include <sys/wait.h>
garbeam@0 54 #include <X11/cursorfont.h>
arg@291 55 #include <X11/keysym.h>
garbeam@0 56 #include <X11/Xatom.h>
meillo@754 57 #include <X11/Xlib.h>
garbeam@0 58 #include <X11/Xproto.h>
meillo@754 59 #include <X11/Xutil.h>
garbeam@0 60
meillo@769 61 #include "config.h"
meillo@769 62
meillo@769 63
meillo@754 64 /* mask shorthands, used in event.c and client.c */
meillo@754 65 #define BUTTONMASK (ButtonPressMask | ButtonReleaseMask)
arg@333 66
meillo@754 67 enum { NetSupported, NetWMName, NetLast }; /* EWMH atoms */
meillo@754 68 enum { WMProtocols, WMDelete, WMState, WMLast }; /* default atoms */
meillo@754 69 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
meillo@764 70 enum { ColFG, ColBG, ColLast }; /* color */
meillo@754 71
meillo@754 72 typedef struct {
meillo@754 73 int ascent;
meillo@754 74 int descent;
meillo@754 75 int height;
meillo@754 76 XFontSet set;
meillo@754 77 XFontStruct *xfont;
meillo@754 78 } Fnt;
meillo@754 79
meillo@754 80 typedef struct {
meillo@754 81 int x, y, w, h;
meillo@754 82 unsigned long norm[ColLast];
meillo@754 83 unsigned long sel[ColLast];
meillo@754 84 Drawable drawable;
meillo@754 85 Fnt font;
meillo@754 86 GC gc;
meillo@754 87 } DC; /* draw context */
meillo@754 88
meillo@754 89 typedef struct Client Client;
meillo@754 90 struct Client {
meillo@754 91 char name[256];
meillo@754 92 int x, y, w, h;
meillo@754 93 int rx, ry, rw, rh; /* revert geometry */
meillo@754 94 int basew, baseh, incw, inch, maxw, maxh, minw, minh;
meillo@754 95 int minax, minay, maxax, maxay;
meillo@754 96 long flags;
meillo@754 97 unsigned int border;
meillo@754 98 Bool isfixed, isfloat, ismax;
meillo@766 99 Bool tag;
meillo@754 100 Client *next;
meillo@754 101 Client *prev;
meillo@754 102 Client *snext;
meillo@754 103 Window win;
meillo@754 104 };
meillo@754 105
meillo@754 106 typedef struct {
meillo@767 107 const char* class;
meillo@767 108 const char* instance;
meillo@767 109 const char* title;
meillo@766 110 int tag;
meillo@754 111 Bool isfloat;
meillo@754 112 } Rule;
meillo@754 113
meillo@754 114
meillo@754 115 typedef struct {
meillo@754 116 unsigned long mod;
meillo@754 117 KeySym keysym;
meillo@758 118 void (*func)(const char* cmd);
meillo@758 119 const char* cmd;
meillo@754 120 } Key;
meillo@754 121
meillo@754 122
meillo@754 123 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
meillo@754 124 #define MOUSEMASK (BUTTONMASK | PointerMotionMask)
meillo@754 125
meillo@754 126
meillo@754 127
meillo@754 128 char stext[256]; /* status text */
meillo@754 129 int bh, bmw; /* bar height, bar mode label width */
meillo@754 130 int screen, sx, sy, sw, sh; /* screen geometry */
meillo@754 131 int wax, way, wah, waw; /* windowarea geometry */
meillo@754 132 unsigned int nmaster; /* number of master clients */
meillo@761 133 unsigned int numlockmask; /* dynamic lock mask */
meillo@754 134 void (*handler[LASTEvent])(XEvent *); /* event handler */
meillo@754 135 void (*arrange)(void); /* arrange function, indicates mode */
arg@333 136 Atom wmatom[WMLast], netatom[NetLast];
meillo@766 137 Bool running, selscreen, seltag;
meillo@754 138 Client *clients, *sel, *stack; /* global client list and stack */
meillo@754 139 Cursor cursor[CurLast];
meillo@754 140 DC dc; /* global draw context */
meillo@754 141 Display *dpy;
meillo@754 142 Window root, barwin;
meillo@754 143
arg@333 144 Bool running = True;
arg@716 145 Bool selscreen = True;
arg@333 146 Client *clients = NULL;
arg@333 147 Client *sel = NULL;
arg@446 148 Client *stack = NULL;
arg@333 149 DC dc = {0};
meillo@754 150
meillo@754 151 static int (*xerrorxlib)(Display *, XErrorEvent *);
meillo@754 152 static Bool otherwm, readin;
meillo@754 153 static unsigned int len = 0;
meillo@754 154
meillo@754 155
meillo@754 156 RULES
meillo@754 157
meillo@754 158
meillo@754 159 /* client.c */
meillo@754 160 void configure(Client *c); /* send synthetic configure event */
meillo@754 161 void focus(Client *c); /* focus c, c may be NULL */
meillo@754 162 Client *getclient(Window w); /* return client of w */
meillo@754 163 Bool isprotodel(Client *c); /* returns True if c->win supports wmatom[WMDelete] */
meillo@754 164 void manage(Window w, XWindowAttributes *wa); /* manage new client */
meillo@754 165 void resize(Client *c, Bool sizehints); /* resize c*/
meillo@754 166 void updatesizehints(Client *c); /* update the size hint variables of c */
meillo@754 167 void updatetitle(Client *c); /* update the name of c */
meillo@754 168 void unmanage(Client *c); /* destroy c */
meillo@754 169
meillo@754 170 /* draw.c */
meillo@754 171 void drawstatus(void); /* draw the bar */
meillo@754 172 unsigned long getcolor(const char *colstr); /* return color of colstr */
meillo@754 173 void setfont(const char *fontstr); /* set the font for DC */
meillo@754 174 unsigned int textw(const char *text); /* return the width of text in px*/
meillo@754 175
meillo@754 176 /* event.c */
meillo@754 177 void grabkeys(void); /* grab all keys defined in config.h */
meillo@754 178 void procevent(void); /* process pending X events */
meillo@754 179
meillo@754 180 /* main.c */
meillo@754 181 void sendevent(Window w, Atom a, long value); /* send synthetic event to w */
meillo@754 182 int xerror(Display *dsply, XErrorEvent *ee); /* dwm's X error handler */
meillo@754 183
meillo@754 184 /* tag.c */
meillo@754 185 Client *getnext(Client *c); /* returns next visible client */
meillo@766 186 void settag(Client *c, Client *trans); /* sets tag of c */
meillo@754 187
meillo@754 188 /* util.c */
meillo@754 189 void *emallocz(unsigned int size); /* allocates zero-initialized memory, exits on error */
meillo@754 190 void eprint(const char *errstr, ...); /* prints errstr and exits with 1 */
meillo@754 191
meillo@754 192 /* view.c */
meillo@754 193 void detach(Client *c); /* detaches c from global client list */
meillo@754 194 void dotile(void); /* arranges all windows tiled */
meillo@754 195 void domax(void); /* arranges all windows fullscreen */
meillo@758 196 Bool isvisible(Client *c); /* returns True if client is visible */
meillo@758 197 void restack(void); /* restores z layers of all clients */
meillo@758 198
meillo@758 199
meillo@766 200 void toggleview(void); /* toggle the view */
meillo@760 201 void focusnext(void); /* focuses next visible client */
meillo@760 202 void zoom(void); /* zooms the focused client to master area */
meillo@760 203 void killclient(void); /* kill c nicely */
meillo@760 204 void quit(void); /* quit dwm nicely */
meillo@761 205 void togglemode(void); /* toggles global arrange function (dotile/domax) */
meillo@760 206 void togglefloat(void); /* toggles focusesd client between floating/non-floating state */
meillo@760 207 void incnmaster(void); /* increments nmaster */
meillo@760 208 void decnmaster(void); /* decrements nmaster */
meillo@766 209 void toggletag(void); /* toggles tag of c */
meillo@758 210 void spawn(const char* cmd); /* forks a new subprocess with cmd */
meillo@754 211
meillo@754 212
meillo@754 213
meillo@754 214
meillo@754 215
meillo@754 216
meillo@754 217
meillo@754 218
meillo@754 219
meillo@754 220
meillo@754 221 /* from view.c */
meillo@754 222 /* static */
meillo@754 223
meillo@754 224 static Client *
meillo@754 225 nexttiled(Client *c) {
meillo@754 226 for(c = getnext(c); c && c->isfloat; c = getnext(c->next));
meillo@754 227 return c;
meillo@754 228 }
meillo@754 229
meillo@754 230 static void
meillo@754 231 togglemax(Client *c) {
meillo@754 232 XEvent ev;
meillo@754 233
meillo@754 234 if(c->isfixed)
meillo@754 235 return;
meillo@754 236
meillo@754 237 if((c->ismax = !c->ismax)) {
meillo@769 238 c->rx = c->x;
meillo@769 239 c->ry = c->y;
meillo@769 240 c->rw = c->w;
meillo@769 241 c->rh = c->h;
meillo@769 242 c->x = wax;
meillo@769 243 c->y = way;
meillo@769 244 c->w = waw - 2 * BORDERPX;
meillo@769 245 c->h = wah - 2 * BORDERPX;
meillo@769 246 } else {
meillo@754 247 c->x = c->rx;
meillo@754 248 c->y = c->ry;
meillo@754 249 c->w = c->rw;
meillo@754 250 c->h = c->rh;
meillo@754 251 }
meillo@769 252 resize(c, False);
meillo@754 253 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
meillo@754 254 }
meillo@754 255
meillo@754 256
meillo@754 257
meillo@754 258 void (*arrange)(void) = DEFMODE;
meillo@754 259
meillo@754 260 void
meillo@754 261 detach(Client *c) {
meillo@754 262 if(c->prev)
meillo@754 263 c->prev->next = c->next;
meillo@754 264 if(c->next)
meillo@754 265 c->next->prev = c->prev;
meillo@754 266 if(c == clients)
meillo@754 267 clients = c->next;
meillo@754 268 c->next = c->prev = NULL;
meillo@754 269 }
meillo@754 270
meillo@754 271 void
meillo@754 272 dotile(void) {
meillo@754 273 unsigned int i, n, mw, mh, tw, th;
meillo@754 274 Client *c;
meillo@754 275
meillo@754 276 for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
meillo@754 277 n++;
meillo@754 278 /* window geoms */
meillo@754 279 mh = (n > nmaster) ? wah / nmaster : wah / (n > 0 ? n : 1);
meillo@754 280 mw = (n > nmaster) ? waw / 2 : waw;
meillo@754 281 th = (n > nmaster) ? wah / (n - nmaster) : 0;
meillo@754 282 tw = waw - mw;
meillo@754 283
meillo@754 284 for(i = 0, c = clients; c; c = c->next)
meillo@754 285 if(isvisible(c)) {
meillo@754 286 if(c->isfloat) {
meillo@754 287 resize(c, True);
meillo@754 288 continue;
meillo@754 289 }
meillo@754 290 c->ismax = False;
meillo@754 291 c->x = wax;
meillo@754 292 c->y = way;
meillo@754 293 if(i < nmaster) {
meillo@754 294 c->y += i * mh;
meillo@754 295 c->w = mw - 2 * BORDERPX;
meillo@754 296 c->h = mh - 2 * BORDERPX;
meillo@754 297 }
meillo@754 298 else { /* tile window */
meillo@754 299 c->x += mw;
meillo@754 300 c->w = tw - 2 * BORDERPX;
meillo@754 301 if(th > 2 * BORDERPX) {
meillo@754 302 c->y += (i - nmaster) * th;
meillo@754 303 c->h = th - 2 * BORDERPX;
meillo@754 304 }
meillo@754 305 else /* fallback if th <= 2 * BORDERPX */
meillo@754 306 c->h = wah - 2 * BORDERPX;
meillo@754 307 }
meillo@754 308 resize(c, False);
meillo@754 309 i++;
meillo@754 310 }
meillo@754 311 else
meillo@754 312 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
meillo@754 313 if(!sel || !isvisible(sel)) {
meillo@754 314 for(c = stack; c && !isvisible(c); c = c->snext);
meillo@754 315 focus(c);
meillo@754 316 }
meillo@754 317 restack();
meillo@754 318 }
meillo@754 319
meillo@754 320 /* begin code by mitch */
meillo@754 321 void
meillo@754 322 domax(void) {
meillo@754 323 Client *c;
meillo@754 324
meillo@754 325 for(c = clients; c; c = c->next) {
meillo@754 326 if(isvisible(c)) {
meillo@754 327 if(c->isfloat) {
meillo@754 328 resize(c, True);
meillo@754 329 continue;
meillo@754 330 }
meillo@768 331 c->ismax = True;
meillo@769 332 c->x = wax;
meillo@769 333 c->y = way;
meillo@769 334 c->w = waw - 2 * BORDERPX;
meillo@769 335 c->h = wah - 2 * BORDERPX;
meillo@769 336 resize(c, False);
meillo@754 337 } else {
meillo@754 338 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
meillo@754 339 }
meillo@754 340 }
meillo@754 341 if(!sel || !isvisible(sel)) {
meillo@754 342 for(c = stack; c && !isvisible(c); c = c->snext);
meillo@754 343 focus(c);
meillo@754 344 }
meillo@754 345 restack();
meillo@754 346 }
meillo@754 347 /* end code by mitch */
meillo@754 348
meillo@754 349 void
meillo@756 350 focusnext() {
meillo@754 351 Client *c;
meillo@754 352
meillo@754 353 if(!sel)
meillo@754 354 return;
meillo@754 355 if(!(c = getnext(sel->next)))
meillo@754 356 c = getnext(clients);
meillo@754 357 if(c) {
meillo@754 358 focus(c);
meillo@754 359 restack();
meillo@754 360 }
meillo@754 361 }
meillo@754 362
meillo@754 363 void
meillo@756 364 incnmaster() {
meillo@761 365 if(wah / (nmaster + 1) <= 2 * BORDERPX)
meillo@754 366 return;
meillo@756 367 nmaster++;
meillo@756 368 if(sel)
meillo@756 369 arrange();
meillo@756 370 else
meillo@756 371 drawstatus();
meillo@756 372 }
meillo@756 373
meillo@756 374 void
meillo@756 375 decnmaster() {
meillo@761 376 if(nmaster <= 1)
meillo@756 377 return;
meillo@756 378 nmaster--;
meillo@754 379 if(sel)
meillo@754 380 arrange();
meillo@754 381 else
meillo@754 382 drawstatus();
meillo@754 383 }
meillo@754 384
meillo@754 385 Bool
meillo@754 386 isvisible(Client *c) {
meillo@766 387 return (c->tag == seltag);
meillo@754 388 }
meillo@754 389
meillo@754 390 void
meillo@754 391 restack(void) {
meillo@754 392 Client *c;
meillo@754 393 XEvent ev;
meillo@754 394
meillo@754 395 drawstatus();
meillo@754 396 if(!sel)
meillo@754 397 return;
meillo@761 398 if(sel->isfloat)
meillo@754 399 XRaiseWindow(dpy, sel->win);
meillo@754 400
meillo@768 401 if(!sel->isfloat)
meillo@768 402 XLowerWindow(dpy, sel->win);
meillo@768 403 for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
meillo@768 404 if(c == sel)
meillo@768 405 continue;
meillo@768 406 XLowerWindow(dpy, c->win);
meillo@768 407 }
meillo@754 408
meillo@754 409 XSync(dpy, False);
meillo@754 410 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
meillo@754 411 }
meillo@754 412
meillo@754 413 void
meillo@756 414 togglefloat() {
meillo@761 415 if (!sel)
meillo@754 416 return;
meillo@754 417 sel->isfloat = !sel->isfloat;
meillo@754 418 arrange();
meillo@754 419 }
meillo@754 420
meillo@754 421 void
meillo@756 422 togglemode() {
meillo@754 423 /* only toggle between tile and max - float is just available through togglefloat */
meillo@754 424 arrange = (arrange == dotile) ? domax : dotile;
meillo@754 425 if(sel)
meillo@754 426 arrange();
meillo@754 427 else
meillo@754 428 drawstatus();
meillo@754 429 }
meillo@754 430
meillo@754 431 void
meillo@755 432 toggleview() {
meillo@766 433 seltag = !seltag;
meillo@754 434 arrange();
meillo@754 435 }
meillo@754 436
meillo@754 437 void
meillo@756 438 zoom() {
meillo@754 439 unsigned int n;
meillo@754 440 Client *c;
meillo@754 441
meillo@754 442 if(!sel)
meillo@754 443 return;
meillo@761 444 if(sel->isfloat) {
meillo@754 445 togglemax(sel);
meillo@754 446 return;
meillo@754 447 }
meillo@754 448 for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
meillo@754 449 n++;
meillo@754 450
meillo@754 451 if((c = sel) == nexttiled(clients))
meillo@754 452 if(!(c = nexttiled(c->next)))
meillo@754 453 return;
meillo@754 454 detach(c);
meillo@754 455 if(clients)
meillo@754 456 clients->prev = c;
meillo@754 457 c->next = clients;
meillo@754 458 clients = c;
meillo@754 459 focus(c);
meillo@754 460 arrange();
meillo@754 461 }
meillo@754 462
meillo@754 463
meillo@754 464
meillo@754 465
meillo@754 466
meillo@754 467
meillo@754 468
meillo@754 469
meillo@754 470
meillo@754 471
meillo@754 472
meillo@754 473
meillo@754 474
meillo@754 475
meillo@754 476
meillo@754 477
meillo@754 478 /* from util.c */
meillo@754 479
meillo@754 480
meillo@754 481 void *
meillo@754 482 emallocz(unsigned int size) {
meillo@754 483 void *res = calloc(1, size);
meillo@754 484
meillo@754 485 if(!res)
meillo@754 486 eprint("fatal: could not malloc() %u bytes\n", size);
meillo@754 487 return res;
meillo@754 488 }
meillo@754 489
meillo@754 490 void
meillo@754 491 eprint(const char *errstr, ...) {
meillo@754 492 va_list ap;
meillo@754 493
meillo@754 494 va_start(ap, errstr);
meillo@754 495 vfprintf(stderr, errstr, ap);
meillo@754 496 va_end(ap);
meillo@754 497 exit(EXIT_FAILURE);
meillo@754 498 }
meillo@754 499
meillo@754 500 void
meillo@758 501 spawn(const char* cmd) {
meillo@754 502 static char *shell = NULL;
meillo@754 503
meillo@758 504 if(!cmd)
meillo@754 505 return;
meillo@769 506 if(!(shell = getenv("SHELL")))
meillo@769 507 shell = "/bin/sh";
meillo@754 508 /* The double-fork construct avoids zombie processes and keeps the code
meillo@754 509 * clean from stupid signal handlers. */
meillo@754 510 if(fork() == 0) {
meillo@754 511 if(fork() == 0) {
meillo@754 512 if(dpy)
meillo@754 513 close(ConnectionNumber(dpy));
meillo@754 514 setsid();
meillo@758 515 execl(shell, shell, "-c", cmd, (char *)NULL);
meillo@758 516 fprintf(stderr, "dwm: execl '%s -c %s'", shell, cmd);
meillo@754 517 perror(" failed");
meillo@754 518 }
meillo@754 519 exit(0);
meillo@754 520 }
meillo@754 521 wait(0);
meillo@754 522 }
meillo@754 523
meillo@754 524
meillo@754 525
meillo@754 526
meillo@754 527
meillo@754 528
meillo@754 529
meillo@754 530
meillo@754 531
meillo@754 532
meillo@754 533
meillo@754 534
meillo@754 535
meillo@754 536 /* from tag.c */
arg@333 537
garbeam@84 538 /* static */
garbeam@0 539
meillo@754 540 Client *
meillo@754 541 getnext(Client *c) {
meillo@768 542 while(c && !isvisible(c)) {
meillo@768 543 c = c->next;
meillo@768 544 }
meillo@754 545 return c;
meillo@754 546 }
meillo@754 547
meillo@754 548 void
meillo@767 549 settag(Client *c, Client *trans) {
meillo@754 550 unsigned int i;
meillo@767 551 XClassHint ch = { 0 };
meillo@754 552
meillo@767 553 if(trans) {
meillo@767 554 c->tag = trans->tag;
meillo@754 555 return;
meillo@767 556 }
meillo@767 557 c->tag = seltag; /* default */
meillo@767 558 XGetClassHint(dpy, c->win, &ch);
meillo@754 559 len = sizeof rule / sizeof rule[0];
meillo@754 560 for(i = 0; i < len; i++) {
meillo@767 561 if((rule[i].title && strstr(c->name, rule[i].title))
meillo@767 562 || (ch.res_class && rule[i].class && strstr(ch.res_class, rule[i].class))
meillo@767 563 || (ch.res_name && rule[i].instance && strstr(ch.res_name, rule[i].instance))) {
meillo@767 564 c->isfloat = rule[i].isfloat;
meillo@767 565 if (rule[i].tag < 0) {
meillo@767 566 c->tag = seltag;
meillo@767 567 } else if (rule[i].tag) {
meillo@767 568 c->tag = True;
meillo@767 569 } else {
meillo@767 570 c->tag = False;
meillo@767 571 }
meillo@767 572 break;
meillo@754 573 }
meillo@754 574 }
meillo@767 575 if(ch.res_class)
meillo@767 576 XFree(ch.res_class);
meillo@767 577 if(ch.res_name)
meillo@767 578 XFree(ch.res_name);
meillo@754 579 }
meillo@754 580
meillo@754 581 void
meillo@766 582 toggletag() {
meillo@754 583 if(!sel)
meillo@754 584 return;
meillo@766 585 sel->tag = !sel->tag;
meillo@755 586 toggleview();
meillo@754 587 }
meillo@754 588
meillo@754 589
meillo@754 590
meillo@754 591
meillo@754 592
meillo@754 593
meillo@754 594
meillo@754 595
meillo@754 596
meillo@754 597
meillo@754 598
meillo@754 599
meillo@754 600
meillo@754 601
meillo@754 602
meillo@754 603
meillo@754 604
meillo@754 605 /* from event.c */
meillo@754 606 /* static */
meillo@754 607
meillo@754 608 KEYS
meillo@754 609
meillo@754 610
meillo@754 611
meillo@754 612 static void
meillo@754 613 movemouse(Client *c) {
meillo@754 614 int x1, y1, ocx, ocy, di;
meillo@754 615 unsigned int dui;
meillo@754 616 Window dummy;
meillo@754 617 XEvent ev;
meillo@754 618
meillo@754 619 ocx = c->x;
meillo@754 620 ocy = c->y;
meillo@754 621 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
meillo@754 622 None, cursor[CurMove], CurrentTime) != GrabSuccess)
meillo@754 623 return;
meillo@754 624 c->ismax = False;
meillo@754 625 XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
meillo@754 626 for(;;) {
meillo@754 627 XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask, &ev);
meillo@754 628 switch (ev.type) {
meillo@754 629 case ButtonRelease:
meillo@754 630 resize(c, True);
meillo@754 631 XUngrabPointer(dpy, CurrentTime);
meillo@754 632 return;
meillo@754 633 case ConfigureRequest:
meillo@754 634 case Expose:
meillo@754 635 case MapRequest:
meillo@754 636 handler[ev.type](&ev);
meillo@754 637 break;
meillo@754 638 case MotionNotify:
meillo@754 639 XSync(dpy, False);
meillo@754 640 c->x = ocx + (ev.xmotion.x - x1);
meillo@754 641 c->y = ocy + (ev.xmotion.y - y1);
meillo@754 642 if(abs(wax + c->x) < SNAP)
meillo@754 643 c->x = wax;
meillo@754 644 else if(abs((wax + waw) - (c->x + c->w + 2 * c->border)) < SNAP)
meillo@754 645 c->x = wax + waw - c->w - 2 * c->border;
meillo@754 646 if(abs(way - c->y) < SNAP)
meillo@754 647 c->y = way;
meillo@754 648 else if(abs((way + wah) - (c->y + c->h + 2 * c->border)) < SNAP)
meillo@754 649 c->y = way + wah - c->h - 2 * c->border;
meillo@754 650 resize(c, False);
meillo@754 651 break;
meillo@754 652 }
meillo@754 653 }
meillo@754 654 }
meillo@754 655
meillo@754 656 static void
meillo@754 657 resizemouse(Client *c) {
meillo@754 658 int ocx, ocy;
meillo@754 659 int nw, nh;
meillo@754 660 XEvent ev;
meillo@754 661
meillo@754 662 ocx = c->x;
meillo@754 663 ocy = c->y;
meillo@754 664 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
meillo@754 665 None, cursor[CurResize], CurrentTime) != GrabSuccess)
meillo@754 666 return;
meillo@754 667 c->ismax = False;
meillo@754 668 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->border - 1, c->h + c->border - 1);
meillo@754 669 for(;;) {
meillo@754 670 XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask , &ev);
meillo@754 671 switch(ev.type) {
meillo@754 672 case ButtonRelease:
meillo@754 673 resize(c, True);
meillo@754 674 XUngrabPointer(dpy, CurrentTime);
meillo@754 675 return;
meillo@754 676 case ConfigureRequest:
meillo@754 677 case Expose:
meillo@754 678 case MapRequest:
meillo@754 679 handler[ev.type](&ev);
meillo@754 680 break;
meillo@754 681 case MotionNotify:
meillo@754 682 XSync(dpy, False);
meillo@754 683 nw = ev.xmotion.x - ocx - 2 * c->border + 1;
meillo@754 684 c->w = nw > 0 ? nw : 1;
meillo@754 685 nh = ev.xmotion.y - ocy - 2 * c->border + 1;
meillo@754 686 c->h = nh > 0 ? nh : 1;
meillo@754 687 resize(c, True);
meillo@754 688 break;
meillo@754 689 }
meillo@754 690 }
meillo@754 691 }
meillo@754 692
meillo@754 693 static void
meillo@754 694 buttonpress(XEvent *e) {
meillo@754 695 Client *c;
meillo@754 696 XButtonPressedEvent *ev = &e->xbutton;
meillo@754 697
meillo@754 698 if(barwin == ev->window) {
meillo@758 699 return;
meillo@763 700 }
meillo@763 701 if((c = getclient(ev->window))) {
meillo@754 702 focus(c);
meillo@754 703 if(CLEANMASK(ev->state) != MODKEY)
meillo@754 704 return;
meillo@761 705 if(ev->button == Button1 && c->isfloat) {
meillo@754 706 restack();
meillo@754 707 movemouse(c);
meillo@761 708 } else if(ev->button == Button3 && c->isfloat && !c->isfixed) {
meillo@754 709 restack();
meillo@754 710 resizemouse(c);
meillo@754 711 }
meillo@754 712 }
meillo@754 713 }
meillo@754 714
meillo@754 715 static void
meillo@754 716 configurerequest(XEvent *e) {
meillo@754 717 unsigned long newmask;
meillo@754 718 Client *c;
meillo@754 719 XConfigureRequestEvent *ev = &e->xconfigurerequest;
meillo@754 720 XWindowChanges wc;
meillo@754 721
meillo@754 722 if((c = getclient(ev->window))) {
meillo@754 723 c->ismax = False;
meillo@754 724 if(ev->value_mask & CWX)
meillo@754 725 c->x = ev->x;
meillo@754 726 if(ev->value_mask & CWY)
meillo@754 727 c->y = ev->y;
meillo@754 728 if(ev->value_mask & CWWidth)
meillo@754 729 c->w = ev->width;
meillo@754 730 if(ev->value_mask & CWHeight)
meillo@754 731 c->h = ev->height;
meillo@754 732 if(ev->value_mask & CWBorderWidth)
meillo@754 733 c->border = ev->border_width;
meillo@754 734 wc.x = c->x;
meillo@754 735 wc.y = c->y;
meillo@754 736 wc.width = c->w;
meillo@754 737 wc.height = c->h;
meillo@754 738 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
meillo@754 739 if(newmask)
meillo@754 740 XConfigureWindow(dpy, c->win, newmask, &wc);
meillo@754 741 else
meillo@754 742 configure(c);
meillo@754 743 XSync(dpy, False);
meillo@754 744 if(c->isfloat) {
meillo@754 745 resize(c, False);
meillo@754 746 if(!isvisible(c))
meillo@754 747 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
meillo@754 748 }
meillo@754 749 else
meillo@754 750 arrange();
meillo@758 751 } else {
meillo@754 752 wc.x = ev->x;
meillo@754 753 wc.y = ev->y;
meillo@754 754 wc.width = ev->width;
meillo@754 755 wc.height = ev->height;
meillo@754 756 wc.border_width = ev->border_width;
meillo@754 757 wc.sibling = ev->above;
meillo@754 758 wc.stack_mode = ev->detail;
meillo@754 759 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
meillo@754 760 XSync(dpy, False);
meillo@754 761 }
meillo@754 762 }
meillo@754 763
meillo@754 764 static void
meillo@754 765 destroynotify(XEvent *e) {
meillo@754 766 Client *c;
meillo@754 767 XDestroyWindowEvent *ev = &e->xdestroywindow;
meillo@754 768
meillo@754 769 if((c = getclient(ev->window)))
meillo@754 770 unmanage(c);
meillo@754 771 }
meillo@754 772
meillo@754 773 static void
meillo@754 774 enternotify(XEvent *e) {
meillo@754 775 Client *c;
meillo@754 776 XCrossingEvent *ev = &e->xcrossing;
meillo@754 777
meillo@754 778 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
meillo@754 779 return;
meillo@754 780 if((c = getclient(ev->window)) && isvisible(c))
meillo@754 781 focus(c);
meillo@754 782 else if(ev->window == root) {
meillo@754 783 selscreen = True;
meillo@754 784 for(c = stack; c && !isvisible(c); c = c->snext);
meillo@754 785 focus(c);
meillo@754 786 }
meillo@754 787 }
meillo@754 788
meillo@754 789 static void
meillo@754 790 expose(XEvent *e) {
meillo@754 791 XExposeEvent *ev = &e->xexpose;
meillo@754 792
meillo@754 793 if(ev->count == 0) {
meillo@754 794 if(barwin == ev->window)
meillo@754 795 drawstatus();
meillo@754 796 }
meillo@754 797 }
meillo@754 798
meillo@754 799 static void
meillo@754 800 keypress(XEvent *e) {
meillo@754 801 static unsigned int len = sizeof key / sizeof key[0];
meillo@754 802 unsigned int i;
meillo@754 803 KeySym keysym;
meillo@754 804 XKeyEvent *ev = &e->xkey;
meillo@754 805
meillo@754 806 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
meillo@754 807 for(i = 0; i < len; i++) {
meillo@756 808 if(keysym == key[i].keysym && CLEANMASK(key[i].mod) == CLEANMASK(ev->state)) {
meillo@754 809 if(key[i].func)
meillo@758 810 key[i].func(key[i].cmd);
meillo@754 811 }
meillo@754 812 }
meillo@754 813 }
meillo@754 814
meillo@754 815 static void
meillo@754 816 leavenotify(XEvent *e) {
meillo@754 817 XCrossingEvent *ev = &e->xcrossing;
meillo@754 818
meillo@754 819 if((ev->window == root) && !ev->same_screen) {
meillo@754 820 selscreen = False;
meillo@754 821 focus(NULL);
meillo@754 822 }
meillo@754 823 }
meillo@754 824
meillo@754 825 static void
meillo@754 826 mappingnotify(XEvent *e) {
meillo@754 827 XMappingEvent *ev = &e->xmapping;
meillo@754 828
meillo@754 829 XRefreshKeyboardMapping(ev);
meillo@754 830 if(ev->request == MappingKeyboard)
meillo@754 831 grabkeys();
meillo@754 832 }
meillo@754 833
meillo@754 834 static void
meillo@754 835 maprequest(XEvent *e) {
meillo@754 836 static XWindowAttributes wa;
meillo@754 837 XMapRequestEvent *ev = &e->xmaprequest;
meillo@754 838
meillo@754 839 if(!XGetWindowAttributes(dpy, ev->window, &wa))
meillo@754 840 return;
meillo@754 841 if(wa.override_redirect) {
meillo@754 842 XSelectInput(dpy, ev->window,
meillo@754 843 (StructureNotifyMask | PropertyChangeMask));
meillo@754 844 return;
meillo@754 845 }
meillo@754 846 if(!getclient(ev->window))
meillo@754 847 manage(ev->window, &wa);
meillo@754 848 }
meillo@754 849
meillo@754 850 static void
meillo@754 851 propertynotify(XEvent *e) {
meillo@754 852 Client *c;
meillo@754 853 Window trans;
meillo@754 854 XPropertyEvent *ev = &e->xproperty;
meillo@754 855
meillo@754 856 if(ev->state == PropertyDelete)
meillo@754 857 return; /* ignore */
meillo@754 858 if((c = getclient(ev->window))) {
meillo@754 859 switch (ev->atom) {
meillo@754 860 default: break;
meillo@754 861 case XA_WM_TRANSIENT_FOR:
meillo@754 862 XGetTransientForHint(dpy, c->win, &trans);
meillo@754 863 if(!c->isfloat && (c->isfloat = (trans != 0)))
meillo@754 864 arrange();
meillo@754 865 break;
meillo@754 866 case XA_WM_NORMAL_HINTS:
meillo@754 867 updatesizehints(c);
meillo@754 868 break;
meillo@754 869 }
meillo@754 870 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
meillo@754 871 updatetitle(c);
meillo@754 872 if(c == sel)
meillo@754 873 drawstatus();
meillo@754 874 }
meillo@754 875 }
meillo@754 876 }
meillo@754 877
meillo@754 878 static void
meillo@754 879 unmapnotify(XEvent *e) {
meillo@754 880 Client *c;
meillo@754 881 XUnmapEvent *ev = &e->xunmap;
meillo@754 882
meillo@754 883 if((c = getclient(ev->window)))
meillo@754 884 unmanage(c);
meillo@754 885 }
meillo@754 886
meillo@754 887
meillo@754 888
meillo@754 889 void (*handler[LASTEvent]) (XEvent *) = {
meillo@754 890 [ButtonPress] = buttonpress,
meillo@754 891 [ConfigureRequest] = configurerequest,
meillo@754 892 [DestroyNotify] = destroynotify,
meillo@754 893 [EnterNotify] = enternotify,
meillo@754 894 [LeaveNotify] = leavenotify,
meillo@754 895 [Expose] = expose,
meillo@754 896 [KeyPress] = keypress,
meillo@754 897 [MappingNotify] = mappingnotify,
meillo@754 898 [MapRequest] = maprequest,
meillo@754 899 [PropertyNotify] = propertynotify,
meillo@754 900 [UnmapNotify] = unmapnotify
meillo@754 901 };
meillo@754 902
meillo@754 903 void
meillo@754 904 grabkeys(void) {
meillo@754 905 static unsigned int len = sizeof key / sizeof key[0];
meillo@754 906 unsigned int i;
meillo@754 907 KeyCode code;
meillo@754 908
meillo@754 909 XUngrabKey(dpy, AnyKey, AnyModifier, root);
meillo@754 910 for(i = 0; i < len; i++) {
meillo@754 911 code = XKeysymToKeycode(dpy, key[i].keysym);
meillo@754 912 XGrabKey(dpy, code, key[i].mod, root, True,
meillo@754 913 GrabModeAsync, GrabModeAsync);
meillo@754 914 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
meillo@754 915 GrabModeAsync, GrabModeAsync);
meillo@754 916 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
meillo@754 917 GrabModeAsync, GrabModeAsync);
meillo@754 918 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
meillo@754 919 GrabModeAsync, GrabModeAsync);
meillo@754 920 }
meillo@754 921 }
meillo@754 922
meillo@754 923 void
meillo@754 924 procevent(void) {
meillo@754 925 XEvent ev;
meillo@754 926
meillo@754 927 while(XPending(dpy)) {
meillo@754 928 XNextEvent(dpy, &ev);
meillo@754 929 if(handler[ev.type])
meillo@754 930 (handler[ev.type])(&ev); /* call handler */
meillo@754 931 }
meillo@754 932 }
meillo@754 933
meillo@754 934
meillo@754 935
meillo@754 936
meillo@754 937
meillo@754 938
meillo@754 939
meillo@754 940
meillo@754 941
meillo@754 942
meillo@754 943
meillo@754 944
meillo@754 945
meillo@754 946
meillo@754 947
meillo@754 948 /* from draw.c */
meillo@754 949 /* static */
meillo@754 950
meillo@754 951 static unsigned int
meillo@754 952 textnw(const char *text, unsigned int len) {
meillo@754 953 XRectangle r;
meillo@754 954
meillo@754 955 if(dc.font.set) {
meillo@754 956 XmbTextExtents(dc.font.set, text, len, NULL, &r);
meillo@754 957 return r.width;
meillo@754 958 }
meillo@754 959 return XTextWidth(dc.font.xfont, text, len);
meillo@754 960 }
meillo@754 961
meillo@754 962 static void
meillo@754 963 drawtext(const char *text, unsigned long col[ColLast]) {
meillo@754 964 int x, y, w, h;
meillo@754 965 static char buf[256];
meillo@754 966 unsigned int len, olen;
meillo@754 967 XGCValues gcv;
meillo@754 968 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
meillo@754 969
meillo@754 970 XSetForeground(dpy, dc.gc, col[ColBG]);
meillo@754 971 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
meillo@754 972 if(!text)
meillo@754 973 return;
meillo@754 974 w = 0;
meillo@754 975 olen = len = strlen(text);
meillo@754 976 if(len >= sizeof buf)
meillo@754 977 len = sizeof buf - 1;
meillo@754 978 memcpy(buf, text, len);
meillo@754 979 buf[len] = 0;
meillo@754 980 h = dc.font.ascent + dc.font.descent;
meillo@754 981 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
meillo@754 982 x = dc.x + (h / 2);
meillo@754 983 /* shorten text if necessary */
meillo@754 984 while(len && (w = textnw(buf, len)) > dc.w - h)
meillo@754 985 buf[--len] = 0;
meillo@754 986 if(len < olen) {
meillo@754 987 if(len > 1)
meillo@754 988 buf[len - 1] = '.';
meillo@754 989 if(len > 2)
meillo@754 990 buf[len - 2] = '.';
meillo@754 991 if(len > 3)
meillo@754 992 buf[len - 3] = '.';
meillo@754 993 }
meillo@754 994 if(w > dc.w)
meillo@754 995 return; /* too long */
meillo@754 996 gcv.foreground = col[ColFG];
meillo@754 997 if(dc.font.set) {
meillo@754 998 XChangeGC(dpy, dc.gc, GCForeground, &gcv);
meillo@754 999 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
meillo@754 1000 } else {
meillo@754 1001 gcv.font = dc.font.xfont->fid;
meillo@754 1002 XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
meillo@754 1003 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
meillo@754 1004 }
meillo@754 1005 }
meillo@754 1006
meillo@754 1007
meillo@754 1008
meillo@754 1009 void
meillo@754 1010 drawstatus(void) {
meillo@756 1011 int x;
meillo@754 1012
meillo@754 1013 dc.x = dc.y = 0;
meillo@766 1014 dc.w = textw(NAMETAGGED);
meillo@766 1015 drawtext(NAMETAGGED, ( seltag ? dc.sel : dc.norm ));
meillo@761 1016 dc.x += dc.w + 1;
meillo@766 1017 dc.w = textw(NAMEUNTAGGED);
meillo@766 1018 drawtext(NAMEUNTAGGED, ( seltag ? dc.norm : dc.sel ));
meillo@761 1019 dc.x += dc.w + 1;
meillo@754 1020 dc.w = bmw;
meillo@754 1021 drawtext("", dc.norm);
meillo@754 1022 x = dc.x + dc.w;
meillo@754 1023 dc.w = textw(stext);
meillo@754 1024 dc.x = sw - dc.w;
meillo@754 1025 if(dc.x < x) {
meillo@754 1026 dc.x = x;
meillo@754 1027 dc.w = sw - x;
meillo@754 1028 }
meillo@754 1029 drawtext(stext, dc.norm);
meillo@754 1030 if((dc.w = dc.x - x) > bh) {
meillo@754 1031 dc.x = x;
meillo@754 1032 drawtext(sel ? sel->name : NULL, dc.norm);
meillo@754 1033 }
meillo@754 1034 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, sw, bh, 0, 0);
meillo@754 1035 XSync(dpy, False);
meillo@754 1036 }
meillo@754 1037
meillo@754 1038 unsigned long
meillo@754 1039 getcolor(const char *colstr) {
meillo@754 1040 Colormap cmap = DefaultColormap(dpy, screen);
meillo@754 1041 XColor color;
meillo@754 1042
meillo@754 1043 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
meillo@754 1044 eprint("error, cannot allocate color '%s'\n", colstr);
meillo@754 1045 return color.pixel;
meillo@754 1046 }
meillo@754 1047
meillo@754 1048 void
meillo@754 1049 setfont(const char *fontstr) {
meillo@754 1050 char *def, **missing;
meillo@754 1051 int i, n;
meillo@754 1052
meillo@754 1053 missing = NULL;
meillo@754 1054 if(dc.font.set)
meillo@754 1055 XFreeFontSet(dpy, dc.font.set);
meillo@754 1056 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
meillo@754 1057 if(missing) {
meillo@754 1058 while(n--)
meillo@754 1059 fprintf(stderr, "missing fontset: %s\n", missing[n]);
meillo@754 1060 XFreeStringList(missing);
meillo@754 1061 }
meillo@754 1062 if(dc.font.set) {
meillo@754 1063 XFontSetExtents *font_extents;
meillo@754 1064 XFontStruct **xfonts;
meillo@754 1065 char **font_names;
meillo@754 1066 dc.font.ascent = dc.font.descent = 0;
meillo@754 1067 font_extents = XExtentsOfFontSet(dc.font.set);
meillo@754 1068 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
meillo@754 1069 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
meillo@754 1070 if(dc.font.ascent < (*xfonts)->ascent)
meillo@754 1071 dc.font.ascent = (*xfonts)->ascent;
meillo@754 1072 if(dc.font.descent < (*xfonts)->descent)
meillo@754 1073 dc.font.descent = (*xfonts)->descent;
meillo@754 1074 xfonts++;
meillo@754 1075 }
meillo@754 1076 } else {
meillo@754 1077 if(dc.font.xfont)
meillo@754 1078 XFreeFont(dpy, dc.font.xfont);
meillo@754 1079 dc.font.xfont = NULL;
meillo@754 1080 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr)))
meillo@754 1081 eprint("error, cannot load font: '%s'\n", fontstr);
meillo@754 1082 dc.font.ascent = dc.font.xfont->ascent;
meillo@754 1083 dc.font.descent = dc.font.xfont->descent;
meillo@754 1084 }
meillo@754 1085 dc.font.height = dc.font.ascent + dc.font.descent;
meillo@754 1086 }
meillo@754 1087
meillo@754 1088 unsigned int
meillo@754 1089 textw(const char *text) {
meillo@754 1090 return textnw(text, strlen(text)) + dc.font.height;
meillo@754 1091 }
meillo@754 1092
meillo@754 1093
meillo@754 1094
meillo@754 1095
meillo@754 1096
meillo@754 1097
meillo@754 1098
meillo@754 1099
meillo@754 1100
meillo@754 1101
meillo@754 1102
meillo@754 1103 /* from client.c */
meillo@754 1104 /* static */
meillo@754 1105
meillo@754 1106 static void
meillo@754 1107 detachstack(Client *c) {
meillo@754 1108 Client **tc;
meillo@754 1109 for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
meillo@754 1110 *tc = c->snext;
meillo@754 1111 }
meillo@754 1112
meillo@754 1113 static void
meillo@754 1114 grabbuttons(Client *c, Bool focused) {
meillo@754 1115 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
meillo@754 1116
meillo@754 1117 if(focused) {
meillo@754 1118 XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
meillo@754 1119 GrabModeAsync, GrabModeSync, None, None);
meillo@754 1120 XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
meillo@754 1121 GrabModeAsync, GrabModeSync, None, None);
meillo@754 1122 XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
meillo@754 1123 GrabModeAsync, GrabModeSync, None, None);
meillo@754 1124 XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
meillo@754 1125 GrabModeAsync, GrabModeSync, None, None);
meillo@754 1126
meillo@754 1127 XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
meillo@754 1128 GrabModeAsync, GrabModeSync, None, None);
meillo@754 1129 XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
meillo@754 1130 GrabModeAsync, GrabModeSync, None, None);
meillo@754 1131 XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
meillo@754 1132 GrabModeAsync, GrabModeSync, None, None);
meillo@754 1133 XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
meillo@754 1134 GrabModeAsync, GrabModeSync, None, None);
meillo@754 1135
meillo@754 1136 XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
meillo@754 1137 GrabModeAsync, GrabModeSync, None, None);
meillo@754 1138 XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
meillo@754 1139 GrabModeAsync, GrabModeSync, None, None);
meillo@754 1140 XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
meillo@754 1141 GrabModeAsync, GrabModeSync, None, None);
meillo@754 1142 XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
meillo@754 1143 GrabModeAsync, GrabModeSync, None, None);
meillo@754 1144 } else {
meillo@754 1145 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
meillo@754 1146 GrabModeAsync, GrabModeSync, None, None);
meillo@754 1147 }
meillo@754 1148 }
meillo@754 1149
meillo@754 1150 static void
meillo@754 1151 setclientstate(Client *c, long state) {
meillo@754 1152 long data[] = {state, None};
meillo@754 1153 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
meillo@754 1154 PropModeReplace, (unsigned char *)data, 2);
meillo@754 1155 }
meillo@754 1156
meillo@754 1157 static int
meillo@754 1158 xerrordummy(Display *dsply, XErrorEvent *ee) {
meillo@754 1159 return 0;
meillo@754 1160 }
meillo@754 1161
meillo@754 1162
meillo@754 1163
meillo@754 1164 void
meillo@754 1165 configure(Client *c) {
meillo@754 1166 XEvent synev;
meillo@754 1167
meillo@754 1168 synev.type = ConfigureNotify;
meillo@754 1169 synev.xconfigure.display = dpy;
meillo@754 1170 synev.xconfigure.event = c->win;
meillo@754 1171 synev.xconfigure.window = c->win;
meillo@754 1172 synev.xconfigure.x = c->x;
meillo@754 1173 synev.xconfigure.y = c->y;
meillo@754 1174 synev.xconfigure.width = c->w;
meillo@754 1175 synev.xconfigure.height = c->h;
meillo@754 1176 synev.xconfigure.border_width = c->border;
meillo@754 1177 synev.xconfigure.above = None;
meillo@754 1178 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
meillo@754 1179 }
meillo@754 1180
meillo@754 1181 void
meillo@754 1182 focus(Client *c) {
meillo@754 1183 if(c && !isvisible(c))
meillo@754 1184 return;
meillo@754 1185 if(sel && sel != c) {
meillo@754 1186 grabbuttons(sel, False);
meillo@764 1187 XSetWindowBorder(dpy, sel->win, dc.norm[ColBG]);
meillo@754 1188 }
meillo@754 1189 if(c) {
meillo@754 1190 detachstack(c);
meillo@754 1191 c->snext = stack;
meillo@754 1192 stack = c;
meillo@754 1193 grabbuttons(c, True);
meillo@754 1194 }
meillo@754 1195 sel = c;
meillo@754 1196 drawstatus();
meillo@754 1197 if(!selscreen)
meillo@754 1198 return;
meillo@754 1199 if(c) {
meillo@764 1200 XSetWindowBorder(dpy, c->win, dc.sel[ColBG]);
meillo@754 1201 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
meillo@754 1202 } else {
meillo@754 1203 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
meillo@754 1204 }
meillo@754 1205 }
meillo@754 1206
meillo@754 1207 Client *
meillo@754 1208 getclient(Window w) {
meillo@754 1209 Client *c;
meillo@754 1210
meillo@754 1211 for(c = clients; c; c = c->next) {
meillo@754 1212 if(c->win == w) {
meillo@754 1213 return c;
meillo@754 1214 }
meillo@754 1215 }
meillo@754 1216 return NULL;
meillo@754 1217 }
meillo@754 1218
meillo@754 1219 Bool
meillo@754 1220 isprotodel(Client *c) {
meillo@754 1221 int i, n;
meillo@754 1222 Atom *protocols;
meillo@754 1223 Bool ret = False;
meillo@754 1224
meillo@754 1225 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
meillo@754 1226 for(i = 0; !ret && i < n; i++)
meillo@754 1227 if(protocols[i] == wmatom[WMDelete])
meillo@754 1228 ret = True;
meillo@754 1229 XFree(protocols);
meillo@754 1230 }
meillo@754 1231 return ret;
meillo@754 1232 }
meillo@754 1233
meillo@754 1234 void
meillo@756 1235 killclient() {
meillo@754 1236 if(!sel)
meillo@754 1237 return;
meillo@754 1238 if(isprotodel(sel))
meillo@754 1239 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
meillo@754 1240 else
meillo@754 1241 XKillClient(dpy, sel->win);
meillo@754 1242 }
meillo@754 1243
meillo@754 1244 void
meillo@754 1245 manage(Window w, XWindowAttributes *wa) {
meillo@754 1246 Client *c;
meillo@754 1247 Window trans;
meillo@754 1248
meillo@754 1249 c = emallocz(sizeof(Client));
meillo@766 1250 c->tag = True;
meillo@754 1251 c->win = w;
meillo@754 1252 c->x = wa->x;
meillo@754 1253 c->y = wa->y;
meillo@754 1254 c->w = wa->width;
meillo@754 1255 c->h = wa->height;
meillo@754 1256 if(c->w == sw && c->h == sh) {
meillo@754 1257 c->border = 0;
meillo@754 1258 c->x = sx;
meillo@754 1259 c->y = sy;
meillo@754 1260 } else {
meillo@754 1261 c->border = BORDERPX;
meillo@754 1262 if(c->x + c->w + 2 * c->border > wax + waw)
meillo@754 1263 c->x = wax + waw - c->w - 2 * c->border;
meillo@754 1264 if(c->y + c->h + 2 * c->border > way + wah)
meillo@754 1265 c->y = way + wah - c->h - 2 * c->border;
meillo@754 1266 if(c->x < wax)
meillo@754 1267 c->x = wax;
meillo@754 1268 if(c->y < way)
meillo@754 1269 c->y = way;
meillo@754 1270 }
meillo@754 1271 updatesizehints(c);
meillo@754 1272 XSelectInput(dpy, c->win,
meillo@754 1273 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
meillo@754 1274 XGetTransientForHint(dpy, c->win, &trans);
meillo@754 1275 grabbuttons(c, False);
meillo@764 1276 XSetWindowBorder(dpy, c->win, dc.norm[ColBG]);
meillo@754 1277 updatetitle(c);
meillo@766 1278 settag(c, getclient(trans));
meillo@754 1279 if(!c->isfloat)
meillo@754 1280 c->isfloat = trans || c->isfixed;
meillo@754 1281 if(clients)
meillo@754 1282 clients->prev = c;
meillo@754 1283 c->next = clients;
meillo@754 1284 c->snext = stack;
meillo@754 1285 stack = clients = c;
meillo@754 1286 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
meillo@754 1287 XMapWindow(dpy, c->win);
meillo@754 1288 setclientstate(c, NormalState);
meillo@754 1289 if(isvisible(c))
meillo@754 1290 focus(c);
meillo@754 1291 arrange();
meillo@754 1292 }
meillo@754 1293
meillo@754 1294 void
meillo@754 1295 resize(Client *c, Bool sizehints) {
meillo@754 1296 float actual, dx, dy, max, min;
meillo@754 1297 XWindowChanges wc;
meillo@754 1298
meillo@754 1299 if(c->w <= 0 || c->h <= 0)
meillo@754 1300 return;
meillo@754 1301 if(sizehints) {
meillo@754 1302 if(c->minw && c->w < c->minw)
meillo@754 1303 c->w = c->minw;
meillo@754 1304 if(c->minh && c->h < c->minh)
meillo@754 1305 c->h = c->minh;
meillo@754 1306 if(c->maxw && c->w > c->maxw)
meillo@754 1307 c->w = c->maxw;
meillo@754 1308 if(c->maxh && c->h > c->maxh)
meillo@754 1309 c->h = c->maxh;
meillo@754 1310 /* inspired by algorithm from fluxbox */
meillo@754 1311 if(c->minay > 0 && c->maxay && (c->h - c->baseh) > 0) {
meillo@754 1312 dx = (float)(c->w - c->basew);
meillo@754 1313 dy = (float)(c->h - c->baseh);
meillo@754 1314 min = (float)(c->minax) / (float)(c->minay);
meillo@754 1315 max = (float)(c->maxax) / (float)(c->maxay);
meillo@754 1316 actual = dx / dy;
meillo@754 1317 if(max > 0 && min > 0 && actual > 0) {
meillo@754 1318 if(actual < min) {
meillo@754 1319 dy = (dx * min + dy) / (min * min + 1);
meillo@754 1320 dx = dy * min;
meillo@754 1321 c->w = (int)dx + c->basew;
meillo@754 1322 c->h = (int)dy + c->baseh;
meillo@754 1323 }
meillo@754 1324 else if(actual > max) {
meillo@754 1325 dy = (dx * min + dy) / (max * max + 1);
meillo@754 1326 dx = dy * min;
meillo@754 1327 c->w = (int)dx + c->basew;
meillo@754 1328 c->h = (int)dy + c->baseh;
meillo@754 1329 }
meillo@754 1330 }
meillo@754 1331 }
meillo@754 1332 if(c->incw)
meillo@754 1333 c->w -= (c->w - c->basew) % c->incw;
meillo@754 1334 if(c->inch)
meillo@754 1335 c->h -= (c->h - c->baseh) % c->inch;
meillo@754 1336 }
meillo@754 1337 if(c->w == sw && c->h == sh)
meillo@754 1338 c->border = 0;
meillo@754 1339 else
meillo@754 1340 c->border = BORDERPX;
meillo@754 1341 /* offscreen appearance fixes */
meillo@754 1342 if(c->x > sw)
meillo@754 1343 c->x = sw - c->w - 2 * c->border;
meillo@754 1344 if(c->y > sh)
meillo@754 1345 c->y = sh - c->h - 2 * c->border;
meillo@754 1346 if(c->x + c->w + 2 * c->border < sx)
meillo@754 1347 c->x = sx;
meillo@754 1348 if(c->y + c->h + 2 * c->border < sy)
meillo@754 1349 c->y = sy;
meillo@754 1350 wc.x = c->x;
meillo@754 1351 wc.y = c->y;
meillo@754 1352 wc.width = c->w;
meillo@754 1353 wc.height = c->h;
meillo@754 1354 wc.border_width = c->border;
meillo@754 1355 XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
meillo@754 1356 configure(c);
meillo@754 1357 XSync(dpy, False);
meillo@754 1358 }
meillo@754 1359
meillo@754 1360 void
meillo@754 1361 updatesizehints(Client *c) {
meillo@754 1362 long msize;
meillo@754 1363 XSizeHints size;
meillo@754 1364
meillo@754 1365 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
meillo@754 1366 size.flags = PSize;
meillo@754 1367 c->flags = size.flags;
meillo@754 1368 if(c->flags & PBaseSize) {
meillo@754 1369 c->basew = size.base_width;
meillo@754 1370 c->baseh = size.base_height;
meillo@754 1371 } else {
meillo@754 1372 c->basew = c->baseh = 0;
meillo@754 1373 }
meillo@754 1374 if(c->flags & PResizeInc) {
meillo@754 1375 c->incw = size.width_inc;
meillo@754 1376 c->inch = size.height_inc;
meillo@754 1377 } else {
meillo@754 1378 c->incw = c->inch = 0;
meillo@754 1379 }
meillo@754 1380 if(c->flags & PMaxSize) {
meillo@754 1381 c->maxw = size.max_width;
meillo@754 1382 c->maxh = size.max_height;
meillo@754 1383 } else {
meillo@754 1384 c->maxw = c->maxh = 0;
meillo@754 1385 }
meillo@754 1386 if(c->flags & PMinSize) {
meillo@754 1387 c->minw = size.min_width;
meillo@754 1388 c->minh = size.min_height;
meillo@754 1389 } else {
meillo@754 1390 c->minw = c->minh = 0;
meillo@754 1391 }
meillo@754 1392 if(c->flags & PAspect) {
meillo@754 1393 c->minax = size.min_aspect.x;
meillo@754 1394 c->minay = size.min_aspect.y;
meillo@754 1395 c->maxax = size.max_aspect.x;
meillo@754 1396 c->maxay = size.max_aspect.y;
meillo@754 1397 } else {
meillo@754 1398 c->minax = c->minay = c->maxax = c->maxay = 0;
meillo@754 1399 }
meillo@754 1400 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh &&
meillo@754 1401 c->maxw == c->minw && c->maxh == c->minh);
meillo@754 1402 }
meillo@754 1403
meillo@754 1404 void
meillo@754 1405 updatetitle(Client *c) {
meillo@754 1406 char **list = NULL;
meillo@754 1407 int n;
meillo@754 1408 XTextProperty name;
meillo@754 1409
meillo@754 1410 name.nitems = 0;
meillo@754 1411 c->name[0] = 0;
meillo@754 1412 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
meillo@754 1413 if(!name.nitems)
meillo@754 1414 XGetWMName(dpy, c->win, &name);
meillo@754 1415 if(!name.nitems)
meillo@754 1416 return;
meillo@754 1417 if(name.encoding == XA_STRING)
meillo@754 1418 strncpy(c->name, (char *)name.value, sizeof c->name);
meillo@754 1419 else {
meillo@754 1420 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
meillo@754 1421 strncpy(c->name, *list, sizeof c->name);
meillo@754 1422 XFreeStringList(list);
meillo@754 1423 }
meillo@754 1424 }
meillo@754 1425 XFree(name.value);
meillo@754 1426 }
meillo@754 1427
meillo@754 1428 void
meillo@754 1429 unmanage(Client *c) {
meillo@754 1430 Client *nc;
meillo@754 1431
meillo@754 1432 /* The server grab construct avoids race conditions. */
meillo@754 1433 XGrabServer(dpy);
meillo@754 1434 XSetErrorHandler(xerrordummy);
meillo@754 1435 detach(c);
meillo@754 1436 detachstack(c);
meillo@754 1437 if(sel == c) {
meillo@754 1438 for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
meillo@754 1439 focus(nc);
meillo@754 1440 }
meillo@754 1441 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
meillo@754 1442 setclientstate(c, WithdrawnState);
meillo@754 1443 free(c);
meillo@754 1444 XSync(dpy, False);
meillo@754 1445 XSetErrorHandler(xerror);
meillo@754 1446 XUngrabServer(dpy);
meillo@754 1447 arrange();
meillo@754 1448 }
meillo@754 1449
meillo@754 1450
meillo@754 1451
meillo@754 1452
meillo@754 1453
meillo@754 1454
meillo@754 1455
meillo@754 1456
meillo@754 1457
meillo@754 1458
meillo@754 1459
meillo@754 1460
meillo@754 1461
meillo@754 1462
meillo@754 1463
meillo@754 1464
meillo@754 1465
meillo@754 1466
meillo@754 1467
meillo@754 1468 /* static */
meillo@754 1469
garbeam@0 1470
garbeam@0 1471 static void
arg@487 1472 cleanup(void) {
arg@302 1473 close(STDIN_FILENO);
arg@645 1474 while(stack) {
arg@708 1475 resize(stack, True);
arg@645 1476 unmanage(stack);
garbeam@76 1477 }
arg@295 1478 if(dc.font.set)
arg@295 1479 XFreeFontSet(dpy, dc.font.set);
arg@295 1480 else
arg@295 1481 XFreeFont(dpy, dc.font.xfont);
arg@292 1482 XUngrabKey(dpy, AnyKey, AnyModifier, root);
arg@295 1483 XFreePixmap(dpy, dc.drawable);
arg@295 1484 XFreeGC(dpy, dc.gc);
arg@309 1485 XDestroyWindow(dpy, barwin);
arg@616 1486 XFreeCursor(dpy, cursor[CurNormal]);
arg@616 1487 XFreeCursor(dpy, cursor[CurResize]);
arg@616 1488 XFreeCursor(dpy, cursor[CurMove]);
garbeam@76 1489 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
arg@292 1490 XSync(dpy, False);
garbeam@76 1491 }
garbeam@0 1492
garbeam@0 1493 static void
arg@487 1494 scan(void) {
garbeam@0 1495 unsigned int i, num;
arg@123 1496 Window *wins, d1, d2;
garbeam@0 1497 XWindowAttributes wa;
garbeam@0 1498
arg@292 1499 wins = NULL;
garbeam@0 1500 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
garbeam@0 1501 for(i = 0; i < num; i++) {
garbeam@0 1502 if(!XGetWindowAttributes(dpy, wins[i], &wa))
garbeam@0 1503 continue;
garbeam@0 1504 if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
garbeam@0 1505 continue;
garbeam@0 1506 if(wa.map_state == IsViewable)
garbeam@10 1507 manage(wins[i], &wa);
garbeam@0 1508 }
garbeam@0 1509 }
garbeam@0 1510 if(wins)
garbeam@0 1511 XFree(wins);
garbeam@0 1512 }
garbeam@0 1513
arg@333 1514 static void
arg@487 1515 setup(void) {
arg@333 1516 int i, j;
arg@333 1517 unsigned int mask;
arg@333 1518 Window w;
arg@333 1519 XModifierKeymap *modmap;
arg@333 1520 XSetWindowAttributes wa;
arg@333 1521
arg@333 1522 /* init atoms */
arg@333 1523 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
arg@333 1524 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
arg@725 1525 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
arg@333 1526 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
arg@333 1527 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
arg@333 1528 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
arg@333 1529 PropModeReplace, (unsigned char *) netatom, NetLast);
arg@333 1530 /* init cursors */
arg@333 1531 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
arg@333 1532 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
arg@333 1533 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
arg@532 1534 /* init modifier map */
arg@679 1535 numlockmask = 0;
arg@333 1536 modmap = XGetModifierMapping(dpy);
arg@333 1537 for (i = 0; i < 8; i++) {
arg@333 1538 for (j = 0; j < modmap->max_keypermod; j++) {
arg@333 1539 if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
arg@333 1540 numlockmask = (1 << i);
arg@333 1541 }
arg@333 1542 }
arg@616 1543 XFreeModifiermap(modmap);
arg@532 1544 /* select for events */
arg@461 1545 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
arg@461 1546 | EnterWindowMask | LeaveWindowMask;
arg@333 1547 wa.cursor = cursor[CurNormal];
arg@333 1548 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
arg@333 1549 grabkeys();
meillo@766 1550 seltag = True;
arg@333 1551 /* style */
arg@353 1552 dc.norm[ColBG] = getcolor(NORMBGCOLOR);
arg@353 1553 dc.norm[ColFG] = getcolor(NORMFGCOLOR);
arg@353 1554 dc.sel[ColBG] = getcolor(SELBGCOLOR);
arg@353 1555 dc.sel[ColFG] = getcolor(SELFGCOLOR);
arg@333 1556 setfont(FONT);
arg@532 1557 /* geometry */
arg@333 1558 sx = sy = 0;
arg@333 1559 sw = DisplayWidth(dpy, screen);
arg@333 1560 sh = DisplayHeight(dpy, screen);
arg@650 1561 nmaster = NMASTER;
meillo@754 1562 bmw = 1;
arg@532 1563 /* bar */
arg@353 1564 dc.h = bh = dc.font.height + 2;
arg@333 1565 wa.override_redirect = 1;
arg@333 1566 wa.background_pixmap = ParentRelative;
arg@333 1567 wa.event_mask = ButtonPressMask | ExposureMask;
meillo@754 1568 barwin = XCreateWindow(dpy, root, sx, sy, sw, bh, 0,
arg@739 1569 DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
arg@333 1570 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
arg@333 1571 XDefineCursor(dpy, barwin, cursor[CurNormal]);
arg@333 1572 XMapRaised(dpy, barwin);
arg@532 1573 strcpy(stext, "dwm-"VERSION);
arg@565 1574 /* windowarea */
arg@565 1575 wax = sx;
meillo@754 1576 way = sy + bh;
arg@565 1577 wah = sh - bh;
arg@565 1578 waw = sw;
arg@532 1579 /* pixmap for everything */
arg@333 1580 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
arg@333 1581 dc.gc = XCreateGC(dpy, root, 0, 0);
arg@344 1582 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
arg@532 1583 /* multihead support */
arg@716 1584 selscreen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
arg@333 1585 }
arg@333 1586
garbeam@76 1587 /*
garbeam@76 1588 * Startup Error handler to check if another window manager
garbeam@76 1589 * is already running.
garbeam@76 1590 */
garbeam@76 1591 static int
arg@461 1592 xerrorstart(Display *dsply, XErrorEvent *ee) {
garbeam@76 1593 otherwm = True;
garbeam@76 1594 return -1;
garbeam@76 1595 }
garbeam@76 1596
meillo@754 1597
garbeam@84 1598
garbeam@13 1599 void
arg@461 1600 sendevent(Window w, Atom a, long value) {
garbeam@13 1601 XEvent e;
garbeam@13 1602
garbeam@13 1603 e.type = ClientMessage;
garbeam@13 1604 e.xclient.window = w;
garbeam@13 1605 e.xclient.message_type = a;
garbeam@13 1606 e.xclient.format = 32;
garbeam@13 1607 e.xclient.data.l[0] = value;
garbeam@13 1608 e.xclient.data.l[1] = CurrentTime;
garbeam@13 1609 XSendEvent(dpy, w, False, NoEventMask, &e);
garbeam@79 1610 XSync(dpy, False);
garbeam@13 1611 }
garbeam@13 1612
garbeam@76 1613 void
meillo@756 1614 quit() {
arg@302 1615 readin = running = False;
garbeam@75 1616 }
garbeam@75 1617
arg@532 1618 /* There's no way to check accesses to destroyed windows, thus those cases are
garbeam@84 1619 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
arg@455 1620 * default error handler, which may call exit.
garbeam@0 1621 */
garbeam@10 1622 int
arg@461 1623 xerror(Display *dpy, XErrorEvent *ee) {
garbeam@75 1624 if(ee->error_code == BadWindow
arg@123 1625 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
arg@123 1626 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
arg@123 1627 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
arg@123 1628 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
arg@123 1629 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
arg@458 1630 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
arg@458 1631 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
garbeam@0 1632 return 0;
garbeam@34 1633 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
arg@123 1634 ee->request_code, ee->error_code);
arg@455 1635 return xerrorxlib(dpy, ee); /* may call exit */
garbeam@27 1636 }
garbeam@27 1637
garbeam@0 1638 int
arg@461 1639 main(int argc, char *argv[]) {
arg@581 1640 char *p;
arg@333 1641 int r, xfd;
garbeam@59 1642 fd_set rd;
garbeam@0 1643
arg@137 1644 if(argc == 2 && !strncmp("-v", argv[1], 3)) {
arg@644 1645 fputs("dwm-"VERSION", (C)opyright MMVI-MMVII Anselm R. Garbe\n", stdout);
arg@137 1646 exit(EXIT_SUCCESS);
meillo@755 1647 } else if(argc != 1) {
meillo@755 1648 eprint("usage: dwm [-v]\n");
garbeam@0 1649 }
arg@619 1650 setlocale(LC_CTYPE, "");
garbeam@0 1651 dpy = XOpenDisplay(0);
meillo@755 1652 if(!dpy) {
arg@197 1653 eprint("dwm: cannot open display\n");
meillo@755 1654 }
arg@265 1655 xfd = ConnectionNumber(dpy);
garbeam@0 1656 screen = DefaultScreen(dpy);
garbeam@0 1657 root = RootWindow(dpy, screen);
garbeam@75 1658 otherwm = False;
garbeam@75 1659 XSetErrorHandler(xerrorstart);
arg@197 1660 /* this causes an error if some other window manager is running */
garbeam@0 1661 XSelectInput(dpy, root, SubstructureRedirectMask);
garbeam@78 1662 XSync(dpy, False);
meillo@755 1663 if(otherwm) {
garbeam@75 1664 eprint("dwm: another window manager is already running\n");
meillo@755 1665 }
garbeam@0 1666
arg@292 1667 XSync(dpy, False);
garbeam@78 1668 XSetErrorHandler(NULL);
garbeam@75 1669 xerrorxlib = XSetErrorHandler(xerror);
arg@275 1670 XSync(dpy, False);
arg@333 1671 setup();
garbeam@74 1672 drawstatus();
garbeam@75 1673 scan();
garbeam@3 1674
arg@214 1675 /* main event loop, also reads status text from stdin */
arg@242 1676 XSync(dpy, False);
arg@292 1677 procevent();
arg@302 1678 readin = True;
garbeam@5 1679 while(running) {
garbeam@59 1680 FD_ZERO(&rd);
arg@164 1681 if(readin)
arg@164 1682 FD_SET(STDIN_FILENO, &rd);
arg@265 1683 FD_SET(xfd, &rd);
arg@578 1684 if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
arg@578 1685 if(errno == EINTR)
arg@578 1686 continue;
arg@581 1687 eprint("select failed\n");
arg@578 1688 }
arg@578 1689 if(FD_ISSET(STDIN_FILENO, &rd)) {
arg@581 1690 switch(r = read(STDIN_FILENO, stext, sizeof stext - 1)) {
arg@578 1691 case -1:
arg@581 1692 strncpy(stext, strerror(errno), sizeof stext - 1);
arg@583 1693 stext[sizeof stext - 1] = '\0';
arg@578 1694 readin = False;
arg@578 1695 break;
arg@578 1696 case 0:
arg@583 1697 strncpy(stext, "EOF", 4);
arg@578 1698 readin = False;
arg@578 1699 break;
arg@578 1700 default:
arg@582 1701 for(stext[r] = '\0', p = stext + strlen(stext) - 1; p >= stext && *p == '\n'; *p-- = '\0');
arg@744 1702 for(; p >= stext && *p != '\n'; --p);
arg@581 1703 if(p > stext)
arg@581 1704 strncpy(stext, p + 1, sizeof stext);
garbeam@59 1705 }
arg@578 1706 drawstatus();
garbeam@59 1707 }
arg@578 1708 if(FD_ISSET(xfd, &rd))
arg@578 1709 procevent();
garbeam@5 1710 }
garbeam@0 1711 cleanup();
garbeam@0 1712 XCloseDisplay(dpy);
garbeam@0 1713 return 0;
garbeam@0 1714 }