A Simple X Image Viewer
 
 
 
 
 
 

565 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. 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_reverse_marks(arg_t a)
  226. {
  227. int i, cnt = mode == MODE_IMAGE ? filecnt : tns.cnt;
  228. for (i = 0; i < cnt; i++) {
  229. files[i].marked = !files[i].marked;
  230. markcnt += files[i].marked ? 1 : -1;
  231. }
  232. return true;
  233. }
  234. bool it_navigate_marked(arg_t a)
  235. {
  236. long n = (long) a;
  237. int d, i, cnt, sel, new;
  238. if (mode == MODE_IMAGE)
  239. cnt = filecnt, sel = new = fileidx;
  240. else
  241. cnt = tns.cnt, sel = new = tns.sel;
  242. if (prefix > 0)
  243. n *= prefix;
  244. d = n > 0 ? 1 : -1;
  245. for (i = sel + d; n != 0 && i >= 0 && i < cnt; i += d) {
  246. if (files[i].marked) {
  247. n -= d;
  248. new = i;
  249. }
  250. }
  251. if (new != sel) {
  252. if (mode == MODE_IMAGE) {
  253. load_image(new);
  254. } else {
  255. tns.sel = new;
  256. tns.dirty = true;
  257. }
  258. return true;
  259. } else {
  260. return false;
  261. }
  262. }
  263. bool it_scroll_move(arg_t a)
  264. {
  265. direction_t dir = (direction_t) a;
  266. if (mode == MODE_IMAGE)
  267. return img_pan(&img, dir, prefix);
  268. else
  269. return tns_move_selection(&tns, dir, prefix);
  270. }
  271. bool it_scroll_screen(arg_t a)
  272. {
  273. direction_t dir = (direction_t) a;
  274. if (mode == MODE_IMAGE)
  275. return img_pan(&img, dir, -1);
  276. else
  277. return tns_scroll(&tns, dir, true);
  278. }
  279. bool i_scroll_to_edge(arg_t a)
  280. {
  281. direction_t dir = (direction_t) a;
  282. if (mode == MODE_IMAGE)
  283. return img_pan_edge(&img, dir);
  284. else
  285. return false;
  286. }
  287. /* Xlib helper function for i_drag() */
  288. Bool is_motionnotify(Display *d, XEvent *e, XPointer a)
  289. {
  290. return e != NULL && e->type == MotionNotify;
  291. }
  292. #define WARP(x,y) \
  293. XWarpPointer(win.env.dpy, None, win.xwin, 0, 0, 0, 0, x, y); \
  294. ox = x, oy = y; \
  295. break
  296. bool i_drag(arg_t a)
  297. {
  298. int dx = 0, dy = 0, i, ox, oy, x, y;
  299. unsigned int ui;
  300. bool dragging = true, next = false;
  301. XEvent e;
  302. Window w;
  303. if (mode != MODE_IMAGE)
  304. return false;
  305. if (!XQueryPointer(win.env.dpy, win.xwin, &w, &w, &i, &i, &ox, &oy, &ui))
  306. return false;
  307. win_set_cursor(&win, CURSOR_HAND);
  308. while (dragging) {
  309. if (!next)
  310. XMaskEvent(win.env.dpy,
  311. ButtonPressMask | ButtonReleaseMask | PointerMotionMask, &e);
  312. switch (e.type) {
  313. case ButtonPress:
  314. case ButtonRelease:
  315. dragging = false;
  316. break;
  317. case MotionNotify:
  318. x = e.xmotion.x;
  319. y = e.xmotion.y;
  320. /* wrap the mouse around */
  321. if (x <= 0) {
  322. WARP(win.w - 2, y);
  323. } else if (x >= win.w - 1) {
  324. WARP(1, y);
  325. } else if (y <= 0) {
  326. WARP(x, win.h - 2);
  327. } else if (y >= win.h - 1) {
  328. WARP(x, 1);
  329. }
  330. dx += x - ox;
  331. dy += y - oy;
  332. ox = x;
  333. oy = y;
  334. break;
  335. }
  336. if (dragging)
  337. next = XCheckIfEvent(win.env.dpy, &e, is_motionnotify, None);
  338. if ((!dragging || !next) && (dx != 0 || dy != 0)) {
  339. if (img_move(&img, dx, dy)) {
  340. img_render(&img);
  341. win_draw(&win);
  342. }
  343. dx = dy = 0;
  344. }
  345. }
  346. win_set_cursor(&win, CURSOR_ARROW);
  347. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  348. reset_timeout(redraw);
  349. return false;
  350. }
  351. bool i_zoom(arg_t a)
  352. {
  353. long scale = (long) a;
  354. if (mode != MODE_IMAGE)
  355. return false;
  356. if (scale > 0)
  357. return img_zoom_in(&img);
  358. else if (scale < 0)
  359. return img_zoom_out(&img);
  360. else
  361. return false;
  362. }
  363. bool i_set_zoom(arg_t a)
  364. {
  365. if (mode == MODE_IMAGE)
  366. return img_zoom(&img, (prefix ? prefix : (long) a) / 100.0);
  367. else
  368. return false;
  369. }
  370. bool i_fit_to_win(arg_t a)
  371. {
  372. bool ret = false;
  373. scalemode_t sm = (scalemode_t) a;
  374. if (mode == MODE_IMAGE) {
  375. if ((ret = img_fit_win(&img, sm)))
  376. img_center(&img);
  377. }
  378. return ret;
  379. }
  380. bool i_fit_to_img(arg_t a)
  381. {
  382. int x, y;
  383. unsigned int w, h;
  384. bool ret = false;
  385. if (mode == MODE_IMAGE) {
  386. x = MAX(0, win.x + img.x);
  387. y = MAX(0, win.y + img.y);
  388. w = img.w * img.zoom;
  389. h = img.h * img.zoom;
  390. if ((ret = win_moveresize(&win, x, y, w, h))) {
  391. img.x = x - win.x;
  392. img.y = y - win.y;
  393. img.dirty = true;
  394. }
  395. }
  396. return ret;
  397. }
  398. bool i_rotate(arg_t a)
  399. {
  400. degree_t degree = (degree_t) a;
  401. if (mode == MODE_IMAGE) {
  402. img_rotate(&img, degree);
  403. return true;
  404. } else {
  405. return false;
  406. }
  407. }
  408. bool i_flip(arg_t a)
  409. {
  410. flipdir_t dir = (flipdir_t) a;
  411. if (mode == MODE_IMAGE) {
  412. img_flip(&img, dir);
  413. return true;
  414. } else {
  415. return false;
  416. }
  417. }
  418. bool i_toggle_antialias(arg_t a)
  419. {
  420. if (mode == MODE_IMAGE) {
  421. img_toggle_antialias(&img);
  422. return true;
  423. } else {
  424. return false;
  425. }
  426. }
  427. bool it_toggle_alpha(arg_t a)
  428. {
  429. img.alpha = tns.alpha = !img.alpha;
  430. if (mode == MODE_IMAGE)
  431. img.dirty = true;
  432. else
  433. tns.dirty = true;
  434. return true;
  435. }
  436. bool it_open_with(arg_t a)
  437. {
  438. const char *prog = (const char*) a;
  439. pid_t pid;
  440. if (prog == NULL || *prog == '\0')
  441. return false;
  442. if ((pid = fork()) == 0) {
  443. execlp(prog, prog,
  444. files[mode == MODE_IMAGE ? fileidx : tns.sel].path, NULL);
  445. warn("could not exec: %s", prog);
  446. exit(EXIT_FAILURE);
  447. } else if (pid < 0) {
  448. warn("could not fork. program was: %s", prog);
  449. }
  450. return false;
  451. }
  452. bool it_shell_cmd(arg_t a)
  453. {
  454. int n, status;
  455. const char *cmdline = (const char*) a;
  456. pid_t pid;
  457. if (cmdline == NULL || *cmdline == '\0')
  458. return false;
  459. n = mode == MODE_IMAGE ? fileidx : tns.sel;
  460. if (setenv("SXIV_IMG", files[n].path, 1) < 0) {
  461. warn("could not set env.-variable: SXIV_IMG. command line was: %s",
  462. cmdline);
  463. return false;
  464. }
  465. if ((pid = fork()) == 0) {
  466. execl("/bin/sh", "/bin/sh", "-c", cmdline, NULL);
  467. warn("could not exec: /bin/sh. command line was: %s", cmdline);
  468. exit(EXIT_FAILURE);
  469. } else if (pid < 0) {
  470. warn("could not fork. command line was: %s", cmdline);
  471. return false;
  472. }
  473. win_set_cursor(&win, CURSOR_WATCH);
  474. waitpid(pid, &status, 0);
  475. if (WIFEXITED(status) == 0 || WEXITSTATUS(status) != 0)
  476. warn("child exited with non-zero return value: %d. command line was: %s",
  477. WEXITSTATUS(status), cmdline);
  478. if (mode == MODE_IMAGE) {
  479. img_close(&img, true);
  480. load_image(fileidx);
  481. }
  482. if (!tns_load(&tns, n, &files[n], true, mode == MODE_IMAGE) &&
  483. mode == MODE_THUMB)
  484. {
  485. remove_file(tns.sel, false);
  486. tns.dirty = true;
  487. if (tns.sel >= tns.cnt)
  488. tns.sel = tns.cnt - 1;
  489. }
  490. return true;
  491. }