st.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071
  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 <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <signal.h>
  13. #include <sys/ioctl.h>
  14. #include <sys/select.h>
  15. #include <sys/stat.h>
  16. #include <sys/types.h>
  17. #include <sys/wait.h>
  18. #include <unistd.h>
  19. #include <X11/Xatom.h>
  20. #include <X11/Xlib.h>
  21. #include <X11/Xutil.h>
  22. #include <X11/cursorfont.h>
  23. #include <X11/keysym.h>
  24. #include <sys/time.h>
  25. #include <time.h>
  26. #if defined(__linux)
  27. #include <pty.h>
  28. #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
  29. #include <util.h>
  30. #elif defined(__FreeBSD__) || defined(__DragonFly__)
  31. #include <libutil.h>
  32. #endif
  33. #define USAGE \
  34. "st " VERSION " (c) 2010-2011 st engineers\n" \
  35. "usage: st [-t title] [-c class] [-w windowid] [-v] [-e command...]\n"
  36. /* XEMBED messages */
  37. #define XEMBED_FOCUS_IN 4
  38. #define XEMBED_FOCUS_OUT 5
  39. /* Arbitrary sizes */
  40. #define ESC_TITLE_SIZ 256
  41. #define ESC_BUF_SIZ 256
  42. #define ESC_ARG_SIZ 16
  43. #define DRAW_BUF_SIZ 1024
  44. #define UTF_SIZ 4
  45. #define XK_NO_MOD UINT_MAX
  46. #define XK_ANY_MOD 0
  47. #define SERRNO strerror(errno)
  48. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  49. #define MAX(a, b) ((a) < (b) ? (b) : (a))
  50. #define LEN(a) (sizeof(a) / sizeof(a[0]))
  51. #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
  52. #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
  53. #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
  54. #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
  55. #define IS_SET(flag) (term.mode & (flag))
  56. #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000)
  57. #define X2COL(x) (((x) - BORDER)/xw.cw)
  58. #define Y2ROW(y) (((y) - BORDER)/xw.ch)
  59. /* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */
  60. enum { ATTR_NULL=0 , ATTR_REVERSE=1 , ATTR_UNDERLINE=2, ATTR_BOLD=4, ATTR_GFX=8 };
  61. enum { CURSOR_UP, CURSOR_DOWN, CURSOR_LEFT, CURSOR_RIGHT,
  62. CURSOR_SAVE, CURSOR_LOAD };
  63. enum { CURSOR_DEFAULT = 0, CURSOR_HIDE = 1, CURSOR_WRAPNEXT = 2 };
  64. enum { GLYPH_SET=1, GLYPH_DIRTY=2 };
  65. enum { MODE_WRAP=1, MODE_INSERT=2, MODE_APPKEYPAD=4, MODE_ALTSCREEN=8,
  66. MODE_CRLF=16, MODE_MOUSEBTN=32, MODE_MOUSEMOTION=64, MODE_MOUSE=32|64, MODE_REVERSE=128 };
  67. enum { ESC_START=1, ESC_CSI=2, ESC_OSC=4, ESC_TITLE=8, ESC_ALTCHARSET=16 };
  68. enum { WIN_VISIBLE=1, WIN_REDRAW=2, WIN_FOCUSED=4 };
  69. #undef B0
  70. enum { B0=1, B1=2, B2=4, B3=8, B4=16, B5=32, B6=64, B7=128 };
  71. typedef struct {
  72. char c[UTF_SIZ]; /* character code */
  73. char mode; /* attribute flags */
  74. int fg; /* foreground */
  75. int bg; /* background */
  76. char state; /* state flags */
  77. } Glyph;
  78. typedef Glyph* Line;
  79. typedef struct {
  80. Glyph attr; /* current char attributes */
  81. int x;
  82. int y;
  83. char state;
  84. } TCursor;
  85. /* CSI Escape sequence structs */
  86. /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
  87. typedef struct {
  88. char buf[ESC_BUF_SIZ]; /* raw string */
  89. int len; /* raw string length */
  90. char priv;
  91. int arg[ESC_ARG_SIZ];
  92. int narg; /* nb of args */
  93. char mode;
  94. } CSIEscape;
  95. /* Internal representation of the screen */
  96. typedef struct {
  97. int row; /* nb row */
  98. int col; /* nb col */
  99. Line* line; /* screen */
  100. Line* alt; /* alternate screen */
  101. char* dirty; /* dirtyness of lines */
  102. TCursor c; /* cursor */
  103. int top; /* top scroll limit */
  104. int bot; /* bottom scroll limit */
  105. int mode; /* terminal mode flags */
  106. int esc; /* escape state flags */
  107. char title[ESC_TITLE_SIZ];
  108. int titlelen;
  109. } Term;
  110. /* Purely graphic info */
  111. typedef struct {
  112. Display* dpy;
  113. Colormap cmap;
  114. Window win;
  115. Pixmap buf;
  116. Atom xembed;
  117. XIM xim;
  118. XIC xic;
  119. int scr;
  120. int w; /* window width */
  121. int h; /* window height */
  122. int bufw; /* pixmap width */
  123. int bufh; /* pixmap height */
  124. int ch; /* char height */
  125. int cw; /* char width */
  126. char state; /* focus, redraw, visible */
  127. } XWindow;
  128. typedef struct {
  129. KeySym k;
  130. unsigned int mask;
  131. char s[ESC_BUF_SIZ];
  132. } Key;
  133. /* Drawing Context */
  134. typedef struct {
  135. unsigned long col[256];
  136. GC gc;
  137. struct {
  138. int ascent;
  139. int descent;
  140. short lbearing;
  141. short rbearing;
  142. XFontSet set;
  143. } font, bfont;
  144. } DC;
  145. /* TODO: use better name for vars... */
  146. typedef struct {
  147. int mode;
  148. int bx, by;
  149. int ex, ey;
  150. struct {int x, y;} b, e;
  151. char *clip;
  152. Atom xtarget;
  153. struct timeval tclick1;
  154. struct timeval tclick2;
  155. } Selection;
  156. #include "config.h"
  157. static void die(const char*, ...);
  158. static void draw(void);
  159. static void drawregion(int, int, int, int);
  160. static void execsh(void);
  161. static void sigchld(int);
  162. static void run(void);
  163. static void csidump(void);
  164. static void csihandle(void);
  165. static void csiparse(void);
  166. static void csireset(void);
  167. static void tclearregion(int, int, int, int);
  168. static void tcursor(int);
  169. static void tdeletechar(int);
  170. static void tdeleteline(int);
  171. static void tinsertblank(int);
  172. static void tinsertblankline(int);
  173. static void tmoveto(int, int);
  174. static void tnew(int, int);
  175. static void tnewline(int);
  176. static void tputtab(void);
  177. static void tputc(char*);
  178. static void treset(void);
  179. static int tresize(int, int);
  180. static void tscrollup(int, int);
  181. static void tscrolldown(int, int);
  182. static void tsetattr(int*, int);
  183. static void tsetchar(char*);
  184. static void tsetscroll(int, int);
  185. static void tswapscreen(void);
  186. static void tfulldirt(void);
  187. static void ttynew(void);
  188. static void ttyread(void);
  189. static void ttyresize(int, int);
  190. static void ttywrite(const char *, size_t);
  191. static void xdraws(char *, Glyph, int, int, int, int);
  192. static void xhints(void);
  193. static void xclear(int, int, int, int);
  194. static void xdrawcursor(void);
  195. static void xinit(void);
  196. static void xloadcols(void);
  197. static void xseturgency(int);
  198. static void xsetsel(char*);
  199. static void xresize(int, int);
  200. static void expose(XEvent *);
  201. static void visibility(XEvent *);
  202. static void unmap(XEvent *);
  203. static char* kmap(KeySym, unsigned int state);
  204. static void kpress(XEvent *);
  205. static void cmessage(XEvent *);
  206. static void resize(XEvent *);
  207. static void focus(XEvent *);
  208. static void brelease(XEvent *);
  209. static void bpress(XEvent *);
  210. static void bmotion(XEvent *);
  211. static void selnotify(XEvent *);
  212. static void selrequest(XEvent *);
  213. static void selinit(void);
  214. static inline int selected(int, int);
  215. static void selcopy(void);
  216. static void selpaste();
  217. static void selscroll(int, int);
  218. static int utf8decode(char *, long *);
  219. static int utf8encode(long *, char *);
  220. static int utf8size(char *);
  221. static int isfullutf8(char *, int);
  222. static void (*handler[LASTEvent])(XEvent *) = {
  223. [KeyPress] = kpress,
  224. [ClientMessage] = cmessage,
  225. [ConfigureNotify] = resize,
  226. [VisibilityNotify] = visibility,
  227. [UnmapNotify] = unmap,
  228. [Expose] = expose,
  229. [FocusIn] = focus,
  230. [FocusOut] = focus,
  231. [MotionNotify] = bmotion,
  232. [ButtonPress] = bpress,
  233. [ButtonRelease] = brelease,
  234. [SelectionNotify] = selnotify,
  235. [SelectionRequest] = selrequest,
  236. };
  237. /* Globals */
  238. static DC dc;
  239. static XWindow xw;
  240. static Term term;
  241. static CSIEscape escseq;
  242. static int cmdfd;
  243. static pid_t pid;
  244. static Selection sel;
  245. static char **opt_cmd = NULL;
  246. static char *opt_title = NULL;
  247. static char *opt_embed = NULL;
  248. static char *opt_class = NULL;
  249. int
  250. utf8decode(char *s, long *u) {
  251. unsigned char c;
  252. int i, n, rtn;
  253. rtn = 1;
  254. c = *s;
  255. if(~c&B7) { /* 0xxxxxxx */
  256. *u = c;
  257. return rtn;
  258. } else if((c&(B7|B6|B5)) == (B7|B6)) { /* 110xxxxx */
  259. *u = c&(B4|B3|B2|B1|B0);
  260. n = 1;
  261. } else if((c&(B7|B6|B5|B4)) == (B7|B6|B5)) { /* 1110xxxx */
  262. *u = c&(B3|B2|B1|B0);
  263. n = 2;
  264. } else if((c&(B7|B6|B5|B4|B3)) == (B7|B6|B5|B4)) { /* 11110xxx */
  265. *u = c&(B2|B1|B0);
  266. n = 3;
  267. } else
  268. goto invalid;
  269. for(i=n,++s; i>0; --i,++rtn,++s) {
  270. c = *s;
  271. if((c&(B7|B6)) != B7) /* 10xxxxxx */
  272. goto invalid;
  273. *u <<= 6;
  274. *u |= c&(B5|B4|B3|B2|B1|B0);
  275. }
  276. if((n == 1 && *u < 0x80) ||
  277. (n == 2 && *u < 0x800) ||
  278. (n == 3 && *u < 0x10000) ||
  279. (*u >= 0xD800 && *u <= 0xDFFF))
  280. goto invalid;
  281. return rtn;
  282. invalid:
  283. *u = 0xFFFD;
  284. return rtn;
  285. }
  286. int
  287. utf8encode(long *u, char *s) {
  288. unsigned char *sp;
  289. unsigned long uc;
  290. int i, n;
  291. sp = (unsigned char*) s;
  292. uc = *u;
  293. if(uc < 0x80) {
  294. *sp = uc; /* 0xxxxxxx */
  295. return 1;
  296. } else if(*u < 0x800) {
  297. *sp = (uc >> 6) | (B7|B6); /* 110xxxxx */
  298. n = 1;
  299. } else if(uc < 0x10000) {
  300. *sp = (uc >> 12) | (B7|B6|B5); /* 1110xxxx */
  301. n = 2;
  302. } else if(uc <= 0x10FFFF) {
  303. *sp = (uc >> 18) | (B7|B6|B5|B4); /* 11110xxx */
  304. n = 3;
  305. } else {
  306. goto invalid;
  307. }
  308. for(i=n,++sp; i>0; --i,++sp)
  309. *sp = ((uc >> 6*(i-1)) & (B5|B4|B3|B2|B1|B0)) | B7; /* 10xxxxxx */
  310. return n+1;
  311. invalid:
  312. /* U+FFFD */
  313. *s++ = '\xEF';
  314. *s++ = '\xBF';
  315. *s = '\xBD';
  316. return 3;
  317. }
  318. /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
  319. UTF-8 otherwise return 0 */
  320. int
  321. isfullutf8(char *s, int b) {
  322. unsigned char *c1, *c2, *c3;
  323. c1 = (unsigned char *) s;
  324. c2 = (unsigned char *) ++s;
  325. c3 = (unsigned char *) ++s;
  326. if(b < 1)
  327. return 0;
  328. else if((*c1&(B7|B6|B5)) == (B7|B6) && b == 1)
  329. return 0;
  330. else if((*c1&(B7|B6|B5|B4)) == (B7|B6|B5) &&
  331. ((b == 1) ||
  332. ((b == 2) && (*c2&(B7|B6)) == B7)))
  333. return 0;
  334. else if((*c1&(B7|B6|B5|B4|B3)) == (B7|B6|B5|B4) &&
  335. ((b == 1) ||
  336. ((b == 2) && (*c2&(B7|B6)) == B7) ||
  337. ((b == 3) && (*c2&(B7|B6)) == B7 && (*c3&(B7|B6)) == B7)))
  338. return 0;
  339. else
  340. return 1;
  341. }
  342. int
  343. utf8size(char *s) {
  344. unsigned char c = *s;
  345. if(~c&B7)
  346. return 1;
  347. else if((c&(B7|B6|B5)) == (B7|B6))
  348. return 2;
  349. else if((c&(B7|B6|B5|B4)) == (B7|B6|B5))
  350. return 3;
  351. else
  352. return 4;
  353. }
  354. void
  355. selinit(void) {
  356. sel.tclick1.tv_sec = 0;
  357. sel.tclick1.tv_usec = 0;
  358. sel.mode = 0;
  359. sel.bx = -1;
  360. sel.clip = NULL;
  361. sel.xtarget = XInternAtom(xw.dpy, "UTF8_STRING", 0);
  362. if(sel.xtarget == None)
  363. sel.xtarget = XA_STRING;
  364. }
  365. static inline int
  366. selected(int x, int y) {
  367. if(sel.ey == y && sel.by == y) {
  368. int bx = MIN(sel.bx, sel.ex);
  369. int ex = MAX(sel.bx, sel.ex);
  370. return BETWEEN(x, bx, ex);
  371. }
  372. return ((sel.b.y < y&&y < sel.e.y) || (y==sel.e.y && x<=sel.e.x))
  373. || (y==sel.b.y && x>=sel.b.x && (x<=sel.e.x || sel.b.y!=sel.e.y));
  374. }
  375. void
  376. getbuttoninfo(XEvent *e, int *b, int *x, int *y) {
  377. if(b)
  378. *b = e->xbutton.button;
  379. *x = X2COL(e->xbutton.x);
  380. *y = Y2ROW(e->xbutton.y);
  381. sel.b.x = sel.by < sel.ey ? sel.bx : sel.ex;
  382. sel.b.y = MIN(sel.by, sel.ey);
  383. sel.e.x = sel.by < sel.ey ? sel.ex : sel.bx;
  384. sel.e.y = MAX(sel.by, sel.ey);
  385. }
  386. void
  387. mousereport(XEvent *e) {
  388. int x = X2COL(e->xbutton.x);
  389. int y = Y2ROW(e->xbutton.y);
  390. int button = e->xbutton.button;
  391. int state = e->xbutton.state;
  392. char buf[] = { '\033', '[', 'M', 0, 32+x+1, 32+y+1 };
  393. static int ob, ox, oy;
  394. /* from urxvt */
  395. if(e->xbutton.type == MotionNotify) {
  396. if(!IS_SET(MODE_MOUSEMOTION) || (x == ox && y == oy))
  397. return;
  398. button = ob + 32;
  399. ox = x, oy = y;
  400. } else if(e->xbutton.type == ButtonRelease || button == AnyButton) {
  401. button = 3;
  402. } else {
  403. button -= Button1;
  404. if(button >= 3)
  405. button += 64 - 3;
  406. if(e->xbutton.type == ButtonPress) {
  407. ob = button;
  408. ox = x, oy = y;
  409. }
  410. }
  411. buf[3] = 32 + button + (state & ShiftMask ? 4 : 0)
  412. + (state & Mod4Mask ? 8 : 0)
  413. + (state & ControlMask ? 16 : 0);
  414. ttywrite(buf, sizeof(buf));
  415. }
  416. void
  417. bpress(XEvent *e) {
  418. if(IS_SET(MODE_MOUSE))
  419. mousereport(e);
  420. else if(e->xbutton.button == Button1) {
  421. if(sel.bx != -1)
  422. for(int i=sel.b.y; i<=sel.e.y; i++)
  423. term.dirty[i] = 1;
  424. sel.mode = 1;
  425. sel.ex = sel.bx = X2COL(e->xbutton.x);
  426. sel.ey = sel.by = Y2ROW(e->xbutton.y);
  427. }
  428. }
  429. void
  430. selcopy(void) {
  431. char *str, *ptr;
  432. int x, y, sz, sl, ls = 0;
  433. if(sel.bx == -1)
  434. str = NULL;
  435. else {
  436. sz = (term.col+1) * (sel.e.y-sel.b.y+1) * UTF_SIZ;
  437. ptr = str = malloc(sz);
  438. for(y = 0; y < term.row; y++) {
  439. for(x = 0; x < term.col; x++)
  440. if(term.line[y][x].state & GLYPH_SET && (ls = selected(x, y))) {
  441. sl = utf8size(term.line[y][x].c);
  442. memcpy(ptr, term.line[y][x].c, sl);
  443. ptr += sl;
  444. }
  445. if(ls && y < sel.e.y)
  446. *ptr++ = '\n';
  447. }
  448. *ptr = 0;
  449. }
  450. xsetsel(str);
  451. }
  452. void
  453. selnotify(XEvent *e) {
  454. unsigned long nitems, ofs, rem;
  455. int format;
  456. unsigned char *data;
  457. Atom type;
  458. ofs = 0;
  459. do {
  460. if(XGetWindowProperty(xw.dpy, xw.win, XA_PRIMARY, ofs, BUFSIZ/4,
  461. False, AnyPropertyType, &type, &format,
  462. &nitems, &rem, &data)) {
  463. fprintf(stderr, "Clipboard allocation failed\n");
  464. return;
  465. }
  466. ttywrite((const char *) data, nitems * format / 8);
  467. XFree(data);
  468. /* number of 32-bit chunks returned */
  469. ofs += nitems * format / 32;
  470. } while(rem > 0);
  471. }
  472. void
  473. selpaste() {
  474. XConvertSelection(xw.dpy, XA_PRIMARY, sel.xtarget, XA_PRIMARY, xw.win, CurrentTime);
  475. }
  476. void
  477. selrequest(XEvent *e) {
  478. XSelectionRequestEvent *xsre;
  479. XSelectionEvent xev;
  480. Atom xa_targets;
  481. xsre = (XSelectionRequestEvent *) e;
  482. xev.type = SelectionNotify;
  483. xev.requestor = xsre->requestor;
  484. xev.selection = xsre->selection;
  485. xev.target = xsre->target;
  486. xev.time = xsre->time;
  487. /* reject */
  488. xev.property = None;
  489. xa_targets = XInternAtom(xw.dpy, "TARGETS", 0);
  490. if(xsre->target == xa_targets) {
  491. /* respond with the supported type */
  492. Atom string = sel.xtarget;
  493. XChangeProperty(xsre->display, xsre->requestor, xsre->property,
  494. XA_ATOM, 32, PropModeReplace,
  495. (unsigned char *) &string, 1);
  496. xev.property = xsre->property;
  497. } else if(xsre->target == sel.xtarget) {
  498. XChangeProperty(xsre->display, xsre->requestor, xsre->property,
  499. xsre->target, 8, PropModeReplace,
  500. (unsigned char *) sel.clip, strlen(sel.clip));
  501. xev.property = xsre->property;
  502. }
  503. /* all done, send a notification to the listener */
  504. if(!XSendEvent(xsre->display, xsre->requestor, True, 0, (XEvent *) &xev))
  505. fprintf(stderr, "Error sending SelectionNotify event\n");
  506. }
  507. void
  508. xsetsel(char *str) {
  509. /* register the selection for both the clipboard and the primary */
  510. Atom clipboard;
  511. free(sel.clip);
  512. sel.clip = str;
  513. XSetSelectionOwner(xw.dpy, XA_PRIMARY, xw.win, CurrentTime);
  514. clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
  515. XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
  516. XFlush(xw.dpy);
  517. }
  518. void
  519. brelease(XEvent *e) {
  520. if(IS_SET(MODE_MOUSE)) {
  521. mousereport(e);
  522. return;
  523. }
  524. if(e->xbutton.button == Button2)
  525. selpaste();
  526. else if(e->xbutton.button == Button1) {
  527. sel.mode = 0;
  528. getbuttoninfo(e, NULL, &sel.ex, &sel.ey);
  529. term.dirty[sel.ey] = 1;
  530. if(sel.bx == sel.ex && sel.by == sel.ey) {
  531. struct timeval now;
  532. sel.bx = -1;
  533. gettimeofday(&now, NULL);
  534. if(TIMEDIFF(now, sel.tclick2) <= TRIPLECLICK_TIMEOUT) {
  535. /* triple click on the line */
  536. sel.b.x = sel.bx = 0;
  537. sel.e.x = sel.ex = term.col;
  538. sel.b.y = sel.e.y = sel.ey;
  539. selcopy();
  540. } else if(TIMEDIFF(now, sel.tclick1) <= DOUBLECLICK_TIMEOUT) {
  541. /* double click to select word */
  542. sel.bx = sel.ex;
  543. while(sel.bx > 0 && term.line[sel.ey][sel.bx-1].state & GLYPH_SET &&
  544. term.line[sel.ey][sel.bx-1].c[0] != ' ') sel.bx--;
  545. sel.b.x = sel.bx;
  546. while(sel.ex < term.col-1 && term.line[sel.ey][sel.ex+1].state & GLYPH_SET &&
  547. term.line[sel.ey][sel.ex+1].c[0] != ' ') sel.ex++;
  548. sel.e.x = sel.ex;
  549. sel.b.y = sel.e.y = sel.ey;
  550. selcopy();
  551. }
  552. } else
  553. selcopy();
  554. }
  555. memcpy(&sel.tclick2, &sel.tclick1, sizeof(struct timeval));
  556. gettimeofday(&sel.tclick1, NULL);
  557. draw();
  558. }
  559. void
  560. bmotion(XEvent *e) {
  561. if(IS_SET(MODE_MOUSE)) {
  562. mousereport(e);
  563. return;
  564. }
  565. if(sel.mode) {
  566. int oldey = sel.ey, oldex = sel.ex;
  567. getbuttoninfo(e, NULL, &sel.ex, &sel.ey);
  568. if(oldey != sel.ey || oldex != sel.ex) {
  569. int starty = MIN(oldey, sel.ey);
  570. int endy = MAX(oldey, sel.ey);
  571. for(int i=starty; i<=endy; i++)
  572. term.dirty[i] = 1;
  573. draw();
  574. }
  575. }
  576. }
  577. void
  578. die(const char *errstr, ...) {
  579. va_list ap;
  580. va_start(ap, errstr);
  581. vfprintf(stderr, errstr, ap);
  582. va_end(ap);
  583. exit(EXIT_FAILURE);
  584. }
  585. void
  586. execsh(void) {
  587. char **args;
  588. char *envshell = getenv("SHELL");
  589. DEFAULT(envshell, "sh");
  590. putenv("TERM="TNAME);
  591. args = opt_cmd ? opt_cmd : (char*[]){envshell, "-i", NULL};
  592. execvp(args[0], args);
  593. exit(EXIT_FAILURE);
  594. }
  595. void
  596. sigchld(int a) {
  597. int stat = 0;
  598. if(waitpid(pid, &stat, 0) < 0)
  599. die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
  600. if(WIFEXITED(stat))
  601. exit(WEXITSTATUS(stat));
  602. else
  603. exit(EXIT_FAILURE);
  604. }
  605. void
  606. ttynew(void) {
  607. int m, s;
  608. /* seems to work fine on linux, openbsd and freebsd */
  609. struct winsize w = {term.row, term.col, 0, 0};
  610. if(openpty(&m, &s, NULL, NULL, &w) < 0)
  611. die("openpty failed: %s\n", SERRNO);
  612. switch(pid = fork()) {
  613. case -1:
  614. die("fork failed\n");
  615. break;
  616. case 0:
  617. setsid(); /* create a new process group */
  618. dup2(s, STDIN_FILENO);
  619. dup2(s, STDOUT_FILENO);
  620. dup2(s, STDERR_FILENO);
  621. if(ioctl(s, TIOCSCTTY, NULL) < 0)
  622. die("ioctl TIOCSCTTY failed: %s\n", SERRNO);
  623. close(s);
  624. close(m);
  625. execsh();
  626. break;
  627. default:
  628. close(s);
  629. cmdfd = m;
  630. signal(SIGCHLD, sigchld);
  631. }
  632. }
  633. void
  634. dump(char c) {
  635. static int col;
  636. fprintf(stderr, " %02x '%c' ", c, isprint(c)?c:'.');
  637. if(++col % 10 == 0)
  638. fprintf(stderr, "\n");
  639. }
  640. void
  641. ttyread(void) {
  642. static char buf[BUFSIZ];
  643. static int buflen = 0;
  644. char *ptr;
  645. char s[UTF_SIZ];
  646. int charsize; /* size of utf8 char in bytes */
  647. long utf8c;
  648. int ret;
  649. /* append read bytes to unprocessed bytes */
  650. if((ret = read(cmdfd, buf+buflen, LEN(buf)-buflen)) < 0)
  651. die("Couldn't read from shell: %s\n", SERRNO);
  652. /* process every complete utf8 char */
  653. buflen += ret;
  654. ptr = buf;
  655. while(buflen >= UTF_SIZ || isfullutf8(ptr,buflen)) {
  656. charsize = utf8decode(ptr, &utf8c);
  657. utf8encode(&utf8c, s);
  658. tputc(s);
  659. ptr += charsize;
  660. buflen -= charsize;
  661. }
  662. /* keep any uncomplete utf8 char for the next call */
  663. memmove(buf, ptr, buflen);
  664. }
  665. void
  666. ttywrite(const char *s, size_t n) {
  667. if(write(cmdfd, s, n) == -1)
  668. die("write error on tty: %s\n", SERRNO);
  669. }
  670. void
  671. ttyresize(int x, int y) {
  672. struct winsize w;
  673. w.ws_row = term.row;
  674. w.ws_col = term.col;
  675. w.ws_xpixel = w.ws_ypixel = 0;
  676. if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
  677. fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
  678. }
  679. void
  680. tfulldirt(void)
  681. {
  682. int i;
  683. for(i = 0; i < term.row; i++)
  684. term.dirty[i] = 1;
  685. }
  686. void
  687. tcursor(int mode) {
  688. static TCursor c;
  689. if(mode == CURSOR_SAVE)
  690. c = term.c;
  691. else if(mode == CURSOR_LOAD)
  692. term.c = c, tmoveto(c.x, c.y);
  693. }
  694. void
  695. treset(void) {
  696. term.c = (TCursor){{
  697. .mode = ATTR_NULL,
  698. .fg = DefaultFG,
  699. .bg = DefaultBG
  700. }, .x = 0, .y = 0, .state = CURSOR_DEFAULT};
  701. term.top = 0, term.bot = term.row - 1;
  702. term.mode = MODE_WRAP;
  703. tclearregion(0, 0, term.col-1, term.row-1);
  704. }
  705. void
  706. tnew(int col, int row) {
  707. /* set screen size */
  708. term.row = row, term.col = col;
  709. term.line = malloc(term.row * sizeof(Line));
  710. term.alt = malloc(term.row * sizeof(Line));
  711. term.dirty = malloc(term.row * sizeof(*term.dirty));
  712. for(row = 0; row < term.row; row++) {
  713. term.line[row] = malloc(term.col * sizeof(Glyph));
  714. term.alt [row] = malloc(term.col * sizeof(Glyph));
  715. term.dirty[row] = 0;
  716. }
  717. /* setup screen */
  718. treset();
  719. }
  720. void
  721. tswapscreen(void) {
  722. Line* tmp = term.line;
  723. term.line = term.alt;
  724. term.alt = tmp;
  725. term.mode ^= MODE_ALTSCREEN;
  726. tfulldirt();
  727. }
  728. void
  729. tscrolldown(int orig, int n) {
  730. int i;
  731. Line temp;
  732. LIMIT(n, 0, term.bot-orig+1);
  733. tclearregion(0, term.bot-n+1, term.col-1, term.bot);
  734. for(i = term.bot; i >= orig+n; i--) {
  735. temp = term.line[i];
  736. term.line[i] = term.line[i-n];
  737. term.line[i-n] = temp;
  738. term.dirty[i] = 1;
  739. term.dirty[i-n] = 1;
  740. }
  741. selscroll(orig, n);
  742. }
  743. void
  744. tscrollup(int orig, int n) {
  745. int i;
  746. Line temp;
  747. LIMIT(n, 0, term.bot-orig+1);
  748. tclearregion(0, orig, term.col-1, orig+n-1);
  749. for(i = orig; i <= term.bot-n; i++) {
  750. temp = term.line[i];
  751. term.line[i] = term.line[i+n];
  752. term.line[i+n] = temp;
  753. term.dirty[i] = 1;
  754. term.dirty[i+n] = 1;
  755. }
  756. selscroll(orig, -n);
  757. }
  758. void
  759. selscroll(int orig, int n) {
  760. if(sel.bx == -1)
  761. return;
  762. if(BETWEEN(sel.by, orig, term.bot) || BETWEEN(sel.ey, orig, term.bot)) {
  763. if((sel.by += n) > term.bot || (sel.ey += n) < term.top) {
  764. sel.bx = -1;
  765. return;
  766. }
  767. if(sel.by < term.top) {
  768. sel.by = term.top;
  769. sel.bx = 0;
  770. }
  771. if(sel.ey > term.bot) {
  772. sel.ey = term.bot;
  773. sel.ex = term.col;
  774. }
  775. sel.b.y = sel.by, sel.b.x = sel.bx;
  776. sel.e.y = sel.ey, sel.e.x = sel.ex;
  777. }
  778. }
  779. void
  780. tnewline(int first_col) {
  781. int y = term.c.y;
  782. if(y == term.bot)
  783. tscrollup(term.top, 1);
  784. else
  785. y++;
  786. tmoveto(first_col ? 0 : term.c.x, y);
  787. }
  788. void
  789. csiparse(void) {
  790. /* int noarg = 1; */
  791. char *p = escseq.buf;
  792. escseq.narg = 0;
  793. if(*p == '?')
  794. escseq.priv = 1, p++;
  795. while(p < escseq.buf+escseq.len) {
  796. while(isdigit(*p)) {
  797. escseq.arg[escseq.narg] *= 10;
  798. escseq.arg[escseq.narg] += *p++ - '0'/*, noarg = 0 */;
  799. }
  800. if(*p == ';' && escseq.narg+1 < ESC_ARG_SIZ)
  801. escseq.narg++, p++;
  802. else {
  803. escseq.mode = *p;
  804. escseq.narg++;
  805. return;
  806. }
  807. }
  808. }
  809. void
  810. tmoveto(int x, int y) {
  811. LIMIT(x, 0, term.col-1);
  812. LIMIT(y, 0, term.row-1);
  813. term.c.state &= ~CURSOR_WRAPNEXT;
  814. term.c.x = x;
  815. term.c.y = y;
  816. }
  817. void
  818. tsetchar(char *c) {
  819. term.dirty[term.c.y] = 1;
  820. term.line[term.c.y][term.c.x] = term.c.attr;
  821. memcpy(term.line[term.c.y][term.c.x].c, c, UTF_SIZ);
  822. term.line[term.c.y][term.c.x].state |= GLYPH_SET;
  823. }
  824. void
  825. tclearregion(int x1, int y1, int x2, int y2) {
  826. int x, y, temp;
  827. if(x1 > x2)
  828. temp = x1, x1 = x2, x2 = temp;
  829. if(y1 > y2)
  830. temp = y1, y1 = y2, y2 = temp;
  831. LIMIT(x1, 0, term.col-1);
  832. LIMIT(x2, 0, term.col-1);
  833. LIMIT(y1, 0, term.row-1);
  834. LIMIT(y2, 0, term.row-1);
  835. for(y = y1; y <= y2; y++) {
  836. term.dirty[y] = 1;
  837. for(x = x1; x <= x2; x++)
  838. term.line[y][x].state = 0;
  839. }
  840. }
  841. void
  842. tdeletechar(int n) {
  843. int src = term.c.x + n;
  844. int dst = term.c.x;
  845. int size = term.col - src;
  846. term.dirty[term.c.y] = 1;
  847. if(src >= term.col) {
  848. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  849. return;
  850. }
  851. memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
  852. tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
  853. }
  854. void
  855. tinsertblank(int n) {
  856. int src = term.c.x;
  857. int dst = src + n;
  858. int size = term.col - dst;
  859. term.dirty[term.c.y] = 1;
  860. if(dst >= term.col) {
  861. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  862. return;
  863. }
  864. memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph));
  865. tclearregion(src, term.c.y, dst - 1, term.c.y);
  866. }
  867. void
  868. tinsertblankline(int n) {
  869. if(term.c.y < term.top || term.c.y > term.bot)
  870. return;
  871. tscrolldown(term.c.y, n);
  872. }
  873. void
  874. tdeleteline(int n) {
  875. if(term.c.y < term.top || term.c.y > term.bot)
  876. return;
  877. tscrollup(term.c.y, n);
  878. }
  879. void
  880. tsetattr(int *attr, int l) {
  881. int i;
  882. for(i = 0; i < l; i++) {
  883. switch(attr[i]) {
  884. case 0:
  885. term.c.attr.mode &= ~(ATTR_REVERSE | ATTR_UNDERLINE | ATTR_BOLD);
  886. term.c.attr.fg = DefaultFG;
  887. term.c.attr.bg = DefaultBG;
  888. break;
  889. case 1:
  890. term.c.attr.mode |= ATTR_BOLD;
  891. break;
  892. case 4:
  893. term.c.attr.mode |= ATTR_UNDERLINE;
  894. break;
  895. case 7:
  896. term.c.attr.mode |= ATTR_REVERSE;
  897. break;
  898. case 22:
  899. term.c.attr.mode &= ~ATTR_BOLD;
  900. break;
  901. case 24:
  902. term.c.attr.mode &= ~ATTR_UNDERLINE;
  903. break;
  904. case 27:
  905. term.c.attr.mode &= ~ATTR_REVERSE;
  906. break;
  907. case 38:
  908. if(i + 2 < l && attr[i + 1] == 5) {
  909. i += 2;
  910. if(BETWEEN(attr[i], 0, 255))
  911. term.c.attr.fg = attr[i];
  912. else
  913. fprintf(stderr, "erresc: bad fgcolor %d\n", attr[i]);
  914. }
  915. else
  916. fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
  917. break;
  918. case 39:
  919. term.c.attr.fg = DefaultFG;
  920. break;
  921. case 48:
  922. if(i + 2 < l && attr[i + 1] == 5) {
  923. i += 2;
  924. if(BETWEEN(attr[i], 0, 255))
  925. term.c.attr.bg = attr[i];
  926. else
  927. fprintf(stderr, "erresc: bad bgcolor %d\n", attr[i]);
  928. }
  929. else
  930. fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]);
  931. break;
  932. case 49:
  933. term.c.attr.bg = DefaultBG;
  934. break;
  935. default:
  936. if(BETWEEN(attr[i], 30, 37))
  937. term.c.attr.fg = attr[i] - 30;
  938. else if(BETWEEN(attr[i], 40, 47))
  939. term.c.attr.bg = attr[i] - 40;
  940. else if(BETWEEN(attr[i], 90, 97))
  941. term.c.attr.fg = attr[i] - 90 + 8;
  942. else if(BETWEEN(attr[i], 100, 107))
  943. term.c.attr.fg = attr[i] - 100 + 8;
  944. else
  945. fprintf(stderr, "erresc: gfx attr %d unknown\n", attr[i]), csidump();
  946. break;
  947. }
  948. }
  949. }
  950. void
  951. tsetscroll(int t, int b) {
  952. int temp;
  953. LIMIT(t, 0, term.row-1);
  954. LIMIT(b, 0, term.row-1);
  955. if(t > b) {
  956. temp = t;
  957. t = b;
  958. b = temp;
  959. }
  960. term.top = t;
  961. term.bot = b;
  962. }
  963. void
  964. csihandle(void) {
  965. switch(escseq.mode) {
  966. default:
  967. unknown:
  968. fprintf(stderr, "erresc: unknown csi ");
  969. csidump();
  970. /* die(""); */
  971. break;
  972. case '@': /* ICH -- Insert <n> blank char */
  973. DEFAULT(escseq.arg[0], 1);
  974. tinsertblank(escseq.arg[0]);
  975. break;
  976. case 'A': /* CUU -- Cursor <n> Up */
  977. case 'e':
  978. DEFAULT(escseq.arg[0], 1);
  979. tmoveto(term.c.x, term.c.y-escseq.arg[0]);
  980. break;
  981. case 'B': /* CUD -- Cursor <n> Down */
  982. DEFAULT(escseq.arg[0], 1);
  983. tmoveto(term.c.x, term.c.y+escseq.arg[0]);
  984. break;
  985. case 'C': /* CUF -- Cursor <n> Forward */
  986. case 'a':
  987. DEFAULT(escseq.arg[0], 1);
  988. tmoveto(term.c.x+escseq.arg[0], term.c.y);
  989. break;
  990. case 'D': /* CUB -- Cursor <n> Backward */
  991. DEFAULT(escseq.arg[0], 1);
  992. tmoveto(term.c.x-escseq.arg[0], term.c.y);
  993. break;
  994. case 'E': /* CNL -- Cursor <n> Down and first col */
  995. DEFAULT(escseq.arg[0], 1);
  996. tmoveto(0, term.c.y+escseq.arg[0]);
  997. break;
  998. case 'F': /* CPL -- Cursor <n> Up and first col */
  999. DEFAULT(escseq.arg[0], 1);
  1000. tmoveto(0, term.c.y-escseq.arg[0]);
  1001. break;
  1002. case 'G': /* CHA -- Move to <col> */
  1003. case '`': /* XXX: HPA -- same? */
  1004. DEFAULT(escseq.arg[0], 1);
  1005. tmoveto(escseq.arg[0]-1, term.c.y);
  1006. break;
  1007. case 'H': /* CUP -- Move to <row> <col> */
  1008. case 'f': /* XXX: HVP -- same? */
  1009. DEFAULT(escseq.arg[0], 1);
  1010. DEFAULT(escseq.arg[1], 1);
  1011. tmoveto(escseq.arg[1]-1, escseq.arg[0]-1);
  1012. break;
  1013. /* XXX: (CSI n I) CHT -- Cursor Forward Tabulation <n> tab stops */
  1014. case 'J': /* ED -- Clear screen */
  1015. sel.bx = -1;
  1016. switch(escseq.arg[0]) {
  1017. case 0: /* below */
  1018. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  1019. if(term.c.y < term.row-1)
  1020. tclearregion(0, term.c.y+1, term.col-1, term.row-1);
  1021. break;
  1022. case 1: /* above */
  1023. if(term.c.y > 1)
  1024. tclearregion(0, 0, term.col-1, term.c.y-1);
  1025. tclearregion(0, term.c.y, term.c.x, term.c.y);
  1026. break;
  1027. case 2: /* all */
  1028. tclearregion(0, 0, term.col-1, term.row-1);
  1029. break;
  1030. default:
  1031. goto unknown;
  1032. }
  1033. break;
  1034. case 'K': /* EL -- Clear line */
  1035. switch(escseq.arg[0]) {
  1036. case 0: /* right */
  1037. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  1038. break;
  1039. case 1: /* left */
  1040. tclearregion(0, term.c.y, term.c.x, term.c.y);
  1041. break;
  1042. case 2: /* all */
  1043. tclearregion(0, term.c.y, term.col-1, term.c.y);
  1044. break;
  1045. }
  1046. break;
  1047. case 'S': /* SU -- Scroll <n> line up */
  1048. DEFAULT(escseq.arg[0], 1);
  1049. tscrollup(term.top, escseq.arg[0]);
  1050. break;
  1051. case 'T': /* SD -- Scroll <n> line down */
  1052. DEFAULT(escseq.arg[0], 1);
  1053. tscrolldown(term.top, escseq.arg[0]);
  1054. break;
  1055. case 'L': /* IL -- Insert <n> blank lines */
  1056. DEFAULT(escseq.arg[0], 1);
  1057. tinsertblankline(escseq.arg[0]);
  1058. break;
  1059. case 'l': /* RM -- Reset Mode */
  1060. if(escseq.priv) {
  1061. switch(escseq.arg[0]) {
  1062. case 1:
  1063. term.mode &= ~MODE_APPKEYPAD;
  1064. break;
  1065. case 5: /* DECSCNM -- Remove reverse video */
  1066. if(IS_SET(MODE_REVERSE)) {
  1067. term.mode &= ~MODE_REVERSE;
  1068. draw();
  1069. }
  1070. break;
  1071. case 7:
  1072. term.mode &= ~MODE_WRAP;
  1073. break;
  1074. case 12: /* att610 -- Stop blinking cursor (IGNORED) */
  1075. break;
  1076. case 20:
  1077. term.mode &= ~MODE_CRLF;
  1078. break;
  1079. case 25:
  1080. term.c.state |= CURSOR_HIDE;
  1081. break;
  1082. case 1000: /* disable X11 xterm mouse reporting */
  1083. term.mode &= ~MODE_MOUSEBTN;
  1084. break;
  1085. case 1002:
  1086. term.mode &= ~MODE_MOUSEMOTION;
  1087. break;
  1088. case 1049: /* = 1047 and 1048 */
  1089. case 47:
  1090. case 1047:
  1091. if(IS_SET(MODE_ALTSCREEN)) {
  1092. tclearregion(0, 0, term.col-1, term.row-1);
  1093. tswapscreen();
  1094. }
  1095. if(escseq.arg[0] != 1049)
  1096. break;
  1097. case 1048:
  1098. tcursor(CURSOR_LOAD);
  1099. break;
  1100. default:
  1101. goto unknown;
  1102. }
  1103. } else {
  1104. switch(escseq.arg[0]) {
  1105. case 4:
  1106. term.mode &= ~MODE_INSERT;
  1107. break;
  1108. default:
  1109. goto unknown;
  1110. }
  1111. }
  1112. break;
  1113. case 'M': /* DL -- Delete <n> lines */
  1114. DEFAULT(escseq.arg[0], 1);
  1115. tdeleteline(escseq.arg[0]);
  1116. break;
  1117. case 'X': /* ECH -- Erase <n> char */
  1118. DEFAULT(escseq.arg[0], 1);
  1119. tclearregion(term.c.x, term.c.y, term.c.x + escseq.arg[0], term.c.y);
  1120. break;
  1121. case 'P': /* DCH -- Delete <n> char */
  1122. DEFAULT(escseq.arg[0], 1);
  1123. tdeletechar(escseq.arg[0]);
  1124. break;
  1125. /* XXX: (CSI n Z) CBT -- Cursor Backward Tabulation <n> tab stops */
  1126. case 'd': /* VPA -- Move to <row> */
  1127. DEFAULT(escseq.arg[0], 1);
  1128. tmoveto(term.c.x, escseq.arg[0]-1);
  1129. break;
  1130. case 'h': /* SM -- Set terminal mode */
  1131. if(escseq.priv) {
  1132. switch(escseq.arg[0]) {
  1133. case 1:
  1134. term.mode |= MODE_APPKEYPAD;
  1135. break;
  1136. case 5: /* DECSCNM -- Reverve video */
  1137. if(!IS_SET(MODE_REVERSE)) {
  1138. term.mode |= MODE_REVERSE;
  1139. draw();
  1140. }
  1141. break;
  1142. case 7:
  1143. term.mode |= MODE_WRAP;
  1144. break;
  1145. case 20:
  1146. term.mode |= MODE_CRLF;
  1147. break;
  1148. case 12: /* att610 -- Start blinking cursor (IGNORED) */
  1149. /* fallthrough for xterm cvvis = CSI [ ? 12 ; 25 h */
  1150. if(escseq.narg > 1 && escseq.arg[1] != 25)
  1151. break;
  1152. case 25:
  1153. term.c.state &= ~CURSOR_HIDE;
  1154. break;
  1155. case 1000: /* 1000,1002: enable xterm mouse report */
  1156. term.mode |= MODE_MOUSEBTN;
  1157. break;
  1158. case 1002:
  1159. term.mode |= MODE_MOUSEMOTION;
  1160. break;
  1161. case 1049: /* = 1047 and 1048 */
  1162. case 47:
  1163. case 1047:
  1164. if(IS_SET(MODE_ALTSCREEN))
  1165. tclearregion(0, 0, term.col-1, term.row-1);
  1166. else
  1167. tswapscreen();
  1168. if(escseq.arg[0] != 1049)
  1169. break;
  1170. case 1048:
  1171. tcursor(CURSOR_SAVE);
  1172. break;
  1173. default: goto unknown;
  1174. }
  1175. } else {
  1176. switch(escseq.arg[0]) {
  1177. case 4:
  1178. term.mode |= MODE_INSERT;
  1179. break;
  1180. default: goto unknown;
  1181. }
  1182. };
  1183. break;
  1184. case 'm': /* SGR -- Terminal attribute (color) */
  1185. tsetattr(escseq.arg, escseq.narg);
  1186. break;
  1187. case 'r': /* DECSTBM -- Set Scrolling Region */
  1188. if(escseq.priv)
  1189. goto unknown;
  1190. else {
  1191. DEFAULT(escseq.arg[0], 1);
  1192. DEFAULT(escseq.arg[1], term.row);
  1193. tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1);
  1194. tmoveto(0, 0);
  1195. }
  1196. break;
  1197. case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
  1198. tcursor(CURSOR_SAVE);
  1199. break;
  1200. case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
  1201. tcursor(CURSOR_LOAD);
  1202. break;
  1203. }
  1204. }
  1205. void
  1206. csidump(void) {
  1207. int i;
  1208. printf("ESC [ %s", escseq.priv ? "? " : "");
  1209. if(escseq.narg)
  1210. for(i = 0; i < escseq.narg; i++)
  1211. printf("%d ", escseq.arg[i]);
  1212. if(escseq.mode)
  1213. putchar(escseq.mode);
  1214. putchar('\n');
  1215. }
  1216. void
  1217. csireset(void) {
  1218. memset(&escseq, 0, sizeof(escseq));
  1219. }
  1220. void
  1221. tputtab(void) {
  1222. int space = TAB - term.c.x % TAB;
  1223. tmoveto(term.c.x + space, term.c.y);
  1224. }
  1225. void
  1226. tputc(char *c) {
  1227. char ascii = *c;
  1228. if(term.esc & ESC_START) {
  1229. if(term.esc & ESC_CSI) {
  1230. escseq.buf[escseq.len++] = ascii;
  1231. if(BETWEEN(ascii, 0x40, 0x7E) || escseq.len >= ESC_BUF_SIZ) {
  1232. term.esc = 0;
  1233. csiparse(), csihandle();
  1234. }
  1235. /* TODO: handle other OSC */
  1236. } else if(term.esc & ESC_OSC) {
  1237. if(ascii == ';') {
  1238. term.titlelen = 0;
  1239. term.esc = ESC_START | ESC_TITLE;
  1240. }
  1241. } else if(term.esc & ESC_TITLE) {
  1242. if(ascii == '\a' || term.titlelen+1 >= ESC_TITLE_SIZ) {
  1243. term.esc = 0;
  1244. term.title[term.titlelen] = '\0';
  1245. XStoreName(xw.dpy, xw.win, term.title);
  1246. } else {
  1247. term.title[term.titlelen++] = ascii;
  1248. }
  1249. } else if(term.esc & ESC_ALTCHARSET) {
  1250. switch(ascii) {
  1251. case '0': /* Line drawing crap */
  1252. term.c.attr.mode |= ATTR_GFX;
  1253. break;
  1254. case 'B': /* Back to regular text */
  1255. term.c.attr.mode &= ~ATTR_GFX;
  1256. break;
  1257. default:
  1258. fprintf(stderr, "esc unhandled charset: ESC ( %c\n", ascii);
  1259. }
  1260. term.esc = 0;
  1261. } else {
  1262. switch(ascii) {
  1263. case '[':
  1264. term.esc |= ESC_CSI;
  1265. break;
  1266. case ']':
  1267. term.esc |= ESC_OSC;
  1268. break;
  1269. case '(':
  1270. term.esc |= ESC_ALTCHARSET;
  1271. break;
  1272. case 'D': /* IND -- Linefeed */
  1273. if(term.c.y == term.bot)
  1274. tscrollup(term.top, 1);
  1275. else
  1276. tmoveto(term.c.x, term.c.y+1);
  1277. term.esc = 0;
  1278. break;
  1279. case 'E': /* NEL -- Next line */
  1280. tnewline(1); /* always go to first col */
  1281. term.esc = 0;
  1282. break;
  1283. case 'M': /* RI -- Reverse index */
  1284. if(term.c.y == term.top)
  1285. tscrolldown(term.top, 1);
  1286. else
  1287. tmoveto(term.c.x, term.c.y-1);
  1288. term.esc = 0;
  1289. break;
  1290. case 'c': /* RIS -- Reset to inital state */
  1291. treset();
  1292. term.esc = 0;
  1293. break;
  1294. case '=': /* DECPAM -- Application keypad */
  1295. term.mode |= MODE_APPKEYPAD;
  1296. term.esc = 0;
  1297. break;
  1298. case '>': /* DECPNM -- Normal keypad */
  1299. term.mode &= ~MODE_APPKEYPAD;
  1300. term.esc = 0;
  1301. break;
  1302. case '7': /* DECSC -- Save Cursor */
  1303. tcursor(CURSOR_SAVE);
  1304. term.esc = 0;
  1305. break;
  1306. case '8': /* DECRC -- Restore Cursor */
  1307. tcursor(CURSOR_LOAD);
  1308. term.esc = 0;
  1309. break;
  1310. default:
  1311. fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n",
  1312. (unsigned char) ascii, isprint(ascii)?ascii:'.');
  1313. term.esc = 0;
  1314. }
  1315. }
  1316. } else {
  1317. if(sel.bx != -1 && BETWEEN(term.c.y, sel.by, sel.ey))
  1318. sel.bx = -1;
  1319. switch(ascii) {
  1320. case '\t':
  1321. tputtab();
  1322. break;
  1323. case '\b':
  1324. tmoveto(term.c.x-1, term.c.y);
  1325. break;
  1326. case '\r':
  1327. tmoveto(0, term.c.y);
  1328. break;
  1329. case '\f':
  1330. case '\v':
  1331. case '\n':
  1332. /* go to first col if the mode is set */
  1333. tnewline(IS_SET(MODE_CRLF));
  1334. break;
  1335. case '\a':
  1336. if(!(xw.state & WIN_FOCUSED))
  1337. xseturgency(1);
  1338. break;
  1339. case '\033':
  1340. csireset();
  1341. term.esc = ESC_START;
  1342. break;
  1343. default:
  1344. if(IS_SET(MODE_WRAP) && term.c.state & CURSOR_WRAPNEXT)
  1345. tnewline(1); /* always go to first col */
  1346. tsetchar(c);
  1347. if(term.c.x+1 < term.col)
  1348. tmoveto(term.c.x+1, term.c.y);
  1349. else
  1350. term.c.state |= CURSOR_WRAPNEXT;
  1351. }
  1352. }
  1353. }
  1354. int
  1355. tresize(int col, int row) {
  1356. int i, x;
  1357. int minrow = MIN(row, term.row);
  1358. int mincol = MIN(col, term.col);
  1359. int slide = term.c.y - row + 1;
  1360. if(col < 1 || row < 1)
  1361. return 0;
  1362. /* free unneeded rows */
  1363. i = 0;
  1364. if(slide > 0) {
  1365. /* slide screen to keep cursor where we expect it -
  1366. * tscrollup would work here, but we can optimize to
  1367. * memmove because we're freeing the earlier lines */
  1368. for(/* i = 0 */; i < slide; i++) {
  1369. free(term.line[i]);
  1370. free(term.alt[i]);
  1371. }
  1372. memmove(term.line, term.line + slide, row * sizeof(Line));
  1373. memmove(term.alt, term.alt + slide, row * sizeof(Line));
  1374. }
  1375. for(i += row; i < term.row; i++) {
  1376. free(term.line[i]);
  1377. free(term.alt[i]);
  1378. }
  1379. /* resize to new height */
  1380. term.line = realloc(term.line, row * sizeof(Line));
  1381. term.alt = realloc(term.alt, row * sizeof(Line));
  1382. term.dirty = realloc(term.dirty, row * sizeof(*term.dirty));
  1383. /* resize each row to new width, zero-pad if needed */
  1384. for(i = 0; i < minrow; i++) {
  1385. term.dirty[i] = 1;
  1386. term.line[i] = realloc(term.line[i], col * sizeof(Glyph));
  1387. term.alt[i] = realloc(term.alt[i], col * sizeof(Glyph));
  1388. for(x = mincol; x < col; x++) {
  1389. term.line[i][x].state = 0;
  1390. term.alt[i][x].state = 0;
  1391. }
  1392. }
  1393. /* allocate any new rows */
  1394. for(/* i == minrow */; i < row; i++) {
  1395. term.dirty[i] = 1;
  1396. term.line[i] = calloc(col, sizeof(Glyph));
  1397. term.alt [i] = calloc(col, sizeof(Glyph));
  1398. }
  1399. /* update terminal size */
  1400. term.col = col, term.row = row;
  1401. /* make use of the LIMIT in tmoveto */
  1402. tmoveto(term.c.x, term.c.y);
  1403. /* reset scrolling region */
  1404. tsetscroll(0, row-1);
  1405. return (slide > 0);
  1406. }
  1407. void
  1408. xresize(int col, int row) {
  1409. Pixmap newbuf;
  1410. int oldw, oldh;
  1411. oldw = xw.bufw;
  1412. oldh = xw.bufh;
  1413. xw.bufw = MAX(1, col * xw.cw);
  1414. xw.bufh = MAX(1, row * xw.ch);
  1415. newbuf = XCreatePixmap(xw.dpy, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dpy, xw.scr));
  1416. XCopyArea(xw.dpy, xw.buf, newbuf, dc.gc, 0, 0, xw.bufw, xw.bufh, 0, 0);
  1417. XFreePixmap(xw.dpy, xw.buf);
  1418. XSetForeground(xw.dpy, dc.gc, dc.col[DefaultBG]);
  1419. if(xw.bufw > oldw)
  1420. XFillRectangle(xw.dpy, newbuf, dc.gc, oldw, 0,
  1421. xw.bufw-oldw, MIN(xw.bufh, oldh));
  1422. else if(xw.bufw < oldw && (BORDER > 0 || xw.w > xw.bufw))
  1423. XClearArea(xw.dpy, xw.win, BORDER+xw.bufw, BORDER,
  1424. xw.w-xw.bufh-BORDER, BORDER+MIN(xw.bufh, oldh),
  1425. False);
  1426. if(xw.bufh > oldh)
  1427. XFillRectangle(xw.dpy, newbuf, dc.gc, 0, oldh,
  1428. xw.bufw, xw.bufh-oldh);
  1429. else if(xw.bufh < oldh && (BORDER > 0 || xw.h > xw.bufh))
  1430. XClearArea(xw.dpy, xw.win, BORDER, BORDER+xw.bufh,
  1431. xw.w-2*BORDER, xw.h-xw.bufh-BORDER,
  1432. False);
  1433. xw.buf = newbuf;
  1434. }
  1435. void
  1436. xloadcols(void) {
  1437. int i, r, g, b;
  1438. XColor color;
  1439. unsigned long white = WhitePixel(xw.dpy, xw.scr);
  1440. for(i = 0; i < LEN(colorname); i++) {
  1441. if(!XAllocNamedColor(xw.dpy, xw.cmap, colorname[i], &color, &color)) {
  1442. dc.col[i] = white;
  1443. fprintf(stderr, "Could not allocate color '%s'\n", colorname[i]);
  1444. } else
  1445. dc.col[i] = color.pixel;
  1446. }
  1447. /* same colors as xterm */
  1448. for(r = 0; r < 6; r++)
  1449. for(g = 0; g < 6; g++)
  1450. for(b = 0; b < 6; b++) {
  1451. color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r;
  1452. color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g;
  1453. color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b;
  1454. if(!XAllocColor(xw.dpy, xw.cmap, &color)) {
  1455. dc.col[i] = white;
  1456. fprintf(stderr, "Could not allocate color %d\n", i);
  1457. } else
  1458. dc.col[i] = color.pixel;
  1459. i++;
  1460. }
  1461. for(r = 0; r < 24; r++, i++) {
  1462. color.red = color.green = color.blue = 0x0808 + 0x0a0a * r;
  1463. if (!XAllocColor(xw.dpy, xw.cmap, &color)) {
  1464. dc.col[i] = white;
  1465. fprintf(stderr, "Could not allocate color %d\n", i);
  1466. } else
  1467. dc.col[i] = color.pixel;
  1468. }
  1469. }
  1470. void
  1471. xclear(int x1, int y1, int x2, int y2) {
  1472. XSetForeground(xw.dpy, dc.gc, dc.col[IS_SET(MODE_REVERSE) ? DefaultFG : DefaultBG]);
  1473. XFillRectangle(xw.dpy, xw.buf, dc.gc,
  1474. x1 * xw.cw, y1 * xw.ch,
  1475. (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch);
  1476. }
  1477. void
  1478. xhints(void) {
  1479. XClassHint class = {opt_class ? opt_class : TNAME, TNAME};
  1480. XWMHints wm = {.flags = InputHint, .input = 1};
  1481. XSizeHints size = {
  1482. .flags = PSize | PResizeInc | PBaseSize,
  1483. .height = xw.h,
  1484. .width = xw.w,
  1485. .height_inc = xw.ch,
  1486. .width_inc = xw.cw,
  1487. .base_height = 2*BORDER,
  1488. .base_width = 2*BORDER,
  1489. };
  1490. XSetWMProperties(xw.dpy, xw.win, NULL, NULL, NULL, 0, &size, &wm, &class);
  1491. }
  1492. XFontSet
  1493. xinitfont(char *fontstr) {
  1494. XFontSet set;
  1495. char *def, **missing;
  1496. int n;
  1497. missing = NULL;
  1498. set = XCreateFontSet(xw.dpy, fontstr, &missing, &n, &def);
  1499. if(missing) {
  1500. while(n--)
  1501. fprintf(stderr, "st: missing fontset: %s\n", missing[n]);
  1502. XFreeStringList(missing);
  1503. }
  1504. return set;
  1505. }
  1506. void
  1507. xgetfontinfo(XFontSet set, int *ascent, int *descent, short *lbearing, short *rbearing) {
  1508. XFontStruct **xfonts;
  1509. char **font_names;
  1510. int i, n;
  1511. *ascent = *descent = *lbearing = *rbearing = 0;
  1512. n = XFontsOfFontSet(set, &xfonts, &font_names);
  1513. for(i = 0; i < n; i++) {
  1514. *ascent = MAX(*ascent, (*xfonts)->ascent);
  1515. *descent = MAX(*descent, (*xfonts)->descent);
  1516. *lbearing = MAX(*lbearing, (*xfonts)->min_bounds.lbearing);
  1517. *rbearing = MAX(*rbearing, (*xfonts)->max_bounds.rbearing);
  1518. xfonts++;
  1519. }
  1520. }
  1521. void
  1522. initfonts(char *fontstr, char *bfontstr) {
  1523. if((dc.font.set = xinitfont(fontstr)) == NULL ||
  1524. (dc.bfont.set = xinitfont(bfontstr)) == NULL)
  1525. die("Can't load font %s\n", dc.font.set ? BOLDFONT : FONT);
  1526. xgetfontinfo(dc.font.set, &dc.font.ascent, &dc.font.descent,
  1527. &dc.font.lbearing, &dc.font.rbearing);
  1528. xgetfontinfo(dc.bfont.set, &dc.bfont.ascent, &dc.bfont.descent,
  1529. &dc.bfont.lbearing, &dc.bfont.rbearing);
  1530. }
  1531. void
  1532. xinit(void) {
  1533. XSetWindowAttributes attrs;
  1534. Cursor cursor;
  1535. Window parent;
  1536. if(!(xw.dpy = XOpenDisplay(NULL)))
  1537. die("Can't open display\n");
  1538. xw.scr = XDefaultScreen(xw.dpy);
  1539. /* font */
  1540. initfonts(FONT, BOLDFONT);
  1541. /* XXX: Assuming same size for bold font */
  1542. xw.cw = dc.font.rbearing - dc.font.lbearing;
  1543. xw.ch = dc.font.ascent + dc.font.descent;
  1544. /* colors */
  1545. xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
  1546. xloadcols();
  1547. /* window - default size */
  1548. xw.bufh = term.row * xw.ch;
  1549. xw.bufw = term.col * xw.cw;
  1550. xw.h = xw.bufh + 2*BORDER;
  1551. xw.w = xw.bufw + 2*BORDER;
  1552. attrs.background_pixel = dc.col[DefaultBG];
  1553. attrs.border_pixel = dc.col[DefaultBG];
  1554. attrs.bit_gravity = NorthWestGravity;
  1555. attrs.event_mask = FocusChangeMask | KeyPressMask
  1556. | ExposureMask | VisibilityChangeMask | StructureNotifyMask
  1557. | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask
  1558. | EnterWindowMask | LeaveWindowMask;
  1559. attrs.colormap = xw.cmap;
  1560. parent = opt_embed ? strtol(opt_embed, NULL, 0) : XRootWindow(xw.dpy, xw.scr);
  1561. xw.win = XCreateWindow(xw.dpy, parent, 0, 0,
  1562. xw.w, xw.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
  1563. XDefaultVisual(xw.dpy, xw.scr),
  1564. CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
  1565. | CWColormap,
  1566. &attrs);
  1567. xw.buf = XCreatePixmap(xw.dpy, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dpy, xw.scr));
  1568. /* input methods */
  1569. xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL);
  1570. xw.xic = XCreateIC(xw.xim, XNInputStyle, XIMPreeditNothing
  1571. | XIMStatusNothing, XNClientWindow, xw.win,
  1572. XNFocusWindow, xw.win, NULL);
  1573. /* gc */
  1574. dc.gc = XCreateGC(xw.dpy, xw.win, 0, NULL);
  1575. /* white cursor, black outline */
  1576. cursor = XCreateFontCursor(xw.dpy, XC_xterm);
  1577. XDefineCursor(xw.dpy, xw.win, cursor);
  1578. XRecolorCursor(xw.dpy, cursor,
  1579. &(XColor){.red = 0xffff, .green = 0xffff, .blue = 0xffff},
  1580. &(XColor){.red = 0x0000, .green = 0x0000, .blue = 0x0000});
  1581. xw.xembed = XInternAtom(xw.dpy, "_XEMBED", False);
  1582. XStoreName(xw.dpy, xw.win, opt_title ? opt_title : "st");
  1583. XMapWindow(xw.dpy, xw.win);
  1584. xhints();
  1585. XSync(xw.dpy, 0);
  1586. }
  1587. void
  1588. xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
  1589. unsigned long xfg = dc.col[base.fg], xbg = dc.col[base.bg], temp;
  1590. int winx = x*xw.cw, winy = y*xw.ch + dc.font.ascent, width = charlen*xw.cw;
  1591. int i;
  1592. /* only switch default fg/bg if term is in RV mode */
  1593. if(IS_SET(MODE_REVERSE)) {
  1594. if(base.fg == DefaultFG)
  1595. xfg = dc.col[DefaultBG];
  1596. if(base.bg == DefaultBG)
  1597. xbg = dc.col[DefaultFG];
  1598. }
  1599. if(base.mode & ATTR_REVERSE)
  1600. temp = xfg, xfg = xbg, xbg = temp;
  1601. XSetBackground(xw.dpy, dc.gc, xbg);
  1602. XSetForeground(xw.dpy, dc.gc, xfg);
  1603. if(base.mode & ATTR_GFX) {
  1604. for(i = 0; i < bytelen; i++) {
  1605. char c = gfx[(unsigned int)s[i] % 256];
  1606. if(c)
  1607. s[i] = c;
  1608. else if(s[i] > 0x5f)
  1609. s[i] -= 0x5f;
  1610. }
  1611. }
  1612. XmbDrawImageString(xw.dpy, xw.buf, base.mode & ATTR_BOLD ? dc.bfont.set : dc.font.set,
  1613. dc.gc, winx, winy, s, bytelen);
  1614. if(base.mode & ATTR_UNDERLINE)
  1615. XDrawLine(xw.dpy, xw.buf, dc.gc, winx, winy+1, winx+width-1, winy+1);
  1616. }
  1617. void
  1618. xdrawcursor(void) {
  1619. static int oldx = 0;
  1620. static int oldy = 0;
  1621. int sl;
  1622. Glyph g = {{' '}, ATTR_NULL, DefaultBG, DefaultCS, 0};
  1623. LIMIT(oldx, 0, term.col-1);
  1624. LIMIT(oldy, 0, term.row-1);
  1625. if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
  1626. memcpy(g.c, term.line[term.c.y][term.c.x].c, UTF_SIZ);
  1627. /* remove the old cursor */
  1628. if(term.line[oldy][oldx].state & GLYPH_SET) {
  1629. sl = utf8size(term.line[oldy][oldx].c);
  1630. xdraws(term.line[oldy][oldx].c, term.line[oldy][oldx], oldx, oldy, 1, sl);
  1631. } else
  1632. xclear(oldx, oldy, oldx, oldy);
  1633. /* draw the new one */
  1634. if(!(term.c.state & CURSOR_HIDE) && (xw.state & WIN_FOCUSED)) {
  1635. sl = utf8size(g.c);
  1636. if(IS_SET(MODE_REVERSE))
  1637. g.mode |= ATTR_REVERSE, g.fg = DefaultCS, g.bg = DefaultFG;
  1638. xdraws(g.c, g, term.c.x, term.c.y, 1, sl);
  1639. oldx = term.c.x, oldy = term.c.y;
  1640. }
  1641. }
  1642. void
  1643. draw() {
  1644. drawregion(0, 0, term.col, term.row);
  1645. }
  1646. void
  1647. drawregion(int x1, int y1, int x2, int y2) {
  1648. int ic, ib, x, y, ox, sl;
  1649. Glyph base, new;
  1650. char buf[DRAW_BUF_SIZ];
  1651. if(!(xw.state & WIN_VISIBLE))
  1652. return;
  1653. for(y = y1; y < y2; y++) {
  1654. if(!term.dirty[y])
  1655. continue;
  1656. xclear(0, y, term.col, y);
  1657. term.dirty[y] = 0;
  1658. base = term.line[y][0];
  1659. ic = ib = ox = 0;
  1660. for(x = x1; x < x2; x++) {
  1661. new = term.line[y][x];
  1662. if(sel.bx != -1 && *(new.c) && selected(x, y))
  1663. new.mode ^= ATTR_REVERSE;
  1664. if(ib > 0 && (!(new.state & GLYPH_SET) || ATTRCMP(base, new) ||
  1665. ib >= DRAW_BUF_SIZ-UTF_SIZ)) {
  1666. xdraws(buf, base, ox, y, ic, ib);
  1667. ic = ib = 0;
  1668. }
  1669. if(new.state & GLYPH_SET) {
  1670. if(ib == 0) {
  1671. ox = x;
  1672. base = new;
  1673. }
  1674. sl = utf8size(new.c);
  1675. memcpy(buf+ib, new.c, sl);
  1676. ib += sl;
  1677. ++ic;
  1678. }
  1679. }
  1680. if(ib > 0)
  1681. xdraws(buf, base, ox, y, ic, ib);
  1682. }
  1683. xdrawcursor();
  1684. XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, 0, 0, xw.bufw, xw.bufh, BORDER, BORDER);
  1685. }
  1686. void
  1687. expose(XEvent *ev) {
  1688. XExposeEvent *e = &ev->xexpose;
  1689. if(xw.state & WIN_REDRAW) {
  1690. if(!e->count) {
  1691. xw.state &= ~WIN_REDRAW;
  1692. draw();
  1693. }
  1694. } else
  1695. XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, e->x-BORDER, e->y-BORDER,
  1696. e->width, e->height, e->x, e->y);
  1697. }
  1698. void
  1699. visibility(XEvent *ev) {
  1700. XVisibilityEvent *e = &ev->xvisibility;
  1701. if(e->state == VisibilityFullyObscured)
  1702. xw.state &= ~WIN_VISIBLE;
  1703. else if(!(xw.state & WIN_VISIBLE))
  1704. /* need a full redraw for next Expose, not just a buf copy */
  1705. xw.state |= WIN_VISIBLE | WIN_REDRAW;
  1706. }
  1707. void
  1708. unmap(XEvent *ev) {
  1709. xw.state &= ~WIN_VISIBLE;
  1710. }
  1711. void
  1712. xseturgency(int add) {
  1713. XWMHints *h = XGetWMHints(xw.dpy, xw.win);
  1714. h->flags = add ? (h->flags | XUrgencyHint) : (h->flags & ~XUrgencyHint);
  1715. XSetWMHints(xw.dpy, xw.win, h);
  1716. XFree(h);
  1717. }
  1718. void
  1719. focus(XEvent *ev) {
  1720. if(ev->type == FocusIn) {
  1721. xw.state |= WIN_FOCUSED;
  1722. xseturgency(0);
  1723. } else
  1724. xw.state &= ~WIN_FOCUSED;
  1725. draw();
  1726. }
  1727. char*
  1728. kmap(KeySym k, unsigned int state) {
  1729. int i;
  1730. state &= ~Mod2Mask;
  1731. for(i = 0; i < LEN(key); i++) {
  1732. unsigned int mask = key[i].mask;
  1733. if(key[i].k == k && ((state & mask) == mask || (mask == XK_NO_MOD && !state)))
  1734. return (char*)key[i].s;
  1735. }
  1736. return NULL;
  1737. }
  1738. void
  1739. kpress(XEvent *ev) {
  1740. XKeyEvent *e = &ev->xkey;
  1741. KeySym ksym;
  1742. char buf[32];
  1743. char *customkey;
  1744. int len;
  1745. int meta;
  1746. int shift;
  1747. Status status;
  1748. meta = e->state & Mod1Mask;
  1749. shift = e->state & ShiftMask;
  1750. len = XmbLookupString(xw.xic, e, buf, sizeof(buf), &ksym, &status);
  1751. /* 1. custom keys from config.h */
  1752. if((customkey = kmap(ksym, e->state)))
  1753. ttywrite(customkey, strlen(customkey));
  1754. /* 2. hardcoded (overrides X lookup) */
  1755. else
  1756. switch(ksym) {
  1757. case XK_Up:
  1758. case XK_Down:
  1759. case XK_Left:
  1760. case XK_Right:
  1761. /* XXX: shift up/down doesn't work */
  1762. sprintf(buf, "\033%c%c", IS_SET(MODE_APPKEYPAD) ? 'O' : '[', (shift ? "dacb":"DACB")[ksym - XK_Left]);
  1763. ttywrite(buf, 3);
  1764. break;
  1765. case XK_Insert:
  1766. if(shift)
  1767. selpaste();
  1768. break;
  1769. case XK_Return:
  1770. if(IS_SET(MODE_CRLF))
  1771. ttywrite("\r\n", 2);
  1772. else
  1773. ttywrite("\r", 1);
  1774. break;
  1775. /* 3. X lookup */
  1776. default:
  1777. if(len > 0) {
  1778. if(meta && len == 1)
  1779. ttywrite("\033", 1);
  1780. ttywrite(buf, len);
  1781. }
  1782. break;
  1783. }
  1784. }
  1785. void
  1786. cmessage(XEvent *e) {
  1787. /* See xembed specs
  1788. http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html */
  1789. if (e->xclient.message_type == xw.xembed && e->xclient.format == 32) {
  1790. if (e->xclient.data.l[1] == XEMBED_FOCUS_IN) {
  1791. xw.state |= WIN_FOCUSED;
  1792. xseturgency(0);
  1793. } else if (e->xclient.data.l[1] == XEMBED_FOCUS_OUT) {
  1794. xw.state &= ~WIN_FOCUSED;
  1795. }
  1796. draw();
  1797. }
  1798. }
  1799. void
  1800. resize(XEvent *e) {
  1801. int col, row;
  1802. if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
  1803. return;
  1804. xw.w = e->xconfigure.width;
  1805. xw.h = e->xconfigure.height;
  1806. col = (xw.w - 2*BORDER) / xw.cw;
  1807. row = (xw.h - 2*BORDER) / xw.ch;
  1808. if(col == term.col && row == term.row)
  1809. return;
  1810. if(tresize(col, row))
  1811. draw();
  1812. ttyresize(col, row);
  1813. xresize(col, row);
  1814. }
  1815. void
  1816. run(void) {
  1817. XEvent ev;
  1818. fd_set rfd;
  1819. int xfd = XConnectionNumber(xw.dpy);
  1820. for(;;) {
  1821. FD_ZERO(&rfd);
  1822. FD_SET(cmdfd, &rfd);
  1823. FD_SET(xfd, &rfd);
  1824. if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL) < 0) {
  1825. if(errno == EINTR)
  1826. continue;
  1827. die("select failed: %s\n", SERRNO);
  1828. }
  1829. if(FD_ISSET(cmdfd, &rfd)) {
  1830. ttyread();
  1831. draw();
  1832. }
  1833. while(XPending(xw.dpy)) {
  1834. XNextEvent(xw.dpy, &ev);
  1835. if(XFilterEvent(&ev, xw.win))
  1836. continue;
  1837. if(handler[ev.type])
  1838. (handler[ev.type])(&ev);
  1839. }
  1840. }
  1841. }
  1842. int
  1843. main(int argc, char *argv[]) {
  1844. int i;
  1845. for(i = 1; i < argc; i++) {
  1846. switch(argv[i][0] != '-' || argv[i][2] ? -1 : argv[i][1]) {
  1847. case 't':
  1848. if(++i < argc) opt_title = argv[i];
  1849. break;
  1850. case 'c':
  1851. if(++i < argc) opt_class = argv[i];
  1852. break;
  1853. case 'w':
  1854. if(++i < argc) opt_embed = argv[i];
  1855. break;
  1856. case 'e':
  1857. /* eat every remaining arguments */
  1858. if(++i < argc) opt_cmd = &argv[i];
  1859. goto run;
  1860. case 'v':
  1861. default:
  1862. die(USAGE);
  1863. }
  1864. }
  1865. run:
  1866. setlocale(LC_CTYPE, "");
  1867. tnew(80, 24);
  1868. ttynew();
  1869. xinit();
  1870. selinit();
  1871. run();
  1872. return 0;
  1873. }