source: libcaca/trunk/src/cacaplay.c @ 1352

Last change on this file since 1352 was 1352, checked in by Sam Hocevar, 14 years ago
  • Allow cacaplay to play streams and updated its manpage.
  • Property svn:keywords set to Id
File size: 2.2 KB
Line 
1/*
2 *  cacaplay      caca file player
3 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: cacaplay.c 1352 2006-11-12 09:48:37Z sam $
7 *
8 *  This program is free software; you can redistribute it and/or
9 *  modify it under the terms of the Do What The Fuck You Want To
10 *  Public License, Version 2, as published by Sam Hocevar. See
11 *  http://sam.zoy.org/wtfpl/COPYING for more details.
12 */
13
14#include "config.h"
15#include "common.h"
16
17#if !defined(__KERNEL__)
18#   include <stdio.h>
19#   include <stdlib.h>
20#   include <sys/types.h>
21#   include <sys/stat.h>
22#   include <fcntl.h>
23#   include <string.h>
24#   include <unistd.h>
25#endif
26
27#include "cucul.h"
28#include "caca.h"
29
30int main(int argc, char **argv)
31{
32    cucul_canvas_t *cv, *app;
33    caca_display_t *dp;
34    unsigned char *buf = NULL;
35    long int bytes, total = 0;
36    int fd;
37
38    if(argc < 2 || !strcmp(argv[1], "-"))
39    {
40        fd = 0; /* use stdin */
41    }
42    else
43    {
44        fd = open(argv[1], O_RDONLY);
45        if(fd < 0)
46        {
47            fprintf(stderr, "%s: could not open `%s'.\n", argv[0], argv[1]);
48            return 1;
49        }
50    }
51
52    cv = cucul_create_canvas(0, 0);
53    app = cucul_create_canvas(0, 0);
54
55    dp = caca_create_display(cv);
56
57    for(;;)
58    {
59        caca_event_t ev;
60        int ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, 0);
61
62        if(ret && ev.type & CACA_EVENT_KEY_PRESS)
63            break;
64
65        bytes = cucul_import_memory(app, buf, total, "caca");
66
67        if(bytes > 0)
68        {
69            total -= bytes;
70            memmove(buf, buf + bytes, total);
71
72            cucul_blit(cv, 0, 0, app, NULL);
73            caca_refresh_display(dp);
74        }
75        else if(bytes == 0)
76        {
77            ssize_t n;
78            buf = realloc(buf, total + 128);
79            n = read(fd, buf + total, 128);
80            if(n < 0)
81            {
82                fprintf(stderr, "%s: read error\n", argv[0]);
83                return -1;
84            }
85            total += n;
86        }
87        else /* bytes < 0 */
88        {
89            fprintf(stderr, "%s: corrupted caca file\n", argv[0]);
90            return -1;
91        }
92    }
93
94    /* Clean up */
95    close(fd);
96
97    caca_free_display(dp);
98    cucul_free_canvas(cv);
99
100    return 0;
101}
102
Note: See TracBrowser for help on using the repository browser.