FreeRDP
Loading...
Searching...
No Matches
wf_info.c
1
20#include <freerdp/config.h>
21
22#include <stdlib.h>
23
24#include <freerdp/build-config.h>
25
26#include <winpr/tchar.h>
27#include <winpr/windows.h>
28
29#include "wf_info.h"
30#include "wf_update.h"
31#include "wf_mirage.h"
32#include "wf_dxgi.h"
33
34#include <freerdp/log.h>
35#define TAG SERVER_TAG("windows")
36
37#define SERVER_KEY "Software\\" FREERDP_VENDOR_STRING "\\" FREERDP_PRODUCT_STRING "\\Server"
38
39static wfInfo* wfInfoInstance = nullptr;
40static int _IDcount = 0;
41
42BOOL wf_info_lock(wfInfo* wfi)
43{
44 DWORD dRes;
45 dRes = WaitForSingleObject(wfi->mutex, INFINITE);
46
47 switch (dRes)
48 {
49 case WAIT_ABANDONED:
50 case WAIT_OBJECT_0:
51 return TRUE;
52
53 case WAIT_TIMEOUT:
54 return FALSE;
55
56 case WAIT_FAILED:
57 WLog_ERR(TAG, "wf_info_lock failed with 0x%08lX", GetLastError());
58 return FALSE;
59 }
60
61 return FALSE;
62}
63
64BOOL wf_info_try_lock(wfInfo* wfi, DWORD dwMilliseconds)
65{
66 DWORD dRes;
67 dRes = WaitForSingleObject(wfi->mutex, dwMilliseconds);
68
69 switch (dRes)
70 {
71 case WAIT_ABANDONED:
72 case WAIT_OBJECT_0:
73 return TRUE;
74
75 case WAIT_TIMEOUT:
76 return FALSE;
77
78 case WAIT_FAILED:
79 WLog_ERR(TAG, "wf_info_try_lock failed with 0x%08lX", GetLastError());
80 return FALSE;
81 }
82
83 return FALSE;
84}
85
86BOOL wf_info_unlock(wfInfo* wfi)
87{
88 if (!ReleaseMutex(wfi->mutex))
89 {
90 WLog_ERR(TAG, "wf_info_unlock failed with 0x%08lX", GetLastError());
91 return FALSE;
92 }
93
94 return TRUE;
95}
96
97wfInfo* wf_info_init()
98{
99 wfInfo* wfi;
100 wfi = (wfInfo*)calloc(1, sizeof(wfInfo));
101
102 if (wfi != nullptr)
103 {
104 HKEY hKey;
105 LONG status;
106 DWORD dwType;
107 DWORD dwSize;
108 DWORD dwValue;
109 wfi->mutex = CreateMutex(nullptr, FALSE, nullptr);
110
111 if (wfi->mutex == nullptr)
112 {
113 WLog_ERR(TAG, "CreateMutex error: %lu", GetLastError());
114 free(wfi);
115 return nullptr;
116 }
117
118 wfi->updateSemaphore = CreateSemaphore(nullptr, 0, 32, nullptr);
119
120 if (!wfi->updateSemaphore)
121 {
122 WLog_ERR(TAG, "CreateSemaphore error: %lu", GetLastError());
123 (void)CloseHandle(wfi->mutex);
124 free(wfi);
125 return nullptr;
126 }
127
128 wfi->updateThread =
129 CreateThread(nullptr, 0, wf_update_thread, wfi, CREATE_SUSPENDED, nullptr);
130
131 if (!wfi->updateThread)
132 {
133 WLog_ERR(TAG, "Failed to create update thread");
134 (void)CloseHandle(wfi->mutex);
135 (void)CloseHandle(wfi->updateSemaphore);
136 free(wfi);
137 return nullptr;
138 }
139
140 wfi->peers =
141 (freerdp_peer**)calloc(FREERDP_SERVER_WIN_INFO_MAXPEERS, sizeof(freerdp_peer*));
142
143 if (!wfi->peers)
144 {
145 WLog_ERR(TAG, "Failed to allocate memory for peer");
146 (void)CloseHandle(wfi->mutex);
147 (void)CloseHandle(wfi->updateSemaphore);
148 (void)CloseHandle(wfi->updateThread);
149 free(wfi);
150 return nullptr;
151 }
152
153 // Set FPS
154 wfi->framesPerSecond = FREERDP_SERVER_WIN_INFO_DEFAULT_FPS;
155 status =
156 RegOpenKeyExA(HKEY_LOCAL_MACHINE, SERVER_KEY, 0, KEY_READ | KEY_WOW64_64KEY, &hKey);
157
158 if (status == ERROR_SUCCESS)
159 {
160 if (RegQueryValueEx(hKey, _T("FramesPerSecond"), nullptr, &dwType, (BYTE*)&dwValue,
161 &dwSize) == ERROR_SUCCESS)
162 wfi->framesPerSecond = dwValue;
163 }
164
165 RegCloseKey(hKey);
166 // Set input toggle
167 wfi->input_disabled = FALSE;
168 status =
169 RegOpenKeyExA(HKEY_LOCAL_MACHINE, SERVER_KEY, 0, KEY_READ | KEY_WOW64_64KEY, &hKey);
170
171 if (status == ERROR_SUCCESS)
172 {
173 if (RegQueryValueEx(hKey, _T("DisableInput"), nullptr, &dwType, (BYTE*)&dwValue,
174 &dwSize) == ERROR_SUCCESS)
175 {
176 if (dwValue != 0)
177 wfi->input_disabled = TRUE;
178 }
179 }
180
181 RegCloseKey(hKey);
182 }
183
184 return wfi;
185}
186
187wfInfo* wf_info_get_instance()
188{
189 if (wfInfoInstance == nullptr)
190 wfInfoInstance = wf_info_init();
191
192 return wfInfoInstance;
193}
194
195BOOL wf_info_peer_register(wfInfo* wfi, wfPeerContext* context)
196{
197 int peerId = 0;
198
199 if (!wfi || !context)
200 return FALSE;
201
202 if (!wf_info_lock(wfi))
203 return FALSE;
204
205 if (wfi->peerCount == FREERDP_SERVER_WIN_INFO_MAXPEERS)
206 goto fail_peer_count;
207
208 context->info = wfi;
209
210 if (!(context->updateEvent = CreateEvent(nullptr, TRUE, FALSE, nullptr)))
211 goto fail_update_event;
212
213 // get the offset of the top left corner of selected screen
214 EnumDisplayMonitors(nullptr, nullptr, wf_info_monEnumCB, 0);
215 _IDcount = 0;
216#ifdef WITH_DXGI_1_2
217
218 if (wfi->peerCount == 0)
219 if (wf_dxgi_init(wfi) != 0)
220 goto fail_driver_init;
221
222#else
223
224 if (!wf_mirror_driver_activate(wfi))
225 goto fail_driver_init;
226
227#endif
228
229 // look through the array of peers until an empty slot
230 for (int i = 0; i < FREERDP_SERVER_WIN_INFO_MAXPEERS; ++i)
231 {
232 // empty index will be our peer id
233 if (wfi->peers[i] == nullptr)
234 {
235 peerId = i;
236 break;
237 }
238 }
239
240 wfi->peers[peerId] = ((rdpContext*)context)->peer;
241 wfi->peers[peerId]->pId = peerId;
242 wfi->peerCount++;
243 WLog_INFO(TAG, "Registering Peer: id=%d #=%d", peerId, wfi->peerCount);
244 wf_info_unlock(wfi);
245 wfreerdp_server_peer_callback_event(peerId, FREERDP_SERVER_WIN_SRV_CALLBACK_EVENT_CONNECT);
246 return TRUE;
247fail_driver_init:
248 (void)CloseHandle(context->updateEvent);
249 context->updateEvent = nullptr;
250fail_update_event:
251fail_peer_count:
252 context->socketClose = TRUE;
253 wf_info_unlock(wfi);
254 return FALSE;
255}
256
257void wf_info_peer_unregister(wfInfo* wfi, wfPeerContext* context)
258{
259 if (wf_info_lock(wfi))
260 {
261 int peerId;
262 peerId = ((rdpContext*)context)->peer->pId;
263 wfi->peers[peerId] = nullptr;
264 wfi->peerCount--;
265 (void)CloseHandle(context->updateEvent);
266 WLog_INFO(TAG, "Unregistering Peer: id=%d, #=%d", peerId, wfi->peerCount);
267#ifdef WITH_DXGI_1_2
268
269 if (wfi->peerCount == 0)
270 wf_dxgi_cleanup(wfi);
271
272#endif
273 wf_info_unlock(wfi);
274 wfreerdp_server_peer_callback_event(peerId,
275 FREERDP_SERVER_WIN_SRV_CALLBACK_EVENT_DISCONNECT);
276 }
277}
278
279BOOL wf_info_have_updates(wfInfo* wfi)
280{
281#ifdef WITH_DXGI_1_2
282
283 if (wfi->framesWaiting == 0)
284 return FALSE;
285
286#else
287
288 if (wfi->nextUpdate == wfi->lastUpdate)
289 return FALSE;
290
291#endif
292 return TRUE;
293}
294
295void wf_info_update_changes(wfInfo* wfi)
296{
297#ifdef WITH_DXGI_1_2
298 wf_dxgi_nextFrame(wfi, wfi->framesPerSecond * 1000);
299#else
300 GETCHANGESBUF* buf;
301 buf = (GETCHANGESBUF*)wfi->changeBuffer;
302 wfi->nextUpdate = buf->buffer->counter;
303#endif
304}
305
306void wf_info_find_invalid_region(wfInfo* wfi)
307{
308#ifdef WITH_DXGI_1_2
309 wf_dxgi_getInvalidRegion(&wfi->invalid);
310#else
311 GETCHANGESBUF* buf;
312 buf = (GETCHANGESBUF*)wfi->changeBuffer;
313
314 for (ULONG i = wfi->lastUpdate; i != wfi->nextUpdate; i = (i + 1) % MAXCHANGES_BUF)
315 {
316 LPRECT lpR = &buf->buffer->pointrect[i].rect;
317
318 // need to make sure we only get updates from the selected screen
319 if ((lpR->left >= wfi->servscreen_xoffset) &&
320 (lpR->right <= (wfi->servscreen_xoffset + wfi->servscreen_width)) &&
321 (lpR->top >= wfi->servscreen_yoffset) &&
322 (lpR->bottom <= (wfi->servscreen_yoffset + wfi->servscreen_height)))
323 {
324 UnionRect(&wfi->invalid, &wfi->invalid, lpR);
325 }
326 else
327 {
328 continue;
329 }
330 }
331
332#endif
333
334 if (wfi->invalid.left < 0)
335 wfi->invalid.left = 0;
336
337 if (wfi->invalid.top < 0)
338 wfi->invalid.top = 0;
339
340 if (wfi->invalid.right >= wfi->servscreen_width)
341 wfi->invalid.right = wfi->servscreen_width - 1;
342
343 if (wfi->invalid.bottom >= wfi->servscreen_height)
344 wfi->invalid.bottom = wfi->servscreen_height - 1;
345
346 // WLog_DBG(TAG, "invalid region: (%"PRId32", %"PRId32"), (%"PRId32", %"PRId32")",
347 // wfi->invalid.left, wfi->invalid.top, wfi->invalid.right, wfi->invalid.bottom);
348}
349
350void wf_info_clear_invalid_region(wfInfo* wfi)
351{
352 wfi->lastUpdate = wfi->nextUpdate;
353 SetRectEmpty(&wfi->invalid);
354}
355
356void wf_info_invalidate_full_screen(wfInfo* wfi)
357{
358 SetRect(&wfi->invalid, 0, 0, wfi->servscreen_width, wfi->servscreen_height);
359}
360
361BOOL wf_info_have_invalid_region(wfInfo* wfi)
362{
363 return IsRectEmpty(&wfi->invalid);
364}
365
366void wf_info_getScreenData(wfInfo* wfi, long* width, long* height, BYTE** pBits, int* pitch)
367{
368 *width = (wfi->invalid.right - wfi->invalid.left);
369 *height = (wfi->invalid.bottom - wfi->invalid.top);
370#ifdef WITH_DXGI_1_2
371 wf_dxgi_getPixelData(wfi, pBits, pitch, &wfi->invalid);
372#else
373 {
374 long offset;
375 GETCHANGESBUF* changes;
376 changes = (GETCHANGESBUF*)wfi->changeBuffer;
377 *width += 1;
378 *height += 1;
379 offset = (4 * wfi->invalid.left) + (wfi->invalid.top * wfi->virtscreen_width * 4);
380 *pBits = ((BYTE*)(changes->Userbuffer)) + offset;
381 *pitch = wfi->virtscreen_width * 4;
382 }
383#endif
384}
385
386BOOL CALLBACK wf_info_monEnumCB(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor,
387 LPARAM dwData)
388{
389 wfInfo* wfi;
390 wfi = wf_info_get_instance();
391
392 if (!wfi)
393 return FALSE;
394
395 if (_IDcount == wfi->screenID)
396 {
397 wfi->servscreen_xoffset = lprcMonitor->left;
398 wfi->servscreen_yoffset = lprcMonitor->top;
399 }
400
401 _IDcount++;
402 return TRUE;
403}