A Simple X Image Viewer
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

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