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

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