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 "views/touchui/touch_factory.h" | 5 #include "views/touchui/touch_factory.h" |
6 | 6 |
7 #include <gtk/gtk.h> | 7 #include <gtk/gtk.h> |
8 #include <gdk/gdkx.h> | 8 #include <gdk/gdkx.h> |
9 #include <X11/cursorfont.h> | 9 #include <X11/cursorfont.h> |
10 #include <X11/extensions/XInput.h> | 10 #include <X11/extensions/XInput.h> |
(...skipping 11 matching lines...) Expand all Loading... | |
22 // The X cursor is hidden if it is idle for kCursorIdleSeconds seconds. | 22 // The X cursor is hidden if it is idle for kCursorIdleSeconds seconds. |
23 int kCursorIdleSeconds = 5; | 23 int kCursorIdleSeconds = 5; |
24 | 24 |
25 // Given the TouchParam, return the correspoding XIValuatorClassInfo using | 25 // Given the TouchParam, return the correspoding XIValuatorClassInfo using |
26 // the X device information through Atom name matching. | 26 // the X device information through Atom name matching. |
27 XIValuatorClassInfo* FindTPValuator(Display* display, | 27 XIValuatorClassInfo* FindTPValuator(Display* display, |
28 XIDeviceInfo* info, | 28 XIDeviceInfo* info, |
29 views::TouchFactory::TouchParam tp) { | 29 views::TouchFactory::TouchParam tp) { |
30 // Lookup table for mapping TouchParam to Atom string used in X. | 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. | 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 { | 32 static struct { |
36 views::TouchFactory::TouchParam tp; | 33 views::TouchFactory::TouchParam tp; |
37 const char* atom; | 34 const char* atom; |
38 } kTouchParamAtom[] = { | 35 } kTouchParamAtom[] = { |
39 { views::TouchFactory::TP_TOUCH_MAJOR, "Abs MT Touch Major" }, | 36 { views::TouchFactory::TP_TOUCH_MAJOR, "Abs MT Touch Major" }, |
40 { views::TouchFactory::TP_TOUCH_MINOR, "Abs MT Touch Minor" }, | 37 { views::TouchFactory::TP_TOUCH_MINOR, "Abs MT Touch Minor" }, |
41 { views::TouchFactory::TP_ORIENTATION, "Abs MT Orientation" }, | 38 { views::TouchFactory::TP_ORIENTATION, "Abs MT Orientation" }, |
39 { views::TouchFactory::TP_POSITION_X, "Abs MT Position X" }, | |
40 { views::TouchFactory::TP_POSITION_Y, "Abs MT Position Y" }, | |
41 { views::TouchFactory::TP_TRACKING_ID, "Abs MT Tracking ID" }, | |
42 { views::TouchFactory::TP_PRESSURE, "Abs MT Pressure" }, | 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 }, | 43 { views::TouchFactory::TP_LAST_ENTRY, NULL }, |
46 }; | 44 }; |
47 | 45 |
48 const char* atom_tp = NULL; | 46 const char* atom_tp = NULL; |
49 | 47 |
50 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTouchParamAtom); i++) { | 48 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTouchParamAtom); i++) { |
51 if (tp == kTouchParamAtom[i].tp) { | 49 if (tp == kTouchParamAtom[i].tp) { |
52 atom_tp = kTouchParamAtom[i].atom; | 50 atom_tp = kTouchParamAtom[i].atom; |
53 break; | 51 break; |
54 } | 52 } |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
120 // static | 118 // static |
121 TouchFactory* TouchFactory::GetInstance() { | 119 TouchFactory* TouchFactory::GetInstance() { |
122 return Singleton<TouchFactory>::get(); | 120 return Singleton<TouchFactory>::get(); |
123 } | 121 } |
124 | 122 |
125 TouchFactory::TouchFactory() | 123 TouchFactory::TouchFactory() |
126 : is_cursor_visible_(true), | 124 : is_cursor_visible_(true), |
127 keep_mouse_cursor_(false), | 125 keep_mouse_cursor_(false), |
128 cursor_timer_(), | 126 cursor_timer_(), |
129 pointer_device_lookup_(), | 127 pointer_device_lookup_(), |
130 touch_device_list_(), | 128 touch_device_list_() { |
131 slots_used_() { | |
132 #if defined(TOUCH_UI) | 129 #if defined(TOUCH_UI) |
133 if (!base::MessagePumpForUI::HasXInput2()) | 130 if (!base::MessagePumpForUI::HasXInput2()) |
134 return; | 131 return; |
135 #endif | 132 #endif |
136 | 133 |
137 char nodata[] = { 0, 0, 0, 0, 0, 0, 0, 0 }; | 134 char nodata[] = { 0, 0, 0, 0, 0, 0, 0, 0 }; |
138 XColor black; | 135 XColor black; |
139 black.red = black.green = black.blue = 0; | 136 black.red = black.green = black.blue = 0; |
140 Display* display = ui::GetXDisplay(); | 137 Display* display = ui::GetXDisplay(); |
141 Pixmap blank = XCreateBitmapFromData(display, ui::GetX11RootWindow(), | 138 Pixmap blank = XCreateBitmapFromData(display, ui::GetX11RootWindow(), |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
175 SetCursorVisible(true, false); | 172 SetCursorVisible(true, false); |
176 Display* display = ui::GetXDisplay(); | 173 Display* display = ui::GetXDisplay(); |
177 XFreeCursor(display, invisible_cursor_); | 174 XFreeCursor(display, invisible_cursor_); |
178 XFreeCursor(display, arrow_cursor_); | 175 XFreeCursor(display, arrow_cursor_); |
179 | 176 |
180 RemoveGtkWidgetRealizeNotifier(); | 177 RemoveGtkWidgetRealizeNotifier(); |
181 } | 178 } |
182 | 179 |
183 void TouchFactory::UpdateDeviceList(Display* display) { | 180 void TouchFactory::UpdateDeviceList(Display* display) { |
184 // Detect touch devices. | 181 // 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; | 182 int count = 0; |
191 touch_device_lookup_.reset(); | 183 touch_device_lookup_.reset(); |
192 touch_device_list_.clear(); | 184 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 | 185 |
206 // Instead of asking X for the list of devices all the time, let's maintain a | 186 // Instead of asking X for the list of devices all the time, let's maintain a |
207 // list of pointer devices we care about. | 187 // list of pointer devices we care about. |
208 // It should not be necessary to select for slave devices. XInput2 provides | 188 // It should not be necessary to select for slave devices. XInput2 provides |
209 // enough information to the event callback to decide which slave device | 189 // enough information to the event callback to decide which slave device |
210 // triggered the event, thus decide whether the 'pointer event' is a | 190 // triggered the event, thus decide whether the 'pointer event' is a |
211 // 'mouse event' or a 'touch event'. | 191 // 'mouse event' or a 'touch event'. |
212 // However, on some desktops, some events from a master pointer are | 192 // However, on some desktops, some events from a master pointer are |
213 // not delivered to the client. So we select for slave devices instead. | 193 // not delivered to the client. So we select for slave devices instead. |
214 // If the touch device has 'GrabDevice' set and 'SendCoreEvents' unset (which | 194 // 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 | 195 // 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 | 196 // floating device is not connected to a master device. So it is necessary to |
217 // also select on the floating devices. | 197 // also select on the floating devices. |
218 pointer_device_lookup_.reset(); | 198 pointer_device_lookup_.reset(); |
219 XIDeviceInfo* devices = XIQueryDevice(display, XIAllDevices, &count); | 199 XIDeviceInfo* devices = XIQueryDevice(display, XIAllDevices, &count); |
220 for (int i = 0; i < count; i++) { | 200 if (devices) { |
221 XIDeviceInfo* devinfo = devices + i; | 201 for (int i = 0; i < count; i++) { |
222 if (devinfo->use == XIFloatingSlave || devinfo->use == XISlavePointer) { | 202 if (!devices[i].enabled) |
223 pointer_device_lookup_[devinfo->deviceid] = true; | 203 continue; |
204 | |
205 XIDeviceInfo* devinfo = devices + i; | |
206 for (int k = 0; k < devices[i].num_classes; ++k) { | |
207 XIAnyClassInfo *xiclassinfo = devices[i].classes[k]; | |
208 if (xiclassinfo->type == XITouchClass) { | |
209 XITouchClassInfo *tci = (XITouchClassInfo *)xiclassinfo; | |
sadrul
2011/09/06 14:35:21
'* ' instead of ' *' (here and other places).
| |
210 // Only care direct touch device (such as touch screen) right now | |
211 if (tci->mode == XIDirectTouch) | |
212 { | |
sadrul
2011/09/06 14:35:21
Brace goes at the end of the previous line.
| |
213 touch_device_lookup_[devinfo->deviceid] = true; | |
214 touch_device_list_.push_back(devinfo->deviceid); | |
215 } | |
216 } | |
217 } | |
218 if (devinfo->use == XIFloatingSlave || devinfo->use == XISlavePointer) { | |
219 pointer_device_lookup_[devinfo->deviceid] = true; | |
220 } | |
sadrul
2011/09/06 14:35:21
No braces necessary here
| |
224 } | 221 } |
225 } | 222 } |
226 if (devices) | 223 if (devices) |
227 XIFreeDeviceInfo(devices); | 224 XIFreeDeviceInfo(devices); |
228 | 225 |
229 SetupValuator(); | 226 SetupValuator(); |
230 } | 227 } |
231 | 228 |
232 bool TouchFactory::ShouldProcessXI2Event(XEvent* xev) { | 229 bool TouchFactory::ShouldProcessXI2Event(XEvent* xev) { |
233 DCHECK_EQ(GenericEvent, xev->type); | 230 DCHECK_EQ(GenericEvent, xev->type); |
234 | 231 |
235 XGenericEventCookie* cookie = &xev->xcookie; | 232 XIEvent* event = static_cast<XIEvent*>(xev->xcookie.data); |
236 if (cookie->evtype != XI_ButtonPress && | 233 |
237 cookie->evtype != XI_ButtonRelease && | 234 if (event->evtype != XI_TouchBegin && |
238 cookie->evtype != XI_Motion) | 235 event->evtype != XI_TouchUpdate && |
236 event->evtype != XI_TouchEnd) | |
239 return true; | 237 return true; |
240 | 238 |
241 XIDeviceEvent* xiev = static_cast<XIDeviceEvent*>(cookie->data); | 239 XIDeviceEvent* xiev = reinterpret_cast<XIDeviceEvent*>(event); |
242 return pointer_device_lookup_[xiev->deviceid]; | 240 return touch_device_lookup_[xiev->sourceid]; |
243 } | 241 } |
244 | 242 |
245 void TouchFactory::SetupXI2ForXWindow(Window window) { | 243 void TouchFactory::SetupXI2ForXWindow(Window window) { |
246 // Setup mask for mouse events. It is possible that a device is loaded/plugged | 244 // 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 | 245 // 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 | 246 // 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 | 247 // 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 | 248 // the events from uninteresting devices. We do the latter because that's |
251 // simpler. | 249 // simpler. |
252 | 250 |
253 Display* display = ui::GetXDisplay(); | 251 Display* display = ui::GetXDisplay(); |
254 | 252 |
255 unsigned char mask[XIMaskLen(XI_LASTEVENT)]; | 253 unsigned char mask[XIMaskLen(XI_LASTEVENT)]; |
256 memset(mask, 0, sizeof(mask)); | 254 memset(mask, 0, sizeof(mask)); |
257 | 255 |
258 XISetMask(mask, XI_ButtonPress); | 256 XISetMask(mask, XI_TouchBegin); |
259 XISetMask(mask, XI_ButtonRelease); | 257 XISetMask(mask, XI_TouchUpdate); |
260 XISetMask(mask, XI_Motion); | 258 XISetMask(mask, XI_TouchEnd); |
261 | 259 |
262 XIEventMask evmask; | 260 XIEventMask evmask; |
263 evmask.deviceid = XIAllDevices; | 261 evmask.deviceid = XIAllDevices; |
264 evmask.mask_len = sizeof(mask); | 262 evmask.mask_len = sizeof(mask); |
265 evmask.mask = mask; | 263 evmask.mask = mask; |
266 XISelectEvents(display, window, &evmask, 1); | 264 XISelectEvents(display, window, &evmask, 1); |
267 XFlush(display); | 265 XFlush(display); |
268 } | 266 } |
269 | 267 |
270 void TouchFactory::SetTouchDeviceList( | 268 void TouchFactory::SetTouchDeviceList( |
271 const std::vector<unsigned int>& devices) { | 269 const std::vector<unsigned int>& devices) { |
272 touch_device_lookup_.reset(); | 270 touch_device_lookup_.reset(); |
273 touch_device_list_.clear(); | 271 touch_device_list_.clear(); |
274 for (std::vector<unsigned int>::const_iterator iter = devices.begin(); | 272 for (std::vector<unsigned int>::const_iterator iter = devices.begin(); |
275 iter != devices.end(); ++iter) { | 273 iter != devices.end(); ++iter) { |
276 DCHECK(*iter < touch_device_lookup_.size()); | 274 DCHECK(*iter < touch_device_lookup_.size()); |
277 touch_device_lookup_[*iter] = true; | 275 touch_device_lookup_[*iter] = true; |
278 touch_device_list_.push_back(*iter); | 276 touch_device_list_.push_back(*iter); |
279 } | 277 } |
280 | 278 |
281 SetupValuator(); | 279 SetupValuator(); |
282 } | 280 } |
283 | 281 |
284 bool TouchFactory::IsTouchDevice(unsigned deviceid) const { | 282 bool TouchFactory::IsTouchDevice(unsigned deviceid) const { |
285 return deviceid < touch_device_lookup_.size() ? | 283 return deviceid < touch_device_lookup_.size() ? |
286 touch_device_lookup_[deviceid] : false; | 284 touch_device_lookup_[deviceid] : false; |
287 } | 285 } |
288 | 286 |
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) { | 287 bool TouchFactory::GrabTouchDevices(Display* display, ::Window window) { |
300 #if defined(TOUCH_UI) | 288 #if defined(TOUCH_UI) |
301 if (!base::MessagePumpForUI::HasXInput2() || | 289 if (!base::MessagePumpForUI::HasXInput2() || |
302 touch_device_list_.empty()) | 290 touch_device_list_.empty()) |
303 return true; | 291 return true; |
304 #endif | 292 #endif |
305 | 293 |
306 unsigned char mask[XIMaskLen(XI_LASTEVENT)]; | 294 unsigned char mask[XIMaskLen(XI_LASTEVENT)]; |
307 bool success = true; | 295 bool success = true; |
308 | 296 |
309 memset(mask, 0, sizeof(mask)); | 297 memset(mask, 0, sizeof(mask)); |
310 XISetMask(mask, XI_ButtonPress); | 298 XISetMask(mask, XI_TouchBegin); |
311 XISetMask(mask, XI_ButtonRelease); | 299 XISetMask(mask, XI_TouchUpdate); |
312 XISetMask(mask, XI_Motion); | 300 XISetMask(mask, XI_TouchEnd); |
313 | 301 |
314 XIEventMask evmask; | 302 XIEventMask evmask; |
315 evmask.mask_len = sizeof(mask); | 303 evmask.mask_len = sizeof(mask); |
316 evmask.mask = mask; | 304 evmask.mask = mask; |
317 for (std::vector<int>::const_iterator iter = | 305 for (std::vector<int>::const_iterator iter = |
318 touch_device_list_.begin(); | 306 touch_device_list_.begin(); |
319 iter != touch_device_list_.end(); ++iter) { | 307 iter != touch_device_list_.end(); ++iter) { |
320 evmask.deviceid = *iter; | 308 evmask.deviceid = *iter; |
321 Status status = XIGrabDevice(display, *iter, window, CurrentTime, None, | 309 Status status = XIGrabDevice(display, *iter, window, CurrentTime, None, |
322 GrabModeAsync, GrabModeAsync, False, &evmask); | 310 GrabModeAsync, GrabModeAsync, False, &evmask); |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
407 float* value) { | 395 float* value) { |
408 XIDeviceEvent* xiev = static_cast<XIDeviceEvent*>(xev.xcookie.data); | 396 XIDeviceEvent* xiev = static_cast<XIDeviceEvent*>(xev.xcookie.data); |
409 if (xiev->sourceid >= kMaxDeviceNum) | 397 if (xiev->sourceid >= kMaxDeviceNum) |
410 return false; | 398 return false; |
411 int v = valuator_lookup_[xiev->sourceid][tp]; | 399 int v = valuator_lookup_[xiev->sourceid][tp]; |
412 if (v >= 0 && XIMaskIsSet(xiev->valuators.mask, v)) { | 400 if (v >= 0 && XIMaskIsSet(xiev->valuators.mask, v)) { |
413 *value = xiev->valuators.values[v]; | 401 *value = xiev->valuators.values[v]; |
414 return true; | 402 return true; |
415 } | 403 } |
416 | 404 |
405 if (tp == TP_TRACKING_ID) | |
406 { | |
sadrul
2011/09/06 14:35:21
brace in the previous line
| |
407 *value = xiev->detail; | |
408 return true; | |
409 } | |
410 | |
417 return false; | 411 return false; |
418 } | 412 } |
419 | 413 |
420 bool TouchFactory::NormalizeTouchParam(unsigned int deviceid, | 414 bool TouchFactory::NormalizeTouchParam(unsigned int deviceid, |
421 TouchParam tp, | 415 TouchParam tp, |
422 float* value) { | 416 float* value) { |
423 float max_value; | 417 float max_value; |
424 float min_value; | 418 float min_value; |
425 if (GetTouchParamRange(deviceid, tp, &min_value, &max_value)) { | 419 if (GetTouchParamRange(deviceid, tp, &min_value, &max_value)) { |
426 *value = (*value - min_value) / (max_value - min_value); | 420 *value = (*value - min_value) / (max_value - min_value); |
427 DCHECK(*value >= 0.0 && *value <= 1.0); | 421 DCHECK(*value >= 0.0 && *value <= 1.0); |
428 return true; | 422 return true; |
429 } | 423 } |
430 return false; | 424 return false; |
431 } | 425 } |
432 | 426 |
433 bool TouchFactory::GetTouchParamRange(unsigned int deviceid, | 427 bool TouchFactory::GetTouchParamRange(unsigned int deviceid, |
434 TouchParam tp, | 428 TouchParam tp, |
435 float* min, | 429 float* min, |
436 float* max) { | 430 float* max) { |
437 if (valuator_lookup_[deviceid][tp] >= 0) { | 431 if (valuator_lookup_[deviceid][tp] >= 0) { |
438 *min = touch_param_min_[deviceid][tp]; | 432 *min = touch_param_min_[deviceid][tp]; |
439 *max = touch_param_max_[deviceid][tp]; | 433 *max = touch_param_max_[deviceid][tp]; |
440 return true; | 434 return true; |
441 } | 435 } |
442 return false; | 436 return false; |
443 } | 437 } |
444 | 438 |
445 } // namespace views | 439 } // namespace views |
OLD | NEW |