symbols matrix: Remove in favor of a levels vector
This commit is contained in:
		@ -28,7 +28,6 @@ sources = [
 | 
			
		||||
  '../eek/eek-section.c',
 | 
			
		||||
  '../eek/eek-serializable.c',
 | 
			
		||||
  '../eek/eek-symbol.c',
 | 
			
		||||
  '../eek/eek-symbol-matrix.c',
 | 
			
		||||
  '../eek/eek-text.c',
 | 
			
		||||
  '../eek/eek-types.c',
 | 
			
		||||
  '../eek/eek-xml-layout.c',
 | 
			
		||||
 | 
			
		||||
@ -6,6 +6,7 @@
 | 
			
		||||
// Defined in Rust
 | 
			
		||||
 | 
			
		||||
struct squeek_symbol;
 | 
			
		||||
struct squeek_symbols;
 | 
			
		||||
 | 
			
		||||
struct squeek_symbol* squeek_symbol_new(const char *element_name,
 | 
			
		||||
                                        const char *text, uint32_t keyval,
 | 
			
		||||
@ -19,4 +20,11 @@ const char *squeek_symbol_get_icon_name(struct squeek_symbol* symbol);
 | 
			
		||||
uint32_t squeek_symbol_get_modifier_mask(struct squeek_symbol* symbol);
 | 
			
		||||
 | 
			
		||||
void squeek_symbol_print(struct squeek_symbol* symbol);
 | 
			
		||||
 | 
			
		||||
struct squeek_symbols* squeek_symbols_new();
 | 
			
		||||
void squeek_symbols_free(struct squeek_symbols*);
 | 
			
		||||
void squeek_symbols_append(struct squeek_symbols*, struct squeek_symbol *item);
 | 
			
		||||
struct squeek_symbol *squeek_symbols_get(struct squeek_symbols*, uint32_t level);
 | 
			
		||||
 | 
			
		||||
const char* squeek_key_to_keymap_entry(const char *key_name, struct squeek_symbols *symbols);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
@ -209,6 +209,85 @@ pub mod c {
 | 
			
		||||
        let symbol = unsafe { &*symbol };
 | 
			
		||||
        println!("{:?}", symbol);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    #[no_mangle]
 | 
			
		||||
    pub extern "C"
 | 
			
		||||
    fn squeek_symbols_new() -> *const Vec<*const Symbol> {
 | 
			
		||||
        Box::into_raw(Box::new(Vec::new()))
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    #[no_mangle]
 | 
			
		||||
    pub extern "C"
 | 
			
		||||
    fn squeek_symbols_append(v: *mut Vec<*const Symbol>, symbol: *const Symbol) {
 | 
			
		||||
        let v = unsafe { &mut *v };
 | 
			
		||||
        v.push(symbol);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    #[no_mangle]
 | 
			
		||||
    pub extern "C"
 | 
			
		||||
    fn squeek_symbols_get(v: *mut Vec<*const Symbol>, index: u32) -> *const Symbol {
 | 
			
		||||
        let v = unsafe { &mut *v };
 | 
			
		||||
        let index = index as usize;
 | 
			
		||||
        v[
 | 
			
		||||
            if index < v.len() { index } else { 0 }
 | 
			
		||||
        ]
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    #[no_mangle]
 | 
			
		||||
    pub extern "C"
 | 
			
		||||
    fn squeek_symbols_free(symbols: *mut Vec<*const Symbol>) {
 | 
			
		||||
        unsafe { Box::from_raw(symbols) }; // Will just get dropped, together with the contents
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    #[no_mangle]
 | 
			
		||||
    pub extern "C"
 | 
			
		||||
    fn squeek_key_to_keymap_entry(
 | 
			
		||||
        key_name: *const c_char,
 | 
			
		||||
        symbols: *const Vec<*const Symbol>,
 | 
			
		||||
    ) -> *const c_char {
 | 
			
		||||
        let key_name = as_cstr(&key_name)
 | 
			
		||||
            .expect("Missing key name")
 | 
			
		||||
            .to_str()
 | 
			
		||||
            .expect("Bad key name");
 | 
			
		||||
        let symbols = unsafe { &*symbols };
 | 
			
		||||
        let symbol_names = symbols.iter()
 | 
			
		||||
            .map(|symbol| {
 | 
			
		||||
                let symbol: &Symbol = unsafe { &**symbol };
 | 
			
		||||
                match &symbol.action {
 | 
			
		||||
                    Action::Submit { text: Some(text), .. } => {
 | 
			
		||||
                        Some(
 | 
			
		||||
                            text.clone()
 | 
			
		||||
                                .into_string().expect("Bad symbol")
 | 
			
		||||
                        )
 | 
			
		||||
                    },
 | 
			
		||||
                    _ => None
 | 
			
		||||
                }
 | 
			
		||||
            })
 | 
			
		||||
            .collect::<Vec<_>>();
 | 
			
		||||
 | 
			
		||||
        let inner = match symbol_names.len() {
 | 
			
		||||
            1 => match &symbol_names[0] {
 | 
			
		||||
                Some(name) => format!("[ {} ]", name),
 | 
			
		||||
                _ => format!("[ ]"),
 | 
			
		||||
            },
 | 
			
		||||
            4 => {
 | 
			
		||||
                let first = match (&symbol_names[0], &symbol_names[1]) {
 | 
			
		||||
                    (Some(left), Some(right)) => format!("{}, {}", left, right),
 | 
			
		||||
                    _ => format!(""),
 | 
			
		||||
                };
 | 
			
		||||
                let second = match (&symbol_names[2], &symbol_names[3]) {
 | 
			
		||||
                    (Some(left), Some(right)) => format!("{}, {}", left, right),
 | 
			
		||||
                    _ => format!(""),
 | 
			
		||||
                };
 | 
			
		||||
                format!("[ {} ], [ {} ]", first, second)
 | 
			
		||||
            },
 | 
			
		||||
            _ => panic!("Unsupported number of symbols"),
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        CString::new(format!("        key <{}> {{ {} }};\n", key_name, inner))
 | 
			
		||||
            .expect("Couldn't convert string")
 | 
			
		||||
            .into_raw()
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Just defines some int->identifier mappings for convenience
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user