A Simple X Image Viewer
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

472 satır
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. int timo_adelay; /* multi-frame animation delay time */
  50. void cleanup() {
  51. static int in = 0;
  52. if (!in++) {
  53. img_close(&img, 0);
  54. tns_free(&tns);
  55. win_close(&win);
  56. }
  57. }
  58. void check_add_file(char *filename) {
  59. if (!filename || !*filename)
  60. return;
  61. if (access(filename, R_OK)) {
  62. warn("could not open file: %s", filename);
  63. return;
  64. }
  65. if (fileidx == filecnt) {
  66. filecnt *= 2;
  67. files = (fileinfo_t*) s_realloc(files, filecnt * sizeof(fileinfo_t));
  68. }
  69. if (*filename != '/') {
  70. files[fileidx].path = absolute_path(filename);
  71. if (!files[fileidx].path) {
  72. warn("could not get absolute path of file: %s\n", filename);
  73. return;
  74. }
  75. }
  76. files[fileidx].name = s_strdup(filename);
  77. if (*filename == '/')
  78. files[fileidx].path = files[fileidx].name;
  79. fileidx++;
  80. }
  81. void remove_file(int n, unsigned char silent) {
  82. if (n < 0 || n >= filecnt)
  83. return;
  84. if (filecnt == 1) {
  85. if (!silent)
  86. fprintf(stderr, "sxiv: no more files to display, aborting\n");
  87. cleanup();
  88. exit(!silent);
  89. }
  90. if (n + 1 < filecnt) {
  91. if (files[n].path != files[n].name)
  92. free((void*) files[n].path);
  93. free((void*) files[n].name);
  94. memmove(files + n, files + n + 1, (filecnt - n - 1) * sizeof(fileinfo_t));
  95. }
  96. if (n + 1 < tns.cnt) {
  97. memmove(tns.thumbs + n, tns.thumbs + n + 1, (tns.cnt - n - 1) *
  98. sizeof(thumb_t));
  99. memset(tns.thumbs + tns.cnt - 1, 0, sizeof(thumb_t));
  100. }
  101. filecnt--;
  102. if (n < tns.cnt)
  103. tns.cnt--;
  104. }
  105. void load_image(int new) {
  106. struct stat fstats;
  107. if (new < 0 || new >= filecnt)
  108. return;
  109. /* cursor gets reset in redraw() */
  110. win_set_cursor(&win, CURSOR_WATCH);
  111. img_close(&img, 0);
  112. while (!img_load(&img, &files[new])) {
  113. remove_file(new, 0);
  114. if (new >= filecnt)
  115. new = filecnt - 1;
  116. }
  117. fileidx = new;
  118. if (!stat(files[new].path, &fstats))
  119. filesize = fstats.st_size;
  120. else
  121. filesize = 0;
  122. if (img.multi.cnt && img.multi.animate)
  123. timo_adelay = img.multi.frames[img.multi.sel].delay;
  124. }
  125. void update_title() {
  126. int n;
  127. float size;
  128. const char *unit;
  129. if (mode == MODE_THUMB) {
  130. n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] %s",
  131. tns.cnt ? tns.sel + 1 : 0, tns.cnt,
  132. tns.cnt ? files[tns.sel].name : "");
  133. } else {
  134. size = filesize;
  135. size_readable(&size, &unit);
  136. if (img.multi.cnt)
  137. n = snprintf(win_title, TITLE_LEN,
  138. "sxiv: [%d/%d] <%d%%> <%dx%d> (%.2f%s) {%d/%d} %s",
  139. fileidx + 1, filecnt, (int) (img.zoom * 100.0), img.w,
  140. img.h, size, unit, img.multi.sel + 1, img.multi.cnt,
  141. files[fileidx].name);
  142. else
  143. n = snprintf(win_title, TITLE_LEN,
  144. "sxiv: [%d/%d] <%d%%> <%dx%d> (%.2f%s) %s",
  145. fileidx + 1, filecnt, (int) (img.zoom * 100.0), img.w,
  146. img.h, size, unit, files[fileidx].name);
  147. }
  148. if (n >= TITLE_LEN) {
  149. for (n = 0; n < 3; n++)
  150. win_title[TITLE_LEN - n - 2] = '.';
  151. }
  152. win_set_title(&win, win_title);
  153. }
  154. void redraw() {
  155. if (mode == MODE_IMAGE) {
  156. img_render(&img, &win);
  157. if (timo_cursor)
  158. win_set_cursor(&win, CURSOR_ARROW);
  159. else
  160. win_set_cursor(&win, CURSOR_NONE);
  161. } else {
  162. tns_render(&tns, &win);
  163. }
  164. update_title();
  165. timo_redraw = 0;
  166. }
  167. Bool keymask(const keymap_t *k, unsigned int state) {
  168. return (k->ctrl ? ControlMask : 0) == (state & ControlMask);
  169. }
  170. Bool buttonmask(const button_t *b, unsigned int state) {
  171. return ((b->ctrl ? ControlMask : 0) | (b->shift ? ShiftMask : 0)) ==
  172. (state & (ControlMask | ShiftMask));
  173. }
  174. void on_keypress(XKeyEvent *kev) {
  175. int i;
  176. KeySym ksym;
  177. char key;
  178. if (!kev)
  179. return;
  180. XLookupString(kev, &key, 1, &ksym, NULL);
  181. for (i = 0; i < LEN(keys); i++) {
  182. if (keys[i].ksym == ksym && keymask(&keys[i], kev->state)) {
  183. if (keys[i].cmd && keys[i].cmd(keys[i].arg))
  184. redraw();
  185. return;
  186. }
  187. }
  188. }
  189. void on_buttonpress(XButtonEvent *bev) {
  190. int i, sel;
  191. if (!bev)
  192. return;
  193. if (mode == MODE_IMAGE) {
  194. win_set_cursor(&win, CURSOR_ARROW);
  195. timo_cursor = TO_CURSOR_HIDE;
  196. for (i = 0; i < LEN(buttons); i++) {
  197. if (buttons[i].button == bev->button &&
  198. buttonmask(&buttons[i], bev->state))
  199. {
  200. if (buttons[i].cmd && buttons[i].cmd(buttons[i].arg))
  201. redraw();
  202. return;
  203. }
  204. }
  205. } else {
  206. /* thumbnail mode (hard-coded) */
  207. switch (bev->button) {
  208. case Button1:
  209. if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
  210. if (sel == tns.sel) {
  211. load_image(tns.sel);
  212. mode = MODE_IMAGE;
  213. timo_cursor = TO_CURSOR_HIDE;
  214. } else {
  215. tns_highlight(&tns, &win, tns.sel, False);
  216. tns_highlight(&tns, &win, sel, True);
  217. tns.sel = sel;
  218. }
  219. redraw();
  220. break;
  221. }
  222. break;
  223. case Button4:
  224. case Button5:
  225. if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN))
  226. redraw();
  227. break;
  228. }
  229. }
  230. }
  231. void run() {
  232. int xfd, timeout;
  233. fd_set fds;
  234. struct timeval tt, t0, t1;
  235. XEvent ev;
  236. timo_cursor = mode == MODE_IMAGE ? TO_CURSOR_HIDE : 0;
  237. redraw();
  238. while (1) {
  239. if (mode == MODE_THUMB && tns.cnt < filecnt) {
  240. /* load thumbnails */
  241. win_set_cursor(&win, CURSOR_WATCH);
  242. gettimeofday(&t0, 0);
  243. while (tns.cnt < filecnt && !XPending(win.env.dpy)) {
  244. if (tns_load(&tns, tns.cnt, &files[tns.cnt], False, False))
  245. tns.cnt++;
  246. else
  247. remove_file(tns.cnt, 0);
  248. gettimeofday(&t1, 0);
  249. if (TIMEDIFF(&t1, &t0) >= TO_THUMBS_LOAD)
  250. break;
  251. }
  252. if (tns.cnt == filecnt)
  253. win_set_cursor(&win, CURSOR_ARROW);
  254. if (!XPending(win.env.dpy)) {
  255. redraw();
  256. continue;
  257. } else {
  258. timo_redraw = TO_THUMBS_LOAD;
  259. }
  260. }
  261. if (timo_cursor || timo_redraw || timo_adelay) {
  262. /* check active timeouts */
  263. gettimeofday(&t0, 0);
  264. timeout = min_int_nz(3, timo_cursor, timo_redraw, timo_adelay);
  265. MSEC_TO_TIMEVAL(timeout, &tt);
  266. xfd = ConnectionNumber(win.env.dpy);
  267. FD_ZERO(&fds);
  268. FD_SET(xfd, &fds);
  269. if (!XPending(win.env.dpy))
  270. select(xfd + 1, &fds, 0, 0, &tt);
  271. gettimeofday(&t1, 0);
  272. timeout = MIN(TIMEDIFF(&t1, &t0), timeout);
  273. /* timeouts fired? */
  274. if (timo_cursor) {
  275. timo_cursor = MAX(0, timo_cursor - timeout);
  276. if (!timo_cursor)
  277. win_set_cursor(&win, CURSOR_NONE);
  278. }
  279. if (timo_redraw) {
  280. timo_redraw = MAX(0, timo_redraw - timeout);
  281. if (!timo_redraw)
  282. redraw();
  283. }
  284. if (timo_adelay) {
  285. timo_adelay = MAX(0, timo_adelay - timeout);
  286. if (!timo_adelay) {
  287. if ((timo_adelay = img_frame_animate(&img, 0)))
  288. redraw();
  289. }
  290. }
  291. if ((timo_cursor || timo_redraw || timo_adelay) &&
  292. !XPending(win.env.dpy))
  293. continue;
  294. }
  295. if (!XNextEvent(win.env.dpy, &ev)) {
  296. /* handle events */
  297. switch (ev.type) {
  298. case ButtonPress:
  299. on_buttonpress(&ev.xbutton);
  300. break;
  301. case ClientMessage:
  302. if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
  303. return;
  304. break;
  305. case ConfigureNotify:
  306. if (win_configure(&win, &ev.xconfigure)) {
  307. timo_redraw = TO_WIN_RESIZE;
  308. if (mode == MODE_IMAGE)
  309. img.checkpan = 1;
  310. else
  311. tns.dirty = 1;
  312. }
  313. break;
  314. case KeyPress:
  315. on_keypress(&ev.xkey);
  316. break;
  317. case MotionNotify:
  318. if (!timo_cursor)
  319. win_set_cursor(&win, CURSOR_ARROW);
  320. timo_cursor = TO_CURSOR_HIDE;
  321. break;
  322. }
  323. }
  324. }
  325. }
  326. int fncmp(const void *a, const void *b) {
  327. return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
  328. }
  329. int main(int argc, char **argv) {
  330. int i, len, start;
  331. size_t n;
  332. char *filename;
  333. struct stat fstats;
  334. r_dir_t dir;
  335. parse_options(argc, argv);
  336. if (options->clean_cache) {
  337. tns_init(&tns, 0);
  338. tns_clean_cache(&tns);
  339. exit(0);
  340. }
  341. if (!options->filecnt) {
  342. print_usage();
  343. exit(1);
  344. }
  345. if (options->recursive || options->from_stdin)
  346. filecnt = FNAME_CNT;
  347. else
  348. filecnt = options->filecnt;
  349. files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t));
  350. fileidx = 0;
  351. /* build file list: */
  352. if (options->from_stdin) {
  353. filename = NULL;
  354. while ((len = getline(&filename, &n, stdin)) > 0) {
  355. if (filename[len-1] == '\n')
  356. filename[len-1] = '\0';
  357. check_add_file(filename);
  358. }
  359. } else {
  360. for (i = 0; i < options->filecnt; i++) {
  361. filename = options->filenames[i];
  362. if (stat(filename, &fstats) || !S_ISDIR(fstats.st_mode)) {
  363. check_add_file(filename);
  364. } else {
  365. if (!options->recursive) {
  366. warn("ignoring directory: %s", filename);
  367. continue;
  368. }
  369. if (r_opendir(&dir, filename)) {
  370. warn("could not open directory: %s", filename);
  371. continue;
  372. }
  373. start = fileidx;
  374. printf("reading dir: %s\n", filename);
  375. while ((filename = r_readdir(&dir))) {
  376. check_add_file(filename);
  377. free((void*) filename);
  378. }
  379. r_closedir(&dir);
  380. if (fileidx - start > 1)
  381. qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
  382. }
  383. }
  384. }
  385. if (!fileidx) {
  386. fprintf(stderr, "sxiv: no valid image file given, aborting\n");
  387. exit(1);
  388. }
  389. filecnt = fileidx;
  390. fileidx = options->startnum < filecnt ? options->startnum : 0;
  391. win_init(&win);
  392. img_init(&img, &win);
  393. if (options->thumbnails) {
  394. mode = MODE_THUMB;
  395. tns_init(&tns, filecnt);
  396. while (!tns_load(&tns, 0, &files[0], False, False))
  397. remove_file(0, 0);
  398. tns.cnt = 1;
  399. } else {
  400. mode = MODE_IMAGE;
  401. tns.thumbs = NULL;
  402. load_image(fileidx);
  403. }
  404. win_open(&win);
  405. run();
  406. cleanup();
  407. return 0;
  408. }