aboutsummaryrefslogtreecommitdiffstats
path: root/src/cocoa_bridge.rs
blob: f33a739c8d440a2a0235f41a8d8aa157c4e8b138 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use std::ffi::{CStr, CString};
use std::ptr;
use std::slice;

// use cocoa::base::nil;
// use cocoa::foundation::{NSArray, NSAutoreleasePool, NSDictionary};
use libc::{c_char, 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,
}

#[repr(C)]
pub struct CKeyActionResult {
    pub action: *const c_char,
    pub kind: *const MapKind,
}

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

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

    let result = match run_key_action(trigger) {
        Some(k) => {
            match k.action {
                Some(a) => {
                    CKeyActionResult {
                        action: CStr::from_bytes_with_nul(b"test?\n\0").unwrap().as_ptr(),
                        kind: &k.kind,
                    }
                },
                None => {
                    CKeyActionResult {
                        action: ptr::null(),
                        kind: &k.kind,
                    }
                },
            }
        },
        None => {
            CKeyActionResult {
                action: ptr::null(),
                kind: ptr::null(),
            }
        }
    };

    &result as *const CKeyActionResult
}

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

    // 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();
    }
}