x.c 43 KB

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