diff --git a/robotgo.go b/robotgo.go index e7fb44c..d73210c 100644 --- a/robotgo.go +++ b/robotgo.go @@ -221,7 +221,7 @@ func GetMouseColor() string { return GetPixelColor(x, y) } -// SysScale get the sys scale (x11 todo) +// SysScale get the sys scale func SysScale() float64 { s := C.sys_scale() return float64(s) @@ -229,7 +229,12 @@ func SysScale() float64 { // Scaled get the screen scaled size 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 @@ -247,15 +252,16 @@ func GetScreenRect(displayId ...int) Rect { } 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{ - Point{ - X: Scaled(int(rect.origin.x)), - Y: Scaled(int(rect.origin.y)), - }, - Size{ - W: Scaled(int(rect.size.w)), - H: Scaled(int(rect.size.h)), - }, + Point{X: x, Y: y}, + Size{W: w, H: h}, } } diff --git a/robotgo_mac.go b/robotgo_mac.go index d01706f..365914b 100644 --- a/robotgo_mac.go +++ b/robotgo_mac.go @@ -15,5 +15,9 @@ package robotgo // ScaleF get the system scale val func ScaleF() float64 { - return SysScale() + f := SysScale() + if f == 0.0 { + f = 1.0 + } + return f } diff --git a/robotgo_win.go b/robotgo_win.go index a7abba8..3826864 100644 --- a/robotgo_win.go +++ b/robotgo_win.go @@ -54,7 +54,11 @@ func SetFocus(hwnd win.HWND) win.HWND { // ScaleF get the system scale val 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 diff --git a/window/win_sys.h b/window/win_sys.h index 317f5bc..7d10e96 100644 --- a/window/win_sys.h +++ b/window/win_sys.h @@ -9,6 +9,11 @@ // except according to those terms. // #include "../base/os.h" +#if defined(USE_X11) + // #include + // #include + #include +#endif Bounds get_client(uintptr pid, uintptr isHwnd); @@ -38,21 +43,34 @@ double sys_scale() { return pixelWidth / targetWidth; #elif defined(USE_X11) - - // double xres; - // Display *dpy; + double xres; + Display *dpy; - // char *displayname = NULL; - // int scr = 0; /* Screen number */ + char *displayname = NULL; + int scr = 0; /* Screen number */ - // dpy = XOpenDisplay (displayname); - // xres = ((((double) DisplayWidth(dpy, scr)) * 25.4) / - // ((double) DisplayWidthMM(dpy, scr))); + dpy = XOpenDisplay(displayname); + xres = ((((double) DisplayWidth(dpy, scr)) * 25.4) / + ((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; - return 1.0; + XrmInitialize(); /* Need to initialize the DB before calling Xrm* functions */ + 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) double s = scaleX() / 96.0; return s;