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.

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