dwm.c 45 KB

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