You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

469 lines
8.6 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. // Kanon added
  38. bool ci_fit_to_win(arg_t sm);
  39. void run_key_handler(const char *key, unsigned int mask);
  40. extern appmode_t mode;
  41. extern img_t img;
  42. extern tns_t tns;
  43. extern win_t win;
  44. extern fileinfo_t *files;
  45. extern int filecnt, fileidx;
  46. extern int alternate;
  47. extern int markcnt;
  48. extern int markidx;
  49. extern int prefix;
  50. extern bool extprefix;
  51. bool cg_quit(arg_t _)
  52. {
  53. unsigned int i;
  54. if (options->to_stdout && markcnt > 0) {
  55. for (i = 0; i < filecnt; i++) {
  56. if (files[i].flags & FF_MARK)
  57. printf("%s\n", files[i].name);
  58. }
  59. }
  60. exit(EXIT_SUCCESS);
  61. }
  62. bool cg_switch_mode(arg_t _)
  63. {
  64. if (mode == MODE_IMAGE) {
  65. if (tns.thumbs == NULL)
  66. tns_init(&tns, files, &filecnt, &fileidx, &win);
  67. img_close(&img, false);
  68. reset_timeout(reset_cursor);
  69. if (img.ss.on) {
  70. img.ss.on = false;
  71. reset_timeout(slideshow);
  72. }
  73. tns.dirty = true;
  74. mode = MODE_THUMB;
  75. } else {
  76. load_image(fileidx);
  77. mode = MODE_IMAGE;
  78. }
  79. return true;
  80. }
  81. bool cg_toggle_fullscreen(arg_t _)
  82. {
  83. win_toggle_fullscreen(&win);
  84. /* redraw after next ConfigureNotify event */
  85. set_timeout(redraw, TO_REDRAW_RESIZE, false);
  86. if (mode == MODE_IMAGE)
  87. img.checkpan = img.dirty = true;
  88. else
  89. tns.dirty = true;
  90. return false;
  91. }
  92. bool cg_toggle_bar(arg_t _)
  93. {
  94. win_toggle_bar(&win);
  95. if (mode == MODE_IMAGE) {
  96. if (win.bar.h > 0)
  97. open_info();
  98. else
  99. close_info();
  100. img.checkpan = img.dirty = true;
  101. } else {
  102. tns.dirty = true;
  103. }
  104. return true;
  105. }
  106. bool cg_prefix_external(arg_t _)
  107. {
  108. extprefix = true;
  109. return false;
  110. }
  111. bool cg_reload_image(arg_t _)
  112. {
  113. if (mode == MODE_IMAGE) {
  114. load_image(fileidx);
  115. } else {
  116. win_set_cursor(&win, CURSOR_WATCH);
  117. if (!tns_load(&tns, fileidx, true, false)) {
  118. remove_file(fileidx, false);
  119. tns.dirty = true;
  120. }
  121. }
  122. return true;
  123. }
  124. // Seems original programmer uses _ to mean parm not used. I changed it to banish.
  125. // In thumbnail mode, still removes current image, not marked ones
  126. bool cg_remove_image(arg_t banish)
  127. {
  128. if (banish) {
  129. // printf("banish\n");
  130. run_key_handler("d", 4); // 4 means ctrl. Ugly hack. Better way: have keyhandler able to remove images from the list. Removes image from external database.
  131. }
  132. remove_file(fileidx, true);
  133. if (mode == MODE_IMAGE)
  134. load_image(fileidx);
  135. else
  136. tns.dirty = true;
  137. return true;
  138. }
  139. bool cg_first(arg_t _)
  140. {
  141. if (mode == MODE_IMAGE && fileidx != 0) {
  142. load_image(0);
  143. return true;
  144. } else if (mode == MODE_THUMB && fileidx != 0) {
  145. fileidx = 0;
  146. tns.dirty = true;
  147. return true;
  148. } else {
  149. return false;
  150. }
  151. }
  152. bool cg_n_or_last(arg_t _)
  153. {
  154. int n = prefix != 0 && prefix - 1 < filecnt ? prefix - 1 : filecnt - 1;
  155. if (mode == MODE_IMAGE && fileidx != n) {
  156. load_image(n);
  157. return true;
  158. } else if (mode == MODE_THUMB && fileidx != n) {
  159. fileidx = n;
  160. tns.dirty = true;
  161. return true;
  162. } else {
  163. return false;
  164. }
  165. }
  166. bool cg_scroll_screen(arg_t dir)
  167. {
  168. if (mode == MODE_IMAGE)
  169. return img_pan(&img, dir, -1);
  170. else
  171. return tns_scroll(&tns, dir, true);
  172. }
  173. bool cg_zoom(arg_t d)
  174. {
  175. if (mode == MODE_THUMB)
  176. return tns_zoom(&tns, d);
  177. else if (d > 0)
  178. return img_zoom_in(&img);
  179. else if (d < 0)
  180. return img_zoom_out(&img);
  181. else
  182. return false;
  183. }
  184. bool cg_toggle_image_mark(arg_t _)
  185. {
  186. return mark_image(fileidx, !(files[fileidx].flags & FF_MARK));
  187. }
  188. bool cg_reverse_marks(arg_t _)
  189. {
  190. int i;
  191. for (i = 0; i < filecnt; i++) {
  192. files[i].flags ^= FF_MARK;
  193. markcnt += files[i].flags & FF_MARK ? 1 : -1;
  194. }
  195. if (mode == MODE_THUMB)
  196. tns.dirty = true;
  197. return true;
  198. }
  199. bool cg_mark_range(arg_t _)
  200. {
  201. int d = markidx < fileidx ? 1 : -1, end, i;
  202. bool dirty = false, on = !!(files[markidx].flags & FF_MARK);
  203. for (i = markidx + d, end = fileidx + d; i != end; i += d)
  204. dirty |= mark_image(i, on);
  205. return dirty;
  206. }
  207. bool cg_unmark_all(arg_t _)
  208. {
  209. int i;
  210. for (i = 0; i < filecnt; i++)
  211. files[i].flags &= ~FF_MARK;
  212. markcnt = 0;
  213. if (mode == MODE_THUMB)
  214. tns.dirty = true;
  215. return true;
  216. }
  217. bool cg_navigate_marked(arg_t n)
  218. {
  219. int d, i;
  220. int new = fileidx;
  221. if (prefix > 0)
  222. n *= prefix;
  223. d = n > 0 ? 1 : -1;
  224. for (i = fileidx + d; n != 0 && i >= 0 && i < filecnt; i += d) {
  225. if (files[i].flags & FF_MARK) {
  226. n -= d;
  227. new = i;
  228. }
  229. }
  230. if (new != fileidx) {
  231. if (mode == MODE_IMAGE) {
  232. load_image(new);
  233. } else {
  234. fileidx = new;
  235. tns.dirty = true;
  236. }
  237. return true;
  238. } else {
  239. return false;
  240. }
  241. }
  242. bool cg_change_gamma(arg_t d)
  243. {
  244. if (img_change_gamma(&img, d * (prefix > 0 ? prefix : 1))) {
  245. if (mode == MODE_THUMB)
  246. tns.dirty = true;
  247. return true;
  248. } else {
  249. return false;
  250. }
  251. }
  252. bool ci_navigate(arg_t n)
  253. {
  254. if (prefix > 0)
  255. n *= prefix;
  256. n += fileidx;
  257. if (n < 0)
  258. n = 0;
  259. if (n >= filecnt)
  260. n = filecnt - 1;
  261. if (n != fileidx) {
  262. load_image(n);
  263. // Kanon added
  264. ci_fit_to_win(SCALE_DOWN);
  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. };