28 #include <winpr/sysinfo.h>
30 #include <freerdp/client/cmdline.h>
31 #include <freerdp/channels/channels.h>
32 #include <freerdp/gdi/gdi.h>
33 #include <freerdp/client.h>
34 #include <freerdp/utils/signal.h>
35 #include <freerdp/locale/keyboard.h>
37 #include <linux/input.h>
39 #include <uwac/uwac.h>
41 #include "wlfreerdp.h"
42 #include "wlf_input.h"
43 #include "wlf_cliprdr.h"
45 #include "wlf_channels.h"
46 #include "wlf_pointer.h"
48 #define TAG CLIENT_TAG("wayland")
50 static BOOL wl_update_buffer(
wlfContext* context_w, INT32 ix, INT32 iy, INT32 iw, INT32 ih)
61 UwacReturnCode rc = UWAC_ERROR_INTERNAL;
67 if ((ix < 0) || (iy < 0) || (iw < 0) || (ih < 0))
70 EnterCriticalSection(&context_w->critical);
75 rc = UwacWindowGetDrawingBufferGeometry(context_w->window, &geometry, &stride);
76 data = UwacWindowGetDrawingBuffer(context_w->window);
78 if (!data || (rc != UWAC_SUCCESS))
81 gdi = context_w->common.context.gdi;
87 if (((INT64)x > geometry.width) || ((INT64)y > geometry.height))
99 gdi->primary_buffer, gdi->stride, gdi->width, gdi->height, data, stride, geometry.width,
100 geometry.height, &area,
104 if (!wlf_scale_coordinates(&context_w->common.context, &x, &y, FALSE))
107 if (!wlf_scale_coordinates(&context_w->common.context, &w, &h, FALSE))
110 if (UwacWindowAddDamage(context_w->window, x, y, w, h) != UWAC_SUCCESS)
113 if (UwacWindowSubmitBuffer(context_w->window,
false) != UWAC_SUCCESS)
118 LeaveCriticalSection(&context_w->critical);
122 static BOOL wl_end_paint(rdpContext* context)
131 if (!context || !context->gdi || !context->gdi->primary)
136 if (gdi->primary->hdc->hwnd->invalid->null)
139 x = gdi->primary->hdc->hwnd->invalid->x;
140 y = gdi->primary->hdc->hwnd->invalid->y;
141 w = gdi->primary->hdc->hwnd->invalid->w;
142 h = gdi->primary->hdc->hwnd->invalid->h;
144 if (!wl_update_buffer(context_w, x, y, w, h))
149 gdi->primary->hdc->hwnd->invalid->null = TRUE;
150 gdi->primary->hdc->hwnd->ninvalid = 0;
154 static BOOL wl_refresh_display(
wlfContext* context)
158 if (!context || !context->common.context.gdi)
161 gdi = context->common.context.gdi;
162 return wl_update_buffer(context, 0, 0, gdi->width, gdi->height);
165 static BOOL wl_resize_display(rdpContext* context)
168 rdpGdi* gdi = context->gdi;
169 rdpSettings* settings = context->settings;
175 return wl_refresh_display(wlc);
178 static BOOL wl_pre_connect(freerdp* instance)
180 rdpSettings* settings = NULL;
182 const UwacOutput* output = NULL;
189 WINPR_ASSERT(context);
191 settings = instance->context->settings;
192 WINPR_ASSERT(settings);
201 PubSub_SubscribeChannelConnected(instance->context->pubSub, wlf_OnChannelConnectedEventHandler);
202 PubSub_SubscribeChannelDisconnected(instance->context->pubSub,
203 wlf_OnChannelDisconnectedEventHandler);
208 output = UwacDisplayGetOutput(context->display, 0);
210 if ((output != NULL) && (UwacOutputGetResolution(output, &resolution) == UWAC_SUCCESS))
213 (UINT32)resolution.width))
216 (UINT32)resolution.height))
221 WLog_WARN(TAG,
"Failed to get output resolution! Check your display settings");
228 static BOOL wl_post_connect(freerdp* instance)
230 if (!instance || !instance->context)
234 WINPR_ASSERT(context);
236 rdpSettings* settings = instance->context->settings;
237 WINPR_ASSERT(settings);
239 const char* title =
"FreeRDP";
244 const char* app_id =
"wlfreerdp";
249 if (!gdi_init(instance, PIXEL_FORMAT_BGRA32))
252 rdpGdi* gdi = instance->context->gdi;
254 if (!gdi || (gdi->width < 0) || (gdi->height < 0))
257 if (!wlf_register_pointer(instance->context->graphics))
260 UINT32 w = (UINT32)gdi->width;
261 UINT32 h = (UINT32)gdi->height;
274 context->window = UwacCreateWindowShm(context->display, w, h, WL_SHM_FORMAT_XRGB8888);
276 if (!context->window)
279 UwacWindowSetFullscreenState(
280 context->window, NULL,
282 UwacWindowSetTitle(context->window, title);
283 UwacWindowSetAppId(context->window, app_id);
284 UwacWindowSetOpaqueRegion(context->window, 0, 0, w, h);
285 instance->context->update->EndPaint = wl_end_paint;
286 instance->context->update->DesktopResize = wl_resize_display;
287 UINT32 KeyboardLayout =
289 const char* KeyboardRemappingList =
292 freerdp_keyboard_init_ex(KeyboardLayout, KeyboardRemappingList);
294 if (!(context->disp = wlf_disp_new(context)))
297 context->clipboard = wlf_clipboard_new(context);
299 if (!context->clipboard)
302 return wl_refresh_display(context);
305 static void wl_post_disconnect(freerdp* instance)
312 if (!instance->context)
317 wlf_clipboard_free(context->clipboard);
318 wlf_disp_free(context->disp);
321 UwacDestroyWindow(&context->window);
324 static BOOL handle_uwac_events(freerdp* instance, UwacDisplay* display)
329 if (UwacDisplayDispatch(display, 1) < 0)
334 while (UwacHasEvent(display))
336 if (UwacNextEvent(display, &event) != UWAC_SUCCESS)
342 case UWAC_EVENT_NEW_SEAT:
343 context->seat =
event.seat_new.seat;
346 case UWAC_EVENT_REMOVED_SEAT:
347 context->seat = NULL;
350 case UWAC_EVENT_FRAME_DONE:
352 EnterCriticalSection(&context->critical);
353 UwacReturnCode r = UwacWindowSubmitBuffer(context->window,
false);
354 LeaveCriticalSection(&context->critical);
355 if (r != UWAC_SUCCESS)
360 case UWAC_EVENT_POINTER_ENTER:
361 if (!wlf_handle_pointer_enter(instance, &event.mouse_enter_leave))
366 case UWAC_EVENT_POINTER_MOTION:
367 if (!wlf_handle_pointer_motion(instance, &event.mouse_motion))
372 case UWAC_EVENT_POINTER_BUTTONS:
373 if (!wlf_handle_pointer_buttons(instance, &event.mouse_button))
378 case UWAC_EVENT_POINTER_AXIS:
379 if (!wlf_handle_pointer_axis(instance, &event.mouse_axis))
383 case UWAC_EVENT_POINTER_AXIS_DISCRETE:
384 if (!wlf_handle_pointer_axis_discrete(instance, &event.mouse_axis))
388 case UWAC_EVENT_POINTER_FRAME:
389 if (!wlf_handle_pointer_frame(instance, &event.mouse_frame))
392 case UWAC_EVENT_POINTER_SOURCE:
393 if (!wlf_handle_pointer_source(instance, &event.mouse_source))
398 if (!wlf_handle_key(instance, &event.key))
403 case UWAC_EVENT_TOUCH_UP:
404 if (!wlf_handle_touch_up(instance, &event.touchUp))
409 case UWAC_EVENT_TOUCH_DOWN:
410 if (!wlf_handle_touch_down(instance, &event.touchDown))
415 case UWAC_EVENT_TOUCH_MOTION:
416 if (!wlf_handle_touch_motion(instance, &event.touchMotion))
421 case UWAC_EVENT_KEYBOARD_ENTER:
423 UwacSeatInhibitShortcuts(event.keyboard_enter_leave.seat,
true);
425 if (!wlf_keyboard_enter(instance, &event.keyboard_enter_leave))
430 case UWAC_EVENT_KEYBOARD_MODIFIERS:
431 if (!wlf_keyboard_modifiers(instance, &event.keyboard_modifiers))
436 case UWAC_EVENT_CONFIGURE:
437 if (!wlf_disp_handle_configure(context->disp, event.configure.width,
438 event.configure.height))
441 if (!wl_refresh_display(context))
446 case UWAC_EVENT_CLIPBOARD_AVAILABLE:
447 case UWAC_EVENT_CLIPBOARD_OFFER:
448 case UWAC_EVENT_CLIPBOARD_SELECT:
449 if (!wlf_cliprdr_handle_event(context->clipboard, &event.clipboard))
454 case UWAC_EVENT_CLOSE:
455 context->closed = TRUE;
467 static BOOL handle_window_events(freerdp* instance)
475 static int wlfreerdp_run(freerdp* instance)
478 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = { 0 };
479 DWORD status = WAIT_ABANDONED;
481 LARGE_INTEGER due = { 0 };
483 TimerEventArgs timerEvent;
484 EventArgsInit(&timerEvent,
"xfreerdp");
494 if (!freerdp_connect(instance))
496 WLog_Print(context->log, WLOG_ERROR,
"Failed to connect");
500 timer = CreateWaitableTimerA(NULL, FALSE,
"mainloop-periodic-timer");
504 WLog_ERR(TAG,
"failed to create timer");
510 if (!SetWaitableTimer(timer, &due, 20, NULL, NULL, FALSE))
515 while (!freerdp_shall_disconnect_context(instance->context))
518 handles[count++] = timer;
519 handles[count++] = context->displayHandle;
520 count += freerdp_get_event_handles(instance->context, &handles[count],
521 ARRAYSIZE(handles) - count);
525 WLog_Print(context->log, WLOG_ERROR,
"Failed to get FreeRDP file descriptor");
529 status = WaitForMultipleObjects(count, handles, FALSE, INFINITE);
531 if (WAIT_FAILED == status)
533 WLog_Print(context->log, WLOG_ERROR,
"WaitForMultipleObjects failed");
537 if (!handle_uwac_events(instance, context->display))
539 WLog_Print(context->log, WLOG_ERROR,
"error handling UWAC events");
545 WLog_Print(context->log, WLOG_INFO,
"Closed from Wayland");
549 if (freerdp_check_event_handles(instance->context) != TRUE)
551 if (client_auto_reconnect_ex(instance, handle_window_events))
559 if (freerdp_error_info(instance) == 0)
563 if (freerdp_get_last_error(instance->context) == FREERDP_ERROR_SUCCESS)
564 WLog_Print(context->log, WLOG_ERROR,
"Failed to check FreeRDP file descriptor");
569 if ((status != WAIT_TIMEOUT) && (status == WAIT_OBJECT_0))
571 timerEvent.now = GetTickCount64();
572 PubSub_OnTimer(context->common.context.pubSub, context, &timerEvent);
578 (void)CloseHandle(timer);
579 freerdp_disconnect(instance);
583 static BOOL wlf_client_global_init(
void)
585 (void)setlocale(LC_ALL,
"");
587 if (freerdp_handle_signals() != 0)
593 static void wlf_client_global_uninit(
void)
597 static int wlf_logon_error_info(freerdp* instance, UINT32 data, UINT32 type)
600 const char* str_data = freerdp_get_logon_error_info_data(data);
601 const char* str_type = freerdp_get_logon_error_info_type(type);
603 if (!instance || !instance->context)
607 WLog_Print(wlf->log, WLOG_INFO,
"Logon Error Info %s [%s]", str_data, str_type);
611 static void wlf_client_free(freerdp* instance, rdpContext* context)
619 UwacCloseDisplay(&wlf->display);
621 if (wlf->displayHandle)
622 (void)CloseHandle(wlf->displayHandle);
623 ArrayList_Free(wlf->events);
624 DeleteCriticalSection(&wlf->critical);
627 static void* uwac_event_clone(
const void* val)
629 UwacEvent* copy = NULL;
630 const UwacEvent* ev = (
const UwacEvent*)val;
632 copy = calloc(1,
sizeof(UwacEvent));
639 static BOOL wlf_client_new(freerdp* instance, rdpContext* context)
642 UwacReturnCode status = UWAC_ERROR_INTERNAL;
645 if (!instance || !context)
648 instance->PreConnect = wl_pre_connect;
649 instance->PostConnect = wl_post_connect;
650 instance->PostDisconnect = wl_post_disconnect;
651 instance->LogonErrorInfo = wlf_logon_error_info;
652 wfl->log = WLog_Get(TAG);
653 wfl->display = UwacOpenDisplay(NULL, &status);
655 if (!wfl->display || (status != UWAC_SUCCESS) || !wfl->log)
658 wfl->displayHandle = CreateFileDescriptorEvent(NULL, FALSE, FALSE,
659 UwacDisplayGetFd(wfl->display), WINPR_FD_READ);
661 if (!wfl->displayHandle)
664 wfl->events = ArrayList_New(FALSE);
668 obj = ArrayList_Object(wfl->events);
669 obj->fnObjectNew = uwac_event_clone;
670 obj->fnObjectFree = free;
672 InitializeCriticalSection(&wfl->critical);
677 static int wfl_client_start(rdpContext* context)
679 WINPR_UNUSED(context);
683 static int RdpClientEntry(RDP_CLIENT_ENTRY_POINTS* pEntryPoints)
685 WINPR_ASSERT(pEntryPoints);
686 ZeroMemory(pEntryPoints,
sizeof(RDP_CLIENT_ENTRY_POINTS));
687 pEntryPoints->Version = RDP_CLIENT_INTERFACE_VERSION;
688 pEntryPoints->Size =
sizeof(RDP_CLIENT_ENTRY_POINTS_V1);
689 pEntryPoints->GlobalInit = wlf_client_global_init;
690 pEntryPoints->GlobalUninit = wlf_client_global_uninit;
691 pEntryPoints->ContextSize =
sizeof(
wlfContext);
692 pEntryPoints->ClientNew = wlf_client_new;
693 pEntryPoints->ClientFree = wlf_client_free;
694 pEntryPoints->ClientStart = wfl_client_start;
695 pEntryPoints->ClientStop = freerdp_client_common_stop;
699 int main(
int argc,
char* argv[])
703 RDP_CLIENT_ENTRY_POINTS clientEntryPoints;
704 rdpContext* context = NULL;
705 rdpSettings* settings = NULL;
708 freerdp_client_warn_deprecated(argc, argv);
710 RdpClientEntry(&clientEntryPoints);
711 context = freerdp_client_context_new(&clientEntryPoints);
715 settings = context->settings;
717 status = freerdp_client_settings_parse_command_line(settings, argc, argv, FALSE);
720 rc = freerdp_client_settings_command_line_status_print(settings, status, argc, argv);
723 wlf_list_monitors(wlc);
728 if (freerdp_client_start(context) != 0)
731 rc = wlfreerdp_run(context->instance);
733 if (freerdp_client_stop(context) != 0)
737 freerdp_client_context_free(context);
741 BOOL wlf_copy_image(
const void* src,
size_t srcStride,
size_t srcWidth,
size_t srcHeight,
void* dst,
742 size_t dstStride,
size_t dstWidth,
size_t dstHeight,
const RECTANGLE_16* area,
747 if (!src || !dst || !area)
752 WINPR_ASSERT(dstStride <= UINT32_MAX);
753 WINPR_ASSERT(dstWidth <= UINT32_MAX);
754 WINPR_ASSERT(dstHeight <= UINT32_MAX);
755 WINPR_ASSERT(srcStride <= UINT32_MAX);
756 WINPR_ASSERT(srcWidth <= UINT32_MAX);
757 WINPR_ASSERT(srcHeight <= UINT32_MAX);
758 return freerdp_image_scale(dst, PIXEL_FORMAT_BGRA32, (UINT32)dstStride, 0, 0,
759 (UINT32)dstWidth, (UINT32)dstHeight, src, PIXEL_FORMAT_BGRA32,
760 (UINT32)srcStride, 0, 0, (UINT32)srcWidth, (UINT32)srcHeight);
764 const size_t baseSrcOffset = 1ULL * area->top * srcStride + 4ULL * area->left;
765 const size_t baseDstOffset = 1ULL * area->top * dstStride + 4ULL * area->left;
766 const size_t width = MIN((
size_t)area->right - area->left, dstWidth - area->left);
767 const size_t height = MIN((
size_t)area->bottom - area->top, dstHeight - area->top);
768 const BYTE* psrc = (
const BYTE*)src;
769 BYTE* pdst = (BYTE*)dst;
771 for (
size_t i = 0; i < height; i++)
773 const size_t srcOffset = i * srcStride + baseSrcOffset;
774 const size_t dstOffset = i * dstStride + baseDstOffset;
775 memcpy(&pdst[dstOffset], &psrc[srcOffset], width * 4);
784 BOOL wlf_scale_coordinates(rdpContext* context, UINT32* px, UINT32* py, BOOL fromLocalToRDP)
787 UwacSize geometry = { 0 };
789 if (!context || !px || !py || !context->gdi)
795 rdpGdi* gdi = context->gdi;
797 if (UwacWindowGetDrawingBufferGeometry(wlf->window, &geometry, NULL) != UWAC_SUCCESS)
800 const double sx = 1.0 * geometry.width / (double)gdi->width;
801 const double sy = 1.0 * geometry.height / (
double)gdi->height;
805 *px *= (UINT32)lround(sx);
806 *py *= (UINT32)lround(sy);
810 *px /= (UINT32)lround(sx);
811 *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.