2 * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "wayland_ivi_wm.hpp"
29 : d(std::unique_ptr<struct wl_display, void (*)(struct wl_display *)>(
30 wl_display_connect(nullptr), &wl_display_disconnect)),
33 bool display::ok() const { return d && wl_display_get_error(d.get()) == 0; }
35 void display::roundtrip() { wl_display_roundtrip(this->d.get()); }
37 int display::dispatch() { return wl_display_dispatch(this->d.get()); }
39 int display::dispatch_pending() { return wl_display_dispatch_pending(this->d.get()); }
41 int display::read_events()
44 while (wl_display_prepare_read(this->d.get()) == -1)
46 if (wl_display_dispatch_pending(this->d.get()) == -1)
52 if (wl_display_flush(this->d.get()) == -1)
57 if (wl_display_read_events(this->d.get()) == -1)
59 wl_display_cancel_read(this->d.get());
65 void display::flush() { wl_display_flush(this->d.get()); }
67 int display::get_fd() const { return wl_display_get_fd(this->d.get()); }
69 int display::get_error() { return wl_display_get_error(this->d.get()); }
76 void registry_global_created(void *data, struct wl_registry * /*r*/, uint32_t name,
77 char const *iface, uint32_t v)
79 static_cast<struct registry *>(data)->global_created(name, iface, v);
82 void registry_global_removed(void *data, struct wl_registry * /*r*/,
85 static_cast<struct registry *>(data)->global_removed(name);
88 constexpr struct wl_registry_listener registry_listener = {
89 registry_global_created, registry_global_removed};
92 registry::registry(struct wl_display *d)
93 : wayland_proxy(d == nullptr ? nullptr : wl_display_get_registry(d))
95 if (this->proxy != nullptr)
97 wl_registry_add_listener(this->proxy.get(), ®istry_listener, this);
101 void registry::add_global_handler(char const *iface, binder bind)
103 this->bindings[iface] = std::move(bind);
106 void registry::global_created(uint32_t name, char const *iface, uint32_t v)
108 auto b = this->bindings.find(iface);
109 if (b != this->bindings.end())
111 b->second(this->proxy.get(), name, v);
113 HMI_DEBUG("wl::registry @ %p global n %u i %s v %u", this->proxy.get(), name,
117 void registry::global_removed(uint32_t /*name*/) {}
124 void output_geometry(void *data, struct wl_output * /*wl_output*/, int32_t x,
125 int32_t y, int32_t physical_width, int32_t physical_height,
126 int32_t subpixel, const char *make, const char *model,
129 static_cast<struct output *>(data)->geometry(
130 x, y, physical_width, physical_height, subpixel, make, model, transform);
133 void output_mode(void *data, struct wl_output * /*wl_output*/, uint32_t flags,
134 int32_t width, int32_t height, int32_t refresh)
136 static_cast<struct output *>(data)->mode(flags, width, height, refresh);
139 void output_done(void *data, struct wl_output * /*wl_output*/)
141 static_cast<struct output *>(data)->done();
144 void output_scale(void *data, struct wl_output * /*wl_output*/,
147 static_cast<struct output *>(data)->scale(factor);
150 constexpr struct wl_output_listener output_listener = {
151 output_geometry, output_mode, output_done, output_scale};
154 output::output(struct wl_registry *r, uint32_t name, uint32_t v)
155 : wayland_proxy(wl_registry_bind(r, name, &wl_output_interface, v))
157 wl_output_add_listener(this->proxy.get(), &output_listener, this);
160 void output::geometry(int32_t x, int32_t y, int32_t pw, int32_t ph,
161 int32_t subpel, char const *make, char const *model,
165 "wl::output %s @ %p x %i y %i w %i h %i spel %x make %s model %s tx %i",
166 __func__, this->proxy.get(), x, y, pw, ph, subpel, make, model, tx);
167 this->physical_width = pw;
168 this->physical_height = ph;
169 this->transform = tx;
172 void output::mode(uint32_t flags, int32_t w, int32_t h, int32_t r)
174 HMI_DEBUG("wl::output %s @ %p f %x w %i h %i r %i", __func__,
175 this->proxy.get(), flags, w, h, r);
176 if ((flags & WL_OUTPUT_MODE_CURRENT) != 0u)
186 HMI_DEBUG("wl::output %s @ %p done", __func__, this->proxy.get());
188 if (this->transform == WL_OUTPUT_TRANSFORM_90 ||
189 this->transform == WL_OUTPUT_TRANSFORM_270 ||
190 this->transform == WL_OUTPUT_TRANSFORM_FLIPPED_90 ||
191 this->transform == WL_OUTPUT_TRANSFORM_FLIPPED_270)
193 std::swap(this->width, this->height);
194 std::swap(this->physical_width, this->physical_height);
198 void output::scale(int32_t factor)
200 HMI_DEBUG("wl::output %s @ %p f %i", __func__, this->proxy.get(), factor);
205 * namespace compositor
213 void surface_visibility_changed(
214 void *data, struct ivi_wm * /*ivi_wm*/,
215 uint32_t surface_id, int32_t visibility)
217 auto c = static_cast<struct controller *>(data);
218 c->surface_visibility_changed(surface_id, visibility);
221 void surface_opacity_changed(void *data, struct ivi_wm * /*ivi_wm*/,
222 uint32_t surface_id, wl_fixed_t opacity)
224 auto c = static_cast<struct controller *>(data);
225 c->surface_opacity_changed(surface_id, float(wl_fixed_to_double(opacity)));
228 void surface_source_rectangle_changed(
229 void *data, struct ivi_wm * /*ivi_wm*/, uint32_t surface_id,
230 int32_t x, int32_t y, int32_t width, int32_t height)
232 auto c = static_cast<struct controller *>(data);
233 c->surface_source_rectangle_changed(surface_id, x, y, width, height);
236 void surface_destination_rectangle_changed(
237 void *data, struct ivi_wm * /*ivi_wm*/, uint32_t surface_id,
238 int32_t x, int32_t y, int32_t width, int32_t height)
240 auto c = static_cast<struct controller *>(data);
241 c->surface_destination_rectangle_changed(surface_id, x, y, width, height);
244 void surface_created(void *data, struct ivi_wm * /*ivi_wm*/,
247 static_cast<struct controller *>(data)->surface_created(id_surface);
250 void surface_destroyed(
251 void *data, struct ivi_wm * /*ivi_wm*/, uint32_t surface_id)
253 auto c = static_cast<struct controller *>(data);
254 c->surface_destroyed(surface_id);
257 void surface_error_detected(void *data, struct ivi_wm * /*ivi_wm*/, uint32_t object_id,
258 uint32_t error_code, const char *error_text)
260 static_cast<struct controller *>(data)->surface_error_detected(
261 object_id, error_code, error_text);
264 void surface_size_changed(
265 void *data, struct ivi_wm * /*ivi_wm*/, uint32_t surface_id,
266 int32_t width, int32_t height)
268 auto c = static_cast<struct controller *>(data);
269 c->surface_size_changed(surface_id, width, height);
272 void surface_stats_received(void *data, struct ivi_wm * /*ivi_wm*/,
273 uint32_t surface_id, uint32_t frame_count, uint32_t pid)
275 auto c = static_cast<struct controller *>(data);
276 c->surface_stats_received(surface_id, frame_count, pid);
279 void surface_added_to_layer(void *data, struct ivi_wm * /*ivi_wm*/,
280 uint32_t layer_id, uint32_t surface_id)
282 auto c = static_cast<struct controller *>(data);
283 c->surface_added_to_layer(layer_id, surface_id);
286 void layer_visibility_changed(void *data, struct ivi_wm * /*ivi_wm*/,
287 uint32_t layer_id, int32_t visibility)
289 auto c = static_cast<struct controller *>(data);
290 c->layer_visibility_changed(layer_id, visibility);
293 void layer_opacity_changed(void *data, struct ivi_wm * /*ivi_wm*/,
294 uint32_t layer_id, wl_fixed_t opacity)
296 auto c = static_cast<struct controller *>(data);
297 c->layer_opacity_changed(layer_id, float(wl_fixed_to_double(opacity)));
300 void layer_source_rectangle_changed(
301 void *data, struct ivi_wm * /*ivi_wm*/, uint32_t layer_id,
302 int32_t x, int32_t y, int32_t width, int32_t height)
304 auto c = static_cast<struct controller *>(data);
305 c->layer_source_rectangle_changed(layer_id, x, y, width, height);
308 void layer_destination_rectangle_changed(
309 void *data, struct ivi_wm * /*ivi_wm*/, uint32_t layer_id,
310 int32_t x, int32_t y, int32_t width, int32_t height)
312 auto c = static_cast<struct controller *>(data);
313 c->layer_destination_rectangle_changed(layer_id, x, y, width, height);
316 void layer_created(void *data, struct ivi_wm * /*ivi_wm*/,
319 static_cast<struct controller *>(data)->layer_created(id_layer);
322 void layer_destroyed(void *data, struct ivi_wm * /*ivi_wm*/, uint32_t layer_id)
324 auto c = static_cast<struct controller *>(data);
325 c->layer_destroyed(layer_id);
328 void layer_error_detected(void *data, struct ivi_wm * /*ivi_wm*/, uint32_t object_id,
329 uint32_t error_code, const char *error_text)
331 static_cast<struct controller *>(data)->layer_error_detected(
332 object_id, error_code, error_text);
335 constexpr struct ivi_wm_listener listener = {
336 surface_visibility_changed,
337 layer_visibility_changed,
338 surface_opacity_changed,
339 layer_opacity_changed,
340 surface_source_rectangle_changed,
341 layer_source_rectangle_changed,
342 surface_destination_rectangle_changed,
343 layer_destination_rectangle_changed,
348 surface_error_detected,
349 layer_error_detected,
350 surface_size_changed,
351 surface_stats_received,
352 surface_added_to_layer,
355 void screen_created(void *data, struct ivi_wm_screen *ivi_wm_screen, uint32_t id)
357 static_cast<struct screen *>(data)->screen_created((struct screen *)data, id);
360 void layer_added(void *data,
361 struct ivi_wm_screen *ivi_wm_screen,
364 HMI_DEBUG("added layer_id:%d", layer_id);
367 void connector_name(void *data,
368 struct ivi_wm_screen *ivi_wm_screen,
369 const char *process_name)
371 HMI_DEBUG("process_name:%s", process_name);
374 void screen_error(void *data,
375 struct ivi_wm_screen *ivi_wm_screen,
379 HMI_DEBUG("screen error:%d message:%s", error, message);
382 constexpr struct ivi_wm_screen_listener screen_listener = {
393 surface::surface(uint32_t i, struct controller *c)
394 : controller_child(c, i)
396 this->parent->add_proxy_to_sid_mapping(this->parent->proxy.get(), i);
399 void surface::set_visibility(uint32_t visibility)
401 HMI_DEBUG("compositor::surface id:%d v:%d", this->id, visibility);
402 ivi_wm_set_surface_visibility(this->parent->proxy.get(), this->id, visibility);
405 void surface::set_source_rectangle(int32_t x, int32_t y,
406 int32_t width, int32_t height)
408 ivi_wm_set_surface_source_rectangle(this->parent->proxy.get(), this->id,
409 x, y, width, height);
412 void surface::set_destination_rectangle(int32_t x, int32_t y,
413 int32_t width, int32_t height)
415 ivi_wm_set_surface_destination_rectangle(this->parent->proxy.get(), this->id,
416 x, y, width, height);
422 layer::layer(uint32_t i, struct controller *c) : layer(i, 0, 0, c) {}
424 layer::layer(uint32_t i, int32_t w, int32_t h, struct controller *c)
425 : controller_child(c, i)
427 this->parent->add_proxy_to_lid_mapping(this->parent->proxy.get(), i);
428 ivi_wm_create_layout_layer(c->proxy.get(), i, w, h);
431 void layer::set_visibility(uint32_t visibility)
433 ivi_wm_set_layer_visibility(this->parent->proxy.get(), this->id, visibility);
436 void layer::set_destination_rectangle(int32_t x, int32_t y,
437 int32_t width, int32_t height)
439 ivi_wm_set_layer_destination_rectangle(this->parent->proxy.get(), this->id,
440 x, y, width, height);
443 void layer::add_surface(uint32_t surface_id)
445 ivi_wm_layer_add_surface(this->parent->proxy.get(), this->id, surface_id);
448 void layer::remove_surface(uint32_t surface_id)
450 ivi_wm_layer_remove_surface(this->parent->proxy.get(), this->id, surface_id);
456 screen::screen(uint32_t i, struct controller *c, struct wl_output *o)
457 : wayland_proxy(ivi_wm_create_screen(c->proxy.get(), o)),
458 controller_child(c, i)
460 HMI_DEBUG("compositor::screen @ %p id %u o %p", this->proxy.get(), i, o);
462 // Add listener for screen
463 ivi_wm_screen_add_listener(this->proxy.get(), &screen_listener, this);
466 void screen::clear() { ivi_wm_screen_clear(this->proxy.get()); }
468 void screen::screen_created(struct screen *screen, uint32_t id)
470 HMI_DEBUG("compositor::screen @ %p screen %u (%x) @ %p", this->proxy.get(),
473 this->parent->screens[id] = screen;
476 void screen::set_render_order(std::vector<uint32_t> const &ro)
480 // Remove all layers from the screen render order
481 ivi_wm_screen_clear(this->proxy.get());
483 for (i = 0; i < ro.size(); i++)
485 HMI_DEBUG("compositor::screen @ %p add layer %u", this->proxy.get(), ro[i]);
486 // Add the layer to screen render order at nearest z-position
487 ivi_wm_screen_add_layer(this->proxy.get(), ro[i]);
494 controller::controller(struct wl_registry *r, uint32_t name, uint32_t version)
496 wl_registry_bind(r, name, &ivi_wm_interface, version)),
499 ivi_wm_add_listener(this->proxy.get(), &listener, this);
502 void controller::layer_create(uint32_t id, int32_t w, int32_t h)
504 this->layers[id] = std::make_unique<struct layer>(id, w, h, this);
507 void controller::surface_create(uint32_t id)
509 this->surfaces[id] = std::make_unique<struct surface>(id, this);
511 // TODO: If Clipping is necessary, this process should be modified.
513 // Set surface type:IVI_WM_SURFACE_TYPE_DESKTOP)
514 // for resizing wayland surface when switching from split to full surface.
515 ivi_wm_set_surface_type(this->proxy.get(), id, IVI_WM_SURFACE_TYPE_DESKTOP);
517 // Set source reactangle even if we should not need to set it
518 // for enable setting for destination region.
519 this->surfaces[id]->set_source_rectangle(0, 0, this->output_size.w, this->output_size.h);
522 this->display->flush();
526 void controller::create_screen(struct wl_output *output)
528 // TODO: screen id is 0 (WM manages one screen for now)
529 this->screen = std::make_unique<struct screen>(0, this, output);
532 void controller::get_surface_properties(uint32_t surface_id, int param)
534 ivi_wm_surface_get(this->proxy.get(), surface_id, param);
537 void controller::layer_created(uint32_t id)
539 HMI_DEBUG("compositor::controller @ %p layer %u (%x)", this->proxy.get(), id, id);
540 if (this->layers.find(id) != this->layers.end())
542 HMI_DEBUG("WindowManager has created layer %u (%x) already", id, id);
546 this->layers[id] = std::make_unique<struct layer>(id, this);
550 void controller::layer_error_detected(uint32_t object_id,
551 uint32_t error_code, const char *error_text)
553 HMI_DEBUG("compositor::controller @ %p error o %d c %d text %s",
554 this->proxy.get(), object_id, error_code, error_text);
557 void controller::surface_visibility_changed(uint32_t id, int32_t visibility)
559 HMI_DEBUG("compositor::surface %s @ %d v %i", __func__, id,
561 this->sprops[id].visibility = visibility;
562 this->chooks->surface_visibility(id, visibility);
565 void controller::surface_opacity_changed(uint32_t id, float opacity)
567 HMI_DEBUG("compositor::surface %s @ %d o %f",
568 __func__, id, opacity);
569 this->sprops[id].opacity = opacity;
572 void controller::surface_source_rectangle_changed(uint32_t id, int32_t x,
573 int32_t y, int32_t width,
576 HMI_DEBUG("compositor::surface %s @ %d x %i y %i w %i h %i", __func__,
577 id, x, y, width, height);
578 this->sprops[id].src_rect = rect{width, height, x, y};
581 void controller::surface_destination_rectangle_changed(uint32_t id, int32_t x,
582 int32_t y, int32_t width,
585 HMI_DEBUG("compositor::surface %s @ %d x %i y %i w %i h %i", __func__,
586 id, x, y, width, height);
587 this->sprops[id].dst_rect = rect{width, height, x, y};
588 this->chooks->surface_destination_rectangle(id, x, y, width, height);
591 void controller::surface_size_changed(uint32_t id, int32_t width,
594 HMI_DEBUG("compositor::surface %s @ %d w %i h %i", __func__, id,
596 this->sprops[id].size = size{uint32_t(width), uint32_t(height)};
597 this->surfaces[id]->set_source_rectangle(0, 0, width, height);
600 void controller::surface_added_to_layer(uint32_t layer_id, uint32_t surface_id)
602 HMI_DEBUG("compositor::surface %s @ %d l %u",
603 __func__, layer_id, surface_id);
606 void controller::surface_stats_received(uint32_t surface_id,
607 uint32_t frame_count, uint32_t pid)
609 HMI_DEBUG("compositor::surface %s @ %d f %u pid %u",
610 __func__, surface_id, frame_count, pid);
611 this->sprops[surface_id].pid = pid;
614 void controller::surface_created(uint32_t id)
616 HMI_DEBUG("compositor::controller @ %p surface %u (%x)", this->proxy.get(), id,
618 if (this->surfaces.find(id) == this->surfaces.end())
620 this->surfaces[id] = std::make_unique<struct surface>(id, this);
621 this->chooks->surface_created(id);
623 // Set surface type:IVI_WM_SURFACE_TYPE_DESKTOP)
624 // for resizing wayland surface when switching from split to full surface.
625 ivi_wm_set_surface_type(this->proxy.get(), id, IVI_WM_SURFACE_TYPE_DESKTOP);
628 this->display->flush();
632 void controller::surface_destroyed(uint32_t surface_id)
634 HMI_DEBUG("compositor::surface %s @ %d", __func__, surface_id);
635 this->chooks->surface_removed(surface_id);
636 this->sprops.erase(surface_id);
637 this->surfaces.erase(surface_id);
640 void controller::surface_error_detected(uint32_t object_id,
641 uint32_t error_code, const char *error_text)
643 HMI_DEBUG("compositor::controller @ %p error o %d c %d text %s",
644 this->proxy.get(), object_id, error_code, error_text);
647 void controller::layer_visibility_changed(uint32_t layer_id, int32_t visibility)
649 HMI_DEBUG("compositor::layer %s @ %d v %i", __func__, layer_id, visibility);
650 this->lprops[layer_id].visibility = visibility;
653 void controller::layer_opacity_changed(uint32_t layer_id, float opacity)
655 HMI_DEBUG("compositor::layer %s @ %d o %f", __func__, layer_id, opacity);
656 this->lprops[layer_id].opacity = opacity;
659 void controller::layer_source_rectangle_changed(uint32_t layer_id,
660 int32_t x, int32_t y,
661 int32_t width, int32_t height)
663 HMI_DEBUG("compositor::layer %s @ %d x %i y %i w %i h %i",
664 __func__, layer_id, x, y, width, height);
665 this->lprops[layer_id].src_rect = rect{width, height, x, y};
668 void controller::layer_destination_rectangle_changed(uint32_t layer_id,
669 int32_t x, int32_t y,
670 int32_t width, int32_t height)
672 HMI_DEBUG("compositor::layer %s @ %d x %i y %i w %i h %i",
673 __func__, layer_id, x, y, width, height);
674 this->lprops[layer_id].dst_rect = rect{width, height, x, y};
677 void controller::layer_destroyed(uint32_t layer_id)
679 HMI_DEBUG("compositor::layer %s @ %d", __func__, layer_id);
680 this->lprops.erase(layer_id);
681 this->layers.erase(layer_id);
684 void controller::add_proxy_to_sid_mapping(struct ivi_wm *p,
687 HMI_DEBUG("Add surface proxy mapping for %p (%u)", p, id);
688 this->surface_proxy_to_id[uintptr_t(p)] = id;
689 this->sprops[id].id = id;
692 void controller::remove_proxy_to_sid_mapping(struct ivi_wm *p)
694 HMI_DEBUG("Remove surface proxy mapping for %p", p);
695 this->surface_proxy_to_id.erase(uintptr_t(p));
698 void controller::add_proxy_to_lid_mapping(struct ivi_wm *p,
701 HMI_DEBUG("Add layer proxy mapping for %p (%u)", p, id);
702 this->layer_proxy_to_id[uintptr_t(p)] = id;
703 this->lprops[id].id = id;
706 void controller::remove_proxy_to_lid_mapping(struct ivi_wm *p)
708 HMI_DEBUG("Remove layer proxy mapping for %p", p);
709 this->layer_proxy_to_id.erase(uintptr_t(p));
712 void controller::add_proxy_to_id_mapping(struct wl_output *p, uint32_t id)
714 HMI_DEBUG("Add screen proxy mapping for %p (%u)", p, id);
715 this->screen_proxy_to_id[uintptr_t(p)] = id;
718 void controller::remove_proxy_to_id_mapping(struct wl_output *p)
720 HMI_DEBUG("Remove screen proxy mapping for %p", p);
721 this->screen_proxy_to_id.erase(uintptr_t(p));
724 } // namespace compositor