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

Last change on this file since 1355 was 1355, checked in by Sam Hocevar, 14 years ago
  • Properly handle EOF in cacaplay.
  • Property svn:keywords set to Id
File size: 2.4 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 1355 2006-11-12 10:06:35Z 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 = 0, 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        int eof = 0;
62
63        if(ret && ev.type & CACA_EVENT_KEY_PRESS)
64            break;
65
66        if(bytes == 0)
67        {
68            ssize_t n;
69            buf = realloc(buf, total + 1);
70            n = read(fd, buf + total, 1);
71            if(n < 0)
72            {
73                fprintf(stderr, "%s: read error\n", argv[0]);
74                return -1;
75            }
76            else if(n == 0)
77            {
78                eof = 1;
79            }
80            total += n;
81        }
82
83        bytes = cucul_import_memory(app, buf, total, "caca");
84
85        if(bytes > 0)
86        {
87            total -= bytes;
88            memmove(buf, buf + bytes, total);
89
90            cucul_blit(cv, 0, 0, app, NULL);
91            caca_refresh_display(dp);
92        }
93        else if(bytes < 0)
94        {
95            fprintf(stderr, "%s: corrupted caca file\n", argv[0]);
96            break;
97        }
98
99        if(eof)
100            break;
101    }
102
103    caca_get_event(dp, CACA_EVENT_KEY_PRESS, NULL, -1);
104
105    /* Clean up */
106    close(fd);
107
108    caca_free_display(dp);
109    cucul_free_canvas(cv);
110
111    return 0;
112}
113
Note: See TracBrowser for help on using the repository browser.