terminal.c 67 KB

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