OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "views/touchui/touch_factory.h" | |
6 | |
7 #if defined(TOOLKIT_USES_GTK) | |
8 // TODO(sad) Remove all TOOLKIT_USES_GTK uses once we move to aura only. | |
9 #include <gtk/gtk.h> | |
10 #include <gdk/gdkx.h> | |
11 #endif | |
12 #include <X11/cursorfont.h> | |
13 #include <X11/extensions/XInput.h> | |
14 #include <X11/extensions/XInput2.h> | |
15 #include <X11/extensions/XIproto.h> | |
16 | |
17 #include "base/basictypes.h" | |
18 #include "base/compiler_specific.h" | |
19 #include "base/logging.h" | |
20 #include "base/message_loop.h" | |
21 #include "ui/base/x/x11_util.h" | |
22 | |
23 namespace { | |
24 | |
25 // The X cursor is hidden if it is idle for kCursorIdleSeconds seconds. | |
26 int kCursorIdleSeconds = 5; | |
27 | |
28 // Given the TouchParam, return the correspoding XIValuatorClassInfo using | |
29 // the X device information through Atom name matching. | |
30 XIValuatorClassInfo* FindTPValuator(Display* display, | |
31 XIDeviceInfo* info, | |
32 views::TouchFactory::TouchParam tp) { | |
33 // Lookup table for mapping TouchParam to Atom string used in X. | |
34 // A full set of Atom strings can be found at xserver-properties.h. | |
35 // For Slot ID, See this chromeos revision: http://git.chromium.org/gitweb/? | |
36 // p=chromiumos/overlays/chromiumos-overlay.git; | |
37 // a=commit;h=9164d0a75e48c4867e4ef4ab51f743ae231c059a | |
38 static struct { | |
39 views::TouchFactory::TouchParam tp; | |
40 const char* atom; | |
41 } kTouchParamAtom[] = { | |
42 { views::TouchFactory::TP_TOUCH_MAJOR, "Abs MT Touch Major" }, | |
43 { views::TouchFactory::TP_TOUCH_MINOR, "Abs MT Touch Minor" }, | |
44 { views::TouchFactory::TP_ORIENTATION, "Abs MT Orientation" }, | |
45 { views::TouchFactory::TP_PRESSURE, "Abs MT Pressure" }, | |
46 { views::TouchFactory::TP_SLOT_ID, "Abs MT Slot ID" }, | |
47 { views::TouchFactory::TP_TRACKING_ID, "Abs MT Tracking ID" }, | |
48 { views::TouchFactory::TP_LAST_ENTRY, NULL }, | |
49 }; | |
50 | |
51 const char* atom_tp = NULL; | |
52 | |
53 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTouchParamAtom); i++) { | |
54 if (tp == kTouchParamAtom[i].tp) { | |
55 atom_tp = kTouchParamAtom[i].atom; | |
56 break; | |
57 } | |
58 } | |
59 | |
60 if (!atom_tp) | |
61 return NULL; | |
62 | |
63 for (int i = 0; i < info->num_classes; i++) { | |
64 if (info->classes[i]->type != XIValuatorClass) | |
65 continue; | |
66 XIValuatorClassInfo* v = | |
67 reinterpret_cast<XIValuatorClassInfo*>(info->classes[i]); | |
68 | |
69 const char* atom = XGetAtomName(display, v->label); | |
70 if (atom && strcmp(atom, atom_tp) == 0) | |
71 return v; | |
72 } | |
73 | |
74 return NULL; | |
75 } | |
76 | |
77 #if defined(TOOLKIT_USES_GTK) | |
78 // Setup XInput2 select for the GtkWidget. | |
79 gboolean GtkWidgetRealizeCallback(GSignalInvocationHint* hint, guint nparams, | |
80 const GValue* pvalues, gpointer data) { | |
81 GtkWidget* widget = GTK_WIDGET(g_value_get_object(pvalues)); | |
82 GdkWindow* window = widget->window; | |
83 views::TouchFactory* factory = static_cast<views::TouchFactory*>(data); | |
84 | |
85 if (GDK_WINDOW_TYPE(window) != GDK_WINDOW_TOPLEVEL && | |
86 GDK_WINDOW_TYPE(window) != GDK_WINDOW_CHILD && | |
87 GDK_WINDOW_TYPE(window) != GDK_WINDOW_DIALOG) | |
88 return true; | |
89 | |
90 factory->SetupXI2ForXWindow(GDK_WINDOW_XID(window)); | |
91 return true; | |
92 } | |
93 | |
94 // We need to capture all the GDK windows that get created, and start | |
95 // listening for XInput2 events. So we setup a callback to the 'realize' | |
96 // signal for GTK+ widgets, so that whenever the signal triggers for any | |
97 // GtkWidget, which means the GtkWidget should now have a GdkWindow, we can | |
98 // setup XInput2 events for the GdkWindow. | |
99 guint realize_signal_id = 0; | |
100 guint realize_hook_id = 0; | |
101 | |
102 void SetupGtkWidgetRealizeNotifier(views::TouchFactory* factory) { | |
103 gpointer klass = g_type_class_ref(GTK_TYPE_WIDGET); | |
104 | |
105 g_signal_parse_name("realize", GTK_TYPE_WIDGET, | |
106 &realize_signal_id, NULL, FALSE); | |
107 realize_hook_id = g_signal_add_emission_hook(realize_signal_id, 0, | |
108 GtkWidgetRealizeCallback, static_cast<gpointer>(factory), NULL); | |
109 | |
110 g_type_class_unref(klass); | |
111 } | |
112 | |
113 void RemoveGtkWidgetRealizeNotifier() { | |
114 if (realize_signal_id != 0) | |
115 g_signal_remove_emission_hook(realize_signal_id, realize_hook_id); | |
116 realize_signal_id = 0; | |
117 realize_hook_id = 0; | |
118 } | |
119 #endif | |
120 | |
121 } // namespace | |
122 | |
123 namespace views { | |
124 | |
125 // static | |
126 TouchFactory* TouchFactory::GetInstance() { | |
127 return Singleton<TouchFactory>::get(); | |
128 } | |
129 | |
130 TouchFactory::TouchFactory() | |
131 : is_cursor_visible_(true), | |
132 keep_mouse_cursor_(false), | |
133 cursor_timer_(), | |
134 pointer_device_lookup_(), | |
135 touch_device_list_(), | |
136 slots_used_() { | |
137 #if defined(TOUCH_UI) | |
138 if (!base::MessagePumpForUI::HasXInput2()) | |
139 return; | |
140 #endif | |
141 | |
142 char nodata[] = { 0, 0, 0, 0, 0, 0, 0, 0 }; | |
143 XColor black; | |
144 black.red = black.green = black.blue = 0; | |
145 Display* display = ui::GetXDisplay(); | |
146 Pixmap blank = XCreateBitmapFromData(display, ui::GetX11RootWindow(), | |
147 nodata, 8, 8); | |
148 invisible_cursor_ = XCreatePixmapCursor(display, blank, blank, | |
149 &black, &black, 0, 0); | |
150 arrow_cursor_ = XCreateFontCursor(display, XC_arrow); | |
151 | |
152 SetCursorVisible(false, false); | |
153 UpdateDeviceList(display); | |
154 | |
155 #if defined(TOOLKIT_USES_GTK) | |
156 // TODO(sad): Here, we only setup so that the X windows created by GTK+ are | |
157 // setup for XInput2 events. We need a way to listen for XInput2 events for X | |
158 // windows created by other means (e.g. for context menus). | |
159 SetupGtkWidgetRealizeNotifier(this); | |
160 #endif | |
161 // Make sure the list of devices is kept up-to-date by listening for | |
162 // XI_HierarchyChanged event on the root window. | |
163 unsigned char mask[XIMaskLen(XI_LASTEVENT)]; | |
164 memset(mask, 0, sizeof(mask)); | |
165 | |
166 XISetMask(mask, XI_HierarchyChanged); | |
167 | |
168 XIEventMask evmask; | |
169 evmask.deviceid = XIAllDevices; | |
170 evmask.mask_len = sizeof(mask); | |
171 evmask.mask = mask; | |
172 XISelectEvents(display, ui::GetX11RootWindow(), &evmask, 1); | |
173 } | |
174 | |
175 TouchFactory::~TouchFactory() { | |
176 #if defined(TOUCH_UI) | |
177 if (!base::MessagePumpForUI::HasXInput2()) | |
178 return; | |
179 #endif | |
180 | |
181 SetCursorVisible(true, false); | |
182 Display* display = ui::GetXDisplay(); | |
183 XFreeCursor(display, invisible_cursor_); | |
184 XFreeCursor(display, arrow_cursor_); | |
185 | |
186 #if defined(TOOLKIT_USES_GTK) | |
187 RemoveGtkWidgetRealizeNotifier(); | |
188 #endif | |
189 } | |
190 | |
191 void TouchFactory::UpdateDeviceList(Display* display) { | |
192 // Detect touch devices. | |
193 // NOTE: The new API for retrieving the list of devices (XIQueryDevice) does | |
194 // not provide enough information to detect a touch device. As a result, the | |
195 // old version of query function (XListInputDevices) is used instead. | |
196 // If XInput2 is not supported, this will return null (with count of -1) so | |
197 // we assume there cannot be any touch devices. | |
198 int count = 0; | |
199 touch_device_lookup_.reset(); | |
200 touch_device_list_.clear(); | |
201 XDeviceInfo* devlist = XListInputDevices(display, &count); | |
202 for (int i = 0; i < count; i++) { | |
203 if (devlist[i].type) { | |
204 const char* devtype = XGetAtomName(display, devlist[i].type); | |
205 if (devtype && !strcmp(devtype, XI_TOUCHSCREEN)) { | |
206 touch_device_lookup_[devlist[i].id] = true; | |
207 touch_device_list_.push_back(devlist[i].id); | |
208 } | |
209 } | |
210 } | |
211 if (devlist) | |
212 XFreeDeviceList(devlist); | |
213 | |
214 // Instead of asking X for the list of devices all the time, let's maintain a | |
215 // list of pointer devices we care about. | |
216 // It should not be necessary to select for slave devices. XInput2 provides | |
217 // enough information to the event callback to decide which slave device | |
218 // triggered the event, thus decide whether the 'pointer event' is a | |
219 // 'mouse event' or a 'touch event'. | |
220 // However, on some desktops, some events from a master pointer are | |
221 // not delivered to the client. So we select for slave devices instead. | |
222 // If the touch device has 'GrabDevice' set and 'SendCoreEvents' unset (which | |
223 // is possible), then the device is detected as a floating device, and a | |
224 // floating device is not connected to a master device. So it is necessary to | |
225 // also select on the floating devices. | |
226 pointer_device_lookup_.reset(); | |
227 XIDeviceInfo* devices = XIQueryDevice(display, XIAllDevices, &count); | |
228 for (int i = 0; i < count; i++) { | |
229 XIDeviceInfo* devinfo = devices + i; | |
230 if (devinfo->use == XIFloatingSlave || devinfo->use == XISlavePointer) { | |
231 pointer_device_lookup_[devinfo->deviceid] = true; | |
232 } | |
233 } | |
234 if (devices) | |
235 XIFreeDeviceInfo(devices); | |
236 | |
237 SetupValuator(); | |
238 } | |
239 | |
240 bool TouchFactory::ShouldProcessXI2Event(XEvent* xev) { | |
241 DCHECK_EQ(GenericEvent, xev->type); | |
242 | |
243 XGenericEventCookie* cookie = &xev->xcookie; | |
244 if (cookie->evtype != XI_ButtonPress && | |
245 cookie->evtype != XI_ButtonRelease && | |
246 cookie->evtype != XI_Motion) | |
247 return true; | |
248 | |
249 XIDeviceEvent* xiev = static_cast<XIDeviceEvent*>(cookie->data); | |
250 return pointer_device_lookup_[xiev->deviceid]; | |
251 } | |
252 | |
253 void TouchFactory::SetupXI2ForXWindow(Window window) { | |
254 // Setup mask for mouse events. It is possible that a device is loaded/plugged | |
255 // in after we have setup XInput2 on a window. In such cases, we need to | |
256 // either resetup XInput2 for the window, so that we get events from the new | |
257 // device, or we need to listen to events from all devices, and then filter | |
258 // the events from uninteresting devices. We do the latter because that's | |
259 // simpler. | |
260 | |
261 Display* display = ui::GetXDisplay(); | |
262 | |
263 unsigned char mask[XIMaskLen(XI_LASTEVENT)]; | |
264 memset(mask, 0, sizeof(mask)); | |
265 | |
266 XISetMask(mask, XI_ButtonPress); | |
267 XISetMask(mask, XI_ButtonRelease); | |
268 XISetMask(mask, XI_Motion); | |
269 | |
270 XIEventMask evmask; | |
271 evmask.deviceid = XIAllDevices; | |
272 evmask.mask_len = sizeof(mask); | |
273 evmask.mask = mask; | |
274 XISelectEvents(display, window, &evmask, 1); | |
275 XFlush(display); | |
276 } | |
277 | |
278 void TouchFactory::SetTouchDeviceList( | |
279 const std::vector<unsigned int>& devices) { | |
280 touch_device_lookup_.reset(); | |
281 touch_device_list_.clear(); | |
282 for (std::vector<unsigned int>::const_iterator iter = devices.begin(); | |
283 iter != devices.end(); ++iter) { | |
284 DCHECK(*iter < touch_device_lookup_.size()); | |
285 touch_device_lookup_[*iter] = true; | |
286 touch_device_list_.push_back(*iter); | |
287 } | |
288 | |
289 SetupValuator(); | |
290 } | |
291 | |
292 bool TouchFactory::IsTouchDevice(unsigned deviceid) const { | |
293 return deviceid < touch_device_lookup_.size() ? | |
294 touch_device_lookup_[deviceid] : false; | |
295 } | |
296 | |
297 bool TouchFactory::IsSlotUsed(int slot) const { | |
298 CHECK_LT(slot, kMaxTouchPoints); | |
299 return slots_used_[slot]; | |
300 } | |
301 | |
302 void TouchFactory::SetSlotUsed(int slot, bool used) { | |
303 CHECK_LT(slot, kMaxTouchPoints); | |
304 slots_used_[slot] = used; | |
305 } | |
306 | |
307 bool TouchFactory::GrabTouchDevices(Display* display, ::Window window) { | |
308 #if defined(TOUCH_UI) | |
309 if (!base::MessagePumpForUI::HasXInput2() || | |
310 touch_device_list_.empty()) | |
311 return true; | |
312 #endif | |
313 | |
314 unsigned char mask[XIMaskLen(XI_LASTEVENT)]; | |
315 bool success = true; | |
316 | |
317 memset(mask, 0, sizeof(mask)); | |
318 XISetMask(mask, XI_ButtonPress); | |
319 XISetMask(mask, XI_ButtonRelease); | |
320 XISetMask(mask, XI_Motion); | |
321 | |
322 XIEventMask evmask; | |
323 evmask.mask_len = sizeof(mask); | |
324 evmask.mask = mask; | |
325 for (std::vector<int>::const_iterator iter = | |
326 touch_device_list_.begin(); | |
327 iter != touch_device_list_.end(); ++iter) { | |
328 evmask.deviceid = *iter; | |
329 Status status = XIGrabDevice(display, *iter, window, CurrentTime, None, | |
330 GrabModeAsync, GrabModeAsync, False, &evmask); | |
331 success = success && status == GrabSuccess; | |
332 } | |
333 | |
334 return success; | |
335 } | |
336 | |
337 bool TouchFactory::UngrabTouchDevices(Display* display) { | |
338 #if defined(TOUCH_UI) | |
339 if (!base::MessagePumpForUI::HasXInput2()) | |
340 return true; | |
341 #endif | |
342 | |
343 bool success = true; | |
344 for (std::vector<int>::const_iterator iter = | |
345 touch_device_list_.begin(); | |
346 iter != touch_device_list_.end(); ++iter) { | |
347 Status status = XIUngrabDevice(display, *iter, CurrentTime); | |
348 success = success && status == GrabSuccess; | |
349 } | |
350 return success; | |
351 } | |
352 | |
353 void TouchFactory::SetCursorVisible(bool show, bool start_timer) { | |
354 #if defined(TOUCH_UI) | |
355 if (!base::MessagePumpForUI::HasXInput2()) | |
356 return; | |
357 #endif | |
358 | |
359 // The cursor is going to be shown. Reset the timer for hiding it. | |
360 if (show && start_timer) { | |
361 cursor_timer_.Stop(); | |
362 cursor_timer_.Start( | |
363 FROM_HERE, base::TimeDelta::FromSeconds(kCursorIdleSeconds), | |
364 this, &TouchFactory::HideCursorForInactivity); | |
365 } else { | |
366 cursor_timer_.Stop(); | |
367 } | |
368 | |
369 if (show == is_cursor_visible_) | |
370 return; | |
371 | |
372 is_cursor_visible_ = show; | |
373 | |
374 Display* display = ui::GetXDisplay(); | |
375 Window window = DefaultRootWindow(display); | |
376 | |
377 if (is_cursor_visible_) { | |
378 XDefineCursor(display, window, arrow_cursor_); | |
379 } else { | |
380 XDefineCursor(display, window, invisible_cursor_); | |
381 } | |
382 } | |
383 | |
384 void TouchFactory::SetupValuator() { | |
385 memset(valuator_lookup_, -1, sizeof(valuator_lookup_)); | |
386 memset(touch_param_min_, 0, sizeof(touch_param_min_)); | |
387 memset(touch_param_max_, 0, sizeof(touch_param_max_)); | |
388 | |
389 Display* display = ui::GetXDisplay(); | |
390 int ndevice; | |
391 XIDeviceInfo* info_list = XIQueryDevice(display, XIAllDevices, &ndevice); | |
392 | |
393 for (int i = 0; i < ndevice; i++) { | |
394 XIDeviceInfo* info = info_list + i; | |
395 | |
396 if (!IsTouchDevice(info->deviceid)) | |
397 continue; | |
398 | |
399 for (int j = 0; j < TP_LAST_ENTRY; j++) { | |
400 TouchParam tp = static_cast<TouchParam>(j); | |
401 XIValuatorClassInfo* valuator = FindTPValuator(display, info, tp); | |
402 if (valuator) { | |
403 valuator_lookup_[info->deviceid][j] = valuator->number; | |
404 touch_param_min_[info->deviceid][j] = valuator->min; | |
405 touch_param_max_[info->deviceid][j] = valuator->max; | |
406 } | |
407 } | |
408 } | |
409 | |
410 if (info_list) | |
411 XIFreeDeviceInfo(info_list); | |
412 } | |
413 | |
414 bool TouchFactory::ExtractTouchParam(const XEvent& xev, | |
415 TouchParam tp, | |
416 float* value) { | |
417 XIDeviceEvent* xiev = static_cast<XIDeviceEvent*>(xev.xcookie.data); | |
418 if (xiev->sourceid >= kMaxDeviceNum) | |
419 return false; | |
420 int v = valuator_lookup_[xiev->sourceid][tp]; | |
421 if (v >= 0 && XIMaskIsSet(xiev->valuators.mask, v)) { | |
422 *value = xiev->valuators.values[v]; | |
423 return true; | |
424 } | |
425 | |
426 return false; | |
427 } | |
428 | |
429 bool TouchFactory::NormalizeTouchParam(unsigned int deviceid, | |
430 TouchParam tp, | |
431 float* value) { | |
432 float max_value; | |
433 float min_value; | |
434 if (GetTouchParamRange(deviceid, tp, &min_value, &max_value)) { | |
435 *value = (*value - min_value) / (max_value - min_value); | |
436 DCHECK(*value >= 0.0 && *value <= 1.0); | |
437 return true; | |
438 } | |
439 return false; | |
440 } | |
441 | |
442 bool TouchFactory::GetTouchParamRange(unsigned int deviceid, | |
443 TouchParam tp, | |
444 float* min, | |
445 float* max) { | |
446 if (valuator_lookup_[deviceid][tp] >= 0) { | |
447 *min = touch_param_min_[deviceid][tp]; | |
448 *max = touch_param_max_[deviceid][tp]; | |
449 return true; | |
450 } | |
451 return false; | |
452 } | |
453 | |
454 } // namespace views | |
OLD | NEW |