OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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_base.h" |
| 6 |
| 7 #include <X11/Xatom.h> |
| 8 #include <X11/Xlib.h> |
| 9 #include <X11/Xutil.h> |
| 10 #include <X11/extensions/XInput2.h> |
| 11 |
| 12 #include <string> |
| 13 |
| 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "ui/events/devices/x11/touch_factory_x11.h" |
| 16 #include "ui/events/event.h" |
| 17 #include "ui/events/event_utils.h" |
| 18 #include "ui/events/platform/platform_event_dispatcher.h" |
| 19 #include "ui/events/platform/platform_event_source.h" |
| 20 #include "ui/events/platform/x11/x11_event_source.h" |
| 21 #include "ui/events/x/events_x_utils.h" |
| 22 #include "ui/gfx/geometry/rect.h" |
| 23 #include "ui/gfx/x/x11_atom_cache.h" |
| 24 #include "ui/gfx/x/x11_types.h" |
| 25 #include "ui/platform_window/platform_window_delegate.h" |
| 26 |
| 27 namespace ui { |
| 28 |
| 29 namespace { |
| 30 |
| 31 const char* kAtomsToCache[] = {"UTF8_STRING", "WM_DELETE_WINDOW", |
| 32 "_NET_WM_NAME", "_NET_WM_PID", |
| 33 "_NET_WM_PING", NULL}; |
| 34 |
| 35 bool g_override_redirect = false; |
| 36 |
| 37 } // namespace |
| 38 |
| 39 X11WindowBase::X11WindowBase(PlatformWindowDelegate* delegate) |
| 40 : delegate_(delegate), |
| 41 xdisplay_(gfx::GetXDisplay()), |
| 42 xwindow_(None), |
| 43 xroot_window_(DefaultRootWindow(xdisplay_)), |
| 44 atom_cache_(xdisplay_, kAtomsToCache) { |
| 45 DCHECK(delegate_); |
| 46 TouchFactory::SetTouchDeviceListFromCommandLine(); |
| 47 } |
| 48 |
| 49 X11WindowBase::~X11WindowBase() { |
| 50 Destroy(); |
| 51 } |
| 52 |
| 53 void X11WindowBase::Destroy() { |
| 54 if (xwindow_ == None) |
| 55 return; |
| 56 |
| 57 // Stop processing events. |
| 58 XID xwindow = xwindow_; |
| 59 XDisplay* xdisplay = xdisplay_; |
| 60 xwindow_ = None; |
| 61 delegate_->OnClosed(); |
| 62 // |this| might be deleted because of the above call. |
| 63 |
| 64 XDestroyWindow(xdisplay, xwindow); |
| 65 } |
| 66 |
| 67 void X11WindowBase::Create() { |
| 68 XSetWindowAttributes swa; |
| 69 memset(&swa, 0, sizeof(swa)); |
| 70 swa.background_pixmap = None; |
| 71 swa.bit_gravity = NorthWestGravity; |
| 72 swa.override_redirect = g_override_redirect; |
| 73 xwindow_ = XCreateWindow( |
| 74 xdisplay_, xroot_window_, requested_bounds_.x(), requested_bounds_.y(), |
| 75 requested_bounds_.width(), requested_bounds_.height(), |
| 76 0, // border width |
| 77 CopyFromParent, // depth |
| 78 InputOutput, |
| 79 CopyFromParent, // visual |
| 80 CWBackPixmap | CWBitGravity | CWOverrideRedirect, &swa); |
| 81 |
| 82 long event_mask = ButtonPressMask | ButtonReleaseMask | FocusChangeMask | |
| 83 KeyPressMask | KeyReleaseMask | EnterWindowMask | |
| 84 LeaveWindowMask | ExposureMask | VisibilityChangeMask | |
| 85 StructureNotifyMask | PropertyChangeMask | |
| 86 PointerMotionMask; |
| 87 XSelectInput(xdisplay_, xwindow_, event_mask); |
| 88 |
| 89 unsigned char mask[XIMaskLen(XI_LASTEVENT)]; |
| 90 memset(mask, 0, sizeof(mask)); |
| 91 |
| 92 XISetMask(mask, XI_TouchBegin); |
| 93 XISetMask(mask, XI_TouchUpdate); |
| 94 XISetMask(mask, XI_TouchEnd); |
| 95 XISetMask(mask, XI_ButtonPress); |
| 96 XISetMask(mask, XI_ButtonRelease); |
| 97 XISetMask(mask, XI_Motion); |
| 98 XISetMask(mask, XI_KeyPress); |
| 99 XISetMask(mask, XI_KeyRelease); |
| 100 |
| 101 XIEventMask evmask; |
| 102 evmask.deviceid = XIAllDevices; |
| 103 evmask.mask_len = sizeof(mask); |
| 104 evmask.mask = mask; |
| 105 XISelectEvents(xdisplay_, xwindow_, &evmask, 1); |
| 106 XFlush(xdisplay_); |
| 107 |
| 108 ::Atom protocols[2]; |
| 109 protocols[0] = atom_cache_.GetAtom("WM_DELETE_WINDOW"); |
| 110 protocols[1] = atom_cache_.GetAtom("_NET_WM_PING"); |
| 111 XSetWMProtocols(xdisplay_, xwindow_, protocols, 2); |
| 112 |
| 113 // We need a WM_CLIENT_MACHINE and WM_LOCALE_NAME value so we integrate with |
| 114 // the desktop environment. |
| 115 XSetWMProperties(xdisplay_, xwindow_, NULL, NULL, NULL, 0, NULL, NULL, NULL); |
| 116 |
| 117 // Likewise, the X server needs to know this window's pid so it knows which |
| 118 // program to kill if the window hangs. |
| 119 // XChangeProperty() expects "pid" to be long. |
| 120 static_assert(sizeof(long) >= sizeof(pid_t), |
| 121 "pid_t should not be larger than long"); |
| 122 long pid = getpid(); |
| 123 XChangeProperty(xdisplay_, xwindow_, atom_cache_.GetAtom("_NET_WM_PID"), |
| 124 XA_CARDINAL, 32, PropModeReplace, |
| 125 reinterpret_cast<unsigned char*>(&pid), 1); |
| 126 // Before we map the window, set size hints. Otherwise, some window managers |
| 127 // will ignore toplevel XMoveWindow commands. |
| 128 XSizeHints size_hints; |
| 129 size_hints.flags = PPosition | PWinGravity; |
| 130 size_hints.x = requested_bounds_.x(); |
| 131 size_hints.y = requested_bounds_.y(); |
| 132 // Set StaticGravity so that the window position is not affected by the |
| 133 // frame width when running with window manager. |
| 134 size_hints.win_gravity = StaticGravity; |
| 135 XSetWMNormalHints(xdisplay_, xwindow_, &size_hints); |
| 136 |
| 137 // TODO(sky): provide real scale factor. |
| 138 delegate_->OnAcceleratedWidgetAvailable(xwindow_, 1.f); |
| 139 } |
| 140 |
| 141 void X11WindowBase::Show() { |
| 142 if (window_mapped_) |
| 143 return; |
| 144 if (xwindow_ == None) |
| 145 Create(); |
| 146 |
| 147 XMapWindow(xdisplay_, xwindow_); |
| 148 |
| 149 // We now block until our window is mapped. Some X11 APIs will crash and |
| 150 // burn if passed |xwindow_| before the window is mapped, and XMapWindow is |
| 151 // asynchronous. |
| 152 if (X11EventSource::GetInstance()) |
| 153 X11EventSource::GetInstance()->BlockUntilWindowMapped(xwindow_); |
| 154 window_mapped_ = true; |
| 155 } |
| 156 |
| 157 void X11WindowBase::Hide() { |
| 158 if (!window_mapped_) |
| 159 return; |
| 160 XWithdrawWindow(xdisplay_, xwindow_, 0); |
| 161 window_mapped_ = false; |
| 162 } |
| 163 |
| 164 void X11WindowBase::SetBounds(const gfx::Rect& bounds) { |
| 165 requested_bounds_ = bounds; |
| 166 if (!window_mapped_ || bounds == confirmed_bounds_) |
| 167 return; |
| 168 XWindowChanges changes = {0}; |
| 169 unsigned value_mask = CWX | CWY | CWWidth | CWHeight; |
| 170 changes.x = bounds.x(); |
| 171 changes.y = bounds.y(); |
| 172 changes.width = bounds.width(); |
| 173 changes.height = bounds.height(); |
| 174 XConfigureWindow(xdisplay_, xwindow_, value_mask, &changes); |
| 175 } |
| 176 |
| 177 gfx::Rect X11WindowBase::GetBounds() { |
| 178 return confirmed_bounds_; |
| 179 } |
| 180 |
| 181 void X11WindowBase::SetTitle(const base::string16& title) { |
| 182 if (window_title_ == title) |
| 183 return; |
| 184 window_title_ = title; |
| 185 std::string utf8str = base::UTF16ToUTF8(title); |
| 186 XChangeProperty(xdisplay_, xwindow_, atom_cache_.GetAtom("_NET_WM_NAME"), |
| 187 atom_cache_.GetAtom("UTF8_STRING"), 8, PropModeReplace, |
| 188 reinterpret_cast<const unsigned char*>(utf8str.c_str()), |
| 189 utf8str.size()); |
| 190 XTextProperty xtp; |
| 191 char* c_utf8_str = const_cast<char*>(utf8str.c_str()); |
| 192 if (Xutf8TextListToTextProperty(xdisplay_, &c_utf8_str, 1, XUTF8StringStyle, |
| 193 &xtp) == Success) { |
| 194 XSetWMName(xdisplay_, xwindow_, &xtp); |
| 195 XFree(xtp.value); |
| 196 } |
| 197 } |
| 198 |
| 199 void X11WindowBase::SetCapture() {} |
| 200 |
| 201 void X11WindowBase::ReleaseCapture() {} |
| 202 |
| 203 void X11WindowBase::ToggleFullscreen() {} |
| 204 |
| 205 void X11WindowBase::Maximize() {} |
| 206 |
| 207 void X11WindowBase::Minimize() {} |
| 208 |
| 209 void X11WindowBase::Restore() {} |
| 210 |
| 211 void X11WindowBase::MoveCursorTo(const gfx::Point& location) {} |
| 212 |
| 213 void X11WindowBase::ConfineCursorToBounds(const gfx::Rect& bounds) {} |
| 214 |
| 215 PlatformImeController* X11WindowBase::GetPlatformImeController() { |
| 216 return nullptr; |
| 217 } |
| 218 |
| 219 void X11WindowBase::ProcessXWindowEvent(XEvent* xev) { |
| 220 switch (xev->type) { |
| 221 case Expose: { |
| 222 gfx::Rect damage_rect(xev->xexpose.x, xev->xexpose.y, xev->xexpose.width, |
| 223 xev->xexpose.height); |
| 224 delegate_->OnDamageRect(damage_rect); |
| 225 break; |
| 226 } |
| 227 |
| 228 case FocusOut: |
| 229 if (xev->xfocus.mode != NotifyGrab) |
| 230 delegate_->OnLostCapture(); |
| 231 break; |
| 232 |
| 233 case ConfigureNotify: { |
| 234 DCHECK_EQ(xwindow_, xev->xconfigure.event); |
| 235 DCHECK_EQ(xwindow_, xev->xconfigure.window); |
| 236 gfx::Rect bounds(xev->xconfigure.x, xev->xconfigure.y, |
| 237 xev->xconfigure.width, xev->xconfigure.height); |
| 238 if (confirmed_bounds_ != bounds) { |
| 239 confirmed_bounds_ = bounds; |
| 240 delegate_->OnBoundsChanged(confirmed_bounds_); |
| 241 } |
| 242 break; |
| 243 } |
| 244 |
| 245 case ClientMessage: { |
| 246 Atom message = static_cast<Atom>(xev->xclient.data.l[0]); |
| 247 if (message == atom_cache_.GetAtom("WM_DELETE_WINDOW")) { |
| 248 delegate_->OnCloseRequest(); |
| 249 } else if (message == atom_cache_.GetAtom("_NET_WM_PING")) { |
| 250 XEvent reply_event = *xev; |
| 251 reply_event.xclient.window = xroot_window_; |
| 252 |
| 253 XSendEvent(xdisplay_, reply_event.xclient.window, False, |
| 254 SubstructureRedirectMask | SubstructureNotifyMask, |
| 255 &reply_event); |
| 256 XFlush(xdisplay_); |
| 257 } |
| 258 break; |
| 259 } |
| 260 } |
| 261 } |
| 262 |
| 263 namespace test { |
| 264 |
| 265 void SetUseOverrideRedirectWindowByDefault(bool override_redirect) { |
| 266 g_override_redirect = override_redirect; |
| 267 } |
| 268 |
| 269 } // namespace test |
| 270 } // namespace ui |
OLD | NEW |