aewl

annotate aewl.c @ 759:45f23169563e

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