A Simple X Image Viewer
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

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