Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(66)

Side by Side Diff: ui/platform_window/x11/x11_window.cc

Issue 391583002: ui/platform_window: Introduce PlatformWindow for windowing primitives. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: self-nits Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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/platform_window/x11/x11_window.h"
6
7 #include <X11/extensions/XInput2.h>
8 #include <X11/Xatom.h>
9 #include <X11/Xlib.h>
10 #include <X11/Xutil.h>
11
12 #include "ui/events/event.h"
13 #include "ui/events/event_utils.h"
14 #include "ui/events/platform/platform_event_dispatcher.h"
15 #include "ui/events/platform/platform_event_source.h"
16 #include "ui/events/platform/x11/x11_event_source.h"
17 #include "ui/gfx/geometry/rect.h"
18 #include "ui/gfx/x/x11_atom_cache.h"
19 #include "ui/gfx/x/x11_types.h"
20 #include "ui/platform_window/platform_window_delegate.h"
21
22 namespace ui {
23
24 namespace {
25
26 const char* kAtomsToCache[] = {
27 "WM_DELETE_WINDOW",
28 "_NET_WM_PING",
29 "_NET_WM_PID",
30 NULL
31 };
32
33 XID FindXEventTarget(XEvent* xevent) {
34 XID target = xevent->xany.window;
35 if (xevent->type == GenericEvent)
36 target = static_cast<XIDeviceEvent*>(xevent->xcookie.data)->event;
37 return target;
38 }
39
40 } // namespace
41
42 X11Window::X11Window(PlatformWindowDelegate* delegate)
43 : delegate_(delegate),
44 xdisplay_(gfx::GetXDisplay()),
45 xwindow_(None),
46 xroot_window_(DefaultRootWindow(xdisplay_)),
47 atom_cache_(xdisplay_, kAtomsToCache),
48 window_mapped_(false) {
49 CHECK(delegate_);
50 }
51
52 X11Window::~X11Window() {
53 Destroy();
54 }
55
56 void X11Window::Destroy() {
57 if (xwindow_ == None)
58 return;
59
60 // Stop processing events.
61 PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(this);
62 XDestroyWindow(xdisplay_, xwindow_);
63 xwindow_ = None;
64 }
65
66 void X11Window::Show() {
67 if (window_mapped_)
68 return;
69
70 CHECK(PlatformEventSource::GetInstance());
71 PlatformEventSource::GetInstance()->AddPlatformEventDispatcher(this);
72
73 XSetWindowAttributes swa;
74 memset(&swa, 0, sizeof(swa));
75 swa.background_pixmap = None;
76 swa.override_redirect = False;
77 xwindow_ = XCreateWindow(xdisplay_,
78 xroot_window_,
79 requested_bounds_.x(),
80 requested_bounds_.y(),
81 requested_bounds_.width(),
82 requested_bounds_.height(),
83 0, // border width
84 CopyFromParent, // depth
85 InputOutput,
86 CopyFromParent, // visual
87 CWBackPixmap | CWOverrideRedirect,
88 &swa);
89
90 long event_mask = ButtonPressMask | ButtonReleaseMask | FocusChangeMask |
91 KeyPressMask | KeyReleaseMask | EnterWindowMask |
92 LeaveWindowMask | ExposureMask | VisibilityChangeMask |
93 StructureNotifyMask | PropertyChangeMask |
94 PointerMotionMask;
95 XSelectInput(xdisplay_, xwindow_, event_mask);
96
97 unsigned char mask[XIMaskLen(XI_LASTEVENT)];
98 memset(mask, 0, sizeof(mask));
99
100 XISetMask(mask, XI_TouchBegin);
101 XISetMask(mask, XI_TouchUpdate);
102 XISetMask(mask, XI_TouchEnd);
103 XISetMask(mask, XI_ButtonPress);
104 XISetMask(mask, XI_ButtonRelease);
105 XISetMask(mask, XI_Motion);
106
107 XIEventMask evmask;
108 evmask.deviceid = XIAllDevices;
109 evmask.mask_len = sizeof(mask);
110 evmask.mask = mask;
111 XISelectEvents(xdisplay_, xwindow_, &evmask, 1);
112 XFlush(xdisplay_);
113
114 ::Atom protocols[2];
115 protocols[0] = atom_cache_.GetAtom("WM_DELETE_WINDOW");
116 protocols[1] = atom_cache_.GetAtom("_NET_WM_PING");
117 XSetWMProtocols(xdisplay_, xwindow_, protocols, 2);
118
119 // We need a WM_CLIENT_MACHINE and WM_LOCALE_NAME value so we integrate with
120 // the desktop environment.
121 XSetWMProperties(
122 xdisplay_, xwindow_, NULL, NULL, NULL, 0, NULL, NULL, NULL);
123
124 // Likewise, the X server needs to know this window's pid so it knows which
125 // program to kill if the window hangs.
126 // XChangeProperty() expects "pid" to be long.
127 COMPILE_ASSERT(sizeof(long) >= sizeof(pid_t), pid_t_bigger_than_long);
128 long pid = getpid();
129 XChangeProperty(xdisplay_,
130 xwindow_,
131 atom_cache_.GetAtom("_NET_WM_PID"),
132 XA_CARDINAL,
133 32,
134 PropModeReplace,
135 reinterpret_cast<unsigned char*>(&pid),
136 1);
137 // Before we map the window, set size hints. Otherwise, some window managers
138 // will ignore toplevel XMoveWindow commands.
139 XSizeHints size_hints;
140 size_hints.flags = PPosition | PWinGravity;
141 size_hints.x = requested_bounds_.x();
142 size_hints.y = requested_bounds_.y();
143 // Set StaticGravity so that the window position is not affected by the
144 // frame width when running with window manager.
145 size_hints.win_gravity = StaticGravity;
146 XSetWMNormalHints(xdisplay_, xwindow_, &size_hints);
147
148 delegate_->OnAcceleratedWidgetAvailable(xwindow_);
149
150 XMapWindow(xdisplay_, xwindow_);
151
152 // We now block until our window is mapped. Some X11 APIs will crash and
153 // burn if passed |xwindow_| before the window is mapped, and XMapWindow is
154 // asynchronous.
155 if (X11EventSource::GetInstance())
156 X11EventSource::GetInstance()->BlockUntilWindowMapped(xwindow_);
157 window_mapped_ = true;
158 }
159
160 void X11Window::Hide() {
161 if (!window_mapped_)
162 return;
163 XWithdrawWindow(xdisplay_, xwindow_, 0);
164 window_mapped_ = false;
165 }
166
167 void X11Window::Close() {
168 Destroy();
169 }
170
171 void X11Window::SetBounds(const gfx::Rect& bounds) {
172 requested_bounds_ = bounds;
173 if (!window_mapped_)
174 return;
175 XWindowChanges changes = {0};
176 unsigned value_mask = CWX | CWY | CWWidth | CWHeight;
177 changes.x = bounds.x();
178 changes.y = bounds.y();
179 changes.width = bounds.width();
180 changes.height = bounds.height();
181 XConfigureWindow(xdisplay_, xwindow_, value_mask, &changes);
182 }
183
184 void X11Window::SetCapture() {}
185
186 void X11Window::ReleaseCapture() {}
187
188 void X11Window::ToggleFullscreen() {}
189
190 void X11Window::Maximize() {}
191
192 void X11Window::Minimize() {}
193
194 void X11Window::Restore() {}
195
196 bool X11Window::CanDispatchEvent(const PlatformEvent& event) {
197 return FindXEventTarget(event) == xwindow_;
198 }
199
200 uint32_t X11Window::DispatchEvent(const PlatformEvent& event) {
201 XEvent* xev = event;
202 switch (xev->type) {
203 case EnterNotify: {
204 // EnterNotify creates ET_MOUSE_MOVED. Mark as synthesized as this is
205 // not real mouse move event.
206 MouseEvent mouse_event(xev);
207 CHECK_EQ(ET_MOUSE_MOVED, mouse_event.type());
208 mouse_event.set_flags(mouse_event.flags() | EF_IS_SYNTHESIZED);
209 delegate_->DispatchEvent(&mouse_event);
210 break;
211 }
212 case LeaveNotify: {
213 MouseEvent mouse_event(xev);
214 delegate_->DispatchEvent(&mouse_event);
215 break;
216 }
217
218 case Expose: {
219 gfx::Rect damage_rect(xev->xexpose.x,
220 xev->xexpose.y,
221 xev->xexpose.width,
222 xev->xexpose.height);
223 delegate_->OnDamageRect(damage_rect);
224 break;
225 }
226
227 case KeyPress:
228 case KeyRelease: {
229 KeyEvent key_event(xev, false);
230 delegate_->DispatchEvent(&key_event);
231 break;
232 }
233
234 case ButtonPress:
235 case ButtonRelease: {
236 switch (EventTypeFromNative(xev)) {
237 case ET_MOUSEWHEEL: {
238 MouseWheelEvent mouseev(xev);
239 delegate_->DispatchEvent(&mouseev);
240 break;
241 }
242 case ET_MOUSE_PRESSED:
243 case ET_MOUSE_RELEASED: {
244 MouseEvent mouseev(xev);
245 delegate_->DispatchEvent(&mouseev);
246 break;
247 }
248 case ET_UNKNOWN:
249 // No event is created for X11-release events for mouse-wheel
250 // buttons.
251 break;
252 default:
253 NOTREACHED();
254 }
255 break;
256 }
257
258 case FocusOut:
259 if (xev->xfocus.mode != NotifyGrab)
260 delegate_->OnLostCapture();
261 break;
262
263 case ConfigureNotify: {
264 DCHECK_EQ(xwindow_, xev->xconfigure.event);
265 DCHECK_EQ(xwindow_, xev->xconfigure.window);
266 gfx::Rect bounds(xev->xconfigure.x,
267 xev->xconfigure.y,
268 xev->xconfigure.width,
269 xev->xconfigure.height);
270 delegate_->OnBoundsChanged(bounds);
271 break;
272 }
273
274 case ClientMessage: {
275 Atom message = static_cast<Atom>(xev->xclient.data.l[0]);
276 if (message == atom_cache_.GetAtom("WM_DELETE_WINDOW")) {
277 delegate_->OnCloseRequest();
278 } else if (message == atom_cache_.GetAtom("_NET_WM_PING")) {
279 XEvent reply_event = *xev;
280 reply_event.xclient.window = xroot_window_;
281
282 XSendEvent(xdisplay_,
283 reply_event.xclient.window,
284 False,
285 SubstructureRedirectMask | SubstructureNotifyMask,
286 &reply_event);
287 XFlush(xdisplay_);
288 }
289 break;
290 }
291 }
292 return POST_DISPATCH_STOP_PROPAGATION;
293 }
294
295 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698