Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(934)

Side by Side Diff: views/touchui/touch_factory.cc

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

Powered by Google App Engine
This is Rietveld 408576698