dwm.c 68 KB

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