aewl

annotate aewl.c @ 782:f1e806b34697

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