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.

1745 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. int bw, oldbw;
  70. Bool isbanned, isfixed, isfloating, ismoved, isurgent;
  71. uint tags;
  72. Client *next;
  73. Client *prev;
  74. Client *snext;
  75. Window win;
  76. };
  77. typedef struct {
  78. int x, y, w, h;
  79. ulong norm[ColLast];
  80. ulong sel[ColLast];
  81. Drawable drawable;
  82. GC gc;
  83. struct {
  84. int ascent;
  85. int descent;
  86. int height;
  87. XFontSet set;
  88. XFontStruct *xfont;
  89. } font;
  90. } DC; /* draw context */
  91. typedef struct {
  92. uint mod;
  93. KeySym keysym;
  94. void (*func)(const void *arg);
  95. const void *arg;
  96. } Key;
  97. typedef struct {
  98. const char *symbol;
  99. void (*arrange)(void);
  100. } Layout;
  101. typedef struct {
  102. const char *class;
  103. const char *instance;
  104. const char *title;
  105. uint tags;
  106. Bool isfloating;
  107. } Rule;
  108. /* function declarations */
  109. void applyrules(Client *c);
  110. void arrange(void);
  111. void attach(Client *c);
  112. void attachstack(Client *c);
  113. void buttonpress(XEvent *e);
  114. void checkotherwm(void);
  115. void cleanup(void);
  116. void configure(Client *c);
  117. void configurenotify(XEvent *e);
  118. void configurerequest(XEvent *e);
  119. void destroynotify(XEvent *e);
  120. void detach(Client *c);
  121. void detachstack(Client *c);
  122. void drawbar(void);
  123. void drawsquare(Bool filled, Bool empty, Bool invert, ulong col[ColLast]);
  124. void drawtext(const char *text, ulong col[ColLast], Bool invert);
  125. void enternotify(XEvent *e);
  126. void eprint(const char *errstr, ...);
  127. void expose(XEvent *e);
  128. void focus(Client *c);
  129. void focusin(XEvent *e);
  130. void focusnext(const void *arg);
  131. void focusprev(const void *arg);
  132. Client *getclient(Window w);
  133. ulong getcolor(const char *colstr);
  134. long getstate(Window w);
  135. Bool gettextprop(Window w, Atom atom, char *text, uint size);
  136. void grabbuttons(Client *c, Bool focused);
  137. void grabkeys(void);
  138. void initfont(const char *fontstr);
  139. Bool isoccupied(uint t);
  140. Bool isprotodel(Client *c);
  141. Bool isurgent(uint t);
  142. void keypress(XEvent *e);
  143. void killclient(const void *arg);
  144. void manage(Window w, XWindowAttributes *wa);
  145. void mappingnotify(XEvent *e);
  146. void maprequest(XEvent *e);
  147. void movemouse(Client *c);
  148. Client *nexttiled(Client *c);
  149. void propertynotify(XEvent *e);
  150. void quit(const void *arg);
  151. void resize(Client *c, int x, int y, int w, int h, Bool sizehints);
  152. void resizemouse(Client *c);
  153. void restack(void);
  154. void run(void);
  155. void scan(void);
  156. void setclientstate(Client *c, long state);
  157. void setmfact(const void *arg);
  158. void setup(void);
  159. void spawn(const void *arg);
  160. void tag(const void *arg);
  161. uint textnw(const char *text, uint len);
  162. void tile(void);
  163. void togglebar(const void *arg);
  164. void togglefloating(const void *arg);
  165. void togglelayout(const void *arg);
  166. void togglemax(const void *arg);
  167. void toggletag(const void *arg);
  168. void toggleview(const void *arg);
  169. void unmanage(Client *c);
  170. void unmapnotify(XEvent *e);
  171. void updatebar(void);
  172. void updategeom(void);
  173. void updatesizehints(Client *c);
  174. void updatetitle(Client *c);
  175. void updatewmhints(Client *c);
  176. void view(const void *arg);
  177. int xerror(Display *dpy, XErrorEvent *ee);
  178. int xerrordummy(Display *dpy, XErrorEvent *ee);
  179. int xerrorstart(Display *dpy, XErrorEvent *ee);
  180. void zoom(const void *arg);
  181. /* variables */
  182. char stext[256];
  183. int screen, sx, sy, sw, sh;
  184. int by, bh, blw, wx, wy, ww, wh;
  185. uint seltags = 0;
  186. int (*xerrorxlib)(Display *, XErrorEvent *);
  187. uint numlockmask = 0;
  188. void (*handler[LASTEvent]) (XEvent *) = {
  189. [ButtonPress] = buttonpress,
  190. [ConfigureRequest] = configurerequest,
  191. [ConfigureNotify] = configurenotify,
  192. [DestroyNotify] = destroynotify,
  193. [EnterNotify] = enternotify,
  194. [Expose] = expose,
  195. [FocusIn] = focusin,
  196. [KeyPress] = keypress,
  197. [MappingNotify] = mappingnotify,
  198. [MapRequest] = maprequest,
  199. [PropertyNotify] = propertynotify,
  200. [UnmapNotify] = unmapnotify
  201. };
  202. Atom wmatom[WMLast], netatom[NetLast];
  203. Bool ismax = False;
  204. Bool otherwm, readin;
  205. Bool running = True;
  206. uint tagset[] = {1, 1}; /* after start, first tag is selected */
  207. Client *clients = NULL;
  208. Client *sel = NULL;
  209. Client *stack = NULL;
  210. Cursor cursor[CurLast];
  211. Display *dpy;
  212. DC dc = {0};
  213. Layout layouts[];
  214. Layout *lt = layouts;
  215. Window root, barwin;
  216. /* configuration, allows nested code to access above variables */
  217. #include "config.h"
  218. /* compile-time check if all tags fit into an uint bit array. */
  219. struct NumTags { char limitexceeded[sizeof(uint) * 8 < LENGTH(tags) ? -1 : 1]; };
  220. /* function implementations */
  221. void
  222. applyrules(Client *c) {
  223. uint i;
  224. Rule *r;
  225. XClassHint ch = { 0 };
  226. /* rule matching */
  227. XGetClassHint(dpy, c->win, &ch);
  228. for(i = 0; i < LENGTH(rules); i++) {
  229. r = &rules[i];
  230. if((!r->title || strstr(c->name, r->title))
  231. && (!r->class || (ch.res_class && strstr(ch.res_class, r->class)))
  232. && (!r->instance || (ch.res_name && strstr(ch.res_name, r->instance)))) {
  233. c->isfloating = r->isfloating;
  234. c->tags |= r->tags & TAGMASK;
  235. }
  236. }
  237. if(ch.res_class)
  238. XFree(ch.res_class);
  239. if(ch.res_name)
  240. XFree(ch.res_name);
  241. if(!c->tags)
  242. c->tags = tagset[seltags];
  243. }
  244. void
  245. arrange(void) {
  246. Client *c;
  247. for(c = clients; c; c = c->next)
  248. if(c->tags & tagset[seltags]) { /* is visible */
  249. if(ismax && !c->isfixed) {
  250. XMoveResizeWindow(dpy, c->win, wx, wy, ww - 2 * c->bw, wh - 2 * c->bw);
  251. c->ismoved = True;
  252. }
  253. else if(!lt->arrange || c->isfloating)
  254. resize(c, c->x, c->y, c->w, c->h, True);
  255. c->isbanned = False;
  256. }
  257. else if(!c->isbanned) {
  258. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  259. c->isbanned = c->ismoved = True;
  260. }
  261. focus(NULL);
  262. if(lt->arrange && !ismax)
  263. lt->arrange();
  264. restack();
  265. }
  266. void
  267. attach(Client *c) {
  268. if(clients)
  269. clients->prev = c;
  270. c->next = clients;
  271. clients = c;
  272. }
  273. void
  274. attachstack(Client *c) {
  275. c->snext = stack;
  276. stack = c;
  277. }
  278. void
  279. buttonpress(XEvent *e) {
  280. uint i, x, mask;
  281. Client *c;
  282. XButtonPressedEvent *ev = &e->xbutton;
  283. if(ev->window == barwin) {
  284. x = 0;
  285. for(i = 0; i < LENGTH(tags); i++) {
  286. x += TEXTW(tags[i]);
  287. if(ev->x < x) {
  288. mask = 1 << i;
  289. if(ev->button == Button1) {
  290. if(ev->state & MODKEY)
  291. tag(&mask);
  292. else
  293. view(&mask);
  294. }
  295. else if(ev->button == Button3) {
  296. if(ev->state & MODKEY)
  297. toggletag(&mask);
  298. else
  299. toggleview(&mask);
  300. }
  301. return;
  302. }
  303. }
  304. if(ev->x < x + blw) {
  305. if(ev->button == Button1)
  306. togglelayout(NULL);
  307. else if(ev->button == Button3)
  308. togglemax(NULL);
  309. }
  310. }
  311. else if((c = getclient(ev->window))) {
  312. focus(c);
  313. if(CLEANMASK(ev->state) != MODKEY || (ismax && !c->isfixed))
  314. return;
  315. if(ev->button == Button1)
  316. movemouse(c);
  317. else if(ev->button == Button2)
  318. togglefloating(NULL);
  319. else if(ev->button == Button3 && !c->isfixed)
  320. resizemouse(c);
  321. }
  322. }
  323. void
  324. checkotherwm(void) {
  325. otherwm = False;
  326. XSetErrorHandler(xerrorstart);
  327. /* this causes an error if some other window manager is running */
  328. XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
  329. XSync(dpy, False);
  330. if(otherwm)
  331. eprint("dwm: another window manager is already running\n");
  332. XSetErrorHandler(NULL);
  333. xerrorxlib = XSetErrorHandler(xerror);
  334. XSync(dpy, False);
  335. }
  336. void
  337. cleanup(void) {
  338. close(STDIN_FILENO);
  339. view((uint[]){~0});
  340. while(stack)
  341. unmanage(stack);
  342. if(dc.font.set)
  343. XFreeFontSet(dpy, dc.font.set);
  344. else
  345. XFreeFont(dpy, dc.font.xfont);
  346. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  347. XFreePixmap(dpy, dc.drawable);
  348. XFreeGC(dpy, dc.gc);
  349. XFreeCursor(dpy, cursor[CurNormal]);
  350. XFreeCursor(dpy, cursor[CurResize]);
  351. XFreeCursor(dpy, cursor[CurMove]);
  352. XDestroyWindow(dpy, barwin);
  353. XSync(dpy, False);
  354. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  355. }
  356. void
  357. configure(Client *c) {
  358. XConfigureEvent ce;
  359. ce.type = ConfigureNotify;
  360. ce.display = dpy;
  361. ce.event = c->win;
  362. ce.window = c->win;
  363. ce.x = c->x;
  364. ce.y = c->y;
  365. ce.width = c->w;
  366. ce.height = c->h;
  367. ce.border_width = c->bw;
  368. ce.above = None;
  369. ce.override_redirect = False;
  370. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
  371. }
  372. void
  373. configurenotify(XEvent *e) {
  374. XConfigureEvent *ev = &e->xconfigure;
  375. if(ev->window == root && (ev->width != sw || ev->height != sh)) {
  376. sw = ev->width;
  377. sh = ev->height;
  378. updategeom();
  379. updatebar();
  380. arrange();
  381. }
  382. }
  383. void
  384. configurerequest(XEvent *e) {
  385. Client *c;
  386. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  387. XWindowChanges wc;
  388. if((c = getclient(ev->window))) {
  389. if(ev->value_mask & CWBorderWidth)
  390. c->bw = ev->border_width;
  391. if(ismax && !c->isbanned && !c->isfixed)
  392. XMoveResizeWindow(dpy, c->win, wx, wy, ww - 2 * c->bw, wh + 2 * c->bw);
  393. else if(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. if(c->isfloating)
  868. XRaiseWindow(dpy, c->win);
  869. attach(c);
  870. attachstack(c);
  871. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h); /* some windows require this */
  872. XMapWindow(dpy, c->win);
  873. setclientstate(c, NormalState);
  874. arrange();
  875. }
  876. void
  877. mappingnotify(XEvent *e) {
  878. XMappingEvent *ev = &e->xmapping;
  879. XRefreshKeyboardMapping(ev);
  880. if(ev->request == MappingKeyboard)
  881. grabkeys();
  882. }
  883. void
  884. maprequest(XEvent *e) {
  885. static XWindowAttributes wa;
  886. XMapRequestEvent *ev = &e->xmaprequest;
  887. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  888. return;
  889. if(wa.override_redirect)
  890. return;
  891. if(!getclient(ev->window))
  892. manage(ev->window, &wa);
  893. }
  894. void
  895. movemouse(Client *c) {
  896. int x1, y1, ocx, ocy, di, nx, ny;
  897. uint dui;
  898. Window dummy;
  899. XEvent ev;
  900. restack();
  901. ocx = nx = c->x;
  902. ocy = ny = c->y;
  903. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  904. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  905. return;
  906. XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
  907. for(;;) {
  908. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  909. switch (ev.type) {
  910. case ButtonRelease:
  911. XUngrabPointer(dpy, CurrentTime);
  912. return;
  913. case ConfigureRequest:
  914. case Expose:
  915. case MapRequest:
  916. handler[ev.type](&ev);
  917. break;
  918. case MotionNotify:
  919. XSync(dpy, False);
  920. nx = ocx + (ev.xmotion.x - x1);
  921. ny = ocy + (ev.xmotion.y - y1);
  922. if(snap && nx >= wx && nx <= wx + ww
  923. && ny >= wy && ny <= wy + wh) {
  924. if(abs(wx - nx) < snap)
  925. nx = wx;
  926. else if(abs((wx + ww) - (nx + c->w + 2 * c->bw)) < snap)
  927. nx = wx + ww - c->w - 2 * c->bw;
  928. if(abs(wy - ny) < snap)
  929. ny = wy;
  930. else if(abs((wy + wh) - (ny + c->h + 2 * c->bw)) < snap)
  931. ny = wy + wh - c->h - 2 * c->bw;
  932. if(!c->isfloating && lt->arrange && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
  933. togglefloating(NULL);
  934. }
  935. if(!lt->arrange || c->isfloating)
  936. resize(c, nx, ny, c->w, c->h, False);
  937. break;
  938. }
  939. }
  940. }
  941. Client *
  942. nexttiled(Client *c) {
  943. for(; c && (c->isfloating || c->isbanned); c = c->next);
  944. return c;
  945. }
  946. void
  947. propertynotify(XEvent *e) {
  948. Client *c;
  949. Window trans;
  950. XPropertyEvent *ev = &e->xproperty;
  951. if(ev->state == PropertyDelete)
  952. return; /* ignore */
  953. if((c = getclient(ev->window))) {
  954. switch (ev->atom) {
  955. default: break;
  956. case XA_WM_TRANSIENT_FOR:
  957. XGetTransientForHint(dpy, c->win, &trans);
  958. if(!c->isfloating && (c->isfloating = (getclient(trans) != NULL)))
  959. arrange();
  960. break;
  961. case XA_WM_NORMAL_HINTS:
  962. updatesizehints(c);
  963. break;
  964. case XA_WM_HINTS:
  965. updatewmhints(c);
  966. drawbar();
  967. break;
  968. }
  969. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  970. updatetitle(c);
  971. if(c == sel)
  972. drawbar();
  973. }
  974. }
  975. }
  976. void
  977. quit(const void *arg) {
  978. readin = running = False;
  979. }
  980. void
  981. resize(Client *c, int x, int y, int w, int h, Bool sizehints) {
  982. XWindowChanges wc;
  983. if(sizehints) {
  984. /* set minimum possible */
  985. w = MAX(1, w);
  986. h = MAX(1, h);
  987. /* temporarily remove base dimensions */
  988. w -= c->basew;
  989. h -= c->baseh;
  990. /* adjust for aspect limits */
  991. if(c->minax != c->maxax && c->minay != c->maxay
  992. && c->minax > 0 && c->maxax > 0 && c->minay > 0 && c->maxay > 0) {
  993. if(w * c->maxay > h * c->maxax)
  994. w = h * c->maxax / c->maxay;
  995. else if(w * c->minay < h * c->minax)
  996. h = w * c->minay / c->minax;
  997. }
  998. /* adjust for increment value */
  999. if(c->incw)
  1000. w -= w % c->incw;
  1001. if(c->inch)
  1002. h -= h % c->inch;
  1003. /* restore base dimensions */
  1004. w += c->basew;
  1005. h += c->baseh;
  1006. w = MAX(w, c->minw);
  1007. h = MAX(h, c->minh);
  1008. if (c->maxw)
  1009. w = MIN(w, c->maxw);
  1010. if (c->maxh)
  1011. h = MIN(h, c->maxh);
  1012. }
  1013. if(w <= 0 || h <= 0)
  1014. return;
  1015. if(x > sx + sw)
  1016. x = sw - w - 2 * c->bw;
  1017. if(y > sy + sh)
  1018. y = sh - h - 2 * c->bw;
  1019. if(x + w + 2 * c->bw < sx)
  1020. x = sx;
  1021. if(y + h + 2 * c->bw < sy)
  1022. y = sy;
  1023. if(h < bh)
  1024. h = bh;
  1025. if(w < bh)
  1026. w = bh;
  1027. if(c->x != x || c->y != y || c->w != w || c->h != h || c->ismoved) {
  1028. c->ismoved = False;
  1029. c->x = wc.x = x;
  1030. c->y = wc.y = y;
  1031. c->w = wc.width = w;
  1032. c->h = wc.height = h;
  1033. wc.border_width = c->bw;
  1034. XConfigureWindow(dpy, c->win,
  1035. CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  1036. configure(c);
  1037. XSync(dpy, False);
  1038. }
  1039. }
  1040. void
  1041. resizemouse(Client *c) {
  1042. int ocx, ocy;
  1043. int nw, nh;
  1044. XEvent ev;
  1045. restack();
  1046. ocx = c->x;
  1047. ocy = c->y;
  1048. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1049. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  1050. return;
  1051. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1052. for(;;) {
  1053. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask , &ev);
  1054. switch(ev.type) {
  1055. case ButtonRelease:
  1056. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0,
  1057. c->w + c->bw - 1, c->h + c->bw - 1);
  1058. XUngrabPointer(dpy, CurrentTime);
  1059. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1060. return;
  1061. case ConfigureRequest:
  1062. case Expose:
  1063. case MapRequest:
  1064. handler[ev.type](&ev);
  1065. break;
  1066. case MotionNotify:
  1067. XSync(dpy, False);
  1068. nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
  1069. nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
  1070. if(snap && nw >= wx && nw <= wx + ww
  1071. && nh >= wy && nh <= wy + wh) {
  1072. if(!c->isfloating && lt->arrange
  1073. && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
  1074. togglefloating(NULL);
  1075. }
  1076. if(!lt->arrange || c->isfloating)
  1077. resize(c, c->x, c->y, nw, nh, True);
  1078. break;
  1079. }
  1080. }
  1081. }
  1082. void
  1083. restack(void) {
  1084. Client *c;
  1085. XEvent ev;
  1086. XWindowChanges wc;
  1087. drawbar();
  1088. if(!sel)
  1089. return;
  1090. if(ismax || sel->isfloating || !lt->arrange)
  1091. XRaiseWindow(dpy, sel->win);
  1092. if(!ismax && lt->arrange) {
  1093. wc.stack_mode = Below;
  1094. wc.sibling = barwin;
  1095. for(c = stack; c; c = c->snext)
  1096. if(!c->isfloating && !c->isbanned) {
  1097. XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
  1098. wc.sibling = c->win;
  1099. }
  1100. }
  1101. XSync(dpy, False);
  1102. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1103. }
  1104. void
  1105. run(void) {
  1106. char *p;
  1107. char sbuf[sizeof stext];
  1108. fd_set rd;
  1109. int r, xfd;
  1110. uint len, offset;
  1111. XEvent ev;
  1112. /* main event loop, also reads status text from stdin */
  1113. XSync(dpy, False);
  1114. xfd = ConnectionNumber(dpy);
  1115. readin = True;
  1116. offset = 0;
  1117. len = sizeof stext - 1;
  1118. sbuf[len] = stext[len] = '\0'; /* 0-terminator is never touched */
  1119. while(running) {
  1120. FD_ZERO(&rd);
  1121. if(readin)
  1122. FD_SET(STDIN_FILENO, &rd);
  1123. FD_SET(xfd, &rd);
  1124. if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
  1125. if(errno == EINTR)
  1126. continue;
  1127. eprint("select failed\n");
  1128. }
  1129. if(FD_ISSET(STDIN_FILENO, &rd)) {
  1130. switch((r = read(STDIN_FILENO, sbuf + offset, len - offset))) {
  1131. case -1:
  1132. strncpy(stext, strerror(errno), len);
  1133. readin = False;
  1134. break;
  1135. case 0:
  1136. strncpy(stext, "EOF", 4);
  1137. readin = False;
  1138. break;
  1139. default:
  1140. for(p = sbuf + offset; r > 0; p++, r--, offset++)
  1141. if(*p == '\n' || *p == '\0') {
  1142. *p = '\0';
  1143. strncpy(stext, sbuf, len);
  1144. p += r - 1; /* p is sbuf + offset + r - 1 */
  1145. for(r = 0; *(p - r) && *(p - r) != '\n'; r++);
  1146. offset = r;
  1147. if(r)
  1148. memmove(sbuf, p - r + 1, r);
  1149. break;
  1150. }
  1151. break;
  1152. }
  1153. drawbar();
  1154. }
  1155. while(XPending(dpy)) {
  1156. XNextEvent(dpy, &ev);
  1157. if(handler[ev.type])
  1158. (handler[ev.type])(&ev); /* call handler */
  1159. }
  1160. }
  1161. }
  1162. void
  1163. scan(void) {
  1164. uint i, num;
  1165. Window *wins, d1, d2;
  1166. XWindowAttributes wa;
  1167. wins = NULL;
  1168. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1169. for(i = 0; i < num; i++) {
  1170. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  1171. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1172. continue;
  1173. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1174. manage(wins[i], &wa);
  1175. }
  1176. for(i = 0; i < num; i++) { /* now the transients */
  1177. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1178. continue;
  1179. if(XGetTransientForHint(dpy, wins[i], &d1)
  1180. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1181. manage(wins[i], &wa);
  1182. }
  1183. }
  1184. if(wins)
  1185. XFree(wins);
  1186. }
  1187. void
  1188. setclientstate(Client *c, long state) {
  1189. long data[] = {state, None};
  1190. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1191. PropModeReplace, (unsigned char *)data, 2);
  1192. }
  1193. /* arg > 1.0 will set mfact absolutly */
  1194. void
  1195. setmfact(const void *arg) {
  1196. double d = *((double*) arg);
  1197. if(!d || !lt->arrange)
  1198. return;
  1199. d = d < 1.0 ? d + mfact : d - 1.0;
  1200. if(d < 0.1 || d > 0.9)
  1201. return;
  1202. mfact = d;
  1203. arrange();
  1204. }
  1205. void
  1206. setup(void) {
  1207. uint i, w;
  1208. XSetWindowAttributes wa;
  1209. /* init screen */
  1210. screen = DefaultScreen(dpy);
  1211. root = RootWindow(dpy, screen);
  1212. initfont(FONT);
  1213. sx = 0;
  1214. sy = 0;
  1215. sw = DisplayWidth(dpy, screen);
  1216. sh = DisplayHeight(dpy, screen);
  1217. bh = dc.font.height + 2;
  1218. updategeom();
  1219. /* init atoms */
  1220. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1221. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1222. wmatom[WMName] = XInternAtom(dpy, "WM_NAME", False);
  1223. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1224. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1225. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1226. /* init cursors */
  1227. wa.cursor = cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  1228. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  1229. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  1230. /* init appearance */
  1231. dc.norm[ColBorder] = getcolor(NORMBORDERCOLOR);
  1232. dc.norm[ColBG] = getcolor(NORMBGCOLOR);
  1233. dc.norm[ColFG] = getcolor(NORMFGCOLOR);
  1234. dc.sel[ColBorder] = getcolor(SELBORDERCOLOR);
  1235. dc.sel[ColBG] = getcolor(SELBGCOLOR);
  1236. dc.sel[ColFG] = getcolor(SELFGCOLOR);
  1237. initfont(FONT);
  1238. dc.h = bh;
  1239. dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
  1240. dc.gc = XCreateGC(dpy, root, 0, 0);
  1241. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1242. if(!dc.font.set)
  1243. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  1244. /* init bar */
  1245. for(blw = i = 0; LENGTH(layouts) > 1 && i < LENGTH(layouts); i++) {
  1246. w = TEXTW(layouts[i].symbol);
  1247. blw = MAX(blw, w);
  1248. }
  1249. wa.override_redirect = 1;
  1250. wa.background_pixmap = ParentRelative;
  1251. wa.event_mask = ButtonPressMask|ExposureMask;
  1252. barwin = XCreateWindow(dpy, root, wx, by, ww, bh, 0, DefaultDepth(dpy, screen),
  1253. CopyFromParent, DefaultVisual(dpy, screen),
  1254. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  1255. XDefineCursor(dpy, barwin, cursor[CurNormal]);
  1256. XMapRaised(dpy, barwin);
  1257. strcpy(stext, "dwm-"VERSION);
  1258. drawbar();
  1259. /* EWMH support per view */
  1260. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1261. PropModeReplace, (unsigned char *) netatom, NetLast);
  1262. /* select for events */
  1263. wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask
  1264. |EnterWindowMask|LeaveWindowMask|StructureNotifyMask;
  1265. XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
  1266. XSelectInput(dpy, root, wa.event_mask);
  1267. /* grab keys */
  1268. grabkeys();
  1269. }
  1270. void
  1271. spawn(const void *arg) {
  1272. static char *shell = NULL;
  1273. if(!shell && !(shell = getenv("SHELL")))
  1274. shell = "/bin/sh";
  1275. /* The double-fork construct avoids zombie processes and keeps the code
  1276. * clean from stupid signal handlers. */
  1277. if(fork() == 0) {
  1278. if(fork() == 0) {
  1279. if(dpy)
  1280. close(ConnectionNumber(dpy));
  1281. setsid();
  1282. execl(shell, shell, "-c", (char *)arg, (char *)NULL);
  1283. fprintf(stderr, "dwm: execl '%s -c %s'", shell, (char *)arg);
  1284. perror(" failed");
  1285. }
  1286. exit(0);
  1287. }
  1288. wait(0);
  1289. }
  1290. void
  1291. tag(const void *arg) {
  1292. if(sel && *(int *)arg & TAGMASK) {
  1293. sel->tags = *(int *)arg & TAGMASK;
  1294. arrange();
  1295. }
  1296. }
  1297. uint
  1298. textnw(const char *text, uint len) {
  1299. XRectangle r;
  1300. if(dc.font.set) {
  1301. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  1302. return r.width;
  1303. }
  1304. return XTextWidth(dc.font.xfont, text, len);
  1305. }
  1306. void
  1307. tile(void) {
  1308. int x, y, h, w, mw;
  1309. uint i, n;
  1310. Client *c;
  1311. for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next), n++);
  1312. if(n == 0)
  1313. return;
  1314. /* master */
  1315. c = nexttiled(clients);
  1316. mw = mfact * ww;
  1317. resize(c, wx, wy, ((n == 1) ? ww : mw) - 2 * c->bw, wh - 2 * c->bw, resizehints);
  1318. if(--n == 0)
  1319. return;
  1320. /* tile stack */
  1321. x = (wx + mw > c->x + c->w) ? c->x + c->w + 2 * c->bw : ww - mw;
  1322. y = wy;
  1323. w = (wx + mw > c->x + c->w) ? wx + ww - x : ww - mw;
  1324. h = wh / n;
  1325. if(h < bh)
  1326. h = wh;
  1327. for(i = 0, c = nexttiled(c->next); c; c = nexttiled(c->next), i++) {
  1328. resize(c, x, y, w - 2 * c->bw, /* remainder */ ((i + 1 == n)
  1329. ? (wy + wh) - y : h) - 2 * c->bw, resizehints);
  1330. if(h != wh)
  1331. y = c->y + c->h + 2 * c->bw;
  1332. }
  1333. }
  1334. void
  1335. togglebar(const void *arg) {
  1336. showbar = !showbar;
  1337. updategeom();
  1338. updatebar();
  1339. arrange();
  1340. }
  1341. void
  1342. togglefloating(const void *arg) {
  1343. if(!sel)
  1344. return;
  1345. sel->isfloating = !sel->isfloating || sel->isfixed;
  1346. if(sel->isfloating)
  1347. resize(sel, sel->x, sel->y, sel->w, sel->h, True);
  1348. arrange();
  1349. }
  1350. void
  1351. togglelayout(const void *arg) {
  1352. uint i;
  1353. if(!arg) {
  1354. if(++lt == &layouts[LENGTH(layouts)])
  1355. lt = &layouts[0];
  1356. }
  1357. else {
  1358. for(i = 0; i < LENGTH(layouts); i++)
  1359. if(!strcmp((char *)arg, layouts[i].symbol))
  1360. break;
  1361. if(i == LENGTH(layouts))
  1362. return;
  1363. lt = &layouts[i];
  1364. }
  1365. if(sel)
  1366. arrange();
  1367. else
  1368. drawbar();
  1369. }
  1370. void
  1371. togglemax(const void *arg) {
  1372. ismax = !ismax;
  1373. arrange();
  1374. }
  1375. void
  1376. toggletag(const void *arg) {
  1377. if(sel && (sel->tags ^ ((*(int *)arg) & TAGMASK))) {
  1378. sel->tags ^= (*(int *)arg) & TAGMASK;
  1379. arrange();
  1380. }
  1381. }
  1382. void
  1383. toggleview(const void *arg) {
  1384. if((tagset[seltags] ^ ((*(int *)arg) & TAGMASK))) {
  1385. tagset[seltags] ^= (*(int *)arg) & TAGMASK;
  1386. arrange();
  1387. }
  1388. }
  1389. void
  1390. unmanage(Client *c) {
  1391. XWindowChanges wc;
  1392. wc.border_width = c->oldbw;
  1393. /* The server grab construct avoids race conditions. */
  1394. XGrabServer(dpy);
  1395. XSetErrorHandler(xerrordummy);
  1396. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1397. detach(c);
  1398. detachstack(c);
  1399. if(sel == c)
  1400. focus(NULL);
  1401. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1402. setclientstate(c, WithdrawnState);
  1403. free(c);
  1404. XSync(dpy, False);
  1405. XSetErrorHandler(xerror);
  1406. XUngrabServer(dpy);
  1407. arrange();
  1408. }
  1409. void
  1410. unmapnotify(XEvent *e) {
  1411. Client *c;
  1412. XUnmapEvent *ev = &e->xunmap;
  1413. if((c = getclient(ev->window)))
  1414. unmanage(c);
  1415. }
  1416. void
  1417. updatebar(void) {
  1418. if(dc.drawable != 0)
  1419. XFreePixmap(dpy, dc.drawable);
  1420. dc.drawable = XCreatePixmap(dpy, root, ww, bh, DefaultDepth(dpy, screen));
  1421. XMoveResizeWindow(dpy, barwin, wx, by, ww, bh);
  1422. }
  1423. void
  1424. updategeom(void) {
  1425. int i;
  1426. #ifdef XINERAMA
  1427. XineramaScreenInfo *info = NULL;
  1428. /* window area geometry */
  1429. if(XineramaIsActive(dpy)) {
  1430. info = XineramaQueryScreens(dpy, &i);
  1431. wx = info[0].x_org;
  1432. wy = showbar && topbar ? info[0].y_org + bh : info[0].y_org;
  1433. ww = info[0].width;
  1434. wh = showbar ? info[0].height - bh : info[0].height;
  1435. XFree(info);
  1436. }
  1437. else
  1438. #endif
  1439. {
  1440. wx = sx;
  1441. wy = showbar && topbar ? sy + bh : sy;
  1442. ww = sw;
  1443. wh = showbar ? sh - bh : sh;
  1444. }
  1445. /* bar position */
  1446. by = showbar ? (topbar ? wy - bh : wy + wh) : -bh;
  1447. }
  1448. void
  1449. updatesizehints(Client *c) {
  1450. long msize;
  1451. XSizeHints size;
  1452. XGetWMNormalHints(dpy, c->win, &size, &msize);
  1453. if(size.flags & PBaseSize) {
  1454. c->basew = size.base_width;
  1455. c->baseh = size.base_height;
  1456. }
  1457. else if(size.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(size.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(size.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(size.flags & PMinSize) {
  1476. c->minw = size.min_width;
  1477. c->minh = size.min_height;
  1478. }
  1479. else if(size.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(size.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. seltags ^= 1; /* toggle sel tagset */
  1515. if(arg && (*(int *)arg & TAGMASK))
  1516. tagset[seltags] = *(int *)arg & TAGMASK;
  1517. arrange();
  1518. }
  1519. /* There's no way to check accesses to destroyed windows, thus those cases are
  1520. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1521. * default error handler, which may call exit. */
  1522. int
  1523. xerror(Display *dpy, XErrorEvent *ee) {
  1524. if(ee->error_code == BadWindow
  1525. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1526. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1527. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1528. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1529. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1530. || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
  1531. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1532. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1533. return 0;
  1534. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1535. ee->request_code, ee->error_code);
  1536. return xerrorxlib(dpy, ee); /* may call exit */
  1537. }
  1538. int
  1539. xerrordummy(Display *dpy, XErrorEvent *ee) {
  1540. return 0;
  1541. }
  1542. /* Startup Error handler to check if another window manager
  1543. * is already running. */
  1544. int
  1545. xerrorstart(Display *dpy, XErrorEvent *ee) {
  1546. otherwm = True;
  1547. return -1;
  1548. }
  1549. void
  1550. zoom(const void *arg) {
  1551. Client *c = sel;
  1552. if(ismax || !lt->arrange || (sel && sel->isfloating))
  1553. return;
  1554. if(c == nexttiled(clients))
  1555. if(!c || !(c = nexttiled(c->next)))
  1556. return;
  1557. detach(c);
  1558. attach(c);
  1559. focus(c);
  1560. arrange();
  1561. }
  1562. int
  1563. main(int argc, char *argv[]) {
  1564. if(argc == 2 && !strcmp("-v", argv[1]))
  1565. eprint("dwm-"VERSION", © 2006-2008 dwm engineers, see LICENSE for details\n");
  1566. else if(argc != 1)
  1567. eprint("usage: dwm [-v]\n");
  1568. setlocale(LC_CTYPE, "");
  1569. if(!(dpy = XOpenDisplay(0)))
  1570. eprint("dwm: cannot open display\n");
  1571. checkotherwm();
  1572. setup();
  1573. scan();
  1574. run();
  1575. cleanup();
  1576. XCloseDisplay(dpy);
  1577. return 0;
  1578. }