x.c 45 KB

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