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.
 
 
 
 
 
 

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