dwm.c 42 KB

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