dwm.c 44 KB

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