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
|
|
|
|
|
|
|
#include "CaptureFactory.h"
|
|
|
|
|
2017-12-14 19:22:52 +00:00
|
|
|
#if __MINGW32__
|
|
|
|
#define INTERLOCKED_AND8 __sync_and_and_fetch
|
|
|
|
#define INTERLOCKED_OR8 __sync_or_and_fetch
|
|
|
|
#else
|
|
|
|
#define INTERLOCKED_OR8 InterlockedOr8
|
|
|
|
#define INTERLOCKED_AND8 InterlockedAnd8
|
|
|
|
#endif
|
|
|
|
|
2017-10-31 12:21:05 +00:00
|
|
|
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),
|
|
|
|
m_cursorData(NULL),
|
|
|
|
m_shapePending(false)
|
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())
|
|
|
|
return false;
|
|
|
|
|
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));
|
|
|
|
m_shmHeader->version = KVMFR_HEADER_VERSION;
|
|
|
|
m_shmHeader->guestID = m_ivshmem->GetPeerID();
|
|
|
|
m_shmHeader->updateCount = 0;
|
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);
|
|
|
|
ZeroMemory(&m_shmHeader->frame , sizeof(KVMFRFrame ));
|
|
|
|
ZeroMemory(&m_shmHeader->cursor, sizeof(KVMFRCursor));
|
2017-12-13 10:06:03 +00:00
|
|
|
|
2017-10-31 12:21:05 +00:00
|
|
|
m_initialized = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-07 19:24:17 +00:00
|
|
|
bool Service::InitPointers()
|
|
|
|
{
|
2017-12-15 23:04:56 +00:00
|
|
|
m_shmHeader = reinterpret_cast<KVMFRHeader *>(m_memory);
|
2017-12-07 19:24:17 +00:00
|
|
|
m_frame[0] = (uint8_t *)(((uintptr_t)m_memory + sizeof(KVMFRHeader *) + 0x7F) & ~0x7F);
|
|
|
|
m_frameSize = ((m_ivshmem->GetSize() - (m_frame[0] - m_memory)) & ~0x7F) >> 1;
|
|
|
|
m_frame[1] = m_frame[0] + m_frameSize;
|
|
|
|
m_dataOffset[0] = m_frame[0] - m_memory;
|
|
|
|
m_dataOffset[1] = m_frame[1] - m_memory;
|
|
|
|
|
|
|
|
if (m_capture->GetMaxFrameSize() > m_frameSize)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Frame can exceed buffer size!");
|
|
|
|
DeInitialize();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-15 00:30:25 +00:00
|
|
|
m_shapePending = false;
|
|
|
|
|
|
|
|
if (m_cursorData)
|
|
|
|
{
|
|
|
|
delete[] m_cursorData;
|
|
|
|
m_cursorDataSize = 0;
|
|
|
|
m_cursorData = NULL;
|
|
|
|
}
|
|
|
|
|
2017-12-15 23:04:56 +00:00
|
|
|
m_shmHeader = NULL;
|
2017-12-03 11:03:22 +00:00
|
|
|
m_frame[0] = NULL;
|
|
|
|
m_frame[1] = NULL;
|
|
|
|
m_dataOffset[0] = 0;
|
|
|
|
m_dataOffset[1] = 0;
|
|
|
|
|
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-13 10:45:58 +00:00
|
|
|
bool restart = 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-15 23:04:56 +00:00
|
|
|
m_shmHeader->updateCount = 0;
|
2017-12-14 19:22:52 +00:00
|
|
|
INTERLOCKED_AND8((volatile char *)flags, ~(KVMFR_HEADER_FLAG_RESTART));
|
2017-12-13 10:45:58 +00:00
|
|
|
restart = true;
|
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-13 19:56:08 +00:00
|
|
|
if (f & KVMFR_HEADER_FLAG_READY)
|
2017-11-15 06:28:17 +00:00
|
|
|
{
|
2017-12-14 19:22:52 +00:00
|
|
|
INTERLOCKED_AND8((volatile char *)flags, ~(KVMFR_HEADER_FLAG_READY));
|
2017-11-15 06:28:17 +00:00
|
|
|
break;
|
2017-12-13 15:22:41 +00:00
|
|
|
}
|
2017-11-15 06:28:17 +00:00
|
|
|
|
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-15 00:30:25 +00:00
|
|
|
if (m_shapePending)
|
2017-12-07 19:24:17 +00:00
|
|
|
{
|
2017-12-15 00:30:25 +00:00
|
|
|
ok = true;
|
|
|
|
cursorOnly = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
for(int i = 0; i < 2; ++i)
|
2017-12-07 19:24:17 +00:00
|
|
|
{
|
2017-12-15 00:30:25 +00:00
|
|
|
// capture a frame of data
|
|
|
|
switch (m_capture->GrabFrame(frame))
|
|
|
|
{
|
|
|
|
case GRAB_STATUS_OK:
|
|
|
|
ok = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GRAB_STATUS_CURSOR:
|
|
|
|
ok = true;
|
|
|
|
cursorOnly = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GRAB_STATUS_ERROR:
|
|
|
|
DEBUG_ERROR("Capture failed");
|
|
|
|
return false;
|
2017-12-07 19:24:17 +00:00
|
|
|
|
2017-12-15 00:30:25 +00:00
|
|
|
case GRAB_STATUS_REINIT:
|
|
|
|
DEBUG_INFO("ReInitialize Requested");
|
|
|
|
if (!m_capture->ReInitialize() || !InitPointers())
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("ReInitialize Failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ok)
|
2017-12-11 20:56:50 +00:00
|
|
|
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-15 23:04:56 +00:00
|
|
|
m_header.cursor.flags = 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-15 23:04:56 +00:00
|
|
|
m_header.frame.type = m_capture->GetFrameType();
|
|
|
|
m_header.frame.width = frame.width;
|
|
|
|
m_header.frame.height = frame.height;
|
|
|
|
m_header.frame.stride = frame.stride;
|
|
|
|
m_header.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-15 00:30:25 +00:00
|
|
|
if (frame.cursor.hasPos || (m_cursor.hasPos && restart))
|
2017-12-05 09:39:54 +00:00
|
|
|
{
|
2017-12-15 01:56:39 +00:00
|
|
|
// remember the last state for client restart
|
|
|
|
if (frame.cursor.hasPos)
|
2017-12-15 00:30:25 +00:00
|
|
|
{
|
|
|
|
m_cursor.hasPos = true;
|
|
|
|
m_cursor.visible = frame.cursor.visible;
|
|
|
|
m_cursor.x = frame.cursor.x;
|
|
|
|
m_cursor.y = frame.cursor.y;
|
|
|
|
}
|
|
|
|
|
2017-12-11 20:56:50 +00:00
|
|
|
// tell the host where the cursor is
|
2017-12-13 19:56:08 +00:00
|
|
|
updateFlags |= KVMFR_HEADER_FLAG_CURSOR;
|
2017-12-15 23:04:56 +00:00
|
|
|
m_header.cursor.flags |= KVMFR_CURSOR_FLAG_POS;
|
2017-12-15 00:30:25 +00:00
|
|
|
if (m_cursor.visible)
|
2017-12-15 23:04:56 +00:00
|
|
|
m_header.cursor.flags |= KVMFR_CURSOR_FLAG_VISIBLE;
|
|
|
|
m_header.cursor.x = m_cursor.x;
|
|
|
|
m_header.cursor.y = m_cursor.y;
|
2017-12-11 20:56:50 +00:00
|
|
|
}
|
|
|
|
|
2017-12-15 00:30:25 +00:00
|
|
|
if (frame.cursor.hasShape || m_shapePending || (m_cursor.hasShape && restart))
|
2017-12-11 20:56:50 +00:00
|
|
|
{
|
2017-12-15 01:56:39 +00:00
|
|
|
if (!m_shapePending && !restart)
|
2017-12-11 20:56:50 +00:00
|
|
|
{
|
2017-12-15 00:30:25 +00:00
|
|
|
if (frame.cursor.dataSize > m_frameSize)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Cursor size exceeds frame size! This should never happen unless your shared memory is WAY too small");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// take a copy of the shape information for client restarts or pending shape changes
|
|
|
|
m_cursor.hasShape = frame.cursor.hasShape;
|
|
|
|
m_cursor.type = frame.cursor.type;
|
|
|
|
m_cursor.w = frame.cursor.w;
|
|
|
|
m_cursor.h = frame.cursor.h;
|
|
|
|
m_cursor.pitch = frame.cursor.pitch;
|
|
|
|
m_cursor.dataSize = frame.cursor.dataSize;
|
|
|
|
memcpy(&m_cursor, &frame.cursor, sizeof(CursorInfo));
|
|
|
|
if (m_cursorDataSize < frame.cursor.dataSize)
|
|
|
|
{
|
|
|
|
delete[] m_cursorData;
|
|
|
|
m_cursorData = new uint8_t[frame.cursor.dataSize];
|
|
|
|
m_cursorDataSize = frame.cursor.dataSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(m_cursorData, frame.cursor.shape, frame.cursor.dataSize);
|
2017-12-11 20:56:50 +00:00
|
|
|
}
|
2017-12-13 10:45:58 +00:00
|
|
|
|
2017-12-15 00:30:25 +00:00
|
|
|
// we can't send a frame with the cursor shape as we need the buffer location
|
|
|
|
// flag it to send on the next packet
|
|
|
|
if (updateFlags & KVMFR_HEADER_FLAG_FRAME)
|
|
|
|
m_shapePending = true;
|
|
|
|
else
|
2017-12-13 10:45:58 +00:00
|
|
|
{
|
2017-12-15 00:30:25 +00:00
|
|
|
// give the host the new cursor shape
|
|
|
|
updateFlags |= KVMFR_HEADER_FLAG_CURSOR;
|
2017-12-15 23:04:56 +00:00
|
|
|
m_header.cursor.flags |= KVMFR_CURSOR_FLAG_SHAPE;
|
2017-12-15 00:30:25 +00:00
|
|
|
if (m_cursor.visible)
|
2017-12-15 23:04:56 +00:00
|
|
|
m_header.cursor.flags |= KVMFR_CURSOR_FLAG_VISIBLE;
|
2017-12-15 00:30:25 +00:00
|
|
|
|
2017-12-15 23:04:56 +00:00
|
|
|
m_header.cursor.type = m_cursor.type;
|
|
|
|
m_header.cursor.w = m_cursor.w;
|
|
|
|
m_header.cursor.h = m_cursor.h;
|
|
|
|
m_header.cursor.pitch = m_cursor.pitch;
|
|
|
|
m_header.cursor.dataPos = m_dataOffset[m_frameIndex];
|
2017-12-15 00:30:25 +00:00
|
|
|
memcpy(m_frame[m_frameIndex], m_cursorData, m_cursor.dataSize);
|
|
|
|
m_shapePending = false;
|
|
|
|
|
|
|
|
if (++m_frameIndex == 2)
|
|
|
|
m_frameIndex = 0;
|
2017-12-13 10:45:58 +00:00
|
|
|
}
|
2017-12-05 09:39:54 +00:00
|
|
|
}
|
2017-12-03 11:03:22 +00:00
|
|
|
|
2017-12-13 10:45:58 +00:00
|
|
|
// update the flags
|
2017-12-14 19:22:52 +00:00
|
|
|
INTERLOCKED_AND8((volatile char *)flags, KVMFR_HEADER_FLAG_RESTART);
|
|
|
|
INTERLOCKED_OR8 ((volatile char *)flags, updateFlags);
|
2017-12-13 10:45:58 +00:00
|
|
|
|
2017-12-15 23:04:56 +00:00
|
|
|
// update the shared header but don't touch the setup fields
|
|
|
|
const size_t offset = (uintptr_t)&m_header.frame - (uintptr_t)&m_header;
|
|
|
|
memcpy(
|
|
|
|
(uint8_t *)m_shmHeader + offset,
|
|
|
|
(uint8_t *)&m_header + offset,
|
|
|
|
sizeof(KVMFRHeader) - offset
|
|
|
|
);
|
|
|
|
|
|
|
|
// increment the update count so the guest stops waiting
|
|
|
|
++m_shmHeader->updateCount;
|
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
|
|
|
}
|