dwm.c.orig 98 KB

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