Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: C API #30

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 119 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ serde = { version = "1.0", features = ["derive"], optional = true }
napi-build = { version = "2.1", optional = true }
# glue (python)
pyo3-build-config = { version = "0.22", optional = true }
# glue (C)
cbindgen = { version = "0.24", optional = true }

[features]
default = []
Expand All @@ -73,6 +75,7 @@ java = ["lazy_static", "jni", "tracing-subscriber", "jni-toolbox"]
js = ["napi-build", "tracing-subscriber", "napi", "napi-derive"]
py-noabi = ["pyo3", "tracing-subscriber", "pyo3-build-config"]
py = ["py-noabi", "pyo3/abi3-py38"]
c = ["cbindgen"]
lua = ["mlua-codemp-patch", "tracing-subscriber", "lazy_static", "serialize"]
lua54 =["lua", "mlua-codemp-patch/lua54"]
luajit = ["lua", "mlua-codemp-patch/luajit"]
Expand Down
16 changes: 14 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ extern crate napi_build;
#[cfg(any(feature = "py", feature = "py-noabi"))]
extern crate pyo3_build_config;

#[cfg(feature = "c")]
extern crate cbindgen;

/// The main method of the buildscript, required by some glue modules.
fn main() {
#[cfg(feature = "js")]
Expand All @@ -20,7 +23,16 @@ fn main() {
{
if let Ok("macos") = std::env::var("CARGO_CFG_TARGET_OS").as_deref() {
println!("cargo:rustc-cdylib-link-arg=-undefined");
println!("cargo:rustc-cdylib-link-arg=dynamic_lookup");
}
println!("cargo:rustc-cdylib-link-arg=dynamic_lookup");
}
}

#[cfg(feature = "c")]
{
cbindgen::Builder::new()
.with_crate(std::env::var("CARGO_MANIFEST_DIR").unwrap())
.generate()
.expect("Unable to generate bindings")
.write_to_file("dist/c/codemp.h");
}
}
28 changes: 28 additions & 0 deletions dist/c/codemp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <cstdarg>
#include <cstdint>
#include <cstdlib>
#include <ostream>
#include <new>

/// A `codemp` client handle.
///
/// It generates a new UUID and stores user credentials upon connecting.
///
/// A new [`Client`] can be obtained with [`Client::connect`].
struct Client;

/// A currently active shared development environment
///
/// Workspaces encapsulate a working environment: cursor positions, filetree, user list
/// and more. Each holds a [cursor::Controller] and a map of [buffer::Controller]s.
/// Using a workspace handle, it's possible to receive events (user join/leave, filetree updates)
/// and create/delete/attach to new buffers.
struct Workspace;

extern "C" {

Client *Codemp_Client_connect();

Workspace *Codemp_Client_join_workspace(Client *client, char *workspace);

} // extern "C"
45 changes: 45 additions & 0 deletions src/ffi/c/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use std::ffi::{c_char, CString};

use crate::{api::Config, Client, Workspace};

pub(crate) fn tokio() -> &'static tokio::runtime::Runtime {
use std::sync::OnceLock;
static RT: OnceLock<tokio::runtime::Runtime> = OnceLock::new();
RT.get_or_init(||
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("could not create tokio runtime")
)
}


#[no_mangle] // TODO config
pub extern "C" fn Codemp_Client_connect() -> *mut Client {
match tokio()
.block_on(Client::connect(Config::new("", "")))
{
Ok(c) => Box::into_raw(Box::new(c)),
Err(e) => {
tracing::error!("failed connecting to remote: {e}");
std::ptr::null_mut()
},
}
}

#[no_mangle]
pub unsafe extern "C" fn Codemp_Client_join_workspace(client: *mut Client, workspace: *mut c_char) -> *mut Workspace {
let client = unsafe { Box::leak(Box::from_raw(client)) };
let workspace = unsafe { CString::from_raw(workspace) }.to_string_lossy().to_string();

match tokio()
.block_on(client.join_workspace(workspace))
{
Ok(ws) => Box::into_raw(Box::new(ws)),
Err(e) => {
tracing::error!("failed joining workspace: {e}");
std::ptr::null_mut()
},
}

}
4 changes: 4 additions & 0 deletions src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,7 @@ pub mod js;
/// python bindings, built with [pyo3]
#[cfg(any(feature = "py", feature = "py-noabi"))]
pub mod python;

/// c bindings, generated with [cbindgen]
#[cfg(feature = "c")]
pub mod c;