-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathswaynag.go
73 lines (59 loc) · 1.42 KB
/
swaynag.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"os"
"os/exec"
"strings"
ps "github.com/mitchellh/go-ps"
)
type Message struct {
PID int
Display string
}
func show(text string, display string) (*Message, error) {
cmd := exec.Command("swaynag", "--message", text, "--output", display, "--layer", "overlay")
err := cmd.Start()
if err != nil {
logError("Unable to show swaynag in display '%s'.\n", display)
return nil, err
}
go func() {
cmd.Wait()
}()
return &Message{PID: cmd.Process.Pid, Display: display}, nil
}
func isSwaynag(process ps.Process, err error) bool {
return err == nil &&
process != nil &&
strings.HasSuffix(process.Executable(), "swaynag")
}
func ShowMessage(text string, message Message) (*Message, error) {
if isSwaynag(ps.FindProcess(message.PID)) {
return &message, nil
}
return show(text, message.Display)
}
func CloseMessage(message Message) {
process, err := os.FindProcess(message.PID)
if err != nil {
return
}
defer process.Release()
process.Signal(os.Interrupt)
}
func ShowAll(text string, messages []Message) []Message {
var openMessages []Message
for _, message := range messages {
newMessage, _ := ShowMessage(text, message)
if newMessage == nil {
openMessages = append(openMessages, message)
} else {
openMessages = append(openMessages, *newMessage)
}
}
return openMessages
}
func CloseAll(messages []Message) {
for _, message := range messages {
CloseMessage(message)
}
}