terminal.c 67 KB

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