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.
 
 
 
 
 
 

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