A Simple X Image Viewer
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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