Skip to content

Commit 52ab692

Browse files
committed
Add some hopefully helpful comments
1 parent 874a1bd commit 52ab692

File tree

4 files changed

+42
-6
lines changed

4 files changed

+42
-6
lines changed

client/client.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"github.com/alex11br/gxhk/common"
88
)
99

10+
// Just SendCommand, let the daemon do the heavy work, and get through the socket
11+
// the readily formated response that you'll simply return.
1012
func SendCommand(args common.Args) (res common.Response) {
1113
conn, err := net.Dial("unix", args.SocketPath)
1214
if err != nil {

common/response.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package common
22

3+
// The Response stores the response which gets displayed.
4+
// WARNING: Make sure that yout Message doesn't end in a newline!!!
5+
// That's beacuse the client will display a newline after the Message.
36
type Response struct {
47
Status int
58
Message string

daemon/commands.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ import (
77
"github.com/alex11br/gxhk/common"
88
)
99

10+
// Exec executes the given sh command string. This method is blocking.
1011
func Exec(command string) {
1112
exec.Command("sh", "-c", command).Run()
1213
}
1314

15+
// MakeDescription handles the logic behind generating a dummy description for the commands
16+
// which don't come with a description (i.e. it is empty) from the command string,
17+
// or returning the description if it isn't empty
1418
func MakeDescription(description string, command string) string {
1519
if description != "" {
1620
return description
@@ -19,19 +23,25 @@ func MakeDescription(description string, command string) string {
1923
}
2024
}
2125

26+
// AddEventInfo handles the logic behind creating a new line and filling it
27+
// with the infos necessary for the 'info' command for each bound hotkey
2228
func AddEventInfo(infos *string, hotkey string, eventName string, description string) {
2329
if *infos != "" {
2430
*infos += "\n"
2531
}
2632
*infos += fmt.Sprintf("On %s %s: %s", hotkey, eventName, description)
2733
}
2834

35+
// Bind does the heavy work of binding a hotkey (plus its eventual sisters) to a command.
2936
func Bind(bindArgs common.BindCmd) error {
30-
hotkeys, err := HotkeyFromStr(bindArgs.Hotkey)
37+
// First, we find the Hotkey's that correspond to the given hotkey string.
38+
hotkeys, err := HotkeysFromStr(bindArgs.Hotkey)
3139
if err != nil {
3240
return err
3341
}
3442

43+
// Then, grab the matching keys which haven't been grabbed yet (i.e. those without commands).
44+
// If any of these new keys fail to grab, we ungrab what we got to grab before and leave.
3545
newlyBound := make([]Hotkey, 0)
3646
for _, hotkey := range hotkeys {
3747
if KeyPressCommands.IsEmpty(hotkey) && KeyReleaseCommands.IsEmpty(hotkey) {
@@ -47,6 +57,13 @@ func Bind(bindArgs common.BindCmd) error {
4757
}
4858
}
4959

60+
// Now we'll set all the appropiate values in the necessary places
61+
// to successfully bind the hotkeys.
62+
// It is worth noting that if there are multiple hotkeys for the same string,
63+
// in the 'info' command only one of them should provide descriptions. As such:
64+
// CONVENTION: Empty description of a Hotkey (description == "") MEANS ignore that Hotkey!!!
65+
// We'll put a non-empty description for the first matching Hotkey (refer to MakeDescription)
66+
// and for any eventual ones an empty description
5067
for i, hotkey := range hotkeys {
5168
var description string
5269
if i == 0 {
@@ -67,8 +84,10 @@ func Bind(bindArgs common.BindCmd) error {
6784
return nil
6885
}
6986

87+
// Unbind does the not-so-heavy work of unbinding a hotkey (plus its eventual sisters),
88+
// which consists in ungrabbing the keys, and deleting the keys in the HotkeyMap's
7089
func Unbind(unbindArgs common.UnbindCmd) error {
71-
hotkeys, err := HotkeyFromStr(unbindArgs.Hotkey)
90+
hotkeys, err := HotkeysFromStr(unbindArgs.Hotkey)
7291
if err != nil {
7392
return err
7493
}
@@ -90,8 +109,13 @@ func Unbind(unbindArgs common.UnbindCmd) error {
90109
return nil
91110
}
92111

112+
// GetInfo returns the information about what is bound to the given hotkeyStr.
113+
// It returns a Response structure to easily accomodate the need to mention
114+
// parsing errors if the hotkeyStr is "naughty".
115+
// The first Hotkey to be parsed should be the main one,
116+
// which represents our needed description in the description maps.
93117
func GetInfo(hotkeyStr string) (res common.Response) {
94-
hotkeys, err := HotkeyFromStr(hotkeyStr)
118+
hotkeys, err := HotkeysFromStr(hotkeyStr)
95119
if err != nil {
96120
return common.Response{
97121
Status: 1,
@@ -113,6 +137,8 @@ func GetInfo(hotkeyStr string) (res common.Response) {
113137
return
114138
}
115139

140+
// GetAllInfo creates a string with all the bound hotkeys there are,
141+
// both those bound on press and those bound on release.
116142
func GetAllInfo() (info string) {
117143
KeyPressDescriptions.ForEach(func(hotkey Hotkey, description string) {
118144
if description != "" {

daemon/hotkeys.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ type Hotkey struct {
1515
key xproto.Keycode
1616
}
1717

18-
var NilHotkey Hotkey = Hotkey{0, 0}
19-
20-
func HotkeyFromStr(str string) (hotkeys []Hotkey, err error) {
18+
// HotkeysFromStr parses a hotkey string and returns the according Hotkey's.
19+
// Mods are unambiguous (though we ignore here the Num and Caps Lock).
20+
// Keycodes aren't, though. For instance, on some layouts, there are 2 keys with different keycodes
21+
// which map to 'backslash' AKA '\'. And we want to handle them both.
22+
// This is why we return an array of Hotkey's here. And an error if the string is "naughty".
23+
func HotkeysFromStr(str string) (hotkeys []Hotkey, err error) {
2124
mods, keys, err := keybind.ParseString(X, str)
2225
if err != nil {
2326
return nil, err
@@ -33,6 +36,8 @@ func HotkeyFromStr(str string) (hotkeys []Hotkey, err error) {
3336
return
3437
}
3538

39+
// ToStr converts the Hotkey into its string representation.
40+
// This helps us show in a human-firendly way the bound hotkeys in the 'info' command.
3641
func (h Hotkey) ToStr() (str string) {
3742
str = keybind.ModifierString(h.mods)
3843
if str != "" {

0 commit comments

Comments
 (0)