A Simple X Image Viewer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

488 lines
8.7 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 a)
  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 a)
  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 a)
  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 a)
  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 a)
  105. {
  106. extprefix = true;
  107. return false;
  108. }
  109. bool cg_reload_image(arg_t a)
  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 a)
  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 a)
  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 a)
  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 a)
  159. {
  160. direction_t dir = (direction_t) a;
  161. if (mode == MODE_IMAGE)
  162. return img_pan(&img, dir, -1);
  163. else
  164. return tns_scroll(&tns, dir, true);
  165. }
  166. bool cg_zoom(arg_t a)
  167. {
  168. long d = (long) a;
  169. if (mode == MODE_THUMB)
  170. return tns_zoom(&tns, d);
  171. else if (d > 0)
  172. return img_zoom_in(&img);
  173. else if (d < 0)
  174. return img_zoom_out(&img);
  175. else
  176. return false;
  177. }
  178. bool cg_toggle_image_mark(arg_t a)
  179. {
  180. files[fileidx].flags ^= FF_MARK;
  181. markcnt += files[fileidx].flags & FF_MARK ? 1 : -1;
  182. if (mode == MODE_THUMB)
  183. tns_mark(&tns, fileidx, !!(files[fileidx].flags & FF_MARK));
  184. return true;
  185. }
  186. bool cg_reverse_marks(arg_t a)
  187. {
  188. int i;
  189. for (i = 0; i < filecnt; i++) {
  190. files[i].flags ^= FF_MARK;
  191. markcnt += files[i].flags & FF_MARK ? 1 : -1;
  192. }
  193. if (mode == MODE_THUMB)
  194. tns.dirty = true;
  195. return true;
  196. }
  197. bool cg_unmark_all(arg_t a)
  198. {
  199. int i;
  200. for (i = 0; i < filecnt; i++)
  201. files[i].flags &= ~FF_MARK;
  202. markcnt = 0;
  203. if (mode == MODE_THUMB)
  204. tns.dirty = true;
  205. return true;
  206. }
  207. bool cg_navigate_marked(arg_t a)
  208. {
  209. long n = (long) a;
  210. int d, i;
  211. int new = fileidx;
  212. if (prefix > 0)
  213. n *= prefix;
  214. d = n > 0 ? 1 : -1;
  215. for (i = fileidx + d; n != 0 && i >= 0 && i < filecnt; i += d) {
  216. if (files[i].flags & FF_MARK) {
  217. n -= d;
  218. new = i;
  219. }
  220. }
  221. if (new != fileidx) {
  222. if (mode == MODE_IMAGE) {
  223. load_image(new);
  224. } else {
  225. fileidx = new;
  226. tns.dirty = true;
  227. }
  228. return true;
  229. } else {
  230. return false;
  231. }
  232. }
  233. bool cg_change_gamma(arg_t a)
  234. {
  235. if (img_change_gamma(&img, (long) a * (prefix > 0 ? prefix : 1))) {
  236. if (mode == MODE_THUMB)
  237. tns.dirty = true;
  238. return true;
  239. } else {
  240. return false;
  241. }
  242. }
  243. bool ci_navigate(arg_t a)
  244. {
  245. long n = (long) a;
  246. if (prefix > 0)
  247. n *= prefix;
  248. n += fileidx;
  249. if (n < 0)
  250. n = 0;
  251. if (n >= filecnt)
  252. n = filecnt - 1;
  253. if (n != fileidx) {
  254. load_image(n);
  255. return true;
  256. } else {
  257. return false;
  258. }
  259. }
  260. bool ci_alternate(arg_t a)
  261. {
  262. load_image(alternate);
  263. return true;
  264. }
  265. bool ci_navigate_frame(arg_t a)
  266. {
  267. return !img.multi.animate && img_frame_navigate(&img, (long) a);
  268. }
  269. bool ci_toggle_animation(arg_t a)
  270. {
  271. bool dirty = false;
  272. if (img.multi.cnt > 0) {
  273. img.multi.animate = !img.multi.animate;
  274. if (img.multi.animate) {
  275. dirty = img_frame_animate(&img);
  276. set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
  277. } else {
  278. reset_timeout(animate);
  279. }
  280. }
  281. return dirty;
  282. }
  283. bool ci_scroll(arg_t a)
  284. {
  285. direction_t dir = (direction_t) a;
  286. return img_pan(&img, dir, prefix);
  287. }
  288. bool ci_scroll_to_edge(arg_t a)
  289. {
  290. direction_t dir = (direction_t) a;
  291. return img_pan_edge(&img, dir);
  292. }
  293. /* Xlib helper function for i_drag() */
  294. Bool is_motionnotify(Display *d, XEvent *e, XPointer a)
  295. {
  296. return e != NULL && e->type == MotionNotify;
  297. }
  298. #define WARP(x,y) \
  299. XWarpPointer(win.env.dpy, None, win.xwin, 0, 0, 0, 0, x, y); \
  300. ox = x, oy = y; \
  301. break
  302. bool ci_drag(arg_t a)
  303. {
  304. int dx = 0, dy = 0, i, ox, oy, x, y;
  305. unsigned int ui;
  306. bool dragging = true, next = false;
  307. XEvent e;
  308. Window w;
  309. if (!XQueryPointer(win.env.dpy, win.xwin, &w, &w, &i, &i, &ox, &oy, &ui))
  310. return false;
  311. win_set_cursor(&win, CURSOR_HAND);
  312. while (dragging) {
  313. if (!next)
  314. XMaskEvent(win.env.dpy,
  315. ButtonPressMask | ButtonReleaseMask | PointerMotionMask, &e);
  316. switch (e.type) {
  317. case ButtonPress:
  318. case ButtonRelease:
  319. dragging = false;
  320. break;
  321. case MotionNotify:
  322. x = e.xmotion.x;
  323. y = e.xmotion.y;
  324. /* wrap the mouse around */
  325. if (x <= 0) {
  326. WARP(win.w - 2, y);
  327. } else if (x >= win.w - 1) {
  328. WARP(1, y);
  329. } else if (y <= 0) {
  330. WARP(x, win.h - 2);
  331. } else if (y >= win.h - 1) {
  332. WARP(x, 1);
  333. }
  334. dx += x - ox;
  335. dy += y - oy;
  336. ox = x;
  337. oy = y;
  338. break;
  339. }
  340. if (dragging)
  341. next = XCheckIfEvent(win.env.dpy, &e, is_motionnotify, None);
  342. if ((!dragging || !next) && (dx != 0 || dy != 0)) {
  343. if (img_move(&img, dx, dy)) {
  344. img_render(&img);
  345. win_draw(&win);
  346. }
  347. dx = dy = 0;
  348. }
  349. }
  350. win_set_cursor(&win, CURSOR_ARROW);
  351. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  352. reset_timeout(redraw);
  353. return true;
  354. }
  355. bool ci_set_zoom(arg_t a)
  356. {
  357. return img_zoom(&img, (prefix ? prefix : (long) a) / 100.0);
  358. }
  359. bool ci_fit_to_win(arg_t a)
  360. {
  361. scalemode_t sm = (scalemode_t) a;
  362. return img_fit_win(&img, sm);
  363. }
  364. bool ci_rotate(arg_t a)
  365. {
  366. degree_t degree = (degree_t) a;
  367. img_rotate(&img, degree);
  368. return true;
  369. }
  370. bool ci_flip(arg_t a)
  371. {
  372. flipdir_t dir = (flipdir_t) a;
  373. img_flip(&img, dir);
  374. return true;
  375. }
  376. bool ci_toggle_antialias(arg_t a)
  377. {
  378. img_toggle_antialias(&img);
  379. return true;
  380. }
  381. bool ci_toggle_alpha(arg_t a)
  382. {
  383. img.alpha = !img.alpha;
  384. img.dirty = true;
  385. return true;
  386. }
  387. bool ci_slideshow(arg_t a)
  388. {
  389. if (prefix > 0) {
  390. img.ss.on = true;
  391. img.ss.delay = prefix;
  392. set_timeout(slideshow, img.ss.delay * 1000, true);
  393. } else if (img.ss.on) {
  394. img.ss.on = false;
  395. reset_timeout(slideshow);
  396. } else {
  397. img.ss.on = true;
  398. }
  399. return true;
  400. }
  401. bool ct_move_sel(arg_t a)
  402. {
  403. direction_t dir = (direction_t) a;
  404. return tns_move_selection(&tns, dir, prefix);
  405. }
  406. bool ct_reload_all(arg_t a)
  407. {
  408. tns_free(&tns);
  409. tns_init(&tns, files, &filecnt, &fileidx, &win);
  410. tns.dirty = true;
  411. return true;
  412. }
  413. #undef G_CMD
  414. #define G_CMD(c) { -1, cg_##c },
  415. #undef I_CMD
  416. #define I_CMD(c) { MODE_IMAGE, ci_##c },
  417. #undef T_CMD
  418. #define T_CMD(c) { MODE_THUMB, ct_##c },
  419. const cmd_t cmds[CMD_COUNT] = {
  420. #include "commands.lst"
  421. };