dwm.c 53 KB

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