dwm.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729
  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. * Calls to fetch an X event from the event queue are blocking. Due reading
  10. * status text from standard input, a select()-driven main loop has been
  11. * implemented which selects for reads on the X connection and STDIN_FILENO to
  12. * handle all data smoothly. The event handlers of dwm are organized in an
  13. * array which is accessed whenever a new event has been fetched. This allows
  14. * event dispatching in O(1) time.
  15. *
  16. * Each child of the root window is called a client, except windows which have
  17. * set the override_redirect flag. Clients are organized in a global
  18. * doubly-linked client list, the focus history is remembered through a global
  19. * stack list. Each client contains a bit array to indicate the tags of a
  20. * client.
  21. *
  22. * Keys and tagging rules are organized as arrays and defined in config.h.
  23. *
  24. * To understand everything else, start reading main().
  25. */
  26. #include <errno.h>
  27. #include <stdarg.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <unistd.h>
  32. #include <sys/select.h>
  33. #include <sys/types.h>
  34. #include <sys/wait.h>
  35. #include <X11/cursorfont.h>
  36. #include <X11/keysym.h>
  37. #include <X11/Xatom.h>
  38. #include <X11/Xlib.h>
  39. #include <X11/Xproto.h>
  40. #include <X11/Xutil.h>
  41. #ifdef XINERAMA
  42. #include <X11/extensions/Xinerama.h>
  43. #endif
  44. /* macros */
  45. #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
  46. #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask))
  47. #define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH))
  48. #define ISVISIBLE(x) (x->tags & tagset[seltags])
  49. #define LENGTH(x) (sizeof x / sizeof x[0])
  50. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  51. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  52. #define MAXTAGLEN 16
  53. #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
  54. #define TAGMASK ((int)((1LL << LENGTH(tags)) - 1))
  55. #define TEXTW(x) (textnw(x, strlen(x)) + dc.font.height)
  56. /* enums */
  57. enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
  58. enum { ColBorder, ColFG, ColBG, ColLast }; /* color */
  59. enum { NetSupported, NetWMName, NetLast }; /* EWMH atoms */
  60. enum { WMProtocols, WMDelete, WMName, WMState, WMLast };/* default atoms */
  61. enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
  62. ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
  63. typedef union {
  64. int i;
  65. unsigned int ui;
  66. float f;
  67. void *v;
  68. } Arg;
  69. typedef struct {
  70. unsigned int click;
  71. unsigned int mask;
  72. unsigned int button;
  73. void (*func)(const Arg *arg);
  74. const Arg arg;
  75. } Button;
  76. typedef struct Client Client;
  77. struct Client {
  78. char name[256];
  79. float mina, maxa;
  80. int x, y, w, h;
  81. int basew, baseh, incw, inch, maxw, maxh, minw, minh;
  82. int bw, oldbw;
  83. unsigned int tags;
  84. Bool isfixed, isfloating, isurgent;
  85. Client *next;
  86. Client *snext;
  87. Window win;
  88. };
  89. typedef struct {
  90. int x, y, w, h;
  91. unsigned long norm[ColLast];
  92. unsigned long sel[ColLast];
  93. Drawable drawable;
  94. GC gc;
  95. struct {
  96. int ascent;
  97. int descent;
  98. int height;
  99. XFontSet set;
  100. XFontStruct *xfont;
  101. } font;
  102. } DC; /* draw context */
  103. typedef struct {
  104. unsigned int mod;
  105. KeySym keysym;
  106. void (*func)(const Arg *);
  107. const Arg arg;
  108. } Key;
  109. typedef struct {
  110. const char *symbol;
  111. void (*arrange)(void);
  112. } Layout;
  113. typedef struct {
  114. const char *class;
  115. const char *instance;
  116. const char *title;
  117. unsigned int tags;
  118. Bool isfloating;
  119. } Rule;
  120. /* function declarations */
  121. static void applyrules(Client *c);
  122. static void arrange(void);
  123. static void attach(Client *c);
  124. static void attachstack(Client *c);
  125. static void buttonpress(XEvent *e);
  126. static void checkotherwm(void);
  127. static void cleanup(void);
  128. static void clearurgent(void);
  129. static void configure(Client *c);
  130. static void configurenotify(XEvent *e);
  131. static void configurerequest(XEvent *e);
  132. static void destroynotify(XEvent *e);
  133. static void detach(Client *c);
  134. static void detachstack(Client *c);
  135. static void die(const char *errstr, ...);
  136. static void drawbar(void);
  137. static void drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]);
  138. static void drawtext(const char *text, unsigned long col[ColLast], Bool invert);
  139. static void enternotify(XEvent *e);
  140. static void expose(XEvent *e);
  141. static void focus(Client *c);
  142. static void focusin(XEvent *e);
  143. static void focusstack(const Arg *arg);
  144. static Client *getclient(Window w);
  145. static unsigned long getcolor(const char *colstr);
  146. static long getstate(Window w);
  147. static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
  148. static void grabbuttons(Client *c, Bool focused);
  149. static void grabkeys(void);
  150. static void initfont(const char *fontstr);
  151. static Bool isprotodel(Client *c);
  152. static void keypress(XEvent *e);
  153. static void killclient(const Arg *arg);
  154. static void manage(Window w, XWindowAttributes *wa);
  155. static void mappingnotify(XEvent *e);
  156. static void maprequest(XEvent *e);
  157. static void monocle(void);
  158. static void movemouse(const Arg *arg);
  159. static Client *nexttiled(Client *c);
  160. static void propertynotify(XEvent *e);
  161. static void quit(const Arg *arg);
  162. static void resize(Client *c, int x, int y, int w, int h, Bool sizehints);
  163. static void resizemouse(const Arg *arg);
  164. static void restack(void);
  165. static void run(void);
  166. static void scan(void);
  167. static void setclientstate(Client *c, long state);
  168. static void setlayout(const Arg *arg);
  169. static void setmfact(const Arg *arg);
  170. static void setup(void);
  171. static void spawn(const Arg *arg);
  172. static void tag(const Arg *arg);
  173. static int textnw(const char *text, unsigned int len);
  174. static void tile(void);
  175. static void togglebar(const Arg *arg);
  176. static void togglefloating(const Arg *arg);
  177. static void toggletag(const Arg *arg);
  178. static void toggleview(const Arg *arg);
  179. static void unmanage(Client *c);
  180. static void unmapnotify(XEvent *e);
  181. static void updatebar(void);
  182. static void updategeom(void);
  183. static void updatesizehints(Client *c);
  184. static void updatetitle(Client *c);
  185. static void updatewmhints(Client *c);
  186. static void view(const Arg *arg);
  187. static int xerror(Display *dpy, XErrorEvent *ee);
  188. static int xerrordummy(Display *dpy, XErrorEvent *ee);
  189. static int xerrorstart(Display *dpy, XErrorEvent *ee);
  190. static void zoom(const Arg *arg);
  191. /* variables */
  192. static char stext[256];
  193. static int screen;
  194. static int sx, sy, sw, sh; /* X display screen geometry x, y, width, height */
  195. static int by, bh, blw; /* bar geometry y, height and layout symbol width */
  196. static int wx, wy, ww, wh; /* window area geometry x, y, width, height, bar excluded */
  197. static unsigned int seltags = 0, sellt = 0;
  198. static int (*xerrorxlib)(Display *, XErrorEvent *);
  199. static unsigned int numlockmask = 0;
  200. static void (*handler[LASTEvent]) (XEvent *) = {
  201. [ButtonPress] = buttonpress,
  202. [ConfigureRequest] = configurerequest,
  203. [ConfigureNotify] = configurenotify,
  204. [DestroyNotify] = destroynotify,
  205. [EnterNotify] = enternotify,
  206. [Expose] = expose,
  207. [FocusIn] = focusin,
  208. [KeyPress] = keypress,
  209. [MappingNotify] = mappingnotify,
  210. [MapRequest] = maprequest,
  211. [PropertyNotify] = propertynotify,
  212. [UnmapNotify] = unmapnotify
  213. };
  214. static Atom wmatom[WMLast], netatom[NetLast];
  215. static Bool otherwm;
  216. static Bool running = True;
  217. static unsigned int tagset[] = {1, 1}; /* after start, first tag is selected */
  218. static Client *clients = NULL;
  219. static Client *sel = NULL;
  220. static Client *stack = NULL;
  221. static Cursor cursor[CurLast];
  222. static Display *dpy;
  223. static DC dc = {0};
  224. static Layout *lt[] = { NULL, NULL };
  225. static Window root, barwin;
  226. /* configuration, allows nested code to access above variables */
  227. #include "config.h"
  228. /* compile-time check if all tags fit into an unsigned int bit array. */
  229. struct NumTags { char limitexceeded[sizeof(unsigned int) * 8 < LENGTH(tags) ? -1 : 1]; };
  230. /* function implementations */
  231. void
  232. applyrules(Client *c) {
  233. unsigned int i;
  234. Rule *r;
  235. XClassHint ch = { 0 };
  236. /* rule matching */
  237. XGetClassHint(dpy, c->win, &ch);
  238. for(i = 0; i < LENGTH(rules); i++) {
  239. r = &rules[i];
  240. if((!r->title || strstr(c->name, r->title))
  241. && (!r->class || (ch.res_class && strstr(ch.res_class, r->class)))
  242. && (!r->instance || (ch.res_name && strstr(ch.res_name, r->instance)))) {
  243. c->isfloating = r->isfloating;
  244. c->tags |= r->tags & TAGMASK;
  245. }
  246. }
  247. if(ch.res_class)
  248. XFree(ch.res_class);
  249. if(ch.res_name)
  250. XFree(ch.res_name);
  251. if(!c->tags)
  252. c->tags = tagset[seltags];
  253. }
  254. void
  255. arrange(void) {
  256. Client *c;
  257. for(c = clients; c; c = c->next)
  258. if(ISVISIBLE(c)) {
  259. XMoveWindow(dpy, c->win, c->x, c->y);
  260. if(!lt[sellt]->arrange || c->isfloating)
  261. resize(c, c->x, c->y, c->w, c->h, True);
  262. }
  263. else {
  264. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  265. }
  266. focus(NULL);
  267. if(lt[sellt]->arrange)
  268. lt[sellt]->arrange();
  269. restack();
  270. }
  271. void
  272. attach(Client *c) {
  273. c->next = clients;
  274. clients = c;
  275. }
  276. void
  277. attachstack(Client *c) {
  278. c->snext = stack;
  279. stack = c;
  280. }
  281. void
  282. buttonpress(XEvent *e) {
  283. unsigned int i, x, click;
  284. Arg arg = {0};
  285. Client *c;
  286. XButtonPressedEvent *ev = &e->xbutton;
  287. click = ClkRootWin;
  288. if(ev->window == barwin) {
  289. i = x = 0;
  290. do x += TEXTW(tags[i]); while(ev->x >= x && ++i < LENGTH(tags));
  291. if(i < LENGTH(tags)) {
  292. click = ClkTagBar;
  293. arg.ui = 1 << i;
  294. }
  295. else if(ev->x < x + blw)
  296. click = ClkLtSymbol;
  297. else if(ev->x > wx + ww - TEXTW(stext))
  298. click = ClkStatusText;
  299. else
  300. click = ClkWinTitle;
  301. }
  302. else if((c = getclient(ev->window))) {
  303. focus(c);
  304. click = ClkClientWin;
  305. }
  306. for(i = 0; i < LENGTH(buttons); i++)
  307. if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
  308. && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
  309. buttons[i].func(click == ClkTagBar ? &arg : &buttons[i].arg);
  310. }
  311. void
  312. checkotherwm(void) {
  313. otherwm = False;
  314. XSetErrorHandler(xerrorstart);
  315. /* this causes an error if some other window manager is running */
  316. XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
  317. XSync(dpy, False);
  318. if(otherwm)
  319. die("dwm: another window manager is already running\n");
  320. XSetErrorHandler(NULL);
  321. xerrorxlib = XSetErrorHandler(xerror);
  322. XSync(dpy, False);
  323. }
  324. void
  325. cleanup(void) {
  326. Arg a = {.ui = ~0};
  327. Layout foo = { "", NULL };
  328. close(STDIN_FILENO);
  329. view(&a);
  330. lt[sellt] = &foo;
  331. while(stack)
  332. unmanage(stack);
  333. if(dc.font.set)
  334. XFreeFontSet(dpy, dc.font.set);
  335. else
  336. XFreeFont(dpy, dc.font.xfont);
  337. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  338. XFreePixmap(dpy, dc.drawable);
  339. XFreeGC(dpy, dc.gc);
  340. XFreeCursor(dpy, cursor[CurNormal]);
  341. XFreeCursor(dpy, cursor[CurResize]);
  342. XFreeCursor(dpy, cursor[CurMove]);
  343. XDestroyWindow(dpy, barwin);
  344. XSync(dpy, False);
  345. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  346. }
  347. void
  348. clearurgent(void) {
  349. XWMHints *wmh;
  350. Client *c;
  351. for(c = clients; c; c = c->next)
  352. if(ISVISIBLE(c) && c->isurgent) {
  353. c->isurgent = False;
  354. if (!(wmh = XGetWMHints(dpy, c->win)))
  355. continue;
  356. wmh->flags &= ~XUrgencyHint;
  357. XSetWMHints(dpy, c->win, wmh);
  358. XFree(wmh);
  359. }
  360. }
  361. void
  362. configure(Client *c) {
  363. XConfigureEvent ce;
  364. ce.type = ConfigureNotify;
  365. ce.display = dpy;
  366. ce.event = c->win;
  367. ce.window = c->win;
  368. ce.x = c->x;
  369. ce.y = c->y;
  370. ce.width = c->w;
  371. ce.height = c->h;
  372. ce.border_width = c->bw;
  373. ce.above = None;
  374. ce.override_redirect = False;
  375. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
  376. }
  377. void
  378. configurenotify(XEvent *e) {
  379. XConfigureEvent *ev = &e->xconfigure;
  380. if(ev->window == root && (ev->width != sw || ev->height != sh)) {
  381. sw = ev->width;
  382. sh = ev->height;
  383. updategeom();
  384. updatebar();
  385. arrange();
  386. }
  387. }
  388. void
  389. configurerequest(XEvent *e) {
  390. Client *c;
  391. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  392. XWindowChanges wc;
  393. if((c = getclient(ev->window))) {
  394. if(ev->value_mask & CWBorderWidth)
  395. c->bw = ev->border_width;
  396. else if(c->isfloating || !lt[sellt]->arrange) {
  397. if(ev->value_mask & CWX)
  398. c->x = sx + ev->x;
  399. if(ev->value_mask & CWY)
  400. c->y = sy + ev->y;
  401. if(ev->value_mask & CWWidth)
  402. c->w = ev->width;
  403. if(ev->value_mask & CWHeight)
  404. c->h = ev->height;
  405. if((c->x - sx + c->w) > sw && c->isfloating)
  406. c->x = sx + (sw / 2 - c->w / 2); /* center in x direction */
  407. if((c->y - sy + c->h) > sh && c->isfloating)
  408. c->y = sy + (sh / 2 - c->h / 2); /* center in y direction */
  409. if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
  410. configure(c);
  411. if(ISVISIBLE(c))
  412. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  413. }
  414. else
  415. configure(c);
  416. }
  417. else {
  418. wc.x = ev->x;
  419. wc.y = ev->y;
  420. wc.width = ev->width;
  421. wc.height = ev->height;
  422. wc.border_width = ev->border_width;
  423. wc.sibling = ev->above;
  424. wc.stack_mode = ev->detail;
  425. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  426. }
  427. XSync(dpy, False);
  428. }
  429. void
  430. destroynotify(XEvent *e) {
  431. Client *c;
  432. XDestroyWindowEvent *ev = &e->xdestroywindow;
  433. if((c = getclient(ev->window)))
  434. unmanage(c);
  435. }
  436. void
  437. detach(Client *c) {
  438. Client **tc;
  439. for(tc = &clients; *tc && *tc != c; tc = &(*tc)->next);
  440. *tc = c->next;
  441. }
  442. void
  443. detachstack(Client *c) {
  444. Client **tc;
  445. for(tc = &stack; *tc && *tc != c; tc = &(*tc)->snext);
  446. *tc = c->snext;
  447. }
  448. void
  449. die(const char *errstr, ...) {
  450. va_list ap;
  451. va_start(ap, errstr);
  452. vfprintf(stderr, errstr, ap);
  453. va_end(ap);
  454. exit(EXIT_FAILURE);
  455. }
  456. void
  457. drawbar(void) {
  458. int x;
  459. unsigned int i, occ = 0, urg = 0;
  460. unsigned long *col;
  461. Client *c;
  462. for(c = clients; c; c = c->next) {
  463. occ |= c->tags;
  464. if(c->isurgent)
  465. urg |= c->tags;
  466. }
  467. dc.x = 0;
  468. for(i = 0; i < LENGTH(tags); i++) {
  469. dc.w = TEXTW(tags[i]);
  470. col = tagset[seltags] & 1 << i ? dc.sel : dc.norm;
  471. drawtext(tags[i], col, urg & 1 << i);
  472. drawsquare(sel && sel->tags & 1 << i, occ & 1 << i, urg & 1 << i, col);
  473. dc.x += dc.w;
  474. }
  475. if(blw > 0) {
  476. dc.w = blw;
  477. drawtext(lt[sellt]->symbol, dc.norm, False);
  478. x = dc.x + dc.w;
  479. }
  480. else
  481. x = dc.x;
  482. dc.w = TEXTW(stext);
  483. dc.x = ww - dc.w;
  484. if(dc.x < x) {
  485. dc.x = x;
  486. dc.w = ww - x;
  487. }
  488. drawtext(stext, dc.norm, False);
  489. if((dc.w = dc.x - x) > bh) {
  490. dc.x = x;
  491. if(sel) {
  492. drawtext(sel->name, dc.sel, False);
  493. drawsquare(sel->isfixed, sel->isfloating, False, dc.sel);
  494. }
  495. else
  496. drawtext(NULL, dc.norm, False);
  497. }
  498. XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, ww, bh, 0, 0);
  499. XSync(dpy, False);
  500. }
  501. void
  502. drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) {
  503. int x;
  504. XGCValues gcv;
  505. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  506. gcv.foreground = col[invert ? ColBG : ColFG];
  507. XChangeGC(dpy, dc.gc, GCForeground, &gcv);
  508. x = (dc.font.ascent + dc.font.descent + 2) / 4;
  509. r.x = dc.x + 1;
  510. r.y = dc.y + 1;
  511. if(filled) {
  512. r.width = r.height = x + 1;
  513. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  514. }
  515. else if(empty) {
  516. r.width = r.height = x;
  517. XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  518. }
  519. }
  520. void
  521. drawtext(const char *text, unsigned long col[ColLast], Bool invert) {
  522. char buf[256];
  523. int i, x, y, h, len, olen;
  524. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  525. XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]);
  526. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  527. if(!text)
  528. return;
  529. olen = strlen(text);
  530. h = dc.font.ascent + dc.font.descent;
  531. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  532. x = dc.x + (h / 2);
  533. /* shorten text if necessary */
  534. for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
  535. if(!len)
  536. return;
  537. memcpy(buf, text, len);
  538. if(len < olen)
  539. for(i = len; i && i > len - 3; buf[--i] = '.');
  540. XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
  541. if(dc.font.set)
  542. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  543. else
  544. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  545. }
  546. void
  547. enternotify(XEvent *e) {
  548. Client *c;
  549. XCrossingEvent *ev = &e->xcrossing;
  550. if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
  551. return;
  552. if((c = getclient(ev->window)))
  553. focus(c);
  554. else
  555. focus(NULL);
  556. }
  557. void
  558. expose(XEvent *e) {
  559. XExposeEvent *ev = &e->xexpose;
  560. if(ev->count == 0 && (ev->window == barwin))
  561. drawbar();
  562. }
  563. void
  564. focus(Client *c) {
  565. if(!c || !ISVISIBLE(c))
  566. for(c = stack; c && !ISVISIBLE(c); c = c->snext);
  567. if(sel && sel != c) {
  568. grabbuttons(sel, False);
  569. XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
  570. }
  571. if(c) {
  572. detachstack(c);
  573. attachstack(c);
  574. grabbuttons(c, True);
  575. XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
  576. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  577. }
  578. else
  579. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  580. sel = c;
  581. drawbar();
  582. }
  583. void
  584. focusin(XEvent *e) { /* there are some broken focus acquiring clients */
  585. XFocusChangeEvent *ev = &e->xfocus;
  586. if(sel && ev->window != sel->win)
  587. XSetInputFocus(dpy, sel->win, RevertToPointerRoot, CurrentTime);
  588. }
  589. void
  590. focusstack(const Arg *arg) {
  591. Client *c = NULL, *i;
  592. if(!sel)
  593. return;
  594. if (arg->i > 0) {
  595. for(c = sel->next; c && !ISVISIBLE(c); c = c->next);
  596. if(!c)
  597. for(c = clients; c && !ISVISIBLE(c); c = c->next);
  598. }
  599. else {
  600. for(i = clients; i != sel; i = i->next)
  601. if(ISVISIBLE(i))
  602. c = i;
  603. if(!c)
  604. for(; i; i = i->next)
  605. if(ISVISIBLE(i))
  606. c = i;
  607. }
  608. if(c) {
  609. focus(c);
  610. restack();
  611. }
  612. }
  613. Client *
  614. getclient(Window w) {
  615. Client *c;
  616. for(c = clients; c && c->win != w; c = c->next);
  617. return c;
  618. }
  619. unsigned long
  620. getcolor(const char *colstr) {
  621. Colormap cmap = DefaultColormap(dpy, screen);
  622. XColor color;
  623. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  624. die("error, cannot allocate color '%s'\n", colstr);
  625. return color.pixel;
  626. }
  627. long
  628. getstate(Window w) {
  629. int format, status;
  630. long result = -1;
  631. unsigned char *p = NULL;
  632. unsigned long n, extra;
  633. Atom real;
  634. status = XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
  635. &real, &format, &n, &extra, (unsigned char **)&p);
  636. if(status != Success)
  637. return -1;
  638. if(n != 0)
  639. result = *p;
  640. XFree(p);
  641. return result;
  642. }
  643. Bool
  644. gettextprop(Window w, Atom atom, char *text, unsigned int size) {
  645. char **list = NULL;
  646. int n;
  647. XTextProperty name;
  648. if(!text || size == 0)
  649. return False;
  650. text[0] = '\0';
  651. XGetTextProperty(dpy, w, &name, atom);
  652. if(!name.nitems)
  653. return False;
  654. if(name.encoding == XA_STRING)
  655. strncpy(text, (char *)name.value, size - 1);
  656. else {
  657. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  658. && n > 0 && *list) {
  659. strncpy(text, *list, size - 1);
  660. XFreeStringList(list);
  661. }
  662. }
  663. text[size - 1] = '\0';
  664. XFree(name.value);
  665. return True;
  666. }
  667. void
  668. grabbuttons(Client *c, Bool focused) {
  669. unsigned int i, j;
  670. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  671. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  672. if(focused) {
  673. for(i = 0; i < LENGTH(buttons); i++)
  674. if(buttons[i].click == ClkClientWin)
  675. for(j = 0; j < LENGTH(modifiers); j++)
  676. XGrabButton(dpy, buttons[i].button, buttons[i].mask | modifiers[j], c->win, False, BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
  677. } else
  678. XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
  679. BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
  680. }
  681. void
  682. grabkeys(void) {
  683. unsigned int i, j;
  684. XModifierKeymap *modmap;
  685. /* update modifier map */
  686. modmap = XGetModifierMapping(dpy);
  687. for(i = 0; i < 8; i++)
  688. for(j = 0; j < modmap->max_keypermod; j++)
  689. if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
  690. numlockmask = (1 << i);
  691. XFreeModifiermap(modmap);
  692. { /* grab keys */
  693. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  694. KeyCode code;
  695. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  696. for(i = 0; i < LENGTH(keys); i++) {
  697. code = XKeysymToKeycode(dpy, keys[i].keysym);
  698. for(j = 0; j < LENGTH(modifiers); j++)
  699. XGrabKey(dpy, code, keys[i].mod | modifiers[j], root, True,
  700. GrabModeAsync, GrabModeAsync);
  701. }
  702. }
  703. }
  704. void
  705. initfont(const char *fontstr) {
  706. char *def, **missing;
  707. int i, n;
  708. missing = NULL;
  709. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  710. if(missing) {
  711. while(n--)
  712. fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
  713. XFreeStringList(missing);
  714. }
  715. if(dc.font.set) {
  716. XFontSetExtents *font_extents;
  717. XFontStruct **xfonts;
  718. char **font_names;
  719. dc.font.ascent = dc.font.descent = 0;
  720. font_extents = XExtentsOfFontSet(dc.font.set);
  721. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  722. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  723. dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
  724. dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent);
  725. xfonts++;
  726. }
  727. }
  728. else {
  729. if(dc.font.xfont)
  730. XFreeFont(dpy, dc.font.xfont);
  731. dc.font.xfont = NULL;
  732. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  733. && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  734. die("error, cannot load font: '%s'\n", fontstr);
  735. dc.font.ascent = dc.font.xfont->ascent;
  736. dc.font.descent = dc.font.xfont->descent;
  737. }
  738. dc.font.height = dc.font.ascent + dc.font.descent;
  739. }
  740. Bool
  741. isprotodel(Client *c) {
  742. int i, n;
  743. Atom *protocols;
  744. Bool ret = False;
  745. if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  746. for(i = 0; !ret && i < n; i++)
  747. if(protocols[i] == wmatom[WMDelete])
  748. ret = True;
  749. XFree(protocols);
  750. }
  751. return ret;
  752. }
  753. void
  754. keypress(XEvent *e) {
  755. unsigned int i;
  756. KeySym keysym;
  757. XKeyEvent *ev;
  758. ev = &e->xkey;
  759. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  760. for(i = 0; i < LENGTH(keys); i++)
  761. if(keysym == keys[i].keysym
  762. && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
  763. && keys[i].func)
  764. keys[i].func(&(keys[i].arg));
  765. }
  766. void
  767. killclient(const Arg *arg) {
  768. XEvent ev;
  769. if(!sel)
  770. return;
  771. if(isprotodel(sel)) {
  772. ev.type = ClientMessage;
  773. ev.xclient.window = sel->win;
  774. ev.xclient.message_type = wmatom[WMProtocols];
  775. ev.xclient.format = 32;
  776. ev.xclient.data.l[0] = wmatom[WMDelete];
  777. ev.xclient.data.l[1] = CurrentTime;
  778. XSendEvent(dpy, sel->win, False, NoEventMask, &ev);
  779. }
  780. else
  781. XKillClient(dpy, sel->win);
  782. }
  783. void
  784. manage(Window w, XWindowAttributes *wa) {
  785. Client *c, *t = NULL;
  786. Window trans = None;
  787. XWindowChanges wc;
  788. if(!(c = calloc(1, sizeof(Client))))
  789. die("fatal: could not calloc() %u bytes\n", sizeof(Client));
  790. c->win = w;
  791. /* geometry */
  792. c->x = wa->x;
  793. c->y = wa->y;
  794. c->w = wa->width;
  795. c->h = wa->height;
  796. c->oldbw = wa->border_width;
  797. if(c->w == sw && c->h == sh) {
  798. c->x = sx;
  799. c->y = sy;
  800. c->bw = 0;
  801. }
  802. else {
  803. if(c->x + c->w + 2 * c->bw > sx + sw)
  804. c->x = sx + sw - c->w - 2 * c->bw;
  805. if(c->y + c->h + 2 * c->bw > sy + sh)
  806. c->y = sy + sh - c->h - 2 * c->bw;
  807. c->x = MAX(c->x, sx);
  808. /* only fix client y-offset, if the client center might cover the bar */
  809. c->y = MAX(c->y, ((by == 0) && (c->x + (c->w / 2) >= wx) && (c->x + (c->w / 2) < wx + ww)) ? bh : sy);
  810. c->bw = borderpx;
  811. }
  812. wc.border_width = c->bw;
  813. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  814. XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
  815. configure(c); /* propagates border_width, if size doesn't change */
  816. updatesizehints(c);
  817. XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
  818. grabbuttons(c, False);
  819. updatetitle(c);
  820. if(XGetTransientForHint(dpy, w, &trans))
  821. t = getclient(trans);
  822. if(t)
  823. c->tags = t->tags;
  824. else
  825. applyrules(c);
  826. if(!c->isfloating)
  827. c->isfloating = trans != None || c->isfixed;
  828. if(c->isfloating)
  829. XRaiseWindow(dpy, c->win);
  830. attach(c);
  831. attachstack(c);
  832. XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
  833. XMapWindow(dpy, c->win);
  834. setclientstate(c, NormalState);
  835. arrange();
  836. }
  837. void
  838. mappingnotify(XEvent *e) {
  839. XMappingEvent *ev = &e->xmapping;
  840. XRefreshKeyboardMapping(ev);
  841. if(ev->request == MappingKeyboard)
  842. grabkeys();
  843. }
  844. void
  845. maprequest(XEvent *e) {
  846. static XWindowAttributes wa;
  847. XMapRequestEvent *ev = &e->xmaprequest;
  848. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  849. return;
  850. if(wa.override_redirect)
  851. return;
  852. if(!getclient(ev->window))
  853. manage(ev->window, &wa);
  854. }
  855. void
  856. monocle(void) {
  857. Client *c;
  858. for(c = nexttiled(clients); c; c = nexttiled(c->next))
  859. resize(c, wx, wy, ww - 2 * c->bw, wh - 2 * c->bw, resizehints);
  860. }
  861. void
  862. movemouse(const Arg *arg) {
  863. int x, y, ocx, ocy, di, nx, ny;
  864. unsigned int dui;
  865. Client *c;
  866. Window dummy;
  867. XEvent ev;
  868. if(!(c = sel))
  869. return;
  870. restack();
  871. ocx = c->x;
  872. ocy = c->y;
  873. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  874. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  875. return;
  876. XQueryPointer(dpy, root, &dummy, &dummy, &x, &y, &di, &di, &dui);
  877. do {
  878. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  879. switch (ev.type) {
  880. case ConfigureRequest:
  881. case Expose:
  882. case MapRequest:
  883. handler[ev.type](&ev);
  884. break;
  885. case MotionNotify:
  886. XSync(dpy, False);
  887. nx = ocx + (ev.xmotion.x - x);
  888. ny = ocy + (ev.xmotion.y - y);
  889. if(snap && nx >= wx && nx <= wx + ww
  890. && ny >= wy && ny <= wy + wh) {
  891. if(abs(wx - nx) < snap)
  892. nx = wx;
  893. else if(abs((wx + ww) - (nx + c->w + 2 * c->bw)) < snap)
  894. nx = wx + ww - c->w - 2 * c->bw;
  895. if(abs(wy - ny) < snap)
  896. ny = wy;
  897. else if(abs((wy + wh) - (ny + c->h + 2 * c->bw)) < snap)
  898. ny = wy + wh - c->h - 2 * c->bw;
  899. if(!c->isfloating && lt[sellt]->arrange && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
  900. togglefloating(NULL);
  901. }
  902. if(!lt[sellt]->arrange || c->isfloating)
  903. resize(c, nx, ny, c->w, c->h, False);
  904. break;
  905. }
  906. }
  907. while(ev.type != ButtonRelease);
  908. XUngrabPointer(dpy, CurrentTime);
  909. }
  910. Client *
  911. nexttiled(Client *c) {
  912. for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
  913. return c;
  914. }
  915. void
  916. propertynotify(XEvent *e) {
  917. Client *c;
  918. Window trans;
  919. XPropertyEvent *ev = &e->xproperty;
  920. if(ev->state == PropertyDelete)
  921. return; /* ignore */
  922. if((c = getclient(ev->window))) {
  923. switch (ev->atom) {
  924. default: break;
  925. case XA_WM_TRANSIENT_FOR:
  926. XGetTransientForHint(dpy, c->win, &trans);
  927. if(!c->isfloating && (c->isfloating = (getclient(trans) != NULL)))
  928. arrange();
  929. break;
  930. case XA_WM_NORMAL_HINTS:
  931. updatesizehints(c);
  932. break;
  933. case XA_WM_HINTS:
  934. updatewmhints(c);
  935. drawbar();
  936. break;
  937. }
  938. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  939. updatetitle(c);
  940. if(c == sel)
  941. drawbar();
  942. }
  943. }
  944. }
  945. void
  946. quit(const Arg *arg) {
  947. readin = running = False;
  948. }
  949. void
  950. resize(Client *c, int x, int y, int w, int h, Bool sizehints) {
  951. float a;
  952. XWindowChanges wc;
  953. if(sizehints) {
  954. /* see last two sentences in ICCCM 4.1.2.3 */
  955. Bool baseismin = c->basew == c->minw && c->baseh == c->minh;
  956. /* set minimum possible */
  957. w = MAX(1, w);
  958. h = MAX(1, h);
  959. if(!baseismin) { /* temporarily remove base dimensions */
  960. w -= c->basew;
  961. h -= c->baseh;
  962. }
  963. /* adjust for aspect limits */
  964. if(c->mina > 0 && c->maxa > 0) {
  965. a = (float) w/h;
  966. if(a > c->maxa)
  967. w = h * c->maxa;
  968. else if(a < c->mina)
  969. h = w / c->mina;
  970. }
  971. if(baseismin) { /* increment calculation requires this */
  972. w -= c->basew;
  973. h -= c->baseh;
  974. }
  975. /* adjust for increment value */
  976. if(c->incw)
  977. w -= w % c->incw;
  978. if(c->inch)
  979. h -= h % c->inch;
  980. /* restore base dimensions */
  981. w += c->basew;
  982. h += c->baseh;
  983. w = MAX(w, c->minw);
  984. h = MAX(h, c->minh);
  985. if(c->maxw)
  986. w = MIN(w, c->maxw);
  987. if(c->maxh)
  988. h = MIN(h, c->maxh);
  989. }
  990. if(w <= 0 || h <= 0)
  991. return;
  992. if(x > sx + sw)
  993. x = sw - w - 2 * c->bw;
  994. if(y > sy + sh)
  995. y = sh - h - 2 * c->bw;
  996. if(x + w + 2 * c->bw < sx)
  997. x = sx;
  998. if(y + h + 2 * c->bw < sy)
  999. y = sy;
  1000. if(h < bh)
  1001. h = bh;
  1002. if(w < bh)
  1003. w = bh;
  1004. if(c->x != x || c->y != y || c->w != w || c->h != h) {
  1005. c->x = wc.x = x;
  1006. c->y = wc.y = y;
  1007. c->w = wc.width = w;
  1008. c->h = wc.height = h;
  1009. wc.border_width = c->bw;
  1010. XConfigureWindow(dpy, c->win,
  1011. CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  1012. configure(c);
  1013. XSync(dpy, False);
  1014. }
  1015. }
  1016. void
  1017. resizemouse(const Arg *arg) {
  1018. int ocx, ocy;
  1019. int nw, nh;
  1020. Client *c;
  1021. XEvent ev;
  1022. if(!(c = sel))
  1023. return;
  1024. restack();
  1025. ocx = c->x;
  1026. ocy = c->y;
  1027. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1028. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  1029. return;
  1030. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1031. do {
  1032. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1033. switch(ev.type) {
  1034. case ConfigureRequest:
  1035. case Expose:
  1036. case MapRequest:
  1037. handler[ev.type](&ev);
  1038. break;
  1039. case MotionNotify:
  1040. XSync(dpy, False);
  1041. nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
  1042. nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
  1043. if(snap && nw >= wx && nw <= wx + ww
  1044. && nh >= wy && nh <= wy + wh) {
  1045. if(!c->isfloating && lt[sellt]->arrange
  1046. && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
  1047. togglefloating(NULL);
  1048. }
  1049. if(!lt[sellt]->arrange || c->isfloating)
  1050. resize(c, c->x, c->y, nw, nh, True);
  1051. break;
  1052. }
  1053. }
  1054. while(ev.type != ButtonRelease);
  1055. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1056. XUngrabPointer(dpy, CurrentTime);
  1057. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1058. }
  1059. void
  1060. restack(void) {
  1061. Client *c;
  1062. XEvent ev;
  1063. XWindowChanges wc;
  1064. drawbar();
  1065. if(!sel)
  1066. return;
  1067. if(sel->isfloating || !lt[sellt]->arrange)
  1068. XRaiseWindow(dpy, sel->win);
  1069. if(lt[sellt]->arrange) {
  1070. wc.stack_mode = Below;
  1071. wc.sibling = barwin;
  1072. for(c = stack; c; c = c->snext)
  1073. if(!c->isfloating && ISVISIBLE(c)) {
  1074. XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
  1075. wc.sibling = c->win;
  1076. }
  1077. }
  1078. XSync(dpy, False);
  1079. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1080. }
  1081. void
  1082. run(void) {
  1083. char *p;
  1084. char sbuf[sizeof stext];
  1085. fd_set rd;
  1086. int r, xfd;
  1087. unsigned int len, offset;
  1088. XEvent ev;
  1089. /* main event loop, also reads status text from stdin */
  1090. XSync(dpy, False);
  1091. xfd = ConnectionNumber(dpy);
  1092. offset = 0;
  1093. len = sizeof stext - 1;
  1094. sbuf[len] = stext[len] = '\0'; /* 0-terminator is never touched */
  1095. while(running) {
  1096. FD_ZERO(&rd);
  1097. if(readin)
  1098. FD_SET(STDIN_FILENO, &rd);
  1099. FD_SET(xfd, &rd);
  1100. if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
  1101. if(errno == EINTR)
  1102. continue;
  1103. die("select failed\n");
  1104. }
  1105. if(FD_ISSET(STDIN_FILENO, &rd)) {
  1106. switch((r = read(STDIN_FILENO, sbuf + offset, len - offset))) {
  1107. case -1:
  1108. strncpy(stext, strerror(errno), len);
  1109. readin = False;
  1110. break;
  1111. case 0:
  1112. strncpy(stext, "EOF", 4);
  1113. readin = False;
  1114. break;
  1115. default:
  1116. for(p = sbuf + offset; r > 0; p++, r--, offset++)
  1117. if(*p == '\n' || *p == '\0') {
  1118. *p = '\0';
  1119. strncpy(stext, sbuf, len);
  1120. p += r - 1; /* p is sbuf + offset + r - 1 */
  1121. for(r = 0; *(p - r) && *(p - r) != '\n'; r++);
  1122. offset = r;
  1123. if(r)
  1124. memmove(sbuf, p - r + 1, r);
  1125. break;
  1126. }
  1127. break;
  1128. }
  1129. drawbar();
  1130. }
  1131. while(XPending(dpy)) {
  1132. XNextEvent(dpy, &ev);
  1133. if(handler[ev.type])
  1134. (handler[ev.type])(&ev); /* call handler */
  1135. }
  1136. }
  1137. }
  1138. void
  1139. scan(void) {
  1140. unsigned int i, num;
  1141. Window d1, d2, *wins = NULL;
  1142. XWindowAttributes wa;
  1143. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1144. for(i = 0; i < num; i++) {
  1145. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  1146. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1147. continue;
  1148. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1149. manage(wins[i], &wa);
  1150. }
  1151. for(i = 0; i < num; i++) { /* now the transients */
  1152. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1153. continue;
  1154. if(XGetTransientForHint(dpy, wins[i], &d1)
  1155. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1156. manage(wins[i], &wa);
  1157. }
  1158. if(wins)
  1159. XFree(wins);
  1160. }
  1161. }
  1162. void
  1163. setclientstate(Client *c, long state) {
  1164. long data[] = {state, None};
  1165. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1166. PropModeReplace, (unsigned char *)data, 2);
  1167. }
  1168. void
  1169. setlayout(const Arg *arg) {
  1170. if(!arg || !arg->v || arg->v != lt[sellt])
  1171. sellt ^= 1;
  1172. if(arg && arg->v)
  1173. lt[sellt] = (Layout *)arg->v;
  1174. if(sel)
  1175. arrange();
  1176. else
  1177. drawbar();
  1178. }
  1179. /* arg > 1.0 will set mfact absolutly */
  1180. void
  1181. setmfact(const Arg *arg) {
  1182. float f;
  1183. if(!arg || !lt[sellt]->arrange)
  1184. return;
  1185. f = arg->f < 1.0 ? arg->f + mfact : arg->f - 1.0;
  1186. if(f < 0.1 || f > 0.9)
  1187. return;
  1188. mfact = f;
  1189. arrange();
  1190. }
  1191. void
  1192. setup(void) {
  1193. unsigned int i;
  1194. int w;
  1195. XSetWindowAttributes wa;
  1196. /* init screen */
  1197. screen = DefaultScreen(dpy);
  1198. root = RootWindow(dpy, screen);
  1199. initfont(font);
  1200. sx = 0;
  1201. sy = 0;
  1202. sw = DisplayWidth(dpy, screen);
  1203. sh = DisplayHeight(dpy, screen);
  1204. bh = dc.h = dc.font.height + 2;
  1205. lt[0] = &layouts[0];
  1206. lt[1] = &layouts[1 % LENGTH(layouts)];
  1207. updategeom();
  1208. /* init atoms */
  1209. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1210. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1211. wmatom[WMName] = XInternAtom(dpy, "WM_NAME", False);
  1212. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1213. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1214. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1215. /* init cursors */
  1216. wa.cursor = cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  1217. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  1218. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  1219. /* init appearance */
  1220. dc.norm[ColBorder] = getcolor(normbordercolor);
  1221. dc.norm[ColBG] = getcolor(normbgcolor);
  1222. dc.norm[ColFG] = getcolor(normfgcolor);
  1223. dc.sel[ColBorder] = getcolor(selbordercolor);
  1224. dc.sel[ColBG] = getcolor(selbgcolor);
  1225. dc.sel[ColFG] = getcolor(selfgcolor);
  1226. dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
  1227. dc.gc = XCreateGC(dpy, root, 0, 0);
  1228. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1229. if(!dc.font.set)
  1230. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  1231. /* init bar */
  1232. for(blw = i = 0; LENGTH(layouts) > 1 && i < LENGTH(layouts); i++) {
  1233. w = TEXTW(layouts[i].symbol);
  1234. blw = MAX(blw, w);
  1235. }
  1236. wa.override_redirect = 1;
  1237. wa.background_pixmap = ParentRelative;
  1238. wa.event_mask = ButtonPressMask|ExposureMask;
  1239. barwin = XCreateWindow(dpy, root, wx, by, ww, bh, 0, DefaultDepth(dpy, screen),
  1240. CopyFromParent, DefaultVisual(dpy, screen),
  1241. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  1242. XDefineCursor(dpy, barwin, cursor[CurNormal]);
  1243. XMapRaised(dpy, barwin);
  1244. strcpy(stext, "dwm-"VERSION);
  1245. drawbar();
  1246. /* EWMH support per view */
  1247. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1248. PropModeReplace, (unsigned char *) netatom, NetLast);
  1249. /* select for events */
  1250. wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask
  1251. |EnterWindowMask|LeaveWindowMask|StructureNotifyMask;
  1252. XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
  1253. XSelectInput(dpy, root, wa.event_mask);
  1254. grabkeys();
  1255. }
  1256. void
  1257. spawn(const Arg *arg) {
  1258. /* The double-fork construct avoids zombie processes and keeps the code
  1259. * clean from stupid signal handlers. */
  1260. if(fork() == 0) {
  1261. if(fork() == 0) {
  1262. if(dpy)
  1263. close(ConnectionNumber(dpy));
  1264. setsid();
  1265. execvp(((char **)arg->v)[0], (char **)arg->v);
  1266. fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
  1267. perror(" failed");
  1268. }
  1269. exit(0);
  1270. }
  1271. wait(0);
  1272. }
  1273. void
  1274. tag(const Arg *arg) {
  1275. if(sel && arg->ui & TAGMASK) {
  1276. sel->tags = arg->ui & TAGMASK;
  1277. arrange();
  1278. }
  1279. }
  1280. int
  1281. textnw(const char *text, unsigned int len) {
  1282. XRectangle r;
  1283. if(dc.font.set) {
  1284. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  1285. return r.width;
  1286. }
  1287. return XTextWidth(dc.font.xfont, text, len);
  1288. }
  1289. void
  1290. tile(void) {
  1291. int x, y, h, w, mw;
  1292. unsigned int i, n;
  1293. Client *c;
  1294. for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next), n++);
  1295. if(n == 0)
  1296. return;
  1297. /* master */
  1298. c = nexttiled(clients);
  1299. mw = mfact * ww;
  1300. resize(c, wx, wy, (n == 1 ? ww : mw) - 2 * c->bw, wh - 2 * c->bw, resizehints);
  1301. if(--n == 0)
  1302. return;
  1303. /* tile stack */
  1304. x = (wx + mw > c->x + c->w) ? c->x + c->w + 2 * c->bw : wx + mw;
  1305. y = wy;
  1306. w = (wx + mw > c->x + c->w) ? wx + ww - x : ww - mw;
  1307. h = wh / n;
  1308. if(h < bh)
  1309. h = wh;
  1310. for(i = 0, c = nexttiled(c->next); c; c = nexttiled(c->next), i++) {
  1311. resize(c, x, y, w - 2 * c->bw, /* remainder */ ((i + 1 == n)
  1312. ? (wy + wh) - y : h) - 2 * c->bw, resizehints);
  1313. if(h != wh)
  1314. y = c->y + c->h + 2 * c->bw;
  1315. }
  1316. }
  1317. void
  1318. togglebar(const Arg *arg) {
  1319. showbar = !showbar;
  1320. updategeom();
  1321. updatebar();
  1322. arrange();
  1323. }
  1324. void
  1325. togglefloating(const Arg *arg) {
  1326. if(!sel)
  1327. return;
  1328. sel->isfloating = !sel->isfloating || sel->isfixed;
  1329. if(sel->isfloating)
  1330. resize(sel, sel->x, sel->y, sel->w, sel->h, True);
  1331. arrange();
  1332. }
  1333. void
  1334. toggletag(const Arg *arg) {
  1335. unsigned int mask;
  1336. if (!sel)
  1337. return;
  1338. mask = sel->tags ^ (arg->ui & TAGMASK);
  1339. if(sel && mask) {
  1340. sel->tags = mask;
  1341. arrange();
  1342. }
  1343. }
  1344. void
  1345. toggleview(const Arg *arg) {
  1346. unsigned int mask = tagset[seltags] ^ (arg->ui & TAGMASK);
  1347. if(mask) {
  1348. tagset[seltags] = mask;
  1349. clearurgent();
  1350. arrange();
  1351. }
  1352. }
  1353. void
  1354. unmanage(Client *c) {
  1355. XWindowChanges wc;
  1356. wc.border_width = c->oldbw;
  1357. /* The server grab construct avoids race conditions. */
  1358. XGrabServer(dpy);
  1359. XSetErrorHandler(xerrordummy);
  1360. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1361. detach(c);
  1362. detachstack(c);
  1363. if(sel == c)
  1364. focus(NULL);
  1365. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1366. setclientstate(c, WithdrawnState);
  1367. free(c);
  1368. XSync(dpy, False);
  1369. XSetErrorHandler(xerror);
  1370. XUngrabServer(dpy);
  1371. arrange();
  1372. }
  1373. void
  1374. unmapnotify(XEvent *e) {
  1375. Client *c;
  1376. XUnmapEvent *ev = &e->xunmap;
  1377. if((c = getclient(ev->window)))
  1378. unmanage(c);
  1379. }
  1380. void
  1381. updatebar(void) {
  1382. if(dc.drawable != 0)
  1383. XFreePixmap(dpy, dc.drawable);
  1384. dc.drawable = XCreatePixmap(dpy, root, ww, bh, DefaultDepth(dpy, screen));
  1385. XMoveResizeWindow(dpy, barwin, wx, by, ww, bh);
  1386. }
  1387. void
  1388. updategeom(void) {
  1389. #ifdef XINERAMA
  1390. int n, i = 0;
  1391. XineramaScreenInfo *info = NULL;
  1392. /* window area geometry */
  1393. if(XineramaIsActive(dpy) && (info = XineramaQueryScreens(dpy, &n))) {
  1394. if(n > 1) {
  1395. int di, x, y;
  1396. unsigned int dui;
  1397. Window dummy;
  1398. if(XQueryPointer(dpy, root, &dummy, &dummy, &x, &y, &di, &di, &dui))
  1399. for(i = 0; i < n; i++)
  1400. if(INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height))
  1401. break;
  1402. }
  1403. wx = info[i].x_org;
  1404. wy = showbar && topbar ? info[i].y_org + bh : info[i].y_org;
  1405. ww = info[i].width;
  1406. wh = showbar ? info[i].height - bh : info[i].height;
  1407. XFree(info);
  1408. }
  1409. else
  1410. #endif
  1411. {
  1412. wx = sx;
  1413. wy = showbar && topbar ? sy + bh : sy;
  1414. ww = sw;
  1415. wh = showbar ? sh - bh : sh;
  1416. }
  1417. /* bar position */
  1418. by = showbar ? (topbar ? wy - bh : wy + wh) : -bh;
  1419. }
  1420. void
  1421. updatesizehints(Client *c) {
  1422. long msize;
  1423. XSizeHints size;
  1424. XGetWMNormalHints(dpy, c->win, &size, &msize);
  1425. if(size.flags & PBaseSize) {
  1426. c->basew = size.base_width;
  1427. c->baseh = size.base_height;
  1428. }
  1429. else if(size.flags & PMinSize) {
  1430. c->basew = size.min_width;
  1431. c->baseh = size.min_height;
  1432. }
  1433. else
  1434. c->basew = c->baseh = 0;
  1435. if(size.flags & PResizeInc) {
  1436. c->incw = size.width_inc;
  1437. c->inch = size.height_inc;
  1438. }
  1439. else
  1440. c->incw = c->inch = 0;
  1441. if(size.flags & PMaxSize) {
  1442. c->maxw = size.max_width;
  1443. c->maxh = size.max_height;
  1444. }
  1445. else
  1446. c->maxw = c->maxh = 0;
  1447. if(size.flags & PMinSize) {
  1448. c->minw = size.min_width;
  1449. c->minh = size.min_height;
  1450. }
  1451. else if(size.flags & PBaseSize) {
  1452. c->minw = size.base_width;
  1453. c->minh = size.base_height;
  1454. }
  1455. else
  1456. c->minw = c->minh = 0;
  1457. if(size.flags & PAspect) {
  1458. c->mina = (float)size.min_aspect.y / (float)size.min_aspect.x;
  1459. c->maxa = (float)size.max_aspect.x / (float)size.max_aspect.y;
  1460. }
  1461. else
  1462. c->maxa = c->mina = 0.0;
  1463. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1464. && c->maxw == c->minw && c->maxh == c->minh);
  1465. }
  1466. void
  1467. updatetitle(Client *c) {
  1468. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1469. gettextprop(c->win, wmatom[WMName], c->name, sizeof c->name);
  1470. }
  1471. void
  1472. updatewmhints(Client *c) {
  1473. XWMHints *wmh;
  1474. if((wmh = XGetWMHints(dpy, c->win))) {
  1475. if(ISVISIBLE(c) && wmh->flags & XUrgencyHint) {
  1476. wmh->flags &= ~XUrgencyHint;
  1477. XSetWMHints(dpy, c->win, wmh);
  1478. }
  1479. else
  1480. c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
  1481. XFree(wmh);
  1482. }
  1483. }
  1484. void
  1485. view(const Arg *arg) {
  1486. if((arg->ui & TAGMASK) == tagset[seltags])
  1487. return;
  1488. seltags ^= 1; /* toggle sel tagset */
  1489. if(arg->ui & TAGMASK)
  1490. tagset[seltags] = arg->ui & TAGMASK;
  1491. clearurgent();
  1492. arrange();
  1493. }
  1494. /* There's no way to check accesses to destroyed windows, thus those cases are
  1495. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1496. * default error handler, which may call exit. */
  1497. int
  1498. xerror(Display *dpy, XErrorEvent *ee) {
  1499. if(ee->error_code == BadWindow
  1500. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1501. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1502. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1503. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1504. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1505. || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
  1506. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1507. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1508. return 0;
  1509. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1510. ee->request_code, ee->error_code);
  1511. return xerrorxlib(dpy, ee); /* may call exit */
  1512. }
  1513. int
  1514. xerrordummy(Display *dpy, XErrorEvent *ee) {
  1515. return 0;
  1516. }
  1517. /* Startup Error handler to check if another window manager
  1518. * is already running. */
  1519. int
  1520. xerrorstart(Display *dpy, XErrorEvent *ee) {
  1521. otherwm = True;
  1522. return -1;
  1523. }
  1524. void
  1525. zoom(const Arg *arg) {
  1526. Client *c = sel;
  1527. if(!lt[sellt]->arrange || lt[sellt]->arrange == monocle || (sel && sel->isfloating))
  1528. return;
  1529. if(c == nexttiled(clients))
  1530. if(!c || !(c = nexttiled(c->next)))
  1531. return;
  1532. detach(c);
  1533. attach(c);
  1534. focus(c);
  1535. arrange();
  1536. }
  1537. int
  1538. main(int argc, char *argv[]) {
  1539. if(argc == 2 && !strcmp("-v", argv[1]))
  1540. die("dwm-"VERSION", © 2006-2008 dwm engineers, see LICENSE for details\n");
  1541. else if(argc != 1)
  1542. die("usage: dwm [-v]\n");
  1543. if(!XSupportsLocale())
  1544. fprintf(stderr, "warning: no locale support\n");
  1545. if(!(dpy = XOpenDisplay(0)))
  1546. die("dwm: cannot open display\n");
  1547. checkotherwm();
  1548. setup();
  1549. scan();
  1550. run();
  1551. cleanup();
  1552. XCloseDisplay(dpy);
  1553. return 0;
  1554. }