aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2018-09-02 14:12:56 +0200
committerTeddy Wing2018-09-02 14:12:56 +0200
commit4b31eb9b8412e429949aca73ef475787910e4b0b (patch)
treebe5e5582ada52cf2e26bd455e91a5a2fb32ce69b
parent69352361515b16c1ed9646b891ba61ed1781a7a7 (diff)
downloaddome-key-map-4b31eb9b8412e429949aca73ef475787910e4b0b.tar.bz2
Test mode finder
Add a new `moder.c` based on `includer.c` to test getting a mode and then getting an action for a mode mapping. Doesn't completely work currently. Right now all I get is this output: $ ./moder 2 Segmentation fault: 11 Clearly I'm messing up something with the pointer in `c_run_key_action()`. Not sure what it is yet. Changed `CKeyActionResult.in_mode` to be a `Trigger` instead of a `HeadphoneButton` list. That allows us to pass it directly into the second argument of `c_run_key_action()` without converting. But maybe the pointer value isn't living long enough? Not sure what the problem is yet.
-rw-r--r--Makefile5
-rw-r--r--dome_key_map.h12
-rw-r--r--moder.c26
-rw-r--r--src/cocoa_bridge.rs14
4 files changed, 48 insertions, 9 deletions
diff --git a/Makefile b/Makefile
index 5f1288b..c5bd2e2 100644
--- a/Makefile
+++ b/Makefile
@@ -8,6 +8,9 @@ $(LIB): $(SOURCE_FILES)
includer: clean $(LIB)
gcc -o $@ includer.c $(LIB)
+moder: clean $(LIB)
+ gcc -o $@ $@.c $(LIB)
+
.PHONY: clean
clean:
- rm -f includer
+ rm -f includer moder
diff --git a/dome_key_map.h b/dome_key_map.h
index 76b5f1c..7bd77ca 100644
--- a/dome_key_map.h
+++ b/dome_key_map.h
@@ -24,14 +24,14 @@ typedef enum {
} MapKind;
typedef struct {
- const char *action;
- const ActionKind *kind;
- const HeadphoneButton *in_mode;
-} CKeyActionResult;
-
-typedef struct {
const HeadphoneButton *buttons;
size_t length;
} Trigger;
+typedef struct {
+ const char *action;
+ const ActionKind *kind;
+ const Trigger *in_mode;
+} CKeyActionResult;
+
const CKeyActionResult *c_run_key_action(Trigger trigger, const Trigger *mode);
diff --git a/moder.c b/moder.c
new file mode 100644
index 0000000..a10a6da
--- /dev/null
+++ b/moder.c
@@ -0,0 +1,26 @@
+#include <stdio.h>
+#include "dome_key_map.h"
+
+#define SIZE 2
+
+int main() {
+ HeadphoneButton mode_buttons[SIZE] = {HeadphoneButton_Play, HeadphoneButton_Up};
+ Trigger mode_trigger = {
+ .buttons = mode_buttons,
+ .length = SIZE
+ };
+ const CKeyActionResult *mode = c_run_key_action(mode_trigger, NULL);
+ printf("%d\n", *mode->kind);
+
+ HeadphoneButton buttons[] = {HeadphoneButton_Down};
+ Trigger trigger = {
+ .buttons = buttons,
+ .length = 1
+ };
+ const CKeyActionResult *result = c_run_key_action(trigger, mode->in_mode);
+
+ printf("%d\n", *result->kind);
+ printf("%s", result->action);
+
+ return 0;
+}
diff --git a/src/cocoa_bridge.rs b/src/cocoa_bridge.rs
index 920ea02..15cee95 100644
--- a/src/cocoa_bridge.rs
+++ b/src/cocoa_bridge.rs
@@ -86,7 +86,7 @@ impl<'a> KeyActionResult<'a> {
pub struct CKeyActionResult {
pub action: *const c_char,
pub kind: *const ActionKind,
- pub in_mode: *const HeadphoneButton,
+ pub in_mode: *const Trigger,
}
#[no_mangle]
@@ -120,7 +120,14 @@ pub extern "C" fn c_run_key_action(
);
let in_mode = k.in_mode.map_or_else(
|| ptr::null(),
- |m| m.as_ptr(),
+ |m| {
+ let trigger = Trigger {
+ buttons: m.as_ptr(),
+ length: m.len(),
+ };
+
+ &trigger
+ },
);
CKeyActionResult {
@@ -149,6 +156,9 @@ pub extern "C" fn run_key_action_for_mode<'a>(
let sample_maps = "map <up> k
map <down> j
map <play><down> works!
+mode <play><up> {
+ map <down> hello
+}
";
// Figure out how to persist this without re-parsing