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

Last change on this file since 554 was 554, checked in by Jean-Yves Lamoureux, 14 years ago
  • Added very preliminary network driver
File size: 5.7 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;    // my address information
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    codes[16] = (unsigned char) kk->drv.p->width&0xff;
130    codes[18] = (unsigned char) kk->drv.p->height&0xff;
131
132    /* Send basic telnet codes */
133    if (send(kk->drv.p->new_fd, codes,sizeof(codes) , 0) == -1) {
134        perror("send");
135        return -1;
136    }
137
138    printf("network ok.\n");
139
140    return 0;
141}
142
143static int network_end_graphics(caca_t *kk)
144{
145    printf("network end graphics\n");
146    return 0;
147}
148
149static int network_set_window_title(caca_t *kk, char const *title)
150{
151    printf("network_set_window_title(%s) not implemented yet.\n", title);
152    return 0;
153}
154
155static unsigned int network_get_window_width(caca_t *kk)
156{
157    return kk->drv.p->width * 6;
158}
159
160static unsigned int network_get_window_height(caca_t *kk)
161{
162    return kk->drv.p->height * 10;
163}
164
165static void network_display(caca_t *kk)
166{
167    /* Clear screen */
168    /*    if (send(kk->drv.p->new_fd, "\033?75l\033[2J\033[H", 12, 0) == -1) {
169        perror("send");
170        return;
171    }
172    */
173
174    char *to_send = cucul_get_ansi(kk->qq, 0);;
175    to_send = realloc(to_send, kk->qq->width * kk->qq->height * 15 * 3);
176   
177
178    if (send(kk->drv.p->new_fd, "\033[s", 4, 0) == -1) {
179        perror("send");
180        return;
181    }
182    if (send(kk->drv.p->new_fd, to_send, kk->qq->width * kk->qq->height * 15, 0) == -1) {
183        perror("send");
184        return;
185    }
186    if (send(kk->drv.p->new_fd, "\033?75l\033[2J\033[H", 12, 0) == -1) {
187        perror("send");
188        return;
189    }
190 
191}
192static void network_handle_resize(caca_t *kk)
193{
194    printf("Resize\n");
195 
196}
197
198static unsigned int network_get_event(caca_t *kk)
199{
200    return 0;
201}
202
203
204/*
205 * Driver initialisation
206 */
207
208void network_init_driver(caca_t *kk)
209{
210    kk->drv.driver = CACA_DRIVER_NETWORK;
211
212    kk->drv.init_graphics = network_init_graphics;
213    kk->drv.end_graphics = network_end_graphics;
214    kk->drv.set_window_title = network_set_window_title;
215    kk->drv.get_window_width = network_get_window_width;
216    kk->drv.get_window_height = network_get_window_height;
217    kk->drv.display = network_display;
218    kk->drv.handle_resize = network_handle_resize;
219    kk->drv.get_event = network_get_event;
220}
221
222#endif // USE_NETWORK
Note: See TracBrowser for help on using the repository browser.