A Simple X Image Viewer
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

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