effect.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package rgb
  2. import (
  3. "fmt"
  4. "os"
  5. "strings"
  6. "github.com/spf13/cobra"
  7. intrgb "github.com/wobkey/rgb/internal/rgb"
  8. "github.com/wobkey/rgb/internal/via"
  9. )
  10. func resolveEffectTargets(name string, zones []intrgb.Zone) ([]intrgb.EffectTarget, []intrgb.Zone, error) {
  11. if name == "static" {
  12. name = "solid"
  13. }
  14. return intrgb.ResolveEffect(name, zones)
  15. }
  16. func NewEffectCmd() *cobra.Command {
  17. return &cobra.Command{
  18. Use: "effect <name>",
  19. Short: "Set RGB effect",
  20. Long: "Set the RGB lighting effect on the connected keyboard.",
  21. Args: cobra.ExactArgs(1),
  22. RunE: func(_ *cobra.Command, args []string) error {
  23. zones, err := selectedZones()
  24. if err != nil {
  25. return err
  26. }
  27. targets, skipped, err := resolveEffectTargets(args[0], zones)
  28. if err != nil {
  29. return err
  30. }
  31. if len(skipped) > 0 {
  32. skippedNames := make([]string, len(skipped))
  33. for i, zone := range skipped {
  34. skippedNames[i] = string(zone)
  35. }
  36. fmt.Fprintf(os.Stderr, "Warning: effect not supported on zone(s): %s\n", strings.Join(skippedNames, ", "))
  37. }
  38. proto, _, err := OpenDevice()
  39. if err != nil {
  40. return err
  41. }
  42. defer proto.Close()
  43. for _, target := range targets {
  44. if err := proto.SetValue(via.LEDType(target.Zone.Channel()), uint8(intrgb.EffectID), target.ID); err != nil {
  45. return fmt.Errorf("set effect on %s: %w", target.Zone, err)
  46. }
  47. }
  48. fmt.Printf("Effect set to %q\n", args[0])
  49. return nil
  50. },
  51. }
  52. }