10f8248bfSmrg<?xml version="1.0" encoding="UTF-8" ?> 20f8248bfSmrg<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.3//EN" 30f8248bfSmrg "http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd"> 4eb411b4bSmrg<chapter id='Data_Structures'> 5e9fcaa8aSmrg<title>Data Structures</title> 6e9fcaa8aSmrg 7e9fcaa8aSmrg<para> 8e9fcaa8aSmrgAn Xkb keyboard description consists of a variety of data structures, each of 9e9fcaa8aSmrgwhich describes some aspect of the keyboard. Although each data structure has 10e9fcaa8aSmrgits own peculiarities, there are a number of features common to nearly all Xkb 11e9fcaa8aSmrgstructures. This chapter describes these common features and techniques for 12e9fcaa8aSmrgmanipulating them. 13e9fcaa8aSmrg</para> 14e9fcaa8aSmrg 15e9fcaa8aSmrg 16e9fcaa8aSmrg<para> 17e9fcaa8aSmrgMany Xkb data structures are interdependent; changing a field in one might 18e9fcaa8aSmrgrequire changes to others. As an additional complication, some Xkb library 19e9fcaa8aSmrgfunctions allocate related components as a group to reduce fragmentation and 20e9fcaa8aSmrgallocator overhead. In these cases, simply allocating and freeing fields of Xkb 21e9fcaa8aSmrgstructures might corrupt program memory. Creating and destroying such 22e9fcaa8aSmrgstructures or keeping them properly synchronized during editing is complicated 23e9fcaa8aSmrgand error prone. 24e9fcaa8aSmrg</para> 25e9fcaa8aSmrg 26e9fcaa8aSmrg 27e9fcaa8aSmrg<para> 28e9fcaa8aSmrgXkb provides functions and macros to allocate and free all major data 29e9fcaa8aSmrgstructures. You should use them instead of allocating and freeing the 30e9fcaa8aSmrgstructures yourself. 31e9fcaa8aSmrg</para> 32e9fcaa8aSmrg 33eb411b4bSmrg<sect1 id='Allocating_Xkb_Data_Structures'> 34e9fcaa8aSmrg<title>Allocating Xkb Data Structures</title> 35e9fcaa8aSmrg 36e9fcaa8aSmrg<para> 37e9fcaa8aSmrgXkb provides functions, known as allocators, to create and initialize Xkb data 38e9fcaa8aSmrgstructures. In most situations, the Xkb functions that read a keyboard 39e9fcaa8aSmrgdescription from the server call these allocators automatically. As a result, 40e9fcaa8aSmrgyou will seldom have to directly allocate or initialize Xkb data structures. 41e9fcaa8aSmrg</para> 42e9fcaa8aSmrg 43e9fcaa8aSmrg 44e9fcaa8aSmrg<para> 45e9fcaa8aSmrgHowever, if you need to enlarge an existing structure or construct a keyboard 46e9fcaa8aSmrgdefinition from scratch, you may need to allocate and initialize Xkb data 47e9fcaa8aSmrgstructures directly. Each major Xkb data structure has its own unique 48e9fcaa8aSmrgallocator. The allocator functions share common features: allocator functions 49e9fcaa8aSmrgfor structures with optional components take as an input argument a mask of 50e9fcaa8aSmrgsubcomponents to be allocated. Allocators for data structures containing 51e9fcaa8aSmrgvariable-length data take an argument specifying the initial length of the data. 52e9fcaa8aSmrg</para> 53e9fcaa8aSmrg 54e9fcaa8aSmrg 55e9fcaa8aSmrg<para> 56e9fcaa8aSmrgYou may call an allocator to change the size of the space allocated for 57e9fcaa8aSmrgvariable-length data. When you call an allocator with an existing data 58e9fcaa8aSmrgstructure as a parameter, the allocator does not change the data in any of the 59e9fcaa8aSmrgfields, with one exception: variable-length data might be moved. The allocator 60e9fcaa8aSmrgresizes the allocated memory if the current size is too small. This normally 61e9fcaa8aSmrginvolves allocating new memory, copying existing data to the newly allocated 62e9fcaa8aSmrgmemory, and freeing the original memory. This possible reallocation is 63e9fcaa8aSmrgimportant to note because local variables pointing into Xkb data structures 64e9fcaa8aSmrgmight be invalidated by calls to allocator functions. 65e9fcaa8aSmrg</para> 66e9fcaa8aSmrg 67e9fcaa8aSmrg</sect1> 68eb411b4bSmrg<sect1 id='Adding_Data_and_Editing_Data_Structures'> 69e9fcaa8aSmrg<title>Adding Data and Editing Data Structures</title> 70e9fcaa8aSmrg 71e9fcaa8aSmrg<para> 72e9fcaa8aSmrgYou should edit most data structures via the Xkb-supplied helper functions and 73e9fcaa8aSmrgmacros, although a few data structures can be edited directly. The helper 74e9fcaa8aSmrgfunctions and macros make sure everything is initialized and interdependent 75e9fcaa8aSmrgvalues are properly updated for those Xkb structures that have 76e9fcaa8aSmrginterdependencies. As a general rule, if there is a helper function or macro to 77e9fcaa8aSmrgedit the data structure, use it. For example, increasing the width of a type 78e9fcaa8aSmrgrequires you to resize every key that uses that type. This is complicated and 790f8248bfSmrgugly, which is why there’s an 800f8248bfSmrg<function>XkbResizeKeyType</function> 810f8248bfSmrgfunction. 82e9fcaa8aSmrg</para> 83e9fcaa8aSmrg 84e9fcaa8aSmrg 85e9fcaa8aSmrg<para> 86e9fcaa8aSmrgMany Xkb data structures have arrays whose size is reported by two fields. The 870f8248bfSmrgfirst field, whose name is usually prefixed by 880f8248bfSmrg<emphasis>sz_</emphasis>, 890f8248bfSmrgrepresents the total number of elements that can be stored in the array. The 900f8248bfSmrgsecond field, whose name is usually prefixed by 910f8248bfSmrg<emphasis>num_</emphasis>, 920f8248bfSmrgspecifies the number of elements currently stored there. These arrays 93e9fcaa8aSmrgtypically represent data whose total size cannot always be determined when the 94e9fcaa8aSmrgarray is created. In these instances, the usual way to allocate space and add 95e9fcaa8aSmrgdata is as follows: 96e9fcaa8aSmrg</para> 97e9fcaa8aSmrg 98e9fcaa8aSmrg<itemizedlist> 99e9fcaa8aSmrg <listitem> 100e9fcaa8aSmrg <para> 101e9fcaa8aSmrgCall the allocator function with some arbitrary size, as a hint. 102e9fcaa8aSmrg </para> 103e9fcaa8aSmrg </listitem> 104e9fcaa8aSmrg <listitem> 105e9fcaa8aSmrg <para> 1060f8248bfSmrgFor those arrays that have an 1070f8248bfSmrg<function>Xkb...Add...</function> 1080f8248bfSmrgfunction, call it each time you want to add new data to the array. The 109e9fcaa8aSmrgfunction expands the array if necessary. 110e9fcaa8aSmrg </para> 111e9fcaa8aSmrg </listitem> 112e9fcaa8aSmrg</itemizedlist> 113e9fcaa8aSmrg 114e9fcaa8aSmrg<para> 115e9fcaa8aSmrgFor example, call: 116e9fcaa8aSmrg 1170f8248bfSmrg<programlisting> 118e9fcaa8aSmrgXkbAllocGeomShapes(geom,4) 1190f8248bfSmrg</programlisting> 1200f8248bfSmrg 1210f8248bfSmrgto say <quote>I’ll need space for four new shapes in this geometry.</quote> 1220f8248bfSmrgThis makes sure that 1230f8248bfSmrg<structfield>sz_shapes</structfield> 1240f8248bfSmrg− 1250f8248bfSmrg<structfield>num_shapes</structfield> 1260f8248bfSmrg>= 4, and resizes the shapes array if it isn’t. If this function 127e9fcaa8aSmrgsucceeds, you are guaranteed to have space for the number of shapes you need. 128e9fcaa8aSmrg</para> 129e9fcaa8aSmrg 130e9fcaa8aSmrg 131e9fcaa8aSmrg<para> 132e9fcaa8aSmrgWhen you call an editing function for a structure, you do not need to check for 1330f8248bfSmrgspace, because the function automatically checks the 1340f8248bfSmrg<emphasis>sz_</emphasis> 1350f8248bfSmrgand 1360f8248bfSmrg<emphasis>num_</emphasis> 1370f8248bfSmrgfields of the array, resizes the array if necessary, adds the entry to the 1380f8248bfSmrgarray, and then updates the 1390f8248bfSmrg<emphasis>num_</emphasis> 1400f8248bfSmrgfield. 141e9fcaa8aSmrg</para> 142e9fcaa8aSmrg 143e9fcaa8aSmrg 144e9fcaa8aSmrg</sect1> 145eb411b4bSmrg<sect1 id='Making_Changes_to_the_Servers_Keyboard_Description'> 146e9fcaa8aSmrg<title>Making Changes to the Server’s Keyboard Description</title> 147e9fcaa8aSmrg 148e9fcaa8aSmrg<para> 149e9fcaa8aSmrgIn Xkb, as in the core protocol, the client and server have independent copies 150e9fcaa8aSmrgof the data structures that describe the keyboard. The recommended way to 151e9fcaa8aSmrgchange some aspect of the keyboard mapping in the X server is to edit a local 152e9fcaa8aSmrgcopy of the Xkb keyboard description and then send only the changes to the X 153e9fcaa8aSmrgserver. This method helps eliminate the need to transfer the entire keyboard 154e9fcaa8aSmrgdescription or even an entire data structure for only minor changes. 155e9fcaa8aSmrg</para> 156e9fcaa8aSmrg 157e9fcaa8aSmrg 158e9fcaa8aSmrg<para> 159e9fcaa8aSmrgTo help you keep track of the changes you make to a local copy of the keyboard 1600f8248bfSmrgdescription, Xkb provides separate special 1610f8248bfSmrg<firstterm>changes</firstterm> 1620f8248bfSmrg<indexterm significance="preferred" zone="Making_Changes_to_the_Servers_Keyboard_Description"> 1630f8248bfSmrg<primary>changes data structures</primary></indexterm> 1640f8248bfSmrgdata structures for each major Xkb data structure. These data structures do 165e9fcaa8aSmrgnot contain the actual changed values: they only indicate the changes that have 166e9fcaa8aSmrgbeen made to the structures that actually describe the keyboard. 167e9fcaa8aSmrg</para> 168e9fcaa8aSmrg 169e9fcaa8aSmrg 170e9fcaa8aSmrg<para> 171e9fcaa8aSmrgWhen you wish to change the keyboard description in the server, you first 172e9fcaa8aSmrgmodify a local copy of the keyboard description and then flag the modifications 173e9fcaa8aSmrgin an appropriate changes data structure. When you finish editing the local 174e9fcaa8aSmrgcopy of the keyboard description, you pass your modified version of the 175e9fcaa8aSmrgkeyboard description and the modified changes data structure to an Xkb 176e9fcaa8aSmrgfunction. This function uses the modified keyboard description and changes 177e9fcaa8aSmrgstructure to pass only the changed information to the server. Note that 178e9fcaa8aSmrgmodifying the keyboard description but not setting the appropriate flags in the 179e9fcaa8aSmrgchanges data structure causes indeterminate behavior. 180e9fcaa8aSmrg</para> 181e9fcaa8aSmrg 182e9fcaa8aSmrg 183e9fcaa8aSmrg</sect1> 184eb411b4bSmrg<sect1 id='Tracking_Keyboard_Changes_in_the_Server'> 185e9fcaa8aSmrg<title>Tracking Keyboard Changes in the Server</title> 186e9fcaa8aSmrg 187e9fcaa8aSmrg<para> 188e9fcaa8aSmrgThe server reports all changes in its keyboard description to any interested 189e9fcaa8aSmrgclients via special Xkb events. Just as clients use special changes data 190e9fcaa8aSmrgstructures to change the keyboard description in the server, the server uses 191e9fcaa8aSmrgspecial changes data structures to tell a client what changed in the server’s 192e9fcaa8aSmrgkeyboard description. 193e9fcaa8aSmrg</para> 194e9fcaa8aSmrg 195e9fcaa8aSmrg 196e9fcaa8aSmrg<para> 197e9fcaa8aSmrgUnlike clients, however, the server does not always pass the new values when it 198e9fcaa8aSmrgreports changes to its copy of the keyboard description. Instead, the server 199e9fcaa8aSmrgonly passes a changes data structure when it reports changes to its keyboard 200e9fcaa8aSmrgdescription. This is done for efficiency reasons — some clients do not always 201e9fcaa8aSmrgneed to update their copy of the keyboard description with every report from 202e9fcaa8aSmrgthe server. 203e9fcaa8aSmrg</para> 204e9fcaa8aSmrg 205e9fcaa8aSmrg 206e9fcaa8aSmrg<para> 207e9fcaa8aSmrgWhen your client application receives a report from the server indicating the 208e9fcaa8aSmrgkeyboard description has changed, you can determine the set of changes by 2090f8248bfSmrgpassing the event to an Xkb function that <quote>notes</quote> event 2100f8248bfSmrginformation in the corresponding changes data structure. These 2110f8248bfSmrg<quote>note changes</quote> functions are 2120f8248bfSmrgdefined for all major Xkb components, and their names have the form 2130f8248bfSmrg<function>XkbNote<replaceable>{Component}</replaceable>Changes</function>, 2140f8248bfSmrgwhere 2150f8248bfSmrg<replaceable>Component</replaceable> 2160f8248bfSmrgis the name of a major Xkb component such as 2170f8248bfSmrg<literal>Map</literal> 2180f8248bfSmrgor 2190f8248bfSmrg<literal>Names</literal>. 2200f8248bfSmrgWhen you want to copy these changes from the server into a local copy of the 2210f8248bfSmrgkeyboard description, use the corresponding 2220f8248bfSmrg<function>XkbGet<replaceable>{Component}</replaceable>Changes</function> 2230f8248bfSmrgfunction, 224e9fcaa8aSmrgpassing it the changes structure. The function then retrieves only the changed 225e9fcaa8aSmrgstructures from the server and copies the modified pieces into the local 226e9fcaa8aSmrgkeyboard description. 227e9fcaa8aSmrg</para> 228e9fcaa8aSmrg 229e9fcaa8aSmrg</sect1> 230eb411b4bSmrg<sect1 id='Freeing_Data_Structures'> 231e9fcaa8aSmrg<title>Freeing Data Structures</title> 232e9fcaa8aSmrg 233e9fcaa8aSmrg<para> 2340f8248bfSmrgFor the same reasons you should not directly use 2350f8248bfSmrg<function>malloc</function> 2360f8248bfSmrgto allocate Xkb data structures, you should not free Xkb data structures or 2370f8248bfSmrgcomponents directly using 2380f8248bfSmrg<function>free</function> 2390f8248bfSmrgor 2400f8248bfSmrg<function>Xfree</function>. 2410f8248bfSmrgXkb provides functions to free the various data structures and their 242e9fcaa8aSmrgcomponents. Always use the free functions supplied by Xkb. There is no 2430f8248bfSmrgguarantee that any particular field can be safely freed by 2440f8248bfSmrg<function>free</function> 2450f8248bfSmrgor 2460f8248bfSmrg<function>Xfree</function>. 247e9fcaa8aSmrg</para> 248e9fcaa8aSmrg</sect1> 249e9fcaa8aSmrg</chapter> 250