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.

1840 lines
44 KiB

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