dwm.c 51 KB

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