dwm.c 42 KB

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