dwm.c 53 KB

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