dwm.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728
  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 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. XModifierKeymap *modmap;
  686. /* update modifier map */
  687. modmap = XGetModifierMapping(dpy);
  688. for(i = 0; i < 8; i++)
  689. for(j = 0; j < modmap->max_keypermod; j++)
  690. if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
  691. numlockmask = (1 << i);
  692. XFreeModifiermap(modmap);
  693. { /* grab keys */
  694. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  695. KeyCode code;
  696. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  697. for(i = 0; i < LENGTH(keys); i++) {
  698. code = XKeysymToKeycode(dpy, keys[i].keysym);
  699. for(j = 0; j < LENGTH(modifiers); j++)
  700. XGrabKey(dpy, code, keys[i].mod | modifiers[j], root, True,
  701. GrabModeAsync, GrabModeAsync);
  702. }
  703. }
  704. }
  705. void
  706. initfont(const char *fontstr) {
  707. char *def, **missing;
  708. int i, n;
  709. missing = NULL;
  710. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  711. if(missing) {
  712. while(n--)
  713. fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
  714. XFreeStringList(missing);
  715. }
  716. if(dc.font.set) {
  717. XFontSetExtents *font_extents;
  718. XFontStruct **xfonts;
  719. char **font_names;
  720. dc.font.ascent = dc.font.descent = 0;
  721. font_extents = XExtentsOfFontSet(dc.font.set);
  722. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  723. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  724. dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
  725. dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent);
  726. xfonts++;
  727. }
  728. }
  729. else {
  730. if(dc.font.xfont)
  731. XFreeFont(dpy, dc.font.xfont);
  732. dc.font.xfont = NULL;
  733. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  734. && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  735. die("error, cannot load font: '%s'\n", fontstr);
  736. dc.font.ascent = dc.font.xfont->ascent;
  737. dc.font.descent = dc.font.xfont->descent;
  738. }
  739. dc.font.height = dc.font.ascent + dc.font.descent;
  740. }
  741. Bool
  742. isprotodel(Client *c) {
  743. int i, n;
  744. Atom *protocols;
  745. Bool ret = False;
  746. if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  747. for(i = 0; !ret && i < n; i++)
  748. if(protocols[i] == wmatom[WMDelete])
  749. ret = True;
  750. XFree(protocols);
  751. }
  752. return ret;
  753. }
  754. void
  755. keypress(XEvent *e) {
  756. unsigned int i;
  757. KeySym keysym;
  758. XKeyEvent *ev;
  759. ev = &e->xkey;
  760. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  761. for(i = 0; i < LENGTH(keys); i++)
  762. if(keysym == keys[i].keysym
  763. && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
  764. && keys[i].func)
  765. keys[i].func(&(keys[i].arg));
  766. }
  767. void
  768. killclient(const Arg *arg) {
  769. XEvent ev;
  770. if(!sel)
  771. return;
  772. if(isprotodel(sel)) {
  773. ev.type = ClientMessage;
  774. ev.xclient.window = sel->win;
  775. ev.xclient.message_type = wmatom[WMProtocols];
  776. ev.xclient.format = 32;
  777. ev.xclient.data.l[0] = wmatom[WMDelete];
  778. ev.xclient.data.l[1] = CurrentTime;
  779. XSendEvent(dpy, sel->win, False, NoEventMask, &ev);
  780. }
  781. else
  782. XKillClient(dpy, sel->win);
  783. }
  784. void
  785. manage(Window w, XWindowAttributes *wa) {
  786. Client *c, *t = NULL;
  787. Window trans = None;
  788. XWindowChanges wc;
  789. if(!(c = calloc(1, sizeof(Client))))
  790. die("fatal: could not calloc() %u bytes\n", sizeof(Client));
  791. c->win = w;
  792. /* geometry */
  793. c->x = wa->x;
  794. c->y = wa->y;
  795. c->w = wa->width;
  796. c->h = wa->height;
  797. c->oldbw = wa->border_width;
  798. if(c->w == sw && c->h == sh) {
  799. c->x = sx;
  800. c->y = sy;
  801. c->bw = 0;
  802. }
  803. else {
  804. if(c->x + c->w + 2 * c->bw > sx + sw)
  805. c->x = sx + sw - c->w - 2 * c->bw;
  806. if(c->y + c->h + 2 * c->bw > sy + sh)
  807. c->y = sy + sh - c->h - 2 * c->bw;
  808. c->x = MAX(c->x, sx);
  809. /* only fix client y-offset, if the client center might cover the bar */
  810. c->y = MAX(c->y, ((by == 0) && (c->x + (c->w / 2) >= wx) && (c->x + (c->w / 2) < wx + ww)) ? bh : sy);
  811. c->bw = borderpx;
  812. }
  813. wc.border_width = c->bw;
  814. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  815. XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
  816. configure(c); /* propagates border_width, if size doesn't change */
  817. updatesizehints(c);
  818. XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
  819. grabbuttons(c, False);
  820. updatetitle(c);
  821. if(XGetTransientForHint(dpy, w, &trans))
  822. t = getclient(trans);
  823. if(t)
  824. c->tags = t->tags;
  825. else
  826. applyrules(c);
  827. if(!c->isfloating)
  828. c->isfloating = trans != None || c->isfixed;
  829. if(c->isfloating)
  830. XRaiseWindow(dpy, c->win);
  831. attach(c);
  832. attachstack(c);
  833. XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
  834. XMapWindow(dpy, c->win);
  835. setclientstate(c, NormalState);
  836. arrange();
  837. }
  838. void
  839. mappingnotify(XEvent *e) {
  840. XMappingEvent *ev = &e->xmapping;
  841. XRefreshKeyboardMapping(ev);
  842. if(ev->request == MappingKeyboard)
  843. grabkeys();
  844. }
  845. void
  846. maprequest(XEvent *e) {
  847. static XWindowAttributes wa;
  848. XMapRequestEvent *ev = &e->xmaprequest;
  849. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  850. return;
  851. if(wa.override_redirect)
  852. return;
  853. if(!getclient(ev->window))
  854. manage(ev->window, &wa);
  855. }
  856. void
  857. monocle(void) {
  858. Client *c;
  859. for(c = nexttiled(clients); c; c = nexttiled(c->next))
  860. resize(c, wx, wy, ww - 2 * c->bw, wh - 2 * c->bw, resizehints);
  861. }
  862. void
  863. movemouse(const Arg *arg) {
  864. int x, y, ocx, ocy, di, nx, ny;
  865. unsigned int dui;
  866. Client *c;
  867. Window dummy;
  868. XEvent ev;
  869. if(!(c = sel))
  870. return;
  871. restack();
  872. ocx = c->x;
  873. ocy = c->y;
  874. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  875. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  876. return;
  877. XQueryPointer(dpy, root, &dummy, &dummy, &x, &y, &di, &di, &dui);
  878. do {
  879. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  880. switch (ev.type) {
  881. case ConfigureRequest:
  882. case Expose:
  883. case MapRequest:
  884. handler[ev.type](&ev);
  885. break;
  886. case MotionNotify:
  887. XSync(dpy, False);
  888. nx = ocx + (ev.xmotion.x - x);
  889. ny = ocy + (ev.xmotion.y - y);
  890. if(snap && nx >= wx && nx <= wx + ww
  891. && ny >= wy && ny <= wy + wh) {
  892. if(abs(wx - nx) < snap)
  893. nx = wx;
  894. else if(abs((wx + ww) - (nx + c->w + 2 * c->bw)) < snap)
  895. nx = wx + ww - c->w - 2 * c->bw;
  896. if(abs(wy - ny) < snap)
  897. ny = wy;
  898. else if(abs((wy + wh) - (ny + c->h + 2 * c->bw)) < snap)
  899. ny = wy + wh - c->h - 2 * c->bw;
  900. if(!c->isfloating && lt[sellt]->arrange && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
  901. togglefloating(NULL);
  902. }
  903. if(!lt[sellt]->arrange || c->isfloating)
  904. resize(c, nx, ny, c->w, c->h, False);
  905. break;
  906. }
  907. }
  908. while(ev.type != ButtonRelease);
  909. XUngrabPointer(dpy, CurrentTime);
  910. }
  911. Client *
  912. nexttiled(Client *c) {
  913. for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
  914. return c;
  915. }
  916. void
  917. propertynotify(XEvent *e) {
  918. Client *c;
  919. Window trans;
  920. XPropertyEvent *ev = &e->xproperty;
  921. if(ev->state == PropertyDelete)
  922. return; /* ignore */
  923. if((c = getclient(ev->window))) {
  924. switch (ev->atom) {
  925. default: break;
  926. case XA_WM_TRANSIENT_FOR:
  927. XGetTransientForHint(dpy, c->win, &trans);
  928. if(!c->isfloating && (c->isfloating = (getclient(trans) != NULL)))
  929. arrange();
  930. break;
  931. case XA_WM_NORMAL_HINTS:
  932. updatesizehints(c);
  933. break;
  934. case XA_WM_HINTS:
  935. updatewmhints(c);
  936. drawbar();
  937. break;
  938. }
  939. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  940. updatetitle(c);
  941. if(c == sel)
  942. drawbar();
  943. }
  944. }
  945. }
  946. void
  947. quit(const Arg *arg) {
  948. readin = running = False;
  949. }
  950. void
  951. resize(Client *c, int x, int y, int w, int h, Bool sizehints) {
  952. XWindowChanges wc;
  953. if(sizehints) {
  954. /* see last two sentences in ICCCM 4.1.2.3 */
  955. Bool baseismin = c->basew == c->minw && c->baseh == c->minh;
  956. /* set minimum possible */
  957. w = MAX(1, w);
  958. h = MAX(1, h);
  959. if(!baseismin) { /* temporarily remove base dimensions */
  960. w -= c->basew;
  961. h -= c->baseh;
  962. }
  963. /* adjust for aspect limits */
  964. if(c->mina > 0 && c->maxa > 0) {
  965. if(c->maxa < (float)(w / h))
  966. w = h * c->maxa;
  967. else if(c->mina < (float)(h / w))
  968. h = w * c->mina;
  969. }
  970. if(baseismin) { /* increment calculation requires this */
  971. w -= c->basew;
  972. h -= c->baseh;
  973. }
  974. /* adjust for increment value */
  975. if(c->incw)
  976. w -= w % c->incw;
  977. if(c->inch)
  978. h -= h % c->inch;
  979. /* restore base dimensions */
  980. w += c->basew;
  981. h += c->baseh;
  982. w = MAX(w, c->minw);
  983. h = MAX(h, c->minh);
  984. if(c->maxw)
  985. w = MIN(w, c->maxw);
  986. if(c->maxh)
  987. h = MIN(h, c->maxh);
  988. }
  989. if(w <= 0 || h <= 0)
  990. return;
  991. if(x > sx + sw)
  992. x = sw - w - 2 * c->bw;
  993. if(y > sy + sh)
  994. y = sh - h - 2 * c->bw;
  995. if(x + w + 2 * c->bw < sx)
  996. x = sx;
  997. if(y + h + 2 * c->bw < sy)
  998. y = sy;
  999. if(h < bh)
  1000. h = bh;
  1001. if(w < bh)
  1002. w = bh;
  1003. if(c->x != x || c->y != y || c->w != w || c->h != h) {
  1004. c->x = wc.x = x;
  1005. c->y = wc.y = y;
  1006. c->w = wc.width = w;
  1007. c->h = wc.height = h;
  1008. wc.border_width = c->bw;
  1009. XConfigureWindow(dpy, c->win,
  1010. CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  1011. configure(c);
  1012. XSync(dpy, False);
  1013. }
  1014. }
  1015. void
  1016. resizemouse(const Arg *arg) {
  1017. int ocx, ocy;
  1018. int nw, nh;
  1019. Client *c;
  1020. XEvent ev;
  1021. if(!(c = sel))
  1022. return;
  1023. restack();
  1024. ocx = c->x;
  1025. ocy = c->y;
  1026. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1027. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  1028. return;
  1029. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1030. do {
  1031. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1032. switch(ev.type) {
  1033. case ConfigureRequest:
  1034. case Expose:
  1035. case MapRequest:
  1036. handler[ev.type](&ev);
  1037. break;
  1038. case MotionNotify:
  1039. XSync(dpy, False);
  1040. nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
  1041. nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
  1042. if(snap && nw >= wx && nw <= wx + ww
  1043. && nh >= wy && nh <= wy + wh) {
  1044. if(!c->isfloating && lt[sellt]->arrange
  1045. && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
  1046. togglefloating(NULL);
  1047. }
  1048. if(!lt[sellt]->arrange || c->isfloating)
  1049. resize(c, c->x, c->y, nw, nh, True);
  1050. break;
  1051. }
  1052. }
  1053. while(ev.type != ButtonRelease);
  1054. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1055. XUngrabPointer(dpy, CurrentTime);
  1056. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1057. }
  1058. void
  1059. restack(void) {
  1060. Client *c;
  1061. XEvent ev;
  1062. XWindowChanges wc;
  1063. drawbar();
  1064. if(!sel)
  1065. return;
  1066. if(sel->isfloating || !lt[sellt]->arrange)
  1067. XRaiseWindow(dpy, sel->win);
  1068. if(lt[sellt]->arrange) {
  1069. wc.stack_mode = Below;
  1070. wc.sibling = barwin;
  1071. for(c = stack; c; c = c->snext)
  1072. if(!c->isfloating && ISVISIBLE(c)) {
  1073. XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
  1074. wc.sibling = c->win;
  1075. }
  1076. }
  1077. XSync(dpy, False);
  1078. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1079. }
  1080. void
  1081. run(void) {
  1082. char *p;
  1083. char sbuf[sizeof stext];
  1084. fd_set rd;
  1085. int r, xfd;
  1086. unsigned int len, offset;
  1087. XEvent ev;
  1088. /* main event loop, also reads status text from stdin */
  1089. XSync(dpy, False);
  1090. xfd = ConnectionNumber(dpy);
  1091. offset = 0;
  1092. len = sizeof stext - 1;
  1093. sbuf[len] = stext[len] = '\0'; /* 0-terminator is never touched */
  1094. while(running) {
  1095. FD_ZERO(&rd);
  1096. if(readin)
  1097. FD_SET(STDIN_FILENO, &rd);
  1098. FD_SET(xfd, &rd);
  1099. if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
  1100. if(errno == EINTR)
  1101. continue;
  1102. die("select failed\n");
  1103. }
  1104. if(FD_ISSET(STDIN_FILENO, &rd)) {
  1105. switch((r = read(STDIN_FILENO, sbuf + offset, len - offset))) {
  1106. case -1:
  1107. strncpy(stext, strerror(errno), len);
  1108. readin = False;
  1109. break;
  1110. case 0:
  1111. strncpy(stext, "EOF", 4);
  1112. readin = False;
  1113. break;
  1114. default:
  1115. for(p = sbuf + offset; r > 0; p++, r--, offset++)
  1116. if(*p == '\n' || *p == '\0') {
  1117. *p = '\0';
  1118. strncpy(stext, sbuf, len);
  1119. p += r - 1; /* p is sbuf + offset + r - 1 */
  1120. for(r = 0; *(p - r) && *(p - r) != '\n'; r++);
  1121. offset = r;
  1122. if(r)
  1123. memmove(sbuf, p - r + 1, r);
  1124. break;
  1125. }
  1126. break;
  1127. }
  1128. drawbar();
  1129. }
  1130. while(XPending(dpy)) {
  1131. XNextEvent(dpy, &ev);
  1132. if(handler[ev.type])
  1133. (handler[ev.type])(&ev); /* call handler */
  1134. }
  1135. }
  1136. }
  1137. void
  1138. scan(void) {
  1139. unsigned int i, num;
  1140. Window d1, d2, *wins = NULL;
  1141. XWindowAttributes wa;
  1142. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1143. for(i = 0; i < num; i++) {
  1144. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  1145. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1146. continue;
  1147. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1148. manage(wins[i], &wa);
  1149. }
  1150. for(i = 0; i < num; i++) { /* now the transients */
  1151. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1152. continue;
  1153. if(XGetTransientForHint(dpy, wins[i], &d1)
  1154. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1155. manage(wins[i], &wa);
  1156. }
  1157. if(wins)
  1158. XFree(wins);
  1159. }
  1160. }
  1161. void
  1162. setclientstate(Client *c, long state) {
  1163. long data[] = {state, None};
  1164. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1165. PropModeReplace, (unsigned char *)data, 2);
  1166. }
  1167. void
  1168. setlayout(const Arg *arg) {
  1169. if(!arg || !arg->v || arg->v != lt[sellt])
  1170. sellt ^= 1;
  1171. if(arg && arg->v)
  1172. lt[sellt] = (Layout *)arg->v;
  1173. if(sel)
  1174. arrange();
  1175. else
  1176. drawbar();
  1177. }
  1178. /* arg > 1.0 will set mfact absolutly */
  1179. void
  1180. setmfact(const Arg *arg) {
  1181. float f;
  1182. if(!arg || !lt[sellt]->arrange)
  1183. return;
  1184. f = arg->f < 1.0 ? arg->f + mfact : arg->f - 1.0;
  1185. if(f < 0.1 || f > 0.9)
  1186. return;
  1187. mfact = f;
  1188. arrange();
  1189. }
  1190. void
  1191. setup(void) {
  1192. unsigned int i;
  1193. int w;
  1194. XSetWindowAttributes wa;
  1195. /* init screen */
  1196. screen = DefaultScreen(dpy);
  1197. root = RootWindow(dpy, screen);
  1198. initfont(font);
  1199. sx = 0;
  1200. sy = 0;
  1201. sw = DisplayWidth(dpy, screen);
  1202. sh = DisplayHeight(dpy, screen);
  1203. bh = dc.h = dc.font.height + 2;
  1204. lt[0] = &layouts[0];
  1205. lt[1] = &layouts[1 % LENGTH(layouts)];
  1206. updategeom();
  1207. /* init atoms */
  1208. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1209. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1210. wmatom[WMName] = XInternAtom(dpy, "WM_NAME", False);
  1211. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1212. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1213. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1214. /* init cursors */
  1215. wa.cursor = cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  1216. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  1217. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  1218. /* init appearance */
  1219. dc.norm[ColBorder] = getcolor(normbordercolor);
  1220. dc.norm[ColBG] = getcolor(normbgcolor);
  1221. dc.norm[ColFG] = getcolor(normfgcolor);
  1222. dc.sel[ColBorder] = getcolor(selbordercolor);
  1223. dc.sel[ColBG] = getcolor(selbgcolor);
  1224. dc.sel[ColFG] = getcolor(selfgcolor);
  1225. dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
  1226. dc.gc = XCreateGC(dpy, root, 0, 0);
  1227. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1228. if(!dc.font.set)
  1229. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  1230. /* init bar */
  1231. for(blw = i = 0; LENGTH(layouts) > 1 && i < LENGTH(layouts); i++) {
  1232. w = TEXTW(layouts[i].symbol);
  1233. blw = MAX(blw, w);
  1234. }
  1235. wa.override_redirect = 1;
  1236. wa.background_pixmap = ParentRelative;
  1237. wa.event_mask = ButtonPressMask|ExposureMask;
  1238. barwin = XCreateWindow(dpy, root, wx, by, ww, bh, 0, DefaultDepth(dpy, screen),
  1239. CopyFromParent, DefaultVisual(dpy, screen),
  1240. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  1241. XDefineCursor(dpy, barwin, cursor[CurNormal]);
  1242. XMapRaised(dpy, barwin);
  1243. strcpy(stext, "dwm-"VERSION);
  1244. drawbar();
  1245. /* EWMH support per view */
  1246. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1247. PropModeReplace, (unsigned char *) netatom, NetLast);
  1248. /* select for events */
  1249. wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask
  1250. |EnterWindowMask|LeaveWindowMask|StructureNotifyMask;
  1251. XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
  1252. XSelectInput(dpy, root, wa.event_mask);
  1253. grabkeys();
  1254. }
  1255. void
  1256. spawn(const Arg *arg) {
  1257. /* The double-fork construct avoids zombie processes and keeps the code
  1258. * clean from stupid signal handlers. */
  1259. if(fork() == 0) {
  1260. if(fork() == 0) {
  1261. if(dpy)
  1262. close(ConnectionNumber(dpy));
  1263. setsid();
  1264. execvp(((char **)arg->v)[0], (char **)arg->v);
  1265. fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
  1266. perror(" failed");
  1267. }
  1268. exit(0);
  1269. }
  1270. wait(0);
  1271. }
  1272. void
  1273. tag(const Arg *arg) {
  1274. if(sel && arg->ui & TAGMASK) {
  1275. sel->tags = arg->ui & TAGMASK;
  1276. arrange();
  1277. }
  1278. }
  1279. int
  1280. textnw(const char *text, unsigned int len) {
  1281. XRectangle r;
  1282. if(dc.font.set) {
  1283. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  1284. return r.width;
  1285. }
  1286. return XTextWidth(dc.font.xfont, text, len);
  1287. }
  1288. void
  1289. tile(void) {
  1290. int x, y, h, w, mw;
  1291. unsigned int i, n;
  1292. Client *c;
  1293. for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next), n++);
  1294. if(n == 0)
  1295. return;
  1296. /* master */
  1297. c = nexttiled(clients);
  1298. mw = mfact * ww;
  1299. resize(c, wx, wy, (n == 1 ? ww : mw) - 2 * c->bw, wh - 2 * c->bw, resizehints);
  1300. if(--n == 0)
  1301. return;
  1302. /* tile stack */
  1303. x = (wx + mw > c->x + c->w) ? c->x + c->w + 2 * c->bw : wx + mw;
  1304. y = wy;
  1305. w = (wx + mw > c->x + c->w) ? wx + ww - x : ww - mw;
  1306. h = wh / n;
  1307. if(h < bh)
  1308. h = wh;
  1309. for(i = 0, c = nexttiled(c->next); c; c = nexttiled(c->next), i++) {
  1310. resize(c, x, y, w - 2 * c->bw, /* remainder */ ((i + 1 == n)
  1311. ? (wy + wh) - y : h) - 2 * c->bw, resizehints);
  1312. if(h != wh)
  1313. y = c->y + c->h + 2 * c->bw;
  1314. }
  1315. }
  1316. void
  1317. togglebar(const Arg *arg) {
  1318. showbar = !showbar;
  1319. updategeom();
  1320. updatebar();
  1321. arrange();
  1322. }
  1323. void
  1324. togglefloating(const Arg *arg) {
  1325. if(!sel)
  1326. return;
  1327. sel->isfloating = !sel->isfloating || sel->isfixed;
  1328. if(sel->isfloating)
  1329. resize(sel, sel->x, sel->y, sel->w, sel->h, True);
  1330. arrange();
  1331. }
  1332. void
  1333. toggletag(const Arg *arg) {
  1334. unsigned int mask;
  1335. if (!sel)
  1336. return;
  1337. mask = sel->tags ^ (arg->ui & TAGMASK);
  1338. if(sel && mask) {
  1339. sel->tags = mask;
  1340. arrange();
  1341. }
  1342. }
  1343. void
  1344. toggleview(const Arg *arg) {
  1345. unsigned int mask = tagset[seltags] ^ (arg->ui & TAGMASK);
  1346. if(mask) {
  1347. tagset[seltags] = mask;
  1348. clearurgent();
  1349. arrange();
  1350. }
  1351. }
  1352. void
  1353. unmanage(Client *c) {
  1354. XWindowChanges wc;
  1355. wc.border_width = c->oldbw;
  1356. /* The server grab construct avoids race conditions. */
  1357. XGrabServer(dpy);
  1358. XSetErrorHandler(xerrordummy);
  1359. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1360. detach(c);
  1361. detachstack(c);
  1362. if(sel == c)
  1363. focus(NULL);
  1364. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1365. setclientstate(c, WithdrawnState);
  1366. free(c);
  1367. XSync(dpy, False);
  1368. XSetErrorHandler(xerror);
  1369. XUngrabServer(dpy);
  1370. arrange();
  1371. }
  1372. void
  1373. unmapnotify(XEvent *e) {
  1374. Client *c;
  1375. XUnmapEvent *ev = &e->xunmap;
  1376. if((c = getclient(ev->window)))
  1377. unmanage(c);
  1378. }
  1379. void
  1380. updatebar(void) {
  1381. if(dc.drawable != 0)
  1382. XFreePixmap(dpy, dc.drawable);
  1383. dc.drawable = XCreatePixmap(dpy, root, ww, bh, DefaultDepth(dpy, screen));
  1384. XMoveResizeWindow(dpy, barwin, wx, by, ww, bh);
  1385. }
  1386. void
  1387. updategeom(void) {
  1388. #ifdef XINERAMA
  1389. int n, i = 0;
  1390. XineramaScreenInfo *info = NULL;
  1391. /* window area geometry */
  1392. if(XineramaIsActive(dpy) && (info = XineramaQueryScreens(dpy, &n))) {
  1393. if(n > 1) {
  1394. int di, x, y;
  1395. unsigned int dui;
  1396. Window dummy;
  1397. if(XQueryPointer(dpy, root, &dummy, &dummy, &x, &y, &di, &di, &dui))
  1398. for(i = 0; i < n; i++)
  1399. if(INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height))
  1400. break;
  1401. }
  1402. wx = info[i].x_org;
  1403. wy = showbar && topbar ? info[i].y_org + bh : info[i].y_org;
  1404. ww = info[i].width;
  1405. wh = showbar ? info[i].height - bh : info[i].height;
  1406. XFree(info);
  1407. }
  1408. else
  1409. #endif
  1410. {
  1411. wx = sx;
  1412. wy = showbar && topbar ? sy + bh : sy;
  1413. ww = sw;
  1414. wh = showbar ? sh - bh : sh;
  1415. }
  1416. /* bar position */
  1417. by = showbar ? (topbar ? wy - bh : wy + wh) : -bh;
  1418. }
  1419. void
  1420. updatesizehints(Client *c) {
  1421. long msize;
  1422. XSizeHints size;
  1423. XGetWMNormalHints(dpy, c->win, &size, &msize);
  1424. if(size.flags & PBaseSize) {
  1425. c->basew = size.base_width;
  1426. c->baseh = size.base_height;
  1427. }
  1428. else if(size.flags & PMinSize) {
  1429. c->basew = size.min_width;
  1430. c->baseh = size.min_height;
  1431. }
  1432. else
  1433. c->basew = c->baseh = 0;
  1434. if(size.flags & PResizeInc) {
  1435. c->incw = size.width_inc;
  1436. c->inch = size.height_inc;
  1437. }
  1438. else
  1439. c->incw = c->inch = 0;
  1440. if(size.flags & PMaxSize) {
  1441. c->maxw = size.max_width;
  1442. c->maxh = size.max_height;
  1443. }
  1444. else
  1445. c->maxw = c->maxh = 0;
  1446. if(size.flags & PMinSize) {
  1447. c->minw = size.min_width;
  1448. c->minh = size.min_height;
  1449. }
  1450. else if(size.flags & PBaseSize) {
  1451. c->minw = size.base_width;
  1452. c->minh = size.base_height;
  1453. }
  1454. else
  1455. c->minw = c->minh = 0;
  1456. if(size.flags & PAspect) {
  1457. c->mina = (float)size.min_aspect.y / (float)size.min_aspect.x;
  1458. c->maxa = (float)size.max_aspect.x / (float)size.max_aspect.y;
  1459. }
  1460. else
  1461. c->maxa = c->mina = 0.0;
  1462. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1463. && c->maxw == c->minw && c->maxh == c->minh);
  1464. }
  1465. void
  1466. updatetitle(Client *c) {
  1467. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1468. gettextprop(c->win, wmatom[WMName], c->name, sizeof c->name);
  1469. }
  1470. void
  1471. updatewmhints(Client *c) {
  1472. XWMHints *wmh;
  1473. if((wmh = XGetWMHints(dpy, c->win))) {
  1474. if(ISVISIBLE(c) && wmh->flags & XUrgencyHint) {
  1475. wmh->flags &= ~XUrgencyHint;
  1476. XSetWMHints(dpy, c->win, wmh);
  1477. }
  1478. else
  1479. c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
  1480. XFree(wmh);
  1481. }
  1482. }
  1483. void
  1484. view(const Arg *arg) {
  1485. if((arg->ui & TAGMASK) == tagset[seltags])
  1486. return;
  1487. seltags ^= 1; /* toggle sel tagset */
  1488. if(arg->ui & TAGMASK)
  1489. tagset[seltags] = arg->ui & TAGMASK;
  1490. clearurgent();
  1491. arrange();
  1492. }
  1493. /* There's no way to check accesses to destroyed windows, thus those cases are
  1494. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1495. * default error handler, which may call exit. */
  1496. int
  1497. xerror(Display *dpy, XErrorEvent *ee) {
  1498. if(ee->error_code == BadWindow
  1499. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1500. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1501. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1502. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1503. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1504. || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
  1505. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1506. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1507. return 0;
  1508. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1509. ee->request_code, ee->error_code);
  1510. return xerrorxlib(dpy, ee); /* may call exit */
  1511. }
  1512. int
  1513. xerrordummy(Display *dpy, XErrorEvent *ee) {
  1514. return 0;
  1515. }
  1516. /* Startup Error handler to check if another window manager
  1517. * is already running. */
  1518. int
  1519. xerrorstart(Display *dpy, XErrorEvent *ee) {
  1520. otherwm = True;
  1521. return -1;
  1522. }
  1523. void
  1524. zoom(const Arg *arg) {
  1525. Client *c = sel;
  1526. if(!lt[sellt]->arrange || lt[sellt]->arrange == monocle || (sel && sel->isfloating))
  1527. return;
  1528. if(c == nexttiled(clients))
  1529. if(!c || !(c = nexttiled(c->next)))
  1530. return;
  1531. detach(c);
  1532. attach(c);
  1533. focus(c);
  1534. arrange();
  1535. }
  1536. int
  1537. main(int argc, char *argv[]) {
  1538. if(argc == 2 && !strcmp("-v", argv[1]))
  1539. die("dwm-"VERSION", © 2006-2008 dwm engineers, see LICENSE for details\n");
  1540. else if(argc != 1)
  1541. die("usage: dwm [-v]\n");
  1542. if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  1543. fprintf(stderr, "warning: no locale support\n");
  1544. if(!(dpy = XOpenDisplay(0)))
  1545. die("dwm: cannot open display\n");
  1546. checkotherwm();
  1547. setup();
  1548. scan();
  1549. run();
  1550. cleanup();
  1551. XCloseDisplay(dpy);
  1552. return 0;
  1553. }