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

Last change on this file since 1387 was 1387, checked in by Sam Hocevar, 14 years ago
  • Add more randomness to the cacademo effects.
  • Adapt the swallow test to the display's size.
  • 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 1387 2006-11-13 13:34:06Z 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    dp = caca_create_display(cv);
44    if(!dp)
45        return 1;
46
47    w = (cucul_get_canvas_width(cv) - 4) / 2;
48    h = (cucul_get_canvas_height(cv) - 6) / 2;
49
50    if(w < 0 || h < 0)
51        return 1;
52
53    cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLUE);
54    cucul_draw_line(cv, 0, 0, cucul_get_canvas_width(cv) - 1, 0, ' ');
55    cucul_printf(cv, cucul_get_canvas_width(cv) / 2 - 10, 0,
56                 "libcaca multiplexer");
57
58    for(i = 0; i < 4; i++)
59    {
60        buf[i] = NULL;
61        total[i] = bytes[i] = 0;
62        sprintf(cmd, "CACA_DRIVER=raw CACA_GEOMETRY=%ix%i %s",
63                     w, h, argv[i + 1]);
64        f[i] = popen(cmd, "r");
65        if(!f[i])
66            return 1;
67        cucul_printf(cv, (w + 2) * (i / 2) + 1,
68                         (h + 2) * ((i % 2) + 1), "%s", argv[i + 1]);
69    }
70
71    for(;;)
72    {
73        caca_event_t ev;
74        int ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, 0);
75
76        if(ret && ev.type & CACA_EVENT_KEY_PRESS)
77            break;
78
79        for(i = 0; i < 4; i++)
80        {
81            bytes[i] = cucul_import_memory(app, buf[i], total[i], "caca");
82
83            if(bytes[i] > 0)
84            {
85                total[i] -= bytes[i];
86                memmove(buf[i], buf[i] + bytes[i], total[i]);
87
88                cucul_blit(cv, (w + 2) * (i / 2) + 1,
89                               (h + 2) * (i % 2) + 2, app, NULL);
90                caca_refresh_display(dp);
91            }
92            else if(bytes[i] == 0)
93            {
94                buf[i] = realloc(buf[i], total[i] + 128);
95                fread(buf[i] + total[i], 128, 1, f[i]);
96                total[i] += 128;
97            }
98            else
99            {
100                fprintf(stderr, "%s: corrupted input %i\n", argv[0], i);
101                return -1;
102            }
103        }
104    }
105
106    /* Clean up */
107    caca_free_display(dp);
108    cucul_free_canvas(cv);
109    cucul_free_canvas(app);
110
111    return 0;
112}
113
Note: See TracBrowser for help on using the repository browser.