A Simple X Image Viewer
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

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