A Simple X Image Viewer
 
 
 
 
 
 

458 lines
10 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. if (img.multi.cnt)
  134. n = snprintf(win_title, TITLE_LEN,
  135. "sxiv: [%d/%d] <%d%%> <%dx%d> (%.2f%s) {%d/%d} %s",
  136. fileidx + 1, filecnt, (int) (img.zoom * 100.0), img.w,
  137. img.h, size, unit, img.multi.cur + 1, img.multi.cnt,
  138. files[fileidx].name);
  139. else
  140. n = snprintf(win_title, TITLE_LEN,
  141. "sxiv: [%d/%d] <%d%%> <%dx%d> (%.2f%s) %s",
  142. fileidx + 1, filecnt, (int) (img.zoom * 100.0), img.w,
  143. img.h, size, unit, files[fileidx].name);
  144. }
  145. if (n >= TITLE_LEN) {
  146. for (n = 0; n < 3; n++)
  147. win_title[TITLE_LEN - n - 2] = '.';
  148. }
  149. win_set_title(&win, win_title);
  150. }
  151. void redraw() {
  152. if (mode == MODE_IMAGE) {
  153. img_render(&img, &win);
  154. if (timo_cursor)
  155. win_set_cursor(&win, CURSOR_ARROW);
  156. else
  157. win_set_cursor(&win, CURSOR_NONE);
  158. } else {
  159. tns_render(&tns, &win);
  160. }
  161. update_title();
  162. timo_redraw = 0;
  163. }
  164. Bool keymask(const keymap_t *k, unsigned int state) {
  165. return (k->ctrl ? ControlMask : 0) == (state & ControlMask);
  166. }
  167. Bool buttonmask(const button_t *b, unsigned int state) {
  168. return ((b->ctrl ? ControlMask : 0) | (b->shift ? ShiftMask : 0)) ==
  169. (state & (ControlMask | ShiftMask));
  170. }
  171. void on_keypress(XKeyEvent *kev) {
  172. int i;
  173. KeySym ksym;
  174. char key;
  175. if (!kev)
  176. return;
  177. XLookupString(kev, &key, 1, &ksym, NULL);
  178. for (i = 0; i < LEN(keys); i++) {
  179. if (keys[i].ksym == ksym && keymask(&keys[i], kev->state)) {
  180. if (keys[i].cmd && keys[i].cmd(keys[i].arg))
  181. redraw();
  182. return;
  183. }
  184. }
  185. }
  186. void on_buttonpress(XButtonEvent *bev) {
  187. int i, sel;
  188. if (!bev)
  189. return;
  190. if (mode == MODE_IMAGE) {
  191. win_set_cursor(&win, CURSOR_ARROW);
  192. timo_cursor = TO_CURSOR_HIDE;
  193. for (i = 0; i < LEN(buttons); i++) {
  194. if (buttons[i].button == bev->button &&
  195. buttonmask(&buttons[i], bev->state))
  196. {
  197. if (buttons[i].cmd && buttons[i].cmd(buttons[i].arg))
  198. redraw();
  199. return;
  200. }
  201. }
  202. } else {
  203. /* thumbnail mode (hard-coded) */
  204. switch (bev->button) {
  205. case Button1:
  206. if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
  207. if (sel == tns.sel) {
  208. load_image(tns.sel);
  209. mode = MODE_IMAGE;
  210. timo_cursor = TO_CURSOR_HIDE;
  211. } else {
  212. tns_highlight(&tns, &win, tns.sel, False);
  213. tns_highlight(&tns, &win, sel, True);
  214. tns.sel = sel;
  215. }
  216. redraw();
  217. break;
  218. }
  219. break;
  220. case Button4:
  221. case Button5:
  222. if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN))
  223. redraw();
  224. break;
  225. }
  226. }
  227. }
  228. void run() {
  229. int xfd, timeout;
  230. fd_set fds;
  231. struct timeval tt, t0, t1;
  232. XEvent ev;
  233. timo_cursor = mode == MODE_IMAGE ? TO_CURSOR_HIDE : 0;
  234. redraw();
  235. while (1) {
  236. if (mode == MODE_THUMB && tns.cnt < filecnt) {
  237. /* load thumbnails */
  238. win_set_cursor(&win, CURSOR_WATCH);
  239. gettimeofday(&t0, 0);
  240. while (tns.cnt < filecnt && !XPending(win.env.dpy)) {
  241. if (tns_load(&tns, tns.cnt, &files[tns.cnt], False, False))
  242. tns.cnt++;
  243. else
  244. remove_file(tns.cnt, 0);
  245. gettimeofday(&t1, 0);
  246. if (TIMEDIFF(&t1, &t0) >= TO_THUMBS_LOAD)
  247. break;
  248. }
  249. if (tns.cnt == filecnt)
  250. win_set_cursor(&win, CURSOR_ARROW);
  251. if (!XPending(win.env.dpy)) {
  252. redraw();
  253. continue;
  254. } else {
  255. timo_redraw = TO_THUMBS_LOAD;
  256. }
  257. } else if (timo_cursor || timo_redraw) {
  258. /* check active timeouts */
  259. gettimeofday(&t0, 0);
  260. timeout = MIN(timo_cursor + 1, timo_redraw + 1);
  261. MSEC_TO_TIMEVAL(timeout, &tt);
  262. xfd = ConnectionNumber(win.env.dpy);
  263. FD_ZERO(&fds);
  264. FD_SET(xfd, &fds);
  265. if (!XPending(win.env.dpy))
  266. select(xfd + 1, &fds, 0, 0, &tt);
  267. gettimeofday(&t1, 0);
  268. timeout = MIN(TIMEDIFF(&t1, &t0), timeout);
  269. /* timeouts fired? */
  270. if (timo_cursor) {
  271. timo_cursor = MAX(0, timo_cursor - timeout);
  272. if (!timo_cursor)
  273. win_set_cursor(&win, CURSOR_NONE);
  274. }
  275. if (timo_redraw) {
  276. timo_redraw = MAX(0, timo_redraw - timeout);
  277. if (!timo_redraw)
  278. redraw();
  279. }
  280. if ((timo_cursor || timo_redraw) && !XPending(win.env.dpy))
  281. continue;
  282. }
  283. if (!XNextEvent(win.env.dpy, &ev)) {
  284. /* handle events */
  285. switch (ev.type) {
  286. case ButtonPress:
  287. on_buttonpress(&ev.xbutton);
  288. break;
  289. case ClientMessage:
  290. if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
  291. return;
  292. break;
  293. case ConfigureNotify:
  294. if (win_configure(&win, &ev.xconfigure)) {
  295. timo_redraw = TO_WIN_RESIZE;
  296. if (mode == MODE_IMAGE)
  297. img.checkpan = 1;
  298. else
  299. tns.dirty = 1;
  300. }
  301. break;
  302. case KeyPress:
  303. on_keypress(&ev.xkey);
  304. break;
  305. case MotionNotify:
  306. if (!timo_cursor)
  307. win_set_cursor(&win, CURSOR_ARROW);
  308. timo_cursor = TO_CURSOR_HIDE;
  309. break;
  310. }
  311. }
  312. }
  313. }
  314. int fncmp(const void *a, const void *b) {
  315. return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
  316. }
  317. int main(int argc, char **argv) {
  318. int i, len, start;
  319. size_t n;
  320. char *filename;
  321. struct stat fstats;
  322. r_dir_t dir;
  323. parse_options(argc, argv);
  324. if (options->clean_cache) {
  325. tns_init(&tns, 0);
  326. tns_clean_cache(&tns);
  327. exit(0);
  328. }
  329. if (!options->filecnt) {
  330. print_usage();
  331. exit(1);
  332. }
  333. if (options->recursive || options->from_stdin)
  334. filecnt = FNAME_CNT;
  335. else
  336. filecnt = options->filecnt;
  337. files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t));
  338. fileidx = 0;
  339. /* build file list: */
  340. if (options->from_stdin) {
  341. filename = NULL;
  342. while ((len = getline(&filename, &n, stdin)) > 0) {
  343. if (filename[len-1] == '\n')
  344. filename[len-1] = '\0';
  345. check_add_file(filename);
  346. }
  347. } else {
  348. for (i = 0; i < options->filecnt; i++) {
  349. filename = options->filenames[i];
  350. if (stat(filename, &fstats) || !S_ISDIR(fstats.st_mode)) {
  351. check_add_file(filename);
  352. } else {
  353. if (!options->recursive) {
  354. warn("ignoring directory: %s", filename);
  355. continue;
  356. }
  357. if (r_opendir(&dir, filename)) {
  358. warn("could not open directory: %s", filename);
  359. continue;
  360. }
  361. start = fileidx;
  362. printf("reading dir: %s\n", filename);
  363. while ((filename = r_readdir(&dir))) {
  364. check_add_file(filename);
  365. free((void*) filename);
  366. }
  367. r_closedir(&dir);
  368. if (fileidx - start > 1)
  369. qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
  370. }
  371. }
  372. }
  373. if (!fileidx) {
  374. fprintf(stderr, "sxiv: no valid image file given, aborting\n");
  375. exit(1);
  376. }
  377. filecnt = fileidx;
  378. fileidx = options->startnum < filecnt ? options->startnum : 0;
  379. win_init(&win);
  380. img_init(&img, &win);
  381. if (options->thumbnails) {
  382. mode = MODE_THUMB;
  383. tns_init(&tns, filecnt);
  384. while (!tns_load(&tns, 0, &files[0], False, False))
  385. remove_file(0, 0);
  386. tns.cnt = 1;
  387. } else {
  388. mode = MODE_IMAGE;
  389. tns.thumbs = NULL;
  390. load_image(fileidx);
  391. }
  392. win_open(&win);
  393. run();
  394. cleanup();
  395. return 0;
  396. }