aboutsummaryrefslogtreecommitdiffstats
path: root/src/cocoa_bridge.rs
blob: d98701fb8639d5af171229e4d99e8e3f4585bb97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use std::ffi::CString;
use std::slice;

use cocoa::base::nil;
use cocoa::foundation::{NSArray, NSAutoreleasePool, NSDictionary};
use libc::size_t;

use {HeadphoneButton, MapGroup, MapKind};

#[repr(C)]
struct renameMeMapGroup {
}

pub extern "C" fn parse_mappings() {
    let sample_maps = "map <up> k
map <down> j";

    let map_group = MapGroup::parse(sample_maps).unwrap();

    unsafe {
        let _pool = NSAutoreleasePool::new(nil);

        let maps = NSDictionary::init(nil).autorelease();
        let modes = NSDictionary::init(nil).autorelease();

        for (trigger, action) in map_group.maps {
            // let t = NSArray::arrayWithObjects(nil, &trigger).autorelease();

            // maps.
        }

        for (trigger, modes) in map_group.modes {
        }
    }
}

// Different method:
// Call Rust function with trigger
// Return keys to press
// or run command (from Rust?)
// Somehow: switch mode inside Rust

#[repr(C)]
pub struct KeyActionResult {
    pub action: Option<CString>,
    pub kind: MapKind,
}

#[no_mangle]
pub extern "C" fn c_run_key_action(
    trigger: *const HeadphoneButton,
    length: size_t,
) -> *const KeyActionResult {
    let trigger = unsafe {
        assert!(!trigger.is_null());

        slice::from_raw_parts(trigger, length as usize)
    };

    let result = run_key_action(trigger).unwrap();
    &result as *const KeyActionResult
}

#[no_mangle]
pub extern "C" fn run_key_action(
    trigger: &[HeadphoneButton]
) -> Option<KeyActionResult> {
    let sample_maps = "map <up> k
map <down> j";

    // Figure out how to persist this without re-parsing
    let map_group = MapGroup::parse(sample_maps).unwrap();

    let map = map_group.maps.get(trigger);
    let mode = map_group.modes.get(trigger);

    if let Some(map) = map {
        return match map.kind {
            MapKind::Map => {
                // let action_bytes = map.action;
                // let x = action_bytes.as_bytes();
                // let action = CStr::from_bytes_with_nul(x).unwrap();
                let action = CString::new(map.action.clone()).unwrap();

                Some(KeyActionResult {
                    action: Some(action),
                    kind: MapKind::Map,
                })
            },
            MapKind::Command => {
                Some(KeyActionResult {
                    action: None,
                    kind: MapKind::Command,
                })
            },
        }
    }

    if let Some(mode) = mode {
    }

    // match map_group.get(trigger) {
    //     Some(map_action) => {
    //         Some(KeyActionResult {
    //             action: map_action.action,
    //             kind: MapKind::Map,
    //         })
    //     },
    //     None => {
    //         // TODO: Figure out how to error
    //         None
    //     },
    // }

    None
}

// fn run_command(command: Action) -> Result {
// }


mod tests {
    use super::*;

    #[test]
    fn parse_mappings_makes_cocoa_mappings() {
        parse_mappings();
    }
}