file-browser.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. contents_sprite = create_sprite(main_window->width - decor_width(), calculated_height, ALPHA_EMBEDDED);
  183. contents = init_graphics_sprite(contents_sprite);
  184. draw_fill(contents, rgb(255,255,255));
  185. /* Draw file entries */
  186. redraw_files();
  187. }
  188. static void redraw_window(void) {
  189. draw_fill(ctx, rgb(255,255,255));
  190. render_decorations(main_window, ctx, APPLICATION_TITLE);
  191. menu_bar.x = decor_left_width;
  192. menu_bar.y = decor_top_height;
  193. menu_bar.width = ctx->width - decor_width();
  194. menu_bar.window = main_window;
  195. menu_bar_render(&menu_bar, ctx);
  196. gfx_clear_clip(ctx);
  197. gfx_add_clip(ctx, decor_left_width, decor_top_height + MENU_BAR_HEIGHT, ctx->width - decor_width(), available_height);
  198. draw_sprite(ctx, contents_sprite, decor_left_width, decor_top_height + MENU_BAR_HEIGHT - scroll_offset);
  199. gfx_clear_clip(ctx);
  200. gfx_add_clip(ctx, 0, 0, ctx->width, ctx->height);
  201. flip(ctx);
  202. yutani_flip(yctx, main_window);
  203. }
  204. static void resize_finish(int w, int h) {
  205. int width_changed = (main_window->width != (unsigned int)w);
  206. yutani_window_resize_accept(yctx, main_window, w, h);
  207. reinit_graphics_yutani(ctx, main_window);
  208. available_height = ctx->height - MENU_BAR_HEIGHT - decor_height();
  209. if (width_changed) {
  210. reinitialize_contents();
  211. }
  212. if (available_height > contents->height) {
  213. scroll_offset = 0;
  214. } else {
  215. if (scroll_offset > contents->height - available_height) {
  216. scroll_offset = contents->height - available_height;
  217. }
  218. }
  219. redraw_window();
  220. yutani_window_resize_done(yctx, main_window);
  221. yutani_flip(yctx, main_window);
  222. }
  223. /* TODO */
  224. #if 0
  225. static void _menu_action_input_path(struct MenuEntry * entry) {
  226. }
  227. #endif
  228. static void _menu_action_navigate(struct MenuEntry * entry) {
  229. /* go to entry->action */
  230. struct MenuEntry_Normal * _entry = (void*)entry;
  231. load_directory(_entry->action);
  232. reinitialize_contents();
  233. redraw_window();
  234. }
  235. static void _menu_action_up(struct MenuEntry * entry) {
  236. /* go up */
  237. char tmp[1024];
  238. sprintf(tmp, "%s/..", last_directory);
  239. load_directory(tmp);
  240. reinitialize_contents();
  241. redraw_window();
  242. }
  243. static void _menu_action_help(struct MenuEntry * entry) {
  244. /* show help documentation */
  245. system("help-browser file-browser.trt &");
  246. redraw_window();
  247. }
  248. static void _menu_action_about(struct MenuEntry * entry) {
  249. /* Show About dialog */
  250. char about_cmd[1024] = "\0";
  251. 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\" ");
  252. char coords[100];
  253. sprintf(coords, "%d %d &", (int)main_window->x + (int)main_window->width / 2, (int)main_window->y + (int)main_window->height / 2);
  254. strcat(about_cmd, coords);
  255. system(about_cmd);
  256. redraw_window();
  257. }
  258. int main(int argc, char * argv[]) {
  259. yctx = yutani_init();
  260. init_decorations();
  261. main_window = yutani_window_create(yctx, 800, 600);
  262. yutani_window_move(yctx, main_window, yctx->display_width / 2 - main_window->width / 2, yctx->display_height / 2 - main_window->height / 2);
  263. ctx = init_graphics_yutani_double_buffer(main_window);
  264. yutani_window_advertise_icon(yctx, main_window, APPLICATION_TITLE, "folder");
  265. menu_bar.entries = menu_entries;
  266. menu_bar.redraw_callback = redraw_window;
  267. menu_bar.set = menu_set_create();
  268. struct MenuList * m = menu_create(); /* File */
  269. menu_insert(m, menu_create_normal("exit",NULL,"Exit", _menu_action_exit));
  270. menu_set_insert(menu_bar.set, "file", m);
  271. m = menu_create(); /* Go */
  272. /* TODO implement input dialog for Path... */
  273. #if 0
  274. menu_insert(m, menu_create_normal("open",NULL,"Path...", _menu_action_input_path));
  275. menu_insert(m, menu_create_separator());
  276. #endif
  277. menu_insert(m, menu_create_normal("home",getenv("HOME"),"Home",_menu_action_navigate));
  278. menu_insert(m, menu_create_normal(NULL,"/","File System",_menu_action_navigate));
  279. menu_insert(m, menu_create_normal("up",NULL,"Up",_menu_action_up));
  280. menu_set_insert(menu_bar.set, "go", m);
  281. m = menu_create();
  282. menu_insert(m, menu_create_normal("help",NULL,"Contents",_menu_action_help));
  283. menu_insert(m, menu_create_separator());
  284. menu_insert(m, menu_create_normal("star",NULL,"About " APPLICATION_TITLE,_menu_action_about));
  285. menu_set_insert(menu_bar.set, "help", m);
  286. available_height = ctx->height - MENU_BAR_HEIGHT - decor_height();
  287. load_directory("/usr/share");
  288. reinitialize_contents();
  289. redraw_window();
  290. while (application_running) {
  291. yutani_msg_t * m = yutani_poll(yctx);
  292. while (m) {
  293. if (menu_process_event(yctx, m)) {
  294. redraw_window();
  295. }
  296. switch (m->type) {
  297. case YUTANI_MSG_KEY_EVENT:
  298. {
  299. struct yutani_msg_key_event * ke = (void*)m->data;
  300. if (ke->event.action == KEY_ACTION_DOWN && ke->event.keycode == 'q') {
  301. _menu_action_exit(NULL);
  302. }
  303. }
  304. break;
  305. case YUTANI_MSG_WINDOW_FOCUS_CHANGE:
  306. {
  307. struct yutani_msg_window_focus_change * wf = (void*)m->data;
  308. yutani_window_t * win = hashmap_get(yctx->windows, (void*)wf->wid);
  309. if (win == main_window) {
  310. win->focused = wf->focused;
  311. redraw_window();
  312. }
  313. }
  314. break;
  315. case YUTANI_MSG_RESIZE_OFFER:
  316. {
  317. struct yutani_msg_window_resize * wr = (void*)m->data;
  318. if (wr->wid == main_window->wid) {
  319. resize_finish(wr->width, wr->height);
  320. }
  321. }
  322. break;
  323. case YUTANI_MSG_WINDOW_MOUSE_EVENT:
  324. {
  325. struct yutani_msg_window_mouse_event * me = (void*)m->data;
  326. yutani_window_t * win = hashmap_get(yctx->windows, (void*)me->wid);
  327. if (win == main_window) {
  328. int result = decor_handle_event(yctx, m);
  329. switch (result) {
  330. case DECOR_CLOSE:
  331. _menu_action_exit(NULL);
  332. break;
  333. case DECOR_RIGHT:
  334. /* right click in decoration, show appropriate menu */
  335. decor_show_default_menu(main_window, main_window->x + me->new_x, main_window->y + me->new_y);
  336. break;
  337. default:
  338. /* Other actions */
  339. break;
  340. }
  341. /* Menu bar */
  342. menu_bar_mouse_event(yctx, main_window, &menu_bar, me, me->new_x, me->new_y);
  343. if (me->new_y > (int)(decor_top_height + MENU_BAR_HEIGHT) &&
  344. me->new_y < (int)(main_window->height - decor_bottom_height) &&
  345. me->new_x > (int)(decor_left_width) &&
  346. me->new_x < (int)(main_window->width - decor_right_width)) {
  347. if (me->buttons & YUTANI_MOUSE_SCROLL_UP) {
  348. /* Scroll up */
  349. scroll_offset -= SCROLL_AMOUNT;
  350. if (scroll_offset < 0) {
  351. scroll_offset = 0;
  352. }
  353. redraw_window();
  354. } else if (me->buttons & YUTANI_MOUSE_SCROLL_DOWN) {
  355. if (available_height > contents->height) {
  356. scroll_offset = 0;
  357. } else {
  358. scroll_offset += SCROLL_AMOUNT;
  359. if (scroll_offset > contents->height - available_height) {
  360. scroll_offset = contents->height - available_height;
  361. }
  362. }
  363. redraw_window();
  364. }
  365. /* Get offset into contents */
  366. int y_into = me->new_y - decor_top_height - MENU_BAR_HEIGHT + scroll_offset;
  367. int offset = y_into / 24;
  368. if (offset != hilighted_offset) {
  369. int old_offset = hilighted_offset;
  370. hilighted_offset = offset;
  371. if (old_offset != -1) {
  372. clear_offset(old_offset);
  373. struct File * f = get_file_at_offset(old_offset);
  374. if (f) {
  375. clear_offset(old_offset);
  376. draw_file(f, old_offset);
  377. }
  378. }
  379. struct File * f = get_file_at_offset(hilighted_offset);
  380. if (f) {
  381. clear_offset(hilighted_offset);
  382. draw_file(f, hilighted_offset);
  383. }
  384. redraw_window();
  385. }
  386. if (me->command == YUTANI_MOUSE_EVENT_CLICK || _close_enough(me)) {
  387. struct File * f = get_file_at_offset(hilighted_offset);
  388. if (f && f->type == 1) {
  389. char tmp[1024];
  390. sprintf(tmp,"%s/%s", last_directory, f->name);
  391. load_directory(tmp);
  392. reinitialize_contents();
  393. redraw_window();
  394. }
  395. }
  396. } else {
  397. int old_offset = hilighted_offset;
  398. hilighted_offset = -1;
  399. if (old_offset != -1) {
  400. clear_offset(old_offset);
  401. struct File * f = get_file_at_offset(old_offset);
  402. if (f) {
  403. clear_offset(old_offset);
  404. draw_file(f, old_offset);
  405. }
  406. redraw_window();
  407. }
  408. }
  409. }
  410. }
  411. break;
  412. case YUTANI_MSG_WINDOW_CLOSE:
  413. case YUTANI_MSG_SESSION_END:
  414. _menu_action_exit(NULL);
  415. break;
  416. default:
  417. break;
  418. }
  419. free(m);
  420. m = yutani_poll_async(yctx);
  421. }
  422. }
  423. }