terminal.c 74 KB

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