dwm.c 41 KB

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