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