A Simple X Image Viewer
 
 
 
 
 
 

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