dwm.c 41 KB

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