dwm.c 44 KB

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