86 lines
2.3 KiB
C
86 lines
2.3 KiB
C
/* ************************************************************************** */
|
|
/* _____ _ */
|
|
/* protocol/getscreen.c |_ _|__ _ _| |__ ___ _ _ */
|
|
/* | Project: libp7 | |/ _ \| | | | '_ \ / _ \ | | | */
|
|
/* | | (_) | |_| | | | | __/ |_| | */
|
|
/* By: thomas <thomas@touhey.fr> |_|\___/ \__,_|_| |_|\___|\__, |.fr */
|
|
/* Last updated: 2016/10/13 07:33:17 |___/ */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
#include <libp7/internals.h>
|
|
|
|
/**
|
|
* p7_getscreen_adapt:
|
|
* Adapts the pixels from the received VRAM.
|
|
*
|
|
* @arg pixels the pixels matrix
|
|
* @arg w pointer to the width var
|
|
* @arg h pointer to the height var
|
|
* @arg scr the answer picture type
|
|
*/
|
|
|
|
static void p7_getscreen_adapt(uint32_t **pixels, int *w, int *h,
|
|
p7_packetscr_t *scr)
|
|
{
|
|
unsigned char *v = scr->vram;
|
|
|
|
switch (scr->type) {
|
|
/* simple 128x64 monochromic rectangle */
|
|
case p7_pscr_typ01:
|
|
*w = 128; *h = 64;
|
|
int bit = 128;
|
|
for (int y = 0; y < 64; y++) for (int x = 0; x < 128; x++) {
|
|
/* get pixel and shift */
|
|
pixels[y][x] = (*v & bit) ? 0x000000 : 0xFFFFFF;
|
|
|
|
/* go to next bit */
|
|
bit >>= 1; if (!bit) { bit = 128; v++; }
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* p7_getscreen:
|
|
* Get the screen.
|
|
*
|
|
* @arg handle the libp7 handle
|
|
* @arg callback the main callback for the function.
|
|
* for each correctly received frame, the function will
|
|
* be called with the dimensions (w, h) and the pixels
|
|
* matrix.
|
|
* if it returns 0, the function stops.
|
|
* @return if it worked
|
|
*/
|
|
|
|
int p7_getscreen(p7_handle_t *handle, int (*callback)(int, int, uint32_t**))
|
|
{
|
|
int err;
|
|
|
|
/* make checks */
|
|
chk_handle(handle);
|
|
chk_passive(handle);
|
|
|
|
/* main loop */
|
|
while (1) {
|
|
/* get packet */
|
|
if ((err = p7_recv(handle, 0)))
|
|
return (err);
|
|
|
|
/* check type */
|
|
if (response.type != p7_pt_ohp)
|
|
return (p7_error_unknown);
|
|
|
|
/* convert */
|
|
int w, h;
|
|
p7_getscreen_adapt(handle->_pixels, &w, &h, &response.screen);
|
|
|
|
/* then call back the callback */
|
|
if (!(*callback)(w, h, handle->_pixels))
|
|
break;
|
|
}
|
|
|
|
/* stop */
|
|
return (0);
|
|
}
|