[1350] | 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 2101 2007-11-30 23:48:39Z 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" |
---|
| 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 | |
---|
| 26 | int main(int argc, char **argv) |
---|
| 27 | { |
---|
| 28 | char cmd[BUFSIZ]; |
---|
| 29 | static cucul_canvas_t *cv, *app; |
---|
| 30 | static caca_display_t *dp; |
---|
[1364] | 31 | unsigned char *buf[4]; |
---|
| 32 | long int bytes[4], total[4]; |
---|
| 33 | FILE *f[4]; |
---|
[1350] | 34 | int w, h, i; |
---|
| 35 | |
---|
[1364] | 36 | if(argc < 5) |
---|
| 37 | { |
---|
| 38 | fprintf(stderr, "usage: %s <cmd1> <cmd2> <cmd3> <cmd4>\n", argv[0]); |
---|
[1350] | 39 | return 1; |
---|
[1364] | 40 | } |
---|
[1350] | 41 | |
---|
| 42 | cv = cucul_create_canvas(0, 0); |
---|
| 43 | app = cucul_create_canvas(0, 0); |
---|
| 44 | dp = caca_create_display(cv); |
---|
| 45 | |
---|
[1753] | 46 | if(cv == NULL || app == NULL ) |
---|
| 47 | { |
---|
| 48 | printf("Can't created canvas\n"); |
---|
| 49 | return -1; |
---|
| 50 | } |
---|
| 51 | if(dp == NULL) |
---|
| 52 | { |
---|
| 53 | printf("Can't create display\n"); |
---|
| 54 | return -1; |
---|
| 55 | } |
---|
| 56 | |
---|
[1387] | 57 | w = (cucul_get_canvas_width(cv) - 4) / 2; |
---|
| 58 | h = (cucul_get_canvas_height(cv) - 6) / 2; |
---|
| 59 | |
---|
| 60 | if(w < 0 || h < 0) |
---|
| 61 | return 1; |
---|
| 62 | |
---|
[1350] | 63 | cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLUE); |
---|
[1387] | 64 | cucul_draw_line(cv, 0, 0, cucul_get_canvas_width(cv) - 1, 0, ' '); |
---|
| 65 | cucul_printf(cv, cucul_get_canvas_width(cv) / 2 - 10, 0, |
---|
| 66 | "libcaca multiplexer"); |
---|
[1350] | 67 | |
---|
[1364] | 68 | for(i = 0; i < 4; i++) |
---|
[1350] | 69 | { |
---|
[1364] | 70 | buf[i] = NULL; |
---|
| 71 | total[i] = bytes[i] = 0; |
---|
[1350] | 72 | sprintf(cmd, "CACA_DRIVER=raw CACA_GEOMETRY=%ix%i %s", |
---|
| 73 | w, h, argv[i + 1]); |
---|
| 74 | f[i] = popen(cmd, "r"); |
---|
| 75 | if(!f[i]) |
---|
| 76 | return 1; |
---|
[1387] | 77 | cucul_printf(cv, (w + 2) * (i / 2) + 1, |
---|
| 78 | (h + 2) * ((i % 2) + 1), "%s", argv[i + 1]); |
---|
[1350] | 79 | } |
---|
| 80 | |
---|
| 81 | for(;;) |
---|
| 82 | { |
---|
| 83 | caca_event_t ev; |
---|
| 84 | int ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, 0); |
---|
| 85 | |
---|
[2049] | 86 | if(ret && caca_get_event_type(&ev) & CACA_EVENT_KEY_PRESS) |
---|
[1350] | 87 | break; |
---|
| 88 | |
---|
[1364] | 89 | for(i = 0; i < 4; i++) |
---|
[1350] | 90 | { |
---|
| 91 | bytes[i] = cucul_import_memory(app, buf[i], total[i], "caca"); |
---|
| 92 | |
---|
| 93 | if(bytes[i] > 0) |
---|
| 94 | { |
---|
| 95 | total[i] -= bytes[i]; |
---|
| 96 | memmove(buf[i], buf[i] + bytes[i], total[i]); |
---|
| 97 | |
---|
[1387] | 98 | cucul_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); |
---|
| 118 | cucul_free_canvas(cv); |
---|
| 119 | cucul_free_canvas(app); |
---|
| 120 | |
---|
| 121 | return 0; |
---|
| 122 | } |
---|
| 123 | |
---|