dwm.c.orig 76 KB

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