A Simple X Image Viewer
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

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