dwm.c 40 KB

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