Add sway_view_impl

master
emersion 7 years ago
parent b2c2ee693b
commit 1d68f9ecca
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48

@ -66,10 +66,23 @@ enum sway_view_prop {
VIEW_PROP_INSTANCE, VIEW_PROP_INSTANCE,
}; };
struct sway_view_impl {
const char *(*get_prop)(struct sway_view *view,
enum sway_view_prop prop);
void (*set_size)(struct sway_view *view,
int width, int height);
void (*set_position)(struct sway_view *view,
double ox, double oy);
void (*set_activated)(struct sway_view *view, bool activated);
void (*close)(struct sway_view *view);
};
struct sway_view { struct sway_view {
enum sway_view_type type; enum sway_view_type type;
struct sway_container *swayc; const struct sway_view_impl *impl;
struct wlr_surface *surface;
struct sway_container *swayc; // NULL for unmanaged views
struct wlr_surface *surface; // NULL for unmapped views
int width, height; int width, height;
union { union {
@ -84,22 +97,12 @@ struct sway_view {
struct sway_wl_shell_surface *sway_wl_shell_surface; struct sway_wl_shell_surface *sway_wl_shell_surface;
}; };
struct {
const char *(*get_prop)(struct sway_view *view,
enum sway_view_prop prop);
void (*set_size)(struct sway_view *view,
int width, int height);
void (*set_position)(struct sway_view *view,
double ox, double oy);
void (*set_activated)(struct sway_view *view, bool activated);
void (*close)(struct sway_view *view);
} iface;
// only used for unmanaged views (shell specific) // only used for unmanaged views (shell specific)
struct wl_list unmanaged_view_link; // sway_root::unmanaged_views struct wl_list unmanaged_view_link; // sway_root::unmanaged_views
}; };
struct sway_view *view_create(enum sway_view_type type); struct sway_view *view_create(enum sway_view_type type,
const struct sway_view_impl *impl);
void view_destroy(struct sway_view *view); void view_destroy(struct sway_view *view);

@ -47,10 +47,6 @@ static void set_position(struct sway_view *view, double ox, double oy) {
view->swayc->y = oy; view->swayc->y = oy;
} }
static void set_activated(struct sway_view *view, bool activated) {
// no way to activate wl_shell
}
static void close(struct sway_view *view) { static void close(struct sway_view *view) {
if (!assert_wl_shell(view)) { if (!assert_wl_shell(view)) {
return; return;
@ -59,6 +55,13 @@ static void close(struct sway_view *view) {
wl_client_destroy(view->wlr_wl_shell_surface->client); wl_client_destroy(view->wlr_wl_shell_surface->client);
} }
static const struct sway_view_impl view_impl = {
.get_prop = get_prop,
.set_size = set_size,
.set_position = set_position,
.close = close,
};
static void handle_commit(struct wl_listener *listener, void *data) { static void handle_commit(struct wl_listener *listener, void *data) {
struct sway_wl_shell_surface *sway_surface = struct sway_wl_shell_surface *sway_surface =
wl_container_of(listener, sway_surface, commit); wl_container_of(listener, sway_surface, commit);
@ -101,15 +104,10 @@ void handle_wl_shell_surface(struct wl_listener *listener, void *data) {
return; return;
} }
struct sway_view *view = view_create(SWAY_WL_SHELL_VIEW); struct sway_view *view = view_create(SWAY_WL_SHELL_VIEW, &view_impl);
if (!sway_assert(view, "Failed to allocate view")) { if (!sway_assert(view, "Failed to allocate view")) {
return; return;
} }
view->iface.get_prop = get_prop;
view->iface.set_size = set_size;
view->iface.set_position = set_position;
view->iface.set_activated = set_activated;
view->iface.close = close;
view->wlr_wl_shell_surface = shell_surface; view->wlr_wl_shell_surface = shell_surface;
view->sway_wl_shell_surface = sway_surface; view->sway_wl_shell_surface = sway_surface;
sway_surface->view = view; sway_surface->view = view;

@ -67,6 +67,14 @@ static void close(struct sway_view *view) {
} }
} }
static const struct sway_view_impl view_impl = {
.get_prop = get_prop,
.set_size = set_size,
.set_position = set_position,
.set_activated = set_activated,
.close = close,
};
static void handle_commit(struct wl_listener *listener, void *data) { static void handle_commit(struct wl_listener *listener, void *data) {
struct sway_xdg_surface_v6 *sway_surface = struct sway_xdg_surface_v6 *sway_surface =
wl_container_of(listener, sway_surface, commit); wl_container_of(listener, sway_surface, commit);
@ -124,15 +132,10 @@ void handle_xdg_shell_v6_surface(struct wl_listener *listener, void *data) {
return; return;
} }
struct sway_view *view = view_create(SWAY_XDG_SHELL_V6_VIEW); struct sway_view *view = view_create(SWAY_XDG_SHELL_V6_VIEW, &view_impl);
if (!sway_assert(view, "Failed to allocate view")) { if (!sway_assert(view, "Failed to allocate view")) {
return; return;
} }
view->iface.get_prop = get_prop;
view->iface.set_size = set_size;
view->iface.set_position = set_position;
view->iface.set_activated = set_activated;
view->iface.close = close;
view->wlr_xdg_surface_v6 = xdg_surface; view->wlr_xdg_surface_v6 = xdg_surface;
view->sway_xdg_surface_v6 = sway_surface; view->sway_xdg_surface_v6 = sway_surface;
sway_surface->view = view; sway_surface->view = view;

@ -81,13 +81,21 @@ static void set_activated(struct sway_view *view, bool activated) {
wlr_xwayland_surface_activate(surface, activated); wlr_xwayland_surface_activate(surface, activated);
} }
static void close_view(struct sway_view *view) { static void _close(struct sway_view *view) {
if (!assert_xwayland(view)) { if (!assert_xwayland(view)) {
return; return;
} }
wlr_xwayland_surface_close(view->wlr_xwayland_surface); wlr_xwayland_surface_close(view->wlr_xwayland_surface);
} }
static const struct sway_view_impl view_impl = {
.get_prop = get_prop,
.set_size = set_size,
.set_position = set_position,
.set_activated = set_activated,
.close = _close,
};
static void handle_commit(struct wl_listener *listener, void *data) { static void handle_commit(struct wl_listener *listener, void *data) {
struct sway_xwayland_surface *sway_surface = struct sway_xwayland_surface *sway_surface =
wl_container_of(listener, sway_surface, commit); wl_container_of(listener, sway_surface, commit);
@ -159,15 +167,10 @@ void handle_xwayland_surface(struct wl_listener *listener, void *data) {
return; return;
} }
struct sway_view *view = view_create(SWAY_XWAYLAND_VIEW); struct sway_view *view = view_create(SWAY_XWAYLAND_VIEW, &view_impl);
if (!sway_assert(view, "Failed to allocate view")) { if (!sway_assert(view, "Failed to allocate view")) {
return; return;
} }
view->iface.get_prop = get_prop;
view->iface.set_size = set_size;
view->iface.set_position = set_position;
view->iface.set_activated = set_activated;
view->iface.close = close_view;
view->wlr_xwayland_surface = xsurface; view->wlr_xwayland_surface = xsurface;
view->sway_xwayland_surface = sway_surface; view->sway_xwayland_surface = sway_surface;
sway_surface->view = view; sway_surface->view = view;

@ -7,12 +7,14 @@
#include "sway/tree/layout.h" #include "sway/tree/layout.h"
#include "sway/tree/view.h" #include "sway/tree/view.h"
struct sway_view *view_create(enum sway_view_type type) { struct sway_view *view_create(enum sway_view_type type,
const struct sway_view_impl *impl) {
struct sway_view *view = calloc(1, sizeof(struct sway_view)); struct sway_view *view = calloc(1, sizeof(struct sway_view));
if (view == NULL) { if (view == NULL) {
return NULL; return NULL;
} }
view->type = type; view->type = type;
view->impl = impl;
wl_list_init(&view->unmanaged_view_link); wl_list_init(&view->unmanaged_view_link);
return view; return view;
} }
@ -33,29 +35,29 @@ void view_destroy(struct sway_view *view) {
} }
const char *view_get_title(struct sway_view *view) { const char *view_get_title(struct sway_view *view) {
if (view->iface.get_prop) { if (view->impl->get_prop) {
return view->iface.get_prop(view, VIEW_PROP_TITLE); return view->impl->get_prop(view, VIEW_PROP_TITLE);
} }
return NULL; return NULL;
} }
const char *view_get_app_id(struct sway_view *view) { const char *view_get_app_id(struct sway_view *view) {
if (view->iface.get_prop) { if (view->impl->get_prop) {
return view->iface.get_prop(view, VIEW_PROP_APP_ID); return view->impl->get_prop(view, VIEW_PROP_APP_ID);
} }
return NULL; return NULL;
} }
const char *view_get_class(struct sway_view *view) { const char *view_get_class(struct sway_view *view) {
if (view->iface.get_prop) { if (view->impl->get_prop) {
return view->iface.get_prop(view, VIEW_PROP_CLASS); return view->impl->get_prop(view, VIEW_PROP_CLASS);
} }
return NULL; return NULL;
} }
const char *view_get_instance(struct sway_view *view) { const char *view_get_instance(struct sway_view *view) {
if (view->iface.get_prop) { if (view->impl->get_prop) {
return view->iface.get_prop(view, VIEW_PROP_INSTANCE); return view->impl->get_prop(view, VIEW_PROP_INSTANCE);
} }
return NULL; return NULL;
} }
@ -86,41 +88,41 @@ static void view_update_outputs(struct sway_view *view,
} }
void view_set_size(struct sway_view *view, int width, int height) { void view_set_size(struct sway_view *view, int width, int height) {
if (view->iface.set_size) { if (view->impl->set_size) {
struct wlr_box box = { struct wlr_box box = {
.x = view->swayc->x, .x = view->swayc->x,
.y = view->swayc->y, .y = view->swayc->y,
.width = view->width, .width = view->width,
.height = view->height, .height = view->height,
}; };
view->iface.set_size(view, width, height); view->impl->set_size(view, width, height);
view_update_outputs(view, &box); view_update_outputs(view, &box);
} }
} }
// TODO make view coordinates in layout coordinates // TODO make view coordinates in layout coordinates
void view_set_position(struct sway_view *view, double ox, double oy) { void view_set_position(struct sway_view *view, double ox, double oy) {
if (view->iface.set_position) { if (view->impl->set_position) {
struct wlr_box box = { struct wlr_box box = {
.x = view->swayc->x, .x = view->swayc->x,
.y = view->swayc->y, .y = view->swayc->y,
.width = view->width, .width = view->width,
.height = view->height, .height = view->height,
}; };
view->iface.set_position(view, ox, oy); view->impl->set_position(view, ox, oy);
view_update_outputs(view, &box); view_update_outputs(view, &box);
} }
} }
void view_set_activated(struct sway_view *view, bool activated) { void view_set_activated(struct sway_view *view, bool activated) {
if (view->iface.set_activated) { if (view->impl->set_activated) {
view->iface.set_activated(view, activated); view->impl->set_activated(view, activated);
} }
} }
void view_close(struct sway_view *view) { void view_close(struct sway_view *view) {
if (view->iface.close) { if (view->impl->close) {
view->iface.close(view); view->impl->close(view);
} }
} }

Loading…
Cancel
Save