dwm.c 101 KB

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