terminal-vga.c 31 KB

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