dwm.c 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693
  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] = TEXTW(c->name) - lrpad + horizpadtabi + horizpadtabo;
  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. tot_width = 0; // recalculate total width of the tab bar
  1408. memcpy(sorted_label_widths, m->tab_widths, sizeof(int) * m->ntabs);
  1409. qsort(sorted_label_widths, m->ntabs, sizeof(int), cmpint);
  1410. for(i = 0; i < m->ntabs; ++i){
  1411. if(tot_width + (m->ntabs - i) * sorted_label_widths[i] > mw)
  1412. break;
  1413. tot_width += sorted_label_widths[i];
  1414. }
  1415. maxsize = (mw - tot_width) / (m->ntabs - i);
  1416. maxsize = (m->ww - tot_width) / (m->ntabs - i);
  1417. } else{
  1418. maxsize = mw;
  1419. }
  1420. i = 0;
  1421. /* cleans window */
  1422. drw_setscheme(drw, scheme[TabNorm]);
  1423. drw_rect(drw, 0, 0, mw, th, 1, 1);
  1424. for(c = m->clients; c; c = c->next){
  1425. if(!ISVISIBLE(c)) continue;
  1426. if(i >= m->ntabs) break;
  1427. if(m->tab_widths[i] > maxsize) m->tab_widths[i] = maxsize;
  1428. w = m->tab_widths[i];
  1429. drw_setscheme(drw, scheme[(c == m->sel) ? TabSel : TabNorm]);
  1430. drw_text(drw, x + horizpadtabo / 2, vertpadbar / 2, w - horizpadtabo, th - vertpadbar, horizpadtabi / 2, c->name, 0);
  1431. x += w;
  1432. ++i;
  1433. }
  1434. w = mw - buttons_w - x;
  1435. x += w;
  1436. drw_setscheme(drw, scheme[SchemeBtnPrev]);
  1437. w = TEXTW(btn_prev) - lrpad + horizpadtabo;
  1438. m->tab_btn_w[0] = w;
  1439. drw_text(drw, x + horizpadtabo / 2, vertpadbar / 2, w, th - vertpadbar, 0, btn_prev, 0);
  1440. x += w;
  1441. drw_setscheme(drw, scheme[SchemeBtnNext]);
  1442. w = TEXTW(btn_next) - lrpad + horizpadtabo;
  1443. m->tab_btn_w[1] = w;
  1444. drw_text(drw, x + horizpadtabo / 2, vertpadbar / 2, w, th - vertpadbar, 0, btn_next, 0);
  1445. x += w;
  1446. drw_setscheme(drw, scheme[SchemeBtnClose]);
  1447. w = TEXTW(btn_close) - lrpad + horizpadtabo;
  1448. m->tab_btn_w[2] = w;
  1449. drw_text(drw, x + horizpadtabo / 2, vertpadbar / 2, w, th - vertpadbar, 0, btn_close, 0);
  1450. x += w;
  1451. drw_map(drw, m->tabwin, 0, 0, m->ww, th);
  1452. }
  1453. void enternotify(XEvent *e) {
  1454. Client *c;
  1455. Monitor *m;
  1456. XCrossingEvent *ev = &e->xcrossing;
  1457. if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) &&
  1458. ev->window != root)
  1459. return;
  1460. c = wintoclient(ev->window);
  1461. m = c ? c->mon : wintomon(ev->window);
  1462. if (m != selmon) {
  1463. unfocus(selmon->sel, 1);
  1464. selmon = m;
  1465. } else if (!c || c == selmon->sel)
  1466. return;
  1467. focus(c);
  1468. }
  1469. void expose(XEvent *e) {
  1470. Monitor *m;
  1471. XExposeEvent *ev = &e->xexpose;
  1472. if (ev->count == 0 && (m = wintomon(ev->window))) {
  1473. drawbar(m);
  1474. if (m == selmon)
  1475. updatesystray();
  1476. }
  1477. }
  1478. void focus(Client *c) {
  1479. if (!c || (!ISVISIBLE(c) || HIDDEN(c)))
  1480. for (c = selmon->stack; c && (!ISVISIBLE(c) || HIDDEN(c)); c = c->snext)
  1481. ;
  1482. if (selmon->sel && selmon->sel != c)
  1483. unfocus(selmon->sel, 0);
  1484. if (c) {
  1485. if (c->mon != selmon)
  1486. selmon = c->mon;
  1487. if (c->isurgent)
  1488. seturgent(c, 0);
  1489. detachstack(c);
  1490. attachstack(c);
  1491. grabbuttons(c, 1);
  1492. XSetWindowBorder(dpy, c->win, scheme[SchemeSel][ColBorder].pixel);
  1493. setfocus(c);
  1494. } else {
  1495. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  1496. XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
  1497. }
  1498. selmon->sel = c;
  1499. drawbars();
  1500. drawtabs();
  1501. }
  1502. /* there are some broken focus acquiring clients needing extra handling */
  1503. void focusin(XEvent *e) {
  1504. XFocusChangeEvent *ev = &e->xfocus;
  1505. if (selmon->sel && ev->window != selmon->sel->win)
  1506. setfocus(selmon->sel);
  1507. }
  1508. void focusmon(const Arg *arg) {
  1509. Monitor *m;
  1510. if (!mons->next)
  1511. return;
  1512. if ((m = dirtomon(arg->i)) == selmon)
  1513. return;
  1514. unfocus(selmon->sel, 0);
  1515. selmon = m;
  1516. focus(NULL);
  1517. }
  1518. void focusstack(const Arg *arg) {
  1519. Client *c = NULL, *i;
  1520. if (!selmon->sel || (selmon->sel->isfullscreen && lockfullscreen))
  1521. return;
  1522. if (arg->i > 0) {
  1523. for (c = selmon->sel->next; c && (!ISVISIBLE(c) || HIDDEN(c)); c = c->next)
  1524. ;
  1525. if (!c)
  1526. for (c = selmon->clients; c && (!ISVISIBLE(c) || HIDDEN(c)); c = c->next)
  1527. ;
  1528. } else {
  1529. for (i = selmon->clients; i != selmon->sel; i = i->next)
  1530. if (ISVISIBLE(i) && !HIDDEN(i))
  1531. c = i;
  1532. if (!c)
  1533. for (; i; i = i->next)
  1534. if (ISVISIBLE(i) && !HIDDEN(i))
  1535. c = i;
  1536. }
  1537. if (c) {
  1538. focus(c);
  1539. restack(selmon);
  1540. }
  1541. }
  1542. void
  1543. focuswin(const Arg* arg){
  1544. int iwin = arg->i;
  1545. Client* c = NULL;
  1546. for(c = selmon->clients; c && (iwin || !ISVISIBLE(c)) ; c = c->next){
  1547. if(ISVISIBLE(c)) --iwin;
  1548. };
  1549. if(c) {
  1550. focus(c);
  1551. restack(selmon);
  1552. }
  1553. }
  1554. Atom getatomprop(Client *c, Atom prop) {
  1555. int di;
  1556. unsigned long dl;
  1557. unsigned char *p = NULL;
  1558. Atom da, atom = None;
  1559. /* FIXME getatomprop should return the number of items and a pointer to
  1560. * the stored data instead of this workaround */
  1561. Atom req = XA_ATOM;
  1562. if (prop == xatom[XembedInfo])
  1563. req = xatom[XembedInfo];
  1564. if (XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, req, &da,
  1565. &di, &dl, &dl, &p) == Success &&
  1566. p) {
  1567. atom = *(Atom *)p;
  1568. if (da == xatom[XembedInfo] && dl == 2)
  1569. atom = ((Atom *)p)[1];
  1570. XFree(p);
  1571. }
  1572. return atom;
  1573. }
  1574. int getrootptr(int *x, int *y) {
  1575. int di;
  1576. unsigned int dui;
  1577. Window dummy;
  1578. return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
  1579. }
  1580. long getstate(Window w) {
  1581. int format;
  1582. long result = -1;
  1583. unsigned char *p = NULL;
  1584. unsigned long n, extra;
  1585. Atom real;
  1586. if (XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False,
  1587. wmatom[WMState], &real, &format, &n, &extra,
  1588. (unsigned char **)&p) != Success)
  1589. return -1;
  1590. if (n != 0)
  1591. result = *p;
  1592. XFree(p);
  1593. return result;
  1594. }
  1595. unsigned int getsystraywidth() {
  1596. unsigned int w = 0;
  1597. Client *i;
  1598. if (showsystray)
  1599. for (i = systray->icons; i; w += i->w + systrayspacing, i = i->next)
  1600. ;
  1601. return w ? w + systrayspacing : 1;
  1602. }
  1603. int gettextprop(Window w, Atom atom, char *text, unsigned int size) {
  1604. char **list = NULL;
  1605. int n;
  1606. XTextProperty name;
  1607. if (!text || size == 0)
  1608. return 0;
  1609. text[0] = '\0';
  1610. if (!XGetTextProperty(dpy, w, &name, atom) || !name.nitems)
  1611. return 0;
  1612. if (name.encoding == XA_STRING)
  1613. strncpy(text, (char *)name.value, size - 1);
  1614. else {
  1615. if (XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 &&
  1616. *list) {
  1617. strncpy(text, *list, size - 1);
  1618. XFreeStringList(list);
  1619. }
  1620. }
  1621. text[size - 1] = '\0';
  1622. XFree(name.value);
  1623. return 1;
  1624. }
  1625. void grabbuttons(Client *c, int focused) {
  1626. updatenumlockmask();
  1627. {
  1628. unsigned int i, j;
  1629. unsigned int modifiers[] = {0, LockMask, numlockmask,
  1630. numlockmask | LockMask};
  1631. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1632. if (!focused)
  1633. XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
  1634. GrabModeSync, GrabModeSync, None, None);
  1635. for (i = 0; i < LENGTH(buttons); i++)
  1636. if (buttons[i].click == ClkClientWin)
  1637. for (j = 0; j < LENGTH(modifiers); j++)
  1638. XGrabButton(dpy, buttons[i].button, buttons[i].mask | modifiers[j],
  1639. c->win, False, BUTTONMASK, GrabModeAsync, GrabModeSync,
  1640. None, None);
  1641. }
  1642. }
  1643. void grabkeys(void) {
  1644. updatenumlockmask();
  1645. {
  1646. unsigned int i, j;
  1647. unsigned int modifiers[] = {0, LockMask, numlockmask,
  1648. numlockmask | LockMask};
  1649. KeyCode code;
  1650. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  1651. for (i = 0; i < LENGTH(keys); i++)
  1652. if ((code = XKeysymToKeycode(dpy, keys[i].keysym)))
  1653. for (j = 0; j < LENGTH(modifiers); j++)
  1654. XGrabKey(dpy, code, keys[i].mod | modifiers[j], root, True,
  1655. GrabModeAsync, GrabModeAsync);
  1656. }
  1657. }
  1658. void
  1659. hide(Client *c) {
  1660. if (!c || HIDDEN(c))
  1661. return;
  1662. Window w = c->win;
  1663. static XWindowAttributes ra, ca;
  1664. // more or less taken directly from blackbox's hide() function
  1665. XGrabServer(dpy);
  1666. XGetWindowAttributes(dpy, root, &ra);
  1667. XGetWindowAttributes(dpy, w, &ca);
  1668. // prevent UnmapNotify events
  1669. XSelectInput(dpy, root, ra.your_event_mask & ~SubstructureNotifyMask);
  1670. XSelectInput(dpy, w, ca.your_event_mask & ~StructureNotifyMask);
  1671. XUnmapWindow(dpy, w);
  1672. setclientstate(c, IconicState);
  1673. XSelectInput(dpy, root, ra.your_event_mask);
  1674. XSelectInput(dpy, w, ca.your_event_mask);
  1675. XUngrabServer(dpy);
  1676. focus(c->snext);
  1677. arrange(c->mon);
  1678. }
  1679. void incnmaster(const Arg *arg) {
  1680. selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag] = MAX(selmon->nmaster + arg->i, 0);
  1681. arrange(selmon);
  1682. }
  1683. #ifdef XINERAMA
  1684. static int isuniquegeom(XineramaScreenInfo *unique, size_t n,
  1685. XineramaScreenInfo *info) {
  1686. while (n--)
  1687. if (unique[n].x_org == info->x_org && unique[n].y_org == info->y_org &&
  1688. unique[n].width == info->width && unique[n].height == info->height)
  1689. return 0;
  1690. return 1;
  1691. }
  1692. #endif /* XINERAMA */
  1693. void keypress(XEvent *e) {
  1694. unsigned int i;
  1695. KeySym keysym;
  1696. XKeyEvent *ev;
  1697. ev = &e->xkey;
  1698. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  1699. for (i = 0; i < LENGTH(keys); i++)
  1700. if (keysym == keys[i].keysym &&
  1701. CLEANMASK(keys[i].mod) == CLEANMASK(ev->state) && keys[i].func)
  1702. keys[i].func(&(keys[i].arg));
  1703. }
  1704. void killclient(const Arg *arg) {
  1705. if (!selmon->sel)
  1706. return;
  1707. if (!sendevent(selmon->sel->win, wmatom[WMDelete], NoEventMask,
  1708. wmatom[WMDelete], CurrentTime, 0, 0, 0)) {
  1709. XGrabServer(dpy);
  1710. XSetErrorHandler(xerrordummy);
  1711. XSetCloseDownMode(dpy, DestroyAll);
  1712. XKillClient(dpy, selmon->sel->win);
  1713. XSync(dpy, False);
  1714. XSetErrorHandler(xerror);
  1715. XUngrabServer(dpy);
  1716. }
  1717. }
  1718. void layoutmenu(const Arg *arg) {
  1719. FILE *p;
  1720. char c[3], *s;
  1721. int i;
  1722. if (!(p = popen(layoutmenu_cmd, "r")))
  1723. return;
  1724. s = fgets(c, sizeof(c), p);
  1725. pclose(p);
  1726. if (!s || *s == '\0' || c == '\0')
  1727. return;
  1728. i = atoi(c);
  1729. setlayout(&((Arg){.v = &layouts[i]}));
  1730. }
  1731. void manage(Window w, XWindowAttributes *wa) {
  1732. Client *c, *t = NULL;
  1733. Window trans = None;
  1734. XWindowChanges wc;
  1735. c = ecalloc(1, sizeof(Client));
  1736. c->win = w;
  1737. /* geometry */
  1738. c->x = c->oldx = wa->x;
  1739. c->y = c->oldy = wa->y;
  1740. c->w = c->oldw = wa->width;
  1741. c->h = c->oldh = wa->height;
  1742. c->oldbw = wa->border_width;
  1743. c->cfact = 1.0;
  1744. updatetitle(c);
  1745. if (XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
  1746. c->mon = t->mon;
  1747. c->tags = t->tags;
  1748. } else {
  1749. c->mon = selmon;
  1750. applyrules(c);
  1751. }
  1752. if (c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
  1753. c->x = c->mon->mx + c->mon->mw - WIDTH(c);
  1754. if (c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
  1755. c->y = c->mon->my + c->mon->mh - HEIGHT(c);
  1756. c->x = MAX(c->x, c->mon->mx);
  1757. /* only fix client y-offset, if the client center might cover the bar */
  1758. c->y = MAX(c->y,
  1759. ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx) &&
  1760. (c->x + (c->w / 2) < c->mon->wx + c->mon->ww))
  1761. ? bh
  1762. : c->mon->my);
  1763. c->bw = c->mon->borderpx;
  1764. wc.border_width = c->bw;
  1765. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  1766. XSetWindowBorder(dpy, w, scheme[SchemeNorm][ColBorder].pixel);
  1767. configure(c); /* propagates border_width, if size doesn't change */
  1768. updatewindowtype(c);
  1769. updatesizehints(c);
  1770. updatewmhints(c);
  1771. if (c->iscentered) {
  1772. c->x = c->mon->mx + (c->mon->mw - WIDTH(c)) / 2;
  1773. c->y = c->mon->my + (c->mon->mh - HEIGHT(c)) / 2;
  1774. }
  1775. XSelectInput(dpy, w,
  1776. EnterWindowMask | FocusChangeMask | PropertyChangeMask |
  1777. StructureNotifyMask);
  1778. grabbuttons(c, 0);
  1779. if (!c->isfloating)
  1780. c->isfloating = c->oldstate = trans != None || c->isfixed;
  1781. if (c->isfloating)
  1782. XRaiseWindow(dpy, c->win);
  1783. attach(c);
  1784. attachstack(c);
  1785. XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32,
  1786. PropModeAppend, (unsigned char *)&(c->win), 1);
  1787. XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w,
  1788. c->h); /* some windows require this */
  1789. if (!HIDDEN(c))
  1790. setclientstate(c, NormalState);
  1791. if (c->mon == selmon)
  1792. unfocus(selmon->sel, 0);
  1793. c->mon->sel = c;
  1794. arrange(c->mon);
  1795. if (!HIDDEN(c))
  1796. XMapWindow(dpy, c->win);
  1797. focus(NULL);
  1798. }
  1799. void mappingnotify(XEvent *e) {
  1800. XMappingEvent *ev = &e->xmapping;
  1801. XRefreshKeyboardMapping(ev);
  1802. if (ev->request == MappingKeyboard)
  1803. grabkeys();
  1804. }
  1805. void maprequest(XEvent *e) {
  1806. static XWindowAttributes wa;
  1807. XMapRequestEvent *ev = &e->xmaprequest;
  1808. Client *i;
  1809. if ((i = wintosystrayicon(ev->window))) {
  1810. sendevent(i->win, netatom[Xembed], StructureNotifyMask, CurrentTime,
  1811. XEMBED_WINDOW_ACTIVATE, 0, systray->win, XEMBED_EMBEDDED_VERSION);
  1812. resizebarwin(selmon);
  1813. updatesystray();
  1814. }
  1815. if (!XGetWindowAttributes(dpy, ev->window, &wa))
  1816. return;
  1817. if (wa.override_redirect)
  1818. return;
  1819. if (!wintoclient(ev->window))
  1820. manage(ev->window, &wa);
  1821. }
  1822. void
  1823. monocle(Monitor *m)
  1824. {
  1825. unsigned int n = 0;
  1826. Client *c;
  1827. for (c = m->clients; c; c = c->next)
  1828. if (ISVISIBLE(c)) n++;
  1829. if (n > 0) /* override layout symbol */
  1830. snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
  1831. int newx, newy, neww, newh;
  1832. for (c = nexttiled(m->clients); c; c = nexttiled(c->next)) {
  1833. newx = m->wx + m->gappov - c->bw;
  1834. newy = m->wy + m->gappoh - c->bw;
  1835. neww = m->ww - 2 * (m->gappov + c->bw);
  1836. newh = m->wh - 2 * (m->gappoh + c->bw);
  1837. applysizehints(c, &newx, &newy, &neww, &newh, 0);
  1838. if (neww < m->ww)
  1839. newx = m->wx + (m->ww - (neww + 2 * c->bw)) / 2;
  1840. if (newh < m->wh)
  1841. newy = m->wy + (m->wh - (newh + 2 * c->bw)) / 2;
  1842. resize(c, newx, newy, neww, newh, 0);
  1843. }
  1844. }
  1845. void motionnotify(XEvent *e) {
  1846. unsigned int i, x;
  1847. static Monitor *mon = NULL;
  1848. Monitor *m;
  1849. XMotionEvent *ev = &e->xmotion;
  1850. if (ev->window == selmon->barwin) {
  1851. i = x = 0;
  1852. do
  1853. x += TEXTW(tags[i]);
  1854. while (ev->x >= x && ++i < LENGTH(tags));
  1855. if (i < LENGTH(tags)) {
  1856. if ((i + 1) != selmon->previewshow && !(selmon->tagset[selmon->seltags] & 1 << i)) {
  1857. selmon->previewshow = i + 1;
  1858. showtagpreview(i);
  1859. } else if (selmon->tagset[selmon->seltags] & 1 << i) {
  1860. selmon->previewshow = 0;
  1861. showtagpreview(0);
  1862. }
  1863. } else if (selmon->previewshow != 0) {
  1864. selmon->previewshow = 0;
  1865. showtagpreview(0);
  1866. }
  1867. } else if (selmon->previewshow != 0) {
  1868. selmon->previewshow = 0;
  1869. showtagpreview(0);
  1870. }
  1871. if (ev->window != root)
  1872. return;
  1873. if ((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
  1874. unfocus(selmon->sel, 1);
  1875. selmon = m;
  1876. focus(NULL);
  1877. }
  1878. mon = m;
  1879. }
  1880. void
  1881. moveorplace(const Arg *arg) {
  1882. if ((!selmon->lt[selmon->sellt]->arrange || (selmon->sel && selmon->sel->isfloating)))
  1883. movemouse(arg);
  1884. else
  1885. placemouse(arg);
  1886. }
  1887. void movemouse(const Arg *arg) {
  1888. int x, y, ocx, ocy, nx, ny;
  1889. Client *c;
  1890. Monitor *m;
  1891. XEvent ev;
  1892. Time lasttime = 0;
  1893. if (!(c = selmon->sel))
  1894. return;
  1895. if (c->isfullscreen) /* no support moving fullscreen windows by mouse */
  1896. return;
  1897. restack(selmon);
  1898. ocx = c->x;
  1899. ocy = c->y;
  1900. if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1901. None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
  1902. return;
  1903. if (!getrootptr(&x, &y))
  1904. return;
  1905. do {
  1906. XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask, &ev);
  1907. switch (ev.type) {
  1908. case ConfigureRequest:
  1909. case Expose:
  1910. case MapRequest:
  1911. handler[ev.type](&ev);
  1912. break;
  1913. case MotionNotify:
  1914. if ((ev.xmotion.time - lasttime) <= (1000 / 60))
  1915. continue;
  1916. lasttime = ev.xmotion.time;
  1917. nx = ocx + (ev.xmotion.x - x);
  1918. ny = ocy + (ev.xmotion.y - y);
  1919. if (abs(selmon->wx - nx) < snap)
  1920. nx = selmon->wx;
  1921. else if (abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
  1922. nx = selmon->wx + selmon->ww - WIDTH(c);
  1923. if (abs(selmon->wy - ny) < snap)
  1924. ny = selmon->wy;
  1925. else if (abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
  1926. ny = selmon->wy + selmon->wh - HEIGHT(c);
  1927. if (!c->isfloating && selmon->lt[selmon->sellt]->arrange &&
  1928. (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
  1929. togglefloating(NULL);
  1930. if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
  1931. resize(c, nx, ny, c->w, c->h, 1);
  1932. break;
  1933. }
  1934. } while (ev.type != ButtonRelease);
  1935. XUngrabPointer(dpy, CurrentTime);
  1936. if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
  1937. sendmon(c, m);
  1938. selmon = m;
  1939. focus(NULL);
  1940. }
  1941. }
  1942. Client *nexttiled(Client *c) {
  1943. for (; c && (c->isfloating || (!ISVISIBLE(c) || HIDDEN(c))); c = c->next)
  1944. ;
  1945. return c;
  1946. }
  1947. void
  1948. placemouse(const Arg *arg)
  1949. {
  1950. int x, y, px, py, ocx, ocy, nx = -9999, ny = -9999, freemove = 0;
  1951. Client *c, *r = NULL, *at, *prevr;
  1952. Monitor *m;
  1953. XEvent ev;
  1954. XWindowAttributes wa;
  1955. Time lasttime = 0;
  1956. int attachmode, prevattachmode;
  1957. attachmode = prevattachmode = -1;
  1958. if (!(c = selmon->sel) || !c->mon->lt[c->mon->sellt]->arrange) /* no support for placemouse when floating layout is used */
  1959. return;
  1960. if (c->isfullscreen) /* no support placing fullscreen windows by mouse */
  1961. return;
  1962. restack(selmon);
  1963. prevr = c;
  1964. if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1965. None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
  1966. return;
  1967. c->isfloating = 0;
  1968. c->beingmoved = 1;
  1969. XGetWindowAttributes(dpy, c->win, &wa);
  1970. ocx = wa.x;
  1971. ocy = wa.y;
  1972. if (arg->i == 2) // warp cursor to client center
  1973. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, WIDTH(c) / 2, HEIGHT(c) / 2);
  1974. if (!getrootptr(&x, &y))
  1975. return;
  1976. do {
  1977. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1978. switch (ev.type) {
  1979. case ConfigureRequest:
  1980. case Expose:
  1981. case MapRequest:
  1982. handler[ev.type](&ev);
  1983. break;
  1984. case MotionNotify:
  1985. if ((ev.xmotion.time - lasttime) <= (1000 / 60))
  1986. continue;
  1987. lasttime = ev.xmotion.time;
  1988. nx = ocx + (ev.xmotion.x - x);
  1989. ny = ocy + (ev.xmotion.y - y);
  1990. if (!freemove && (abs(nx - ocx) > snap || abs(ny - ocy) > snap))
  1991. freemove = 1;
  1992. if (freemove)
  1993. XMoveWindow(dpy, c->win, nx, ny);
  1994. if ((m = recttomon(ev.xmotion.x, ev.xmotion.y, 1, 1)) && m != selmon)
  1995. selmon = m;
  1996. if (arg->i == 1) { // tiled position is relative to the client window center point
  1997. px = nx + wa.width / 2;
  1998. py = ny + wa.height / 2;
  1999. } else { // tiled position is relative to the mouse cursor
  2000. px = ev.xmotion.x;
  2001. py = ev.xmotion.y;
  2002. }
  2003. r = recttoclient(px, py, 1, 1);
  2004. if (!r || r == c)
  2005. break;
  2006. attachmode = 0; // below
  2007. if (((float)(r->y + r->h - py) / r->h) > ((float)(r->x + r->w - px) / r->w)) {
  2008. if (abs(r->y - py) < r->h / 2)
  2009. attachmode = 1; // above
  2010. } else if (abs(r->x - px) < r->w / 2)
  2011. attachmode = 1; // above
  2012. if ((r && r != prevr) || (attachmode != prevattachmode)) {
  2013. detachstack(c);
  2014. detach(c);
  2015. if (c->mon != r->mon) {
  2016. arrangemon(c->mon);
  2017. c->tags = r->mon->tagset[r->mon->seltags];
  2018. }
  2019. c->mon = r->mon;
  2020. r->mon->sel = r;
  2021. if (attachmode) {
  2022. if (r == r->mon->clients)
  2023. attach(c);
  2024. else {
  2025. for (at = r->mon->clients; at->next != r; at = at->next);
  2026. c->next = at->next;
  2027. at->next = c;
  2028. }
  2029. } else {
  2030. c->next = r->next;
  2031. r->next = c;
  2032. }
  2033. attachstack(c);
  2034. arrangemon(r->mon);
  2035. prevr = r;
  2036. prevattachmode = attachmode;
  2037. }
  2038. break;
  2039. }
  2040. } while (ev.type != ButtonRelease);
  2041. XUngrabPointer(dpy, CurrentTime);
  2042. if ((m = recttomon(ev.xmotion.x, ev.xmotion.y, 1, 1)) && m != c->mon) {
  2043. detach(c);
  2044. detachstack(c);
  2045. arrangemon(c->mon);
  2046. c->mon = m;
  2047. c->tags = m->tagset[m->seltags];
  2048. attach(c);
  2049. attachstack(c);
  2050. selmon = m;
  2051. }
  2052. focus(c);
  2053. c->beingmoved = 0;
  2054. if (nx != -9999)
  2055. resize(c, nx, ny, c->w, c->h, 0);
  2056. arrangemon(c->mon);
  2057. }
  2058. void pop(Client *c) {
  2059. detach(c);
  2060. attach(c);
  2061. focus(c);
  2062. arrange(c->mon);
  2063. }
  2064. void propertynotify(XEvent *e) {
  2065. Client *c;
  2066. Window trans;
  2067. XPropertyEvent *ev = &e->xproperty;
  2068. if ((c = wintosystrayicon(ev->window))) {
  2069. if (ev->atom == XA_WM_NORMAL_HINTS) {
  2070. updatesizehints(c);
  2071. updatesystrayicongeom(c, c->w, c->h);
  2072. } else
  2073. updatesystrayiconstate(c, ev);
  2074. resizebarwin(selmon);
  2075. updatesystray();
  2076. }
  2077. if ((ev->window == root) && (ev->atom == XA_WM_NAME))
  2078. updatestatus();
  2079. else if (ev->state == PropertyDelete)
  2080. return; /* ignore */
  2081. else if ((c = wintoclient(ev->window))) {
  2082. switch (ev->atom) {
  2083. default:
  2084. break;
  2085. case XA_WM_TRANSIENT_FOR:
  2086. if (!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
  2087. (c->isfloating = (wintoclient(trans)) != NULL))
  2088. arrange(c->mon);
  2089. break;
  2090. case XA_WM_NORMAL_HINTS:
  2091. updatesizehints(c);
  2092. break;
  2093. case XA_WM_HINTS:
  2094. updatewmhints(c);
  2095. drawbars();
  2096. drawtabs();
  2097. break;
  2098. }
  2099. if (ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  2100. updatetitle(c);
  2101. if (c == c->mon->sel)
  2102. drawbar(c->mon);
  2103. drawtab(c->mon);
  2104. }
  2105. if (ev->atom == netatom[NetWMWindowType])
  2106. updatewindowtype(c);
  2107. }
  2108. }
  2109. void quit(const Arg *arg) {
  2110. if(arg->i == 0) system("killall bar");
  2111. else if (arg->i)
  2112. restart = 1;
  2113. Monitor *m;
  2114. Client *c;
  2115. for (m = mons; m; m = m->next) {
  2116. if (m) {
  2117. for (c = m->stack; c; c = c->next)
  2118. if (c && HIDDEN(c)) show(c);
  2119. }
  2120. }
  2121. running = 0;
  2122. }
  2123. Client *
  2124. recttoclient(int x, int y, int w, int h)
  2125. {
  2126. Client *c, *r = NULL;
  2127. int a, area = 0;
  2128. for (c = nexttiled(selmon->clients); c; c = nexttiled(c->next)) {
  2129. if ((a = INTERSECTC(x, y, w, h, c)) > area) {
  2130. area = a;
  2131. r = c;
  2132. }
  2133. }
  2134. return r;
  2135. }
  2136. Monitor *recttomon(int x, int y, int w, int h) {
  2137. Monitor *m, *r = selmon;
  2138. int a, area = 0;
  2139. for (m = mons; m; m = m->next)
  2140. if ((a = INTERSECT(x, y, w, h, m)) > area) {
  2141. area = a;
  2142. r = m;
  2143. }
  2144. return r;
  2145. }
  2146. void removesystrayicon(Client *i) {
  2147. Client **ii;
  2148. if (!showsystray || !i)
  2149. return;
  2150. for (ii = &systray->icons; *ii && *ii != i; ii = &(*ii)->next)
  2151. ;
  2152. if (ii)
  2153. *ii = i->next;
  2154. free(i);
  2155. }
  2156. void resize(Client *c, int x, int y, int w, int h, int interact) {
  2157. if (applysizehints(c, &x, &y, &w, &h, interact))
  2158. resizeclient(c, x, y, w, h);
  2159. }
  2160. void resizebarwin(Monitor *m) {
  2161. unsigned int w = m->ww - 2 * m->gappov;
  2162. if (showsystray && m == systraytomon(m))
  2163. w -= getsystraywidth();
  2164. XMoveResizeWindow(dpy, m->barwin, m->wx + m->gappov, m->by, w, bh);
  2165. }
  2166. void resizeclient(Client *c, int x, int y, int w, int h) {
  2167. XWindowChanges wc;
  2168. c->oldx = c->x;
  2169. c->x = wc.x = x;
  2170. c->oldy = c->y;
  2171. c->y = wc.y = y;
  2172. c->oldw = c->w;
  2173. c->w = wc.width = w;
  2174. c->oldh = c->h;
  2175. c->h = wc.height = h;
  2176. if (c->beingmoved)
  2177. return;
  2178. wc.border_width = c->bw;
  2179. XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth,
  2180. &wc);
  2181. configure(c);
  2182. XSync(dpy, False);
  2183. }
  2184. void resizemouse(const Arg *arg) {
  2185. int ocx, ocy, nw, nh;
  2186. Client *c;
  2187. Monitor *m;
  2188. XEvent ev;
  2189. Time lasttime = 0;
  2190. if (!(c = selmon->sel))
  2191. return;
  2192. if (c->isfullscreen) /* no support resizing fullscreen windows by mouse */
  2193. return;
  2194. restack(selmon);
  2195. ocx = c->x;
  2196. ocy = c->y;
  2197. if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  2198. None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
  2199. return;
  2200. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1,
  2201. c->h + c->bw - 1);
  2202. do {
  2203. XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask, &ev);
  2204. switch (ev.type) {
  2205. case ConfigureRequest:
  2206. case Expose:
  2207. case MapRequest:
  2208. handler[ev.type](&ev);
  2209. break;
  2210. case MotionNotify:
  2211. if ((ev.xmotion.time - lasttime) <= (1000 / 60))
  2212. continue;
  2213. lasttime = ev.xmotion.time;
  2214. nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
  2215. nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
  2216. if (c->mon->wx + nw >= selmon->wx &&
  2217. c->mon->wx + nw <= selmon->wx + selmon->ww &&
  2218. c->mon->wy + nh >= selmon->wy &&
  2219. c->mon->wy + nh <= selmon->wy + selmon->wh) {
  2220. if (!c->isfloating && selmon->lt[selmon->sellt]->arrange &&
  2221. (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
  2222. togglefloating(NULL);
  2223. }
  2224. if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
  2225. resize(c, c->x, c->y, nw, nh, 1);
  2226. break;
  2227. }
  2228. } while (ev.type != ButtonRelease);
  2229. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1,
  2230. c->h + c->bw - 1);
  2231. XUngrabPointer(dpy, CurrentTime);
  2232. while (XCheckMaskEvent(dpy, EnterWindowMask, &ev))
  2233. ;
  2234. if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
  2235. sendmon(c, m);
  2236. selmon = m;
  2237. focus(NULL);
  2238. }
  2239. }
  2240. void resizerequest(XEvent *e) {
  2241. XResizeRequestEvent *ev = &e->xresizerequest;
  2242. Client *i;
  2243. if ((i = wintosystrayicon(ev->window))) {
  2244. updatesystrayicongeom(i, ev->width, ev->height);
  2245. resizebarwin(selmon);
  2246. updatesystray();
  2247. }
  2248. }
  2249. void restack(Monitor *m) {
  2250. Client *c;
  2251. XEvent ev;
  2252. XWindowChanges wc;
  2253. drawbar(m);
  2254. drawtab(m);
  2255. if (!m->sel)
  2256. return;
  2257. if (m->sel->isfloating || !m->lt[m->sellt]->arrange)
  2258. XRaiseWindow(dpy, m->sel->win);
  2259. if (m->lt[m->sellt]->arrange) {
  2260. wc.stack_mode = Below;
  2261. wc.sibling = m->barwin;
  2262. for (c = m->stack; c; c = c->snext)
  2263. if (!c->isfloating && ISVISIBLE(c)) {
  2264. XConfigureWindow(dpy, c->win, CWSibling | CWStackMode, &wc);
  2265. wc.sibling = c->win;
  2266. }
  2267. }
  2268. XSync(dpy, False);
  2269. while (XCheckMaskEvent(dpy, EnterWindowMask, &ev))
  2270. ;
  2271. }
  2272. void run(void) {
  2273. XEvent ev;
  2274. /* main event loop */
  2275. XSync(dpy, False);
  2276. while (running && !XNextEvent(dpy, &ev))
  2277. if (handler[ev.type])
  2278. handler[ev.type](&ev); /* call handler */
  2279. }
  2280. void scan(void) {
  2281. unsigned int i, num;
  2282. Window d1, d2, *wins = NULL;
  2283. XWindowAttributes wa;
  2284. if (XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  2285. for (i = 0; i < num; i++) {
  2286. if (!XGetWindowAttributes(dpy, wins[i], &wa) || wa.override_redirect ||
  2287. XGetTransientForHint(dpy, wins[i], &d1))
  2288. continue;
  2289. if (wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  2290. manage(wins[i], &wa);
  2291. }
  2292. for (i = 0; i < num; i++) { /* now the transients */
  2293. if (!XGetWindowAttributes(dpy, wins[i], &wa))
  2294. continue;
  2295. if (XGetTransientForHint(dpy, wins[i], &d1) &&
  2296. (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  2297. manage(wins[i], &wa);
  2298. }
  2299. if (wins)
  2300. XFree(wins);
  2301. }
  2302. }
  2303. void sendmon(Client *c, Monitor *m) {
  2304. if (c->mon == m)
  2305. return;
  2306. unfocus(c, 1);
  2307. detach(c);
  2308. detachstack(c);
  2309. c->mon = m;
  2310. c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
  2311. attach(c);
  2312. attachstack(c);
  2313. focus(NULL);
  2314. arrange(NULL);
  2315. }
  2316. void setborderpx(const Arg *arg) {
  2317. Client *c;
  2318. int prev_borderpx = selmon->borderpx;
  2319. if (arg->i == 0)
  2320. selmon->borderpx = borderpx;
  2321. else if (selmon->borderpx + arg->i < 0)
  2322. selmon->borderpx = 0;
  2323. else
  2324. selmon->borderpx += arg->i;
  2325. for (c = selmon->clients; c; c = c->next) {
  2326. if (c->bw + arg->i < 0)
  2327. c->bw = selmon->borderpx = 0;
  2328. else
  2329. c->bw = selmon->borderpx;
  2330. if (c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
  2331. if (arg->i != 0 && prev_borderpx + arg->i >= 0)
  2332. resize(c, c->x, c->y, c->w - (arg->i * 2), c->h - (arg->i * 2), 0);
  2333. else if (arg->i != 0)
  2334. resizeclient(c, c->x, c->y, c->w, c->h);
  2335. else if (prev_borderpx > borderpx)
  2336. resize(c, c->x, c->y, c->w + 2 * (prev_borderpx - borderpx),
  2337. c->h + 2 * (prev_borderpx - borderpx), 0);
  2338. else if (prev_borderpx < borderpx)
  2339. resize(c, c->x, c->y, c->w - 2 * (borderpx - prev_borderpx),
  2340. c->h - 2 * (borderpx - prev_borderpx), 0);
  2341. }
  2342. }
  2343. arrange(selmon);
  2344. }
  2345. void setclientstate(Client *c, long state) {
  2346. long data[] = {state, None};
  2347. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  2348. PropModeReplace, (unsigned char *)data, 2);
  2349. }
  2350. void
  2351. setcurrentdesktop(void){
  2352. long data[] = { 0 };
  2353. XChangeProperty(dpy, root, netatom[NetCurrentDesktop], XA_CARDINAL, 32, PropModeReplace, (unsigned char *)data, 1);
  2354. }
  2355. void setdesktopnames(void){
  2356. XTextProperty text;
  2357. Xutf8TextListToTextProperty(dpy, tags, TAGSLENGTH, XUTF8StringStyle, &text);
  2358. XSetTextProperty(dpy, root, &text, netatom[NetDesktopNames]);
  2359. }
  2360. int sendevent(Window w, Atom proto, int mask, long d0, long d1, long d2,
  2361. long d3, long d4) {
  2362. int n;
  2363. Atom *protocols, mt;
  2364. int exists = 0;
  2365. XEvent ev;
  2366. if (proto == wmatom[WMTakeFocus] || proto == wmatom[WMDelete]) {
  2367. mt = wmatom[WMProtocols];
  2368. if (XGetWMProtocols(dpy, w, &protocols, &n)) {
  2369. while (!exists && n--)
  2370. exists = protocols[n] == proto;
  2371. XFree(protocols);
  2372. }
  2373. } else {
  2374. exists = True;
  2375. mt = proto;
  2376. }
  2377. if (exists) {
  2378. ev.type = ClientMessage;
  2379. ev.xclient.window = w;
  2380. ev.xclient.message_type = mt;
  2381. ev.xclient.format = 32;
  2382. ev.xclient.data.l[0] = d0;
  2383. ev.xclient.data.l[1] = d1;
  2384. ev.xclient.data.l[2] = d2;
  2385. ev.xclient.data.l[3] = d3;
  2386. ev.xclient.data.l[4] = d4;
  2387. XSendEvent(dpy, w, False, mask, &ev);
  2388. }
  2389. return exists;
  2390. }
  2391. void
  2392. setnumdesktops(void){
  2393. long data[] = { TAGSLENGTH };
  2394. XChangeProperty(dpy, root, netatom[NetNumberOfDesktops], XA_CARDINAL, 32, PropModeReplace, (unsigned char *)data, 1);
  2395. }
  2396. void setfocus(Client *c) {
  2397. if (!c->neverfocus) {
  2398. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  2399. XChangeProperty(dpy, root, netatom[NetActiveWindow], XA_WINDOW, 32,
  2400. PropModeReplace, (unsigned char *)&(c->win), 1);
  2401. }
  2402. sendevent(c->win, wmatom[WMTakeFocus], NoEventMask, wmatom[WMTakeFocus],
  2403. CurrentTime, 0, 0, 0);
  2404. }
  2405. void setfullscreen(Client *c, int fullscreen) {
  2406. if (fullscreen && !c->isfullscreen) {
  2407. XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
  2408. PropModeReplace, (unsigned char *)&netatom[NetWMFullscreen],
  2409. 1);
  2410. c->isfullscreen = 1;
  2411. c->oldstate = c->isfloating;
  2412. c->oldbw = c->bw;
  2413. c->bw = 0;
  2414. c->isfloating = 1;
  2415. resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
  2416. XRaiseWindow(dpy, c->win);
  2417. } else if (!fullscreen && c->isfullscreen) {
  2418. XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
  2419. PropModeReplace, (unsigned char *)0, 0);
  2420. c->isfullscreen = 0;
  2421. c->isfloating = c->oldstate;
  2422. c->bw = c->oldbw;
  2423. c->x = c->oldx;
  2424. c->y = c->oldy;
  2425. c->w = c->oldw;
  2426. c->h = c->oldh;
  2427. resizeclient(c, c->x, c->y, c->w, c->h);
  2428. arrange(c->mon);
  2429. }
  2430. }
  2431. void setlayout(const Arg *arg) {
  2432. if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
  2433. selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag] ^= 1;
  2434. if (arg && arg->v)
  2435. selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt] = (Layout *)arg->v;
  2436. strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol,
  2437. sizeof selmon->ltsymbol);
  2438. if (selmon->sel)
  2439. arrange(selmon);
  2440. else
  2441. drawbar(selmon);
  2442. }
  2443. void setcfact(const Arg *arg) {
  2444. float f;
  2445. Client *c;
  2446. c = selmon->sel;
  2447. if (!arg || !c || !selmon->lt[selmon->sellt]->arrange)
  2448. return;
  2449. if (!arg->f)
  2450. f = 1.0;
  2451. else if (arg->f > 4.0) // set fact absolutely
  2452. f = arg->f - 4.0;
  2453. else
  2454. f = arg->f + c->cfact;
  2455. if (f < 0.25)
  2456. f = 0.25;
  2457. else if (f > 4.0)
  2458. f = 4.0;
  2459. c->cfact = f;
  2460. arrange(selmon);
  2461. }
  2462. /* arg > 1.0 will set mfact absolutely */
  2463. void setmfact(const Arg *arg) {
  2464. float f;
  2465. if (!arg || !selmon->lt[selmon->sellt]->arrange)
  2466. return;
  2467. f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
  2468. if (f < 0.05 || f > 0.95)
  2469. return;
  2470. selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag] = f;
  2471. arrange(selmon);
  2472. }
  2473. void setup(void) {
  2474. int i;
  2475. XSetWindowAttributes wa;
  2476. Atom utf8string;
  2477. /* clean up any zombies immediately */
  2478. sigchld(0);
  2479. signal(SIGHUP, sighup);
  2480. signal(SIGTERM, sigterm);
  2481. /* init screen */
  2482. screen = DefaultScreen(dpy);
  2483. sw = DisplayWidth(dpy, screen);
  2484. sh = DisplayHeight(dpy, screen);
  2485. root = RootWindow(dpy, screen);
  2486. drw = drw_create(dpy, screen, root, sw, sh);
  2487. if (!drw_fontset_create(drw, fonts, LENGTH(fonts)))
  2488. die("no fonts could be loaded.");
  2489. lrpad = drw->fonts->h;
  2490. bh = drw->fonts->h + 2 + vertpadbar + borderpx * 2;
  2491. th = vertpadtab;
  2492. // bh_n = vertpadtab;
  2493. updategeom();
  2494. /* init atoms */
  2495. utf8string = XInternAtom(dpy, "UTF8_STRING", False);
  2496. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  2497. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  2498. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  2499. wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
  2500. netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
  2501. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  2502. netatom[NetSystemTray] = XInternAtom(dpy, "_NET_SYSTEM_TRAY_S0", False);
  2503. netatom[NetSystemTrayOP] = XInternAtom(dpy, "_NET_SYSTEM_TRAY_OPCODE", False);
  2504. netatom[NetSystemTrayOrientation] =
  2505. XInternAtom(dpy, "_NET_SYSTEM_TRAY_ORIENTATION", False);
  2506. netatom[NetSystemTrayOrientationHorz] =
  2507. XInternAtom(dpy, "_NET_SYSTEM_TRAY_ORIENTATION_HORZ", False);
  2508. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  2509. netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
  2510. netatom[NetWMCheck] = XInternAtom(dpy, "_NET_SUPPORTING_WM_CHECK", False);
  2511. netatom[NetWMFullscreen] =
  2512. XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
  2513. netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
  2514. netatom[NetWMWindowTypeDialog] =
  2515. XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
  2516. netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
  2517. xatom[Manager] = XInternAtom(dpy, "MANAGER", False);
  2518. xatom[Xembed] = XInternAtom(dpy, "_XEMBED", False);
  2519. xatom[XembedInfo] = XInternAtom(dpy, "_XEMBED_INFO", False);
  2520. netatom[NetDesktopViewport] = XInternAtom(dpy, "_NET_DESKTOP_VIEWPORT", False);
  2521. netatom[NetNumberOfDesktops] = XInternAtom(dpy, "_NET_NUMBER_OF_DESKTOPS", False);
  2522. netatom[NetCurrentDesktop] = XInternAtom(dpy, "_NET_CURRENT_DESKTOP", False);
  2523. netatom[NetDesktopNames] = XInternAtom(dpy, "_NET_DESKTOP_NAMES", False);
  2524. /* init cursors */
  2525. cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
  2526. cursor[CurResize] = drw_cur_create(drw, XC_sizing);
  2527. cursor[CurMove] = drw_cur_create(drw, XC_fleur);
  2528. cursor[CurResizeHorzArrow] = drw_cur_create(drw, XC_sb_h_double_arrow);
  2529. cursor[CurResizeVertArrow] = drw_cur_create(drw, XC_sb_v_double_arrow);
  2530. /* init appearance */
  2531. scheme = ecalloc(LENGTH(colors) + 1, sizeof(Clr *));
  2532. scheme[LENGTH(colors)] = drw_scm_create(drw, colors[0], 3);
  2533. for (i = 0; i < LENGTH(colors); i++)
  2534. scheme[i] = drw_scm_create(drw, colors[i], 3);
  2535. drw_clr_create(drw, &clrborder, col_borderbar);
  2536. /* init system tray */
  2537. updatesystray();
  2538. /* init bars */
  2539. updatebars();
  2540. updatestatus();
  2541. updatebarpos(selmon);
  2542. updatepreview();
  2543. /* supporting window for NetWMCheck */
  2544. wmcheckwin = XCreateSimpleWindow(dpy, root, 0, 0, 1, 1, 0, 0, 0);
  2545. XChangeProperty(dpy, wmcheckwin, netatom[NetWMCheck], XA_WINDOW, 32,
  2546. PropModeReplace, (unsigned char *)&wmcheckwin, 1);
  2547. XChangeProperty(dpy, wmcheckwin, netatom[NetWMName], utf8string, 8,
  2548. PropModeReplace, (unsigned char *)"dwm", 3);
  2549. XChangeProperty(dpy, root, netatom[NetWMCheck], XA_WINDOW, 32,
  2550. PropModeReplace, (unsigned char *)&wmcheckwin, 1);
  2551. /* EWMH support per view */
  2552. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  2553. PropModeReplace, (unsigned char *)netatom, NetLast);
  2554. setnumdesktops();
  2555. setcurrentdesktop();
  2556. setdesktopnames();
  2557. setviewport();
  2558. XDeleteProperty(dpy, root, netatom[NetClientList]);
  2559. /* select events */
  2560. wa.cursor = cursor[CurNormal]->cursor;
  2561. wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask |
  2562. ButtonPressMask | PointerMotionMask | EnterWindowMask |
  2563. LeaveWindowMask | StructureNotifyMask | PropertyChangeMask;
  2564. XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
  2565. XSelectInput(dpy, root, wa.event_mask);
  2566. grabkeys();
  2567. focus(NULL);
  2568. }
  2569. void
  2570. setviewport(void){
  2571. long data[] = { 0, 0 };
  2572. XChangeProperty(dpy, root, netatom[NetDesktopViewport], XA_CARDINAL, 32, PropModeReplace, (unsigned char *)data, 2);
  2573. }
  2574. void seturgent(Client *c, int urg) {
  2575. XWMHints *wmh;
  2576. c->isurgent = urg;
  2577. if (!(wmh = XGetWMHints(dpy, c->win)))
  2578. return;
  2579. wmh->flags = urg ? (wmh->flags | XUrgencyHint) : (wmh->flags & ~XUrgencyHint);
  2580. XSetWMHints(dpy, c->win, wmh);
  2581. XFree(wmh);
  2582. }
  2583. void
  2584. show(Client *c)
  2585. {
  2586. if (!c || !HIDDEN(c))
  2587. return;
  2588. XMapWindow(dpy, c->win);
  2589. setclientstate(c, NormalState);
  2590. arrange(c->mon);
  2591. }
  2592. void showhide(Client *c) {
  2593. if (!c)
  2594. return;
  2595. if (ISVISIBLE(c)) {
  2596. /* show clients top down */
  2597. XMoveWindow(dpy, c->win, c->x, c->y);
  2598. if ((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) &&
  2599. !c->isfullscreen)
  2600. resize(c, c->x, c->y, c->w, c->h, 0);
  2601. showhide(c->snext);
  2602. } else {
  2603. /* hide clients bottom up */
  2604. showhide(c->snext);
  2605. XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
  2606. }
  2607. }
  2608. void
  2609. showtagpreview(int tag)
  2610. {
  2611. if (!selmon->previewshow) {
  2612. XUnmapWindow(dpy, selmon->tagwin);
  2613. return;
  2614. }
  2615. if (selmon->tagmap[tag]) {
  2616. XSetWindowBackgroundPixmap(dpy, selmon->tagwin, selmon->tagmap[tag]);
  2617. XCopyArea(dpy, selmon->tagmap[tag], selmon->tagwin, drw->gc, 0, 0, selmon->mw / scalepreview, selmon->mh / scalepreview, 0, 0);
  2618. XSync(dpy, False);
  2619. XMapWindow(dpy, selmon->tagwin);
  2620. } else
  2621. XUnmapWindow(dpy, selmon->tagwin);
  2622. }
  2623. void sigchld(int unused) {
  2624. if (signal(SIGCHLD, sigchld) == SIG_ERR)
  2625. die("can't install SIGCHLD handler:");
  2626. while (0 < waitpid(-1, NULL, WNOHANG))
  2627. ;
  2628. }
  2629. void sighup(int unused) {
  2630. Arg a = {.i = 1};
  2631. quit(&a);
  2632. }
  2633. void sigterm(int unused) {
  2634. Arg a = {.i = 0};
  2635. quit(&a);
  2636. }
  2637. void spawn(const Arg *arg) {
  2638. if (arg->v == dmenucmd)
  2639. dmenumon[0] = '0' + selmon->num;
  2640. if (fork() == 0) {
  2641. if (dpy)
  2642. close(ConnectionNumber(dpy));
  2643. setsid();
  2644. execvp(((char **)arg->v)[0], (char **)arg->v);
  2645. fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
  2646. perror(" failed");
  2647. exit(EXIT_SUCCESS);
  2648. }
  2649. }
  2650. void switchtag(void) {
  2651. int i;
  2652. unsigned int occ = 0;
  2653. Client *c;
  2654. Imlib_Image image;
  2655. for (c = selmon->clients; c; c = c->next)
  2656. occ |= c->tags;
  2657. for (i = 0; i < LENGTH(tags); i++) {
  2658. if (selmon->tagset[selmon->seltags] & 1 << i) {
  2659. if (selmon->tagmap[i] != 0) {
  2660. XFreePixmap(dpy, selmon->tagmap[i]);
  2661. selmon->tagmap[i] = 0;
  2662. }
  2663. if (occ & 1 << i) {
  2664. image = imlib_create_image(sw, sh);
  2665. imlib_context_set_image(image);
  2666. imlib_context_set_display(dpy);
  2667. imlib_context_set_visual(DefaultVisual(dpy, screen));
  2668. imlib_context_set_drawable(RootWindow(dpy, screen));
  2669. imlib_copy_drawable_to_image(0, selmon->mx, selmon->my, selmon->mw ,selmon->mh, 0, 0, 1);
  2670. selmon->tagmap[i] = XCreatePixmap(dpy, selmon->tagwin, selmon->mw / scalepreview, selmon->mh / scalepreview, DefaultDepth(dpy, screen));
  2671. imlib_context_set_drawable(selmon->tagmap[i]);
  2672. imlib_render_image_part_on_drawable_at_size(0, 0, selmon->mw, selmon->mh, 0, 0, selmon->mw / scalepreview, selmon->mh / scalepreview);
  2673. imlib_free_image();
  2674. }
  2675. }
  2676. }
  2677. }
  2678. void
  2679. tabmode(const Arg *arg)
  2680. {
  2681. if(arg && arg->i >= 0)
  2682. selmon->showtab = arg->ui % showtab_nmodes;
  2683. else
  2684. selmon->showtab = (selmon->showtab + 1 ) % showtab_nmodes;
  2685. arrange(selmon);
  2686. }
  2687. void tag(const Arg *arg) {
  2688. if (selmon->sel && arg->ui & TAGMASK) {
  2689. selmon->sel->tags = arg->ui & TAGMASK;
  2690. focus(NULL);
  2691. arrange(selmon);
  2692. }
  2693. }
  2694. void tagmon(const Arg *arg) {
  2695. if (!selmon->sel || !mons->next)
  2696. return;
  2697. sendmon(selmon->sel, dirtomon(arg->i));
  2698. }
  2699. void togglebar(const Arg *arg) {
  2700. selmon->showbar = selmon->pertag->showbars[selmon->pertag->curtag] = !selmon->showbar;
  2701. updatebarpos(selmon);
  2702. resizebarwin(selmon);
  2703. if (showsystray) {
  2704. XWindowChanges wc;
  2705. if (!selmon->showbar)
  2706. wc.y = -bh;
  2707. else if (selmon->showbar) {
  2708. wc.y = selmon->gappoh;
  2709. if (!selmon->topbar)
  2710. wc.y = selmon->mh - bh + selmon->gappoh;
  2711. }
  2712. XConfigureWindow(dpy, systray->win, CWY, &wc);
  2713. }
  2714. arrange(selmon);
  2715. }
  2716. void togglefloating(const Arg *arg) {
  2717. if (!selmon->sel)
  2718. return;
  2719. if (selmon->sel->isfullscreen) /* no support for fullscreen windows */
  2720. return;
  2721. selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
  2722. if (selmon->sel->isfloating)
  2723. resize(selmon->sel, selmon->sel->x, selmon->sel->y, selmon->sel->w,
  2724. selmon->sel->h, 0);
  2725. arrange(selmon);
  2726. }
  2727. void togglefullscr(const Arg *arg) {
  2728. if (selmon->sel)
  2729. setfullscreen(selmon->sel, !selmon->sel->isfullscreen);
  2730. }
  2731. void toggletag(const Arg *arg) {
  2732. unsigned int newtags;
  2733. if (!selmon->sel)
  2734. return;
  2735. newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
  2736. if (newtags) {
  2737. selmon->sel->tags = newtags;
  2738. focus(NULL);
  2739. arrange(selmon);
  2740. }
  2741. updatecurrentdesktop();
  2742. }
  2743. void toggleview(const Arg *arg) {
  2744. unsigned int newtagset =
  2745. selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
  2746. int i;
  2747. if (newtagset) {
  2748. switchtag();
  2749. selmon->tagset[selmon->seltags] = newtagset;
  2750. if (newtagset == ~0) {
  2751. selmon->pertag->prevtag = selmon->pertag->curtag;
  2752. selmon->pertag->curtag = 0;
  2753. }
  2754. /* test if the user did not select the same tag */
  2755. if (!(newtagset & 1 << (selmon->pertag->curtag - 1))) {
  2756. selmon->pertag->prevtag = selmon->pertag->curtag;
  2757. for (i = 0; !(newtagset & 1 << i); i++) ;
  2758. selmon->pertag->curtag = i + 1;
  2759. }
  2760. /* apply settings for this view */
  2761. selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag];
  2762. selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag];
  2763. selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag];
  2764. selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt];
  2765. selmon->lt[selmon->sellt^1] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt^1];
  2766. if (selmon->showbar != selmon->pertag->showbars[selmon->pertag->curtag])
  2767. togglebar(NULL);
  2768. focus(NULL);
  2769. arrange(selmon);
  2770. }
  2771. updatecurrentdesktop();
  2772. }
  2773. void hidewin(const Arg *arg) {
  2774. if (!selmon->sel)
  2775. return;
  2776. Client *c = (Client*)selmon->sel;
  2777. hide(c);
  2778. hiddenWinStack[++hiddenWinStackTop] = c;
  2779. }
  2780. void restorewin(const Arg *arg) {
  2781. int i = hiddenWinStackTop;
  2782. while (i > -1) {
  2783. if (HIDDEN(hiddenWinStack[i]) && hiddenWinStack[i]->tags == selmon->tagset[selmon->seltags]) {
  2784. show(hiddenWinStack[i]);
  2785. focus(hiddenWinStack[i]);
  2786. restack(selmon);
  2787. for (int j = i; j < hiddenWinStackTop; ++j) {
  2788. hiddenWinStack[j] = hiddenWinStack[j + 1];
  2789. }
  2790. --hiddenWinStackTop;
  2791. return;
  2792. }
  2793. --i;
  2794. }
  2795. }
  2796. void unfocus(Client *c, int setfocus) {
  2797. if (!c)
  2798. return;
  2799. grabbuttons(c, 0);
  2800. XSetWindowBorder(dpy, c->win, scheme[SchemeNorm][ColBorder].pixel);
  2801. if (setfocus) {
  2802. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  2803. XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
  2804. }
  2805. }
  2806. void unmanage(Client *c, int destroyed) {
  2807. Monitor *m = c->mon;
  2808. XWindowChanges wc;
  2809. detach(c);
  2810. detachstack(c);
  2811. if (!destroyed) {
  2812. wc.border_width = c->oldbw;
  2813. XGrabServer(dpy); /* avoid race conditions */
  2814. XSetErrorHandler(xerrordummy);
  2815. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  2816. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  2817. setclientstate(c, WithdrawnState);
  2818. XSync(dpy, False);
  2819. XSetErrorHandler(xerror);
  2820. XUngrabServer(dpy);
  2821. }
  2822. free(c);
  2823. focus(NULL);
  2824. updateclientlist();
  2825. arrange(m);
  2826. }
  2827. void unmapnotify(XEvent *e) {
  2828. Client *c;
  2829. XUnmapEvent *ev = &e->xunmap;
  2830. if ((c = wintoclient(ev->window))) {
  2831. if (ev->send_event)
  2832. setclientstate(c, WithdrawnState);
  2833. else
  2834. unmanage(c, 0);
  2835. } else if ((c = wintosystrayicon(ev->window))) {
  2836. /* KLUDGE! sometimes icons occasionally unmap their windows, but do
  2837. * _not_ destroy them. We map those windows back */
  2838. XMapRaised(dpy, c->win);
  2839. updatesystray();
  2840. }
  2841. }
  2842. void updatebars(void) {
  2843. unsigned int w;
  2844. Monitor *m;
  2845. XSetWindowAttributes wa = {.override_redirect = True,
  2846. .background_pixmap = ParentRelative,
  2847. .event_mask = ButtonPressMask|ExposureMask|PointerMotionMask};
  2848. XClassHint ch = {"dwm", "dwm"};
  2849. for (m = mons; m; m = m->next) {
  2850. if (m->barwin)
  2851. continue;
  2852. w = m->ww;
  2853. if (showsystray && m == systraytomon(m))
  2854. w -= getsystraywidth();
  2855. m->barwin = XCreateWindow(
  2856. dpy, root, m->wx + m->gappov, m->by, w - 2 * m->gappov, bh, 0,
  2857. DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
  2858. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  2859. XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
  2860. if (showsystray && m == systraytomon(m))
  2861. XMapRaised(dpy, systray->win);
  2862. XMapRaised(dpy, m->barwin);
  2863. m->tabwin = XCreateWindow(dpy, root, m->wx + m->gappov, m->ty, m->ww - 2 * m->gappov, th, 0, DefaultDepth(dpy, screen),
  2864. CopyFromParent, DefaultVisual(dpy, screen),
  2865. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  2866. XDefineCursor(dpy, m->tabwin, cursor[CurNormal]->cursor);
  2867. XMapRaised(dpy, m->tabwin);
  2868. XSetClassHint(dpy, m->barwin, &ch);
  2869. }
  2870. }
  2871. void
  2872. updatepreview(void)
  2873. {
  2874. Monitor *m;
  2875. XSetWindowAttributes wa = {
  2876. .override_redirect = True,
  2877. .background_pixmap = ParentRelative,
  2878. .event_mask = ButtonPressMask|ExposureMask
  2879. };
  2880. for (m = mons; m; m = m->next) {
  2881. m->tagwin = XCreateWindow(dpy, root, m->wx, m->by + bh, m->mw / 4, m->mh / 4, 0,
  2882. DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
  2883. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  2884. XDefineCursor(dpy, m->tagwin, cursor[CurNormal]->cursor);
  2885. XMapRaised(dpy, m->tagwin);
  2886. XUnmapWindow(dpy, m->tagwin);
  2887. }
  2888. }
  2889. void updatebarpos(Monitor *m) {
  2890. Client *c;
  2891. int nvis = 0;
  2892. m->wy = m->my;
  2893. m->wh = m->mh;
  2894. for(c = m->clients; c; c = c->next) {
  2895. if(ISVISIBLE(c)) ++nvis;
  2896. }
  2897. if(m->showtab == showtab_always
  2898. || ((m->showtab == showtab_auto) && (nvis > 1) && (m->lt[m->sellt]->arrange == monocle))) {
  2899. m->topbar = !toptab;
  2900. m->wh -= th + ((m->topbar == toptab && m->showbar) ? 0 : m->gappoh) - m->gappoh;
  2901. m->ty = m->toptab ? m->wy + ((m->topbar && m->showbar) ? 0 : m->gappoh) : m->wy + m->wh - m->gappoh;
  2902. if ( m->toptab )
  2903. m->wy += th + ((m->topbar && m->showbar) ? 0 : m->gappoh) - m->gappoh;
  2904. } else {
  2905. m->ty = -th - m->gappoh;
  2906. m->topbar = topbar;
  2907. }
  2908. if (m->showbar) {
  2909. m->wh = m->wh - m->gappoh - bh;
  2910. m->by = m->topbar ? m->wy + m->gappoh : m->wy + m->wh;
  2911. if (m->topbar)
  2912. m->wy += bh + m->gappoh;
  2913. } else
  2914. m->by = -bh - m->gappoh;
  2915. }
  2916. void updateclientlist() {
  2917. Client *c;
  2918. Monitor *m;
  2919. XDeleteProperty(dpy, root, netatom[NetClientList]);
  2920. for (m = mons; m; m = m->next)
  2921. for (c = m->clients; c; c = c->next)
  2922. XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32,
  2923. PropModeAppend, (unsigned char *)&(c->win), 1);
  2924. }
  2925. void updatecurrentdesktop(void){
  2926. long rawdata[] = { selmon->tagset[selmon->seltags] };
  2927. int i=0;
  2928. while(*rawdata >> (i+1)){
  2929. i++;
  2930. }
  2931. long data[] = { i };
  2932. XChangeProperty(dpy, root, netatom[NetCurrentDesktop], XA_CARDINAL, 32, PropModeReplace, (unsigned char *)data, 1);
  2933. }
  2934. int updategeom(void) {
  2935. int dirty = 0;
  2936. #ifdef XINERAMA
  2937. if (XineramaIsActive(dpy)) {
  2938. int i, j, n, nn;
  2939. Client *c;
  2940. Monitor *m;
  2941. XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
  2942. XineramaScreenInfo *unique = NULL;
  2943. for (n = 0, m = mons; m; m = m->next, n++)
  2944. ;
  2945. /* only consider unique geometries as separate screens */
  2946. unique = ecalloc(nn, sizeof(XineramaScreenInfo));
  2947. for (i = 0, j = 0; i < nn; i++)
  2948. if (isuniquegeom(unique, j, &info[i]))
  2949. memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
  2950. XFree(info);
  2951. nn = j;
  2952. if (n <= nn) { /* new monitors available */
  2953. for (i = 0; i < (nn - n); i++) {
  2954. for (m = mons; m && m->next; m = m->next)
  2955. ;
  2956. if (m)
  2957. m->next = createmon();
  2958. else
  2959. mons = createmon();
  2960. }
  2961. for (i = 0, m = mons; i < nn && m; m = m->next, i++)
  2962. if (i >= n || unique[i].x_org != m->mx || unique[i].y_org != m->my ||
  2963. unique[i].width != m->mw || unique[i].height != m->mh) {
  2964. dirty = 1;
  2965. m->num = i;
  2966. m->mx = m->wx = unique[i].x_org;
  2967. m->my = m->wy = unique[i].y_org;
  2968. m->mw = m->ww = unique[i].width;
  2969. m->mh = m->wh = unique[i].height;
  2970. updatebarpos(m);
  2971. }
  2972. } else { /* less monitors available nn < n */
  2973. for (i = nn; i < n; i++) {
  2974. for (m = mons; m && m->next; m = m->next)
  2975. ;
  2976. while ((c = m->clients)) {
  2977. dirty = 1;
  2978. m->clients = c->next;
  2979. detachstack(c);
  2980. c->mon = mons;
  2981. attach(c);
  2982. attachstack(c);
  2983. }
  2984. if (m == selmon)
  2985. selmon = mons;
  2986. cleanupmon(m);
  2987. }
  2988. }
  2989. free(unique);
  2990. } else
  2991. #endif /* XINERAMA */
  2992. { /* default monitor setup */
  2993. if (!mons)
  2994. mons = createmon();
  2995. if (mons->mw != sw || mons->mh != sh) {
  2996. dirty = 1;
  2997. mons->mw = mons->ww = sw;
  2998. mons->mh = mons->wh = sh;
  2999. updatebarpos(mons);
  3000. }
  3001. }
  3002. if (dirty) {
  3003. selmon = mons;
  3004. selmon = wintomon(root);
  3005. }
  3006. return dirty;
  3007. }
  3008. void updatenumlockmask(void) {
  3009. unsigned int i, j;
  3010. XModifierKeymap *modmap;
  3011. numlockmask = 0;
  3012. modmap = XGetModifierMapping(dpy);
  3013. for (i = 0; i < 8; i++)
  3014. for (j = 0; j < modmap->max_keypermod; j++)
  3015. if (modmap->modifiermap[i * modmap->max_keypermod + j] ==
  3016. XKeysymToKeycode(dpy, XK_Num_Lock))
  3017. numlockmask = (1 << i);
  3018. XFreeModifiermap(modmap);
  3019. }
  3020. void updatesizehints(Client *c) {
  3021. long msize;
  3022. XSizeHints size;
  3023. if (!XGetWMNormalHints(dpy, c->win, &size, &msize))
  3024. /* size is uninitialized, ensure that size.flags aren't used */
  3025. size.flags = PSize;
  3026. if (size.flags & PBaseSize) {
  3027. c->basew = size.base_width;
  3028. c->baseh = size.base_height;
  3029. } else if (size.flags & PMinSize) {
  3030. c->basew = size.min_width;
  3031. c->baseh = size.min_height;
  3032. } else
  3033. c->basew = c->baseh = 0;
  3034. if (size.flags & PResizeInc) {
  3035. c->incw = size.width_inc;
  3036. c->inch = size.height_inc;
  3037. } else
  3038. c->incw = c->inch = 0;
  3039. if (size.flags & PMaxSize) {
  3040. c->maxw = size.max_width;
  3041. c->maxh = size.max_height;
  3042. } else
  3043. c->maxw = c->maxh = 0;
  3044. if (size.flags & PMinSize) {
  3045. c->minw = size.min_width;
  3046. c->minh = size.min_height;
  3047. } else if (size.flags & PBaseSize) {
  3048. c->minw = size.base_width;
  3049. c->minh = size.base_height;
  3050. } else
  3051. c->minw = c->minh = 0;
  3052. if (size.flags & PAspect) {
  3053. c->mina = (float)size.min_aspect.y / size.min_aspect.x;
  3054. c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
  3055. } else
  3056. c->maxa = c->mina = 0.0;
  3057. c->isfixed = (c->maxw && c->maxh && c->maxw == c->minw && c->maxh == c->minh);
  3058. }
  3059. void updatestatus(void) {
  3060. if (!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
  3061. strcpy(stext, "dwm-" VERSION);
  3062. drawbar(selmon);
  3063. updatesystray();
  3064. }
  3065. void updatesystrayicongeom(Client *i, int w, int h) {
  3066. int rh = bh - vertpadbar;
  3067. if (i) {
  3068. i->h = rh;
  3069. if (w == h)
  3070. i->w = rh;
  3071. else if (h == rh)
  3072. i->w = w;
  3073. else
  3074. i->w = (int)((float)rh * ((float)w / (float)h));
  3075. i->y = i->y + vertpadbar / 2;
  3076. applysizehints(i, &(i->x), &(i->y), &(i->w), &(i->h), False);
  3077. /* force icons into the systray dimensions if they don't want to */
  3078. if (i->h > rh) {
  3079. if (i->w == i->h)
  3080. i->w = rh;
  3081. else
  3082. i->w = (int)((float)rh * ((float)i->w / (float)i->h));
  3083. i->h = rh;
  3084. }
  3085. }
  3086. }
  3087. void updatesystrayiconstate(Client *i, XPropertyEvent *ev) {
  3088. long flags;
  3089. int code = 0;
  3090. if (!showsystray || !i || ev->atom != xatom[XembedInfo] ||
  3091. !(flags = getatomprop(i, xatom[XembedInfo])))
  3092. return;
  3093. if (flags & XEMBED_MAPPED && !i->tags) {
  3094. i->tags = 1;
  3095. code = XEMBED_WINDOW_ACTIVATE;
  3096. XMapRaised(dpy, i->win);
  3097. setclientstate(i, NormalState);
  3098. } else if (!(flags & XEMBED_MAPPED) && i->tags) {
  3099. i->tags = 0;
  3100. code = XEMBED_WINDOW_DEACTIVATE;
  3101. XUnmapWindow(dpy, i->win);
  3102. setclientstate(i, WithdrawnState);
  3103. } else
  3104. return;
  3105. sendevent(i->win, xatom[Xembed], StructureNotifyMask, CurrentTime, code, 0,
  3106. systray->win, XEMBED_EMBEDDED_VERSION);
  3107. }
  3108. void updatesystray(void) {
  3109. XSetWindowAttributes wa;
  3110. XWindowChanges wc;
  3111. Client *i;
  3112. Monitor *m = systraytomon(NULL);
  3113. unsigned int x = m->mx + m->mw - m->gappov;
  3114. unsigned int w = 1;
  3115. if (!showsystray)
  3116. return;
  3117. if (!systray) {
  3118. /* init systray */
  3119. if (!(systray = (Systray *)calloc(1, sizeof(Systray))))
  3120. die("fatal: could not malloc() %u bytes\n", sizeof(Systray));
  3121. systray->win = XCreateSimpleWindow(dpy, root, x, m->by, w, bh, 0, 0,
  3122. scheme[SchemeSel][ColBg].pixel);
  3123. wa.event_mask = ButtonPressMask | ExposureMask;
  3124. wa.override_redirect = True;
  3125. wa.background_pixel = scheme[SchemeNorm][ColBg].pixel;
  3126. XSelectInput(dpy, systray->win, SubstructureNotifyMask);
  3127. XChangeProperty(dpy, systray->win, netatom[NetSystemTrayOrientation],
  3128. XA_CARDINAL, 32, PropModeReplace,
  3129. (unsigned char *)&netatom[NetSystemTrayOrientationHorz], 1);
  3130. XChangeWindowAttributes(
  3131. dpy, systray->win, CWEventMask | CWOverrideRedirect | CWBackPixel, &wa);
  3132. XMapRaised(dpy, systray->win);
  3133. XSetSelectionOwner(dpy, netatom[NetSystemTray], systray->win, CurrentTime);
  3134. if (XGetSelectionOwner(dpy, netatom[NetSystemTray]) == systray->win) {
  3135. sendevent(root, xatom[Manager], StructureNotifyMask, CurrentTime,
  3136. netatom[NetSystemTray], systray->win, 0, 0);
  3137. XSync(dpy, False);
  3138. } else {
  3139. fprintf(stderr, "dwm: unable to obtain system tray.\n");
  3140. free(systray);
  3141. systray = NULL;
  3142. return;
  3143. }
  3144. }
  3145. for (w = 0, i = systray->icons; i; i = i->next) {
  3146. /* make sure the background color stays the same */
  3147. wa.background_pixel = scheme[SchemeNorm][ColBg].pixel;
  3148. XChangeWindowAttributes(dpy, i->win, CWBackPixel, &wa);
  3149. XMapRaised(dpy, i->win);
  3150. w += systrayspacing;
  3151. i->x = w;
  3152. XMoveResizeWindow(dpy, i->win, i->x, vertpadbar / 2, i->w, i->h);
  3153. w += i->w;
  3154. if (i->mon != m)
  3155. i->mon = m;
  3156. }
  3157. w = w ? w + systrayspacing : 1;
  3158. x -= w;
  3159. XMoveResizeWindow(dpy, systray->win, x, m->by, w, bh);
  3160. wc.x = x;
  3161. wc.y = m->by;
  3162. wc.width = w;
  3163. wc.height = bh;
  3164. wc.stack_mode = Above;
  3165. wc.sibling = m->barwin;
  3166. XConfigureWindow(dpy, systray->win,
  3167. CWX | CWY | CWWidth | CWHeight | CWSibling | CWStackMode,
  3168. &wc);
  3169. XMapWindow(dpy, systray->win);
  3170. XMapSubwindows(dpy, systray->win);
  3171. /* redraw background */
  3172. XSetForeground(dpy, drw->gc, scheme[SchemeNorm][ColBg].pixel);
  3173. XFillRectangle(dpy, systray->win, drw->gc, 0, 0, w, bh);
  3174. XSync(dpy, False);
  3175. }
  3176. void updatetitle(Client *c) {
  3177. if (!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  3178. gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
  3179. if (c->name[0] == '\0') /* hack to mark broken clients */
  3180. strcpy(c->name, broken);
  3181. }
  3182. void updatewindowtype(Client *c) {
  3183. Atom state = getatomprop(c, netatom[NetWMState]);
  3184. Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
  3185. if (state == netatom[NetWMFullscreen])
  3186. setfullscreen(c, 1);
  3187. if (wtype == netatom[NetWMWindowTypeDialog]) {
  3188. c->iscentered = 1;
  3189. c->isfloating = 1;
  3190. }
  3191. }
  3192. void updatewmhints(Client *c) {
  3193. XWMHints *wmh;
  3194. if ((wmh = XGetWMHints(dpy, c->win))) {
  3195. if (c == selmon->sel && wmh->flags & XUrgencyHint) {
  3196. wmh->flags &= ~XUrgencyHint;
  3197. XSetWMHints(dpy, c->win, wmh);
  3198. } else
  3199. c->isurgent = (wmh->flags & XUrgencyHint) ? 1 : 0;
  3200. if (wmh->flags & InputHint)
  3201. c->neverfocus = !wmh->input;
  3202. else
  3203. c->neverfocus = 0;
  3204. XFree(wmh);
  3205. }
  3206. }
  3207. void view(const Arg *arg) {
  3208. int i;
  3209. unsigned int tmptag;
  3210. if ((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
  3211. return;
  3212. switchtag();
  3213. selmon->seltags ^= 1; /* toggle sel tagset */
  3214. if (arg->ui & TAGMASK) {
  3215. selmon->pertag->prevtag = selmon->pertag->curtag;
  3216. if (arg->ui == ~0)
  3217. selmon->pertag->curtag = 0;
  3218. else {
  3219. for (i = 0; !(arg->ui & 1 << i); i++) ;
  3220. selmon->pertag->curtag = i + 1;
  3221. }
  3222. } else {
  3223. tmptag = selmon->pertag->prevtag;
  3224. selmon->pertag->prevtag = selmon->pertag->curtag;
  3225. selmon->pertag->curtag = tmptag;
  3226. }
  3227. selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag];
  3228. selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag];
  3229. selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag];
  3230. selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt];
  3231. selmon->lt[selmon->sellt^1] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt^1];
  3232. if (selmon->showbar != selmon->pertag->showbars[selmon->pertag->curtag])
  3233. togglebar(NULL);
  3234. selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
  3235. focus(NULL);
  3236. arrange(selmon);
  3237. updatecurrentdesktop();
  3238. }
  3239. Client *wintoclient(Window w) {
  3240. Client *c;
  3241. Monitor *m;
  3242. for (m = mons; m; m = m->next)
  3243. for (c = m->clients; c; c = c->next)
  3244. if (c->win == w)
  3245. return c;
  3246. return NULL;
  3247. }
  3248. Client *wintosystrayicon(Window w) {
  3249. Client *i = NULL;
  3250. if (!showsystray || !w)
  3251. return i;
  3252. for (i = systray->icons; i && i->win != w; i = i->next)
  3253. ;
  3254. return i;
  3255. }
  3256. Monitor *wintomon(Window w) {
  3257. int x, y;
  3258. Client *c;
  3259. Monitor *m;
  3260. if (w == root && getrootptr(&x, &y))
  3261. return recttomon(x, y, 1, 1);
  3262. for (m = mons; m; m = m->next)
  3263. if (w == m->barwin || w == m->tabwin)
  3264. return m;
  3265. if ((c = wintoclient(w)))
  3266. return c->mon;
  3267. return selmon;
  3268. }
  3269. /* There's no way to check accesses to destroyed windows, thus those cases are
  3270. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  3271. * default error handler, which may call exit. */
  3272. int xerror(Display *dpy, XErrorEvent *ee) {
  3273. if (ee->error_code == BadWindow ||
  3274. (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch) ||
  3275. (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable) ||
  3276. (ee->request_code == X_PolyFillRectangle &&
  3277. ee->error_code == BadDrawable) ||
  3278. (ee->request_code == X_PolySegment && ee->error_code == BadDrawable) ||
  3279. (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch) ||
  3280. (ee->request_code == X_GrabButton && ee->error_code == BadAccess) ||
  3281. (ee->request_code == X_GrabKey && ee->error_code == BadAccess) ||
  3282. (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  3283. return 0;
  3284. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  3285. ee->request_code, ee->error_code);
  3286. return xerrorxlib(dpy, ee); /* may call exit */
  3287. }
  3288. int xerrordummy(Display *dpy, XErrorEvent *ee) { return 0; }
  3289. /* Startup Error handler to check if another window manager
  3290. * is already running. */
  3291. int xerrorstart(Display *dpy, XErrorEvent *ee) {
  3292. die("dwm: another window manager is already running");
  3293. return -1;
  3294. }
  3295. Monitor *systraytomon(Monitor *m) {
  3296. Monitor *t;
  3297. int i, n;
  3298. if (!systraypinning) {
  3299. if (!m)
  3300. return selmon;
  3301. return m == selmon ? m : NULL;
  3302. }
  3303. for (n = 1, t = mons; t && t->next; n++, t = t->next)
  3304. ;
  3305. for (i = 1, t = mons; t && t->next && i < systraypinning; i++, t = t->next)
  3306. ;
  3307. if (systraypinningfailfirst && n < systraypinning)
  3308. return mons;
  3309. return t;
  3310. }
  3311. void zoom(const Arg *arg) {
  3312. Client *c = selmon->sel;
  3313. if (!selmon->lt[selmon->sellt]->arrange ||
  3314. (selmon->sel && selmon->sel->isfloating))
  3315. return;
  3316. if (c == nexttiled(selmon->clients))
  3317. if (!c || !(c = nexttiled(c->next)))
  3318. return;
  3319. pop(c);
  3320. }
  3321. int main(int argc, char *argv[]) {
  3322. if (argc == 2 && !strcmp("-v", argv[1]))
  3323. die("dwm-" VERSION);
  3324. else if (argc != 1 && strcmp("-s", argv[1]))
  3325. die("usage: dwm [-v]");
  3326. if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  3327. fputs("warning: no locale support\n", stderr);
  3328. if (!(dpy = XOpenDisplay(NULL)))
  3329. die("dwm: cannot open display");
  3330. if (argc > 1 && !strcmp("-s", argv[1])) {
  3331. XStoreName(dpy, RootWindow(dpy, DefaultScreen(dpy)), argv[2]);
  3332. XCloseDisplay(dpy);
  3333. return 0;
  3334. }
  3335. checkotherwm();
  3336. setup();
  3337. #ifdef __OpenBSD__
  3338. if (pledge("stdio rpath proc exec", NULL) == -1)
  3339. die("pledge");
  3340. #endif /* __OpenBSD__ */
  3341. scan();
  3342. run();
  3343. if (restart)
  3344. execvp(argv[0], argv);
  3345. cleanup();
  3346. XCloseDisplay(dpy);
  3347. return EXIT_SUCCESS;
  3348. }