doorbell

diff doorbelld/src/srv.c @ 5:f21ae31ef0e7

craptacular
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 15 Mar 2016 08:35:21 +0200
parents 08ea0abdbb8a
children 026156ea8801
line diff
     1.1 --- a/doorbelld/src/srv.c	Mon Mar 14 04:20:59 2016 +0200
     1.2 +++ b/doorbelld/src/srv.c	Tue Mar 15 08:35:21 2016 +0200
     1.3 @@ -1,6 +1,8 @@
     1.4  #include <stdio.h>
     1.5  #include <stdlib.h>
     1.6  #include <string.h>
     1.7 +#include <stddef.h>
     1.8 +#include <stdint.h>
     1.9  #include <errno.h>
    1.10  #include <assert.h>
    1.11  #include <unistd.h>
    1.12 @@ -35,7 +37,7 @@
    1.13  	csock[0] = lis_sock;
    1.14  	max_socket = lis_sock;
    1.15  
    1.16 -	fcntl(lis_sock, F_SETFD, fcntl(lis_sock, F_GETFD) | O_NONBLOCK);
    1.17 +	fcntl(lis_sock, F_SETFL, fcntl(lis_sock, F_GETFL) | O_NONBLOCK);
    1.18  
    1.19  	memset(&addr, 0, sizeof addr);
    1.20  	addr.sin_family = AF_INET;
    1.21 @@ -99,7 +101,7 @@
    1.22  		if(s > max_socket) {
    1.23  			max_socket = s;
    1.24  		}
    1.25 -		fcntl(s, F_SETFD, fcntl(s, F_GETFD) | O_NONBLOCK);
    1.26 +		fcntl(s, F_SETFL, fcntl(s, F_GETFL) | O_NONBLOCK);
    1.27  		return 0;
    1.28  	}
    1.29  
    1.30 @@ -129,22 +131,54 @@
    1.31  }
    1.32  
    1.33  struct video_block {
    1.34 +	unsigned int magic;
    1.35  	int frame;
    1.36  	int x, y;
    1.37  	int width, height;
    1.38 +	int frame_width, frame_height;
    1.39  	char pixels[1];
    1.40  };
    1.41  #define VBLOCK_HEADER_SIZE	(offsetof(struct video_block, pixels))
    1.42  
    1.43  void srv_send_frame(unsigned char *frame, int xsz, int ysz)
    1.44  {
    1.45 +	static int frame_num;
    1.46  	static unsigned char *buffer;
    1.47 +	int i, j, size, num_clients;
    1.48  	struct video_block *vblock;
    1.49 +	uint16_t *dest;
    1.50 +
    1.51 +	printf("sending frame\n");
    1.52  
    1.53  	/* for now just send a big block */
    1.54 -	if(!buffer && !(buffer = malloc(xsz * ysz * 2) + VBLOCK_HEADER_SIZE)) {
    1.55 +	size = xsz * ysz * 2 + VBLOCK_HEADER_SIZE;
    1.56 +	if(!buffer && !(buffer = malloc(size))) {
    1.57  		fprintf(stderr, "failed to allocate frame send buffer\n");
    1.58  		return;
    1.59  	}
    1.60 -	vblock = buffer;
    1.61 +	vblock = (struct video_block*)buffer;
    1.62 +
    1.63 +	vblock->magic = 0x12341234;
    1.64 +	vblock->frame = frame_num++;
    1.65 +	vblock->x = vblock->y = 0;
    1.66 +	vblock->width = vblock->frame_width = xsz;
    1.67 +	vblock->height = vblock->frame_height = ysz;
    1.68 +
    1.69 +	dest = (uint16_t*)vblock->pixels;
    1.70 +
    1.71 +	for(i=0; i<ysz; i++) {
    1.72 +		for(j=0; j<xsz; j++) {
    1.73 +			unsigned int r = frame[0];
    1.74 +			unsigned int g = frame[1];
    1.75 +			unsigned int b = frame[2];
    1.76 +
    1.77 +			*dest++ = ((r << 8) & 0xf800) | ((g << 3) & 0x7e0) | ((b >> 3) & 0x1f);
    1.78 +			frame += 4;
    1.79 +		}
    1.80 +	}
    1.81 +
    1.82 +	num_clients = dynarr_size(csock);
    1.83 +	for(i=1; i<num_clients; i++) {	/* first socket is the listening socket */
    1.84 +		write(csock[i], buffer, size);
    1.85 +	}
    1.86  }