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.
 
 
 
 
 
 

427 lines
8.7 KiB

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