|
|
@@ -1,44 +1,40 @@ |
|
|
|
/* |
|
|
|
* libcaca Colour ASCII-Art library |
|
|
|
* Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org> |
|
|
|
* cacaserver Colour ASCII-Art library |
|
|
|
* Copyright (c) 2006 Jean-Yves Lamoureux <jylam@lnxscene.org> |
|
|
|
* 2006 Sam Hocevar <sam@zoy.org> |
|
|
|
* All Rights Reserved |
|
|
|
* |
|
|
|
* This library is free software; you can redistribute it and/or |
|
|
|
* This program is free software; you can redistribute it and/or |
|
|
|
* modify it under the terms of the Do What The Fuck You Want To |
|
|
|
* Public License, Version 2, as published by Sam Hocevar. See |
|
|
|
* http://sam.zoy.org/wtfpl/COPYING for more details. |
|
|
|
*/ |
|
|
|
|
|
|
|
/** \file driver_network.c |
|
|
|
* \version \$Id$ |
|
|
|
* \author Jean-Yves Lamoureux <jylam@lnxscene.org> |
|
|
|
* \brief Network driver |
|
|
|
* |
|
|
|
* This file contains the libcaca network input and output driver |
|
|
|
*/ |
|
|
|
|
|
|
|
#include "config.h" |
|
|
|
|
|
|
|
#if defined(USE_NETWORK) |
|
|
|
#include <stdio.h> |
|
|
|
#include <string.h> |
|
|
|
#include <stdlib.h> |
|
|
|
#include <sys/types.h> |
|
|
|
#include <sys/socket.h> |
|
|
|
#include <arpa/inet.h> |
|
|
|
#include <fcntl.h> |
|
|
|
#include <string.h> |
|
|
|
#include <signal.h> |
|
|
|
#include <errno.h> |
|
|
|
|
|
|
|
#include "cucul.h" |
|
|
|
#include "caca.h" |
|
|
|
|
|
|
|
#include "config.h" |
|
|
|
|
|
|
|
#if defined(HAVE_UNISTD_H) |
|
|
|
# include <unistd.h> |
|
|
|
#endif |
|
|
|
|
|
|
|
#include <stdarg.h> |
|
|
|
|
|
|
|
#include "caca.h" |
|
|
|
#include "caca_internals.h" |
|
|
|
#include "cucul.h" |
|
|
|
#include "cucul_internals.h" |
|
|
|
|
|
|
|
#define BACKLOG 1337 /* Number of pending connections */ |
|
|
|
#define INBUFFER 32 /* Size of per-client input buffer */ |
|
|
@@ -64,22 +60,24 @@ |
|
|
|
"\x1b[?1049h" /* Clear screen */ \ |
|
|
|
"\x1b[?1049h" /* Clear screen again */ |
|
|
|
|
|
|
|
|
|
|
|
static char const telnet_commands[16][5] = |
|
|
|
{"SE ", "NOP ", "DM ", "BRK ", "IP ", "AO ", "AYT ", "EC ", |
|
|
|
"EL ", "GA ", "SB ", "WILL", "WONT", "DO ", "DONT", "IAC "}; |
|
|
|
{ |
|
|
|
"SE ", "NOP ", "DM ", "BRK ", "IP ", "AO ", "AYT ", "EC ", |
|
|
|
"EL ", "GA ", "SB ", "WILL", "WONT", "DO ", "DONT", "IAC " |
|
|
|
}; |
|
|
|
|
|
|
|
static char const telnet_options[37][5] = |
|
|
|
{"????", "ECHO", "????", "SUGH", "????", "STTS", "TIMK", "????", "????", "????", |
|
|
|
"????", "????", "????", "????", "????", "????", "????", "????", "????", "????", |
|
|
|
"????", "????", "????", "????", "TTYP", "????", "????", "????", "????", "????", |
|
|
|
"????", "NAWS", "TRSP", "RMFC", "LIMO", "????", "EVAR"}; |
|
|
|
|
|
|
|
{ |
|
|
|
"????", "ECHO", "????", "SUGH", "????", "STTS", "TIMK", "????", |
|
|
|
"????", "????", "????", "????", "????", "????", "????", "????", |
|
|
|
"????", "????", "????", "????", "????", "????", "????", "????", |
|
|
|
"TTYP", "????", "????", "????", "????", "????", "????", "NAWS", |
|
|
|
"TRSP", "RMFC", "LIMO", "????", "EVAR" |
|
|
|
}; |
|
|
|
|
|
|
|
#define COMMAND_NAME(x) (x>=240)?telnet_commands[x-240]:"????" |
|
|
|
#define OPTION_NAME(x) (x<=36)?telnet_options[x]:"????" |
|
|
|
|
|
|
|
|
|
|
|
struct client |
|
|
|
{ |
|
|
|
int fd; |
|
|
@@ -90,7 +88,7 @@ struct client |
|
|
|
int start, stop; |
|
|
|
}; |
|
|
|
|
|
|
|
struct driver_private |
|
|
|
struct server |
|
|
|
{ |
|
|
|
unsigned int width, height; |
|
|
|
unsigned int port; |
|
|
@@ -98,6 +96,8 @@ struct driver_private |
|
|
|
struct sockaddr_in my_addr; |
|
|
|
socklen_t sin_size; |
|
|
|
|
|
|
|
/* Input buffer */ |
|
|
|
|
|
|
|
char prefix[sizeof(INIT_PREFIX)]; |
|
|
|
|
|
|
|
struct cucul_export *ex; |
|
|
@@ -108,79 +108,50 @@ struct driver_private |
|
|
|
RETSIGTYPE (*sigpipe_handler)(int); |
|
|
|
}; |
|
|
|
|
|
|
|
static void manage_connections(caca_t *kk); |
|
|
|
static int send_data(caca_t *kk, struct client *c); |
|
|
|
static void manage_connections(struct server *server); |
|
|
|
static int send_data(struct server *server, struct client *c); |
|
|
|
ssize_t nonblock_write(int fd, void *buf, size_t len); |
|
|
|
|
|
|
|
static int network_init_graphics(caca_t *kk) |
|
|
|
int main(void) |
|
|
|
{ |
|
|
|
int yes = 1, flags; |
|
|
|
int port = 0xCACA; /* 51914 */ |
|
|
|
unsigned int width = 0, height = 0; |
|
|
|
cucul_t *qq; |
|
|
|
int i, yes = 1, flags; |
|
|
|
struct server *server; |
|
|
|
char *tmp; |
|
|
|
|
|
|
|
kk->drv.p = malloc(sizeof(struct driver_private)); |
|
|
|
if(kk->drv.p == NULL) |
|
|
|
return -1; |
|
|
|
server = malloc(sizeof(struct server)); |
|
|
|
|
|
|
|
#if defined(HAVE_GETENV) |
|
|
|
tmp = getenv("CACA_PORT"); |
|
|
|
if(tmp && *tmp) |
|
|
|
{ |
|
|
|
int new_port = atoi(tmp); |
|
|
|
if(new_port) |
|
|
|
port = new_port; |
|
|
|
} |
|
|
|
|
|
|
|
tmp = getenv("CACA_GEOMETRY"); |
|
|
|
if(tmp && *tmp) |
|
|
|
sscanf(tmp, "%ux%u", &width, &height); |
|
|
|
#endif |
|
|
|
|
|
|
|
if(width && height) |
|
|
|
{ |
|
|
|
kk->drv.p->width = width; |
|
|
|
kk->drv.p->height = height; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
kk->drv.p->width = 80; |
|
|
|
kk->drv.p->height = 24; |
|
|
|
} |
|
|
|
|
|
|
|
kk->drv.p->client_count = 0; |
|
|
|
kk->drv.p->clients = NULL; |
|
|
|
kk->drv.p->port = port; |
|
|
|
|
|
|
|
_cucul_set_size(kk->qq, kk->drv.p->width, kk->drv.p->height); |
|
|
|
server->client_count = 0; |
|
|
|
server->clients = NULL; |
|
|
|
server->port = 0xCACA; /* 51914 */ |
|
|
|
|
|
|
|
/* FIXME, handle >255 sizes */ |
|
|
|
memcpy(kk->drv.p->prefix, INIT_PREFIX, sizeof(INIT_PREFIX)); |
|
|
|
tmp = strstr(kk->drv.p->prefix, "____"); |
|
|
|
tmp[0] = (unsigned char) (kk->drv.p->width & 0xff00) >> 8; |
|
|
|
tmp[1] = (unsigned char) kk->drv.p->width & 0xff; |
|
|
|
tmp[2] = (unsigned char) (kk->drv.p->height & 0xff00) >> 8; |
|
|
|
tmp[3] = (unsigned char) kk->drv.p->height & 0xff; |
|
|
|
|
|
|
|
if ((kk->drv.p->sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) |
|
|
|
memcpy(server->prefix, INIT_PREFIX, sizeof(INIT_PREFIX)); |
|
|
|
tmp = strstr(server->prefix, "____"); |
|
|
|
tmp[0] = (unsigned char) (server->width & 0xff00) >> 8; |
|
|
|
tmp[1] = (unsigned char) server->width & 0xff; |
|
|
|
tmp[2] = (unsigned char) (server->height & 0xff00) >> 8; |
|
|
|
tmp[3] = (unsigned char) server->height & 0xff; |
|
|
|
|
|
|
|
if((server->sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) |
|
|
|
{ |
|
|
|
perror("socket"); |
|
|
|
return -1; |
|
|
|
} |
|
|
|
|
|
|
|
if (setsockopt(kk->drv.p->sockfd, SOL_SOCKET, |
|
|
|
SO_REUSEADDR, &yes, sizeof(int)) == -1) |
|
|
|
if(setsockopt(server->sockfd, SOL_SOCKET, |
|
|
|
SO_REUSEADDR, &yes, sizeof(int)) == -1) |
|
|
|
{ |
|
|
|
perror("setsockopt SO_REUSEADDR"); |
|
|
|
return -1; |
|
|
|
} |
|
|
|
|
|
|
|
kk->drv.p->my_addr.sin_family = AF_INET; |
|
|
|
kk->drv.p-> my_addr.sin_port = htons(kk->drv.p->port); |
|
|
|
kk->drv.p->my_addr.sin_addr.s_addr = INADDR_ANY; |
|
|
|
memset(&(kk->drv.p->my_addr.sin_zero), '\0', 8); |
|
|
|
server->my_addr.sin_family = AF_INET; |
|
|
|
server-> my_addr.sin_port = htons(server->port); |
|
|
|
server->my_addr.sin_addr.s_addr = INADDR_ANY; |
|
|
|
memset(&(server->my_addr.sin_zero), '\0', 8); |
|
|
|
|
|
|
|
if (bind(kk->drv.p->sockfd, (struct sockaddr *)&kk->drv.p->my_addr, |
|
|
|
if(bind(server->sockfd, (struct sockaddr *)&server->my_addr, |
|
|
|
sizeof(struct sockaddr)) == -1) |
|
|
|
{ |
|
|
|
perror("bind"); |
|
|
@@ -188,108 +159,78 @@ static int network_init_graphics(caca_t *kk) |
|
|
|
} |
|
|
|
|
|
|
|
/* Non blocking socket */ |
|
|
|
flags = fcntl(kk->drv.p->sockfd, F_GETFL, 0); |
|
|
|
fcntl(kk->drv.p->sockfd, F_SETFL, flags | O_NONBLOCK); |
|
|
|
flags = fcntl(server->sockfd, F_GETFL, 0); |
|
|
|
fcntl(server->sockfd, F_SETFL, flags | O_NONBLOCK); |
|
|
|
|
|
|
|
if (listen(kk->drv.p->sockfd, BACKLOG) == -1) |
|
|
|
if(listen(server->sockfd, BACKLOG) == -1) |
|
|
|
{ |
|
|
|
perror("listen"); |
|
|
|
return -1; |
|
|
|
} |
|
|
|
|
|
|
|
kk->drv.p->ex = NULL; |
|
|
|
server->ex = NULL; |
|
|
|
|
|
|
|
/* Ignore SIGPIPE */ |
|
|
|
kk->drv.p->sigpipe_handler = signal(SIGPIPE, SIG_IGN); |
|
|
|
server->sigpipe_handler = signal(SIGPIPE, SIG_IGN); |
|
|
|
|
|
|
|
fprintf(stderr, "initialised network, listening on port %i\n", |
|
|
|
kk->drv.p->port); |
|
|
|
|
|
|
|
return 0; |
|
|
|
} |
|
|
|
server->port); |
|
|
|
|
|
|
|
static int network_end_graphics(caca_t *kk) |
|
|
|
{ |
|
|
|
int i; |
|
|
|
|
|
|
|
for(i = 0; i < kk->drv.p->client_count; i++) |
|
|
|
/* Main loop */ |
|
|
|
for(;;) |
|
|
|
{ |
|
|
|
close(kk->drv.p->clients[i].fd); |
|
|
|
kk->drv.p->clients[i].fd = -1; |
|
|
|
} |
|
|
|
/* Manage new connections as this function will be called sometimes |
|
|
|
* more often than display */ |
|
|
|
manage_connections(server); |
|
|
|
|
|
|
|
if(kk->drv.p->ex) |
|
|
|
cucul_free_export(kk->drv.p->ex); |
|
|
|
/* Read data from stdin */ |
|
|
|
/* FIXME: read data, then continue if there was a new image */ |
|
|
|
|
|
|
|
/* Restore SIGPIPE handler */ |
|
|
|
signal(SIGPIPE, kk->drv.p->sigpipe_handler); |
|
|
|
|
|
|
|
free(kk->drv.p); |
|
|
|
|
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
static int network_set_window_title(caca_t *kk, char const *title) |
|
|
|
{ |
|
|
|
/* Not handled (yet) */ |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
static unsigned int network_get_window_width(caca_t *kk) |
|
|
|
{ |
|
|
|
return kk->drv.p->width * 6; |
|
|
|
} |
|
|
|
/* Free the previous export buffer, if any */ |
|
|
|
if(server->ex) |
|
|
|
{ |
|
|
|
cucul_free_export(server->ex); |
|
|
|
server->ex = NULL; |
|
|
|
} |
|
|
|
|
|
|
|
static unsigned int network_get_window_height(caca_t *kk) |
|
|
|
{ |
|
|
|
return kk->drv.p->height * 10; |
|
|
|
} |
|
|
|
/* Get ANSI representation of the image and skip the end-of buffer |
|
|
|
* linefeed ("\r\n\0", 3 bytes) */ |
|
|
|
server->ex = cucul_create_export(qq, CUCUL_FORMAT_ANSI); |
|
|
|
server->ex->size -= 3; |
|
|
|
|
|
|
|
static void network_display(caca_t *kk) |
|
|
|
{ |
|
|
|
int i; |
|
|
|
for(i = 0; i < server->client_count; i++) |
|
|
|
{ |
|
|
|
if(server->clients[i].fd == -1) |
|
|
|
continue; |
|
|
|
|
|
|
|
/* Free the previous export buffer, if any */ |
|
|
|
if(kk->drv.p->ex) |
|
|
|
{ |
|
|
|
cucul_free_export(kk->drv.p->ex); |
|
|
|
kk->drv.p->ex = NULL; |
|
|
|
if(send_data(server, &server->clients[i])) |
|
|
|
{ |
|
|
|
fprintf(stderr, "client %i dropped connection\n", |
|
|
|
server->clients[i].fd); |
|
|
|
close(server->clients[i].fd); |
|
|
|
server->clients[i].fd = -1; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/* Get ANSI representation of the image and skip the end-of buffer |
|
|
|
* linefeed ("\r\n\0", 3 bytes) */ |
|
|
|
kk->drv.p->ex = cucul_create_export(kk->qq, CUCUL_FORMAT_ANSI); |
|
|
|
kk->drv.p->ex->size -= 3; |
|
|
|
|
|
|
|
for(i = 0; i < kk->drv.p->client_count; i++) |
|
|
|
/* Kill all remaining clients */ |
|
|
|
for(i = 0; i < server->client_count; i++) |
|
|
|
{ |
|
|
|
if(kk->drv.p->clients[i].fd == -1) |
|
|
|
if(server->clients[i].fd == -1) |
|
|
|
continue; |
|
|
|
|
|
|
|
if(send_data(kk, &kk->drv.p->clients[i])) |
|
|
|
{ |
|
|
|
fprintf(stderr, "client %i dropped connection\n", |
|
|
|
kk->drv.p->clients[i].fd); |
|
|
|
close(kk->drv.p->clients[i].fd); |
|
|
|
kk->drv.p->clients[i].fd = -1; |
|
|
|
} |
|
|
|
close(server->clients[i].fd); |
|
|
|
server->clients[i].fd = -1; |
|
|
|
} |
|
|
|
|
|
|
|
manage_connections(kk); |
|
|
|
} |
|
|
|
if(server->ex) |
|
|
|
cucul_free_export(server->ex); |
|
|
|
|
|
|
|
static void network_handle_resize(caca_t *kk) |
|
|
|
{ |
|
|
|
/* Not handled */ |
|
|
|
} |
|
|
|
/* Restore SIGPIPE handler */ |
|
|
|
signal(SIGPIPE, server->sigpipe_handler); |
|
|
|
|
|
|
|
static int network_get_event(caca_t *kk, struct caca_event *ev) |
|
|
|
{ |
|
|
|
/* Manage new connections as this function will be called sometimes |
|
|
|
* more often than display */ |
|
|
|
manage_connections(kk); |
|
|
|
free(server); |
|
|
|
|
|
|
|
/* Event not handled */ |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
@@ -297,13 +238,13 @@ static int network_get_event(caca_t *kk, struct caca_event *ev) |
|
|
|
* XXX: The following functions are local |
|
|
|
*/ |
|
|
|
|
|
|
|
static void manage_connections(caca_t *kk) |
|
|
|
static void manage_connections(struct server *server) |
|
|
|
{ |
|
|
|
int fd, flags; |
|
|
|
struct sockaddr_in remote_addr; |
|
|
|
socklen_t len = sizeof(struct sockaddr_in); |
|
|
|
|
|
|
|
fd = accept(kk->drv.p->sockfd, (struct sockaddr *)&remote_addr, &len); |
|
|
|
fd = accept(server->sockfd, (struct sockaddr *)&remote_addr, &len); |
|
|
|
if(fd == -1) |
|
|
|
return; |
|
|
|
|
|
|
@@ -314,37 +255,37 @@ static void manage_connections(caca_t *kk) |
|
|
|
flags = fcntl(fd, F_SETFL, 0); |
|
|
|
fcntl(fd, F_SETFL, flags | O_NONBLOCK); |
|
|
|
|
|
|
|
if(kk->drv.p->clients == NULL) |
|
|
|
if(server->clients == NULL) |
|
|
|
{ |
|
|
|
kk->drv.p->clients = malloc(sizeof(struct client)); |
|
|
|
if(kk->drv.p->clients == NULL) |
|
|
|
server->clients = malloc(sizeof(struct client)); |
|
|
|
if(server->clients == NULL) |
|
|
|
return; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
kk->drv.p->clients = realloc(kk->drv.p->clients, |
|
|
|
(kk->drv.p->client_count+1) * sizeof(struct client)); |
|
|
|
server->clients = realloc(server->clients, |
|
|
|
(server->client_count+1) * sizeof(struct client)); |
|
|
|
} |
|
|
|
|
|
|
|
kk->drv.p->clients[kk->drv.p->client_count].fd = fd; |
|
|
|
kk->drv.p->clients[kk->drv.p->client_count].ready = 0; |
|
|
|
kk->drv.p->clients[kk->drv.p->client_count].inbytes = 0; |
|
|
|
kk->drv.p->clients[kk->drv.p->client_count].start = 0; |
|
|
|
kk->drv.p->clients[kk->drv.p->client_count].stop = 0; |
|
|
|
server->clients[server->client_count].fd = fd; |
|
|
|
server->clients[server->client_count].ready = 0; |
|
|
|
server->clients[server->client_count].inbytes = 0; |
|
|
|
server->clients[server->client_count].start = 0; |
|
|
|
server->clients[server->client_count].stop = 0; |
|
|
|
|
|
|
|
/* If we already have data to send, send it to the new client */ |
|
|
|
if(send_data(kk, &kk->drv.p->clients[kk->drv.p->client_count])) |
|
|
|
if(send_data(server, &server->clients[server->client_count])) |
|
|
|
{ |
|
|
|
fprintf(stderr, "client %i dropped connection\n", fd); |
|
|
|
close(fd); |
|
|
|
kk->drv.p->clients[kk->drv.p->client_count].fd = -1; |
|
|
|
server->clients[server->client_count].fd = -1; |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
kk->drv.p->client_count++; |
|
|
|
server->client_count++; |
|
|
|
} |
|
|
|
|
|
|
|
static int send_data(caca_t *kk, struct client *c) |
|
|
|
static int send_data(struct server *server, struct client *c) |
|
|
|
{ |
|
|
|
ssize_t ret; |
|
|
|
|
|
|
@@ -397,7 +338,7 @@ static int send_data(caca_t *kk, struct client *c) |
|
|
|
/* Send the telnet initialisation commands */ |
|
|
|
if(!c->ready) |
|
|
|
{ |
|
|
|
ret = nonblock_write(c->fd, kk->drv.p->prefix, sizeof(INIT_PREFIX)); |
|
|
|
ret = nonblock_write(c->fd, server->prefix, sizeof(INIT_PREFIX)); |
|
|
|
if(ret == -1) |
|
|
|
return (errno == EAGAIN) ? 0 : -1; |
|
|
|
|
|
|
@@ -408,7 +349,7 @@ static int send_data(caca_t *kk, struct client *c) |
|
|
|
} |
|
|
|
|
|
|
|
/* No error, there's just nothing to send yet */ |
|
|
|
if(!kk->drv.p->ex) |
|
|
|
if(!server->ex) |
|
|
|
return 0; |
|
|
|
|
|
|
|
/* If we have backlog, send the backlog */ |
|
|
@@ -433,7 +374,7 @@ static int send_data(caca_t *kk, struct client *c) |
|
|
|
{ |
|
|
|
c->start += ret; |
|
|
|
|
|
|
|
if(c->stop - c->start + strlen(ANSI_PREFIX) + kk->drv.p->ex->size |
|
|
|
if(c->stop - c->start + strlen(ANSI_PREFIX) + server->ex->size |
|
|
|
> OUTBUFFER) |
|
|
|
{ |
|
|
|
/* Overflow! Empty buffer and start again */ |
|
|
@@ -444,7 +385,7 @@ static int send_data(caca_t *kk, struct client *c) |
|
|
|
} |
|
|
|
|
|
|
|
/* Need to move? */ |
|
|
|
if(c->stop + strlen(ANSI_PREFIX) + kk->drv.p->ex->size > OUTBUFFER) |
|
|
|
if(c->stop + strlen(ANSI_PREFIX) + server->ex->size > OUTBUFFER) |
|
|
|
{ |
|
|
|
memmove(c->outbuf, c->outbuf + c->start, c->stop - c->start); |
|
|
|
c->stop -= c->start; |
|
|
@@ -453,8 +394,8 @@ static int send_data(caca_t *kk, struct client *c) |
|
|
|
|
|
|
|
memcpy(c->outbuf + c->stop, ANSI_PREFIX, strlen(ANSI_PREFIX)); |
|
|
|
c->stop += strlen(ANSI_PREFIX); |
|
|
|
memcpy(c->outbuf + c->stop, kk->drv.p->ex->buffer, kk->drv.p->ex->size); |
|
|
|
c->stop += kk->drv.p->ex->size; |
|
|
|
memcpy(c->outbuf + c->stop, server->ex->buffer, server->ex->size); |
|
|
|
c->stop += server->ex->size; |
|
|
|
|
|
|
|
return 0; |
|
|
|
} |
|
|
@@ -474,7 +415,7 @@ static int send_data(caca_t *kk, struct client *c) |
|
|
|
|
|
|
|
if(ret < (ssize_t)strlen(ANSI_PREFIX)) |
|
|
|
{ |
|
|
|
if(strlen(ANSI_PREFIX) + kk->drv.p->ex->size > OUTBUFFER) |
|
|
|
if(strlen(ANSI_PREFIX) + server->ex->size > OUTBUFFER) |
|
|
|
{ |
|
|
|
/* Overflow! Empty buffer and start again */ |
|
|
|
memcpy(c->outbuf, ANSI_RESET, strlen(ANSI_RESET)); |
|
|
@@ -485,14 +426,14 @@ static int send_data(caca_t *kk, struct client *c) |
|
|
|
|
|
|
|
memcpy(c->outbuf, ANSI_PREFIX, strlen(ANSI_PREFIX) - ret); |
|
|
|
c->stop = strlen(ANSI_PREFIX) - ret; |
|
|
|
memcpy(c->outbuf + c->stop, kk->drv.p->ex->buffer, kk->drv.p->ex->size); |
|
|
|
c->stop += kk->drv.p->ex->size; |
|
|
|
memcpy(c->outbuf + c->stop, server->ex->buffer, server->ex->size); |
|
|
|
c->stop += server->ex->size; |
|
|
|
|
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
/* Send actual data */ |
|
|
|
ret = nonblock_write(c->fd, kk->drv.p->ex->buffer, kk->drv.p->ex->size); |
|
|
|
ret = nonblock_write(c->fd, server->ex->buffer, server->ex->size); |
|
|
|
if(ret == -1) |
|
|
|
{ |
|
|
|
if(errno == EAGAIN) |
|
|
@@ -501,9 +442,9 @@ static int send_data(caca_t *kk, struct client *c) |
|
|
|
return -1; |
|
|
|
} |
|
|
|
|
|
|
|
if(ret < (int)kk->drv.p->ex->size) |
|
|
|
if(ret < (int)server->ex->size) |
|
|
|
{ |
|
|
|
if(kk->drv.p->ex->size > OUTBUFFER) |
|
|
|
if(server->ex->size > OUTBUFFER) |
|
|
|
{ |
|
|
|
/* Overflow! Empty buffer and start again */ |
|
|
|
memcpy(c->outbuf, ANSI_RESET, strlen(ANSI_RESET)); |
|
|
@@ -512,8 +453,8 @@ static int send_data(caca_t *kk, struct client *c) |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
memcpy(c->outbuf, kk->drv.p->ex->buffer, kk->drv.p->ex->size - ret); |
|
|
|
c->stop = kk->drv.p->ex->size - ret; |
|
|
|
memcpy(c->outbuf, server->ex->buffer, server->ex->size - ret); |
|
|
|
c->stop = server->ex->size - ret; |
|
|
|
|
|
|
|
return 0; |
|
|
|
} |
|
|
@@ -546,26 +487,3 @@ ssize_t nonblock_write(int fd, void *buf, size_t len) |
|
|
|
return total; |
|
|
|
} |
|
|
|
|
|
|
|
/* |
|
|
|
* Driver initialisation |
|
|
|
*/ |
|
|
|
|
|
|
|
int network_install(caca_t *kk) |
|
|
|
{ |
|
|
|
kk->drv.driver = CACA_DRIVER_NETWORK; |
|
|
|
|
|
|
|
kk->drv.init_graphics = network_init_graphics; |
|
|
|
kk->drv.end_graphics = network_end_graphics; |
|
|
|
kk->drv.set_window_title = network_set_window_title; |
|
|
|
kk->drv.get_window_width = network_get_window_width; |
|
|
|
kk->drv.get_window_height = network_get_window_height; |
|
|
|
kk->drv.display = network_display; |
|
|
|
kk->drv.handle_resize = network_handle_resize; |
|
|
|
kk->drv.get_event = network_get_event; |
|
|
|
kk->drv.set_mouse = NULL; |
|
|
|
|
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
#endif /* USE_NETWORK */ |
|
|
|
|