help-browser.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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) 2018 K. Lange
  5. *
  6. * help-browser - Display documentation.
  7. *
  8. * This is a work-in-progress reimplementation of the help browser
  9. * from mainline ToaruOS. It is currently incomplete.
  10. *
  11. * Eventually, this should be a rich text document browser, almost
  12. * akin to a web browser. Right now it just says "Hello, world."
  13. */
  14. #include <stdio.h>
  15. #include <unistd.h>
  16. #include <toaru/yutani.h>
  17. #include <toaru/graphics.h>
  18. #include <toaru/decorations.h>
  19. #include <toaru/menu.h>
  20. #include <toaru/sdf.h>
  21. #include <toaru/markup.h>
  22. #define APPLICATION_TITLE "Help Browser"
  23. static yutani_t * yctx;
  24. static yutani_window_t * main_window;
  25. static gfx_context_t * ctx;
  26. static int application_running = 1;
  27. static gfx_context_t * contents = NULL;
  28. static sprite_t * contents_sprite = NULL;
  29. static char * current_topic = "This is the ToaruOS Help Browser. No help document is currently loaded.";
  30. /* Markup Renderer { */
  31. #define BASE_X 0
  32. #define BASE_Y 0
  33. #define LINE_HEIGHT 20
  34. static gfx_context_t * nctx = NULL;
  35. static int cursor_y = 0;
  36. static int cursor_x = 0;
  37. static list_t * state = NULL;
  38. static int current_state = 0;
  39. static int size = 16;
  40. struct Char {
  41. char c; /* TODO: unicode */
  42. char state;
  43. };
  44. //static list_t * lines = NULL;
  45. static list_t * buffer = NULL;
  46. static int state_to_font(int current_state) {
  47. if (current_state & (1 << 0)) {
  48. if (current_state & (1 << 1)) {
  49. return SDF_FONT_BOLD_OBLIQUE;
  50. }
  51. return SDF_FONT_BOLD;
  52. } else if (current_state & (1 << 1)) {
  53. return SDF_FONT_OBLIQUE;
  54. }
  55. return SDF_FONT_THIN;
  56. }
  57. static int buffer_width(list_t * buffer) {
  58. int out = 0;
  59. foreach(node, buffer) {
  60. struct Char * c = node->value;
  61. char tmp[2] = {c->c, '\0'};
  62. out += draw_sdf_string_width(tmp, size, state_to_font(c->state));
  63. }
  64. return out;
  65. }
  66. static int draw_buffer(list_t * buffer) {
  67. int x = 0;
  68. while (buffer->length) {
  69. node_t * node = list_dequeue(buffer);
  70. struct Char * c = node->value;
  71. char tmp[2] = { c->c, '\0' };
  72. x += draw_sdf_string(nctx, cursor_x + x, cursor_y, tmp, size, 0xFF000000, state_to_font(c->state));
  73. free(c);
  74. free(node);
  75. }
  76. x += 4;
  77. return x;
  78. }
  79. static void write_buffer(void) {
  80. if (buffer_width(buffer) + cursor_x > nctx->width) {
  81. cursor_x = BASE_X;
  82. cursor_y += LINE_HEIGHT;
  83. }
  84. cursor_x += draw_buffer(buffer);
  85. }
  86. static int parser_open(struct markup_state * self, void * user, struct markup_tag * tag) {
  87. if (!strcmp(tag->name, "b")) {
  88. list_insert(state, (void*)current_state);
  89. current_state |= (1 << 0);
  90. } else if (!strcmp(tag->name, "i")) {
  91. list_insert(state, (void*)current_state);
  92. current_state |= (1 << 1);
  93. } else if (!strcmp(tag->name, "br")) {
  94. write_buffer();
  95. cursor_x = BASE_X;
  96. cursor_y += LINE_HEIGHT;
  97. }
  98. markup_free_tag(tag);
  99. return 0;
  100. }
  101. static int parser_close(struct markup_state * self, void * user, char * tag_name) {
  102. if (!strcmp(tag_name, "b")) {
  103. node_t * nstate = list_pop(state);
  104. current_state = (int)nstate->value;
  105. free(nstate);
  106. } else if (!strcmp(tag_name, "i")) {
  107. node_t * nstate = list_pop(state);
  108. current_state = (int)nstate->value;
  109. free(nstate);
  110. }
  111. return 0;
  112. }
  113. static int parser_data(struct markup_state * self, void * user, char * data) {
  114. char * c = data;
  115. while (*c) {
  116. if (*c == ' ') {
  117. if (buffer->length) {
  118. write_buffer();
  119. }
  120. } else {
  121. struct Char * ch = malloc(sizeof(struct Char));
  122. ch->c = *c;
  123. ch->state = current_state;
  124. list_insert(buffer, ch);
  125. }
  126. c++;
  127. }
  128. return 0;
  129. }
  130. /* } End Markup Renderer */
  131. static struct menu_bar menu_bar = {0};
  132. static struct menu_bar_entries menu_entries[] = {
  133. {"File", "file"},
  134. {"Go", "go"},
  135. {"Help", "help"},
  136. {NULL, NULL},
  137. };
  138. static void _menu_action_exit(struct MenuEntry * entry) {
  139. application_running = 0;
  140. }
  141. static void reinitialize_contents(void) {
  142. if (contents) {
  143. free(contents);
  144. }
  145. if (contents_sprite) {
  146. sprite_free(contents_sprite);
  147. }
  148. /* Calculate height for current directory */
  149. int calculated_height = 200;
  150. struct decor_bounds bounds;
  151. decor_get_bounds(main_window, &bounds);
  152. contents_sprite = create_sprite(main_window->width - bounds.width, calculated_height, ALPHA_EMBEDDED);
  153. contents = init_graphics_sprite(contents_sprite);
  154. draw_fill(contents, rgb(255,255,255));
  155. nctx = contents;
  156. struct markup_state * parser = markup_init(NULL, parser_open, parser_close, parser_data);
  157. cursor_y = BASE_Y;
  158. cursor_x = BASE_X;
  159. state = list_create();
  160. buffer = list_create();
  161. char * str = current_topic;
  162. while (*str) {
  163. if (markup_parse(parser, *str++)) {
  164. fprintf(stderr,"There was an error.\n");
  165. return;
  166. }
  167. }
  168. markup_finish(parser);
  169. write_buffer();
  170. list_free(state);
  171. free(state);
  172. free(buffer);
  173. }
  174. static void redraw_window(void) {
  175. draw_fill(ctx, rgb(255,255,255));
  176. render_decorations(main_window, ctx, APPLICATION_TITLE);
  177. struct decor_bounds bounds;
  178. decor_get_bounds(main_window, &bounds);
  179. menu_bar.x = bounds.left_width;
  180. menu_bar.y = bounds.top_height;
  181. menu_bar.width = ctx->width - bounds.width;
  182. menu_bar.window = main_window;
  183. menu_bar_render(&menu_bar, ctx);
  184. gfx_clear_clip(ctx);
  185. gfx_add_clip(ctx, bounds.left_width, bounds.top_height + MENU_BAR_HEIGHT, ctx->width - bounds.width, ctx->height - MENU_BAR_HEIGHT - bounds.height);
  186. draw_sprite(ctx, contents_sprite, bounds.left_width, bounds.top_height + MENU_BAR_HEIGHT);
  187. gfx_clear_clip(ctx);
  188. gfx_add_clip(ctx, 0, 0, ctx->width, ctx->height);
  189. flip(ctx);
  190. yutani_flip(yctx, main_window);
  191. }
  192. static void resize_finish(int w, int h) {
  193. int height_changed = (main_window->width != (unsigned int)w);
  194. yutani_window_resize_accept(yctx, main_window, w, h);
  195. reinit_graphics_yutani(ctx, main_window);
  196. if (height_changed) {
  197. reinitialize_contents();
  198. }
  199. redraw_window();
  200. yutani_window_resize_done(yctx, main_window);
  201. yutani_flip(yctx, main_window);
  202. }
  203. static void navigate(const char * t) {
  204. if (!strcmp(t,"file-browser.trt")) {
  205. current_topic =
  206. "<b>File Browser</b><br />"
  207. "The File Browser shows files. Double click to navigate through the file system.";
  208. } else if (!strcmp(t,"terminal.trt")) {
  209. current_topic =
  210. "<b>Terminal</b><br />"
  211. "It's a terminal emulator. Supports a large set of xterm escapes, plus 24-bit color.";
  212. } else if (!strcmp(t,"help_browser.trt")) {
  213. current_topic =
  214. "<b>Help Browser</b><br />"
  215. "A bit meta, reading about the Help Browser from within the Help Browser...<br />"
  216. "This is an incomplete port of the original Python Help Browser, which was, effectively "
  217. "a very bad web browser, built off of the expanding text label widget library.";
  218. } else if (!strcmp(t,"package-manager.trt")) {
  219. current_topic =
  220. "<b>Package Manager</b><br />"
  221. "Install additional third-party software through packages from the ToaruOS Package Repository. "
  222. "Packages can be installed by double-clicking. Dependencies will automatically be installed. "
  223. "At this time it is not possible to remove packages.";
  224. } else if (!strcmp(t,"0_index.trt")) {
  225. current_topic =
  226. "<b>Welcome!</b><br />"
  227. "Welcome to ToaruOS. This Help Browser is still a bit experimental.";
  228. } else if (!strcmp(t,"special:contents")) {
  229. current_topic =
  230. "<i>A list of topics should go here, but, alas...</i>";
  231. } else {
  232. current_topic =
  233. "<i>No help document exists for this topic.</i>";
  234. }
  235. reinitialize_contents();
  236. redraw_window();
  237. }
  238. static void _menu_action_navigate(struct MenuEntry * entry) {
  239. /* go to entry->action */
  240. struct MenuEntry_Normal * _entry = (void*)entry;
  241. navigate(_entry->action);
  242. }
  243. #if 0
  244. static void _menu_action_back(struct MenuEntry * entry) {
  245. /* go back */
  246. }
  247. static void _menu_action_forward(struct MenuEntry * entry) {
  248. /* go forward */
  249. }
  250. #endif
  251. static void _menu_action_about(struct MenuEntry * entry) {
  252. /* Show About dialog */
  253. char about_cmd[1024] = "\0";
  254. strcat(about_cmd, "about \"About Help Browser\" /usr/share/icons/48/help.bmp \"ToaruOS Help Browser\" \"(C) 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://github.com/klange/toaruos\" ");
  255. char coords[100];
  256. sprintf(coords, "%d %d &", (int)main_window->x + (int)main_window->width / 2, (int)main_window->y + (int)main_window->height / 2);
  257. strcat(about_cmd, coords);
  258. system(about_cmd);
  259. redraw_window();
  260. }
  261. int main(int argc, char * argv[]) {
  262. yctx = yutani_init();
  263. init_decorations();
  264. main_window = yutani_window_create(yctx, 640, 480);
  265. yutani_window_move(yctx, main_window, yctx->display_width / 2 - main_window->width / 2, yctx->display_height / 2 - main_window->height / 2);
  266. ctx = init_graphics_yutani_double_buffer(main_window);
  267. yutani_window_advertise_icon(yctx, main_window, APPLICATION_TITLE, "help");
  268. menu_bar.entries = menu_entries;
  269. menu_bar.redraw_callback = redraw_window;
  270. menu_bar.set = menu_set_create();
  271. struct MenuList * m = menu_create(); /* File */
  272. menu_insert(m, menu_create_normal("exit",NULL,"Exit", _menu_action_exit));
  273. menu_set_insert(menu_bar.set, "file", m);
  274. m = menu_create(); /* Go */
  275. menu_insert(m, menu_create_normal("home","0_index.trt","Home",_menu_action_navigate));
  276. menu_insert(m, menu_create_normal("bookmark","special:contents","Topics",_menu_action_navigate));
  277. #if 0
  278. /* TODO: History */
  279. menu_insert(m, menu_create_separator());
  280. menu_insert(m, menu_create_normal("back",NULL,"Back",_menu_action_back));
  281. menu_insert(m, menu_create_normal("forward",NULL,"Forward",_menu_action_forward));
  282. #endif
  283. menu_set_insert(menu_bar.set, "go", m);
  284. m = menu_create();
  285. menu_insert(m, menu_create_normal("help","help_browser.trt","Contents",_menu_action_navigate));
  286. menu_insert(m, menu_create_separator());
  287. menu_insert(m, menu_create_normal("star",NULL,"About " APPLICATION_TITLE,_menu_action_about));
  288. menu_set_insert(menu_bar.set, "help", m);
  289. if (argc > 1) {
  290. navigate(argv[1]);
  291. } else {
  292. reinitialize_contents();
  293. redraw_window();
  294. }
  295. while (application_running) {
  296. yutani_msg_t * m = yutani_poll(yctx);
  297. while (m) {
  298. if (menu_process_event(yctx, m)) {
  299. redraw_window();
  300. }
  301. switch (m->type) {
  302. case YUTANI_MSG_KEY_EVENT:
  303. {
  304. struct yutani_msg_key_event * ke = (void*)m->data;
  305. if (ke->event.action == KEY_ACTION_DOWN && ke->wid == main_window->wid) {
  306. switch (ke->event.keycode) {
  307. case 'f':
  308. if (ke->event.modifiers & YUTANI_KEY_MODIFIER_ALT) {
  309. menu_bar_show_menu(yctx,main_window,&menu_bar,-1,&menu_entries[0]);
  310. }
  311. break;
  312. case 'g':
  313. if (ke->event.modifiers & YUTANI_KEY_MODIFIER_ALT) {
  314. menu_bar_show_menu(yctx,main_window,&menu_bar,-1,&menu_entries[1]);
  315. }
  316. break;
  317. case 'h':
  318. if (ke->event.modifiers & YUTANI_KEY_MODIFIER_ALT) {
  319. menu_bar_show_menu(yctx,main_window,&menu_bar,-1,&menu_entries[2]);
  320. }
  321. break;
  322. case 'q':
  323. _menu_action_exit(NULL);
  324. break;
  325. }
  326. }
  327. }
  328. break;
  329. case YUTANI_MSG_WINDOW_FOCUS_CHANGE:
  330. {
  331. struct yutani_msg_window_focus_change * wf = (void*)m->data;
  332. yutani_window_t * win = hashmap_get(yctx->windows, (void*)wf->wid);
  333. if (win == main_window) {
  334. win->focused = wf->focused;
  335. redraw_window();
  336. }
  337. }
  338. break;
  339. case YUTANI_MSG_RESIZE_OFFER:
  340. {
  341. struct yutani_msg_window_resize * wr = (void*)m->data;
  342. if (wr->wid == main_window->wid) {
  343. resize_finish(wr->width, wr->height);
  344. }
  345. }
  346. break;
  347. case YUTANI_MSG_WINDOW_MOUSE_EVENT:
  348. {
  349. struct yutani_msg_window_mouse_event * me = (void*)m->data;
  350. yutani_window_t * win = hashmap_get(yctx->windows, (void*)me->wid);
  351. if (win == main_window) {
  352. int result = decor_handle_event(yctx, m);
  353. switch (result) {
  354. case DECOR_CLOSE:
  355. _menu_action_exit(NULL);
  356. break;
  357. case DECOR_RIGHT:
  358. /* right click in decoration, show appropriate menu */
  359. decor_show_default_menu(main_window, main_window->x + me->new_x, main_window->y + me->new_y);
  360. break;
  361. default:
  362. /* Other actions */
  363. break;
  364. }
  365. /* Menu bar */
  366. menu_bar_mouse_event(yctx, main_window, &menu_bar, me, me->new_x, me->new_y);
  367. }
  368. }
  369. break;
  370. case YUTANI_MSG_WINDOW_CLOSE:
  371. case YUTANI_MSG_SESSION_END:
  372. _menu_action_exit(NULL);
  373. break;
  374. default:
  375. break;
  376. }
  377. free(m);
  378. m = yutani_poll_async(yctx);
  379. }
  380. }
  381. }