1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104

ash / rgb_keyboard / rgb_keyboard_manager.h [blame]

// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ASH_RGB_KEYBOARD_RGB_KEYBOARD_MANAGER_H_
#define ASH_RGB_KEYBOARD_RGB_KEYBOARD_MANAGER_H_

#include <stdint.h>

#include "ash/ash_export.h"
#include "ash/ime/ime_controller_impl.h"
#include "base/containers/flat_map.h"
#include "base/memory/raw_ptr.h"
#include "chromeos/ash/components/dbus/rgbkbd/rgbkbd_client.h"
#include "third_party/cros_system_api/dbus/rgbkbd/dbus-constants.h"
#include "third_party/skia/include/core/SkColor.h"

namespace ash {

class RgbKeyboardManagerObserver;

// RgbKeyboardManager is singleton class that provides clients access to
// RGB keyboard-related API's. Clients should interact with this class instead
// of the rgbkbd DBus client.
// This class is owned by ash/shell and should NOT be created by any other
// means.
class ASH_EXPORT RgbKeyboardManager : public ImeController::Observer,
                                      public RgbkbdClient::Observer {
 public:
  explicit RgbKeyboardManager(ImeControllerImpl* ime_controller);
  RgbKeyboardManager(const RgbKeyboardManager&) = delete;
  RgbKeyboardManager& operator=(const RgbKeyboardManager&) = delete;
  ~RgbKeyboardManager() override;

  rgbkbd::RgbKeyboardCapabilities GetRgbKeyboardCapabilities() const;
  int GetZoneCount();
  void SetStaticBackgroundColor(uint8_t r, uint8_t g, uint8_t b);
  void SetZoneColor(int zone, uint8_t r, uint8_t g, uint8_t b);
  void SetRainbowMode();
  void SetAnimationMode(rgbkbd::RgbAnimationMode mode);

  // RgbkbdClient::Observer:
  // Also used in tests to override the keyboard capability.
  void OnCapabilityUpdatedForTesting(
      rgbkbd::RgbKeyboardCapabilities capability) override;

  // Returns the global instance if initialized. May return null.
  static RgbKeyboardManager* Get();

  bool IsRgbKeyboardSupported() const {
    return capabilities_ != rgbkbd::RgbKeyboardCapabilities::kNone;
  }

  // Add and remove observers.
  void AddObserver(RgbKeyboardManagerObserver* observer);
  void RemoveObserver(RgbKeyboardManagerObserver* observer);

 private:
  friend class KeyboardBacklightColorControllerTest;

  // Enum to track the background mode sent to rgbkbd
  enum class BackgroundType {
    kNone,
    kStaticSingleColor,
    kStaticRainbow,
    kStaticZones,
  };

  // ImeController::Observer:
  void OnCapsLockChanged(bool enabled) override;
  void OnKeyboardLayoutNameChanged(const std::string&) override {}

  void FetchRgbKeyboardSupport();

  void OnGetRgbKeyboardCapabilities(
      std::optional<rgbkbd::RgbKeyboardCapabilities> reply);

  void InitializeRgbKeyboard();

  bool IsPerKeyKeyboard() const;

  rgbkbd::RgbKeyboardCapabilities capabilities_ =
      rgbkbd::RgbKeyboardCapabilities::kNone;

  raw_ptr<ImeControllerImpl> ime_controller_ptr_;

  // Tracks the currently set background color when `background_type_` is set to
  // `BackgroundType::kStaticSingleColor`.
  SkColor background_color_;
  // Tracks the currently set zone colors when `background_type_` is set to
  // `BackgroundType::kStaticZones`.
  base::flat_map<int, SkColor> zone_colors_;
  BackgroundType background_type_ = BackgroundType::kNone;

  base::ObserverList<RgbKeyboardManagerObserver> observers_;

  // Note: This should remain the last member so it'll be destroyed and
  // invalidate its weak pointers before any other members are destroyed.
  base::WeakPtrFactory<RgbKeyboardManager> weak_ptr_factory_{this};
};

}  // namespace ash

#endif  // ASH_RGB_KEYBOARD_RGB_KEYBOARD_MANAGER_H_