Debian Codegraph @

examples/swallow.c

raw
1 /*
2 * swallow swallow another libcaca application
3 * Copyright (c) 2006-2012 Sam Hocevar <[email protected]>
4 * All Rights Reserved
5 *
6 * This program is free software. It comes without any warranty, to
7 * the extent permitted by applicable law. You can redistribute it
8 * and/or modify it under the terms of the Do What the Fuck You Want
9 * to Public License, Version 2, as published by Sam Hocevar. See
10 * http://www.wtfpl.net/ for more details.
11 */
12
13 #include "config.h"
14
15 #if !defined(__KERNEL__)
16 # include <stdio.h>
17 # include <string.h>
18 # include <stdlib.h>
19 #endif
20
21 #include "caca.h"
22
23 int main(int argc, char **argv)
24 {
25 char cmd[BUFSIZ];
26 static caca_canvas_t *cv, *app;
27 static caca_display_t *dp;
28 uint8_t *buf[4];
29 long int bytes[4], total[4];
30 FILE *f[4];
31 int w, h, i;
32
33 if(argc < 5)
34 {
35 fprintf(stderr, "usage: %s <cmd1> <cmd2> <cmd3> <cmd4>\n", argv[0]);
36 return 1;
37 }
38
39 cv = caca_create_canvas(0, 0);
40 app = caca_create_canvas(0, 0);
41 dp = caca_create_display(cv);
42
43 if(cv == NULL || app == NULL )
44 {
45 printf("Can't created canvas\n");
46 return -1;
47 }
48 if(dp == NULL)
49 {
50 printf("Can't create display\n");
51 return -1;
52 }
53
54 w = (caca_get_canvas_width(cv) - 4) / 2;
55 h = (caca_get_canvas_height(cv) - 6) / 2;
56
57 if(w < 0 || h < 0)
58 return 1;
59
60 caca_set_color_ansi(cv, CACA_WHITE, CACA_BLUE);
61 caca_draw_line(cv, 0, 0, caca_get_canvas_width(cv) - 1, 0, ' ');
62 caca_printf(cv, caca_get_canvas_width(cv) / 2 - 10, 0,
63 "libcaca multiplexer");
64
65 for(i = 0; i < 4; i++)
66 {
67 buf[i] = NULL;
68 total[i] = bytes[i] = 0;
69 sprintf(cmd, "CACA_DRIVER=raw CACA_GEOMETRY=%ix%i %s",
70 w, h, argv[i + 1]);
71 f[i] = popen(cmd, "r");
72 if(!f[i])
73 return 1;
74 caca_printf(cv, (w + 2) * (i / 2) + 1,
75 (h + 2) * ((i % 2) + 1), "%s", argv[i + 1]);
76 }
77
78 for(;;)
79 {
80 caca_event_t ev;
81 int ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, 0);
82
83 if(ret && caca_get_event_type(&ev) & CACA_EVENT_KEY_PRESS)
84 break;
85
86 for(i = 0; i < 4; i++)
87 {
88 bytes[i] = caca_import_canvas_from_memory(app, buf[i],
89 total[i], "caca");
90
91 if(bytes[i] > 0)
92 {
93 total[i] -= bytes[i];
94 memmove(buf[i], buf[i] + bytes[i], total[i]);
95
96 caca_blit(cv, (w + 2) * (i / 2) + 1,
97 (h + 2) * (i % 2) + 2, app, NULL);
98 caca_refresh_display(dp);
99 }
100 else if(bytes[i] == 0)
101 {
102 buf[i] = realloc(buf[i], total[i] + 128);
103 fread(buf[i] + total[i], 128, 1, f[i]);
104 total[i] += 128;
105 }
106 else
107 {
108 fprintf(stderr, "%s: corrupted input %i\n", argv[0], i);
109 return -1;
110 }
111 }
112 }
113
114 /* Clean up */
115 caca_free_display(dp);
116 caca_free_canvas(cv);
117 caca_free_canvas(app);
118
119 return 0;
120 }
121