get display DPI info to scale mouse movement

This commit is contained in:
Quantum
2021-01-04 16:01:24 -05:00
committed by Geoffrey McRae
parent 0bd5f0b2f1
commit 7e4d323427
10 changed files with 123 additions and 36 deletions

View File

@@ -24,6 +24,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA
#include "common/option.h"
#include "common/locking.h"
#include "common/event.h"
#include "common/dpi.h"
#include <assert.h>
#include <stdatomic.h>
@@ -98,12 +99,12 @@ struct iface
unsigned int pitch;
unsigned int stride;
CaptureFormat format;
unsigned int dpi;
int lastPointerX, lastPointerY;
bool lastPointerVisible;
};
static bool dpiDone = false;
static struct iface * this = NULL;
// forwards
@@ -210,22 +211,6 @@ static bool dxgi_init()
DEBUG_INFO("looking-glass-host.exe InstallService");
}
// this is required for DXGI 1.5 support to function
if (!dpiDone)
{
DECLARE_HANDLE(DPI_AWARENESS_CONTEXT);
#define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 ((DPI_AWARENESS_CONTEXT)-4)
typedef BOOL (*User32_SetProcessDpiAwarenessContext)(DPI_AWARENESS_CONTEXT value);
HMODULE user32 = LoadLibraryA("user32.dll");
User32_SetProcessDpiAwarenessContext fn;
fn = (User32_SetProcessDpiAwarenessContext)GetProcAddress(user32, "SetProcessDpiAwarenessContext");
if (fn)
fn(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
FreeLibrary(user32);
dpiDone = true;
}
HRESULT status;
DXGI_OUTPUT_DESC outputDesc;
@@ -388,6 +373,7 @@ static bool dxgi_init()
IDXGIAdapter1_GetDesc1(this->adapter, &adapterDesc);
this->width = outputDesc.DesktopCoordinates.right - outputDesc.DesktopCoordinates.left;
this->height = outputDesc.DesktopCoordinates.bottom - outputDesc.DesktopCoordinates.top;
this->dpi = monitor_dpi(outputDesc.Monitor);
++this->formatVer;
DEBUG_INFO("Device Descripion: %ls" , adapterDesc.Description);
@@ -688,6 +674,14 @@ static unsigned int dxgi_getMaxFrameSize()
return this->height * this->pitch;
}
static unsigned int dxgi_getMouseScale()
{
assert(this);
assert(this->initialized);
return this->dpi * 100 / DPI_100_PERCENT;
}
static CaptureResult dxgi_hResultToCaptureResult(const HRESULT status)
{
switch(status)
@@ -1013,6 +1007,7 @@ struct CaptureInterface Capture_DXGI =
.deinit = dxgi_deinit,
.free = dxgi_free,
.getMaxFrameSize = dxgi_getMaxFrameSize,
.getMouseScale = dxgi_getMouseScale,
.capture = dxgi_capture,
.waitFrame = dxgi_waitFrame,
.getFrame = dxgi_getFrame

View File

@@ -25,6 +25,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA
#include "common/framebuffer.h"
#include "common/event.h"
#include "common/thread.h"
#include "common/dpi.h"
#include <assert.h>
#include <stdlib.h>
#include <windows.h>
@@ -44,6 +45,7 @@ struct iface
unsigned int maxWidth , maxHeight;
unsigned int width , height;
unsigned int dpi;
unsigned int formatVer;
unsigned int grabWidth, grabHeight, grabStride;
@@ -65,7 +67,7 @@ static struct iface * this = NULL;
static void nvfbc_free();
static int pointerThread(void * unused);
static void getDesktopSize(unsigned int * width, unsigned int * height)
static void getDesktopSize(unsigned int * width, unsigned int * height, unsigned int * dpi)
{
HMONITOR monitor = MonitorFromWindow(GetDesktopWindow(), MONITOR_DEFAULTTOPRIMARY);
MONITORINFO monitorInfo = {
@@ -73,6 +75,7 @@ static void getDesktopSize(unsigned int * width, unsigned int * height)
};
GetMonitorInfo(monitor, &monitorInfo);
*dpi = monitor_dpi(monitor);
CloseHandle(monitor);
*width = monitorInfo.rcMonitor.right - monitorInfo.rcMonitor.left;
@@ -163,7 +166,7 @@ static bool nvfbc_create(
static bool nvfbc_init()
{
this->stop = false;
getDesktopSize(&this->width, &this->height);
getDesktopSize(&this->width, &this->height, &this->dpi);
lgResetEvent(this->frameEvent);
HANDLE event;
@@ -245,9 +248,14 @@ static unsigned int nvfbc_getMaxFrameSize()
return this->maxWidth * this->maxHeight * 4;
}
static unsigned int nvfbc_getMouseScale()
{
return this->dpi * 100 / DPI_100_PERCENT;
}
static CaptureResult nvfbc_capture()
{
getDesktopSize(&this->width, &this->height);
getDesktopSize(&this->width, &this->height, &this->dpi);
NvFBCFrameGrabInfo grabInfo;
CaptureResult result = NvFBCToSysCapture(
this->nvfbc,
@@ -397,6 +405,7 @@ struct CaptureInterface Capture_NVFBC =
.deinit = nvfbc_deinit,
.free = nvfbc_free,
.getMaxFrameSize = nvfbc_getMaxFrameSize,
.getMouseScale = nvfbc_getMouseScale,
.capture = nvfbc_capture,
.waitFrame = nvfbc_waitFrame,
.getFrame = nvfbc_getFrame

View File

@@ -226,6 +226,18 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
// setup a handler for ctrl+c
SetConsoleCtrlHandler(CtrlHandler, TRUE);
// enable high DPI awareness
// this is required for DXGI 1.5 support to function and also capturing desktops with high DPI
DECLARE_HANDLE(DPI_AWARENESS_CONTEXT);
#define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 ((DPI_AWARENESS_CONTEXT)-4)
typedef BOOL (*User32_SetProcessDpiAwarenessContext)(DPI_AWARENESS_CONTEXT value);
HMODULE user32 = GetModuleHandle("user32.dll");
User32_SetProcessDpiAwarenessContext fn;
fn = (User32_SetProcessDpiAwarenessContext)GetProcAddress(user32, "SetProcessDpiAwarenessContext");
if (fn)
fn(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
// create a message window so that our message pump works
WNDCLASSEX wx = {};
wx.cbSize = sizeof(WNDCLASSEX);
@@ -249,7 +261,6 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
app.messageWnd = CreateWindowEx(0, MAKEINTATOM(class), NULL, 0, 0, 0, 0, 0, NULL, NULL, hInstance, NULL);
// this is needed so that unprivileged processes can send us this message
HMODULE user32 = GetModuleHandle("user32.dll");
_ChangeWindowMessageFilterEx = (PChangeWindowMessageFilterEx)GetProcAddress(user32, "ChangeWindowMessageFilterEx");
if (_ChangeWindowMessageFilterEx)
_ChangeWindowMessageFilterEx(app.messageWnd, app.trayRestartMsg, MSGFLT_ALLOW, NULL);