A Simple X Image Viewer
 
 
 
 
 
 

476 lines
10 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. appmode_t mode;
  40. img_t img;
  41. tns_t tns;
  42. win_t win;
  43. fileinfo_t *files;
  44. int filecnt, fileidx;
  45. size_t filesize;
  46. char win_title[TITLE_LEN];
  47. int timo_cursor;
  48. int timo_redraw;
  49. int timo_adelay; /* multi-frame animation delay time */
  50. void cleanup() {
  51. static int in = 0;
  52. if (!in++) {
  53. img_close(&img, 0);
  54. tns_free(&tns);
  55. win_close(&win);
  56. }
  57. }
  58. void check_add_file(char *filename) {
  59. if (!filename || !*filename)
  60. return;
  61. if (access(filename, R_OK)) {
  62. warn("could not open file: %s", filename);
  63. return;
  64. }
  65. if (fileidx == filecnt) {
  66. filecnt *= 2;
  67. files = (fileinfo_t*) s_realloc(files, filecnt * sizeof(fileinfo_t));
  68. }
  69. if (*filename != '/') {
  70. files[fileidx].path = absolute_path(filename);
  71. if (!files[fileidx].path) {
  72. warn("could not get absolute path of file: %s\n", filename);
  73. return;
  74. }
  75. }
  76. files[fileidx].name = s_strdup(filename);
  77. if (*filename == '/')
  78. files[fileidx].path = files[fileidx].name;
  79. fileidx++;
  80. }
  81. void remove_file(int n, unsigned char silent) {
  82. if (n < 0 || n >= filecnt)
  83. return;
  84. if (filecnt == 1) {
  85. if (!silent)
  86. fprintf(stderr, "sxiv: no more files to display, aborting\n");
  87. cleanup();
  88. exit(!silent);
  89. }
  90. if (n + 1 < filecnt) {
  91. if (files[n].path != files[n].name)
  92. free((void*) files[n].path);
  93. free((void*) files[n].name);
  94. memmove(files + n, files + n + 1, (filecnt - n - 1) * sizeof(fileinfo_t));
  95. }
  96. if (n + 1 < tns.cnt) {
  97. memmove(tns.thumbs + n, tns.thumbs + n + 1, (tns.cnt - n - 1) *
  98. sizeof(thumb_t));
  99. memset(tns.thumbs + tns.cnt - 1, 0, sizeof(thumb_t));
  100. }
  101. filecnt--;
  102. if (n < tns.cnt)
  103. tns.cnt--;
  104. }
  105. void load_image(int new) {
  106. struct stat fstats;
  107. if (new < 0 || new >= filecnt)
  108. return;
  109. /* cursor gets reset in redraw() */
  110. win_set_cursor(&win, CURSOR_WATCH);
  111. img_close(&img, 0);
  112. while (!img_load(&img, &files[new])) {
  113. remove_file(new, 0);
  114. if (new >= filecnt)
  115. new = filecnt - 1;
  116. }
  117. fileidx = new;
  118. if (!stat(files[new].path, &fstats))
  119. filesize = fstats.st_size;
  120. else
  121. filesize = 0;
  122. if (img.multi.cnt) {
  123. if (img.multi.animate)
  124. timo_adelay = img.multi.frames[img.multi.sel].delay;
  125. else
  126. timo_adelay = 0;
  127. }
  128. }
  129. void update_title() {
  130. int n;
  131. float size;
  132. const char *unit;
  133. if (mode == MODE_THUMB) {
  134. n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] %s",
  135. tns.cnt ? tns.sel + 1 : 0, tns.cnt,
  136. tns.cnt ? files[tns.sel].name : "");
  137. } else {
  138. size = filesize;
  139. size_readable(&size, &unit);
  140. if (img.multi.cnt)
  141. n = snprintf(win_title, TITLE_LEN,
  142. "sxiv: [%d/%d] <%d%%> <%dx%d> (%.2f%s) {%d/%d} %s",
  143. fileidx + 1, filecnt, (int) (img.zoom * 100.0), img.w,
  144. img.h, size, unit, img.multi.sel + 1, img.multi.cnt,
  145. files[fileidx].name);
  146. else
  147. n = snprintf(win_title, TITLE_LEN,
  148. "sxiv: [%d/%d] <%d%%> <%dx%d> (%.2f%s) %s",
  149. fileidx + 1, filecnt, (int) (img.zoom * 100.0), img.w,
  150. img.h, size, unit, files[fileidx].name);
  151. }
  152. if (n >= TITLE_LEN) {
  153. for (n = 0; n < 3; n++)
  154. win_title[TITLE_LEN - n - 2] = '.';
  155. }
  156. win_set_title(&win, win_title);
  157. }
  158. void redraw() {
  159. if (mode == MODE_IMAGE) {
  160. img_render(&img, &win);
  161. if (timo_cursor)
  162. win_set_cursor(&win, CURSOR_ARROW);
  163. else
  164. win_set_cursor(&win, CURSOR_NONE);
  165. } else {
  166. tns_render(&tns, &win);
  167. }
  168. update_title();
  169. timo_redraw = 0;
  170. }
  171. Bool keymask(const keymap_t *k, unsigned int state) {
  172. return (k->ctrl ? ControlMask : 0) == (state & ControlMask);
  173. }
  174. Bool buttonmask(const button_t *b, unsigned int state) {
  175. return ((b->ctrl ? ControlMask : 0) | (b->shift ? ShiftMask : 0)) ==
  176. (state & (ControlMask | ShiftMask));
  177. }
  178. void on_keypress(XKeyEvent *kev) {
  179. int i;
  180. KeySym ksym;
  181. char key;
  182. if (!kev)
  183. return;
  184. XLookupString(kev, &key, 1, &ksym, NULL);
  185. for (i = 0; i < LEN(keys); i++) {
  186. if (keys[i].ksym == ksym && keymask(&keys[i], kev->state)) {
  187. if (keys[i].cmd && keys[i].cmd(keys[i].arg))
  188. redraw();
  189. return;
  190. }
  191. }
  192. }
  193. void on_buttonpress(XButtonEvent *bev) {
  194. int i, sel;
  195. if (!bev)
  196. return;
  197. if (mode == MODE_IMAGE) {
  198. win_set_cursor(&win, CURSOR_ARROW);
  199. timo_cursor = TO_CURSOR_HIDE;
  200. for (i = 0; i < LEN(buttons); i++) {
  201. if (buttons[i].button == bev->button &&
  202. buttonmask(&buttons[i], bev->state))
  203. {
  204. if (buttons[i].cmd && buttons[i].cmd(buttons[i].arg))
  205. redraw();
  206. return;
  207. }
  208. }
  209. } else {
  210. /* thumbnail mode (hard-coded) */
  211. switch (bev->button) {
  212. case Button1:
  213. if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
  214. if (sel == tns.sel) {
  215. load_image(tns.sel);
  216. mode = MODE_IMAGE;
  217. timo_cursor = TO_CURSOR_HIDE;
  218. } else {
  219. tns_highlight(&tns, &win, tns.sel, False);
  220. tns_highlight(&tns, &win, sel, True);
  221. tns.sel = sel;
  222. }
  223. redraw();
  224. break;
  225. }
  226. break;
  227. case Button4:
  228. case Button5:
  229. if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN))
  230. redraw();
  231. break;
  232. }
  233. }
  234. }
  235. void run() {
  236. int xfd, timeout;
  237. fd_set fds;
  238. struct timeval tt, t0, t1;
  239. XEvent ev;
  240. timo_cursor = mode == MODE_IMAGE ? TO_CURSOR_HIDE : 0;
  241. redraw();
  242. while (1) {
  243. if (mode == MODE_THUMB && tns.cnt < filecnt) {
  244. /* load thumbnails */
  245. win_set_cursor(&win, CURSOR_WATCH);
  246. gettimeofday(&t0, 0);
  247. while (tns.cnt < filecnt && !XPending(win.env.dpy)) {
  248. if (tns_load(&tns, tns.cnt, &files[tns.cnt], False, False))
  249. tns.cnt++;
  250. else
  251. remove_file(tns.cnt, 0);
  252. gettimeofday(&t1, 0);
  253. if (TIMEDIFF(&t1, &t0) >= TO_THUMBS_LOAD)
  254. break;
  255. }
  256. if (tns.cnt == filecnt)
  257. win_set_cursor(&win, CURSOR_ARROW);
  258. if (!XPending(win.env.dpy)) {
  259. redraw();
  260. continue;
  261. } else {
  262. timo_redraw = TO_THUMBS_LOAD;
  263. }
  264. }
  265. if (timo_cursor || timo_redraw || timo_adelay) {
  266. /* check active timeouts */
  267. gettimeofday(&t0, 0);
  268. timeout = min_int_nz(3, timo_cursor, timo_redraw, timo_adelay);
  269. MSEC_TO_TIMEVAL(timeout, &tt);
  270. xfd = ConnectionNumber(win.env.dpy);
  271. FD_ZERO(&fds);
  272. FD_SET(xfd, &fds);
  273. if (!XPending(win.env.dpy))
  274. select(xfd + 1, &fds, 0, 0, &tt);
  275. gettimeofday(&t1, 0);
  276. timeout = MIN(TIMEDIFF(&t1, &t0), timeout);
  277. /* timeouts fired? */
  278. if (timo_cursor) {
  279. timo_cursor = MAX(0, timo_cursor - timeout);
  280. if (!timo_cursor)
  281. win_set_cursor(&win, CURSOR_NONE);
  282. }
  283. if (timo_redraw) {
  284. timo_redraw = MAX(0, timo_redraw - timeout);
  285. if (!timo_redraw)
  286. redraw();
  287. }
  288. if (timo_adelay) {
  289. timo_adelay = MAX(0, timo_adelay - timeout);
  290. if (!timo_adelay) {
  291. if ((timo_adelay = img_frame_animate(&img, 0)))
  292. redraw();
  293. }
  294. }
  295. if ((timo_cursor || timo_redraw || timo_adelay) &&
  296. !XPending(win.env.dpy))
  297. continue;
  298. }
  299. if (!XNextEvent(win.env.dpy, &ev)) {
  300. /* handle events */
  301. switch (ev.type) {
  302. case ButtonPress:
  303. on_buttonpress(&ev.xbutton);
  304. break;
  305. case ClientMessage:
  306. if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
  307. return;
  308. break;
  309. case ConfigureNotify:
  310. if (win_configure(&win, &ev.xconfigure)) {
  311. timo_redraw = TO_WIN_RESIZE;
  312. if (mode == MODE_IMAGE)
  313. img.checkpan = 1;
  314. else
  315. tns.dirty = 1;
  316. }
  317. break;
  318. case KeyPress:
  319. on_keypress(&ev.xkey);
  320. break;
  321. case MotionNotify:
  322. if (!timo_cursor)
  323. win_set_cursor(&win, CURSOR_ARROW);
  324. timo_cursor = TO_CURSOR_HIDE;
  325. break;
  326. }
  327. }
  328. }
  329. }
  330. int fncmp(const void *a, const void *b) {
  331. return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
  332. }
  333. int main(int argc, char **argv) {
  334. int i, len, start;
  335. size_t n;
  336. char *filename;
  337. struct stat fstats;
  338. r_dir_t dir;
  339. parse_options(argc, argv);
  340. if (options->clean_cache) {
  341. tns_init(&tns, 0);
  342. tns_clean_cache(&tns);
  343. exit(0);
  344. }
  345. if (!options->filecnt) {
  346. print_usage();
  347. exit(1);
  348. }
  349. if (options->recursive || options->from_stdin)
  350. filecnt = FNAME_CNT;
  351. else
  352. filecnt = options->filecnt;
  353. files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t));
  354. fileidx = 0;
  355. /* build file list: */
  356. if (options->from_stdin) {
  357. filename = NULL;
  358. while ((len = getline(&filename, &n, stdin)) > 0) {
  359. if (filename[len-1] == '\n')
  360. filename[len-1] = '\0';
  361. check_add_file(filename);
  362. }
  363. } else {
  364. for (i = 0; i < options->filecnt; i++) {
  365. filename = options->filenames[i];
  366. if (stat(filename, &fstats) || !S_ISDIR(fstats.st_mode)) {
  367. check_add_file(filename);
  368. } else {
  369. if (!options->recursive) {
  370. warn("ignoring directory: %s", filename);
  371. continue;
  372. }
  373. if (r_opendir(&dir, filename)) {
  374. warn("could not open directory: %s", filename);
  375. continue;
  376. }
  377. start = fileidx;
  378. printf("reading dir: %s\n", filename);
  379. while ((filename = r_readdir(&dir))) {
  380. check_add_file(filename);
  381. free((void*) filename);
  382. }
  383. r_closedir(&dir);
  384. if (fileidx - start > 1)
  385. qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
  386. }
  387. }
  388. }
  389. if (!fileidx) {
  390. fprintf(stderr, "sxiv: no valid image file given, aborting\n");
  391. exit(1);
  392. }
  393. filecnt = fileidx;
  394. fileidx = options->startnum < filecnt ? options->startnum : 0;
  395. win_init(&win);
  396. img_init(&img, &win);
  397. if (options->thumbnails) {
  398. mode = MODE_THUMB;
  399. tns_init(&tns, filecnt);
  400. while (!tns_load(&tns, 0, &files[0], False, False))
  401. remove_file(0, 0);
  402. tns.cnt = 1;
  403. } else {
  404. mode = MODE_IMAGE;
  405. tns.thumbs = NULL;
  406. load_image(fileidx);
  407. }
  408. win_open(&win);
  409. run();
  410. cleanup();
  411. return 0;
  412. }