aewl

annotate aewl.c @ 772:0c1e9952a278

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