Configuration file for DWM on MacBook Air
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.

1890 lines
43 KiB

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