terminal.c 57 KB

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