Configuration of dwm for Mac Computers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1718 lines
41 KiB

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