2019-02-28 05:35:30 +00:00
|
|
|
/*
|
|
|
|
Looking Glass - KVM FrameRelay (KVMFR) Client
|
2020-01-02 11:21:42 +00:00
|
|
|
Copyright (C) 2017-2020 Geoffrey McRae <geoff@hostfission.com>
|
2019-02-28 05:35:30 +00:00
|
|
|
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-04-09 06:28:11 +00:00
|
|
|
#include "interface/platform.h"
|
|
|
|
#include "interface/capture.h"
|
|
|
|
#include "dynamic/capture.h"
|
2019-04-11 01:03:30 +00:00
|
|
|
#include "common/debug.h"
|
2019-05-09 12:06:58 +00:00
|
|
|
#include "common/option.h"
|
2019-04-11 01:03:30 +00:00
|
|
|
#include "common/locking.h"
|
|
|
|
#include "common/KVMFR.h"
|
2019-04-11 01:34:22 +00:00
|
|
|
#include "common/crash.h"
|
2020-01-02 11:21:42 +00:00
|
|
|
#include "common/thread.h"
|
2020-01-03 03:53:56 +00:00
|
|
|
#include "common/ivshmem.h"
|
2020-01-13 04:52:54 +00:00
|
|
|
#include "common/sysinfo.h"
|
2019-02-28 08:20:35 +00:00
|
|
|
|
2020-01-09 04:42:32 +00:00
|
|
|
#include <lgmp/host.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 09:50:22 +00:00
|
|
|
|
|
|
|
#define ALIGN_DN(x) ((uintptr_t)(x) & ~0x7F)
|
|
|
|
#define ALIGN_UP(x) ALIGN_DN(x + 0x7F)
|
2020-01-09 04:42:32 +00:00
|
|
|
|
2020-01-09 05:15:04 +00:00
|
|
|
#define LGMP_Q_FRAME_LEN 2
|
2020-01-10 09:04:46 +00:00
|
|
|
#define LGMP_Q_POINTER_LEN 20
|
|
|
|
|
|
|
|
static const struct LGMPQueueConfig FRAME_QUEUE_CONFIG =
|
|
|
|
{
|
|
|
|
.queueID = LGMP_Q_FRAME,
|
|
|
|
.numMessages = LGMP_Q_FRAME_LEN,
|
2020-01-20 03:18:45 +00:00
|
|
|
.subTimeout = 5000
|
2020-01-10 09:04:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct LGMPQueueConfig POINTER_QUEUE_CONFIG =
|
|
|
|
{
|
|
|
|
.queueID = LGMP_Q_POINTER,
|
|
|
|
.numMessages = LGMP_Q_POINTER_LEN,
|
2020-01-20 03:18:45 +00:00
|
|
|
.subTimeout = 5000
|
2020-01-10 09:04:46 +00:00
|
|
|
};
|
2020-01-09 04:42:32 +00:00
|
|
|
|
|
|
|
#define MAX_POINTER_SIZE (sizeof(KVMFRCursor) + (128 * 128 * 4))
|
2019-02-28 09:50:22 +00:00
|
|
|
|
|
|
|
struct app
|
|
|
|
{
|
2020-01-09 04:42:32 +00:00
|
|
|
PLGMPHost lgmp;
|
2019-05-28 04:06:15 +00:00
|
|
|
|
2020-01-10 00:00:46 +00:00
|
|
|
PLGMPHostQueue pointerQueue;
|
|
|
|
PLGMPMemory pointerMemory[LGMP_Q_POINTER_LEN];
|
|
|
|
PLGMPMemory pointerShape;
|
|
|
|
bool pointerShapeValid;
|
|
|
|
unsigned int pointerIndex;
|
2019-03-02 09:33:21 +00:00
|
|
|
|
2020-01-10 00:00:46 +00:00
|
|
|
size_t maxFrameSize;
|
|
|
|
PLGMPHostQueue frameQueue;
|
|
|
|
PLGMPMemory frameMemory[LGMP_Q_FRAME_LEN];
|
|
|
|
unsigned int frameIndex;
|
2019-02-28 09:50:22 +00:00
|
|
|
|
2020-01-09 04:42:32 +00:00
|
|
|
CaptureInterface * iface;
|
2019-03-01 01:42:12 +00:00
|
|
|
|
2020-01-02 11:21:42 +00:00
|
|
|
bool running;
|
|
|
|
bool reinit;
|
2020-01-09 04:42:32 +00:00
|
|
|
LGThread * lgmpThread;
|
2020-01-02 11:21:42 +00:00
|
|
|
LGThread * frameThread;
|
2019-02-28 09:50:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct app app;
|
2019-02-28 05:35:30 +00:00
|
|
|
|
2020-01-09 04:42:32 +00:00
|
|
|
static int lgmpThread(void * opaque)
|
2019-03-01 01:42:12 +00:00
|
|
|
{
|
2020-01-09 04:42:32 +00:00
|
|
|
LGMP_STATUS status;
|
2019-03-04 04:03:11 +00:00
|
|
|
while(app.running)
|
|
|
|
{
|
2020-01-09 04:42:32 +00:00
|
|
|
if ((status = lgmpHostProcess(app.lgmp)) != LGMP_OK)
|
2019-03-04 08:26:02 +00:00
|
|
|
{
|
2020-01-09 04:42:32 +00:00
|
|
|
DEBUG_ERROR("lgmpHostProcess Failed: %s", lgmpStatusString(status));
|
|
|
|
break;
|
2019-03-04 08:26:02 +00:00
|
|
|
}
|
2020-01-26 14:55:14 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
2ms should be good for up to 500FPS, do not lower this value as excessive
|
|
|
|
polling of io memory at this time causes QEMU/KVM stalls due to an unknown
|
|
|
|
fault.
|
|
|
|
See: https://lists.gnu.org/archive/html/qemu-devel/2020-01/msg06331.html
|
|
|
|
*/
|
|
|
|
usleep(2000);
|
2019-03-02 09:33:21 +00:00
|
|
|
}
|
2019-03-01 04:45:46 +00:00
|
|
|
|
2020-01-09 04:42:32 +00:00
|
|
|
app.running = false;
|
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-05-28 04:24:48 +00:00
|
|
|
bool frameValid = false;
|
2019-12-17 09:56:14 +00:00
|
|
|
bool repeatFrame = false;
|
2019-05-28 04:24:48 +00:00
|
|
|
CaptureFrame frame = { 0 };
|
2020-01-13 08:17:09 +00:00
|
|
|
const long pageSize = sysinfo_getPageSize();
|
2019-05-28 04:24:48 +00:00
|
|
|
|
2019-03-04 04:03:11 +00:00
|
|
|
while(app.running)
|
2019-03-02 09:33:21 +00:00
|
|
|
{
|
2019-10-01 13:17:20 +00:00
|
|
|
switch(app.iface->waitFrame(&frame))
|
2019-03-04 02:06:30 +00:00
|
|
|
{
|
2019-05-28 04:24:48 +00:00
|
|
|
case CAPTURE_RESULT_OK:
|
2019-12-17 09:56:14 +00:00
|
|
|
repeatFrame = false;
|
2019-05-28 04:24:48 +00:00
|
|
|
break;
|
2019-03-04 02:06:30 +00:00
|
|
|
|
2019-05-28 04:24:48 +00:00
|
|
|
case CAPTURE_RESULT_REINIT:
|
|
|
|
{
|
|
|
|
app.reinit = true;
|
|
|
|
DEBUG_INFO("Frame thread reinit");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
case CAPTURE_RESULT_ERROR:
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to get the frame");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
case CAPTURE_RESULT_TIMEOUT:
|
|
|
|
{
|
2020-01-10 00:00:46 +00:00
|
|
|
if (frameValid && lgmpHostQueueNewSubs(app.frameQueue) > 0)
|
2019-05-28 04:24:48 +00:00
|
|
|
{
|
|
|
|
// resend the last frame
|
2019-12-17 09:56:14 +00:00
|
|
|
repeatFrame = true;
|
2019-05-28 04:24:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2019-03-04 06:55:45 +00:00
|
|
|
}
|
|
|
|
|
2020-01-09 08:49:47 +00:00
|
|
|
//wait until there is room in the queue
|
|
|
|
if (lgmpHostQueuePending(app.frameQueue) == LGMP_Q_FRAME_LEN)
|
|
|
|
{
|
|
|
|
if (!app.running)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we are repeating a frame just send the last frame again
|
2019-12-17 09:56:14 +00:00
|
|
|
if (repeatFrame)
|
2019-03-03 22:37:50 +00:00
|
|
|
{
|
2020-01-10 00:00:46 +00:00
|
|
|
lgmpHostQueuePost(app.frameQueue, 0, app.frameMemory[app.frameIndex]);
|
2020-01-09 04:42:32 +00:00
|
|
|
continue;
|
|
|
|
}
|
2019-03-03 22:37:50 +00:00
|
|
|
|
2020-01-09 04:42:32 +00:00
|
|
|
// we increment the index first so that if we need to repeat a frame
|
|
|
|
// the index still points to the latest valid frame
|
2020-01-24 05:31:03 +00:00
|
|
|
if (++app.frameIndex == LGMP_Q_FRAME_LEN)
|
2020-01-24 05:06:38 +00:00
|
|
|
app.frameIndex = 0;
|
2019-03-03 22:37:50 +00:00
|
|
|
|
2020-01-09 04:42:32 +00:00
|
|
|
KVMFRFrame * fi = lgmpHostMemPtr(app.frameMemory[app.frameIndex]);
|
|
|
|
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;
|
2019-12-17 09:56:14 +00:00
|
|
|
}
|
2019-03-03 22:37:50 +00:00
|
|
|
|
2020-01-09 04:42:32 +00:00
|
|
|
fi->width = frame.width;
|
|
|
|
fi->height = frame.height;
|
|
|
|
fi->stride = frame.stride;
|
|
|
|
fi->pitch = frame.pitch;
|
2020-01-13 08:17:09 +00:00
|
|
|
fi->offset = pageSize - sizeof(FrameBuffer);
|
2020-01-09 04:42:32 +00:00
|
|
|
frameValid = true;
|
|
|
|
|
2020-01-13 08:17:09 +00:00
|
|
|
// put the framebuffer on the border of the next page
|
|
|
|
// this is to allow for aligned DMA transfers by the receiver
|
2020-01-13 08:30:49 +00:00
|
|
|
FrameBuffer * fb = (FrameBuffer *)(((uint8_t*)fi) + fi->offset);
|
2020-01-09 04:42:32 +00:00
|
|
|
framebuffer_prepare(fb);
|
|
|
|
|
2020-01-09 08:49:47 +00:00
|
|
|
/* we post and then get the frame, this is intentional! */
|
2020-01-10 00:00:46 +00:00
|
|
|
lgmpHostQueuePost(app.frameQueue, 0, app.frameMemory[app.frameIndex]);
|
2020-01-09 08:49:47 +00:00
|
|
|
app.iface->getFrame(fb);
|
2019-03-02 09:33:21 +00:00
|
|
|
}
|
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;
|
2020-01-09 04:42:32 +00:00
|
|
|
if (!lgCreateThread("LGMPThread", lgmpThread, NULL, &app.lgmpThread))
|
2019-03-01 04:45:46 +00:00
|
|
|
{
|
2020-01-09 04:42:32 +00:00
|
|
|
DEBUG_ERROR("Failed to create the LGMP thread");
|
2019-03-01 04:45:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-01-02 11:21:42 +00:00
|
|
|
if (!lgCreateThread("FrameThread", frameThread, NULL, &app.frameThread))
|
2019-03-01 04:45:46 +00:00
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to create the frame thread");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool stopThreads()
|
|
|
|
{
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
app.running = false;
|
2019-04-10 11:36:43 +00:00
|
|
|
app.iface->stop();
|
2019-03-02 09:33:21 +00:00
|
|
|
|
2020-01-02 11:21:42 +00:00
|
|
|
if (app.frameThread && !lgJoinThread(app.frameThread, NULL))
|
2019-03-01 04:45:46 +00:00
|
|
|
{
|
|
|
|
DEBUG_WARN("Failed to join the frame thread");
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
app.frameThread = NULL;
|
|
|
|
|
2020-01-09 04:42:32 +00:00
|
|
|
if (app.lgmpThread && !lgJoinThread(app.lgmpThread, NULL))
|
2019-03-01 04:45:46 +00:00
|
|
|
{
|
2020-01-09 04:42:32 +00:00
|
|
|
DEBUG_WARN("Failed to join the LGMP thread");
|
2019-03-01 04:45:46 +00:00
|
|
|
ok = false;
|
|
|
|
}
|
2020-01-09 04:42:32 +00:00
|
|
|
app.lgmpThread = 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();
|
2020-01-09 04:42:32 +00:00
|
|
|
if (maxFrameSize > app.maxFrameSize)
|
2019-03-01 04:57:48 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2020-01-09 04:42:32 +00:00
|
|
|
if (!app.iface->deinit() || !app.iface->init())
|
2019-03-04 02:06:30 +00:00
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to reinitialize the capture device");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!captureStart())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-01-09 04:42:32 +00:00
|
|
|
bool captureGetPointerBuffer(void ** data, uint32_t * size)
|
|
|
|
{
|
2020-01-09 08:49:47 +00:00
|
|
|
// spin until there is room
|
|
|
|
while(lgmpHostQueuePending(app.pointerQueue) == LGMP_Q_POINTER_LEN)
|
|
|
|
{
|
2020-01-09 09:27:55 +00:00
|
|
|
DEBUG_INFO("pending");
|
2020-01-09 08:49:47 +00:00
|
|
|
if (!app.running)
|
|
|
|
return false;
|
|
|
|
}
|
2020-01-09 04:42:32 +00:00
|
|
|
|
|
|
|
PLGMPMemory mem = app.pointerMemory[app.pointerIndex];
|
|
|
|
*data = ((uint8_t*)lgmpHostMemPtr(mem)) + sizeof(KVMFRCursor);
|
|
|
|
*size = MAX_POINTER_SIZE - sizeof(KVMFRCursor);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void capturePostPointerBuffer(CapturePointer pointer)
|
|
|
|
{
|
2020-01-09 08:49:47 +00:00
|
|
|
PLGMPMemory mem;
|
2020-01-10 00:00:46 +00:00
|
|
|
const bool newClient = lgmpHostQueueNewSubs(app.pointerQueue) > 0;
|
2020-01-09 04:42:32 +00:00
|
|
|
|
2020-01-09 08:49:47 +00:00
|
|
|
if (pointer.shapeUpdate || newClient)
|
|
|
|
{
|
2020-01-09 09:27:55 +00:00
|
|
|
if (pointer.shapeUpdate)
|
2020-01-09 08:49:47 +00:00
|
|
|
{
|
|
|
|
// swap the latest shape buffer out of rotation
|
|
|
|
PLGMPMemory tmp = app.pointerShape;
|
|
|
|
app.pointerShape = app.pointerMemory[app.pointerIndex];
|
|
|
|
app.pointerMemory[app.pointerIndex] = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// use the last known shape buffer
|
|
|
|
mem = app.pointerShape;
|
|
|
|
}
|
|
|
|
else
|
2020-01-09 09:27:55 +00:00
|
|
|
{
|
2020-01-09 08:49:47 +00:00
|
|
|
mem = app.pointerMemory[app.pointerIndex];
|
2020-01-09 09:27:55 +00:00
|
|
|
if (++app.pointerIndex == LGMP_Q_POINTER_LEN)
|
|
|
|
app.pointerIndex = 0;
|
|
|
|
}
|
2020-01-09 08:49:47 +00:00
|
|
|
|
2020-01-26 06:25:14 +00:00
|
|
|
uint32_t flags = 0;
|
2020-01-09 08:49:47 +00:00
|
|
|
KVMFRCursor *cursor = lgmpHostMemPtr(mem);
|
2020-01-26 06:25:14 +00:00
|
|
|
|
|
|
|
if (pointer.positionUpdate)
|
|
|
|
{
|
|
|
|
flags |= CURSOR_FLAG_POSITION;
|
2020-01-26 15:07:32 +00:00
|
|
|
cursor->x = pointer.x;
|
|
|
|
cursor->y = pointer.y;
|
2020-01-26 06:25:14 +00:00
|
|
|
}
|
2020-01-09 04:42:32 +00:00
|
|
|
|
2020-01-26 15:07:32 +00:00
|
|
|
if (pointer.visible)
|
|
|
|
flags |= CURSOR_FLAG_VISIBLE;
|
|
|
|
|
2020-01-09 04:42:32 +00:00
|
|
|
if (pointer.shapeUpdate)
|
|
|
|
{
|
2020-01-09 08:49:47 +00:00
|
|
|
// remember which slot has the latest shape
|
2020-01-09 04:42:32 +00:00
|
|
|
cursor->width = pointer.width;
|
|
|
|
cursor->height = pointer.height;
|
|
|
|
cursor->pitch = pointer.pitch;
|
|
|
|
switch(pointer.format)
|
|
|
|
{
|
|
|
|
case CAPTURE_FMT_COLOR : cursor->type = CURSOR_TYPE_COLOR ; break;
|
|
|
|
case CAPTURE_FMT_MONO : cursor->type = CURSOR_TYPE_MONOCHROME ; break;
|
|
|
|
case CAPTURE_FMT_MASKED: cursor->type = CURSOR_TYPE_MASKED_COLOR; break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
DEBUG_ERROR("Invalid pointer type");
|
|
|
|
return;
|
|
|
|
}
|
2020-01-09 08:49:47 +00:00
|
|
|
|
|
|
|
app.pointerShapeValid = true;
|
2020-01-09 04:42:32 +00:00
|
|
|
}
|
|
|
|
|
2020-01-26 06:25:14 +00:00
|
|
|
if ((pointer.shapeUpdate || newClient) && app.pointerShapeValid)
|
|
|
|
flags |= CURSOR_FLAG_SHAPE;
|
2020-01-09 08:49:47 +00:00
|
|
|
|
2020-01-09 04:42:32 +00:00
|
|
|
LGMP_STATUS status;
|
2020-01-26 06:25:14 +00:00
|
|
|
while ((status = lgmpHostQueuePost(app.pointerQueue, flags, mem)) != LGMP_OK)
|
2020-01-09 04:42:32 +00:00
|
|
|
{
|
2020-01-09 09:27:55 +00:00
|
|
|
if (status == LGMP_ERR_QUEUE_FULL)
|
|
|
|
continue;
|
|
|
|
|
2020-01-10 00:00:46 +00:00
|
|
|
DEBUG_ERROR("lgmpHostQueuePost Failed (Pointer): %s", lgmpStatusString(status));
|
2020-01-09 04:42:32 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-09 12:06:58 +00:00
|
|
|
// this is called from the platform specific startup routine
|
2019-05-09 09:30:09 +00:00
|
|
|
int app_main(int argc, char * argv[])
|
2019-02-28 05:35:30 +00:00
|
|
|
{
|
2019-04-11 07:15:17 +00:00
|
|
|
if (!installCrashHandler(os_getExecutable()))
|
2019-04-11 01:34:22 +00:00
|
|
|
DEBUG_WARN("Failed to install the crash handler");
|
|
|
|
|
2020-01-03 03:53:56 +00:00
|
|
|
ivshmemOptionsInit();
|
|
|
|
|
2019-05-09 12:06:58 +00:00
|
|
|
// register capture interface options
|
|
|
|
for(int i = 0; CaptureInterfaces[i]; ++i)
|
|
|
|
if (CaptureInterfaces[i]->initOptions)
|
|
|
|
CaptureInterfaces[i]->initOptions();
|
|
|
|
|
2019-05-11 08:23:06 +00:00
|
|
|
// try load values from a config file
|
|
|
|
option_load("looking-glass-host.ini");
|
|
|
|
|
2019-05-09 12:06:58 +00:00
|
|
|
// parse the command line arguments
|
|
|
|
if (!option_parse(argc, argv))
|
|
|
|
{
|
|
|
|
option_free();
|
|
|
|
DEBUG_ERROR("Failure to parse the command line");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-05-11 01:35:42 +00:00
|
|
|
if (!option_validate())
|
|
|
|
{
|
|
|
|
option_free();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-05-09 12:06:58 +00:00
|
|
|
// perform platform specific initialization
|
|
|
|
if (!app_init())
|
|
|
|
return -1;
|
|
|
|
|
2019-04-09 06:30:07 +00:00
|
|
|
DEBUG_INFO("Looking Glass Host (" BUILD_VERSION ")");
|
2020-01-03 03:53:56 +00:00
|
|
|
|
|
|
|
struct IVSHMEM shmDev;
|
|
|
|
if (!ivshmemOpen(&shmDev))
|
2019-02-28 08:20:35 +00:00
|
|
|
{
|
2020-01-03 03:53:56 +00:00
|
|
|
DEBUG_ERROR("Failed to open the IVSHMEM device");
|
2019-02-28 08:20:35 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2019-02-28 09:50:22 +00:00
|
|
|
|
2020-01-03 03:53:56 +00:00
|
|
|
int exitcode = 0;
|
|
|
|
DEBUG_INFO("IVSHMEM Size : %u MiB", shmDev.size / 1048576);
|
|
|
|
DEBUG_INFO("IVSHMEM Address : 0x%" PRIXPTR, (uintptr_t)shmDev.mem);
|
2020-01-13 05:06:53 +00:00
|
|
|
DEBUG_INFO("Max Pointer Size : %u KiB", (unsigned int)MAX_POINTER_SIZE / 1024);
|
2020-01-03 03:53:56 +00:00
|
|
|
|
2020-01-09 04:42:32 +00:00
|
|
|
LGMP_STATUS status;
|
|
|
|
if ((status = lgmpHostInit(shmDev.mem, shmDev.size, &app.lgmp)) != LGMP_OK)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("lgmpHostInit Failed: %s", lgmpStatusString(status));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2020-01-10 09:04:46 +00:00
|
|
|
if ((status = lgmpHostQueueNew(app.lgmp, FRAME_QUEUE_CONFIG, &app.frameQueue)) != LGMP_OK)
|
2020-01-09 08:49:47 +00:00
|
|
|
{
|
|
|
|
DEBUG_ERROR("lgmpHostQueueCreate Failed (Frame): %s", lgmpStatusString(status));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2020-01-10 09:04:46 +00:00
|
|
|
if ((status = lgmpHostQueueNew(app.lgmp, POINTER_QUEUE_CONFIG, &app.pointerQueue)) != LGMP_OK)
|
2020-01-09 04:42:32 +00:00
|
|
|
{
|
2020-01-10 00:00:46 +00:00
|
|
|
DEBUG_ERROR("lgmpHostQueueNew Failed (Pointer): %s", lgmpStatusString(status));
|
2020-01-09 04:42:32 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
2019-02-28 09:50:22 +00:00
|
|
|
|
2020-01-09 04:42:32 +00:00
|
|
|
for(int i = 0; i < LGMP_Q_POINTER_LEN; ++i)
|
|
|
|
{
|
|
|
|
if ((status = lgmpHostMemAlloc(app.lgmp, MAX_POINTER_SIZE, &app.pointerMemory[i])) != LGMP_OK)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("lgmpHostMemAlloc Failed (Pointer): %s", lgmpStatusString(status));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
2019-02-28 09:50:22 +00:00
|
|
|
|
2020-01-09 08:49:47 +00:00
|
|
|
app.pointerShapeValid = false;
|
|
|
|
if ((status = lgmpHostMemAlloc(app.lgmp, MAX_POINTER_SIZE, &app.pointerShape)) != LGMP_OK)
|
2019-02-28 09:50:22 +00:00
|
|
|
{
|
2020-01-09 08:49:47 +00:00
|
|
|
DEBUG_ERROR("lgmpHostMemAlloc Failed (Pointer Shape): %s", lgmpStatusString(status));
|
2020-01-09 04:42:32 +00:00
|
|
|
goto fail;
|
2019-02-28 09:50:22 +00:00
|
|
|
}
|
2019-02-28 08:20:35 +00:00
|
|
|
|
2020-01-13 05:06:53 +00:00
|
|
|
const long sz = sysinfo_getPageSize();
|
|
|
|
app.maxFrameSize = lgmpHostMemAvail(app.lgmp);
|
|
|
|
app.maxFrameSize = (app.maxFrameSize - (sz - 1)) & ~(sz - 1);
|
|
|
|
app.maxFrameSize /= LGMP_Q_FRAME_LEN;
|
|
|
|
DEBUG_INFO("Max Frame Size : %u MiB", (unsigned int)(app.maxFrameSize / 1048576LL));
|
|
|
|
|
2020-01-09 04:42:32 +00:00
|
|
|
for(int i = 0; i < LGMP_Q_FRAME_LEN; ++i)
|
|
|
|
{
|
2020-01-13 04:52:54 +00:00
|
|
|
if ((status = lgmpHostMemAllocAligned(app.lgmp, app.maxFrameSize, sz, &app.frameMemory[i])) != LGMP_OK)
|
2020-01-09 04:42:32 +00:00
|
|
|
{
|
|
|
|
DEBUG_ERROR("lgmpHostMemAlloc Failed (Frame): %s", lgmpStatusString(status));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-01-09 04:42:32 +00:00
|
|
|
if (!iface->create(captureGetPointerBuffer, capturePostPointerBuffer))
|
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
|
|
|
|
2020-01-09 04:42:32 +00:00
|
|
|
if (iface->init())
|
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-02 09:33:21 +00:00
|
|
|
if (!captureStart())
|
|
|
|
{
|
|
|
|
exitcode = -1;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2019-03-02 09:31:33 +00:00
|
|
|
while(app.running)
|
2019-03-01 01:42:12 +00:00
|
|
|
{
|
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-04-10 03:06:33 +00:00
|
|
|
app.reinit = false;
|
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:
|
2020-01-09 04:42:32 +00:00
|
|
|
|
|
|
|
for(int i = 0; i < LGMP_Q_FRAME_LEN; ++i)
|
|
|
|
lgmpHostMemFree(&app.frameMemory[i]);
|
2020-01-09 08:49:47 +00:00
|
|
|
for(int i = 0; i < LGMP_Q_POINTER_LEN; ++i)
|
|
|
|
lgmpHostMemFree(&app.pointerMemory[i]);
|
|
|
|
lgmpHostMemFree(&app.pointerShape);
|
2020-01-09 04:42:32 +00:00
|
|
|
lgmpHostFree(&app.lgmp);
|
|
|
|
|
2020-01-03 03:53:56 +00:00
|
|
|
ivshmemClose(&shmDev);
|
2019-02-28 09:50:22 +00:00
|
|
|
return exitcode;
|
2019-03-02 09:31:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void app_quit()
|
|
|
|
{
|
|
|
|
app.running = false;
|
2020-01-20 03:18:45 +00:00
|
|
|
}
|