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.
 
 
 
 
 
 

440 line
9.0 KiB

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