A Simple X Image Viewer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

561 line
12 KiB

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