A Simple X Image Viewer
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

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