A Simple X Image Viewer
 
 
 
 
 
 

526 行
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 reset_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, reset_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. win_set_cursor(&win, CURSOR_WATCH);
  165. img_close(&img, 0);
  166. while (!img_load(&img, &files[new])) {
  167. remove_file(new, 0);
  168. if (new >= filecnt)
  169. new = filecnt - 1;
  170. }
  171. fileidx = new;
  172. if (!stat(files[new].path, &fstats))
  173. filesize = fstats.st_size;
  174. else
  175. filesize = 0;
  176. if (img.multi.cnt) {
  177. if (img.multi.animate)
  178. set_timeout(animate, img.multi.frames[img.multi.sel].delay, 1);
  179. else
  180. reset_timeout(animate);
  181. }
  182. }
  183. void update_title() {
  184. int n;
  185. float size;
  186. const char *unit;
  187. if (mode == MODE_THUMB) {
  188. n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] %s",
  189. tns.cnt ? tns.sel + 1 : 0, tns.cnt,
  190. tns.cnt ? files[tns.sel].name : "");
  191. } else {
  192. size = filesize;
  193. size_readable(&size, &unit);
  194. if (img.multi.cnt)
  195. n = snprintf(win_title, TITLE_LEN,
  196. "sxiv: [%d/%d] <%d%%> <%dx%d> (%.2f%s) {%d/%d} %s",
  197. fileidx + 1, filecnt, (int) (img.zoom * 100.0), img.w,
  198. img.h, size, unit, img.multi.sel + 1, img.multi.cnt,
  199. files[fileidx].name);
  200. else
  201. n = snprintf(win_title, TITLE_LEN,
  202. "sxiv: [%d/%d] <%d%%> <%dx%d> (%.2f%s) %s",
  203. fileidx + 1, filecnt, (int) (img.zoom * 100.0), img.w,
  204. img.h, size, unit, files[fileidx].name);
  205. }
  206. if (n >= TITLE_LEN) {
  207. for (n = 0; n < 3; n++)
  208. win_title[TITLE_LEN - n - 2] = '.';
  209. }
  210. win_set_title(&win, win_title);
  211. }
  212. void redraw() {
  213. if (mode == MODE_IMAGE)
  214. img_render(&img, &win);
  215. else
  216. tns_render(&tns, &win);
  217. update_title();
  218. reset_timeout(redraw);
  219. reset_cursor();
  220. }
  221. void reset_cursor() {
  222. int i;
  223. cursor_t cursor = CURSOR_NONE;
  224. if (mode == MODE_IMAGE) {
  225. for (i = 0; i < LEN(timeouts); i++) {
  226. if (timeouts[i].handler == reset_cursor) {
  227. if (timeouts[i].active)
  228. cursor = CURSOR_ARROW;
  229. break;
  230. }
  231. }
  232. } else {
  233. if (tns.cnt != filecnt)
  234. cursor = CURSOR_WATCH;
  235. else
  236. cursor = CURSOR_ARROW;
  237. }
  238. win_set_cursor(&win, cursor);
  239. }
  240. void animate() {
  241. int delay;
  242. delay = img_frame_animate(&img, 0);
  243. if (delay) {
  244. set_timeout(animate, delay, 1);
  245. redraw();
  246. }
  247. }
  248. Bool keymask(const keymap_t *k, unsigned int state) {
  249. return (k->ctrl ? ControlMask : 0) == (state & ControlMask);
  250. }
  251. Bool buttonmask(const button_t *b, unsigned int state) {
  252. return ((b->ctrl ? ControlMask : 0) | (b->shift ? ShiftMask : 0)) ==
  253. (state & (ControlMask | ShiftMask));
  254. }
  255. void on_keypress(XKeyEvent *kev) {
  256. int i;
  257. KeySym ksym;
  258. char key;
  259. if (!kev)
  260. return;
  261. XLookupString(kev, &key, 1, &ksym, NULL);
  262. for (i = 0; i < LEN(keys); i++) {
  263. if (keys[i].ksym == ksym && keymask(&keys[i], kev->state)) {
  264. if (keys[i].cmd && keys[i].cmd(keys[i].arg))
  265. redraw();
  266. return;
  267. }
  268. }
  269. }
  270. void on_buttonpress(XButtonEvent *bev) {
  271. int i, sel;
  272. if (!bev)
  273. return;
  274. if (mode == MODE_IMAGE) {
  275. win_set_cursor(&win, CURSOR_ARROW);
  276. set_timeout(reset_cursor, TO_CURSOR_HIDE, 1);
  277. for (i = 0; i < LEN(buttons); i++) {
  278. if (buttons[i].button == bev->button &&
  279. buttonmask(&buttons[i], bev->state))
  280. {
  281. if (buttons[i].cmd && buttons[i].cmd(buttons[i].arg))
  282. redraw();
  283. return;
  284. }
  285. }
  286. } else {
  287. /* thumbnail mode (hard-coded) */
  288. switch (bev->button) {
  289. case Button1:
  290. if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
  291. if (sel == tns.sel) {
  292. mode = MODE_IMAGE;
  293. set_timeout(reset_cursor, TO_CURSOR_HIDE, 1);
  294. load_image(tns.sel);
  295. redraw();
  296. } else {
  297. tns_highlight(&tns, &win, tns.sel, False);
  298. tns_highlight(&tns, &win, sel, True);
  299. tns.sel = sel;
  300. }
  301. break;
  302. }
  303. break;
  304. case Button4:
  305. case Button5:
  306. if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN))
  307. redraw();
  308. break;
  309. }
  310. }
  311. }
  312. void run() {
  313. int xfd;
  314. fd_set fds;
  315. struct timeval timeout;
  316. XEvent ev;
  317. redraw();
  318. while (1) {
  319. while (mode == MODE_THUMB && tns.cnt < filecnt &&
  320. !XPending(win.env.dpy))
  321. {
  322. /* load thumbnails */
  323. set_timeout(redraw, TO_REDRAW_THUMBS, 0);
  324. if (tns_load(&tns, tns.cnt, &files[tns.cnt], False, False))
  325. tns.cnt++;
  326. else
  327. remove_file(tns.cnt, 0);
  328. if (tns.cnt == filecnt)
  329. redraw();
  330. else
  331. check_timeouts(NULL);
  332. }
  333. while (!XPending(win.env.dpy) && check_timeouts(&timeout)) {
  334. /* wait for timeouts */
  335. xfd = ConnectionNumber(win.env.dpy);
  336. FD_ZERO(&fds);
  337. FD_SET(xfd, &fds);
  338. select(xfd + 1, &fds, 0, 0, &timeout);
  339. }
  340. if (!XNextEvent(win.env.dpy, &ev)) {
  341. /* handle events */
  342. switch (ev.type) {
  343. case ButtonPress:
  344. on_buttonpress(&ev.xbutton);
  345. break;
  346. case ClientMessage:
  347. if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
  348. return;
  349. break;
  350. case ConfigureNotify:
  351. if (win_configure(&win, &ev.xconfigure)) {
  352. set_timeout(redraw, TO_REDRAW_RESIZE, 0);
  353. if (mode == MODE_IMAGE)
  354. img.checkpan = 1;
  355. else
  356. tns.dirty = 1;
  357. }
  358. break;
  359. case KeyPress:
  360. on_keypress(&ev.xkey);
  361. break;
  362. case MotionNotify:
  363. if (mode == MODE_IMAGE) {
  364. win_set_cursor(&win, CURSOR_ARROW);
  365. set_timeout(reset_cursor, TO_CURSOR_HIDE, 1);
  366. }
  367. break;
  368. }
  369. }
  370. }
  371. }
  372. int fncmp(const void *a, const void *b) {
  373. return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
  374. }
  375. int main(int argc, char **argv) {
  376. int i, len, start;
  377. size_t n;
  378. char *filename;
  379. struct stat fstats;
  380. r_dir_t dir;
  381. parse_options(argc, argv);
  382. if (options->clean_cache) {
  383. tns_init(&tns, 0);
  384. tns_clean_cache(&tns);
  385. exit(0);
  386. }
  387. if (!options->filecnt) {
  388. print_usage();
  389. exit(1);
  390. }
  391. if (options->recursive || options->from_stdin)
  392. filecnt = FNAME_CNT;
  393. else
  394. filecnt = options->filecnt;
  395. files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t));
  396. fileidx = 0;
  397. /* build file list: */
  398. if (options->from_stdin) {
  399. filename = NULL;
  400. while ((len = getline(&filename, &n, stdin)) > 0) {
  401. if (filename[len-1] == '\n')
  402. filename[len-1] = '\0';
  403. check_add_file(filename);
  404. }
  405. } else {
  406. for (i = 0; i < options->filecnt; i++) {
  407. filename = options->filenames[i];
  408. if (stat(filename, &fstats) || !S_ISDIR(fstats.st_mode)) {
  409. check_add_file(filename);
  410. } else {
  411. if (!options->recursive) {
  412. warn("ignoring directory: %s", filename);
  413. continue;
  414. }
  415. if (r_opendir(&dir, filename)) {
  416. warn("could not open directory: %s", filename);
  417. continue;
  418. }
  419. start = fileidx;
  420. printf("reading dir: %s\n", filename);
  421. while ((filename = r_readdir(&dir))) {
  422. check_add_file(filename);
  423. free((void*) filename);
  424. }
  425. r_closedir(&dir);
  426. if (fileidx - start > 1)
  427. qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
  428. }
  429. }
  430. }
  431. if (!fileidx) {
  432. fprintf(stderr, "sxiv: no valid image file given, aborting\n");
  433. exit(1);
  434. }
  435. filecnt = fileidx;
  436. fileidx = options->startnum < filecnt ? options->startnum : 0;
  437. win_init(&win);
  438. img_init(&img, &win);
  439. if (options->thumbnails) {
  440. mode = MODE_THUMB;
  441. tns_init(&tns, filecnt);
  442. while (!tns_load(&tns, 0, &files[0], False, False))
  443. remove_file(0, 0);
  444. tns.cnt = 1;
  445. } else {
  446. mode = MODE_IMAGE;
  447. tns.thumbs = NULL;
  448. load_image(fileidx);
  449. }
  450. win_open(&win);
  451. run();
  452. cleanup();
  453. return 0;
  454. }