Chromium Code Reviews| 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_connection.h" | |
| 6 | |
| 7 #include <xdg-shell-unstable-v5-client-protocol.h> | |
|
rjkroege
2016/07/08 21:59:19
As someone who may have to maintain this code, sho
Michael Forney
2016/07/08 22:28:44
I don't think you should be too concerned. xdg-she
| |
| 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 WaylandConnection::WaylandConnection() {} | |
| 27 | |
| 28 WaylandConnection::~WaylandConnection() { | |
| 29 for (WaylandOutput* output : output_list_) | |
| 30 delete output; | |
| 31 output_list_.clear(); | |
| 32 } | |
| 33 | |
| 34 bool WaylandConnection::Initialize() { | |
| 35 static const wl_registry_listener registry_listener = { | |
| 36 &WaylandConnection::Global, &WaylandConnection::GlobalRemove, | |
| 37 }; | |
| 38 | |
| 39 display_.reset(wl_display_connect(nullptr)); | |
| 40 if (!display_) { | |
| 41 LOG(ERROR) << "Failed to connect to Wayland display"; | |
| 42 return false; | |
| 43 } | |
| 44 | |
| 45 registry_.reset(wl_display_get_registry(display_.get())); | |
| 46 if (!registry_) { | |
| 47 LOG(ERROR) << "Failed to get Wayland registry"; | |
| 48 return false; | |
| 49 } | |
| 50 | |
| 51 wl_registry_add_listener(registry_.get(), ®istry_listener, this); | |
| 52 wl_display_roundtrip(display_.get()); | |
| 53 | |
| 54 if (!compositor_) { | |
| 55 LOG(ERROR) << "No wl_compositor object"; | |
| 56 return false; | |
| 57 } | |
| 58 if (!shm_) { | |
| 59 LOG(ERROR) << "No wl_shm object"; | |
| 60 return false; | |
| 61 } | |
| 62 if (!seat_) { | |
| 63 LOG(ERROR) << "No wl_seat object"; | |
| 64 return false; | |
| 65 } | |
| 66 if (!shell_) { | |
| 67 LOG(ERROR) << "No xdg_shell object"; | |
| 68 return false; | |
| 69 } | |
| 70 | |
| 71 return true; | |
| 72 } | |
| 73 | |
| 74 bool WaylandConnection::StartProcessingEvents() { | |
| 75 if (watching_) | |
| 76 return true; | |
| 77 | |
| 78 DCHECK(display_); | |
| 79 wl_display_flush(display_.get()); | |
| 80 | |
| 81 DCHECK(base::MessageLoopForUI::IsCurrent()); | |
| 82 if (!base::MessageLoopForUI::current()->WatchFileDescriptor( | |
| 83 wl_display_get_fd(display_.get()), true, | |
| 84 base::MessagePumpLibevent::WATCH_READ, &controller_, this)) | |
| 85 return false; | |
| 86 | |
| 87 watching_ = true; | |
| 88 return true; | |
| 89 } | |
| 90 | |
| 91 void WaylandConnection::ScheduleFlush() { | |
| 92 if (scheduled_flush_ || !watching_) | |
| 93 return; | |
| 94 base::MessageLoopForUI::current()->task_runner()->PostTask( | |
| 95 FROM_HERE, base::Bind(&WaylandConnection::Flush, base::Unretained(this))); | |
| 96 scheduled_flush_ = true; | |
| 97 } | |
| 98 | |
| 99 WaylandWindow* WaylandConnection::GetWindow(gfx::AcceleratedWidget widget) { | |
| 100 auto it = window_map_.find(widget); | |
| 101 return it == window_map_.end() ? nullptr : it->second; | |
| 102 } | |
| 103 | |
| 104 void WaylandConnection::AddWindow(gfx::AcceleratedWidget widget, | |
| 105 WaylandWindow* window) { | |
| 106 window_map_[widget] = window; | |
| 107 } | |
| 108 | |
| 109 void WaylandConnection::RemoveWindow(gfx::AcceleratedWidget widget) { | |
| 110 window_map_.erase(widget); | |
| 111 } | |
| 112 | |
| 113 WaylandOutput* WaylandConnection::PrimaryOutput() const { | |
| 114 if (!output_list_.size()) | |
| 115 return nullptr; | |
| 116 return output_list_.front(); | |
| 117 } | |
| 118 | |
| 119 void WaylandConnection::OnDispatcherListChanged() { | |
| 120 StartProcessingEvents(); | |
| 121 } | |
| 122 | |
| 123 void WaylandConnection::Flush() { | |
| 124 wl_display_flush(display_.get()); | |
| 125 scheduled_flush_ = false; | |
| 126 } | |
| 127 | |
| 128 void WaylandConnection::DispatchUiEvent(Event* event) { | |
| 129 PlatformEventSource::DispatchEvent(event); | |
| 130 } | |
| 131 | |
| 132 void WaylandConnection::OnFileCanReadWithoutBlocking(int fd) { | |
| 133 wl_display_dispatch(display_.get()); | |
| 134 for (const auto& window : window_map_) | |
| 135 window.second->ApplyPendingBounds(); | |
| 136 } | |
| 137 | |
| 138 void WaylandConnection::OnFileCanWriteWithoutBlocking(int fd) {} | |
| 139 | |
| 140 const std::vector<WaylandOutput*>& WaylandConnection::GetOutputList() const { | |
| 141 return output_list_; | |
| 142 } | |
| 143 | |
| 144 // static | |
| 145 void WaylandConnection::Global(void* data, | |
| 146 wl_registry* registry, | |
| 147 uint32_t name, | |
| 148 const char* interface, | |
| 149 uint32_t version) { | |
| 150 static const wl_seat_listener seat_listener = { | |
| 151 &WaylandConnection::Capabilities, &WaylandConnection::Name, | |
| 152 }; | |
| 153 static const xdg_shell_listener shell_listener = { | |
| 154 &WaylandConnection::Ping, | |
| 155 }; | |
| 156 | |
| 157 WaylandConnection* connection = static_cast<WaylandConnection*>(data); | |
| 158 if (!connection->compositor_ && strcmp(interface, "wl_compositor") == 0) { | |
| 159 connection->compositor_ = wl::Bind<wl_compositor>( | |
| 160 registry, name, std::min(version, kMaxCompositorVersion)); | |
| 161 if (!connection->compositor_) | |
| 162 LOG(ERROR) << "Failed to bind to wl_compositor global"; | |
| 163 } else if (!connection->shm_ && strcmp(interface, "wl_shm") == 0) { | |
| 164 connection->shm_ = | |
| 165 wl::Bind<wl_shm>(registry, name, std::min(version, kMaxShmVersion)); | |
| 166 if (!connection->shm_) | |
| 167 LOG(ERROR) << "Failed to bind to wl_shm global"; | |
| 168 } else if (!connection->seat_ && strcmp(interface, "wl_seat") == 0) { | |
| 169 connection->seat_ = | |
| 170 wl::Bind<wl_seat>(registry, name, std::min(version, kMaxSeatVersion)); | |
| 171 if (!connection->seat_) { | |
| 172 LOG(ERROR) << "Failed to bind to wl_seat global"; | |
| 173 return; | |
| 174 } | |
| 175 wl_seat_add_listener(connection->seat_.get(), &seat_listener, connection); | |
| 176 } else if (!connection->shell_ && strcmp(interface, "xdg_shell") == 0) { | |
| 177 connection->shell_ = wl::Bind<xdg_shell>( | |
| 178 registry, name, std::min(version, kMaxXdgShellVersion)); | |
| 179 if (!connection->shell_) { | |
| 180 LOG(ERROR) << "Failed to bind to xdg_shell global"; | |
| 181 return; | |
| 182 } | |
| 183 xdg_shell_add_listener(connection->shell_.get(), &shell_listener, | |
| 184 connection); | |
| 185 xdg_shell_use_unstable_version(connection->shell_.get(), | |
| 186 XDG_SHELL_VERSION_CURRENT); | |
| 187 } else if (strcmp(interface, "wl_output") == 0) { | |
| 188 wl::Object<wl_output> output = wl::Bind<wl_output>(registry, name, 1); | |
| 189 if (!output) { | |
| 190 LOG(ERROR) << "Failed to bind to wl_output global"; | |
| 191 return; | |
| 192 } | |
| 193 | |
| 194 WaylandOutput* wayland_output = new WaylandOutput(name, output.release()); | |
| 195 if (!connection->output_list_.empty()) | |
| 196 NOTIMPLEMENTED() << "Multiple screens support is not implemented"; | |
| 197 | |
| 198 connection->output_list_.push_back(wayland_output); | |
| 199 } | |
| 200 | |
| 201 connection->ScheduleFlush(); | |
| 202 } | |
| 203 | |
| 204 // static | |
| 205 void WaylandConnection::GlobalRemove(void* data, | |
| 206 wl_registry* registry, | |
| 207 uint32_t name) { | |
| 208 NOTIMPLEMENTED(); | |
| 209 } | |
| 210 | |
| 211 // static | |
| 212 void WaylandConnection::Capabilities(void* data, | |
| 213 wl_seat* seat, | |
| 214 uint32_t capabilities) { | |
| 215 WaylandConnection* connection = static_cast<WaylandConnection*>(data); | |
| 216 if (capabilities & WL_SEAT_CAPABILITY_POINTER) { | |
| 217 if (!connection->pointer_) { | |
| 218 wl_pointer* pointer = wl_seat_get_pointer(connection->seat_.get()); | |
| 219 if (!pointer) { | |
| 220 LOG(ERROR) << "Failed to get wl_pointer from seat"; | |
| 221 return; | |
| 222 } | |
| 223 connection->pointer_ = base::WrapUnique(new WaylandPointer( | |
| 224 pointer, base::Bind(&WaylandConnection::DispatchUiEvent, | |
| 225 base::Unretained(connection)))); | |
| 226 } | |
| 227 } else if (connection->pointer_) { | |
| 228 connection->pointer_.reset(); | |
| 229 } | |
| 230 connection->ScheduleFlush(); | |
| 231 } | |
| 232 | |
| 233 // static | |
| 234 void WaylandConnection::Name(void* data, wl_seat* seat, const char* name) {} | |
| 235 | |
| 236 // static | |
| 237 void WaylandConnection::Ping(void* data, xdg_shell* shell, uint32_t serial) { | |
| 238 WaylandConnection* connection = static_cast<WaylandConnection*>(data); | |
| 239 xdg_shell_pong(shell, serial); | |
| 240 connection->ScheduleFlush(); | |
| 241 } | |
| 242 | |
| 243 } // namespace ui | |
| OLD | NEW |