Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / ui / events / platform / x11 / x11_event_source.cc
1 // Copyright 2014 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 "ui/events/platform/x11/x11_event_source.h"
6
7 #include <X11/extensions/XInput2.h>
8 #include <X11/X.h>
9 #include <X11/Xlib.h>
10 #include <X11/XKBlib.h>
11
12 #include "base/logging.h"
13 #include "ui/events/event_utils.h"
14 #include "ui/events/platform/platform_event_dispatcher.h"
15 #include "ui/gfx/x/x11_types.h"
16
17 namespace ui {
18
19 namespace {
20
21 int g_xinput_opcode = -1;
22
23 bool InitializeXInput2(XDisplay* display) {
24   if (!display)
25     return false;
26
27   int event, err;
28
29   int xiopcode;
30   if (!XQueryExtension(display, "XInputExtension", &xiopcode, &event, &err)) {
31     DVLOG(1) << "X Input extension not available.";
32     return false;
33   }
34   g_xinput_opcode = xiopcode;
35
36 #if defined(USE_XI2_MT)
37   // USE_XI2_MT also defines the required XI2 minor minimum version.
38   int major = 2, minor = USE_XI2_MT;
39 #else
40   int major = 2, minor = 0;
41 #endif
42   if (XIQueryVersion(display, &major, &minor) == BadRequest) {
43     DVLOG(1) << "XInput2 not supported in the server.";
44     return false;
45   }
46 #if defined(USE_XI2_MT)
47   if (major < 2 || (major == 2 && minor < USE_XI2_MT)) {
48     DVLOG(1) << "XI version on server is " << major << "." << minor << ". "
49             << "But 2." << USE_XI2_MT << " is required.";
50     return false;
51   }
52 #endif
53
54   return true;
55 }
56
57 bool InitializeXkb(XDisplay* display) {
58   if (!display)
59     return false;
60
61   int opcode, event, error;
62   int major = XkbMajorVersion;
63   int minor = XkbMinorVersion;
64   if (!XkbQueryExtension(display, &opcode, &event, &error, &major, &minor)) {
65     DVLOG(1) << "Xkb extension not available.";
66     return false;
67   }
68
69   // Ask the server not to send KeyRelease event when the user holds down a key.
70   // crbug.com/138092
71   Bool supported_return;
72   if (!XkbSetDetectableAutoRepeat(display, True, &supported_return)) {
73     DVLOG(1) << "XKB not supported in the server.";
74     return false;
75   }
76
77   return true;
78 }
79
80 }  // namespace
81
82 X11EventSource::X11EventSource(XDisplay* display)
83     : display_(display) {
84   CHECK(display_);
85   InitializeXInput2(display_);
86   InitializeXkb(display_);
87 }
88
89 X11EventSource::~X11EventSource() {
90 }
91
92 // static
93 X11EventSource* X11EventSource::GetInstance() {
94   return static_cast<X11EventSource*>(PlatformEventSource::GetInstance());
95 }
96
97 ////////////////////////////////////////////////////////////////////////////////
98 // X11EventSource, public
99
100 void X11EventSource::DispatchXEvents() {
101   DCHECK(display_);
102   // Handle all pending events.
103   // It may be useful to eventually align this event dispatch with vsync, but
104   // not yet.
105   while (XPending(display_)) {
106     XEvent xevent;
107     XNextEvent(display_, &xevent);
108     uint32_t action = DispatchEvent(&xevent);
109     if (action & POST_DISPATCH_QUIT_LOOP)
110       break;
111   }
112 }
113
114 void X11EventSource::BlockUntilWindowMapped(XID window) {
115   XEvent event;
116   do {
117     // Block until there's a message of |event_mask| type on |w|. Then remove
118     // it from the queue and stuff it in |event|.
119     XWindowEvent(display_, window, StructureNotifyMask, &event);
120     DispatchEvent(&event);
121   } while (event.type != MapNotify);
122 }
123
124 ////////////////////////////////////////////////////////////////////////////////
125 // X11EventSource, private
126
127 uint32_t X11EventSource::DispatchEvent(XEvent* xevent) {
128   bool have_cookie = false;
129   if (xevent->type == GenericEvent &&
130       XGetEventData(xevent->xgeneric.display, &xevent->xcookie)) {
131     have_cookie = true;
132   }
133
134   uint32_t action = PlatformEventSource::DispatchEvent(xevent);
135   if (xevent->type == GenericEvent &&
136       xevent->xgeneric.evtype == XI_HierarchyChanged) {
137     ui::UpdateDeviceList();
138   }
139
140   if (have_cookie)
141     XFreeEventData(xevent->xgeneric.display, &xevent->xcookie);
142   return action;
143 }
144
145 }  // namespace ui