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.

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