From a860bbcff9bd8b4f52ecb6d15cde5f7d12a782c8 Mon Sep 17 00:00:00 2001 From: vcaesar Date: Thu, 20 Jan 2022 13:03:35 -0400 Subject: [PATCH] add get DisplaysNum support --- robotgo.go | 4 ++++ robotgo_mac_win.go | 5 +++++ robotgo_x11.go | 22 ++++++++++++++++++++++ screen/goScreen.h | 27 +++++++++++++++++++++++++++ 4 files changed, 58 insertions(+) diff --git a/robotgo.go b/robotgo.go index 5613459..79eac1e 100644 --- a/robotgo.go +++ b/robotgo.go @@ -239,6 +239,10 @@ func displayIdx(id ...int) int { return display } +func getNumDisplays() int { + return int(C.get_num_displays()) +} + // SysScale get the sys scale func SysScale(displayId ...int) float64 { display := displayIdx(displayId...) diff --git a/robotgo_mac_win.go b/robotgo_mac_win.go index b0486c9..df183ea 100644 --- a/robotgo_mac_win.go +++ b/robotgo_mac_win.go @@ -50,3 +50,8 @@ func ActivePID(pid int32, args ...int) error { internalActive(pid, hwnd) return nil } + +// DisplaysNum get the count of displays +func DisplaysNum() int { + return getNumDisplays() +} diff --git a/robotgo_x11.go b/robotgo_x11.go index 009c9d4..87bdde6 100644 --- a/robotgo_x11.go +++ b/robotgo_x11.go @@ -17,6 +17,7 @@ import ( "errors" "log" + "github.com/robotn/xgb/xinerama" "github.com/robotn/xgb/xproto" "github.com/robotn/xgbutil" "github.com/robotn/xgbutil/ewmh" @@ -149,3 +150,24 @@ func GetXidFromPid(xu *xgbutil.XUtil, pid int32) (xproto.Window, error) { return 0, errors.New("failed to find a window with a matching pid.") } + +// DisplaysNum get the count of displays +func DisplaysNum() int { + c, err := xgb.NewConn() + if err != nil { + return 0 + } + defer c.Close() + + err = xinerama.Init(c) + if err != nil { + return 0 + } + + reply, err := xinerama.QueryScreens(c).Reply() + if err != nil { + return 0 + } + + return int(reply.Number) +} diff --git a/screen/goScreen.h b/screen/goScreen.h index ce6b117..ab6cf70 100644 --- a/screen/goScreen.h +++ b/screen/goScreen.h @@ -96,6 +96,33 @@ char* get_XDisplay_name(){ #endif } +#if defined(IS_WINDOWS) +bool CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) { + uint32_t *count = (uint32_t*)dwData; + (*count)++; + return true; +} +#endif + +uint32_t get_num_displays() { +#if defined(IS_MACOSX) + uint32_t count = 0; + if (CGGetActiveDisplayList(0, nil, &count) == kCGErrorSuccess) { + return count; + } + return 0; +#elif defined(USE_X11) + return 0; +#elif defined(IS_WINDOWS) + uint32_t count = 0; + if (EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, (LPARAM)&count)) { + return count; + } + return 0; +#endif +} + + void bitmap_dealloc(MMBitmapRef bitmap){ if (bitmap != NULL) { destroyMMBitmap(bitmap);