dwm.c 66 KB

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