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.

1891 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. if(ISTILE && !c->isfloating)
  606. restack();
  607. }
  608. else if(ev->window == root) {
  609. selscreen = True;
  610. focus(NULL);
  611. }
  612. }
  613. void
  614. eprint(const char *errstr, ...) {
  615. va_list ap;
  616. va_start(ap, errstr);
  617. vfprintf(stderr, errstr, ap);
  618. va_end(ap);
  619. exit(EXIT_FAILURE);
  620. }
  621. void
  622. expose(XEvent *e) {
  623. XExposeEvent *ev = &e->xexpose;
  624. if(ev->count == 0) {
  625. if(barwin == ev->window)
  626. drawbar();
  627. }
  628. }
  629. void
  630. floating(void) { /* default floating layout */
  631. Client *c;
  632. for(c = clients; c; c = c->next)
  633. if(isvisible(c))
  634. resize(c, c->x, c->y, c->w, c->h, True);
  635. }
  636. void
  637. focus(Client *c) {
  638. if((!c && selscreen) || (c && !isvisible(c)))
  639. for(c = stack; c && !isvisible(c); c = c->snext);
  640. if(sel && sel != c) {
  641. grabbuttons(sel, False);
  642. XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
  643. }
  644. if(c) {
  645. detachstack(c);
  646. attachstack(c);
  647. grabbuttons(c, True);
  648. }
  649. sel = c;
  650. drawbar();
  651. if(!selscreen)
  652. return;
  653. if(c) {
  654. XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
  655. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  656. }
  657. else
  658. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  659. }
  660. void
  661. focusnext(const char *arg) {
  662. Client *c;
  663. if(!sel)
  664. return;
  665. for(c = sel->next; c && !isvisible(c); c = c->next);
  666. if(!c)
  667. for(c = clients; c && !isvisible(c); c = c->next);
  668. if(c) {
  669. focus(c);
  670. restack();
  671. }
  672. }
  673. void
  674. focusprev(const char *arg) {
  675. Client *c;
  676. if(!sel)
  677. return;
  678. for(c = sel->prev; c && !isvisible(c); c = c->prev);
  679. if(!c) {
  680. for(c = clients; c && c->next; c = c->next);
  681. for(; c && !isvisible(c); c = c->prev);
  682. }
  683. if(c) {
  684. focus(c);
  685. restack();
  686. }
  687. }
  688. Client *
  689. getclient(Window w) {
  690. Client *c;
  691. for(c = clients; c && c->win != w; c = c->next);
  692. return c;
  693. }
  694. unsigned long
  695. getcolor(const char *colstr) {
  696. Colormap cmap = DefaultColormap(dpy, screen);
  697. XColor color;
  698. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  699. eprint("error, cannot allocate color '%s'\n", colstr);
  700. return color.pixel;
  701. }
  702. long
  703. getstate(Window w) {
  704. int format, status;
  705. long result = -1;
  706. unsigned char *p = NULL;
  707. unsigned long n, extra;
  708. Atom real;
  709. status = XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
  710. &real, &format, &n, &extra, (unsigned char **)&p);
  711. if(status != Success)
  712. return -1;
  713. if(n != 0)
  714. result = *p;
  715. XFree(p);
  716. return result;
  717. }
  718. Bool
  719. gettextprop(Window w, Atom atom, char *text, unsigned int size) {
  720. char **list = NULL;
  721. int n;
  722. XTextProperty name;
  723. if(!text || size == 0)
  724. return False;
  725. text[0] = '\0';
  726. XGetTextProperty(dpy, w, &name, atom);
  727. if(!name.nitems)
  728. return False;
  729. if(name.encoding == XA_STRING)
  730. strncpy(text, (char *)name.value, size - 1);
  731. else {
  732. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  733. && n > 0 && *list)
  734. {
  735. strncpy(text, *list, size - 1);
  736. XFreeStringList(list);
  737. }
  738. }
  739. text[size - 1] = '\0';
  740. XFree(name.value);
  741. return True;
  742. }
  743. void
  744. grabbuttons(Client *c, Bool focused) {
  745. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  746. if(focused) {
  747. XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
  748. GrabModeAsync, GrabModeSync, None, None);
  749. XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
  750. GrabModeAsync, GrabModeSync, None, None);
  751. XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
  752. GrabModeAsync, GrabModeSync, None, None);
  753. XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
  754. GrabModeAsync, GrabModeSync, None, None);
  755. XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
  756. GrabModeAsync, GrabModeSync, None, None);
  757. XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
  758. GrabModeAsync, GrabModeSync, None, None);
  759. XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
  760. GrabModeAsync, GrabModeSync, None, None);
  761. XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
  762. GrabModeAsync, GrabModeSync, None, None);
  763. XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
  764. GrabModeAsync, GrabModeSync, None, None);
  765. XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
  766. GrabModeAsync, GrabModeSync, None, None);
  767. XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
  768. GrabModeAsync, GrabModeSync, None, None);
  769. XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
  770. GrabModeAsync, GrabModeSync, None, None);
  771. }
  772. else
  773. XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
  774. GrabModeAsync, GrabModeSync, None, None);
  775. }
  776. unsigned int
  777. idxoftag(const char *tag) {
  778. unsigned int i;
  779. for(i = 0; i < ntags; i++)
  780. if(tags[i] == tag)
  781. return i;
  782. return 0;
  783. }
  784. void
  785. initfont(const char *fontstr) {
  786. char *def, **missing;
  787. int i, n;
  788. missing = NULL;
  789. if(dc.font.set)
  790. XFreeFontSet(dpy, dc.font.set);
  791. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  792. if(missing) {
  793. while(n--)
  794. fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
  795. XFreeStringList(missing);
  796. }
  797. if(dc.font.set) {
  798. XFontSetExtents *font_extents;
  799. XFontStruct **xfonts;
  800. char **font_names;
  801. dc.font.ascent = dc.font.descent = 0;
  802. font_extents = XExtentsOfFontSet(dc.font.set);
  803. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  804. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  805. if(dc.font.ascent < (*xfonts)->ascent)
  806. dc.font.ascent = (*xfonts)->ascent;
  807. if(dc.font.descent < (*xfonts)->descent)
  808. dc.font.descent = (*xfonts)->descent;
  809. xfonts++;
  810. }
  811. }
  812. else {
  813. if(dc.font.xfont)
  814. XFreeFont(dpy, dc.font.xfont);
  815. dc.font.xfont = NULL;
  816. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  817. && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  818. eprint("error, cannot load font: '%s'\n", fontstr);
  819. dc.font.ascent = dc.font.xfont->ascent;
  820. dc.font.descent = dc.font.xfont->descent;
  821. }
  822. dc.font.height = dc.font.ascent + dc.font.descent;
  823. }
  824. Bool
  825. isarrange(void (*func)())
  826. {
  827. return func == layouts[ltidx].arrange;
  828. }
  829. Bool
  830. isoccupied(unsigned int t) {
  831. Client *c;
  832. for(c = clients; c; c = c->next)
  833. if(c->tags[t])
  834. return True;
  835. return False;
  836. }
  837. Bool
  838. isprotodel(Client *c) {
  839. int i, n;
  840. Atom *protocols;
  841. Bool ret = False;
  842. if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  843. for(i = 0; !ret && i < n; i++)
  844. if(protocols[i] == wmatom[WMDelete])
  845. ret = True;
  846. XFree(protocols);
  847. }
  848. return ret;
  849. }
  850. Bool
  851. isvisible(Client *c) {
  852. unsigned int i;
  853. for(i = 0; i < ntags; i++)
  854. if(c->tags[i] && seltags[i])
  855. return True;
  856. return False;
  857. }
  858. void
  859. keypress(XEvent *e) {
  860. KEYS
  861. unsigned int len = sizeof keys / sizeof keys[0];
  862. unsigned int i;
  863. KeyCode code;
  864. KeySym keysym;
  865. XKeyEvent *ev;
  866. if(!e) { /* grabkeys */
  867. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  868. for(i = 0; i < len; i++) {
  869. code = XKeysymToKeycode(dpy, keys[i].keysym);
  870. XGrabKey(dpy, code, keys[i].mod, root, True,
  871. GrabModeAsync, GrabModeAsync);
  872. XGrabKey(dpy, code, keys[i].mod | LockMask, root, True,
  873. GrabModeAsync, GrabModeAsync);
  874. XGrabKey(dpy, code, keys[i].mod | numlockmask, root, True,
  875. GrabModeAsync, GrabModeAsync);
  876. XGrabKey(dpy, code, keys[i].mod | numlockmask | LockMask, root, True,
  877. GrabModeAsync, GrabModeAsync);
  878. }
  879. return;
  880. }
  881. ev = &e->xkey;
  882. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  883. for(i = 0; i < len; i++)
  884. if(keysym == keys[i].keysym
  885. && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state))
  886. {
  887. if(keys[i].func)
  888. keys[i].func(keys[i].arg);
  889. }
  890. }
  891. void
  892. killclient(const char *arg) {
  893. XEvent ev;
  894. if(!sel)
  895. return;
  896. if(isprotodel(sel)) {
  897. ev.type = ClientMessage;
  898. ev.xclient.window = sel->win;
  899. ev.xclient.message_type = wmatom[WMProtocols];
  900. ev.xclient.format = 32;
  901. ev.xclient.data.l[0] = wmatom[WMDelete];
  902. ev.xclient.data.l[1] = CurrentTime;
  903. XSendEvent(dpy, sel->win, False, NoEventMask, &ev);
  904. }
  905. else
  906. XKillClient(dpy, sel->win);
  907. }
  908. void
  909. leavenotify(XEvent *e) {
  910. XCrossingEvent *ev = &e->xcrossing;
  911. if((ev->window == root) && !ev->same_screen) {
  912. selscreen = False;
  913. focus(NULL);
  914. }
  915. }
  916. void
  917. manage(Window w, XWindowAttributes *wa) {
  918. unsigned int i;
  919. Client *c, *t = NULL;
  920. Window trans;
  921. Status rettrans;
  922. XWindowChanges wc;
  923. c = emallocz(sizeof(Client));
  924. c->tags = emallocz(ntags * sizeof(Bool));
  925. c->win = w;
  926. c->x = wa->x;
  927. c->y = wa->y;
  928. c->w = wa->width;
  929. c->h = wa->height;
  930. c->oldborder = wa->border_width;
  931. if(c->w == sw && c->h == sh) {
  932. c->x = sx;
  933. c->y = sy;
  934. c->border = wa->border_width;
  935. }
  936. else {
  937. if(c->x + c->w + 2 * c->border > wax + waw)
  938. c->x = wax + waw - c->w - 2 * c->border;
  939. if(c->y + c->h + 2 * c->border > way + wah)
  940. c->y = way + wah - c->h - 2 * c->border;
  941. if(c->x < wax)
  942. c->x = wax;
  943. if(c->y < way)
  944. c->y = way;
  945. c->border = BORDERPX;
  946. }
  947. wc.border_width = c->border;
  948. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  949. XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
  950. configure(c); /* propagates border_width, if size doesn't change */
  951. updatesizehints(c);
  952. XSelectInput(dpy, w,
  953. StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
  954. grabbuttons(c, False);
  955. updatetitle(c);
  956. if((rettrans = XGetTransientForHint(dpy, w, &trans) == Success))
  957. for(t = clients; t && t->win != trans; t = t->next);
  958. if(t)
  959. for(i = 0; i < ntags; i++)
  960. c->tags[i] = t->tags[i];
  961. applyrules(c);
  962. if(!c->isfloating)
  963. c->isfloating = (rettrans == Success) || c->isfixed;
  964. attach(c);
  965. attachstack(c);
  966. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h); /* some windows require this */
  967. ban(c);
  968. XMapWindow(dpy, c->win);
  969. setclientstate(c, NormalState);
  970. arrange();
  971. }
  972. void
  973. mappingnotify(XEvent *e) {
  974. XMappingEvent *ev = &e->xmapping;
  975. XRefreshKeyboardMapping(ev);
  976. if(ev->request == MappingKeyboard)
  977. keypress(NULL);
  978. }
  979. void
  980. maprequest(XEvent *e) {
  981. static XWindowAttributes wa;
  982. XMapRequestEvent *ev = &e->xmaprequest;
  983. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  984. return;
  985. if(wa.override_redirect)
  986. return;
  987. if(!getclient(ev->window))
  988. manage(ev->window, &wa);
  989. }
  990. void
  991. movemouse(Client *c) {
  992. int x1, y1, ocx, ocy, di, nx, ny;
  993. unsigned int dui;
  994. Window dummy;
  995. XEvent ev;
  996. ocx = nx = c->x;
  997. ocy = ny = c->y;
  998. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  999. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  1000. return;
  1001. c->ismax = False;
  1002. XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
  1003. for(;;) {
  1004. XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask, &ev);
  1005. switch (ev.type) {
  1006. case ButtonRelease:
  1007. XUngrabPointer(dpy, CurrentTime);
  1008. return;
  1009. case ConfigureRequest:
  1010. case Expose:
  1011. case MapRequest:
  1012. handler[ev.type](&ev);
  1013. break;
  1014. case MotionNotify:
  1015. XSync(dpy, False);
  1016. nx = ocx + (ev.xmotion.x - x1);
  1017. ny = ocy + (ev.xmotion.y - y1);
  1018. if(abs(wax + nx) < SNAP)
  1019. nx = wax;
  1020. else if(abs((wax + waw) - (nx + c->w + 2 * c->border)) < SNAP)
  1021. nx = wax + waw - c->w - 2 * c->border;
  1022. if(abs(way - ny) < SNAP)
  1023. ny = way;
  1024. else if(abs((way + wah) - (ny + c->h + 2 * c->border)) < SNAP)
  1025. ny = way + wah - c->h - 2 * c->border;
  1026. resize(c, nx, ny, c->w, c->h, False);
  1027. break;
  1028. }
  1029. }
  1030. }
  1031. Client *
  1032. nexttiled(Client *c) {
  1033. for(; c && (c->isfloating || !isvisible(c)); c = c->next);
  1034. return c;
  1035. }
  1036. void
  1037. propertynotify(XEvent *e) {
  1038. Client *c;
  1039. Window trans;
  1040. XPropertyEvent *ev = &e->xproperty;
  1041. if(ev->state == PropertyDelete)
  1042. return; /* ignore */
  1043. if((c = getclient(ev->window))) {
  1044. switch (ev->atom) {
  1045. default: break;
  1046. case XA_WM_TRANSIENT_FOR:
  1047. XGetTransientForHint(dpy, c->win, &trans);
  1048. if(!c->isfloating && (c->isfloating = (getclient(trans) != NULL)))
  1049. arrange();
  1050. break;
  1051. case XA_WM_NORMAL_HINTS:
  1052. updatesizehints(c);
  1053. break;
  1054. }
  1055. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  1056. updatetitle(c);
  1057. if(c == sel)
  1058. drawbar();
  1059. }
  1060. }
  1061. }
  1062. void
  1063. quit(const char *arg) {
  1064. readin = running = False;
  1065. }
  1066. void
  1067. resize(Client *c, int x, int y, int w, int h, Bool sizehints) {
  1068. double dx, dy, max, min, ratio;
  1069. XWindowChanges wc;
  1070. if(sizehints) {
  1071. if(c->minay > 0 && c->maxay > 0 && (h - c->baseh) > 0 && (w - c->basew) > 0) {
  1072. dx = (double)(w - c->basew);
  1073. dy = (double)(h - c->baseh);
  1074. min = (double)(c->minax) / (double)(c->minay);
  1075. max = (double)(c->maxax) / (double)(c->maxay);
  1076. ratio = dx / dy;
  1077. if(max > 0 && min > 0 && ratio > 0) {
  1078. if(ratio < min) {
  1079. dy = (dx * min + dy) / (min * min + 1);
  1080. dx = dy * min;
  1081. w = (int)dx + c->basew;
  1082. h = (int)dy + c->baseh;
  1083. }
  1084. else if(ratio > max) {
  1085. dy = (dx * min + dy) / (max * max + 1);
  1086. dx = dy * min;
  1087. w = (int)dx + c->basew;
  1088. h = (int)dy + c->baseh;
  1089. }
  1090. }
  1091. }
  1092. if(c->minw && w < c->minw)
  1093. w = c->minw;
  1094. if(c->minh && h < c->minh)
  1095. h = c->minh;
  1096. if(c->maxw && w > c->maxw)
  1097. w = c->maxw;
  1098. if(c->maxh && h > c->maxh)
  1099. h = c->maxh;
  1100. if(c->incw)
  1101. w -= (w - c->basew) % c->incw;
  1102. if(c->inch)
  1103. h -= (h - c->baseh) % c->inch;
  1104. }
  1105. if(w <= 0 || h <= 0)
  1106. return;
  1107. /* offscreen appearance fixes */
  1108. if(x > sw)
  1109. x = sw - w - 2 * c->border;
  1110. if(y > sh)
  1111. y = sh - h - 2 * c->border;
  1112. if(x + w + 2 * c->border < sx)
  1113. x = sx;
  1114. if(y + h + 2 * c->border < sy)
  1115. y = sy;
  1116. if(c->x != x || c->y != y || c->w != w || c->h != h) {
  1117. c->x = wc.x = x;
  1118. c->y = wc.y = y;
  1119. c->w = wc.width = w;
  1120. c->h = wc.height = h;
  1121. wc.border_width = c->border;
  1122. XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
  1123. configure(c);
  1124. XSync(dpy, False);
  1125. }
  1126. }
  1127. void
  1128. resizemouse(Client *c) {
  1129. int ocx, ocy;
  1130. int nw, nh;
  1131. XEvent ev;
  1132. ocx = c->x;
  1133. ocy = c->y;
  1134. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1135. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  1136. return;
  1137. c->ismax = False;
  1138. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->border - 1, c->h + c->border - 1);
  1139. for(;;) {
  1140. XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask , &ev);
  1141. switch(ev.type) {
  1142. case ButtonRelease:
  1143. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0,
  1144. c->w + c->border - 1, c->h + c->border - 1);
  1145. XUngrabPointer(dpy, CurrentTime);
  1146. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1147. return;
  1148. case ConfigureRequest:
  1149. case Expose:
  1150. case MapRequest:
  1151. handler[ev.type](&ev);
  1152. break;
  1153. case MotionNotify:
  1154. XSync(dpy, False);
  1155. if((nw = ev.xmotion.x - ocx - 2 * c->border + 1) <= 0)
  1156. nw = 1;
  1157. if((nh = ev.xmotion.y - ocy - 2 * c->border + 1) <= 0)
  1158. nh = 1;
  1159. resize(c, c->x, c->y, nw, nh, True);
  1160. break;
  1161. }
  1162. }
  1163. }
  1164. void
  1165. restack(void) {
  1166. Client *c;
  1167. XEvent ev;
  1168. XWindowChanges wc;
  1169. drawbar();
  1170. if(!sel)
  1171. return;
  1172. if(sel->isfloating || isarrange(floating))
  1173. XRaiseWindow(dpy, sel->win);
  1174. if(!isarrange(floating)) {
  1175. wc.stack_mode = Below;
  1176. wc.sibling = barwin;
  1177. if(!sel->isfloating) {
  1178. XConfigureWindow(dpy, sel->win, CWSibling | CWStackMode, &wc);
  1179. wc.sibling = sel->win;
  1180. }
  1181. for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
  1182. if(c == sel)
  1183. continue;
  1184. XConfigureWindow(dpy, c->win, CWSibling | CWStackMode, &wc);
  1185. wc.sibling = c->win;
  1186. }
  1187. }
  1188. XSync(dpy, False);
  1189. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1190. }
  1191. void
  1192. run(void) {
  1193. char *p;
  1194. int r, xfd;
  1195. fd_set rd;
  1196. XEvent ev;
  1197. /* main event loop, also reads status text from stdin */
  1198. XSync(dpy, False);
  1199. xfd = ConnectionNumber(dpy);
  1200. readin = True;
  1201. while(running) {
  1202. FD_ZERO(&rd);
  1203. if(readin)
  1204. FD_SET(STDIN_FILENO, &rd);
  1205. FD_SET(xfd, &rd);
  1206. if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
  1207. if(errno == EINTR)
  1208. continue;
  1209. eprint("select failed\n");
  1210. }
  1211. if(FD_ISSET(STDIN_FILENO, &rd)) {
  1212. switch(r = read(STDIN_FILENO, stext, sizeof stext - 1)) {
  1213. case -1:
  1214. strncpy(stext, strerror(errno), sizeof stext - 1);
  1215. stext[sizeof stext - 1] = '\0';
  1216. readin = False;
  1217. break;
  1218. case 0:
  1219. strncpy(stext, "EOF", 4);
  1220. readin = False;
  1221. break;
  1222. default:
  1223. for(stext[r] = '\0', p = stext + strlen(stext) - 1; p >= stext && *p == '\n'; *p-- = '\0');
  1224. for(; p >= stext && *p != '\n'; --p);
  1225. if(p > stext)
  1226. strncpy(stext, p + 1, sizeof stext);
  1227. }
  1228. drawbar();
  1229. }
  1230. while(XPending(dpy)) {
  1231. XNextEvent(dpy, &ev);
  1232. if(handler[ev.type])
  1233. (handler[ev.type])(&ev); /* call handler */
  1234. }
  1235. }
  1236. }
  1237. void
  1238. scan(void) {
  1239. unsigned int i, num;
  1240. Window *wins, d1, d2;
  1241. XWindowAttributes wa;
  1242. wins = NULL;
  1243. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1244. for(i = 0; i < num; i++) {
  1245. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  1246. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1247. continue;
  1248. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1249. manage(wins[i], &wa);
  1250. }
  1251. for(i = 0; i < num; i++) { /* now the transients */
  1252. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1253. continue;
  1254. if(XGetTransientForHint(dpy, wins[i], &d1)
  1255. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1256. manage(wins[i], &wa);
  1257. }
  1258. }
  1259. if(wins)
  1260. XFree(wins);
  1261. }
  1262. void
  1263. setclientstate(Client *c, long state) {
  1264. long data[] = {state, None};
  1265. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1266. PropModeReplace, (unsigned char *)data, 2);
  1267. }
  1268. void
  1269. setlayout(const char *arg) {
  1270. unsigned int i;
  1271. if(!arg) {
  1272. if(++ltidx == nlayouts)
  1273. ltidx = 0;;
  1274. }
  1275. else {
  1276. for(i = 0; i < nlayouts; i++)
  1277. if(!strcmp(arg, layouts[i].symbol))
  1278. break;
  1279. if(i == nlayouts)
  1280. return;
  1281. ltidx = i;
  1282. }
  1283. if(sel)
  1284. arrange();
  1285. else
  1286. drawbar();
  1287. }
  1288. void
  1289. setmwfact(const char *arg) {
  1290. double delta;
  1291. if(!ISTILE)
  1292. return;
  1293. /* arg handling, manipulate mwfact */
  1294. if(arg == NULL)
  1295. mwfact = MWFACT;
  1296. else if(1 == sscanf(arg, "%lf", &delta)) {
  1297. if(arg[0] == '+' || arg[0] == '-')
  1298. mwfact += delta;
  1299. else
  1300. mwfact = delta;
  1301. if(mwfact < 0.1)
  1302. mwfact = 0.1;
  1303. else if(mwfact > 0.9)
  1304. mwfact = 0.9;
  1305. }
  1306. arrange();
  1307. }
  1308. void
  1309. setup(void) {
  1310. int d;
  1311. unsigned int i, j, mask;
  1312. Window w;
  1313. XModifierKeymap *modmap;
  1314. XSetWindowAttributes wa;
  1315. /* init atoms */
  1316. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1317. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1318. wmatom[WMName] = XInternAtom(dpy, "WM_NAME", False);
  1319. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1320. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1321. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1322. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1323. PropModeReplace, (unsigned char *) netatom, NetLast);
  1324. /* init cursors */
  1325. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  1326. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  1327. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  1328. /* init geometry */
  1329. sx = sy = 0;
  1330. sw = DisplayWidth(dpy, screen);
  1331. sh = DisplayHeight(dpy, screen);
  1332. /* init modifier map */
  1333. modmap = XGetModifierMapping(dpy);
  1334. for(i = 0; i < 8; i++)
  1335. for(j = 0; j < modmap->max_keypermod; j++) {
  1336. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  1337. == XKeysymToKeycode(dpy, XK_Num_Lock))
  1338. numlockmask = (1 << i);
  1339. }
  1340. XFreeModifiermap(modmap);
  1341. /* select for events */
  1342. wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
  1343. | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
  1344. wa.cursor = cursor[CurNormal];
  1345. XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
  1346. XSelectInput(dpy, root, wa.event_mask);
  1347. /* grab keys */
  1348. keypress(NULL);
  1349. /* init tags */
  1350. compileregs();
  1351. for(ntags = 0; tags[ntags]; ntags++);
  1352. seltags = emallocz(sizeof(Bool) * ntags);
  1353. seltags[0] = True;
  1354. /* init appearance */
  1355. dc.norm[ColBorder] = getcolor(NORMBORDERCOLOR);
  1356. dc.norm[ColBG] = getcolor(NORMBGCOLOR);
  1357. dc.norm[ColFG] = getcolor(NORMFGCOLOR);
  1358. dc.sel[ColBorder] = getcolor(SELBORDERCOLOR);
  1359. dc.sel[ColBG] = getcolor(SELBGCOLOR);
  1360. dc.sel[ColFG] = getcolor(SELFGCOLOR);
  1361. initfont(FONT);
  1362. dc.h = bh = dc.font.height + 2;
  1363. /* init layouts */
  1364. mwfact = MWFACT;
  1365. nlayouts = sizeof layouts / sizeof layouts[0];
  1366. for(blw = i = 0; i < nlayouts; i++) {
  1367. j = textw(layouts[i].symbol);
  1368. if(j > blw)
  1369. blw = j;
  1370. }
  1371. /* init bar */
  1372. bpos = BARPOS;
  1373. wa.override_redirect = 1;
  1374. wa.background_pixmap = ParentRelative;
  1375. wa.event_mask = ButtonPressMask | ExposureMask;
  1376. barwin = XCreateWindow(dpy, root, sx, sy, sw, bh, 0,
  1377. DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
  1378. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  1379. XDefineCursor(dpy, barwin, cursor[CurNormal]);
  1380. updatebarpos();
  1381. XMapRaised(dpy, barwin);
  1382. strcpy(stext, "dwm-"VERSION);
  1383. dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
  1384. dc.gc = XCreateGC(dpy, root, 0, 0);
  1385. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1386. if(!dc.font.set)
  1387. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  1388. /* multihead support */
  1389. selscreen = XQueryPointer(dpy, root, &w, &w, &d, &d, &d, &d, &mask);
  1390. }
  1391. void
  1392. spawn(const char *arg) {
  1393. static char *shell = NULL;
  1394. if(!shell && !(shell = getenv("SHELL")))
  1395. shell = "/bin/sh";
  1396. if(!arg)
  1397. return;
  1398. /* The double-fork construct avoids zombie processes and keeps the code
  1399. * clean from stupid signal handlers. */
  1400. if(fork() == 0) {
  1401. if(fork() == 0) {
  1402. if(dpy)
  1403. close(ConnectionNumber(dpy));
  1404. setsid();
  1405. execl(shell, shell, "-c", arg, (char *)NULL);
  1406. fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg);
  1407. perror(" failed");
  1408. }
  1409. exit(0);
  1410. }
  1411. wait(0);
  1412. }
  1413. void
  1414. tag(const char *arg) {
  1415. unsigned int i;
  1416. if(!sel)
  1417. return;
  1418. for(i = 0; i < ntags; i++)
  1419. sel->tags[i] = arg == NULL;
  1420. i = idxoftag(arg);
  1421. if(i >= 0 && i < ntags)
  1422. sel->tags[i] = True;
  1423. arrange();
  1424. }
  1425. unsigned int
  1426. textnw(const char *text, unsigned int len) {
  1427. XRectangle r;
  1428. if(dc.font.set) {
  1429. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  1430. return r.width;
  1431. }
  1432. return XTextWidth(dc.font.xfont, text, len);
  1433. }
  1434. unsigned int
  1435. textw(const char *text) {
  1436. return textnw(text, strlen(text)) + dc.font.height;
  1437. }
  1438. void
  1439. tile(void) {
  1440. unsigned int i, n, nx, ny, nw, nh, mw, th;
  1441. Client *c, *mc;
  1442. for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
  1443. n++;
  1444. /* window geoms */
  1445. mw = (n == 1) ? waw : mwfact * waw;
  1446. th = (n > 1) ? wah / (n - 1) : 0;
  1447. if(n > 1 && th < bh)
  1448. th = wah;
  1449. nx = wax;
  1450. ny = way;
  1451. nw = 0; /* gcc stupidity requires this */
  1452. for(i = 0, c = mc = nexttiled(clients); c; c = nexttiled(c->next), i++) {
  1453. c->ismax = False;
  1454. if(i == 0) { /* master */
  1455. nw = mw - 2 * c->border;
  1456. nh = wah - 2 * c->border;
  1457. }
  1458. else { /* tile window */
  1459. if(i == 1) {
  1460. ny = way;
  1461. nx += mc->w + mc->border;
  1462. nw = waw - nx - 2 * c->border;
  1463. }
  1464. if(i + 1 == n) /* remainder */
  1465. nh = (way + wah) - ny - 2 * c->border;
  1466. else
  1467. nh = th - 2 * c->border;
  1468. }
  1469. resize(c, nx, ny, nw, nh, RESIZEHINTS);
  1470. if(n > 1 && th != wah)
  1471. ny = c->y + c->h + c->border;
  1472. }
  1473. }
  1474. void
  1475. togglebar(const char *arg) {
  1476. if(bpos == BarOff)
  1477. bpos = (BARPOS == BarOff) ? BarTop : BARPOS;
  1478. else
  1479. bpos = BarOff;
  1480. updatebarpos();
  1481. arrange();
  1482. }
  1483. void
  1484. togglefloating(const char *arg) {
  1485. if(!sel)
  1486. return;
  1487. sel->isfloating = !sel->isfloating;
  1488. if(sel->isfloating)
  1489. resize(sel, sel->x, sel->y, sel->w, sel->h, True);
  1490. arrange();
  1491. }
  1492. void
  1493. togglemax(const char *arg) {
  1494. XEvent ev;
  1495. if(!sel || sel->isfixed)
  1496. return;
  1497. if((sel->ismax = !sel->ismax)) {
  1498. if(isarrange(floating) || sel->isfloating)
  1499. sel->wasfloating = True;
  1500. else {
  1501. togglefloating(NULL);
  1502. sel->wasfloating = False;
  1503. }
  1504. sel->rx = sel->x;
  1505. sel->ry = sel->y;
  1506. sel->rw = sel->w;
  1507. sel->rh = sel->h;
  1508. resize(sel, wax, way, waw - 2 * sel->border, wah - 2 * sel->border, True);
  1509. }
  1510. else {
  1511. resize(sel, sel->rx, sel->ry, sel->rw, sel->rh, True);
  1512. if(!sel->wasfloating)
  1513. togglefloating(NULL);
  1514. }
  1515. drawbar();
  1516. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1517. }
  1518. void
  1519. toggletag(const char *arg) {
  1520. unsigned int i, j;
  1521. if(!sel)
  1522. return;
  1523. i = idxoftag(arg);
  1524. sel->tags[i] = !sel->tags[i];
  1525. for(j = 0; j < ntags && !sel->tags[j]; j++);
  1526. if(j == ntags)
  1527. sel->tags[i] = True;
  1528. arrange();
  1529. }
  1530. void
  1531. toggleview(const char *arg) {
  1532. unsigned int i, j;
  1533. i = idxoftag(arg);
  1534. seltags[i] = !seltags[i];
  1535. for(j = 0; j < ntags && !seltags[j]; j++);
  1536. if(j == ntags)
  1537. seltags[i] = True; /* at least one tag must be viewed */
  1538. arrange();
  1539. }
  1540. void
  1541. unban(Client *c) {
  1542. if(!c->isbanned)
  1543. return;
  1544. XMoveWindow(dpy, c->win, c->x, c->y);
  1545. c->isbanned = False;
  1546. }
  1547. void
  1548. unmanage(Client *c) {
  1549. XWindowChanges wc;
  1550. wc.border_width = c->oldborder;
  1551. /* The server grab construct avoids race conditions. */
  1552. XGrabServer(dpy);
  1553. XSetErrorHandler(xerrordummy);
  1554. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1555. detach(c);
  1556. detachstack(c);
  1557. if(sel == c)
  1558. focus(NULL);
  1559. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1560. setclientstate(c, WithdrawnState);
  1561. free(c->tags);
  1562. free(c);
  1563. XSync(dpy, False);
  1564. XSetErrorHandler(xerror);
  1565. XUngrabServer(dpy);
  1566. arrange();
  1567. }
  1568. void
  1569. unmapnotify(XEvent *e) {
  1570. Client *c;
  1571. XUnmapEvent *ev = &e->xunmap;
  1572. if((c = getclient(ev->window)))
  1573. unmanage(c);
  1574. }
  1575. void
  1576. updatebarpos(void) {
  1577. XEvent ev;
  1578. wax = sx;
  1579. way = sy;
  1580. wah = sh;
  1581. waw = sw;
  1582. switch(bpos) {
  1583. default:
  1584. wah -= bh;
  1585. way += bh;
  1586. XMoveWindow(dpy, barwin, sx, sy);
  1587. break;
  1588. case BarBot:
  1589. wah -= bh;
  1590. XMoveWindow(dpy, barwin, sx, sy + wah);
  1591. break;
  1592. case BarOff:
  1593. XMoveWindow(dpy, barwin, sx, sy - bh);
  1594. break;
  1595. }
  1596. XSync(dpy, False);
  1597. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1598. }
  1599. void
  1600. updatesizehints(Client *c) {
  1601. long msize;
  1602. XSizeHints size;
  1603. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  1604. size.flags = PSize;
  1605. c->flags = size.flags;
  1606. if(c->flags & PBaseSize) {
  1607. c->basew = size.base_width;
  1608. c->baseh = size.base_height;
  1609. }
  1610. else if(c->flags & PMinSize) {
  1611. c->basew = size.min_width;
  1612. c->baseh = size.min_height;
  1613. }
  1614. else
  1615. c->basew = c->baseh = 0;
  1616. if(c->flags & PResizeInc) {
  1617. c->incw = size.width_inc;
  1618. c->inch = size.height_inc;
  1619. }
  1620. else
  1621. c->incw = c->inch = 0;
  1622. if(c->flags & PMaxSize) {
  1623. c->maxw = size.max_width;
  1624. c->maxh = size.max_height;
  1625. }
  1626. else
  1627. c->maxw = c->maxh = 0;
  1628. if(c->flags & PMinSize) {
  1629. c->minw = size.min_width;
  1630. c->minh = size.min_height;
  1631. }
  1632. else if(c->flags & PBaseSize) {
  1633. c->minw = size.base_width;
  1634. c->minh = size.base_height;
  1635. }
  1636. else
  1637. c->minw = c->minh = 0;
  1638. if(c->flags & PAspect) {
  1639. c->minax = size.min_aspect.x;
  1640. c->maxax = size.max_aspect.x;
  1641. c->minay = size.min_aspect.y;
  1642. c->maxay = size.max_aspect.y;
  1643. }
  1644. else
  1645. c->minax = c->maxax = c->minay = c->maxay = 0;
  1646. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1647. && c->maxw == c->minw && c->maxh == c->minh);
  1648. }
  1649. void
  1650. updatetitle(Client *c) {
  1651. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1652. gettextprop(c->win, wmatom[WMName], c->name, sizeof c->name);
  1653. }
  1654. /* There's no way to check accesses to destroyed windows, thus those cases are
  1655. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1656. * default error handler, which may call exit. */
  1657. int
  1658. xerror(Display *dpy, XErrorEvent *ee) {
  1659. if(ee->error_code == BadWindow
  1660. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1661. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1662. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1663. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1664. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1665. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1666. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1667. return 0;
  1668. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1669. ee->request_code, ee->error_code);
  1670. return xerrorxlib(dpy, ee); /* may call exit */
  1671. }
  1672. int
  1673. xerrordummy(Display *dsply, XErrorEvent *ee) {
  1674. return 0;
  1675. }
  1676. /* Startup Error handler to check if another window manager
  1677. * is already running. */
  1678. int
  1679. xerrorstart(Display *dsply, XErrorEvent *ee) {
  1680. otherwm = True;
  1681. return -1;
  1682. }
  1683. void
  1684. view(const char *arg) {
  1685. unsigned int i;
  1686. for(i = 0; i < ntags; i++)
  1687. seltags[i] = arg == NULL;
  1688. i = idxoftag(arg);
  1689. if(i >= 0 && i < ntags)
  1690. seltags[i] = True;
  1691. arrange();
  1692. }
  1693. void
  1694. zoom(const char *arg) {
  1695. Client *c;
  1696. if(!sel || !ISTILE || sel->isfloating)
  1697. return;
  1698. if((c = sel) == nexttiled(clients))
  1699. if(!(c = nexttiled(c->next)))
  1700. return;
  1701. detach(c);
  1702. attach(c);
  1703. focus(c);
  1704. arrange();
  1705. }
  1706. int
  1707. main(int argc, char *argv[]) {
  1708. if(argc == 2 && !strcmp("-v", argv[1]))
  1709. eprint("dwm-"VERSION", © 2006-2007 A. R. Garbe, S. van Dijk, J. Salmi, P. Hruby, S. Nagy\n");
  1710. else if(argc != 1)
  1711. eprint("usage: dwm [-v]\n");
  1712. setlocale(LC_CTYPE, "");
  1713. if(!(dpy = XOpenDisplay(0)))
  1714. eprint("dwm: cannot open display\n");
  1715. screen = DefaultScreen(dpy);
  1716. root = RootWindow(dpy, screen);
  1717. checkotherwm();
  1718. setup();
  1719. drawbar();
  1720. scan();
  1721. run();
  1722. cleanup();
  1723. XCloseDisplay(dpy);
  1724. return 0;
  1725. }