add Linux high DPI support and only windows use scaled()

This commit is contained in:
vcaesar 2021-11-16 16:07:06 -04:00
parent e433e36e8a
commit faeb2b1e20
4 changed files with 55 additions and 23 deletions

View File

@ -221,7 +221,7 @@ func GetMouseColor() string {
return GetPixelColor(x, y) return GetPixelColor(x, y)
} }
// SysScale get the sys scale (x11 todo) // SysScale get the sys scale
func SysScale() float64 { func SysScale() float64 {
s := C.sys_scale() s := C.sys_scale()
return float64(s) return float64(s)
@ -229,7 +229,12 @@ func SysScale() float64 {
// Scaled get the screen scaled size // Scaled get the screen scaled size
func Scaled(x int) int { func Scaled(x int) int {
return int(float64(x) * ScaleF()) return Scaled0(x, ScaleF())
}
// Scaled0 return int(x * f)
func Scaled0(x int, f float64) int {
return int(float64(x) * f)
} }
// GetScreenSize get the screen size // GetScreenSize get the screen size
@ -247,15 +252,16 @@ func GetScreenRect(displayId ...int) Rect {
} }
rect := C.getScreenRect(C.int32_t(display)) rect := C.getScreenRect(C.int32_t(display))
x, y, w, h := int(rect.origin.x), int(rect.origin.y),
int(rect.size.w), int(rect.size.h)
if runtime.GOOS == "windows" {
f := ScaleF()
x, y, w, h = Scaled0(x, f), Scaled0(y, f), Scaled0(w, f), Scaled0(h, f)
}
return Rect{ return Rect{
Point{ Point{X: x, Y: y},
X: Scaled(int(rect.origin.x)), Size{W: w, H: h},
Y: Scaled(int(rect.origin.y)),
},
Size{
W: Scaled(int(rect.size.w)),
H: Scaled(int(rect.size.h)),
},
} }
} }

View File

@ -15,5 +15,9 @@ package robotgo
// ScaleF get the system scale val // ScaleF get the system scale val
func ScaleF() float64 { func ScaleF() float64 {
return SysScale() f := SysScale()
if f == 0.0 {
f = 1.0
}
return f
} }

View File

@ -54,7 +54,11 @@ func SetFocus(hwnd win.HWND) win.HWND {
// ScaleF get the system scale val // ScaleF get the system scale val
func ScaleF() float64 { func ScaleF() float64 {
return float64(GetMainDPI()) / 96.0 f := float64(GetMainDPI()) / 96.0
if f == 0.0 {
f = 1.0
}
return f
} }
// GetMainDPI get the display dpi // GetMainDPI get the display dpi

View File

@ -9,6 +9,11 @@
// except according to those terms. // except according to those terms.
// #include "../base/os.h" // #include "../base/os.h"
#if defined(USE_X11)
// #include <X11/Xlib.h>
// #include <X11/Xatom.h>
#include <X11/Xresource.h>
#endif
Bounds get_client(uintptr pid, uintptr isHwnd); Bounds get_client(uintptr pid, uintptr isHwnd);
@ -38,21 +43,34 @@ double sys_scale() {
return pixelWidth / targetWidth; return pixelWidth / targetWidth;
#elif defined(USE_X11) #elif defined(USE_X11)
double xres;
// double xres; Display *dpy;
// Display *dpy;
// char *displayname = NULL; char *displayname = NULL;
// int scr = 0; /* Screen number */ int scr = 0; /* Screen number */
// dpy = XOpenDisplay (displayname); dpy = XOpenDisplay(displayname);
// xres = ((((double) DisplayWidth(dpy, scr)) * 25.4) / xres = ((((double) DisplayWidth(dpy, scr)) * 25.4) /
// ((double) DisplayWidthMM(dpy, scr))); ((double) DisplayWidthMM(dpy, scr)));
// XCloseDisplay (dpy); // https://github.com/glfw/glfw/issues/1019#issuecomment-302772498
char *rms = XResourceManagerString(dpy);
if (rms) {
XrmDatabase db;
XrmValue value;
char *type = NULL;
// return xres / 96.0; XrmInitialize(); /* Need to initialize the DB before calling Xrm* functions */
return 1.0; db = XrmGetStringDatabase(rms);
if (XrmGetResource(db, "Xft.dpi", "String", &type, &value) == True) {
if (value.addr) {
xres = atof(value.addr);
}
}
}
XCloseDisplay (dpy);
return xres / 96.0;
#elif defined(IS_WINDOWS) #elif defined(IS_WINDOWS)
double s = scaleX() / 96.0; double s = scaleX() / 96.0;
return s; return s;