ucmd_files.c 26 KB

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