Skip to content

Commit 75ccccf

Browse files
committed
It's working
1 parent 410bb2f commit 75ccccf

15 files changed

+180
-2
lines changed

.gitignore

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,11 @@ Cargo.lock
1818
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
1919
# and can be added to the global gitignore or merged into this file. For a more nuclear
2020
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
21-
#.idea/
21+
#.idea/
22+
23+
*.dll
24+
*.lib
25+
*.so
26+
*.so.*
27+
main
28+
.idea

Cargo.toml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[package]
2+
name = "xframes-rust"
3+
version = "0.1.0"
4+
authors = ["Andrea Mancuso <[email protected]>"]
5+
edition = "2021"
6+
7+
[dependencies]
8+
serde = { version = "1.0", features = ["derive"] }
9+
serde_json = "1.0"
10+
libc = "0.2" # If you haven't added libc already
11+
12+
[profile.dev]
13+
opt-level = 0
14+
debug = true
15+
16+
[profile.release]
17+
opt-level = 3 # Maximum optimization for performance
18+
debug = false
19+
lto = true
20+
panic = "abort"
21+
22+
[[bin]]
23+
name = "main"
24+
path = "main.rs"

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
# xframes-rust
1+
# xframes-rust
2+
3+
rustc main.rs -l xframesshared -L .

assets/.gitignore

Whitespace-only changes.
Loading

assets/bitcoin-btc-logo_gqud0f.png

379 Bytes
Loading

assets/fonts/fa-regular-400.ttf

66.3 KB
Binary file not shown.

assets/fonts/fa-solid-900.ttf

410 KB
Binary file not shown.
1.25 MB
Binary file not shown.

assets/fonts/roboto-bold.ttf

163 KB
Binary file not shown.

assets/fonts/roboto-light.ttf

163 KB
Binary file not shown.

assets/fonts/roboto-mono-regular.ttf

22.6 KB
Binary file not shown.

assets/fonts/roboto-regular.ttf

164 KB
Binary file not shown.

build.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn main() {
2+
println!("cargo:rustc-link-lib=dylib=xframesshared");
3+
println!("cargo:rustc-link-search=native=./");
4+
}

main.rs

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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

Comments
 (0)