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.
 
 
 
 
 
 

516 linhas
9.6 KiB

  1. /* Copyright 2011, 2012 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 prefix;
  48. extern bool extprefix;
  49. const int ss_delays[] = {
  50. 1, 2, 3, 5, 10, 15, 20, 30, 60, 120, 180, 300, 600
  51. };
  52. cmdreturn_t it_quit(arg_t a)
  53. {
  54. unsigned int i;
  55. if (options->to_stdout) {
  56. for (i = 0; i < filecnt; i++) {
  57. if (files[i].marked)
  58. printf("%s\n", files[i].name);
  59. }
  60. }
  61. cleanup();
  62. exit(EXIT_SUCCESS);
  63. }
  64. cmdreturn_t it_switch_mode(arg_t a)
  65. {
  66. if (mode == MODE_IMAGE) {
  67. if (tns.thumbs == NULL) {
  68. tns_init(&tns, filecnt, &win);
  69. tns.alpha = img.alpha;
  70. }
  71. img_close(&img, false);
  72. reset_timeout(reset_cursor);
  73. if (img.ss.on) {
  74. img.ss.on = false;
  75. reset_timeout(slideshow);
  76. }
  77. tns.sel = fileidx;
  78. tns.dirty = true;
  79. mode = MODE_THUMB;
  80. } else {
  81. load_image(tns.sel);
  82. mode = MODE_IMAGE;
  83. }
  84. return CMD_DIRTY;
  85. }
  86. cmdreturn_t it_toggle_fullscreen(arg_t a)
  87. {
  88. win_toggle_fullscreen(&win);
  89. /* redraw after next ConfigureNotify event */
  90. set_timeout(redraw, TO_REDRAW_RESIZE, false);
  91. if (mode == MODE_IMAGE)
  92. img.checkpan = img.dirty = true;
  93. else
  94. tns.dirty = true;
  95. return CMD_OK;
  96. }
  97. cmdreturn_t it_toggle_bar(arg_t a)
  98. {
  99. win_toggle_bar(&win);
  100. if (mode == MODE_IMAGE) {
  101. img.checkpan = img.dirty = true;
  102. if (win.bar.h > 0)
  103. open_info();
  104. } else {
  105. tns.dirty = true;
  106. }
  107. return CMD_DIRTY;
  108. }
  109. cmdreturn_t it_prefix_external(arg_t a)
  110. {
  111. extprefix = true;
  112. return CMD_OK;
  113. }
  114. cmdreturn_t t_reload_all(arg_t a)
  115. {
  116. if (mode == MODE_THUMB) {
  117. tns_free(&tns);
  118. tns_init(&tns, filecnt, &win);
  119. return CMD_DIRTY;
  120. } else {
  121. return CMD_INVALID;
  122. }
  123. }
  124. cmdreturn_t it_reload_image(arg_t a)
  125. {
  126. if (mode == MODE_IMAGE) {
  127. load_image(fileidx);
  128. } else {
  129. win_set_cursor(&win, CURSOR_WATCH);
  130. if (!tns_load(&tns, tns.sel, &files[tns.sel], true, false)) {
  131. remove_file(tns.sel, false);
  132. tns.dirty = true;
  133. if (tns.sel >= tns.cnt)
  134. tns.sel = tns.cnt - 1;
  135. }
  136. }
  137. return CMD_DIRTY;
  138. }
  139. cmdreturn_t it_remove_image(arg_t a)
  140. {
  141. if (mode == MODE_IMAGE) {
  142. remove_file(fileidx, true);
  143. load_image(fileidx >= filecnt ? filecnt - 1 : fileidx);
  144. return CMD_DIRTY;
  145. } else if (tns.sel < tns.cnt) {
  146. remove_file(tns.sel, true);
  147. tns.dirty = true;
  148. if (tns.sel >= tns.cnt)
  149. tns.sel = tns.cnt - 1;
  150. return CMD_DIRTY;
  151. } else {
  152. return CMD_OK;
  153. }
  154. }
  155. cmdreturn_t i_navigate(arg_t a)
  156. {
  157. long n = (long) a;
  158. if (mode == MODE_IMAGE) {
  159. if (prefix > 0)
  160. n *= prefix;
  161. n += fileidx;
  162. if (n < 0)
  163. n = 0;
  164. if (n >= filecnt)
  165. n = filecnt - 1;
  166. if (n != fileidx) {
  167. load_image(n);
  168. return CMD_DIRTY;
  169. }
  170. }
  171. return CMD_INVALID;
  172. }
  173. cmdreturn_t i_alternate(arg_t a)
  174. {
  175. if (mode == MODE_IMAGE) {
  176. load_image(alternate);
  177. return CMD_DIRTY;
  178. } else {
  179. return CMD_INVALID;
  180. }
  181. }
  182. cmdreturn_t it_first(arg_t a)
  183. {
  184. if (mode == MODE_IMAGE && fileidx != 0) {
  185. load_image(0);
  186. return CMD_DIRTY;
  187. } else if (mode == MODE_THUMB && tns.sel != 0) {
  188. tns.sel = 0;
  189. tns.dirty = true;
  190. return CMD_DIRTY;
  191. } else {
  192. return CMD_OK;
  193. }
  194. }
  195. cmdreturn_t it_n_or_last(arg_t a)
  196. {
  197. int n = prefix != 0 && prefix - 1 < filecnt ? prefix - 1 : filecnt - 1;
  198. if (mode == MODE_IMAGE && fileidx != n) {
  199. load_image(n);
  200. return CMD_DIRTY;
  201. } else if (mode == MODE_THUMB && tns.sel != n) {
  202. tns.sel = n;
  203. tns.dirty = true;
  204. return CMD_DIRTY;
  205. } else {
  206. return CMD_OK;
  207. }
  208. }
  209. cmdreturn_t i_navigate_frame(arg_t a)
  210. {
  211. if (mode != MODE_IMAGE)
  212. return CMD_INVALID;
  213. else
  214. return !img.multi.animate && img_frame_navigate(&img, (long) a);
  215. }
  216. cmdreturn_t i_toggle_animation(arg_t a)
  217. {
  218. if (mode != MODE_IMAGE)
  219. return CMD_INVALID;
  220. if (img.multi.animate) {
  221. reset_timeout(animate);
  222. img.multi.animate = false;
  223. } else if (img_frame_animate(&img, true)) {
  224. set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
  225. }
  226. return CMD_DIRTY;
  227. }
  228. cmdreturn_t it_toggle_image_mark(arg_t a)
  229. {
  230. int sel = mode == MODE_IMAGE ? fileidx : tns.sel;
  231. files[sel].marked = !files[sel].marked;
  232. if (mode == MODE_THUMB)
  233. tns_mark(&tns, sel, files[sel].marked);
  234. return CMD_DIRTY;
  235. }
  236. cmdreturn_t it_reverse_marks(arg_t a)
  237. {
  238. int i, cnt = mode == MODE_IMAGE ? filecnt : tns.cnt;
  239. for (i = 0; i < cnt; i++)
  240. files[i].marked = !files[i].marked;
  241. if (mode == MODE_THUMB)
  242. tns.dirty = true;
  243. return CMD_DIRTY;
  244. }
  245. cmdreturn_t it_navigate_marked(arg_t a)
  246. {
  247. long n = (long) a;
  248. int d, i, cnt, sel, new;
  249. if (mode == MODE_IMAGE)
  250. cnt = filecnt, sel = new = fileidx;
  251. else
  252. cnt = tns.cnt, sel = new = tns.sel;
  253. if (prefix > 0)
  254. n *= prefix;
  255. d = n > 0 ? 1 : -1;
  256. for (i = sel + d; n != 0 && i >= 0 && i < cnt; i += d) {
  257. if (files[i].marked) {
  258. n -= d;
  259. new = i;
  260. }
  261. }
  262. if (new != sel) {
  263. if (mode == MODE_IMAGE) {
  264. load_image(new);
  265. } else {
  266. tns.sel = new;
  267. tns.dirty = true;
  268. }
  269. return CMD_DIRTY;
  270. } else {
  271. return CMD_OK;
  272. }
  273. }
  274. cmdreturn_t it_scroll_move(arg_t a)
  275. {
  276. direction_t dir = (direction_t) a;
  277. if (mode == MODE_IMAGE)
  278. return img_pan(&img, dir, prefix);
  279. else
  280. return tns_move_selection(&tns, dir, prefix);
  281. }
  282. cmdreturn_t it_scroll_screen(arg_t a)
  283. {
  284. direction_t dir = (direction_t) a;
  285. if (mode == MODE_IMAGE)
  286. return img_pan(&img, dir, -1);
  287. else
  288. return tns_scroll(&tns, dir, true);
  289. }
  290. cmdreturn_t i_scroll_to_edge(arg_t a)
  291. {
  292. direction_t dir = (direction_t) a;
  293. if (mode == MODE_IMAGE)
  294. return img_pan_edge(&img, dir);
  295. else
  296. return CMD_INVALID;
  297. }
  298. /* Xlib helper function for i_drag() */
  299. Bool is_motionnotify(Display *d, XEvent *e, XPointer a)
  300. {
  301. return e != NULL && e->type == MotionNotify;
  302. }
  303. #define WARP(x,y) \
  304. XWarpPointer(win.env.dpy, None, win.xwin, 0, 0, 0, 0, x, y); \
  305. ox = x, oy = y; \
  306. break
  307. cmdreturn_t i_drag(arg_t a)
  308. {
  309. int dx = 0, dy = 0, i, ox, oy, x, y;
  310. unsigned int ui;
  311. bool dragging = true, next = false;
  312. XEvent e;
  313. Window w;
  314. if (mode != MODE_IMAGE)
  315. return CMD_INVALID;
  316. if (!XQueryPointer(win.env.dpy, win.xwin, &w, &w, &i, &i, &ox, &oy, &ui))
  317. return CMD_OK;
  318. win_set_cursor(&win, CURSOR_HAND);
  319. while (dragging) {
  320. if (!next)
  321. XMaskEvent(win.env.dpy,
  322. ButtonPressMask | ButtonReleaseMask | PointerMotionMask, &e);
  323. switch (e.type) {
  324. case ButtonPress:
  325. case ButtonRelease:
  326. dragging = false;
  327. break;
  328. case MotionNotify:
  329. x = e.xmotion.x;
  330. y = e.xmotion.y;
  331. /* wrap the mouse around */
  332. if (x <= 0) {
  333. WARP(win.w - 2, y);
  334. } else if (x >= win.w - 1) {
  335. WARP(1, y);
  336. } else if (y <= 0) {
  337. WARP(x, win.h - 2);
  338. } else if (y >= win.h - 1) {
  339. WARP(x, 1);
  340. }
  341. dx += x - ox;
  342. dy += y - oy;
  343. ox = x;
  344. oy = y;
  345. break;
  346. }
  347. if (dragging)
  348. next = XCheckIfEvent(win.env.dpy, &e, is_motionnotify, None);
  349. if ((!dragging || !next) && (dx != 0 || dy != 0)) {
  350. if (img_move(&img, dx, dy)) {
  351. img_render(&img);
  352. win_draw(&win);
  353. }
  354. dx = dy = 0;
  355. }
  356. }
  357. win_set_cursor(&win, CURSOR_ARROW);
  358. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  359. reset_timeout(redraw);
  360. return CMD_OK;
  361. }
  362. cmdreturn_t i_zoom(arg_t a)
  363. {
  364. long scale = (long) a;
  365. if (mode != MODE_IMAGE)
  366. return CMD_INVALID;
  367. if (scale > 0)
  368. return img_zoom_in(&img);
  369. else if (scale < 0)
  370. return img_zoom_out(&img);
  371. else
  372. return CMD_OK;
  373. }
  374. cmdreturn_t i_set_zoom(arg_t a)
  375. {
  376. if (mode == MODE_IMAGE)
  377. return img_zoom(&img, (prefix ? prefix : (long) a) / 100.0);
  378. else
  379. return CMD_INVALID;
  380. }
  381. cmdreturn_t i_fit_to_win(arg_t a)
  382. {
  383. scalemode_t sm = (scalemode_t) a;
  384. if (mode == MODE_IMAGE)
  385. return img_fit_win(&img, sm);
  386. else
  387. return CMD_INVALID;
  388. }
  389. cmdreturn_t i_rotate(arg_t a)
  390. {
  391. degree_t degree = (degree_t) a;
  392. if (mode == MODE_IMAGE) {
  393. img_rotate(&img, degree);
  394. return CMD_DIRTY;
  395. } else {
  396. return CMD_INVALID;
  397. }
  398. }
  399. cmdreturn_t i_flip(arg_t a)
  400. {
  401. flipdir_t dir = (flipdir_t) a;
  402. if (mode == MODE_IMAGE) {
  403. img_flip(&img, dir);
  404. return CMD_DIRTY;
  405. } else {
  406. return CMD_INVALID;
  407. }
  408. }
  409. cmdreturn_t i_slideshow(arg_t a)
  410. {
  411. if (mode == MODE_IMAGE) {
  412. if (prefix > 0) {
  413. img.ss.on = true;
  414. img.ss.delay = prefix;
  415. set_timeout(slideshow, img.ss.delay * 1000, true);
  416. } else if (img.ss.on) {
  417. img.ss.on = false;
  418. reset_timeout(slideshow);
  419. } else {
  420. img.ss.on = true;
  421. }
  422. return CMD_DIRTY;
  423. } else {
  424. return CMD_INVALID;
  425. }
  426. }
  427. cmdreturn_t i_toggle_antialias(arg_t a)
  428. {
  429. if (mode == MODE_IMAGE) {
  430. img_toggle_antialias(&img);
  431. return CMD_DIRTY;
  432. } else {
  433. return CMD_INVALID;
  434. }
  435. }
  436. cmdreturn_t it_toggle_alpha(arg_t a)
  437. {
  438. img.alpha = tns.alpha = !img.alpha;
  439. if (mode == MODE_IMAGE)
  440. img.dirty = true;
  441. else
  442. tns.dirty = true;
  443. return CMD_DIRTY;
  444. }
  445. cmdreturn_t i_change_gamma(arg_t a)
  446. {
  447. if (mode == MODE_IMAGE) {
  448. return img_change_gamma(&img, (long) a);
  449. } else {
  450. return CMD_INVALID;
  451. }
  452. }