A Simple X Image Viewer
 
 
 
 
 
 

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