Configuration of dwm for Mac Computers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1706 lines
41 KiB

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