dwm.c 41 KB

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