# C API: XR body module (sona_xr.h)

An optional library, `sona_xr`: a first-person body driven by a headset and two controllers, for OpenXR games. Include `sona_xr.h` and link `sona_xr`.

## sona_xr_defaults

```c
void sona_xr_defaults(sona_xr_options *o);
```

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

**Returns** Nothing.

**Example**:

```c
sona_xr_options x_xr_options;
memset(&x_xr_options, 0, sizeof x_xr_options);
x_xr_options.size = sizeof x_xr_options;
sona_xr_defaults(&x_xr_options);
```

`size` set; `step_distance` 0.25.

## sona_xr_create

```c
sona_result sona_xr_create(const sona_xr_options *o, sona_xr_body **out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `o` | `const sona_xr_options *` | The options, a [`sona_xr_options`](../fields.md#sona_xr_options) | `NULL`: the defaults |
| `out` | `sona_xr_body **` | Receives the new XR body | required |

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

A first-person body for a headset and two controllers. NULL options: the defaults.

## sona_xr_destroy

```c
void sona_xr_destroy(sona_xr_body *xr);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `xr` | `sona_xr_body *` | The XR body | required |

**Returns** Nothing.

Frees it.

## sona_xr_input_defaults

```c
void sona_xr_input_defaults(sona_xr_input *in);
```

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

**Returns** Nothing.

**Example** (also in [VR and XR bodies (sona_xr)](../../guides/xr.md)):

```c
sona_xr_input in;
sona_xr_input_defaults(&in);
in.time = now;                                  /* seconds, rising */
in.flags = SONA_XR_HAS_HEAD;                    /* and _HAS_LEFT, _HAS_RIGHT for the grips the runtime tracked */
in.head.position[0] = head_pos[0];
in.head.position[1] = head_pos[1];
in.head.position[2] = head_pos[2];
sona_xr_feed(xr, &in);
if (sona_avatar_state_of(avatar) == SONA_AVATAR_READY) sona_xr_apply(xr, avatar);
sona_update(ctx, dt, &view, NULL);
```

`size` set, the three poses at the origin with no turn, the floor at 0.

## sona_xr_feed

```c
sona_result sona_xr_feed(sona_xr_body *xr, const sona_xr_input *in);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `xr` | `sona_xr_body *` | The XR body | required |
| `in` | `const sona_xr_input *` | A [`sona_xr_input`](../fields.md#sona_xr_input) | required |

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

**Example** (also in [VR and XR bodies (sona_xr)](../../guides/xr.md)):

```c
sona_xr_input in;
sona_xr_input_defaults(&in);
in.time = now;                                  /* seconds, rising */
in.flags = SONA_XR_HAS_HEAD;                    /* and _HAS_LEFT, _HAS_RIGHT for the grips the runtime tracked */
in.head.position[0] = head_pos[0];
in.head.position[1] = head_pos[1];
in.head.position[2] = head_pos[2];
sona_xr_feed(xr, &in);
if (sona_avatar_state_of(avatar) == SONA_AVATAR_READY) sona_xr_apply(xr, avatar);
sona_update(ctx, dt, &view, NULL);
```

One frame of the head and the two grip poses (world metres, as `XrPosef`), with its time in seconds and the floor's height.

## sona_xr_calibrate

```c
sona_result sona_xr_calibrate(sona_xr_body *xr);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `xr` | `sona_xr_body *` | The XR body | required |

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

Takes the wearer's height and arm span from the poses fed while they stand straight.

## sona_xr_apply

```c
sona_result sona_xr_apply(sona_xr_body *xr, sona_avatar *a);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `xr` | `sona_xr_body *` | The XR body | required |
| `a` | `sona_avatar *` | The avatar | required |

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

**Example** (also in [VR and XR bodies (sona_xr)](../../guides/xr.md)):

```c
sona_xr_input in;
sona_xr_input_defaults(&in);
in.time = now;                                  /* seconds, rising */
in.flags = SONA_XR_HAS_HEAD;                    /* and _HAS_LEFT, _HAS_RIGHT for the grips the runtime tracked */
in.head.position[0] = head_pos[0];
in.head.position[1] = head_pos[1];
in.head.position[2] = head_pos[2];
sona_xr_feed(xr, &in);
if (sona_avatar_state_of(avatar) == SONA_AVATAR_READY) sona_xr_apply(xr, avatar);
sona_update(ctx, dt, &view, NULL);
```

Puts the body on an avatar, scaled to the wearer. `SONA_E_NOT_READY` until the avatar is READY and posed.

## sona_xr_release

```c
void sona_xr_release(sona_xr_body *xr, sona_avatar *a);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `xr` | `sona_xr_body *` | The XR body | required |
| `a` | `sona_avatar *` | The avatar | required |

**Returns** Nothing.

Hands the avatar's joints back to its clip; nothing when `a` is not the avatar it drives.

## sona_xr_status_get

```c
sona_result sona_xr_status_get(const sona_xr_body *xr, sona_xr_status *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `xr` | `const sona_xr_body *` | The XR body | required |
| `out` | `sona_xr_status *` | Receives the [`sona_xr_status`](../fields.md#sona_xr_status) | required |

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

Fills `out` (a `sona_xr_status`) with what the last `sona_xr_apply` did: the poses tracked, the eye height and scale
in use, the body's turn, placement and crouch, the steps taken and where the avatar's eyes were put. `out->size` is set
by the call. `SONA_E_NOT_READY`, with `out` zeroed, before the body has been applied; `SONA_E_ARGUMENT` for NULL.

## sona_xr_openxr_to_world

```c
void sona_xr_openxr_to_world(const sona_xr_pose *in, sona_xr_pose *out);
```

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

**Returns** Nothing.

**Example**:

```c
sona_xr_pose x_xr_pose;
memset(&x_xr_pose, 0, sizeof x_xr_pose);
sona_xr_pose x_xr_pose2;
memset(&x_xr_pose2, 0, sizeof x_xr_pose2);
sona_xr_openxr_to_world(&x_xr_pose, &x_xr_pose2);
```

Turns an OpenXR pose (a view, a hand, an object; right-handed) into sona's world: z mirrored, then half a turn about
Y, so OpenXR's identity faces -Z as an avatar at turn 0 does. `in` and `out` may be the same. The module does this
itself for poses fed with `SONA_XR_OPENXR_SPACE`; call it to place your own things beside the body. Nothing for NULL.

## sona_xr_struct_size

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

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `name` | `const char *` | A public struct's name | required |

**Returns** `uint32_t`.

**Example**:

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

sizeof a public struct of this header by name, for the bindings' layout checks; 0 when unknown.

## sona_xr_struct_offset

```c
uint32_t sona_xr_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_xr_struct_offset("example");
printf("sona_xr_struct_offset: %lld\n", (long long)x_got);
```

offsetof a field by "struct.field"; `UINT32_MAX` when unknown.
