A Simple X Image Viewer
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

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