ucmd_files.c 25 KB

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