Wayland入门教程08.07:shell stable协议

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

上一篇:Wayland入门教程08.06:获取全局对象

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

之前的v1.0文章中使用的是wayland默认的wl_shell,当前使用非官方默认的stable版扩展协议。

需要监听新的注册事件

1
2
3
4
5
6
7
8
9
10
static void
global_registry_handler(void *data, struct wl_registry *registry,
uint32_t id, const char *interface, uint32_t version) {
printf("Got a registry event for %s id %d\n", interface, id);
if (strcmp(interface, "wl_compositor") == 0) {
compositor = wl_registry_bind(registry,id,&wl_compositor_interface,version);
} else if (strcmp(interface, xdg_wm_base_interface.name) == 0) {
xdg_shell = wl_registry_bind(registry,id,&xdg_wm_base_interface,1);
}
}

创建新对象

1
2
3
4
5
6
7
8
struct xdg_surface *shell_surface = xdg_wm_base_get_xdg_surface(xdg_shell, surface);
if (shell_surface == NULL)
{
fprintf(stderr, "Can't create shell surface\n");
exit(1);
}else{
fprintf(stderr, "Created shell surface\n");
}

编译

协议以xml格式提供,我们在使用时要把它生成为代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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)
gcc -o shell_stable shell_stable.c $(SOURCES) -I. -lwayland-client -lwayland-egl -lEGL -lGL

xdg-shell-client-protocol.h:
$(WAYLAND_SCANNER) client-header $(XDG_SHELL_PROTOCOL) xdg-shell-client-protocol.h

xdg-shell-protocol.c:
$(WAYLAND_SCANNER) private-code $(XDG_SHELL_PROTOCOL) xdg-shell-protocol.c

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

编译,运行,输出为

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
32
33
XDG_RUNTIME_DIR= /run/user/1000
connected to display
Got a registry event for wl_compositor id 1
Got a registry event for wl_drm id 2
Got a registry event for wl_shm id 3
Got a registry event for wl_output id 4
Got a registry event for zxdg_output_manager_v1 id 5
Got a registry event for wl_data_device_manager id 6
Got a registry event for zwp_primary_selection_device_manager_v1 id 7
Got a registry event for gtk_primary_selection_device_manager id 8
Got a registry event for wl_subcompositor id 9
Got a registry event for xdg_wm_base id 10
Got a registry event for zxdg_shell_v6 id 11
Got a registry event for gtk_shell1 id 12
Got a registry event for wp_viewporter id 13
Got a registry event for zwp_pointer_gestures_v1 id 14
Got a registry event for zwp_tablet_manager_v2 id 15
Got a registry event for wl_seat id 16
Got a registry event for zwp_relative_pointer_manager_v1 id 17
Got a registry event for zwp_pointer_constraints_v1 id 18
Got a registry event for zxdg_exporter_v1 id 19
Got a registry event for zxdg_importer_v1 id 20
Got a registry event for zwp_linux_dmabuf_v1 id 21
Got a registry event for zwp_keyboard_shortcuts_inhibit_manager_v1 id 22
Got a registry event for zwp_text_input_manager_v3 id 23
Got a registry event for wp_presentation id 24
Got a registry event for xdg_activation_v1 id 25
Found compositor
Created surface
Found valid shell.
Created shell surface
Nothing to display.
disconnected from display

完整代码在Wayland_Freshman中的08.07.shell下,在同目录下提供了unstable版协议的代码。

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


Wayland入门教程08.07:shell stable协议
https://blog.jackeylea.com/wayland/wayland-shell-stable-protocols/
作者
JackeyLea
发布于
2021年11月12日
许可协议