help-browser.c 12 KB

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