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

Last change on this file since 4330 was 3495, checked in by Sam Hocevar, 10 years ago

Add caca_export_area_to_memory() to export only a selected part of the
current canvas. This is useful to export dirty rectangles. Accordingly,
add caca_import_area_from_memory() and caca_import_area_from_file().

  • Property svn:keywords set to Id
File size: 2.6 KB
Line 
1/*
2 *  cacaplay      caca file player
3 *  Copyright (c) 2006-2009 Sam Hocevar <sam@hocevar.net>
4 *                All Rights Reserved
5 *
6 *  $Id: cacaplay.c 3495 2009-05-21 20:55:21Z 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 "caca.h"
28
29int main(int argc, char **argv)
30{
31    caca_canvas_t *cv, *app;
32    caca_display_t *dp;
33    uint8_t *buf = NULL;
34    long int bytes = 0, total = 0;
35    int fd;
36
37    if(argc < 2 || !strcmp(argv[1], "-"))
38    {
39        fd = 0; /* use stdin */
40    }
41    else
42    {
43        fd = open(argv[1], O_RDONLY);
44        if(fd < 0)
45        {
46            fprintf(stderr, "%s: could not open `%s'.\n", argv[0], argv[1]);
47            return 1;
48        }
49    }
50
51    cv = caca_create_canvas(0, 0);
52    app = caca_create_canvas(0, 0);
53    if(cv == NULL || app == NULL)
54    {
55        printf("Can't created canvas\n");
56        return -1;
57    }
58    dp = caca_create_display(cv);
59    if(dp == NULL)
60    {
61        printf("Can't create display\n");
62        return -1;
63    }
64
65
66    for(;;)
67    {
68        caca_event_t ev;
69        int ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, 0);
70        int has_eof = 0;
71
72        if(ret && caca_get_event_type(&ev) & CACA_EVENT_KEY_PRESS)
73            break;
74
75        if(bytes == 0)
76        {
77            ssize_t n;
78            buf = realloc(buf, total + 1);
79            n = read(fd, buf + total, 1);
80            if(n < 0)
81            {
82                fprintf(stderr, "%s: read error\n", argv[0]);
83                return -1;
84            }
85            else if(n == 0)
86            {
87                has_eof = 1;
88            }
89            total += n;
90        }
91
92        bytes = caca_import_canvas_from_memory(app, buf, total, "caca");
93
94        if(bytes > 0)
95        {
96            total -= bytes;
97            memmove(buf, buf + bytes, total);
98
99            caca_blit(cv, 0, 0, app, NULL);
100            caca_refresh_display(dp);
101        }
102        else if(bytes < 0)
103        {
104            fprintf(stderr, "%s: corrupted caca file\n", argv[0]);
105            break;
106        }
107
108        if(has_eof)
109            break;
110    }
111
112    caca_get_event(dp, CACA_EVENT_KEY_PRESS, NULL, -1);
113
114    /* Clean up */
115    close(fd);
116
117    caca_free_display(dp);
118    caca_free_canvas(cv);
119
120    return 0;
121}
122
Note: See TracBrowser for help on using the repository browser.