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.
 
 
 
 
 
 

569 lines
11 KiB

  1. /* Copyright 2011, 2012 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 _IMAGE_CONFIG
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include <sys/wait.h>
  24. #include "commands.h"
  25. #include "image.h"
  26. #include "options.h"
  27. #include "thumbs.h"
  28. #include "util.h"
  29. #include "config.h"
  30. void cleanup(void);
  31. void remove_file(int, bool);
  32. void load_image(int);
  33. void open_info(void);
  34. void redraw(void);
  35. void reset_cursor(void);
  36. void animate(void);
  37. void set_timeout(timeout_f, int, bool);
  38. void reset_timeout(timeout_f);
  39. extern appmode_t mode;
  40. extern img_t img;
  41. extern tns_t tns;
  42. extern win_t win;
  43. extern fileinfo_t *files;
  44. extern int filecnt, fileidx;
  45. extern int alternate;
  46. extern int prefix;
  47. const int ss_delays[] = {
  48. 1, 2, 3, 5, 10, 15, 20, 30, 60, 120, 180, 300, 600
  49. };
  50. bool it_quit(arg_t a)
  51. {
  52. unsigned int i;
  53. if (options->to_stdout) {
  54. for (i = 0; i < filecnt; i++) {
  55. if (files[i].marked)
  56. printf("%s\n", files[i].name);
  57. }
  58. }
  59. cleanup();
  60. exit(EXIT_SUCCESS);
  61. }
  62. bool it_switch_mode(arg_t a)
  63. {
  64. if (mode == MODE_IMAGE) {
  65. if (tns.thumbs == NULL) {
  66. tns_init(&tns, filecnt, &win);
  67. tns.alpha = img.alpha;
  68. }
  69. img_close(&img, false);
  70. reset_timeout(reset_cursor);
  71. tns.sel = fileidx;
  72. tns.dirty = true;
  73. mode = MODE_THUMB;
  74. } else {
  75. load_image(tns.sel);
  76. mode = MODE_IMAGE;
  77. }
  78. return true;
  79. }
  80. bool it_toggle_fullscreen(arg_t a)
  81. {
  82. win_toggle_fullscreen(&win);
  83. /* redraw after next ConfigureNotify event */
  84. set_timeout(redraw, TO_REDRAW_RESIZE, false);
  85. if (mode == MODE_IMAGE)
  86. img.checkpan = img.dirty = true;
  87. else
  88. tns.dirty = true;
  89. return false;
  90. }
  91. bool it_toggle_bar(arg_t a)
  92. {
  93. win_toggle_bar(&win);
  94. if (mode == MODE_IMAGE) {
  95. img.checkpan = img.dirty = true;
  96. if (win.bar.h > 0)
  97. open_info();
  98. } else {
  99. tns.dirty = true;
  100. }
  101. return true;
  102. }
  103. bool t_reload_all(arg_t a)
  104. {
  105. if (mode == MODE_THUMB) {
  106. tns_free(&tns);
  107. tns_init(&tns, filecnt, &win);
  108. return true;
  109. } else {
  110. return false;
  111. }
  112. }
  113. bool it_reload_image(arg_t a)
  114. {
  115. if (mode == MODE_IMAGE) {
  116. load_image(fileidx);
  117. } else {
  118. win_set_cursor(&win, CURSOR_WATCH);
  119. if (!tns_load(&tns, tns.sel, &files[tns.sel], true, false)) {
  120. remove_file(tns.sel, false);
  121. tns.dirty = true;
  122. if (tns.sel >= tns.cnt)
  123. tns.sel = tns.cnt - 1;
  124. }
  125. }
  126. return true;
  127. }
  128. bool it_remove_image(arg_t a)
  129. {
  130. if (mode == MODE_IMAGE) {
  131. remove_file(fileidx, true);
  132. load_image(fileidx >= filecnt ? filecnt - 1 : fileidx);
  133. return true;
  134. } else if (tns.sel < tns.cnt) {
  135. remove_file(tns.sel, true);
  136. tns.dirty = true;
  137. if (tns.sel >= tns.cnt)
  138. tns.sel = tns.cnt - 1;
  139. return true;
  140. } else {
  141. return false;
  142. }
  143. }
  144. bool i_navigate(arg_t a)
  145. {
  146. long n = (long) a;
  147. if (mode == MODE_IMAGE) {
  148. if (prefix > 0)
  149. n *= prefix;
  150. n += fileidx;
  151. if (n < 0)
  152. n = 0;
  153. if (n >= filecnt)
  154. n = filecnt - 1;
  155. if (n != fileidx) {
  156. load_image(n);
  157. return true;
  158. }
  159. }
  160. return false;
  161. }
  162. bool i_alternate(arg_t a)
  163. {
  164. if (mode == MODE_IMAGE) {
  165. load_image(alternate);
  166. return true;
  167. } else {
  168. return false;
  169. }
  170. }
  171. bool it_first(arg_t a)
  172. {
  173. if (mode == MODE_IMAGE && fileidx != 0) {
  174. load_image(0);
  175. return true;
  176. } else if (mode == MODE_THUMB && tns.sel != 0) {
  177. tns.sel = 0;
  178. tns.dirty = true;
  179. return true;
  180. } else {
  181. return false;
  182. }
  183. }
  184. bool it_n_or_last(arg_t a)
  185. {
  186. int n = prefix != 0 && prefix - 1 < filecnt ? prefix - 1 : filecnt - 1;
  187. if (mode == MODE_IMAGE && fileidx != n) {
  188. load_image(n);
  189. return true;
  190. } else if (mode == MODE_THUMB && tns.sel != n) {
  191. tns.sel = n;
  192. tns.dirty = true;
  193. return true;
  194. } else {
  195. return false;
  196. }
  197. }
  198. bool i_navigate_frame(arg_t a)
  199. {
  200. if (mode == MODE_IMAGE && !img.multi.animate)
  201. return img_frame_navigate(&img, (long) a);
  202. else
  203. return false;
  204. }
  205. bool i_toggle_animation(arg_t a)
  206. {
  207. if (mode != MODE_IMAGE)
  208. return false;
  209. if (img.multi.animate) {
  210. reset_timeout(animate);
  211. img.multi.animate = false;
  212. } else if (img_frame_animate(&img, true)) {
  213. set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
  214. }
  215. return true;
  216. }
  217. bool it_toggle_image_mark(arg_t a)
  218. {
  219. int sel = mode == MODE_IMAGE ? fileidx : tns.sel;
  220. files[sel].marked = !files[sel].marked;
  221. if (mode == MODE_THUMB)
  222. tns_mark(&tns, sel, files[sel].marked);
  223. return true;
  224. }
  225. bool it_navigate_marked(arg_t a)
  226. {
  227. long n = (long) a;
  228. int d, i, cnt, sel, new;
  229. if (mode == MODE_IMAGE)
  230. cnt = filecnt, sel = new = fileidx;
  231. else
  232. cnt = tns.cnt, sel = new = tns.sel;
  233. if (prefix > 0)
  234. n *= prefix;
  235. d = n > 0 ? 1 : -1;
  236. for (i = sel + d; n != 0 && i >= 0 && i < cnt; i += d) {
  237. if (files[i].marked) {
  238. n -= d;
  239. new = i;
  240. }
  241. }
  242. if (new != sel) {
  243. if (mode == MODE_IMAGE) {
  244. load_image(new);
  245. } else {
  246. tns.sel = new;
  247. tns.dirty = true;
  248. }
  249. return true;
  250. } else {
  251. return false;
  252. }
  253. }
  254. bool it_scroll_move(arg_t a)
  255. {
  256. direction_t dir = (direction_t) a;
  257. if (mode == MODE_IMAGE)
  258. return img_pan(&img, dir, prefix);
  259. else
  260. return tns_move_selection(&tns, dir, prefix);
  261. }
  262. bool it_scroll_screen(arg_t a)
  263. {
  264. direction_t dir = (direction_t) a;
  265. if (mode == MODE_IMAGE)
  266. return img_pan(&img, dir, -1);
  267. else
  268. return tns_scroll(&tns, dir, true);
  269. }
  270. bool i_scroll_to_edge(arg_t a)
  271. {
  272. direction_t dir = (direction_t) a;
  273. if (mode == MODE_IMAGE)
  274. return img_pan_edge(&img, dir);
  275. else
  276. return false;
  277. }
  278. /* Xlib helper function for i_drag() */
  279. Bool is_motionnotify(Display *d, XEvent *e, XPointer a)
  280. {
  281. return e != NULL && e->type == MotionNotify;
  282. }
  283. #define WARP(x,y) \
  284. XWarpPointer(win.env.dpy, None, win.xwin, 0, 0, 0, 0, x, y); \
  285. ox = x, oy = y; \
  286. break
  287. bool i_drag(arg_t a)
  288. {
  289. int dx = 0, dy = 0, i, ox, oy, x, y;
  290. unsigned int ui;
  291. bool dragging = true, next = false;
  292. XEvent e;
  293. Window w;
  294. if (mode != MODE_IMAGE)
  295. return false;
  296. if (!XQueryPointer(win.env.dpy, win.xwin, &w, &w, &i, &i, &ox, &oy, &ui))
  297. return false;
  298. win_set_cursor(&win, CURSOR_HAND);
  299. while (dragging) {
  300. if (!next)
  301. XMaskEvent(win.env.dpy,
  302. ButtonPressMask | ButtonReleaseMask | PointerMotionMask, &e);
  303. switch (e.type) {
  304. case ButtonPress:
  305. case ButtonRelease:
  306. dragging = false;
  307. break;
  308. case MotionNotify:
  309. x = e.xmotion.x;
  310. y = e.xmotion.y;
  311. /* wrap the mouse around */
  312. if (x <= 0) {
  313. WARP(win.w - 2, y);
  314. } else if (x >= win.w - 1) {
  315. WARP(1, y);
  316. } else if (y <= 0) {
  317. WARP(x, win.h - 2);
  318. } else if (y >= win.h - 1) {
  319. WARP(x, 1);
  320. }
  321. dx += x - ox;
  322. dy += y - oy;
  323. ox = x;
  324. oy = y;
  325. break;
  326. }
  327. if (dragging)
  328. next = XCheckIfEvent(win.env.dpy, &e, is_motionnotify, None);
  329. if ((!dragging || !next) && (dx != 0 || dy != 0)) {
  330. if (img_move(&img, dx, dy)) {
  331. img_render(&img);
  332. win_draw(&win);
  333. }
  334. dx = dy = 0;
  335. }
  336. }
  337. win_set_cursor(&win, CURSOR_ARROW);
  338. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  339. reset_timeout(redraw);
  340. return false;
  341. }
  342. bool i_zoom(arg_t a)
  343. {
  344. long scale = (long) a;
  345. if (mode != MODE_IMAGE)
  346. return false;
  347. if (scale > 0)
  348. return img_zoom_in(&img);
  349. else if (scale < 0)
  350. return img_zoom_out(&img);
  351. else
  352. return false;
  353. }
  354. bool i_set_zoom(arg_t a)
  355. {
  356. if (mode == MODE_IMAGE)
  357. return img_zoom(&img, (prefix ? prefix : (long) a) / 100.0);
  358. else
  359. return false;
  360. }
  361. bool i_fit_to_win(arg_t a)
  362. {
  363. bool ret = false;
  364. scalemode_t sm = (scalemode_t) a;
  365. if (mode == MODE_IMAGE) {
  366. if ((ret = img_fit_win(&img, sm)))
  367. img_center(&img);
  368. }
  369. return ret;
  370. }
  371. bool i_fit_to_img(arg_t a)
  372. {
  373. int x, y;
  374. unsigned int w, h;
  375. bool ret = false;
  376. if (mode == MODE_IMAGE) {
  377. x = MAX(0, win.x + img.x);
  378. y = MAX(0, win.y + img.y);
  379. w = img.w * img.zoom;
  380. h = img.h * img.zoom;
  381. if ((ret = win_moveresize(&win, x, y, w, h))) {
  382. img.x = x - win.x;
  383. img.y = y - win.y;
  384. img.dirty = true;
  385. }
  386. }
  387. return ret;
  388. }
  389. bool i_rotate(arg_t a)
  390. {
  391. degree_t degree = (degree_t) a;
  392. if (mode == MODE_IMAGE) {
  393. img_rotate(&img, degree);
  394. return true;
  395. } else {
  396. return false;
  397. }
  398. }
  399. bool i_flip(arg_t a)
  400. {
  401. flipdir_t dir = (flipdir_t) a;
  402. if (mode == MODE_IMAGE) {
  403. img_flip(&img, dir);
  404. return true;
  405. } else {
  406. return false;
  407. }
  408. }
  409. bool i_toggle_antialias(arg_t a)
  410. {
  411. if (mode == MODE_IMAGE) {
  412. img_toggle_antialias(&img);
  413. return true;
  414. } else {
  415. return false;
  416. }
  417. }
  418. /* a < 0: decrease gamma
  419. * a == 0: reset gamma
  420. * a > 0: increase gamma
  421. */
  422. bool i_change_gamma(arg_t a)
  423. {
  424. if (mode == MODE_IMAGE) {
  425. long val = (long) a;
  426. int delta = val > 0 ? 1 : (val < 0 ? -1 : -img.gamma);
  427. img_set_gamma(&img, img.gamma + delta);
  428. return true;
  429. } else {
  430. return false;
  431. }
  432. }
  433. bool it_toggle_alpha(arg_t a)
  434. {
  435. img.alpha = tns.alpha = !img.alpha;
  436. if (mode == MODE_IMAGE)
  437. img.dirty = true;
  438. else
  439. tns.dirty = true;
  440. return true;
  441. }
  442. bool it_open_with(arg_t a)
  443. {
  444. const char *prog = (const char*) a;
  445. pid_t pid;
  446. if (prog == NULL || *prog == '\0')
  447. return false;
  448. if ((pid = fork()) == 0) {
  449. execlp(prog, prog,
  450. files[mode == MODE_IMAGE ? fileidx : tns.sel].path, NULL);
  451. warn("could not exec: %s", prog);
  452. exit(EXIT_FAILURE);
  453. } else if (pid < 0) {
  454. warn("could not fork. program was: %s", prog);
  455. }
  456. return false;
  457. }
  458. bool it_shell_cmd(arg_t a)
  459. {
  460. int n, status;
  461. const char *cmdline = (const char*) a;
  462. pid_t pid;
  463. if (cmdline == NULL || *cmdline == '\0')
  464. return false;
  465. n = mode == MODE_IMAGE ? fileidx : tns.sel;
  466. if (setenv("SXIV_IMG", files[n].path, 1) < 0) {
  467. warn("could not set env.-variable: SXIV_IMG. command line was: %s",
  468. cmdline);
  469. return false;
  470. }
  471. if ((pid = fork()) == 0) {
  472. execl("/bin/sh", "/bin/sh", "-c", cmdline, NULL);
  473. warn("could not exec: /bin/sh. command line was: %s", cmdline);
  474. exit(EXIT_FAILURE);
  475. } else if (pid < 0) {
  476. warn("could not fork. command line was: %s", cmdline);
  477. return false;
  478. }
  479. win_set_cursor(&win, CURSOR_WATCH);
  480. waitpid(pid, &status, 0);
  481. if (WIFEXITED(status) == 0 || WEXITSTATUS(status) != 0)
  482. warn("child exited with non-zero return value: %d. command line was: %s",
  483. WEXITSTATUS(status), cmdline);
  484. if (mode == MODE_IMAGE) {
  485. img_close(&img, true);
  486. load_image(fileidx);
  487. }
  488. if (!tns_load(&tns, n, &files[n], true, mode == MODE_IMAGE) &&
  489. mode == MODE_THUMB)
  490. {
  491. remove_file(tns.sel, false);
  492. tns.dirty = true;
  493. if (tns.sel >= tns.cnt)
  494. tns.sel = tns.cnt - 1;
  495. }
  496. return true;
  497. }