dwm.c 68 KB

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