From bb577d274dbd9981a726092a504466bc9b5c9bdb Mon Sep 17 00:00:00 2001
From: Bert <ber.t@gmx.com>
Date: Fri, 19 Aug 2011 18:46:17 +0200
Subject: [PATCH] Added command to toggle gif animations

---
 commands.c | 22 ++++++++++++++++++++--
 commands.h |  1 +
 config.h   |  7 ++++---
 image.c    | 18 +++++++-----------
 main.c     |  8 ++++++--
 5 files changed, 38 insertions(+), 18 deletions(-)

diff --git a/commands.c b/commands.c
index b0f7b08..51f7a55 100644
--- a/commands.c
+++ b/commands.c
@@ -41,7 +41,7 @@ extern int filecnt, fileidx;
 
 extern int timo_cursor;
 extern int timo_redraw;
-extern int timo_delay;
+extern int timo_adelay;
 
 int it_quit(arg_t a) {
 	cleanup();
@@ -149,7 +149,24 @@ int it_last(arg_t a) {
 }
 
 int i_navigate_frame(arg_t a) {
-	return img_frame_navigate(&img, (int) a);
+	if (mode == MODE_IMAGE && !img.multi.animate)
+		return img_frame_navigate(&img, (int) a);
+	else
+		return 0;
+}
+
+int i_toggle_animation(arg_t a) {
+	if (mode != MODE_IMAGE)
+		return 0;
+
+	if (img.multi.animate) {
+		timo_adelay = 0;
+		img.multi.animate = 0;
+		return 0;
+	} else {
+		timo_adelay = img_frame_animate(&img, 1);
+		return 1;
+	}
 }
 
 int it_move(arg_t a) {
@@ -239,6 +256,7 @@ int i_zoom(arg_t a) {
 
 	if (mode != MODE_IMAGE)
 		return 0;
+
 	if (scale > 0)
 		return img_zoom_in(&img, &win);
 	else if (scale < 0)
diff --git a/commands.h b/commands.h
index ee2e07a..c7e58ae 100644
--- a/commands.h
+++ b/commands.h
@@ -48,6 +48,7 @@ int i_navigate(arg_t);
 int it_first(arg_t);
 int it_last(arg_t);
 int i_navigate_frame(arg_t);
+int i_toggle_animation(arg_t);
 int it_move(arg_t);
 int i_pan_screen(arg_t);
 int i_pan_edge(arg_t);
diff --git a/config.h b/config.h
index 07371c9..46e30c6 100644
--- a/config.h
+++ b/config.h
@@ -29,7 +29,7 @@ static const float zoom_levels[] = {
 	100.0, 150.0, 200.0, 400.0, 800.0
 };
 
-/* default settings for gif images: */
+/* default settings for multi-frame gif images: */
 enum {
 	GIF_DELAY    = 100, /* delay time (in ms) */
 	GIF_AUTOPLAY = 1,   /* autoplay when loaded [0/1] */
@@ -64,8 +64,9 @@ static const keymap_t keys[] = {
 	{ False,  XK_g,             it_first,             (arg_t) None },
 	{ False,  XK_G,             it_last,              (arg_t) None },
 
-	{ False,  XK_N,             i_navigate_frame,     (arg_t) +1 },
-	{ False,  XK_P,             i_navigate_frame,     (arg_t) -1 },
+	{ True,   XK_n,             i_navigate_frame,     (arg_t) +1 },
+	{ True,   XK_p,             i_navigate_frame,     (arg_t) -1 },
+	{ True,   XK_space,         i_toggle_animation,   (arg_t) None },
 
 	{ False,  XK_h,             it_move,              (arg_t) DIR_LEFT },
 	{ False,  XK_Left,          it_move,              (arg_t) DIR_LEFT },
diff --git a/image.c b/image.c
index 178ace3..efd4569 100644
--- a/image.c
+++ b/image.c
@@ -596,22 +596,18 @@ int img_frame_animate(img_t *img, int restart) {
 	if (!img || !img->multi.cnt)
 		return 0;
 
-	if (!img->multi.animate && !restart)
-		return 0;
-
-	if (restart) {
-		img_frame_goto(img, 0);
-		img->multi.animate = 1;
-	} else if (img->multi.sel + 1 >= img->multi.cnt) {
-		if (!GIF_LOOP) {
+	if (img->multi.sel + 1 >= img->multi.cnt) {
+		if (restart || GIF_LOOP) {
+			img_frame_goto(img, 0);
+		} else {
 			img->multi.animate = 0;
 			return 0;
-		} else {
-			img_frame_goto(img, 0);
 		}
-	} else {
+	} else if (!restart) {
 		img_frame_goto(img, img->multi.sel + 1);
 	}
 
+	img->multi.animate = 1;
+
 	return img->multi.frames[img->multi.sel].delay;
 }
diff --git a/main.c b/main.c
index 3e2f353..6545cfb 100644
--- a/main.c
+++ b/main.c
@@ -142,8 +142,12 @@ void load_image(int new) {
 	else
 		filesize = 0;
 
-	if (img.multi.cnt && img.multi.animate)
-		timo_adelay = img.multi.frames[img.multi.sel].delay;
+	if (img.multi.cnt) {
+		if (img.multi.animate)
+			timo_adelay = img.multi.frames[img.multi.sel].delay;
+		else
+			timo_adelay = 0;
+	}
 }
 
 void update_title() {