From ad86cb531a7b421257cf737e112395a37276de3f Mon Sep 17 00:00:00 2001 From: Dorota Czaplejewicz Date: Sat, 3 Aug 2019 09:29:03 +0000 Subject: [PATCH] symbol: Store symbols instead of pointers --- eek/eek-keyboard.c | 2 +- eek/eek-xml-layout.c | 4 +- src/symbol.h | 10 ++--- src/symbol.rs | 93 +++++++++++++++++++++----------------------- 4 files changed, 53 insertions(+), 56 deletions(-) diff --git a/eek/eek-keyboard.c b/eek/eek-keyboard.c index 20c68ed1..313c37f5 100644 --- a/eek/eek-keyboard.c +++ b/eek/eek-keyboard.c @@ -761,7 +761,7 @@ eek_keyboard_get_keymap(EekKeyboard *keyboard) g_free(current); // FIXME: free - char *key_str = squeek_key_to_keymap_entry( + const char *key_str = squeek_key_to_keymap_entry( (char*)key_name, eek_key_get_symbol_matrix(key) ); diff --git a/eek/eek-xml-layout.c b/eek/eek-xml-layout.c index 1d6a194e..74c327f5 100644 --- a/eek/eek-xml-layout.c +++ b/eek/eek-xml-layout.c @@ -748,7 +748,8 @@ symbols_end_element_callback (GMarkupParseContext *pcontext, g_strcmp0 (element_name, "keysym") == 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, text, data->keyval, @@ -757,7 +758,6 @@ symbols_end_element_callback (GMarkupParseContext *pcontext, data->tooltip ); - squeek_symbols_append(eek_key_get_symbol_matrix(data->key), symbol); data->keyval = 0; g_free(data->label); data->label = NULL; diff --git a/src/symbol.h b/src/symbol.h index 9cdbcb32..98eca502 100644 --- a/src/symbol.h +++ b/src/symbol.h @@ -8,10 +8,11 @@ struct squeek_symbol; struct squeek_symbols; -struct squeek_symbol* squeek_symbol_new(const char *element_name, - const char *text, uint32_t keyval, - const char *label, const char *icon, - const char *tooltip); +void squeek_symbols_add(struct squeek_symbols*, + const char *element_name, + const char *text, uint32_t keyval, + const char *label, const char *icon, + const char *tooltip); 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(); 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); diff --git a/src/symbol.rs b/src/symbol.rs index 6b73d120..a57335a3 100644 --- a/src/symbol.rs +++ b/src/symbol.rs @@ -68,18 +68,22 @@ pub mod c { 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, // so it should handle garbled strings in the future #[no_mangle] pub extern "C" - fn squeek_symbol_new( + fn squeek_symbols_add( + v: *mut Vec, element: *const c_char, text_raw: *const c_char, keyval: u32, label: *const c_char, icon: *const c_char, tooltip: *const c_char, - ) -> *mut Symbol { + ) { let element = as_cstr(&element) .expect("Missing element name"); @@ -123,40 +127,41 @@ pub mod c { eprintln!("Tooltip unreadable: {}", e); None }); - - Box::::into_raw(Box::new( - match element.to_bytes() { - b"symbol" => Symbol { - action: Action::Submit { - text: text, - keys: Vec::new(), + + let symbol = match element.to_bytes() { + b"symbol" => Symbol { + action: Action::Submit { + text: text, + 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, 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, - tooltip: tooltip, - } - }, - _ => panic!("unsupported element type {:?}", element), - } - )) + } + }, + _ => panic!("unsupported element type {:?}", element), + }; + + let v = unsafe { &mut *v }; + v.push(symbol); } #[no_mangle] @@ -212,30 +217,23 @@ pub mod c { #[no_mangle] pub extern "C" - fn squeek_symbols_new() -> *const Vec<*const Symbol> { + fn squeek_symbols_new() -> *const Vec { 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 { + fn squeek_symbols_get(v: *mut Vec, index: u32) -> *const Symbol { let v = unsafe { &mut *v }; let index = index as usize; - v[ + &v[ if index < v.len() { index } else { 0 } - ] + ] as *const Symbol } #[no_mangle] pub extern "C" - fn squeek_symbols_free(symbols: *mut Vec<*const Symbol>) { + fn squeek_symbols_free(symbols: *mut Vec) { unsafe { Box::from_raw(symbols) }; // Will just get dropped, together with the contents } @@ -243,7 +241,7 @@ pub mod c { pub extern "C" fn squeek_key_to_keymap_entry( key_name: *const c_char, - symbols: *const Vec<*const Symbol>, + symbols: *const Vec, ) -> *const c_char { let key_name = as_cstr(&key_name) .expect("Missing key name") @@ -252,7 +250,6 @@ pub mod c { 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(