catalog.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. intdevice "netdome.biz/paul/qmk-rgb/internal/device"
  7. intrgb "netdome.biz/paul/qmk-rgb/internal/rgb"
  8. "netdome.biz/paul/qmk-rgb/internal/via"
  9. )
  10. // definitionFlag names a definition file to use instead of looking one up.
  11. var definitionFlag string
  12. // resolveCatalog returns the effect catalog for a board, in the order the tool
  13. // trusts: the file named by --definition, then the definition in the data
  14. // directory that matches the board, then the catalog compiled in for a known
  15. // board. The second return value says which of the three it was, because a
  16. // board that has no names at all is a different situation from one whose names
  17. // came from somewhere the user can see.
  18. //
  19. // This is the one place a catalog is looked up. A command that reached for a
  20. // catalog itself would silently ignore a definition file the user had placed,
  21. // which is the whole point of having one.
  22. func resolveCatalog(target targetDeviceData) (*intrgb.Catalog, string, error) {
  23. if definitionFlag != "" {
  24. def, err := intrgb.LoadDefinition(definitionFlag)
  25. if err != nil {
  26. return nil, "", err
  27. }
  28. if !def.Matches(target.Device.VendorID, target.Device.ProductID) {
  29. return nil, "", fmt.Errorf("%s is a definition for %s (0x%04X/0x%04X), not for this keyboard (0x%04X/0x%04X)",
  30. definitionFlag, def.Name, def.VendorID, def.ProductID,
  31. target.Device.VendorID, target.Device.ProductID)
  32. }
  33. return def.Catalog, def.Path, nil
  34. }
  35. catalog, source, err := resolveCatalogFor(target.Device.VendorID, target.Device.ProductID)
  36. return catalog, source, err
  37. }
  38. // resolveCatalogFor is the lookup without a target, for the commands that report
  39. // on a board rather than open it.
  40. func resolveCatalogFor(vendorID, productID uint16) (*intrgb.Catalog, string, error) {
  41. if definitionFlag != "" {
  42. def, err := intrgb.LoadDefinition(definitionFlag)
  43. if err != nil {
  44. return nil, "", err
  45. }
  46. if !def.Matches(vendorID, productID) {
  47. return nil, "", nil
  48. }
  49. return def.Catalog, def.Path, nil
  50. }
  51. if dir, err := definitionsDir(); err == nil {
  52. if defs, loadErr := intrgb.LoadDefinitionsDir(dir); loadErr == nil {
  53. if def := intrgb.FindDefinition(defs, vendorID, productID); def != nil {
  54. return def.Catalog, def.Path, nil
  55. }
  56. }
  57. }
  58. catalog, ok := intrgb.CatalogFor(vendorID, productID)
  59. if !ok {
  60. return nil, "", nil
  61. }
  62. return catalog, fmt.Sprintf("built in (%s)", catalog.Name()), nil
  63. }
  64. // lookupPaths returns the directories a data file is looked for in: the one
  65. // holding the executable, then the working directory, which is the order the
  66. // profile directory uses too.
  67. func lookupPaths() []string {
  68. var paths []string
  69. if exe, err := os.Executable(); err == nil {
  70. paths = append(paths, filepath.Dir(exe))
  71. }
  72. if cwd, err := os.Getwd(); err == nil {
  73. paths = append(paths, cwd)
  74. }
  75. return paths
  76. }
  77. // definitionLabels returns the channel names a definition gives the board, which
  78. // are the names VIA shows. They are the only channel names there are: a board
  79. // without a definition is addressed by its QMK subsystem name.
  80. func definitionLabels(vendorID, productID uint16) map[uint16]string {
  81. def := loadedDefinition(vendorID, productID)
  82. if def == nil {
  83. return nil
  84. }
  85. return def.Labels
  86. }
  87. // loadedDefinition returns the definition file for a board, from the data
  88. // directory or from the file --definition names, and nil when there is none.
  89. func loadedDefinition(vendorID, productID uint16) *intrgb.Definition {
  90. if definitionFlag != "" {
  91. if def, err := intrgb.LoadDefinition(definitionFlag); err == nil && def.Matches(vendorID, productID) {
  92. return def
  93. }
  94. return nil
  95. }
  96. dir, err := definitionsDir()
  97. if err != nil {
  98. return nil
  99. }
  100. defs, err := intrgb.LoadDefinitionsDir(dir)
  101. if err != nil {
  102. return nil
  103. }
  104. return intrgb.FindDefinition(defs, vendorID, productID)
  105. }
  106. // applyDefinitionLabels returns the display names a board answers to and, beside
  107. // them, the alternatives each channel keeps. The definition's label is the name
  108. // the board is called in VIA; the QMK subsystem name stays an accepted
  109. // alternative, because it follows from the channel number and is the one a
  110. // document can promise without knowing the board.
  111. func applyDefinitionLabels(vendorID, productID uint16) (map[uint16]string, map[uint16][]string) {
  112. labels := definitionLabels(vendorID, productID)
  113. display := make(map[uint16]string, len(labels))
  114. alternatives := make(map[uint16][]string, len(labels))
  115. for _, ch := range via.LightingChannels {
  116. label := labels[uint16(ch)]
  117. if label == "" {
  118. label = ch.Subsystem()
  119. }
  120. display[uint16(ch)] = label
  121. if label != ch.Subsystem() {
  122. alternatives[uint16(ch)] = []string{ch.Subsystem()}
  123. }
  124. }
  125. return display, alternatives
  126. }
  127. // boardName is what a board is called: the name its definition gives it, else the
  128. // USB product string the keyboard itself reports, else an honest placeholder.
  129. func boardName(dev intdevice.Device, _ map[uint16]string) string {
  130. if def := loadedDefinition(dev.VendorID, dev.ProductID); def != nil && def.Name != "" {
  131. return def.Name
  132. }
  133. if dev.Name != "" {
  134. return dev.Name
  135. }
  136. return "unknown model"
  137. }
  138. // definitionsDir returns the data directory, looked for next to the executable
  139. // first and then in the working directory.
  140. var definitionsDir = func() (string, error) {
  141. paths := lookupPaths()
  142. for _, p := range paths {
  143. dir := filepath.Join(p, intrgb.DefinitionsDir)
  144. if info, err := os.Stat(dir); err == nil && info.IsDir() {
  145. return dir, nil
  146. }
  147. }
  148. if len(paths) == 0 {
  149. return intrgb.DefinitionsDir, nil
  150. }
  151. return filepath.Join(paths[0], intrgb.DefinitionsDir), nil
  152. }
  153. // ensureDefinitionsDir returns the data directory, creating it if it is not
  154. // there yet, so a fetch has somewhere to write to.
  155. func ensureDefinitionsDir() (string, error) {
  156. dir, err := definitionsDir()
  157. if err != nil {
  158. return "", err
  159. }
  160. if err := os.MkdirAll(dir, 0o755); err != nil {
  161. return "", fmt.Errorf("create %s: %w", dir, err)
  162. }
  163. return dir, nil
  164. }