effect.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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, rewriting the
  11. // legacy spelling the Impact 80's own keycodes use.
  12. func resolveEffectTargets(catalog *intrgb.Catalog, name string, channels []via.Channel) ([]intrgb.EffectTarget, []string, error) {
  13. if name == "static" {
  14. name = "solid"
  15. }
  16. return intrgb.ResolveEffect(catalog, name, channels)
  17. }
  18. func NewEffectCmd() *cobra.Command {
  19. cmd := &cobra.Command{
  20. Use: "effect [name]",
  21. Short: "Set or list RGB effects",
  22. Long: "Set the RGB lighting effect on the connected keyboard. Without an argument, lists all effects.\n" +
  23. "Effect names come from a per-board catalog; a keyboard without one has no names\n" +
  24. "and is driven with `mode <index>` instead.",
  25. Args: cobra.MaximumNArgs(1),
  26. RunE: func(cmd *cobra.Command, args []string) error {
  27. if listEffects || len(args) == 0 {
  28. return listAllEffects(cmd)
  29. }
  30. return runEffectSet(cmd, args)
  31. },
  32. }
  33. cmd.Flags().BoolVar(&listEffects, "list", false, "List all available effects per zone")
  34. return cmd
  35. }
  36. func listAllEffects(cmd *cobra.Command) error {
  37. proto, target, channels, err := openTarget()
  38. if err != nil {
  39. return err
  40. }
  41. defer proto.Close()
  42. catalog, _ := intrgb.CatalogFor(target.Device.VendorID, target.Device.ProductID)
  43. return printEffectList(cmd, catalog, channels, target.Display)
  44. }
  45. func printEffectList(cmd *cobra.Command, catalog *intrgb.Catalog, channels []via.Channel, display map[uint16]string) error {
  46. type ZoneEffectList struct {
  47. Zone string `json:"zone"`
  48. Channel uint8 `json:"channel"`
  49. Subsystem string `json:"subsystem"`
  50. Effect string `json:"effect"`
  51. ID uint8 `json:"id"`
  52. }
  53. type EffectList struct {
  54. Catalog string `json:"catalog"`
  55. Zones []ZoneEffectList `json:"zones"`
  56. }
  57. list := EffectList{Catalog: catalog.Name(), Zones: []ZoneEffectList{}}
  58. for _, ch := range channels {
  59. for id, name := range catalog.Names(ch) {
  60. list.Zones = append(list.Zones, ZoneEffectList{
  61. Zone: channelName(ch, display),
  62. Channel: uint8(ch),
  63. Subsystem: ch.Subsystem(),
  64. Effect: name,
  65. ID: uint8(id),
  66. })
  67. }
  68. }
  69. return encodeJSON(cmd.OutOrStdout(), list)
  70. }
  71. func runEffectSet(cmd *cobra.Command, args []string) error {
  72. proto, target, channels, err := openTarget()
  73. if err != nil {
  74. return err
  75. }
  76. defer proto.Close()
  77. catalog, _ := intrgb.CatalogFor(target.Device.VendorID, target.Device.ProductID)
  78. targets, skipped, err := resolveEffectTargets(catalog, args[0], channels)
  79. if err != nil {
  80. return err
  81. }
  82. if len(skipped) > 0 {
  83. fmt.Fprintf(cmd.ErrOrStderr(), "Warning: effect not supported on channel(s): %s\n", strings.Join(skipped, ", "))
  84. }
  85. for _, t := range targets {
  86. if err := proto.SetValue(t.Channel, uint8(intrgb.EffectID), t.ID); err != nil {
  87. return fmt.Errorf("set effect on %s: %w", channelName(t.Channel, target.Display), err)
  88. }
  89. }
  90. fmt.Fprintf(cmd.OutOrStdout(), "Effect set to %q\n", args[0])
  91. return nil
  92. }