]>
Commit | Line | Data |
---|---|---|
a4a617fd SW |
1 | /* tattlekey: A one-key UDP keyboard |
2 | * Copyright (C) 2023 Scott Worley <scottworley@scottworley.com> | |
3 | * | |
4 | * This program is free software: you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation, either version 3 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | |
16 | */ | |
17 | ||
00ca9fa6 | 18 | #include "press.h" |
e3ff9e0d | 19 | #include "blink.h" |
0527f229 | 20 | #include "config.h" |
2dcbb2a0 | 21 | |
0527f229 | 22 | static uint32_t press_next_send(press_t *s) { |
2dcbb2a0 SW |
23 | return s->timestamp + (1 << s->send_count) - 1; |
24 | } | |
25 | ||
0527f229 SW |
26 | static uint32_t press_queue_next_send(queue_t *q) { |
27 | press_t press; | |
28 | return queue_try_peek(q, &press) ? press_next_send(&press) : UINT32_MAX; | |
29 | } | |
30 | ||
e3ff9e0d SW |
31 | static bool next_send_less_than(void *user_data, pheap_node_id_t a, |
32 | pheap_node_id_t b) { | |
0527f229 SW |
33 | queue_t **sleeps = (queue_t **)user_data; |
34 | return press_queue_next_send(sleeps[a]) < press_queue_next_send(sleeps[b]); | |
2dcbb2a0 | 35 | } |
67001c93 | 36 | |
6e43b84f SW |
37 | static void *xcalloc(size_t nmemb, size_t size) { |
38 | void *p = calloc(nmemb, size); | |
39 | if (p == NULL) | |
e3ff9e0d | 40 | signal_error_by_blinking(); |
6e43b84f SW |
41 | return p; |
42 | } | |
43 | ||
44 | press_pile_t *create_press_pile() { | |
45 | press_pile_t *pp = (press_pile_t *)xcalloc(1, sizeof(press_pile_t)); | |
0527f229 SW |
46 | pp->presses = (queue_t *)xcalloc(config_resend_count, sizeof(queue_t)); |
47 | pp->sleeps = (queue_t **)xcalloc(config_resend_count, sizeof(queue_t *)); | |
8c76d103 | 48 | for (uint i = 0; i < config_resend_count; i++) { |
a36c278f SW |
49 | const uint paranoid_safety_fudge = 10; |
50 | uint element_count = paranoid_safety_fudge + (1 << i); | |
51 | element_count = MAX(element_count, config_minimum_queue_size); | |
52 | element_count = MIN(element_count, config_maximum_queue_size); | |
0527f229 SW |
53 | queue_init(&pp->presses[i], sizeof(press_t), element_count); |
54 | } | |
e3ff9e0d | 55 | pp->sleeps_heap = |
0527f229 | 56 | ph_create(config_resend_count, next_send_less_than, pp->sleeps); |
e3ff9e0d SW |
57 | if (pp->sleeps_heap == NULL) |
58 | signal_error_by_blinking(); | |
59 | return pp; | |
60 | } | |
61 | ||
f68e05b2 | 62 | void add_press(press_pile_t *pp, press_t *press) { |
8c76d103 | 63 | u16_t sc = press->send_count; |
0527f229 SW |
64 | if (sc >= config_resend_count) |
65 | signal_error_by_blinking(); | |
66 | bool was_empty = queue_is_empty(&pp->presses[sc]); | |
67 | /* No error check; blithely continue if the queue was full. */ | |
68 | queue_try_add(&pp->presses[sc], press); | |
69 | if (was_empty) { | |
70 | pheap_node_id_t i = ph_new_node(pp->sleeps_heap); | |
71 | pp->sleeps[i] = &pp->presses[sc]; | |
72 | ph_insert_node(pp->sleeps_heap, i); | |
67001c93 | 73 | } |
67001c93 | 74 | } |
b512ec26 | 75 | |
e3ff9e0d SW |
76 | int32_t next_scheduled_send(press_pile_t *pp) { |
77 | pheap_node_id_t i = ph_peek_head(pp->sleeps_heap); | |
b512ec26 SW |
78 | if (i == 0) |
79 | return -1; | |
0527f229 | 80 | return press_queue_next_send(pp->sleeps[i]); |
b512ec26 | 81 | } |
9ae691e9 SW |
82 | |
83 | bool get_press_due_for_resend(press_pile_t *pp, uint32_t now, press_t *press) { | |
84 | pheap_node_id_t i = ph_peek_head(pp->sleeps_heap); | |
0527f229 | 85 | if (i == 0 || press_queue_next_send(pp->sleeps[i]) > now) |
9ae691e9 | 86 | return false; |
0527f229 SW |
87 | if (!queue_try_remove(pp->sleeps[i], press)) |
88 | signal_error_by_blinking(); | |
89 | bool became_empty = queue_is_empty(pp->sleeps[i]); | |
90 | if (ph_remove_head(pp->sleeps_heap, became_empty) != i) | |
9ae691e9 | 91 | signal_error_by_blinking(); |
0527f229 SW |
92 | if (became_empty) { |
93 | pp->sleeps[i] = NULL; | |
94 | } else { | |
95 | ph_insert_node(pp->sleeps_heap, i); | |
96 | } | |
9ae691e9 SW |
97 | return true; |
98 | } |