-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
remove support for go1.3 #7
Merged
inconshreveable
merged 3 commits into
inconshreveable:master
from
thaJeztah:remove_go1.4
Nov 27, 2022
+17
−118
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
//go:build !windows | ||
// +build !windows | ||
|
||
package mousetrap | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,98 +1,42 @@ | ||
// +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 | ||
func getProcessEntry(pid int) (*syscall.ProcessEntry32, error) { | ||
snapshot, err := syscall.CreateToolhelp32Snapshot(syscall.TH32CS_SNAPPROCESS, 0) | ||
if err != nil { | ||
return nil, err | ||
} | ||
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 | ||
defer syscall.CloseHandle(snapshot) | ||
var procEntry syscall.ProcessEntry32 | ||
procEntry.Size = uint32(unsafe.Sizeof(procEntry)) | ||
if err = syscall.Process32First(snapshot, &procEntry); err != nil { | ||
return nil, err | ||
} | ||
|
||
for { | ||
if processEntry.th32ProcessID == uint32(pid) { | ||
pe = &processEntry | ||
return | ||
if procEntry.ProcessID == uint32(pid) { | ||
return &procEntry, nil | ||
} | ||
|
||
ok, _, e1 = Process32Next.Call(snapshot, uintptr(unsafe.Pointer(&processEntry))) | ||
if ok == 0 { | ||
err = fmt.Errorf("Process32Next: %v", e1) | ||
return | ||
err = syscall.Process32Next(snapshot, &procEntry) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
} | ||
|
||
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() | ||
pe, err := getProcessEntry(syscall.Getppid()) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
pe, err := getProcessEntry(ppid) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
name := syscall.UTF16ToString(pe.szExeFile[:]) | ||
return name == "explorer.exe" | ||
return "explorer.exe" == syscall.UTF16ToString(pe.ExeFile[:]) | ||
} |
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
FWIW, I'm considering a pull request in golang.org/x/sys to see if the maintainers there are open to exporting the equivalent function there. I'll let you know if they do (and open a PR here)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I opened golang/sys#143 (https://go-review.googlesource.com/c/sys/+/459039) to see if the maintainers are open to exporting it 👍