gosl/src/display.c

112 lines
2.6 KiB
C
Raw Normal View History

2013-11-02 19:47:37 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ncurses.h>
2013-11-02 18:44:02 +01:00
2013-11-02 19:58:39 +01:00
#include "msg.h"
2013-11-02 18:44:02 +01:00
#include "display.h"
2013-11-02 19:47:37 +01:00
void setup_display() {
initscr(); // ncurses initialization
curs_set(0); // invisible cursor
}
void cleanup_display() {
endwin(); // clean ncurses shutdown
}
/*
image[row][col] >>> image[row*width+col];
uint32_t width; // normally 80
uint32_t height; // normally 25, may vary
char *image; // dimension is width x height
*/
2013-11-03 16:06:36 +01:00
static void print_current_image(const struct message* msg, int start, int end) {
2013-11-02 20:24:50 +01:00
end = 80; // end is ignored and 80 is used, should be handled properly later
2013-11-02 19:47:37 +01:00
2013-11-02 20:24:50 +01:00
//move(0,0); // start in the upper left corner
2013-11-03 16:06:36 +01:00
char *pic = msg->image; // get a ptr to the actual image
2013-11-02 19:47:37 +01:00
2013-11-03 16:06:36 +01:00
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
2013-11-02 20:10:52 +01:00
strcpy(line, original_line + start); // get a line from the right start
2013-11-02 20:50:42 +01:00
line[end-start] = '\0'; // terminate it
2013-11-02 20:24:50 +01:00
mvprintw(row, 0, "%s", line); // print it
2013-11-03 16:06:36 +01:00
free(line); // free it
2013-11-02 19:47:37 +01:00
}
refresh(); // refresh the screen
}
2013-11-02 22:52:42 +01:00
2013-11-02 21:45:42 +01:00
void prntscreen(const struct message *msg, const struct prog_info *pinfo);
2013-11-02 20:57:08 +01:00
void callback(const struct message *msg, const struct prog_info *pinfo) {
2013-11-02 23:14:08 +01:00
//printf("in callback, tst=%d\n", msg->timestamp);
2013-11-02 20:50:42 +01:00
2013-11-02 21:16:12 +01:00
// calculate the actual offset to use
2013-11-02 21:45:42 +01:00
//int start = msg->timestamp % msg->width + pinfo->client_offset;
//print_current_image(msg, start, start+80);
2013-11-03 16:06:36 +01:00
2013-11-02 21:45:42 +01:00
prntscreen(msg, pinfo);
2013-11-02 18:44:02 +01:00
}
2013-11-02 19:47:37 +01:00
2013-11-02 21:21:20 +01:00
2013-11-02 21:45:42 +01:00
//int init = 0;
//int rows;
2013-11-02 21:45:42 +01:00
//int cols;
2013-11-02 21:21:20 +01:00
void prntscreen(const struct message *msg, const struct prog_info *pinfo) {
static int init = 0;
static int rows;
static int cols;
2013-11-02 23:39:48 +01:00
static char *img = NULL;
static int w;
static int h;
2013-11-03 13:13:48 +01:00
if (!img && msg->image) {
img = msg->image;
w = msg->width;
h = msg->height;
}
if (!img) {
printf("awaiting state ... %d\r", msg->timestamp);
return;
}
2013-11-02 21:21:20 +01:00
if (!init) {
2013-11-02 21:45:42 +01:00
// printf("init start\n");
2013-11-02 21:21:20 +01:00
initscr();
2013-11-03 16:06:36 +01:00
curs_set(0); // invisible cursor
2013-11-02 21:21:20 +01:00
getmaxyx(stdscr, rows, cols);
init = 1;
2013-11-02 21:45:42 +01:00
// printf("init end\n");
2013-11-02 21:21:20 +01:00
}
2013-11-02 21:45:42 +01:00
int frame = msg->timestamp;
2013-11-02 21:21:20 +01:00
int right = pinfo->client_offset;
int left = right + cols;
2013-11-03 13:38:15 +01:00
// for (int y=0; y<h; y++)
2013-11-03 16:06:36 +01:00
// for (int x=0; x<w; x++)
2013-11-03 13:38:15 +01:00
// mvaddch(y,x,img[y*w+x]);
2013-11-02 21:45:42 +01:00
// printf("loop\n");
2013-11-02 23:39:48 +01:00
int rowoffset = (rows-h)/2;
2013-11-03 16:06:36 +01:00
//int coloffset = left-frame;
2013-11-02 23:39:48 +01:00
for (int y=0; y<h; y++) { // y<msg->height; y++) {
2013-11-02 21:45:42 +01:00
for (int x=left-frame; x<cols; x++) {
2013-11-03 16:06:36 +01:00
if (x<0)
2013-11-03 13:38:15 +01:00
continue;
2013-11-03 13:13:48 +01:00
//mvaddch(y + rowoffset, x, ('0' + x-(left-frame)+y));
int p = x-(left-frame);
mvaddch(y + rowoffset, x, p>=w?' ':img[y*w+p]);
2013-11-02 21:45:42 +01:00
}
}
2013-11-02 21:21:20 +01:00
refresh();
}