Can now quit the simple example

This commit is contained in:
Timidger 2018-04-16 20:13:02 -04:00
parent b98779a674
commit 44b2bf18f0
No known key found for this signature in database
GPG Key ID: 42EF7ECE5FEF1D17
1 changed files with 75 additions and 2 deletions

View File

@ -9,11 +9,14 @@
#include <wlr/backend.h>
#include <wlr/backend/session.h>
#include <wlr/types/wlr_output.h>
#include <wlr/types/wlr_input_device.h>
#include <wlr/util/log.h>
#include <xkbcommon/xkbcommon.h>
struct sample_state {
struct wl_display *display;
struct wl_listener new_output;
struct wl_listener new_input;
struct timespec last_frame;
float color[3];
int dec;
@ -26,6 +29,13 @@ struct sample_output {
struct wl_listener destroy;
};
struct sample_keyboard {
struct sample_state *sample;
struct wlr_input_device *device;
struct wl_listener key;
struct wl_listener destroy;
};
void output_frame_notify(struct wl_listener *listener, void *data) {
struct sample_output *sample_output = wl_container_of(listener, sample_output, frame);
@ -58,6 +68,7 @@ void output_frame_notify(struct wl_listener *listener, void *data) {
void output_remove_notify(struct wl_listener *listener, void *data) {
struct sample_output *sample_output = wl_container_of(listener, sample_output, destroy);
wl_list_remove(&sample_output->frame.link);
wl_list_remove(&sample_output->destroy.link);
free(sample_output);
}
@ -73,20 +84,82 @@ void new_output_notify(struct wl_listener *listener, void *data) {
sample_output->destroy.notify = output_remove_notify;
}
void keyboard_key_notify(struct wl_listener *listener, void *data) {
struct sample_keyboard *keyboard = wl_container_of(listener, keyboard, key);
struct sample_state *sample = keyboard->sample;
struct wlr_event_keyboard_key *event = data;
uint32_t keycode = event->keycode + 8;
const xkb_keysym_t *syms;
int nsyms = xkb_state_key_get_syms(keyboard->device->keyboard->xkb_state,
keycode, &syms);
for (int i = 0; i < nsyms; i++) {
xkb_keysym_t sym = syms[i];
if (sym == XKB_KEY_Escape) {
wl_display_terminate(sample->display);
}
}
// TODO keyboard input
}
void keyboard_destroy_notify(struct wl_listener *listener, void *data) {
struct sample_keyboard *keyboard = wl_container_of(listener, keyboard, destroy);
wl_list_remove(&keyboard->destroy.link);
wl_list_remove(&keyboard->key.link);
free(keyboard);
}
void new_input_notify(struct wl_listener *listener, void *data) {
struct wlr_input_device *device = data;
struct sample_state *sample = wl_container_of(listener, sample, new_input);
struct sample_keyboard *keyboard;
struct xkb_rule_names rules;
struct xkb_context *context;
switch (device->type) {
case WLR_INPUT_DEVICE_KEYBOARD:
keyboard = calloc(1, sizeof(struct sample_keyboard));
keyboard->device = device;
keyboard->sample = sample;
wl_signal_add(&device->events.destroy, &keyboard->destroy);
keyboard->destroy.notify = keyboard_destroy_notify;
wl_signal_add(&device->keyboard->events.key, &keyboard->key);
keyboard->key.notify = keyboard_key_notify;
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");
context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
if (!context) {
wlr_log(L_ERROR, "Failed to create XKB context");
exit(1);
}
wlr_keyboard_set_keymap(device->keyboard, xkb_map_new_from_names(context,
&rules, XKB_KEYMAP_COMPILE_NO_FLAGS));
xkb_context_unref(context);
break;
default:
break;
}
}
int main() {
wlr_log_init(L_DEBUG, NULL);
struct wl_display *display = wl_display_create();
struct sample_state state = {
.color = { 1.0, 0.0, 0.0 },
.dec = 0,
.last_frame = { 0 }
.last_frame = { 0 },
.display = display
};
struct wl_display *display = wl_display_create();
struct wlr_backend *wlr = wlr_backend_autocreate(display);
if (!wlr) {
exit(1);
}
wl_signal_add(&wlr->events.new_output, &state.new_output);
state.new_output.notify = new_output_notify;
wl_signal_add(&wlr->events.new_input, &state.new_input);
state.new_input.notify = new_input_notify;
clock_gettime(CLOCK_MONOTONIC, &state.last_frame);
const char *socket = wl_display_add_socket_auto(display);
if (!socket) {