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>
28 #include <winpr/crt.h>
29 #include <winpr/assert.h>
35 #include "mf_rdpsnd.h"
38 #include <mach/clock.h>
39 #include <mach/mach.h>
40 #include <dispatch/dispatch.h>
42 #include "OpenGL/OpenGL.h"
43 #include "OpenGL/gl.h"
45 #include "CoreVideo/CoreVideo.h"
47 #include <freerdp/log.h>
48 #define TAG SERVER_TAG("mac")
51 static int info_last_sec = 0;
52 static int info_last_nsec = 0;
54 static dispatch_source_t info_timer;
55 static dispatch_queue_t info_queue;
57 static mfEventQueue* info_event_queue;
59 static CGLContextObj glContext;
60 static CGContextRef bmp;
61 static CGImageRef img;
63 static void mf_peer_context_free(freerdp_peer* client, rdpContext* context);
65 static BOOL mf_peer_get_fds(freerdp_peer* client,
void** rfds,
int* rcount)
67 if (info_event_queue->pipe_fd[0] == -1)
70 rfds[*rcount] = (
void*)(
long)info_event_queue->pipe_fd[0];
75 static void mf_peer_rfx_update(freerdp_peer* client)
78 mfInfo* mfi = mf_info_get_instance();
79 mf_info_find_invalid_region(mfi);
81 if (mf_info_have_invalid_region(mfi) ==
false)
89 BYTE* dataBits = NULL;
90 mf_info_getScreenData(mfi, &width, &height, &dataBits, &pitch);
91 mf_info_clear_invalid_region(mfi);
101 mfp = (mfPeerContext*)client->context;
104 update = client->context->update;
105 WINPR_ASSERT(update);
111 Stream_SetPosition(s, 0);
112 UINT32 x = mfi->invalid.x / mfi->scale;
113 UINT32 y = mfi->invalid.y / mfi->scale;
117 rect.height = height;
119 rfx_context_reset(mfp->rfx_context, mfi->servscreen_width, mfi->servscreen_height);
121 if (!(rfx_compose_message(mfp->rfx_context, s, &rect, 1, (BYTE*)dataBits, rect.width,
122 rect.height, pitch)))
129 cmd.destRight = x + rect.width;
130 cmd.destBottom = y + rect.height;
133 cmd.bmp.width = rect.width;
134 cmd.bmp.height = rect.height;
135 cmd.bmp.bitmapDataLength = Stream_GetPosition(s);
136 cmd.bmp.bitmapData = Stream_Buffer(s);
138 update->SurfaceBits(update->context, &cmd);
142 static BOOL mf_peer_check_fds(freerdp_peer* client)
144 mfPeerContext* context = (mfPeerContext*)client->context;
147 if (context->activated == FALSE)
150 event = mf_event_peek(info_event_queue);
154 if (event->type == FREERDP_SERVER_MAC_EVENT_TYPE_REGION)
157 else if (event->type == FREERDP_SERVER_MAC_EVENT_TYPE_FRAME_TICK)
159 event = mf_event_pop(info_event_queue);
160 mf_peer_rfx_update(client);
161 mf_event_free(event);
169 static BOOL mf_peer_context_new(freerdp_peer* client, rdpContext* context)
171 rdpSettings* settings;
172 mfPeerContext* peer = (mfPeerContext*)context;
174 WINPR_ASSERT(client);
175 WINPR_ASSERT(context);
177 settings = context->settings;
178 WINPR_ASSERT(settings);
180 if (!(peer->info = mf_info_get_instance()))
183 if (!(peer->rfx_context = rfx_context_new_ex(
187 rfx_context_reset(peer->rfx_context,
190 rfx_context_set_mode(peer->rfx_context, RLGR3);
191 rfx_context_set_pixel_format(peer->rfx_context, PIXEL_FORMAT_BGRA32);
193 if (!(peer->s = Stream_New(NULL, 0xFFFF)))
196 peer->vcm = WTSOpenServerA((LPSTR)client->context);
198 if (!peer->vcm || (peer->vcm == INVALID_HANDLE_VALUE))
201 mf_info_peer_register(peer->info, peer);
204 mf_peer_context_free(client, context);
209 static void mf_peer_context_free(freerdp_peer* client, rdpContext* context)
211 mfPeerContext* peer = (mfPeerContext*)context;
214 mf_info_peer_unregister(peer->info, peer);
215 dispatch_suspend(info_timer);
216 Stream_Free(peer->s, TRUE);
217 rfx_context_free(peer->rfx_context);
219 #ifdef CHANNEL_AUDIN_SERVER
221 mf_peer_audin_uninit(peer);
224 #ifdef CHANNEL_RDPSND_SERVER
225 mf_peer_rdpsnd_stop();
228 rdpsnd_server_context_free(peer->rdpsnd);
231 WTSCloseServer(peer->vcm);
236 static BOOL mf_peer_init(freerdp_peer* client)
238 client->ContextSize =
sizeof(mfPeerContext);
239 client->ContextNew = mf_peer_context_new;
240 client->ContextFree = mf_peer_context_free;
242 if (!freerdp_peer_context_new(client))
245 info_event_queue = mf_event_queue_new();
246 info_queue = dispatch_queue_create(
"FreeRDP.update.timer", DISPATCH_QUEUE_SERIAL);
247 info_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, info_queue);
252 dispatch_source_set_timer(info_timer, DISPATCH_TIME_NOW, 42ull * NSEC_PER_MSEC,
253 100ull * NSEC_PER_MSEC);
254 dispatch_source_set_event_handler(info_timer, ^{
256 mfEvent*
event = mf_event_new(FREERDP_SERVER_MAC_EVENT_TYPE_FRAME_TICK);
257 mf_event_push(info_event_queue, (mfEvent*)event);
259 dispatch_resume(info_timer);
265 static BOOL mf_peer_post_connect(freerdp_peer* client)
267 mfInfo* mfi = mf_info_get_instance();
269 WINPR_ASSERT(client);
271 mfPeerContext* context = (mfPeerContext*)client->context;
272 WINPR_ASSERT(context);
274 rdpSettings* settings = client->context->settings;
275 WINPR_ASSERT(settings);
280 UINT32 bitsPerPixel = 32;
294 WINPR_ASSERT(client->context->update);
295 WINPR_ASSERT(client->context->update->DesktopResize);
296 client->context->update->DesktopResize(client->context);
298 mfi->mouse_down_left = FALSE;
299 mfi->mouse_down_right = FALSE;
300 mfi->mouse_down_other = FALSE;
301 #ifdef CHANNEL_RDPSND_SERVER
303 if (WTSVirtualChannelManagerIsChannelJoined(context->vcm,
"rdpsnd"))
305 mf_peer_rdpsnd_init(context);
310 #ifdef CHANNEL_AUDIN_SERVER
311 mf_peer_audin_init(context);
316 static BOOL mf_peer_activate(freerdp_peer* client)
318 WINPR_ASSERT(client);
320 mfPeerContext* context = (mfPeerContext*)client->context;
321 WINPR_ASSERT(context);
323 rdpSettings* settings = client->context->settings;
324 WINPR_ASSERT(settings);
326 rfx_context_reset(context->rfx_context,
329 context->activated = TRUE;
333 static BOOL mf_peer_synchronize_event(rdpInput* input, UINT32 flags)
338 static BOOL mf_peer_keyboard_event(rdpInput* input, UINT16 flags, UINT8 code)
340 bool state_down = FALSE;
342 if (flags == KBD_FLAGS_DOWN)
349 static BOOL mf_peer_unicode_keyboard_event(rdpInput* input, UINT16 flags, UINT16 code)
354 static BOOL mf_peer_suppress_output(rdpContext* context, BYTE allow,
const RECTANGLE_16* area)
359 static void* mf_peer_main_loop(
void* arg)
361 mfPeerContext* context;
362 rdpSettings* settings;
365 freerdp_peer* client = (freerdp_peer*)arg;
367 if (!mf_peer_init(client))
373 WINPR_ASSERT(client->context);
375 settings = client->context->settings;
376 WINPR_ASSERT(settings);
379 rdpPrivateKey* key = freerdp_key_new_from_file(info->key);
384 rdpCertificate* cert = freerdp_certificate_new_from_file(info->cert);
402 client->PostConnect = mf_peer_post_connect;
403 client->Activate = mf_peer_activate;
405 input = client->context->input;
408 input->SynchronizeEvent = mf_peer_synchronize_event;
409 input->KeyboardEvent = mf_input_keyboard_event;
410 input->UnicodeKeyboardEvent = mf_peer_unicode_keyboard_event;
411 input->MouseEvent = mf_input_mouse_event;
412 input->ExtendedMouseEvent = mf_input_extended_mouse_event;
414 update = client->context->update;
415 WINPR_ASSERT(update);
418 update->SuppressOutput = mf_peer_suppress_output;
420 WINPR_ASSERT(client->Initialize);
421 const BOOL rc = client->Initialize(client);
424 context = (mfPeerContext*)client->context;
429 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = { 0 };
430 DWORD count = client->GetEventHandles(client, handles, ARRAYSIZE(handles));
432 if ((count == 0) || (count == MAXIMUM_WAIT_OBJECTS))
434 WLog_ERR(TAG,
"Failed to get FreeRDP file descriptor");
438 handles[count++] = WTSVirtualChannelManagerGetEventHandle(context->vcm);
440 status = WaitForMultipleObjects(count, handles, FALSE, INFINITE);
441 if (status == WAIT_FAILED)
443 WLog_ERR(TAG,
"WaitForMultipleObjects failed");
447 if (client->CheckFileDescriptor(client) != TRUE)
452 if ((mf_peer_check_fds(client)) != TRUE)
457 if (WTSVirtualChannelManagerCheckFileDescriptor(context->vcm) != TRUE)
463 client->Disconnect(client);
464 freerdp_peer_context_free(client);
466 freerdp_peer_free(client);
470 BOOL mf_peer_accepted(freerdp_listener* instance, freerdp_peer* client)
474 WINPR_ASSERT(instance);
475 WINPR_ASSERT(client);
477 client->ContextExtra = instance->info;
478 if (pthread_create(&th, 0, mf_peer_main_loop, client) == 0)
FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 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.
FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 param)
Sets a UINT32 settings value.
FREERDP_API BOOL freerdp_settings_set_bool(rdpSettings *settings, FreeRDP_Settings_Keys_Bool id, BOOL param)
Sets a BOOL settings value.