dwm.c 43 KB

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