terminal-vga.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309
  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. /* BACKSPACE = reads as ^H, should be ^? */
  806. if (event->keycode == 8) {
  807. handle_input(0x7F);
  808. return;
  809. }
  810. handle_input(event->key);
  811. } else {
  812. if (event->action == KEY_ACTION_UP) return;
  813. switch (event->keycode) {
  814. case KEY_F1:
  815. handle_input_s("\033OP");
  816. break;
  817. case KEY_F2:
  818. handle_input_s("\033OQ");
  819. break;
  820. case KEY_F3:
  821. handle_input_s("\033OR");
  822. break;
  823. case KEY_F4:
  824. handle_input_s("\033OS");
  825. break;
  826. case KEY_F5:
  827. handle_input_s("\033[15~");
  828. break;
  829. case KEY_F6:
  830. handle_input_s("\033[17~");
  831. break;
  832. case KEY_F7:
  833. handle_input_s("\033[18~");
  834. break;
  835. case KEY_F8:
  836. handle_input_s("\033[19~");
  837. break;
  838. case KEY_F9:
  839. handle_input_s("\033[20~");
  840. break;
  841. case KEY_F10:
  842. handle_input_s("\033[21~");
  843. break;
  844. case KEY_F11:
  845. handle_input_s("\033[23~");
  846. break;
  847. case KEY_F12:
  848. handle_input_s("\033[24~");
  849. break;
  850. case KEY_ARROW_UP:
  851. if (event->modifiers & KEY_MOD_LEFT_SHIFT && event->modifiers & KEY_MOD_LEFT_CTRL) {
  852. handle_input_s("\033[6A");
  853. } else if (event->modifiers & KEY_MOD_LEFT_CTRL) {
  854. handle_input_s("\033[5A");
  855. } else if (event->modifiers & KEY_MOD_LEFT_SHIFT && event->modifiers & KEY_MOD_LEFT_ALT) {
  856. handle_input_s("\033[4A");
  857. } else if (event->modifiers & KEY_MOD_LEFT_ALT) {
  858. handle_input_s("\033[3A");
  859. } else if (event->modifiers & KEY_MOD_LEFT_SHIFT) {
  860. handle_input_s("\033[2A");
  861. } else {
  862. handle_input_s("\033[A");
  863. }
  864. break;
  865. case KEY_ARROW_DOWN:
  866. if (event->modifiers & KEY_MOD_LEFT_SHIFT && event->modifiers & KEY_MOD_LEFT_CTRL) {
  867. handle_input_s("\033[6B");
  868. } else if (event->modifiers & KEY_MOD_LEFT_CTRL) {
  869. handle_input_s("\033[5B");
  870. } else if (event->modifiers & KEY_MOD_LEFT_SHIFT && event->modifiers & KEY_MOD_LEFT_ALT) {
  871. handle_input_s("\033[4B");
  872. } else if (event->modifiers & KEY_MOD_LEFT_ALT) {
  873. handle_input_s("\033[3B");
  874. } else if (event->modifiers & KEY_MOD_LEFT_SHIFT) {
  875. handle_input_s("\033[2B");
  876. } else {
  877. handle_input_s("\033[B");
  878. }
  879. break;
  880. case KEY_ARROW_RIGHT:
  881. if (event->modifiers & KEY_MOD_LEFT_SHIFT && event->modifiers & KEY_MOD_LEFT_CTRL) {
  882. handle_input_s("\033[6C");
  883. } else if (event->modifiers & KEY_MOD_LEFT_CTRL) {
  884. handle_input_s("\033[5C");
  885. } else if (event->modifiers & KEY_MOD_LEFT_SHIFT && event->modifiers & KEY_MOD_LEFT_ALT) {
  886. handle_input_s("\033[4C");
  887. } else if (event->modifiers & KEY_MOD_LEFT_ALT) {
  888. handle_input_s("\033[3C");
  889. } else if (event->modifiers & KEY_MOD_LEFT_SHIFT) {
  890. handle_input_s("\033[2C");
  891. } else {
  892. handle_input_s("\033[C");
  893. }
  894. break;
  895. case KEY_ARROW_LEFT:
  896. if (event->modifiers & KEY_MOD_LEFT_SHIFT && event->modifiers & KEY_MOD_LEFT_CTRL) {
  897. handle_input_s("\033[6D");
  898. } else if (event->modifiers & KEY_MOD_LEFT_CTRL) {
  899. handle_input_s("\033[5D");
  900. } else if (event->modifiers & KEY_MOD_LEFT_SHIFT && event->modifiers & KEY_MOD_LEFT_ALT) {
  901. handle_input_s("\033[4D");
  902. } else if (event->modifiers & KEY_MOD_LEFT_ALT) {
  903. handle_input_s("\033[3D");
  904. } else if (event->modifiers & KEY_MOD_LEFT_SHIFT) {
  905. handle_input_s("\033[2D");
  906. } else {
  907. handle_input_s("\033[D");
  908. }
  909. break;
  910. case KEY_PAGE_UP:
  911. handle_input_s("\033[5~");
  912. break;
  913. case KEY_PAGE_DOWN:
  914. handle_input_s("\033[6~");
  915. break;
  916. case KEY_HOME:
  917. handle_input_s("\033[H");
  918. break;
  919. case KEY_END:
  920. handle_input_s("\033[F");
  921. break;
  922. case KEY_DEL:
  923. handle_input_s("\033[3~");
  924. break;
  925. }
  926. }
  927. }
  928. void usage(char * argv[]) {
  929. printf(
  930. "VGA Terminal Emulator\n"
  931. "\n"
  932. "usage: %s [-b] [-F] [-h]\n"
  933. "\n"
  934. " -h --help \033[3mShow this help message.\033[0m\n"
  935. "\n",
  936. argv[0]);
  937. }
  938. int unsupported_int(void) { return 0; }
  939. void unsupported(int x, int y, char * data) { }
  940. term_callbacks_t term_callbacks = {
  941. term_write,
  942. term_set_colors,
  943. term_set_csr,
  944. term_get_csr_x,
  945. term_get_csr_y,
  946. term_set_cell,
  947. term_clear,
  948. term_scroll,
  949. term_redraw_cursor,
  950. input_buffer_stuff,
  951. set_title,
  952. unsupported,
  953. unsupported_int,
  954. unsupported_int,
  955. term_set_csr_show,
  956. NULL,
  957. };
  958. void reinit(int send_sig) {
  959. if (term_buffer) {
  960. /* Do nothing */
  961. } else {
  962. term_buffer = malloc(sizeof(term_cell_t) * term_width * term_height);
  963. memset(term_buffer, 0x0, sizeof(term_cell_t) * term_width * term_height);
  964. }
  965. ansi_state = ansi_init(ansi_state, term_width, term_height, &term_callbacks);
  966. term_redraw_all();
  967. }
  968. void maybe_flip_cursor(void) {
  969. uint64_t ticks = get_ticks();
  970. if (ticks > mouse_ticks + 600000LL) {
  971. mouse_ticks = ticks;
  972. flip_cursor();
  973. }
  974. }
  975. void check_for_exit(void) {
  976. if (exit_application) return;
  977. pid_t pid = waitpid(-1, NULL, WNOHANG);
  978. if (pid != child_pid) return;
  979. /* Clean up */
  980. exit_application = 1;
  981. /* Exit */
  982. char exit_message[] = "[Process terminated]\n";
  983. write(fd_slave, exit_message, sizeof(exit_message));
  984. }
  985. static int mouse_x = 0;
  986. static int mouse_y = 0;
  987. static int last_mouse_buttons = 0;
  988. static int mouse_is_dragging = 0;
  989. #define MOUSE_X_R 820
  990. #define MOUSE_Y_R 2621
  991. static int old_x = 0;
  992. static int old_y = 0;
  993. void handle_mouse_event(mouse_device_packet_t * packet) {
  994. if (mouse_is_dragging) {
  995. if (packet->buttons & LEFT_CLICK) {
  996. int old_end_x = selection_end_x;
  997. int old_end_y = selection_end_y;
  998. selection_end_x = mouse_x;
  999. selection_end_y = mouse_y;
  1000. redraw_new_selection(old_end_x, old_end_y);
  1001. } else {
  1002. mouse_is_dragging = 0;
  1003. }
  1004. } else {
  1005. if (packet->buttons & LEFT_CLICK) {
  1006. term_redraw_all();
  1007. selection_start_x = mouse_x;
  1008. selection_start_y = mouse_y;
  1009. selection_end_x = mouse_x;
  1010. selection_end_y = mouse_y;
  1011. selection = 1;
  1012. redraw_selection();
  1013. mouse_is_dragging = 1;
  1014. } else {
  1015. cell_redraw(old_x, old_y);
  1016. cell_redraw_inverted(mouse_x, mouse_y);
  1017. old_x = mouse_x;
  1018. old_y = mouse_y;
  1019. }
  1020. }
  1021. }
  1022. static int rel_mouse_x = 0;
  1023. static int rel_mouse_y = 0;
  1024. void handle_mouse(mouse_device_packet_t * packet) {
  1025. rel_mouse_x += packet->x_difference;
  1026. rel_mouse_y -= packet->y_difference;
  1027. mouse_x = rel_mouse_x / 20;
  1028. mouse_y = rel_mouse_y / 40;
  1029. if (mouse_x < 0) mouse_x = 0;
  1030. if (mouse_y < 0) mouse_y = 0;
  1031. if (mouse_x >= term_width) mouse_x = term_width - 1;
  1032. if (mouse_y >= term_height) mouse_y = term_height - 1;
  1033. handle_mouse_event(packet);
  1034. }
  1035. void handle_mouse_abs(mouse_device_packet_t * packet) {
  1036. mouse_x = packet->x_difference / MOUSE_X_R;
  1037. mouse_y = packet->y_difference / MOUSE_Y_R;
  1038. rel_mouse_x = mouse_x * 20;
  1039. rel_mouse_y = mouse_y * 40;
  1040. handle_mouse_event(packet);
  1041. }
  1042. int main(int argc, char ** argv) {
  1043. _login_shell = 0;
  1044. static struct option long_opts[] = {
  1045. {"login", no_argument, 0, 'l'},
  1046. {"help", no_argument, 0, 'h'},
  1047. {0,0,0,0}
  1048. };
  1049. /* Read some arguments */
  1050. int index, c;
  1051. while ((c = getopt_long(argc, argv, "hl", long_opts, &index)) != -1) {
  1052. switch (c) {
  1053. case 'l':
  1054. _login_shell = 1;
  1055. break;
  1056. case 'h':
  1057. usage(argv);
  1058. return 0;
  1059. break;
  1060. case '?':
  1061. break;
  1062. default:
  1063. break;
  1064. }
  1065. }
  1066. putenv("TERM=toaru");
  1067. openpty(&fd_master, &fd_slave, NULL, NULL, NULL);
  1068. terminal = fdopen(fd_slave, "w");
  1069. struct winsize w;
  1070. w.ws_row = term_height;
  1071. w.ws_col = term_width;
  1072. w.ws_xpixel = 0;
  1073. w.ws_ypixel = 0;
  1074. ioctl(fd_master, TIOCSWINSZ, &w);
  1075. reinit(0);
  1076. fflush(stdin);
  1077. system("cursor-off"); /* Might GPF */
  1078. int pid = getpid();
  1079. uint32_t f = fork();
  1080. if (getpid() != pid) {
  1081. setsid();
  1082. dup2(fd_slave, 0);
  1083. dup2(fd_slave, 1);
  1084. dup2(fd_slave, 2);
  1085. if (argv[optind] != NULL) {
  1086. char * tokens[] = {argv[optind], NULL};
  1087. execvp(tokens[0], tokens);
  1088. fprintf(stderr, "Failed to launch requested startup application.\n");
  1089. } else {
  1090. if (_login_shell) {
  1091. char * tokens[] = {"/bin/login-loop",NULL};
  1092. execvp(tokens[0], tokens);
  1093. exit(1);
  1094. } else {
  1095. char * shell = getenv("SHELL");
  1096. if (!shell) shell = "/bin/sh"; /* fallback */
  1097. char * tokens[] = {shell,NULL};
  1098. execvp(tokens[0], tokens);
  1099. exit(1);
  1100. }
  1101. }
  1102. exit_application = 1;
  1103. return 1;
  1104. } else {
  1105. child_pid = f;
  1106. int kfd = open("/dev/kbd", O_RDONLY);
  1107. key_event_t event;
  1108. char c;
  1109. int vmmouse = 0;
  1110. mouse_device_packet_t packet;
  1111. int mfd = open("/dev/mouse", O_RDONLY);
  1112. int amfd = open("/dev/absmouse", O_RDONLY);
  1113. if (amfd == -1) {
  1114. amfd = open("/dev/vmmouse", O_RDONLY);
  1115. vmmouse = 1;
  1116. }
  1117. key_event_state_t kbd_state = {0};
  1118. /* Prune any keyboard input we got before the terminal started. */
  1119. struct stat s;
  1120. fstat(kfd, &s);
  1121. for (int i = 0; i < s.st_size; i++) {
  1122. char tmp[1];
  1123. read(kfd, tmp, 1);
  1124. }
  1125. int fds[] = {fd_master, kfd, mfd, amfd};
  1126. unsigned char buf[1024];
  1127. while (!exit_application) {
  1128. int index = fswait2(amfd == -1 ? 3 : 4,fds,200);
  1129. check_for_exit();
  1130. if (index == 0) {
  1131. maybe_flip_cursor();
  1132. int r = read(fd_master, buf, 1024);
  1133. for (int i = 0; i < r; ++i) {
  1134. ansi_put(ansi_state, buf[i]);
  1135. }
  1136. } else if (index == 1) {
  1137. maybe_flip_cursor();
  1138. int r = read(kfd, &c, 1);
  1139. if (r > 0) {
  1140. int ret = kbd_scancode(&kbd_state, c, &event);
  1141. key_event(ret, &event);
  1142. }
  1143. } else if (index == 2) {
  1144. /* mouse event */
  1145. int r = read(mfd, (char *)&packet, sizeof(mouse_device_packet_t));
  1146. if (r > 0) {
  1147. last_mouse_buttons = packet.buttons;
  1148. handle_mouse(&packet);
  1149. }
  1150. } else if (amfd != -1 && index == 3) {
  1151. int r = read(amfd, (char *)&packet, sizeof(mouse_device_packet_t));
  1152. if (r > 0) {
  1153. if (!vmmouse) {
  1154. packet.buttons = last_mouse_buttons & 0xF;
  1155. } else {
  1156. last_mouse_buttons = packet.buttons;
  1157. }
  1158. handle_mouse_abs(&packet);
  1159. }
  1160. continue;
  1161. } else {
  1162. maybe_flip_cursor();
  1163. }
  1164. }
  1165. }
  1166. return 0;
  1167. }