ucmd_files.c 25 KB

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