dwm.c 43 KB

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