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

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