From 74c3f6e08e32c6dfbfea7312d13552d81a9af6a9 Mon Sep 17 00:00:00 2001 From: vcaesar Date: Fri, 4 Feb 2022 00:20:23 -0800 Subject: [PATCH] Add GetMainId and IsMain function support --- robotgo.go | 5 +++++ robotgo_mac.go | 24 ++++++++++++++++++++++++ robotgo_win.go | 14 ++++++++++++-- robotgo_x11.go | 19 +++++++++++++++++++ 4 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 robotgo_mac.go diff --git a/robotgo.go b/robotgo.go index b4b8f6f..78937df 100644 --- a/robotgo.go +++ b/robotgo.go @@ -215,6 +215,11 @@ func GetMouseColor(displayId ...int) string { return GetPixelColor(x, y, displayId...) } +// IsMain is main display +func IsMain(displayId int) bool { + return displayId == GetMainId() +} + func displayIdx(id ...int) int { display := -1 if DisplayID != -1 { diff --git a/robotgo_mac.go b/robotgo_mac.go new file mode 100644 index 0000000..0f9ce4a --- /dev/null +++ b/robotgo_mac.go @@ -0,0 +1,24 @@ +// Copyright 2016 The go-vgo Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// https://github.com/go-vgo/robotgo/blob/master/LICENSE +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +//go:build darwin +// +build darwin + +package robotgo + +/* +#include +*/ +import "C" + +// GetMianId get the main display id +func GetMainId() int { + return int(C.CGMainDisplayID()) +} diff --git a/robotgo_win.go b/robotgo_win.go index 6d06213..bf6f5b8 100644 --- a/robotgo_win.go +++ b/robotgo_win.go @@ -42,8 +42,8 @@ func SendMsg(hwnd win.HWND, msg uint32, wParam, lParam uintptr) uintptr { return win.SendMessage(hwnd, msg, wParam, lParam) } -// SetActiveWindow set window active with hwnd -func SetActiveWindow(hwnd win.HWND) win.HWND { +// SetActive set window active with hwnd +func SetActive(hwnd win.HWND) win.HWND { return win.SetActiveWindow(hwnd) } @@ -52,6 +52,16 @@ func SetFocus(hwnd win.HWND) win.HWND { return win.SetFocus(hwnd) } +// GetMian get the main display hwnd +func GetMain() win.HWND { + return win.GetActiveWindow() +} + +// GetMianId get the main display id +func GetMainId() int { + return int(GetMain()) +} + // ScaleF get the system scale val func ScaleF(displayId ...int) (f float64) { if len(displayId) > 0 && displayId[0] != -1 { diff --git a/robotgo_x11.go b/robotgo_x11.go index 1bd0018..d6d2e6b 100644 --- a/robotgo_x11.go +++ b/robotgo_x11.go @@ -191,6 +191,25 @@ func DisplaysNum() int { return int(reply.Number) } +// GetMianId get the main display id +func GetMainId() int { + conn, err := xgb.NewConn() + if err != nil { + return -1 + } + + setup := xproto.Setup(conn) + defaultScreen := setup.DefaultScreen(conn) + id := -1 + for i, screen := range setup.Roots { + if defaultScreen.Root == screen.Root { + id = i + break + } + } + return id +} + // Alert show a alert window // Displays alert with the attributes. // If cancel button is not given, only the default button is displayed