Skip to content

Commit

Permalink
release all keys on sigterm and sigint
Browse files Browse the repository at this point in the history
  • Loading branch information
xdavidwu committed Mar 13, 2021
1 parent 8939f4f commit 367b693
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 20 deletions.
22 changes: 13 additions & 9 deletions im.c
Original file line number Diff line number Diff line change
Expand Up @@ -431,15 +431,7 @@ static void handle_done(void *data,
}
chewing_Reset(state->chewing);

struct wlchewing_keysym *mkeysym, *tmp;
wl_list_for_each_safe(mkeysym, tmp,
&state->press_sent_keysyms, link) {
zwp_virtual_keyboard_v1_key(state->virtual_keyboard,
get_millis() - state->millis_offset,
mkeysym->key, WL_KEYBOARD_KEY_STATE_RELEASED);
wl_list_remove(&mkeysym->link);
free(mkeysym);
}
im_release_all_keys(state);
}
state->activated = state->pending_activate;
wl_display_roundtrip(state->display);
Expand All @@ -455,6 +447,18 @@ static const struct zwp_input_method_v2_listener im_listener = {
.unavailable = handle_unavailable,
};

void im_release_all_keys(struct wlchewing_state *state) {
struct wlchewing_keysym *mkeysym, *tmp;
wl_list_for_each_safe(mkeysym, tmp,
&state->press_sent_keysyms, link) {
zwp_virtual_keyboard_v1_key(state->virtual_keyboard,
get_millis() - state->millis_offset,
mkeysym->key, WL_KEYBOARD_KEY_STATE_RELEASED);
wl_list_remove(&mkeysym->link);
free(mkeysym);
}
}

/* TODO for adding panel with input-method support
* currently only panel with wlr-layer-shell
*/
Expand Down
38 changes: 27 additions & 11 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
#include <assert.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <sys/epoll.h>
#include <sys/timerfd.h>

#include "wlchewing.h"
#include "bottom-panel.h"
#include "sni.h"

struct wlchewing_state global_state = {0};

static void handle_signal(int signo) {
im_release_all_keys(&global_state);
wl_display_roundtrip(global_state.display);
raise(signo);
}

static const struct wl_output_listener output_listener;

static void handle_global(void *data, struct wl_registry *registry,
Expand Down Expand Up @@ -60,12 +69,7 @@ static const struct wl_output_listener output_listener = {
};

int main(int argc, char **argv) {
struct wlchewing_state *state = calloc(1,
sizeof(struct wlchewing_state));
if (state == NULL) {
wlchewing_err("Failed to calloc state");
return EXIT_FAILURE;
}
struct wlchewing_state *state = &global_state;
state->display = wl_display_connect(NULL);
if (state->display == NULL) {
wlchewing_err("Failed to create display");
Expand Down Expand Up @@ -118,8 +122,7 @@ int main(int argc, char **argv) {
.fd = state->timer_fd,
},
};
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, state->timer_fd, &timer_epoll)
== -1) {
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, state->timer_fd, &timer_epoll) < 0) {
wlchewing_err("Failed to add timer epoll: %s", strerror(errno));
return EXIT_FAILURE;
}
Expand All @@ -131,7 +134,7 @@ int main(int argc, char **argv) {
}
int bus_fd = sni_setup(state->sni);
if (bus_fd < 0) {
wlchewing_err("Failed to setup bus: %s", strerror(-bus_fd));
wlchewing_err("Failed to setup dbus: %s", strerror(-bus_fd));
return EXIT_FAILURE;
}
struct epoll_event bus_epoll = {
Expand All @@ -140,14 +143,27 @@ int main(int argc, char **argv) {
.fd = bus_fd,
},
};
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, bus_fd, &bus_epoll)
== -1) {
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, bus_fd, &bus_epoll) < 0) {
wlchewing_err("Failed to add bus epoll: %s", strerror(errno));
return EXIT_FAILURE;
}

im_setup(state);

struct sigaction sa;
sa.sa_handler = handle_signal;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESETHAND;

if (sigaction(SIGTERM, &sa, NULL) < 0) {
wlchewing_err("Failed to set SIGTERM handler: %s", strerror(errno));
return EXIT_FAILURE;
}
if (sigaction(SIGINT, &sa, NULL) < 0) {
wlchewing_err("Failed to set SIGINT handler: %s", strerror(errno));
return EXIT_FAILURE;
}

struct epoll_event event_caught;
while (epoll_wait(epoll_fd, &event_caught, 1, -1)) {
if (event_caught.data.fd == display_fd) {
Expand Down
2 changes: 2 additions & 0 deletions wlchewing.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ void im_destory(struct wlchewing_state *state);

int im_key_press(struct wlchewing_state *state, uint32_t key);

void im_release_all_keys(struct wlchewing_state *state);

#define wlchewing_err(fmt, ...) fprintf(stderr, "[%s:%d] " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__)

#define KEY_HANDLE_FORWARD (1 << 0)
Expand Down

0 comments on commit 367b693

Please sign in to comment.