# C API: The context

`sona_config` holds everything `sona_open` needs:

| Field | Meaning |
|---|---|
| `abi` | `SONA_ABI` |
| `app_id` | NULL: the app id stamped into this build. Release builds refuse an id that differs from the stamp |
| `licence`, `licence_len` | `sona-licence.json` from the download, as bytes. The WASM build has none |
| `sources[SONA_MAX_SOURCES]`, `source_count` | Asked in order; the first that has an asset wins |
| `http` | NULL: `sona_http_platform()` |
| `allocator` | All zero: `malloc` and `free` |
| `budget` | See `sona_budget` |
| `worker_threads` | -1: cores - 1 (the default); 0: none, work runs inside `sona_update` |
| `jobs` | When `.run` is set it replaces `worker_threads` |
| `seed` | Idle and start-time randomness; 0: from the clock. A fixed seed makes runs repeatable |
| `max_avatars` | Default 256, at most 16384 |
| `build_budget_ms` | Building allowed per `sona_update`; default 6 |
| `face_size` | 0: auto (512 up to 4 avatars, 256 up to 32, else 128); or 128, 256, 512 |
| `flags` | `SONA_CFG_*`, below |
| `log`, `log_user` | `log(user, level, msg)` |
| `check_host` | NULL: `https://test.360sona.com`, the developer site, where the native check answers. Release builds accept only an `https://` host, and only a signed answer from 360sona opens the gate, so a proxy works but cannot stand in for the server |

Flags:

- `SONA_CFG_STILL_FACES`: one face per avatar (the web's `faces: "still"`).
- `SONA_CFG_APP_BEHAVIOUR`: the app-faithful idle player with crossfades, instead of the web SDK's idle chooser.
- `SONA_CFG_NO_CPU_SKIN`: never allocate CPU-skinned vertex buffers.

## sona_config_defaults

```c
void sona_config_defaults(sona_config *cfg);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `cfg` | `sona_config *` | The [`sona_config`](../fields.md#sona_config) to fill with the defaults | required |

**Returns** Nothing.

**Example** (also in [Signed releases, stamped downloads and revocation](../../guides/release.md)):

```c
sona_config_defaults(&cfg);
cfg.licence = licence;          /* the bytes of sona-licence.json */
cfg.licence_len = licence_len;
sona_result r = sona_open(&cfg, &ctx);
```

Sets `abi` and leaves everything else at its default.

## sona_open

```c
sona_result sona_open(const sona_config *cfg, sona_ctx **out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `cfg` | `const sona_config *` | The configuration, a [`sona_config`](../fields.md#sona_config) | required |
| `out` | `sona_ctx **` | Receives the new context | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_UNSUPPORTED`, `SONA_E_LIMIT`, `SONA_E_MEMORY`, `SONA_E_GATE`, `SONA_E_LICENCE`, `SONA_E_PLATFORM` ([what each means](../../troubleshooting.md#error-codes)).

**Example** (also in [Signed releases, stamped downloads and revocation](../../guides/release.md)):

```c
sona_config_defaults(&cfg);
cfg.licence = licence;          /* the bytes of sona-licence.json */
cfg.licence_len = licence_len;
sona_result r = sona_open(&cfg, &ctx);
```

Opens a context. The licence file is checked at once: its signature, its app and build against the ones stamped into this library, and this library's platform in its list. A failure is `SONA_E_LICENCE` or `SONA_E_PLATFORM`, and no source opens.

The native check with 360sona runs in the background, and gate reads wait for it. A failed check arrives as a `SONA_EVENT_ERROR` and turns the gate source off; a passed one as `SONA_EVENT_LICENCE_OK`. Over the daily use (`SONA_E_LIMIT`), the gate source stays off until the next UTC day. Local sources (dir and console) need only the licence file, work offline and are never counted: only reads that reach the gate count toward its caps (3,000 distinct items and 20,000 answers per ctx). If no source can give the avatar rig (`avatar-rig.json`), the ctx raises one `SONA_EVENT_ERROR` with `SONA_E_GATE` and its avatars fail with the same code.

## sona_close

```c
void sona_close(sona_ctx *ctx);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ctx` | `sona_ctx *` | The context | required |

**Returns** Nothing.

**Example** (also in [Getting started](../../getting-started.md)):

```c
sona_avatar_remove(avatar);       /* frees what only it used */
sona_renderer_destroy(renderer);  /* before your device goes */
sona_close(ctx);
```

Closes a context. It calls `cancel` on every read in flight, then each source's `destroy`, and frees everything else. Reads still open are freed by their own completion, whose result is dropped.

## sona_app_id

```c
const char *sona_app_id(const sona_ctx *ctx);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ctx` | `const sona_ctx *` | The context | required |

**Returns** `const char *`.

**Example**:

```c
const char * x_got = sona_app_id(ctx);
printf("sona_app_id: %s\n", x_got ? "a value" : "none");
```

The app id this context runs as.

## sona_version

```c
const char *sona_version(void);
```

**Returns** `const char *`.

**Example** (also in [Installing](../../install.md)):

```c
printf("sona %s on %s\n", sona_version(), sona_platform());
```

The library's version, `"1.1.0"`.

## sona_platform

```c
const char *sona_platform(void);
```

**Returns** `const char *`.

**Example** (also in [Installing](../../install.md)):

```c
printf("sona %s on %s\n", sona_version(), sona_platform());
```

The binary platform id compiled into this library: `"windows-x64"`, `"linux-x64"`, `"linux-arm64"`, `"macos-arm64"`, `"macos-x64"`, `"ios-arm64"`, `"android-arm64"`, `"android-x64"`, `"xbox360"` or `"wasm"`. The licence file lists these ids. They are not the download names (`macos`, `unity`, ...): one download can hold several.

## sona_struct_size

```c
uint32_t sona_struct_size(const char *name);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `name` | `const char *` | A public struct's name, such as `"sona_config"` | required |

**Returns** `uint32_t`.

**Example**:

```c
uint32_t x_got = sona_struct_size("sona_config");
printf("sona_struct_size: %lld\n", (long long)x_got);
```

`sizeof` a public struct by its name (`"sona_config"`, `"sona_pose_data"`, ...); 0 if unknown.

## sona_struct_offset

```c
uint32_t sona_struct_offset(const char *struct_dot_field);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `struct_dot_field` | `const char *` | `"struct.field"` | required |

**Returns** `uint32_t`.

**Example**:

```c
uint32_t x_got = sona_struct_offset("example");
printf("sona_struct_offset: %lld\n", (long long)x_got);
```

`offsetof` a field, by `"struct.field"`: `"sona_manifest.xuid"`, `"sona_device_desc.u.vk.queue_family"`; `UINT32_MAX` if unknown. Bindings check the size of every struct and the offset of every field they mirror when they load, and refuse to run on a mismatch.

## sona_abi_query

```c
sona_result sona_abi_query(sona_abi_info *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `out` | `sona_abi_info *` | Receives the [`sona_abi_info`](../fields.md#sona_abi_info) | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT` ([what each means](../../troubleshooting.md#error-codes)).

**Example** (also in [The stable ABI and the deprecation policy](../../guides/abi.md)):

```c
sona_abi_info i = { 0 };
i.size = sizeof i;
if (sona_abi_query(&i) == SONA_OK && SONA_ABI >= i.oldest_abi && SONA_ABI <= i.abi) {
  /* this program, built against SONA_ABI, can use the library */
}
```

The ABI of the library that is loaded, for hosts and bindings that load it at run time: `abi` is its `SONA_ABI`, `oldest_abi` the oldest `SONA_ABI` a caller may be built against and still load it (nothing breaks for two minor versions), and its version as numbers. Set `out->size` first.
