A Simple X Image Viewer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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