dwm.c 42 KB

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