A Simple X Image Viewer
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

471 lignes
8.5 KiB

  1. /* Copyright 2011, 2012, 2014 Bert Muennich
  2. *
  3. * This file is part of sxiv.
  4. *
  5. * sxiv is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published
  7. * by the Free Software Foundation; either version 2 of the License,
  8. * or (at your option) any later version.
  9. *
  10. * sxiv is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with sxiv. If not, see <http://www.gnu.org/licenses/>.
  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 "options.h"
  27. #include "thumbs.h"
  28. #include "util.h"
  29. #include "config.h"
  30. void cleanup(void);
  31. void remove_file(int, bool);
  32. void load_image(int);
  33. void open_info(void);
  34. void redraw(void);
  35. void reset_cursor(void);
  36. void animate(void);
  37. void slideshow(void);
  38. void set_timeout(timeout_f, int, bool);
  39. void reset_timeout(timeout_f);
  40. extern appmode_t mode;
  41. extern img_t img;
  42. extern tns_t tns;
  43. extern win_t win;
  44. extern fileinfo_t *files;
  45. extern int filecnt, fileidx;
  46. extern int alternate;
  47. extern int prefix;
  48. extern bool extprefix;
  49. const int ss_delays[] = {
  50. 1, 2, 3, 5, 10, 15, 20, 30, 60, 120, 180, 300, 600
  51. };
  52. bool cg_quit(arg_t a)
  53. {
  54. unsigned int i;
  55. if (options->to_stdout) {
  56. for (i = 0; i < filecnt; i++) {
  57. if (files[i].marked)
  58. printf("%s\n", files[i].name);
  59. }
  60. }
  61. cleanup();
  62. exit(EXIT_SUCCESS);
  63. }
  64. bool cg_switch_mode(arg_t a)
  65. {
  66. if (mode == MODE_IMAGE) {
  67. if (tns.thumbs == NULL)
  68. tns_init(&tns, filecnt, &win, &fileidx);
  69. img_close(&img, false);
  70. reset_timeout(reset_cursor);
  71. if (img.ss.on) {
  72. img.ss.on = false;
  73. reset_timeout(slideshow);
  74. }
  75. tns.dirty = true;
  76. mode = MODE_THUMB;
  77. } else {
  78. load_image(fileidx);
  79. mode = MODE_IMAGE;
  80. }
  81. return true;
  82. }
  83. bool cg_toggle_fullscreen(arg_t a)
  84. {
  85. win_toggle_fullscreen(&win);
  86. /* redraw after next ConfigureNotify event */
  87. set_timeout(redraw, TO_REDRAW_RESIZE, false);
  88. if (mode == MODE_IMAGE)
  89. img.checkpan = img.dirty = true;
  90. else
  91. tns.dirty = true;
  92. return false;
  93. }
  94. bool cg_toggle_bar(arg_t a)
  95. {
  96. win_toggle_bar(&win);
  97. if (mode == MODE_IMAGE) {
  98. img.checkpan = img.dirty = true;
  99. if (win.bar.h > 0)
  100. open_info();
  101. } else {
  102. tns.dirty = true;
  103. }
  104. return true;
  105. }
  106. bool cg_prefix_external(arg_t a)
  107. {
  108. extprefix = true;
  109. return false;
  110. }
  111. bool cg_reload_image(arg_t a)
  112. {
  113. if (mode == MODE_IMAGE) {
  114. load_image(fileidx);
  115. } else {
  116. win_set_cursor(&win, CURSOR_WATCH);
  117. if (!tns_load(&tns, fileidx, &files[fileidx], true, false)) {
  118. remove_file(fileidx, false);
  119. tns.dirty = true;
  120. }
  121. }
  122. return true;
  123. }
  124. bool cg_remove_image(arg_t a)
  125. {
  126. if (mode == MODE_IMAGE) {
  127. remove_file(fileidx, true);
  128. load_image(fileidx >= filecnt ? filecnt - 1 : fileidx);
  129. return true;
  130. } else if (fileidx < tns.cnt) {
  131. remove_file(fileidx, true);
  132. tns.dirty = true;
  133. return true;
  134. } else {
  135. return false;
  136. }
  137. }
  138. bool cg_first(arg_t a)
  139. {
  140. if (mode == MODE_IMAGE && fileidx != 0) {
  141. load_image(0);
  142. return true;
  143. } else if (mode == MODE_THUMB && fileidx != 0) {
  144. fileidx = 0;
  145. tns.dirty = true;
  146. return true;
  147. } else {
  148. return false;
  149. }
  150. }
  151. bool cg_n_or_last(arg_t a)
  152. {
  153. int n = prefix != 0 && prefix - 1 < filecnt ? prefix - 1 : filecnt - 1;
  154. if (mode == MODE_IMAGE && fileidx != n) {
  155. load_image(n);
  156. return true;
  157. } else if (mode == MODE_THUMB && fileidx != n) {
  158. fileidx = n;
  159. tns.dirty = true;
  160. return true;
  161. } else {
  162. return false;
  163. }
  164. }
  165. bool cg_scroll_screen(arg_t a)
  166. {
  167. direction_t dir = (direction_t) a;
  168. if (mode == MODE_IMAGE)
  169. return img_pan(&img, dir, -1);
  170. else
  171. return tns_scroll(&tns, dir, true);
  172. }
  173. bool cg_toggle_image_mark(arg_t a)
  174. {
  175. files[fileidx].marked = !files[fileidx].marked;
  176. if (mode == MODE_THUMB)
  177. tns_mark(&tns, fileidx, files[fileidx].marked);
  178. return true;
  179. }
  180. bool cg_reverse_marks(arg_t a)
  181. {
  182. int i, cnt = mode == MODE_IMAGE ? filecnt : tns.cnt;
  183. for (i = 0; i < cnt; i++)
  184. files[i].marked = !files[i].marked;
  185. if (mode == MODE_THUMB)
  186. tns.dirty = true;
  187. return true;
  188. }
  189. bool cg_navigate_marked(arg_t a)
  190. {
  191. long n = (long) a;
  192. int d, i;
  193. int cnt = mode == MODE_IMAGE ? filecnt : tns.cnt, new = fileidx;
  194. if (prefix > 0)
  195. n *= prefix;
  196. d = n > 0 ? 1 : -1;
  197. for (i = fileidx + d; n != 0 && i >= 0 && i < cnt; i += d) {
  198. if (files[i].marked) {
  199. n -= d;
  200. new = i;
  201. }
  202. }
  203. if (new != fileidx) {
  204. if (mode == MODE_IMAGE) {
  205. load_image(new);
  206. } else {
  207. fileidx = new;
  208. tns.dirty = true;
  209. }
  210. return true;
  211. } else {
  212. return false;
  213. }
  214. }
  215. bool ci_navigate(arg_t a)
  216. {
  217. long n = (long) a;
  218. if (prefix > 0)
  219. n *= prefix;
  220. n += fileidx;
  221. if (n < 0)
  222. n = 0;
  223. if (n >= filecnt)
  224. n = filecnt - 1;
  225. if (n != fileidx) {
  226. load_image(n);
  227. return true;
  228. } else {
  229. return false;
  230. }
  231. }
  232. bool ci_alternate(arg_t a)
  233. {
  234. load_image(alternate);
  235. return true;
  236. }
  237. bool ci_navigate_frame(arg_t a)
  238. {
  239. return !img.multi.animate && img_frame_navigate(&img, (long) a);
  240. }
  241. bool ci_toggle_animation(arg_t a)
  242. {
  243. bool dirty = false;
  244. img.multi.animate = !img.multi.animate;
  245. if (img.multi.animate) {
  246. dirty = img_frame_animate(&img, true);
  247. set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
  248. } else {
  249. reset_timeout(animate);
  250. }
  251. return dirty;
  252. }
  253. bool ci_scroll(arg_t a)
  254. {
  255. direction_t dir = (direction_t) a;
  256. return img_pan(&img, dir, prefix);
  257. }
  258. bool ci_scroll_to_edge(arg_t a)
  259. {
  260. direction_t dir = (direction_t) a;
  261. return img_pan_edge(&img, dir);
  262. }
  263. /* Xlib helper function for i_drag() */
  264. Bool is_motionnotify(Display *d, XEvent *e, XPointer a)
  265. {
  266. return e != NULL && e->type == MotionNotify;
  267. }
  268. #define WARP(x,y) \
  269. XWarpPointer(win.env.dpy, None, win.xwin, 0, 0, 0, 0, x, y); \
  270. ox = x, oy = y; \
  271. break
  272. bool ci_drag(arg_t a)
  273. {
  274. int dx = 0, dy = 0, i, ox, oy, x, y;
  275. unsigned int ui;
  276. bool dragging = true, next = false;
  277. XEvent e;
  278. Window w;
  279. if (!XQueryPointer(win.env.dpy, win.xwin, &w, &w, &i, &i, &ox, &oy, &ui))
  280. return false;
  281. win_set_cursor(&win, CURSOR_HAND);
  282. while (dragging) {
  283. if (!next)
  284. XMaskEvent(win.env.dpy,
  285. ButtonPressMask | ButtonReleaseMask | PointerMotionMask, &e);
  286. switch (e.type) {
  287. case ButtonPress:
  288. case ButtonRelease:
  289. dragging = false;
  290. break;
  291. case MotionNotify:
  292. x = e.xmotion.x;
  293. y = e.xmotion.y;
  294. /* wrap the mouse around */
  295. if (x <= 0) {
  296. WARP(win.w - 2, y);
  297. } else if (x >= win.w - 1) {
  298. WARP(1, y);
  299. } else if (y <= 0) {
  300. WARP(x, win.h - 2);
  301. } else if (y >= win.h - 1) {
  302. WARP(x, 1);
  303. }
  304. dx += x - ox;
  305. dy += y - oy;
  306. ox = x;
  307. oy = y;
  308. break;
  309. }
  310. if (dragging)
  311. next = XCheckIfEvent(win.env.dpy, &e, is_motionnotify, None);
  312. if ((!dragging || !next) && (dx != 0 || dy != 0)) {
  313. if (img_move(&img, dx, dy)) {
  314. img_render(&img);
  315. win_draw(&win);
  316. }
  317. dx = dy = 0;
  318. }
  319. }
  320. win_set_cursor(&win, CURSOR_ARROW);
  321. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  322. reset_timeout(redraw);
  323. return true;
  324. }
  325. bool ci_zoom(arg_t a)
  326. {
  327. long scale = (long) a;
  328. if (scale > 0)
  329. return img_zoom_in(&img);
  330. else if (scale < 0)
  331. return img_zoom_out(&img);
  332. else
  333. return false;
  334. }
  335. bool ci_set_zoom(arg_t a)
  336. {
  337. return img_zoom(&img, (prefix ? prefix : (long) a) / 100.0);
  338. }
  339. bool ci_fit_to_win(arg_t a)
  340. {
  341. scalemode_t sm = (scalemode_t) a;
  342. return img_fit_win(&img, sm);
  343. }
  344. bool ci_rotate(arg_t a)
  345. {
  346. degree_t degree = (degree_t) a;
  347. img_rotate(&img, degree);
  348. return true;
  349. }
  350. bool ci_flip(arg_t a)
  351. {
  352. flipdir_t dir = (flipdir_t) a;
  353. img_flip(&img, dir);
  354. return true;
  355. }
  356. bool ci_change_gamma(arg_t a)
  357. {
  358. return img_change_gamma(&img, (long) a);
  359. }
  360. bool ci_toggle_antialias(arg_t a)
  361. {
  362. img_toggle_antialias(&img);
  363. return true;
  364. }
  365. bool ci_toggle_alpha(arg_t a)
  366. {
  367. img.alpha = !img.alpha;
  368. img.dirty = true;
  369. return true;
  370. }
  371. bool ci_slideshow(arg_t a)
  372. {
  373. if (prefix > 0) {
  374. img.ss.on = true;
  375. img.ss.delay = prefix;
  376. set_timeout(slideshow, img.ss.delay * 1000, true);
  377. } else if (img.ss.on) {
  378. img.ss.on = false;
  379. reset_timeout(slideshow);
  380. } else {
  381. img.ss.on = true;
  382. }
  383. return true;
  384. }
  385. bool ct_move_sel(arg_t a)
  386. {
  387. direction_t dir = (direction_t) a;
  388. return tns_move_selection(&tns, dir, prefix);
  389. }
  390. bool ct_reload_all(arg_t a)
  391. {
  392. tns_free(&tns);
  393. tns_init(&tns, filecnt, &win, &fileidx);
  394. return false;
  395. }
  396. #undef G_CMD
  397. #define G_CMD(c) { -1, cg_##c },
  398. #undef I_CMD
  399. #define I_CMD(c) { MODE_IMAGE, ci_##c },
  400. #undef T_CMD
  401. #define T_CMD(c) { MODE_THUMB, ct_##c },
  402. const cmd_t cmds[CMD_COUNT] = {
  403. #include "commands.lst"
  404. };