terminal.c 63 KB

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