Advertisement
bvn13

Untitled

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