A Simple X Image Viewer
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

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