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.

1736 lines
40 KiB

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