dwm.c 41 KB

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