terminal.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052
  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) 2013-2018 K. Lange
  5. *
  6. * Terminal Emulator
  7. *
  8. * Graphical terminal emulator.
  9. *
  10. * Provides a number of features:
  11. * - Windowed and full screen modes
  12. * - Antialiased fonts
  13. * - Built-in fallback bitmap font
  14. * - ANSI escape support
  15. * - 256 colors
  16. */
  17. #include <stdio.h>
  18. #include <stdint.h>
  19. #include <syscall.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <signal.h>
  23. #include <time.h>
  24. #include <unistd.h>
  25. #include <fcntl.h>
  26. #include <sys/stat.h>
  27. #include <sys/ioctl.h>
  28. #include <sys/wait.h>
  29. #include <sys/time.h>
  30. #include <getopt.h>
  31. #include <errno.h>
  32. #include <wchar.h>
  33. #define TRACE_APP_NAME "terminal"
  34. #include <toaru/trace.h>
  35. #include <toaru/utf8decode.h>
  36. #include <toaru/yutani.h>
  37. #include <toaru/decorations.h>
  38. #include <toaru/graphics.h>
  39. #include <toaru/kbd.h>
  40. #include <toaru/termemu.h>
  41. #include <toaru/spinlock.h>
  42. #include <toaru/list.h>
  43. #include <toaru/menu.h>
  44. #include <toaru/menubar.h>
  45. #include <toaru/sdf.h>
  46. #include "terminal-palette.h"
  47. #include "terminal-font.h"
  48. #define USE_BELL 0
  49. /* master and slave pty descriptors */
  50. static int fd_master, fd_slave;
  51. static FILE * terminal;
  52. int scale_fonts = 0; /* Whether fonts should be scaled */
  53. float font_scaling = 1.0; /* How much they should be scaled by */
  54. float font_gamma = 1.7; /* Gamma to use for SDF library */
  55. uint16_t term_width = 0; /* Width of the terminal (in cells) */
  56. uint16_t term_height = 0; /* Height of the terminal (in cells) */
  57. uint16_t font_size = 16; /* Font size according to SDF library */
  58. uint16_t char_width = 9; /* Width of a cell in pixels */
  59. uint16_t char_height = 17; /* Height of a cell in pixels */
  60. int csr_x = 0; /* Cursor X */
  61. int csr_y = 0; /* Cursor Y */
  62. term_cell_t * term_buffer = NULL; /* The terminal cell buffer */
  63. uint32_t current_fg = 7; /* Current foreground color */
  64. uint32_t current_bg = 0; /* Current background color */
  65. uint8_t cursor_on = 1; /* Whether or not the cursor should be rendered */
  66. uint8_t _fullscreen = 0; /* Whether or not we are running in fullscreen mode (GUI only) */
  67. uint8_t _no_frame = 0; /* Whether to disable decorations or not */
  68. uint8_t _login_shell = 0; /* Whether we're going to display a login shell or not */
  69. uint8_t _use_sdf = 1; /* Whether or not to use SDF text rendering */
  70. uint8_t _force_kernel = 0;
  71. uint8_t _hold_out = 0; /* state indicator on last cell ignore \n */
  72. uint8_t _free_size = 1; /* Disable rounding when resized */
  73. int menu_bar_height = 24;
  74. int selection = 0;
  75. int selection_start_x = 0;
  76. int selection_start_y = 0;
  77. int selection_end_x = 0;
  78. int selection_end_y = 0;
  79. char * selection_text = NULL;
  80. int last_mouse_x = -1;
  81. int last_mouse_y = -1;
  82. int button_state = 0;
  83. uint64_t mouse_ticks = 0;
  84. struct MenuList * menu_right_click = NULL;
  85. yutani_window_t * window = NULL; /* GUI window */
  86. yutani_t * yctx = NULL;
  87. term_state_t * ansi_state = NULL;
  88. int32_t l_x = INT32_MAX;
  89. int32_t l_y = INT32_MAX;
  90. int32_t r_x = -1;
  91. int32_t r_y = -1;
  92. void reinit(); /* Defined way further down */
  93. void term_redraw_cursor();
  94. /* Some GUI-only options */
  95. uint32_t window_width = 640;
  96. uint32_t window_height = 480;
  97. #define TERMINAL_TITLE_SIZE 512
  98. char terminal_title[TERMINAL_TITLE_SIZE];
  99. size_t terminal_title_length = 0;
  100. gfx_context_t * ctx;
  101. static void render_decors(void);
  102. void term_clear();
  103. void flush_unused_images(void);
  104. void dump_buffer();
  105. /* Trigger to exit the terminal when the child process dies or
  106. * we otherwise receive an exit signal */
  107. volatile int exit_application = 0;
  108. static void cell_redraw(uint16_t x, uint16_t y);
  109. static void cell_redraw_inverted(uint16_t x, uint16_t y);
  110. static uint64_t get_ticks(void) {
  111. struct timeval now;
  112. gettimeofday(&now, NULL);
  113. return (uint64_t)now.tv_sec * 1000000LL + (uint64_t)now.tv_usec;
  114. }
  115. static void display_flip(void) {
  116. if (l_x != INT32_MAX && l_y != INT32_MAX) {
  117. flip(ctx);
  118. yutani_flip_region(yctx, window, l_x, l_y, r_x - l_x, r_y - l_y);
  119. l_x = INT32_MAX;
  120. l_y = INT32_MAX;
  121. r_x = -1;
  122. r_y = -1;
  123. }
  124. }
  125. static void set_term_font_size(float s) {
  126. scale_fonts = 1;
  127. font_scaling = s;
  128. reinit(1);
  129. }
  130. static void set_term_font_gamma(float s) {
  131. font_gamma = s;
  132. reinit(1);
  133. }
  134. static void set_term_font_mode(int i) {
  135. _use_sdf = i;
  136. reinit(1);
  137. }
  138. /* Returns the lower of two shorts */
  139. int32_t min(int32_t a, int32_t b) {
  140. return (a < b) ? a : b;
  141. }
  142. /* Returns the higher of two shorts */
  143. int32_t max(int32_t a, int32_t b) {
  144. return (a > b) ? a : b;
  145. }
  146. void set_title(char * c) {
  147. int len = min(TERMINAL_TITLE_SIZE, strlen(c)+1);
  148. memcpy(terminal_title, c, len);
  149. terminal_title[len-1] = '\0';
  150. terminal_title_length = len - 1;
  151. render_decors();
  152. }
  153. void iterate_selection(void (*func)(uint16_t x, uint16_t y)) {
  154. if (selection_end_y < selection_start_y) {
  155. for (int x = selection_end_x; x < term_width; ++x) {
  156. func(x, selection_end_y);
  157. }
  158. for (int y = selection_end_y + 1; y < selection_start_y; ++y) {
  159. for (int x = 0; x < term_width; ++x) {
  160. func(x, y);
  161. }
  162. }
  163. for (int x = 0; x <= selection_start_x; ++x) {
  164. func(x, selection_start_y);
  165. }
  166. } else if (selection_start_y == selection_end_y) {
  167. if (selection_start_x > selection_end_x) {
  168. for (int x = selection_end_x; x <= selection_start_x; ++x) {
  169. func(x, selection_start_y);
  170. }
  171. } else {
  172. for (int x = selection_start_x; x <= selection_end_x; ++x) {
  173. func(x, selection_start_y);
  174. }
  175. }
  176. } else {
  177. for (int x = selection_start_x; x < term_width; ++x) {
  178. func(x, selection_start_y);
  179. }
  180. for (int y = selection_start_y + 1; y < selection_end_y; ++y) {
  181. for (int x = 0; x < term_width; ++x) {
  182. func(x, y);
  183. }
  184. }
  185. for (int x = 0; x <= selection_end_x; ++x) {
  186. func(x, selection_end_y);
  187. }
  188. }
  189. }
  190. void unredraw_selection(void) {
  191. iterate_selection(cell_redraw);
  192. }
  193. void redraw_selection(void) {
  194. iterate_selection(cell_redraw_inverted);
  195. }
  196. static int _selection_count = 0;
  197. static int _selection_i = 0;
  198. static int to_eight(uint32_t codepoint, char * out) {
  199. memset(out, 0x00, 7);
  200. if (codepoint < 0x0080) {
  201. out[0] = (char)codepoint;
  202. } else if (codepoint < 0x0800) {
  203. out[0] = 0xC0 | (codepoint >> 6);
  204. out[1] = 0x80 | (codepoint & 0x3F);
  205. } else if (codepoint < 0x10000) {
  206. out[0] = 0xE0 | (codepoint >> 12);
  207. out[1] = 0x80 | ((codepoint >> 6) & 0x3F);
  208. out[2] = 0x80 | (codepoint & 0x3F);
  209. } else if (codepoint < 0x200000) {
  210. out[0] = 0xF0 | (codepoint >> 18);
  211. out[1] = 0x80 | ((codepoint >> 12) & 0x3F);
  212. out[2] = 0x80 | ((codepoint >> 6) & 0x3F);
  213. out[3] = 0x80 | ((codepoint) & 0x3F);
  214. } else if (codepoint < 0x4000000) {
  215. out[0] = 0xF8 | (codepoint >> 24);
  216. out[1] = 0x80 | (codepoint >> 18);
  217. out[2] = 0x80 | ((codepoint >> 12) & 0x3F);
  218. out[3] = 0x80 | ((codepoint >> 6) & 0x3F);
  219. out[4] = 0x80 | ((codepoint) & 0x3F);
  220. } else {
  221. out[0] = 0xF8 | (codepoint >> 30);
  222. out[1] = 0x80 | ((codepoint >> 24) & 0x3F);
  223. out[2] = 0x80 | ((codepoint >> 18) & 0x3F);
  224. out[3] = 0x80 | ((codepoint >> 12) & 0x3F);
  225. out[4] = 0x80 | ((codepoint >> 6) & 0x3F);
  226. out[5] = 0x80 | ((codepoint) & 0x3F);
  227. }
  228. return strlen(out);
  229. }
  230. void count_selection(uint16_t x, uint16_t y) {
  231. term_cell_t * cell = (term_cell_t *)((uintptr_t)term_buffer + (y * term_width + x) * sizeof(term_cell_t));
  232. if (!(cell->flags & ANSI_EXT_IMG)) {
  233. if (((uint32_t *)cell)[0] != 0x00000000) {
  234. char tmp[7];
  235. _selection_count += to_eight(cell->c, tmp);
  236. }
  237. }
  238. if (x == term_width - 1) {
  239. _selection_count++;
  240. }
  241. }
  242. void write_selection(uint16_t x, uint16_t y) {
  243. term_cell_t * cell = (term_cell_t *)((uintptr_t)term_buffer + (y * term_width + x) * sizeof(term_cell_t));
  244. if (!(cell->flags & ANSI_EXT_IMG)) {
  245. if (((uint32_t *)cell)[0] != 0x00000000) {
  246. char tmp[7];
  247. int count = to_eight(cell->c, tmp);
  248. for (int i = 0; i < count; ++i) {
  249. selection_text[_selection_i] = tmp[i];
  250. _selection_i++;
  251. }
  252. }
  253. }
  254. if (x == term_width - 1) {
  255. selection_text[_selection_i] = '\n';;
  256. _selection_i++;
  257. }
  258. }
  259. char * copy_selection(void) {
  260. _selection_count = 0;
  261. iterate_selection(count_selection);
  262. fprintf(stderr, "Selection length is %d\n", _selection_count);
  263. if (selection_text) {
  264. free(selection_text);
  265. }
  266. if (_selection_count == 0) {
  267. return NULL;
  268. }
  269. selection_text = malloc(_selection_count + 1);
  270. selection_text[_selection_count] = '\0';
  271. _selection_i = 0;
  272. iterate_selection(write_selection);
  273. if (selection_text[_selection_count-1] == '\n') {
  274. /* Don't end on a line feed */
  275. selection_text[_selection_count-1] = '\0';
  276. }
  277. yutani_set_clipboard(yctx, selection_text);
  278. return selection_text;
  279. }
  280. /* Stuffs a string into the stdin of the terminal's child process
  281. * Useful for things like the ANSI DSR command. */
  282. void input_buffer_stuff(char * str) {
  283. size_t s = strlen(str) + 1;
  284. write(fd_master, str, s);
  285. }
  286. struct menu_bar terminal_menu_bar = {0};
  287. struct menu_bar_entries terminal_menu_entries[] = {
  288. {"File", "file"},
  289. {"Edit", "edit"},
  290. {"View", "view"},
  291. {"Help", "help"},
  292. {NULL, NULL},
  293. };
  294. static void render_decors(void) {
  295. /* XXX Make the decorations library support Yutani windows */
  296. if (_fullscreen) return;
  297. if (!_no_frame) {
  298. render_decorations(window, ctx, terminal_title_length ? terminal_title : "Terminal");
  299. terminal_menu_bar.x = decor_left_width;
  300. terminal_menu_bar.y = decor_top_height;
  301. terminal_menu_bar.width = window_width;
  302. menu_bar_render(&terminal_menu_bar, ctx);
  303. }
  304. yutani_window_advertise_icon(yctx, window, terminal_title_length ? terminal_title : "Terminal", "utilities-terminal");
  305. l_x = 0; l_y = 0;
  306. r_x = window->width;
  307. r_y = window->height;
  308. display_flip();
  309. }
  310. static inline void term_set_point(uint16_t x, uint16_t y, uint32_t color ) {
  311. if (_fullscreen) {
  312. color = alpha_blend_rgba(premultiply(rgba(0,0,0,0xFF)), color);
  313. }
  314. if (!_no_frame) {
  315. GFX(ctx, (x+decor_left_width),(y+decor_top_height+menu_bar_height)) = color;
  316. } else {
  317. GFX(ctx, x,y) = color;
  318. }
  319. }
  320. void draw_semi_block(int c, int x, int y, uint32_t fg, uint32_t bg) {
  321. int height;
  322. bg = premultiply(bg);
  323. fg = premultiply(fg);
  324. if (c == 0x2580) {
  325. uint32_t t = bg;
  326. bg = fg;
  327. fg = t;
  328. c = 0x2584;
  329. for (uint8_t i = 0; i < char_height; ++i) {
  330. for (uint8_t j = 0; j < char_width; ++j) {
  331. term_set_point(x+j,y+i,bg);
  332. }
  333. }
  334. }
  335. c -= 0x2580;
  336. height = char_height - ((c * char_height) / 8);
  337. for (uint8_t i = height; i < char_height; ++i) {
  338. for (uint8_t j = 0; j < char_width; ++j) {
  339. term_set_point(x+j, y+i,fg);
  340. }
  341. }
  342. }
  343. uint32_t ununicode(uint32_t c) {
  344. switch (c) {
  345. case L'☺': return 1;
  346. case L'☻': return 2;
  347. case L'♥': return 3;
  348. case L'♦': return 4;
  349. case L'♣': return 5;
  350. case L'♠': return 6;
  351. case L'•': return 7;
  352. case L'◘': return 8;
  353. case L'○': return 9;
  354. case L'◙': return 10;
  355. case L'♂': return 11;
  356. case L'♀': return 12;
  357. case L'♪': return 13;
  358. case L'♫': return 14;
  359. case L'☼': return 15;
  360. case L'►': return 16;
  361. case L'◄': return 17;
  362. case L'↕': return 18;
  363. case L'‼': return 19;
  364. case L'¶': return 20;
  365. case L'§': return 21;
  366. case L'▬': return 22;
  367. case L'↨': return 23;
  368. case L'↑': return 24;
  369. case L'↓': return 25;
  370. case L'→': return 26;
  371. case L'←': return 27;
  372. case L'∟': return 28;
  373. case L'↔': return 29;
  374. case L'▲': return 30;
  375. case L'▼': return 31;
  376. /* ASCII text */
  377. case L'⌂': return 127;
  378. case L'Ç': return 128;
  379. case L'ü': return 129;
  380. case L'é': return 130;
  381. case L'â': return 131;
  382. case L'ä': return 132;
  383. case L'à': return 133;
  384. case L'å': return 134;
  385. case L'ç': return 135;
  386. case L'ê': return 136;
  387. case L'ë': return 137;
  388. case L'è': return 138;
  389. case L'ï': return 139;
  390. case L'î': return 140;
  391. case L'ì': return 141;
  392. case L'Ä': return 142;
  393. case L'Å': return 143;
  394. case L'É': return 144;
  395. case L'æ': return 145;
  396. case L'Æ': return 146;
  397. case L'ô': return 147;
  398. case L'ö': return 148;
  399. case L'ò': return 149;
  400. case L'û': return 150;
  401. case L'ù': return 151;
  402. case L'ÿ': return 152;
  403. case L'Ö': return 153;
  404. case L'Ü': return 154;
  405. case L'¢': return 155;
  406. case L'£': return 156;
  407. case L'¥': return 157;
  408. case L'₧': return 158;
  409. case L'ƒ': return 159;
  410. case L'á': return 160;
  411. case L'í': return 161;
  412. case L'ó': return 162;
  413. case L'ú': return 163;
  414. case L'ñ': return 164;
  415. case L'Ñ': return 165;
  416. case L'ª': return 166;
  417. case L'º': return 167;
  418. case L'¿': return 168;
  419. case L'⌐': return 169;
  420. case L'¬': return 170;
  421. case L'½': return 171;
  422. case L'¼': return 172;
  423. case L'¡': return 173;
  424. case L'«': return 174;
  425. case L'»': return 175;
  426. case L'░': return 176;
  427. case L'▒': return 177;
  428. case L'▓': return 178;
  429. case L'│': return 179;
  430. case L'┤': return 180;
  431. case L'╡': return 181;
  432. case L'╢': return 182;
  433. case L'╖': return 183;
  434. case L'╕': return 184;
  435. case L'╣': return 185;
  436. case L'║': return 186;
  437. case L'╗': return 187;
  438. case L'╝': return 188;
  439. case L'╜': return 189;
  440. case L'╛': return 190;
  441. case L'┐': return 191;
  442. case L'└': return 192;
  443. case L'┴': return 193;
  444. case L'┬': return 194;
  445. case L'├': return 195;
  446. case L'─': return 196;
  447. case L'┼': return 197;
  448. case L'╞': return 198;
  449. case L'╟': return 199;
  450. case L'╚': return 200;
  451. case L'╔': return 201;
  452. case L'╩': return 202;
  453. case L'╦': return 203;
  454. case L'╠': return 204;
  455. case L'═': return 205;
  456. case L'╬': return 206;
  457. case L'╧': return 207;
  458. case L'╨': return 208;
  459. case L'╤': return 209;
  460. case L'╥': return 210;
  461. case L'╙': return 211;
  462. case L'╘': return 212;
  463. case L'╒': return 213;
  464. case L'╓': return 214;
  465. case L'╫': return 215;
  466. case L'╪': return 216;
  467. case L'┘': return 217;
  468. case L'┌': return 218;
  469. case L'█': return 219;
  470. case L'▄': return 220;
  471. case L'▌': return 221;
  472. case L'▐': return 222;
  473. case L'▀': return 223;
  474. case L'α': return 224;
  475. case L'ß': return 225;
  476. case L'Γ': return 226;
  477. case L'π': return 227;
  478. case L'Σ': return 228;
  479. case L'σ': return 229;
  480. case L'µ': return 230;
  481. case L'τ': return 231;
  482. case L'Φ': return 232;
  483. case L'Θ': return 233;
  484. case L'Ω': return 234;
  485. case L'δ': return 235;
  486. case L'∞': return 236;
  487. case L'φ': return 237;
  488. case L'ε': return 238;
  489. case L'∩': return 239;
  490. case L'≡': return 240;
  491. case L'±': return 241;
  492. case L'≥': return 242;
  493. case L'≤': return 243;
  494. case L'⌠': return 244;
  495. case L'⌡': return 245;
  496. case L'÷': return 246;
  497. case L'≈': return 247;
  498. case L'°': return 248;
  499. case L'∙': return 249;
  500. case L'·': return 250;
  501. case L'√': return 251;
  502. case L'ⁿ': return 252;
  503. case L'²': return 253;
  504. case L'■': return 254;
  505. }
  506. return 4;
  507. }
  508. void
  509. term_write_char(
  510. uint32_t val,
  511. uint16_t x,
  512. uint16_t y,
  513. uint32_t fg,
  514. uint32_t bg,
  515. uint8_t flags
  516. ) {
  517. uint32_t _fg, _bg;
  518. if (fg < PALETTE_COLORS) {
  519. _fg = term_colors[fg];
  520. _fg |= 0xFF << 24;
  521. } else {
  522. _fg = fg;
  523. }
  524. if (bg < PALETTE_COLORS) {
  525. _bg = term_colors[bg];
  526. if (flags & ANSI_SPECBG) {
  527. _bg |= 0xFF << 24;
  528. } else {
  529. _bg |= TERM_DEFAULT_OPAC << 24;
  530. }
  531. } else {
  532. _bg = bg;
  533. }
  534. if (val >= 0x2580 && val <= 0x2588) {
  535. for (uint8_t i = 0; i < char_height; ++i) {
  536. for (uint8_t j = 0; j < char_width; ++j) {
  537. term_set_point(x+j,y+i,premultiply(_bg));
  538. }
  539. }
  540. draw_semi_block(val, x, y, _fg, _bg);
  541. goto _extra_stuff;
  542. }
  543. if (val > 128) {
  544. val = ununicode(val);
  545. }
  546. if (_use_sdf) {
  547. char tmp[2] = {val,0};
  548. for (uint8_t i = 0; i < char_height; ++i) {
  549. for (uint8_t j = 0; j < char_width; ++j) {
  550. term_set_point(x+j,y+i,_bg);
  551. }
  552. }
  553. if (val != 0 && val != ' ' && _fg != _bg) {
  554. int _font = SDF_FONT_MONO;
  555. if (flags & ANSI_BOLD && flags & ANSI_ITALIC) {
  556. _font = SDF_FONT_MONO_BOLD_OBLIQUE;
  557. } else if (flags & ANSI_BOLD) {
  558. _font = SDF_FONT_MONO_BOLD;
  559. } else if (flags & ANSI_ITALIC) {
  560. _font = SDF_FONT_MONO_OBLIQUE;
  561. }
  562. if (_no_frame) {
  563. draw_sdf_string_gamma(ctx, x-1, y, tmp, font_size, _fg, _font, font_gamma);
  564. } else {
  565. draw_sdf_string_gamma(ctx, x+decor_left_width-1, y+decor_top_height+menu_bar_height, tmp, font_size, _fg, _font, font_gamma);
  566. }
  567. }
  568. } else {
  569. #ifdef number_font
  570. uint8_t * c = number_font[val];
  571. for (uint8_t i = 0; i < char_height; ++i) {
  572. for (uint8_t j = 0; j < char_width; ++j) {
  573. if (c[i] & (1 << (8-j))) {
  574. term_set_point(x+j,y+i,_fg);
  575. } else {
  576. term_set_point(x+j,y+i,_bg);
  577. }
  578. }
  579. }
  580. #else
  581. uint16_t * c = large_font[val];
  582. for (uint8_t i = 0; i < char_height; ++i) {
  583. for (uint8_t j = 0; j < char_width; ++j) {
  584. if (c[i] & (1 << (15-j))) {
  585. term_set_point(x+j,y+i,_fg);
  586. } else {
  587. term_set_point(x+j,y+i,_bg);
  588. }
  589. }
  590. }
  591. #endif
  592. }
  593. _extra_stuff:
  594. if (flags & ANSI_UNDERLINE) {
  595. for (uint8_t i = 0; i < char_width; ++i) {
  596. term_set_point(x + i, y + char_height - 1, _fg);
  597. }
  598. }
  599. if (flags & ANSI_CROSS) {
  600. for (uint8_t i = 0; i < char_width; ++i) {
  601. term_set_point(x + i, y + char_height - 7, _fg);
  602. }
  603. }
  604. if (flags & ANSI_BORDER) {
  605. for (uint8_t i = 0; i < char_height; ++i) {
  606. term_set_point(x , y + i, _fg);
  607. term_set_point(x + (char_width - 1), y + i, _fg);
  608. }
  609. for (uint8_t j = 0; j < char_width; ++j) {
  610. term_set_point(x + j, y, _fg);
  611. term_set_point(x + j, y + (char_height - 1), _fg);
  612. }
  613. }
  614. if (!_no_frame) {
  615. l_x = min(l_x, decor_left_width + x);
  616. l_y = min(l_y, decor_top_height+menu_bar_height + y);
  617. if (flags & ANSI_WIDE) {
  618. r_x = max(r_x, decor_left_width + x + char_width * 2);
  619. r_y = max(r_y, decor_top_height+menu_bar_height + y + char_height * 2);
  620. } else {
  621. r_x = max(r_x, decor_left_width + x + char_width);
  622. r_y = max(r_y, decor_top_height+menu_bar_height + y + char_height);
  623. }
  624. } else {
  625. l_x = min(l_x, x);
  626. l_y = min(l_y, y);
  627. if (flags & ANSI_WIDE) {
  628. r_x = max(r_x, x + char_width * 2);
  629. r_y = max(r_y, y + char_height * 2);
  630. } else {
  631. r_x = max(r_x, x + char_width);
  632. r_y = max(r_y, y + char_height);
  633. }
  634. }
  635. }
  636. static void cell_set(uint16_t x, uint16_t y, uint32_t c, uint32_t fg, uint32_t bg, uint32_t flags) {
  637. if (x >= term_width || y >= term_height) return;
  638. term_cell_t * cell = (term_cell_t *)((uintptr_t)term_buffer + (y * term_width + x) * sizeof(term_cell_t));
  639. cell->c = c;
  640. cell->fg = fg;
  641. cell->bg = bg;
  642. cell->flags = flags;
  643. }
  644. static void redraw_cell_image(uint16_t x, uint16_t y, term_cell_t * cell) {
  645. if (x >= term_width || y >= term_height) return;
  646. uint32_t * data = (uint32_t *)cell->fg;
  647. for (uint32_t yy = 0; yy < char_height; ++yy) {
  648. for (uint32_t xx = 0; xx < char_width; ++xx) {
  649. term_set_point(x * char_width + xx, y * char_height + yy, *data);
  650. data++;
  651. }
  652. }
  653. if (!_no_frame) {
  654. l_x = min(l_x, decor_left_width + x * char_width);
  655. l_y = min(l_y, decor_top_height+menu_bar_height + y * char_height);
  656. r_x = max(r_x, decor_left_width + x * char_width + char_width);
  657. r_y = max(r_y, decor_top_height+menu_bar_height + y * char_height + char_height);
  658. } else {
  659. l_x = min(l_x, x * char_width);
  660. l_y = min(l_y, y * char_height);
  661. r_x = max(r_x, x * char_width + char_width);
  662. r_y = max(r_y, y * char_height + char_height);
  663. }
  664. }
  665. static void cell_redraw(uint16_t x, uint16_t y) {
  666. if (x >= term_width || y >= term_height) return;
  667. term_cell_t * cell = (term_cell_t *)((uintptr_t)term_buffer + (y * term_width + x) * sizeof(term_cell_t));
  668. if (cell->flags & ANSI_EXT_IMG) { redraw_cell_image(x,y,cell); return; }
  669. if (((uint32_t *)cell)[0] == 0x00000000) {
  670. term_write_char(' ', x * char_width, y * char_height, TERM_DEFAULT_FG, TERM_DEFAULT_BG, TERM_DEFAULT_FLAGS);
  671. } else {
  672. term_write_char(cell->c, x * char_width, y * char_height, cell->fg, cell->bg, cell->flags);
  673. }
  674. }
  675. static void cell_redraw_inverted(uint16_t x, uint16_t y) {
  676. if (x >= term_width || y >= term_height) return;
  677. term_cell_t * cell = (term_cell_t *)((uintptr_t)term_buffer + (y * term_width + x) * sizeof(term_cell_t));
  678. if (cell->flags & ANSI_EXT_IMG) { redraw_cell_image(x,y,cell); return; }
  679. if (((uint32_t *)cell)[0] == 0x00000000) {
  680. term_write_char(' ', x * char_width, y * char_height, TERM_DEFAULT_BG, TERM_DEFAULT_FG, TERM_DEFAULT_FLAGS | ANSI_SPECBG);
  681. } else {
  682. term_write_char(cell->c, x * char_width, y * char_height, cell->bg, cell->fg, cell->flags | ANSI_SPECBG);
  683. }
  684. }
  685. static void cell_redraw_box(uint16_t x, uint16_t y) {
  686. if (x >= term_width || y >= term_height) return;
  687. term_cell_t * cell = (term_cell_t *)((uintptr_t)term_buffer + (y * term_width + x) * sizeof(term_cell_t));
  688. if (cell->flags & ANSI_EXT_IMG) { redraw_cell_image(x,y,cell); return; }
  689. if (((uint32_t *)cell)[0] == 0x00000000) {
  690. term_write_char(' ', x * char_width, y * char_height, TERM_DEFAULT_FG, TERM_DEFAULT_BG, TERM_DEFAULT_FLAGS | ANSI_BORDER);
  691. } else {
  692. term_write_char(cell->c, x * char_width, y * char_height, cell->fg, cell->bg, cell->flags | ANSI_BORDER);
  693. }
  694. }
  695. void render_cursor() {
  696. if (!window->focused) {
  697. cell_redraw_box(csr_x, csr_y);
  698. } else {
  699. cell_redraw_inverted(csr_x, csr_y);
  700. }
  701. }
  702. void draw_cursor() {
  703. if (!cursor_on) return;
  704. mouse_ticks = get_ticks();
  705. render_cursor();
  706. }
  707. void term_redraw_all() {
  708. for (int i = 0; i < term_height; i++) {
  709. for (int x = 0; x < term_width; ++x) {
  710. term_cell_t * cell = (term_cell_t *)((uintptr_t)term_buffer + (i * term_width + x) * sizeof(term_cell_t));
  711. if (cell->flags & ANSI_EXT_IMG) { redraw_cell_image(x,i,cell); continue; }
  712. if (((uint32_t *)cell)[0] == 0x00000000) {
  713. term_write_char(' ', x * char_width, i * char_height, TERM_DEFAULT_FG, TERM_DEFAULT_BG, TERM_DEFAULT_FLAGS);
  714. } else {
  715. term_write_char(cell->c, x * char_width, i * char_height, cell->fg, cell->bg, cell->flags);
  716. }
  717. }
  718. }
  719. }
  720. void term_scroll(int how_much) {
  721. if (how_much >= term_height || -how_much >= term_height) {
  722. term_clear();
  723. return;
  724. }
  725. if (how_much == 0) {
  726. return;
  727. }
  728. cell_redraw(csr_x, csr_y);
  729. if (how_much > 0) {
  730. /* Shift terminal cells one row up */
  731. memmove(term_buffer, (void *)((uintptr_t)term_buffer + sizeof(term_cell_t) * term_width), sizeof(term_cell_t) * term_width * (term_height - how_much));
  732. /* Reset the "new" row to clean cells */
  733. 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);
  734. /* In graphical modes, we will shift the graphics buffer up as necessary */
  735. uintptr_t dst, src;
  736. size_t siz = char_height * (term_height - how_much) * GFX_W(ctx) * GFX_B(ctx);
  737. if (!_no_frame) {
  738. /* Must include decorations */
  739. dst = (uintptr_t)ctx->backbuffer + (GFX_W(ctx) * (decor_top_height+menu_bar_height)) * GFX_B(ctx);
  740. src = (uintptr_t)ctx->backbuffer + (GFX_W(ctx) * (decor_top_height+menu_bar_height + char_height * how_much)) * GFX_B(ctx);
  741. } else {
  742. /* Can skip decorations */
  743. dst = (uintptr_t)ctx->backbuffer;
  744. src = (uintptr_t)ctx->backbuffer + (GFX_W(ctx) * char_height * how_much) * GFX_B(ctx);
  745. }
  746. /* Perform the shift */
  747. memmove((void *)dst, (void *)src, siz);
  748. /* And redraw the new rows */
  749. for (int i = 0; i < how_much; ++i) {
  750. for (uint16_t x = 0; x < term_width; ++x) {
  751. cell_set(x,term_height - how_much,' ', current_fg, current_bg, ansi_state->flags);
  752. cell_redraw(x, term_height - how_much);
  753. }
  754. }
  755. } else {
  756. how_much = -how_much;
  757. /* Shift terminal cells one row up */
  758. memmove((void *)((uintptr_t)term_buffer + sizeof(term_cell_t) * term_width), term_buffer, sizeof(term_cell_t) * term_width * (term_height - how_much));
  759. /* Reset the "new" row to clean cells */
  760. memset(term_buffer, 0x0, sizeof(term_cell_t) * term_width * how_much);
  761. uintptr_t dst, src;
  762. size_t siz = char_height * (term_height - how_much) * GFX_W(ctx) * GFX_B(ctx);
  763. if (!_no_frame) {
  764. src = (uintptr_t)ctx->backbuffer + (GFX_W(ctx) * (decor_top_height+menu_bar_height)) * GFX_B(ctx);
  765. dst = (uintptr_t)ctx->backbuffer + (GFX_W(ctx) * (decor_top_height+menu_bar_height + char_height * how_much)) * GFX_B(ctx);
  766. } else {
  767. src = (uintptr_t)ctx->backbuffer;
  768. dst = (uintptr_t)ctx->backbuffer + (GFX_W(ctx) * char_height * how_much) * GFX_B(ctx);
  769. }
  770. /* Perform the shift */
  771. memmove((void *)dst, (void *)src, siz);
  772. /* And redraw the new rows */
  773. for (int i = 0; i < how_much; ++i) {
  774. for (uint16_t x = 0; x < term_width; ++x) {
  775. cell_redraw(x, i);
  776. }
  777. }
  778. }
  779. flush_unused_images();
  780. yutani_flip(yctx, window);
  781. }
  782. int is_wide(uint32_t codepoint) {
  783. if (codepoint < 256) return 0;
  784. return wcwidth(codepoint) == 2;
  785. }
  786. struct scrollback_row {
  787. unsigned short width;
  788. term_cell_t cells[];
  789. };
  790. #define MAX_SCROLLBACK 10240
  791. list_t * scrollback_list = NULL;
  792. int scrollback_offset = 0;
  793. void save_scrollback() {
  794. /* Save the current top row for scrollback */
  795. if (!scrollback_list) {
  796. scrollback_list = list_create();
  797. }
  798. if (scrollback_list->length == MAX_SCROLLBACK) {
  799. free(list_dequeue(scrollback_list));
  800. }
  801. struct scrollback_row * row = malloc(sizeof(struct scrollback_row) + sizeof(term_cell_t) * term_width + 20);
  802. row->width = term_width;
  803. for (int i = 0; i < term_width; ++i) {
  804. term_cell_t * cell = (term_cell_t *)((uintptr_t)term_buffer + (i) * sizeof(term_cell_t));
  805. memcpy(&row->cells[i], cell, sizeof(term_cell_t));
  806. }
  807. list_insert(scrollback_list, row);
  808. }
  809. void redraw_scrollback() {
  810. if (!scrollback_offset) {
  811. term_redraw_all();
  812. display_flip();
  813. return;
  814. }
  815. if (scrollback_offset < term_height) {
  816. for (int i = scrollback_offset; i < term_height; i++) {
  817. int y = i - scrollback_offset;
  818. for (int x = 0; x < term_width; ++x) {
  819. term_cell_t * cell = (term_cell_t *)((uintptr_t)term_buffer + (y * term_width + x) * sizeof(term_cell_t));
  820. if (cell->flags & ANSI_EXT_IMG) { redraw_cell_image(x,i,cell); continue; }
  821. if (((uint32_t *)cell)[0] == 0x00000000) {
  822. term_write_char(' ', x * char_width, i * char_height, TERM_DEFAULT_FG, TERM_DEFAULT_BG, TERM_DEFAULT_FLAGS);
  823. } else {
  824. term_write_char(cell->c, x * char_width, i * char_height, cell->fg, cell->bg, cell->flags);
  825. }
  826. }
  827. }
  828. node_t * node = scrollback_list->tail;
  829. for (int i = 0; i < scrollback_offset; ++i) {
  830. struct scrollback_row * row = (struct scrollback_row *)node->value;
  831. int y = scrollback_offset - 1 - i;
  832. int width = row->width;
  833. if (width > term_width) {
  834. width = term_width;
  835. } else {
  836. for (int x = row->width; x < term_width; ++x) {
  837. term_write_char(' ', x * char_width, y * char_height, TERM_DEFAULT_FG, TERM_DEFAULT_BG, TERM_DEFAULT_FLAGS);
  838. }
  839. }
  840. for (int x = 0; x < width; ++x) {
  841. term_cell_t * cell = &row->cells[x];
  842. if (((uint32_t *)cell)[0] == 0x00000000) {
  843. term_write_char(' ', x * char_width, y * char_height, TERM_DEFAULT_FG, TERM_DEFAULT_BG, TERM_DEFAULT_FLAGS);
  844. } else {
  845. term_write_char(cell->c, x * char_width, y * char_height, cell->fg, cell->bg, cell->flags);
  846. }
  847. }
  848. node = node->prev;
  849. }
  850. } else {
  851. node_t * node = scrollback_list->tail;
  852. for (int i = 0; i < scrollback_offset - term_height; ++i) {
  853. node = node->prev;
  854. }
  855. for (int i = scrollback_offset - term_height; i < scrollback_offset; ++i) {
  856. struct scrollback_row * row = (struct scrollback_row *)node->value;
  857. int y = scrollback_offset - 1 - i;
  858. int width = row->width;
  859. if (width > term_width) {
  860. width = term_width;
  861. } else {
  862. for (int x = row->width; x < term_width; ++x) {
  863. term_write_char(' ', x * char_width, y * char_height, TERM_DEFAULT_FG, TERM_DEFAULT_BG, TERM_DEFAULT_FLAGS);
  864. }
  865. }
  866. for (int x = 0; x < width; ++x) {
  867. term_cell_t * cell = &row->cells[x];
  868. if (((uint32_t *)cell)[0] == 0x00000000) {
  869. term_write_char(' ', x * char_width, y * char_height, TERM_DEFAULT_FG, TERM_DEFAULT_BG, TERM_DEFAULT_FLAGS);
  870. } else {
  871. term_write_char(cell->c, x * char_width, y * char_height, cell->fg, cell->bg, cell->flags);
  872. }
  873. }
  874. node = node->prev;
  875. }
  876. }
  877. display_flip();
  878. }
  879. void term_write(char c) {
  880. static uint32_t unicode_state = 0;
  881. static uint32_t codepoint = 0;
  882. cell_redraw(csr_x, csr_y);
  883. if (!decode(&unicode_state, &codepoint, (uint8_t)c)) {
  884. uint32_t o = codepoint;
  885. codepoint = 0;
  886. if (c == '\r') {
  887. csr_x = 0;
  888. return;
  889. }
  890. if (csr_x < 0) csr_x = 0;
  891. if (csr_y < 0) csr_y = 0;
  892. if (csr_x == term_width) {
  893. csr_x = 0;
  894. ++csr_y;
  895. }
  896. if (csr_y == term_height) {
  897. save_scrollback();
  898. term_scroll(1);
  899. csr_y = term_height - 1;
  900. }
  901. if (c == '\n') {
  902. if (csr_x == 0 && _hold_out) {
  903. _hold_out = 0;
  904. return;
  905. }
  906. ++csr_y;
  907. if (csr_y == term_height) {
  908. save_scrollback();
  909. term_scroll(1);
  910. csr_y = term_height - 1;
  911. }
  912. draw_cursor();
  913. } else if (c == '\007') {
  914. /* bell */
  915. #if USE_BELL
  916. for (int i = 0; i < term_height; ++i) {
  917. for (int j = 0; j < term_width; ++j) {
  918. cell_redraw_inverted(j, i);
  919. }
  920. }
  921. syscall_nanosleep(0,10);
  922. term_redraw_all();
  923. #endif
  924. } else if (c == '\b') {
  925. if (csr_x > 0) {
  926. --csr_x;
  927. }
  928. cell_redraw(csr_x, csr_y);
  929. draw_cursor();
  930. } else if (c == '\t') {
  931. csr_x += (8 - csr_x % 8);
  932. draw_cursor();
  933. } else {
  934. int wide = is_wide(o);
  935. uint8_t flags = ansi_state->flags;
  936. if (wide && csr_x == term_width - 1) {
  937. csr_x = 0;
  938. ++csr_y;
  939. }
  940. if (wide) {
  941. flags = flags | ANSI_WIDE;
  942. }
  943. cell_set(csr_x,csr_y, o, current_fg, current_bg, flags);
  944. cell_redraw(csr_x,csr_y);
  945. csr_x++;
  946. if (wide && csr_x != term_width) {
  947. cell_set(csr_x, csr_y, 0xFFFF, current_fg, current_bg, ansi_state->flags);
  948. cell_redraw(csr_x,csr_y);
  949. cell_redraw(csr_x-1,csr_y);
  950. csr_x++;
  951. }
  952. }
  953. } else if (unicode_state == UTF8_REJECT) {
  954. unicode_state = 0;
  955. codepoint = 0;
  956. }
  957. draw_cursor();
  958. }
  959. void term_set_csr(int x, int y) {
  960. cell_redraw(csr_x,csr_y);
  961. csr_x = x;
  962. csr_y = y;
  963. draw_cursor();
  964. }
  965. int term_get_csr_x(void) {
  966. return csr_x;
  967. }
  968. int term_get_csr_y(void) {
  969. return csr_y;
  970. }
  971. static list_t * images_list = NULL;
  972. void term_set_cell_contents(int x, int y, char * data) {
  973. if (!images_list) {
  974. images_list = list_create();
  975. }
  976. char * cell_data = malloc(char_width * char_height * sizeof(uint32_t));
  977. memcpy(cell_data, data, char_width * char_height * sizeof(uint32_t));
  978. list_insert(images_list, cell_data);
  979. cell_set(x, y, ' ', (uint32_t)cell_data, 0, ANSI_EXT_IMG);
  980. return;
  981. }
  982. void flush_unused_images(void) {
  983. if (!images_list) return;
  984. list_t * tmp = list_create();
  985. for (int y = 0; y < term_height; ++y) {
  986. for (int x = 0; x < term_width; ++x) {
  987. term_cell_t * cell = (term_cell_t *)((uintptr_t)term_buffer + (y * term_width + x) * sizeof(term_cell_t));
  988. if (cell->flags & ANSI_EXT_IMG) {
  989. list_insert(tmp, (void *)cell->fg);
  990. }
  991. }
  992. }
  993. foreach(node, images_list) {
  994. if (!list_find(tmp, node->value)) {
  995. free(node->value);
  996. }
  997. }
  998. list_free(images_list);
  999. images_list = tmp;
  1000. }
  1001. int term_get_cell_width(void) {
  1002. return char_width;
  1003. }
  1004. int term_get_cell_height(void) {
  1005. return char_height;
  1006. }
  1007. void term_set_csr_show(int on) {
  1008. cursor_on = on;
  1009. }
  1010. void term_set_colors(uint32_t fg, uint32_t bg) {
  1011. current_fg = fg;
  1012. current_bg = bg;
  1013. }
  1014. void term_redraw_cursor() {
  1015. if (term_buffer) {
  1016. draw_cursor();
  1017. }
  1018. }
  1019. void flip_cursor() {
  1020. static uint8_t cursor_flipped = 0;
  1021. if (scrollback_offset != 0) {
  1022. return; /* Don't flip cursor while drawing scrollback */
  1023. }
  1024. if (window->focused && cursor_flipped) {
  1025. cell_redraw(csr_x, csr_y);
  1026. } else {
  1027. render_cursor();
  1028. }
  1029. display_flip();
  1030. cursor_flipped = 1 - cursor_flipped;
  1031. }
  1032. void term_set_cell(int x, int y, uint32_t c) {
  1033. cell_set(x, y, c, current_fg, current_bg, ansi_state->flags);
  1034. cell_redraw(x, y);
  1035. }
  1036. void term_redraw_cell(int x, int y) {
  1037. if (x < 0 || y < 0 || x >= term_width || y >= term_height) return;
  1038. cell_redraw(x,y);
  1039. }
  1040. void term_clear(int i) {
  1041. if (i == 2) {
  1042. /* Oh dear */
  1043. csr_x = 0;
  1044. csr_y = 0;
  1045. memset((void *)term_buffer, 0x00, term_width * term_height * sizeof(term_cell_t));
  1046. if (!_no_frame) {
  1047. render_decors();
  1048. }
  1049. term_redraw_all();
  1050. } else if (i == 0) {
  1051. for (int x = csr_x; x < term_width; ++x) {
  1052. term_set_cell(x, csr_y, ' ');
  1053. }
  1054. for (int y = csr_y + 1; y < term_height; ++y) {
  1055. for (int x = 0; x < term_width; ++x) {
  1056. term_set_cell(x, y, ' ');
  1057. }
  1058. }
  1059. } else if (i == 1) {
  1060. for (int y = 0; y < csr_y; ++y) {
  1061. for (int x = 0; x < term_width; ++x) {
  1062. term_set_cell(x, y, ' ');
  1063. }
  1064. }
  1065. for (int x = 0; x < csr_x; ++x) {
  1066. term_set_cell(x, csr_y, ' ');
  1067. }
  1068. }
  1069. flush_unused_images();
  1070. }
  1071. #if 0
  1072. char * loadMemFont(char * name, char * ident, size_t * size) {
  1073. size_t s = 0;
  1074. int error;
  1075. char tmp[100];
  1076. snprintf(tmp, 100, "sys.%s.fonts.%s", yctx->server_ident, ident);
  1077. char * font = (char *)syscall_shm_obtain(tmp, &s);
  1078. *size = s;
  1079. return font;
  1080. }
  1081. #endif
  1082. #define INPUT_SIZE 1024
  1083. char input_buffer[INPUT_SIZE];
  1084. int input_collected = 0;
  1085. void clear_input() {
  1086. memset(input_buffer, 0x0, INPUT_SIZE);
  1087. input_collected = 0;
  1088. }
  1089. pid_t child_pid = 0;
  1090. void handle_input(char c) {
  1091. write(fd_master, &c, 1);
  1092. display_flip();
  1093. }
  1094. void handle_input_s(char * c) {
  1095. write(fd_master, c, strlen(c));
  1096. display_flip();
  1097. }
  1098. void scroll_up(int amount) {
  1099. int i = 0;
  1100. while (i < amount && scrollback_list && scrollback_offset < (int)scrollback_list->length) {
  1101. scrollback_offset ++;
  1102. i++;
  1103. }
  1104. redraw_scrollback();
  1105. }
  1106. void scroll_down(int amount) {
  1107. int i = 0;
  1108. while (i < amount && scrollback_list && scrollback_offset != 0) {
  1109. scrollback_offset -= 1;
  1110. i++;
  1111. }
  1112. redraw_scrollback();
  1113. }
  1114. void key_event(int ret, key_event_t * event) {
  1115. if (ret) {
  1116. /* Special keys */
  1117. if ((event->modifiers & KEY_MOD_LEFT_SHIFT || event->modifiers & KEY_MOD_RIGHT_SHIFT) &&
  1118. (event->modifiers & KEY_MOD_LEFT_CTRL || event->modifiers & KEY_MOD_RIGHT_CTRL) &&
  1119. (event->keycode == 'c')) {
  1120. if (selection) {
  1121. /* Copy selection */
  1122. copy_selection();
  1123. }
  1124. return;
  1125. }
  1126. if ((event->modifiers & KEY_MOD_LEFT_SHIFT || event->modifiers & KEY_MOD_RIGHT_SHIFT) &&
  1127. (event->modifiers & KEY_MOD_LEFT_CTRL || event->modifiers & KEY_MOD_RIGHT_CTRL) &&
  1128. (event->keycode == 'v')) {
  1129. /* Paste selection */
  1130. yutani_special_request(yctx, NULL, YUTANI_SPECIAL_REQUEST_CLIPBOARD);
  1131. #if 0
  1132. if (selection_text) {
  1133. handle_input_s(selection_text);
  1134. }
  1135. #endif
  1136. return;
  1137. }
  1138. if (event->modifiers & KEY_MOD_LEFT_ALT || event->modifiers & KEY_MOD_RIGHT_ALT) {
  1139. handle_input('\033');
  1140. }
  1141. if ((event->modifiers & KEY_MOD_LEFT_SHIFT || event->modifiers & KEY_MOD_RIGHT_SHIFT) &&
  1142. event->key == '\t') {
  1143. handle_input_s("\033[Z");
  1144. return;
  1145. }
  1146. handle_input(event->key);
  1147. } else {
  1148. if (event->action == KEY_ACTION_UP) return;
  1149. switch (event->keycode) {
  1150. case KEY_F1:
  1151. handle_input_s("\033OP");
  1152. break;
  1153. case KEY_F2:
  1154. handle_input_s("\033OQ");
  1155. break;
  1156. case KEY_F3:
  1157. handle_input_s("\033OR");
  1158. break;
  1159. case KEY_F4:
  1160. handle_input_s("\033OS");
  1161. break;
  1162. case KEY_F5:
  1163. handle_input_s("\033[15~");
  1164. break;
  1165. case KEY_F6:
  1166. handle_input_s("\033[17~");
  1167. break;
  1168. case KEY_F7:
  1169. handle_input_s("\033[18~");
  1170. break;
  1171. case KEY_F8:
  1172. handle_input_s("\033[19~");
  1173. break;
  1174. case KEY_F9:
  1175. handle_input_s("\033[20~");
  1176. break;
  1177. case KEY_F10:
  1178. handle_input_s("\033[21~");
  1179. break;
  1180. case KEY_F11:
  1181. handle_input_s("\033[23~");
  1182. break;
  1183. case KEY_F12:
  1184. /* Toggle decorations */
  1185. if (!_fullscreen) {
  1186. _no_frame = !_no_frame;
  1187. window_width = window->width - decor_width() * (!_no_frame);
  1188. window_height = window->height - (decor_height() + menu_bar_height) * (!_no_frame);
  1189. reinit(1);
  1190. }
  1191. break;
  1192. case KEY_ARROW_UP:
  1193. if (event->modifiers & KEY_MOD_LEFT_SHIFT && event->modifiers & KEY_MOD_LEFT_CTRL) {
  1194. handle_input_s("\033[6A");
  1195. } else if (event->modifiers & KEY_MOD_LEFT_CTRL) {
  1196. handle_input_s("\033[5A");
  1197. } else if (event->modifiers & KEY_MOD_LEFT_SHIFT && event->modifiers & KEY_MOD_LEFT_ALT) {
  1198. handle_input_s("\033[4A");
  1199. } else if (event->modifiers & KEY_MOD_LEFT_ALT) {
  1200. handle_input_s("\033[3A");
  1201. } else if (event->modifiers & KEY_MOD_LEFT_SHIFT) {
  1202. handle_input_s("\033[2A");
  1203. } else {
  1204. handle_input_s("\033[A");
  1205. }
  1206. break;
  1207. case KEY_ARROW_DOWN:
  1208. if (event->modifiers & KEY_MOD_LEFT_SHIFT && event->modifiers & KEY_MOD_LEFT_CTRL) {
  1209. handle_input_s("\033[6B");
  1210. } else if (event->modifiers & KEY_MOD_LEFT_CTRL) {
  1211. handle_input_s("\033[5B");
  1212. } else if (event->modifiers & KEY_MOD_LEFT_SHIFT && event->modifiers & KEY_MOD_LEFT_ALT) {
  1213. handle_input_s("\033[4B");
  1214. } else if (event->modifiers & KEY_MOD_LEFT_ALT) {
  1215. handle_input_s("\033[3B");
  1216. } else if (event->modifiers & KEY_MOD_LEFT_SHIFT) {
  1217. handle_input_s("\033[2B");
  1218. } else {
  1219. handle_input_s("\033[B");
  1220. }
  1221. break;
  1222. case KEY_ARROW_RIGHT:
  1223. if (event->modifiers & KEY_MOD_LEFT_SHIFT && event->modifiers & KEY_MOD_LEFT_CTRL) {
  1224. handle_input_s("\033[6C");
  1225. } else if (event->modifiers & KEY_MOD_LEFT_CTRL) {
  1226. handle_input_s("\033[5C");
  1227. } else if (event->modifiers & KEY_MOD_LEFT_SHIFT && event->modifiers & KEY_MOD_LEFT_ALT) {
  1228. handle_input_s("\033[4C");
  1229. } else if (event->modifiers & KEY_MOD_LEFT_ALT) {
  1230. handle_input_s("\033[3C");
  1231. } else if (event->modifiers & KEY_MOD_LEFT_SHIFT) {
  1232. handle_input_s("\033[2C");
  1233. } else {
  1234. handle_input_s("\033[C");
  1235. }
  1236. break;
  1237. case KEY_ARROW_LEFT:
  1238. if (event->modifiers & KEY_MOD_LEFT_SHIFT && event->modifiers & KEY_MOD_LEFT_CTRL) {
  1239. handle_input_s("\033[6D");
  1240. } else if (event->modifiers & KEY_MOD_LEFT_CTRL) {
  1241. handle_input_s("\033[5D");
  1242. } else if (event->modifiers & KEY_MOD_LEFT_SHIFT && event->modifiers & KEY_MOD_LEFT_ALT) {
  1243. handle_input_s("\033[4D");
  1244. } else if (event->modifiers & KEY_MOD_LEFT_ALT) {
  1245. handle_input_s("\033[3D");
  1246. } else if (event->modifiers & KEY_MOD_LEFT_SHIFT) {
  1247. handle_input_s("\033[2D");
  1248. } else {
  1249. handle_input_s("\033[D");
  1250. }
  1251. break;
  1252. case KEY_PAGE_UP:
  1253. if (event->modifiers & KEY_MOD_LEFT_SHIFT) {
  1254. scroll_up(term_height/2);
  1255. } else {
  1256. handle_input_s("\033[5~");
  1257. }
  1258. break;
  1259. case KEY_PAGE_DOWN:
  1260. if (event->modifiers & KEY_MOD_LEFT_SHIFT) {
  1261. scroll_down(term_height/2);
  1262. } else {
  1263. handle_input_s("\033[6~");
  1264. }
  1265. break;
  1266. case KEY_HOME:
  1267. if (event->modifiers & KEY_MOD_LEFT_SHIFT) {
  1268. if (scrollback_list) {
  1269. scrollback_offset = scrollback_list->length;
  1270. redraw_scrollback();
  1271. }
  1272. } else {
  1273. handle_input_s("\033OH");
  1274. }
  1275. break;
  1276. case KEY_END:
  1277. if (event->modifiers & KEY_MOD_LEFT_SHIFT) {
  1278. scrollback_offset = 0;
  1279. redraw_scrollback();
  1280. } else {
  1281. handle_input_s("\033OF");
  1282. }
  1283. break;
  1284. case KEY_DEL:
  1285. handle_input_s("\033[3~");
  1286. break;
  1287. case KEY_INSERT:
  1288. handle_input_s("\033[2~");
  1289. break;
  1290. }
  1291. }
  1292. }
  1293. void check_for_exit(void) {
  1294. if (exit_application) return;
  1295. pid_t pid = waitpid(-1, NULL, WNOHANG);
  1296. if (pid != child_pid) return;
  1297. /* Clean up */
  1298. exit_application = 1;
  1299. /* Exit */
  1300. char exit_message[] = "[Process terminated]\n";
  1301. write(fd_slave, exit_message, sizeof(exit_message));
  1302. }
  1303. void usage(char * argv[]) {
  1304. printf(
  1305. "Terminal Emulator\n"
  1306. "\n"
  1307. "usage: %s [-b] [-F] [-h]\n"
  1308. "\n"
  1309. " -F --fullscreen \033[3mRun in fullscreen (background) mode.\033[0m\n"
  1310. " -b --bitmap \033[3mUse the integrated bitmap font.\033[0m\n"
  1311. " -s --scale \033[3mScale the font in SDF mode by a given amount.\033[0m\n"
  1312. " -h --help \033[3mShow this help message.\033[0m\n"
  1313. " -x --grid \033[3mMake resizes round to nearest match for character cell size.\033[0m\n"
  1314. " -n --no-frame \033[3mDisable decorations.\033[0m\n"
  1315. "\n"
  1316. " This terminal emulator provides basic support for VT220 escapes and\n"
  1317. " XTerm extensions, including 256 color support and font effects.\n",
  1318. argv[0]);
  1319. }
  1320. term_callbacks_t term_callbacks = {
  1321. &term_write,
  1322. term_set_colors,
  1323. term_set_csr,
  1324. term_get_csr_x,
  1325. term_get_csr_y,
  1326. term_set_cell,
  1327. term_clear,
  1328. term_scroll,
  1329. term_redraw_cursor,
  1330. input_buffer_stuff,
  1331. set_term_font_size,
  1332. set_title,
  1333. term_set_cell_contents,
  1334. term_get_cell_width,
  1335. term_get_cell_height,
  1336. term_set_csr_show,
  1337. set_term_font_gamma,
  1338. set_term_font_mode,
  1339. };
  1340. void reinit(int send_sig) {
  1341. if (_use_sdf) {
  1342. char_width = 9;
  1343. char_height = 17;
  1344. font_size = 16;
  1345. if (scale_fonts) {
  1346. font_size *= font_scaling;
  1347. char_height *= font_scaling;
  1348. char_width *= font_scaling;
  1349. }
  1350. } else {
  1351. char_width = 9;
  1352. char_height = 20;
  1353. }
  1354. int old_width = term_width;
  1355. int old_height = term_height;
  1356. term_width = window_width / char_width;
  1357. term_height = window_height / char_height;
  1358. if (term_buffer) {
  1359. term_cell_t * new_term_buffer = malloc(sizeof(term_cell_t) * term_width * term_height);
  1360. memset(new_term_buffer, 0x0, sizeof(term_cell_t) * term_width * term_height);
  1361. int offset = 0;
  1362. if (term_height < old_height) {
  1363. while (csr_y >= term_height) {
  1364. offset++;
  1365. old_height--;
  1366. csr_y--;
  1367. }
  1368. }
  1369. for (int row = 0; row < min(old_height, term_height); ++row) {
  1370. for (int col = 0; col < min(old_width, term_width); ++col) {
  1371. term_cell_t * old_cell = (term_cell_t *)((uintptr_t)term_buffer + ((row + offset) * old_width + col) * sizeof(term_cell_t));
  1372. term_cell_t * new_cell = (term_cell_t *)((uintptr_t)new_term_buffer + (row * term_width + col) * sizeof(term_cell_t));
  1373. *new_cell = *old_cell;
  1374. }
  1375. }
  1376. if (csr_x >= term_width) {
  1377. csr_x = term_width-1;
  1378. }
  1379. free(term_buffer);
  1380. term_buffer = new_term_buffer;
  1381. } else {
  1382. term_buffer = malloc(sizeof(term_cell_t) * term_width * term_height);
  1383. memset(term_buffer, 0x0, sizeof(term_cell_t) * term_width * term_height);
  1384. }
  1385. int old_mouse_state = 0;
  1386. if (ansi_state) old_mouse_state = ansi_state->mouse_on;
  1387. ansi_state = ansi_init(ansi_state, term_width, term_height, &term_callbacks);
  1388. ansi_state->mouse_on = old_mouse_state;
  1389. draw_fill(ctx, rgba(0,0,0, TERM_DEFAULT_OPAC));
  1390. render_decors();
  1391. term_redraw_all();
  1392. display_flip();
  1393. struct winsize w;
  1394. w.ws_row = term_height;
  1395. w.ws_col = term_width;
  1396. w.ws_xpixel = term_width * char_width;
  1397. w.ws_ypixel = term_height * char_height;
  1398. ioctl(fd_master, TIOCSWINSZ, &w);
  1399. if (send_sig) {
  1400. kill(child_pid, SIGWINCH);
  1401. }
  1402. }
  1403. static void resize_finish(int width, int height) {
  1404. static int resize_attempts = 0;
  1405. int extra_x = 0;
  1406. int extra_y = 0;
  1407. if (!_no_frame) {
  1408. extra_x = decor_width();
  1409. extra_y = decor_height() + menu_bar_height;
  1410. }
  1411. int t_window_width = width - extra_x;
  1412. int t_window_height = height - extra_y;
  1413. if (t_window_width < char_width * 20 || t_window_height < char_height * 10) {
  1414. resize_attempts++;
  1415. int n_width = extra_x + max(char_width * 20, t_window_width);
  1416. int n_height = extra_y + max(char_height * 10, t_window_height);
  1417. yutani_window_resize_offer(yctx, window, n_width, n_height);
  1418. return;
  1419. }
  1420. if (!_free_size && ((t_window_width % char_width != 0 || t_window_height % char_height != 0) && resize_attempts < 3)) {
  1421. resize_attempts++;
  1422. int n_width = extra_x + t_window_width - (t_window_width % char_width);
  1423. int n_height = extra_y + t_window_height - (t_window_height % char_height);
  1424. yutani_window_resize_offer(yctx, window, n_width, n_height);
  1425. return;
  1426. }
  1427. resize_attempts = 0;
  1428. yutani_window_resize_accept(yctx, window, width, height);
  1429. window_width = window->width - extra_x;
  1430. window_height = window->height - extra_y;
  1431. reinit_graphics_yutani(ctx, window);
  1432. reinit(1);
  1433. yutani_window_resize_done(yctx, window);
  1434. yutani_flip(yctx, window);
  1435. }
  1436. void mouse_event(int button, int x, int y) {
  1437. char buf[7];
  1438. sprintf(buf, "\033[M%c%c%c", button + 32, x + 33, y + 33);
  1439. handle_input_s(buf);
  1440. }
  1441. void * handle_incoming(void) {
  1442. yutani_msg_t * m = yutani_poll(yctx);
  1443. while (m) {
  1444. menu_process_event(yctx, m);
  1445. switch (m->type) {
  1446. case YUTANI_MSG_KEY_EVENT:
  1447. {
  1448. struct yutani_msg_key_event * ke = (void*)m->data;
  1449. int ret = (ke->event.action == KEY_ACTION_DOWN) && (ke->event.key);
  1450. key_event(ret, &ke->event);
  1451. }
  1452. break;
  1453. case YUTANI_MSG_WINDOW_FOCUS_CHANGE:
  1454. {
  1455. struct yutani_msg_window_focus_change * wf = (void*)m->data;
  1456. yutani_window_t * win = hashmap_get(yctx->windows, (void*)wf->wid);
  1457. if (win) {
  1458. win->focused = wf->focused;
  1459. render_decors();
  1460. }
  1461. }
  1462. break;
  1463. case YUTANI_MSG_WINDOW_CLOSE:
  1464. {
  1465. struct yutani_msg_window_close * wc = (void*)m->data;
  1466. if (wc->wid == window->wid) {
  1467. kill(child_pid, SIGKILL);
  1468. exit_application = 1;
  1469. }
  1470. }
  1471. break;
  1472. case YUTANI_MSG_SESSION_END:
  1473. {
  1474. kill(child_pid, SIGKILL);
  1475. exit_application = 1;
  1476. }
  1477. break;
  1478. case YUTANI_MSG_RESIZE_OFFER:
  1479. {
  1480. struct yutani_msg_window_resize * wr = (void*)m->data;
  1481. resize_finish(wr->width, wr->height);
  1482. }
  1483. break;
  1484. case YUTANI_MSG_CLIPBOARD:
  1485. {
  1486. struct yutani_msg_clipboard * cb = (void *)m->data;
  1487. if (selection_text) {
  1488. free(selection_text);
  1489. }
  1490. if (*cb->content == '\002') {
  1491. int size = atoi(&cb->content[2]);
  1492. FILE * clipboard = yutani_open_clipboard(yctx);
  1493. selection_text = malloc(size + 1);
  1494. fread(selection_text, 1, size, clipboard);
  1495. selection_text[size] = '\0';
  1496. fclose(clipboard);
  1497. } else {
  1498. selection_text = malloc(cb->size+1);
  1499. memcpy(selection_text, cb->content, cb->size);
  1500. selection_text[cb->size] = '\0';
  1501. }
  1502. handle_input_s(selection_text);
  1503. }
  1504. break;
  1505. case YUTANI_MSG_WINDOW_MOUSE_EVENT:
  1506. {
  1507. struct yutani_msg_window_mouse_event * me = (void*)m->data;
  1508. if (me->wid != window->wid) break;
  1509. if (!_no_frame) {
  1510. int decor_response = decor_handle_event(yctx, m);
  1511. switch (decor_response) {
  1512. case DECOR_CLOSE:
  1513. kill(child_pid, SIGKILL);
  1514. exit_application = 1;
  1515. break;
  1516. case DECOR_RIGHT:
  1517. /* right click in decoration, show appropriate menu */
  1518. decor_show_default_menu(window, window->x + me->new_x, window->y + me->new_y);
  1519. break;
  1520. default:
  1521. break;
  1522. }
  1523. menu_bar_mouse_event(yctx, window, &terminal_menu_bar, me, me->new_x, me->new_y);
  1524. }
  1525. if (!_no_frame) {
  1526. if (me->new_x < 0 || me->new_x >= (int)window_width + (int)decor_width() || me->new_y < 0 || me->new_y >= (int)window_height + (int)decor_height()) {
  1527. break;
  1528. }
  1529. if (me->new_y < (int)decor_top_height+menu_bar_height || me->new_y >= (int)(window_height + decor_top_height+menu_bar_height)) {
  1530. break;
  1531. }
  1532. if (me->new_x < (int)decor_left_width || me->new_y >= (int)(window_width + decor_left_width)) {
  1533. break;
  1534. }
  1535. } else {
  1536. if (me->new_x < 0 || me->new_x >= (int)window_width || me->new_y < 0 || me->new_y >= (int)window_height) {
  1537. break;
  1538. }
  1539. }
  1540. int new_x = me->new_x;
  1541. int new_y = me->new_y;
  1542. if (!_no_frame) {
  1543. new_x -= decor_left_width;
  1544. new_y -= decor_top_height+menu_bar_height;
  1545. }
  1546. /* Convert from coordinate to cell positon */
  1547. new_x /= char_width;
  1548. new_y /= char_height;
  1549. if (new_x < 0 || new_y < 0) break;
  1550. if (new_x > term_width || new_y > term_height) break;
  1551. /* Map Cursor Action */
  1552. if (ansi_state->mouse_on) {
  1553. if (me->buttons & YUTANI_MOUSE_SCROLL_UP) {
  1554. mouse_event(32+32, new_x, new_y);
  1555. } else if (me->buttons & YUTANI_MOUSE_SCROLL_DOWN) {
  1556. mouse_event(32+32+1, new_x, new_y);
  1557. }
  1558. if (me->buttons != button_state) {
  1559. /* Figure out what changed */
  1560. if (me->buttons & YUTANI_MOUSE_BUTTON_LEFT && !(button_state & YUTANI_MOUSE_BUTTON_LEFT)) mouse_event(0, new_x, new_y);
  1561. if (me->buttons & YUTANI_MOUSE_BUTTON_MIDDLE && !(button_state & YUTANI_MOUSE_BUTTON_MIDDLE)) mouse_event(1, new_x, new_y);
  1562. if (me->buttons & YUTANI_MOUSE_BUTTON_RIGHT && !(button_state & YUTANI_MOUSE_BUTTON_RIGHT)) mouse_event(2, new_x, new_y);
  1563. if (!(me->buttons & YUTANI_MOUSE_BUTTON_LEFT) && button_state & YUTANI_MOUSE_BUTTON_LEFT) mouse_event(3, new_x, new_y);
  1564. if (!(me->buttons & YUTANI_MOUSE_BUTTON_MIDDLE) && button_state & YUTANI_MOUSE_BUTTON_MIDDLE) mouse_event(3, new_x, new_y);
  1565. if (!(me->buttons & YUTANI_MOUSE_BUTTON_RIGHT) && button_state & YUTANI_MOUSE_BUTTON_RIGHT) mouse_event(3, new_x, new_y);
  1566. last_mouse_x = new_x;
  1567. last_mouse_y = new_y;
  1568. button_state = me->buttons;
  1569. } else if (ansi_state->mouse_on == 2) {
  1570. /* Report motion for pressed buttons */
  1571. if (last_mouse_x == new_x && last_mouse_y == new_y) break;
  1572. if (button_state & YUTANI_MOUSE_BUTTON_LEFT) mouse_event(32, new_x, new_y);
  1573. if (button_state & YUTANI_MOUSE_BUTTON_MIDDLE) mouse_event(33, new_x, new_y);
  1574. if (button_state & YUTANI_MOUSE_BUTTON_RIGHT) mouse_event(34, new_x, new_y);
  1575. last_mouse_x = new_x;
  1576. last_mouse_y = new_y;
  1577. }
  1578. } else {
  1579. if (me->command == YUTANI_MOUSE_EVENT_DOWN && me->buttons & YUTANI_MOUSE_BUTTON_LEFT) {
  1580. term_redraw_all();
  1581. selection_start_x = new_x;
  1582. selection_start_y = new_y;
  1583. selection_end_x = new_x;
  1584. selection_end_y = new_y;
  1585. selection = 1;
  1586. redraw_selection();
  1587. display_flip();
  1588. }
  1589. if (me->command == YUTANI_MOUSE_EVENT_DRAG && me->buttons & YUTANI_MOUSE_BUTTON_LEFT ){
  1590. unredraw_selection();
  1591. selection_end_x = new_x;
  1592. selection_end_y = new_y;
  1593. redraw_selection();
  1594. display_flip();
  1595. }
  1596. if (me->command == YUTANI_MOUSE_EVENT_RAISE) {
  1597. if (me->new_x == me->old_x && me->new_y == me->old_y) {
  1598. selection = 0;
  1599. term_redraw_all();
  1600. display_flip();
  1601. } /* else selection */
  1602. }
  1603. if (me->buttons & YUTANI_MOUSE_SCROLL_UP) {
  1604. scroll_up(5);
  1605. } else if (me->buttons & YUTANI_MOUSE_SCROLL_DOWN) {
  1606. scroll_down(5);
  1607. } else if (me->buttons & YUTANI_MOUSE_BUTTON_RIGHT) {
  1608. if (!menu_right_click->window) {
  1609. menu_show(menu_right_click, yctx);
  1610. if (window->x + me->new_x + menu_right_click->window->width > yctx->display_width) {
  1611. yutani_window_move(yctx, menu_right_click->window, window->x + me->new_x - menu_right_click->window->width, window->y + me->new_y);
  1612. } else {
  1613. yutani_window_move(yctx, menu_right_click->window, window->x + me->new_x, window->y + me->new_y);
  1614. }
  1615. }
  1616. }
  1617. }
  1618. }
  1619. break;
  1620. default:
  1621. break;
  1622. }
  1623. free(m);
  1624. m = yutani_poll_async(yctx);
  1625. }
  1626. return NULL;
  1627. }
  1628. void _menu_action_exit(struct MenuEntry * self) {
  1629. kill(child_pid, SIGKILL);
  1630. exit_application = 1;
  1631. }
  1632. void _menu_action_hide_borders(struct MenuEntry * self) {
  1633. _no_frame = !(_no_frame);
  1634. window_width = window->width - decor_width() * (!_no_frame);
  1635. window_height = window->height - (decor_height() + menu_bar_height) * (!_no_frame);
  1636. reinit(1);
  1637. }
  1638. void _menu_action_show_about(struct MenuEntry * self) {
  1639. if (!fork()) {
  1640. system("about \"About Terminal\" /usr/share/icons/48/utilities-terminal.bmp \"ToaruOS Terminal\" \"(C) 2013-2018 K. Lange\n-\nPart of ToaruOS, which is free software\nreleased under the NCSA/University of Illinois\nlicense.\n-\n%https://toaruos.org\n%https://github.com/klange/toaru-nih\"");
  1641. exit(0);
  1642. }
  1643. render_decors();
  1644. }
  1645. void _menu_action_show_help(struct MenuEntry * self) {
  1646. if (!fork()) {
  1647. system("help-browser terminal.trt");
  1648. exit(0);
  1649. }
  1650. render_decors();
  1651. }
  1652. void _menu_action_copy(struct MenuEntry * self) {
  1653. copy_selection();
  1654. }
  1655. void _menu_action_paste(struct MenuEntry * self) {
  1656. yutani_special_request(yctx, NULL, YUTANI_SPECIAL_REQUEST_CLIPBOARD);
  1657. }
  1658. void maybe_flip_cursor(void) {
  1659. uint64_t ticks = get_ticks();
  1660. if (ticks > mouse_ticks + 600000LL) {
  1661. mouse_ticks = ticks;
  1662. flip_cursor();
  1663. }
  1664. }
  1665. int main(int argc, char ** argv) {
  1666. _login_shell = 0;
  1667. _fullscreen = 0;
  1668. _no_frame = 0;
  1669. window_width = char_width * 80;
  1670. window_height = char_height * 24;
  1671. static struct option long_opts[] = {
  1672. {"fullscreen", no_argument, 0, 'F'},
  1673. {"bitmap", no_argument, 0, 'b'},
  1674. {"scale", required_argument, 0, 's'},
  1675. {"login", no_argument, 0, 'l'},
  1676. {"help", no_argument, 0, 'h'},
  1677. {"kernel", no_argument, 0, 'k'},
  1678. {"grid", no_argument, 0, 'x'},
  1679. {"no-frame", no_argument, 0, 'n'},
  1680. {"geometry", required_argument, 0, 'g'},
  1681. {0,0,0,0}
  1682. };
  1683. /* Read some arguments */
  1684. int index, c;
  1685. while ((c = getopt_long(argc, argv, "bhxnFlks:g:", long_opts, &index)) != -1) {
  1686. if (!c) {
  1687. if (long_opts[index].flag == 0) {
  1688. c = long_opts[index].val;
  1689. }
  1690. }
  1691. switch (c) {
  1692. case 'k':
  1693. _force_kernel = 1;
  1694. break;
  1695. case 'x':
  1696. _free_size = 0;
  1697. break;
  1698. case 'l':
  1699. _login_shell = 1;
  1700. break;
  1701. case 'n':
  1702. _no_frame = 1;
  1703. break;
  1704. case 'F':
  1705. _fullscreen = 1;
  1706. _no_frame = 1;
  1707. break;
  1708. case 'b':
  1709. _use_sdf = 0;
  1710. break;
  1711. case 'h':
  1712. usage(argv);
  1713. return 0;
  1714. break;
  1715. case 's':
  1716. scale_fonts = 1;
  1717. font_scaling = atof(optarg);
  1718. break;
  1719. case 'g':
  1720. {
  1721. char * c = strstr(optarg, "x");
  1722. if (c) {
  1723. *c = '\0';
  1724. c++;
  1725. window_width = atoi(optarg);
  1726. window_height = atoi(c);
  1727. }
  1728. }
  1729. break;
  1730. case '?':
  1731. break;
  1732. default:
  1733. break;
  1734. }
  1735. }
  1736. // XXX
  1737. putenv("TERM=toaru");
  1738. /* Initialize the windowing library */
  1739. yctx = yutani_init();
  1740. if (_fullscreen) {
  1741. window_width = yctx->display_width;
  1742. window_height = yctx->display_height;
  1743. }
  1744. if (_no_frame) {
  1745. window = yutani_window_create(yctx, window_width, window_height);
  1746. } else {
  1747. init_decorations();
  1748. window = yutani_window_create(yctx, window_width + decor_left_width + decor_right_width, window_height + decor_top_height+menu_bar_height + decor_bottom_height);
  1749. }
  1750. if (_fullscreen) {
  1751. yutani_set_stack(yctx, window, YUTANI_ZORDER_BOTTOM);
  1752. window->focused = 1;
  1753. } else {
  1754. window->focused = 0;
  1755. }
  1756. /* Set up menus */
  1757. terminal_menu_bar.entries = terminal_menu_entries;
  1758. terminal_menu_bar.redraw_callback = render_decors;
  1759. struct MenuEntry * _menu_exit = menu_create_normal("exit","exit","Exit", _menu_action_exit);
  1760. struct MenuEntry * _menu_copy = menu_create_normal(NULL, NULL, "Copy", _menu_action_copy);
  1761. struct MenuEntry * _menu_paste = menu_create_normal(NULL, NULL, "Paste", _menu_action_paste);
  1762. menu_right_click = menu_create();
  1763. menu_insert(menu_right_click, _menu_copy);
  1764. menu_insert(menu_right_click, _menu_paste);
  1765. menu_insert(menu_right_click, menu_create_separator());
  1766. menu_insert(menu_right_click, menu_create_normal(NULL, NULL, "Toggle borders", _menu_action_hide_borders));
  1767. menu_insert(menu_right_click, menu_create_separator());
  1768. menu_insert(menu_right_click, _menu_exit);
  1769. /* Menu Bar menus */
  1770. terminal_menu_bar.set = menu_set_create();
  1771. struct MenuList * m;
  1772. m = menu_create(); /* File */
  1773. menu_insert(m, _menu_exit);
  1774. menu_set_insert(terminal_menu_bar.set, "file", m);
  1775. m = menu_create();
  1776. menu_insert(m, _menu_copy);
  1777. menu_insert(m, _menu_paste);
  1778. menu_set_insert(terminal_menu_bar.set, "edit", m);
  1779. m = menu_create();
  1780. menu_insert(m, menu_create_normal(NULL, NULL, "Hide borders", _menu_action_hide_borders));
  1781. menu_set_insert(terminal_menu_bar.set, "view", m);
  1782. m = menu_create();
  1783. menu_insert(m, menu_create_normal("help","help","Contents", _menu_action_show_help));
  1784. menu_insert(m, menu_create_separator());
  1785. menu_insert(m, menu_create_normal("star","star","About Terminal", _menu_action_show_about));
  1786. menu_set_insert(terminal_menu_bar.set, "help", m);
  1787. /* Initialize the graphics context */
  1788. ctx = init_graphics_yutani_double_buffer(window);
  1789. /* Clear to black */
  1790. draw_fill(ctx, rgba(0,0,0,0));
  1791. yutani_window_move(yctx, window, yctx->display_width / 2 - window->width / 2, yctx->display_height / 2 - window->height / 2);
  1792. syscall_openpty(&fd_master, &fd_slave, NULL, NULL, NULL);
  1793. terminal = fdopen(fd_slave, "w");
  1794. reinit(0);
  1795. fflush(stdin);
  1796. pid_t pid = getpid();
  1797. pid_t f = fork();
  1798. if (getpid() != pid) {
  1799. dup2(fd_slave, 0);
  1800. dup2(fd_slave, 1);
  1801. dup2(fd_slave, 2);
  1802. if (argv[optind] != NULL) {
  1803. char * tokens[] = {argv[optind], NULL};
  1804. execvp(tokens[0], tokens);
  1805. fprintf(stderr, "Failed to launch requested startup application.\n");
  1806. } else {
  1807. if (_login_shell) {
  1808. char * tokens[] = {"/bin/login",NULL};
  1809. execvp(tokens[0], tokens);
  1810. exit(1);
  1811. } else {
  1812. char * shell = getenv("SHELL");
  1813. if (!shell) shell = "/bin/sh"; /* fallback */
  1814. char * tokens[] = {shell,NULL};
  1815. execvp(tokens[0], tokens);
  1816. exit(1);
  1817. }
  1818. }
  1819. exit_application = 1;
  1820. return 1;
  1821. } else {
  1822. if (_force_kernel) {
  1823. /* Request kernel output to this terminal */
  1824. //syscall_system_function(4, (char **)fd_slave);
  1825. }
  1826. child_pid = f;
  1827. int fds[2] = {fileno(yctx->sock), fd_master};
  1828. unsigned char buf[1024];
  1829. while (!exit_application) {
  1830. int index = syscall_fswait2(2,fds,200);
  1831. check_for_exit();
  1832. if (index == 1) {
  1833. maybe_flip_cursor();
  1834. int r = read(fd_master, buf, 1024);
  1835. for (int i = 0; i < r; ++i) {
  1836. ansi_put(ansi_state, buf[i]);
  1837. }
  1838. display_flip();
  1839. } else if (index == 0) {
  1840. maybe_flip_cursor();
  1841. handle_incoming();
  1842. } else if (index == 2) {
  1843. maybe_flip_cursor();
  1844. }
  1845. }
  1846. }
  1847. //yutani_close(yctx, window);
  1848. return 0;
  1849. }