A Simple X Image Viewer
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

441 líneas
7.9 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 <stdlib.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <sys/wait.h>
  22. #include "commands.h"
  23. #include "image.h"
  24. #include "options.h"
  25. #include "thumbs.h"
  26. #include "util.h"
  27. #define _IMAGE_CONFIG
  28. #include "config.h"
  29. void remove_file(int, bool);
  30. void load_image(int);
  31. void open_info(void);
  32. int ptr_third_x(void);
  33. void redraw(void);
  34. void reset_cursor(void);
  35. void animate(void);
  36. void slideshow(void);
  37. void set_timeout(timeout_f, int, bool);
  38. void reset_timeout(timeout_f);
  39. extern appmode_t mode;
  40. extern img_t img;
  41. extern tns_t tns;
  42. extern win_t win;
  43. extern fileinfo_t *files;
  44. extern int filecnt, fileidx;
  45. extern int alternate;
  46. extern int markcnt;
  47. extern int prefix;
  48. extern bool extprefix;
  49. bool cg_quit(arg_t _)
  50. {
  51. unsigned int i;
  52. if (options->to_stdout && markcnt > 0) {
  53. for (i = 0; i < filecnt; i++) {
  54. if (files[i].flags & FF_MARK)
  55. printf("%s\n", files[i].name);
  56. }
  57. }
  58. exit(EXIT_SUCCESS);
  59. }
  60. bool cg_switch_mode(arg_t _)
  61. {
  62. if (mode == MODE_IMAGE) {
  63. if (tns.thumbs == NULL)
  64. tns_init(&tns, files, &filecnt, &fileidx, &win);
  65. img_close(&img, false);
  66. reset_timeout(reset_cursor);
  67. if (img.ss.on) {
  68. img.ss.on = false;
  69. reset_timeout(slideshow);
  70. }
  71. tns.dirty = true;
  72. mode = MODE_THUMB;
  73. } else {
  74. load_image(fileidx);
  75. mode = MODE_IMAGE;
  76. }
  77. return true;
  78. }
  79. bool cg_toggle_fullscreen(arg_t _)
  80. {
  81. win_toggle_fullscreen(&win);
  82. /* redraw after next ConfigureNotify event */
  83. set_timeout(redraw, TO_REDRAW_RESIZE, false);
  84. if (mode == MODE_IMAGE)
  85. img.checkpan = img.dirty = true;
  86. else
  87. tns.dirty = true;
  88. return false;
  89. }
  90. bool cg_toggle_bar(arg_t _)
  91. {
  92. win_toggle_bar(&win);
  93. if (mode == MODE_IMAGE) {
  94. img.checkpan = img.dirty = true;
  95. if (win.bar.h > 0)
  96. open_info();
  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. return true;
  181. }
  182. bool cg_reverse_marks(arg_t _)
  183. {
  184. int i;
  185. for (i = 0; i < filecnt; i++) {
  186. files[i].flags ^= FF_MARK;
  187. markcnt += files[i].flags & FF_MARK ? 1 : -1;
  188. }
  189. if (mode == MODE_THUMB)
  190. tns.dirty = true;
  191. return true;
  192. }
  193. bool cg_unmark_all(arg_t _)
  194. {
  195. int i;
  196. for (i = 0; i < filecnt; i++)
  197. files[i].flags &= ~FF_MARK;
  198. markcnt = 0;
  199. if (mode == MODE_THUMB)
  200. tns.dirty = true;
  201. return true;
  202. }
  203. bool cg_navigate_marked(arg_t n)
  204. {
  205. int d, i;
  206. int new = fileidx;
  207. if (prefix > 0)
  208. n *= prefix;
  209. d = n > 0 ? 1 : -1;
  210. for (i = fileidx + d; n != 0 && i >= 0 && i < filecnt; i += d) {
  211. if (files[i].flags & FF_MARK) {
  212. n -= d;
  213. new = i;
  214. }
  215. }
  216. if (new != fileidx) {
  217. if (mode == MODE_IMAGE) {
  218. load_image(new);
  219. } else {
  220. fileidx = new;
  221. tns.dirty = true;
  222. }
  223. return true;
  224. } else {
  225. return false;
  226. }
  227. }
  228. bool cg_change_gamma(arg_t d)
  229. {
  230. if (img_change_gamma(&img, d * (prefix > 0 ? prefix : 1))) {
  231. if (mode == MODE_THUMB)
  232. tns.dirty = true;
  233. return true;
  234. } else {
  235. return false;
  236. }
  237. }
  238. bool ci_navigate(arg_t n)
  239. {
  240. if (prefix > 0)
  241. n *= prefix;
  242. n += fileidx;
  243. if (n < 0)
  244. n = 0;
  245. if (n >= filecnt)
  246. n = filecnt - 1;
  247. if (n != fileidx) {
  248. load_image(n);
  249. return true;
  250. } else {
  251. return false;
  252. }
  253. }
  254. bool ci_cursor_navigate(arg_t _)
  255. {
  256. return ci_navigate(ptr_third_x() - 1);
  257. }
  258. bool ci_alternate(arg_t _)
  259. {
  260. load_image(alternate);
  261. return true;
  262. }
  263. bool ci_navigate_frame(arg_t d)
  264. {
  265. if (prefix > 0)
  266. d *= prefix;
  267. return !img.multi.animate && img_frame_navigate(&img, d);
  268. }
  269. bool ci_toggle_animation(arg_t _)
  270. {
  271. bool dirty = false;
  272. if (img.multi.cnt > 0) {
  273. img.multi.animate = !img.multi.animate;
  274. if (img.multi.animate) {
  275. dirty = img_frame_animate(&img);
  276. set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
  277. } else {
  278. reset_timeout(animate);
  279. }
  280. }
  281. return dirty;
  282. }
  283. bool ci_scroll(arg_t dir)
  284. {
  285. return img_pan(&img, dir, prefix);
  286. }
  287. bool ci_scroll_to_edge(arg_t dir)
  288. {
  289. return img_pan_edge(&img, dir);
  290. }
  291. bool ci_drag(arg_t _)
  292. {
  293. int x, y;
  294. float px, py;
  295. XEvent e;
  296. if ((int)(img.w * img.zoom) <= win.w && (int)(img.h * img.zoom) <= win.h)
  297. return false;
  298. win_set_cursor(&win, CURSOR_DRAG);
  299. win_cursor_pos(&win, &x, &y);
  300. for (;;) {
  301. px = MIN(MAX(0.0, x - win.w*0.1), win.w*0.8) / (win.w*0.8)
  302. * (win.w - img.w * img.zoom);
  303. py = MIN(MAX(0.0, y - win.h*0.1), win.h*0.8) / (win.h*0.8)
  304. * (win.h - img.h * img.zoom);
  305. if (img_pos(&img, px, py)) {
  306. img_render(&img);
  307. win_draw(&win);
  308. }
  309. XMaskEvent(win.env.dpy,
  310. ButtonPressMask | ButtonReleaseMask | PointerMotionMask, &e);
  311. if (e.type == ButtonPress || e.type == ButtonRelease)
  312. break;
  313. while (XCheckTypedEvent(win.env.dpy, MotionNotify, &e));
  314. x = e.xmotion.x;
  315. y = e.xmotion.y;
  316. }
  317. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  318. reset_cursor();
  319. return true;
  320. }
  321. bool ci_set_zoom(arg_t zl)
  322. {
  323. return img_zoom(&img, (prefix ? prefix : zl) / 100.0);
  324. }
  325. bool ci_fit_to_win(arg_t sm)
  326. {
  327. return img_fit_win(&img, sm);
  328. }
  329. bool ci_rotate(arg_t degree)
  330. {
  331. img_rotate(&img, degree);
  332. return true;
  333. }
  334. bool ci_flip(arg_t dir)
  335. {
  336. img_flip(&img, dir);
  337. return true;
  338. }
  339. bool ci_toggle_antialias(arg_t _)
  340. {
  341. img_toggle_antialias(&img);
  342. return true;
  343. }
  344. bool ci_toggle_alpha(arg_t _)
  345. {
  346. img.alpha = !img.alpha;
  347. img.dirty = true;
  348. return true;
  349. }
  350. bool ci_slideshow(arg_t _)
  351. {
  352. if (prefix > 0) {
  353. img.ss.on = true;
  354. img.ss.delay = prefix * 10;
  355. set_timeout(slideshow, img.ss.delay * 100, true);
  356. } else if (img.ss.on) {
  357. img.ss.on = false;
  358. reset_timeout(slideshow);
  359. } else {
  360. img.ss.on = true;
  361. }
  362. return true;
  363. }
  364. bool ct_move_sel(arg_t dir)
  365. {
  366. return tns_move_selection(&tns, dir, prefix);
  367. }
  368. bool ct_reload_all(arg_t _)
  369. {
  370. tns_free(&tns);
  371. tns_init(&tns, files, &filecnt, &fileidx, &win);
  372. tns.dirty = true;
  373. return true;
  374. }
  375. #undef G_CMD
  376. #define G_CMD(c) { -1, cg_##c },
  377. #undef I_CMD
  378. #define I_CMD(c) { MODE_IMAGE, ci_##c },
  379. #undef T_CMD
  380. #define T_CMD(c) { MODE_THUMB, ct_##c },
  381. const cmd_t cmds[CMD_COUNT] = {
  382. #include "commands.lst"
  383. };