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-10-31 13:51:53 +00:00
|
|
|
#include "common\debug.h"
|
2017-12-03 15:31:16 +00:00
|
|
|
#include "common\KVMFR.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-10-31 12:21:05 +00:00
|
|
|
m_readyEvent(INVALID_HANDLE_VALUE),
|
|
|
|
m_capture(NULL),
|
2017-12-03 11:03:22 +00:00
|
|
|
m_header(NULL),
|
2017-11-15 06:28:17 +00:00
|
|
|
m_frameIndex(0)
|
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-10-31 12:21:05 +00:00
|
|
|
m_readyEvent = m_ivshmem->CreateVectorEvent(0);
|
|
|
|
if (m_readyEvent == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to get event for vector 0");
|
|
|
|
DeInitialize();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-31 14:46:47 +00:00
|
|
|
// we save this as it might actually be valid
|
2017-12-03 11:03:22 +00:00
|
|
|
UINT16 hostID = m_header->hostID;
|
2017-10-31 14:46:47 +00:00
|
|
|
|
2017-12-03 15:31:16 +00:00
|
|
|
ZeroMemory(m_header, sizeof(KVMFRHeader));
|
|
|
|
memcpy(m_header->magic, KVMFR_HEADER_MAGIC, sizeof(KVMFR_HEADER_MAGIC));
|
2017-10-31 12:21:05 +00:00
|
|
|
|
2017-12-11 20:56:50 +00:00
|
|
|
m_header->version = KVMFR_HEADER_VERSION;
|
|
|
|
m_header->guestID = m_ivshmem->GetPeerID();
|
|
|
|
m_header->hostID = hostID;
|
|
|
|
m_header->updateCount = 0;
|
2017-10-31 12:21:05 +00:00
|
|
|
|
|
|
|
m_initialized = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-07 19:24:17 +00:00
|
|
|
bool Service::InitPointers()
|
|
|
|
{
|
|
|
|
m_header = reinterpret_cast<KVMFRHeader *>(m_memory);
|
|
|
|
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-10-31 13:51:53 +00:00
|
|
|
if (m_readyEvent != INVALID_HANDLE_VALUE)
|
2017-12-07 19:24:17 +00:00
|
|
|
{
|
2017-10-31 13:51:53 +00:00
|
|
|
CloseHandle(m_readyEvent);
|
2017-12-07 19:24:17 +00:00
|
|
|
m_readyEvent = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
2017-10-31 13:51:53 +00:00
|
|
|
|
2017-12-03 11:03:22 +00:00
|
|
|
m_header = NULL;
|
|
|
|
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-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-11-15 06:28:17 +00:00
|
|
|
// wait for the host to notify that is it is ready to proceed
|
|
|
|
bool eventDone = false;
|
|
|
|
while (!eventDone)
|
|
|
|
{
|
|
|
|
switch (WaitForSingleObject(m_readyEvent, 1000))
|
|
|
|
{
|
|
|
|
case WAIT_ABANDONED:
|
|
|
|
DEBUG_ERROR("Wait abandoned");
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case WAIT_OBJECT_0:
|
|
|
|
eventDone = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WAIT_TIMEOUT:
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case WAIT_FAILED:
|
|
|
|
DEBUG_ERROR("Wait failed");
|
|
|
|
return false;
|
|
|
|
|
|
|
|
default:
|
|
|
|
DEBUG_ERROR("Unknown error");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2017-12-03 11:03:22 +00:00
|
|
|
ResetEvent(m_readyEvent);
|
|
|
|
|
2017-12-11 20:56:50 +00:00
|
|
|
bool ok = false;
|
|
|
|
bool cursorOnly = false;
|
2017-12-07 19:24:17 +00:00
|
|
|
for(int i = 0; i < 2; ++i)
|
|
|
|
{
|
|
|
|
// capture a frame of data
|
|
|
|
switch (m_capture->GrabFrame(frame))
|
|
|
|
{
|
|
|
|
case GRAB_STATUS_OK:
|
|
|
|
ok = true;
|
|
|
|
break;
|
|
|
|
|
2017-12-11 20:56:50 +00:00
|
|
|
case GRAB_STATUS_CURSOR:
|
|
|
|
ok = true;
|
|
|
|
cursorOnly = true;
|
|
|
|
break;
|
|
|
|
|
2017-12-07 19:24:17 +00:00
|
|
|
case GRAB_STATUS_ERROR:
|
|
|
|
DEBUG_ERROR("Capture failed");
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case GRAB_STATUS_REINIT:
|
|
|
|
DEBUG_INFO("ReInitialize Requested");
|
|
|
|
if (!m_capture->ReInitialize() || !InitPointers())
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("ReInitialize Failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ok)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
m_header->flags = 0;
|
|
|
|
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
|
|
|
|
m_header->flags |= KVMFR_HEADER_FLAG_FRAME;
|
|
|
|
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];
|
|
|
|
if (++m_frameIndex == 2)
|
|
|
|
m_frameIndex = 0;
|
2017-12-05 09:39:54 +00:00
|
|
|
}
|
2017-12-11 20:56:50 +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
|
|
|
|
m_header->flags |= KVMFR_HEADER_FLAG_CURSOR;
|
|
|
|
m_header->cursor.flags |= KVMFR_CURSOR_FLAG_POS;
|
2017-12-12 15:29:53 +00:00
|
|
|
if (frame.cursor.visible)
|
|
|
|
m_header->cursor.flags |= KVMFR_CURSOR_FLAG_VISIBLE;
|
2017-12-11 20:56:50 +00:00
|
|
|
m_header->cursor.x = frame.cursor.x;
|
|
|
|
m_header->cursor.y = frame.cursor.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frame.cursor.hasShape)
|
|
|
|
{
|
|
|
|
// give the host the new cursor shape
|
|
|
|
m_header->flags |= KVMFR_HEADER_FLAG_CURSOR;
|
|
|
|
m_header->cursor.flags |= KVMFR_CURSOR_FLAG_SHAPE;
|
|
|
|
m_header->cursor.type = frame.cursor.type;
|
|
|
|
m_header->cursor.w = frame.cursor.w;
|
|
|
|
m_header->cursor.h = frame.cursor.h;
|
2017-12-12 17:47:28 +00:00
|
|
|
m_header->cursor.pitch = frame.cursor.pitch;
|
2017-12-11 20:56:50 +00:00
|
|
|
if (frame.cursor.dataSize > KVMFR_CURSOR_BUFFER)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Cursor shape size exceeds buffer size");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
memcpy(m_header->cursor.shape, frame.cursor.shape, frame.cursor.dataSize);
|
2017-12-05 09:39:54 +00:00
|
|
|
}
|
2017-12-03 11:03:22 +00:00
|
|
|
|
2017-12-11 20:56:50 +00:00
|
|
|
// increment the update count to resume the host
|
|
|
|
++m_header->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
|
|
|
}
|