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.

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