Skip to content

Commit

Permalink
Unfinished work. Transferring to desktop.
Browse files Browse the repository at this point in the history
  • Loading branch information
judemille committed Nov 3, 2023
1 parent 0931739 commit 0702ac3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 35 deletions.
55 changes: 21 additions & 34 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bindgen::callbacks::ParseCallbacks;
use bindgen::callbacks::{ParseCallbacks, IntKind};
use bindgen::EnumVariation;
use std::path::{Path, PathBuf};
use std::{env, str::FromStr};
Expand All @@ -8,12 +8,9 @@ fn get_clang_args(crate_path: &Path) -> Vec<String> {
let mut r = Vec::new();
r.push("-DLIN".to_string()); // Technically tells the headers they're being compiled for Linux.
// Doesn't matter for our use case -- the only things that are changed are irrelevant to bindgen.
if cfg!(not(any(feature = "XPLM200", feature = "OLD"))) {
if cfg!(not(feature = "XPLM200")) {
panic!("Please set a desired SDK version!");
}
if cfg!(all(feature = "XPLM200", feature = "OLD")) {
panic!("Using both normal version features and the OLD feature is paradoxical. Pick one.");
}
if cfg!(feature = "XPLM400") {
r.push("-DXPLM400".to_string());
}
Expand Down Expand Up @@ -92,8 +89,15 @@ fn handle_platform(crate_path: PathBuf) {
}

#[derive(Debug)]
struct EnumHandler;
impl ParseCallbacks for EnumHandler {
struct NamingHandler;
impl ParseCallbacks for NamingHandler {
fn int_macro(&self, name: &str, _value: i64) -> Option<IntKind> {
if name.starts_with("XPLM_VK") || name.starts_with("XPLM_KEY") {
Some(IntKind::U32)
} else {
None
}
}
fn enum_variant_name(
&self,
enum_name: Option<&str>,
Expand All @@ -120,6 +124,7 @@ impl ParseCallbacks for EnumHandler {
'3' => "Center",
_ => unreachable!(),
};
out = out.trim_start_matches("device_");
return Some(out[0..out.len() - 1].to_string() + side);
} else {
out
Expand Down Expand Up @@ -164,7 +169,7 @@ impl ParseCallbacks for EnumHandler {
out = out.trim_start_matches("Text");
} else if enum_name == "XPButtonBehavior" {
out = out.trim_start_matches("ButtonBehavior");
}
}
out.trim_start_matches('_')
} else {
out
Expand All @@ -181,13 +186,18 @@ fn main() {
let crate_path = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
let bindings = bindgen::Builder::default()
.header("src/combined.h")
.default_enum_style(EnumVariation::Rust {
non_exhaustive: true,
.default_enum_style(EnumVariation::NewType {
is_bitfield: false,
is_global: false,
})
.bitfield_enum("XPLMDataTypeID")
.bitfield_enum("XPLMKeyFlags")
.bitfield_enum("XPLMNavType")
.use_core()
.parse_callbacks(Box::new(EnumHandler))
.prepend_enum_name(false)
.parse_callbacks(Box::new(NamingHandler))
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.default_macro_constant_type(bindgen::MacroTypeVariation::Signed)
.clang_args(get_clang_args(&crate_path))
.generate()
.expect("Unable to generate bindings!");
Expand All @@ -198,27 +208,4 @@ fn main() {
.expect("Couldn't write bindings!");

handle_platform(crate_path);
/* if target.contains("-apple-") {
let library_path = crate_path.join("SDK/Libraries/Mac");
println!(
"cargo:rustc-link-search=framework={}",
library_path.to_str().unwrap()
);
println!("cargo:rustc-link-lib=framework=XPLM");
println!("cargo:rustc-link-lib=framework=XPWidgets");
} else if target.contains("-linux-") {
// Do nothing for Linux
} else if target.contains("-windows-") {
let library_path = crate_path.join("SDK/Libraries/Win");
println!("cargo:rustc-link-search={}", library_path.to_str().unwrap());
if target.contains("x86_64") {
println!("cargo:rustc-link-lib=XPLM_64");
println!("cargo:rustc-link-lib=XPWidgets_64");
} else {
println!("cargo:rustc-link-lib=XPLM");
println!("cargo:rustc-link-lib=XPWidgets");
}
} else {
panic!("Target operating system not Mac OS, Linux, or Windows. As of the writing of this version of this crate, X-Plane does not support any other platform.")
} */
}
28 changes: 27 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2015 xplm-sys developers
// Copyright (c) 2023 xplm-sys developers
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT
Expand All @@ -11,3 +11,29 @@
#![allow(non_upper_case_globals, non_camel_case_types, non_snake_case)]

include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

macro_rules! bitfield_impls {
($t:ty) => {
impl $t {
#[inline]
pub fn field_true(self, field: Self) -> bool {
self & field == field
}

#[inline]
pub fn field_false(self, field: Self) -> bool {
!self.field_true(field)
}
}
impl std::fmt::Binary for $t {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let val = self.0;
std::fmt::Binary::fmt(&val, f) // Delegate to interior.
}
}
};
}

bitfield_impls!(XPLMDataTypeID);
bitfield_impls!(XPLMKeyFlags);
bitfield_impls!(XPLMNavType);

0 comments on commit 0702ac3

Please sign in to comment.