-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoll.go
66 lines (59 loc) · 1.39 KB
/
poll.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
// Copyright ©2024 Dan Kortschak. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"os"
"runtime"
"slices"
"strings"
"github.com/kortschak/dex/rpc"
)
func newDetailer(strategy string) (detailer, error) {
if strategy == "none" {
return noDetails{}, nil
}
if strategy == "" {
var err error
strategy, err = defaultStrategy()
if err != nil {
return noDetails{}, err
}
}
d, ok := detailers[strategy]
if !ok {
return noDetails{}, rpc.NewError(rpc.ErrCodeInvalidData,
fmt.Sprintf("no detailer strategy: %q", strategy),
map[string]any{
"type": rpc.ErrCodeBounds,
"strategy": strategy,
},
)
}
return d()
}
func defaultStrategy() (string, error) {
if runtime.GOOS == "darwin" {
return "macos", nil
}
switch typ := os.Getenv("XDG_SESSION_TYPE"); typ {
default:
return "", fmt.Errorf("unknown session type: %q", typ)
case "x11":
return "xorg", nil
case "wayland":
xdgCurrentDesktop := os.Getenv("XDG_CURRENT_DESKTOP")
switch de := strings.Split(xdgCurrentDesktop, ":"); {
case slices.Contains(de, "GNOME"):
return "gnome/mutter", nil
default:
return "", fmt.Errorf("unknown desktop environment: %q", xdgCurrentDesktop)
}
}
}
var detailers = map[string]func() (detailer, error){}
// warning is a warn-only error.
type warning struct {
error
}