summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/system/input_device_settings.cc
blob: f5a74fd4bdabed2635aa00ff6da339b0bc24e614 (plain)
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
// Copyright (c) 2012 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 <stdarg.h>
#include <string>
#include <vector>

#include "base/bind.h"
#include "base/chromeos/chromeos_version.h"
#include "base/command_line.h"
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/message_loop.h"
#include "base/process_util.h"
#include "base/stringprintf.h"
#include "base/threading/sequenced_worker_pool.h"
#include "content/public/browser/browser_thread.h"

namespace chromeos {
namespace system {

namespace {
const char kTpControl[] = "/opt/google/touchpad/tpcontrol";
const char kMouseControl[] = "/opt/google/mouse/mousecontrol";

bool ScriptExists(const std::string& script) {
  DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
  return file_util::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.
  DCHECK(!base::chromeos::IsRunningOnChromeOS() || ScriptExists(script));

  if (!ScriptExists(script))
    return;

  base::LaunchOptions options;
  options.wait = true;
  base::LaunchProcess(CommandLine(argv), options, NULL);
}

void ExecuteScript(int argc, ...) {
  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
  std::vector<std::string> argv;
  va_list vl;
  va_start(vl, argc);
  for (int i = 0; i < argc; ++i) {
    argv.push_back(va_arg(vl, const char*));
  }
  va_end(vl);

  content::BrowserThread::GetBlockingPool()->PostTask(FROM_HERE,
      base::Bind(&ExecuteScriptOnFileThread, argv));
}

void SetPointerSensitivity(const char* script, int value) {
  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
  DCHECK(value > 0 && value < 6);
  ExecuteScript(3, script, "sensitivity", StringPrintf("%d", value).c_str());
}

void SetTPControl(const char* control, bool enabled) {
  ExecuteScript(3, kTpControl, control, enabled ? "on" : "off");
}

void DeviceExistsBlockingPool(const char* script, bool* exists) {
  DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
  *exists = false;
  if (!ScriptExists(script))
    return;

  std::vector<std::string> argv;
  argv.push_back(script);
  argv.push_back("status");
  std::string output;
  // Output is empty if the device is not found.
  *exists = base::GetAppOutput(CommandLine(argv), &output) && !output.empty();
  DVLOG(1) << "DeviceExistsBlockingPool:" << script << "=" << *exists;
}

void RunCallbackUIThread(bool* exists, const DeviceExistsCallback& callback) {
  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
  DVLOG(1) << "RunCallbackUIThread " << *exists;
  callback.Run(*exists);
}

void DeviceExists(const char* script, const DeviceExistsCallback& callback) {
  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));

  bool* exists = new bool(false);
  content::BrowserThread::GetBlockingPool()->PostTaskAndReply(FROM_HERE,
      base::Bind(&DeviceExistsBlockingPool, script, exists),
      base::Bind(&RunCallbackUIThread, base::Owned(exists), callback));
}

}  // namespace

namespace touchpad_settings {

void TouchpadExists(const DeviceExistsCallback& callback) {
  DeviceExists(kTpControl, callback);
}

// Sets the touchpad sensitivity in the range [1, 5].
void SetSensitivity(int value) {
  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
  SetPointerSensitivity(kTpControl, value);
}

void SetTapToClick(bool enabled) {
  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
  SetTPControl("taptoclick", enabled);
}

void SetThreeFingerClick(bool enabled) {
  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));

  SetTPControl("three_finger_click", enabled);
  // For Alex/ZGB.
  SetTPControl("t5r2_three_finger_click", enabled);
}

void SetThreeFingerSwipe(bool enabled) {
  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
  SetTPControl("three_finger_swipe", enabled);
}

void SetTapDragging(bool enabled) {
  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
  SetTPControl("tap_dragging", enabled);
}

}  // namespace touchpad_settings

namespace mouse_settings {

void MouseExists(const DeviceExistsCallback& callback) {
  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
  DeviceExists(kMouseControl, callback);
}

// Sets the touchpad sensitivity in the range [1, 5].
void SetSensitivity(int value) {
  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
  SetPointerSensitivity(kMouseControl, value);
}

void SetPrimaryButtonRight(bool right) {
  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
  ExecuteScript(3, kMouseControl, "swap_left_right", right ? "1" : "0");
}

}  // namespace mouse_settings

}  // namespace system
}  // namespace chromeos