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()
43 while (wl_display_prepare_read(this->d.get()) == -1)
45 if (wl_display_dispatch_pending(this->d.get()) == -1)
51 if (wl_display_flush(this->d.get()) == -1)
56 if (wl_display_read_events(this->d.get()) == -1)
58 wl_display_cancel_read(this->d.get());
64 void display::flush() { wl_display_flush(this->d.get()); }
66 int display::get_fd() const { return wl_display_get_fd(this->d.get()); }
68 int display::get_error() { return wl_display_get_error(this->d.get()); }
75 void registry_global_created(void *data, struct wl_registry * /*r*/, uint32_t name,
76 char const *iface, uint32_t v)
78 static_cast<struct registry *>(data)->global_created(name, iface, v);
81 void registry_global_removed(void *data, struct wl_registry * /*r*/,
84 static_cast<struct registry *>(data)->global_removed(name);
87 constexpr struct wl_registry_listener registry_listener = {
88 registry_global_created, registry_global_removed};
91 registry::registry(struct wl_display *d)
92 : wayland_proxy(d == nullptr ? nullptr : wl_display_get_registry(d))
94 if (this->proxy != nullptr)
96 wl_registry_add_listener(this->proxy.get(), ®istry_listener, this);
100 void registry::add_global_handler(char const *iface, binder bind)
102 this->bindings[iface] = std::move(bind);
105 void registry::global_created(uint32_t name, char const *iface, uint32_t v)
107 auto b = this->bindings.find(iface);
108 if (b != this->bindings.end())
110 b->second(this->proxy.get(), name, v);
112 HMI_DEBUG("wl::registry @ %p global n %u i %s v %u", this->proxy.get(), name,
116 void registry::global_removed(uint32_t /*name*/) {}
123 void output_geometry(void *data, struct wl_output * /*wl_output*/, int32_t x,
124 int32_t y, int32_t physical_width, int32_t physical_height,
125 int32_t subpixel, const char *make, const char *model,
128 static_cast<struct output *>(data)->geometry(
129 x, y, physical_width, physical_height, subpixel, make, model, transform);
132 void output_mode(void *data, struct wl_output * /*wl_output*/, uint32_t flags,
133 int32_t width, int32_t height, int32_t refresh)
135 static_cast<struct output *>(data)->mode(flags, width, height, refresh);
138 void output_done(void *data, struct wl_output * /*wl_output*/)
140 static_cast<struct output *>(data)->done();
143 void output_scale(void *data, struct wl_output * /*wl_output*/,
146 static_cast<struct output *>(data)->scale(factor);
149 constexpr struct wl_output_listener output_listener = {
150 output_geometry, output_mode, output_done, output_scale};
153 output::output(struct wl_registry *r, uint32_t name, uint32_t v)
154 : wayland_proxy(wl_registry_bind(r, name, &wl_output_interface, v))
156 wl_output_add_listener(this->proxy.get(), &output_listener, this);
159 void output::geometry(int32_t x, int32_t y, int32_t pw, int32_t ph,
160 int32_t subpel, char const *make, char const *model,
163 HMI_DEBUG("wl::output %s @ %p x %i y %i w %i h %i spel %x make %s model %s tx %i",
164 __func__, this->proxy.get(), x, y, pw, ph, subpel, make, model, tx);
165 this->physical_width = pw;
166 this->physical_height = ph;
167 this->transform = tx;
170 void output::mode(uint32_t flags, int32_t w, int32_t h, int32_t r)
172 HMI_DEBUG("wl::output %s @ %p f %x w %i h %i r %i", __func__,
173 this->proxy.get(), flags, w, h, r);
174 if ((flags & WL_OUTPUT_MODE_CURRENT) != 0u)
184 HMI_DEBUG("wl::output %s @ %p done", __func__, this->proxy.get());
186 if (this->transform == WL_OUTPUT_TRANSFORM_90 ||
187 this->transform == WL_OUTPUT_TRANSFORM_270 ||
188 this->transform == WL_OUTPUT_TRANSFORM_FLIPPED_90 ||
189 this->transform == WL_OUTPUT_TRANSFORM_FLIPPED_270)
191 std::swap(this->width, this->height);
192 std::swap(this->physical_width, this->physical_height);
196 void output::scale(int32_t factor)
198 HMI_DEBUG("wl::output %s @ %p f %i", __func__, this->proxy.get(), factor);
203 * namespace compositor
211 void surface_visibility_changed(
212 void *data, struct ivi_wm * /*ivi_wm*/,
213 uint32_t surface_id, int32_t visibility)
215 auto c = static_cast<struct controller *>(data);
216 c->surface_visibility_changed(surface_id, visibility);
219 void surface_opacity_changed(void *data, struct ivi_wm * /*ivi_wm*/,
220 uint32_t surface_id, wl_fixed_t opacity)
222 auto c = static_cast<struct controller *>(data);
223 c->surface_opacity_changed(surface_id, float(wl_fixed_to_double(opacity)));
226 void surface_source_rectangle_changed(
227 void *data, struct ivi_wm * /*ivi_wm*/, uint32_t surface_id,
228 int32_t x, int32_t y, int32_t width, int32_t height)
230 auto c = static_cast<struct controller *>(data);
231 c->surface_source_rectangle_changed(surface_id, x, y, width, height);
234 void surface_destination_rectangle_changed(
235 void *data, struct ivi_wm * /*ivi_wm*/, uint32_t surface_id,
236 int32_t x, int32_t y, int32_t width, int32_t height)
238 auto c = static_cast<struct controller *>(data);
239 c->surface_destination_rectangle_changed(surface_id, x, y, width, height);
242 void surface_created(void *data, struct ivi_wm * /*ivi_wm*/,
245 static_cast<struct controller *>(data)->surface_created(id_surface);
248 void surface_destroyed(
249 void *data, struct ivi_wm * /*ivi_wm*/, uint32_t surface_id)
251 auto c = static_cast<struct controller *>(data);
252 c->surface_destroyed(surface_id);
255 void surface_error_detected(void *data, struct ivi_wm * /*ivi_wm*/, uint32_t object_id,
256 uint32_t error_code, const char *error_text)
258 static_cast<struct controller *>(data)->surface_error_detected(
259 object_id, error_code, error_text);
262 void surface_size_changed(
263 void *data, struct ivi_wm * /*ivi_wm*/, uint32_t surface_id,
264 int32_t width, int32_t height)
266 auto c = static_cast<struct controller *>(data);
267 c->surface_size_changed(surface_id, width, height);
270 void surface_stats_received(void *data, struct ivi_wm * /*ivi_wm*/,
271 uint32_t surface_id, uint32_t frame_count, uint32_t pid)
273 auto c = static_cast<struct controller *>(data);
274 c->surface_stats_received(surface_id, frame_count, pid);
277 void surface_added_to_layer(void *data, struct ivi_wm * /*ivi_wm*/,
278 uint32_t layer_id, uint32_t surface_id)
280 auto c = static_cast<struct controller *>(data);
281 c->surface_added_to_layer(layer_id, surface_id);
284 void layer_visibility_changed(void *data, struct ivi_wm * /*ivi_wm*/,
285 uint32_t layer_id, int32_t visibility)
287 auto c = static_cast<struct controller *>(data);
288 c->layer_visibility_changed(layer_id, visibility);
291 void layer_opacity_changed(void *data, struct ivi_wm * /*ivi_wm*/,
292 uint32_t layer_id, wl_fixed_t opacity)
294 auto c = static_cast<struct controller *>(data);
295 c->layer_opacity_changed(layer_id, float(wl_fixed_to_double(opacity)));
298 void layer_source_rectangle_changed(
299 void *data, struct ivi_wm * /*ivi_wm*/, uint32_t layer_id,
300 int32_t x, int32_t y, int32_t width, int32_t height)
302 auto c = static_cast<struct controller *>(data);
303 c->layer_source_rectangle_changed(layer_id, x, y, width, height);
306 void layer_destination_rectangle_changed(
307 void *data, struct ivi_wm * /*ivi_wm*/, uint32_t layer_id,
308 int32_t x, int32_t y, int32_t width, int32_t height)
310 auto c = static_cast<struct controller *>(data);
311 c->layer_destination_rectangle_changed(layer_id, x, y, width, height);
314 void layer_created(void *data, struct ivi_wm * /*ivi_wm*/,
317 static_cast<struct controller *>(data)->layer_created(id_layer);
320 void layer_destroyed(void *data, struct ivi_wm * /*ivi_wm*/, uint32_t layer_id)
322 auto c = static_cast<struct controller *>(data);
323 c->layer_destroyed(layer_id);
326 void layer_error_detected(void *data, struct ivi_wm * /*ivi_wm*/, uint32_t object_id,
327 uint32_t error_code, const char *error_text)
329 static_cast<struct controller *>(data)->layer_error_detected(
330 object_id, error_code, error_text);
333 constexpr struct ivi_wm_listener listener = {
334 surface_visibility_changed,
335 layer_visibility_changed,
336 surface_opacity_changed,
337 layer_opacity_changed,
338 surface_source_rectangle_changed,
339 layer_source_rectangle_changed,
340 surface_destination_rectangle_changed,
341 layer_destination_rectangle_changed,
346 surface_error_detected,
347 layer_error_detected,
348 surface_size_changed,
349 surface_stats_received,
350 surface_added_to_layer,
353 void screen_created(void *data, struct ivi_wm_screen *ivi_wm_screen, uint32_t id)
355 static_cast<struct screen *>(data)->screen_created((struct screen *)data, id);
358 void layer_added(void *data,
359 struct ivi_wm_screen *ivi_wm_screen,
362 HMI_DEBUG("added layer_id:%d", layer_id);
365 void connector_name(void *data,
366 struct ivi_wm_screen *ivi_wm_screen,
367 const char *process_name)
369 HMI_DEBUG("process_name:%s", process_name);
372 void screen_error(void *data,
373 struct ivi_wm_screen *ivi_wm_screen,
377 HMI_DEBUG("screen error:%d message:%s", error, message);
380 constexpr struct ivi_wm_screen_listener screen_listener = {
391 surface::surface(uint32_t i, struct controller *c)
392 : controller_child(c, i)
394 this->parent->add_proxy_to_sid_mapping(this->parent->proxy.get(), i);
397 void surface::set_visibility(uint32_t visibility)
399 HMI_DEBUG("compositor::surface id:%d v:%d", this->id, visibility);
400 ivi_wm_set_surface_visibility(this->parent->proxy.get(), this->id, visibility);
403 void surface::set_source_rectangle(int32_t x, int32_t y,
404 int32_t width, int32_t height)
406 ivi_wm_set_surface_source_rectangle(this->parent->proxy.get(), this->id,
407 x, y, width, height);
410 void surface::set_destination_rectangle(int32_t x, int32_t y,
411 int32_t width, int32_t height)
413 ivi_wm_set_surface_destination_rectangle(this->parent->proxy.get(), this->id,
414 x, y, width, height);
420 layer::layer(uint32_t i, struct controller *c) : layer(i, 0, 0, c) {}
422 layer::layer(uint32_t i, int32_t w, int32_t h, struct controller *c)
423 : controller_child(c, i)
425 this->parent->add_proxy_to_lid_mapping(this->parent->proxy.get(), i);
426 ivi_wm_create_layout_layer(c->proxy.get(), i, w, h);
429 void layer::set_visibility(uint32_t visibility)
431 ivi_wm_set_layer_visibility(this->parent->proxy.get(), this->id, visibility);
434 void layer::set_destination_rectangle(int32_t x, int32_t y,
435 int32_t width, int32_t height)
437 ivi_wm_set_layer_destination_rectangle(this->parent->proxy.get(), this->id,
438 x, y, width, height);
441 void layer::add_surface(uint32_t surface_id)
443 ivi_wm_layer_add_surface(this->parent->proxy.get(), this->id, surface_id);
446 void layer::remove_surface(uint32_t surface_id)
448 ivi_wm_layer_remove_surface(this->parent->proxy.get(), this->id, surface_id);
454 screen::screen(uint32_t i, struct controller *c, struct wl_output *o)
455 : wayland_proxy(ivi_wm_create_screen(c->proxy.get(), o)),
456 controller_child(c, i)
458 HMI_DEBUG("compositor::screen @ %p id %u o %p", this->proxy.get(), i, o);
460 // Add listener for screen
461 ivi_wm_screen_add_listener(this->proxy.get(), &screen_listener, this);
464 void screen::clear() { ivi_wm_screen_clear(this->proxy.get()); }
466 void screen::screen_created(struct screen *screen, uint32_t id)
468 HMI_DEBUG("compositor::screen @ %p screen %u (%x) @ %p", this->proxy.get(),
471 this->parent->screens[id] = screen;
474 void screen::set_render_order(std::vector<uint32_t> const &ro)
478 // Remove all layers from the screen render order
479 ivi_wm_screen_clear(this->proxy.get());
481 for (i = 0; i < ro.size(); i++)
483 HMI_DEBUG("compositor::screen @ %p add layer %u", this->proxy.get(), ro[i]);
484 // Add the layer to screen render order at nearest z-position
485 ivi_wm_screen_add_layer(this->proxy.get(), ro[i]);
492 controller::controller(struct wl_registry *r, uint32_t name, uint32_t version)
494 wl_registry_bind(r, name, &ivi_wm_interface, version)),
497 ivi_wm_add_listener(this->proxy.get(), &listener, this);
500 void controller::layer_create(uint32_t id, int32_t w, int32_t h)
502 this->layers[id] = std::make_unique<struct layer>(id, w, h, this);
505 void controller::surface_create(uint32_t id)
507 this->surfaces[id] = std::make_unique<struct surface>(id, this);
509 // TODO: If Clipping is necessary, this process should be modified.
511 // Set surface type:IVI_WM_SURFACE_TYPE_DESKTOP)
512 // for resizing wayland surface when switching from split to full surface.
513 ivi_wm_set_surface_type(this->proxy.get(), id, IVI_WM_SURFACE_TYPE_DESKTOP);
515 // Set source reactangle even if we should not need to set it
516 // for enable setting for destination region.
517 this->surfaces[id]->set_source_rectangle(0, 0, this->output_size.w, this->output_size.h);
520 this->display->flush();
524 void controller::create_screen(struct wl_output *output)
526 // TODO: screen id is 0 (WM manages one screen for now)
527 this->screen = std::make_unique<struct screen>(0, this, output);
530 void controller::get_surface_properties(uint32_t surface_id, int param)
532 ivi_wm_surface_get(this->proxy.get(), surface_id, param);
535 void controller::layer_created(uint32_t id)
537 HMI_DEBUG("compositor::controller @ %p layer %u (%x)", this->proxy.get(), id, id);
538 if (this->layers.find(id) != this->layers.end())
540 HMI_DEBUG("WindowManager has created layer %u (%x) already", id, id);
544 this->layers[id] = std::make_unique<struct layer>(id, this);
548 void controller::layer_error_detected(uint32_t object_id,
549 uint32_t error_code, const char *error_text)
551 HMI_DEBUG("compositor::controller @ %p error o %d c %d text %s",
552 this->proxy.get(), object_id, error_code, error_text);
555 void controller::surface_visibility_changed(uint32_t id, int32_t visibility)
557 HMI_DEBUG("compositor::surface %s @ %d v %i", __func__, id,
559 this->sprops[id].visibility = visibility;
560 this->chooks->surface_visibility(id, visibility);
563 void controller::surface_opacity_changed(uint32_t id, float opacity)
565 HMI_DEBUG("compositor::surface %s @ %d o %f",
566 __func__, id, opacity);
567 this->sprops[id].opacity = opacity;
570 void controller::surface_source_rectangle_changed(uint32_t id, int32_t x,
571 int32_t y, int32_t width,
574 HMI_DEBUG("compositor::surface %s @ %d x %i y %i w %i h %i", __func__,
575 id, x, y, width, height);
576 this->sprops[id].src_rect = rect{width, height, x, y};
579 void controller::surface_destination_rectangle_changed(uint32_t id, int32_t x,
580 int32_t y, int32_t width,
583 HMI_DEBUG("compositor::surface %s @ %d x %i y %i w %i h %i", __func__,
584 id, x, y, width, height);
585 this->sprops[id].dst_rect = rect{width, height, x, y};
586 this->chooks->surface_destination_rectangle(id, x, y, width, height);
589 void controller::surface_size_changed(uint32_t id, int32_t width,
592 HMI_DEBUG("compositor::surface %s @ %d w %i h %i", __func__, id,
594 this->sprops[id].size = size{uint32_t(width), uint32_t(height)};
595 this->surfaces[id]->set_source_rectangle(0, 0, width, height);
598 void controller::surface_added_to_layer(uint32_t layer_id, uint32_t surface_id)
600 HMI_DEBUG("compositor::surface %s @ %d l %u",
601 __func__, layer_id, surface_id);
604 void controller::surface_stats_received(uint32_t surface_id,
605 uint32_t frame_count, uint32_t pid)
607 HMI_DEBUG("compositor::surface %s @ %d f %u pid %u",
608 __func__, surface_id, frame_count, pid);
611 void controller::surface_created(uint32_t id)
613 HMI_DEBUG("compositor::controller @ %p surface %u (%x)", this->proxy.get(), id,
615 if (this->surfaces.find(id) == this->surfaces.end())
617 this->surfaces[id] = std::make_unique<struct surface>(id, this);
618 this->chooks->surface_created(id);
620 // Set surface type:IVI_WM_SURFACE_TYPE_DESKTOP)
621 // for resizing wayland surface when switching from split to full surface.
622 ivi_wm_set_surface_type(this->proxy.get(), id, IVI_WM_SURFACE_TYPE_DESKTOP);
625 this->display->flush();
629 void controller::surface_destroyed(uint32_t surface_id)
631 HMI_DEBUG("compositor::surface %s @ %d", __func__, surface_id);
632 this->chooks->surface_removed(surface_id);
633 this->sprops.erase(surface_id);
634 this->surfaces.erase(surface_id);
637 void controller::surface_error_detected(uint32_t object_id,
638 uint32_t error_code, const char *error_text)
640 HMI_DEBUG("compositor::controller @ %p error o %d c %d text %s",
641 this->proxy.get(), object_id, error_code, error_text);
644 void controller::layer_visibility_changed(uint32_t layer_id, int32_t visibility)
646 HMI_DEBUG("compositor::layer %s @ %d v %i", __func__, layer_id, visibility);
647 this->lprops[layer_id].visibility = visibility;
650 void controller::layer_opacity_changed(uint32_t layer_id, float opacity)
652 HMI_DEBUG("compositor::layer %s @ %d o %f", __func__, layer_id, opacity);
653 this->lprops[layer_id].opacity = opacity;
656 void controller::layer_source_rectangle_changed(uint32_t layer_id,
657 int32_t x, int32_t y,
658 int32_t width, int32_t height)
660 HMI_DEBUG("compositor::layer %s @ %d x %i y %i w %i h %i",
661 __func__, layer_id, x, y, width, height);
662 this->lprops[layer_id].src_rect = rect{width, height, x, y};
665 void controller::layer_destination_rectangle_changed(uint32_t layer_id,
666 int32_t x, int32_t y,
667 int32_t width, int32_t height)
669 HMI_DEBUG("compositor::layer %s @ %d x %i y %i w %i h %i",
670 __func__, layer_id, x, y, width, height);
671 this->lprops[layer_id].dst_rect = rect{width, height, x, y};
674 void controller::layer_destroyed(uint32_t layer_id)
676 HMI_DEBUG("compositor::layer %s @ %d", __func__, layer_id);
677 this->lprops.erase(layer_id);
678 this->layers.erase(layer_id);
681 void controller::add_proxy_to_sid_mapping(struct ivi_wm *p,
684 HMI_DEBUG("Add surface proxy mapping for %p (%u)", p, id);
685 this->surface_proxy_to_id[uintptr_t(p)] = id;
686 this->sprops[id].id = id;
689 void controller::remove_proxy_to_sid_mapping(struct ivi_wm *p)
691 HMI_DEBUG("Remove surface proxy mapping for %p", p);
692 this->surface_proxy_to_id.erase(uintptr_t(p));
695 void controller::add_proxy_to_lid_mapping(struct ivi_wm *p,
698 HMI_DEBUG("Add layer proxy mapping for %p (%u)", p, id);
699 this->layer_proxy_to_id[uintptr_t(p)] = id;
700 this->lprops[id].id = id;
703 void controller::remove_proxy_to_lid_mapping(struct ivi_wm *p)
705 HMI_DEBUG("Remove layer proxy mapping for %p", p);
706 this->layer_proxy_to_id.erase(uintptr_t(p));
709 void controller::add_proxy_to_id_mapping(struct wl_output *p, uint32_t id)
711 HMI_DEBUG("Add screen proxy mapping for %p (%u)", p, id);
712 this->screen_proxy_to_id[uintptr_t(p)] = id;
715 void controller::remove_proxy_to_id_mapping(struct wl_output *p)
717 HMI_DEBUG("Remove screen proxy mapping for %p", p);
718 this->screen_proxy_to_id.erase(uintptr_t(p));
721 } // namespace compositor