A Simple X Image Viewer
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

603 wiersze
13 KiB

  1. /* sxiv: main.c
  2. * Copyright (c) 2012 Bert Muennich <be.muennich at googlemail.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or (at your
  7. * option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. */
  18. #define _POSIX_C_SOURCE 200112L
  19. #define _MAPPINGS_CONFIG
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include <sys/select.h>
  25. #include <sys/stat.h>
  26. #include <sys/time.h>
  27. #include <X11/Xutil.h>
  28. #include <X11/keysym.h>
  29. #include "commands.h"
  30. #include "image.h"
  31. #include "options.h"
  32. #include "thumbs.h"
  33. #include "types.h"
  34. #include "util.h"
  35. #include "window.h"
  36. #include "config.h"
  37. enum {
  38. INFO_STR_LEN = 256,
  39. FILENAME_CNT = 1024
  40. };
  41. typedef struct {
  42. struct timeval when;
  43. bool active;
  44. timeout_f handler;
  45. } timeout_t;
  46. /* timeout handler functions: */
  47. void redraw(void);
  48. void reset_cursor(void);
  49. void animate(void);
  50. void clear_resize(void);
  51. appmode_t mode;
  52. img_t img;
  53. tns_t tns;
  54. win_t win;
  55. fileinfo_t *files;
  56. int filecnt, fileidx;
  57. int alternate;
  58. size_t filesize;
  59. int prefix;
  60. bool resized = false;
  61. char win_bar_l[INFO_STR_LEN];
  62. char win_bar_r[INFO_STR_LEN];
  63. char win_title[INFO_STR_LEN];
  64. timeout_t timeouts[] = {
  65. { { 0, 0 }, false, redraw },
  66. { { 0, 0 }, false, reset_cursor },
  67. { { 0, 0 }, false, animate },
  68. { { 0, 0 }, false, clear_resize },
  69. };
  70. void cleanup(void) {
  71. static bool in = false;
  72. if (!in) {
  73. in = true;
  74. img_close(&img, false);
  75. tns_free(&tns);
  76. win_close(&win);
  77. }
  78. }
  79. void check_add_file(char *filename) {
  80. const char *bn;
  81. if (filename == NULL || *filename == '\0')
  82. return;
  83. if (access(filename, R_OK) < 0) {
  84. warn("could not open file: %s", filename);
  85. return;
  86. }
  87. if (fileidx == filecnt) {
  88. filecnt *= 2;
  89. files = (fileinfo_t*) s_realloc(files, filecnt * sizeof(fileinfo_t));
  90. }
  91. if (*filename != '/') {
  92. files[fileidx].path = absolute_path(filename);
  93. if (files[fileidx].path == NULL) {
  94. warn("could not get absolute path of file: %s\n", filename);
  95. return;
  96. }
  97. }
  98. files[fileidx].loaded = false;
  99. files[fileidx].name = s_strdup(filename);
  100. if (*filename == '/')
  101. files[fileidx].path = files[fileidx].name;
  102. if ((bn = strrchr(files[fileidx].name , '/')) != NULL && bn[1] != '\0')
  103. files[fileidx].base = ++bn;
  104. else
  105. files[fileidx].base = files[fileidx].name;
  106. fileidx++;
  107. }
  108. void remove_file(int n, bool manual) {
  109. if (n < 0 || n >= filecnt)
  110. return;
  111. if (filecnt == 1) {
  112. if (!manual)
  113. fprintf(stderr, "sxiv: no more files to display, aborting\n");
  114. cleanup();
  115. exit(manual ? EXIT_SUCCESS : EXIT_FAILURE);
  116. }
  117. if (files[n].path != files[n].name)
  118. free((void*) files[n].path);
  119. free((void*) files[n].name);
  120. if (n + 1 < filecnt)
  121. memmove(files + n, files + n + 1, (filecnt - n - 1) * sizeof(fileinfo_t));
  122. if (n + 1 < tns.cnt) {
  123. memmove(tns.thumbs + n, tns.thumbs + n + 1, (tns.cnt - n - 1) *
  124. sizeof(thumb_t));
  125. memset(tns.thumbs + tns.cnt - 1, 0, sizeof(thumb_t));
  126. }
  127. filecnt--;
  128. if (n < tns.cnt)
  129. tns.cnt--;
  130. }
  131. void set_timeout(timeout_f handler, int time, bool overwrite) {
  132. int i;
  133. for (i = 0; i < ARRLEN(timeouts); i++) {
  134. if (timeouts[i].handler == handler) {
  135. if (!timeouts[i].active || overwrite) {
  136. gettimeofday(&timeouts[i].when, 0);
  137. TV_ADD_MSEC(&timeouts[i].when, time);
  138. timeouts[i].active = true;
  139. }
  140. return;
  141. }
  142. }
  143. }
  144. void reset_timeout(timeout_f handler) {
  145. int i;
  146. for (i = 0; i < ARRLEN(timeouts); i++) {
  147. if (timeouts[i].handler == handler) {
  148. timeouts[i].active = false;
  149. return;
  150. }
  151. }
  152. }
  153. bool check_timeouts(struct timeval *t) {
  154. int i = 0, tdiff, tmin = -1;
  155. struct timeval now;
  156. gettimeofday(&now, 0);
  157. while (i < ARRLEN(timeouts)) {
  158. if (timeouts[i].active) {
  159. tdiff = TV_DIFF(&timeouts[i].when, &now);
  160. if (tdiff <= 0) {
  161. timeouts[i].active = false;
  162. if (timeouts[i].handler != NULL)
  163. timeouts[i].handler();
  164. i = tmin = -1;
  165. } else if (tmin < 0 || tdiff < tmin) {
  166. tmin = tdiff;
  167. }
  168. }
  169. i++;
  170. }
  171. if (tmin > 0 && t != NULL)
  172. TV_SET_MSEC(t, tmin);
  173. return tmin > 0;
  174. }
  175. void load_image(int new) {
  176. struct stat fstats;
  177. if (new < 0 || new >= filecnt)
  178. return;
  179. win_set_cursor(&win, CURSOR_WATCH);
  180. img_close(&img, false);
  181. while (!img_load(&img, &files[new])) {
  182. remove_file(new, false);
  183. if (new >= filecnt)
  184. new = filecnt - 1;
  185. }
  186. files[new].loaded = true;
  187. alternate = fileidx;
  188. fileidx = new;
  189. if (stat(files[new].path, &fstats) == 0)
  190. filesize = fstats.st_size;
  191. else
  192. filesize = 0;
  193. if (img.multi.cnt > 0 && img.multi.animate)
  194. set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
  195. else
  196. reset_timeout(animate);
  197. }
  198. void update_info(void) {
  199. int i, fw, pw, fi, ln, rn;
  200. char frame_info[16];
  201. const char *size_unit;
  202. float size = filesize;
  203. pw = 0;
  204. for (i = filecnt; i > 0; i /= 10)
  205. pw++;
  206. if (mode == MODE_THUMB) {
  207. if (tns.cnt != filecnt) {
  208. snprintf(win_bar_l, sizeof win_bar_l, "Loading... %0*d/%d",
  209. pw, tns.cnt, filecnt);
  210. } else {
  211. fi = snprintf(win_bar_l, sizeof win_bar_l, "%0*d/%d%s",
  212. pw, tns.sel + 1, filecnt, BAR_SEPARATOR);
  213. ln = snprintf(win_bar_l + fi, sizeof win_bar_l - fi, "%s",
  214. files[tns.sel].name) + fi;
  215. if (win_textwidth(win_bar_l, ln, true) > win.w)
  216. snprintf(win_bar_l + fi, sizeof win_bar_l - fi, "%s",
  217. files[tns.sel].base);
  218. }
  219. win_set_title(&win, "sxiv");
  220. win_set_bar_info(&win, win_bar_l, NULL);
  221. } else {
  222. size_readable(&size, &size_unit);
  223. if (img.multi.cnt > 0) {
  224. fw = 0;
  225. for (i = img.multi.cnt; i > 0; i /= 10)
  226. fw++;
  227. snprintf(frame_info, sizeof frame_info, "%s%0*d/%d",
  228. BAR_SEPARATOR, fw, img.multi.sel+1, img.multi.cnt);
  229. } else {
  230. frame_info[0] = '\0';
  231. }
  232. fi = snprintf(win_bar_l, sizeof win_bar_l, "%0*d/%d%s",
  233. pw, fileidx + 1, filecnt, BAR_SEPARATOR);
  234. ln = snprintf(win_bar_l + fi, sizeof win_bar_l - fi, "%s",
  235. files[fileidx].name) + fi;
  236. rn = snprintf(win_bar_r, sizeof win_bar_r, "%.2f%s%s%dx%d%s%3d%%%s",
  237. size, size_unit, BAR_SEPARATOR, img.w, img.h, BAR_SEPARATOR,
  238. (int) (img.zoom * 100.0), frame_info);
  239. if (win_textwidth(win_bar_l, ln, true) +
  240. win_textwidth(win_bar_r, rn, true) > win.w)
  241. {
  242. snprintf(win_bar_l + fi, sizeof win_bar_l - fi, "%s",
  243. files[fileidx].base);
  244. }
  245. win_set_bar_info(&win, win_bar_l, win_bar_r);
  246. snprintf(win_title, sizeof win_title, "sxiv - %s", files[fileidx].name);
  247. win_set_title(&win, win_title);
  248. }
  249. }
  250. void redraw(void) {
  251. if (mode == MODE_IMAGE)
  252. img_render(&img);
  253. else
  254. tns_render(&tns);
  255. update_info();
  256. win_draw(&win);
  257. reset_timeout(redraw);
  258. reset_cursor();
  259. }
  260. void reset_cursor(void) {
  261. int i;
  262. cursor_t cursor = CURSOR_NONE;
  263. if (mode == MODE_IMAGE) {
  264. for (i = 0; i < ARRLEN(timeouts); i++) {
  265. if (timeouts[i].handler == reset_cursor) {
  266. if (timeouts[i].active)
  267. cursor = CURSOR_ARROW;
  268. break;
  269. }
  270. }
  271. } else {
  272. if (tns.cnt != filecnt)
  273. cursor = CURSOR_WATCH;
  274. else
  275. cursor = CURSOR_ARROW;
  276. }
  277. win_set_cursor(&win, cursor);
  278. }
  279. void animate(void) {
  280. if (img_frame_animate(&img, false)) {
  281. redraw();
  282. set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
  283. }
  284. }
  285. void clear_resize(void) {
  286. resized = false;
  287. }
  288. bool keymask(const keymap_t *k, unsigned int state) {
  289. return (k->ctrl ? ControlMask : 0) == (state & ControlMask);
  290. }
  291. bool buttonmask(const button_t *b, unsigned int state) {
  292. return ((b->ctrl ? ControlMask : 0) | (b->shift ? ShiftMask : 0)) ==
  293. (state & (ControlMask | ShiftMask));
  294. }
  295. void on_keypress(XKeyEvent *kev) {
  296. int i;
  297. KeySym ksym;
  298. char key;
  299. if (kev == NULL)
  300. return;
  301. XLookupString(kev, &key, 1, &ksym, NULL);
  302. if ((ksym == XK_Escape || (key >= '0' && key <= '9')) &&
  303. (kev->state & ControlMask) == 0)
  304. {
  305. /* number prefix for commands */
  306. prefix = ksym == XK_Escape ? 0 : prefix * 10 + (int) (key - '0');
  307. return;
  308. }
  309. for (i = 0; i < ARRLEN(keys); i++) {
  310. if (keys[i].ksym == ksym && keymask(&keys[i], kev->state)) {
  311. if (keys[i].cmd != NULL && keys[i].cmd(keys[i].arg))
  312. redraw();
  313. prefix = 0;
  314. return;
  315. }
  316. }
  317. }
  318. void on_buttonpress(XButtonEvent *bev) {
  319. int i, sel;
  320. if (bev == NULL)
  321. return;
  322. if (mode == MODE_IMAGE) {
  323. win_set_cursor(&win, CURSOR_ARROW);
  324. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  325. for (i = 0; i < ARRLEN(buttons); i++) {
  326. if (buttons[i].button == bev->button &&
  327. buttonmask(&buttons[i], bev->state))
  328. {
  329. if (buttons[i].cmd != NULL && buttons[i].cmd(buttons[i].arg))
  330. redraw();
  331. return;
  332. }
  333. }
  334. } else {
  335. /* thumbnail mode (hard-coded) */
  336. switch (bev->button) {
  337. case Button1:
  338. if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
  339. if (sel == tns.sel) {
  340. mode = MODE_IMAGE;
  341. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  342. load_image(tns.sel);
  343. } else {
  344. tns_highlight(&tns, tns.sel, false);
  345. tns_highlight(&tns, sel, true);
  346. tns.sel = sel;
  347. }
  348. redraw();
  349. break;
  350. }
  351. break;
  352. case Button4:
  353. case Button5:
  354. if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN,
  355. (bev->state & ControlMask) != 0))
  356. redraw();
  357. break;
  358. }
  359. }
  360. }
  361. void run(void) {
  362. int xfd;
  363. fd_set fds;
  364. struct timeval timeout;
  365. XEvent ev, nextev;
  366. unsigned int qlen;
  367. redraw();
  368. while (true) {
  369. while (mode == MODE_THUMB && tns.cnt < filecnt &&
  370. XPending(win.env.dpy) == 0)
  371. {
  372. /* load thumbnails */
  373. set_timeout(redraw, TO_REDRAW_THUMBS, false);
  374. if (tns_load(&tns, tns.cnt, &files[tns.cnt], false, false)) {
  375. tns.cnt++;
  376. } else {
  377. remove_file(tns.cnt, false);
  378. if (tns.sel >= tns.cnt)
  379. tns.sel--;
  380. }
  381. if (tns.cnt == filecnt)
  382. redraw();
  383. else
  384. check_timeouts(NULL);
  385. }
  386. while (XPending(win.env.dpy) == 0 && check_timeouts(&timeout)) {
  387. /* wait for timeouts */
  388. xfd = ConnectionNumber(win.env.dpy);
  389. FD_ZERO(&fds);
  390. FD_SET(xfd, &fds);
  391. select(xfd + 1, &fds, 0, 0, &timeout);
  392. }
  393. do {
  394. XNextEvent(win.env.dpy, &ev);
  395. qlen = XEventsQueued(win.env.dpy, QueuedAlready);
  396. if (qlen > 0)
  397. XPeekEvent(win.env.dpy, &nextev);
  398. } while (qlen > 0 && ev.type == nextev.type);
  399. switch (ev.type) {
  400. /* handle events */
  401. case ButtonPress:
  402. on_buttonpress(&ev.xbutton);
  403. break;
  404. case ClientMessage:
  405. if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
  406. return;
  407. break;
  408. case ConfigureNotify:
  409. if (win_configure(&win, &ev.xconfigure)) {
  410. if (mode == MODE_IMAGE)
  411. img.checkpan = true;
  412. else
  413. tns.dirty = true;
  414. if (!resized || win.fullscreen) {
  415. redraw();
  416. set_timeout(clear_resize, TO_REDRAW_RESIZE, false);
  417. resized = true;
  418. } else {
  419. set_timeout(redraw, TO_REDRAW_RESIZE, false);
  420. }
  421. }
  422. break;
  423. case KeyPress:
  424. if (qlen == 0 || ev.xkey.keycode != nextev.xkey.keycode)
  425. on_keypress(&ev.xkey);
  426. break;
  427. case MotionNotify:
  428. if (mode == MODE_IMAGE) {
  429. win_set_cursor(&win, CURSOR_ARROW);
  430. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  431. }
  432. break;
  433. }
  434. }
  435. }
  436. int fncmp(const void *a, const void *b) {
  437. return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
  438. }
  439. int main(int argc, char **argv) {
  440. int i, start;
  441. size_t n;
  442. ssize_t len;
  443. char *filename;
  444. struct stat fstats;
  445. r_dir_t dir;
  446. parse_options(argc, argv);
  447. if (options->clean_cache) {
  448. tns_init(&tns, 0, NULL);
  449. tns_clean_cache(&tns);
  450. exit(EXIT_SUCCESS);
  451. }
  452. if (options->filecnt == 0) {
  453. print_usage();
  454. exit(EXIT_FAILURE);
  455. }
  456. if (options->recursive || options->from_stdin)
  457. filecnt = FILENAME_CNT;
  458. else
  459. filecnt = options->filecnt;
  460. files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t));
  461. fileidx = 0;
  462. /* build file list: */
  463. if (options->from_stdin) {
  464. filename = NULL;
  465. while ((len = get_line(&filename, &n, stdin)) > 0) {
  466. if (filename[len-1] == '\n')
  467. filename[len-1] = '\0';
  468. check_add_file(filename);
  469. }
  470. if (filename != NULL)
  471. free(filename);
  472. } else {
  473. for (i = 0; i < options->filecnt; i++) {
  474. filename = options->filenames[i];
  475. if (stat(filename, &fstats) < 0) {
  476. warn("could not stat file: %s", filename);
  477. continue;
  478. }
  479. if (!S_ISDIR(fstats.st_mode)) {
  480. check_add_file(filename);
  481. } else {
  482. if (!options->recursive) {
  483. warn("ignoring directory: %s", filename);
  484. continue;
  485. }
  486. if (r_opendir(&dir, filename) < 0) {
  487. warn("could not open directory: %s", filename);
  488. continue;
  489. }
  490. start = fileidx;
  491. while ((filename = r_readdir(&dir)) != NULL) {
  492. check_add_file(filename);
  493. free((void*) filename);
  494. }
  495. r_closedir(&dir);
  496. if (fileidx - start > 1)
  497. qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
  498. }
  499. }
  500. }
  501. if (fileidx == 0) {
  502. fprintf(stderr, "sxiv: no valid image file given, aborting\n");
  503. exit(EXIT_FAILURE);
  504. }
  505. filecnt = fileidx;
  506. fileidx = options->startnum < filecnt ? options->startnum : 0;
  507. win_init(&win);
  508. img_init(&img, &win);
  509. if (options->thumb_mode) {
  510. mode = MODE_THUMB;
  511. tns_init(&tns, filecnt, &win);
  512. while (!tns_load(&tns, 0, &files[0], false, false))
  513. remove_file(0, false);
  514. tns.cnt = 1;
  515. } else {
  516. mode = MODE_IMAGE;
  517. tns.thumbs = NULL;
  518. load_image(fileidx);
  519. }
  520. win_open(&win);
  521. run();
  522. cleanup();
  523. return 0;
  524. }