dwm.c 42 KB

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