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

Last change on this file since 2299 was 2299, checked in by Sam Hocevar, 12 years ago
  • Remove #include <stdint.h> etc. from "common.h". Instead, make sure that <cucul.h> will provide the C99 types, even if libcaca has been installed.
  • Rename what's left of "common.h" to "stubs.h".
  • Remove all references to erroneous <inttypes.h> from source files.
  • Property svn:keywords set to Id
File size: 2.6 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 2299 2008-04-19 12:42:50Z sam $
7 *
8 *  This program is free software. It comes 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
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    if(cv == NULL || app == NULL)
55    {
56        printf("Can't created canvas\n");
57        return -1;
58    }
59    dp = caca_create_display(cv);
60    if(dp == NULL)
61    {
62        printf("Can't create display\n");
63        return -1;
64    }
65
66
67    for(;;)
68    {
69        caca_event_t ev;
70        int ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, 0);
71        int eof = 0;
72
73        if(ret && caca_get_event_type(&ev) & CACA_EVENT_KEY_PRESS)
74            break;
75
76        if(bytes == 0)
77        {
78            ssize_t n;
79            buf = realloc(buf, total + 1);
80            n = read(fd, buf + total, 1);
81            if(n < 0)
82            {
83                fprintf(stderr, "%s: read error\n", argv[0]);
84                return -1;
85            }
86            else if(n == 0)
87            {
88                eof = 1;
89            }
90            total += n;
91        }
92
93        bytes = cucul_import_memory(app, buf, total, "caca");
94
95        if(bytes > 0)
96        {
97            total -= bytes;
98            memmove(buf, buf + bytes, total);
99
100            cucul_blit(cv, 0, 0, app, NULL);
101            caca_refresh_display(dp);
102        }
103        else if(bytes < 0)
104        {
105            fprintf(stderr, "%s: corrupted caca file\n", argv[0]);
106            break;
107        }
108
109        if(eof)
110            break;
111    }
112
113    caca_get_event(dp, CACA_EVENT_KEY_PRESS, NULL, -1);
114
115    /* Clean up */
116    close(fd);
117
118    caca_free_display(dp);
119    cucul_free_canvas(cv);
120
121    return 0;
122}
123
Note: See TracBrowser for help on using the repository browser.