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.
 
 
 
 
 
 

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