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.

1721 lines
41 KiB

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