From 163230f9ba1a8041471f0137010f842b851a5ce2 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sun, 6 Nov 2022 01:22:50 +0100 Subject: [PATCH 1/3] remove support for go1.3 go1.4 was released 8 Years ago, so it's very unlikely anyone is still using go1.3. Signed-off-by: Sebastiaan van Stijn --- trap_windows.go | 98 ------------------------------------------------- 1 file changed, 98 deletions(-) delete mode 100644 trap_windows.go diff --git a/trap_windows.go b/trap_windows.go deleted file mode 100644 index 336142a..0000000 --- a/trap_windows.go +++ /dev/null @@ -1,98 +0,0 @@ -// +build windows -// +build !go1.4 - -package mousetrap - -import ( - "fmt" - "os" - "syscall" - "unsafe" -) - -const ( - // defined by the Win32 API - th32cs_snapprocess uintptr = 0x2 -) - -var ( - kernel = syscall.MustLoadDLL("kernel32.dll") - CreateToolhelp32Snapshot = kernel.MustFindProc("CreateToolhelp32Snapshot") - Process32First = kernel.MustFindProc("Process32FirstW") - Process32Next = kernel.MustFindProc("Process32NextW") -) - -// ProcessEntry32 structure defined by the Win32 API -type processEntry32 struct { - dwSize uint32 - cntUsage uint32 - th32ProcessID uint32 - th32DefaultHeapID int - th32ModuleID uint32 - cntThreads uint32 - th32ParentProcessID uint32 - pcPriClassBase int32 - dwFlags uint32 - szExeFile [syscall.MAX_PATH]uint16 -} - -func getProcessEntry(pid int) (pe *processEntry32, err error) { - snapshot, _, e1 := CreateToolhelp32Snapshot.Call(th32cs_snapprocess, uintptr(0)) - if snapshot == uintptr(syscall.InvalidHandle) { - err = fmt.Errorf("CreateToolhelp32Snapshot: %v", e1) - return - } - defer syscall.CloseHandle(syscall.Handle(snapshot)) - - var processEntry processEntry32 - processEntry.dwSize = uint32(unsafe.Sizeof(processEntry)) - ok, _, e1 := Process32First.Call(snapshot, uintptr(unsafe.Pointer(&processEntry))) - if ok == 0 { - err = fmt.Errorf("Process32First: %v", e1) - return - } - - for { - if processEntry.th32ProcessID == uint32(pid) { - pe = &processEntry - return - } - - ok, _, e1 = Process32Next.Call(snapshot, uintptr(unsafe.Pointer(&processEntry))) - if ok == 0 { - err = fmt.Errorf("Process32Next: %v", e1) - return - } - } -} - -func getppid() (pid int, err error) { - pe, err := getProcessEntry(os.Getpid()) - if err != nil { - return - } - - pid = int(pe.th32ParentProcessID) - return -} - -// StartedByExplorer returns true if the program was invoked by the user double-clicking -// on the executable from explorer.exe -// -// It is conservative and returns false if any of the internal calls fail. -// It does not guarantee that the program was run from a terminal. It only can tell you -// whether it was launched from explorer.exe -func StartedByExplorer() bool { - ppid, err := getppid() - if err != nil { - return false - } - - pe, err := getProcessEntry(ppid) - if err != nil { - return false - } - - name := syscall.UTF16ToString(pe.szExeFile[:]) - return name == "explorer.exe" -} From 109353ec74ac5408d2eec1f6f8b1880ad03803dc Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sun, 6 Nov 2022 01:25:25 +0100 Subject: [PATCH 2/3] rename files to regular name, and update buildtags This allows us to remove the buildtag comment. Also updated the other buildtags to the current go format. Doing the rename in a separate commit, to assist Git history to continue keeping track. Signed-off-by: Sebastiaan van Stijn --- trap_others.go | 1 + trap_windows_1.4.go => trap_windows.go | 3 --- 2 files changed, 1 insertion(+), 3 deletions(-) rename trap_windows_1.4.go => trap_windows.go (97%) diff --git a/trap_others.go b/trap_others.go index 9d2d8a4..06a91f0 100644 --- a/trap_others.go +++ b/trap_others.go @@ -1,3 +1,4 @@ +//go:build !windows // +build !windows package mousetrap diff --git a/trap_windows_1.4.go b/trap_windows.go similarity index 97% rename from trap_windows_1.4.go rename to trap_windows.go index 9a28e57..a9f5691 100644 --- a/trap_windows_1.4.go +++ b/trap_windows.go @@ -1,6 +1,3 @@ -// +build windows -// +build go1.4 - package mousetrap import ( From 15567a6a2a6c316885a999e716b00530ea287f96 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sun, 6 Nov 2022 01:41:49 +0100 Subject: [PATCH 3/3] remove "os" import os.Getppid() is an alias for syscall.Getppid(), which was already imported, so use it directly instead of the alias. Signed-off-by: Sebastiaan van Stijn --- trap_windows.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/trap_windows.go b/trap_windows.go index a9f5691..0c56880 100644 --- a/trap_windows.go +++ b/trap_windows.go @@ -1,7 +1,6 @@ package mousetrap import ( - "os" "syscall" "unsafe" ) @@ -35,7 +34,7 @@ func getProcessEntry(pid int) (*syscall.ProcessEntry32, error) { // It does not guarantee that the program was run from a terminal. It only can tell you // whether it was launched from explorer.exe func StartedByExplorer() bool { - pe, err := getProcessEntry(os.Getppid()) + pe, err := getProcessEntry(syscall.Getppid()) if err != nil { return false }