-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathcmd_image.c
More file actions
237 lines (189 loc) · 5.34 KB
/
cmd_image.c
File metadata and controls
237 lines (189 loc) · 5.34 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright (c) 2021 David Helkowski
// Anti-Corruption License
#include<CoreFoundation/CoreFoundation.h>
#include"uclop.h"
#include"cfutil.h"
#include"mobiledevice.h"
#include"service.h"
#include"services.h"
#ifdef NNG
#include<nng/nng.h>
#include<nng/protocol/reqrep0/rep.h>
#endif
static ucmd *g_cmd = NULL;
void runImage( void *device );
void run_img( ucmd *cmd ) { g_cmd = cmd; waitForConnect( runImage ); }
void runIServer( void *device );
void run_iserver( ucmd *cmd ) { g_cmd = cmd; waitForConnect( runIServer ); }
void fatal( char *str, int rv ) {
fprintf(stderr,"Err %s ; rv=%d\n", str, rv );
//exit(1);
}
void *initScreenshotService( void *device ) {
void *imgService = activateScreenshotService( device );
CFTypeRef info = NULL;
exitOnError(
AMDServiceConnectionReceiveMessage( imgService, &info, nil ),
"Receive Intro" );
exitOnError(
AMDServiceConnectionSendMessage(
imgService,
genarr( 2,
CFSTR("DLMessageVersionExchange"),
CFSTR("DLVersionsOk")
), kCFPropertyListXMLFormat_v1_0
),
"Accept Version"
);
exitOnError(
AMDServiceConnectionReceiveMessage(imgService, &info, nil),
"Receive Version Accept" );
return imgService;
}
void *getImage( void *imgService, size_t *lenOut ) {
exitOnError(
AMDServiceConnectionSendMessage(
imgService,
genarr( 2,
CFSTR( "DLMessageProcessMessage" ),
genmap( 2, "MessageType", CFSTR("ScreenShotRequest") )
), kCFPropertyListXMLFormat_v1_0
),
"Send SC Request"
);
CFTypeRef result = NULL;
exitOnError(
AMDServiceConnectionReceiveMessage( imgService, &result, nil ),
"Get SC"
);
if( result ) {
CFDictionaryRef data = CFArrayGetValueAtIndex( result, 1 );
if( data ) {
CFTypeRef image = CFDictionaryGetValue( data, CFSTR("ScreenShotData") );
if( image ) {
int len = CFDataGetLength( image );
CFRange range = CFRangeMake(0,len);
char *bytes = malloc( len );
CFDataGetBytes( image, range, (UInt8 *) bytes );
*lenOut = len;
CFRelease( result );
return bytes;
}
}
}
CFRelease( result );
return 0;
}
#ifdef NNG
void runIServer( void *device ) {
if( !desired_device( device, g_cmd ) ) return;
void *imgService = initScreenshotService( device );
int port = 8271;
char *portStr = ucmd__get( g_cmd, "-port" );
if( portStr ) port = atoi( portStr );
char url[50];
sprintf( url, "tcp://127.0.0.1:%d", port );
nng_socket sock;
int rv;
rv = nng_rep0_open( &sock );
if( rv ) fatal("nng_rep0_open",rv);
rv = nng_listen( sock, url, NULL, 0 );
if( rv ) fatal("nng_listen",rv);
printf("listening on %s\n", url );
fflush( stdout );
void *lastdata;
size_t lastlen;
int n = 0;
for(;;) {
//printf("n:%i\n", n );
char *buf = NULL;
size_t size;
rv = nng_recv( sock, &buf, &size, NNG_FLAG_ALLOC );
if( rv ) fatal("nng_recv",rv);
if( !size ) fatal("recv empty",0);
//printf("Received %.*s\n", (int) size, buf );
if( buf && !strncmp( buf, "img", 3 ) ) {
char *left = &buf[ 4 ];
int id = atoi( left );
printf("id:%d\n", id );
/*if( id <= n ) {
nng_free( buf, size );
printf("Duplicate; size=%i\n", lastlen);
fflush( stdout );
nng_send( sock, lastdata, lastlen, 0 );
continue;
}*/
n = id;
size_t imgLen;
printf("Fetching image\n");
void *data = getImage( imgService, &imgLen );
printf("Fetch done; got %i\n", imgLen );
fflush( stdout );
if( data && imgLen ) {
rv = nng_send( sock, data, imgLen, 0 );
if( lastdata ) free( lastdata );
lastdata = data;
lastlen = imgLen;
//free( data );
}
else {
fprintf(stderr, "Did not get image\n" );
fflush( stderr );
}
//nng_send( sock, "test", 5, 0 );
}
else {
rv = nng_send( sock, "what?", 6, 0 );
}
nng_free( buf, size );
}
exit(0);
}
#else
#include "sandbird/sandbird.h"
void *imgService;
static int event_handler(sb_Event *e) {
if( e->type == SB_EV_REQUEST ) {
size_t imgLen;
printf("Fetching image\n");
void *data = getImage( imgService, &imgLen );
sb_send_status( e->stream, 200, "OK" );
sb_send_header( e->stream, "Content-Type", "image/png" );
sb_write( e->stream, data, imgLen );
}
return SB_RES_OK;
}
void runIServer( void *device ) {
if( !desired_device( device, g_cmd ) ) return;
imgService = initScreenshotService( device );
char *portStr = ucmd__get( g_cmd, "-port" );
if( !portStr ) portStr = "8271";
sb_Server *srv;
sb_Options opt;
memset(&opt, 0, sizeof(opt));
opt.port = portStr;
opt.handler = event_handler;
srv = sb_new_server( &opt );
for (;;) {
sb_poll_server(srv, 200);
}
sb_close_server(srv);
}
#endif
void runImage( void *device ) {
if( !desired_device( device, g_cmd ) ) return;
void *imgService = initScreenshotService( device );
size_t imgLen;
void *data = getImage( imgService, &imgLen );
if( data ) {
FILE *fh = fopen( "test.png", "w" );
if( !fh ) {
printf("Could not open test.png for writing\n");
exit(1);
}
fwrite( data, imgLen, 1, fh );
free( data );
fclose( fh );
}
exit(0);
}