您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 

1146 行
39 KiB

  1. /*
  2. * neercs — console-based window manager
  3. *
  4. * Copyright © 2006—2015 Sam Hocevar <sam@hocevar.net>
  5. * © 2008—2010 Jean-Yves Lamoureux <jylam@lnxscene.org>
  6. * © 2008—2010 Pascal Terjan <pterjan@linuxfr.org>
  7. *
  8. * This program is free software. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What the Fuck You Want
  11. * to Public License, Version 2, as published by the WTFPL Task Force.
  12. * See http://www.wtfpl.net/ for more details.
  13. */
  14. #if HAVE_CONFIG_H
  15. # include "config.h"
  16. #endif
  17. #include <string.h>
  18. #include <lol/engine.h>
  19. using namespace lol;
  20. #include "../neercs.h"
  21. #include "term.h"
  22. /* DEC ACS with common extensions */
  23. static uint32_t dec_acs(uint32_t uc)
  24. {
  25. switch (uc)
  26. {
  27. case '+':
  28. return 0x2192; /* RIGHTWARDS ARROW */
  29. case ',':
  30. return 0x2190; /* LEFTWARDS ARROW */
  31. case '-':
  32. return 0x2191; /* UPWARDS ARROW */
  33. case '.':
  34. return 0x2193; /* DOWNWARDS ARROW */
  35. case '0':
  36. return 0x25AE; /* BLACK VERTICAL RECTANGLE */
  37. case '_':
  38. return 0x25AE; /* BLACK VERTICAL RECTANGLE */
  39. case '`':
  40. return 0x25C6; /* BLACK DIAMOND */
  41. case 'a':
  42. return 0x2592; /* MEDIUM SHADE */
  43. case 'b':
  44. return 0x2409; /* SYMBOL FOR HORIZONTAL TABULATION */
  45. case 'c':
  46. return 0x240C; /* SYMBOL FOR FORM FEED */
  47. case 'd':
  48. return 0x240D; /* SYMBOL FOR CARRIAGE RETURN */
  49. case 'e':
  50. return 0x240A; /* SYMBOL FOR LINE FEED */
  51. case 'f':
  52. return 0x00B0; /* DEGREE SIGN */
  53. case 'g':
  54. return 0x00B1; /* PLUS-MINUS SIGN */
  55. case 'h':
  56. return 0x2424; /* SYMBOL FOR NEWLINE */
  57. case 'i':
  58. return 0x240B; /* SYMBOL FOR VERTICAL TABULATION */
  59. case 'j':
  60. return 0x2518; /* BOX DRAWINGS LIGHT UP AND LEFT */
  61. case 'k':
  62. return 0x2510; /* BOX DRAWINGS LIGHT DOWN AND LEFT */
  63. case 'l':
  64. return 0x250C; /* BOX DRAWINGS LIGHT DOWN AND RIGHT */
  65. case 'm':
  66. return 0x2514; /* BOX DRAWINGS LIGHT UP AND RIGHT */
  67. case 'n':
  68. return 0x253C; /* BOX DRAWINGS LIGHT VERTICAL AND HORIZONTAL */
  69. case 'o':
  70. return 0x23BA; /* HORIZONTAL SCAN LINE-1 */
  71. case 'p':
  72. return 0x23BB; /* HORIZONTAL SCAN LINE-3 */
  73. case 'q':
  74. return 0x2500; /* BOX DRAWINGS LIGHT HORIZONTAL */
  75. case 'r':
  76. return 0x23BC; /* HORIZONTAL SCAN LINE-7 */
  77. case 's':
  78. return 0x23BD; /* HORIZONTAL SCAN LINE-9 */
  79. case 't':
  80. return 0x251C; /* BOX DRAWINGS LIGHT VERTICAL AND RIGHT */
  81. case 'u':
  82. return 0x2524; /* BOX DRAWINGS LIGHT VERTICAL AND LEFT */
  83. case 'v':
  84. return 0x2534; /* BOX DRAWINGS LIGHT UP AND HORIZONTAL */
  85. case 'w':
  86. return 0x252C; /* BOX DRAWINGS LIGHT DOWN AND HORIZONTAL */
  87. case 'x':
  88. return 0x2502; /* BOX DRAWINGS LIGHT VERTICAL */
  89. case 'y':
  90. return 0x2264; /* LESS-THAN OR EQUAL TO */
  91. case 'z':
  92. return 0x2265; /* GREATER-THAN OR EQUAL TO */
  93. case '{':
  94. return 0x03C0; /* GREEK SMALL LETTER PI */
  95. case '|':
  96. return 0x2260; /* NOT EQUAL TO */
  97. case '}':
  98. return 0x00A3; /* POUND SIGN */
  99. case '~':
  100. return 0x00B7; /* MIDDLE DOT */
  101. default:
  102. return uc;
  103. }
  104. };
  105. #define LITERAL2CHAR(i0,i1) (((i0) << 8) | (i1))
  106. #define LITERAL3CHAR(i0,i1,i2) LITERAL2CHAR(LITERAL2CHAR(i0, i1), i2)
  107. inline int Term::ReadChar(unsigned char c, int *x, int *y)
  108. {
  109. if (c == '\r')
  110. {
  111. *x = 0;
  112. }
  113. else if (c == '\n')
  114. {
  115. *x = 0;
  116. *y = *y + 1;
  117. }
  118. else if (c == '\a')
  119. {
  120. /* FIXME TODO: in_bell ? */
  121. // if (!m_bell)
  122. // screen_list->in_bell = 10;
  123. m_bell = 1;
  124. }
  125. else if (c == '\t')
  126. {
  127. *x = (*x + 7) & ~7;
  128. }
  129. else if (c == '\x08')
  130. {
  131. if (*x > 0)
  132. *x = *x - 1;
  133. }
  134. else if (c == '\x0b')
  135. {
  136. /* Vertical tab */
  137. /* Not sure about the real meaning of it, just y++ for now */
  138. if (*y < caca_get_canvas_height(m_caca))
  139. *y = *y + 1;
  140. }
  141. else if (c == '\x0e')
  142. {
  143. /* Shift Out (Ctrl-N) -> Switch to Alternate Character Set: invokes
  144. the G1 character set. */
  145. m_conv_state.glr[0] = 1;
  146. }
  147. else if (c == '\x0f')
  148. {
  149. /* Shift In (Ctrl-O) -> Switch to Standard Character Set: invokes the
  150. G0 character set. */
  151. m_conv_state.glr[0] = 0;
  152. }
  153. else
  154. {
  155. return 1;
  156. }
  157. return 0;
  158. }
  159. inline int Term::ReadDuplet(unsigned char const *buffer, unsigned int *skip,
  160. int top, int bottom, int width, int height)
  161. {
  162. int i = 0, j, k;
  163. unsigned int dummy = 0;
  164. /* Single Shift Select of G2 Character Set (SS2: 0x8e): affects next
  165. character only */
  166. if (buffer[i] == '\033' && buffer[i + 1] == 'N')
  167. {
  168. m_conv_state.ss = 2;
  169. *skip += 1;
  170. }
  171. /* Reverse Index (RI) go up one line, reverse scroll if necessary */
  172. else if (buffer[i] == '\033' && buffer[i + 1] == 'M')
  173. {
  174. /* FIXME : not sure about the meaning of 'go up one line' and 'if
  175. necessary' words. Implemented as a scroller only. */
  176. for (j = bottom - 1; j > top; j--)
  177. {
  178. for (k = 0; k < width; k++)
  179. {
  180. caca_put_char(m_caca, k, j, caca_get_char(m_caca, k, j - 1));
  181. caca_put_attr(m_caca, k, j, caca_get_attr(m_caca, k, j - 1));
  182. }
  183. }
  184. caca_draw_line(m_caca, 0, top - 1, width - 1, top - 1, ' ');
  185. *skip += 1;
  186. }
  187. /* Single Shift Select of G3 Character Set (SS2: 0x8f): affects next
  188. character only */
  189. else if (buffer[i] == '\033' && buffer[i + 1] == 'O')
  190. {
  191. m_conv_state.ss = 3;
  192. *skip += 1;
  193. }
  194. /* LOCKING-SHIFT TWO (LS2), ISO 2022, ECMA-48 (1986), ISO 6429 : 1988 */
  195. else if (buffer[i] == '\033' && buffer[i + 1] == 'n')
  196. {
  197. m_conv_state.glr[0] = 2;
  198. *skip += 1;
  199. }
  200. /* LOCKING-SHIFT THREE (LS3) ISO 2022, ECMA-48 (1986), ISO 6429 : 1988 */
  201. else if (buffer[i] == '\033' && buffer[i + 1] == 'o')
  202. {
  203. m_conv_state.glr[0] = 3;
  204. *skip += 1;
  205. }
  206. /* RESET TO INITIAL STATE (RIS), ECMA-48 (1986), ISO 6429 : 1988 */
  207. else if (buffer[i] == '\033' && buffer[i + 1] == 'c')
  208. {
  209. m_dfg = CACA_DEFAULT;
  210. m_dbg = CACA_DEFAULT;
  211. caca_set_color_ansi(m_caca, m_dfg, m_dbg);
  212. m_clearattr = caca_get_attr(m_caca, -1, -1);
  213. ReadGrcm(1, &dummy);
  214. m_conv_state.Reset();
  215. *skip += 1;
  216. }
  217. /* Coding Method Delimiter (CMD), ECMA-48 (1991), ISO/IEC 6429:1992 (ISO
  218. IR 189) */
  219. else if (buffer[i] == '\033' && buffer[i + 1] == 'd')
  220. {
  221. m_conv_state.Reset();
  222. *skip += 1;
  223. }
  224. else
  225. {
  226. return 1;
  227. }
  228. return 0;
  229. }
  230. size_t Term::ReadAnsi(void const *data, size_t size)
  231. {
  232. unsigned char const *buffer = (unsigned char const *)data;
  233. unsigned int i, j, k, skip, dummy = 0;
  234. unsigned int width, height, top, bottom;
  235. uint32_t savedattr;
  236. int x = 0, y = 0, save_x = 0, save_y = 0;
  237. char b[100];
  238. msg::debug("ansi : import_term\n");
  239. width = caca_get_canvas_width(m_caca);
  240. height = caca_get_canvas_height(m_caca);
  241. x = caca_get_cursor_x(m_caca);
  242. y = caca_get_cursor_y(m_caca);
  243. top = 1;
  244. bottom = height;
  245. if (!m_init)
  246. {
  247. m_fg = m_dfg = CACA_LIGHTGRAY;
  248. m_bg = m_dbg = CACA_BLACK;
  249. m_bold = m_blink = m_italics = m_negative = m_concealed
  250. = m_underline = m_faint = m_strike = m_proportional = 0;
  251. caca_set_color_ansi(m_caca, m_dfg, m_dbg);
  252. m_clearattr = caca_get_attr(m_caca, -1, -1);
  253. ReadGrcm(1, &dummy);
  254. m_conv_state.Reset();
  255. m_init = 1;
  256. }
  257. for (i = 0; i < size; i += skip)
  258. {
  259. uint32_t ch = 0;
  260. int wch = 0;
  261. skip = 1;
  262. /* Control codes (ASCII < \x20) */
  263. if (!ReadChar(buffer[i], &x, &y))
  264. {
  265. }
  266. /* If there are not enough characters to parse the escape sequence,
  267. wait until the next try. We require 3. */
  268. else if (buffer[i] == '\033' && i + 2 >= size)
  269. break;
  270. else if (!ReadDuplet(&buffer[i], &skip, top, bottom, width, height))
  271. {
  272. }
  273. /* GZDM4, G0-Designators, multi, 94^n chars [grandfathered short form
  274. from ISO 2022:1986] */
  275. else if (buffer[i] == '\033' && buffer[i + 1] == '$'
  276. && (buffer[i + 2] >= '@') && (buffer[i + 2] <= 'C'))
  277. {
  278. m_conv_state.gn[0] = LITERAL2CHAR('$', buffer[i + 2]);
  279. skip += 2;
  280. }
  281. /* GnDMx Gn-Designators, 9x^n chars; need one more char to distinguish
  282. these */
  283. else if (buffer[i] == '\033' && buffer[i + 1] == '$'
  284. && (i + 3 >= size))
  285. break;
  286. /* GZD4 G0-Designator, 94 chars */
  287. else if (buffer[i] == '\033' && buffer[i + 1] == '(')
  288. {
  289. m_conv_state.gn[0] = buffer[i + 2];
  290. skip += 2;
  291. }
  292. /* G1D4 G1-Designator, 94 chars */
  293. else if (buffer[i] == '\033' && buffer[i + 1] == ')')
  294. {
  295. m_conv_state.gn[1] = buffer[i + 2];
  296. skip += 2;
  297. }
  298. /* G2D4 G2-Designator, 94 chars */
  299. else if (buffer[i] == '\033' && buffer[i + 1] == '*')
  300. {
  301. m_conv_state.gn[2] = buffer[i + 2];
  302. skip += 2;
  303. }
  304. /* G3D4 G3-Designator, 94 chars */
  305. else if (buffer[i] == '\033' && buffer[i + 1] == '+')
  306. {
  307. m_conv_state.gn[3] = buffer[i + 2];
  308. skip += 2;
  309. }
  310. /* G2D6 G2-Designator, 96 chars */
  311. else if (buffer[i] == '\033' && buffer[i + 1] == '.')
  312. {
  313. m_conv_state.gn[2] = LITERAL2CHAR('.', buffer[i + 2]);
  314. skip += 2;
  315. }
  316. /* G3D6 G3-Designator, 96 chars */
  317. else if (buffer[i] == '\033' && buffer[i + 1] == '/')
  318. {
  319. m_conv_state.gn[3] = LITERAL2CHAR('.', buffer[i + 2]);
  320. skip += 2;
  321. }
  322. /* GZDM4 G0-Designator, 94^n chars */
  323. else if (buffer[i] == '\033' && buffer[i + 1] == '$'
  324. && buffer[i + 2] == '(')
  325. {
  326. m_conv_state.gn[0] = LITERAL2CHAR('$', buffer[i + 3]);
  327. skip += 3;
  328. }
  329. /* G1DM4 G1-Designator, 94^n chars */
  330. else if (buffer[i] == '\033' && buffer[i + 1] == '$'
  331. && buffer[i + 2] == ')')
  332. {
  333. m_conv_state.gn[1] = LITERAL2CHAR('$', buffer[i + 3]);
  334. skip += 3;
  335. }
  336. /* G2DM4 G2-Designator, 94^n chars */
  337. else if (buffer[i] == '\033' && buffer[i + 1] == '$'
  338. && buffer[i + 2] == '*')
  339. {
  340. m_conv_state.gn[2] = LITERAL2CHAR('$', buffer[i + 3]);
  341. skip += 3;
  342. }
  343. /* G3DM4 G3-Designator, 94^n chars */
  344. else if (buffer[i] == '\033' && buffer[i + 1] == '$'
  345. && buffer[i + 2] == '+')
  346. {
  347. m_conv_state.gn[3] = LITERAL2CHAR('$', buffer[i + 3]);
  348. skip += 3;
  349. }
  350. /* G2DM6 G2-Designator, 96^n chars */
  351. else if (buffer[i] == '\033' && buffer[i + 1] == '$'
  352. && buffer[i + 2] == '.')
  353. {
  354. m_conv_state.gn[2] = LITERAL3CHAR('$', '.', buffer[i + 3]);
  355. skip += 3;
  356. }
  357. /* G3DM6 G3-Designator, 96^n chars */
  358. else if (buffer[i] == '\033' && buffer[i + 1] == '$'
  359. && buffer[i + 2] == '/')
  360. {
  361. m_conv_state.gn[3] = LITERAL3CHAR('$', '.', buffer[i + 3]);
  362. skip += 3;
  363. }
  364. else if (buffer[i] == '\033' && buffer[i + 1] == '#')
  365. {
  366. msg::debug("ansi private '#' sequence\n");
  367. switch (buffer[i + 2])
  368. {
  369. case '8': /* DECALN Fills the entire screen area with
  370. uppercase Es for screen focus and
  371. alignment. */
  372. for (j = 0; j < height; j++)
  373. {
  374. for (k = 0; k < width; k++)
  375. {
  376. caca_put_char(m_caca, k, j, 'E');
  377. }
  378. }
  379. x = 0;
  380. y = 0;
  381. skip += 2;
  382. break;
  383. default:
  384. msg::debug("Unknow private sequence 'ESC#%c'\n", buffer[i + 2]);
  385. continue;
  386. }
  387. }
  388. /* Interpret escape commands, as per Standard ECMA-48 "Control
  389. Functions for Coded Character Sets", 5.4. Control sequences. */
  390. else if (buffer[i] == '\033' && buffer[i + 1] == '[')
  391. {
  392. unsigned int argc = 0, argv[101];
  393. unsigned int param, inter, junk, final;
  394. if (buffer[i + 2] == '?')
  395. {
  396. msg::debug("CSI? %c%c%c%c%c\n",
  397. buffer[i + 3], buffer[i + 4], buffer[i + 5],
  398. buffer[i + 6], buffer[i + 7]);
  399. }
  400. /* Compute offsets to parameter bytes, intermediate bytes and to
  401. the final byte. Only the final byte is mandatory, there can be
  402. zero of the others. 0 param=2 inter final final+1
  403. +-----+------------------+---------------------+-----------------+
  404. | CSI | parameter bytes | intermediate bytes | final byte | | |
  405. 0x30 - 0x3f | 0x20 - 0x2f | 0x40 - 0x7e | | ^[[ | 0123456789:;<=>?
  406. | SPC !"#$%&'()*+,-./ | azAZ@[\]^_`{|}~ |
  407. +-----+------------------+---------------------+-----------------+ */
  408. param = 2;
  409. /* vttest use to interleave control characters (\014 CR or \010
  410. BS) into CSI sequences, either directly after ESC[ or after
  411. param. Can't find anything related to this in any documentation
  412. nor XTerm sources, thought. */
  413. for (junk = param; i + junk < size; junk++)
  414. if (buffer[i + junk] < 0x20)
  415. {
  416. ReadChar(buffer[i + junk], &x, &y);
  417. }
  418. else
  419. {
  420. break;
  421. }
  422. /* Intermediate offset */
  423. for (inter = junk; i + inter < size; inter++)
  424. if (buffer[i + inter] < 0x30 || buffer[i + inter] > 0x3f)
  425. {
  426. break;
  427. }
  428. /* Interleaved character */
  429. for (junk = inter; i + junk < size; junk++)
  430. if (buffer[i + junk] < 0x20)
  431. {
  432. ReadChar(buffer[i + junk], &x, &y);
  433. }
  434. else
  435. {
  436. break;
  437. }
  438. /* Final Byte offset */
  439. for (final = junk; i + final < size; final++)
  440. if (buffer[i + final] < 0x20 || buffer[i + final] > 0x2f)
  441. {
  442. break;
  443. }
  444. if (i + final >= size
  445. || buffer[i + final] < 0x40 || buffer[i + final] > 0x7e)
  446. {
  447. msg::debug("ansi Invalid Final Byte (%d %c)\n",
  448. buffer[i + final], buffer[i + final]);
  449. break; /* Invalid Final Byte */
  450. }
  451. skip += final;
  452. /* Sanity checks */
  453. if (param < inter && buffer[i + param] >= 0x3c)
  454. {
  455. /* Private sequence, only parse what we know */
  456. msg::debug("ansi import: private sequence \"^[[%.*s\"\n",
  457. final - param + 1, buffer + i + param);
  458. /* FIXME better parsing */
  459. if (buffer[i + 2] == '?')
  460. {
  461. char arg[5], *end;
  462. int a = 0;
  463. int c, p, Pm;
  464. for (p = 0; p < 4; p++)
  465. {
  466. if (buffer[i + 3 + p] >= '0'
  467. && buffer[i + 3 + p] <= '9')
  468. {
  469. arg[a] = buffer[i + 3 + p];
  470. arg[a + 1] = 0;
  471. a++;
  472. msg::debug("private a now '%s'\n", arg);
  473. }
  474. else
  475. {
  476. break;
  477. }
  478. }
  479. Pm = strtol(arg, &end, 10);
  480. c = buffer[i + 3 + (end - arg)];
  481. msg::debug("ansi private mouse: command %c arg %d\n", c, Pm);
  482. if (c == 'h') /* DECSET DEC Private Mode Set */
  483. {
  484. switch (Pm)
  485. {
  486. /* FIXME Handle different modes */
  487. case 9:
  488. msg::debug("mouse : X10 mode\n");
  489. m_report_mouse = MOUSE_X10;
  490. break;
  491. case 1000: /* Send Mouse X & Y on button press
  492. and release. */
  493. msg::debug("mouse : VT200 mode\n");
  494. m_report_mouse = MOUSE_VT200;
  495. break;
  496. case 1001: /* Use Hilite Mouse Tracking. */
  497. msg::debug("mouse : VT200_HIGHLIGHT mode\n");
  498. m_report_mouse = MOUSE_VT200_HIGHLIGHT;
  499. break;
  500. case 1002: /* Use Cell Motion Mouse Tracking. */
  501. msg::debug("mouse : BTN mode\n");
  502. m_report_mouse = MOUSE_BTN_EVENT;
  503. break;
  504. case 1003: /* Use All Motion Mouse Tracking. */
  505. msg::debug("mouse : ANY mode\n");
  506. m_report_mouse = MOUSE_ANY_EVENT;
  507. break;
  508. default:
  509. break;
  510. }
  511. }
  512. else if (c == 'l') /* DECRST DEC Private Mode Reset */
  513. {
  514. Pm = atoi(arg);
  515. switch (Pm)
  516. {
  517. /* FIXME Handle different modes */
  518. case 9:
  519. case 1000: /* Send Mouse X & Y on button press
  520. and release. */
  521. case 1001: /* Use Hilite Mouse Tracking. */
  522. case 1002: /* Use Cell Motion Mouse Tracking. */
  523. case 1003: /* Use All Motion Mouse Tracking. */
  524. m_report_mouse = MOUSE_NONE;
  525. msg::debug("ansi private mouse : NOT reporting mouse\n");
  526. break;
  527. default:
  528. break;
  529. }
  530. }
  531. }
  532. continue; /* Private sequence, skip it entirely */
  533. }
  534. if (final - param > 100)
  535. continue; /* Suspiciously long sequence, skip it */
  536. /* Parse parameter bytes as per ECMA-48 5.4.2: Parameter string
  537. format */
  538. if (param < inter)
  539. {
  540. argv[0] = 0;
  541. for (j = param; j < inter; j++)
  542. {
  543. if (buffer[i + j] == ';')
  544. argv[++argc] = 0;
  545. else if (buffer[i + j] >= '0' && buffer[i + j] <= '9')
  546. argv[argc] = 10 * argv[argc] + (buffer[i + j] - '0');
  547. }
  548. argc++;
  549. }
  550. /* Interpret final byte. The code representations are given in
  551. ECMA-48 5.4: Control sequences, and the code definitions are
  552. given in ECMA-48 8.3: Definition of control functions. */
  553. msg::debug("ansi import: command '%c'\n", buffer[i + final]);
  554. switch (buffer[i + final])
  555. {
  556. case 'A': /* CUU (0x41) - Cursor Up */
  557. y -= argc ? argv[0] : 1;
  558. if (y < 0)
  559. y = 0;
  560. break;
  561. case 'B': /* CUD (0x42) - Cursor Down */
  562. y += argc ? argv[0] : 1;
  563. break;
  564. case 'C': /* CUF (0x43) - Cursor Right */
  565. x += argc ? argv[0] : 1;
  566. break;
  567. case 'D': /* CUB (0x44) - Cursor Left */
  568. x -= argc ? argv[0] : 1;
  569. if (x < 0)
  570. x = 0;
  571. break;
  572. case 'G': /* CHA (0x47) - Cursor Character Absolute */
  573. x = (argc && argv[0] > 0) ? argv[0] - 1 : 0;
  574. break;
  575. case 'H': /* CUP (0x48) - Cursor Position */
  576. x = (argc > 1 && argv[1] > 0) ? argv[1] - 1 : 0;
  577. y = (argc > 0 && argv[0] > 0) ? argv[0] - 1 : 0;
  578. msg::debug("ansi CUP : Cursor at %dx%d\n", x, y);
  579. break;
  580. case 'J': /* ED (0x4a) - Erase In Page */
  581. savedattr = caca_get_attr(m_caca, -1, -1);
  582. caca_set_attr(m_caca, m_clearattr);
  583. if (!argc || argv[0] == 0)
  584. {
  585. caca_draw_line(m_caca, x, y, width, y, ' ');
  586. caca_fill_box(m_caca, 0, y + 1, width, height - 1, ' ');
  587. }
  588. else if (argv[0] == 1)
  589. {
  590. caca_fill_box(m_caca, 0, 0, width, y, ' ');
  591. caca_draw_line(m_caca, 0, y, x, y, ' ');
  592. }
  593. else if (argv[0] == 2)
  594. {
  595. // x = y = 0;
  596. caca_fill_box(m_caca, 0, 0, width, height, ' ');
  597. }
  598. caca_set_attr(m_caca, savedattr);
  599. break;
  600. case 'K': /* EL (0x4b) - Erase In Line */
  601. msg::debug("ansi EL : cursor at %dx%d\n", x, y);
  602. if (!argc || argv[0] == 0)
  603. {
  604. caca_draw_line(m_caca, x, y, width, y, ' ');
  605. }
  606. else if (argv[0] == 1)
  607. {
  608. caca_draw_line(m_caca, 0, y, x, y, ' ');
  609. }
  610. else if (argv[0] == 2)
  611. {
  612. caca_draw_line(m_caca, 0, y, width, y, ' ');
  613. }
  614. break;
  615. case 'L': /* IL - Insert line */
  616. {
  617. unsigned int nb_lines = argc ? argv[0] : 1;
  618. for (j = bottom - 1; j >= (unsigned int)y + nb_lines; j--)
  619. {
  620. for (k = 0; k < width; k++)
  621. {
  622. caca_put_char(m_caca, k, j,
  623. caca_get_char(m_caca, k,
  624. j - nb_lines));
  625. caca_put_attr(m_caca, k, j,
  626. caca_get_attr(m_caca, k,
  627. j - nb_lines));
  628. }
  629. caca_draw_line(m_caca, 0, j - nb_lines, width,
  630. j - nb_lines, ' ');
  631. }
  632. }
  633. break;
  634. case 'P': /* DCH (0x50) - Delete Character */
  635. if (!argc || argv[0] == 0)
  636. argv[0] = 1; /* echo -ne 'foobar\r\e[0P\n' */
  637. for (j = x; (unsigned int)(j + argv[0]) < width; j++)
  638. {
  639. caca_put_char(m_caca, j, y,
  640. caca_get_char(m_caca, j + argv[0], y));
  641. caca_put_attr(m_caca, j, y,
  642. caca_get_attr(m_caca, j + argv[0], y));
  643. }
  644. break;
  645. #if 0
  646. savedattr = caca_get_attr(m_caca, -1, -1);
  647. caca_set_attr(m_caca, m_clearattr);
  648. for (; (unsigned int)j < width; j++)
  649. caca_put_char(m_caca, j, y, ' ');
  650. caca_set_attr(m_caca, savedattr);
  651. #endif
  652. case 'X': /* ECH (0x58) - Erase Character */
  653. if (argc && argv[0])
  654. {
  655. savedattr = caca_get_attr(m_caca, -1, -1);
  656. caca_set_attr(m_caca, m_clearattr);
  657. caca_draw_line(m_caca, x, y, x + argv[0] - 1, y, ' ');
  658. caca_set_attr(m_caca, savedattr);
  659. }
  660. case 'c': /* DA -- Device Attributes */
  661. /*
  662. 0 Base VT100, no options 1 Processor options (STP) 2
  663. Advanced video option (AVO) 3 AVO and STP 4 Graphics
  664. processor option (GPO) 5 GPO and STP 6 GPO and AVO 7 GPO,
  665. STP, and AVO */
  666. /* Warning, argument is Pn */
  667. msg::debug("ansi Got command c, argc %d, argv[0] (%d)\n",
  668. argc, argv[0], argv[0]);
  669. if (!argc || argv[0] == 0)
  670. {
  671. SendAnsi("\x1b[?1;0c");
  672. }
  673. else
  674. {
  675. switch (argv[0])
  676. {
  677. case 1:
  678. SendAnsi("\x1b[?\x1;\x1c");
  679. break;
  680. case 2:
  681. SendAnsi("\x1b[?\x1;\x2c");
  682. break;
  683. case 3:
  684. SendAnsi("\x1b[?\x1;\x3c");
  685. break;
  686. case 4:
  687. SendAnsi("\x1b[?\x1;\x4c");
  688. break;
  689. case 5:
  690. SendAnsi("\x1b[?\x1;\x5c");
  691. break;
  692. case 6:
  693. SendAnsi("\x1b[?\x1;\x6c");
  694. break;
  695. case 7:
  696. SendAnsi("\x1b[?\x1;\x7c");
  697. break;
  698. default:
  699. msg::debug("Unsupported DA option '%d'\n", argv[0]);
  700. break;
  701. }
  702. }
  703. break;
  704. case 'd': /* VPA (0x64) - Line Position Absolute */
  705. y = (argc && argv[0] > 0) ? argv[0] - 1 : 0;
  706. break;
  707. case 'f': /* HVP (0x66) - Character And Line Position */
  708. x = (argc > 1 && argv[1] > 0) ? argv[1] - 1 : 0;
  709. y = (argc > 0 && argv[0] > 0) ? argv[0] - 1 : 0;
  710. break;
  711. case 'g': /* TBC -- Tabulation Clear */
  712. break;
  713. case 'r': /* FIXME */
  714. if (argc == 2) /* DCSTBM - Set top and bottom margin */
  715. {
  716. msg::debug("DCSTBM %d %d\n", argv[0], argv[1]);
  717. top = argv[0];
  718. bottom = argv[1];
  719. }
  720. else
  721. msg::debug("ansi import: command r with %d params\n", argc);
  722. break;
  723. case 'h': /* SM (0x68) - FIXME */
  724. msg::debug("ansi import: set mode %i\n", argc ? (int)argv[0] : -1);
  725. break;
  726. case 'l': /* RM (0x6c) - FIXME */
  727. msg::debug("ansi import: reset mode %i\n", argc ? (int)argv[0] : -1);
  728. break;
  729. case 'm': /* SGR (0x6d) - Select Graphic Rendition */
  730. if (argc)
  731. ReadGrcm(argc, argv);
  732. else
  733. ReadGrcm(1, &dummy);
  734. break;
  735. case 'n':
  736. msg::debug("ansi command n, argc %d, argv[0] %d\n", argc, argv[0]);
  737. if (!argc)
  738. break;
  739. switch (argv[0])
  740. {
  741. case 5:
  742. /* Term ok */
  743. SendAnsi("\x1b[0n");
  744. break;
  745. case 6:
  746. /* Cursor Position */
  747. sprintf(b, "\x1b[%d;%dR", y + 1, x + 1);
  748. SendAnsi(b);
  749. break;
  750. }
  751. break;
  752. case 's': /* Private (save cursor position) */
  753. save_x = x;
  754. save_y = y;
  755. break;
  756. case 'u': /* Private (reload cursor position) */
  757. x = save_x;
  758. y = save_y;
  759. break;
  760. default:
  761. msg::debug("ansi import: unknown command \"^[%.*s\"\n",
  762. final - param + 1, buffer + i + param);
  763. break;
  764. }
  765. }
  766. /* Parse OSC stuff. */
  767. else if (buffer[i] == '\033' && buffer[i + 1] == ']')
  768. {
  769. char *string;
  770. unsigned int command = 0;
  771. unsigned int mode = 2, semicolon, final;
  772. for (semicolon = mode; i + semicolon < size; semicolon++)
  773. {
  774. if (buffer[i + semicolon] < '0' || buffer[i + semicolon] > '9')
  775. break;
  776. command = 10 * command + (buffer[i + semicolon] - '0');
  777. }
  778. if (i + semicolon >= size || buffer[i + semicolon] != ';')
  779. break; /* Invalid Mode */
  780. for (final = semicolon + 1; i + final < size; final++)
  781. if (buffer[i + final] < 0x20)
  782. break;
  783. if (i + final >= size || buffer[i + final] != '\a')
  784. break; /* Not enough data or no bell found */
  785. /* FIXME: XTerm also reacts to <ESC><backslash> and <ST> */
  786. /* FIXME: differenciate between not enough data (try again) and
  787. invalid data (print shit) */
  788. skip += final;
  789. string = (char *)malloc(final - (semicolon + 1) + 1);
  790. memcpy(string, buffer + i + (semicolon + 1),
  791. final - (semicolon + 1));
  792. string[final - (semicolon + 1)] = '\0';
  793. msg::debug("ansi import: got OSC command %i string '%s'\n", command,
  794. string);
  795. if (command == 0 || command == 2)
  796. {
  797. if (m_title)
  798. free(m_title);
  799. m_title = string;
  800. }
  801. else
  802. free(string);
  803. }
  804. /* Get the character we’re going to paste */
  805. else
  806. {
  807. size_t bytes;
  808. if (i + 6 < size)
  809. {
  810. ch = caca_utf8_to_utf32((char const *)(buffer + i), &bytes);
  811. }
  812. else
  813. {
  814. /* Add a trailing zero to what we're going to read */
  815. char tmp[7];
  816. memcpy(tmp, buffer + i, size - i);
  817. tmp[size - i] = '\0';
  818. ch = caca_utf8_to_utf32(tmp, &bytes);
  819. }
  820. if (!bytes)
  821. {
  822. /* If the Unicode is invalid, assume it was latin1. */
  823. ch = buffer[i];
  824. bytes = 1;
  825. }
  826. /* very incomplete ISO-2022 implementation tailored to DEC ACS */
  827. if (m_conv_state.cs == '@')
  828. {
  829. if (((ch > ' ') && (ch <= '~'))
  830. &&
  831. (m_conv_state.
  832. gn[m_conv_state.ss ? m_conv_state.
  833. gn[m_conv_state.ss] : m_conv_state.glr[0]] == '0'))
  834. {
  835. ch = dec_acs(ch);
  836. }
  837. else if (((ch > 0x80) && (ch < 0xff))
  838. && (m_conv_state.gn[m_conv_state.glr[1]] == '0'))
  839. {
  840. ch = dec_acs(ch + ' ' - 0x80);
  841. }
  842. }
  843. m_conv_state.ss = 0; /* no single-shift (GL) */
  844. wch = caca_utf32_is_fullwidth(ch) ? 2 : 1;
  845. skip += bytes - 1;
  846. }
  847. /* Wrap long lines or grow horizontally */
  848. while ((unsigned int)x + wch > width)
  849. {
  850. x -= width;
  851. y++;
  852. }
  853. /* Scroll or grow vertically */
  854. if ((unsigned int)y >= bottom)
  855. {
  856. int lines = (y - bottom) + 1;
  857. savedattr = caca_get_attr(m_caca, -1, -1);
  858. for (j = top - 1; j + lines < bottom; j++)
  859. {
  860. for (k = 0; k < width; k++)
  861. {
  862. caca_put_char(m_caca, k, j,
  863. caca_get_char(m_caca, k, j + lines));
  864. caca_put_attr(m_caca, k, j,
  865. caca_get_attr(m_caca, k, j + lines));
  866. }
  867. }
  868. caca_set_attr(m_caca, m_clearattr);
  869. caca_fill_box(m_caca, 0, bottom - lines, width, bottom - 1, ' ');
  870. y -= lines;
  871. caca_set_attr(m_caca, savedattr);
  872. }
  873. /* Now paste our character, if any */
  874. if (wch)
  875. {
  876. caca_put_char(m_caca, x, y, ch);
  877. x += wch;
  878. }
  879. }
  880. caca_gotoxy(m_caca, x, y);
  881. if (i)
  882. m_changed = 1;
  883. return i;
  884. }
  885. size_t Term::SendAnsi(char const *str)
  886. {
  887. size_t ret = strlen(str);
  888. /* FIXME TODO: not implemented */
  889. msg::debug("Sending %d-character ANSI sequence\n", (int)ret);
  890. return ret;
  891. }
  892. /* Coding Method Delimiter (CMD), ECMA-48 (1991), ISO/IEC 6429:1992 (ISO IR
  893. 189) */
  894. void Iso2022Conversion::Reset()
  895. {
  896. cs = '@'; /* ISO-2022 coding system */
  897. cn[0] = '@'; /* ISO 646 C0 control charset */
  898. cn[1] = 'C'; /* ISO 6429-1983 C1 control charset */
  899. glr[0] = 0; /* G0 in GL */
  900. glr[1] = 2; /* G2 in GR */
  901. gn[0] = 'B'; /* US-ASCII G0 charset */
  902. gn[1] = '0'; /* DEC ACS G1 charset */
  903. gn[2] = LITERAL2CHAR('.', 'A'); /* ISO 8859-1 G2 charset */
  904. gn[3] = LITERAL2CHAR('.', 'A'); /* ISO 8859-1 G3 charset */
  905. ss = 0; /* no single-shift (GL) */
  906. ctrl8bit = 1;
  907. }
  908. /* XXX : ANSI loader helper */
  909. void Term::ReadGrcm(unsigned int argc, unsigned int const *argv)
  910. {
  911. static uint8_t const ansi2caca[] = {
  912. CACA_BLACK, CACA_RED, CACA_GREEN, CACA_BROWN,
  913. CACA_BLUE, CACA_MAGENTA, CACA_CYAN, CACA_LIGHTGRAY
  914. };
  915. unsigned int j;
  916. uint8_t efg, ebg; /* Effective (libcaca) fg/bg */
  917. for (j = 0; j < argc; j++)
  918. {
  919. /* Defined in ECMA-48 8.3.117: SGR - SELECT GRAPHIC RENDITION */
  920. if (argv[j] >= 30 && argv[j] <= 37)
  921. m_fg = ansi2caca[argv[j] - 30];
  922. else if (argv[j] >= 40 && argv[j] <= 47)
  923. m_bg = ansi2caca[argv[j] - 40];
  924. else if (argv[j] >= 90 && argv[j] <= 97)
  925. m_fg = ansi2caca[argv[j] - 90] + 8;
  926. else if (argv[j] >= 100 && argv[j] <= 107)
  927. m_bg = ansi2caca[argv[j] - 100] + 8;
  928. else
  929. switch (argv[j])
  930. {
  931. case 0: /* default rendition */
  932. m_fg = m_dfg;
  933. m_bg = m_dbg;
  934. m_bold = m_blink = m_italics = m_negative
  935. = m_concealed = m_underline = m_faint = m_strike
  936. = m_proportional = 0;
  937. break;
  938. case 1: /* bold or increased intensity */
  939. m_bold = 1;
  940. break;
  941. case 2: /* faint, decreased intensity or second colour
  942. */
  943. m_faint = 1;
  944. break;
  945. case 3: /* italicized */
  946. m_italics = 1;
  947. break;
  948. case 4: /* singly underlined */
  949. m_underline = 1;
  950. break;
  951. case 5: /* slowly blinking (less then 150 per minute) */
  952. case 6: /* rapidly blinking (150 per minute or more) */
  953. m_blink = 1;
  954. break;
  955. case 7: /* negative image */
  956. m_negative = 1;
  957. break;
  958. case 8: /* concealed characters */
  959. m_concealed = 1;
  960. break;
  961. case 9: /* crossed-out (characters still legible but
  962. marked as to be deleted */
  963. m_strike = 1;
  964. break;
  965. case 21: /* doubly underlined */
  966. m_underline = 1;
  967. break;
  968. case 22: /* normal colour or normal intensity (neither
  969. bold nor faint) */
  970. m_bold = m_faint = 0;
  971. break;
  972. case 23: /* not italicized, not fraktur */
  973. m_italics = 0;
  974. break;
  975. case 24: /* not underlined (neither singly nor doubly) */
  976. m_underline = 0;
  977. break;
  978. case 25: /* steady (not blinking) */
  979. m_blink = 0;
  980. break;
  981. case 26: /* (reserved for proportional spacing as
  982. specified in CCITT Recommendation T.61) */
  983. m_proportional = 1;
  984. break;
  985. case 27: /* positive image */
  986. m_negative = 0;
  987. break;
  988. case 28: /* revealed characters */
  989. m_concealed = 0;
  990. break;
  991. case 29: /* not crossed out */
  992. m_strike = 0;
  993. break;
  994. case 38: /* (reserved for future standardization,
  995. intended for setting character foreground
  996. colour as specified in ISO 8613-6 [CCITT
  997. Recommendation T.416]) */
  998. break;
  999. case 39: /* default display colour
  1000. (implementation-defined) */
  1001. m_fg = m_dfg;
  1002. break;
  1003. case 48: /* (reserved for future standardization,
  1004. intended for setting character background
  1005. colour as specified in ISO 8613-6 [CCITT
  1006. Recommendation T.416]) */
  1007. break;
  1008. case 49: /* default background colour
  1009. (implementation-defined) */
  1010. m_bg = m_dbg;
  1011. break;
  1012. case 50: /* (reserved for cancelling the effect of the
  1013. rendering aspect established by parameter
  1014. value 26) */
  1015. m_proportional = 0;
  1016. break;
  1017. default:
  1018. msg::debug("ansi import: unknown sgr %i\n", argv[j]);
  1019. break;
  1020. }
  1021. }
  1022. if (m_concealed)
  1023. {
  1024. efg = ebg = CACA_TRANSPARENT;
  1025. }
  1026. else
  1027. {
  1028. efg = m_negative ? m_bg : m_fg;
  1029. ebg = m_negative ? m_fg : m_bg;
  1030. if (m_bold)
  1031. {
  1032. if (efg < 8)
  1033. efg += 8;
  1034. else if (efg == CACA_DEFAULT)
  1035. efg = CACA_WHITE;
  1036. }
  1037. }
  1038. caca_set_color_ansi(m_caca, efg, ebg);
  1039. }