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.
20 #include "wayland_ivi_wm.hpp"
21 #include "hmi-debug.h"
32 : d(std::unique_ptr<struct wl_display, void (*)(struct wl_display *)>(
33 wl_display_connect(nullptr), &wl_display_disconnect)),
36 bool display::ok() const { return d && wl_display_get_error(d.get()) == 0; }
38 void display::roundtrip() { wl_display_roundtrip(this->d.get()); }
40 int display::dispatch() { return wl_display_dispatch(this->d.get()); }
42 int display::dispatch_pending() { return wl_display_dispatch_pending(this->d.get()); }
44 int display::read_events() {
46 while (wl_display_prepare_read(this->d.get()) == -1) {
47 STN(pending_events_dispatch);
48 if (wl_display_dispatch_pending(this->d.get()) == -1) {
53 if (wl_display_flush(this->d.get()) == -1) {
57 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()); }
74 void registry_global_created(void *data, struct wl_registry * /*r*/, uint32_t name,
75 char const *iface, uint32_t v) {
76 static_cast<struct registry *>(data)->global_created(name, iface, v);
79 void registry_global_removed(void *data, struct wl_registry * /*r*/,
81 static_cast<struct registry *>(data)->global_removed(name);
84 constexpr struct wl_registry_listener registry_listener = {
85 registry_global_created, registry_global_removed};
88 registry::registry(struct wl_display *d)
89 : wayland_proxy(d == nullptr ? nullptr : wl_display_get_registry(d)) {
90 if (this->proxy != nullptr) {
91 wl_registry_add_listener(this->proxy.get(), ®istry_listener, this);
95 void registry::add_global_handler(char const *iface, binder bind) {
96 this->bindings[iface] = std::move(bind);
99 void registry::global_created(uint32_t name, char const *iface, uint32_t v) {
100 auto b = this->bindings.find(iface);
101 if (b != this->bindings.end()) {
102 b->second(this->proxy.get(), name, v);
104 HMI_DEBUG("wm", "wl::registry @ %p global n %u i %s v %u", this->proxy.get(), name,
108 void registry::global_removed(uint32_t /*name*/) {}
114 void output_geometry(void *data, struct wl_output * /*wl_output*/, int32_t x,
115 int32_t y, int32_t physical_width, int32_t physical_height,
116 int32_t subpixel, const char *make, const char *model,
118 static_cast<struct output *>(data)->geometry(
119 x, y, physical_width, physical_height, subpixel, make, model, transform);
122 void output_mode(void *data, struct wl_output * /*wl_output*/, uint32_t flags,
123 int32_t width, int32_t height, int32_t refresh) {
124 static_cast<struct output *>(data)->mode(flags, width, height, refresh);
127 void output_done(void *data, struct wl_output * /*wl_output*/) {
128 static_cast<struct output *>(data)->done();
131 void output_scale(void *data, struct wl_output * /*wl_output*/,
133 static_cast<struct output *>(data)->scale(factor);
136 constexpr struct wl_output_listener output_listener = {
137 output_geometry, output_mode, output_done, output_scale};
140 output::output(struct wl_registry *r, uint32_t name, uint32_t v)
141 : wayland_proxy(wl_registry_bind(r, name, &wl_output_interface, v)) {
142 wl_output_add_listener(this->proxy.get(), &output_listener, this);
145 void output::geometry(int32_t x, int32_t y, int32_t pw, int32_t ph,
146 int32_t subpel, char const *make, char const *model,
149 "wl::output %s @ %p x %i y %i w %i h %i spel %x make %s model %s tx %i",
150 __func__, this->proxy.get(), x, y, pw, ph, subpel, make, model, tx);
151 this->transform = tx;
154 void output::mode(uint32_t flags, int32_t w, int32_t h, int32_t r) {
155 HMI_DEBUG("wm", "wl::output %s @ %p f %x w %i h %i r %i", __func__,
156 this->proxy.get(), flags, w, h, r);
157 if ((flags & WL_OUTPUT_MODE_CURRENT) != 0u) {
164 void output::done() {
165 HMI_DEBUG("wm", "wl::output %s @ %p done", __func__, this->proxy.get());
167 if (this->transform == WL_OUTPUT_TRANSFORM_90 ||
168 this->transform == WL_OUTPUT_TRANSFORM_270 ||
169 this->transform == WL_OUTPUT_TRANSFORM_FLIPPED_90 ||
170 this->transform == WL_OUTPUT_TRANSFORM_FLIPPED_270) {
171 std::swap(this->width, this->height);
175 void output::scale(int32_t factor) {
176 HMI_DEBUG("wm", "wl::output %s @ %p f %i", __func__, this->proxy.get(), factor);
181 * namespace compositor
183 namespace compositor {
187 void surface_visibility_changed(
188 void *data, struct ivi_wm * /*ivi_wm*/,
189 uint32_t surface_id, int32_t visibility) {
190 auto s = static_cast<struct surface *>(data);
191 s->parent->surface_visibility_changed(s, visibility);
194 void surface_opacity_changed(void *data, struct ivi_wm * /*ivi_wm*/,
195 uint32_t surface_id, wl_fixed_t opacity) {
196 auto s = static_cast<struct surface *>(data);
197 s->parent->surface_opacity_changed(s, float(wl_fixed_to_double(opacity)));
200 void surface_source_rectangle_changed(
201 void *data, struct ivi_wm * /*ivi_wm*/, uint32_t surface_id,
202 int32_t x, int32_t y, int32_t width, int32_t height) {
203 auto s = static_cast<struct surface *>(data);
204 s->parent->surface_source_rectangle_changed(s, x, y, width, height);
207 void surface_destination_rectangle_changed(
208 void *data, struct ivi_wm * /*ivi_wm*/, uint32_t surface_id,
209 int32_t x, int32_t y, int32_t width, int32_t height) {
210 auto s = static_cast<struct surface *>(data);
211 s->parent->surface_destination_rectangle_changed(s, x, y, width, height);
214 void surface_created(void *data, struct ivi_wm * /*ivi_wm*/,
215 uint32_t id_surface) {
216 static_cast<struct controller *>(data)->surface_created(id_surface);
219 void surface_destroyed(
220 void *data, struct ivi_wm * /*ivi_wm*/, uint32_t surface_id) {
221 auto s = static_cast<struct surface *>(data);
222 s->parent->surface_destroyed(s, surface_id);
225 void surface_error_detected(void *data, struct ivi_wm * /*ivi_wm*/, uint32_t object_id,
226 uint32_t error_code, const char *error_text) {
227 static_cast<struct controller *>(data)->surface_error_detected(
228 object_id, error_code, error_text);
231 void surface_size_changed(
232 void *data, struct ivi_wm * /*ivi_wm*/, uint32_t surface_id,
233 int32_t width, int32_t height) {
234 auto s = static_cast<struct surface *>(data);
235 s->parent->surface_size_changed(s, width, height);
238 void surface_stats_received(void *data, struct ivi_wm * /*ivi_wm*/,
239 uint32_t surface_id, uint32_t frame_count, uint32_t pid) {
240 auto s = static_cast<struct surface *>(data);
241 s->parent->surface_stats_received(s, surface_id, frame_count, pid);
244 void surface_added_to_layer(void *data, struct ivi_wm * /*ivi_wm*/,
245 uint32_t layer_id, uint32_t surface_id) {
246 auto s = static_cast<struct surface *>(data);
247 s->parent->surface_added_to_layer(s, layer_id, surface_id);
250 void layer_visibility_changed(void *data, struct ivi_wm * /*ivi_wm*/,
251 uint32_t layer_id, int32_t visibility) {
252 auto l = static_cast<struct layer *>(data);
253 l->parent->layer_visibility_changed(l, layer_id, visibility);
256 void layer_opacity_changed(void *data, struct ivi_wm * /*ivi_wm*/,
257 uint32_t layer_id, wl_fixed_t opacity) {
258 auto l = static_cast<struct layer *>(data);
259 l->parent->layer_opacity_changed(l, layer_id, float(wl_fixed_to_double(opacity)));
262 void layer_source_rectangle_changed(
263 void *data, struct ivi_wm * /*ivi_wm*/, uint32_t layer_id,
264 int32_t x, int32_t y, int32_t width, int32_t height) {
265 auto l = static_cast<struct layer *>(data);
266 l->parent->layer_source_rectangle_changed(l, layer_id, x, y, width, height);
269 void layer_destination_rectangle_changed(
270 void *data, struct ivi_wm * /*ivi_wm*/, uint32_t layer_id,
271 int32_t x, int32_t y, int32_t width, int32_t height) {
272 auto l = static_cast<struct layer *>(data);
273 l->parent->layer_destination_rectangle_changed(l, layer_id, x, y, width, height);
276 void layer_created(void *data, struct ivi_wm * /*ivi_wm*/,
278 static_cast<struct controller *>(data)->layer_created(id_layer);
281 void layer_destroyed(void *data, struct ivi_wm * /*ivi_wm*/, uint32_t layer_id) {
282 auto l = static_cast<struct layer *>(data);
283 l->parent->layer_destroyed(l, layer_id);
286 void layer_error_detected(void *data, struct ivi_wm * /*ivi_wm*/, uint32_t object_id,
287 uint32_t error_code, const char *error_text) {
288 static_cast<struct controller *>(data)->layer_error_detected(
289 object_id, error_code, error_text);
292 constexpr struct ivi_wm_listener listener = {
293 surface_visibility_changed, layer_visibility_changed,
294 surface_opacity_changed, layer_opacity_changed,
295 surface_source_rectangle_changed, layer_source_rectangle_changed,
296 surface_destination_rectangle_changed, layer_destination_rectangle_changed,
297 surface_created, layer_created,
298 surface_destroyed, layer_destroyed,
299 surface_error_detected, layer_error_detected,
300 surface_size_changed,
301 surface_stats_received,
302 surface_added_to_layer,
305 void screen_created(void *data, struct ivi_wm_screen *ivi_wm_screen, uint32_t id) {
306 static_cast<struct screen *>(data)->screen_created((struct screen *)data, id);
309 void layer_added(void *data,
310 struct ivi_wm_screen *ivi_wm_screen,
312 HMI_DEBUG("wm", "added layer_id:%d", layer_id);
315 void connector_name(void *data,
316 struct ivi_wm_screen *ivi_wm_screen,
317 const char *process_name) {
318 HMI_DEBUG("wm", "process_name:%s", process_name);
321 void screen_error(void *data,
322 struct ivi_wm_screen *ivi_wm_screen,
324 const char *message) {
325 HMI_DEBUG("wm", "screen error:%d message:%s", error, message);
328 constexpr struct ivi_wm_screen_listener screen_listener = {
339 surface::surface(uint32_t i, struct controller *c)
340 : controller_child(c, i) {
341 this->parent->add_proxy_to_sid_mapping(this->parent->proxy.get(), i);
344 void surface::set_visibility(uint32_t visibility) {
345 HMI_DEBUG("wm", "compositor::surface id:%d v:%d", this->id, visibility);
346 ivi_wm_set_surface_visibility(this->parent->proxy.get(), this->id, visibility);
349 void surface::set_source_rectangle(int32_t x, int32_t y,
350 int32_t width, int32_t height) {
351 ivi_wm_set_surface_source_rectangle(this->parent->proxy.get(), this->id,
352 x, y, width, height);
355 void surface::set_destination_rectangle(int32_t x, int32_t y,
356 int32_t width, int32_t height) {
357 ivi_wm_set_surface_destination_rectangle(this->parent->proxy.get(), this->id,
358 x, y, width, height);
365 layer::layer(uint32_t i, struct controller *c) : layer(i, 0, 0, c) {}
367 layer::layer(uint32_t i, int32_t w, int32_t h, struct controller *c)
368 : controller_child(c, i) {
369 this->parent->add_proxy_to_lid_mapping(this->parent->proxy.get(), i);
370 ivi_wm_create_layout_layer(c->proxy.get(), i, w, h);
373 void layer::set_visibility(uint32_t visibility) {
374 ivi_wm_set_layer_visibility(this->parent->proxy.get(), this->id, visibility);
377 void layer::set_destination_rectangle(int32_t x, int32_t y,
378 int32_t width, int32_t height) {
379 ivi_wm_set_layer_destination_rectangle(this->parent->proxy.get(), this->id,
380 x, y, width, height);
383 void layer::add_surface(uint32_t surface_id) {
384 ivi_wm_layer_add_surface(this->parent->proxy.get(), this->id, surface_id);
387 void layer::remove_surface(uint32_t surface_id) {
388 ivi_wm_layer_remove_surface(this->parent->proxy.get(), this->id, surface_id);
395 screen::screen(uint32_t i, struct controller *c, struct wl_output *o)
396 : wayland_proxy(ivi_wm_create_screen(c->proxy.get(), o)),
397 controller_child(c, i) {
398 HMI_DEBUG("wm", "compositor::screen @ %p id %u o %p", this->proxy.get(), i, o);
400 // Add listener for screen
401 ivi_wm_screen_add_listener(this->proxy.get(), &screen_listener, this);
404 void screen::clear() { ivi_wm_screen_clear(this->proxy.get()); }
406 void screen::screen_created(struct screen *screen, uint32_t id) {
407 HMI_DEBUG("wm", "compositor::screen @ %p screen %u (%x) @ %p", this->proxy.get(),
410 this->parent->screens[id] = screen;
413 void screen::set_render_order(std::vector<uint32_t> const &ro) {
416 // Remove all layers from the screen render order
417 ivi_wm_screen_clear(this->proxy.get());
419 for (i = 0; i < ro.size(); i++) {
420 HMI_DEBUG("wm", "compositor::screen @ %p add layer %u", this->proxy.get(), ro[i]);
421 // Add the layer to screen render order at nearest z-position
422 ivi_wm_screen_add_layer(this->proxy.get(), ro[i]);
430 controller::controller(struct wl_registry *r, uint32_t name, uint32_t version)
432 wl_registry_bind(r, name, &ivi_wm_interface, version)),
434 ivi_wm_add_listener(this->proxy.get(), &listener, this);
437 void controller::layer_create(uint32_t id, int32_t w, int32_t h) {
438 this->layers[id] = std::make_unique<struct layer>(id, w, h, this);
441 void controller::surface_create(uint32_t id) {
442 this->surfaces[id] = std::make_unique<struct surface>(id, this);
444 // TODO: If Clipping is necessary, this process should be modified.
446 // Set surface type:IVI_WM_SURFACE_TYPE_DESKTOP)
447 // for resizing wayland surface when switching from split to full surface.
448 ivi_wm_set_surface_type(this->proxy.get(), id, IVI_WM_SURFACE_TYPE_DESKTOP);
450 // Set source reactangle even if we should not need to set it
451 // for enable setting for destination region.
452 this->surfaces[id]->set_source_rectangle(0, 0, this->output_size.w, this->output_size.h);
455 this->display->flush();
459 void controller::create_screen(struct wl_output *output) {
460 // TODO: screen id is 0 (WM manages one screen for now)
461 this->screen = std::make_unique<struct screen>(0, this, output);
464 void controller::layer_created(uint32_t id) {
465 HMI_DEBUG("wm", "compositor::controller @ %p layer %u (%x)", this->proxy.get(), id, id);
466 if (this->layers.find(id) != this->layers.end()) {
467 HMI_DEBUG("wm", "WindowManager has created layer %u (%x) already", id, id);
469 this->layers[id] = std::make_unique<struct layer>(id, this);
473 void controller::layer_error_detected(uint32_t object_id,
474 uint32_t error_code, const char *error_text) {
475 HMI_DEBUG("wm", "compositor::controller @ %p error o %d c %d text %s",
476 this->proxy.get(), object_id, error_code, error_text);
479 void controller::surface_visibility_changed(struct surface *s, int32_t visibility) {
480 HMI_DEBUG("wm", "compositor::surface %s @ %d v %i", __func__, s->id,
482 this->sprops[s->id].visibility = visibility;
483 this->chooks->surface_visibility(s->id, visibility);
486 void controller::surface_opacity_changed(struct surface *s, float opacity) {
487 HMI_DEBUG("wm", "compositor::surface %s @ %d o %f", __func__, s->id,
489 this->sprops[s->id].opacity = opacity;
492 void controller::surface_source_rectangle_changed(struct surface *s, int32_t x,
493 int32_t y, int32_t width,
495 HMI_DEBUG("wm", "compositor::surface %s @ %d x %i y %i w %i h %i", __func__,
496 s->id, x, y, width, height);
497 this->sprops[s->id].src_rect = rect{width, height, x, y};
500 void controller::surface_destination_rectangle_changed(struct surface *s, int32_t x,
501 int32_t y, int32_t width,
503 HMI_DEBUG("wm", "compositor::surface %s @ %d x %i y %i w %i h %i", __func__,
504 s->id, x, y, width, height);
505 this->sprops[s->id].dst_rect = rect{width, height, x, y};
506 this->chooks->surface_destination_rectangle(s->id, x, y, width, height);
509 void controller::surface_size_changed(struct surface *s, int32_t width,
511 HMI_DEBUG("wm", "compositor::surface %s @ %d w %i h %i", __func__, s->id,
513 this->sprops[s->id].size = size{uint32_t(width), uint32_t(height)};
516 void controller::surface_added_to_layer(struct surface * s,
517 uint32_t layer_id, uint32_t surface_id) {
518 HMI_DEBUG("wm", "compositor::surface %s @ %d l %u",
519 __func__, layer_id, surface_id);
522 void controller::surface_stats_received(struct surface *s, uint32_t surface_id,
523 uint32_t frame_count, uint32_t pid) {
524 HMI_DEBUG("wm", "compositor::surface %s @ %d f %u pid %u",
525 __func__, surface_id, frame_count, pid);
528 void controller::surface_created(uint32_t id) {
529 HMI_DEBUG("wm", "compositor::controller @ %p surface %u (%x)", this->proxy.get(), id,
531 if (this->surfaces.find(id) == this->surfaces.end()) {
532 this->surfaces[id] = std::make_unique<struct surface>(id, this);
533 this->chooks->surface_created(id);
535 // TODO: If Clipping is necessary, this process should be modified.
537 // Set surface type:IVI_WM_SURFACE_TYPE_DESKTOP)
538 // for resizing wayland surface when switching from split to full surface.
539 ivi_wm_set_surface_type(this->proxy.get(), id, IVI_WM_SURFACE_TYPE_DESKTOP);
541 // Set source reactangle even if we should not need to set it
542 // for enable setting for destination region.
543 this->surfaces[id]->set_source_rectangle(0, 0, this->output_size.w, this->output_size.h);
546 this->display->flush();
551 void controller::surface_destroyed(struct surface *s, uint32_t surface_id) {
552 HMI_DEBUG("wm", "compositor::surface %s @ %d", __func__, surface_id);
553 this->chooks->surface_removed(surface_id);
554 this->sprops.erase(surface_id);
555 this->surfaces.erase(surface_id);
558 void controller::surface_error_detected(uint32_t object_id,
559 uint32_t error_code, const char *error_text) {
560 HMI_DEBUG("wm", "compositor::controller @ %p error o %d c %d text %s",
561 this->proxy.get(), object_id, error_code, error_text);
564 void controller::layer_visibility_changed(struct layer *l, uint32_t layer_id, int32_t visibility) {
565 HMI_DEBUG("wm", "compositor::layer %s @ %d v %i", __func__, layer_id, visibility);
566 this->lprops[layer_id].visibility = visibility;
569 void controller::layer_opacity_changed(struct layer *l, uint32_t layer_id, float opacity) {
570 HMI_DEBUG("wm", "compositor::layer %s @ %d o %f", __func__, layer_id, opacity);
571 this->lprops[layer_id].opacity = opacity;
574 void controller::layer_source_rectangle_changed(struct layer *l, uint32_t layer_id,
575 int32_t x, int32_t y,
576 int32_t width, int32_t height) {
577 HMI_DEBUG("wm", "compositor::layer %s @ %d x %i y %i w %i h %i",
578 __func__, layer_id, x, y, width, height);
579 this->lprops[layer_id].src_rect = rect{width, height, x, y};
582 void controller::layer_destination_rectangle_changed(struct layer *l, uint32_t layer_id,
583 int32_t x, int32_t y,
584 int32_t width, int32_t height) {
585 HMI_DEBUG("wm", "compositor::layer %s @ %d x %i y %i w %i h %i",
586 __func__, layer_id, x, y, width, height);
587 this->lprops[layer_id].dst_rect = rect{width, height, x, y};
590 void controller::layer_configuration(struct layer *l, int32_t width,
592 HMI_DEBUG("wm", "compositor::layer %s @ %d w %i h %i", __func__, l->id,
594 this->lprops[l->id].size = size{uint32_t(width), uint32_t(height)};
597 void controller::layer_orientation(struct layer *l, int32_t orientation) {
598 HMI_DEBUG("wm", "compositor::layer %s @ %d o %i", __func__, l->id,
600 this->lprops[l->id].orientation = orientation;
603 void controller::layer_screen(struct layer *l, struct wl_output *screen) {
604 HMI_DEBUG("wm", "compositor::layer %s @ %d s %p", __func__, l->id, screen);
607 void controller::layer_destroyed(struct layer *l, uint32_t layer_id) {
608 HMI_DEBUG("wm", "compositor::layer %s @ %d", __func__, layer_id);
609 this->lprops.erase(layer_id);
610 this->layers.erase(layer_id);
613 void controller::add_proxy_to_sid_mapping(struct ivi_wm *p,
615 HMI_DEBUG("wm", "Add surface proxy mapping for %p (%u)", p, id);
616 this->surface_proxy_to_id[uintptr_t(p)] = id;
617 this->sprops[id].id = id;
620 void controller::remove_proxy_to_sid_mapping(struct ivi_wm *p) {
621 HMI_DEBUG("wm", "Remove surface proxy mapping for %p", p);
622 this->surface_proxy_to_id.erase(uintptr_t(p));
625 void controller::add_proxy_to_lid_mapping(struct ivi_wm *p,
627 HMI_DEBUG("wm", "Add layer proxy mapping for %p (%u)", p, id);
628 this->layer_proxy_to_id[uintptr_t(p)] = id;
629 this->lprops[id].id = id;
632 void controller::remove_proxy_to_lid_mapping(struct ivi_wm *p) {
633 HMI_DEBUG("wm", "Remove layer proxy mapping for %p", p);
634 this->layer_proxy_to_id.erase(uintptr_t(p));
637 void controller::add_proxy_to_id_mapping(struct wl_output *p, uint32_t id) {
638 HMI_DEBUG("wm", "Add screen proxy mapping for %p (%u)", p, id);
639 this->screen_proxy_to_id[uintptr_t(p)] = id;
642 void controller::remove_proxy_to_id_mapping(struct wl_output *p) {
643 HMI_DEBUG("wm", "Remove screen proxy mapping for %p", p);
644 this->screen_proxy_to_id.erase(uintptr_t(p));
647 } // namespace compositor