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.
 
 
 
 
 
 

451 lines
9.7 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
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301, USA.
  17. */
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include <sys/stat.h>
  23. #include <sys/time.h>
  24. #include <X11/Xutil.h>
  25. #include <X11/keysym.h>
  26. #include "commands.h"
  27. #include "image.h"
  28. #include "options.h"
  29. #include "thumbs.h"
  30. #include "types.h"
  31. #include "util.h"
  32. #include "window.h"
  33. #define _MAPPINGS_CONFIG
  34. #include "config.h"
  35. enum {
  36. TITLE_LEN = 256,
  37. FNAME_CNT = 1024
  38. };
  39. appmode_t mode;
  40. img_t img;
  41. tns_t tns;
  42. win_t win;
  43. fileinfo_t *files;
  44. int filecnt, fileidx;
  45. size_t filesize;
  46. char win_title[TITLE_LEN];
  47. int timo_cursor;
  48. int timo_redraw;
  49. void cleanup() {
  50. static int in = 0;
  51. if (!in++) {
  52. img_close(&img, 0);
  53. tns_free(&tns);
  54. win_close(&win);
  55. }
  56. }
  57. void check_add_file(char *filename) {
  58. if (!filename || !*filename)
  59. return;
  60. if (access(filename, R_OK)) {
  61. warn("could not open file: %s", filename);
  62. return;
  63. }
  64. if (fileidx == filecnt) {
  65. filecnt *= 2;
  66. files = (fileinfo_t*) s_realloc(files, filecnt * sizeof(fileinfo_t));
  67. }
  68. if (*filename != '/') {
  69. files[fileidx].path = absolute_path(filename);
  70. if (!files[fileidx].path) {
  71. warn("could not get absolute path of file: %s\n", filename);
  72. return;
  73. }
  74. }
  75. files[fileidx].name = s_strdup(filename);
  76. if (*filename == '/')
  77. files[fileidx].path = files[fileidx].name;
  78. fileidx++;
  79. }
  80. void remove_file(int n, unsigned char silent) {
  81. if (n < 0 || n >= filecnt)
  82. return;
  83. if (filecnt == 1) {
  84. if (!silent)
  85. fprintf(stderr, "sxiv: no more files to display, aborting\n");
  86. cleanup();
  87. exit(!silent);
  88. }
  89. if (n + 1 < filecnt) {
  90. if (files[n].path != files[n].name)
  91. free((void*) files[n].path);
  92. free((void*) files[n].name);
  93. memmove(files + n, files + n + 1, (filecnt - n - 1) * sizeof(fileinfo_t));
  94. }
  95. if (n + 1 < tns.cnt) {
  96. memmove(tns.thumbs + n, tns.thumbs + n + 1, (tns.cnt - n - 1) *
  97. sizeof(thumb_t));
  98. memset(tns.thumbs + tns.cnt - 1, 0, sizeof(thumb_t));
  99. }
  100. filecnt--;
  101. if (n < tns.cnt)
  102. tns.cnt--;
  103. }
  104. void load_image(int new) {
  105. struct stat fstats;
  106. if (new < 0 || new >= filecnt)
  107. return;
  108. /* cursor gets reset in redraw() */
  109. win_set_cursor(&win, CURSOR_WATCH);
  110. img_close(&img, 0);
  111. while (!img_load(&img, &files[new])) {
  112. remove_file(new, 0);
  113. if (new >= filecnt)
  114. new = filecnt - 1;
  115. }
  116. fileidx = new;
  117. if (!stat(files[new].path, &fstats))
  118. filesize = fstats.st_size;
  119. else
  120. filesize = 0;
  121. }
  122. void update_title() {
  123. int n;
  124. float size;
  125. const char *unit;
  126. if (mode == MODE_THUMB) {
  127. n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] %s",
  128. tns.cnt ? tns.sel + 1 : 0, tns.cnt,
  129. tns.cnt ? files[tns.sel].name : "");
  130. } else {
  131. size = filesize;
  132. size_readable(&size, &unit);
  133. n = snprintf(win_title, TITLE_LEN,
  134. "sxiv: [%d/%d] <%d%%> <%dx%d> (%.2f%s) %s",
  135. fileidx + 1, filecnt, (int) (img.zoom * 100.0), img.w, img.h,
  136. size, unit, files[fileidx].name);
  137. }
  138. if (n >= TITLE_LEN) {
  139. for (n = 0; n < 3; n++)
  140. win_title[TITLE_LEN - n - 2] = '.';
  141. }
  142. win_set_title(&win, win_title);
  143. }
  144. void redraw() {
  145. if (mode == MODE_IMAGE) {
  146. img_render(&img, &win);
  147. if (timo_cursor)
  148. win_set_cursor(&win, CURSOR_ARROW);
  149. else
  150. win_set_cursor(&win, CURSOR_NONE);
  151. } else {
  152. tns_render(&tns, &win);
  153. }
  154. update_title();
  155. timo_redraw = 0;
  156. }
  157. Bool keymask(const keymap_t *k, unsigned int state) {
  158. return (k->ctrl ? ControlMask : 0) == (state & ControlMask);
  159. }
  160. Bool buttonmask(const button_t *b, unsigned int state) {
  161. return ((b->ctrl ? ControlMask : 0) | (b->shift ? ShiftMask : 0)) ==
  162. (state & (ControlMask | ShiftMask));
  163. }
  164. void on_keypress(XKeyEvent *kev) {
  165. int i;
  166. KeySym ksym;
  167. char key;
  168. if (!kev)
  169. return;
  170. XLookupString(kev, &key, 1, &ksym, NULL);
  171. for (i = 0; i < LEN(keys); i++) {
  172. if (keys[i].ksym == ksym && keymask(&keys[i], kev->state)) {
  173. if (keys[i].cmd && keys[i].cmd(keys[i].arg))
  174. redraw();
  175. return;
  176. }
  177. }
  178. }
  179. void on_buttonpress(XButtonEvent *bev) {
  180. int i, sel;
  181. if (!bev)
  182. return;
  183. if (mode == MODE_IMAGE) {
  184. win_set_cursor(&win, CURSOR_ARROW);
  185. timo_cursor = TO_CURSOR_HIDE;
  186. for (i = 0; i < LEN(buttons); i++) {
  187. if (buttons[i].button == bev->button &&
  188. buttonmask(&buttons[i], bev->state))
  189. {
  190. if (buttons[i].cmd && buttons[i].cmd(buttons[i].arg))
  191. redraw();
  192. return;
  193. }
  194. }
  195. } else {
  196. /* thumbnail mode (hard-coded) */
  197. switch (bev->button) {
  198. case Button1:
  199. if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
  200. if (sel == tns.sel) {
  201. load_image(tns.sel);
  202. mode = MODE_IMAGE;
  203. timo_cursor = TO_CURSOR_HIDE;
  204. } else {
  205. tns_highlight(&tns, &win, tns.sel, False);
  206. tns_highlight(&tns, &win, sel, True);
  207. tns.sel = sel;
  208. }
  209. redraw();
  210. break;
  211. }
  212. break;
  213. case Button4:
  214. case Button5:
  215. if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN))
  216. redraw();
  217. break;
  218. }
  219. }
  220. }
  221. void run() {
  222. int xfd, timeout;
  223. fd_set fds;
  224. struct timeval tt, t0, t1;
  225. XEvent ev;
  226. timo_cursor = mode == MODE_IMAGE ? TO_CURSOR_HIDE : 0;
  227. redraw();
  228. while (1) {
  229. if (mode == MODE_THUMB && tns.cnt < filecnt) {
  230. /* load thumbnails */
  231. win_set_cursor(&win, CURSOR_WATCH);
  232. gettimeofday(&t0, 0);
  233. while (tns.cnt < filecnt && !XPending(win.env.dpy)) {
  234. if (tns_load(&tns, tns.cnt, &files[tns.cnt], False, False))
  235. tns.cnt++;
  236. else
  237. remove_file(tns.cnt, 0);
  238. gettimeofday(&t1, 0);
  239. if (TIMEDIFF(&t1, &t0) >= TO_THUMBS_LOAD)
  240. break;
  241. }
  242. if (tns.cnt == filecnt)
  243. win_set_cursor(&win, CURSOR_ARROW);
  244. if (!XPending(win.env.dpy)) {
  245. redraw();
  246. continue;
  247. } else {
  248. timo_redraw = TO_THUMBS_LOAD;
  249. }
  250. } else if (timo_cursor || timo_redraw) {
  251. /* check active timeouts */
  252. gettimeofday(&t0, 0);
  253. timeout = MIN(timo_cursor + 1, timo_redraw + 1);
  254. MSEC_TO_TIMEVAL(timeout, &tt);
  255. xfd = ConnectionNumber(win.env.dpy);
  256. FD_ZERO(&fds);
  257. FD_SET(xfd, &fds);
  258. if (!XPending(win.env.dpy))
  259. select(xfd + 1, &fds, 0, 0, &tt);
  260. gettimeofday(&t1, 0);
  261. timeout = MIN(TIMEDIFF(&t1, &t0), timeout);
  262. /* timeouts fired? */
  263. if (timo_cursor) {
  264. timo_cursor = MAX(0, timo_cursor - timeout);
  265. if (!timo_cursor)
  266. win_set_cursor(&win, CURSOR_NONE);
  267. }
  268. if (timo_redraw) {
  269. timo_redraw = MAX(0, timo_redraw - timeout);
  270. if (!timo_redraw)
  271. redraw();
  272. }
  273. if ((timo_cursor || timo_redraw) && !XPending(win.env.dpy))
  274. continue;
  275. }
  276. if (!XNextEvent(win.env.dpy, &ev)) {
  277. /* handle events */
  278. switch (ev.type) {
  279. case ButtonPress:
  280. on_buttonpress(&ev.xbutton);
  281. break;
  282. case ClientMessage:
  283. if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
  284. return;
  285. break;
  286. case ConfigureNotify:
  287. if (win_configure(&win, &ev.xconfigure)) {
  288. timo_redraw = TO_WIN_RESIZE;
  289. if (mode == MODE_IMAGE)
  290. img.checkpan = 1;
  291. else
  292. tns.dirty = 1;
  293. }
  294. break;
  295. case KeyPress:
  296. on_keypress(&ev.xkey);
  297. break;
  298. case MotionNotify:
  299. if (!timo_cursor)
  300. win_set_cursor(&win, CURSOR_ARROW);
  301. timo_cursor = TO_CURSOR_HIDE;
  302. break;
  303. }
  304. }
  305. }
  306. }
  307. int fncmp(const void *a, const void *b) {
  308. return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
  309. }
  310. int main(int argc, char **argv) {
  311. int i, len, start;
  312. size_t n;
  313. char *filename;
  314. struct stat fstats;
  315. r_dir_t dir;
  316. parse_options(argc, argv);
  317. if (options->clean_cache) {
  318. tns_init(&tns, 0);
  319. tns_clean_cache(&tns);
  320. exit(0);
  321. }
  322. if (!options->filecnt) {
  323. print_usage();
  324. exit(1);
  325. }
  326. if (options->recursive || options->from_stdin)
  327. filecnt = FNAME_CNT;
  328. else
  329. filecnt = options->filecnt;
  330. files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t));
  331. fileidx = 0;
  332. /* build file list: */
  333. if (options->from_stdin) {
  334. filename = NULL;
  335. while ((len = getline(&filename, &n, stdin)) > 0) {
  336. if (filename[len-1] == '\n')
  337. filename[len-1] = '\0';
  338. check_add_file(filename);
  339. }
  340. } else {
  341. for (i = 0; i < options->filecnt; i++) {
  342. filename = options->filenames[i];
  343. if (stat(filename, &fstats) || !S_ISDIR(fstats.st_mode)) {
  344. check_add_file(filename);
  345. } else {
  346. if (!options->recursive) {
  347. warn("ignoring directory: %s", filename);
  348. continue;
  349. }
  350. if (r_opendir(&dir, filename)) {
  351. warn("could not open directory: %s", filename);
  352. continue;
  353. }
  354. start = fileidx;
  355. printf("reading dir: %s\n", filename);
  356. while ((filename = r_readdir(&dir))) {
  357. check_add_file(filename);
  358. free((void*) filename);
  359. }
  360. r_closedir(&dir);
  361. if (fileidx - start > 1)
  362. qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
  363. }
  364. }
  365. }
  366. if (!fileidx) {
  367. fprintf(stderr, "sxiv: no valid image file given, aborting\n");
  368. exit(1);
  369. }
  370. filecnt = fileidx;
  371. fileidx = options->startnum < filecnt ? options->startnum : 0;
  372. win_init(&win);
  373. img_init(&img, &win);
  374. if (options->thumbnails) {
  375. mode = MODE_THUMB;
  376. tns_init(&tns, filecnt);
  377. while (!tns_load(&tns, 0, &files[0], False, False))
  378. remove_file(0, 0);
  379. tns.cnt = 1;
  380. } else {
  381. mode = MODE_IMAGE;
  382. tns.thumbs = NULL;
  383. load_image(fileidx);
  384. }
  385. win_open(&win);
  386. run();
  387. cleanup();
  388. return 0;
  389. }