1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROME_BROWSER_CHROMEOS_SYSTEM_INPUT_DEVICE_SETTINGS_H_
6#define CHROME_BROWSER_CHROMEOS_SYSTEM_INPUT_DEVICE_SETTINGS_H_
7
8#include <string>
9#include <vector>
10
11#include "base/callback.h"
12#include "base/logging.h"
13
14namespace chromeos {
15namespace system {
16
17namespace internal {
18
19// Objects of this class are intended to store values of type T, but might have
20// "unset" state. Object will be in "unset" state until Set is called first
21// time.
22template <typename T>
23class Optional {
24 public:
25  Optional()
26    : value_(),
27      is_set_(false) {
28  }
29
30  Optional& operator=(const Optional& other) {
31    if (&other != this) {
32      value_ = other.value_;
33      is_set_ = other.is_set_;
34    }
35    return *this;
36  }
37
38  void Set(const T& value) {
39    is_set_ = true;
40    value_ = value;
41  }
42
43  bool is_set() const {
44    return is_set_;
45  }
46
47  T value() const {
48    DCHECK(is_set());
49    return value_;
50  }
51
52  // Tries to update |this| with |update|. If |update| is unset or has same
53  // value as |this| method returns false. Otherwise |this| takes value of
54  // |update| and returns true.
55  bool Update(const Optional& update) {
56    if (update.is_set_ && (!is_set_ || value_ != update.value_)) {
57      value_ = update.value_;
58      is_set_ = true;
59      return true;
60    }
61    return false;
62  }
63
64 private:
65  T value_;
66  bool is_set_;
67};
68
69}  // namespace internal
70
71// Min/max possible pointer sensitivity values. Defined in CrOS inputcontrol
72// scripts (see kTpControl/kMouseControl in the source file).
73const int kMinPointerSensitivity = 1;
74const int kMaxPointerSensitivity = 5;
75
76// Auxiliary class used to update several touchpad settings at a time. User
77// should set any number of settings and pass object to UpdateTouchpadSettings
78// method of InputDeviceSettings.
79// Objects of this class have no default values for settings, so it is error
80// to call Get* method before calling corresponding Set* method at least
81// once.
82class TouchpadSettings {
83 public:
84  TouchpadSettings();
85  TouchpadSettings& operator=(const TouchpadSettings& other);
86
87  void SetSensitivity(int value);
88  int GetSensitivity() const;
89
90  void SetTapToClick(bool enabled);
91  bool GetTapToClick() const;
92
93  void SetThreeFingerClick(bool enabled);
94  bool GetThreeFingerClick() const;
95
96  void SetTapDragging(bool enabled);
97  bool GetTapDragging() const;
98
99  void SetNaturalScroll(bool enabled);
100  bool GetNaturalScroll() const;
101
102  // Updates |this| with |settings|. If at least one setting was updated returns
103  // true.
104  // |argv| is filled with arguments of script, that should be launched in order
105  // to apply update. This argument is optional and could be NULL.
106  bool Update(const TouchpadSettings& settings, std::vector<std::string>* argv);
107
108 private:
109  internal::Optional<int> sensitivity_;
110  internal::Optional<bool> tap_to_click_;
111  internal::Optional<bool> three_finger_click_;
112  internal::Optional<bool> tap_dragging_;
113  internal::Optional<bool> natural_scroll_;
114};
115
116// Auxiliary class used to update several mouse settings at a time. User
117// should set any number of settings and pass object to UpdateMouseSettings
118// method of InputDeviceSettings.
119// Objects of this class have no default values for settings, so it is error
120// to call Get* method before calling corresponding Set* method at least
121// once.
122class MouseSettings {
123 public:
124  MouseSettings();
125  MouseSettings& operator=(const MouseSettings& other);
126
127  void SetSensitivity(int value);
128  int GetSensitivity() const;
129
130  void SetPrimaryButtonRight(bool right);
131  bool GetPrimaryButtonRight() const;
132
133  // Updates |this| with |settings|. If at least one setting was updated returns
134  // true.
135  // |argv| is filled with arguments of script, that should be launched in order
136  // to apply update. This argument is optional and could be NULL.
137  bool Update(const MouseSettings& update, std::vector<std::string>* argv);
138
139 private:
140  internal::Optional<int> sensitivity_;
141  internal::Optional<bool> primary_button_right_;
142};
143
144class InputDeviceSettings {
145 public:
146  typedef base::Callback<void(bool)> DeviceExistsCallback;
147
148  virtual ~InputDeviceSettings() {}
149
150  // Returns current instance of InputDeviceSettings.
151  static InputDeviceSettings* Get();
152
153  // Replaces current instance with |test_settings|. Takes ownership of
154  // |test_settings|. Default implementation could be returned back by passing
155  // NULL to this method.
156  static void SetSettingsForTesting(InputDeviceSettings* test_settings);
157
158  // Calls |callback| asynchronously after determining if a touchpad is
159  // connected.
160  virtual void TouchpadExists(const DeviceExistsCallback& callback) = 0;
161
162  // Updates several touchpad settings at a time. Updates only settings that
163  // are set in |settings| object. It is more efficient to use this method to
164  // update several settings then calling Set* methods one by one.
165  virtual void UpdateTouchpadSettings(const TouchpadSettings& settings) = 0;
166
167  // Sets the touchpad sensitivity in the range [kMinPointerSensitivity,
168  // kMaxPointerSensitivity].
169  virtual void SetTouchpadSensitivity(int value) = 0;
170
171  // Turns tap to click on/off.
172  virtual void SetTapToClick(bool enabled) = 0;
173
174  // Switch for three-finger click.
175  virtual void SetThreeFingerClick(bool enabled) = 0;
176
177  // Turns tap-dragging on/off.
178  virtual void SetTapDragging(bool enabled) = 0;
179
180  // Turns natural scrolling on/off for all devices except wheel mice
181  virtual void SetNaturalScroll(bool enabled) = 0;
182
183  // Calls |callback| asynchronously after determining if a mouse is connected.
184  virtual void MouseExists(const DeviceExistsCallback& callback) = 0;
185
186  // Updates several mouse settings at a time. Updates only settings that
187  // are set in |settings| object. It is more efficient to use this method to
188  // update several settings then calling Set* methods one by one.
189  virtual void UpdateMouseSettings(const MouseSettings& settings) = 0;
190
191  // Sets the mouse sensitivity in the range [kMinPointerSensitivity,
192  // kMaxPointerSensitivity].
193  virtual void SetMouseSensitivity(int value) = 0;
194
195  // Sets the primary mouse button to the right button if |right| is true.
196  virtual void SetPrimaryButtonRight(bool right) = 0;
197
198  // Returns true if UI should implement enhanced keyboard support for cases
199  // where other input devices like mouse are absent.
200  virtual bool ForceKeyboardDrivenUINavigation() = 0;
201
202  // Reapplies previously set touchpad settings.
203  virtual void ReapplyTouchpadSettings() = 0;
204
205  // Reapplies previously set mouse settings.
206  virtual void ReapplyMouseSettings() = 0;
207};
208
209}  // namespace system
210}  // namespace chromeos
211
212#endif  // CHROME_BROWSER_CHROMEOS_SYSTEM_INPUT_DEVICE_SETTINGS_H_
213