forked from jmakip/niuwm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
100 lines (89 loc) · 2.03 KB
/
config.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
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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)
//Hard coded config paths
const keyConfigPath string = "/.config/niuwm/keybinds.json"
//CmdKeybind keybinds for executables
type CmdKeybind struct {
Cmd string
Cmdparams string
Mod uint16
Keycode byte
}
//LogoutKeybind keycode for exiting WM
type LogoutKeybind struct {
Mod uint16
Keycode byte
}
//ActionKeybind WM actions keybinds
type ActionKeybind struct {
Action string
Mod uint16
Keycode byte
}
//CmdKeys all keybinds
type CmdKeys struct {
Actions []ActionKeybind
Cmd []CmdKeybind
}
//NiuCfg settings load from config files
type NiuCfg struct {
Keybinds CmdKeys
//mouse bindings
//workspaces etc...
}
//InitConfig initialize settings by loading from file
func InitConfig() (cfg NiuCfg) {
var err error
cfg.Keybinds, err = LoadKeyBindings()
if err != nil {
//trying to generate some defaults
cfg.Keybinds = GenKeyBinds()
}
return cfg
}
//LoadKeyBindings load keybindings from JSON config files
func LoadKeyBindings() (ret CmdKeys, err error) {
keyFile, err := os.Open(os.Getenv("HOME") + keyConfigPath)
if err != nil {
fmt.Printf("Could not open config file: %s \n", keyConfigPath)
return
}
defer keyFile.Close()
ascii, err := ioutil.ReadAll(keyFile)
if err != nil {
fmt.Printf("Cant readl config file: %s \n", keyConfigPath)
}
err = json.Unmarshal(ascii, &ret)
if err != nil {
//file exists but cant translate it perhaps version mismatch
fmt.Printf("Cant Unmarshal config file: %s \n", keyConfigPath)
}
return ret, err
}
//GenKeyBinds generate default config for keybinds, use when file does not exists.
func GenKeyBinds() (keys CmdKeys) {
keys = CmdKeys{
Actions: []ActionKeybind{
ActionKeybind{
Action: "logout",
Mod: 4,
Keycode: 9,
},
{
Action: "unknown",
Mod: 0xff,
Keycode: 69,
},
},
Cmd: []CmdKeybind{
CmdKeybind{Cmd: "termite", Cmdparams: "", Mod: 4, Keycode: 36},
CmdKeybind{Cmd: "rofi", Cmdparams: "-show drun", Mod: 4, Keycode: 40},
},
}
return keys
}