file-browser.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. * file-browser - Show directory listings.
  7. *
  8. * This is a basic graphical file navigator. It's based somewhat
  9. * on the original Python implementation. There's still a lot
  10. * of work to do here presentation-wise.
  11. */
  12. #include <stdio.h>
  13. #include <unistd.h>
  14. #include <dirent.h>
  15. #include <time.h>
  16. #include <math.h>
  17. #include <sys/stat.h>
  18. #include <sys/time.h>
  19. #include <toaru/yutani.h>
  20. #include <toaru/graphics.h>
  21. #include <toaru/decorations.h>
  22. #include <toaru/menu.h>
  23. #include <toaru/icon_cache.h>
  24. #include <toaru/list.h>
  25. #include <toaru/sdf.h>
  26. #define APPLICATION_TITLE "File Browser"
  27. static yutani_t * yctx;
  28. static yutani_window_t * main_window;
  29. static gfx_context_t * ctx;
  30. #define SCROLL_AMOUNT 20
  31. static int application_running = 1;
  32. static int show_hidden = 1;
  33. static int scroll_offset = 0;
  34. static int available_height = 0;
  35. static struct menu_bar menu_bar = {0};
  36. static struct menu_bar_entries menu_entries[] = {
  37. {"File", "file"},
  38. {"Go", "go"},
  39. {"Help", "help"},
  40. {NULL, NULL},
  41. };
  42. static void _menu_action_exit(struct MenuEntry * entry) {
  43. application_running = 0;
  44. }
  45. struct File {
  46. char name[256];
  47. char icon[256];
  48. char date[256];
  49. int type;
  50. };
  51. static gfx_context_t * contents = NULL;
  52. static sprite_t * contents_sprite = NULL;
  53. static char * last_directory = NULL;
  54. static int hilighted_offset = -1;
  55. static struct File ** file_pointers = NULL;
  56. static ssize_t file_pointers_len = 0;
  57. static int _close_enough(struct yutani_msg_window_mouse_event * me) {
  58. if (me->command == YUTANI_MOUSE_EVENT_RAISE && sqrt(pow(me->new_x - me->old_x, 2) + pow(me->new_y - me->old_y, 2)) < 10) {
  59. return 1;
  60. }
  61. return 0;
  62. }
  63. static void clear_offset(int offset) {
  64. draw_rectangle(contents, 0, offset * 24, contents->width, 24, rgb(255,255,255));
  65. }
  66. static void draw_file(struct File * f, int offset) {
  67. sprite_t * icon = icon_get_16(f->icon);
  68. draw_sprite(contents, icon, 2, offset * 24 + 2);
  69. if (offset == hilighted_offset) {
  70. draw_sprite_alpha_paint(contents, icon, 2, offset * 24 + 2, 0.5, rgb(72,167,255));
  71. draw_sdf_string(contents, 30, offset * 24, f->name, 16, rgb(72,167,255), SDF_FONT_THIN);
  72. } else {
  73. draw_sdf_string(contents, 30, offset * 24, f->name, 16, rgb(0,0,0), SDF_FONT_THIN);
  74. }
  75. }
  76. static struct File * get_file_at_offset(int offset) {
  77. if (offset >= 0 && offset < file_pointers_len) {
  78. return file_pointers[offset];
  79. }
  80. return NULL;
  81. }
  82. static void redraw_files(void) {
  83. for (int i = 0; i < file_pointers_len; ++i) {
  84. draw_file(file_pointers[i], i);
  85. }
  86. }
  87. static void load_directory(const char * path) {
  88. if (file_pointers) {
  89. for (int i = 0; i < file_pointers_len; ++i) {
  90. free(file_pointers[i]);
  91. }
  92. free(file_pointers);
  93. }
  94. DIR * dirp = opendir(path);
  95. if (!dirp) {
  96. /* Failed to open directory. Throw up a warning? */
  97. file_pointers = NULL;
  98. file_pointers_len = 0;
  99. return;
  100. }
  101. if (last_directory) {
  102. free(last_directory);
  103. }
  104. last_directory = strdup(path);
  105. /* Get the current time */
  106. #if 0
  107. struct tm * timeinfo;
  108. struct timeval now;
  109. gettimeofday(&now, NULL); //time(NULL);
  110. timeinfo = localtime((time_t *)&now.tv_sec);
  111. int this_year = timeinfo->tm_year;
  112. #endif
  113. list_t * file_list = list_create();
  114. struct dirent * ent = readdir(dirp);
  115. while (ent != NULL) {
  116. if (ent->d_name[0] == '.' &&
  117. (ent->d_name[1] == '\0' ||
  118. (ent->d_name[1] == '.' &&
  119. ent->d_name[2] == '\0'))) {
  120. /* skip . and .. */
  121. ent = readdir(dirp);
  122. continue;
  123. }
  124. if (show_hidden || (ent->d_name[0] != '.')) {
  125. struct File * f = malloc(sizeof(struct File));
  126. sprintf(f->name, "%s", ent->d_name); /* snprintf? copy min()? */
  127. struct stat statbuf;
  128. //struct stat statbufl;
  129. //char * link;
  130. char tmp[strlen(path)+strlen(ent->d_name)+2];
  131. sprintf(tmp, "%s/%s", path, ent->d_name);
  132. lstat(tmp, &statbuf);
  133. #if 0
  134. if (S_ISLNK(statbuf.st_mode)) {
  135. stat(tmp, &statbufl);
  136. f->link = malloc(4096);
  137. readlink(tmp, f->link, 4096);
  138. }
  139. #endif
  140. if (S_ISDIR(statbuf.st_mode)) {
  141. sprintf(f->icon, "folder");
  142. f->type = 1;
  143. } else {
  144. sprintf(f->icon, "file");
  145. f->type = 0;
  146. }
  147. list_insert(file_list, f);
  148. }
  149. ent = readdir(dirp);
  150. }
  151. closedir(dirp);
  152. /* create a an array to hold the files */
  153. file_pointers = malloc(sizeof(struct File *) * file_list->length);
  154. file_pointers_len = file_list->length;
  155. int i = 0;
  156. foreach (node, file_list) {
  157. file_pointers[i] = node->value;
  158. i++;
  159. }
  160. list_free(file_list);
  161. free(file_list);
  162. /* Sort files */
  163. int comparator(const void * c1, const void * c2) {
  164. const struct File * f1 = *(const struct File **)(c1);
  165. const struct File * f2 = *(const struct File **)(c2);
  166. if (f1->type == 1 && f2->type == 0) return -1;
  167. if (f1->type == 0 && f2->type == 1) return 1;
  168. return strcmp(f1->name, f2->name);
  169. }
  170. qsort(file_pointers, file_pointers_len, sizeof(struct File *), comparator);
  171. scroll_offset = 0;
  172. }
  173. static void reinitialize_contents(void) {
  174. if (contents) {
  175. free(contents);
  176. }
  177. if (contents_sprite) {
  178. sprite_free(contents_sprite);
  179. }
  180. /* Calculate height for current directory */
  181. int calculated_height = file_pointers_len * 24;
  182. struct decor_bounds bounds;
  183. decor_get_bounds(main_window, &bounds);
  184. contents_sprite = create_sprite(main_window->width - bounds.width, calculated_height, ALPHA_EMBEDDED);
  185. contents = init_graphics_sprite(contents_sprite);
  186. draw_fill(contents, rgb(255,255,255));
  187. /* Draw file entries */
  188. redraw_files();
  189. }
  190. static void redraw_window(void) {
  191. draw_fill(ctx, rgb(255,255,255));
  192. render_decorations(main_window, ctx, APPLICATION_TITLE);
  193. struct decor_bounds bounds;
  194. decor_get_bounds(main_window, &bounds);
  195. menu_bar.x = bounds.left_width;
  196. menu_bar.y = bounds.top_height;
  197. menu_bar.width = ctx->width - bounds.width;
  198. menu_bar.window = main_window;
  199. menu_bar_render(&menu_bar, ctx);
  200. gfx_clear_clip(ctx);
  201. gfx_add_clip(ctx, bounds.left_width, bounds.top_height + MENU_BAR_HEIGHT, ctx->width - bounds.width, available_height);
  202. draw_sprite(ctx, contents_sprite, bounds.left_width, bounds.top_height + MENU_BAR_HEIGHT - scroll_offset);
  203. gfx_clear_clip(ctx);
  204. gfx_add_clip(ctx, 0, 0, ctx->width, ctx->height);
  205. flip(ctx);
  206. yutani_flip(yctx, main_window);
  207. }
  208. static void resize_finish(int w, int h) {
  209. int width_changed = (main_window->width != (unsigned int)w);
  210. yutani_window_resize_accept(yctx, main_window, w, h);
  211. reinit_graphics_yutani(ctx, main_window);
  212. struct decor_bounds bounds;
  213. decor_get_bounds(main_window, &bounds);
  214. available_height = ctx->height - MENU_BAR_HEIGHT - bounds.height;
  215. if (width_changed) {
  216. reinitialize_contents();
  217. }
  218. if (available_height > contents->height) {
  219. scroll_offset = 0;
  220. } else {
  221. if (scroll_offset > contents->height - available_height) {
  222. scroll_offset = contents->height - available_height;
  223. }
  224. }
  225. redraw_window();
  226. yutani_window_resize_done(yctx, main_window);
  227. yutani_flip(yctx, main_window);
  228. }
  229. /* TODO */
  230. #if 0
  231. static void _menu_action_input_path(struct MenuEntry * entry) {
  232. }
  233. #endif
  234. static void _menu_action_navigate(struct MenuEntry * entry) {
  235. /* go to entry->action */
  236. struct MenuEntry_Normal * _entry = (void*)entry;
  237. load_directory(_entry->action);
  238. reinitialize_contents();
  239. redraw_window();
  240. }
  241. static void _menu_action_up(struct MenuEntry * entry) {
  242. /* go up */
  243. char tmp[1024];
  244. sprintf(tmp, "%s/..", last_directory);
  245. load_directory(tmp);
  246. reinitialize_contents();
  247. redraw_window();
  248. }
  249. static void _menu_action_help(struct MenuEntry * entry) {
  250. /* show help documentation */
  251. system("help-browser file-browser.trt &");
  252. redraw_window();
  253. }
  254. static void _menu_action_about(struct MenuEntry * entry) {
  255. /* Show About dialog */
  256. char about_cmd[1024] = "\0";
  257. strcat(about_cmd, "about \"About File Browser\" /usr/share/icons/48/folder.bmp \"ToaruOS File 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://gitlab.com/toaruos\" ");
  258. char coords[100];
  259. sprintf(coords, "%d %d &", (int)main_window->x + (int)main_window->width / 2, (int)main_window->y + (int)main_window->height / 2);
  260. strcat(about_cmd, coords);
  261. system(about_cmd);
  262. redraw_window();
  263. }
  264. int main(int argc, char * argv[]) {
  265. yctx = yutani_init();
  266. init_decorations();
  267. main_window = yutani_window_create(yctx, 800, 600);
  268. yutani_window_move(yctx, main_window, yctx->display_width / 2 - main_window->width / 2, yctx->display_height / 2 - main_window->height / 2);
  269. ctx = init_graphics_yutani_double_buffer(main_window);
  270. struct decor_bounds bounds;
  271. decor_get_bounds(main_window, &bounds);
  272. yutani_window_advertise_icon(yctx, main_window, APPLICATION_TITLE, "folder");
  273. menu_bar.entries = menu_entries;
  274. menu_bar.redraw_callback = redraw_window;
  275. menu_bar.set = menu_set_create();
  276. struct MenuList * m = menu_create(); /* File */
  277. menu_insert(m, menu_create_normal("exit",NULL,"Exit", _menu_action_exit));
  278. menu_set_insert(menu_bar.set, "file", m);
  279. m = menu_create(); /* Go */
  280. /* TODO implement input dialog for Path... */
  281. #if 0
  282. menu_insert(m, menu_create_normal("open",NULL,"Path...", _menu_action_input_path));
  283. menu_insert(m, menu_create_separator());
  284. #endif
  285. menu_insert(m, menu_create_normal("home",getenv("HOME"),"Home",_menu_action_navigate));
  286. menu_insert(m, menu_create_normal(NULL,"/","File System",_menu_action_navigate));
  287. menu_insert(m, menu_create_normal("up",NULL,"Up",_menu_action_up));
  288. menu_set_insert(menu_bar.set, "go", m);
  289. m = menu_create();
  290. menu_insert(m, menu_create_normal("help",NULL,"Contents",_menu_action_help));
  291. menu_insert(m, menu_create_separator());
  292. menu_insert(m, menu_create_normal("star",NULL,"About " APPLICATION_TITLE,_menu_action_about));
  293. menu_set_insert(menu_bar.set, "help", m);
  294. available_height = ctx->height - MENU_BAR_HEIGHT - bounds.height;
  295. load_directory("/usr/share");
  296. reinitialize_contents();
  297. redraw_window();
  298. while (application_running) {
  299. yutani_msg_t * m = yutani_poll(yctx);
  300. while (m) {
  301. if (menu_process_event(yctx, m)) {
  302. redraw_window();
  303. }
  304. switch (m->type) {
  305. case YUTANI_MSG_KEY_EVENT:
  306. {
  307. struct yutani_msg_key_event * ke = (void*)m->data;
  308. if (ke->event.action == KEY_ACTION_DOWN && ke->event.keycode == 'q') {
  309. _menu_action_exit(NULL);
  310. }
  311. }
  312. break;
  313. case YUTANI_MSG_WINDOW_FOCUS_CHANGE:
  314. {
  315. struct yutani_msg_window_focus_change * wf = (void*)m->data;
  316. yutani_window_t * win = hashmap_get(yctx->windows, (void*)wf->wid);
  317. if (win == main_window) {
  318. win->focused = wf->focused;
  319. redraw_window();
  320. }
  321. }
  322. break;
  323. case YUTANI_MSG_RESIZE_OFFER:
  324. {
  325. struct yutani_msg_window_resize * wr = (void*)m->data;
  326. if (wr->wid == main_window->wid) {
  327. resize_finish(wr->width, wr->height);
  328. }
  329. }
  330. break;
  331. case YUTANI_MSG_WINDOW_MOUSE_EVENT:
  332. {
  333. struct yutani_msg_window_mouse_event * me = (void*)m->data;
  334. yutani_window_t * win = hashmap_get(yctx->windows, (void*)me->wid);
  335. struct decor_bounds bounds;
  336. decor_get_bounds(win, &bounds);
  337. if (win == main_window) {
  338. int result = decor_handle_event(yctx, m);
  339. switch (result) {
  340. case DECOR_CLOSE:
  341. _menu_action_exit(NULL);
  342. break;
  343. case DECOR_RIGHT:
  344. /* right click in decoration, show appropriate menu */
  345. decor_show_default_menu(main_window, main_window->x + me->new_x, main_window->y + me->new_y);
  346. break;
  347. default:
  348. /* Other actions */
  349. break;
  350. }
  351. /* Menu bar */
  352. menu_bar_mouse_event(yctx, main_window, &menu_bar, me, me->new_x, me->new_y);
  353. if (me->new_y > (int)(bounds.top_height + MENU_BAR_HEIGHT) &&
  354. me->new_y < (int)(main_window->height - bounds.bottom_height) &&
  355. me->new_x > (int)(bounds.left_width) &&
  356. me->new_x < (int)(main_window->width - bounds.right_width)) {
  357. if (me->buttons & YUTANI_MOUSE_SCROLL_UP) {
  358. /* Scroll up */
  359. scroll_offset -= SCROLL_AMOUNT;
  360. if (scroll_offset < 0) {
  361. scroll_offset = 0;
  362. }
  363. redraw_window();
  364. } else if (me->buttons & YUTANI_MOUSE_SCROLL_DOWN) {
  365. if (available_height > contents->height) {
  366. scroll_offset = 0;
  367. } else {
  368. scroll_offset += SCROLL_AMOUNT;
  369. if (scroll_offset > contents->height - available_height) {
  370. scroll_offset = contents->height - available_height;
  371. }
  372. }
  373. redraw_window();
  374. }
  375. /* Get offset into contents */
  376. int y_into = me->new_y - bounds.top_height - MENU_BAR_HEIGHT + scroll_offset;
  377. int offset = y_into / 24;
  378. if (offset != hilighted_offset) {
  379. int old_offset = hilighted_offset;
  380. hilighted_offset = offset;
  381. if (old_offset != -1) {
  382. clear_offset(old_offset);
  383. struct File * f = get_file_at_offset(old_offset);
  384. if (f) {
  385. clear_offset(old_offset);
  386. draw_file(f, old_offset);
  387. }
  388. }
  389. struct File * f = get_file_at_offset(hilighted_offset);
  390. if (f) {
  391. clear_offset(hilighted_offset);
  392. draw_file(f, hilighted_offset);
  393. }
  394. redraw_window();
  395. }
  396. if (me->command == YUTANI_MOUSE_EVENT_CLICK || _close_enough(me)) {
  397. struct File * f = get_file_at_offset(hilighted_offset);
  398. if (f && f->type == 1) {
  399. char tmp[1024];
  400. sprintf(tmp,"%s/%s", last_directory, f->name);
  401. load_directory(tmp);
  402. reinitialize_contents();
  403. redraw_window();
  404. }
  405. }
  406. } else {
  407. int old_offset = hilighted_offset;
  408. hilighted_offset = -1;
  409. if (old_offset != -1) {
  410. clear_offset(old_offset);
  411. struct File * f = get_file_at_offset(old_offset);
  412. if (f) {
  413. clear_offset(old_offset);
  414. draw_file(f, old_offset);
  415. }
  416. redraw_window();
  417. }
  418. }
  419. }
  420. }
  421. break;
  422. case YUTANI_MSG_WINDOW_CLOSE:
  423. case YUTANI_MSG_SESSION_END:
  424. _menu_action_exit(NULL);
  425. break;
  426. default:
  427. break;
  428. }
  429. free(m);
  430. m = yutani_poll_async(yctx);
  431. }
  432. }
  433. }