nandi/cosmicnimpublic Fork 0
v0.1.2
Commits
Clone
git clone https://git.rickub.com/nandi/cosmicnim.git
git clone ssh://git@rickub.com/nandi/cosmicnim.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

README.md · 44 lines · 1.3 KBmarkdown
Blame HistoryOpen raw

cosmicnim

libcosmic behind a small C ABI, driven from Nim.

libcosmic's API is generics, traits and closures, so it has no C ABI of its own —
building libcosmic itself as a cdylib exports nothing. The cosmic_ffi crate
therefore defines the C surface: it depends on libcosmic as a normal Rust
crate and exports one function.

cosmic_ffi/   Rust cdylib -> libcosmic_ffi.so + cosmic_ffi.h
nim/          Nim bindings (cosmic.nim) and the demo app (app.nim)

The contract

Rust owns the widget tree; the host owns the application state. Each button
press is handed back through on_press, which writes the next label into the
buffer it is given.

typedef void (*cosmic_on_press)(void *ctx, int32_t button_id,
                                char *out, size_t out_len);
int32_t cosmic_run(const CosmicConfig *config);   /* blocks until closed */

cosmic_run must be called from the main thread, and ctx is only ever
touched from that thread.

Using a prebuilt release

tar xzf cosmic_ffi-<version>-x86_64-unknown-linux-gnu.tar.gz
cp cosmic_ffi-*/libcosmic_ffi.so nim/
cd nim && nim c -r app.nim

Building from source

cd cosmic_ffi && cargo build --release
cp target/release/libcosmic_ffi.so ../nim/
cd ../nim && nim c -r app.nim
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# cosmicnim

[libcosmic](https://github.com/pop-os/libcosmic) behind a small C ABI, driven from Nim.

libcosmic's API is generics, traits and closures, so it has no C ABI of its own —
building libcosmic itself as a `cdylib` exports nothing. The `cosmic_ffi` crate
therefore *defines* the C surface: it depends on libcosmic as a normal Rust
crate and exports one function.

```
cosmic_ffi/   Rust cdylib -> libcosmic_ffi.so + cosmic_ffi.h
nim/          Nim bindings (cosmic.nim) and the demo app (app.nim)
```

## The contract

Rust owns the widget tree; the host owns the application state. Each button
press is handed back through `on_press`, which writes the next label into the
buffer it is given.

```c
typedef void (*cosmic_on_press)(void *ctx, int32_t button_id,
                                char *out, size_t out_len);
int32_t cosmic_run(const CosmicConfig *config);   /* blocks until closed */
```

`cosmic_run` must be called from the main thread, and `ctx` is only ever
touched from that thread.

## Using a prebuilt release

```bash
tar xzf cosmic_ffi-<version>-x86_64-unknown-linux-gnu.tar.gz
cp cosmic_ffi-*/libcosmic_ffi.so nim/
cd nim && nim c -r app.nim
```

## Building from source

```bash
cd cosmic_ffi && cargo build --release
cp target/release/libcosmic_ffi.so ../nim/
cd ../nim && nim c -r app.nim
```