x.c 43 KB

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