|
|
@@ -2,8 +2,6 @@ package main
|
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
- "os"
|
|
|
- "path/filepath"
|
|
|
|
|
|
intdevice "netdome.biz/paul/qmk-rgb/internal/device"
|
|
|
intrgb "netdome.biz/paul/qmk-rgb/internal/rgb"
|
|
|
@@ -54,11 +52,9 @@ func resolveCatalogFor(vendorID, productID uint16) (*intrgb.Catalog, string, err
|
|
|
return def.Catalog, def.Path, nil
|
|
|
}
|
|
|
|
|
|
- if dir, err := definitionsDir(); err == nil {
|
|
|
- if defs, loadErr := intrgb.LoadDefinitionsDir(dir); loadErr == nil {
|
|
|
- if def := intrgb.FindDefinition(defs, vendorID, productID); def != nil {
|
|
|
- return def.Catalog, def.Path, nil
|
|
|
- }
|
|
|
+ if defs, err := intrgb.LoadDefinitionsDir(definitionsPath()); err == nil {
|
|
|
+ if def := intrgb.FindDefinition(defs, vendorID, productID); def != nil {
|
|
|
+ return def.Catalog, def.Path, nil
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -67,31 +63,6 @@ func resolveCatalogFor(vendorID, productID uint16) (*intrgb.Catalog, string, err
|
|
|
return nil, "", nil
|
|
|
}
|
|
|
|
|
|
-// lookupPaths returns the directories a data file is looked for in: the one
|
|
|
-// holding the executable, then the working directory, which is the order the
|
|
|
-// profile directory uses too.
|
|
|
-func lookupPaths() []string {
|
|
|
- var paths []string
|
|
|
- if exe, err := os.Executable(); err == nil {
|
|
|
- paths = append(paths, filepath.Dir(exe))
|
|
|
- }
|
|
|
- if cwd, err := os.Getwd(); err == nil {
|
|
|
- paths = append(paths, cwd)
|
|
|
- }
|
|
|
- return paths
|
|
|
-}
|
|
|
-
|
|
|
-// definitionLabels returns the channel names a definition gives the board, which
|
|
|
-// are the names VIA shows. They are the only channel names there are: a board
|
|
|
-// without a definition is addressed by its QMK subsystem name.
|
|
|
-func definitionLabels(vendorID, productID uint16) map[uint16]string {
|
|
|
- def := loadedDefinition(vendorID, productID)
|
|
|
- if def == nil {
|
|
|
- return nil
|
|
|
- }
|
|
|
- return def.Labels
|
|
|
-}
|
|
|
-
|
|
|
// loadedDefinition returns the definition file for a board, from the data
|
|
|
// directory or from the file --definition names, and nil when there is none.
|
|
|
func loadedDefinition(vendorID, productID uint16) *intrgb.Definition {
|
|
|
@@ -101,22 +72,30 @@ func loadedDefinition(vendorID, productID uint16) *intrgb.Definition {
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
- dir, err := definitionsDir()
|
|
|
+ defs, err := intrgb.LoadDefinitionsDir(definitionsPath())
|
|
|
if err != nil {
|
|
|
return nil
|
|
|
}
|
|
|
- defs, err := intrgb.LoadDefinitionsDir(dir)
|
|
|
- if err != nil {
|
|
|
+ return intrgb.FindDefinition(defs, vendorID, productID)
|
|
|
+}
|
|
|
+
|
|
|
+// definitionLabels returns the channel names a definition gives the board, which
|
|
|
+// are the names VIA shows. They are the only channel names there are: a board
|
|
|
+// without a definition is addressed by its QMK subsystem name.
|
|
|
+func definitionLabels(vendorID, productID uint16) map[uint16]string {
|
|
|
+ def := loadedDefinition(vendorID, productID)
|
|
|
+ if def == nil {
|
|
|
return nil
|
|
|
}
|
|
|
- return intrgb.FindDefinition(defs, vendorID, productID)
|
|
|
+ return def.Labels
|
|
|
}
|
|
|
|
|
|
// applyDefinitionLabels returns the display names a board answers to and, beside
|
|
|
// them, the alternatives each channel keeps. The definition's label is the name
|
|
|
// the board is called in VIA; the QMK subsystem name stays an accepted
|
|
|
// alternative, because it follows from the channel number and is the one a
|
|
|
-// document can promise without knowing the board.
|
|
|
+// document can promise without knowing a board.
|
|
|
+//
|
|
|
// Only a channel the definition names gets a display name, and that is
|
|
|
// deliberate. Adding an entry for every QMK lighting channel would put
|
|
|
// "backlight" on channel 1 of a board that has none, where the same word is also
|
|
|
@@ -148,31 +127,12 @@ func boardName(dev intdevice.Device, _ map[uint16]string) string {
|
|
|
return "unknown model"
|
|
|
}
|
|
|
|
|
|
-// definitionsDir returns the data directory, looked for next to the executable
|
|
|
-// first and then in the working directory.
|
|
|
-var definitionsDir = func() (string, error) {
|
|
|
- paths := lookupPaths()
|
|
|
- for _, p := range paths {
|
|
|
- dir := filepath.Join(p, intrgb.DefinitionsDir)
|
|
|
- if info, err := os.Stat(dir); err == nil && info.IsDir() {
|
|
|
- return dir, nil
|
|
|
- }
|
|
|
- }
|
|
|
- if len(paths) == 0 {
|
|
|
- return intrgb.DefinitionsDir, nil
|
|
|
- }
|
|
|
- return filepath.Join(paths[0], intrgb.DefinitionsDir), nil
|
|
|
-}
|
|
|
-
|
|
|
// ensureDefinitionsDir returns the data directory, creating it if it is not
|
|
|
// there yet, so a fetch has somewhere to write to.
|
|
|
func ensureDefinitionsDir() (string, error) {
|
|
|
- dir, err := definitionsDir()
|
|
|
+ dir, err := ensureDataDir(definitionsPath())
|
|
|
if err != nil {
|
|
|
- return "", err
|
|
|
- }
|
|
|
- if err := os.MkdirAll(dir, 0o755); err != nil {
|
|
|
- return "", fmt.Errorf("create %s: %w", dir, err)
|
|
|
+ return "", fmt.Errorf("create %s: %w", definitionsPath(), err)
|
|
|
}
|
|
|
return dir, nil
|
|
|
}
|