terminal.c 57 KB

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