terminal-vga.c 31 KB

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