OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/message_pump_x.h" | 5 #include "base/message_pump_x.h" |
6 | 6 |
7 #include <X11/extensions/XInput2.h> | 7 #include <X11/extensions/XInput2.h> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
(...skipping 19 matching lines...) Expand all Loading... | |
30 return pump->DispatchXEvents(); | 30 return pump->DispatchXEvents(); |
31 } | 31 } |
32 | 32 |
33 GSourceFuncs XSourceFuncs = { | 33 GSourceFuncs XSourceFuncs = { |
34 XSourcePrepare, | 34 XSourcePrepare, |
35 XSourceCheck, | 35 XSourceCheck, |
36 XSourceDispatch, | 36 XSourceDispatch, |
37 NULL | 37 NULL |
38 }; | 38 }; |
39 | 39 |
40 // The opcode used for checking events. | 40 // The status of XInput2 initialization. |
41 int xiopcode = -1; | 41 bool xinput2_initialized = false; |
Mark Mentovai
2012/05/17 14:17:34
This can just be a static bool inside InitializeXI
| |
42 bool xinput2_supported = false; | |
42 | 43 |
43 // The message-pump opens a connection to the display and owns it. | 44 // The message-pump opens a connection to the display and owns it. |
44 Display* g_xdisplay = NULL; | 45 Display* g_xdisplay = NULL; |
45 | 46 |
46 // The default dispatcher to process native events when no dispatcher | 47 // The default dispatcher to process native events when no dispatcher |
47 // is specified. | 48 // is specified. |
48 base::MessagePumpDispatcher* g_default_dispatcher = NULL; | 49 base::MessagePumpDispatcher* g_default_dispatcher = NULL; |
49 | 50 |
50 void InitializeXInput2(void) { | 51 void InitializeXInput2IfNeeded(void) { |
52 if (xinput2_initialized) | |
Mark Mentovai
2012/05/17 14:17:34
…but this pattern is a little bit of a pet peeve.
Rick Byers
2012/05/17 15:13:15
I agree this is better, thanks! Done.
| |
53 return; | |
54 xinput2_initialized = true; | |
55 | |
51 Display* display = base::MessagePumpX::GetDefaultXDisplay(); | 56 Display* display = base::MessagePumpX::GetDefaultXDisplay(); |
52 if (!display) | 57 if (!display) |
53 return; | 58 return; |
54 | 59 |
55 int event, err; | 60 int event, err; |
56 | 61 |
62 int xiopcode; | |
57 if (!XQueryExtension(display, "XInputExtension", &xiopcode, &event, &err)) { | 63 if (!XQueryExtension(display, "XInputExtension", &xiopcode, &event, &err)) { |
58 DVLOG(1) << "X Input extension not available."; | 64 DVLOG(1) << "X Input extension not available."; |
59 xiopcode = -1; | |
60 return; | 65 return; |
61 } | 66 } |
62 | 67 |
63 #if defined(USE_XI2_MT) | 68 #if defined(USE_XI2_MT) |
64 // USE_XI2_MT also defines the required XI2 minor minimum version. | 69 // USE_XI2_MT also defines the required XI2 minor minimum version. |
65 int major = 2, minor = USE_XI2_MT; | 70 int major = 2, minor = USE_XI2_MT; |
66 #else | 71 #else |
67 int major = 2, minor = 0; | 72 int major = 2, minor = 0; |
68 #endif | 73 #endif |
69 if (XIQueryVersion(display, &major, &minor) == BadRequest) { | 74 if (XIQueryVersion(display, &major, &minor) == BadRequest) { |
70 DVLOG(1) << "XInput2 not supported in the server."; | 75 DVLOG(1) << "XInput2 not supported in the server."; |
71 xiopcode = -1; | |
72 return; | 76 return; |
73 } | 77 } |
74 #if defined(USE_XI2_MT) | 78 #if defined(USE_XI2_MT) |
75 if (major < 2 || (major == 2 && minor < USE_XI2_MT)) { | 79 if (major < 2 || (major == 2 && minor < USE_XI2_MT)) { |
76 DVLOG(1) << "XI version on server is " << major << "." << minor << ". " | 80 DVLOG(1) << "XI version on server is " << major << "." << minor << ". " |
77 << "But 2." << USE_XI2_MT << " is required."; | 81 << "But 2." << USE_XI2_MT << " is required."; |
78 xiopcode = -1; | |
79 return; | 82 return; |
80 } | 83 } |
81 #endif | 84 #endif |
85 | |
86 xinput2_supported = true; | |
82 } | 87 } |
83 | 88 |
84 } // namespace | 89 } // namespace |
85 | 90 |
86 namespace base { | 91 namespace base { |
87 | 92 |
88 MessagePumpX::MessagePumpX() : MessagePumpGlib(), | 93 MessagePumpX::MessagePumpX() : MessagePumpGlib(), |
89 x_source_(NULL) { | 94 x_source_(NULL) { |
90 InitializeXInput2(); | 95 InitializeXInput2IfNeeded(); |
91 InitXSource(); | 96 InitXSource(); |
92 } | 97 } |
93 | 98 |
94 MessagePumpX::~MessagePumpX() { | 99 MessagePumpX::~MessagePumpX() { |
95 g_source_destroy(x_source_); | 100 g_source_destroy(x_source_); |
96 g_source_unref(x_source_); | 101 g_source_unref(x_source_); |
97 XCloseDisplay(g_xdisplay); | 102 XCloseDisplay(g_xdisplay); |
98 g_xdisplay = NULL; | 103 g_xdisplay = NULL; |
99 } | 104 } |
100 | 105 |
101 // static | 106 // static |
102 Display* MessagePumpX::GetDefaultXDisplay() { | 107 Display* MessagePumpX::GetDefaultXDisplay() { |
103 if (!g_xdisplay) | 108 if (!g_xdisplay) |
104 g_xdisplay = XOpenDisplay(NULL); | 109 g_xdisplay = XOpenDisplay(NULL); |
105 return g_xdisplay; | 110 return g_xdisplay; |
106 } | 111 } |
107 | 112 |
108 // static | 113 // static |
109 bool MessagePumpX::HasXInput2() { | 114 bool MessagePumpX::HasXInput2() { |
110 return xiopcode != -1; | 115 InitializeXInput2IfNeeded(); |
116 return xinput2_supported; | |
111 } | 117 } |
112 | 118 |
113 // static | 119 // static |
114 void MessagePumpX::SetDefaultDispatcher(MessagePumpDispatcher* dispatcher) { | 120 void MessagePumpX::SetDefaultDispatcher(MessagePumpDispatcher* dispatcher) { |
115 DCHECK(!g_default_dispatcher || !dispatcher); | 121 DCHECK(!g_default_dispatcher || !dispatcher); |
116 g_default_dispatcher = dispatcher; | 122 g_default_dispatcher = dispatcher; |
117 } | 123 } |
118 | 124 |
119 void MessagePumpX::InitXSource() { | 125 void MessagePumpX::InitXSource() { |
120 // CHECKs are to help track down crbug.com/113106. | 126 // CHECKs are to help track down crbug.com/113106. |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
185 return true; | 191 return true; |
186 } | 192 } |
187 return false; | 193 return false; |
188 } | 194 } |
189 | 195 |
190 void MessagePumpX::DidProcessXEvent(XEvent* xevent) { | 196 void MessagePumpX::DidProcessXEvent(XEvent* xevent) { |
191 FOR_EACH_OBSERVER(MessagePumpObserver, observers(), DidProcessEvent(xevent)); | 197 FOR_EACH_OBSERVER(MessagePumpObserver, observers(), DidProcessEvent(xevent)); |
192 } | 198 } |
193 | 199 |
194 } // namespace base | 200 } // namespace base |
OLD | NEW |