Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ui / events / ozone / evdev / libgestures_glue / gesture_feedback.cc
blobceedee3b2d77056ee1aaec84e6b7f5cbcce7bc75
1 // Copyright 2015 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.
5 #include "ui/events/ozone/evdev/libgestures_glue/gesture_feedback.h"
7 #include <time.h>
9 #include "base/bind.h"
10 #include "base/command_line.h"
11 #include "base/location.h"
12 #include "base/logging.h"
13 #include "base/process/launch.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/stringprintf.h"
17 #include "base/threading/worker_pool.h"
18 #include "ui/events/ozone/evdev/libgestures_glue/gesture_property_provider.h"
20 namespace ui {
22 namespace {
24 // Binary paths.
25 const char kGzipCommand[] = "/bin/gzip";
27 const size_t kTouchLogTimestampMaxSize = 80;
29 // Return the values in an array in one string. Used for touch logging.
30 template <typename T>
31 std::string DumpArrayProperty(const std::vector<T>& value, const char* format) {
32 std::string ret;
33 for (size_t i = 0; i < value.size(); ++i) {
34 if (i > 0)
35 ret.append(", ");
36 ret.append(base::StringPrintf(format, value[i]));
38 return ret;
41 // Return the values in a gesture property in one string. Used for touch
42 // logging.
43 std::string DumpGesturePropertyValue(GesturesProp* property) {
44 switch (property->type()) {
45 case GesturePropertyProvider::PT_INT:
46 return DumpArrayProperty(property->GetIntValue(), "%d");
47 break;
48 case GesturePropertyProvider::PT_SHORT:
49 return DumpArrayProperty(property->GetShortValue(), "%d");
50 break;
51 case GesturePropertyProvider::PT_BOOL:
52 return DumpArrayProperty(property->GetBoolValue(), "%d");
53 break;
54 case GesturePropertyProvider::PT_STRING:
55 return "\"" + property->GetStringValue() + "\"";
56 break;
57 case GesturePropertyProvider::PT_REAL:
58 return DumpArrayProperty(property->GetDoubleValue(), "%lf");
59 break;
60 default:
61 NOTREACHED();
62 break;
64 return std::string();
67 // Compress dumped event logs in place.
68 void CompressDumpedLog(scoped_ptr<std::vector<std::string>> log_paths) {
69 for (size_t i = 0; i < log_paths->size(); ++i) {
70 // Zip the file.
71 base::CommandLine command = base::CommandLine(base::FilePath(kGzipCommand));
72 command.AppendArg("-f");
73 command.AppendArg((*log_paths)[i]);
74 std::string output;
75 base::GetAppOutput(command, &output);
77 // Replace the original file with the zipped one.
78 base::Move(base::FilePath((*log_paths)[i] + ".gz"),
79 base::FilePath((*log_paths)[i]));
83 // Get the current time in a string.
84 std::string GetCurrentTimeForLogging() {
85 time_t rawtime;
86 struct tm timeinfo;
87 char buffer[kTouchLogTimestampMaxSize];
89 time(&rawtime);
90 if (!localtime_r(&rawtime, &timeinfo)) {
91 PLOG(ERROR) << "localtime_r failed";
92 return "";
94 if (!strftime(buffer, kTouchLogTimestampMaxSize, "%Y%m%d-%H%M%S", &timeinfo))
95 return "";
96 return std::string(buffer);
99 // Canonize the device name for logging.
100 std::string GetCanonicalDeviceName(const std::string& name) {
101 std::string ret(name);
102 for (size_t i = 0; i < ret.size(); ++i)
103 if (!base::IsAsciiAlpha(ret[i]))
104 ret[i] = '_';
105 return ret;
108 // Name event logs in a way that is compatible with existing toolchain.
109 std::string GenerateEventLogName(const base::FilePath& out_dir,
110 const std::string& prefix,
111 const std::string& now,
112 int id) {
113 return out_dir.value() + "/" + prefix + now + "." + base::IntToString(id);
115 // Name event logs in a way that is compatible with existing toolchain.
116 std::string GenerateEventLogName(GesturePropertyProvider* provider,
117 const base::FilePath& out_dir,
118 const std::string& prefix,
119 const std::string& now,
120 int id) {
121 return out_dir.value() + "/" + prefix + now + "." + base::IntToString(id) +
122 "." + GetCanonicalDeviceName(provider->GetDeviceNameById(id));
125 // Set the logging properties to dump event logs.
126 void StartToDumpEventLog(GesturePropertyProvider* provider,
127 const int device_id) {
128 // Dump gesture log.
129 GesturesProp* property = provider->GetProperty(device_id, "Log Path");
130 property->SetStringValue(kTouchpadGestureLogPath);
131 property = provider->GetProperty(device_id, "Logging Notify");
132 property->SetIntValue(std::vector<int>(1, 1));
134 // Dump evdev log.
135 property = provider->GetProperty(device_id, "Dump Debug Log");
136 property->SetBoolValue(std::vector<bool>(1, true));
139 } // namespace
141 // Dump touch device property values to a string.
142 void DumpTouchDeviceStatus(GesturePropertyProvider* provider,
143 std::string* status) {
144 // We use DT_ALL since we want gesture property values for all devices that
145 // run with the gesture library, not just mice or touchpads.
146 std::vector<int> ids;
147 provider->GetDeviceIdsByType(DT_ALL, &ids);
149 // Dump the property names and values for each device.
150 for (size_t i = 0; i < ids.size(); ++i) {
151 std::vector<std::string> names = provider->GetPropertyNamesById(ids[i]);
152 status->append("\n");
153 status->append(base::StringPrintf("ID %d:\n", ids[i]));
154 status->append(base::StringPrintf(
155 "Device \'%s\':\n", provider->GetDeviceNameById(ids[i]).c_str()));
157 // Note that, unlike X11, we don't maintain the "atom" concept here.
158 // Therefore, the property name indices we output here shouldn't be treated
159 // as unique identifiers of the properties.
160 std::sort(names.begin(), names.end());
161 for (size_t j = 0; j < names.size(); ++j) {
162 status->append(base::StringPrintf("\t%s (%zu):", names[j].c_str(), j));
163 GesturesProp* property = provider->GetProperty(ids[i], names[j]);
164 status->append("\t" + DumpGesturePropertyValue(property) + '\n');
169 // Dump touch event logs.
170 void DumpTouchEventLog(
171 std::map<base::FilePath, EventConverterEvdev*>& converters,
172 GesturePropertyProvider* provider,
173 const base::FilePath& out_dir,
174 scoped_ptr<std::vector<base::FilePath>> log_paths,
175 const GetTouchEventLogReply& reply) {
176 // Get device ids.
177 std::vector<int> ids;
178 provider->GetDeviceIdsByType(DT_ALL, &ids);
180 // Get current time stamp.
181 std::string now = GetCurrentTimeForLogging();
183 // Dump event logs for gesture devices.
184 scoped_ptr<std::vector<std::string>> log_paths_to_be_compressed(
185 new std::vector<std::string>);
186 for (size_t i = 0; i < ids.size(); ++i) {
187 // First, see if the device actually uses the gesture library by checking
188 // if it has any gesture property.
189 std::vector<std::string> names = provider->GetPropertyNamesById(ids[i]);
190 if (names.size() == 0)
191 continue;
193 // Set the logging properties to dump event logs. This needs to be done
194 // synchronously for now or we might have race conditions on the debug
195 // buffer. If the performance becomes a concern then, we can fork and
196 // synchronize it.
198 // TODO(sheckylin): Make sure this has no performance impact for user
199 // feedbacks.
200 StartToDumpEventLog(provider, ids[i]);
202 // Rename/move the file to another place since each device's log is
203 // always dumped using the same name.
204 std::string gesture_log_filename = GenerateEventLogName(
205 provider, out_dir, "touchpad_activity_", now, ids[i]);
206 base::Move(base::FilePath(kTouchpadGestureLogPath),
207 base::FilePath(gesture_log_filename));
208 std::string evdev_log_filename = GenerateEventLogName(
209 provider, out_dir, "cmt_input_events_", now, ids[i]);
210 base::Move(base::FilePath(kTouchpadEvdevLogPath),
211 base::FilePath(evdev_log_filename));
213 // Historically, we compress touchpad/mouse logs with gzip before tarring
214 // them up. We DONT compress touchscreen logs though.
215 log_paths_to_be_compressed->push_back(gesture_log_filename);
216 log_paths->push_back(base::FilePath(gesture_log_filename));
217 log_paths_to_be_compressed->push_back(evdev_log_filename);
218 log_paths->push_back(base::FilePath(evdev_log_filename));
221 for (auto it = converters.begin(); it != converters.end(); ++it) {
222 EventConverterEvdev* converter = it->second;
223 if (converter->HasTouchscreen()) {
224 converter->DumpTouchEventLog(kInputEventsLogFile);
225 std::string touch_evdev_log_filename = GenerateEventLogName(
226 out_dir, "evdev_input_events_", now, converter->id());
227 base::Move(base::FilePath(kInputEventsLogFile),
228 base::FilePath(touch_evdev_log_filename));
229 log_paths->push_back(base::FilePath(touch_evdev_log_filename));
233 // Compress touchpad/mouse logs on another thread and return.
234 base::WorkerPool::PostTaskAndReply(
235 FROM_HERE,
236 base::Bind(&CompressDumpedLog, base::Passed(&log_paths_to_be_compressed)),
237 base::Bind(reply, base::Passed(&log_paths)), true /* task_is_slow */);
240 } // namespace ui