dwm.c 42 KB

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