From 6ea43a3213bb264525e04c729c67204f82c7a2c8 Mon Sep 17 00:00:00 2001
From: Daniel Friesel <derf@finalrewind.org>
Date: Fri, 1 Feb 2013 17:46:50 +0100
Subject: support "feh -" to read from stdin. closes #118

---
 ChangeLog      |  1 +
 man/feh.pre    |  4 ++++
 src/filelist.c |  5 +++++
 src/imlib.c    | 40 +++++++++++++++++++++++++++++++++++++++-
 4 files changed, 49 insertions(+), 1 deletion(-)

diff --git a/ChangeLog b/ChangeLog
index 29238dd..561abfd 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -3,6 +3,7 @@ git HEAD
       images (patch by sdaau). Press 'k' to toggle it.
     * Add --sort mtime option (patch by guns)
     * Add a desktop file (installed to share/applications/feh.desktop)
+    * Use "feh -" to read image from stdin
 
 Mon, 24 Dec 2012 15:45:54 +0100  Daniel Friesel <derf+feh@finalrewind.org>
 
diff --git a/man/feh.pre b/man/feh.pre
index cecc58f..a1df25a 100644
--- a/man/feh.pre
+++ b/man/feh.pre
@@ -72,6 +72,10 @@ image in it, the keyboard and mouse can be used to change slides
 In slideshow mode, images can be deleted either from the filelist or from the
 disk, the new filelist can then be saved to the disk and reopened at a later
 time.
+An image can also be read from stdin via
+.Qq feh - .
+However, you should not combine reading from stdin with normal file / filelist
+usage.
 .
 .Pp
 .
diff --git a/src/filelist.c b/src/filelist.c
index 8f3bfe9..3ea0928 100644
--- a/src/filelist.c
+++ b/src/filelist.c
@@ -185,6 +185,11 @@ void add_file_to_filelist_recursively(char *origpath, unsigned char level)
 			/* We'll download it later... */
 			free(path);
 			return;
+		} else if ((len == 1) && (path[0] == '-')) {
+			D(("Addig stdin (-) to filelist\n"));
+			filelist = gib_list_add_front(filelist, feh_file_new(path));
+			free(path);
+			return;
 		} else if (opt.filelistfile) {
 			char *newpath = feh_absolute_path(path);
 
diff --git a/src/imlib.c b/src/imlib.c
index e9f92ad..bdf54ac 100644
--- a/src/imlib.c
+++ b/src/imlib.c
@@ -61,6 +61,7 @@ int num_xinerama_screens;
 
 int childpid = 0;
 
+static char *feh_stdin_load_image();
 static char *feh_http_load_image(char *url);
 static char *feh_magick_load_image(char *filename);
 
@@ -209,7 +210,8 @@ void feh_imlib_print_load_error(char *file, winwidget w, Imlib_Load_Error err)
 int feh_load_image(Imlib_Image * im, feh_file * file)
 {
 	Imlib_Load_Error err;
-	enum { SRC_IMLIB, SRC_HTTP, SRC_MAGICK } image_source = SRC_IMLIB;
+	enum { SRC_IMLIB, SRC_HTTP, SRC_MAGICK, SRC_STDIN } image_source =
+		SRC_IMLIB;
 	char *tmpname = NULL;
 	char *real_filename = NULL;
 
@@ -225,6 +227,10 @@ int feh_load_image(Imlib_Image * im, feh_file * file)
 
 		tmpname = feh_http_load_image(file->filename);
 	}
+	if ((strlen(file->filename) == 1) && (file->filename[0] == '-')) {
+		image_source = SRC_STDIN;
+		tmpname = feh_stdin_load_image();
+	}
 	else
 		*im = imlib_load_image_with_error_return(file->filename, &err);
 
@@ -269,6 +275,38 @@ int feh_load_image(Imlib_Image * im, feh_file * file)
 	return(1);
 }
 
+static char *feh_stdin_load_image()
+{
+	char buf[1024];
+	size_t readsize;
+	char *sfn = estrjoin("_", "/tmp/feh_stdin", "XXXXXX", NULL);
+	int fd = mkstemp(sfn);
+	FILE *outfile;
+
+	if (fd == -1) {
+		free(sfn);
+		return NULL;
+	}
+
+	outfile = fdopen(fd, "w");
+
+	if (outfile == NULL) {
+		free(sfn);
+		return NULL;
+	}
+
+	while ((readsize = fread(buf, sizeof(char), sizeof(buf), stdin)) > 0) {
+		if (fwrite(buf, sizeof(char), readsize, outfile) < readsize) {
+			free(sfn);
+			return NULL;
+		}
+	}
+
+	fclose(outfile);
+
+	return sfn;
+}
+
 static char *feh_magick_load_image(char *filename)
 {
 	char argv_fd[12];
-- 
cgit v1.2.3