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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
/**
* FreeRDP: A Remote Desktop Protocol Implementation
* OS X Server Event Handling
*
* Copyright 2012 Corey Clayton <can.of.tuna@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <dispatch/dispatch.h>
#include <CoreGraphics/CoreGraphics.h>
#include <CoreVideo/CoreVideo.h>
#include <IOKit/IOKitLib.h>
#include <IOSurface/IOSurface.h>
#include "mf_mountain_lion.h"
dispatch_semaphore_t region_sem;
dispatch_semaphore_t data_sem;
dispatch_queue_t screen_update_q;
CGDisplayStreamRef stream;
CGDisplayStreamUpdateRef lastUpdate = NULL;
BYTE* localBuf = NULL;
BOOL ready = FALSE;
void (^streamHandler)(CGDisplayStreamFrameStatus, uint64_t, IOSurfaceRef,
CGDisplayStreamUpdateRef) = ^(CGDisplayStreamFrameStatus status,
uint64_t displayTime, IOSurfaceRef frameSurface,
CGDisplayStreamUpdateRef updateRef) {
dispatch_semaphore_wait(region_sem, DISPATCH_TIME_FOREVER);
// may need to move this down
if (ready == TRUE)
{
RFX_RECT rect;
unsigned long offset_beg;
unsigned long stride;
rect.x = 0;
rect.y = 0;
rect.width = 0;
rect.height = 0;
mf_mlion_peek_dirty_region(&rect);
// lock surface
IOSurfaceLock(frameSurface, kIOSurfaceLockReadOnly, NULL);
// get pointer
void* baseAddress = IOSurfaceGetBaseAddress(frameSurface);
// copy region
stride = IOSurfaceGetBytesPerRow(frameSurface);
// memcpy(localBuf, baseAddress + offset_beg, surflen);
for (int i = 0; i < rect.height; i++)
{
offset_beg = (stride * (rect.y + i) + (rect.x * 4));
memcpy(localBuf + offset_beg, baseAddress + offset_beg, rect.width * 4);
}
// unlock surface
IOSurfaceUnlock(frameSurface, kIOSurfaceLockReadOnly, NULL);
ready = FALSE;
dispatch_semaphore_signal(data_sem);
}
if (status != kCGDisplayStreamFrameStatusFrameComplete)
{
switch (status)
{
case kCGDisplayStreamFrameStatusFrameIdle:
break;
case kCGDisplayStreamFrameStatusStopped:
break;
case kCGDisplayStreamFrameStatusFrameBlank:
break;
default:
break;
}
}
else if (lastUpdate == NULL)
{
CFRetain(updateRef);
lastUpdate = updateRef;
}
else
{
CGDisplayStreamUpdateRef tmpRef;
tmpRef = lastUpdate;
lastUpdate = CGDisplayStreamUpdateCreateMergedUpdate(tmpRef, updateRef);
CFRelease(tmpRef);
}
dispatch_semaphore_signal(region_sem);
};
int mf_mlion_display_info(UINT32* disp_width, UINT32* disp_height, UINT32* scale)
{
CGDirectDisplayID display_id;
display_id = CGMainDisplayID();
CGDisplayModeRef mode = CGDisplayCopyDisplayMode(display_id);
size_t pixelWidth = CGDisplayModeGetPixelWidth(mode);
// size_t pixelHeight = CGDisplayModeGetPixelHeight(mode);
size_t wide = CGDisplayPixelsWide(display_id);
size_t high = CGDisplayPixelsHigh(display_id);
CGDisplayModeRelease(mode);
*disp_width = wide; // pixelWidth;
*disp_height = high; // pixelHeight;
*scale = pixelWidth / wide;
return 0;
}
int mf_mlion_screen_updates_init()
{
CGDirectDisplayID display_id;
display_id = CGMainDisplayID();
screen_update_q = dispatch_queue_create("mfreerdp.server.screenUpdate", NULL);
region_sem = dispatch_semaphore_create(1);
data_sem = dispatch_semaphore_create(1);
UINT32 pixelWidth;
UINT32 pixelHeight;
UINT32 scale;
mf_mlion_display_info(&pixelWidth, &pixelHeight, &scale);
localBuf = malloc(pixelWidth * pixelHeight * 4);
if (!localBuf)
return -1;
CFDictionaryRef opts;
void* keys[2];
void* values[2];
keys[0] = (void*)kCGDisplayStreamShowCursor;
values[0] = (void*)kCFBooleanFalse;
opts = CFDictionaryCreate(kCFAllocatorDefault, (const void**)keys, (const void**)values, 1,
NULL, NULL);
stream = CGDisplayStreamCreateWithDispatchQueue(display_id, pixelWidth, pixelHeight, 'BGRA',
opts, screen_update_q, streamHandler);
CFRelease(opts);
return 0;
}
int mf_mlion_start_getting_screen_updates()
{
CGError err;
err = CGDisplayStreamStart(stream);
if (err != kCGErrorSuccess)
{
return 1;
}
return 0;
}
int mf_mlion_stop_getting_screen_updates()
{
CGError err;
err = CGDisplayStreamStop(stream);
if (err != kCGErrorSuccess)
{
return 1;
}
return 0;
}
int mf_mlion_get_dirty_region(RFX_RECT* invalid)
{
dispatch_semaphore_wait(region_sem, DISPATCH_TIME_FOREVER);
if (lastUpdate != NULL)
{
mf_mlion_peek_dirty_region(invalid);
}
dispatch_semaphore_signal(region_sem);
return 0;
}
int mf_mlion_peek_dirty_region(RFX_RECT* invalid)
{
size_t num_rects;
CGRect dirtyRegion;
const CGRect* rects =
CGDisplayStreamUpdateGetRects(lastUpdate, kCGDisplayStreamUpdateDirtyRects, &num_rects);
if (num_rects == 0)
{
return 0;
}
dirtyRegion = *rects;
for (size_t i = 0; i < num_rects; i++)
{
dirtyRegion = CGRectUnion(dirtyRegion, *(rects + i));
}
invalid->x = dirtyRegion.origin.x;
invalid->y = dirtyRegion.origin.y;
invalid->height = dirtyRegion.size.height;
invalid->width = dirtyRegion.size.width;
return 0;
}
int mf_mlion_clear_dirty_region()
{
dispatch_semaphore_wait(region_sem, DISPATCH_TIME_FOREVER);
CFRelease(lastUpdate);
lastUpdate = NULL;
dispatch_semaphore_signal(region_sem);
return 0;
}
int mf_mlion_get_pixelData(long x, long y, long width, long height, BYTE** pxData)
{
dispatch_semaphore_wait(region_sem, DISPATCH_TIME_FOREVER);
ready = TRUE;
dispatch_semaphore_wait(data_sem, DISPATCH_TIME_FOREVER);
dispatch_semaphore_signal(region_sem);
// this second wait allows us to block until data is copied... more on this later
dispatch_semaphore_wait(data_sem, DISPATCH_TIME_FOREVER);
*pxData = localBuf;
dispatch_semaphore_signal(data_sem);
return 0;
}
|