Skip to content

Commit

Permalink
x11: Make XKB support optional at build time
Browse files Browse the repository at this point in the history
Allow disabling the XKB support in the x11 platform plug-in at build
time. For this, introduce a new "x11_keyboard" Meson build option that
can be set to use xkb, xcb-keysyms, both, or none. Preprocessor guards
are introduced where needed to skip the XKB code.
  • Loading branch information
aperezdc committed Nov 30, 2023
1 parent 896679a commit 6c79372
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
9 changes: 9 additions & 0 deletions meson_options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ option(
description: 'Build content-protection support in the Wayland platform plug-in'
)

# X11 platform-specifig features
option(
'x11_keyboard',
type: 'array',
value: ['xkb'],
choices: ['xkb', 'xcb-keysyms'],
description: 'Keyboard handler (xkb recommended)',
)

# Additional cog/cogctl options
option(
'cog_appid',
Expand Down
26 changes: 19 additions & 7 deletions platform/x11/cog-platform-x11.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
# include <xcb/xcb_keysyms.h>
#endif /* COG_X11_USE_XCB_KEYSYMS */

#include <xkbcommon/xkbcommon-x11.h>
#ifdef COG_X11_USE_XKB
# include <xkbcommon/xkbcommon-x11.h>
#endif

#if COG_HAVE_LIBPORTAL
# include "../common/cog-file-chooser.h"
Expand Down Expand Up @@ -87,6 +89,7 @@ struct CogX11Display {
GSource *source;
} xcb;

#ifdef COG_X11_USE_XKB
struct {
int32_t device_id;
struct xkb_context *context;
Expand All @@ -98,6 +101,7 @@ struct CogX11Display {
xkb_mod_mask_t num_lock;
xkb_mod_mask_t caps_lock;
} xkb;
#endif /* COG_X11_USE_XKB */

#ifdef COG_X11_USE_XCB_KEYSYMS
xcb_key_symbols_t *xcb_keysyms;
Expand Down Expand Up @@ -194,6 +198,7 @@ xcb_paint_image (struct wpe_fdo_egl_exported_image *image)
eglSwapBuffers(s_display->egl.display, s_window->egl.surface);
}

#ifdef COG_X11_USE_XKB
static uint32_t
xcb_update_xkb_modifiers(uint32_t event_state)
{
Expand All @@ -220,6 +225,7 @@ xcb_update_xkb_modifiers(uint32_t event_state)
xkb_state_update_mask(s_display->xkb.state, depressed_mods, 0, locked_mods, 0, 0, 0);
return wpe_modifiers;
}
#endif /* COG_X11_USE_XKB */

#if COG_X11_USE_XCB_KEYSYMS
/*
Expand Down Expand Up @@ -271,11 +277,13 @@ key_event_fill(struct wpe_input_keyboard_event *wpe_event, xcb_key_press_event_t
wpe_event->time = event->time;
wpe_event->hardware_key_code = event->detail;

#if COG_X11_USE_XKB
if (s_display->xkb.device_id >= 0 && s_display->xkb.state) {
wpe_event->modifiers = xcb_update_xkb_modifiers(event->state);
wpe_event->key_code = xkb_state_key_get_one_sym(s_display->xkb.state, event->detail);
return;
}
#endif /* COG_X11_USE_XKB */

#if COG_X11_USE_XCB_KEYSYMS
if (s_display->xcb_keysyms) {
Expand Down Expand Up @@ -662,6 +670,7 @@ clear_xcb (void)
XCloseDisplay (s_display->display);
}

#ifdef COG_X11_USE_XKB
static gboolean
init_xkb (void)
{
Expand Down Expand Up @@ -690,16 +699,20 @@ init_xkb (void)
s_display->xkb.device_id = -1;
return FALSE;
}
#endif /* COG_X11_USE_XKB */

static gboolean
init_keyboard(GError **error)
{
g_autoptr(GString) tried_impl = NULL;

#ifdef COG_X11_USE_XKB
tried_impl = g_string_new("XKB");
if (init_xkb()) {
g_debug("%s: Using XKB", G_STRFUNC);
return TRUE;
}
#endif /* COG_X11_USE_XKB */

#ifdef COG_X11_USE_XCB_KEYSYMS
tried_impl = tried_impl ? g_string_append(tried_impl, ", XCB-Keysyms") : g_string_new("XCB-Keysyms");
Expand All @@ -721,12 +734,11 @@ init_keyboard(GError **error)
static void
clear_keyboard(void)
{
if (s_display->xkb.state)
xkb_state_unref (s_display->xkb.state);
if (s_display->xkb.keymap)
xkb_keymap_unref (s_display->xkb.keymap);
if (s_display->xkb.context)
xkb_context_unref (s_display->xkb.context);
#ifdef COG_X11_USE_XKB
g_clear_pointer(&s_display->xkb.state, xkb_state_unref);
g_clear_pointer(&s_display->xkb.keymap, xkb_keymap_unref);
g_clear_pointer(&s_display->xkb.context, xkb_context_unref);
#endif /* COG_X11_USE_XKB */

#ifdef COG_X11_USE_XCB_KEYSYMS
g_clear_pointer(&s_display->xcb_keysyms, xcb_key_symbols_free);
Expand Down
17 changes: 13 additions & 4 deletions platform/x11/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,21 @@ x11_platform_dependencies = [
dependency('xcb-cursor'),
]

xcb_keysyms_dep = dependency('xcb-keysyms', required: false)
if xcb_keysyms_dep.found()
x11_platform_dependencies += [xcb_keysyms_dep]
x11_platform_c_args += ['-DCOG_X11_USE_XCB_KEYSYMS']
x11_platform_keyboard = get_option('x11_keyboard')
if x11_platform_keyboard.length() == 0
warning('No X11 keyboard support chosen, keyboard input will NOT work')
endif

x11_platform_keyboard_dep_names = {
'xkb': 'xkbcommon-x11',
'xcb-keysyms': 'xcb-keysyms',
}

foreach item : x11_platform_keyboard
x11_platform_dependencies += [dependency(x11_platform_keyboard_dep_names[item])]
x11_platform_c_args += ['-DCOG_X11_USE_@0@=1'.format(item.underscorify().to_upper())]
endforeach

x11_platform_plugin = shared_module('cogplatform-x11',
'cog-platform-x11.c',
x11_platform_sources,
Expand Down

0 comments on commit 6c79372

Please sign in to comment.