]> git.saurik.com Git - veency.git/blob - iphonevnc/iPhoneVNC.mm
eac7e0f36ec550956b62b4854cd1657dfa23c980
[veency.git] / iphonevnc / iPhoneVNC.mm
1 #import <CoreFoundation/CFData.h>
2 #import <CoreGraphics/CGBitmapContext.h>
3
4 #include <stdint.h>
5 #include <stdlib.h>
6 #include <rfb/rfb.h>
7
8 extern "C" UIImage *UIGetScreenImage();
9
10 static const size_t Width = 320;
11 static const size_t Height = 480;
12 static const size_t BytesPerPixel = 4;
13 static const size_t BitsPerComponent = 8;
14
15 static const size_t Stride = Width * BytesPerPixel;
16 static const size_t Size32 = Width * Height;
17 static const size_t Size8 = Size32 * BytesPerPixel;
18
19 CGContextRef CreateContext() {
20 uint8_t *buffer = (uint8_t *) malloc(Size8);
21 if (buffer == NULL)
22 return NULL;
23
24 CGColorSpaceRef space = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
25
26 CGContextRef context = CGBitmapContextCreate(buffer, Width, Height, BitsPerComponent, Stride, space, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
27 if (context == NULL)
28 free(buffer);
29
30 CGColorSpaceRelease(space);
31 return context;
32 }
33
34 int main(int argc, char *argv[]) {
35 CGContextRef context0 = CreateContext();
36 CGContextRef context1 = CreateContext();
37
38 CGRect rect = CGRectMake(0, 0, Width, Height);
39
40 rfbScreenInfoPtr screen = rfbGetScreen(&argc, argv, Width, Height, BitsPerComponent, 3, BytesPerPixel);
41
42 screen->desktopName = "iPhone";
43 screen->alwaysShared = TRUE;
44
45 rfbInitServer(screen);
46
47 for (;;) {
48 CGContextRef context = context1;
49 context1 = context0;
50 context0 = context;
51
52 uint8_t *buffer0 = (uint8_t *) CGBitmapContextGetData(context0);
53 uint8_t *buffer1 = (uint8_t *) CGBitmapContextGetData(context1);
54 screen->frameBuffer = (char *) buffer0;
55
56 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
57
58 UIImageRef *image = UIGetScreenImage();
59 CGImageRef ref = [image CGImage];
60 CGContextDrawImage(context0, rect, ref);
61
62 [pool release];
63
64 if (memcmp(buffer0, buffer1, Size8) != 0)
65 rfbMarkRectAsModified(screen, 0, 0, Width, Height);
66 rfbProcessEvents(screen, 100000);
67 }
68
69
70 rfbScreenCleanup(screen);
71
72 return 0;
73 }