dwm.c 43 KB

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