1<?xml version='1.0' encoding='utf-8' ?>
2<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
3<!ENTITY % BOOK_ENTITIES SYSTEM "Wayland.ent">
4%BOOK_ENTITIES;
5]>
6<chapter id="chap-Protocol">
7  <title>Wayland Protocol and Model of Operation</title>
8  <section id="sect-Protocol-Basic-Principles">
9    <title>Basic Principles</title>
10    <para>
11      The Wayland protocol is an asynchronous object oriented protocol.  All
12      requests are method invocations on some object.  The requests include
13      an object ID that uniquely identifies an object on the server.  Each
14      object implements an interface and the requests include an opcode that
15      identifies which method in the interface to invoke.
16    </para>
17    <para>
18      The protocol is message-based.  A message sent by a client to the server
19      is called request.  A message from the server to a client is called event.
20      A message has a number of arguments, each of which has a certain type (see
21      <xref linkend="sect-Protocol-Wire-Format"/> for a list of argument types).
22    </para>
23    <para>
24      Additionally, the protocol can specify <type>enum</type>s which associate
25      names to specific numeric enumeration values.  These are primarily just
26      descriptive in nature: at the wire format level enums are just integers.
27      But they also serve a secondary purpose to enhance type safety or
28      otherwise add context for use in language bindings or other such code.
29      This latter usage is only supported so long as code written before these
30      attributes were introduced still works after; in other words, adding an
31      enum should not break API, otherwise it puts backwards compatibility at
32      risk.
33    </para>
34    <para>
35      <type>enum</type>s can be defined as just a set of integers, or as
36      bitfields.  This is specified via the <type>bitfield</type> boolean
37      attribute in the <type>enum</type> definition.  If this attribute is true,
38      the enum is intended to be accessed primarily using bitwise operations,
39      for example when arbitrarily many choices of the enum can be ORed
40      together; if it is false, or the attribute is omitted, then the enum
41      arguments are a just a sequence of numerical values.
42    </para>
43    <para>
44      The <type>enum</type> attribute can be used on either <type>uint</type>
45      or <type>int</type> arguments, however if the <type>enum</type> is
46      defined as a <type>bitfield</type>, it can only be used on
47      <type>uint</type> args.
48    </para>
49    <para>
50      The server sends back events to the client, each event is emitted from
51      an object.  Events can be error conditions.  The event includes the
52      object ID and the event opcode, from which the client can determine
53      the type of event.  Events are generated both in response to requests
54      (in which case the request and the event constitutes a round trip) or
55      spontaneously when the server state changes.
56    </para>
57    <para>
58      <itemizedlist>
59	<listitem>
60	  <para>
61	    State is broadcast on connect, events are sent
62	    out when state changes. Clients must listen for
63	    these changes and cache the state.
64	    There is no need (or mechanism) to query server state.
65	  </para>
66	</listitem>
67	<listitem>
68	  <para>
69	    The server will broadcast the presence of a number of global objects,
70	    which in turn will broadcast their current state.
71	  </para>
72	</listitem>
73      </itemizedlist>
74    </para>
75  </section>
76  <section id="sect-Protocol-Code-Generation">
77    <title>Code Generation</title>
78    <para>
79      The interfaces, requests and events are defined in
80      <filename>protocol/wayland.xml</filename>.
81      This xml is used to generate the function prototypes that can be used by
82      clients and compositors.
83    </para>
84    <para>
85      The protocol entry points are generated as inline functions which just
86      wrap the <function>wl_proxy_*</function> functions.  The inline functions aren't
87      part of the library ABI and language bindings should generate their
88      own stubs for the protocol entry points from the xml.
89    </para>
90  </section>
91  <section id="sect-Protocol-Wire-Format">
92    <title>Wire Format</title>
93    <para>
94      The protocol is sent over a UNIX domain stream socket, where the endpoint
95      usually is named <systemitem class="service">wayland-0</systemitem>
96      (although it can be changed via <emphasis>WAYLAND_DISPLAY</emphasis>
97      in the environment). Beginning in Wayland 1.15, implementations can
98      optionally support server socket endpoints located at arbitrary
99      locations in the filesystem by setting <emphasis>WAYLAND_DISPLAY</emphasis>
100      to the absolute path at which the server endpoint listens.
101    </para>
102    <para>
103      Every message is structured as 32-bit words; values are represented in the
104      host's byte-order.  The message header has 2 words in it:
105      <itemizedlist>
106	<listitem>
107	  <para>
108	    The first word is the sender's object ID (32-bit).
109	  </para>
110	</listitem>
111	<listitem>
112	  <para>
113	    The second has 2 parts of 16-bit.  The upper 16-bits are the message
114	    size in bytes, starting at the header (i.e. it has a minimum value of 8).The lower is the request/event opcode.
115	  </para>
116	</listitem>
117      </itemizedlist>
118      The payload describes the request/event arguments.  Every argument is always
119      aligned to 32-bits. Where padding is required, the value of padding bytes is
120      undefined. There is no prefix that describes the type, but it is
121      inferred implicitly from the xml specification.
122    </para>
123    <para>
124
125      The representation of argument types are as follows:
126      <variablelist>
127	<varlistentry>
128	  <term>int</term>
129	  <term>uint</term>
130	  <listitem>
131	    <para>
132	      The value is the 32-bit value of the signed/unsigned
133	      int.
134	    </para>
135	  </listitem>
136	</varlistentry>
137	<varlistentry>
138	  <term>fixed</term>
139	  <listitem>
140	    <para>
141	      Signed 24.8 decimal numbers. It is a signed decimal type which
142	      offers a sign bit, 23 bits of integer precision and 8 bits of
143	      decimal precision. This is exposed as an opaque struct with
144	      conversion helpers to and from double and int on the C API side.
145	    </para>
146	  </listitem>
147	</varlistentry>
148	<varlistentry>
149	  <term>string</term>
150	  <listitem>
151	    <para>
152	      Starts with an unsigned 32-bit length, followed by the
153	      string contents, including terminating null byte, then padding
154	      to a 32-bit boundary.
155	    </para>
156	  </listitem>
157	</varlistentry>
158	<varlistentry>
159	  <term>object</term>
160	  <listitem>
161	    <para>
162	      32-bit object ID.
163	    </para>
164	  </listitem>
165	</varlistentry>
166	<varlistentry>
167	  <term>new_id</term>
168	  <listitem>
169	    <para>
170	      The 32-bit object ID.  Generally, the interface used for the new
171	      object is inferred from the xml, but in the case where it's not
172	      specified, a new_id is preceded by a <code>string</code> specifying
173	      the interface name, and a <code>uint</code> specifying the version.
174	    </para>
175	  </listitem>
176	</varlistentry>
177	<varlistentry>
178	  <term>array</term>
179	  <listitem>
180	    <para>
181	      Starts with 32-bit array size in bytes, followed by the array
182	      contents verbatim, and finally padding to a 32-bit boundary.
183	    </para>
184	  </listitem>
185	</varlistentry>
186	<varlistentry>
187	  <term>fd</term>
188	  <listitem>
189	    <para>
190	      The file descriptor is not stored in the message buffer, but in
191	      the ancillary data of the UNIX domain socket message (msg_control).
192	    </para>
193	  </listitem>
194	</varlistentry>
195      </variablelist>
196    </para>
197  </section>
198  <xi:include href="ProtocolInterfaces.xml" xmlns:xi="http://www.w3.org/2001/XInclude"/>
199  <section id="sect-Protocol-Versioning">
200    <title>Versioning</title>
201    <para>
202      Every interface is versioned and every protocol object implements a
203      particular version of its interface.  For global objects, the maximum
204      version supported by the server is advertised with the global and the
205      actual version of the created protocol object is determined by the
206      version argument passed to wl_registry.bind().  For objects that are
207      not globals, their version is inferred from the object that created
208      them.
209    </para>
210    <para>
211      In order to keep things sane, this has a few implications for
212      interface versions:
213      <itemizedlist>
214	<listitem>
215	  <para>
216	    The object creation hierarchy must be a tree.  Otherwise,
217	    inferring object versions from the parent object becomes a much
218	    more difficult to properly track.
219	  </para>
220	</listitem>
221	<listitem>
222	  <para>
223	    When the version of an interface increases, so does the version
224	    of its parent (recursively until you get to a global interface)
225	  </para>
226	</listitem>
227	<listitem>
228	  <para>
229	    A global interface's version number acts like a counter for all
230	    of its child interfaces.  Whenever a child interface gets
231	    modified, the global parent's interface version number also
232	    increases (see above).  The child interface then takes on the
233	    same version number as the new version of its parent global
234	    interface.
235	  </para>
236	</listitem>
237      </itemizedlist>
238    </para>
239    <para>
240      To illustrate the above, consider the wl_compositor interface.  It
241      has two children, wl_surface and wl_region.  As of wayland version
242      1.2, wl_surface and wl_compositor are both at version 3.  If
243      something is added to the wl_region interface, both wl_region and
244      wl_compositor will get bumpped to version 4.  If, afterwards,
245      wl_surface is changed, both wl_compositor and wl_surface will be at
246      version 5.  In this way the global interface version is used as a
247      sort of "counter" for all of its child interfaces.  This makes it
248      very simple to know the version of the child given the version of its
249      parent.  The child is at the highest possible interface version that
250      is less than or equal to its parent's version.
251    </para>
252    <para>
253      It is worth noting a particular exception to the above versioning
254      scheme.  The wl_display (and, by extension, wl_registry) interface
255      cannot change because it is the core protocol object and its version
256      is never advertised nor is there a mechanism to request a different
257      version.
258    </para>
259  </section>
260  <section id="sect-Protocol-Connect-Time">
261    <title>Connect Time</title>
262    <para>
263      There is no fixed connection setup information, the server emits
264      multiple events at connect time, to indicate the presence and
265      properties of global objects: outputs, compositor, input devices.
266    </para>
267  </section>
268  <section id="sect-Protocol-Security-and-Authentication">
269    <title>Security and Authentication</title>
270    <para>
271      <itemizedlist>
272	<listitem>
273	  <para>
274	    mostly about access to underlying buffers, need new drm auth
275	    mechanism (the grant-to ioctl idea), need to check the cmd stream?
276	  </para>
277	</listitem>
278	<listitem>
279	  <para>
280	    getting the server socket depends on the compositor type, could
281	    be a system wide name, through fd passing on the session dbus.
282	    or the client is forked by the compositor and the fd is
283	    already opened.
284	  </para>
285	</listitem>
286      </itemizedlist>
287    </para>
288  </section>
289  <section id="sect-Protocol-Creating-Objects">
290    <title>Creating Objects</title>
291    <para>
292      Each object has a unique ID.  The IDs are allocated by the entity
293      creating the object (either client or server).  IDs allocated by the
294      client are in the range [1, 0xfeffffff] while IDs allocated by the
295      server are in the range [0xff000000, 0xffffffff].  The 0 ID is
296      reserved to represent a null or non-existent object.
297
298      For efficiency purposes, the IDs are densely packed in the sense that
299      the ID N will not be used until N-1 has been used.  Any ID allocation
300      algorithm that does not maintain this property is incompatible with
301      the implementation in libwayland.
302    </para>
303  </section>
304  <section id="sect-Protocol-Compositor">
305    <title>Compositor</title>
306    <para>
307      The compositor is a global object, advertised at connect time.
308    </para>
309    <para>
310      See <xref linkend="protocol-spec-wl_compositor"/> for the
311      protocol description.
312    </para>
313  </section>
314  <section id="sect-Protocol-Surface">
315    <title>Surfaces</title>
316    <para>
317      A surface manages a rectangular grid of pixels that clients create
318      for displaying their content to the screen.  Clients don't know
319      the global position of their surfaces, and cannot access other
320      clients' surfaces.
321    </para>
322    <para>
323      Once the client has finished writing pixels, it 'commits' the
324      buffer; this permits the compositor to access the buffer and read
325      the pixels.  When the compositor is finished, it releases the
326      buffer back to the client.
327    </para>
328    <para>
329      See <xref linkend="protocol-spec-wl_surface"/> for the protocol
330      description.
331    </para>
332  </section>
333  <section id="sect-Protocol-Input">
334    <title>Input</title>
335    <para>
336      A seat represents a group of input devices including mice,
337      keyboards and touchscreens. It has a keyboard and pointer
338      focus. Seats are global objects. Pointer events are delivered
339      in surface-local coordinates.
340    </para>
341    <para>
342      The compositor maintains an implicit grab when a button is
343      pressed, to ensure that the corresponding button release
344      event gets delivered to the same surface. But there is no way
345      for clients to take an explicit grab. Instead, surfaces can
346      be mapped as 'popup', which combines transient window semantics
347      with a pointer grab.
348    </para>
349    <para>
350      To avoid race conditions, input events that are likely to
351      trigger further requests (such as button presses, key events,
352      pointer motions) carry serial numbers, and requests such as
353      wl_surface.set_popup require that the serial number of the
354      triggering event is specified. The server maintains a
355      monotonically increasing counter for these serial numbers.
356    </para>
357    <para>
358      Input events also carry timestamps with millisecond granularity.
359      Their base is undefined, so they can't be compared against
360      system time (as obtained with clock_gettime or gettimeofday).
361      They can be compared with each other though, and for instance
362      be used to identify sequences of button presses as double
363      or triple clicks.
364    </para>
365    <para>
366      See <xref linkend="protocol-spec-wl_seat"/> for the
367      protocol description.
368    </para>
369    <para>
370      Talk about:
371
372      <itemizedlist>
373	<listitem>
374	  <para>
375	    keyboard map, change events
376	  </para>
377	</listitem>
378	<listitem>
379	  <para>
380	    xkb on Wayland
381	  </para>
382	</listitem>
383	<listitem>
384	  <para>
385	    multi pointer Wayland
386	  </para>
387	</listitem>
388      </itemizedlist>
389    </para>
390    <para>
391      A surface can change the pointer image when the surface is the pointer
392      focus of the input device.  Wayland doesn't automatically change the
393      pointer image when a pointer enters a surface, but expects the
394      application to set the cursor it wants in response to the pointer
395      focus and motion events.  The rationale is that a client has to manage
396      changing pointer images for UI elements within the surface in response
397      to motion events anyway, so we'll make that the only mechanism for
398      setting or changing the pointer image.  If the server receives a request
399      to set the pointer image after the surface loses pointer focus, the
400      request is ignored.  To the client this will look like it successfully
401      set the pointer image.
402    </para>
403    <para>
404      Setting the pointer image to NULL causes the cursor to be hidden.
405    </para>
406    <para>
407      The compositor will revert the pointer image back to a default image
408      when no surface has the pointer focus for that device.
409    </para>
410    <para>
411      What if the pointer moves from one window which has set a special
412      pointer image to a surface that doesn't set an image in response to
413      the motion event?  The new surface will be stuck with the special
414      pointer image.  We can't just revert the pointer image on leaving a
415      surface, since if we immediately enter a surface that sets a different
416      image, the image will flicker.  If a client does not set a pointer image
417      when the pointer enters a surface, the pointer stays with the image set
418      by the last surface that changed it, possibly even hidden.  Such a client
419      is likely just broken.
420    </para>
421  </section>
422  <section id="sect-Protocol-Output">
423    <title>Output</title>
424    <para>
425      An output is a global object, advertised at connect time or as it
426      comes and goes.
427    </para>
428    <para>
429      See <xref linkend="protocol-spec-wl_output"/> for the protocol
430      description.
431    </para>
432    <para>
433    </para>
434    <itemizedlist>
435      <listitem>
436	<para>
437	  laid out in a big (compositor) coordinate system
438	</para>
439      </listitem>
440      <listitem>
441	<para>
442	  basically xrandr over Wayland
443	</para>
444      </listitem>
445      <listitem>
446	<para>
447	  geometry needs position in compositor coordinate system
448	</para>
449      </listitem>
450      <listitem>
451	<para>
452	  events to advertise available modes, requests to move and change
453	  modes
454	</para>
455      </listitem>
456    </itemizedlist>
457  </section>
458  <section id="sect-Protocol-data-sharing">
459    <title>Data sharing between clients</title>
460    <para>
461      The Wayland protocol provides clients a mechanism for sharing
462      data that allows the implementation of copy-paste and
463      drag-and-drop. The client providing the data creates a
464      <function>wl_data_source</function> object and the clients
465      obtaining the data will see it as <function>wl_data_offer</function>
466      object. This interface allows the clients to agree on a mutually
467      supported mime type and transfer the data via a file descriptor
468      that is passed through the protocol.
469    </para>
470    <para>
471      The next section explains the negotiation between data source and
472      data offer objects. <xref linkend="sect-Protocol-data-sharing-devices"/>
473      explains how these objects are created and passed to different
474      clients using the <function>wl_data_device</function> interface
475      that implements copy-paste and drag-and-drop support.
476    </para>
477    <para>
478      See <xref linkend="protocol-spec-wl_data_offer"/>,
479      <xref linkend="protocol-spec-wl_data_source"/>,
480      <xref linkend="protocol-spec-wl_data_device"/> and
481      <xref linkend="protocol-spec-wl_data_device_manager"/> for
482      protocol descriptions.
483    </para>
484    <para>
485      MIME is defined in RFC's 2045-2049. A
486      <ulink url="https://www.iana.org/assignments/media-types/media-types.xhtml">
487      registry of MIME types</ulink> is maintained by the Internet Assigned
488      Numbers Authority (IANA).
489    </para>
490    <section>
491      <title>Data negotiation</title>
492      <para>
493	A client providing data to other clients will create a <function>wl_data_source</function>
494	object and advertise the mime types for the formats it supports for
495	that data through the <function>wl_data_source.offer</function>
496	request. On the receiving end, the data offer object will generate one
497	<function>wl_data_offer.offer</function> event for each supported mime
498	type.
499      </para>
500      <para>
501	The actual data transfer happens when the receiving client sends a
502	<function>wl_data_offer.receive</function> request. This request takes
503	a mime type and a file descriptor as arguments. This request will generate a
504	<function>wl_data_source.send</function> event on the sending client
505	with the same arguments, and the latter client is expected to write its
506	data to the given file descriptor using the chosen mime type.
507      </para>
508    </section>
509    <section id="sect-Protocol-data-sharing-devices">
510      <title>Data devices</title>
511      <para>
512	Data devices glue data sources and offers together. A data device is
513	associated with a <function>wl_seat</function> and is obtained by the clients using the
514	<function>wl_data_device_manager</function> factory object, which is also responsible for
515	creating data sources.
516      </para>
517      <para>
518	Clients are informed of new data offers through the
519	<function>wl_data_device.data_offer</function> event. After this
520	event is generated the data offer will advertise the available mime
521	types. New data offers are introduced prior to their use for
522	copy-paste or drag-and-drop.
523      </para>
524      <section>
525	<title>Selection</title>
526	<para>
527	  Each data device has a selection data source. Clients create a data
528	  source object using the device manager and may set it as the
529	  current selection for a given data device. Whenever the current
530	  selection changes, the client with keyboard focus receives a
531	  <function>wl_data_device.selection</function> event. This event is
532	  also generated on a client immediately before it receives keyboard
533	  focus.
534	</para>
535	<para>
536	  The data offer is introduced with
537	  <function>wl_data_device.data_offer</function> event before the
538	  selection event.
539	</para>
540      </section>
541      <section>
542	<title>Drag and Drop</title>
543	<para>
544	  A drag-and-drop operation is started using the
545	  <function>wl_data_device.start_drag</function> request. This
546	  requests causes a pointer grab that will generate enter, motion and
547	  leave events on the data device. A data source is supplied as
548	  argument to start_drag, and data offers associated with it are
549	  supplied to clients surfaces under the pointer in the
550	  <function>wl_data_device.enter</function> event. The data offer
551	  is introduced to the client prior to the enter event with the
552	  <function>wl_data_device.data_offer</function> event.
553	</para>
554	<para>
555	  Clients are expected to provide feedback to the data sending client
556	  by calling the <function>wl_data_offer.accept</function> request with
557	  a mime type it accepts. If none of the advertised mime types is
558	  supported by the receiving client, it should supply NULL to the
559	  accept request. The accept request causes the sending client to
560	  receive a <function>wl_data_source.target</function> event with the
561	  chosen mime type.
562	</para>
563	<para>
564	  When the drag ends, the receiving client receives a
565	  <function>wl_data_device.drop</function> event at which it is expected
566	  to transfer the data using the
567	  <function>wl_data_offer.receive</function> request.
568	</para>
569      </section>
570    </section>
571  </section>
572</chapter>
573