x.c 44 KB

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