dwm.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068
  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. m = ecalloc(1, sizeof(Monitor));
  592. m->tagset[0] = m->tagset[1] = 1;
  593. m->mfact = mfact;
  594. m->nmaster = nmaster;
  595. m->showbar = showbar;
  596. m->topbar = topbar;
  597. m->lt[0] = &layouts[0];
  598. m->lt[1] = &layouts[1 % LENGTH(layouts)];
  599. strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
  600. return m;
  601. }
  602. void
  603. destroynotify(XEvent *e) {
  604. Client *c;
  605. XDestroyWindowEvent *ev = &e->xdestroywindow;
  606. if((c = wintoclient(ev->window)))
  607. unmanage(c, True);
  608. }
  609. void
  610. detach(Client *c) {
  611. Client **tc;
  612. for(tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
  613. *tc = c->next;
  614. }
  615. void
  616. detachstack(Client *c) {
  617. Client **tc, *t;
  618. for(tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
  619. *tc = c->snext;
  620. if(c == c->mon->sel) {
  621. for(t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
  622. c->mon->sel = t;
  623. }
  624. }
  625. Monitor *
  626. dirtomon(int dir) {
  627. Monitor *m = NULL;
  628. if(dir > 0) {
  629. if(!(m = selmon->next))
  630. m = mons;
  631. }
  632. else if(selmon == mons)
  633. for(m = mons; m->next; m = m->next);
  634. else
  635. for(m = mons; m->next != selmon; m = m->next);
  636. return m;
  637. }
  638. void
  639. drawbar(Monitor *m) {
  640. int x, xx, w;
  641. unsigned int i, occ = 0, urg = 0;
  642. Client *c;
  643. for(c = m->clients; c; c = c->next) {
  644. occ |= c->tags;
  645. if(c->isurgent)
  646. urg |= c->tags;
  647. }
  648. x = 0;
  649. for(i = 0; i < LENGTH(tags); i++) {
  650. w = TEXTW(tags[i]);
  651. drw_setscheme(drw, m->tagset[m->seltags] & 1 << i ? &scheme[SchemeSel] : &scheme[SchemeNorm]);
  652. drw_text(drw, x, 0, w, bh, tags[i], urg & 1 << i);
  653. drw_rect(drw, x, 0, w, bh, m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
  654. occ & 1 << i, urg & 1 << i);
  655. x += w;
  656. }
  657. w = blw = TEXTW(m->ltsymbol);
  658. drw_setscheme(drw, &scheme[SchemeNorm]);
  659. drw_text(drw, x, 0, w, bh, m->ltsymbol, 0);
  660. x += w;
  661. xx = x;
  662. if(m == selmon) { /* status is only drawn on selected monitor */
  663. w = TEXTW(stext);
  664. x = m->ww - w;
  665. if(x < xx) {
  666. x = xx;
  667. w = m->ww - xx;
  668. }
  669. drw_text(drw, x, 0, w, bh, stext, 0);
  670. }
  671. else
  672. x = m->ww;
  673. if((w = x - xx) > bh) {
  674. x = xx;
  675. if(m->sel) {
  676. drw_setscheme(drw, m == selmon ? &scheme[SchemeSel] : &scheme[SchemeNorm]);
  677. drw_text(drw, x, 0, w, bh, m->sel->name, 0);
  678. drw_rect(drw, x, 0, w, bh, m->sel->isfixed, m->sel->isfloating, 0);
  679. }
  680. else {
  681. drw_setscheme(drw, &scheme[SchemeNorm]);
  682. drw_text(drw, x, 0, w, bh, NULL, 0);
  683. }
  684. }
  685. drw_map(drw, m->barwin, 0, 0, m->ww, bh);
  686. }
  687. void
  688. drawbars(void) {
  689. Monitor *m;
  690. for(m = mons; m; m = m->next)
  691. drawbar(m);
  692. }
  693. void
  694. enternotify(XEvent *e) {
  695. Client *c;
  696. Monitor *m;
  697. XCrossingEvent *ev = &e->xcrossing;
  698. if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
  699. return;
  700. c = wintoclient(ev->window);
  701. m = c ? c->mon : wintomon(ev->window);
  702. if(m != selmon) {
  703. unfocus(selmon->sel, True);
  704. selmon = m;
  705. }
  706. else if(!c || c == selmon->sel)
  707. return;
  708. focus(c);
  709. }
  710. void
  711. expose(XEvent *e) {
  712. Monitor *m;
  713. XExposeEvent *ev = &e->xexpose;
  714. if(ev->count == 0 && (m = wintomon(ev->window)))
  715. drawbar(m);
  716. }
  717. void
  718. focus(Client *c) {
  719. if(!c || !ISVISIBLE(c))
  720. for(c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
  721. /* was if(selmon->sel) */
  722. if(selmon->sel && selmon->sel != c)
  723. unfocus(selmon->sel, False);
  724. if(c) {
  725. if(c->mon != selmon)
  726. selmon = c->mon;
  727. if(c->isurgent)
  728. clearurgent(c);
  729. detachstack(c);
  730. attachstack(c);
  731. grabbuttons(c, True);
  732. XSetWindowBorder(dpy, c->win, scheme[SchemeSel].border->pix);
  733. setfocus(c);
  734. }
  735. else {
  736. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  737. XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
  738. }
  739. selmon->sel = c;
  740. drawbars();
  741. }
  742. void
  743. focusin(XEvent *e) { /* there are some broken focus acquiring clients */
  744. XFocusChangeEvent *ev = &e->xfocus;
  745. if(selmon->sel && ev->window != selmon->sel->win)
  746. setfocus(selmon->sel);
  747. }
  748. void
  749. focusmon(const Arg *arg) {
  750. Monitor *m;
  751. if(!mons->next)
  752. return;
  753. if((m = dirtomon(arg->i)) == selmon)
  754. return;
  755. unfocus(selmon->sel, False); /* s/True/False/ fixes input focus issues
  756. in gedit and anjuta */
  757. selmon = m;
  758. focus(NULL);
  759. }
  760. void
  761. focusstack(const Arg *arg) {
  762. Client *c = NULL, *i;
  763. if(!selmon->sel)
  764. return;
  765. if(arg->i > 0) {
  766. for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
  767. if(!c)
  768. for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
  769. }
  770. else {
  771. for(i = selmon->clients; i != selmon->sel; i = i->next)
  772. if(ISVISIBLE(i))
  773. c = i;
  774. if(!c)
  775. for(; i; i = i->next)
  776. if(ISVISIBLE(i))
  777. c = i;
  778. }
  779. if(c) {
  780. focus(c);
  781. restack(selmon);
  782. }
  783. }
  784. Atom
  785. getatomprop(Client *c, Atom prop) {
  786. int di;
  787. unsigned long dl;
  788. unsigned char *p = NULL;
  789. Atom da, atom = None;
  790. if(XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
  791. &da, &di, &dl, &dl, &p) == Success && p) {
  792. atom = *(Atom *)p;
  793. XFree(p);
  794. }
  795. return atom;
  796. }
  797. Bool
  798. getrootptr(int *x, int *y) {
  799. int di;
  800. unsigned int dui;
  801. Window dummy;
  802. return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
  803. }
  804. long
  805. getstate(Window w) {
  806. int format;
  807. long result = -1;
  808. unsigned char *p = NULL;
  809. unsigned long n, extra;
  810. Atom real;
  811. if(XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
  812. &real, &format, &n, &extra, (unsigned char **)&p) != Success)
  813. return -1;
  814. if(n != 0)
  815. result = *p;
  816. XFree(p);
  817. return result;
  818. }
  819. Bool
  820. gettextprop(Window w, Atom atom, char *text, unsigned int size) {
  821. char **list = NULL;
  822. int n;
  823. XTextProperty name;
  824. if(!text || size == 0)
  825. return False;
  826. text[0] = '\0';
  827. XGetTextProperty(dpy, w, &name, atom);
  828. if(!name.nitems)
  829. return False;
  830. if(name.encoding == XA_STRING)
  831. strncpy(text, (char *)name.value, size - 1);
  832. else {
  833. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
  834. strncpy(text, *list, size - 1);
  835. XFreeStringList(list);
  836. }
  837. }
  838. text[size - 1] = '\0';
  839. XFree(name.value);
  840. return True;
  841. }
  842. void
  843. grabbuttons(Client *c, Bool focused) {
  844. updatenumlockmask();
  845. {
  846. unsigned int i, j;
  847. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  848. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  849. if(focused) {
  850. for(i = 0; i < LENGTH(buttons); i++)
  851. if(buttons[i].click == ClkClientWin)
  852. for(j = 0; j < LENGTH(modifiers); j++)
  853. XGrabButton(dpy, buttons[i].button,
  854. buttons[i].mask | modifiers[j],
  855. c->win, False, BUTTONMASK,
  856. GrabModeAsync, GrabModeSync, None, None);
  857. }
  858. else
  859. XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
  860. BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
  861. }
  862. }
  863. void
  864. grabkeys(void) {
  865. updatenumlockmask();
  866. {
  867. unsigned int i, j;
  868. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  869. KeyCode code;
  870. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  871. for(i = 0; i < LENGTH(keys); i++)
  872. if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
  873. for(j = 0; j < LENGTH(modifiers); j++)
  874. XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
  875. True, GrabModeAsync, GrabModeAsync);
  876. }
  877. }
  878. void
  879. incnmaster(const Arg *arg) {
  880. selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
  881. arrange(selmon);
  882. }
  883. #ifdef XINERAMA
  884. static Bool
  885. isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info) {
  886. while(n--)
  887. if(unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
  888. && unique[n].width == info->width && unique[n].height == info->height)
  889. return False;
  890. return True;
  891. }
  892. #endif /* XINERAMA */
  893. void
  894. keypress(XEvent *e) {
  895. unsigned int i;
  896. KeySym keysym;
  897. XKeyEvent *ev;
  898. ev = &e->xkey;
  899. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  900. for(i = 0; i < LENGTH(keys); i++)
  901. if(keysym == keys[i].keysym
  902. && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
  903. && keys[i].func)
  904. keys[i].func(&(keys[i].arg));
  905. }
  906. void
  907. killclient(const Arg *arg) {
  908. if(!selmon->sel)
  909. return;
  910. if(!sendevent(selmon->sel, wmatom[WMDelete])) {
  911. XGrabServer(dpy);
  912. XSetErrorHandler(xerrordummy);
  913. XSetCloseDownMode(dpy, DestroyAll);
  914. XKillClient(dpy, selmon->sel->win);
  915. XSync(dpy, False);
  916. XSetErrorHandler(xerror);
  917. XUngrabServer(dpy);
  918. }
  919. }
  920. void
  921. manage(Window w, XWindowAttributes *wa) {
  922. Client *c, *t = NULL;
  923. Window trans = None;
  924. XWindowChanges wc;
  925. c = ecalloc(1, sizeof(Client));
  926. c->win = w;
  927. updatetitle(c);
  928. if(XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
  929. c->mon = t->mon;
  930. c->tags = t->tags;
  931. }
  932. else {
  933. c->mon = selmon;
  934. applyrules(c);
  935. }
  936. /* geometry */
  937. c->x = c->oldx = wa->x;
  938. c->y = c->oldy = wa->y;
  939. c->w = c->oldw = wa->width;
  940. c->h = c->oldh = wa->height;
  941. c->oldbw = wa->border_width;
  942. if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
  943. c->x = c->mon->mx + c->mon->mw - WIDTH(c);
  944. if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
  945. c->y = c->mon->my + c->mon->mh - HEIGHT(c);
  946. c->x = MAX(c->x, c->mon->mx);
  947. /* only fix client y-offset, if the client center might cover the bar */
  948. c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
  949. && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
  950. c->bw = borderpx;
  951. wc.border_width = c->bw;
  952. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  953. XSetWindowBorder(dpy, w, scheme[SchemeNorm].border->pix);
  954. configure(c); /* propagates border_width, if size doesn't change */
  955. updatewindowtype(c);
  956. updatesizehints(c);
  957. updatewmhints(c);
  958. XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
  959. grabbuttons(c, False);
  960. if(!c->isfloating)
  961. c->isfloating = c->oldstate = trans != None || c->isfixed;
  962. if(c->isfloating)
  963. XRaiseWindow(dpy, c->win);
  964. attach(c);
  965. attachstack(c);
  966. XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
  967. (unsigned char *) &(c->win), 1);
  968. XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
  969. setclientstate(c, NormalState);
  970. if (c->mon == selmon)
  971. unfocus(selmon->sel, False);
  972. c->mon->sel = c;
  973. arrange(c->mon);
  974. XMapWindow(dpy, c->win);
  975. focus(NULL);
  976. }
  977. void
  978. mappingnotify(XEvent *e) {
  979. XMappingEvent *ev = &e->xmapping;
  980. XRefreshKeyboardMapping(ev);
  981. if(ev->request == MappingKeyboard)
  982. grabkeys();
  983. }
  984. void
  985. maprequest(XEvent *e) {
  986. static XWindowAttributes wa;
  987. XMapRequestEvent *ev = &e->xmaprequest;
  988. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  989. return;
  990. if(wa.override_redirect)
  991. return;
  992. if(!wintoclient(ev->window))
  993. manage(ev->window, &wa);
  994. }
  995. void
  996. monocle(Monitor *m) {
  997. unsigned int n = 0;
  998. Client *c;
  999. for(c = m->clients; c; c = c->next)
  1000. if(ISVISIBLE(c))
  1001. n++;
  1002. if(n > 0) /* override layout symbol */
  1003. snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
  1004. for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
  1005. resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, False);
  1006. }
  1007. void
  1008. motionnotify(XEvent *e) {
  1009. static Monitor *mon = NULL;
  1010. Monitor *m;
  1011. XMotionEvent *ev = &e->xmotion;
  1012. if(ev->window != root)
  1013. return;
  1014. if((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
  1015. unfocus(selmon->sel, True);
  1016. selmon = m;
  1017. focus(NULL);
  1018. }
  1019. mon = m;
  1020. }
  1021. void
  1022. movemouse(const Arg *arg) {
  1023. int x, y, ocx, ocy, nx, ny;
  1024. Client *c;
  1025. Monitor *m;
  1026. XEvent ev;
  1027. Time lasttime = 0;
  1028. if(!(c = selmon->sel))
  1029. return;
  1030. if(c->isfullscreen) /* no support moving fullscreen windows by mouse */
  1031. return;
  1032. restack(selmon);
  1033. ocx = c->x;
  1034. ocy = c->y;
  1035. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1036. None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
  1037. return;
  1038. if(!getrootptr(&x, &y))
  1039. return;
  1040. do {
  1041. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1042. switch(ev.type) {
  1043. case ConfigureRequest:
  1044. case Expose:
  1045. case MapRequest:
  1046. handler[ev.type](&ev);
  1047. break;
  1048. case MotionNotify:
  1049. if ((ev.xmotion.time - lasttime) <= (1000 / 60))
  1050. continue;
  1051. lasttime = ev.xmotion.time;
  1052. nx = ocx + (ev.xmotion.x - x);
  1053. ny = ocy + (ev.xmotion.y - y);
  1054. if(nx >= selmon->wx && nx <= selmon->wx + selmon->ww
  1055. && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
  1056. if(abs(selmon->wx - nx) < snap)
  1057. nx = selmon->wx;
  1058. else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
  1059. nx = selmon->wx + selmon->ww - WIDTH(c);
  1060. if(abs(selmon->wy - ny) < snap)
  1061. ny = selmon->wy;
  1062. else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
  1063. ny = selmon->wy + selmon->wh - HEIGHT(c);
  1064. if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1065. && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
  1066. togglefloating(NULL);
  1067. }
  1068. if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
  1069. resize(c, nx, ny, c->w, c->h, True);
  1070. break;
  1071. }
  1072. } while(ev.type != ButtonRelease);
  1073. XUngrabPointer(dpy, CurrentTime);
  1074. if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
  1075. sendmon(c, m);
  1076. selmon = m;
  1077. focus(NULL);
  1078. }
  1079. }
  1080. Client *
  1081. nexttiled(Client *c) {
  1082. for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
  1083. return c;
  1084. }
  1085. void
  1086. pop(Client *c) {
  1087. detach(c);
  1088. attach(c);
  1089. focus(c);
  1090. arrange(c->mon);
  1091. }
  1092. void
  1093. propertynotify(XEvent *e) {
  1094. Client *c;
  1095. Window trans;
  1096. XPropertyEvent *ev = &e->xproperty;
  1097. if((ev->window == root) && (ev->atom == XA_WM_NAME))
  1098. updatestatus();
  1099. else if(ev->state == PropertyDelete)
  1100. return; /* ignore */
  1101. else if((c = wintoclient(ev->window))) {
  1102. switch(ev->atom) {
  1103. default: break;
  1104. case XA_WM_TRANSIENT_FOR:
  1105. if(!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
  1106. (c->isfloating = (wintoclient(trans)) != NULL))
  1107. arrange(c->mon);
  1108. break;
  1109. case XA_WM_NORMAL_HINTS:
  1110. updatesizehints(c);
  1111. break;
  1112. case XA_WM_HINTS:
  1113. updatewmhints(c);
  1114. drawbars();
  1115. break;
  1116. }
  1117. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  1118. updatetitle(c);
  1119. if(c == c->mon->sel)
  1120. drawbar(c->mon);
  1121. }
  1122. if(ev->atom == netatom[NetWMWindowType])
  1123. updatewindowtype(c);
  1124. }
  1125. }
  1126. void
  1127. quit(const Arg *arg) {
  1128. running = False;
  1129. }
  1130. Monitor *
  1131. recttomon(int x, int y, int w, int h) {
  1132. Monitor *m, *r = selmon;
  1133. int a, area = 0;
  1134. for(m = mons; m; m = m->next)
  1135. if((a = INTERSECT(x, y, w, h, m)) > area) {
  1136. area = a;
  1137. r = m;
  1138. }
  1139. return r;
  1140. }
  1141. void
  1142. resize(Client *c, int x, int y, int w, int h, Bool interact) {
  1143. if(applysizehints(c, &x, &y, &w, &h, interact))
  1144. resizeclient(c, x, y, w, h);
  1145. }
  1146. void
  1147. resizeclient(Client *c, int x, int y, int w, int h) {
  1148. XWindowChanges wc;
  1149. c->oldx = c->x; c->x = wc.x = x;
  1150. c->oldy = c->y; c->y = wc.y = y;
  1151. c->oldw = c->w; c->w = wc.width = w;
  1152. c->oldh = c->h; c->h = wc.height = h;
  1153. wc.border_width = c->bw;
  1154. XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  1155. configure(c);
  1156. XSync(dpy, False);
  1157. }
  1158. void
  1159. resizemouse(const Arg *arg) {
  1160. int ocx, ocy, nw, nh;
  1161. Client *c;
  1162. Monitor *m;
  1163. XEvent ev;
  1164. Time lasttime = 0;
  1165. if(!(c = selmon->sel))
  1166. return;
  1167. if(c->isfullscreen) /* no support resizing fullscreen windows by mouse */
  1168. return;
  1169. restack(selmon);
  1170. ocx = c->x;
  1171. ocy = c->y;
  1172. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1173. None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
  1174. return;
  1175. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1176. do {
  1177. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1178. switch(ev.type) {
  1179. case ConfigureRequest:
  1180. case Expose:
  1181. case MapRequest:
  1182. handler[ev.type](&ev);
  1183. break;
  1184. case MotionNotify:
  1185. if ((ev.xmotion.time - lasttime) <= (1000 / 60))
  1186. continue;
  1187. lasttime = ev.xmotion.time;
  1188. nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
  1189. nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
  1190. if(c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
  1191. && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
  1192. {
  1193. if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1194. && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
  1195. togglefloating(NULL);
  1196. }
  1197. if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
  1198. resize(c, c->x, c->y, nw, nh, True);
  1199. break;
  1200. }
  1201. } while(ev.type != ButtonRelease);
  1202. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1203. XUngrabPointer(dpy, CurrentTime);
  1204. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1205. if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
  1206. sendmon(c, m);
  1207. selmon = m;
  1208. focus(NULL);
  1209. }
  1210. }
  1211. void
  1212. restack(Monitor *m) {
  1213. Client *c;
  1214. XEvent ev;
  1215. XWindowChanges wc;
  1216. drawbar(m);
  1217. if(!m->sel)
  1218. return;
  1219. if(m->sel->isfloating || !m->lt[m->sellt]->arrange)
  1220. XRaiseWindow(dpy, m->sel->win);
  1221. if(m->lt[m->sellt]->arrange) {
  1222. wc.stack_mode = Below;
  1223. wc.sibling = m->barwin;
  1224. for(c = m->stack; c; c = c->snext)
  1225. if(!c->isfloating && ISVISIBLE(c)) {
  1226. XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
  1227. wc.sibling = c->win;
  1228. }
  1229. }
  1230. XSync(dpy, False);
  1231. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1232. }
  1233. void
  1234. run(void) {
  1235. XEvent ev;
  1236. /* main event loop */
  1237. XSync(dpy, False);
  1238. while(running && !XNextEvent(dpy, &ev))
  1239. if(handler[ev.type])
  1240. handler[ev.type](&ev); /* call handler */
  1241. }
  1242. void
  1243. scan(void) {
  1244. unsigned int i, num;
  1245. Window d1, d2, *wins = NULL;
  1246. XWindowAttributes wa;
  1247. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1248. for(i = 0; i < num; i++) {
  1249. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  1250. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1251. continue;
  1252. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1253. manage(wins[i], &wa);
  1254. }
  1255. for(i = 0; i < num; i++) { /* now the transients */
  1256. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1257. continue;
  1258. if(XGetTransientForHint(dpy, wins[i], &d1)
  1259. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1260. manage(wins[i], &wa);
  1261. }
  1262. if(wins)
  1263. XFree(wins);
  1264. }
  1265. }
  1266. void
  1267. sendmon(Client *c, Monitor *m) {
  1268. if(c->mon == m)
  1269. return;
  1270. unfocus(c, True);
  1271. detach(c);
  1272. detachstack(c);
  1273. c->mon = m;
  1274. c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
  1275. attach(c);
  1276. attachstack(c);
  1277. focus(NULL);
  1278. arrange(NULL);
  1279. }
  1280. void
  1281. setclientstate(Client *c, long state) {
  1282. long data[] = { state, None };
  1283. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1284. PropModeReplace, (unsigned char *)data, 2);
  1285. }
  1286. Bool
  1287. sendevent(Client *c, Atom proto) {
  1288. int n;
  1289. Atom *protocols;
  1290. Bool exists = False;
  1291. XEvent ev;
  1292. if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  1293. while(!exists && n--)
  1294. exists = protocols[n] == proto;
  1295. XFree(protocols);
  1296. }
  1297. if(exists) {
  1298. ev.type = ClientMessage;
  1299. ev.xclient.window = c->win;
  1300. ev.xclient.message_type = wmatom[WMProtocols];
  1301. ev.xclient.format = 32;
  1302. ev.xclient.data.l[0] = proto;
  1303. ev.xclient.data.l[1] = CurrentTime;
  1304. XSendEvent(dpy, c->win, False, NoEventMask, &ev);
  1305. }
  1306. return exists;
  1307. }
  1308. void
  1309. setfocus(Client *c) {
  1310. if(!c->neverfocus) {
  1311. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  1312. XChangeProperty(dpy, root, netatom[NetActiveWindow],
  1313. XA_WINDOW, 32, PropModeReplace,
  1314. (unsigned char *) &(c->win), 1);
  1315. }
  1316. sendevent(c, wmatom[WMTakeFocus]);
  1317. }
  1318. void
  1319. setfullscreen(Client *c, Bool fullscreen) {
  1320. if(fullscreen) {
  1321. XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
  1322. PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
  1323. c->isfullscreen = True;
  1324. c->oldstate = c->isfloating;
  1325. c->oldbw = c->bw;
  1326. c->bw = 0;
  1327. c->isfloating = True;
  1328. resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
  1329. XRaiseWindow(dpy, c->win);
  1330. }
  1331. else {
  1332. XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
  1333. PropModeReplace, (unsigned char*)0, 0);
  1334. c->isfullscreen = False;
  1335. c->isfloating = c->oldstate;
  1336. c->bw = c->oldbw;
  1337. c->x = c->oldx;
  1338. c->y = c->oldy;
  1339. c->w = c->oldw;
  1340. c->h = c->oldh;
  1341. resizeclient(c, c->x, c->y, c->w, c->h);
  1342. arrange(c->mon);
  1343. }
  1344. }
  1345. void
  1346. setlayout(const Arg *arg) {
  1347. if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
  1348. selmon->sellt ^= 1;
  1349. if(arg && arg->v)
  1350. selmon->lt[selmon->sellt] = (Layout *)arg->v;
  1351. strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
  1352. if(selmon->sel)
  1353. arrange(selmon);
  1354. else
  1355. drawbar(selmon);
  1356. }
  1357. /* arg > 1.0 will set mfact absolutly */
  1358. void
  1359. setmfact(const Arg *arg) {
  1360. float f;
  1361. if(!arg || !selmon->lt[selmon->sellt]->arrange)
  1362. return;
  1363. f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
  1364. if(f < 0.1 || f > 0.9)
  1365. return;
  1366. selmon->mfact = f;
  1367. arrange(selmon);
  1368. }
  1369. void
  1370. setup(void) {
  1371. XSetWindowAttributes wa;
  1372. /* clean up any zombies immediately */
  1373. sigchld(0);
  1374. /* init screen */
  1375. screen = DefaultScreen(dpy);
  1376. sw = DisplayWidth(dpy, screen);
  1377. sh = DisplayHeight(dpy, screen);
  1378. root = RootWindow(dpy, screen);
  1379. drw = drw_create(dpy, screen, root, sw, sh);
  1380. drw_load_fonts(drw, fonts, LENGTH(fonts));
  1381. if (!drw->fontcount)
  1382. die("no fonts could be loaded.\n");
  1383. bh = drw->fonts[0]->h + 2;
  1384. updategeom();
  1385. /* init atoms */
  1386. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1387. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1388. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1389. wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
  1390. netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
  1391. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1392. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1393. netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
  1394. netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
  1395. netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
  1396. netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
  1397. netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
  1398. /* init cursors */
  1399. cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
  1400. cursor[CurResize] = drw_cur_create(drw, XC_sizing);
  1401. cursor[CurMove] = drw_cur_create(drw, XC_fleur);
  1402. /* init appearance */
  1403. scheme[SchemeNorm].border = drw_clr_create(drw, normbordercolor);
  1404. scheme[SchemeNorm].bg = drw_clr_create(drw, normbgcolor);
  1405. scheme[SchemeNorm].fg = drw_clr_create(drw, normfgcolor);
  1406. scheme[SchemeSel].border = drw_clr_create(drw, selbordercolor);
  1407. scheme[SchemeSel].bg = drw_clr_create(drw, selbgcolor);
  1408. scheme[SchemeSel].fg = drw_clr_create(drw, selfgcolor);
  1409. /* init bars */
  1410. updatebars();
  1411. updatestatus();
  1412. /* EWMH support per view */
  1413. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1414. PropModeReplace, (unsigned char *) netatom, NetLast);
  1415. XDeleteProperty(dpy, root, netatom[NetClientList]);
  1416. /* select for events */
  1417. wa.cursor = cursor[CurNormal]->cursor;
  1418. wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask|PointerMotionMask
  1419. |EnterWindowMask|LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
  1420. XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
  1421. XSelectInput(dpy, root, wa.event_mask);
  1422. grabkeys();
  1423. focus(NULL);
  1424. }
  1425. void
  1426. showhide(Client *c) {
  1427. if(!c)
  1428. return;
  1429. if(ISVISIBLE(c)) { /* show clients top down */
  1430. XMoveWindow(dpy, c->win, c->x, c->y);
  1431. if((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
  1432. resize(c, c->x, c->y, c->w, c->h, False);
  1433. showhide(c->snext);
  1434. }
  1435. else { /* hide clients bottom up */
  1436. showhide(c->snext);
  1437. XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
  1438. }
  1439. }
  1440. void
  1441. sigchld(int unused) {
  1442. if(signal(SIGCHLD, sigchld) == SIG_ERR)
  1443. die("can't install SIGCHLD handler:");
  1444. while(0 < waitpid(-1, NULL, WNOHANG));
  1445. }
  1446. void
  1447. spawn(const Arg *arg) {
  1448. if(arg->v == dmenucmd)
  1449. dmenumon[0] = '0' + selmon->num;
  1450. if(fork() == 0) {
  1451. if(dpy)
  1452. close(ConnectionNumber(dpy));
  1453. setsid();
  1454. execvp(((char **)arg->v)[0], (char **)arg->v);
  1455. fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
  1456. perror(" failed");
  1457. exit(EXIT_SUCCESS);
  1458. }
  1459. }
  1460. void
  1461. tag(const Arg *arg) {
  1462. if(selmon->sel && arg->ui & TAGMASK) {
  1463. selmon->sel->tags = arg->ui & TAGMASK;
  1464. focus(NULL);
  1465. arrange(selmon);
  1466. }
  1467. }
  1468. void
  1469. tagmon(const Arg *arg) {
  1470. if(!selmon->sel || !mons->next)
  1471. return;
  1472. sendmon(selmon->sel, dirtomon(arg->i));
  1473. }
  1474. void
  1475. tile(Monitor *m) {
  1476. unsigned int i, n, h, mw, my, ty;
  1477. Client *c;
  1478. for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
  1479. if(n == 0)
  1480. return;
  1481. if(n > m->nmaster)
  1482. mw = m->nmaster ? m->ww * m->mfact : 0;
  1483. else
  1484. mw = m->ww;
  1485. for(i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
  1486. if(i < m->nmaster) {
  1487. h = (m->wh - my) / (MIN(n, m->nmaster) - i);
  1488. resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), False);
  1489. my += HEIGHT(c);
  1490. }
  1491. else {
  1492. h = (m->wh - ty) / (n - i);
  1493. resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), False);
  1494. ty += HEIGHT(c);
  1495. }
  1496. }
  1497. void
  1498. togglebar(const Arg *arg) {
  1499. selmon->showbar = !selmon->showbar;
  1500. updatebarpos(selmon);
  1501. XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
  1502. arrange(selmon);
  1503. }
  1504. void
  1505. togglefloating(const Arg *arg) {
  1506. if(!selmon->sel)
  1507. return;
  1508. if(selmon->sel->isfullscreen) /* no support for fullscreen windows */
  1509. return;
  1510. selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
  1511. if(selmon->sel->isfloating)
  1512. resize(selmon->sel, selmon->sel->x, selmon->sel->y,
  1513. selmon->sel->w, selmon->sel->h, False);
  1514. arrange(selmon);
  1515. }
  1516. void
  1517. toggletag(const Arg *arg) {
  1518. unsigned int newtags;
  1519. if(!selmon->sel)
  1520. return;
  1521. newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
  1522. if(newtags) {
  1523. selmon->sel->tags = newtags;
  1524. focus(NULL);
  1525. arrange(selmon);
  1526. }
  1527. }
  1528. void
  1529. toggleview(const Arg *arg) {
  1530. unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
  1531. if(newtagset) {
  1532. selmon->tagset[selmon->seltags] = newtagset;
  1533. focus(NULL);
  1534. arrange(selmon);
  1535. }
  1536. }
  1537. void
  1538. unfocus(Client *c, Bool setfocus) {
  1539. if(!c)
  1540. return;
  1541. grabbuttons(c, False);
  1542. XSetWindowBorder(dpy, c->win, scheme[SchemeNorm].border->pix);
  1543. if(setfocus) {
  1544. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  1545. XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
  1546. }
  1547. }
  1548. void
  1549. unmanage(Client *c, Bool destroyed) {
  1550. Monitor *m = c->mon;
  1551. XWindowChanges wc;
  1552. /* The server grab construct avoids race conditions. */
  1553. detach(c);
  1554. detachstack(c);
  1555. if(!destroyed) {
  1556. wc.border_width = c->oldbw;
  1557. XGrabServer(dpy);
  1558. XSetErrorHandler(xerrordummy);
  1559. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1560. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1561. setclientstate(c, WithdrawnState);
  1562. XSync(dpy, False);
  1563. XSetErrorHandler(xerror);
  1564. XUngrabServer(dpy);
  1565. }
  1566. free(c);
  1567. focus(NULL);
  1568. updateclientlist();
  1569. arrange(m);
  1570. }
  1571. void
  1572. unmapnotify(XEvent *e) {
  1573. Client *c;
  1574. XUnmapEvent *ev = &e->xunmap;
  1575. if((c = wintoclient(ev->window))) {
  1576. if(ev->send_event)
  1577. setclientstate(c, WithdrawnState);
  1578. else
  1579. unmanage(c, False);
  1580. }
  1581. }
  1582. void
  1583. updatebars(void) {
  1584. Monitor *m;
  1585. XSetWindowAttributes wa = {
  1586. .override_redirect = True,
  1587. .background_pixmap = ParentRelative,
  1588. .event_mask = ButtonPressMask|ExposureMask
  1589. };
  1590. for(m = mons; m; m = m->next) {
  1591. if (m->barwin)
  1592. continue;
  1593. m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
  1594. CopyFromParent, DefaultVisual(dpy, screen),
  1595. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  1596. XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
  1597. XMapRaised(dpy, m->barwin);
  1598. }
  1599. }
  1600. void
  1601. updatebarpos(Monitor *m) {
  1602. m->wy = m->my;
  1603. m->wh = m->mh;
  1604. if(m->showbar) {
  1605. m->wh -= bh;
  1606. m->by = m->topbar ? m->wy : m->wy + m->wh;
  1607. m->wy = m->topbar ? m->wy + bh : m->wy;
  1608. }
  1609. else
  1610. m->by = -bh;
  1611. }
  1612. void
  1613. updateclientlist() {
  1614. Client *c;
  1615. Monitor *m;
  1616. XDeleteProperty(dpy, root, netatom[NetClientList]);
  1617. for(m = mons; m; m = m->next)
  1618. for(c = m->clients; c; c = c->next)
  1619. XChangeProperty(dpy, root, netatom[NetClientList],
  1620. XA_WINDOW, 32, PropModeAppend,
  1621. (unsigned char *) &(c->win), 1);
  1622. }
  1623. Bool
  1624. updategeom(void) {
  1625. Bool dirty = False;
  1626. #ifdef XINERAMA
  1627. if(XineramaIsActive(dpy)) {
  1628. int i, j, n, nn;
  1629. Client *c;
  1630. Monitor *m;
  1631. XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
  1632. XineramaScreenInfo *unique = NULL;
  1633. for(n = 0, m = mons; m; m = m->next, n++);
  1634. /* only consider unique geometries as separate screens */
  1635. unique = ecalloc(nn, sizeof(XineramaScreenInfo));
  1636. for(i = 0, j = 0; i < nn; i++)
  1637. if(isuniquegeom(unique, j, &info[i]))
  1638. memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
  1639. XFree(info);
  1640. nn = j;
  1641. if(n <= nn) {
  1642. for(i = 0; i < (nn - n); i++) { /* new monitors available */
  1643. for(m = mons; m && m->next; m = m->next);
  1644. if(m)
  1645. m->next = createmon();
  1646. else
  1647. mons = createmon();
  1648. }
  1649. for(i = 0, m = mons; i < nn && m; m = m->next, i++)
  1650. if(i >= n
  1651. || (unique[i].x_org != m->mx || unique[i].y_org != m->my
  1652. || unique[i].width != m->mw || unique[i].height != m->mh))
  1653. {
  1654. dirty = True;
  1655. m->num = i;
  1656. m->mx = m->wx = unique[i].x_org;
  1657. m->my = m->wy = unique[i].y_org;
  1658. m->mw = m->ww = unique[i].width;
  1659. m->mh = m->wh = unique[i].height;
  1660. updatebarpos(m);
  1661. }
  1662. }
  1663. else { /* less monitors available nn < n */
  1664. for(i = nn; i < n; i++) {
  1665. for(m = mons; m && m->next; m = m->next);
  1666. while(m->clients) {
  1667. dirty = True;
  1668. c = m->clients;
  1669. m->clients = c->next;
  1670. detachstack(c);
  1671. c->mon = mons;
  1672. attach(c);
  1673. attachstack(c);
  1674. }
  1675. if(m == selmon)
  1676. selmon = mons;
  1677. cleanupmon(m);
  1678. }
  1679. }
  1680. free(unique);
  1681. }
  1682. else
  1683. #endif /* XINERAMA */
  1684. /* default monitor setup */
  1685. {
  1686. if(!mons)
  1687. mons = createmon();
  1688. if(mons->mw != sw || mons->mh != sh) {
  1689. dirty = True;
  1690. mons->mw = mons->ww = sw;
  1691. mons->mh = mons->wh = sh;
  1692. updatebarpos(mons);
  1693. }
  1694. }
  1695. if(dirty) {
  1696. selmon = mons;
  1697. selmon = wintomon(root);
  1698. }
  1699. return dirty;
  1700. }
  1701. void
  1702. updatenumlockmask(void) {
  1703. unsigned int i, j;
  1704. XModifierKeymap *modmap;
  1705. numlockmask = 0;
  1706. modmap = XGetModifierMapping(dpy);
  1707. for(i = 0; i < 8; i++)
  1708. for(j = 0; j < modmap->max_keypermod; j++)
  1709. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  1710. == XKeysymToKeycode(dpy, XK_Num_Lock))
  1711. numlockmask = (1 << i);
  1712. XFreeModifiermap(modmap);
  1713. }
  1714. void
  1715. updatesizehints(Client *c) {
  1716. long msize;
  1717. XSizeHints size;
  1718. if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
  1719. /* size is uninitialized, ensure that size.flags aren't used */
  1720. size.flags = PSize;
  1721. if(size.flags & PBaseSize) {
  1722. c->basew = size.base_width;
  1723. c->baseh = size.base_height;
  1724. }
  1725. else if(size.flags & PMinSize) {
  1726. c->basew = size.min_width;
  1727. c->baseh = size.min_height;
  1728. }
  1729. else
  1730. c->basew = c->baseh = 0;
  1731. if(size.flags & PResizeInc) {
  1732. c->incw = size.width_inc;
  1733. c->inch = size.height_inc;
  1734. }
  1735. else
  1736. c->incw = c->inch = 0;
  1737. if(size.flags & PMaxSize) {
  1738. c->maxw = size.max_width;
  1739. c->maxh = size.max_height;
  1740. }
  1741. else
  1742. c->maxw = c->maxh = 0;
  1743. if(size.flags & PMinSize) {
  1744. c->minw = size.min_width;
  1745. c->minh = size.min_height;
  1746. }
  1747. else if(size.flags & PBaseSize) {
  1748. c->minw = size.base_width;
  1749. c->minh = size.base_height;
  1750. }
  1751. else
  1752. c->minw = c->minh = 0;
  1753. if(size.flags & PAspect) {
  1754. c->mina = (float)size.min_aspect.y / size.min_aspect.x;
  1755. c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
  1756. }
  1757. else
  1758. c->maxa = c->mina = 0.0;
  1759. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1760. && c->maxw == c->minw && c->maxh == c->minh);
  1761. }
  1762. void
  1763. updatetitle(Client *c) {
  1764. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1765. gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
  1766. if(c->name[0] == '\0') /* hack to mark broken clients */
  1767. strcpy(c->name, broken);
  1768. }
  1769. void
  1770. updatestatus(void) {
  1771. if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
  1772. strcpy(stext, "dwm-"VERSION);
  1773. drawbar(selmon);
  1774. }
  1775. void
  1776. updatewindowtype(Client *c) {
  1777. Atom state = getatomprop(c, netatom[NetWMState]);
  1778. Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
  1779. if(state == netatom[NetWMFullscreen])
  1780. setfullscreen(c, True);
  1781. if(wtype == netatom[NetWMWindowTypeDialog])
  1782. c->isfloating = True;
  1783. }
  1784. void
  1785. updatewmhints(Client *c) {
  1786. XWMHints *wmh;
  1787. if((wmh = XGetWMHints(dpy, c->win))) {
  1788. if(c == selmon->sel && wmh->flags & XUrgencyHint) {
  1789. wmh->flags &= ~XUrgencyHint;
  1790. XSetWMHints(dpy, c->win, wmh);
  1791. }
  1792. else
  1793. c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
  1794. if(wmh->flags & InputHint)
  1795. c->neverfocus = !wmh->input;
  1796. else
  1797. c->neverfocus = False;
  1798. XFree(wmh);
  1799. }
  1800. }
  1801. void
  1802. view(const Arg *arg) {
  1803. if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
  1804. return;
  1805. selmon->seltags ^= 1; /* toggle sel tagset */
  1806. if(arg->ui & TAGMASK)
  1807. selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
  1808. focus(NULL);
  1809. arrange(selmon);
  1810. }
  1811. Client *
  1812. wintoclient(Window w) {
  1813. Client *c;
  1814. Monitor *m;
  1815. for(m = mons; m; m = m->next)
  1816. for(c = m->clients; c; c = c->next)
  1817. if(c->win == w)
  1818. return c;
  1819. return NULL;
  1820. }
  1821. Monitor *
  1822. wintomon(Window w) {
  1823. int x, y;
  1824. Client *c;
  1825. Monitor *m;
  1826. if(w == root && getrootptr(&x, &y))
  1827. return recttomon(x, y, 1, 1);
  1828. for(m = mons; m; m = m->next)
  1829. if(w == m->barwin)
  1830. return m;
  1831. if((c = wintoclient(w)))
  1832. return c->mon;
  1833. return selmon;
  1834. }
  1835. /* There's no way to check accesses to destroyed windows, thus those cases are
  1836. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1837. * default error handler, which may call exit. */
  1838. int
  1839. xerror(Display *dpy, XErrorEvent *ee) {
  1840. if(ee->error_code == BadWindow
  1841. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1842. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1843. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1844. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1845. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1846. || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
  1847. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1848. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1849. return 0;
  1850. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1851. ee->request_code, ee->error_code);
  1852. return xerrorxlib(dpy, ee); /* may call exit */
  1853. }
  1854. int
  1855. xerrordummy(Display *dpy, XErrorEvent *ee) {
  1856. return 0;
  1857. }
  1858. /* Startup Error handler to check if another window manager
  1859. * is already running. */
  1860. int
  1861. xerrorstart(Display *dpy, XErrorEvent *ee) {
  1862. die("dwm: another window manager is already running\n");
  1863. return -1;
  1864. }
  1865. void
  1866. zoom(const Arg *arg) {
  1867. Client *c = selmon->sel;
  1868. if(!selmon->lt[selmon->sellt]->arrange
  1869. || (selmon->sel && selmon->sel->isfloating))
  1870. return;
  1871. if(c == nexttiled(selmon->clients))
  1872. if(!c || !(c = nexttiled(c->next)))
  1873. return;
  1874. pop(c);
  1875. }
  1876. int
  1877. main(int argc, char *argv[]) {
  1878. if(argc == 2 && !strcmp("-v", argv[1]))
  1879. die("dwm-"VERSION", © 2006-2015 dwm engineers, see LICENSE for details\n");
  1880. else if(argc != 1)
  1881. die("usage: dwm [-v]\n");
  1882. if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  1883. fputs("warning: no locale support\n", stderr);
  1884. if(!(dpy = XOpenDisplay(NULL)))
  1885. die("dwm: cannot open display\n");
  1886. checkotherwm();
  1887. setup();
  1888. scan();
  1889. run();
  1890. cleanup();
  1891. XCloseDisplay(dpy);
  1892. return EXIT_SUCCESS;
  1893. }