aewl

annotate aewl.c @ 786:e65be4ffdbdc

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