dwm.c 40 KB

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