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

519 行
11 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
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301, USA.
  17. */
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include <sys/stat.h>
  23. #include <sys/time.h>
  24. #include <X11/Xutil.h>
  25. #include <X11/keysym.h>
  26. #include "commands.h"
  27. #include "image.h"
  28. #include "options.h"
  29. #include "thumbs.h"
  30. #include "types.h"
  31. #include "util.h"
  32. #include "window.h"
  33. #define _MAPPINGS_CONFIG
  34. #include "config.h"
  35. enum {
  36. TITLE_LEN = 256,
  37. FNAME_CNT = 1024
  38. };
  39. typedef struct {
  40. struct timeval when;
  41. Bool active;
  42. timeout_f handler;
  43. } timeout_t;
  44. /* timeout handler functions: */
  45. void redraw();
  46. void hide_cursor();
  47. void animate();
  48. appmode_t mode;
  49. img_t img;
  50. tns_t tns;
  51. win_t win;
  52. fileinfo_t *files;
  53. int filecnt, fileidx;
  54. size_t filesize;
  55. char win_title[TITLE_LEN];
  56. timeout_t timeouts[] = {
  57. { { 0, 0 }, False, redraw },
  58. { { 0, 0 }, False, hide_cursor },
  59. { { 0, 0 }, False, animate }
  60. };
  61. void cleanup() {
  62. static int in = 0;
  63. if (!in++) {
  64. img_close(&img, 0);
  65. tns_free(&tns);
  66. win_close(&win);
  67. }
  68. }
  69. void check_add_file(char *filename) {
  70. if (!filename || !*filename)
  71. return;
  72. if (access(filename, R_OK)) {
  73. warn("could not open file: %s", filename);
  74. return;
  75. }
  76. if (fileidx == filecnt) {
  77. filecnt *= 2;
  78. files = (fileinfo_t*) s_realloc(files, filecnt * sizeof(fileinfo_t));
  79. }
  80. if (*filename != '/') {
  81. files[fileidx].path = absolute_path(filename);
  82. if (!files[fileidx].path) {
  83. warn("could not get absolute path of file: %s\n", filename);
  84. return;
  85. }
  86. }
  87. files[fileidx].name = s_strdup(filename);
  88. if (*filename == '/')
  89. files[fileidx].path = files[fileidx].name;
  90. fileidx++;
  91. }
  92. void remove_file(int n, unsigned char silent) {
  93. if (n < 0 || n >= filecnt)
  94. return;
  95. if (filecnt == 1) {
  96. if (!silent)
  97. fprintf(stderr, "sxiv: no more files to display, aborting\n");
  98. cleanup();
  99. exit(!silent);
  100. }
  101. if (n + 1 < filecnt) {
  102. if (files[n].path != files[n].name)
  103. free((void*) files[n].path);
  104. free((void*) files[n].name);
  105. memmove(files + n, files + n + 1, (filecnt - n - 1) * sizeof(fileinfo_t));
  106. }
  107. if (n + 1 < tns.cnt) {
  108. memmove(tns.thumbs + n, tns.thumbs + n + 1, (tns.cnt - n - 1) *
  109. sizeof(thumb_t));
  110. memset(tns.thumbs + tns.cnt - 1, 0, sizeof(thumb_t));
  111. }
  112. filecnt--;
  113. if (n < tns.cnt)
  114. tns.cnt--;
  115. }
  116. void set_timeout(timeout_f handler, int time, int overwrite) {
  117. int i;
  118. for (i = 0; i < LEN(timeouts); i++) {
  119. if (timeouts[i].handler == handler) {
  120. if (!timeouts[i].active || overwrite) {
  121. gettimeofday(&timeouts[i].when, 0);
  122. MSEC_ADD_TO_TIMEVAL(time, &timeouts[i].when);
  123. timeouts[i].active = True;
  124. }
  125. return;
  126. }
  127. }
  128. }
  129. void reset_timeout(timeout_f handler) {
  130. int i;
  131. for (i = 0; i < LEN(timeouts); i++) {
  132. if (timeouts[i].handler == handler) {
  133. timeouts[i].active = False;
  134. return;
  135. }
  136. }
  137. }
  138. int check_timeouts(struct timeval *t) {
  139. int i = 0, tdiff, tmin = -1;
  140. struct timeval now;
  141. gettimeofday(&now, 0);
  142. while (i < LEN(timeouts)) {
  143. if (timeouts[i].active) {
  144. tdiff = TIMEDIFF(&timeouts[i].when, &now);
  145. if (tdiff <= 0) {
  146. timeouts[i].active = False;
  147. if (timeouts[i].handler)
  148. timeouts[i].handler();
  149. i = tmin = -1;
  150. } else if (tmin < 0 || tdiff < tmin) {
  151. tmin = tdiff;
  152. }
  153. }
  154. i++;
  155. }
  156. if (tmin > 0 && t)
  157. MSEC_TO_TIMEVAL(tmin, t);
  158. return tmin > 0;
  159. }
  160. void load_image(int new) {
  161. struct stat fstats;
  162. if (new < 0 || new >= filecnt)
  163. return;
  164. /* cursor gets reset in redraw() */
  165. win_set_cursor(&win, CURSOR_WATCH);
  166. img_close(&img, 0);
  167. while (!img_load(&img, &files[new])) {
  168. remove_file(new, 0);
  169. if (new >= filecnt)
  170. new = filecnt - 1;
  171. }
  172. fileidx = new;
  173. if (!stat(files[new].path, &fstats))
  174. filesize = fstats.st_size;
  175. else
  176. filesize = 0;
  177. if (img.multi.cnt) {
  178. if (img.multi.animate)
  179. set_timeout(animate, img.multi.frames[img.multi.sel].delay, 1);
  180. else
  181. reset_timeout(animate);
  182. }
  183. }
  184. void update_title() {
  185. int n;
  186. float size;
  187. const char *unit;
  188. if (mode == MODE_THUMB) {
  189. n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] %s",
  190. tns.cnt ? tns.sel + 1 : 0, tns.cnt,
  191. tns.cnt ? files[tns.sel].name : "");
  192. } else {
  193. size = filesize;
  194. size_readable(&size, &unit);
  195. if (img.multi.cnt)
  196. n = snprintf(win_title, TITLE_LEN,
  197. "sxiv: [%d/%d] <%d%%> <%dx%d> (%.2f%s) {%d/%d} %s",
  198. fileidx + 1, filecnt, (int) (img.zoom * 100.0), img.w,
  199. img.h, size, unit, img.multi.sel + 1, img.multi.cnt,
  200. files[fileidx].name);
  201. else
  202. n = snprintf(win_title, TITLE_LEN,
  203. "sxiv: [%d/%d] <%d%%> <%dx%d> (%.2f%s) %s",
  204. fileidx + 1, filecnt, (int) (img.zoom * 100.0), img.w,
  205. img.h, size, unit, files[fileidx].name);
  206. }
  207. if (n >= TITLE_LEN) {
  208. for (n = 0; n < 3; n++)
  209. win_title[TITLE_LEN - n - 2] = '.';
  210. }
  211. win_set_title(&win, win_title);
  212. }
  213. void redraw() {
  214. if (mode == MODE_IMAGE) {
  215. img_render(&img, &win);
  216. if (img.multi.animate) {
  217. win_set_cursor(&win, CURSOR_NONE);
  218. } else {
  219. win_set_cursor(&win, CURSOR_ARROW);
  220. set_timeout(hide_cursor, TO_CURSOR_HIDE, 1);
  221. }
  222. } else {
  223. tns_render(&tns, &win);
  224. }
  225. update_title();
  226. reset_timeout(redraw);
  227. }
  228. void hide_cursor() {
  229. win_set_cursor(&win, CURSOR_NONE);
  230. }
  231. void animate() {
  232. int delay;
  233. delay = img_frame_animate(&img, 0);
  234. if (delay) {
  235. set_timeout(animate, delay, 1);
  236. redraw();
  237. }
  238. }
  239. Bool keymask(const keymap_t *k, unsigned int state) {
  240. return (k->ctrl ? ControlMask : 0) == (state & ControlMask);
  241. }
  242. Bool buttonmask(const button_t *b, unsigned int state) {
  243. return ((b->ctrl ? ControlMask : 0) | (b->shift ? ShiftMask : 0)) ==
  244. (state & (ControlMask | ShiftMask));
  245. }
  246. void on_keypress(XKeyEvent *kev) {
  247. int i;
  248. KeySym ksym;
  249. char key;
  250. if (!kev)
  251. return;
  252. XLookupString(kev, &key, 1, &ksym, NULL);
  253. for (i = 0; i < LEN(keys); i++) {
  254. if (keys[i].ksym == ksym && keymask(&keys[i], kev->state)) {
  255. if (keys[i].cmd && keys[i].cmd(keys[i].arg))
  256. redraw();
  257. return;
  258. }
  259. }
  260. }
  261. void on_buttonpress(XButtonEvent *bev) {
  262. int i, sel;
  263. if (!bev)
  264. return;
  265. if (mode == MODE_IMAGE) {
  266. win_set_cursor(&win, CURSOR_ARROW);
  267. set_timeout(hide_cursor, TO_CURSOR_HIDE, 1);
  268. for (i = 0; i < LEN(buttons); i++) {
  269. if (buttons[i].button == bev->button &&
  270. buttonmask(&buttons[i], bev->state))
  271. {
  272. if (buttons[i].cmd && buttons[i].cmd(buttons[i].arg))
  273. redraw();
  274. return;
  275. }
  276. }
  277. } else {
  278. /* thumbnail mode (hard-coded) */
  279. switch (bev->button) {
  280. case Button1:
  281. if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
  282. if (sel == tns.sel) {
  283. load_image(tns.sel);
  284. mode = MODE_IMAGE;
  285. set_timeout(hide_cursor, TO_CURSOR_HIDE, 1);
  286. } else {
  287. tns_highlight(&tns, &win, tns.sel, False);
  288. tns_highlight(&tns, &win, sel, True);
  289. tns.sel = sel;
  290. }
  291. redraw();
  292. break;
  293. }
  294. break;
  295. case Button4:
  296. case Button5:
  297. if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN))
  298. redraw();
  299. break;
  300. }
  301. }
  302. }
  303. void run() {
  304. int xfd;
  305. fd_set fds;
  306. struct timeval timeout;
  307. XEvent ev;
  308. redraw();
  309. while (1) {
  310. while (mode == MODE_THUMB && tns.cnt < filecnt &&
  311. !XPending(win.env.dpy))
  312. {
  313. /* load thumbnails */
  314. win_set_cursor(&win, CURSOR_WATCH);
  315. set_timeout(redraw, TO_REDRAW_THUMBS, 0);
  316. if (tns_load(&tns, tns.cnt, &files[tns.cnt], False, False))
  317. tns.cnt++;
  318. else
  319. remove_file(tns.cnt, 0);
  320. if (tns.cnt == filecnt) {
  321. redraw();
  322. win_set_cursor(&win, CURSOR_ARROW);
  323. } else {
  324. check_timeouts(NULL);
  325. }
  326. }
  327. while (!XPending(win.env.dpy) && check_timeouts(&timeout)) {
  328. /* wait for timeouts */
  329. xfd = ConnectionNumber(win.env.dpy);
  330. FD_ZERO(&fds);
  331. FD_SET(xfd, &fds);
  332. select(xfd + 1, &fds, 0, 0, &timeout);
  333. }
  334. if (!XNextEvent(win.env.dpy, &ev)) {
  335. /* handle events */
  336. switch (ev.type) {
  337. case ButtonPress:
  338. on_buttonpress(&ev.xbutton);
  339. break;
  340. case ClientMessage:
  341. if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
  342. return;
  343. break;
  344. case ConfigureNotify:
  345. if (win_configure(&win, &ev.xconfigure)) {
  346. set_timeout(redraw, TO_REDRAW_RESIZE, 0);
  347. if (mode == MODE_IMAGE)
  348. img.checkpan = 1;
  349. else
  350. tns.dirty = 1;
  351. }
  352. break;
  353. case KeyPress:
  354. on_keypress(&ev.xkey);
  355. break;
  356. case MotionNotify:
  357. if (mode == MODE_IMAGE) {
  358. win_set_cursor(&win, CURSOR_ARROW);
  359. set_timeout(hide_cursor, TO_CURSOR_HIDE, 1);
  360. }
  361. break;
  362. }
  363. }
  364. }
  365. }
  366. int fncmp(const void *a, const void *b) {
  367. return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
  368. }
  369. int main(int argc, char **argv) {
  370. int i, len, start;
  371. size_t n;
  372. char *filename;
  373. struct stat fstats;
  374. r_dir_t dir;
  375. parse_options(argc, argv);
  376. if (options->clean_cache) {
  377. tns_init(&tns, 0);
  378. tns_clean_cache(&tns);
  379. exit(0);
  380. }
  381. if (!options->filecnt) {
  382. print_usage();
  383. exit(1);
  384. }
  385. if (options->recursive || options->from_stdin)
  386. filecnt = FNAME_CNT;
  387. else
  388. filecnt = options->filecnt;
  389. files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t));
  390. fileidx = 0;
  391. /* build file list: */
  392. if (options->from_stdin) {
  393. filename = NULL;
  394. while ((len = getline(&filename, &n, stdin)) > 0) {
  395. if (filename[len-1] == '\n')
  396. filename[len-1] = '\0';
  397. check_add_file(filename);
  398. }
  399. } else {
  400. for (i = 0; i < options->filecnt; i++) {
  401. filename = options->filenames[i];
  402. if (stat(filename, &fstats) || !S_ISDIR(fstats.st_mode)) {
  403. check_add_file(filename);
  404. } else {
  405. if (!options->recursive) {
  406. warn("ignoring directory: %s", filename);
  407. continue;
  408. }
  409. if (r_opendir(&dir, filename)) {
  410. warn("could not open directory: %s", filename);
  411. continue;
  412. }
  413. start = fileidx;
  414. printf("reading dir: %s\n", filename);
  415. while ((filename = r_readdir(&dir))) {
  416. check_add_file(filename);
  417. free((void*) filename);
  418. }
  419. r_closedir(&dir);
  420. if (fileidx - start > 1)
  421. qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
  422. }
  423. }
  424. }
  425. if (!fileidx) {
  426. fprintf(stderr, "sxiv: no valid image file given, aborting\n");
  427. exit(1);
  428. }
  429. filecnt = fileidx;
  430. fileidx = options->startnum < filecnt ? options->startnum : 0;
  431. win_init(&win);
  432. img_init(&img, &win);
  433. if (options->thumbnails) {
  434. mode = MODE_THUMB;
  435. tns_init(&tns, filecnt);
  436. while (!tns_load(&tns, 0, &files[0], False, False))
  437. remove_file(0, 0);
  438. tns.cnt = 1;
  439. } else {
  440. mode = MODE_IMAGE;
  441. tns.thumbs = NULL;
  442. load_image(fileidx);
  443. }
  444. win_open(&win);
  445. run();
  446. cleanup();
  447. return 0;
  448. }