effect.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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, source, err := resolveCatalog(target)
  41. if err != nil {
  42. return err
  43. }
  44. return printEffectList(cmd, catalog, channels, target.Display, source)
  45. }
  46. // printEffectList writes the catalog per channel, as text unless --json asks
  47. // for the machine shape. The source of the names travels along, because a list
  48. // without it does not say whether it came from a definition file or from the
  49. // tool itself.
  50. func printEffectList(cmd *cobra.Command, catalog *intrgb.Catalog, channels []via.Channel, display map[uint16]string, source string) error {
  51. if !jsonOutput {
  52. return printEffectListText(cmd.OutOrStdout(), catalog, channels, display, source)
  53. }
  54. return printEffectListJSON(cmd, catalog, channels, display)
  55. }
  56. func printEffectListJSON(cmd *cobra.Command, catalog *intrgb.Catalog, channels []via.Channel, display map[uint16]string) error {
  57. type ZoneEffectList struct {
  58. Zone string `json:"zone"`
  59. Channel uint8 `json:"channel"`
  60. Subsystem string `json:"subsystem"`
  61. Effect string `json:"effect"`
  62. ID uint8 `json:"id"`
  63. }
  64. type EffectList struct {
  65. Catalog string `json:"catalog"`
  66. Zones []ZoneEffectList `json:"zones"`
  67. }
  68. list := EffectList{Catalog: catalog.Name(), Zones: []ZoneEffectList{}}
  69. for _, ch := range channels {
  70. for _, e := range catalog.Effects(ch) {
  71. list.Zones = append(list.Zones, ZoneEffectList{
  72. Zone: channelName(ch, display),
  73. Channel: uint8(ch),
  74. Subsystem: ch.Subsystem(),
  75. Effect: e.Name,
  76. ID: e.ID,
  77. })
  78. }
  79. }
  80. return encodeJSON(cmd.OutOrStdout(), list)
  81. }
  82. func runEffectSet(cmd *cobra.Command, args []string) error {
  83. proto, target, channels, err := openTarget()
  84. if err != nil {
  85. return err
  86. }
  87. defer proto.Close()
  88. catalog, _, err := resolveCatalog(target)
  89. if err != nil {
  90. return err
  91. }
  92. targets, skipped, err := resolveEffectTargets(catalog, args[0], channels)
  93. if err != nil {
  94. return err
  95. }
  96. if len(skipped) > 0 {
  97. fmt.Fprintf(cmd.ErrOrStderr(), "Warning: effect not supported on channel(s): %s\n", strings.Join(skipped, ", "))
  98. }
  99. results, err := setEffectVerified(proto, targets, target.Display, catalog)
  100. if err != nil {
  101. return err
  102. }
  103. // Claiming the requested effect when the keyboard kept another one is the
  104. // contradiction this read-back exists to prevent: on the logo and side
  105. // channels an effect of ID 0 leaves the previous effect running.
  106. if anyEffectMismatch(results) {
  107. fmt.Fprintln(cmd.OutOrStdout(), formatEffectResults(args[0], results))
  108. return nil
  109. }
  110. fmt.Fprintf(cmd.OutOrStdout(), "Effect set to %q\n", args[0])
  111. return nil
  112. }