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.
 
 
 
 
 
 

557 lines
12 KiB

  1. /* sxiv: main.c
  2. * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
  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();
  47. void reset_cursor();
  48. void animate();
  49. void slideshow();
  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() {
  65. static int in = 0;
  66. if (!in++) {
  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 || !*filename)
  74. return;
  75. if (access(filename, R_OK)) {
  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) {
  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)
  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)
  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))
  177. filesize = fstats.st_size;
  178. else
  179. filesize = 0;
  180. if (img.multi.cnt && 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() {
  186. int n;
  187. char sshow_info[16];
  188. char frame_info[16];
  189. float size, time;
  190. const char *size_unit, *time_unit;
  191. if (mode == MODE_THUMB) {
  192. n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] %s",
  193. tns.cnt ? tns.sel + 1 : 0, tns.cnt,
  194. tns.cnt ? files[tns.sel].name : "");
  195. } else {
  196. size = filesize;
  197. size_readable(&size, &size_unit);
  198. if (img.slideshow) {
  199. time = img.ss_delay / 1000.0;
  200. time_readable(&time, &time_unit);
  201. snprintf(sshow_info, sizeof(sshow_info), "*%d%s* ",
  202. (int) time, time_unit);
  203. } else {
  204. sshow_info[0] = '\0';
  205. }
  206. if (img.multi.cnt)
  207. snprintf(frame_info, sizeof(frame_info), "{%d/%d} ",
  208. img.multi.sel + 1, img.multi.cnt);
  209. else
  210. frame_info[0] = '\0';
  211. n = snprintf(win_title, TITLE_LEN,
  212. "sxiv: [%d/%d] <%dx%d:%d%%> (%.2f%s) %s%s%s",
  213. fileidx + 1, filecnt, img.w, img.h,
  214. (int) (img.zoom * 100.0), size, size_unit,
  215. sshow_info, frame_info, files[fileidx].name);
  216. }
  217. if (n >= TITLE_LEN) {
  218. for (n = 0; n < 3; n++)
  219. win_title[TITLE_LEN - n - 2] = '.';
  220. }
  221. win_set_title(&win, win_title);
  222. }
  223. void redraw() {
  224. if (mode == MODE_IMAGE) {
  225. img_render(&img);
  226. if (img.slideshow && !img.multi.animate) {
  227. if (fileidx + 1 < filecnt)
  228. set_timeout(slideshow, img.ss_delay, true);
  229. else
  230. img.slideshow = false;
  231. }
  232. } else {
  233. tns_render(&tns);
  234. }
  235. update_title();
  236. reset_timeout(redraw);
  237. reset_cursor();
  238. }
  239. void reset_cursor() {
  240. int i;
  241. cursor_t cursor = CURSOR_NONE;
  242. if (mode == MODE_IMAGE) {
  243. for (i = 0; i < ARRLEN(timeouts); i++) {
  244. if (timeouts[i].handler == reset_cursor) {
  245. if (timeouts[i].active)
  246. cursor = CURSOR_ARROW;
  247. break;
  248. }
  249. }
  250. } else {
  251. if (tns.cnt != filecnt)
  252. cursor = CURSOR_WATCH;
  253. else
  254. cursor = CURSOR_ARROW;
  255. }
  256. win_set_cursor(&win, cursor);
  257. }
  258. void animate() {
  259. if (img_frame_animate(&img, false)) {
  260. redraw();
  261. set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
  262. }
  263. }
  264. void slideshow() {
  265. if (mode == MODE_IMAGE && !img.multi.animate) {
  266. if (fileidx + 1 < filecnt) {
  267. load_image(fileidx + 1);
  268. redraw();
  269. } else {
  270. img.slideshow = false;
  271. }
  272. }
  273. }
  274. bool keymask(const keymap_t *k, unsigned int state) {
  275. return (k->ctrl ? ControlMask : 0) == (state & ControlMask);
  276. }
  277. bool buttonmask(const button_t *b, unsigned int state) {
  278. return ((b->ctrl ? ControlMask : 0) | (b->shift ? ShiftMask : 0)) ==
  279. (state & (ControlMask | ShiftMask));
  280. }
  281. void on_keypress(XKeyEvent *kev) {
  282. int i;
  283. KeySym ksym;
  284. char key;
  285. if (!kev)
  286. return;
  287. XLookupString(kev, &key, 1, &ksym, NULL);
  288. for (i = 0; i < ARRLEN(keys); i++) {
  289. if (keys[i].ksym == ksym && keymask(&keys[i], kev->state)) {
  290. if (keys[i].cmd && keys[i].cmd(keys[i].arg))
  291. redraw();
  292. return;
  293. }
  294. }
  295. }
  296. void on_buttonpress(XButtonEvent *bev) {
  297. int i, sel;
  298. if (!bev)
  299. return;
  300. if (mode == MODE_IMAGE) {
  301. win_set_cursor(&win, CURSOR_ARROW);
  302. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  303. for (i = 0; i < ARRLEN(buttons); i++) {
  304. if (buttons[i].button == bev->button &&
  305. buttonmask(&buttons[i], bev->state))
  306. {
  307. if (buttons[i].cmd && buttons[i].cmd(buttons[i].arg))
  308. redraw();
  309. return;
  310. }
  311. }
  312. } else {
  313. /* thumbnail mode (hard-coded) */
  314. switch (bev->button) {
  315. case Button1:
  316. if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
  317. if (sel == tns.sel) {
  318. mode = MODE_IMAGE;
  319. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  320. load_image(tns.sel);
  321. } else {
  322. tns_highlight(&tns, tns.sel, false);
  323. tns_highlight(&tns, sel, true);
  324. tns.sel = sel;
  325. }
  326. redraw();
  327. break;
  328. }
  329. break;
  330. case Button4:
  331. case Button5:
  332. if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN))
  333. redraw();
  334. break;
  335. }
  336. }
  337. }
  338. void run() {
  339. int xfd;
  340. fd_set fds;
  341. struct timeval timeout;
  342. XEvent ev;
  343. redraw();
  344. while (1) {
  345. while (mode == MODE_THUMB && tns.cnt < filecnt &&
  346. !XPending(win.env.dpy))
  347. {
  348. /* load thumbnails */
  349. set_timeout(redraw, TO_REDRAW_THUMBS, false);
  350. if (tns_load(&tns, tns.cnt, &files[tns.cnt], false, false))
  351. tns.cnt++;
  352. else
  353. remove_file(tns.cnt, false);
  354. if (tns.cnt == filecnt)
  355. redraw();
  356. else
  357. check_timeouts(NULL);
  358. }
  359. while (!XPending(win.env.dpy) && check_timeouts(&timeout)) {
  360. /* wait for timeouts */
  361. xfd = ConnectionNumber(win.env.dpy);
  362. FD_ZERO(&fds);
  363. FD_SET(xfd, &fds);
  364. select(xfd + 1, &fds, 0, 0, &timeout);
  365. }
  366. if (!XNextEvent(win.env.dpy, &ev)) {
  367. /* handle events */
  368. switch (ev.type) {
  369. case ButtonPress:
  370. on_buttonpress(&ev.xbutton);
  371. break;
  372. case ClientMessage:
  373. if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
  374. return;
  375. break;
  376. case ConfigureNotify:
  377. if (win_configure(&win, &ev.xconfigure)) {
  378. set_timeout(redraw, TO_REDRAW_RESIZE, false);
  379. if (mode == MODE_IMAGE)
  380. img.checkpan = true;
  381. else
  382. tns.dirty = true;
  383. }
  384. break;
  385. case KeyPress:
  386. on_keypress(&ev.xkey);
  387. break;
  388. case MotionNotify:
  389. if (mode == MODE_IMAGE) {
  390. win_set_cursor(&win, CURSOR_ARROW);
  391. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  392. }
  393. break;
  394. }
  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) {
  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)
  433. free(filename);
  434. } else {
  435. for (i = 0; i < options->filecnt; i++) {
  436. filename = options->filenames[i];
  437. if (stat(filename, &fstats) || !S_ISDIR(fstats.st_mode)) {
  438. check_add_file(filename);
  439. } else {
  440. if (!options->recursive) {
  441. warn("ignoring directory: %s", filename);
  442. continue;
  443. }
  444. if (r_opendir(&dir, filename)) {
  445. warn("could not open directory: %s", filename);
  446. continue;
  447. }
  448. start = fileidx;
  449. while ((filename = r_readdir(&dir))) {
  450. check_add_file(filename);
  451. free((void*) filename);
  452. }
  453. r_closedir(&dir);
  454. if (fileidx - start > 1)
  455. qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
  456. }
  457. }
  458. }
  459. if (!fileidx) {
  460. fprintf(stderr, "sxiv: no valid image file given, aborting\n");
  461. exit(EXIT_FAILURE);
  462. }
  463. filecnt = fileidx;
  464. fileidx = options->startnum < filecnt ? options->startnum : 0;
  465. win_init(&win);
  466. img_init(&img, &win);
  467. if (options->thumb_mode) {
  468. mode = MODE_THUMB;
  469. tns_init(&tns, filecnt, &win);
  470. while (!tns_load(&tns, 0, &files[0], false, false))
  471. remove_file(0, false);
  472. tns.cnt = 1;
  473. } else {
  474. mode = MODE_IMAGE;
  475. tns.thumbs = NULL;
  476. load_image(fileidx);
  477. }
  478. win_open(&win);
  479. run();
  480. cleanup();
  481. return 0;
  482. }