dwm.c 42 KB

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