2017-10-31 16:53:06 +00:00
|
|
|
/*
|
2017-12-03 15:31:16 +00:00
|
|
|
Looking Glass - KVM FrameRelay (KVMFR) Client
|
2017-10-31 16:53:06 +00:00
|
|
|
Copyright (C) 2017 Geoffrey McRae <geoff@hostfission.com>
|
2017-12-11 17:30:47 +00:00
|
|
|
https://looking-glass.hostfission.com
|
2017-10-31 16:53:06 +00:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation; either version 2 of the License, or (at your option) any later
|
|
|
|
version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
|
|
|
PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
|
|
|
Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2017-10-31 12:22:55 +00:00
|
|
|
#include "Service.h"
|
|
|
|
#include "IVSHMEM.h"
|
2017-10-31 12:21:05 +00:00
|
|
|
|
2017-12-14 19:11:41 +00:00
|
|
|
#include "common/debug.h"
|
|
|
|
#include "common/KVMFR.h"
|
2017-10-31 12:21:05 +00:00
|
|
|
|
2017-12-21 02:48:57 +00:00
|
|
|
#include "Util.h"
|
2017-10-31 12:21:05 +00:00
|
|
|
#include "CaptureFactory.h"
|
|
|
|
|
|
|
|
Service * Service::m_instance = NULL;
|
|
|
|
|
|
|
|
Service::Service() :
|
|
|
|
m_initialized(false),
|
2017-12-07 19:24:17 +00:00
|
|
|
m_memory(NULL),
|
2017-12-13 15:22:41 +00:00
|
|
|
m_timer(NULL),
|
2017-10-31 12:21:05 +00:00
|
|
|
m_capture(NULL),
|
2017-12-15 23:04:56 +00:00
|
|
|
m_shmHeader(NULL),
|
2017-12-15 00:30:25 +00:00
|
|
|
m_frameIndex(0),
|
|
|
|
m_cursorDataSize(0),
|
2017-12-19 13:58:42 +00:00
|
|
|
m_cursorData(NULL)
|
2017-10-31 12:21:05 +00:00
|
|
|
{
|
2017-12-16 17:50:04 +00:00
|
|
|
m_consoleSessionID = WTSGetActiveConsoleSessionId();
|
2017-10-31 12:21:05 +00:00
|
|
|
m_ivshmem = IVSHMEM::Get();
|
|
|
|
}
|
|
|
|
|
|
|
|
Service::~Service()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-11-16 09:53:22 +00:00
|
|
|
bool Service::Initialize(ICapture * captureDevice)
|
2017-10-31 12:21:05 +00:00
|
|
|
{
|
|
|
|
if (m_initialized)
|
|
|
|
DeInitialize();
|
|
|
|
|
2017-11-16 09:53:22 +00:00
|
|
|
m_capture = captureDevice;
|
2017-10-31 12:21:05 +00:00
|
|
|
if (!m_ivshmem->Initialize())
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("IVSHMEM failed to initalize");
|
|
|
|
DeInitialize();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-03 15:31:16 +00:00
|
|
|
if (m_ivshmem->GetSize() < sizeof(KVMFRHeader))
|
2017-10-31 12:21:05 +00:00
|
|
|
{
|
2017-12-03 15:31:16 +00:00
|
|
|
DEBUG_ERROR("Shared memory is not large enough for the KVMFRHeader");
|
2017-10-31 12:21:05 +00:00
|
|
|
DeInitialize();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-07 19:24:17 +00:00
|
|
|
m_memory = static_cast<uint8_t*>(m_ivshmem->GetMemory());
|
|
|
|
if (!m_memory)
|
2017-10-31 12:21:05 +00:00
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to get IVSHMEM memory");
|
|
|
|
DeInitialize();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-07 19:24:17 +00:00
|
|
|
if (!InitPointers())
|
2017-12-28 19:10:50 +00:00
|
|
|
{
|
|
|
|
DeInitialize();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_capture->GetMaxFrameSize() > m_frameSize)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Maximum frame size of %zu bytes excceds maximum space available", m_capture->GetMaxFrameSize());
|
|
|
|
DeInitialize();
|
2017-12-07 19:24:17 +00:00
|
|
|
return false;
|
2017-12-28 19:10:50 +00:00
|
|
|
}
|
2017-12-07 19:24:17 +00:00
|
|
|
|
2017-12-13 15:22:41 +00:00
|
|
|
m_timer = CreateWaitableTimer(NULL, TRUE, NULL);
|
|
|
|
if (!m_timer)
|
2017-10-31 12:21:05 +00:00
|
|
|
{
|
2017-12-13 15:22:41 +00:00
|
|
|
DEBUG_ERROR("Failed to create waitable timer");
|
2017-10-31 12:21:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-24 01:24:24 +00:00
|
|
|
// Create the cursor thread
|
|
|
|
m_cursorThread = CreateThread(NULL, 0, _CursorThread, this, 0, NULL);
|
|
|
|
m_cursorEvent = CreateEvent (NULL, FALSE, FALSE, L"CursorEvent");
|
2018-05-24 06:43:16 +00:00
|
|
|
InitializeCriticalSection(&m_cursorCS);
|
2018-05-24 01:24:24 +00:00
|
|
|
|
2017-12-13 10:06:03 +00:00
|
|
|
// update everything except for the hostID
|
2017-12-15 23:04:56 +00:00
|
|
|
memcpy(m_shmHeader->magic, KVMFR_HEADER_MAGIC, sizeof(KVMFR_HEADER_MAGIC));
|
2017-12-19 13:58:42 +00:00
|
|
|
m_shmHeader->version = KVMFR_HEADER_VERSION;
|
2017-10-31 12:21:05 +00:00
|
|
|
|
2018-05-24 01:24:24 +00:00
|
|
|
// zero and tell the client we have restarted
|
|
|
|
ZeroMemory(&m_shmHeader->frame , sizeof(KVMFRFrame ));
|
|
|
|
ZeroMemory(&m_shmHeader->cursor, sizeof(KVMFRCursor));
|
|
|
|
m_shmHeader->flags &= ~KVMFR_HEADER_FLAG_RESTART;
|
2017-12-13 10:06:03 +00:00
|
|
|
|
2017-10-31 12:21:05 +00:00
|
|
|
m_initialized = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-28 19:10:50 +00:00
|
|
|
#define ALIGN_DN(x) ((uintptr_t)(x) & ~0x7F)
|
|
|
|
#define ALIGN_UP(x) ALIGN_DN(x + 0x7F)
|
|
|
|
|
2017-12-07 19:24:17 +00:00
|
|
|
bool Service::InitPointers()
|
|
|
|
{
|
2017-12-19 13:58:42 +00:00
|
|
|
m_shmHeader = reinterpret_cast<KVMFRHeader *>(m_memory);
|
2017-12-28 19:10:50 +00:00
|
|
|
m_cursorData = (uint8_t *)ALIGN_UP(m_memory + sizeof(KVMFRHeader));
|
|
|
|
m_cursorDataSize = 1048576; // 1MB fixed for cursor size, should be more then enough
|
|
|
|
m_frame[0] = (uint8_t *)ALIGN_UP(m_cursorData + m_cursorDataSize);
|
|
|
|
m_frameSize = ALIGN_DN((m_ivshmem->GetSize() - (m_frame[0] - m_memory)) >> 1);
|
|
|
|
m_frame[1] = (uint8_t *)ALIGN_DN(m_frame[0] + m_frameSize);
|
|
|
|
|
2017-12-19 13:58:42 +00:00
|
|
|
|
|
|
|
m_cursorOffset = m_cursorData - m_memory;
|
|
|
|
m_dataOffset[0] = m_frame[0] - m_memory;
|
|
|
|
m_dataOffset[1] = m_frame[1] - m_memory;
|
2017-12-07 19:24:17 +00:00
|
|
|
|
2018-01-30 10:07:46 +00:00
|
|
|
DEBUG_INFO("Total Available : %3u MB", (unsigned int)(m_ivshmem->GetSize() / 1024 / 1024));
|
|
|
|
DEBUG_INFO("Max Cursor Size : %3u MB", (unsigned int)(m_cursorDataSize / 1024 / 1024));
|
|
|
|
DEBUG_INFO("Max Frame Size : %3u MB", (unsigned int)(m_frameSize / 1024 / 1024));
|
|
|
|
DEBUG_INFO("Cursor : %p (0x%08x)", m_cursorData, (int)m_cursorOffset );
|
|
|
|
DEBUG_INFO("Frame 1 : %p (0x%08x)", m_frame[0] , (int)m_dataOffset[0]);
|
|
|
|
DEBUG_INFO("Frame 2 : %p (0x%08x)", m_frame[1] , (int)m_dataOffset[1]);
|
2017-12-07 19:24:17 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-31 12:21:05 +00:00
|
|
|
void Service::DeInitialize()
|
|
|
|
{
|
2017-12-13 15:22:41 +00:00
|
|
|
if (m_timer)
|
2017-12-07 19:24:17 +00:00
|
|
|
{
|
2017-12-13 15:22:41 +00:00
|
|
|
CloseHandle(m_timer);
|
|
|
|
m_timer = NULL;
|
2017-12-07 19:24:17 +00:00
|
|
|
}
|
2017-10-31 13:51:53 +00:00
|
|
|
|
2017-12-19 13:58:42 +00:00
|
|
|
m_shmHeader = NULL;
|
|
|
|
m_cursorData = NULL;
|
|
|
|
m_frame[0] = NULL;
|
|
|
|
m_frame[1] = NULL;
|
|
|
|
m_cursorOffset = 0;
|
|
|
|
m_dataOffset[0] = 0;
|
|
|
|
m_dataOffset[1] = 0;
|
|
|
|
m_cursorDataSize = 0;
|
|
|
|
m_frameSize = 0;
|
2017-12-03 11:03:22 +00:00
|
|
|
|
2017-10-31 13:51:53 +00:00
|
|
|
m_ivshmem->DeInitialize();
|
|
|
|
|
2017-10-31 12:21:05 +00:00
|
|
|
if (m_capture)
|
|
|
|
{
|
|
|
|
m_capture->DeInitialize();
|
|
|
|
m_capture = NULL;
|
|
|
|
}
|
|
|
|
|
2018-05-24 01:24:24 +00:00
|
|
|
WaitForSingleObject(m_cursorThread, INFINITE);
|
|
|
|
CloseHandle(m_cursorThread);
|
|
|
|
CloseHandle(m_cursorEvent );
|
|
|
|
|
2017-12-07 19:24:17 +00:00
|
|
|
m_memory = NULL;
|
2017-10-31 12:21:05 +00:00
|
|
|
m_initialized = false;
|
|
|
|
}
|
|
|
|
|
2017-10-31 16:23:46 +00:00
|
|
|
bool Service::Process()
|
2017-10-31 12:21:05 +00:00
|
|
|
{
|
|
|
|
if (!m_initialized)
|
|
|
|
return false;
|
|
|
|
|
2017-12-15 23:04:56 +00:00
|
|
|
volatile uint8_t *flags = &m_shmHeader->flags;
|
2017-12-13 19:56:08 +00:00
|
|
|
|
2017-11-15 06:28:17 +00:00
|
|
|
// wait for the host to notify that is it is ready to proceed
|
2017-12-13 15:22:41 +00:00
|
|
|
while (true)
|
2017-11-15 06:28:17 +00:00
|
|
|
{
|
2017-12-13 19:56:08 +00:00
|
|
|
const uint8_t f = *flags;
|
|
|
|
|
2017-12-13 10:06:03 +00:00
|
|
|
// check if the client has flagged a restart
|
2017-12-13 19:56:08 +00:00
|
|
|
if (f & KVMFR_HEADER_FLAG_RESTART)
|
2017-12-13 10:06:03 +00:00
|
|
|
{
|
2017-12-30 02:35:19 +00:00
|
|
|
DEBUG_INFO("Restart Requested");
|
|
|
|
if (!m_capture->ReInitialize())
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("ReInitialize Failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_capture->GetMaxFrameSize() > m_frameSize)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Maximum frame size of %zd bytes excceds maximum space available", m_capture->GetMaxFrameSize());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-14 19:22:52 +00:00
|
|
|
INTERLOCKED_AND8((volatile char *)flags, ~(KVMFR_HEADER_FLAG_RESTART));
|
2017-12-13 10:06:03 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-05-24 01:24:24 +00:00
|
|
|
// check if the client has flagged it's ready for a frame
|
|
|
|
if (!(m_shmHeader->frame.flags & KVMFR_FRAME_FLAG_UPDATE))
|
2017-11-15 06:28:17 +00:00
|
|
|
break;
|
|
|
|
|
2018-05-24 01:24:24 +00:00
|
|
|
// wait for 2ms before polling again
|
|
|
|
Sleep(2);
|
2017-11-15 06:28:17 +00:00
|
|
|
}
|
2017-12-03 11:03:22 +00:00
|
|
|
|
2018-05-24 01:24:24 +00:00
|
|
|
struct FrameInfo frame;
|
|
|
|
ZeroMemory(&frame, sizeof(FrameInfo));
|
|
|
|
frame.buffer = m_frame[m_frameIndex];
|
|
|
|
frame.bufferSize = m_frameSize;
|
|
|
|
|
2017-12-11 20:56:50 +00:00
|
|
|
bool ok = false;
|
|
|
|
bool cursorOnly = false;
|
2017-12-19 13:58:42 +00:00
|
|
|
for(int i = 0; i < 2; ++i)
|
2017-12-07 19:24:17 +00:00
|
|
|
{
|
2018-05-24 06:43:16 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-12-19 13:58:42 +00:00
|
|
|
// capture a frame of data
|
2018-05-24 06:43:16 +00:00
|
|
|
switch (status)
|
2017-12-07 19:24:17 +00:00
|
|
|
{
|
2017-12-19 13:58:42 +00:00
|
|
|
case GRAB_STATUS_OK:
|
|
|
|
ok = true;
|
|
|
|
break;
|
2017-12-07 19:24:17 +00:00
|
|
|
|
2017-12-19 13:58:42 +00:00
|
|
|
case GRAB_STATUS_CURSOR:
|
|
|
|
ok = true;
|
|
|
|
cursorOnly = true;
|
2017-12-11 20:56:50 +00:00
|
|
|
break;
|
2017-12-19 13:58:42 +00:00
|
|
|
|
|
|
|
case GRAB_STATUS_ERROR:
|
|
|
|
DEBUG_ERROR("Capture failed");
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case GRAB_STATUS_REINIT:
|
|
|
|
DEBUG_INFO("ReInitialize Requested");
|
|
|
|
if(WTSGetActiveConsoleSessionId() != m_consoleSessionID)
|
|
|
|
{
|
|
|
|
DEBUG_INFO("User switch detected, waiting to regain control");
|
2018-05-24 01:24:24 +00:00
|
|
|
*flags |= KVMFR_HEADER_FLAG_PAUSED;
|
2017-12-19 13:58:42 +00:00
|
|
|
while (WTSGetActiveConsoleSessionId() != m_consoleSessionID)
|
|
|
|
Sleep(100);
|
2018-05-24 01:24:24 +00:00
|
|
|
*flags &= ~KVMFR_HEADER_FLAG_PAUSED;
|
2017-12-19 13:58:42 +00:00
|
|
|
}
|
|
|
|
|
2017-12-28 19:10:50 +00:00
|
|
|
if (!m_capture->ReInitialize())
|
2017-12-19 13:58:42 +00:00
|
|
|
{
|
|
|
|
DEBUG_ERROR("ReInitialize Failed");
|
|
|
|
return false;
|
|
|
|
}
|
2017-12-28 19:10:50 +00:00
|
|
|
|
|
|
|
if (m_capture->GetMaxFrameSize() > m_frameSize)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Maximum frame size of %zd bytes excceds maximum space available", m_capture->GetMaxFrameSize());
|
|
|
|
return false;
|
|
|
|
}
|
2017-12-19 13:58:42 +00:00
|
|
|
continue;
|
2017-12-07 19:24:17 +00:00
|
|
|
}
|
|
|
|
|
2017-12-19 13:58:42 +00:00
|
|
|
if (ok)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-12-07 19:24:17 +00:00
|
|
|
if (!ok)
|
2017-12-03 11:03:22 +00:00
|
|
|
{
|
2017-12-07 19:24:17 +00:00
|
|
|
DEBUG_ERROR("Capture retry count exceeded");
|
2017-12-03 11:03:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-11 20:56:50 +00:00
|
|
|
if (!cursorOnly)
|
2017-12-05 09:39:54 +00:00
|
|
|
{
|
2018-05-24 01:24:24 +00:00
|
|
|
KVMFRFrame * fi = &m_shmHeader->frame;
|
|
|
|
|
|
|
|
fi->type = m_capture->GetFrameType();
|
|
|
|
fi->width = frame.width;
|
|
|
|
fi->height = frame.height;
|
|
|
|
fi->stride = frame.stride;
|
|
|
|
fi->pitch = frame.pitch;
|
|
|
|
fi->dataPos = m_dataOffset[m_frameIndex];
|
2017-12-11 20:56:50 +00:00
|
|
|
if (++m_frameIndex == 2)
|
|
|
|
m_frameIndex = 0;
|
|
|
|
|
2018-05-24 01:24:24 +00:00
|
|
|
// signal a frame update
|
|
|
|
fi->flags |= KVMFR_FRAME_FLAG_UPDATE;
|
2017-12-11 20:56:50 +00:00
|
|
|
}
|
|
|
|
|
2018-05-24 01:24:24 +00:00
|
|
|
// update the flags
|
|
|
|
INTERLOCKED_AND8((volatile char *)flags, KVMFR_HEADER_FLAG_RESTART);
|
|
|
|
// _mm_sfence();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD Service::CursorThread()
|
|
|
|
{
|
|
|
|
while(m_capture)
|
2017-12-11 20:56:50 +00:00
|
|
|
{
|
2018-05-24 01:24:24 +00:00
|
|
|
if (WaitForSingleObject(m_cursorEvent, 1000) != WAIT_OBJECT_0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
KVMFRCursor * cursor = &m_shmHeader->cursor;
|
|
|
|
// wait until the client is ready
|
|
|
|
while (cursor->flags != 0)
|
2017-12-13 10:45:58 +00:00
|
|
|
{
|
2018-05-24 01:24:24 +00:00
|
|
|
Sleep(2);
|
|
|
|
if (!m_capture)
|
|
|
|
return 0;
|
|
|
|
}
|
2017-12-03 11:03:22 +00:00
|
|
|
|
2018-05-24 06:43:16 +00:00
|
|
|
EnterCriticalSection(&m_cursorCS);
|
|
|
|
if (m_cursorInfo.hasPos)
|
2018-05-24 01:24:24 +00:00
|
|
|
{
|
2018-05-24 06:43:16 +00:00
|
|
|
m_cursorInfo.hasPos = false;
|
2017-11-15 06:28:17 +00:00
|
|
|
|
2018-05-24 01:24:24 +00:00
|
|
|
// tell the client where the cursor is
|
|
|
|
cursor->flags |= KVMFR_CURSOR_FLAG_POS;
|
2018-05-24 06:43:16 +00:00
|
|
|
cursor->x = m_cursorInfo.x;
|
|
|
|
cursor->y = m_cursorInfo.y;
|
2018-05-24 01:24:24 +00:00
|
|
|
|
2018-05-24 06:43:16 +00:00
|
|
|
if (m_cursorInfo.visible)
|
2018-05-24 01:24:24 +00:00
|
|
|
cursor->flags |= KVMFR_CURSOR_FLAG_VISIBLE;
|
|
|
|
else
|
|
|
|
cursor->flags &= ~KVMFR_CURSOR_FLAG_VISIBLE;
|
|
|
|
}
|
|
|
|
|
2018-05-24 06:43:16 +00:00
|
|
|
if (m_cursorInfo.hasShape)
|
2018-05-24 01:24:24 +00:00
|
|
|
{
|
2018-05-24 06:43:16 +00:00
|
|
|
m_cursorInfo.hasShape = false;
|
2018-05-24 01:24:24 +00:00
|
|
|
|
2018-05-24 06:43:16 +00:00
|
|
|
if (m_cursorInfo.dataSize > m_cursorDataSize)
|
2018-05-24 01:24:24 +00:00
|
|
|
DEBUG_ERROR("Cursor size exceeds allocated space");
|
2018-05-24 06:43:16 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// give the client the new cursor shape
|
|
|
|
cursor->flags |= KVMFR_CURSOR_FLAG_SHAPE;
|
|
|
|
++cursor->version;
|
2018-05-24 01:24:24 +00:00
|
|
|
|
2018-05-24 06:43:16 +00:00
|
|
|
cursor->type = m_cursorInfo.type;
|
|
|
|
cursor->width = m_cursorInfo.w;
|
|
|
|
cursor->height = m_cursorInfo.h;
|
|
|
|
cursor->pitch = m_cursorInfo.pitch;
|
|
|
|
cursor->dataPos = m_cursorOffset;
|
2018-05-24 01:24:24 +00:00
|
|
|
|
2018-05-24 06:43:16 +00:00
|
|
|
memcpy(m_cursorData, m_cursorInfo.shape, m_cursorInfo.dataSize);
|
|
|
|
}
|
2018-05-24 01:24:24 +00:00
|
|
|
}
|
2018-05-24 06:43:16 +00:00
|
|
|
LeaveCriticalSection(&m_cursorCS);
|
2018-05-24 01:24:24 +00:00
|
|
|
|
|
|
|
cursor->flags |= KVMFR_CURSOR_FLAG_UPDATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2017-10-31 16:53:06 +00:00
|
|
|
}
|