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 TP_SLOT_ID, // ID of the finger that triggered a touch event | |
46 // (useful when tracking multiple simultaneous | |
47 // touches) | |
48 TP_TRACKING_ID, // ID of the touch point. | |
49 | |
50 TP_LAST_ENTRY | |
51 }; | |
52 | |
53 // Returns the TouchFactory singleton. | |
54 static TouchFactory* GetInstance(); | |
55 | |
56 // Updates the list of devices. | |
57 void UpdateDeviceList(Display* display); | |
58 | |
59 // Checks whether an XI2 event should be processed or not (i.e. if the event | |
60 // originated from a device we are interested in). | |
61 bool ShouldProcessXI2Event(XEvent* xevent); | |
62 | |
63 // Setup an X Window for XInput2 events. | |
64 void SetupXI2ForXWindow(::Window xid); | |
65 | |
66 // Keeps a list of touch devices so that it is possible to determine if a | |
67 // pointer event is a touch-event or a mouse-event. The list is reset each | |
68 // time this is called. | |
69 void SetTouchDeviceList(const std::vector<unsigned int>& devices); | |
70 | |
71 // Is the device a touch-device? | |
72 bool IsTouchDevice(unsigned int deviceid) const; | |
73 | |
74 // Is the slot ID currently used? | |
75 bool IsSlotUsed(int slot) const; | |
76 | |
77 // Marks a slot as being used/unused. | |
78 void SetSlotUsed(int slot, bool used); | |
79 | |
80 // Grabs the touch devices for the specified window on the specified display. | |
81 // Returns if grab was successful for all touch devices. | |
82 bool GrabTouchDevices(Display* display, ::Window window); | |
83 | |
84 // Ungrabs the touch devices. Returns if ungrab was successful for all touch | |
85 // devices. | |
86 bool UngrabTouchDevices(Display* display); | |
87 | |
88 // Updates the root window to show (or hide) the cursor. Also indicate whether | |
89 // the timer should be started to automatically hide the cursor after a | |
90 // certain duration of inactivity (i.e. it is ignored if |show| is false). | |
91 void SetCursorVisible(bool show, bool start_timer); | |
92 | |
93 // Whether the cursor is currently visible or not. | |
94 bool is_cursor_visible() const { | |
95 return is_cursor_visible_; | |
96 } | |
97 | |
98 // Extract the TouchParam from the XEvent. Return true and the value is set | |
99 // if the TouchParam is found, false and value unchanged if the TouchParam | |
100 // is not found. | |
101 bool ExtractTouchParam(const XEvent& xev, TouchParam tp, float* value); | |
102 | |
103 // Normalize the TouchParam with value on deviceid to fall into [0, 1]. | |
104 // *value = (*value - min_value_of_tp) / (max_value_of_tp - min_value_of_tp) | |
105 // Returns true and sets the normalized value in|value| if normalization is | |
106 // successful. Returns false and |value| is unchanged otherwise. | |
107 bool NormalizeTouchParam(unsigned int deviceid, TouchParam tp, float* value); | |
108 | |
109 // Extract the range of the TouchParam. Return true if the range is available | |
110 // and written into min & max, false if the range is not available. | |
111 bool GetTouchParamRange(unsigned int deviceid, | |
112 TouchParam tp, | |
113 float* min, | |
114 float* max); | |
115 | |
116 void set_keep_mouse_cursor(bool keep) { keep_mouse_cursor_ = keep; } | |
117 bool keep_mouse_cursor() const { return keep_mouse_cursor_; } | |
118 | |
119 private: | |
120 TouchFactory(); | |
121 | |
122 ~TouchFactory(); | |
123 | |
124 void HideCursorForInactivity() { | |
125 SetCursorVisible(false, false); | |
126 } | |
127 | |
128 // Setup the internal bookkeeping of the touch params valuator information for | |
129 // touch devices | |
130 void SetupValuator(); | |
131 | |
132 // Requirement for Signleton | |
133 friend struct DefaultSingletonTraits<TouchFactory>; | |
134 | |
135 // The default cursor is hidden after startup, and when the mouse pointer is | |
136 // idle for a while. Once there is some event from a mouse device, the cursor | |
137 // is immediately displayed. | |
138 bool is_cursor_visible_; | |
139 | |
140 // Whether to turn off automatic hiding of mouse cursor. This is useful for | |
141 // debugging touch build on the desktop. | |
142 bool keep_mouse_cursor_; | |
143 | |
144 // The cursor is hidden if it is idle for a certain amount time. This timer | |
145 // is used to keep track of the idleness. | |
146 base::OneShotTimer<TouchFactory> cursor_timer_; | |
147 | |
148 // The default cursor. | |
149 Cursor arrow_cursor_; | |
150 | |
151 // The invisible cursor. | |
152 Cursor invisible_cursor_; | |
153 | |
154 // NOTE: To keep track of touch devices, we currently maintain a lookup table | |
155 // to quickly decide if a device is a touch device or not. We also maintain a | |
156 // list of the touch devices. Ideally, there will be only one touch device, | |
157 // and instead of having the lookup table and the list, there will be a single | |
158 // identifier for the touch device. This can be completed after enough testing | |
159 // on real touch devices. | |
160 | |
161 static const int kMaxDeviceNum = 128; | |
162 | |
163 // A quick lookup table for determining if events from the pointer device | |
164 // should be processed. | |
165 std::bitset<kMaxDeviceNum> pointer_device_lookup_; | |
166 | |
167 // A quick lookup table for determining if a device is a touch device. | |
168 std::bitset<kMaxDeviceNum> touch_device_lookup_; | |
169 | |
170 // The list of touch devices. | |
171 std::vector<int> touch_device_list_; | |
172 | |
173 // Index table to find the valuator for the TouchParam on the specific device | |
174 // by valuator_lookup_[device_id][touch_params]. Use 2-D array to get fast | |
175 // index at the expense of space. If the kMaxDeviceNum grows larger that the | |
176 // space waste becomes a concern, the 2D lookup table can be replaced by a | |
177 // hash map. | |
178 signed char valuator_lookup_[kMaxDeviceNum][TP_LAST_ENTRY]; | |
179 | |
180 // Index table to find the min & max value of the TouchParam on a specific | |
181 // device. | |
182 int touch_param_min_[kMaxDeviceNum][TP_LAST_ENTRY]; | |
183 int touch_param_max_[kMaxDeviceNum][TP_LAST_ENTRY]; | |
184 | |
185 // Maximum simultaneous touch points. | |
186 static const int kMaxTouchPoints = 32; | |
187 | |
188 // A lookup table for slots in use for a touch event. | |
189 std::bitset<kMaxTouchPoints> slots_used_; | |
190 | |
191 DISALLOW_COPY_AND_ASSIGN(TouchFactory); | |
192 }; | |
193 | |
194 } // namespace views | |
195 | |
196 #endif // VIEWS_TOUCHUI_TOUCH_FACTORY_H_ | |
OLD | NEW |