dwm.c 49 KB

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