mirror of
				https://gitlab.freedesktop.org/wlroots/wlroots.git
				synced 2025-11-03 09:01:40 -05:00 
			
		
		
		
	Fix keyboard handling in examples
This commit is contained in:
		
							parent
							
								
									6241ac41d5
								
							
						
					
					
						commit
						aa8a4f12b7
					
				
					 3 changed files with 8 additions and 42 deletions
				
			
		| 
						 | 
					@ -1,4 +1,5 @@
 | 
				
			||||||
#define _POSIX_C_SOURCE 200112L
 | 
					#define _POSIX_C_SOURCE 200112L
 | 
				
			||||||
 | 
					#include <assert.h>
 | 
				
			||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
#include <stdlib.h>
 | 
					#include <stdlib.h>
 | 
				
			||||||
#include <time.h>
 | 
					#include <time.h>
 | 
				
			||||||
| 
						 | 
					@ -18,27 +19,15 @@
 | 
				
			||||||
#include <wlr/util/log.h>
 | 
					#include <wlr/util/log.h>
 | 
				
			||||||
#include "shared.h"
 | 
					#include "shared.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
static void keyboard_led_update(struct keyboard_state *kbstate) {
 | 
					 | 
				
			||||||
	uint32_t leds = 0;
 | 
					 | 
				
			||||||
	for (uint32_t i = 0; i < WLR_LED_LAST; ++i) {
 | 
					 | 
				
			||||||
		if (xkb_state_led_index_is_active(kbstate->xkb_state, kbstate->leds[i])) {
 | 
					 | 
				
			||||||
			leds |= (1 << i);
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	wlr_keyboard_led_update(kbstate->device->keyboard, leds);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
static void keyboard_key_notify(struct wl_listener *listener, void *data) {
 | 
					static void keyboard_key_notify(struct wl_listener *listener, void *data) {
 | 
				
			||||||
	struct wlr_event_keyboard_key *event = data;
 | 
						struct wlr_event_keyboard_key *event = data;
 | 
				
			||||||
	struct keyboard_state *kbstate = wl_container_of(listener, kbstate, key);
 | 
						struct keyboard_state *kbstate = wl_container_of(listener, kbstate, key);
 | 
				
			||||||
	uint32_t keycode = event->keycode + 8;
 | 
						uint32_t keycode = event->keycode + 8;
 | 
				
			||||||
	enum wlr_key_state key_state = event->state;
 | 
						enum wlr_key_state key_state = event->state;
 | 
				
			||||||
	const xkb_keysym_t *syms;
 | 
						const xkb_keysym_t *syms;
 | 
				
			||||||
	int nsyms = xkb_state_key_get_syms(kbstate->xkb_state, keycode, &syms);
 | 
						int nsyms = xkb_state_key_get_syms(kbstate->device->keyboard->xkb_state,
 | 
				
			||||||
	xkb_state_update_key(kbstate->xkb_state, keycode,
 | 
								keycode, &syms);
 | 
				
			||||||
		event->state == WLR_KEY_PRESSED ?  XKB_KEY_DOWN : XKB_KEY_UP);
 | 
					
 | 
				
			||||||
	keyboard_led_update(kbstate);
 | 
					 | 
				
			||||||
	for (int i = 0; i < nsyms; ++i) {
 | 
						for (int i = 0; i < nsyms; ++i) {
 | 
				
			||||||
		xkb_keysym_t sym = syms[i];
 | 
							xkb_keysym_t sym = syms[i];
 | 
				
			||||||
		char name[64];
 | 
							char name[64];
 | 
				
			||||||
| 
						 | 
					@ -88,26 +77,9 @@ static void keyboard_add(struct wlr_input_device *device, struct compositor_stat
 | 
				
			||||||
		wlr_log(L_ERROR, "Failed to create XKB context");
 | 
							wlr_log(L_ERROR, "Failed to create XKB context");
 | 
				
			||||||
		exit(1);
 | 
							exit(1);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	kbstate->keymap = xkb_map_new_from_names(
 | 
						wlr_keyboard_set_keymap(device->keyboard, xkb_map_new_from_names(context,
 | 
				
			||||||
			context, &rules, XKB_KEYMAP_COMPILE_NO_FLAGS);
 | 
									&rules, XKB_KEYMAP_COMPILE_NO_FLAGS));
 | 
				
			||||||
	if (!kbstate->keymap) {
 | 
					 | 
				
			||||||
		wlr_log(L_ERROR, "Failed to create XKB keymap");
 | 
					 | 
				
			||||||
		exit(1);
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	xkb_context_unref(context);
 | 
						xkb_context_unref(context);
 | 
				
			||||||
	kbstate->xkb_state = xkb_state_new(kbstate->keymap);
 | 
					 | 
				
			||||||
	if (!kbstate->xkb_state) {
 | 
					 | 
				
			||||||
		wlr_log(L_ERROR, "Failed to create XKB state");
 | 
					 | 
				
			||||||
		exit(1);
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	const char *led_names[3] = {
 | 
					 | 
				
			||||||
		XKB_LED_NAME_NUM,
 | 
					 | 
				
			||||||
		XKB_LED_NAME_CAPS,
 | 
					 | 
				
			||||||
		XKB_LED_NAME_SCROLL
 | 
					 | 
				
			||||||
	};
 | 
					 | 
				
			||||||
	for (uint32_t i = 0; i < 3; ++i) {
 | 
					 | 
				
			||||||
		kbstate->leds[i] = xkb_map_led_get_index(kbstate->keymap, led_names[i]);
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void pointer_motion_notify(struct wl_listener *listener, void *data) {
 | 
					static void pointer_motion_notify(struct wl_listener *listener, void *data) {
 | 
				
			||||||
| 
						 | 
					@ -320,8 +292,6 @@ static void keyboard_remove(struct wlr_input_device *device, struct compositor_s
 | 
				
			||||||
	if (!kbstate) {
 | 
						if (!kbstate) {
 | 
				
			||||||
		return;
 | 
							return;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	xkb_state_unref(kbstate->xkb_state);
 | 
					 | 
				
			||||||
	xkb_map_unref(kbstate->keymap);
 | 
					 | 
				
			||||||
	wl_list_remove(&kbstate->link);
 | 
						wl_list_remove(&kbstate->link);
 | 
				
			||||||
	wl_list_remove(&kbstate->key.link);
 | 
						wl_list_remove(&kbstate->key.link);
 | 
				
			||||||
	free(kbstate);
 | 
						free(kbstate);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -27,9 +27,6 @@ struct keyboard_state {
 | 
				
			||||||
	struct wlr_input_device *device;
 | 
						struct wlr_input_device *device;
 | 
				
			||||||
	struct wl_listener key;
 | 
						struct wl_listener key;
 | 
				
			||||||
	struct wl_list link;
 | 
						struct wl_list link;
 | 
				
			||||||
	struct xkb_keymap *keymap;
 | 
					 | 
				
			||||||
	struct xkb_state *xkb_state;
 | 
					 | 
				
			||||||
	xkb_led_index_t leds[WLR_LED_LAST];
 | 
					 | 
				
			||||||
	void *data;
 | 
						void *data;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -59,9 +59,8 @@ void keyboard_add(struct wlr_input_device *device, struct roots_input *input) {
 | 
				
			||||||
	rules.options = getenv("XKB_DEFAULT_OPTIONS");
 | 
						rules.options = getenv("XKB_DEFAULT_OPTIONS");
 | 
				
			||||||
	struct xkb_context *context;
 | 
						struct xkb_context *context;
 | 
				
			||||||
	assert(context = xkb_context_new(XKB_CONTEXT_NO_FLAGS));
 | 
						assert(context = xkb_context_new(XKB_CONTEXT_NO_FLAGS));
 | 
				
			||||||
	wlr_keyboard_set_keymap(device->keyboard,
 | 
						wlr_keyboard_set_keymap(device->keyboard, xkb_map_new_from_names(context,
 | 
				
			||||||
			xkb_map_new_from_names(context, &rules,
 | 
									&rules, XKB_KEYMAP_COMPILE_NO_FLAGS));
 | 
				
			||||||
				XKB_KEYMAP_COMPILE_NO_FLAGS));
 | 
					 | 
				
			||||||
	xkb_context_unref(context);
 | 
						xkb_context_unref(context);
 | 
				
			||||||
	wlr_seat_attach_keyboard(input->wl_seat, device);
 | 
						wlr_seat_attach_keyboard(input->wl_seat, device);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue