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.

commands.c 8.1 KiB

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