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.

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