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 "ui/base/touch/touch_factory.h" | 5 #include "ui/base/touch/touch_factory.h" |
6 | 6 |
7 #include <X11/cursorfont.h> | 7 #include <X11/cursorfont.h> |
8 #include <X11/extensions/XInput.h> | 8 #include <X11/extensions/XInput.h> |
9 #include <X11/extensions/XInput2.h> | 9 #include <X11/extensions/XInput2.h> |
10 #include <X11/extensions/XIproto.h> | 10 #include <X11/extensions/XIproto.h> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/message_loop.h" | 15 #include "base/message_loop.h" |
16 #include "ui/base/x/x11_util.h" | 16 #include "ui/base/x/x11_util.h" |
17 | 17 |
18 #if defined(TOOLKIT_USES_GTK) | |
19 // TODO(sad) Remove all TOOLKIT_USES_GTK uses once we move to aura only. | |
20 #include <gtk/gtk.h> | |
21 #include <gdk/gdkx.h> | |
22 #endif | |
23 | |
24 namespace { | 18 namespace { |
25 | 19 |
26 // The X cursor is hidden if it is idle for kCursorIdleSeconds seconds. | 20 // The X cursor is hidden if it is idle for kCursorIdleSeconds seconds. |
27 int kCursorIdleSeconds = 5; | 21 int kCursorIdleSeconds = 5; |
28 | 22 |
29 // Given the TouchParam, return the correspoding XIValuatorClassInfo using | 23 // Given the TouchParam, return the correspoding XIValuatorClassInfo using |
30 // the X device information through Atom name matching. | 24 // the X device information through Atom name matching. |
31 XIValuatorClassInfo* FindTPValuator(Display* display, | 25 XIValuatorClassInfo* FindTPValuator(Display* display, |
32 XIDeviceInfo* info, | 26 XIDeviceInfo* info, |
33 ui::TouchFactory::TouchParam tp) { | 27 ui::TouchFactory::TouchParam tp) { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 reinterpret_cast<XIValuatorClassInfo*>(info->classes[i]); | 64 reinterpret_cast<XIValuatorClassInfo*>(info->classes[i]); |
71 | 65 |
72 const char* atom = XGetAtomName(display, v->label); | 66 const char* atom = XGetAtomName(display, v->label); |
73 if (atom && strcmp(atom, atom_tp) == 0) | 67 if (atom && strcmp(atom, atom_tp) == 0) |
74 return v; | 68 return v; |
75 } | 69 } |
76 | 70 |
77 return NULL; | 71 return NULL; |
78 } | 72 } |
79 | 73 |
80 #if defined(TOOLKIT_USES_GTK) | |
81 // Setup XInput2 select for the GtkWidget. | |
82 gboolean GtkWidgetRealizeCallback(GSignalInvocationHint* hint, guint nparams, | |
83 const GValue* pvalues, gpointer data) { | |
84 GtkWidget* widget = GTK_WIDGET(g_value_get_object(pvalues)); | |
85 GdkWindow* window = widget->window; | |
86 ui::TouchFactory* factory = static_cast<ui::TouchFactory*>(data); | |
87 | |
88 if (GDK_WINDOW_TYPE(window) != GDK_WINDOW_TOPLEVEL && | |
89 GDK_WINDOW_TYPE(window) != GDK_WINDOW_CHILD && | |
90 GDK_WINDOW_TYPE(window) != GDK_WINDOW_DIALOG) | |
91 return true; | |
92 | |
93 factory->SetupXI2ForXWindow(GDK_WINDOW_XID(window)); | |
94 return true; | |
95 } | |
96 | |
97 // We need to capture all the GDK windows that get created, and start | |
98 // listening for XInput2 events. So we setup a callback to the 'realize' | |
99 // signal for GTK+ widgets, so that whenever the signal triggers for any | |
100 // GtkWidget, which means the GtkWidget should now have a GdkWindow, we can | |
101 // setup XInput2 events for the GdkWindow. | |
102 guint realize_signal_id = 0; | |
103 guint realize_hook_id = 0; | |
104 | |
105 void SetupGtkWidgetRealizeNotifier(ui::TouchFactory* factory) { | |
106 gpointer klass = g_type_class_ref(GTK_TYPE_WIDGET); | |
107 | |
108 g_signal_parse_name("realize", GTK_TYPE_WIDGET, | |
109 &realize_signal_id, NULL, FALSE); | |
110 realize_hook_id = g_signal_add_emission_hook(realize_signal_id, 0, | |
111 GtkWidgetRealizeCallback, static_cast<gpointer>(factory), NULL); | |
112 | |
113 g_type_class_unref(klass); | |
114 } | |
115 | |
116 void RemoveGtkWidgetRealizeNotifier() { | |
117 if (realize_signal_id != 0) | |
118 g_signal_remove_emission_hook(realize_signal_id, realize_hook_id); | |
119 realize_signal_id = 0; | |
120 realize_hook_id = 0; | |
121 } | |
122 #endif | |
123 | |
124 } // namespace | 74 } // namespace |
125 | 75 |
126 namespace ui { | 76 namespace ui { |
127 | 77 |
128 // static | 78 // static |
129 TouchFactory* TouchFactory::GetInstance() { | 79 TouchFactory* TouchFactory::GetInstance() { |
130 return Singleton<TouchFactory>::get(); | 80 return Singleton<TouchFactory>::get(); |
131 } | 81 } |
132 | 82 |
133 TouchFactory::TouchFactory() | 83 TouchFactory::TouchFactory() |
(...skipping 17 matching lines...) Expand all Loading... |
151 Display* display = ui::GetXDisplay(); | 101 Display* display = ui::GetXDisplay(); |
152 Pixmap blank = XCreateBitmapFromData(display, ui::GetX11RootWindow(), | 102 Pixmap blank = XCreateBitmapFromData(display, ui::GetX11RootWindow(), |
153 nodata, 8, 8); | 103 nodata, 8, 8); |
154 invisible_cursor_ = XCreatePixmapCursor(display, blank, blank, | 104 invisible_cursor_ = XCreatePixmapCursor(display, blank, blank, |
155 &black, &black, 0, 0); | 105 &black, &black, 0, 0); |
156 arrow_cursor_ = XCreateFontCursor(display, XC_arrow); | 106 arrow_cursor_ = XCreateFontCursor(display, XC_arrow); |
157 | 107 |
158 SetCursorVisible(false, false); | 108 SetCursorVisible(false, false); |
159 UpdateDeviceList(display); | 109 UpdateDeviceList(display); |
160 | 110 |
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 | 111 // Make sure the list of devices is kept up-to-date by listening for |
168 // XI_HierarchyChanged event on the root window. | 112 // XI_HierarchyChanged event on the root window. |
169 unsigned char mask[XIMaskLen(XI_LASTEVENT)]; | 113 unsigned char mask[XIMaskLen(XI_LASTEVENT)]; |
170 memset(mask, 0, sizeof(mask)); | 114 memset(mask, 0, sizeof(mask)); |
171 | 115 |
172 XISetMask(mask, XI_HierarchyChanged); | 116 XISetMask(mask, XI_HierarchyChanged); |
173 | 117 |
174 XIEventMask evmask; | 118 XIEventMask evmask; |
175 evmask.deviceid = XIAllDevices; | 119 evmask.deviceid = XIAllDevices; |
176 evmask.mask_len = sizeof(mask); | 120 evmask.mask_len = sizeof(mask); |
177 evmask.mask = mask; | 121 evmask.mask = mask; |
178 XISelectEvents(display, ui::GetX11RootWindow(), &evmask, 1); | 122 XISelectEvents(display, ui::GetX11RootWindow(), &evmask, 1); |
179 } | 123 } |
180 | 124 |
181 TouchFactory::~TouchFactory() { | 125 TouchFactory::~TouchFactory() { |
182 #if defined(TOUCH_UI) | 126 #if defined(TOUCH_UI) |
183 if (!base::MessagePumpForUI::HasXInput2()) | 127 if (!base::MessagePumpForUI::HasXInput2()) |
184 return; | 128 return; |
185 #endif | 129 #endif |
186 | 130 |
187 SetCursorVisible(true, false); | 131 SetCursorVisible(true, false); |
188 Display* display = ui::GetXDisplay(); | 132 Display* display = ui::GetXDisplay(); |
189 XFreeCursor(display, invisible_cursor_); | 133 XFreeCursor(display, invisible_cursor_); |
190 XFreeCursor(display, arrow_cursor_); | 134 XFreeCursor(display, arrow_cursor_); |
191 | |
192 #if defined(TOOLKIT_USES_GTK) | |
193 RemoveGtkWidgetRealizeNotifier(); | |
194 #endif | |
195 } | 135 } |
196 | 136 |
197 void TouchFactory::UpdateDeviceList(Display* display) { | 137 void TouchFactory::UpdateDeviceList(Display* display) { |
198 // Detect touch devices. | 138 // Detect touch devices. |
199 // NOTE: The new API for retrieving the list of devices (XIQueryDevice) does | 139 // 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 | 140 // not provide enough information to detect a touch device. As a result, the |
201 // old version of query function (XListInputDevices) is used instead. | 141 // old version of query function (XListInputDevices) is used instead. |
202 // If XInput2 is not supported, this will return null (with count of -1) so | 142 // If XInput2 is not supported, this will return null (with count of -1) so |
203 // we assume there cannot be any touch devices. | 143 // we assume there cannot be any touch devices. |
204 int count = 0; | 144 int count = 0; |
(...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
538 float* max) { | 478 float* max) { |
539 if (valuator_lookup_[deviceid][tp] >= 0) { | 479 if (valuator_lookup_[deviceid][tp] >= 0) { |
540 *min = touch_param_min_[deviceid][tp]; | 480 *min = touch_param_min_[deviceid][tp]; |
541 *max = touch_param_max_[deviceid][tp]; | 481 *max = touch_param_max_[deviceid][tp]; |
542 return true; | 482 return true; |
543 } | 483 } |
544 return false; | 484 return false; |
545 } | 485 } |
546 | 486 |
547 } // namespace ui | 487 } // namespace ui |
OLD | NEW |