ucmd_files.c 25 KB

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