forked from ahmetb/RectangleWin
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconf.go
More file actions
212 lines (197 loc) · 5.17 KB
/
conf.go
File metadata and controls
212 lines (197 loc) · 5.17 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
_ "embed"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
import (
"github.com/davecgh/go-spew/spew"
"github.com/golobby/config/v3"
"github.com/golobby/config/v3/pkg/feeder"
"github.com/gonutz/w32/v2"
)
type KeyBinding struct {
// A repeated value of key modifiers.
// Valid values include:
// SHIFT, ALT, CTRL, WIN (SUPER/META).
Modifier []string `yaml: "modifier"`
// When this is set, this overrides Modifier
ModifierCode []int32
// Calculated bitwise OR result of modifiers
CombinedMod int32
// Valid values are:
// A - Z, 0 - 9, UP_ARROW, =, -
// Anything not covered here could be set directly via KeyCode
Key string `yaml: "key"`
// Automatically converted from Key.
// When this is set, it overrides Key.
KeyCode int32 `yaml: "key_code"`
// The feature in RectangleWin to bind to.
// Valid values:
// moveToTop
// moveToBottom
// moveToLeft
// moveToRight
// moveToTopLeft
// moveToTopRight
// moveToBottomLeft
// moveToBottomRight
// makeSmaller
// makeLarger
// makeFullHeight
//
BindFeature string `yaml: "bindfeature"`
}
type Configuration struct {
Keybindings []KeyBinding `yaml: "key_binding"`
}
// This mini config is returned if we can't load a valid file
// and cannot write the detailed example yaml config.example.yaml
// into the expected path at %HOME%
var DEFAULT_CONF = Configuration{
Keybindings: []KeyBinding{
{
Modifier: []string{"Ctrl", "Alt"},
Key: "UP_ARROW",
KeyCode: 0x26,
BindFeature: "moveToTop",
},
},
}
//go:embed config.example.yaml
var configExampleYaml []byte
// Expected config path at %HOME%/.config/RectangleWin/config.yaml
var DEFAULT_CONF_PATH_PREFIX = ".config/RectangleWin/"
var DEFAULT_CONF_NAME = "config.yaml"
func convertModifier(keyName string) (int32, error) {
switch strings.ToLower(keyName) {
case "ctrl":
return MOD_CONTROL, nil
case "alt":
return MOD_ALT, nil
case "shift":
return MOD_SHIFT, nil
case "win", "meta", "super":
return MOD_WIN, nil
default:
return 0, errors.New("invalid keyname")
}
return 0, errors.New("unreachable")
}
func convertKeyCode(key string) (int32, error) {
k := strings.ToLower(key)
if len(k) == 1 {
if k[0] >= 'a' && k[0] <= 'z' {
return int32(k[0]) - 32, nil
}
if k[0] >= '0' && k[0] <= '9' {
return int32(k[0]), nil
}
}
switch k {
case "up_arrow":
return w32.VK_UP, nil
case "down_arrow":
return w32.VK_DOWN, nil
case "left_arrow":
return w32.VK_LEFT, nil
case "right_arrow":
return w32.VK_RIGHT, nil
case "-":
return 189, nil
case "=":
return 187, nil
}
for id, v := range keyNames {
lv := strings.ToLower(v)
if lv == k || lv == (k+" key") {
return int32(id), nil
}
}
return 0, errors.New("Unknown key")
}
func bitwiseOr(nums []int32) int32 {
if len(nums) == 0 {
return 0
}
result := nums[0]
for _, n := range nums[1:] {
result |= n // bitwise OR
}
return result
}
func getValidConfigPathOrCreate() string {
homeDir := os.Getenv("HOME")
if homeDir == "" {
homeDir = os.Getenv("USERPROFILE")
}
if homeDir == "" {
// Give up generating a valid path.
// read or write the conf in current folder.
return DEFAULT_CONF_NAME
}
configDir := filepath.Join(homeDir, DEFAULT_CONF_PATH_PREFIX)
err := os.MkdirAll(configDir, 0755)
if err != nil {
fmt.Printf("Error creating directory under user's home folder: %s", err)
// read or write the conf in current folder
return DEFAULT_CONF_NAME
}
configPath := filepath.Join(configDir, DEFAULT_CONF_NAME)
return configPath
}
func maybeDropExampleConfigFile(target string) {
// Check if the file exists, if not, create it with some content
if _, err := os.Stat(target); os.IsNotExist(err) {
// Create the file and write the sample content
err := ioutil.WriteFile(target, configExampleYaml, 0644)
if err != nil {
fmt.Println("Failed to create file created: %s %v", target, err)
}
fmt.Println("File created: %s", target)
}
}
func fetchConfiguration() Configuration {
spew.Dump(DEFAULT_CONF)
// Create a Configuration file.
myConfig := Configuration{}
// Yaml feeder
configFilePath := getValidConfigPathOrCreate()
maybeDropExampleConfigFile(configFilePath)
yamlFeeder := feeder.Yaml{Path: configFilePath}
c := config.New()
c.AddFeeder(yamlFeeder)
c.AddStruct(&myConfig)
err := c.Feed()
if err != nil {
fmt.Printf("warn: invalid config files found: %s %v\n", configFilePath, err)
return DEFAULT_CONF
}
for i := range myConfig.Keybindings {
if len(myConfig.Keybindings[i].ModifierCode) == 0 {
for _, mod := range myConfig.Keybindings[i].Modifier {
if modCode, err := convertModifier(mod); err == nil {
myConfig.Keybindings[i].ModifierCode = append(myConfig.Keybindings[i].ModifierCode, modCode)
} else {
fmt.Printf("warn: invalid key name %s", mod)
continue
}
}
}
myConfig.Keybindings[i].CombinedMod = bitwiseOr(myConfig.Keybindings[i].ModifierCode)
if myConfig.Keybindings[i].KeyCode == 0 {
if key, err := convertKeyCode(myConfig.Keybindings[i].Key); err == nil {
myConfig.Keybindings[i].KeyCode = key
} else {
fmt.Printf("warn: invalid key string %s", myConfig.Keybindings[i].Key)
continue
}
}
}
spew.Dump(myConfig)
return myConfig
}