aewl

annotate aewl.c @ 761:59ce221b9a37

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