A Simple X Image Viewer
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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