Configuration of dwm for Mac Computers
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.

1822 lines
41 KiB

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