A Simple X Image Viewer
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

commands.c 9.0 KiB

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