Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Merrit/nyrna into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Merrit committed Oct 11, 2020
2 parents c7922b5 + a8ab27c commit 6cfe3d2
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 40 deletions.
4 changes: 3 additions & 1 deletion helpers.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package main

import (
"fmt"
"log"
)

// Check is a helper to make checking for
// simple errors cleaner and easier.
func Check(e error) {
if e != nil {
log.Println(e)
errorText := fmt.Sprintf("Caught an error: %v", e)
log.Println(errorText)
}
}
46 changes: 26 additions & 20 deletions suspend.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,33 @@ func ToggleSuspend() {
case err != nil:
log.Println("No saved process details found")
name, pid := findProcess()
process, err := process.NewProcess(pid)
Check(err)
status, err := process.Status()
Check(err)
if status == "T" {
status = "Suspended"
if pid == 0 {
log.Println("Error! Received PID 0.")
Notify("Error! Received PID 0.")
} else {
status = "Running"
}
log.Println("Checking process - name:", name, "PID:", pid, "status:", status)
switch status {
case "Running":
log.Println("Suspending", name)
NotifySuspend(name)
process.Suspend()
case "Suspended":
log.Println("Resuming", name)
NotifyResume(name)
process.Resume()
process, err := process.NewProcess(pid)
Check(err)
status, err := process.Status()
Check(err)
if status == "T" {
status = "Suspended"
} else {
status = "Running"
}
log.Println("Checking process - name:", name, "PID:", pid, "status:", status)
switch status {
case "Running":
log.Println("Suspending", name)
NotifySuspend(name)
process.Suspend()
case "Suspended":
log.Println("Resuming", name)
NotifyResume(name)
process.Resume()
}
// Save suspended process details to file
SaveProcessFile(name, pid)
}
// Save suspended process details to file
SaveProcessFile(name, pid)

}
}
53 changes: 34 additions & 19 deletions suspendlinux.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,45 +53,60 @@ func GetActiveWindowLinux() (string, int32) {
}

func findWineProcess() (string, int32) {
// Use pgrep to search for running *.exe processes
// Use ps to search for running *.exe processes
log.Print("Searching for Wine process..")
pgrepOut, err := exec.Command("pgrep", ".exe$", "-l").Output()
cmd := "ps aux | grep .exe$"
psOutput, err := exec.Command("bash", "-c", cmd).Output()
Check(err)
// Convert the pgrep output to a map
pgrepString := string(pgrepOut[:])
pgrepSlice := strings.Fields(pgrepString)
pgrepMap := make(map[string]string)
for i := 0; i < len(pgrepSlice); i += 2 {
pgrepMap[pgrepSlice[i+1]] = pgrepSlice[i]
// Convert the output to a string so we can search and manipulate.
psString := string(psOutput[:])
// Each line of the output is for a single process, so seperate them into
// a slice, one for each process found.
var rawProcesses []string = strings.Split(psString, "\n")
// Create a map of the found processes in the form of name:PID.
psMap := make(map[string]string)
for _, process := range rawProcesses {
if process != "" {
// Split the string by white space.
// 0th entry should be the user, which we don't care about.
// 1st entry should be the PID.
var parts []string = strings.Fields(process)
var PID string = parts[1]
// log.Printf("PID: %v", PID)
findName := strings.Split(process, "\\")
var processName string = findName[len(findName)-1]
// log.Printf("processName: %v", processName)
psMap[processName] = PID
}
}
// Print the results for debugging purposes
for name, pid := range pgrepMap {
log.Println("Name:", name, "=>", "PID:", pid)
for name, pid := range psMap {
log.Println("\n", "Name:", name, "\n", "PID:", pid, "\n ")
}
// Remove the .exe processes belonging to Wine
for name := range pgrepMap {
for name := range psMap {
switch name {
case "services.exe":
delete(pgrepMap, name)
delete(psMap, name)
fallthrough
case "explorer.exe":
delete(pgrepMap, name)
delete(psMap, name)
fallthrough
case "winedevice.exe":
delete(pgrepMap, name)
delete(psMap, name)
fallthrough
case "plugplay.exe":
delete(pgrepMap, name)
delete(psMap, name)
}
}
if len(pgrepMap) != 1 {
log.Println("Multiple remaining processes:", pgrepMap)
log.Fatal("Not able to find real wine process! Please report this issue.")
if len(psMap) != 1 {
log.Println("Multiple remaining processes:", psMap)
log.Println("Not able to find real wine process! Please report this issue.")
}
// Extract name and pid from the map
var processName string
var processIDint int
for name, pid := range pgrepMap {
for name, pid := range psMap {
processName = name
processIDint, err = strconv.Atoi(pid)
Check(err)
Expand Down

0 comments on commit 6cfe3d2

Please sign in to comment.