dwm.c 41 KB

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