Wayland入门教程08.08:Protocol说明

系列索引地址:Wayland入门教程索引

上一篇:Wayland入门教程08.07:shell stable协议

在上一篇Wayland入门教程08.07:shell stable协议中,我们操作的界面是客户端,鼠标点击、窗口移动、窗口大小、键盘输入都是由客户端捕获,然后通过协议发送给服务端。这个协议Wayland由提供默认的,如果默认的满足不了需求,可以自定义协议。协议格式为XML,使用wayland-scanner程序转换为代码。

生成代码

在makefile文件中使用命令直接导出为代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
WAYLAND_FLAGS = $(shell pkg-config wayland-client --cflags --libs)
WAYLAND_PROTOCOLS_DIR = $(shell pkg-config wayland-protocols --variable=pkgdatadir)
WAYLAND_SCANNER = $(shell pkg-config --variable=wayland_scanner wayland-scanner)

XDG_SHELL_PROTOCOL = $(WAYLAND_PROTOCOLS_DIR)/stable/xdg-shell/xdg-shell.xml

HEADERS=xdg-shell-client-protocol.h
SOURCES=xdg-shell-protocol.c

all: $(HEADERS) $(SOURCES)

$(HEADERS):
$(WAYLAND_SCANNER) client-header $(XDG_SHELL_PROTOCOL) $@

$(SOURCES):
$(WAYLAND_SCANNER) private-code $(XDG_SHELL_PROTOCOL) $@

clean:
rm -rf $(HEADERS) $(SOURCES)

从pkg-config中提取变量值,然后调用wayland-scanner加上参数生成h/c文件。

执行命令,可以生成代码文件

1
2
3
hyper@wayland:~/scan$ make
/usr/bin/wayland-scanner client-header //usr/share/wayland-protocols/stable/xdg-shell/xdg-shell.xml xdg-shell-client-protocol.h
/usr/bin/wayland-scanner private-code //usr/share/wayland-protocols/stable/xdg-shell/xdg-shell.xml xdg-shell-protocol.c

代码对应

协议中的部分内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 <interface name="xdg_positioner" version="6">
...
<enum name="gravity">
<entry name="none" value="0"/>
<entry name="top" value="1"/>
<entry name="bottom" value="2"/>
<entry name="left" value="3"/>
<entry name="right" value="4"/>
<entry name="top_left" value="5"/>
<entry name="bottom_left" value="6"/>
<entry name="top_right" value="7"/>
<entry name="bottom_right" value="8"/>
</enum>

<request name="set_gravity">
<description summary="set child surface gravity">
Defines in what direction a surface should be positioned, relative to
the anchor point of the parent surface. If a corner gravity is
specified (e.g. 'bottom_right' or 'top_left'), then the child surface
will be placed towards the specified gravity; otherwise, the child
surface will be centered over the anchor point on any axis that had no
gravity specified. If the gravity is not in the ‘gravity’ enum, an
invalid_input error is raised.
</description>
<arg name="gravity" type="uint" enum="gravity"
summary="gravity direction"/>
</request>

...

</interface>

enum字段的gravity在头文件中对应

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef XDG_POSITIONER_GRAVITY_ENUM
#define XDG_POSITIONER_GRAVITY_ENUM
enum xdg_positioner_gravity {
XDG_POSITIONER_GRAVITY_NONE = 0,
XDG_POSITIONER_GRAVITY_TOP = 1,
XDG_POSITIONER_GRAVITY_BOTTOM = 2,
XDG_POSITIONER_GRAVITY_LEFT = 3,
XDG_POSITIONER_GRAVITY_RIGHT = 4,
XDG_POSITIONER_GRAVITY_TOP_LEFT = 5,
XDG_POSITIONER_GRAVITY_BOTTOM_LEFT = 6,
XDG_POSITIONER_GRAVITY_TOP_RIGHT = 7,
XDG_POSITIONER_GRAVITY_BOTTOM_RIGHT = 8,
};
#endif /* XDG_POSITIONER_GRAVITY_ENUM */

request字段的set_gravity对应头文件的函数为

1
2
3
4
5
6
static inline void
xdg_positioner_set_gravity(struct xdg_positioner *xdg_positioner, uint32_t gravity)
{
wl_proxy_marshal_flags((struct wl_proxy *) xdg_positioner,
XDG_POSITIONER_SET_GRAVITY, NULL, wl_proxy_get_version((struct wl_proxy *) xdg_positioner), 0, gravity);
}

对应的实现部分为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
static const struct wl_message xdg_positioner_requests[] = {
{ "destroy", "", xdg_shell_types + 0 },
{ "set_size", "ii", xdg_shell_types + 0 },
{ "set_anchor_rect", "iiii", xdg_shell_types + 0 },
{ "set_anchor", "u", xdg_shell_types + 0 },
{ "set_gravity", "u", xdg_shell_types + 0 },
{ "set_constraint_adjustment", "u", xdg_shell_types + 0 },
{ "set_offset", "ii", xdg_shell_types + 0 },
{ "set_reactive", "3", xdg_shell_types + 0 },
{ "set_parent_size", "3ii", xdg_shell_types + 0 },
{ "set_parent_configure", "3u", xdg_shell_types + 0 },
};

WL_PRIVATE const struct wl_interface xdg_positioner_interface = {
"xdg_positioner", 6,
10, xdg_positioner_requests,
0, NULL,
};

完整代码在Wayland_Freshman中的08.08.protocol下。

下一篇:Wayland入门教程08.09:第一个窗口


Wayland入门教程08.08:Protocol说明
https://blog.jackeylea.com/wayland/intro-of-wayland-protocols/
作者
JackeyLea
发布于
2024年3月2日
许可协议