ch03.xml revision eb411b4b
1eb411b4bSmrg<chapter id='Data_Structures'>
2e9fcaa8aSmrg<title>Data Structures</title>
3e9fcaa8aSmrg
4e9fcaa8aSmrg<para>
5e9fcaa8aSmrgAn Xkb keyboard description consists of a variety of data structures, each of
6e9fcaa8aSmrgwhich describes some aspect of the keyboard. Although each data structure has
7e9fcaa8aSmrgits own peculiarities, there are a number of features common to nearly all Xkb
8e9fcaa8aSmrgstructures. This chapter describes these common features and techniques for
9e9fcaa8aSmrgmanipulating them.
10e9fcaa8aSmrg</para>
11e9fcaa8aSmrg
12e9fcaa8aSmrg
13e9fcaa8aSmrg<para>
14e9fcaa8aSmrgMany Xkb data structures are interdependent; changing a field in one might
15e9fcaa8aSmrgrequire changes to others. As an additional complication, some Xkb library
16e9fcaa8aSmrgfunctions allocate related components as a group to reduce fragmentation and
17e9fcaa8aSmrgallocator overhead. In these cases, simply allocating and freeing fields of Xkb
18e9fcaa8aSmrgstructures might corrupt program memory. Creating and destroying such
19e9fcaa8aSmrgstructures or keeping them properly synchronized during editing is complicated
20e9fcaa8aSmrgand error prone.
21e9fcaa8aSmrg</para>
22e9fcaa8aSmrg
23e9fcaa8aSmrg
24e9fcaa8aSmrg<para>
25e9fcaa8aSmrgXkb provides functions and macros to allocate and free all major data
26e9fcaa8aSmrgstructures. You should use them instead of allocating and freeing the
27e9fcaa8aSmrgstructures yourself.
28e9fcaa8aSmrg</para>
29e9fcaa8aSmrg
30eb411b4bSmrg<sect1 id='Allocating_Xkb_Data_Structures'>
31e9fcaa8aSmrg<title>Allocating Xkb Data Structures</title>
32e9fcaa8aSmrg
33e9fcaa8aSmrg<para>
34e9fcaa8aSmrgXkb provides functions, known as allocators, to create and initialize Xkb data
35e9fcaa8aSmrgstructures. In most situations, the Xkb functions that read a keyboard
36e9fcaa8aSmrgdescription from the server call these allocators automatically. As a result,
37e9fcaa8aSmrgyou will seldom have to directly allocate or initialize Xkb data structures.
38e9fcaa8aSmrg</para>
39e9fcaa8aSmrg
40e9fcaa8aSmrg
41e9fcaa8aSmrg<para>
42e9fcaa8aSmrgHowever, if you need to enlarge an existing structure or construct a keyboard
43e9fcaa8aSmrgdefinition from scratch, you may need to allocate and initialize Xkb data
44e9fcaa8aSmrgstructures directly. Each major Xkb data structure has its own unique
45e9fcaa8aSmrgallocator. The allocator functions share common features: allocator functions
46e9fcaa8aSmrgfor structures with optional components take as an input argument a mask of
47e9fcaa8aSmrgsubcomponents to be allocated. Allocators for data structures containing
48e9fcaa8aSmrgvariable-length data take an argument specifying the initial length of the data.
49e9fcaa8aSmrg</para>
50e9fcaa8aSmrg
51e9fcaa8aSmrg
52e9fcaa8aSmrg<para>
53e9fcaa8aSmrgYou may call an allocator to change the size of the space allocated for
54e9fcaa8aSmrgvariable-length data. When you call an allocator with an existing data
55e9fcaa8aSmrgstructure as a parameter, the allocator does not change the data in any of the
56e9fcaa8aSmrgfields, with one exception: variable-length data might be moved. The allocator
57e9fcaa8aSmrgresizes the allocated memory if the current size is too small. This normally
58e9fcaa8aSmrginvolves allocating new memory, copying existing data to the newly allocated
59e9fcaa8aSmrgmemory, and freeing the original memory. This possible reallocation is
60e9fcaa8aSmrgimportant to note because local variables pointing into Xkb data structures
61e9fcaa8aSmrgmight be invalidated by calls to allocator functions.
62e9fcaa8aSmrg</para>
63e9fcaa8aSmrg
64e9fcaa8aSmrg</sect1>
65eb411b4bSmrg<sect1 id='Adding_Data_and_Editing_Data_Structures'>
66e9fcaa8aSmrg<title>Adding Data and Editing Data Structures</title>
67e9fcaa8aSmrg
68e9fcaa8aSmrg<para>
69e9fcaa8aSmrgYou should edit most data structures via the Xkb-supplied helper functions and
70e9fcaa8aSmrgmacros, although a few data structures can be edited directly. The helper
71e9fcaa8aSmrgfunctions and macros make sure everything is initialized and interdependent
72e9fcaa8aSmrgvalues are properly updated for those Xkb structures that have
73e9fcaa8aSmrginterdependencies. As a general rule, if there is a helper function or macro to
74e9fcaa8aSmrgedit the data structure, use it. For example, increasing the width of a type
75e9fcaa8aSmrgrequires you to resize every key that uses that type. This is complicated and
76e9fcaa8aSmrgugly, which is why there’s an <emphasis>
77e9fcaa8aSmrgXkbResizeKeyType</emphasis>
78e9fcaa8aSmrg function.
79e9fcaa8aSmrg</para>
80e9fcaa8aSmrg
81e9fcaa8aSmrg
82e9fcaa8aSmrg<para>
83e9fcaa8aSmrgMany Xkb data structures have arrays whose size is reported by two fields. The
84e9fcaa8aSmrgfirst field, whose name is usually prefixed by <emphasis>
85e9fcaa8aSmrgsz_</emphasis>
86e9fcaa8aSmrg, represents the total number of elements that can be stored in the array. The
87e9fcaa8aSmrgsecond field, whose name is usually prefixed by <emphasis>
88e9fcaa8aSmrgnum_</emphasis>
89e9fcaa8aSmrg, specifies the number of elements currently stored there. These arrays
90e9fcaa8aSmrgtypically represent data whose total size cannot always be determined when the
91e9fcaa8aSmrgarray is created. In these instances, the usual way to allocate space and add
92e9fcaa8aSmrgdata is as follows:
93e9fcaa8aSmrg</para>
94e9fcaa8aSmrg
95e9fcaa8aSmrg<itemizedlist>
96e9fcaa8aSmrg  <listitem>
97e9fcaa8aSmrg    <para>
98e9fcaa8aSmrgCall the allocator function with some arbitrary size, as a hint.
99e9fcaa8aSmrg    </para>
100e9fcaa8aSmrg  </listitem>
101e9fcaa8aSmrg  <listitem>
102e9fcaa8aSmrg    <para>
103e9fcaa8aSmrgFor those arrays that have an <emphasis>
104e9fcaa8aSmrgXkb...Add...</emphasis>
105e9fcaa8aSmrg function, call it each time you want to add new data to the array. The
106e9fcaa8aSmrgfunction expands the array if necessary.
107e9fcaa8aSmrg    </para>
108e9fcaa8aSmrg  </listitem>
109e9fcaa8aSmrg</itemizedlist>
110e9fcaa8aSmrg
111e9fcaa8aSmrg<para>
112e9fcaa8aSmrgFor example, call:
113e9fcaa8aSmrg</para>
114e9fcaa8aSmrg
115e9fcaa8aSmrg<para>
116e9fcaa8aSmrgXkbAllocGeomShapes(geom,4)
117e9fcaa8aSmrg</para>
118e9fcaa8aSmrg
119e9fcaa8aSmrg<para>
120e9fcaa8aSmrgto say "I’ll need space for four new shapes in this geometry." This makes
121e9fcaa8aSmrgsure that <emphasis>
122e9fcaa8aSmrgsz_shapes</emphasis>
123e9fcaa8aSmrg - <emphasis>
124e9fcaa8aSmrgnum_shapes</emphasis>
125e9fcaa8aSmrg &gt;= 4, and resizes the shapes array if it isn’t. If this function
126e9fcaa8aSmrgsucceeds, you are guaranteed to have space for the number of shapes you need.
127e9fcaa8aSmrg</para>
128e9fcaa8aSmrg
129e9fcaa8aSmrg
130e9fcaa8aSmrg<para>
131e9fcaa8aSmrgWhen you call an editing function for a structure, you do not need to check for
132e9fcaa8aSmrgspace, because the function automatically checks the <emphasis>
133e9fcaa8aSmrgsz_</emphasis>
134e9fcaa8aSmrg and <emphasis>
135e9fcaa8aSmrgnum_</emphasis>
136e9fcaa8aSmrg fields of the array, resizes the array if necessary, adds the entry to the
137e9fcaa8aSmrgarray, and then updates the <emphasis>
138e9fcaa8aSmrgnum_</emphasis>
139e9fcaa8aSmrg field.
140e9fcaa8aSmrg</para>
141e9fcaa8aSmrg
142e9fcaa8aSmrg
143e9fcaa8aSmrg</sect1>
144eb411b4bSmrg<sect1 id='Making_Changes_to_the_Servers_Keyboard_Description'>
145e9fcaa8aSmrg<title>Making Changes to the Server’s Keyboard Description</title>
146e9fcaa8aSmrg
147e9fcaa8aSmrg<para>
148e9fcaa8aSmrgIn Xkb, as in the core protocol, the client and server have independent copies
149e9fcaa8aSmrgof the data structures that describe the keyboard. The recommended way to
150e9fcaa8aSmrgchange some aspect of the keyboard mapping in the X server is to edit a local
151e9fcaa8aSmrgcopy of the Xkb keyboard description and then send only the changes to the X
152e9fcaa8aSmrgserver. This method helps eliminate the need to transfer the entire keyboard
153e9fcaa8aSmrgdescription or even an entire data structure for only minor changes.
154e9fcaa8aSmrg</para>
155e9fcaa8aSmrg
156e9fcaa8aSmrg
157e9fcaa8aSmrg<para>
158e9fcaa8aSmrgTo help you keep track of the changes you make to a local copy of the keyboard
159e9fcaa8aSmrgdescription, Xkb provides separate special <emphasis>
160e9fcaa8aSmrgchanges</emphasis>
161e9fcaa8aSmrg data structures for each major Xkb data structure. These data structures do
162e9fcaa8aSmrgnot contain the actual changed values: they only indicate the changes that have
163e9fcaa8aSmrgbeen made to the structures that actually describe the keyboard.
164e9fcaa8aSmrg</para>
165e9fcaa8aSmrg
166e9fcaa8aSmrg
167e9fcaa8aSmrg<para>
168e9fcaa8aSmrgWhen you wish to change the keyboard description in the server, you first
169e9fcaa8aSmrgmodify a local copy of the keyboard description and then flag the modifications
170e9fcaa8aSmrgin an appropriate changes data structure. When you finish editing the local
171e9fcaa8aSmrgcopy of the keyboard description, you pass your modified version of the
172e9fcaa8aSmrgkeyboard description and the modified changes data structure to an Xkb
173e9fcaa8aSmrgfunction. This function uses the modified keyboard description and changes
174e9fcaa8aSmrgstructure to pass only the changed information to the server. Note that
175e9fcaa8aSmrgmodifying the keyboard description but not setting the appropriate flags in the
176e9fcaa8aSmrgchanges data structure causes indeterminate behavior.
177e9fcaa8aSmrg</para>
178e9fcaa8aSmrg
179e9fcaa8aSmrg
180e9fcaa8aSmrg</sect1>
181eb411b4bSmrg<sect1 id='Tracking_Keyboard_Changes_in_the_Server'>
182e9fcaa8aSmrg<title>Tracking Keyboard Changes in the Server</title>
183e9fcaa8aSmrg
184e9fcaa8aSmrg<para>
185e9fcaa8aSmrgThe server reports all changes in its keyboard description to any interested
186e9fcaa8aSmrgclients via special Xkb events. Just as clients use special changes data
187e9fcaa8aSmrgstructures to change the keyboard description in the server, the server uses
188e9fcaa8aSmrgspecial changes data structures to tell a client what changed in the server’s
189e9fcaa8aSmrgkeyboard description.
190e9fcaa8aSmrg</para>
191e9fcaa8aSmrg
192e9fcaa8aSmrg
193e9fcaa8aSmrg<para>
194e9fcaa8aSmrgUnlike clients, however, the server does not always pass the new values when it
195e9fcaa8aSmrgreports changes to its copy of the keyboard description. Instead, the server
196e9fcaa8aSmrgonly passes a changes data structure when it reports changes to its keyboard
197e9fcaa8aSmrgdescription. This is done for efficiency reasons — some clients do not always
198e9fcaa8aSmrgneed to update their copy of the keyboard description with every report from
199e9fcaa8aSmrgthe server.
200e9fcaa8aSmrg</para>
201e9fcaa8aSmrg
202e9fcaa8aSmrg
203e9fcaa8aSmrg<para>
204e9fcaa8aSmrgWhen your client application receives a report from the server indicating the
205e9fcaa8aSmrgkeyboard description has changed, you can determine the set of changes by
206e9fcaa8aSmrgpassing the event to an Xkb function that "notes" event information in the
207e9fcaa8aSmrgcorresponding changes data structure. These "note changes" functions are
208e9fcaa8aSmrgdefined for all major Xkb components, and their names have the form <emphasis>
209e9fcaa8aSmrgXkbNote{Component}Changes</emphasis>
210e9fcaa8aSmrg, where <emphasis>
211e9fcaa8aSmrgComponent</emphasis>
212e9fcaa8aSmrg is the name of a major Xkb component such as <emphasis>
213e9fcaa8aSmrgMap</emphasis>
214e9fcaa8aSmrg or <emphasis>
215e9fcaa8aSmrgNames</emphasis>
216e9fcaa8aSmrg. When you want to copy these changes from the server into a local copy of the
217e9fcaa8aSmrgkeyboard description, use the corresponding <emphasis>
218e9fcaa8aSmrgXkbGet{Component}Changes</emphasis>
219e9fcaa8aSmrg function<emphasis>
220e9fcaa8aSmrg, </emphasis>
221e9fcaa8aSmrgpassing it the changes structure. The function then retrieves only the changed
222e9fcaa8aSmrgstructures from the server and copies the modified pieces into the local
223e9fcaa8aSmrgkeyboard description.
224e9fcaa8aSmrg</para>
225e9fcaa8aSmrg
226e9fcaa8aSmrg</sect1>
227eb411b4bSmrg<sect1 id='Freeing_Data_Structures'>
228e9fcaa8aSmrg<title>Freeing Data Structures</title>
229e9fcaa8aSmrg
230e9fcaa8aSmrg<para>
231e9fcaa8aSmrgFor the same reasons you should not directly use <emphasis>
232e9fcaa8aSmrgmalloc</emphasis>
233e9fcaa8aSmrg to allocate Xkb data structures, you should not free Xkb data structures or
234e9fcaa8aSmrgcomponents directly using <emphasis>
235e9fcaa8aSmrgfree</emphasis>
236e9fcaa8aSmrg or <emphasis>
237e9fcaa8aSmrgXfree</emphasis>
238e9fcaa8aSmrg. Xkb provides functions to free the various data structures and their
239e9fcaa8aSmrgcomponents. Always use the free functions supplied by Xkb. There is no
240e9fcaa8aSmrgguarantee that any particular field can be safely freed by <emphasis>
241e9fcaa8aSmrgfree</emphasis>
242e9fcaa8aSmrg or <emphasis>
243e9fcaa8aSmrgXfree</emphasis>
244e9fcaa8aSmrg.
245e9fcaa8aSmrg</para>
246e9fcaa8aSmrg</sect1>
247e9fcaa8aSmrg</chapter>
248