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)
93 wfContext* wfc = (wfContext*)context;
95 ninvalid = gdi->primary->hdc->hwnd->ninvalid;
96 cinvalid = gdi->primary->hdc->hwnd->cinvalid;
101 region16_init(&invalidRegion);
103 for (
int i = 0; i < ninvalid; i++)
105 invalidRect.left = cinvalid[i].x;
106 invalidRect.top = cinvalid[i].y;
107 invalidRect.right = cinvalid[i].x + cinvalid[i].w;
108 invalidRect.bottom = cinvalid[i].y + cinvalid[i].h;
109 region16_union_rect(&invalidRegion, &invalidRegion, &invalidRect);
112 if (!region16_is_empty(&invalidRegion))
114 extents = region16_extents(&invalidRegion);
115 updateRect.left = extents->left;
116 updateRect.top = extents->top;
117 updateRect.right = extents->right;
118 updateRect.bottom = extents->bottom;
120 wf_scale_rect(wfc, &updateRect);
122 InvalidateRect(wfc->hwnd, &updateRect, FALSE);
125 wf_rail_invalidate_region(wfc, &invalidRegion);
128 region16_uninit(&invalidRegion);
132 wfc->is_shown = TRUE;
134#ifdef WITH_PROGRESS_BAR
135 if (wfc->taskBarList)
137 wfc->taskBarList->lpVtbl->SetProgressState(wfc->taskBarList, wfc->hwnd,
142 PostMessage(wfc->hwnd, WM_FREERDP_SHOWWINDOW, 0, 0);
143 WLog_INFO(TAG,
"Window is shown!");
148static BOOL wf_begin_paint(rdpContext* context)
152 if (!context || !context->gdi || !context->gdi->primary || !context->gdi->primary->hdc)
155 hdc = context->gdi->primary->hdc;
157 if (!hdc || !hdc->hwnd || !hdc->hwnd->invalid)
160 hdc->hwnd->invalid->null = TRUE;
161 hdc->hwnd->ninvalid = 0;
165static BOOL wf_desktop_resize(rdpContext* context)
169 rdpSettings* settings;
170 wfContext* wfc = (wfContext*)context;
172 if (!context || !context->settings)
175 settings = context->settings;
179 same = (wfc->primary == wfc->drawing) ? TRUE : FALSE;
180 wf_image_free(wfc->primary);
184 context->gdi->dstFormat, NULL);
189 context->gdi->dstFormat, wfc->primary->pdata, NULL))
193 wfc->drawing = wfc->primary;
195 if (wfc->fullscreen != TRUE)
198 SetWindowPos(wfc->hwnd, HWND_TOP, -1, -1,
205 wf_update_offset(wfc);
206 GetWindowRect(wfc->hwnd, &rect);
207 InvalidateRect(wfc->hwnd, &rect, TRUE);
213static BOOL wf_pre_connect(freerdp* instance)
215 WINPR_ASSERT(instance);
216 WINPR_ASSERT(instance->context);
217 WINPR_ASSERT(instance->context->settings);
219 rdpContext* context = instance->context;
220 wfContext* wfc = (wfContext*)instance->context;
221 rdpSettings* settings = context->settings;
231 if (wfc->percentscreen > 0)
233 desktopWidth = (GetSystemMetrics(SM_CXSCREEN) * wfc->percentscreen) / 100;
236 desktopHeight = (GetSystemMetrics(SM_CYSCREEN) * wfc->percentscreen) / 100;
245 desktopWidth = GetSystemMetrics(SM_CXVIRTUALSCREEN);
246 desktopHeight = GetSystemMetrics(SM_CYVIRTUALSCREEN);
250 desktopWidth = GetSystemMetrics(SM_CXSCREEN);
251 desktopHeight = GetSystemMetrics(SM_CYSCREEN);
257 desktopWidth = (desktopWidth + 3) & (~3);
274 CHAR name[KL_NAMELENGTH + 1] = { 0 };
275 if (GetKeyboardLayoutNameA(name))
280 rc = strtoul(name, NULL, 16);
282 keyboardLayoutId = rc;
285 if (keyboardLayoutId == 0)
287 const HKL layout = GetKeyboardLayout(0);
288 const uint32_t masked = (uint32_t)(((uintptr_t)layout >> 16) & 0xFFFF);
289 keyboardLayoutId = masked;
293 if (keyboardLayoutId == 0)
294 freerdp_detect_keyboard_layout_from_system_locale(&keyboardLayoutId);
295 if (keyboardLayoutId == 0)
296 keyboardLayoutId = ENGLISH_UNITED_STATES;
299 PubSub_SubscribeChannelConnected(instance->context->pubSub, wf_OnChannelConnectedEventHandler);
300 PubSub_SubscribeChannelDisconnected(instance->context->pubSub,
301 wf_OnChannelDisconnectedEventHandler);
305static void wf_append_item_to_system_menu(HMENU hMenu, UINT fMask, UINT wID,
const wchar_t* text,
308 MENUITEMINFO item_info = { 0 };
309 item_info.fMask = fMask;
310 item_info.cbSize =
sizeof(MENUITEMINFO);
312 item_info.fType = MFT_STRING;
313 item_info.dwTypeData = _wcsdup(text);
314 item_info.cch = (UINT)_wcslen(text);
316 item_info.dwItemData = (ULONG_PTR)wfc;
317 InsertMenuItem(hMenu, wfc->systemMenuInsertPosition++, TRUE, &item_info);
320static void wf_add_system_menu(wfContext* wfc)
324 if (wfc->fullscreen && !wfc->fullscreen_toggle)
334 hMenu = GetSystemMenu(wfc->hwnd, FALSE);
336 wf_append_item_to_system_menu(hMenu,
337 MIIM_CHECKMARKS | MIIM_FTYPE | MIIM_ID | MIIM_STRING | MIIM_DATA,
338 SYSCOMMAND_ID_SMARTSIZING, L
"Smart sizing", wfc);
342 CheckMenuItem(hMenu, SYSCOMMAND_ID_SMARTSIZING, MF_CHECKED);
346 wf_append_item_to_system_menu(hMenu, MIIM_FTYPE | MIIM_ID | MIIM_STRING,
347 SYSCOMMAND_ID_REQUEST_CONTROL, L
"Request control", wfc);
350static WCHAR* wf_window_get_title(rdpSettings* settings)
353 WCHAR* windowTitle = NULL;
355 WCHAR prefix[] = L
"FreeRDP:";
367 size = strlen(name) + 16 + wcslen(prefix);
368 windowTitle = calloc(size,
sizeof(WCHAR));
374 _snwprintf_s(windowTitle, size, _TRUNCATE, L
"%s %S", prefix, name);
376 _snwprintf_s(windowTitle, size, _TRUNCATE, L
"%s %S:%u", prefix, name,
382static BOOL wf_post_connect(freerdp* instance)
389 rdpSettings* settings;
390 EmbedWindowEventArgs e;
391 const UINT32 format = PIXEL_FORMAT_BGRX32;
393 WINPR_ASSERT(instance);
395 context = instance->context;
396 WINPR_ASSERT(context);
398 settings = context->settings;
399 WINPR_ASSERT(settings);
401 wfc = (wfContext*)instance->context;
408 if (!gdi_init_ex(instance, format, 0, wfc->primary->pdata, NULL))
411 cache = instance->context->cache;
414 gdi = instance->context->gdi;
418 wf_gdi_register_update_callbacks(context->update);
421 wfc->window_title = wf_window_get_title(settings);
423 if (!wfc->window_title)
435 dwStyle = WS_CHILD | WS_BORDER;
438 WS_CAPTION | WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX | WS_SIZEBOX | WS_MAXIMIZEBOX;
442 wfc->hwnd = CreateWindowEx(0, wfc->wndClassName, wfc->window_title, dwStyle, 0, 0, 0, 0,
443 wfc->hWndParent, NULL, wfc->hInstance, NULL);
444 SetWindowLongPtr(wfc->hwnd, GWLP_USERDATA, (LONG_PTR)wfc);
447 wf_resize_window(wfc);
448 wf_add_system_menu(wfc);
451 wfc->drawing = wfc->primary;
452 EventArgsInit(&e,
"wfreerdp");
454 e.handle = (
void*)wfc->hwnd;
455 PubSub_OnEmbedWindow(context->pubSub, context, &e);
456#ifdef WITH_PROGRESS_BAR
457 if (wfc->taskBarList)
459 ShowWindow(wfc->hwnd, SW_SHOWMINIMIZED);
460 wfc->taskBarList->lpVtbl->SetProgressState(wfc->taskBarList, wfc->hwnd, TBPF_INDETERMINATE);
463 UpdateWindow(wfc->hwnd);
464 context->update->BeginPaint = wf_begin_paint;
465 context->update->DesktopResize = wf_desktop_resize;
466 context->update->EndPaint = wf_end_paint;
467 wf_register_pointer(context->graphics);
469 wfc->floatbar = wf_floatbar_new(wfc, wfc->hInstance,
474static void wf_post_disconnect(freerdp* instance)
478 if (!instance || !instance->context)
481 wfc = (wfContext*)instance->context;
482 free(wfc->window_title);
485static CREDUI_INFOW wfUiInfo = {
sizeof(CREDUI_INFOW), NULL, L
"Enter your credentials",
486 L
"Remote Desktop Security", NULL };
488static BOOL wf_authenticate_ex(freerdp* instance,
char** username,
char** password,
char** domain,
489 rdp_auth_reason reason)
495 WCHAR UserNameW[CREDUI_MAX_USERNAME_LENGTH + 1] = { 0 };
496 WCHAR UserW[CREDUI_MAX_USERNAME_LENGTH + 1] = { 0 };
497 WCHAR DomainW[CREDUI_MAX_DOMAIN_TARGET_LENGTH + 1] = { 0 };
498 WCHAR PasswordW[CREDUI_MAX_PASSWORD_LENGTH + 1] = { 0 };
500 WINPR_ASSERT(instance);
501 WINPR_ASSERT(instance->context);
502 WINPR_ASSERT(instance->context->settings);
504 wfc = (wfContext*)instance->context;
507 WINPR_ASSERT(username);
508 WINPR_ASSERT(domain);
509 WINPR_ASSERT(password);
511 const WCHAR auth[] = L
"Target credentials requested";
512 const WCHAR authPin[] = L
"PIN requested";
513 const WCHAR gwAuth[] = L
"Gateway credentials requested";
514 const WCHAR* titleW = auth;
517 dwFlags = CREDUI_FLAGS_DO_NOT_PERSIST | CREDUI_FLAGS_EXCLUDE_CERTIFICATES |
518 CREDUI_FLAGS_USERNAME_TARGET_CREDENTIALS;
525 if ((*username) && (*password))
528 case AUTH_SMARTCARD_PIN:
529 dwFlags &= ~CREDUI_FLAGS_USERNAME_TARGET_CREDENTIALS;
530 dwFlags |= CREDUI_FLAGS_PASSWORD_ONLY_OK | CREDUI_FLAGS_KEEP_USERNAME;
535 *username = _strdup(
"PIN");
548 (void)ConvertUtf8ToWChar(*username, UserNameW, ARRAYSIZE(UserNameW));
549 (void)ConvertUtf8ToWChar(*username, UserW, ARRAYSIZE(UserW));
553 (void)ConvertUtf8ToWChar(*password, PasswordW, ARRAYSIZE(PasswordW));
556 (void)ConvertUtf8ToWChar(*domain, DomainW, ARRAYSIZE(DomainW));
558 if (_wcsnlen(PasswordW, ARRAYSIZE(PasswordW)) == 0)
560 if (!wfc->isConsole &&
562 WLog_ERR(TAG,
"Flag for stdin read present but stdin is redirected; using GUI");
563 if (wfc->isConsole &&
565 status = CredUICmdLinePromptForCredentialsW(titleW, NULL, 0, UserNameW,
566 ARRAYSIZE(UserNameW), PasswordW,
567 ARRAYSIZE(PasswordW), &fSave, dwFlags);
569 status = CredUIPromptForCredentialsW(&wfUiInfo, titleW, NULL, 0, UserNameW,
570 ARRAYSIZE(UserNameW), PasswordW,
571 ARRAYSIZE(PasswordW), &fSave, dwFlags);
572 if (status != NO_ERROR)
574 WLog_ERR(TAG,
"CredUIPromptForCredentials unexpected status: 0x%08lX", status);
578 if ((dwFlags & CREDUI_FLAGS_KEEP_USERNAME) == 0)
580 status = CredUIParseUserNameW(UserNameW, UserW, ARRAYSIZE(UserW), DomainW,
582 if (status != NO_ERROR)
584 CHAR User[CREDUI_MAX_USERNAME_LENGTH + 1] = { 0 };
585 CHAR UserName[CREDUI_MAX_USERNAME_LENGTH + 1] = { 0 };
586 CHAR Domain[CREDUI_MAX_DOMAIN_TARGET_LENGTH + 1] = { 0 };
588 (void)ConvertWCharNToUtf8(UserNameW, ARRAYSIZE(UserNameW), UserName,
589 ARRAYSIZE(UserName));
590 (void)ConvertWCharNToUtf8(UserW, ARRAYSIZE(UserW), User, ARRAYSIZE(User));
591 (void)ConvertWCharNToUtf8(DomainW, ARRAYSIZE(DomainW), Domain, ARRAYSIZE(Domain));
592 WLog_ERR(TAG,
"Failed to parse UserName: %s into User: %s Domain: %s", UserName,
599 *username = ConvertWCharNToUtf8Alloc(UserW, ARRAYSIZE(UserW), NULL);
602 WLog_ERR(TAG,
"ConvertWCharNToUtf8Alloc failed", status);
606 if (_wcsnlen(DomainW, ARRAYSIZE(DomainW)) > 0)
607 *domain = ConvertWCharNToUtf8Alloc(DomainW, ARRAYSIZE(DomainW), NULL);
609 *domain = _strdup(
"\0");
614 WLog_ERR(TAG,
"strdup failed", status);
618 *password = ConvertWCharNToUtf8Alloc(PasswordW, ARRAYSIZE(PasswordW), NULL);
629static WCHAR* wf_format_text(
const WCHAR* fmt, ...)
633 WCHAR* buffer = NULL;
640 rc = _vsnwprintf(buffer, size, fmt, ap);
645 if ((
size_t)rc < size)
648 size = (size_t)rc + 1;
649 tmp = realloc(buffer, size *
sizeof(WCHAR));
661#ifdef WITH_WINDOWS_CERT_STORE
669static void wf_report_error(
char* wszMessage, DWORD dwErrCode)
671 LPSTR pwszMsgBuf = NULL;
673 if (NULL != wszMessage && 0 != *wszMessage)
675 WLog_ERR(TAG,
"%s", wszMessage);
678 FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
683 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
693 if (NULL != pwszMsgBuf)
695 WLog_ERR(TAG,
"Error: 0x%08x (%d) %s", dwErrCode, dwErrCode, pwszMsgBuf);
696 LocalFree(pwszMsgBuf);
700 WLog_ERR(TAG,
"Error: 0x%08x (%d)", dwErrCode, dwErrCode);
704static DWORD wf_is_x509_certificate_trusted(
const char* common_name,
const char* subject,
705 const char* issuer,
const char* fingerprint)
707 HRESULT hr = CRYPT_E_NOT_FOUND;
709 DWORD dwChainFlags = CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT;
711 HCERTCHAINENGINE hChainEngine = NULL;
712 PCCERT_CHAIN_CONTEXT pChainContext = NULL;
714 CERT_ENHKEY_USAGE EnhkeyUsage = { 0 };
715 CERT_USAGE_MATCH CertUsage = { 0 };
716 CERT_CHAIN_PARA ChainPara = { 0 };
717 CERT_CHAIN_POLICY_PARA ChainPolicy = { 0 };
718 CERT_CHAIN_POLICY_STATUS PolicyStatus = { 0 };
719 CERT_CHAIN_ENGINE_CONFIG EngineConfig = { 0 };
721 DWORD derPubKeyLen = WINPR_ASSERTING_INT_CAST(uint32_t, strlen(fingerprint));
722 char* derPubKey = calloc(derPubKeyLen,
sizeof(
char));
723 if (NULL == derPubKey)
725 WLog_ERR(TAG,
"Could not allocate derPubKey");
732 if (!CryptStringToBinaryA(fingerprint, 0, CRYPT_STRING_BASE64HEADER, derPubKey, &derPubKeyLen,
735 WLog_ERR(TAG,
"CryptStringToBinary failed. Err: %d", GetLastError());
742 EnhkeyUsage.cUsageIdentifier = 0;
743 EnhkeyUsage.rgpszUsageIdentifier = NULL;
745 CertUsage.dwType = USAGE_MATCH_TYPE_AND;
746 CertUsage.Usage = EnhkeyUsage;
748 ChainPara.cbSize =
sizeof(ChainPara);
749 ChainPara.RequestedUsage = CertUsage;
751 ChainPolicy.cbSize =
sizeof(ChainPolicy);
753 PolicyStatus.cbSize =
sizeof(PolicyStatus);
755 EngineConfig.cbSize =
sizeof(EngineConfig);
756 EngineConfig.dwUrlRetrievalTimeout = 0;
758 pCert = CertCreateCertificateContext(X509_ASN_ENCODING, derPubKey, derPubKeyLen);
761 WLog_ERR(TAG,
"FAILED: Certificate could not be parsed.");
765 dwChainFlags |= CERT_CHAIN_ENABLE_PEER_TRUST;
774 if (!CertCreateCertificateChainEngine(&EngineConfig, &hChainEngine))
776 hr = HRESULT_FROM_WIN32(GetLastError());
783 if (!CertGetCertificateChain(hChainEngine,
794 hr = HRESULT_FROM_WIN32(GetLastError());
801 if (!CertVerifyCertificateChainPolicy(CERT_CHAIN_POLICY_BASE,
806 hr = HRESULT_FROM_WIN32(GetLastError());
810 if (PolicyStatus.dwError != S_OK)
812 wf_report_error(
"CertVerifyCertificateChainPolicy: Chain Status", PolicyStatus.dwError);
813 hr = PolicyStatus.dwError;
819 if (PolicyStatus.dwError == CRYPT_E_NO_REVOCATION_CHECK ||
820 PolicyStatus.dwError == CRYPT_E_REVOCATION_OFFLINE)
828 WLog_INFO(TAG,
"CertVerifyCertificateChainPolicy succeeded for %s (%s) issued by %s",
829 common_name, subject, issuer);
836 WLog_INFO(TAG,
"CertVerifyCertificateChainPolicy failed for %s (%s) issued by %s",
837 common_name, subject, issuer);
838 wf_report_error(NULL, hr);
843 if (NULL != pChainContext)
845 CertFreeCertificateChain(pChainContext);
848 if (NULL != hChainEngine)
850 CertFreeCertificateChainEngine(hChainEngine);
855 CertFreeCertificateContext(pCert);
862static DWORD wf_cli_verify_certificate_ex(freerdp* instance,
const char* host, UINT16 port,
863 const char* common_name,
const char* subject,
864 const char* issuer,
const char* fingerprint, DWORD flags)
866#ifdef WITH_WINDOWS_CERT_STORE
867 if (flags & VERIFY_CERT_FLAG_FP_IS_PEM && !(flags & VERIFY_CERT_FLAG_MISMATCH))
869 if (wf_is_x509_certificate_trusted(common_name, subject, issuer, fingerprint) == S_OK)
876 return client_cli_verify_certificate_ex(instance, host, port, common_name, subject, issuer,
880static DWORD wf_verify_certificate_ex(freerdp* instance,
const char* host, UINT16 port,
881 const char* common_name,
const char* subject,
882 const char* issuer,
const char* fingerprint, DWORD flags)
888#ifdef WITH_WINDOWS_CERT_STORE
889 if (flags & VERIFY_CERT_FLAG_FP_IS_PEM && !(flags & VERIFY_CERT_FLAG_MISMATCH))
891 if (wf_is_x509_certificate_trusted(common_name, subject, issuer, fingerprint) == S_OK)
898 buffer = wf_format_text(
899 L
"Certificate details:\n"
900 L
"\tCommonName: %S\n"
903 L
"\tThumbprint: %S\n"
904 L
"\tHostMismatch: %S\n"
906 L
"The above X.509 certificate could not be verified, possibly because you do not have "
907 L
"the CA certificate in your certificate store, or the certificate has expired. "
908 L
"Please look at the OpenSSL documentation on how to add a private CA to the store.\n"
910 L
"YES\tAccept permanently\n"
911 L
"NO\tAccept for this session only\n"
912 L
"CANCEL\tAbort connection\n",
913 common_name, subject, issuer, fingerprint,
914 flags & VERIFY_CERT_FLAG_MISMATCH ?
"Yes" :
"No");
915 caption = wf_format_text(L
"Verify certificate for %S:%hu", host, port);
917 WINPR_UNUSED(instance);
919 if (!buffer || !caption)
922 what = MessageBoxW(NULL, buffer, caption, MB_YESNOCANCEL);
940static DWORD wf_verify_changed_certificate_ex(freerdp* instance,
const char* host, UINT16 port,
941 const char* common_name,
const char* subject,
942 const char* issuer,
const char* new_fingerprint,
943 const char* old_subject,
const char* old_issuer,
944 const char* old_fingerprint, DWORD flags)
950 buffer = wf_format_text(
951 L
"New Certificate details:\n"
952 L
"\tCommonName: %S\n"
955 L
"\tThumbprint: %S\n"
956 L
"\tHostMismatch: %S\n"
958 L
"Old Certificate details:\n"
962 L
"The above X.509 certificate could not be verified, possibly because you do not have "
963 L
"the CA certificate in your certificate store, or the certificate has expired. "
964 L
"Please look at the OpenSSL documentation on how to add a private CA to the store.\n"
966 L
"YES\tAccept permanently\n"
967 L
"NO\tAccept for this session only\n"
968 L
"CANCEL\tAbort connection\n",
969 common_name, subject, issuer, new_fingerprint,
970 flags & VERIFY_CERT_FLAG_MISMATCH ?
"Yes" :
"No", old_subject, old_issuer, old_fingerprint);
971 caption = wf_format_text(L
"Verify certificate change for %S:%hu", host, port);
973 WINPR_UNUSED(instance);
974 if (!buffer || !caption)
977 what = MessageBoxW(NULL, buffer, caption, MB_YESNOCANCEL);
995static BOOL wf_present_gateway_message(freerdp* instance, UINT32 type, BOOL isDisplayMandatory,
996 BOOL isConsentMandatory,
size_t length,
const WCHAR* message)
998 if (!isDisplayMandatory && !isConsentMandatory)
1002 if (type == GATEWAY_MESSAGE_CONSENT && isConsentMandatory)
1007 msg = wf_format_text(L
"%.*s\n\nI understand and agree to the terms of this policy", length,
1009 mbRes = MessageBoxW(NULL, msg, L
"Consent Message", MB_YESNO);
1016 return client_cli_present_gateway_message(instance, type, isDisplayMandatory,
1017 isConsentMandatory, length, message);
1022static DWORD WINAPI wf_client_thread(LPVOID lpParam)
1027 BOOL msg_ret = FALSE;
1031 freerdp* instance = (freerdp*)lpParam;
1032 WINPR_ASSERT(instance);
1034 if (!freerdp_connect(instance))
1037 rdpContext* context = instance->context;
1038 WINPR_ASSERT(context);
1040 wfContext* wfc = (wfContext*)instance->context;
1043 rdpChannels* channels = context->channels;
1044 WINPR_ASSERT(channels);
1046 rdpSettings* settings = context->settings;
1047 WINPR_ASSERT(settings);
1049 while (!freerdp_shall_disconnect_context(instance->context))
1051 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = { 0 };
1054 if (freerdp_focus_required(instance))
1056 wf_event_focus_in(wfc);
1057 wf_event_focus_in(wfc);
1061 DWORD tmp = freerdp_get_event_handles(context, &handles[nCount], 64 - nCount);
1065 WLog_ERR(TAG,
"freerdp_get_event_handles failed");
1072 DWORD status = MsgWaitForMultipleObjectsEx(nCount, handles, 5 * 1000, QS_ALLINPUT,
1073 MWMO_ALERTABLE | MWMO_INPUTAVAILABLE);
1074 if (status == WAIT_FAILED)
1076 WLog_ERR(TAG,
"wfreerdp_run: WaitForMultipleObjects failed: 0x%08lX", GetLastError());
1081 if (!freerdp_check_event_handles(context))
1083 if (client_auto_reconnect(instance))
1086 WLog_ERR(TAG,
"Failed to check FreeRDP file descriptor");
1091 if (freerdp_shall_disconnect_context(instance->context))
1096 while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
1098 msg_ret = GetMessage(&msg, NULL, 0, 0);
1102 if ((msg.message == WM_SETFOCUS) && (msg.lParam == 1))
1104 PostMessage(wfc->hwnd, WM_SETFOCUS, 0, 0);
1106 else if ((msg.message == WM_KILLFOCUS) && (msg.lParam == 1))
1108 PostMessage(wfc->hwnd, WM_KILLFOCUS, 0, 0);
1112 switch (msg.message)
1116 width = LOWORD(msg.lParam);
1117 height = HIWORD(msg.lParam);
1118 SetWindowPos(wfc->hwnd, HWND_TOP, 0, 0, width, height, SWP_FRAMECHANGED);
1121 case WM_FREERDP_SHOWWINDOW:
1123 ShowWindow(wfc->hwnd, SW_NORMAL);
1130 if ((msg_ret == 0) || (msg_ret == -1))
1136 TranslateMessage(&msg);
1137 DispatchMessage(&msg);
1145 freerdp_disconnect(instance);
1148 error = freerdp_get_last_error(instance->context);
1149 WLog_DBG(TAG,
"Main thread exited with %" PRIu32, error);
1154static DWORD WINAPI wf_keyboard_thread(LPVOID lpParam)
1160 wfc = (wfContext*)lpParam;
1161 WINPR_ASSERT(NULL != wfc);
1162 hook_handle = SetWindowsHookEx(WH_KEYBOARD_LL, wf_ll_kbd_proc, wfc->hInstance, 0);
1166 while ((status = GetMessage(&msg, NULL, 0, 0)) != 0)
1170 WLog_ERR(TAG,
"keyboard thread error getting message");
1175 TranslateMessage(&msg);
1176 DispatchMessage(&msg);
1180 UnhookWindowsHookEx(hook_handle);
1184 WLog_ERR(TAG,
"failed to install keyboard hook");
1187 WLog_DBG(TAG,
"Keyboard thread exited.");
1192int freerdp_client_set_window_size(wfContext* wfc,
int width,
int height)
1194 WLog_DBG(TAG,
"freerdp_client_set_window_size %d, %d", width, height);
1196 if ((width != wfc->client_width) || (height != wfc->client_height))
1198 PostThreadMessage(wfc->mainThreadId, WM_SIZE, SIZE_RESTORED,
1199 ((UINT)height << 16) | (UINT)width);
1205void wf_size_scrollbars(wfContext* wfc, UINT32 client_width, UINT32 client_height)
1207 const rdpSettings* settings;
1210 settings = wfc->common.context.settings;
1211 WINPR_ASSERT(settings);
1213 if (wfc->disablewindowtracking)
1217 wfc->disablewindowtracking = TRUE;
1222 wfc->xCurrentScroll = 0;
1223 wfc->yCurrentScroll = 0;
1225 if (wfc->xScrollVisible || wfc->yScrollVisible)
1227 if (ShowScrollBar(wfc->hwnd, SB_BOTH, FALSE))
1229 wfc->xScrollVisible = FALSE;
1230 wfc->yScrollVisible = FALSE;
1237 BOOL horiz = wfc->xScrollVisible;
1238 BOOL vert = wfc->yScrollVisible;
1247 settings, FreeRDP_DesktopWidth) )
1259 settings, FreeRDP_DesktopHeight) )
1264 if (horiz == vert && (horiz != wfc->xScrollVisible && vert != wfc->yScrollVisible))
1266 if (ShowScrollBar(wfc->hwnd, SB_BOTH, horiz))
1268 wfc->xScrollVisible = horiz;
1269 wfc->yScrollVisible = vert;
1273 if (horiz != wfc->xScrollVisible)
1275 if (ShowScrollBar(wfc->hwnd, SB_HORZ, horiz))
1277 wfc->xScrollVisible = horiz;
1281 if (vert != wfc->yScrollVisible)
1283 if (ShowScrollBar(wfc->hwnd, SB_VERT, vert))
1285 wfc->yScrollVisible = vert;
1296 wfc->xCurrentScroll = MIN(wfc->xCurrentScroll, wfc->xMaxScroll);
1297 si.cbSize =
sizeof(si);
1298 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
1299 si.nMin = wfc->xMinScroll;
1301 si.nPage = client_width;
1302 si.nPos = wfc->xCurrentScroll;
1303 SetScrollInfo(wfc->hwnd, SB_HORZ, &si, TRUE);
1311 wfc->yMaxScroll = MAX(
1313 wfc->yCurrentScroll = MIN(wfc->yCurrentScroll, wfc->yMaxScroll);
1314 si.cbSize =
sizeof(si);
1315 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
1316 si.nMin = wfc->yMinScroll;
1318 si.nPage = client_height;
1319 si.nPos = wfc->yCurrentScroll;
1320 SetScrollInfo(wfc->hwnd, SB_VERT, &si, TRUE);
1324 wfc->disablewindowtracking = FALSE;
1325 wf_update_canvas_diff(wfc);
1328static BOOL wfreerdp_client_global_init(
void)
1332 WSAStartup(0x101, &wsaData);
1334 freerdp_register_addin_provider(freerdp_channels_load_static_addin_entry, 0);
1339static void wfreerdp_client_global_uninit(
void)
1344static BOOL wfreerdp_client_new(freerdp* instance, rdpContext* context)
1346 wfContext* wfc = (wfContext*)context;
1352 wfc->isConsole = wf_has_console();
1354 if (!(wfreerdp_client_global_init()))
1357 WINPR_ASSERT(instance);
1358 instance->PreConnect = wf_pre_connect;
1359 instance->PostConnect = wf_post_connect;
1360 instance->PostDisconnect = wf_post_disconnect;
1361 instance->AuthenticateEx = wf_authenticate_ex;
1363#ifdef WITH_WINDOWS_CERT_STORE
1368 if (!wfc->isConsole)
1370 instance->VerifyCertificateEx = wf_verify_certificate_ex;
1371 instance->VerifyChangedCertificateEx = wf_verify_changed_certificate_ex;
1372 instance->PresentGatewayMessage = wf_present_gateway_message;
1375#ifdef WITH_PROGRESS_BAR
1376 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1377 CoCreateInstance(&CLSID_TaskbarList, NULL, CLSCTX_ALL, &IID_ITaskbarList3,
1378 (
void**)&wfc->taskBarList);
1384static void wfreerdp_client_free(freerdp* instance, rdpContext* context)
1386 WINPR_UNUSED(instance);
1390#ifdef WITH_PROGRESS_BAR
1395static int wfreerdp_client_start(rdpContext* context)
1397 wfContext* wfc = (wfContext*)context;
1399 WINPR_ASSERT(context);
1400 WINPR_ASSERT(context->settings);
1402 freerdp* instance = context->instance;
1403 WINPR_ASSERT(instance);
1405 rdpSettings* settings = context->settings;
1406 WINPR_ASSERT(settings);
1408 HINSTANCE hInstance = GetModuleHandle(NULL);
1413 wfc->hWndParent = hWndParent;
1417 typedef UINT(WINAPI * GetDpiForWindow_t)(HWND hwnd);
1418 typedef BOOL(WINAPI * SetProcessDPIAware_t)(void);
1420 HMODULE
module = GetModuleHandle(_T("User32"));
1423 GetDpiForWindow_t pGetDpiForWindow =
1424 GetProcAddressAs(module,
"GetDpiForWindow", GetDpiForWindow_t);
1425 SetProcessDPIAware_t pSetProcessDPIAware =
1426 GetProcAddressAs(module,
"SetProcessDPIAware", SetProcessDPIAware_t);
1427 if (pGetDpiForWindow && pSetProcessDPIAware)
1429 const UINT dpiAwareness = pGetDpiForWindow(hWndParent);
1430 if (dpiAwareness != USER_DEFAULT_SCREEN_DPI)
1431 pSetProcessDPIAware();
1433 FreeLibrary(module);
1441 wfc->systemMenuInsertPosition = 6;
1443 wfc->hInstance = hInstance;
1444 wfc->cursor = LoadCursor(NULL, IDC_ARROW);
1445 wfc->icon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1));
1446 wfc->wndClassName = _tcsdup(_T(
"FreeRDP"));
1447 wfc->wndClass.cbSize =
sizeof(WNDCLASSEX);
1448 wfc->wndClass.style = CS_HREDRAW | CS_VREDRAW;
1449 wfc->wndClass.lpfnWndProc = wf_event_proc;
1450 wfc->wndClass.cbClsExtra = 0;
1451 wfc->wndClass.cbWndExtra = 0;
1452 wfc->wndClass.hCursor = NULL;
1453 wfc->wndClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
1454 wfc->wndClass.lpszMenuName = NULL;
1455 wfc->wndClass.lpszClassName = wfc->wndClassName;
1456 wfc->wndClass.hInstance = hInstance;
1457 wfc->wndClass.hIcon = wfc->icon;
1458 wfc->wndClass.hIconSm = wfc->icon;
1459 RegisterClassEx(&(wfc->wndClass));
1460 wfc->keyboardThread =
1461 CreateThread(NULL, 0, wf_keyboard_thread, (
void*)wfc, 0, &wfc->keyboardThreadId);
1463 if (!wfc->keyboardThread)
1466 wfc->common.thread =
1467 CreateThread(NULL, 0, wf_client_thread, (
void*)instance, 0, &wfc->mainThreadId);
1469 if (!wfc->common.thread)
1475static int wfreerdp_client_stop(rdpContext* context)
1478 wfContext* wfc = (wfContext*)context;
1481 PostThreadMessage(wfc->mainThreadId, WM_QUIT, 0, 0);
1482 rc = freerdp_client_common_stop(context);
1483 wfc->mainThreadId = 0;
1485 if (wfc->keyboardThread)
1487 PostThreadMessage(wfc->keyboardThreadId, WM_QUIT, 0, 0);
1488 (void)WaitForSingleObject(wfc->keyboardThread, INFINITE);
1489 (void)CloseHandle(wfc->keyboardThread);
1490 wfc->keyboardThread = NULL;
1491 wfc->keyboardThreadId = 0;
1497int RdpClientEntry(RDP_CLIENT_ENTRY_POINTS* pEntryPoints)
1499 pEntryPoints->Version = 1;
1500 pEntryPoints->Size =
sizeof(RDP_CLIENT_ENTRY_POINTS_V1);
1501 pEntryPoints->GlobalInit = wfreerdp_client_global_init;
1502 pEntryPoints->GlobalUninit = wfreerdp_client_global_uninit;
1503 pEntryPoints->ContextSize =
sizeof(wfContext);
1504 pEntryPoints->ClientNew = wfreerdp_client_new;
1505 pEntryPoints->ClientFree = wfreerdp_client_free;
1506 pEntryPoints->ClientStart = wfreerdp_client_start;
1507 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.