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.

1742 lines
42 KiB

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