ucmd_view.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. /* view: image viewer built on the img library and the ngui toolkit.
  2. *
  3. * Usage: view <file>. The command owns a canvas window, reads the whole file
  4. * through the kernel file service, decodes it into an exact-size malloc arena,
  5. * and draws it with fit/1:1/zoom/pan support. */
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include "ucmd_lib.h"
  9. #include "ngui.h"
  10. #include "img.h"
  11. #include "img_file.h"
  12. #define VIEW_FILE_CAP (IMG_FILE_MAX + 1u)
  13. #define VIEW_HEAP_MAX (32u * 1024u * 1024u)
  14. #define VIEW_TOOLBAR_H 28
  15. #define VIEW_STATUS_H 18
  16. #define VIEW_BAR_H (VIEW_TOOLBAR_H + VIEW_STATUS_H)
  17. #define VIEW_ID_FIT 1
  18. #define VIEW_ID_ONE 2
  19. #define VIEW_ID_OUT 3
  20. #define VIEW_ID_IN 4
  21. #define VIEW_ID_WALL 5
  22. #define VIEW_BG NGUI_RGB(24, 26, 32)
  23. #define VIEW_VIEW_BG NGUI_RGB(16, 17, 21)
  24. #define VIEW_CHK1 NGUI_RGB(46, 48, 54)
  25. #define VIEW_CHK2 NGUI_RGB(58, 60, 67)
  26. struct viewer {
  27. struct ngui ui;
  28. struct img_image img;
  29. struct img_mem mem;
  30. unsigned char *file;
  31. unsigned char *arena;
  32. unsigned int file_size;
  33. unsigned int iw, ih;
  34. unsigned int format;
  35. unsigned int zoom; /* 16.16 when fit == 0 */
  36. int fit;
  37. int off_x, off_y; /* destination top-left inside the viewport */
  38. int dragging;
  39. int drag_x, drag_y;
  40. int have_img;
  41. int loading;
  42. int error;
  43. unsigned int gif_frames, gif_index, gif_delay, gif_next_ms;
  44. int last_w, last_h;
  45. char err[64];
  46. char notice[40];
  47. unsigned int notice_until;
  48. char name[20];
  49. char title[20];
  50. int dirty;
  51. };
  52. static struct viewer v;
  53. static unsigned int s_len(const char *s)
  54. {
  55. unsigned int n = 0;
  56. while (s && s[n])
  57. n++;
  58. return n;
  59. }
  60. static void s_copy(char *d, const char *s, unsigned int cap)
  61. {
  62. unsigned int i = 0;
  63. if (!cap)
  64. return;
  65. while (s && s[i] && i + 1 < cap) {
  66. d[i] = s[i];
  67. i++;
  68. }
  69. d[i] = '\0';
  70. }
  71. static void s_append(char *d, unsigned int *n, const char *s, unsigned int cap)
  72. {
  73. while (s && *s && *n + 1 < cap)
  74. d[(*n)++] = *s++;
  75. d[*n] = '\0';
  76. }
  77. static void s_append_uint(char *d, unsigned int *n, unsigned int v,
  78. unsigned int cap)
  79. {
  80. char tmp[12];
  81. unsigned int i = 0, j;
  82. do {
  83. tmp[i++] = (char)('0' + v % 10);
  84. v /= 10;
  85. } while (v && i < sizeof(tmp));
  86. for (j = 0; j < i && *n + 1 < cap; j++)
  87. d[(*n)++] = tmp[i - 1 - j];
  88. d[*n] = '\0';
  89. }
  90. static void base_name(const char *path, char *out, unsigned int cap)
  91. {
  92. unsigned int n = 0, i, start = 0;
  93. if (!path)
  94. path = "";
  95. for (i = 0; path[i]; i++)
  96. if (path[i] == '/')
  97. start = i + 1;
  98. for (i = start; path[i] && n + 1 < cap; i++)
  99. out[n++] = path[i];
  100. out[n] = '\0';
  101. if (!n)
  102. s_copy(out, "image", cap);
  103. }
  104. struct ngui_rect view_body(struct ngui *ui)
  105. {
  106. struct ngui_rect r = {0, 0, ui->w, ui->h};
  107. struct ngui_rect top, bottom;
  108. top = ngui_cut_top(&r, ui->h >= 140 ? VIEW_TOOLBAR_H : 22);
  109. bottom = ngui_cut_bottom(&r, ui->h >= 140 ? VIEW_STATUS_H : 14);
  110. (void)top;
  111. (void)bottom;
  112. return r;
  113. }
  114. static unsigned int view_scale(struct ngui *ui)
  115. {
  116. struct ngui_rect b = view_body(ui);
  117. unsigned int s;
  118. if (!v.have_img || b.w <= 0 || b.h <= 0)
  119. return 65536;
  120. if (!v.fit)
  121. return v.zoom ? v.zoom : 65536;
  122. s = img_fit_scale(v.iw, v.ih, (unsigned int)b.w, (unsigned int)b.h);
  123. if (s > 65536)
  124. s = 65536; /* don't enlarge a smaller image */
  125. return s ? s : 256;
  126. }
  127. static void clamp_view(struct ngui *ui, unsigned int scale)
  128. {
  129. struct ngui_rect b = view_body(ui);
  130. unsigned int sw, sh;
  131. if (!v.have_img) {
  132. v.off_x = v.off_y = 0;
  133. return;
  134. }
  135. sw = (unsigned int)(((unsigned long long)v.iw * scale) >> 16);
  136. sh = (unsigned int)(((unsigned long long)v.ih * scale) >> 16);
  137. if (sw < 1) sw = 1;
  138. if (sh < 1) sh = 1;
  139. if (sw <= (unsigned int)b.w)
  140. v.off_x = ((int)b.w - (int)sw) / 2;
  141. else {
  142. if (v.off_x > 0) v.off_x = 0;
  143. if (v.off_x < (int)b.w - (int)sw)
  144. v.off_x = (int)b.w - (int)sw;
  145. }
  146. if (sh <= (unsigned int)b.h)
  147. v.off_y = ((int)b.h - (int)sh) / 2;
  148. else {
  149. if (v.off_y > 0) v.off_y = 0;
  150. if (v.off_y < (int)b.h - (int)sh)
  151. v.off_y = (int)b.h - (int)sh;
  152. }
  153. if (v.fit) {
  154. v.off_x = ((int)b.w - (int)sw) / 2;
  155. v.off_y = ((int)b.h - (int)sh) / 2;
  156. }
  157. }
  158. static void view_set_scale(struct ngui *ui, unsigned int scale)
  159. {
  160. struct ngui_rect b = view_body(ui);
  161. unsigned int old = view_scale(ui);
  162. unsigned int sx, sy;
  163. int dx, dy;
  164. if (!v.have_img || b.w <= 0 || b.h <= 0)
  165. return;
  166. if (scale < 256) scale = 256;
  167. if (scale > 8u * 65536u) scale = 8u * 65536u;
  168. dx = b.w / 2 - v.off_x;
  169. dy = b.h / 2 - v.off_y;
  170. sx = ((unsigned int)dx << 16) / old;
  171. sy = ((unsigned int)dy << 16) / old;
  172. v.fit = 0;
  173. v.zoom = scale;
  174. v.off_x = (int)(b.w / 2 - (int)(((unsigned long long)sx * scale) >> 16));
  175. v.off_y = (int)(b.h / 2 - (int)(((unsigned long long)sy * scale) >> 16));
  176. clamp_view(ui, scale);
  177. }
  178. static void view_fit(void)
  179. {
  180. v.fit = 1;
  181. }
  182. static void view_one(struct ngui *ui)
  183. {
  184. struct ngui_rect b = view_body(ui);
  185. unsigned int sw;
  186. v.fit = 0;
  187. v.zoom = 65536;
  188. sw = v.iw;
  189. v.off_x = ((int)b.w - (int)sw) / 2;
  190. v.off_y = ((int)b.h - (int)v.ih) / 2;
  191. clamp_view(ui, v.zoom);
  192. }
  193. static void view_zoom_step(struct ngui *ui, int in)
  194. {
  195. unsigned int s = view_scale(ui);
  196. if (in)
  197. s = s + s / 4;
  198. else
  199. s = s - s / 5;
  200. view_set_scale(ui, s);
  201. }
  202. static void view_notice(const char *s)
  203. {
  204. s_copy(v.notice, s, sizeof(v.notice));
  205. v.notice_until = u_ticks_ms() + 4000;
  206. v.dirty = 1;
  207. }
  208. /* Pre-render the image as a screen-aspect RGB565 wallpaper (at most
  209. * 1440x810) and hand it to the WM. The WM has no image decoder; it only
  210. * scales this buffer to the desktop. A mounted data volume gets the
  211. * persistent copy, otherwise the RAM copy is used. */
  212. static void view_set_wallpaper(void)
  213. {
  214. const char *magic = WM_WALLPAPER_MAGIC;
  215. const char *path = WM_WALLPAPER_SHARE;
  216. struct wm_wallpaper_hdr *h;
  217. struct sys_stat st;
  218. struct img_blit b;
  219. unsigned char *buf;
  220. unsigned int sw, sh, tw, th, pixbytes, total, i;
  221. unsigned long long sx, sy, scale, dw, dh;
  222. if (!v.have_img) {
  223. view_notice("No image to use");
  224. return;
  225. }
  226. if (u_screen_size(&sw, &sh) != 0 || !sw || !sh) {
  227. view_notice("Cannot read screen size");
  228. return;
  229. }
  230. tw = sw;
  231. th = sh;
  232. if (tw > WM_WALLPAPER_MAX_W) {
  233. th = (unsigned int)((unsigned long long)th *
  234. WM_WALLPAPER_MAX_W / tw);
  235. tw = WM_WALLPAPER_MAX_W;
  236. }
  237. if (th > WM_WALLPAPER_MAX_H) {
  238. tw = (unsigned int)((unsigned long long)tw *
  239. WM_WALLPAPER_MAX_H / th);
  240. th = WM_WALLPAPER_MAX_H;
  241. }
  242. if (!tw)
  243. tw = 1;
  244. if (!th)
  245. th = 1;
  246. pixbytes = tw * th * 2u;
  247. total = sizeof(*h) + pixbytes;
  248. buf = (unsigned char *)malloc(total);
  249. if (!buf) {
  250. view_notice("Not enough memory");
  251. return;
  252. }
  253. memset(buf, 0, total);
  254. h = (struct wm_wallpaper_hdr *)buf;
  255. for (i = 0; i < sizeof(h->magic); i++)
  256. h->magic[i] = magic[i];
  257. h->width = tw;
  258. h->height = th;
  259. /* Cover the screen, cropping the longer source axis. */
  260. sx = (((unsigned long long)tw << 16) + v.iw - 1) / v.iw;
  261. sy = (((unsigned long long)th << 16) + v.ih - 1) / v.ih;
  262. scale = sx > sy ? sx : sy;
  263. if (!scale)
  264. scale = 1;
  265. dw = ((unsigned long long)v.iw * scale) >> 16;
  266. dh = ((unsigned long long)v.ih * scale) >> 16;
  267. memset(&b, 0, sizeof(b));
  268. b.dst = (unsigned short *)(buf + sizeof(*h));
  269. b.dst_w = (int)tw;
  270. b.dst_h = (int)th;
  271. b.clip_w = (int)tw;
  272. b.clip_h = (int)th;
  273. b.x = ((int)tw - (int)dw) / 2;
  274. b.y = ((int)th - (int)dh) / 2;
  275. b.scale = (unsigned int)scale;
  276. img_draw(&v.img, &b);
  277. if (u_stat("/data", &st) == 0 && st.is_dir)
  278. path = WM_WALLPAPER_DATA;
  279. if (u_syscall3(SYS_WRITE_FILE, (int)path, (int)buf, (int)total) < 0) {
  280. free(buf);
  281. view_notice("Cannot save wallpaper");
  282. return;
  283. }
  284. u_syscall1(SYS_WM_WALLPAPER, 1);
  285. free(buf);
  286. view_notice("Wallpaper set");
  287. }
  288. static void format_status(char *buf, unsigned int cap)
  289. {
  290. unsigned int n = 0;
  291. buf[0] = '\0';
  292. if (v.notice[0]) {
  293. s_copy(buf, v.notice, cap);
  294. return;
  295. }
  296. if (v.have_img) {
  297. unsigned int scale = view_scale(&v.ui);
  298. s_append(buf, &n, img_format_name(v.format), cap);
  299. s_append(buf, &n, " ", cap);
  300. s_append_uint(buf, &n, v.iw, cap);
  301. s_append(buf, &n, "x", cap);
  302. s_append_uint(buf, &n, v.ih, cap);
  303. s_append(buf, &n, " ", cap);
  304. if (v.file_size >= 1024 * 1024) {
  305. s_append_uint(buf, &n, v.file_size / (1024 * 1024), cap);
  306. s_append(buf, &n, " MB", cap);
  307. } else {
  308. s_append_uint(buf, &n, (v.file_size + 1023) / 1024, cap);
  309. s_append(buf, &n, " KB", cap);
  310. }
  311. s_append(buf, &n, " ", cap);
  312. if (v.fit)
  313. s_append(buf, &n, "fit ", cap);
  314. s_append_uint(buf, &n, (scale * 100u) >> 16, cap);
  315. s_append(buf, &n, "%", cap);
  316. } else {
  317. s_append(buf, &n, "view ", cap);
  318. s_append(buf, &n, v.name, cap);
  319. }
  320. }
  321. static void draw_message(struct ngui *ui, struct ngui_rect body,
  322. const char *msg)
  323. {
  324. int tw = (int)ngui_text_width_ui(ui, msg);
  325. int x = body.x + (body.w - tw) / 2;
  326. int y = body.y + body.h / 2 - 8;
  327. if (x < body.x + 6)
  328. x = body.x + 6;
  329. ngui_text(ui, x, y, msg, ui->theme.text);
  330. if (!v.loading) {
  331. const char *hint = "Esc closes";
  332. int hw = (int)ngui_text_width_ui(ui, hint);
  333. ngui_text(ui, body.x + (body.w - hw) / 2, y + 24, hint,
  334. ui->theme.text_dim);
  335. }
  336. }
  337. static void draw_toolbar(struct ngui *ui, struct ngui_rect r)
  338. {
  339. struct ngui_rect b;
  340. int x = r.x + 6;
  341. int by = r.y + (r.h - 20) / 2;
  342. int bw;
  343. ngui_fill_rect(ui, r, ui->theme.panel);
  344. ngui_fill_rect(ui, (struct ngui_rect){r.x, r.y + r.h - 1, r.w, 1},
  345. ui->theme.border);
  346. bw = 40;
  347. b = (struct ngui_rect){x, by, bw, 20};
  348. if (ngui_button(ui, VIEW_ID_FIT, b, "Fit")) {
  349. view_fit();
  350. v.dirty = 1;
  351. }
  352. x += bw + 5;
  353. b = (struct ngui_rect){x, by, 40, 20};
  354. if (ngui_button(ui, VIEW_ID_ONE, b, "1:1")) {
  355. view_one(ui);
  356. v.dirty = 1;
  357. }
  358. x += 45;
  359. b = (struct ngui_rect){x, by, 24, 20};
  360. if (ngui_button(ui, VIEW_ID_OUT, b, "-")) {
  361. view_zoom_step(ui, 0);
  362. v.dirty = 1;
  363. }
  364. x += 29;
  365. b = (struct ngui_rect){x, by, 24, 20};
  366. if (ngui_button(ui, VIEW_ID_IN, b, "+")) {
  367. view_zoom_step(ui, 1);
  368. v.dirty = 1;
  369. }
  370. x += 32;
  371. if (v.have_img && x + 90 < r.x + r.w) {
  372. b = (struct ngui_rect){x, by, 84, 20};
  373. if (ngui_button(ui, VIEW_ID_WALL, b, "Background"))
  374. view_set_wallpaper();
  375. x += 89;
  376. }
  377. if (v.have_img && x + 70 < r.x + r.w) {
  378. char z[16];
  379. unsigned int n = 0;
  380. z[0] = '\0';
  381. s_append_uint(z, &n, (view_scale(ui) * 100u) >> 16, sizeof(z));
  382. s_append(z, &n, "%", sizeof(z));
  383. ngui_text(ui, x, r.y + (r.h - 16) / 2, z, ui->theme.text_dim);
  384. }
  385. }
  386. static void draw(struct ngui *ui)
  387. {
  388. struct ngui_rect all = {0, 0, ui->w, ui->h};
  389. struct ngui_rect r = all;
  390. struct ngui_rect toolbar, status, body;
  391. char line[96];
  392. toolbar = ngui_cut_top(&r, ui->h >= 140 ? VIEW_TOOLBAR_H : 22);
  393. status = ngui_cut_bottom(&r, ui->h >= 140 ? VIEW_STATUS_H : 14);
  394. body = r;
  395. ngui_fill_rect(ui, all, VIEW_BG);
  396. draw_toolbar(ui, toolbar);
  397. ngui_fill_rect(ui, status, ui->theme.panel);
  398. format_status(line, sizeof(line));
  399. ngui_text_ellipsis(ui, status, status.x + 6,
  400. status.y + (status.h - 16) / 2, line,
  401. ui->theme.text_dim);
  402. if (body.w <= 0 || body.h <= 0)
  403. return;
  404. ngui_fill_rect(ui, body, VIEW_VIEW_BG);
  405. if (!v.have_img) {
  406. if (v.error)
  407. draw_message(ui, body, v.err);
  408. else {
  409. char msg[48];
  410. s_copy(msg, "Loading ", sizeof(msg));
  411. {
  412. unsigned int n = s_len(msg);
  413. s_append(msg, &n, v.name, sizeof(msg));
  414. s_append(msg, &n, "...", sizeof(msg));
  415. }
  416. draw_message(ui, body, msg);
  417. }
  418. return;
  419. }
  420. {
  421. unsigned int scale = view_scale(ui);
  422. unsigned int sw =
  423. (unsigned int)(((unsigned long long)v.iw * scale) >> 16);
  424. unsigned int sh =
  425. (unsigned int)(((unsigned long long)v.ih * scale) >> 16);
  426. struct img_blit blit;
  427. if (!sw) sw = 1;
  428. if (!sh) sh = 1;
  429. clamp_view(ui, scale);
  430. blit.dst = ui->px;
  431. blit.dst_w = ui->w;
  432. blit.dst_h = ui->h;
  433. blit.clip_x = body.x;
  434. blit.clip_y = body.y;
  435. blit.clip_w = body.w;
  436. blit.clip_h = body.h;
  437. blit.x = body.x + v.off_x;
  438. blit.y = body.y + v.off_y;
  439. blit.scale = scale;
  440. blit.bg = VIEW_CHK1;
  441. blit.bg2 = VIEW_CHK2;
  442. blit.flags = IMG_BLIT_CHECKER;
  443. if (v.dragging)
  444. blit.flags |= IMG_BLIT_FAST;
  445. if (v.img.alpha)
  446. img_draw(&v.img, &blit);
  447. else {
  448. blit.flags &= ~IMG_BLIT_CHECKER;
  449. img_draw(&v.img, &blit);
  450. }
  451. (void)sw;
  452. (void)sh;
  453. }
  454. }
  455. static int create_window(unsigned int base_w, unsigned int base_h,
  456. const char *title, struct win_info *wi)
  457. {
  458. unsigned int w = base_w, h = base_h;
  459. if (w < 320) w = 320;
  460. if (h < 200) h = 200;
  461. if (w > 960) w = 960;
  462. if (h > 640) h = 640;
  463. for (;;) {
  464. if (u_win_create(w, h + VIEW_BAR_H, title, wi) == 0)
  465. return 0;
  466. if (w <= 240 && h <= 160)
  467. break;
  468. w = w * 4 / 5;
  469. h = h * 4 / 5;
  470. if (w < 240) w = 240;
  471. if (h < 160) h = 160;
  472. }
  473. return -1;
  474. }
  475. static void error_exit(const char *msg)
  476. {
  477. struct win_info wi;
  478. struct ngui ui;
  479. int quit = 0;
  480. if (u_win_create(480, 240 + VIEW_BAR_H, "view", &wi) != 0) {
  481. u_puts("view: ");
  482. u_puts(msg);
  483. u_puts("\n");
  484. u_exit(1);
  485. }
  486. ngui_init(&ui, (unsigned short *)wi.fb, (int)wi.width, (int)wi.height);
  487. ngui_font_attach(&ui);
  488. v.error = 1;
  489. s_copy(v.err, msg, sizeof(v.err));
  490. v.have_img = 0;
  491. for (;;) {
  492. if (!ngui_begin(&ui)) {
  493. u_sleep_ms(10);
  494. continue;
  495. }
  496. {
  497. int k = ngui_key(&ui);
  498. if (k == NGUI_KEY_ESC || k == 'q' || k == 0x03)
  499. quit = 1;
  500. }
  501. draw(&ui);
  502. ngui_end(&ui);
  503. if (quit)
  504. break;
  505. u_sleep_ms(10);
  506. }
  507. u_win_close();
  508. u_exit(1);
  509. }
  510. void _start(const char *arg)
  511. {
  512. struct img_info info;
  513. struct win_info wi;
  514. struct img_options opt = {0};
  515. struct sys_stat st;
  516. unsigned int n, fcap, est;
  517. int r;
  518. if (!arg || !arg[0]) {
  519. u_puts("usage: view <file>\n");
  520. u_exit(1);
  521. }
  522. base_name(arg, v.name, sizeof(v.name));
  523. s_copy(v.title, "view ", sizeof(v.title));
  524. {
  525. unsigned int tn = s_len(v.title);
  526. s_append(v.title, &tn, v.name, sizeof(v.title));
  527. }
  528. fcap = VIEW_FILE_CAP;
  529. if (u_stat(arg, &st) == 0 && !st.is_dir && st.size > 0 &&
  530. st.size < VIEW_FILE_CAP)
  531. fcap = st.size + 1u;
  532. v.file = (unsigned char *)malloc(fcap);
  533. if (!v.file)
  534. error_exit("Not enough memory for the image");
  535. n = (unsigned int)img_read_file(arg, v.file, fcap);
  536. if ((int)n < 0) {
  537. if ((int)n == IMG_ERR_TOOBIG)
  538. error_exit("Image file is larger than 4 MB");
  539. error_exit(img_error_string((int)n));
  540. }
  541. v.file_size = n;
  542. r = img_sniff(v.file, n, &info);
  543. if (r != 0)
  544. error_exit(img_error_string(r));
  545. est = img_estimate(&info);
  546. if (est == 0xFFFFFFFFu || est > VIEW_HEAP_MAX ||
  547. (unsigned long long)n + est > VIEW_HEAP_MAX)
  548. error_exit("Image needs more than 32 MB");
  549. v.arena = (unsigned char *)malloc(est);
  550. if (!v.arena)
  551. error_exit("Not enough memory for the image");
  552. img_mem_init(&v.mem, v.arena, est);
  553. v.iw = info.width;
  554. v.ih = info.height;
  555. v.format = info.format;
  556. if (create_window(info.width, info.height, v.title, &wi) != 0) {
  557. u_puts("view: no graphics window\n");
  558. u_exit(1);
  559. }
  560. ngui_init(&v.ui, (unsigned short *)wi.fb, (int)wi.width, (int)wi.height);
  561. ngui_font_attach(&v.ui);
  562. v.fit = 1;
  563. v.zoom = 65536;
  564. v.loading = 1;
  565. v.dirty = 1;
  566. v.last_w = (int)wi.width;
  567. v.last_h = (int)wi.height;
  568. {
  569. struct ngui *ui = &v.ui;
  570. /* Draw the loading frame once; the input loop starts after decode. */
  571. ngui_begin(ui);
  572. draw(ui);
  573. ngui_end(ui);
  574. }
  575. opt.max_pixels = 0;
  576. r = img_decode(v.file, n, &opt, &v.mem, &v.img);
  577. v.loading = 0;
  578. if (r != 0) {
  579. s_copy(v.err, img_error_string(r), sizeof(v.err));
  580. v.error = 1;
  581. v.have_img = 0;
  582. } else {
  583. v.have_img = 1;
  584. v.error = 0;
  585. v.iw = v.img.w;
  586. v.ih = v.img.h;
  587. v.format = v.img.format;
  588. v.fit = 1;
  589. }
  590. if (v.have_img && v.format == IMG_FMT_GIF) {
  591. unsigned int frames = 0, delay = 100;
  592. int ha = 0;
  593. if (img_gif_frames(v.file, v.file_size, &frames, &delay, &ha) == 0 &&
  594. frames > 1) {
  595. v.gif_frames = frames;
  596. v.gif_index = 0;
  597. v.gif_delay = delay;
  598. v.gif_next_ms = u_ticks_ms() + delay;
  599. }
  600. }
  601. v.dirty = 1;
  602. for (;;) {
  603. struct ngui *ui = &v.ui;
  604. struct ngui_rect body = view_body(ui);
  605. int close = 0;
  606. int k;
  607. unsigned int now = u_ticks_ms();
  608. if (v.notice[0] && (int)(now - v.notice_until) >= 0) {
  609. v.notice[0] = '\0';
  610. v.dirty = 1;
  611. }
  612. if (v.gif_frames > 1 &&
  613. (int)(now - v.gif_next_ms) >= 0) {
  614. unsigned int d = v.gif_delay;
  615. v.gif_index = (v.gif_index + 1) % v.gif_frames;
  616. if (img_decode_gif_frame(v.file, v.file_size,
  617. v.gif_index, &v.mem, &v.img,
  618. &d) == 0) {
  619. if (d < 20) d = 20;
  620. v.gif_delay = d;
  621. v.dirty = 1;
  622. } else {
  623. v.gif_frames = 0;
  624. }
  625. v.gif_next_ms = u_ticks_ms() + v.gif_delay;
  626. }
  627. ngui_begin(ui);
  628. if (ui->w != v.last_w || ui->h != v.last_h) {
  629. /* size changed; redraw at the new dimensions */
  630. v.last_w = ui->w;
  631. v.last_h = ui->h;
  632. v.dirty = 1;
  633. }
  634. while ((k = ngui_key(ui)) != 0) {
  635. if (k == NGUI_KEY_ESC || k == 'q' || k == 0x03) {
  636. close = 1;
  637. break;
  638. } else if (k == 'f' || k == '0') {
  639. view_fit();
  640. v.dirty = 1;
  641. } else if (k == '1') {
  642. view_one(ui);
  643. v.dirty = 1;
  644. } else if (k == '+' || k == '=') {
  645. view_zoom_step(ui, 1);
  646. v.dirty = 1;
  647. } else if (k == '-' || k == '_') {
  648. view_zoom_step(ui, 0);
  649. v.dirty = 1;
  650. } else if (k == 'b' || k == 'B') {
  651. view_set_wallpaper();
  652. } else if (k == NGUI_KEY_LEFT || k == NGUI_KEY_RIGHT ||
  653. k == NGUI_KEY_UP || k == NGUI_KEY_DOWN) {
  654. if (v.have_img) {
  655. if (v.fit)
  656. view_set_scale(ui, view_scale(ui));
  657. if (k == NGUI_KEY_LEFT)
  658. v.off_x += 32;
  659. if (k == NGUI_KEY_RIGHT)
  660. v.off_x -= 32;
  661. if (k == NGUI_KEY_UP)
  662. v.off_y += 32;
  663. if (k == NGUI_KEY_DOWN)
  664. v.off_y -= 32;
  665. clamp_view(ui, view_scale(ui));
  666. v.dirty = 1;
  667. }
  668. }
  669. }
  670. if (close)
  671. break;
  672. if (ui->pressed && ui->press_x >= body.x && ui->press_y >= body.y &&
  673. ui->press_x < body.x + body.w && ui->press_y < body.y + body.h &&
  674. v.have_img) {
  675. v.dragging = 1;
  676. v.drag_x = ui->mouse_x;
  677. v.drag_y = ui->mouse_y;
  678. v.dirty = 1;
  679. }
  680. if (v.dragging) {
  681. if (!(ui->buttons & 1) || ui->released) {
  682. v.dragging = 0;
  683. v.dirty = 1;
  684. } else {
  685. int dx = ui->mouse_x - v.drag_x;
  686. int dy = ui->mouse_y - v.drag_y;
  687. if (dx || dy) {
  688. v.off_x += dx;
  689. v.off_y += dy;
  690. v.drag_x = ui->mouse_x;
  691. v.drag_y = ui->mouse_y;
  692. clamp_view(ui, view_scale(ui));
  693. v.dirty = 1;
  694. }
  695. }
  696. }
  697. if (ui->pressed || ui->released)
  698. v.dirty = 1;
  699. if (v.dirty) {
  700. v.dirty = 0;
  701. draw(ui);
  702. ngui_end_ex(ui, 1);
  703. } else {
  704. ngui_end_ex(ui, 0);
  705. }
  706. u_sleep_ms(10);
  707. }
  708. u_win_close();
  709. u_exit(0);
  710. }