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 #ifndef VIEWS_TOUCHUI_TOUCH_FACTORY_H_ | |
6 #define VIEWS_TOUCHUI_TOUCH_FACTORY_H_ | |
7 #pragma once | |
8 | |
9 #include <bitset> | |
10 #include <vector> | |
11 | |
12 #include "base/memory/singleton.h" | |
13 #include "base/timer.h" | |
14 #include "views/views_export.h" | |
15 | |
16 typedef unsigned long Cursor; | |
17 typedef unsigned long Window; | |
18 typedef struct _XDisplay Display; | |
19 typedef union _XEvent XEvent; | |
20 | |
21 namespace views { | |
22 | |
23 // Functions related to determining touch devices. | |
24 class VIEWS_EXPORT TouchFactory { | |
25 public: | |
26 // Define the touch params following the Multi-touch Protocol. | |
27 enum TouchParam { | |
28 TP_TOUCH_MAJOR = 0, // Length of the touch area. | |
29 TP_TOUCH_MINOR, // Width of the touch area. | |
30 TP_ORIENTATION, // Angle between the X-axis and the major axis of the | |
31 // touch area. | |
32 TP_PRESSURE, // Pressure of the touch contact. | |
33 | |
34 // NOTE: A touch event can have multiple touch points. So when we receive a | |
35 // touch event, we need to determine which point triggered the event. | |
36 // A touch point can have both a 'Slot ID' and a 'Tracking ID', and they can | |
37 // be (in fact, usually are) different. The 'Slot ID' ranges between 0 and | |
38 // (X - 1), where X is the maximum touch points supported by the device. The | |
39 // 'Tracking ID' can be any 16-bit value. With XInput 2.0, an XI_Motion | |
40 // event that comes from a currently-unused 'Slot ID' indicates the creation | |
41 // of a new touch point, and any event that comes with a 0 value for | |
42 // 'Tracking ID' marks the removal of a touch point. During the lifetime of | |
43 // a touchpoint, we use the 'Slot ID' as its identifier. The XI_ButtonPress | |
44 // and XI_ButtonRelease events are ignored. | |
45 #if !defined(USE_XI2_MT) | |
46 TP_SLOT_ID, // ID of the finger that triggered a touch event | |
47 // (useful when tracking multiple simultaneous | |
48 // touches) | |
49 #endif | |
50 // NOTE for XInput MT: 'Tracking ID' is provided in every touch event to | |
51 // track individual touch. 'Tracking ID' is an unsigned 32-bit value and | |
52 // is increased for each new touch. It will wrap back to 0 when reaching | |
53 // the numerical limit. | |
54 TP_TRACKING_ID, // ID of the touch point. | |
55 | |
56 TP_LAST_ENTRY | |
57 }; | |
58 | |
59 // Returns the TouchFactory singleton. | |
60 static TouchFactory* GetInstance(); | |
61 | |
62 // Updates the list of devices. | |
63 void UpdateDeviceList(Display* display); | |
64 | |
65 // Checks whether an XI2 event should be processed or not (i.e. if the event | |
66 // originated from a device we are interested in). | |
67 bool ShouldProcessXI2Event(XEvent* xevent); | |
68 | |
69 // Setup an X Window for XInput2 events. | |
70 void SetupXI2ForXWindow(::Window xid); | |
71 | |
72 // Keeps a list of touch devices so that it is possible to determine if a | |
73 // pointer event is a touch-event or a mouse-event. The list is reset each | |
74 // time this is called. | |
75 void SetTouchDeviceList(const std::vector<unsigned int>& devices); | |
76 | |
77 // Is the device a touch-device? | |
78 bool IsTouchDevice(unsigned int deviceid) const; | |
79 | |
80 #if !defined(USE_XI2_MT) | |
81 // Is the slot ID currently used? | |
82 bool IsSlotUsed(int slot) const; | |
83 | |
84 // Marks a slot as being used/unused. | |
85 void SetSlotUsed(int slot, bool used); | |
86 #endif | |
87 | |
88 // Grabs the touch devices for the specified window on the specified display. | |
89 // Returns if grab was successful for all touch devices. | |
90 bool GrabTouchDevices(Display* display, ::Window window); | |
91 | |
92 // Ungrabs the touch devices. Returns if ungrab was successful for all touch | |
93 // devices. | |
94 bool UngrabTouchDevices(Display* display); | |
95 | |
96 // Updates the root window to show (or hide) the cursor. Also indicate whether | |
97 // the timer should be started to automatically hide the cursor after a | |
98 // certain duration of inactivity (i.e. it is ignored if |show| is false). | |
99 void SetCursorVisible(bool show, bool start_timer); | |
100 | |
101 // Whether the cursor is currently visible or not. | |
102 bool is_cursor_visible() const { | |
103 return is_cursor_visible_; | |
104 } | |
105 | |
106 // Extract the TouchParam from the XEvent. Return true and the value is set | |
107 // if the TouchParam is found, false and value unchanged if the TouchParam | |
108 // is not found. | |
109 bool ExtractTouchParam(const XEvent& xev, TouchParam tp, float* value); | |
110 | |
111 // Normalize the TouchParam with value on deviceid to fall into [0, 1]. | |
112 // *value = (*value - min_value_of_tp) / (max_value_of_tp - min_value_of_tp) | |
113 // Returns true and sets the normalized value in|value| if normalization is | |
114 // successful. Returns false and |value| is unchanged otherwise. | |
115 bool NormalizeTouchParam(unsigned int deviceid, TouchParam tp, float* value); | |
116 | |
117 // Extract the range of the TouchParam. Return true if the range is available | |
118 // and written into min & max, false if the range is not available. | |
119 bool GetTouchParamRange(unsigned int deviceid, | |
120 TouchParam tp, | |
121 float* min, | |
122 float* max); | |
123 | |
124 void set_keep_mouse_cursor(bool keep) { keep_mouse_cursor_ = keep; } | |
125 bool keep_mouse_cursor() const { return keep_mouse_cursor_; } | |
126 | |
127 private: | |
128 TouchFactory(); | |
129 | |
130 ~TouchFactory(); | |
131 | |
132 void HideCursorForInactivity() { | |
133 SetCursorVisible(false, false); | |
134 } | |
135 | |
136 // Setup the internal bookkeeping of the touch params valuator information for | |
137 // touch devices | |
138 void SetupValuator(); | |
139 | |
140 // Requirement for Signleton | |
141 friend struct DefaultSingletonTraits<TouchFactory>; | |
142 | |
143 // The default cursor is hidden after startup, and when the mouse pointer is | |
144 // idle for a while. Once there is some event from a mouse device, the cursor | |
145 // is immediately displayed. | |
146 bool is_cursor_visible_; | |
147 | |
148 // Whether to turn off automatic hiding of mouse cursor. This is useful for | |
149 // debugging touch build on the desktop. | |
150 bool keep_mouse_cursor_; | |
151 | |
152 // The cursor is hidden if it is idle for a certain amount time. This timer | |
153 // is used to keep track of the idleness. | |
154 base::OneShotTimer<TouchFactory> cursor_timer_; | |
155 | |
156 // The default cursor. | |
157 Cursor arrow_cursor_; | |
158 | |
159 // The invisible cursor. | |
160 Cursor invisible_cursor_; | |
161 | |
162 // NOTE: To keep track of touch devices, we currently maintain a lookup table | |
163 // to quickly decide if a device is a touch device or not. We also maintain a | |
164 // list of the touch devices. Ideally, there will be only one touch device, | |
165 // and instead of having the lookup table and the list, there will be a single | |
166 // identifier for the touch device. This can be completed after enough testing | |
167 // on real touch devices. | |
168 | |
169 static const int kMaxDeviceNum = 128; | |
170 | |
171 // A quick lookup table for determining if events from the pointer device | |
172 // should be processed. | |
173 std::bitset<kMaxDeviceNum> pointer_device_lookup_; | |
174 | |
175 // A quick lookup table for determining if a device is a touch device. | |
176 std::bitset<kMaxDeviceNum> touch_device_lookup_; | |
177 | |
178 // The list of touch devices. | |
179 std::vector<int> touch_device_list_; | |
180 | |
181 // Index table to find the valuator for the TouchParam on the specific device | |
182 // by valuator_lookup_[device_id][touch_params]. Use 2-D array to get fast | |
183 // index at the expense of space. If the kMaxDeviceNum grows larger that the | |
184 // space waste becomes a concern, the 2D lookup table can be replaced by a | |
185 // hash map. | |
186 signed char valuator_lookup_[kMaxDeviceNum][TP_LAST_ENTRY]; | |
187 | |
188 // Index table to find the min & max value of the TouchParam on a specific | |
189 // device. | |
190 int touch_param_min_[kMaxDeviceNum][TP_LAST_ENTRY]; | |
191 int touch_param_max_[kMaxDeviceNum][TP_LAST_ENTRY]; | |
192 | |
193 #if !defined(USE_XI2_MT) | |
194 // Maximum simultaneous touch points. | |
195 static const int kMaxTouchPoints = 32; | |
196 | |
197 // A lookup table for slots in use for a touch event. | |
198 std::bitset<kMaxTouchPoints> slots_used_; | |
199 #endif | |
200 | |
201 DISALLOW_COPY_AND_ASSIGN(TouchFactory); | |
202 }; | |
203 | |
204 } // namespace views | |
205 | |
206 #endif // VIEWS_TOUCHUI_TOUCH_FACTORY_H_ | |
OLD | NEW |