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.
 
 
 
 
 
 

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