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

Last change on this file since 1364 was 1364, checked in by Sam Hocevar, 15 years ago
  • Random transition times in cacademo.
  • Swallow 4 applications instead of 2 in the swallow test.
  • Property svn:keywords set to Id
File size: 2.7 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 1364 2006-11-12 15:54:50Z 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[4];
31    long int bytes[4], total[4];
32    FILE *f[4];
33    int w, h, i;
34
35    if(argc < 5)
36    {
37        fprintf(stderr, "usage: %s <cmd1> <cmd2> <cmd3> <cmd4>\n", argv[0]);
38        return 1;
39    }
40
41    cv = cucul_create_canvas(0, 0);
42    app = cucul_create_canvas(0, 0);
43    w = 38;
44    h = 13;
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_draw_line(cv, 0, 0, 80, 0, ' ');
52    cucul_printf(cv, 30, 0, "libcaca multiplexer");
53
54    for(i = 0; i < 4; i++)
55    {
56        buf[i] = NULL;
57        total[i] = bytes[i] = 0;
58        sprintf(cmd, "CACA_DRIVER=raw CACA_GEOMETRY=%ix%i %s",
59                     w, h, argv[i + 1]);
60        f[i] = popen(cmd, "r");
61        if(!f[i])
62            return 1;
63        cucul_printf(cv, 40 * (i / 2) + 1,
64                         (h + 2) * (i % 2) + h + 2, "%s", argv[i + 1]);
65    }
66
67    for(;;)
68    {
69        caca_event_t ev;
70        int ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, 0);
71
72        if(ret && ev.type & CACA_EVENT_KEY_PRESS)
73            break;
74
75        for(i = 0; i < 4; i++)
76        {
77            bytes[i] = cucul_import_memory(app, buf[i], total[i], "caca");
78
79            if(bytes[i] > 0)
80            {
81                total[i] -= bytes[i];
82                memmove(buf[i], buf[i] + bytes[i], total[i]);
83
84                cucul_blit(cv, 1 + (i / 2) * (w + 2),
85                               (h + 2) * (i % 2) + 2, app, NULL);
86                caca_refresh_display(dp);
87            }
88            else if(bytes[i] == 0)
89            {
90                buf[i] = realloc(buf[i], total[i] + 128);
91                fread(buf[i] + total[i], 128, 1, f[i]);
92                total[i] += 128;
93            }
94            else
95            {
96                fprintf(stderr, "%s: corrupted input %i\n", argv[0], i);
97                return -1;
98            }
99        }
100    }
101
102    /* Clean up */
103    caca_free_display(dp);
104    cucul_free_canvas(cv);
105    cucul_free_canvas(app);
106
107    return 0;
108}
109
Note: See TracBrowser for help on using the repository browser.