OLD | NEW |
(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 gfx::Rect X11Window::GetBounds() { |
| 185 return confirmed_bounds_; |
| 186 } |
| 187 |
| 188 void X11Window::SetCapture() {} |
| 189 |
| 190 void X11Window::ReleaseCapture() {} |
| 191 |
| 192 void X11Window::ToggleFullscreen() {} |
| 193 |
| 194 void X11Window::Maximize() {} |
| 195 |
| 196 void X11Window::Minimize() {} |
| 197 |
| 198 void X11Window::Restore() {} |
| 199 |
| 200 bool X11Window::CanDispatchEvent(const PlatformEvent& event) { |
| 201 return FindXEventTarget(event) == xwindow_; |
| 202 } |
| 203 |
| 204 uint32_t X11Window::DispatchEvent(const PlatformEvent& event) { |
| 205 XEvent* xev = event; |
| 206 switch (xev->type) { |
| 207 case EnterNotify: { |
| 208 // EnterNotify creates ET_MOUSE_MOVED. Mark as synthesized as this is |
| 209 // not real mouse move event. |
| 210 MouseEvent mouse_event(xev); |
| 211 CHECK_EQ(ET_MOUSE_MOVED, mouse_event.type()); |
| 212 mouse_event.set_flags(mouse_event.flags() | EF_IS_SYNTHESIZED); |
| 213 delegate_->DispatchEvent(&mouse_event); |
| 214 break; |
| 215 } |
| 216 case LeaveNotify: { |
| 217 MouseEvent mouse_event(xev); |
| 218 delegate_->DispatchEvent(&mouse_event); |
| 219 break; |
| 220 } |
| 221 |
| 222 case Expose: { |
| 223 gfx::Rect damage_rect(xev->xexpose.x, |
| 224 xev->xexpose.y, |
| 225 xev->xexpose.width, |
| 226 xev->xexpose.height); |
| 227 delegate_->OnDamageRect(damage_rect); |
| 228 break; |
| 229 } |
| 230 |
| 231 case KeyPress: |
| 232 case KeyRelease: { |
| 233 KeyEvent key_event(xev, false); |
| 234 delegate_->DispatchEvent(&key_event); |
| 235 break; |
| 236 } |
| 237 |
| 238 case ButtonPress: |
| 239 case ButtonRelease: { |
| 240 switch (EventTypeFromNative(xev)) { |
| 241 case ET_MOUSEWHEEL: { |
| 242 MouseWheelEvent mouseev(xev); |
| 243 delegate_->DispatchEvent(&mouseev); |
| 244 break; |
| 245 } |
| 246 case ET_MOUSE_PRESSED: |
| 247 case ET_MOUSE_RELEASED: { |
| 248 MouseEvent mouseev(xev); |
| 249 delegate_->DispatchEvent(&mouseev); |
| 250 break; |
| 251 } |
| 252 case ET_UNKNOWN: |
| 253 // No event is created for X11-release events for mouse-wheel |
| 254 // buttons. |
| 255 break; |
| 256 default: |
| 257 NOTREACHED(); |
| 258 } |
| 259 break; |
| 260 } |
| 261 |
| 262 case FocusOut: |
| 263 if (xev->xfocus.mode != NotifyGrab) |
| 264 delegate_->OnLostCapture(); |
| 265 break; |
| 266 |
| 267 case ConfigureNotify: { |
| 268 DCHECK_EQ(xwindow_, xev->xconfigure.event); |
| 269 DCHECK_EQ(xwindow_, xev->xconfigure.window); |
| 270 gfx::Rect bounds(xev->xconfigure.x, |
| 271 xev->xconfigure.y, |
| 272 xev->xconfigure.width, |
| 273 xev->xconfigure.height); |
| 274 if (confirmed_bounds_ != bounds) { |
| 275 confirmed_bounds_ = bounds; |
| 276 delegate_->OnBoundsChanged(confirmed_bounds_); |
| 277 } |
| 278 break; |
| 279 } |
| 280 |
| 281 case ClientMessage: { |
| 282 Atom message = static_cast<Atom>(xev->xclient.data.l[0]); |
| 283 if (message == atom_cache_.GetAtom("WM_DELETE_WINDOW")) { |
| 284 delegate_->OnCloseRequest(); |
| 285 } else if (message == atom_cache_.GetAtom("_NET_WM_PING")) { |
| 286 XEvent reply_event = *xev; |
| 287 reply_event.xclient.window = xroot_window_; |
| 288 |
| 289 XSendEvent(xdisplay_, |
| 290 reply_event.xclient.window, |
| 291 False, |
| 292 SubstructureRedirectMask | SubstructureNotifyMask, |
| 293 &reply_event); |
| 294 XFlush(xdisplay_); |
| 295 } |
| 296 break; |
| 297 } |
| 298 } |
| 299 return POST_DISPATCH_STOP_PROPAGATION; |
| 300 } |
| 301 |
| 302 } // namespace ui |
OLD | NEW |