ucmd_files.c 26 KB

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