A Simple X Image Viewer
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

13 роки тому
13 роки тому
13 роки тому
10 роки тому
10 роки тому
10 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
10 роки тому
10 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
7 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. void redraw(void);
  33. void reset_cursor(void);
  34. void animate(void);
  35. void slideshow(void);
  36. void set_timeout(timeout_f, int, bool);
  37. void reset_timeout(timeout_f);
  38. extern appmode_t mode;
  39. extern img_t img;
  40. extern tns_t tns;
  41. extern win_t win;
  42. extern fileinfo_t *files;
  43. extern int filecnt, fileidx;
  44. extern int alternate;
  45. extern int markcnt;
  46. extern int prefix;
  47. extern bool extprefix;
  48. bool cg_quit(arg_t _)
  49. {
  50. unsigned int i;
  51. if (options->to_stdout && markcnt > 0) {
  52. for (i = 0; i < filecnt; i++) {
  53. if (files[i].flags & FF_MARK)
  54. printf("%s\n", files[i].name);
  55. }
  56. }
  57. exit(EXIT_SUCCESS);
  58. }
  59. bool cg_switch_mode(arg_t _)
  60. {
  61. if (mode == MODE_IMAGE) {
  62. if (tns.thumbs == NULL)
  63. tns_init(&tns, files, &filecnt, &fileidx, &win);
  64. img_close(&img, false);
  65. reset_timeout(reset_cursor);
  66. if (img.ss.on) {
  67. img.ss.on = false;
  68. reset_timeout(slideshow);
  69. }
  70. tns.dirty = true;
  71. mode = MODE_THUMB;
  72. } else {
  73. load_image(fileidx);
  74. mode = MODE_IMAGE;
  75. }
  76. return true;
  77. }
  78. bool cg_toggle_fullscreen(arg_t _)
  79. {
  80. win_toggle_fullscreen(&win);
  81. /* redraw after next ConfigureNotify event */
  82. set_timeout(redraw, TO_REDRAW_RESIZE, false);
  83. if (mode == MODE_IMAGE)
  84. img.checkpan = img.dirty = true;
  85. else
  86. tns.dirty = true;
  87. return false;
  88. }
  89. bool cg_toggle_bar(arg_t _)
  90. {
  91. win_toggle_bar(&win);
  92. if (mode == MODE_IMAGE) {
  93. img.checkpan = img.dirty = true;
  94. if (win.bar.h > 0)
  95. open_info();
  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_alternate(arg_t _)
  254. {
  255. load_image(alternate);
  256. return true;
  257. }
  258. bool ci_navigate_frame(arg_t d)
  259. {
  260. if (prefix > 0)
  261. d *= prefix;
  262. return !img.multi.animate && img_frame_navigate(&img, d);
  263. }
  264. bool ci_toggle_animation(arg_t _)
  265. {
  266. bool dirty = false;
  267. if (img.multi.cnt > 0) {
  268. img.multi.animate = !img.multi.animate;
  269. if (img.multi.animate) {
  270. dirty = img_frame_animate(&img);
  271. set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
  272. } else {
  273. reset_timeout(animate);
  274. }
  275. }
  276. return dirty;
  277. }
  278. bool ci_scroll(arg_t dir)
  279. {
  280. return img_pan(&img, dir, prefix);
  281. }
  282. bool ci_scroll_to_edge(arg_t dir)
  283. {
  284. return img_pan_edge(&img, dir);
  285. }
  286. /* Xlib helper function for i_drag() */
  287. Bool is_motionnotify(Display *d, XEvent *e, XPointer a)
  288. {
  289. return e != NULL && e->type == MotionNotify;
  290. }
  291. #define WARP(x,y) \
  292. XWarpPointer(win.env.dpy, None, win.xwin, 0, 0, 0, 0, x, y); \
  293. ox = x, oy = y; \
  294. break
  295. bool ci_drag(arg_t _)
  296. {
  297. int dx = 0, dy = 0, i, ox, oy, x, y;
  298. unsigned int ui;
  299. bool dragging = true, next = false;
  300. XEvent e;
  301. Window w;
  302. if (!XQueryPointer(win.env.dpy, win.xwin, &w, &w, &i, &i, &ox, &oy, &ui))
  303. return false;
  304. win_set_cursor(&win, CURSOR_HAND);
  305. while (dragging) {
  306. if (!next)
  307. XMaskEvent(win.env.dpy,
  308. ButtonPressMask | ButtonReleaseMask | PointerMotionMask, &e);
  309. switch (e.type) {
  310. case ButtonPress:
  311. case ButtonRelease:
  312. dragging = false;
  313. break;
  314. case MotionNotify:
  315. x = e.xmotion.x;
  316. y = e.xmotion.y;
  317. /* wrap the mouse around */
  318. if (x <= 0) {
  319. WARP(win.w - 2, y);
  320. } else if (x >= win.w - 1) {
  321. WARP(1, y);
  322. } else if (y <= 0) {
  323. WARP(x, win.h - 2);
  324. } else if (y >= win.h - 1) {
  325. WARP(x, 1);
  326. }
  327. dx += x - ox;
  328. dy += y - oy;
  329. ox = x;
  330. oy = y;
  331. break;
  332. }
  333. if (dragging)
  334. next = XCheckIfEvent(win.env.dpy, &e, is_motionnotify, None);
  335. if ((!dragging || !next) && (dx != 0 || dy != 0)) {
  336. if (img_move(&img, dx, dy)) {
  337. img_render(&img);
  338. win_draw(&win);
  339. }
  340. dx = dy = 0;
  341. }
  342. }
  343. win_set_cursor(&win, CURSOR_ARROW);
  344. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  345. reset_timeout(redraw);
  346. return true;
  347. }
  348. bool ci_set_zoom(arg_t zl)
  349. {
  350. return img_zoom(&img, (prefix ? prefix : zl) / 100.0);
  351. }
  352. bool ci_fit_to_win(arg_t sm)
  353. {
  354. return img_fit_win(&img, sm);
  355. }
  356. bool ci_rotate(arg_t degree)
  357. {
  358. img_rotate(&img, degree);
  359. return true;
  360. }
  361. bool ci_flip(arg_t dir)
  362. {
  363. img_flip(&img, dir);
  364. return true;
  365. }
  366. bool ci_toggle_antialias(arg_t _)
  367. {
  368. img_toggle_antialias(&img);
  369. return true;
  370. }
  371. bool ci_toggle_alpha(arg_t _)
  372. {
  373. img.alpha = !img.alpha;
  374. img.dirty = true;
  375. return true;
  376. }
  377. bool ci_slideshow(arg_t _)
  378. {
  379. if (prefix > 0) {
  380. img.ss.on = true;
  381. img.ss.delay = prefix * 10;
  382. set_timeout(slideshow, img.ss.delay * 100, true);
  383. } else if (img.ss.on) {
  384. img.ss.on = false;
  385. reset_timeout(slideshow);
  386. } else {
  387. img.ss.on = true;
  388. }
  389. return true;
  390. }
  391. bool ct_move_sel(arg_t dir)
  392. {
  393. return tns_move_selection(&tns, dir, prefix);
  394. }
  395. bool ct_reload_all(arg_t _)
  396. {
  397. tns_free(&tns);
  398. tns_init(&tns, files, &filecnt, &fileidx, &win);
  399. tns.dirty = true;
  400. return true;
  401. }
  402. #undef G_CMD
  403. #define G_CMD(c) { -1, cg_##c },
  404. #undef I_CMD
  405. #define I_CMD(c) { MODE_IMAGE, ci_##c },
  406. #undef T_CMD
  407. #define T_CMD(c) { MODE_THUMB, ct_##c },
  408. const cmd_t cmds[CMD_COUNT] = {
  409. #include "commands.lst"
  410. };