Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for global menus #2576

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions metadata/kde-appmenu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<wayfire>
<plugin name="kde-appmenu">
<_short>KDE Appmenu support</_short>
<_long>An implementation of the KDE Appmenu protocol for global menus.</_long>
<category>Utility</category>
</plugin>
</wayfire>
1 change: 1 addition & 0 deletions metadata/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ install_data('input-method-v1.xml', install_dir: conf_data.get('PLUGIN_XML_DIR')
install_data('invert.xml', install_dir: conf_data.get('PLUGIN_XML_DIR'))
install_data('ipc.xml', install_dir: conf_data.get('PLUGIN_XML_DIR'))
install_data('ipc-rules.xml', install_dir: conf_data.get('PLUGIN_XML_DIR'))
install_data('kde-appmenu.xml', install_dir: conf_data.get('PLUGIN_XML_DIR'))
install_data('move.xml', install_dir: conf_data.get('PLUGIN_XML_DIR'))
install_data('oswitch.xml', install_dir: conf_data.get('PLUGIN_XML_DIR'))
install_data('output.xml', install_dir: conf_data.get('PLUGIN_XML_DIR'))
Expand Down
189 changes: 189 additions & 0 deletions plugins/protocols/foreign-toplevel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,29 @@
#include <wayfire/toplevel-view.hpp>
#include <wayfire/window-manager.hpp>
#include "gtk-shell.hpp"
#include "kde-appmenu.hpp"
#include "config.h"

class wayfire_foreign_toplevel;
using foreign_toplevel_map_type = std::map<wayfire_toplevel_view, std::unique_ptr<wayfire_foreign_toplevel>>;

class toplevel_gtk_shell1_dbus_properties_t : public wf::custom_data_t
{
public:
std::optional<std::string> app_menu_path;
std::optional<std::string> menubar_path;
std::optional<std::string> window_object_path;
std::optional<std::string> application_object_path;
std::optional<std::string> unique_bus_name;
};

class toplevel_kde_appmenu_path_t : public wf::custom_data_t
{
public:
std::optional<std::string> service_name;
std::optional<std::string> object_path;
};

class wayfire_foreign_toplevel
{
wayfire_toplevel_view view;
Expand Down Expand Up @@ -62,6 +80,59 @@ class wayfire_foreign_toplevel
wlr_foreign_toplevel_handle_v1_destroy(handle);
}

void toplevel_send_gtk_shell1_dbus_properties(
const char *app_menu_path,
const char *menubar_path,
const char *window_object_path,
const char *application_object_path,
const char *unique_bus_name)
{
// !! TODO: app_menu_path (not sure which interface it corresponds to)

if (menubar_path && unique_bus_name)
{
wlr_foreign_toplevel_handle_v1_add_surface_dbus_annotation(
handle, "org.gtk.Menus", unique_bus_name, menubar_path);
} else
{
wlr_foreign_toplevel_handle_v1_remove_surface_dbus_annotation(
handle, "org.gtk.Menus");
}

if (window_object_path && unique_bus_name)
{
wlr_foreign_toplevel_handle_v1_add_surface_dbus_annotation(
handle, "org.gtk.Actions", unique_bus_name, window_object_path);
} else
{
wlr_foreign_toplevel_handle_v1_remove_surface_dbus_annotation(
handle, "org.gtk.Actions");
}

if (application_object_path && unique_bus_name)
{
wlr_foreign_toplevel_handle_v1_add_client_dbus_annotation(
handle, "org.gtk.Actions", unique_bus_name, application_object_path);
} else
{
wlr_foreign_toplevel_handle_v1_remove_client_dbus_annotation(
handle, "org.gtk.Actions");
}
}

void toplevel_send_kde_appmenu_path(const char *service_name, const char *object_path)
{
if (service_name && object_path)
{
wlr_foreign_toplevel_handle_v1_add_surface_dbus_annotation(
handle, "com.canonical.dbusmenu", service_name, object_path);
} else
{
wlr_foreign_toplevel_handle_v1_remove_surface_dbus_annotation(
handle, "com.canonical.dbusmenu");
}
}

private:
void toplevel_send_title()
{
Expand Down Expand Up @@ -257,6 +328,8 @@ class wayfire_foreign_toplevel_protocol_impl : public wf::plugin_interface_t
toplevel_manager = wlr_foreign_toplevel_manager_v1_create(wf::get_core().display);
wf::get_core().connect(&on_view_mapped);
wf::get_core().connect(&on_view_unmapped);
wf::get_core().connect(&on_view_dbus_properties_changed);
wf::get_core().connect(&on_view_kde_appmenu_changed);
}

void fini() override
Expand All @@ -275,6 +348,23 @@ class wayfire_foreign_toplevel_protocol_impl : public wf::plugin_interface_t
auto handle = wlr_foreign_toplevel_handle_v1_create(toplevel_manager);
handle_for_view[toplevel] =
std::make_unique<wayfire_foreign_toplevel>(toplevel, handle, &handle_for_view);

if (auto props = toplevel->get_data<toplevel_gtk_shell1_dbus_properties_t>())
{
handle_for_view[toplevel]->toplevel_send_gtk_shell1_dbus_properties(
props->app_menu_path ? props->app_menu_path->c_str() : nullptr,
props->menubar_path ? props->menubar_path->c_str() : nullptr,
props->window_object_path ? props->window_object_path->c_str() : nullptr,
props->application_object_path ? props->application_object_path->c_str() : nullptr,
props->unique_bus_name ? props->unique_bus_name->c_str() : nullptr);
}

if (auto props = toplevel->get_data<toplevel_kde_appmenu_path_t>())
{
handle_for_view[toplevel]->toplevel_send_kde_appmenu_path(
props->service_name ? props->service_name->c_str() : nullptr,
props->object_path ? props->object_path->c_str() : nullptr);
}
}
};

Expand All @@ -283,6 +373,105 @@ class wayfire_foreign_toplevel_protocol_impl : public wf::plugin_interface_t
handle_for_view.erase(toplevel_cast(ev->view));
};

wf::signal::connection_t<gtk_shell_dbus_properties_signal> on_view_dbus_properties_changed =
[=] (gtk_shell_dbus_properties_signal *ev)
{
if (auto toplevel = wf::toplevel_cast(ev->view))
{
auto it = handle_for_view.find(toplevel);
if (it != handle_for_view.end())
{
it->second->toplevel_send_gtk_shell1_dbus_properties(
ev->app_menu_path,
ev->menubar_path,
ev->window_object_path,
ev->application_object_path,
ev->unique_bus_name);
}

/* Store the values with the view. This is necessary to cover the cases when either:
* (1) the view has not been mapped yet; or (2) the view is later unmapped and remapped
*/
auto props = toplevel->get_data_safe<toplevel_gtk_shell1_dbus_properties_t>();
if (ev->app_menu_path)
{
props->app_menu_path = ev->app_menu_path;
} else
{
props->app_menu_path.reset();
}

if (ev->application_object_path)
{
props->application_object_path =
ev->application_object_path;
} else
{
props->application_object_path.reset();
}

if (ev->menubar_path)
{
props->menubar_path = ev->menubar_path;
} else
{
props->menubar_path.reset();
}

if (ev->unique_bus_name)
{
props->unique_bus_name = ev->unique_bus_name;
} else
{
props->unique_bus_name.reset();
}

if (ev->window_object_path)
{
props->window_object_path =
ev->window_object_path;
} else
{
props->window_object_path.reset();
}
}
};

wf::signal::connection_t<kde_appmenu_dbus_address_signal> on_view_kde_appmenu_changed =
[=] (kde_appmenu_dbus_address_signal *ev)
{
if (auto toplevel = wf::toplevel_cast(ev->view))
{
auto it = handle_for_view.find(toplevel);
if (it != handle_for_view.end())
{
it->second->toplevel_send_kde_appmenu_path(
ev->service_name,
ev->object_path);
}

/* Store the values with the view. This is necessary to cover the cases when either:
* (1) the view has not been mapped yet; or (2) the view is later unmapped and remapped
*/
auto props = toplevel->get_data_safe<toplevel_kde_appmenu_path_t>();
if (ev->service_name)
{
props->service_name = ev->service_name;
} else
{
props->service_name.reset();
}

if (ev->object_path)
{
props->object_path = ev->object_path;
} else
{
props->object_path.reset();
}
}
};

wlr_foreign_toplevel_manager_v1 *toplevel_manager;
std::map<wayfire_toplevel_view, std::unique_ptr<wayfire_foreign_toplevel>> handle_for_view;
};
Expand Down
18 changes: 18 additions & 0 deletions plugins/protocols/gtk-shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ static void handle_gtk_surface_set_dbus_properties(wl_client *client,
{
wf::get_core().get_data_safe<wf_gtk_shell>()->surface_app_id[surface->wl_surface] = application_id;
}

wayfire_view view = wf::wl_surface_to_wayfire_view(surface->wl_surface);
if (!view)
{
LOGE("Could not get view");
return;
} else
{
gtk_shell_dbus_properties_signal ev;
ev.view = view;
ev.app_menu_path = app_menu_path;
ev.menubar_path = menubar_path;
ev.window_object_path = window_object_path;
ev.application_object_path = application_object_path;
ev.unique_bus_name = unique_bus_name;

wf::get_core().emit(&ev);
}
}

/**
Expand Down
14 changes: 14 additions & 0 deletions plugins/protocols/gtk-shell.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,17 @@ struct gtk_shell_app_id_query_signal
// Set by the gtk-shell plugin in response to the signal
std::string app_id;
};

/**
* A signal that is emitted when the DBus properties of a gtk-shell surface change.
*/
struct gtk_shell_dbus_properties_signal
{
wayfire_view view;

const char *app_menu_path;
const char *menubar_path;
const char *window_object_path;
const char *application_object_path;
const char *unique_bus_name;
};
106 changes: 106 additions & 0 deletions plugins/protocols/kde-appmenu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#include "kde-appmenu-protocol.h"
#include "wayfire/core.hpp"
#include "wayfire/object.hpp"
#include <wayfire/plugin.hpp>
#include <wayfire/view.hpp>
#include <wayfire/toplevel-view.hpp>
#include "kde-appmenu.hpp"

#define KDE_APPMENU_VERSION 2

struct wf_kde_appmenu_surface
{
wl_resource *resource;
wl_resource *wl_surface;
};

static void handle_kde_appmenu_set_address(wl_client *client,
wl_resource *resource, const char *service_name,
const char *object_path)
{
auto surface = static_cast<wf_kde_appmenu_surface*>(wl_resource_get_user_data(resource));
wayfire_view view = wf::wl_surface_to_wayfire_view(surface->wl_surface);
if (!view)
{
LOGE("Could not get view");
return;
} else
{
kde_appmenu_dbus_address_signal ev;
ev.view = view;
ev.service_name = service_name;
ev.object_path = object_path;

wf::get_core().emit(&ev);
}
}

static void handle_kde_appmenu_release(wl_client*, wl_resource*)
{
/* no-op */
}

static void handle_kde_appmenu_destroy(wl_resource *resource)
{
auto surface = static_cast<wf_kde_appmenu_surface*>(wl_resource_get_user_data(resource));
delete surface;
}

const struct org_kde_kwin_appmenu_interface kde_appmenu_impl = {
.set_address = handle_kde_appmenu_set_address,
.release = handle_kde_appmenu_release
};


static void handle_kde_appmenu_manager_create(wl_client *client,
wl_resource *resource, uint32_t id, wl_resource *surface)
{
wf_kde_appmenu_surface *kde_appmenu_surface = new wf_kde_appmenu_surface;
kde_appmenu_surface->resource = wl_resource_create(client,
&org_kde_kwin_appmenu_interface, wl_resource_get_version(resource), id);
kde_appmenu_surface->wl_surface = surface;
wl_resource_set_implementation(kde_appmenu_surface->resource, &kde_appmenu_impl,
kde_appmenu_surface, handle_kde_appmenu_destroy);
}

static void handle_kde_appmenu_manager_release(wl_client*, wl_resource*)
{
/* no-op */
}

static void handle_kde_appmenu_manager_destroy(wl_resource*)
{
/* no-op */
}

static const struct org_kde_kwin_appmenu_manager_interface kde_appmenu_manager_impl = {
.create = handle_kde_appmenu_manager_create,
.release = handle_kde_appmenu_manager_release
};


static void bind_kde_appmenu(wl_client *client, void *data, uint32_t version, uint32_t id)
{
auto resource = wl_resource_create(client, &org_kde_kwin_appmenu_manager_interface,
KDE_APPMENU_VERSION, id);
wl_resource_set_implementation(resource, &kde_appmenu_manager_impl,
data, handle_kde_appmenu_manager_destroy);
}

class wayfire_kde_appmenu_impl : public wf::plugin_interface_t
{
public:
void init() override
{
auto display = wf::get_core().display;
wl_global_create(display, &org_kde_kwin_appmenu_manager_interface,
KDE_APPMENU_VERSION, NULL, bind_kde_appmenu);
}

bool is_unloadable() override
{
return false;
}
};

DECLARE_WAYFIRE_PLUGIN(wayfire_kde_appmenu_impl);
Loading
Loading