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.

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