28 #include <winpr/sysinfo.h>
29 #include <winpr/cast.h>
31 #include <freerdp/client/cmdline.h>
32 #include <freerdp/channels/channels.h>
33 #include <freerdp/gdi/gdi.h>
34 #include <freerdp/client.h>
35 #include <freerdp/utils/signal.h>
36 #include <freerdp/locale/keyboard.h>
38 #include <linux/input.h>
40 #include <uwac/uwac.h>
42 #include "wlfreerdp.h"
43 #include "wlf_input.h"
44 #include "wlf_cliprdr.h"
46 #include "wlf_channels.h"
47 #include "wlf_pointer.h"
49 #define TAG CLIENT_TAG("wayland")
51 static BOOL wl_update_buffer(
wlfContext* context_w, INT32 ix, INT32 iy, INT32 iw, INT32 ih)
56 UwacSize geometry = { 0 };
58 UwacReturnCode rc = UWAC_ERROR_INTERNAL;
64 if ((ix < 0) || (iy < 0) || (iw < 0) || (ih < 0))
67 EnterCriticalSection(&context_w->critical);
68 UINT32 x = WINPR_ASSERTING_INT_CAST(UINT16, ix);
69 UINT32 y = WINPR_ASSERTING_INT_CAST(UINT16, iy);
70 UINT32 w = WINPR_ASSERTING_INT_CAST(UINT16, iw);
71 UINT32 h = WINPR_ASSERTING_INT_CAST(UINT16, ih);
72 rc = UwacWindowGetDrawingBufferGeometry(context_w->window, &geometry, &stride);
73 data = UwacWindowGetDrawingBuffer(context_w->window);
75 if (!data || (rc != UWAC_SUCCESS))
78 gdi = context_w->common.context.gdi;
84 if (((INT64)x > geometry.width) || ((INT64)y > geometry.height))
90 area.left = WINPR_ASSERTING_INT_CAST(UINT16, x);
91 area.top = WINPR_ASSERTING_INT_CAST(UINT16, y);
92 area.right = WINPR_ASSERTING_INT_CAST(UINT16, x + w);
93 area.bottom = WINPR_ASSERTING_INT_CAST(UINT16, y + h);
96 gdi->primary_buffer, gdi->stride, WINPR_ASSERTING_INT_CAST(
size_t, gdi->width),
97 WINPR_ASSERTING_INT_CAST(
size_t, gdi->height), data, stride,
98 WINPR_ASSERTING_INT_CAST(
size_t, geometry.width),
99 WINPR_ASSERTING_INT_CAST(
size_t, geometry.height), &area,
103 if (!wlf_scale_coordinates(&context_w->common.context, &x, &y, FALSE))
106 if (!wlf_scale_coordinates(&context_w->common.context, &w, &h, FALSE))
109 if (UwacWindowAddDamage(context_w->window, x, y, w, h) != UWAC_SUCCESS)
112 if (UwacWindowSubmitBuffer(context_w->window,
false) != UWAC_SUCCESS)
117 LeaveCriticalSection(&context_w->critical);
121 static BOOL wl_end_paint(rdpContext* context)
130 if (!context || !context->gdi || !context->gdi->primary)
135 if (gdi->primary->hdc->hwnd->invalid->null)
138 x = gdi->primary->hdc->hwnd->invalid->x;
139 y = gdi->primary->hdc->hwnd->invalid->y;
140 w = gdi->primary->hdc->hwnd->invalid->w;
141 h = gdi->primary->hdc->hwnd->invalid->h;
143 if (!wl_update_buffer(context_w, x, y, w, h))
148 gdi->primary->hdc->hwnd->invalid->null = TRUE;
149 gdi->primary->hdc->hwnd->ninvalid = 0;
153 static BOOL wl_refresh_display(
wlfContext* context)
157 if (!context || !context->common.context.gdi)
160 gdi = context->common.context.gdi;
161 return wl_update_buffer(context, 0, 0, gdi->width, gdi->height);
164 static BOOL wl_resize_display(rdpContext* context)
167 rdpGdi* gdi = context->gdi;
168 rdpSettings* settings = context->settings;
174 return wl_refresh_display(wlc);
177 static BOOL wl_pre_connect(freerdp* instance)
179 rdpSettings* settings = NULL;
181 const UwacOutput* output = NULL;
188 WINPR_ASSERT(context);
190 settings = instance->context->settings;
191 WINPR_ASSERT(settings);
200 PubSub_SubscribeChannelConnected(instance->context->pubSub, wlf_OnChannelConnectedEventHandler);
201 PubSub_SubscribeChannelDisconnected(instance->context->pubSub,
202 wlf_OnChannelDisconnectedEventHandler);
207 output = UwacDisplayGetOutput(context->display, 0);
209 if ((output != NULL) && (UwacOutputGetResolution(output, &resolution) == UWAC_SUCCESS))
212 (UINT32)resolution.width))
215 (UINT32)resolution.height))
220 WLog_WARN(TAG,
"Failed to get output resolution! Check your display settings");
227 static BOOL wl_post_connect(freerdp* instance)
229 if (!instance || !instance->context)
233 WINPR_ASSERT(context);
235 rdpSettings* settings = instance->context->settings;
236 WINPR_ASSERT(settings);
238 const char* title =
"FreeRDP";
243 const char* app_id =
"wlfreerdp";
248 if (!gdi_init(instance, PIXEL_FORMAT_BGRA32))
251 rdpGdi* gdi = instance->context->gdi;
253 if (!gdi || (gdi->width < 0) || (gdi->height < 0))
256 if (!wlf_register_pointer(instance->context->graphics))
259 UINT32 w = (UINT32)gdi->width;
260 UINT32 h = (UINT32)gdi->height;
273 context->window = UwacCreateWindowShm(context->display, w, h, WL_SHM_FORMAT_XRGB8888);
275 if (!context->window)
278 UwacWindowSetFullscreenState(
279 context->window, NULL,
281 UwacWindowSetTitle(context->window, title);
282 UwacWindowSetAppId(context->window, app_id);
283 UwacWindowSetOpaqueRegion(context->window, 0, 0, w, h);
284 instance->context->update->EndPaint = wl_end_paint;
285 instance->context->update->DesktopResize = wl_resize_display;
286 const char* KeyboardRemappingList =
289 context->remap_table = freerdp_keyboard_remap_string_to_list(KeyboardRemappingList);
290 if (!context->remap_table)
293 if (!(context->disp = wlf_disp_new(context)))
296 context->clipboard = wlf_clipboard_new(context);
298 if (!context->clipboard)
301 return wl_refresh_display(context);
304 static void wl_post_disconnect(freerdp* instance)
309 if (!instance->context)
314 wlf_clipboard_free(context->clipboard);
315 wlf_disp_free(context->disp);
318 UwacDestroyWindow(&context->window);
319 freerdp_keyboard_remap_free(context->remap_table);
320 context->remap_table = NULL;
323 static BOOL handle_uwac_events(freerdp* instance, UwacDisplay* display)
328 if (UwacDisplayDispatch(display, 1) < 0)
333 while (UwacHasEvent(display))
335 if (UwacNextEvent(display, &event) != UWAC_SUCCESS)
341 case UWAC_EVENT_NEW_SEAT:
342 context->seat =
event.seat_new.seat;
345 case UWAC_EVENT_REMOVED_SEAT:
346 context->seat = NULL;
349 case UWAC_EVENT_FRAME_DONE:
351 EnterCriticalSection(&context->critical);
352 UwacReturnCode r = UwacWindowSubmitBuffer(context->window,
false);
353 LeaveCriticalSection(&context->critical);
354 if (r != UWAC_SUCCESS)
359 case UWAC_EVENT_POINTER_ENTER:
360 if (!wlf_handle_pointer_enter(instance, &event.mouse_enter_leave))
365 case UWAC_EVENT_POINTER_MOTION:
366 if (!wlf_handle_pointer_motion(instance, &event.mouse_motion))
371 case UWAC_EVENT_POINTER_BUTTONS:
372 if (!wlf_handle_pointer_buttons(instance, &event.mouse_button))
377 case UWAC_EVENT_POINTER_AXIS:
378 if (!wlf_handle_pointer_axis(instance, &event.mouse_axis))
382 case UWAC_EVENT_POINTER_AXIS_DISCRETE:
383 if (!wlf_handle_pointer_axis_discrete(instance, &event.mouse_axis))
387 case UWAC_EVENT_POINTER_FRAME:
388 if (!wlf_handle_pointer_frame(instance, &event.mouse_frame))
391 case UWAC_EVENT_POINTER_SOURCE:
392 if (!wlf_handle_pointer_source(instance, &event.mouse_source))
397 if (!wlf_handle_key(instance, &event.key))
402 case UWAC_EVENT_TOUCH_UP:
403 if (!wlf_handle_touch_up(instance, &event.touchUp))
408 case UWAC_EVENT_TOUCH_DOWN:
409 if (!wlf_handle_touch_down(instance, &event.touchDown))
414 case UWAC_EVENT_TOUCH_MOTION:
415 if (!wlf_handle_touch_motion(instance, &event.touchMotion))
420 case UWAC_EVENT_KEYBOARD_ENTER:
422 UwacSeatInhibitShortcuts(event.keyboard_enter_leave.seat,
true);
424 if (!wlf_keyboard_enter(instance, &event.keyboard_enter_leave))
429 case UWAC_EVENT_KEYBOARD_MODIFIERS:
430 if (!wlf_keyboard_modifiers(instance, &event.keyboard_modifiers))
435 case UWAC_EVENT_CONFIGURE:
436 if (!wlf_disp_handle_configure(context->disp, event.configure.width,
437 event.configure.height))
440 if (!wl_refresh_display(context))
445 case UWAC_EVENT_CLIPBOARD_AVAILABLE:
446 case UWAC_EVENT_CLIPBOARD_OFFER:
447 case UWAC_EVENT_CLIPBOARD_SELECT:
448 if (!wlf_cliprdr_handle_event(context->clipboard, &event.clipboard))
453 case UWAC_EVENT_CLOSE:
454 context->closed = TRUE;
466 static BOOL handle_window_events(freerdp* instance)
474 static int wlfreerdp_run(freerdp* instance)
477 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = { 0 };
478 DWORD status = WAIT_ABANDONED;
482 TimerEventArgs timerEvent;
483 EventArgsInit(&timerEvent,
"xfreerdp");
493 if (!freerdp_connect(instance))
495 WLog_Print(context->log, WLOG_ERROR,
"Failed to connect");
499 timer = CreateWaitableTimerA(NULL, FALSE,
"mainloop-periodic-timer");
503 WLog_ERR(TAG,
"failed to create timer");
509 if (!SetWaitableTimer(timer, &due, 20, NULL, NULL, FALSE))
514 while (!freerdp_shall_disconnect_context(instance->context))
517 handles[count++] = timer;
518 handles[count++] = context->displayHandle;
519 count += freerdp_get_event_handles(instance->context, &handles[count],
520 ARRAYSIZE(handles) - count);
524 WLog_Print(context->log, WLOG_ERROR,
"Failed to get FreeRDP file descriptor");
528 status = WaitForMultipleObjects(count, handles, FALSE, INFINITE);
530 if (WAIT_FAILED == status)
532 WLog_Print(context->log, WLOG_ERROR,
"WaitForMultipleObjects failed");
536 if (!handle_uwac_events(instance, context->display))
538 WLog_Print(context->log, WLOG_ERROR,
"error handling UWAC events");
544 WLog_Print(context->log, WLOG_INFO,
"Closed from Wayland");
548 if (freerdp_check_event_handles(instance->context) != TRUE)
550 if (client_auto_reconnect_ex(instance, handle_window_events))
558 if (freerdp_error_info(instance) == 0)
562 if (freerdp_get_last_error(instance->context) == FREERDP_ERROR_SUCCESS)
563 WLog_Print(context->log, WLOG_ERROR,
"Failed to check FreeRDP file descriptor");
568 if ((status != WAIT_TIMEOUT) && (status == WAIT_OBJECT_0))
570 timerEvent.now = GetTickCount64();
571 PubSub_OnTimer(context->common.context.pubSub, context, &timerEvent);
577 (void)CloseHandle(timer);
578 freerdp_disconnect(instance);
579 return WINPR_ASSERTING_INT_CAST(
int, status);
582 static BOOL wlf_client_global_init(
void)
584 (void)setlocale(LC_ALL,
"");
586 if (freerdp_handle_signals() != 0)
592 static void wlf_client_global_uninit(
void)
596 static int wlf_logon_error_info(freerdp* instance, UINT32 data, UINT32 type)
599 const char* str_data = freerdp_get_logon_error_info_data(data);
600 const char* str_type = freerdp_get_logon_error_info_type(type);
602 if (!instance || !instance->context)
606 WLog_Print(wlf->log, WLOG_INFO,
"Logon Error Info %s [%s]", str_data, str_type);
610 static void wlf_client_free(freerdp* instance, rdpContext* context)
618 UwacCloseDisplay(&wlf->display);
620 if (wlf->displayHandle)
621 (void)CloseHandle(wlf->displayHandle);
622 ArrayList_Free(wlf->events);
623 DeleteCriticalSection(&wlf->critical);
626 static void* uwac_event_clone(
const void* val)
628 UwacEvent* copy = NULL;
629 const UwacEvent* ev = (
const UwacEvent*)val;
631 copy = calloc(1,
sizeof(UwacEvent));
638 static BOOL wlf_client_new(freerdp* instance, rdpContext* context)
641 UwacReturnCode status = UWAC_ERROR_INTERNAL;
644 if (!instance || !context)
647 instance->PreConnect = wl_pre_connect;
648 instance->PostConnect = wl_post_connect;
649 instance->PostDisconnect = wl_post_disconnect;
650 instance->LogonErrorInfo = wlf_logon_error_info;
651 wfl->log = WLog_Get(TAG);
652 wfl->display = UwacOpenDisplay(NULL, &status);
654 if (!wfl->display || (status != UWAC_SUCCESS) || !wfl->log)
657 wfl->displayHandle = CreateFileDescriptorEvent(NULL, FALSE, FALSE,
658 UwacDisplayGetFd(wfl->display), WINPR_FD_READ);
660 if (!wfl->displayHandle)
663 wfl->events = ArrayList_New(FALSE);
667 obj = ArrayList_Object(wfl->events);
668 obj->fnObjectNew = uwac_event_clone;
669 obj->fnObjectFree = free;
671 InitializeCriticalSection(&wfl->critical);
676 static int wfl_client_start(rdpContext* context)
678 WINPR_UNUSED(context);
682 static int RdpClientEntry(RDP_CLIENT_ENTRY_POINTS* pEntryPoints)
684 WINPR_ASSERT(pEntryPoints);
685 ZeroMemory(pEntryPoints,
sizeof(RDP_CLIENT_ENTRY_POINTS));
686 pEntryPoints->Version = RDP_CLIENT_INTERFACE_VERSION;
687 pEntryPoints->Size =
sizeof(RDP_CLIENT_ENTRY_POINTS_V1);
688 pEntryPoints->GlobalInit = wlf_client_global_init;
689 pEntryPoints->GlobalUninit = wlf_client_global_uninit;
690 pEntryPoints->ContextSize =
sizeof(
wlfContext);
691 pEntryPoints->ClientNew = wlf_client_new;
692 pEntryPoints->ClientFree = wlf_client_free;
693 pEntryPoints->ClientStart = wfl_client_start;
694 pEntryPoints->ClientStop = freerdp_client_common_stop;
698 int main(
int argc,
char* argv[])
702 RDP_CLIENT_ENTRY_POINTS clientEntryPoints;
703 rdpContext* context = NULL;
704 rdpSettings* settings = NULL;
707 freerdp_client_warn_deprecated(argc, argv);
709 RdpClientEntry(&clientEntryPoints);
710 context = freerdp_client_context_new(&clientEntryPoints);
714 settings = context->settings;
716 status = freerdp_client_settings_parse_command_line(settings, argc, argv, FALSE);
719 rc = freerdp_client_settings_command_line_status_print(settings, status, argc, argv);
722 wlf_list_monitors(wlc);
727 if (freerdp_client_start(context) != 0)
730 rc = wlfreerdp_run(context->instance);
732 if (freerdp_client_stop(context) != 0)
736 freerdp_client_context_free(context);
740 BOOL wlf_copy_image(
const void* src,
size_t srcStride,
size_t srcWidth,
size_t srcHeight,
void* dst,
741 size_t dstStride,
size_t dstWidth,
size_t dstHeight,
const RECTANGLE_16* area,
746 if (!src || !dst || !area)
751 WINPR_ASSERT(dstStride <= UINT32_MAX);
752 WINPR_ASSERT(dstWidth <= UINT32_MAX);
753 WINPR_ASSERT(dstHeight <= UINT32_MAX);
754 WINPR_ASSERT(srcStride <= UINT32_MAX);
755 WINPR_ASSERT(srcWidth <= UINT32_MAX);
756 WINPR_ASSERT(srcHeight <= UINT32_MAX);
757 return freerdp_image_scale(dst, PIXEL_FORMAT_BGRA32, (UINT32)dstStride, 0, 0,
758 (UINT32)dstWidth, (UINT32)dstHeight, src, PIXEL_FORMAT_BGRA32,
759 (UINT32)srcStride, 0, 0, (UINT32)srcWidth, (UINT32)srcHeight);
763 const size_t baseSrcOffset = 1ULL * area->top * srcStride + 4ULL * area->left;
764 const size_t baseDstOffset = 1ULL * area->top * dstStride + 4ULL * area->left;
765 const size_t width = MIN((
size_t)area->right - area->left, dstWidth - area->left);
766 const size_t height = MIN((
size_t)area->bottom - area->top, dstHeight - area->top);
767 const BYTE* psrc = (
const BYTE*)src;
768 BYTE* pdst = (BYTE*)dst;
770 for (
size_t i = 0; i < height; i++)
772 const size_t srcOffset = i * srcStride + baseSrcOffset;
773 const size_t dstOffset = i * dstStride + baseDstOffset;
774 memcpy(&pdst[dstOffset], &psrc[srcOffset], width * 4);
783 BOOL wlf_scale_coordinates(rdpContext* context, UINT32* px, UINT32* py, BOOL fromLocalToRDP)
786 UwacSize geometry = { 0 };
788 if (!context || !px || !py || !context->gdi)
794 rdpGdi* gdi = context->gdi;
796 if (UwacWindowGetDrawingBufferGeometry(wlf->window, &geometry, NULL) != UWAC_SUCCESS)
799 const double sx = 1.0 * geometry.width / (double)gdi->width;
800 const double sy = 1.0 * geometry.height / (
double)gdi->height;
804 *px *= (UINT32)lround(sx);
805 *py *= (UINT32)lround(sy);
809 *px /= (UINT32)lround(sx);
810 *py /= (UINT32)lround(sy);
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_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.
FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.
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.
This struct contains function pointer to initialize/free objects.