From 337cd3f73a238d24ec25c29c7866f82e91c959a4 Mon Sep 17 00:00:00 2001 From: vcaesar Date: Mon, 3 Jan 2022 19:49:54 -0400 Subject: [PATCH] add sys scale option displayID support --- base/MMBitmap.h | 38 +++++++++++++++++++------------------- base/deadbeef_rand_c.h | 9 +++------ robotgo.go | 15 ++++++++------- robotgo_mac_unix.go | 4 ++-- robotgo_win.go | 9 +++++++-- window/alert_c.h | 18 +++++------------- window/win_sys.h | 42 ++++++++++++++++++++++-------------------- 7 files changed, 66 insertions(+), 69 deletions(-) diff --git a/base/MMBitmap.h b/base/MMBitmap.h index 31b66cf..9e73feb 100644 --- a/base/MMBitmap.h +++ b/base/MMBitmap.h @@ -33,7 +33,7 @@ typedef MMBitmap *MMBitmapRef; /* Creates new MMBitmap with the given values. * Follows the Create Rule (caller is responsible for destroy()'ing object). */ -MMBitmapRef createMMBitmap(uint8_t *buffer, size_t width, size_t height, +MMBitmapRef createMMBitmap_c(uint8_t *buffer, size_t width, size_t height, size_t bytewidth, uint8_t bitsPerPixel, uint8_t bytesPerPixel); @@ -43,12 +43,12 @@ void destroyMMBitmap(MMBitmapRef bitmap); /* Releases memory occupied by MMBitmap. Acts via CallBack method*/ void destroyMMBitmapBuffer(char * bitmapBuffer, void * hint); -/* Returns copy of MMBitmap, to be destroy()'d by caller. */ -MMBitmapRef copyMMBitmap(MMBitmapRef bitmap); +// /* Returns copy of MMBitmap, to be destroy()'d by caller. */ +// MMBitmapRef copyMMBitmap(MMBitmapRef bitmap); -/* Returns copy of one MMBitmap juxtaposed in another (to be destroy()'d - * by the caller.), or NULL on error. */ -MMBitmapRef copyMMBitmapFromPortion(MMBitmapRef source, MMRect rect); +// /* Returns copy of one MMBitmap juxtaposed in another (to be destroy()'d +// * by the caller.), or NULL on error. */ +// MMBitmapRef copyMMBitmapFromPortion(MMBitmapRef source, MMRect rect); #define MMBitmapPointInBounds(image, p) ((p).x < (image)->width && \ (p).y < (image)->height) @@ -72,19 +72,19 @@ MMBitmapRef copyMMBitmapFromPortion(MMBitmapRef source, MMRect rect); #define MMRGBHexAtPoint(image, x, y) \ hexFromMMRGB(MMRGBColorAtPoint(image, x, y)) -/* Increment either point.x or point.y depending on the position of point.x. - * That is, if x + 1 is >= width, increment y and start x at the beginning. - * Otherwise, increment x. - * - * This is used as a convenience macro to scan rows when calling functions such - * as findColorInRectAt() and findBitmapInBitmapAt(). */ -#define ITER_NEXT_POINT(pixel, width, start_x) \ -do { \ - if (++(pixel).x >= (width)) { \ - (pixel).x = start_x; \ - ++(point).y; \ - } \ -} while (0); +// /* Increment either point.x or point.y depending on the position of point.x. +// * That is, if x + 1 is >= width, increment y and start x at the beginning. +// * Otherwise, increment x. +// * +// * This is used as a convenience macro to scan rows when calling functions such +// * as findColorInRectAt() and findBitmapInBitmapAt(). */ +// #define ITER_NEXT_POINT(pixel, width, start_x) \ +// do { \ +// if (++(pixel).x >= (width)) { \ +// (pixel).x = start_x; \ +// ++(point).y; \ +// } \ +// } while (0); #ifdef __cplusplus } diff --git a/base/deadbeef_rand_c.h b/base/deadbeef_rand_c.h index 1de295f..d33062f 100644 --- a/base/deadbeef_rand_c.h +++ b/base/deadbeef_rand_c.h @@ -4,23 +4,20 @@ static uint32_t deadbeef_seed; static uint32_t deadbeef_beef = 0xdeadbeef; -uint32_t deadbeef_rand(void) -{ +uint32_t deadbeef_rand(void) { deadbeef_seed = (deadbeef_seed << 7) ^ ((deadbeef_seed >> 25) + deadbeef_beef); deadbeef_beef = (deadbeef_beef << 7) ^ ((deadbeef_beef >> 25) + 0xdeadbeef); return deadbeef_seed; } -void deadbeef_srand(uint32_t x) -{ +void deadbeef_srand(uint32_t x) { deadbeef_seed = x; deadbeef_beef = 0xdeadbeef; } /* Taken directly from the documentation: * http://inglorion.net/software/cstuff/deadbeef_rand/ */ -uint32_t deadbeef_generate_seed(void) -{ +uint32_t deadbeef_generate_seed(void) { uint32_t t = (uint32_t)time(NULL); uint32_t c = (uint32_t)clock(); return (t << 24) ^ (c << 11) ^ t ^ (size_t) &c; diff --git a/robotgo.go b/robotgo.go index f62390a..da6a6d9 100644 --- a/robotgo.go +++ b/robotgo.go @@ -236,14 +236,15 @@ func displayIdx(id ...int) int { } // SysScale get the sys scale -func SysScale() float64 { - s := C.sys_scale() +func SysScale(displayId ...int) float64 { + display := displayIdx(displayId...) + s := C.sys_scale(C.int32_t(display)) return float64(s) } // Scaled get the screen scaled size -func Scaled(x int) int { - f := ScaleF() +func Scaled(x int, displayId ...int) int { + f := ScaleF(displayId...) return Scaled0(x, f) } @@ -271,7 +272,7 @@ func GetScreenRect(displayId ...int) Rect { int(rect.size.w), int(rect.size.h) if runtime.GOOS == "windows" { - f := ScaleF() + f := ScaleF(displayId...) x, y, w, h = Scaled0(x, f), Scaled0(y, f), Scaled0(w, f), Scaled0(h, f) } return Rect{ @@ -281,9 +282,9 @@ func GetScreenRect(displayId ...int) Rect { } // GetScaleSize get the screen scale size -func GetScaleSize() (int, int) { +func GetScaleSize(displayId ...int) (int, int) { x, y := GetScreenSize() - f := ScaleF() + f := ScaleF(displayId...) return int(float64(x) * f), int(float64(y) * f) } diff --git a/robotgo_mac_unix.go b/robotgo_mac_unix.go index 365914b..72ad4ec 100644 --- a/robotgo_mac_unix.go +++ b/robotgo_mac_unix.go @@ -14,8 +14,8 @@ package robotgo // ScaleF get the system scale val -func ScaleF() float64 { - f := SysScale() +func ScaleF(displayId ...int) float64 { + f := SysScale(displayId...) if f == 0.0 { f = 1.0 } diff --git a/robotgo_win.go b/robotgo_win.go index 3826864..6d06213 100644 --- a/robotgo_win.go +++ b/robotgo_win.go @@ -53,8 +53,13 @@ func SetFocus(hwnd win.HWND) win.HWND { } // ScaleF get the system scale val -func ScaleF() float64 { - f := float64(GetMainDPI()) / 96.0 +func ScaleF(displayId ...int) (f float64) { + if len(displayId) > 0 && displayId[0] != -1 { + dpi := GetDPI(win.HWND(displayId[0])) + f = float64(dpi) / 96.0 + } else { + f = float64(GetMainDPI()) / 96.0 + } if f == 0.0 { f = 1.0 } diff --git a/window/alert_c.h b/window/alert_c.h index b6761c4..12f7143 100644 --- a/window/alert_c.h +++ b/window/alert_c.h @@ -37,8 +37,8 @@ static int xmessage(char *argv[], int *exit_status); kCFStringEncodingUTF8)) #endif -int showAlert(const char *title, const char *msg, const char *defaultButton, - const char *cancelButton) +int showAlert(const char *title, const char *msg, + const char *defaultButton, const char *cancelButton) { #if defined(IS_MACOSX) CFStringRef alertHeader = CFStringCreateWithUTF8String(title); @@ -46,17 +46,9 @@ int showAlert(const char *title, const char *msg, const char *defaultButton, CFStringRef defaultButtonTitle = CFStringCreateWithUTF8String(defaultButton); CFStringRef cancelButtonTitle = CFStringCreateWithUTF8String(cancelButton); CFOptionFlags responseFlags; - SInt32 err = CFUserNotificationDisplayAlert(0.0, - kCFUserNotificationNoteAlertLevel, - NULL, - NULL, - NULL, - alertHeader, - alertMessage, - defaultButtonTitle, - cancelButtonTitle, - NULL, - &responseFlags); + SInt32 err = CFUserNotificationDisplayAlert( + 0.0, kCFUserNotificationNoteAlertLevel, NULL, NULL, NULL, alertHeader, alertMessage, + defaultButtonTitle, cancelButtonTitle, NULL, &responseFlags); if (alertHeader != NULL) CFRelease(alertHeader); if (alertMessage != NULL) CFRelease(alertMessage); diff --git a/window/win_sys.h b/window/win_sys.h index 0e1dd15..e6e2ddb 100644 --- a/window/win_sys.h +++ b/window/win_sys.h @@ -17,25 +17,13 @@ Bounds get_client(uintptr pid, uintptr isHwnd); -intptr scaleX(){ - #if defined(IS_MACOSX) - return 0; - #elif defined(USE_X11) - return 0; - #elif defined(IS_WINDOWS) - // Get desktop dc - HDC desktopDc = GetDC(NULL); - // Get native resolution - intptr horizontalDPI = GetDeviceCaps(desktopDc, LOGPIXELSX); - // intptr verticalDPI = GetDeviceCaps(desktopDc, LOGPIXELSY); - return horizontalDPI; - #endif -} - -double sys_scale() { +double sys_scale(int32_t display_id) { #if defined(IS_MACOSX) - CGDirectDisplayID displayID = CGMainDisplayID(); + CGDirectDisplayID displayID = (CGDirectDisplayID) display_id; + if (displayID == -1) { + displayID = CGMainDisplayID(); + } CGDisplayModeRef modeRef = CGDisplayCopyDisplayMode(displayID); double pixelWidth = CGDisplayModeGetPixelWidth(modeRef); @@ -44,12 +32,11 @@ double sys_scale() { return pixelWidth / targetWidth; #elif defined(USE_X11) double xres; - Display *dpy; char *displayname = NULL; + Display *dpy = XOpenDisplay(displayname); + int scr = 0; /* Screen number */ - - dpy = XOpenDisplay(displayname); xres = ((((double) DisplayWidth(dpy, scr)) * 25.4) / ((double) DisplayWidthMM(dpy, scr))); @@ -79,6 +66,21 @@ double sys_scale() { #endif } +intptr scaleX(){ + #if defined(IS_MACOSX) + return 0; + #elif defined(USE_X11) + return 0; + #elif defined(IS_WINDOWS) + // Get desktop dc + HDC desktopDc = GetDC(NULL); + // Get native resolution + intptr horizontalDPI = GetDeviceCaps(desktopDc, LOGPIXELSX); + // intptr verticalDPI = GetDeviceCaps(desktopDc, LOGPIXELSY); + return horizontalDPI; + #endif +} + intptr scaleY(){ #if defined(IS_MACOSX) return 0;