dwm.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353
  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 <stdarg.h>
  26. #include <signal.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. /* macros */
  43. #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
  44. #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask))
  45. #define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH))
  46. #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
  47. #define ISFOCUSABLE(C) (ISVISIBLE(C) && !C->isbg)
  48. #define LENGTH(X) (sizeof X / sizeof X[0])
  49. #define MAX(A, B) ((A) > (B) ? (A) : (B))
  50. #define MIN(A, B) ((A) < (B) ? (A) : (B))
  51. #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
  52. #define WIDTH(X) ((X)->w + 2 * (X)->bw)
  53. #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
  54. #define TAGMASK ((1 << LENGTH(tags)) - 1)
  55. #define TEXTW(X) (textnw(X, strlen(X)) + dc.font.height)
  56. /*For Systray*/
  57. #define XEMBED_EMBEDDED_NOTIFY 0
  58. #define IFREE(x) if(x) free(x)
  59. #define zcalloc(size) calloc(1, (size))
  60. /* enums */
  61. enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
  62. enum { ColBorder, ColFG, ColBG, ColLast }; /* color */
  63. enum { NetSupported, NetWMName, NetWMState,
  64. NetWMFullscreen, NetSystemTray, KdeWMSysTrayWindow, NetLast }; /* EWMH atoms */
  65. enum { WMProtocols, WMDelete, WMState, WMLast }; /* default atoms */
  66. enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
  67. ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
  68. typedef union {
  69. int i;
  70. unsigned int ui;
  71. float f;
  72. const void *v;
  73. } Arg;
  74. typedef struct {
  75. unsigned int click;
  76. unsigned int mask;
  77. unsigned int button;
  78. void (*func)(const Arg *arg);
  79. const Arg arg;
  80. } Button;
  81. typedef struct Monitor Monitor;
  82. typedef struct Client Client;
  83. struct Client {
  84. char name[256];
  85. float mina, maxa;
  86. int x, y, w, h;
  87. int oldx, oldy, oldw, oldh;
  88. int basew, baseh, incw, inch, maxw, maxh, minw, minh;
  89. int bw, oldbw;
  90. unsigned int tags;
  91. Bool isfixed, isfloating, isbg, isurgent, oldstate;
  92. Client *next;
  93. Client *snext;
  94. Monitor *mon;
  95. Window win;
  96. };
  97. typedef struct {
  98. int x, y, w, h;
  99. unsigned long norm[ColLast];
  100. unsigned long sel[ColLast];
  101. Drawable drawable;
  102. GC gc;
  103. struct {
  104. int ascent;
  105. int descent;
  106. int height;
  107. XFontSet set;
  108. XFontStruct *xfont;
  109. } font;
  110. } DC; /* draw context */
  111. typedef struct {
  112. unsigned int mod;
  113. KeySym keysym;
  114. void (*func)(const Arg *);
  115. const Arg arg;
  116. } Key;
  117. typedef struct {
  118. const char *symbol;
  119. void (*arrange)(Monitor *);
  120. } Layout;
  121. struct Monitor {
  122. char ltsymbol[16];
  123. float mfact;
  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. unsigned int seltags;
  129. unsigned int sellt;
  130. unsigned int tagset[2];
  131. Bool showbar;
  132. Bool topbar;
  133. Client *clients;
  134. Client *sel;
  135. Client *stack;
  136. Monitor *next;
  137. Window barwin;
  138. const Layout *lt[2];
  139. };
  140. typedef struct {
  141. const char *class;
  142. const char *instance;
  143. const char *title;
  144. unsigned int tags;
  145. Bool isfloating;
  146. Bool isbg;
  147. int monitor;
  148. } Rule;
  149. /*For Systray*/
  150. typedef struct Systray Systray;
  151. struct Systray {
  152. Window win;
  153. XRectangle geo;
  154. Systray *next, *prev;
  155. };
  156. /* function declarations */
  157. static void applyrules(Client *c);
  158. static Bool applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact);
  159. static void arrange(Monitor *m);
  160. static void arrangemon(Monitor *m);
  161. static void attach(Client *c);
  162. static void attachstack(Client *c);
  163. static void buttonpress(XEvent *e);
  164. static void checkotherwm(void);
  165. static void cleanup(void);
  166. static void cleanupmon(Monitor *mon);
  167. static void clearurgent(Client *c);
  168. static void clientmessage(XEvent *e);
  169. static void configure(Client *c);
  170. static void configurenotify(XEvent *e);
  171. static void configurerequest(XEvent *e);
  172. static Monitor *createmon(void);
  173. static void destroynotify(XEvent *e);
  174. static void detach(Client *c);
  175. static void detachstack(Client *c);
  176. static void die(const char *errstr, ...);
  177. static Monitor *dirtomon(int dir);
  178. static void drawbar(Monitor *m);
  179. static void drawbars(void);
  180. static void drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]);
  181. static void drawtext(const char *text, unsigned long col[ColLast], Bool invert);
  182. static void enternotify(XEvent *e);
  183. static void expose(XEvent *e);
  184. static void focus(Client *c);
  185. static void focusin(XEvent *e);
  186. static void focusmon(const Arg *arg);
  187. static void focusstack(const Arg *arg);
  188. static void paddingAdd(const Arg *arg);
  189. static unsigned long getcolor(const char *colstr);
  190. static Bool getrootptr(int *x, int *y);
  191. static long getstate(Window w);
  192. static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
  193. static void grabbuttons(Client *c, Bool focused);
  194. static void grabkeys(void);
  195. static void initfont(const char *fontstr);
  196. static Bool isprotodel(Client *c);
  197. static void keypress(XEvent *e);
  198. static void killclient(const Arg *arg);
  199. static void manage(Window w, XWindowAttributes *wa);
  200. static void mappingnotify(XEvent *e);
  201. static void maprequest(XEvent *e);
  202. static void monocle(Monitor *m);
  203. static void movemouse(const Arg *arg);
  204. static Client *nexttiled(Client *c);
  205. static Monitor *ptrtomon(int x, int y);
  206. static void propertynotify(XEvent *e);
  207. static void quit(const Arg *arg);
  208. static void resize(Client *c, int x, int y, int w, int h, Bool interact);
  209. static void resizeclient(Client *c, int x, int y, int w, int h);
  210. static void resizemouse(const Arg *arg);
  211. static void restack(Monitor *m);
  212. static void run(void);
  213. static void scan(void);
  214. static void sendmon(Client *c, Monitor *m);
  215. static void setclientstate(Client *c, long state);
  216. static void setlayout(const Arg *arg);
  217. static void setmfact(const Arg *arg);
  218. static void setup(void);
  219. static void showhide(Client *c);
  220. static void sigchld(int unused);
  221. static void spawn(const Arg *arg);
  222. static void tag(const Arg *arg);
  223. static void tagmon(const Arg *arg);
  224. static int textnw(const char *text, unsigned int len);
  225. static void tile(Monitor *);
  226. static void togglebar(const Arg *arg);
  227. static void togglebg(const Arg *arg);
  228. static void togglefloating(const Arg *arg);
  229. static void toggletag(const Arg *arg);
  230. static void toggleview(const Arg *arg);
  231. static void unfocus(Client *c, Bool setfocus);
  232. static void unmanage(Client *c, Bool destroyed);
  233. static void unmapnotify(XEvent *e);
  234. static Bool updategeom(void);
  235. static void updatebarpos(Monitor *m);
  236. static void updatebars(void);
  237. static void updatenumlockmask(void);
  238. static void updatesizehints(Client *c);
  239. static void updatestatus(void);
  240. static void updatetitle(Client *c);
  241. static void updatewmhints(Client *c);
  242. static void view(const Arg *arg);
  243. static Client *wintoclient(Window w);
  244. static Monitor *wintomon(Window w);
  245. static int xerror(Display *dpy, XErrorEvent *ee);
  246. static int xerrordummy(Display *dpy, XErrorEvent *ee);
  247. static int xerrorstart(Display *dpy, XErrorEvent *ee);
  248. static void zoom(const Arg *arg);
  249. /*For Systray*/
  250. static Bool systray_acquire(void);
  251. static void systray_add(Window win);
  252. static void systray_del(Systray *s);
  253. static void systray_freeicons(void);
  254. static Systray* systray_find(Window win);
  255. static void systray_update(void);
  256. /*static int systray_get_width(void);*/
  257. /* variables */
  258. static const char broken[] = "broken";
  259. static char stext[256];
  260. static int screen;
  261. static int sw, sh; /* X display screen geometry width, height */
  262. static int bh, blw = 0; /* bar geometry */
  263. static int (*xerrorxlib)(Display *, XErrorEvent *);
  264. static unsigned int numlockmask = 0;
  265. static void (*handler[LASTEvent]) (XEvent *) = {
  266. [ButtonPress] = buttonpress,
  267. [ClientMessage] = clientmessage,
  268. [ConfigureRequest] = configurerequest,
  269. [ConfigureNotify] = configurenotify,
  270. [DestroyNotify] = destroynotify,
  271. [EnterNotify] = enternotify,
  272. [Expose] = expose,
  273. [FocusIn] = focusin,
  274. [KeyPress] = keypress,
  275. [MappingNotify] = mappingnotify,
  276. [MapRequest] = maprequest,
  277. [PropertyNotify] = propertynotify,
  278. [UnmapNotify] = unmapnotify
  279. };
  280. static Atom wmatom[WMLast], netatom[NetLast];
  281. static Bool otherwm;
  282. static Bool running = True;
  283. static Cursor cursor[CurLast];
  284. static Display *dpy;
  285. static DC dc;
  286. static Monitor *mons = NULL, *selmon = NULL;
  287. static Window root;
  288. /*For systray*/
  289. static Systray *trayicons;
  290. static Window traywin;
  291. /* configuration, allows nested code to access above variables */
  292. #include "config.h"
  293. /* compile-time check if all tags fit into an unsigned int bit array. */
  294. struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
  295. /* function implementations */
  296. void
  297. applyrules(Client *c) {
  298. const char *class, *instance;
  299. unsigned int i;
  300. const Rule *r;
  301. Monitor *m;
  302. XClassHint ch = { 0 };
  303. /* rule matching */
  304. c->isfloating = c->tags = 0;
  305. if(XGetClassHint(dpy, c->win, &ch)) {
  306. class = ch.res_class ? ch.res_class : broken;
  307. instance = ch.res_name ? ch.res_name : broken;
  308. for(i = 0; i < LENGTH(rules); i++) {
  309. r = &rules[i];
  310. if((!r->title || strstr(c->name, r->title))
  311. && (!r->class || strstr(class, r->class))
  312. && (!r->instance || strstr(instance, r->instance)))
  313. {
  314. c->isfloating = r->isfloating;
  315. c->isbg = r->isbg;
  316. c->tags |= r->tags;
  317. for(m = mons; m && m->num != r->monitor; m = m->next);
  318. if(m)
  319. c->mon = m;
  320. }
  321. }
  322. if(ch.res_class)
  323. XFree(ch.res_class);
  324. if(ch.res_name)
  325. XFree(ch.res_name);
  326. }
  327. c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
  328. }
  329. Bool
  330. applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact) {
  331. Bool baseismin;
  332. Monitor *m = c->mon;
  333. /* set minimum possible */
  334. *w = MAX(1, *w);
  335. *h = MAX(1, *h);
  336. if(interact) {
  337. if(*x > sw)
  338. *x = sw - WIDTH(c);
  339. if(*y > sh)
  340. *y = sh - HEIGHT(c);
  341. if(*x + *w + 2 * c->bw < 0)
  342. *x = 0;
  343. if(*y + *h + 2 * c->bw < 0)
  344. *y = 0;
  345. }
  346. else {
  347. if(*x > m->mx + m->mw)
  348. *x = m->mx + m->mw - WIDTH(c);
  349. if(*y > m->my + m->mh)
  350. *y = m->my + m->mh - HEIGHT(c);
  351. if(*x + *w + 2 * c->bw < m->mx)
  352. *x = m->mx;
  353. if(*y + *h + 2 * c->bw < m->my)
  354. *y = m->my;
  355. }
  356. if(*h < bh)
  357. *h = bh;
  358. if(*w < bh)
  359. *w = bh;
  360. if(resizehints || c->isfloating) {
  361. /* see last two sentences in ICCCM 4.1.2.3 */
  362. baseismin = c->basew == c->minw && c->baseh == c->minh;
  363. if(!baseismin) { /* temporarily remove base dimensions */
  364. *w -= c->basew;
  365. *h -= c->baseh;
  366. }
  367. /* adjust for aspect limits */
  368. if(c->mina > 0 && c->maxa > 0) {
  369. if(c->maxa < (float)*w / *h)
  370. *w = *h * c->maxa + 0.5;
  371. else if(c->mina < (float)*h / *w)
  372. *h = *w * c->mina + 0.5;
  373. }
  374. if(baseismin) { /* increment calculation requires this */
  375. *w -= c->basew;
  376. *h -= c->baseh;
  377. }
  378. /* adjust for increment value */
  379. if(c->incw)
  380. *w -= *w % c->incw;
  381. if(c->inch)
  382. *h -= *h % c->inch;
  383. /* restore base dimensions */
  384. *w += c->basew;
  385. *h += c->baseh;
  386. *w = MAX(*w, c->minw);
  387. *h = MAX(*h, c->minh);
  388. if(c->maxw)
  389. *w = MIN(*w, c->maxw);
  390. if(c->maxh)
  391. *h = MIN(*h, c->maxh);
  392. }
  393. return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
  394. }
  395. void
  396. arrange(Monitor *m) {
  397. if(m)
  398. showhide(m->stack);
  399. else for(m = mons; m; m = m->next)
  400. showhide(m->stack);
  401. focus(NULL);
  402. if(m)
  403. arrangemon(m);
  404. else for(m = mons; m; m = m->next)
  405. arrangemon(m);
  406. }
  407. void
  408. arrangemon(Monitor *m) {
  409. strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
  410. if(m->lt[m->sellt]->arrange)
  411. m->lt[m->sellt]->arrange(m);
  412. restack(m);
  413. }
  414. void
  415. attach(Client *c) {
  416. c->next = c->mon->clients;
  417. c->mon->clients = c;
  418. }
  419. void
  420. attachstack(Client *c) {
  421. c->snext = c->mon->stack;
  422. c->mon->stack = c;
  423. }
  424. void
  425. buttonpress(XEvent *e) {
  426. unsigned int i, x, click;
  427. Arg arg = {0};
  428. Client *c;
  429. Monitor *m;
  430. XButtonPressedEvent *ev = &e->xbutton;
  431. click = ClkRootWin;
  432. if(ev->window == selmon->barwin) {
  433. /* focus monitor if necessary */
  434. if((m = wintomon(ev->window)) && m != selmon) {
  435. unfocus(selmon->sel, True);
  436. selmon = m;
  437. focus(NULL);
  438. }
  439. i = x = 0;
  440. do {
  441. x += TEXTW(tags[i]);
  442. } while(ev->x >= x && ++i < LENGTH(tags));
  443. if(i < LENGTH(tags)) {
  444. click = ClkTagBar;
  445. arg.ui = 1 << i;
  446. }
  447. else if(ev->x < x + blw)
  448. click = ClkLtSymbol;
  449. else if(ev->x > selmon->wx + selmon->ww - TEXTW(stext))
  450. click = ClkStatusText;
  451. else
  452. click = ClkWinTitle;
  453. }
  454. else if((c = wintoclient(ev->window))) {
  455. if(c == NULL || !c->isbg) {
  456. /* focus monitor if necessary */
  457. if((m = wintomon(ev->window)) && m != selmon) {
  458. unfocus(selmon->sel, True);
  459. selmon = m;
  460. focus(NULL);
  461. }
  462. focus(c);
  463. click = ClkClientWin;
  464. }
  465. }
  466. for(i = 0; i < LENGTH(buttons); i++)
  467. if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
  468. && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
  469. buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
  470. }
  471. void
  472. checkotherwm(void) {
  473. otherwm = False;
  474. xerrorxlib = XSetErrorHandler(xerrorstart);
  475. /* this causes an error if some other window manager is running */
  476. XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
  477. XSync(dpy, False);
  478. if(otherwm)
  479. die("dwm: another window manager is already running\n");
  480. XSetErrorHandler(xerror);
  481. XSync(dpy, False);
  482. }
  483. void
  484. cleanup(void) {
  485. Arg a = {.ui = ~0};
  486. Layout foo = { "", NULL };
  487. Monitor *m;
  488. view(&a);
  489. selmon->lt[selmon->sellt] = &foo;
  490. for(m = mons; m; m = m->next)
  491. while(m->stack)
  492. unmanage(m->stack, False);
  493. if(dc.font.set)
  494. XFreeFontSet(dpy, dc.font.set);
  495. else
  496. XFreeFont(dpy, dc.font.xfont);
  497. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  498. XFreePixmap(dpy, dc.drawable);
  499. XFreeGC(dpy, dc.gc);
  500. XFreeCursor(dpy, cursor[CurNormal]);
  501. XFreeCursor(dpy, cursor[CurResize]);
  502. XFreeCursor(dpy, cursor[CurMove]);
  503. while(mons)
  504. cleanupmon(mons);
  505. XSync(dpy, False);
  506. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  507. }
  508. void
  509. cleanupmon(Monitor *mon) {
  510. Monitor *m;
  511. if(mon == mons)
  512. mons = mons->next;
  513. else {
  514. for(m = mons; m && m->next != mon; m = m->next);
  515. m->next = mon->next;
  516. }
  517. XUnmapWindow(dpy, mon->barwin);
  518. XDestroyWindow(dpy, mon->barwin);
  519. free(mon);
  520. }
  521. void
  522. clearurgent(Client *c) {
  523. XWMHints *wmh;
  524. c->isurgent = False;
  525. if(!(wmh = XGetWMHints(dpy, c->win)))
  526. return;
  527. wmh->flags &= ~XUrgencyHint;
  528. XSetWMHints(dpy, c->win, wmh);
  529. XFree(wmh);
  530. }
  531. void
  532. configure(Client *c) {
  533. XConfigureEvent ce;
  534. ce.type = ConfigureNotify;
  535. ce.display = dpy;
  536. ce.event = c->win;
  537. ce.window = c->win;
  538. ce.x = c->x;
  539. ce.y = c->y;
  540. ce.width = c->w;
  541. ce.height = c->h;
  542. ce.border_width = c->bw;
  543. ce.above = None;
  544. ce.override_redirect = False;
  545. if (c->w > padding*4 && c->h > padding*4) {
  546. ce.x = c->x + padding;
  547. ce.y = c->y + padding;
  548. ce.width = c->w - padding*2;
  549. ce.height = c->h - padding*2;
  550. }
  551. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
  552. }
  553. void
  554. configurenotify(XEvent *e) {
  555. Monitor *m;
  556. XConfigureEvent *ev = &e->xconfigure;
  557. if(ev->window == root) {
  558. sw = ev->width;
  559. sh = ev->height;
  560. if(updategeom()) {
  561. if(dc.drawable != 0)
  562. XFreePixmap(dpy, dc.drawable);
  563. dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
  564. updatebars();
  565. for(m = mons; m; m = m->next)
  566. XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
  567. arrange(NULL);
  568. }
  569. }
  570. }
  571. void
  572. configurerequest(XEvent *e) {
  573. Client *c;
  574. Monitor *m;
  575. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  576. XWindowChanges wc;
  577. if((c = wintoclient(ev->window))) {
  578. if(ev->value_mask & CWBorderWidth)
  579. c->bw = ev->border_width;
  580. else if(c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
  581. m = c->mon;
  582. if(ev->value_mask & CWX)
  583. c->x = m->mx + ev->x;
  584. if(ev->value_mask & CWY)
  585. c->y = m->my + ev->y;
  586. if(ev->value_mask & CWWidth)
  587. c->w = ev->width;
  588. if(ev->value_mask & CWHeight)
  589. c->h = ev->height;
  590. if((c->x + c->w) > m->mx + m->mw && c->isfloating)
  591. c->x = m->mx + (m->mw / 2 - c->w / 2); /* center in x direction */
  592. if((c->y + c->h) > m->my + m->mh && c->isfloating)
  593. c->y = m->my + (m->mh / 2 - c->h / 2); /* center in y direction */
  594. if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
  595. configure(c);
  596. if(ISVISIBLE(c))
  597. XMoveResizeWindow(dpy, c->win, c->x + padding, c->y + padding, c->w - padding*2, c->h - padding*2);
  598. }
  599. else
  600. configure(c);
  601. }
  602. else {
  603. wc.x = ev->x;
  604. wc.y = ev->y;
  605. wc.width = ev->width;
  606. wc.height = ev->height;
  607. wc.border_width = ev->border_width;
  608. wc.sibling = ev->above;
  609. wc.stack_mode = ev->detail;
  610. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  611. }
  612. XSync(dpy, False);
  613. }
  614. Monitor *
  615. createmon(void) {
  616. Monitor *m;
  617. if(!(m = (Monitor *)calloc(1, sizeof(Monitor))))
  618. die("fatal: could not malloc() %u bytes\n", sizeof(Monitor));
  619. m->tagset[0] = m->tagset[1] = 1;
  620. m->mfact = mfact;
  621. m->showbar = showbar;
  622. m->topbar = topbar;
  623. m->lt[0] = &layouts[0];
  624. m->lt[1] = &layouts[1 % LENGTH(layouts)];
  625. strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
  626. return m;
  627. }
  628. void
  629. destroynotify(XEvent *e) {
  630. Client *c;
  631. XDestroyWindowEvent *ev = &e->xdestroywindow;
  632. Systray *s;
  633. if((c = wintoclient(ev->window)))
  634. unmanage(c, True);
  635. else if((s = systray_find(ev->window))){
  636. systray_del(s);
  637. systray_update();
  638. }
  639. }
  640. void
  641. detach(Client *c) {
  642. Client **tc;
  643. for(tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
  644. *tc = c->next;
  645. }
  646. void
  647. detachstack(Client *c) {
  648. Client **tc, *t;
  649. for(tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
  650. *tc = c->snext;
  651. if(c == c->mon->sel) {
  652. for(t = c->mon->stack; t && !ISFOCUSABLE(t); t = t->snext);
  653. c->mon->sel = t;
  654. }
  655. }
  656. void
  657. die(const char *errstr, ...) {
  658. va_list ap;
  659. va_start(ap, errstr);
  660. vfprintf(stderr, errstr, ap);
  661. va_end(ap);
  662. exit(EXIT_FAILURE);
  663. }
  664. Monitor *
  665. dirtomon(int dir) {
  666. Monitor *m = NULL;
  667. if(dir > 0) {
  668. if(!(m = selmon->next))
  669. m = mons;
  670. }
  671. else {
  672. if(selmon == mons)
  673. for(m = mons; m->next; m = m->next);
  674. else
  675. for(m = mons; m->next != selmon; m = m->next);
  676. }
  677. return m;
  678. }
  679. void
  680. drawbar(Monitor *m) {
  681. int x;
  682. unsigned int i, occ = 0, urg = 0;
  683. unsigned long *col;
  684. Client *c;
  685. for(c = m->clients; c; c = c->next) {
  686. occ |= c->tags;
  687. if(c->isurgent)
  688. urg |= c->tags;
  689. }
  690. dc.x = 0;
  691. for(i = 0; i < LENGTH(tags); i++) {
  692. dc.w = TEXTW(tags[i]);
  693. col = m->tagset[m->seltags] & 1 << i ? dc.sel : dc.norm;
  694. drawtext(tags[i], col, urg & 1 << i);
  695. drawsquare(m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
  696. occ & 1 << i, urg & 1 << i, col);
  697. dc.x += dc.w;
  698. }
  699. dc.w = blw = TEXTW(m->ltsymbol);
  700. drawtext(m->ltsymbol, dc.norm, False);
  701. dc.x += dc.w;
  702. x = dc.x;
  703. if(m == selmon) { /* status is only drawn on selected monitor */
  704. dc.w = TEXTW(stext);
  705. dc.x = m->ww - dc.w;
  706. if(dc.x < x) {
  707. dc.x = x;
  708. dc.w = m->ww - x;
  709. }
  710. drawtext(stext, dc.norm, False);
  711. }
  712. else
  713. dc.x = m->ww;
  714. if((dc.w = dc.x - x) > bh) {
  715. dc.x = x;
  716. if(m->sel) {
  717. col = m == selmon ? dc.sel : dc.norm;
  718. drawtext(m->sel->name, col, False);
  719. drawsquare(m->sel->isfixed, m->sel->isfloating, False, col);
  720. }
  721. else
  722. drawtext(NULL, dc.norm, False);
  723. }
  724. XCopyArea(dpy, dc.drawable, m->barwin, dc.gc, 0, 0, m->ww, bh, 0, 0);
  725. XSync(dpy, False);
  726. }
  727. void
  728. drawbars(void) {
  729. Monitor *m;
  730. for(m = mons; m; m = m->next)
  731. drawbar(m);
  732. }
  733. void
  734. drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) {
  735. int x;
  736. XGCValues gcv;
  737. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  738. gcv.foreground = col[invert ? ColBG : ColFG];
  739. XChangeGC(dpy, dc.gc, GCForeground, &gcv);
  740. x = (dc.font.ascent + dc.font.descent + 2) / 4;
  741. r.x = dc.x + 1;
  742. r.y = dc.y + 1;
  743. if(filled) {
  744. r.width = r.height = x + 1;
  745. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  746. }
  747. else if(empty) {
  748. r.width = r.height = x;
  749. XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  750. }
  751. }
  752. void
  753. drawtext(const char *text, unsigned long col[ColLast], Bool invert) {
  754. char buf[256];
  755. int i, x, y, h, len, olen;
  756. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  757. XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]);
  758. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  759. if(!text)
  760. return;
  761. olen = strlen(text);
  762. h = dc.font.ascent + dc.font.descent;
  763. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  764. x = dc.x + (h / 2);
  765. /* shorten text if necessary */
  766. for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
  767. if(!len)
  768. return;
  769. memcpy(buf, text, len);
  770. if(len < olen)
  771. for(i = len; i && i > len - 3; buf[--i] = '.');
  772. XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
  773. if(dc.font.set)
  774. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  775. else
  776. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  777. }
  778. void
  779. enternotify(XEvent *e) {
  780. Client *c;
  781. Monitor *m;
  782. XCrossingEvent *ev = &e->xcrossing;
  783. /* Don't handle EnterNotify event if it's about systray */
  784. if(systray_find(ev->window) || ev->window == traywin)
  785. return;
  786. if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
  787. return;
  788. c = wintoclient(ev->window);
  789. if(c != NULL)
  790. if(c->isbg) {
  791. focus(NULL);
  792. return;
  793. }
  794. if((m = wintomon(ev->window)) && m != selmon) {
  795. unfocus(selmon->sel, True);
  796. selmon = m;
  797. }
  798. if(c != NULL)
  799. focus(c);
  800. else
  801. focus(NULL);
  802. }
  803. void
  804. expose(XEvent *e) {
  805. Monitor *m;
  806. XExposeEvent *ev = &e->xexpose;
  807. if(ev->count == 0 && (m = wintomon(ev->window)))
  808. drawbar(m);
  809. }
  810. void
  811. focus(Client *c) {
  812. if(!c || !ISFOCUSABLE(c))
  813. for(c = selmon->stack; c && !ISFOCUSABLE(c); c = c->snext);
  814. /* was if(selmon->sel) */
  815. if(selmon->sel && selmon->sel != c)
  816. unfocus(selmon->sel, False);
  817. if(c) {
  818. if(c->mon != selmon)
  819. selmon = c->mon;
  820. if(c->isurgent)
  821. clearurgent(c);
  822. detachstack(c);
  823. attachstack(c);
  824. grabbuttons(c, True);
  825. XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
  826. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  827. }
  828. else
  829. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  830. selmon->sel = c;
  831. drawbars();
  832. }
  833. void
  834. focusin(XEvent *e) { /* there are some broken focus acquiring clients */
  835. XFocusChangeEvent *ev = &e->xfocus;
  836. if(selmon->sel && ev->window != selmon->sel->win && !selmon->sel->isbg)
  837. XSetInputFocus(dpy, selmon->sel->win, RevertToPointerRoot, CurrentTime);
  838. }
  839. void
  840. focusmon(const Arg *arg) {
  841. Monitor *m = NULL;
  842. if(!mons->next)
  843. return;
  844. if((m = dirtomon(arg->i)) == selmon)
  845. return;
  846. unfocus(selmon->sel, True);
  847. selmon = m;
  848. focus(NULL);
  849. }
  850. void
  851. focusstack(const Arg *arg) {
  852. Client *c = NULL, *i;
  853. if(!selmon->sel)
  854. return;
  855. if(arg->i > 0) {
  856. for(c = selmon->sel->next; c && !ISFOCUSABLE(c); c = c->next);
  857. if(!c)
  858. for(c = selmon->clients; c && !ISFOCUSABLE(c); c = c->next);
  859. }
  860. else {
  861. for(i = selmon->clients; i != selmon->sel; i = i->next)
  862. if(ISFOCUSABLE(i))
  863. c = i;
  864. if(!c)
  865. for(; i; i = i->next)
  866. if(ISFOCUSABLE(i))
  867. c = i;
  868. }
  869. if(c) {
  870. focus(c);
  871. restack(selmon);
  872. }
  873. }
  874. void paddingAdd(const Arg *arg) {
  875. padding += arg->i;
  876. if (padding < 0) padding = 0;
  877. printf("New padding is: %i\n", padding);
  878. arrange(NULL);
  879. }
  880. unsigned long
  881. getcolor(const char *colstr) {
  882. Colormap cmap = DefaultColormap(dpy, screen);
  883. XColor color;
  884. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  885. die("error, cannot allocate color '%s'\n", colstr);
  886. return color.pixel;
  887. }
  888. Bool
  889. getrootptr(int *x, int *y) {
  890. int di;
  891. unsigned int dui;
  892. Window dummy;
  893. return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
  894. }
  895. long
  896. getstate(Window w) {
  897. int format;
  898. long result = -1;
  899. unsigned char *p = NULL;
  900. unsigned long n, extra;
  901. Atom real;
  902. if(XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
  903. &real, &format, &n, &extra, (unsigned char **)&p) != Success)
  904. return -1;
  905. if(n != 0)
  906. result = *p;
  907. XFree(p);
  908. return result;
  909. }
  910. Bool
  911. gettextprop(Window w, Atom atom, char *text, unsigned int size) {
  912. char **list = NULL;
  913. int n;
  914. XTextProperty name;
  915. if(!text || size == 0)
  916. return False;
  917. text[0] = '\0';
  918. XGetTextProperty(dpy, w, &name, atom);
  919. if(!name.nitems)
  920. return False;
  921. if(name.encoding == XA_STRING)
  922. strncpy(text, (char *)name.value, size - 1);
  923. else {
  924. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
  925. strncpy(text, *list, size - 1);
  926. XFreeStringList(list);
  927. }
  928. }
  929. text[size - 1] = '\0';
  930. XFree(name.value);
  931. return True;
  932. }
  933. void
  934. grabbuttons(Client *c, Bool focused) {
  935. updatenumlockmask();
  936. {
  937. unsigned int i, j;
  938. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  939. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  940. if(focused) {
  941. for(i = 0; i < LENGTH(buttons); i++)
  942. if(buttons[i].click == ClkClientWin)
  943. for(j = 0; j < LENGTH(modifiers); j++)
  944. XGrabButton(dpy, buttons[i].button,
  945. buttons[i].mask | modifiers[j],
  946. c->win, False, BUTTONMASK,
  947. GrabModeAsync, GrabModeSync, None, None);
  948. }
  949. else
  950. XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
  951. BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
  952. }
  953. }
  954. void
  955. grabkeys(void) {
  956. updatenumlockmask();
  957. {
  958. unsigned int i, j;
  959. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  960. KeyCode code;
  961. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  962. for(i = 0; i < LENGTH(keys); i++) {
  963. if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
  964. for(j = 0; j < LENGTH(modifiers); j++)
  965. XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
  966. True, GrabModeAsync, GrabModeAsync);
  967. }
  968. }
  969. }
  970. void
  971. initfont(const char *fontstr) {
  972. char *def, **missing;
  973. int i, n;
  974. missing = NULL;
  975. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  976. if(missing) {
  977. while(n--)
  978. fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
  979. XFreeStringList(missing);
  980. }
  981. if(dc.font.set) {
  982. XFontSetExtents *font_extents;
  983. XFontStruct **xfonts;
  984. char **font_names;
  985. dc.font.ascent = dc.font.descent = 0;
  986. font_extents = XExtentsOfFontSet(dc.font.set);
  987. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  988. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  989. dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
  990. dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent);
  991. xfonts++;
  992. }
  993. }
  994. else {
  995. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  996. && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  997. die("error, cannot load font: '%s'\n", fontstr);
  998. dc.font.ascent = dc.font.xfont->ascent;
  999. dc.font.descent = dc.font.xfont->descent;
  1000. }
  1001. dc.font.height = dc.font.ascent + dc.font.descent;
  1002. }
  1003. Bool
  1004. isprotodel(Client *c) {
  1005. int i, n;
  1006. Atom *protocols;
  1007. Bool ret = False;
  1008. if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  1009. for(i = 0; !ret && i < n; i++)
  1010. if(protocols[i] == wmatom[WMDelete])
  1011. ret = True;
  1012. XFree(protocols);
  1013. }
  1014. return ret;
  1015. }
  1016. #ifdef XINERAMA
  1017. static Bool
  1018. isuniquegeom(XineramaScreenInfo *unique, size_t len, XineramaScreenInfo *info) {
  1019. unsigned int i;
  1020. for(i = 0; i < len; i++)
  1021. if(unique[i].x_org == info->x_org && unique[i].y_org == info->y_org
  1022. && unique[i].width == info->width && unique[i].height == info->height)
  1023. return False;
  1024. return True;
  1025. }
  1026. #endif /* XINERAMA */
  1027. void
  1028. keypress(XEvent *e) {
  1029. unsigned int i;
  1030. KeySym keysym;
  1031. XKeyEvent *ev;
  1032. ev = &e->xkey;
  1033. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  1034. for(i = 0; i < LENGTH(keys); i++)
  1035. if(keysym == keys[i].keysym
  1036. && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
  1037. && keys[i].func)
  1038. keys[i].func(&(keys[i].arg));
  1039. }
  1040. void
  1041. killclient(const Arg *arg) {
  1042. XEvent ev;
  1043. if(!selmon->sel)
  1044. return;
  1045. if(isprotodel(selmon->sel)) {
  1046. ev.type = ClientMessage;
  1047. ev.xclient.window = selmon->sel->win;
  1048. ev.xclient.message_type = wmatom[WMProtocols];
  1049. ev.xclient.format = 32;
  1050. ev.xclient.data.l[0] = wmatom[WMDelete];
  1051. ev.xclient.data.l[1] = CurrentTime;
  1052. XSendEvent(dpy, selmon->sel->win, False, NoEventMask, &ev);
  1053. }
  1054. else {
  1055. XGrabServer(dpy);
  1056. XSetErrorHandler(xerrordummy);
  1057. XSetCloseDownMode(dpy, DestroyAll);
  1058. XKillClient(dpy, selmon->sel->win);
  1059. XSync(dpy, False);
  1060. XSetErrorHandler(xerror);
  1061. XUngrabServer(dpy);
  1062. }
  1063. }
  1064. void
  1065. manage(Window w, XWindowAttributes *wa) {
  1066. static Client cz;
  1067. Client *c, *t = NULL;
  1068. Window trans = None;
  1069. XWindowChanges wc;
  1070. /*For kde3 apps*/
  1071. char kdetmp[6];
  1072. if ( gettextprop(w, netatom[KdeWMSysTrayWindow] , kdetmp, 6))
  1073. {
  1074. systray_add(w);
  1075. systray_update();
  1076. return;
  1077. }
  1078. if(!(c = malloc(sizeof(Client))))
  1079. die("fatal: could not malloc() %u bytes\n", sizeof(Client));
  1080. *c = cz;
  1081. c->win = w;
  1082. updatetitle(c);
  1083. if(XGetTransientForHint(dpy, w, &trans))
  1084. t = wintoclient(trans);
  1085. if(t) {
  1086. c->mon = t->mon;
  1087. c->tags = t->tags;
  1088. }
  1089. else {
  1090. c->mon = selmon;
  1091. applyrules(c);
  1092. }
  1093. /* geometry */
  1094. c->x = c->oldx = wa->x + c->mon->wx;
  1095. c->y = c->oldy = wa->y + c->mon->wy;
  1096. c->w = c->oldw = wa->width;
  1097. c->h = c->oldh = wa->height;
  1098. c->oldbw = wa->border_width;
  1099. if(c->w == c->mon->mw && c->h == c->mon->mh) {
  1100. c->isfloating = 1;
  1101. c->x = c->mon->mx;
  1102. c->y = c->mon->my;
  1103. c->bw = 0;
  1104. }
  1105. else {
  1106. if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
  1107. c->x = c->mon->mx + c->mon->mw - WIDTH(c);
  1108. if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
  1109. c->y = c->mon->my + c->mon->mh - HEIGHT(c);
  1110. c->x = MAX(c->x, c->mon->mx);
  1111. /* only fix client y-offset, if the client center might cover the bar */
  1112. c->y = MAX(c->y, ((c->mon->by == 0) && (c->x + (c->w / 2) >= c->mon->wx)
  1113. && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
  1114. c->bw = borderpx;
  1115. }
  1116. wc.border_width = c->bw;
  1117. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  1118. XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
  1119. configure(c); /* propagates border_width, if size doesn't change */
  1120. updatesizehints(c);
  1121. XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
  1122. grabbuttons(c, False);
  1123. if(!c->isfloating)
  1124. c->isfloating = c->oldstate = trans != None || c->isfixed;
  1125. if(c->isfloating && !c->isbg)
  1126. XRaiseWindow(dpy, c->win);
  1127. if(c->isbg)
  1128. XLowerWindow(dpy, c->win);
  1129. attach(c);
  1130. attachstack(c);
  1131. XMoveResizeWindow(dpy, c->win, c->x + 2 * sw + padding, c->y + padding, c->w - padding*2, c->h - padding*2); /* some windows require this */
  1132. XMapWindow(dpy, c->win);
  1133. setclientstate(c, NormalState);
  1134. arrange(c->mon);
  1135. }
  1136. void
  1137. mappingnotify(XEvent *e) {
  1138. XMappingEvent *ev = &e->xmapping;
  1139. XRefreshKeyboardMapping(ev);
  1140. if(ev->request == MappingKeyboard)
  1141. grabkeys();
  1142. }
  1143. void
  1144. maprequest(XEvent *e) {
  1145. static XWindowAttributes wa;
  1146. XMapRequestEvent *ev = &e->xmaprequest;
  1147. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  1148. return;
  1149. if(wa.override_redirect)
  1150. return;
  1151. if(!wintoclient(ev->window))
  1152. manage(ev->window, &wa);
  1153. }
  1154. void
  1155. monocle(Monitor *m) {
  1156. unsigned int n = 0;
  1157. Client *c;
  1158. for(c = m->clients; c; c = c->next)
  1159. if(ISVISIBLE(c))
  1160. n++;
  1161. if(n > 0) /* override layout symbol */
  1162. snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
  1163. for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
  1164. resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, False);
  1165. }
  1166. void
  1167. movemouse(const Arg *arg) {
  1168. int x, y, ocx, ocy, nx, ny;
  1169. Client *c;
  1170. Monitor *m;
  1171. XEvent ev;
  1172. if(!(c = selmon->sel))
  1173. return;
  1174. /*if(c->isbg)
  1175. return;*/
  1176. restack(selmon);
  1177. ocx = c->x;
  1178. ocy = c->y;
  1179. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1180. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  1181. return;
  1182. if(!getrootptr(&x, &y))
  1183. return;
  1184. do {
  1185. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1186. switch (ev.type) {
  1187. case ConfigureRequest:
  1188. case Expose:
  1189. case MapRequest:
  1190. handler[ev.type](&ev);
  1191. break;
  1192. case MotionNotify:
  1193. nx = ocx + (ev.xmotion.x - x);
  1194. ny = ocy + (ev.xmotion.y - y);
  1195. if(snap && nx >= selmon->wx && nx <= selmon->wx + selmon->ww
  1196. && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
  1197. if(abs(selmon->wx - nx) < snap)
  1198. nx = selmon->wx;
  1199. else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
  1200. nx = selmon->wx + selmon->ww - WIDTH(c);
  1201. if(abs(selmon->wy - ny) < snap)
  1202. ny = selmon->wy;
  1203. else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
  1204. ny = selmon->wy + selmon->wh - HEIGHT(c);
  1205. if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1206. && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
  1207. togglefloating(NULL);
  1208. }
  1209. if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
  1210. resize(c, nx, ny, c->w, c->h, True);
  1211. break;
  1212. }
  1213. } while(ev.type != ButtonRelease);
  1214. XUngrabPointer(dpy, CurrentTime);
  1215. if((m = ptrtomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
  1216. sendmon(c, m);
  1217. selmon = m;
  1218. focus(NULL);
  1219. }
  1220. }
  1221. Client *
  1222. nexttiled(Client *c) {
  1223. for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
  1224. return c;
  1225. }
  1226. Monitor *
  1227. ptrtomon(int x, int y) {
  1228. Monitor *m;
  1229. for(m = mons; m; m = m->next)
  1230. if(INRECT(x, y, m->wx, m->wy, m->ww, m->wh))
  1231. return m;
  1232. return selmon;
  1233. }
  1234. void
  1235. propertynotify(XEvent *e) {
  1236. Client *c;
  1237. Window trans;
  1238. XPropertyEvent *ev = &e->xproperty;
  1239. if((ev->window == root) && (ev->atom == XA_WM_NAME))
  1240. updatestatus();
  1241. else if(ev->state == PropertyDelete)
  1242. return; /* ignore */
  1243. else if((c = wintoclient(ev->window))) {
  1244. switch (ev->atom) {
  1245. default: break;
  1246. case XA_WM_TRANSIENT_FOR:
  1247. XGetTransientForHint(dpy, c->win, &trans);
  1248. if(!c->isfloating && (c->isfloating = (wintoclient(trans) != NULL)))
  1249. arrange(c->mon);
  1250. break;
  1251. case XA_WM_NORMAL_HINTS:
  1252. updatesizehints(c);
  1253. break;
  1254. case XA_WM_HINTS:
  1255. updatewmhints(c);
  1256. drawbars();
  1257. break;
  1258. }
  1259. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  1260. updatetitle(c);
  1261. if(c == c->mon->sel)
  1262. drawbar(c->mon);
  1263. }
  1264. }
  1265. }
  1266. void
  1267. clientmessage(XEvent *e) {
  1268. XClientMessageEvent *cme = &e->xclient;
  1269. Client *c;
  1270. if((c = wintoclient(cme->window))
  1271. && (cme->message_type == netatom[NetWMState] && cme->data.l[1] == netatom[NetWMFullscreen]))
  1272. {
  1273. if(cme->data.l[0]) {
  1274. XChangeProperty(dpy, cme->window, netatom[NetWMState], XA_ATOM, 32,
  1275. PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
  1276. c->oldstate = c->isfloating;
  1277. c->oldbw = c->bw;
  1278. c->bw = 0;
  1279. c->isfloating = 1;
  1280. resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
  1281. XRaiseWindow(dpy, c->win);
  1282. }
  1283. else {
  1284. XChangeProperty(dpy, cme->window, netatom[NetWMState], XA_ATOM, 32,
  1285. PropModeReplace, (unsigned char*)0, 0);
  1286. c->isfloating = c->oldstate;
  1287. c->bw = c->oldbw;
  1288. c->x = c->oldx;
  1289. c->y = c->oldy;
  1290. c->w = c->oldw;
  1291. c->h = c->oldh;
  1292. resizeclient(c, c->x, c->y, c->w, c->h);
  1293. arrange(c->mon);
  1294. }
  1295. }
  1296. if(cme->window == traywin) {
  1297. if(cme->data.l[1] == XEMBED_EMBEDDED_NOTIFY){
  1298. systray_add(cme->data.l[2]);
  1299. systray_update();
  1300. }
  1301. }
  1302. }
  1303. void
  1304. quit(const Arg *arg) {
  1305. running = False;
  1306. }
  1307. void
  1308. resize(Client *c, int x, int y, int w, int h, Bool interact) {
  1309. if(applysizehints(c, &x, &y, &w, &h, interact))
  1310. resizeclient(c, x, y, w, h);
  1311. }
  1312. void
  1313. resizeclient(Client *c, int x, int y, int w, int h) {
  1314. XWindowChanges wc;
  1315. c->oldx = c->x; c->x = x;
  1316. c->oldy = c->y; c->y = y;
  1317. c->oldw = c->w; c->w = w;
  1318. c->oldh = c->h; c->h = h;
  1319. if (w > padding*4 && h > padding*4) {
  1320. x += padding;
  1321. y += padding;
  1322. w -= padding*2;
  1323. h -= padding*2;
  1324. }
  1325. wc.x = x;
  1326. wc.y = y;
  1327. wc.width = w;
  1328. wc.height = h;
  1329. wc.border_width = c->bw;
  1330. XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  1331. configure(c);
  1332. XSync(dpy, False);
  1333. }
  1334. void
  1335. resizemouse(const Arg *arg) {
  1336. int ocx, ocy;
  1337. int nw, nh;
  1338. Client *c;
  1339. Monitor *m;
  1340. XEvent ev;
  1341. if(!(c = selmon->sel))
  1342. return;
  1343. restack(selmon);
  1344. ocx = c->x;
  1345. ocy = c->y;
  1346. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1347. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  1348. return;
  1349. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1350. do {
  1351. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1352. switch(ev.type) {
  1353. case ConfigureRequest:
  1354. case Expose:
  1355. case MapRequest:
  1356. handler[ev.type](&ev);
  1357. break;
  1358. case MotionNotify:
  1359. nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
  1360. nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
  1361. if(snap && nw >= selmon->wx && nw <= selmon->wx + selmon->ww
  1362. && nh >= selmon->wy && nh <= selmon->wy + selmon->wh)
  1363. {
  1364. if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1365. && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
  1366. togglefloating(NULL);
  1367. }
  1368. if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
  1369. resize(c, c->x, c->y, nw, nh, True);
  1370. break;
  1371. }
  1372. } while(ev.type != ButtonRelease);
  1373. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1374. XUngrabPointer(dpy, CurrentTime);
  1375. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1376. if((m = ptrtomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
  1377. sendmon(c, m);
  1378. selmon = m;
  1379. focus(NULL);
  1380. }
  1381. }
  1382. void
  1383. restack(Monitor *m) {
  1384. Client *c;
  1385. XEvent ev;
  1386. XWindowChanges wc;
  1387. drawbar(m);
  1388. if(!m->sel)
  1389. return;
  1390. if((m->sel->isfloating || !m->lt[m->sellt]->arrange) && (!m->sel->isbg))
  1391. XRaiseWindow(dpy, m->sel->win);
  1392. if(m->sel->isbg)
  1393. XLowerWindow(dpy, m->sel->win);
  1394. if(m->lt[m->sellt]->arrange) {
  1395. wc.stack_mode = Below;
  1396. wc.sibling = m->barwin;
  1397. for(c = m->stack; c; c = c->snext)
  1398. if(!c->isfloating && ISVISIBLE(c)) {
  1399. XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
  1400. wc.sibling = c->win;
  1401. }
  1402. }
  1403. XSync(dpy, False);
  1404. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1405. }
  1406. void
  1407. run(void) {
  1408. XEvent ev;
  1409. /* main event loop */
  1410. XSync(dpy, False);
  1411. while(running && !XNextEvent(dpy, &ev)) {
  1412. if(handler[ev.type])
  1413. handler[ev.type](&ev); /* call handler */
  1414. }
  1415. }
  1416. void
  1417. scan(void) {
  1418. unsigned int i, num;
  1419. Window d1, d2, *wins = NULL;
  1420. XWindowAttributes wa;
  1421. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1422. for(i = 0; i < num; i++) {
  1423. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  1424. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1425. continue;
  1426. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1427. manage(wins[i], &wa);
  1428. }
  1429. for(i = 0; i < num; i++) { /* now the transients */
  1430. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1431. continue;
  1432. if(XGetTransientForHint(dpy, wins[i], &d1)
  1433. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1434. manage(wins[i], &wa);
  1435. }
  1436. if(wins)
  1437. XFree(wins);
  1438. }
  1439. }
  1440. void
  1441. sendmon(Client *c, Monitor *m) {
  1442. if(c->mon == m)
  1443. return;
  1444. unfocus(c, True);
  1445. detach(c);
  1446. detachstack(c);
  1447. c->mon = m;
  1448. c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
  1449. attach(c);
  1450. attachstack(c);
  1451. focus(NULL);
  1452. arrange(NULL);
  1453. }
  1454. void
  1455. setclientstate(Client *c, long state) {
  1456. long data[] = { state, None };
  1457. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1458. PropModeReplace, (unsigned char *)data, 2);
  1459. }
  1460. void
  1461. setlayout(const Arg *arg) {
  1462. if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
  1463. selmon->sellt ^= 1;
  1464. if(arg && arg->v)
  1465. selmon->lt[selmon->sellt] = (Layout *)arg->v;
  1466. strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
  1467. if(selmon->sel)
  1468. arrange(selmon);
  1469. else
  1470. drawbar(selmon);
  1471. }
  1472. /* arg > 1.0 will set mfact absolutly */
  1473. void
  1474. setmfact(const Arg *arg) {
  1475. float f;
  1476. if(!arg || !selmon->lt[selmon->sellt]->arrange)
  1477. return;
  1478. f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
  1479. if(f < 0.1 || f > 0.9)
  1480. return;
  1481. selmon->mfact = f;
  1482. arrange(selmon);
  1483. }
  1484. void
  1485. setup(void) {
  1486. XSetWindowAttributes wa;
  1487. /* clean up any zombies immediately */
  1488. sigchld(0);
  1489. /* init screen */
  1490. screen = DefaultScreen(dpy);
  1491. root = RootWindow(dpy, screen);
  1492. initfont(font);
  1493. sw = DisplayWidth(dpy, screen);
  1494. sh = DisplayHeight(dpy, screen);
  1495. bh = dc.h = dc.font.height + 2;
  1496. updategeom();
  1497. /* init atoms */
  1498. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1499. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1500. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1501. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1502. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1503. netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
  1504. netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
  1505. netatom[NetSystemTray] = XInternAtom(dpy, "_NET_SYSTEM_TRAY_S0", False);
  1506. netatom[KdeWMSysTrayWindow] = XInternAtom(dpy, "_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR", False);
  1507. /* init cursors */
  1508. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  1509. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  1510. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  1511. /* init appearance */
  1512. dc.norm[ColBorder] = getcolor(normbordercolor);
  1513. dc.norm[ColBG] = getcolor(normbgcolor);
  1514. dc.norm[ColFG] = getcolor(normfgcolor);
  1515. dc.sel[ColBorder] = getcolor(selbordercolor);
  1516. dc.sel[ColBG] = getcolor(selbgcolor);
  1517. dc.sel[ColFG] = getcolor(selfgcolor);
  1518. dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
  1519. dc.gc = XCreateGC(dpy, root, 0, NULL);
  1520. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1521. if(!dc.font.set)
  1522. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  1523. /* init bars */
  1524. updatebars();
  1525. updatestatus();
  1526. /* EWMH support per view */
  1527. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1528. PropModeReplace, (unsigned char *) netatom, NetLast);
  1529. /* select for events */
  1530. wa.cursor = cursor[CurNormal];
  1531. wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask
  1532. |EnterWindowMask|LeaveWindowMask|StructureNotifyMask
  1533. |PropertyChangeMask;
  1534. XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
  1535. XSelectInput(dpy, root, wa.event_mask);
  1536. grabkeys();
  1537. }
  1538. void
  1539. showhide(Client *c) {
  1540. if(!c)
  1541. return;
  1542. if(c->isbg) {
  1543. showhide(c->snext);
  1544. return;
  1545. }
  1546. if(ISVISIBLE(c)) { /* show clients top down */
  1547. XMoveWindow(dpy, c->win, c->x + padding, c->y + padding);
  1548. if(!c->mon->lt[c->mon->sellt]->arrange || c->isfloating)
  1549. resize(c, c->x, c->y, c->w, c->h, False);
  1550. showhide(c->snext);
  1551. }
  1552. else { /* hide clients bottom up */
  1553. showhide(c->snext);
  1554. XMoveWindow(dpy, c->win, c->x + 2 * sw + padding, c->y + padding);
  1555. }
  1556. }
  1557. void
  1558. sigchld(int unused) {
  1559. if(signal(SIGCHLD, sigchld) == SIG_ERR)
  1560. die("Can't install SIGCHLD handler");
  1561. while(0 < waitpid(-1, NULL, WNOHANG));
  1562. }
  1563. void
  1564. spawn(const Arg *arg) {
  1565. if(fork() == 0) {
  1566. if(dpy)
  1567. close(ConnectionNumber(dpy));
  1568. setsid();
  1569. execvp(((char **)arg->v)[0], (char **)arg->v);
  1570. fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
  1571. perror(" failed");
  1572. exit(0);
  1573. }
  1574. }
  1575. void
  1576. tag(const Arg *arg) {
  1577. if(selmon->sel && arg->ui & TAGMASK) {
  1578. selmon->sel->tags = arg->ui & TAGMASK;
  1579. arrange(selmon);
  1580. }
  1581. }
  1582. void
  1583. tagmon(const Arg *arg) {
  1584. if(!selmon->sel || !mons->next)
  1585. return;
  1586. sendmon(selmon->sel, dirtomon(arg->i));
  1587. }
  1588. int
  1589. textnw(const char *text, unsigned int len) {
  1590. XRectangle r;
  1591. if(dc.font.set) {
  1592. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  1593. return r.width;
  1594. }
  1595. return XTextWidth(dc.font.xfont, text, len);
  1596. }
  1597. void
  1598. tile(Monitor *m) {
  1599. int x, y, h, w, mw;
  1600. unsigned int i, n;
  1601. Client *c;
  1602. for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
  1603. if(n == 0)
  1604. return;
  1605. /* master */
  1606. c = nexttiled(m->clients);
  1607. mw = m->mfact * m->ww;
  1608. resize(c, m->wx, m->wy, (n == 1 ? m->ww : mw) - 2 * c->bw, m->wh - 2 * c->bw, False);
  1609. if(--n == 0)
  1610. return;
  1611. /* tile stack */
  1612. x = (m->wx + mw > c->x + c->w) ? c->x + c->w + 2 * c->bw : m->wx + mw;
  1613. y = m->wy;
  1614. w = (m->wx + mw > c->x + c->w) ? m->wx + m->ww - x : m->ww - mw;
  1615. h = m->wh / n;
  1616. if(h < bh)
  1617. h = m->wh;
  1618. for(i = 0, c = nexttiled(c->next); c; c = nexttiled(c->next), i++) {
  1619. resize(c, x, y, w - 2 * c->bw, /* remainder */ ((i + 1 == n)
  1620. ? m->wy + m->wh - y - 2 * c->bw : h - 2 * c->bw), False);
  1621. if(h != m->wh)
  1622. y = c->y + HEIGHT(c);
  1623. }
  1624. }
  1625. void
  1626. togglebar(const Arg *arg) {
  1627. selmon->showbar = !selmon->showbar;
  1628. updatebarpos(selmon);
  1629. XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
  1630. arrange(selmon);
  1631. }
  1632. void
  1633. togglebg(const Arg *arg) {
  1634. if(!selmon->sel)
  1635. return;
  1636. selmon->sel->isbg = !selmon->sel->isbg;
  1637. if(selmon->sel->isbg) {
  1638. XLowerWindow(dpy, selmon->sel->win);
  1639. XWindowChanges wc;
  1640. wc.border_width = 0;
  1641. selmon->sel->tags = 0;
  1642. XConfigureWindow(dpy, selmon->sel->win, CWBorderWidth, &wc);
  1643. } else {
  1644. selmon->sel->tags = ~0;
  1645. }
  1646. arrange(selmon);
  1647. }
  1648. void
  1649. togglefloating(const Arg *arg) {
  1650. if(!selmon->sel)
  1651. return;
  1652. selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
  1653. if(selmon->sel->isfloating)
  1654. resize(selmon->sel, selmon->sel->x, selmon->sel->y,
  1655. selmon->sel->w, selmon->sel->h, False);
  1656. arrange(selmon);
  1657. }
  1658. void
  1659. toggletag(const Arg *arg) {
  1660. unsigned int newtags;
  1661. if(!selmon->sel)
  1662. return;
  1663. newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
  1664. if(newtags) {
  1665. selmon->sel->tags = newtags;
  1666. arrange(selmon);
  1667. }
  1668. }
  1669. void
  1670. toggleview(const Arg *arg) {
  1671. unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
  1672. if(newtagset) {
  1673. selmon->tagset[selmon->seltags] = newtagset;
  1674. arrange(selmon);
  1675. }
  1676. }
  1677. void
  1678. unfocus(Client *c, Bool setfocus) {
  1679. if(!c)
  1680. return;
  1681. grabbuttons(c, False);
  1682. XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
  1683. if(setfocus)
  1684. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  1685. }
  1686. void
  1687. unmanage(Client *c, Bool destroyed) {
  1688. Monitor *m = c->mon;
  1689. XWindowChanges wc;
  1690. /* The server grab construct avoids race conditions. */
  1691. detach(c);
  1692. detachstack(c);
  1693. if(!destroyed) {
  1694. wc.border_width = c->oldbw;
  1695. XGrabServer(dpy);
  1696. XSetErrorHandler(xerrordummy);
  1697. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1698. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1699. setclientstate(c, WithdrawnState);
  1700. XSync(dpy, False);
  1701. XSetErrorHandler(xerror);
  1702. XUngrabServer(dpy);
  1703. }
  1704. free(c);
  1705. focus(NULL);
  1706. arrange(m);
  1707. }
  1708. void
  1709. unmapnotify(XEvent *e) {
  1710. Client *c;
  1711. XUnmapEvent *ev = &e->xunmap;
  1712. if((c = wintoclient(ev->window)))
  1713. unmanage(c, False);
  1714. }
  1715. void
  1716. updatebars(void) {
  1717. Monitor *m;
  1718. XSetWindowAttributes wa;
  1719. wa.override_redirect = True;
  1720. wa.background_pixmap = ParentRelative;
  1721. wa.event_mask = ButtonPressMask|ExposureMask;
  1722. for(m = mons; m; m = m->next) {
  1723. m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
  1724. CopyFromParent, DefaultVisual(dpy, screen),
  1725. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  1726. XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
  1727. XMapRaised(dpy, m->barwin);
  1728. }
  1729. systray_acquire();
  1730. }
  1731. void
  1732. updatebarpos(Monitor *m) {
  1733. m->wy = m->my;
  1734. m->wh = m->mh;
  1735. if(m->showbar) {
  1736. m->wh -= bh;
  1737. m->by = m->topbar ? m->wy : m->wy + m->wh;
  1738. m->wy = m->topbar ? m->wy + bh : m->wy;
  1739. }
  1740. else
  1741. m->by = -bh;
  1742. }
  1743. Bool
  1744. updategeom(void) {
  1745. Bool dirty = False;
  1746. #ifdef XINERAMA
  1747. if(XineramaIsActive(dpy)) {
  1748. int i, j, n, nn;
  1749. Client *c;
  1750. Monitor *m;
  1751. XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
  1752. XineramaScreenInfo *unique = NULL;
  1753. info = XineramaQueryScreens(dpy, &nn);
  1754. for(n = 0, m = mons; m; m = m->next, n++);
  1755. /* only consider unique geometries as separate screens */
  1756. if(!(unique = (XineramaScreenInfo *)malloc(sizeof(XineramaScreenInfo) * nn)))
  1757. die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo) * nn);
  1758. for(i = 0, j = 0; i < nn; i++)
  1759. if(isuniquegeom(unique, j, &info[i]))
  1760. memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
  1761. XFree(info);
  1762. nn = j;
  1763. if(n <= nn) {
  1764. for(i = 0; i < (nn - n); i++) { /* new monitors available */
  1765. for(m = mons; m && m->next; m = m->next);
  1766. if(m)
  1767. m->next = createmon();
  1768. else
  1769. mons = createmon();
  1770. }
  1771. for(i = 0, m = mons; i < nn && m; m = m->next, i++)
  1772. if(i >= n
  1773. || (unique[i].x_org != m->mx || unique[i].y_org != m->my
  1774. || unique[i].width != m->mw || unique[i].height != m->mh))
  1775. {
  1776. dirty = True;
  1777. m->num = i;
  1778. m->mx = m->wx = unique[i].x_org;
  1779. m->my = m->wy = unique[i].y_org;
  1780. m->mw = m->ww = unique[i].width;
  1781. m->mh = m->wh = unique[i].height;
  1782. updatebarpos(m);
  1783. }
  1784. }
  1785. else { /* less monitors available nn < n */
  1786. for(i = nn; i < n; i++) {
  1787. for(m = mons; m && m->next; m = m->next);
  1788. while(m->clients) {
  1789. dirty = True;
  1790. c = m->clients;
  1791. m->clients = c->next;
  1792. detachstack(c);
  1793. c->mon = mons;
  1794. attach(c);
  1795. attachstack(c);
  1796. }
  1797. if(m == selmon)
  1798. selmon = mons;
  1799. cleanupmon(m);
  1800. }
  1801. }
  1802. free(unique);
  1803. }
  1804. else
  1805. #endif /* XINERAMA */
  1806. /* default monitor setup */
  1807. {
  1808. if(!mons)
  1809. mons = createmon();
  1810. if(mons->mw != sw || mons->mh != sh) {
  1811. dirty = True;
  1812. mons->mw = mons->ww = sw;
  1813. mons->mh = mons->wh = sh;
  1814. updatebarpos(mons);
  1815. }
  1816. }
  1817. if(dirty) {
  1818. selmon = mons;
  1819. selmon = wintomon(root);
  1820. }
  1821. return dirty;
  1822. }
  1823. void
  1824. updatenumlockmask(void) {
  1825. unsigned int i, j;
  1826. XModifierKeymap *modmap;
  1827. numlockmask = 0;
  1828. modmap = XGetModifierMapping(dpy);
  1829. for(i = 0; i < 8; i++)
  1830. for(j = 0; j < modmap->max_keypermod; j++)
  1831. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  1832. == XKeysymToKeycode(dpy, XK_Num_Lock))
  1833. numlockmask = (1 << i);
  1834. XFreeModifiermap(modmap);
  1835. }
  1836. void
  1837. updatesizehints(Client *c) {
  1838. long msize;
  1839. XSizeHints size;
  1840. if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
  1841. /* size is uninitialized, ensure that size.flags aren't used */
  1842. size.flags = PSize;
  1843. if(size.flags & PBaseSize) {
  1844. c->basew = size.base_width;
  1845. c->baseh = size.base_height;
  1846. }
  1847. else if(size.flags & PMinSize) {
  1848. c->basew = size.min_width;
  1849. c->baseh = size.min_height;
  1850. }
  1851. else
  1852. c->basew = c->baseh = 0;
  1853. if(size.flags & PResizeInc) {
  1854. c->incw = size.width_inc;
  1855. c->inch = size.height_inc;
  1856. }
  1857. else
  1858. c->incw = c->inch = 0;
  1859. if(size.flags & PMaxSize) {
  1860. c->maxw = size.max_width;
  1861. c->maxh = size.max_height;
  1862. }
  1863. else
  1864. c->maxw = c->maxh = 0;
  1865. if(size.flags & PMinSize) {
  1866. c->minw = size.min_width;
  1867. c->minh = size.min_height;
  1868. }
  1869. else if(size.flags & PBaseSize) {
  1870. c->minw = size.base_width;
  1871. c->minh = size.base_height;
  1872. }
  1873. else
  1874. c->minw = c->minh = 0;
  1875. if(size.flags & PAspect) {
  1876. c->mina = (float)size.min_aspect.y / size.min_aspect.x;
  1877. c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
  1878. }
  1879. else
  1880. c->maxa = c->mina = 0.0;
  1881. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1882. && c->maxw == c->minw && c->maxh == c->minh);
  1883. }
  1884. void
  1885. updatetitle(Client *c) {
  1886. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1887. gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
  1888. if(c->name[0] == '\0') /* hack to mark broken clients */
  1889. strcpy(c->name, broken);
  1890. }
  1891. void
  1892. updatestatus(void) {
  1893. if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
  1894. strcpy(stext, "dwm-"VERSION);
  1895. drawbar(selmon);
  1896. systray_acquire();
  1897. }
  1898. void
  1899. updatewmhints(Client *c) {
  1900. XWMHints *wmh;
  1901. if((wmh = XGetWMHints(dpy, c->win))) {
  1902. if(c == selmon->sel && wmh->flags & XUrgencyHint) {
  1903. wmh->flags &= ~XUrgencyHint;
  1904. XSetWMHints(dpy, c->win, wmh);
  1905. }
  1906. else
  1907. c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
  1908. XFree(wmh);
  1909. }
  1910. }
  1911. void
  1912. view(const Arg *arg) {
  1913. if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
  1914. return;
  1915. selmon->seltags ^= 1; /* toggle sel tagset */
  1916. if(arg->ui & TAGMASK)
  1917. selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
  1918. arrange(selmon);
  1919. }
  1920. Client *
  1921. wintoclient(Window w) {
  1922. Client *c;
  1923. Monitor *m;
  1924. for(m = mons; m; m = m->next)
  1925. for(c = m->clients; c; c = c->next)
  1926. if(c->win == w)
  1927. return c;
  1928. return NULL;
  1929. }
  1930. Monitor *
  1931. wintomon(Window w) {
  1932. int x, y;
  1933. Client *c;
  1934. Monitor *m;
  1935. if(w == root && getrootptr(&x, &y))
  1936. return ptrtomon(x, y);
  1937. for(m = mons; m; m = m->next)
  1938. if(w == m->barwin)
  1939. return m;
  1940. if((c = wintoclient(w)))
  1941. return c->mon;
  1942. return selmon;
  1943. }
  1944. /* There's no way to check accesses to destroyed windows, thus those cases are
  1945. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1946. * default error handler, which may call exit. */
  1947. int
  1948. xerror(Display *dpy, XErrorEvent *ee) {
  1949. if(ee->error_code == BadWindow
  1950. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1951. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1952. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1953. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1954. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1955. || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
  1956. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1957. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1958. return 0;
  1959. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1960. ee->request_code, ee->error_code);
  1961. return xerrorxlib(dpy, ee); /* may call exit */
  1962. }
  1963. int
  1964. xerrordummy(Display *dpy, XErrorEvent *ee) {
  1965. return 0;
  1966. }
  1967. /* Startup Error handler to check if another window manager
  1968. * is already running. */
  1969. int
  1970. xerrorstart(Display *dpy, XErrorEvent *ee) {
  1971. otherwm = True;
  1972. return -1;
  1973. }
  1974. void
  1975. zoom(const Arg *arg) {
  1976. Client *c = selmon->sel;
  1977. if(!selmon->lt[selmon->sellt]->arrange
  1978. || selmon->lt[selmon->sellt]->arrange == monocle
  1979. || (selmon->sel && selmon->sel->isfloating))
  1980. return;
  1981. if(c == nexttiled(selmon->clients))
  1982. if(!c || !(c = nexttiled(c->next)))
  1983. return;
  1984. detach(c);
  1985. attach(c);
  1986. focus(c);
  1987. arrange(c->mon);
  1988. }
  1989. int
  1990. main(int argc, char *argv[]) {
  1991. if(argc == 2 && !strcmp("-v", argv[1]))
  1992. die("dwm-"VERSION", © 2006-2010 dwm engineers, see LICENSE for details\n");
  1993. else if(argc != 1)
  1994. die("usage: dwm [-v]\n");
  1995. if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  1996. fputs("warning: no locale support\n", stderr);
  1997. if(!(dpy = XOpenDisplay(NULL)))
  1998. die("dwm: cannot open display\n");
  1999. checkotherwm();
  2000. setup();
  2001. scan();
  2002. run();
  2003. cleanup();
  2004. XCloseDisplay(dpy);
  2005. return 0;
  2006. }
  2007. /*For Systray*/
  2008. /*---Systray ---*/
  2009. Bool systray_acquire(void) {
  2010. XSetWindowAttributes wattr;
  2011. if(!systray_enable || traywin) return False;
  2012. if(XGetSelectionOwner(dpy, netatom[NetSystemTray]) != None) {
  2013. fprintf(stderr, "Can't initialize system tray: owned by another process\n");
  2014. return False;
  2015. }
  2016. // Init traywin window
  2017. wattr.event_mask = ButtonPressMask | ExposureMask;
  2018. wattr.override_redirect = True;
  2019. wattr.background_pixmap = ParentRelative;
  2020. wattr.background_pixel = dc.norm[ColBG];
  2021. traywin = XCreateSimpleWindow(dpy, root, 0, 0, 1, 1, 0, 0, dc.norm[ColBG]);
  2022. XChangeWindowAttributes(dpy, traywin, CWEventMask | CWOverrideRedirect | CWBackPixel, &wattr);
  2023. XSelectInput(dpy, traywin, KeyPressMask | ButtonPressMask);
  2024. XMapRaised(dpy, traywin);
  2025. XSetSelectionOwner(dpy, netatom[NetSystemTray], traywin, CurrentTime);
  2026. if(XGetSelectionOwner(dpy, netatom[NetSystemTray]) != traywin) {
  2027. systray_freeicons();
  2028. fprintf(stderr, "System tray: can't get systray manager\n");
  2029. return False;
  2030. }
  2031. XSync(dpy, False);
  2032. return True;
  2033. }
  2034. void systray_add(Window win) {
  2035. Systray *s;
  2036. if(!systray_enable) return;
  2037. if(systray_find(win)) return;
  2038. s = zcalloc(sizeof(Systray));
  2039. s->win = win;
  2040. s->geo.height = bh;
  2041. s->geo.width = bh;
  2042. XSelectInput(dpy, s->win, StructureNotifyMask | PropertyChangeMask| EnterWindowMask | FocusChangeMask);
  2043. XReparentWindow(dpy, s->win, traywin, 0, 0);
  2044. // Attach
  2045. if(trayicons) trayicons->prev = s;
  2046. s->next = trayicons;
  2047. trayicons = s;
  2048. return;
  2049. }
  2050. void systray_del(Systray *s) {
  2051. Systray **ss;
  2052. if(!systray_enable) return;
  2053. for(ss = &trayicons; *ss && *ss != s; ss = &(*ss)->next);
  2054. *ss = s->next;
  2055. IFREE(s);
  2056. return;
  2057. }
  2058. void systray_freeicons(void) {
  2059. Systray *i;
  2060. if(!systray_enable) return;
  2061. for(i = trayicons; i; i = i->next) {
  2062. XUnmapWindow(dpy, i->win);
  2063. XReparentWindow(dpy, i->win, root, 0, 0);
  2064. IFREE(i);
  2065. }
  2066. XSetSelectionOwner(dpy, netatom[NetSystemTray], None, CurrentTime);
  2067. XDestroyWindow(dpy, traywin);
  2068. XSync(dpy, 0);
  2069. return;
  2070. }
  2071. Systray* systray_find(Window win) {
  2072. Systray *i;
  2073. if(!systray_enable) return NULL;
  2074. for(i = trayicons; i; i = i->next)
  2075. if(i->win == win) return i;
  2076. return NULL;
  2077. }
  2078. /*
  2079. int systray_get_width(void) {
  2080. int w = 0;
  2081. Systray *i;
  2082. if(!systray_enable) return 0;
  2083. for(i = trayicons; i; i = i->next)
  2084. w += i->geo.width + systray_spacing + 1;
  2085. return w;
  2086. }
  2087. */
  2088. void systray_update(void) {
  2089. Systray *i;
  2090. //Monitor *m;
  2091. int x = 1;
  2092. int pos = blw;
  2093. if(!systray_enable) return;
  2094. /*
  2095. for(m = mons; m; m = m -> next){
  2096. if(m->primary == 1) pos = m -> ww;
  2097. }*/
  2098. // pos = mons -> ww - W_bar_reserved;
  2099. pos = mons -> ww - 250;
  2100. if(!trayicons) {
  2101. pos -= 1;
  2102. XMoveResizeWindow(dpy, traywin, pos, 0, 1, 1);
  2103. return;
  2104. }
  2105. for(i = trayicons; i; i = i->next) {
  2106. XMapWindow(dpy, i->win);
  2107. XMoveResizeWindow(dpy, i->win, (i->geo.x = x), 0, i->geo.width, i->geo.height);
  2108. x += i->geo.width + systray_spacing;
  2109. }
  2110. pos -= x;
  2111. XMoveResizeWindow(dpy, traywin, pos, 0, x, bh);
  2112. return;
  2113. }