[digesting_libdecor] eliminate the gtk plugin constructor function, lift gtk library init out of the plugin init path

main
Allen Webster 2026-02-25 20:03:21 -08:00
parent 8467af0fd8
commit 63fc21f13a
2 changed files with 76 additions and 93 deletions

View File

@ -74,6 +74,9 @@ const struct wl_callback_listener shm_callback_listener;
const struct wl_registry_listener registry_listener; const struct wl_registry_listener registry_listener;
const struct wl_callback_listener globals_callback_listener; const struct wl_callback_listener globals_callback_listener;
struct libdecor_plugin_interface gtk_plugin_iface;
struct libdecor_plugin_interface fallback_plugin_iface;
// X(N:name,R:return,P:params) // X(N:name,R:return,P:params)
#define GL_FUNCS_XLIST(X)\ #define GL_FUNCS_XLIST(X)\
X(glDrawBuffer, void, (GLenum buf)) \ X(glDrawBuffer, void, (GLenum buf)) \
@ -87,14 +90,6 @@ GL_FUNCS_XLIST(X)
static Ctx ctx = {0}; static Ctx ctx = {0};
/* (1) Appendix A: wl_registry::global
** " The event notifies the client that a global object with the given
** name is now available "
*/
static void
init_xdg_wm_base(uint32_t id, uint32_t version);
static void static void
wlevent__wl_registry_global(void *data, struct wl_registry *registry, wlevent__wl_registry_global(void *data, struct wl_registry *registry,
uint32_t name, const char *interface, uint32_t name, const char *interface,
@ -251,7 +246,25 @@ int main(){
} }
} }
/* setup GTK context */
int gtk_init_success = 0;
enum libdecor_color_scheme color_scheme = 0;
if (ctx.wl_display != 0 && ctx.wl_compositor != 0){ if (ctx.wl_display != 0 && ctx.wl_compositor != 0){
gdk_set_allowed_backends("wayland");
gtk_disable_setlocale();
if (gtk_init_check(0, 0)){
gtk_init_success = 1;
}
{
color_scheme = libdecor_get_color_scheme();
g_object_set(gtk_settings_get_default(),
"gtk-application-prefer-dark-theme",
(color_scheme == LIBDECOR_COLOR_SCHEME_PREFER_DARK), NULL);
}
}
if (gtk_init_success){
ctx.wl_callback = wl_display_sync(ctx.wl_display); ctx.wl_callback = wl_display_sync(ctx.wl_display);
wl_callback_add_listener(ctx.wl_callback, wl_callback_add_listener(ctx.wl_callback,
&init_wl_display_callback_listener, &init_wl_display_callback_listener,
@ -259,7 +272,44 @@ int main(){
wl_list_init(&ctx.frames); wl_list_init(&ctx.frames);
ctx.plugin = libdecor_plugin_new(); {
struct libdecor_plugin_gtk *plugin_gtk;
plugin_gtk = calloc(1, sizeof *plugin_gtk);
libdecor_plugin_init(&plugin_gtk->plugin, &gtk_plugin_iface);
wl_list_init(&plugin_gtk->visible_frame_list);
wl_list_init(&plugin_gtk->seat_list);
wl_list_init(&plugin_gtk->output_list);
/* fetch cursor theme and size*/
if (!libdecor_get_cursor_settings(&plugin_gtk->cursor_theme_name,
&plugin_gtk->cursor_size)){
plugin_gtk->cursor_theme_name = 0;
plugin_gtk->cursor_size = 24;
}
plugin_gtk->color_scheme_setting = color_scheme;
plugin_gtk->wl_registry = wl_display_get_registry(ctx.wl_display);
wl_registry_add_listener(plugin_gtk->wl_registry,
&registry_listener, plugin_gtk);
plugin_gtk->globals_callback = wl_display_sync(ctx.wl_display);
wl_callback_add_listener(plugin_gtk->globals_callback,
&globals_callback_listener,
plugin_gtk);
wl_display_roundtrip(ctx.wl_display);
if (!has_required_globals(plugin_gtk)) {
fprintf(stderr, "libdecor-gtk-WARNING: Could not get required globals\n");
libdecor_plugin_gtk_destroy(&plugin_gtk->plugin);
goto plugin_failed;
}
ctx.plugin = &plugin_gtk->plugin;
}
plugin_failed:;
if (ctx.plugin == 0){ if (ctx.plugin == 0){
fprintf(stderr, "Failed to load static plugin: failed to init\n"); fprintf(stderr, "Failed to load static plugin: failed to init\n");
exit(1); exit(1);
@ -1998,7 +2048,7 @@ libdecor_plugin_fallback_frame_get_border_size(struct libdecor_plugin *plugin,
return true; return true;
} }
static struct libdecor_plugin_interface fallback_plugin_iface = { struct libdecor_plugin_interface fallback_plugin_iface = {
.destroy = libdecor_plugin_fallback_destroy, .destroy = libdecor_plugin_fallback_destroy,
.get_fd = libdecor_plugin_fallback_get_fd, .get_fd = libdecor_plugin_fallback_get_fd,
.dispatch = libdecor_plugin_fallback_dispatch, .dispatch = libdecor_plugin_fallback_dispatch,
@ -3949,7 +3999,7 @@ libdecor_plugin_gtk_frame_get_border_size(struct libdecor_plugin *plugin,
return true; return true;
} }
static struct libdecor_plugin_interface gtk_plugin_iface = { struct libdecor_plugin_interface gtk_plugin_iface = {
.destroy = libdecor_plugin_gtk_destroy, .destroy = libdecor_plugin_gtk_destroy,
.get_fd = libdecor_plugin_gtk_get_fd, .get_fd = libdecor_plugin_gtk_get_fd,
.dispatch = libdecor_plugin_gtk_dispatch, .dispatch = libdecor_plugin_gtk_dispatch,
@ -4006,7 +4056,6 @@ struct wl_shm_listener shm_listener = {
static void static void
shm_callback(void *user_data, struct wl_callback *callback, uint32_t time){ shm_callback(void *user_data, struct wl_callback *callback, uint32_t time){
struct libdecor_plugin_gtk *plugin_gtk = user_data; struct libdecor_plugin_gtk *plugin_gtk = user_data;
struct libdecor *context = plugin_gtk->context;
wl_callback_destroy(callback); wl_callback_destroy(callback);
plugin_gtk->globals_callback_shm = NULL; plugin_gtk->globals_callback_shm = NULL;
@ -4029,7 +4078,6 @@ init_wl_shm(struct libdecor_plugin_gtk *plugin_gtk,
uint32_t id, uint32_t id,
uint32_t version) uint32_t version)
{ {
struct libdecor *context = plugin_gtk->context;
struct wl_display *wl_display = ctx.wl_display; struct wl_display *wl_display = ctx.wl_display;
plugin_gtk->wl_shm = plugin_gtk->wl_shm =
@ -5069,70 +5117,6 @@ const struct wl_callback_listener globals_callback_listener = {
globals_callback globals_callback
}; };
static struct libdecor_plugin *
libdecor_plugin_new(void){
struct libdecor_plugin_gtk *plugin_gtk;
struct wl_display *wl_display;
#ifdef HAVE_GETTID
/* Only support running on the main thread. */
if (getpid () != gettid ())
return NULL;
#endif
plugin_gtk = calloc(1, sizeof *plugin_gtk);
libdecor_plugin_init(&plugin_gtk->plugin, &gtk_plugin_iface);
plugin_gtk->context = 0;
wl_list_init(&plugin_gtk->visible_frame_list);
wl_list_init(&plugin_gtk->seat_list);
wl_list_init(&plugin_gtk->output_list);
/* fetch cursor theme and size*/
if (!libdecor_get_cursor_settings(&plugin_gtk->cursor_theme_name,
&plugin_gtk->cursor_size)) {
plugin_gtk->cursor_theme_name = NULL;
plugin_gtk->cursor_size = 24;
}
plugin_gtk->color_scheme_setting = libdecor_get_color_scheme();
wl_display = ctx.wl_display;
plugin_gtk->wl_registry = wl_display_get_registry(wl_display);
wl_registry_add_listener(plugin_gtk->wl_registry,
&registry_listener,
plugin_gtk);
plugin_gtk->globals_callback = wl_display_sync(wl_display);
wl_callback_add_listener(plugin_gtk->globals_callback,
&globals_callback_listener,
plugin_gtk);
wl_display_roundtrip(wl_display);
if (!has_required_globals(plugin_gtk)) {
fprintf(stderr, "libdecor-gtk-WARNING: Could not get required globals\n");
libdecor_plugin_gtk_destroy(&plugin_gtk->plugin);
return NULL;
}
/* setup GTK context */
gdk_set_allowed_backends("wayland");
gtk_disable_setlocale();
if (!gtk_init_check(NULL, NULL)) {
fprintf(stderr, "libdecor-gtk-WARNING: Failed to initialize GTK\n");
libdecor_plugin_gtk_destroy(&plugin_gtk->plugin);
return NULL;
}
g_object_set(gtk_settings_get_default(),
"gtk-application-prefer-dark-theme",
plugin_gtk->color_scheme_setting == LIBDECOR_COLOR_SCHEME_PREFER_DARK,
NULL);
return &plugin_gtk->plugin;
}
static struct libdecor_plugin_priority priorities[] = { static struct libdecor_plugin_priority priorities[] = {
{ NULL, LIBDECOR_PLUGIN_PRIORITY_HIGH } { NULL, LIBDECOR_PLUGIN_PRIORITY_HIGH }
}; };
@ -5161,8 +5145,7 @@ get_cursor_settings_from_env(char **theme, int *size)
static DBusMessage * static DBusMessage *
get_setting_sync(DBusConnection *const connection, get_setting_sync(DBusConnection *const connection,
const char *key, const char *key, const char *value)
const char *value)
{ {
DBusError error; DBusError error;
dbus_bool_t success; dbus_bool_t success;
@ -5203,10 +5186,7 @@ get_setting_sync(DBusConnection *const connection,
} }
static bool static bool
parse_type(DBusMessage *const reply, parse_type(DBusMessage *const reply, const int type, void *value){
const int type,
void *value)
{
DBusMessageIter iter[3]; DBusMessageIter iter[3];
dbus_message_iter_init(reply, &iter[0]); dbus_message_iter_init(reply, &iter[0]);
@ -5275,8 +5255,7 @@ libdecor_get_cursor_settings(char **theme, int *size){
} }
enum libdecor_color_scheme enum libdecor_color_scheme
libdecor_get_color_scheme() libdecor_get_color_scheme(){
{
static const char name[] = "org.freedesktop.appearance"; static const char name[] = "org.freedesktop.appearance";
static const char key_color_scheme[] = "color-scheme"; static const char key_color_scheme[] = "color-scheme";
uint32_t color = 0; uint32_t color = 0;
@ -5305,17 +5284,18 @@ libdecor_get_color_scheme()
return color; return color;
} }
#else #else
bool bool
libdecor_get_cursor_settings(char **theme, int *size) libdecor_get_cursor_settings(char **theme, int *size){
{
return get_cursor_settings_from_env(theme, size); return get_cursor_settings_from_env(theme, size);
} }
uint32_t uint32_t
libdecor_get_color_scheme() libdecor_get_color_scheme(){
{
return LIBDECOR_COLOR_SCHEME_DEFAULT; return LIBDECOR_COLOR_SCHEME_DEFAULT;
} }
#endif #endif

View File

@ -438,8 +438,6 @@ struct libdecor_plugin_gtk {
struct wl_callback *globals_callback; struct wl_callback *globals_callback;
struct wl_callback *globals_callback_shm; struct wl_callback *globals_callback_shm;
struct libdecor *context;
struct wl_registry *wl_registry; struct wl_registry *wl_registry;
struct wl_subcompositor *wl_subcompositor; struct wl_subcompositor *wl_subcompositor;
struct wl_compositor *wl_compositor; struct wl_compositor *wl_compositor;
@ -606,9 +604,14 @@ enum libdecor_color_scheme libdecor_get_color_scheme();
int libdecor_os_create_anonymous_file(off_t size); int libdecor_os_create_anonymous_file(off_t size);
// #include "libdecor.c"
static void init_xdg_wm_base(uint32_t id, uint32_t version);
//#include "plugins/gtk/libdecor-gtk.c" //#include "plugins/gtk/libdecor-gtk.c"
static struct libdecor_plugin * libdecor_plugin_new(void); static bool has_required_globals(struct libdecor_plugin_gtk *plugin_gtk);
static void libdecor_plugin_gtk_destroy(struct libdecor_plugin *plugin);
// digesting_libdecor // digesting_libdecor