A Simple X Image Viewer
 
 
 
 
 
 

478 lines
8.5 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. #include "sxiv.h"
  19. #define _IMAGE_CONFIG
  20. #include "config.h"
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include <sys/wait.h>
  25. void remove_file(int, bool);
  26. void load_image(int);
  27. void close_info(void);
  28. void open_info(void);
  29. int ptr_third_x(void);
  30. void redraw(void);
  31. void reset_cursor(void);
  32. void animate(void);
  33. void slideshow(void);
  34. void set_timeout(timeout_f, int, bool);
  35. void reset_timeout(timeout_f);
  36. extern appmode_t mode;
  37. extern img_t img;
  38. extern tns_t tns;
  39. extern win_t win;
  40. extern fileinfo_t *files;
  41. extern int filecnt, fileidx;
  42. extern int alternate;
  43. extern int markcnt;
  44. extern int toggledidx;
  45. extern int prefix;
  46. extern bool extprefix;
  47. bool cg_quit(arg_t _)
  48. {
  49. unsigned int i;
  50. if (options->to_stdout && markcnt > 0) {
  51. for (i = 0; i < filecnt; i++) {
  52. if (files[i].flags & FF_MARK)
  53. printf("%s\n", files[i].name);
  54. }
  55. }
  56. exit(EXIT_SUCCESS);
  57. }
  58. bool cg_switch_mode(arg_t _)
  59. {
  60. if (mode == MODE_IMAGE) {
  61. if (tns.thumbs == NULL)
  62. tns_init(&tns, files, &filecnt, &fileidx, &win);
  63. img_close(&img, false);
  64. reset_timeout(reset_cursor);
  65. if (img.ss.on) {
  66. img.ss.on = false;
  67. reset_timeout(slideshow);
  68. }
  69. tns.dirty = true;
  70. mode = MODE_THUMB;
  71. } else {
  72. load_image(fileidx);
  73. mode = MODE_IMAGE;
  74. }
  75. return true;
  76. }
  77. bool cg_toggle_fullscreen(arg_t _)
  78. {
  79. win_toggle_fullscreen(&win);
  80. /* redraw after next ConfigureNotify event */
  81. set_timeout(redraw, TO_REDRAW_RESIZE, false);
  82. if (mode == MODE_IMAGE)
  83. img.checkpan = img.dirty = true;
  84. else
  85. tns.dirty = true;
  86. return false;
  87. }
  88. bool cg_toggle_bar(arg_t _)
  89. {
  90. win_toggle_bar(&win);
  91. if (mode == MODE_IMAGE) {
  92. if (win.bar.h > 0)
  93. open_info();
  94. else
  95. close_info();
  96. img.checkpan = img.dirty = true;
  97. } else {
  98. tns.dirty = true;
  99. }
  100. return true;
  101. }
  102. bool cg_prefix_external(arg_t _)
  103. {
  104. extprefix = true;
  105. return false;
  106. }
  107. bool cg_reload_image(arg_t _)
  108. {
  109. if (mode == MODE_IMAGE) {
  110. load_image(fileidx);
  111. } else {
  112. win_set_cursor(&win, CURSOR_WATCH);
  113. if (!tns_load(&tns, fileidx, true, false)) {
  114. remove_file(fileidx, false);
  115. tns.dirty = true;
  116. }
  117. }
  118. return true;
  119. }
  120. bool cg_remove_image(arg_t _)
  121. {
  122. remove_file(fileidx, true);
  123. if (mode == MODE_IMAGE)
  124. load_image(fileidx);
  125. else
  126. tns.dirty = true;
  127. return true;
  128. }
  129. bool cg_first(arg_t _)
  130. {
  131. if (mode == MODE_IMAGE && fileidx != 0) {
  132. load_image(0);
  133. return true;
  134. } else if (mode == MODE_THUMB && fileidx != 0) {
  135. fileidx = 0;
  136. tns.dirty = true;
  137. return true;
  138. } else {
  139. return false;
  140. }
  141. }
  142. bool cg_n_or_last(arg_t _)
  143. {
  144. int n = prefix != 0 && prefix - 1 < filecnt ? prefix - 1 : filecnt - 1;
  145. if (mode == MODE_IMAGE && fileidx != n) {
  146. load_image(n);
  147. return true;
  148. } else if (mode == MODE_THUMB && fileidx != n) {
  149. fileidx = n;
  150. tns.dirty = true;
  151. return true;
  152. } else {
  153. return false;
  154. }
  155. }
  156. bool cg_scroll_screen(arg_t dir)
  157. {
  158. if (mode == MODE_IMAGE)
  159. return img_pan(&img, dir, -1);
  160. else
  161. return tns_scroll(&tns, dir, true);
  162. }
  163. bool cg_zoom(arg_t d)
  164. {
  165. if (mode == MODE_THUMB)
  166. return tns_zoom(&tns, d);
  167. else if (d > 0)
  168. return img_zoom_in(&img);
  169. else if (d < 0)
  170. return img_zoom_out(&img);
  171. else
  172. return false;
  173. }
  174. bool cg_toggle_image_mark(arg_t _)
  175. {
  176. files[fileidx].flags ^= FF_MARK;
  177. markcnt += files[fileidx].flags & FF_MARK ? 1 : -1;
  178. if (mode == MODE_THUMB)
  179. tns_mark(&tns, fileidx, !!(files[fileidx].flags & FF_MARK));
  180. toggledidx = fileidx;
  181. return true;
  182. }
  183. bool cg_reverse_marks(arg_t _)
  184. {
  185. int i;
  186. for (i = 0; i < filecnt; i++) {
  187. files[i].flags ^= FF_MARK;
  188. markcnt += files[i].flags & FF_MARK ? 1 : -1;
  189. }
  190. if (mode == MODE_THUMB)
  191. tns.dirty = true;
  192. return true;
  193. }
  194. bool cg_mark_range(arg_t _)
  195. {
  196. int i, from, to;
  197. from = toggledidx;
  198. to = fileidx;
  199. if (fileidx < toggledidx) {
  200. from = fileidx;
  201. to = toggledidx;
  202. }
  203. for (i = from; i <= to; i++) {
  204. if (files[toggledidx].flags & FF_MARK) {
  205. if (!(files[i].flags & FF_MARK)) {
  206. files[i].flags |= FF_MARK;
  207. markcnt += 1;
  208. }
  209. } else {
  210. if (files[i].flags & FF_MARK) {
  211. files[i].flags &= ~FF_MARK;
  212. markcnt -= 1;
  213. }
  214. }
  215. if (mode == MODE_THUMB)
  216. tns_mark(&tns, i, !!(files[i].flags & FF_MARK));
  217. }
  218. return true;
  219. }
  220. bool cg_unmark_all(arg_t _)
  221. {
  222. int i;
  223. for (i = 0; i < filecnt; i++)
  224. files[i].flags &= ~FF_MARK;
  225. markcnt = 0;
  226. if (mode == MODE_THUMB)
  227. tns.dirty = true;
  228. return true;
  229. }
  230. bool cg_navigate_marked(arg_t n)
  231. {
  232. int d, i;
  233. int new = fileidx;
  234. if (prefix > 0)
  235. n *= prefix;
  236. d = n > 0 ? 1 : -1;
  237. for (i = fileidx + d; n != 0 && i >= 0 && i < filecnt; i += d) {
  238. if (files[i].flags & FF_MARK) {
  239. n -= d;
  240. new = i;
  241. }
  242. }
  243. if (new != fileidx) {
  244. if (mode == MODE_IMAGE) {
  245. load_image(new);
  246. } else {
  247. fileidx = new;
  248. tns.dirty = true;
  249. }
  250. return true;
  251. } else {
  252. return false;
  253. }
  254. }
  255. bool cg_change_gamma(arg_t d)
  256. {
  257. if (img_change_gamma(&img, d * (prefix > 0 ? prefix : 1))) {
  258. if (mode == MODE_THUMB)
  259. tns.dirty = true;
  260. return true;
  261. } else {
  262. return false;
  263. }
  264. }
  265. bool ci_navigate(arg_t n)
  266. {
  267. if (prefix > 0)
  268. n *= prefix;
  269. n += fileidx;
  270. if (n < 0)
  271. n = 0;
  272. if (n >= filecnt)
  273. n = filecnt - 1;
  274. if (n != fileidx) {
  275. load_image(n);
  276. return true;
  277. } else {
  278. return false;
  279. }
  280. }
  281. bool ci_cursor_navigate(arg_t _)
  282. {
  283. return ci_navigate(ptr_third_x() - 1);
  284. }
  285. bool ci_alternate(arg_t _)
  286. {
  287. load_image(alternate);
  288. return true;
  289. }
  290. bool ci_navigate_frame(arg_t d)
  291. {
  292. if (prefix > 0)
  293. d *= prefix;
  294. return !img.multi.animate && img_frame_navigate(&img, d);
  295. }
  296. bool ci_toggle_animation(arg_t _)
  297. {
  298. bool dirty = false;
  299. if (img.multi.cnt > 0) {
  300. img.multi.animate = !img.multi.animate;
  301. if (img.multi.animate) {
  302. dirty = img_frame_animate(&img);
  303. set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
  304. } else {
  305. reset_timeout(animate);
  306. }
  307. }
  308. return dirty;
  309. }
  310. bool ci_scroll(arg_t dir)
  311. {
  312. return img_pan(&img, dir, prefix);
  313. }
  314. bool ci_scroll_to_edge(arg_t dir)
  315. {
  316. return img_pan_edge(&img, dir);
  317. }
  318. bool ci_drag(arg_t mode)
  319. {
  320. int x, y, ox, oy;
  321. float px, py;
  322. XEvent e;
  323. if ((int)(img.w * img.zoom) <= win.w && (int)(img.h * img.zoom) <= win.h)
  324. return false;
  325. win_set_cursor(&win, CURSOR_DRAG);
  326. win_cursor_pos(&win, &x, &y);
  327. ox = x;
  328. oy = y;
  329. for (;;) {
  330. if (mode == DRAG_ABSOLUTE) {
  331. px = MIN(MAX(0.0, x - win.w*0.1), win.w*0.8) / (win.w*0.8)
  332. * (win.w - img.w * img.zoom);
  333. py = MIN(MAX(0.0, y - win.h*0.1), win.h*0.8) / (win.h*0.8)
  334. * (win.h - img.h * img.zoom);
  335. } else {
  336. px = img.x + x - ox;
  337. py = img.y + y - oy;
  338. }
  339. if (img_pos(&img, px, py)) {
  340. img_render(&img);
  341. win_draw(&win);
  342. }
  343. XMaskEvent(win.env.dpy,
  344. ButtonPressMask | ButtonReleaseMask | PointerMotionMask, &e);
  345. if (e.type == ButtonPress || e.type == ButtonRelease)
  346. break;
  347. while (XCheckTypedEvent(win.env.dpy, MotionNotify, &e));
  348. ox = x;
  349. oy = y;
  350. x = e.xmotion.x;
  351. y = e.xmotion.y;
  352. }
  353. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  354. reset_cursor();
  355. return true;
  356. }
  357. bool ci_set_zoom(arg_t zl)
  358. {
  359. return img_zoom(&img, (prefix ? prefix : zl) / 100.0);
  360. }
  361. bool ci_fit_to_win(arg_t sm)
  362. {
  363. return img_fit_win(&img, sm);
  364. }
  365. bool ci_rotate(arg_t degree)
  366. {
  367. img_rotate(&img, degree);
  368. return true;
  369. }
  370. bool ci_flip(arg_t dir)
  371. {
  372. img_flip(&img, dir);
  373. return true;
  374. }
  375. bool ci_toggle_antialias(arg_t _)
  376. {
  377. img_toggle_antialias(&img);
  378. return true;
  379. }
  380. bool ci_toggle_alpha(arg_t _)
  381. {
  382. img.alpha = !img.alpha;
  383. img.dirty = true;
  384. return true;
  385. }
  386. bool ci_slideshow(arg_t _)
  387. {
  388. if (prefix > 0) {
  389. img.ss.on = true;
  390. img.ss.delay = prefix * 10;
  391. set_timeout(slideshow, img.ss.delay * 100, true);
  392. } else if (img.ss.on) {
  393. img.ss.on = false;
  394. reset_timeout(slideshow);
  395. } else {
  396. img.ss.on = true;
  397. }
  398. return true;
  399. }
  400. bool ct_move_sel(arg_t dir)
  401. {
  402. return tns_move_selection(&tns, dir, prefix);
  403. }
  404. bool ct_reload_all(arg_t _)
  405. {
  406. tns_free(&tns);
  407. tns_init(&tns, files, &filecnt, &fileidx, &win);
  408. tns.dirty = true;
  409. return true;
  410. }
  411. #undef G_CMD
  412. #define G_CMD(c) { -1, cg_##c },
  413. #undef I_CMD
  414. #define I_CMD(c) { MODE_IMAGE, ci_##c },
  415. #undef T_CMD
  416. #define T_CMD(c) { MODE_THUMB, ct_##c },
  417. const cmd_t cmds[CMD_COUNT] = {
  418. #include "commands.lst"
  419. };