terminal-vga.c 31 KB

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