terminal.c 72 KB

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