profile.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "os"
  7. "path/filepath"
  8. "sort"
  9. "strings"
  10. "github.com/spf13/cobra"
  11. intrgb "netdome.biz/paul/qmk-rgb/internal/rgb"
  12. "netdome.biz/paul/qmk-rgb/internal/via"
  13. )
  14. // profilesDir is a var so tests can redirect it at a temp directory.
  15. var profilesDir = "profiles"
  16. type Profile struct {
  17. Name string `json:"name"`
  18. Version int `json:"version"`
  19. Board *Board `json:"board,omitempty"`
  20. Zones map[string]*ZoneSettings `json:"zones"`
  21. }
  22. // Board identifies the keyboard a profile was saved from. A profile stores
  23. // effect names, and names belong to a board, so without this a profile written
  24. // for one keyboard would be applied to another without a word. The pair is
  25. // spelled as hex strings, the way the VIA definition files
  26. // spell it, so the three can be read side by side.
  27. type Board struct {
  28. VendorID string `json:"vendorId"`
  29. ProductID string `json:"productId"`
  30. }
  31. // boardMatch is what comparing a profile's board to the connected one tells us.
  32. type boardMatch int
  33. const (
  34. // boardUnknown is a profile written before profiles carried a board.
  35. boardUnknown boardMatch = iota
  36. boardSame
  37. boardOther
  38. )
  39. func boardFor(vendorID, productID uint16) *Board {
  40. return &Board{
  41. VendorID: fmt.Sprintf("0x%04X", vendorID),
  42. ProductID: fmt.Sprintf("0x%04X", productID),
  43. }
  44. }
  45. // MatchesBoard reports whether a profile was saved from the connected keyboard,
  46. // was saved from another one, or says nothing about it.
  47. func (p *Profile) MatchesBoard(vendorID, productID uint16) boardMatch {
  48. if p == nil || p.Board == nil {
  49. return boardUnknown
  50. }
  51. if strings.EqualFold(p.Board.VendorID, fmt.Sprintf("0x%04X", vendorID)) &&
  52. strings.EqualFold(p.Board.ProductID, fmt.Sprintf("0x%04X", productID)) {
  53. return boardSame
  54. }
  55. return boardOther
  56. }
  57. // boardMismatchWarning says which profile belongs to which keyboard, because
  58. // the user has to be able to tell which of their profiles is the wrong one.
  59. func (p *Profile) boardMismatchWarning(vendorID, productID uint16) string {
  60. return fmt.Sprintf("Warning: profile %q was saved for keyboard %s/%s, and this keyboard is %s/%s; "+
  61. "its effect names may not exist here\n",
  62. p.Name, p.Board.VendorID, p.Board.ProductID,
  63. fmt.Sprintf("0x%04X", vendorID), fmt.Sprintf("0x%04X", productID))
  64. }
  65. // listLine is one profile as the list command prints it.
  66. func (p *Profile) listLine() string {
  67. if p.Board == nil {
  68. return p.Name
  69. }
  70. return fmt.Sprintf("%s %s/%s", p.Name, p.Board.VendorID, p.Board.ProductID)
  71. }
  72. type ZoneSettings struct {
  73. Enabled bool `json:"enabled"`
  74. Effect string `json:"effect"`
  75. Brightness uint8 `json:"brightness"`
  76. Speed uint8 `json:"speed"`
  77. Color string `json:"color"`
  78. }
  79. func ProfilesPath() string {
  80. return filepath.Join(profilesDir)
  81. }
  82. func (p *Profile) Save() error {
  83. if p.Name == "" {
  84. return fmt.Errorf("profile name is required")
  85. }
  86. if err := os.MkdirAll(profilesDir, 0755); err != nil {
  87. return fmt.Errorf("create profiles directory: %w", err)
  88. }
  89. data, err := json.MarshalIndent(p, "", " ")
  90. if err != nil {
  91. return fmt.Errorf("marshal profile: %w", err)
  92. }
  93. path := filepath.Join(profilesDir, sanitizeFilename(p.Name)+".json")
  94. if err := os.WriteFile(path, data, 0644); err != nil {
  95. return fmt.Errorf("write profile: %w", err)
  96. }
  97. return nil
  98. }
  99. func LoadProfile(name string) (*Profile, error) {
  100. path := filepath.Join(profilesDir, sanitizeFilename(name)+".json")
  101. data, err := os.ReadFile(path)
  102. if err != nil {
  103. return nil, fmt.Errorf("read profile %s: %w", name, err)
  104. }
  105. var p Profile
  106. if err := json.Unmarshal(data, &p); err != nil {
  107. return nil, fmt.Errorf("parse profile %s: %w", name, err)
  108. }
  109. return &p, nil
  110. }
  111. func ListProfiles() ([]string, error) {
  112. entries, err := os.ReadDir(profilesDir)
  113. if err != nil {
  114. if os.IsNotExist(err) {
  115. return nil, nil
  116. }
  117. return nil, fmt.Errorf("list profiles: %w", err)
  118. }
  119. var names []string
  120. for _, entry := range entries {
  121. if entry.IsDir() {
  122. continue
  123. }
  124. name := entry.Name()
  125. if strings.HasSuffix(name, ".json") {
  126. name = name[:len(name)-5]
  127. names = append(names, name)
  128. }
  129. }
  130. return names, nil
  131. }
  132. func DeleteProfile(name string) error {
  133. path := filepath.Join(profilesDir, sanitizeFilename(name)+".json")
  134. if err := os.Remove(path); err != nil {
  135. if os.IsNotExist(err) {
  136. return fmt.Errorf("profile %s not found", name)
  137. }
  138. return fmt.Errorf("delete profile %s: %w", name, err)
  139. }
  140. return nil
  141. }
  142. func sanitizeFilename(name string) string {
  143. name = strings.ToLower(name)
  144. name = strings.Map(func(r rune) rune {
  145. if (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') || r == '-' || r == '_' {
  146. return r
  147. }
  148. return '-'
  149. }, name)
  150. if len(name) > 0 && name[0] == '-' {
  151. name = "unnamed-" + name
  152. }
  153. return name
  154. }
  155. // applyProfile reads RGB state from the device and stores it in a Profile.
  156. func applyProfileToProfile(proto rgbProtocol, channels []via.Channel, display map[uint16]string, catalog *intrgb.Catalog, p *Profile) error {
  157. out, err := readInfo(proto, channels, display, catalog)
  158. if err != nil {
  159. return fmt.Errorf("read device state: %w", err)
  160. }
  161. p.Zones = make(map[string]*ZoneSettings)
  162. for _, zi := range out.Zones {
  163. if zi.Error != "" {
  164. continue
  165. }
  166. p.Zones[zi.Zone] = &ZoneSettings{
  167. Enabled: zi.Enabled,
  168. Effect: zi.Effect,
  169. Brightness: zi.Brightness,
  170. Speed: zi.Speed,
  171. Color: fmt.Sprintf("%02x%02x", zi.Color.Hue, zi.Color.Saturation),
  172. }
  173. }
  174. return nil
  175. }
  176. // loadProfileFromDevice reads RGB state from device and saves it. Warnings go
  177. // to warn, which is the command's stderr.
  178. func loadProfileFromDevice(name string, warn io.Writer) error {
  179. proto, target, channels, err := openTarget()
  180. if err != nil {
  181. return err
  182. }
  183. defer proto.Close()
  184. catalog, _, err := resolveCatalog(target)
  185. if err != nil {
  186. return err
  187. }
  188. if catalog == nil {
  189. // The profile stores effect names and a board without a catalog has none
  190. // to store, so every channel is written as "unknown" and cannot be
  191. // restored. Say so here, where the user can still act on it.
  192. fmt.Fprintf(warn,
  193. "Warning: this keyboard has no effect catalog, so the profile records effect %q and cannot restore it; set an effect with `mode <index>`\n", "unknown")
  194. }
  195. p := &Profile{
  196. Name: name,
  197. Version: 1,
  198. Board: boardFor(target.Device.VendorID, target.Device.ProductID),
  199. }
  200. if err := applyProfileToProfile(proto, channels, target.Display, catalog, p); err != nil {
  201. return err
  202. }
  203. return p.Save()
  204. }
  205. func NewProfileSaveCmd() *cobra.Command {
  206. var name string
  207. cmd := &cobra.Command{
  208. Use: "save [name]",
  209. Short: "Save current RGB state to a profile",
  210. Long: "Read the current RGB settings from the keyboard and save them as a JSON profile in the profiles/ directory.",
  211. Args: cobra.MaximumNArgs(1),
  212. RunE: func(cmd *cobra.Command, args []string) error {
  213. if len(args) == 0 {
  214. name = "default"
  215. } else {
  216. name = args[0]
  217. }
  218. return loadProfileFromDevice(name, cmd.ErrOrStderr())
  219. },
  220. }
  221. return cmd
  222. }
  223. func NewProfileLoadCmd() *cobra.Command {
  224. var name string
  225. cmd := &cobra.Command{
  226. Use: "load [name]",
  227. Short: "Load a profile and apply it to the keyboard",
  228. Long: "Read a JSON profile from the profiles/ directory and apply the saved RGB settings to the keyboard.",
  229. Args: cobra.MaximumNArgs(1),
  230. RunE: func(cmd *cobra.Command, args []string) error {
  231. if len(args) == 0 {
  232. name = "default"
  233. } else {
  234. name = args[0]
  235. }
  236. proto, target, selected, err := openTarget()
  237. if err != nil {
  238. return err
  239. }
  240. defer proto.Close()
  241. p, err := LoadProfile(name)
  242. if err != nil {
  243. return err
  244. }
  245. // A profile belongs to the keyboard it was saved from: its effect
  246. // names are that board's. Applying it elsewhere is allowed, because
  247. // the names that do not exist are reported per key below, but it is
  248. // said out loud, since a mismatch is the likeliest reason.
  249. if p.MatchesBoard(target.Device.VendorID, target.Device.ProductID) == boardOther {
  250. fmt.Fprintln(cmd.ErrOrStderr(), p.boardMismatchWarning(target.Device.VendorID, target.Device.ProductID))
  251. }
  252. catalog, _, err := resolveCatalog(target)
  253. if err != nil {
  254. return err
  255. }
  256. keys := make([]string, 0, len(p.Zones))
  257. for key := range p.Zones {
  258. keys = append(keys, key)
  259. }
  260. sort.Strings(keys)
  261. // A key is a channel name as the file wrote it. Resolving it here
  262. // means a profile written before a board was renamed reports the
  263. // key it cannot place instead of silently applying nothing.
  264. selectedSet := make(map[via.Channel]bool, len(selected))
  265. for _, ch := range selected {
  266. selectedSet[ch] = true
  267. }
  268. // Presence and selection are two different questions. A key the
  269. // selection leaves out is the user's own choice and stays quiet; a
  270. // key naming a channel the keyboard does not have has nowhere to go
  271. // and is reported, whatever the selection says.
  272. present, err := proto.DetectChannels()
  273. if err != nil {
  274. return err
  275. }
  276. presentSet := make(map[via.Channel]bool, len(present))
  277. for _, ch := range present {
  278. presentSet[ch] = true
  279. }
  280. // Without a catalog there are no effect names to look the
  281. // profile's value up in, so the whole load is impossible. Say that
  282. // once instead of reporting every key's name as not found.
  283. if catalog == nil {
  284. fmt.Fprintf(cmd.ErrOrStderr(),
  285. "Warning: profile %q has no effect catalog for this keyboard; nothing applied\n", name)
  286. return nil
  287. }
  288. applied := 0
  289. for _, key := range keys {
  290. settings := p.Zones[key]
  291. keyChannels, err := resolveZoneName(key, target.Display, target.Alternatives)
  292. if err != nil {
  293. fmt.Fprintf(cmd.ErrOrStderr(),
  294. "Warning: profile %q names zone %q, which this keyboard does not have; skipping\n", name, key)
  295. continue
  296. }
  297. placed := false
  298. for _, ch := range keyChannels {
  299. if presentSet[ch] {
  300. placed = true
  301. break
  302. }
  303. }
  304. if !placed {
  305. fmt.Fprintf(cmd.ErrOrStderr(),
  306. "Warning: profile %q names zone %q, which this keyboard does not have; skipping\n", name, key)
  307. continue
  308. }
  309. for _, ch := range keyChannels {
  310. // A key is applied only where the selection allows it, so
  311. // `--zone logo load <name>` leaves the other channels alone.
  312. if !selectedSet[ch] {
  313. continue
  314. }
  315. targets, _, err := intrgb.ResolveEffect(catalog, settings.Effect, []via.Channel{ch}, targetZone != "")
  316. if err != nil {
  317. fmt.Fprintf(cmd.ErrOrStderr(),
  318. "Warning: effect %q not found on %s, skipping\n", settings.Effect, channelName(ch, target.Display))
  319. continue
  320. }
  321. // ResolveEffect can answer with nothing to do and no
  322. // error: the name exists on the board but not on this
  323. // channel, and no zone was named, so it is skipped. Indexing
  324. // its result would crash here instead of saying so.
  325. if len(targets) == 0 {
  326. fmt.Fprintf(cmd.ErrOrStderr(),
  327. "Warning: effect %q not found on %s, skipping\n", settings.Effect, channelName(ch, target.Display))
  328. continue
  329. }
  330. if err := proto.SetValue(ch, uint8(intrgb.EffectID), targets[0].ID); err != nil {
  331. return err
  332. }
  333. if err := proto.SetValue(ch, uint8(intrgb.Brightness), settings.Brightness); err != nil {
  334. return err
  335. }
  336. if err := proto.SetValue(ch, uint8(intrgb.Speed), settings.Speed); err != nil {
  337. return err
  338. }
  339. if settings.Enabled && settings.Color != "" {
  340. hue, sat, err := hexToHSV(settings.Color)
  341. if err != nil {
  342. fmt.Fprintf(cmd.ErrOrStderr(),
  343. "Warning: invalid color %q on zone %s, skipping\n", settings.Color, channelName(ch, target.Display))
  344. } else if err := proto.SetColor(ch, hue, sat); err != nil {
  345. return err
  346. }
  347. }
  348. applied++
  349. }
  350. }
  351. if applied == 0 {
  352. fmt.Fprintf(cmd.ErrOrStderr(),
  353. "Warning: profile %q has no settings for the selected zone(s); nothing applied\n", name)
  354. }
  355. return nil
  356. },
  357. }
  358. return cmd
  359. }
  360. func NewProfileListCmd() *cobra.Command {
  361. return &cobra.Command{
  362. Use: "list",
  363. Short: "List saved profiles",
  364. Args: cobra.NoArgs,
  365. RunE: func(cmd *cobra.Command, args []string) error {
  366. names, err := ListProfiles()
  367. if err != nil {
  368. return err
  369. }
  370. if names == nil {
  371. names = []string{}
  372. }
  373. lines := make([]string, 0, len(names))
  374. for _, name := range names {
  375. p, err := LoadProfile(name)
  376. if err != nil {
  377. // A file that cannot be read is still a name in the
  378. // directory; the name is what the user can act on.
  379. lines = append(lines, name)
  380. continue
  381. }
  382. lines = append(lines, p.listLine())
  383. }
  384. if jsonOutput {
  385. return encodeJSON(cmd.OutOrStdout(), struct {
  386. Profiles []string `json:"profiles"`
  387. }{Profiles: names})
  388. }
  389. for _, line := range lines {
  390. fmt.Fprintln(cmd.OutOrStdout(), line)
  391. }
  392. return nil
  393. },
  394. }
  395. }
  396. func NewProfileDeleteCmd() *cobra.Command {
  397. var name string
  398. cmd := &cobra.Command{
  399. Use: "delete [name]",
  400. Short: "Delete a saved profile",
  401. Args: cobra.MaximumNArgs(1),
  402. RunE: func(cmd *cobra.Command, args []string) error {
  403. if len(args) == 0 {
  404. name = "default"
  405. } else {
  406. name = args[0]
  407. }
  408. return DeleteProfile(name)
  409. },
  410. }
  411. return cmd
  412. }
  413. func hexToHSV(s string) (uint8, uint8, error) {
  414. if len(s) != 4 {
  415. return 0, 0, fmt.Errorf("invalid HSV hex (expected 4 hex digits): %s", s)
  416. }
  417. var hue, sat uint8
  418. _, err := fmt.Sscanf(s, "%02x%02x", &hue, &sat)
  419. return hue, sat, err
  420. }