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.

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