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.

1899 lines
44 KiB

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