FreeRDP/server/Mac/mf_mountain_lion.c

271 lines
6.2 KiB
C
Raw Normal View History

/**
* 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;
2019-11-06 17:24:51 +03:00
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;
int i;
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 (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)
{
2013-02-20 00:06:42 +04:00
CGDirectDisplayID display_id;
2019-11-06 17:24:51 +03:00
2013-02-20 00:06:42 +04:00
display_id = CGMainDisplayID();
2019-11-06 17:24:51 +03:00
2013-02-20 00:06:42 +04:00
CGDisplayModeRef mode = CGDisplayCopyDisplayMode(display_id);
2019-11-06 17:24:51 +03:00
2013-02-20 00:06:42 +04:00
size_t pixelWidth = CGDisplayModeGetPixelWidth(mode);
2019-11-06 17:24:51 +03:00
// size_t pixelHeight = CGDisplayModeGetPixelHeight(mode);
2013-02-20 00:06:42 +04:00
size_t wide = CGDisplayPixelsWide(display_id);
size_t high = CGDisplayPixelsHigh(display_id);
2019-11-06 17:24:51 +03:00
2013-02-20 00:06:42 +04:00
CGDisplayModeRelease(mode);
2019-11-06 17:24:51 +03:00
*disp_width = wide; // pixelWidth;
*disp_height = high; // pixelHeight;
2013-02-20 00:06:42 +04:00
*scale = pixelWidth / wide;
2019-11-06 17:24:51 +03:00
2013-02-20 00:06:42 +04:00
return 0;
}
int mf_mlion_screen_updates_init()
{
2013-02-20 00:06:42 +04:00
CGDirectDisplayID display_id;
2019-11-06 17:24:51 +03:00
2013-02-20 00:06:42 +04:00
display_id = CGMainDisplayID();
2019-11-06 17:24:51 +03:00
2013-02-20 00:06:42 +04:00
screen_update_q = dispatch_queue_create("mfreerdp.server.screenUpdate", NULL);
2019-11-06 17:24:51 +03:00
2013-02-20 00:06:42 +04:00
region_sem = dispatch_semaphore_create(1);
data_sem = dispatch_semaphore_create(1);
2019-11-06 17:24:51 +03:00
2013-02-20 00:06:42 +04:00
UINT32 pixelWidth;
UINT32 pixelHeight;
UINT32 scale;
2019-11-06 17:24:51 +03:00
2013-02-20 00:06:42 +04:00
mf_mlion_display_info(&pixelWidth, &pixelHeight, &scale);
2019-11-06 17:24:51 +03:00
2013-02-20 00:06:42 +04:00
localBuf = malloc(pixelWidth * pixelHeight * 4);
if (!localBuf)
return -1;
2019-11-06 17:24:51 +03:00
CFDictionaryRef opts;
2019-11-06 17:24:51 +03:00
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);
2019-11-06 17:24:51 +03:00
2013-02-20 00:06:42 +04:00
return 0;
}
int mf_mlion_start_getting_screen_updates()
{
2013-02-20 00:06:42 +04:00
CGError err;
2019-11-06 17:24:51 +03:00
2013-02-20 00:06:42 +04:00
err = CGDisplayStreamStart(stream);
2019-11-06 17:24:51 +03:00
2014-09-13 20:33:33 +04:00
if (err != kCGErrorSuccess)
2013-02-20 00:06:42 +04:00
{
return 1;
}
2019-11-06 17:24:51 +03:00
2013-02-20 00:06:42 +04:00
return 0;
}
int mf_mlion_stop_getting_screen_updates()
{
2013-02-20 00:06:42 +04:00
CGError err;
2019-11-06 17:24:51 +03:00
2013-02-20 00:06:42 +04:00
err = CGDisplayStreamStop(stream);
2019-11-06 17:24:51 +03:00
2014-09-13 20:33:33 +04:00
if (err != kCGErrorSuccess)
2013-02-20 00:06:42 +04:00
{
return 1;
}
2019-11-06 17:24:51 +03:00
2013-02-20 00:06:42 +04:00
return 0;
}
int mf_mlion_get_dirty_region(RFX_RECT* invalid)
{
2013-02-20 00:06:42 +04:00
dispatch_semaphore_wait(region_sem, DISPATCH_TIME_FOREVER);
2019-11-06 17:24:51 +03:00
2013-02-20 00:06:42 +04:00
if (lastUpdate != NULL)
{
mf_mlion_peek_dirty_region(invalid);
}
2019-11-06 17:24:51 +03:00
2013-02-20 00:06:42 +04:00
dispatch_semaphore_signal(region_sem);
2019-11-06 17:24:51 +03:00
2013-02-20 00:06:42 +04:00
return 0;
}
int mf_mlion_peek_dirty_region(RFX_RECT* invalid)
{
size_t num_rects, i;
2013-02-20 00:06:42 +04:00
CGRect dirtyRegion;
2019-11-06 17:24:51 +03:00
const CGRect* rects =
CGDisplayStreamUpdateGetRects(lastUpdate, kCGDisplayStreamUpdateDirtyRects, &num_rects);
if (num_rects == 0)
{
2013-02-20 00:06:42 +04:00
return 0;
}
2019-11-06 17:24:51 +03:00
2013-02-20 00:06:42 +04:00
dirtyRegion = *rects;
for (i = 0; i < num_rects; i++)
2013-02-20 00:06:42 +04:00
{
2019-11-06 17:24:51 +03:00
dirtyRegion = CGRectUnion(dirtyRegion, *(rects + i));
2013-02-20 00:06:42 +04:00
}
2019-11-06 17:24:51 +03:00
2013-02-20 00:06:42 +04:00
invalid->x = dirtyRegion.origin.x;
invalid->y = dirtyRegion.origin.y;
invalid->height = dirtyRegion.size.height;
invalid->width = dirtyRegion.size.width;
2019-11-06 17:24:51 +03:00
2013-02-20 00:06:42 +04:00
return 0;
}
int mf_mlion_clear_dirty_region()
{
2013-02-20 00:06:42 +04:00
dispatch_semaphore_wait(region_sem, DISPATCH_TIME_FOREVER);
2019-11-06 17:24:51 +03:00
2013-02-20 00:06:42 +04:00
CFRelease(lastUpdate);
lastUpdate = NULL;
2019-11-06 17:24:51 +03:00
2013-02-20 00:06:42 +04:00
dispatch_semaphore_signal(region_sem);
2019-11-06 17:24:51 +03:00
2013-02-20 00:06:42 +04:00
return 0;
}
int mf_mlion_get_pixelData(long x, long y, long width, long height, BYTE** pxData)
{
2013-02-20 00:06:42 +04:00
dispatch_semaphore_wait(region_sem, DISPATCH_TIME_FOREVER);
ready = TRUE;
dispatch_semaphore_wait(data_sem, DISPATCH_TIME_FOREVER);
dispatch_semaphore_signal(region_sem);
2019-11-06 17:24:51 +03:00
// this second wait allows us to block until data is copied... more on this later
2013-02-20 00:06:42 +04:00
dispatch_semaphore_wait(data_sem, DISPATCH_TIME_FOREVER);
*pxData = localBuf;
dispatch_semaphore_signal(data_sem);
2019-11-06 17:24:51 +03:00
2013-02-20 00:06:42 +04:00
return 0;
}