terminal.c 69 KB

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