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.
 
 
 
 
 
 

482 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. 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].marked)
  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)) {
  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].marked = !files[fileidx].marked;
  181. markcnt += files[fileidx].marked ? 1 : -1;
  182. if (mode == MODE_THUMB)
  183. tns_mark(&tns, fileidx, files[fileidx].marked);
  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].marked = !files[i].marked;
  191. markcnt += files[i].marked ? 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].marked = false;
  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].marked) {
  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 ci_navigate(arg_t a)
  234. {
  235. long n = (long) a;
  236. if (prefix > 0)
  237. n *= prefix;
  238. n += fileidx;
  239. if (n < 0)
  240. n = 0;
  241. if (n >= filecnt)
  242. n = filecnt - 1;
  243. if (n != fileidx) {
  244. load_image(n);
  245. return true;
  246. } else {
  247. return false;
  248. }
  249. }
  250. bool ci_alternate(arg_t a)
  251. {
  252. load_image(alternate);
  253. return true;
  254. }
  255. bool ci_navigate_frame(arg_t a)
  256. {
  257. return !img.multi.animate && img_frame_navigate(&img, (long) a);
  258. }
  259. bool ci_toggle_animation(arg_t a)
  260. {
  261. bool dirty = false;
  262. if (img.multi.cnt > 0) {
  263. img.multi.animate = !img.multi.animate;
  264. if (img.multi.animate) {
  265. dirty = img_frame_animate(&img);
  266. set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
  267. } else {
  268. reset_timeout(animate);
  269. }
  270. }
  271. return dirty;
  272. }
  273. bool ci_scroll(arg_t a)
  274. {
  275. direction_t dir = (direction_t) a;
  276. return img_pan(&img, dir, prefix);
  277. }
  278. bool ci_scroll_to_edge(arg_t a)
  279. {
  280. direction_t dir = (direction_t) a;
  281. return img_pan_edge(&img, dir);
  282. }
  283. /* Xlib helper function for i_drag() */
  284. Bool is_motionnotify(Display *d, XEvent *e, XPointer a)
  285. {
  286. return e != NULL && e->type == MotionNotify;
  287. }
  288. #define WARP(x,y) \
  289. XWarpPointer(win.env.dpy, None, win.xwin, 0, 0, 0, 0, x, y); \
  290. ox = x, oy = y; \
  291. break
  292. bool ci_drag(arg_t a)
  293. {
  294. int dx = 0, dy = 0, i, ox, oy, x, y;
  295. unsigned int ui;
  296. bool dragging = true, next = false;
  297. XEvent e;
  298. Window w;
  299. if (!XQueryPointer(win.env.dpy, win.xwin, &w, &w, &i, &i, &ox, &oy, &ui))
  300. return false;
  301. win_set_cursor(&win, CURSOR_HAND);
  302. while (dragging) {
  303. if (!next)
  304. XMaskEvent(win.env.dpy,
  305. ButtonPressMask | ButtonReleaseMask | PointerMotionMask, &e);
  306. switch (e.type) {
  307. case ButtonPress:
  308. case ButtonRelease:
  309. dragging = false;
  310. break;
  311. case MotionNotify:
  312. x = e.xmotion.x;
  313. y = e.xmotion.y;
  314. /* wrap the mouse around */
  315. if (x <= 0) {
  316. WARP(win.w - 2, y);
  317. } else if (x >= win.w - 1) {
  318. WARP(1, y);
  319. } else if (y <= 0) {
  320. WARP(x, win.h - 2);
  321. } else if (y >= win.h - 1) {
  322. WARP(x, 1);
  323. }
  324. dx += x - ox;
  325. dy += y - oy;
  326. ox = x;
  327. oy = y;
  328. break;
  329. }
  330. if (dragging)
  331. next = XCheckIfEvent(win.env.dpy, &e, is_motionnotify, None);
  332. if ((!dragging || !next) && (dx != 0 || dy != 0)) {
  333. if (img_move(&img, dx, dy)) {
  334. img_render(&img);
  335. win_draw(&win);
  336. }
  337. dx = dy = 0;
  338. }
  339. }
  340. win_set_cursor(&win, CURSOR_ARROW);
  341. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  342. reset_timeout(redraw);
  343. return true;
  344. }
  345. bool ci_set_zoom(arg_t a)
  346. {
  347. return img_zoom(&img, (prefix ? prefix : (long) a) / 100.0);
  348. }
  349. bool ci_fit_to_win(arg_t a)
  350. {
  351. scalemode_t sm = (scalemode_t) a;
  352. return img_fit_win(&img, sm);
  353. }
  354. bool ci_rotate(arg_t a)
  355. {
  356. degree_t degree = (degree_t) a;
  357. img_rotate(&img, degree);
  358. return true;
  359. }
  360. bool ci_flip(arg_t a)
  361. {
  362. flipdir_t dir = (flipdir_t) a;
  363. img_flip(&img, dir);
  364. return true;
  365. }
  366. bool ci_change_gamma(arg_t a)
  367. {
  368. return img_change_gamma(&img, (long) a);
  369. }
  370. bool ci_toggle_antialias(arg_t a)
  371. {
  372. img_toggle_antialias(&img);
  373. return true;
  374. }
  375. bool ci_toggle_alpha(arg_t a)
  376. {
  377. img.alpha = !img.alpha;
  378. img.dirty = true;
  379. return true;
  380. }
  381. bool ci_slideshow(arg_t a)
  382. {
  383. if (prefix > 0) {
  384. img.ss.on = true;
  385. img.ss.delay = prefix;
  386. set_timeout(slideshow, img.ss.delay * 1000, true);
  387. } else if (img.ss.on) {
  388. img.ss.on = false;
  389. reset_timeout(slideshow);
  390. } else {
  391. img.ss.on = true;
  392. }
  393. return true;
  394. }
  395. bool ct_move_sel(arg_t a)
  396. {
  397. direction_t dir = (direction_t) a;
  398. return tns_move_selection(&tns, dir, prefix);
  399. }
  400. bool ct_reload_all(arg_t a)
  401. {
  402. tns_free(&tns);
  403. tns_init(&tns, files, &filecnt, &fileidx, &win);
  404. tns.dirty = true;
  405. return true;
  406. }
  407. #undef G_CMD
  408. #define G_CMD(c) { -1, cg_##c },
  409. #undef I_CMD
  410. #define I_CMD(c) { MODE_IMAGE, ci_##c },
  411. #undef T_CMD
  412. #define T_CMD(c) { MODE_THUMB, ct_##c },
  413. const cmd_t cmds[CMD_COUNT] = {
  414. #include "commands.lst"
  415. };