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.

1733 lines
42 KiB

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