1 of 60

smithay で作って動かす Wayland compositor

2 of 60

smithay で作って動かす

Wayland compositor

kenoss

Rust.Tokyo 2024 2024-11-30

3 of 60

Goal

Goal

  • Wayland と smithay の解説
  • 「Wayland compositor って自分で作れるんだ」と思ってもらう
  • 自分が普段どういう風にコードを調べているかを共有

4 of 60

自己紹介

  • 名前
    • keno (kenoss/keno_ss)
  • 仕事
    • Web 系
  • 趣味
    • コーヒー, ソフトウェアトーク
    • テキストエディタ, Wayland compositor, Yak Shaving (?)

5 of 60

Wayland and Wayland compositor

  • Wayland は Linux GUI management のプロトコル
  • Wayland compositor がサーバー
  • アプリケーションがクライアント

6 of 60

Wayland and Wayland compositor

  • HTTP はインターネット通信のプロトコル
  • (サーバーが) サーバー
  • ブラウザがクライアント

POST /hoge

200 OK

7 of 60

Wayland and Wayland compositor

  • Wayland は Linux GUI のプロトコル
  • Wayland compositor がサーバー
  • アプリケーションがクライアント

表示して

OK

8 of 60

9 of 60

10 of 60

Linux kernel

11 of 60

Linux kernel

KMS

DRM

mesa

12 of 60

Linux kernel

KMS

DRM

mesa

13 of 60

Linux kernel

evdev

KMS

DRM

mesa

libinput

14 of 60

Writing an Wayland compositor?

  • 理想の挙動を実現したいから
  • Rust で書けるから
  • 楽しいから

15 of 60

Using smithay

smithay: A smithy for rusty wayland compositors

  • Wayland compositor 用のフレームワーク
  • かなりの部分が Rust で書かれている.

16 of 60

Linux kernel

evdev

KMS

DRM

mesa

libinput

17 of 60

Linux kernel

evdev

KMS

DRM

mesa

libinput

winit

今日は簡単のため winit を使う

18 of 60

Using winit

winit: Cross-platform window creation and management in Rust

  • Example: alacritty

19 of 60

Wayland compositors in Rust

  • Smallvil (smithay)
    • 簡素な example. winit backend のみ.
  • Anvil (smithay)
    • smithay の新機能のテストのための sample.
  • niri
    • A scrollable-tiling Wayland compositor.
    • smithay 界隈でコードが一番綺麗
    • GPL-3.0

  • sabiniwm
    • A tiling Wayland compositor, influenced xmonad
    • 拙作. Fork of anvil.
    • まだ「動いた」レベル.
  • tatarajo
    • 教育用タイリング Wayland compositor (Supports udev backend.)
    • Anvil - floating windows + refactoring
    • 今日の話

20 of 60

A course tatarajo

おおむね僕が最初に smallvil を弄っていたときのリプレイ.

21 of 60

Run tatarajo

$ git clone https://github.com/kenoss/tatarajo

$ cargo run --bin tatarajo-chocomint

Mod4-b Mod4-t でターミナルを開く

22 of 60

23 of 60

基本的な機能

  • alacritty を起動
  • ウインドウを描画
  • tatarajo を終了

まずはこれらの機能を見ていく.

24 of 60

tatarajo を終了

「まぁ quit か kill でひっかかるだろう」

25 of 60

  • H-x H-qaction::ActionQuitSabiniwm をなにかする.
  • こいつは exec() されるとなんか stop() する.

26 of 60

TatarajoState: この Wayland compositor の神オブジェクト. 全ての根っこ.

27 of 60

DisplayHandle: ソケット WAYLAND_DISPLAY のハンドラ

28 of 60

calloop::EventLoop, calloop::LoopHandle: イベントループ

stop() するとこのループが止まる!

29 of 60

alacritty を起動

std::process::Command::spawn() しとるはず」

30 of 60

このとき環境変数 WAYLAND_DISPLAY がセットされている.

これで各種イベントが入ってくる.

31 of 60

ウインドウを描画

「render か paint かな?」

32 of 60

33 of 60

小休止

  • alacritty を起動
  • ウインドウを描画
  • tatarajo を終了

なんとなく流れはわかった.

じゃあ機能を追加してみよう!!

34 of 60

機能追加

  • ウインドウをよしなに並べる (Tall layout)
  • マージンを空ける
  • focus されているウインドウにボーダーをつける

35 of 60

ウインドウをよしなに並べる (Tall layout)

  • ウインドウの扱い
  • レンダリング

あたりを見る.

36 of 60

37 of 60

ウインドウを抽象

Window が Output (ディスプレイ)のどこにいるかをマネジメント

38 of 60

smithay::desktop::Window

Window を抽象

smithay::desktop::space::Space<E: SpaceElement>:

Window が Output (ディスプレイ) のどこにいるかをマネジメント

39 of 60

ウインドウはどこから来るのか?

40 of 60

41 of 60

ここに panic!(); を入れて

RUST_BACKTRACE=1 で実行してみると

呼び出し元がわかるが省略

細かいハンドリングは smithay がやってくれるので,

この後の Window をよしなに弄ればよさそう

42 of 60

View::layout()Space::map_element() を呼ぶ.

この前に位置とかを弄ればよさそう.

43 of 60

いいかんじに並べる + etc. (See commit.)

44 of 60

45 of 60

マージンを空ける

隙間がないと見にくくない?

46 of 60

あける! (See commit.)

47 of 60

48 of 60

focus されているウインドウにボーダーをつける

どうやって...?

49 of 60

focus されているウインドウにボーダーをつける

ところで Anvil ではウインドウに

装飾を付けられる.

50 of 60

なんかそれっぽい

51 of 60

52 of 60

53 of 60

これや!!

  • smithay::desktop::SpaceWindowElement::render_elements() を呼ぶが, これは複数の render element を返せる.
  • focus が付いているときに SolidColorRenderElement を返せば良さそう.

See commit.

54 of 60

55 of 60

できた!!

  • ウインドウをよしなに並べる (Tall layout)
  • マージンを空ける
  • focus されているウインドウにボーダーをつける

56 of 60

まとめ

話したこと

  • smithay が下層をかなりハンドリングしてくれる. べんり
  • ベースとなる Wayland compositor にどうやって機能を追加していくか
  • Rust の良さ
    • cargo build, cargo doc, crates, rg, trait, macro, etc.

話さなかったこと

  • calloop のべんりさ
  • udev/winit backend や extention まわりをどう書くか
  • sabiniwm/tatarajo での工夫 (の全容)

57 of 60

Additional resources

全て読破するのではなく, どこに何があるかを把握しておいて必要になったら読むのがオススメ

  • The Wayland protocol (book)
    • Protocol について
  • The Smithay Handbook
    • App 視点. Wayland compositor 側は WIP.
  • wlroots の Getting started
  • smithay を利用しているアプリケーションのリスト
    • ひとつの実装を見るだけではわからないことを複数調べたりする.

58 of 60

  • Q. wlroots [本体][crate] はどう?
  • A. “This is no longer maintained. As an alternative consider smithay.”

  • Q. リッチな Wayland compositor を作りたいが何を読むとよい?
  • A. Smallvil, anvil, niri, あと smithay を使ってる他のリッチなやつ, 他の desktop environment.

59 of 60

60 of 60

Smithay

で作って動かす

ayland compositor