some refactoring

This commit is contained in:
Lennart Buhl 2013-11-03 16:06:36 +01:00
parent 9cf94bbcbb
commit 66a4e9bf50
4 changed files with 28 additions and 65 deletions

View File

@ -5,7 +5,6 @@
#include "msg.h"
#include "display.h"
#include "ger.h"
void setup_display() {
initscr(); // ncurses initialization
@ -23,20 +22,20 @@ void cleanup_display() {
uint32_t height; // normally 25, may vary
char *image; // dimension is width x height
*/
statisch nichts print_current_image(konstant struktur message* msg, zahl start, zahl end) {
static void print_current_image(const struct message* msg, int start, int end) {
end = 80; // end is ignored and 80 is used, should be handled properly later
//move(0,0); // start in the upper left corner
zeichen *pic = msg->image; // get a ptr to the actual image
char *pic = msg->image; // get a ptr to the actual image
fuer (zahl row=0; row < 25; row++) { // iterate over rows
zeichen *original_line = pic + row * msg->width; // pointer to the start of the line to print
zeichen *line = (zeichen*) reserviere(end-start+1); // allocate a line because we modify it
for (int row=0; row < 25; row++) { // iterate over rows
char *original_line = pic + row * msg->width; // pointer to the start of the line to print
char *line = (char*) malloc(end-start+1); // allocate a line because we modify it
strcpy(line, original_line + start); // get a line from the right start
line[end-start] = '\0'; // terminate it
mvprintw(row, 0, "%s", line); // print it
befreie(line); // free it
free(line); // free it
}
refresh(); // refresh the screen
@ -50,8 +49,8 @@ void callback(const struct message *msg, const struct prog_info *pinfo) {
// calculate the actual offset to use
//int start = msg->timestamp % msg->width + pinfo->client_offset;
//print_current_image(msg, start, start+80);
prntscreen(msg, pinfo);
}
@ -79,6 +78,7 @@ void prntscreen(const struct message *msg, const struct prog_info *pinfo) {
if (!init) {
// printf("init start\n");
initscr();
curs_set(0); // invisible cursor
getmaxyx(stdscr, rows, cols);
init = 1;
@ -90,16 +90,16 @@ void prntscreen(const struct message *msg, const struct prog_info *pinfo) {
int left = right + cols;
// for (int y=0; y<h; y++)
// for (int x=0; x<w; x++)
// for (int x=0; x<w; x++)
// mvaddch(y,x,img[y*w+x]);
// printf("loop\n");
int rowoffset = (rows-h)/2;
int coloffset = left-frame;
//int coloffset = left-frame;
for (int y=0; y<h; y++) { // y<msg->height; y++) {
for (int x=left-frame; x<cols; x++) {
if (x<0)
if (x<0)
continue;
//mvaddch(y + rowoffset, x, ('0' + x-(left-frame)+y));
int p = x-(left-frame);

View File

@ -1,35 +0,0 @@
#ifndef __GER
#define __GER
#define wenn if
#define sonst else
#define fuer for
#define waehrend while
#define tue do
#define statisch static
#define konstant const
#define struktur struct
#define nichts void
#define wahrheitswert bool
#define zeichen char
#define kurz short
#define zahl int
#define lang long
#define fliess float
#define doppel double
#define wahr true
#define falsch false
#define zurueckliefern return
#define haupt main
#define druckenf printf
#define schlaf sleep
#define uschlaf usleep
#define reserviere malloc
#define befreie free
#endif

View File

@ -32,11 +32,11 @@ int parseArgs(struct prog_info *pinfo, int argc, char **argv) {
printf( " -c <off> run in client mode\n");
printf( " off is the column offset to use.\n");
printf( " -p <port> use the specified port\n");
printf( " -t <fps> when in server mode: update <fps> times\n");
printf( " -t <fps> when in server mode: update <fps> times\n");
printf( " per second. Valid range: 2 - 99\n");
printf( " ignored in client mode\n");
printf( " -w <width> maximum width of the whole field. server only.\n");
printf( " -i <img> image fielname. server only\n");
printf( " -i <img> image filename. server only\n");
printf("\n\n");
return -1;
}

View File

@ -1,5 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h> // ?
#include <errno.h> // geguttenbergt aus bejees networking guide
@ -40,14 +41,14 @@ int run_server(const struct prog_info *pinfo, char *img, int w, int h) {
struct addrinfo hints, *servinfo, *p;
int ret;
int sockfd;
char portbuf[6];
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
sprintf(portbuf, "%d", pinfo->port);
if ((ret=getaddrinfo("255.255.255.255", portbuf, &hints, &servinfo)) != 0) {
if ((ret = getaddrinfo("255.255.255.255", portbuf, &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
return 1;
}
@ -71,7 +72,7 @@ int run_server(const struct prog_info *pinfo, char *img, int w, int h) {
// Einem Socket muss das Broadcasting explizit erlaubt werden:
int broadcastPermission = 1;
if (setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, (void *) &broadcastPermission,sizeof(broadcastPermission)) < 0){
if (setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, (void *) &broadcastPermission, sizeof(broadcastPermission)) < 0){
fprintf(stderr, "setsockopt error");
exit(1);
}
@ -84,19 +85,16 @@ int run_server(const struct prog_info *pinfo, char *img, int w, int h) {
t++;
t %= pinfo->width;
struct message *outmsg = (struct message *)malloc(sizeof(struct message));
struct message *outmsg = (struct message *) malloc(sizeof(struct message));
outmsg->timestamp = (uint32_t)t; //(uint32_t)time(NULL);
if ((t % 100) == 0) {
outmsg->width = w;
outmsg->height = h;
outmsg->image = img;
} else {
outmsg->width = 0;
outmsg->height = 0;
outmsg->image = NULL;
}
// send image every 100 frames
bool sendimg = (t % 100) == 0;
outmsg->width = sendimg ? w : 0;
outmsg->height = sendimg ? h : 0;
outmsg->image = sendimg ? img : NULL;
int buflen = getBufferSize(outmsg);
char *outbuf = (char *)malloc(buflen);
char *outbuf = (char *) malloc(buflen);
serialize(outbuf, outmsg);
if ((numbytes = sendto(sockfd, outbuf, buflen, 0, p->ai_addr, p->ai_addrlen)) == -1) {
@ -143,12 +141,12 @@ int run_client(const struct prog_info *pinfo, void (*framecallback)(const struct
for (info = servinfo; info != NULL; info = info->ai_next) {
if ((sockfd = socket(info->ai_family, info->ai_socktype, info->ai_protocol)) == -1) {
perror("sock");
continue;
continue;
}
if (bind(sockfd, info->ai_addr, info->ai_addrlen) == -1) {
perror("bind");
continue;
continue;
}
break;