Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit

Permalink
vm_tools: Uses templates for sommelier's HostEventHandler
Browse files Browse the repository at this point in the history
+ Adds templated version of HostEventHandler where it takes a template
  now.

NOTE: This unfortunately needs to be mapped via the
    MAP_STRUCT_TO_LISTENER macro because there isn't a nice way to
    convert a `struct <blah>` -> `<blah>_listener`

BUG=b:255439799
TEST=meson build && ninja -C build && build/sommelier_test

Change-Id: I8aae3d44dd438b7c1799fde781b51715c9ad03ee
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform2/+/4439989
Tested-by: Justin Huang <[email protected]>
Reviewed-by: Chloe Pelling <[email protected]>
Auto-Submit: Justin Huang <[email protected]>
Commit-Queue: Justin Huang <[email protected]>
  • Loading branch information
Justin Huang authored and Chromeos LUCI committed Apr 20, 2023
1 parent e6c7506 commit 4a24fb4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 77 deletions.
58 changes: 0 additions & 58 deletions testing/sommelier-test-util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,64 +9,6 @@
namespace vm_tools {
namespace sommelier {

const zaura_toplevel_listener* HostEventHandler(
struct zaura_toplevel* aura_toplevel) {
const void* listener =
wl_proxy_get_listener(reinterpret_cast<wl_proxy*>(aura_toplevel));
EXPECT_NE(listener, nullptr);
return static_cast<const zaura_toplevel_listener*>(listener);
}

const xdg_surface_listener* HostEventHandler(struct xdg_surface* xdg_surface) {
const void* listener =
wl_proxy_get_listener(reinterpret_cast<wl_proxy*>(xdg_surface));
EXPECT_NE(listener, nullptr);
return static_cast<const xdg_surface_listener*>(listener);
}

const xdg_toplevel_listener* HostEventHandler(
struct xdg_toplevel* xdg_toplevel) {
const void* listener =
wl_proxy_get_listener(reinterpret_cast<wl_proxy*>(xdg_toplevel));
EXPECT_NE(listener, nullptr);
return static_cast<const xdg_toplevel_listener*>(listener);
}

const wl_callback_listener* HostEventHandler(struct wl_callback* callback) {
const void* listener =
wl_proxy_get_listener(reinterpret_cast<wl_proxy*>(callback));
EXPECT_NE(listener, nullptr);
return static_cast<const wl_callback_listener*>(listener);
}

const wl_output_listener* HostEventHandler(struct wl_output* output) {
const void* listener =
wl_proxy_get_listener(reinterpret_cast<wl_proxy*>(output));
EXPECT_NE(listener, nullptr);
return static_cast<const wl_output_listener*>(listener);
}

const zaura_output_listener* HostEventHandler(struct zaura_output* output) {
const void* listener =
wl_proxy_get_listener(reinterpret_cast<wl_proxy*>(output));
EXPECT_NE(listener, nullptr);
return static_cast<const zaura_output_listener*>(listener);
}

const wl_surface_listener* HostEventHandler(struct wl_surface* surface) {
const void* listener =
wl_proxy_get_listener(reinterpret_cast<wl_proxy*>(surface));
EXPECT_NE(listener, nullptr);
return static_cast<const wl_surface_listener*>(listener);
}

const zxdg_output_v1_listener* HostEventHandler(struct zxdg_output_v1* output) {
const void* listener =
wl_proxy_get_listener(reinterpret_cast<wl_proxy*>(output));
EXPECT_NE(listener, nullptr);
return static_cast<const zxdg_output_v1_listener*>(listener);
}

uint32_t XdgToplevelId(sl_window* window) {
assert(window->xdg_toplevel);
return wl_proxy_get_id(reinterpret_cast<wl_proxy*>(window->xdg_toplevel));
Expand Down
48 changes: 29 additions & 19 deletions testing/sommelier-test-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,49 @@
#ifndef VM_TOOLS_SOMMELIER_TESTING_SOMMELIER_TEST_UTIL_H_
#define VM_TOOLS_SOMMELIER_TESTING_SOMMELIER_TEST_UTIL_H_

#include <gtest/gtest.h>
#include <wayland-server.h>

#include "../sommelier.h" // NOLINT(build/include_directory)
#include "aura-shell-client-protocol.h" // NOLINT(build/include_directory)
#include "viewporter-client-protocol.h" // NOLINT(build/include_directory)
#include "xdg-output-unstable-v1-client-protocol.h" // NOLINT(build/include_directory)
#include "xdg-shell-client-protocol.h" // NOLINT(build/include_directory)
#include "xdg-shell-client-protocol.h" // NOLINT(build/include_directory)

// Maps a wayland object to its listener struct.
template <typename WaylandType>
struct WlToListener;

#define MAP_STRUCT_TO_LISTENER(WlType, Listener) \
template <> \
struct WlToListener<WlType> { \
using type = Listener; \
};

MAP_STRUCT_TO_LISTENER(xdg_surface*, xdg_surface_listener);
MAP_STRUCT_TO_LISTENER(xdg_toplevel*, xdg_toplevel_listener);
MAP_STRUCT_TO_LISTENER(wl_output*, wl_output_listener);
MAP_STRUCT_TO_LISTENER(wl_callback*, wl_callback_listener);
MAP_STRUCT_TO_LISTENER(wl_surface*, wl_surface_listener);
MAP_STRUCT_TO_LISTENER(zaura_output*, zaura_output_listener);
MAP_STRUCT_TO_LISTENER(zaura_toplevel*, zaura_toplevel_listener);
MAP_STRUCT_TO_LISTENER(zxdg_output_v1*, zxdg_output_v1_listener);

namespace vm_tools {
namespace sommelier {

// This family of functions retrieves Sommelier's listeners for events received
// This function retrieves Sommelier's listeners for events received
// from the host, so we can call them directly in the test rather than
// (a) exporting the actual functions (which are typically static), or (b)
// creating a fake host compositor to dispatch events via libwayland
// (unnecessarily complicated).
const zaura_toplevel_listener* HostEventHandler(
struct zaura_toplevel* aura_toplevel);

const xdg_surface_listener* HostEventHandler(struct xdg_surface* xdg_surface);

const xdg_toplevel_listener* HostEventHandler(
struct xdg_toplevel* xdg_toplevel);

const wl_callback_listener* HostEventHandler(struct wl_callback* callback);

const wl_output_listener* HostEventHandler(struct wl_output* output);

const zaura_output_listener* HostEventHandler(struct zaura_output* output);

const wl_surface_listener* HostEventHandler(struct wl_surface* surface);

const zxdg_output_v1_listener* HostEventHandler(struct zxdg_output_v1* output);
template <typename T>
const typename WlToListener<T>::type* HostEventHandler(T proxy) {
const void* listener =
wl_proxy_get_listener(reinterpret_cast<wl_proxy*>(proxy));
EXPECT_NE(listener, nullptr);
return static_cast<const typename WlToListener<T>::type*>(listener);
}

uint32_t XdgToplevelId(sl_window* window);
uint32_t AuraSurfaceId(sl_window* window);
Expand Down

0 comments on commit 4a24fb4

Please sign in to comment.