symbol: Store symbols instead of pointers
This commit is contained in:
		@ -761,7 +761,7 @@ eek_keyboard_get_keymap(EekKeyboard *keyboard)
 | 
				
			|||||||
        g_free(current);
 | 
					        g_free(current);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // FIXME: free
 | 
					        // FIXME: free
 | 
				
			||||||
        char *key_str = squeek_key_to_keymap_entry(
 | 
					        const char *key_str = squeek_key_to_keymap_entry(
 | 
				
			||||||
            (char*)key_name,
 | 
					            (char*)key_name,
 | 
				
			||||||
            eek_key_get_symbol_matrix(key)
 | 
					            eek_key_get_symbol_matrix(key)
 | 
				
			||||||
        );
 | 
					        );
 | 
				
			||||||
 | 
				
			|||||||
@ -748,7 +748,8 @@ symbols_end_element_callback (GMarkupParseContext *pcontext,
 | 
				
			|||||||
        g_strcmp0 (element_name, "keysym") == 0 ||
 | 
					        g_strcmp0 (element_name, "keysym") == 0 ||
 | 
				
			||||||
        g_strcmp0 (element_name, "text") == 0) {
 | 
					        g_strcmp0 (element_name, "text") == 0) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        struct squeek_symbol *symbol = squeek_symbol_new(
 | 
					        squeek_symbols_add(
 | 
				
			||||||
 | 
					            eek_key_get_symbol_matrix(data->key),
 | 
				
			||||||
            element_name,
 | 
					            element_name,
 | 
				
			||||||
            text,
 | 
					            text,
 | 
				
			||||||
            data->keyval,
 | 
					            data->keyval,
 | 
				
			||||||
@ -757,7 +758,6 @@ symbols_end_element_callback (GMarkupParseContext *pcontext,
 | 
				
			|||||||
            data->tooltip
 | 
					            data->tooltip
 | 
				
			||||||
        );
 | 
					        );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        squeek_symbols_append(eek_key_get_symbol_matrix(data->key), symbol);
 | 
					 | 
				
			||||||
        data->keyval = 0;
 | 
					        data->keyval = 0;
 | 
				
			||||||
        g_free(data->label);
 | 
					        g_free(data->label);
 | 
				
			||||||
        data->label = NULL;
 | 
					        data->label = NULL;
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										10
									
								
								src/symbol.h
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								src/symbol.h
									
									
									
									
									
								
							@ -8,10 +8,11 @@
 | 
				
			|||||||
struct squeek_symbol;
 | 
					struct squeek_symbol;
 | 
				
			||||||
struct squeek_symbols;
 | 
					struct squeek_symbols;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct squeek_symbol* squeek_symbol_new(const char *element_name,
 | 
					void squeek_symbols_add(struct squeek_symbols*,
 | 
				
			||||||
                                        const char *text, uint32_t keyval,
 | 
					                        const char *element_name,
 | 
				
			||||||
                                        const char *label, const char *icon,
 | 
					                        const char *text, uint32_t keyval,
 | 
				
			||||||
                                        const char *tooltip);
 | 
					                        const char *label, const char *icon,
 | 
				
			||||||
 | 
					                        const char *tooltip);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const char *squeek_symbol_get_name(struct squeek_symbol* symbol);
 | 
					const char *squeek_symbol_get_name(struct squeek_symbol* symbol);
 | 
				
			||||||
@ -23,7 +24,6 @@ void squeek_symbol_print(struct squeek_symbol* symbol);
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
struct squeek_symbols* squeek_symbols_new();
 | 
					struct squeek_symbols* squeek_symbols_new();
 | 
				
			||||||
void squeek_symbols_free(struct squeek_symbols*);
 | 
					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);
 | 
					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);
 | 
					const char* squeek_key_to_keymap_entry(const char *key_name, struct squeek_symbols *symbols);
 | 
				
			||||||
 | 
				
			|||||||
@ -68,18 +68,22 @@ pub mod c {
 | 
				
			|||||||
        Shift = 1,
 | 
					        Shift = 1,
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    // The following defined in Rust. TODO: wrap naked pointers to Rust data inside RefCells to prevent multiple writers
 | 
					    // The following defined in Rust.
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    // TODO: wrap naked pointers to Rust data inside RefCells to prevent multiple writers
 | 
				
			||||||
 | 
					    // Symbols are owned by Rust and will move towards no C manipulation, so it may make sense not to wrap them
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    // TODO: this will receive data from the filesystem,
 | 
					    // TODO: this will receive data from the filesystem,
 | 
				
			||||||
    // so it should handle garbled strings in the future
 | 
					    // so it should handle garbled strings in the future
 | 
				
			||||||
    #[no_mangle]
 | 
					    #[no_mangle]
 | 
				
			||||||
    pub extern "C"
 | 
					    pub extern "C"
 | 
				
			||||||
    fn squeek_symbol_new(
 | 
					    fn squeek_symbols_add(
 | 
				
			||||||
 | 
					        v: *mut Vec<Symbol>,
 | 
				
			||||||
        element: *const c_char,
 | 
					        element: *const c_char,
 | 
				
			||||||
        text_raw: *const c_char, keyval: u32,
 | 
					        text_raw: *const c_char, keyval: u32,
 | 
				
			||||||
        label: *const c_char, icon: *const c_char,
 | 
					        label: *const c_char, icon: *const c_char,
 | 
				
			||||||
        tooltip: *const c_char,
 | 
					        tooltip: *const c_char,
 | 
				
			||||||
    ) -> *mut Symbol {
 | 
					    ) {
 | 
				
			||||||
        let element = as_cstr(&element)
 | 
					        let element = as_cstr(&element)
 | 
				
			||||||
            .expect("Missing element name");
 | 
					            .expect("Missing element name");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -123,40 +127,41 @@ pub mod c {
 | 
				
			|||||||
                eprintln!("Tooltip unreadable: {}", e);
 | 
					                eprintln!("Tooltip unreadable: {}", e);
 | 
				
			||||||
                None
 | 
					                None
 | 
				
			||||||
            });
 | 
					            });
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
        Box::<Symbol>::into_raw(Box::new(
 | 
					        let symbol = match element.to_bytes() {
 | 
				
			||||||
            match element.to_bytes() {
 | 
					            b"symbol" => Symbol {
 | 
				
			||||||
                b"symbol" => Symbol {
 | 
					                action: Action::Submit {
 | 
				
			||||||
                    action: Action::Submit {
 | 
					                    text: text,
 | 
				
			||||||
                        text: text,
 | 
					                    keys: Vec::new(),
 | 
				
			||||||
                        keys: Vec::new(),
 | 
					                },
 | 
				
			||||||
 | 
					                label: label,
 | 
				
			||||||
 | 
					                tooltip: tooltip,
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            b"keysym" => {
 | 
				
			||||||
 | 
					                let keysym = XKeySym(
 | 
				
			||||||
 | 
					                    if keyval == 0 {
 | 
				
			||||||
 | 
					                        unsafe { eek_keysym_from_name(text_raw) }
 | 
				
			||||||
 | 
					                    } else {
 | 
				
			||||||
 | 
					                        keyval
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                );
 | 
				
			||||||
 | 
					                Symbol {
 | 
				
			||||||
 | 
					                    action: match KeySym::from_u32(keysym.0) {
 | 
				
			||||||
 | 
					                        KeySym::Shift => Action::SetLevel(1),
 | 
				
			||||||
 | 
					                        _ => Action::Submit {
 | 
				
			||||||
 | 
					                            text: text,
 | 
				
			||||||
 | 
					                            keys: vec![keysym],
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
                    },
 | 
					                    },
 | 
				
			||||||
                    label: label,
 | 
					                    label: label,
 | 
				
			||||||
                    tooltip: tooltip,
 | 
					                    tooltip: tooltip,
 | 
				
			||||||
                },
 | 
					                }
 | 
				
			||||||
                b"keysym" => {
 | 
					            },
 | 
				
			||||||
                    let keysym = XKeySym(
 | 
					            _ => panic!("unsupported element type {:?}", element),
 | 
				
			||||||
                        if keyval == 0 {
 | 
					        };
 | 
				
			||||||
                            unsafe { eek_keysym_from_name(text_raw) }
 | 
					        
 | 
				
			||||||
                        } else {
 | 
					        let v = unsafe { &mut *v };
 | 
				
			||||||
                            keyval
 | 
					        v.push(symbol);
 | 
				
			||||||
                        }
 | 
					 | 
				
			||||||
                    );
 | 
					 | 
				
			||||||
                    Symbol {
 | 
					 | 
				
			||||||
                        action: match KeySym::from_u32(keysym.0) {
 | 
					 | 
				
			||||||
                            KeySym::Shift => Action::SetLevel(1),
 | 
					 | 
				
			||||||
                            _ => Action::Submit {
 | 
					 | 
				
			||||||
                                text: text,
 | 
					 | 
				
			||||||
                                keys: vec![keysym],
 | 
					 | 
				
			||||||
                            }
 | 
					 | 
				
			||||||
                        },
 | 
					 | 
				
			||||||
                        label: label,
 | 
					 | 
				
			||||||
                        tooltip: tooltip,
 | 
					 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                },
 | 
					 | 
				
			||||||
                _ => panic!("unsupported element type {:?}", element),
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        ))
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    #[no_mangle]
 | 
					    #[no_mangle]
 | 
				
			||||||
@ -212,30 +217,23 @@ pub mod c {
 | 
				
			|||||||
    
 | 
					    
 | 
				
			||||||
    #[no_mangle]
 | 
					    #[no_mangle]
 | 
				
			||||||
    pub extern "C"
 | 
					    pub extern "C"
 | 
				
			||||||
    fn squeek_symbols_new() -> *const Vec<*const Symbol> {
 | 
					    fn squeek_symbols_new() -> *const Vec<Symbol> {
 | 
				
			||||||
        Box::into_raw(Box::new(Vec::new()))
 | 
					        Box::into_raw(Box::new(Vec::new()))
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    #[no_mangle]
 | 
					    #[no_mangle]
 | 
				
			||||||
    pub extern "C"
 | 
					    pub extern "C"
 | 
				
			||||||
    fn squeek_symbols_append(v: *mut Vec<*const Symbol>, symbol: *const Symbol) {
 | 
					    fn squeek_symbols_get(v: *mut Vec<Symbol>, index: u32) -> *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 v = unsafe { &mut *v };
 | 
				
			||||||
        let index = index as usize;
 | 
					        let index = index as usize;
 | 
				
			||||||
        v[
 | 
					        &v[
 | 
				
			||||||
            if index < v.len() { index } else { 0 }
 | 
					            if index < v.len() { index } else { 0 }
 | 
				
			||||||
        ]
 | 
					        ] as *const Symbol
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    #[no_mangle]
 | 
					    #[no_mangle]
 | 
				
			||||||
    pub extern "C"
 | 
					    pub extern "C"
 | 
				
			||||||
    fn squeek_symbols_free(symbols: *mut Vec<*const Symbol>) {
 | 
					    fn squeek_symbols_free(symbols: *mut Vec<Symbol>) {
 | 
				
			||||||
        unsafe { Box::from_raw(symbols) }; // Will just get dropped, together with the contents
 | 
					        unsafe { Box::from_raw(symbols) }; // Will just get dropped, together with the contents
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
@ -243,7 +241,7 @@ pub mod c {
 | 
				
			|||||||
    pub extern "C"
 | 
					    pub extern "C"
 | 
				
			||||||
    fn squeek_key_to_keymap_entry(
 | 
					    fn squeek_key_to_keymap_entry(
 | 
				
			||||||
        key_name: *const c_char,
 | 
					        key_name: *const c_char,
 | 
				
			||||||
        symbols: *const Vec<*const Symbol>,
 | 
					        symbols: *const Vec<Symbol>,
 | 
				
			||||||
    ) -> *const c_char {
 | 
					    ) -> *const c_char {
 | 
				
			||||||
        let key_name = as_cstr(&key_name)
 | 
					        let key_name = as_cstr(&key_name)
 | 
				
			||||||
            .expect("Missing key name")
 | 
					            .expect("Missing key name")
 | 
				
			||||||
@ -252,7 +250,6 @@ pub mod c {
 | 
				
			|||||||
        let symbols = unsafe { &*symbols };
 | 
					        let symbols = unsafe { &*symbols };
 | 
				
			||||||
        let symbol_names = symbols.iter()
 | 
					        let symbol_names = symbols.iter()
 | 
				
			||||||
            .map(|symbol| {
 | 
					            .map(|symbol| {
 | 
				
			||||||
                let symbol: &Symbol = unsafe { &**symbol };
 | 
					 | 
				
			||||||
                match &symbol.action {
 | 
					                match &symbol.action {
 | 
				
			||||||
                    Action::Submit { text: Some(text), .. } => {
 | 
					                    Action::Submit { text: Some(text), .. } => {
 | 
				
			||||||
                        Some(
 | 
					                        Some(
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user