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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-12-13 10:06:03 +00:00
|
|
|
// clear but retain the restart flag if it was set by the client
|
2017-12-15 23:04:56 +00:00
|
|
|
INTERLOCKED_AND8((char *)&m_shmHeader->flags, KVMFR_HEADER_FLAG_RESTART);
|
2017-12-16 18:03:47 +00:00
|
|
|
ZeroMemory(&m_shmHeader->detail, sizeof(KVMFRDetail));
|
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
|
|
|
|
2017-12-28 19:10:50 +00:00
|
|
|
DEBUG_INFO("Total Available : %3I64u MB", m_ivshmem->GetSize() / 1024 / 1024);
|
|
|
|
DEBUG_INFO("Max Cursor Size : %3I64u MB", m_cursorDataSize / 1024 / 1024);
|
|
|
|
DEBUG_INFO("Max Frame Size : %3I64u MB", m_frameSize / 1024 / 1024);
|
|
|
|
DEBUG_INFO("Cursor : %p (0x%08I64x)", m_cursorData, m_cursorOffset );
|
|
|
|
DEBUG_INFO("Frame 1 : %p (0x%08I64x)", m_frame[0] , m_dataOffset[0]);
|
|
|
|
DEBUG_INFO("Frame 2 : %p (0x%08I64x)", m_frame[1] , 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;
|
|
|
|
}
|
|
|
|
|
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-11 20:56:50 +00:00
|
|
|
struct FrameInfo frame;
|
|
|
|
ZeroMemory(&frame, sizeof(FrameInfo));
|
2017-12-05 09:39:54 +00:00
|
|
|
frame.buffer = m_frame[m_frameIndex];
|
|
|
|
frame.bufferSize = m_frameSize;
|
2017-11-28 04:18:22 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-12-13 15:22:41 +00:00
|
|
|
// check if the client has flagged it's ready
|
2017-12-19 13:58:42 +00:00
|
|
|
if (!(f & KVMFR_HEADER_FLAG_FRAME))
|
2017-11-15 06:28:17 +00:00
|
|
|
break;
|
|
|
|
|
2017-12-13 15:22:41 +00:00
|
|
|
// wait for 100ns before polling again
|
|
|
|
LARGE_INTEGER timeout;
|
|
|
|
timeout.QuadPart = -100;
|
|
|
|
if (!SetWaitableTimer(m_timer, &timeout, 0, NULL, NULL, FALSE))
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to set waitable timer");
|
2017-11-15 06:28:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-12-13 15:22:41 +00:00
|
|
|
WaitForSingleObject(m_timer, INFINITE);
|
2017-11-15 06:28:17 +00:00
|
|
|
}
|
2017-12-03 11:03:22 +00:00
|
|
|
|
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
|
|
|
{
|
2017-12-19 13:58:42 +00:00
|
|
|
// capture a frame of data
|
|
|
|
switch (m_capture->GrabFrame(frame))
|
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");
|
|
|
|
while (WTSGetActiveConsoleSessionId() != m_consoleSessionID)
|
|
|
|
Sleep(100);
|
|
|
|
}
|
|
|
|
|
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-13 19:56:08 +00:00
|
|
|
uint8_t updateFlags = 0;
|
2017-12-03 11:03:22 +00:00
|
|
|
|
2017-12-11 20:56:50 +00:00
|
|
|
if (!cursorOnly)
|
2017-12-05 09:39:54 +00:00
|
|
|
{
|
2017-12-11 20:56:50 +00:00
|
|
|
// signal a frame update
|
2017-12-13 19:56:08 +00:00
|
|
|
updateFlags |= KVMFR_HEADER_FLAG_FRAME;
|
2017-12-16 18:03:47 +00:00
|
|
|
m_detail.frame.type = m_capture->GetFrameType();
|
|
|
|
m_detail.frame.width = frame.width;
|
|
|
|
m_detail.frame.height = frame.height;
|
|
|
|
m_detail.frame.stride = frame.stride;
|
2017-12-16 18:05:56 +00:00
|
|
|
m_detail.frame.pitch = frame.pitch;
|
2017-12-16 18:03:47 +00:00
|
|
|
m_detail.frame.dataPos = m_dataOffset[m_frameIndex];
|
2017-12-11 20:56:50 +00:00
|
|
|
if (++m_frameIndex == 2)
|
|
|
|
m_frameIndex = 0;
|
2017-12-05 09:39:54 +00:00
|
|
|
}
|
2017-12-11 20:56:50 +00:00
|
|
|
|
2017-12-19 13:58:42 +00:00
|
|
|
if (frame.cursor.hasPos)
|
2017-12-05 09:39:54 +00:00
|
|
|
{
|
2017-12-11 20:56:50 +00:00
|
|
|
// tell the host where the cursor is
|
2017-12-19 13:58:42 +00:00
|
|
|
updateFlags |= KVMFR_HEADER_FLAG_CURSOR;
|
2017-12-16 18:03:47 +00:00
|
|
|
m_detail.cursor.flags |= KVMFR_CURSOR_FLAG_POS;
|
2017-12-19 13:58:42 +00:00
|
|
|
m_detail.cursor.x = frame.cursor.x;
|
|
|
|
m_detail.cursor.y = frame.cursor.y;
|
|
|
|
|
|
|
|
if (frame.cursor.visible)
|
2017-12-16 18:03:47 +00:00
|
|
|
m_detail.cursor.flags |= KVMFR_CURSOR_FLAG_VISIBLE;
|
2017-12-19 13:58:42 +00:00
|
|
|
else
|
|
|
|
m_detail.cursor.flags &= ~KVMFR_CURSOR_FLAG_VISIBLE;
|
2017-12-11 20:56:50 +00:00
|
|
|
}
|
|
|
|
|
2017-12-19 13:58:42 +00:00
|
|
|
if (frame.cursor.hasShape)
|
2017-12-11 20:56:50 +00:00
|
|
|
{
|
2017-12-19 13:58:42 +00:00
|
|
|
if (frame.cursor.dataSize > m_cursorDataSize)
|
2017-12-13 10:45:58 +00:00
|
|
|
{
|
2017-12-19 13:58:42 +00:00
|
|
|
DEBUG_ERROR("Cursor size exceeds allocated space");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// give the host the new cursor shape
|
|
|
|
updateFlags |= KVMFR_HEADER_FLAG_CURSOR;
|
|
|
|
m_detail.cursor.flags |= KVMFR_CURSOR_FLAG_SHAPE;
|
|
|
|
++m_detail.cursor.version;
|
|
|
|
|
|
|
|
m_detail.cursor.type = frame.cursor.type;
|
|
|
|
m_detail.cursor.width = frame.cursor.w;
|
|
|
|
m_detail.cursor.height = frame.cursor.h;
|
|
|
|
m_detail.cursor.pitch = frame.cursor.pitch;
|
|
|
|
m_detail.cursor.dataPos = m_cursorOffset;
|
|
|
|
memcpy(m_cursorData, frame.cursor.shape, frame.cursor.dataSize);
|
2017-12-05 09:39:54 +00:00
|
|
|
}
|
2017-12-03 11:03:22 +00:00
|
|
|
|
2017-12-16 18:03:47 +00:00
|
|
|
// update the shared details only
|
|
|
|
memcpy(&m_shmHeader->detail, &m_detail, sizeof(KVMFRDetail));
|
2017-12-15 23:04:56 +00:00
|
|
|
|
2017-12-19 13:58:42 +00:00
|
|
|
// update the flags
|
|
|
|
INTERLOCKED_AND8((volatile char *)flags, KVMFR_HEADER_FLAG_RESTART);
|
|
|
|
INTERLOCKED_OR8 ((volatile char *)flags, updateFlags);
|
2017-12-28 04:41:37 +00:00
|
|
|
_mm_sfence();
|
2017-11-15 06:28:17 +00:00
|
|
|
|
2017-10-31 12:21:05 +00:00
|
|
|
return true;
|
2017-10-31 16:53:06 +00:00
|
|
|
}
|