Incorporate XKBCommon into example

This commit is contained in:
Drew DeVault 2017-06-12 21:53:41 -04:00
parent c385a7f8af
commit 5dd96c0772
7 changed files with 81 additions and 11 deletions

19
CMake/FindXKBCommon.cmake Normal file
View File

@ -0,0 +1,19 @@
# - Find XKBCommon
# Once done, this will define
#
# XKBCOMMON_FOUND - System has XKBCommon
# XKBCOMMON_INCLUDE_DIRS - The XKBCommon include directories
# XKBCOMMON_LIBRARIES - The libraries needed to use XKBCommon
# XKBCOMMON_DEFINITIONS - Compiler switches required for using XKBCommon
find_package(PkgConfig)
pkg_check_modules(PC_XKBCOMMON QUIET xkbcommon)
find_path(XKBCOMMON_INCLUDE_DIRS NAMES xkbcommon/xkbcommon.h HINTS ${PC_XKBCOMMON_INCLUDE_DIRS})
find_library(XKBCOMMON_LIBRARIES NAMES xkbcommon HINTS ${PC_XKBCOMMON_LIBRARY_DIRS})
set(XKBCOMMON_DEFINITIONS ${PC_XKBCOMMON_CFLAGS_OTHER})
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(XKBCOMMON DEFAULT_MSG XKBCOMMON_LIBRARIES XKBCOMMON_INCLUDE_DIRS)
mark_as_advanced(XKBCOMMON_LIBRARIES XKBCOMMON_INCLUDE_DIRS)

View File

@ -48,6 +48,7 @@ find_package(GLESv2 REQUIRED)
find_package(DRM REQUIRED) find_package(DRM REQUIRED)
find_package(GBM REQUIRED) find_package(GBM REQUIRED)
find_package(LibInput REQUIRED) find_package(LibInput REQUIRED)
find_package(XKBCommon REQUIRED)
find_package(Udev REQUIRED) find_package(Udev REQUIRED)
find_package(Systemd) find_package(Systemd)

View File

@ -11,6 +11,7 @@
struct wlr_keyboard *wlr_libinput_keyboard_create( struct wlr_keyboard *wlr_libinput_keyboard_create(
struct libinput_device *device) { struct libinput_device *device) {
assert(device); assert(device);
libinput_device_led_update(device, 0);
return wlr_keyboard_create(NULL, NULL); return wlr_keyboard_create(NULL, NULL);
} }

View File

@ -1,6 +1,7 @@
include_directories( include_directories(
${DRM_INCLUDE_DIRS} ${DRM_INCLUDE_DIRS}
${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}
${XKBCOMMON_INCLUDE_DIRS}
) )
add_executable(simple add_executable(simple
@ -10,6 +11,7 @@ add_executable(simple
target_link_libraries(simple target_link_libraries(simple
wlr-backend wlr-backend
wlr-session wlr-session
${XKBCOMMON_LIBRARIES}
) )
add_executable(rotation add_executable(rotation
@ -21,4 +23,5 @@ target_link_libraries(rotation
wlr-backend wlr-backend
wlr-session wlr-session
wlr-render wlr-render
${XKBCOMMON_LIBRARIES}
) )

View File

@ -1,4 +1,5 @@
#define _POSIX_C_SOURCE 199309L #define _POSIX_C_SOURCE 199309L
#include <string.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
@ -8,12 +9,15 @@
#include <wlr/backend.h> #include <wlr/backend.h>
#include <wlr/session.h> #include <wlr/session.h>
#include <wlr/types.h> #include <wlr/types.h>
#include <xkbcommon/xkbcommon.h>
struct state { struct state {
float color[3]; float color[3];
int dec; int dec;
bool exit; bool exit;
struct timespec last_frame; struct timespec last_frame;
struct xkb_keymap *keymap;
struct xkb_state *xkb_state;
struct wl_list keyboards; struct wl_list keyboards;
struct wl_listener input_add; struct wl_listener input_add;
@ -35,7 +39,6 @@ struct keyboard_state {
struct state *state; struct state *state;
struct wlr_input_device *device; struct wlr_input_device *device;
struct wl_listener key; struct wl_listener key;
struct wl_listener mods;
struct wl_list link; struct wl_list link;
}; };
@ -98,12 +101,30 @@ void output_remove(struct wl_listener *listener, void *data) {
wl_list_remove(&ostate->frame.link); wl_list_remove(&ostate->frame.link);
} }
static void handle_keysym(struct state *state, xkb_keysym_t sym,
enum wlr_key_state key_state) {
char name[64];
int l = xkb_keysym_get_name(sym, name, sizeof(name));
if (l != -1 && l != sizeof(name)) {
fprintf(stderr, "Key event: %s %s\n", name,
key_state == WLR_KEY_PRESSED ? "pressed" : "released");
}
if (sym == XKB_KEY_Escape) {
state->exit = true;
}
}
static void keyboard_key(struct wl_listener *listener, void *data) { static void keyboard_key(struct wl_listener *listener, void *data) {
struct wlr_keyboard_key *event = data; struct wlr_keyboard_key *event = data;
struct keyboard_state *kbstate = wl_container_of(listener, kbstate, key); struct keyboard_state *kbstate = wl_container_of(listener, kbstate, key);
fprintf(stderr, "Key event: %u %s\n", event->keycode, uint32_t keycode = event->keycode + 8;
event->state == WLR_KEY_PRESSED ? "pressed" : "released"); const xkb_keysym_t *syms;
kbstate->state->exit = true; int nsyms = xkb_state_key_get_syms(kbstate->state->xkb_state, keycode, &syms);
for (int i = 0; i < nsyms; ++i) {
handle_keysym(kbstate->state, syms[i], event->state);
}
xkb_state_update_key(kbstate->state->xkb_state, keycode,
event->state == WLR_KEY_PRESSED ? XKB_KEY_DOWN : XKB_KEY_UP);
} }
void input_add(struct wl_listener *listener, void *data) { void input_add(struct wl_listener *listener, void *data) {
@ -116,7 +137,6 @@ void input_add(struct wl_listener *listener, void *data) {
kbstate->device = device; kbstate->device = device;
kbstate->state = state; kbstate->state = state;
wl_list_init(&kbstate->key.link); wl_list_init(&kbstate->key.link);
wl_list_init(&kbstate->mods.link);
kbstate->key.notify = keyboard_key; kbstate->key.notify = keyboard_key;
wl_signal_add(&device->keyboard->events.key, &kbstate->key); wl_signal_add(&device->keyboard->events.key, &kbstate->key);
wl_list_insert(&state->keyboards, &kbstate->link); wl_list_insert(&state->keyboards, &kbstate->link);
@ -140,7 +160,12 @@ void input_remove(struct wl_listener *listener, void *data) {
} }
wl_list_remove(&kbstate->link); wl_list_remove(&kbstate->link);
wl_list_remove(&kbstate->key.link); wl_list_remove(&kbstate->key.link);
//wl_list_remove(&kbstate->mods.link); }
static int timer_done(void *data) {
struct state *state = data;
state->exit = true;
return 1;
} }
int main() { int main() {
@ -151,9 +176,30 @@ int main() {
.input_add = { .notify = input_add }, .input_add = { .notify = input_add },
.input_remove = { .notify = input_remove }, .input_remove = { .notify = input_remove },
.output_add = { .notify = output_add }, .output_add = { .notify = output_add },
.output_remove = { .notify = output_remove } .output_remove = { .notify = output_remove },
}; };
struct xkb_rule_names rules;
memset(&rules, 0, sizeof(rules));
rules.rules = getenv("XKB_DEFAULT_RULES");
rules.model = getenv("XKB_DEFAULT_MODEL");
rules.layout = getenv("XKB_DEFAULT_LAYOUT");
rules.variant = getenv("XKB_DEFAULT_VARIANT");
rules.options = getenv("XKB_DEFAULT_OPTIONS");
struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
if (!context) {
fprintf(stderr, "Failed to create XKB context\n");
return 1;
}
state.keymap =
xkb_map_new_from_names(context, &rules, XKB_KEYMAP_COMPILE_NO_FLAGS);
if (!state.keymap) {
fprintf(stderr, "Failed to create XKB keymap\n");
return 1;
}
xkb_context_unref(context);
state.xkb_state = xkb_state_new(state.keymap);
wl_list_init(&state.keyboards); wl_list_init(&state.keyboards);
wl_list_init(&state.input_add.link); wl_list_init(&state.input_add.link);
wl_list_init(&state.input_remove.link); wl_list_init(&state.input_remove.link);
@ -179,6 +225,10 @@ int main() {
if (!wlr || !wlr_backend_init(wlr)) { if (!wlr || !wlr_backend_init(wlr)) {
return 1; return 1;
} }
struct wl_event_source *timer = wl_event_loop_add_timer(event_loop,
timer_done, &state);
wl_event_source_timer_update(timer, 30000);
while (!state.exit) { while (!state.exit) {
wl_event_loop_dispatch(event_loop, 0); wl_event_loop_dispatch(event_loop, 0);

View File

@ -50,8 +50,6 @@ void wlr_output_destroy(struct wlr_output *output);
void wlr_output_effective_resolution(struct wlr_output *output, void wlr_output_effective_resolution(struct wlr_output *output,
int *width, int *height); int *width, int *height);
// TODO: keymaps
struct wlr_keyboard_state; struct wlr_keyboard_state;
struct wlr_keyboard_impl; struct wlr_keyboard_impl;
@ -61,7 +59,6 @@ struct wlr_keyboard {
struct { struct {
struct wl_signal key; struct wl_signal key;
struct wl_signal mods;
} events; } events;
}; };

View File

@ -11,7 +11,6 @@ struct wlr_keyboard *wlr_keyboard_create(struct wlr_keyboard_impl *impl,
kb->impl = impl; kb->impl = impl;
kb->state = state; kb->state = state;
wl_signal_init(&kb->events.key); wl_signal_init(&kb->events.key);
wl_signal_init(&kb->events.mods);
return kb; return kb;
} }