dwm.c 41 KB

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