22#include <freerdp/config.h>
24#include <winpr/windows.h>
25#include <winpr/library.h>
28#include <winpr/assert.h>
35#include <winpr/assert.h>
39#ifdef WITH_PROGRESS_BAR
43#ifdef WITH_WINDOWS_CERT_STORE
47#include <freerdp/log.h>
48#include <freerdp/freerdp.h>
49#include <freerdp/constants.h>
50#include <freerdp/settings.h>
52#include <freerdp/locale/locale.h>
53#include <freerdp/locale/keyboard.h>
54#include <freerdp/codec/region.h>
55#include <freerdp/client/cmdline.h>
56#include <freerdp/client/channels.h>
57#include <freerdp/channels/channels.h>
61#include "wf_channels.h"
62#include "wf_graphics.h"
64#include "resource/resource.h"
66#define TAG CLIENT_TAG("windows")
68#define WM_FREERDP_SHOWWINDOW (WM_USER + 100)
70static BOOL wf_has_console(
void)
72#ifdef WITH_WIN_CONSOLE
73 int file = _fileno(stdin);
74 int tty = _isatty(file);
80 WLog_INFO(TAG,
"Detected stdin=%d -> %s mode", file, tty ?
"console" :
"gui");
84static BOOL wf_end_paint(rdpContext* context)
86 RECT updateRect = { 0 };
91 WINPR_ASSERT(context);
93 wfContext* wfc = (wfContext*)context;
94 rdpGdi* gdi = context->gdi;
97 HGDI_DC hdc = gdi->primary->hdc;
103 WINPR_ASSERT(hwnd->invalid || (hwnd->ninvalid == 0));
105 if (hwnd->invalid->null)
108 const int ninvalid = hwnd->ninvalid;
109 const HGDI_RGN cinvalid = hwnd->cinvalid;
114 region16_init(&invalidRegion);
116 for (
int i = 0; i < ninvalid; i++)
118 invalidRect.left = cinvalid[i].x;
119 invalidRect.top = cinvalid[i].y;
120 invalidRect.right = cinvalid[i].x + cinvalid[i].w;
121 invalidRect.bottom = cinvalid[i].y + cinvalid[i].h;
122 region16_union_rect(&invalidRegion, &invalidRegion, &invalidRect);
125 if (!region16_is_empty(&invalidRegion))
127 extents = region16_extents(&invalidRegion);
128 updateRect.left = extents->left;
129 updateRect.top = extents->top;
130 updateRect.right = extents->right;
131 updateRect.bottom = extents->bottom;
133 wf_scale_rect(wfc, &updateRect);
135 InvalidateRect(wfc->hwnd, &updateRect, FALSE);
138 wf_rail_invalidate_region(wfc, &invalidRegion);
141 region16_uninit(&invalidRegion);
145 wfc->is_shown = TRUE;
147#ifdef WITH_PROGRESS_BAR
148 if (wfc->taskBarList)
150 wfc->taskBarList->lpVtbl->SetProgressState(wfc->taskBarList, wfc->hwnd,
155 PostMessage(wfc->hwnd, WM_FREERDP_SHOWWINDOW, 0, 0);
156 WLog_INFO(TAG,
"Window is shown!");
161static BOOL wf_begin_paint(rdpContext* context)
165 if (!context || !context->gdi || !context->gdi->primary || !context->gdi->primary->hdc)
168 hdc = context->gdi->primary->hdc;
170 if (!hdc || !hdc->hwnd || !hdc->hwnd->invalid)
173 hdc->hwnd->invalid->null = TRUE;
174 hdc->hwnd->ninvalid = 0;
178static BOOL wf_desktop_resize(rdpContext* context)
182 rdpSettings* settings;
183 wfContext* wfc = (wfContext*)context;
185 if (!context || !context->settings)
188 settings = context->settings;
192 same = (wfc->primary == wfc->drawing) ? TRUE : FALSE;
193 wf_image_free(wfc->primary);
197 context->gdi->dstFormat, NULL);
202 context->gdi->dstFormat, wfc->primary->pdata, NULL))
206 wfc->drawing = wfc->primary;
208 if (wfc->fullscreen != TRUE)
211 SetWindowPos(wfc->hwnd, HWND_TOP, -1, -1,
218 wf_update_offset(wfc);
219 GetWindowRect(wfc->hwnd, &rect);
220 InvalidateRect(wfc->hwnd, &rect, TRUE);
226static BOOL wf_pre_connect(freerdp* instance)
228 WINPR_ASSERT(instance);
229 WINPR_ASSERT(instance->context);
230 WINPR_ASSERT(instance->context->settings);
232 rdpContext* context = instance->context;
233 wfContext* wfc = (wfContext*)instance->context;
234 rdpSettings* settings = context->settings;
244 if (wfc->percentscreen > 0)
246 desktopWidth = (GetSystemMetrics(SM_CXSCREEN) * wfc->percentscreen) / 100;
249 desktopHeight = (GetSystemMetrics(SM_CYSCREEN) * wfc->percentscreen) / 100;
258 desktopWidth = GetSystemMetrics(SM_CXVIRTUALSCREEN);
259 desktopHeight = GetSystemMetrics(SM_CYVIRTUALSCREEN);
263 desktopWidth = GetSystemMetrics(SM_CXSCREEN);
264 desktopHeight = GetSystemMetrics(SM_CYSCREEN);
270 desktopWidth = (desktopWidth + 3) & (~3);
287 CHAR name[KL_NAMELENGTH + 1] = { 0 };
288 if (GetKeyboardLayoutNameA(name))
293 rc = strtoul(name, NULL, 16);
295 keyboardLayoutId = rc;
298 if (keyboardLayoutId == 0)
300 const HKL layout = GetKeyboardLayout(0);
301 const uint32_t masked = (uint32_t)(((uintptr_t)layout >> 16) & 0xFFFF);
302 keyboardLayoutId = masked;
306 if (keyboardLayoutId == 0)
307 freerdp_detect_keyboard_layout_from_system_locale(&keyboardLayoutId);
308 if (keyboardLayoutId == 0)
309 keyboardLayoutId = ENGLISH_UNITED_STATES;
312 PubSub_SubscribeChannelConnected(instance->context->pubSub, wf_OnChannelConnectedEventHandler);
313 PubSub_SubscribeChannelDisconnected(instance->context->pubSub,
314 wf_OnChannelDisconnectedEventHandler);
318static void wf_append_item_to_system_menu(HMENU hMenu, UINT fMask, UINT wID,
const wchar_t* text,
321 MENUITEMINFO item_info = { 0 };
322 item_info.fMask = fMask;
323 item_info.cbSize =
sizeof(MENUITEMINFO);
325 item_info.fType = MFT_STRING;
326 item_info.dwTypeData = _wcsdup(text);
327 item_info.cch = (UINT)_wcslen(text);
329 item_info.dwItemData = (ULONG_PTR)wfc;
330 InsertMenuItem(hMenu, wfc->systemMenuInsertPosition++, TRUE, &item_info);
333static void wf_add_system_menu(wfContext* wfc)
337 if (wfc->fullscreen && !wfc->fullscreen_toggle)
347 hMenu = GetSystemMenu(wfc->hwnd, FALSE);
349 wf_append_item_to_system_menu(hMenu,
350 MIIM_CHECKMARKS | MIIM_FTYPE | MIIM_ID | MIIM_STRING | MIIM_DATA,
351 SYSCOMMAND_ID_SMARTSIZING, L
"Smart sizing", wfc);
355 CheckMenuItem(hMenu, SYSCOMMAND_ID_SMARTSIZING, MF_CHECKED);
359 wf_append_item_to_system_menu(hMenu, MIIM_FTYPE | MIIM_ID | MIIM_STRING,
360 SYSCOMMAND_ID_REQUEST_CONTROL, L
"Request control", wfc);
363static WCHAR* wf_window_get_title(rdpSettings* settings)
366 WCHAR* windowTitle = NULL;
368 WCHAR prefix[] = L
"FreeRDP:";
380 size = strlen(name) + 16 + wcslen(prefix);
381 windowTitle = calloc(size,
sizeof(WCHAR));
387 _snwprintf_s(windowTitle, size, _TRUNCATE, L
"%s %S", prefix, name);
389 _snwprintf_s(windowTitle, size, _TRUNCATE, L
"%s %S:%u", prefix, name,
395static BOOL wf_post_connect(freerdp* instance)
402 rdpSettings* settings;
403 EmbedWindowEventArgs e;
404 const UINT32 format = PIXEL_FORMAT_BGRX32;
406 WINPR_ASSERT(instance);
408 context = instance->context;
409 WINPR_ASSERT(context);
411 settings = context->settings;
412 WINPR_ASSERT(settings);
414 wfc = (wfContext*)instance->context;
421 if (!gdi_init_ex(instance, format, 0, wfc->primary->pdata, NULL))
424 cache = instance->context->cache;
427 gdi = instance->context->gdi;
431 wf_gdi_register_update_callbacks(context->update);
434 wfc->window_title = wf_window_get_title(settings);
436 if (!wfc->window_title)
448 dwStyle = WS_CHILD | WS_BORDER;
451 WS_CAPTION | WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX | WS_SIZEBOX | WS_MAXIMIZEBOX;
455 wfc->hwnd = CreateWindowEx(0, wfc->wndClassName, wfc->window_title, dwStyle, 0, 0, 0, 0,
456 wfc->hWndParent, NULL, wfc->hInstance, NULL);
457 SetWindowLongPtr(wfc->hwnd, GWLP_USERDATA, (LONG_PTR)wfc);
460 wf_resize_window(wfc);
461 wf_add_system_menu(wfc);
464 wfc->drawing = wfc->primary;
465 EventArgsInit(&e,
"wfreerdp");
467 e.handle = (
void*)wfc->hwnd;
468 PubSub_OnEmbedWindow(context->pubSub, context, &e);
469#ifdef WITH_PROGRESS_BAR
470 if (wfc->taskBarList)
472 ShowWindow(wfc->hwnd, SW_SHOWMINIMIZED);
473 wfc->taskBarList->lpVtbl->SetProgressState(wfc->taskBarList, wfc->hwnd, TBPF_INDETERMINATE);
476 UpdateWindow(wfc->hwnd);
477 context->update->BeginPaint = wf_begin_paint;
478 context->update->DesktopResize = wf_desktop_resize;
479 context->update->EndPaint = wf_end_paint;
480 wf_register_pointer(context->graphics);
482 wfc->floatbar = wf_floatbar_new(wfc, wfc->hInstance,
487static void wf_post_disconnect(freerdp* instance)
491 if (!instance || !instance->context)
494 wfc = (wfContext*)instance->context;
495 free(wfc->window_title);
498static CREDUI_INFOW wfUiInfo = {
sizeof(CREDUI_INFOW), NULL, L
"Enter your credentials",
499 L
"Remote Desktop Security", NULL };
501static BOOL wf_authenticate_ex(freerdp* instance,
char** username,
char** password,
char** domain,
502 rdp_auth_reason reason)
508 WCHAR UserNameW[CREDUI_MAX_USERNAME_LENGTH + 1] = { 0 };
509 WCHAR UserW[CREDUI_MAX_USERNAME_LENGTH + 1] = { 0 };
510 WCHAR DomainW[CREDUI_MAX_DOMAIN_TARGET_LENGTH + 1] = { 0 };
511 WCHAR PasswordW[CREDUI_MAX_PASSWORD_LENGTH + 1] = { 0 };
513 WINPR_ASSERT(instance);
514 WINPR_ASSERT(instance->context);
515 WINPR_ASSERT(instance->context->settings);
517 wfc = (wfContext*)instance->context;
520 WINPR_ASSERT(username);
521 WINPR_ASSERT(domain);
522 WINPR_ASSERT(password);
524 const WCHAR auth[] = L
"Target credentials requested";
525 const WCHAR authPin[] = L
"PIN requested";
526 const WCHAR gwAuth[] = L
"Gateway credentials requested";
527 const WCHAR* titleW = auth;
530 dwFlags = CREDUI_FLAGS_DO_NOT_PERSIST | CREDUI_FLAGS_EXCLUDE_CERTIFICATES |
531 CREDUI_FLAGS_USERNAME_TARGET_CREDENTIALS;
538 if ((*username) && (*password))
541 case AUTH_SMARTCARD_PIN:
542 dwFlags &= ~CREDUI_FLAGS_USERNAME_TARGET_CREDENTIALS;
543 dwFlags |= CREDUI_FLAGS_PASSWORD_ONLY_OK | CREDUI_FLAGS_KEEP_USERNAME;
548 *username = _strdup(
"PIN");
561 (void)ConvertUtf8ToWChar(*username, UserNameW, ARRAYSIZE(UserNameW));
562 (void)ConvertUtf8ToWChar(*username, UserW, ARRAYSIZE(UserW));
566 (void)ConvertUtf8ToWChar(*password, PasswordW, ARRAYSIZE(PasswordW));
569 (void)ConvertUtf8ToWChar(*domain, DomainW, ARRAYSIZE(DomainW));
571 if (_wcsnlen(PasswordW, ARRAYSIZE(PasswordW)) == 0)
573 if (!wfc->isConsole &&
575 WLog_ERR(TAG,
"Flag for stdin read present but stdin is redirected; using GUI");
576 if (wfc->isConsole &&
578 status = CredUICmdLinePromptForCredentialsW(titleW, NULL, 0, UserNameW,
579 ARRAYSIZE(UserNameW), PasswordW,
580 ARRAYSIZE(PasswordW), &fSave, dwFlags);
582 status = CredUIPromptForCredentialsW(&wfUiInfo, titleW, NULL, 0, UserNameW,
583 ARRAYSIZE(UserNameW), PasswordW,
584 ARRAYSIZE(PasswordW), &fSave, dwFlags);
585 if (status != NO_ERROR)
587 WLog_ERR(TAG,
"CredUIPromptForCredentials unexpected status: 0x%08lX", status);
591 if ((dwFlags & CREDUI_FLAGS_KEEP_USERNAME) == 0)
593 status = CredUIParseUserNameW(UserNameW, UserW, ARRAYSIZE(UserW), DomainW,
595 if (status != NO_ERROR)
597 CHAR User[CREDUI_MAX_USERNAME_LENGTH + 1] = { 0 };
598 CHAR UserName[CREDUI_MAX_USERNAME_LENGTH + 1] = { 0 };
599 CHAR Domain[CREDUI_MAX_DOMAIN_TARGET_LENGTH + 1] = { 0 };
601 (void)ConvertWCharNToUtf8(UserNameW, ARRAYSIZE(UserNameW), UserName,
602 ARRAYSIZE(UserName));
603 (void)ConvertWCharNToUtf8(UserW, ARRAYSIZE(UserW), User, ARRAYSIZE(User));
604 (void)ConvertWCharNToUtf8(DomainW, ARRAYSIZE(DomainW), Domain, ARRAYSIZE(Domain));
605 WLog_ERR(TAG,
"Failed to parse UserName: %s into User: %s Domain: %s", UserName,
612 *username = ConvertWCharNToUtf8Alloc(UserW, ARRAYSIZE(UserW), NULL);
615 WLog_ERR(TAG,
"ConvertWCharNToUtf8Alloc failed", status);
619 if (_wcsnlen(DomainW, ARRAYSIZE(DomainW)) > 0)
620 *domain = ConvertWCharNToUtf8Alloc(DomainW, ARRAYSIZE(DomainW), NULL);
622 *domain = _strdup(
"\0");
627 WLog_ERR(TAG,
"strdup failed", status);
631 *password = ConvertWCharNToUtf8Alloc(PasswordW, ARRAYSIZE(PasswordW), NULL);
642static WCHAR* wf_format_text(
const WCHAR* fmt, ...)
646 WCHAR* buffer = NULL;
653 rc = _vsnwprintf(buffer, size, fmt, ap);
658 if ((
size_t)rc < size)
661 size = (size_t)rc + 1;
662 tmp = realloc(buffer, size *
sizeof(WCHAR));
674#ifdef WITH_WINDOWS_CERT_STORE
682static void wf_report_error(
char* wszMessage, DWORD dwErrCode)
684 LPSTR pwszMsgBuf = NULL;
686 if (NULL != wszMessage && 0 != *wszMessage)
688 WLog_ERR(TAG,
"%s", wszMessage);
691 FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
696 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
706 if (NULL != pwszMsgBuf)
708 WLog_ERR(TAG,
"Error: 0x%08x (%d) %s", dwErrCode, dwErrCode, pwszMsgBuf);
709 LocalFree(pwszMsgBuf);
713 WLog_ERR(TAG,
"Error: 0x%08x (%d)", dwErrCode, dwErrCode);
717static DWORD wf_is_x509_certificate_trusted(
const char* common_name,
const char* subject,
718 const char* issuer,
const char* fingerprint)
720 HRESULT hr = CRYPT_E_NOT_FOUND;
722 DWORD dwChainFlags = CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT;
724 HCERTCHAINENGINE hChainEngine = NULL;
725 PCCERT_CHAIN_CONTEXT pChainContext = NULL;
727 CERT_ENHKEY_USAGE EnhkeyUsage = { 0 };
728 CERT_USAGE_MATCH CertUsage = { 0 };
729 CERT_CHAIN_PARA ChainPara = { 0 };
730 CERT_CHAIN_POLICY_PARA ChainPolicy = { 0 };
731 CERT_CHAIN_POLICY_STATUS PolicyStatus = { 0 };
732 CERT_CHAIN_ENGINE_CONFIG EngineConfig = { 0 };
734 DWORD derPubKeyLen = WINPR_ASSERTING_INT_CAST(uint32_t, strlen(fingerprint));
735 char* derPubKey = calloc(derPubKeyLen,
sizeof(
char));
736 if (NULL == derPubKey)
738 WLog_ERR(TAG,
"Could not allocate derPubKey");
745 if (!CryptStringToBinaryA(fingerprint, 0, CRYPT_STRING_BASE64HEADER, derPubKey, &derPubKeyLen,
748 WLog_ERR(TAG,
"CryptStringToBinary failed. Err: %d", GetLastError());
755 EnhkeyUsage.cUsageIdentifier = 0;
756 EnhkeyUsage.rgpszUsageIdentifier = NULL;
758 CertUsage.dwType = USAGE_MATCH_TYPE_AND;
759 CertUsage.Usage = EnhkeyUsage;
761 ChainPara.cbSize =
sizeof(ChainPara);
762 ChainPara.RequestedUsage = CertUsage;
764 ChainPolicy.cbSize =
sizeof(ChainPolicy);
766 PolicyStatus.cbSize =
sizeof(PolicyStatus);
768 EngineConfig.cbSize =
sizeof(EngineConfig);
769 EngineConfig.dwUrlRetrievalTimeout = 0;
771 pCert = CertCreateCertificateContext(X509_ASN_ENCODING, derPubKey, derPubKeyLen);
774 WLog_ERR(TAG,
"FAILED: Certificate could not be parsed.");
778 dwChainFlags |= CERT_CHAIN_ENABLE_PEER_TRUST;
787 if (!CertCreateCertificateChainEngine(&EngineConfig, &hChainEngine))
789 hr = HRESULT_FROM_WIN32(GetLastError());
796 if (!CertGetCertificateChain(hChainEngine,
807 hr = HRESULT_FROM_WIN32(GetLastError());
814 if (!CertVerifyCertificateChainPolicy(CERT_CHAIN_POLICY_BASE,
819 hr = HRESULT_FROM_WIN32(GetLastError());
823 if (PolicyStatus.dwError != S_OK)
825 wf_report_error(
"CertVerifyCertificateChainPolicy: Chain Status", PolicyStatus.dwError);
826 hr = PolicyStatus.dwError;
832 if (PolicyStatus.dwError == CRYPT_E_NO_REVOCATION_CHECK ||
833 PolicyStatus.dwError == CRYPT_E_REVOCATION_OFFLINE)
841 WLog_INFO(TAG,
"CertVerifyCertificateChainPolicy succeeded for %s (%s) issued by %s",
842 common_name, subject, issuer);
849 WLog_INFO(TAG,
"CertVerifyCertificateChainPolicy failed for %s (%s) issued by %s",
850 common_name, subject, issuer);
851 wf_report_error(NULL, hr);
856 if (NULL != pChainContext)
858 CertFreeCertificateChain(pChainContext);
861 if (NULL != hChainEngine)
863 CertFreeCertificateChainEngine(hChainEngine);
868 CertFreeCertificateContext(pCert);
875static DWORD wf_cli_verify_certificate_ex(freerdp* instance,
const char* host, UINT16 port,
876 const char* common_name,
const char* subject,
877 const char* issuer,
const char* fingerprint, DWORD flags)
879#ifdef WITH_WINDOWS_CERT_STORE
880 if (flags & VERIFY_CERT_FLAG_FP_IS_PEM && !(flags & VERIFY_CERT_FLAG_MISMATCH))
882 if (wf_is_x509_certificate_trusted(common_name, subject, issuer, fingerprint) == S_OK)
889 return client_cli_verify_certificate_ex(instance, host, port, common_name, subject, issuer,
893static DWORD wf_verify_certificate_ex(freerdp* instance,
const char* host, UINT16 port,
894 const char* common_name,
const char* subject,
895 const char* issuer,
const char* fingerprint, DWORD flags)
901#ifdef WITH_WINDOWS_CERT_STORE
902 if (flags & VERIFY_CERT_FLAG_FP_IS_PEM && !(flags & VERIFY_CERT_FLAG_MISMATCH))
904 if (wf_is_x509_certificate_trusted(common_name, subject, issuer, fingerprint) == S_OK)
911 buffer = wf_format_text(
912 L
"Certificate details:\n"
913 L
"\tCommonName: %S\n"
916 L
"\tThumbprint: %S\n"
917 L
"\tHostMismatch: %S\n"
919 L
"The above X.509 certificate could not be verified, possibly because you do not have "
920 L
"the CA certificate in your certificate store, or the certificate has expired. "
921 L
"Please look at the OpenSSL documentation on how to add a private CA to the store.\n"
923 L
"YES\tAccept permanently\n"
924 L
"NO\tAccept for this session only\n"
925 L
"CANCEL\tAbort connection\n",
926 common_name, subject, issuer, fingerprint,
927 flags & VERIFY_CERT_FLAG_MISMATCH ?
"Yes" :
"No");
928 caption = wf_format_text(L
"Verify certificate for %S:%hu", host, port);
930 WINPR_UNUSED(instance);
932 if (!buffer || !caption)
935 what = MessageBoxW(NULL, buffer, caption, MB_YESNOCANCEL);
953static DWORD wf_verify_changed_certificate_ex(freerdp* instance,
const char* host, UINT16 port,
954 const char* common_name,
const char* subject,
955 const char* issuer,
const char* new_fingerprint,
956 const char* old_subject,
const char* old_issuer,
957 const char* old_fingerprint, DWORD flags)
963 buffer = wf_format_text(
964 L
"New Certificate details:\n"
965 L
"\tCommonName: %S\n"
968 L
"\tThumbprint: %S\n"
969 L
"\tHostMismatch: %S\n"
971 L
"Old Certificate details:\n"
975 L
"The above X.509 certificate could not be verified, possibly because you do not have "
976 L
"the CA certificate in your certificate store, or the certificate has expired. "
977 L
"Please look at the OpenSSL documentation on how to add a private CA to the store.\n"
979 L
"YES\tAccept permanently\n"
980 L
"NO\tAccept for this session only\n"
981 L
"CANCEL\tAbort connection\n",
982 common_name, subject, issuer, new_fingerprint,
983 flags & VERIFY_CERT_FLAG_MISMATCH ?
"Yes" :
"No", old_subject, old_issuer, old_fingerprint);
984 caption = wf_format_text(L
"Verify certificate change for %S:%hu", host, port);
986 WINPR_UNUSED(instance);
987 if (!buffer || !caption)
990 what = MessageBoxW(NULL, buffer, caption, MB_YESNOCANCEL);
1008static BOOL wf_present_gateway_message(freerdp* instance, UINT32 type, BOOL isDisplayMandatory,
1009 BOOL isConsentMandatory,
size_t length,
const WCHAR* message)
1011 if (!isDisplayMandatory && !isConsentMandatory)
1015 if (type == GATEWAY_MESSAGE_CONSENT && isConsentMandatory)
1020 msg = wf_format_text(L
"%.*s\n\nI understand and agree to the terms of this policy", length,
1022 mbRes = MessageBoxW(NULL, msg, L
"Consent Message", MB_YESNO);
1029 return client_cli_present_gateway_message(instance, type, isDisplayMandatory,
1030 isConsentMandatory, length, message);
1035static DWORD WINAPI wf_client_thread(LPVOID lpParam)
1040 BOOL msg_ret = FALSE;
1044 freerdp* instance = (freerdp*)lpParam;
1045 WINPR_ASSERT(instance);
1047 if (!freerdp_connect(instance))
1050 rdpContext* context = instance->context;
1051 WINPR_ASSERT(context);
1053 wfContext* wfc = (wfContext*)instance->context;
1056 rdpChannels* channels = context->channels;
1057 WINPR_ASSERT(channels);
1059 rdpSettings* settings = context->settings;
1060 WINPR_ASSERT(settings);
1062 while (!freerdp_shall_disconnect_context(instance->context))
1064 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = { 0 };
1067 if (freerdp_focus_required(instance))
1069 wf_event_focus_in(wfc);
1070 wf_event_focus_in(wfc);
1074 DWORD tmp = freerdp_get_event_handles(context, &handles[nCount], 64 - nCount);
1078 WLog_ERR(TAG,
"freerdp_get_event_handles failed");
1085 DWORD status = MsgWaitForMultipleObjectsEx(nCount, handles, 5 * 1000, QS_ALLINPUT,
1086 MWMO_ALERTABLE | MWMO_INPUTAVAILABLE);
1087 if (status == WAIT_FAILED)
1089 WLog_ERR(TAG,
"wfreerdp_run: WaitForMultipleObjects failed: 0x%08lX", GetLastError());
1094 if (!freerdp_check_event_handles(context))
1096 if (client_auto_reconnect(instance))
1099 WLog_ERR(TAG,
"Failed to check FreeRDP file descriptor");
1104 if (freerdp_shall_disconnect_context(instance->context))
1109 while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
1111 msg_ret = GetMessage(&msg, NULL, 0, 0);
1115 if ((msg.message == WM_SETFOCUS) && (msg.lParam == 1))
1117 PostMessage(wfc->hwnd, WM_SETFOCUS, 0, 0);
1119 else if ((msg.message == WM_KILLFOCUS) && (msg.lParam == 1))
1121 PostMessage(wfc->hwnd, WM_KILLFOCUS, 0, 0);
1125 switch (msg.message)
1129 width = LOWORD(msg.lParam);
1130 height = HIWORD(msg.lParam);
1131 SetWindowPos(wfc->hwnd, HWND_TOP, 0, 0, width, height, SWP_FRAMECHANGED);
1134 case WM_FREERDP_SHOWWINDOW:
1136 ShowWindow(wfc->hwnd, SW_NORMAL);
1143 if ((msg_ret == 0) || (msg_ret == -1))
1149 TranslateMessage(&msg);
1150 DispatchMessage(&msg);
1158 freerdp_disconnect(instance);
1161 error = freerdp_get_last_error(instance->context);
1162 WLog_DBG(TAG,
"Main thread exited with %" PRIu32, error);
1167static DWORD WINAPI wf_keyboard_thread(LPVOID lpParam)
1173 wfc = (wfContext*)lpParam;
1174 WINPR_ASSERT(NULL != wfc);
1175 hook_handle = SetWindowsHookEx(WH_KEYBOARD_LL, wf_ll_kbd_proc, wfc->hInstance, 0);
1179 while ((status = GetMessage(&msg, NULL, 0, 0)) != 0)
1183 WLog_ERR(TAG,
"keyboard thread error getting message");
1188 TranslateMessage(&msg);
1189 DispatchMessage(&msg);
1193 UnhookWindowsHookEx(hook_handle);
1197 WLog_ERR(TAG,
"failed to install keyboard hook");
1200 WLog_DBG(TAG,
"Keyboard thread exited.");
1205int freerdp_client_set_window_size(wfContext* wfc,
int width,
int height)
1207 WLog_DBG(TAG,
"freerdp_client_set_window_size %d, %d", width, height);
1209 if ((width != wfc->client_width) || (height != wfc->client_height))
1211 PostThreadMessage(wfc->mainThreadId, WM_SIZE, SIZE_RESTORED,
1212 ((UINT)height << 16) | (UINT)width);
1218void wf_size_scrollbars(wfContext* wfc, UINT32 client_width, UINT32 client_height)
1220 const rdpSettings* settings;
1223 settings = wfc->common.context.settings;
1224 WINPR_ASSERT(settings);
1226 if (wfc->disablewindowtracking)
1230 wfc->disablewindowtracking = TRUE;
1235 wfc->xCurrentScroll = 0;
1236 wfc->yCurrentScroll = 0;
1238 if (wfc->xScrollVisible || wfc->yScrollVisible)
1240 if (ShowScrollBar(wfc->hwnd, SB_BOTH, FALSE))
1242 wfc->xScrollVisible = FALSE;
1243 wfc->yScrollVisible = FALSE;
1250 BOOL horiz = wfc->xScrollVisible;
1251 BOOL vert = wfc->yScrollVisible;
1260 settings, FreeRDP_DesktopWidth) )
1272 settings, FreeRDP_DesktopHeight) )
1277 if (horiz == vert && (horiz != wfc->xScrollVisible && vert != wfc->yScrollVisible))
1279 if (ShowScrollBar(wfc->hwnd, SB_BOTH, horiz))
1281 wfc->xScrollVisible = horiz;
1282 wfc->yScrollVisible = vert;
1286 if (horiz != wfc->xScrollVisible)
1288 if (ShowScrollBar(wfc->hwnd, SB_HORZ, horiz))
1290 wfc->xScrollVisible = horiz;
1294 if (vert != wfc->yScrollVisible)
1296 if (ShowScrollBar(wfc->hwnd, SB_VERT, vert))
1298 wfc->yScrollVisible = vert;
1309 wfc->xCurrentScroll = MIN(wfc->xCurrentScroll, wfc->xMaxScroll);
1310 si.cbSize =
sizeof(si);
1311 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
1312 si.nMin = wfc->xMinScroll;
1314 si.nPage = client_width;
1315 si.nPos = wfc->xCurrentScroll;
1316 SetScrollInfo(wfc->hwnd, SB_HORZ, &si, TRUE);
1324 wfc->yMaxScroll = MAX(
1326 wfc->yCurrentScroll = MIN(wfc->yCurrentScroll, wfc->yMaxScroll);
1327 si.cbSize =
sizeof(si);
1328 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
1329 si.nMin = wfc->yMinScroll;
1331 si.nPage = client_height;
1332 si.nPos = wfc->yCurrentScroll;
1333 SetScrollInfo(wfc->hwnd, SB_VERT, &si, TRUE);
1337 wfc->disablewindowtracking = FALSE;
1338 wf_update_canvas_diff(wfc);
1341static BOOL wfreerdp_client_global_init(
void)
1345 WSAStartup(0x101, &wsaData);
1347 freerdp_register_addin_provider(freerdp_channels_load_static_addin_entry, 0);
1352static void wfreerdp_client_global_uninit(
void)
1357static BOOL wfreerdp_client_new(freerdp* instance, rdpContext* context)
1359 wfContext* wfc = (wfContext*)context;
1365 wfc->isConsole = wf_has_console();
1367 if (!(wfreerdp_client_global_init()))
1370 WINPR_ASSERT(instance);
1371 instance->PreConnect = wf_pre_connect;
1372 instance->PostConnect = wf_post_connect;
1373 instance->PostDisconnect = wf_post_disconnect;
1374 instance->AuthenticateEx = wf_authenticate_ex;
1376#ifdef WITH_WINDOWS_CERT_STORE
1381 if (!wfc->isConsole)
1383 instance->VerifyCertificateEx = wf_verify_certificate_ex;
1384 instance->VerifyChangedCertificateEx = wf_verify_changed_certificate_ex;
1385 instance->PresentGatewayMessage = wf_present_gateway_message;
1388#ifdef WITH_PROGRESS_BAR
1389 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1390 CoCreateInstance(&CLSID_TaskbarList, NULL, CLSCTX_ALL, &IID_ITaskbarList3,
1391 (
void**)&wfc->taskBarList);
1397static void wfreerdp_client_free(freerdp* instance, rdpContext* context)
1399 WINPR_UNUSED(instance);
1403#ifdef WITH_PROGRESS_BAR
1408static int wfreerdp_client_start(rdpContext* context)
1410 wfContext* wfc = (wfContext*)context;
1412 WINPR_ASSERT(context);
1413 WINPR_ASSERT(context->settings);
1415 freerdp* instance = context->instance;
1416 WINPR_ASSERT(instance);
1418 rdpSettings* settings = context->settings;
1419 WINPR_ASSERT(settings);
1421 HINSTANCE hInstance = GetModuleHandle(NULL);
1426 wfc->hWndParent = hWndParent;
1430 typedef UINT(WINAPI * GetDpiForWindow_t)(HWND hwnd);
1431 typedef BOOL(WINAPI * SetProcessDPIAware_t)(void);
1433 HMODULE
module = GetModuleHandle(_T("User32"));
1436 GetDpiForWindow_t pGetDpiForWindow =
1437 GetProcAddressAs(module,
"GetDpiForWindow", GetDpiForWindow_t);
1438 SetProcessDPIAware_t pSetProcessDPIAware =
1439 GetProcAddressAs(module,
"SetProcessDPIAware", SetProcessDPIAware_t);
1440 if (pGetDpiForWindow && pSetProcessDPIAware)
1442 const UINT dpiAwareness = pGetDpiForWindow(hWndParent);
1443 if (dpiAwareness != USER_DEFAULT_SCREEN_DPI)
1444 pSetProcessDPIAware();
1446 FreeLibrary(module);
1454 wfc->systemMenuInsertPosition = 6;
1456 wfc->hInstance = hInstance;
1457 wfc->cursor = LoadCursor(NULL, IDC_ARROW);
1458 wfc->icon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1));
1459 wfc->wndClassName = _tcsdup(_T(
"FreeRDP"));
1460 wfc->wndClass.cbSize =
sizeof(WNDCLASSEX);
1461 wfc->wndClass.style = CS_HREDRAW | CS_VREDRAW;
1462 wfc->wndClass.lpfnWndProc = wf_event_proc;
1463 wfc->wndClass.cbClsExtra = 0;
1464 wfc->wndClass.cbWndExtra = 0;
1465 wfc->wndClass.hCursor = NULL;
1466 wfc->wndClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
1467 wfc->wndClass.lpszMenuName = NULL;
1468 wfc->wndClass.lpszClassName = wfc->wndClassName;
1469 wfc->wndClass.hInstance = hInstance;
1470 wfc->wndClass.hIcon = wfc->icon;
1471 wfc->wndClass.hIconSm = wfc->icon;
1472 RegisterClassEx(&(wfc->wndClass));
1473 wfc->keyboardThread =
1474 CreateThread(NULL, 0, wf_keyboard_thread, (
void*)wfc, 0, &wfc->keyboardThreadId);
1476 if (!wfc->keyboardThread)
1479 wfc->common.thread =
1480 CreateThread(NULL, 0, wf_client_thread, (
void*)instance, 0, &wfc->mainThreadId);
1482 if (!wfc->common.thread)
1488static int wfreerdp_client_stop(rdpContext* context)
1491 wfContext* wfc = (wfContext*)context;
1494 PostThreadMessage(wfc->mainThreadId, WM_QUIT, 0, 0);
1495 rc = freerdp_client_common_stop(context);
1496 wfc->mainThreadId = 0;
1498 if (wfc->keyboardThread)
1500 PostThreadMessage(wfc->keyboardThreadId, WM_QUIT, 0, 0);
1501 (void)WaitForSingleObject(wfc->keyboardThread, INFINITE);
1502 (void)CloseHandle(wfc->keyboardThread);
1503 wfc->keyboardThread = NULL;
1504 wfc->keyboardThreadId = 0;
1510int RdpClientEntry(RDP_CLIENT_ENTRY_POINTS* pEntryPoints)
1512 pEntryPoints->Version = 1;
1513 pEntryPoints->Size =
sizeof(RDP_CLIENT_ENTRY_POINTS_V1);
1514 pEntryPoints->GlobalInit = wfreerdp_client_global_init;
1515 pEntryPoints->GlobalUninit = wfreerdp_client_global_uninit;
1516 pEntryPoints->ContextSize =
sizeof(wfContext);
1517 pEntryPoints->ClientNew = wfreerdp_client_new;
1518 pEntryPoints->ClientFree = wfreerdp_client_free;
1519 pEntryPoints->ClientStart = wfreerdp_client_start;
1520 pEntryPoints->ClientStop = wfreerdp_client_stop;
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 UINT64 freerdp_settings_get_uint64(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt64 id)
Returns a UINT64 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 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_bool(rdpSettings *settings, FreeRDP_Settings_Keys_Bool id, BOOL param)
Sets a BOOL settings value.