A Simple X Image Viewer
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

593 строки
13 KiB

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