A Simple X Image Viewer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.c 12 KiB

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