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.
 
 
 
 
 
 

473 lines
9.7 KiB

  1. /* sxiv: commands.c
  2. * Copyright (c) 2011 Bert Muennich <be.muennich at googlemail.com>
  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. #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 "thumbs.h"
  27. #include "util.h"
  28. #include "config.h"
  29. void cleanup(void);
  30. void remove_file(int, bool);
  31. void load_image(int);
  32. void redraw(void);
  33. void reset_cursor(void);
  34. void animate(void);
  35. void slideshow(void);
  36. void set_timeout(timeout_f, int, bool);
  37. void reset_timeout(timeout_f);
  38. extern appmode_t mode;
  39. extern img_t img;
  40. extern tns_t tns;
  41. extern win_t win;
  42. extern fileinfo_t *files;
  43. extern int filecnt, fileidx;
  44. extern int prefix;
  45. const int ss_delays[] = {
  46. 1, 2, 3, 5, 10, 15, 20, 30, 60, 120, 180, 300, 600
  47. };
  48. bool it_quit(arg_t a) {
  49. cleanup();
  50. exit(EXIT_SUCCESS);
  51. }
  52. bool it_switch_mode(arg_t a) {
  53. if (mode == MODE_IMAGE) {
  54. if (tns.thumbs == NULL)
  55. tns_init(&tns, filecnt, &win);
  56. img_close(&img, false);
  57. reset_timeout(reset_cursor);
  58. if (img.slideshow) {
  59. img.slideshow = false;
  60. reset_timeout(slideshow);
  61. }
  62. tns.sel = fileidx;
  63. tns.dirty = true;
  64. mode = MODE_THUMB;
  65. } else {
  66. load_image(tns.sel);
  67. mode = MODE_IMAGE;
  68. }
  69. return true;
  70. }
  71. bool it_toggle_fullscreen(arg_t a) {
  72. win_toggle_fullscreen(&win);
  73. set_timeout(redraw, TO_REDRAW_RESIZE, false);
  74. if (mode == MODE_IMAGE)
  75. img.checkpan = true;
  76. else
  77. tns.dirty = true;
  78. return false;
  79. }
  80. bool it_reload_image(arg_t a) {
  81. if (mode == MODE_IMAGE) {
  82. load_image(fileidx);
  83. } else {
  84. win_set_cursor(&win, CURSOR_WATCH);
  85. if (!tns_load(&tns, tns.sel, &files[tns.sel], true, false)) {
  86. remove_file(tns.sel, false);
  87. tns.dirty = true;
  88. if (tns.sel >= tns.cnt)
  89. tns.sel = tns.cnt - 1;
  90. }
  91. }
  92. return true;
  93. }
  94. bool it_remove_image(arg_t a) {
  95. if (mode == MODE_IMAGE) {
  96. remove_file(fileidx, true);
  97. load_image(fileidx >= filecnt ? filecnt - 1 : fileidx);
  98. return true;
  99. } else if (tns.sel < tns.cnt) {
  100. remove_file(tns.sel, true);
  101. tns.dirty = true;
  102. if (tns.sel >= tns.cnt)
  103. tns.sel = tns.cnt - 1;
  104. return true;
  105. } else {
  106. return false;
  107. }
  108. }
  109. bool i_navigate(arg_t a) {
  110. long n = (long) a;
  111. if (mode == MODE_IMAGE) {
  112. if (prefix > 0)
  113. n *= prefix;
  114. n += fileidx;
  115. if (n < 0)
  116. n = 0;
  117. if (n >= filecnt)
  118. n = filecnt - 1;
  119. if (n != fileidx) {
  120. load_image(n);
  121. return true;
  122. }
  123. }
  124. return false;
  125. }
  126. bool it_first(arg_t a) {
  127. if (mode == MODE_IMAGE && fileidx != 0) {
  128. load_image(0);
  129. return true;
  130. } else if (mode == MODE_THUMB && tns.sel != 0) {
  131. tns.sel = 0;
  132. tns.dirty = true;
  133. return true;
  134. } else {
  135. return false;
  136. }
  137. }
  138. bool it_n_or_last(arg_t a) {
  139. int n = prefix != 0 && prefix - 1 < filecnt ? prefix - 1 : filecnt - 1;
  140. if (mode == MODE_IMAGE && fileidx != n) {
  141. load_image(n);
  142. return true;
  143. } else if (mode == MODE_THUMB && tns.sel != n) {
  144. tns.sel = n;
  145. tns.dirty = true;
  146. return true;
  147. } else {
  148. return false;
  149. }
  150. }
  151. bool i_navigate_frame(arg_t a) {
  152. if (mode == MODE_IMAGE && !img.multi.animate)
  153. return img_frame_navigate(&img, (long) a);
  154. else
  155. return false;
  156. }
  157. bool i_toggle_animation(arg_t a) {
  158. if (mode != MODE_IMAGE)
  159. return false;
  160. if (img.multi.animate) {
  161. reset_timeout(animate);
  162. img.multi.animate = false;
  163. } else if (img_frame_animate(&img, true)) {
  164. set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
  165. }
  166. return true;
  167. }
  168. bool it_scroll_move(arg_t a) {
  169. direction_t dir = (direction_t) a;
  170. if (mode == MODE_IMAGE)
  171. return img_pan(&img, dir, prefix);
  172. else
  173. return tns_move_selection(&tns, dir);
  174. }
  175. bool it_scroll_screen(arg_t a) {
  176. direction_t dir = (direction_t) a;
  177. if (mode == MODE_IMAGE)
  178. return img_pan(&img, dir, -1);
  179. else
  180. return tns_scroll(&tns, dir, true);
  181. }
  182. bool i_scroll_to_edge(arg_t a) {
  183. direction_t dir = (direction_t) a;
  184. if (mode == MODE_IMAGE)
  185. return img_pan_edge(&img, dir);
  186. else
  187. return false;
  188. }
  189. /* Xlib helper function for i_drag() */
  190. Bool is_motionnotify(Display *d, XEvent *e, XPointer a) {
  191. return e != NULL && e->type == MotionNotify;
  192. }
  193. bool i_drag(arg_t a) {
  194. int dx = 0, dy = 0, i, ox, oy, x, y;
  195. unsigned int ui;
  196. bool dragging = true, next = false;
  197. XEvent e;
  198. Window w;
  199. if (mode != MODE_IMAGE)
  200. return false;
  201. if (!XQueryPointer(win.env.dpy, win.xwin, &w, &w, &i, &i, &ox, &oy, &ui))
  202. return false;
  203. win_set_cursor(&win, CURSOR_HAND);
  204. while (dragging) {
  205. if (!next)
  206. XMaskEvent(win.env.dpy,
  207. ButtonPressMask | ButtonReleaseMask | PointerMotionMask, &e);
  208. switch (e.type) {
  209. case ButtonPress:
  210. case ButtonRelease:
  211. dragging = false;
  212. break;
  213. case MotionNotify:
  214. x = e.xmotion.x;
  215. y = e.xmotion.y;
  216. if (x >= 0 && x <= win.w && y >= 0 && y <= win.h) {
  217. dx += x - ox;
  218. dy += y - oy;
  219. }
  220. ox = x;
  221. oy = y;
  222. break;
  223. }
  224. if (dragging)
  225. next = XCheckIfEvent(win.env.dpy, &e, is_motionnotify, None);
  226. if ((!dragging || !next) && (dx != 0 || dy != 0)) {
  227. if (img_move(&img, dx, dy))
  228. img_render(&img);
  229. dx = dy = 0;
  230. }
  231. }
  232. win_set_cursor(&win, CURSOR_ARROW);
  233. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  234. reset_timeout(redraw);
  235. return false;
  236. }
  237. bool i_zoom(arg_t a) {
  238. long scale = (long) a;
  239. if (mode != MODE_IMAGE)
  240. return false;
  241. if (scale > 0)
  242. return img_zoom_in(&img);
  243. else if (scale < 0)
  244. return img_zoom_out(&img);
  245. else
  246. return false;
  247. }
  248. bool i_set_zoom(arg_t a) {
  249. if (mode == MODE_IMAGE)
  250. return img_zoom(&img, (prefix ? prefix : (long) a) / 100.0);
  251. else
  252. return false;
  253. }
  254. bool i_fit_to_win(arg_t a) {
  255. bool ret = false;
  256. if (mode == MODE_IMAGE) {
  257. if ((ret = img_fit_win(&img)))
  258. img_center(&img);
  259. }
  260. return ret;
  261. }
  262. bool i_fit_to_img(arg_t a) {
  263. int x, y;
  264. unsigned int w, h;
  265. bool ret = false;
  266. if (mode == MODE_IMAGE) {
  267. x = MAX(0, win.x + img.x);
  268. y = MAX(0, win.y + img.y);
  269. w = img.w * img.zoom;
  270. h = img.h * img.zoom;
  271. if ((ret = win_moveresize(&win, x, y, w, h))) {
  272. img.x = x - win.x;
  273. img.y = y - win.y;
  274. img.dirty = true;
  275. }
  276. }
  277. return ret;
  278. }
  279. bool i_rotate(arg_t a) {
  280. direction_t dir = (direction_t) a;
  281. if (mode == MODE_IMAGE) {
  282. if (dir == DIR_LEFT) {
  283. img_rotate_left(&img);
  284. return true;
  285. } else if (dir == DIR_RIGHT) {
  286. img_rotate_right(&img);
  287. return true;
  288. }
  289. }
  290. return false;
  291. }
  292. bool i_toggle_slideshow(arg_t a) {
  293. if (mode == MODE_IMAGE) {
  294. if (img.slideshow) {
  295. img.slideshow = false;
  296. reset_timeout(slideshow);
  297. return true;
  298. } else if (fileidx + 1 < filecnt) {
  299. img.slideshow = true;
  300. set_timeout(slideshow, img.ss_delay, true);
  301. return true;
  302. }
  303. }
  304. return false;
  305. }
  306. bool i_adjust_slideshow(arg_t a) {
  307. long d = (long) a;
  308. int i;
  309. if (mode != MODE_IMAGE || !img.slideshow)
  310. return false;
  311. if (d < 0) {
  312. for (i = ARRLEN(ss_delays) - 2; i >= 0; i--) {
  313. if (img.ss_delay > ss_delays[i] * 1000) {
  314. img.ss_delay = ss_delays[i] * 1000;
  315. return true;
  316. }
  317. }
  318. } else {
  319. for (i = 1; i < ARRLEN(ss_delays); i++) {
  320. if (img.ss_delay < ss_delays[i] * 1000) {
  321. img.ss_delay = ss_delays[i] * 1000;
  322. return true;
  323. }
  324. }
  325. }
  326. return false;
  327. }
  328. bool i_reset_slideshow(arg_t a) {
  329. if (mode != MODE_IMAGE || !img.slideshow)
  330. return false;
  331. if (prefix > 0) {
  332. img.ss_delay = MIN(prefix, ss_delays[ARRLEN(ss_delays) - 1]);
  333. img.ss_delay = MAX(img.ss_delay, ss_delays[0]) * 1000;
  334. } else {
  335. img.ss_delay = SLIDESHOW_DELAY * 1000;
  336. }
  337. return true;
  338. }
  339. bool i_toggle_antialias(arg_t a) {
  340. if (mode == MODE_IMAGE) {
  341. img_toggle_antialias(&img);
  342. return true;
  343. } else {
  344. return false;
  345. }
  346. }
  347. bool it_toggle_alpha(arg_t a) {
  348. img.alpha = tns.alpha = !img.alpha;
  349. if (mode == MODE_IMAGE)
  350. img.dirty = true;
  351. else
  352. tns.dirty = true;
  353. return true;
  354. }
  355. bool it_open_with(arg_t a) {
  356. const char *prog = (const char*) a;
  357. pid_t pid;
  358. if (prog == NULL || *prog == '\0')
  359. return false;
  360. if ((pid = fork()) == 0) {
  361. execlp(prog, prog,
  362. files[mode == MODE_IMAGE ? fileidx : tns.sel].path, NULL);
  363. warn("could not exec: %s", prog);
  364. exit(EXIT_FAILURE);
  365. } else if (pid < 0) {
  366. warn("could not fork. program was: %s", prog);
  367. }
  368. return false;
  369. }
  370. bool it_shell_cmd(arg_t a) {
  371. int n, status;
  372. const char *cmdline = (const char*) a;
  373. pid_t pid;
  374. if (cmdline == NULL || *cmdline == '\0')
  375. return false;
  376. n = mode == MODE_IMAGE ? fileidx : tns.sel;
  377. if (setenv("SXIV_IMG", files[n].path, 1) < 0) {
  378. warn("could not set env.-variable: SXIV_IMG. command line was: %s",
  379. cmdline);
  380. return false;
  381. }
  382. if ((pid = fork()) == 0) {
  383. execl("/bin/sh", "/bin/sh", "-c", cmdline, NULL);
  384. warn("could not exec: /bin/sh. command line was: %s", cmdline);
  385. exit(EXIT_FAILURE);
  386. } else if (pid < 0) {
  387. warn("could not fork. command line was: %s", cmdline);
  388. return false;
  389. }
  390. win_set_cursor(&win, CURSOR_WATCH);
  391. waitpid(pid, &status, 0);
  392. if (WIFEXITED(status) == 0 || WEXITSTATUS(status) != 0)
  393. warn("child exited with non-zero return value: %d. command line was: %s",
  394. WEXITSTATUS(status), cmdline);
  395. if (mode == MODE_IMAGE) {
  396. img_close(&img, true);
  397. load_image(fileidx);
  398. }
  399. if (!tns_load(&tns, n, &files[n], true, mode == MODE_IMAGE) &&
  400. mode == MODE_THUMB)
  401. {
  402. remove_file(tns.sel, false);
  403. tns.dirty = true;
  404. if (tns.sel >= tns.cnt)
  405. tns.sel = tns.cnt - 1;
  406. }
  407. return true;
  408. }