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.
 
 
 
 
 
 

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