dwm.c.orig 53 KB

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