A Simple X Image Viewer
 
 
 
 
 
 

651 lines
14 KiB

  1. /* sxiv: main.c
  2. * Copyright (c) 2012 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/select.h>
  25. #include <sys/stat.h>
  26. #include <sys/time.h>
  27. #include <X11/keysym.h>
  28. #include "types.h"
  29. #include "commands.h"
  30. #include "image.h"
  31. #include "options.h"
  32. #include "thumbs.h"
  33. #include "util.h"
  34. #include "window.h"
  35. #include "config.h"
  36. enum {
  37. BAR_L_LEN = 512,
  38. BAR_R_LEN = 64,
  39. FILENAME_CNT = 1024,
  40. TITLE_LEN = 256
  41. };
  42. typedef struct {
  43. struct timeval when;
  44. bool active;
  45. timeout_f handler;
  46. } timeout_t;
  47. /* timeout handler functions: */
  48. void redraw(void);
  49. void reset_cursor(void);
  50. void animate(void);
  51. void clear_resize(void);
  52. appmode_t mode;
  53. img_t img;
  54. tns_t tns;
  55. win_t win;
  56. fileinfo_t *files;
  57. int filecnt, fileidx;
  58. int alternate;
  59. int prefix;
  60. bool resized = false;
  61. const char * const INFO_SCRIPT = ".sxiv/exec/image-info";
  62. char *info_script;
  63. struct {
  64. char l[BAR_L_LEN];
  65. char r[BAR_R_LEN];
  66. } bar;
  67. timeout_t timeouts[] = {
  68. { { 0, 0 }, false, redraw },
  69. { { 0, 0 }, false, reset_cursor },
  70. { { 0, 0 }, false, animate },
  71. { { 0, 0 }, false, clear_resize },
  72. };
  73. void cleanup(void) {
  74. static bool in = false;
  75. if (!in) {
  76. in = true;
  77. img_close(&img, false);
  78. tns_free(&tns);
  79. win_close(&win);
  80. }
  81. }
  82. void check_add_file(char *filename) {
  83. const char *bn;
  84. if (filename == NULL || *filename == '\0')
  85. return;
  86. if (access(filename, R_OK) < 0) {
  87. warn("could not open file: %s", filename);
  88. return;
  89. }
  90. if (fileidx == filecnt) {
  91. filecnt *= 2;
  92. files = (fileinfo_t*) s_realloc(files, filecnt * sizeof(fileinfo_t));
  93. }
  94. if (*filename != '/') {
  95. files[fileidx].path = absolute_path(filename);
  96. if (files[fileidx].path == NULL) {
  97. warn("could not get absolute path of file: %s\n", filename);
  98. return;
  99. }
  100. }
  101. files[fileidx].loaded = false;
  102. files[fileidx].name = s_strdup(filename);
  103. if (*filename == '/')
  104. files[fileidx].path = files[fileidx].name;
  105. if ((bn = strrchr(files[fileidx].name , '/')) != NULL && bn[1] != '\0')
  106. files[fileidx].base = ++bn;
  107. else
  108. files[fileidx].base = files[fileidx].name;
  109. fileidx++;
  110. }
  111. void remove_file(int n, bool manual) {
  112. if (n < 0 || n >= filecnt)
  113. return;
  114. if (filecnt == 1) {
  115. if (!manual)
  116. fprintf(stderr, "sxiv: no more files to display, aborting\n");
  117. cleanup();
  118. exit(manual ? EXIT_SUCCESS : EXIT_FAILURE);
  119. }
  120. if (files[n].path != files[n].name)
  121. free((void*) files[n].path);
  122. free((void*) files[n].name);
  123. if (n + 1 < filecnt)
  124. memmove(files + n, files + n + 1, (filecnt - n - 1) * sizeof(fileinfo_t));
  125. if (n + 1 < tns.cnt) {
  126. memmove(tns.thumbs + n, tns.thumbs + n + 1, (tns.cnt - n - 1) *
  127. sizeof(thumb_t));
  128. memset(tns.thumbs + tns.cnt - 1, 0, sizeof(thumb_t));
  129. }
  130. filecnt--;
  131. if (n < tns.cnt)
  132. tns.cnt--;
  133. }
  134. void set_timeout(timeout_f handler, int time, bool overwrite) {
  135. int i;
  136. for (i = 0; i < ARRLEN(timeouts); i++) {
  137. if (timeouts[i].handler == handler) {
  138. if (!timeouts[i].active || overwrite) {
  139. gettimeofday(&timeouts[i].when, 0);
  140. TV_ADD_MSEC(&timeouts[i].when, time);
  141. timeouts[i].active = true;
  142. }
  143. return;
  144. }
  145. }
  146. }
  147. void reset_timeout(timeout_f handler) {
  148. int i;
  149. for (i = 0; i < ARRLEN(timeouts); i++) {
  150. if (timeouts[i].handler == handler) {
  151. timeouts[i].active = false;
  152. return;
  153. }
  154. }
  155. }
  156. bool check_timeouts(struct timeval *t) {
  157. int i = 0, tdiff, tmin = -1;
  158. struct timeval now;
  159. while (i < ARRLEN(timeouts)) {
  160. if (timeouts[i].active) {
  161. gettimeofday(&now, 0);
  162. tdiff = TV_DIFF(&timeouts[i].when, &now);
  163. if (tdiff <= 0) {
  164. timeouts[i].active = false;
  165. if (timeouts[i].handler != NULL)
  166. timeouts[i].handler();
  167. i = tmin = -1;
  168. } else if (tmin < 0 || tdiff < tmin) {
  169. tmin = tdiff;
  170. }
  171. }
  172. i++;
  173. }
  174. if (tmin > 0 && t != NULL)
  175. TV_SET_MSEC(t, tmin);
  176. return tmin > 0;
  177. }
  178. void read_info(void) {
  179. char cmd[4096];
  180. FILE *outp;
  181. int c, i = 0, n = sizeof(bar.l) - 1;
  182. bool lastsep = false;
  183. if (info_script != NULL) {
  184. snprintf(cmd, sizeof(cmd), "%s \"%s\"", info_script, files[fileidx].name);
  185. outp = popen(cmd, "r");
  186. if (outp == NULL)
  187. goto end;
  188. while (i < n && (c = fgetc(outp)) != EOF) {
  189. if (c == '\n') {
  190. if (!lastsep) {
  191. bar.l[i++] = ' ';
  192. lastsep = true;
  193. }
  194. } else {
  195. bar.l[i++] = c;
  196. lastsep = false;
  197. }
  198. }
  199. pclose(outp);
  200. }
  201. end:
  202. if (lastsep)
  203. i--;
  204. bar.l[i] = '\0';
  205. }
  206. void load_image(int new) {
  207. if (new < 0 || new >= filecnt)
  208. return;
  209. win_set_cursor(&win, CURSOR_WATCH);
  210. img_close(&img, false);
  211. while (!img_load(&img, &files[new])) {
  212. remove_file(new, false);
  213. if (new >= filecnt)
  214. new = filecnt - 1;
  215. }
  216. files[new].loaded = true;
  217. alternate = fileidx;
  218. fileidx = new;
  219. read_info();
  220. if (img.multi.cnt > 0 && img.multi.animate)
  221. set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
  222. else
  223. reset_timeout(animate);
  224. }
  225. void update_info(void) {
  226. unsigned int i, fn, fw, n, len = sizeof(bar.r);
  227. int sel;
  228. char *t = bar.r, title[TITLE_LEN];
  229. bool ow_info;
  230. for (fw = 0, i = filecnt; i > 0; fw++, i /= 10);
  231. sel = mode == MODE_IMAGE ? fileidx : tns.sel;
  232. if (mode == MODE_THUMB) {
  233. win_set_title(&win, "sxiv");
  234. if (tns.cnt == filecnt) {
  235. n = snprintf(t, len, "%0*d/%d", fw, sel + 1, filecnt);
  236. ow_info = true;
  237. } else {
  238. snprintf(bar.l, sizeof(bar.l), "Loading... %0*d/%d",
  239. fw, tns.cnt, filecnt);
  240. bar.r[0] = '\0';
  241. ow_info = false;
  242. }
  243. } else {
  244. snprintf(title, sizeof(title), "sxiv - %s", files[sel].name);
  245. win_set_title(&win, title);
  246. n = snprintf(t, len, "%3d%% ", (int) (img.zoom * 100.0));
  247. if (img.multi.cnt > 0) {
  248. for (fn = 0, i = img.multi.cnt; i > 0; fn++, i /= 10);
  249. n += snprintf(t + n, len - n, "(%0*d/%d) ",
  250. fn, img.multi.sel + 1, img.multi.cnt);
  251. }
  252. n += snprintf(t + n, len - n, "%0*d/%d", fw, sel + 1, filecnt);
  253. ow_info = bar.l[0] == '\0';
  254. }
  255. if (ow_info) {
  256. fn = strlen(files[sel].name);
  257. if (fn < sizeof(bar.l) &&
  258. win_textwidth(files[sel].name, fn, true) +
  259. win_textwidth(bar.r, n, true) < win.w)
  260. {
  261. strncpy(bar.l, files[sel].name, sizeof(bar.l));
  262. } else {
  263. strncpy(bar.l, files[sel].base, sizeof(bar.l));
  264. }
  265. }
  266. win_set_bar_info(&win, bar.l, bar.r);
  267. }
  268. void redraw(void) {
  269. if (mode == MODE_IMAGE)
  270. img_render(&img);
  271. else
  272. tns_render(&tns);
  273. update_info();
  274. win_draw(&win);
  275. reset_timeout(redraw);
  276. reset_cursor();
  277. }
  278. void reset_cursor(void) {
  279. int i;
  280. cursor_t cursor = CURSOR_NONE;
  281. if (mode == MODE_IMAGE) {
  282. for (i = 0; i < ARRLEN(timeouts); i++) {
  283. if (timeouts[i].handler == reset_cursor) {
  284. if (timeouts[i].active)
  285. cursor = CURSOR_ARROW;
  286. break;
  287. }
  288. }
  289. } else {
  290. if (tns.cnt != filecnt)
  291. cursor = CURSOR_WATCH;
  292. else
  293. cursor = CURSOR_ARROW;
  294. }
  295. win_set_cursor(&win, cursor);
  296. }
  297. void animate(void) {
  298. if (img_frame_animate(&img, false)) {
  299. redraw();
  300. set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
  301. }
  302. }
  303. void clear_resize(void) {
  304. resized = false;
  305. }
  306. bool keymask(const keymap_t *k, unsigned int state) {
  307. return (k->ctrl ? ControlMask : 0) == (state & ControlMask);
  308. }
  309. bool buttonmask(const button_t *b, unsigned int state) {
  310. return ((b->ctrl ? ControlMask : 0) | (b->shift ? ShiftMask : 0)) ==
  311. (state & (ControlMask | ShiftMask));
  312. }
  313. void on_keypress(XKeyEvent *kev) {
  314. int i;
  315. KeySym ksym;
  316. char key;
  317. if (kev == NULL)
  318. return;
  319. XLookupString(kev, &key, 1, &ksym, NULL);
  320. if ((ksym == XK_Escape || (key >= '0' && key <= '9')) &&
  321. (kev->state & ControlMask) == 0)
  322. {
  323. /* number prefix for commands */
  324. prefix = ksym == XK_Escape ? 0 : prefix * 10 + (int) (key - '0');
  325. return;
  326. }
  327. for (i = 0; i < ARRLEN(keys); i++) {
  328. if (keys[i].ksym == ksym && keymask(&keys[i], kev->state)) {
  329. if (keys[i].cmd != NULL && keys[i].cmd(keys[i].arg))
  330. redraw();
  331. prefix = 0;
  332. return;
  333. }
  334. }
  335. }
  336. void on_buttonpress(XButtonEvent *bev) {
  337. int i, sel;
  338. if (bev == NULL)
  339. return;
  340. if (mode == MODE_IMAGE) {
  341. win_set_cursor(&win, CURSOR_ARROW);
  342. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  343. for (i = 0; i < ARRLEN(buttons); i++) {
  344. if (buttons[i].button == bev->button &&
  345. buttonmask(&buttons[i], bev->state))
  346. {
  347. if (buttons[i].cmd != NULL && buttons[i].cmd(buttons[i].arg))
  348. redraw();
  349. return;
  350. }
  351. }
  352. } else {
  353. /* thumbnail mode (hard-coded) */
  354. switch (bev->button) {
  355. case Button1:
  356. if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
  357. if (sel == tns.sel) {
  358. mode = MODE_IMAGE;
  359. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  360. load_image(tns.sel);
  361. } else {
  362. tns_highlight(&tns, tns.sel, false);
  363. tns_highlight(&tns, sel, true);
  364. tns.sel = sel;
  365. }
  366. redraw();
  367. break;
  368. }
  369. break;
  370. case Button4:
  371. case Button5:
  372. if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN,
  373. (bev->state & ControlMask) != 0))
  374. redraw();
  375. break;
  376. }
  377. }
  378. }
  379. void run(void) {
  380. int xfd;
  381. fd_set fds;
  382. struct timeval timeout;
  383. XEvent ev, nextev;
  384. bool discard;
  385. redraw();
  386. while (true) {
  387. while (mode == MODE_THUMB && tns.cnt < filecnt &&
  388. XPending(win.env.dpy) == 0)
  389. {
  390. /* load thumbnails */
  391. set_timeout(redraw, TO_REDRAW_THUMBS, false);
  392. if (tns_load(&tns, tns.cnt, &files[tns.cnt], false, false)) {
  393. tns.cnt++;
  394. } else {
  395. remove_file(tns.cnt, false);
  396. if (tns.sel >= tns.cnt)
  397. tns.sel--;
  398. }
  399. if (tns.cnt == filecnt)
  400. redraw();
  401. else
  402. check_timeouts(NULL);
  403. }
  404. while (XPending(win.env.dpy) == 0 && check_timeouts(&timeout)) {
  405. /* wait for timeouts */
  406. xfd = ConnectionNumber(win.env.dpy);
  407. FD_ZERO(&fds);
  408. FD_SET(xfd, &fds);
  409. select(xfd + 1, &fds, 0, 0, &timeout);
  410. }
  411. do {
  412. XNextEvent(win.env.dpy, &ev);
  413. discard = false;
  414. if (XEventsQueued(win.env.dpy, QueuedAlready) > 0) {
  415. XPeekEvent(win.env.dpy, &nextev);
  416. switch (ev.type) {
  417. case ConfigureNotify:
  418. discard = ev.type == nextev.type;
  419. break;
  420. case KeyPress:
  421. discard = (nextev.type == KeyPress || nextev.type == KeyRelease)
  422. && ev.xkey.keycode == nextev.xkey.keycode;
  423. break;
  424. }
  425. }
  426. } while (discard);
  427. switch (ev.type) {
  428. /* handle events */
  429. case ButtonPress:
  430. on_buttonpress(&ev.xbutton);
  431. break;
  432. case ClientMessage:
  433. if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
  434. return;
  435. break;
  436. case ConfigureNotify:
  437. if (win_configure(&win, &ev.xconfigure)) {
  438. if (mode == MODE_IMAGE) {
  439. img.dirty = true;
  440. img.checkpan = true;
  441. } else {
  442. tns.dirty = true;
  443. }
  444. if (!resized || win.fullscreen) {
  445. redraw();
  446. set_timeout(clear_resize, TO_REDRAW_RESIZE, false);
  447. resized = true;
  448. } else {
  449. set_timeout(redraw, TO_REDRAW_RESIZE, false);
  450. }
  451. }
  452. break;
  453. case Expose:
  454. win_expose(&win, &ev.xexpose);
  455. break;
  456. case KeyPress:
  457. on_keypress(&ev.xkey);
  458. break;
  459. case MotionNotify:
  460. if (mode == MODE_IMAGE) {
  461. win_set_cursor(&win, CURSOR_ARROW);
  462. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  463. }
  464. break;
  465. }
  466. }
  467. }
  468. int fncmp(const void *a, const void *b) {
  469. return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
  470. }
  471. int main(int argc, char **argv) {
  472. int i, start;
  473. size_t n;
  474. ssize_t len;
  475. char *filename;
  476. const char *homedir;
  477. struct stat fstats;
  478. r_dir_t dir;
  479. parse_options(argc, argv);
  480. if (options->clean_cache) {
  481. tns_init(&tns, 0, NULL);
  482. tns_clean_cache(&tns);
  483. exit(EXIT_SUCCESS);
  484. }
  485. if (options->filecnt == 0) {
  486. print_usage();
  487. exit(EXIT_FAILURE);
  488. }
  489. if (options->recursive || options->from_stdin)
  490. filecnt = FILENAME_CNT;
  491. else
  492. filecnt = options->filecnt;
  493. files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t));
  494. fileidx = 0;
  495. /* build file list: */
  496. if (options->from_stdin) {
  497. filename = NULL;
  498. while ((len = get_line(&filename, &n, stdin)) > 0) {
  499. if (filename[len-1] == '\n')
  500. filename[len-1] = '\0';
  501. check_add_file(filename);
  502. }
  503. if (filename != NULL)
  504. free(filename);
  505. } else {
  506. for (i = 0; i < options->filecnt; i++) {
  507. filename = options->filenames[i];
  508. if (stat(filename, &fstats) < 0) {
  509. warn("could not stat file: %s", filename);
  510. continue;
  511. }
  512. if (!S_ISDIR(fstats.st_mode)) {
  513. check_add_file(filename);
  514. } else {
  515. if (!options->recursive) {
  516. warn("ignoring directory: %s", filename);
  517. continue;
  518. }
  519. if (r_opendir(&dir, filename) < 0) {
  520. warn("could not open directory: %s", filename);
  521. continue;
  522. }
  523. start = fileidx;
  524. while ((filename = r_readdir(&dir)) != NULL) {
  525. check_add_file(filename);
  526. free((void*) filename);
  527. }
  528. r_closedir(&dir);
  529. if (fileidx - start > 1)
  530. qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
  531. }
  532. }
  533. }
  534. if (fileidx == 0) {
  535. fprintf(stderr, "sxiv: no valid image file given, aborting\n");
  536. exit(EXIT_FAILURE);
  537. }
  538. filecnt = fileidx;
  539. fileidx = options->startnum < filecnt ? options->startnum : 0;
  540. win_init(&win);
  541. img_init(&img, &win);
  542. if ((homedir = getenv("HOME")) == NULL) {
  543. warn("could not locate home directory");
  544. } else {
  545. len = strlen(homedir) + strlen(INFO_SCRIPT) + 2;
  546. info_script = (char*) s_malloc(len);
  547. snprintf(info_script, len, "%s/%s", homedir, INFO_SCRIPT);
  548. if (access(info_script, X_OK) != 0) {
  549. free(info_script);
  550. info_script = NULL;
  551. }
  552. }
  553. if (options->thumb_mode) {
  554. mode = MODE_THUMB;
  555. tns_init(&tns, filecnt, &win);
  556. while (!tns_load(&tns, 0, &files[0], false, false))
  557. remove_file(0, false);
  558. tns.cnt = 1;
  559. } else {
  560. mode = MODE_IMAGE;
  561. tns.thumbs = NULL;
  562. load_image(fileidx);
  563. }
  564. win_open(&win);
  565. run();
  566. cleanup();
  567. return 0;
  568. }