A Simple X Image Viewer
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

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