dwm.c 87 KB

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