aewl

annotate dwm.c @ 757:427608ef0687

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