1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/platform_window/x11/x11_window.h"
#include <X11/extensions/XInput2.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include "ui/events/devices/x11/touch_factory_x11.h"
#include "ui/events/event.h"
#include "ui/events/event_utils.h"
#include "ui/events/platform/platform_event_dispatcher.h"
#include "ui/events/platform/platform_event_source.h"
#include "ui/events/platform/x11/x11_event_source.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/x/x11_atom_cache.h"
#include "ui/gfx/x/x11_types.h"
#include "ui/platform_window/platform_window_delegate.h"
namespace ui {
namespace {
const char* kAtomsToCache[] = {
"WM_DELETE_WINDOW",
"_NET_WM_PING",
"_NET_WM_PID",
NULL
};
XID FindXEventTarget(XEvent* xevent) {
XID target = xevent->xany.window;
if (xevent->type == GenericEvent)
target = static_cast<XIDeviceEvent*>(xevent->xcookie.data)->event;
return target;
}
} // namespace
X11Window::X11Window(PlatformWindowDelegate* delegate)
: delegate_(delegate),
xdisplay_(gfx::GetXDisplay()),
xwindow_(None),
xroot_window_(DefaultRootWindow(xdisplay_)),
atom_cache_(xdisplay_, kAtomsToCache),
window_mapped_(false) {
CHECK(delegate_);
TouchFactory::SetTouchDeviceListFromCommandLine();
}
X11Window::~X11Window() {
Destroy();
}
void X11Window::Destroy() {
delegate_->OnClosed();
if (xwindow_ == None)
return;
// Stop processing events.
PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(this);
XDestroyWindow(xdisplay_, xwindow_);
xwindow_ = None;
}
void X11Window::ProcessXInput2Event(XEvent* xev) {
if (!TouchFactory::GetInstance()->ShouldProcessXI2Event(xev))
return;
EventType event_type = EventTypeFromNative(xev);
switch (event_type) {
case ET_KEY_PRESSED:
case ET_KEY_RELEASED: {
KeyEvent key_event(xev);
delegate_->DispatchEvent(&key_event);
break;
}
case ET_MOUSE_PRESSED:
case ET_MOUSE_MOVED:
case ET_MOUSE_DRAGGED:
case ET_MOUSE_RELEASED: {
MouseEvent mouse_event(xev);
delegate_->DispatchEvent(&mouse_event);
break;
}
case ET_MOUSEWHEEL: {
MouseWheelEvent wheel_event(xev);
delegate_->DispatchEvent(&wheel_event);
break;
}
case ET_SCROLL_FLING_START:
case ET_SCROLL_FLING_CANCEL:
case ET_SCROLL: {
ScrollEvent scroll_event(xev);
delegate_->DispatchEvent(&scroll_event);
break;
}
case ET_TOUCH_MOVED:
case ET_TOUCH_PRESSED:
case ET_TOUCH_CANCELLED:
case ET_TOUCH_RELEASED: {
TouchEvent touch_event(xev);
delegate_->DispatchEvent(&touch_event);
break;
}
default:
break;
}
}
void X11Window::Show() {
if (window_mapped_)
return;
CHECK(PlatformEventSource::GetInstance());
PlatformEventSource::GetInstance()->AddPlatformEventDispatcher(this);
XSetWindowAttributes swa;
memset(&swa, 0, sizeof(swa));
swa.background_pixmap = None;
swa.override_redirect = False;
xwindow_ = XCreateWindow(xdisplay_,
xroot_window_,
requested_bounds_.x(),
requested_bounds_.y(),
requested_bounds_.width(),
requested_bounds_.height(),
0, // border width
CopyFromParent, // depth
InputOutput,
CopyFromParent, // visual
CWBackPixmap | CWOverrideRedirect,
&swa);
long event_mask = ButtonPressMask | ButtonReleaseMask | FocusChangeMask |
KeyPressMask | KeyReleaseMask | EnterWindowMask |
LeaveWindowMask | ExposureMask | VisibilityChangeMask |
StructureNotifyMask | PropertyChangeMask |
PointerMotionMask;
XSelectInput(xdisplay_, xwindow_, event_mask);
unsigned char mask[XIMaskLen(XI_LASTEVENT)];
memset(mask, 0, sizeof(mask));
XISetMask(mask, XI_TouchBegin);
XISetMask(mask, XI_TouchUpdate);
XISetMask(mask, XI_TouchEnd);
XISetMask(mask, XI_ButtonPress);
XISetMask(mask, XI_ButtonRelease);
XISetMask(mask, XI_Motion);
XISetMask(mask, XI_KeyPress);
XISetMask(mask, XI_KeyRelease);
XIEventMask evmask;
evmask.deviceid = XIAllDevices;
evmask.mask_len = sizeof(mask);
evmask.mask = mask;
XISelectEvents(xdisplay_, xwindow_, &evmask, 1);
XFlush(xdisplay_);
::Atom protocols[2];
protocols[0] = atom_cache_.GetAtom("WM_DELETE_WINDOW");
protocols[1] = atom_cache_.GetAtom("_NET_WM_PING");
XSetWMProtocols(xdisplay_, xwindow_, protocols, 2);
// We need a WM_CLIENT_MACHINE and WM_LOCALE_NAME value so we integrate with
// the desktop environment.
XSetWMProperties(
xdisplay_, xwindow_, NULL, NULL, NULL, 0, NULL, NULL, NULL);
// Likewise, the X server needs to know this window's pid so it knows which
// program to kill if the window hangs.
// XChangeProperty() expects "pid" to be long.
COMPILE_ASSERT(sizeof(long) >= sizeof(pid_t), pid_t_bigger_than_long);
long pid = getpid();
XChangeProperty(xdisplay_,
xwindow_,
atom_cache_.GetAtom("_NET_WM_PID"),
XA_CARDINAL,
32,
PropModeReplace,
reinterpret_cast<unsigned char*>(&pid),
1);
// Before we map the window, set size hints. Otherwise, some window managers
// will ignore toplevel XMoveWindow commands.
XSizeHints size_hints;
size_hints.flags = PPosition | PWinGravity;
size_hints.x = requested_bounds_.x();
size_hints.y = requested_bounds_.y();
// Set StaticGravity so that the window position is not affected by the
// frame width when running with window manager.
size_hints.win_gravity = StaticGravity;
XSetWMNormalHints(xdisplay_, xwindow_, &size_hints);
delegate_->OnAcceleratedWidgetAvailable(xwindow_);
XMapWindow(xdisplay_, xwindow_);
// We now block until our window is mapped. Some X11 APIs will crash and
// burn if passed |xwindow_| before the window is mapped, and XMapWindow is
// asynchronous.
if (X11EventSource::GetInstance())
X11EventSource::GetInstance()->BlockUntilWindowMapped(xwindow_);
window_mapped_ = true;
}
void X11Window::Hide() {
if (!window_mapped_)
return;
XWithdrawWindow(xdisplay_, xwindow_, 0);
window_mapped_ = false;
}
void X11Window::Close() {
Destroy();
}
void X11Window::SetBounds(const gfx::Rect& bounds) {
requested_bounds_ = bounds;
if (!window_mapped_)
return;
XWindowChanges changes = {0};
unsigned value_mask = CWX | CWY | CWWidth | CWHeight;
changes.x = bounds.x();
changes.y = bounds.y();
changes.width = bounds.width();
changes.height = bounds.height();
XConfigureWindow(xdisplay_, xwindow_, value_mask, &changes);
}
gfx::Rect X11Window::GetBounds() {
return confirmed_bounds_;
}
void X11Window::SetCapture() {}
void X11Window::ReleaseCapture() {}
void X11Window::ToggleFullscreen() {}
void X11Window::Maximize() {}
void X11Window::Minimize() {}
void X11Window::Restore() {}
void X11Window::SetCursor(PlatformCursor cursor) {}
void X11Window::MoveCursorTo(const gfx::Point& location) {}
void X11Window::ConfineCursorToBounds(const gfx::Rect& bounds) {
}
bool X11Window::CanDispatchEvent(const PlatformEvent& event) {
return FindXEventTarget(event) == xwindow_;
}
uint32_t X11Window::DispatchEvent(const PlatformEvent& event) {
XEvent* xev = event;
switch (xev->type) {
case EnterNotify: {
// EnterNotify creates ET_MOUSE_MOVED. Mark as synthesized as this is
// not real mouse move event.
MouseEvent mouse_event(xev);
CHECK_EQ(ET_MOUSE_MOVED, mouse_event.type());
mouse_event.set_flags(mouse_event.flags() | EF_IS_SYNTHESIZED);
delegate_->DispatchEvent(&mouse_event);
break;
}
case LeaveNotify: {
MouseEvent mouse_event(xev);
delegate_->DispatchEvent(&mouse_event);
break;
}
case Expose: {
gfx::Rect damage_rect(xev->xexpose.x,
xev->xexpose.y,
xev->xexpose.width,
xev->xexpose.height);
delegate_->OnDamageRect(damage_rect);
break;
}
case KeyPress:
case KeyRelease: {
KeyEvent key_event(xev);
delegate_->DispatchEvent(&key_event);
break;
}
case ButtonPress:
case ButtonRelease: {
switch (EventTypeFromNative(xev)) {
case ET_MOUSEWHEEL: {
MouseWheelEvent mouseev(xev);
delegate_->DispatchEvent(&mouseev);
break;
}
case ET_MOUSE_PRESSED:
case ET_MOUSE_RELEASED: {
MouseEvent mouseev(xev);
delegate_->DispatchEvent(&mouseev);
break;
}
case ET_UNKNOWN:
// No event is created for X11-release events for mouse-wheel
// buttons.
break;
default:
NOTREACHED();
}
break;
}
case FocusOut:
if (xev->xfocus.mode != NotifyGrab)
delegate_->OnLostCapture();
break;
case ConfigureNotify: {
DCHECK_EQ(xwindow_, xev->xconfigure.event);
DCHECK_EQ(xwindow_, xev->xconfigure.window);
gfx::Rect bounds(xev->xconfigure.x,
xev->xconfigure.y,
xev->xconfigure.width,
xev->xconfigure.height);
if (confirmed_bounds_ != bounds) {
confirmed_bounds_ = bounds;
delegate_->OnBoundsChanged(confirmed_bounds_);
}
break;
}
case ClientMessage: {
Atom message = static_cast<Atom>(xev->xclient.data.l[0]);
if (message == atom_cache_.GetAtom("WM_DELETE_WINDOW")) {
delegate_->OnCloseRequest();
} else if (message == atom_cache_.GetAtom("_NET_WM_PING")) {
XEvent reply_event = *xev;
reply_event.xclient.window = xroot_window_;
XSendEvent(xdisplay_,
reply_event.xclient.window,
False,
SubstructureRedirectMask | SubstructureNotifyMask,
&reply_event);
XFlush(xdisplay_);
}
break;
}
case GenericEvent: {
ProcessXInput2Event(xev);
break;
}
}
return POST_DISPATCH_STOP_PROPAGATION;
}
} // namespace ui
|