dwm.c 40 KB

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