dwm.c 43 KB

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