st.c 45 KB

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