dwm.c 56 KB

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