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.

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