OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/system/input_device_settings.h" | 5 #include "chrome/browser/chromeos/system/input_device_settings.h" |
6 | 6 |
7 #include <stdarg.h> | |
7 #include <string> | 8 #include <string> |
8 #include <vector> | 9 #include <vector> |
9 | 10 |
10 #include "base/bind.h" | 11 #include "base/bind.h" |
11 #include "base/command_line.h" | 12 #include "base/command_line.h" |
12 #include "base/file_path.h" | 13 #include "base/file_path.h" |
13 #include "base/file_util.h" | 14 #include "base/file_util.h" |
14 #include "base/message_loop.h" | 15 #include "base/message_loop.h" |
15 #include "base/process_util.h" | 16 #include "base/process_util.h" |
16 #include "base/stringprintf.h" | 17 #include "base/stringprintf.h" |
17 #include "chrome/browser/chromeos/system/runtime_environment.h" | 18 #include "chrome/browser/chromeos/system/runtime_environment.h" |
18 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
19 | 20 |
20 using content::BrowserThread; | 21 using content::BrowserThread; |
21 | 22 |
22 namespace chromeos { | 23 namespace chromeos { |
23 namespace system { | 24 namespace system { |
24 | 25 |
25 namespace touchpad_settings { | |
26 namespace { | 26 namespace { |
27 const char* kTpControl = "/opt/google/touchpad/tpcontrol"; | 27 const char* kTpControl = "/opt/google/touchpad/tpcontrol"; |
28 const char* kMouseControl = "/opt/google/mouse/mousecontrol"; | |
xiyuan
2012/01/19 20:49:40
nit: think the preferred way to define string cons
achuithb
2012/01/19 22:19:04
Done.
| |
28 | 29 |
29 bool TPCtrlExists() { | 30 bool ScriptExists(const std::string& script) { |
30 return file_util::PathExists(FilePath(kTpControl)); | 31 return file_util::PathExists(FilePath(script)); |
31 } | 32 } |
32 | 33 |
33 // Launches the tpcontrol command asynchronously, if it exists. | 34 // Executes the input control script asynchronously, if it exists. |
34 void LaunchTpControl(const std::vector<std::string>& argv) { | 35 void ExecuteScriptOnFileThread(const std::vector<std::string>& argv) { |
35 if (!TPCtrlExists()) | 36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
37 DCHECK(!argv.empty()); | |
38 const std::string& script(argv[0]); | |
39 | |
40 // Script must exist on device. | |
41 DCHECK(!runtime_environment::IsRunningOnChromeOS() || ScriptExists(script)); | |
42 | |
43 if (!ScriptExists(script)) | |
36 return; | 44 return; |
37 | 45 |
38 base::LaunchOptions options; | 46 base::LaunchOptions options; |
39 options.wait = true; | 47 options.wait = true; |
40 base::LaunchProcess(CommandLine(argv), options, NULL); | 48 base::LaunchProcess(CommandLine(argv), options, NULL); |
41 } | 49 } |
42 | 50 |
51 void ExecuteScript(int argc, ...) { | |
52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
53 std::vector<std::string> argv; | |
54 va_list vl; | |
55 va_start(vl, argc); | |
56 for (int i = 0; i < argc; ++i) { | |
57 argv.push_back(va_arg(vl, const char*)); | |
58 } | |
59 va_end(vl); | |
60 | |
61 BrowserThread::PostTask( | |
62 BrowserThread::FILE, FROM_HERE, | |
63 base::Bind(&ExecuteScriptOnFileThread, argv)); | |
64 } | |
65 | |
66 void SetPointerSensitivity(const char* script, int value) { | |
67 DCHECK(value > 0 && value < 6); | |
68 ExecuteScript(3, script, "sensitivity", StringPrintf("%d", value).c_str()); | |
69 } | |
70 | |
71 bool DeviceExists(const char* script) { | |
72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
73 if (!ScriptExists(script)) | |
74 return false; | |
75 | |
76 std::vector<std::string> argv; | |
77 argv.push_back(script); | |
78 argv.push_back("status"); | |
79 std::string output; | |
80 // Output is empty if the device is not found. | |
81 return base::GetAppOutput(CommandLine(argv), &output) && !output.empty(); | |
82 } | |
83 | |
43 } // namespace | 84 } // namespace |
44 | 85 |
86 namespace pointer_settings { | |
87 | |
88 void SetSensitivity(int value) { | |
89 SetPointerSensitivity(kTpControl, value); | |
90 SetPointerSensitivity(kMouseControl, value); | |
91 } | |
92 | |
93 } // namespace pointer_settings | |
94 | |
95 namespace touchpad_settings { | |
96 | |
45 bool TouchpadExists() { | 97 bool TouchpadExists() { |
98 // We only need to do this check once, assuming no pluggable touchpad devices. | |
46 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 99 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
47 static bool init = false; | 100 static bool init = false; |
48 static bool exists = false; | 101 static bool exists = false; |
49 | 102 |
50 if (init) | 103 if (!init) { |
51 return exists; | 104 init = true; |
52 | 105 exists = DeviceExists(kTpControl); |
53 init = true; | 106 } |
54 if (!TPCtrlExists()) | |
55 return exists; | |
56 | |
57 std::vector<std::string> argv; | |
58 argv.push_back(kTpControl); | |
59 argv.push_back("status"); | |
60 std::string output; | |
61 // On devices with no touchpad, output is empty. | |
62 exists = base::GetAppOutput(CommandLine(argv), &output) && !output.empty(); | |
63 return exists; | 107 return exists; |
64 } | 108 } |
65 | 109 |
66 void SetSensitivity(int value) { | |
67 // Run this on the FILE thread. | |
68 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { | |
69 BrowserThread::PostTask( | |
70 BrowserThread::FILE, FROM_HERE, | |
71 base::Bind(&SetSensitivity, value)); | |
72 return; | |
73 } | |
74 | |
75 std::vector<std::string> argv; | |
76 argv.push_back(kTpControl); | |
77 argv.push_back("sensitivity"); | |
78 argv.push_back(StringPrintf("%d", value)); | |
79 | |
80 LaunchTpControl(argv); | |
81 } | |
82 | |
83 void SetTapToClick(bool enabled) { | 110 void SetTapToClick(bool enabled) { |
84 // Run this on the FILE thread. | 111 ExecuteScript(3, kTpControl, "taptoclick", enabled ? "on" : "off"); |
85 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { | |
86 BrowserThread::PostTask( | |
87 BrowserThread::FILE, FROM_HERE, | |
88 base::Bind(&SetTapToClick, enabled)); | |
89 return; | |
90 } | |
91 | |
92 std::vector<std::string> argv; | |
93 argv.push_back(kTpControl); | |
94 argv.push_back("taptoclick"); | |
95 argv.push_back(enabled ? "on" : "off"); | |
96 | |
97 LaunchTpControl(argv); | |
98 } | 112 } |
99 | 113 |
100 } // namespace touchpad_settings | 114 } // namespace touchpad_settings |
101 | 115 |
102 namespace mouse_settings { | 116 namespace mouse_settings { |
103 | 117 |
104 bool MouseExists() { | 118 bool MouseExists() { |
105 // TODO(achuith, adlr): Call mouse_ctrl when it exists. | 119 return DeviceExists(kMouseControl); |
106 return false; | |
107 } | 120 } |
108 | 121 |
109 void SetPrimaryButtonRight(bool right) { | 122 void SetPrimaryButtonRight(bool right) { |
110 // TODO(achuith, adlr): Call mouse_ctrl when it exists. | 123 ExecuteScript(3, kMouseControl, "swap_left_right", right ? "1" : "0"); |
111 } | 124 } |
112 | 125 |
113 } // namespace mouse_settings | 126 } // namespace mouse_settings |
114 | 127 |
115 } // namespace system | 128 } // namespace system |
116 } // namespace chromeos | 129 } // namespace chromeos |
OLD | NEW |