dwm.c 53 KB

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