dwm.c 44 KB

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