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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/system/input_device_settings.h"
#include <string>
#include <vector>
#include "base/bind.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/memory/ref_counted.h"
#include "base/message_loop/message_loop.h"
#include "base/process/kill.h"
#include "base/process/launch.h"
#include "base/process/process_handle.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/sys_info.h"
#include "base/task_runner.h"
#include "base/threading/sequenced_worker_pool.h"
#include "content/public/browser/browser_thread.h"
namespace chromeos {
namespace system {
namespace {
InputDeviceSettings* g_instance = nullptr;
InputDeviceSettings* g_test_instance = nullptr;
const char kDeviceTypeTouchpad[] = "touchpad";
const char kDeviceTypeMouse[] = "mouse";
const char kInputControl[] = "/opt/google/input/inputcontrol";
typedef base::RefCountedData<bool> RefCountedBool;
bool ScriptExists(const std::string& script) {
DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
return base::PathExists(base::FilePath(script));
}
// Executes the input control script asynchronously, if it exists.
void ExecuteScriptOnFileThread(const std::vector<std::string>& argv) {
DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
DCHECK(!argv.empty());
const std::string& script(argv[0]);
// Script must exist on device and is of correct format.
DCHECK(script.compare(kInputControl) == 0);
DCHECK(!base::SysInfo::IsRunningOnChromeOS() || ScriptExists(script));
if (!ScriptExists(script))
return;
base::ProcessHandle handle;
base::LaunchProcess(base::CommandLine(argv), base::LaunchOptions(), &handle);
base::EnsureProcessGetsReaped(handle);
}
void ExecuteScript(const std::vector<std::string>& argv) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
if (argv.size() == 1)
return;
VLOG(1) << "About to launch: \""
<< base::CommandLine(argv).GetCommandLineString() << "\"";
// Control scripts can take long enough to cause SIGART during shutdown
// (http://crbug.com/261426). Run the blocking pool task with
// CONTINUE_ON_SHUTDOWN so it won't be joined when Chrome shuts down.
base::SequencedWorkerPool* pool = content::BrowserThread::GetBlockingPool();
scoped_refptr<base::TaskRunner> runner =
pool->GetTaskRunnerWithShutdownBehavior(
base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN);
runner->PostTask(FROM_HERE, base::Bind(&ExecuteScriptOnFileThread, argv));
}
void AddSensitivityArguments(const char* device_type,
int value,
std::vector<std::string>* argv) {
DCHECK(value >= kMinPointerSensitivity && value <= kMaxPointerSensitivity);
argv->push_back(
base::StringPrintf("--%s_sensitivity=%d", device_type, value));
}
void AddTPControlArguments(const char* control,
bool enabled,
std::vector<std::string>* argv) {
argv->push_back(base::StringPrintf("--%s=%d", control, enabled ? 1 : 0));
}
void DeviceExistsBlockingPool(const char* device_type,
scoped_refptr<RefCountedBool> exists) {
DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
exists->data = false;
if (!ScriptExists(kInputControl))
return;
std::vector<std::string> argv;
argv.push_back(kInputControl);
argv.push_back(base::StringPrintf("--type=%s", device_type));
argv.push_back("--list");
std::string output;
// Output is empty if the device is not found.
exists->data =
base::GetAppOutput(base::CommandLine(argv), &output) && !output.empty();
DVLOG(1) << "DeviceExistsBlockingPool:" << device_type << "=" << exists->data;
}
void RunCallbackUIThread(
scoped_refptr<RefCountedBool> exists,
const InputDeviceSettings::DeviceExistsCallback& callback) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
DVLOG(1) << "RunCallbackUIThread " << exists->data;
callback.Run(exists->data);
}
void DeviceExists(const char* script,
const InputDeviceSettings::DeviceExistsCallback& callback) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
// One or both of the control scripts can apparently hang during shutdown
// (http://crbug.com/255546). Run the blocking pool task with
// CONTINUE_ON_SHUTDOWN so it won't be joined when Chrome shuts down.
scoped_refptr<RefCountedBool> exists(new RefCountedBool(false));
base::SequencedWorkerPool* pool = content::BrowserThread::GetBlockingPool();
scoped_refptr<base::TaskRunner> runner =
pool->GetTaskRunnerWithShutdownBehavior(
base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN);
runner->PostTaskAndReply(
FROM_HERE, base::Bind(&DeviceExistsBlockingPool, script, exists),
base::Bind(&RunCallbackUIThread, exists, callback));
}
// InputDeviceSettings for Linux with X11.
class InputDeviceSettingsImplX11 : public InputDeviceSettings {
public:
InputDeviceSettingsImplX11();
protected:
~InputDeviceSettingsImplX11() override {}
private:
// Overridden from InputDeviceSettings.
void TouchpadExists(const DeviceExistsCallback& callback) override;
void UpdateTouchpadSettings(const TouchpadSettings& settings) override;
void SetTouchpadSensitivity(int value) override;
void SetTapToClick(bool enabled) override;
void SetThreeFingerClick(bool enabled) override;
void SetTapDragging(bool enabled) override;
void SetNaturalScroll(bool enabled) override;
void MouseExists(const DeviceExistsCallback& callback) override;
void UpdateMouseSettings(const MouseSettings& settings) override;
void SetMouseSensitivity(int value) override;
void SetPrimaryButtonRight(bool right) override;
void ReapplyTouchpadSettings() override;
void ReapplyMouseSettings() override;
// Generate arguments for the inputcontrol script.
//
// |argv| is filled with arguments of script, that should be launched in order
// to apply update.
void GenerateTouchpadArguments(std::vector<std::string>* argv);
void GenerateMouseArguments(std::vector<std::string>* argv);
TouchpadSettings current_touchpad_settings_;
MouseSettings current_mouse_settings_;
DISALLOW_COPY_AND_ASSIGN(InputDeviceSettingsImplX11);
};
InputDeviceSettingsImplX11::InputDeviceSettingsImplX11() {
}
void InputDeviceSettingsImplX11::TouchpadExists(
const DeviceExistsCallback& callback) {
DeviceExists(kDeviceTypeTouchpad, callback);
}
void InputDeviceSettingsImplX11::UpdateTouchpadSettings(
const TouchpadSettings& settings) {
std::vector<std::string> argv;
if (current_touchpad_settings_.Update(settings)) {
GenerateTouchpadArguments(&argv);
ExecuteScript(argv);
}
}
void InputDeviceSettingsImplX11::SetTouchpadSensitivity(int value) {
TouchpadSettings settings;
settings.SetSensitivity(value);
UpdateTouchpadSettings(settings);
}
void InputDeviceSettingsImplX11::SetNaturalScroll(bool enabled) {
TouchpadSettings settings;
settings.SetNaturalScroll(enabled);
UpdateTouchpadSettings(settings);
}
void InputDeviceSettingsImplX11::SetTapToClick(bool enabled) {
TouchpadSettings settings;
settings.SetTapToClick(enabled);
UpdateTouchpadSettings(settings);
}
void InputDeviceSettingsImplX11::SetThreeFingerClick(bool enabled) {
// For Alex/ZGB.
TouchpadSettings settings;
settings.SetThreeFingerClick(enabled);
UpdateTouchpadSettings(settings);
}
void InputDeviceSettingsImplX11::SetTapDragging(bool enabled) {
TouchpadSettings settings;
settings.SetTapDragging(enabled);
UpdateTouchpadSettings(settings);
}
void InputDeviceSettingsImplX11::MouseExists(
const DeviceExistsCallback& callback) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
DeviceExists(kDeviceTypeMouse, callback);
}
void InputDeviceSettingsImplX11::UpdateMouseSettings(
const MouseSettings& update) {
std::vector<std::string> argv;
if (current_mouse_settings_.Update(update)) {
GenerateMouseArguments(&argv);
ExecuteScript(argv);
}
}
void InputDeviceSettingsImplX11::SetMouseSensitivity(int value) {
MouseSettings settings;
settings.SetSensitivity(value);
UpdateMouseSettings(settings);
}
void InputDeviceSettingsImplX11::SetPrimaryButtonRight(bool right) {
MouseSettings settings;
settings.SetPrimaryButtonRight(right);
UpdateMouseSettings(settings);
}
void InputDeviceSettingsImplX11::ReapplyTouchpadSettings() {
TouchpadSettings settings = current_touchpad_settings_;
current_touchpad_settings_ = TouchpadSettings();
UpdateTouchpadSettings(settings);
}
void InputDeviceSettingsImplX11::ReapplyMouseSettings() {
MouseSettings settings = current_mouse_settings_;
current_mouse_settings_ = MouseSettings();
UpdateMouseSettings(settings);
}
void InputDeviceSettingsImplX11::GenerateTouchpadArguments(
std::vector<std::string>* argv) {
argv->push_back(kInputControl);
if (current_touchpad_settings_.IsSensitivitySet()) {
AddSensitivityArguments(kDeviceTypeTouchpad,
current_touchpad_settings_.GetSensitivity(), argv);
}
if (current_touchpad_settings_.IsTapToClickSet()) {
AddTPControlArguments("tapclick",
current_touchpad_settings_.GetTapToClick(), argv);
}
if (current_touchpad_settings_.IsThreeFingerClickSet()) {
AddTPControlArguments("t5r2_three_finger_click",
current_touchpad_settings_.GetThreeFingerClick(),
argv);
}
if (current_touchpad_settings_.IsTapDraggingSet()) {
AddTPControlArguments("tapdrag",
current_touchpad_settings_.GetTapDragging(), argv);
}
if (current_touchpad_settings_.IsNaturalScrollSet()) {
AddTPControlArguments("australian_scrolling",
current_touchpad_settings_.GetNaturalScroll(), argv);
}
}
void InputDeviceSettingsImplX11::GenerateMouseArguments(
std::vector<std::string>* argv) {
argv->push_back(kInputControl);
if (current_mouse_settings_.IsSensitivitySet()) {
AddSensitivityArguments(kDeviceTypeMouse,
current_mouse_settings_.GetSensitivity(), argv);
}
if (current_mouse_settings_.IsPrimaryButtonRightSet()) {
AddTPControlArguments(
"mouse_swap_lr", current_mouse_settings_.GetPrimaryButtonRight(), argv);
}
}
} // namespace
// static
InputDeviceSettings* InputDeviceSettings::Get() {
if (g_test_instance)
return g_test_instance;
if (!g_instance)
g_instance = new InputDeviceSettingsImplX11;
return g_instance;
}
// static
void InputDeviceSettings::SetSettingsForTesting(
InputDeviceSettings* test_settings) {
if (g_test_instance == test_settings)
return;
delete g_test_instance;
g_test_instance = test_settings;
}
} // namespace system
} // namespace chromeos
|