source: libcaca/trunk/test/swallow.c @ 1351

Last change on this file since 1351 was 1351, checked in by Sam Hocevar, 15 years ago
  • Fix infinite loop due to signed/unsigned mistake.
  • Property svn:keywords set to Id
File size: 2.4 KB
Line 
1/*
2 *  swallow       swallow another libcaca application
3 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: swallow.c 1351 2006-11-12 09:46:10Z 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#if !defined(__KERNEL__)
17#   include <stdio.h>
18#   include <string.h>
19#   include <stdlib.h>
20#endif
21
22#include "cucul.h"
23#include "caca.h"
24
25int main(int argc, char **argv)
26{
27    char cmd[BUFSIZ];
28    static cucul_canvas_t *cv, *app;
29    static caca_display_t *dp;
30    unsigned char *buf[2];
31    long int bytes[2], total[2];
32    FILE *f[2];
33    int w, h, i;
34
35    buf[0] = buf[1] = NULL;
36    total[0] = total[1] = 0;
37
38    if(argc < 3)
39        return 1;
40
41    cv = cucul_create_canvas(0, 0);
42    app = cucul_create_canvas(0, 0);
43    w = 38;
44    h = 26;
45
46    dp = caca_create_display(cv);
47    if(!dp)
48        return 1;
49
50    cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLUE);
51    cucul_printf(cv, 1, h + 4, "libcaca multiplexer");
52
53    for(i = 0; i < 2; i++)
54    {
55        sprintf(cmd, "CACA_DRIVER=raw CACA_GEOMETRY=%ix%i %s",
56                     w, h, argv[i + 1]);
57        f[i] = popen(cmd, "r");
58        if(!f[i])
59            return 1;
60        cucul_printf(cv, 40 * i + 1, 1, "%s", argv[i + 1]);
61    }
62
63    for(;;)
64    {
65        caca_event_t ev;
66        int ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, 0);
67
68        if(ret && ev.type & CACA_EVENT_KEY_PRESS)
69            break;
70
71        for(i = 0; i < 2; i++)
72        {
73            bytes[i] = cucul_import_memory(app, buf[i], total[i], "caca");
74
75            if(bytes[i] > 0)
76            {
77                total[i] -= bytes[i];
78                memmove(buf[i], buf[i] + bytes[i], total[i]);
79
80                cucul_blit(cv, 1 + i * (w + 2), 3, app, NULL);
81                caca_refresh_display(dp);
82            }
83            else if(bytes[i] == 0)
84            {
85                buf[i] = realloc(buf[i], total[i] + 128);
86                fread(buf[i] + total[i], 128, 1, f[i]);
87                total[i] += 128;
88            }
89            else
90            {
91                fprintf(stderr, "%s: corrupted input\n", argv[0]);
92                return -1;
93            }
94        }
95    }
96
97    /* Clean up */
98    caca_free_display(dp);
99    cucul_free_canvas(cv);
100    cucul_free_canvas(app);
101
102    return 0;
103}
104
Note: See TracBrowser for help on using the repository browser.