dwm.c 61 KB

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