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.
 
 
 
 
 
 

671 lines
14 KiB

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