terminal.c 73 KB

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