x.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094
  1. /* See LICENSE for license details. */
  2. #include <errno.h>
  3. #include <math.h>
  4. #include <limits.h>
  5. #include <locale.h>
  6. #include <signal.h>
  7. #include <sys/select.h>
  8. #include <time.h>
  9. #include <unistd.h>
  10. #include <libgen.h>
  11. #include <X11/Xatom.h>
  12. #include <X11/Xlib.h>
  13. #include <X11/cursorfont.h>
  14. #include <X11/keysym.h>
  15. #include <X11/Xft/Xft.h>
  16. #include <X11/XKBlib.h>
  17. #include <X11/Xresource.h>
  18. static char *argv0;
  19. #include "arg.h"
  20. #include "st.h"
  21. #include "win.h"
  22. /* types used in config.h */
  23. typedef struct {
  24. uint mod;
  25. KeySym keysym;
  26. void (*func)(const Arg *);
  27. const Arg arg;
  28. } Shortcut;
  29. typedef struct {
  30. uint b;
  31. uint mask;
  32. char *s;
  33. } MouseShortcut;
  34. typedef struct {
  35. KeySym k;
  36. uint mask;
  37. char *s;
  38. /* three-valued logic variables: 0 indifferent, 1 on, -1 off */
  39. signed char appkey; /* application keypad */
  40. signed char appcursor; /* application cursor */
  41. } Key;
  42. /* Xresources preferences */
  43. enum resource_type {
  44. STRING = 0,
  45. INTEGER = 1,
  46. FLOAT = 2
  47. };
  48. typedef struct {
  49. char *name;
  50. enum resource_type type;
  51. void *dst;
  52. } ResourcePref;
  53. /* X modifiers */
  54. #define XK_ANY_MOD UINT_MAX
  55. #define XK_NO_MOD 0
  56. #define XK_SWITCH_MOD (1<<13)
  57. /* function definitions used in config.h */
  58. static void clipcopy(const Arg *);
  59. static void clippaste(const Arg *);
  60. static void numlock(const Arg *);
  61. static void selpaste(const Arg *);
  62. static void zoom(const Arg *);
  63. static void zoomabs(const Arg *);
  64. static void zoomreset(const Arg *);
  65. /* config.h for applying patches and the configuration. */
  66. #include "config.h"
  67. /* XEMBED messages */
  68. #define XEMBED_FOCUS_IN 4
  69. #define XEMBED_FOCUS_OUT 5
  70. /* macros */
  71. #define IS_SET(flag) ((win.mode & (flag)) != 0)
  72. #define TRUERED(x) (((x) & 0xff0000) >> 8)
  73. #define TRUEGREEN(x) (((x) & 0xff00))
  74. #define TRUEBLUE(x) (((x) & 0xff) << 8)
  75. typedef XftDraw *Draw;
  76. typedef XftColor Color;
  77. typedef XftGlyphFontSpec GlyphFontSpec;
  78. /* Purely graphic info */
  79. typedef struct {
  80. int tw, th; /* tty width and height */
  81. int w, h; /* window width and height */
  82. int ch; /* char height */
  83. int cw; /* char width */
  84. int cyo; /* char y offset */
  85. int mode; /* window state/mode flags */
  86. int cursor; /* cursor style */
  87. } TermWindow;
  88. typedef struct {
  89. Display *dpy;
  90. Colormap cmap;
  91. Window win;
  92. Drawable buf;
  93. GlyphFontSpec *specbuf; /* font spec buffer used for rendering */
  94. Atom xembed, wmdeletewin, netwmname, netwmpid;
  95. XIM xim;
  96. XIC xic;
  97. Draw draw;
  98. Visual *vis;
  99. XSetWindowAttributes attrs;
  100. int scr;
  101. int isfixed; /* is fixed geometry? */
  102. int depth; /* bit depth */
  103. int l, t; /* left and top offset */
  104. int gm; /* geometry mask */
  105. } XWindow;
  106. typedef struct {
  107. Atom xtarget;
  108. char *primary, *clipboard;
  109. struct timespec tclick1;
  110. struct timespec tclick2;
  111. } XSelection;
  112. /* Font structure */
  113. #define Font Font_
  114. typedef struct {
  115. int height;
  116. int width;
  117. int ascent;
  118. int descent;
  119. int badslant;
  120. int badweight;
  121. short lbearing;
  122. short rbearing;
  123. XftFont *match;
  124. FcFontSet *set;
  125. FcPattern *pattern;
  126. } Font;
  127. /* Drawing Context */
  128. typedef struct {
  129. Color *col;
  130. size_t collen;
  131. Font font, bfont, ifont, ibfont;
  132. GC gc;
  133. } DC;
  134. static inline ushort sixd_to_16bit(int);
  135. static int xmakeglyphfontspecs(XftGlyphFontSpec *, const Glyph *, int, int, int);
  136. static void xdrawglyphfontspecs(const XftGlyphFontSpec *, Glyph, int, int, int);
  137. static void xdrawglyph(Glyph, int, int);
  138. static void xclear(int, int, int, int);
  139. static int xgeommasktogravity(int);
  140. static void xinit(int, int);
  141. static void cresize(int, int);
  142. static void xresize(int, int);
  143. static void xhints(void);
  144. static int xloadcolor(int, const char *, Color *);
  145. static int xloadfont(Font *, FcPattern *);
  146. static void xloadfonts(char *, double);
  147. static void xunloadfont(Font *);
  148. static void xunloadfonts(void);
  149. static void xsetenv(void);
  150. static void xseturgency(int);
  151. static int evcol(XEvent *);
  152. static int evrow(XEvent *);
  153. static void expose(XEvent *);
  154. static void visibility(XEvent *);
  155. static void unmap(XEvent *);
  156. static void kpress(XEvent *);
  157. static void cmessage(XEvent *);
  158. static void resize(XEvent *);
  159. static void focus(XEvent *);
  160. static void brelease(XEvent *);
  161. static void bpress(XEvent *);
  162. static void bmotion(XEvent *);
  163. static void propnotify(XEvent *);
  164. static void selnotify(XEvent *);
  165. static void selclear_(XEvent *);
  166. static void selrequest(XEvent *);
  167. static void setsel(char *, Time);
  168. static void mousesel(XEvent *, int);
  169. static void mousereport(XEvent *);
  170. static char *kmap(KeySym, uint);
  171. static int match(uint, uint);
  172. static void run(void);
  173. static void usage(void);
  174. static void (*handler[LASTEvent])(XEvent *) = {
  175. [KeyPress] = kpress,
  176. [ClientMessage] = cmessage,
  177. [ConfigureNotify] = resize,
  178. [VisibilityNotify] = visibility,
  179. [UnmapNotify] = unmap,
  180. [Expose] = expose,
  181. [FocusIn] = focus,
  182. [FocusOut] = focus,
  183. [MotionNotify] = bmotion,
  184. [ButtonPress] = bpress,
  185. [ButtonRelease] = brelease,
  186. /*
  187. * Uncomment if you want the selection to disappear when you select something
  188. * different in another window.
  189. */
  190. /* [SelectionClear] = selclear_, */
  191. [SelectionNotify] = selnotify,
  192. /*
  193. * PropertyNotify is only turned on when there is some INCR transfer happening
  194. * for the selection retrieval.
  195. */
  196. [PropertyNotify] = propnotify,
  197. [SelectionRequest] = selrequest,
  198. };
  199. /* Globals */
  200. static DC dc;
  201. static XWindow xw;
  202. static XSelection xsel;
  203. static TermWindow win;
  204. /* Font Ring Cache */
  205. enum {
  206. FRC_NORMAL,
  207. FRC_ITALIC,
  208. FRC_BOLD,
  209. FRC_ITALICBOLD
  210. };
  211. typedef struct {
  212. XftFont *font;
  213. int flags;
  214. Rune unicodep;
  215. } Fontcache;
  216. /* Fontcache is an array now. A new font will be appended to the array. */
  217. static Fontcache frc[16];
  218. static int frclen = 0;
  219. static char *usedfont = NULL;
  220. static double usedfontsize = 0;
  221. static double defaultfontsize = 0;
  222. static char *opt_class = NULL;
  223. static char **opt_cmd = NULL;
  224. static char *opt_embed = NULL;
  225. static char *opt_font = NULL;
  226. static char *opt_io = NULL;
  227. static char *opt_line = NULL;
  228. static char *opt_name = NULL;
  229. static char *opt_title = NULL;
  230. static int oldbutton = 3; /* button event on startup: 3 = release */
  231. void
  232. clipcopy(const Arg *dummy)
  233. {
  234. Atom clipboard;
  235. free(xsel.clipboard);
  236. xsel.clipboard = NULL;
  237. if (xsel.primary != NULL) {
  238. xsel.clipboard = xstrdup(xsel.primary);
  239. clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
  240. XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
  241. }
  242. }
  243. void
  244. clippaste(const Arg *dummy)
  245. {
  246. Atom clipboard;
  247. clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
  248. XConvertSelection(xw.dpy, clipboard, xsel.xtarget, clipboard,
  249. xw.win, CurrentTime);
  250. }
  251. void
  252. selpaste(const Arg *dummy)
  253. {
  254. XConvertSelection(xw.dpy, XA_PRIMARY, xsel.xtarget, XA_PRIMARY,
  255. xw.win, CurrentTime);
  256. }
  257. void
  258. numlock(const Arg *dummy)
  259. {
  260. win.mode ^= MODE_NUMLOCK;
  261. }
  262. void
  263. zoom(const Arg *arg)
  264. {
  265. Arg larg;
  266. larg.f = usedfontsize + arg->f;
  267. zoomabs(&larg);
  268. }
  269. void
  270. zoomabs(const Arg *arg)
  271. {
  272. xunloadfonts();
  273. xloadfonts(usedfont, arg->f);
  274. cresize(0, 0);
  275. redraw();
  276. xhints();
  277. }
  278. void
  279. zoomreset(const Arg *arg)
  280. {
  281. Arg larg;
  282. if (defaultfontsize > 0) {
  283. larg.f = defaultfontsize;
  284. zoomabs(&larg);
  285. }
  286. }
  287. int
  288. evcol(XEvent *e)
  289. {
  290. int x = e->xbutton.x - borderpx;
  291. LIMIT(x, 0, win.tw - 1);
  292. return x / win.cw;
  293. }
  294. int
  295. evrow(XEvent *e)
  296. {
  297. int y = e->xbutton.y - borderpx;
  298. LIMIT(y, 0, win.th - 1);
  299. return y / win.ch;
  300. }
  301. void
  302. mousesel(XEvent *e, int done)
  303. {
  304. int type, seltype = SEL_REGULAR;
  305. uint state = e->xbutton.state & ~(Button1Mask | forceselmod);
  306. for (type = 1; type < LEN(selmasks); ++type) {
  307. if (match(selmasks[type], state)) {
  308. seltype = type;
  309. break;
  310. }
  311. }
  312. selextend(evcol(e), evrow(e), seltype, done);
  313. if (done)
  314. setsel(getsel(), e->xbutton.time);
  315. }
  316. void
  317. mousereport(XEvent *e)
  318. {
  319. int len, x = evcol(e), y = evrow(e),
  320. button = e->xbutton.button, state = e->xbutton.state;
  321. char buf[40];
  322. static int ox, oy;
  323. /* from urxvt */
  324. if (e->xbutton.type == MotionNotify) {
  325. if (x == ox && y == oy)
  326. return;
  327. if (!IS_SET(MODE_MOUSEMOTION) && !IS_SET(MODE_MOUSEMANY))
  328. return;
  329. /* MOUSE_MOTION: no reporting if no button is pressed */
  330. if (IS_SET(MODE_MOUSEMOTION) && oldbutton == 3)
  331. return;
  332. button = oldbutton + 32;
  333. ox = x;
  334. oy = y;
  335. } else {
  336. if (!IS_SET(MODE_MOUSESGR) && e->xbutton.type == ButtonRelease) {
  337. button = 3;
  338. } else {
  339. button -= Button1;
  340. if (button >= 3)
  341. button += 64 - 3;
  342. }
  343. if (e->xbutton.type == ButtonPress) {
  344. oldbutton = button;
  345. ox = x;
  346. oy = y;
  347. } else if (e->xbutton.type == ButtonRelease) {
  348. oldbutton = 3;
  349. /* MODE_MOUSEX10: no button release reporting */
  350. if (IS_SET(MODE_MOUSEX10))
  351. return;
  352. if (button == 64 || button == 65)
  353. return;
  354. }
  355. }
  356. if (!IS_SET(MODE_MOUSEX10)) {
  357. button += ((state & ShiftMask ) ? 4 : 0)
  358. + ((state & Mod4Mask ) ? 8 : 0)
  359. + ((state & ControlMask) ? 16 : 0);
  360. }
  361. if (IS_SET(MODE_MOUSESGR)) {
  362. len = snprintf(buf, sizeof(buf), "\033[<%d;%d;%d%c",
  363. button, x+1, y+1,
  364. e->xbutton.type == ButtonRelease ? 'm' : 'M');
  365. } else if (x < 223 && y < 223) {
  366. len = snprintf(buf, sizeof(buf), "\033[M%c%c%c",
  367. 32+button, 32+x+1, 32+y+1);
  368. } else {
  369. return;
  370. }
  371. ttywrite(buf, len, 0);
  372. }
  373. void
  374. bpress(XEvent *e)
  375. {
  376. struct timespec now;
  377. MouseShortcut *ms;
  378. MouseKey *mk;
  379. int snap;
  380. if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forceselmod)) {
  381. mousereport(e);
  382. return;
  383. }
  384. for (ms = mshortcuts; ms < mshortcuts + LEN(mshortcuts); ms++) {
  385. if (e->xbutton.button == ms->b
  386. && match(ms->mask, e->xbutton.state)) {
  387. ttywrite(ms->s, strlen(ms->s), 1);
  388. return;
  389. }
  390. }
  391. for (mk = mkeys; mk < mkeys + LEN(mkeys); mk++) {
  392. if (e->xbutton.button == mk->b
  393. && match(mk->mask, e->xbutton.state)) {
  394. mk->func(&mk->arg);
  395. return;
  396. }
  397. }
  398. if (e->xbutton.button == Button1) {
  399. /*
  400. * If the user clicks below predefined timeouts specific
  401. * snapping behaviour is exposed.
  402. */
  403. clock_gettime(CLOCK_MONOTONIC, &now);
  404. if (TIMEDIFF(now, xsel.tclick2) <= tripleclicktimeout) {
  405. snap = SNAP_LINE;
  406. } else if (TIMEDIFF(now, xsel.tclick1) <= doubleclicktimeout) {
  407. snap = SNAP_WORD;
  408. } else {
  409. snap = 0;
  410. }
  411. xsel.tclick2 = xsel.tclick1;
  412. xsel.tclick1 = now;
  413. selstart(evcol(e), evrow(e), snap);
  414. }
  415. }
  416. void
  417. propnotify(XEvent *e)
  418. {
  419. XPropertyEvent *xpev;
  420. Atom clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
  421. xpev = &e->xproperty;
  422. if (xpev->state == PropertyNewValue &&
  423. (xpev->atom == XA_PRIMARY ||
  424. xpev->atom == clipboard)) {
  425. selnotify(e);
  426. }
  427. }
  428. void
  429. selnotify(XEvent *e)
  430. {
  431. ulong nitems, ofs, rem;
  432. int format;
  433. uchar *data, *last, *repl;
  434. Atom type, incratom, property = None;
  435. incratom = XInternAtom(xw.dpy, "INCR", 0);
  436. ofs = 0;
  437. if (e->type == SelectionNotify)
  438. property = e->xselection.property;
  439. else if (e->type == PropertyNotify)
  440. property = e->xproperty.atom;
  441. if (property == None)
  442. return;
  443. do {
  444. if (XGetWindowProperty(xw.dpy, xw.win, property, ofs,
  445. BUFSIZ/4, False, AnyPropertyType,
  446. &type, &format, &nitems, &rem,
  447. &data)) {
  448. fprintf(stderr, "Clipboard allocation failed\n");
  449. return;
  450. }
  451. if (e->type == PropertyNotify && nitems == 0 && rem == 0) {
  452. /*
  453. * If there is some PropertyNotify with no data, then
  454. * this is the signal of the selection owner that all
  455. * data has been transferred. We won't need to receive
  456. * PropertyNotify events anymore.
  457. */
  458. MODBIT(xw.attrs.event_mask, 0, PropertyChangeMask);
  459. XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask,
  460. &xw.attrs);
  461. }
  462. if (type == incratom) {
  463. /*
  464. * Activate the PropertyNotify events so we receive
  465. * when the selection owner does send us the next
  466. * chunk of data.
  467. */
  468. MODBIT(xw.attrs.event_mask, 1, PropertyChangeMask);
  469. XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask,
  470. &xw.attrs);
  471. /*
  472. * Deleting the property is the transfer start signal.
  473. */
  474. XDeleteProperty(xw.dpy, xw.win, (int)property);
  475. continue;
  476. }
  477. /*
  478. * As seen in getsel:
  479. * Line endings are inconsistent in the terminal and GUI world
  480. * copy and pasting. When receiving some selection data,
  481. * replace all '\n' with '\r'.
  482. * FIXME: Fix the computer world.
  483. */
  484. repl = data;
  485. last = data + nitems * format / 8;
  486. while ((repl = memchr(repl, '\n', last - repl))) {
  487. *repl++ = '\r';
  488. }
  489. if (IS_SET(MODE_BRCKTPASTE) && ofs == 0)
  490. ttywrite("\033[200~", 6, 0);
  491. ttywrite((char *)data, nitems * format / 8, 1);
  492. if (IS_SET(MODE_BRCKTPASTE) && rem == 0)
  493. ttywrite("\033[201~", 6, 0);
  494. XFree(data);
  495. /* number of 32-bit chunks returned */
  496. ofs += nitems * format / 32;
  497. } while (rem > 0);
  498. /*
  499. * Deleting the property again tells the selection owner to send the
  500. * next data chunk in the property.
  501. */
  502. XDeleteProperty(xw.dpy, xw.win, (int)property);
  503. }
  504. void
  505. xclipcopy(void)
  506. {
  507. clipcopy(NULL);
  508. }
  509. void
  510. selclear_(XEvent *e)
  511. {
  512. selclear();
  513. }
  514. void
  515. selrequest(XEvent *e)
  516. {
  517. XSelectionRequestEvent *xsre;
  518. XSelectionEvent xev;
  519. Atom xa_targets, string, clipboard;
  520. char *seltext;
  521. xsre = (XSelectionRequestEvent *) e;
  522. xev.type = SelectionNotify;
  523. xev.requestor = xsre->requestor;
  524. xev.selection = xsre->selection;
  525. xev.target = xsre->target;
  526. xev.time = xsre->time;
  527. if (xsre->property == None)
  528. xsre->property = xsre->target;
  529. /* reject */
  530. xev.property = None;
  531. xa_targets = XInternAtom(xw.dpy, "TARGETS", 0);
  532. if (xsre->target == xa_targets) {
  533. /* respond with the supported type */
  534. string = xsel.xtarget;
  535. XChangeProperty(xsre->display, xsre->requestor, xsre->property,
  536. XA_ATOM, 32, PropModeReplace,
  537. (uchar *) &string, 1);
  538. xev.property = xsre->property;
  539. } else if (xsre->target == xsel.xtarget || xsre->target == XA_STRING) {
  540. /*
  541. * xith XA_STRING non ascii characters may be incorrect in the
  542. * requestor. It is not our problem, use utf8.
  543. */
  544. clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
  545. if (xsre->selection == XA_PRIMARY) {
  546. seltext = xsel.primary;
  547. } else if (xsre->selection == clipboard) {
  548. seltext = xsel.clipboard;
  549. } else {
  550. fprintf(stderr,
  551. "Unhandled clipboard selection 0x%lx\n",
  552. xsre->selection);
  553. return;
  554. }
  555. if (seltext != NULL) {
  556. XChangeProperty(xsre->display, xsre->requestor,
  557. xsre->property, xsre->target,
  558. 8, PropModeReplace,
  559. (uchar *)seltext, strlen(seltext));
  560. xev.property = xsre->property;
  561. }
  562. }
  563. /* all done, send a notification to the listener */
  564. if (!XSendEvent(xsre->display, xsre->requestor, 1, 0, (XEvent *) &xev))
  565. fprintf(stderr, "Error sending SelectionNotify event\n");
  566. }
  567. void
  568. setsel(char *str, Time t)
  569. {
  570. if (!str)
  571. return;
  572. free(xsel.primary);
  573. xsel.primary = str;
  574. XSetSelectionOwner(xw.dpy, XA_PRIMARY, xw.win, t);
  575. if (XGetSelectionOwner(xw.dpy, XA_PRIMARY) != xw.win)
  576. selclear();
  577. xclipcopy();
  578. }
  579. void
  580. xsetsel(char *str)
  581. {
  582. setsel(str, CurrentTime);
  583. }
  584. void
  585. brelease(XEvent *e)
  586. {
  587. if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forceselmod)) {
  588. mousereport(e);
  589. return;
  590. }
  591. if (e->xbutton.button == Button2)
  592. selpaste(NULL);
  593. else if (e->xbutton.button == Button1)
  594. mousesel(e, 1);
  595. }
  596. void
  597. bmotion(XEvent *e)
  598. {
  599. if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forceselmod)) {
  600. mousereport(e);
  601. return;
  602. }
  603. mousesel(e, 0);
  604. }
  605. void
  606. cresize(int width, int height)
  607. {
  608. int col, row;
  609. if (width != 0)
  610. win.w = width;
  611. if (height != 0)
  612. win.h = height;
  613. col = (win.w - 2 * borderpx) / win.cw;
  614. row = (win.h - 2 * borderpx) / win.ch;
  615. col = MAX(1, col);
  616. row = MAX(1, row);
  617. tresize(col, row);
  618. xresize(col, row);
  619. ttyresize(win.tw, win.th);
  620. }
  621. void
  622. xresize(int col, int row)
  623. {
  624. win.tw = col * win.cw;
  625. win.th = row * win.ch;
  626. XFreePixmap(xw.dpy, xw.buf);
  627. xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h,
  628. xw.depth);
  629. XftDrawChange(xw.draw, xw.buf);
  630. xclear(0, 0, win.w, win.h);
  631. /* resize to new width */
  632. xw.specbuf = xrealloc(xw.specbuf, col * sizeof(GlyphFontSpec));
  633. }
  634. ushort
  635. sixd_to_16bit(int x)
  636. {
  637. return x == 0 ? 0 : 0x3737 + 0x2828 * x;
  638. }
  639. int
  640. xloadcolor(int i, const char *name, Color *ncolor)
  641. {
  642. XRenderColor color = { .alpha = 0xffff };
  643. if (!name) {
  644. if (BETWEEN(i, 16, 255)) { /* 256 color */
  645. if (i < 6*6*6+16) { /* same colors as xterm */
  646. color.red = sixd_to_16bit( ((i-16)/36)%6 );
  647. color.green = sixd_to_16bit( ((i-16)/6) %6 );
  648. color.blue = sixd_to_16bit( ((i-16)/1) %6 );
  649. } else { /* greyscale */
  650. color.red = 0x0808 + 0x0a0a * (i - (6*6*6+16));
  651. color.green = color.blue = color.red;
  652. }
  653. return XftColorAllocValue(xw.dpy, xw.vis,
  654. xw.cmap, &color, ncolor);
  655. } else
  656. name = colorname[i];
  657. }
  658. return XftColorAllocName(xw.dpy, xw.vis, xw.cmap, name, ncolor);
  659. }
  660. void
  661. xloadcols(void)
  662. {
  663. int i;
  664. static int loaded;
  665. Color *cp;
  666. if (loaded) {
  667. for (cp = dc.col; cp < &dc.col[dc.collen]; ++cp)
  668. XftColorFree(xw.dpy, xw.vis, xw.cmap, cp);
  669. } else {
  670. dc.collen = MAX(LEN(colorname), 256);
  671. dc.col = xmalloc(dc.collen * sizeof(Color));
  672. }
  673. for (i = 0; i < dc.collen; i++)
  674. if (!xloadcolor(i, NULL, &dc.col[i])) {
  675. if (colorname[i])
  676. die("Could not allocate color '%s'\n", colorname[i]);
  677. else
  678. die("Could not allocate color %d\n", i);
  679. }
  680. /* set alpha value of bg color */
  681. if (USE_ARGB) {
  682. dc.col[defaultbg].color.alpha = (0xffff * alpha) / OPAQUE;
  683. dc.col[defaultbg].pixel &= 0x00111111;
  684. dc.col[defaultbg].pixel |= alpha << 24;
  685. }
  686. loaded = 1;
  687. }
  688. int
  689. xsetcolorname(int x, const char *name)
  690. {
  691. Color ncolor;
  692. if (!BETWEEN(x, 0, dc.collen))
  693. return 1;
  694. if (!xloadcolor(x, name, &ncolor))
  695. return 1;
  696. XftColorFree(xw.dpy, xw.vis, xw.cmap, &dc.col[x]);
  697. dc.col[x] = ncolor;
  698. return 0;
  699. }
  700. void
  701. xtermclear(int col1, int row1, int col2, int row2)
  702. {
  703. XftDrawRect(xw.draw,
  704. &dc.col[IS_SET(MODE_REVERSE) ? defaultfg : defaultbg],
  705. borderpx + col1 * win.cw,
  706. borderpx + row1 * win.ch,
  707. (col2-col1+1) * win.cw,
  708. (row2-row1+1) * win.ch);
  709. }
  710. /*
  711. * Absolute coordinates.
  712. */
  713. void
  714. xclear(int x1, int y1, int x2, int y2)
  715. {
  716. XftDrawRect(xw.draw,
  717. &dc.col[IS_SET(MODE_REVERSE)? defaultfg : defaultbg],
  718. x1, y1, x2-x1, y2-y1);
  719. }
  720. void
  721. xhints(void)
  722. {
  723. XClassHint class = {opt_name ? opt_name : "st",
  724. opt_class ? opt_class : "St"};
  725. XWMHints wm = {.flags = InputHint, .input = 1};
  726. XSizeHints *sizeh;
  727. sizeh = XAllocSizeHints();
  728. sizeh->flags = PSize | PResizeInc | PBaseSize | PMinSize;
  729. sizeh->height = win.h;
  730. sizeh->width = win.w;
  731. sizeh->height_inc = win.ch;
  732. sizeh->width_inc = win.cw;
  733. sizeh->base_height = 2 * borderpx;
  734. sizeh->base_width = 2 * borderpx;
  735. sizeh->min_height = win.ch + 2 * borderpx;
  736. sizeh->min_width = win.cw + 2 * borderpx;
  737. if (xw.isfixed) {
  738. sizeh->flags |= PMaxSize;
  739. sizeh->min_width = sizeh->max_width = win.w;
  740. sizeh->min_height = sizeh->max_height = win.h;
  741. }
  742. if (xw.gm & (XValue|YValue)) {
  743. sizeh->flags |= USPosition | PWinGravity;
  744. sizeh->x = xw.l;
  745. sizeh->y = xw.t;
  746. sizeh->win_gravity = xgeommasktogravity(xw.gm);
  747. }
  748. XSetWMProperties(xw.dpy, xw.win, NULL, NULL, NULL, 0, sizeh, &wm,
  749. &class);
  750. XFree(sizeh);
  751. }
  752. int
  753. xgeommasktogravity(int mask)
  754. {
  755. switch (mask & (XNegative|YNegative)) {
  756. case 0:
  757. return NorthWestGravity;
  758. case XNegative:
  759. return NorthEastGravity;
  760. case YNegative:
  761. return SouthWestGravity;
  762. }
  763. return SouthEastGravity;
  764. }
  765. int
  766. xloadfont(Font *f, FcPattern *pattern)
  767. {
  768. FcPattern *configured;
  769. FcPattern *match;
  770. FcResult result;
  771. XGlyphInfo extents;
  772. int wantattr, haveattr;
  773. /*
  774. * Manually configure instead of calling XftMatchFont
  775. * so that we can use the configured pattern for
  776. * "missing glyph" lookups.
  777. */
  778. configured = FcPatternDuplicate(pattern);
  779. if (!configured)
  780. return 1;
  781. FcConfigSubstitute(NULL, configured, FcMatchPattern);
  782. XftDefaultSubstitute(xw.dpy, xw.scr, configured);
  783. match = FcFontMatch(NULL, configured, &result);
  784. if (!match) {
  785. FcPatternDestroy(configured);
  786. return 1;
  787. }
  788. if (!(f->match = XftFontOpenPattern(xw.dpy, match))) {
  789. FcPatternDestroy(configured);
  790. FcPatternDestroy(match);
  791. return 1;
  792. }
  793. if ((XftPatternGetInteger(pattern, "slant", 0, &wantattr) ==
  794. XftResultMatch)) {
  795. /*
  796. * Check if xft was unable to find a font with the appropriate
  797. * slant but gave us one anyway. Try to mitigate.
  798. */
  799. if ((XftPatternGetInteger(f->match->pattern, "slant", 0,
  800. &haveattr) != XftResultMatch) || haveattr < wantattr) {
  801. f->badslant = 1;
  802. fputs("st: font slant does not match\n", stderr);
  803. }
  804. }
  805. if ((XftPatternGetInteger(pattern, "weight", 0, &wantattr) ==
  806. XftResultMatch)) {
  807. if ((XftPatternGetInteger(f->match->pattern, "weight", 0,
  808. &haveattr) != XftResultMatch) || haveattr != wantattr) {
  809. f->badweight = 1;
  810. fputs("st: font weight does not match\n", stderr);
  811. }
  812. }
  813. XftTextExtentsUtf8(xw.dpy, f->match,
  814. (const FcChar8 *) ascii_printable,
  815. strlen(ascii_printable), &extents);
  816. f->set = NULL;
  817. f->pattern = configured;
  818. f->ascent = f->match->ascent;
  819. f->descent = f->match->descent;
  820. f->lbearing = 0;
  821. f->rbearing = f->match->max_advance_width;
  822. f->height = f->ascent + f->descent;
  823. f->width = DIVCEIL(extents.xOff, strlen(ascii_printable));
  824. return 0;
  825. }
  826. void
  827. xloadfonts(char *fontstr, double fontsize)
  828. {
  829. FcPattern *pattern;
  830. double fontval;
  831. if (fontstr[0] == '-') {
  832. pattern = XftXlfdParse(fontstr, False, False);
  833. } else {
  834. pattern = FcNameParse((FcChar8 *)fontstr);
  835. }
  836. if (!pattern)
  837. die("st: can't open font %s\n", fontstr);
  838. if (fontsize > 1) {
  839. FcPatternDel(pattern, FC_PIXEL_SIZE);
  840. FcPatternDel(pattern, FC_SIZE);
  841. FcPatternAddDouble(pattern, FC_PIXEL_SIZE, (double)fontsize);
  842. usedfontsize = fontsize;
  843. } else {
  844. if (FcPatternGetDouble(pattern, FC_PIXEL_SIZE, 0, &fontval) ==
  845. FcResultMatch) {
  846. usedfontsize = fontval;
  847. } else if (FcPatternGetDouble(pattern, FC_SIZE, 0, &fontval) ==
  848. FcResultMatch) {
  849. usedfontsize = -1;
  850. } else {
  851. /*
  852. * Default font size is 12, if none given. This is to
  853. * have a known usedfontsize value.
  854. */
  855. FcPatternAddDouble(pattern, FC_PIXEL_SIZE, 12);
  856. usedfontsize = 12;
  857. }
  858. defaultfontsize = usedfontsize;
  859. }
  860. if (xloadfont(&dc.font, pattern))
  861. die("st: can't open font %s\n", fontstr);
  862. if (usedfontsize < 0) {
  863. FcPatternGetDouble(dc.font.match->pattern,
  864. FC_PIXEL_SIZE, 0, &fontval);
  865. usedfontsize = fontval;
  866. if (fontsize == 0)
  867. defaultfontsize = fontval;
  868. }
  869. /* Setting character width and height. */
  870. win.cw = ceilf(dc.font.width * cwscale);
  871. win.ch = ceilf(dc.font.height * chscale);
  872. win.cyo = ceilf(dc.font.height * (chscale - 1) / 2);
  873. FcPatternDel(pattern, FC_SLANT);
  874. FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
  875. if (xloadfont(&dc.ifont, pattern))
  876. die("st: can't open font %s\n", fontstr);
  877. FcPatternDel(pattern, FC_WEIGHT);
  878. FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
  879. if (xloadfont(&dc.ibfont, pattern))
  880. die("st: can't open font %s\n", fontstr);
  881. FcPatternDel(pattern, FC_SLANT);
  882. FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ROMAN);
  883. if (xloadfont(&dc.bfont, pattern))
  884. die("st: can't open font %s\n", fontstr);
  885. FcPatternDestroy(pattern);
  886. }
  887. void
  888. xunloadfont(Font *f)
  889. {
  890. XftFontClose(xw.dpy, f->match);
  891. FcPatternDestroy(f->pattern);
  892. if (f->set)
  893. FcFontSetDestroy(f->set);
  894. }
  895. void
  896. xunloadfonts(void)
  897. {
  898. /* Free the loaded fonts in the font cache. */
  899. while (frclen > 0)
  900. XftFontClose(xw.dpy, frc[--frclen].font);
  901. xunloadfont(&dc.font);
  902. xunloadfont(&dc.bfont);
  903. xunloadfont(&dc.ifont);
  904. xunloadfont(&dc.ibfont);
  905. }
  906. void
  907. xinit(int cols, int rows)
  908. {
  909. XGCValues gcvalues;
  910. Cursor cursor;
  911. Window parent;
  912. pid_t thispid = getpid();
  913. XColor xmousefg, xmousebg;
  914. xw.scr = XDefaultScreen(xw.dpy);
  915. xw.depth = (USE_ARGB) ? 32: XDefaultDepth(xw.dpy, xw.scr);
  916. if (!USE_ARGB)
  917. xw.vis = XDefaultVisual(xw.dpy, xw.scr);
  918. else {
  919. XVisualInfo *vis;
  920. XRenderPictFormat *fmt;
  921. int nvi;
  922. int i;
  923. XVisualInfo tpl = {
  924. .screen = xw.scr,
  925. .depth = 32,
  926. .class = TrueColor
  927. };
  928. vis = XGetVisualInfo(xw.dpy,
  929. VisualScreenMask | VisualDepthMask | VisualClassMask,
  930. &tpl, &nvi);
  931. xw.vis = NULL;
  932. for (i = 0; i < nvi; i++) {
  933. fmt = XRenderFindVisualFormat(xw.dpy, vis[i].visual);
  934. if (fmt->type == PictTypeDirect && fmt->direct.alphaMask) {
  935. xw.vis = vis[i].visual;
  936. break;
  937. }
  938. }
  939. XFree(vis);
  940. if (!xw.vis) {
  941. fprintf(stderr, "Couldn't find ARGB visual.\n");
  942. exit(1);
  943. }
  944. }
  945. /* font */
  946. if (!FcInit())
  947. die("Could not init fontconfig.\n");
  948. usedfont = (opt_font == NULL)? font : opt_font;
  949. xloadfonts(usedfont, 0);
  950. /* colors */
  951. if (!USE_ARGB)
  952. xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
  953. else
  954. xw.cmap = XCreateColormap(xw.dpy, XRootWindow(xw.dpy, xw.scr),
  955. xw.vis, None);
  956. xloadcols();
  957. /* adjust fixed window geometry */
  958. win.w = 2 * borderpx + cols * win.cw;
  959. win.h = 2 * borderpx + rows * win.ch;
  960. if (xw.gm & XNegative)
  961. xw.l += DisplayWidth(xw.dpy, xw.scr) - win.w - 2;
  962. if (xw.gm & YNegative)
  963. xw.t += DisplayHeight(xw.dpy, xw.scr) - win.h - 2;
  964. /* Events */
  965. xw.attrs.background_pixel = dc.col[defaultbg].pixel;
  966. xw.attrs.border_pixel = dc.col[defaultbg].pixel;
  967. xw.attrs.bit_gravity = NorthWestGravity;
  968. xw.attrs.event_mask = FocusChangeMask | KeyPressMask
  969. | ExposureMask | VisibilityChangeMask | StructureNotifyMask
  970. | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask;
  971. xw.attrs.colormap = xw.cmap;
  972. if (!(opt_embed && (parent = strtol(opt_embed, NULL, 0))))
  973. parent = XRootWindow(xw.dpy, xw.scr);
  974. xw.win = XCreateWindow(xw.dpy, parent, xw.l, xw.t,
  975. win.w, win.h, 0, xw.depth, InputOutput,
  976. xw.vis, CWBackPixel | CWBorderPixel | CWBitGravity
  977. | CWEventMask | CWColormap, &xw.attrs);
  978. memset(&gcvalues, 0, sizeof(gcvalues));
  979. gcvalues.graphics_exposures = False;
  980. xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h, xw.depth);
  981. dc.gc = XCreateGC(xw.dpy, (USE_ARGB) ? xw.buf: parent,
  982. GCGraphicsExposures, &gcvalues);
  983. XSetForeground(xw.dpy, dc.gc, dc.col[defaultbg].pixel);
  984. XFillRectangle(xw.dpy, xw.buf, dc.gc, 0, 0, win.w, win.h);
  985. /* font spec buffer */
  986. xw.specbuf = xmalloc(cols * sizeof(GlyphFontSpec));
  987. /* Xft rendering context */
  988. xw.draw = XftDrawCreate(xw.dpy, xw.buf, xw.vis, xw.cmap);
  989. /* input methods */
  990. if ((xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL) {
  991. XSetLocaleModifiers("@im=local");
  992. if ((xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL) {
  993. XSetLocaleModifiers("@im=");
  994. if ((xw.xim = XOpenIM(xw.dpy,
  995. NULL, NULL, NULL)) == NULL) {
  996. die("XOpenIM failed. Could not open input"
  997. " device.\n");
  998. }
  999. }
  1000. }
  1001. xw.xic = XCreateIC(xw.xim, XNInputStyle, XIMPreeditNothing
  1002. | XIMStatusNothing, XNClientWindow, xw.win,
  1003. XNFocusWindow, xw.win, NULL);
  1004. if (xw.xic == NULL)
  1005. die("XCreateIC failed. Could not obtain input method.\n");
  1006. /* white cursor, black outline */
  1007. cursor = XCreateFontCursor(xw.dpy, mouseshape);
  1008. XDefineCursor(xw.dpy, xw.win, cursor);
  1009. if (XParseColor(xw.dpy, xw.cmap, colorname[mousefg], &xmousefg) == 0) {
  1010. xmousefg.red = 0xffff;
  1011. xmousefg.green = 0xffff;
  1012. xmousefg.blue = 0xffff;
  1013. }
  1014. if (XParseColor(xw.dpy, xw.cmap, colorname[mousebg], &xmousebg) == 0) {
  1015. xmousebg.red = 0x0000;
  1016. xmousebg.green = 0x0000;
  1017. xmousebg.blue = 0x0000;
  1018. }
  1019. XRecolorCursor(xw.dpy, cursor, &xmousefg, &xmousebg);
  1020. xw.xembed = XInternAtom(xw.dpy, "_XEMBED", False);
  1021. xw.wmdeletewin = XInternAtom(xw.dpy, "WM_DELETE_WINDOW", False);
  1022. xw.netwmname = XInternAtom(xw.dpy, "_NET_WM_NAME", False);
  1023. XSetWMProtocols(xw.dpy, xw.win, &xw.wmdeletewin, 1);
  1024. xw.netwmpid = XInternAtom(xw.dpy, "_NET_WM_PID", False);
  1025. XChangeProperty(xw.dpy, xw.win, xw.netwmpid, XA_CARDINAL, 32,
  1026. PropModeReplace, (uchar *)&thispid, 1);
  1027. win.mode = MODE_NUMLOCK;
  1028. resettitle();
  1029. XMapWindow(xw.dpy, xw.win);
  1030. xhints();
  1031. XSync(xw.dpy, False);
  1032. clock_gettime(CLOCK_MONOTONIC, &xsel.tclick1);
  1033. clock_gettime(CLOCK_MONOTONIC, &xsel.tclick2);
  1034. xsel.primary = NULL;
  1035. xsel.clipboard = NULL;
  1036. xsel.xtarget = XInternAtom(xw.dpy, "UTF8_STRING", 0);
  1037. if (xsel.xtarget == None)
  1038. xsel.xtarget = XA_STRING;
  1039. }
  1040. int
  1041. xmakeglyphfontspecs(XftGlyphFontSpec *specs, const Glyph *glyphs, int len, int x, int y)
  1042. {
  1043. float winx = borderpx + x * win.cw, winy = borderpx + y * win.ch, xp, yp;
  1044. ushort mode, prevmode = USHRT_MAX;
  1045. Font *font = &dc.font;
  1046. int frcflags = FRC_NORMAL;
  1047. float runewidth = win.cw;
  1048. Rune rune;
  1049. FT_UInt glyphidx;
  1050. FcResult fcres;
  1051. FcPattern *fcpattern, *fontpattern;
  1052. FcFontSet *fcsets[] = { NULL };
  1053. FcCharSet *fccharset;
  1054. int i, f, numspecs = 0;
  1055. for (i = 0, xp = winx, yp = winy + font->ascent + win.cyo; i < len; ++i) {
  1056. /* Fetch rune and mode for current glyph. */
  1057. rune = glyphs[i].u;
  1058. mode = glyphs[i].mode;
  1059. /* Skip dummy wide-character spacing. */
  1060. if (mode == ATTR_WDUMMY)
  1061. continue;
  1062. /* Determine font for glyph if different from previous glyph. */
  1063. if (prevmode != mode) {
  1064. prevmode = mode;
  1065. font = &dc.font;
  1066. frcflags = FRC_NORMAL;
  1067. runewidth = win.cw * ((mode & ATTR_WIDE) ? 2.0f : 1.0f);
  1068. if ((mode & ATTR_ITALIC) && (mode & ATTR_BOLD)) {
  1069. font = &dc.ibfont;
  1070. frcflags = FRC_ITALICBOLD;
  1071. } else if (mode & ATTR_ITALIC) {
  1072. font = &dc.ifont;
  1073. frcflags = FRC_ITALIC;
  1074. } else if (mode & ATTR_BOLD) {
  1075. font = &dc.bfont;
  1076. frcflags = FRC_BOLD;
  1077. }
  1078. yp = winy + font->ascent + win.cyo;
  1079. }
  1080. /* Lookup character index with default font. */
  1081. glyphidx = XftCharIndex(xw.dpy, font->match, rune);
  1082. if (glyphidx) {
  1083. specs[numspecs].font = font->match;
  1084. specs[numspecs].glyph = glyphidx;
  1085. specs[numspecs].x = (short)xp;
  1086. specs[numspecs].y = (short)yp;
  1087. xp += runewidth;
  1088. numspecs++;
  1089. continue;
  1090. }
  1091. /* Fallback on font cache, search the font cache for match. */
  1092. for (f = 0; f < frclen; f++) {
  1093. glyphidx = XftCharIndex(xw.dpy, frc[f].font, rune);
  1094. /* Everything correct. */
  1095. if (glyphidx && frc[f].flags == frcflags)
  1096. break;
  1097. /* We got a default font for a not found glyph. */
  1098. if (!glyphidx && frc[f].flags == frcflags
  1099. && frc[f].unicodep == rune) {
  1100. break;
  1101. }
  1102. }
  1103. /* Nothing was found. Use fontconfig to find matching font. */
  1104. if (f >= frclen) {
  1105. if (!font->set)
  1106. font->set = FcFontSort(0, font->pattern,
  1107. 1, 0, &fcres);
  1108. fcsets[0] = font->set;
  1109. /*
  1110. * Nothing was found in the cache. Now use
  1111. * some dozen of Fontconfig calls to get the
  1112. * font for one single character.
  1113. *
  1114. * Xft and fontconfig are design failures.
  1115. */
  1116. fcpattern = FcPatternDuplicate(font->pattern);
  1117. fccharset = FcCharSetCreate();
  1118. FcCharSetAddChar(fccharset, rune);
  1119. FcPatternAddCharSet(fcpattern, FC_CHARSET,
  1120. fccharset);
  1121. FcPatternAddBool(fcpattern, FC_SCALABLE, 1);
  1122. FcConfigSubstitute(0, fcpattern,
  1123. FcMatchPattern);
  1124. FcDefaultSubstitute(fcpattern);
  1125. fontpattern = FcFontSetMatch(0, fcsets, 1,
  1126. fcpattern, &fcres);
  1127. /*
  1128. * Overwrite or create the new cache entry.
  1129. */
  1130. if (frclen >= LEN(frc)) {
  1131. frclen = LEN(frc) - 1;
  1132. XftFontClose(xw.dpy, frc[frclen].font);
  1133. frc[frclen].unicodep = 0;
  1134. }
  1135. frc[frclen].font = XftFontOpenPattern(xw.dpy,
  1136. fontpattern);
  1137. if (!frc[frclen].font)
  1138. die("XftFontOpenPattern failed seeking fallback font: %s\n",
  1139. strerror(errno));
  1140. frc[frclen].flags = frcflags;
  1141. frc[frclen].unicodep = rune;
  1142. glyphidx = XftCharIndex(xw.dpy, frc[frclen].font, rune);
  1143. f = frclen;
  1144. frclen++;
  1145. FcPatternDestroy(fcpattern);
  1146. FcCharSetDestroy(fccharset);
  1147. }
  1148. specs[numspecs].font = frc[f].font;
  1149. specs[numspecs].glyph = glyphidx;
  1150. specs[numspecs].x = (short)xp;
  1151. specs[numspecs].y = (short)yp;
  1152. xp += runewidth;
  1153. numspecs++;
  1154. }
  1155. return numspecs;
  1156. }
  1157. void
  1158. xdrawglyphfontspecs(const XftGlyphFontSpec *specs, Glyph base, int len, int x, int y)
  1159. {
  1160. int charlen = len * ((base.mode & ATTR_WIDE) ? 2 : 1);
  1161. int winx = borderpx + x * win.cw, winy = borderpx + y * win.ch,
  1162. width = charlen * win.cw;
  1163. Color *fg, *bg, *temp, revfg, revbg, truefg, truebg;
  1164. XRenderColor colfg, colbg;
  1165. XRectangle r;
  1166. /* Fallback on color display for attributes not supported by the font */
  1167. if (base.mode & ATTR_ITALIC && base.mode & ATTR_BOLD) {
  1168. if (dc.ibfont.badslant || dc.ibfont.badweight)
  1169. base.fg = defaultattr;
  1170. } else if ((base.mode & ATTR_ITALIC && dc.ifont.badslant) ||
  1171. (base.mode & ATTR_BOLD && dc.bfont.badweight)) {
  1172. base.fg = defaultattr;
  1173. }
  1174. if (IS_TRUECOL(base.fg)) {
  1175. colfg.alpha = 0xffff;
  1176. colfg.red = TRUERED(base.fg);
  1177. colfg.green = TRUEGREEN(base.fg);
  1178. colfg.blue = TRUEBLUE(base.fg);
  1179. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &truefg);
  1180. fg = &truefg;
  1181. } else {
  1182. fg = &dc.col[base.fg];
  1183. }
  1184. if (IS_TRUECOL(base.bg)) {
  1185. colbg.alpha = 0xffff;
  1186. colbg.green = TRUEGREEN(base.bg);
  1187. colbg.red = TRUERED(base.bg);
  1188. colbg.blue = TRUEBLUE(base.bg);
  1189. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg, &truebg);
  1190. bg = &truebg;
  1191. } else {
  1192. bg = &dc.col[base.bg];
  1193. }
  1194. /* Change basic system colors [0-7] to bright system colors [8-15] */
  1195. if ((base.mode & ATTR_BOLD_FAINT) == ATTR_BOLD && BETWEEN(base.fg, 0, 7))
  1196. fg = &dc.col[base.fg];
  1197. if (IS_SET(MODE_REVERSE)) {
  1198. if (fg == &dc.col[defaultfg]) {
  1199. fg = &dc.col[defaultbg];
  1200. } else {
  1201. colfg.red = ~fg->color.red;
  1202. colfg.green = ~fg->color.green;
  1203. colfg.blue = ~fg->color.blue;
  1204. colfg.alpha = fg->color.alpha;
  1205. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg,
  1206. &revfg);
  1207. fg = &revfg;
  1208. }
  1209. if (bg == &dc.col[defaultbg]) {
  1210. bg = &dc.col[defaultfg];
  1211. } else {
  1212. colbg.red = ~bg->color.red;
  1213. colbg.green = ~bg->color.green;
  1214. colbg.blue = ~bg->color.blue;
  1215. colbg.alpha = bg->color.alpha;
  1216. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg,
  1217. &revbg);
  1218. bg = &revbg;
  1219. }
  1220. }
  1221. if ((base.mode & ATTR_BOLD_FAINT) == ATTR_FAINT) {
  1222. colfg.red = fg->color.red / 2;
  1223. colfg.green = fg->color.green / 2;
  1224. colfg.blue = fg->color.blue / 2;
  1225. colfg.alpha = fg->color.alpha;
  1226. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &revfg);
  1227. fg = &revfg;
  1228. }
  1229. if (base.mode & ATTR_REVERSE) {
  1230. temp = fg;
  1231. fg = bg;
  1232. bg = temp;
  1233. }
  1234. if (base.mode & ATTR_BLINK && win.mode & MODE_BLINK)
  1235. fg = bg;
  1236. if (base.mode & ATTR_INVISIBLE)
  1237. fg = bg;
  1238. /* Intelligent cleaning up of the borders. */
  1239. if (x == 0) {
  1240. xclear(0, (y == 0)? 0 : winy, borderpx,
  1241. winy + win.ch +
  1242. ((winy + win.ch >= borderpx + win.th)? win.h : 0));
  1243. }
  1244. if (winx + width >= borderpx + win.tw) {
  1245. xclear(winx + width, (y == 0)? 0 : winy, win.w,
  1246. ((winy + win.ch >= borderpx + win.th)? win.h : (winy + win.ch)));
  1247. }
  1248. if (y == 0)
  1249. xclear(winx, 0, winx + width, borderpx);
  1250. if (winy + win.ch >= borderpx + win.th)
  1251. xclear(winx, winy + win.ch, winx + width, win.h);
  1252. /* Clean up the region we want to draw to. */
  1253. XftDrawRect(xw.draw, bg, winx, winy, width, win.ch);
  1254. /* Set the clip region because Xft is sometimes dirty. */
  1255. r.x = 0;
  1256. r.y = 0;
  1257. r.height = win.ch;
  1258. r.width = width;
  1259. XftDrawSetClipRectangles(xw.draw, winx, winy, &r, 1);
  1260. /* Render the glyphs. */
  1261. XftDrawGlyphFontSpec(xw.draw, fg, specs, len);
  1262. /* Render underline and strikethrough. */
  1263. if (base.mode & ATTR_UNDERLINE) {
  1264. XftDrawRect(xw.draw, fg, winx, winy + win.cyo + dc.font.ascent + 1,
  1265. width, 1);
  1266. }
  1267. if (base.mode & ATTR_STRUCK) {
  1268. XftDrawRect(xw.draw, fg, winx, winy + win.cyo + 2 * dc.font.ascent / 3,
  1269. width, 1);
  1270. }
  1271. /* Reset clip to none. */
  1272. XftDrawSetClip(xw.draw, 0);
  1273. }
  1274. void
  1275. xdrawglyph(Glyph g, int x, int y)
  1276. {
  1277. int numspecs;
  1278. XftGlyphFontSpec spec;
  1279. numspecs = xmakeglyphfontspecs(&spec, &g, 1, x, y);
  1280. xdrawglyphfontspecs(&spec, g, numspecs, x, y);
  1281. }
  1282. void
  1283. xdrawcursor(int cx, int cy, Glyph g, int ox, int oy, Glyph og)
  1284. {
  1285. Color drawcol;
  1286. /* remove the old cursor */
  1287. if (selected(ox, oy))
  1288. og.mode ^= ATTR_REVERSE;
  1289. xdrawglyph(og, ox, oy);
  1290. if (IS_SET(MODE_HIDE))
  1291. return;
  1292. /*
  1293. * Select the right color for the right mode.
  1294. */
  1295. g.mode &= ATTR_BOLD|ATTR_ITALIC|ATTR_UNDERLINE|ATTR_STRUCK|ATTR_WIDE;
  1296. if (IS_SET(MODE_REVERSE)) {
  1297. g.mode |= ATTR_REVERSE;
  1298. g.bg = defaultfg;
  1299. if (selected(cx, cy)) {
  1300. drawcol = dc.col[defaultcs];
  1301. g.fg = defaultrcs;
  1302. } else {
  1303. drawcol = dc.col[defaultrcs];
  1304. g.fg = defaultcs;
  1305. }
  1306. } else {
  1307. if (selected(cx, cy)) {
  1308. g.fg = defaultfg;
  1309. g.bg = defaultrcs;
  1310. } else {
  1311. g.fg = defaultbg;
  1312. g.bg = defaultcs;
  1313. }
  1314. drawcol = dc.col[g.bg];
  1315. }
  1316. /* draw the new one */
  1317. if (IS_SET(MODE_FOCUSED)) {
  1318. switch (win.cursor) {
  1319. case 7: /* st extension: snowman (U+2603) */
  1320. g.u = 0x2603;
  1321. case 0: /* Blinking Block */
  1322. case 1: /* Blinking Block (Default) */
  1323. case 2: /* Steady Block */
  1324. xdrawglyph(g, cx, cy);
  1325. break;
  1326. case 3: /* Blinking Underline */
  1327. case 4: /* Steady Underline */
  1328. XftDrawRect(xw.draw, &drawcol,
  1329. borderpx + cx * win.cw,
  1330. borderpx + (cy + 1) * win.ch - \
  1331. cursorthickness,
  1332. win.cw, cursorthickness);
  1333. break;
  1334. case 5: /* Blinking bar */
  1335. case 6: /* Steady bar */
  1336. XftDrawRect(xw.draw, &drawcol,
  1337. borderpx + cx * win.cw,
  1338. borderpx + cy * win.ch,
  1339. cursorthickness, win.ch);
  1340. break;
  1341. }
  1342. } else {
  1343. XftDrawRect(xw.draw, &drawcol,
  1344. borderpx + cx * win.cw,
  1345. borderpx + cy * win.ch,
  1346. win.cw - 1, 1);
  1347. XftDrawRect(xw.draw, &drawcol,
  1348. borderpx + cx * win.cw,
  1349. borderpx + cy * win.ch,
  1350. 1, win.ch - 1);
  1351. XftDrawRect(xw.draw, &drawcol,
  1352. borderpx + (cx + 1) * win.cw - 1,
  1353. borderpx + cy * win.ch,
  1354. 1, win.ch - 1);
  1355. XftDrawRect(xw.draw, &drawcol,
  1356. borderpx + cx * win.cw,
  1357. borderpx + (cy + 1) * win.ch - 1,
  1358. win.cw, 1);
  1359. }
  1360. }
  1361. void
  1362. xsetenv(void)
  1363. {
  1364. char buf[sizeof(long) * 8 + 1];
  1365. snprintf(buf, sizeof(buf), "%lu", xw.win);
  1366. setenv("WINDOWID", buf, 1);
  1367. }
  1368. void
  1369. xsettitle(char *p)
  1370. {
  1371. XTextProperty prop;
  1372. DEFAULT(p, opt_title);
  1373. Xutf8TextListToTextProperty(xw.dpy, &p, 1, XUTF8StringStyle,
  1374. &prop);
  1375. XSetWMName(xw.dpy, xw.win, &prop);
  1376. XSetTextProperty(xw.dpy, xw.win, &prop, xw.netwmname);
  1377. XFree(prop.value);
  1378. }
  1379. int
  1380. xstartdraw(void)
  1381. {
  1382. return IS_SET(MODE_VISIBLE);
  1383. }
  1384. void
  1385. xdrawline(Line line, int x1, int y1, int x2)
  1386. {
  1387. int i, x, ox, numspecs;
  1388. Glyph base, new;
  1389. XftGlyphFontSpec *specs = xw.specbuf;
  1390. numspecs = xmakeglyphfontspecs(specs, &line[x1], x2 - x1, x1, y1);
  1391. i = ox = 0;
  1392. for (x = x1; x < x2 && i < numspecs; x++) {
  1393. new = line[x];
  1394. if (new.mode == ATTR_WDUMMY)
  1395. continue;
  1396. if (selected(x, y1))
  1397. new.mode ^= ATTR_REVERSE;
  1398. if (i > 0 && ATTRCMP(base, new)) {
  1399. xdrawglyphfontspecs(specs, base, i, ox, y1);
  1400. specs += i;
  1401. numspecs -= i;
  1402. i = 0;
  1403. }
  1404. if (i == 0) {
  1405. ox = x;
  1406. base = new;
  1407. }
  1408. i++;
  1409. }
  1410. if (i > 0)
  1411. xdrawglyphfontspecs(specs, base, i, ox, y1);
  1412. }
  1413. void
  1414. xfinishdraw(void)
  1415. {
  1416. XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, 0, 0, win.w,
  1417. win.h, 0, 0);
  1418. XSetForeground(xw.dpy, dc.gc,
  1419. dc.col[IS_SET(MODE_REVERSE)?
  1420. defaultfg : defaultbg].pixel);
  1421. }
  1422. void
  1423. expose(XEvent *ev)
  1424. {
  1425. redraw();
  1426. }
  1427. void
  1428. visibility(XEvent *ev)
  1429. {
  1430. XVisibilityEvent *e = &ev->xvisibility;
  1431. MODBIT(win.mode, e->state != VisibilityFullyObscured, MODE_VISIBLE);
  1432. }
  1433. void
  1434. unmap(XEvent *ev)
  1435. {
  1436. win.mode &= ~MODE_VISIBLE;
  1437. }
  1438. void
  1439. xsetpointermotion(int set)
  1440. {
  1441. MODBIT(xw.attrs.event_mask, set, PointerMotionMask);
  1442. XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask, &xw.attrs);
  1443. }
  1444. void
  1445. xsetmode(int set, unsigned int flags)
  1446. {
  1447. int mode = win.mode;
  1448. MODBIT(win.mode, set, flags);
  1449. if ((win.mode & MODE_REVERSE) != (mode & MODE_REVERSE))
  1450. redraw();
  1451. }
  1452. int
  1453. xsetcursor(int cursor)
  1454. {
  1455. DEFAULT(cursor, 1);
  1456. if (!BETWEEN(cursor, 0, 6))
  1457. return 1;
  1458. win.cursor = cursor;
  1459. return 0;
  1460. }
  1461. void
  1462. xseturgency(int add)
  1463. {
  1464. XWMHints *h = XGetWMHints(xw.dpy, xw.win);
  1465. MODBIT(h->flags, add, XUrgencyHint);
  1466. XSetWMHints(xw.dpy, xw.win, h);
  1467. XFree(h);
  1468. }
  1469. void
  1470. xbell(void)
  1471. {
  1472. if (!(IS_SET(MODE_FOCUSED)))
  1473. xseturgency(1);
  1474. if (bellvolume)
  1475. XkbBell(xw.dpy, xw.win, bellvolume, (Atom)NULL);
  1476. }
  1477. void
  1478. focus(XEvent *ev)
  1479. {
  1480. XFocusChangeEvent *e = &ev->xfocus;
  1481. if (e->mode == NotifyGrab)
  1482. return;
  1483. if (ev->type == FocusIn) {
  1484. XSetICFocus(xw.xic);
  1485. win.mode |= MODE_FOCUSED;
  1486. xseturgency(0);
  1487. if (IS_SET(MODE_FOCUS))
  1488. ttywrite("\033[I", 3, 0);
  1489. } else {
  1490. XUnsetICFocus(xw.xic);
  1491. win.mode &= ~MODE_FOCUSED;
  1492. if (IS_SET(MODE_FOCUS))
  1493. ttywrite("\033[O", 3, 0);
  1494. }
  1495. }
  1496. int
  1497. match(uint mask, uint state)
  1498. {
  1499. return mask == XK_ANY_MOD || mask == (state & ~ignoremod);
  1500. }
  1501. char*
  1502. kmap(KeySym k, uint state)
  1503. {
  1504. Key *kp;
  1505. int i;
  1506. /* Check for mapped keys out of X11 function keys. */
  1507. for (i = 0; i < LEN(mappedkeys); i++) {
  1508. if (mappedkeys[i] == k)
  1509. break;
  1510. }
  1511. if (i == LEN(mappedkeys)) {
  1512. if ((k & 0xFFFF) < 0xFD00)
  1513. return NULL;
  1514. }
  1515. for (kp = key; kp < key + LEN(key); kp++) {
  1516. if (kp->k != k)
  1517. continue;
  1518. if (!match(kp->mask, state))
  1519. continue;
  1520. if (IS_SET(MODE_APPKEYPAD) ? kp->appkey < 0 : kp->appkey > 0)
  1521. continue;
  1522. if (IS_SET(MODE_NUMLOCK) && kp->appkey == 2)
  1523. continue;
  1524. if (IS_SET(MODE_APPCURSOR) ? kp->appcursor < 0 : kp->appcursor > 0)
  1525. continue;
  1526. return kp->s;
  1527. }
  1528. return NULL;
  1529. }
  1530. void
  1531. kpress(XEvent *ev)
  1532. {
  1533. XKeyEvent *e = &ev->xkey;
  1534. KeySym ksym;
  1535. char buf[32], *customkey;
  1536. int len;
  1537. Rune c;
  1538. Status status;
  1539. Shortcut *bp;
  1540. if (IS_SET(MODE_KBDLOCK))
  1541. return;
  1542. len = XmbLookupString(xw.xic, e, buf, sizeof buf, &ksym, &status);
  1543. /* 1. shortcuts */
  1544. for (bp = shortcuts; bp < shortcuts + LEN(shortcuts); bp++) {
  1545. if (ksym == bp->keysym && match(bp->mod, e->state)) {
  1546. bp->func(&(bp->arg));
  1547. return;
  1548. }
  1549. }
  1550. /* 2. custom keys from config.h */
  1551. if ((customkey = kmap(ksym, e->state))) {
  1552. ttywrite(customkey, strlen(customkey), 1);
  1553. return;
  1554. }
  1555. /* 3. composed string from input method */
  1556. if (len == 0)
  1557. return;
  1558. if (len == 1 && e->state & Mod1Mask) {
  1559. if (IS_SET(MODE_8BIT)) {
  1560. if (*buf < 0177) {
  1561. c = *buf | 0x80;
  1562. len = utf8encode(c, buf);
  1563. }
  1564. } else {
  1565. buf[1] = buf[0];
  1566. buf[0] = '\033';
  1567. len = 2;
  1568. }
  1569. }
  1570. ttywrite(buf, len, 1);
  1571. }
  1572. void
  1573. cmessage(XEvent *e)
  1574. {
  1575. /*
  1576. * See xembed specs
  1577. * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
  1578. */
  1579. if (e->xclient.message_type == xw.xembed && e->xclient.format == 32) {
  1580. if (e->xclient.data.l[1] == XEMBED_FOCUS_IN) {
  1581. win.mode |= MODE_FOCUSED;
  1582. xseturgency(0);
  1583. } else if (e->xclient.data.l[1] == XEMBED_FOCUS_OUT) {
  1584. win.mode &= ~MODE_FOCUSED;
  1585. }
  1586. } else if (e->xclient.data.l[0] == xw.wmdeletewin) {
  1587. ttyhangup();
  1588. exit(0);
  1589. }
  1590. }
  1591. void
  1592. resize(XEvent *e)
  1593. {
  1594. if (e->xconfigure.width == win.w && e->xconfigure.height == win.h)
  1595. return;
  1596. cresize(e->xconfigure.width, e->xconfigure.height);
  1597. }
  1598. void
  1599. run(void)
  1600. {
  1601. XEvent ev;
  1602. int w = win.w, h = win.h;
  1603. fd_set rfd;
  1604. int xfd = XConnectionNumber(xw.dpy), xev, blinkset = 0, dodraw = 0;
  1605. int ttyfd;
  1606. struct timespec drawtimeout, *tv = NULL, now, last, lastblink;
  1607. long deltatime;
  1608. /* Waiting for window mapping */
  1609. do {
  1610. XNextEvent(xw.dpy, &ev);
  1611. /*
  1612. * This XFilterEvent call is required because of XOpenIM. It
  1613. * does filter out the key event and some client message for
  1614. * the input method too.
  1615. */
  1616. if (XFilterEvent(&ev, None))
  1617. continue;
  1618. if (ev.type == ConfigureNotify) {
  1619. w = ev.xconfigure.width;
  1620. h = ev.xconfigure.height;
  1621. }
  1622. } while (ev.type != MapNotify);
  1623. ttyfd = ttynew(opt_line, shell, opt_io, opt_cmd);
  1624. cresize(w, h);
  1625. clock_gettime(CLOCK_MONOTONIC, &last);
  1626. lastblink = last;
  1627. for (xev = actionfps;;) {
  1628. FD_ZERO(&rfd);
  1629. FD_SET(ttyfd, &rfd);
  1630. FD_SET(xfd, &rfd);
  1631. if (pselect(MAX(xfd, ttyfd)+1, &rfd, NULL, NULL, tv, NULL) < 0) {
  1632. if (errno == EINTR)
  1633. continue;
  1634. die("select failed: %s\n", strerror(errno));
  1635. }
  1636. if (FD_ISSET(ttyfd, &rfd)) {
  1637. ttyread();
  1638. if (blinktimeout) {
  1639. blinkset = tattrset(ATTR_BLINK);
  1640. if (!blinkset)
  1641. MODBIT(win.mode, 0, MODE_BLINK);
  1642. }
  1643. }
  1644. if (FD_ISSET(xfd, &rfd))
  1645. xev = actionfps;
  1646. clock_gettime(CLOCK_MONOTONIC, &now);
  1647. drawtimeout.tv_sec = 0;
  1648. drawtimeout.tv_nsec = (1000 * 1E6)/ xfps;
  1649. tv = &drawtimeout;
  1650. dodraw = 0;
  1651. if (blinktimeout && TIMEDIFF(now, lastblink) > blinktimeout) {
  1652. tsetdirtattr(ATTR_BLINK);
  1653. win.mode ^= MODE_BLINK;
  1654. lastblink = now;
  1655. dodraw = 1;
  1656. }
  1657. deltatime = TIMEDIFF(now, last);
  1658. if (deltatime > 1000 / (xev ? xfps : actionfps)) {
  1659. dodraw = 1;
  1660. last = now;
  1661. }
  1662. if (dodraw) {
  1663. while (XPending(xw.dpy)) {
  1664. XNextEvent(xw.dpy, &ev);
  1665. if (XFilterEvent(&ev, None))
  1666. continue;
  1667. if (handler[ev.type])
  1668. (handler[ev.type])(&ev);
  1669. }
  1670. draw();
  1671. XFlush(xw.dpy);
  1672. if (xev && !FD_ISSET(xfd, &rfd))
  1673. xev--;
  1674. if (!FD_ISSET(ttyfd, &rfd) && !FD_ISSET(xfd, &rfd)) {
  1675. if (blinkset) {
  1676. if (TIMEDIFF(now, lastblink) \
  1677. > blinktimeout) {
  1678. drawtimeout.tv_nsec = 1000;
  1679. } else {
  1680. drawtimeout.tv_nsec = (1E6 * \
  1681. (blinktimeout - \
  1682. TIMEDIFF(now,
  1683. lastblink)));
  1684. }
  1685. drawtimeout.tv_sec = \
  1686. drawtimeout.tv_nsec / 1E9;
  1687. drawtimeout.tv_nsec %= (long)1E9;
  1688. } else {
  1689. tv = NULL;
  1690. }
  1691. }
  1692. }
  1693. }
  1694. }
  1695. int
  1696. resource_load(XrmDatabase db, char *name, enum resource_type rtype, void *dst)
  1697. {
  1698. char **sdst = dst;
  1699. int *idst = dst;
  1700. float *fdst = dst;
  1701. char fullname[256];
  1702. char fullclass[256];
  1703. char *type;
  1704. XrmValue ret;
  1705. snprintf(fullname, sizeof(fullname), "%s.%s",
  1706. opt_name ? opt_name : "st", name);
  1707. snprintf(fullclass, sizeof(fullclass), "%s.%s",
  1708. opt_class ? opt_class : "St", name);
  1709. fullname[sizeof(fullname) - 1] = fullclass[sizeof(fullclass) - 1] = '\0';
  1710. XrmGetResource(db, fullname, fullclass, &type, &ret);
  1711. if (ret.addr == NULL || strncmp("String", type, 64))
  1712. return 1;
  1713. switch (rtype) {
  1714. case STRING:
  1715. *sdst = ret.addr;
  1716. break;
  1717. case INTEGER:
  1718. *idst = strtoul(ret.addr, NULL, 10);
  1719. break;
  1720. case FLOAT:
  1721. *fdst = strtof(ret.addr, NULL);
  1722. break;
  1723. }
  1724. return 0;
  1725. }
  1726. void
  1727. config_init(void)
  1728. {
  1729. char *resm;
  1730. XrmDatabase db;
  1731. ResourcePref *p;
  1732. XrmInitialize();
  1733. resm = XResourceManagerString(xw.dpy);
  1734. if (!resm)
  1735. return;
  1736. db = XrmGetStringDatabase(resm);
  1737. for (p = resources; p < resources + LEN(resources); p++)
  1738. resource_load(db, p->name, p->type, p->dst);
  1739. }
  1740. void
  1741. usage(void)
  1742. {
  1743. die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]"
  1744. " [-n name] [-o file]\n"
  1745. " [-T title] [-t title] [-w windowid]"
  1746. " [[-e] command [args ...]]\n"
  1747. " %s [-aiv] [-c class] [-f font] [-g geometry]"
  1748. " [-n name] [-o file]\n"
  1749. " [-T title] [-t title] [-w windowid] -l line"
  1750. " [stty_args ...]\n", argv0, argv0);
  1751. }
  1752. int
  1753. main(int argc, char *argv[])
  1754. {
  1755. xw.l = xw.t = 0;
  1756. xw.isfixed = False;
  1757. win.cursor = cursorshape;
  1758. ARGBEGIN {
  1759. case 'a':
  1760. allowaltscreen = 0;
  1761. break;
  1762. case 'c':
  1763. opt_class = EARGF(usage());
  1764. break;
  1765. case 'e':
  1766. if (argc > 0)
  1767. --argc, ++argv;
  1768. goto run;
  1769. case 'f':
  1770. opt_font = EARGF(usage());
  1771. break;
  1772. case 'g':
  1773. xw.gm = XParseGeometry(EARGF(usage()),
  1774. &xw.l, &xw.t, &cols, &rows);
  1775. break;
  1776. case 'i':
  1777. xw.isfixed = 1;
  1778. break;
  1779. case 'o':
  1780. opt_io = EARGF(usage());
  1781. break;
  1782. case 'l':
  1783. opt_line = EARGF(usage());
  1784. break;
  1785. case 'n':
  1786. opt_name = EARGF(usage());
  1787. break;
  1788. case 't':
  1789. case 'T':
  1790. opt_title = EARGF(usage());
  1791. break;
  1792. case 'w':
  1793. opt_embed = EARGF(usage());
  1794. break;
  1795. case 'v':
  1796. die("%s " VERSION " (c) 2010-2016 st engineers\n", argv0);
  1797. break;
  1798. default:
  1799. usage();
  1800. } ARGEND;
  1801. run:
  1802. if (argc > 0) /* eat all remaining arguments */
  1803. opt_cmd = argv;
  1804. if (!opt_title)
  1805. opt_title = (opt_line || !opt_cmd) ? "st" : opt_cmd[0];
  1806. setlocale(LC_CTYPE, "");
  1807. XSetLocaleModifiers("");
  1808. if(!(xw.dpy = XOpenDisplay(NULL)))
  1809. die("Can't open display\n");
  1810. config_init();
  1811. cols = MAX(cols, 1);
  1812. rows = MAX(rows, 1);
  1813. tnew(cols, rows);
  1814. xinit(cols, rows);
  1815. xsetenv();
  1816. selinit();
  1817. run();
  1818. return 0;
  1819. }