A Simple X Image Viewer
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

491 satır
8.8 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. if (mode == MODE_IMAGE) {
  128. remove_file(fileidx, true);
  129. load_image(fileidx >= filecnt ? filecnt - 1 : fileidx);
  130. return true;
  131. } else if (fileidx < tns.cnt) {
  132. remove_file(fileidx, true);
  133. tns.dirty = true;
  134. return true;
  135. } else {
  136. return false;
  137. }
  138. }
  139. bool cg_first(arg_t a)
  140. {
  141. if (mode == MODE_IMAGE && fileidx != 0) {
  142. load_image(0);
  143. return true;
  144. } else if (mode == MODE_THUMB && fileidx != 0) {
  145. fileidx = 0;
  146. tns.dirty = true;
  147. return true;
  148. } else {
  149. return false;
  150. }
  151. }
  152. bool cg_n_or_last(arg_t a)
  153. {
  154. int n = prefix != 0 && prefix - 1 < filecnt ? prefix - 1 : filecnt - 1;
  155. if (mode == MODE_IMAGE && fileidx != n) {
  156. load_image(n);
  157. return true;
  158. } else if (mode == MODE_THUMB && fileidx != n) {
  159. fileidx = n;
  160. tns.dirty = true;
  161. return true;
  162. } else {
  163. return false;
  164. }
  165. }
  166. bool cg_scroll_screen(arg_t a)
  167. {
  168. direction_t dir = (direction_t) a;
  169. if (mode == MODE_IMAGE)
  170. return img_pan(&img, dir, -1);
  171. else
  172. return tns_scroll(&tns, dir, true);
  173. }
  174. bool cg_zoom(arg_t a)
  175. {
  176. long d = (long) a;
  177. if (mode == MODE_THUMB)
  178. return tns_zoom(&tns, d);
  179. else if (d > 0)
  180. return img_zoom_in(&img);
  181. else if (d < 0)
  182. return img_zoom_out(&img);
  183. else
  184. return false;
  185. }
  186. bool cg_toggle_image_mark(arg_t a)
  187. {
  188. files[fileidx].marked = !files[fileidx].marked;
  189. markcnt += files[fileidx].marked ? 1 : -1;
  190. if (mode == MODE_THUMB)
  191. tns_mark(&tns, fileidx, files[fileidx].marked);
  192. return true;
  193. }
  194. bool cg_reverse_marks(arg_t a)
  195. {
  196. int i;
  197. for (i = 0; i < filecnt; i++) {
  198. files[i].marked = !files[i].marked;
  199. markcnt += files[i].marked ? 1 : -1;
  200. }
  201. if (mode == MODE_THUMB)
  202. tns.dirty = true;
  203. return true;
  204. }
  205. bool cg_unmark_all(arg_t a)
  206. {
  207. int i;
  208. for (i = 0; i < filecnt; i++)
  209. files[i].marked = false;
  210. markcnt = 0;
  211. if (mode == MODE_THUMB)
  212. tns.dirty = true;
  213. return true;
  214. }
  215. bool cg_navigate_marked(arg_t a)
  216. {
  217. long n = (long) a;
  218. int d, i;
  219. int cnt = mode == MODE_IMAGE ? filecnt : tns.cnt, new = fileidx;
  220. if (prefix > 0)
  221. n *= prefix;
  222. d = n > 0 ? 1 : -1;
  223. for (i = fileidx + d; n != 0 && i >= 0 && i < cnt; i += d) {
  224. if (files[i].marked) {
  225. n -= d;
  226. new = i;
  227. }
  228. }
  229. if (new != fileidx) {
  230. if (mode == MODE_IMAGE) {
  231. load_image(new);
  232. } else {
  233. fileidx = new;
  234. tns.dirty = true;
  235. }
  236. return true;
  237. } else {
  238. return false;
  239. }
  240. }
  241. bool ci_navigate(arg_t a)
  242. {
  243. long n = (long) a;
  244. if (prefix > 0)
  245. n *= prefix;
  246. n += fileidx;
  247. if (n < 0)
  248. n = 0;
  249. if (n >= filecnt)
  250. n = filecnt - 1;
  251. if (n != fileidx) {
  252. load_image(n);
  253. return true;
  254. } else {
  255. return false;
  256. }
  257. }
  258. bool ci_alternate(arg_t a)
  259. {
  260. load_image(alternate);
  261. return true;
  262. }
  263. bool ci_navigate_frame(arg_t a)
  264. {
  265. return !img.multi.animate && img_frame_navigate(&img, (long) a);
  266. }
  267. bool ci_toggle_animation(arg_t a)
  268. {
  269. bool dirty = false;
  270. if (img.multi.cnt > 0) {
  271. img.multi.animate = !img.multi.animate;
  272. if (img.multi.animate) {
  273. dirty = img_frame_animate(&img);
  274. set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
  275. } else {
  276. reset_timeout(animate);
  277. }
  278. }
  279. return dirty;
  280. }
  281. bool ci_scroll(arg_t a)
  282. {
  283. direction_t dir = (direction_t) a;
  284. return img_pan(&img, dir, prefix);
  285. }
  286. bool ci_scroll_to_edge(arg_t a)
  287. {
  288. direction_t dir = (direction_t) a;
  289. return img_pan_edge(&img, dir);
  290. }
  291. /* Xlib helper function for i_drag() */
  292. Bool is_motionnotify(Display *d, XEvent *e, XPointer a)
  293. {
  294. return e != NULL && e->type == MotionNotify;
  295. }
  296. #define WARP(x,y) \
  297. XWarpPointer(win.env.dpy, None, win.xwin, 0, 0, 0, 0, x, y); \
  298. ox = x, oy = y; \
  299. break
  300. bool ci_drag(arg_t a)
  301. {
  302. int dx = 0, dy = 0, i, ox, oy, x, y;
  303. unsigned int ui;
  304. bool dragging = true, next = false;
  305. XEvent e;
  306. Window w;
  307. if (!XQueryPointer(win.env.dpy, win.xwin, &w, &w, &i, &i, &ox, &oy, &ui))
  308. return false;
  309. win_set_cursor(&win, CURSOR_HAND);
  310. while (dragging) {
  311. if (!next)
  312. XMaskEvent(win.env.dpy,
  313. ButtonPressMask | ButtonReleaseMask | PointerMotionMask, &e);
  314. switch (e.type) {
  315. case ButtonPress:
  316. case ButtonRelease:
  317. dragging = false;
  318. break;
  319. case MotionNotify:
  320. x = e.xmotion.x;
  321. y = e.xmotion.y;
  322. /* wrap the mouse around */
  323. if (x <= 0) {
  324. WARP(win.w - 2, y);
  325. } else if (x >= win.w - 1) {
  326. WARP(1, y);
  327. } else if (y <= 0) {
  328. WARP(x, win.h - 2);
  329. } else if (y >= win.h - 1) {
  330. WARP(x, 1);
  331. }
  332. dx += x - ox;
  333. dy += y - oy;
  334. ox = x;
  335. oy = y;
  336. break;
  337. }
  338. if (dragging)
  339. next = XCheckIfEvent(win.env.dpy, &e, is_motionnotify, None);
  340. if ((!dragging || !next) && (dx != 0 || dy != 0)) {
  341. if (img_move(&img, dx, dy)) {
  342. img_render(&img);
  343. win_draw(&win);
  344. }
  345. dx = dy = 0;
  346. }
  347. }
  348. win_set_cursor(&win, CURSOR_ARROW);
  349. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  350. reset_timeout(redraw);
  351. return true;
  352. }
  353. bool ci_set_zoom(arg_t a)
  354. {
  355. return img_zoom(&img, (prefix ? prefix : (long) a) / 100.0);
  356. }
  357. bool ci_fit_to_win(arg_t a)
  358. {
  359. scalemode_t sm = (scalemode_t) a;
  360. return img_fit_win(&img, sm);
  361. }
  362. bool ci_rotate(arg_t a)
  363. {
  364. degree_t degree = (degree_t) a;
  365. img_rotate(&img, degree);
  366. return true;
  367. }
  368. bool ci_flip(arg_t a)
  369. {
  370. flipdir_t dir = (flipdir_t) a;
  371. img_flip(&img, dir);
  372. return true;
  373. }
  374. bool ci_change_gamma(arg_t a)
  375. {
  376. return img_change_gamma(&img, (long) a);
  377. }
  378. bool ci_toggle_antialias(arg_t a)
  379. {
  380. img_toggle_antialias(&img);
  381. return true;
  382. }
  383. bool ci_toggle_alpha(arg_t a)
  384. {
  385. img.alpha = !img.alpha;
  386. img.dirty = true;
  387. return true;
  388. }
  389. bool ci_slideshow(arg_t a)
  390. {
  391. if (prefix > 0) {
  392. img.ss.on = true;
  393. img.ss.delay = prefix;
  394. set_timeout(slideshow, img.ss.delay * 1000, true);
  395. } else if (img.ss.on) {
  396. img.ss.on = false;
  397. reset_timeout(slideshow);
  398. } else {
  399. img.ss.on = true;
  400. }
  401. return true;
  402. }
  403. bool ct_move_sel(arg_t a)
  404. {
  405. direction_t dir = (direction_t) a;
  406. return tns_move_selection(&tns, dir, prefix);
  407. }
  408. bool ct_reload_all(arg_t a)
  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. };