st.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621
  1. /* See LICENSE for licence details. */
  2. #define _XOPEN_SOURCE 600
  3. #include <ctype.h>
  4. #include <errno.h>
  5. #include <fcntl.h>
  6. #include <limits.h>
  7. #include <locale.h>
  8. #include <stdarg.h>
  9. #include <stdbool.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <signal.h>
  14. #include <sys/ioctl.h>
  15. #include <sys/select.h>
  16. #include <sys/stat.h>
  17. #include <sys/time.h>
  18. #include <sys/types.h>
  19. #include <sys/wait.h>
  20. #include <time.h>
  21. #include <unistd.h>
  22. #include <X11/Xatom.h>
  23. #include <X11/Xlib.h>
  24. #include <X11/Xutil.h>
  25. #include <X11/cursorfont.h>
  26. #include <X11/keysym.h>
  27. #include <X11/extensions/Xdbe.h>
  28. #include <X11/Xft/Xft.h>
  29. #include <fontconfig/fontconfig.h>
  30. #define Glyph Glyph_
  31. #define Font Font_
  32. #if defined(__linux)
  33. #include <pty.h>
  34. #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
  35. #include <util.h>
  36. #elif defined(__FreeBSD__) || defined(__DragonFly__)
  37. #include <libutil.h>
  38. #endif
  39. #define USAGE \
  40. "st " VERSION " (c) 2010-2012 st engineers\n" \
  41. "usage: st [-v] [-c class] [-f font] [-g geometry] [-o file]" \
  42. " [-t title] [-w windowid] [-e command ...]\n"
  43. /* XEMBED messages */
  44. #define XEMBED_FOCUS_IN 4
  45. #define XEMBED_FOCUS_OUT 5
  46. /* Arbitrary sizes */
  47. #define ESC_BUF_SIZ 256
  48. #define ESC_ARG_SIZ 16
  49. #define STR_BUF_SIZ 256
  50. #define STR_ARG_SIZ 16
  51. #define DRAW_BUF_SIZ 20*1024
  52. #define UTF_SIZ 4
  53. #define XK_NO_MOD UINT_MAX
  54. #define XK_ANY_MOD 0
  55. #define REDRAW_TIMEOUT (80*1000) /* 80 ms */
  56. #define SERRNO strerror(errno)
  57. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  58. #define MAX(a, b) ((a) < (b) ? (b) : (a))
  59. #define LEN(a) (sizeof(a) / sizeof(a[0]))
  60. #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
  61. #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
  62. #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
  63. #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
  64. #define IS_SET(flag) (term.mode & (flag))
  65. #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000)
  66. #define X2COL(x) (((x) - BORDER)/xw.cw)
  67. #define Y2ROW(y) (((y) - BORDER)/xw.ch)
  68. enum glyph_attribute {
  69. ATTR_NULL = 0,
  70. ATTR_REVERSE = 1,
  71. ATTR_UNDERLINE = 2,
  72. ATTR_BOLD = 4,
  73. ATTR_GFX = 8,
  74. ATTR_ITALIC = 16,
  75. ATTR_BLINK = 32,
  76. };
  77. enum cursor_movement {
  78. CURSOR_UP,
  79. CURSOR_DOWN,
  80. CURSOR_LEFT,
  81. CURSOR_RIGHT,
  82. CURSOR_SAVE,
  83. CURSOR_LOAD
  84. };
  85. enum cursor_state {
  86. CURSOR_DEFAULT = 0,
  87. CURSOR_HIDE = 1,
  88. CURSOR_WRAPNEXT = 2
  89. };
  90. enum glyph_state {
  91. GLYPH_SET = 1,
  92. GLYPH_DIRTY = 2
  93. };
  94. enum term_mode {
  95. MODE_WRAP = 1,
  96. MODE_INSERT = 2,
  97. MODE_APPKEYPAD = 4,
  98. MODE_ALTSCREEN = 8,
  99. MODE_CRLF = 16,
  100. MODE_MOUSEBTN = 32,
  101. MODE_MOUSEMOTION = 64,
  102. MODE_MOUSE = 32|64,
  103. MODE_REVERSE = 128,
  104. MODE_KBDLOCK = 256
  105. };
  106. enum escape_state {
  107. ESC_START = 1,
  108. ESC_CSI = 2,
  109. ESC_STR = 4, /* DSC, OSC, PM, APC */
  110. ESC_ALTCHARSET = 8,
  111. ESC_STR_END = 16, /* a final string was encountered */
  112. };
  113. enum window_state {
  114. WIN_VISIBLE = 1,
  115. WIN_REDRAW = 2,
  116. WIN_FOCUSED = 4
  117. };
  118. /* bit macro */
  119. #undef B0
  120. enum { B0=1, B1=2, B2=4, B3=8, B4=16, B5=32, B6=64, B7=128 };
  121. typedef unsigned char uchar;
  122. typedef unsigned int uint;
  123. typedef unsigned long ulong;
  124. typedef unsigned short ushort;
  125. typedef struct {
  126. char c[UTF_SIZ]; /* character code */
  127. uchar mode; /* attribute flags */
  128. ushort fg; /* foreground */
  129. ushort bg; /* background */
  130. uchar state; /* state flags */
  131. } Glyph;
  132. typedef Glyph* Line;
  133. typedef struct {
  134. Glyph attr; /* current char attributes */
  135. int x;
  136. int y;
  137. char state;
  138. } TCursor;
  139. /* CSI Escape sequence structs */
  140. /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
  141. typedef struct {
  142. char buf[ESC_BUF_SIZ]; /* raw string */
  143. int len; /* raw string length */
  144. char priv;
  145. int arg[ESC_ARG_SIZ];
  146. int narg; /* nb of args */
  147. char mode;
  148. } CSIEscape;
  149. /* STR Escape sequence structs */
  150. /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
  151. typedef struct {
  152. char type; /* ESC type ... */
  153. char buf[STR_BUF_SIZ]; /* raw string */
  154. int len; /* raw string length */
  155. char *args[STR_ARG_SIZ];
  156. int narg; /* nb of args */
  157. } STREscape;
  158. /* Internal representation of the screen */
  159. typedef struct {
  160. int row; /* nb row */
  161. int col; /* nb col */
  162. Line* line; /* screen */
  163. Line* alt; /* alternate screen */
  164. bool* dirty; /* dirtyness of lines */
  165. TCursor c; /* cursor */
  166. int top; /* top scroll limit */
  167. int bot; /* bottom scroll limit */
  168. int mode; /* terminal mode flags */
  169. int esc; /* escape state flags */
  170. bool *tabs;
  171. } Term;
  172. /* Purely graphic info */
  173. typedef struct {
  174. Display* dpy;
  175. Colormap cmap;
  176. Window win;
  177. XdbeBackBuffer buf;
  178. Atom xembed;
  179. XIM xim;
  180. XIC xic;
  181. XftDraw *xft_draw;
  182. Visual *vis;
  183. int scr;
  184. bool isfixed; /* is fixed geometry? */
  185. int fx, fy, fw, fh; /* fixed geometry */
  186. int tw, th; /* tty width and height */
  187. int w; /* window width */
  188. int h; /* window height */
  189. int ch; /* char height */
  190. int cw; /* char width */
  191. char state; /* focus, redraw, visible */
  192. } XWindow;
  193. typedef struct {
  194. KeySym k;
  195. uint mask;
  196. char s[ESC_BUF_SIZ];
  197. } Key;
  198. /* TODO: use better name for vars... */
  199. typedef struct {
  200. int mode;
  201. int bx, by;
  202. int ex, ey;
  203. struct {int x, y;} b, e;
  204. char *clip;
  205. Atom xtarget;
  206. bool alt;
  207. struct timeval tclick1;
  208. struct timeval tclick2;
  209. } Selection;
  210. #include "config.h"
  211. /* Font structure */
  212. typedef struct {
  213. int height;
  214. int width;
  215. int ascent;
  216. int descent;
  217. short lbearing;
  218. short rbearing;
  219. XftFont* xft_set;
  220. } Font;
  221. /* Drawing Context */
  222. typedef struct {
  223. XftColor xft_col[LEN(colorname) < 256 ? 256 : LEN(colorname)];
  224. GC gc;
  225. Font font, bfont, ifont, ibfont;
  226. } DC;
  227. static void die(const char*, ...);
  228. static void draw(void);
  229. static void redraw(void);
  230. static void drawregion(int, int, int, int);
  231. static void execsh(void);
  232. static void sigchld(int);
  233. static void run(void);
  234. static void csidump(void);
  235. static void csihandle(void);
  236. static void csiparse(void);
  237. static void csireset(void);
  238. static void strdump(void);
  239. static void strhandle(void);
  240. static void strparse(void);
  241. static void strreset(void);
  242. static void tclearregion(int, int, int, int);
  243. static void tcursor(int);
  244. static void tdeletechar(int);
  245. static void tdeleteline(int);
  246. static void tinsertblank(int);
  247. static void tinsertblankline(int);
  248. static void tmoveto(int, int);
  249. static void tnew(int, int);
  250. static void tnewline(int);
  251. static void tputtab(bool);
  252. static void tputc(char*, int);
  253. static void treset(void);
  254. static int tresize(int, int);
  255. static void tscrollup(int, int);
  256. static void tscrolldown(int, int);
  257. static void tsetattr(int*, int);
  258. static void tsetchar(char*);
  259. static void tsetscroll(int, int);
  260. static void tswapscreen(void);
  261. static void tsetdirt(int, int);
  262. static void tsetmode(bool, bool, int *, int);
  263. static void tfulldirt(void);
  264. static void ttynew(void);
  265. static void ttyread(void);
  266. static void ttyresize(void);
  267. static void ttywrite(const char *, size_t);
  268. static void xdraws(char *, Glyph, int, int, int, int);
  269. static void xhints(void);
  270. static void xclear(int, int, int, int);
  271. static void xclearborders(void);
  272. static void xdrawcursor(void);
  273. static void xinit(void);
  274. static void xloadcols(void);
  275. static void xresettitle(void);
  276. static void xseturgency(int);
  277. static void xsetsel(char*);
  278. static void xtermclear(int, int, int, int);
  279. static void xresize(int, int);
  280. static void expose(XEvent *);
  281. static void visibility(XEvent *);
  282. static void unmap(XEvent *);
  283. static char* kmap(KeySym, uint);
  284. static void kpress(XEvent *);
  285. static void cmessage(XEvent *);
  286. static void resize(XEvent *);
  287. static void focus(XEvent *);
  288. static void brelease(XEvent *);
  289. static void bpress(XEvent *);
  290. static void bmotion(XEvent *);
  291. static void selnotify(XEvent *);
  292. static void selclear(XEvent *);
  293. static void selrequest(XEvent *);
  294. static void selinit(void);
  295. static inline bool selected(int, int);
  296. static void selcopy(void);
  297. static void selpaste(void);
  298. static void selscroll(int, int);
  299. static int utf8decode(char *, long *);
  300. static int utf8encode(long *, char *);
  301. static int utf8size(char *);
  302. static int isfullutf8(char *, int);
  303. static void *xmalloc(size_t);
  304. static void *xrealloc(void *, size_t);
  305. static void *xcalloc(size_t nmemb, size_t size);
  306. static char *smstrcat(char *, ...);
  307. static void (*handler[LASTEvent])(XEvent *) = {
  308. [KeyPress] = kpress,
  309. [ClientMessage] = cmessage,
  310. [ConfigureNotify] = resize,
  311. [VisibilityNotify] = visibility,
  312. [UnmapNotify] = unmap,
  313. [Expose] = expose,
  314. [FocusIn] = focus,
  315. [FocusOut] = focus,
  316. [MotionNotify] = bmotion,
  317. [ButtonPress] = bpress,
  318. [ButtonRelease] = brelease,
  319. [SelectionClear] = selclear,
  320. [SelectionNotify] = selnotify,
  321. [SelectionRequest] = selrequest,
  322. };
  323. /* Globals */
  324. static DC dc;
  325. static XWindow xw;
  326. static Term term;
  327. static CSIEscape csiescseq;
  328. static STREscape strescseq;
  329. static int cmdfd;
  330. static pid_t pid;
  331. static Selection sel;
  332. static int iofd = -1;
  333. static char **opt_cmd = NULL;
  334. static char *opt_io = NULL;
  335. static char *opt_title = NULL;
  336. static char *opt_embed = NULL;
  337. static char *opt_class = NULL;
  338. static char *opt_font = NULL;
  339. void *
  340. xmalloc(size_t len) {
  341. void *p = malloc(len);
  342. if(!p)
  343. die("Out of memory\n");
  344. return p;
  345. }
  346. void *
  347. xrealloc(void *p, size_t len) {
  348. if((p = realloc(p, len)) == NULL)
  349. die("Out of memory\n");
  350. return p;
  351. }
  352. void *
  353. xcalloc(size_t nmemb, size_t size) {
  354. void *p = calloc(nmemb, size);
  355. if(!p)
  356. die("Out of memory\n");
  357. return p;
  358. }
  359. char *
  360. smstrcat(char *src, ...)
  361. {
  362. va_list fmtargs;
  363. char *ret, *p, *v;
  364. int len, slen, flen;
  365. len = slen = strlen(src);
  366. va_start(fmtargs, src);
  367. for(;;) {
  368. v = va_arg(fmtargs, char *);
  369. if(v == NULL)
  370. break;
  371. len += strlen(v);
  372. }
  373. va_end(fmtargs);
  374. p = ret = xmalloc(len+1);
  375. memmove(p, src, slen);
  376. p += slen;
  377. va_start(fmtargs, src);
  378. for(;;) {
  379. v = va_arg(fmtargs, char *);
  380. if(v == NULL)
  381. break;
  382. flen = strlen(v);
  383. memmove(p, v, flen);
  384. p += flen;
  385. }
  386. va_end(fmtargs);
  387. ret[len] = '\0';
  388. return ret;
  389. }
  390. int
  391. utf8decode(char *s, long *u) {
  392. uchar c;
  393. int i, n, rtn;
  394. rtn = 1;
  395. c = *s;
  396. if(~c & B7) { /* 0xxxxxxx */
  397. *u = c;
  398. return rtn;
  399. } else if((c & (B7|B6|B5)) == (B7|B6)) { /* 110xxxxx */
  400. *u = c&(B4|B3|B2|B1|B0);
  401. n = 1;
  402. } else if((c & (B7|B6|B5|B4)) == (B7|B6|B5)) { /* 1110xxxx */
  403. *u = c&(B3|B2|B1|B0);
  404. n = 2;
  405. } else if((c & (B7|B6|B5|B4|B3)) == (B7|B6|B5|B4)) { /* 11110xxx */
  406. *u = c & (B2|B1|B0);
  407. n = 3;
  408. } else
  409. goto invalid;
  410. for(i = n, ++s; i > 0; --i, ++rtn, ++s) {
  411. c = *s;
  412. if((c & (B7|B6)) != B7) /* 10xxxxxx */
  413. goto invalid;
  414. *u <<= 6;
  415. *u |= c & (B5|B4|B3|B2|B1|B0);
  416. }
  417. if((n == 1 && *u < 0x80) ||
  418. (n == 2 && *u < 0x800) ||
  419. (n == 3 && *u < 0x10000) ||
  420. (*u >= 0xD800 && *u <= 0xDFFF))
  421. goto invalid;
  422. return rtn;
  423. invalid:
  424. *u = 0xFFFD;
  425. return rtn;
  426. }
  427. int
  428. utf8encode(long *u, char *s) {
  429. uchar *sp;
  430. ulong uc;
  431. int i, n;
  432. sp = (uchar*) s;
  433. uc = *u;
  434. if(uc < 0x80) {
  435. *sp = uc; /* 0xxxxxxx */
  436. return 1;
  437. } else if(*u < 0x800) {
  438. *sp = (uc >> 6) | (B7|B6); /* 110xxxxx */
  439. n = 1;
  440. } else if(uc < 0x10000) {
  441. *sp = (uc >> 12) | (B7|B6|B5); /* 1110xxxx */
  442. n = 2;
  443. } else if(uc <= 0x10FFFF) {
  444. *sp = (uc >> 18) | (B7|B6|B5|B4); /* 11110xxx */
  445. n = 3;
  446. } else {
  447. goto invalid;
  448. }
  449. for(i=n,++sp; i>0; --i,++sp)
  450. *sp = ((uc >> 6*(i-1)) & (B5|B4|B3|B2|B1|B0)) | B7; /* 10xxxxxx */
  451. return n+1;
  452. invalid:
  453. /* U+FFFD */
  454. *s++ = '\xEF';
  455. *s++ = '\xBF';
  456. *s = '\xBD';
  457. return 3;
  458. }
  459. /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
  460. UTF-8 otherwise return 0 */
  461. int
  462. isfullutf8(char *s, int b) {
  463. uchar *c1, *c2, *c3;
  464. c1 = (uchar *) s;
  465. c2 = (uchar *) ++s;
  466. c3 = (uchar *) ++s;
  467. if(b < 1)
  468. return 0;
  469. else if((*c1&(B7|B6|B5)) == (B7|B6) && b == 1)
  470. return 0;
  471. else if((*c1&(B7|B6|B5|B4)) == (B7|B6|B5) &&
  472. ((b == 1) ||
  473. ((b == 2) && (*c2&(B7|B6)) == B7)))
  474. return 0;
  475. else if((*c1&(B7|B6|B5|B4|B3)) == (B7|B6|B5|B4) &&
  476. ((b == 1) ||
  477. ((b == 2) && (*c2&(B7|B6)) == B7) ||
  478. ((b == 3) && (*c2&(B7|B6)) == B7 && (*c3&(B7|B6)) == B7)))
  479. return 0;
  480. else
  481. return 1;
  482. }
  483. int
  484. utf8size(char *s) {
  485. uchar c = *s;
  486. if(~c&B7)
  487. return 1;
  488. else if((c&(B7|B6|B5)) == (B7|B6))
  489. return 2;
  490. else if((c&(B7|B6|B5|B4)) == (B7|B6|B5))
  491. return 3;
  492. else
  493. return 4;
  494. }
  495. void
  496. selinit(void) {
  497. memset(&sel.tclick1, 0, sizeof(sel.tclick1));
  498. memset(&sel.tclick2, 0, sizeof(sel.tclick2));
  499. sel.mode = 0;
  500. sel.bx = -1;
  501. sel.clip = NULL;
  502. sel.xtarget = XInternAtom(xw.dpy, "UTF8_STRING", 0);
  503. if(sel.xtarget == None)
  504. sel.xtarget = XA_STRING;
  505. }
  506. static inline bool
  507. selected(int x, int y) {
  508. if(sel.ey == y && sel.by == y) {
  509. int bx = MIN(sel.bx, sel.ex);
  510. int ex = MAX(sel.bx, sel.ex);
  511. return BETWEEN(x, bx, ex);
  512. }
  513. return ((sel.b.y < y&&y < sel.e.y) || (y==sel.e.y && x<=sel.e.x))
  514. || (y==sel.b.y && x>=sel.b.x && (x<=sel.e.x || sel.b.y!=sel.e.y));
  515. }
  516. void
  517. getbuttoninfo(XEvent *e, int *b, int *x, int *y) {
  518. if(b)
  519. *b = e->xbutton.button;
  520. *x = X2COL(e->xbutton.x);
  521. *y = Y2ROW(e->xbutton.y);
  522. sel.b.x = sel.by < sel.ey ? sel.bx : sel.ex;
  523. sel.b.y = MIN(sel.by, sel.ey);
  524. sel.e.x = sel.by < sel.ey ? sel.ex : sel.bx;
  525. sel.e.y = MAX(sel.by, sel.ey);
  526. }
  527. void
  528. mousereport(XEvent *e) {
  529. int x = X2COL(e->xbutton.x);
  530. int y = Y2ROW(e->xbutton.y);
  531. int button = e->xbutton.button;
  532. int state = e->xbutton.state;
  533. char buf[] = { '\033', '[', 'M', 0, 32+x+1, 32+y+1 };
  534. static int ob, ox, oy;
  535. /* from urxvt */
  536. if(e->xbutton.type == MotionNotify) {
  537. if(!IS_SET(MODE_MOUSEMOTION) || (x == ox && y == oy))
  538. return;
  539. button = ob + 32;
  540. ox = x, oy = y;
  541. } else if(e->xbutton.type == ButtonRelease || button == AnyButton) {
  542. button = 3;
  543. } else {
  544. button -= Button1;
  545. if(button >= 3)
  546. button += 64 - 3;
  547. if(e->xbutton.type == ButtonPress) {
  548. ob = button;
  549. ox = x, oy = y;
  550. }
  551. }
  552. buf[3] = 32 + button + (state & ShiftMask ? 4 : 0)
  553. + (state & Mod4Mask ? 8 : 0)
  554. + (state & ControlMask ? 16 : 0);
  555. ttywrite(buf, sizeof(buf));
  556. }
  557. void
  558. bpress(XEvent *e) {
  559. if(IS_SET(MODE_MOUSE))
  560. mousereport(e);
  561. else if(e->xbutton.button == Button1) {
  562. if(sel.bx != -1) {
  563. sel.bx = -1;
  564. tsetdirt(sel.b.y, sel.e.y);
  565. draw();
  566. }
  567. sel.mode = 1;
  568. sel.ex = sel.bx = X2COL(e->xbutton.x);
  569. sel.ey = sel.by = Y2ROW(e->xbutton.y);
  570. }
  571. }
  572. void
  573. selcopy(void) {
  574. char *str, *ptr;
  575. int x, y, bufsize, is_selected = 0;
  576. if(sel.bx == -1)
  577. str = NULL;
  578. else {
  579. bufsize = (term.col+1) * (sel.e.y-sel.b.y+1) * UTF_SIZ;
  580. ptr = str = xmalloc(bufsize);
  581. /* append every set & selected glyph to the selection */
  582. for(y = 0; y < term.row; y++) {
  583. for(x = 0; x < term.col; x++) {
  584. int size;
  585. char *p;
  586. Glyph *gp = &term.line[y][x];
  587. if(!(is_selected = selected(x, y)))
  588. continue;
  589. p = (gp->state & GLYPH_SET) ? gp->c : " ";
  590. size = utf8size(p);
  591. memcpy(ptr, p, size);
  592. ptr += size;
  593. }
  594. /* \n at the end of every selected line except for the last one */
  595. if(is_selected && y < sel.e.y)
  596. *ptr++ = '\n';
  597. }
  598. *ptr = 0;
  599. }
  600. sel.alt = IS_SET(MODE_ALTSCREEN);
  601. xsetsel(str);
  602. }
  603. void
  604. selnotify(XEvent *e) {
  605. ulong nitems, ofs, rem;
  606. int format;
  607. uchar *data;
  608. Atom type;
  609. ofs = 0;
  610. do {
  611. if(XGetWindowProperty(xw.dpy, xw.win, XA_PRIMARY, ofs, BUFSIZ/4,
  612. False, AnyPropertyType, &type, &format,
  613. &nitems, &rem, &data)) {
  614. fprintf(stderr, "Clipboard allocation failed\n");
  615. return;
  616. }
  617. ttywrite((const char *) data, nitems * format / 8);
  618. XFree(data);
  619. /* number of 32-bit chunks returned */
  620. ofs += nitems * format / 32;
  621. } while(rem > 0);
  622. }
  623. void
  624. selpaste() {
  625. XConvertSelection(xw.dpy, XA_PRIMARY, sel.xtarget, XA_PRIMARY, xw.win, CurrentTime);
  626. }
  627. void selclear(XEvent *e) {
  628. if(sel.bx == -1)
  629. return;
  630. sel.bx = -1;
  631. tsetdirt(sel.b.y, sel.e.y);
  632. }
  633. void
  634. selrequest(XEvent *e) {
  635. XSelectionRequestEvent *xsre;
  636. XSelectionEvent xev;
  637. Atom xa_targets;
  638. xsre = (XSelectionRequestEvent *) e;
  639. xev.type = SelectionNotify;
  640. xev.requestor = xsre->requestor;
  641. xev.selection = xsre->selection;
  642. xev.target = xsre->target;
  643. xev.time = xsre->time;
  644. /* reject */
  645. xev.property = None;
  646. xa_targets = XInternAtom(xw.dpy, "TARGETS", 0);
  647. if(xsre->target == xa_targets) {
  648. /* respond with the supported type */
  649. Atom string = sel.xtarget;
  650. XChangeProperty(xsre->display, xsre->requestor, xsre->property,
  651. XA_ATOM, 32, PropModeReplace,
  652. (uchar *) &string, 1);
  653. xev.property = xsre->property;
  654. } else if(xsre->target == sel.xtarget && sel.clip != NULL) {
  655. XChangeProperty(xsre->display, xsre->requestor, xsre->property,
  656. xsre->target, 8, PropModeReplace,
  657. (uchar *) sel.clip, strlen(sel.clip));
  658. xev.property = xsre->property;
  659. }
  660. /* all done, send a notification to the listener */
  661. if(!XSendEvent(xsre->display, xsre->requestor, True, 0, (XEvent *) &xev))
  662. fprintf(stderr, "Error sending SelectionNotify event\n");
  663. }
  664. void
  665. xsetsel(char *str) {
  666. /* register the selection for both the clipboard and the primary */
  667. Atom clipboard;
  668. free(sel.clip);
  669. sel.clip = str;
  670. XSetSelectionOwner(xw.dpy, XA_PRIMARY, xw.win, CurrentTime);
  671. clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
  672. XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
  673. }
  674. void
  675. brelease(XEvent *e) {
  676. if(IS_SET(MODE_MOUSE)) {
  677. mousereport(e);
  678. return;
  679. }
  680. if(e->xbutton.button == Button2)
  681. selpaste();
  682. else if(e->xbutton.button == Button1) {
  683. sel.mode = 0;
  684. getbuttoninfo(e, NULL, &sel.ex, &sel.ey);
  685. term.dirty[sel.ey] = 1;
  686. if(sel.bx == sel.ex && sel.by == sel.ey) {
  687. struct timeval now;
  688. sel.bx = -1;
  689. gettimeofday(&now, NULL);
  690. if(TIMEDIFF(now, sel.tclick2) <= TRIPLECLICK_TIMEOUT) {
  691. /* triple click on the line */
  692. sel.b.x = sel.bx = 0;
  693. sel.e.x = sel.ex = term.col;
  694. sel.b.y = sel.e.y = sel.ey;
  695. selcopy();
  696. } else if(TIMEDIFF(now, sel.tclick1) <= DOUBLECLICK_TIMEOUT) {
  697. /* double click to select word */
  698. sel.bx = sel.ex;
  699. while(sel.bx > 0 && term.line[sel.ey][sel.bx-1].state & GLYPH_SET &&
  700. term.line[sel.ey][sel.bx-1].c[0] != ' ') sel.bx--;
  701. sel.b.x = sel.bx;
  702. while(sel.ex < term.col-1 && term.line[sel.ey][sel.ex+1].state & GLYPH_SET &&
  703. term.line[sel.ey][sel.ex+1].c[0] != ' ') sel.ex++;
  704. sel.e.x = sel.ex;
  705. sel.b.y = sel.e.y = sel.ey;
  706. selcopy();
  707. }
  708. } else
  709. selcopy();
  710. }
  711. memcpy(&sel.tclick2, &sel.tclick1, sizeof(struct timeval));
  712. gettimeofday(&sel.tclick1, NULL);
  713. }
  714. void
  715. bmotion(XEvent *e) {
  716. if(IS_SET(MODE_MOUSE)) {
  717. mousereport(e);
  718. return;
  719. }
  720. if(sel.mode) {
  721. int oldey = sel.ey, oldex = sel.ex;
  722. getbuttoninfo(e, NULL, &sel.ex, &sel.ey);
  723. if(oldey != sel.ey || oldex != sel.ex) {
  724. int starty = MIN(oldey, sel.ey);
  725. int endy = MAX(oldey, sel.ey);
  726. tsetdirt(starty, endy);
  727. }
  728. }
  729. }
  730. void
  731. die(const char *errstr, ...) {
  732. va_list ap;
  733. va_start(ap, errstr);
  734. vfprintf(stderr, errstr, ap);
  735. va_end(ap);
  736. exit(EXIT_FAILURE);
  737. }
  738. void
  739. execsh(void) {
  740. char **args;
  741. char *envshell = getenv("SHELL");
  742. unsetenv("COLUMNS");
  743. unsetenv("LINES");
  744. unsetenv("TERMCAP");
  745. signal(SIGCHLD, SIG_DFL);
  746. signal(SIGHUP, SIG_DFL);
  747. signal(SIGINT, SIG_DFL);
  748. signal(SIGQUIT, SIG_DFL);
  749. signal(SIGTERM, SIG_DFL);
  750. signal(SIGALRM, SIG_DFL);
  751. DEFAULT(envshell, SHELL);
  752. putenv("TERM="TNAME);
  753. args = opt_cmd ? opt_cmd : (char*[]){envshell, "-i", NULL};
  754. execvp(args[0], args);
  755. exit(EXIT_FAILURE);
  756. }
  757. void
  758. sigchld(int a) {
  759. int stat = 0;
  760. if(waitpid(pid, &stat, 0) < 0)
  761. die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
  762. if(WIFEXITED(stat))
  763. exit(WEXITSTATUS(stat));
  764. else
  765. exit(EXIT_FAILURE);
  766. }
  767. void
  768. ttynew(void) {
  769. int m, s;
  770. /* seems to work fine on linux, openbsd and freebsd */
  771. struct winsize w = {term.row, term.col, 0, 0};
  772. if(openpty(&m, &s, NULL, NULL, &w) < 0)
  773. die("openpty failed: %s\n", SERRNO);
  774. switch(pid = fork()) {
  775. case -1:
  776. die("fork failed\n");
  777. break;
  778. case 0:
  779. setsid(); /* create a new process group */
  780. dup2(s, STDIN_FILENO);
  781. dup2(s, STDOUT_FILENO);
  782. dup2(s, STDERR_FILENO);
  783. if(ioctl(s, TIOCSCTTY, NULL) < 0)
  784. die("ioctl TIOCSCTTY failed: %s\n", SERRNO);
  785. close(s);
  786. close(m);
  787. execsh();
  788. break;
  789. default:
  790. close(s);
  791. cmdfd = m;
  792. signal(SIGCHLD, sigchld);
  793. if(opt_io) {
  794. if(!strcmp(opt_io, "-")) {
  795. iofd = STDOUT_FILENO;
  796. } else {
  797. if((iofd = open(opt_io, O_WRONLY | O_CREAT, 0666)) < 0) {
  798. fprintf(stderr, "Error opening %s:%s\n",
  799. opt_io, strerror(errno));
  800. }
  801. }
  802. }
  803. }
  804. }
  805. void
  806. dump(char c) {
  807. static int col;
  808. fprintf(stderr, " %02x '%c' ", c, isprint(c)?c:'.');
  809. if(++col % 10 == 0)
  810. fprintf(stderr, "\n");
  811. }
  812. void
  813. ttyread(void) {
  814. static char buf[BUFSIZ];
  815. static int buflen = 0;
  816. char *ptr;
  817. char s[UTF_SIZ];
  818. int charsize; /* size of utf8 char in bytes */
  819. long utf8c;
  820. int ret;
  821. /* append read bytes to unprocessed bytes */
  822. if((ret = read(cmdfd, buf+buflen, LEN(buf)-buflen)) < 0)
  823. die("Couldn't read from shell: %s\n", SERRNO);
  824. /* process every complete utf8 char */
  825. buflen += ret;
  826. ptr = buf;
  827. while(buflen >= UTF_SIZ || isfullutf8(ptr,buflen)) {
  828. charsize = utf8decode(ptr, &utf8c);
  829. utf8encode(&utf8c, s);
  830. tputc(s, charsize);
  831. ptr += charsize;
  832. buflen -= charsize;
  833. }
  834. /* keep any uncomplete utf8 char for the next call */
  835. memmove(buf, ptr, buflen);
  836. }
  837. void
  838. ttywrite(const char *s, size_t n) {
  839. if(write(cmdfd, s, n) == -1)
  840. die("write error on tty: %s\n", SERRNO);
  841. }
  842. void
  843. ttyresize(void) {
  844. struct winsize w;
  845. w.ws_row = term.row;
  846. w.ws_col = term.col;
  847. w.ws_xpixel = xw.tw;
  848. w.ws_ypixel = xw.th;
  849. if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
  850. fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
  851. }
  852. void
  853. tsetdirt(int top, int bot)
  854. {
  855. int i;
  856. LIMIT(top, 0, term.row-1);
  857. LIMIT(bot, 0, term.row-1);
  858. for(i = top; i <= bot; i++)
  859. term.dirty[i] = 1;
  860. }
  861. void
  862. tfulldirt(void)
  863. {
  864. tsetdirt(0, term.row-1);
  865. }
  866. void
  867. tcursor(int mode) {
  868. static TCursor c;
  869. if(mode == CURSOR_SAVE)
  870. c = term.c;
  871. else if(mode == CURSOR_LOAD)
  872. term.c = c, tmoveto(c.x, c.y);
  873. }
  874. void
  875. treset(void) {
  876. uint i;
  877. term.c = (TCursor){{
  878. .mode = ATTR_NULL,
  879. .fg = DefaultFG,
  880. .bg = DefaultBG
  881. }, .x = 0, .y = 0, .state = CURSOR_DEFAULT};
  882. memset(term.tabs, 0, term.col * sizeof(*term.tabs));
  883. for(i = TAB; i < term.col; i += TAB)
  884. term.tabs[i] = 1;
  885. term.top = 0, term.bot = term.row - 1;
  886. term.mode = MODE_WRAP;
  887. tclearregion(0, 0, term.col-1, term.row-1);
  888. }
  889. void
  890. tnew(int col, int row) {
  891. /* set screen size */
  892. term.row = row, term.col = col;
  893. term.line = xmalloc(term.row * sizeof(Line));
  894. term.alt = xmalloc(term.row * sizeof(Line));
  895. term.dirty = xmalloc(term.row * sizeof(*term.dirty));
  896. term.tabs = xmalloc(term.col * sizeof(*term.tabs));
  897. for(row = 0; row < term.row; row++) {
  898. term.line[row] = xmalloc(term.col * sizeof(Glyph));
  899. term.alt [row] = xmalloc(term.col * sizeof(Glyph));
  900. term.dirty[row] = 0;
  901. }
  902. memset(term.tabs, 0, term.col * sizeof(*term.tabs));
  903. /* setup screen */
  904. treset();
  905. }
  906. void
  907. tswapscreen(void) {
  908. Line* tmp = term.line;
  909. term.line = term.alt;
  910. term.alt = tmp;
  911. term.mode ^= MODE_ALTSCREEN;
  912. tfulldirt();
  913. }
  914. void
  915. tscrolldown(int orig, int n) {
  916. int i;
  917. Line temp;
  918. LIMIT(n, 0, term.bot-orig+1);
  919. tclearregion(0, term.bot-n+1, term.col-1, term.bot);
  920. for(i = term.bot; i >= orig+n; i--) {
  921. temp = term.line[i];
  922. term.line[i] = term.line[i-n];
  923. term.line[i-n] = temp;
  924. term.dirty[i] = 1;
  925. term.dirty[i-n] = 1;
  926. }
  927. selscroll(orig, n);
  928. }
  929. void
  930. tscrollup(int orig, int n) {
  931. int i;
  932. Line temp;
  933. LIMIT(n, 0, term.bot-orig+1);
  934. tclearregion(0, orig, term.col-1, orig+n-1);
  935. for(i = orig; i <= term.bot-n; i++) {
  936. temp = term.line[i];
  937. term.line[i] = term.line[i+n];
  938. term.line[i+n] = temp;
  939. term.dirty[i] = 1;
  940. term.dirty[i+n] = 1;
  941. }
  942. selscroll(orig, -n);
  943. }
  944. void
  945. selscroll(int orig, int n) {
  946. if(sel.bx == -1)
  947. return;
  948. if(BETWEEN(sel.by, orig, term.bot) || BETWEEN(sel.ey, orig, term.bot)) {
  949. if((sel.by += n) > term.bot || (sel.ey += n) < term.top) {
  950. sel.bx = -1;
  951. return;
  952. }
  953. if(sel.by < term.top) {
  954. sel.by = term.top;
  955. sel.bx = 0;
  956. }
  957. if(sel.ey > term.bot) {
  958. sel.ey = term.bot;
  959. sel.ex = term.col;
  960. }
  961. sel.b.y = sel.by, sel.b.x = sel.bx;
  962. sel.e.y = sel.ey, sel.e.x = sel.ex;
  963. }
  964. }
  965. void
  966. tnewline(int first_col) {
  967. int y = term.c.y;
  968. if(y == term.bot)
  969. tscrollup(term.top, 1);
  970. else
  971. y++;
  972. tmoveto(first_col ? 0 : term.c.x, y);
  973. }
  974. void
  975. csiparse(void) {
  976. /* int noarg = 1; */
  977. char *p = csiescseq.buf;
  978. csiescseq.narg = 0;
  979. if(*p == '?')
  980. csiescseq.priv = 1, p++;
  981. while(p < csiescseq.buf+csiescseq.len) {
  982. while(isdigit(*p)) {
  983. csiescseq.arg[csiescseq.narg] *= 10;
  984. csiescseq.arg[csiescseq.narg] += *p++ - '0'/*, noarg = 0 */;
  985. }
  986. if(*p == ';' && csiescseq.narg+1 < ESC_ARG_SIZ)
  987. csiescseq.narg++, p++;
  988. else {
  989. csiescseq.mode = *p;
  990. csiescseq.narg++;
  991. return;
  992. }
  993. }
  994. }
  995. void
  996. tmoveto(int x, int y) {
  997. LIMIT(x, 0, term.col-1);
  998. LIMIT(y, 0, term.row-1);
  999. term.c.state &= ~CURSOR_WRAPNEXT;
  1000. term.c.x = x;
  1001. term.c.y = y;
  1002. }
  1003. void
  1004. tsetchar(char *c) {
  1005. /*
  1006. * The table is proudly stolen from rxvt.
  1007. */
  1008. if(term.c.attr.mode & ATTR_GFX) {
  1009. char *vt100_0[62] = { /* 0x41 - 0x7e */
  1010. "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
  1011. 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
  1012. 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
  1013. 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
  1014. "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
  1015. "␤", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
  1016. "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
  1017. "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
  1018. };
  1019. if(c[0] >= 0x41 && c[0] <= 0x7e
  1020. && vt100_0[c[0] - 0x41]) {
  1021. c = vt100_0[c[0] - 0x41];
  1022. }
  1023. }
  1024. term.dirty[term.c.y] = 1;
  1025. term.line[term.c.y][term.c.x] = term.c.attr;
  1026. memcpy(term.line[term.c.y][term.c.x].c, c, UTF_SIZ);
  1027. term.line[term.c.y][term.c.x].state |= GLYPH_SET;
  1028. }
  1029. void
  1030. tclearregion(int x1, int y1, int x2, int y2) {
  1031. int x, y, temp;
  1032. if(x1 > x2)
  1033. temp = x1, x1 = x2, x2 = temp;
  1034. if(y1 > y2)
  1035. temp = y1, y1 = y2, y2 = temp;
  1036. LIMIT(x1, 0, term.col-1);
  1037. LIMIT(x2, 0, term.col-1);
  1038. LIMIT(y1, 0, term.row-1);
  1039. LIMIT(y2, 0, term.row-1);
  1040. for(y = y1; y <= y2; y++) {
  1041. term.dirty[y] = 1;
  1042. for(x = x1; x <= x2; x++)
  1043. term.line[y][x].state = 0;
  1044. }
  1045. }
  1046. void
  1047. tdeletechar(int n) {
  1048. int src = term.c.x + n;
  1049. int dst = term.c.x;
  1050. int size = term.col - src;
  1051. term.dirty[term.c.y] = 1;
  1052. if(src >= term.col) {
  1053. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  1054. return;
  1055. }
  1056. memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
  1057. tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
  1058. }
  1059. void
  1060. tinsertblank(int n) {
  1061. int src = term.c.x;
  1062. int dst = src + n;
  1063. int size = term.col - dst;
  1064. term.dirty[term.c.y] = 1;
  1065. if(dst >= term.col) {
  1066. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  1067. return;
  1068. }
  1069. memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
  1070. tclearregion(src, term.c.y, dst - 1, term.c.y);
  1071. }
  1072. void
  1073. tinsertblankline(int n) {
  1074. if(term.c.y < term.top || term.c.y > term.bot)
  1075. return;
  1076. tscrolldown(term.c.y, n);
  1077. }
  1078. void
  1079. tdeleteline(int n) {
  1080. if(term.c.y < term.top || term.c.y > term.bot)
  1081. return;
  1082. tscrollup(term.c.y, n);
  1083. }
  1084. void
  1085. tsetattr(int *attr, int l) {
  1086. int i;
  1087. for(i = 0; i < l; i++) {
  1088. switch(attr[i]) {
  1089. case 0:
  1090. term.c.attr.mode &= ~(ATTR_REVERSE | ATTR_UNDERLINE | ATTR_BOLD \
  1091. | ATTR_ITALIC | ATTR_BLINK);
  1092. term.c.attr.fg = DefaultFG;
  1093. term.c.attr.bg = DefaultBG;
  1094. break;
  1095. case 1:
  1096. term.c.attr.mode |= ATTR_BOLD;
  1097. break;
  1098. case 3: /* enter standout (highlight) */
  1099. term.c.attr.mode |= ATTR_ITALIC;
  1100. break;
  1101. case 4:
  1102. term.c.attr.mode |= ATTR_UNDERLINE;
  1103. break;
  1104. case 5:
  1105. term.c.attr.mode |= ATTR_BLINK;
  1106. break;
  1107. case 7:
  1108. term.c.attr.mode |= ATTR_REVERSE;
  1109. break;
  1110. case 21:
  1111. case 22:
  1112. term.c.attr.mode &= ~ATTR_BOLD;
  1113. break;
  1114. case 23: /* leave standout (highlight) mode */
  1115. term.c.attr.mode &= ~ATTR_ITALIC;
  1116. break;
  1117. case 24:
  1118. term.c.attr.mode &= ~ATTR_UNDERLINE;
  1119. break;
  1120. case 25:
  1121. term.c.attr.mode &= ~ATTR_BLINK;
  1122. break;
  1123. case 27:
  1124. term.c.attr.mode &= ~ATTR_REVERSE;
  1125. break;
  1126. case 38:
  1127. if(i + 2 < l && attr[i + 1] == 5) {
  1128. i += 2;
  1129. if(BETWEEN(attr[i], 0, 255))
  1130. term.c.attr.fg = attr[i];
  1131. else
  1132. fprintf(stderr, "erresc: bad fgcolor %d\n", attr[i]);
  1133. }
  1134. else
  1135. fprintf(stderr, "erresc(38): gfx attr %d unknown\n", attr[i]);
  1136. break;
  1137. case 39:
  1138. term.c.attr.fg = DefaultFG;
  1139. break;
  1140. case 48:
  1141. if(i + 2 < l && attr[i + 1] == 5) {
  1142. i += 2;
  1143. if(BETWEEN(attr[i], 0, 255))
  1144. term.c.attr.bg = attr[i];
  1145. else
  1146. fprintf(stderr, "erresc: bad bgcolor %d\n", attr[i]);
  1147. }
  1148. else
  1149. fprintf(stderr, "erresc(48): gfx attr %d unknown\n", attr[i]);
  1150. break;
  1151. case 49:
  1152. term.c.attr.bg = DefaultBG;
  1153. break;
  1154. default:
  1155. if(BETWEEN(attr[i], 30, 37))
  1156. term.c.attr.fg = attr[i] - 30;
  1157. else if(BETWEEN(attr[i], 40, 47))
  1158. term.c.attr.bg = attr[i] - 40;
  1159. else if(BETWEEN(attr[i], 90, 97))
  1160. term.c.attr.fg = attr[i] - 90 + 8;
  1161. else if(BETWEEN(attr[i], 100, 107))
  1162. term.c.attr.bg = attr[i] - 100 + 8;
  1163. else
  1164. fprintf(stderr, "erresc(default): gfx attr %d unknown\n", attr[i]), csidump();
  1165. break;
  1166. }
  1167. }
  1168. }
  1169. void
  1170. tsetscroll(int t, int b) {
  1171. int temp;
  1172. LIMIT(t, 0, term.row-1);
  1173. LIMIT(b, 0, term.row-1);
  1174. if(t > b) {
  1175. temp = t;
  1176. t = b;
  1177. b = temp;
  1178. }
  1179. term.top = t;
  1180. term.bot = b;
  1181. }
  1182. #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
  1183. void
  1184. tsetmode(bool priv, bool set, int *args, int narg) {
  1185. int *lim, mode;
  1186. for(lim = args + narg; args < lim; ++args) {
  1187. if(priv) {
  1188. switch(*args) {
  1189. break;
  1190. case 1: /* DECCKM -- Cursor key */
  1191. MODBIT(term.mode, set, MODE_APPKEYPAD);
  1192. break;
  1193. case 5: /* DECSCNM -- Reverse video */
  1194. mode = term.mode;
  1195. MODBIT(term.mode, set, MODE_REVERSE);
  1196. if(mode != term.mode)
  1197. redraw();
  1198. break;
  1199. case 6: /* XXX: DECOM -- Origin */
  1200. break;
  1201. case 7: /* DECAWM -- Auto wrap */
  1202. MODBIT(term.mode, set, MODE_WRAP);
  1203. break;
  1204. case 8: /* XXX: DECARM -- Auto repeat */
  1205. break;
  1206. case 0: /* Error (IGNORED) */
  1207. case 12: /* att610 -- Start blinking cursor (IGNORED) */
  1208. break;
  1209. case 25:
  1210. MODBIT(term.c.state, !set, CURSOR_HIDE);
  1211. break;
  1212. case 1000: /* 1000,1002: enable xterm mouse report */
  1213. MODBIT(term.mode, set, MODE_MOUSEBTN);
  1214. break;
  1215. case 1002:
  1216. MODBIT(term.mode, set, MODE_MOUSEMOTION);
  1217. break;
  1218. case 1049: /* = 1047 and 1048 */
  1219. case 47:
  1220. case 1047:
  1221. if(IS_SET(MODE_ALTSCREEN))
  1222. tclearregion(0, 0, term.col-1, term.row-1);
  1223. if((set && !IS_SET(MODE_ALTSCREEN)) ||
  1224. (!set && IS_SET(MODE_ALTSCREEN))) {
  1225. tswapscreen();
  1226. }
  1227. if(*args != 1049)
  1228. break;
  1229. /* pass through */
  1230. case 1048:
  1231. tcursor((set) ? CURSOR_SAVE : CURSOR_LOAD);
  1232. break;
  1233. default:
  1234. /* case 2: DECANM -- ANSI/VT52 (NOT SUPPOURTED) */
  1235. /* case 3: DECCOLM -- Column (NOT SUPPORTED) */
  1236. /* case 4: DECSCLM -- Scroll (NOT SUPPORTED) */
  1237. /* case 18: DECPFF -- Printer feed (NOT SUPPORTED) */
  1238. /* case 19: DECPEX -- Printer extent (NOT SUPPORTED) */
  1239. /* case 42: DECNRCM -- National characters (NOT SUPPORTED) */
  1240. fprintf(stderr,
  1241. "erresc: unknown private set/reset mode %d\n",
  1242. *args);
  1243. break;
  1244. }
  1245. } else {
  1246. switch(*args) {
  1247. case 0: /* Error (IGNORED) */
  1248. break;
  1249. case 2: /* KAM -- keyboard action */
  1250. MODBIT(term.mode, set, MODE_KBDLOCK);
  1251. break;
  1252. case 4: /* IRM -- Insertion-replacement */
  1253. MODBIT(term.mode, set, MODE_INSERT);
  1254. break;
  1255. case 12: /* XXX: SRM -- Send/Receive */
  1256. break;
  1257. case 20: /* LNM -- Linefeed/new line */
  1258. MODBIT(term.mode, set, MODE_CRLF);
  1259. break;
  1260. default:
  1261. fprintf(stderr,
  1262. "erresc: unknown set/reset mode %d\n",
  1263. *args);
  1264. break;
  1265. }
  1266. }
  1267. }
  1268. }
  1269. #undef MODBIT
  1270. void
  1271. csihandle(void) {
  1272. switch(csiescseq.mode) {
  1273. default:
  1274. unknown:
  1275. fprintf(stderr, "erresc: unknown csi ");
  1276. csidump();
  1277. /* die(""); */
  1278. break;
  1279. case '@': /* ICH -- Insert <n> blank char */
  1280. DEFAULT(csiescseq.arg[0], 1);
  1281. tinsertblank(csiescseq.arg[0]);
  1282. break;
  1283. case 'A': /* CUU -- Cursor <n> Up */
  1284. case 'e':
  1285. DEFAULT(csiescseq.arg[0], 1);
  1286. tmoveto(term.c.x, term.c.y-csiescseq.arg[0]);
  1287. break;
  1288. case 'B': /* CUD -- Cursor <n> Down */
  1289. DEFAULT(csiescseq.arg[0], 1);
  1290. tmoveto(term.c.x, term.c.y+csiescseq.arg[0]);
  1291. break;
  1292. case 'C': /* CUF -- Cursor <n> Forward */
  1293. case 'a':
  1294. DEFAULT(csiescseq.arg[0], 1);
  1295. tmoveto(term.c.x+csiescseq.arg[0], term.c.y);
  1296. break;
  1297. case 'D': /* CUB -- Cursor <n> Backward */
  1298. DEFAULT(csiescseq.arg[0], 1);
  1299. tmoveto(term.c.x-csiescseq.arg[0], term.c.y);
  1300. break;
  1301. case 'E': /* CNL -- Cursor <n> Down and first col */
  1302. DEFAULT(csiescseq.arg[0], 1);
  1303. tmoveto(0, term.c.y+csiescseq.arg[0]);
  1304. break;
  1305. case 'F': /* CPL -- Cursor <n> Up and first col */
  1306. DEFAULT(csiescseq.arg[0], 1);
  1307. tmoveto(0, term.c.y-csiescseq.arg[0]);
  1308. break;
  1309. case 'g': /* TBC -- Tabulation clear */
  1310. switch (csiescseq.arg[0]) {
  1311. case 0: /* clear current tab stop */
  1312. term.tabs[term.c.x] = 0;
  1313. break;
  1314. case 3: /* clear all the tabs */
  1315. memset(term.tabs, 0, term.col * sizeof(*term.tabs));
  1316. break;
  1317. default:
  1318. goto unknown;
  1319. }
  1320. break;
  1321. case 'G': /* CHA -- Move to <col> */
  1322. case '`': /* HPA */
  1323. DEFAULT(csiescseq.arg[0], 1);
  1324. tmoveto(csiescseq.arg[0]-1, term.c.y);
  1325. break;
  1326. case 'H': /* CUP -- Move to <row> <col> */
  1327. case 'f': /* HVP */
  1328. DEFAULT(csiescseq.arg[0], 1);
  1329. DEFAULT(csiescseq.arg[1], 1);
  1330. tmoveto(csiescseq.arg[1]-1, csiescseq.arg[0]-1);
  1331. break;
  1332. case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
  1333. DEFAULT(csiescseq.arg[0], 1);
  1334. while(csiescseq.arg[0]--)
  1335. tputtab(1);
  1336. break;
  1337. case 'J': /* ED -- Clear screen */
  1338. sel.bx = -1;
  1339. switch(csiescseq.arg[0]) {
  1340. case 0: /* below */
  1341. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  1342. if(term.c.y < term.row-1)
  1343. tclearregion(0, term.c.y+1, term.col-1, term.row-1);
  1344. break;
  1345. case 1: /* above */
  1346. if(term.c.y > 1)
  1347. tclearregion(0, 0, term.col-1, term.c.y-1);
  1348. tclearregion(0, term.c.y, term.c.x, term.c.y);
  1349. break;
  1350. case 2: /* all */
  1351. tclearregion(0, 0, term.col-1, term.row-1);
  1352. break;
  1353. default:
  1354. goto unknown;
  1355. }
  1356. break;
  1357. case 'K': /* EL -- Clear line */
  1358. switch(csiescseq.arg[0]) {
  1359. case 0: /* right */
  1360. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  1361. break;
  1362. case 1: /* left */
  1363. tclearregion(0, term.c.y, term.c.x, term.c.y);
  1364. break;
  1365. case 2: /* all */
  1366. tclearregion(0, term.c.y, term.col-1, term.c.y);
  1367. break;
  1368. }
  1369. break;
  1370. case 'S': /* SU -- Scroll <n> line up */
  1371. DEFAULT(csiescseq.arg[0], 1);
  1372. tscrollup(term.top, csiescseq.arg[0]);
  1373. break;
  1374. case 'T': /* SD -- Scroll <n> line down */
  1375. DEFAULT(csiescseq.arg[0], 1);
  1376. tscrolldown(term.top, csiescseq.arg[0]);
  1377. break;
  1378. case 'L': /* IL -- Insert <n> blank lines */
  1379. DEFAULT(csiescseq.arg[0], 1);
  1380. tinsertblankline(csiescseq.arg[0]);
  1381. break;
  1382. case 'l': /* RM -- Reset Mode */
  1383. tsetmode(csiescseq.priv, 0, csiescseq.arg, csiescseq.narg);
  1384. break;
  1385. case 'M': /* DL -- Delete <n> lines */
  1386. DEFAULT(csiescseq.arg[0], 1);
  1387. tdeleteline(csiescseq.arg[0]);
  1388. break;
  1389. case 'X': /* ECH -- Erase <n> char */
  1390. DEFAULT(csiescseq.arg[0], 1);
  1391. tclearregion(term.c.x, term.c.y, term.c.x + csiescseq.arg[0], term.c.y);
  1392. break;
  1393. case 'P': /* DCH -- Delete <n> char */
  1394. DEFAULT(csiescseq.arg[0], 1);
  1395. tdeletechar(csiescseq.arg[0]);
  1396. break;
  1397. case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
  1398. DEFAULT(csiescseq.arg[0], 1);
  1399. while(csiescseq.arg[0]--)
  1400. tputtab(0);
  1401. break;
  1402. case 'd': /* VPA -- Move to <row> */
  1403. DEFAULT(csiescseq.arg[0], 1);
  1404. tmoveto(term.c.x, csiescseq.arg[0]-1);
  1405. break;
  1406. case 'h': /* SM -- Set terminal mode */
  1407. tsetmode(csiescseq.priv, 1, csiescseq.arg, csiescseq.narg);
  1408. break;
  1409. case 'm': /* SGR -- Terminal attribute (color) */
  1410. tsetattr(csiescseq.arg, csiescseq.narg);
  1411. break;
  1412. case 'r': /* DECSTBM -- Set Scrolling Region */
  1413. if(csiescseq.priv)
  1414. goto unknown;
  1415. else {
  1416. DEFAULT(csiescseq.arg[0], 1);
  1417. DEFAULT(csiescseq.arg[1], term.row);
  1418. tsetscroll(csiescseq.arg[0]-1, csiescseq.arg[1]-1);
  1419. tmoveto(0, 0);
  1420. }
  1421. break;
  1422. case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
  1423. tcursor(CURSOR_SAVE);
  1424. break;
  1425. case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
  1426. tcursor(CURSOR_LOAD);
  1427. break;
  1428. }
  1429. }
  1430. void
  1431. csidump(void) {
  1432. int i;
  1433. printf("ESC[");
  1434. for(i = 0; i < csiescseq.len; i++) {
  1435. uint c = csiescseq.buf[i] & 0xff;
  1436. if(isprint(c)) putchar(c);
  1437. else if(c == '\n') printf("(\\n)");
  1438. else if(c == '\r') printf("(\\r)");
  1439. else if(c == 0x1b) printf("(\\e)");
  1440. else printf("(%02x)", c);
  1441. }
  1442. putchar('\n');
  1443. }
  1444. void
  1445. csireset(void) {
  1446. memset(&csiescseq, 0, sizeof(csiescseq));
  1447. }
  1448. void
  1449. strhandle(void) {
  1450. char *p;
  1451. /*
  1452. * TODO: make this being useful in case of color palette change.
  1453. */
  1454. strparse();
  1455. p = strescseq.buf;
  1456. switch(strescseq.type) {
  1457. case ']': /* OSC -- Operating System Command */
  1458. switch(p[0]) {
  1459. case '0':
  1460. case '1':
  1461. case '2':
  1462. /*
  1463. * TODO: Handle special chars in string, like umlauts.
  1464. */
  1465. if(p[1] == ';') {
  1466. XStoreName(xw.dpy, xw.win, strescseq.buf+2);
  1467. }
  1468. break;
  1469. case ';':
  1470. XStoreName(xw.dpy, xw.win, strescseq.buf+1);
  1471. break;
  1472. case '4': /* TODO: Set color (arg0) to "rgb:%hexr/$hexg/$hexb" (arg1) */
  1473. break;
  1474. default:
  1475. fprintf(stderr, "erresc: unknown str ");
  1476. strdump();
  1477. break;
  1478. }
  1479. break;
  1480. case 'k': /* old title set compatibility */
  1481. XStoreName(xw.dpy, xw.win, strescseq.buf);
  1482. break;
  1483. case 'P': /* DSC -- Device Control String */
  1484. case '_': /* APC -- Application Program Command */
  1485. case '^': /* PM -- Privacy Message */
  1486. default:
  1487. fprintf(stderr, "erresc: unknown str ");
  1488. strdump();
  1489. /* die(""); */
  1490. break;
  1491. }
  1492. }
  1493. void
  1494. strparse(void) {
  1495. /*
  1496. * TODO: Implement parsing like for CSI when required.
  1497. * Format: ESC type cmd ';' arg0 [';' argn] ESC \
  1498. */
  1499. return;
  1500. }
  1501. void
  1502. strdump(void) {
  1503. int i;
  1504. printf("ESC%c", strescseq.type);
  1505. for(i = 0; i < strescseq.len; i++) {
  1506. uint c = strescseq.buf[i] & 0xff;
  1507. if(isprint(c)) putchar(c);
  1508. else if(c == '\n') printf("(\\n)");
  1509. else if(c == '\r') printf("(\\r)");
  1510. else if(c == 0x1b) printf("(\\e)");
  1511. else printf("(%02x)", c);
  1512. }
  1513. printf("ESC\\\n");
  1514. }
  1515. void
  1516. strreset(void) {
  1517. memset(&strescseq, 0, sizeof(strescseq));
  1518. }
  1519. void
  1520. tputtab(bool forward) {
  1521. uint x = term.c.x;
  1522. if(forward) {
  1523. if(x == term.col)
  1524. return;
  1525. for(++x; x < term.col && !term.tabs[x]; ++x)
  1526. /* nothing */ ;
  1527. } else {
  1528. if(x == 0)
  1529. return;
  1530. for(--x; x > 0 && !term.tabs[x]; --x)
  1531. /* nothing */ ;
  1532. }
  1533. tmoveto(x, term.c.y);
  1534. }
  1535. void
  1536. tputc(char *c, int len) {
  1537. uchar ascii = *c;
  1538. if(iofd != -1)
  1539. write(iofd, c, len);
  1540. switch(ascii) {
  1541. case '\t':
  1542. tputtab(1);
  1543. return;
  1544. case '\b':
  1545. tmoveto(term.c.x-1, term.c.y);
  1546. return;
  1547. case '\r':
  1548. tmoveto(0, term.c.y);
  1549. return;
  1550. case '\f':
  1551. case '\v':
  1552. case '\n':
  1553. /* go to first col if the mode is set */
  1554. tnewline(IS_SET(MODE_CRLF));
  1555. return;
  1556. case '\a':
  1557. if(term.esc & ESC_STR)
  1558. break;
  1559. if(!(xw.state & WIN_FOCUSED))
  1560. xseturgency(1);
  1561. return;
  1562. case '\033':
  1563. csireset();
  1564. term.esc = ESC_START;
  1565. return;
  1566. }
  1567. if(term.esc & ESC_START) {
  1568. if(term.esc & ESC_CSI) {
  1569. csiescseq.buf[csiescseq.len++] = ascii;
  1570. if(BETWEEN(ascii, 0x40, 0x7E) || csiescseq.len >= ESC_BUF_SIZ) {
  1571. term.esc = 0;
  1572. csiparse(), csihandle();
  1573. }
  1574. } else if(term.esc & ESC_STR) {
  1575. switch(ascii) {
  1576. case '\033':
  1577. term.esc = ESC_START | ESC_STR_END;
  1578. break;
  1579. case '\a': /* backwards compatibility to xterm */
  1580. term.esc = 0;
  1581. strhandle();
  1582. break;
  1583. default:
  1584. strescseq.buf[strescseq.len++] = ascii;
  1585. if(strescseq.len+1 >= STR_BUF_SIZ) {
  1586. term.esc = 0;
  1587. strhandle();
  1588. }
  1589. }
  1590. } else if(term.esc & ESC_STR_END) {
  1591. term.esc = 0;
  1592. if(ascii == '\\')
  1593. strhandle();
  1594. } else if(term.esc & ESC_ALTCHARSET) {
  1595. switch(ascii) {
  1596. case '0': /* Line drawing set */
  1597. term.c.attr.mode |= ATTR_GFX;
  1598. break;
  1599. case 'B': /* USASCII */
  1600. term.c.attr.mode &= ~ATTR_GFX;
  1601. break;
  1602. case 'A': /* UK (IGNORED) */
  1603. case '<': /* multinational charset (IGNORED) */
  1604. case '5': /* Finnish (IGNORED) */
  1605. case 'C': /* Finnish (IGNORED) */
  1606. case 'K': /* German (IGNORED) */
  1607. break;
  1608. default:
  1609. fprintf(stderr, "esc unhandled charset: ESC ( %c\n", ascii);
  1610. }
  1611. term.esc = 0;
  1612. } else {
  1613. switch(ascii) {
  1614. case '[':
  1615. term.esc |= ESC_CSI;
  1616. break;
  1617. case 'P': /* DCS -- Device Control String */
  1618. case '_': /* APC -- Application Program Command */
  1619. case '^': /* PM -- Privacy Message */
  1620. case ']': /* OSC -- Operating System Command */
  1621. case 'k': /* old title set compatibility */
  1622. strreset();
  1623. strescseq.type = ascii;
  1624. term.esc |= ESC_STR;
  1625. break;
  1626. case '(': /* set primary charset G0 */
  1627. term.esc |= ESC_ALTCHARSET;
  1628. break;
  1629. case ')': /* set secondary charset G1 (IGNORED) */
  1630. case '*': /* set tertiary charset G2 (IGNORED) */
  1631. case '+': /* set quaternary charset G3 (IGNORED) */
  1632. term.esc = 0;
  1633. break;
  1634. case 'D': /* IND -- Linefeed */
  1635. if(term.c.y == term.bot)
  1636. tscrollup(term.top, 1);
  1637. else
  1638. tmoveto(term.c.x, term.c.y+1);
  1639. term.esc = 0;
  1640. break;
  1641. case 'E': /* NEL -- Next line */
  1642. tnewline(1); /* always go to first col */
  1643. term.esc = 0;
  1644. break;
  1645. case 'H': /* HTS -- Horizontal tab stop */
  1646. term.tabs[term.c.x] = 1;
  1647. term.esc = 0;
  1648. break;
  1649. case 'M': /* RI -- Reverse index */
  1650. if(term.c.y == term.top)
  1651. tscrolldown(term.top, 1);
  1652. else
  1653. tmoveto(term.c.x, term.c.y-1);
  1654. term.esc = 0;
  1655. break;
  1656. case 'c': /* RIS -- Reset to inital state */
  1657. treset();
  1658. term.esc = 0;
  1659. xclearborders();
  1660. xresettitle();
  1661. break;
  1662. case '=': /* DECPAM -- Application keypad */
  1663. term.mode |= MODE_APPKEYPAD;
  1664. term.esc = 0;
  1665. break;
  1666. case '>': /* DECPNM -- Normal keypad */
  1667. term.mode &= ~MODE_APPKEYPAD;
  1668. term.esc = 0;
  1669. break;
  1670. case '7': /* DECSC -- Save Cursor */
  1671. tcursor(CURSOR_SAVE);
  1672. term.esc = 0;
  1673. break;
  1674. case '8': /* DECRC -- Restore Cursor */
  1675. tcursor(CURSOR_LOAD);
  1676. term.esc = 0;
  1677. break;
  1678. case '\\': /* ST -- Stop */
  1679. term.esc = 0;
  1680. break;
  1681. default:
  1682. fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n",
  1683. (uchar) ascii, isprint(ascii)?ascii:'.');
  1684. term.esc = 0;
  1685. }
  1686. }
  1687. } else {
  1688. if(sel.bx != -1 && BETWEEN(term.c.y, sel.by, sel.ey))
  1689. sel.bx = -1;
  1690. if(ascii >= '\020' || term.c.attr.mode & ATTR_GFX) {
  1691. if(IS_SET(MODE_WRAP) && term.c.state & CURSOR_WRAPNEXT)
  1692. tnewline(1); /* always go to first col */
  1693. tsetchar(c);
  1694. if(term.c.x+1 < term.col)
  1695. tmoveto(term.c.x+1, term.c.y);
  1696. else
  1697. term.c.state |= CURSOR_WRAPNEXT;
  1698. }
  1699. }
  1700. }
  1701. int
  1702. tresize(int col, int row) {
  1703. int i, x;
  1704. int minrow = MIN(row, term.row);
  1705. int mincol = MIN(col, term.col);
  1706. int slide = term.c.y - row + 1;
  1707. if(col < 1 || row < 1)
  1708. return 0;
  1709. /* free unneeded rows */
  1710. i = 0;
  1711. if(slide > 0) {
  1712. /* slide screen to keep cursor where we expect it -
  1713. * tscrollup would work here, but we can optimize to
  1714. * memmove because we're freeing the earlier lines */
  1715. for(/* i = 0 */; i < slide; i++) {
  1716. free(term.line[i]);
  1717. free(term.alt[i]);
  1718. }
  1719. memmove(term.line, term.line + slide, row * sizeof(Line));
  1720. memmove(term.alt, term.alt + slide, row * sizeof(Line));
  1721. }
  1722. for(i += row; i < term.row; i++) {
  1723. free(term.line[i]);
  1724. free(term.alt[i]);
  1725. }
  1726. /* resize to new height */
  1727. term.line = xrealloc(term.line, row * sizeof(Line));
  1728. term.alt = xrealloc(term.alt, row * sizeof(Line));
  1729. term.dirty = xrealloc(term.dirty, row * sizeof(*term.dirty));
  1730. term.tabs = xrealloc(term.tabs, col * sizeof(*term.tabs));
  1731. /* resize each row to new width, zero-pad if needed */
  1732. for(i = 0; i < minrow; i++) {
  1733. term.dirty[i] = 1;
  1734. term.line[i] = xrealloc(term.line[i], col * sizeof(Glyph));
  1735. term.alt[i] = xrealloc(term.alt[i], col * sizeof(Glyph));
  1736. for(x = mincol; x < col; x++) {
  1737. term.line[i][x].state = 0;
  1738. term.alt[i][x].state = 0;
  1739. }
  1740. }
  1741. /* allocate any new rows */
  1742. for(/* i == minrow */; i < row; i++) {
  1743. term.dirty[i] = 1;
  1744. term.line[i] = xcalloc(col, sizeof(Glyph));
  1745. term.alt [i] = xcalloc(col, sizeof(Glyph));
  1746. }
  1747. if(col > term.col) {
  1748. bool *bp = term.tabs + term.col;
  1749. memset(bp, 0, sizeof(*term.tabs) * (col - term.col));
  1750. while(--bp > term.tabs && !*bp)
  1751. /* nothing */ ;
  1752. for(bp += TAB; bp < term.tabs + col; bp += TAB)
  1753. *bp = 1;
  1754. }
  1755. /* update terminal size */
  1756. term.col = col, term.row = row;
  1757. /* make use of the LIMIT in tmoveto */
  1758. tmoveto(term.c.x, term.c.y);
  1759. /* reset scrolling region */
  1760. tsetscroll(0, row-1);
  1761. return (slide > 0);
  1762. }
  1763. void
  1764. xresize(int col, int row) {
  1765. xw.tw = MAX(1, 2*BORDER + col * xw.cw);
  1766. xw.th = MAX(1, 2*BORDER + row * xw.ch);
  1767. XftDrawChange(xw.xft_draw, xw.buf);
  1768. }
  1769. void
  1770. xloadcols(void) {
  1771. int i, r, g, b;
  1772. XRenderColor xft_color = { .alpha = 0 };
  1773. /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
  1774. for(i = 0; i < LEN(colorname); i++) {
  1775. if(!colorname[i])
  1776. continue;
  1777. if(!XftColorAllocName(xw.dpy, xw.vis, xw.cmap, colorname[i], &dc.xft_col[i])) {
  1778. die("Could not allocate color '%s'\n", colorname[i]);
  1779. }
  1780. }
  1781. /* load colors [16-255] ; same colors as xterm */
  1782. for(i = 16, r = 0; r < 6; r++) {
  1783. for(g = 0; g < 6; g++) {
  1784. for(b = 0; b < 6; b++) {
  1785. xft_color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r;
  1786. xft_color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g;
  1787. xft_color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b;
  1788. if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &xft_color, &dc.xft_col[i])) {
  1789. die("Could not allocate color %d\n", i);
  1790. }
  1791. i++;
  1792. }
  1793. }
  1794. }
  1795. for(r = 0; r < 24; r++, i++) {
  1796. xft_color.red = xft_color.green = xft_color.blue = 0x0808 + 0x0a0a * r;
  1797. if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &xft_color, &dc.xft_col[i])) {
  1798. die("Could not allocate color %d\n", i);
  1799. }
  1800. }
  1801. }
  1802. void
  1803. xtermclear(int col1, int row1, int col2, int row2) {
  1804. XftDrawRect(xw.xft_draw,
  1805. &dc.xft_col[IS_SET(MODE_REVERSE) ? DefaultFG : DefaultBG],
  1806. BORDER + col1 * xw.cw,
  1807. BORDER + row1 * xw.ch,
  1808. (col2-col1+1) * xw.cw,
  1809. (row2-row1+1) * xw.ch);
  1810. }
  1811. /*
  1812. * Absolute coordinates.
  1813. */
  1814. void
  1815. xclear(int x1, int y1, int x2, int y2) {
  1816. XftDrawRect(xw.xft_draw,
  1817. &dc.xft_col[IS_SET(MODE_REVERSE) ? DefaultFG : DefaultBG],
  1818. x1, y1, x2-x1, y2-y1);
  1819. }
  1820. void
  1821. xclearborders(void) {
  1822. /* top and left border */
  1823. xclear(0, 0, BORDER, xw.h);
  1824. xclear(0, 0, xw.w, BORDER);
  1825. /* lower and right border */
  1826. xclear(BORDER, xw.th - 1, xw.w, xw.h);
  1827. /* Will just draw what hasn't been drawn by the previous call. */
  1828. xclear(xw.tw - 1, BORDER, xw.w, xw.h - xw.th - 2);
  1829. }
  1830. void
  1831. xhints(void) {
  1832. XClassHint class = {opt_class ? opt_class : TNAME, TNAME};
  1833. XWMHints wm = {.flags = InputHint, .input = 1};
  1834. XSizeHints *sizeh = NULL;
  1835. sizeh = XAllocSizeHints();
  1836. if(xw.isfixed == False) {
  1837. sizeh->flags = PSize | PResizeInc | PBaseSize;
  1838. sizeh->height = xw.h;
  1839. sizeh->width = xw.w;
  1840. sizeh->height_inc = xw.ch;
  1841. sizeh->width_inc = xw.cw;
  1842. sizeh->base_height = 2*BORDER;
  1843. sizeh->base_width = 2*BORDER;
  1844. } else {
  1845. sizeh->flags = PMaxSize | PMinSize;
  1846. sizeh->min_width = sizeh->max_width = xw.fw;
  1847. sizeh->min_height = sizeh->max_height = xw.fh;
  1848. }
  1849. XSetWMProperties(xw.dpy, xw.win, NULL, NULL, NULL, 0, sizeh, &wm, &class);
  1850. XFree(sizeh);
  1851. }
  1852. void
  1853. xinitfont(Font *f, char *fontstr) {
  1854. FcPattern *pattern, *match;
  1855. FcResult result;
  1856. pattern = FcNameParse((FcChar8 *)fontstr);
  1857. if(!pattern)
  1858. die("st: can't open font %s\n", fontstr);
  1859. match = XftFontMatch(xw.dpy, xw.scr, pattern, &result);
  1860. FcPatternDestroy(pattern);
  1861. if(!match)
  1862. die("st: can't open font %s\n", fontstr);
  1863. if(!(f->xft_set = XftFontOpenPattern(xw.dpy, match))) {
  1864. FcPatternDestroy(match);
  1865. die("st: can't open font %s.\n", fontstr);
  1866. }
  1867. f->ascent = f->xft_set->ascent;
  1868. f->descent = f->xft_set->descent;
  1869. f->lbearing = 0;
  1870. f->rbearing = f->xft_set->max_advance_width;
  1871. f->height = f->xft_set->height;
  1872. f->width = f->lbearing + f->rbearing;
  1873. }
  1874. void
  1875. initfonts(char *fontstr) {
  1876. char *fstr;
  1877. xinitfont(&dc.font, fontstr);
  1878. xw.cw = dc.font.width;
  1879. xw.ch = dc.font.height;
  1880. fstr = smstrcat(fontstr, ":weight=bold", NULL);
  1881. xinitfont(&dc.bfont, fstr);
  1882. free(fstr);
  1883. fstr = smstrcat(fontstr, ":slant=italic,oblique", NULL);
  1884. xinitfont(&dc.ifont, fstr);
  1885. free(fstr);
  1886. fstr = smstrcat(fontstr, ":weight=bold:slant=italic,oblique", NULL);
  1887. xinitfont(&dc.ibfont, fstr);
  1888. free(fstr);
  1889. }
  1890. void
  1891. xinit(void) {
  1892. XSetWindowAttributes attrs;
  1893. Cursor cursor;
  1894. Window parent;
  1895. int sw, sh, major, minor;
  1896. if(!(xw.dpy = XOpenDisplay(NULL)))
  1897. die("Can't open display\n");
  1898. xw.scr = XDefaultScreen(xw.dpy);
  1899. xw.vis = XDefaultVisual(xw.dpy, xw.scr);
  1900. /* font */
  1901. initfonts((opt_font != NULL)? opt_font : FONT);
  1902. /* colors */
  1903. xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
  1904. xloadcols();
  1905. /* adjust fixed window geometry */
  1906. if(xw.isfixed) {
  1907. sw = DisplayWidth(xw.dpy, xw.scr);
  1908. sh = DisplayHeight(xw.dpy, xw.scr);
  1909. if(xw.fx < 0)
  1910. xw.fx = sw + xw.fx - xw.fw - 1;
  1911. if(xw.fy < 0)
  1912. xw.fy = sh + xw.fy - xw.fh - 1;
  1913. xw.h = xw.fh;
  1914. xw.w = xw.fw;
  1915. } else {
  1916. /* window - default size */
  1917. xw.h = 2*BORDER + term.row * xw.ch;
  1918. xw.w = 2*BORDER + term.col * xw.cw;
  1919. xw.fx = 0;
  1920. xw.fy = 0;
  1921. }
  1922. attrs.background_pixel = dc.xft_col[DefaultBG].pixel;
  1923. attrs.border_pixel = dc.xft_col[DefaultBG].pixel;
  1924. attrs.bit_gravity = NorthWestGravity;
  1925. attrs.event_mask = FocusChangeMask | KeyPressMask
  1926. | ExposureMask | VisibilityChangeMask | StructureNotifyMask
  1927. | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask;
  1928. attrs.colormap = xw.cmap;
  1929. parent = opt_embed ? strtol(opt_embed, NULL, 0) : XRootWindow(xw.dpy, xw.scr);
  1930. xw.win = XCreateWindow(xw.dpy, parent, xw.fx, xw.fy,
  1931. xw.w, xw.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
  1932. xw.vis,
  1933. CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
  1934. | CWColormap,
  1935. &attrs);
  1936. /* double buffering */
  1937. if(!XdbeQueryExtension(xw.dpy, &major, &minor))
  1938. die("Xdbe extension is not present\n");
  1939. xw.buf = XdbeAllocateBackBufferName(xw.dpy, xw.win, XdbeCopied);
  1940. /* Xft rendering context */
  1941. xw.xft_draw = XftDrawCreate(xw.dpy, xw.buf, xw.vis, xw.cmap);
  1942. /* input methods */
  1943. xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL);
  1944. xw.xic = XCreateIC(xw.xim, XNInputStyle, XIMPreeditNothing
  1945. | XIMStatusNothing, XNClientWindow, xw.win,
  1946. XNFocusWindow, xw.win, NULL);
  1947. /* gc */
  1948. dc.gc = XCreateGC(xw.dpy, xw.win, 0, NULL);
  1949. /* white cursor, black outline */
  1950. cursor = XCreateFontCursor(xw.dpy, XC_xterm);
  1951. XDefineCursor(xw.dpy, xw.win, cursor);
  1952. XRecolorCursor(xw.dpy, cursor,
  1953. &(XColor){.red = 0xffff, .green = 0xffff, .blue = 0xffff},
  1954. &(XColor){.red = 0x0000, .green = 0x0000, .blue = 0x0000});
  1955. xw.xembed = XInternAtom(xw.dpy, "_XEMBED", False);
  1956. xresettitle();
  1957. XMapWindow(xw.dpy, xw.win);
  1958. xhints();
  1959. XSync(xw.dpy, 0);
  1960. }
  1961. void
  1962. xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
  1963. int winx = BORDER+x*xw.cw, winy = BORDER+y*xw.ch,
  1964. width = charlen*xw.cw;
  1965. Font *font = &dc.font;
  1966. XGlyphInfo extents;
  1967. XftColor *fg = &dc.xft_col[base.fg], *bg = &dc.xft_col[base.bg],
  1968. *temp, revfg, revbg;
  1969. XRenderColor colfg, colbg;
  1970. if(base.mode & ATTR_REVERSE)
  1971. temp = fg, fg = bg, bg = temp;
  1972. if(base.mode & ATTR_BOLD) {
  1973. if(BETWEEN(base.fg, 0, 7)) {
  1974. /* basic system colors */
  1975. fg = &dc.xft_col[base.fg + 8];
  1976. } else if(BETWEEN(base.fg, 16, 195)) {
  1977. /* 256 colors */
  1978. fg = &dc.xft_col[base.fg + 36];
  1979. } else if(BETWEEN(base.fg, 232, 251)) {
  1980. /* greyscale */
  1981. fg = &dc.xft_col[base.fg + 4];
  1982. }
  1983. /*
  1984. * Those ranges will not be brightened:
  1985. * 8 - 15 – bright system colors
  1986. * 196 - 231 – highest 256 color cube
  1987. * 252 - 255 – brightest colors in grescale
  1988. */
  1989. font = &dc.bfont;
  1990. }
  1991. if(base.mode & ATTR_ITALIC)
  1992. font = &dc.ifont;
  1993. if(base.mode & (ATTR_ITALIC|ATTR_ITALIC))
  1994. font = &dc.ibfont;
  1995. if(IS_SET(MODE_REVERSE)) {
  1996. if(fg == &dc.xft_col[DefaultFG]) {
  1997. fg = &dc.xft_col[DefaultBG];
  1998. } else {
  1999. colfg.red = ~fg->color.red;
  2000. colfg.green = ~fg->color.green;
  2001. colfg.blue = ~fg->color.blue;
  2002. colfg.alpha = fg->color.alpha;
  2003. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &revfg);
  2004. fg = &revfg;
  2005. }
  2006. if(bg == &dc.xft_col[DefaultBG]) {
  2007. bg = &dc.xft_col[DefaultFG];
  2008. } else {
  2009. colbg.red = ~bg->color.red;
  2010. colbg.green = ~bg->color.green;
  2011. colbg.blue = ~bg->color.blue;
  2012. colbg.alpha = bg->color.alpha;
  2013. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg, &revbg);
  2014. bg = &revbg;
  2015. }
  2016. }
  2017. XftTextExtentsUtf8(xw.dpy, font->xft_set, (FcChar8 *)s, bytelen,
  2018. &extents);
  2019. width = extents.xOff;
  2020. XftDrawRect(xw.xft_draw, bg, winx, winy, width, xw.ch);
  2021. XftDrawStringUtf8(xw.xft_draw, fg, font->xft_set, winx,
  2022. winy + font->ascent, (FcChar8 *)s, bytelen);
  2023. if(base.mode & ATTR_UNDERLINE) {
  2024. XftDrawRect(xw.xft_draw, fg, winx, winy + font->ascent + 1,
  2025. width, 1);
  2026. }
  2027. }
  2028. void
  2029. xdrawcursor(void) {
  2030. static int oldx = 0;
  2031. static int oldy = 0;
  2032. int sl;
  2033. Glyph g = {{' '}, ATTR_NULL, DefaultBG, DefaultCS, 0};
  2034. LIMIT(oldx, 0, term.col-1);
  2035. LIMIT(oldy, 0, term.row-1);
  2036. if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
  2037. memcpy(g.c, term.line[term.c.y][term.c.x].c, UTF_SIZ);
  2038. /* remove the old cursor */
  2039. if(term.line[oldy][oldx].state & GLYPH_SET) {
  2040. sl = utf8size(term.line[oldy][oldx].c);
  2041. xdraws(term.line[oldy][oldx].c, term.line[oldy][oldx], oldx, oldy, 1, sl);
  2042. } else
  2043. xtermclear(oldx, oldy, oldx, oldy);
  2044. /* draw the new one */
  2045. if(!(term.c.state & CURSOR_HIDE)) {
  2046. if(!(xw.state & WIN_FOCUSED))
  2047. g.bg = DefaultUCS;
  2048. if(IS_SET(MODE_REVERSE))
  2049. g.mode |= ATTR_REVERSE, g.fg = DefaultCS, g.bg = DefaultFG;
  2050. sl = utf8size(g.c);
  2051. xdraws(g.c, g, term.c.x, term.c.y, 1, sl);
  2052. oldx = term.c.x, oldy = term.c.y;
  2053. }
  2054. }
  2055. void
  2056. xresettitle(void) {
  2057. XStoreName(xw.dpy, xw.win, opt_title ? opt_title : "st");
  2058. }
  2059. void
  2060. redraw(void) {
  2061. struct timespec tv = {0, REDRAW_TIMEOUT * 1000};
  2062. xclearborders();
  2063. tfulldirt();
  2064. draw();
  2065. XSync(xw.dpy, False); /* necessary for a good tput flash */
  2066. nanosleep(&tv, NULL);
  2067. }
  2068. void
  2069. draw(void) {
  2070. XdbeSwapInfo swpinfo[1] = {{xw.win, XdbeCopied}};
  2071. drawregion(0, 0, term.col, term.row);
  2072. XdbeSwapBuffers(xw.dpy, swpinfo, 1);
  2073. }
  2074. void
  2075. drawregion(int x1, int y1, int x2, int y2) {
  2076. int ic, ib, x, y, ox, sl;
  2077. Glyph base, new;
  2078. char buf[DRAW_BUF_SIZ];
  2079. bool ena_sel = sel.bx != -1, alt = IS_SET(MODE_ALTSCREEN);
  2080. if((sel.alt && !alt) || (!sel.alt && alt))
  2081. ena_sel = 0;
  2082. if(!(xw.state & WIN_VISIBLE))
  2083. return;
  2084. for(y = y1; y < y2; y++) {
  2085. if(!term.dirty[y])
  2086. continue;
  2087. xtermclear(0, y, term.col, y);
  2088. term.dirty[y] = 0;
  2089. base = term.line[y][0];
  2090. ic = ib = ox = 0;
  2091. for(x = x1; x < x2; x++) {
  2092. new = term.line[y][x];
  2093. if(ena_sel && *(new.c) && selected(x, y))
  2094. new.mode ^= ATTR_REVERSE;
  2095. if(ib > 0 && (!(new.state & GLYPH_SET) || ATTRCMP(base, new) ||
  2096. ib >= DRAW_BUF_SIZ-UTF_SIZ)) {
  2097. xdraws(buf, base, ox, y, ic, ib);
  2098. ic = ib = 0;
  2099. }
  2100. if(new.state & GLYPH_SET) {
  2101. if(ib == 0) {
  2102. ox = x;
  2103. base = new;
  2104. }
  2105. sl = utf8size(new.c);
  2106. memcpy(buf+ib, new.c, sl);
  2107. ib += sl;
  2108. ++ic;
  2109. }
  2110. }
  2111. if(ib > 0)
  2112. xdraws(buf, base, ox, y, ic, ib);
  2113. }
  2114. xdrawcursor();
  2115. }
  2116. void
  2117. expose(XEvent *ev) {
  2118. XExposeEvent *e = &ev->xexpose;
  2119. if(xw.state & WIN_REDRAW) {
  2120. if(!e->count)
  2121. xw.state &= ~WIN_REDRAW;
  2122. }
  2123. }
  2124. void
  2125. visibility(XEvent *ev) {
  2126. XVisibilityEvent *e = &ev->xvisibility;
  2127. if(e->state == VisibilityFullyObscured)
  2128. xw.state &= ~WIN_VISIBLE;
  2129. else if(!(xw.state & WIN_VISIBLE))
  2130. /* need a full redraw for next Expose, not just a buf copy */
  2131. xw.state |= WIN_VISIBLE | WIN_REDRAW;
  2132. }
  2133. void
  2134. unmap(XEvent *ev) {
  2135. xw.state &= ~WIN_VISIBLE;
  2136. }
  2137. void
  2138. xseturgency(int add) {
  2139. XWMHints *h = XGetWMHints(xw.dpy, xw.win);
  2140. h->flags = add ? (h->flags | XUrgencyHint) : (h->flags & ~XUrgencyHint);
  2141. XSetWMHints(xw.dpy, xw.win, h);
  2142. XFree(h);
  2143. }
  2144. void
  2145. focus(XEvent *ev) {
  2146. if(ev->type == FocusIn) {
  2147. xw.state |= WIN_FOCUSED;
  2148. xseturgency(0);
  2149. } else
  2150. xw.state &= ~WIN_FOCUSED;
  2151. }
  2152. char*
  2153. kmap(KeySym k, uint state) {
  2154. int i;
  2155. state &= ~Mod2Mask;
  2156. for(i = 0; i < LEN(key); i++) {
  2157. uint mask = key[i].mask;
  2158. if(key[i].k == k && ((state & mask) == mask || (mask == XK_NO_MOD && !state)))
  2159. return (char*)key[i].s;
  2160. }
  2161. return NULL;
  2162. }
  2163. void
  2164. kpress(XEvent *ev) {
  2165. XKeyEvent *e = &ev->xkey;
  2166. KeySym ksym;
  2167. char buf[32];
  2168. char *customkey;
  2169. int len;
  2170. int meta;
  2171. int shift;
  2172. Status status;
  2173. if (IS_SET(MODE_KBDLOCK))
  2174. return;
  2175. meta = e->state & Mod1Mask;
  2176. shift = e->state & ShiftMask;
  2177. len = XmbLookupString(xw.xic, e, buf, sizeof(buf), &ksym, &status);
  2178. /* 1. custom keys from config.h */
  2179. if((customkey = kmap(ksym, e->state)))
  2180. ttywrite(customkey, strlen(customkey));
  2181. /* 2. hardcoded (overrides X lookup) */
  2182. else
  2183. switch(ksym) {
  2184. case XK_Up:
  2185. case XK_Down:
  2186. case XK_Left:
  2187. case XK_Right:
  2188. /* XXX: shift up/down doesn't work */
  2189. sprintf(buf, "\033%c%c", IS_SET(MODE_APPKEYPAD) ? 'O' : '[', (shift ? "dacb":"DACB")[ksym - XK_Left]);
  2190. ttywrite(buf, 3);
  2191. break;
  2192. case XK_Insert:
  2193. if(shift)
  2194. selpaste();
  2195. break;
  2196. case XK_Return:
  2197. if(IS_SET(MODE_CRLF))
  2198. ttywrite("\r\n", 2);
  2199. else
  2200. ttywrite("\r", 1);
  2201. break;
  2202. /* 3. X lookup */
  2203. default:
  2204. if(len > 0) {
  2205. if(meta && len == 1)
  2206. ttywrite("\033", 1);
  2207. ttywrite(buf, len);
  2208. }
  2209. break;
  2210. }
  2211. }
  2212. void
  2213. cmessage(XEvent *e) {
  2214. /* See xembed specs
  2215. http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html */
  2216. if(e->xclient.message_type == xw.xembed && e->xclient.format == 32) {
  2217. if(e->xclient.data.l[1] == XEMBED_FOCUS_IN) {
  2218. xw.state |= WIN_FOCUSED;
  2219. xseturgency(0);
  2220. } else if(e->xclient.data.l[1] == XEMBED_FOCUS_OUT) {
  2221. xw.state &= ~WIN_FOCUSED;
  2222. }
  2223. }
  2224. }
  2225. void
  2226. resize(XEvent *e) {
  2227. int col, row;
  2228. if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
  2229. return;
  2230. xw.w = e->xconfigure.width;
  2231. xw.h = e->xconfigure.height;
  2232. col = (xw.w - 2*BORDER) / xw.cw;
  2233. row = (xw.h - 2*BORDER) / xw.ch;
  2234. if(col == term.col && row == term.row)
  2235. return;
  2236. tresize(col, row);
  2237. xresize(col, row);
  2238. xclearborders();
  2239. ttyresize();
  2240. }
  2241. void
  2242. run(void) {
  2243. XEvent ev;
  2244. fd_set rfd;
  2245. int xfd = XConnectionNumber(xw.dpy), i;
  2246. struct timeval drawtimeout, *tv = NULL;
  2247. for(i = 0;; i++) {
  2248. FD_ZERO(&rfd);
  2249. FD_SET(cmdfd, &rfd);
  2250. FD_SET(xfd, &rfd);
  2251. if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, tv) < 0) {
  2252. if(errno == EINTR)
  2253. continue;
  2254. die("select failed: %s\n", SERRNO);
  2255. }
  2256. /*
  2257. * Stop after a certain number of reads so the user does not
  2258. * feel like the system is stuttering.
  2259. */
  2260. if(i < 1000 && FD_ISSET(cmdfd, &rfd)) {
  2261. ttyread();
  2262. /*
  2263. * Just wait a bit so it isn't disturbing the
  2264. * user and the system is able to write something.
  2265. */
  2266. drawtimeout.tv_sec = 0;
  2267. drawtimeout.tv_usec = 5;
  2268. tv = &drawtimeout;
  2269. continue;
  2270. }
  2271. i = 0;
  2272. tv = NULL;
  2273. while(XPending(xw.dpy)) {
  2274. XNextEvent(xw.dpy, &ev);
  2275. if(XFilterEvent(&ev, xw.win))
  2276. continue;
  2277. if(handler[ev.type])
  2278. (handler[ev.type])(&ev);
  2279. }
  2280. draw();
  2281. XFlush(xw.dpy);
  2282. }
  2283. }
  2284. int
  2285. main(int argc, char *argv[]) {
  2286. int i, bitm, xr, yr;
  2287. uint wr, hr;
  2288. xw.fw = xw.fh = xw.fx = xw.fy = 0;
  2289. xw.isfixed = False;
  2290. for(i = 1; i < argc; i++) {
  2291. switch(argv[i][0] != '-' || argv[i][2] ? -1 : argv[i][1]) {
  2292. case 'c':
  2293. if(++i < argc)
  2294. opt_class = argv[i];
  2295. break;
  2296. case 'e':
  2297. /* eat every remaining arguments */
  2298. if(++i < argc)
  2299. opt_cmd = &argv[i];
  2300. goto run;
  2301. case 'f':
  2302. if(++i < argc)
  2303. opt_font = argv[i];
  2304. break;
  2305. case 'g':
  2306. if(++i >= argc)
  2307. break;
  2308. bitm = XParseGeometry(argv[i], &xr, &yr, &wr, &hr);
  2309. if(bitm & XValue)
  2310. xw.fx = xr;
  2311. if(bitm & YValue)
  2312. xw.fy = yr;
  2313. if(bitm & WidthValue)
  2314. xw.fw = (int)wr;
  2315. if(bitm & HeightValue)
  2316. xw.fh = (int)hr;
  2317. if(bitm & XNegative && xw.fx == 0)
  2318. xw.fx = -1;
  2319. if(bitm & XNegative && xw.fy == 0)
  2320. xw.fy = -1;
  2321. if(xw.fh != 0 && xw.fw != 0)
  2322. xw.isfixed = True;
  2323. break;
  2324. case 'o':
  2325. if(++i < argc)
  2326. opt_io = argv[i];
  2327. break;
  2328. case 't':
  2329. if(++i < argc)
  2330. opt_title = argv[i];
  2331. break;
  2332. case 'v':
  2333. default:
  2334. die(USAGE);
  2335. case 'w':
  2336. if(++i < argc)
  2337. opt_embed = argv[i];
  2338. break;
  2339. }
  2340. }
  2341. run:
  2342. setlocale(LC_CTYPE, "");
  2343. tnew(80, 24);
  2344. ttynew();
  2345. xinit();
  2346. selinit();
  2347. run();
  2348. return 0;
  2349. }