Guest User

Untitled

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