dwm.c 40 KB

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