dwm.c 75 KB

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