terminal-vga.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2014-2018 K. Lange
  5. *
  6. * Terminal Emulator - VGA
  7. */
  8. #include <stdio.h>
  9. #include <stdint.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <signal.h>
  13. #include <time.h>
  14. #include <fcntl.h>
  15. #include <unistd.h>
  16. #include <sys/stat.h>
  17. #include <sys/ioctl.h>
  18. #include <sys/time.h>
  19. #include <sys/wait.h>
  20. #include <getopt.h>
  21. #include <errno.h>
  22. #include <pty.h>
  23. #include <sys/fswait.h>
  24. #include <wchar.h>
  25. #include <toaru/utf8decode.h>
  26. #include <toaru/kbd.h>
  27. #include <toaru/graphics.h>
  28. #include <toaru/termemu.h>
  29. #include <toaru/mouse.h>
  30. #include "vga-palette.h"
  31. #define USE_BELL 0
  32. /* master and slave pty descriptors */
  33. static int fd_master, fd_slave;
  34. static FILE * terminal;
  35. uint16_t term_width = 80; /* Width of the terminal (in cells) */
  36. uint16_t term_height = 25; /* Height of the terminal (in cells) */
  37. uint16_t csr_x = 0; /* Cursor X */
  38. uint16_t csr_y = 0; /* Cursor Y */
  39. term_cell_t * term_buffer = NULL; /* The terminal cell buffer */
  40. uint32_t current_fg = 7; /* Current foreground color */
  41. uint32_t current_bg = 0; /* Current background color */
  42. uint8_t cursor_on = 1; /* Whether or not the cursor should be rendered */
  43. uint8_t _login_shell = 0; /* Whether we're going to display a login shell or not */
  44. uint8_t _hold_out = 0; /* state indicator on last cell ignore \n */
  45. uint64_t mouse_ticks = 0;
  46. int selection = 0;
  47. int selection_start_x = 0;
  48. int selection_start_y = 0;
  49. int selection_end_x = 0;
  50. int selection_end_y = 0;
  51. char * selection_text = NULL;
  52. #define char_width 1
  53. #define char_height 1
  54. term_state_t * ansi_state = NULL;
  55. void reinit(); /* Defined way further down */
  56. void term_redraw_cursor();
  57. void term_clear();
  58. void dump_buffer();
  59. static uint64_t get_ticks(void) {
  60. struct timeval now;
  61. gettimeofday(&now, NULL);
  62. return (uint64_t)now.tv_sec * 1000000LL + (uint64_t)now.tv_usec;
  63. }
  64. static int color_distance(uint32_t a, uint32_t b) {
  65. int a_r = (a & 0xFF0000) >> 16;
  66. int a_g = (a & 0xFF00) >> 8;
  67. int a_b = (a & 0xFF);
  68. int b_r = (b & 0xFF0000) >> 16;
  69. int b_g = (b & 0xFF00) >> 8;
  70. int b_b = (b & 0xFF);
  71. int distance = 0;
  72. distance += abs(a_r - b_r) * 3;
  73. distance += abs(a_g - b_g) * 6;
  74. distance += abs(a_b - b_b) * 10;
  75. return distance;
  76. }
  77. static uint32_t vga_base_colors[] = {
  78. 0x000000,
  79. 0xAA0000,
  80. 0x00AA00,
  81. 0xAA5500,
  82. 0x0000AA,
  83. 0xAA00AA,
  84. 0x00AAAA,
  85. 0xAAAAAA,
  86. 0x555555,
  87. 0xFF5555,
  88. 0x55AA55,
  89. 0xFFFF55,
  90. 0x5555FF,
  91. 0xFF55FF,
  92. 0x55FFFF,
  93. 0xFFFFFF,
  94. };
  95. #if 0
  96. static int is_gray(uint32_t a) {
  97. int a_r = (a & 0xFF0000) >> 16;
  98. int a_g = (a & 0xFF00) >> 8;
  99. int a_b = (a & 0xFF);
  100. return (a_r == a_g && a_g == a_b);
  101. }
  102. #endif
  103. static int best_match(uint32_t a) {
  104. int best_distance = INT32_MAX;
  105. int best_index = 0;
  106. for (int j = 0; j < 16; ++j) {
  107. int distance = color_distance(a, vga_base_colors[j]);
  108. if (distance < best_distance) {
  109. best_index = j;
  110. best_distance = distance;
  111. }
  112. }
  113. return best_index;
  114. }
  115. volatile int exit_application = 0;
  116. /* Returns the lower of two shorts */
  117. uint16_t min(uint16_t a, uint16_t b) {
  118. return (a < b) ? a : b;
  119. }
  120. /* Returns the higher of two shorts */
  121. uint16_t max(uint16_t a, uint16_t b) {
  122. return (a > b) ? a : b;
  123. }
  124. void set_title(char * c) {
  125. /* Do nothing */
  126. }
  127. static void cell_redraw(uint16_t x, uint16_t y);
  128. static void cell_redraw_inverted(uint16_t x, uint16_t y);
  129. void iterate_selection(void (*func)(uint16_t x, uint16_t y)) {
  130. if (selection_end_y < selection_start_y) {
  131. for (int x = selection_end_x; x < term_width; ++x) {
  132. func(x, selection_end_y);
  133. }
  134. for (int y = selection_end_y + 1; y < selection_start_y; ++y) {
  135. for (int x = 0; x < term_width; ++x) {
  136. func(x, y);
  137. }
  138. }
  139. for (int x = 0; x <= selection_start_x; ++x) {
  140. func(x, selection_start_y);
  141. }
  142. } else if (selection_start_y == selection_end_y) {
  143. if (selection_start_x > selection_end_x) {
  144. for (int x = selection_end_x; x <= selection_start_x; ++x) {
  145. func(x, selection_start_y);
  146. }
  147. } else {
  148. for (int x = selection_start_x; x <= selection_end_x; ++x) {
  149. func(x, selection_start_y);
  150. }
  151. }
  152. } else {
  153. for (int x = selection_start_x; x < term_width; ++x) {
  154. func(x, selection_start_y);
  155. }
  156. for (int y = selection_start_y + 1; y < selection_end_y; ++y) {
  157. for (int x = 0; x < term_width; ++x) {
  158. func(x, y);
  159. }
  160. }
  161. for (int x = 0; x <= selection_end_x; ++x) {
  162. func(x, selection_end_y);
  163. }
  164. }
  165. }
  166. void unredraw_selection(void) {
  167. iterate_selection(cell_redraw);
  168. }
  169. void redraw_selection(void) {
  170. iterate_selection(cell_redraw_inverted);
  171. }
  172. static int _selection_count = 0;
  173. static int _selection_i = 0;
  174. static int to_eight(uint32_t codepoint, char * out) {
  175. memset(out, 0x00, 7);
  176. if (codepoint < 0x0080) {
  177. out[0] = (char)codepoint;
  178. } else if (codepoint < 0x0800) {
  179. out[0] = 0xC0 | (codepoint >> 6);
  180. out[1] = 0x80 | (codepoint & 0x3F);
  181. } else if (codepoint < 0x10000) {
  182. out[0] = 0xE0 | (codepoint >> 12);
  183. out[1] = 0x80 | ((codepoint >> 6) & 0x3F);
  184. out[2] = 0x80 | (codepoint & 0x3F);
  185. } else if (codepoint < 0x200000) {
  186. out[0] = 0xF0 | (codepoint >> 18);
  187. out[1] = 0x80 | ((codepoint >> 12) & 0x3F);
  188. out[2] = 0x80 | ((codepoint >> 6) & 0x3F);
  189. out[3] = 0x80 | ((codepoint) & 0x3F);
  190. } else if (codepoint < 0x4000000) {
  191. out[0] = 0xF8 | (codepoint >> 24);
  192. out[1] = 0x80 | (codepoint >> 18);
  193. out[2] = 0x80 | ((codepoint >> 12) & 0x3F);
  194. out[3] = 0x80 | ((codepoint >> 6) & 0x3F);
  195. out[4] = 0x80 | ((codepoint) & 0x3F);
  196. } else {
  197. out[0] = 0xF8 | (codepoint >> 30);
  198. out[1] = 0x80 | ((codepoint >> 24) & 0x3F);
  199. out[2] = 0x80 | ((codepoint >> 18) & 0x3F);
  200. out[3] = 0x80 | ((codepoint >> 12) & 0x3F);
  201. out[4] = 0x80 | ((codepoint >> 6) & 0x3F);
  202. out[5] = 0x80 | ((codepoint) & 0x3F);
  203. }
  204. return strlen(out);
  205. }
  206. void count_selection(uint16_t x, uint16_t y) {
  207. term_cell_t * cell = (term_cell_t *)((uintptr_t)term_buffer + (y * term_width + x) * sizeof(term_cell_t));
  208. if (((uint32_t *)cell)[0] != 0x00000000) {
  209. char tmp[7];
  210. _selection_count += to_eight(cell->c, tmp);
  211. }
  212. if (x == term_width - 1) {
  213. _selection_count++;
  214. }
  215. }
  216. void write_selection(uint16_t x, uint16_t y) {
  217. term_cell_t * cell = (term_cell_t *)((uintptr_t)term_buffer + (y * term_width + x) * sizeof(term_cell_t));
  218. if (((uint32_t *)cell)[0] != 0x00000000) {
  219. char tmp[7];
  220. int count = to_eight(cell->c, tmp);
  221. for (int i = 0; i < count; ++i) {
  222. selection_text[_selection_i] = tmp[i];
  223. _selection_i++;
  224. }
  225. }
  226. if (x == term_width - 1) {
  227. selection_text[_selection_i] = '\n';;
  228. _selection_i++;
  229. }
  230. }
  231. char * copy_selection(void) {
  232. _selection_count = 0;
  233. iterate_selection(count_selection);
  234. if (selection_text) {
  235. free(selection_text);
  236. }
  237. if (_selection_count == 0) {
  238. return NULL;
  239. }
  240. selection_text = malloc(_selection_count + 1);
  241. selection_text[_selection_count] = '\0';
  242. _selection_i = 0;
  243. iterate_selection(write_selection);
  244. if (selection_text[_selection_count-1] == '\n') {
  245. /* Don't end on a line feed */
  246. selection_text[_selection_count-1] = '\0';
  247. }
  248. return selection_text;
  249. }
  250. void input_buffer_stuff(char * str) {
  251. size_t s = strlen(str) + 1;
  252. write(fd_master, str, s);
  253. }
  254. unsigned short * textmemptr = (unsigned short *)0xB8000;
  255. void placech(unsigned char c, int x, int y, int attr) {
  256. unsigned short *where;
  257. unsigned att = attr << 8;
  258. where = textmemptr + (y * 80 + x);
  259. *where = c | att;
  260. }
  261. /* ANSI-to-VGA */
  262. char vga_to_ansi[] = {
  263. 0, 4, 2, 6, 1, 5, 3, 7,
  264. 8,12,10,14, 9,13,11,15
  265. };
  266. uint32_t ununicode(uint32_t c) {
  267. switch (c) {
  268. case L'☺': return 1;
  269. case L'☻': return 2;
  270. case L'♥': return 3;
  271. case L'♦': return 4;
  272. case L'♣': return 5;
  273. case L'♠': return 6;
  274. case L'•': return 7;
  275. case L'◘': return 8;
  276. case L'○': return 9;
  277. case L'◙': return 10;
  278. case L'♂': return 11;
  279. case L'♀': return 12;
  280. case L'♪': return 13;
  281. case L'♫': return 14;
  282. case L'☼': return 15;
  283. case L'►': return 16;
  284. case L'◄': return 17;
  285. case L'↕': return 18;
  286. case L'‼': return 19;
  287. case L'¶': return 20;
  288. case L'§': return 21;
  289. case L'▬': return 22;
  290. case L'↨': return 23;
  291. case L'↑': return 24;
  292. case L'↓': return 25;
  293. case L'→': return 26;
  294. case L'←': return 27;
  295. case L'∟': return 28;
  296. case L'↔': return 29;
  297. case L'▲': return 30;
  298. case L'▼': return 31;
  299. /* ASCII text */
  300. case L'⌂': return 127;
  301. case L'Ç': return 128;
  302. case L'ü': return 129;
  303. case L'é': return 130;
  304. case L'â': return 131;
  305. case L'ä': return 132;
  306. case L'à': return 133;
  307. case L'å': return 134;
  308. case L'ç': return 135;
  309. case L'ê': return 136;
  310. case L'ë': return 137;
  311. case L'è': return 138;
  312. case L'ï': return 139;
  313. case L'î': return 140;
  314. case L'ì': return 141;
  315. case L'Ä': return 142;
  316. case L'Å': return 143;
  317. case L'É': return 144;
  318. case L'æ': return 145;
  319. case L'Æ': return 146;
  320. case L'ô': return 147;
  321. case L'ö': return 148;
  322. case L'ò': return 149;
  323. case L'û': return 150;
  324. case L'ù': return 151;
  325. case L'ÿ': return 152;
  326. case L'Ö': return 153;
  327. case L'Ü': return 154;
  328. case L'¢': return 155;
  329. case L'£': return 156;
  330. case L'¥': return 157;
  331. case L'₧': return 158;
  332. case L'ƒ': return 159;
  333. case L'á': return 160;
  334. case L'í': return 161;
  335. case L'ó': return 162;
  336. case L'ú': return 163;
  337. case L'ñ': return 164;
  338. case L'Ñ': return 165;
  339. case L'ª': return 166;
  340. case L'º': return 167;
  341. case L'¿': return 168;
  342. case L'⌐': return 169;
  343. case L'¬': return 170;
  344. case L'½': return 171;
  345. case L'¼': return 172;
  346. case L'¡': return 173;
  347. case L'«': return 174;
  348. case L'»': return 175;
  349. case L'░': return 176;
  350. case L'▒': return 177;
  351. case L'▓': return 178;
  352. case L'│': return 179;
  353. case L'┤': return 180;
  354. case L'╡': return 181;
  355. case L'╢': return 182;
  356. case L'╖': return 183;
  357. case L'╕': return 184;
  358. case L'╣': return 185;
  359. case L'║': return 186;
  360. case L'╗': return 187;
  361. case L'╝': return 188;
  362. case L'╜': return 189;
  363. case L'╛': return 190;
  364. case L'┐': return 191;
  365. case L'└': return 192;
  366. case L'┴': return 193;
  367. case L'┬': return 194;
  368. case L'├': return 195;
  369. case L'─': return 196;
  370. case L'┼': return 197;
  371. case L'╞': return 198;
  372. case L'╟': return 199;
  373. case L'╚': return 200;
  374. case L'╔': return 201;
  375. case L'╩': return 202;
  376. case L'╦': return 203;
  377. case L'╠': return 204;
  378. case L'═': return 205;
  379. case L'╬': return 206;
  380. case L'╧': return 207;
  381. case L'╨': return 208;
  382. case L'╤': return 209;
  383. case L'╥': return 210;
  384. case L'╙': return 211;
  385. case L'╘': return 212;
  386. case L'╒': return 213;
  387. case L'╓': return 214;
  388. case L'╫': return 215;
  389. case L'╪': return 216;
  390. case L'┘': return 217;
  391. case L'┌': return 218;
  392. case L'█': return 219;
  393. case L'▄': return 220;
  394. case L'▌': return 221;
  395. case L'▐': return 222;
  396. case L'▀': return 223;
  397. case L'α': return 224;
  398. case L'ß': return 225;
  399. case L'Γ': return 226;
  400. case L'π': return 227;
  401. case L'Σ': return 228;
  402. case L'σ': return 229;
  403. case L'µ': return 230;
  404. case L'τ': return 231;
  405. case L'Φ': return 232;
  406. case L'Θ': return 233;
  407. case L'Ω': return 234;
  408. case L'δ': return 235;
  409. case L'∞': return 236;
  410. case L'φ': return 237;
  411. case L'ε': return 238;
  412. case L'∩': return 239;
  413. case L'≡': return 240;
  414. case L'±': return 241;
  415. case L'≥': return 242;
  416. case L'≤': return 243;
  417. case L'⌠': return 244;
  418. case L'⌡': return 245;
  419. case L'÷': return 246;
  420. case L'≈': return 247;
  421. case L'°': return 248;
  422. case L'∙': return 249;
  423. case L'·': return 250;
  424. case L'√': return 251;
  425. case L'ⁿ': return 252;
  426. case L'²': return 253;
  427. case L'■': return 254;
  428. }
  429. return 4;
  430. }
  431. void
  432. term_write_char(
  433. uint32_t val,
  434. uint16_t x,
  435. uint16_t y,
  436. uint32_t fg,
  437. uint32_t bg,
  438. uint8_t flags
  439. ) {
  440. if (val > 128) val = ununicode(val);
  441. if (fg > 256) {
  442. fg = best_match(fg);
  443. }
  444. if (bg > 256) {
  445. bg = best_match(bg);
  446. }
  447. if (fg > 16) {
  448. fg = vga_colors[fg];
  449. }
  450. if (bg > 16) {
  451. bg = vga_colors[bg];
  452. }
  453. if (fg == 16) fg = 0;
  454. if (bg == 16) bg = 0;
  455. placech(val, x, y, (vga_to_ansi[fg] & 0xF) | (vga_to_ansi[bg] << 4));
  456. }
  457. static void cell_set(uint16_t x, uint16_t y, uint32_t c, uint32_t fg, uint32_t bg, uint8_t flags) {
  458. if (x >= term_width || y >= term_height) return;
  459. term_cell_t * cell = (term_cell_t *)((uintptr_t)term_buffer + (y * term_width + x) * sizeof(term_cell_t));
  460. cell->c = c;
  461. cell->fg = fg;
  462. cell->bg = bg;
  463. cell->flags = flags;
  464. }
  465. static void cell_redraw(uint16_t x, uint16_t y) {
  466. if (x >= term_width || y >= term_height) return;
  467. term_cell_t * cell = (term_cell_t *)((uintptr_t)term_buffer + (y * term_width + x) * sizeof(term_cell_t));
  468. if (((uint32_t *)cell)[0] == 0x00000000) {
  469. term_write_char(' ', x * char_width, y * char_height, TERM_DEFAULT_FG, TERM_DEFAULT_BG, TERM_DEFAULT_FLAGS);
  470. } else {
  471. term_write_char(cell->c, x * char_width, y * char_height, cell->fg, cell->bg, cell->flags);
  472. }
  473. }
  474. static void cell_redraw_inverted(uint16_t x, uint16_t y) {
  475. if (x >= term_width || y >= term_height) return;
  476. term_cell_t * cell = (term_cell_t *)((uintptr_t)term_buffer + (y * term_width + x) * sizeof(term_cell_t));
  477. if (((uint32_t *)cell)[0] == 0x00000000) {
  478. term_write_char(' ', x * char_width, y * char_height, TERM_DEFAULT_BG, TERM_DEFAULT_FG, TERM_DEFAULT_FLAGS | ANSI_SPECBG);
  479. } else {
  480. term_write_char(cell->c, x * char_width, y * char_height, cell->bg, cell->fg, cell->flags | ANSI_SPECBG);
  481. }
  482. }
  483. #if 0
  484. static void cell_redraw_box(uint16_t x, uint16_t y) {
  485. if (x >= term_width || y >= term_height) return;
  486. term_cell_t * cell = (term_cell_t *)((uintptr_t)term_buffer + (y * term_width + x) * sizeof(term_cell_t));
  487. if (((uint32_t *)cell)[0] == 0x00000000) {
  488. term_write_char(' ', x * char_width, y * char_height, TERM_DEFAULT_FG, TERM_DEFAULT_BG, TERM_DEFAULT_FLAGS | ANSI_BORDER);
  489. } else {
  490. term_write_char(cell->c, x * char_width, y * char_height, cell->fg, cell->bg, cell->flags | ANSI_BORDER);
  491. }
  492. }
  493. #endif
  494. void render_cursor() {
  495. cell_redraw_inverted(csr_x, csr_y);
  496. }
  497. static uint8_t cursor_flipped = 0;
  498. void draw_cursor() {
  499. if (!cursor_on) return;
  500. mouse_ticks = get_ticks();
  501. cursor_flipped = 0;
  502. render_cursor();
  503. }
  504. void term_redraw_all() {
  505. for (uint16_t y = 0; y < term_height; ++y) {
  506. for (uint16_t x = 0; x < term_width; ++x) {
  507. cell_redraw(x,y);
  508. }
  509. }
  510. }
  511. void term_scroll(int how_much) {
  512. if (how_much >= term_height || -how_much >= term_height) {
  513. term_clear();
  514. return;
  515. }
  516. if (how_much == 0) {
  517. return;
  518. }
  519. if (how_much > 0) {
  520. /* Shift terminal cells one row up */
  521. memmove(term_buffer, (void *)((uintptr_t)term_buffer + sizeof(term_cell_t) * term_width), sizeof(term_cell_t) * term_width * (term_height - how_much));
  522. /* Reset the "new" row to clean cells */
  523. memset((void *)((uintptr_t)term_buffer + sizeof(term_cell_t) * term_width * (term_height - how_much)), 0x0, sizeof(term_cell_t) * term_width * how_much);
  524. for (int i = 0; i < how_much; ++i) {
  525. for (uint16_t x = 0; x < term_width; ++x) {
  526. cell_set(x,term_height - how_much,' ', current_fg, current_bg, ansi_state->flags);
  527. }
  528. }
  529. term_redraw_all();
  530. } else {
  531. how_much = -how_much;
  532. /* Shift terminal cells one row up */
  533. memmove((void *)((uintptr_t)term_buffer + sizeof(term_cell_t) * term_width), term_buffer, sizeof(term_cell_t) * term_width * (term_height - how_much));
  534. /* Reset the "new" row to clean cells */
  535. memset(term_buffer, 0x0, sizeof(term_cell_t) * term_width * how_much);
  536. term_redraw_all();
  537. }
  538. }
  539. int is_wide(uint32_t codepoint) {
  540. if (codepoint < 256) return 0;
  541. return wcwidth(codepoint) == 2;
  542. }
  543. void term_write(char c) {
  544. static uint32_t codepoint = 0;
  545. static uint32_t unicode_state = 0;
  546. cell_redraw(csr_x, csr_y);
  547. if (!decode(&unicode_state, &codepoint, (uint8_t)c)) {
  548. if (c == '\r') {
  549. csr_x = 0;
  550. return;
  551. }
  552. if (csr_x == term_width) {
  553. csr_x = 0;
  554. ++csr_y;
  555. }
  556. if (csr_y == term_height) {
  557. term_scroll(1);
  558. csr_y = term_height - 1;
  559. }
  560. if (c == '\n') {
  561. if (csr_x == 0 && _hold_out) {
  562. _hold_out = 0;
  563. return;
  564. }
  565. ++csr_y;
  566. if (csr_y == term_height) {
  567. term_scroll(1);
  568. csr_y = term_height - 1;
  569. }
  570. draw_cursor();
  571. } else if (c == '\007') {
  572. /* bell */
  573. } else if (c == '\b') {
  574. if (csr_x > 0) {
  575. --csr_x;
  576. }
  577. cell_redraw(csr_x, csr_y);
  578. draw_cursor();
  579. } else if (c == '\t') {
  580. csr_x += (8 - csr_x % 8);
  581. draw_cursor();
  582. } else {
  583. int wide = is_wide(codepoint);
  584. uint8_t flags = ansi_state->flags;
  585. if (wide && csr_x == term_width - 1) {
  586. csr_x = 0;
  587. ++csr_y;
  588. }
  589. if (wide) {
  590. flags = flags | ANSI_WIDE;
  591. }
  592. cell_set(csr_x,csr_y, codepoint, current_fg, current_bg, flags);
  593. cell_redraw(csr_x,csr_y);
  594. csr_x++;
  595. if (wide && csr_x != term_width) {
  596. cell_set(csr_x, csr_y, 0xFFFF, current_fg, current_bg, ansi_state->flags);
  597. cell_redraw(csr_x,csr_y);
  598. cell_redraw(csr_x-1,csr_y);
  599. csr_x++;
  600. }
  601. }
  602. } else if (unicode_state == UTF8_REJECT) {
  603. unicode_state = 0;
  604. }
  605. draw_cursor();
  606. }
  607. void term_set_csr(int x, int y) {
  608. cell_redraw(csr_x,csr_y);
  609. csr_x = x;
  610. csr_y = y;
  611. draw_cursor();
  612. }
  613. int term_get_csr_x() {
  614. return csr_x;
  615. }
  616. int term_get_csr_y() {
  617. return csr_y;
  618. }
  619. void term_set_csr_show(int on) {
  620. cursor_on = on;
  621. if (on) {
  622. draw_cursor();
  623. }
  624. }
  625. void term_set_colors(uint32_t fg, uint32_t bg) {
  626. current_fg = fg;
  627. current_bg = bg;
  628. }
  629. void term_redraw_cursor() {
  630. if (term_buffer) {
  631. draw_cursor();
  632. }
  633. }
  634. void flip_cursor() {
  635. if (cursor_flipped) {
  636. cell_redraw(csr_x, csr_y);
  637. } else {
  638. render_cursor();
  639. }
  640. cursor_flipped = 1 - cursor_flipped;
  641. }
  642. void term_set_cell(int x, int y, uint32_t c) {
  643. cell_set(x, y, c, current_fg, current_bg, ansi_state->flags);
  644. cell_redraw(x, y);
  645. }
  646. void term_redraw_cell(int x, int y) {
  647. if (x < 0 || y < 0 || x >= term_width || y >= term_height) return;
  648. cell_redraw(x,y);
  649. }
  650. void term_clear(int i) {
  651. if (i == 2) {
  652. /* Oh dear */
  653. csr_x = 0;
  654. csr_y = 0;
  655. memset((void *)term_buffer, 0x00, term_width * term_height * sizeof(term_cell_t));
  656. term_redraw_all();
  657. } else if (i == 0) {
  658. for (int x = csr_x; x < term_width; ++x) {
  659. term_set_cell(x, csr_y, ' ');
  660. }
  661. for (int y = csr_y + 1; y < term_height; ++y) {
  662. for (int x = 0; x < term_width; ++x) {
  663. term_set_cell(x, y, ' ');
  664. }
  665. }
  666. } else if (i == 1) {
  667. for (int y = 0; y < csr_y; ++y) {
  668. for (int x = 0; x < term_width; ++x) {
  669. term_set_cell(x, y, ' ');
  670. }
  671. }
  672. for (int x = 0; x < csr_x; ++x) {
  673. term_set_cell(x, csr_y, ' ');
  674. }
  675. }
  676. }
  677. #define INPUT_SIZE 1024
  678. char input_buffer[INPUT_SIZE];
  679. int input_collected = 0;
  680. void clear_input() {
  681. memset(input_buffer, 0x0, INPUT_SIZE);
  682. input_collected = 0;
  683. }
  684. pid_t child_pid = 0;
  685. void handle_input(char c) {
  686. write(fd_master, &c, 1);
  687. }
  688. void handle_input_s(char * c) {
  689. write(fd_master, c, strlen(c));
  690. }
  691. void key_event(int ret, key_event_t * event) {
  692. if (ret) {
  693. /* Special keys */
  694. if ((event->modifiers & KEY_MOD_LEFT_SHIFT || event->modifiers & KEY_MOD_RIGHT_SHIFT) &&
  695. (event->modifiers & KEY_MOD_LEFT_CTRL || event->modifiers & KEY_MOD_RIGHT_CTRL) &&
  696. (event->keycode == 'c')) {
  697. if (selection) {
  698. /* Copy selection */
  699. copy_selection();
  700. }
  701. return;
  702. }
  703. if ((event->modifiers & KEY_MOD_LEFT_SHIFT || event->modifiers & KEY_MOD_RIGHT_SHIFT) &&
  704. (event->modifiers & KEY_MOD_LEFT_CTRL || event->modifiers & KEY_MOD_RIGHT_CTRL) &&
  705. (event->keycode == 'v')) {
  706. /* Paste selection */
  707. if (selection_text) {
  708. handle_input_s(selection_text);
  709. }
  710. return;
  711. }
  712. if (event->modifiers & KEY_MOD_LEFT_ALT || event->modifiers & KEY_MOD_RIGHT_ALT) {
  713. handle_input('\033');
  714. }
  715. if ((event->modifiers & KEY_MOD_LEFT_SHIFT || event->modifiers & KEY_MOD_RIGHT_SHIFT) &&
  716. event->key == '\t') {
  717. handle_input_s("\033[Z");
  718. return;
  719. }
  720. handle_input(event->key);
  721. } else {
  722. if (event->action == KEY_ACTION_UP) return;
  723. switch (event->keycode) {
  724. case KEY_F1:
  725. handle_input_s("\033OP");
  726. break;
  727. case KEY_F2:
  728. handle_input_s("\033OQ");
  729. break;
  730. case KEY_F3:
  731. handle_input_s("\033OR");
  732. break;
  733. case KEY_F4:
  734. handle_input_s("\033OS");
  735. break;
  736. case KEY_F5:
  737. handle_input_s("\033[15~");
  738. break;
  739. case KEY_F6:
  740. handle_input_s("\033[17~");
  741. break;
  742. case KEY_F7:
  743. handle_input_s("\033[18~");
  744. break;
  745. case KEY_F8:
  746. handle_input_s("\033[19~");
  747. break;
  748. case KEY_F9:
  749. handle_input_s("\033[20~");
  750. break;
  751. case KEY_F10:
  752. handle_input_s("\033[21~");
  753. break;
  754. case KEY_F11:
  755. handle_input_s("\033[23~");
  756. break;
  757. case KEY_F12:
  758. /* XXX This is for testing only */
  759. handle_input_s("テスト");
  760. //handle_input_s("\033[24~");
  761. break;
  762. case KEY_ARROW_UP:
  763. handle_input_s("\033[A");
  764. break;
  765. case KEY_ARROW_DOWN:
  766. handle_input_s("\033[B");
  767. break;
  768. case KEY_ARROW_RIGHT:
  769. handle_input_s("\033[C");
  770. break;
  771. case KEY_ARROW_LEFT:
  772. handle_input_s("\033[D");
  773. break;
  774. case KEY_PAGE_UP:
  775. handle_input_s("\033[5~");
  776. break;
  777. case KEY_PAGE_DOWN:
  778. handle_input_s("\033[6~");
  779. break;
  780. case KEY_HOME:
  781. handle_input_s("\033[H");
  782. break;
  783. case KEY_END:
  784. handle_input_s("\033[F");
  785. break;
  786. case KEY_DEL:
  787. handle_input_s("\033[3~");
  788. break;
  789. }
  790. }
  791. }
  792. void usage(char * argv[]) {
  793. printf(
  794. "VGA Terminal Emulator\n"
  795. "\n"
  796. "usage: %s [-b] [-F] [-h]\n"
  797. "\n"
  798. " -h --help \033[3mShow this help message.\033[0m\n"
  799. "\n",
  800. argv[0]);
  801. }
  802. int unsupported_int(void) { return 0; }
  803. void unsupported(int x, int y, char * data) { }
  804. term_callbacks_t term_callbacks = {
  805. term_write,
  806. term_set_colors,
  807. term_set_csr,
  808. term_get_csr_x,
  809. term_get_csr_y,
  810. term_set_cell,
  811. term_clear,
  812. term_scroll,
  813. term_redraw_cursor,
  814. input_buffer_stuff,
  815. set_title,
  816. unsupported,
  817. unsupported_int,
  818. unsupported_int,
  819. term_set_csr_show,
  820. };
  821. void reinit(int send_sig) {
  822. if (term_buffer) {
  823. /* Do nothing */
  824. } else {
  825. term_buffer = malloc(sizeof(term_cell_t) * term_width * term_height);
  826. memset(term_buffer, 0x0, sizeof(term_cell_t) * term_width * term_height);
  827. }
  828. ansi_state = ansi_init(ansi_state, term_width, term_height, &term_callbacks);
  829. term_redraw_all();
  830. }
  831. void maybe_flip_cursor(void) {
  832. uint64_t ticks = get_ticks();
  833. if (ticks > mouse_ticks + 600000LL) {
  834. mouse_ticks = ticks;
  835. flip_cursor();
  836. }
  837. }
  838. void check_for_exit(void) {
  839. if (exit_application) return;
  840. pid_t pid = waitpid(-1, NULL, WNOHANG);
  841. if (pid != child_pid) return;
  842. /* Clean up */
  843. exit_application = 1;
  844. /* Exit */
  845. char exit_message[] = "[Process terminated]\n";
  846. write(fd_slave, exit_message, sizeof(exit_message));
  847. }
  848. static int mouse_x = 0;
  849. static int mouse_y = 0;
  850. static int last_mouse_buttons = 0;
  851. static int mouse_is_dragging = 0;
  852. #define MOUSE_X_R 820
  853. #define MOUSE_Y_R 2730
  854. static int old_x = 0;
  855. static int old_y = 0;
  856. void handle_mouse_event(mouse_device_packet_t * packet) {
  857. if (mouse_is_dragging) {
  858. if (packet->buttons & LEFT_CLICK) {
  859. /* still dragging */
  860. unredraw_selection();
  861. selection_end_x = mouse_x;
  862. selection_end_y = mouse_y;
  863. redraw_selection();
  864. } else {
  865. mouse_is_dragging = 0;
  866. }
  867. } else {
  868. if (packet->buttons & LEFT_CLICK) {
  869. term_redraw_all();
  870. selection_start_x = mouse_x;
  871. selection_start_y = mouse_y;
  872. selection_end_x = mouse_x;
  873. selection_end_y = mouse_y;
  874. selection = 1;
  875. redraw_selection();
  876. mouse_is_dragging = 1;
  877. } else {
  878. cell_redraw(old_x, old_y);
  879. cell_redraw_inverted(mouse_x, mouse_y);
  880. old_x = mouse_x;
  881. old_y = mouse_y;
  882. }
  883. }
  884. }
  885. static int rel_mouse_x = 0;
  886. static int rel_mouse_y = 0;
  887. void handle_mouse(mouse_device_packet_t * packet) {
  888. rel_mouse_x += packet->x_difference;
  889. rel_mouse_y -= packet->y_difference;
  890. mouse_x = rel_mouse_x / 20;
  891. mouse_y = rel_mouse_y / 40;
  892. if (mouse_x < 0) mouse_x = 0;
  893. if (mouse_y < 0) mouse_y = 0;
  894. if (mouse_x >= term_width) mouse_x = term_width - 1;
  895. if (mouse_y >= term_height) mouse_y = term_height - 1;
  896. handle_mouse_event(packet);
  897. }
  898. void handle_mouse_abs(mouse_device_packet_t * packet) {
  899. mouse_x = packet->x_difference / MOUSE_X_R;
  900. mouse_y = packet->y_difference / MOUSE_Y_R;
  901. rel_mouse_x = mouse_x * 20;
  902. rel_mouse_y = mouse_y * 40;
  903. handle_mouse_event(packet);
  904. }
  905. int main(int argc, char ** argv) {
  906. _login_shell = 0;
  907. static struct option long_opts[] = {
  908. {"login", no_argument, 0, 'l'},
  909. {"help", no_argument, 0, 'h'},
  910. {0,0,0,0}
  911. };
  912. /* Read some arguments */
  913. int index, c;
  914. while ((c = getopt_long(argc, argv, "hl", long_opts, &index)) != -1) {
  915. switch (c) {
  916. case 'l':
  917. _login_shell = 1;
  918. break;
  919. case 'h':
  920. usage(argv);
  921. return 0;
  922. break;
  923. case '?':
  924. break;
  925. default:
  926. break;
  927. }
  928. }
  929. putenv("TERM=toaru");
  930. openpty(&fd_master, &fd_slave, NULL, NULL, NULL);
  931. terminal = fdopen(fd_slave, "w");
  932. struct winsize w;
  933. w.ws_row = term_height;
  934. w.ws_col = term_width;
  935. w.ws_xpixel = 0;
  936. w.ws_ypixel = 0;
  937. ioctl(fd_master, TIOCSWINSZ, &w);
  938. reinit(0);
  939. fflush(stdin);
  940. system("cursor-off"); /* Might GPF */
  941. int pid = getpid();
  942. uint32_t f = fork();
  943. if (getpid() != pid) {
  944. dup2(fd_slave, 0);
  945. dup2(fd_slave, 1);
  946. dup2(fd_slave, 2);
  947. if (argv[optind] != NULL) {
  948. char * tokens[] = {argv[optind], NULL};
  949. execvp(tokens[0], tokens);
  950. fprintf(stderr, "Failed to launch requested startup application.\n");
  951. } else {
  952. if (_login_shell) {
  953. char * tokens[] = {"/bin/login-loop",NULL};
  954. execvp(tokens[0], tokens);
  955. exit(1);
  956. } else {
  957. char * shell = getenv("SHELL");
  958. if (!shell) shell = "/bin/sh"; /* fallback */
  959. char * tokens[] = {shell,NULL};
  960. execvp(tokens[0], tokens);
  961. exit(1);
  962. }
  963. }
  964. exit_application = 1;
  965. return 1;
  966. } else {
  967. child_pid = f;
  968. int kfd = open("/dev/kbd", O_RDONLY);
  969. key_event_t event;
  970. char c;
  971. int vmmouse = 0;
  972. mouse_device_packet_t packet;
  973. int mfd = open("/dev/mouse", O_RDONLY);
  974. int amfd = open("/dev/absmouse", O_RDONLY);
  975. if (amfd == -1) {
  976. amfd = open("/dev/vmmouse", O_RDONLY);
  977. vmmouse = 1;
  978. }
  979. key_event_state_t kbd_state = {0};
  980. /* Prune any keyboard input we got before the terminal started. */
  981. struct stat s;
  982. fstat(kfd, &s);
  983. for (unsigned int i = 0; i < s.st_size; i++) {
  984. char tmp[1];
  985. read(kfd, tmp, 1);
  986. }
  987. int fds[] = {fd_master, kfd, mfd, amfd};
  988. unsigned char buf[1024];
  989. while (!exit_application) {
  990. int index = fswait2(amfd == -1 ? 3 : 4,fds,200);
  991. check_for_exit();
  992. if (index == 0) {
  993. maybe_flip_cursor();
  994. int r = read(fd_master, buf, 1024);
  995. for (int i = 0; i < r; ++i) {
  996. ansi_put(ansi_state, buf[i]);
  997. }
  998. } else if (index == 1) {
  999. maybe_flip_cursor();
  1000. int r = read(kfd, &c, 1);
  1001. if (r > 0) {
  1002. int ret = kbd_scancode(&kbd_state, c, &event);
  1003. key_event(ret, &event);
  1004. }
  1005. } else if (index == 2) {
  1006. /* mouse event */
  1007. int r = read(mfd, (char *)&packet, sizeof(mouse_device_packet_t));
  1008. if (r > 0) {
  1009. last_mouse_buttons = packet.buttons;
  1010. handle_mouse(&packet);
  1011. }
  1012. } else if (amfd != -1 && index == 3) {
  1013. int r = read(amfd, (char *)&packet, sizeof(mouse_device_packet_t));
  1014. if (r > 0) {
  1015. if (!vmmouse) {
  1016. packet.buttons = last_mouse_buttons & 0xF;
  1017. } else {
  1018. last_mouse_buttons = packet.buttons;
  1019. }
  1020. handle_mouse_abs(&packet);
  1021. }
  1022. continue;
  1023. } else {
  1024. maybe_flip_cursor();
  1025. }
  1026. }
  1027. }
  1028. return 0;
  1029. }