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.

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