2017-10-31 08:07:16 +00:00
|
|
|
/*
|
2017-12-03 15:22:49 +00:00
|
|
|
Looking Glass - KVM FrameRelay (KVMFR) Client
|
2019-02-22 11:16:14 +00:00
|
|
|
Copyright (C) 2017-2019 Geoffrey McRae <geoff@hostfission.com>
|
2017-12-11 17:30:47 +00:00
|
|
|
https://looking-glass.hostfission.com
|
2017-10-31 08:07:16 +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
|
|
|
|
*/
|
|
|
|
|
2019-03-30 01:26:06 +00:00
|
|
|
#include "main.h"
|
2019-03-30 04:52:00 +00:00
|
|
|
#include "config.h"
|
2019-03-30 01:26:06 +00:00
|
|
|
|
2017-11-25 07:20:30 +00:00
|
|
|
#include <getopt.h>
|
2017-12-17 09:09:47 +00:00
|
|
|
#include <signal.h>
|
2017-12-10 17:11:36 +00:00
|
|
|
#include <SDL2/SDL_syswm.h>
|
2017-10-19 04:15:49 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2017-12-28 04:30:03 +00:00
|
|
|
#include <sys/mman.h>
|
2019-03-30 04:52:00 +00:00
|
|
|
#include <sys/stat.h>
|
2017-12-28 04:30:03 +00:00
|
|
|
#include <fcntl.h>
|
2017-10-19 04:15:49 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <assert.h>
|
2020-08-09 05:14:17 +00:00
|
|
|
#include <stdatomic.h>
|
2021-01-18 02:54:21 +00:00
|
|
|
#include <linux/input.h>
|
2017-10-19 04:15:49 +00:00
|
|
|
|
2019-04-11 01:12:59 +00:00
|
|
|
#include "common/debug.h"
|
2019-04-11 06:41:52 +00:00
|
|
|
#include "common/crash.h"
|
2019-04-11 01:12:59 +00:00
|
|
|
#include "common/KVMFR.h"
|
2019-05-23 19:29:38 +00:00
|
|
|
#include "common/stringutils.h"
|
2020-01-02 12:59:06 +00:00
|
|
|
#include "common/thread.h"
|
2020-01-17 03:35:08 +00:00
|
|
|
#include "common/locking.h"
|
2020-01-02 13:08:43 +00:00
|
|
|
#include "common/event.h"
|
2020-01-03 04:17:14 +00:00
|
|
|
#include "common/ivshmem.h"
|
2020-01-03 04:53:44 +00:00
|
|
|
#include "common/time.h"
|
2020-10-08 15:17:20 +00:00
|
|
|
#include "common/version.h"
|
2020-01-03 04:17:14 +00:00
|
|
|
|
2017-12-10 14:31:52 +00:00
|
|
|
#include "utils.h"
|
2021-01-18 04:33:56 +00:00
|
|
|
#include "kb.h"
|
2019-02-24 18:43:18 +00:00
|
|
|
#include "ll.h"
|
2017-10-19 04:15:49 +00:00
|
|
|
|
2020-01-03 04:53:44 +00:00
|
|
|
#define RESIZE_TIMEOUT (10 * 1000) // 10ms
|
|
|
|
|
2019-03-30 01:26:06 +00:00
|
|
|
// forwards
|
|
|
|
static int cursorThread(void * unused);
|
|
|
|
static int renderThread(void * unused);
|
|
|
|
static int frameThread (void * unused);
|
2017-11-25 06:51:34 +00:00
|
|
|
|
2020-01-02 13:08:43 +00:00
|
|
|
static LGEvent *e_startup = NULL;
|
2020-05-21 03:41:59 +00:00
|
|
|
static LGEvent *e_frame = NULL;
|
2020-01-02 13:08:43 +00:00
|
|
|
static LGThread *t_spice = NULL;
|
|
|
|
static LGThread *t_render = NULL;
|
|
|
|
static LGThread *t_cursor = NULL;
|
|
|
|
static LGThread *t_frame = NULL;
|
|
|
|
static SDL_Cursor *cursor = NULL;
|
2019-10-26 01:03:10 +00:00
|
|
|
|
2021-01-08 12:12:15 +00:00
|
|
|
static Uint32 e_SDLEvent; // our SDL event
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
LG_EVENT_ALIGN_TO_GUEST
|
|
|
|
};
|
2021-01-05 00:47:17 +00:00
|
|
|
|
2021-01-04 01:06:54 +00:00
|
|
|
struct AppState g_state;
|
2021-01-04 01:04:43 +00:00
|
|
|
struct CursorState g_cursor;
|
2019-05-21 05:03:59 +00:00
|
|
|
|
|
|
|
// this structure is initialized in config.c
|
|
|
|
struct AppParams params = { 0 };
|
2017-10-19 04:15:49 +00:00
|
|
|
|
2021-01-09 07:41:02 +00:00
|
|
|
static void setGrab(bool enable);
|
|
|
|
static void setGrabQuiet(bool enable);
|
2021-01-16 10:01:15 +00:00
|
|
|
static void setCursorInView(bool enable);
|
2021-01-09 07:41:02 +00:00
|
|
|
|
2021-01-14 06:05:26 +00:00
|
|
|
static void lgInit(void)
|
2020-12-03 13:32:28 +00:00
|
|
|
{
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.state = APP_STATE_RUNNING;
|
|
|
|
g_state.resizeDone = true;
|
2020-12-03 13:32:28 +00:00
|
|
|
|
2021-01-08 09:49:20 +00:00
|
|
|
g_cursor.useScale = false;
|
|
|
|
g_cursor.scale.x = 1.0;
|
|
|
|
g_cursor.scale.y = 1.0;
|
2021-01-18 19:00:59 +00:00
|
|
|
g_cursor.draw = false;
|
2021-01-04 04:40:02 +00:00
|
|
|
g_cursor.inView = false;
|
2021-01-04 01:04:43 +00:00
|
|
|
g_cursor.guest.valid = false;
|
2020-12-03 13:32:28 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 09:30:03 +00:00
|
|
|
bool app_getProp(LG_DSProperty prop, void * ret)
|
|
|
|
{
|
|
|
|
return g_state.ds->getProp(prop, ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Window * app_getWindow(void)
|
|
|
|
{
|
|
|
|
return g_state.window;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool app_inputEnabled(void)
|
2021-01-09 07:01:08 +00:00
|
|
|
{
|
|
|
|
return params.useSpiceInput && !g_state.ignoreInput &&
|
|
|
|
((g_cursor.grab && params.captureInputOnly) || !params.captureInputOnly);
|
|
|
|
}
|
|
|
|
|
2021-01-15 09:30:03 +00:00
|
|
|
bool app_cursorInWindow(void)
|
|
|
|
{
|
|
|
|
return g_cursor.inWindow;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool app_cursorIsGrabbed(void)
|
|
|
|
{
|
|
|
|
return g_cursor.grab;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool app_cursorWantsRaw(void)
|
|
|
|
{
|
|
|
|
return params.rawMouse;
|
|
|
|
}
|
|
|
|
|
|
|
|
void app_updateCursorPos(double x, double y)
|
|
|
|
{
|
|
|
|
g_cursor.pos.x = x;
|
|
|
|
g_cursor.pos.y = y;
|
2021-01-18 18:56:40 +00:00
|
|
|
g_cursor.valid = true;
|
2021-01-15 09:30:03 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 01:53:55 +00:00
|
|
|
void app_handleFocusEvent(bool focused)
|
|
|
|
{
|
|
|
|
if (!app_inputEnabled())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (params.grabKeyboardOnFocus)
|
|
|
|
{
|
|
|
|
if (focused)
|
|
|
|
g_state.ds->grabKeyboard();
|
|
|
|
else
|
|
|
|
g_state.ds->ungrabKeyboard();
|
|
|
|
}
|
|
|
|
|
|
|
|
g_state.focused = focused;
|
|
|
|
g_cursor.realign = true;
|
|
|
|
g_state.ds->realignPointer();
|
|
|
|
}
|
|
|
|
|
2021-01-17 02:36:37 +00:00
|
|
|
void app_handleCloseEvent(void)
|
|
|
|
{
|
|
|
|
if (!params.ignoreQuit || !g_cursor.inView)
|
|
|
|
g_state.state = APP_STATE_SHUTDOWN;
|
|
|
|
}
|
|
|
|
|
2021-01-14 06:05:26 +00:00
|
|
|
static void alignToGuest(void)
|
2021-01-08 12:12:15 +00:00
|
|
|
{
|
2021-01-09 07:01:08 +00:00
|
|
|
if (SDL_HasEvent(e_SDLEvent))
|
2021-01-08 12:12:15 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
SDL_Event event;
|
|
|
|
SDL_memset(&event, 0, sizeof(event));
|
|
|
|
event.type = e_SDLEvent;
|
|
|
|
event.user.code = LG_EVENT_ALIGN_TO_GUEST;
|
|
|
|
|
|
|
|
SDL_PushEvent(&event);
|
|
|
|
}
|
|
|
|
|
2021-01-14 06:05:26 +00:00
|
|
|
static void updatePositionInfo(void)
|
2017-12-01 02:42:58 +00:00
|
|
|
{
|
2021-01-18 15:44:56 +00:00
|
|
|
if (!g_state.haveSrcSize)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
float srcW;
|
|
|
|
float srcH;
|
|
|
|
switch(params.winRotate)
|
2017-12-01 02:42:58 +00:00
|
|
|
{
|
2021-01-18 15:44:56 +00:00
|
|
|
case LG_ROTATE_0:
|
|
|
|
case LG_ROTATE_180:
|
|
|
|
srcW = g_state.srcSize.x;
|
|
|
|
srcH = g_state.srcSize.y;
|
|
|
|
break;
|
2020-01-03 06:23:48 +00:00
|
|
|
|
2021-01-18 15:44:56 +00:00
|
|
|
case LG_ROTATE_90:
|
|
|
|
case LG_ROTATE_270:
|
|
|
|
srcW = g_state.srcSize.y;
|
|
|
|
srcH = g_state.srcSize.x;
|
|
|
|
break;
|
|
|
|
}
|
2020-01-03 06:23:48 +00:00
|
|
|
|
2021-01-18 15:44:56 +00:00
|
|
|
if (params.keepAspect)
|
|
|
|
{
|
|
|
|
const float srcAspect = srcH / srcW;
|
|
|
|
const float wndAspect = (float)g_state.windowH / (float)g_state.windowW;
|
|
|
|
bool force = true;
|
|
|
|
|
|
|
|
if (params.dontUpscale &&
|
|
|
|
srcW <= g_state.windowW &&
|
|
|
|
srcH <= g_state.windowH)
|
|
|
|
{
|
|
|
|
force = false;
|
|
|
|
g_state.dstRect.w = srcW;
|
|
|
|
g_state.dstRect.h = srcH;
|
|
|
|
g_state.dstRect.x = g_state.windowCX - srcW / 2;
|
|
|
|
g_state.dstRect.y = g_state.windowCY - srcH / 2;
|
2017-12-01 02:42:58 +00:00
|
|
|
}
|
|
|
|
else
|
2021-01-18 15:44:56 +00:00
|
|
|
if ((int)(wndAspect * 1000) == (int)(srcAspect * 1000))
|
2017-12-01 02:42:58 +00:00
|
|
|
{
|
2021-01-18 15:44:56 +00:00
|
|
|
force = false;
|
|
|
|
g_state.dstRect.w = g_state.windowW;
|
|
|
|
g_state.dstRect.h = g_state.windowH;
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.dstRect.x = 0;
|
|
|
|
g_state.dstRect.y = 0;
|
2021-01-18 15:44:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if (wndAspect < srcAspect)
|
|
|
|
{
|
|
|
|
g_state.dstRect.w = (float)g_state.windowH / srcAspect;
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.dstRect.h = g_state.windowH;
|
2021-01-18 15:44:56 +00:00
|
|
|
g_state.dstRect.x = (g_state.windowW >> 1) - (g_state.dstRect.w >> 1);
|
|
|
|
g_state.dstRect.y = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_state.dstRect.w = g_state.windowW;
|
|
|
|
g_state.dstRect.h = (float)g_state.windowW * srcAspect;
|
|
|
|
g_state.dstRect.x = 0;
|
|
|
|
g_state.dstRect.y = (g_state.windowH >> 1) - (g_state.dstRect.h >> 1);
|
2017-12-01 02:42:58 +00:00
|
|
|
}
|
2020-12-03 13:50:27 +00:00
|
|
|
|
2021-01-18 15:44:56 +00:00
|
|
|
if (force && params.forceAspect)
|
|
|
|
{
|
|
|
|
g_state.resizeTimeout = microtime() + RESIZE_TIMEOUT;
|
|
|
|
g_state.resizeDone = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_state.dstRect.x = 0;
|
|
|
|
g_state.dstRect.y = 0;
|
|
|
|
g_state.dstRect.w = g_state.windowW;
|
|
|
|
g_state.dstRect.h = g_state.windowH;
|
2018-07-19 13:32:42 +00:00
|
|
|
}
|
2021-01-18 15:44:56 +00:00
|
|
|
g_state.dstRect.valid = true;
|
|
|
|
|
|
|
|
g_cursor.useScale = (
|
|
|
|
srcH != g_state.dstRect.h ||
|
|
|
|
srcW != g_state.dstRect.w ||
|
|
|
|
g_cursor.guest.dpiScale != 100);
|
2017-12-20 19:38:56 +00:00
|
|
|
|
2021-01-18 15:44:56 +00:00
|
|
|
g_cursor.scale.x = (float)srcH / (float)g_state.dstRect.h;
|
|
|
|
g_cursor.scale.y = (float)srcW / (float)g_state.dstRect.w;
|
|
|
|
g_cursor.dpiScale = g_cursor.guest.dpiScale / 100.0f;
|
|
|
|
|
2021-01-18 18:56:40 +00:00
|
|
|
if (!g_state.posInfoValid)
|
|
|
|
{
|
|
|
|
g_state.posInfoValid = true;
|
|
|
|
alignToGuest();
|
|
|
|
}
|
|
|
|
|
2021-01-18 15:44:56 +00:00
|
|
|
done:
|
2021-01-04 02:40:03 +00:00
|
|
|
atomic_fetch_add(&g_state.lgrResize, 1);
|
2017-12-06 15:37:46 +00:00
|
|
|
}
|
|
|
|
|
2019-03-28 10:23:24 +00:00
|
|
|
static int renderThread(void * unused)
|
2017-10-19 04:15:49 +00:00
|
|
|
{
|
2021-01-04 01:06:54 +00:00
|
|
|
if (!g_state.lgr->render_startup(g_state.lgrData, g_state.window))
|
2019-05-27 08:38:36 +00:00
|
|
|
{
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.state = APP_STATE_SHUTDOWN;
|
2018-07-28 04:49:37 +00:00
|
|
|
|
2019-12-09 16:30:04 +00:00
|
|
|
/* unblock threads waiting on the condition */
|
2020-01-02 13:08:43 +00:00
|
|
|
lgSignalEvent(e_startup);
|
2019-03-28 10:23:24 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-12-09 16:30:04 +00:00
|
|
|
/* signal to other threads that the renderer is ready */
|
2020-01-02 13:08:43 +00:00
|
|
|
lgSignalEvent(e_startup);
|
2019-12-09 16:30:04 +00:00
|
|
|
|
2018-12-12 14:21:56 +00:00
|
|
|
struct timespec time;
|
2020-10-29 15:36:12 +00:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &time);
|
|
|
|
|
2021-01-04 01:06:54 +00:00
|
|
|
while(g_state.state != APP_STATE_SHUTDOWN)
|
2017-12-19 13:53:45 +00:00
|
|
|
{
|
2020-11-07 23:58:18 +00:00
|
|
|
if (params.fpsMin != 0)
|
2020-05-22 08:39:19 +00:00
|
|
|
{
|
2020-10-26 07:09:45 +00:00
|
|
|
lgWaitEventAbs(e_frame, &time);
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &time);
|
2021-01-04 01:06:54 +00:00
|
|
|
tsAdd(&time, g_state.frameTime);
|
2020-05-22 08:39:19 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 02:40:03 +00:00
|
|
|
int resize = atomic_load(&g_state.lgrResize);
|
|
|
|
if (resize)
|
2018-07-28 04:49:37 +00:00
|
|
|
{
|
2021-01-04 01:06:54 +00:00
|
|
|
if (g_state.lgr)
|
2021-01-18 15:44:56 +00:00
|
|
|
g_state.lgr->on_resize(g_state.lgrData, g_state.windowW, g_state.windowH,
|
|
|
|
g_state.dstRect, params.winRotate);
|
2021-01-04 02:40:03 +00:00
|
|
|
atomic_compare_exchange_weak(&g_state.lgrResize, &resize, 0);
|
2018-07-28 04:49:37 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 15:44:56 +00:00
|
|
|
if (!g_state.lgr->render(g_state.lgrData, g_state.window, params.winRotate))
|
2018-05-28 05:30:04 +00:00
|
|
|
break;
|
2018-05-23 23:01:53 +00:00
|
|
|
|
2018-12-12 14:28:00 +00:00
|
|
|
if (params.showFPS)
|
2018-11-19 18:26:51 +00:00
|
|
|
{
|
2018-12-12 14:28:00 +00:00
|
|
|
const uint64_t t = nanotime();
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.renderTime += t - g_state.lastFrameTime;
|
|
|
|
g_state.lastFrameTime = t;
|
|
|
|
++g_state.renderCount;
|
2018-12-12 14:28:00 +00:00
|
|
|
|
2021-01-04 01:06:54 +00:00
|
|
|
if (g_state.renderTime > 1e9)
|
2018-12-12 14:28:00 +00:00
|
|
|
{
|
2021-01-04 01:06:54 +00:00
|
|
|
const float avgUPS = 1000.0f / (((float)g_state.renderTime /
|
|
|
|
atomic_exchange_explicit(&g_state.frameCount, 0, memory_order_acquire)) /
|
2020-10-26 07:09:45 +00:00
|
|
|
1e6f);
|
|
|
|
|
2021-01-04 01:06:54 +00:00
|
|
|
const float avgFPS = 1000.0f / (((float)g_state.renderTime /
|
|
|
|
g_state.renderCount) /
|
2020-10-26 07:09:45 +00:00
|
|
|
1e6f);
|
|
|
|
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.lgr->update_fps(g_state.lgrData, avgUPS, avgFPS);
|
2018-12-12 14:21:56 +00:00
|
|
|
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.renderTime = 0;
|
|
|
|
g_state.renderCount = 0;
|
2018-12-12 14:28:00 +00:00
|
|
|
}
|
2018-11-19 18:26:51 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 01:06:54 +00:00
|
|
|
if (!g_state.resizeDone && g_state.resizeTimeout < microtime())
|
2020-01-03 04:53:44 +00:00
|
|
|
{
|
|
|
|
SDL_SetWindowSize(
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.window,
|
|
|
|
g_state.dstRect.w,
|
|
|
|
g_state.dstRect.h
|
2020-01-03 04:53:44 +00:00
|
|
|
);
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.resizeDone = true;
|
2020-01-03 04:53:44 +00:00
|
|
|
}
|
2017-12-19 13:53:45 +00:00
|
|
|
}
|
2018-01-24 12:46:11 +00:00
|
|
|
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.state = APP_STATE_SHUTDOWN;
|
2019-08-30 01:50:43 +00:00
|
|
|
|
|
|
|
if (t_cursor)
|
2020-01-02 12:59:06 +00:00
|
|
|
lgJoinThread(t_cursor, NULL);
|
2019-08-30 01:50:43 +00:00
|
|
|
|
2020-01-02 12:59:06 +00:00
|
|
|
if (t_frame)
|
|
|
|
lgJoinThread(t_frame, NULL);
|
2019-08-30 01:36:28 +00:00
|
|
|
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.lgr->deinitialize(g_state.lgrData);
|
|
|
|
g_state.lgr = NULL;
|
2018-01-24 12:46:11 +00:00
|
|
|
return 0;
|
2017-12-19 13:53:45 +00:00
|
|
|
}
|
2017-12-15 08:13:36 +00:00
|
|
|
|
2019-03-28 10:23:24 +00:00
|
|
|
static int cursorThread(void * unused)
|
2017-12-19 13:53:45 +00:00
|
|
|
{
|
2020-01-09 09:32:42 +00:00
|
|
|
LGMP_STATUS status;
|
2020-01-10 00:00:46 +00:00
|
|
|
PLGMPClientQueue queue;
|
2017-12-15 08:13:36 +00:00
|
|
|
LG_RendererCursor cursorType = LG_CURSOR_COLOR;
|
2017-12-15 01:02:08 +00:00
|
|
|
|
2020-01-02 13:08:43 +00:00
|
|
|
lgWaitEvent(e_startup, TIMEOUT_INFINITE);
|
2020-01-09 09:32:42 +00:00
|
|
|
|
|
|
|
// subscribe to the pointer queue
|
2021-01-04 01:06:54 +00:00
|
|
|
while(g_state.state == APP_STATE_RUNNING)
|
2017-12-06 15:37:46 +00:00
|
|
|
{
|
2021-01-04 01:06:54 +00:00
|
|
|
status = lgmpClientSubscribe(g_state.lgmp, LGMP_Q_POINTER, &queue);
|
2020-01-09 09:32:42 +00:00
|
|
|
if (status == LGMP_OK)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (status == LGMP_ERR_NO_SUCH_QUEUE)
|
2017-12-20 14:56:59 +00:00
|
|
|
{
|
2020-01-09 09:32:42 +00:00
|
|
|
usleep(1000);
|
2017-12-20 14:56:59 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-01-09 09:32:42 +00:00
|
|
|
DEBUG_ERROR("lgmpClientSubscribe Failed: %s", lgmpStatusString(status));
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.state = APP_STATE_SHUTDOWN;
|
2020-01-09 09:32:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-01-04 01:06:54 +00:00
|
|
|
while(g_state.state == APP_STATE_RUNNING)
|
2020-01-09 09:32:42 +00:00
|
|
|
{
|
|
|
|
LGMPMessage msg;
|
|
|
|
if ((status = lgmpClientProcess(queue, &msg)) != LGMP_OK)
|
2019-01-11 12:59:46 +00:00
|
|
|
{
|
2020-01-09 09:32:42 +00:00
|
|
|
if (status == LGMP_ERR_QUEUE_EMPTY)
|
|
|
|
{
|
2021-01-08 10:46:34 +00:00
|
|
|
if (g_cursor.redraw && g_cursor.guest.valid)
|
2020-01-10 19:03:16 +00:00
|
|
|
{
|
2021-01-04 01:04:43 +00:00
|
|
|
g_cursor.redraw = false;
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.lgr->on_mouse_event
|
2020-01-10 19:03:16 +00:00
|
|
|
(
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.lgrData,
|
2021-01-04 01:04:43 +00:00
|
|
|
g_cursor.guest.visible && g_cursor.draw,
|
|
|
|
g_cursor.guest.x,
|
|
|
|
g_cursor.guest.y
|
2020-01-10 19:03:16 +00:00
|
|
|
);
|
2020-08-20 06:05:55 +00:00
|
|
|
|
|
|
|
lgSignalEvent(e_frame);
|
2020-01-10 19:03:16 +00:00
|
|
|
}
|
|
|
|
|
2021-01-07 21:27:12 +00:00
|
|
|
const struct timespec req =
|
|
|
|
{
|
|
|
|
.tv_sec = 0,
|
|
|
|
.tv_nsec = params.cursorPollInterval * 1000L
|
|
|
|
};
|
|
|
|
|
|
|
|
struct timespec rem;
|
|
|
|
while(nanosleep(&req, &rem) < 0)
|
|
|
|
if (errno != -EINTR)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("nanosleep failed");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-01-09 09:32:42 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-08-11 04:30:44 +00:00
|
|
|
if (status == LGMP_ERR_INVALID_SESSION)
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.state = APP_STATE_RESTART;
|
2020-08-11 04:45:43 +00:00
|
|
|
else
|
2020-08-11 05:14:58 +00:00
|
|
|
{
|
2020-08-11 04:45:43 +00:00
|
|
|
DEBUG_ERROR("lgmpClientProcess Failed: %s", lgmpStatusString(status));
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.state = APP_STATE_SHUTDOWN;
|
2020-08-11 05:14:58 +00:00
|
|
|
}
|
2020-01-09 09:32:42 +00:00
|
|
|
break;
|
2019-01-11 12:59:46 +00:00
|
|
|
}
|
|
|
|
|
2020-01-09 09:32:42 +00:00
|
|
|
KVMFRCursor * cursor = (KVMFRCursor *)msg.mem;
|
2020-01-10 18:22:12 +00:00
|
|
|
|
2021-01-04 01:04:43 +00:00
|
|
|
g_cursor.guest.visible =
|
2020-01-26 15:07:32 +00:00
|
|
|
msg.udata & CURSOR_FLAG_VISIBLE;
|
|
|
|
|
2020-01-26 06:30:16 +00:00
|
|
|
if (msg.udata & CURSOR_FLAG_SHAPE)
|
2019-01-11 12:59:46 +00:00
|
|
|
{
|
2020-01-09 10:18:35 +00:00
|
|
|
switch(cursor->type)
|
|
|
|
{
|
|
|
|
case CURSOR_TYPE_COLOR : cursorType = LG_CURSOR_COLOR ; break;
|
|
|
|
case CURSOR_TYPE_MONOCHROME : cursorType = LG_CURSOR_MONOCHROME ; break;
|
|
|
|
case CURSOR_TYPE_MASKED_COLOR: cursorType = LG_CURSOR_MASKED_COLOR; break;
|
|
|
|
default:
|
|
|
|
DEBUG_ERROR("Invalid cursor type");
|
|
|
|
lgmpClientMessageDone(queue);
|
|
|
|
continue;
|
|
|
|
}
|
2019-01-11 12:59:46 +00:00
|
|
|
|
2021-01-04 01:04:43 +00:00
|
|
|
g_cursor.guest.hx = cursor->hx;
|
|
|
|
g_cursor.guest.hy = cursor->hy;
|
2020-08-20 03:46:18 +00:00
|
|
|
|
2020-01-09 10:18:35 +00:00
|
|
|
const uint8_t * data = (const uint8_t *)(cursor + 1);
|
2021-01-04 01:06:54 +00:00
|
|
|
if (!g_state.lgr->on_mouse_shape(
|
|
|
|
g_state.lgrData,
|
2020-01-09 10:18:35 +00:00
|
|
|
cursorType,
|
|
|
|
cursor->width,
|
|
|
|
cursor->height,
|
|
|
|
cursor->pitch,
|
|
|
|
data)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to update mouse shape");
|
2020-01-09 09:32:42 +00:00
|
|
|
lgmpClientMessageDone(queue);
|
|
|
|
continue;
|
2020-01-09 10:18:35 +00:00
|
|
|
}
|
2020-01-09 09:32:42 +00:00
|
|
|
}
|
2017-11-25 09:21:57 +00:00
|
|
|
|
2020-08-20 04:52:24 +00:00
|
|
|
if (msg.udata & CURSOR_FLAG_POSITION)
|
|
|
|
{
|
2021-01-08 12:12:15 +00:00
|
|
|
bool valid = g_cursor.guest.valid;
|
2021-01-08 08:41:10 +00:00
|
|
|
g_cursor.guest.x = cursor->x;
|
|
|
|
g_cursor.guest.y = cursor->y;
|
2021-01-04 01:04:43 +00:00
|
|
|
g_cursor.guest.valid = true;
|
2021-01-08 12:12:15 +00:00
|
|
|
|
2021-01-09 07:01:08 +00:00
|
|
|
// if the state just became valid
|
2021-01-15 09:30:03 +00:00
|
|
|
if (valid != true && app_inputEnabled())
|
2021-01-08 12:12:15 +00:00
|
|
|
alignToGuest();
|
2020-08-20 04:52:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-09 09:32:42 +00:00
|
|
|
lgmpClientMessageDone(queue);
|
2021-01-04 01:04:43 +00:00
|
|
|
g_cursor.redraw = false;
|
2020-01-26 06:30:16 +00:00
|
|
|
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.lgr->on_mouse_event
|
2020-01-26 15:11:21 +00:00
|
|
|
(
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.lgrData,
|
2021-01-04 01:04:43 +00:00
|
|
|
g_cursor.guest.visible && g_cursor.draw,
|
|
|
|
g_cursor.guest.x,
|
|
|
|
g_cursor.guest.y
|
2020-01-26 15:11:21 +00:00
|
|
|
);
|
2020-08-20 05:50:33 +00:00
|
|
|
|
|
|
|
if (params.mouseRedraw)
|
|
|
|
lgSignalEvent(e_frame);
|
2017-12-19 13:53:45 +00:00
|
|
|
}
|
2017-12-15 07:47:44 +00:00
|
|
|
|
2020-01-09 09:32:42 +00:00
|
|
|
lgmpClientUnsubscribe(&queue);
|
2017-12-19 13:53:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2017-12-15 08:13:36 +00:00
|
|
|
|
2019-03-28 10:23:24 +00:00
|
|
|
static int frameThread(void * unused)
|
2017-12-19 13:53:45 +00:00
|
|
|
{
|
2020-10-29 11:57:04 +00:00
|
|
|
struct DMAFrameInfo
|
|
|
|
{
|
|
|
|
KVMFRFrame * frame;
|
|
|
|
size_t dataSize;
|
|
|
|
int fd;
|
|
|
|
};
|
|
|
|
|
2020-01-10 00:00:46 +00:00
|
|
|
LGMP_STATUS status;
|
|
|
|
PLGMPClientQueue queue;
|
2017-12-15 08:13:36 +00:00
|
|
|
|
2020-10-12 07:52:37 +00:00
|
|
|
uint32_t formatVer = 0;
|
|
|
|
bool formatValid = false;
|
2020-10-29 11:57:04 +00:00
|
|
|
size_t dataSize;
|
2020-10-12 07:52:37 +00:00
|
|
|
LG_RendererFormat lgrFormat;
|
|
|
|
|
2020-10-30 07:48:41 +00:00
|
|
|
struct DMAFrameInfo dmaInfo[LGMP_Q_FRAME_LEN] = {0};
|
2020-10-29 15:32:25 +00:00
|
|
|
const bool useDMA =
|
|
|
|
params.allowDMA &&
|
2021-01-04 01:06:54 +00:00
|
|
|
ivshmemHasDMA(&g_state.shm) &&
|
|
|
|
g_state.lgr->supports &&
|
|
|
|
g_state.lgr->supports(g_state.lgrData, LG_SUPPORTS_DMABUF);
|
2020-10-29 11:57:04 +00:00
|
|
|
|
|
|
|
if (useDMA)
|
|
|
|
DEBUG_INFO("Using DMA buffer support");
|
|
|
|
|
2018-10-04 07:03:09 +00:00
|
|
|
SDL_SetThreadPriority(SDL_THREAD_PRIORITY_HIGH);
|
2020-01-02 13:08:43 +00:00
|
|
|
lgWaitEvent(e_startup, TIMEOUT_INFINITE);
|
2021-01-04 01:06:54 +00:00
|
|
|
if (g_state.state != APP_STATE_RUNNING)
|
2020-01-27 01:25:47 +00:00
|
|
|
return 0;
|
2020-01-09 09:32:42 +00:00
|
|
|
|
|
|
|
// subscribe to the frame queue
|
2021-01-04 01:06:54 +00:00
|
|
|
while(g_state.state == APP_STATE_RUNNING)
|
2017-12-19 13:53:45 +00:00
|
|
|
{
|
2021-01-04 01:06:54 +00:00
|
|
|
status = lgmpClientSubscribe(g_state.lgmp, LGMP_Q_FRAME, &queue);
|
2020-01-09 09:32:42 +00:00
|
|
|
if (status == LGMP_OK)
|
|
|
|
break;
|
2018-01-03 22:25:17 +00:00
|
|
|
|
2020-01-09 09:32:42 +00:00
|
|
|
if (status == LGMP_ERR_NO_SUCH_QUEUE)
|
|
|
|
{
|
|
|
|
usleep(1000);
|
2017-12-19 13:53:45 +00:00
|
|
|
continue;
|
|
|
|
}
|
2017-10-19 04:15:49 +00:00
|
|
|
|
2020-01-09 09:32:42 +00:00
|
|
|
DEBUG_ERROR("lgmpClientSubscribe Failed: %s", lgmpStatusString(status));
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.state = APP_STATE_SHUTDOWN;
|
2020-01-09 09:32:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-01-04 01:06:54 +00:00
|
|
|
while(g_state.state == APP_STATE_RUNNING && !g_state.stopVideo)
|
2020-01-09 09:32:42 +00:00
|
|
|
{
|
|
|
|
LGMPMessage msg;
|
|
|
|
if ((status = lgmpClientProcess(queue, &msg)) != LGMP_OK)
|
|
|
|
{
|
|
|
|
if (status == LGMP_ERR_QUEUE_EMPTY)
|
|
|
|
{
|
2021-01-07 21:27:12 +00:00
|
|
|
const struct timespec req =
|
|
|
|
{
|
|
|
|
.tv_sec = 0,
|
|
|
|
.tv_nsec = params.framePollInterval * 1000L
|
|
|
|
};
|
|
|
|
|
|
|
|
struct timespec rem;
|
|
|
|
while(nanosleep(&req, &rem) < 0)
|
|
|
|
if (errno != -EINTR)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("nanosleep failed");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-01-09 09:32:42 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-08-11 04:30:44 +00:00
|
|
|
if (status == LGMP_ERR_INVALID_SESSION)
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.state = APP_STATE_RESTART;
|
2020-08-11 04:45:43 +00:00
|
|
|
else
|
2020-08-11 05:14:58 +00:00
|
|
|
{
|
2020-08-11 04:45:43 +00:00
|
|
|
DEBUG_ERROR("lgmpClientProcess Failed: %s", lgmpStatusString(status));
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.state = APP_STATE_SHUTDOWN;
|
2020-08-11 05:14:58 +00:00
|
|
|
}
|
2020-01-09 09:32:42 +00:00
|
|
|
break;
|
2017-11-25 09:21:57 +00:00
|
|
|
}
|
2017-12-12 15:22:47 +00:00
|
|
|
|
2020-10-29 11:57:04 +00:00
|
|
|
KVMFRFrame * frame = (KVMFRFrame *)msg.mem;
|
|
|
|
struct DMAFrameInfo *dma = NULL;
|
2020-01-09 09:32:42 +00:00
|
|
|
|
2020-10-12 07:52:37 +00:00
|
|
|
if (!formatValid || frame->formatVer != formatVer)
|
2017-12-12 15:22:47 +00:00
|
|
|
{
|
2020-10-12 07:52:37 +00:00
|
|
|
// setup the renderer format with the frame format details
|
|
|
|
lgrFormat.type = frame->type;
|
|
|
|
lgrFormat.width = frame->width;
|
|
|
|
lgrFormat.height = frame->height;
|
|
|
|
lgrFormat.stride = frame->stride;
|
|
|
|
lgrFormat.pitch = frame->pitch;
|
|
|
|
|
2021-01-18 15:44:56 +00:00
|
|
|
switch(frame->rotation)
|
|
|
|
{
|
|
|
|
case FRAME_ROT_0 : lgrFormat.rotate = LG_ROTATE_0 ; break;
|
|
|
|
case FRAME_ROT_90 : lgrFormat.rotate = LG_ROTATE_90 ; break;
|
|
|
|
case FRAME_ROT_180: lgrFormat.rotate = LG_ROTATE_180; break;
|
|
|
|
case FRAME_ROT_270: lgrFormat.rotate = LG_ROTATE_270; break;
|
|
|
|
}
|
|
|
|
g_state.rotate = lgrFormat.rotate;
|
|
|
|
|
2020-10-29 11:57:04 +00:00
|
|
|
bool error = false;
|
2020-10-12 07:52:37 +00:00
|
|
|
switch(frame->type)
|
|
|
|
{
|
|
|
|
case FRAME_TYPE_RGBA:
|
|
|
|
case FRAME_TYPE_BGRA:
|
|
|
|
case FRAME_TYPE_RGBA10:
|
|
|
|
dataSize = lgrFormat.height * lgrFormat.pitch;
|
|
|
|
lgrFormat.bpp = 32;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FRAME_TYPE_RGBA16F:
|
|
|
|
dataSize = lgrFormat.height * lgrFormat.pitch;
|
|
|
|
lgrFormat.bpp = 64;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
DEBUG_ERROR("Unsupported frameType");
|
|
|
|
error = true;
|
|
|
|
break;
|
|
|
|
}
|
2018-07-27 22:41:15 +00:00
|
|
|
|
2020-10-12 07:52:37 +00:00
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
lgmpClientMessageDone(queue);
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.state = APP_STATE_SHUTDOWN;
|
2017-12-19 13:53:45 +00:00
|
|
|
break;
|
2020-10-12 07:52:37 +00:00
|
|
|
}
|
2017-12-12 16:08:13 +00:00
|
|
|
|
2020-10-12 07:52:37 +00:00
|
|
|
formatValid = true;
|
|
|
|
formatVer = frame->formatVer;
|
2020-10-12 08:43:29 +00:00
|
|
|
|
2021-01-18 15:44:56 +00:00
|
|
|
DEBUG_INFO("Format: %s %ux%u stride:%u pitch:%u rotation:%d",
|
2020-10-12 08:43:29 +00:00
|
|
|
FrameTypeStr[frame->type],
|
|
|
|
frame->width, frame->height,
|
2021-01-18 15:44:56 +00:00
|
|
|
frame->stride, frame->pitch,
|
|
|
|
frame->rotation);
|
2021-01-15 01:40:59 +00:00
|
|
|
|
2021-01-04 01:06:54 +00:00
|
|
|
if (!g_state.lgr->on_frame_format(g_state.lgrData, lgrFormat, useDMA))
|
2020-10-12 08:43:29 +00:00
|
|
|
{
|
|
|
|
DEBUG_ERROR("renderer failed to configure format");
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.state = APP_STATE_SHUTDOWN;
|
2020-10-12 08:43:29 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-01-04 08:16:18 +00:00
|
|
|
|
|
|
|
g_state.srcSize.x = lgrFormat.width;
|
|
|
|
g_state.srcSize.y = lgrFormat.height;
|
|
|
|
g_state.haveSrcSize = true;
|
|
|
|
if (params.autoResize)
|
|
|
|
SDL_SetWindowSize(g_state.window, lgrFormat.width, lgrFormat.height);
|
|
|
|
|
2021-01-04 22:48:30 +00:00
|
|
|
g_cursor.guest.dpiScale = frame->mouseScalePercent;
|
2021-01-04 08:16:18 +00:00
|
|
|
updatePositionInfo();
|
2017-12-19 13:53:45 +00:00
|
|
|
}
|
2017-12-15 08:13:36 +00:00
|
|
|
|
2020-10-29 11:57:04 +00:00
|
|
|
if (useDMA)
|
|
|
|
{
|
|
|
|
/* find the existing dma buffer if it exists */
|
|
|
|
for(int i = 0; i < sizeof(dmaInfo) / sizeof(struct DMAFrameInfo); ++i)
|
|
|
|
{
|
|
|
|
if (dmaInfo[i].frame == frame)
|
|
|
|
{
|
|
|
|
dma = &dmaInfo[i];
|
|
|
|
/* if it's too small close it */
|
|
|
|
if (dma->dataSize < dataSize)
|
|
|
|
{
|
|
|
|
close(dma->fd);
|
|
|
|
dma->fd = -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* otherwise find a free buffer for use */
|
|
|
|
if (!dma)
|
|
|
|
for(int i = 0; i < sizeof(dmaInfo) / sizeof(struct DMAFrameInfo); ++i)
|
|
|
|
{
|
|
|
|
if (!dmaInfo[i].frame)
|
|
|
|
{
|
|
|
|
dma = &dmaInfo[i];
|
|
|
|
dma->frame = frame;
|
|
|
|
dma->fd = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* open the buffer */
|
|
|
|
if (dma->fd == -1)
|
|
|
|
{
|
2021-01-04 01:06:54 +00:00
|
|
|
const uintptr_t pos = (uintptr_t)msg.mem - (uintptr_t)g_state.shm.mem;
|
2020-10-29 11:57:04 +00:00
|
|
|
const uintptr_t offset = (uintptr_t)frame->offset + FrameBufferStructSize;
|
|
|
|
|
|
|
|
dma->dataSize = dataSize;
|
2021-01-04 01:06:54 +00:00
|
|
|
dma->fd = ivshmemGetDMABuf(&g_state.shm, pos + offset, dataSize);
|
2020-10-29 11:57:04 +00:00
|
|
|
if (dma->fd < 0)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to get the DMA buffer for the frame");
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.state = APP_STATE_SHUTDOWN;
|
2020-10-29 11:57:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-13 08:30:49 +00:00
|
|
|
FrameBuffer * fb = (FrameBuffer *)(((uint8_t*)frame) + frame->offset);
|
2021-01-04 01:06:54 +00:00
|
|
|
if (!g_state.lgr->on_frame(g_state.lgrData, fb, useDMA ? dma->fd : -1))
|
2017-12-19 13:53:45 +00:00
|
|
|
{
|
2020-10-12 08:43:29 +00:00
|
|
|
lgmpClientMessageDone(queue);
|
|
|
|
DEBUG_ERROR("renderer on frame returned failure");
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.state = APP_STATE_SHUTDOWN;
|
2017-12-19 13:53:45 +00:00
|
|
|
break;
|
2017-12-12 15:22:47 +00:00
|
|
|
}
|
2020-05-22 08:00:18 +00:00
|
|
|
|
2021-01-04 01:06:54 +00:00
|
|
|
atomic_fetch_add_explicit(&g_state.frameCount, 1, memory_order_relaxed);
|
2020-10-26 07:09:45 +00:00
|
|
|
lgSignalEvent(e_frame);
|
2020-08-09 05:14:17 +00:00
|
|
|
lgmpClientMessageDone(queue);
|
2017-10-19 04:15:49 +00:00
|
|
|
}
|
|
|
|
|
2020-01-09 09:32:42 +00:00
|
|
|
lgmpClientUnsubscribe(&queue);
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.lgr->on_restart(g_state.lgrData);
|
2020-10-14 08:40:20 +00:00
|
|
|
|
2020-10-29 11:57:04 +00:00
|
|
|
if (useDMA)
|
|
|
|
{
|
|
|
|
for(int i = 0; i < sizeof(dmaInfo) / sizeof(struct DMAFrameInfo); ++i)
|
|
|
|
if (dmaInfo[i].fd >= 0)
|
|
|
|
close(dmaInfo[i].fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-19 04:15:49 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int spiceThread(void * arg)
|
|
|
|
{
|
2021-01-04 01:06:54 +00:00
|
|
|
while(g_state.state != APP_STATE_SHUTDOWN)
|
2020-01-18 19:51:21 +00:00
|
|
|
if (!spice_process(1000))
|
2017-10-19 04:15:49 +00:00
|
|
|
{
|
2021-01-04 01:06:54 +00:00
|
|
|
if (g_state.state != APP_STATE_SHUTDOWN)
|
2017-10-19 07:50:42 +00:00
|
|
|
{
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.state = APP_STATE_SHUTDOWN;
|
2017-10-19 07:50:42 +00:00
|
|
|
DEBUG_ERROR("failed to process spice messages");
|
|
|
|
}
|
2017-10-19 04:15:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.state = APP_STATE_SHUTDOWN;
|
2017-10-19 04:15:49 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-02-24 00:43:32 +00:00
|
|
|
static LG_ClipboardData spice_type_to_clipboard_type(const SpiceDataType type)
|
|
|
|
{
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case SPICE_DATA_TEXT: return LG_CLIPBOARD_DATA_TEXT; break;
|
|
|
|
case SPICE_DATA_PNG : return LG_CLIPBOARD_DATA_PNG ; break;
|
|
|
|
case SPICE_DATA_BMP : return LG_CLIPBOARD_DATA_BMP ; break;
|
|
|
|
case SPICE_DATA_TIFF: return LG_CLIPBOARD_DATA_TIFF; break;
|
|
|
|
case SPICE_DATA_JPEG: return LG_CLIPBOARD_DATA_JPEG; break;
|
|
|
|
default:
|
|
|
|
DEBUG_ERROR("invalid spice data type");
|
|
|
|
return LG_CLIPBOARD_DATA_NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static SpiceDataType clipboard_type_to_spice_type(const LG_ClipboardData type)
|
|
|
|
{
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case LG_CLIPBOARD_DATA_TEXT: return SPICE_DATA_TEXT; break;
|
|
|
|
case LG_CLIPBOARD_DATA_PNG : return SPICE_DATA_PNG ; break;
|
|
|
|
case LG_CLIPBOARD_DATA_BMP : return SPICE_DATA_BMP ; break;
|
|
|
|
case LG_CLIPBOARD_DATA_TIFF: return SPICE_DATA_TIFF; break;
|
|
|
|
case LG_CLIPBOARD_DATA_JPEG: return SPICE_DATA_JPEG; break;
|
|
|
|
default:
|
|
|
|
DEBUG_ERROR("invalid clipboard data type");
|
|
|
|
return SPICE_DATA_NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-15 09:30:03 +00:00
|
|
|
void app_clipboardRelease(void)
|
2019-02-24 00:43:32 +00:00
|
|
|
{
|
2019-02-25 16:06:53 +00:00
|
|
|
if (!params.clipboardToVM)
|
|
|
|
return;
|
|
|
|
|
2019-02-24 00:43:32 +00:00
|
|
|
spice_clipboard_release();
|
|
|
|
}
|
|
|
|
|
2021-01-15 09:30:03 +00:00
|
|
|
void app_clipboardNotify(const LG_ClipboardData type, size_t size)
|
2019-02-24 00:43:32 +00:00
|
|
|
{
|
2019-02-25 16:06:53 +00:00
|
|
|
if (!params.clipboardToVM)
|
|
|
|
return;
|
|
|
|
|
2019-02-24 00:43:32 +00:00
|
|
|
if (type == LG_CLIPBOARD_DATA_NONE)
|
|
|
|
{
|
|
|
|
spice_clipboard_release();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.cbType = clipboard_type_to_spice_type(type);
|
|
|
|
g_state.cbChunked = size > 0;
|
|
|
|
g_state.cbXfer = size;
|
2020-12-03 01:01:51 +00:00
|
|
|
|
2021-01-04 01:06:54 +00:00
|
|
|
spice_clipboard_grab(g_state.cbType);
|
2020-12-03 01:01:51 +00:00
|
|
|
|
|
|
|
if (size)
|
2021-01-04 01:06:54 +00:00
|
|
|
spice_clipboard_data_start(g_state.cbType, size);
|
2019-02-24 00:43:32 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 09:30:03 +00:00
|
|
|
void app_clipboardData(const LG_ClipboardData type, uint8_t * data, size_t size)
|
2019-02-24 00:43:32 +00:00
|
|
|
{
|
2019-02-25 16:06:53 +00:00
|
|
|
if (!params.clipboardToVM)
|
|
|
|
return;
|
|
|
|
|
2021-01-04 01:06:54 +00:00
|
|
|
if (g_state.cbChunked && size > g_state.cbXfer)
|
2019-02-24 04:35:31 +00:00
|
|
|
{
|
2020-12-03 01:01:51 +00:00
|
|
|
DEBUG_ERROR("refusing to send more then cbXfer bytes for chunked xfer");
|
2021-01-04 01:06:54 +00:00
|
|
|
size = g_state.cbXfer;
|
2019-02-24 04:35:31 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 01:06:54 +00:00
|
|
|
if (!g_state.cbChunked)
|
|
|
|
spice_clipboard_data_start(g_state.cbType, size);
|
2020-12-03 01:01:51 +00:00
|
|
|
|
2021-01-04 01:06:54 +00:00
|
|
|
spice_clipboard_data(g_state.cbType, data, (uint32_t)size);
|
|
|
|
g_state.cbXfer -= size;
|
2019-02-24 00:43:32 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 09:30:03 +00:00
|
|
|
void app_clipboardRequest(const LG_ClipboardReplyFn replyFn, void * opaque)
|
2019-02-22 17:24:30 +00:00
|
|
|
{
|
2019-02-25 16:06:53 +00:00
|
|
|
if (!params.clipboardToLocal)
|
|
|
|
return;
|
|
|
|
|
2021-01-10 00:36:36 +00:00
|
|
|
struct CBRequest * cbr = (struct CBRequest *)malloc(sizeof(struct CBRequest));
|
2019-02-24 18:43:18 +00:00
|
|
|
|
2021-01-04 01:06:54 +00:00
|
|
|
cbr->type = g_state.cbType;
|
2019-02-24 18:43:18 +00:00
|
|
|
cbr->replyFn = replyFn;
|
|
|
|
cbr->opaque = opaque;
|
2021-01-04 01:06:54 +00:00
|
|
|
ll_push(g_state.cbRequestList, cbr);
|
2019-02-22 17:24:30 +00:00
|
|
|
|
2021-01-04 01:06:54 +00:00
|
|
|
spice_clipboard_request(g_state.cbType);
|
2019-02-22 17:24:30 +00:00
|
|
|
}
|
2019-02-22 07:59:45 +00:00
|
|
|
|
2019-02-22 08:51:14 +00:00
|
|
|
void spiceClipboardNotice(const SpiceDataType type)
|
2019-02-22 07:59:45 +00:00
|
|
|
{
|
2019-02-25 16:06:53 +00:00
|
|
|
if (!params.clipboardToLocal)
|
|
|
|
return;
|
|
|
|
|
2021-01-15 09:30:03 +00:00
|
|
|
if (!g_state.cbAvailable)
|
2019-02-22 17:24:30 +00:00
|
|
|
return;
|
|
|
|
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.cbType = type;
|
2021-01-15 09:30:03 +00:00
|
|
|
g_state.ds->cbNotice(spice_type_to_clipboard_type(type));
|
2019-02-22 07:59:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void spiceClipboardData(const SpiceDataType type, uint8_t * buffer, uint32_t size)
|
|
|
|
{
|
2019-02-25 16:06:53 +00:00
|
|
|
if (!params.clipboardToLocal)
|
|
|
|
return;
|
|
|
|
|
2019-02-22 17:24:30 +00:00
|
|
|
if (type == SPICE_DATA_TEXT)
|
2019-02-22 07:59:45 +00:00
|
|
|
{
|
2019-02-22 17:24:30 +00:00
|
|
|
// dos2unix
|
2019-02-24 18:43:18 +00:00
|
|
|
uint8_t * p = buffer;
|
|
|
|
uint32_t newSize = size;
|
2019-02-22 17:24:30 +00:00
|
|
|
for(uint32_t i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
uint8_t c = buffer[i];
|
2019-02-24 18:43:18 +00:00
|
|
|
if (c == '\r')
|
|
|
|
{
|
|
|
|
--newSize;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*p++ = c;
|
2019-02-22 17:24:30 +00:00
|
|
|
}
|
2019-02-24 18:43:18 +00:00
|
|
|
size = newSize;
|
2019-02-22 07:59:45 +00:00
|
|
|
}
|
2019-02-22 17:24:30 +00:00
|
|
|
|
2019-02-24 18:43:18 +00:00
|
|
|
struct CBRequest * cbr;
|
2021-01-04 01:06:54 +00:00
|
|
|
if (ll_shift(g_state.cbRequestList, (void **)&cbr))
|
2019-02-24 18:43:18 +00:00
|
|
|
{
|
2020-10-29 02:31:14 +00:00
|
|
|
cbr->replyFn(cbr->opaque, spice_type_to_clipboard_type(type), buffer, size);
|
2019-02-24 18:43:18 +00:00
|
|
|
free(cbr);
|
|
|
|
}
|
2019-02-22 07:59:45 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 06:05:26 +00:00
|
|
|
void spiceClipboardRelease(void)
|
2019-02-24 00:43:32 +00:00
|
|
|
{
|
2019-02-25 16:06:53 +00:00
|
|
|
if (!params.clipboardToLocal)
|
|
|
|
return;
|
|
|
|
|
2021-01-15 09:30:03 +00:00
|
|
|
if (g_state.cbAvailable)
|
|
|
|
g_state.ds->cbRelease();
|
2019-02-24 00:43:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void spiceClipboardRequest(const SpiceDataType type)
|
|
|
|
{
|
2019-02-25 16:06:53 +00:00
|
|
|
if (!params.clipboardToVM)
|
|
|
|
return;
|
|
|
|
|
2021-01-15 09:30:03 +00:00
|
|
|
if (g_state.cbAvailable)
|
|
|
|
g_state.ds->cbRequest(spice_type_to_clipboard_type(type));
|
2019-02-24 00:43:32 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 19:00:59 +00:00
|
|
|
static bool warpPointer(int x, int y, bool exiting)
|
2020-08-20 04:52:24 +00:00
|
|
|
{
|
2021-01-18 18:38:25 +00:00
|
|
|
if (!g_cursor.inWindow)
|
2021-01-18 19:00:59 +00:00
|
|
|
return false;
|
2021-01-18 18:38:25 +00:00
|
|
|
|
2021-01-04 04:40:23 +00:00
|
|
|
if (g_cursor.warpState == WARP_STATE_OFF)
|
2021-01-18 19:00:59 +00:00
|
|
|
return false;
|
2020-08-20 04:52:24 +00:00
|
|
|
|
2021-01-15 09:30:03 +00:00
|
|
|
if (exiting)
|
2021-01-04 01:04:43 +00:00
|
|
|
g_cursor.warpState = WARP_STATE_OFF;
|
2020-08-20 04:52:24 +00:00
|
|
|
|
2021-01-08 02:17:42 +00:00
|
|
|
if (g_cursor.pos.x == x && g_cursor.pos.y == y)
|
2021-01-18 19:00:59 +00:00
|
|
|
return true;
|
2021-01-06 11:11:28 +00:00
|
|
|
|
2021-01-16 18:10:52 +00:00
|
|
|
g_state.ds->warpPointer(x, y, exiting);
|
2021-01-18 19:00:59 +00:00
|
|
|
return true;
|
2020-08-20 04:52:24 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 06:32:28 +00:00
|
|
|
static bool isValidCursorLocation(int x, int y)
|
|
|
|
{
|
|
|
|
const int displays = SDL_GetNumVideoDisplays();
|
|
|
|
for(int i = 0; i < displays; ++i)
|
|
|
|
{
|
|
|
|
SDL_Rect r;
|
|
|
|
SDL_GetDisplayBounds(i, &r);
|
|
|
|
if ((x >= r.x && x < r.x + r.w) &&
|
|
|
|
(y >= r.y && y < r.y + r.h))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-08 02:17:42 +00:00
|
|
|
static void cursorToInt(double ex, double ey, int *x, int *y)
|
|
|
|
{
|
2021-01-09 03:55:20 +00:00
|
|
|
/* only smooth if enabled and not using raw mode */
|
|
|
|
if (params.mouseSmoothing && !(g_cursor.grab && params.rawMouse))
|
|
|
|
{
|
|
|
|
static struct DoublePoint last = { 0 };
|
2021-01-16 16:11:41 +00:00
|
|
|
|
|
|
|
/* only apply smoothing to small deltas */
|
|
|
|
if (fabs(ex - last.x) < 5.0 && fabs(ey - last.y) < 5.0)
|
|
|
|
{
|
|
|
|
ex = last.x = (last.x + ex) / 2.0;
|
|
|
|
ey = last.y = (last.y + ey) / 2.0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
last.x = ex;
|
|
|
|
last.y = ey;
|
|
|
|
}
|
2021-01-09 03:55:20 +00:00
|
|
|
}
|
|
|
|
|
2021-01-08 02:17:42 +00:00
|
|
|
/* convert to int accumulating the fractional error */
|
2021-01-12 00:48:29 +00:00
|
|
|
ex += g_cursor.acc.x;
|
|
|
|
ey += g_cursor.acc.y;
|
|
|
|
g_cursor.acc.x = modf(ex, &ex);
|
|
|
|
g_cursor.acc.y = modf(ey, &ey);
|
|
|
|
|
|
|
|
*x = (int)ex;
|
|
|
|
*y = (int)ey;
|
2021-01-08 02:17:42 +00:00
|
|
|
}
|
|
|
|
|
2021-01-16 10:01:15 +00:00
|
|
|
static void setCursorInView(bool enable)
|
2021-01-16 09:04:15 +00:00
|
|
|
{
|
2021-01-17 01:53:55 +00:00
|
|
|
// if the state has not changed, don't do anything else
|
|
|
|
if (g_cursor.inView == enable)
|
|
|
|
return;
|
|
|
|
|
2021-01-16 10:01:15 +00:00
|
|
|
if (enable && !g_state.focused)
|
|
|
|
return;
|
|
|
|
|
2021-01-18 17:40:31 +00:00
|
|
|
// do not allow the view to become active if any mouse buttons are being held,
|
|
|
|
// this fixes issues with meta window resizing.
|
|
|
|
if (enable && g_cursor.buttons)
|
|
|
|
return;
|
|
|
|
|
2021-01-17 01:53:55 +00:00
|
|
|
g_cursor.inView = enable;
|
|
|
|
g_cursor.draw = params.alwaysShowCursor ? true : enable;
|
|
|
|
g_cursor.redraw = true;
|
|
|
|
|
2021-01-16 10:17:35 +00:00
|
|
|
/* if the display server does not support warp, then we can not operate in
|
|
|
|
* always relative mode and we should not grab the pointer */
|
|
|
|
bool warpSupport = true;
|
|
|
|
app_getProp(LG_DS_WARP_SUPPORT, &warpSupport);
|
|
|
|
|
2021-01-16 10:01:15 +00:00
|
|
|
g_cursor.warpState = enable ? WARP_STATE_ON : WARP_STATE_OFF;
|
|
|
|
|
|
|
|
if (enable)
|
|
|
|
{
|
|
|
|
if (params.hideMouse)
|
|
|
|
SDL_ShowCursor(SDL_DISABLE);
|
|
|
|
|
2021-01-16 10:17:35 +00:00
|
|
|
if (warpSupport)
|
|
|
|
g_state.ds->grabPointer();
|
2021-01-16 10:01:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (params.hideMouse)
|
|
|
|
SDL_ShowCursor(SDL_ENABLE);
|
|
|
|
|
2021-01-16 10:17:35 +00:00
|
|
|
if (warpSupport)
|
|
|
|
g_state.ds->ungrabPointer();
|
2021-01-17 01:53:55 +00:00
|
|
|
|
|
|
|
if (!g_cursor.grab)
|
|
|
|
g_cursor.realign = true;
|
2021-01-16 10:01:15 +00:00
|
|
|
}
|
2021-01-17 01:53:55 +00:00
|
|
|
|
|
|
|
g_cursor.warpState = WARP_STATE_ON;
|
2021-01-16 09:04:15 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 09:30:03 +00:00
|
|
|
void app_handleMouseGrabbed(double ex, double ey)
|
2021-01-07 13:44:15 +00:00
|
|
|
{
|
2021-01-09 03:39:48 +00:00
|
|
|
int x, y;
|
|
|
|
|
2021-01-16 23:44:30 +00:00
|
|
|
/* do not pass mouse events to the guest if we do not have focus */
|
|
|
|
if (!g_state.focused)
|
|
|
|
return;
|
|
|
|
|
2021-01-09 03:55:20 +00:00
|
|
|
if (params.rawMouse && !g_cursor.useScale)
|
2021-01-07 14:01:53 +00:00
|
|
|
{
|
2021-01-09 03:55:20 +00:00
|
|
|
/* raw unscaled input are always round numbers */
|
|
|
|
x = floor(ex);
|
|
|
|
y = floor(ey);
|
2021-01-09 03:39:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-09 03:55:20 +00:00
|
|
|
/* apply sensitivity */
|
|
|
|
ex = (ex / 10.0) * (g_cursor.sens + 10);
|
|
|
|
ey = (ey / 10.0) * (g_cursor.sens + 10);
|
|
|
|
cursorToInt(ex, ey, &x, &y);
|
2021-01-07 14:01:53 +00:00
|
|
|
}
|
|
|
|
|
2021-01-07 15:52:47 +00:00
|
|
|
if (x == 0 && y == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!spice_mouse_motion(x, y))
|
2021-01-07 13:44:15 +00:00
|
|
|
DEBUG_ERROR("failed to send mouse motion message");
|
|
|
|
}
|
|
|
|
|
2021-01-17 03:23:51 +00:00
|
|
|
void app_handleButtonPress(int button)
|
|
|
|
{
|
|
|
|
if (!app_inputEnabled() || !g_cursor.inView)
|
|
|
|
return;
|
|
|
|
|
2021-01-17 04:13:45 +00:00
|
|
|
g_cursor.buttons |= (1U << button);
|
|
|
|
|
2021-01-17 03:23:51 +00:00
|
|
|
if (!spice_mouse_press(button))
|
|
|
|
DEBUG_ERROR("SDL_MOUSEBUTTONDOWN: failed to send message");
|
|
|
|
}
|
|
|
|
|
|
|
|
void app_handleButtonRelease(int button)
|
|
|
|
{
|
|
|
|
if (!app_inputEnabled())
|
|
|
|
return;
|
|
|
|
|
2021-01-17 04:13:45 +00:00
|
|
|
g_cursor.buttons &= ~(1U << button);
|
|
|
|
|
2021-01-17 03:23:51 +00:00
|
|
|
if (!spice_mouse_release(button))
|
|
|
|
DEBUG_ERROR("SDL_MOUSEBUTTONUP: failed to send message");
|
|
|
|
}
|
|
|
|
|
2021-01-18 02:20:42 +00:00
|
|
|
void app_handleKeyPress(int sc)
|
|
|
|
{
|
|
|
|
if (sc == params.escapeKey && !g_state.escapeActive)
|
|
|
|
{
|
|
|
|
g_state.escapeActive = true;
|
|
|
|
g_state.escapeAction = -1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_state.escapeActive)
|
|
|
|
{
|
|
|
|
g_state.escapeAction = sc;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!app_inputEnabled())
|
|
|
|
return;
|
|
|
|
|
2021-01-18 04:33:56 +00:00
|
|
|
if (params.ignoreWindowsKeys && (sc == KEY_LEFTMETA || sc == KEY_RIGHTMETA))
|
2021-01-18 02:20:42 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (!g_state.keyDown[sc])
|
|
|
|
{
|
2021-01-18 04:33:56 +00:00
|
|
|
uint32_t ps2 = xfree86_to_ps2[sc];
|
|
|
|
if (!ps2)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (spice_key_down(ps2))
|
2021-01-18 02:20:42 +00:00
|
|
|
g_state.keyDown[sc] = true;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("SDL_KEYDOWN: failed to send message");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void app_handleKeyRelease(int sc)
|
|
|
|
{
|
|
|
|
if (g_state.escapeActive)
|
|
|
|
{
|
|
|
|
if (g_state.escapeAction == -1)
|
|
|
|
{
|
|
|
|
if (params.useSpiceInput)
|
|
|
|
setGrab(!g_cursor.grab);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
KeybindHandle handle = g_state.bindings[sc];
|
|
|
|
if (handle)
|
|
|
|
{
|
|
|
|
handle->callback(sc, handle->opaque);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sc == params.escapeKey)
|
|
|
|
g_state.escapeActive = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!app_inputEnabled())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// avoid sending key up events when we didn't send a down
|
|
|
|
if (!g_state.keyDown[sc])
|
|
|
|
return;
|
|
|
|
|
2021-01-18 04:33:56 +00:00
|
|
|
if (params.ignoreWindowsKeys && (sc == KEY_LEFTMETA || sc == KEY_RIGHTMETA))
|
|
|
|
return;
|
|
|
|
|
|
|
|
uint32_t ps2 = xfree86_to_ps2[sc];
|
|
|
|
if (!ps2)
|
2021-01-18 02:20:42 +00:00
|
|
|
return;
|
|
|
|
|
2021-01-18 04:33:56 +00:00
|
|
|
if (spice_key_up(ps2))
|
2021-01-18 02:20:42 +00:00
|
|
|
g_state.keyDown[sc] = false;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("SDL_KEYUP: failed to send message");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-18 15:44:56 +00:00
|
|
|
static void rotatePoint(struct DoublePoint *point)
|
|
|
|
{
|
|
|
|
double temp;
|
|
|
|
|
|
|
|
switch((g_state.rotate + params.winRotate) % LG_ROTATE_MAX)
|
|
|
|
{
|
|
|
|
case LG_ROTATE_0:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LG_ROTATE_90:
|
|
|
|
temp = point->x;
|
|
|
|
point->x = point->y;
|
|
|
|
point->y = -temp;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LG_ROTATE_180:
|
|
|
|
point->x = -point->x;
|
|
|
|
point->y = -point->y;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LG_ROTATE_270:
|
|
|
|
temp = point->x;
|
|
|
|
point->x = -point->y;
|
|
|
|
point->y = temp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-18 18:56:40 +00:00
|
|
|
static bool guestCurToLocal(struct DoublePoint *local)
|
2021-01-08 12:12:15 +00:00
|
|
|
{
|
2021-01-18 18:56:40 +00:00
|
|
|
if (!g_cursor.guest.valid || !g_state.posInfoValid)
|
|
|
|
return false;
|
|
|
|
|
2021-01-18 15:44:56 +00:00
|
|
|
const struct DoublePoint point =
|
|
|
|
{
|
|
|
|
.x = g_cursor.guest.x + g_cursor.guest.hx,
|
|
|
|
.y = g_cursor.guest.y + g_cursor.guest.hy
|
|
|
|
};
|
|
|
|
|
|
|
|
switch((g_state.rotate + params.winRotate) % LG_ROTATE_MAX)
|
|
|
|
{
|
|
|
|
case LG_ROTATE_0:
|
|
|
|
local->x = (point.x / g_cursor.scale.x) + g_state.dstRect.x;
|
|
|
|
local->y = (point.y / g_cursor.scale.y) + g_state.dstRect.y;;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LG_ROTATE_90:
|
|
|
|
local->x = (g_state.dstRect.x + g_state.dstRect.w) -
|
|
|
|
point.y / g_cursor.scale.y;
|
|
|
|
local->y = (point.x / g_cursor.scale.x) + g_state.dstRect.y;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LG_ROTATE_180:
|
|
|
|
local->x = (g_state.dstRect.x + g_state.dstRect.w) -
|
|
|
|
point.x / g_cursor.scale.x;
|
|
|
|
local->y = (g_state.dstRect.y + g_state.dstRect.h) -
|
|
|
|
point.y / g_cursor.scale.y;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LG_ROTATE_270:
|
|
|
|
local->x = (point.y / g_cursor.scale.y) + g_state.dstRect.x;
|
|
|
|
local->y = (g_state.dstRect.y + g_state.dstRect.h) -
|
|
|
|
point.x / g_cursor.scale.x;
|
|
|
|
break;
|
|
|
|
}
|
2021-01-18 18:56:40 +00:00
|
|
|
|
|
|
|
return true;
|
2021-01-18 15:44:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline static void localCurToGuest(struct DoublePoint *guest)
|
|
|
|
{
|
|
|
|
const struct DoublePoint point =
|
|
|
|
g_cursor.pos;
|
|
|
|
|
2021-01-18 16:44:36 +00:00
|
|
|
switch((g_state.rotate + params.winRotate) % LG_ROTATE_MAX)
|
2021-01-18 15:44:56 +00:00
|
|
|
{
|
|
|
|
case LG_ROTATE_0:
|
|
|
|
guest->x = (point.x - g_state.dstRect.x) * g_cursor.scale.x;
|
|
|
|
guest->y = (point.y - g_state.dstRect.y) * g_cursor.scale.y;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LG_ROTATE_90:
|
|
|
|
guest->x = (point.y - g_state.dstRect.y) * g_cursor.scale.y;
|
|
|
|
guest->y = (g_state.dstRect.w - point.x + g_state.dstRect.x)
|
|
|
|
* g_cursor.scale.x;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LG_ROTATE_180:
|
|
|
|
guest->x = (g_state.dstRect.w - point.x + g_state.dstRect.x)
|
|
|
|
* g_cursor.scale.x;
|
|
|
|
guest->y = (g_state.dstRect.h - point.y + g_state.dstRect.y)
|
|
|
|
* g_cursor.scale.y;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LG_ROTATE_270:
|
|
|
|
guest->x = (g_state.dstRect.h - point.y + g_state.dstRect.y)
|
|
|
|
* g_cursor.scale.y;
|
|
|
|
guest->y = (point.x - g_state.dstRect.x) * g_cursor.scale.x;
|
|
|
|
break;
|
|
|
|
}
|
2021-01-08 12:12:15 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 09:30:03 +00:00
|
|
|
void app_handleMouseNormal(double ex, double ey)
|
2018-01-24 12:46:11 +00:00
|
|
|
{
|
2021-01-16 10:37:43 +00:00
|
|
|
// prevent cursor handling outside of capture if the position is not known
|
2021-01-04 01:04:43 +00:00
|
|
|
if (!g_cursor.guest.valid)
|
2020-12-03 13:32:28 +00:00
|
|
|
return;
|
2020-08-21 05:40:22 +00:00
|
|
|
|
2021-01-08 02:17:42 +00:00
|
|
|
/* scale the movement to the guest */
|
2021-01-08 09:49:20 +00:00
|
|
|
if (g_cursor.useScale && params.scaleMouseInput)
|
2021-01-08 02:17:42 +00:00
|
|
|
{
|
2021-01-08 09:49:20 +00:00
|
|
|
ex *= g_cursor.scale.x / g_cursor.dpiScale;
|
|
|
|
ey *= g_cursor.scale.y / g_cursor.dpiScale;
|
2021-01-08 02:17:42 +00:00
|
|
|
}
|
2021-01-04 08:34:10 +00:00
|
|
|
|
2021-01-08 09:55:48 +00:00
|
|
|
bool testExit = true;
|
2021-01-08 02:17:42 +00:00
|
|
|
if (!g_cursor.inView)
|
2020-12-03 13:32:28 +00:00
|
|
|
{
|
2021-01-08 02:17:42 +00:00
|
|
|
const bool inView =
|
|
|
|
g_cursor.pos.x >= g_state.dstRect.x &&
|
|
|
|
g_cursor.pos.x < g_state.dstRect.x + g_state.dstRect.w &&
|
|
|
|
g_cursor.pos.y >= g_state.dstRect.y &&
|
|
|
|
g_cursor.pos.y < g_state.dstRect.y + g_state.dstRect.h;
|
2020-12-03 13:32:28 +00:00
|
|
|
|
2021-01-17 01:53:55 +00:00
|
|
|
setCursorInView(inView);
|
|
|
|
}
|
2021-01-08 02:17:42 +00:00
|
|
|
|
2021-01-17 01:53:55 +00:00
|
|
|
/* nothing to do if we are outside the viewport */
|
|
|
|
if (!g_cursor.inView)
|
|
|
|
return;
|
2021-01-08 02:17:42 +00:00
|
|
|
|
2021-01-17 01:53:55 +00:00
|
|
|
/*
|
|
|
|
* do not pass mouse events to the guest if we do not have focus, this must be
|
|
|
|
* done after the inView test has been performed so that when focus is gained
|
|
|
|
* we know if we should be drawing the cursor.
|
|
|
|
*/
|
|
|
|
if (!g_state.focused)
|
|
|
|
return;
|
2021-01-08 02:17:42 +00:00
|
|
|
|
2021-01-17 01:53:55 +00:00
|
|
|
/* if we have been instructed to realign */
|
|
|
|
if (g_cursor.realign)
|
|
|
|
{
|
|
|
|
g_cursor.realign = false;
|
|
|
|
|
2021-01-18 15:44:56 +00:00
|
|
|
struct DoublePoint guest;
|
|
|
|
localCurToGuest(&guest);
|
2021-01-17 01:53:55 +00:00
|
|
|
|
|
|
|
/* add the difference to the offset */
|
|
|
|
ex += guest.x - (g_cursor.guest.x + g_cursor.guest.hx);
|
|
|
|
ey += guest.y - (g_cursor.guest.y + g_cursor.guest.hy);
|
|
|
|
|
|
|
|
/* don't test for an exit as we just entered, we can get into a enter/exit
|
|
|
|
* loop otherwise */
|
|
|
|
testExit = false;
|
2020-01-10 16:41:44 +00:00
|
|
|
}
|
2021-01-03 06:21:04 +00:00
|
|
|
|
2021-01-17 01:53:55 +00:00
|
|
|
/* if we are in "autoCapture" and the delta was large don't test for exit */
|
2021-01-08 09:55:48 +00:00
|
|
|
if (params.autoCapture &&
|
2021-01-14 06:58:29 +00:00
|
|
|
(fabs(ex) > 100.0 / g_cursor.scale.x || fabs(ey) > 100.0 / g_cursor.scale.y))
|
2021-01-08 09:55:48 +00:00
|
|
|
testExit = false;
|
|
|
|
|
2021-01-17 04:13:45 +00:00
|
|
|
/* if any buttons are held we should not allow exit to happen */
|
|
|
|
if (g_cursor.buttons)
|
|
|
|
testExit = false;
|
|
|
|
|
2021-01-18 15:44:56 +00:00
|
|
|
if (testExit)
|
2021-01-08 02:17:42 +00:00
|
|
|
{
|
2021-01-18 15:44:56 +00:00
|
|
|
/* translate the move to the guests orientation */
|
|
|
|
struct DoublePoint move = {.x = ex, .y = ey};
|
|
|
|
rotatePoint(&move);
|
|
|
|
|
|
|
|
/* translate the guests position to our coordinate space */
|
|
|
|
struct DoublePoint local;
|
|
|
|
guestCurToLocal(&local);
|
|
|
|
|
|
|
|
/* check if the move would push the cursor outside the guest's viewport */
|
|
|
|
if (
|
|
|
|
local.x + move.x < g_state.dstRect.x ||
|
|
|
|
local.y + move.y < g_state.dstRect.y ||
|
|
|
|
local.x + move.x >= g_state.dstRect.x + g_state.dstRect.w ||
|
|
|
|
local.y + move.y >= g_state.dstRect.y + g_state.dstRect.h)
|
2020-12-03 13:32:28 +00:00
|
|
|
{
|
2021-01-18 15:44:56 +00:00
|
|
|
local.x += move.x;
|
|
|
|
local.y += move.y;
|
|
|
|
const int tx = (local.x <= 0.0) ? floor(local.x) : ceil(local.x);
|
|
|
|
const int ty = (local.y <= 0.0) ? floor(local.y) : ceil(local.y);
|
|
|
|
|
|
|
|
if (isValidCursorLocation(
|
|
|
|
g_state.windowPos.x + g_state.border.x + tx,
|
|
|
|
g_state.windowPos.y + g_state.border.y + ty))
|
|
|
|
{
|
|
|
|
setCursorInView(false);
|
2021-01-03 06:42:58 +00:00
|
|
|
|
2021-01-18 15:44:56 +00:00
|
|
|
/* preempt the window leave flag if the warp will leave our window */
|
|
|
|
if (tx < 0 || ty < 0 || tx > g_state.windowW || ty > g_state.windowH)
|
|
|
|
g_cursor.inWindow = false;
|
2021-01-08 02:17:42 +00:00
|
|
|
|
2021-01-18 15:44:56 +00:00
|
|
|
/* ungrab the pointer and move the local cursor to the exit point */
|
|
|
|
g_state.ds->ungrabPointer();
|
2021-01-17 01:53:55 +00:00
|
|
|
|
2021-01-18 15:44:56 +00:00
|
|
|
warpPointer(tx, ty, true);
|
|
|
|
return;
|
|
|
|
}
|
2020-12-03 13:32:28 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-10 18:22:12 +00:00
|
|
|
|
2021-01-08 02:17:42 +00:00
|
|
|
int x, y;
|
2021-01-09 03:55:20 +00:00
|
|
|
cursorToInt(ex, ey, &x, &y);
|
2021-01-09 03:39:48 +00:00
|
|
|
|
2021-01-08 02:17:42 +00:00
|
|
|
if (x == 0 && y == 0)
|
|
|
|
return;
|
|
|
|
|
2021-01-08 08:41:10 +00:00
|
|
|
if (params.autoCapture)
|
|
|
|
{
|
|
|
|
g_cursor.delta.x += x;
|
|
|
|
g_cursor.delta.y += y;
|
|
|
|
|
2021-01-14 21:49:56 +00:00
|
|
|
if (fabs(g_cursor.delta.x) > 50.0 || fabs(g_cursor.delta.y) > 50.0)
|
2021-01-08 08:41:10 +00:00
|
|
|
{
|
|
|
|
g_cursor.delta.x = 0;
|
|
|
|
g_cursor.delta.y = 0;
|
2021-01-16 18:10:52 +00:00
|
|
|
warpPointer(g_state.windowCX, g_state.windowCY, false);
|
2021-01-08 08:41:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
g_cursor.guest.x = g_state.srcSize.x / 2;
|
|
|
|
g_cursor.guest.y = g_state.srcSize.y / 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* assume the mouse will move to the location we attempt to move it to so we
|
|
|
|
* avoid warp out of window issues. The cursorThread will correct this if
|
|
|
|
* wrong after the movement has ocurred on the guest */
|
|
|
|
g_cursor.guest.x += x;
|
|
|
|
g_cursor.guest.y += y;
|
|
|
|
}
|
2021-01-08 02:17:42 +00:00
|
|
|
|
|
|
|
if (!spice_mouse_motion(x, y))
|
2020-12-03 13:32:28 +00:00
|
|
|
DEBUG_ERROR("failed to send mouse motion message");
|
2020-01-10 18:22:12 +00:00
|
|
|
}
|
|
|
|
|
2021-01-16 09:17:19 +00:00
|
|
|
|
|
|
|
// On some display servers normal cursor logic does not work due to the lack of
|
|
|
|
// cursor warp support. Instead, we attempt a best-effort emulation which works
|
|
|
|
// with a 1:1 mouse movement patch applied in the guest. For anything fancy, use
|
|
|
|
// capture mode.
|
2021-01-16 09:59:05 +00:00
|
|
|
void app_handleMouseBasic()
|
2021-01-16 09:17:19 +00:00
|
|
|
{
|
2021-01-16 23:44:30 +00:00
|
|
|
/* do not pass mouse events to the guest if we do not have focus */
|
|
|
|
if (!g_state.focused)
|
|
|
|
return;
|
|
|
|
|
2021-01-16 09:17:19 +00:00
|
|
|
const bool inView =
|
|
|
|
g_cursor.pos.x >= g_state.dstRect.x &&
|
|
|
|
g_cursor.pos.x < g_state.dstRect.x + g_state.dstRect.w &&
|
|
|
|
g_cursor.pos.y >= g_state.dstRect.y &&
|
|
|
|
g_cursor.pos.y < g_state.dstRect.y + g_state.dstRect.h;
|
|
|
|
|
2021-01-17 01:53:55 +00:00
|
|
|
setCursorInView(inView);
|
2021-01-16 09:17:19 +00:00
|
|
|
|
|
|
|
if (g_cursor.guest.dpiScale == 0)
|
|
|
|
return;
|
|
|
|
|
2021-01-17 04:28:44 +00:00
|
|
|
double px = g_cursor.pos.x;
|
|
|
|
double py = g_cursor.pos.y;
|
|
|
|
|
|
|
|
if (px < g_state.dstRect.x)
|
|
|
|
px = g_state.dstRect.x;
|
|
|
|
else if (px > g_state.dstRect.x + g_state.dstRect.w)
|
|
|
|
px = g_state.dstRect.x + g_state.dstRect.w;
|
|
|
|
|
|
|
|
if (py < g_state.dstRect.y)
|
|
|
|
py = g_state.dstRect.y;
|
|
|
|
else if (py > g_state.dstRect.y + g_state.dstRect.h)
|
|
|
|
py = g_state.dstRect.y + g_state.dstRect.h;
|
|
|
|
|
2021-01-16 09:17:19 +00:00
|
|
|
/* translate the guests position to our coordinate space */
|
|
|
|
struct DoublePoint local;
|
|
|
|
guestCurToLocal(&local);
|
|
|
|
|
2021-01-17 04:28:44 +00:00
|
|
|
double lx = (px - local.x) / g_cursor.dpiScale;
|
|
|
|
double ly = (py - local.y) / g_cursor.dpiScale;
|
2021-01-16 09:17:19 +00:00
|
|
|
|
|
|
|
int x, y;
|
|
|
|
cursorToInt(lx, ly, &x, &y);
|
|
|
|
|
|
|
|
g_cursor.guest.x += x;
|
|
|
|
g_cursor.guest.y += y;
|
|
|
|
|
|
|
|
if (!spice_mouse_motion(x, y))
|
|
|
|
DEBUG_ERROR("failed to send mouse motion message");
|
|
|
|
}
|
|
|
|
|
2021-01-15 09:30:03 +00:00
|
|
|
void app_updateWindowPos(int x, int y)
|
|
|
|
{
|
|
|
|
g_state.windowPos.x = x;
|
|
|
|
g_state.windowPos.y = y;
|
|
|
|
}
|
|
|
|
|
|
|
|
void app_handleResizeEvent(int w, int h)
|
2020-01-10 16:41:44 +00:00
|
|
|
{
|
2021-01-04 01:06:54 +00:00
|
|
|
SDL_GetWindowBordersSize(g_state.window,
|
|
|
|
&g_state.border.y,
|
|
|
|
&g_state.border.x,
|
|
|
|
&g_state.border.h,
|
|
|
|
&g_state.border.w
|
2020-12-04 06:32:28 +00:00
|
|
|
);
|
|
|
|
|
2021-01-04 01:59:14 +00:00
|
|
|
g_state.windowW = w;
|
|
|
|
g_state.windowH = h;
|
|
|
|
g_state.windowCX = w / 2;
|
|
|
|
g_state.windowCY = h / 2;
|
2020-01-10 16:41:44 +00:00
|
|
|
updatePositionInfo();
|
2021-01-09 07:01:08 +00:00
|
|
|
|
2021-01-15 09:30:03 +00:00
|
|
|
if (app_inputEnabled())
|
2021-01-09 07:41:02 +00:00
|
|
|
{
|
|
|
|
/* if the window is moved/resized causing a loss of focus while grabbed, it
|
|
|
|
* makes it impossible to re-focus the window, so we quietly re-enter
|
|
|
|
* capture if we were already in it */
|
|
|
|
if (g_cursor.grab)
|
|
|
|
{
|
|
|
|
setGrabQuiet(false);
|
|
|
|
setGrabQuiet(true);
|
|
|
|
}
|
2021-01-09 07:01:08 +00:00
|
|
|
alignToGuest();
|
2021-01-09 07:41:02 +00:00
|
|
|
}
|
2020-01-10 16:41:44 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 09:30:03 +00:00
|
|
|
void app_handleWindowLeave(void)
|
2020-01-10 19:03:16 +00:00
|
|
|
{
|
2021-01-04 01:04:43 +00:00
|
|
|
g_cursor.inWindow = false;
|
2021-01-17 01:53:55 +00:00
|
|
|
setCursorInView(false);
|
2020-12-03 13:32:28 +00:00
|
|
|
|
2021-01-15 09:30:03 +00:00
|
|
|
if (!app_inputEnabled())
|
2020-01-10 19:03:16 +00:00
|
|
|
return;
|
|
|
|
|
2020-11-15 13:41:22 +00:00
|
|
|
if (!params.alwaysShowCursor)
|
2021-01-04 01:04:43 +00:00
|
|
|
g_cursor.draw = false;
|
|
|
|
g_cursor.redraw = true;
|
2020-01-10 19:03:16 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 09:30:03 +00:00
|
|
|
void app_handleWindowEnter(void)
|
2020-01-10 19:03:16 +00:00
|
|
|
{
|
2021-01-04 01:04:43 +00:00
|
|
|
g_cursor.inWindow = true;
|
2021-01-15 09:30:03 +00:00
|
|
|
if (!app_inputEnabled())
|
2020-01-10 19:03:16 +00:00
|
|
|
return;
|
2021-01-17 01:53:55 +00:00
|
|
|
|
|
|
|
g_cursor.realign = true;
|
2020-01-10 19:03:16 +00:00
|
|
|
}
|
|
|
|
|
2021-01-07 13:44:15 +00:00
|
|
|
static void setGrab(bool enable)
|
2021-01-09 07:41:02 +00:00
|
|
|
{
|
|
|
|
setGrabQuiet(enable);
|
|
|
|
|
|
|
|
app_alert(
|
|
|
|
g_cursor.grab ? LG_ALERT_SUCCESS : LG_ALERT_WARNING,
|
|
|
|
g_cursor.grab ? "Capture Enabled" : "Capture Disabled"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setGrabQuiet(bool enable)
|
2021-01-07 13:44:15 +00:00
|
|
|
{
|
2021-01-09 07:01:08 +00:00
|
|
|
/* we always do this so that at init the cursor is in the right state */
|
|
|
|
if (params.captureInputOnly && params.hideMouse)
|
|
|
|
SDL_ShowCursor(enable ? SDL_DISABLE : SDL_ENABLE);
|
|
|
|
|
2021-01-07 13:44:15 +00:00
|
|
|
if (g_cursor.grab == enable)
|
|
|
|
return;
|
|
|
|
|
|
|
|
g_cursor.grab = enable;
|
2021-01-12 00:49:27 +00:00
|
|
|
g_cursor.acc.x = 0.0;
|
|
|
|
g_cursor.acc.y = 0.0;
|
2021-01-07 13:44:15 +00:00
|
|
|
|
2021-01-16 10:32:38 +00:00
|
|
|
/* if the display server does not support warp we need to grab the pointer
|
|
|
|
* here instead of in the move handler */
|
|
|
|
bool warpSupport = true;
|
|
|
|
app_getProp(LG_DS_WARP_SUPPORT, &warpSupport);
|
|
|
|
|
2021-01-10 04:26:33 +00:00
|
|
|
if (enable)
|
|
|
|
{
|
2021-01-17 01:53:55 +00:00
|
|
|
setCursorInView(true);
|
2021-01-16 09:04:15 +00:00
|
|
|
|
2021-01-10 04:26:33 +00:00
|
|
|
if (params.grabKeyboard)
|
2021-01-15 09:30:03 +00:00
|
|
|
g_state.ds->grabKeyboard();
|
2021-01-16 10:32:38 +00:00
|
|
|
|
|
|
|
if (!warpSupport)
|
|
|
|
g_state.ds->grabPointer();
|
|
|
|
|
2021-01-10 04:26:33 +00:00
|
|
|
}
|
2021-01-07 13:44:15 +00:00
|
|
|
else
|
|
|
|
{
|
2021-01-10 04:26:33 +00:00
|
|
|
if (params.grabKeyboard)
|
2021-01-07 13:44:15 +00:00
|
|
|
{
|
2021-01-10 04:26:33 +00:00
|
|
|
if (!g_state.focused || !params.grabKeyboardOnFocus)
|
2021-01-15 09:30:03 +00:00
|
|
|
g_state.ds->ungrabKeyboard();
|
2021-01-07 13:44:15 +00:00
|
|
|
}
|
2021-01-16 10:32:38 +00:00
|
|
|
|
|
|
|
if (!warpSupport)
|
|
|
|
g_state.ds->ungrabPointer();
|
2021-01-07 13:44:15 +00:00
|
|
|
}
|
|
|
|
|
2021-01-09 07:01:08 +00:00
|
|
|
// if exiting capture when input on capture only, we want to show the cursor
|
|
|
|
if (!enable && (params.captureInputOnly || !params.hideMouse))
|
|
|
|
alignToGuest();
|
|
|
|
|
2021-01-07 16:27:03 +00:00
|
|
|
if (g_cursor.grab)
|
2021-01-17 01:53:55 +00:00
|
|
|
setCursorInView(true);
|
2021-01-07 13:44:15 +00:00
|
|
|
}
|
|
|
|
|
2020-01-10 16:41:44 +00:00
|
|
|
int eventFilter(void * userdata, SDL_Event * event)
|
|
|
|
{
|
2021-01-15 09:30:03 +00:00
|
|
|
if (g_state.ds->eventFilter(event))
|
|
|
|
return 0;
|
|
|
|
|
2021-01-17 02:33:42 +00:00
|
|
|
// always include the default handler (SDL) for any unhandled events
|
|
|
|
if (g_state.ds != LG_DisplayServers[0])
|
|
|
|
if (LG_DisplayServers[0]->eventFilter(event))
|
|
|
|
return 0;
|
|
|
|
|
2021-01-08 12:12:15 +00:00
|
|
|
if (event->type == e_SDLEvent)
|
|
|
|
{
|
|
|
|
switch(event->user.code)
|
|
|
|
{
|
|
|
|
case LG_EVENT_ALIGN_TO_GUEST:
|
|
|
|
{
|
|
|
|
if (!g_cursor.guest.valid || !g_state.focused)
|
|
|
|
break;
|
|
|
|
|
|
|
|
struct DoublePoint local;
|
2021-01-18 18:56:40 +00:00
|
|
|
if (guestCurToLocal(&local))
|
2021-01-18 19:00:59 +00:00
|
|
|
if (warpPointer(round(local.x), round(local.y), false))
|
|
|
|
setCursorInView(true);
|
2021-01-08 12:12:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-04 07:17:46 +00:00
|
|
|
// consume all events
|
2017-10-19 04:15:49 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-13 23:15:03 +00:00
|
|
|
void int_handler(int signal)
|
2017-12-17 09:09:47 +00:00
|
|
|
{
|
|
|
|
switch(signal)
|
|
|
|
{
|
|
|
|
case SIGINT:
|
2019-04-13 23:15:03 +00:00
|
|
|
case SIGTERM:
|
|
|
|
DEBUG_INFO("Caught signal, shutting down...");
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.state = APP_STATE_SHUTDOWN;
|
2017-12-17 09:09:47 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-17 12:15:18 +00:00
|
|
|
static bool try_renderer(const int index, const LG_RendererParams lgrParams, Uint32 * sdlFlags)
|
|
|
|
{
|
2019-05-21 05:03:59 +00:00
|
|
|
const LG_Renderer *r = LG_Renderers[index];
|
2017-12-17 12:15:18 +00:00
|
|
|
|
|
|
|
if (!IS_LG_RENDERER_VALID(r))
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("FIXME: Renderer %d is invalid, skipping", index);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create the renderer
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.lgrData = NULL;
|
|
|
|
if (!r->create(&g_state.lgrData, lgrParams))
|
2017-12-17 12:15:18 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// initialize the renderer
|
2021-01-04 01:06:54 +00:00
|
|
|
if (!r->initialize(g_state.lgrData, sdlFlags))
|
2017-12-17 12:15:18 +00:00
|
|
|
{
|
2021-01-04 01:06:54 +00:00
|
|
|
r->deinitialize(g_state.lgrData);
|
2017-12-17 12:15:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG_INFO("Using Renderer: %s", r->get_name());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-18 02:54:21 +00:00
|
|
|
static void toggle_fullscreen(uint32_t scancode, void * opaque)
|
2019-03-28 15:06:37 +00:00
|
|
|
{
|
2021-01-04 01:06:54 +00:00
|
|
|
SDL_SetWindowFullscreen(g_state.window, params.fullscreen ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP);
|
2019-03-28 15:06:37 +00:00
|
|
|
params.fullscreen = !params.fullscreen;
|
|
|
|
}
|
|
|
|
|
2021-01-18 02:54:21 +00:00
|
|
|
static void toggle_video(uint32_t scancode, void * opaque)
|
2020-10-14 08:32:21 +00:00
|
|
|
{
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.stopVideo = !g_state.stopVideo;
|
2020-10-14 08:32:21 +00:00
|
|
|
app_alert(
|
|
|
|
LG_ALERT_INFO,
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.stopVideo ? "Video Stream Disabled" : "Video Stream Enabled"
|
2020-10-14 08:32:21 +00:00
|
|
|
);
|
|
|
|
|
2021-01-04 01:06:54 +00:00
|
|
|
if (!g_state.stopVideo)
|
2020-10-14 08:32:21 +00:00
|
|
|
{
|
|
|
|
if (t_frame)
|
|
|
|
{
|
|
|
|
lgJoinThread(t_frame, NULL);
|
|
|
|
t_frame = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!lgCreateThread("frameThread", frameThread, NULL, &t_frame))
|
|
|
|
DEBUG_ERROR("frame create thread failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-18 16:49:09 +00:00
|
|
|
static void toggle_rotate(uint32_t scancode, void * opaque)
|
|
|
|
{
|
|
|
|
if (params.winRotate == LG_ROTATE_MAX-1)
|
|
|
|
params.winRotate = 0;
|
|
|
|
else
|
|
|
|
++params.winRotate;
|
|
|
|
updatePositionInfo();
|
|
|
|
}
|
|
|
|
|
2021-01-18 02:54:21 +00:00
|
|
|
static void toggle_input(uint32_t scancode, void * opaque)
|
2019-03-28 15:17:06 +00:00
|
|
|
{
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.ignoreInput = !g_state.ignoreInput;
|
2019-03-30 01:26:06 +00:00
|
|
|
app_alert(
|
|
|
|
LG_ALERT_INFO,
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.ignoreInput ? "Input Disabled" : "Input Enabled"
|
2019-03-30 01:26:06 +00:00
|
|
|
);
|
2019-03-28 15:17:06 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 02:54:21 +00:00
|
|
|
static void quit(uint32_t scancode, void * opaque)
|
2020-04-22 16:31:30 +00:00
|
|
|
{
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.state = APP_STATE_SHUTDOWN;
|
2020-04-22 16:31:30 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 02:54:21 +00:00
|
|
|
static void mouse_sens_inc(uint32_t scancode, void * opaque)
|
2019-05-23 19:29:38 +00:00
|
|
|
{
|
|
|
|
char * msg;
|
2021-01-04 01:04:43 +00:00
|
|
|
if (g_cursor.sens < 9)
|
|
|
|
++g_cursor.sens;
|
2019-05-23 19:29:38 +00:00
|
|
|
|
2021-01-04 01:04:43 +00:00
|
|
|
alloc_sprintf(&msg, "Sensitivity: %s%d", g_cursor.sens > 0 ? "+" : "", g_cursor.sens);
|
2019-05-23 19:29:38 +00:00
|
|
|
app_alert(
|
|
|
|
LG_ALERT_INFO,
|
|
|
|
msg
|
|
|
|
);
|
|
|
|
free(msg);
|
|
|
|
}
|
|
|
|
|
2021-01-18 02:54:21 +00:00
|
|
|
static void mouse_sens_dec(uint32_t scancode, void * opaque)
|
2019-05-23 19:29:38 +00:00
|
|
|
{
|
|
|
|
char * msg;
|
|
|
|
|
2021-01-04 01:04:43 +00:00
|
|
|
if (g_cursor.sens > -9)
|
|
|
|
--g_cursor.sens;
|
2019-05-23 19:29:38 +00:00
|
|
|
|
2021-01-04 01:04:43 +00:00
|
|
|
alloc_sprintf(&msg, "Sensitivity: %s%d", g_cursor.sens > 0 ? "+" : "", g_cursor.sens);
|
2019-05-23 19:29:38 +00:00
|
|
|
app_alert(
|
|
|
|
LG_ALERT_INFO,
|
|
|
|
msg
|
|
|
|
);
|
|
|
|
free(msg);
|
|
|
|
}
|
|
|
|
|
2021-01-18 04:33:56 +00:00
|
|
|
static void ctrl_alt_fn(uint32_t key, void * opaque)
|
2019-05-31 06:39:55 +00:00
|
|
|
{
|
2021-01-18 04:33:56 +00:00
|
|
|
const uint32_t ctrl = xfree86_to_ps2[KEY_LEFTCTRL];
|
|
|
|
const uint32_t alt = xfree86_to_ps2[KEY_LEFTALT ];
|
|
|
|
const uint32_t fn = xfree86_to_ps2[key];
|
|
|
|
spice_key_down(ctrl);
|
|
|
|
spice_key_down(alt );
|
2019-05-31 06:39:55 +00:00
|
|
|
spice_key_down(fn );
|
|
|
|
|
2021-01-18 04:33:56 +00:00
|
|
|
spice_key_up(ctrl);
|
|
|
|
spice_key_up(alt );
|
2019-05-31 06:39:55 +00:00
|
|
|
spice_key_up(fn );
|
|
|
|
}
|
|
|
|
|
2021-01-18 02:54:21 +00:00
|
|
|
static void key_passthrough(uint32_t sc, void * opaque)
|
2021-01-04 07:07:09 +00:00
|
|
|
{
|
2021-01-18 04:33:56 +00:00
|
|
|
sc = xfree86_to_ps2[sc];
|
2021-01-04 07:07:09 +00:00
|
|
|
spice_key_down(sc);
|
|
|
|
spice_key_up (sc);
|
|
|
|
}
|
|
|
|
|
2021-01-14 06:05:26 +00:00
|
|
|
static void register_key_binds(void)
|
2019-03-28 15:06:37 +00:00
|
|
|
{
|
2021-01-18 02:54:21 +00:00
|
|
|
g_state.kbFS = app_register_keybind(KEY_F , toggle_fullscreen, NULL);
|
|
|
|
g_state.kbVideo = app_register_keybind(KEY_V , toggle_video , NULL);
|
2021-01-18 16:49:09 +00:00
|
|
|
g_state.kbRotate = app_register_keybind(KEY_R , toggle_rotate , NULL);
|
2021-01-18 02:54:21 +00:00
|
|
|
g_state.kbInput = app_register_keybind(KEY_I , toggle_input , NULL);
|
|
|
|
g_state.kbQuit = app_register_keybind(KEY_Q , quit , NULL);
|
|
|
|
g_state.kbMouseSensInc = app_register_keybind(KEY_INSERT, mouse_sens_inc , NULL);
|
|
|
|
g_state.kbMouseSensDec = app_register_keybind(KEY_DELETE, mouse_sens_dec , NULL);
|
|
|
|
|
|
|
|
g_state.kbCtrlAltFn[0 ] = app_register_keybind(KEY_F1 , ctrl_alt_fn, NULL);
|
|
|
|
g_state.kbCtrlAltFn[1 ] = app_register_keybind(KEY_F2 , ctrl_alt_fn, NULL);
|
|
|
|
g_state.kbCtrlAltFn[2 ] = app_register_keybind(KEY_F3 , ctrl_alt_fn, NULL);
|
|
|
|
g_state.kbCtrlAltFn[3 ] = app_register_keybind(KEY_F4 , ctrl_alt_fn, NULL);
|
|
|
|
g_state.kbCtrlAltFn[4 ] = app_register_keybind(KEY_F5 , ctrl_alt_fn, NULL);
|
|
|
|
g_state.kbCtrlAltFn[5 ] = app_register_keybind(KEY_F6 , ctrl_alt_fn, NULL);
|
|
|
|
g_state.kbCtrlAltFn[6 ] = app_register_keybind(KEY_F7 , ctrl_alt_fn, NULL);
|
|
|
|
g_state.kbCtrlAltFn[7 ] = app_register_keybind(KEY_F8 , ctrl_alt_fn, NULL);
|
|
|
|
g_state.kbCtrlAltFn[8 ] = app_register_keybind(KEY_F9 , ctrl_alt_fn, NULL);
|
|
|
|
g_state.kbCtrlAltFn[9 ] = app_register_keybind(KEY_F10, ctrl_alt_fn, NULL);
|
|
|
|
g_state.kbCtrlAltFn[10] = app_register_keybind(KEY_F11, ctrl_alt_fn, NULL);
|
|
|
|
g_state.kbCtrlAltFn[11] = app_register_keybind(KEY_F12, ctrl_alt_fn, NULL);
|
|
|
|
|
2021-01-18 04:33:56 +00:00
|
|
|
g_state.kbPass[0] = app_register_keybind(KEY_LEFTMETA , key_passthrough, NULL);
|
|
|
|
g_state.kbPass[1] = app_register_keybind(KEY_RIGHTMETA, key_passthrough, NULL);
|
2019-03-28 15:06:37 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 06:05:26 +00:00
|
|
|
static void release_key_binds(void)
|
2019-03-28 15:06:37 +00:00
|
|
|
{
|
2021-01-04 01:06:54 +00:00
|
|
|
app_release_keybind(&g_state.kbFS );
|
|
|
|
app_release_keybind(&g_state.kbVideo);
|
|
|
|
app_release_keybind(&g_state.kbInput);
|
|
|
|
app_release_keybind(&g_state.kbQuit );
|
|
|
|
app_release_keybind(&g_state.kbMouseSensInc);
|
|
|
|
app_release_keybind(&g_state.kbMouseSensDec);
|
2019-05-31 06:39:55 +00:00
|
|
|
for(int i = 0; i < 12; ++i)
|
2021-01-04 01:06:54 +00:00
|
|
|
app_release_keybind(&g_state.kbCtrlAltFn[i]);
|
2021-01-04 07:07:09 +00:00
|
|
|
for(int i = 0; i < 2; ++i)
|
|
|
|
app_release_keybind(&g_state.kbPass[i]);
|
2019-03-28 15:06:37 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 06:05:26 +00:00
|
|
|
static void initSDLCursor(void)
|
2020-12-05 22:58:50 +00:00
|
|
|
{
|
|
|
|
const uint8_t data[4] = {0xf, 0x9, 0x9, 0xf};
|
|
|
|
const uint8_t mask[4] = {0xf, 0xf, 0xf, 0xf};
|
2021-01-03 11:28:06 +00:00
|
|
|
cursor = SDL_CreateCursor(data, mask, 8, 4, 4, 0);
|
2020-12-05 22:58:50 +00:00
|
|
|
SDL_SetCursor(cursor);
|
|
|
|
}
|
|
|
|
|
2021-01-14 06:05:26 +00:00
|
|
|
static int lg_run(void)
|
2017-10-19 04:15:49 +00:00
|
|
|
{
|
2021-01-04 01:06:54 +00:00
|
|
|
memset(&g_state, 0, sizeof(g_state));
|
2021-01-05 00:47:17 +00:00
|
|
|
|
2020-12-03 13:32:28 +00:00
|
|
|
lgInit();
|
2017-10-19 04:15:49 +00:00
|
|
|
|
2021-01-04 01:04:43 +00:00
|
|
|
g_cursor.sens = params.mouseSens;
|
|
|
|
if (g_cursor.sens < -9) g_cursor.sens = -9;
|
|
|
|
else if (g_cursor.sens > 9) g_cursor.sens = 9;
|
2019-05-23 19:29:38 +00:00
|
|
|
|
2021-01-16 09:29:54 +00:00
|
|
|
|
|
|
|
// try to early detect the platform
|
|
|
|
SDL_SYSWM_TYPE subsystem = SDL_SYSWM_UNKNOWN;
|
|
|
|
if (getenv("WAYLAND_DISPLAY")) subsystem = SDL_SYSWM_WAYLAND;
|
|
|
|
else if (getenv("DISPLAY" )) subsystem = SDL_SYSWM_X11;
|
|
|
|
else
|
|
|
|
DEBUG_WARN("Unknown subsystem, falling back to SDL default");
|
|
|
|
|
|
|
|
// search for the best displayserver ops to use
|
|
|
|
for(int i = 0; i < LG_DISPLAYSERVER_COUNT; ++i)
|
|
|
|
if (LG_DisplayServers[i]->subsystem == subsystem)
|
|
|
|
{
|
|
|
|
g_state.ds = LG_DisplayServers[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(g_state.ds);
|
|
|
|
|
|
|
|
// set any null methods to the fallback
|
|
|
|
#define SET_FALLBACK(x) \
|
|
|
|
if (!g_state.ds->x) g_state.ds->x = LG_DisplayServers[0]->x;
|
|
|
|
SET_FALLBACK(earlyInit);
|
|
|
|
SET_FALLBACK(getProp);
|
|
|
|
SET_FALLBACK(init);
|
|
|
|
SET_FALLBACK(startup);
|
|
|
|
SET_FALLBACK(shutdown);
|
|
|
|
SET_FALLBACK(free);
|
|
|
|
SET_FALLBACK(eventFilter);
|
|
|
|
SET_FALLBACK(grabPointer);
|
|
|
|
SET_FALLBACK(ungrabKeyboard);
|
2021-01-16 18:10:52 +00:00
|
|
|
SET_FALLBACK(warpPointer);
|
2021-01-17 01:53:55 +00:00
|
|
|
SET_FALLBACK(realignPointer);
|
2021-01-16 09:29:54 +00:00
|
|
|
SET_FALLBACK(cbInit);
|
|
|
|
SET_FALLBACK(cbNotice);
|
|
|
|
SET_FALLBACK(cbRelease);
|
|
|
|
SET_FALLBACK(cbRequest);
|
|
|
|
#undef SET_FALLBACK
|
|
|
|
|
|
|
|
// init the subsystem
|
|
|
|
if (!g_state.ds->earlyInit())
|
2019-02-22 11:40:57 +00:00
|
|
|
{
|
2021-01-16 09:29:54 +00:00
|
|
|
DEBUG_ERROR("Subsystem early init failed");
|
|
|
|
return -1;
|
2018-07-28 20:37:48 +00:00
|
|
|
}
|
|
|
|
|
2021-01-09 16:36:56 +00:00
|
|
|
if (!params.noScreensaver)
|
|
|
|
SDL_SetHint(SDL_HINT_VIDEO_ALLOW_SCREENSAVER, "1");
|
|
|
|
|
2017-10-26 16:08:54 +00:00
|
|
|
if (SDL_Init(SDL_INIT_VIDEO) < 0)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("SDL_Init Failed");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-12-17 09:09:47 +00:00
|
|
|
// override SDL's SIGINIT handler so that we can tell the difference between
|
|
|
|
// SIGINT and the user sending a close event, such as ALT+F4
|
2019-04-13 23:15:03 +00:00
|
|
|
signal(SIGINT , int_handler);
|
|
|
|
signal(SIGTERM, int_handler);
|
2017-12-17 09:09:47 +00:00
|
|
|
|
2019-10-26 01:03:10 +00:00
|
|
|
// try map the shared memory
|
2021-01-04 01:06:54 +00:00
|
|
|
if (!ivshmemOpen(&g_state.shm))
|
2019-10-26 01:03:10 +00:00
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to map memory");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// try to connect to the spice server
|
|
|
|
if (params.useSpiceInput || params.useSpiceClipboard)
|
|
|
|
{
|
|
|
|
spice_set_clipboard_cb(
|
|
|
|
spiceClipboardNotice,
|
|
|
|
spiceClipboardData,
|
|
|
|
spiceClipboardRelease,
|
|
|
|
spiceClipboardRequest);
|
|
|
|
|
|
|
|
if (!spice_connect(params.spiceHost, params.spicePort, ""))
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to connect to spice server");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-01-04 01:06:54 +00:00
|
|
|
while(g_state.state != APP_STATE_SHUTDOWN && !spice_ready())
|
2020-01-18 19:51:21 +00:00
|
|
|
if (!spice_process(1000))
|
2019-10-26 01:03:10 +00:00
|
|
|
{
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.state = APP_STATE_SHUTDOWN;
|
2019-10-26 01:03:10 +00:00
|
|
|
DEBUG_ERROR("Failed to process spice messages");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-08-09 06:17:08 +00:00
|
|
|
spice_mouse_mode(true);
|
2020-01-02 12:59:06 +00:00
|
|
|
if (!lgCreateThread("spiceThread", spiceThread, NULL, &t_spice))
|
2019-10-26 01:03:10 +00:00
|
|
|
{
|
|
|
|
DEBUG_ERROR("spice create thread failed");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// select and init a renderer
|
2017-12-14 06:42:16 +00:00
|
|
|
LG_RendererParams lgrParams;
|
2020-07-19 04:29:29 +00:00
|
|
|
lgrParams.showFPS = params.showFPS;
|
|
|
|
lgrParams.quickSplash = params.quickSplash;
|
2017-12-14 06:42:16 +00:00
|
|
|
Uint32 sdlFlags;
|
|
|
|
|
2017-12-17 12:15:18 +00:00
|
|
|
if (params.forceRenderer)
|
2017-12-14 06:42:16 +00:00
|
|
|
{
|
2017-12-17 12:15:18 +00:00
|
|
|
DEBUG_INFO("Trying forced renderer");
|
|
|
|
sdlFlags = 0;
|
|
|
|
if (!try_renderer(params.forceRendererIndex, lgrParams, &sdlFlags))
|
2017-12-14 06:42:16 +00:00
|
|
|
{
|
2017-12-17 12:15:18 +00:00
|
|
|
DEBUG_ERROR("Forced renderer failed to iniailize");
|
|
|
|
return -1;
|
2017-12-14 06:42:16 +00:00
|
|
|
}
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.lgr = LG_Renderers[params.forceRendererIndex];
|
2017-12-17 12:15:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// probe for a a suitable renderer
|
|
|
|
for(unsigned int i = 0; i < LG_RENDERER_COUNT; ++i)
|
2017-12-14 06:42:16 +00:00
|
|
|
{
|
2017-12-17 12:15:18 +00:00
|
|
|
sdlFlags = 0;
|
|
|
|
if (try_renderer(i, lgrParams, &sdlFlags))
|
|
|
|
{
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.lgr = LG_Renderers[i];
|
2017-12-17 12:15:18 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-12-14 06:42:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 01:06:54 +00:00
|
|
|
if (!g_state.lgr)
|
2017-12-14 06:42:16 +00:00
|
|
|
{
|
|
|
|
DEBUG_INFO("Unable to find a suitable renderer");
|
|
|
|
return -1;
|
|
|
|
}
|
2017-12-13 23:30:55 +00:00
|
|
|
|
2019-10-26 01:03:10 +00:00
|
|
|
// all our ducks are in a line, create the window
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.window = SDL_CreateWindow(
|
2019-03-13 20:17:24 +00:00
|
|
|
params.windowTitle,
|
2017-11-25 09:16:26 +00:00
|
|
|
params.center ? SDL_WINDOWPOS_CENTERED : params.x,
|
|
|
|
params.center ? SDL_WINDOWPOS_CENTERED : params.y,
|
|
|
|
params.w,
|
|
|
|
params.h,
|
|
|
|
(
|
2017-12-14 06:42:16 +00:00
|
|
|
SDL_WINDOW_SHOWN |
|
2017-11-28 08:48:40 +00:00
|
|
|
(params.allowResize ? SDL_WINDOW_RESIZABLE : 0) |
|
2017-12-14 06:42:16 +00:00
|
|
|
(params.borderless ? SDL_WINDOW_BORDERLESS : 0) |
|
2019-05-26 06:30:24 +00:00
|
|
|
(params.maximize ? SDL_WINDOW_MAXIMIZED : 0) |
|
2017-12-14 06:42:16 +00:00
|
|
|
sdlFlags
|
2017-11-25 09:16:26 +00:00
|
|
|
)
|
2017-11-25 06:51:34 +00:00
|
|
|
);
|
|
|
|
|
2021-01-04 01:06:54 +00:00
|
|
|
if (g_state.window == NULL)
|
2019-02-20 21:27:08 +00:00
|
|
|
{
|
2018-07-28 20:37:48 +00:00
|
|
|
DEBUG_ERROR("Could not create an SDL window: %s\n", SDL_GetError());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-01-15 09:30:03 +00:00
|
|
|
SDL_VERSION(&g_state.wminfo.version);
|
|
|
|
if (!SDL_GetWindowWMInfo(g_state.window, &g_state.wminfo))
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Could not get SDL window information %s", SDL_GetError());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-01-18 18:29:25 +00:00
|
|
|
// enable WM events
|
|
|
|
SDL_EventState(SDL_SYSWMEVENT, SDL_ENABLE);
|
|
|
|
|
2021-01-15 09:30:03 +00:00
|
|
|
g_state.ds->init(&g_state.wminfo);
|
|
|
|
|
2020-11-27 23:16:11 +00:00
|
|
|
SDL_SetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS,
|
|
|
|
params.minimizeOnFocusLoss ? "1" : "0");
|
2017-12-15 02:27:53 +00:00
|
|
|
|
2020-11-27 23:19:17 +00:00
|
|
|
if (params.fullscreen)
|
2021-01-04 01:06:54 +00:00
|
|
|
SDL_SetWindowFullscreen(g_state.window, SDL_WINDOW_FULLSCREEN_DESKTOP);
|
2020-11-27 23:19:17 +00:00
|
|
|
|
2018-10-03 14:09:00 +00:00
|
|
|
if (!params.center)
|
2021-01-04 01:06:54 +00:00
|
|
|
SDL_SetWindowPosition(g_state.window, params.x, params.y);
|
2018-10-03 14:09:00 +00:00
|
|
|
|
2018-12-16 09:01:19 +00:00
|
|
|
// ensure the initial window size is stored in the state
|
2021-01-04 01:06:54 +00:00
|
|
|
SDL_GetWindowSize(g_state.window, &g_state.windowW, &g_state.windowH);
|
2018-12-16 09:01:19 +00:00
|
|
|
|
2019-02-20 18:58:07 +00:00
|
|
|
// ensure renderer viewport is aware of the current window size
|
|
|
|
updatePositionInfo();
|
|
|
|
|
2020-11-07 23:58:18 +00:00
|
|
|
if (params.fpsMin <= 0)
|
2020-08-20 05:50:33 +00:00
|
|
|
{
|
2020-10-26 07:09:45 +00:00
|
|
|
// default 30 fps
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.frameTime = 1000000000ULL / 30ULL;
|
2020-08-20 05:50:33 +00:00
|
|
|
}
|
2019-08-30 01:36:28 +00:00
|
|
|
else
|
2019-07-09 01:13:24 +00:00
|
|
|
{
|
2020-10-26 07:09:45 +00:00
|
|
|
DEBUG_INFO("Using the FPS minimum from args: %d", params.fpsMin);
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.frameTime = 1000000000ULL / (unsigned long long)params.fpsMin;
|
2019-07-09 01:13:24 +00:00
|
|
|
}
|
2019-08-30 01:36:28 +00:00
|
|
|
|
2021-01-08 12:12:15 +00:00
|
|
|
// create our custom event
|
|
|
|
e_SDLEvent = SDL_RegisterEvents(1);
|
|
|
|
|
2019-03-28 15:06:37 +00:00
|
|
|
register_key_binds();
|
|
|
|
|
2020-12-05 22:58:50 +00:00
|
|
|
initSDLCursor();
|
2017-12-03 08:53:30 +00:00
|
|
|
if (params.hideMouse)
|
|
|
|
SDL_ShowCursor(SDL_DISABLE);
|
2017-10-26 19:31:05 +00:00
|
|
|
|
2019-12-09 16:30:04 +00:00
|
|
|
// setup the startup condition
|
2020-01-02 13:08:43 +00:00
|
|
|
if (!(e_startup = lgCreateEvent(false, 0)))
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("failed to create the startup event");
|
|
|
|
return -1;
|
|
|
|
}
|
2019-12-09 16:30:04 +00:00
|
|
|
|
2020-05-21 03:41:59 +00:00
|
|
|
// setup the new frame event
|
|
|
|
if (!(e_frame = lgCreateEvent(true, 0)))
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("failed to create the frame event");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-10-26 01:03:10 +00:00
|
|
|
// start the renderThread so we don't just display junk
|
2020-01-02 12:59:06 +00:00
|
|
|
if (!lgCreateThread("renderThread", renderThread, NULL, &t_render))
|
2017-10-19 04:15:49 +00:00
|
|
|
{
|
2019-10-26 01:03:10 +00:00
|
|
|
DEBUG_ERROR("render create thread failed");
|
|
|
|
return -1;
|
|
|
|
}
|
2017-11-25 06:51:34 +00:00
|
|
|
|
2019-10-26 01:03:10 +00:00
|
|
|
// ensure mouse acceleration is identical in server mode
|
|
|
|
SDL_SetHintWithPriority(SDL_HINT_MOUSE_RELATIVE_MODE_WARP, "1", SDL_HINT_OVERRIDE);
|
|
|
|
SDL_SetEventFilter(eventFilter, NULL);
|
2017-10-19 04:15:49 +00:00
|
|
|
|
2020-05-17 01:25:27 +00:00
|
|
|
// wait for startup to complete so that any error messages below are output at
|
|
|
|
// the end of the output
|
|
|
|
lgWaitEvent(e_startup, TIMEOUT_INFINITE);
|
|
|
|
|
2021-01-15 09:30:03 +00:00
|
|
|
g_state.ds->startup();
|
|
|
|
g_state.cbAvailable = g_state.ds->cbInit && g_state.ds->cbInit();
|
|
|
|
if (g_state.cbAvailable)
|
2021-01-12 01:55:42 +00:00
|
|
|
g_state.cbRequestList = ll_new();
|
|
|
|
|
2020-01-09 09:32:42 +00:00
|
|
|
LGMP_STATUS status;
|
2020-05-20 23:28:41 +00:00
|
|
|
|
2021-01-04 01:06:54 +00:00
|
|
|
while(g_state.state == APP_STATE_RUNNING)
|
2020-05-20 23:28:41 +00:00
|
|
|
{
|
2021-01-04 01:06:54 +00:00
|
|
|
if ((status = lgmpClientInit(g_state.shm.mem, g_state.shm.size, &g_state.lgmp)) == LGMP_OK)
|
2020-05-20 23:28:41 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
DEBUG_ERROR("lgmpClientInit Failed: %s", lgmpStatusString(status));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-05-20 23:32:08 +00:00
|
|
|
/* this short timeout is to allow the LGMP host to update the timestamp before
|
|
|
|
* we start checking for a valid session */
|
|
|
|
SDL_WaitEventTimeout(NULL, 200);
|
|
|
|
|
2021-01-09 13:56:34 +00:00
|
|
|
if (params.captureOnStart)
|
|
|
|
setGrab(true);
|
2021-01-08 12:12:15 +00:00
|
|
|
|
2020-05-17 01:54:07 +00:00
|
|
|
uint32_t udataSize;
|
|
|
|
KVMFR *udata;
|
2020-08-11 04:45:43 +00:00
|
|
|
int waitCount = 0;
|
2020-05-17 01:13:08 +00:00
|
|
|
|
2020-08-11 04:45:43 +00:00
|
|
|
restart:
|
2021-01-04 01:06:54 +00:00
|
|
|
while(g_state.state == APP_STATE_RUNNING)
|
2020-05-17 01:54:07 +00:00
|
|
|
{
|
2021-01-04 01:06:54 +00:00
|
|
|
if ((status = lgmpClientSessionInit(g_state.lgmp, &udataSize, (uint8_t **)&udata)) == LGMP_OK)
|
2020-01-09 09:32:42 +00:00
|
|
|
break;
|
2018-05-28 05:30:04 +00:00
|
|
|
|
2020-05-20 23:28:41 +00:00
|
|
|
if (status != LGMP_ERR_INVALID_SESSION && status != LGMP_ERR_INVALID_MAGIC)
|
2020-01-09 09:32:42 +00:00
|
|
|
{
|
2020-05-20 23:28:41 +00:00
|
|
|
DEBUG_ERROR("lgmpClientSessionInit Failed: %s", lgmpStatusString(status));
|
|
|
|
return -1;
|
|
|
|
}
|
2020-05-17 01:54:07 +00:00
|
|
|
|
2020-05-20 23:28:41 +00:00
|
|
|
if (waitCount++ == 0)
|
|
|
|
{
|
|
|
|
DEBUG_BREAK();
|
|
|
|
DEBUG_INFO("The host application seems to not be running");
|
|
|
|
DEBUG_INFO("Waiting for the host application to start...");
|
|
|
|
}
|
2020-05-17 01:54:07 +00:00
|
|
|
|
2020-05-20 23:28:41 +00:00
|
|
|
if (waitCount == 30)
|
|
|
|
{
|
|
|
|
DEBUG_BREAK();
|
|
|
|
DEBUG_INFO("Please check the host application is running and is the correct version");
|
|
|
|
DEBUG_INFO("Check the host log in your guest at %%TEMP%%\\looking-glass-host.txt");
|
|
|
|
DEBUG_INFO("Continuing to wait...");
|
2020-05-17 01:13:08 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 23:28:41 +00:00
|
|
|
SDL_WaitEventTimeout(NULL, 1000);
|
2019-10-26 01:03:10 +00:00
|
|
|
}
|
2017-12-13 23:08:47 +00:00
|
|
|
|
2021-01-04 01:06:54 +00:00
|
|
|
if (g_state.state != APP_STATE_RUNNING)
|
2019-10-26 01:03:10 +00:00
|
|
|
return -1;
|
2020-01-09 09:32:42 +00:00
|
|
|
|
2020-08-11 04:52:22 +00:00
|
|
|
// dont show warnings again after the first startup
|
|
|
|
waitCount = 100;
|
|
|
|
|
2020-05-29 04:14:31 +00:00
|
|
|
const bool magicMatches = memcmp(udata->magic, KVMFR_MAGIC, sizeof(udata->magic)) == 0;
|
|
|
|
if (udataSize != sizeof(KVMFR) || !magicMatches || udata->version != KVMFR_VERSION)
|
2020-05-17 01:54:07 +00:00
|
|
|
{
|
|
|
|
DEBUG_BREAK();
|
|
|
|
DEBUG_ERROR("The host application is not compatible with this client");
|
|
|
|
DEBUG_ERROR("This is not a Looking Glass error, do not report this");
|
2020-05-29 04:14:31 +00:00
|
|
|
DEBUG_ERROR("Please install the matching host application for this client");
|
|
|
|
|
|
|
|
if (magicMatches)
|
2020-05-29 04:24:06 +00:00
|
|
|
{
|
2020-05-29 04:14:31 +00:00
|
|
|
DEBUG_ERROR("Expected KVMFR version %d, got %d", KVMFR_VERSION, udata->version);
|
2021-01-04 22:18:40 +00:00
|
|
|
DEBUG_ERROR("Client version: %s", BUILD_VERSION);
|
2020-05-29 04:24:06 +00:00
|
|
|
if (udata->version >= 2)
|
2021-01-04 22:18:40 +00:00
|
|
|
DEBUG_ERROR(" Host version: %s", udata->hostver);
|
2020-05-29 04:24:06 +00:00
|
|
|
}
|
2020-05-29 04:14:31 +00:00
|
|
|
else
|
|
|
|
DEBUG_ERROR("Invalid KVMFR magic");
|
|
|
|
|
2020-05-17 01:54:07 +00:00
|
|
|
DEBUG_BREAK();
|
2021-01-04 22:13:54 +00:00
|
|
|
|
|
|
|
if (magicMatches)
|
|
|
|
{
|
|
|
|
DEBUG_INFO("Waiting for you to upgrade the host application");
|
|
|
|
while (g_state.state == APP_STATE_RUNNING && udata->version != KVMFR_VERSION)
|
|
|
|
SDL_WaitEventTimeout(NULL, 1000);
|
|
|
|
|
|
|
|
if (g_state.state != APP_STATE_RUNNING)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return -1;
|
2020-05-17 01:54:07 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 04:14:31 +00:00
|
|
|
DEBUG_INFO("Host ready, reported version: %s", udata->hostver);
|
|
|
|
DEBUG_INFO("Starting session");
|
2017-12-19 13:53:45 +00:00
|
|
|
|
2020-01-02 12:59:06 +00:00
|
|
|
if (!lgCreateThread("cursorThread", cursorThread, NULL, &t_cursor))
|
2019-12-09 16:30:04 +00:00
|
|
|
{
|
|
|
|
DEBUG_ERROR("cursor create thread failed");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-01-02 12:59:06 +00:00
|
|
|
if (!lgCreateThread("frameThread", frameThread, NULL, &t_frame))
|
2019-10-26 01:03:10 +00:00
|
|
|
{
|
|
|
|
DEBUG_ERROR("frame create thread failed");
|
|
|
|
return -1;
|
|
|
|
}
|
2017-12-19 13:53:45 +00:00
|
|
|
|
2021-01-04 01:06:54 +00:00
|
|
|
while(g_state.state == APP_STATE_RUNNING)
|
2019-10-26 01:03:10 +00:00
|
|
|
{
|
2021-01-04 01:06:54 +00:00
|
|
|
if (!lgmpClientSessionValid(g_state.lgmp))
|
2020-01-09 09:32:42 +00:00
|
|
|
{
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.state = APP_STATE_RESTART;
|
2020-04-12 03:48:59 +00:00
|
|
|
break;
|
2020-01-09 09:32:42 +00:00
|
|
|
}
|
2020-08-11 04:52:22 +00:00
|
|
|
SDL_WaitEventTimeout(NULL, 100);
|
2017-10-19 04:15:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 01:06:54 +00:00
|
|
|
if (g_state.state == APP_STATE_RESTART)
|
2020-08-11 04:30:44 +00:00
|
|
|
{
|
|
|
|
lgSignalEvent(e_startup);
|
|
|
|
lgSignalEvent(e_frame);
|
|
|
|
lgJoinThread(t_frame , NULL);
|
|
|
|
lgJoinThread(t_cursor, NULL);
|
2020-08-11 04:45:43 +00:00
|
|
|
t_frame = NULL;
|
|
|
|
t_cursor = NULL;
|
|
|
|
|
2020-12-03 13:32:28 +00:00
|
|
|
lgInit();
|
|
|
|
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.lgr->on_restart(g_state.lgrData);
|
2020-08-11 04:30:44 +00:00
|
|
|
|
|
|
|
DEBUG_INFO("Waiting for the host to restart...");
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
|
2019-10-26 01:03:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-01-14 06:05:26 +00:00
|
|
|
static void lg_shutdown(void)
|
2019-10-26 01:03:10 +00:00
|
|
|
{
|
2021-01-04 01:06:54 +00:00
|
|
|
g_state.state = APP_STATE_SHUTDOWN;
|
2018-01-29 06:27:12 +00:00
|
|
|
if (t_render)
|
2020-01-27 01:25:47 +00:00
|
|
|
{
|
|
|
|
lgSignalEvent(e_startup);
|
2020-05-21 03:41:59 +00:00
|
|
|
lgSignalEvent(e_frame);
|
2020-01-02 12:59:06 +00:00
|
|
|
lgJoinThread(t_render, NULL);
|
2020-01-27 01:25:47 +00:00
|
|
|
}
|
2018-01-29 06:27:12 +00:00
|
|
|
|
2021-01-04 01:06:54 +00:00
|
|
|
lgmpClientFree(&g_state.lgmp);
|
2020-01-09 09:32:42 +00:00
|
|
|
|
2020-05-21 03:41:59 +00:00
|
|
|
if (e_frame)
|
|
|
|
{
|
|
|
|
lgFreeEvent(e_frame);
|
|
|
|
e_frame = NULL;
|
|
|
|
}
|
|
|
|
|
2020-01-02 13:08:43 +00:00
|
|
|
if (e_startup)
|
2019-12-09 16:30:04 +00:00
|
|
|
{
|
2020-01-02 13:08:43 +00:00
|
|
|
lgFreeEvent(e_startup);
|
|
|
|
e_startup = NULL;
|
2019-12-09 16:30:04 +00:00
|
|
|
}
|
|
|
|
|
2018-01-29 06:27:12 +00:00
|
|
|
// if spice is still connected send key up events for any pressed keys
|
2019-02-25 16:06:53 +00:00
|
|
|
if (params.useSpiceInput && spice_ready())
|
2018-01-29 06:27:12 +00:00
|
|
|
{
|
2021-01-18 02:54:21 +00:00
|
|
|
for(uint32_t scancode = 0; scancode < KEY_MAX; ++scancode)
|
|
|
|
if (g_state.keyDown[scancode])
|
2018-01-29 06:27:12 +00:00
|
|
|
{
|
2021-01-18 02:54:21 +00:00
|
|
|
g_state.keyDown[scancode] = false;
|
2018-01-29 06:27:12 +00:00
|
|
|
spice_key_up(scancode);
|
|
|
|
}
|
|
|
|
|
2020-02-01 03:24:23 +00:00
|
|
|
spice_disconnect();
|
2018-01-29 06:27:12 +00:00
|
|
|
if (t_spice)
|
2020-01-02 12:59:06 +00:00
|
|
|
lgJoinThread(t_spice, NULL);
|
2018-01-29 06:27:12 +00:00
|
|
|
}
|
2017-10-19 04:15:49 +00:00
|
|
|
|
2021-01-15 09:30:03 +00:00
|
|
|
if (g_state.ds)
|
|
|
|
g_state.ds->shutdown();
|
2019-02-22 11:38:52 +00:00
|
|
|
|
2021-01-15 09:30:03 +00:00
|
|
|
if (g_state.cbRequestList)
|
|
|
|
{
|
2021-01-04 01:06:54 +00:00
|
|
|
ll_free(g_state.cbRequestList);
|
2021-01-15 09:30:03 +00:00
|
|
|
g_state.cbRequestList = NULL;
|
2019-02-24 18:43:18 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 01:06:54 +00:00
|
|
|
if (g_state.window)
|
2021-01-11 23:52:42 +00:00
|
|
|
{
|
2021-01-15 09:30:03 +00:00
|
|
|
g_state.ds->free();
|
2021-01-04 01:06:54 +00:00
|
|
|
SDL_DestroyWindow(g_state.window);
|
2021-01-11 23:52:42 +00:00
|
|
|
}
|
2017-10-19 04:15:49 +00:00
|
|
|
|
2017-10-26 19:31:05 +00:00
|
|
|
if (cursor)
|
|
|
|
SDL_FreeCursor(cursor);
|
|
|
|
|
2021-01-04 01:06:54 +00:00
|
|
|
ivshmemClose(&g_state.shm);
|
2017-10-19 04:15:49 +00:00
|
|
|
|
2019-10-26 01:03:10 +00:00
|
|
|
release_key_binds();
|
2021-01-05 00:47:17 +00:00
|
|
|
|
2017-10-19 04:15:49 +00:00
|
|
|
SDL_Quit();
|
2017-11-25 07:20:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char * argv[])
|
|
|
|
{
|
2020-05-29 04:18:02 +00:00
|
|
|
if (getuid() == 0)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Do not run looking glass as root!");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-10-08 15:17:20 +00:00
|
|
|
DEBUG_INFO("Looking Glass (%s)", BUILD_VERSION);
|
2019-10-26 01:03:10 +00:00
|
|
|
DEBUG_INFO("Locking Method: " LG_LOCK_MODE);
|
|
|
|
|
2019-05-26 15:20:05 +00:00
|
|
|
if (!installCrashHandler("/proc/self/exe"))
|
2019-04-11 06:41:52 +00:00
|
|
|
DEBUG_WARN("Failed to install the crash handler");
|
|
|
|
|
2019-05-21 05:03:59 +00:00
|
|
|
config_init();
|
2020-01-03 04:17:14 +00:00
|
|
|
ivshmemOptionsInit();
|
2019-05-21 05:03:59 +00:00
|
|
|
|
|
|
|
// early renderer setup for option registration
|
|
|
|
for(unsigned int i = 0; i < LG_RENDERER_COUNT; ++i)
|
|
|
|
LG_Renderers[i]->setup();
|
|
|
|
|
2019-03-30 04:52:00 +00:00
|
|
|
if (!config_load(argc, argv))
|
2017-12-19 00:58:38 +00:00
|
|
|
return -1;
|
2019-03-30 04:52:00 +00:00
|
|
|
|
2019-10-26 01:03:10 +00:00
|
|
|
const int ret = lg_run();
|
|
|
|
lg_shutdown();
|
2017-12-28 08:55:13 +00:00
|
|
|
|
2019-03-30 05:00:47 +00:00
|
|
|
config_free();
|
2017-12-28 08:55:13 +00:00
|
|
|
return ret;
|
2019-10-26 01:03:10 +00:00
|
|
|
|
2020-01-17 03:35:08 +00:00
|
|
|
}
|