terminal-vga.c 29 KB

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