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.
 
 
 
 
 
 

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