20#include <freerdp/config.h>
22#include <freerdp/listener.h>
23#include <freerdp/codec/rfx.h>
24#include <winpr/stream.h>
25#include <freerdp/peer.h>
26#include <freerdp/codec/color.h>
29#include <winpr/assert.h>
38#include <mach/clock.h>
40#include <dispatch/dispatch.h>
42#include "OpenGL/OpenGL.h"
45#include "CoreVideo/CoreVideo.h"
47#include <freerdp/log.h>
48#define TAG SERVER_TAG("mac")
51static int info_last_sec = 0;
52static int info_last_nsec = 0;
54static dispatch_source_t info_timer;
55static dispatch_queue_t info_queue;
57static mfEventQueue* info_event_queue;
59static CGLContextObj glContext;
60static CGContextRef bmp;
63static void mf_peer_context_free(freerdp_peer* client, rdpContext* context);
66static BOOL mf_peer_get_fds(freerdp_peer* client,
void** rfds,
int* rcount)
68 if (info_event_queue->pipe_fd[0] == -1)
71 rfds[*rcount] = (
void*)(
long)info_event_queue->pipe_fd[0];
76static void mf_peer_rfx_update(freerdp_peer* client)
79 mfInfo* mfi = mf_info_get_instance();
80 mf_info_find_invalid_region(mfi);
82 if (mf_info_have_invalid_region(mfi) ==
false)
90 BYTE* dataBits =
nullptr;
91 mf_info_getScreenData(mfi, &width, &height, &dataBits, &pitch);
92 mf_info_clear_invalid_region(mfi);
100 WINPR_ASSERT(client);
102 mfp = (mfPeerContext*)client->context;
105 update = client->context->update;
106 WINPR_ASSERT(update);
112 Stream_SetPosition(s, 0);
113 UINT32 x = mfi->invalid.x / mfi->scale;
114 UINT32 y = mfi->invalid.y / mfi->scale;
118 rect.height = height;
120 rfx_context_reset(mfp->rfx_context, mfi->servscreen_width, mfi->servscreen_height);
122 if (!(rfx_compose_message(mfp->rfx_context, s, &rect, 1, (BYTE*)dataBits, rect.width,
123 rect.height, pitch)))
130 cmd.destRight = x + rect.width;
131 cmd.destBottom = y + rect.height;
134 cmd.bmp.width = rect.width;
135 cmd.bmp.height = rect.height;
136 cmd.bmp.bitmapDataLength = Stream_GetPosition(s);
137 cmd.bmp.bitmapData = Stream_Buffer(s);
139 update->SurfaceBits(update->context, &cmd);
144static BOOL mf_peer_check_fds(freerdp_peer* client)
146 mfPeerContext* context = (mfPeerContext*)client->context;
149 if (context->activated == FALSE)
152 event = mf_event_peek(info_event_queue);
154 if (event !=
nullptr)
156 if (event->type == FREERDP_SERVER_MAC_EVENT_TYPE_REGION)
159 else if (event->type == FREERDP_SERVER_MAC_EVENT_TYPE_FRAME_TICK)
161 event = mf_event_pop(info_event_queue);
162 mf_peer_rfx_update(client);
163 mf_event_free(event);
172static BOOL mf_peer_context_new(freerdp_peer* client, rdpContext* context)
174 rdpSettings* settings;
175 mfPeerContext* peer = (mfPeerContext*)context;
177 WINPR_ASSERT(client);
178 WINPR_ASSERT(context);
180 settings = context->settings;
181 WINPR_ASSERT(settings);
183 if (!(peer->info = mf_info_get_instance()))
186 if (!(peer->rfx_context = rfx_context_new_ex(
190 rfx_context_reset(peer->rfx_context,
193 rfx_context_set_mode(peer->rfx_context, RLGR3);
194 rfx_context_set_pixel_format(peer->rfx_context, PIXEL_FORMAT_BGRA32);
196 if (!(peer->s = Stream_New(
nullptr, 0xFFFF)))
199 peer->vcm = WTSOpenServerA((LPSTR)client->context);
201 if (!peer->vcm || (peer->vcm == INVALID_HANDLE_VALUE))
204 mf_info_peer_register(peer->info, peer);
207 mf_peer_context_free(client, context);
212static void mf_peer_context_free(freerdp_peer* client, rdpContext* context)
214 mfPeerContext* peer = (mfPeerContext*)context;
217 mf_info_peer_unregister(peer->info, peer);
218 dispatch_suspend(info_timer);
219 Stream_Free(peer->s, TRUE);
220 rfx_context_free(peer->rfx_context);
222#ifdef CHANNEL_AUDIN_SERVER
224 mf_peer_audin_uninit(peer);
227#ifdef CHANNEL_RDPSND_SERVER
228 mf_peer_rdpsnd_stop();
231 rdpsnd_server_context_free(peer->rdpsnd);
234 WTSCloseServer(peer->vcm);
240static BOOL mf_peer_init(freerdp_peer* client)
242 client->ContextSize =
sizeof(mfPeerContext);
243 client->ContextNew = mf_peer_context_new;
244 client->ContextFree = mf_peer_context_free;
246 if (!freerdp_peer_context_new(client))
249 info_event_queue = mf_event_queue_new();
250 info_queue = dispatch_queue_create(
"FreeRDP.update.timer", DISPATCH_QUEUE_SERIAL);
251 info_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, info_queue);
256 dispatch_source_set_timer(info_timer, DISPATCH_TIME_NOW, 42ull * NSEC_PER_MSEC,
257 100ull * NSEC_PER_MSEC);
258 dispatch_source_set_event_handler(info_timer, ^{
260 mfEvent*
event = mf_event_new(FREERDP_SERVER_MAC_EVENT_TYPE_FRAME_TICK);
261 mf_event_push(info_event_queue, (mfEvent*)event);
263 dispatch_resume(info_timer);
270static BOOL mf_peer_post_connect(freerdp_peer* client)
272 mfInfo* mfi = mf_info_get_instance();
274 WINPR_ASSERT(client);
276 mfPeerContext* context = (mfPeerContext*)client->context;
277 WINPR_ASSERT(context);
279 rdpSettings* settings = client->context->settings;
280 WINPR_ASSERT(settings);
285 UINT32 bitsPerPixel = 32;
299 WINPR_ASSERT(client->context->update);
300 WINPR_ASSERT(client->context->update->DesktopResize);
301 client->context->update->DesktopResize(client->context);
303 mfi->mouse_down_left = FALSE;
304 mfi->mouse_down_right = FALSE;
305 mfi->mouse_down_other = FALSE;
306#ifdef CHANNEL_RDPSND_SERVER
308 if (WTSVirtualChannelManagerIsChannelJoined(context->vcm,
"rdpsnd"))
310 mf_peer_rdpsnd_init(context);
315#ifdef CHANNEL_AUDIN_SERVER
316 mf_peer_audin_init(context);
322static BOOL mf_peer_activate(freerdp_peer* client)
324 WINPR_ASSERT(client);
326 mfPeerContext* context = (mfPeerContext*)client->context;
327 WINPR_ASSERT(context);
329 rdpSettings* settings = client->context->settings;
330 WINPR_ASSERT(settings);
332 rfx_context_reset(context->rfx_context,
335 context->activated = TRUE;
340static BOOL mf_peer_synchronize_event(rdpInput* input, UINT32 flags)
346static BOOL mf_peer_keyboard_event(rdpInput* input, UINT16 flags, UINT8 code)
348 bool state_down = FALSE;
350 if (flags == KBD_FLAGS_DOWN)
358static BOOL mf_peer_unicode_keyboard_event(rdpInput* input, UINT16 flags, UINT16 code)
364static BOOL mf_peer_suppress_output(rdpContext* context, BYTE allow,
const RECTANGLE_16* area)
370static void* mf_peer_main_loop(
void* arg)
372 mfPeerContext* context;
373 rdpSettings* settings;
376 freerdp_peer* client = (freerdp_peer*)arg;
378 if (!mf_peer_init(client))
384 WINPR_ASSERT(client->context);
386 settings = client->context->settings;
387 WINPR_ASSERT(settings);
390 rdpPrivateKey* key = freerdp_key_new_from_file_enc(info->key,
nullptr);
395 rdpCertificate* cert = freerdp_certificate_new_from_file(info->cert);
413 client->PostConnect = mf_peer_post_connect;
414 client->Activate = mf_peer_activate;
416 input = client->context->input;
419 input->SynchronizeEvent = mf_peer_synchronize_event;
420 input->KeyboardEvent = mf_input_keyboard_event;
421 input->UnicodeKeyboardEvent = mf_peer_unicode_keyboard_event;
422 input->MouseEvent = mf_input_mouse_event;
423 input->ExtendedMouseEvent = mf_input_extended_mouse_event;
425 update = client->context->update;
426 WINPR_ASSERT(update);
429 update->SuppressOutput = mf_peer_suppress_output;
431 WINPR_ASSERT(client->Initialize);
432 const BOOL rc = client->Initialize(client);
435 context = (mfPeerContext*)client->context;
440 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = WINPR_C_ARRAY_INIT;
441 DWORD count = client->GetEventHandles(client, handles, ARRAYSIZE(handles));
443 if ((count == 0) || (count == MAXIMUM_WAIT_OBJECTS))
445 WLog_ERR(TAG,
"Failed to get FreeRDP file descriptor");
449 handles[count++] = WTSVirtualChannelManagerGetEventHandle(context->vcm);
451 status = WaitForMultipleObjects(count, handles, FALSE, INFINITE);
452 if (status == WAIT_FAILED)
454 WLog_ERR(TAG,
"WaitForMultipleObjects failed");
458 if (client->CheckFileDescriptor(client) != TRUE)
463 if ((mf_peer_check_fds(client)) != TRUE)
468 if (WTSVirtualChannelManagerCheckFileDescriptor(context->vcm) != TRUE)
474 client->Disconnect(client);
475 freerdp_peer_context_free(client);
477 freerdp_peer_free(client);
481BOOL mf_peer_accepted(freerdp_listener* instance, freerdp_peer* client)
485 WINPR_ASSERT(instance);
486 WINPR_ASSERT(client);
488 client->ContextExtra = instance->info;
489 if (pthread_create(&th, 0, mf_peer_main_loop, client) == 0)
FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 val)
Sets a UINT32 settings value.
FREERDP_API BOOL freerdp_settings_set_bool(rdpSettings *settings, FreeRDP_Settings_Keys_Bool id, BOOL val)
Sets a BOOL settings value.
FREERDP_API BOOL freerdp_settings_set_pointer_len(rdpSettings *settings, FreeRDP_Settings_Keys_Pointer id, const void *data, size_t len)
Set a pointer to value data.
WINPR_ATTR_NODISCARD FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.