terminal.c 73 KB

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