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.
 
 
 
 
 
 

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