aewl

annotate aewl.c @ 781:1ec698ca43c5

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