dwm.c 43 KB

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