A Simple X Image Viewer
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. direction_t dir = (direction_t) a;
  351. if (mode == MODE_IMAGE) {
  352. if (dir == DIR_LEFT) {
  353. img_rotate_left(&img);
  354. return true;
  355. } else if (dir == DIR_RIGHT) {
  356. img_rotate_right(&img);
  357. return true;
  358. }
  359. }
  360. return false;
  361. }
  362. bool i_flip(arg_t a)
  363. {
  364. flipdir_t dir = (flipdir_t) a;
  365. if (mode == MODE_IMAGE) {
  366. img_flip(&img, dir);
  367. return true;
  368. } else {
  369. return false;
  370. }
  371. }
  372. bool i_toggle_antialias(arg_t a)
  373. {
  374. if (mode == MODE_IMAGE) {
  375. img_toggle_antialias(&img);
  376. return true;
  377. } else {
  378. return false;
  379. }
  380. }
  381. bool it_toggle_alpha(arg_t a)
  382. {
  383. img.alpha = tns.alpha = !img.alpha;
  384. if (mode == MODE_IMAGE)
  385. img.dirty = true;
  386. else
  387. tns.dirty = true;
  388. return true;
  389. }
  390. bool it_open_with(arg_t a)
  391. {
  392. const char *prog = (const char*) a;
  393. pid_t pid;
  394. if (prog == NULL || *prog == '\0')
  395. return false;
  396. if ((pid = fork()) == 0) {
  397. execlp(prog, prog,
  398. files[mode == MODE_IMAGE ? fileidx : tns.sel].path, NULL);
  399. warn("could not exec: %s", prog);
  400. exit(EXIT_FAILURE);
  401. } else if (pid < 0) {
  402. warn("could not fork. program was: %s", prog);
  403. }
  404. return false;
  405. }
  406. bool it_shell_cmd(arg_t a)
  407. {
  408. int n, status;
  409. const char *cmdline = (const char*) a;
  410. pid_t pid;
  411. if (cmdline == NULL || *cmdline == '\0')
  412. return false;
  413. n = mode == MODE_IMAGE ? fileidx : tns.sel;
  414. if (setenv("SXIV_IMG", files[n].path, 1) < 0) {
  415. warn("could not set env.-variable: SXIV_IMG. command line was: %s",
  416. cmdline);
  417. return false;
  418. }
  419. if ((pid = fork()) == 0) {
  420. execl("/bin/sh", "/bin/sh", "-c", cmdline, NULL);
  421. warn("could not exec: /bin/sh. command line was: %s", cmdline);
  422. exit(EXIT_FAILURE);
  423. } else if (pid < 0) {
  424. warn("could not fork. command line was: %s", cmdline);
  425. return false;
  426. }
  427. win_set_cursor(&win, CURSOR_WATCH);
  428. waitpid(pid, &status, 0);
  429. if (WIFEXITED(status) == 0 || WEXITSTATUS(status) != 0)
  430. warn("child exited with non-zero return value: %d. command line was: %s",
  431. WEXITSTATUS(status), cmdline);
  432. if (mode == MODE_IMAGE) {
  433. img_close(&img, true);
  434. load_image(fileidx);
  435. }
  436. if (!tns_load(&tns, n, &files[n], true, mode == MODE_IMAGE) &&
  437. mode == MODE_THUMB)
  438. {
  439. remove_file(tns.sel, false);
  440. tns.dirty = true;
  441. if (tns.sel >= tns.cnt)
  442. tns.sel = tns.cnt - 1;
  443. }
  444. return true;
  445. }