A Simple X Image Viewer
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

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