dwm.c 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133
  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 + gappx)
  57. #define HEIGHT(X) ((X)->h + 2 * (X)->bw + gappx)
  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. unsigned int n;
  1591. unsigned int gapoffset;
  1592. unsigned int gapincr;
  1593. Client *nbc;
  1594. wc.border_width = c->bw;
  1595. /* Get number of clients for the selected monitor */
  1596. for (n = 0, nbc = nexttiled(selmon->clients); nbc; nbc = nexttiled(nbc->next), n++);
  1597. /* Do nothing if layout is floating */
  1598. if (c->isfloating || selmon->lt[selmon->sellt]->arrange == NULL) {
  1599. gapincr = gapoffset = 0;
  1600. } else {
  1601. /* Remove border and gap if layout is monocle or only one client */
  1602. if (selmon->lt[selmon->sellt]->arrange == monocle || n == 1) {
  1603. gapoffset = 0;
  1604. gapincr = -2 * borderpx;
  1605. wc.border_width = 0;
  1606. } else {
  1607. gapoffset = gappx;
  1608. gapincr = 2 * gappx;
  1609. }
  1610. }
  1611. c->oldx = c->x; c->x = wc.x = x + gapoffset;
  1612. c->oldy = c->y; c->y = wc.y = y + gapoffset;
  1613. c->oldw = c->w; c->w = wc.width = w - gapincr;
  1614. c->oldh = c->h; c->h = wc.height = h - gapincr;
  1615. XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  1616. configure(c);
  1617. XSync(dpy, False);
  1618. }
  1619. void
  1620. resizemouse(const Arg *arg)
  1621. {
  1622. int ocx, ocy, nw, nh;
  1623. Client *c;
  1624. Monitor *m;
  1625. XEvent ev;
  1626. Time lasttime = 0;
  1627. if (!(c = selmon->sel))
  1628. return;
  1629. if (c->isfullscreen) /* no support resizing fullscreen windows by mouse */
  1630. return;
  1631. restack(selmon);
  1632. ocx = c->x;
  1633. ocy = c->y;
  1634. if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1635. None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
  1636. return;
  1637. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1638. do {
  1639. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1640. switch(ev.type) {
  1641. case ConfigureRequest:
  1642. case Expose:
  1643. case MapRequest:
  1644. handler[ev.type](&ev);
  1645. break;
  1646. case MotionNotify:
  1647. if ((ev.xmotion.time - lasttime) <= (1000 / 60))
  1648. continue;
  1649. lasttime = ev.xmotion.time;
  1650. nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
  1651. nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
  1652. if (c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
  1653. && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
  1654. {
  1655. if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1656. && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
  1657. togglefloating(NULL);
  1658. }
  1659. if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
  1660. resize(c, c->x, c->y, nw, nh, 1);
  1661. break;
  1662. }
  1663. } while (ev.type != ButtonRelease);
  1664. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1665. XUngrabPointer(dpy, CurrentTime);
  1666. while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1667. if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
  1668. sendmon(c, m);
  1669. selmon = m;
  1670. focus(NULL);
  1671. }
  1672. }
  1673. void
  1674. resizerequest(XEvent *e)
  1675. {
  1676. XResizeRequestEvent *ev = &e->xresizerequest;
  1677. Client *i;
  1678. if ((i = wintosystrayicon(ev->window))) {
  1679. updatesystrayicongeom(i, ev->width, ev->height);
  1680. resizebarwin(selmon);
  1681. updatesystray();
  1682. }
  1683. }
  1684. void
  1685. restack(Monitor *m)
  1686. {
  1687. Client *c;
  1688. XEvent ev;
  1689. XWindowChanges wc;
  1690. drawbar(m);
  1691. if (!m->sel)
  1692. return;
  1693. if (m->sel->isfloating || !m->lt[m->sellt]->arrange)
  1694. XRaiseWindow(dpy, m->sel->win);
  1695. if (m->lt[m->sellt]->arrange) {
  1696. wc.stack_mode = Below;
  1697. wc.sibling = m->barwin;
  1698. for (c = m->stack; c; c = c->snext)
  1699. if (!c->isfloating && ISVISIBLE(c)) {
  1700. XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
  1701. wc.sibling = c->win;
  1702. }
  1703. }
  1704. XSync(dpy, False);
  1705. while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1706. }
  1707. void
  1708. run(void)
  1709. {
  1710. XEvent ev;
  1711. /* main event loop */
  1712. XSync(dpy, False);
  1713. while (running && !XNextEvent(dpy, &ev))
  1714. if (handler[ev.type])
  1715. handler[ev.type](&ev); /* call handler */
  1716. }
  1717. void
  1718. scan(void)
  1719. {
  1720. scanner = 1;
  1721. unsigned int i, num;
  1722. char swin[256];
  1723. Window d1, d2, *wins = NULL;
  1724. XWindowAttributes wa;
  1725. if (XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1726. for (i = 0; i < num; i++) {
  1727. if (!XGetWindowAttributes(dpy, wins[i], &wa)
  1728. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1729. continue;
  1730. if (wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1731. manage(wins[i], &wa);
  1732. else if (gettextprop(wins[i], netatom[NetClientList], swin, sizeof swin))
  1733. manage(wins[i], &wa);
  1734. }
  1735. for (i = 0; i < num; i++) { /* now the transients */
  1736. if (!XGetWindowAttributes(dpy, wins[i], &wa))
  1737. continue;
  1738. if (XGetTransientForHint(dpy, wins[i], &d1)
  1739. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1740. manage(wins[i], &wa);
  1741. }
  1742. if (wins)
  1743. XFree(wins);
  1744. }
  1745. scanner = 0;
  1746. }
  1747. void
  1748. sendmon(Client *c, Monitor *m)
  1749. {
  1750. if (c->mon == m)
  1751. return;
  1752. unfocus(c, 1);
  1753. detach(c);
  1754. detachstack(c);
  1755. c->mon = m;
  1756. c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
  1757. attach(c);
  1758. attachstack(c);
  1759. focus(NULL);
  1760. arrange(NULL);
  1761. }
  1762. void
  1763. setclientstate(Client *c, long state)
  1764. {
  1765. long data[] = { state, None };
  1766. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1767. PropModeReplace, (unsigned char *)data, 2);
  1768. }
  1769. int
  1770. sendevent(Window w, Atom proto, int mask, long d0, long d1, long d2, long d3, long d4)
  1771. {
  1772. int n;
  1773. Atom *protocols, mt;
  1774. int exists = 0;
  1775. XEvent ev;
  1776. if (proto == wmatom[WMTakeFocus] || proto == wmatom[WMDelete]) {
  1777. mt = wmatom[WMProtocols];
  1778. if (XGetWMProtocols(dpy, w, &protocols, &n)) {
  1779. while (!exists && n--)
  1780. exists = protocols[n] == proto;
  1781. XFree(protocols);
  1782. }
  1783. }
  1784. else {
  1785. exists = True;
  1786. mt = proto;
  1787. }
  1788. if (exists) {
  1789. ev.type = ClientMessage;
  1790. ev.xclient.window = w;
  1791. ev.xclient.message_type = mt;
  1792. ev.xclient.format = 32;
  1793. ev.xclient.data.l[0] = d0;
  1794. ev.xclient.data.l[1] = d1;
  1795. ev.xclient.data.l[2] = d2;
  1796. ev.xclient.data.l[3] = d3;
  1797. ev.xclient.data.l[4] = d4;
  1798. XSendEvent(dpy, w, False, mask, &ev);
  1799. }
  1800. return exists;
  1801. }
  1802. void
  1803. setfocus(Client *c)
  1804. {
  1805. if (!c->neverfocus) {
  1806. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  1807. XChangeProperty(dpy, root, netatom[NetActiveWindow],
  1808. XA_WINDOW, 32, PropModeReplace,
  1809. (unsigned char *) &(c->win), 1);
  1810. }
  1811. sendevent(c->win, wmatom[WMTakeFocus], NoEventMask, wmatom[WMTakeFocus], CurrentTime, 0, 0, 0);
  1812. }
  1813. void
  1814. setfullscreen(Client *c, int fullscreen)
  1815. {
  1816. if (fullscreen && !c->isfullscreen) {
  1817. XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
  1818. PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
  1819. c->isfullscreen = 1;
  1820. c->oldstate = c->isfloating;
  1821. c->oldbw = c->bw;
  1822. c->bw = 0;
  1823. c->isfloating = 1;
  1824. resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
  1825. XRaiseWindow(dpy, c->win);
  1826. } else if (!fullscreen && c->isfullscreen){
  1827. XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
  1828. PropModeReplace, (unsigned char*)0, 0);
  1829. c->isfullscreen = 0;
  1830. c->isfloating = c->oldstate;
  1831. c->bw = c->oldbw;
  1832. c->x = c->oldx;
  1833. c->y = c->oldy;
  1834. c->w = c->oldw;
  1835. c->h = c->oldh;
  1836. resizeclient(c, c->x, c->y, c->w, c->h);
  1837. arrange(c->mon);
  1838. }
  1839. }
  1840. void
  1841. setlayout(const Arg *arg)
  1842. {
  1843. if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
  1844. selmon->sellt ^= 1;
  1845. if (arg && arg->v)
  1846. selmon->lt[selmon->sellt] = (Layout *)arg->v;
  1847. strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
  1848. if (selmon->sel)
  1849. arrange(selmon);
  1850. else
  1851. drawbar(selmon);
  1852. }
  1853. void setcfact(const Arg *arg) {
  1854. float f;
  1855. Client *c;
  1856. c = selmon->sel;
  1857. if(!arg || !c || !selmon->lt[selmon->sellt]->arrange)
  1858. return;
  1859. f = arg->f + c->cfact;
  1860. if(arg->f == 0.0)
  1861. f = 1.0;
  1862. else if(f < 0.25 || f > 4.0)
  1863. return;
  1864. c->cfact = f;
  1865. arrange(selmon);
  1866. }
  1867. /* arg > 1.0 will set mfact absolutely */
  1868. void
  1869. setmfact(const Arg *arg)
  1870. {
  1871. float f;
  1872. if (!arg || !selmon->lt[selmon->sellt]->arrange)
  1873. return;
  1874. f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
  1875. if (f < 0.1 || f > 0.9)
  1876. return;
  1877. selmon->mfact = f;
  1878. arrange(selmon);
  1879. }
  1880. void
  1881. setup(void)
  1882. {
  1883. int i;
  1884. XSetWindowAttributes wa;
  1885. Atom utf8string;
  1886. /* clean up any zombies immediately */
  1887. sigchld(0);
  1888. /* init screen */
  1889. screen = DefaultScreen(dpy);
  1890. sw = DisplayWidth(dpy, screen);
  1891. sh = DisplayHeight(dpy, screen);
  1892. root = RootWindow(dpy, screen);
  1893. drw = drw_create(dpy, screen, root, sw, sh);
  1894. if (!drw_fontset_create(drw, fonts, LENGTH(fonts)))
  1895. die("no fonts could be loaded.");
  1896. lrpad = drw->fonts->h;
  1897. bh = user_bh ? user_bh : drw->fonts->h + 2;
  1898. updategeom();
  1899. /* init atoms */
  1900. utf8string = XInternAtom(dpy, "UTF8_STRING", False);
  1901. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1902. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1903. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1904. wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
  1905. netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
  1906. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1907. netatom[NetSystemTray] = XInternAtom(dpy, "_NET_SYSTEM_TRAY_S0", False);
  1908. netatom[NetSystemTrayOP] = XInternAtom(dpy, "_NET_SYSTEM_TRAY_OPCODE", False);
  1909. netatom[NetSystemTrayOrientation] = XInternAtom(dpy, "_NET_SYSTEM_TRAY_ORIENTATION", False);
  1910. netatom[NetSystemTrayOrientationHorz] = XInternAtom(dpy, "_NET_SYSTEM_TRAY_ORIENTATION_HORZ", False);
  1911. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1912. netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
  1913. netatom[NetWMCheck] = XInternAtom(dpy, "_NET_SUPPORTING_WM_CHECK", False);
  1914. netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
  1915. netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
  1916. netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
  1917. netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
  1918. xatom[Manager] = XInternAtom(dpy, "MANAGER", False);
  1919. xatom[Xembed] = XInternAtom(dpy, "_XEMBED", False);
  1920. xatom[XembedInfo] = XInternAtom(dpy, "_XEMBED_INFO", False);
  1921. /* init cursors */
  1922. cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
  1923. cursor[CurResize] = drw_cur_create(drw, XC_sizing);
  1924. cursor[CurMove] = drw_cur_create(drw, XC_fleur);
  1925. /* init appearance */
  1926. scheme = ecalloc(LENGTH(colors), sizeof(Clr *));
  1927. for (i = 0; i < LENGTH(colors); i++)
  1928. scheme[i] = drw_scm_create(drw, colors[i], 3);
  1929. /* init system tray */
  1930. updatesystray();
  1931. /* init bars */
  1932. updatebars();
  1933. updatestatus();
  1934. /* supporting window for NetWMCheck */
  1935. wmcheckwin = XCreateSimpleWindow(dpy, root, 0, 0, 1, 1, 0, 0, 0);
  1936. XChangeProperty(dpy, wmcheckwin, netatom[NetWMCheck], XA_WINDOW, 32,
  1937. PropModeReplace, (unsigned char *) &wmcheckwin, 1);
  1938. XChangeProperty(dpy, wmcheckwin, netatom[NetWMName], utf8string, 8,
  1939. PropModeReplace, (unsigned char *) "dwm", 3);
  1940. XChangeProperty(dpy, root, netatom[NetWMCheck], XA_WINDOW, 32,
  1941. PropModeReplace, (unsigned char *) &wmcheckwin, 1);
  1942. /* EWMH support per view */
  1943. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1944. PropModeReplace, (unsigned char *) netatom, NetLast);
  1945. XDeleteProperty(dpy, root, netatom[NetClientList]);
  1946. /* select events */
  1947. wa.cursor = cursor[CurNormal]->cursor;
  1948. wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask
  1949. |ButtonPressMask|PointerMotionMask|EnterWindowMask
  1950. |LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
  1951. XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
  1952. XSelectInput(dpy, root, wa.event_mask);
  1953. grabkeys();
  1954. focus(NULL);
  1955. }
  1956. void
  1957. seturgent(Client *c, int urg)
  1958. {
  1959. XWMHints *wmh;
  1960. c->isurgent = urg;
  1961. if (!(wmh = XGetWMHints(dpy, c->win)))
  1962. return;
  1963. wmh->flags = urg ? (wmh->flags | XUrgencyHint) : (wmh->flags & ~XUrgencyHint);
  1964. XSetWMHints(dpy, c->win, wmh);
  1965. XFree(wmh);
  1966. }
  1967. void
  1968. showhide(Client *c)
  1969. {
  1970. if (!c)
  1971. return;
  1972. if (ISVISIBLE(c)) {
  1973. /* show clients top down */
  1974. XMoveWindow(dpy, c->win, c->x, c->y);
  1975. if ((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
  1976. resize(c, c->x, c->y, c->w, c->h, 0);
  1977. showhide(c->snext);
  1978. } else {
  1979. /* hide clients bottom up */
  1980. showhide(c->snext);
  1981. XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
  1982. }
  1983. }
  1984. void
  1985. sigchld(int unused)
  1986. {
  1987. if (signal(SIGCHLD, sigchld) == SIG_ERR)
  1988. die("can't install SIGCHLD handler:");
  1989. while (0 < waitpid(-1, NULL, WNOHANG));
  1990. }
  1991. void
  1992. spawn(const Arg *arg)
  1993. {
  1994. if (arg->v == dmenucmd)
  1995. dmenumon[0] = '0' + selmon->num;
  1996. if (fork() == 0) {
  1997. if (dpy)
  1998. close(ConnectionNumber(dpy));
  1999. setsid();
  2000. execvp(((char **)arg->v)[0], (char **)arg->v);
  2001. fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
  2002. perror(" failed");
  2003. exit(EXIT_SUCCESS);
  2004. }
  2005. }
  2006. void
  2007. tag(const Arg *arg)
  2008. {
  2009. if (selmon->sel && arg->ui & TAGMASK) {
  2010. selmon->sel->tags = arg->ui & TAGMASK;
  2011. focus(NULL);
  2012. arrange(selmon);
  2013. }
  2014. }
  2015. void
  2016. tagmon(const Arg *arg)
  2017. {
  2018. if (!selmon->sel || !mons->next)
  2019. return;
  2020. sendmon(selmon->sel, dirtomon(arg->i));
  2021. }
  2022. void
  2023. tile(Monitor *m)
  2024. {
  2025. unsigned int i, n, h, mw, my, ty;
  2026. float mfacts = 0, sfacts = 0;
  2027. Client *c;
  2028. for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++) {
  2029. if (n < m->nmaster)
  2030. mfacts += c->cfact;
  2031. else
  2032. sfacts += c->cfact;
  2033. }
  2034. if (n == 0)
  2035. return;
  2036. if (n > m->nmaster)
  2037. mw = m->nmaster ? m->ww * m->mfact : 0;
  2038. else
  2039. mw = m->ww;
  2040. for (i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
  2041. if (i < m->nmaster) {
  2042. h = (m->wh - my) * (c->cfact / mfacts);
  2043. resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), 0);
  2044. my += HEIGHT(c);
  2045. mfacts -= c->cfact;
  2046. } else {
  2047. h = (m->wh - ty) / (n - i);
  2048. resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), 0);
  2049. ty += HEIGHT(c);
  2050. sfacts -= c->cfact;
  2051. }
  2052. }
  2053. void
  2054. togglealttag()
  2055. {
  2056. selmon->alttag = !selmon->alttag;
  2057. drawbar(selmon);
  2058. }
  2059. void
  2060. togglebar(const Arg *arg)
  2061. {
  2062. selmon->showbar = !selmon->showbar;
  2063. updatebarpos(selmon);
  2064. resizebarwin(selmon);
  2065. if (showsystray) {
  2066. XWindowChanges wc;
  2067. if (!selmon->showbar)
  2068. wc.y = -bh;
  2069. else if (selmon->showbar) {
  2070. wc.y = 0;
  2071. if (!selmon->topbar)
  2072. wc.y = selmon->mh - bh;
  2073. }
  2074. XConfigureWindow(dpy, systray->win, CWY, &wc);
  2075. }
  2076. arrange(selmon);
  2077. }
  2078. void
  2079. togglefloating(const Arg *arg)
  2080. {
  2081. if (!selmon->sel)
  2082. return;
  2083. if (selmon->sel->isfullscreen) /* no support for fullscreen windows */
  2084. return;
  2085. selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
  2086. if (selmon->sel->isfloating)
  2087. resize(selmon->sel, selmon->sel->x, selmon->sel->y,
  2088. selmon->sel->w, selmon->sel->h, 0);
  2089. arrange(selmon);
  2090. }
  2091. void
  2092. togglefullscr(const Arg *arg)
  2093. {
  2094. if(selmon->sel)
  2095. setfullscreen(selmon->sel, !selmon->sel->isfullscreen);
  2096. }
  2097. void
  2098. toggletag(const Arg *arg)
  2099. {
  2100. unsigned int newtags;
  2101. if (!selmon->sel)
  2102. return;
  2103. newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
  2104. if (newtags) {
  2105. selmon->sel->tags = newtags;
  2106. focus(NULL);
  2107. arrange(selmon);
  2108. }
  2109. }
  2110. void
  2111. toggleview(const Arg *arg)
  2112. {
  2113. unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
  2114. if (newtagset) {
  2115. selmon->tagset[selmon->seltags] = newtagset;
  2116. focus(NULL);
  2117. arrange(selmon);
  2118. }
  2119. }
  2120. void
  2121. unfocus(Client *c, int setfocus)
  2122. {
  2123. if (!c)
  2124. return;
  2125. grabbuttons(c, 0);
  2126. XSetWindowBorder(dpy, c->win, scheme[SchemeNorm][ColBorder].pixel);
  2127. if (setfocus) {
  2128. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  2129. XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
  2130. }
  2131. }
  2132. void
  2133. unmanage(Client *c, int destroyed)
  2134. {
  2135. Monitor *m = c->mon;
  2136. XWindowChanges wc;
  2137. if (c->swallowing) {
  2138. unswallow(c);
  2139. return;
  2140. }
  2141. Client *s = swallowingclient(c->win);
  2142. if (s) {
  2143. free(s->swallowing);
  2144. s->swallowing = NULL;
  2145. arrange(m);
  2146. focus(NULL);
  2147. return;
  2148. }
  2149. detach(c);
  2150. detachstack(c);
  2151. if (!destroyed) {
  2152. wc.border_width = c->oldbw;
  2153. XGrabServer(dpy); /* avoid race conditions */
  2154. XSetErrorHandler(xerrordummy);
  2155. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  2156. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  2157. setclientstate(c, WithdrawnState);
  2158. XSync(dpy, False);
  2159. XSetErrorHandler(xerror);
  2160. XUngrabServer(dpy);
  2161. }
  2162. free(c);
  2163. if (!s) {
  2164. arrange(m);
  2165. focus(NULL);
  2166. updateclientlist();
  2167. }
  2168. }
  2169. void
  2170. unmapnotify(XEvent *e)
  2171. {
  2172. Client *c;
  2173. XUnmapEvent *ev = &e->xunmap;
  2174. if ((c = wintoclient(ev->window))) {
  2175. if (ev->send_event)
  2176. setclientstate(c, WithdrawnState);
  2177. else
  2178. unmanage(c, 0);
  2179. }
  2180. else if ((c = wintosystrayicon(ev->window))) {
  2181. /* KLUDGE! sometimes icons occasionally unmap their windows, but do
  2182. * _not_ destroy them. We map those windows back */
  2183. XMapRaised(dpy, c->win);
  2184. updatesystray();
  2185. }
  2186. }
  2187. void
  2188. updatebars(void)
  2189. {
  2190. unsigned int w;
  2191. Monitor *m;
  2192. XSetWindowAttributes wa = {
  2193. .override_redirect = True,
  2194. .background_pixmap = ParentRelative,
  2195. .event_mask = ButtonPressMask|ExposureMask
  2196. };
  2197. XClassHint ch = {"dwm", "dwm"};
  2198. for (m = mons; m; m = m->next) {
  2199. if (m->barwin)
  2200. continue;
  2201. w = m->ww;
  2202. if (showsystray && m == systraytomon(m))
  2203. w -= getsystraywidth();
  2204. m->barwin = XCreateWindow(dpy, root, m->wx, m->by, w, bh, 0, DefaultDepth(dpy, screen),
  2205. CopyFromParent, DefaultVisual(dpy, screen),
  2206. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  2207. XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
  2208. if (showsystray && m == systraytomon(m))
  2209. XMapRaised(dpy, systray->win);
  2210. XMapRaised(dpy, m->barwin);
  2211. XSetClassHint(dpy, m->barwin, &ch);
  2212. }
  2213. }
  2214. void
  2215. updatebarpos(Monitor *m)
  2216. {
  2217. m->wy = m->my;
  2218. m->wh = m->mh;
  2219. if (m->showbar) {
  2220. m->wh -= bh;
  2221. m->by = m->topbar ? m->wy : m->wy + m->wh;
  2222. m->wy = m->topbar ? m->wy + bh : m->wy;
  2223. } else
  2224. m->by = -bh;
  2225. }
  2226. void
  2227. updateclientlist()
  2228. {
  2229. Client *c;
  2230. Monitor *m;
  2231. XDeleteProperty(dpy, root, netatom[NetClientList]);
  2232. for (m = mons; m; m = m->next)
  2233. for (c = m->clients; c; c = c->next)
  2234. XChangeProperty(dpy, root, netatom[NetClientList],
  2235. XA_WINDOW, 32, PropModeAppend,
  2236. (unsigned char *) &(c->win), 1);
  2237. }
  2238. int
  2239. updategeom(void)
  2240. {
  2241. int dirty = 0;
  2242. #ifdef XINERAMA
  2243. if (XineramaIsActive(dpy)) {
  2244. int i, j, n, nn;
  2245. Client *c;
  2246. Monitor *m;
  2247. XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
  2248. XineramaScreenInfo *unique = NULL;
  2249. for (n = 0, m = mons; m; m = m->next, n++);
  2250. /* only consider unique geometries as separate screens */
  2251. unique = ecalloc(nn, sizeof(XineramaScreenInfo));
  2252. for (i = 0, j = 0; i < nn; i++)
  2253. if (isuniquegeom(unique, j, &info[i]))
  2254. memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
  2255. XFree(info);
  2256. nn = j;
  2257. if (n <= nn) { /* new monitors available */
  2258. for (i = 0; i < (nn - n); i++) {
  2259. for (m = mons; m && m->next; m = m->next);
  2260. if (m)
  2261. m->next = createmon();
  2262. else
  2263. mons = createmon();
  2264. }
  2265. for (i = 0, m = mons; i < nn && m; m = m->next, i++)
  2266. if (i >= n
  2267. || unique[i].x_org != m->mx || unique[i].y_org != m->my
  2268. || unique[i].width != m->mw || unique[i].height != m->mh)
  2269. {
  2270. dirty = 1;
  2271. m->num = i;
  2272. m->mx = m->wx = unique[i].x_org;
  2273. m->my = m->wy = unique[i].y_org;
  2274. m->mw = m->ww = unique[i].width;
  2275. m->mh = m->wh = unique[i].height;
  2276. updatebarpos(m);
  2277. }
  2278. } else { /* less monitors available nn < n */
  2279. for (i = nn; i < n; i++) {
  2280. for (m = mons; m && m->next; m = m->next);
  2281. while ((c = m->clients)) {
  2282. dirty = 1;
  2283. m->clients = c->next;
  2284. detachstack(c);
  2285. c->mon = mons;
  2286. attach(c);
  2287. attachstack(c);
  2288. }
  2289. if (m == selmon)
  2290. selmon = mons;
  2291. cleanupmon(m);
  2292. }
  2293. }
  2294. free(unique);
  2295. } else
  2296. #endif /* XINERAMA */
  2297. { /* default monitor setup */
  2298. if (!mons)
  2299. mons = createmon();
  2300. if (mons->mw != sw || mons->mh != sh) {
  2301. dirty = 1;
  2302. mons->mw = mons->ww = sw;
  2303. mons->mh = mons->wh = sh;
  2304. updatebarpos(mons);
  2305. }
  2306. }
  2307. if (dirty) {
  2308. selmon = mons;
  2309. selmon = wintomon(root);
  2310. }
  2311. return dirty;
  2312. }
  2313. void
  2314. updatenumlockmask(void)
  2315. {
  2316. unsigned int i, j;
  2317. XModifierKeymap *modmap;
  2318. numlockmask = 0;
  2319. modmap = XGetModifierMapping(dpy);
  2320. for (i = 0; i < 8; i++)
  2321. for (j = 0; j < modmap->max_keypermod; j++)
  2322. if (modmap->modifiermap[i * modmap->max_keypermod + j]
  2323. == XKeysymToKeycode(dpy, XK_Num_Lock))
  2324. numlockmask = (1 << i);
  2325. XFreeModifiermap(modmap);
  2326. }
  2327. void
  2328. updatesizehints(Client *c)
  2329. {
  2330. long msize;
  2331. XSizeHints size;
  2332. if (!XGetWMNormalHints(dpy, c->win, &size, &msize))
  2333. /* size is uninitialized, ensure that size.flags aren't used */
  2334. size.flags = PSize;
  2335. if (size.flags & PBaseSize) {
  2336. c->basew = size.base_width;
  2337. c->baseh = size.base_height;
  2338. } else if (size.flags & PMinSize) {
  2339. c->basew = size.min_width;
  2340. c->baseh = size.min_height;
  2341. } else
  2342. c->basew = c->baseh = 0;
  2343. if (size.flags & PResizeInc) {
  2344. c->incw = size.width_inc;
  2345. c->inch = size.height_inc;
  2346. } else
  2347. c->incw = c->inch = 0;
  2348. if (size.flags & PMaxSize) {
  2349. c->maxw = size.max_width;
  2350. c->maxh = size.max_height;
  2351. } else
  2352. c->maxw = c->maxh = 0;
  2353. if (size.flags & PMinSize) {
  2354. c->minw = size.min_width;
  2355. c->minh = size.min_height;
  2356. } else if (size.flags & PBaseSize) {
  2357. c->minw = size.base_width;
  2358. c->minh = size.base_height;
  2359. } else
  2360. c->minw = c->minh = 0;
  2361. if (size.flags & PAspect) {
  2362. c->mina = (float)size.min_aspect.y / size.min_aspect.x;
  2363. c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
  2364. } else
  2365. c->maxa = c->mina = 0.0;
  2366. c->isfixed = (c->maxw && c->maxh && c->maxw == c->minw && c->maxh == c->minh);
  2367. }
  2368. void
  2369. updatestatus(void)
  2370. {
  2371. if (!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
  2372. strcpy(stext, "dwm-"VERSION);
  2373. drawbar(selmon);
  2374. updatesystray();
  2375. }
  2376. void
  2377. updatesystrayicongeom(Client *i, int w, int h)
  2378. {
  2379. if (i) {
  2380. i->h = bh;
  2381. if (w == h)
  2382. i->w = bh;
  2383. else if (h == bh)
  2384. i->w = w;
  2385. else
  2386. i->w = (int) ((float)bh * ((float)w / (float)h));
  2387. applysizehints(i, &(i->x), &(i->y), &(i->w), &(i->h), False);
  2388. /* force icons into the systray dimensions if they don't want to */
  2389. if (i->h > bh) {
  2390. if (i->w == i->h)
  2391. i->w = bh;
  2392. else
  2393. i->w = (int) ((float)bh * ((float)i->w / (float)i->h));
  2394. i->h = bh;
  2395. }
  2396. }
  2397. }
  2398. void
  2399. updatesystrayiconstate(Client *i, XPropertyEvent *ev)
  2400. {
  2401. long flags;
  2402. int code = 0;
  2403. if (!showsystray || !i || ev->atom != xatom[XembedInfo] ||
  2404. !(flags = getatomprop(i, xatom[XembedInfo])))
  2405. return;
  2406. if (flags & XEMBED_MAPPED && !i->tags) {
  2407. i->tags = 1;
  2408. code = XEMBED_WINDOW_ACTIVATE;
  2409. XMapRaised(dpy, i->win);
  2410. setclientstate(i, NormalState);
  2411. }
  2412. else if (!(flags & XEMBED_MAPPED) && i->tags) {
  2413. i->tags = 0;
  2414. code = XEMBED_WINDOW_DEACTIVATE;
  2415. XUnmapWindow(dpy, i->win);
  2416. setclientstate(i, WithdrawnState);
  2417. }
  2418. else
  2419. return;
  2420. sendevent(i->win, xatom[Xembed], StructureNotifyMask, CurrentTime, code, 0,
  2421. systray->win, XEMBED_EMBEDDED_VERSION);
  2422. }
  2423. void
  2424. updatesystray(void)
  2425. {
  2426. XSetWindowAttributes wa;
  2427. XWindowChanges wc;
  2428. Client *i;
  2429. Monitor *m = systraytomon(NULL);
  2430. unsigned int x = m->mx + m->mw;
  2431. unsigned int w = 1;
  2432. if (!showsystray)
  2433. return;
  2434. if (!systray) {
  2435. /* init systray */
  2436. if (!(systray = (Systray *)calloc(1, sizeof(Systray))))
  2437. die("fatal: could not malloc() %u bytes\n", sizeof(Systray));
  2438. systray->win = XCreateSimpleWindow(dpy, root, x, m->by, w, bh, 0, 0, scheme[SchemeSel][ColBg].pixel);
  2439. wa.event_mask = ButtonPressMask | ExposureMask;
  2440. wa.override_redirect = True;
  2441. wa.background_pixel = scheme[SchemeNorm][ColBg].pixel;
  2442. XSelectInput(dpy, systray->win, SubstructureNotifyMask);
  2443. XChangeProperty(dpy, systray->win, netatom[NetSystemTrayOrientation], XA_CARDINAL, 32,
  2444. PropModeReplace, (unsigned char *)&netatom[NetSystemTrayOrientationHorz], 1);
  2445. XChangeWindowAttributes(dpy, systray->win, CWEventMask|CWOverrideRedirect|CWBackPixel, &wa);
  2446. XMapRaised(dpy, systray->win);
  2447. XSetSelectionOwner(dpy, netatom[NetSystemTray], systray->win, CurrentTime);
  2448. if (XGetSelectionOwner(dpy, netatom[NetSystemTray]) == systray->win) {
  2449. sendevent(root, xatom[Manager], StructureNotifyMask, CurrentTime, netatom[NetSystemTray], systray->win, 0, 0);
  2450. XSync(dpy, False);
  2451. }
  2452. else {
  2453. fprintf(stderr, "dwm: unable to obtain system tray.\n");
  2454. free(systray);
  2455. systray = NULL;
  2456. return;
  2457. }
  2458. }
  2459. for (w = 0, i = systray->icons; i; i = i->next) {
  2460. /* make sure the background color stays the same */
  2461. wa.background_pixel = scheme[SchemeNorm][ColBg].pixel;
  2462. XChangeWindowAttributes(dpy, i->win, CWBackPixel, &wa);
  2463. XMapRaised(dpy, i->win);
  2464. w += systrayspacing;
  2465. i->x = w;
  2466. XMoveResizeWindow(dpy, i->win, i->x, 0, i->w, i->h);
  2467. w += i->w;
  2468. if (i->mon != m)
  2469. i->mon = m;
  2470. }
  2471. w = w ? w + systrayspacing : 1;
  2472. x -= w;
  2473. XMoveResizeWindow(dpy, systray->win, x, m->by, w, bh);
  2474. wc.x = x; wc.y = m->by; wc.width = w; wc.height = bh;
  2475. wc.stack_mode = Above; wc.sibling = m->barwin;
  2476. XConfigureWindow(dpy, systray->win, CWX|CWY|CWWidth|CWHeight|CWSibling|CWStackMode, &wc);
  2477. XMapWindow(dpy, systray->win);
  2478. XMapSubwindows(dpy, systray->win);
  2479. /* redraw background */
  2480. XSetForeground(dpy, drw->gc, scheme[SchemeNorm][ColBg].pixel);
  2481. XFillRectangle(dpy, systray->win, drw->gc, 0, 0, w, bh);
  2482. XSync(dpy, False);
  2483. }
  2484. void
  2485. updatetitle(Client *c)
  2486. {
  2487. if (!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  2488. gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
  2489. if (c->name[0] == '\0') /* hack to mark broken clients */
  2490. strcpy(c->name, broken);
  2491. }
  2492. void
  2493. updatewindowtype(Client *c)
  2494. {
  2495. Atom state = getatomprop(c, netatom[NetWMState]);
  2496. Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
  2497. if (state == netatom[NetWMFullscreen])
  2498. setfullscreen(c, 1);
  2499. if (wtype == netatom[NetWMWindowTypeDialog])
  2500. c->isfloating = 1;
  2501. }
  2502. void
  2503. updatewmhints(Client *c)
  2504. {
  2505. XWMHints *wmh;
  2506. if ((wmh = XGetWMHints(dpy, c->win))) {
  2507. if (c == selmon->sel && wmh->flags & XUrgencyHint) {
  2508. wmh->flags &= ~XUrgencyHint;
  2509. XSetWMHints(dpy, c->win, wmh);
  2510. } else
  2511. c->isurgent = (wmh->flags & XUrgencyHint) ? 1 : 0;
  2512. if (wmh->flags & InputHint)
  2513. c->neverfocus = !wmh->input;
  2514. else
  2515. c->neverfocus = 0;
  2516. XFree(wmh);
  2517. }
  2518. }
  2519. void
  2520. view(const Arg *arg)
  2521. {
  2522. if ((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
  2523. return;
  2524. selmon->seltags ^= 1; /* toggle sel tagset */
  2525. if (arg->ui & TAGMASK)
  2526. selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
  2527. focus(NULL);
  2528. arrange(selmon);
  2529. }
  2530. pid_t
  2531. winpid(Window w)
  2532. {
  2533. pid_t result = 0;
  2534. xcb_res_client_id_spec_t spec = {0};
  2535. spec.client = w;
  2536. spec.mask = XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID;
  2537. xcb_generic_error_t *e = NULL;
  2538. xcb_res_query_client_ids_cookie_t c = xcb_res_query_client_ids(xcon, 1, &spec);
  2539. xcb_res_query_client_ids_reply_t *r = xcb_res_query_client_ids_reply(xcon, c, &e);
  2540. if (!r)
  2541. return (pid_t)0;
  2542. xcb_res_client_id_value_iterator_t i = xcb_res_query_client_ids_ids_iterator(r);
  2543. for (; i.rem; xcb_res_client_id_value_next(&i)) {
  2544. spec = i.data->spec;
  2545. if (spec.mask & XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID) {
  2546. uint32_t *t = xcb_res_client_id_value_value(i.data);
  2547. result = *t;
  2548. break;
  2549. }
  2550. }
  2551. free(r);
  2552. if (result == (pid_t)-1)
  2553. result = 0;
  2554. return result;
  2555. }
  2556. pid_t
  2557. getparentprocess(pid_t p)
  2558. {
  2559. unsigned int v = 0;
  2560. #if defined(__linux__)
  2561. FILE *f;
  2562. char buf[256];
  2563. snprintf(buf, sizeof(buf) - 1, "/proc/%u/stat", (unsigned)p);
  2564. if (!(f = fopen(buf, "r")))
  2565. return (pid_t)0;
  2566. if (fscanf(f, "%*u %*s %*c %u", (unsigned *)&v) != 1)
  2567. v = (pid_t)0;
  2568. fclose(f);
  2569. #elif defined(__FreeBSD__)
  2570. struct kinfo_proc *proc = kinfo_getproc(p);
  2571. if (!proc)
  2572. return (pid_t)0;
  2573. v = proc->ki_ppid;
  2574. free(proc);
  2575. #endif
  2576. return (pid_t)v;
  2577. }
  2578. int
  2579. isdescprocess(pid_t p, pid_t c)
  2580. {
  2581. while (p != c && c != 0)
  2582. c = getparentprocess(c);
  2583. return (int)c;
  2584. }
  2585. Client *
  2586. termforwin(const Client *w)
  2587. {
  2588. Client *c;
  2589. Monitor *m;
  2590. if (!w->pid || w->isterminal)
  2591. return NULL;
  2592. for (m = mons; m; m = m->next) {
  2593. for (c = m->clients; c; c = c->next) {
  2594. if (c->isterminal && !c->swallowing && c->pid && isdescprocess(c->pid, w->pid))
  2595. return c;
  2596. }
  2597. }
  2598. return NULL;
  2599. }
  2600. Client *
  2601. swallowingclient(Window w)
  2602. {
  2603. Client *c;
  2604. Monitor *m;
  2605. for (m = mons; m; m = m->next) {
  2606. for (c = m->clients; c; c = c->next) {
  2607. if (c->swallowing && c->swallowing->win == w)
  2608. return c;
  2609. }
  2610. }
  2611. return NULL;
  2612. }
  2613. Client *
  2614. wintoclient(Window w)
  2615. {
  2616. Client *c;
  2617. Monitor *m;
  2618. for (m = mons; m; m = m->next)
  2619. for (c = m->clients; c; c = c->next)
  2620. if (c->win == w)
  2621. return c;
  2622. return NULL;
  2623. }
  2624. Client *
  2625. wintosystrayicon(Window w) {
  2626. Client *i = NULL;
  2627. if (!showsystray || !w)
  2628. return i;
  2629. for (i = systray->icons; i && i->win != w; i = i->next) ;
  2630. return i;
  2631. }
  2632. Monitor *
  2633. wintomon(Window w)
  2634. {
  2635. int x, y;
  2636. Client *c;
  2637. Monitor *m;
  2638. if (w == root && getrootptr(&x, &y))
  2639. return recttomon(x, y, 1, 1);
  2640. for (m = mons; m; m = m->next)
  2641. if (w == m->barwin)
  2642. return m;
  2643. if ((c = wintoclient(w)))
  2644. return c->mon;
  2645. return selmon;
  2646. }
  2647. /* There's no way to check accesses to destroyed windows, thus those cases are
  2648. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  2649. * default error handler, which may call exit. */
  2650. int
  2651. xerror(Display *dpy, XErrorEvent *ee)
  2652. {
  2653. if (ee->error_code == BadWindow
  2654. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  2655. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  2656. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  2657. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  2658. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  2659. || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
  2660. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  2661. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  2662. return 0;
  2663. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  2664. ee->request_code, ee->error_code);
  2665. return xerrorxlib(dpy, ee); /* may call exit */
  2666. }
  2667. int
  2668. xerrordummy(Display *dpy, XErrorEvent *ee)
  2669. {
  2670. return 0;
  2671. }
  2672. /* Startup Error handler to check if another window manager
  2673. * is already running. */
  2674. int
  2675. xerrorstart(Display *dpy, XErrorEvent *ee)
  2676. {
  2677. die("dwm: another window manager is already running");
  2678. return -1;
  2679. }
  2680. Monitor *
  2681. systraytomon(Monitor *m) {
  2682. Monitor *t;
  2683. int i, n;
  2684. if(!systraypinning) {
  2685. if(!m)
  2686. return selmon;
  2687. return m == selmon ? m : NULL;
  2688. }
  2689. for(n = 1, t = mons; t && t->next; n++, t = t->next) ;
  2690. for(i = 1, t = mons; t && t->next && i < systraypinning; i++, t = t->next) ;
  2691. if(systraypinningfailfirst && n < systraypinning)
  2692. return mons;
  2693. return t;
  2694. }
  2695. void
  2696. xrdb(const Arg *arg)
  2697. {
  2698. loadxrdb();
  2699. int i;
  2700. for (i = 0; i < LENGTH(colors); i++)
  2701. scheme[i] = drw_scm_create(drw, colors[i], 3);
  2702. focus(NULL);
  2703. arrange(NULL);
  2704. }
  2705. void
  2706. zoom(const Arg *arg)
  2707. {
  2708. Client *c = selmon->sel;
  2709. if (!selmon->lt[selmon->sellt]->arrange
  2710. || (selmon->sel && selmon->sel->isfloating))
  2711. return;
  2712. if (c == nexttiled(selmon->clients))
  2713. if (!c || !(c = nexttiled(c->next)))
  2714. return;
  2715. pop(c);
  2716. }
  2717. int
  2718. main(int argc, char *argv[])
  2719. {
  2720. if (argc == 2 && !strcmp("-v", argv[1]))
  2721. die("dwm-"VERSION);
  2722. else if (argc != 1)
  2723. die("usage: dwm [-v]");
  2724. if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  2725. fputs("warning: no locale support\n", stderr);
  2726. if (!(dpy = XOpenDisplay(NULL)))
  2727. die("dwm: cannot open display");
  2728. if (!(xcon = XGetXCBConnection(dpy)))
  2729. die("dwm: cannot get xcb connection\n");
  2730. checkotherwm();
  2731. XrmInitialize();
  2732. loadxrdb();
  2733. setup();
  2734. #ifdef __OpenBSD__
  2735. if (pledge("stdio rpath proc exec", NULL) == -1)
  2736. die("pledge");
  2737. #endif /* __OpenBSD__ */
  2738. scan();
  2739. run();
  2740. cleanup();
  2741. XCloseDisplay(dpy);
  2742. return EXIT_SUCCESS;
  2743. }
  2744. void
  2745. centeredmaster(Monitor *m)
  2746. {
  2747. unsigned int i, n, h, mw, mx, my, oty, ety, tw;
  2748. Client *c;
  2749. /* count number of clients in the selected monitor */
  2750. for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
  2751. if (n == 0)
  2752. return;
  2753. /* initialize areas */
  2754. mw = m->ww;
  2755. mx = 0;
  2756. my = 0;
  2757. tw = mw;
  2758. if (n > m->nmaster) {
  2759. /* go mfact box in the center if more than nmaster clients */
  2760. mw = m->nmaster ? m->ww * m->mfact : 0;
  2761. tw = m->ww - mw;
  2762. if (n - m->nmaster > 1) {
  2763. /* only one client */
  2764. mx = (m->ww - mw) / 2;
  2765. tw = (m->ww - mw) / 2;
  2766. }
  2767. }
  2768. oty = 0;
  2769. ety = 0;
  2770. for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
  2771. if (i < m->nmaster) {
  2772. /* nmaster clients are stacked vertically, in the center
  2773. * of the screen */
  2774. h = (m->wh - my) / (MIN(n, m->nmaster) - i);
  2775. resize(c, m->wx + mx, m->wy + my, mw - (2*c->bw),
  2776. h - (2*c->bw), 0);
  2777. my += HEIGHT(c);
  2778. } else {
  2779. /* stack clients are stacked vertically */
  2780. if ((i - m->nmaster) % 2 ) {
  2781. h = (m->wh - ety) / ( (1 + n - i) / 2);
  2782. resize(c, m->wx, m->wy + ety, tw - (2*c->bw),
  2783. h - (2*c->bw), 0);
  2784. ety += HEIGHT(c);
  2785. } else {
  2786. h = (m->wh - oty) / ((1 + n - i) / 2);
  2787. resize(c, m->wx + mx + mw, m->wy + oty,
  2788. tw - (2*c->bw), h - (2*c->bw), 0);
  2789. oty += HEIGHT(c);
  2790. }
  2791. }
  2792. }
  2793. void
  2794. centeredfloatingmaster(Monitor *m)
  2795. {
  2796. unsigned int i, n, w, mh, mw, mx, mxo, my, myo, tx;
  2797. Client *c;
  2798. /* count number of clients in the selected monitor */
  2799. for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
  2800. if (n == 0)
  2801. return;
  2802. /* initialize nmaster area */
  2803. if (n > m->nmaster) {
  2804. /* go mfact box in the center if more than nmaster clients */
  2805. if (m->ww > m->wh) {
  2806. mw = m->nmaster ? m->ww * m->mfact : 0;
  2807. mh = m->nmaster ? m->wh * 0.9 : 0;
  2808. } else {
  2809. mh = m->nmaster ? m->wh * m->mfact : 0;
  2810. mw = m->nmaster ? m->ww * 0.9 : 0;
  2811. }
  2812. mx = mxo = (m->ww - mw) / 2;
  2813. my = myo = (m->wh - mh) / 2;
  2814. } else {
  2815. /* go fullscreen if all clients are in the master area */
  2816. mh = m->wh;
  2817. mw = m->ww;
  2818. mx = mxo = 0;
  2819. my = myo = 0;
  2820. }
  2821. for(i = tx = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
  2822. if (i < m->nmaster) {
  2823. /* nmaster clients are stacked horizontally, in the center
  2824. * of the screen */
  2825. w = (mw + mxo - mx) / (MIN(n, m->nmaster) - i);
  2826. resize(c, m->wx + mx, m->wy + my, w - (2*c->bw),
  2827. mh - (2*c->bw), 0);
  2828. mx += WIDTH(c);
  2829. } else {
  2830. /* stack clients are stacked horizontally */
  2831. w = (m->ww - tx) / (n - i);
  2832. resize(c, m->wx + tx, m->wy, w - (2*c->bw),
  2833. m->wh - (2*c->bw), 0);
  2834. tx += WIDTH(c);
  2835. }
  2836. }