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

436 строки
7.8 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. #include "sxiv.h"
  19. #define _IMAGE_CONFIG
  20. #include "config.h"
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include <sys/wait.h>
  25. void remove_file(int, bool);
  26. void load_image(int);
  27. void open_info(void);
  28. int ptr_third_x(void);
  29. void redraw(void);
  30. void reset_cursor(void);
  31. void animate(void);
  32. void slideshow(void);
  33. void set_timeout(timeout_f, int, bool);
  34. void reset_timeout(timeout_f);
  35. extern appmode_t mode;
  36. extern img_t img;
  37. extern tns_t tns;
  38. extern win_t win;
  39. extern fileinfo_t *files;
  40. extern int filecnt, fileidx;
  41. extern int alternate;
  42. extern int markcnt;
  43. extern int prefix;
  44. extern bool extprefix;
  45. bool cg_quit(arg_t _)
  46. {
  47. unsigned int i;
  48. if (options->to_stdout && markcnt > 0) {
  49. for (i = 0; i < filecnt; i++) {
  50. if (files[i].flags & FF_MARK)
  51. printf("%s\n", files[i].name);
  52. }
  53. }
  54. exit(EXIT_SUCCESS);
  55. }
  56. bool cg_switch_mode(arg_t _)
  57. {
  58. if (mode == MODE_IMAGE) {
  59. if (tns.thumbs == NULL)
  60. tns_init(&tns, files, &filecnt, &fileidx, &win);
  61. img_close(&img, false);
  62. reset_timeout(reset_cursor);
  63. if (img.ss.on) {
  64. img.ss.on = false;
  65. reset_timeout(slideshow);
  66. }
  67. tns.dirty = true;
  68. mode = MODE_THUMB;
  69. } else {
  70. load_image(fileidx);
  71. mode = MODE_IMAGE;
  72. }
  73. return true;
  74. }
  75. bool cg_toggle_fullscreen(arg_t _)
  76. {
  77. win_toggle_fullscreen(&win);
  78. /* redraw after next ConfigureNotify event */
  79. set_timeout(redraw, TO_REDRAW_RESIZE, false);
  80. if (mode == MODE_IMAGE)
  81. img.checkpan = img.dirty = true;
  82. else
  83. tns.dirty = true;
  84. return false;
  85. }
  86. bool cg_toggle_bar(arg_t _)
  87. {
  88. win_toggle_bar(&win);
  89. if (mode == MODE_IMAGE) {
  90. img.checkpan = img.dirty = true;
  91. if (win.bar.h > 0)
  92. open_info();
  93. } else {
  94. tns.dirty = true;
  95. }
  96. return true;
  97. }
  98. bool cg_prefix_external(arg_t _)
  99. {
  100. extprefix = true;
  101. return false;
  102. }
  103. bool cg_reload_image(arg_t _)
  104. {
  105. if (mode == MODE_IMAGE) {
  106. load_image(fileidx);
  107. } else {
  108. win_set_cursor(&win, CURSOR_WATCH);
  109. if (!tns_load(&tns, fileidx, true, false)) {
  110. remove_file(fileidx, false);
  111. tns.dirty = true;
  112. }
  113. }
  114. return true;
  115. }
  116. bool cg_remove_image(arg_t _)
  117. {
  118. remove_file(fileidx, true);
  119. if (mode == MODE_IMAGE)
  120. load_image(fileidx);
  121. else
  122. tns.dirty = true;
  123. return true;
  124. }
  125. bool cg_first(arg_t _)
  126. {
  127. if (mode == MODE_IMAGE && fileidx != 0) {
  128. load_image(0);
  129. return true;
  130. } else if (mode == MODE_THUMB && fileidx != 0) {
  131. fileidx = 0;
  132. tns.dirty = true;
  133. return true;
  134. } else {
  135. return false;
  136. }
  137. }
  138. bool cg_n_or_last(arg_t _)
  139. {
  140. int n = prefix != 0 && prefix - 1 < filecnt ? prefix - 1 : filecnt - 1;
  141. if (mode == MODE_IMAGE && fileidx != n) {
  142. load_image(n);
  143. return true;
  144. } else if (mode == MODE_THUMB && fileidx != n) {
  145. fileidx = n;
  146. tns.dirty = true;
  147. return true;
  148. } else {
  149. return false;
  150. }
  151. }
  152. bool cg_scroll_screen(arg_t dir)
  153. {
  154. if (mode == MODE_IMAGE)
  155. return img_pan(&img, dir, -1);
  156. else
  157. return tns_scroll(&tns, dir, true);
  158. }
  159. bool cg_zoom(arg_t d)
  160. {
  161. if (mode == MODE_THUMB)
  162. return tns_zoom(&tns, d);
  163. else if (d > 0)
  164. return img_zoom_in(&img);
  165. else if (d < 0)
  166. return img_zoom_out(&img);
  167. else
  168. return false;
  169. }
  170. bool cg_toggle_image_mark(arg_t _)
  171. {
  172. files[fileidx].flags ^= FF_MARK;
  173. markcnt += files[fileidx].flags & FF_MARK ? 1 : -1;
  174. if (mode == MODE_THUMB)
  175. tns_mark(&tns, fileidx, !!(files[fileidx].flags & FF_MARK));
  176. return true;
  177. }
  178. bool cg_reverse_marks(arg_t _)
  179. {
  180. int i;
  181. for (i = 0; i < filecnt; i++) {
  182. files[i].flags ^= FF_MARK;
  183. markcnt += files[i].flags & FF_MARK ? 1 : -1;
  184. }
  185. if (mode == MODE_THUMB)
  186. tns.dirty = true;
  187. return true;
  188. }
  189. bool cg_unmark_all(arg_t _)
  190. {
  191. int i;
  192. for (i = 0; i < filecnt; i++)
  193. files[i].flags &= ~FF_MARK;
  194. markcnt = 0;
  195. if (mode == MODE_THUMB)
  196. tns.dirty = true;
  197. return true;
  198. }
  199. bool cg_navigate_marked(arg_t n)
  200. {
  201. int d, i;
  202. int new = fileidx;
  203. if (prefix > 0)
  204. n *= prefix;
  205. d = n > 0 ? 1 : -1;
  206. for (i = fileidx + d; n != 0 && i >= 0 && i < filecnt; i += d) {
  207. if (files[i].flags & FF_MARK) {
  208. n -= d;
  209. new = i;
  210. }
  211. }
  212. if (new != fileidx) {
  213. if (mode == MODE_IMAGE) {
  214. load_image(new);
  215. } else {
  216. fileidx = new;
  217. tns.dirty = true;
  218. }
  219. return true;
  220. } else {
  221. return false;
  222. }
  223. }
  224. bool cg_change_gamma(arg_t d)
  225. {
  226. if (img_change_gamma(&img, d * (prefix > 0 ? prefix : 1))) {
  227. if (mode == MODE_THUMB)
  228. tns.dirty = true;
  229. return true;
  230. } else {
  231. return false;
  232. }
  233. }
  234. bool ci_navigate(arg_t n)
  235. {
  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_cursor_navigate(arg_t _)
  251. {
  252. return ci_navigate(ptr_third_x() - 1);
  253. }
  254. bool ci_alternate(arg_t _)
  255. {
  256. load_image(alternate);
  257. return true;
  258. }
  259. bool ci_navigate_frame(arg_t d)
  260. {
  261. if (prefix > 0)
  262. d *= prefix;
  263. return !img.multi.animate && img_frame_navigate(&img, d);
  264. }
  265. bool ci_toggle_animation(arg_t _)
  266. {
  267. bool dirty = false;
  268. if (img.multi.cnt > 0) {
  269. img.multi.animate = !img.multi.animate;
  270. if (img.multi.animate) {
  271. dirty = img_frame_animate(&img);
  272. set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
  273. } else {
  274. reset_timeout(animate);
  275. }
  276. }
  277. return dirty;
  278. }
  279. bool ci_scroll(arg_t dir)
  280. {
  281. return img_pan(&img, dir, prefix);
  282. }
  283. bool ci_scroll_to_edge(arg_t dir)
  284. {
  285. return img_pan_edge(&img, dir);
  286. }
  287. bool ci_drag(arg_t _)
  288. {
  289. int x, y;
  290. float px, py;
  291. XEvent e;
  292. if ((int)(img.w * img.zoom) <= win.w && (int)(img.h * img.zoom) <= win.h)
  293. return false;
  294. win_set_cursor(&win, CURSOR_DRAG);
  295. win_cursor_pos(&win, &x, &y);
  296. for (;;) {
  297. px = MIN(MAX(0.0, x - win.w*0.1), win.w*0.8) / (win.w*0.8)
  298. * (win.w - img.w * img.zoom);
  299. py = MIN(MAX(0.0, y - win.h*0.1), win.h*0.8) / (win.h*0.8)
  300. * (win.h - img.h * img.zoom);
  301. if (img_pos(&img, px, py)) {
  302. img_render(&img);
  303. win_draw(&win);
  304. }
  305. XMaskEvent(win.env.dpy,
  306. ButtonPressMask | ButtonReleaseMask | PointerMotionMask, &e);
  307. if (e.type == ButtonPress || e.type == ButtonRelease)
  308. break;
  309. while (XCheckTypedEvent(win.env.dpy, MotionNotify, &e));
  310. x = e.xmotion.x;
  311. y = e.xmotion.y;
  312. }
  313. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  314. reset_cursor();
  315. return true;
  316. }
  317. bool ci_set_zoom(arg_t zl)
  318. {
  319. return img_zoom(&img, (prefix ? prefix : zl) / 100.0);
  320. }
  321. bool ci_fit_to_win(arg_t sm)
  322. {
  323. return img_fit_win(&img, sm);
  324. }
  325. bool ci_rotate(arg_t degree)
  326. {
  327. img_rotate(&img, degree);
  328. return true;
  329. }
  330. bool ci_flip(arg_t dir)
  331. {
  332. img_flip(&img, dir);
  333. return true;
  334. }
  335. bool ci_toggle_antialias(arg_t _)
  336. {
  337. img_toggle_antialias(&img);
  338. return true;
  339. }
  340. bool ci_toggle_alpha(arg_t _)
  341. {
  342. img.alpha = !img.alpha;
  343. img.dirty = true;
  344. return true;
  345. }
  346. bool ci_slideshow(arg_t _)
  347. {
  348. if (prefix > 0) {
  349. img.ss.on = true;
  350. img.ss.delay = prefix * 10;
  351. set_timeout(slideshow, img.ss.delay * 100, true);
  352. } else if (img.ss.on) {
  353. img.ss.on = false;
  354. reset_timeout(slideshow);
  355. } else {
  356. img.ss.on = true;
  357. }
  358. return true;
  359. }
  360. bool ct_move_sel(arg_t dir)
  361. {
  362. return tns_move_selection(&tns, dir, prefix);
  363. }
  364. bool ct_reload_all(arg_t _)
  365. {
  366. tns_free(&tns);
  367. tns_init(&tns, files, &filecnt, &fileidx, &win);
  368. tns.dirty = true;
  369. return true;
  370. }
  371. #undef G_CMD
  372. #define G_CMD(c) { -1, cg_##c },
  373. #undef I_CMD
  374. #define I_CMD(c) { MODE_IMAGE, ci_##c },
  375. #undef T_CMD
  376. #define T_CMD(c) { MODE_THUMB, ct_##c },
  377. const cmd_t cmds[CMD_COUNT] = {
  378. #include "commands.lst"
  379. };