A Simple X Image Viewer
 
 
 
 
 
 

531 lines
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 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. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include <sys/stat.h>
  24. #include <sys/time.h>
  25. #include <X11/Xutil.h>
  26. #include <X11/keysym.h>
  27. #include "commands.h"
  28. #include "image.h"
  29. #include "options.h"
  30. #include "thumbs.h"
  31. #include "types.h"
  32. #include "util.h"
  33. #include "window.h"
  34. #define _MAPPINGS_CONFIG
  35. #include "config.h"
  36. enum {
  37. TITLE_LEN = 256,
  38. FNAME_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();
  47. void reset_cursor();
  48. void animate();
  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. char win_title[TITLE_LEN];
  57. timeout_t timeouts[] = {
  58. { { 0, 0 }, False, redraw },
  59. { { 0, 0 }, False, reset_cursor },
  60. { { 0, 0 }, False, animate }
  61. };
  62. void cleanup() {
  63. static int in = 0;
  64. if (!in++) {
  65. img_close(&img, 0);
  66. tns_free(&tns);
  67. win_close(&win);
  68. }
  69. }
  70. void check_add_file(char *filename) {
  71. if (!filename || !*filename)
  72. return;
  73. if (access(filename, R_OK)) {
  74. warn("could not open file: %s", filename);
  75. return;
  76. }
  77. if (fileidx == filecnt) {
  78. filecnt *= 2;
  79. files = (fileinfo_t*) s_realloc(files, filecnt * sizeof(fileinfo_t));
  80. }
  81. if (*filename != '/') {
  82. files[fileidx].path = absolute_path(filename);
  83. if (!files[fileidx].path) {
  84. warn("could not get absolute path of file: %s\n", filename);
  85. return;
  86. }
  87. }
  88. files[fileidx].loaded = 0;
  89. files[fileidx].name = s_strdup(filename);
  90. if (*filename == '/')
  91. files[fileidx].path = files[fileidx].name;
  92. fileidx++;
  93. }
  94. void remove_file(int n, unsigned char silent) {
  95. if (n < 0 || n >= filecnt)
  96. return;
  97. if (filecnt == 1) {
  98. if (!silent)
  99. fprintf(stderr, "sxiv: no more files to display, aborting\n");
  100. cleanup();
  101. exit(!silent);
  102. }
  103. if (files[n].path != files[n].name)
  104. free((void*) files[n].path);
  105. free((void*) files[n].name);
  106. if (n + 1 < filecnt)
  107. memmove(files + n, files + n + 1, (filecnt - n - 1) * sizeof(fileinfo_t));
  108. if (n + 1 < tns.cnt) {
  109. memmove(tns.thumbs + n, tns.thumbs + n + 1, (tns.cnt - n - 1) *
  110. sizeof(thumb_t));
  111. memset(tns.thumbs + tns.cnt - 1, 0, sizeof(thumb_t));
  112. }
  113. filecnt--;
  114. if (n < tns.cnt)
  115. tns.cnt--;
  116. }
  117. void set_timeout(timeout_f handler, int time, int overwrite) {
  118. int i;
  119. for (i = 0; i < ARRLEN(timeouts); i++) {
  120. if (timeouts[i].handler == handler) {
  121. if (!timeouts[i].active || overwrite) {
  122. gettimeofday(&timeouts[i].when, 0);
  123. MSEC_ADD_TO_TIMEVAL(time, &timeouts[i].when);
  124. timeouts[i].active = True;
  125. }
  126. return;
  127. }
  128. }
  129. }
  130. void reset_timeout(timeout_f handler) {
  131. int i;
  132. for (i = 0; i < ARRLEN(timeouts); i++) {
  133. if (timeouts[i].handler == handler) {
  134. timeouts[i].active = False;
  135. return;
  136. }
  137. }
  138. }
  139. int check_timeouts(struct timeval *t) {
  140. int i = 0, tdiff, tmin = -1;
  141. struct timeval now;
  142. gettimeofday(&now, 0);
  143. while (i < ARRLEN(timeouts)) {
  144. if (timeouts[i].active) {
  145. tdiff = TIMEDIFF(&timeouts[i].when, &now);
  146. if (tdiff <= 0) {
  147. timeouts[i].active = False;
  148. if (timeouts[i].handler)
  149. timeouts[i].handler();
  150. i = tmin = -1;
  151. } else if (tmin < 0 || tdiff < tmin) {
  152. tmin = tdiff;
  153. }
  154. }
  155. i++;
  156. }
  157. if (tmin > 0 && t)
  158. MSEC_TO_TIMEVAL(tmin, t);
  159. return tmin > 0;
  160. }
  161. void load_image(int new) {
  162. struct stat fstats;
  163. if (new < 0 || new >= filecnt)
  164. return;
  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. files[new].loaded = 1;
  173. fileidx = new;
  174. if (!stat(files[new].path, &fstats))
  175. filesize = fstats.st_size;
  176. else
  177. filesize = 0;
  178. if (img.multi.cnt && img.multi.animate)
  179. set_timeout(animate, img.multi.frames[img.multi.sel].delay, 1);
  180. else
  181. reset_timeout(animate);
  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 < ARRLEN(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 < ARRLEN(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 < ARRLEN(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. } else {
  296. tns_highlight(&tns, &win, tns.sel, False);
  297. tns_highlight(&tns, &win, sel, True);
  298. tns.sel = sel;
  299. }
  300. redraw();
  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, start;
  377. size_t n;
  378. ssize_t len;
  379. char *filename;
  380. struct stat fstats;
  381. r_dir_t dir;
  382. parse_options(argc, argv);
  383. if (options->clean_cache) {
  384. tns_init(&tns, 0);
  385. tns_clean_cache(&tns);
  386. exit(0);
  387. }
  388. if (!options->filecnt) {
  389. print_usage();
  390. exit(1);
  391. }
  392. if (options->recursive || options->from_stdin)
  393. filecnt = FNAME_CNT;
  394. else
  395. filecnt = options->filecnt;
  396. files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t));
  397. fileidx = 0;
  398. /* build file list: */
  399. if (options->from_stdin) {
  400. filename = NULL;
  401. while ((len = get_line(&filename, &n, stdin)) > 0) {
  402. if (filename[len-1] == '\n')
  403. filename[len-1] = '\0';
  404. check_add_file(filename);
  405. }
  406. if (filename)
  407. free(filename);
  408. } else {
  409. for (i = 0; i < options->filecnt; i++) {
  410. filename = options->filenames[i];
  411. if (stat(filename, &fstats) || !S_ISDIR(fstats.st_mode)) {
  412. check_add_file(filename);
  413. } else {
  414. if (!options->recursive) {
  415. warn("ignoring directory: %s", filename);
  416. continue;
  417. }
  418. if (r_opendir(&dir, filename)) {
  419. warn("could not open directory: %s", filename);
  420. continue;
  421. }
  422. start = fileidx;
  423. printf("reading dir: %s\n", filename);
  424. while ((filename = r_readdir(&dir))) {
  425. check_add_file(filename);
  426. free((void*) filename);
  427. }
  428. r_closedir(&dir);
  429. if (fileidx - start > 1)
  430. qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
  431. }
  432. }
  433. }
  434. if (!fileidx) {
  435. fprintf(stderr, "sxiv: no valid image file given, aborting\n");
  436. exit(1);
  437. }
  438. filecnt = fileidx;
  439. fileidx = options->startnum < filecnt ? options->startnum : 0;
  440. win_init(&win);
  441. img_init(&img, &win);
  442. if (options->thumbnails) {
  443. mode = MODE_THUMB;
  444. tns_init(&tns, filecnt);
  445. while (!tns_load(&tns, 0, &files[0], False, False))
  446. remove_file(0, 0);
  447. tns.cnt = 1;
  448. } else {
  449. mode = MODE_IMAGE;
  450. tns.thumbs = NULL;
  451. load_image(fileidx);
  452. }
  453. win_open(&win);
  454. run();
  455. cleanup();
  456. return 0;
  457. }