From: Michael Gernoth <michael@gernoth.net>
Date: Sun, 6 Jun 2010 12:54:29 +0000 (+0200)
Subject: new "display" command to just show the display content
X-Git-Url: https://git.zerfleddert.de/cgi-bin/gitweb.cgi/rigol/commitdiff_plain/616d54b715cbfdd3547724e19fff416046fdc44d

new "display" command to just show the display content
---

diff --git a/commands.c b/commands.c
index f5b3973..646d3de 100644
--- a/commands.c
+++ b/commands.c
@@ -106,17 +106,11 @@ void do_get_buf (struct usb_dev_handle *sc)
 	usbtmc_sendscpi (sc, ":TIM:OFFSET 0", NULL, 0);
 }
 
-void do_get_screen(struct usb_dev_handle *sc)
+static unsigned char* get_lcd(struct usb_dev_handle *sc, int *imglen)
 {
 	unsigned char screen[320*234];
-	time_t lt;
-	char filename[256];
 	unsigned char *png;
-	int imglen;
-	int ret;
 	int l;
-	int fd;
-	pid_t display;
 
 	/* Hide "RMT" from screen */
 	l = usbtmc_sendscpi(sc, ":KEY:LOCK DISABLE", NULL, 0); 
@@ -128,21 +122,40 @@ void do_get_screen(struct usb_dev_handle *sc)
 		printf ("hmm. didnt' get %d bytes, but %d\n\n", sizeof(screen), l); 
 	}
 
-	png = lcd2png(screen, &imglen);
+	png = lcd2png(screen, imglen);
+
+	return png;
+}
+
+void do_get_screen(struct usb_dev_handle *sc)
+{
+	time_t lt;
+	char filename[256];
+	unsigned char *png;
+	int imglen;
+	int ret;
+	int fd;
+	pid_t display;
+
+	png = get_lcd(sc, &imglen);
+	if (png == NULL) {
+		perror("get_lcd");
+		return;
+	}
 
 	lt = time(NULL);
 	strftime(filename, sizeof(filename), "screen_%Y%m%d-%H%M%S.png", localtime(&lt));
 	fd=open(filename, O_CREAT|O_WRONLY, 0644);
 	if (fd == -1) {
 		perror("open");
-		exit(EXIT_FAILURE);
+		return;
 	}
 
 	while(imglen > 0) {
 		ret = write(fd, png, imglen);
 		if (ret == -1) {
 			perror("write");
-			exit(EXIT_FAILURE);
+			return;
 		}
 		imglen -= ret;
 	}
@@ -165,3 +178,49 @@ void do_get_screen(struct usb_dev_handle *sc)
 	}
 }
 
+void do_display_screen(struct usb_dev_handle *sc)
+{
+	unsigned char *png;
+	int imglen;
+	int ret;
+	int pipefd[2];
+	pid_t display;
+
+	png = get_lcd(sc, &imglen);
+	if (png == NULL) {
+		perror("get_lcd");
+		return;
+	}
+
+	if (pipe(pipefd) == -1) {
+		perror("pipe");
+		return;
+	}
+
+	display = fork();
+	switch(display) {
+		case 0:
+			close(pipefd[1]);
+			close(STDIN_FILENO);
+			dup2(pipefd[0], STDIN_FILENO);
+			execlp("display", "display", "-", NULL);
+			exit(0);
+			break;
+		case -1:
+			perror("fork");
+			break;
+		default:
+			close(pipefd[0]);
+			while(imglen > 0) {
+				ret = write(pipefd[1], png, imglen);
+				if (ret == -1) {
+					perror("write");
+					exit(EXIT_FAILURE);
+				}
+				imglen -= ret;
+			}
+			close(pipefd[1]);
+			break;
+	}
+}
+
diff --git a/rigol.c b/rigol.c
index 22a5cea..93b8566 100644
--- a/rigol.c
+++ b/rigol.c
@@ -135,11 +135,15 @@ int main(int argc, char **argv)
 			continue;
 		}
 		if (strncmp (scpi, "databuf", 7) == 0) {
-			do_get_buf (sc);
+			do_get_buf(sc);
 			continue;
 		}
 		if (strncmp (scpi, "screen", 6) == 0) {
-			do_get_screen (sc);
+			do_get_screen(sc);
+			continue;
+		}
+		if (strncmp (scpi, "display", 7) == 0) {
+			do_display_screen(sc);
 			continue;
 		}