dwm.c 54 KB

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