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.

1895 lines
44 KiB

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