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 364 365 366 367 368 369
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/public/cpp/window_properties.h"
#include "ash/shell.h"
#include "ash/wm/desks/desks_util.h"
#include "ash/wm/work_area_insets.h"
#include "components/exo/wayland/server_util.h"
#include "components/exo/wayland/zcr_remote_shell_impl.h"
namespace exo::wayland {
namespace {
const struct zcr_remote_surface_v1_interface remote_surface_implementation = {
+[](wl_client* client, wl_resource* resource) {
wl_resource_destroy(resource);
},
zcr_remote_shell::remote_surface_set_app_id,
zcr_remote_shell::remote_surface_set_window_geometry,
zcr_remote_shell::remote_surface_set_scale,
zcr_remote_shell::remote_surface_set_rectangular_shadow_DEPRECATED,
zcr_remote_shell::
remote_surface_set_rectangular_shadow_background_opacity_DEPRECATED,
zcr_remote_shell::remote_surface_set_title,
zcr_remote_shell::remote_surface_set_top_inset,
zcr_remote_shell::remote_surface_activate,
zcr_remote_shell::remote_surface_maximize,
zcr_remote_shell::remote_surface_minimize,
zcr_remote_shell::remote_surface_restore,
zcr_remote_shell::remote_surface_fullscreen,
zcr_remote_shell::remote_surface_unfullscreen,
zcr_remote_shell::remote_surface_pin,
zcr_remote_shell::remote_surface_unpin,
zcr_remote_shell::remote_surface_set_system_modal,
zcr_remote_shell::remote_surface_unset_system_modal,
zcr_remote_shell::remote_surface_set_rectangular_surface_shadow,
zcr_remote_shell::remote_surface_set_systemui_visibility,
zcr_remote_shell::remote_surface_set_always_on_top,
zcr_remote_shell::remote_surface_unset_always_on_top,
zcr_remote_shell::remote_surface_ack_configure_DEPRECATED,
zcr_remote_shell::remote_surface_move_DEPRECATED,
zcr_remote_shell::remote_surface_set_orientation,
zcr_remote_shell::remote_surface_set_window_type,
zcr_remote_shell::remote_surface_resize_DEPRECATED,
zcr_remote_shell::remote_surface_set_resize_outset_DEPRECATED,
zcr_remote_shell::remote_surface_start_move,
zcr_remote_shell::remote_surface_set_can_maximize,
zcr_remote_shell::remote_surface_unset_can_maximize,
zcr_remote_shell::remote_surface_set_min_size,
zcr_remote_shell::remote_surface_set_max_size,
zcr_remote_shell::remote_surface_set_snapped_to_left,
zcr_remote_shell::remote_surface_set_snapped_to_right,
zcr_remote_shell::remote_surface_start_resize,
zcr_remote_shell::remote_surface_set_frame,
zcr_remote_shell::remote_surface_set_frame_buttons,
zcr_remote_shell::remote_surface_set_extra_title,
zcr_remote_shell::remote_surface_set_orientation_lock,
zcr_remote_shell::remote_surface_pip,
zcr_remote_shell::remote_surface_set_bounds,
zcr_remote_shell::remote_surface_set_aspect_ratio,
zcr_remote_shell::remote_surface_block_ime,
zcr_remote_shell::remote_surface_unblock_ime,
zcr_remote_shell::remote_surface_set_accessibility_id_DEPRECATED,
zcr_remote_shell::remote_surface_set_pip_original_window,
zcr_remote_shell::remote_surface_unset_pip_original_window,
zcr_remote_shell::remote_surface_set_system_gesture_exclusion,
zcr_remote_shell::remote_surface_set_resize_lock,
zcr_remote_shell::remote_surface_unset_resize_lock,
zcr_remote_shell::remote_surface_set_bounds_in_output,
};
const struct zcr_notification_surface_v1_interface
notification_surface_implementation = {
+[](wl_client* client, wl_resource* resource) {
wl_resource_destroy(resource);
},
zcr_remote_shell::notification_surface_set_app_id,
};
const struct zcr_input_method_surface_v1_interface
input_method_surface_implementation = {
+[](wl_client* client, wl_resource* resource) {
wl_resource_destroy(resource);
},
zcr_remote_shell::input_method_surface_set_bounds,
zcr_remote_shell::input_method_surface_set_bounds_in_output,
};
const struct zcr_toast_surface_v1_interface toast_surface_implementation = {
+[](wl_client* client, wl_resource* resource) {
wl_resource_destroy(resource);
},
zcr_remote_shell::toast_surface_set_position,
zcr_remote_shell::toast_surface_set_size,
zcr_remote_shell::toast_surface_set_bounds_in_output,
};
const struct zcr_remote_output_v1_interface remote_output_implementation = {
+[](wl_client* client, wl_resource* resource) {
wl_resource_destroy(resource);
},
};
const struct WaylandRemoteOutputEventMapping remote_output_event_mapping_v1 = {
zcr_remote_output_v1_send_identification_data,
zcr_remote_output_v1_send_display_id,
zcr_remote_output_v1_send_port,
zcr_remote_output_v1_send_insets,
zcr_remote_output_v1_send_stable_insets,
zcr_remote_output_v1_send_systemui_behavior,
ZCR_REMOTE_OUTPUT_V1_SYSTEMUI_BEHAVIOR_SINCE_VERSION,
ZCR_REMOTE_OUTPUT_V1_STABLE_INSETS_SINCE_VERSION,
};
const WaylandRemoteShellEventMapping wayland_remote_shell_event_mapping_v1 = {
zcr_remote_surface_v1_send_window_geometry_changed,
zcr_remote_surface_v1_send_change_zoom_level,
zcr_remote_surface_v1_send_state_type_changed,
zcr_remote_surface_v1_send_bounds_changed_in_output,
zcr_remote_surface_v1_send_bounds_changed,
zcr_remote_shell_v1_send_activated,
zcr_remote_shell_v1_send_desktop_focus_state_changed,
zcr_remote_shell_v1_send_workspace_info,
zcr_remote_surface_v1_send_drag_finished,
zcr_remote_surface_v1_send_drag_started,
zcr_remote_shell_v1_send_layout_mode,
zcr_remote_shell_v1_send_default_device_scale_factor,
zcr_remote_shell_v1_send_configure,
ZCR_REMOTE_SURFACE_V1_BOUNDS_CHANGED_IN_OUTPUT_SINCE_VERSION,
ZCR_REMOTE_SHELL_V1_DESKTOP_FOCUS_STATE_CHANGED_SINCE_VERSION,
ZCR_REMOTE_SHELL_V1_LAYOUT_MODE_SINCE_VERSION,
ZCR_REMOTE_SHELL_V1_DEFAULT_DEVICE_SCALE_FACTOR_SINCE_VERSION,
ZCR_REMOTE_SURFACE_V1_CHANGE_ZOOM_LEVEL_SINCE_VERSION,
ZCR_REMOTE_SHELL_V1_WORKSPACE_INFO_SINCE_VERSION,
ZCR_REMOTE_SHELL_V1_SET_USE_DEFAULT_DEVICE_SCALE_CANCELLATION_SINCE_VERSION,
/*has_bounds_change_reason_float=*/false,
};
int RemoteSurfaceContainer(uint32_t container) {
switch (container) {
case ZCR_REMOTE_SHELL_V1_CONTAINER_DEFAULT:
return ash::desks_util::GetActiveDeskContainerId();
case ZCR_REMOTE_SHELL_V1_CONTAINER_OVERLAY:
return ash::kShellWindowId_SystemModalContainer;
default:
DLOG(WARNING) << "Unsupported container: " << container;
return ash::desks_util::GetActiveDeskContainerId();
}
}
void HandleRemoteSurfaceCloseCallback(wl_resource* resource) {
zcr_remote_surface_v1_send_close(resource);
wl_client_flush(wl_resource_get_client(resource));
}
void remote_shell_get_remote_surface(wl_client* client,
wl_resource* resource,
uint32_t id,
wl_resource* surface,
uint32_t container) {
WaylandRemoteShell* shell = GetUserDataAs<WaylandRemoteShell>(resource);
std::unique_ptr<ClientControlledShellSurface> shell_surface =
shell->CreateShellSurface(GetUserDataAs<Surface>(surface),
RemoteSurfaceContainer(container));
if (!shell_surface) {
wl_resource_post_error(resource, ZCR_REMOTE_SHELL_V1_ERROR_ROLE,
"surface has already been assigned a role");
return;
}
wl_resource* remote_surface_resource =
wl_resource_create(client, &zcr_remote_surface_v1_interface,
wl_resource_get_version(resource), id);
if (wl_resource_get_version(remote_surface_resource) < 18)
shell_surface->set_server_reparent_window(true);
shell_surface->SetSecurityDelegate(GetSecurityDelegate(client));
shell_surface->set_delegate(
shell->CreateShellSurfaceDelegate(remote_surface_resource));
shell_surface->set_close_callback(
base::BindRepeating(&HandleRemoteSurfaceCloseCallback,
base::Unretained(remote_surface_resource)));
shell_surface->set_surface_destroyed_callback(base::BindOnce(
&wl_resource_destroy, base::Unretained(remote_surface_resource)));
DCHECK(wl_resource_get_version(remote_surface_resource) >= 10);
SetImplementation(remote_surface_resource, &remote_surface_implementation,
std::move(shell_surface));
}
void remote_shell_get_notification_surface(wl_client* client,
wl_resource* resource,
uint32_t id,
wl_resource* surface,
const char* notification_key) {
if (GetUserDataAs<Surface>(surface)->HasSurfaceDelegate()) {
wl_resource_post_error(resource, ZCR_REMOTE_SHELL_V1_ERROR_ROLE,
"surface has already been assigned a role");
return;
}
std::unique_ptr<NotificationSurface> notification_surface =
GetUserDataAs<WaylandRemoteShell>(resource)->CreateNotificationSurface(
GetUserDataAs<Surface>(surface), std::string(notification_key));
if (!notification_surface) {
wl_resource_post_error(resource,
ZCR_REMOTE_SHELL_V1_ERROR_INVALID_NOTIFICATION_KEY,
"invalid notification key");
return;
}
wl_resource* notification_surface_resource =
wl_resource_create(client, &zcr_notification_surface_v1_interface,
wl_resource_get_version(resource), id);
SetImplementation(notification_surface_resource,
¬ification_surface_implementation,
std::move(notification_surface));
}
void remote_shell_get_input_method_surface(wl_client* client,
wl_resource* resource,
uint32_t id,
wl_resource* surface) {
if (GetUserDataAs<Surface>(surface)->HasSurfaceDelegate()) {
wl_resource_post_error(resource, ZCR_REMOTE_SHELL_V1_ERROR_ROLE,
"surface has already been assigned a role");
return;
}
std::unique_ptr<ClientControlledShellSurface> input_method_surface =
GetUserDataAs<WaylandRemoteShell>(resource)->CreateInputMethodSurface(
GetUserDataAs<Surface>(surface));
if (!input_method_surface) {
wl_resource_post_error(resource, ZCR_REMOTE_SHELL_V1_ERROR_ROLE,
"Cannot create an IME surface");
return;
}
wl_resource* input_method_surface_resource =
wl_resource_create(client, &zcr_input_method_surface_v1_interface,
wl_resource_get_version(resource), id);
SetImplementation(input_method_surface_resource,
&input_method_surface_implementation,
std::move(input_method_surface));
}
void remote_shell_get_toast_surface(wl_client* client,
wl_resource* resource,
uint32_t id,
wl_resource* surface) {
if (GetUserDataAs<Surface>(surface)->HasSurfaceDelegate()) {
wl_resource_post_error(resource, ZCR_REMOTE_SHELL_V1_ERROR_ROLE,
"surface has already been assigned a role");
return;
}
std::unique_ptr<ClientControlledShellSurface> toast_surface =
GetUserDataAs<WaylandRemoteShell>(resource)->CreateToastSurface(
GetUserDataAs<Surface>(surface));
if (!toast_surface) {
wl_resource_post_error(resource, ZCR_REMOTE_SHELL_V1_ERROR_ROLE,
"Cannot create an toast surface");
return;
}
wl_resource* toast_surface_resource =
wl_resource_create(client, &zcr_toast_surface_v1_interface,
wl_resource_get_version(resource), id);
SetImplementation(toast_surface_resource, &toast_surface_implementation,
std::move(toast_surface));
}
void remote_shell_get_remote_output(wl_client* client,
wl_resource* resource,
uint32_t id,
wl_resource* output_resource) {
WaylandDisplayHandler* display_handler =
GetUserDataAs<WaylandDisplayHandler>(output_resource);
wl_resource* remote_output_resource =
wl_resource_create(client, &zcr_remote_output_v1_interface,
wl_resource_get_version(resource), id);
auto remote_output = std::make_unique<WaylandRemoteOutput>(
remote_output_resource, remote_output_event_mapping_v1, display_handler);
SetImplementation(remote_output_resource, &remote_output_implementation,
std::move(remote_output));
}
const struct zcr_remote_shell_v1_interface remote_shell_implementation = {
+[](wl_client* client, wl_resource* resource) {
// Nothing to do here.
},
remote_shell_get_remote_surface,
remote_shell_get_notification_surface,
remote_shell_get_input_method_surface,
remote_shell_get_toast_surface,
remote_shell_get_remote_output,
zcr_remote_shell::remote_shell_set_use_default_scale_cancellation};
} // namespace
WaylandRemoteShellData::WaylandRemoteShellData(
Display* display,
OutputResourceProvider output_provider)
: display(display), output_provider(output_provider) {}
WaylandRemoteShellData::~WaylandRemoteShellData() {}
void bind_remote_shell(wl_client* client,
void* data,
uint32_t version,
uint32_t id) {
wl_resource* resource = wl_resource_create(
client, &zcr_remote_shell_v1_interface,
std::min<uint32_t>(version, zcr_remote_shell_v1_interface.version), id);
auto* remote_shell_data = static_cast<WaylandRemoteShellData*>(data);
SetImplementation(
resource, &remote_shell_implementation,
std::make_unique<WaylandRemoteShell>(
remote_shell_data->display, resource,
base::BindRepeating(remote_shell_data->output_provider, client),
wayland_remote_shell_event_mapping_v1,
/*use_default_scale_cancellation_default=*/true));
}
gfx::Insets GetWorkAreaInsetsInPixel(const display::Display& display,
float device_scale_factor,
const gfx::Size& size_in_pixel,
const gfx::Rect& work_area_in_dp) {
gfx::Rect local_work_area_in_dp = work_area_in_dp;
local_work_area_in_dp.Offset(-display.bounds().x(), -display.bounds().y());
gfx::Rect work_area_in_pixel =
zcr_remote_shell::ScaleBoundsToPixelSnappedToParent(
size_in_pixel, display.bounds().size(), device_scale_factor,
local_work_area_in_dp);
gfx::Insets insets_in_pixel =
gfx::Rect(size_in_pixel).InsetsFrom(work_area_in_pixel);
// TODO(oshima): I think this is more conservative than necessary. The correct
// way is to use enclosed rect when converting the work area from dp to
// client pixel, but that led to weird buffer size in overlay detection.
// (crbug.com/920650). Investigate if we can fix it and use enclosed rect.
return gfx::Insets::TLBR(
base::ClampRound(
base::ClampCeil(insets_in_pixel.top() / device_scale_factor) *
device_scale_factor),
base::ClampRound(
base::ClampCeil(insets_in_pixel.left() / device_scale_factor) *
device_scale_factor),
base::ClampRound(
base::ClampCeil(insets_in_pixel.bottom() / device_scale_factor) *
device_scale_factor),
base::ClampRound(
base::ClampCeil(insets_in_pixel.right() / device_scale_factor) *
device_scale_factor));
}
gfx::Rect GetStableWorkArea(const display::Display& display) {
auto* root = ash::Shell::GetRootWindowForDisplayId(display.id());
return ash::WorkAreaInsets::ForWindow(root)->ComputeStableWorkArea();
}
} // namespace exo::wayland
|