|
| 1 | +use serde_json::json; |
| 2 | +use std::ffi::CStr; |
| 3 | +use std::io::{self, Write}; |
| 4 | +use std::ffi::CString; |
| 5 | +use std::os::raw::{c_char, c_float, c_int}; |
| 6 | + |
| 7 | +pub type OnInitCb = extern "C" fn(); |
| 8 | +pub type OnTextChangedCb = extern "C" fn(id: c_int, text: *const c_char); |
| 9 | +pub type OnComboChangedCb = extern "C" fn(id: c_int, index: c_int); |
| 10 | +pub type OnNumericValueChangedCb = extern "C" fn(id: c_int, value: c_float); |
| 11 | +pub type OnBooleanValueChangedCb = extern "C" fn(id: c_int, value: bool); |
| 12 | +pub type OnMultipleNumericValuesChangedCb = |
| 13 | +extern "C" fn(id: c_int, values: *const c_float, num_values: c_int); |
| 14 | +pub type OnClickCb = extern "C" fn(id: c_int); |
| 15 | + |
| 16 | + |
| 17 | +#[link(name = "xframesshared")] |
| 18 | +extern "C" { |
| 19 | + pub fn init( |
| 20 | + assets_base_path: *const c_char, |
| 21 | + raw_font_definitions: *const c_char, |
| 22 | + raw_style_override_definitions: *const c_char, |
| 23 | + on_init: OnInitCb, |
| 24 | + on_text_changed: OnTextChangedCb, |
| 25 | + on_combo_changed: OnComboChangedCb, |
| 26 | + on_numeric_value_changed: OnNumericValueChangedCb, |
| 27 | + on_boolean_value_changed: OnBooleanValueChangedCb, |
| 28 | + on_multiple_numeric_values_changed: OnMultipleNumericValuesChangedCb, |
| 29 | + on_click: OnClickCb, |
| 30 | + ); |
| 31 | + |
| 32 | + fn setElement(element_json: *const libc::c_char); |
| 33 | + fn setChildren(id: libc::c_int, children_ids: *const libc::c_char); |
| 34 | +} |
| 35 | + |
| 36 | +extern "C" fn on_init_callback() { |
| 37 | + println!("Init callback called"); |
| 38 | + |
| 39 | + // For some reason `0` as integer isn't serialized / passed correctly. Same as in Free Pascal on x64-linux |
| 40 | + let root_node = json!({ |
| 41 | + "id": 0f64, |
| 42 | + "type": "node", |
| 43 | + "root": true |
| 44 | + }); |
| 45 | + |
| 46 | + let unformatted_text = json!({ |
| 47 | + "id": 1, |
| 48 | + "type": "unformatted-text", |
| 49 | + "text": "Hello, world" |
| 50 | + }); |
| 51 | + |
| 52 | + let root_node_json = root_node.to_string(); |
| 53 | + let unformatted_text_json = unformatted_text.to_string(); |
| 54 | + |
| 55 | + let root_node_json_cstring = CString::new(root_node_json).expect("CString::new failed"); |
| 56 | + let unformatted_text_json_cstring = CString::new(unformatted_text_json).expect("CString::new failed"); |
| 57 | + |
| 58 | + let children = json!([1]); |
| 59 | + let children_json = children.to_string(); |
| 60 | + let children_json_cstring = CString::new(children_json).expect("CString::new failed"); |
| 61 | + |
| 62 | + unsafe { |
| 63 | + setElement(root_node_json_cstring.as_ptr()); |
| 64 | + setElement(unformatted_text_json_cstring.as_ptr()); |
| 65 | + |
| 66 | + setChildren(0, children_json_cstring.as_ptr()); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +extern "C" fn on_text_changed_callback(id: c_int, text: *const c_char) { |
| 71 | + unsafe { |
| 72 | + let text_str = CStr::from_ptr(text).to_string_lossy(); |
| 73 | + println!("Text changed (id: {}, text: {})", id, text_str); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +extern "C" fn on_combo_changed_callback(id: c_int, index: c_int) { |
| 78 | + println!("Combo changed (id: {}, index: {})", id, index); |
| 79 | +} |
| 80 | + |
| 81 | +extern "C" fn on_numeric_value_changed_callback(id: c_int, value: c_float) { |
| 82 | + println!("Numeric value changed (id: {}, value: {})", id, value); |
| 83 | +} |
| 84 | + |
| 85 | +extern "C" fn on_boolean_value_changed_callback(id: c_int, value: bool) { |
| 86 | + println!("Boolean value changed (id: {}, value: {})", id, value); |
| 87 | +} |
| 88 | + |
| 89 | +extern "C" fn on_multiple_numeric_values_changed_callback( |
| 90 | + id: c_int, |
| 91 | + values: *const c_float, |
| 92 | + num_values: c_int, |
| 93 | +) { |
| 94 | + unsafe { |
| 95 | + let values_slice = std::slice::from_raw_parts(values, num_values as usize); |
| 96 | + println!("Multiple numeric values changed (id: {}, values: {:?})", id, values_slice); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +extern "C" fn on_click_callback(id: c_int) { |
| 101 | + println!("Click callback called (id: {})", id); |
| 102 | +} |
| 103 | + |
| 104 | +fn main() { |
| 105 | + unsafe { |
| 106 | + let font_defs = json!({ |
| 107 | + "defs": [{ |
| 108 | + "name": "roboto-regular", |
| 109 | + "size": 16 |
| 110 | + }] |
| 111 | + }); |
| 112 | + |
| 113 | + let font_defs_json = font_defs.to_string(); |
| 114 | + |
| 115 | + // Prepare strings |
| 116 | + let assets_base_path = CString::new("./assets").unwrap(); |
| 117 | + let raw_font_definitions = CString::new(font_defs_json).unwrap(); |
| 118 | + let raw_style_override_definitions = CString::new("{}").unwrap(); |
| 119 | + |
| 120 | + // Call init |
| 121 | + init( |
| 122 | + assets_base_path.as_ptr(), |
| 123 | + raw_font_definitions.as_ptr(), |
| 124 | + raw_style_override_definitions.as_ptr(), |
| 125 | + on_init_callback, |
| 126 | + on_text_changed_callback, |
| 127 | + on_combo_changed_callback, |
| 128 | + on_numeric_value_changed_callback, |
| 129 | + on_boolean_value_changed_callback, |
| 130 | + on_multiple_numeric_values_changed_callback, |
| 131 | + on_click_callback, |
| 132 | + ); |
| 133 | + |
| 134 | + println!("Press Enter to exit..."); |
| 135 | + let mut buffer = String::new(); |
| 136 | + io::stdout().flush().unwrap(); // Ensure the prompt is shown |
| 137 | + io::stdin().read_line(&mut buffer).unwrap(); // Wait for user input |
| 138 | + println!("Exiting..."); |
| 139 | + |
| 140 | + } |
| 141 | +} |
0 commit comments