【Wayland】Wayland协议说明

Wayland是一种窗口管理协议,定义了DisplayServer与DisplayClient间的通信方式。Weston是其参考实现,提供窗口管理组件。协议基于消息,采用异步面向对象模型。通过XML定义接口,使用Scanner工具生成客户端和服务端代码。错误处理、同步请求和全局对象管理是核心接口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Wayland协议说明

Wayland基本概念

什么是wayland?

  • 它是一种窗口管理协议。简单理解,wayland是一套Display Server与Display Client间通信的协议。Wayland定义了一套标准协议,并规定这套协议中的核心对象(https://wayland.freedesktop.org/docs/html/ch04.html#sect-Protocol-Interfaces)。

Weston是什么?

  • Wayland只是一套协议,一套规范。Weston是wayland官网给出的关于Wayland Compositor的一套参考实现。

不深究细节的话,可以稍微简单的把Weston直接理解为,wayland官方给出的,遵循Wayland协议的,一套了窗口管理的组件。

几个概念

  • 异步面向对象的协议:wayland中所有的请求,都是对”对象“的方法调用。这里的请求,可以理解为Client对Server端的功能请求。
    在这里插入图片描述
  • 基于消息的协议:wayland是基于消息的协议。Server和Client两端间,通过消息交互。Wayland协议规定了消息的固定格式。详细请参考官网 (https://wayland.freedesktop.org/docs/html/ch04.html#sect-Protocol-Wire-Format)
    在这里插入图片描述
    scanner代码生成
  • 一般来讲,如果定义了某套协议规范。基本上都会有一套,与之相应的代码生成方式。Wayland协议代码生成,类似于AIDL、FIDL等接口协议。如下图,遵循协议规范的xml文件(文件内容,根据wayland协议接口规范编写),利用wayland提供的工具,生成对应的源码文件。
    在这里插入图片描述

Wayland协议

  • wayland协议在表现上为,一系列Interface的组合。Interface包括Request和Event。
  • 一系列的Interface分为两部分,Core和扩展两部分,其中Core为wayland协议规定,必须实现的Interface。
    在这里插入图片描述
协议语义语法

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

代码生成

  • 注意,下述生成的源文件,只是相关的Interface的定义。只是接口的定义,关于接口定义的实现,在另一部分(例如weston)。Interface功能的实现,不在这里介绍。
# 生成Client端文件
./scanner client-header wayland.xml wayland-client-protocol.h
# 生成Server端头文件
./scanner server-header wayland.xml wayland-server-protocol.h
# 生成共同代码
./scanner code  wayland.xml wayland-protocol.c

生成代码部分截取

  • wayland-server-protocol.h
@ingroup iface_wl_display										
@struct wl_display_interface										
/										
struct wl_display_interface {		// wl_display +interface						
/**										
asynchronous roundtrip				// 这段注释跟上面xml写的是一样的						
The sync request asks the server to emit the ‘done’ event on										
the returned wl_callback object. Since requests are handled										
in-order and events are delivered in-order, this can be used as										
a barrier to ensure all previous requests and the resulting										
events have been handled.										
The object returned by this request will be destroyed by the										
compositor after the callback is fired and as such the client										
must not attempt to use it after that point.										
The callback_data passed in the callback is the event serial.										
/										
void (*sync)(struct wl_client *client,	// Server端的接口,以函数指针形式定义					
   struct wl_resource *resource,		// client +resource是wayland协议自动补全的					
   uint32_t callback);					// 其实就是类似于C++成员函数里,有一个隐含的this指针形参一样					
/**										
get global registry object										
This request creates a registry object that allows the client										
to list and bind the global objects available from the										
compositor.										
/										
void (*get_registry)(struct wl_client *client,										
       struct wl_resource *resource,										
       uint32_t registry);										
};										
										
										
										
#define WL_DISPLAY_ERROR 0 					// Event(Error)的宏定义					
										
#define WL_DISPLAY_ERROR_SINCE_VERSION  1										
										
										
#ifndef WL_DISPLAY_ERROR_ENUM					// Enmu的定义					
										
#define WL_DISPLAY_ERROR_ENUM										
/**										
										
@ingroup iface_wl_display										
global error values										
These errors are global and can be emitted in response to any										
server request.										
/										
enum wl_display_error {										
/**										
server couldn’t find object										
/										
WL_DISPLAY_ERROR_INVALID_OBJECT = 0,										
/**										
method doesn’t exist on the specified interface										
/										
WL_DISPLAY_ERROR_INVALID_METHOD = 1,										
/**										
server is out of memory										
/										
WL_DISPLAY_ERROR_NO_MEMORY = 2,										
};										
#endif /* WL_DISPLAY_ERROR_ENUM */										
										

  • wayland-client-protocol.h
// define WL_DISPLAY_SYNC 0								// 方法定义的宏(OpeID)		
										
										
/**											
@ingroup iface_wl_display										
										
The sync request asks the server to emit the ‘done’ event										
										
on the returned wl_callback object. Since requests are										
										
handled in-order and events are delivered in-order, this can										
										
be used as a barrier to ensure all previous requests and the										
										
resulting events have been handled.										
										
The object returned by this request will be destroyed by the										
										
compositor after the callback is fired and as such the client must not										
										
attempt to use it after that point.										
										
The callback_data passed in the callback is the event serial.										
										
/										
static inline struct wl_callback *										
wl_display_sync(struct wl_display *wl_display)	// Client端 对应的接口										
{										
struct wl_proxy *callback;										
										
callback = wl_proxy_marshal_constructor((struct wl_proxy *) wl_display,										
       WL_DISPLAY_SYNC, &wl_callback_interface, NULL);										
return (struct wl_callback *) callback;										
}										
										
										
										
/**	
										
@ingroup iface_wl_display										
@struct wl_display_listener										
/										
struct wl_display_listener {	// Client端 Event接口								
/**										
fatal error event										
The error event is sent out when a fatal (non-recoverable)										
error has occurred. The object_id argument is the object where										
the error occurred, most often in response to a request to that										
object. The code identifies the error and is defined by the										
object interface. As such, each interface defines its own set of										
error codes. The message is an brief description of the error,										
for (debugging) convenience.										
/										
void (*error)(void *data,										
    struct wl_display *wl_display,										
    void *object_id,										
    uint32_t code,										
    const char *message);										
/**										
acknowledge object ID deletion										
This event is used internally by the object ID management										
logic. When a client deletes an object, the server will send										
this event to acknowledge that it has seen the delete request.										
When the client receive this event, it will know that it can										
safely reuse the object ID.										
/										
void (*delete_id)(void *data,										
    struct wl_display *wl_display,										
    uint32_t id);										
};					
  • wayland-protocol.c
static const struct wl_message wl_display_requests[] = { // request	
{ “sync”, “n”, types + 8 },							
{ “get_registry”, “n”, types + 9 },								
};								
								
static const struct wl_message wl_display_events[] = {	 // event	
{ “error”, “ous”, types + 0 },								
{ “delete_id”, “u”, types + 0 },								
};								
								
WL_EXPORT const struct wl_interface wl_display_interface = { // interface	
“wl_display”, 1,								
2, wl_display_requests,								
2, wl_display_events,								
};								
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林多

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值