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.

1738 lines
42 KiB

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