A Simple X Image Viewer
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

486 lines
8.7 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. #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 markcnt;
  48. extern int prefix;
  49. extern bool extprefix;
  50. const int ss_delays[] = {
  51. 1, 2, 3, 5, 10, 15, 20, 30, 60, 120, 180, 300, 600
  52. };
  53. bool cg_quit(arg_t a)
  54. {
  55. unsigned int i;
  56. if (options->to_stdout && markcnt > 0) {
  57. for (i = 0; i < filecnt; i++) {
  58. if (files[i].marked)
  59. printf("%s\n", files[i].name);
  60. }
  61. }
  62. cleanup();
  63. exit(EXIT_SUCCESS);
  64. }
  65. bool cg_switch_mode(arg_t a)
  66. {
  67. if (mode == MODE_IMAGE) {
  68. if (tns.thumbs == NULL)
  69. tns_init(&tns, files, &filecnt, &fileidx, &win);
  70. img_close(&img, false);
  71. reset_timeout(reset_cursor);
  72. if (img.ss.on) {
  73. img.ss.on = false;
  74. reset_timeout(slideshow);
  75. }
  76. tns.dirty = true;
  77. mode = MODE_THUMB;
  78. } else {
  79. load_image(fileidx);
  80. mode = MODE_IMAGE;
  81. }
  82. return true;
  83. }
  84. bool cg_toggle_fullscreen(arg_t a)
  85. {
  86. win_toggle_fullscreen(&win);
  87. /* redraw after next ConfigureNotify event */
  88. set_timeout(redraw, TO_REDRAW_RESIZE, false);
  89. if (mode == MODE_IMAGE)
  90. img.checkpan = img.dirty = true;
  91. else
  92. tns.dirty = true;
  93. return false;
  94. }
  95. bool cg_toggle_bar(arg_t a)
  96. {
  97. win_toggle_bar(&win);
  98. if (mode == MODE_IMAGE) {
  99. img.checkpan = img.dirty = true;
  100. if (win.bar.h > 0)
  101. open_info();
  102. } else {
  103. tns.dirty = true;
  104. }
  105. return true;
  106. }
  107. bool cg_prefix_external(arg_t a)
  108. {
  109. extprefix = true;
  110. return false;
  111. }
  112. bool cg_reload_image(arg_t a)
  113. {
  114. if (mode == MODE_IMAGE) {
  115. load_image(fileidx);
  116. } else {
  117. win_set_cursor(&win, CURSOR_WATCH);
  118. if (!tns_load(&tns, fileidx, true)) {
  119. remove_file(fileidx, false);
  120. tns.dirty = true;
  121. }
  122. }
  123. return true;
  124. }
  125. bool cg_remove_image(arg_t a)
  126. {
  127. remove_file(fileidx, true);
  128. if (mode == MODE_IMAGE)
  129. load_image(fileidx);
  130. else
  131. tns.dirty = true;
  132. return true;
  133. }
  134. bool cg_first(arg_t a)
  135. {
  136. if (mode == MODE_IMAGE && fileidx != 0) {
  137. load_image(0);
  138. return true;
  139. } else if (mode == MODE_THUMB && fileidx != 0) {
  140. fileidx = 0;
  141. tns.dirty = true;
  142. return true;
  143. } else {
  144. return false;
  145. }
  146. }
  147. bool cg_n_or_last(arg_t a)
  148. {
  149. int n = prefix != 0 && prefix - 1 < filecnt ? prefix - 1 : filecnt - 1;
  150. if (mode == MODE_IMAGE && fileidx != n) {
  151. load_image(n);
  152. return true;
  153. } else if (mode == MODE_THUMB && fileidx != n) {
  154. fileidx = n;
  155. tns.dirty = true;
  156. return true;
  157. } else {
  158. return false;
  159. }
  160. }
  161. bool cg_scroll_screen(arg_t a)
  162. {
  163. direction_t dir = (direction_t) a;
  164. if (mode == MODE_IMAGE)
  165. return img_pan(&img, dir, -1);
  166. else
  167. return tns_scroll(&tns, dir, true);
  168. }
  169. bool cg_zoom(arg_t a)
  170. {
  171. long d = (long) a;
  172. if (mode == MODE_THUMB)
  173. return tns_zoom(&tns, d);
  174. else if (d > 0)
  175. return img_zoom_in(&img);
  176. else if (d < 0)
  177. return img_zoom_out(&img);
  178. else
  179. return false;
  180. }
  181. bool cg_toggle_image_mark(arg_t a)
  182. {
  183. files[fileidx].marked = !files[fileidx].marked;
  184. markcnt += files[fileidx].marked ? 1 : -1;
  185. if (mode == MODE_THUMB)
  186. tns_mark(&tns, fileidx, files[fileidx].marked);
  187. return true;
  188. }
  189. bool cg_reverse_marks(arg_t a)
  190. {
  191. int i;
  192. for (i = 0; i < filecnt; i++) {
  193. files[i].marked = !files[i].marked;
  194. markcnt += files[i].marked ? 1 : -1;
  195. }
  196. if (mode == MODE_THUMB)
  197. tns.dirty = true;
  198. return true;
  199. }
  200. bool cg_unmark_all(arg_t a)
  201. {
  202. int i;
  203. for (i = 0; i < filecnt; i++)
  204. files[i].marked = false;
  205. markcnt = 0;
  206. if (mode == MODE_THUMB)
  207. tns.dirty = true;
  208. return true;
  209. }
  210. bool cg_navigate_marked(arg_t a)
  211. {
  212. long n = (long) a;
  213. int d, i;
  214. int new = fileidx;
  215. if (prefix > 0)
  216. n *= prefix;
  217. d = n > 0 ? 1 : -1;
  218. for (i = fileidx + d; n != 0 && i >= 0 && i < filecnt; i += d) {
  219. if (files[i].marked) {
  220. n -= d;
  221. new = i;
  222. }
  223. }
  224. if (new != fileidx) {
  225. if (mode == MODE_IMAGE) {
  226. load_image(new);
  227. } else {
  228. fileidx = new;
  229. tns.dirty = true;
  230. }
  231. return true;
  232. } else {
  233. return false;
  234. }
  235. }
  236. bool ci_navigate(arg_t a)
  237. {
  238. long n = (long) a;
  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 a)
  254. {
  255. load_image(alternate);
  256. return true;
  257. }
  258. bool ci_navigate_frame(arg_t a)
  259. {
  260. return !img.multi.animate && img_frame_navigate(&img, (long) a);
  261. }
  262. bool ci_toggle_animation(arg_t a)
  263. {
  264. bool dirty = false;
  265. if (img.multi.cnt > 0) {
  266. img.multi.animate = !img.multi.animate;
  267. if (img.multi.animate) {
  268. dirty = img_frame_animate(&img);
  269. set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
  270. } else {
  271. reset_timeout(animate);
  272. }
  273. }
  274. return dirty;
  275. }
  276. bool ci_scroll(arg_t a)
  277. {
  278. direction_t dir = (direction_t) a;
  279. return img_pan(&img, dir, prefix);
  280. }
  281. bool ci_scroll_to_edge(arg_t a)
  282. {
  283. direction_t dir = (direction_t) a;
  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 a)
  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 a)
  349. {
  350. return img_zoom(&img, (prefix ? prefix : (long) a) / 100.0);
  351. }
  352. bool ci_fit_to_win(arg_t a)
  353. {
  354. scalemode_t sm = (scalemode_t) a;
  355. return img_fit_win(&img, sm);
  356. }
  357. bool ci_rotate(arg_t a)
  358. {
  359. degree_t degree = (degree_t) a;
  360. img_rotate(&img, degree);
  361. return true;
  362. }
  363. bool ci_flip(arg_t a)
  364. {
  365. flipdir_t dir = (flipdir_t) a;
  366. img_flip(&img, dir);
  367. return true;
  368. }
  369. bool ci_change_gamma(arg_t a)
  370. {
  371. return img_change_gamma(&img, (long) a);
  372. }
  373. bool ci_toggle_antialias(arg_t a)
  374. {
  375. img_toggle_antialias(&img);
  376. return true;
  377. }
  378. bool ci_toggle_alpha(arg_t a)
  379. {
  380. img.alpha = !img.alpha;
  381. img.dirty = true;
  382. return true;
  383. }
  384. bool ci_slideshow(arg_t a)
  385. {
  386. if (prefix > 0) {
  387. img.ss.on = true;
  388. img.ss.delay = prefix;
  389. set_timeout(slideshow, img.ss.delay * 1000, true);
  390. } else if (img.ss.on) {
  391. img.ss.on = false;
  392. reset_timeout(slideshow);
  393. } else {
  394. img.ss.on = true;
  395. }
  396. return true;
  397. }
  398. bool ct_move_sel(arg_t a)
  399. {
  400. direction_t dir = (direction_t) a;
  401. return tns_move_selection(&tns, dir, prefix);
  402. }
  403. bool ct_reload_all(arg_t a)
  404. {
  405. tns_free(&tns);
  406. tns_init(&tns, files, &filecnt, &fileidx, &win);
  407. tns.dirty = true;
  408. return true;
  409. }
  410. #undef G_CMD
  411. #define G_CMD(c) { -1, cg_##c },
  412. #undef I_CMD
  413. #define I_CMD(c) { MODE_IMAGE, ci_##c },
  414. #undef T_CMD
  415. #define T_CMD(c) { MODE_THUMB, ct_##c },
  416. const cmd_t cmds[CMD_COUNT] = {
  417. #include "commands.lst"
  418. };