[host] add critical section for m_cursorInfo

This commit is contained in:
Geoffrey McRae 2018-05-24 16:43:16 +10:00
parent a4600e7278
commit eef18dd655
2 changed files with 43 additions and 37 deletions

View File

@ -97,6 +97,7 @@ bool Service::Initialize(ICapture * captureDevice)
// Create the cursor thread // Create the cursor thread
m_cursorThread = CreateThread(NULL, 0, _CursorThread, this, 0, NULL); m_cursorThread = CreateThread(NULL, 0, _CursorThread, this, 0, NULL);
m_cursorEvent = CreateEvent (NULL, FALSE, FALSE, L"CursorEvent"); m_cursorEvent = CreateEvent (NULL, FALSE, FALSE, L"CursorEvent");
InitializeCriticalSection(&m_cursorCS);
// update everything except for the hostID // update everything except for the hostID
memcpy(m_shmHeader->magic, KVMFR_HEADER_MAGIC, sizeof(KVMFR_HEADER_MAGIC)); memcpy(m_shmHeader->magic, KVMFR_HEADER_MAGIC, sizeof(KVMFR_HEADER_MAGIC));
@ -221,8 +222,19 @@ bool Service::Process()
bool cursorOnly = false; bool cursorOnly = false;
for(int i = 0; i < 2; ++i) for(int i = 0; i < 2; ++i)
{ {
CursorInfo ci;
GrabStatus status = m_capture->GrabFrame(frame, ci);
if (ci.hasPos || ci.hasShape)
{
EnterCriticalSection(&m_cursorCS);
memcpy(&m_cursorInfo, &ci, sizeof(ci));
LeaveCriticalSection(&m_cursorCS);
SetEvent(m_cursorEvent);
}
// capture a frame of data // capture a frame of data
switch (m_capture->GrabFrame(frame, m_cursorInfo)) switch (status)
{ {
case GRAB_STATUS_OK: case GRAB_STATUS_OK:
ok = true; ok = true;
@ -272,9 +284,6 @@ bool Service::Process()
return false; return false;
} }
if (m_cursorInfo.hasPos || m_cursorInfo.hasShape)
SetEvent(m_cursorEvent);
if (!cursorOnly) if (!cursorOnly)
{ {
KVMFRFrame * fi = &m_shmHeader->frame; KVMFRFrame * fi = &m_shmHeader->frame;
@ -315,48 +324,44 @@ DWORD Service::CursorThread()
return 0; return 0;
} }
struct CursorInfo ci; EnterCriticalSection(&m_cursorCS);
memcpy(&ci, &m_cursorInfo, sizeof(ci)); if (m_cursorInfo.hasPos)
ZeroMemory(&m_cursorInfo, sizeof(m_cursorInfo));
if (ci.hasPos)
{ {
ci.hasPos = false; m_cursorInfo.hasPos = false;
// tell the client where the cursor is // tell the client where the cursor is
cursor->flags |= KVMFR_CURSOR_FLAG_POS; cursor->flags |= KVMFR_CURSOR_FLAG_POS;
cursor->x = ci.x; cursor->x = m_cursorInfo.x;
cursor->y = ci.y; cursor->y = m_cursorInfo.y;
if (ci.visible) if (m_cursorInfo.visible)
cursor->flags |= KVMFR_CURSOR_FLAG_VISIBLE; cursor->flags |= KVMFR_CURSOR_FLAG_VISIBLE;
else else
cursor->flags &= ~KVMFR_CURSOR_FLAG_VISIBLE; cursor->flags &= ~KVMFR_CURSOR_FLAG_VISIBLE;
} }
if (ci.hasShape) if (m_cursorInfo.hasShape)
{ {
ci.hasShape = false; m_cursorInfo.hasShape = false;
if (ci.dataSize > m_cursorDataSize) if (m_cursorInfo.dataSize > m_cursorDataSize)
{
DEBUG_ERROR("Cursor size exceeds allocated space"); DEBUG_ERROR("Cursor size exceeds allocated space");
cursor->flags = 0; else
continue; {
// give the client the new cursor shape
cursor->flags |= KVMFR_CURSOR_FLAG_SHAPE;
++cursor->version;
cursor->type = m_cursorInfo.type;
cursor->width = m_cursorInfo.w;
cursor->height = m_cursorInfo.h;
cursor->pitch = m_cursorInfo.pitch;
cursor->dataPos = m_cursorOffset;
memcpy(m_cursorData, m_cursorInfo.shape, m_cursorInfo.dataSize);
} }
// give the client the new cursor shape
cursor->flags |= KVMFR_CURSOR_FLAG_SHAPE;
++cursor->version;
cursor->type = ci.type;
cursor->width = ci.w;
cursor->height = ci.h;
cursor->pitch = ci.pitch;
cursor->dataPos = m_cursorOffset;
memcpy(m_cursorData, ci.shape, ci.dataSize);
} }
LeaveCriticalSection(&m_cursorCS);
cursor->flags |= KVMFR_CURSOR_FLAG_UPDATE; cursor->flags |= KVMFR_CURSOR_FLAG_UPDATE;
} }

View File

@ -65,10 +65,11 @@ private:
static DWORD WINAPI _CursorThread(LPVOID lpParameter) { return ((Service *)lpParameter)->CursorThread(); } static DWORD WINAPI _CursorThread(LPVOID lpParameter) { return ((Service *)lpParameter)->CursorThread(); }
DWORD CursorThread(); DWORD CursorThread();
HANDLE m_cursorThread; HANDLE m_cursorThread;
HANDLE m_cursorEvent; HANDLE m_cursorEvent;
CursorInfo m_cursorInfo; CursorInfo m_cursorInfo;
size_t m_cursorDataSize; CRITICAL_SECTION m_cursorCS;
uint8_t * m_cursorData; size_t m_cursorDataSize;
uint64_t m_cursorOffset; uint8_t * m_cursorData;
uint64_t m_cursorOffset;
}; };