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