dwm.c 44 KB

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