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 "base/message_pump_glib_x.h"
6
7#include <gdk/gdkx.h>
8#if defined(HAVE_XINPUT2)
9#include <X11/extensions/XInput2.h>
10#else
11#include <X11/Xlib.h>
12#endif
13
14#include "base/message_pump_glib_x_dispatch.h"
15
16namespace {
17
18gboolean PlaceholderDispatch(GSource* source,
19                             GSourceFunc cb,
20                             gpointer data) {
21  return TRUE;
22}
23
24#if defined(HAVE_XINPUT2)
25
26// Setup XInput2 select for the GtkWidget.
27gboolean GtkWidgetRealizeCallback(GSignalInvocationHint* hint, guint nparams,
28                                  const GValue* pvalues, gpointer data) {
29  GtkWidget* widget = GTK_WIDGET(g_value_get_object(pvalues));
30  GdkWindow* window = widget->window;
31  base::MessagePumpGlibX* msgpump = static_cast<base::MessagePumpGlibX*>(data);
32
33  DCHECK(window);  // TODO(sad): Remove once determined if necessary.
34
35  if (GDK_WINDOW_TYPE(window) != GDK_WINDOW_TOPLEVEL &&
36      GDK_WINDOW_TYPE(window) != GDK_WINDOW_CHILD &&
37      GDK_WINDOW_TYPE(window) != GDK_WINDOW_DIALOG)
38    return true;
39
40  // TODO(sad): Do we need to set a flag on |window| to make sure we don't
41  // select for the same GdkWindow multiple times? Does it matter?
42  msgpump->SetupXInput2ForXWindow(GDK_WINDOW_XID(window));
43
44  return true;
45}
46
47// We need to capture all the GDK windows that get created, and start
48// listening for XInput2 events. So we setup a callback to the 'realize'
49// signal for GTK+ widgets, so that whenever the signal triggers for any
50// GtkWidget, which means the GtkWidget should now have a GdkWindow, we can
51// setup XInput2 events for the GdkWindow.
52static guint realize_signal_id = 0;
53static guint realize_hook_id = 0;
54
55void SetupGtkWidgetRealizeNotifier(base::MessagePumpGlibX* msgpump) {
56  gpointer klass = g_type_class_ref(GTK_TYPE_WIDGET);
57
58  g_signal_parse_name("realize", GTK_TYPE_WIDGET,
59                      &realize_signal_id, NULL, FALSE);
60  realize_hook_id = g_signal_add_emission_hook(realize_signal_id, 0,
61      GtkWidgetRealizeCallback, static_cast<gpointer>(msgpump), NULL);
62
63  g_type_class_unref(klass);
64}
65
66void RemoveGtkWidgetRealizeNotifier() {
67  if (realize_signal_id != 0)
68    g_signal_remove_emission_hook(realize_signal_id, realize_hook_id);
69  realize_signal_id = 0;
70  realize_hook_id = 0;
71}
72
73#endif  // HAVE_XINPUT2
74
75}  // namespace
76
77namespace base {
78
79MessagePumpGlibX::MessagePumpGlibX() : base::MessagePumpForUI(),
80#if defined(HAVE_XINPUT2)
81    xiopcode_(-1),
82    pointer_devices_(),
83#endif
84    gdksource_(NULL),
85    dispatching_event_(false),
86    capture_x_events_(0),
87    capture_gdk_events_(0) {
88  gdk_event_handler_set(&EventDispatcherX, this, NULL);
89
90#if defined(HAVE_XINPUT2)
91  InitializeXInput2();
92#endif
93  InitializeEventsToCapture();
94}
95
96MessagePumpGlibX::~MessagePumpGlibX() {
97#if defined(HAVE_XINPUT2)
98  RemoveGtkWidgetRealizeNotifier();
99#endif
100}
101
102#if defined(HAVE_XINPUT2)
103void MessagePumpGlibX::SetupXInput2ForXWindow(Window xwindow) {
104  Display* xdisplay = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
105
106  // Setup mask for mouse events.
107  unsigned char mask[(XI_LASTEVENT + 7)/8];
108  memset(mask, 0, sizeof(mask));
109
110  XISetMask(mask, XI_ButtonPress);
111  XISetMask(mask, XI_ButtonRelease);
112  XISetMask(mask, XI_Motion);
113
114  XIEventMask evmasks[pointer_devices_.size()];
115  int count = 0;
116  for (std::set<int>::const_iterator iter = pointer_devices_.begin();
117       iter != pointer_devices_.end();
118       ++iter, ++count) {
119    evmasks[count].deviceid = *iter;
120    evmasks[count].mask_len = sizeof(mask);
121    evmasks[count].mask = mask;
122  }
123
124  XISelectEvents(xdisplay, xwindow, evmasks, pointer_devices_.size());
125
126  // TODO(sad): Setup masks for keyboard events.
127
128  XFlush(xdisplay);
129}
130#endif  // HAVE_XINPUT2
131
132bool MessagePumpGlibX::RunOnce(GMainContext* context, bool block) {
133  GdkDisplay* gdisp = gdk_display_get_default();
134  if (!gdisp || !GetDispatcher())
135    return MessagePumpForUI::RunOnce(context, block);
136
137  Display* display = GDK_DISPLAY_XDISPLAY(gdisp);
138  bool should_quit = false;
139
140  if (XPending(display)) {
141    XEvent xev;
142    XPeekEvent(display, &xev);
143    if (capture_x_events_[xev.type]
144#if defined(HAVE_XINPUT2)
145        && (xev.type != GenericEvent || xev.xcookie.extension == xiopcode_)
146#endif
147        ) {
148      XNextEvent(display, &xev);
149
150#if defined(HAVE_XINPUT2)
151      bool have_cookie = false;
152      if (xev.type == GenericEvent &&
153          XGetEventData(xev.xgeneric.display, &xev.xcookie)) {
154        have_cookie = true;
155      }
156#endif
157
158      MessagePumpGlibXDispatcher::DispatchStatus status =
159          static_cast<MessagePumpGlibXDispatcher*>
160          (GetDispatcher())->DispatchX(&xev);
161
162      if (status == MessagePumpGlibXDispatcher::EVENT_QUIT) {
163        should_quit = true;
164        Quit();
165      } else if (status == MessagePumpGlibXDispatcher::EVENT_IGNORED) {
166        DLOG(WARNING) << "Event (" << xev.type << ") not handled.";
167
168        // TODO(sad): It is necessary to put back the event so that the default
169        // GDK events handler can take care of it. Without this, it is
170        // impossible to use the omnibox at the moment. However, this will
171        // eventually be removed once the omnibox code is updated for touchui.
172        XPutBackEvent(display, &xev);
173        if (gdksource_)
174          gdksource_->source_funcs->dispatch = gdkdispatcher_;
175        g_main_context_iteration(context, FALSE);
176      }
177
178#if defined(HAVE_XINPUT2)
179      if (have_cookie) {
180        XFreeEventData(xev.xgeneric.display, &xev.xcookie);
181      }
182#endif
183    } else {
184      // TODO(sad): A couple of extra events can still sneak in during this.
185      // Those should be sent back to the X queue from the dispatcher
186      // EventDispatcherX.
187      if (gdksource_)
188        gdksource_->source_funcs->dispatch = gdkdispatcher_;
189      g_main_context_iteration(context, FALSE);
190    }
191  }
192
193  if (should_quit)
194    return true;
195
196  bool retvalue;
197  if (gdksource_) {
198    // Replace the dispatch callback of the GDK event source temporarily so that
199    // it doesn't read events from X.
200    gboolean (*cb)(GSource*, GSourceFunc, void*) =
201        gdksource_->source_funcs->dispatch;
202    gdksource_->source_funcs->dispatch = PlaceholderDispatch;
203
204    dispatching_event_ = true;
205    retvalue = g_main_context_iteration(context, block);
206    dispatching_event_ = false;
207
208    gdksource_->source_funcs->dispatch = cb;
209  } else {
210    retvalue = g_main_context_iteration(context, block);
211  }
212
213  return retvalue;
214}
215
216void MessagePumpGlibX::EventDispatcherX(GdkEvent* event, gpointer data) {
217  MessagePumpGlibX* pump_x = reinterpret_cast<MessagePumpGlibX*>(data);
218
219  if (!pump_x->gdksource_) {
220    pump_x->gdksource_ = g_main_current_source();
221    if (pump_x->gdksource_)
222      pump_x->gdkdispatcher_ = pump_x->gdksource_->source_funcs->dispatch;
223  } else if (!pump_x->IsDispatchingEvent()) {
224    if (event->type != GDK_NOTHING &&
225        pump_x->capture_gdk_events_[event->type]) {
226      // TODO(sad): An X event is caught by the GDK handler. Put it back in the
227      // X queue so that we catch it in the next iteration. When done, the
228      // following DLOG statement will be removed.
229      DLOG(WARNING) << "GDK received an event it shouldn't have";
230    }
231  }
232
233  pump_x->DispatchEvents(event);
234}
235
236void MessagePumpGlibX::InitializeEventsToCapture(void) {
237  // TODO(sad): Decide which events we want to capture and update the tables
238  // accordingly.
239  capture_x_events_[KeyPress] = true;
240  capture_gdk_events_[GDK_KEY_PRESS] = true;
241
242  capture_x_events_[KeyRelease] = true;
243  capture_gdk_events_[GDK_KEY_RELEASE] = true;
244
245  capture_x_events_[ButtonPress] = true;
246  capture_gdk_events_[GDK_BUTTON_PRESS] = true;
247
248  capture_x_events_[ButtonRelease] = true;
249  capture_gdk_events_[GDK_BUTTON_RELEASE] = true;
250
251  capture_x_events_[MotionNotify] = true;
252  capture_gdk_events_[GDK_MOTION_NOTIFY] = true;
253
254#if defined(HAVE_XINPUT2)
255  capture_x_events_[GenericEvent] = true;
256#endif
257}
258
259#if defined(HAVE_XINPUT2)
260void MessagePumpGlibX::InitializeXInput2(void) {
261  GdkDisplay* display = gdk_display_get_default();
262  if (!display)
263    return;
264
265  Display* xdisplay = GDK_DISPLAY_XDISPLAY(display);
266  int event, err;
267
268  if (!XQueryExtension(xdisplay, "XInputExtension", &xiopcode_, &event, &err)) {
269    DLOG(WARNING) << "X Input extension not available.";
270    xiopcode_ = -1;
271    return;
272  }
273
274  int major = 2, minor = 0;
275  if (XIQueryVersion(xdisplay, &major, &minor) == BadRequest) {
276    DLOG(WARNING) << "XInput2 not supported in the server.";
277    xiopcode_ = -1;
278    return;
279  }
280
281  // TODO(sad): Here, we only setup so that the X windows created by GTK+ are
282  // setup for XInput2 events. We need a way to listen for XInput2 events for X
283  // windows created by other means (e.g. for context menus).
284  SetupGtkWidgetRealizeNotifier(this);
285
286  // Instead of asking X for the list of devices all the time, let's maintain a
287  // list of pointer devices we care about.
288  // It is not necessary to select for slave devices. XInput2 provides enough
289  // information to the event callback to decide which slave device triggered
290  // the event, thus decide whether the 'pointer event' is a 'mouse event' or a
291  // 'touch event'.
292  // If the touch device has 'GrabDevice' set and 'SendCoreEvents' unset (which
293  // is possible), then the device is detected as a floating device, and a
294  // floating device is not connected to a master device. So it is necessary to
295  // also select on the floating devices.
296  int count = 0;
297  XIDeviceInfo* devices = XIQueryDevice(xdisplay, XIAllDevices, &count);
298  for (int i = 0; i < count; i++) {
299    XIDeviceInfo* devinfo = devices + i;
300    if (devinfo->use == XIFloatingSlave || devinfo->use == XIMasterPointer)
301      pointer_devices_.insert(devinfo->deviceid);
302  }
303  XIFreeDeviceInfo(devices);
304
305  // TODO(sad): Select on root for XI_HierarchyChanged so that floats_ and
306  // masters_ can be kept up-to-date. This is a relatively rare event, so we can
307  // put it off for a later time.
308  // Note: It is not necessary to listen for XI_DeviceChanged events.
309}
310#endif  // HAVE_XINPUT2
311
312}  // namespace base
313