source: libcaca/trunk/examples/swallow.c @ 3514

Last change on this file since 3514 was 3495, checked in by Sam Hocevar, 11 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: 3.1 KB
RevLine 
[1350]1/*
2 *  swallow       swallow another libcaca application
[3495]3 *  Copyright (c) 2006-2009 Sam Hocevar <sam@hocevar.net>
[1350]4 *                All Rights Reserved
5 *
6 *  $Id: swallow.c 3495 2009-05-21 20:55:21Z sam $
7 *
[1462]8 *  This program is free software. It comes without any warranty, to
[1452]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
[1350]12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15#include "config.h"
[2299]16
[1350]17#if !defined(__KERNEL__)
18#   include <stdio.h>
19#   include <string.h>
20#   include <stdlib.h>
21#endif
22
23#include "caca.h"
24
25int main(int argc, char **argv)
26{
27    char cmd[BUFSIZ];
[2821]28    static caca_canvas_t *cv, *app;
[1350]29    static caca_display_t *dp;
[2300]30    uint8_t *buf[4];
[1364]31    long int bytes[4], total[4];
32    FILE *f[4];
[1350]33    int w, h, i;
34
[1364]35    if(argc < 5)
36    {
37        fprintf(stderr, "usage: %s <cmd1> <cmd2> <cmd3> <cmd4>\n", argv[0]);
[1350]38        return 1;
[1364]39    }
[1350]40
[2821]41    cv = caca_create_canvas(0, 0);
42    app = caca_create_canvas(0, 0);
[1350]43    dp = caca_create_display(cv);
44
[1753]45    if(cv == NULL || app == NULL )
46    {
47        printf("Can't created canvas\n");
48        return -1;
49    }
50    if(dp == NULL)
51    {
52        printf("Can't create display\n");
53        return -1;
54    }
55
[2821]56    w = (caca_get_canvas_width(cv) - 4) / 2;
57    h = (caca_get_canvas_height(cv) - 6) / 2;
[1387]58
59    if(w < 0 || h < 0)
60        return 1;
61
[2821]62    caca_set_color_ansi(cv, CACA_WHITE, CACA_BLUE);
63    caca_draw_line(cv, 0, 0, caca_get_canvas_width(cv) - 1, 0, ' ');
64    caca_printf(cv, caca_get_canvas_width(cv) / 2 - 10, 0,
[1387]65                 "libcaca multiplexer");
[1350]66
[1364]67    for(i = 0; i < 4; i++)
[1350]68    {
[1364]69        buf[i] = NULL;
70        total[i] = bytes[i] = 0;
[1350]71        sprintf(cmd, "CACA_DRIVER=raw CACA_GEOMETRY=%ix%i %s",
72                     w, h, argv[i + 1]);
73        f[i] = popen(cmd, "r");
74        if(!f[i])
75            return 1;
[2821]76        caca_printf(cv, (w + 2) * (i / 2) + 1,
[1387]77                         (h + 2) * ((i % 2) + 1), "%s", argv[i + 1]);
[1350]78    }
79
80    for(;;)
81    {
82        caca_event_t ev;
83        int ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, 0);
84
[2049]85        if(ret && caca_get_event_type(&ev) & CACA_EVENT_KEY_PRESS)
[1350]86            break;
87
[1364]88        for(i = 0; i < 4; i++)
[1350]89        {
[3495]90            bytes[i] = caca_import_canvas_from_memory(app, buf[i],
91                                                      total[i], "caca");
[1350]92
93            if(bytes[i] > 0)
94            {
95                total[i] -= bytes[i];
96                memmove(buf[i], buf[i] + bytes[i], total[i]);
97
[2821]98                caca_blit(cv, (w + 2) * (i / 2) + 1,
[1364]99                               (h + 2) * (i % 2) + 2, app, NULL);
[1350]100                caca_refresh_display(dp);
101            }
102            else if(bytes[i] == 0)
103            {
104                buf[i] = realloc(buf[i], total[i] + 128);
105                fread(buf[i] + total[i], 128, 1, f[i]);
106                total[i] += 128;
107            }
[1351]108            else
109            {
[1364]110                fprintf(stderr, "%s: corrupted input %i\n", argv[0], i);
[1351]111                return -1;
112            }
[1350]113        }
114    }
115
116    /* Clean up */
117    caca_free_display(dp);
[2821]118    caca_free_canvas(cv);
119    caca_free_canvas(app);
[1350]120
121    return 0;
122}
123
Note: See TracBrowser for help on using the repository browser.