https://github.com/x9void/waylandsharp
C# Source Generator for Wayland
https://github.com/x9void/waylandsharp
csharp source-generator wayland
Last synced: 6 months ago
JSON representation
C# Source Generator for Wayland
- Host: GitHub
- URL: https://github.com/x9void/waylandsharp
- Owner: X9VoiD
- License: mit
- Created: 2022-06-20T21:44:51.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-11-22T14:57:57.000Z (11 months ago)
- Last Synced: 2025-03-27T23:06:10.769Z (7 months ago)
- Topics: csharp, source-generator, wayland
- Language: C#
- Homepage:
- Size: 129 KB
- Stars: 22
- Watchers: 3
- Forks: 9
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# WaylandSharp
An incremental source generator to automatically create bindings to Wayland
using given protocol xml files.
> `wayland.xml` should always be included to the list.## Compatibility
| .NET Version | Compatibility |
| ------------ | ------------- |
| .NET | (6.0 and above) :heavy_check_mark: |
| .NET Standard | :x: |
| .NET Framework | :x: |## Getting started
Install WaylandSharp nuget package.
```sh
dotnet add package WaylandSharp
```Grab `wayland.xml` from [freedesktop.org](https://gitlab.freedesktop.org/wayland/wayland/-/blob/main/protocol/wayland.xml). Drop the file into your project.
Add the following to your `.csproj`:
```xml
```
> No support for generating server-side bindings yet.
Benefit! :bread:
## Quick Guide
Connection to a wayland display server can be established by calling:
```cs
WlDisplay.Connect(string);
```Global objects can be retrieved by creating a registry object and listening for
`Global` event
```cs
using wlDisplay = WlDisplay.Connect();
using wlRegistry = wlDisplay.GetRegistry();wlRegistry.Global += (_, e) =>
{
Console.WriteLine($"{e.Name}:{e.Interface}:{e.Version}");
};wlDisplay.Roundtrip();
```Either a `WlDisplay.Roundtrip()` or `WlDisplay.Dispatch()` is required to
generate event invocations. In this case, `Global` event will occur upon calling
`Roundtrip()`.As described in Wayland's [official docs](https://wayland.freedesktop.org/docs/html/apb.html#Client-classwl__display),
`WlDisplay.Roundtrip()` dispatches all currently pending events. If no events
are pending, the function returns 0, otherwise it returns the number of pending
events that were processed. This internally calls `Sync` and waits for the
server's callback before returning.On the other hand, `Dispatch` will block until there are events to process,
as such, it will never return 0. It is useful for setting up an event loop, like
in this example below.```cs
while (someWlDisplayInstance.Dispatch() != -1)
{
// intentionally empty
}
```Binding to global objects are done by using the data received from
`GlobalEventArgs`, specifically `Name` (a unique `uint` given by the server
for this instance of global object), `Interface` (the contract used), and
`Version` and passing it as the arguments of `WlRegistry.Bind()`.As an example, assuming that the user wants to bind to a `wl_output`
```cs
wlRegistry.Global += (_, e) =>
{
if (e.Interface == WlInterface.WlOutput.Name)
{
// Passing a version is optional, it'll use the version specified in
// the protocol xml by default.
using var wlOutput = wlRegistry.Bind(e.Name, e.Interface);// do something about wlOutput here.
}
};
```> A helper function will be introduced in the future to help shorten this
specific pattern.