ucmd_files.c 26 KB

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