summaryrefslogtreecommitdiff
path: root/src/common/input.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/input.c')
-rw-r--r--src/common/input.c21
1 files changed, 13 insertions, 8 deletions
diff --git a/src/common/input.c b/src/common/input.c
index ff578e3..471cd2b 100644
--- a/src/common/input.c
+++ b/src/common/input.c
@@ -35,11 +35,11 @@ static void discard_pending(struct input_queue *q)
bool input_queue_push(struct input_queue *q, const struct yasl_input_event *ev)
{
struct yasl_input_event copy = *ev;
- size_t len;
- const char *text = event_text(ev, &len);
char *owned = NULL;
if (ev->kind == YASL_INPUT_TEXT || ev->kind == YASL_INPUT_PASTE ||
ev->kind == YASL_INPUT_DROP_FILES) {
+ size_t len;
+ const char *text = event_text(ev, &len);
if ((!text && len) || len == SIZE_MAX) goto failed;
owned = malloc(len + 1);
if (!owned) goto failed;
@@ -65,7 +65,7 @@ bool input_queue_push(struct input_queue *q, const struct yasl_input_event *ev)
q->capacity = capacity;
q->head = 0;
}
- q->events[(q->head + q->count) % q->capacity] = copy;
+ q->events[(q->head + q->count) & (q->capacity - 1)] = copy;
q->count++;
return true;
@@ -82,8 +82,10 @@ failed:
bool input_queue_pop(struct input_queue *q, struct yasl_input_event *ev)
{
if (!ev || (!q->count && !q->reset_pending)) return false;
- free(q->last_text);
- q->last_text = NULL;
+ if (q->last_text) {
+ free(q->last_text);
+ q->last_text = NULL;
+ }
if (q->reset_pending) {
*ev = (struct yasl_input_event){
.surface = q->reset_surface, .kind = YASL_INPUT_RESET,
@@ -92,9 +94,12 @@ bool input_queue_pop(struct input_queue *q, struct yasl_input_event *ev)
return true;
}
*ev = q->events[q->head];
- size_t len;
- q->last_text = (char *)event_text(ev, &len);
- q->head = (q->head + 1) % q->capacity;
+ if (ev->kind == YASL_INPUT_TEXT || ev->kind == YASL_INPUT_PASTE ||
+ ev->kind == YASL_INPUT_DROP_FILES) {
+ size_t len;
+ q->last_text = (char *)event_text(ev, &len);
+ }
+ q->head = (q->head + 1) & (q->capacity - 1);
q->count--;
return true;
}