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.
 
 
 
 
 
 

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