A Simple X Image Viewer
 
 
 
 
 
 

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