LookingGlass/client/src/main.h

300 lines
7.6 KiB
C
Raw Normal View History

/**
* Looking Glass
* Copyright © 2017-2021 The Looking Glass Authors
* https://looking-glass.io
*
* 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
*/
#include <stdbool.h>
#include <stdatomic.h>
#include <linux/input.h>
#include "dynamic/displayservers.h"
#include "dynamic/renderers.h"
#include "dynamic/audiodev.h"
#include "common/thread.h"
#include "common/types.h"
#include "common/ivshmem.h"
#include "common/locking.h"
#include "common/ringbuffer.h"
#include "common/event.h"
#include <purespice.h>
2020-01-09 09:32:42 +00:00
#include <lgmp/client.h>
#include "cimgui.h"
enum RunState
{
APP_STATE_RUNNING,
APP_STATE_RESTART,
APP_STATE_SHUTDOWN
};
struct AppState
{
enum RunState state;
ImGuiIO * io;
ImGuiStyle * style;
struct ll * overlays;
char * fontName;
ImFont * fontLarge;
bool overlayInput;
ImGuiMouseCursor cursorLast;
char * imGuiIni;
bool modCtrl;
bool modShift;
bool modAlt;
bool modSuper;
uint64_t lastImGuiFrame;
bool alertShow;
char * alertMessage;
LG_MsgAlert alertType;
uint64_t alertTimeout;
struct LG_DisplayServerOps * ds;
bool dsInitialized;
bool jitRender;
bool stopVideo;
bool ignoreInput;
bool escapeActive;
uint64_t escapeTime;
int escapeAction;
bool escapeHelp;
KeybindHandle bindings[KEY_MAX];
const char * keyDescription[KEY_MAX];
bool keyDown[KEY_MAX];
bool haveSrcSize;
struct Point windowPos;
int windowW, windowH;
int windowCX, windowCY;
double windowScale;
LG_RendererRotate rotate;
bool focused;
struct Border border;
struct Point srcSize;
LG_RendererRect dstRect;
bool posInfoValid;
bool alignToGuest;
LG_Renderer * lgr;
atomic_int lgrResize;
LG_Lock lgrLock;
bool useDMA;
bool cbAvailable;
PSDataType cbType;
bool cbChunked;
size_t cbXfer;
struct ll * cbRequestList;
struct IVSHMEM shm;
2020-01-09 09:32:42 +00:00
PLGMPClient lgmp;
PLGMPClientQueue pointerQueue;
KVMFRFeatureFlags kvmfrFeatures;
LGThread * cursorThread;
LGThread * frameThread;
LGEvent * frameEvent;
atomic_bool invalidateWindow;
bool formatValid;
uint64_t frameTime;
uint64_t overlayFrameTime;
uint64_t lastFrameTime;
bool lastFrameTimeValid;
uint64_t lastRenderTime;
bool lastRenderTimeValid;
RingBuffer renderTimings;
RingBuffer renderDuration;
RingBuffer uploadTimings;
atomic_uint_least64_t pendingCount;
atomic_uint_least64_t renderCount, frameCount;
_Atomic(float) fps, ups;
uint64_t resizeTimeout;
bool resizeDone;
bool autoIdleInhibitState;
struct LG_AudioDevOps * audioDev;
bool audioStarted;
};
struct AppParams
{
bool autoResize;
bool allowResize;
bool keepAspect;
bool forceAspect;
bool dontUpscale;
bool shrinkOnUpscale;
bool borderless;
bool fullscreen;
bool maximize;
bool minimizeOnFocusLoss;
bool center;
int x, y;
unsigned int w, h;
int fpsMin;
LG_RendererRotate winRotate;
bool useSpiceInput;
bool useSpiceClipboard;
bool useSpiceAudio;
const char * spiceHost;
unsigned int spicePort;
bool clipboardToVM;
bool clipboardToLocal;
bool scaleMouseInput;
bool hideMouse;
bool ignoreQuit;
bool noScreensaver;
bool autoScreensaver;
bool grabKeyboard;
bool grabKeyboardOnFocus;
int escapeKey;
bool ignoreWindowsKeys;
bool releaseKeysOnFocusLoss;
bool showAlerts;
bool captureOnStart;
bool quickSplash;
bool alwaysShowCursor;
uint64_t helpMenuDelayUs;
const char * uiFont;
int uiSize;
bool jitRender;
unsigned int cursorPollInterval;
unsigned int framePollInterval;
bool allowDMA;
bool forceRenderer;
unsigned int forceRendererIndex;
const char * windowTitle;
bool mouseRedraw;
int mouseSens;
bool mouseSmoothing;
bool rawMouse;
bool autoCapture;
bool captureInputOnly;
bool showCursorDot;
};
struct CBRequest
{
PSDataType type;
LG_ClipboardReplyFn replyFn;
void * opaque;
};
struct KeybindHandle
{
int sc;
KeybindFn callback;
void * opaque;
};
enum WarpState
{
WARP_STATE_ON,
WARP_STATE_OFF
};
struct CursorInfo
{
/* x & y postiion */
int x , y;
/* pointer hotspot offsets */
int hx, hy;
/* true if the pointer is visible on the guest */
bool visible;
/* true if the details in this struct are valid */
bool valid;
};
struct CursorState
{
/* cursor is in grab mode */
bool grab;
/* true if we are to draw the cursor on screen */
bool draw;
/* true if the cursor is currently in our window */
bool inWindow;
/* true if the cursor is currently in the guest view area */
bool inView;
/* true if the guest should be realigned to the host when next drawn */
bool realign;
/* true if the cursor needs re-drawing/updating */
bool redraw;
/* true if the cursor movements should be scaled */
bool useScale;
/* the amount to scale the X & Y movements by */
struct DoublePoint scale;
/* the error accumulator */
struct DoublePoint acc;
/* the local position */
struct DoublePoint pos;
/* true if the position is valid */
bool valid;
/* the button state */
unsigned int buttons;
/* the delta since last warp when in auto capture mode */
struct DoublePoint delta;
/* the scale factor for the mouse sensitiviy */
int sens;
/* the mouse warp state */
enum WarpState warpState;
/* the guest's cursor position */
struct CursorInfo guest;
[client] spice/wayland: improve cursor tracking logic One of the major issues with the old tracking code is a data race between the cursor thread updating g_cursor.guest and the app_handleMouseBasic function. Specifically, the latter may have sent mouse input via spice that has not been processed by the guest and updated g_cursor.guest, but the guest may overwrite g_cursor.guest to a previous state before the input is processed. This causes some movements to be doubled. Eventually, the cursor positions will synchronize, but this nevertheless causes a lot of jitter. In this commit, we introduce a new field g_cursor.projected, which is unambiguously the position of the cursor after taking into account all the input already sent via spice. This is synced up to the guest cursor upon entering the window and when the host restarts. Afterwards, all mouse movements will be based on this position. This eliminates all cursor jitter as far as I could tell. Also, the cursor is now synced to the host position when exiting capture mode. A downside of this commit is that if the 1:1 movement patch is not correctly applied, the cursor position would be wildly off instead of simply jittering, but that is an unsupported configuration and should not matter. Also unsupported is when an application in guest moves the cursor programmatically and bypassing spice. When using those applications, capture mode must be on. Before this commit, we try to move the guest cursor back to where it should be, but it's inherently fragile and may lead to scenarios such as wild movements in first-person shooters.
2021-01-21 02:05:50 +00:00
/* the projected position after move, for app_handleMouseBasic only */
struct Point projected;
};
// forwards
extern struct AppState g_state;
extern struct CursorState g_cursor;
extern struct AppParams g_params;
int main_cursorThread(void * unused);
int main_frameThread(void * unused);
#define RENDERER(fn, ...) g_state.lgr->ops.fn(g_state.lgr, ##__VA_ARGS__)