Advertisement
Guest User

Untitled

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