FreeRDP
wf_interface.c
1 
21 #include <freerdp/config.h>
22 
23 #include <winpr/tchar.h>
24 #include <winpr/windows.h>
25 #include <winpr/winsock.h>
26 #include <winpr/assert.h>
27 
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>
34 
35 #include "wf_peer.h"
36 #include "wf_settings.h"
37 #include "wf_info.h"
38 
39 #include "wf_interface.h"
40 
41 #include <freerdp/log.h>
42 #define TAG SERVER_TAG("windows")
43 
44 #define SERVER_KEY "Software\\" FREERDP_VENDOR_STRING "\\" FREERDP_PRODUCT_STRING "\\Server"
45 
46 static cbCallback cbEvent = NULL;
47 
48 int get_screen_info(int id, _TCHAR* name, size_t length, int* width, int* height, int* bpp)
49 {
50  DISPLAY_DEVICE dd = { 0 };
51 
52  dd.cb = sizeof(DISPLAY_DEVICE);
53 
54  if (EnumDisplayDevices(NULL, id, &dd, 0) != 0)
55  {
56  HDC dc;
57 
58  if (name != NULL)
59  _stprintf_s(name, length, _T("%s (%s)"), dd.DeviceName, dd.DeviceString);
60 
61  dc = CreateDC(dd.DeviceName, NULL, NULL, NULL);
62  *width = GetDeviceCaps(dc, HORZRES);
63  *height = GetDeviceCaps(dc, VERTRES);
64  *bpp = GetDeviceCaps(dc, BITSPIXEL);
65  // ReleaseDC(NULL, dc);
66  DeleteDC(dc);
67  }
68  else
69  {
70  return 0;
71  }
72 
73  return 1;
74 }
75 
76 void set_screen_id(int id)
77 {
78  wfInfo* wfi;
79 
80  wfi = wf_info_get_instance();
81  if (!wfi)
82  return;
83  wfi->screenID = id;
84 
85  return;
86 }
87 
88 static DWORD WINAPI wf_server_main_loop(LPVOID lpParam)
89 {
90  freerdp_listener* instance;
91  wfInfo* wfi;
92 
93  wfi = wf_info_get_instance();
94  if (!wfi)
95  {
96  WLog_ERR(TAG, "Failed to get instance");
97  return -1;
98  }
99 
100  wfi->force_all_disconnect = FALSE;
101 
102  instance = (freerdp_listener*)lpParam;
103  WINPR_ASSERT(instance);
104  WINPR_ASSERT(instance->GetEventHandles);
105  WINPR_ASSERT(instance->CheckFileDescriptor);
106 
107  while (wfi->force_all_disconnect == FALSE)
108  {
109  DWORD status;
110  HANDLE handles[MAXIMUM_WAIT_OBJECTS] = { 0 };
111  DWORD count = instance->GetEventHandles(instance, handles, ARRAYSIZE(handles));
112 
113  if (count == 0)
114  {
115  WLog_ERR(TAG, "Failed to get FreeRDP file descriptor");
116  break;
117  }
118 
119  status = WaitForMultipleObjects(count, handles, FALSE, INFINITE);
120  if (status == WAIT_FAILED)
121  {
122  WLog_ERR(TAG, "WaitForMultipleObjects failed");
123  break;
124  }
125 
126  if (instance->CheckFileDescriptor(instance) != TRUE)
127  {
128  WLog_ERR(TAG, "Failed to check FreeRDP file descriptor");
129  break;
130  }
131  }
132 
133  WLog_INFO(TAG, "wf_server_main_loop terminating");
134  instance->Close(instance);
135 
136  return 0;
137 }
138 
139 BOOL wfreerdp_server_start(wfServer* server)
140 {
141  freerdp_listener* instance;
142 
143  server->instance = freerdp_listener_new();
144  server->instance->PeerAccepted = wf_peer_accepted;
145  instance = server->instance;
146 
147  wf_settings_read_dword(HKEY_LOCAL_MACHINE, SERVER_KEY, _T("DefaultPort"), &server->port);
148 
149  if (!instance->Open(instance, NULL, (UINT16)server->port))
150  return FALSE;
151 
152  if (!(server->thread = CreateThread(NULL, 0, wf_server_main_loop, (void*)instance, 0, NULL)))
153  return FALSE;
154 
155  return TRUE;
156 }
157 
158 BOOL wfreerdp_server_stop(wfServer* server)
159 {
160  wfInfo* wfi;
161 
162  wfi = wf_info_get_instance();
163  if (!wfi)
164  return FALSE;
165  WLog_INFO(TAG, "Stopping server");
166  wfi->force_all_disconnect = TRUE;
167  server->instance->Close(server->instance);
168  return TRUE;
169 }
170 
171 wfServer* wfreerdp_server_new()
172 {
173  WSADATA wsaData;
174  wfServer* server;
175 
176  if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
177  return NULL;
178 
179  server = (wfServer*)calloc(1, sizeof(wfServer));
180 
181  if (server)
182  {
183  server->port = 3389;
184  }
185 
186  WTSRegisterWtsApiFunctionTable(FreeRDP_InitWtsApi());
187 
188  cbEvent = NULL;
189 
190  return server;
191 }
192 
193 void wfreerdp_server_free(wfServer* server)
194 {
195  free(server);
196 
197  WSACleanup();
198 }
199 
200 BOOL wfreerdp_server_is_running(wfServer* server)
201 {
202  DWORD tStatus;
203  BOOL bRet;
204 
205  bRet = GetExitCodeThread(server->thread, &tStatus);
206  if (bRet == 0)
207  {
208  WLog_ERR(TAG, "Error in call to GetExitCodeThread");
209  return FALSE;
210  }
211 
212  if (tStatus == STILL_ACTIVE)
213  return TRUE;
214  return FALSE;
215 }
216 
217 UINT32 wfreerdp_server_num_peers()
218 {
219  wfInfo* wfi;
220 
221  wfi = wf_info_get_instance();
222  if (!wfi)
223  return -1;
224  return wfi->peerCount;
225 }
226 
227 UINT32 wfreerdp_server_get_peer_hostname(int pId, wchar_t* dstStr)
228 {
229  wfInfo* wfi;
230  freerdp_peer* peer;
231 
232  wfi = wf_info_get_instance();
233  if (!wfi)
234  return 0;
235  peer = wfi->peers[pId];
236 
237  if (peer)
238  {
239  UINT32 sLen;
240 
241  sLen = strnlen_s(peer->hostname, 50);
242  swprintf(dstStr, 50, L"%hs", peer->hostname);
243  return sLen;
244  }
245  else
246  {
247  WLog_WARN(TAG, "nonexistent peer id=%d", pId);
248  return 0;
249  }
250 }
251 
252 BOOL wfreerdp_server_peer_is_local(int pId)
253 {
254  wfInfo* wfi;
255  freerdp_peer* peer;
256 
257  wfi = wf_info_get_instance();
258  if (!wfi)
259  return FALSE;
260  peer = wfi->peers[pId];
261 
262  if (peer)
263  {
264  return peer->local;
265  }
266  else
267  {
268  return FALSE;
269  }
270 }
271 
272 BOOL wfreerdp_server_peer_is_connected(int pId)
273 {
274  wfInfo* wfi;
275  freerdp_peer* peer;
276 
277  wfi = wf_info_get_instance();
278  if (!wfi)
279  return FALSE;
280  peer = wfi->peers[pId];
281 
282  if (peer)
283  {
284  return peer->connected;
285  }
286  else
287  {
288  return FALSE;
289  }
290 }
291 
292 BOOL wfreerdp_server_peer_is_activated(int pId)
293 {
294  wfInfo* wfi;
295  freerdp_peer* peer;
296 
297  wfi = wf_info_get_instance();
298  if (!wfi)
299  return FALSE;
300  peer = wfi->peers[pId];
301 
302  if (peer)
303  {
304  return peer->activated;
305  }
306  else
307  {
308  return FALSE;
309  }
310 }
311 
312 BOOL wfreerdp_server_peer_is_authenticated(int pId)
313 {
314  wfInfo* wfi;
315  freerdp_peer* peer;
316 
317  wfi = wf_info_get_instance();
318  if (!wfi)
319  return FALSE;
320  peer = wfi->peers[pId];
321 
322  if (peer)
323  {
324  return peer->authenticated;
325  }
326  else
327  {
328  return FALSE;
329  }
330 }
331 
332 void wfreerdp_server_register_callback_event(cbCallback cb)
333 {
334  cbEvent = cb;
335 }
336 
337 void wfreerdp_server_peer_callback_event(int pId, UINT32 eType)
338 {
339  if (cbEvent)
340  cbEvent(pId, eType);
341 }