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

Review compositor tutorial #3606

Merged
merged 10 commits into from
Oct 9, 2024
78 changes: 42 additions & 36 deletions doc/sphinx/tutorial/write-your-first-wayland-compositor.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,50 @@
discourse: 4911,5164,5603,6756,8037
---

# Write Your First Wayland Compositor
This tutorial will guide you through writing a simple compositor: the
installation of dependencies, building it, and running it. You'll be impressed
by how easy it is to create a Wayland compositor using Mir!
# Write your first Wayland compositor
This tutorial will guide you through writing a basic Mir compositor. By the end of it, you will create, build, and run a program with basic window management
capabilities such as controlling multiple windows, minimizing and maximizing, and handling mouse input.

## Assumptions
This tutorial assumes that:
1. The reader is familiar with C++ and CMake.
2. The reader has cmake and a C++ compiler installed

## A barebones Mir compositor
This section will cover the needed dependencies and how to install them, the
minimum code needed for a Mir compositor, how to build this code, and finally
how to run your compositor.
tarek-y-ismail marked this conversation as resolved.
Show resolved Hide resolved
tarek-y-ismail marked this conversation as resolved.
Show resolved Hide resolved

### Dependencies
Before you start coding, you'll need to install `libmiral`, and
`mir-graphics-drivers-desktop` which can be done on different distros as
follows:
## Assumptions

This tutorial assumes that:

* You are familiar with C++ and CMake.
* You have `cmake` and a C++ compiler installed.

## Installing dependencies

tarek-y-ismail marked this conversation as resolved.
Show resolved Hide resolved
The example program requires
* `libmiral`- a library for Mir abstraction layer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions: (looks like I misclicked something when starting the review :))

yhontyk marked this conversation as resolved.
Show resolved Hide resolved
* `mir-graphics-drivers-desktop` - a display server
yhontyk marked this conversation as resolved.
Show resolved Hide resolved

Installing dependencies on Debian and its derivatives:
Install dependecnices on Debian and its derivatives:
yhontyk marked this conversation as resolved.
Show resolved Hide resolved
```sh
sudo apt install libmiral-dev mir-graphics-drivers-desktop
```

<details>
<summary> Installing Dependencies on Other Distros </summary>
tarek-y-ismail marked this conversation as resolved.
Show resolved Hide resolved

Installing dependencies on Fedora
Instal dependencies on Fedora:
AlanGriffiths marked this conversation as resolved.
Show resolved Hide resolved
```sh
sudo dnf install mir-devel libxkbcommon
```
Installing dependencies on Alpine
Instal dependencies on Alpine:
yhontyk marked this conversation as resolved.
Show resolved Hide resolved
```sh
sudo apk add mir-dev
```
</details>


### Code
The following code block is the bare minimum you need to run a Mir compositor:
## Coding a Mir compositor

The following code which defines a barebone Mir compositor:
yhontyk marked this conversation as resolved.
Show resolved Hide resolved

```cpp
#include <miral/runner.h>
#include <miral/minimal_window_manager.h>
Expand All @@ -61,13 +64,17 @@ int main(int argc, char const* argv[])
}
```

This program creates a floating window manager with basic window management
capabilities such as controlling multiple windows, minimizing and maximizing,
and handling mouse input. This is done with the help of MirAL (Mir Abstraction
Layer) which gives you a high level interface to work with Mir.

### Building
To compile this simple program, you can use this `CMakeLists.txt` file:
`MirRunner` is a class from the `libmiral` library that that provides a high level interface for Mir.
yhontyk marked this conversation as resolved.
Show resolved Hide resolved

`MirRunner runner` calls `run_with()` which specifies different fucntions of the composer. In this example, `run_with()` evokes a function `set_window_management_policy` that applies `MinimalWindowManager` policy to `runner`. The composer is therefore created with basic window management capabilities such as controlling multiple windows, minimizing and maximizing, and handling mouse input.
yhontyk marked this conversation as resolved.
Show resolved Hide resolved

Through this syntax, you can add different functionality to the conposer such as different onscreen keyboards, screen capture, pointer confinement, and so on.
yhontyk marked this conversation as resolved.
Show resolved Hide resolved

## Building a Mir composer
AlanGriffiths marked this conversation as resolved.
Show resolved Hide resolved

To compile this simple program, create a `CMakeLists.txt` file with the following content:

```cmake
cmake_minimum_required(VERSION 3.5)

Expand All @@ -86,30 +93,29 @@ target_link_libraries( demo-mir-compositor ${MIRAL_LDFLAGS})
target_link_libraries( demo-mir-compositor ${XKBCOMMON_LIBRARIES})
```

Then to build:
Then build it:
```sh
cmake -B build
cmake --build build
```

### Running
To run, you can run nested in an X or Wayland session, or from a virtual
terminal, just like the demo applications in [Learn What Mir Can
Do](learn-what-mir-can-do.md). For example, if we were to run inside an
existing Wayland session, we'd run the following command:
## Running a Mir composer
AlanGriffiths marked this conversation as resolved.
Show resolved Hide resolved
You can run a composer nested in an X or Wayland session, or from a virtual terminal, just like the demo applications in [Getting started with Mir](learn-what-mir-can-do.md).
AlanGriffiths marked this conversation as resolved.
Show resolved Hide resolved

For example, to run inside an existing Wayland session:
```sh
WAYLAND_DISPLAY=wayland-99 ./build/demo-mir-compositor
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To the uninitiated like me, wayland-99 seems like a magic number. Maybe it can get a note or a comment?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually, Wayland compositors use wayland-0 (or wayland-1, wayland-2, ... if you're running multiple compositors). This is used as the name of the compositor's socket which two compositors cannot share (as far as I know). So wayland-99 is just a convention we use in the team to not have to worry about that.

```
You'll see a window housing the compositor with a black void filling it. To
fill this void with some content, you can run the following from another
terminal:
An all-black window with the compositor will pop up. Try moving it around the screen, maximizing and minimazing it. This functionality is provided by the `MinimalWindowManager` policy that you have added to your composer.
yhontyk marked this conversation as resolved.
Show resolved Hide resolved

To change the black background of the window and display some content instead, open another terminal and run:
```sh
WAYLAND_DISPLAY=wayland-99 bomber
```
You're free to replace `bomber` with any other Wayland compatible application.
You can replace `bomber` with any other Wayland-compatible application.
yhontyk marked this conversation as resolved.
Show resolved Hide resolved

## Next steps
Now that you have your base compositor working, feel free to check out these guides on how to further develop your compositor:
Now that you have your base compositor working, check out these guides on how to further develop your compositor:

- [How to specify start up applications](/how-to/how-to-specify-startup-apps.md)
- [How to handle user input](/how-to/how-to-handle-keyboard-input.md)
Expand Down
Loading