A Simple X Image Viewer
 
 
 
 
 
 

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