2012-08-18 22:34:24 +04:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Client
|
|
|
|
* FreeRDP Windows Server
|
|
|
|
*
|
|
|
|
* Copyright 2012 Corey Clayton <can.of.tuna@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2012-08-24 00:35:54 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2012-08-18 22:34:24 +04:00
|
|
|
#include <stdlib.h>
|
2012-08-24 00:35:54 +04:00
|
|
|
|
2012-08-18 22:34:24 +04:00
|
|
|
#include <winpr/tchar.h>
|
|
|
|
#include <winpr/windows.h>
|
|
|
|
|
|
|
|
#include "wf_info.h"
|
2012-09-07 20:01:36 +04:00
|
|
|
#include "wf_update.h"
|
2012-08-18 22:34:24 +04:00
|
|
|
#include "wf_mirage.h"
|
|
|
|
|
2012-09-03 01:09:36 +04:00
|
|
|
static wfInfo* wfInfoInstance = NULL;
|
|
|
|
|
2012-09-03 02:04:41 +04:00
|
|
|
int wf_info_lock(wfInfo* wfi)
|
2012-08-29 03:28:04 +04:00
|
|
|
{
|
|
|
|
DWORD dRes;
|
|
|
|
|
2012-09-03 02:04:41 +04:00
|
|
|
dRes = WaitForSingleObject(wfi->mutex, INFINITE);
|
2012-08-29 03:28:04 +04:00
|
|
|
|
2012-09-03 01:09:36 +04:00
|
|
|
switch (dRes)
|
2012-08-29 03:28:04 +04:00
|
|
|
{
|
2012-09-03 01:09:36 +04:00
|
|
|
case WAIT_ABANDONED:
|
2012-09-03 02:04:41 +04:00
|
|
|
case WAIT_OBJECT_0:
|
|
|
|
return TRUE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WAIT_TIMEOUT:
|
|
|
|
return FALSE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WAIT_FAILED:
|
|
|
|
printf("wf_info_lock failed with 0x%08X\n", GetLastError());
|
|
|
|
return -1;
|
2012-09-03 01:09:36 +04:00
|
|
|
break;
|
2012-09-03 02:04:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int wf_info_try_lock(wfInfo* wfi, DWORD dwMilliseconds)
|
|
|
|
{
|
|
|
|
DWORD dRes;
|
|
|
|
|
|
|
|
dRes = WaitForSingleObject(wfi->mutex, dwMilliseconds);
|
2012-09-03 01:09:36 +04:00
|
|
|
|
2012-09-03 02:04:41 +04:00
|
|
|
switch (dRes)
|
|
|
|
{
|
|
|
|
case WAIT_ABANDONED:
|
2012-09-03 01:09:36 +04:00
|
|
|
case WAIT_OBJECT_0:
|
2012-09-03 02:04:41 +04:00
|
|
|
return TRUE;
|
2012-09-03 01:09:36 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WAIT_TIMEOUT:
|
2012-09-03 02:04:41 +04:00
|
|
|
return FALSE;
|
2012-09-03 01:09:36 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WAIT_FAILED:
|
2012-09-03 02:04:41 +04:00
|
|
|
printf("wf_info_try_lock failed with 0x%08X\n", GetLastError());
|
2012-09-03 01:09:36 +04:00
|
|
|
return -1;
|
|
|
|
break;
|
2012-08-29 03:28:04 +04:00
|
|
|
}
|
2012-09-03 01:09:36 +04:00
|
|
|
|
2012-09-03 02:04:41 +04:00
|
|
|
return -1;
|
2012-08-29 03:28:04 +04:00
|
|
|
}
|
|
|
|
|
2012-09-03 01:09:36 +04:00
|
|
|
int wf_info_unlock(wfInfo* wfi)
|
2012-08-18 22:34:24 +04:00
|
|
|
{
|
2012-09-03 01:09:36 +04:00
|
|
|
if (ReleaseMutex(wfi->mutex) == 0)
|
2012-09-03 02:04:41 +04:00
|
|
|
{
|
|
|
|
printf("wf_info_unlock failed with 0x%08X\n", GetLastError());
|
|
|
|
return -1;
|
|
|
|
}
|
2012-09-03 01:09:36 +04:00
|
|
|
|
2012-09-03 02:04:41 +04:00
|
|
|
return TRUE;
|
2012-08-29 03:28:04 +04:00
|
|
|
}
|
|
|
|
|
2012-09-03 02:04:41 +04:00
|
|
|
wfInfo* wf_info_init()
|
2012-08-18 22:34:24 +04:00
|
|
|
{
|
2012-09-03 02:04:41 +04:00
|
|
|
wfInfo* wfi;
|
|
|
|
|
|
|
|
wfi = (wfInfo*) malloc(sizeof(wfInfo));
|
|
|
|
ZeroMemory(wfi, sizeof(wfInfo));
|
2012-08-18 22:34:24 +04:00
|
|
|
|
2012-09-03 02:04:41 +04:00
|
|
|
if (wfi != NULL)
|
|
|
|
{
|
2012-09-07 20:01:36 +04:00
|
|
|
HKEY hKey;
|
|
|
|
LONG status;
|
|
|
|
DWORD dwType;
|
|
|
|
DWORD dwSize;
|
|
|
|
DWORD dwValue;
|
|
|
|
|
2012-08-24 01:10:22 +04:00
|
|
|
wfi->mutex = CreateMutex(NULL, FALSE, NULL);
|
2012-08-18 22:34:24 +04:00
|
|
|
|
2012-08-24 01:10:22 +04:00
|
|
|
if (wfi->mutex == NULL)
|
2012-08-18 22:34:24 +04:00
|
|
|
{
|
|
|
|
_tprintf(_T("CreateMutex error: %d\n"), GetLastError());
|
|
|
|
}
|
2012-09-03 08:51:02 +04:00
|
|
|
|
2012-09-08 00:48:30 +04:00
|
|
|
wfi->updateEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
|
|
|
|
|
|
|
|
wfi->updateSemaphore = CreateSemaphore(NULL, 0, 32, NULL);
|
2012-09-06 04:03:07 +04:00
|
|
|
|
2012-09-07 20:01:36 +04:00
|
|
|
wfi->updateThread = CreateThread(NULL, 0, wf_update_thread, wfi, CREATE_SUSPENDED, NULL);
|
|
|
|
|
|
|
|
if (!wfi->updateThread)
|
|
|
|
{
|
|
|
|
_tprintf(_T("Failed to create update thread\n"));
|
|
|
|
}
|
|
|
|
|
2012-09-06 04:03:07 +04:00
|
|
|
wfi->peers = (wfPeerContext**) malloc(sizeof(wfPeerContext*) * 32);
|
2012-09-07 20:01:36 +04:00
|
|
|
|
|
|
|
wfi->framesPerSecond = 24;
|
|
|
|
|
|
|
|
status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Software\\FreeRDP\\Server"), 0, KEY_READ | KEY_WOW64_64KEY, &hKey);
|
|
|
|
|
|
|
|
if (status == ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
if (RegQueryValueEx(hKey, _T("FramesPerSecond"), NULL, &dwType, (BYTE*) &dwValue, &dwSize) == ERROR_SUCCESS)
|
|
|
|
wfi->framesPerSecond = dwValue;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
RegCloseKey(hKey);
|
2012-08-18 22:34:24 +04:00
|
|
|
}
|
|
|
|
|
2012-08-24 01:10:22 +04:00
|
|
|
return wfi;
|
2012-08-18 22:34:24 +04:00
|
|
|
}
|
|
|
|
|
2012-09-03 01:09:36 +04:00
|
|
|
wfInfo* wf_info_get_instance()
|
|
|
|
{
|
|
|
|
if (wfInfoInstance == NULL)
|
2012-09-03 02:04:41 +04:00
|
|
|
wfInfoInstance = wf_info_init();
|
2012-09-03 01:09:36 +04:00
|
|
|
|
|
|
|
return wfInfoInstance;
|
|
|
|
}
|
|
|
|
|
2012-09-07 08:01:16 +04:00
|
|
|
void wf_update_encoder_init(wfInfo* wfi)
|
|
|
|
{
|
|
|
|
wfi->rfx_context = rfx_context_new();
|
|
|
|
wfi->rfx_context->mode = RLGR3;
|
|
|
|
wfi->rfx_context->width = wfi->width;
|
|
|
|
wfi->rfx_context->height = wfi->height;
|
|
|
|
rfx_context_set_pixel_format(wfi->rfx_context, RDP_PIXEL_FORMAT_B8G8R8A8);
|
|
|
|
wfi->s = stream_new(0xFFFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
void wf_update_encoder_uninit(wfInfo* wfi)
|
|
|
|
{
|
|
|
|
if (wfi->rfx_context != NULL)
|
|
|
|
{
|
|
|
|
rfx_context_free(wfi->rfx_context);
|
|
|
|
wfi->rfx_context = NULL;
|
|
|
|
stream_free(wfi->s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void wf_update_encoder_reinit(wfInfo* wfi)
|
|
|
|
{
|
|
|
|
wf_update_encoder_uninit(wfi);
|
|
|
|
wf_update_encoder_init(wfi);
|
|
|
|
}
|
|
|
|
|
2012-09-08 00:48:30 +04:00
|
|
|
void wf_mirror_driver_init(wfInfo* wfi)
|
|
|
|
{
|
|
|
|
wf_mirror_driver_find_display_device(wfi);
|
|
|
|
wf_mirror_driver_display_device_attach(wfi, 1);
|
|
|
|
wf_mirror_driver_update(wfi, FALSE);
|
|
|
|
wf_mirror_driver_map_memory(wfi);
|
|
|
|
}
|
|
|
|
|
|
|
|
void wf_mirror_driver_uninit(wfInfo* wfi)
|
|
|
|
{
|
|
|
|
wf_mirror_driver_cleanup(wfi);
|
|
|
|
wf_mirror_driver_display_device_attach(wfi, 0);
|
|
|
|
wf_mirror_driver_update(wfi, 1);
|
|
|
|
}
|
|
|
|
|
2012-09-06 04:03:07 +04:00
|
|
|
void wf_info_peer_register(wfInfo* wfi, wfPeerContext* context)
|
2012-08-18 22:34:24 +04:00
|
|
|
{
|
2012-09-03 03:54:17 +04:00
|
|
|
if (wf_info_lock(wfi) > 0)
|
2012-08-18 22:34:24 +04:00
|
|
|
{
|
2012-09-06 04:03:07 +04:00
|
|
|
context->info = wfi;
|
2012-09-08 00:48:30 +04:00
|
|
|
context->updateEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
2012-09-06 04:03:07 +04:00
|
|
|
|
|
|
|
if (wfi->peerCount < 1)
|
2012-09-08 00:48:30 +04:00
|
|
|
wf_mirror_driver_init(wfi);
|
2012-08-18 22:34:24 +04:00
|
|
|
|
2012-09-07 08:01:16 +04:00
|
|
|
wf_update_encoder_reinit(wfi);
|
2012-09-06 04:03:07 +04:00
|
|
|
wfi->peers[wfi->peerCount++] = context;
|
2012-08-18 22:34:24 +04:00
|
|
|
|
2012-09-03 03:54:17 +04:00
|
|
|
wf_info_unlock(wfi);
|
2012-08-18 22:34:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-06 04:03:07 +04:00
|
|
|
void wf_info_peer_unregister(wfInfo* wfi, wfPeerContext* context)
|
2012-08-18 22:34:24 +04:00
|
|
|
{
|
2012-09-03 03:54:17 +04:00
|
|
|
if (wf_info_lock(wfi) > 0)
|
2012-08-18 22:34:24 +04:00
|
|
|
{
|
2012-09-08 00:48:30 +04:00
|
|
|
if (wfi->peerCount == 1)
|
|
|
|
wf_mirror_driver_uninit(wfi);
|
2012-09-06 04:03:07 +04:00
|
|
|
|
2012-09-08 00:48:30 +04:00
|
|
|
wfi->peers[--(wfi->peerCount)] = NULL;
|
2012-08-19 00:42:17 +04:00
|
|
|
|
2012-09-03 03:54:17 +04:00
|
|
|
wf_info_unlock(wfi);
|
|
|
|
}
|
2012-08-19 01:49:52 +04:00
|
|
|
}
|
|
|
|
|
2012-08-24 01:10:22 +04:00
|
|
|
BOOL wf_info_have_updates(wfInfo* wfi)
|
2012-08-19 01:49:52 +04:00
|
|
|
{
|
2012-08-24 01:10:22 +04:00
|
|
|
if (wfi->nextUpdate == wfi->lastUpdate)
|
2012-08-30 23:50:46 +04:00
|
|
|
return FALSE;
|
2012-08-19 01:49:52 +04:00
|
|
|
|
2012-08-30 23:50:46 +04:00
|
|
|
return TRUE;
|
2012-08-19 01:49:52 +04:00
|
|
|
}
|
|
|
|
|
2012-08-24 01:10:22 +04:00
|
|
|
void wf_info_update_changes(wfInfo* wfi)
|
2012-08-19 01:49:52 +04:00
|
|
|
{
|
|
|
|
GETCHANGESBUF* buf;
|
|
|
|
|
2012-08-24 01:10:22 +04:00
|
|
|
buf = (GETCHANGESBUF*) wfi->changeBuffer;
|
|
|
|
wfi->nextUpdate = buf->buffer->counter;
|
2012-08-19 01:49:52 +04:00
|
|
|
}
|
|
|
|
|
2012-08-24 01:10:22 +04:00
|
|
|
void wf_info_find_invalid_region(wfInfo* wfi)
|
2012-08-19 01:49:52 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
GETCHANGESBUF* buf;
|
|
|
|
|
2012-08-24 01:10:22 +04:00
|
|
|
buf = (GETCHANGESBUF*) wfi->changeBuffer;
|
2012-08-20 21:47:44 +04:00
|
|
|
|
2012-08-24 01:10:22 +04:00
|
|
|
for (i = wfi->lastUpdate; i != wfi->nextUpdate; i = (i + 1) % MAXCHANGES_BUF)
|
2012-08-19 01:49:52 +04:00
|
|
|
{
|
2012-09-03 03:54:17 +04:00
|
|
|
UnionRect(&wfi->invalid, &wfi->invalid, &buf->buffer->pointrect[i].rect);
|
2012-08-19 01:49:52 +04:00
|
|
|
}
|
|
|
|
|
2012-09-03 03:54:17 +04:00
|
|
|
if (wfi->invalid.left < 0)
|
|
|
|
wfi->invalid.left = 0;
|
2012-08-19 03:46:42 +04:00
|
|
|
|
2012-09-03 03:54:17 +04:00
|
|
|
if (wfi->invalid.top < 0)
|
|
|
|
wfi->invalid.top = 0;
|
2012-08-24 01:10:22 +04:00
|
|
|
|
2012-09-03 03:54:17 +04:00
|
|
|
if (wfi->invalid.right >= wfi->width)
|
|
|
|
wfi->invalid.right = wfi->width - 1;
|
2012-08-24 01:10:22 +04:00
|
|
|
|
2012-09-03 03:54:17 +04:00
|
|
|
if (wfi->invalid.bottom >= wfi->height)
|
|
|
|
wfi->invalid.bottom = wfi->height - 1;
|
2012-08-20 22:36:59 +04:00
|
|
|
}
|
|
|
|
|
2012-08-24 01:10:22 +04:00
|
|
|
void wf_info_clear_invalid_region(wfInfo* wfi)
|
2012-08-20 22:36:59 +04:00
|
|
|
{
|
2012-08-24 01:10:22 +04:00
|
|
|
wfi->lastUpdate = wfi->nextUpdate;
|
2012-09-03 03:54:17 +04:00
|
|
|
SetRectEmpty(&wfi->invalid);
|
2012-08-20 22:36:59 +04:00
|
|
|
}
|
|
|
|
|
2012-08-24 01:10:22 +04:00
|
|
|
BOOL wf_info_have_invalid_region(wfInfo* wfi)
|
2012-08-20 22:36:59 +04:00
|
|
|
{
|
2012-09-03 03:54:17 +04:00
|
|
|
return IsRectEmpty(&wfi->invalid);
|
2012-08-21 02:19:17 +04:00
|
|
|
}
|