A Simple X Image Viewer
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

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