aboutsummaryrefslogtreecommitdiffstats
path: root/moder.c
AgeCommit message (Collapse)Author
2018-09-06c_run_key_action(): Fix segfault on `in_mode`Teddy Wing
Finally figured out where the problem was with the `in_mode` on my `CKeyActionResult` getting freed too early. It happens because we're trying to use the reference to the `in_mode` slice from the `KeyActionResult` returned by `run_key_action_for_mode()`. We were putting that reference in the `in_mode` of the `CKeyActionResult` returned by `c_run_key_action()`, but it would get freed in that function. In order to make it last indefinitely, we needed to convert it into a raw boxed pointer to prevent Rust from deallocating it. This does mean we now have a memory leak, but we'll just have to make a call into Rust to free it from the C code.
2018-09-06Trying to fix pointer lifetime of mode `CKeyActionResult` (WIP)Teddy Wing
Can't get this working. Tried a bunch of things. Decided to commit this at a stopping point before messing any further. Sort of getting there, with some things that seem like they're pointing in the right direction, but it's not working out so far. Still getting a segfault. Actually, with the `Box::into_raw` addition, the lifetime of the mode result actually lasts for a single call (printf), and then gets freed (weirdly). Makefile: * Add `-W` error flags to see if those would give me any extra information about the pointer problems, but didn't get anything. The fact that I didn't get errors indicated it was a problem on the Rust side. * Change `clean` dependency to `moder.c` so we don't rebuild if the C file (and lib) hasn't changed. c_run_key_action(): * Add some `println!`s and debug clutter * Add a separate `let` binding for `run_key_action_for_mode()` because that seemed to fix some weirdness in control flow when I was debugging the code at one point in `lldb`. Not sure it this change still makes sense to keep. * Changed function/closure version of `in_mode` to `if`/`else` because that may have made a difference in some dumb things I was experimenting with at some point. * Unsuccessful tries of `mem::forget()`, ultimately commented out. * Return a `Box::into_raw` pointer to the `CKeyActionResult` instead of the Rust pointer we were previously returning. This allows the value to live beyond the function and beyond Rust's control into the calling C code. This does, however, mean that the value leaks memory, and will need to be freed manually in the calling C code. run_key_action_for_mode(): * Wrap map handler in a check to verify `in_mode` is `None`. This ensures that if we should be returning a `KeyActionResult` for a mapping inside a mode, we don't run this code that's meant for mappings outside a mode.
2018-09-02Test mode finderTeddy Wing
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.