dwm.c 41 KB

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