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