dwm.c 42 KB

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