Configuration file for DWM on MacBook Air
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.

1739 lines
40 KiB

16 years ago
17 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 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. memcpy(&buf[MAX(0, len - 3)], "...", 3);
  536. XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
  537. if(dc.font.set)
  538. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  539. else
  540. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  541. }
  542. void
  543. enternotify(XEvent *e) {
  544. Client *c;
  545. XCrossingEvent *ev = &e->xcrossing;
  546. if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
  547. return;
  548. if((c = getclient(ev->window)))
  549. focus(c);
  550. else
  551. focus(NULL);
  552. }
  553. void
  554. eprint(const char *errstr, ...) {
  555. va_list ap;
  556. va_start(ap, errstr);
  557. vfprintf(stderr, errstr, ap);
  558. va_end(ap);
  559. exit(EXIT_FAILURE);
  560. }
  561. void
  562. expose(XEvent *e) {
  563. XExposeEvent *ev = &e->xexpose;
  564. if(ev->count == 0 && (ev->window == barwin))
  565. drawbar();
  566. }
  567. void
  568. focus(Client *c) {
  569. if(!c || (c && c->isbanned))
  570. for(c = stack; c && c->isbanned; c = c->snext);
  571. if(sel && sel != c) {
  572. grabbuttons(sel, False);
  573. XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
  574. }
  575. if(c) {
  576. detachstack(c);
  577. attachstack(c);
  578. grabbuttons(c, True);
  579. }
  580. sel = c;
  581. if(c) {
  582. XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
  583. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  584. }
  585. else
  586. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  587. drawbar();
  588. }
  589. void
  590. focusin(XEvent *e) { /* there are some broken focus acquiring clients */
  591. XFocusChangeEvent *ev = &e->xfocus;
  592. if(sel && ev->window != sel->win)
  593. XSetInputFocus(dpy, sel->win, RevertToPointerRoot, CurrentTime);
  594. }
  595. void
  596. focusnext(const void *arg) {
  597. Client *c;
  598. if(!sel)
  599. return;
  600. for(c = sel->next; c && c->isbanned; c = c->next);
  601. if(!c)
  602. for(c = clients; c && c->isbanned; c = c->next);
  603. if(c) {
  604. focus(c);
  605. restack();
  606. }
  607. }
  608. void
  609. focusprev(const void *arg) {
  610. Client *c;
  611. if(!sel)
  612. return;
  613. for(c = sel->prev; c && c->isbanned; c = c->prev);
  614. if(!c) {
  615. for(c = clients; c && c->next; c = c->next);
  616. for(; c && c->isbanned; c = c->prev);
  617. }
  618. if(c) {
  619. focus(c);
  620. restack();
  621. }
  622. }
  623. Client *
  624. getclient(Window w) {
  625. Client *c;
  626. for(c = clients; c && c->win != w; c = c->next);
  627. return c;
  628. }
  629. ulong
  630. getcolor(const char *colstr) {
  631. Colormap cmap = DefaultColormap(dpy, screen);
  632. XColor color;
  633. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  634. eprint("error, cannot allocate color '%s'\n", colstr);
  635. return color.pixel;
  636. }
  637. long
  638. getstate(Window w) {
  639. int format, status;
  640. long result = -1;
  641. unsigned char *p = NULL;
  642. ulong n, extra;
  643. Atom real;
  644. status = XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
  645. &real, &format, &n, &extra, (unsigned char **)&p);
  646. if(status != Success)
  647. return -1;
  648. if(n != 0)
  649. result = *p;
  650. XFree(p);
  651. return result;
  652. }
  653. Bool
  654. gettextprop(Window w, Atom atom, char *text, uint size) {
  655. char **list = NULL;
  656. int n;
  657. XTextProperty name;
  658. if(!text || size == 0)
  659. return False;
  660. text[0] = '\0';
  661. XGetTextProperty(dpy, w, &name, atom);
  662. if(!name.nitems)
  663. return False;
  664. if(name.encoding == XA_STRING)
  665. strncpy(text, (char *)name.value, size - 1);
  666. else {
  667. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  668. && n > 0 && *list) {
  669. strncpy(text, *list, size - 1);
  670. XFreeStringList(list);
  671. }
  672. }
  673. text[size - 1] = '\0';
  674. XFree(name.value);
  675. return True;
  676. }
  677. void
  678. grabbuttons(Client *c, Bool focused) {
  679. int i, j;
  680. uint buttons[] = { Button1, Button2, Button3 };
  681. uint modifiers[] = { MODKEY, MODKEY|LockMask, MODKEY|numlockmask,
  682. MODKEY|numlockmask|LockMask} ;
  683. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  684. if(focused)
  685. for(i = 0; i < LENGTH(buttons); i++)
  686. for(j = 0; j < LENGTH(modifiers); j++)
  687. XGrabButton(dpy, buttons[i], modifiers[j], c->win, False,
  688. BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
  689. else
  690. XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
  691. BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
  692. }
  693. void
  694. grabkeys(void) {
  695. uint i, j;
  696. KeyCode code;
  697. XModifierKeymap *modmap;
  698. /* init modifier map */
  699. modmap = XGetModifierMapping(dpy);
  700. for(i = 0; i < 8; i++)
  701. for(j = 0; j < modmap->max_keypermod; j++) {
  702. if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
  703. numlockmask = (1 << i);
  704. }
  705. XFreeModifiermap(modmap);
  706. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  707. for(i = 0; i < LENGTH(keys); i++) {
  708. code = XKeysymToKeycode(dpy, keys[i].keysym);
  709. XGrabKey(dpy, code, keys[i].mod, root, True,
  710. GrabModeAsync, GrabModeAsync);
  711. XGrabKey(dpy, code, keys[i].mod|LockMask, root, True,
  712. GrabModeAsync, GrabModeAsync);
  713. XGrabKey(dpy, code, keys[i].mod|numlockmask, root, True,
  714. GrabModeAsync, GrabModeAsync);
  715. XGrabKey(dpy, code, keys[i].mod|numlockmask|LockMask, root, True,
  716. GrabModeAsync, GrabModeAsync);
  717. }
  718. }
  719. void
  720. initfont(const char *fontstr) {
  721. char *def, **missing;
  722. int i, n;
  723. missing = NULL;
  724. if(dc.font.set)
  725. XFreeFontSet(dpy, dc.font.set);
  726. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  727. if(missing) {
  728. while(n--)
  729. fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
  730. XFreeStringList(missing);
  731. }
  732. if(dc.font.set) {
  733. XFontSetExtents *font_extents;
  734. XFontStruct **xfonts;
  735. char **font_names;
  736. dc.font.ascent = dc.font.descent = 0;
  737. font_extents = XExtentsOfFontSet(dc.font.set);
  738. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  739. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  740. dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
  741. dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent);
  742. xfonts++;
  743. }
  744. }
  745. else {
  746. if(dc.font.xfont)
  747. XFreeFont(dpy, dc.font.xfont);
  748. dc.font.xfont = NULL;
  749. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  750. && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  751. eprint("error, cannot load font: '%s'\n", fontstr);
  752. dc.font.ascent = dc.font.xfont->ascent;
  753. dc.font.descent = dc.font.xfont->descent;
  754. }
  755. dc.font.height = dc.font.ascent + dc.font.descent;
  756. }
  757. Bool
  758. isoccupied(uint t) {
  759. Client *c;
  760. for(c = clients; c; c = c->next)
  761. if(c->tags & 1 << t)
  762. return True;
  763. return False;
  764. }
  765. Bool
  766. isprotodel(Client *c) {
  767. int i, n;
  768. Atom *protocols;
  769. Bool ret = False;
  770. if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  771. for(i = 0; !ret && i < n; i++)
  772. if(protocols[i] == wmatom[WMDelete])
  773. ret = True;
  774. XFree(protocols);
  775. }
  776. return ret;
  777. }
  778. Bool
  779. isurgent(uint t) {
  780. Client *c;
  781. for(c = clients; c; c = c->next)
  782. if(c->isurgent && c->tags & 1 << t)
  783. return True;
  784. return False;
  785. }
  786. void
  787. keypress(XEvent *e) {
  788. uint i;
  789. KeySym keysym;
  790. XKeyEvent *ev;
  791. ev = &e->xkey;
  792. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  793. for(i = 0; i < LENGTH(keys); i++)
  794. if(keysym == keys[i].keysym
  795. && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
  796. && keys[i].func)
  797. keys[i].func(keys[i].arg);
  798. }
  799. void
  800. killclient(const void *arg) {
  801. XEvent ev;
  802. if(!sel)
  803. return;
  804. if(isprotodel(sel)) {
  805. ev.type = ClientMessage;
  806. ev.xclient.window = sel->win;
  807. ev.xclient.message_type = wmatom[WMProtocols];
  808. ev.xclient.format = 32;
  809. ev.xclient.data.l[0] = wmatom[WMDelete];
  810. ev.xclient.data.l[1] = CurrentTime;
  811. XSendEvent(dpy, sel->win, False, NoEventMask, &ev);
  812. }
  813. else
  814. XKillClient(dpy, sel->win);
  815. }
  816. void
  817. manage(Window w, XWindowAttributes *wa) {
  818. Client *c, *t = NULL;
  819. Status rettrans;
  820. Window trans;
  821. XWindowChanges wc;
  822. if(!(c = calloc(1, sizeof(Client))))
  823. eprint("fatal: could not calloc() %u bytes\n", sizeof(Client));
  824. c->win = w;
  825. /* geometry */
  826. c->x = wa->x;
  827. c->y = wa->y;
  828. c->w = wa->width;
  829. c->h = wa->height;
  830. c->oldbw = wa->border_width;
  831. if(c->w == sw && c->h == sh) {
  832. c->x = sx;
  833. c->y = sy;
  834. c->bw = wa->border_width;
  835. }
  836. else {
  837. if(c->x + c->w + 2 * c->bw > sx + sw)
  838. c->x = sx + sw - c->w - 2 * c->bw;
  839. if(c->y + c->h + 2 * c->bw > sy + sh)
  840. c->y = sy + sh - c->h - 2 * c->bw;
  841. c->x = MAX(c->x, sx);
  842. c->y = MAX(c->y, by == 0 ? bh : sy);
  843. c->bw = borderpx;
  844. }
  845. wc.border_width = c->bw;
  846. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  847. XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
  848. configure(c); /* propagates border_width, if size doesn't change */
  849. updatesizehints(c);
  850. XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
  851. grabbuttons(c, False);
  852. updatetitle(c);
  853. if((rettrans = XGetTransientForHint(dpy, w, &trans) == Success))
  854. for(t = clients; t && t->win != trans; t = t->next);
  855. if(t)
  856. c->tags = t->tags;
  857. else
  858. applyrules(c);
  859. if(!c->isfloating)
  860. c->isfloating = (rettrans == Success) || c->isfixed;
  861. if(c->isfloating)
  862. XRaiseWindow(dpy, c->win);
  863. attach(c);
  864. attachstack(c);
  865. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h); /* some windows require this */
  866. XMapWindow(dpy, c->win);
  867. setclientstate(c, NormalState);
  868. arrange();
  869. }
  870. void
  871. mappingnotify(XEvent *e) {
  872. XMappingEvent *ev = &e->xmapping;
  873. XRefreshKeyboardMapping(ev);
  874. if(ev->request == MappingKeyboard)
  875. grabkeys();
  876. }
  877. void
  878. maprequest(XEvent *e) {
  879. static XWindowAttributes wa;
  880. XMapRequestEvent *ev = &e->xmaprequest;
  881. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  882. return;
  883. if(wa.override_redirect)
  884. return;
  885. if(!getclient(ev->window))
  886. manage(ev->window, &wa);
  887. }
  888. void
  889. movemouse(Client *c) {
  890. int x1, y1, ocx, ocy, di, nx, ny;
  891. uint dui;
  892. Window dummy;
  893. XEvent ev;
  894. restack();
  895. ocx = nx = c->x;
  896. ocy = ny = c->y;
  897. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  898. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  899. return;
  900. XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
  901. for(;;) {
  902. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  903. switch (ev.type) {
  904. case ButtonRelease:
  905. XUngrabPointer(dpy, CurrentTime);
  906. return;
  907. case ConfigureRequest:
  908. case Expose:
  909. case MapRequest:
  910. handler[ev.type](&ev);
  911. break;
  912. case MotionNotify:
  913. XSync(dpy, False);
  914. nx = ocx + (ev.xmotion.x - x1);
  915. ny = ocy + (ev.xmotion.y - y1);
  916. if(snap && nx >= wx && nx <= wx + ww
  917. && ny >= wy && ny <= wy + wh) {
  918. if(abs(wx - nx) < snap)
  919. nx = wx;
  920. else if(abs((wx + ww) - (nx + c->w + 2 * c->bw)) < snap)
  921. nx = wx + ww - c->w - 2 * c->bw;
  922. if(abs(wy - ny) < snap)
  923. ny = wy;
  924. else if(abs((wy + wh) - (ny + c->h + 2 * c->bw)) < snap)
  925. ny = wy + wh - c->h - 2 * c->bw;
  926. if(!c->isfloating && lt->arrange && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
  927. togglefloating(NULL);
  928. }
  929. if(!lt->arrange || c->isfloating)
  930. resize(c, nx, ny, c->w, c->h, False);
  931. break;
  932. }
  933. }
  934. }
  935. Client *
  936. nexttiled(Client *c) {
  937. for(; c && (c->isfloating || c->isbanned); c = c->next);
  938. return c;
  939. }
  940. void
  941. propertynotify(XEvent *e) {
  942. Client *c;
  943. Window trans;
  944. XPropertyEvent *ev = &e->xproperty;
  945. if(ev->state == PropertyDelete)
  946. return; /* ignore */
  947. if((c = getclient(ev->window))) {
  948. switch (ev->atom) {
  949. default: break;
  950. case XA_WM_TRANSIENT_FOR:
  951. XGetTransientForHint(dpy, c->win, &trans);
  952. if(!c->isfloating && (c->isfloating = (getclient(trans) != NULL)))
  953. arrange();
  954. break;
  955. case XA_WM_NORMAL_HINTS:
  956. updatesizehints(c);
  957. break;
  958. case XA_WM_HINTS:
  959. updatewmhints(c);
  960. drawbar();
  961. break;
  962. }
  963. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  964. updatetitle(c);
  965. if(c == sel)
  966. drawbar();
  967. }
  968. }
  969. }
  970. void
  971. quit(const void *arg) {
  972. readin = running = False;
  973. }
  974. void
  975. resize(Client *c, int x, int y, int w, int h, Bool sizehints) {
  976. XWindowChanges wc;
  977. if(sizehints) {
  978. /* set minimum possible */
  979. w = MAX(1, w);
  980. h = MAX(1, h);
  981. /* temporarily remove base dimensions */
  982. w -= c->basew;
  983. h -= c->baseh;
  984. /* adjust for aspect limits */
  985. if(c->minax != c->maxax && c->minay != c->maxay
  986. && c->minax > 0 && c->maxax > 0 && c->minay > 0 && c->maxay > 0) {
  987. if(w * c->maxay > h * c->maxax)
  988. w = h * c->maxax / c->maxay;
  989. else if(w * c->minay < h * c->minax)
  990. h = w * c->minay / c->minax;
  991. }
  992. /* adjust for increment value */
  993. if(c->incw)
  994. w -= w % c->incw;
  995. if(c->inch)
  996. h -= h % c->inch;
  997. /* restore base dimensions */
  998. w += c->basew;
  999. h += c->baseh;
  1000. w = MAX(w, c->minw);
  1001. h = MAX(h, c->minh);
  1002. if (c->maxw)
  1003. w = MIN(w, c->maxw);
  1004. if (c->maxh)
  1005. h = MIN(h, c->maxh);
  1006. }
  1007. if(w <= 0 || h <= 0)
  1008. return;
  1009. if(x > sx + sw)
  1010. x = sw - w - 2 * c->bw;
  1011. if(y > sy + sh)
  1012. y = sh - h - 2 * c->bw;
  1013. if(x + w + 2 * c->bw < sx)
  1014. x = sx;
  1015. if(y + h + 2 * c->bw < sy)
  1016. y = sy;
  1017. if(h < bh)
  1018. h = bh;
  1019. if(w < bh)
  1020. w = bh;
  1021. if(c->x != x || c->y != y || c->w != w || c->h != h || c->ismoved) {
  1022. c->ismoved = False;
  1023. c->x = wc.x = x;
  1024. c->y = wc.y = y;
  1025. c->w = wc.width = w;
  1026. c->h = wc.height = h;
  1027. wc.border_width = c->bw;
  1028. XConfigureWindow(dpy, c->win,
  1029. CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  1030. configure(c);
  1031. XSync(dpy, False);
  1032. }
  1033. }
  1034. void
  1035. resizemouse(Client *c) {
  1036. int ocx, ocy;
  1037. int nw, nh;
  1038. XEvent ev;
  1039. restack();
  1040. ocx = c->x;
  1041. ocy = c->y;
  1042. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1043. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  1044. return;
  1045. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1046. for(;;) {
  1047. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask , &ev);
  1048. switch(ev.type) {
  1049. case ButtonRelease:
  1050. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0,
  1051. c->w + c->bw - 1, c->h + c->bw - 1);
  1052. XUngrabPointer(dpy, CurrentTime);
  1053. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1054. return;
  1055. case ConfigureRequest:
  1056. case Expose:
  1057. case MapRequest:
  1058. handler[ev.type](&ev);
  1059. break;
  1060. case MotionNotify:
  1061. XSync(dpy, False);
  1062. nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
  1063. nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
  1064. if(snap && nw >= wx && nw <= wx + ww
  1065. && nh >= wy && nh <= wy + wh) {
  1066. if(!c->isfloating && lt->arrange
  1067. && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
  1068. togglefloating(NULL);
  1069. }
  1070. if(!lt->arrange || c->isfloating)
  1071. resize(c, c->x, c->y, nw, nh, True);
  1072. break;
  1073. }
  1074. }
  1075. }
  1076. void
  1077. restack(void) {
  1078. Client *c;
  1079. XEvent ev;
  1080. XWindowChanges wc;
  1081. drawbar();
  1082. if(!sel)
  1083. return;
  1084. if(ismax || sel->isfloating || !lt->arrange)
  1085. XRaiseWindow(dpy, sel->win);
  1086. if(!ismax && lt->arrange) {
  1087. wc.stack_mode = Below;
  1088. wc.sibling = barwin;
  1089. for(c = stack; c; c = c->snext)
  1090. if(!c->isfloating && !c->isbanned) {
  1091. XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
  1092. wc.sibling = c->win;
  1093. }
  1094. }
  1095. XSync(dpy, False);
  1096. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1097. }
  1098. void
  1099. run(void) {
  1100. char *p;
  1101. char sbuf[sizeof stext];
  1102. fd_set rd;
  1103. int r, xfd;
  1104. uint len, offset;
  1105. XEvent ev;
  1106. /* main event loop, also reads status text from stdin */
  1107. XSync(dpy, False);
  1108. xfd = ConnectionNumber(dpy);
  1109. readin = True;
  1110. offset = 0;
  1111. len = sizeof stext - 1;
  1112. sbuf[len] = stext[len] = '\0'; /* 0-terminator is never touched */
  1113. while(running) {
  1114. FD_ZERO(&rd);
  1115. if(readin)
  1116. FD_SET(STDIN_FILENO, &rd);
  1117. FD_SET(xfd, &rd);
  1118. if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
  1119. if(errno == EINTR)
  1120. continue;
  1121. eprint("select failed\n");
  1122. }
  1123. if(FD_ISSET(STDIN_FILENO, &rd)) {
  1124. switch((r = read(STDIN_FILENO, sbuf + offset, len - offset))) {
  1125. case -1:
  1126. strncpy(stext, strerror(errno), len);
  1127. readin = False;
  1128. break;
  1129. case 0:
  1130. strncpy(stext, "EOF", 4);
  1131. readin = False;
  1132. break;
  1133. default:
  1134. for(p = sbuf + offset; r > 0; p++, r--, offset++)
  1135. if(*p == '\n' || *p == '\0') {
  1136. *p = '\0';
  1137. strncpy(stext, sbuf, len);
  1138. p += r - 1; /* p is sbuf + offset + r - 1 */
  1139. for(r = 0; *(p - r) && *(p - r) != '\n'; r++);
  1140. offset = r;
  1141. if(r)
  1142. memmove(sbuf, p - r + 1, r);
  1143. break;
  1144. }
  1145. break;
  1146. }
  1147. drawbar();
  1148. }
  1149. while(XPending(dpy)) {
  1150. XNextEvent(dpy, &ev);
  1151. if(handler[ev.type])
  1152. (handler[ev.type])(&ev); /* call handler */
  1153. }
  1154. }
  1155. }
  1156. void
  1157. scan(void) {
  1158. uint i, num;
  1159. Window *wins, d1, d2;
  1160. XWindowAttributes wa;
  1161. wins = NULL;
  1162. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1163. for(i = 0; i < num; i++) {
  1164. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  1165. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1166. continue;
  1167. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1168. manage(wins[i], &wa);
  1169. }
  1170. for(i = 0; i < num; i++) { /* now the transients */
  1171. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1172. continue;
  1173. if(XGetTransientForHint(dpy, wins[i], &d1)
  1174. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1175. manage(wins[i], &wa);
  1176. }
  1177. }
  1178. if(wins)
  1179. XFree(wins);
  1180. }
  1181. void
  1182. setclientstate(Client *c, long state) {
  1183. long data[] = {state, None};
  1184. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1185. PropModeReplace, (unsigned char *)data, 2);
  1186. }
  1187. /* arg > 1.0 will set mfact absolutly */
  1188. void
  1189. setmfact(const void *arg) {
  1190. double d = *((double*) arg);
  1191. if(!d || !lt->arrange)
  1192. return;
  1193. d = d < 1.0 ? d + mfact : d - 1.0;
  1194. if(d < 0.1 || d > 0.9)
  1195. return;
  1196. mfact = d;
  1197. arrange();
  1198. }
  1199. void
  1200. setup(void) {
  1201. uint i, w;
  1202. XSetWindowAttributes wa;
  1203. /* init screen */
  1204. screen = DefaultScreen(dpy);
  1205. root = RootWindow(dpy, screen);
  1206. initfont(FONT);
  1207. sx = 0;
  1208. sy = 0;
  1209. sw = DisplayWidth(dpy, screen);
  1210. sh = DisplayHeight(dpy, screen);
  1211. bh = dc.font.height + 2;
  1212. updategeom();
  1213. /* init atoms */
  1214. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1215. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1216. wmatom[WMName] = XInternAtom(dpy, "WM_NAME", False);
  1217. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1218. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1219. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1220. /* init cursors */
  1221. wa.cursor = cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  1222. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  1223. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  1224. /* init appearance */
  1225. dc.norm[ColBorder] = getcolor(NORMBORDERCOLOR);
  1226. dc.norm[ColBG] = getcolor(NORMBGCOLOR);
  1227. dc.norm[ColFG] = getcolor(NORMFGCOLOR);
  1228. dc.sel[ColBorder] = getcolor(SELBORDERCOLOR);
  1229. dc.sel[ColBG] = getcolor(SELBGCOLOR);
  1230. dc.sel[ColFG] = getcolor(SELFGCOLOR);
  1231. initfont(FONT);
  1232. dc.h = bh;
  1233. dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
  1234. dc.gc = XCreateGC(dpy, root, 0, 0);
  1235. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1236. if(!dc.font.set)
  1237. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  1238. /* init bar */
  1239. for(blw = i = 0; LENGTH(layouts) > 1 && i < LENGTH(layouts); i++) {
  1240. w = TEXTW(layouts[i].symbol);
  1241. blw = MAX(blw, w);
  1242. }
  1243. wa.override_redirect = 1;
  1244. wa.background_pixmap = ParentRelative;
  1245. wa.event_mask = ButtonPressMask|ExposureMask;
  1246. barwin = XCreateWindow(dpy, root, wx, by, ww, bh, 0, DefaultDepth(dpy, screen),
  1247. CopyFromParent, DefaultVisual(dpy, screen),
  1248. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  1249. XDefineCursor(dpy, barwin, cursor[CurNormal]);
  1250. XMapRaised(dpy, barwin);
  1251. strcpy(stext, "dwm-"VERSION);
  1252. drawbar();
  1253. /* EWMH support per view */
  1254. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1255. PropModeReplace, (unsigned char *) netatom, NetLast);
  1256. /* select for events */
  1257. wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask
  1258. |EnterWindowMask|LeaveWindowMask|StructureNotifyMask;
  1259. XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
  1260. XSelectInput(dpy, root, wa.event_mask);
  1261. /* grab keys */
  1262. grabkeys();
  1263. }
  1264. void
  1265. spawn(const void *arg) {
  1266. static char *shell = NULL;
  1267. if(!shell && !(shell = getenv("SHELL")))
  1268. shell = "/bin/sh";
  1269. /* The double-fork construct avoids zombie processes and keeps the code
  1270. * clean from stupid signal handlers. */
  1271. if(fork() == 0) {
  1272. if(fork() == 0) {
  1273. if(dpy)
  1274. close(ConnectionNumber(dpy));
  1275. setsid();
  1276. execl(shell, shell, "-c", (char *)arg, (char *)NULL);
  1277. fprintf(stderr, "dwm: execl '%s -c %s'", shell, (char *)arg);
  1278. perror(" failed");
  1279. }
  1280. exit(0);
  1281. }
  1282. wait(0);
  1283. }
  1284. void
  1285. tag(const void *arg) {
  1286. if(sel && *(int *)arg & TAGMASK) {
  1287. sel->tags = *(int *)arg & TAGMASK;
  1288. arrange();
  1289. }
  1290. }
  1291. uint
  1292. textnw(const char *text, uint len) {
  1293. XRectangle r;
  1294. if(dc.font.set) {
  1295. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  1296. return r.width;
  1297. }
  1298. return XTextWidth(dc.font.xfont, text, len);
  1299. }
  1300. void
  1301. tile(void) {
  1302. int x, y, h, w, mw;
  1303. uint i, n;
  1304. Client *c;
  1305. for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next), n++);
  1306. if(n == 0)
  1307. return;
  1308. /* master */
  1309. c = nexttiled(clients);
  1310. mw = mfact * ww;
  1311. resize(c, wx, wy, (n == 1 ? ww : mw) - 2 * c->bw, wh - 2 * c->bw, resizehints);
  1312. if(--n == 0)
  1313. return;
  1314. /* tile stack */
  1315. x = (wx + mw > c->x + c->w) ? c->x + c->w + 2 * c->bw : wx + mw;
  1316. y = wy;
  1317. w = (wx + mw > c->x + c->w) ? wx + ww - x : ww - mw;
  1318. h = wh / n;
  1319. if(h < bh)
  1320. h = wh;
  1321. for(i = 0, c = nexttiled(c->next); c; c = nexttiled(c->next), i++) {
  1322. resize(c, x, y, w - 2 * c->bw, /* remainder */ ((i + 1 == n)
  1323. ? (wy + wh) - y : h) - 2 * c->bw, resizehints);
  1324. if(h != wh)
  1325. y = c->y + c->h + 2 * c->bw;
  1326. }
  1327. }
  1328. void
  1329. togglebar(const void *arg) {
  1330. showbar = !showbar;
  1331. updategeom();
  1332. updatebar();
  1333. arrange();
  1334. }
  1335. void
  1336. togglefloating(const void *arg) {
  1337. if(!sel)
  1338. return;
  1339. sel->isfloating = !sel->isfloating || sel->isfixed;
  1340. if(sel->isfloating)
  1341. resize(sel, sel->x, sel->y, sel->w, sel->h, True);
  1342. arrange();
  1343. }
  1344. void
  1345. togglelayout(const void *arg) {
  1346. uint i;
  1347. if(!arg) {
  1348. if(++lt == &layouts[LENGTH(layouts)])
  1349. lt = &layouts[0];
  1350. }
  1351. else {
  1352. for(i = 0; i < LENGTH(layouts); i++)
  1353. if(!strcmp((char *)arg, layouts[i].symbol))
  1354. break;
  1355. if(i == LENGTH(layouts))
  1356. return;
  1357. lt = &layouts[i];
  1358. }
  1359. if(sel)
  1360. arrange();
  1361. else
  1362. drawbar();
  1363. }
  1364. void
  1365. togglemax(const void *arg) {
  1366. ismax = !ismax;
  1367. arrange();
  1368. }
  1369. void
  1370. toggletag(const void *arg) {
  1371. if(sel && (sel->tags ^ ((*(int *)arg) & TAGMASK))) {
  1372. sel->tags ^= (*(int *)arg) & TAGMASK;
  1373. arrange();
  1374. }
  1375. }
  1376. void
  1377. toggleview(const void *arg) {
  1378. if((tagset[seltags] ^ ((*(int *)arg) & TAGMASK))) {
  1379. tagset[seltags] ^= (*(int *)arg) & TAGMASK;
  1380. arrange();
  1381. }
  1382. }
  1383. void
  1384. unmanage(Client *c) {
  1385. XWindowChanges wc;
  1386. wc.border_width = c->oldbw;
  1387. /* The server grab construct avoids race conditions. */
  1388. XGrabServer(dpy);
  1389. XSetErrorHandler(xerrordummy);
  1390. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1391. detach(c);
  1392. detachstack(c);
  1393. if(sel == c)
  1394. focus(NULL);
  1395. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1396. setclientstate(c, WithdrawnState);
  1397. free(c);
  1398. XSync(dpy, False);
  1399. XSetErrorHandler(xerror);
  1400. XUngrabServer(dpy);
  1401. arrange();
  1402. }
  1403. void
  1404. unmapnotify(XEvent *e) {
  1405. Client *c;
  1406. XUnmapEvent *ev = &e->xunmap;
  1407. if((c = getclient(ev->window)))
  1408. unmanage(c);
  1409. }
  1410. void
  1411. updatebar(void) {
  1412. if(dc.drawable != 0)
  1413. XFreePixmap(dpy, dc.drawable);
  1414. dc.drawable = XCreatePixmap(dpy, root, ww, bh, DefaultDepth(dpy, screen));
  1415. XMoveResizeWindow(dpy, barwin, wx, by, ww, bh);
  1416. }
  1417. void
  1418. updategeom(void) {
  1419. int i;
  1420. #ifdef XINERAMA
  1421. XineramaScreenInfo *info = NULL;
  1422. /* window area geometry */
  1423. if(XineramaIsActive(dpy)) {
  1424. info = XineramaQueryScreens(dpy, &i);
  1425. wx = info[0].x_org;
  1426. wy = showbar && topbar ? info[0].y_org + bh : info[0].y_org;
  1427. ww = info[0].width;
  1428. wh = showbar ? info[0].height - bh : info[0].height;
  1429. XFree(info);
  1430. }
  1431. else
  1432. #endif
  1433. {
  1434. wx = sx;
  1435. wy = showbar && topbar ? sy + bh : sy;
  1436. ww = sw;
  1437. wh = showbar ? sh - bh : sh;
  1438. }
  1439. /* bar position */
  1440. by = showbar ? (topbar ? wy - bh : wy + wh) : -bh;
  1441. }
  1442. void
  1443. updatesizehints(Client *c) {
  1444. long msize;
  1445. XSizeHints size;
  1446. XGetWMNormalHints(dpy, c->win, &size, &msize);
  1447. if(size.flags & PBaseSize) {
  1448. c->basew = size.base_width;
  1449. c->baseh = size.base_height;
  1450. }
  1451. else if(size.flags & PMinSize) {
  1452. c->basew = size.min_width;
  1453. c->baseh = size.min_height;
  1454. }
  1455. else
  1456. c->basew = c->baseh = 0;
  1457. if(size.flags & PResizeInc) {
  1458. c->incw = size.width_inc;
  1459. c->inch = size.height_inc;
  1460. }
  1461. else
  1462. c->incw = c->inch = 0;
  1463. if(size.flags & PMaxSize) {
  1464. c->maxw = size.max_width;
  1465. c->maxh = size.max_height;
  1466. }
  1467. else
  1468. c->maxw = c->maxh = 0;
  1469. if(size.flags & PMinSize) {
  1470. c->minw = size.min_width;
  1471. c->minh = size.min_height;
  1472. }
  1473. else if(size.flags & PBaseSize) {
  1474. c->minw = size.base_width;
  1475. c->minh = size.base_height;
  1476. }
  1477. else
  1478. c->minw = c->minh = 0;
  1479. if(size.flags & PAspect) {
  1480. c->minax = size.min_aspect.x;
  1481. c->maxax = size.max_aspect.x;
  1482. c->minay = size.min_aspect.y;
  1483. c->maxay = size.max_aspect.y;
  1484. }
  1485. else
  1486. c->minax = c->maxax = c->minay = c->maxay = 0;
  1487. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1488. && c->maxw == c->minw && c->maxh == c->minh);
  1489. }
  1490. void
  1491. updatetitle(Client *c) {
  1492. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1493. gettextprop(c->win, wmatom[WMName], c->name, sizeof c->name);
  1494. }
  1495. void
  1496. updatewmhints(Client *c) {
  1497. XWMHints *wmh;
  1498. if((wmh = XGetWMHints(dpy, c->win))) {
  1499. if(c == sel)
  1500. sel->isurgent = False;
  1501. else
  1502. c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
  1503. XFree(wmh);
  1504. }
  1505. }
  1506. void
  1507. view(const void *arg) {
  1508. seltags ^= 1; /* toggle sel tagset */
  1509. if(arg && (*(int *)arg & TAGMASK))
  1510. tagset[seltags] = *(int *)arg & TAGMASK;
  1511. arrange();
  1512. }
  1513. /* There's no way to check accesses to destroyed windows, thus those cases are
  1514. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1515. * default error handler, which may call exit. */
  1516. int
  1517. xerror(Display *dpy, XErrorEvent *ee) {
  1518. if(ee->error_code == BadWindow
  1519. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1520. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1521. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1522. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1523. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1524. || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
  1525. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1526. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1527. return 0;
  1528. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1529. ee->request_code, ee->error_code);
  1530. return xerrorxlib(dpy, ee); /* may call exit */
  1531. }
  1532. int
  1533. xerrordummy(Display *dpy, XErrorEvent *ee) {
  1534. return 0;
  1535. }
  1536. /* Startup Error handler to check if another window manager
  1537. * is already running. */
  1538. int
  1539. xerrorstart(Display *dpy, XErrorEvent *ee) {
  1540. otherwm = True;
  1541. return -1;
  1542. }
  1543. void
  1544. zoom(const void *arg) {
  1545. Client *c = sel;
  1546. if(ismax || !lt->arrange || (sel && sel->isfloating))
  1547. return;
  1548. if(c == nexttiled(clients))
  1549. if(!c || !(c = nexttiled(c->next)))
  1550. return;
  1551. detach(c);
  1552. attach(c);
  1553. focus(c);
  1554. arrange();
  1555. }
  1556. int
  1557. main(int argc, char *argv[]) {
  1558. if(argc == 2 && !strcmp("-v", argv[1]))
  1559. eprint("dwm-"VERSION", © 2006-2008 dwm engineers, see LICENSE for details\n");
  1560. else if(argc != 1)
  1561. eprint("usage: dwm [-v]\n");
  1562. setlocale(LC_CTYPE, "");
  1563. if(!(dpy = XOpenDisplay(0)))
  1564. eprint("dwm: cannot open display\n");
  1565. checkotherwm();
  1566. setup();
  1567. scan();
  1568. run();
  1569. cleanup();
  1570. XCloseDisplay(dpy);
  1571. return 0;
  1572. }