2019-02-28 05:35:30 +00:00
|
|
|
/*
|
|
|
|
Looking Glass - KVM FrameRelay (KVMFR) Client
|
|
|
|
Copyright (C) 2017-2019 Geoffrey McRae <geoff@hostfission.com>
|
|
|
|
https://looking-glass.hostfission.com
|
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
2019-02-28 08:20:35 +00:00
|
|
|
#include "app.h"
|
|
|
|
|
2019-02-28 05:35:30 +00:00
|
|
|
#include <stdio.h>
|
2019-02-28 09:50:22 +00:00
|
|
|
#include <inttypes.h>
|
2019-03-01 01:42:12 +00:00
|
|
|
#include <unistd.h>
|
2019-03-02 09:33:21 +00:00
|
|
|
#include <stdlib.h>
|
2019-03-03 22:45:45 +00:00
|
|
|
#include <string.h>
|
2019-02-28 05:35:30 +00:00
|
|
|
#include "debug.h"
|
2019-03-03 22:37:50 +00:00
|
|
|
#include "locking.h"
|
2019-02-28 05:35:30 +00:00
|
|
|
#include "capture/interfaces.h"
|
2019-02-28 09:50:22 +00:00
|
|
|
#include "KVMFR.h"
|
|
|
|
|
|
|
|
#define ALIGN_DN(x) ((uintptr_t)(x) & ~0x7F)
|
|
|
|
#define ALIGN_UP(x) ALIGN_DN(x + 0x7F)
|
|
|
|
#define MAX_FRAMES 2
|
|
|
|
|
|
|
|
struct app
|
|
|
|
{
|
|
|
|
KVMFRHeader * shmHeader;
|
2019-03-02 09:33:21 +00:00
|
|
|
uint8_t * pointerData;
|
|
|
|
unsigned int pointerDataSize;
|
|
|
|
unsigned int pointerOffset;
|
|
|
|
|
|
|
|
CaptureInterface * iface;
|
2019-02-28 09:50:22 +00:00
|
|
|
|
|
|
|
uint8_t * frames;
|
|
|
|
unsigned int frameSize;
|
|
|
|
uint8_t * frame[MAX_FRAMES];
|
|
|
|
unsigned int frameOffset[MAX_FRAMES];
|
2019-03-01 01:42:12 +00:00
|
|
|
|
|
|
|
bool running;
|
2019-03-04 02:06:30 +00:00
|
|
|
bool reinit;
|
2019-03-02 09:33:21 +00:00
|
|
|
osThreadHandle * pointerThread;
|
2019-03-01 01:42:12 +00:00
|
|
|
osThreadHandle * frameThread;
|
2019-02-28 09:50:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct app app;
|
2019-02-28 05:35:30 +00:00
|
|
|
|
2019-03-02 09:33:21 +00:00
|
|
|
static int pointerThread(void * opaque)
|
2019-03-01 01:42:12 +00:00
|
|
|
{
|
2019-03-04 04:03:11 +00:00
|
|
|
DEBUG_INFO("Pointer thread started");
|
2019-03-01 04:45:46 +00:00
|
|
|
|
2019-03-04 08:26:02 +00:00
|
|
|
volatile KVMFRCursor * ci = &(app.shmHeader->cursor);
|
|
|
|
uint8_t flags;
|
|
|
|
|
2019-03-04 04:03:11 +00:00
|
|
|
while(app.running)
|
|
|
|
{
|
2019-03-04 08:26:02 +00:00
|
|
|
CaptureResult result;
|
|
|
|
CapturePointer pointer = { 0 };
|
|
|
|
|
|
|
|
result = app.iface->getPointer(&pointer);
|
|
|
|
if (result == CAPTURE_RESULT_REINIT)
|
2019-03-04 04:03:11 +00:00
|
|
|
{
|
|
|
|
app.reinit = true;
|
|
|
|
break;
|
|
|
|
}
|
2019-03-04 08:26:02 +00:00
|
|
|
|
|
|
|
if (result == CAPTURE_RESULT_ERROR)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to get the pointer");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for the client to finish with the previous update
|
|
|
|
while((ci->flags & ~KVMFR_CURSOR_FLAG_UPDATE) != 0 && app.running)
|
|
|
|
usleep(1000);
|
|
|
|
|
|
|
|
flags = KVMFR_CURSOR_FLAG_UPDATE;
|
|
|
|
ci->x = pointer.x;
|
|
|
|
ci->y = pointer.y;
|
|
|
|
flags |= KVMFR_CURSOR_FLAG_POS;
|
|
|
|
if (pointer.visible)
|
|
|
|
flags |= KVMFR_CURSOR_FLAG_VISIBLE;
|
|
|
|
|
|
|
|
// if we have shape data
|
|
|
|
if (pointer.shapeUpdate)
|
|
|
|
{
|
|
|
|
switch(pointer.format)
|
|
|
|
{
|
|
|
|
case CAPTURE_FMT_COLOR : ci->type = CURSOR_TYPE_COLOR ; break;
|
|
|
|
case CAPTURE_FMT_MONO : ci->type = CURSOR_TYPE_MONOCHROME ; break;
|
|
|
|
case CAPTURE_FMT_MASKED: ci->type = CURSOR_TYPE_MASKED_COLOR; break;
|
|
|
|
default:
|
|
|
|
DEBUG_ERROR("Invalid pointer format: %d", pointer.format);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ci->width = pointer.width;
|
|
|
|
ci->height = pointer.height;
|
|
|
|
ci->pitch = pointer.pitch;
|
|
|
|
ci->dataPos = app.pointerOffset;
|
|
|
|
++ci->version;
|
|
|
|
flags |= KVMFR_CURSOR_FLAG_SHAPE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update the flags for the client
|
|
|
|
ci->flags = flags;
|
2019-03-02 09:33:21 +00:00
|
|
|
}
|
2019-03-01 04:45:46 +00:00
|
|
|
|
2019-03-04 04:03:11 +00:00
|
|
|
DEBUG_INFO("Pointer thread stopped");
|
2019-03-01 01:42:12 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int frameThread(void * opaque)
|
|
|
|
{
|
2019-03-01 04:45:46 +00:00
|
|
|
DEBUG_INFO("Frame thread started");
|
|
|
|
|
2019-03-03 22:37:50 +00:00
|
|
|
int frameIndex = 0;
|
|
|
|
volatile KVMFRFrame * fi = &(app.shmHeader->frame);
|
|
|
|
|
2019-03-04 04:03:11 +00:00
|
|
|
while(app.running)
|
2019-03-02 09:33:21 +00:00
|
|
|
{
|
2019-03-04 06:55:45 +00:00
|
|
|
CaptureResult result;
|
2019-03-04 08:26:02 +00:00
|
|
|
CaptureFrame frame =
|
|
|
|
{
|
|
|
|
.data = app.frame[frameIndex]
|
|
|
|
};
|
2019-03-04 06:55:45 +00:00
|
|
|
|
|
|
|
result = app.iface->getFrame(&frame);
|
|
|
|
if (result == CAPTURE_RESULT_REINIT)
|
2019-03-04 02:06:30 +00:00
|
|
|
{
|
|
|
|
app.reinit = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-03-04 06:55:45 +00:00
|
|
|
if (result == CAPTURE_RESULT_ERROR)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to get the frame");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-03-03 22:37:50 +00:00
|
|
|
// wait for the client to finish with the previous frame
|
2019-03-04 02:38:17 +00:00
|
|
|
while(fi->flags & KVMFR_FRAME_FLAG_UPDATE && app.running)
|
2019-03-03 22:37:50 +00:00
|
|
|
usleep(1000);
|
|
|
|
|
|
|
|
switch(frame.format)
|
|
|
|
{
|
|
|
|
case CAPTURE_FMT_BGRA : fi->type = FRAME_TYPE_BGRA ; break;
|
|
|
|
case CAPTURE_FMT_RGBA : fi->type = FRAME_TYPE_RGBA ; break;
|
|
|
|
case CAPTURE_FMT_RGBA10: fi->type = FRAME_TYPE_RGBA10; break;
|
|
|
|
case CAPTURE_FMT_YUV420: fi->type = FRAME_TYPE_YUV420; break;
|
|
|
|
default:
|
|
|
|
DEBUG_ERROR("Unsupported frame format %d, skipping frame", frame.format);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
fi->width = frame.width;
|
|
|
|
fi->height = frame.height;
|
|
|
|
fi->stride = frame.stride;
|
|
|
|
fi->pitch = frame.pitch;
|
|
|
|
fi->dataPos = app.frameOffset[frameIndex];
|
|
|
|
|
|
|
|
INTERLOCKED_OR8(&fi->flags, KVMFR_FRAME_FLAG_UPDATE);
|
|
|
|
|
2019-03-02 09:33:21 +00:00
|
|
|
if (++frameIndex == MAX_FRAMES)
|
|
|
|
frameIndex = 0;
|
|
|
|
}
|
2019-03-01 04:45:46 +00:00
|
|
|
DEBUG_INFO("Frame thread stopped");
|
2019-03-01 01:42:12 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-03-01 04:45:46 +00:00
|
|
|
bool startThreads()
|
|
|
|
{
|
|
|
|
app.running = true;
|
2019-03-02 09:33:21 +00:00
|
|
|
if (!os_createThread("CursorThread", pointerThread, NULL, &app.pointerThread))
|
2019-03-01 04:45:46 +00:00
|
|
|
{
|
2019-03-02 09:33:21 +00:00
|
|
|
DEBUG_ERROR("Failed to create the pointer thread");
|
2019-03-01 04:45:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!os_createThread("FrameThread", frameThread, NULL, &app.frameThread))
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to create the frame thread");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool stopThreads()
|
|
|
|
{
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
app.running = false;
|
2019-03-02 09:33:21 +00:00
|
|
|
|
2019-03-01 04:45:46 +00:00
|
|
|
if (app.frameThread && !os_joinThread(app.frameThread, NULL))
|
|
|
|
{
|
|
|
|
DEBUG_WARN("Failed to join the frame thread");
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
app.frameThread = NULL;
|
|
|
|
|
2019-03-02 09:33:21 +00:00
|
|
|
if (app.pointerThread && !os_joinThread(app.pointerThread, NULL))
|
2019-03-01 04:45:46 +00:00
|
|
|
{
|
2019-03-02 09:33:21 +00:00
|
|
|
DEBUG_WARN("Failed to join the pointer thread");
|
2019-03-01 04:45:46 +00:00
|
|
|
ok = false;
|
|
|
|
}
|
2019-03-02 09:33:21 +00:00
|
|
|
app.pointerThread = NULL;
|
2019-03-01 04:45:46 +00:00
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2019-03-02 09:33:21 +00:00
|
|
|
static bool captureStart()
|
2019-03-01 04:57:48 +00:00
|
|
|
{
|
2019-03-02 09:33:21 +00:00
|
|
|
DEBUG_INFO("Using : %s", app.iface->getName());
|
2019-03-01 04:57:48 +00:00
|
|
|
|
2019-03-02 09:33:21 +00:00
|
|
|
const unsigned int maxFrameSize = app.iface->getMaxFrameSize();
|
2019-03-01 04:57:48 +00:00
|
|
|
if (maxFrameSize > app.frameSize)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Maximum frame size of %d bytes excceds maximum space available", maxFrameSize);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
DEBUG_INFO("Capture Size : %u MiB (%u)", maxFrameSize / 1048576, maxFrameSize);
|
|
|
|
|
|
|
|
DEBUG_INFO("==== [ Capture Start ] ====");
|
|
|
|
return startThreads();
|
|
|
|
}
|
|
|
|
|
2019-03-04 02:06:30 +00:00
|
|
|
static bool captureRestart()
|
|
|
|
{
|
|
|
|
DEBUG_INFO("==== [ Capture Restart ] ====");
|
|
|
|
if (!stopThreads())
|
|
|
|
return false;
|
|
|
|
|
2019-03-04 08:26:02 +00:00
|
|
|
if (!app.iface->deinit() || !app.iface->init(app.pointerData, app.pointerDataSize))
|
2019-03-04 02:06:30 +00:00
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to reinitialize the capture device");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!captureStart())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-02 09:31:33 +00:00
|
|
|
int app_main()
|
2019-02-28 05:35:30 +00:00
|
|
|
{
|
2019-02-28 08:20:35 +00:00
|
|
|
unsigned int shmemSize = os_shmemSize();
|
2019-02-28 09:50:22 +00:00
|
|
|
uint8_t * shmemMap = NULL;
|
|
|
|
int exitcode = 0;
|
2019-02-28 08:20:35 +00:00
|
|
|
|
|
|
|
DEBUG_INFO("IVSHMEM Size : %u MiB", shmemSize / 1048576);
|
2019-02-28 09:50:22 +00:00
|
|
|
if (!os_shmemMmap((void **)&shmemMap) || !shmemMap)
|
2019-02-28 08:20:35 +00:00
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to map the shared memory");
|
|
|
|
return -1;
|
|
|
|
}
|
2019-02-28 09:50:22 +00:00
|
|
|
DEBUG_INFO("IVSHMEM Address : 0x%" PRIXPTR, (uintptr_t)shmemMap);
|
|
|
|
|
|
|
|
app.shmHeader = (KVMFRHeader *)shmemMap;
|
2019-03-03 22:37:50 +00:00
|
|
|
app.pointerData = (uint8_t *)ALIGN_UP(shmemMap + sizeof(KVMFRHeader));
|
|
|
|
app.pointerDataSize = 1048576; // 1MB fixed for pointer size, should be more then enough
|
|
|
|
app.pointerOffset = app.pointerData - shmemMap;
|
2019-03-02 09:33:21 +00:00
|
|
|
app.frames = (uint8_t *)ALIGN_UP(app.pointerData + app.pointerDataSize);
|
2019-02-28 09:50:22 +00:00
|
|
|
app.frameSize = ALIGN_DN((shmemSize - (app.frames - shmemMap)) / MAX_FRAMES);
|
|
|
|
|
2019-03-02 09:33:21 +00:00
|
|
|
DEBUG_INFO("Max Cursor Size : %u MiB" , app.pointerDataSize / 1048576);
|
2019-02-28 09:50:22 +00:00
|
|
|
DEBUG_INFO("Max Frame Size : %u MiB" , app.frameSize / 1048576);
|
2019-03-02 09:33:21 +00:00
|
|
|
DEBUG_INFO("Cursor : 0x%" PRIXPTR " (0x%08x)", (uintptr_t)app.pointerData, app.pointerOffset);
|
2019-02-28 09:50:22 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < MAX_FRAMES; ++i)
|
|
|
|
{
|
|
|
|
app.frame [i] = app.frames + i * app.frameSize;
|
|
|
|
app.frameOffset[i] = app.frame[i] - shmemMap;
|
|
|
|
DEBUG_INFO("Frame %d : 0x%" PRIXPTR " (0x%08x)", i, (uintptr_t)app.frame[i], app.frameOffset[i]);
|
|
|
|
}
|
2019-02-28 08:20:35 +00:00
|
|
|
|
2019-03-02 09:33:21 +00:00
|
|
|
CaptureInterface * iface = NULL;
|
2019-02-28 05:35:30 +00:00
|
|
|
for(int i = 0; CaptureInterfaces[i]; ++i)
|
|
|
|
{
|
|
|
|
iface = CaptureInterfaces[i];
|
|
|
|
DEBUG_INFO("Trying : %s", iface->getName());
|
2019-03-01 10:41:06 +00:00
|
|
|
|
2019-02-28 05:35:30 +00:00
|
|
|
if (!iface->create())
|
2019-03-01 10:41:06 +00:00
|
|
|
{
|
|
|
|
iface = NULL;
|
2019-02-28 05:35:30 +00:00
|
|
|
continue;
|
2019-03-01 10:41:06 +00:00
|
|
|
}
|
2019-02-28 05:35:30 +00:00
|
|
|
|
2019-03-04 08:26:02 +00:00
|
|
|
if (iface->init(app.pointerData, app.pointerDataSize))
|
2019-02-28 05:35:30 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
iface->free();
|
|
|
|
iface = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!iface)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to find a supported capture interface");
|
2019-02-28 09:50:22 +00:00
|
|
|
exitcode = -1;
|
|
|
|
goto fail;
|
2019-02-28 05:35:30 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 04:03:11 +00:00
|
|
|
app.iface = iface;
|
2019-03-01 01:42:12 +00:00
|
|
|
|
2019-03-03 22:37:50 +00:00
|
|
|
// initialize the shared memory headers
|
|
|
|
memcpy(app.shmHeader->magic, KVMFR_HEADER_MAGIC, sizeof(KVMFR_HEADER_MAGIC));
|
|
|
|
app.shmHeader->version = KVMFR_HEADER_VERSION;
|
|
|
|
|
|
|
|
// zero and notify the client we are starting
|
|
|
|
memset(&(app.shmHeader->frame ), 0, sizeof(KVMFRFrame ));
|
|
|
|
memset(&(app.shmHeader->cursor), 0, sizeof(KVMFRCursor));
|
|
|
|
app.shmHeader->flags &= ~KVMFR_HEADER_FLAG_RESTART;
|
|
|
|
|
2019-03-02 09:33:21 +00:00
|
|
|
if (!captureStart())
|
|
|
|
{
|
|
|
|
exitcode = -1;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2019-03-03 22:37:50 +00:00
|
|
|
volatile char * flags = (volatile char *)&(app.shmHeader->flags);
|
|
|
|
|
2019-03-02 09:31:33 +00:00
|
|
|
while(app.running)
|
2019-03-01 01:42:12 +00:00
|
|
|
{
|
2019-03-03 22:37:50 +00:00
|
|
|
INTERLOCKED_AND8(flags, ~(KVMFR_HEADER_FLAG_RESTART));
|
|
|
|
|
2019-03-04 02:06:30 +00:00
|
|
|
if (app.reinit && !captureRestart())
|
|
|
|
{
|
|
|
|
exitcode = -1;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
app.reinit = false;
|
|
|
|
|
2019-03-04 06:45:19 +00:00
|
|
|
switch(iface->capture())
|
2019-03-01 04:45:46 +00:00
|
|
|
{
|
|
|
|
case CAPTURE_RESULT_OK:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CAPTURE_RESULT_TIMEOUT:
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case CAPTURE_RESULT_REINIT:
|
2019-03-04 02:06:30 +00:00
|
|
|
if (!captureRestart())
|
2019-03-01 04:45:46 +00:00
|
|
|
{
|
|
|
|
exitcode = -1;
|
2019-03-04 02:06:30 +00:00
|
|
|
goto exit;
|
2019-03-01 04:45:46 +00:00
|
|
|
}
|
2019-03-03 12:45:37 +00:00
|
|
|
continue;
|
2019-03-01 04:45:46 +00:00
|
|
|
|
|
|
|
case CAPTURE_RESULT_ERROR:
|
|
|
|
DEBUG_ERROR("Capture interface reported a fatal error");
|
|
|
|
exitcode = -1;
|
|
|
|
goto finish;
|
|
|
|
}
|
2019-03-01 01:42:12 +00:00
|
|
|
}
|
|
|
|
|
2019-03-01 04:45:46 +00:00
|
|
|
finish:
|
|
|
|
stopThreads();
|
2019-02-28 09:50:22 +00:00
|
|
|
exit:
|
2019-03-02 09:33:21 +00:00
|
|
|
|
2019-02-28 05:35:30 +00:00
|
|
|
iface->deinit();
|
|
|
|
iface->free();
|
2019-02-28 09:50:22 +00:00
|
|
|
fail:
|
2019-02-28 08:27:17 +00:00
|
|
|
os_shmemUnmap();
|
2019-02-28 09:50:22 +00:00
|
|
|
return exitcode;
|
2019-03-02 09:31:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void app_quit()
|
|
|
|
{
|
|
|
|
app.running = false;
|
2019-02-28 05:35:30 +00:00
|
|
|
}
|