ucmd_files.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  1. /* files: a small graphical file manager built on the ngui toolkit.
  2. *
  3. * The window is a canvas window (SYS_WIN_CREATE), so it owns its pixels and
  4. * receives keys and pointer events. It browses the VFS: "/" lists the mounted
  5. * filesystems, everything else lists a directory through SYS_LIST_DIR. */
  6. #include "ucmd_lib.h"
  7. #include "ngui.h"
  8. #include "img.h"
  9. #define FM_PATH_MAX 128
  10. #define FM_MAX_ENTRIES 256
  11. #define FM_MAX_MOUNTS 8
  12. #define FM_ROW_H 22
  13. #define FM_TOOLBAR_H 30
  14. #define FM_PATH_H 26
  15. #define FM_STATUS_H 18
  16. #define FM_SIDEBAR_W 140
  17. #define FM_SCROLL_W 9
  18. #define FM_EDIT_MAX 16384
  19. #define FM_STATUS_MS 3000
  20. enum {
  21. ID_UP = 1,
  22. ID_HOME,
  23. ID_REFRESH,
  24. ID_NEW,
  25. ID_GO,
  26. ID_PATH,
  27. ID_NEW_INPUT,
  28. ID_CREATE,
  29. ID_CANCEL,
  30. };
  31. #define ACT_UP (1u << 0)
  32. #define ACT_HOME (1u << 1)
  33. #define ACT_REFRESH (1u << 2)
  34. #define ACT_NEW (1u << 3)
  35. #define ACT_NAV (1u << 4)
  36. #define ACT_LOAD (1u << 5)
  37. #define ACT_OPEN (1u << 6)
  38. #define ACT_PARENT (1u << 7)
  39. #define ACT_CREATE (1u << 8)
  40. #define ACT_CANCEL (1u << 9)
  41. #define ACT_CLOSE (1u << 10)
  42. struct fm_entry {
  43. char name[64];
  44. char detail[8];
  45. unsigned int size;
  46. unsigned int is_dir;
  47. unsigned int is_mount;
  48. };
  49. static struct fm_entry entries[FM_MAX_ENTRIES];
  50. static struct fm_entry load_entries[FM_MAX_ENTRIES];
  51. static unsigned int entry_count;
  52. static int truncated;
  53. static int selected;
  54. static char cwd[FM_PATH_MAX];
  55. static char action_path[FM_PATH_MAX];
  56. static struct sys_mount mounts[FM_MAX_MOUNTS];
  57. static unsigned int mount_count;
  58. static struct ngui ui;
  59. static struct ngui_input path_in;
  60. static struct ngui_input new_in;
  61. static struct ngui_scroll list_scroll;
  62. static int modal;
  63. static int last_click_index = -1;
  64. static unsigned int last_click_ms;
  65. static char status_text[96];
  66. static unsigned int status_until;
  67. static int status_error;
  68. /* ---- tiny freestanding string helpers ---- */
  69. static unsigned int s_len(const char *s)
  70. {
  71. unsigned int n = 0;
  72. while (s && s[n])
  73. n++;
  74. return n;
  75. }
  76. static void s_copy(char *d, const char *s, unsigned int cap)
  77. {
  78. unsigned int i = 0;
  79. if (!cap)
  80. return;
  81. while (s && s[i] && i + 1 < cap) {
  82. d[i] = s[i];
  83. i++;
  84. }
  85. d[i] = '\0';
  86. }
  87. static void s_append(char *d, unsigned int *n, const char *s, unsigned int cap)
  88. {
  89. while (s && *s && *n + 1 < cap)
  90. d[(*n)++] = *s++;
  91. d[*n] = '\0';
  92. }
  93. static void s_append_uint(char *d, unsigned int *n, unsigned int v,
  94. unsigned int cap)
  95. {
  96. char tmp[12];
  97. unsigned int i = 0, j;
  98. do {
  99. tmp[i++] = (char)('0' + v % 10);
  100. v /= 10;
  101. } while (v && i < sizeof(tmp));
  102. for (j = 0; j < i && *n + 1 < cap; j++)
  103. d[(*n)++] = tmp[i - 1 - j];
  104. d[*n] = '\0';
  105. }
  106. static int s_eq(const char *a, const char *b)
  107. {
  108. while (*a && *b) {
  109. if (*a != *b)
  110. return 0;
  111. a++;
  112. b++;
  113. }
  114. return *a == *b;
  115. }
  116. static int s_ci_cmp(const char *a, const char *b)
  117. {
  118. for (;;) {
  119. unsigned char ca = (unsigned char)*a++;
  120. unsigned char cb = (unsigned char)*b++;
  121. if (ca >= 'a' && ca <= 'z')
  122. ca = (unsigned char)(ca - 32);
  123. if (cb >= 'a' && cb <= 'z')
  124. cb = (unsigned char)(cb - 32);
  125. if (ca != cb)
  126. return (int)ca - (int)cb;
  127. if (!ca)
  128. return 0;
  129. }
  130. }
  131. static int is_root(const char *p)
  132. {
  133. return p && p[0] == '/' && p[1] == '\0';
  134. }
  135. static int ext_is(const char *name, const char *ext)
  136. {
  137. unsigned int n = s_len(name), e = s_len(ext), i;
  138. if (n < e)
  139. return 0;
  140. for (i = 0; i < e; i++) {
  141. unsigned char a = (unsigned char)name[n - e + i];
  142. unsigned char b = (unsigned char)ext[i];
  143. if (a >= 'A' && a <= 'Z')
  144. a = (unsigned char)(a + 32);
  145. if (a != b)
  146. return 0;
  147. }
  148. return 1;
  149. }
  150. static int is_image_name(const char *name)
  151. {
  152. return ext_is(name, ".png") || ext_is(name, ".jpg") ||
  153. ext_is(name, ".jpeg") || ext_is(name, ".gif") ||
  154. ext_is(name, ".bmp") || ext_is(name, ".tga") ||
  155. ext_is(name, ".ppm") || ext_is(name, ".pgm") ||
  156. ext_is(name, ".pbm") || ext_is(name, ".ico") ||
  157. ext_is(name, ".cur") || ext_is(name, ".pam");
  158. }
  159. static int is_html_name(const char *name)
  160. {
  161. return ext_is(name, ".htm") || ext_is(name, ".html") ||
  162. ext_is(name, ".xhtml");
  163. }
  164. static void path_join(const char *dir, const char *name, char *out,
  165. unsigned int cap)
  166. {
  167. unsigned int n = 0;
  168. if (!dir || !dir[0])
  169. dir = "/";
  170. if (name && name[0] == '/') {
  171. s_copy(out, name, cap);
  172. return;
  173. }
  174. s_append(out, &n, dir, cap);
  175. if (!is_root(dir))
  176. s_append(out, &n, "/", cap);
  177. s_append(out, &n, name, cap);
  178. }
  179. static void path_parent(const char *in, char *out, unsigned int cap)
  180. {
  181. unsigned int n, i, last = 0;
  182. s_copy(out, in, cap);
  183. if (!out[0] || is_root(out)) {
  184. s_copy(out, "/", cap);
  185. return;
  186. }
  187. n = s_len(out);
  188. if (n > 1 && out[n - 1] == '/')
  189. out[--n] = '\0';
  190. for (i = 0; i < n; i++)
  191. if (out[i] == '/')
  192. last = i;
  193. if (last == 0)
  194. out[1] = '\0';
  195. else
  196. out[last] = '\0';
  197. if (!out[0])
  198. s_copy(out, "/", cap);
  199. }
  200. static void make_abs(const char *p, char *out, unsigned int cap)
  201. {
  202. if (!p || !p[0]) {
  203. s_copy(out, cwd, cap);
  204. return;
  205. }
  206. if (p[0] == '/')
  207. s_copy(out, p, cap);
  208. else
  209. path_join(cwd, p, out, cap);
  210. }
  211. static void format_name(const char *raw, unsigned int raw_83, char *out,
  212. unsigned int cap)
  213. {
  214. /* Name-string filesystems (ramfs, share) already give a display name;
  215. * FAT gives the raw 11-byte, space-padded 8.3 field. */
  216. if (!raw_83)
  217. s_copy(out, raw, cap);
  218. else
  219. u_fat_name(raw, out, cap);
  220. }
  221. static void format_size(unsigned int bytes, char *out, unsigned int cap)
  222. {
  223. unsigned int n = 0;
  224. if (bytes < 1024) {
  225. s_append_uint(out, &n, bytes, cap);
  226. s_append(out, &n, " B", cap);
  227. } else if (bytes < 1024u * 1024u) {
  228. s_append_uint(out, &n, bytes / 1024, cap);
  229. s_append(out, &n, " KB", cap);
  230. } else {
  231. s_append_uint(out, &n, bytes / (1024u * 1024u), cap);
  232. s_append(out, &n, " MB", cap);
  233. }
  234. }
  235. /* ---- state / filesystem loading ---- */
  236. static void set_status(const char *text, int error)
  237. {
  238. s_copy(status_text, text, sizeof(status_text));
  239. status_until = u_ticks_ms() + FM_STATUS_MS;
  240. status_error = error;
  241. if (!status_until)
  242. status_until = 1;
  243. ngui_invalidate(&ui);
  244. }
  245. static void clear_status(void)
  246. {
  247. status_text[0] = '\0';
  248. status_until = 0;
  249. status_error = 0;
  250. }
  251. static void load_mounts(void)
  252. {
  253. struct sys_mount m;
  254. unsigned int i;
  255. mount_count = 0;
  256. for (i = 0; i < FM_MAX_MOUNTS; i++) {
  257. if (u_syscall2(SYS_LIST_MOUNTS, (int)i, (int)&m) != 1)
  258. break;
  259. mounts[mount_count++] = m;
  260. }
  261. }
  262. static int entry_before(const struct fm_entry *a, const struct fm_entry *b)
  263. {
  264. int adir = (int)(a->is_dir || a->is_mount);
  265. int bdir = (int)(b->is_dir || b->is_mount);
  266. if (adir != bdir)
  267. return adir > bdir;
  268. return s_ci_cmp(a->name, b->name) < 0;
  269. }
  270. static void sort_entries(struct fm_entry *list, unsigned int n)
  271. {
  272. unsigned int i, j;
  273. for (i = 1; i < n; i++) {
  274. struct fm_entry key = list[i];
  275. j = i;
  276. while (j > 0 && entry_before(&key, &list[j - 1])) {
  277. list[j] = list[j - 1];
  278. j--;
  279. }
  280. list[j] = key;
  281. }
  282. }
  283. static void commit_list(unsigned int n, const char *path)
  284. {
  285. unsigned int i;
  286. for (i = 0; i < n; i++)
  287. entries[i] = load_entries[i];
  288. entry_count = n;
  289. s_copy(cwd, path, sizeof(cwd));
  290. selected = 0;
  291. list_scroll.offset = 0;
  292. list_scroll.content_h = (int)(n * FM_ROW_H);
  293. last_click_index = -1;
  294. ngui_input_set(&path_in, cwd);
  295. ngui_invalidate(&ui);
  296. }
  297. static int load_root(void)
  298. {
  299. unsigned int i;
  300. load_mounts();
  301. for (i = 0; i < mount_count; i++) {
  302. s_copy(load_entries[i].name, mounts[i].name,
  303. sizeof(load_entries[i].name));
  304. s_copy(load_entries[i].detail, mounts[i].type,
  305. sizeof(load_entries[i].detail));
  306. load_entries[i].size = 0;
  307. load_entries[i].is_dir = 1;
  308. load_entries[i].is_mount = 1;
  309. }
  310. sort_entries(load_entries, mount_count);
  311. truncated = 0;
  312. commit_list(mount_count, "/");
  313. clear_status();
  314. return 0;
  315. }
  316. static void list_error(int r)
  317. {
  318. switch (r) {
  319. case SYS_ENOENT:
  320. set_status("No such file or directory", 1);
  321. break;
  322. case SYS_ENODEV:
  323. set_status("No such device", 1);
  324. break;
  325. case SYS_ENOFS:
  326. set_status("No filesystem on device", 1);
  327. break;
  328. case SYS_EINVAL:
  329. set_status("Not a directory", 1);
  330. break;
  331. case SYS_EIO:
  332. set_status("Disk read error", 1);
  333. break;
  334. default:
  335. set_status("Cannot open directory", 1);
  336. break;
  337. }
  338. }
  339. static int load_path(const char *path)
  340. {
  341. struct sys_dirent d;
  342. unsigned int n = 0, i;
  343. int r;
  344. if (!path || !path[0] || is_root(path))
  345. return load_root();
  346. truncated = 0;
  347. for (i = 0; ; i++) {
  348. r = u_syscall3(SYS_LIST_DIR, (int)path, (int)i, (int)&d);
  349. if (r != 1) {
  350. if (r < 0) {
  351. list_error(r);
  352. return -1;
  353. }
  354. break;
  355. }
  356. if (d.name[0] == '.')
  357. continue;
  358. if (n >= FM_MAX_ENTRIES) {
  359. truncated = 1;
  360. break;
  361. }
  362. format_name(d.name, d.raw_83, load_entries[n].name,
  363. sizeof(load_entries[n].name));
  364. load_entries[n].detail[0] = '\0';
  365. load_entries[n].size = d.size;
  366. load_entries[n].is_dir = d.is_dir;
  367. load_entries[n].is_mount = 0;
  368. n++;
  369. }
  370. sort_entries(load_entries, n);
  371. commit_list(n, path);
  372. clear_status();
  373. if (truncated)
  374. set_status("Showing the first 256 entries", 0);
  375. return 0;
  376. }
  377. static void navigate(const char *path)
  378. {
  379. char abs[FM_PATH_MAX];
  380. if (!path || !path[0]) {
  381. set_status("Empty path", 1);
  382. return;
  383. }
  384. make_abs(path, abs, sizeof(abs));
  385. if (load_path(abs) == 0)
  386. ngui_set_focus(&ui, 0);
  387. }
  388. static void go_parent(void)
  389. {
  390. char parent[FM_PATH_MAX];
  391. path_parent(cwd, parent, sizeof(parent));
  392. if (s_eq(parent, cwd)) {
  393. set_status("Already at the root", 0);
  394. return;
  395. }
  396. load_path(parent);
  397. }
  398. static void go_home(void)
  399. {
  400. if (!is_root(cwd))
  401. load_path("/");
  402. }
  403. static void select_index(unsigned int idx)
  404. {
  405. if (idx >= entry_count)
  406. return;
  407. selected = (int)idx;
  408. ngui_scroll_ensure_visible(&list_scroll, (int)idx * FM_ROW_H, FM_ROW_H);
  409. ngui_invalidate(&ui);
  410. }
  411. static void select_by_name(const char *name)
  412. {
  413. unsigned int i;
  414. for (i = 0; i < entry_count; i++)
  415. if (s_ci_cmp(entries[i].name, name) == 0) {
  416. select_index(i);
  417. return;
  418. }
  419. }
  420. static void refresh(void)
  421. {
  422. char keep[16];
  423. int had = 0;
  424. if (entry_count && selected >= 0 && selected < (int)entry_count) {
  425. s_copy(keep, entries[selected].name, sizeof(keep));
  426. had = 1;
  427. }
  428. if (load_path(cwd) != 0)
  429. return;
  430. if (had)
  431. select_by_name(keep);
  432. }
  433. static void open_selected(void)
  434. {
  435. struct fm_entry *e;
  436. char path[FM_PATH_MAX];
  437. int pid;
  438. if (!entry_count || selected < 0 || selected >= (int)entry_count)
  439. return;
  440. e = &entries[selected];
  441. if (e->is_mount || e->is_dir) {
  442. if (e->is_mount)
  443. path_join("/", e->name, path, sizeof(path));
  444. else
  445. path_join(cwd, e->name, path, sizeof(path));
  446. load_path(path);
  447. return;
  448. }
  449. if (is_html_name(e->name)) {
  450. path_join(cwd, e->name, path, sizeof(path));
  451. pid = u_spawn("/ramfs/bin/browser", path, 0, cwd);
  452. if (pid <= 0)
  453. set_status("Cannot launch the browser", 1);
  454. else
  455. set_status("Opened in the browser", 0);
  456. return;
  457. }
  458. if (is_image_name(e->name)) {
  459. if (e->size > IMG_FILE_MAX) {
  460. set_status("Image is larger than 4 MB", 1);
  461. return;
  462. }
  463. path_join(cwd, e->name, path, sizeof(path));
  464. pid = u_spawn("/ramfs/bin/view", path, 0, cwd);
  465. if (pid <= 0)
  466. set_status("Cannot launch the viewer", 1);
  467. else
  468. set_status("Opened in the viewer", 0);
  469. return;
  470. }
  471. if (e->size > FM_EDIT_MAX) {
  472. set_status("File too large for the editor", 1);
  473. return;
  474. }
  475. path_join(cwd, e->name, path, sizeof(path));
  476. pid = u_spawn("/ramfs/bin/edit", path, 0, cwd);
  477. if (pid <= 0)
  478. set_status("Cannot launch the editor", 1);
  479. else
  480. set_status("Opened in the editor", 0);
  481. }
  482. static void new_folder(void)
  483. {
  484. if (is_root(cwd)) {
  485. set_status("Select a filesystem first", 1);
  486. return;
  487. }
  488. modal = 1;
  489. ngui_input_set(&new_in, "");
  490. new_in.placeholder = "NAME";
  491. ngui_set_focus(&ui, ID_NEW_INPUT);
  492. ngui_invalidate(&ui);
  493. }
  494. static void create_folder(void)
  495. {
  496. char path[FM_PATH_MAX];
  497. char made[16];
  498. int r;
  499. if (!new_in.text[0]) {
  500. set_status("Enter a folder name", 1);
  501. return;
  502. }
  503. path_join(cwd, new_in.text, path, sizeof(path));
  504. r = u_syscall1(SYS_MKDIR, (int)path);
  505. if (r != 0) {
  506. switch (r) {
  507. case SYS_EROFS:
  508. set_status("The filesystem is read-only", 1);
  509. break;
  510. case SYS_EINVAL:
  511. set_status("Invalid folder name", 1);
  512. break;
  513. case SYS_EIO:
  514. set_status("Disk write error", 1);
  515. break;
  516. default:
  517. set_status("Cannot create the folder", 1);
  518. break;
  519. }
  520. return;
  521. }
  522. s_copy(made, new_in.text, sizeof(made));
  523. modal = 0;
  524. ngui_set_focus(&ui, 0);
  525. refresh();
  526. select_by_name(made);
  527. clear_status();
  528. set_status("Folder created", 0);
  529. }
  530. static void do_action(unsigned int action)
  531. {
  532. if (action & ACT_LOAD)
  533. load_path(action_path);
  534. if (action & ACT_NAV)
  535. navigate(path_in.text);
  536. if (action & ACT_UP)
  537. go_parent();
  538. if (action & ACT_PARENT)
  539. go_parent();
  540. if (action & ACT_HOME)
  541. go_home();
  542. if (action & ACT_REFRESH)
  543. refresh();
  544. if (action & ACT_NEW)
  545. new_folder();
  546. if (action & ACT_OPEN)
  547. open_selected();
  548. if (action & ACT_CREATE) {
  549. create_folder();
  550. if (modal)
  551. ngui_set_focus(&ui, ID_NEW_INPUT);
  552. }
  553. if (action & ACT_CANCEL) {
  554. modal = 0;
  555. ngui_set_focus(&ui, 0);
  556. ngui_invalidate(&ui);
  557. }
  558. }
  559. /* ---- drawing ---- */
  560. static void draw_toolbar(struct ngui *g, struct ngui_rect r, unsigned int *action)
  561. {
  562. int compact = g->w < 480;
  563. int y = r.y + 4;
  564. int h = 22;
  565. int x = r.x + 6;
  566. ngui_fill_rect(g, r, g->theme.panel);
  567. ngui_fill_rect(g, (struct ngui_rect){r.x, r.y + r.h - 1, r.w, 1},
  568. g->theme.border);
  569. if (compact) {
  570. if (ngui_icon_button(g, ID_UP,
  571. (struct ngui_rect){x, y, 30, h},
  572. NGUI_ICON_UP))
  573. *action |= ACT_UP;
  574. x += 36;
  575. if (ngui_icon_button(g, ID_HOME,
  576. (struct ngui_rect){x, y, 30, h},
  577. NGUI_ICON_HOME))
  578. *action |= ACT_HOME;
  579. x += 36;
  580. if (ngui_icon_button(g, ID_REFRESH,
  581. (struct ngui_rect){x, y, 30, h},
  582. NGUI_ICON_REFRESH))
  583. *action |= ACT_REFRESH;
  584. x += 36;
  585. if (ngui_icon_button(g, ID_NEW,
  586. (struct ngui_rect){x, y, 30, h},
  587. NGUI_ICON_PLUS))
  588. *action |= ACT_NEW;
  589. } else {
  590. int w;
  591. w = (int)ngui_text_width("Up") + 18;
  592. if (ngui_button(g, ID_UP, (struct ngui_rect){x, y, w, h}, "Up"))
  593. *action |= ACT_UP;
  594. x += w + 6;
  595. w = (int)ngui_text_width("Home") + 18;
  596. if (ngui_button(g, ID_HOME, (struct ngui_rect){x, y, w, h},
  597. "Home"))
  598. *action |= ACT_HOME;
  599. x += w + 6;
  600. w = (int)ngui_text_width("Refresh") + 18;
  601. if (ngui_button(g, ID_REFRESH, (struct ngui_rect){x, y, w, h},
  602. "Refresh"))
  603. *action |= ACT_REFRESH;
  604. x += w + 6;
  605. w = (int)ngui_text_width("New Folder") + 18;
  606. if (ngui_button(g, ID_NEW, (struct ngui_rect){x, y, w, h},
  607. "New Folder"))
  608. *action |= ACT_NEW;
  609. }
  610. }
  611. static void draw_path(struct ngui *g, struct ngui_rect r, unsigned int *action)
  612. {
  613. int go_w = g->w < 260 ? 40 : 50;
  614. struct ngui_rect in = {r.x + 6, r.y + 3, r.w - 12 - go_w - 6, 20};
  615. struct ngui_rect go = {r.x + r.w - 6 - go_w, r.y + 3, go_w, 20};
  616. int res;
  617. ngui_fill_rect(g, r, g->theme.panel);
  618. if (in.w < 40)
  619. in.w = 40;
  620. res = ngui_text_input(g, ID_PATH, in, &path_in);
  621. if (res & NGUI_INPUT_SUBMIT)
  622. *action |= ACT_NAV;
  623. if (res & NGUI_INPUT_CANCEL)
  624. ngui_set_focus(g, 0);
  625. if (ngui_button(g, ID_GO, go, "Go"))
  626. *action |= ACT_NAV;
  627. }
  628. static int path_under(const char *path, const char *base)
  629. {
  630. unsigned int n = s_len(base);
  631. if (!path || !base)
  632. return 0;
  633. if (s_eq(path, base))
  634. return 1;
  635. if (s_len(path) > n && path[n] == '/' &&
  636. path[0] == base[0]) {
  637. unsigned int i;
  638. for (i = 0; i < n; i++)
  639. if (path[i] != base[i])
  640. return 0;
  641. return 1;
  642. }
  643. return 0;
  644. }
  645. static void draw_sidebar(struct ngui *g, struct ngui_rect r, unsigned int *action)
  646. {
  647. int y = r.y + 4;
  648. int row_y = r.y + 24;
  649. unsigned int i;
  650. ngui_fill_rect(g, r, g->theme.panel);
  651. ngui_text(g, r.x + 8, y, "Places", g->theme.text_dim);
  652. {
  653. int hot = g->inside && g->mouse_x >= r.x &&
  654. g->mouse_x < r.x + r.w &&
  655. g->mouse_y >= row_y && g->mouse_y < row_y + FM_ROW_H;
  656. int pressed = g->pressed && g->press_x >= r.x &&
  657. g->press_x < r.x + r.w &&
  658. g->press_y >= row_y && g->press_y < row_y + FM_ROW_H;
  659. if (is_root(cwd))
  660. ngui_fill_rect(g, (struct ngui_rect){r.x + 1, row_y,
  661. r.w - 2, FM_ROW_H - 1},
  662. g->theme.selection);
  663. else if (hot)
  664. ngui_fill_rect(g, (struct ngui_rect){r.x + 1, row_y,
  665. r.w - 2, FM_ROW_H - 1},
  666. g->theme.panel_alt);
  667. ngui_icon(g, (struct ngui_rect){r.x + 6, row_y + 3, 16, 16},
  668. NGUI_ICON_HOME, g->theme.icon_folder);
  669. ngui_text(g, r.x + 28, row_y + 3, "Root", g->theme.text);
  670. if (pressed) {
  671. s_copy(action_path, "/", sizeof(action_path));
  672. *action |= ACT_LOAD;
  673. }
  674. }
  675. row_y += FM_ROW_H;
  676. for (i = 0; i < mount_count && row_y + FM_ROW_H <= r.y + r.h; i++) {
  677. char mpath[FM_PATH_MAX];
  678. int hot;
  679. int active;
  680. int pressed;
  681. path_join("/", mounts[i].name, mpath, sizeof(mpath));
  682. hot = g->inside && g->mouse_x >= r.x &&
  683. g->mouse_x < r.x + r.w &&
  684. g->mouse_y >= row_y && g->mouse_y < row_y + FM_ROW_H;
  685. pressed = g->pressed && g->press_x >= r.x &&
  686. g->press_x < r.x + r.w &&
  687. g->press_y >= row_y && g->press_y < row_y + FM_ROW_H;
  688. active = path_under(cwd, mpath);
  689. if (active)
  690. ngui_fill_rect(g, (struct ngui_rect){r.x + 1, row_y,
  691. r.w - 2, FM_ROW_H - 1},
  692. g->theme.selection);
  693. else if (hot)
  694. ngui_fill_rect(g, (struct ngui_rect){r.x + 1, row_y,
  695. r.w - 2, FM_ROW_H - 1},
  696. g->theme.panel_alt);
  697. ngui_icon(g, (struct ngui_rect){r.x + 6, row_y + 3, 16, 16},
  698. NGUI_ICON_DISK, g->theme.icon_disk);
  699. ngui_text_ellipsis(g, (struct ngui_rect){r.x + 28, row_y,
  700. r.w - 34, FM_ROW_H},
  701. r.x + 28, row_y + 3, mounts[i].name,
  702. g->theme.text);
  703. if (pressed) {
  704. s_copy(action_path, mpath, sizeof(action_path));
  705. *action |= ACT_LOAD;
  706. }
  707. row_y += FM_ROW_H;
  708. }
  709. if (!mount_count)
  710. ngui_text(g, r.x + 8, r.y + 50, "No volumes", g->theme.text_dim);
  711. ngui_fill_rect(g, (struct ngui_rect){r.x + r.w - 1, r.y, 1, r.h},
  712. g->theme.border);
  713. }
  714. static void draw_list(struct ngui *g, struct ngui_rect r, unsigned int *action)
  715. {
  716. unsigned int i;
  717. int scrolls;
  718. int row_w;
  719. int hover_idx = -1;
  720. int press_idx = -1;
  721. ngui_fill_rect(g, r, g->theme.bg);
  722. list_scroll.content_h = (int)(entry_count * FM_ROW_H);
  723. ngui_scroll_begin(g, r, &list_scroll);
  724. scrolls = list_scroll.content_h > list_scroll.view_h;
  725. row_w = r.w - (scrolls ? FM_SCROLL_W : 0);
  726. if (g->inside && g->mouse_x >= r.x && g->mouse_x < r.x + row_w &&
  727. g->mouse_y >= r.y && g->mouse_y < r.y + r.h &&
  728. list_scroll.content_h > 0) {
  729. int hi = (g->mouse_y - r.y + list_scroll.offset) / FM_ROW_H;
  730. if (hi >= 0 && hi < (int)entry_count)
  731. hover_idx = hi;
  732. }
  733. if (g->pressed && g->press_x >= r.x &&
  734. g->press_x < r.x + row_w && g->press_y >= r.y &&
  735. g->press_y < r.y + r.h) {
  736. press_idx = (g->press_y - r.y + list_scroll.offset) / FM_ROW_H;
  737. if (press_idx < 0 || press_idx >= (int)entry_count)
  738. press_idx = -1;
  739. }
  740. for (i = 0; i < entry_count; i++) {
  741. struct fm_entry *e = &entries[i];
  742. int y = r.y + (int)i * FM_ROW_H - list_scroll.offset;
  743. int detail_x;
  744. char detail[20];
  745. unsigned int dn = 0;
  746. if (y + FM_ROW_H <= r.y || y >= r.y + r.h)
  747. continue;
  748. if ((int)i == selected)
  749. ngui_fill_rect(g, (struct ngui_rect){r.x, y, row_w,
  750. FM_ROW_H - 1},
  751. g->theme.selection);
  752. else if ((int)i == hover_idx)
  753. ngui_fill_rect(g, (struct ngui_rect){r.x, y, row_w,
  754. FM_ROW_H - 1},
  755. g->theme.panel_alt);
  756. ngui_icon(g, (struct ngui_rect){r.x + 4, y + 3, 16, 16},
  757. e->is_mount ? NGUI_ICON_DISK :
  758. e->is_dir ? NGUI_ICON_FOLDER : NGUI_ICON_FILE,
  759. e->is_mount ? g->theme.icon_disk :
  760. e->is_dir ? g->theme.icon_folder : g->theme.icon_file);
  761. if (e->is_mount) {
  762. s_append(detail, &dn, e->detail, sizeof(detail));
  763. } else if (e->is_dir) {
  764. s_append(detail, &dn, "<DIR>", sizeof(detail));
  765. } else {
  766. format_size(e->size, detail, sizeof(detail));
  767. }
  768. detail_x = r.x + row_w - 6 - (int)ngui_text_width(detail);
  769. if (detail_x < r.x + 30) {
  770. detail[0] = '\0';
  771. detail_x = r.x + row_w;
  772. }
  773. ngui_text_ellipsis(g, (struct ngui_rect){r.x + 28, y,
  774. detail_x - r.x - 30, FM_ROW_H},
  775. r.x + 28, y + 3, e->name,
  776. g->theme.text);
  777. if (detail[0])
  778. ngui_text(g, detail_x, y + 3, detail,
  779. g->theme.text_dim);
  780. }
  781. if (!entry_count)
  782. ngui_text(g, r.x + 10, r.y + 8, "Empty directory",
  783. g->theme.text_dim);
  784. ngui_scroll_end(g, r, &list_scroll);
  785. if (press_idx >= 0) {
  786. unsigned int now = u_ticks_ms();
  787. select_index((unsigned int)press_idx);
  788. if (press_idx == last_click_index &&
  789. now - last_click_ms < 500) {
  790. *action |= ACT_OPEN;
  791. last_click_index = -1;
  792. } else {
  793. last_click_index = press_idx;
  794. last_click_ms = now;
  795. }
  796. }
  797. }
  798. static void draw_status(struct ngui *g, struct ngui_rect r)
  799. {
  800. char buf[160];
  801. unsigned int n = 0;
  802. unsigned short color = g->theme.text;
  803. ngui_fill_rect(g, r, g->theme.panel);
  804. ngui_fill_rect(g, (struct ngui_rect){r.x, r.y, r.w, 1},
  805. g->theme.border);
  806. if (status_text[0]) {
  807. s_append(buf, &n, status_text, sizeof(buf));
  808. if (status_error)
  809. color = g->theme.error;
  810. } else {
  811. s_append_uint(buf, &n, entry_count, sizeof(buf));
  812. s_append(buf, &n, entry_count == 1 ? " item" : " items", sizeof(buf));
  813. if (truncated)
  814. s_append(buf, &n, " (first 256)", sizeof(buf));
  815. if (entry_count && selected >= 0 &&
  816. selected < (int)entry_count) {
  817. s_append(buf, &n, " ", sizeof(buf));
  818. s_append(buf, &n, entries[selected].name, sizeof(buf));
  819. if (entries[selected].is_mount) {
  820. s_append(buf, &n, " ", sizeof(buf));
  821. s_append(buf, &n, entries[selected].detail,
  822. sizeof(buf));
  823. } else if (!entries[selected].is_dir) {
  824. char sz[20];
  825. format_size(entries[selected].size, sz, sizeof(sz));
  826. s_append(buf, &n, " ", sizeof(buf));
  827. s_append(buf, &n, sz, sizeof(buf));
  828. }
  829. }
  830. }
  831. ngui_text_ellipsis(g, r, r.x + 6, r.y + (r.h - 16) / 2, buf, color);
  832. }
  833. static void draw_modal(struct ngui *g, unsigned int *action)
  834. {
  835. struct ngui_rect all = {0, 0, g->w, g->h};
  836. int pw = g->w - 24;
  837. int ph = g->h - 16;
  838. int x, y, title_y, label_y, input_y, buttons_y;
  839. int bw, bx;
  840. struct ngui_rect input;
  841. int res;
  842. if (pw > 340)
  843. pw = 340;
  844. if (pw < 160)
  845. pw = 160;
  846. if (ph > 130)
  847. ph = 130;
  848. if (ph < 96)
  849. ph = 96;
  850. x = (g->w - pw) / 2;
  851. y = (g->h - ph) / 2;
  852. title_y = y + (ph >= 120 ? 9 : 6);
  853. label_y = y + (ph >= 120 ? 30 : 22);
  854. input_y = y + (ph >= 120 ? 48 : 38);
  855. buttons_y = y + ph - 34;
  856. bw = (pw - 36) / 2;
  857. if (bw > 80)
  858. bw = 80;
  859. if (bw < 50)
  860. bw = 50;
  861. bx = x + (pw - (bw * 2 + 8)) / 2;
  862. ngui_fill_rect(g, all, g->theme.bg);
  863. ngui_fill_rect(g, (struct ngui_rect){x + 3, y + 3, pw, ph},
  864. NGUI_RGB(8, 10, 16));
  865. ngui_fill_rect(g, (struct ngui_rect){x, y, pw, ph}, g->theme.panel);
  866. ngui_frame(g, (struct ngui_rect){x, y, pw, ph}, g->theme.accent);
  867. ngui_text(g, x + 12, title_y, "New folder", g->theme.text);
  868. ngui_text(g, x + 12, label_y, "Name", g->theme.text_dim);
  869. input = (struct ngui_rect){x + 12, input_y, pw - 24, 22};
  870. res = ngui_text_input(g, ID_NEW_INPUT, input, &new_in);
  871. if (res & NGUI_INPUT_SUBMIT)
  872. *action |= ACT_CREATE;
  873. if (res & NGUI_INPUT_CANCEL)
  874. *action |= ACT_CANCEL;
  875. if (buttons_y > input_y + 44)
  876. ngui_text(g, x + 12, input_y + 28,
  877. "Enter creates, Esc cancels", g->theme.text_dim);
  878. if (ngui_button(g, ID_CREATE,
  879. (struct ngui_rect){bx, buttons_y, bw, 24}, "Create"))
  880. *action |= ACT_CREATE;
  881. if (ngui_button(g, ID_CANCEL,
  882. (struct ngui_rect){bx + bw + 8, buttons_y, bw, 24},
  883. "Cancel"))
  884. *action |= ACT_CANCEL;
  885. }
  886. static void draw(struct ngui *g, unsigned int *action)
  887. {
  888. struct ngui_rect r = {0, 0, g->w, g->h};
  889. struct ngui_rect toolbar, path, status, body;
  890. int side_w;
  891. if (modal) {
  892. draw_modal(g, action);
  893. return;
  894. }
  895. toolbar = ngui_cut_top(&r, FM_TOOLBAR_H);
  896. path = ngui_cut_top(&r, FM_PATH_H);
  897. status = ngui_cut_bottom(&r, FM_STATUS_H);
  898. body = r;
  899. draw_toolbar(g, toolbar, action);
  900. draw_path(g, path, action);
  901. if (body.w > 0 && body.h > 0) {
  902. side_w = g->w >= 520 ? FM_SIDEBAR_W : 0;
  903. if (side_w) {
  904. struct ngui_rect side = ngui_cut_left(&body, side_w);
  905. draw_sidebar(g, side, action);
  906. }
  907. draw_list(g, body, action);
  908. }
  909. draw_status(g, status);
  910. }
  911. static void handle_keys(struct ngui *g, unsigned int *action)
  912. {
  913. int k;
  914. if (ngui_focus(g))
  915. return;
  916. if (ngui_scroll_keys(g, &list_scroll))
  917. ngui_invalidate(g);
  918. while ((k = ngui_key(g)) != 0) {
  919. switch (k) {
  920. case NGUI_KEY_UP:
  921. if (entry_count && selected > 0)
  922. select_index((unsigned int)(selected - 1));
  923. break;
  924. case NGUI_KEY_DOWN:
  925. if (entry_count &&
  926. selected + 1 < (int)entry_count)
  927. select_index((unsigned int)(selected + 1));
  928. break;
  929. case NGUI_KEY_HOME:
  930. if (entry_count)
  931. select_index(0);
  932. break;
  933. case NGUI_KEY_END:
  934. if (entry_count)
  935. select_index(entry_count - 1);
  936. break;
  937. case '\n':
  938. case '\r':
  939. *action |= ACT_OPEN;
  940. break;
  941. case NGUI_KEY_BACKSPACE:
  942. *action |= ACT_PARENT;
  943. break;
  944. case NGUI_KEY_TAB:
  945. case 0x0C: /* Ctrl+L */
  946. ngui_set_focus(g, ID_PATH);
  947. break;
  948. case 0x0E: /* Ctrl+N */
  949. *action |= ACT_NEW;
  950. break;
  951. case 0x12: /* Ctrl+R */
  952. *action |= ACT_REFRESH;
  953. break;
  954. case NGUI_KEY_ESC:
  955. *action |= ACT_CLOSE;
  956. break;
  957. default:
  958. break;
  959. }
  960. }
  961. }
  962. void _start(const char *arg)
  963. {
  964. struct win_info wi;
  965. char start[FM_PATH_MAX];
  966. int quit = 0;
  967. (void)arg;
  968. if (u_win_create(600, 400, "Files", &wi) != 0) {
  969. u_puts("files: no graphics window\n");
  970. u_exit(1);
  971. }
  972. ngui_init(&ui, (unsigned short *)wi.fb, (int)wi.width, (int)wi.height);
  973. path_in.placeholder = "/path";
  974. new_in.placeholder = "NAME";
  975. list_scroll.offset = 0;
  976. load_mounts();
  977. if (u_getcwd(start, sizeof(start)) != 0 || !start[0])
  978. s_copy(start, "/", sizeof(start));
  979. if (load_path(start) != 0)
  980. load_path("/");
  981. for (;;) {
  982. unsigned int action = 0;
  983. unsigned int now;
  984. now = u_ticks_ms();
  985. if (status_until && (int)(now - status_until) >= 0) {
  986. status_text[0] = '\0';
  987. status_until = 0;
  988. status_error = 0;
  989. ngui_invalidate(&ui);
  990. }
  991. if (!ngui_begin(&ui)) {
  992. u_sleep_ms(10);
  993. continue;
  994. }
  995. draw(&ui, &action);
  996. if (modal) {
  997. int k = ngui_key_peek(&ui);
  998. if (k == NGUI_KEY_ESC) {
  999. ngui_key(&ui);
  1000. action |= ACT_CANCEL;
  1001. }
  1002. } else {
  1003. handle_keys(&ui, &action);
  1004. }
  1005. ngui_end(&ui);
  1006. if (action)
  1007. do_action(action);
  1008. if (action & ACT_CLOSE)
  1009. quit = 1;
  1010. if (quit)
  1011. break;
  1012. u_sleep_ms(10);
  1013. }
  1014. u_win_close();
  1015. u_exit(0);
  1016. }