21 #include <freerdp/config.h>
23 #include <winpr/tchar.h>
24 #include <winpr/windows.h>
25 #include <winpr/winsock.h>
26 #include <winpr/assert.h>
28 #include <freerdp/freerdp.h>
29 #include <freerdp/listener.h>
30 #include <freerdp/constants.h>
31 #include <freerdp/channels/wtsvc.h>
32 #include <freerdp/channels/channels.h>
33 #include <freerdp/build-config.h>
36 #include "wf_settings.h"
39 #include "wf_interface.h"
41 #include <freerdp/log.h>
42 #define TAG SERVER_TAG("windows")
44 #define SERVER_KEY "Software\\" FREERDP_VENDOR_STRING "\\" FREERDP_PRODUCT_STRING "\\Server"
46 static cbCallback cbEvent = NULL;
48 int get_screen_info(
int id, _TCHAR* name,
size_t length,
int* width,
int* height,
int* bpp)
50 DISPLAY_DEVICE dd = { 0 };
52 dd.cb =
sizeof(DISPLAY_DEVICE);
54 if (EnumDisplayDevices(NULL,
id, &dd, 0) != 0)
59 _stprintf_s(name, length, _T(
"%s (%s)"), dd.DeviceName, dd.DeviceString);
61 dc = CreateDC(dd.DeviceName, NULL, NULL, NULL);
62 *width = GetDeviceCaps(dc, HORZRES);
63 *height = GetDeviceCaps(dc, VERTRES);
64 *bpp = GetDeviceCaps(dc, BITSPIXEL);
76 void set_screen_id(
int id)
80 wfi = wf_info_get_instance();
88 static DWORD WINAPI wf_server_main_loop(LPVOID lpParam)
90 freerdp_listener* instance;
93 wfi = wf_info_get_instance();
96 WLog_ERR(TAG,
"Failed to get instance");
100 wfi->force_all_disconnect = FALSE;
102 instance = (freerdp_listener*)lpParam;
103 WINPR_ASSERT(instance);
104 WINPR_ASSERT(instance->GetEventHandles);
105 WINPR_ASSERT(instance->CheckFileDescriptor);
107 while (wfi->force_all_disconnect == FALSE)
110 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = { 0 };
111 DWORD count = instance->GetEventHandles(instance, handles, ARRAYSIZE(handles));
115 WLog_ERR(TAG,
"Failed to get FreeRDP file descriptor");
119 status = WaitForMultipleObjects(count, handles, FALSE, INFINITE);
120 if (status == WAIT_FAILED)
122 WLog_ERR(TAG,
"WaitForMultipleObjects failed");
126 if (instance->CheckFileDescriptor(instance) != TRUE)
128 WLog_ERR(TAG,
"Failed to check FreeRDP file descriptor");
133 WLog_INFO(TAG,
"wf_server_main_loop terminating");
134 instance->Close(instance);
139 BOOL wfreerdp_server_start(wfServer* server)
141 freerdp_listener* instance;
143 server->instance = freerdp_listener_new();
144 server->instance->PeerAccepted = wf_peer_accepted;
145 instance = server->instance;
147 wf_settings_read_dword(HKEY_LOCAL_MACHINE, SERVER_KEY, _T(
"DefaultPort"), &server->port);
149 if (!instance->Open(instance, NULL, (UINT16)server->port))
152 if (!(server->thread = CreateThread(NULL, 0, wf_server_main_loop, (
void*)instance, 0, NULL)))
158 BOOL wfreerdp_server_stop(wfServer* server)
162 wfi = wf_info_get_instance();
165 WLog_INFO(TAG,
"Stopping server");
166 wfi->force_all_disconnect = TRUE;
167 server->instance->Close(server->instance);
171 wfServer* wfreerdp_server_new()
176 if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
179 server = (wfServer*)calloc(1,
sizeof(wfServer));
186 WTSRegisterWtsApiFunctionTable(FreeRDP_InitWtsApi());
193 void wfreerdp_server_free(wfServer* server)
200 BOOL wfreerdp_server_is_running(wfServer* server)
205 bRet = GetExitCodeThread(server->thread, &tStatus);
208 WLog_ERR(TAG,
"Error in call to GetExitCodeThread");
212 if (tStatus == STILL_ACTIVE)
217 UINT32 wfreerdp_server_num_peers()
221 wfi = wf_info_get_instance();
224 return wfi->peerCount;
227 UINT32 wfreerdp_server_get_peer_hostname(
int pId,
wchar_t* dstStr)
232 wfi = wf_info_get_instance();
235 peer = wfi->peers[pId];
241 sLen = strnlen_s(peer->hostname, 50);
242 swprintf(dstStr, 50, L
"%hs", peer->hostname);
247 WLog_WARN(TAG,
"nonexistent peer id=%d", pId);
252 BOOL wfreerdp_server_peer_is_local(
int pId)
257 wfi = wf_info_get_instance();
260 peer = wfi->peers[pId];
272 BOOL wfreerdp_server_peer_is_connected(
int pId)
277 wfi = wf_info_get_instance();
280 peer = wfi->peers[pId];
284 return peer->connected;
292 BOOL wfreerdp_server_peer_is_activated(
int pId)
297 wfi = wf_info_get_instance();
300 peer = wfi->peers[pId];
304 return peer->activated;
312 BOOL wfreerdp_server_peer_is_authenticated(
int pId)
317 wfi = wf_info_get_instance();
320 peer = wfi->peers[pId];
324 return peer->authenticated;
332 void wfreerdp_server_register_callback_event(cbCallback cb)
337 void wfreerdp_server_peer_callback_event(
int pId, UINT32 eType)