dwm.c 43 KB

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