dwm.c 42 KB

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