st.c 57 KB

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