A Simple X Image Viewer
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

488 řádky
8.8 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. #define _POSIX_C_SOURCE 200112L
  19. #define _IMAGE_CONFIG
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include <sys/wait.h>
  24. #include "commands.h"
  25. #include "image.h"
  26. #include "options.h"
  27. #include "thumbs.h"
  28. #include "util.h"
  29. #include "config.h"
  30. void cleanup(void);
  31. void remove_file(int, bool);
  32. void load_image(int);
  33. void open_info(void);
  34. void redraw(void);
  35. void reset_cursor(void);
  36. void animate(void);
  37. void slideshow(void);
  38. void set_timeout(timeout_f, int, bool);
  39. void reset_timeout(timeout_f);
  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 prefix;
  49. extern bool extprefix;
  50. const int ss_delays[] = {
  51. 1, 2, 3, 5, 10, 15, 20, 30, 60, 120, 180, 300, 600
  52. };
  53. bool cg_quit(arg_t a)
  54. {
  55. unsigned int i;
  56. if (options->to_stdout && markcnt > 0) {
  57. for (i = 0; i < filecnt; i++) {
  58. if (files[i].marked)
  59. printf("%s\n", files[i].name);
  60. }
  61. }
  62. cleanup();
  63. exit(EXIT_SUCCESS);
  64. }
  65. bool cg_switch_mode(arg_t a)
  66. {
  67. if (mode == MODE_IMAGE) {
  68. if (tns.thumbs == NULL)
  69. tns_init(&tns, files, filecnt, &fileidx, &win);
  70. img_close(&img, false);
  71. reset_timeout(reset_cursor);
  72. if (img.ss.on) {
  73. img.ss.on = false;
  74. reset_timeout(slideshow);
  75. }
  76. tns.dirty = true;
  77. mode = MODE_THUMB;
  78. } else {
  79. load_image(fileidx);
  80. mode = MODE_IMAGE;
  81. }
  82. return true;
  83. }
  84. bool cg_toggle_fullscreen(arg_t a)
  85. {
  86. win_toggle_fullscreen(&win);
  87. /* redraw after next ConfigureNotify event */
  88. set_timeout(redraw, TO_REDRAW_RESIZE, false);
  89. if (mode == MODE_IMAGE)
  90. img.checkpan = img.dirty = true;
  91. else
  92. tns.dirty = true;
  93. return false;
  94. }
  95. bool cg_toggle_bar(arg_t a)
  96. {
  97. win_toggle_bar(&win);
  98. if (mode == MODE_IMAGE) {
  99. img.checkpan = img.dirty = true;
  100. if (win.bar.h > 0)
  101. open_info();
  102. } else {
  103. tns.dirty = true;
  104. }
  105. return true;
  106. }
  107. bool cg_prefix_external(arg_t a)
  108. {
  109. extprefix = true;
  110. return false;
  111. }
  112. bool cg_reload_image(arg_t a)
  113. {
  114. if (mode == MODE_IMAGE) {
  115. load_image(fileidx);
  116. } else {
  117. win_set_cursor(&win, CURSOR_WATCH);
  118. if (!tns_load(&tns, fileidx, true)) {
  119. remove_file(fileidx, false);
  120. tns.dirty = true;
  121. }
  122. }
  123. return true;
  124. }
  125. bool cg_remove_image(arg_t a)
  126. {
  127. if (mode == MODE_IMAGE) {
  128. remove_file(fileidx, true);
  129. load_image(fileidx >= filecnt ? filecnt - 1 : fileidx);
  130. return true;
  131. } else if (fileidx < tns.cnt) {
  132. remove_file(fileidx, true);
  133. tns.dirty = true;
  134. return true;
  135. } else {
  136. return false;
  137. }
  138. }
  139. bool cg_first(arg_t a)
  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 a)
  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 a)
  167. {
  168. direction_t dir = (direction_t) a;
  169. if (mode == MODE_IMAGE)
  170. return img_pan(&img, dir, -1);
  171. else
  172. return tns_scroll(&tns, dir, true);
  173. }
  174. bool cg_toggle_image_mark(arg_t a)
  175. {
  176. files[fileidx].marked = !files[fileidx].marked;
  177. markcnt += files[fileidx].marked ? 1 : -1;
  178. if (mode == MODE_THUMB)
  179. tns_mark(&tns, fileidx, files[fileidx].marked);
  180. return true;
  181. }
  182. bool cg_reverse_marks(arg_t a)
  183. {
  184. int i;
  185. for (i = 0; i < filecnt; i++) {
  186. files[i].marked = !files[i].marked;
  187. markcnt += files[i].marked ? 1 : -1;
  188. }
  189. if (mode == MODE_THUMB)
  190. tns.dirty = true;
  191. return true;
  192. }
  193. bool cg_unmark_all(arg_t a)
  194. {
  195. int i;
  196. for (i = 0; i < filecnt; i++)
  197. files[i].marked = false;
  198. markcnt = 0;
  199. if (mode == MODE_THUMB)
  200. tns.dirty = true;
  201. return true;
  202. }
  203. bool cg_navigate_marked(arg_t a)
  204. {
  205. long n = (long) a;
  206. int d, i;
  207. int cnt = mode == MODE_IMAGE ? filecnt : tns.cnt, new = fileidx;
  208. if (prefix > 0)
  209. n *= prefix;
  210. d = n > 0 ? 1 : -1;
  211. for (i = fileidx + d; n != 0 && i >= 0 && i < cnt; i += d) {
  212. if (files[i].marked) {
  213. n -= d;
  214. new = i;
  215. }
  216. }
  217. if (new != fileidx) {
  218. if (mode == MODE_IMAGE) {
  219. load_image(new);
  220. } else {
  221. fileidx = new;
  222. tns.dirty = true;
  223. }
  224. return true;
  225. } else {
  226. return false;
  227. }
  228. }
  229. bool ci_navigate(arg_t a)
  230. {
  231. long n = (long) a;
  232. if (prefix > 0)
  233. n *= prefix;
  234. n += fileidx;
  235. if (n < 0)
  236. n = 0;
  237. if (n >= filecnt)
  238. n = filecnt - 1;
  239. if (n != fileidx) {
  240. load_image(n);
  241. return true;
  242. } else {
  243. return false;
  244. }
  245. }
  246. bool ci_alternate(arg_t a)
  247. {
  248. load_image(alternate);
  249. return true;
  250. }
  251. bool ci_navigate_frame(arg_t a)
  252. {
  253. return !img.multi.animate && img_frame_navigate(&img, (long) a);
  254. }
  255. bool ci_toggle_animation(arg_t a)
  256. {
  257. bool dirty = false;
  258. if (img.multi.cnt > 0) {
  259. img.multi.animate = !img.multi.animate;
  260. if (img.multi.animate) {
  261. dirty = img_frame_animate(&img);
  262. set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
  263. } else {
  264. reset_timeout(animate);
  265. }
  266. }
  267. return dirty;
  268. }
  269. bool ci_scroll(arg_t a)
  270. {
  271. direction_t dir = (direction_t) a;
  272. return img_pan(&img, dir, prefix);
  273. }
  274. bool ci_scroll_to_edge(arg_t a)
  275. {
  276. direction_t dir = (direction_t) a;
  277. return img_pan_edge(&img, dir);
  278. }
  279. /* Xlib helper function for i_drag() */
  280. Bool is_motionnotify(Display *d, XEvent *e, XPointer a)
  281. {
  282. return e != NULL && e->type == MotionNotify;
  283. }
  284. #define WARP(x,y) \
  285. XWarpPointer(win.env.dpy, None, win.xwin, 0, 0, 0, 0, x, y); \
  286. ox = x, oy = y; \
  287. break
  288. bool ci_drag(arg_t a)
  289. {
  290. int dx = 0, dy = 0, i, ox, oy, x, y;
  291. unsigned int ui;
  292. bool dragging = true, next = false;
  293. XEvent e;
  294. Window w;
  295. if (!XQueryPointer(win.env.dpy, win.xwin, &w, &w, &i, &i, &ox, &oy, &ui))
  296. return false;
  297. win_set_cursor(&win, CURSOR_HAND);
  298. while (dragging) {
  299. if (!next)
  300. XMaskEvent(win.env.dpy,
  301. ButtonPressMask | ButtonReleaseMask | PointerMotionMask, &e);
  302. switch (e.type) {
  303. case ButtonPress:
  304. case ButtonRelease:
  305. dragging = false;
  306. break;
  307. case MotionNotify:
  308. x = e.xmotion.x;
  309. y = e.xmotion.y;
  310. /* wrap the mouse around */
  311. if (x <= 0) {
  312. WARP(win.w - 2, y);
  313. } else if (x >= win.w - 1) {
  314. WARP(1, y);
  315. } else if (y <= 0) {
  316. WARP(x, win.h - 2);
  317. } else if (y >= win.h - 1) {
  318. WARP(x, 1);
  319. }
  320. dx += x - ox;
  321. dy += y - oy;
  322. ox = x;
  323. oy = y;
  324. break;
  325. }
  326. if (dragging)
  327. next = XCheckIfEvent(win.env.dpy, &e, is_motionnotify, None);
  328. if ((!dragging || !next) && (dx != 0 || dy != 0)) {
  329. if (img_move(&img, dx, dy)) {
  330. img_render(&img);
  331. win_draw(&win);
  332. }
  333. dx = dy = 0;
  334. }
  335. }
  336. win_set_cursor(&win, CURSOR_ARROW);
  337. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  338. reset_timeout(redraw);
  339. return true;
  340. }
  341. bool ci_zoom(arg_t a)
  342. {
  343. long scale = (long) a;
  344. if (scale > 0)
  345. return img_zoom_in(&img);
  346. else if (scale < 0)
  347. return img_zoom_out(&img);
  348. else
  349. return false;
  350. }
  351. bool ci_set_zoom(arg_t a)
  352. {
  353. return img_zoom(&img, (prefix ? prefix : (long) a) / 100.0);
  354. }
  355. bool ci_fit_to_win(arg_t a)
  356. {
  357. scalemode_t sm = (scalemode_t) a;
  358. return img_fit_win(&img, sm);
  359. }
  360. bool ci_rotate(arg_t a)
  361. {
  362. degree_t degree = (degree_t) a;
  363. img_rotate(&img, degree);
  364. return true;
  365. }
  366. bool ci_flip(arg_t a)
  367. {
  368. flipdir_t dir = (flipdir_t) a;
  369. img_flip(&img, dir);
  370. return true;
  371. }
  372. bool ci_change_gamma(arg_t a)
  373. {
  374. return img_change_gamma(&img, (long) a);
  375. }
  376. bool ci_toggle_antialias(arg_t a)
  377. {
  378. img_toggle_antialias(&img);
  379. return true;
  380. }
  381. bool ci_toggle_alpha(arg_t a)
  382. {
  383. img.alpha = !img.alpha;
  384. img.dirty = true;
  385. return true;
  386. }
  387. bool ci_slideshow(arg_t a)
  388. {
  389. if (prefix > 0) {
  390. img.ss.on = true;
  391. img.ss.delay = prefix;
  392. set_timeout(slideshow, img.ss.delay * 1000, true);
  393. } else if (img.ss.on) {
  394. img.ss.on = false;
  395. reset_timeout(slideshow);
  396. } else {
  397. img.ss.on = true;
  398. }
  399. return true;
  400. }
  401. bool ct_move_sel(arg_t a)
  402. {
  403. direction_t dir = (direction_t) a;
  404. return tns_move_selection(&tns, dir, prefix);
  405. }
  406. bool ct_reload_all(arg_t a)
  407. {
  408. tns_free(&tns);
  409. tns_init(&tns, files, filecnt, &fileidx, &win);
  410. return false;
  411. }
  412. #undef G_CMD
  413. #define G_CMD(c) { -1, cg_##c },
  414. #undef I_CMD
  415. #define I_CMD(c) { MODE_IMAGE, ci_##c },
  416. #undef T_CMD
  417. #define T_CMD(c) { MODE_THUMB, ct_##c },
  418. const cmd_t cmds[CMD_COUNT] = {
  419. #include "commands.lst"
  420. };