catalog.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 the 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 the 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. // HasNames reports whether the catalog names any effect at all, on any channel. A
  91. // definition can be present and name nothing, which is not the same as having no
  92. // definition: a board whose file refers to VIA's built-in lighting menu by name
  93. // rather than listing its effects carries no names, because the names for that menu
  94. // live in VIA's own code. The two situations are told apart in a message, because
  95. // the fix for one is no use at all for the other.
  96. func (c *Catalog) HasNames() bool {
  97. if c == nil {
  98. return false
  99. }
  100. return len(c.names) > 0
  101. }
  102. // Names returns the effect names of a channel in effect-ID order, or nil when the
  103. // catalog says nothing about it.
  104. func (c *Catalog) Names(ch via.Channel) []string {
  105. effects := c.Effects(ch)
  106. if effects == nil {
  107. return nil
  108. }
  109. names := make([]string, 0, len(effects))
  110. for _, e := range effects {
  111. names = append(names, e.Name)
  112. }
  113. return names
  114. }
  115. // EffectName returns the name of an effect ID, or "unknown" when the catalog has no
  116. // entry for it. A board may take an effect ID that no definition names, and then
  117. // this says so rather than inventing a label for it.
  118. func (c *Catalog) EffectName(ch via.Channel, id uint8) string {
  119. if c == nil {
  120. return unknownEffectName
  121. }
  122. for _, e := range c.names[ch] {
  123. if e.ID == id {
  124. return e.Name
  125. }
  126. }
  127. return unknownEffectName
  128. }
  129. // EffectID resolves an effect name on a channel, following one level of alias.
  130. func (c *Catalog) EffectID(ch via.Channel, name string) (uint8, bool) {
  131. if c == nil {
  132. return 0, false
  133. }
  134. if id, found := c.byName(ch, name); found {
  135. return id, true
  136. }
  137. if canonical, ok := c.aliases[ch][name]; ok {
  138. if id, found := c.EffectID(ch, canonical); found {
  139. return id, true
  140. }
  141. }
  142. // A definition may carry the other spelling of the same pair: the tool's alias
  143. // table says "breathe" for "breathing", and a manufacturer's file may use
  144. // either. So a name that is the target of an alias resolves to that alias where
  145. // the channel has it.
  146. for alias, canonical := range c.aliases[ch] {
  147. if canonical != name {
  148. continue
  149. }
  150. if id, found := c.byName(ch, alias); found {
  151. return id, true
  152. }
  153. }
  154. return 0, false
  155. }
  156. // byName finds an effect by its exact name, without following an alias, then by the
  157. // same name with spaces written as underscores. A definition carries display
  158. // spellings where the tool carries identifiers, and the difference is whitespace,
  159. // not a different effect: "fixed wave" and "fixed_wave" name the same thing, and the
  160. // tool's documented spelling has to keep working while a definition file is present.
  161. func (c *Catalog) byName(ch via.Channel, name string) (uint8, bool) {
  162. for _, e := range c.names[ch] {
  163. if e.Name == name {
  164. return e.ID, true
  165. }
  166. }
  167. spaced := strings.ReplaceAll(name, "_", " ")
  168. if spaced != name {
  169. for _, e := range c.names[ch] {
  170. if e.Name == spaced {
  171. return e.ID, true
  172. }
  173. }
  174. }
  175. return 0, false
  176. }
  177. // DefaultEffect returns the effect enable writes when turning a channel on: the
  178. // first effect the board's own list names that is not the off entry, which is what
  179. // makes a channel light at all. A definition carries no notion of a default effect,
  180. // so the board's list is where the answer comes from, and a channel whose list holds
  181. // nothing but the off entry has none to give.
  182. func (c *Catalog) DefaultEffect(ch via.Channel) (uint8, bool) {
  183. if c == nil {
  184. return 0, false
  185. }
  186. for _, e := range c.names[ch] {
  187. if e.ID != 0 {
  188. return e.ID, true
  189. }
  190. }
  191. return 0, false
  192. }
  193. // ResolveEffect turns an effect name into one target per channel that supports it,
  194. // plus the subsystem names of those that do not. A skip is an error rather than a
  195. // warning when the caller asked for one channel explicitly, because a command that
  196. // silently did nothing looks like a command that worked. That is the caller's
  197. // knowledge to pass: a board with a single lighting channel is not an explicit
  198. // request for it.
  199. func ResolveEffect(catalog *Catalog, name string, channels []via.Channel, explicit bool) ([]EffectTarget, []string, error) {
  200. if catalog == nil {
  201. // Both ways out belong in the message: the board is still drivable by
  202. // number, and the names are one command away. A user who reads only the
  203. // error should not have to find either out elsewhere.
  204. return nil, nil, fmt.Errorf("no effect names for this keyboard: run `keyboard fetch` for its VIA definition, " +
  205. "or set an effect by number with `effect <zone> <index>`")
  206. }
  207. if !catalog.HasNames() {
  208. // The definition was found and it names nothing, so it is not a missing file
  209. // and fetching again changes nothing. Saying "unknown effect" here would
  210. // point at a typo in a name the tool holds none of.
  211. return nil, nil, fmt.Errorf("the VIA definition for %s names no effects, so no effect name can be resolved; "+
  212. "this board's names are not in the file, and an effect is set by number with `effect <zone> <index>`",
  213. catalog.Name())
  214. }
  215. // The compatibility spelling every catalog shares, so a caller cannot resolve
  216. // "static" differently from another.
  217. if name == "static" {
  218. name = "solid"
  219. }
  220. // Whether the board has the name at all is a different question from whether the
  221. // channels asked for can do it: naming a real effect on the wrong channel is a
  222. // different mistake, with a different fix, and gets its own message.
  223. knownAnywhere := false
  224. for ch := range catalog.names {
  225. if _, ok := catalog.EffectID(ch, name); ok {
  226. knownAnywhere = true
  227. break
  228. }
  229. }
  230. if !knownAnywhere {
  231. // A name nobody wrote is either a typo or a number that belongs in the
  232. // other form, and pointing at it saves the user from guessing. The ID form
  233. // is `effect <zone> <index>`: the zone is the command's first argument and
  234. // is required, so an index on its own is read as a channel name.
  235. return nil, nil, fmt.Errorf("unknown effect: %s (an effect ID from 0 to 255 is `effect <zone> <index>`)", name)
  236. }
  237. var targets []EffectTarget
  238. var skipped []string
  239. for _, ch := range channels {
  240. id, ok := catalog.EffectID(ch, name)
  241. if !ok {
  242. if explicit {
  243. return nil, nil, fmt.Errorf("effect %s is not supported on %s", name, ch.Subsystem())
  244. }
  245. skipped = append(skipped, ch.Subsystem())
  246. continue
  247. }
  248. targets = append(targets, EffectTarget{Channel: ch, ID: id})
  249. }
  250. return targets, skipped, nil
  251. }