Imported Upstream version 1.15.1
[deb_xorg-server.git] / doc / Xserver-spec.xml
CommitLineData
a09e091a
JB
1<?xml version="1.0" encoding="ISO-8859-1"?>
2<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.3//EN"
3 "http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd" [
4 <!ENTITY % xorg-defs SYSTEM "defs.ent"> %xorg-defs;
5 <!ENTITY % defs SYSTEM "/xserver/doc/xml/xserver.ent"> %defs;
6]>
7
8<article>
9 <articleinfo>
10 <title>Definition of the Porting Layer for the X v11 Sample Server</title>
11 <titleabbrev>X Porting Layer</titleabbrev>
12 <author>
13 <firstname>Susan</firstname><surname>Angebranndt</surname>
14 <affiliation><orgname>Digital Equipment Corporation</orgname></affiliation>
15 </author>
16 <author>
17 <firstname>Raymond</firstname><surname>Drewry</surname>
18 <affiliation><orgname>Digital Equipment Corporation</orgname></affiliation>
19 </author>
20 <author>
21 <firstname>Philip</firstname><surname>Karlton</surname>
22 <affiliation><orgname>Digital Equipment Corporation</orgname></affiliation>
23 </author>
24 <author>
25 <firstname>Todd</firstname><surname>Newman</surname>
26 <affiliation><orgname>Digital Equipment Corporation</orgname></affiliation>
27 </author>
28 <author>
29 <firstname>Bob</firstname><surname>Scheifler</surname>
30 <affiliation><orgname>Massachusetts Institute of Technology</orgname></affiliation>
31 </author>
32 <author>
33 <firstname>Keith</firstname><surname>Packard</surname>
34 <affiliation><orgname>MIT X Consortium</orgname></affiliation>
35 </author>
36 <author>
37 <firstname>David</firstname><othername>P.</othername><surname>Wiggins</surname>
38 <affiliation><orgname>X Consortium</orgname></affiliation>
39 </author>
40 <author>
41 <firstname>Jim</firstname><surname>Gettys</surname>
42 <affiliation><orgname>X.org Foundation and Hewlett Packard</orgname></affiliation>
43 </author>
44 <publisher><publishername>The X.Org Foundation</publishername></publisher>
45 <releaseinfo>X Version 11, Release &fullrelvers;</releaseinfo>
46 <releaseinfo>X Server Version &xserver.version;</releaseinfo>
47 <copyright><year>1994</year><holder>X Consortium, Inc.</holder></copyright>
48 <copyright><year>2004</year><holder>X.org Foundation, Inc.</holder></copyright>
49 <legalnotice>
50 <para>Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ``Software''), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:</para>
51 <para>The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.</para>
52 <para>THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</para>
53 <para>LK201 and DEC are trademarks of Digital Equipment Corporation. Macintosh and Apple are trademarks of Apple Computer, Inc. PostScript is a trademark of Adobe Systems, Inc. Ethernet is a trademark of Xerox Corporation. X Window System is a trademark of the X.org Foundation, Inc. Cray is a trademark of Cray Research, Inc.</para>
54 </legalnotice>
55 <pubdate>&xserver.reldate;</pubdate>
56 <revhistory>
57 <revision>
58 <revnumber>1.0</revnumber>
59 <date>27 Oct 2004</date>
60 <authorinitials>sa</authorinitials>
61 <revremark>Initial Version</revremark>
62 </revision>
63 <revision>
64 <revnumber>1.1</revnumber>
65 <date>27 Oct 2004</date>
66 <authorinitials>bs</authorinitials>
67 <revremark>Minor Revisions</revremark>
68 </revision>
69 <revision>
70 <revnumber>2.0</revnumber>
71 <date>27 Oct 2004</date>
72 <authorinitials>kp</authorinitials>
73 <revremark>Revised for Release 4 and 5</revremark>
74 </revision>
75 <revision>
76 <revnumber>3.0</revnumber>
77 <date>27 Oct 2004</date>
78 <authorinitials>dpw</authorinitials>
79 <revremark>Revised for Release 6</revremark>
80 </revision>
81 <revision>
82 <revnumber>3.1</revnumber>
83 <date>27 Oct 2004</date>
84 <authorinitials>jg</authorinitials>
85 <revremark>Revised for Release 6.8.2</revremark>
86 </revision>
87 <revision>
88 <revnumber>3.2</revnumber>
89 <date>17 Dec 2006</date>
90 <authorinitials>efw</authorinitials>
91 <revremark>DocBook conversion</revremark>
92 </revision>
93 <revision>
94 <revnumber>3.3</revnumber>
95 <date>17 Feb 2008</date>
96 <authorinitials>aj</authorinitials>
97 <revremark>Revised for backing store changes</revremark>
98 </revision>
99 <revision>
100 <revnumber>3.4</revnumber>
101 <date>31 Mar 2008</date>
102 <authorinitials>efw</authorinitials>
103 <revremark>Revised for devPrivates changes</revremark>
104 </revision>
105 <revision>
106 <revnumber>3.5</revnumber>
107 <date>July 2010</date>
108 <authorinitials>ac</authorinitials>
109 <revremark>Revised for Xorg 1.9 devPrivates changes
110 and 1.8 CreateNewResourceType changes</revremark>
111 </revision>
112 <revision>
113 <revnumber>3.6</revnumber>
114 <date>July 2012</date>
115 <authorinitials>kp</authorinitials>
116 <revremark>Revised for X server 1.13 screen-specific devPrivates changes</revremark>
117 </revision>
118 </revhistory>
119 <abstract>
120 <para>The following document explains the structure of the X Window System display server and the interfaces among the larger pieces. It is intended as a reference for programmers who are implementing an X Display Server on their workstation hardware. It is included with the X Window System source tape, along with the document "Strategies for Porting the X v11 Sample Server." The order in which you should read these documents is:
121 <orderedlist>
122 <listitem><para>Read the first section of the "Strategies for Porting" document (Overview of Porting Process).</para></listitem>
123 <listitem><para>Skim over this document (the Definition document).</para></listitem>
124 <listitem><para>Skim over the remainder of the Strategies document.</para></listitem>
125 <listitem><para>Start planning and working, referring to the Strategies and Definition documents.</para></listitem>
126 </orderedlist>
127 You may also want to look at the following documents:
128 <itemizedlist>
129 <listitem><para>"The X Window System" for an overview of X.</para></listitem>
130 <listitem><para>"Xlib - C Language X Interface" for a view of what the client programmer sees.</para></listitem>
131 <listitem><para>"X Window System Protocol" for a terse description of the byte stream protocol between the client and server.</para></listitem>
132 </itemizedlist>
133 </para>
134 <para>To understand this document and the accompanying source code, you should know the C language. You should be familiar with 2D graphics and windowing concepts such as clipping, bitmaps, fonts, etc. You should have a general knowledge of the X Window System. To implement the server code on your hardware, you need to know a lot about your hardware, its graphic display device(s), and (possibly) its networking and multitasking facilities. This document depends a lot on the source code, so you should have a listing of the code handy.</para>
135 <para>Some source in the distribution is directly compilable on your machine. Some of it will require modification. Other parts may have to be completely written from scratch. The distribution also includes source for a sample implementation of a display server which runs on a very wide variety of color and monochrome displays on Linux and *BSD which you will find useful for implementing any type of X server.</para>
136 <para>Note to the 2008 edition: at this time this document must be considered incomplete, though improved over the 2004 edition. In particular, the new Render extension is still lacking good documentation, and has become vital to high performance X implementations. Modern applications and desktop environments are now much more sensitive to good implementation of the Render extension than in most operations of the old X graphics model. The shadow frame buffer implementation is also very useful in many circumstances, and also needs documentation. We hope to rectify these shortcomings in our documentation in the future. Help would be greatly appreciated.</para>
137 </abstract>
138 </articleinfo>
139
140<!-- Original authorship information:
141
142.OF 'Porting Layer Definition'- % -'October 27, 2004'
143Definition of the Porting Layer
144for the X v11 Sample Server
145Susan Angebranndt
146Raymond Drewry
147Philip Karlton
148Todd Newman
149Digital Equipment Corporation
150
151minor revisions by
152Bob Scheifler
153Massachusetts Institute of Technology
154
155Revised for Release 4 and Release 5 by
156Keith Packard
157MIT X Consortium
158
159Revised for Release 6 by
160David P. Wiggins
161X Consortium
162
163Minor Revisions for Release 6.8.2 by
164Jim Gettys
165X.org Foundation and Hewlett Packard
166-->
167
168<section>
169 <title>The X Window System</title>
170<para>
171The X Window System, or simply "X," is a
172windowing system that provides high-performance, high-level,
173device-independent graphics.
174</para>
175<para>
176X is a windowing system designed for bitmapped graphic displays.
177The display can have a
178simple, monochrome display or it can have a color display with up to 32 bits
179per pixel with a special graphics processor doing the work. (In this
180document, monochrome means a black and white display with one bit per pixel.
181Even though the usual meaning of monochrome is more general, this special
182case is so common that we decided to reserve the word for this purpose.)
183In practice, monochrome displays are now almost unheard of, with 4 bit
184gray scale displays being the low end.
185</para>
186<para>
187X is designed for a networking environment where
188users can run applications on machines other than their own workstations.
189Sometimes, the connection is over an Ethernet network with a protocol such as TCP/IP;
190but, any "reliable" byte stream is allowable.
191A high-bandwidth byte stream is preferable; RS-232 at
1929600 baud would be slow without compression techniques.
193</para>
194<para>
195X by itself allows great freedom of design.
196For instance, it does not include any user interface standard.
197Its intent is to "provide mechanism, not policy."
198By making it general, it can be the foundation for a wide
199variety of interactive software.
200</para>
201<para>
202For a more detailed overview, see the document "The X Window System."
203For details on the byte stream protocol, see "X Window System protocol."
204</para>
205</section>
206<section>
207<title>Overview of the Server</title>
208<para>
209The display server
210manages windows and simple graphics requests
211for the user on behalf of different client applications.
212The client applications can be running on any machine on the network.
213The server mainly does three things:
214<itemizedlist>
215 <listitem><para>Responds to protocol requests from existing clients (mostly graphic and text drawing commands)</para></listitem>
216 <listitem><para>Sends device input (keystrokes and mouse actions) and other events to existing clients</para></listitem>
217 <listitem><para>Maintains client connections</para></listitem>
218</itemizedlist>
219</para>
220<para>
221The server code is organized into four major pieces:
222<itemizedlist>
223 <listitem><para>Device Independent (DIX) layer - code shared among all implementations</para></listitem>
224 <listitem><para>Operating System (OS) layer - code that is different for each operating system but is shared among all graphic devices for this operating system</para></listitem>
225 <listitem><para>Device Dependent (DDX) layer - code that is (potentially) different for each combination of operating system and graphic device</para></listitem>
226 <listitem><para>Extension Interface - a standard way to add features to the X server</para></listitem>
227</itemizedlist>
228</para>
229<para>
230The "porting layer" consists of the OS and DDX layers; these are
231actually parallel and neither one is on top of the other.
232The DIX layer is intended to be portable
233without change to target systems and is not
234detailed here, although several routines
235in DIX that are called by DDX are
236documented.
237Extensions incorporate new functionality into the server; and require
238additional functionality over a simple DDX.
239</para>
240<para>
241The following sections outline the functions of the layers.
242Section 3 briefly tells what you need to know about the DIX layer.
243The OS layer is explained in Section 4.
244Section 5 gives the theory of operation and procedural interface for the
245DDX layer.
246Section 6 describes the functions which exist for the extension writer.
247</para>
248</section>
249
250<section>
251 <title>DIX Layer</title>
252<para>
253The DIX layer is the machine and device independent part of X.
254The source should be common to all operating systems and devices.
255The port process should not include changes to this part, therefore internal interfaces to DIX
256modules are not discussed, except for public interfaces to the DDX and the OS layers.
257The functions described in this section are available for extension writers to use.
258</para>
259<para>
260In the process of getting your server to work, if
261you think that DIX must be modified for purposes other than bug fixes,
262you may be doing something wrong.
263Keep looking for a more compatible solution.
264When the next release of the X server code is available,
265you should be able to just drop in the new DIX code and compile it.
266If you change DIX,
267you will have to remember what changes you made and will have
268to change the new sources before you can update to the new version.
269</para>
270<para>
271The heart of the DIX code is a loop called the dispatch loop.
272Each time the processor goes around the loop, it sends off accumulated input events
273from the input devices to the clients, and it processes requests from the clients.
274This loop is the most organized way for the server to
275process the asynchronous requests that
276it needs to process.
277Most of these operations are performed by OS and DDX routines that you must supply.
278</para>
279<section>
280 <title>Server Resource System</title>
281<para>
282X resources are C structs inside the server.
283Client applications create and manipulate these objects
284according to the rules of the X byte stream protocol.
285Client applications refer to resources with resource IDs,
286which are 32-bit integers that are sent over the network.
287Within the server, of course, they are just C structs, and we refer to them
288by pointers.
289</para>
290<section>
291 <title>Pre-Defined Resource Types</title>
292<para>
293The DDX layer has several kinds of resources:
294<itemizedlist>
295<listitem><para>Window</para></listitem>
296<listitem><para>Pixmap</para></listitem>
297<listitem><para>Screen</para></listitem>
298<listitem><para>Device</para></listitem>
299<listitem><para>Colormap</para></listitem>
300<listitem><para>Font</para></listitem>
301<listitem><para>Cursor</para></listitem>
302<listitem><para>Graphics Contexts</para></listitem>
303</itemizedlist>
304</para>
305<para>
306The type names of the more
307important server
308structs usually end in "Rec," such as "DeviceRec;"
309the pointer types usually end in "Ptr," such as "DevicePtr."
310</para>
311<para>
312The structs and
313important defined constants are declared
314in .h files that have names that suggest the name of the object.
315For instance, there are two .h files for windows,
316window.h and windowstr.h.
317window.h defines only what needs to be defined in order to use windows
318without peeking inside of them;
319windowstr.h defines the structs with all of their components in great detail
320for those who need it.
321</para>
322<para>
323Three kinds of fields are in these structs:
324<itemizedlist>
325<listitem><para>Attribute fields - struct fields that contain values like normal structs</para></listitem>
326<listitem><para>Pointers to procedures, or structures of procedures, that operate on the object</para></listitem>
327<listitem><para>A single private field or a devPrivates list (see <xref linkend="wrappers_and_privates"/>)
328used by your DDX code to store private data.</para></listitem>
329</itemizedlist>
330</para>
331<para>
332DIX calls through
333the struct's procedure pointers to do its tasks.
334These procedures are set either directly or indirectly by DDX procedures.
335Most of
336the procedures described in the remainder of this
337document are accessed through one of these structs.
338For example, the procedure to create a pixmap
339is attached to a ScreenRec and might be called by using the expression
340</para>
341<para>
342<blockquote>
343<programlisting>(* pScreen->CreatePixmap)(pScreen, width, height, depth).</programlisting>
344</blockquote>
345</para>
346<para>
347All procedure pointers must be set to some routine unless noted otherwise;
348a null pointer will have unfortunate consequences.
349</para>
350<para>
351Procedure routines will be indicated in the documentation by this convention:
352<blockquote>
353<programlisting>void pScreen->MyScreenRoutine(arg, arg, ...)</programlisting>
354</blockquote>
355as opposed to a free routine, not in a data structure:
356<blockquote>
357<programlisting>void MyFreeRoutine(arg, arg, ...)</programlisting>
358</blockquote>
359</para>
360<para>
361The attribute fields are mostly set by DIX; DDX should not modify them
362unless noted otherwise.
363</para>
364</section>
365<section>
366 <title>Creating Resources and Resource Types</title>
367<para>
368These functions should also be called from your extensionInitProc to
369allocate all of the various resource classes and types required for
370the extension. Each time the server resets, these types must be reallocated
371as the old allocations will have been discarded.
372Resource types are integer values starting at 1. Get
373a resource type by calling
374<blockquote><programlisting>
375
376 RESTYPE CreateNewResourceType(deleteFunc, char *name)
377
378</programlisting></blockquote>
379deleteFunc will be called to destroy all resources with this
380type. name will be used to identify this type of resource
381to clients using the X-Resource extension, to security
382extensions such as SELinux, and to tracing frameworks such as DTrace.
383[The name argument was added in xorg-server 1.8.]
384</para>
385<para>
386Resource classes are masks starting at 1 &lt;&lt; 31 which can
387be or'ed with any resource type to provide attributes for the
388type. To allocate a new class bit, call
389<blockquote><programlisting>
390
391 RESTYPE CreateNewResourceClass()
392
393</programlisting></blockquote>
394</para>
395<para>
396There are two ways of looking up resources, by type or
397by class. Classes are non-exclusive subsets of the space of
398all resources, so you can lookup the union of multiple classes.
399(RC_ANY is the union of all classes).</para>
400<para>
401Note that the appropriate class bits must be or'ed into the value returned
402by CreateNewResourceType when calling resource lookup functions.</para>
403<para>
404If you need to create a ``private'' resource ID for internal use, you
405can call FakeClientID.
406<blockquote><programlisting>
407
408 XID FakeClientID(client)
409 int client;
410
411</programlisting></blockquote>
412This allocates from ID space reserved for the server.</para>
413<para>
414To associate a resource value with an ID, use AddResource.
415<blockquote><programlisting>
416
417 Bool AddResource(id, type, value)
418 XID id;
419 RESTYPE type;
420 pointer value;
421
422</programlisting></blockquote>
423The type should be the full type of the resource, including any class
424bits. If AddResource fails to allocate memory to store the resource,
425it will call the deleteFunc for the type, and then return False.</para>
426<para>
427To free a resource, use one of the following.
428<blockquote><programlisting>
429
430 void FreeResource(id, skipDeleteFuncType)
431 XID id;
432 RESTYPE skipDeleteFuncType;
433
434 void FreeResourceByType(id, type, skipFree)
435 XID id;
436 RESTYPE type;
437 Bool skipFree;
438
439</programlisting></blockquote>
440FreeResource frees all resources matching the given id, regardless of
441type; the type's deleteFunc will be called on each matching resource,
442except that skipDeleteFuncType can be set to a single type for which
443the deleteFunc should not be called (otherwise pass RT_NONE).
444FreeResourceByType frees a specific resource matching a given id
445and type; if skipFree is true, then the deleteFunc is not called.
446</para>
447</section>
448<section>
449 <title>Looking Up Resources</title>
450<para>
451To look up a resource, use one of the following.
452<blockquote><programlisting>
453
454 int dixLookupResourceByType(
455 pointer *result,
456 XID id,
457 RESTYPE rtype,
458 ClientPtr client,
459 Mask access_mode);
460
461 int dixLookupResourceByClass(
462 pointer *result,
463 XID id,
464 RESTYPE rclass,
465 ClientPtr client,
466 Mask access_mode);
467
468</programlisting></blockquote>
469dixLookupResourceByType finds a resource with the given id and exact type.
470dixLookupResourceByClass finds a resource with the given id whose type is
471included in any one of the specified classes.
472The client and access_mode must be provided to allow security extensions to
473check if the client has the right privileges for the requested access.
474The bitmask values defined in the dixaccess.h header are or'ed together
475to define the requested access_mode.
476</para>
477</section>
478</section>
479<section>
480 <title>Callback Manager</title>
481<para>
482To satisfy a growing number of requests for the introduction of ad hoc
483notification style hooks in the server, a generic callback manager was
484introduced in R6. A callback list object can be introduced for each
485new hook that is desired, and other modules in the server can register
486interest in the new callback list. The following functions support
487these operations.</para>
488<para>
489Before getting bogged down in the interface details, an typical usage
490example should establish the framework. Let's look at the
491ClientStateCallback in dix/dispatch.c. The purpose of this particular
492callback is to notify interested parties when a client's state
493(initial, running, gone) changes. The callback is "created" in this
494case by simply declaring a variable:
495<blockquote><programlisting>
496 CallbackListPtr ClientStateCallback;
497</programlisting></blockquote>
498</para>
499<para>
500Whenever the client's state changes, the following code appears, which notifies
501all interested parties of the change:
502<blockquote><programlisting>
503 if (ClientStateCallback) CallCallbacks(&amp;ClientStateCallback, (pointer)client);
504</programlisting></blockquote>
505</para>
506<para>
507Interested parties subscribe to the ClientStateCallback list by saying:
508<blockquote><programlisting>
509 AddCallback(&amp;ClientStateCallback, func, data);
510</programlisting></blockquote>
511</para>
512<para>
513When CallCallbacks is invoked on the list, func will be called thusly:
514<blockquote><programlisting>
515 (*func)(&amp;ClientStateCallback, data, client)
516</programlisting></blockquote>
517</para>
518<para>
519Now for the details.
520<blockquote><programlisting>
521
522 Bool AddCallback(pcbl, callback, subscriber_data)
523 CallbackListPtr *pcbl;
524 CallbackProcPtr callback;
525 pointer subscriber_data;
526
527</programlisting></blockquote>
528Adds the (callback, subscriber_data) pair to the given callback list. Creates the callback
529list if it doesn't exist. Returns TRUE if successful.</para>
530<para>
531<blockquote><programlisting>
532
533 Bool DeleteCallback(pcbl, callback, subscriber_data)
534 CallbackListPtr *pcbl;
535 CallbackProcPtr callback;
536 pointer subscriber_data;
537
538</programlisting></blockquote>
539Removes the (callback, data) pair to the given callback list if present.
540Returns TRUE if (callback, data) was found.</para>
541<para>
542<blockquote><programlisting>
543
544 void CallCallbacks(pcbl, call_data)
545 CallbackListPtr *pcbl;
546 pointer call_data;
547
548</programlisting></blockquote>
549For each callback currently registered on the given callback list, call
550it as follows:
551<blockquote><programlisting>
552
553 (*callback)(pcbl, subscriber_data, call_data);
554</programlisting></blockquote>
555</para>
556<para>
557<blockquote><programlisting>
558 void DeleteCallbackList(pcbl)
559 CallbackListPtr *pcbl;
560
561</programlisting></blockquote>
562Destroys the given callback list.</para>
563</section>
564<section>
565 <title>Extension Interfaces</title>
566<para>
567This function should be called from your extensionInitProc which
568should be called by InitExtensions.
569<blockquote><programlisting>
570
571 ExtensionEntry *AddExtension(name, NumEvents,NumErrors,
572 MainProc, SwappedMainProc, CloseDownProc, MinorOpcodeProc)
573
574 const char *name; /*Null terminate string; case matters*/
575 int NumEvents;
576 int NumErrors;
577 int (* MainProc)(ClientPtr);/*Called if client matches server order*/
578 int (* SwappedMainProc)(ClientPtr);/*Called if client differs from server*/
579 void (* CloseDownProc)(ExtensionEntry *);
580 unsigned short (*MinorOpcodeProc)(ClientPtr);
581
582</programlisting></blockquote>
583name is the name used by clients to refer to the extension. NumEvents is the
584number of event types used by the extension, NumErrors is the number of
585error codes needed by the extension. MainProc is called whenever a client
586accesses the major opcode assigned to the extension. SwappedMainProc is
587identical, except the client using the extension has reversed byte-sex.
588CloseDownProc is called at server reset time to deallocate any private
589storage used by the extension. MinorOpcodeProc is used by DIX to place the
590appropriate value into errors. The DIX routine StandardMinorOpcode can be
591used here which takes the minor opcode from the normal place in the request
592(i.e. just after the major opcode).</para>
593</section>
594<section>
595 <title>Macros and Other Helpers</title>
596<para>
597There are a number of macros in Xserver/include/dix.h which
598are useful to the extension writer. Ones of particular interest
599are: REQUEST, REQUEST_SIZE_MATCH, REQUEST_AT_LEAST_SIZE,
600REQUEST_FIXED_SIZE, LEGAL_NEW_RESOURCE, and
601VALIDATE_DRAWABLE_AND_GC. Useful byte swapping macros can be found
602in Xserver/include/dix.h: WriteReplyToClient and WriteSwappedDataToClient; and
603in Xserver/include/misc.h: lswapl, lswaps, LengthRestB, LengthRestS,
604LengthRestL, SwapRestS, SwapRestL, swapl, swaps, cpswapl, and cpswaps.</para>
605</section>
606</section>
607
608<section>
609 <title>OS Layer</title>
610<para>
611This part of the source consists of a few routines that you have to rewrite
612for each operating system.
613These OS functions maintain the client connections and schedule work
614to be done for clients.
615They also provide an interface to font files,
616font name to file name translation, and
617low level memory management.
618<blockquote>
619<programlisting>void OsInit()</programlisting>
620</blockquote>
621OsInit initializes your OS code, performing whatever tasks need to be done.
622Frequently there is not much to be done.
623The sample server implementation is in Xserver/os/osinit.c.
624</para>
625<section>
626 <title>Scheduling and Request Delivery</title>
627<para>
628The main dispatch loop in DIX creates the illusion of multitasking between
629different windows, while the server is itself but a single process.
630The dispatch loop breaks up the work for each client into small digestible parts.
631Some parts are requests from a client, such as individual graphic commands.
632Some parts are events delivered to the client, such as keystrokes from the user.
633The processing of events and requests for different
634clients can be interleaved with one another so true multitasking
635is not needed in the server.
636</para>
637<para>
638You must supply some of the pieces for proper scheduling between clients.
639<blockquote>
640<programlisting>
641 int WaitForSomething(pClientReady)
642 int *pClientReady;
643</programlisting>
644</blockquote>
645</para>
646<para>
647WaitForSomething is the scheduler procedure you must write that will
648suspend your server process until something needs to be done.
649This call should
650make the server suspend until one or more of the following occurs:
651<itemizedlist>
652<listitem><para>There is an input event from the user or hardware (see SetInputCheck())</para></listitem>
653<listitem><para>There are requests waiting from known clients, in which case you should return a count of clients stored in pClientReady</para></listitem>
654<listitem><para>A new client tries to connect, in which case you should create the client and then continue waiting</para></listitem>
655</itemizedlist>
656</para>
657<para>
658Before WaitForSomething() computes the masks to pass to select, poll or
659similar operating system interface, it needs to
660see if there is anything to do on the work queue; if so, it must call a DIX
661routine called ProcessWorkQueue.
662<blockquote>
663<programlisting>
664 extern WorkQueuePtr workQueue;
665
666 if (workQueue)
667 ProcessWorkQueue ();
668</programlisting>
669</blockquote>
670</para>
671<para>
672If WaitForSomething() decides it is about to do something that might block
673(in the sample server, before it calls select() or poll) it must call a DIX
674routine called BlockHandler().
675<blockquote>
676<programlisting>
677 void BlockHandler(pTimeout, pReadmask)
678 pointer pTimeout;
679 pointer pReadmask;
680</programlisting>
681</blockquote>
682The types of the arguments are for agreement between the OS and DDX
683implementations, but the pTimeout is a pointer to the information
684determining how long the block is allowed to last, and the
685pReadmask is a pointer to the information describing the descriptors
686that will be waited on.
687</para>
688<para>
689In the sample server, pTimeout is a pointer, and pReadmask is
690the address of the select() mask for reading.
691</para>
692<para>
693The DIX BlockHandler() iterates through the Screens, for each one calling
694its BlockHandler. A BlockHandler is declared thus:
695<blockquote>
696<programlisting>
697 void xxxBlockHandler(pScreen, pTimeout, pReadmask)
698 ScreenPtr pScreen;
699 pointer pTimeout;
700 pointer pReadmask;
701</programlisting>
702</blockquote>
703The arguments are a pointer to the Screen, and the arguments to the
704DIX BlockHandler().
705</para>
706<para>
707Immediately after WaitForSomething returns from the
708block, even if it didn't actually block, it must call the DIX routine
709WakeupHandler().
710<blockquote>
711<programlisting>
712 void WakeupHandler(result, pReadmask)
713 int result;
714 pointer pReadmask;
715</programlisting>
716</blockquote>
717Once again, the types are not specified by DIX. The result is the
718success indicator for the thing that (may have) blocked,
719and the pReadmask is a mask of the descriptors that came active.
720In the sample server, result is the result from select() (or equivalent
721operating system function), and pReadmask is
722the address of the select() mask for reading.
723</para>
724<para>
725The DIX WakeupHandler() calls each Screen's
726WakeupHandler. A WakeupHandler is declared thus:
727<blockquote>
728<programlisting>
729 void xxxWakeupHandler(pScreen, result, pReadmask)
730 ScreenPtr pScreen;
731 unsigned long result;
732 pointer pReadmask;
733</programlisting>
734</blockquote>
735The arguments are the Screen, of the Screen, and the arguments to
736the DIX WakeupHandler().
737</para>
738<para>
739In addition to the per-screen BlockHandlers, any module may register
740block and wakeup handlers (only together) using:
741<blockquote>
742<programlisting>
743 Bool RegisterBlockAndWakeupHandlers (blockHandler, wakeupHandler, blockData)
744 BlockHandlerProcPtr blockHandler;
745 WakeupHandlerProcPtr wakeupHandler;
746 pointer blockData;
747</programlisting>
748</blockquote>
749A FALSE return code indicates that the registration failed for lack of
750memory. To remove a registered Block handler at other than server reset time
751(when they are all removed automatically), use:
752<blockquote>
753<programlisting>
754 RemoveBlockAndWakeupHandlers (blockHandler, wakeupHandler, blockData)
755 BlockHandlerProcPtr blockHandler;
756 WakeupHandlerProcPtr wakeupHandler;
757 pointer blockData;
758</programlisting>
759</blockquote>
760All three arguments must match the values passed to
761RegisterBlockAndWakeupHandlers.
762</para>
763<para>
764These registered block handlers are called after the per-screen handlers:
765<blockquote>
766<programlisting>
767 void (*BlockHandler) (blockData, pptv, pReadmask)
768 pointer blockData;
769 OsTimerPtr pptv;
770 pointer pReadmask;
771</programlisting>
772</blockquote>
773</para>
774<para>
775Sometimes block handlers need to adjust the time in a OSTimePtr structure,
776which on UNIX family systems is generally represented by a struct timeval
777consisting of seconds and microseconds in 32 bit values.
778As a convenience to reduce error prone struct timeval computations which
779require modulus arithmetic and correct overflow behavior in the face of
780millisecond wrapping through 32 bits,
781<blockquote><programlisting>
782
783 void AdjustWaitForDelay(pointer /*waitTime*, unsigned long /* newdelay */)
784
785</programlisting></blockquote>
786has been provided.
787</para>
788<para>
789Any wakeup handlers registered with RegisterBlockAndWakeupHandlers will
790be called before the Screen handlers:
791<blockquote><programlisting>
792
793 void (*WakeupHandler) (blockData, err, pReadmask)
794 pointer blockData;
795 int err;
796 pointer pReadmask;
797</programlisting></blockquote>
798</para>
799<para>
800The WaitForSomething on the sample server also has a built
801in screen saver that darkens the screen if no input happens for a period of time.
802The sample server implementation is in Xserver/os/WaitFor.c.
803</para>
804<para>
805Note that WaitForSomething() may be called when you already have several
806outstanding things (events, requests, or new clients) queued up.
807For instance, your server may have just done a large graphics request,
808and it may have been a long time since WaitForSomething() was last called.
809If many clients have lots of requests queued up, DIX will only service
810some of them for a given client
811before going on to the next client (see isItTimeToYield, below).
812Therefore, WaitForSomething() will have to report that these same clients
813still have requests queued up the next time around.
814</para>
815<para>
816An implementation should return information on as
817many outstanding things as it can.
818For instance, if your implementation always checks for client data first and does not
819report any input events until there is no client data left,
820your mouse and keyboard might get locked out by an application that constantly
821barrages the server with graphics drawing requests.
822Therefore, as a general rule, input devices should always have priority over graphics
823devices.
824</para>
825<para>
826A list of indexes (client->index) for clients with data ready to be read or
827processed should be returned in pClientReady, and the count of indexes
828returned as the result value of the call.
829These are not clients that have full requests ready, but any clients who have
830any data ready to be read or processed.
831The DIX dispatcher
832will process requests from each client in turn by calling
833ReadRequestFromClient(), below.
834</para>
835<para>
836WaitForSomething() must create new clients as they are requested (by
837whatever mechanism at the transport level). A new client is created
838by calling the DIX routine:
839<blockquote><programlisting>
840
841 ClientPtr NextAvailableClient(ospriv)
842 pointer ospriv;
843</programlisting></blockquote>
844This routine returns NULL if a new client cannot be allocated (e.g. maximum
845number of clients reached). The ospriv argument will be stored into the OS
846private field (pClient->osPrivate), to store OS private information about the
847client. In the sample server, the osPrivate field contains the
848number of the socket for this client. See also "New Client Connections."
849NextAvailableClient() will call InsertFakeRequest(), so you must be
850prepared for this.
851</para>
852<para>
853If there are outstanding input events,
854you should make sure that the two SetInputCheck() locations are unequal.
855The DIX dispatcher will call your implementation of ProcessInputEvents()
856until the SetInputCheck() locations are equal.
857</para>
858<para>
859The sample server contains an implementation of WaitForSomething().
860The
861following two routines indicate to WaitForSomething() what devices should
862be waited for. fd is an OS dependent type; in the sample server
863it is an open file descriptor.
864<blockquote><programlisting>
865
866 int AddEnabledDevice(fd)
867 int fd;
868
869 int RemoveEnabledDevice(fd)
870 int fd;
871</programlisting></blockquote>
872These two routines are
873usually called by DDX from the initialize cases of the
874Input Procedures that are stored in the DeviceRec (the
875routine passed to AddInputDevice()).
876The sample server implementation of AddEnabledDevice
877and RemoveEnabledDevice are in Xserver/os/connection.c.
878</para>
879<section>
880 <title>Timer Facilities</title>
881<para>
882Similarly, the X server or an extension may need to wait for some timeout.
883Early X releases implemented this functionality using block and wakeup handlers,
884but this has been rewritten to use a general timer facilty, and the
885internal screen saver facilities reimplemented to use Timers.
886These functions are TimerInit, TimerForce, TimerSet, TimerCheck, TimerCancel,
887and TimerFree, as defined in Xserver/include/os.h. A callback function will be called
888when the timer fires, along with the current time, and a user provided argument.
889<blockquote><programlisting>
890 typedef struct _OsTimerRec *OsTimerPtr;
891
892 typedef CARD32 (*OsTimerCallback)(
893 OsTimerPtr /* timer */,
894 CARD32 /* time */,
895 pointer /* arg */);
896
897 OsTimerPtr TimerSet( OsTimerPtr /* timer */,
898 int /* flags */,
899 CARD32 /* millis */,
900 OsTimerCallback /* func */,
901 pointer /* arg */);
902
903</programlisting></blockquote>
904</para>
905<para>
906TimerSet returns a pointer to a timer structure and sets a timer to the specified time
907with the specified argument. The flags can be TimerAbsolute and TimerForceOld.
908The TimerSetOld flag controls whether if the timer is reset and the timer is pending, the
909whether the callback function will get called.
910The TimerAbsolute flag sets the callback time to an absolute time in the future rather
911than a time relative to when TimerSet is called.
912TimerFree should be called to free the memory allocated
913for the timer entry.
914<blockquote><programlisting>
915 void TimerInit(void)
916
917 Bool TimerForce(OsTimerPtr /* pTimer */)
918
919 void TimerCheck(void);
920
921 void TimerCancel(OsTimerPtr /* pTimer */)
922
923 void TimerFree(OsTimerPtr /* pTimer */)
924</programlisting></blockquote>
925</para>
926<para>
927TimerInit frees any existing timer entries. TimerForce forces a call to the timer's
928callback function and returns true if the timer entry existed, else it returns false and
929does not call the callback function. TimerCancel will cancel the specified timer.
930TimerFree calls TimerCancel and frees the specified timer.
931Calling TimerCheck will force the server to see if any timer callbacks should be called.
932</para>
933</section>
934</section>
935<section>
936 <title>New Client Connections</title>
937<para>
938The process whereby a new client-server connection starts up is
939very dependent upon what your byte stream mechanism.
940This section describes byte stream initiation using examples from the TCP/IP
941implementation on the sample server.
942</para>
943<para>
944The first thing that happens is a client initiates a connection with the server.
945How a client knows to do this depends upon your network facilities and the
946Xlib implementation.
947In a typical scenario, a user named Fred
948on his X workstation is logged onto a Cray
949supercomputer running a command shell in an X window. Fred can type shell
950commands and have the Cray respond as though the X server were a dumb terminal.
951Fred types in a command to run an X client application that was linked with Xlib.
952Xlib looks at the shell environment variable DISPLAY, which has the
953value "fredsbittube:0.0."
954The host name of Fred's workstation is "fredsbittube," and the 0s are
955for multiple screens and multiple X server processes.
956(Precisely what
957happens on your system depends upon how X and Xlib are implemented.)
958</para>
959<para>
960The client application calls a TCP routine on the
961Cray to open a TCP connection for X
962to communicate with the network node "fredsbittube."
963The TCP software on the Cray does this by looking up the TCP
964address of "fredsbittube" and sending an open request to TCP port 6000
965on fredsbittube.
966</para>
967<para>
968All X servers on TCP listen for new clients on port 6000 by default;
969this is known as a "well-known port" in IP terminology.
970</para>
971<para>
972The server receives this request from its port 6000
973and checks where it came from to see if it is on the server's list
974of "trustworthy" hosts to talk to.
975Then, it opens another port for communications with the client.
976This is the byte stream that all X communications will go over.
977</para>
978<para>
979Actually, it is a bit more complicated than that.
980Each X server process running on the host machine is called a "display."
981Each display can have more than one screen that it manages.
982"corporatehydra:3.2" represents screen 2 on display 3 on
983the multi-screened network node corporatehydra.
984The open request would be sent on well-known port number 6003.
985</para>
986<para>
987Once the byte stream is set up, what goes on does not depend very much
988upon whether or not it is TCP.
989The client sends an xConnClientPrefix struct (see Xproto.h) that has the
990version numbers for the version of Xlib it is running, some byte-ordering information,
991and two character strings used for authorization.
992If the server does not like the authorization strings
993or the version numbers do not match within the rules,
994or if anything else is wrong, it sends a failure
995response with a reason string.
996</para>
997<para>
998If the information never comes, or comes much too slowly, the connection
999should be broken off. You must implement the connection timeout. The
1000sample server implements this by keeping a timestamp for each still-connecting
1001client and, each time just before it attempts to accept new connections, it
1002closes any connection that are too old.
1003The connection timeout can be set from the command line.
1004</para>
1005<para>
1006You must implement whatever authorization schemes you want to support.
1007The sample server on the distribution tape supports a simple authorization
1008scheme. The only interface seen by DIX is:
1009<blockquote><programlisting>
1010
1011 char *
1012 ClientAuthorized(client, proto_n, auth_proto, string_n, auth_string)
1013 ClientPtr client;
1014 unsigned int proto_n;
1015 char *auth_proto;
1016 unsigned int string_n;
1017 char *auth_string;
1018</programlisting></blockquote>
1019DIX will only call this once per client, once it has read the full initial
1020connection data from the client. If the connection should be
1021accepted ClientAuthorized() should return NULL, and otherwise should
1022return an error message string.
1023</para>
1024<para>
1025Accepting new connections happens internally to WaitForSomething().
1026WaitForSomething() must call the DIX routine NextAvailableClient()
1027to create a client object.
1028Processing of the initial connection data will be handled by DIX.
1029Your OS layer must be able to map from a client
1030to whatever information your OS code needs to communicate
1031on the given byte stream to the client.
1032DIX uses this ClientPtr to refer to
1033the client from now on. The sample server uses the osPrivate field in
1034the ClientPtr to store the file descriptor for the socket, the
1035input and output buffers, and authorization information.
1036</para>
1037<para>
1038To initialize the methods you choose to allow clients to connect to
1039your server, main() calls the routine
1040<blockquote><programlisting>
1041
1042 void CreateWellKnownSockets()
1043</programlisting></blockquote>
1044This routine is called only once, and not called when the server
1045is reset. To recreate any sockets during server resets, the following
1046routine is called from the main loop:
1047<blockquote><programlisting>
1048
1049 void ResetWellKnownSockets()
1050</programlisting></blockquote>
1051Sample implementations of both of these routines are found in
1052Xserver/os/connection.c.
1053</para>
1054<para>
1055For more details, see the section called "Connection Setup" in the X protocol specification.
1056</para>
1057</section>
1058<section>
1059 <title>Reading Data from Clients</title>
1060<para>
1061Requests from the client are read in as a byte stream by the OS layer.
1062They may be in the form of several blocks of bytes delivered in sequence; requests may
1063be broken up over block boundaries or there may be many requests per block.
1064Each request carries with it length information.
1065It is the responsibility of the following routine to break it up into request blocks.
1066<blockquote><programlisting>
1067
1068 int ReadRequestFromClient(who)
1069 ClientPtr who;
1070</programlisting></blockquote>
1071</para>
1072<para>
1073You must write
1074the routine ReadRequestFromClient() to get one request from the byte stream
1075belonging to client "who."
1076You must swap the third and fourth bytes (the second 16-bit word) according to the
1077byte-swap rules of
1078the protocol to determine the length of the
1079request.
1080This length is measured in 32-bit words, not in bytes. Therefore, the
1081theoretical maximum request is 256K.
1082(However, the maximum length allowed is dependent upon the server's input
1083buffer. This size is sent to the client upon connection. The maximum
1084size is the constant MAX_REQUEST_SIZE in Xserver/include/os.h)
1085The rest of the request you return is
1086assumed NOT to be correctly swapped for internal
1087use, because that is the responsibility of DIX.
1088</para>
1089<para>
1090The 'who' argument is the ClientPtr returned from WaitForSomething.
1091The return value indicating status should be set to the (positive) byte count if the read is successful,
10920 if the read was blocked, or a negative error code if an error happened.
1093</para>
1094<para>
1095You must then store a pointer to
1096the bytes of the request in the client request buffer field;
1097who->requestBuffer. This can simply be a pointer into your buffer;
1098DIX may modify it in place but will not otherwise cause damage.
1099Of course, the request must be contiguous; you must
1100shuffle it around in your buffers if not.
1101</para>
1102<para>
1103The sample server implementation is in Xserver/os/io.c.
1104</para>
1105<section><title>Inserting Data for Clients</title>
1106<para>
1107DIX can insert data into the client stream, and can cause a "replay" of
1108the current request.
1109<blockquote><programlisting>
1110
1111 Bool InsertFakeRequest(client, data, count)
1112 ClientPtr client;
1113 char *data;
1114 int count;
1115
1116 int ResetCurrentRequest(client)
1117 ClientPtr client;
1118</programlisting></blockquote>
1119</para>
1120<para>
1121InsertFakeRequest() must insert the specified number of bytes of data
1122into the head of the input buffer for the client. This may be a
1123complete request, or it might be a partial request. For example,
1124NextAvailableCient() will insert a partial request in order to read
1125the initial connection data sent by the client. The routine returns FALSE
1126if memory could not be allocated. ResetCurrentRequest()
1127should "back up" the input buffer so that the currently executing request
1128will be reexecuted. DIX may have altered some values (e.g. the overall
1129request length), so you must recheck to see if you still have a complete
1130request. ResetCurrentRequest() should always cause a yield (isItTimeToYield).
1131</para>
1132</section>
1133</section>
1134
1135<section>
1136 <title>Sending Events, Errors And Replies To Clients</title>
1137<para>
1138<blockquote><programlisting>
1139
1140 int WriteToClient(who, n, buf)
1141 ClientPtr who;
1142 int n;
1143 char *buf;
1144</programlisting></blockquote>
1145WriteToClient should write n bytes starting at buf to the
1146ClientPtr "who".
1147It returns the number of bytes written, but for simplicity,
1148the number returned must be either the same value as the number
1149requested, or -1, signaling an error.
1150The sample server implementation is in Xserver/os/io.c.
1151</para>
1152<para>
1153<blockquote><programlisting>
1154 void SendErrorToClient(client, majorCode, minorCode, resId, errorCode)
1155 ClientPtr client;
1156 unsigned int majorCode;
1157 unsigned int minorCode;
1158 XID resId;
1159 int errorCode;
1160</programlisting></blockquote>
1161SendErrorToClient can be used to send errors back to clients,
1162although in most cases your request function should simply return
1163the error code, having set client->errorValue to the appropriate
1164error value to return to the client, and DIX will call this
1165function with the correct opcodes for you.
1166</para>
1167<para>
1168<blockquote><programlisting>
1169
1170 void FlushAllOutput()
1171
1172 void FlushIfCriticalOutputPending()
1173
1174 void SetCriticalOutputPending()
1175</programlisting></blockquote>
1176These three routines may be implemented to support buffered or delayed
1177writes to clients, but at the very least, the stubs must exist.
1178FlushAllOutput() unconditionally flushes all output to clients;
1179FlushIfCriticalOutputPending() flushes output only if
1180SetCriticalOutputPending() has be called since the last time output
1181was flushed.
1182The sample server implementation is in Xserver/os/io.c and
1183actually ignores requests to flush output on a per-client basis
1184if it knows that there
1185are requests in that client's input queue.
1186</para>
1187</section>
1188<section>
1189 <title>Font Support</title>
1190<para>
1191In the sample server, fonts are encoded in disk files or fetched from the
1192font server. The two fonts required by the server, <quote>fixed</quote>
1193and <quote>cursor</quote> are commonly compiled into the font library.
1194For disk fonts, there is one file per font, with a file name like
1195"fixed.pcf". Font server fonts are read over the network using the
1196X Font Server Protocol. The disk directories containing disk fonts and
1197the names of the font servers are listed together in the current "font path."
1198</para>
1199<para>
1200In principle, you can put all your fonts in ROM or in RAM in your server.
1201You can put them all in one library file on disk.
1202You could generate them on the fly from stroke descriptions. By placing the
1203appropriate code in the Font Library, you will automatically export fonts in
1204that format both through the X server and the Font server.
1205</para>
1206<para>
1207The code for processing fonts in different formats, as well as handling the
1208metadata files for them on disk (such as <filename>fonts.dir</filename>) is
1209located in the libXfont library, which is provided as a separately compiled
1210module. These routines are
1211shared between the X server and the Font server, so instead of this document
1212specifying what you must implement, simply refer to the font
1213library interface specification for the details. All of the interface code to the Font
1214library is contained in dix/dixfonts.c
1215</para>
1216</section>
1217<section>
1218 <title>Memory Management</title>
1219<para>
1220Memory management is based on functions in the C runtime library.
1221Xalloc(), Xrealloc(), and Xfree() are deprecated aliases for malloc(),
1222realloc(), and free(), and you should simply call the C library functions
1223directly. Consult a C runtime library reference
1224manual for more details.
1225</para>
1226<para>
1227Treat memory allocation carefully in your implementation. Memory
1228leaks can be very hard to find and are frustrating to a user. An X
1229server could be running for days or weeks without being reset, just
1230like a regular terminal. If you leak a few dozen k per day, that will
1231add up and will cause problems for users that leave their workstations
1232on.
1233</para>
1234</section>
1235<section>
1236 <title>Client Scheduling</title>
1237<para>
1238The X server
1239has the ability to schedule clients much like an operating system would,
1240suspending and restarting them without regard for the state of their input
1241buffers. This functionality allows the X server to suspend one client and
1242continue processing requests from other clients while waiting for a
1243long-term network activity (like loading a font) before continuing with the
1244first client.
1245<blockquote><programlisting>
1246 Bool isItTimeToYield;
1247</programlisting></blockquote>
1248isItTimeToYield is a global variable you can set
1249if you want to tell
1250DIX to end the client's "time slice" and start paying attention to the next client.
1251After the current request is finished, DIX will move to the next client.
1252</para>
1253<para>
1254In the sample
1255server, ReadRequestFromClient() sets isItTimeToYield after
125610 requests packets in a row are read from the same client.
1257</para>
1258<para>
1259This scheduling algorithm can have a serious effect upon performance when two
1260clients are drawing into their windows simultaneously.
1261If it allows one client to run until its request
1262queue is empty by ignoring isItTimeToYield, the client's queue may
1263in fact never empty and other clients will be blocked out.
1264On the other hand, if it switchs between different clients too quickly,
1265performance may suffer due to too much switching between contexts.
1266For example, if a graphics processor needs to be set up with drawing modes
1267before drawing, and two different clients are drawing with
1268different modes into two different windows, you may
1269switch your graphics processor modes so often that performance is impacted.
1270</para>
1271<para>
1272See the Strategies document for
1273heuristics on setting isItTimeToYield.
1274</para>
1275<para>
1276The following functions provide the ability to suspend request
1277processing on a particular client, resuming it at some later time:
1278<blockquote><programlisting>
1279
1280 int IgnoreClient (who)
1281 ClientPtr who;
1282
1283 int AttendClient (who)
1284 ClientPtr who;
1285</programlisting></blockquote>
1286Ignore client is responsible for pretending that the given client doesn't
1287exist. WaitForSomething should not return this client as ready for reading
1288and should not return if only this client is ready. AttendClient undoes
1289whatever IgnoreClient did, setting it up for input again.
1290</para>
1291<para>
1292Three functions support "process control" for X clients:
1293<blockquote><programlisting>
1294
1295 Bool ClientSleep (client, function, closure)
1296 ClientPtr client;
1297 Bool (*function)();
1298 pointer closure;
1299
1300</programlisting></blockquote>
1301This suspends the current client (the calling routine is responsible for
1302making its way back to Dispatch()). No more X requests will be processed
1303for this client until ClientWakeup is called.
1304<blockquote><programlisting>
1305
1306 Bool ClientSignal (client)
1307 ClientPtr client;
1308
1309</programlisting></blockquote>
1310This function causes a call to the (*function) parameter passed to
1311ClientSleep to be queued on the work queue. This does not automatically
1312"wakeup" the client, but the function called is free to do so by calling:
1313<blockquote><programlisting>
1314
1315 ClientWakeup (client)
1316 ClientPtr client;
1317
1318</programlisting></blockquote>
1319This re-enables X request processing for the specified client.
1320</para>
1321</section>
1322<section>
1323 <title>Other OS Functions</title>
1324<para>
1325<blockquote><programlisting>
1326 void
1327 ErrorF(char *f, ...)
1328
1329 void
1330 FatalError(char *f, ...)
1331</programlisting></blockquote>
1332You should write these three routines to provide for diagnostic output
1333from the dix and ddx layers, although implementing them to produce no
1334output will not affect the correctness of your server. ErrorF() and
1335FatalError() take a printf() type of format specification in the first
1336argument and an implementation-dependent number of arguments following
1337that. Normally, the formats passed to ErrorF() and FatalError()
1338should be terminated with a newline.
1339</para>
1340<para>
1341After printing the message arguments, FatalError() must be implemented
1342such that the server will call AbortDDX() to give the ddx layer
1343a chance to reset the hardware, and then
1344terminate the server; it must not return.
1345</para>
1346<para>
1347The sample server implementation for these routines
1348is in Xserver/os/log.c along with other routines for logging messages.
1349</para>
1350</section>
1351</section>
1352
1353<section>
1354 <title>DDX Layer</title>
1355<para>
1356This section describes the
1357interface between DIX and DDX.
1358While there may be an OS-dependent driver interface between DDX
1359and the physical device, that interface is left to the DDX
1360implementor and is not specified here.
1361</para>
1362<para>
1363The DDX layer does most of its work through procedures that are
1364pointed to by different structs.
1365As previously described, the behavior of these resources is largely determined by
1366these procedure pointers.
1367Most of these routines are for graphic display on the screen or support functions thereof.
1368The rest are for user input from input devices.
1369</para>
1370<section>
1371 <title>Input</title>
1372<para>
1373In this document "input" refers to input from the user,
1374such as mouse, keyboard, and
1375bar code readers.
1376X input devices are of several types: keyboard, pointing device, and
1377many others. The core server has support for extension devices as
1378described by the X Input Extension document; the interfaces used by
1379that extension are described elsewhere. The core devices are actually
1380implemented as two collections of devices, the mouse is a ButtonDevice,
1381a ValuatorDevice and a PtrFeedbackDevice while the keyboard is a KeyDevice,
1382a FocusDevice and a KbdFeedbackDevice. Each part implements a portion of
1383the functionality of the device. This abstraction is hidden from view for
1384core devices by DIX.
1385</para>
1386<para>
1387You, the DDX programmer, are
1388responsible for some of the routines in this section.
1389Others are DIX routines that you should call to do the things you need to do in these DDX routines.
1390Pay attention to which is which.
1391</para>
1392<section>
1393 <title>Input Device Data Structures</title>
1394<para>
1395DIX keeps a global directory of devices in a central data structure
1396called InputInfo.
1397For each device there is a device structure called a DeviceRec.
1398DIX can locate any DeviceRec through InputInfo.
1399In addition, it has a special pointer to identify the main pointing device
1400and a special pointer to identify the main keyboard.
1401</para>
1402<para>
1403The DeviceRec (Xserver/include/input.h) is a device-independent
1404structure that contains the state of an input device.
1405A DevicePtr is simply a pointer to a DeviceRec.
1406</para>
1407<para>
1408An xEvent describes an event the server reports to a client.
1409Defined in Xproto.h, it is a huge struct of union of structs that have fields for
1410all kinds of events.
1411All of the variants overlap, so that the struct is actually very small in memory.
1412</para>
1413</section>
1414<section>
1415 <title>Processing Events</title>
1416<para>
1417The main DDX input interface is the following routine:
1418<blockquote><programlisting>
1419
1420 void ProcessInputEvents()
1421</programlisting></blockquote>
1422You must write this routine to deliver input events from the user.
1423DIX calls it when input is pending (see next section), and possibly
1424even when it is not.
1425You should write it to get events from each device and deliver
1426the events to DIX.
1427To deliver the events to DIX, DDX should call the following
1428routine:
1429<blockquote><programlisting>
1430
1431 void DevicePtr->processInputProc(pEvent, device, count)
1432 xEventPtr events;
1433 DeviceIntPtr device;
1434 int count;
1435</programlisting></blockquote>
1436This is the "input proc" for the device, a DIX procedure.
1437DIX will fill in this procedure pointer to one of its own routines by
1438the time ProcessInputEvents() is called the first time.
1439Call this input proc routine as many times as needed to
1440deliver as many events as should be delivered.
1441DIX will buffer them up and send them out as needed. Count is set
1442to the number of event records which make up one atomic device event and
1443is always 1 for the core devices (see the X Input Extension for descriptions
1444of devices which may use count &#x3E; 1).
1445</para>
1446<para>
1447For example, your ProcessInputEvents() routine might check the mouse and the
1448keyboard.
1449If the keyboard had several keystrokes queued up, it could just call
1450the keyboard's processInputProc as many times as needed to flush its internal queue.
1451</para>
1452<para>
1453event is an xEvent struct you pass to the input proc.
1454When the input proc returns, it is finished with the event rec, and you can fill
1455in new values and call the input proc again with it.
1456</para>
1457<para>
1458You should deliver the events in the same order that they were generated.
1459</para>
1460<para>
1461For keyboard and pointing devices the xEvent variant should be keyButtonPointer.
1462Fill in the following fields in the xEvent record:
1463<itemizedlist>
1464
1465<listitem><para>type - is one of the following: KeyPress, KeyRelease, ButtonPress,
1466 ButtonRelease, or MotionNotify</para></listitem>
1467<listitem><para>detail - for KeyPress or KeyRelease fields, this should be the
1468 key number (not the ASCII code); otherwise unused</para></listitem>
1469<listitem><para>time - is the time that the event happened (32-bits, in milliseconds, arbitrary origin)</para></listitem>
1470<listitem><para>rootX - is the x coordinate of cursor</para></listitem>
1471<listitem><para>rootY - is the y coordinate of cursor</para></listitem>
1472
1473</itemizedlist>
1474The rest of the fields are filled in by DIX.
1475</para>
1476<para>
1477The time stamp is maintained by your code in the DDX layer, and it is your responsibility to
1478stamp all events correctly.
1479</para>
1480<para>
1481The x and y coordinates of the pointing device and the time must be filled in for all event types
1482including keyboard events.
1483</para>
1484<para>
1485The pointing device must report all button press and release events.
1486In addition, it should report a MotionNotify event every time it gets called
1487if the pointing device has moved since the last notify.
1488Intermediate pointing device moves are stored in a special GetMotionEvents buffer,
1489because most client programs are not interested in them.
1490</para>
1491<para>
1492There are quite a collection of sample implementations of this routine,
1493one for each supported device.
1494</para>
1495</section>
1496<section>
1497<title>Telling DIX When Input is Pending</title>
1498<para>
1499In the server's dispatch loop, DIX checks to see
1500if there is any device input pending whenever WaitForSomething() returns.
1501If the check says that input is pending, DIX calls the
1502DDX routine ProcessInputEvents().
1503</para>
1504<para>
1505This check for pending input must be very quick; a procedure call
1506is too slow.
1507The code that does the check is a hardwired IF
1508statement in DIX code that simply compares the values
1509pointed to by two pointers.
1510If the values are different, then it assumes that input is pending and
1511ProcessInputEvents() is called by DIX.
1512</para>
1513<para>
1514You must pass pointers to DIX to tell it what values to compare.
1515The following procedure
1516is used to set these pointers:
1517<blockquote><programlisting>
1518
1519 void SetInputCheck(p1, p2)
1520 long *p1, *p2;
1521</programlisting></blockquote>
1522You should call it sometime during initialization to indicate to DIX the
1523correct locations to check.
1524You should
1525pay special attention to the size of what they actually point to,
1526because the locations are assumed to be longs.
1527</para>
1528<para>
1529These two pointers are initialized by DIX
1530to point to arbitrary values that
1531are different.
1532In other words, if you forget to call this routine during initialization,
1533the worst thing that will happen is that
1534ProcessInputEvents will be called when
1535there are no events to process.
1536</para>
1537<para>
1538p1 and p2 might
1539point at the head and tail of some shared
1540memory queue.
1541Another use would be to have one point at a constant 0, with the
1542other pointing at some mask containing 1s
1543for each input device that has
1544something pending.
1545</para>
1546<para>
1547The DDX layer of the sample server calls SetInputCheck()
1548once when the
1549server's private internal queue is initialized.
1550It passes pointers to the queue's head and tail. See Xserver/mi/mieq.c.
1551</para>
1552<para>
1553<blockquote><programlisting>
1554 int TimeSinceLastInputEvent()
1555</programlisting></blockquote>
1556DDX must time stamp all hardware input
1557events. But DIX sometimes needs to know the
1558time and the OS layer needs to know the time since the last hardware
1559input event in
1560order for the screen saver to work. TimeSinceLastInputEvent() returns
1561the this time in milliseconds.
1562</para>
1563</section>
1564<section>
1565 <title>Controlling Input Devices</title>
1566<para>
1567You must write four routines to do various device-specific
1568things with the keyboard and pointing device.
1569They can have any name you wish because
1570you pass the procedure pointers to DIX routines.
1571</para>
1572<para>
1573<blockquote><programlisting>
1574
1575 int pInternalDevice->valuator->GetMotionProc(pdevice, coords, start, stop, pScreen)
1576 DeviceIntPtr pdevice;
1577 xTimecoord * coords;
1578 unsigned long start;
1579 unsigned long stop;
1580 ScreenPtr pScreen;
1581</programlisting></blockquote>
1582You write this DDX routine to fill in coords with all the motion
1583events that have times (32-bit count of milliseconds) between time
1584start and time stop. It should return the number of motion events
1585returned. If there is no motion events support, this routine should
1586do nothing and return zero. The maximum number of coords to return is
1587set in InitPointerDeviceStruct(), below.
1588</para>
1589<para>
1590When the user drags the pointing device, the cursor position
1591theoretically sweeps through an infinite number of points. Normally,
1592a client that is concerned with points other than the starting and
1593ending points will receive a pointer-move event only as often as the
1594server generates them. (Move events do not queue up; each new one
1595replaces the last in the queue.) A server, if desired, can implement
1596a scheme to save these intermediate events in a motion buffer. A
1597client application, like a paint program, may then request that these
1598events be delivered to it through the GetMotionProc routine.
1599</para>
1600<para>
1601<blockquote><programlisting>
1602
1603 void pInternalDevice->bell->BellProc(percent, pDevice, ctrl, unknown)
1604 int percent;
1605 DeviceIntPtr pDevice;
1606 pointer ctrl;
1607 int class;
1608</programlisting></blockquote>
1609You need to write this routine to ring the bell on the keyboard.
1610loud is a number from 0 to 100, with 100 being the loudest.
1611Class is either BellFeedbackClass or KbdFeedbackClass (from XI.h).
1612</para>
1613<para>
1614<blockquote><programlisting>
1615
1616 void pInternalDevice->somedevice->CtrlProc(device, ctrl)
1617 DevicePtr device;
1618 SomethingCtrl *ctrl;
1619
1620</programlisting></blockquote>
1621You write two versions of this procedure, one for the keyboard and one for the pointing device.
1622DIX calls it to inform DDX when a client has requested changes in the current
1623settings for the particular device.
1624For a keyboard, this might be the repeat threshold and rate.
1625For a pointing device, this might be a scaling factor (coarse or fine) for position reporting.
1626See input.h for the ctrl structures.
1627</para>
1628</section>
1629<section>
1630 <title>Input Initialization</title>
1631<para>
1632Input initialization is a bit complicated.
1633It all starts with InitInput(), a routine that you write to call
1634AddInputDevice() twice
1635(once for pointing device and once for keyboard.)
1636</para>
1637<para>
1638When you Add the devices, a routine you supply for each device
1639gets called to initialize them.
1640Your individual initialize routines must call InitKeyboardDeviceStruct()
1641or InitPointerDeviceStruct(), depending upon which it is.
1642In other words, you indicate twice that the keyboard is the keyboard and
1643the pointer is the pointer.
1644</para>
1645<para>
1646<blockquote><programlisting>
1647
1648 void InitInput(argc, argv)
1649 int argc;
1650 char **argv;
1651</programlisting></blockquote>
1652InitInput is a DDX routine you must write to initialize the
1653input subsystem in DDX.
1654It must call AddInputDevice() for each device that might generate events.
1655</para>
1656<para>
1657<blockquote><programlisting>
1658
1659 DevicePtr AddInputDevice(deviceProc, autoStart)
1660 DeviceProc deviceProc;
1661 Bool autoStart;
1662</programlisting></blockquote>
1663AddInputDevice is a DIX routine you call to create a device object.
1664deviceProc is a DDX routine that is called by DIX to do various operations.
1665AutoStart should be TRUE for devices that need to be turned on at
1666initialization time with a special call, as opposed to waiting for some
1667client application to
1668turn them on.
1669This routine returns NULL if sufficient memory cannot be allocated to
1670install the device.
1671</para>
1672<para>
1673Note also that except for the main keyboard and pointing device,
1674an extension is needed to provide for a client interface to a device.
1675</para>
1676<para>
1677The following DIX
1678procedures return the specified DevicePtr. They may or may not be useful
1679to DDX implementors.
1680</para>
1681<para>
1682<blockquote><programlisting>
1683
1684 DevicePtr LookupKeyboardDevice()
1685</programlisting></blockquote>
1686LookupKeyboardDevice returns pointer for current main keyboard device.
1687</para>
1688<para>
1689<blockquote><programlisting>
1690
1691 DevicePtr LookupPointerDevice()
1692</programlisting></blockquote>
1693LookupPointerDevice returns pointer for current main pointing device.
1694</para>
1695<para>
1696A DeviceProc (the kind passed to AddInputDevice()) in the following form:
1697<blockquote><programlisting>
1698
1699 Bool pInternalDevice->DeviceProc(device, action);
1700 DeviceIntPtr device;
1701 int action;
1702</programlisting></blockquote>
1703You must write a DeviceProc for each device.
1704device points to the device record.
1705action tells what action to take;
1706it will be one of these defined constants (defined in input.h):
1707<itemizedlist>
1708<listitem><para>
1709DEVICE_INIT -
1710At DEVICE_INIT time, the device should initialize itself by calling
1711InitPointerDeviceStruct(), InitKeyboardDeviceStruct(), or a similar
1712routine (see below)
1713and "opening" the device if necessary.
1714If you return a non-zero (i.e., != Success) value from the DEVICE_INIT
1715call, that device will be considered unavailable. If either the main keyboard
1716or main pointing device cannot be initialized, the DIX code will refuse
1717to continue booting up.</para></listitem>
1718<listitem><para>
1719DEVICE_ON - If the DeviceProc is called with DEVICE_ON, then it is
1720allowed to start
1721putting events into the client stream by calling through the ProcessInputProc
1722in the device.</para></listitem>
1723<listitem><para>
1724DEVICE_OFF - If the DeviceProc is called with DEVICE_OFF, no further
1725events from that
1726device should be given to the DIX layer.
1727The device will appear to be dead to the user.</para></listitem>
1728<listitem><para>
1729DEVICE_CLOSE - At DEVICE_CLOSE (terminate or reset) time, the device should
1730be totally closed down.</para></listitem>
1731</itemizedlist>
1732</para>
1733<para>
1734<blockquote><programlisting>
1735
1736 void InitPointerDeviceStruct(device, map, mapLength,
1737 GetMotionEvents, ControlProc, numMotionEvents)
1738 DevicePtr device;
1739 CARD8 *map;
1740 int mapLength;
1741 ValuatorMotionProcPtr ControlProc;
1742 PtrCtrlProcPtr GetMotionEvents;
1743 int numMotionEvents;
1744</programlisting></blockquote>
1745InitPointerDeviceStruct is a DIX routine you call at DEVICE_INIT time to declare
1746some operating routines and data structures for a pointing device.
1747map and mapLength are as described in the X Window
1748System protocol specification.
1749ControlProc and GetMotionEvents are DDX routines, see above.
1750</para>
1751<para>
1752numMotionEvents is for the motion-buffer-size for the GetMotionEvents
1753request.
1754A typical length for a motion buffer would be 100 events.
1755A server that does not implement this capability should set
1756numMotionEvents to zero.
1757</para>
1758<para>
1759<blockquote><programlisting>
1760
1761 void InitKeyboardDeviceStruct(device, pKeySyms, pModifiers, Bell, ControlProc)
1762 DevicePtr device;
1763 KeySymsPtr pKeySyms;
1764 CARD8 *pModifiers;
1765 BellProcPtr Bell;
1766 KbdCtrlProcPtr ControlProc;
1767
1768</programlisting></blockquote>
1769You call this DIX routine when a keyboard device is initialized and
1770its device procedure is called with
1771DEVICE_INIT.
1772The formats of the keysyms and modifier maps are defined in
1773Xserver/include/input.h.
1774They describe the layout of keys on the keyboards, and the glyphs
1775associated with them. ( See the next section for information on
1776setting up the modifier map and the keysym map.)
1777ControlProc and Bell are DDX routines, see above.
1778</para>
1779</section>
1780<section>
1781 <title>Keyboard Mapping and Keycodes</title>
1782<para>
1783When you send a keyboard event, you send a report that a given key has
1784either been pressed or has been released. There must be a keycode for
1785each key that identifies the key; the keycode-to-key mapping can be
1786any mapping you desire, because you specify the mapping in a table you
1787set up for DIX. However, you are restricted by the protocol
1788specification to keycode values in the range 8 to 255 inclusive.
1789</para>
1790<para>
1791The keycode mapping information that you set up consists of the following:
1792<itemizedlist>
1793<listitem><para>
1794A minimum and maximum keycode number</para></listitem>
1795<listitem><para>
1796An array of sets of keysyms for each key, that is of length
1797maxkeycode - minkeycode + 1.
1798Each element of this array is a list of codes for symbols that are on that key.
1799There is no limit to the number of symbols that can be on a key.</para></listitem>
1800</itemizedlist>
1801Once the map is set up, DIX keeps and
1802maintains the client's changes to it.
1803</para>
1804<para>
1805The X protocol defines standard names to indicate the symbol(s)
1806printed on each keycap. (See X11/keysym.h)
1807</para>
1808<para>
1809Legal modifier keys must generate both up and down transitions. When
1810a client tries to change a modifier key (for instance, to make "A" the
1811"Control" key), DIX calls the following routine, which should return
1812TRUE if the key can be used as a modifier on the given device:
1813<blockquote><programlisting>
1814
1815 Bool LegalModifier(key, pDev)
1816 unsigned int key;
1817 DevicePtr pDev;
1818</programlisting></blockquote>
1819</para>
1820</section>
1821</section>
1822<section>
1823<title>Screens</title>
1824<para>
1825Different computer graphics
1826displays have different capabilities.
1827Some are simple monochrome
1828frame buffers that are just lying
1829there in memory, waiting to be written into.
1830Others are color displays with many bits per pixel using some color lookup table.
1831Still others have high-speed graphic processors that prefer to do all of the work
1832themselves,
1833including maintaining their own high-level, graphic data structures.
1834</para>
1835<section>
1836 <title>Screen Hardware Requirements</title>
1837<para>
1838The only requirement on screens is that you be able to both read
1839and write locations in the frame buffer.
1840All screens must have a depth of 32 or less (unless you use
1841an X extension to allow a greater depth).
1842All screens must fit into one of the classes listed in the section
1843in this document on Visuals and Depths.
1844</para>
1845<para>
1846X uses the pixel as its fundamental unit of distance on the screen.
1847Therefore, most programs will measure everything in pixels.</para>
1848<para>
1849The sample server assumes square pixels.
1850Serious WYSIWYG (what you see is what you get) applications for
1851publishing and drawing programs will adjust for
1852different screen resolutions automatically.
1853Considerable work
1854is involved in compensating for non-square pixels (a bit in the DDX
1855code for the sample server but quite a bit in the client applications).</para>
1856</section>
1857<section>
1858 <title>Data Structures</title>
1859<para>
1860X supports multiple screens that are connected to the same
1861server. Therefore, all the per-screen information is bundled into one data
1862structure of attributes and procedures, which is the ScreenRec (see
1863Xserver/include/scrnintstr.h).
1864The procedure entry points in a ScreenRec operate on
1865regions, colormaps, cursors, and fonts, because these resources
1866can differ in format from one screen to another.</para>
1867<para>
1868Windows are areas on the screen that can be drawn into by graphic
1869routines. "Pixmaps" are off-screen graphic areas that can be drawn
1870into. They are both considered drawables and are described in the
1871section on Drawables. All graphic operations work on drawables, and
1872operations are available to copy patches from one drawable to another.</para>
1873<para>
1874The pixel image data in all drawables is in a format that is private
1875to DDX. In fact, each instance of a drawable is associated with a
1876given screen. Presumably, the pixel image data for pixmaps is chosen
1877to be conveniently understood by the hardware. All screens in a
1878single server must be able to handle all pixmaps depths declared in
1879the connection setup information.</para>
1880<para>
1881Pixmap images are transferred to the server in one of two ways:
1882XYPixmap or ZPimap. XYPixmaps are a series of bitmaps, one for each
1883bit plane of the image, using the bitmap padding rules from the
1884connection setup. ZPixmaps are a series of bits, nibbles, bytes or
1885words, one for each pixel, using the format rules (padding and so on)
1886for the appropriate depth.</para>
1887<para>
1888All screens in a given server must agree on a set of pixmap image
1889formats (PixmapFormat) to support (depth, number of bits per pixel,
1890etc.).</para>
1891<para>
1892There is no color interpretation of bits in the pixmap. Pixmaps
1893do not contain pixel values. The interpretation is made only when
1894the bits are transferred onto the screen.</para>
1895<para>
1896The screenInfo structure (in scrnintstr.h) is a global data structure
1897that has a pointer to an array of ScreenRecs, one for each screen on
1898the server. (These constitute the one and only description of each
1899screen in the server.) Each screen has an identifying index (0, 1, 2, ...).
1900In addition, the screenInfo struct contains global server-wide
1901details, such as the bit- and byte- order in all bit images, and the
1902list of pixmap image formats that are supported. The X protocol
1903insists that these must be the same for all screens on the server.</para>
1904</section>
1905<section>
1906 <title>Output Initialization</title>
1907<para>
1908<blockquote><programlisting>
1909
1910 InitOutput(pScreenInfo, argc, argv)
1911 ScreenInfo *pScreenInfo;
1912 int argc;
1913 char **argv;
1914</programlisting></blockquote>
1915Upon initialization, your DDX routine InitOutput() is called by DIX.
1916It is passed a pointer to screenInfo to initialize. It is also passed
1917the argc and argv from main() for your server for the command-line
1918arguments. These arguments may indicate what or how many screen
1919device(s) to use or in what way to use them. For instance, your
1920server command line may allow a "-D" flag followed by the name of the
1921screen device to use.</para>
1922<para>
1923Your InitOutput() routine should initialize each screen you wish to
1924use by calling AddScreen(), and then it should initialize the pixmap
1925formats that you support by storing values directly into the
1926screenInfo data structure. You should also set certain
1927implementation-dependent numbers and procedures in your screenInfo,
1928which determines the pixmap and scanline padding rules for all screens
1929in the server.</para>
1930<para>
1931<blockquote><programlisting>
1932
1933 int AddScreen(scrInitProc, argc, argv)
1934 Bool (*scrInitProc)();
1935 int argc;
1936 char **argv;
1937</programlisting></blockquote>
1938You should call AddScreen(), a DIX procedure, in InitOutput() once for
1939each screen to add it to the screenInfo database. The first argument
1940is an initialization procedure for the screen that you supply. The
1941second and third are the argc and argv from main(). It returns the
1942screen number of the screen installed, or -1 if there is either
1943insufficient memory to add the screen, or (*scrInitProc) returned
1944FALSE.</para>
1945<para>
1946The scrInitProc should be of the following form:
1947<blockquote><programlisting>
1948
1949 Bool scrInitProc(pScreen, argc, argv)
1950 ScreenPtr pScreen;
1951 int argc;
1952 char **argv;
1953</programlisting></blockquote>
1954pScreen is the pointer to the screen's new ScreenRec. argc and argv
1955are as before. Your screen initialize procedure should return TRUE
1956upon success or FALSE if the screen cannot be initialized (for
1957 instance, if the screen hardware does not exist on this machine).</para>
1958<para>
1959This procedure must determine what actual device it is supposed to initialize.
1960If you have a different procedure for each screen, then it is no problem.
1961If you have the same procedure for multiple screens, it may have trouble
1962figuring out which screen to initialize each time around, especially if
1963InitOutput() does not initialize all of the screens.
1964It is probably easiest to have one procedure for each screen.</para>
1965<para>
1966The initialization procedure should fill in all the screen procedures
1967for that screen (windowing functions, region functions, etc.) and certain
1968screen attributes for that screen.</para>
1969</section>
1970<section>
1971 <title>Region Routines in the ScreenRec</title>
1972<para>
1973A region is a dynamically allocated data structure that describes an
1974irregularly shaped piece of real estate in XY pixel space. You can
1975think of it as a set of pixels on the screen to be operated upon with
1976set operations such as AND and OR.</para>
1977<para>
1978A region is frequently implemented as a list of rectangles or bitmaps
1979that enclose the selected pixels. Region operators control the
1980"clipping policy," or the operations that work on regions. (The
1981sample server uses YX-banded rectangles. Unless you have something
1982already implemented for your graphics system, you should keep that
1983implementation.) The procedure pointers to the region operators are
1984located in the ScreenRec data structure. The definition of a region
1985can be found in the file Xserver/include/regionstr.h. The region code
1986is found in Xserver/mi/miregion.c. DDX implementations using other
1987region formats will need to supply different versions of the region
1988operators.</para>
1989<para>
1990Since the list of rectangles is unbounded in size, part of the region
1991data structure is usually a large, dynamically allocated chunk of
1992memory. As your region operators calculate logical combinations of
1993regions, these blocks may need to be reallocated by your region
1994software. For instance, in the sample server, a RegionRec has some
1995header information and a pointer to a dynamically allocated rectangle
1996list. Periodically, the rectangle list needs to be expanded with
1997Xrealloc(), whereupon the new pointer is remembered in the RegionRec.</para>
1998<para>
1999Most of the region operations come in two forms: a function pointer in
2000the Screen structure, and a macro. The server can be compiled so that
2001the macros make direct calls to the appropriate functions (instead of
2002indirecting through a screen function pointer), or it can be compiled
2003so that the macros are identical to the function pointer forms.
2004Making direct calls is faster on many architectures.</para>
2005<para>
2006<blockquote><programlisting>
2007
2008 RegionPtr pScreen->RegionCreate( rect, size)
2009 BoxPtr rect;
2010 int size;
2011
2012 macro: RegionPtr RegionCreate(rect, size)
2013
2014</programlisting></blockquote>
2015RegionCreate creates a region that describes ONE rectangle. The
2016caller can avoid unnecessary reallocation and copying by declaring the
2017probable maximum number of rectangles that this region will need to
2018describe itself. Your region routines, though, cannot fail just
2019because the region grows beyond this size. The caller of this routine
2020can pass almost anything as the size; the value is merely a good guess
2021as to the maximum size until it is proven wrong by subsequent use.
2022Your region procedures are then on their own in estimating how big the
2023region will get. Your implementation might ignore size, if
2024applicable.</para>
2025<para>
2026<blockquote><programlisting>
2027
2028 void pScreen->RegionInit (pRegion, rect, size)
2029 RegionPtr pRegion;
2030 BoxPtr rect;
2031 int size;
2032
2033 macro: RegionInit(pRegion, rect, size)
2034
2035</programlisting></blockquote>
2036Given an existing raw region structure (such as an local variable), this
2037routine fills in the appropriate fields to make this region as usable as
2038one returned from RegionCreate. This avoids the additional dynamic memory
2039allocation overhead for the region structure itself.
2040</para>
2041<para>
2042<blockquote><programlisting>
2043
2044 Bool pScreen->RegionCopy(dstrgn, srcrgn)
2045 RegionPtr dstrgn, srcrgn;
2046
2047 macro: Bool RegionCopy(dstrgn, srcrgn)
2048
2049</programlisting></blockquote>
2050RegionCopy copies the description of one region, srcrgn, to another
2051already-created region,
2052dstrgn; returning TRUE if the copy succeeded, and FALSE otherwise.</para>
2053<para>
2054<blockquote><programlisting>
2055
2056 void pScreen->RegionDestroy( pRegion)
2057 RegionPtr pRegion;
2058
2059 macro: RegionDestroy(pRegion)
2060
2061</programlisting></blockquote>
2062RegionDestroy destroys a region and frees all allocated memory.</para>
2063<para>
2064<blockquote><programlisting>
2065
2066 void pScreen->RegionUninit (pRegion)
2067 RegionPtr pRegion;
2068
2069 macro: RegionUninit(pRegion)
2070
2071</programlisting></blockquote>
2072Frees everything except the region structure itself, useful when the
2073region was originally passed to RegionInit instead of received from
2074RegionCreate. When this call returns, pRegion must not be reused until
2075it has been RegionInit'ed again.</para>
2076<para>
2077<blockquote><programlisting>
2078
2079 Bool pScreen->Intersect(newReg, reg1, reg2)
2080 RegionPtr newReg, reg1, reg2;
2081
2082 macro: Bool RegionIntersect(newReg, reg1, reg2)
2083
2084 Bool pScreen->Union(newReg, reg1, reg2)
2085 RegionPtr newReg, reg1, reg2;
2086
2087 macro: Bool RegionUnion(newReg, reg1, reg2)
2088
2089 Bool pScreen->Subtract(newReg, regMinuend, regSubtrahend)
2090 RegionPtr newReg, regMinuend, regSubtrahend;
2091
2092 macro: Bool RegionUnion(newReg, regMinuend, regSubtrahend)
2093
2094 Bool pScreen->Inverse(newReg, pReg, pBox)
2095 RegionPtr newReg, pReg;
2096 BoxPtr pBox;
2097
2098 macro: Bool RegionInverse(newReg, pReg, pBox)
2099
2100</programlisting></blockquote>
2101The above four calls all do basic logical operations on regions. They
2102set the new region (which already exists) to describe the logical
2103intersection, union, set difference, or inverse of the region(s) that
2104were passed in. Your routines must be able to handle a situation
2105where the newReg is the same region as one of the other region
2106arguments.</para>
2107<para>
2108The subtract function removes the Subtrahend from the Minuend and
2109puts the result in newReg.</para>
2110<para>
2111The inverse function returns a region that is the pBox minus the
2112region passed in. (A true "inverse" would make a region that extends
2113to infinity in all directions but has holes in the middle.) It is
2114undefined for situations where the region extends beyond the box.</para>
2115<para>
2116Each routine must return the value TRUE for success.</para>
2117<para>
2118<blockquote><programlisting>
2119
2120 void pScreen->RegionReset(pRegion, pBox)
2121 RegionPtr pRegion;
2122 BoxPtr pBox;
2123
2124 macro: RegionReset(pRegion, pBox)
2125
2126</programlisting></blockquote>
2127RegionReset sets the region to describe
2128one rectangle and reallocates it to a size of one rectangle, if applicable.</para>
2129<para>
2130<blockquote><programlisting>
2131
2132 void pScreen->TranslateRegion(pRegion, x, y)
2133 RegionPtr pRegion;
2134 int x, y;
2135
2136 macro: RegionTranslate(pRegion, x, y)
2137
2138</programlisting></blockquote>
2139TranslateRegion simply moves a region +x in the x direction and +y in the y
2140direction.</para>
2141<para>
2142<blockquote><programlisting>
2143
2144 int pScreen->RectIn(pRegion, pBox)
2145 RegionPtr pRegion;
2146 BoxPtr pBox;
2147
2148 macro: int RegionContainsRect(pRegion, pBox)
2149
2150</programlisting></blockquote>
2151RectIn returns one of the defined constants rgnIN, rgnOUT, or rgnPART,
2152depending upon whether the box is entirely inside the region, entirely
2153outside of the region, or partly in and partly out of the region.
2154These constants are defined in Xserver/include/region.h.</para>
2155<para>
2156<blockquote><programlisting>
2157
2158 Bool pScreen->PointInRegion(pRegion, x, y, pBox)
2159 RegionPtr pRegion;
2160 int x, y;
2161 BoxPtr pBox;
2162
2163 macro: Bool RegionContainsPoint(pRegion, x, y, pBox)
2164
2165</programlisting></blockquote>
2166PointInRegion returns true if the point x, y is in the region. In
2167addition, it fills the rectangle pBox with coordinates of a rectangle
2168that is entirely inside of pRegion and encloses the point. In the mi
2169implementation, it is the largest such rectangle. (Due to the sample
2170server implementation, this comes cheaply.)</para>
2171<para>
2172This routine used by DIX when tracking the pointing device and
2173deciding whether to report mouse events or change the cursor. For
2174instance, DIX needs to change the cursor when it moves from one window
2175to another. Due to overlapping windows, the shape to check may be
2176irregular. A PointInRegion() call for every pointing device movement
2177may be too expensive. The pBox is a kind of wake-up box; DIX need not
2178call PointInRegion() again until the cursor wanders outside of the
2179returned box.</para>
2180<para>
2181<blockquote><programlisting>
2182
2183 Bool pScreen->RegionNotEmpty(pRegion)
2184 RegionPtr pRegion;
2185
2186 macro: Bool RegionNotEmpty(pRegion)
2187
2188</programlisting></blockquote>
2189RegionNotEmpty is a boolean function that returns
2190true or false depending upon whether the region encloses any pixels.</para>
2191<para>
2192<blockquote><programlisting>
2193
2194 void pScreen->RegionEmpty(pRegion)
2195 RegionPtr pRegion;
2196
2197 macro: RegionEmpty(pRegion)
2198
2199</programlisting></blockquote>
2200RegionEmpty sets the region to be empty.</para>
2201<para>
2202<blockquote><programlisting>
2203
2204 BoxPtr pScreen->RegionExtents(pRegion)
2205 RegionPtr pRegion;
2206
2207 macro: RegionExtents(pRegion)
2208
2209</programlisting></blockquote>
2210RegionExtents returns a rectangle that is the smallest
2211possible superset of the entire region.
2212The caller will not modify this rectangle, so it can be the one
2213in your region struct.</para>
2214<para>
2215<blockquote><programlisting>
2216
2217 Bool pScreen->RegionAppend (pDstRgn, pRegion)
2218 RegionPtr pDstRgn;
2219 RegionPtr pRegion;
2220
2221 macro: Bool RegionAppend(pDstRgn, pRegion)
2222
2223 Bool pScreen->RegionValidate (pRegion, pOverlap)
2224 RegionPtr pRegion;
2225 Bool *pOverlap;
2226
2227 macro: Bool RegionValidate(pRegion, pOverlap)
2228
2229</programlisting></blockquote>
2230These functions provide an optimization for clip list generation and
2231must be used in conjunction. The combined effect is to produce the
2232union of a collection of regions, by using RegionAppend several times,
2233and finally calling RegionValidate which takes the intermediate
2234representation (which needn't be a valid region) and produces the
2235desired union. pOverlap is set to TRUE if any of the original
2236regions overlap; FALSE otherwise.</para>
2237<para>
2238<blockquote><programlisting>
2239
2240 RegionPtr pScreen->BitmapToRegion (pPixmap)
2241 PixmapPtr pPixmap;
2242
2243 macro: RegionPtr BitmapToRegion(pScreen, pPixmap)
2244
2245</programlisting></blockquote>
2246Given a depth-1 pixmap, this routine must create a valid region which
2247includes all the areas of the pixmap filled with 1's and excludes the
2248areas filled with 0's. This routine returns NULL if out of memory.</para>
2249<para>
2250<blockquote><programlisting>
2251
2252 RegionPtr pScreen->RectsToRegion (nrects, pRects, ordering)
2253 int nrects;
2254 xRectangle *pRects;
2255 int ordering;
2256
2257 macro: RegionPtr RegionFromRects(nrects, pRects, ordering)
2258
2259</programlisting></blockquote>
2260Given a client-supplied list of rectangles, produces a region which includes
2261the union of all the rectangles. Ordering may be used as a hint which
2262describes how the rectangles are sorted. As the hint is provided by a
2263client, it must not be required to be correct, but the results when it is
2264not correct are not defined (core dump is not an option here).</para>
2265<para>
2266<blockquote><programlisting>
2267
2268 void pScreen->SendGraphicsExpose(client,pRegion,drawable,major,minor)
2269 ClientPtr client;
2270 RegionPtr pRegion;
2271 XID drawable;
2272 int major;
2273 int minor;
2274
2275</programlisting></blockquote>
2276SendGraphicsExpose dispatches a list of GraphicsExposure events which
2277span the region to the specified client. If the region is empty, or
2278a NULL pointer, a NoExpose event is sent instead.</para>
2279</section>
2280<section>
2281 <title>Cursor Routines for a Screen</title>
2282<para>
2283A cursor is the visual form tied to the pointing device. The default
2284cursor is an "X" shape, but the cursor can have any shape. When a
2285client creates a window, it declares what shape the cursor will be
2286when it strays into that window on the screen.</para>
2287<para>
2288For each possible shape the cursor assumes, there is a CursorRec data
2289structure. This data structure contains a pointer to a CursorBits
2290data structure which contains a bitmap for the image of the cursor and
2291a bitmap for a mask behind the cursor, in addition, the CursorRec data
2292structure contains foreground and background colors for the cursor.
2293The CursorBits data structure is shared among multiple CursorRec
2294structures which use the same font and glyph to describe both source
2295and mask. The cursor image is applied to the screen by applying the
2296mask first, clearing 1 bits in its form to the background color, and
2297then overwriting on the source image, in the foreground color. (One
2298bits of the source image that fall on top of zero bits of the mask
2299image are undefined.) This way, a cursor can have transparent parts,
2300and opaque parts in two colors. X allows any cursor size, but some
2301hardware cursor schemes allow a maximum of N pixels by M pixels.
2302Therefore, you are allowed to transform the cursor to a smaller size,
2303but be sure to include the hot-spot.</para>
2304<para>
2305CursorBits in Xserver/include/cursorstr.h is a device-independent
2306structure containing a device-independent representation of the bits
2307for the source and mask. (This is possible because the bitmap
2308representation is the same for all screens.)</para>
2309<para>
2310When a cursor is created, it is "realized" for each screen. At
2311realization time, each screen has the chance to convert the bits into
2312some other representation that may be more convenient (for instance,
2313putting the cursor into off-screen memory) and set up its
2314device-private area in either the CursorRec data structure or
2315CursorBits data structure as appropriate to possibly point to whatever
2316data structures are needed. It is more memory-conservative to share
2317realizations by using the CursorBits private field, but this makes the
2318assumption that the realization is independent of the colors used
2319(which is typically true). For instance, the following are the device
2320private entries for a particular screen and cursor:
2321<blockquote><programlisting>
2322
2323 pCursor->devPriv[pScreen->myNum]
2324 pCursor->bits->devPriv[pScreen->myNum]
2325
2326</programlisting></blockquote>
2327This is done because the change from one cursor shape to another must
2328be fast and responsive; the cursor image should be able to flutter as
2329fast as the user moves it across the screen.</para>
2330<para>
2331You must implement the following routines for your hardware:
2332<blockquote><programlisting>
2333
2334 Bool pScreen->RealizeCursor( pScr, pCurs)
2335 ScreenPtr pScr;
2336 CursorPtr pCurs;
2337
2338 Bool pScreen->UnrealizeCursor( pScr, pCurs)
2339 ScreenPtr pScr;
2340 CursorPtr pCurs;
2341
2342</programlisting></blockquote>
2343</para>
2344<para>
2345RealizeCursor and UnrealizeCursor should realize (allocate and
2346calculate all data needed) and unrealize (free the dynamically
2347allocated data) a given cursor when DIX needs them. They are called
2348whenever a device-independent cursor is created or destroyed. The
2349source and mask bits pointed to by fields in pCurs are undefined for
2350bits beyond the right edge of the cursor. This is so because the bits
2351are in Bitmap format, which may have pad bits on the right edge. You
2352should inhibit UnrealizeCursor() if the cursor is currently in use;
2353this happens when the system is reset.</para>
2354<para>
2355<blockquote><programlisting>
2356
2357 Bool pScreen->DisplayCursor( pScr, pCurs)
2358 ScreenPtr pScr;
2359 CursorPtr pCurs;
2360
2361</programlisting></blockquote>
2362DisplayCursor should change the cursor on the given screen to the one
2363passed in. It is called by DIX when the user moves the pointing
2364device into a different window with a different cursor. The hotspot
2365in the cursor should be aligned with the current cursor position.</para>
2366<para>
2367<blockquote><programlisting>
2368
2369 void pScreen->RecolorCursor( pScr, pCurs, displayed)
2370 ScreenPtr pScr;
2371 CursorPtr pCurs;
2372 Bool displayed;
2373</programlisting></blockquote>
2374RecolorCursor notifies DDX that the colors in pCurs have changed and
2375indicates whether this is the cursor currently being displayed. If it
2376is, the cursor hardware state may have to be updated. Whether
2377displayed or not, state created at RealizeCursor time may have to be
2378updated. A generic version, miRecolorCursor, may be used that
2379does an unrealize, a realize, and possibly a display (in micursor.c);
2380however this constrains UnrealizeCursor and RealizeCursor to always return
2381TRUE as no error indication is returned here.</para>
2382<para>
2383<blockquote><programlisting>
2384
2385 void pScreen->ConstrainCursor( pScr, pBox)
2386 ScreenPtr pScr;
2387 BoxPtr pBox;
2388
2389</programlisting></blockquote>
2390ConstrainCursor should cause the cursor to restrict its motion to the
2391rectangle pBox. DIX code is capable of enforcing this constraint by
2392forcefully moving the cursor if it strays out of the rectangle, but
2393ConstrainCursor offers a way to send a hint to the driver or hardware
2394if such support is available. This can prevent the cursor from
2395wandering out of the box, then jumping back, as DIX forces it back.</para>
2396<para>
2397<blockquote><programlisting>
2398
2399 void pScreen->PointerNonInterestBox( pScr, pBox)
2400 ScreenPtr pScr;
2401 BoxPtr pBox;
2402
2403</programlisting></blockquote>
2404PointerNonInterestBox is DIX's way of telling the pointing device code
2405not to report motion events while the cursor is inside a given
2406rectangle on the given screen. It is optional and, if not
2407implemented, it should do nothing. This routine is called only when
2408the client has declared that it is not interested in motion events in
2409a given window. The rectangle you get may be a subset of that window.
2410It saves DIX code the time required to discard uninteresting mouse
2411motion events. This is only a hint, which may speed performance.
2412Nothing in DIX currently calls PointerNonInterestBox.</para>
2413<para>
2414<blockquote><programlisting>
2415
2416 void pScreen->CursorLimits( pScr, pCurs, pHotBox, pTopLeftBox)
2417 ScreenPtr pScr;
2418 CursorPtr pCurs;
2419 BoxPtr pHotBox;
2420 BoxPtr pTopLeftBox; /* return value */
2421
2422</programlisting></blockquote>
2423CursorLimits should calculate the box that the cursor hot spot is
2424physically capable of moving within, as a function of the screen pScr,
2425the device-independent cursor pCurs, and a box that DIX hypothetically
2426would want the hot spot confined within, pHotBox. This routine is for
2427informing DIX only; it alters no state within DDX.</para>
2428<para>
2429<blockquote><programlisting>
2430
2431 Bool pScreen->SetCursorPosition( pScr, newx, newy, generateEvent)
2432 ScreenPtr pScr;
2433 int newx;
2434 int newy;
2435 Bool generateEvent;
2436
2437</programlisting></blockquote>
2438SetCursorPosition should artificially move the cursor as though the
2439user had jerked the pointing device very quickly. This is called in
2440response to the WarpPointer request from the client, and at other
2441times. If generateEvent is True, the device should decide whether or
2442not to call ProcessInputEvents() and then it must call
2443DevicePtr->processInputProc. Its effects are, of course, limited in
2444value for absolute pointing devices such as a tablet.</para>
2445<para>
2446<blockquote><programlisting>
2447
2448 void NewCurrentScreen(newScreen, x, y)
2449 ScreenPtr newScreen;
2450 int x,y;
2451
2452</programlisting></blockquote>
2453If your ddx provides some mechanism for the user to magically move the
2454pointer between multiple screens, you need to inform DIX when this
2455occurs. You should call NewCurrentScreen to accomplish this, specifying
2456the new screen and the new x and y coordinates of the pointer on that screen.</para>
2457</section>
2458<section>
2459 <title>Visuals, Depths and Pixmap Formats for Screens</title>
2460<para>
2461The "depth" of a image is the number of bits that are used per pixel to display it.</para>
2462<para>
2463The "bits per pixel" of a pixmap image that is sent over the client
2464byte stream is a number that is either 4, 8, 16, 24 or 32. It is the
2465number of bits used per pixel in Z format. For instance, a pixmap
2466image that has a depth of six is best sent in Z format as 8 bits per
2467pixel.</para>
2468<para>
2469A "pixmap image format" or a "pixmap format" is a description of the
2470format of a pixmap image as it is sent over the byte stream. For each
2471depth available on a server, there is one and only one pixmap format.
2472This pixmap image format gives the bits per pixel and the scanline
2473padding unit. (For instance, are pixel rows padded to bytes, 16-bit
2474words, or 32-bit words?)</para>
2475<para>
2476For each screen, you must decide upon what depth(s) it supports. You
2477should only count the number of bits used for the actual image. Some
2478displays store additional bits to indicate what window this pixel is
2479in, how close this object is to a viewer, transparency, and other
2480data; do not count these bits.</para>
2481<para>
2482A "display class" tells whether the display is monochrome or color,
2483whether there is a lookup table, and how the lookup table works.</para>
2484<para>
2485A "visual" is a combination of depth, display class, and a description
2486of how the pixel values result in a color on the screen. Each visual
2487has a set of masks and offsets that are used to separate a pixel value
2488into its red, green, and blue components and a count of the number of
2489colormap entries. Some of these fields are only meaningful when the
2490class dictates so. Each visual also has a screen ID telling which
2491screen it is usable on. Note that the depth does not imply the number
2492of map_entries; for instance, a display can have 8 bits per pixel but
2493only 254 colormap entries for use by applications (the other two being
2494reserved by hardware for the cursor).</para>
2495<para>
2496Each visual is identified by a 32-bit visual ID which the client uses
2497to choose what visual is desired on a given window. Clients can be
2498using more than one visual on the same screen at the same time.</para>
2499<para>
2500The class of a display describes how this translation takes place.
2501There are three ways to do the translation.
2502<itemizedlist>
2503<listitem><para>
2504Pseudo - The pixel value, as a whole, is looked up
2505in a table of length map_entries to
2506determine the color to display.</para></listitem>
2507<listitem><para>
2508True - The
2509pixel value is broken up into red, green, and blue fields, each of which
2510are looked up in separate red, green, and blue lookup tables,
2511each of length map_entries.</para></listitem>
2512<listitem><para>
2513Gray - The pixel value is looked up in a table of length map_entries to
2514determine a gray level to display.</para></listitem>
2515</itemizedlist>
2516</para>
2517<para>
2518In addition, the lookup table can be static (resulting colors are fixed for each
2519pixel value)
2520or dynamic (lookup entries are under control of the client program).
2521This leads to a total of six classes:
2522<itemizedlist>
2523<listitem><para>
2524Static Gray - The pixel value (of however many bits) determines directly the
2525level of gray
2526that the pixel assumes.</para></listitem>
2527<listitem><para>
2528Gray Scale - The pixel value is fed through a lookup table to arrive at the level
2529of gray to display
2530for the given pixel.</para></listitem>
2531<listitem><para>
2532Static Color - The pixel value is fed through a fixed lookup table that yields the
2533color to display
2534for that pixel.</para></listitem>
2535<listitem><para>
2536PseudoColor - The whole pixel value is fed through a programmable lookup
2537table that has one
2538color (including red, green, and blue intensities) for each possible pixel value,
2539and that color is displayed.</para></listitem>
2540<listitem><para>
2541True Color - Each pixel value consists of one or more bits
2542that directly determine each primary color intensity after being fed through
2543a fixed table.</para></listitem>
2544<listitem><para>
2545Direct Color - Each pixel value consists of one or more bits for each primary color.
2546Each primary color value is individually looked up in a table for that primary
2547color, yielding
2548an intensity for that primary color.
2549For each pixel, the red value is looked up in the
2550red table, the green value in the green table, and
2551the blue value in the blue table.</para></listitem>
2552</itemizedlist>
2553</para>
2554<para>
2555Here are some examples:
2556<itemizedlist>
2557<listitem><para>
2558A simple monochrome 1 bit per pixel display is Static Gray.</para></listitem>
2559<listitem><para>
2560A display that has 2 bits per pixel for a choice
2561between the colors of black, white, green and violet is Static Color.</para></listitem>
2562<listitem><para>
2563A display that has three bits per pixel, where
2564each bit turns on or off one of the red, green or
2565blue guns, is in the True Color class.</para></listitem>
2566<listitem><para>
2567If you take the last example and scramble the
2568correspondence between pixel values and colors
2569it becomes a Static Color display.</para></listitem>
2570</itemizedlist></para>
2571<para>
2572A display has 8 bits per pixel. The 8 bits select one entry out of 256 entries
2573in a lookup table, each entry consisting of 24 bits (8bits each for red, green,
2574and blue).
2575The display can show any 256 of 16 million colors on the screen at once.
2576This is a pseudocolor display.
2577The client application gets to fill the lookup table in this class of display.</para>
2578<para>
2579Imagine the same hardware from the last example.
2580Your server software allows the user, on the
2581command line that starts up the server
2582program,
2583to fill the lookup table to his liking once and for all.
2584From then on, the server software would not change the lookup table
2585until it exits.
2586For instance, the default might be a lookup table with a reasonable sample of
2587colors from throughout the color space.
2588But the user could specify that the table be filled with 256 steps of gray scale
2589because he knew ahead of time he would be manipulating a lot of black-and-white
2590scanned photographs
2591and not very many color things.
2592Clients would be presented with this unchangeable lookup table.
2593Although the hardware qualifies as a PseudoColor display,
2594the facade presented to the X client is that this is a Static Color display.</para>
2595<para>
2596You have to decide what kind of display you have or want
2597to pretend you have.
2598When you initialize the screen(s), this class value must be set in the
2599VisualRec data structure along with other display characteristics like the
2600depth and other numbers.</para>
2601<para>
2602The allowable DepthRec's and VisualRec's are pointed to by fields in the ScreenRec.
2603These are set up when InitOutput() is called; you should Xalloc() appropriate blocks
2604or use static variables initialized to the correct values.</para>
2605</section>
2606<section>
2607<title>Colormaps for Screens</title>
2608<para>
2609A colormap is a device-independent
2610mapping between pixel values and colors displayed on the screen.</para>
2611<para>
2612Different windows on the same screen can have different
2613colormaps at the same time.
2614At any given time, the most recently installed
2615colormap(s) will be in use in the server
2616so that its (their) windows' colors will be guaranteed to be correct.
2617Other windows may be off-color.
2618Although this may seem to be chaotic, in practice most clients
2619use the default colormap for the screen.</para>
2620<para>
2621The default colormap for a screen is initialized when the screen is initialized.
2622It always remains in existence and is not owned by any regular client. It
2623is owned by client 0 (the server itself).
2624Many clients will simply use this default colormap for their drawing.
2625Depending upon the class of the screen, the entries in this colormap may
2626be modifiable by client applications.</para>
2627</section>
2628<section>
2629 <title>Colormap Routines</title>
2630<para>
2631You need to implement the following routines to handle the device-dependent
2632aspects of color maps. You will end up placing pointers to these procedures
2633in your ScreenRec data structure(s). The sample server implementations of
2634many of these routines are in fbcmap.c.</para>
2635<para>
2636<blockquote><programlisting>
2637
2638 Bool pScreen->CreateColormap(pColormap)
2639 ColormapPtr pColormap;
2640
2641</programlisting></blockquote>
2642This routine is called by the DIX CreateColormap routine after it has allocated
2643all the data for the new colormap and just before it returns to the dispatcher.
2644It is the DDX layer's chance to initialize the colormap, particularly if it is
2645a static map. See the following
2646section for more details on initializing colormaps.
2647The routine returns FALSE if creation failed, such as due to memory
2648limitations.
2649Notice that the colormap has a devPriv field from which you can hang any
2650colormap specific storage you need. Since each colormap might need special
2651information, we attached the field to the colormap and not the visual.</para>
2652<para>
2653<blockquote><programlisting>
2654
2655 void pScreen->DestroyColormap(pColormap)
2656 ColormapPtr pColormap;
2657
2658</programlisting></blockquote>
2659This routine is called by the DIX FreeColormap routine after it has uninstalled
2660the colormap and notified all interested parties, and before it has freed
2661any of the colormap storage.
2662It is the DDX layer's chance to free any data it added to the colormap.</para>
2663<para>
2664<blockquote><programlisting>
2665
2666 void pScreen->InstallColormap(pColormap)
2667 ColormapPtr pColormap;
2668
2669</programlisting></blockquote>
2670InstallColormap should
2671fill a lookup table on the screen with which the colormap is associated with
2672the colors in pColormap.
2673If there is only one hardware lookup table for the screen, then all colors on
2674the screen may change simultaneously.</para>
2675<para>
2676In the more general case of multiple hardware lookup tables,
2677this may cause some other colormap to be
2678uninstalled, meaning that windows that subscribed to the colormap
2679that was uninstalled may end up being off-color.
2680See the note, below, about uninstalling maps.</para>
2681<para>
2682<blockquote><programlisting>
2683
2684 void pScreen->UninstallColormap(pColormap)
2685 ColormapPtr pColormap;
2686
2687</programlisting></blockquote>
2688UninstallColormap should
2689remove pColormap from screen pColormap->pScreen.
2690Some other map, such as the default map if possible,
2691should be installed in place of pColormap if applicable.
2692If
2693pColormap is the default map, do nothing.
2694If any client has requested ColormapNotify events, the DDX layer must notify the client.
2695(The routine WalkTree() is
2696be used to find such windows. The DIX routines TellNoMap(),
2697TellNewMap() and TellGainedMap() are provided to be used as
2698the procedure parameter to WalkTree. These procedures are in
2699Xserver/dix/colormap.c.)</para>
2700<para>
2701<blockquote><programlisting>
2702
2703 int pScreen->ListInstalledColormaps(pScreen, pCmapList)
2704 ScreenPtr pScreen;
2705 XID *pCmapList;
2706
2707
2708</programlisting></blockquote>
2709ListInstalledColormaps fills the pCmapList in with the resource ids
2710of the installed maps and returns a count of installed maps.
2711pCmapList will point to an array of size MaxInstalledMaps that was allocated
2712by the caller.</para>
2713<para>
2714<blockquote><programlisting>
2715
2716 void pScreen->StoreColors (pmap, ndef, pdefs)
2717 ColormapPtr pmap;
2718 int ndef;
2719 xColorItem *pdefs;
2720
2721</programlisting></blockquote>
2722StoreColors changes some of the entries in the colormap pmap.
2723The number of entries to change are ndef, and pdefs points to the information
2724describing what to change.
2725Note that partial changes of entries in the colormap are allowed.
2726Only the colors
2727indicated in the flags field of each xColorItem need to be changed.
2728However, all three color fields will be sent with the proper value for the
2729benefit of screens that may not be able to set part of a colormap value.
2730If the screen is a static class, this routine does nothing.
2731The structure of colormap entries is nontrivial; see colormapst.h
2732and the definition of xColorItem in Xproto.h for
2733more details.</para>
2734<para>
2735<blockquote><programlisting>
2736
2737 void pScreen->ResolveColor(pRed, pGreen, pBlue, pVisual)
2738 unsigned short *pRed, *pGreen, *pBlue;
2739 VisualPtr pVisual;
2740
2741
2742</programlisting></blockquote>
2743Given a requested color, ResolveColor returns the nearest color that this hardware is
2744capable of displaying on this visual.
2745In other words, this rounds off each value, in place, to the number of bits
2746per primary color that your screen can use.
2747Remember that each screen has one of these routines.
2748The level of roundoff should be what you would expect from the value
2749you put in the bits_per_rgb field of the pVisual.</para>
2750<para>
2751Each value is an unsigned value ranging from 0 to 65535.
2752The bits least likely to be used are the lowest ones.</para>
2753<para>
2754For example, if you had a pseudocolor display
2755with any number of bits per pixel
2756that had a lookup table supplying 6 bits for each color gun
2757(a total of 256K different colors), you would
2758round off each value to 6 bits. Please don't simply truncate these values
2759to the upper 6 bits, scale the result so that the maximum value seen
2760by the client will be 65535 for each primary. This makes color values
2761more portable between different depth displays (a 6-bit truncated white
2762will not look white on an 8-bit display).</para>
2763<section>
2764<title>Initializing a Colormap</title>
2765<para>
2766When a client requests a new colormap and when the server creates the default
2767colormap, the procedure CreateColormap in the DIX layer is invoked.
2768That procedure allocates memory for the colormap and related storage such as
2769the lists of which client owns which pixels.
2770It then sets a bit, BeingCreated, in the flags field of the ColormapRec
2771and calls the DDX layer's CreateColormap routine.
2772This is your chance to initialize the colormap.
2773If the colormap is static, which you can tell by looking at the class field,
2774you will want to fill in each color cell to match the hardwares notion of the
2775color for that pixel.
2776If the colormap is the default for the screen, which you can tell by looking
2777at the IsDefault bit in the flags field, you should allocate BlackPixel
2778and WhitePixel to match the values you set in the pScreen structure.
2779(Of course, you picked those values to begin with.)</para>
2780<para>
2781You can also wait and use AllocColor() to allocate blackPixel
2782and whitePixel after the default colormap has been created.
2783If the default colormap is static and you initialized it in
2784pScreen->CreateColormap, then use can use AllocColor afterwards
2785to choose pixel values with the closest rgb values to those
2786desired for blackPixel and whitePixel.
2787If the default colormap is dynamic and uninitialized, then
2788the rgb values you request will be obeyed, and AllocColor will
2789again choose pixel values for you.
2790These pixel values can then be stored into the screen.</para>
2791<para>
2792There are two ways to fill in the colormap.
2793The simplest way is to use the DIX function AllocColor.
2794<blockquote><programlisting>
2795
2796int AllocColor (pmap, pred, pgreen, pblue, pPix, client)
2797 ColormapPtr pmap;
2798 unsigned short *pred, *pgreen, *pblue;
2799 Pixel *pPix;
2800 int client;
2801
2802</programlisting></blockquote>
2803This takes three pointers to 16 bit color values and a pointer to a suggested
2804pixel value. The pixel value is either an index into one colormap or a
2805combination of three indices depending on the type of pmap.
2806If your colormap starts out empty, and you don't deliberately pick the same
2807value twice, you will always get your suggested pixel.
2808The truly nervous could check that the value returned in *pPix is the one
2809AllocColor was called with.
2810If you don't care which pixel is used, or would like them sequentially
2811allocated from entry 0, set *pPix to 0. This will find the first free
2812pixel and use that.</para>
2813<para>
2814AllocColor will take care of all the bookkeeping and will
2815call StoreColors to get the colormap rgb values initialized.
2816The hardware colormap will be changed whenever this colormap
2817is installed.</para>
2818<para>
2819If for some reason AllocColor doesn't do what you want, you can do your
2820own bookkeeping and call StoreColors yourself. This is much more difficult
2821and shouldn't be necessary for most devices.</para>
2822</section>
2823</section>
2824<section>
2825 <title>Fonts for Screens</title>
2826<para>
2827A font is a set of bitmaps that depict the symbols in a character set.
2828Each font is for only one typeface in a given size, in other words,
2829just one bitmap for each character. Parallel fonts may be available
2830in a variety of sizes and variations, including "bold" and "italic."
2831X supports fonts for 8-bit and 16-bit character codes (for oriental
2832languages that have more than 256 characters in the font). Glyphs are
2833bitmaps for individual characters.</para>
2834<para>
2835The source comes with some useful font files in an ASCII, plain-text
2836format that should be comprehensible on a wide variety of operating
2837systems. The text format, referred to as BDF, is a slight extension
2838of the current Adobe 2.1 Bitmap Distribution Format (Adobe Systems,
2839Inc.).</para>
2840<para>
2841A short paper in PostScript format is included with the sample server
2842that defines BDF. It includes helpful pictures, which is why it is
2843done in PostScript and is not included in this document.</para>
2844<para>
2845Your implementation should include some sort of font compiler to read
2846these files and generate binary files that are directly usable by your
2847server implementation. The sample server comes with the source for a
2848font compiler.</para>
2849<para>
2850It is important the font properties contained in the BDF files are
2851preserved across any font compilation. In particular, copyright
2852information cannot be casually tossed aside without legal
2853ramifications. Other properties will be important to some
2854sophisticated applications.</para>
2855<para>
2856All clients get font information from the server. Therefore, your
2857server can support any fonts it wants to. It should probably support
2858at least the fonts supplied with the X11 tape. In principle, you can
2859convert fonts from other sources or dream up your own fonts for use on
2860your server.</para>
2861<section>
2862<title>Portable Compiled Format</title>
2863<para>
2864A font compiler is supplied with the sample server. It has
2865compile-time switches to convert the BDF files into a portable binary
2866form, called Portable Compiled Format or PCF. This allows for an
2867arbitrary data format inside the file, and by describing the details
2868of the format in the header of the file, any PCF file can be read by
2869any PCF reading client. By selecting the format which matches the
2870required internal format for your renderer, the PCF reader can avoid
2871reformatting the data each time it is read in. The font compiler
2872should be quite portable.</para>
2873<para>
2874The fonts included with the tape are stored in fonts/bdf. The
2875font compiler is found in fonts/tools/bdftopcf.</para>
2876</section>
2877<section>
2878 <title>Font Realization</title>
2879<para>
2880Each screen configured into the server
2881has an opportunity at font-load time
2882to "realize" a font into some internal format if necessary.
2883This happens every time the font is loaded into memory.</para>
2884<para>
2885A font (FontRec in Xserver/include/dixfontstr.h) is
2886a device-independent structure containing a device-independent
2887representation of the font. When a font is created, it is "realized"
2888for each screen. At this point, the screen has the chance to convert
2889the font into some other format. The DDX layer can also put information
2890in the devPrivate storage.</para>
2891<para>
2892<blockquote><programlisting>
2893
2894 Bool pScreen->RealizeFont(pScr, pFont)
2895 ScreenPtr pScr;
2896 FontPtr pFont;
2897
2898 Bool pScreen->UnrealizeFont(pScr, pFont)
2899 ScreenPtr pScr;
2900 FontPtr pFont;
2901
2902</programlisting></blockquote>
2903RealizeFont and UnrealizeFont should calculate and allocate these extra data structures and
2904dispose of them when no longer needed.
2905These are called in response to OpenFont and CloseFont requests from
2906the client.
2907The sample server implementation is in fbscreen.c (which does very little).</para>
2908</section>
2909</section>
2910<section>
2911 <title>Other Screen Routines</title>
2912<para>
2913You must supply several other screen-specific routines for
2914your X server implementation.
2915Some of these are described in other sections:
2916<itemizedlist>
2917<listitem><para>
2918GetImage() is described in the Drawing Primitives section.</para></listitem>
2919<listitem><para>
2920GetSpans() is described in the Pixblit routine section.</para></listitem>
2921<listitem><para>
2922Several window and pixmap manipulation procedures are
2923described in the Window section under Drawables.</para></listitem>
2924<listitem><para>
2925The CreateGC() routine is described under Graphics Contexts.</para></listitem>
2926</itemizedlist>
2927</para>
2928<para>
2929<blockquote><programlisting>
2930
2931 void pScreen->QueryBestSize(kind, pWidth, pHeight)
2932 int kind;
2933 unsigned short *pWidth, *pHeight;
2934 ScreenPtr pScreen;
2935
2936</programlisting></blockquote>
2937QueryBestSize() returns the best sizes for cursors, tiles, and stipples
2938in response to client requests.
2939kind is one of the defined constants CursorShape, TileShape, or StippleShape
2940(defined in X.h).
2941For CursorShape, return the maximum width and
2942height for cursors that you can handle.
2943For TileShape and StippleShape, start with the suggested values in pWidth
2944and pHeight and modify them in place to be optimal values that are
2945greater than or equal to the suggested values.
2946The sample server implementation is in Xserver/fb/fbscreen.c.</para>
2947<para>
2948<blockquote><programlisting>
2949
2950 pScreen->SourceValidate(pDrawable, x, y, width, height)
2951 DrawablePtr pDrawable;
2952 int x, y, width, height;
2953 unsigned int subWindowMode;
2954
2955</programlisting></blockquote>
2956SourceValidate should be called by CopyArea/CopyPlane primitives when
2957the SourceValidate function pointer in the screen is non-null. If you know that
2958you will never need SourceValidate, you can avoid this check. Currently,
2959SourceValidate is used by the mi software cursor code to remove the cursor
2960from the screen when the source rectangle overlaps the cursor position.
2961x,y,width,height describe the source rectangle (source relative, that is)
2962for the copy operation. subWindowMode comes from the GC or source Picture.
2963</para>
2964<para>
2965<blockquote><programlisting>
2966
2967 Bool pScreen->SaveScreen(pScreen, on)
2968 ScreenPtr pScreen;
2969 int on;
2970
2971</programlisting></blockquote>
2972SaveScreen() is used for Screen Saver support (see WaitForSomething()).
2973pScreen is the screen to save.</para>
2974<para>
2975<blockquote><programlisting>
2976
2977 Bool pScreen->CloseScreen(pScreen)
2978 ScreenPtr pScreen;
2979
2980</programlisting></blockquote>
2981When the server is reset, it calls this routine for each screen.</para>
2982<para>
2983<blockquote><programlisting>
2984
2985 Bool pScreen->CreateScreenResources(pScreen)
2986 ScreenPtr pScreen;
2987
2988</programlisting></blockquote>
2989If this routine is not NULL, it will be called once per screen per
2990server initialization/reset after all modules have had a chance to
2991request private space on all structures that support them (see
2992<xref linkend="wrappers_and_privates"/> below). You may create resources
2993in this function instead of in the
2994screen init function passed to AddScreen in order to guarantee that
2995all pre-allocated space requests have been registered first. With the
2996new devPrivates mechanism, this is not strictly necessary, however.
2997This routine returns TRUE if successful.</para>
2998</section>
2999</section>
3000<section>
3001<title>Drawables</title>
3002<para>
3003A drawable is a descriptor of a surface that graphics are drawn into, either
3004a window on the screen or a pixmap in memory.</para>
3005<para>
3006Each drawable has a type, class,
3007ScreenPtr for the screen it is associated with, depth, position, size,
3008and serial number.
3009The type is one of the defined constants DRAWABLE_PIXMAP,
3010DRAWABLE_WINDOW and UNDRAWABLE_WINDOW.
3011(An undrawable window is used for window class InputOnly.)
3012The serial number is guaranteed to be unique across drawables, and
3013is used in determining
3014the validity of the clipping information in a GC.
3015The screen selects the set of procedures used to manipulate and draw into the
3016drawable. Position is used (currently) only by windows; pixmaps must
3017set these fields to 0,0 as this reduces the amount of conditional code
3018executed throughout the mi code. Size indicates the actual client-specified
3019size of the drawable.
3020There are, in fact, no other fields that a window drawable and pixmap
3021drawable have in common besides those mentioned here.</para>
3022<para>
3023Both PixmapRecs and WindowRecs are structs that start with a drawable
3024and continue on with more fields. Pixmaps have a single pointer field
3025named devPrivate which usually points to the pixmap data but could conceivably be
3026used for anything that DDX wants. Both windows and pixmaps also have a
3027devPrivates field which can be used for DDX specific data (see <xref linkend="wrappers_and_privates"/>
3028below). This is done because different graphics hardware has
3029different requirements for management; if the graphics is always
3030handled by a processor with an independent address space, there is no
3031point having a pointer to the bit image itself.</para>
3032<para>
3033The definition of a drawable and a pixmap can be found in the file
3034Xserver/include/pixmapstr.h.
3035The definition of a window can be found in the file Xserver/include/windowstr.h.</para>
3036<section>
3037 <title>Pixmaps</title>
3038<para>
3039A pixmap is a three-dimensional array of bits stored somewhere offscreen,
3040rather than in the visible portion of the screen's display frame buffer. It
3041can be used as a source or destination in graphics operations. There is no
3042implied interpretation of the pixel values in a pixmap, because it has no
3043associated visual or colormap. There is only a depth that indicates the
3044number of significant bits per pixel. Also, there is no implied physical
3045size for each pixel; all graphic units are in numbers of pixels. Therefore,
3046a pixmap alone does not constitute a complete image; it represents only a
3047rectangular array of pixel values.</para>
3048<para>
3049Note that the pixmap data structure is reference-counted.</para>
3050<para>
3051The server implementation is free to put the pixmap data
3052anywhere it sees fit, according to its graphics hardware setup. Many
3053implementations will simply have the data dynamically allocated in the
3054server's address space. More sophisticated implementations may put the
3055data in undisplayed framebuffer storage.</para>
3056<para>
3057In addition to dynamic devPrivates (see <xref linkend="wrappers_and_privates"/>
3058below), the pixmap data structure has two fields that are private to
3059the device. Although you can use them for anything you want, they
3060have intended purposes. devKind is intended to be a device specific
3061indication of the pixmap location (host memory, off-screen, etc.). In
3062the sample server, since all pixmaps are in memory, devKind stores the
3063width of the pixmap in bitmap scanline units. devPrivate is usually
3064a pointer to the bits in the pixmap.</para>
3065<para>
3066A bitmap is a pixmap that is one bit deep.</para>
3067<para>
3068<blockquote><programlisting>
3069
3070 PixmapPtr pScreen->CreatePixmap(pScreen, width, height, depth)
3071 ScreenPtr pScreen;
3072 int width, height, depth;
3073
3074</programlisting></blockquote>
3075This ScreenRec procedure must create a pixmap of the size
3076requested.
3077It must allocate a PixmapRec and fill in all of the fields.
3078The reference count field must be set to 1.
3079If width or height are zero, no space should be allocated
3080for the pixmap data, and if the implementation is using the
3081devPrivate field as a pointer to the pixmap data, it should be
3082set to NULL.
3083If successful, it returns a pointer to the new pixmap; if not, it returns NULL.
3084See Xserver/fb/fbpixmap.c for the sample server implementation.</para>
3085<para>
3086<blockquote><programlisting>
3087
3088 Bool pScreen->DestroyPixmap(pPixmap)
3089 PixmapPtr pPixmap;
3090
3091</programlisting></blockquote>
3092This ScreenRec procedure must "destroy" a pixmap.
3093It should decrement the reference count and, if zero, it
3094must deallocate the PixmapRec and all attached devPrivate blocks.
3095If successful, it returns TRUE.
3096See Xserver/fb/fbpixmap.c for the sample server implementation.</para>
3097<para>
3098<blockquote><programlisting>
3099
3100 Bool
3101 pScreen->ModifyPixmapHeader(pPixmap, width, height, depth, bitsPerPixel, devKind, pPixData)
3102 PixmapPtr pPixmap;
3103 int width;
3104 int height;
3105 int depth;
3106 int bitsPerPixel;
3107 int devKind;
3108 pointer pPixData;
3109
3110</programlisting></blockquote>
3111This routine takes a pixmap header and initializes the fields of the PixmapRec to the
3112parameters of the same name. pPixmap must have been created via
3113pScreen->CreatePixmap with a zero width or height to avoid
3114allocating space for the pixmap data. pPixData is assumed to be the
3115pixmap data; it will be stored in an implementation-dependent place
3116(usually pPixmap->devPrivate.ptr). This routine returns
3117TRUE if successful. See Xserver/mi/miscrinit.c for the sample
3118server implementation.</para>
3119<para>
3120<blockquote><programlisting>
3121
3122 PixmapPtr
3123 GetScratchPixmapHeader(pScreen, width, height, depth, bitsPerPixel, devKind, pPixData)
3124 ScreenPtr pScreen;
3125 int width;
3126 int height;
3127 int depth;
3128 int bitsPerPixel;
3129 int devKind;
3130 pointer pPixData;
3131
3132 void FreeScratchPixmapHeader(pPixmap)
3133 PixmapPtr pPixmap;
3134
3135</programlisting></blockquote>
3136DDX should use these two DIX routines when it has a buffer of raw
3137image data that it wants to manipulate as a pixmap temporarily,
3138usually so that some other part of the server can be leveraged to
3139perform some operation on the data. The data should be passed in
3140pPixData, and will be stored in an implementation-dependent place
3141(usually pPixmap->devPrivate.ptr). The other
3142fields go into the corresponding PixmapRec fields.
3143If successful, GetScratchPixmapHeader returns a valid PixmapPtr which can
3144be used anywhere the server expects a pixmap, else
3145it returns NULL. The pixmap should be released when no longer needed
3146(usually within the same function that allocated it)
3147with FreeScratchPixmapHeader.</para>
3148</section>
3149<section>
3150 <title>Windows</title>
3151<para>
3152A window is a visible, or potentially visible, rectangle on the screen.
3153DIX windowing functions maintain an internal n-ary tree data structure, which
3154represents the current relationships of the mapped windows.
3155Windows that are contained in another window are children of that window and
3156are clipped to the boundaries of the parent.
3157The root window in the tree is the window for the entire screen.
3158Sibling windows constitute a doubly-linked list; the parent window has a pointer
3159to the head and tail of this list.
3160Each child also has a pointer to its parent.</para>
3161<para>
3162The border of a window is drawn by a DDX procedure when DIX requests that it
3163be drawn. The contents of the window is drawn by the client through
3164requests to the server.</para>
3165<para>
3166Window painting is orchestrated through an expose event system.
3167When a region is exposed,
3168DIX generates an expose event, telling the client to repaint the window and
3169passing the region that is the minimal area needed to be repainted.</para>
3170<para>
3171As a favor to clients, the server may retain
3172the output to the hidden parts of windows
3173in off-screen memory; this is called "backing store".
3174When a part of such a window becomes exposed, it
3175can quickly move pixels into place instead of
3176triggering an expose event and waiting for a client on the other
3177end of the network to respond.
3178Even if the network response is insignificant, the time to
3179intelligently paint a section of a window is usually more than
3180the time to just copy already-painted sections.
3181At best, the repainting involves blanking out the area to a background color,
3182which will take about the
3183same amount of time.
3184In this way, backing store can dramatically increase the
3185performance of window moves.</para>
3186<para>
3187On the other hand, backing store can be quite complex, because
3188all graphics drawn to hidden areas must be intercepted and redirected
3189to the off-screen window sections.
3190Not only can this be complicated for the server programmer,
3191but it can also impact window painting performance.
3192The backing store implementation can choose, at any time, to
3193forget pieces of backing that are written into, relying instead upon
3194expose events to repaint for simplicity.</para>
3195<para>
3196In X, the decision to use the backing-store scheme is made
3197by you, the server implementor. The sample server implements
3198backing store "for free" by reusing the infrastructure for the Composite
3199extension. As a side effect, it treats the WhenMapped and Always hints
3200as equivalent. However, it will never forget pixel contents when the
3201window is mapped.</para>
3202<para>
3203When a window operation is requested by the client,
3204such as a window being created or moved,
3205a new state is computed.
3206During this transition, DIX informs DDX what rectangles in what windows are about to
3207become obscured and what rectangles in what windows have become exposed.
3208This provides a hook for the implementation of backing store.
3209If DDX is unable to restore exposed regions, DIX generates expose
3210events to the client.
3211It is then the client's responsibility to paint the
3212window parts that were exposed but not restored.</para>
3213<para>
3214If a window is resized, pixels sometimes need to be
3215moved, depending upon
3216the application.
3217The client can request "Gravity" so that
3218certain blocks of the window are
3219moved as a result of a resize.
3220For instance, if the window has controls or other items
3221that always hang on the edge of the
3222window, and that edge is moved as a result of the resize,
3223then those pixels should be moved
3224to avoid having the client repaint it.
3225If the client needs to repaint it anyway, such an operation takes
3226time, so it is desirable
3227for the server to approximate the appearance of the window as best
3228it can while waiting for the client
3229to do it perfectly.
3230Gravity is used for that, also.</para>
3231<para>
3232The window has several fields used in drawing
3233operations:
3234<itemizedlist>
3235<listitem><para>
3236clipList - This region, in conjunction with
3237the client clip region in the gc, is used to clip output.
3238clipList has the window's children subtracted from it, in addition to pieces of sibling windows
3239that overlap this window. To get the list with the
3240children included (subwindow-mode is IncludeInferiors),
3241the routine NotClippedByChildren(pWin) returns the unclipped region.</para></listitem>
3242<listitem><para>
3243borderClip is the region used by CopyWindow and
3244includes the area of the window, its children, and the border, but with the
3245overlapping areas of sibling children removed.</para></listitem>
3246</itemizedlist>
3247Most of the other fields are for DIX use only.</para>
3248<section>
3249<title>Window Procedures in the ScreenRec</title>
3250<para>
3251You should implement
3252all of the following procedures and store pointers to them in the screen record.</para>
3253<para>
3254The device-independent portion of the server "owns" the window tree.
3255However, clever hardware might want to know the relationship of
3256mapped windows. There are pointers to procedures
3257in the ScreenRec data structure that are called to give the hardware
3258a chance to update its internal state. These are helpers and
3259hints to DDX only;
3260they do not change the window tree, which is only changed by DIX.</para>
3261<para>
3262<blockquote><programlisting>
3263
3264 Bool pScreen->CreateWindow(pWin)
3265 WindowPtr pWin;
3266
3267</programlisting></blockquote>
3268This routine is a hook for when DIX creates a window.
3269It should fill in the "Window Procedures in the WindowRec" below
3270and also allocate the devPrivate block for it.</para>
3271<para>
3272See Xserver/fb/fbwindow.c for the sample server implementation.</para>
3273<para>
3274<blockquote><programlisting>
3275
3276 Bool pScreen->DestroyWindow(pWin);
3277 WindowPtr pWin;
3278
3279</programlisting></blockquote>
3280This routine is a hook for when DIX destroys a window.
3281It should deallocate the devPrivate block for it and any other blocks that need
3282to be freed, besides doing other cleanup actions.</para>
3283<para>
3284See Xserver/fb/fbwindow.c for the sample server implementation.</para>
3285<para>
3286<blockquote><programlisting>
3287
3288 Bool pScreen->PositionWindow(pWin, x, y);
3289 WindowPtr pWin;
3290 int x, y;
3291
3292</programlisting></blockquote>
3293This routine is a hook for when DIX moves or resizes a window.
3294It should do whatever private operations need to be done when a window is moved or resized.
3295For instance, if DDX keeps a pixmap tile used for drawing the background
3296or border, and it keeps the tile rotated such that it is longword
3297aligned to longword locations in the frame buffer, then you should rotate your tiles here.
3298The actual graphics involved in moving the pixels on the screen and drawing the
3299border are handled by CopyWindow(), below.</para>
3300<para>
3301See Xserver/fb/fbwindow.c for the sample server implementation.</para>
3302<para>
3303<blockquote><programlisting>
3304
3305 Bool pScreen->RealizeWindow(pWin);
3306 WindowPtr pWin;
3307
3308 Bool pScreen->UnrealizeWindow(pWin);
3309 WindowPtr pWin;
3310
3311</programlisting></blockquote>
3312These routines are hooks for when DIX maps (makes visible) and unmaps
3313(makes invisible) a window. It should do whatever private operations
3314need to be done when these happen, such as allocating or deallocating
3315structures that are only needed for visible windows. RealizeWindow
3316does NOT draw the window border, background or contents;
3317UnrealizeWindow does NOT erase the window or generate exposure events
3318for underlying windows; this is taken care of by DIX. DIX does,
3319however, call PaintWindowBackground() and PaintWindowBorder() to
3320perform some of these.</para>
3321<para>
3322<blockquote><programlisting>
3323
3324 Bool pScreen->ChangeWindowAttributes(pWin, vmask)
3325 WindowPtr pWin;
3326 unsigned long vmask;
3327
3328</programlisting></blockquote>
3329ChangeWindowAttributes is called whenever DIX changes window
3330attributes, such as the size, front-to-back ordering, title, or
3331anything of lesser severity that affects the window itself. The
3332sample server implements this routine. It computes accelerators for
3333quickly putting up background and border tiles. (See description of
3334the set of routines stored in the WindowRec.)</para>
3335<para>
3336<blockquote><programlisting>
3337
3338 int pScreen->ValidateTree(pParent, pChild, kind)
3339 WindowPtr pParent, pChild;
3340 VTKind kind;
3341
3342</programlisting></blockquote>
3343ValidateTree calculates the clipping region for the parent window and
3344all of its children. This routine must be provided. The sample server
3345has a machine-independent version in Xserver/mi/mivaltree.c. This is
3346a very difficult routine to replace.</para>
3347<para>
3348<blockquote><programlisting>
3349
3350 void pScreen->PostValidateTree(pParent, pChild, kind)
3351 WindowPtr pParent, pChild;
3352 VTKind kind;
3353
3354</programlisting></blockquote>
3355If this routine is not NULL, DIX calls it shortly after calling
3356ValidateTree, passing it the same arguments. This is useful for
3357managing multi-layered framebuffers.
3358The sample server sets this to NULL.</para>
3359<para>
3360<blockquote><programlisting>
3361
3362 void pScreen->WindowExposures(pWin, pRegion, pBSRegion)
3363 WindowPtr pWin;
3364 RegionPtr pRegion;
3365 RegionPtr pBSRegion;
3366
3367</programlisting></blockquote>
3368The WindowExposures() routine
3369paints the border and generates exposure events for the window.
3370pRegion is an unoccluded region of the window, and pBSRegion is an
3371occluded region that has backing store.
3372Since exposure events include a rectangle describing what was exposed,
3373this routine may have to send back a series of exposure events, one for
3374each rectangle of the region.
3375The count field in the expose event is a hint to the
3376client as to the number of
3377regions that are after this one.
3378This routine must be provided. The sample
3379server has a machine-independent version in Xserver/mi/miexpose.c.</para>
3380<para>
3381<blockquote><programlisting>
3382
3383 void pScreen->ClipNotify (pWin, dx, dy)
3384 WindowPtr pWin;
3385 int dx, dy;
3386
3387</programlisting></blockquote>
3388Whenever the cliplist for a window is changed, this function is called to
3389perform whatever hardware manipulations might be necessary. When called,
3390the clip list and border clip regions in the window are set to the new
3391values. dx,dy are the distance that the window has been moved (if at all).</para>
3392</section>
3393<section>
3394 <title>Window Painting Procedures</title>
3395<para>
3396In addition to the procedures listed above, there are two routines which
3397manipulate the actual window image directly.
3398In the sample server, mi implementations will work for
3399most purposes and fb routines speed up situations, such
3400as solid backgrounds/borders or tiles that are 8, 16 or 32 pixels square.</para>
3401<para>
3402<blockquote><programlisting>
3403
3404 void pScreen->ClearToBackground(pWin, x, y, w, h, generateExposures);
3405 WindowPtr pWin;
3406 int x, y, w, h;
3407 Bool generateExposures;
3408
3409</programlisting></blockquote>
3410This routine is called on a window in response to a ClearToBackground request
3411from the client.
3412This request has two different but related functions, depending upon generateExposures.</para>
3413<para>
3414If generateExposures is true, the client is declaring that the given rectangle
3415on the window is incorrectly painted and needs to be repainted.
3416The sample server implementation calculates the exposure region
3417and hands it to the DIX procedure HandleExposures(), which
3418calls the WindowExposures() routine, below, for the window
3419and all of its child windows.</para>
3420<para>
3421If generateExposures is false, the client is trying to simply erase part
3422of the window to the background fill style.
3423ClearToBackground should write the background color or tile to the
3424rectangle in question (probably using PaintWindowBackground).
3425If w or h is zero, it clears all the way to the right or lower edge of the window.</para>
3426<para>
3427The sample server implementation is in Xserver/mi/miwindow.c.</para>
3428<para>
3429<blockquote><programlisting>
3430
3431 void pScreen->CopyWindow(pWin, oldpt, oldRegion);
3432 WindowPtr pWin;
3433 DDXPointRec oldpt;
3434 RegionPtr oldRegion;
3435
3436</programlisting></blockquote>
3437CopyWindow is called when a window is moved, and graphically moves to
3438pixels of a window on the screen. It should not change any other
3439state within DDX (see PositionWindow(), above).</para>
3440<para>
3441oldpt is the old location of the upper-left corner. oldRegion is the
3442old region it is coming from. The new location and new region is
3443stored in the WindowRec. oldRegion might modified in place by this
3444routine (the sample implementation does this).</para>
3445<para>
3446CopyArea could be used, except that this operation has more
3447complications. First of all, you do not want to copy a rectangle onto
3448a rectangle. The original window may be obscured by other windows,
3449and the new window location may be similarly obscured. Second, some
3450hardware supports multiple windows with multiple depths, and your
3451routine needs to take care of that.</para>
3452<para>
3453The pixels in oldRegion (with reference point oldpt) are copied to the
3454window's new region (pWin->borderClip). pWin->borderClip is gotten
3455directly from the window, rather than passing it as a parameter.</para>
3456<para>
3457The sample server implementation is in Xserver/fb/fbwindow.c.</para>
3458</section>
3459<section>
3460<title>Screen Operations for Multi-Layered Framebuffers</title>
3461<para>
3462The following screen functions are useful if you have a framebuffer with
3463multiple sets of independent bit planes, e.g. overlays or underlays in
3464addition to the "main" planes. If you have a simple single-layer
3465framebuffer, you should probably use the mi versions of these routines
3466in mi/miwindow.c. This can be easily accomplished by calling miScreenInit.</para>
3467<para>
3468<blockquote><programlisting>
3469
3470 void pScreen->MarkWindow(pWin)
3471 WindowPtr pWin;
3472
3473</programlisting></blockquote>
3474This formerly dix function MarkWindow has moved to ddx and is accessed
3475via this screen function. This function should store something,
3476usually a pointer to a device-dependent structure, in pWin->valdata so
3477that ValidateTree has the information it needs to validate the window.</para>
3478<para>
3479<blockquote><programlisting>
3480
3481 Bool pScreen->MarkOverlappedWindows(parent, firstChild, ppLayerWin)
3482 WindowPtr parent;
3483 WindowPtr firstChild;
3484 WindowPtr * ppLayerWin;
3485
3486</programlisting></blockquote>
3487This formerly dix function MarkWindow has moved to ddx and is accessed
3488via this screen function. In the process, it has grown another
3489parameter: ppLayerWin, which is filled in with a pointer to the window
3490at which save under marking and ValidateTree should begin. In the
3491single-layered framebuffer case, pLayerWin == pWin.</para>
3492<para>
3493<blockquote><programlisting>
3494
3495 Bool pScreen->ChangeSaveUnder(pLayerWin, firstChild)
3496 WindowPtr pLayerWin;
3497 WindowPtr firstChild;
3498
3499</programlisting></blockquote>
3500The dix functions ChangeSaveUnder and CheckSaveUnder have moved to ddx and
3501are accessed via this screen function. pLayerWin should be the window
3502returned in the ppLayerWin parameter of MarkOverlappedWindows. The function
3503may turn on backing store for windows that might be covered, and may partially
3504turn off backing store for windows. It returns TRUE if PostChangeSaveUnder
3505needs to be called to finish turning off backing store.</para>
3506<para>
3507<blockquote><programlisting>
3508
3509 void pScreen->PostChangeSaveUnder(pLayerWin, firstChild)
3510 WindowPtr pLayerWin;
3511 WindowPtr firstChild;
3512
3513</programlisting></blockquote>
3514The dix function DoChangeSaveUnder has moved to ddx and is accessed via
3515this screen function. This function completes the job of turning off
3516backing store that was started by ChangeSaveUnder.</para>
3517<para>
3518<blockquote><programlisting>
3519
3520 void pScreen->MoveWindow(pWin, x, y, pSib, kind)
3521 WindowPtr pWin;
3522 int x;
3523 int y;
3524 WindowPtr pSib;
3525 VTKind kind;
3526
3527</programlisting></blockquote>
3528The formerly dix function MoveWindow has moved to ddx and is accessed via
3529this screen function. The new position of the window is given by
3530x,y. kind is VTMove if the window is only moving, or VTOther if
3531the border is also changing.</para>
3532<para>
3533<blockquote><programlisting>
3534
3535 void pScreen->ResizeWindow(pWin, x, y, w, h, pSib)
3536 WindowPtr pWin;
3537 int x;
3538 int y;
3539 unsigned int w;
3540 unsigned int h;
3541 WindowPtr pSib;
3542
3543</programlisting></blockquote>
3544The formerly dix function SlideAndSizeWindow has moved to ddx and is accessed via
3545this screen function. The new position is given by x,y. The new size
3546is given by w,h.</para>
3547<para>
3548<blockquote><programlisting>
3549
3550 WindowPtr pScreen->GetLayerWindow(pWin)
3551 WindowPtr pWin
3552
3553</programlisting></blockquote>
3554This is a new function which returns a child of the layer parent of pWin.</para>
3555<para>
3556<blockquote><programlisting>
3557
3558 void pScreen->HandleExposures(pWin)
3559 WindowPtr pWin;
3560
3561</programlisting></blockquote>
3562The formerly dix function HandleExposures has moved to ddx and is accessed via
3563this screen function. This function is called after ValidateTree and
3564uses the information contained in valdata to send exposures to windows.</para>
3565<para>
3566<blockquote><programlisting>
3567
3568 void pScreen->ReparentWindow(pWin, pPriorParent)
3569 WindowPtr pWin;
3570 WindowPtr pPriorParent;
3571
3572</programlisting></blockquote>
3573This function will be called when a window is reparented. At the time of
3574the call, pWin will already be spliced into its new position in the
3575window tree, and pPriorParent is its previous parent. This function
3576can be NULL.</para>
3577<para>
3578<blockquote><programlisting>
3579
3580 void pScreen->SetShape(pWin)
3581 WindowPtr pWin;
3582
3583</programlisting></blockquote>
3584The formerly dix function SetShape has moved to ddx and is accessed via
3585this screen function. The window's new shape will have already been
3586stored in the window when this function is called.</para>
3587<para>
3588<blockquote><programlisting>
3589
3590 void pScreen->ChangeBorderWidth(pWin, width)
3591 WindowPtr pWin;
3592 unsigned int width;
3593
3594</programlisting></blockquote>
3595The formerly dix function ChangeBorderWidth has moved to ddx and is accessed via
3596this screen function. The new border width is given by width.</para>
3597<para>
3598<blockquote><programlisting>
3599
3600 void pScreen->MarkUnrealizedWindow(pChild, pWin, fromConfigure)
3601 WindowPtr pChild;
3602 WindowPtr pWin;
3603 Bool fromConfigure;
3604
3605</programlisting></blockquote>
3606This function is called for windows that are being unrealized as part of
3607an UnrealizeTree. pChild is the window being unrealized, pWin is an
3608ancestor, and the fromConfigure value is simply propagated from UnrealizeTree.</para>
3609</section>
3610</section>
3611</section>
3612<section>
3613<title>Graphics Contexts and Validation</title>
3614<para>
3615This graphics context (GC) contains state variables such as foreground and
3616background pixel value (color), the current line style and width,
3617the current tile or stipple for pattern generation, the current font for text
3618generation, and other similar attributes.</para>
3619<para>
3620In many graphics systems, the equivalent of the graphics context and the
3621drawable are combined as one entity.
3622The main distinction between the two kinds of status is that a drawable
3623describes a writing surface and the writings that may have already been done
3624on it, whereas a graphics context describes the drawing process.
3625A drawable is like a chalkboard.
3626A GC is like a piece of chalk.</para>
3627<para>
3628Unlike many similar systems, there is no "current pen location."
3629Every graphic operation is accompanied by the coordinates where it is to happen.</para>
3630<para>
3631The GC also includes two vectors of procedure pointers, the first
3632operate on the GC itself and are called GC funcs. The second, called
3633GC ops,
3634contains the functions that carry out the fundamental graphic operations
3635such as drawing lines, polygons, arcs, text, and copying bitmaps.
3636The DDX graphic software can, if it
3637wants to be smart, change these two vectors of procedure pointers
3638to take advantage of hardware/firmware in the server machine, which can do
3639a better job under certain circumstances. To reduce the amount of memory
3640consumed by each GC, it is wise to create a few "boilerplate" GC ops vectors
3641which can be shared by every GC which matches the constraints for that set.
3642Also, it is usually reasonable to have every GC created by a particular
3643module to share a common set of GC funcs. Samples of this sort of
3644sharing can be seen in fb/fbgc.c.</para>
3645<para>
3646The DDX software is notified any time the client (or DIX) uses a changed GC.
3647For instance, if the hardware has special support for drawing fixed-width
3648fonts, DDX can intercept changes to the current font in a GC just before
3649drawing is done. It can plug into either a fixed-width procedure that makes
3650the hardware draw characters, or a variable-width procedure that carefully
3651lays out glyphs by hand in software, depending upon the new font that is
3652selected.</para>
3653<para>
3654A definition of these structures can be found in the file
3655Xserver/include/gcstruct.h.</para>
3656<para>
3657Also included in each GC is support for dynamic devPrivates, which the
3658DDX can use for any purpose (see <xref linkend="wrappers_and_privates"/> below).</para>
3659<para>
3660The DIX routines available for manipulating GCs are
3661CreateGC, ChangeGC, ChangeGCXIDs, CopyGC, SetClipRects, SetDashes, and FreeGC.
3662<blockquote><programlisting>
3663
3664 GCPtr CreateGC(pDrawable, mask, pval, pStatus)
3665 DrawablePtr pDrawable;
3666 BITS32 mask;
3667 XID *pval;
3668 int *pStatus;
3669
3670 int ChangeGC(client, pGC, mask, pUnion)
3671 ClientPtr client;
3672 GCPtr pGC;
3673 BITS32 mask;
3674 ChangeGCValPtr pUnion;
3675
3676 int ChangeGCXIDs(client, pGC, mask, pC32)
3677 ClientPtr client;
3678 GCPtr pGC;
3679 BITS32 mask;
3680 CARD32 *pC32;
3681
3682 int CopyGC(pgcSrc, pgcDst, mask)
3683 GCPtr pgcSrc;
3684 GCPtr pgcDst;
3685 BITS32 mask;
3686
3687 int SetClipRects(pGC, xOrigin, yOrigin, nrects, prects, ordering)
3688 GCPtr pGC;
3689 int xOrigin, yOrigin;
3690 int nrects;
3691 xRectangle *prects;
3692 int ordering;
3693
3694 SetDashes(pGC, offset, ndash, pdash)
3695 GCPtr pGC;
3696 unsigned offset;
3697 unsigned ndash;
3698 unsigned char *pdash;
3699
3700 int FreeGC(pGC, gid)
3701 GCPtr pGC;
3702 GContext gid;
3703
3704</programlisting></blockquote>
3705</para>
3706<para>
3707As a convenience, each Screen structure contains an array of
3708GCs that are preallocated, one at each depth the screen supports.
3709These are particularly useful in the mi code. Two DIX routines
3710must be used to get these GCs:
3711<blockquote><programlisting>
3712
3713 GCPtr GetScratchGC(depth, pScreen)
3714 int depth;
3715 ScreenPtr pScreen;
3716
3717 FreeScratchGC(pGC)
3718 GCPtr pGC;
3719
3720</programlisting></blockquote>
3721Always use these two routines, don't try to extract the scratch
3722GC yourself -- someone else might be using it, so a new one must
3723be created on the fly.</para>
3724<para>
3725If you need a GC for a very long time, say until the server is restarted,
3726you should not take one from the pool used by GetScratchGC, but should
3727get your own using CreateGC or CreateScratchGC.
3728This leaves the ones in the pool free for routines that only need it for
3729a little while and don't want to pay a heavy cost to get it.
3730<blockquote><programlisting>
3731
3732 GCPtr CreateScratchGC(pScreen, depth)
3733 ScreenPtr pScreen;
3734 int depth;
3735
3736</programlisting></blockquote>
3737NULL is returned if the GC cannot be created.
3738The GC returned can be freed with FreeScratchGC.</para>
3739<section>
3740 <title>Details of Operation</title>
3741<para>
3742At screen initialization, a screen must supply a GC creation procedure.
3743At GC creation, the screen must fill in GC funcs and GC ops vectors
3744(Xserver/include/gcstruct.h). For any particular GC, the func vector
3745must remain constant, while the op vector may vary. This invariant is to
3746ensure that Wrappers work correctly.</para>
3747<para>
3748When a client request is processed that results in a change
3749to the GC, the device-independent state of the GC is updated.
3750This includes a record of the state that changed.
3751Then the ChangeGC GC func is called.
3752This is useful for graphics subsystems that are able to process
3753state changes in parallel with the server CPU.
3754DDX may opt not to take any action at GC-modify time.
3755This is more efficient if multiple GC-modify requests occur
3756between draws using a given GC.</para>
3757<para>
3758Validation occurs at the first draw operation that specifies the GC after
3759that GC was modified. DIX calls then the ValidateGC GC func. DDX should
3760then update its internal state. DDX internal state may be stored as one or
3761more of the following: 1) device private block on the GC; 2) hardware
3762state; 3) changes to the GC ops.</para>
3763<para>
3764The GC contains a serial number, which is loaded with a number fetched from
3765the window that was drawn into the last time the GC was used. The serial
3766number in the drawable is changed when the drawable's
3767clipList or absCorner changes. Thus, by
3768comparing the GC serial number with the drawable serial number, DIX can
3769force a validate if the drawable has been changed since the last time it
3770was used with this GC.</para>
3771<para>
3772In addition, the drawable serial number is always guaranteed to have the
3773most significant bit set to 0. Thus, the DDX layer can set the most
3774significant bit of the serial number to 1 in a GC to force a validate the next time
3775the GC is used. DIX also uses this technique to indicate that a change has
3776been made to the GC by way of a SetGC, a SetDashes or a SetClip request.</para>
3777</section>
3778<section>
3779 <title>GC Handling Routines</title>
3780<para>
3781The ScreenRec data structure has a pointer for
3782CreateGC().
3783<blockquote><programlisting>
3784
3785 Bool pScreen->CreateGC(pGC)
3786 GCPtr pGC;
3787</programlisting></blockquote>
3788This routine must fill in the fields of
3789a dynamically allocated GC that is passed in.
3790It does NOT allocate the GC record itself or fill
3791in the defaults; DIX does that.</para>
3792<para>
3793This must fill in both the GC funcs and ops; none of the drawing
3794functions will be called before the GC has been validated,
3795but the others (dealing with allocating of clip regions,
3796changing and destroying the GC, etc.) might be.</para>
3797<para>
3798The GC funcs vector contains pointers to 7
3799routines and a devPrivate field:
3800<blockquote><programlisting>
3801
3802 pGC->funcs->ChangeGC(pGC, changes)
3803 GCPtr pGC;
3804 unsigned long changes;
3805
3806</programlisting></blockquote>
3807This GC func is called immediately after a field in the GC is changed.
3808changes is a bit mask indicating the changed fields of the GC in this
3809request.</para>
3810<para>
3811The ChangeGC routine is useful if you have a system where
3812state-changes to the GC can be swallowed immediately by your graphics
3813system, and a validate is not necessary.</para>
3814<para>
3815<blockquote><programlisting>
3816
3817 pGC->funcs->ValidateGC(pGC, changes, pDraw)
3818 GCPtr pGC;
3819 unsigned long changes;
3820 DrawablePtr pDraw;
3821
3822</programlisting></blockquote>
3823ValidateGC is called by DIX just before the GC will be used when one
3824of many possible changes to the GC or the graphics system has
3825happened. It can modify devPrivates data attached to the GC,
3826change the op vector, or change hardware according to the
3827values in the GC. It may not change the device-independent portion of
3828the GC itself.</para>
3829<para>
3830In almost all cases, your ValidateGC() procedure should take the
3831regions that drawing needs to be clipped to and combine them into a
3832composite clip region, which you keep a pointer to in the private part
3833of the GC. In this way, your drawing primitive routines (and whatever
3834is below them) can easily determine what to clip and where. You
3835should combine the regions clientClip (the region that the client
3836desires to clip output to) and the region returned by
3837NotClippedByChildren(), in DIX. An example is in Xserver/fb/fbgc.c.</para>
3838<para>
3839Some kinds of extension software may cause this routine to be called
3840more than originally intended; you should not rely on algorithms that
3841will break under such circumstances.</para>
3842<para>
3843See the Strategies document for more information on creatively using
3844this routine.</para>
3845<para>
3846<blockquote><programlisting>
3847
3848 pGC->funcs->CopyGC(pGCSrc, mask, pGCDst)
3849 GCPtr pGCSrc;
3850 unsigned long mask;
3851 GCPtr pGCDst;
3852
3853</programlisting></blockquote>
3854This routine is called by DIX when a GC is being copied to another GC.
3855This is for situations where dynamically allocated chunks of memory
3856are stored in the GC's dynamic devPrivates and need to be transferred to
3857the destination GC.</para>
3858<para>
3859<blockquote><programlisting>
3860
3861 pGC->funcs->DestroyGC(pGC)
3862 GCPtr pGC;
3863
3864</programlisting></blockquote>
3865This routine is called before the GC is destroyed for the
3866entity interested in this GC to clean up after itself.
3867This routine is responsible for freeing any auxiliary storage allocated.</para>
3868</section>
3869<section>
3870 <title>GC Clip Region Routines</title>
3871<para>
3872The GC clientClip field requires three procedures to manage it. These
3873procedures are in the GC funcs vector. The underlying principle is that dix
3874knows nothing about the internals of the clipping information, (except when
3875it has come from the client), and so calls ddX whenever it needs to copy,
3876set, or destroy such information. It could have been possible for dix not
3877to allow ddX to touch the field in the GC, and require it to keep its own
3878copy in devPriv, but since clip masks can be very large, this seems like a
3879bad idea. Thus, the server allows ddX to do whatever it wants to the
3880clientClip field of the GC, but requires it to do all manipulation itself.</para>
3881<para>
3882<blockquote><programlisting>
3883
3884 void pGC->funcs->ChangeClip(pGC, type, pValue, nrects)
3885 GCPtr pGC;
3886 int type;
3887 char *pValue;
3888 int nrects;
3889
3890</programlisting></blockquote>
3891This routine is called whenever the client changes the client clip
3892region. The pGC points to the GC involved, the type tells what form
3893the region has been sent in. If type is CT_NONE, then there is no
3894client clip. If type is CT_UNSORTED, CT_YBANDED or CT_YXBANDED, then
3895pValue pointer to a list of rectangles, nrects long. If type is
3896CT_REGION, then pValue pointer to a RegionRec from the mi region code.
3897If type is CT_PIXMAP pValue is a pointer to a pixmap. (The defines
3898for CT_NONE, etc. are in Xserver/include/gc.h.) This routine is
3899responsible for incrementing any necessary reference counts (e.g. for
3900a pixmap clip mask) for the new clipmask and freeing anything that
3901used to be in the GC's clipMask field. The lists of rectangles passed
3902in can be freed with Xfree(), the regions can be destroyed with the
3903RegionDestroy field in the screen, and pixmaps can be destroyed by
3904calling the screen's DestroyPixmap function. DIX and MI code expect
3905what they pass in to this to be freed or otherwise inaccessible, and
3906will never look inside what's been put in the GC. This is a good
3907place to be wary of storage leaks.</para>
3908<para>
3909In the sample server, this routine transforms either the bitmap or the
3910rectangle list into a region, so that future routines will have a more
3911predictable starting point to work from. (The validate routine must
3912take this client clip region and merge it with other regions to arrive
3913at a composite clip region before any drawing is done.)</para>
3914<para>
3915<blockquote><programlisting>
3916
3917 void pGC->funcs->DestroyClip(pGC)
3918 GCPtr pGC;
3919
3920</programlisting></blockquote>
3921This routine is called whenever the client clip region must be destroyed.
3922The pGC points to the GC involved. This call should set the clipType
3923field of the GC to CT_NONE.
3924In the sample server, the pointer to the client clip region is set to NULL
3925by this routine after destroying the region, so that other software
3926(including ChangeClip() above) will recognize that there is no client clip region.</para>
3927<para>
3928<blockquote><programlisting>
3929
3930 void pGC->funcs->CopyClip(pgcDst, pgcSrc)
3931 GCPtr pgcDst, pgcSrc;
3932
3933</programlisting></blockquote>
3934This routine makes a copy of the clipMask and clipType from pgcSrc
3935into pgcDst. It is responsible for destroying any previous clipMask
3936in pgcDst. The clip mask in the source can be the same as the
3937clip mask in the dst (clients do the strangest things), so care must
3938be taken when destroying things. This call is required because dix
3939does not know how to copy the clip mask from pgcSrc.</para>
3940</section>
3941</section>
3942<section>
3943 <title>Drawing Primitives</title>
3944<para>
3945The X protocol (rules for the byte stream that goes between client and server)
3946does all graphics using primitive
3947operations, which are called Drawing Primitives.
3948These include line drawing, area filling, arcs, and text drawing.
3949Your implementation must supply 16 routines
3950to perform these on your hardware.
3951(The number 16 is arbitrary.)</para>
3952<para>
3953More specifically, 16 procedure pointers are in each
3954GC op vector.
3955At any given time, ALL of them MUST point to a valid procedure that
3956attempts to do the operation assigned, although
3957the procedure pointers may change and may
3958point to different procedures to carry out the same operation.
3959A simple server will leave them all pointing to the same 16 routines, while
3960a more optimized implementation will switch each from one
3961procedure to another, depending upon what is most optimal
3962for the current GC and drawable.</para>
3963<para>
3964The sample server contains a considerable chunk of code called the
3965mi (machine independent)
3966routines, which serve as drawing primitive routines.
3967Many server implementations will be able to use these as-is,
3968because they work for arbitrary depths.
3969They make no assumptions about the formats of pixmaps
3970and frame buffers, since they call a set of routines
3971known as the "Pixblit Routines" (see next section).
3972They do assume that the way to draw is
3973through these low-level routines that apply pixel values rows at a time.
3974If your hardware or firmware gives more performance when
3975things are done differently, you will want to take this fact into account
3976and rewrite some or all of the drawing primitives to fit your needs.</para>
3977<section>
3978 <title>GC Components</title>
3979<para>
3980This section describes the fields in the GC that affect each drawing primitive.
3981The only primitive that is not affected is GetImage, which does not use a GC
3982because its destination is a protocol-style bit image.
3983Since each drawing primitive mirrors exactly the X protocol request of the
3984same name, you should refer to the X protocol specification document
3985for more details.</para>
3986<para>
3987ALL of these routines MUST CLIP to the
3988appropriate regions in the drawable.
3989Since there are many regions to clip to simultaneously,
3990your ValidateGC routine should combine these into a unified
3991clip region to which your drawing routines can quickly refer.
3992This is exactly what the fb routines supplied with the sample server
3993do.
3994The mi implementation passes responsibility for clipping while drawing
3995down to the Pixblit routines.</para>
3996<para>
3997Also, all of them must adhere to the current plane mask.
3998The plane mask has one bit for every bit plane in the drawable;
3999only planes with 1 bits in the mask are affected by any drawing operation.</para>
4000<para>
4001All functions except for ImageText calls must obey the alu function.
4002This is usually Copy, but could be any of the allowable 16 raster-ops.</para>
4003<para>
4004All of the functions, except for CopyArea, might use the current
4005foreground and background pixel values.
4006Each pixel value is 32 bits.
4007These correspond to foreground and background colors, but you have
4008to run them through the colormap to find out what color the pixel values
4009represent. Do not worry about the color, just apply the pixel value.</para>
4010<para>
4011The routines that draw lines (PolyLine, PolySegment, PolyRect, and PolyArc)
4012use the line width, line style, cap style, and join style.
4013Line width is in pixels.
4014The line style specifies whether it is solid or dashed, and what kind of dash.
4015The cap style specifies whether Rounded, Butt, etc.
4016The join style specifies whether joins between joined lines are Miter, Round or Beveled.
4017When lines cross as part of the same polyline, they are assumed to be drawn once.
4018(See the X protocol specification for more details.)</para>
4019<para>
4020Zero-width lines are NOT meant to be really zero width; this is the client's way
4021of telling you that you can optimize line drawing with little regard to
4022the end caps and joins.
4023They are called "thin" lines and are meant to be one pixel wide.
4024These are frequently done in hardware or in a streamlined assembly language
4025routine.</para>
4026<para>
4027Lines with widths greater than zero, though, must all be drawn with the same
4028algorithm, because client software assumes that every jag on every
4029line at an angle will come at the same place.
4030Two lines that should have
4031one pixel in the space between them
4032(because of their distance apart and their widths) should have such a one-pixel line
4033of space between them if drawn, regardless of angle.</para>
4034<para>
4035The solid area fill routines (FillPolygon, PolyFillRect, PolyFillArc)
4036all use the fill rule, which specifies subtle interpretations of
4037what points are inside and what are outside of a given polygon.
4038The PolyFillArc routine also uses the arc mode, which specifies
4039whether to fill pie segments or single-edge slices of an ellipse.</para>
4040<para>
4041The line drawing, area fill, and PolyText routines must all
4042apply the correct "fill style."
4043This can be either a solid foreground color, a transparent stipple,
4044an opaque stipple, or a tile.
4045Stipples are bitmaps where the 1 bits represent that the foreground color is written,
4046and 0 bits represent that either the pixel is left alone (transparent) or that
4047the background color is written (opaque).
4048A tile is a pixmap of the full depth of the GC that is applied in its full glory to all areas.
4049The stipple and tile patterns can be any rectangular size, although some implementations
4050will be faster for certain sizes such as 8x8 or 32x32.
4051The mi implementation passes this responsibility down to the Pixblit routines.</para>
4052<para>
4053See the X protocol document for full details.
4054The description of the CreateGC request has a very good, detailed description of these
4055attributes.</para>
4056</section>
4057<section>
4058<title>The Primitives</title>
4059<para>
4060The Drawing Primitives are as follows:
4061
4062<blockquote><programlisting>
4063
4064 RegionPtr pGC->ops->CopyArea(src, dst, pGC, srcx, srcy, w, h, dstx, dsty)
4065 DrawablePtr dst, src;
4066 GCPtr pGC;
4067 int srcx, srcy, w, h, dstx, dsty;
4068
4069</programlisting></blockquote>
4070CopyArea copies a rectangle of pixels from one drawable to another of
4071the same depth. To effect scrolling, this must be able to copy from
4072any drawable to itself, overlapped. No squeezing or stretching is done
4073because the source and destination are the same size. However,
4074everything is still clipped to the clip regions of the destination
4075drawable.</para>
4076<para>
4077If pGC->graphicsExposures is True, any portions of the destination which
4078were not valid in the source (either occluded by covering windows, or
4079outside the bounds of the drawable) should be collected together and
4080returned as a region (if this resultant region is empty, NULL can be
4081returned instead). Furthermore, the invalid bits of the source are
4082not copied to the destination and (when the destination is a window)
4083are filled with the background tile. The sample routine
4084miHandleExposures generates the appropriate return value and fills the
4085invalid area using pScreen->PaintWindowBackground.</para>
4086<para>
4087For instance, imagine a window that is partially obscured by other
4088windows in front of it. As text is scrolled on your window, the pixels
4089that are scrolled out from under obscuring windows will not be
4090available on the screen to copy to the right places, and so an exposure
4091event must be sent for the client to correctly repaint them. Of
4092course, if you implement backing store, you could do this without resorting
4093to exposure events.</para>
4094<para>
4095An example implementation is fbCopyArea() in Xserver/fb/fbcopy.c.</para>
4096<para>
4097<blockquote><programlisting>
4098
4099 RegionPtr pGC->ops->CopyPlane(src, dst, pGC, srcx, srcy, w, h, dstx, dsty, plane)
4100 DrawablePtr dst, src;
4101 GCPtr pGC;
4102 int srcx, srcy, w, h, dstx, dsty;
4103 unsigned long plane;
4104
4105</programlisting></blockquote>
4106CopyPlane must copy one plane of a rectangle from the source drawable
4107onto the destination drawable. Because this routine only copies one
4108bit out of each pixel, it can copy between drawables of different
4109depths. This is the only way of copying between drawables of
4110different depths, except for copying bitmaps to pixmaps and applying
4111foreground and background colors to it. All other conditions of
4112CopyArea apply to CopyPlane too.</para>
4113<para>
4114An example implementation is fbCopyPlane() in
4115Xserver/fb/fbcopy.c.</para>
4116<para>
4117<blockquote><programlisting>
4118
4119 void pGC->ops->PolyPoint(dst, pGC, mode, n, pPoint)
4120 DrawablePtr dst;
4121 GCPtr pGC;
4122 int mode;
4123 int n;
4124 DDXPointPtr pPoint;
4125
4126</programlisting></blockquote>
4127PolyPoint draws a set of one-pixel dots (foreground color)
4128at the locations given in the array.
4129mode is one of the defined constants Origin (absolute coordinates) or Previous
4130(each coordinate is relative to the last).
4131Note that this does not use the background color or any tiles or stipples.</para>
4132<para>
4133Example implementations are fbPolyPoint() in Xserver/fb/fbpoint.c and
4134miPolyPoint in Xserver/mi/mipolypnt.c.</para>
4135<para>
4136<blockquote><programlisting>
4137
4138 void pGC->ops->Polylines(dst, pGC, mode, n, pPoint)
4139 DrawablePtr dst;
4140 GCPtr pGC;
4141 int mode;
4142 int n;
4143 DDXPointPtr pPoint;
4144
4145</programlisting></blockquote>
4146Similar to PolyPoint, Polylines draws lines between the locations given in the array.
4147Zero-width lines are NOT meant to be really zero width; this is the client's way of
4148telling you that you can maximally optimize line drawing with little regard to
4149the end caps and joins.
4150mode is one of the defined constants Previous or Origin, depending upon
4151whether the points are each relative to the last or are absolute.</para>
4152<para>
4153Example implementations are miWideLine() and miWideDash() in
4154mi/miwideline.c and miZeroLine() in mi/mizerline.c.</para>
4155<para>
4156<blockquote><programlisting>
4157
4158 void pGC->ops->PolySegment(dst, pGC, n, pPoint)
4159 DrawablePtr dst;
4160 GCPtr pGC;
4161 int n;
4162 xSegment *pSegments;
4163
4164</programlisting></blockquote>
4165PolySegments draws unconnected
4166lines between pairs of points in the array; the array must be of
4167even size; no interconnecting lines are drawn.</para>
4168<para>
4169An example implementation is miPolySegment() in mipolyseg.c.</para>
4170<para>
4171<blockquote><programlisting>
4172
4173 void pGC->ops->PolyRectangle(dst, pGC, n, pRect)
4174 DrawablePtr dst;
4175 GCPtr pGC;
4176 int n;
4177 xRectangle *pRect;
4178
4179</programlisting></blockquote>
4180PolyRectangle draws outlines of rectangles for each rectangle in the array.</para>
4181<para>
4182An example implementation is miPolyRectangle() in Xserver/mi/mipolyrect.c.</para>
4183<para>
4184<blockquote><programlisting>
4185
4186 void pGC->ops->PolyArc(dst, pGC, n, pArc)
4187 DrawablePtr dst;
4188 GCPtr pGC;
4189 int n;
4190 xArc*pArc;
4191
4192</programlisting></blockquote>
4193PolyArc draws connected conic arcs according to the descriptions in the array.
4194See the protocol specification for more details.</para>
4195<para>
4196Example implementations are miZeroPolyArc in Xserver/mi/mizerarc. and
4197miPolyArc() in Xserver/mi/miarc.c.</para>
4198<para>
4199<blockquote><programlisting>
4200
4201 void pGC->ops->FillPolygon(dst, pGC, shape, mode, count, pPoint)
4202 DrawablePtr dst;
4203 GCPtr pGC;
4204 int shape;
4205 int mode;
4206 int count;
4207 DDXPointPtr pPoint;
4208
4209</programlisting></blockquote>
4210FillPolygon fills a polygon specified by the points in the array
4211with the appropriate fill style.
4212If necessary, an extra border line is assumed between the starting and ending lines.
4213The shape can be used as a hint
4214to optimize filling; it indicates whether it is convex (all interior angles
4215less than 180), nonconvex (some interior angles greater than 180 but
4216border does not cross itself), or complex (border crosses itself).
4217You can choose appropriate algorithms or hardware based upon mode.
4218mode is one of the defined constants Previous or Origin, depending upon
4219whether the points are each relative to the last or are absolute.</para>
4220<para>
4221An example implementation is miFillPolygon() in Xserver/mi/mipoly.c.</para>
4222<para>
4223<blockquote><programlisting>
4224
4225 void pGC->ops->PolyFillRect(dst, pGC, n, pRect)
4226 DrawablePtr dst;
4227 GCPtr pGC;
4228 int n;
4229 xRectangle *pRect;
4230
4231</programlisting></blockquote>
4232PolyFillRect fills multiple rectangles.</para>
4233<para>
4234Example implementations are fbPolyFillRect() in Xserver/fb/fbfillrect.c and
4235miPolyFillRect() in Xserver/mi/mifillrct.c.</para>
4236<para>
4237<blockquote><programlisting>
4238
4239 void pGC->ops->PolyFillArc(dst, pGC, n, pArc)
4240 DrawablePtr dst;
4241 GCPtr pGC;
4242 int n;
4243 xArc *pArc;
4244
4245</programlisting></blockquote>
4246PolyFillArc fills a shape for each arc in the
4247list that is bounded by the arc and one or two
4248line segments with the current fill style.</para>
4249<para>
4250An example implementation is miPolyFillArc() in Xserver/mi/mifillarc.c.</para>
4251<para>
4252<blockquote><programlisting>
4253
4254 void pGC->ops->PutImage(dst, pGC, depth, x, y, w, h, leftPad, format, pBinImage)
4255 DrawablePtr dst;
4256 GCPtr pGC;
4257 int x, y, w, h;
4258 int format;
4259 char *pBinImage;
4260
4261</programlisting></blockquote>
4262PutImage copies a pixmap image into the drawable. The pixmap image
4263must be in X protocol format (either Bitmap, XYPixmap, or ZPixmap),
4264and format tells the format. (See the X protocol specification for
4265details on these formats). You must be able to accept all three
4266formats, because the client gets to decide which format to send.
4267Either the drawable and the pixmap image have the same depth, or the
4268source pixmap image must be a Bitmap. If a Bitmap, the foreground and
4269background colors will be applied to the destination.</para>
4270<para>
4271An example implementation is fbPutImage() in Xserver/fb/fbimage.c.</para>
4272<para>
4273<blockquote><programlisting>
4274
4275 void pScreen->GetImage(src, x, y, w, h, format, planeMask, pBinImage)
4276 DrawablePtr src;
4277 int x, y, w, h;
4278 unsigned int format;
4279 unsigned long planeMask;
4280 char *pBinImage;
4281
4282</programlisting></blockquote>
4283GetImage copies the bits from the source drawable into
4284the destination pointer. The bits are written into the buffer
4285according to the server-defined pixmap padding rules.
4286pBinImage is guaranteed to be big enough to hold all
4287the bits that must be written.</para>
4288<para>
4289This routine does not correspond exactly to the X protocol GetImage
4290request, since DIX has to break the reply up into buffers of a size
4291requested by the transport layer. If format is ZPixmap, the bits are
4292written in the ZFormat for the depth of the drawable; if there is a 0
4293bit in the planeMask for a particular plane, all pixels must have the
4294bit in that plane equal to 0. If format is XYPixmap, planemask is
4295guaranteed to have a single bit set; the bits should be written in
4296Bitmap format, which is the format for a single plane of an XYPixmap.</para>
4297<para>
4298An example implementation is miGetImage() in Xserver/mi/mibitblt.c.
4299<blockquote><programlisting>
4300
4301 void pGC->ops->ImageText8(pDraw, pGC, x, y, count, chars)
4302 DrawablePtr pDraw;
4303 GCPtr pGC;
4304 int x, y;
4305 int count;
4306 char *chars;
4307
4308</programlisting></blockquote>
4309ImageText8 draws text. The text is drawn in the foreground color; the
4310background color fills the remainder of the character rectangles. The
4311coordinates specify the baseline and start of the text.</para>
4312<para>
4313An example implementation is miImageText8() in Xserver/mi/mipolytext.c.</para>
4314<para>
4315<blockquote><programlisting>
4316
4317 int pGC->ops->PolyText8(pDraw, pGC, x, y, count, chars)
4318 DrawablePtr pDraw;
4319 GCPtr pGC;
4320 int x, y;
4321 int count;
4322 char *chars;
4323
4324</programlisting></blockquote>
4325PolyText8 works like ImageText8, except it draws with
4326the current fill style for special effects such as
4327shaded text.
4328See the X protocol specification for more details.</para>
4329<para>
4330An example implementation is miPolyText8() in Xserver/mi/mipolytext.c.</para>
4331<para>
4332<blockquote><programlisting>
4333
4334 int pGC->ops->PolyText16(pDraw, pGC, x, y, count, chars)
4335 DrawablePtr pDraw;
4336 GCPtr pGC;
4337 int x, y;
4338 int count;
4339 unsigned short *chars;
4340
4341 void pGC->ops->ImageText16(pDraw, pGC, x, y, count, chars)
4342 DrawablePtr pDraw;
4343 GCPtr pGC;
4344 int x, y;
4345 int count;
4346 unsigned short *chars;
4347
4348</programlisting></blockquote>
4349These two routines are the same as the "8" versions,
4350except that they are for 16-bit character codes (useful
4351for oriental writing systems).</para>
4352<para>
4353The primary difference is in the way the character information is
4354looked up. The 8-bit and the 16-bit versions obviously have different
4355kinds of character values to look up; the main goal of the lookup is
4356to provide a pointer to the CharInfo structs for the characters to
4357draw and to pass these pointers to the Glyph routines. Given a
4358CharInfo struct, lower-level software can draw the glyph desired with
4359little concern for other characteristics of the font.</para>
4360<para>
436116-bit character fonts have a row-and-column scheme, where the 2bytes
4362of the character code constitute the row and column in a square matrix
4363of CharInfo structs. Each font has row and column minimum and maximum
4364values; the CharInfo structures form a two-dimensional matrix.</para>
4365<para>
4366Example implementations are miPolyText16() and
4367miImageText16() in Xserver/mi/mipolytext.c.</para>
4368<para>
4369See the X protocol specification for more details on these graphic operations.</para>
4370<para>
4371There is a hook in the GC ops, called LineHelper, that used to be used in the
4372sample implementation by the code for wide lines. It no longer servers any
4373purpose in the sample servers, but still exists, #ifdef'ed by NEED_LINEHELPER,
4374in case someone needs it.</para>
4375</section>
4376</section>
4377<section>
4378 <title>Pixblit Procedures</title>
4379<para>
4380The Drawing Primitive functions must be defined for your server.
4381One possible way to do this is to use the mi routines from the sample server.
4382If you choose to use the mi routines (even part of them!) you must implement
4383these Pixblit routines.
4384These routines read and write pixel values
4385and deal directly with the image data.</para>
4386<para>
4387The Pixblit routines for the sample server are part of the "fb"
4388routines. As with the mi routines, the fb routines are
4389portable but are not as portable as the mi routines.</para>
4390<para>
4391The fb subsystem is a depth-independent framebuffer core, capable of
4392operating at any depth from 1 to 32, based on the depth of the window
4393or pixmap it is currently operating on. In particular, this means it
4394can support pixmaps of multiple depths on the same screen. It supplies
4395both Pixblit routines and higher-level optimized implementations of the
4396Drawing Primitive routines. It does make the assumption that the pixel
4397data it touches is available in the server's address space.</para>
4398<para>
4399In other words, if you have a "normal" frame buffer type display, you
4400can probably use the fb code, and the mi code. If you
4401have a stranger hardware, you will have to supply your own Pixblit
4402routines, but you can use the mi routines on top of them. If you have
4403better ways of doing some of the Drawing Primitive functions, then you
4404may want to supply some of your own Drawing Primitive routines. (Even
4405people who write their own Drawing Primitives save at least some of
4406the mi code for certain special cases that their hardware or library
4407or fancy algorithm does not handle.)</para>
4408<para>
4409The client, DIX, and the machine-independent routines do not carry the
4410final responsibility of clipping. They all depend upon the Pixblit
4411routines to do their clipping for them. The rule is, if you touch the
4412frame buffer, you clip.</para>
4413<para>
4414(The higher level routines may decide to clip at a high level, but
4415this is only for increased performance and cannot substitute for
4416bottom-level clipping. For instance, the mi routines, DIX, or the
4417client may decide to check all character strings to be drawn and chop
4418off all characters that would not be displayed. If so, it must retain
4419the character on the edge that is partly displayed so that the Pixblit
4420routines can clip off precisely at the right place.)</para>
4421<para>
4422To make this easier, all of the reasons to clip can be combined into
4423one region in your ValidateGC procedure. You take this composite clip
4424region with you into the Pixblit routines. (The sample server does
4425this.)</para>
4426<para>
4427Also, FillSpans() has to apply tile and stipple patterns. The
4428patterns are all aligned to the window origin so that when two people
4429write patches that are contiguous, they will merge nicely. (Really,
4430they are aligned to the patOrg point in the GC. This defaults to (0,
44310) but can be set by the client to anything.)</para>
4432<para>
4433However, the mi routines can translate (relocate) the points from
4434window-relative to screen-relative if desired. If you set the
4435miTranslate field in the GC (set it in the CreateGC or ValidateGC
4436routine), then the mi output routines will translate all coordinates.
4437If it is false, then the coordinates will be passed window-relative.
4438Screens with no hardware translation will probably set miTranslate to
4439TRUE, so that geometry (e.g. polygons, rectangles) can be translated,
4440rather than having the resulting list of scanlines translated; this is
4441good because the list vertices in a drawing request will generally be
4442much smaller than the list of scanlines it produces. Similarly,
4443hardware that does translation can set miTranslate to FALSE, and avoid
4444the extra addition per vertex, which can be (but is not always)
4445important for getting the highest possible performance. (Contrast the
4446behavior of GetSpans, which is not expected to be called as often, and
4447so has different constraints.) The miTranslate field is settable in
4448each GC, if , for example, you are mixing several kinds of
4449destinations (offscreen pixmaps, main memory pixmaps, backing store,
4450and windows), all of which have different requirements, on one screen.</para>
4451<para>
4452As with other drawing routines, there are fields in the GC to direct
4453higher code to the correct routine to execute for each function. In
4454this way, you can optimize for special cases, for example, drawing
4455solids versus drawing stipples.</para>
4456<para>
4457The Pixblit routines are broken up into three sets. The Span routines
4458simply fill in rows of pixels. The Glyph routines fill in character
4459glyphs. The PushPixels routine is a three-input bitblt for more
4460sophisticated image creation.</para>
4461<para>
4462It turns out that the Glyph and PushPixels routines actually have a
4463machine-independent implementation that depends upon the Span
4464routines. If you are really pressed for time, you can use these
4465versions, although they are quite slow.</para>
4466<section>
4467<title>Span Routines</title>
4468<para>
4469For these routines, all graphic operations have been reduced to "spans."
4470A span is a horizontal row of pixels.
4471If you can design these routines which write into and read from
4472rows of pixels at a time, you can use the mi routines.</para>
4473<para>
4474Each routine takes
4475a destination drawable to draw into, a GC to use while drawing,
4476the number of spans to do, and two pointers to arrays that indicate the list
4477of starting points and the list of widths of spans.</para>
4478<para>
4479<blockquote><programlisting>
4480
4481 void pGC->ops->FillSpans(dst, pGC, nSpans, pPoints, pWidths, sorted)
4482 DrawablePtr dst;
4483 GCPtr pGC;
4484 int nSpans;
4485 DDXPointPtr pPoints;
4486 int *pWidths;
4487 int sorted;
4488
4489</programlisting></blockquote>
4490FillSpans should fill horizontal rows of pixels with
4491the appropriate patterns, stipples, etc.,
4492based on the values in the GC.
4493The starting points are in the array at pPoints; the widths are in pWidths.
4494If sorted is true, the scan lines are in increasing y order, in which case
4495you may be able to make assumptions and optimizations.</para>
4496<para>
4497GC components: alu, clipOrg, clientClip, and fillStyle.</para>
4498<para>
4499GC mode-dependent components: fgPixel (for fillStyle Solid); tile, patOrg
4500(for fillStyle Tile); stipple, patOrg, fgPixel (for fillStyle Stipple);
4501and stipple, patOrg, fgPixel and bgPixel (for fillStyle OpaqueStipple).</para>
4502<para>
4503<blockquote><programlisting>
4504
4505 void pGC->ops->SetSpans(pDrawable, pGC, pSrc, ppt, pWidths, nSpans, sorted)
4506 DrawablePtr pDrawable;
4507 GCPtr pGC;
4508 char *pSrc;
4509 DDXPointPtr pPoints;
4510 int *pWidths;
4511 int nSpans;
4512 int sorted;
4513
4514</programlisting></blockquote>
4515For each span, this routine should copy pWidths bits from pSrc to
4516pDrawable at pPoints using the raster-op from the GC.
4517If sorted is true, the scan lines are in increasing y order.
4518The pixels in pSrc are
4519padded according to the screen's padding rules.
4520These
4521can be used to support
4522interesting extension libraries, for example, shaded primitives. It does not
4523use the tile and stipple.</para>
4524<para>
4525GC components: alu, clipOrg, and clientClip</para>
4526<para>
4527The above functions are expected to handle all modifiers in the current
4528GC. Therefore, it is expedient to have
4529different routines to quickly handle common special cases
4530and reload the procedure pointers
4531at validate time, as with the other output functions.</para>
4532<para>
4533<blockquote><programlisting>
4534
4535 void pScreen->GetSpans(pDrawable, wMax, pPoints, pWidths, nSpans)
4536 DrawablePtr pDrawable;
4537 int wMax;
4538 DDXPointPtr pPoints;
4539 int *pWidths;
4540 int nSpans;
4541 char *pDst;
4542
4543</programlisting></blockquote>
4544For each span, GetSpans gets bits from the drawable starting at pPoints
4545and continuing for pWidths bits.
4546Each scanline returned will be server-scanline padded.
4547The routine can return NULL if memory cannot be allocated to hold the
4548result.</para>
4549<para>
4550GetSpans never translates -- for a window, the coordinates are already
4551screen-relative. Consider the case of hardware that doesn't do
4552translation: the mi code that calls ddX will translate each shape
4553(rectangle, polygon,. etc.) before scan-converting it, which requires
4554many fewer additions that having GetSpans translate each span does.
4555Conversely, consider hardware that does translate: it can set its
4556translation point to (0, 0) and get each span, and the only penalty is
4557the small number of additions required to translate each shape being
4558scan-converted by the calling code. Contrast the behavior of
4559FillSpans and SetSpans (discussed above under miTranslate), which are
4560expected to be used more often.</para>
4561<para>
4562Thus, the penalty to hardware that does hardware translation is
4563negligible, and code that wants to call GetSpans() is greatly
4564simplified, both for extensions and the machine-independent core
4565implementation.</para>
4566<section>
4567 <title>Glyph Routines</title>
4568<para>
4569The Glyph routines draw individual character glyphs for text drawing requests.</para>
4570<para>
4571You have a choice in implementing these routines. You can use the mi
4572versions; they depend ultimately upon the span routines. Although
4573text drawing will work, it will be very slow.</para>
4574<para>
4575<blockquote><programlisting>
4576
4577 void pGC->ops->PolyGlyphBlt(pDrawable, pGC, x, y, nglyph, ppci, pglyphBase)
4578 DrawablePtr pDrawable;
4579 GCPtr pGC;
4580 int x , y;
4581 unsigned int nglyph;
4582 CharInfoRec **ppci; /* array of character info */
4583 pointer unused; /* unused since R5 */
4584
4585</programlisting></blockquote>
4586GC components: alu, clipOrg, clientClip, font, and fillStyle.</para>
4587<para>
4588GC mode-dependent components: fgPixel (for fillStyle Solid); tile, patOrg
4589(for fillStyle Tile); stipple, patOrg, fgPixel (for fillStyle Stipple);
4590and stipple, patOrg, fgPixel and bgPixel (for fillStyle OpaqueStipple).</para>
4591<para>
4592<blockquote><programlisting>
4593
4594 void pGC->ops->ImageGlyphBlt(pDrawable, pGC, x, y, nglyph, ppci, pglyphBase)
4595 DrawablePtr pDrawable;
4596 GCPtr pGC;
4597 int x , y;
4598 unsigned int nglyph;
4599 CharInfoRec **ppci; /* array of character info */
4600 pointer unused; /* unused since R5 */
4601
4602</programlisting></blockquote>
4603GC components: clipOrg, clientClip, font, fgPixel, bgPixel</para>
4604<para>
4605These routines must copy the glyphs defined by the bitmaps in
4606pglyphBase and the font metrics in ppci to the DrawablePtr, pDrawable.
4607The poly routine follows all fill, stipple, and tile rules. The image
4608routine simply blasts the glyph onto the glyph's rectangle, in
4609foreground and background colors.</para>
4610<para>
4611More precisely, the Image routine fills the character rectangle with
4612the background color, and then the glyph is applied in the foreground
4613color. The glyph can extend outside of the character rectangle.
4614ImageGlyph() is used for terminal emulators and informal text purposes
4615such as button labels.</para>
4616<para>
4617The exact specification for the Poly routine is that the glyph is
4618painted with the current fill style. The character rectangle is
4619irrelevant for this operation. PolyText, at a higher level, includes
4620facilities for font changes within strings and such; it is to be used
4621for WYSIWYG word processing and similar systems.</para>
4622<para>
4623Both of these routines must clip themselves to the overall clipping region.</para>
4624<para>
4625Example implementations in mi are miPolyGlyphBlt() and
4626miImageGlyphBlt() in Xserver/mi/miglblt.c.</para>
4627</section>
4628<section>
4629<title>PushPixels routine</title>
4630<para>
4631The PushPixels routine writes the current fill style onto the drawable
4632in a certain shape defined by a bitmap. PushPixels is equivalent to
4633using a second stipple. You can thing of it as pushing the fillStyle
4634through a stencil. PushPixels is not used by any of the mi rendering code,
4635but is used by the mi software cursor code.
4636<blockquote><para>
4637 Suppose the stencil is: 00111100
4638 and the stipple is: 10101010
4639 PushPixels result: 00101000
4640</para></blockquote>
4641</para>
4642<para>
4643You have a choice in implementing this routine.
4644You can use the mi version which depends ultimately upon FillSpans().
4645Although it will work, it will be slow.</para>
4646<para>
4647<blockquote><programlisting>
4648
4649 void pGC->ops->PushPixels(pGC, pBitMap, pDrawable, dx, dy, xOrg, yOrg)
4650 GCPtr pGC;
4651 PixmapPtr pBitMap;
4652 DrawablePtr pDrawable;
4653 int dx, dy, xOrg, yOrg;
4654
4655</programlisting></blockquote>
4656GC components: alu, clipOrg, clientClip, and fillStyle.</para>
4657<para>
4658GC mode-dependent components: fgPixel (for fillStyle Solid); tile, patOrg
4659(for fillStyle Tile); stipple, patOrg, fgPixel (for fillStyle Stipple);
4660and stipple, patOrg, fgPixel and bgPixel (for fillStyle OpaqueStipple).</para>
4661<para>
4662PushPixels applys the foreground color, tile, or stipple from the pGC
4663through a stencil onto pDrawable. pBitMap points to a stencil (of
4664which we use an area dx wide by dy high), which is oriented over the
4665drawable at xOrg, yOrg. Where there is a 1 bit in the bitmap, the
4666destination is set according to the current fill style. Where there
4667is a 0 bit in the bitmap, the destination is left the way it is.</para>
4668<para>
4669This routine must clip to the overall clipping region.</para>
4670<para>
4671An Example implementation is miPushPixels() in Xserver/mi/mipushpxl.c.</para>
4672</section>
4673</section>
4674</section>
4675<section>
4676 <title>Shutdown Procedures</title>
4677<para>
4678<blockquote><programlisting>
4679 void AbortDDX(enum ExitCode error)
4680 void ddxGiveUp(enum ExitCode error)
4681</programlisting></blockquote>
4682Some hardware may require special work to be done before the server
4683exits so that it is not left in an intermediate state. As explained
4684in the OS layer, FatalError() will call AbortDDX() just before
4685terminating the server. In addition, ddxGiveUp() will be called just
4686before terminating the server on a "clean" death. What AbortDDX() and
4687ddxGiveUP do is left unspecified, only that stubs must exist in the
4688ddx layer. It is up to local implementors as to what they should
4689accomplish before termination.</para>
4690<section>
4691 <title>Command Line Procedures</title>
4692<para>
4693<blockquote><programlisting>
4694 int ddxProcessArgument(argc, argv, i)
4695 int argc;
4696 char *argv[];
4697 int i;
4698
4699 void
4700 ddxUseMsg()
4701
4702</programlisting></blockquote>
4703You should write these routines to deal with device-dependent command line
4704arguments. The routine ddxProcessArgument() is called with the command line,
4705and the current index into argv; you should return zero if the argument
4706is not a device-dependent one, and otherwise return a count of the number
4707of elements of argv that are part of this one argument. For a typical
4708option (e.g., "-realtime"), you should return the value one. This
4709routine gets called before checks are made against device-independent
4710arguments, so it is possible to peek at all arguments or to override
4711device-independent argument processing. You can document the
4712device-dependent arguments in ddxUseMsg(), which will be
4713called from UseMsg() after printing out the device-independent arguments.</para>
4714</section>
4715</section>
4716<section id="wrappers_and_privates">
4717 <title>Wrappers and Privates</title>
4718<para>
4719Two new extensibility concepts have been developed for release 4, Wrappers
4720and devPrivates. These replace the R3 GCInterest queues, which were not a
4721general enough mechanism for many extensions and only provided hooks into a
4722single data structure. devPrivates have been revised substantially for
4723X.Org X server release 1.5, updated again for the 1.9 release and extended
4724again for the 1.13 relealse.</para>
4725<section>
4726 <title>devPrivates</title>
4727<para>
4728devPrivates provides a way to attach arbitrary private data to various server structures.
4729Any structure which contains a <structfield>devPrivates</structfield> field of
4730type <type>PrivateRec</type> supports this mechanism. Some structures allow
4731allocating space for private data after some objects have been created, others
4732require all space allocations be registered before any objects of that type
4733are created. <filename class="headerfile">Xserver/include/privates.h</filename>
4734lists which of these cases applies to each structure containing
4735<structfield>devPrivates</structfield>.</para>
4736
4737<para>
4738To request private space, use
4739<blockquote><programlisting>
4740 Bool dixRegisterPrivateKey(DevPrivateKey key, DevPrivateType type, unsigned size);
4741</programlisting></blockquote>
4742The first argument is a pointer to a <type>DevPrivateKeyRec</type> which
4743will serve as the unique identifier for the private data. Typically this is
4744the address of a static <type>DevPrivateKeyRec</type> in your code.
4745The second argument is the class of objects for which this key will apply.
4746The third argument is the size of the space being requested, or
4747<constant>0</constant> to only allocate a pointer that the caller will manage.
4748If space is requested, this space will be automatically freed when the object
4749is destroyed. Note that a call to <function>dixSetPrivate</function>
4750that changes the pointer value may cause the space to be unreachable by the caller, however it will still be automatically freed.
4751The function returns <literal>TRUE</literal> unless memory allocation fails.
4752If the function is called more than once on the same key, all calls must use
4753the same value for <type>size</type> or the server will abort.</para>
4754
4755<para>
4756To request per-screen private space in an object, use
4757<blockquote><programlisting>
4758 Bool dixRegisterScreenPrivateKey(DevScreenPrivateKey key, ScreenPtr pScreen, DevPrivateType type, unsigned size);
4759</programlisting></blockquote>
4760The <parameter>type</parameter> and <parameter>size</parameter> arguments are
4761the same as those to <function>dixRegisterPrivateKey</function> but this
4762function ensures the given <parameter>key</parameter> exists on objects of
4763the specified type with distinct storage for the given
4764<parameter>pScreen</parameter>. The key is usable on ScreenPrivate variants
4765that are otherwise equivalent to the following Private functions.</para>
4766
4767<para>
4768 To request private space in objects created for a specific screen, use
4769 <blockquote><programlisting>
4770 Bool dixRegisterScreenSpecificPrivateKey(ScreenPtr pScreen, DevPrivateKey key, DevPrivateType type, unsigned size);
4771 </programlisting></blockquote>
4772 The <parameter>type</parameter> and <parameter>size</parameter> arguments are
4773 the same as those to <function>dixRegisterPrivateKey</function> but this
4774 function ensures only that the given <parameter>key</parameter> exists on objects of
4775 the specified type that are allocated with reference to the specified
4776 <parameter>pScreen</parameter>. Using the key on objects allocated for
4777 other screens will result in incorrect results; there is no check made to
4778 ensure that the caller's screen matches the private's screen. The key is
4779 usable in any of the following functions. Screen-specific private storage is available
4780 only for Windows, GCs, Pixmaps and Pictures. Attempts to allocate screen-specific
4781 privates on other objects will result in a call to FatalError.
4782</para>
4783
4784<para>
4785To attach a piece of private data to an object, use:
4786<blockquote><programlisting>
4787 void dixSetPrivate(PrivateRec **privates, const DevPrivateKey key, pointer val)
4788</programlisting></blockquote>
4789The first argument is the address of the <structfield>devPrivates</structfield>
4790field in the target structure. This field is managed privately by the DIX
4791layer and should not be directly modified. The second argument is a pointer
4792to the <type>DevPrivateKeyRec</type> which you registered with
4793<function>dixRegisterPrivateKey</function> or allocated with
4794<function>dixCreatePrivateKey</function>. Only one
4795piece of data with a given key can be attached to an object, and in most cases
4796each key is specific to the type of object it was registered for. (An
4797exception is the PRIVATE_XSELINUX class which applies to multiple object types.)
4798The third argument is the value to store.</para>
4799<para>
4800If private data with the given key is already associated with the object,
4801<function>dixSetPrivate</function> will overwrite the old value with the
4802new one.</para>
4803
4804<para>
4805To look up a piece of private data, use one of:
4806<blockquote><programlisting>
4807 pointer dixLookupPrivate(PrivateRec **privates, const DevPrivateKey key)
4808 pointer *dixLookupPrivateAddr(PrivateRec **privates, const DevPrivateKey key)
4809</programlisting></blockquote>
4810The first argument is the address of the <structfield>devPrivates</structfield> field
4811in the target structure. The second argument is the key to look up.
4812If a non-zero size was given when the key was registered, or if private data
4813with the given key is already associated with the object, then
4814<function>dixLookupPrivate</function> will return the pointer value
4815while <function>dixLookupPrivateAddr</function>
4816will return the address of the pointer.</para>
4817
4818<para>
4819When implementing new server resource objects that support devPrivates, there
4820are four steps to perform:
4821Add a type value to the <type>DevPrivateType</type> enum in
4822<filename class="headerfile">Xserver/include/privates.h</filename>,
4823declare a field of type <type>PrivateRec *</type> in your structure;
4824initialize this field to <literal>NULL</literal> when creating any objects; and
4825when freeing any objects call the <function>dixFreePrivates</function> or
4826<function>dixFreeObjectWithPrivates</function> function.</para>
4827</section>
4828<section>
4829 <title>Wrappers</title>
4830<para>
4831Wrappers are not a body of code, nor an interface spec. They are, instead,
4832a technique for hooking a new module into an existing calling sequence.
4833There are limitations on other portions of the server implementation which
4834make using wrappers possible; limits on when specific fields of data
4835structures may be modified. They are intended as a replacement for
4836GCInterest queues, which were not general enough to support existing
4837modules; in particular software cursors needed more
4838control over the activity. The general mechanism for using wrappers is:
4839<blockquote><programlisting>
4840privateWrapperFunction (object, ...)
4841 ObjectPtr object;
4842{
4843 pre-wrapped-function-stuff ...
4844
4845 object->functionVector = dixLookupPrivate(&amp;object->devPrivates, privateKey);
4846 (*object->functionVector) (object, ...);
4847 /*
4848 * this next line is occasionally required by the rules governing
4849 * wrapper functions. Always using it will not cause problems.
4850 * Not using it when necessary can cause severe troubles.
4851 */
4852 dixSetPrivate(&amp;object->devPrivates, privateKey, object->functionVector);
4853 object->functionVector = privateWrapperFunction;
4854
4855 post-wrapped-function-stuff ...
4856}
4857
4858privateInitialize (object)
4859 ObjectPtr object;
4860{
4861 dixSetPrivate(&amp;object->devPrivates, privateKey, object->functionVector);
4862 object->functionVector = privateWrapperFunction;
4863}
4864</programlisting></blockquote>
4865</para>
4866<para>
4867Thus the privateWrapperFunction provides hooks for performing work both
4868before and after the wrapped function has been called; the process of
4869resetting the functionVector is called "unwrapping" while the process of
4870fetching the wrapped function and replacing it with the wrapping function
4871is called "wrapping". It should be clear that GCInterest queues could
4872be emulated using wrappers. In general, any function vectors contained in
4873objects can be wrapped, but only vectors in GCs and Screens have been tested.</para>
4874<para>
4875Wrapping screen functions is quite easy; each vector is individually
4876wrapped. Screen functions are not supposed to change after initialization,
4877so rewrapping is technically not necessary, but causes no problems.</para>
4878<para>
4879Wrapping GC functions is a bit more complicated. GC's have two tables of
4880function vectors, one hanging from gc->ops and the other from gc->funcs, which
4881should be initially wrapped from a CreateGC wrapper. Wrappers should modify
4882only table pointers, not the contents of the tables, as they
4883may be shared by more than one GC (and, in the case of funcs, are probably
4884shared by all gcs). Your func wrappers may change the GC funcs or ops
4885pointers, and op wrappers may change the GC op pointers but not the funcs.</para>
4886<para>
4887Thus, the rule for GC wrappings is: wrap the funcs from CreateGC and, in each
4888func wrapper, unwrap the ops and funcs, call down, and re-wrap. In each op
4889wrapper, unwrap the ops, call down, and rewrap afterwards. Note that in
4890re-wrapping you must save out the pointer you're replacing again. This way the
4891chain will be maintained when wrappers adjust the funcs/ops tables they use.</para>
4892</section>
4893</section>
4894<section>
4895 <title>Work Queue</title>
4896<para>
4897To queue work for execution when all clients are in a stable state (i.e.
4898just before calling select() in WaitForSomething), call:
4899<blockquote><programlisting>
4900 Bool QueueWorkProc(function,client,closure)
4901 Bool (*function)();
4902 ClientPtr client;
4903 pointer closure;
4904</programlisting></blockquote>
4905</para>
4906<para>
4907When the server is about to suspend itself, the given function will be
4908executed:
4909<blockquote><programlisting>
4910 (*function) (client, closure)
4911</programlisting></blockquote>
4912</para>
4913<para>
4914Neither client nor closure are actually used inside the work queue routines.</para>
4915</section>
4916</section>
4917<section>
4918 <title>Summary of Routines</title>
4919<para>
4920This is a summary of the routines discussed in this document.
4921The procedure names are in alphabetical order.
4922The Struct is the structure it is attached to; if blank, this
4923procedure is not attached to a struct and must be named as shown.
4924The sample server provides implementations in the following
4925categories. Notice that many of the graphics routines have both
4926mi and fb implementations.</para>
4927<para>
4928<itemizedlist>
4929<listitem><para>dix portable to all systems; do not attempt to rewrite (Xserver/dix)</para></listitem>
4930<listitem><para>os routine provided in Xserver/os or Xserver/include/os.h</para></listitem>
4931<listitem><para>ddx frame buffer dependent (examples in Xserver/fb)</para></listitem>
4932<listitem><para>mi routine provided in Xserver/mi</para></listitem>
4933<listitem><para>hd hardware dependent (examples in many Xserver/hw directories)</para></listitem>
4934<listitem><para>none not implemented in sample implementation</para></listitem>
4935</itemizedlist>
4936</para>
4937 <table frame="all" id="routines-1">
4938 <title>Server Routines (Page 1)</title>
4939 <tgroup cols='3' align='left' colsep='1' rowsep='1'>
4940 <thead>
4941 <row>
4942 <entry>Procedure</entry>
4943 <entry>Port</entry>
4944 <entry>Struct</entry>
4945 </row>
4946 </thead>
4947 <tbody>
4948<row><entry><function>ALLOCATE_LOCAL</function></entry><entry><literal>os</literal></entry><entry><para></para></entry></row>
4949<row><entry><function>AbortDDX</function></entry><entry><literal>hd</literal></entry><entry><para></para></entry></row>
4950<row><entry><function>AddCallback</function></entry><entry><literal>dix</literal></entry><entry><para></para></entry></row>
4951<row><entry><function>AddEnabledDevice</function></entry><entry><literal>os</literal></entry><entry><para></para></entry></row>
4952<row><entry><function>AddInputDevice</function></entry><entry><literal>dix</literal></entry><entry><para></para></entry></row>
4953<row><entry><function>AddScreen</function></entry><entry><literal>dix</literal></entry><entry><para></para></entry></row>
4954<row><entry><function>AdjustWaitForDelay</function></entry><entry><literal>os</literal></entry><entry><para></para></entry></row>
4955<row><entry><function>Bell</function></entry><entry><literal>hd</literal></entry><entry><para>Device</para></entry></row>
4956<row><entry><function>ChangeClip</function></entry><entry><literal>mi</literal></entry><entry><para>GC func</para></entry></row>
4957<row><entry><function>ChangeGC</function></entry><entry><literal></literal></entry><entry><para>GC func</para></entry></row>
4958<row><entry><function>ChangeWindowAttributes</function></entry><entry><literal>ddx</literal></entry><entry><para>Screen</para></entry></row>
4959<row><entry><function>ClearToBackground</function></entry><entry><literal>ddx</literal></entry><entry><para>Window</para></entry></row>
4960<row><entry><function>ClientAuthorized</function></entry><entry><literal>os</literal></entry><entry><para></para></entry></row>
4961<row><entry><function>ClientSignal</function></entry><entry><literal>dix</literal></entry><entry><para></para></entry></row>
4962<row><entry><function>ClientSleep</function></entry><entry><literal>dix</literal></entry><entry><para></para></entry></row>
4963<row><entry><function>ClientWakeup</function></entry><entry><literal>dix</literal></entry><entry><para></para></entry></row>
4964<row><entry><function>ClipNotify</function></entry><entry><literal>ddx</literal></entry><entry><para>Screen</para></entry></row>
4965<row><entry><function>CloseScreen</function></entry><entry><literal>hd</literal></entry><entry><para></para></entry></row>
4966<row><entry><function>ConstrainCursor</function></entry><entry><literal>hd</literal></entry><entry><para>Screen</para></entry></row>
4967<row><entry><function>CopyArea</function></entry><entry><literal>mi</literal></entry><entry><para>GC op</para></entry></row>
4968<row><entry><function>CopyGCDest</function></entry><entry><literal>ddx</literal></entry><entry><para>GC func</para></entry></row>
4969<row><entry><function>CopyGCSource</function></entry><entry><literal>none</literal></entry><entry><para>GC func</para></entry></row>
4970<row><entry><function>CopyPlane</function></entry><entry><literal>mi</literal></entry><entry><para>GC op</para></entry></row>
4971<row><entry><function>CopyWindow</function></entry><entry><literal>ddx</literal></entry><entry><para>Window</para></entry></row>
4972<row><entry><function>CreateGC</function></entry><entry><literal>ddx</literal></entry><entry><para>Screen</para></entry></row>
4973<row><entry><function>CreateCallbackList</function></entry><entry><literal>dix</literal></entry><entry><para></para></entry></row>
4974<row><entry><function>CreatePixmap</function></entry><entry><literal>ddx</literal></entry><entry><para>Screen</para></entry></row>
4975<row><entry><function>CreateScreenResources</function></entry><entry><literal>ddx</literal></entry><entry><para>Screen</para></entry></row>
4976<row><entry><function>CreateWellKnowSockets</function></entry><entry><literal>os</literal></entry><entry><para></para></entry></row>
4977<row><entry><function>CreateWindow</function></entry><entry><literal>ddx</literal></entry><entry><para>Screen</para></entry></row>
4978<row><entry><function>CursorLimits</function></entry><entry><literal>hd</literal></entry><entry><para>Screen</para></entry></row>
4979<row><entry><function>DEALLOCATE_LOCAL</function></entry><entry><literal>os</literal></entry><entry><para></para></entry></row>
4980<row><entry><function>DeleteCallback</function></entry><entry><literal>dix</literal></entry><entry><para></para></entry></row>
4981<row><entry><function>DeleteCallbackList</function></entry><entry><literal>dix</literal></entry><entry><para></para></entry></row>
4982<row><entry><function>DestroyClip</function></entry><entry><literal>ddx</literal></entry><entry><para>GC func</para></entry></row>
4983<row><entry><function>DestroyGC</function></entry><entry><literal>ddx</literal></entry><entry><para>GC func</para></entry></row>
4984<row><entry><function>DestroyPixmap</function></entry><entry><literal>ddx</literal></entry><entry><para>Screen</para></entry></row>
4985<row><entry><function>DestroyWindow</function></entry><entry><literal>ddx</literal></entry><entry><para>Screen</para></entry></row>
4986<row><entry><function>DisplayCursor</function></entry><entry><literal>hd</literal></entry><entry><para>Screen</para></entry></row>
4987<row><entry><function>Error</function></entry><entry><literal>os</literal></entry><entry><para></para></entry></row>
4988<row><entry><function>ErrorF</function></entry><entry><literal>os</literal></entry><entry><para></para></entry></row>
4989<row><entry><function>FatalError</function></entry><entry><literal>os</literal></entry><entry><para></para></entry></row>
4990<row><entry><function>FillPolygon</function></entry><entry><literal>mi</literal></entry><entry><para>GC op</para></entry></row>
4991<row><entry><function>FillSpans</function></entry><entry><literal>ddx</literal></entry><entry><para>GC op</para></entry></row>
4992<row><entry><function>FlushAllOutput</function></entry><entry><literal>os</literal></entry><entry><para></para></entry></row>
4993<row><entry><function>FlushIfCriticalOutputPending</function></entry><entry><literal>os</literal></entry><entry><para></para></entry></row>
4994<row><entry><function>FreeScratchPixmapHeader</function></entry><entry><literal>dix</literal></entry><entry><para></para></entry></row>
4995<row><entry><function>GetImage</function></entry><entry><literal>mi</literal></entry><entry><para>Screen</para></entry></row>
4996<row><entry><function>GetMotionEvents</function></entry><entry><literal>hd</literal></entry><entry><para>Device</para></entry></row>
4997<row><entry><function>GetScratchPixmapHeader</function></entry><entry><literal>dix</literal></entry><entry><para></para></entry></row>
4998<row><entry><function>GetSpans</function></entry><entry><literal>ddx</literal></entry><entry><para>Screen</para></entry></row>
4999<row><entry><function>GetStaticColormap</function></entry><entry><literal>ddx</literal></entry><entry><para>Screen</para></entry></row>
5000 </tbody>
5001 </tgroup>
5002 </table>
5003
5004 <table frame="all" id="routines-2">
5005 <title>Server Routines (Page 2)</title>
5006 <tgroup cols='3' align='left' colsep='1' rowsep='1'>
5007 <thead>
5008 <row>
5009 <entry>Procedure</entry>
5010 <entry>Port</entry>
5011 <entry>Struct</entry>
5012 </row>
5013 </thead>
5014 <tbody>
5015<row><entry><function>ImageGlyphBlt</function></entry><entry><literal>mi</literal></entry><entry><para>GC op</para></entry></row>
5016<row><entry><function>ImageText16</function></entry><entry><literal>mi</literal></entry><entry><para>GC op</para></entry></row>
5017<row><entry><function>ImageText8</function></entry><entry><literal>mi</literal></entry><entry><para>GC op</para></entry></row>
5018<row><entry><function>InitInput</function></entry><entry><literal>hd</literal></entry><entry><para></para></entry></row>
5019<row><entry><function>InitKeyboardDeviceStruct</function></entry><entry><literal>dix</literal></entry><entry><para></para></entry></row>
5020<row><entry><function>InitOutput</function></entry><entry><literal>hd</literal></entry><entry><para></para></entry></row>
5021<row><entry><function>InitPointerDeviceStruct</function></entry><entry><literal>dix</literal></entry><entry><para></para></entry></row>
5022<row><entry><function>InsertFakeRequest</function></entry><entry><literal>os</literal></entry><entry><para></para></entry></row>
5023<row><entry><function>InstallColormap</function></entry><entry><literal>ddx</literal></entry><entry><para>Screen</para></entry></row>
5024<row><entry><function>Intersect</function></entry><entry><literal>mi</literal></entry><entry><para>Screen</para></entry></row>
5025<row><entry><function>Inverse</function></entry><entry><literal>mi</literal></entry><entry><para>Screen</para></entry></row>
5026<row><entry><function>LegalModifier</function></entry><entry><literal>hd</literal></entry><entry><para></para></entry></row>
5027<row><entry><function>LineHelper</function></entry><entry><literal>mi</literal></entry><entry><para>GC op</para></entry></row>
5028<row><entry><function>ListInstalledColormaps</function></entry><entry><literal>ddx</literal></entry><entry><para>Screen</para></entry></row>
5029<row><entry><function>LookupKeyboardDevice</function></entry><entry><literal>dix</literal></entry><entry><para></para></entry></row>
5030<row><entry><function>LookupPointerDevice</function></entry><entry><literal>dix</literal></entry><entry><para></para></entry></row>
5031<row><entry><function>ModifyPixmapHeader</function></entry><entry><literal>mi</literal></entry><entry><para>Screen</para></entry></row>
5032<row><entry><function>NextAvailableClient</function></entry><entry><literal>dix</literal></entry><entry><para></para></entry></row>
5033<row><entry><function>OsInit</function></entry><entry><literal>os</literal></entry><entry><para></para></entry></row>
5034<row><entry><function>PaintWindowBackground</function></entry><entry><literal>mi</literal></entry><entry><para>Window</para></entry></row>
5035<row><entry><function>PaintWindowBorder</function></entry><entry><literal>mi</literal></entry><entry><para>Window</para></entry></row>
5036<row><entry><function>PointerNonInterestBox</function></entry><entry><literal>hd</literal></entry><entry><para>Screen</para></entry></row>
5037<row><entry><function>PointInRegion</function></entry><entry><literal>mi</literal></entry><entry><para>Screen</para></entry></row>
5038<row><entry><function>PolyArc</function></entry><entry><literal>mi</literal></entry><entry><para>GC op</para></entry></row>
5039<row><entry><function>PolyFillArc</function></entry><entry><literal>mi</literal></entry><entry><para>GC op</para></entry></row>
5040<row><entry><function>PolyFillRect</function></entry><entry><literal>mi</literal></entry><entry><para>GC op</para></entry></row>
5041<row><entry><function>PolyGlyphBlt</function></entry><entry><literal>mi</literal></entry><entry><para>GC op</para></entry></row>
5042<row><entry><function>Polylines</function></entry><entry><literal>mi</literal></entry><entry><para>GC op</para></entry></row>
5043<row><entry><function>PolyPoint</function></entry><entry><literal>mi</literal></entry><entry><para>GC op</para></entry></row>
5044<row><entry><function>PolyRectangle</function></entry><entry><literal>mi</literal></entry><entry><para>GC op</para></entry></row>
5045<row><entry><function>PolySegment</function></entry><entry><literal>mi</literal></entry><entry><para>GC op</para></entry></row>
5046<row><entry><function>PolyText16</function></entry><entry><literal>mi</literal></entry><entry><para>GC op</para></entry></row>
5047<row><entry><function>PolyText8</function></entry><entry><literal>mi</literal></entry><entry><para>GC op</para></entry></row>
5048<row><entry><function>PositionWindow</function></entry><entry><literal>ddx</literal></entry><entry><para>Screen</para></entry></row>
5049<row><entry><function>ProcessInputEvents</function></entry><entry><literal>hd</literal></entry><entry><para></para></entry></row>
5050<row><entry><function>PushPixels</function></entry><entry><literal>mi</literal></entry><entry><para>GC op</para></entry></row>
5051<row><entry><function>PutImage</function></entry><entry><literal>mi</literal></entry><entry><para>GC op</para></entry></row>
5052<row><entry><function>QueryBestSize</function></entry><entry><literal>hd</literal></entry><entry><para>Screen</para></entry></row>
5053<row><entry><function>ReadRequestFromClient</function></entry><entry><literal>os</literal></entry><entry><para></para></entry></row>
5054<row><entry><function>RealizeCursor</function></entry><entry><literal>hd</literal></entry><entry><para>Screen</para></entry></row>
5055<row><entry><function>RealizeFont</function></entry><entry><literal>ddx</literal></entry><entry><para>Screen</para></entry></row>
5056<row><entry><function>RealizeWindow</function></entry><entry><literal>ddx</literal></entry><entry><para>Screen</para></entry></row>
5057<row><entry><function>RecolorCursor</function></entry><entry><literal>hd</literal></entry><entry><para>Screen</para></entry></row>
5058<row><entry><function>RectIn</function></entry><entry><literal>mi</literal></entry><entry><para>Screen</para></entry></row>
5059<row><entry><function>RegionCopy</function></entry><entry><literal>mi</literal></entry><entry><para>Screen</para></entry></row>
5060<row><entry><function>RegionCreate</function></entry><entry><literal>mi</literal></entry><entry><para>Screen</para></entry></row>
5061<row><entry><function>RegionDestroy</function></entry><entry><literal>mi</literal></entry><entry><para>Screen</para></entry></row>
5062<row><entry><function>RegionEmpty</function></entry><entry><literal>mi</literal></entry><entry><para>Screen</para></entry></row>
5063<row><entry><function>RegionExtents</function></entry><entry><literal>mi</literal></entry><entry><para>Screen</para></entry></row>
5064<row><entry><function>RegionNotEmpty</function></entry><entry><literal>mi</literal></entry><entry><para>Screen</para></entry></row>
5065<row><entry><function>RegionReset</function></entry><entry><literal>mi</literal></entry><entry><para>Screen</para></entry></row>
5066<row><entry><function>ResolveColor</function></entry><entry><literal>ddx</literal></entry><entry><para>Screen</para></entry></row>
5067 </tbody>
5068 </tgroup>
5069 </table>
5070
5071 <table frame="all" id="routines-3">
5072 <title>Server Routines (Page 3)</title>
5073 <tgroup cols='3' align='left' colsep='1' rowsep='1'>
5074 <thead>
5075 <row>
5076 <entry>Procedure</entry>
5077 <entry>Port</entry>
5078 <entry>Struct</entry>
5079 </row>
5080 </thead>
5081 <tbody>
5082<row><entry><function>RemoveEnabledDevice</function></entry><entry><literal>os</literal></entry><entry><para></para></entry></row>
5083<row><entry><function>ResetCurrentRequest</function></entry><entry><literal>os</literal></entry><entry><para></para></entry></row>
5084<row><entry><function>SaveScreen</function></entry><entry><literal>ddx</literal></entry><entry><para>Screen</para></entry></row>
5085<row><entry><function>SetCriticalOutputPending</function></entry><entry><literal>os</literal></entry><entry><para></para></entry></row>
5086<row><entry><function>SetCursorPosition</function></entry><entry><literal>hd</literal></entry><entry><para>Screen</para></entry></row>
5087<row><entry><function>SetInputCheck</function></entry><entry><literal>dix</literal></entry><entry><para></para></entry></row>
5088<row><entry><function>SetSpans</function></entry><entry><literal>ddx</literal></entry><entry><para>GC op</para></entry></row>
5089<row><entry><function>StoreColors</function></entry><entry><literal>ddx</literal></entry><entry><para>Screen</para></entry></row>
5090<row><entry><function>Subtract</function></entry><entry><literal>mi</literal></entry><entry><para>Screen</para></entry></row>
5091<row><entry><function>TimerCancel</function></entry><entry><literal>os</literal></entry><entry><para></para></entry></row>
5092<row><entry><function>TimerCheck</function></entry><entry><literal>os</literal></entry><entry><para></para></entry></row>
5093<row><entry><function>TimerForce</function></entry><entry><literal>os</literal></entry><entry><para></para></entry></row>
5094<row><entry><function>TimerFree</function></entry><entry><literal>os</literal></entry><entry><para></para></entry></row>
5095<row><entry><function>TimerInit</function></entry><entry><literal>os</literal></entry><entry><para></para></entry></row>
5096<row><entry><function>TimerSet</function></entry><entry><literal>os</literal></entry><entry><para></para></entry></row>
5097<row><entry><function>TimeSinceLastInputEvent</function></entry><entry><literal>hd</literal></entry><entry><para></para></entry></row>
5098<row><entry><function>TranslateRegion</function></entry><entry><literal>mi</literal></entry><entry><para>Screen</para></entry></row>
5099<row><entry><function>UninstallColormap</function></entry><entry><literal>ddx</literal></entry><entry><para>Screen</para></entry></row>
5100<row><entry><function>Union</function></entry><entry><literal>mi</literal></entry><entry><para>Screen</para></entry></row>
5101<row><entry><function>UnrealizeCursor</function></entry><entry><literal>hd</literal></entry><entry><para>Screen</para></entry></row>
5102<row><entry><function>UnrealizeFont</function></entry><entry><literal>ddx</literal></entry><entry><para>Screen</para></entry></row>
5103<row><entry><function>UnrealizeWindow</function></entry><entry><literal>ddx</literal></entry><entry><para>Screen</para></entry></row>
5104<row><entry><function>ValidateGC</function></entry><entry><literal>ddx</literal></entry><entry><para>GC func</para></entry></row>
5105<row><entry><function>ValidateTree</function></entry><entry><literal>mi</literal></entry><entry><para>Screen</para></entry></row>
5106<row><entry><function>WaitForSomething</function></entry><entry><literal>os</literal></entry><entry><para></para></entry></row>
5107<row><entry><function>WindowExposures</function></entry><entry><literal>mi</literal></entry><entry><para>Window</para></entry></row>
5108<row><entry><function>WriteToClient</function></entry><entry><literal>os</literal></entry><entry><para></para></entry></row>
5109<row><entry><function>Xalloc</function></entry><entry><literal>os</literal></entry><entry><para></para></entry></row>
5110<row><entry><function>Xfree</function></entry><entry><literal>os</literal></entry><entry><para></para></entry></row>
5111<row><entry><function>Xrealloc</function></entry><entry><literal>os</literal></entry><entry><para></para></entry></row>
5112 </tbody>
5113 </tgroup>
5114 </table>
5115</section>
5116</article>