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.
 
 
 
 
 
 

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