A Simple X Image Viewer
 
 
 
 
 
 

561 行
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. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include <sys/stat.h>
  24. #include <sys/time.h>
  25. #include <X11/Xutil.h>
  26. #include <X11/keysym.h>
  27. #include "commands.h"
  28. #include "image.h"
  29. #include "options.h"
  30. #include "thumbs.h"
  31. #include "types.h"
  32. #include "util.h"
  33. #include "window.h"
  34. #define _MAPPINGS_CONFIG
  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, 0);
  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 = 0;
  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, unsigned char silent) {
  97. if (n < 0 || n >= filecnt)
  98. return;
  99. if (filecnt == 1) {
  100. if (!silent)
  101. fprintf(stderr, "sxiv: no more files to display, aborting\n");
  102. cleanup();
  103. exit(!silent);
  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, int 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. MSEC_ADD_TO_TIMEVAL(time, &timeouts[i].when);
  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. int 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 = TIMEDIFF(&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. MSEC_TO_TIMEVAL(tmin, t);
  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, 0);
  169. while (!img_load(&img, &files[new])) {
  170. remove_file(new, 0);
  171. if (new >= filecnt)
  172. new = filecnt - 1;
  173. }
  174. files[new].loaded = 1;
  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, 1);
  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, &win);
  226. if (img.slideshow && !img.multi.animate) {
  227. if (fileidx + 1 < filecnt)
  228. set_timeout(slideshow, img.ss_delay, 1);
  229. else
  230. img.slideshow = 0;
  231. }
  232. } else {
  233. tns_render(&tns, &win);
  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. int delay;
  260. delay = img_frame_animate(&img, 0);
  261. redraw();
  262. if (delay)
  263. set_timeout(animate, delay, 1);
  264. }
  265. void slideshow() {
  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 = 0;
  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)
  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 && 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)
  300. return;
  301. if (mode == MODE_IMAGE) {
  302. win_set_cursor(&win, CURSOR_ARROW);
  303. set_timeout(reset_cursor, TO_CURSOR_HIDE, 1);
  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 && 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, 1);
  321. load_image(tns.sel);
  322. } else {
  323. tns_highlight(&tns, &win, tns.sel, False);
  324. tns_highlight(&tns, &win, 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() {
  340. int xfd;
  341. fd_set fds;
  342. struct timeval timeout;
  343. XEvent ev;
  344. redraw();
  345. while (1) {
  346. while (mode == MODE_THUMB && tns.cnt < filecnt &&
  347. !XPending(win.env.dpy))
  348. {
  349. /* load thumbnails */
  350. set_timeout(redraw, TO_REDRAW_THUMBS, 0);
  351. if (tns_load(&tns, tns.cnt, &files[tns.cnt], False, False))
  352. tns.cnt++;
  353. else
  354. remove_file(tns.cnt, 0);
  355. if (tns.cnt == filecnt)
  356. redraw();
  357. else
  358. check_timeouts(NULL);
  359. }
  360. while (!XPending(win.env.dpy) && 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. if (!XNextEvent(win.env.dpy, &ev)) {
  368. /* handle events */
  369. switch (ev.type) {
  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, 0);
  380. if (mode == MODE_IMAGE)
  381. img.checkpan = 1;
  382. else
  383. tns.dirty = 1;
  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, 1);
  393. }
  394. break;
  395. }
  396. }
  397. }
  398. }
  399. int fncmp(const void *a, const void *b) {
  400. return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
  401. }
  402. int main(int argc, char **argv) {
  403. int i, start;
  404. size_t n;
  405. ssize_t len;
  406. char *filename;
  407. struct stat fstats;
  408. r_dir_t dir;
  409. parse_options(argc, argv);
  410. if (options->clean_cache) {
  411. tns_init(&tns, 0);
  412. tns_clean_cache(&tns);
  413. exit(0);
  414. }
  415. if (!options->filecnt) {
  416. print_usage();
  417. exit(1);
  418. }
  419. if (options->recursive || options->from_stdin)
  420. filecnt = FNAME_CNT;
  421. else
  422. filecnt = options->filecnt;
  423. files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t));
  424. fileidx = 0;
  425. /* build file list: */
  426. if (options->from_stdin) {
  427. filename = NULL;
  428. while ((len = get_line(&filename, &n, stdin)) > 0) {
  429. if (filename[len-1] == '\n')
  430. filename[len-1] = '\0';
  431. check_add_file(filename);
  432. }
  433. if (filename)
  434. free(filename);
  435. } else {
  436. for (i = 0; i < options->filecnt; i++) {
  437. filename = options->filenames[i];
  438. if (stat(filename, &fstats) || !S_ISDIR(fstats.st_mode)) {
  439. check_add_file(filename);
  440. } else {
  441. if (!options->recursive) {
  442. warn("ignoring directory: %s", filename);
  443. continue;
  444. }
  445. if (r_opendir(&dir, filename)) {
  446. warn("could not open directory: %s", filename);
  447. continue;
  448. }
  449. start = fileidx;
  450. printf("reading dir: %s\n", filename);
  451. while ((filename = r_readdir(&dir))) {
  452. check_add_file(filename);
  453. free((void*) filename);
  454. }
  455. r_closedir(&dir);
  456. if (fileidx - start > 1)
  457. qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
  458. }
  459. }
  460. }
  461. if (!fileidx) {
  462. fprintf(stderr, "sxiv: no valid image file given, aborting\n");
  463. exit(1);
  464. }
  465. filecnt = fileidx;
  466. fileidx = options->startnum < filecnt ? options->startnum : 0;
  467. win_init(&win);
  468. img_init(&img, &win);
  469. if (options->thumbnails) {
  470. mode = MODE_THUMB;
  471. tns_init(&tns, filecnt);
  472. while (!tns_load(&tns, 0, &files[0], False, False))
  473. remove_file(0, 0);
  474. tns.cnt = 1;
  475. } else {
  476. mode = MODE_IMAGE;
  477. tns.thumbs = NULL;
  478. load_image(fileidx);
  479. }
  480. win_open(&win);
  481. run();
  482. cleanup();
  483. return 0;
  484. }