Advertisement
Guest User

sashi

a guest
Dec 31st, 2010
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 61.75 KB | None | 0 0
  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. * The event handlers of dwm are organized in an array which is accessed
  10. * whenever a new event has been fetched. This allows event dispatching
  11. * in O(1) time.
  12. *
  13. * Each child of the root window is called a client, except windows which have
  14. * set the override_redirect flag. Clients are organized in a linked client
  15. * list on each monitor, the focus history is remembered through a stack list
  16. * on each monitor. Each client contains a bit array to indicate the tags of a
  17. * client.
  18. *
  19. * Keys and tagging rules are organized as arrays and defined in config.h.
  20. *
  21. * To understand everything else, start reading main().
  22. */
  23. #include <errno.h>
  24. #include <locale.h>
  25. #include <stdarg.h>
  26. #include <signal.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <unistd.h>
  31. #include <sys/types.h>
  32. #include <sys/wait.h>
  33. #include <X11/cursorfont.h>
  34. #include <X11/keysym.h>
  35. #include <X11/Xatom.h>
  36. #include <X11/Xlib.h>
  37. #include <X11/Xproto.h>
  38. #include <X11/Xutil.h>
  39. #ifdef XINERAMA
  40. #include <X11/extensions/Xinerama.h>
  41. #endif /* XINERAMA */
  42.  
  43. /* macros */
  44. #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
  45. #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask))
  46. #define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH))
  47. #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
  48. #define LENGTH(X) (sizeof X / sizeof X[0])
  49. #define MAX(A, B) ((A) > (B) ? (A) : (B))
  50. #define MIN(A, B) ((A) < (B) ? (A) : (B))
  51. #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
  52. #define WIDTH(X) ((X)->w + 2 * (X)->bw)
  53. #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
  54. #define TAGMASK ((1 << LENGTH(tags)) - 1)
  55. #define TEXTW(X) (textnw(X, strlen(X)) + dc.font.height)
  56.  
  57. /* enums */
  58. enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
  59. enum { ColBorder, ColFG, ColBG, ColLast }; /* color */
  60. enum { NetSupported, NetWMName, NetWMState,
  61. NetWMFullscreen, NetWMWindowOpacity, NetLast }; /* EWMH atoms */
  62. enum { WMProtocols, WMDelete, WMState, WMLast }; /* default atoms */
  63. enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
  64. ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
  65. enum { AttAsFirst, AttAsLast, AttAbove, AttBelow, /* Attach modes */
  66. AttAside };
  67. enum { ColNorm, ColSel, ColUrg, ColErr, ColDelim, /* more colors */
  68. ColHot, ColMed, ColCool, NumColors };
  69.  
  70. typedef union {
  71. int i;
  72. unsigned int ui;
  73. float f;
  74. const void *v;
  75. } Arg;
  76.  
  77. typedef struct {
  78. unsigned int click;
  79. unsigned int mask;
  80. unsigned int button;
  81. void (*func)(const Arg *arg);
  82. const Arg arg;
  83. } Button;
  84.  
  85. typedef struct Monitor Monitor;
  86. typedef struct Client Client;
  87. struct Client {
  88. char name[256];
  89. float mina, maxa;
  90. int x, y, w, h;
  91. int oldx, oldy, oldw, oldh;
  92. int basew, baseh, incw, inch, maxw, maxh, minw, minh;
  93. int bw, oldbw;
  94. unsigned int tags;
  95. Bool isfixed, isfloating, isurgent, oldstate, needresize;
  96. Client *next;
  97. Client *snext;
  98. Monitor *mon;
  99. Window win;
  100. double opacity;
  101. };
  102.  
  103. typedef struct {
  104. int x, y, w, h;
  105. unsigned long norm[ColLast];
  106. unsigned long sel[ColLast];
  107. Drawable drawable;
  108. GC gc;
  109. struct {
  110. int ascent;
  111. int descent;
  112. int height;
  113. XFontSet set;
  114. XFontStruct *xfont;
  115. } font;
  116. } DC; /* draw context */
  117.  
  118. typedef struct {
  119. unsigned int mod;
  120. KeySym keysym;
  121. void (*func)(const Arg *);
  122. const Arg arg;
  123. } Key;
  124.  
  125. typedef struct {
  126. const char *symbol;
  127. Bool addgaps;
  128. void (*arrange)(Monitor *);
  129. } Layout;
  130.  
  131. struct Monitor {
  132. char ltsymbol[16];
  133. float mfact;
  134. int num;
  135. int by; /* bar geometry */
  136. int mx, my, mw, mh; /* screen size */
  137. int wx, wy, ww, wh; /* window area */
  138. unsigned int seltags;
  139. unsigned int sellt;
  140. unsigned int tagset[2];
  141. Bool showbar;
  142. Bool topbar;
  143. Client *clients;
  144. Client *sel;
  145. Client *stack;
  146. Monitor *next;
  147. Window barwin;
  148. const Layout *lt[2];
  149. int curtag;
  150. int prevtag;
  151. int titlebarbegin;
  152. int titlebarend;
  153. const Layout **lts;
  154. double *mfacts;
  155. Bool *showbars;
  156. Bool *topbars;
  157. int *attachmodes;
  158. int *nmasters;
  159. Client **lastfocus;
  160. };
  161.  
  162. typedef struct {
  163. const char *name;
  164. const Layout *layout;
  165. float mfact;
  166. int showbar;
  167. int topbar;
  168. int attachmode;
  169. int nmaster;
  170. } Tag;
  171.  
  172. typedef struct {
  173. const char *class;
  174. const char *instance;
  175. const char *title;
  176. unsigned int tags;
  177. Bool isfloating;
  178. int monitor;
  179. double opacity;
  180. } Rule;
  181.  
  182. /* function declarations */
  183. static void applyrules(Client *c);
  184. static Bool applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact);
  185. static void arrange(Monitor *m);
  186. static void arrangemon(Monitor *m);
  187. static void attachasfirst(Client *c);
  188. static void attachaslast(Client *c);
  189. static void attachabove(Client *c);
  190. static void attachbelow(Client *c);
  191. static void attachaside(Client *c);
  192. static void attach(Client *c);
  193. static void setattachmode(const Arg *arg);
  194. static void attachstack(Client *c);
  195. static void buttonpress(XEvent *e);
  196. static void checkotherwm(void);
  197. static void cleanup(void);
  198. static void cleanupmon(Monitor *mon);
  199. static void clearurgent(Client *c);
  200. static void clientmessage(XEvent *e);
  201. static void configure(Client *c);
  202. static void configurenotify(XEvent *e);
  203. static void configurerequest(XEvent *e);
  204. static Monitor *createmon(void);
  205. static void destroynotify(XEvent *e);
  206. static void detach(Client *c);
  207. static void detachstack(Client *c);
  208. static void die(const char *errstr, ...);
  209. static Monitor *dirtomon(int dir);
  210. static void drawbar(Monitor *m);
  211. static void drawbars(void);
  212. static void drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]);
  213. static void drawtext(const char *text, unsigned long col[ColLast], Bool invert, Bool pad);
  214. static void drawvline(unsigned long col[ColLast]);
  215. static void drawcoloredtext(Monitor *m, char *text);
  216. static void enternotify(XEvent *e);
  217. static void expose(XEvent *e);
  218. static void window_opacity_set(Client *c, double opacity);
  219. static void focus(Client *c);
  220. static void focusonclick(const Arg *arg);
  221. static void focusin(XEvent *e);
  222. static void focusmon(const Arg *arg);
  223. static void focusstack(const Arg *arg);
  224. static void focustoggle(const Arg *arg);
  225. static unsigned long getcolor(const char *colstr);
  226. static Bool getrootptr(int *x, int *y);
  227. static long getstate(Window w);
  228. static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
  229. static void grabbuttons(Client *c, Bool focused);
  230. static void grabkeys(void);
  231. static void initfont(const char *fontstr);
  232. static Bool isprotodel(Client *c);
  233. static void keypress(XEvent *e);
  234. static void killclient(const Arg *arg);
  235. static void manage(Window w, XWindowAttributes *wa);
  236. static void mappingnotify(XEvent *e);
  237. static void maprequest(XEvent *e);
  238. static void monocle(Monitor *m);
  239. static void movemouse(const Arg *arg);
  240. static Client *nexttiled(Client *c);
  241. static Monitor *ptrtomon(int x, int y);
  242. static void propertynotify(XEvent *e);
  243. static void quit(const Arg *arg);
  244. static void resize(Client *c, int x, int y, int w, int h, Bool interact);
  245. static void resizeclient(Client *c, int x, int y, int w, int h);
  246. static void resizemouse(const Arg *arg);
  247. static void restack(Monitor *m);
  248. static void run(void);
  249. static void scan(void);
  250. static void sendmon(Client *c, Monitor *m);
  251. static void setclientstate(Client *c, long state);
  252. static void setlayout(const Arg *arg);
  253. static void setmfact(const Arg *arg);
  254. static void setnmaster(const Arg *arg);
  255. static void incnmaster(const Arg *arg);
  256. static void setup(void);
  257. static void showhide(Client *c);
  258. static void sigchld(int unused);
  259. static void spawn(const Arg *arg);
  260. static void tag(const Arg *arg);
  261. static void tagmon(const Arg *arg);
  262. static int textnw(const char *text, unsigned int len);
  263. static void ntile(Monitor *);
  264. static void togglebar(const Arg *arg);
  265. static void togglefloating(const Arg *arg);
  266. static void toggletag(const Arg *arg);
  267. static void toggleview(const Arg *arg);
  268. static void unfocus(Client *c, Bool setfocus);
  269. static void unmanage(Client *c, Bool destroyed);
  270. static void unmapnotify(XEvent *e);
  271. static Bool updategeom(void);
  272. static void updatebarpos(Monitor *m);
  273. static void updatebars(void);
  274. static void updatenumlockmask(void);
  275. static void updatesizehints(Client *c);
  276. static void updatestatus(void);
  277. static void updatetitle(Client *c);
  278. static void updatewmhints(Client *c);
  279. static void view(const Arg *arg);
  280. static Client *wintoclient(Window w);
  281. static Monitor *wintomon(Window w);
  282. static int xerror(Display *dpy, XErrorEvent *ee);
  283. static int xerrordummy(Display *dpy, XErrorEvent *ee);
  284. static int xerrorstart(Display *dpy, XErrorEvent *ee);
  285. static void zoom(const Arg *arg);
  286.  
  287. /* variables */
  288. static const char broken[] = "broken";
  289. static char stext[256];
  290. static int screen;
  291. static int sw, sh; /* X display screen geometry width, height */
  292. static int bh, blw = 0; /* bar geometry */
  293. static int (*xerrorxlib)(Display *, XErrorEvent *);
  294. static unsigned int numlockmask = 0;
  295. static void (*handler[LASTEvent]) (XEvent *) = {
  296. [ButtonPress] = buttonpress,
  297. [ClientMessage] = clientmessage,
  298. [ConfigureRequest] = configurerequest,
  299. [ConfigureNotify] = configurenotify,
  300. [DestroyNotify] = destroynotify,
  301. [EnterNotify] = enternotify,
  302. [Expose] = expose,
  303. [FocusIn] = focusin,
  304. [KeyPress] = keypress,
  305. [MappingNotify] = mappingnotify,
  306. [MapRequest] = maprequest,
  307. [PropertyNotify] = propertynotify,
  308. [UnmapNotify] = unmapnotify
  309. };
  310. static Atom wmatom[WMLast], netatom[NetLast];
  311. static Bool otherwm;
  312. static Bool running = True;
  313. static int exitcode = 0;
  314. static Cursor cursor[CurLast];
  315. static Display *dpy;
  316. static DC dc;
  317. static Monitor *mons = NULL, *selmon = NULL;
  318. static Window root;
  319. unsigned long barcolors[NumColors][ColLast];
  320.  
  321. /* configuration, allows nested code to access above variables */
  322. #include "config.h"
  323.  
  324. /* compile-time check if all tags fit into an unsigned int bit array. */
  325. struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
  326.  
  327. /* function implementations */
  328. void
  329. applyrules(Client *c) {
  330. const char *class, *instance;
  331. unsigned int i;
  332. const Rule *r;
  333. Monitor *m;
  334. XClassHint ch = { 0 };
  335.  
  336. /* rule matching */
  337. c->isfloating = c->tags = 0;
  338. if(XGetClassHint(dpy, c->win, &ch)) {
  339. class = ch.res_class ? ch.res_class : broken;
  340. instance = ch.res_name ? ch.res_name : broken;
  341. for(i = 0; i < LENGTH(rules); i++) {
  342. r = &rules[i];
  343. if((!r->title || strstr(c->name, r->title))
  344. && (!r->class || strstr(class, r->class))
  345. && (!r->instance || strstr(instance, r->instance)))
  346. {
  347. c->isfloating = r->isfloating;
  348. c->opacity = r->opacity;
  349. c->tags |= r->tags;
  350. for(m = mons; m && m->num != r->monitor; m = m->next);
  351. if(m)
  352. c->mon = m;
  353. }
  354. }
  355. if(ch.res_class)
  356. XFree(ch.res_class);
  357. if(ch.res_name)
  358. XFree(ch.res_name);
  359. }
  360. c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
  361. }
  362.  
  363. Bool
  364. applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact) {
  365. Bool baseismin;
  366. Monitor *m = c->mon;
  367.  
  368. /* set minimum possible */
  369. *w = MAX(1, *w);
  370. *h = MAX(1, *h);
  371. if(interact) {
  372. if(*x > sw)
  373. *x = sw - WIDTH(c);
  374. if(*y > sh)
  375. *y = sh - HEIGHT(c);
  376. if(*x + *w + 2 * c->bw < 0)
  377. *x = 0;
  378. if(*y + *h + 2 * c->bw < 0)
  379. *y = 0;
  380. }
  381. else {
  382. if(*x > m->mx + m->mw)
  383. *x = m->mx + m->mw - WIDTH(c);
  384. if(*y > m->my + m->mh)
  385. *y = m->my + m->mh - HEIGHT(c);
  386. if(*x + *w + 2 * c->bw < m->mx)
  387. *x = m->mx;
  388. if(*y + *h + 2 * c->bw < m->my)
  389. *y = m->my;
  390. }
  391. if(*h < bh)
  392. *h = bh;
  393. if(*w < bh)
  394. *w = bh;
  395. if(resizehints || c->isfloating) {
  396. /* see last two sentences in ICCCM 4.1.2.3 */
  397. baseismin = c->basew == c->minw && c->baseh == c->minh;
  398. if(!baseismin) { /* temporarily remove base dimensions */
  399. *w -= c->basew;
  400. *h -= c->baseh;
  401. }
  402. /* adjust for aspect limits */
  403. if(c->mina > 0 && c->maxa > 0) {
  404. if(c->maxa < (float)*w / *h)
  405. *w = *h * c->maxa + 0.5;
  406. else if(c->mina < (float)*h / *w)
  407. *h = *w * c->mina + 0.5;
  408. }
  409. if(baseismin) { /* increment calculation requires this */
  410. *w -= c->basew;
  411. *h -= c->baseh;
  412. }
  413. /* adjust for increment value */
  414. if(c->incw)
  415. *w -= *w % c->incw;
  416. if(c->inch)
  417. *h -= *h % c->inch;
  418. /* restore base dimensions */
  419. *w += c->basew;
  420. *h += c->baseh;
  421. *w = MAX(*w, c->minw);
  422. *h = MAX(*h, c->minh);
  423. if(c->maxw)
  424. *w = MIN(*w, c->maxw);
  425. if(c->maxh)
  426. *h = MIN(*h, c->maxh);
  427. }
  428. return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
  429. }
  430.  
  431. void
  432. arrange(Monitor *m) {
  433. if(m)
  434. showhide(m->stack);
  435. else for(m = mons; m; m = m->next)
  436. showhide(m->stack);
  437. focus(NULL);
  438. if(m)
  439. arrangemon(m);
  440. else for(m = mons; m; m = m->next)
  441. arrangemon(m);
  442. }
  443.  
  444. void
  445. arrangemon(Monitor *m) {
  446. strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
  447. if(m->lt[m->sellt]->arrange)
  448. m->lt[m->sellt]->arrange(m);
  449. restack(m);
  450. }
  451.  
  452. void
  453. attachasfirst(Client *c) {
  454. c->next = c->mon->clients;
  455. c->mon->clients = c;
  456. }
  457.  
  458. void
  459. attachaslast(Client *c) {
  460. /*if(!c->mon->clients) {
  461. attachasfirst();
  462. return;
  463. }*/
  464. Client *at;
  465. for(at = c->mon->clients; at->next; at = at->next);
  466. c->next = at->next;
  467. at->next = c;
  468. }
  469.  
  470. void
  471. attachabove(Client *c) {
  472. if(c->mon->sel == NULL /*|| c->mon->sel == c->mon->clients */|| c->mon->sel->isfloating) {
  473. attachasfirst(c);
  474. return;
  475. }
  476.  
  477. Client *at;
  478. for (at = c->mon->clients; at->next != c->mon->sel; at = at->next);
  479. c->next = at->next;
  480. at->next = c;
  481. }
  482.  
  483. void
  484. attachbelow(Client *c) {
  485. if(c->mon->sel == NULL /*|| c->mon->sel == c->mon->clients */|| c->mon->sel->isfloating) {
  486. attachaslast(c);
  487. return;
  488. }
  489.  
  490. Client *at;
  491. for (at = c->mon->clients; at->next != c->mon->sel; at = at->next);
  492. if(at->next)
  493. at = at->next;
  494. c->next = at->next;
  495. at->next = c;
  496. }
  497.  
  498. void
  499. attachaside(Client *c) {
  500. Client *at = nexttiled(c->mon->clients);
  501. if(/*c->mon->sel == NULL || c->mon->sel->isfloating || */!at) {
  502. attachasfirst(c);
  503. return;
  504. }
  505. c->next = at->next;
  506. at->next = c;
  507. }
  508.  
  509. void
  510. attach(Client *c) {
  511. if(selmon->attachmodes[selmon->curtag] == AttAside)
  512. attachaside(c);
  513. else if(selmon->attachmodes[selmon->curtag] == AttBelow)
  514. attachbelow(c);
  515. else if(selmon->attachmodes[selmon->curtag] == AttAbove)
  516. attachabove(c);
  517. else if(selmon->attachmodes[selmon->curtag] == AttAsLast)
  518. attachaslast(c);
  519. else
  520. attachasfirst(c);
  521. }
  522.  
  523. void
  524. setattachmode(const Arg *arg) {
  525. if(!arg) return;
  526. selmon->attachmodes[selmon->curtag] = arg->i;
  527. }
  528.  
  529. void
  530. attachstack(Client *c) {
  531. c->snext = c->mon->stack;
  532. c->mon->stack = c;
  533. }
  534.  
  535. void
  536. buttonpress(XEvent *e) {
  537. unsigned int i, x, click;
  538. Arg arg = {0};
  539. Client *c;
  540. Monitor *m;
  541. XButtonPressedEvent *ev = &e->xbutton;
  542.  
  543. click = ClkRootWin;
  544. /* focus monitor if necessary */
  545. if((m = wintomon(ev->window)) && m != selmon) {
  546. unfocus(selmon->sel, True);
  547. selmon = m;
  548. focus(NULL);
  549. }
  550. if(ev->window == selmon->barwin) {
  551. i = x = 0;
  552. do {
  553. x += TEXTW(tags[i].name);
  554. } while(ev->x >= x && ++i < LENGTH(tags));
  555. if(i < LENGTH(tags)) {
  556. click = ClkTagBar;
  557. arg.ui = 1 << i;
  558. }
  559. else if(ev->x < x + blw)
  560. click = ClkLtSymbol;
  561. else if(ev->x > selmon->wx + selmon->ww - TEXTW(stext))
  562. click = ClkStatusText;
  563. else {
  564. arg.ui = ev->x;
  565. click = ClkWinTitle;
  566. }
  567. }
  568. else if((c = wintoclient(ev->window))) {
  569. focus(c);
  570. click = ClkClientWin;
  571. }
  572. for(i = 0; i < LENGTH(buttons); i++)
  573. if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
  574. && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
  575. buttons[i].func((click == ClkTagBar || click == ClkWinTitle) && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
  576. }
  577.  
  578. void
  579. checkotherwm(void) {
  580. otherwm = False;
  581. xerrorxlib = XSetErrorHandler(xerrorstart);
  582. /* this causes an error if some other window manager is running */
  583. XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
  584. XSync(dpy, False);
  585. if(otherwm)
  586. die("dwm: another window manager is already running\n");
  587. XSetErrorHandler(xerror);
  588. XSync(dpy, False);
  589. }
  590.  
  591. void
  592. cleanup(void) {
  593. Arg a = {.ui = 1};
  594. Layout foo = { "", False, NULL };
  595. Monitor *m;
  596.  
  597. view(&a);
  598. selmon->lt[selmon->sellt] = &foo;
  599. for(m = mons; m; m = m->next)
  600. while(m->stack)
  601. unmanage(m->stack, False);
  602. if(dc.font.set)
  603. XFreeFontSet(dpy, dc.font.set);
  604. else
  605. XFreeFont(dpy, dc.font.xfont);
  606. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  607. XFreePixmap(dpy, dc.drawable);
  608. XFreeGC(dpy, dc.gc);
  609. XFreeCursor(dpy, cursor[CurNormal]);
  610. XFreeCursor(dpy, cursor[CurResize]);
  611. XFreeCursor(dpy, cursor[CurMove]);
  612. while(mons)
  613. cleanupmon(mons);
  614. XSync(dpy, False);
  615. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  616. }
  617.  
  618. void
  619. cleanupmon(Monitor *mon) {
  620. Monitor *m;
  621.  
  622. if(mon == mons)
  623. mons = mons->next;
  624. else {
  625. for(m = mons; m && m->next != mon; m = m->next);
  626. m->next = mon->next;
  627. }
  628. XUnmapWindow(dpy, mon->barwin);
  629. XDestroyWindow(dpy, mon->barwin);
  630. free(mon->mfacts);
  631. free(mon->lts);
  632. free(mon->showbars);
  633. free(mon->topbars);
  634. free(mon->attachmodes);
  635. free(mon->nmasters);
  636. free(mon->lastfocus);
  637. free(mon);
  638. }
  639.  
  640. void
  641. clearurgent(Client *c) {
  642. XWMHints *wmh;
  643.  
  644. c->isurgent = False;
  645. if(!(wmh = XGetWMHints(dpy, c->win)))
  646. return;
  647. wmh->flags &= ~XUrgencyHint;
  648. XSetWMHints(dpy, c->win, wmh);
  649. XFree(wmh);
  650. }
  651.  
  652. void
  653. configure(Client *c) {
  654. XConfigureEvent ce;
  655.  
  656. ce.type = ConfigureNotify;
  657. ce.display = dpy;
  658. ce.event = c->win;
  659. ce.window = c->win;
  660. ce.x = c->x;
  661. ce.y = c->y;
  662. ce.width = c->w;
  663. ce.height = c->h;
  664. ce.border_width = c->bw;
  665. ce.above = None;
  666. ce.override_redirect = False;
  667. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
  668. }
  669.  
  670. void
  671. configurenotify(XEvent *e) {
  672. Monitor *m;
  673. XConfigureEvent *ev = &e->xconfigure;
  674.  
  675. if(ev->window == root) {
  676. sw = ev->width;
  677. sh = ev->height;
  678. if(updategeom()) {
  679. if(dc.drawable != 0)
  680. XFreePixmap(dpy, dc.drawable);
  681. dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
  682. updatebars();
  683. for(m = mons; m; m = m->next)
  684. XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
  685. arrange(NULL);
  686. }
  687. }
  688. }
  689.  
  690. void
  691. configurerequest(XEvent *e) {
  692. Client *c;
  693. Monitor *m;
  694. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  695. XWindowChanges wc;
  696.  
  697. if((c = wintoclient(ev->window))) {
  698. if(ev->value_mask & CWBorderWidth)
  699. c->bw = ev->border_width;
  700. else if(c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
  701. m = c->mon;
  702. if(ev->value_mask & CWX)
  703. c->x = m->mx + ev->x;
  704. if(ev->value_mask & CWY)
  705. c->y = m->my + ev->y;
  706. if(ev->value_mask & CWWidth)
  707. c->w = ev->width;
  708. if(ev->value_mask & CWHeight)
  709. c->h = ev->height;
  710. if((c->x + c->w) > m->mx + m->mw && c->isfloating)
  711. c->x = m->mx + (m->mw / 2 - c->w / 2); /* center in x direction */
  712. if((c->y + c->h) > m->my + m->mh && c->isfloating)
  713. c->y = m->my + (m->mh / 2 - c->h / 2); /* center in y direction */
  714. if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
  715. configure(c);
  716. if(ISVISIBLE(c))
  717. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  718. else
  719. c->needresize=1;
  720. }
  721. else
  722. configure(c);
  723. }
  724. else {
  725. wc.x = ev->x;
  726. wc.y = ev->y;
  727. wc.width = ev->width;
  728. wc.height = ev->height;
  729. wc.border_width = ev->border_width;
  730. wc.sibling = ev->above;
  731. wc.stack_mode = ev->detail;
  732. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  733. }
  734. XSync(dpy, False);
  735. }
  736.  
  737. Monitor *
  738. createmon(void) {
  739. Monitor *m;
  740. int i, numtags = LENGTH(tags) + 1;
  741.  
  742. if(!(m = (Monitor *)calloc(1, sizeof(Monitor))))
  743. die("fatal: could not malloc() %u bytes\n", sizeof(Monitor));
  744. if(!(m->mfacts = calloc(sizeof(double), numtags)))
  745. die("fatal: could not malloc() %u bytes\n", sizeof(double) * numtags);
  746. if(!(m->lts = calloc(sizeof(Layout *), numtags)))
  747. die("fatal: could not malloc() %u bytes\n", sizeof(Layout *) * numtags);
  748. if(!(m->showbars = calloc(sizeof(Bool *), numtags)))
  749. die("fatal: could not malloc() %u bytes\n", sizeof(Bool *) * numtags);
  750. if(!(m->topbars = calloc(sizeof(Bool *), numtags)))
  751. die("fatal: could not malloc() %u bytes\n", sizeof(Bool *) * numtags);
  752. if(!(m->attachmodes = calloc(sizeof(int *), numtags)))
  753. die("fatal: could not malloc() %u bytes\n", sizeof(int *) * numtags);
  754. if(!(m->nmasters = calloc(sizeof(int *), numtags)))
  755. die("fatal: could not malloc() %u bytes\n", sizeof(int *) * numtags);
  756. if(!(m->lastfocus = calloc(sizeof(Client *), numtags)))
  757. die("fatal: could not malloc() %u bytes\n", sizeof(Client *) * numtags);
  758. m->tagset[0] = m->tagset[1] = 1;
  759. m->showbar = showbar;
  760. m->topbar = topbar;
  761.  
  762. m->curtag = m->prevtag = 1;
  763. for(i = 1; i < numtags; i++) {
  764. m->mfacts[i] = tags[i - 1].mfact < 0 ? mfact : tags[i - 1].mfact;
  765. m->lts[i] = tags[i - 1].layout;
  766. m->showbars[i] = tags[i - 1].showbar < 0 ? showbar : tags[i - 1].showbar;
  767. m->topbars[i] = tags[i - 1].topbar < 0 ? topbar : tags[i - 1].topbar;
  768. m->attachmodes[i] = tags[i - 1].attachmode < 0 ? attachmode : tags[i - 1].attachmode;
  769. m->nmasters[i] = tags[i - 1].nmaster < 0 ? nmaster : tags[i - 1].nmaster;
  770. m->lastfocus[i] = NULL;
  771. }
  772. m->lt[0] = m->lts[m->curtag];
  773.  
  774. m->lt[1] = &layouts[1 % LENGTH(layouts)];
  775. strncpy(m->ltsymbol, m->lt[0]->symbol, sizeof m->ltsymbol);
  776. return m;
  777. }
  778.  
  779. void
  780. destroynotify(XEvent *e) {
  781. Client *c;
  782. XDestroyWindowEvent *ev = &e->xdestroywindow;
  783.  
  784. if((c = wintoclient(ev->window)))
  785. unmanage(c, True);
  786. }
  787.  
  788. void
  789. detach(Client *c) {
  790. Client **tc;
  791.  
  792. for(tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
  793. *tc = c->next;
  794. }
  795.  
  796. void
  797. detachstack(Client *c) {
  798. Client **tc, *t;
  799.  
  800. for(tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
  801. *tc = c->snext;
  802.  
  803. if(c == c->mon->sel) {
  804. for(t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
  805. c->mon->sel = t;
  806. }
  807. }
  808.  
  809. void
  810. die(const char *errstr, ...) {
  811. va_list ap;
  812.  
  813. va_start(ap, errstr);
  814. vfprintf(stderr, errstr, ap);
  815. va_end(ap);
  816. exit(EXIT_FAILURE);
  817. }
  818.  
  819. Monitor *
  820. dirtomon(int dir) {
  821. Monitor *m = NULL;
  822.  
  823. if(dir > 0) {
  824. if(!(m = selmon->next))
  825. m = mons;
  826. }
  827. else {
  828. if(selmon == mons)
  829. for(m = mons; m->next; m = m->next);
  830. else
  831. for(m = mons; m->next != selmon; m = m->next);
  832. }
  833. return m;
  834. }
  835.  
  836. void
  837. drawbar(Monitor *m) {
  838. int x, a= 0, s= 0, ow, mw = 0, extra, tw;
  839. unsigned int i, n = 0, occ = 0, urg = 0;
  840. char posbuf[10];
  841. unsigned long *col;
  842. Client *c, *firstvis, *lastvis = NULL;
  843. DC seldc;
  844.  
  845. for(c = m->clients; c; c = c->next) {
  846. if(ISVISIBLE(c))
  847. n++;
  848. occ |= c->tags;
  849. if(c->isurgent)
  850. urg |= c->tags;
  851. }
  852. dc.x = 0;
  853. for(i = 0; i < LENGTH(tags); i++) {
  854. dc.w = TEXTW(tags[i].name);
  855. col = m->tagset[m->seltags] & 1 << i ? dc.sel : dc.norm;
  856. drawtext(tags[i].name, col, urg & 1 << i, True);
  857. drawsquare(m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
  858. occ & 1 << i, urg & 1 << i, col);
  859. dc.x += dc.w;
  860. }
  861. dc.w = blw = TEXTW(m->ltsymbol);
  862. drawtext(m->ltsymbol, dc.norm, False, True);
  863. dc.x += dc.w;
  864. x = dc.x;
  865. if(m == selmon) { /* status is only drawn on selected monitor */
  866. if(m->lt[m->sellt]->arrange == monocle){
  867. dc.x= x;
  868. for(c= nexttiled(m->clients), a= 0, s= 0; c; c= nexttiled(c->next), a++)
  869. if(c == m->stack)
  870. s = a;
  871. if(!s && a)
  872. s = a;
  873. snprintf(posbuf, LENGTH(posbuf), "[%d/%d]", s, a);
  874. dc.w= TEXTW(posbuf);
  875. drawtext(posbuf, dc.norm, False, True);
  876. x= dc.x + dc.w;
  877. }
  878.  
  879. dc.w=0;
  880. char *buf = stext, *ptr = buf;
  881. while( *ptr ) {
  882. for( i = 0; *ptr < 0 || *ptr > NumColors; i++, ptr++);
  883. dc.w += textnw(buf,i);
  884. buf=++ptr;
  885. }
  886. dc.w+=dc.font.height;
  887. dc.x = m->ww - dc.w;
  888. if(dc.x < x) {
  889. dc.x = x;
  890. dc.w = m->ww - x;
  891. }
  892. m->titlebarend=dc.x;
  893. drawcoloredtext(m, stext);
  894. }
  895. else {
  896. dc.x = m->ww;
  897. m->titlebarbegin=dc.x;
  898. }
  899.  
  900. for(c = m->clients; c && !ISVISIBLE(c); c = c->next);
  901. firstvis = c;
  902.  
  903. col = m == selmon ? dc.sel : dc.norm;
  904. dc.w = dc.x - x;
  905. dc.x = x;
  906.  
  907. if(n > 0) {
  908. mw = dc.w / n;
  909. extra = 0;
  910. seldc = dc;
  911. i = 0;
  912.  
  913. while(c) {
  914. lastvis = c;
  915. tw = TEXTW(c->name);
  916. if(tw < mw) extra += (mw - tw); else i++;
  917. for(c = c->next; c && !ISVISIBLE(c); c = c->next);
  918. }
  919.  
  920. if(i > 0) mw += extra / i;
  921.  
  922. c = firstvis;
  923. x = dc.x;
  924. }
  925. m->titlebarbegin=dc.x;
  926. while(dc.w > bh) {
  927. if(c) {
  928. ow = dc.w;
  929. tw = TEXTW(c->name);
  930. dc.w = MIN(ow, tw);
  931.  
  932. if(dc.w > mw) dc.w = mw;
  933. if(m->sel == c) seldc = dc;
  934. if(c == lastvis) dc.w = ow;
  935.  
  936. drawtext(c->name, col, False, True);
  937. if(c != firstvis) drawvline(col);
  938. drawsquare(c->isfixed, c->isfloating, False, col);
  939.  
  940. dc.x += dc.w;
  941. dc.w = ow - dc.w;
  942. for(c = c->next; c && !ISVISIBLE(c); c = c->next);
  943. } else {
  944. drawtext(NULL, dc.norm, False, True);
  945. break;
  946. }
  947. }
  948.  
  949. if(m == selmon && m->sel && ISVISIBLE(m->sel)) {
  950. dc = seldc;
  951. drawtext(m->sel->name, col, True, True);
  952. drawsquare(m->sel->isfixed, m->sel->isfloating, True, col);
  953. }
  954.  
  955. XCopyArea(dpy, dc.drawable, m->barwin, dc.gc, 0, 0, m->ww, bh, 0, 0);
  956. XSync(dpy, False);
  957. }
  958.  
  959. void
  960. drawbars(void) {
  961. Monitor *m;
  962.  
  963. for(m = mons; m; m = m->next)
  964. drawbar(m);
  965. }
  966.  
  967. void
  968. drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) {
  969. int x;
  970. XGCValues gcv;
  971. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  972.  
  973. gcv.foreground = col[invert ? ColBG : ColFG];
  974. XChangeGC(dpy, dc.gc, GCForeground, &gcv);
  975. x = (dc.font.ascent + dc.font.descent + 2) / 4;
  976. r.x = dc.x + 1;
  977. r.y = dc.y + 1;
  978. if(filled) {
  979. r.width = r.height = x + 1;
  980. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  981. }
  982. else if(empty) {
  983. r.width = r.height = x;
  984. XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  985. }
  986. }
  987.  
  988. void
  989. drawtext(const char *text, unsigned long col[ColLast], Bool invert, Bool pad) {
  990. char buf[256];
  991. int i, x, y, h, len, olen;
  992. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  993.  
  994. XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]);
  995. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  996. if(!text)
  997. return;
  998. olen = strlen(text);
  999. h = dc.font.ascent + dc.font.descent;
  1000. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  1001. x = dc.x + ((pad ? (dc.font.ascent + dc.font.descent) : 0) / 2);
  1002. /* shorten text if necessary */
  1003. for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
  1004. if(!len)
  1005. return;
  1006. memcpy(buf, text, len);
  1007. if(len < olen)
  1008. for(i = len; i && i > len - 3; buf[--i] = '.');
  1009. XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
  1010. if(dc.font.set)
  1011. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  1012. else
  1013. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  1014. }
  1015.  
  1016. void
  1017. drawvline(unsigned long col[ColLast]) {
  1018. XGCValues gcv;
  1019.  
  1020. gcv.foreground = col[ColFG];
  1021. XChangeGC(dpy, dc.gc, GCForeground, &gcv);
  1022. XDrawLine(dpy, dc.drawable, dc.gc, dc.x, dc.y, dc.x, dc.y + (dc.font.ascent + dc.font.descent + 2));
  1023. }
  1024.  
  1025. void
  1026. drawcoloredtext(Monitor *m, char *text) {
  1027. Bool first=True;
  1028. char *buf = text, *ptr = buf, c = 1;
  1029. unsigned long *col = barcolors[0];
  1030. int i, ox = dc.x;
  1031.  
  1032. while( *ptr ) {
  1033. for( i = 0; *ptr < 0 || *ptr > NumColors; i++, ptr++);
  1034. if( !*ptr ) break;
  1035. c=*ptr;
  1036. *ptr=0;
  1037. if( i ) {
  1038. dc.w = m->ww - dc.x;
  1039. drawtext(buf, col, False, first);
  1040. dc.x += textnw(buf, i);// + textnw(&c,1);
  1041. if( first ) dc.x += ( dc.font.ascent + dc.font.descent ) / 2;
  1042. first = False;
  1043. } else if( first ) {
  1044. ox = dc.x += textnw(&c,1);
  1045. }
  1046. *ptr = c;
  1047. col = barcolors[ c-1 ];
  1048. buf = ++ptr;
  1049. }
  1050. //if( !first ) dc.x-=(dc.font.ascent+dc.font.descent)/2;
  1051. drawtext(buf, col, False, False);
  1052. dc.x = ox;
  1053. }
  1054. void
  1055. enternotify(XEvent *e) {
  1056. Client *c;
  1057. Monitor *m;
  1058. XCrossingEvent *ev = &e->xcrossing;
  1059.  
  1060. if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
  1061. return;
  1062. if((m = wintomon(ev->window)) && m != selmon) {
  1063. unfocus(selmon->sel, True);
  1064. selmon = m;
  1065. }
  1066. if((c = wintoclient(ev->window)))
  1067. focus(c);
  1068. else
  1069. focus(NULL);
  1070. }
  1071.  
  1072. void
  1073. expose(XEvent *e) {
  1074. Monitor *m;
  1075. XExposeEvent *ev = &e->xexpose;
  1076.  
  1077. if(ev->count == 0 && (m = wintomon(ev->window)))
  1078. drawbar(m);
  1079. }
  1080.  
  1081. void
  1082. window_opacity_set(Client *c, double opacity) {
  1083. if(opacity >= 0 && opacity <= 1) {
  1084. unsigned long real_opacity[] = { opacity * 0xffffffff };
  1085. XChangeProperty(dpy, c->win, netatom[NetWMWindowOpacity], XA_CARDINAL, 32, PropModeReplace, (unsigned char *)real_opacity, 1);
  1086. }
  1087. else
  1088. XDeleteProperty(dpy, c->win, netatom[NetWMWindowOpacity]);
  1089. }
  1090.  
  1091. void
  1092. focus(Client *c) {
  1093. if(!c || !ISVISIBLE(c))
  1094. for(c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
  1095. /* was if(selmon->sel) */
  1096. if(selmon->sel && selmon->sel != c)
  1097. unfocus(selmon->sel, False);
  1098. if(selmon->sel && c!=selmon->sel && c && (!root || (selmon->sel->win!=root && c->win!=root)) ) window_opacity_set(selmon->sel, shade);
  1099. if(c && c!=selmon->sel && (!root || (c->win!=root)) ) window_opacity_set(c, c->opacity);
  1100. if(c) {
  1101. if(c->mon != selmon)
  1102. selmon = c->mon;
  1103. if(c->isurgent)
  1104. clearurgent(c);
  1105. detachstack(c);
  1106. attachstack(c);
  1107. grabbuttons(c, True);
  1108. XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
  1109. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  1110. }
  1111. else
  1112. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  1113. selmon->lastfocus[selmon->curtag]=selmon->sel;
  1114. selmon->sel = c;
  1115. drawbars();
  1116. if(c) window_opacity_set(c, c->opacity);
  1117. }
  1118.  
  1119. void
  1120. focusonclick(const Arg *arg) {
  1121. int x, w, mw = 0, tw, n = 0, i = 0, extra = 0;
  1122. Monitor *m = selmon;
  1123. Client *c, *firstvis;
  1124.  
  1125. for(c = m->clients; c && !ISVISIBLE(c); c = c->next);
  1126. firstvis = c;
  1127.  
  1128. for(c = m->clients; c; c = c->next)
  1129. if (ISVISIBLE(c))
  1130. n++;
  1131.  
  1132. if(n > 0) {
  1133. mw = (m->titlebarend - m->titlebarbegin) / n;
  1134. c = firstvis;
  1135. while(c) {
  1136. tw = TEXTW(c->name);
  1137. if(tw < mw) extra += (mw - tw); else i++;
  1138. for(c = c->next; c && !ISVISIBLE(c); c = c->next);
  1139. }
  1140. if(i > 0) mw += extra / i;
  1141. }
  1142.  
  1143. x=m->titlebarbegin;
  1144.  
  1145. c = firstvis;
  1146. while(x < m->titlebarend) {
  1147. if(c) {
  1148. w=MIN(TEXTW(c->name), mw);
  1149. if (x < arg->i && x+w > arg->i) {
  1150. focus(c);
  1151. restack(selmon);
  1152. break;
  1153. } else
  1154. x+=w;
  1155.  
  1156. for(c = c->next; c && !ISVISIBLE(c); c = c->next);
  1157. } else {
  1158. break;
  1159. }
  1160. }
  1161. }
  1162.  
  1163. void
  1164. focusin(XEvent *e) { /* there are some broken focus acquiring clients */
  1165. XFocusChangeEvent *ev = &e->xfocus;
  1166.  
  1167. if(selmon->sel && ev->window != selmon->sel->win)
  1168. XSetInputFocus(dpy, selmon->sel->win, RevertToPointerRoot, CurrentTime);
  1169. }
  1170.  
  1171. void
  1172. focusmon(const Arg *arg) {
  1173. Monitor *m = NULL;
  1174.  
  1175. if(!mons->next)
  1176. return;
  1177. if((m = dirtomon(arg->i)) == selmon)
  1178. return;
  1179. unfocus(selmon->sel, True);
  1180. selmon = m;
  1181. focus(NULL);
  1182. }
  1183.  
  1184. void
  1185. focusstack(const Arg *arg) {
  1186. Client *c = NULL, *i;
  1187.  
  1188. if(!selmon->sel)
  1189. return;
  1190. if(arg->i > 0) {
  1191. for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
  1192. if(!c)
  1193. for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
  1194. }
  1195. else {
  1196. for(i = selmon->clients; i != selmon->sel; i = i->next)
  1197. if(ISVISIBLE(i))
  1198. c = i;
  1199. if(!c)
  1200. for(; i; i = i->next)
  1201. if(ISVISIBLE(i))
  1202. c = i;
  1203. }
  1204. if(c) {
  1205. focus(c);
  1206. restack(selmon);
  1207. }
  1208. }
  1209.  
  1210. void
  1211. focustoggle(const Arg *arg) {
  1212. if(selmon->sel && selmon->lastfocus[selmon->curtag]) {
  1213. focus(selmon->lastfocus[selmon->curtag]);
  1214. restack(selmon);
  1215. }
  1216. }
  1217.  
  1218. unsigned long
  1219. getcolor(const char *colstr) {
  1220. Colormap cmap = DefaultColormap(dpy, screen);
  1221. XColor color;
  1222.  
  1223. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  1224. die("error, cannot allocate color '%s'\n", colstr);
  1225. return color.pixel;
  1226. }
  1227.  
  1228. Bool
  1229. getrootptr(int *x, int *y) {
  1230. int di;
  1231. unsigned int dui;
  1232. Window dummy;
  1233.  
  1234. return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
  1235. }
  1236.  
  1237. long
  1238. getstate(Window w) {
  1239. int format;
  1240. long result = -1;
  1241. unsigned char *p = NULL;
  1242. unsigned long n, extra;
  1243. Atom real;
  1244.  
  1245. if(XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
  1246. &real, &format, &n, &extra, (unsigned char **)&p) != Success)
  1247. return -1;
  1248. if(n != 0)
  1249. result = *p;
  1250. XFree(p);
  1251. return result;
  1252. }
  1253.  
  1254. Bool
  1255. gettextprop(Window w, Atom atom, char *text, unsigned int size) {
  1256. char **list = NULL;
  1257. int n;
  1258. XTextProperty name;
  1259.  
  1260. if(!text || size == 0)
  1261. return False;
  1262. text[0] = '\0';
  1263. XGetTextProperty(dpy, w, &name, atom);
  1264. if(!name.nitems)
  1265. return False;
  1266. if(name.encoding == XA_STRING)
  1267. strncpy(text, (char *)name.value, size - 1);
  1268. else {
  1269. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
  1270. strncpy(text, *list, size - 1);
  1271. XFreeStringList(list);
  1272. }
  1273. }
  1274. text[size - 1] = '\0';
  1275. XFree(name.value);
  1276. return True;
  1277. }
  1278.  
  1279. void
  1280. grabbuttons(Client *c, Bool focused) {
  1281. updatenumlockmask();
  1282. {
  1283. unsigned int i, j;
  1284. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  1285. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1286. if(focused) {
  1287. for(i = 0; i < LENGTH(buttons); i++)
  1288. if(buttons[i].click == ClkClientWin)
  1289. for(j = 0; j < LENGTH(modifiers); j++)
  1290. XGrabButton(dpy, buttons[i].button,
  1291. buttons[i].mask | modifiers[j],
  1292. c->win, False, BUTTONMASK,
  1293. GrabModeAsync, GrabModeSync, None, None);
  1294. }
  1295. else
  1296. XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
  1297. BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
  1298. }
  1299. }
  1300.  
  1301. void
  1302. grabkeys(void) {
  1303. updatenumlockmask();
  1304. {
  1305. unsigned int i, j;
  1306. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  1307. KeyCode code;
  1308.  
  1309. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  1310. for(i = 0; i < LENGTH(keys); i++) {
  1311. if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
  1312. for(j = 0; j < LENGTH(modifiers); j++)
  1313. XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
  1314. True, GrabModeAsync, GrabModeAsync);
  1315. }
  1316. }
  1317. }
  1318.  
  1319. void
  1320. initfont(const char *fontstr) {
  1321. char *def, **missing;
  1322. int i, n;
  1323.  
  1324. missing = NULL;
  1325. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  1326. if(missing) {
  1327. while(n--)
  1328. fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
  1329. XFreeStringList(missing);
  1330. }
  1331. if(dc.font.set) {
  1332. XFontSetExtents *font_extents;
  1333. XFontStruct **xfonts;
  1334. char **font_names;
  1335.  
  1336. dc.font.ascent = dc.font.descent = 0;
  1337. font_extents = XExtentsOfFontSet(dc.font.set);
  1338. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  1339. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  1340. dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
  1341. dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent);
  1342. xfonts++;
  1343. }
  1344. }
  1345. else {
  1346. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  1347. && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  1348. die("error, cannot load font: '%s'\n", fontstr);
  1349. dc.font.ascent = dc.font.xfont->ascent;
  1350. dc.font.descent = dc.font.xfont->descent;
  1351. }
  1352. dc.font.height = dc.font.ascent + dc.font.descent;
  1353. }
  1354.  
  1355. Bool
  1356. isprotodel(Client *c) {
  1357. int i, n;
  1358. Atom *protocols;
  1359. Bool ret = False;
  1360.  
  1361. if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  1362. for(i = 0; !ret && i < n; i++)
  1363. if(protocols[i] == wmatom[WMDelete])
  1364. ret = True;
  1365. XFree(protocols);
  1366. }
  1367. return ret;
  1368. }
  1369.  
  1370. #ifdef XINERAMA
  1371. static Bool
  1372. isuniquegeom(XineramaScreenInfo *unique, size_t len, XineramaScreenInfo *info) {
  1373. unsigned int i;
  1374.  
  1375. for(i = 0; i < len; i++)
  1376. if(unique[i].x_org == info->x_org && unique[i].y_org == info->y_org
  1377. && unique[i].width == info->width && unique[i].height == info->height)
  1378. return False;
  1379. return True;
  1380. }
  1381. #endif /* XINERAMA */
  1382.  
  1383. void
  1384. keypress(XEvent *e) {
  1385. unsigned int i;
  1386. KeySym keysym;
  1387. XKeyEvent *ev;
  1388.  
  1389. ev = &e->xkey;
  1390. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  1391. for(i = 0; i < LENGTH(keys); i++)
  1392. if(keysym == keys[i].keysym
  1393. && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
  1394. && keys[i].func)
  1395. keys[i].func(&(keys[i].arg));
  1396. }
  1397.  
  1398. void
  1399. killclient(const Arg *arg) {
  1400. XEvent ev;
  1401.  
  1402. if(!selmon->sel)
  1403. return;
  1404. if(isprotodel(selmon->sel)) {
  1405. ev.type = ClientMessage;
  1406. ev.xclient.window = selmon->sel->win;
  1407. ev.xclient.message_type = wmatom[WMProtocols];
  1408. ev.xclient.format = 32;
  1409. ev.xclient.data.l[0] = wmatom[WMDelete];
  1410. ev.xclient.data.l[1] = CurrentTime;
  1411. XSendEvent(dpy, selmon->sel->win, False, NoEventMask, &ev);
  1412. }
  1413. else {
  1414. XGrabServer(dpy);
  1415. XSetErrorHandler(xerrordummy);
  1416. XSetCloseDownMode(dpy, DestroyAll);
  1417. XKillClient(dpy, selmon->sel->win);
  1418. XSync(dpy, False);
  1419. XSetErrorHandler(xerror);
  1420. XUngrabServer(dpy);
  1421. }
  1422. }
  1423.  
  1424. void
  1425. manage(Window w, XWindowAttributes *wa) {
  1426. static Client cz;
  1427. Client *c, *t = NULL;
  1428. Window trans = None;
  1429. XWindowChanges wc;
  1430.  
  1431. if(!(c = malloc(sizeof(Client))))
  1432. die("fatal: could not malloc() %u bytes\n", sizeof(Client));
  1433. *c = cz;
  1434. c->win = w;
  1435. updatetitle(c);
  1436. if(XGetTransientForHint(dpy, w, &trans))
  1437. t = wintoclient(trans);
  1438. c->opacity=-1;
  1439. if(t) {
  1440. c->mon = t->mon;
  1441. c->tags = t->tags;
  1442. }
  1443. else {
  1444. c->mon = selmon;
  1445. applyrules(c);
  1446. }
  1447. /* geometry */
  1448. c->x = c->oldx = wa->x + c->mon->wx;
  1449. c->y = c->oldy = wa->y + c->mon->wy;
  1450. c->w = c->oldw = wa->width;
  1451. c->h = c->oldh = wa->height;
  1452. c->oldbw = wa->border_width;
  1453. if(c->w == c->mon->mw && c->h == c->mon->mh) {
  1454. c->isfloating = 1;
  1455. c->x = c->mon->mx;
  1456. c->y = c->mon->my;
  1457. c->bw = 0;
  1458. }
  1459. else {
  1460. if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
  1461. c->x = c->mon->mx + c->mon->mw - WIDTH(c);
  1462. if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
  1463. c->y = c->mon->my + c->mon->mh - HEIGHT(c);
  1464. c->x = MAX(c->x, c->mon->mx);
  1465. /* only fix client y-offset, if the client center might cover the bar */
  1466. c->y = MAX(c->y, ((c->mon->by == 0) && (c->x + (c->w / 2) >= c->mon->wx)
  1467. && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
  1468. c->bw = borderpx;
  1469. }
  1470. wc.border_width = c->bw;
  1471. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  1472. XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
  1473. configure(c); /* propagates border_width, if size doesn't change */
  1474. updatesizehints(c);
  1475. XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
  1476. grabbuttons(c, False);
  1477. if(!c->isfloating)
  1478. c->isfloating = c->oldstate = trans != None || c->isfixed;
  1479. if(c->isfloating)
  1480. XRaiseWindow(dpy, c->win);
  1481. attach(c);
  1482. attachstack(c);
  1483. XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
  1484. XMapWindow(dpy, c->win);
  1485. setclientstate(c, NormalState);
  1486. arrange(c->mon);
  1487. }
  1488.  
  1489. void
  1490. mappingnotify(XEvent *e) {
  1491. XMappingEvent *ev = &e->xmapping;
  1492.  
  1493. XRefreshKeyboardMapping(ev);
  1494. if(ev->request == MappingKeyboard)
  1495. grabkeys();
  1496. }
  1497.  
  1498. void
  1499. maprequest(XEvent *e) {
  1500. static XWindowAttributes wa;
  1501. XMapRequestEvent *ev = &e->xmaprequest;
  1502.  
  1503. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  1504. return;
  1505. if(wa.override_redirect)
  1506. return;
  1507. if(!wintoclient(ev->window))
  1508. manage(ev->window, &wa);
  1509. }
  1510.  
  1511. void
  1512. monocle(Monitor *m) {
  1513. unsigned int n = 0;
  1514. Client *c;
  1515.  
  1516. for(c = m->clients; c; c = c->next)
  1517. if(ISVISIBLE(c))
  1518. n++;
  1519. if(n > 0) /* override layout symbol */
  1520. snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
  1521. for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
  1522. resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, False);
  1523. }
  1524.  
  1525. void
  1526. movemouse(const Arg *arg) {
  1527. int x, y, ocx, ocy, nx, ny;
  1528. Client *c;
  1529. Monitor *m;
  1530. XEvent ev;
  1531.  
  1532. if(!(c = selmon->sel))
  1533. return;
  1534. restack(selmon);
  1535. ocx = c->x;
  1536. ocy = c->y;
  1537. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1538. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  1539. return;
  1540. if(!getrootptr(&x, &y))
  1541. return;
  1542. do {
  1543. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1544. switch (ev.type) {
  1545. case ConfigureRequest:
  1546. case Expose:
  1547. case MapRequest:
  1548. handler[ev.type](&ev);
  1549. break;
  1550. case MotionNotify:
  1551. nx = ocx + (ev.xmotion.x - x);
  1552. ny = ocy + (ev.xmotion.y - y);
  1553. if(snap && nx >= selmon->wx && nx <= selmon->wx + selmon->ww
  1554. && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
  1555. if(abs(selmon->wx - nx) < snap)
  1556. nx = selmon->wx;
  1557. else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
  1558. nx = selmon->wx + selmon->ww - WIDTH(c);
  1559. if(abs(selmon->wy - ny) < snap)
  1560. ny = selmon->wy;
  1561. else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
  1562. ny = selmon->wy + selmon->wh - HEIGHT(c);
  1563. if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1564. && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
  1565. togglefloating(NULL);
  1566. }
  1567. if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
  1568. resize(c, nx, ny, c->w, c->h, True);
  1569. break;
  1570. }
  1571. } while(ev.type != ButtonRelease);
  1572. XUngrabPointer(dpy, CurrentTime);
  1573. if((m = ptrtomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
  1574. sendmon(c, m);
  1575. selmon = m;
  1576. focus(NULL);
  1577. }
  1578. }
  1579.  
  1580. Client *
  1581. nexttiled(Client *c) {
  1582. for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
  1583. return c;
  1584. }
  1585.  
  1586. Monitor *
  1587. ptrtomon(int x, int y) {
  1588. Monitor *m;
  1589.  
  1590. for(m = mons; m; m = m->next)
  1591. if(INRECT(x, y, m->wx, m->wy, m->ww, m->wh))
  1592. return m;
  1593. return selmon;
  1594. }
  1595.  
  1596. void
  1597. propertynotify(XEvent *e) {
  1598. Client *c;
  1599. Window trans;
  1600. XPropertyEvent *ev = &e->xproperty;
  1601.  
  1602. if((ev->window == root) && (ev->atom == XA_WM_NAME))
  1603. updatestatus();
  1604. else if(ev->state == PropertyDelete)
  1605. return; /* ignore */
  1606. else if((c = wintoclient(ev->window))) {
  1607. switch (ev->atom) {
  1608. default: break;
  1609. case XA_WM_TRANSIENT_FOR:
  1610. XGetTransientForHint(dpy, c->win, &trans);
  1611. if(!c->isfloating && (c->isfloating = (wintoclient(trans) != NULL)))
  1612. arrange(c->mon);
  1613. break;
  1614. case XA_WM_NORMAL_HINTS:
  1615. updatesizehints(c);
  1616. break;
  1617. case XA_WM_HINTS:
  1618. updatewmhints(c);
  1619. drawbars();
  1620. break;
  1621. }
  1622. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  1623. updatetitle(c);
  1624. if(c == c->mon->sel)
  1625. drawbar(c->mon);
  1626. }
  1627. }
  1628. }
  1629.  
  1630. void
  1631. clientmessage(XEvent *e) {
  1632. XClientMessageEvent *cme = &e->xclient;
  1633. Client *c;
  1634.  
  1635. if((c = wintoclient(cme->window))
  1636. && (cme->message_type == netatom[NetWMState] && cme->data.l[1] == netatom[NetWMFullscreen]))
  1637. {
  1638. if(cme->data.l[0]) {
  1639. XChangeProperty(dpy, cme->window, netatom[NetWMState], XA_ATOM, 32,
  1640. PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
  1641. c->oldstate = c->isfloating;
  1642. c->oldbw = c->bw;
  1643. c->bw = 0;
  1644. c->isfloating = 1;
  1645. resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
  1646. XRaiseWindow(dpy, c->win);
  1647. }
  1648. else {
  1649. XChangeProperty(dpy, cme->window, netatom[NetWMState], XA_ATOM, 32,
  1650. PropModeReplace, (unsigned char*)0, 0);
  1651. c->isfloating = c->oldstate;
  1652. c->bw = c->oldbw;
  1653. c->x = c->oldx;
  1654. c->y = c->oldy;
  1655. c->w = c->oldw;
  1656. c->h = c->oldh;
  1657. resizeclient(c, c->x, c->y, c->w, c->h);
  1658. arrange(c->mon);
  1659. }
  1660. }
  1661. }
  1662.  
  1663. void
  1664. quit(const Arg *arg) {
  1665. running = False;
  1666. //exitcode = arg->i;
  1667. }
  1668.  
  1669. void
  1670. resize(Client *c, int x, int y, int w, int h, Bool interact) {
  1671. if(applysizehints(c, &x, &y, &w, &h, interact))
  1672. resizeclient(c, x, y, w, h);
  1673. }
  1674.  
  1675. void
  1676. resizeclient(Client *c, int x, int y, int w, int h) {
  1677. XWindowChanges wc;
  1678. unsigned int gap = c->isfloating ? 0 : selmon->lt[selmon->sellt]->addgaps ? gappx : 0;
  1679.  
  1680. c->oldx = c->x; c->x = wc.x = x + gap;
  1681. c->oldy = c->y; c->y = wc.y = y + gap;
  1682. c->oldw = c->w; c->w = wc.width = w - (gap ? (x + w + (c->bw * 2) == c->mon->wx + c->mon->ww ? 2 : 1) * gap : 0);
  1683. c->oldh = c->h; c->h = wc.height = h - (gap ? (y + h + (c->bw * 2) == c->mon->wy + c->mon->wh ? 2 : 1) * gap : 0);
  1684. wc.border_width = c->bw;
  1685. XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  1686. configure(c);
  1687. XSync(dpy, False);
  1688. }
  1689.  
  1690. void
  1691. resizemouse(const Arg *arg) {
  1692. int ocx, ocy;
  1693. int nw, nh;
  1694. Client *c;
  1695. Monitor *m;
  1696. XEvent ev;
  1697.  
  1698. if(!(c = selmon->sel))
  1699. return;
  1700. restack(selmon);
  1701. ocx = c->x;
  1702. ocy = c->y;
  1703. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1704. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  1705. return;
  1706. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1707. do {
  1708. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1709. switch(ev.type) {
  1710. case ConfigureRequest:
  1711. case Expose:
  1712. case MapRequest:
  1713. handler[ev.type](&ev);
  1714. break;
  1715. case MotionNotify:
  1716. nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
  1717. nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
  1718. if(snap && nw >= selmon->wx && nw <= selmon->wx + selmon->ww
  1719. && nh >= selmon->wy && nh <= selmon->wy + selmon->wh)
  1720. {
  1721. if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1722. && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
  1723. togglefloating(NULL);
  1724. }
  1725. if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
  1726. resize(c, c->x, c->y, nw, nh, True);
  1727. break;
  1728. }
  1729. } while(ev.type != ButtonRelease);
  1730. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1731. XUngrabPointer(dpy, CurrentTime);
  1732. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1733. if((m = ptrtomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
  1734. sendmon(c, m);
  1735. selmon = m;
  1736. focus(NULL);
  1737. }
  1738. }
  1739.  
  1740. void
  1741. restack(Monitor *m) {
  1742. Client *c;
  1743. XEvent ev;
  1744. XWindowChanges wc;
  1745.  
  1746. drawbar(m);
  1747. if(!m->sel)
  1748. return;
  1749. if(m->sel->isfloating || !m->lt[m->sellt]->arrange)
  1750. XRaiseWindow(dpy, m->sel->win);
  1751. if(m->lt[m->sellt]->arrange) {
  1752. wc.stack_mode = Below;
  1753. wc.sibling = m->barwin;
  1754. for(c = m->stack; c; c = c->snext)
  1755. if(!c->isfloating && ISVISIBLE(c)) {
  1756. XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
  1757. wc.sibling = c->win;
  1758. }
  1759. }
  1760. XSync(dpy, False);
  1761. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1762. }
  1763.  
  1764. void
  1765. run(void) {
  1766. XEvent ev;
  1767. /* main event loop */
  1768. XSync(dpy, False);
  1769. while(running && !XNextEvent(dpy, &ev)) {
  1770. if(handler[ev.type])
  1771. handler[ev.type](&ev); /* call handler */
  1772. }
  1773. }
  1774.  
  1775. void
  1776. scan(void) {
  1777. unsigned int i, num;
  1778. Window d1, d2, *wins = NULL;
  1779. XWindowAttributes wa;
  1780.  
  1781. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1782. for(i = 0; i < num; i++) {
  1783. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  1784. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1785. continue;
  1786. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1787. manage(wins[i], &wa);
  1788. }
  1789. for(i = 0; i < num; i++) { /* now the transients */
  1790. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1791. continue;
  1792. if(XGetTransientForHint(dpy, wins[i], &d1)
  1793. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1794. manage(wins[i], &wa);
  1795. }
  1796. if(wins)
  1797. XFree(wins);
  1798. }
  1799. }
  1800.  
  1801. void
  1802. sendmon(Client *c, Monitor *m) {
  1803. if(c->mon == m)
  1804. return;
  1805. unfocus(c, True);
  1806. detach(c);
  1807. detachstack(c);
  1808. c->mon = m;
  1809. c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
  1810. attach(c);
  1811. attachstack(c);
  1812. focus(NULL);
  1813. arrange(NULL);
  1814. }
  1815.  
  1816. void
  1817. setclientstate(Client *c, long state) {
  1818. long data[] = { state, None };
  1819.  
  1820. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1821. PropModeReplace, (unsigned char *)data, 2);
  1822. }
  1823.  
  1824. void
  1825. setlayout(const Arg *arg) {
  1826. if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
  1827. selmon->sellt ^= 1;
  1828. if(arg && arg->v)
  1829. selmon->lt[selmon->sellt] = selmon->lts[selmon->curtag] = (Layout *)arg->v;
  1830. strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
  1831. if(selmon->sel)
  1832. arrange(selmon);
  1833. else
  1834. drawbar(selmon);
  1835. }
  1836.  
  1837. void
  1838. incnmaster(const Arg *arg) {
  1839. int i;
  1840.  
  1841. if(!arg || !selmon->lt[selmon->sellt]->arrange)
  1842. return;
  1843. i = selmon->nmasters[selmon->curtag] + arg->i;
  1844. selmon->nmasters[selmon->curtag] = i < 0 ? 0 : i;
  1845. arrange(selmon);
  1846. }
  1847.  
  1848. void
  1849. setnmaster(const Arg *arg) {
  1850. if(!arg || !selmon->lt[selmon->sellt]->arrange)
  1851. return;
  1852. selmon->nmasters[selmon->curtag] = arg->i > 0 ? arg->i : 0;
  1853. arrange(selmon);
  1854. }
  1855.  
  1856. /* arg > 1.0 will set mfact absolutly */
  1857. void
  1858. setmfact(const Arg *arg) {
  1859. float f;
  1860.  
  1861. if(!arg || !selmon->lt[selmon->sellt]->arrange)
  1862. return;
  1863. f = arg->f < 1.0 ? arg->f + selmon->mfacts[selmon->curtag] : arg->f - 1.0;
  1864. if(f < 0.1 || f > 0.9)
  1865. return;
  1866. selmon->mfacts[selmon->curtag] = f;
  1867. arrange(selmon);
  1868. }
  1869.  
  1870. void
  1871. setup(void) {
  1872. XSetWindowAttributes wa;
  1873.  
  1874. /* clean up any zombies immediately */
  1875. sigchld(0);
  1876.  
  1877. /* init screen */
  1878. screen = DefaultScreen(dpy);
  1879. root = RootWindow(dpy, screen);
  1880. initfont(font);
  1881. sw = DisplayWidth(dpy, screen);
  1882. sh = DisplayHeight(dpy, screen);
  1883. bh = dc.h = dc.font.height + 2;
  1884. updategeom();
  1885. /* init atoms */
  1886. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1887. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1888. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1889. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1890. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1891. netatom[NetWMWindowOpacity] = XInternAtom(dpy, "_NET_WM_WINDOW_OPACITY", False);
  1892. netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
  1893. netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
  1894. /* init cursors */
  1895. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  1896. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  1897. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  1898. /* init appearance */
  1899. unsigned int c;
  1900. for(c=0; c<NumColors; c++) {
  1901. barcolors[c][ColBorder] = getcolor( colors[c][ColBorder] );
  1902. barcolors[c][ColFG] = getcolor( colors[c][ColFG] );
  1903. barcolors[c][ColBG] = getcolor( colors[c][ColBG] );
  1904. }
  1905. dc.norm[ColBorder] = getcolor(normbordercolor);
  1906. dc.norm[ColBG] = getcolor(normbgcolor);
  1907. dc.norm[ColFG] = getcolor(normfgcolor);
  1908. dc.sel[ColBorder] = getcolor(selbordercolor);
  1909. dc.sel[ColBG] = getcolor(selbgcolor);
  1910. dc.sel[ColFG] = getcolor(selfgcolor);
  1911. dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
  1912. dc.gc = XCreateGC(dpy, root, 0, NULL);
  1913. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1914. if(!dc.font.set)
  1915. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  1916. /* init bars */
  1917. updatebars();
  1918. updatestatus();
  1919. /* EWMH support per view */
  1920. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1921. PropModeReplace, (unsigned char *) netatom, NetLast);
  1922. /* select for events */
  1923. wa.cursor = cursor[CurNormal];
  1924. wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask
  1925. |EnterWindowMask|LeaveWindowMask|StructureNotifyMask
  1926. |PropertyChangeMask;
  1927. XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
  1928. XSelectInput(dpy, root, wa.event_mask);
  1929. grabkeys();
  1930. }
  1931.  
  1932. void
  1933. showhide(Client *c) {
  1934. if(!c)
  1935. return;
  1936. if(ISVISIBLE(c)) { /* show clients top down */
  1937. if(c->needresize) {
  1938. c->needresize=0;
  1939. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  1940. } else {
  1941. XMoveWindow(dpy, c->win, c->x, c->y);
  1942. }
  1943. if(!c->mon->lt[c->mon->sellt]->arrange || c->isfloating)
  1944. resize(c, c->x, c->y, c->w, c->h, False);
  1945. showhide(c->snext);
  1946. }
  1947. else { /* hide clients bottom up */
  1948. showhide(c->snext);
  1949. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  1950. }
  1951. }
  1952.  
  1953.  
  1954. void
  1955. sigchld(int unused) {
  1956. if(signal(SIGCHLD, sigchld) == SIG_ERR)
  1957. die("Can't install SIGCHLD handler");
  1958. while(0 < waitpid(-1, NULL, WNOHANG));
  1959. }
  1960.  
  1961. void
  1962. spawn(const Arg *arg) {
  1963. if(fork() == 0) {
  1964. if(dpy)
  1965. close(ConnectionNumber(dpy));
  1966. setsid();
  1967. execvp(((char **)arg->v)[0], (char **)arg->v);
  1968. fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
  1969. perror(" failed");
  1970. exit(0);
  1971. }
  1972. }
  1973.  
  1974. void
  1975. tag(const Arg *arg) {
  1976. if(selmon->sel && arg->ui & TAGMASK) {
  1977. selmon->sel->tags = arg->ui & TAGMASK;
  1978. arrange(selmon);
  1979. }
  1980. }
  1981.  
  1982. void
  1983. tagmon(const Arg *arg) {
  1984. if(!selmon->sel || !mons->next)
  1985. return;
  1986. sendmon(selmon->sel, dirtomon(arg->i));
  1987. }
  1988.  
  1989. int
  1990. textnw(const char *text, unsigned int len) {
  1991. XRectangle r;
  1992.  
  1993. if(dc.font.set) {
  1994. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  1995. return r.width;
  1996. }
  1997. return XTextWidth(dc.font.xfont, text, len);
  1998. }
  1999.  
  2000. void
  2001. ntile(Monitor *m) {
  2002. int x, y, h, w, mw, nmax, nm;
  2003. unsigned int i, n;
  2004. Client *c;
  2005.  
  2006. snprintf(m->ltsymbol, sizeof m->ltsymbol, "=%d]", m->nmasters[m->curtag]);
  2007. for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
  2008. c = nexttiled(m->clients);
  2009. nmax = m->nmasters[m->curtag];
  2010. nm = nmax == 1 ? 1 : MIN(n / 2, nmax);
  2011. if(nm > n)
  2012. nm = n;
  2013. /* master */
  2014. if(nm > 0) {
  2015. mw = selmon->mfacts[selmon->curtag] * m->ww;
  2016. h = m->wh / nm;
  2017. if(h < bh)
  2018. h = m->wh;
  2019. y = m->wy;
  2020. for(i = 0; i < nm; i++, c = nexttiled(c->next)) {
  2021. resize(c, m->wx, y, (n == nm ? m->ww : mw) - 2 * c->bw,
  2022. ((i + 1 == nm) ? m->wy + m->wh - y : h) - 2 * c->bw, False);
  2023. if(h != m->wh)
  2024. y = c->y + HEIGHT(c);
  2025. }
  2026. n -= nm;
  2027. } else
  2028. mw = 0;
  2029. if(n == 0)
  2030. return;
  2031. /* tile stack */
  2032. x = m->wx + mw;
  2033. y = m->wy;
  2034. w = m->ww - mw;
  2035. h = m->wh / n;
  2036. if(h < bh)
  2037. h = m->wh;
  2038. for(i = 0; c; c = nexttiled(c->next), i++) {
  2039. resize(c, x, y, w - 2 * c->bw,
  2040. ((i + 1 == n) ? m->wy + m->wh - y : h) - 2 * c->bw, False);
  2041. if(h != m->wh)
  2042. y = c->y + HEIGHT(c);
  2043. }
  2044. }
  2045.  
  2046. void
  2047. togglebar(const Arg *arg) {
  2048. selmon->showbars[selmon->curtag] = !selmon->showbars[selmon->curtag];
  2049. updatebarpos(selmon);
  2050. XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
  2051. arrange(selmon);
  2052. }
  2053.  
  2054. void
  2055. togglefloating(const Arg *arg) {
  2056. if(!selmon->sel)
  2057. return;
  2058. selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
  2059. if(selmon->sel->isfloating)
  2060. resize(selmon->sel, selmon->sel->x, selmon->sel->y,
  2061. selmon->sel->w, selmon->sel->h, False);
  2062. arrange(selmon);
  2063. }
  2064.  
  2065. void
  2066. toggletag(const Arg *arg) {
  2067. unsigned int newtags;
  2068. unsigned int i;
  2069.  
  2070. if(!selmon->sel)
  2071. return;
  2072. newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
  2073. if(newtags) {
  2074. selmon->sel->tags = newtags;
  2075. if(newtags == ~0) {
  2076. selmon->prevtag = selmon->curtag;
  2077. selmon->curtag = 0;
  2078. }
  2079. if(!(newtags & 1 << (selmon->curtag - 1))) {
  2080. selmon->prevtag = selmon->curtag;
  2081. for (i=0; !(newtags & 1 << i); i++);
  2082. selmon->curtag = i + 1;
  2083. }
  2084. selmon->sel->tags = newtags;
  2085. selmon->lt[selmon->sellt] = selmon->lts[selmon->curtag];
  2086. arrange(selmon);
  2087. }
  2088. }
  2089.  
  2090. void
  2091. toggleview(const Arg *arg) {
  2092. unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
  2093.  
  2094. if(newtagset) {
  2095. selmon->tagset[selmon->seltags] = newtagset;
  2096. arrange(selmon);
  2097. }
  2098. }
  2099.  
  2100. void
  2101. unfocus(Client *c, Bool setfocus) {
  2102. if(!c)
  2103. return;
  2104. grabbuttons(c, False);
  2105. XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
  2106. if(setfocus)
  2107. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  2108. }
  2109.  
  2110. void
  2111. unmanage(Client *c, Bool destroyed) {
  2112. Monitor *m = c->mon;
  2113. XWindowChanges wc;
  2114.  
  2115. /* The server grab construct avoids race conditions. */
  2116. detach(c);
  2117. detachstack(c);
  2118. if(!destroyed) {
  2119. wc.border_width = c->oldbw;
  2120. XGrabServer(dpy);
  2121. XSetErrorHandler(xerrordummy);
  2122. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  2123. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  2124. setclientstate(c, WithdrawnState);
  2125. XSync(dpy, False);
  2126. XSetErrorHandler(xerror);
  2127. XUngrabServer(dpy);
  2128. }
  2129. free(c);
  2130. focus(NULL);
  2131. arrange(m);
  2132. }
  2133.  
  2134. void
  2135. unmapnotify(XEvent *e) {
  2136. Client *c;
  2137. XUnmapEvent *ev = &e->xunmap;
  2138.  
  2139. if((c = wintoclient(ev->window)))
  2140. unmanage(c, False);
  2141. }
  2142.  
  2143. void
  2144. updatebars(void) {
  2145. Monitor *m;
  2146. XSetWindowAttributes wa;
  2147.  
  2148. wa.override_redirect = True;
  2149. wa.background_pixmap = ParentRelative;
  2150. wa.event_mask = ButtonPressMask|ExposureMask;
  2151. for(m = mons; m; m = m->next) {
  2152. m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
  2153. CopyFromParent, DefaultVisual(dpy, screen),
  2154. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  2155. XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
  2156. XMapRaised(dpy, m->barwin);
  2157. }
  2158. }
  2159.  
  2160. void
  2161. updatebarpos(Monitor *m) {
  2162. m->wy = m->my;
  2163. m->wh = m->mh;
  2164. if(m->showbars[m->curtag]) {
  2165. m->wh -= bh;
  2166. m->by = m->topbars[m->curtag] ? m->wy : m->wy + m->wh;
  2167. m->wy = m->topbars[m->curtag] ? m->wy + bh : m->wy;
  2168. }
  2169. else
  2170. m->by = -bh;
  2171. }
  2172.  
  2173. Bool
  2174. updategeom(void) {
  2175. Bool dirty = False;
  2176.  
  2177. #ifdef XINERAMA
  2178. if(XineramaIsActive(dpy)) {
  2179. int i, j, n, nn;
  2180. Client *c;
  2181. Monitor *m;
  2182. XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
  2183. XineramaScreenInfo *unique = NULL;
  2184.  
  2185. info = XineramaQueryScreens(dpy, &nn);
  2186. for(n = 0, m = mons; m; m = m->next, n++);
  2187. /* only consider unique geometries as separate screens */
  2188. if(!(unique = (XineramaScreenInfo *)malloc(sizeof(XineramaScreenInfo) * nn)))
  2189. die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo) * nn);
  2190. for(i = 0, j = 0; i < nn; i++)
  2191. if(isuniquegeom(unique, j, &info[i]))
  2192. memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
  2193. XFree(info);
  2194. nn = j;
  2195. if(n <= nn) {
  2196. for(i = 0; i < (nn - n); i++) { /* new monitors available */
  2197. for(m = mons; m && m->next; m = m->next);
  2198. if(m)
  2199. m->next = createmon();
  2200. else
  2201. mons = createmon();
  2202. }
  2203. for(i = 0, m = mons; i < nn && m; m = m->next, i++)
  2204. if(i >= n
  2205. || (unique[i].x_org != m->mx || unique[i].y_org != m->my
  2206. || unique[i].width != m->mw || unique[i].height != m->mh))
  2207. {
  2208. dirty = True;
  2209. m->num = i;
  2210. m->mx = m->wx = unique[i].x_org;
  2211. m->my = m->wy = unique[i].y_org;
  2212. m->mw = m->ww = unique[i].width;
  2213. m->mh = m->wh = unique[i].height;
  2214. updatebarpos(m);
  2215. }
  2216. }
  2217. else { /* less monitors available nn < n */
  2218. for(i = nn; i < n; i++) {
  2219. for(m = mons; m && m->next; m = m->next);
  2220. while(m->clients) {
  2221. dirty = True;
  2222. c = m->clients;
  2223. m->clients = c->next;
  2224. detachstack(c);
  2225. c->mon = mons;
  2226. attach(c);
  2227. attachstack(c);
  2228. }
  2229. if(m == selmon)
  2230. selmon = mons;
  2231. cleanupmon(m);
  2232. }
  2233. }
  2234. free(unique);
  2235. }
  2236. else
  2237. #endif /* XINERAMA */
  2238. /* default monitor setup */
  2239. {
  2240. if(!mons)
  2241. mons = createmon();
  2242. if(mons->mw != sw || mons->mh != sh) {
  2243. dirty = True;
  2244. mons->mw = mons->ww = sw;
  2245. mons->mh = mons->wh = sh;
  2246. updatebarpos(mons);
  2247. }
  2248. }
  2249. if(dirty) {
  2250. selmon = mons;
  2251. selmon = wintomon(root);
  2252. }
  2253. return dirty;
  2254. }
  2255.  
  2256. void
  2257. updatenumlockmask(void) {
  2258. unsigned int i, j;
  2259. XModifierKeymap *modmap;
  2260.  
  2261. numlockmask = 0;
  2262. modmap = XGetModifierMapping(dpy);
  2263. for(i = 0; i < 8; i++)
  2264. for(j = 0; j < modmap->max_keypermod; j++)
  2265. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  2266. == XKeysymToKeycode(dpy, XK_Num_Lock))
  2267. numlockmask = (1 << i);
  2268. XFreeModifiermap(modmap);
  2269. }
  2270.  
  2271. void
  2272. updatesizehints(Client *c) {
  2273. long msize;
  2274. XSizeHints size;
  2275.  
  2276. if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
  2277. /* size is uninitialized, ensure that size.flags aren't used */
  2278. size.flags = PSize;
  2279. if(size.flags & PBaseSize) {
  2280. c->basew = size.base_width;
  2281. c->baseh = size.base_height;
  2282. }
  2283. else if(size.flags & PMinSize) {
  2284. c->basew = size.min_width;
  2285. c->baseh = size.min_height;
  2286. }
  2287. else
  2288. c->basew = c->baseh = 0;
  2289. if(size.flags & PResizeInc) {
  2290. c->incw = size.width_inc;
  2291. c->inch = size.height_inc;
  2292. }
  2293. else
  2294. c->incw = c->inch = 0;
  2295. if(size.flags & PMaxSize) {
  2296. c->maxw = size.max_width;
  2297. c->maxh = size.max_height;
  2298. }
  2299. else
  2300. c->maxw = c->maxh = 0;
  2301. if(size.flags & PMinSize) {
  2302. c->minw = size.min_width;
  2303. c->minh = size.min_height;
  2304. }
  2305. else if(size.flags & PBaseSize) {
  2306. c->minw = size.base_width;
  2307. c->minh = size.base_height;
  2308. }
  2309. else
  2310. c->minw = c->minh = 0;
  2311. if(size.flags & PAspect) {
  2312. c->mina = (float)size.min_aspect.y / size.min_aspect.x;
  2313. c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
  2314. }
  2315. else
  2316. c->maxa = c->mina = 0.0;
  2317. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  2318. && c->maxw == c->minw && c->maxh == c->minh);
  2319. }
  2320.  
  2321. void
  2322. updatetitle(Client *c) {
  2323. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  2324. gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
  2325. if(c->name[0] == '\0') /* hack to mark broken clients */
  2326. strcpy(c->name, broken);
  2327. }
  2328.  
  2329. void
  2330. updatestatus(void) {
  2331. if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
  2332. strcpy(stext, "dwm-"VERSION);
  2333. drawbar(selmon);
  2334. }
  2335.  
  2336. void
  2337. updatewmhints(Client *c) {
  2338. XWMHints *wmh;
  2339.  
  2340. if((wmh = XGetWMHints(dpy, c->win))) {
  2341. if(c == selmon->sel && wmh->flags & XUrgencyHint) {
  2342. wmh->flags &= ~XUrgencyHint;
  2343. XSetWMHints(dpy, c->win, wmh);
  2344. }
  2345. else
  2346. c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
  2347. XFree(wmh);
  2348. }
  2349. }
  2350.  
  2351. void
  2352. view(const Arg *arg) {
  2353. unsigned int i;
  2354.  
  2355. if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
  2356. return;
  2357. selmon->seltags ^= 1; /* toggle sel tagset */
  2358. if(arg->ui & TAGMASK) {
  2359. selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
  2360. selmon->prevtag = selmon->curtag;
  2361. if(arg->ui == ~0)
  2362. selmon->curtag = 0;
  2363. else {
  2364. for (i=0; !(arg->ui & 1 << i); i++);
  2365. selmon->curtag = i + 1;
  2366. }
  2367. } else {
  2368. selmon->prevtag= selmon->curtag ^ selmon->prevtag;
  2369. selmon->curtag^= selmon->prevtag;
  2370. selmon->prevtag= selmon->curtag ^ selmon->prevtag;
  2371. }
  2372. selmon->lt[selmon->sellt] = selmon->lts[selmon->curtag ? selmon->curtag : selmon->prevtag];
  2373. if(selmon->showbars[selmon->curtag] != selmon->showbars[selmon->prevtag] || selmon->topbars[selmon->curtag] != selmon->topbars[selmon->prevtag]) {
  2374. updatebarpos(selmon);
  2375. XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
  2376. }
  2377. arrange(selmon);
  2378. }
  2379.  
  2380. Client *
  2381. wintoclient(Window w) {
  2382. Client *c;
  2383. Monitor *m;
  2384.  
  2385. for(m = mons; m; m = m->next)
  2386. for(c = m->clients; c; c = c->next)
  2387. if(c->win == w)
  2388. return c;
  2389. return NULL;
  2390. }
  2391.  
  2392. Monitor *
  2393. wintomon(Window w) {
  2394. int x, y;
  2395. Client *c;
  2396. Monitor *m;
  2397.  
  2398. if(w == root && getrootptr(&x, &y))
  2399. return ptrtomon(x, y);
  2400. for(m = mons; m; m = m->next)
  2401. if(w == m->barwin)
  2402. return m;
  2403. if((c = wintoclient(w)))
  2404. return c->mon;
  2405. return selmon;
  2406. }
  2407.  
  2408. /* There's no way to check accesses to destroyed windows, thus those cases are
  2409. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  2410. * default error handler, which may call exit. */
  2411. int
  2412. xerror(Display *dpy, XErrorEvent *ee) {
  2413. if(ee->error_code == BadWindow
  2414. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  2415. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  2416. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  2417. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  2418. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  2419. || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
  2420. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  2421. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  2422. return 0;
  2423. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  2424. ee->request_code, ee->error_code);
  2425. return xerrorxlib(dpy, ee); /* may call exit */
  2426. }
  2427.  
  2428. int
  2429. xerrordummy(Display *dpy, XErrorEvent *ee) {
  2430. return 0;
  2431. }
  2432.  
  2433. /* Startup Error handler to check if another window manager
  2434. * is already running. */
  2435. int
  2436. xerrorstart(Display *dpy, XErrorEvent *ee) {
  2437. otherwm = True;
  2438. return -1;
  2439. }
  2440.  
  2441. void
  2442. zoom(const Arg *arg) {
  2443. Client *c = selmon->sel;
  2444.  
  2445. if(!selmon->lt[selmon->sellt]->arrange
  2446. || selmon->lt[selmon->sellt]->arrange == monocle
  2447. || (selmon->sel && selmon->sel->isfloating))
  2448. return;
  2449. if(c == nexttiled(selmon->clients))
  2450. if(!c || !(c = nexttiled(c->next)))
  2451. return;
  2452. detach(c);
  2453. attach(c);
  2454. focus(c);
  2455. arrange(c->mon);
  2456. }
  2457.  
  2458. int
  2459. main(int argc, char *argv[]) {
  2460. if(argc == 2 && !strcmp("-v", argv[1]))
  2461. die("dwm-"VERSION", © 2006-2010 dwm engineers, see LICENSE for details\n");
  2462. else if(argc != 1)
  2463. die("usage: dwm [-v]\n");
  2464. if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  2465. fputs("warning: no locale support\n", stderr);
  2466. if(!(dpy = XOpenDisplay(NULL)))
  2467. die("dwm: cannot open display\n");
  2468. checkotherwm();
  2469. setup();
  2470. scan();
  2471. run();
  2472. cleanup();
  2473. XCloseDisplay(dpy);
  2474. return exitcode;
  2475. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement