foreign-toplevel: simplify and fully separate ext-foreign/wlr-foreign

Currently, the dependencies between foreign-toplevel[-internal],
ext-foreign, and wlr-foreign are cyclical and a bit complex.

I suggest we reorganize it into a simpler hierarchy:

  foreign-toplevel/
    -> foreign.c/h
      -> (depends on) ext-foreign.c/h
      -> (depends on) wlr-foreign.c/h

The refactored code is smaller and (IMO) easier to follow.

In detail:

- Add include/foreign-toplevel folder mirroring src/foreign-toplevel
- Split foreign-toplevel-internal.h to ext-foreign.h and wlr-foreign.h
- Eliminate ext-/wlr-foreign.c -> foreign.c reverse dependencies
  (including internal signals and foreign_request* functions)
- Make struct foreign_toplevel private to foreign.c

Lightly tested with qmpanel (which uses wlr-foreign-toplevel).

v2: reorder foreign-toplevel internal API funcs
This commit is contained in:
John Lindgren 2025-08-07 20:30:59 -04:00
parent 55b495f398
commit 02df0a15d7
11 changed files with 202 additions and 271 deletions

View file

@ -1,10 +1,10 @@
// SPDX-License-Identifier: GPL-2.0-only
#include "foreign-toplevel/ext-foreign.h"
#include <assert.h>
#include <wlr/types/wlr_ext_foreign_toplevel_list_v1.h>
#include "common/macros.h"
#include "labwc.h"
#include "view.h"
#include "foreign-toplevel-internal.h"
/* ext signals */
static void
@ -20,9 +20,6 @@ handle_handle_destroy(struct wl_listener *listener, void *data)
wl_list_remove(&ext_toplevel->on_view.new_app_id.link);
wl_list_remove(&ext_toplevel->on_view.new_title.link);
/* Internal signals */
wl_list_remove(&ext_toplevel->on_foreign_toplevel.toplevel_destroy.link);
ext_toplevel->handle = NULL;
}
@ -30,60 +27,44 @@ handle_handle_destroy(struct wl_listener *listener, void *data)
static void
handle_new_app_id(struct wl_listener *listener, void *data)
{
struct foreign_toplevel *toplevel =
wl_container_of(listener, toplevel, ext_toplevel.on_view.new_app_id);
assert(toplevel->ext_toplevel.handle);
struct ext_foreign_toplevel *ext_toplevel =
wl_container_of(listener, ext_toplevel, on_view.new_app_id);
assert(ext_toplevel->handle);
struct wlr_ext_foreign_toplevel_handle_v1_state state = {
.title = view_get_string_prop(toplevel->view, "title"),
.app_id = view_get_string_prop(toplevel->view, "app_id")
.title = view_get_string_prop(ext_toplevel->view, "title"),
.app_id = view_get_string_prop(ext_toplevel->view, "app_id")
};
wlr_ext_foreign_toplevel_handle_v1_update_state(
toplevel->ext_toplevel.handle, &state);
wlr_ext_foreign_toplevel_handle_v1_update_state(ext_toplevel->handle,
&state);
}
static void
handle_new_title(struct wl_listener *listener, void *data)
{
struct foreign_toplevel *toplevel =
wl_container_of(listener, toplevel, ext_toplevel.on_view.new_title);
assert(toplevel->ext_toplevel.handle);
struct ext_foreign_toplevel *ext_toplevel =
wl_container_of(listener, ext_toplevel, on_view.new_title);
assert(ext_toplevel->handle);
struct wlr_ext_foreign_toplevel_handle_v1_state state = {
.title = view_get_string_prop(toplevel->view, "title"),
.app_id = view_get_string_prop(toplevel->view, "app_id")
.title = view_get_string_prop(ext_toplevel->view, "title"),
.app_id = view_get_string_prop(ext_toplevel->view, "app_id")
};
wlr_ext_foreign_toplevel_handle_v1_update_state(
toplevel->ext_toplevel.handle, &state);
}
/* Internal signals */
static void
handle_toplevel_destroy(struct wl_listener *listener, void *data)
{
struct ext_foreign_toplevel *ext_toplevel =
wl_container_of(listener, ext_toplevel, on_foreign_toplevel.toplevel_destroy);
if (!ext_toplevel->handle) {
return;
}
/* invokes handle_handle_destroy() which does more cleanup */
wlr_ext_foreign_toplevel_handle_v1_destroy(ext_toplevel->handle);
wlr_ext_foreign_toplevel_handle_v1_update_state(ext_toplevel->handle,
&state);
}
/* Internal API */
void
ext_foreign_toplevel_init(struct foreign_toplevel *toplevel)
ext_foreign_toplevel_init(struct ext_foreign_toplevel *ext_toplevel,
struct view *view)
{
struct ext_foreign_toplevel *ext_toplevel = &toplevel->ext_toplevel;
struct view *view = toplevel->view;
assert(view->server->foreign_toplevel_list);
ext_toplevel->view = view;
struct wlr_ext_foreign_toplevel_handle_v1_state state = {
.title = view_get_string_prop(toplevel->view, "title"),
.app_id = view_get_string_prop(toplevel->view, "app_id")
.title = view_get_string_prop(view, "title"),
.app_id = view_get_string_prop(view, "app_id")
};
ext_toplevel->handle = wlr_ext_foreign_toplevel_handle_v1_create(
view->server->foreign_toplevel_list, &state);
@ -101,7 +82,16 @@ ext_foreign_toplevel_init(struct foreign_toplevel *toplevel)
/* Compositor side state changes */
CONNECT_SIGNAL(view, &ext_toplevel->on_view, new_app_id);
CONNECT_SIGNAL(view, &ext_toplevel->on_view, new_title);
/* Internal signals */
CONNECT_SIGNAL(toplevel, &ext_toplevel->on_foreign_toplevel, toplevel_destroy);
}
void
ext_foreign_toplevel_finish(struct ext_foreign_toplevel *ext_toplevel)
{
if (!ext_toplevel->handle) {
return;
}
/* invokes handle_handle_destroy() which does more cleanup */
wlr_ext_foreign_toplevel_handle_v1_destroy(ext_toplevel->handle);
assert(!ext_toplevel->handle);
}