dwm.c 66 KB

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