A Simple X Image Viewer
 
 
 
 
 
 

444 lines
9.1 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, false);
  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, true);
  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 img_zoom(&img, 1.0);
  240. }
  241. bool i_fit_to_win(arg_t a) {
  242. bool ret = false;
  243. if (mode == MODE_IMAGE) {
  244. if ((ret = img_fit_win(&img)))
  245. img_center(&img);
  246. }
  247. return ret;
  248. }
  249. bool i_fit_to_img(arg_t a) {
  250. int x, y;
  251. unsigned int w, h;
  252. bool ret = false;
  253. if (mode == MODE_IMAGE) {
  254. x = MAX(0, win.x + img.x);
  255. y = MAX(0, win.y + img.y);
  256. w = img.w * img.zoom;
  257. h = img.h * img.zoom;
  258. if ((ret = win_moveresize(&win, x, y, w, h))) {
  259. img.x = x - win.x;
  260. img.y = y - win.y;
  261. }
  262. }
  263. return ret;
  264. }
  265. bool i_rotate(arg_t a) {
  266. direction_t dir = (direction_t) a;
  267. if (mode == MODE_IMAGE) {
  268. if (dir == DIR_LEFT) {
  269. img_rotate_left(&img);
  270. return true;
  271. } else if (dir == DIR_RIGHT) {
  272. img_rotate_right(&img);
  273. return true;
  274. }
  275. }
  276. return false;
  277. }
  278. bool i_toggle_slideshow(arg_t a) {
  279. if (mode == MODE_IMAGE) {
  280. if (img.slideshow) {
  281. img.slideshow = false;
  282. reset_timeout(slideshow);
  283. return true;
  284. } else if (fileidx + 1 < filecnt) {
  285. img.slideshow = true;
  286. set_timeout(slideshow, img.ss_delay, true);
  287. return true;
  288. }
  289. }
  290. return false;
  291. }
  292. bool i_adjust_slideshow(arg_t a) {
  293. long d = (long) a;
  294. int i, delays[] = { 1, 2, 3, 5, 10, 15, 20, 30, 60, 120, 180, 300, 600 };
  295. if (mode != MODE_IMAGE || !img.slideshow)
  296. return false;
  297. if (d < 0) {
  298. for (i = ARRLEN(delays) - 2; i >= 0; i--) {
  299. if (img.ss_delay > delays[i] * 1000) {
  300. img.ss_delay = delays[i] * 1000;
  301. return true;
  302. }
  303. }
  304. } else {
  305. for (i = 1; i < ARRLEN(delays); i++) {
  306. if (img.ss_delay < delays[i] * 1000) {
  307. img.ss_delay = delays[i] * 1000;
  308. return true;
  309. }
  310. }
  311. }
  312. return false;
  313. }
  314. bool i_toggle_antialias(arg_t a) {
  315. if (mode == MODE_IMAGE) {
  316. img_toggle_antialias(&img);
  317. return true;
  318. } else {
  319. return false;
  320. }
  321. }
  322. bool it_toggle_alpha(arg_t a) {
  323. img.alpha = tns.alpha = !img.alpha;
  324. if (mode == MODE_IMAGE)
  325. img.dirty = true;
  326. else
  327. tns.dirty = true;
  328. return true;
  329. }
  330. bool it_open_with(arg_t a) {
  331. const char *prog = (const char*) a;
  332. pid_t pid;
  333. if (prog == NULL || *prog == '\0')
  334. return false;
  335. if ((pid = fork()) == 0) {
  336. execlp(prog, prog,
  337. files[mode == MODE_IMAGE ? fileidx : tns.sel].path, NULL);
  338. warn("could not exec: %s", prog);
  339. exit(EXIT_FAILURE);
  340. } else if (pid < 0) {
  341. warn("could not fork. program was: %s", prog);
  342. }
  343. return false;
  344. }
  345. bool it_shell_cmd(arg_t a) {
  346. int n, status;
  347. const char *cmdline = (const char*) a;
  348. pid_t pid;
  349. if (cmdline == NULL || *cmdline == '\0')
  350. return false;
  351. n = mode == MODE_IMAGE ? fileidx : tns.sel;
  352. if (setenv("SXIV_IMG", files[n].path, 1) < 0) {
  353. warn("could not set env.-variable: SXIV_IMG. command line was: %s",
  354. cmdline);
  355. return false;
  356. }
  357. if ((pid = fork()) == 0) {
  358. execl("/bin/sh", "/bin/sh", "-c", cmdline, NULL);
  359. warn("could not exec: /bin/sh. command line was: %s", cmdline);
  360. exit(EXIT_FAILURE);
  361. } else if (pid < 0) {
  362. warn("could not fork. command line was: %s", cmdline);
  363. return false;
  364. }
  365. win_set_cursor(&win, CURSOR_WATCH);
  366. waitpid(pid, &status, 0);
  367. if (WIFEXITED(status) == 0 || WEXITSTATUS(status) != 0)
  368. warn("child exited with non-zero return value: %d. command line was: %s",
  369. WEXITSTATUS(status), cmdline);
  370. if (mode == MODE_IMAGE) {
  371. img_close(&img, true);
  372. load_image(fileidx);
  373. }
  374. if (!tns_load(&tns, n, &files[n], true, mode == MODE_IMAGE) &&
  375. mode == MODE_THUMB)
  376. {
  377. remove_file(tns.sel, false);
  378. tns.dirty = true;
  379. if (tns.sel >= tns.cnt)
  380. tns.sel = tns.cnt - 1;
  381. }
  382. return true;
  383. }