dwm.c 41 KB

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