terminal.c 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550
  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();
  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 (!window->focused) {
  1017. /* An unfocused terminal should draw an unfilled box. */
  1018. cell_redraw_box(csr_x, csr_y);
  1019. } else {
  1020. /* A focused terminal draws a solid box. */
  1021. cell_redraw_inverted(csr_x, csr_y);
  1022. }
  1023. }
  1024. static uint8_t cursor_flipped = 0;
  1025. /* A soft request to draw the cursor. */
  1026. static void draw_cursor() {
  1027. if (!cursor_on) return;
  1028. mouse_ticks = get_ticks();
  1029. cursor_flipped = 0;
  1030. render_cursor();
  1031. }
  1032. /* Timer callback to flip (flash) the cursor */
  1033. static void maybe_flip_cursor(void) {
  1034. uint64_t ticks = get_ticks();
  1035. if (ticks > mouse_ticks + 600000LL) {
  1036. mouse_ticks = ticks;
  1037. if (scrollback_offset != 0) {
  1038. return; /* Don't flip cursor while drawing scrollback */
  1039. }
  1040. if (window->focused && cursor_flipped) {
  1041. cell_redraw(csr_x, csr_y);
  1042. } else {
  1043. render_cursor();
  1044. }
  1045. display_flip();
  1046. cursor_flipped = 1 - cursor_flipped;
  1047. }
  1048. }
  1049. /* Draw all cells. Duplicates code from cell_redraw to avoid unecessary bounds checks. */
  1050. static void term_redraw_all() {
  1051. for (int i = 0; i < term_height; i++) {
  1052. for (int x = 0; x < term_width; ++x) {
  1053. /* Calculate the cell position in the terminal buffer */
  1054. term_cell_t * cell = (term_cell_t *)((uintptr_t)term_buffer + (i * term_width + x) * sizeof(term_cell_t));
  1055. /* If it's an image cell, redraw the image data. */
  1056. if (cell->flags & ANSI_EXT_IMG) {
  1057. redraw_cell_image(x,i,cell);
  1058. continue;
  1059. }
  1060. /* Special case empty cells. */
  1061. if (((uint32_t *)cell)[0] == 0x00000000) {
  1062. term_write_char(' ', x * char_width, i * char_height, TERM_DEFAULT_FG, TERM_DEFAULT_BG, TERM_DEFAULT_FLAGS);
  1063. } else {
  1064. term_write_char(cell->c, x * char_width, i * char_height, cell->fg, cell->bg, cell->flags);
  1065. }
  1066. }
  1067. }
  1068. }
  1069. /* Remove no-longer-visible image cell data. */
  1070. static void flush_unused_images(void) {
  1071. list_t * tmp = list_create();
  1072. for (int y = 0; y < term_height; ++y) {
  1073. for (int x = 0; x < term_width; ++x) {
  1074. term_cell_t * cell = (term_cell_t *)((uintptr_t)term_buffer + (y * term_width + x) * sizeof(term_cell_t));
  1075. if (cell->flags & ANSI_EXT_IMG) {
  1076. list_insert(tmp, (void *)cell->fg);
  1077. }
  1078. }
  1079. }
  1080. foreach(node, images_list) {
  1081. if (!list_find(tmp, node->value)) {
  1082. free(node->value);
  1083. }
  1084. }
  1085. list_free(images_list);
  1086. images_list = tmp;
  1087. }
  1088. /* Scroll the terminal up or down. */
  1089. static void term_scroll(int how_much) {
  1090. /* A large scroll request should just clear the screen. */
  1091. if (how_much >= term_height || -how_much >= term_height) {
  1092. term_clear();
  1093. return;
  1094. }
  1095. /* A request to scroll 0... is a request not to scroll. */
  1096. if (how_much == 0) {
  1097. return;
  1098. }
  1099. /* Redraw the cursor before continuing. */
  1100. cell_redraw(csr_x, csr_y);
  1101. if (how_much > 0) {
  1102. /* Scroll up */
  1103. memmove(term_buffer, (void *)((uintptr_t)term_buffer + sizeof(term_cell_t) * term_width), sizeof(term_cell_t) * term_width * (term_height - how_much));
  1104. /* Reset the "new" row to clean cells */
  1105. 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);
  1106. /* In graphical modes, we will shift the graphics buffer up as necessary */
  1107. uintptr_t dst, src;
  1108. size_t siz = char_height * (term_height - how_much) * GFX_W(ctx) * GFX_B(ctx);
  1109. if (!_no_frame) {
  1110. /* Must include decorations */
  1111. dst = (uintptr_t)ctx->backbuffer + (GFX_W(ctx) * (decor_top_height+menu_bar_height)) * GFX_B(ctx);
  1112. src = (uintptr_t)ctx->backbuffer + (GFX_W(ctx) * (decor_top_height+menu_bar_height + char_height * how_much)) * GFX_B(ctx);
  1113. } else {
  1114. /* Can skip decorations */
  1115. dst = (uintptr_t)ctx->backbuffer;
  1116. src = (uintptr_t)ctx->backbuffer + (GFX_W(ctx) * char_height * how_much) * GFX_B(ctx);
  1117. }
  1118. /* Perform the shift */
  1119. memmove((void *)dst, (void *)src, siz);
  1120. /* And redraw the new rows */
  1121. for (int i = 0; i < how_much; ++i) {
  1122. for (uint16_t x = 0; x < term_width; ++x) {
  1123. cell_set(x,term_height - how_much,' ', current_fg, current_bg, ansi_state->flags);
  1124. cell_redraw(x, term_height - how_much);
  1125. }
  1126. }
  1127. } else {
  1128. how_much = -how_much;
  1129. /* Scroll down */
  1130. memmove((void *)((uintptr_t)term_buffer + sizeof(term_cell_t) * term_width), term_buffer, sizeof(term_cell_t) * term_width * (term_height - how_much));
  1131. /* Reset the "new" row to clean cells */
  1132. memset(term_buffer, 0x0, sizeof(term_cell_t) * term_width * how_much);
  1133. uintptr_t dst, src;
  1134. size_t siz = char_height * (term_height - how_much) * GFX_W(ctx) * GFX_B(ctx);
  1135. if (!_no_frame) {
  1136. src = (uintptr_t)ctx->backbuffer + (GFX_W(ctx) * (decor_top_height+menu_bar_height)) * GFX_B(ctx);
  1137. dst = (uintptr_t)ctx->backbuffer + (GFX_W(ctx) * (decor_top_height+menu_bar_height + char_height * how_much)) * GFX_B(ctx);
  1138. } else {
  1139. src = (uintptr_t)ctx->backbuffer;
  1140. dst = (uintptr_t)ctx->backbuffer + (GFX_W(ctx) * char_height * how_much) * GFX_B(ctx);
  1141. }
  1142. /* Perform the shift */
  1143. memmove((void *)dst, (void *)src, siz);
  1144. /* And redraw the new rows */
  1145. for (int i = 0; i < how_much; ++i) {
  1146. for (uint16_t x = 0; x < term_width; ++x) {
  1147. cell_redraw(x, i);
  1148. }
  1149. }
  1150. }
  1151. /* Remove image data for image cells that are no longer on screen. */
  1152. flush_unused_images();
  1153. /* Flip the entire window. */
  1154. yutani_flip(yctx, window);
  1155. }
  1156. /* Is this a wide character? (does wcwidth == 2) */
  1157. static int is_wide(uint32_t codepoint) {
  1158. if (codepoint < 256) return 0;
  1159. return wcwidth(codepoint) == 2;
  1160. }
  1161. /* Save the row that is about to be scrolled offscreen into the scrollback buffer. */
  1162. static void save_scrollback(void) {
  1163. /* If the scrollback is already full, remove the oldest element. */
  1164. if (scrollback_list->length == MAX_SCROLLBACK) {
  1165. free(list_dequeue(scrollback_list));
  1166. }
  1167. struct scrollback_row * row = malloc(sizeof(struct scrollback_row) + sizeof(term_cell_t) * term_width + 20);
  1168. row->width = term_width;
  1169. for (int i = 0; i < term_width; ++i) {
  1170. term_cell_t * cell = (term_cell_t *)((uintptr_t)term_buffer + (i) * sizeof(term_cell_t));
  1171. memcpy(&row->cells[i], cell, sizeof(term_cell_t));
  1172. }
  1173. list_insert(scrollback_list, row);
  1174. }
  1175. /* Draw the scrollback. */
  1176. static void redraw_scrollback(void) {
  1177. if (!scrollback_offset) {
  1178. term_redraw_all();
  1179. display_flip();
  1180. return;
  1181. }
  1182. if (scrollback_offset < term_height) {
  1183. for (int i = scrollback_offset; i < term_height; i++) {
  1184. int y = i - scrollback_offset;
  1185. for (int x = 0; x < term_width; ++x) {
  1186. term_cell_t * cell = (term_cell_t *)((uintptr_t)term_buffer + (y * term_width + x) * sizeof(term_cell_t));
  1187. if (cell->flags & ANSI_EXT_IMG) { redraw_cell_image(x,i,cell); continue; }
  1188. if (((uint32_t *)cell)[0] == 0x00000000) {
  1189. term_write_char(' ', x * char_width, i * char_height, TERM_DEFAULT_FG, TERM_DEFAULT_BG, TERM_DEFAULT_FLAGS);
  1190. } else {
  1191. term_write_char(cell->c, x * char_width, i * char_height, cell->fg, cell->bg, cell->flags);
  1192. }
  1193. }
  1194. }
  1195. node_t * node = scrollback_list->tail;
  1196. for (int i = 0; i < scrollback_offset; ++i) {
  1197. struct scrollback_row * row = (struct scrollback_row *)node->value;
  1198. int y = scrollback_offset - 1 - i;
  1199. int width = row->width;
  1200. if (width > term_width) {
  1201. width = term_width;
  1202. } else {
  1203. for (int x = row->width; x < term_width; ++x) {
  1204. term_write_char(' ', x * char_width, y * char_height, TERM_DEFAULT_FG, TERM_DEFAULT_BG, TERM_DEFAULT_FLAGS);
  1205. }
  1206. }
  1207. for (int x = 0; x < width; ++x) {
  1208. term_cell_t * cell = &row->cells[x];
  1209. if (((uint32_t *)cell)[0] == 0x00000000) {
  1210. term_write_char(' ', x * char_width, y * char_height, TERM_DEFAULT_FG, TERM_DEFAULT_BG, TERM_DEFAULT_FLAGS);
  1211. } else {
  1212. term_write_char(cell->c, x * char_width, y * char_height, cell->fg, cell->bg, cell->flags);
  1213. }
  1214. }
  1215. node = node->prev;
  1216. }
  1217. } else {
  1218. node_t * node = scrollback_list->tail;
  1219. for (int i = 0; i < scrollback_offset - term_height; ++i) {
  1220. node = node->prev;
  1221. }
  1222. for (int i = scrollback_offset - term_height; i < scrollback_offset; ++i) {
  1223. struct scrollback_row * row = (struct scrollback_row *)node->value;
  1224. int y = scrollback_offset - 1 - i;
  1225. int width = row->width;
  1226. if (width > term_width) {
  1227. width = term_width;
  1228. } else {
  1229. for (int x = row->width; x < term_width; ++x) {
  1230. term_write_char(' ', x * char_width, y * char_height, TERM_DEFAULT_FG, TERM_DEFAULT_BG, TERM_DEFAULT_FLAGS);
  1231. }
  1232. }
  1233. for (int x = 0; x < width; ++x) {
  1234. term_cell_t * cell = &row->cells[x];
  1235. if (((uint32_t *)cell)[0] == 0x00000000) {
  1236. term_write_char(' ', x * char_width, y * char_height, TERM_DEFAULT_FG, TERM_DEFAULT_BG, TERM_DEFAULT_FLAGS);
  1237. } else {
  1238. term_write_char(cell->c, x * char_width, y * char_height, cell->fg, cell->bg, cell->flags);
  1239. }
  1240. }
  1241. node = node->prev;
  1242. }
  1243. }
  1244. display_flip();
  1245. }
  1246. /*
  1247. * ANSI callback for writing characters.
  1248. * Parses some things (\n\r, etc.) itself that should probably
  1249. * be moved into the ANSI library.
  1250. */
  1251. static void term_write(char c) {
  1252. static uint32_t unicode_state = 0;
  1253. static uint32_t codepoint = 0;
  1254. cell_redraw(csr_x, csr_y);
  1255. if (!decode(&unicode_state, &codepoint, (uint8_t)c)) {
  1256. uint32_t o = codepoint;
  1257. codepoint = 0;
  1258. if (c == '\r') {
  1259. csr_x = 0;
  1260. draw_cursor();
  1261. return;
  1262. }
  1263. if (csr_x < 0) csr_x = 0;
  1264. if (csr_y < 0) csr_y = 0;
  1265. if (csr_x == term_width) {
  1266. csr_x = 0;
  1267. ++csr_y;
  1268. if (c == '\n') return;
  1269. }
  1270. if (csr_y == term_height) {
  1271. save_scrollback();
  1272. term_scroll(1);
  1273. csr_y = term_height - 1;
  1274. }
  1275. if (c == '\n') {
  1276. ++csr_y;
  1277. if (csr_y == term_height) {
  1278. save_scrollback();
  1279. term_scroll(1);
  1280. csr_y = term_height - 1;
  1281. }
  1282. draw_cursor();
  1283. } else if (c == '\007') {
  1284. /* bell */
  1285. /* XXX play sound */
  1286. } else if (c == '\b') {
  1287. if (csr_x > 0) {
  1288. --csr_x;
  1289. }
  1290. cell_redraw(csr_x, csr_y);
  1291. draw_cursor();
  1292. } else if (c == '\t') {
  1293. csr_x += (8 - csr_x % 8);
  1294. draw_cursor();
  1295. } else {
  1296. int wide = is_wide(o);
  1297. uint8_t flags = ansi_state->flags;
  1298. if (wide && csr_x == term_width - 1) {
  1299. csr_x = 0;
  1300. ++csr_y;
  1301. }
  1302. if (wide) {
  1303. flags = flags | ANSI_WIDE;
  1304. }
  1305. cell_set(csr_x,csr_y, o, current_fg, current_bg, flags);
  1306. cell_redraw(csr_x,csr_y);
  1307. csr_x++;
  1308. if (wide && csr_x != term_width) {
  1309. cell_set(csr_x, csr_y, 0xFFFF, current_fg, current_bg, ansi_state->flags);
  1310. cell_redraw(csr_x,csr_y);
  1311. cell_redraw(csr_x-1,csr_y);
  1312. csr_x++;
  1313. }
  1314. }
  1315. } else if (unicode_state == UTF8_REJECT) {
  1316. unicode_state = 0;
  1317. codepoint = 0;
  1318. }
  1319. draw_cursor();
  1320. }
  1321. /* ANSI callback to set cursor position */
  1322. static void term_set_csr(int x, int y) {
  1323. cell_redraw(csr_x,csr_y);
  1324. csr_x = x;
  1325. csr_y = y;
  1326. draw_cursor();
  1327. }
  1328. /* ANSI callback to get cursor x position */
  1329. static int term_get_csr_x(void) {
  1330. return csr_x;
  1331. }
  1332. /* ANSI callback to get cursor y position */
  1333. static int term_get_csr_y(void) {
  1334. return csr_y;
  1335. }
  1336. /* ANSI callback to set cell image data. */
  1337. static void term_set_cell_contents(int x, int y, char * data) {
  1338. char * cell_data = malloc(char_width * char_height * sizeof(uint32_t));
  1339. memcpy(cell_data, data, char_width * char_height * sizeof(uint32_t));
  1340. list_insert(images_list, cell_data);
  1341. cell_set(x, y, ' ', (uint32_t)cell_data, 0, ANSI_EXT_IMG);
  1342. }
  1343. /* ANSI callback to get character cell width */
  1344. static int term_get_cell_width(void) {
  1345. return char_width;
  1346. }
  1347. /* ANSI callback to get character cell height */
  1348. static int term_get_cell_height(void) {
  1349. return char_height;
  1350. }
  1351. /* ANSI callback to set cursor visibility */
  1352. static void term_set_csr_show(int on) {
  1353. cursor_on = on;
  1354. if (on) {
  1355. draw_cursor();
  1356. }
  1357. }
  1358. /* ANSI callback to set the foreground/background colors. */
  1359. static void term_set_colors(uint32_t fg, uint32_t bg) {
  1360. current_fg = fg;
  1361. current_bg = bg;
  1362. }
  1363. /* ANSI callback to force the cursor to draw */
  1364. static void term_redraw_cursor() {
  1365. if (term_buffer) {
  1366. draw_cursor();
  1367. }
  1368. }
  1369. /* ANSI callback to set a cell to a codepoint (only ever used to set spaces) */
  1370. static void term_set_cell(int x, int y, uint32_t c) {
  1371. cell_set(x, y, c, current_fg, current_bg, ansi_state->flags);
  1372. cell_redraw(x, y);
  1373. }
  1374. /* ANSI callback to clear the terminal. */
  1375. static void term_clear(int i) {
  1376. if (i == 2) {
  1377. /* Clear all */
  1378. csr_x = 0;
  1379. csr_y = 0;
  1380. memset((void *)term_buffer, 0x00, term_width * term_height * sizeof(term_cell_t));
  1381. if (!_no_frame) {
  1382. render_decors();
  1383. }
  1384. term_redraw_all();
  1385. } else if (i == 0) {
  1386. /* Clear after cursor */
  1387. for (int x = csr_x; x < term_width; ++x) {
  1388. term_set_cell(x, csr_y, ' ');
  1389. }
  1390. for (int y = csr_y + 1; y < term_height; ++y) {
  1391. for (int x = 0; x < term_width; ++x) {
  1392. term_set_cell(x, y, ' ');
  1393. }
  1394. }
  1395. } else if (i == 1) {
  1396. /* Clear before cursor */
  1397. for (int y = 0; y < csr_y; ++y) {
  1398. for (int x = 0; x < term_width; ++x) {
  1399. term_set_cell(x, y, ' ');
  1400. }
  1401. }
  1402. for (int x = 0; x < csr_x; ++x) {
  1403. term_set_cell(x, csr_y, ' ');
  1404. }
  1405. }
  1406. flush_unused_images();
  1407. }
  1408. #define SWAP(T,a,b) do { T _a = a; a = b; b = _a; } while(0);
  1409. static void term_switch_buffer(int buffer) {
  1410. if (buffer != 0 && buffer != 1) return;
  1411. if (buffer != active_buffer) {
  1412. active_buffer = buffer;
  1413. term_buffer = active_buffer == 0 ? term_buffer_a : term_buffer_b;
  1414. SWAP(int, csr_x, _orig_x);
  1415. SWAP(int, csr_y, _orig_y);
  1416. SWAP(uint32_t, current_fg, _orig_fg);
  1417. SWAP(uint32_t, current_bg, _orig_bg);
  1418. term_redraw_all();
  1419. display_flip();
  1420. }
  1421. }
  1422. /* ANSI callbacks */
  1423. term_callbacks_t term_callbacks = {
  1424. term_write,
  1425. term_set_colors,
  1426. term_set_csr,
  1427. term_get_csr_x,
  1428. term_get_csr_y,
  1429. term_set_cell,
  1430. term_clear,
  1431. term_scroll,
  1432. term_redraw_cursor,
  1433. input_buffer_stuff,
  1434. set_title,
  1435. term_set_cell_contents,
  1436. term_get_cell_width,
  1437. term_get_cell_height,
  1438. term_set_csr_show,
  1439. term_switch_buffer,
  1440. };
  1441. /* Write data into the PTY */
  1442. static void handle_input(char c) {
  1443. write(fd_master, &c, 1);
  1444. display_flip();
  1445. if (scrollback_offset != 0) {
  1446. scrollback_offset = 0;
  1447. term_redraw_all();
  1448. }
  1449. }
  1450. /* Write a string into the PTY */
  1451. static void handle_input_s(char * c) {
  1452. write(fd_master, c, strlen(c));
  1453. display_flip();
  1454. if (scrollback_offset != 0) {
  1455. scrollback_offset = 0;
  1456. term_redraw_all();
  1457. }
  1458. }
  1459. /* Scroll the view up (scrollback) */
  1460. static void scroll_up(int amount) {
  1461. int i = 0;
  1462. while (i < amount && scrollback_list && scrollback_offset < (int)scrollback_list->length) {
  1463. scrollback_offset ++;
  1464. i++;
  1465. }
  1466. redraw_scrollback();
  1467. }
  1468. /* Scroll the view down (scrollback) */
  1469. void scroll_down(int amount) {
  1470. int i = 0;
  1471. while (i < amount && scrollback_list && scrollback_offset != 0) {
  1472. scrollback_offset -= 1;
  1473. i++;
  1474. }
  1475. redraw_scrollback();
  1476. }
  1477. /* Handle a key press from Yutani */
  1478. static void key_event(int ret, key_event_t * event) {
  1479. if (ret) {
  1480. /* Ctrl-Shift-C - Copy selection */
  1481. if ((event->modifiers & KEY_MOD_LEFT_SHIFT || event->modifiers & KEY_MOD_RIGHT_SHIFT) &&
  1482. (event->modifiers & KEY_MOD_LEFT_CTRL || event->modifiers & KEY_MOD_RIGHT_CTRL) &&
  1483. (event->keycode == 'c')) {
  1484. if (selection) {
  1485. /* Copy selection */
  1486. copy_selection();
  1487. }
  1488. return;
  1489. }
  1490. /* Ctrl-Shift-V - Paste selection */
  1491. if ((event->modifiers & KEY_MOD_LEFT_SHIFT || event->modifiers & KEY_MOD_RIGHT_SHIFT) &&
  1492. (event->modifiers & KEY_MOD_LEFT_CTRL || event->modifiers & KEY_MOD_RIGHT_CTRL) &&
  1493. (event->keycode == 'v')) {
  1494. /* Paste selection */
  1495. yutani_special_request(yctx, NULL, YUTANI_SPECIAL_REQUEST_CLIPBOARD);
  1496. return;
  1497. }
  1498. /* Left alt */
  1499. if (event->modifiers & KEY_MOD_LEFT_ALT || event->modifiers & KEY_MOD_RIGHT_ALT) {
  1500. handle_input('\033');
  1501. }
  1502. /* Shift-Tab */
  1503. if ((event->modifiers & KEY_MOD_LEFT_SHIFT || event->modifiers & KEY_MOD_RIGHT_SHIFT) &&
  1504. event->key == '\t') {
  1505. handle_input_s("\033[Z");
  1506. return;
  1507. }
  1508. /* ENTER = reads as linefeed, should be carriage return */
  1509. if (event->keycode == 10) {
  1510. handle_input('\r');
  1511. return;
  1512. }
  1513. /* BACKSPACE = reads as ^H, should be ^? */
  1514. if (event->keycode == 8) {
  1515. handle_input(0x7F);
  1516. return;
  1517. }
  1518. /* Pass key value to PTY */
  1519. handle_input(event->key);
  1520. } else {
  1521. /* Special keys without ->key values */
  1522. /* Only trigger on key down */
  1523. if (event->action == KEY_ACTION_UP) return;
  1524. switch (event->keycode) {
  1525. case KEY_F1:
  1526. handle_input_s("\033OP");
  1527. break;
  1528. case KEY_F2:
  1529. handle_input_s("\033OQ");
  1530. break;
  1531. case KEY_F3:
  1532. handle_input_s("\033OR");
  1533. break;
  1534. case KEY_F4:
  1535. handle_input_s("\033OS");
  1536. break;
  1537. case KEY_F5:
  1538. handle_input_s("\033[15~");
  1539. break;
  1540. case KEY_F6:
  1541. handle_input_s("\033[17~");
  1542. break;
  1543. case KEY_F7:
  1544. handle_input_s("\033[18~");
  1545. break;
  1546. case KEY_F8:
  1547. handle_input_s("\033[19~");
  1548. break;
  1549. case KEY_F9:
  1550. handle_input_s("\033[20~");
  1551. break;
  1552. case KEY_F10:
  1553. handle_input_s("\033[21~");
  1554. break;
  1555. case KEY_F11:
  1556. handle_input_s("\033[23~");
  1557. break;
  1558. case KEY_F12:
  1559. /* Toggle decorations */
  1560. if (!_fullscreen) {
  1561. _no_frame = !_no_frame;
  1562. window_width = window->width - decor_width * (!_no_frame);
  1563. window_height = window->height - (decor_height + menu_bar_height) * (!_no_frame);
  1564. reinit(1);
  1565. }
  1566. break;
  1567. case KEY_ARROW_UP:
  1568. if (event->modifiers & KEY_MOD_LEFT_SHIFT && event->modifiers & KEY_MOD_LEFT_CTRL) {
  1569. handle_input_s("\033[6A");
  1570. } else if (event->modifiers & KEY_MOD_LEFT_CTRL) {
  1571. handle_input_s("\033[5A");
  1572. } else if (event->modifiers & KEY_MOD_LEFT_SHIFT && event->modifiers & KEY_MOD_LEFT_ALT) {
  1573. handle_input_s("\033[4A");
  1574. } else if (event->modifiers & KEY_MOD_LEFT_ALT) {
  1575. handle_input_s("\033[3A");
  1576. } else if (event->modifiers & KEY_MOD_LEFT_SHIFT) {
  1577. handle_input_s("\033[2A");
  1578. } else {
  1579. handle_input_s("\033[A");
  1580. }
  1581. break;
  1582. case KEY_ARROW_DOWN:
  1583. if (event->modifiers & KEY_MOD_LEFT_SHIFT && event->modifiers & KEY_MOD_LEFT_CTRL) {
  1584. handle_input_s("\033[6B");
  1585. } else if (event->modifiers & KEY_MOD_LEFT_CTRL) {
  1586. handle_input_s("\033[5B");
  1587. } else if (event->modifiers & KEY_MOD_LEFT_SHIFT && event->modifiers & KEY_MOD_LEFT_ALT) {
  1588. handle_input_s("\033[4B");
  1589. } else if (event->modifiers & KEY_MOD_LEFT_ALT) {
  1590. handle_input_s("\033[3B");
  1591. } else if (event->modifiers & KEY_MOD_LEFT_SHIFT) {
  1592. handle_input_s("\033[2B");
  1593. } else {
  1594. handle_input_s("\033[B");
  1595. }
  1596. break;
  1597. case KEY_ARROW_RIGHT:
  1598. if (event->modifiers & KEY_MOD_LEFT_SHIFT && event->modifiers & KEY_MOD_LEFT_CTRL) {
  1599. handle_input_s("\033[6C");
  1600. } else if (event->modifiers & KEY_MOD_LEFT_CTRL) {
  1601. handle_input_s("\033[5C");
  1602. } else if (event->modifiers & KEY_MOD_LEFT_SHIFT && event->modifiers & KEY_MOD_LEFT_ALT) {
  1603. handle_input_s("\033[4C");
  1604. } else if (event->modifiers & KEY_MOD_LEFT_ALT) {
  1605. handle_input_s("\033[3C");
  1606. } else if (event->modifiers & KEY_MOD_LEFT_SHIFT) {
  1607. handle_input_s("\033[2C");
  1608. } else {
  1609. handle_input_s("\033[C");
  1610. }
  1611. break;
  1612. case KEY_ARROW_LEFT:
  1613. if (event->modifiers & KEY_MOD_LEFT_SHIFT && event->modifiers & KEY_MOD_LEFT_CTRL) {
  1614. handle_input_s("\033[6D");
  1615. } else if (event->modifiers & KEY_MOD_LEFT_CTRL) {
  1616. handle_input_s("\033[5D");
  1617. } else if (event->modifiers & KEY_MOD_LEFT_SHIFT && event->modifiers & KEY_MOD_LEFT_ALT) {
  1618. handle_input_s("\033[4D");
  1619. } else if (event->modifiers & KEY_MOD_LEFT_ALT) {
  1620. handle_input_s("\033[3D");
  1621. } else if (event->modifiers & KEY_MOD_LEFT_SHIFT) {
  1622. handle_input_s("\033[2D");
  1623. } else {
  1624. handle_input_s("\033[D");
  1625. }
  1626. break;
  1627. case KEY_PAGE_UP:
  1628. if (event->modifiers & KEY_MOD_LEFT_SHIFT) {
  1629. scroll_up(term_height/2);
  1630. } else {
  1631. handle_input_s("\033[5~");
  1632. }
  1633. break;
  1634. case KEY_PAGE_DOWN:
  1635. if (event->modifiers & KEY_MOD_LEFT_SHIFT) {
  1636. scroll_down(term_height/2);
  1637. } else {
  1638. handle_input_s("\033[6~");
  1639. }
  1640. break;
  1641. case KEY_HOME:
  1642. if (event->modifiers & KEY_MOD_LEFT_SHIFT) {
  1643. if (scrollback_list) {
  1644. scrollback_offset = scrollback_list->length;
  1645. redraw_scrollback();
  1646. }
  1647. } else {
  1648. handle_input_s("\033[H");
  1649. }
  1650. break;
  1651. case KEY_END:
  1652. if (event->modifiers & KEY_MOD_LEFT_SHIFT) {
  1653. scrollback_offset = 0;
  1654. redraw_scrollback();
  1655. } else {
  1656. handle_input_s("\033[F");
  1657. }
  1658. break;
  1659. case KEY_DEL:
  1660. handle_input_s("\033[3~");
  1661. break;
  1662. case KEY_INSERT:
  1663. handle_input_s("\033[2~");
  1664. break;
  1665. }
  1666. }
  1667. }
  1668. /* Check if the Terminal should close. */
  1669. static void check_for_exit(void) {
  1670. /* If something has set exit_application, we should exit. */
  1671. if (exit_application) return;
  1672. pid_t pid = waitpid(-1, NULL, WNOHANG);
  1673. /* If the child has exited, we should exit. */
  1674. if (pid != child_pid) return;
  1675. /* Clean up */
  1676. exit_application = 1;
  1677. /* Write [Process terminated] */
  1678. char exit_message[] = "[Process terminated]\n";
  1679. write(fd_slave, exit_message, sizeof(exit_message));
  1680. }
  1681. static term_cell_t * copy_terminal(int old_width, int old_height, term_cell_t * term_buffer) {
  1682. term_cell_t * new_term_buffer = malloc(sizeof(term_cell_t) * term_width * term_height);
  1683. memset(new_term_buffer, 0x0, sizeof(term_cell_t) * term_width * term_height);
  1684. int offset = 0;
  1685. if (term_height < old_height) {
  1686. while (csr_y >= term_height) {
  1687. offset++;
  1688. old_height--;
  1689. csr_y--;
  1690. }
  1691. }
  1692. for (int row = 0; row < min(old_height, term_height); ++row) {
  1693. for (int col = 0; col < min(old_width, term_width); ++col) {
  1694. term_cell_t * old_cell = (term_cell_t *)((uintptr_t)term_buffer + ((row + offset) * old_width + col) * sizeof(term_cell_t));
  1695. term_cell_t * new_cell = (term_cell_t *)((uintptr_t)new_term_buffer + (row * term_width + col) * sizeof(term_cell_t));
  1696. *new_cell = *old_cell;
  1697. }
  1698. }
  1699. if (csr_x >= term_width) {
  1700. csr_x = term_width-1;
  1701. }
  1702. return new_term_buffer;
  1703. }
  1704. /* Reinitialize the terminal after a resize. */
  1705. static void reinit(int send_sig) {
  1706. /* Figure out character sizes if fonts have changed. */
  1707. if (_use_aa && !_have_freetype) {
  1708. char_width = 9;
  1709. char_height = 17;
  1710. font_size = 16;
  1711. if (scale_fonts) {
  1712. font_size *= font_scaling;
  1713. char_height *= font_scaling;
  1714. char_width *= font_scaling;
  1715. }
  1716. } else if (_use_aa && _have_freetype) {
  1717. font_size = 13;
  1718. char_height = 17;
  1719. char_width = 8;
  1720. char_offset = 13;
  1721. if (scale_fonts) {
  1722. /* Recalculate scaling */
  1723. font_size *= font_scaling;
  1724. char_height *= font_scaling;
  1725. char_width *= font_scaling;
  1726. char_offset *= font_scaling;
  1727. }
  1728. } else {
  1729. char_width = 9;
  1730. char_height = 20;
  1731. }
  1732. int old_width = term_width;
  1733. int old_height = term_height;
  1734. /* Resize the terminal buffer */
  1735. term_width = window_width / char_width;
  1736. term_height = window_height / char_height;
  1737. if (term_buffer) {
  1738. term_cell_t * new_a = copy_terminal(old_width, old_height, term_buffer_a);
  1739. term_cell_t * new_b = copy_terminal(old_width, old_height, term_buffer_b);
  1740. free(term_buffer_a);
  1741. term_buffer_a = new_a;
  1742. free(term_buffer_b);
  1743. term_buffer_b = new_b;
  1744. if (active_buffer == 0) {
  1745. term_buffer = new_a;
  1746. } else {
  1747. term_buffer = new_b;
  1748. }
  1749. } else {
  1750. term_buffer_a = malloc(sizeof(term_cell_t) * term_width * term_height);
  1751. memset(term_buffer_a, 0x0, sizeof(term_cell_t) * term_width * term_height);
  1752. term_buffer_b = malloc(sizeof(term_cell_t) * term_width * term_height);
  1753. memset(term_buffer_b, 0x0, sizeof(term_cell_t) * term_width * term_height);
  1754. term_buffer = term_buffer_a;
  1755. }
  1756. /* Reset the ANSI library, ensuring we keep certain values */
  1757. int old_mouse_state = 0;
  1758. if (ansi_state) old_mouse_state = ansi_state->mouse_on;
  1759. ansi_state = ansi_init(ansi_state, term_width, term_height, &term_callbacks);
  1760. ansi_state->mouse_on = old_mouse_state;
  1761. /* Redraw the window */
  1762. draw_fill(ctx, rgba(0,0,0, TERM_DEFAULT_OPAC));
  1763. render_decors();
  1764. term_redraw_all();
  1765. display_flip();
  1766. /* Send window size change ioctl */
  1767. struct winsize w;
  1768. w.ws_row = term_height;
  1769. w.ws_col = term_width;
  1770. w.ws_xpixel = term_width * char_width;
  1771. w.ws_ypixel = term_height * char_height;
  1772. ioctl(fd_master, TIOCSWINSZ, &w);
  1773. /* If requested, send a signal to the application. */
  1774. if (send_sig) {
  1775. kill(child_pid, SIGWINCH);
  1776. }
  1777. }
  1778. static void update_bounds(void) {
  1779. struct decor_bounds bounds;
  1780. decor_get_bounds(window, &bounds);
  1781. decor_left_width = bounds.left_width;
  1782. decor_top_height = bounds.top_height;
  1783. decor_right_width = bounds.right_width;
  1784. decor_bottom_height = bounds.bottom_height;
  1785. decor_width = bounds.width;
  1786. decor_height = bounds.height;
  1787. }
  1788. /* Handle window resize event. */
  1789. static void resize_finish(int width, int height) {
  1790. static int resize_attempts = 0;
  1791. int extra_x = 0;
  1792. int extra_y = 0;
  1793. /* Calculate window size */
  1794. if (!_no_frame) {
  1795. update_bounds();
  1796. extra_x = decor_width;
  1797. extra_y = decor_height + menu_bar_height;
  1798. }
  1799. int t_window_width = width - extra_x;
  1800. int t_window_height = height - extra_y;
  1801. /* Prevent the terminal from becoming too small. */
  1802. if (t_window_width < char_width * 20 || t_window_height < char_height * 10) {
  1803. resize_attempts++;
  1804. int n_width = extra_x + max(char_width * 20, t_window_width);
  1805. int n_height = extra_y + max(char_height * 10, t_window_height);
  1806. yutani_window_resize_offer(yctx, window, n_width, n_height);
  1807. return;
  1808. }
  1809. /* If requested, ensure the terminal resizes to a fixed size based on the cell size. */
  1810. if (!_free_size && ((t_window_width % char_width != 0 || t_window_height % char_height != 0) && resize_attempts < 3)) {
  1811. resize_attempts++;
  1812. int n_width = extra_x + t_window_width - (t_window_width % char_width);
  1813. int n_height = extra_y + t_window_height - (t_window_height % char_height);
  1814. yutani_window_resize_offer(yctx, window, n_width, n_height);
  1815. return;
  1816. }
  1817. resize_attempts = 0;
  1818. /* Accept new window size */
  1819. yutani_window_resize_accept(yctx, window, width, height);
  1820. window_width = window->width - extra_x;
  1821. window_height = window->height - extra_y;
  1822. /* Reinitialize the graphics library */
  1823. reinit_graphics_yutani(ctx, window);
  1824. /* Reinitialize the terminal buffer and ANSI library */
  1825. reinit(1);
  1826. /* We are done resizing. */
  1827. yutani_window_resize_done(yctx, window);
  1828. yutani_flip(yctx, window);
  1829. }
  1830. /* Insert a mouse event sequence into the PTY */
  1831. static void mouse_event(int button, int x, int y) {
  1832. char buf[7];
  1833. sprintf(buf, "\033[M%c%c%c", button + 32, x + 33, y + 33);
  1834. handle_input_s(buf);
  1835. }
  1836. /* Handle Yutani messages */
  1837. static void * handle_incoming(void) {
  1838. yutani_msg_t * m = yutani_poll(yctx);
  1839. while (m) {
  1840. if (menu_process_event(yctx, m)) {
  1841. render_decors();
  1842. }
  1843. switch (m->type) {
  1844. case YUTANI_MSG_KEY_EVENT:
  1845. {
  1846. struct yutani_msg_key_event * ke = (void*)m->data;
  1847. int ret = (ke->event.action == KEY_ACTION_DOWN) && (ke->event.key);
  1848. key_event(ret, &ke->event);
  1849. }
  1850. break;
  1851. case YUTANI_MSG_WINDOW_FOCUS_CHANGE:
  1852. {
  1853. struct yutani_msg_window_focus_change * wf = (void*)m->data;
  1854. yutani_window_t * win = hashmap_get(yctx->windows, (void*)wf->wid);
  1855. if (win == window) {
  1856. win->focused = wf->focused;
  1857. render_decors();
  1858. }
  1859. }
  1860. break;
  1861. case YUTANI_MSG_WINDOW_CLOSE:
  1862. {
  1863. struct yutani_msg_window_close * wc = (void*)m->data;
  1864. if (wc->wid == window->wid) {
  1865. kill(child_pid, SIGKILL);
  1866. exit_application = 1;
  1867. }
  1868. }
  1869. break;
  1870. case YUTANI_MSG_SESSION_END:
  1871. {
  1872. kill(child_pid, SIGKILL);
  1873. exit_application = 1;
  1874. }
  1875. break;
  1876. case YUTANI_MSG_RESIZE_OFFER:
  1877. {
  1878. struct yutani_msg_window_resize * wr = (void*)m->data;
  1879. resize_finish(wr->width, wr->height);
  1880. }
  1881. break;
  1882. case YUTANI_MSG_CLIPBOARD:
  1883. {
  1884. struct yutani_msg_clipboard * cb = (void *)m->data;
  1885. if (selection_text) {
  1886. free(selection_text);
  1887. }
  1888. if (*cb->content == '\002') {
  1889. int size = atoi(&cb->content[2]);
  1890. FILE * clipboard = yutani_open_clipboard(yctx);
  1891. selection_text = malloc(size + 1);
  1892. fread(selection_text, 1, size, clipboard);
  1893. selection_text[size] = '\0';
  1894. fclose(clipboard);
  1895. } else {
  1896. selection_text = malloc(cb->size+1);
  1897. memcpy(selection_text, cb->content, cb->size);
  1898. selection_text[cb->size] = '\0';
  1899. }
  1900. handle_input_s(selection_text);
  1901. }
  1902. break;
  1903. case YUTANI_MSG_WINDOW_MOUSE_EVENT:
  1904. {
  1905. struct yutani_msg_window_mouse_event * me = (void*)m->data;
  1906. if (me->wid != window->wid) break;
  1907. if (!_no_frame) {
  1908. int decor_response = decor_handle_event(yctx, m);
  1909. switch (decor_response) {
  1910. case DECOR_CLOSE:
  1911. kill(child_pid, SIGKILL);
  1912. exit_application = 1;
  1913. break;
  1914. case DECOR_RIGHT:
  1915. /* right click in decoration, show appropriate menu */
  1916. decor_show_default_menu(window, window->x + me->new_x, window->y + me->new_y);
  1917. break;
  1918. default:
  1919. break;
  1920. }
  1921. menu_bar_mouse_event(yctx, window, &terminal_menu_bar, me, me->new_x, me->new_y);
  1922. }
  1923. if (me->new_x < 0 || me->new_y < 0) break;
  1924. if (!_no_frame) {
  1925. if (me->new_x >= (int)window_width + (int)decor_width) break;
  1926. if (me->new_y < (int)decor_top_height+menu_bar_height) break;
  1927. if (me->new_y >= (int)(window_height + decor_top_height+menu_bar_height)) break;
  1928. if (me->new_x < (int)decor_left_width) break;
  1929. if (me->new_x >= (int)(window_width + decor_left_width)) break;
  1930. } else {
  1931. if (me->new_x >= (int)window_width) break;
  1932. if (me->new_y >= (int)window_height) break;
  1933. }
  1934. int new_x = me->new_x;
  1935. int new_y = me->new_y;
  1936. if (!_no_frame) {
  1937. new_x -= decor_left_width;
  1938. new_y -= decor_top_height+menu_bar_height;
  1939. }
  1940. /* Convert from coordinate to cell positon */
  1941. new_x /= char_width;
  1942. new_y /= char_height;
  1943. if (new_x < 0 || new_y < 0) break;
  1944. if (new_x >= term_width || new_y >= term_height) break;
  1945. /* Map Cursor Action */
  1946. if (ansi_state->mouse_on) {
  1947. if (me->buttons & YUTANI_MOUSE_SCROLL_UP) {
  1948. mouse_event(32+32, new_x, new_y);
  1949. } else if (me->buttons & YUTANI_MOUSE_SCROLL_DOWN) {
  1950. mouse_event(32+32+1, new_x, new_y);
  1951. }
  1952. if (me->buttons != button_state) {
  1953. /* Figure out what changed */
  1954. if (me->buttons & YUTANI_MOUSE_BUTTON_LEFT &&
  1955. !(button_state & YUTANI_MOUSE_BUTTON_LEFT))
  1956. mouse_event(0, new_x, new_y);
  1957. if (me->buttons & YUTANI_MOUSE_BUTTON_MIDDLE &&
  1958. !(button_state & YUTANI_MOUSE_BUTTON_MIDDLE))
  1959. mouse_event(1, new_x, new_y);
  1960. if (me->buttons & YUTANI_MOUSE_BUTTON_RIGHT &&
  1961. !(button_state & YUTANI_MOUSE_BUTTON_RIGHT))
  1962. mouse_event(2, new_x, new_y);
  1963. if (!(me->buttons & YUTANI_MOUSE_BUTTON_LEFT) &&
  1964. button_state & YUTANI_MOUSE_BUTTON_LEFT)
  1965. mouse_event(3, new_x, new_y);
  1966. if (!(me->buttons & YUTANI_MOUSE_BUTTON_MIDDLE) &&
  1967. button_state & YUTANI_MOUSE_BUTTON_MIDDLE)
  1968. mouse_event(3, new_x, new_y);
  1969. if (!(me->buttons & YUTANI_MOUSE_BUTTON_RIGHT) &&
  1970. button_state & YUTANI_MOUSE_BUTTON_RIGHT)
  1971. mouse_event(3, new_x, new_y);
  1972. last_mouse_x = new_x;
  1973. last_mouse_y = new_y;
  1974. button_state = me->buttons;
  1975. } else if (ansi_state->mouse_on == 2) {
  1976. /* Report motion for pressed buttons */
  1977. if (last_mouse_x == new_x && last_mouse_y == new_y) break;
  1978. if (button_state & YUTANI_MOUSE_BUTTON_LEFT) mouse_event(32, new_x, new_y);
  1979. if (button_state & YUTANI_MOUSE_BUTTON_MIDDLE) mouse_event(33, new_x, new_y);
  1980. if (button_state & YUTANI_MOUSE_BUTTON_RIGHT) mouse_event(34, new_x, new_y);
  1981. last_mouse_x = new_x;
  1982. last_mouse_y = new_y;
  1983. }
  1984. } else {
  1985. if (me->command == YUTANI_MOUSE_EVENT_DOWN && me->buttons & YUTANI_MOUSE_BUTTON_LEFT) {
  1986. redraw_scrollback();
  1987. selection_start_x = new_x;
  1988. selection_start_y = new_y;
  1989. selection_end_x = new_x;
  1990. selection_end_y = new_y;
  1991. selection = 1;
  1992. redraw_selection();
  1993. display_flip();
  1994. }
  1995. if (me->command == YUTANI_MOUSE_EVENT_DRAG && me->buttons & YUTANI_MOUSE_BUTTON_LEFT ){
  1996. int old_end_x = selection_end_x;
  1997. int old_end_y = selection_end_y;
  1998. selection_end_x = new_x;
  1999. selection_end_y = new_y;
  2000. redraw_new_selection(old_end_x, old_end_y);
  2001. display_flip();
  2002. }
  2003. if (me->command == YUTANI_MOUSE_EVENT_RAISE) {
  2004. if (me->new_x == me->old_x && me->new_y == me->old_y) {
  2005. selection = 0;
  2006. term_redraw_all();
  2007. redraw_scrollback();
  2008. display_flip();
  2009. } /* else selection */
  2010. }
  2011. if (me->buttons & YUTANI_MOUSE_SCROLL_UP) {
  2012. scroll_up(5);
  2013. } else if (me->buttons & YUTANI_MOUSE_SCROLL_DOWN) {
  2014. scroll_down(5);
  2015. } else if (me->buttons & YUTANI_MOUSE_BUTTON_RIGHT) {
  2016. if (!menu_right_click->window) {
  2017. menu_show(menu_right_click, yctx);
  2018. if (window->x + me->new_x + menu_right_click->window->width > yctx->display_width) {
  2019. yutani_window_move(yctx, menu_right_click->window, window->x + me->new_x - menu_right_click->window->width, window->y + me->new_y);
  2020. } else {
  2021. yutani_window_move(yctx, menu_right_click->window, window->x + me->new_x, window->y + me->new_y);
  2022. }
  2023. }
  2024. }
  2025. }
  2026. }
  2027. break;
  2028. default:
  2029. break;
  2030. }
  2031. free(m);
  2032. m = yutani_poll_async(yctx);
  2033. }
  2034. return NULL;
  2035. }
  2036. /*
  2037. * Menu Actions
  2038. */
  2039. /* File > Exit */
  2040. static void _menu_action_exit(struct MenuEntry * self) {
  2041. kill(child_pid, SIGKILL);
  2042. exit_application = 1;
  2043. }
  2044. /* We need to track these so we can retitle both of them */
  2045. static struct MenuEntry * _menu_toggle_borders_context = NULL;
  2046. static struct MenuEntry * _menu_toggle_borders_bar = NULL;
  2047. static void _menu_action_hide_borders(struct MenuEntry * self) {
  2048. _no_frame = !(_no_frame);
  2049. window_width = window->width - decor_width * (!_no_frame);
  2050. window_height = window->height - (decor_height + menu_bar_height) * (!_no_frame);
  2051. menu_update_title(_menu_toggle_borders_context, _no_frame ? "Show borders" : "Hide borders");
  2052. menu_update_title(_menu_toggle_borders_bar, _no_frame ? "Show borders" : "Hide borders");
  2053. reinit(1);
  2054. }
  2055. static void _menu_action_toggle_sdf(struct MenuEntry * self) {
  2056. _use_aa = !(_use_aa);
  2057. menu_update_title(self, _use_aa ? "Bitmap font" : "Anti-aliased font");
  2058. reinit(1);
  2059. }
  2060. static void _menu_action_show_about(struct MenuEntry * self) {
  2061. char about_cmd[1024] = "\0";
  2062. strcat(about_cmd, "about \"About Terminal\" /usr/share/icons/48/utilities-terminal.bmp \"ToaruOS Terminal\" \"(C) 2013-2018 K. Lange\n-\nPart of ToaruOS, which is free software\nreleased under the NCSA/University of Illinois\nlicense.\n-\n%https://toaruos.org\n%https://gitlab.com/toaruos\" ");
  2063. char coords[100];
  2064. sprintf(coords, "%d %d &", (int)window->x + (int)window->width / 2, (int)window->y + (int)window->height / 2);
  2065. strcat(about_cmd, coords);
  2066. system(about_cmd);
  2067. render_decors();
  2068. }
  2069. static void _menu_action_show_help(struct MenuEntry * self) {
  2070. system("help-browser terminal.trt &");
  2071. render_decors();
  2072. }
  2073. static void _menu_action_copy(struct MenuEntry * self) {
  2074. copy_selection();
  2075. }
  2076. static void _menu_action_paste(struct MenuEntry * self) {
  2077. yutani_special_request(yctx, NULL, YUTANI_SPECIAL_REQUEST_CLIPBOARD);
  2078. }
  2079. static void _menu_action_set_scale(struct MenuEntry * self) {
  2080. struct MenuEntry_Normal * _self = (struct MenuEntry_Normal *)self;
  2081. if (!_self->action) {
  2082. scale_fonts = 0;
  2083. font_scaling = 1.0;
  2084. } else {
  2085. scale_fonts = 1;
  2086. font_scaling = atof(_self->action);
  2087. }
  2088. reinit(1);
  2089. }
  2090. int main(int argc, char ** argv) {
  2091. window_width = char_width * 80;
  2092. window_height = char_height * 24;
  2093. static struct option long_opts[] = {
  2094. {"fullscreen", no_argument, 0, 'F'},
  2095. {"bitmap", no_argument, 0, 'b'},
  2096. {"scale", required_argument, 0, 's'},
  2097. {"help", no_argument, 0, 'h'},
  2098. {"grid", no_argument, 0, 'x'},
  2099. {"no-frame", no_argument, 0, 'n'},
  2100. {"geometry", required_argument, 0, 'g'},
  2101. {"no-ft", no_argument, 0, 'f'},
  2102. {0,0,0,0}
  2103. };
  2104. /* Read some arguments */
  2105. int index, c;
  2106. while ((c = getopt_long(argc, argv, "bhxnfFls:g:", long_opts, &index)) != -1) {
  2107. if (!c) {
  2108. if (long_opts[index].flag == 0) {
  2109. c = long_opts[index].val;
  2110. }
  2111. }
  2112. switch (c) {
  2113. case 'x':
  2114. _free_size = 0;
  2115. break;
  2116. case 'n':
  2117. _no_frame = 1;
  2118. break;
  2119. case 'f':
  2120. _force_no_ft = 1;
  2121. break;
  2122. case 'F':
  2123. _fullscreen = 1;
  2124. _no_frame = 1;
  2125. break;
  2126. case 'b':
  2127. _use_aa = 0;
  2128. break;
  2129. case 'h':
  2130. usage(argv);
  2131. return 0;
  2132. break;
  2133. case 's':
  2134. scale_fonts = 1;
  2135. font_scaling = atof(optarg);
  2136. break;
  2137. case 'g':
  2138. {
  2139. char * c = strstr(optarg, "x");
  2140. if (c) {
  2141. *c = '\0';
  2142. c++;
  2143. window_width = atoi(optarg);
  2144. window_height = atoi(c);
  2145. }
  2146. }
  2147. break;
  2148. case '?':
  2149. break;
  2150. default:
  2151. break;
  2152. }
  2153. }
  2154. if (!_force_no_ft) {
  2155. void * freetype = dlopen("libtoaru_ext_freetype_fonts.so", 0);
  2156. if (freetype) {
  2157. _have_freetype = 1;
  2158. freetype_set_font_face = dlsym(freetype, "freetype_set_font_face");
  2159. freetype_set_font_size = dlsym(freetype, "freetype_set_font_size");
  2160. freetype_draw_char = dlsym(freetype, "freetype_draw_char");
  2161. }
  2162. }
  2163. /* Initialize the windowing library */
  2164. yctx = yutani_init();
  2165. if (!yctx) {
  2166. fprintf(stderr, "%s: failed to connect to compositor\n", argv[0]);
  2167. return 1;
  2168. }
  2169. /* Full screen mode forces window size to be that the display server */
  2170. if (_fullscreen) {
  2171. window_width = yctx->display_width;
  2172. window_height = yctx->display_height;
  2173. }
  2174. if (_no_frame) {
  2175. window = yutani_window_create(yctx, window_width, window_height);
  2176. } else {
  2177. init_decorations();
  2178. struct decor_bounds bounds;
  2179. decor_get_bounds(NULL, &bounds);
  2180. window = yutani_window_create(yctx, window_width + bounds.width, window_height + bounds.height + menu_bar_height);
  2181. update_bounds();
  2182. }
  2183. if (_fullscreen) {
  2184. /* If fullscreen, assume we're always focused and put us on the bottom. */
  2185. yutani_set_stack(yctx, window, YUTANI_ZORDER_BOTTOM);
  2186. window->focused = 1;
  2187. } else {
  2188. window->focused = 0;
  2189. }
  2190. /* Set up menus */
  2191. terminal_menu_bar.entries = terminal_menu_entries;
  2192. terminal_menu_bar.redraw_callback = render_decors;
  2193. struct MenuEntry * _menu_exit = menu_create_normal("exit","exit","Exit", _menu_action_exit);
  2194. struct MenuEntry * _menu_copy = menu_create_normal(NULL, NULL, "Copy", _menu_action_copy);
  2195. struct MenuEntry * _menu_paste = menu_create_normal(NULL, NULL, "Paste", _menu_action_paste);
  2196. menu_right_click = menu_create();
  2197. menu_insert(menu_right_click, _menu_copy);
  2198. menu_insert(menu_right_click, _menu_paste);
  2199. menu_insert(menu_right_click, menu_create_separator());
  2200. _menu_toggle_borders_context = menu_create_normal(NULL, NULL, _no_frame ? "Show borders" : "Hide borders", _menu_action_hide_borders);
  2201. menu_insert(menu_right_click, _menu_toggle_borders_context);
  2202. menu_insert(menu_right_click, menu_create_separator());
  2203. menu_insert(menu_right_click, _menu_exit);
  2204. /* Menu Bar menus */
  2205. terminal_menu_bar.set = menu_set_create();
  2206. struct MenuList * m;
  2207. m = menu_create(); /* File */
  2208. menu_insert(m, _menu_exit);
  2209. menu_set_insert(terminal_menu_bar.set, "file", m);
  2210. m = menu_create();
  2211. menu_insert(m, _menu_copy);
  2212. menu_insert(m, _menu_paste);
  2213. menu_set_insert(terminal_menu_bar.set, "edit", m);
  2214. m = menu_create();
  2215. menu_insert(m, menu_create_normal(NULL, "0.75", "75%", _menu_action_set_scale));
  2216. menu_insert(m, menu_create_normal(NULL, NULL, "100%", _menu_action_set_scale));
  2217. menu_insert(m, menu_create_normal(NULL, "1.5", "150%", _menu_action_set_scale));
  2218. menu_insert(m, menu_create_normal(NULL, "2.0", "200%", _menu_action_set_scale));
  2219. menu_set_insert(terminal_menu_bar.set, "zoom", m);
  2220. m = menu_create();
  2221. _menu_toggle_borders_bar = menu_create_normal(NULL, NULL, _no_frame ? "Show borders" : "Hide borders", _menu_action_hide_borders);
  2222. menu_insert(m, _menu_toggle_borders_bar);
  2223. menu_insert(m, menu_create_submenu(NULL,"zoom","Set zoom..."));
  2224. menu_insert(m, menu_create_normal(NULL, NULL, _use_aa ? "Bitmap font" : "Anti-aliased font", _menu_action_toggle_sdf));
  2225. menu_set_insert(terminal_menu_bar.set, "view", m);
  2226. m = menu_create();
  2227. menu_insert(m, menu_create_normal("help","help","Contents", _menu_action_show_help));
  2228. menu_insert(m, menu_create_separator());
  2229. menu_insert(m, menu_create_normal("star","star","About Terminal", _menu_action_show_about));
  2230. menu_set_insert(terminal_menu_bar.set, "help", m);
  2231. scrollback_list = list_create();
  2232. images_list = list_create();
  2233. /* Initialize the graphics context */
  2234. ctx = init_graphics_yutani_double_buffer(window);
  2235. /* Clear to black */
  2236. draw_fill(ctx, rgba(0,0,0,0));
  2237. /* Move window to screen center (XXX maybe remove this and do better window placement elsewhere */
  2238. yutani_window_move(yctx, window, yctx->display_width / 2 - window->width / 2, yctx->display_height / 2 - window->height / 2);
  2239. /* Open a PTY */
  2240. openpty(&fd_master, &fd_slave, NULL, NULL, NULL);
  2241. terminal = fdopen(fd_slave, "w");
  2242. /* Initialize the terminal buffer and ANSI library for the first time. */
  2243. reinit(0);
  2244. /* Make sure we're not passing anything to stdin on the child */
  2245. fflush(stdin);
  2246. /* Fork off child */
  2247. child_pid = fork();
  2248. if (!child_pid) {
  2249. setsid();
  2250. /* Prepare stdin/out/err */
  2251. dup2(fd_slave, 0);
  2252. dup2(fd_slave, 1);
  2253. dup2(fd_slave, 2);
  2254. /* Set the TERM environment variable. */
  2255. putenv("TERM=toaru");
  2256. /* Execute requested initial process */
  2257. if (argv[optind] != NULL) {
  2258. /* Run something specified by the terminal startup */
  2259. char * tokens[] = {argv[optind], NULL};
  2260. execvp(tokens[0], tokens);
  2261. fprintf(stderr, "Failed to launch requested startup application.\n");
  2262. } else {
  2263. /* Run the user's shell */
  2264. char * shell = getenv("SHELL");
  2265. if (!shell) shell = "/bin/sh"; /* fallback */
  2266. char * tokens[] = {shell,NULL};
  2267. execvp(tokens[0], tokens);
  2268. exit(1);
  2269. }
  2270. /* Failed to start */
  2271. exit_application = 1;
  2272. return 1;
  2273. } else {
  2274. /* Set up fswait to check Yutani and the PTY master */
  2275. int fds[2] = {fileno(yctx->sock), fd_master};
  2276. /* PTY read buffer */
  2277. unsigned char buf[1024];
  2278. while (!exit_application) {
  2279. /* Wait for something to happen. */
  2280. int index = fswait2(2,fds,200);
  2281. /* Check if the child application has closed. */
  2282. check_for_exit();
  2283. if (index == 1) {
  2284. /* Read from PTY */
  2285. maybe_flip_cursor();
  2286. int r = read(fd_master, buf, 1024);
  2287. for (int i = 0; i < r; ++i) {
  2288. ansi_put(ansi_state, buf[i]);
  2289. }
  2290. display_flip();
  2291. } else if (index == 0) {
  2292. /* Handle Yutani events. */
  2293. maybe_flip_cursor();
  2294. handle_incoming();
  2295. } else if (index == 2) {
  2296. /* Timeout, flip the cursor. */
  2297. maybe_flip_cursor();
  2298. }
  2299. }
  2300. }
  2301. /* Windows will close automatically on exit. */
  2302. return 0;
  2303. }