source: libcaca/trunk/caca/driver_network.c @ 555

Last change on this file since 555 was 555, checked in by Jean-Yves Lamoureux, 14 years ago
  • Exporters now return size of generated data
File size: 5.5 KB
Line 
1/*
2 *  libcaca       ASCII-Art library
3 *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  This library is free software; you can redistribute it and/or
7 *  modify it under the terms of the Do What The Fuck You Want To
8 *  Public License, Version 2, as published by Sam Hocevar. See
9 *  http://sam.zoy.org/wtfpl/COPYING for more details.
10 */
11
12/** \file driver_network.c
13 *  \version \$Id: driver_gl.c 330 2006-03-07 09:17:35Z sam $
14 *  \author Jean-Yves Lamoureux <jylam@lnxscene.org>
15 *  \brief Network driver
16 *
17 *  This file contains the libcaca network input and output driver
18 */
19
20#include "config.h"
21
22#if defined(USE_NETWORK)
23#include <stdio.h>
24#include <stdlib.h>
25#include <sys/types.h>
26#include <sys/socket.h>
27#include <arpa/inet.h>
28#include <string.h>
29
30#if defined(HAVE_UNISTD_H)
31#   include <unistd.h>
32#endif
33#include <stdarg.h>
34
35#include "caca.h"
36#include "caca_internals.h"
37#include "cucul.h"
38#include "cucul_internals.h"
39
40
41
42struct driver_private
43{
44    unsigned int width, height;
45    unsigned int port;
46    int sockfd, new_fd;
47    struct sockaddr_in my_addr;
48    struct sockaddr_in remote_addr;
49    socklen_t sin_size;
50    int clilen;
51    char buffer[256];
52};
53
54
55
56#define BACKLOG 1337    /* Number of pending connections */
57
58
59
60/* Following vars are static */
61static char codes[] = {0xff, 0xfb, 0x01,  // WILL ECHO                                                             
62                       0xff, 0xfb, 0x03,  // WILL SUPPRESS GO AHEAD
63                       0xff, 253, 31,     // DO NAWS
64                       0xff, 254, 31,     // DON'T NAWS
65                       0xff, 31, 250, 0, 30, 0, 0xFF, // to be replaced
66                       0xff, 240};
67
68
69static int network_init_graphics(caca_t *kk)
70{
71    int yes=1;
72    printf("Initing network stack.\n");
73
74    kk->drv.p = malloc(sizeof(struct driver_private));
75
76
77    kk->drv.p->width = 80;
78    kk->drv.p->height = 25;
79    kk->drv.p->port = 7575; // 75 75 decimal ASCII -> KK   // FIXME, sadly
80
81
82    cucul_set_size(kk->qq, kk->drv.p->width, kk->drv.p->height);
83
84
85    printf("socket\n");
86    if ((kk->drv.p->sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
87        perror("socket");
88        return -1;
89    }
90    printf("setsockopt\n");
91    if (setsockopt(kk->drv.p->sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
92        perror("setsockopt");
93        return -1;
94    }
95
96
97    kk->drv.p->my_addr.sin_family = AF_INET;
98    kk->drv.p-> my_addr.sin_port = htons(kk->drv.p->port);
99    kk->drv.p->my_addr.sin_addr.s_addr = INADDR_ANY;
100    memset(&(kk->drv.p->my_addr.sin_zero), '\0', 8);
101
102    printf("bind\n");
103    if (bind(kk->drv.p->sockfd, (struct sockaddr *)&kk->drv.p->my_addr, sizeof(struct sockaddr))
104                                                                   == -1) {
105        perror("bind");
106        return -1;
107    }
108    printf("listen\n");
109    if (listen(kk->drv.p->sockfd, BACKLOG) == -1) {
110        perror("listen");
111        return -1;
112    }
113
114    printf("accept\n");
115    kk->drv.p->clilen = sizeof(kk->drv.p->remote_addr);
116    kk->drv.p->new_fd = accept(kk->drv.p->sockfd, (struct sockaddr *) &kk->drv.p->remote_addr, &kk->drv.p->clilen);
117    if (kk->drv.p->new_fd < 0) {
118        perror("ERROR on accept");
119        return -1;
120    }
121
122
123    printf("Got connexion from %d.%d.%d.%d\n",
124           (unsigned int)((kk->drv.p->remote_addr.sin_addr.s_addr)&0x000000FF),
125           (unsigned int)((kk->drv.p->remote_addr.sin_addr.s_addr)&0x0000FF00)>>8,
126           (unsigned int)((kk->drv.p->remote_addr.sin_addr.s_addr)&0x00FF0000)>>16,
127           (unsigned int)((kk->drv.p->remote_addr.sin_addr.s_addr)&0xFF000000)>>24);
128   
129    /* FIXME, handle >255 sizes */
130    codes[16] = (unsigned char) kk->drv.p->width&0xff;
131    codes[18] = (unsigned char) kk->drv.p->height&0xff;
132
133    /* Send basic telnet codes */
134    if (send(kk->drv.p->new_fd, codes,sizeof(codes) , 0) == -1) {
135        perror("send");
136        return -1;
137    }
138
139    printf("network ok.\n");
140
141    return 0;
142}
143
144static int network_end_graphics(caca_t *kk)
145{
146    printf("network end graphics\n");
147    return 0;
148}
149
150static int network_set_window_title(caca_t *kk, char const *title)
151{
152    printf("network_set_window_title(%s) not implemented yet.\n", title);
153    return 0;
154}
155
156static unsigned int network_get_window_width(caca_t *kk)
157{
158    return kk->drv.p->width * 6;
159}
160
161static unsigned int network_get_window_height(caca_t *kk)
162{
163    return kk->drv.p->height * 10;
164}
165
166static void network_display(caca_t *kk)
167{
168    int size;
169    char *to_send = cucul_get_ansi(kk->qq, 0, &size);;
170    to_send = realloc(to_send, kk->qq->width * kk->qq->height * 15 * 3);
171   
172
173    if (send(kk->drv.p->new_fd, "\033[s", 4, 0) == -1) {
174        perror("send");
175        return;
176    }
177    if (send(kk->drv.p->new_fd, to_send, size, 0) == -1) {
178        perror("send");
179        return;
180    }
181    /*   if (send(kk->drv.p->new_fd, "\033?75l\033[2J\033[H", 12, 0) == -1) {
182        perror("send");
183        return;
184    }
185    */
186}
187static void network_handle_resize(caca_t *kk)
188{
189    printf("Resize\n");
190 
191}
192
193static unsigned int network_get_event(caca_t *kk)
194{
195    return 0;
196}
197
198
199/*
200 * Driver initialisation
201 */
202
203void network_init_driver(caca_t *kk)
204{
205    kk->drv.driver = CACA_DRIVER_NETWORK;
206
207    kk->drv.init_graphics = network_init_graphics;
208    kk->drv.end_graphics = network_end_graphics;
209    kk->drv.set_window_title = network_set_window_title;
210    kk->drv.get_window_width = network_get_window_width;
211    kk->drv.get_window_height = network_get_window_height;
212    kk->drv.display = network_display;
213    kk->drv.handle_resize = network_handle_resize;
214    kk->drv.get_event = network_get_event;
215}
216
217#endif // USE_NETWORK
Note: See TracBrowser for help on using the repository browser.