From 35f77b6e06c19970eec55ff3ba6686ee77643bae Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sun, 12 Nov 2006 09:48:37 +0000 Subject: [PATCH] * Allow cacaplay to play streams and updated its manpage. --- doc/cacaplay.1 | 14 +++++----- src/cacaplay.c | 74 +++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 65 insertions(+), 23 deletions(-) diff --git a/doc/cacaplay.1 b/doc/cacaplay.1 index befd7d9..f5c598c 100644 --- a/doc/cacaplay.1 +++ b/doc/cacaplay.1 @@ -12,14 +12,14 @@ program by setting the environment variable to .B "raw" and storing the program's standard output. -.SH EXAMPLE -cacaplay file.caca -.SH BUGS -As of now, + +If no file argument is provided or '\-' is used, .B cacaplay -can only play single-image canvases, rendering it pretty useless. See -.B cacaserver -for a more usable alternative. +will read its data from the standard input. +.SH EXAMPLES +cacaplay file.caca + +CACA_DRIVER=raw CACA_GEOMETRY=80x32 cacademo | cacaplay .SH SEE ALSO cacaserver(1) .SH AUTHOR diff --git a/src/cacaplay.c b/src/cacaplay.c index 4fd319b..ff855ef 100644 --- a/src/cacaplay.c +++ b/src/cacaplay.c @@ -15,8 +15,13 @@ #include "common.h" #if !defined(__KERNEL__) - #include - #include +# include +# include +# include +# include +# include +# include +# include #endif #include "cucul.h" @@ -24,34 +29,71 @@ int main(int argc, char **argv) { - caca_event_t ev; - cucul_canvas_t *cv; + cucul_canvas_t *cv, *app; caca_display_t *dp; + unsigned char *buf = NULL; + long int bytes, total = 0; + int fd; - if(argc < 2) + if(argc < 2 || !strcmp(argv[1], "-")) { - fprintf(stderr, "%s: missing argument (filename).\n", argv[0]); - return 1; + fd = 0; /* use stdin */ } - - cv = cucul_create_canvas(0, 0); - if(cucul_import_file(cv, argv[1], "caca") < 0) + else { - fprintf(stderr, "%s: could not import file %s.\n", argv[0], argv[1]); - return 1; + fd = open(argv[1], O_RDONLY); + if(fd < 0) + { + fprintf(stderr, "%s: could not open `%s'.\n", argv[0], argv[1]); + return 1; + } } - dp = caca_create_display(cv); + cv = cucul_create_canvas(0, 0); + app = cucul_create_canvas(0, 0); - caca_refresh_display(dp); + dp = caca_create_display(cv); - while(caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, -1)) + for(;;) { - if(ev.data.key.ch == CACA_KEY_ESCAPE) + caca_event_t ev; + int ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, 0); + + if(ret && ev.type & CACA_EVENT_KEY_PRESS) break; + + bytes = cucul_import_memory(app, buf, total, "caca"); + + if(bytes > 0) + { + total -= bytes; + memmove(buf, buf + bytes, total); + + cucul_blit(cv, 0, 0, app, NULL); + caca_refresh_display(dp); + } + else if(bytes == 0) + { + ssize_t n; + buf = realloc(buf, total + 128); + n = read(fd, buf + total, 128); + if(n < 0) + { + fprintf(stderr, "%s: read error\n", argv[0]); + return -1; + } + total += n; + } + else /* bytes < 0 */ + { + fprintf(stderr, "%s: corrupted caca file\n", argv[0]); + return -1; + } } /* Clean up */ + close(fd); + caca_free_display(dp); cucul_free_canvas(cv);