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.

main.c 11 KiB

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