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