dwm.c.orig 55 KB

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