dwm.c 41 KB

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