terminal-vga.c 33 KB

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