Initialize keyboards from libinput

This commit is contained in:
Drew DeVault 2017-06-09 17:31:21 -04:00
parent 019fff06be
commit 0e75d157f5
12 changed files with 338 additions and 51 deletions

View file

@ -13,16 +13,29 @@ struct state {
float color[3];
int dec;
struct timespec last_frame;
struct wl_list keyboards;
struct wl_listener input_add;
struct wl_listener input_remove;
struct wl_listener output_add;
struct wl_listener output_remove;
struct wl_list outputs;
};
struct output_state {
struct wl_list link;
struct wlr_output *output;
struct state *state;
struct wlr_output *output;
struct wl_listener frame;
struct wl_list link;
};
struct keyboard_state {
struct state *state;
struct wlr_input_device *device;
struct wl_listener key;
struct wl_listener mods;
struct wl_list link;
};
void output_frame(struct wl_listener *listener, void *data) {
@ -84,41 +97,63 @@ void output_remove(struct wl_listener *listener, void *data) {
wl_list_remove(&ostate->frame.link);
}
void input_add(struct wl_listener *listener, void *data) {
struct wlr_input_device *device = data;
struct state *state = wl_container_of(listener, state, input_add);
if (device->type != WLR_INPUT_DEVICE_KEYBOARD) {
return;
}
fprintf(stderr, "Keyboard '%s' (%d:%d) added\n", device->name,
device->vendor, device->product);
struct keyboard_state *kbstate = calloc(sizeof(struct keyboard_state), 1);
kbstate->device = device;
wl_list_init(&kbstate->key.link);
wl_list_init(&kbstate->mods.link);
wl_list_insert(&state->keyboards, &kbstate->link);
}
void input_remove(struct wl_listener *listener, void *data) {
struct wlr_input_device *device = data;
struct state *state = wl_container_of(listener, state, input_add);
if (device->type != WLR_INPUT_DEVICE_KEYBOARD) {
return;
}
struct keyboard_state *kbstate = NULL, *_kbstate;
wl_list_for_each(_kbstate, &state->keyboards, link) {
if (_kbstate->device == device) {
kbstate = kbstate;
break;
}
}
if (!kbstate) {
return; // We are unfamiliar with this keyboard
}
wl_list_remove(&kbstate->link);
//wl_list_remove(&kbstate->key.link);
//wl_list_remove(&kbstate->mods.link);
}
int timer_done(void *data) {
*(bool *)data = true;
return 1;
}
int enable_outputs(void *data) {
struct state *state = data;
struct output_state *ostate;
wl_list_for_each(ostate, &state->outputs, link) {
struct wlr_output *output = ostate->output;
wlr_output_enable(output, true);
}
return 1;
}
int disable_outputs(void *data) {
struct state *state = data;
struct output_state *ostate;
wl_list_for_each(ostate, &state->outputs, link) {
struct wlr_output *output = ostate->output;
wlr_output_enable(output, false);
}
return 1;
}
int main() {
struct state state = {
.color = { 1.0, 0.0, 0.0 },
.dec = 0,
.input_add = { .notify = input_add },
.input_remove = { .notify = input_remove },
.output_add = { .notify = output_add },
.output_remove = { .notify = output_remove }
};
wl_list_init(&state.keyboards);
wl_list_init(&state.input_add.link);
wl_list_init(&state.input_remove.link);
wl_list_init(&state.outputs);
wl_list_init(&state.output_add.link);
wl_list_init(&state.output_remove.link);
wl_list_init(&state.output_remove.link);
clock_gettime(CLOCK_MONOTONIC, &state.last_frame);
@ -131,6 +166,8 @@ int main() {
}
struct wlr_backend *wlr = wlr_backend_autocreate(display, session);
wl_signal_add(&wlr->events.input_add, &state.input_add);
wl_signal_add(&wlr->events.input_remove, &state.input_remove);
wl_signal_add(&wlr->events.output_add, &state.output_add);
wl_signal_add(&wlr->events.output_remove, &state.output_remove);
if (!wlr || !wlr_backend_init(wlr)) {
@ -140,14 +177,8 @@ int main() {
bool done = false;
struct wl_event_source *timer = wl_event_loop_add_timer(event_loop,
timer_done, &done);
struct wl_event_source *timer_disable_outputs =
wl_event_loop_add_timer(event_loop, disable_outputs, &state);
struct wl_event_source *timer_enable_outputs =
wl_event_loop_add_timer(event_loop, enable_outputs, &state);
wl_event_source_timer_update(timer, 20000);
wl_event_source_timer_update(timer_disable_outputs, 5000);
wl_event_source_timer_update(timer_enable_outputs, 10000);
wl_event_source_timer_update(timer, 10000);
while (!done) {
wl_event_loop_dispatch(event_loop, 0);