effect.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/spf13/cobra"
  6. intrgb "netdome.biz/paul/qmk-rgb/internal/rgb"
  7. "netdome.biz/paul/qmk-rgb/internal/via"
  8. )
  9. var listEffects bool
  10. // resolveEffectTargets turns a name into one target per channel. Whether the
  11. // user named a channel is passed in, because a board with a single channel is
  12. // not an explicit request for it.
  13. func resolveEffectTargets(catalog *intrgb.Catalog, name string, channels []via.Channel) ([]intrgb.EffectTarget, []string, error) {
  14. return intrgb.ResolveEffect(catalog, name, channels, targetZone != "")
  15. }
  16. func NewEffectCmd() *cobra.Command {
  17. cmd := &cobra.Command{
  18. Use: "effect [name]",
  19. Short: "Set or list RGB effects",
  20. Long: "Set the RGB lighting effect on the connected keyboard. Without an argument, lists all effects.\n" +
  21. "Effect names come from a per-board catalog; a keyboard without one has no names\n" +
  22. "and is driven with `mode <index>` instead.",
  23. Args: cobra.MaximumNArgs(1),
  24. RunE: func(cmd *cobra.Command, args []string) error {
  25. if listEffects || len(args) == 0 {
  26. return listAllEffects(cmd)
  27. }
  28. return runEffectSet(cmd, args)
  29. },
  30. }
  31. cmd.Flags().BoolVar(&listEffects, "list", false, "List all available effects per zone")
  32. return cmd
  33. }
  34. func listAllEffects(cmd *cobra.Command) error {
  35. proto, target, channels, err := openTarget()
  36. if err != nil {
  37. return err
  38. }
  39. defer proto.Close()
  40. catalog, _ := intrgb.CatalogFor(target.Device.VendorID, target.Device.ProductID)
  41. return printEffectList(cmd, catalog, channels, target.Display)
  42. }
  43. func printEffectList(cmd *cobra.Command, catalog *intrgb.Catalog, channels []via.Channel, display map[uint16]string) error {
  44. type ZoneEffectList struct {
  45. Zone string `json:"zone"`
  46. Channel uint8 `json:"channel"`
  47. Subsystem string `json:"subsystem"`
  48. Effect string `json:"effect"`
  49. ID uint8 `json:"id"`
  50. }
  51. type EffectList struct {
  52. Catalog string `json:"catalog"`
  53. Zones []ZoneEffectList `json:"zones"`
  54. }
  55. list := EffectList{Catalog: catalog.Name(), Zones: []ZoneEffectList{}}
  56. for _, ch := range channels {
  57. for id, name := range catalog.Names(ch) {
  58. list.Zones = append(list.Zones, ZoneEffectList{
  59. Zone: channelName(ch, display),
  60. Channel: uint8(ch),
  61. Subsystem: ch.Subsystem(),
  62. Effect: name,
  63. ID: uint8(id),
  64. })
  65. }
  66. }
  67. return encodeJSON(cmd.OutOrStdout(), list)
  68. }
  69. func runEffectSet(cmd *cobra.Command, args []string) error {
  70. proto, target, channels, err := openTarget()
  71. if err != nil {
  72. return err
  73. }
  74. defer proto.Close()
  75. catalog, _ := intrgb.CatalogFor(target.Device.VendorID, target.Device.ProductID)
  76. targets, skipped, err := resolveEffectTargets(catalog, args[0], channels)
  77. if err != nil {
  78. return err
  79. }
  80. if len(skipped) > 0 {
  81. fmt.Fprintf(cmd.ErrOrStderr(), "Warning: effect not supported on channel(s): %s\n", strings.Join(skipped, ", "))
  82. }
  83. results, err := setEffectVerified(proto, targets, target.Display, catalog)
  84. if err != nil {
  85. return err
  86. }
  87. // Claiming the requested effect when the keyboard kept another one is the
  88. // contradiction this read-back exists to prevent: on the logo and side
  89. // channels an effect of ID 0 leaves the previous effect running.
  90. if anyEffectMismatch(results) {
  91. fmt.Fprintln(cmd.OutOrStdout(), formatEffectResults(args[0], results))
  92. return nil
  93. }
  94. fmt.Fprintf(cmd.OutOrStdout(), "Effect set to %q\n", args[0])
  95. return nil
  96. }