/
support.c
79 lines (62 loc) · 1.99 KB
/
support.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* Copyright 2014, Vidar Hokstad <vidar@hokstad.com>
*
* Licensed under the LGPL - see LICENSE
*
*/
#include "SDL_vnc.h"
#ifdef DEBUG
#define DBMESSAGE printf
#else
#define DBMESSAGE //
#endif
#define CHECKED_READ(vnc, dest, len, message) { \
int result = Recv(vnc->socket, dest, len, 0); \
if (result!=len) { \
printf("Error reading %s. Got %i of %i bytes.\n", message, result, len); \
return 0; \
} \
}
void vnc_to_sdl_rect(tSDL_vnc_rect * src, SDL_Rect * dest);
void GrowUpdateRegion(tSDL_vnc *vnc, SDL_Rect *trec);
int read_raw(tSDL_vnc * vnc, tSDL_vnc_rect rect) {
SDL_Rect trec;
vnc_to_sdl_rect(&rect, &trec);
SDL_LockMutex(vnc->mutex);
SDL_LockSurface(vnc->framebuffer);
uint32_t pitch = vnc->framebuffer->pitch/4;
uint32_t * dest = (uint32_t *)vnc->framebuffer->pixels + (rect.y * pitch) + rect.x;
uint32_t len = rect.width * rect.height * 4;
DBMESSAGE("Reading %ld bytes straight into buffer", len);
CHECKED_READ(vnc, dest, len, "framebuffer");
GrowUpdateRegion(vnc,&trec);
SDL_UnlockSurface(vnc->framebuffer);
SDL_UnlockMutex(vnc->mutex);
return 1;
}
void blit_raw(tSDL_vnc * vnc, tSDL_vnc_rect rect)
{
SDL_Rect trec;
vnc_to_sdl_rect(&rect, &trec);
SDL_LockMutex(vnc->mutex);
SDL_LockSurface(vnc->framebuffer);
uint32_t * src = vnc->rawbuffer;
uint32_t srcpitch = rect.width;
uint32_t len = srcpitch * 4;
uint32_t pitch = vnc->framebuffer->pitch/4;
uint32_t * dest = (uint32_t *)vnc->framebuffer->pixels + (rect.y * pitch) + rect.x;
if (srcpitch == pitch) {
/* full screen, or at least full width */
memcpy(dest, src, len * rect.height);
} else {
uint32_t * end = dest + (pitch*rect.height);
while (dest < end) {
memcpy(dest, src, len);
dest += pitch;
src += srcpitch;
}
}
GrowUpdateRegion(vnc,&trec);
SDL_UnlockSurface(vnc->framebuffer);
SDL_UnlockMutex(vnc->mutex);
}