dwm.c 54 KB

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