| 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/ozone/platform/wayland/wayland_display.h" | |
| 6 | |
| 7 #include <xdg-shell-unstable-v5-client-protocol.h> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/memory/ptr_util.h" | |
| 12 #include "base/message_loop/message_loop.h" | |
| 13 #include "ui/ozone/platform/wayland/wayland_window.h" | |
| 14 | |
| 15 static_assert(XDG_SHELL_VERSION_CURRENT == 5, "Unsupported xdg-shell version"); | |
| 16 | |
| 17 namespace ui { | |
| 18 namespace { | |
| 19 const uint32_t kMaxCompositorVersion = 4; | |
| 20 const uint32_t kMaxSeatVersion = 4; | |
| 21 const uint32_t kMaxShmVersion = 1; | |
| 22 const uint32_t kMaxXdgShellVersion = 1; | |
| 23 } // namespace | |
| 24 | |
| 25 WaylandDisplay::WaylandDisplay() {} | |
| 26 | |
| 27 WaylandDisplay::~WaylandDisplay() {} | |
| 28 | |
| 29 bool WaylandDisplay::Initialize() { | |
| 30 static const wl_registry_listener registry_listener = { | |
| 31 &WaylandDisplay::Global, &WaylandDisplay::GlobalRemove, | |
| 32 }; | |
| 33 | |
| 34 display_.reset(wl_display_connect(nullptr)); | |
| 35 if (!display_) { | |
| 36 LOG(ERROR) << "Failed to connect to Wayland display"; | |
| 37 return false; | |
| 38 } | |
| 39 | |
| 40 registry_.reset(wl_display_get_registry(display_.get())); | |
| 41 if (!registry_) { | |
| 42 LOG(ERROR) << "Failed to get Wayland registry"; | |
| 43 return false; | |
| 44 } | |
| 45 | |
| 46 wl_registry_add_listener(registry_.get(), ®istry_listener, this); | |
| 47 wl_display_roundtrip(display_.get()); | |
| 48 | |
| 49 if (!compositor_) { | |
| 50 LOG(ERROR) << "No wl_compositor object"; | |
| 51 return false; | |
| 52 } | |
| 53 if (!shm_) { | |
| 54 LOG(ERROR) << "No wl_shm object"; | |
| 55 return false; | |
| 56 } | |
| 57 if (!seat_) { | |
| 58 LOG(ERROR) << "No wl_seat object"; | |
| 59 return false; | |
| 60 } | |
| 61 if (!shell_) { | |
| 62 LOG(ERROR) << "No xdg_shell object"; | |
| 63 return false; | |
| 64 } | |
| 65 | |
| 66 return true; | |
| 67 } | |
| 68 | |
| 69 bool WaylandDisplay::StartProcessingEvents() { | |
| 70 if (watching_) | |
| 71 return true; | |
| 72 | |
| 73 DCHECK(display_); | |
| 74 wl_display_flush(display_.get()); | |
| 75 | |
| 76 DCHECK(base::MessageLoopForUI::IsCurrent()); | |
| 77 if (!base::MessageLoopForUI::current()->WatchFileDescriptor( | |
| 78 wl_display_get_fd(display_.get()), true, | |
| 79 base::MessagePumpLibevent::WATCH_READ, &controller_, this)) | |
| 80 return false; | |
| 81 | |
| 82 watching_ = true; | |
| 83 return true; | |
| 84 } | |
| 85 | |
| 86 void WaylandDisplay::ScheduleFlush() { | |
| 87 if (scheduled_flush_ || !watching_) | |
| 88 return; | |
| 89 base::MessageLoopForUI::current()->task_runner()->PostTask( | |
| 90 FROM_HERE, base::Bind(&WaylandDisplay::Flush, base::Unretained(this))); | |
| 91 scheduled_flush_ = true; | |
| 92 } | |
| 93 | |
| 94 WaylandWindow* WaylandDisplay::GetWindow(gfx::AcceleratedWidget widget) { | |
| 95 auto it = window_map_.find(widget); | |
| 96 return it == window_map_.end() ? nullptr : it->second; | |
| 97 } | |
| 98 | |
| 99 void WaylandDisplay::AddWindow(gfx::AcceleratedWidget widget, | |
| 100 WaylandWindow* window) { | |
| 101 window_map_[widget] = window; | |
| 102 } | |
| 103 | |
| 104 void WaylandDisplay::RemoveWindow(gfx::AcceleratedWidget widget) { | |
| 105 window_map_.erase(widget); | |
| 106 } | |
| 107 | |
| 108 void WaylandDisplay::OnDispatcherListChanged() { | |
| 109 StartProcessingEvents(); | |
| 110 } | |
| 111 | |
| 112 void WaylandDisplay::Flush() { | |
| 113 wl_display_flush(display_.get()); | |
| 114 scheduled_flush_ = false; | |
| 115 } | |
| 116 | |
| 117 void WaylandDisplay::DispatchUiEvent(Event* event) { | |
| 118 PlatformEventSource::DispatchEvent(event); | |
| 119 } | |
| 120 | |
| 121 void WaylandDisplay::OnFileCanReadWithoutBlocking(int fd) { | |
| 122 wl_display_dispatch(display_.get()); | |
| 123 for (const auto& window : window_map_) | |
| 124 window.second->ApplyPendingBounds(); | |
| 125 } | |
| 126 | |
| 127 void WaylandDisplay::OnFileCanWriteWithoutBlocking(int fd) {} | |
| 128 | |
| 129 // static | |
| 130 void WaylandDisplay::Global(void* data, | |
| 131 wl_registry* registry, | |
| 132 uint32_t name, | |
| 133 const char* interface, | |
| 134 uint32_t version) { | |
| 135 static const wl_seat_listener seat_listener = { | |
| 136 &WaylandDisplay::Capabilities, &WaylandDisplay::Name, | |
| 137 }; | |
| 138 static const xdg_shell_listener shell_listener = { | |
| 139 &WaylandDisplay::Ping, | |
| 140 }; | |
| 141 | |
| 142 WaylandDisplay* display = static_cast<WaylandDisplay*>(data); | |
| 143 if (!display->compositor_ && strcmp(interface, "wl_compositor") == 0) { | |
| 144 display->compositor_ = wl::Bind<wl_compositor>( | |
| 145 registry, name, std::min(version, kMaxCompositorVersion)); | |
| 146 if (!display->compositor_) | |
| 147 LOG(ERROR) << "Failed to bind to wl_compositor global"; | |
| 148 } else if (!display->shm_ && strcmp(interface, "wl_shm") == 0) { | |
| 149 display->shm_ = | |
| 150 wl::Bind<wl_shm>(registry, name, std::min(version, kMaxShmVersion)); | |
| 151 if (!display->shm_) | |
| 152 LOG(ERROR) << "Failed to bind to wl_shm global"; | |
| 153 } else if (!display->seat_ && strcmp(interface, "wl_seat") == 0) { | |
| 154 display->seat_ = | |
| 155 wl::Bind<wl_seat>(registry, name, std::min(version, kMaxSeatVersion)); | |
| 156 if (!display->seat_) { | |
| 157 LOG(ERROR) << "Failed to bind to wl_seat global"; | |
| 158 return; | |
| 159 } | |
| 160 wl_seat_add_listener(display->seat_.get(), &seat_listener, display); | |
| 161 } else if (!display->shell_ && strcmp(interface, "xdg_shell") == 0) { | |
| 162 display->shell_ = wl::Bind<xdg_shell>( | |
| 163 registry, name, std::min(version, kMaxXdgShellVersion)); | |
| 164 if (!display->shell_) { | |
| 165 LOG(ERROR) << "Failed to bind to xdg_shell global"; | |
| 166 return; | |
| 167 } | |
| 168 xdg_shell_add_listener(display->shell_.get(), &shell_listener, display); | |
| 169 xdg_shell_use_unstable_version(display->shell_.get(), | |
| 170 XDG_SHELL_VERSION_CURRENT); | |
| 171 } | |
| 172 | |
| 173 display->ScheduleFlush(); | |
| 174 } | |
| 175 | |
| 176 // static | |
| 177 void WaylandDisplay::GlobalRemove(void* data, | |
| 178 wl_registry* registry, | |
| 179 uint32_t name) { | |
| 180 NOTIMPLEMENTED(); | |
| 181 } | |
| 182 | |
| 183 // static | |
| 184 void WaylandDisplay::Capabilities(void* data, | |
| 185 wl_seat* seat, | |
| 186 uint32_t capabilities) { | |
| 187 WaylandDisplay* display = static_cast<WaylandDisplay*>(data); | |
| 188 if (capabilities & WL_SEAT_CAPABILITY_POINTER) { | |
| 189 if (!display->pointer_) { | |
| 190 wl_pointer* pointer = wl_seat_get_pointer(display->seat_.get()); | |
| 191 if (!pointer) { | |
| 192 LOG(ERROR) << "Failed to get wl_pointer from seat"; | |
| 193 return; | |
| 194 } | |
| 195 display->pointer_ = base::WrapUnique(new WaylandPointer( | |
| 196 pointer, base::Bind(&WaylandDisplay::DispatchUiEvent, | |
| 197 base::Unretained(display)))); | |
| 198 } | |
| 199 } else if (display->pointer_) { | |
| 200 display->pointer_.reset(); | |
| 201 } | |
| 202 display->ScheduleFlush(); | |
| 203 } | |
| 204 | |
| 205 // static | |
| 206 void WaylandDisplay::Name(void* data, wl_seat* seat, const char* name) {} | |
| 207 | |
| 208 // static | |
| 209 void WaylandDisplay::Ping(void* data, xdg_shell* shell, uint32_t serial) { | |
| 210 WaylandDisplay* display = static_cast<WaylandDisplay*>(data); | |
| 211 xdg_shell_pong(shell, serial); | |
| 212 display->ScheduleFlush(); | |
| 213 } | |
| 214 | |
| 215 } // namespace ui | |
| OLD | NEW |