A Simple X Image Viewer
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

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