dwm.c 54 KB

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