catalog.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package rgb
  2. import (
  3. "fmt"
  4. "strings"
  5. "netdome.biz/paul/qmk-rgb/internal/via"
  6. )
  7. // EffectTarget is one effect ID to write to one channel.
  8. type EffectTarget struct {
  9. Channel via.Channel
  10. ID uint8
  11. }
  12. const unknownEffectName = "unknown"
  13. // Effect is one named effect on a channel: the ID the keyboard uses and the name
  14. // a definition gives it. The pair is explicit because a VIA definition may attach
  15. // a number to an option that is not its position in the list, so a board's effect
  16. // ID is not an index into its names.
  17. type Effect struct {
  18. ID uint8
  19. Name string
  20. }
  21. // Catalog is one board's effect names, per channel. The keyboard holds numbers,
  22. // not names, so `effect <name>` needs a catalog and a board without one is driven
  23. // through raw IDs.
  24. //
  25. // There is no compiled-in catalog for any board. A board's effect names come from
  26. // its VIA definition file, read at runtime from the data directory or from the file
  27. // --definition names, because a hand-written list and the vendor's file describing
  28. // the same board are two places to update one name, and that is how they drift
  29. // apart. A board with no definition has no names and is driven through raw effect
  30. // IDs.
  31. type Catalog struct {
  32. board string
  33. names map[via.Channel][]Effect
  34. aliases map[via.Channel]map[string]string
  35. }
  36. // NewCatalog returns a catalog for a board described by effect entries, as a VIA
  37. // definition file provides them. The board name is what `effect --list` reports;
  38. // the entries may leave gaps, because a definition names only the effects a board's
  39. // firmware implements.
  40. //
  41. // The tool's aliases come with it, because they are a spelling convenience and not
  42. // board knowledge: a definition writes the manufacturer's spelling, and the user
  43. // should not have to know which one it chose.
  44. func NewCatalog(board string, effects map[via.Channel][]Effect) *Catalog {
  45. return &Catalog{board: board, names: effects, aliases: defaultAliases}
  46. }
  47. // defaultAliases are the spellings every catalog accepts, whatever the source of
  48. // its names. They resolve per channel, so `rainbow` is the vendor's `spectrum`
  49. // where that is the name and the catalog's own on another channel.
  50. var defaultAliases = map[via.Channel]map[string]string{
  51. via.ChannelRgblight: {
  52. "off": "none",
  53. "breathe": "breathing",
  54. "rainbow": "spectrum",
  55. "rainbow_wave": "wave",
  56. "solid": "light",
  57. "static": "solid",
  58. },
  59. via.ChannelRgbMatrix: {
  60. "off": "none",
  61. "breathe": "breathing",
  62. "rainbow": "rainbow_moving_chevron",
  63. "solid": "solid_color",
  64. "static": "solid",
  65. },
  66. via.ChannelAudio: {
  67. "off": "none",
  68. "breathe": "breathing",
  69. "rainbow": "spectrum",
  70. "rainbow_wave": "wave",
  71. "solid": "light",
  72. "static": "solid",
  73. },
  74. }
  75. // Name returns the catalog's board name, which `effect --list` reports.
  76. func (c *Catalog) Name() string {
  77. if c == nil {
  78. return ""
  79. }
  80. return c.board
  81. }
  82. // Effects returns a channel's named effects, or nil when the catalog says nothing
  83. // about it.
  84. func (c *Catalog) Effects(ch via.Channel) []Effect {
  85. if c == nil {
  86. return nil
  87. }
  88. return c.names[ch]
  89. }
  90. // Names returns the effect names of a channel in effect-ID order, or nil when the
  91. // catalog says nothing about it.
  92. func (c *Catalog) Names(ch via.Channel) []string {
  93. effects := c.Effects(ch)
  94. if effects == nil {
  95. return nil
  96. }
  97. names := make([]string, 0, len(effects))
  98. for _, e := range effects {
  99. names = append(names, e.Name)
  100. }
  101. return names
  102. }
  103. // EffectName returns the name of an effect ID, or "unknown" when the catalog has no
  104. // entry for it. A board may take an effect ID that no definition names, and then
  105. // this says so rather than inventing a label for it.
  106. func (c *Catalog) EffectName(ch via.Channel, id uint8) string {
  107. if c == nil {
  108. return unknownEffectName
  109. }
  110. for _, e := range c.names[ch] {
  111. if e.ID == id {
  112. return e.Name
  113. }
  114. }
  115. return unknownEffectName
  116. }
  117. // EffectID resolves an effect name on a channel, following one level of alias.
  118. func (c *Catalog) EffectID(ch via.Channel, name string) (uint8, bool) {
  119. if c == nil {
  120. return 0, false
  121. }
  122. if id, found := c.byName(ch, name); found {
  123. return id, true
  124. }
  125. if canonical, ok := c.aliases[ch][name]; ok {
  126. if id, found := c.EffectID(ch, canonical); found {
  127. return id, true
  128. }
  129. }
  130. // A definition may carry the other spelling of the same pair: the tool's alias
  131. // table says "breathe" for "breathing", and a manufacturer's file may use
  132. // either. So a name that is the target of an alias resolves to that alias where
  133. // the channel has it.
  134. for alias, canonical := range c.aliases[ch] {
  135. if canonical != name {
  136. continue
  137. }
  138. if id, found := c.byName(ch, alias); found {
  139. return id, true
  140. }
  141. }
  142. return 0, false
  143. }
  144. // byName finds an effect by its exact name, without following an alias, then by the
  145. // same name with spaces written as underscores. A definition carries display
  146. // spellings where the tool carries identifiers, and the difference is whitespace,
  147. // not a different effect: "fixed wave" and "fixed_wave" name the same thing, and the
  148. // tool's documented spelling has to keep working while a definition file is present.
  149. func (c *Catalog) byName(ch via.Channel, name string) (uint8, bool) {
  150. for _, e := range c.names[ch] {
  151. if e.Name == name {
  152. return e.ID, true
  153. }
  154. }
  155. spaced := strings.ReplaceAll(name, "_", " ")
  156. if spaced != name {
  157. for _, e := range c.names[ch] {
  158. if e.Name == spaced {
  159. return e.ID, true
  160. }
  161. }
  162. }
  163. return 0, false
  164. }
  165. // DefaultEffect returns the effect enable writes when turning a channel on: the
  166. // first effect the board's own list names that is not the off entry, which is what
  167. // makes a channel light at all. A definition carries no notion of a default effect,
  168. // so the board's list is where the answer comes from, and a channel whose list holds
  169. // nothing but the off entry has none to give.
  170. func (c *Catalog) DefaultEffect(ch via.Channel) (uint8, bool) {
  171. if c == nil {
  172. return 0, false
  173. }
  174. for _, e := range c.names[ch] {
  175. if e.ID != 0 {
  176. return e.ID, true
  177. }
  178. }
  179. return 0, false
  180. }
  181. // ResolveEffect turns an effect name into one target per channel that supports it,
  182. // plus the subsystem names of those that do not. A skip is an error rather than a
  183. // warning when the caller asked for one channel explicitly, because a command that
  184. // silently did nothing looks like a command that worked. That is the caller's
  185. // knowledge to pass: a board with a single lighting channel is not an explicit
  186. // request for it.
  187. func ResolveEffect(catalog *Catalog, name string, channels []via.Channel, explicit bool) ([]EffectTarget, []string, error) {
  188. if catalog == nil {
  189. // Both ways out belong in the message: the board is still drivable by
  190. // number, and the names are one command away. A user who reads only the
  191. // error should not have to find either out elsewhere.
  192. return nil, nil, fmt.Errorf("no effect names for this keyboard: run `definition fetch` for its VIA definition, " +
  193. "or set an effect by number with `effect <index>`")
  194. }
  195. // The compatibility spelling every catalog shares, so a caller cannot resolve
  196. // "static" differently from another.
  197. if name == "static" {
  198. name = "solid"
  199. }
  200. // Whether the board has the name at all is a different question from whether the
  201. // channels asked for can do it: naming a real effect on the wrong channel is a
  202. // different mistake, with a different fix, and gets its own message.
  203. knownAnywhere := false
  204. for ch := range catalog.names {
  205. if _, ok := catalog.EffectID(ch, name); ok {
  206. knownAnywhere = true
  207. break
  208. }
  209. }
  210. if !knownAnywhere {
  211. // A name nobody wrote is either a typo or a number that belongs in the
  212. // other form, and pointing at it saves the user from guessing.
  213. return nil, nil, fmt.Errorf("unknown effect: %s (an effect ID from 0 to 255 is `effect <index>`)", name)
  214. }
  215. var targets []EffectTarget
  216. var skipped []string
  217. for _, ch := range channels {
  218. id, ok := catalog.EffectID(ch, name)
  219. if !ok {
  220. if explicit {
  221. return nil, nil, fmt.Errorf("effect %s is not supported on %s", name, ch.Subsystem())
  222. }
  223. skipped = append(skipped, ch.Subsystem())
  224. continue
  225. }
  226. targets = append(targets, EffectTarget{Channel: ch, ID: id})
  227. }
  228. return targets, skipped, nil
  229. }