# Display Functions

Before your program can use a display, you must establish a connection to the X server. Once you have established a connection, you then can use the Xlib macros and functions discussed in this chapter to return information about the display. This chapter discusses how to:

- Open (connect to) the display

- Obtain information about the display, image formats, or screens

- Generate a NoOperation protocol request

- Free client-created data

- Close (disconnect from) a display

- Use X Server connection close operations

- Use Xlib with threads

- Use internal connections

## Opening the Display

To open a connection to the X server that controls a display, use .

XOpenDisplay
Display *`XOpenDisplay` char *display_name

display_name

  Specifies the hardware display name, which determines the display and communications domain to be used. On a POSIX-conformant system, if the display_name is NULL, it defaults to the value of the DISPLAY environment variable.

  Environment DISPLAY

The encoding and interpretation of the display name are implementation-dependent. Strings in the Host Portable Character Encoding are supported; support for other characters is implementation-dependent. On POSIX-conformant systems, the display name or DISPLAY environment variable can be a string in the format:

    	protocol/hostname:number.screen_number

protocol

  Specifies a protocol family or an alias for a protocol family. Supported protocol families are implementation dependent. The protocol entry is optional. If protocol is not specified, the / separating protocol and hostname must also not be specified.

hostname

  Specifies the name of the host machine on which the display is physically attached. You follow the hostname with either a single colon (:) or a double colon (::).

number

  Specifies the number of the display server on that host machine. You may optionally follow this display number with a period (.). A single CPU can have more than one display. Multiple displays are usually numbered starting with zero.

  Screen
screen_number

  Specifies the screen to be used on that server. Multiple screens can be controlled by a single X server. The screen_number sets an internal variable that can be accessed by using the `DefaultScreen` macro or the function if you are using languages other than C (see section 2.2.1).

For example, the following would specify screen 1 of display 0 on the machine named ``dual-headed'':

    dual-headed:0.1

The function returns a Display structure that serves as the connection to the X server and that contains all the information about that X server. connects your application to the X server through TCP or DECnet communications protocols, or through some local inter-process communication protocol. If the protocol is specified as "tcp", "inet", or "inet6", or if no protocol is specified and the hostname is a host machine name and a single colon (:) separates the hostname and display number, connects using TCP streams. (If the protocol is specified as "inet", TCP over IPv4 is used. If the protocol is specified as "inet6", TCP over IPv6 is used. Otherwise, the implementation determines which IP version is used.) If the hostname and protocol are both not specified, Xlib uses whatever it believes is the fastest transport. If the hostname is a host machine name and a double colon (::) separates the hostname and display number, connects using DECnet. A single X server can support any or all of these transport mechanisms simultaneously. A particular Xlib implementation can support many more of these transport mechanisms.

Protocol TCP
Protocol DECnet

If successful, returns a pointer to a Display structure, which is defined in `<X11/Xlib.h>`. If does not succeed, it returns NULL. After a successful call to , all of the screens in the display can be used by the client. The screen number specified in the display_name argument is returned by the `DefaultScreen` macro (or the function). You can access elements of the Display and Screen structures only by using the information macros or functions. For information about using macros and functions to obtain information from the Display structure, see section 2.2.1.

Display
X11/Xlib.h
Files <X11/Xlib.h>
Headers <X11/Xlib.h>

X servers may implement various types of access control mechanisms (see section 9.8).

## Obtaining Information about the Display, Image Formats, or Screens

The Xlib library provides a number of useful macros and corresponding functions that return data from the Display structure. The macros are used for C programming, and their corresponding function equivalents are for other language bindings. This section discusses the:

- Display macros

- Image format functions and macros

- Screen information macros

All other members of the Display structure (that is, those for which no macros are defined) are private to Xlib and must not be used. Applications must never directly modify or inspect these private members of the Display structure. The , , , , , and functions in the next sections are misnamed. These functions really should be named Screenwhatever and XScreenwhatever, not Displaywhatever or XDisplaywhatever. Our apologies for the resulting confusion.

Display data structure

### Display Macros

Applications should not directly modify any part of the Display and Screen structures. The members should be considered read-only, although they may change as the result of other operations on the display.

The following lists the C language macros, their corresponding function equivalents that are for other language bindings, and what data both can return.

    AllPlanes

unsigned long `XAllPlanes`

Both return a value with all bits set to 1 suitable for use in a plane argument to a procedure.

AllPlanes
XAllPlanes

Both `BlackPixel` and `WhitePixel` can be used in implementing a monochrome application. These pixel values are for permanently allocated entries in the default colormap. The actual RGB (red, green, and blue) values are settable on some screens and, in any case, may not actually be black or white. The names are intended to convey the expected relative intensity of the colors.

BlackPixel
XBlackPixel
WhitePixel
XWhitePixel
`BlackPixel` display screen_number unsigned long `XBlackPixel` Display *display int screen_number

display

  Specifies the connection to the X server.

screen_number

  Specifies the appropriate screen number on the host server.

Both return the black pixel value for the specified screen.

`WhitePixel` display screen_number unsigned long `XWhitePixel` Display *display int screen_number

display

  Specifies the connection to the X server.

screen_number

  Specifies the appropriate screen number on the host server.

Both return the white pixel value for the specified screen.

`ConnectionNumber` display int `XConnectionNumber` Display *display

display

  Specifies the connection to the X server.

Both return a connection number for the specified display. On a POSIX-conformant system, this is the file descriptor of the connection.

ConnectionNumber
XConnectionNumber

`DefaultColormap` display screen_number Colormap `XDefaultColormap` Display *display int screen_number

display

  Specifies the connection to the X server.

screen_number

  Specifies the appropriate screen number on the host server.

Both return the default colormap ID for allocation on the specified screen. Most routine allocations of color should be made out of this colormap.

DefaultColormap
XDefaultColormap

`DefaultDepth` display screen_number int `XDefaultDepth` Display *display int screen_number

display

  Specifies the connection to the X server.

screen_number

  Specifies the appropriate screen number on the host server.

Both return the depth (number of planes) of the default root window for the specified screen. Other depths may also be supported on this screen (see ).

DefaultDepth
XDefaultDepth

To determine the number of depths that are available on a given screen, use `XListDepths`.

XListDepths
int *`XListDepths` Display *display int screen_number int *count_return

display

  Specifies the connection to the X server.

screen_number

  Specifies the appropriate screen number on the host server.

count_return

  Returns the number of depths.

The `XListDepths` function returns the array of depths that are available on the specified screen. If the specified screen_number is valid and sufficient memory for the array can be allocated, `XListDepths` sets count_return to the number of available depths. Otherwise, it does not set count_return and returns NULL. To release the memory allocated for the array of depths, use .

`DefaultGC` display screen_number GC `XDefaultGC` Display *display int screen_number

display

  Specifies the connection to the X server.

screen_number

  Specifies the appropriate screen number on the host server.

Both return the default graphics context for the root window of the specified screen. This GC is created for the convenience of simple applications and contains the default GC components with the foreground and background pixel values initialized to the black and white pixels for the screen, respectively. You can modify its contents freely because it is not used in any Xlib function. This GC should never be freed.

DefaultGC
XDefaultGC

`DefaultRootWindow` display Window `XDefaultRootWindow` Display *display

display

  Specifies the connection to the X server.

Both return the root window for the default screen.

DefaultRootWindow
XDefaultRootWindow

`DefaultScreenOfDisplay` display Screen *`XDefaultScreenOfDisplay` Display *display

display

  Specifies the connection to the X server.

Both return a pointer to the default screen.

DefaultScreenOfDisplay
XDefaultScreenOfDisplay

`ScreenOfDisplay` display screen_number Screen *`XScreenOfDisplay` Display *display int screen_number

display

  Specifies the connection to the X server.

screen_number

  Specifies the appropriate screen number on the host server.

Both return a pointer to the indicated screen.

ScreenOfDisplay
XScreenOfDisplay

`DefaultScreen` display int `XDefaultScreen` Display *display

display

  Specifies the connection to the X server.

Both return the default screen number referenced by the function. This macro or function should be used to retrieve the screen number in applications that will use only a single screen.

DefaultScreen
XDefaultScreen

`DefaultVisual` display screen_number Visual *`XDefaultVisual` Display *display int screen_number

display

  Specifies the connection to the X server.

screen_number

  Specifies the appropriate screen number on the host server.

Both return the default visual type for the specified screen. For further information about visual types, see section 3.1.

DefaultVisual
XDefaultVisual

`DisplayCells` display screen_number int `XDisplayCells` Display *display int screen_number

display

  Specifies the connection to the X server.

screen_number

  Specifies the appropriate screen number on the host server.

Both return the number of entries in the default colormap.

DisplayCells
XDisplayCells

`DisplayPlanes` display screen_number int `XDisplayPlanes` Display *display int screen_number

display

  Specifies the connection to the X server.

screen_number

  Specifies the appropriate screen number on the host server.

Both return the depth of the root window of the specified screen. For an explanation of depth, see the glossary.

DisplayPlanes
XDisplayPlanes

`DisplayString` display char *`XDisplayString` Display *display

display

  Specifies the connection to the X server.

Both return the string that was passed to when the current display was opened. On POSIX-conformant systems, if the passed string was NULL, these return the value of the DISPLAY environment variable when the current display was opened. These are useful to applications that invoke the `fork` system call and want to open a new connection to the same display from the child process as well as for printing error messages.

DisplayString
XDisplayString
POSIX System Call fork

long `XExtendedMaxRequestSize` Display *display

display

  Specifies the connection to the X server.

The `XExtendedMaxRequestSize` function returns zero if the specified display does not support an extended-length protocol encoding; otherwise, it returns the maximum request size (in 4-byte units) supported by the server using the extended-length encoding. The Xlib functions , , , , , and will use the extended-length encoding as necessary, if supported by the server. Use of the extended-length encoding in other Xlib functions (for example, , , , , , ) is permitted but not required; an Xlib implementation may choose to split the data across multiple smaller requests instead.

XExtendedMaxRequestSize

long `XMaxRequestSize` Display *display

display

  Specifies the connection to the X server.

The `XMaxRequestSize` function returns the maximum request size (in 4-byte units) supported by the server without using an extended-length protocol encoding. Single protocol requests to the server can be no larger than this size unless an extended-length protocol encoding is supported by the server. The protocol guarantees the size to be no smaller than 4096 units (16384 bytes). Xlib automatically breaks data up into multiple protocol requests as necessary for the following functions: , , , , , and .

XMaxRequestSize

`LastKnownRequestProcessed` display unsigned long `XLastKnownRequestProcessed` Display *display

display

  Specifies the connection to the X server.

Both extract the full serial number of the last request known by Xlib to have been processed by the X server. Xlib automatically sets this number when replies, events, and errors are received.

LastKnownRequestProcessed
XLastKnownRequestProcessed

`NextRequest` display unsigned long `XNextRequest` Display *display

display

  Specifies the connection to the X server.

Both extract the full serial number that is to be used for the next request. Serial numbers are maintained separately for each display connection.

NextRequest
XNextRequest

`ProtocolVersion` display int `XProtocolVersion` Display *display

display

  Specifies the connection to the X server.

Both return the major version number (11) of the X protocol associated with the connected display.

ProtocolVersion
XProtocolVersion

`ProtocolRevision` display int `XProtocolRevision` Display *display

display

  Specifies the connection to the X server.

Both return the minor protocol revision number of the X server.

ProtocolRevision
XProtocolRevision

`QLength` display int `XQLength` Display *display

display

  Specifies the connection to the X server.

Both return the length of the event queue for the connected display. Note that there may be more events that have not been read into the queue yet (see ).

QLength
XQLength

`RootWindow` display screen_number Window `XRootWindow` Display *display int screen_number

display

  Specifies the connection to the X server.

screen_number

  Specifies the appropriate screen number on the host server.

Both return the root window. These are useful with functions that need a drawable of a particular screen and for creating top-level windows.

Window RootWindow
RootWindow
Window XRootWindow
XRootWindow

`ScreenCount` display int `XScreenCount` Display *display

display

  Specifies the connection to the X server.

Both return the number of available screens.

ScreenCount
XScreenCount

`ServerVendor` display char *`XServerVendor` Display *display

display

  Specifies the connection to the X server.

Both return a pointer to a null-terminated string that provides some identification of the owner of the X server implementation. If the data returned by the server is in the Latin Portable Character Encoding, then the string is in the Host Portable Character Encoding. Otherwise, the contents of the string are implementation-dependent.

ServerVendor
XServerVendor

`VendorRelease` display int `XVendorRelease` Display *display

display

  Specifies the connection to the X server.

Both return a number related to a vendor's release of the X server.

VendorRelease
XVendorRelease

### Image Format Functions and Macros

Applications are required to present data to the X server in a format that the server demands. To help simplify applications, most of the work required to convert the data is provided by Xlib (see sections 8.7 and 16.8).

The XPixmapFormatValues structure provides an interface to the pixmap format information that is returned at the time of a connection setup. It contains:

    typedef struct {
    	int depth;
    	int bits_per_pixel;
    	int scanline_pad;
    } XPixmapFormatValues;

XPixmapFormatValues

To obtain the pixmap format information for a given display, use `XListPixmapFormats`.

XListPixmapFormats
XPixmapFormatValues *`XListPixmapFormats` Display *display int *count_return

display

  Specifies the connection to the X server.

count_return

  Returns the number of pixmap formats that are supported by the display.

The `XListPixmapFormats` function returns an array of XPixmapFormatValues structures that describe the types of Z format images supported by the specified display. If insufficient memory is available, `XListPixmapFormats` returns NULL. To free the allocated storage for the XPixmapFormatValues structures, use .

The following lists the C language macros, their corresponding function equivalents that are for other language bindings, and what data they both return for the specified server and screen. These are often used by toolkits as well as by simple applications.

`ImageByteOrder` display int `XImageByteOrder` Display *display

display

  Specifies the connection to the X server.

Both specify the required byte order for images for each scanline unit in XY format (bitmap) or for each pixel value in Z format. The macro or function can return either LSBFirst or MSBFirst.

ImageByteOrder
XImageByteOrder

`XBitmapUnit` display int `XBitmapUnit` Display *display

display

  Specifies the connection to the X server.

Both return the size of a bitmap's scanline unit in bits. The scanline is calculated in multiples of this value.

BitmapUnit
XBitmapUnit

`BitmapBitOrder` display int `XBitmapBitOrder` Display *display

display

  Specifies the connection to the X server.

Within each bitmap unit, the left-most bit in the bitmap as displayed on the screen is either the least significant or most significant bit in the unit. This macro or function can return LSBFirst or MSBFirst.

BitmapBitOrder
XBitmapBitOrder

`BitmapPad` display int `XBitmapPad` Display *display

display

  Specifies the connection to the X server.

Each scanline must be padded to a multiple of bits returned by this macro or function.

BitmapPad
XBitmapPad

`DisplayHeight` display screen_number int `XDisplayHeight` Display *display int screen_number

display

  Specifies the connection to the X server.

screen_number

  Specifies the appropriate screen number on the host server.

Both return an integer that describes the height of the screen in pixels.

DisplayHeight
XDisplayHeight

`DisplayHeightMM` display screen_number int `XDisplayHeightMM` Display *display int screen_number

display

  Specifies the connection to the X server.

screen_number

  Specifies the appropriate screen number on the host server.

Both return the height of the specified screen in millimeters.

DisplayHeightMM
XDisplayHeightMM

`DisplayWidth` display screen_number int `XDisplayWidth` Display *display int screen_number

display

  Specifies the connection to the X server.

screen_number

  Specifies the appropriate screen number on the host server.

Both return the width of the screen in pixels.

DisplayWidth
XDisplayWidth

`DisplayWidthMM` display screen_number int `XDisplayWidthMM` Display *display int screen_number

display

  Specifies the connection to the X server.

screen_number

  Specifies the appropriate screen number on the host server.

Both return the width of the specified screen in millimeters.

DisplayWidthMM
XDisplayWidthMM

### Screen Information Macros

The following lists the C language macros, their corresponding function equivalents that are for other language bindings, and what data they both can return. These macros or functions all take a pointer to the appropriate screen structure.

`BlackPixelOfScreen` screen unsigned long `XBlackPixelOfScreen` Screen *screen

screen

  Specifies the appropriate Screen structure.

Both return the black pixel value of the specified screen.

BlackPixelOfScreen
XBlackPixelOfScreen

`XWhitePixelOfScreen` screen unsigned long `XWhitePixelOfScreen` Screen *screen

screen

  Specifies the appropriate Screen structure.

Both return the white pixel value of the specified screen.

WhitePixelOfScreen
XWhitePixelOfScreen

`CellsOfScreen` screen int `XCellsOfScreen` Screen *screen

screen

  Specifies the appropriate Screen structure.

Both return the number of colormap cells in the default colormap of the specified screen.

CellsOfScreen
XCellsOfScreen

`DefaultColormapOfScreen` screen Colormap `XDefaultColormapOfScreen` Screen *screen

screen

  Specifies the appropriate Screen structure.

Both return the default colormap of the specified screen.

DefaultColormapOfScreen
XDefaultColormapOfScreen

`DefaultDepthOfScreen` screen int `XDefaultDepthOfScreen` Screen *screen

screen

  Specifies the appropriate Screen structure.

Both return the depth of the root window.

DefaultDepthOfScreen
XDefaultDepthOfScreen

`DefaultGCOfScreen` screen GC `XDefaultGCOfScreen` Screen *screen

screen

  Specifies the appropriate Screen structure.

Both return a default graphics context (GC) of the specified screen, which has the same depth as the root window of the screen. The GC must never be freed.

DefaultGCOfScreen
XDefaultGCOfScreen

`XDefaultVisualOfScreen` screen Visual *`XDefaultVisualOfScreen` Screen *screen

screen

  Specifies the appropriate Screen structure.

Both return the default visual of the specified screen. For information on visual types, see section 3.1.

DefaultVisualOfScreen
XDefaultVisualOfScreen

`DoesBackingStore` screen int `XDoesBackingStore` Screen *screen

screen

  Specifies the appropriate Screen structure.

Both return a value indicating whether the screen supports backing stores. The value returned can be one of WhenMapped, NotUseful, or Always (see section 3.2.4).

DoesBackingStore
XDoesBackingStore

`DoesSaveUnders` screen Bool `XDoesSaveUnders` Screen *screen

screen

  Specifies the appropriate Screen structure.

Both return a Boolean value indicating whether the screen supports save unders. If True, the screen supports save unders. If False, the screen does not support save unders (see section 3.2.5).

DoesSaveUnders
XDoesSaveUnders

`DisplayOfScreen` screen Display *`XDisplayOfScreen` Screen *screen

screen

  Specifies the appropriate Screen structure.

Both return the display of the specified screen.

DisplayOfScreen
XDisplayOfScreen

XScreenNumberOfScreen
`ScreenNumberOfScreen` screen long `XScreenNumberOfScreen` Screen *screen

screen

  Specifies the appropriate Screen structure.

The `XScreenNumberOfScreen` function returns the screen index number of the specified screen.

`EventMaskOfScreen` screen long `XEventMaskOfScreen` Screen *screen

screen

  Specifies the appropriate Screen structure.

Both return the event mask of the root window for the specified screen at connection setup time.

EventMaskOfScreen
XEventMaskOfScreen

`WidthOfScreen` screen int `XWidthOfScreen` Screen *screen

screen

  Specifies the appropriate Screen structure.

Both return the width of the specified screen in pixels.

WidthOfScreen
XWidthOfScreen

`HeightOfScreen` screen int `XHeightOfScreen` Screen *screen

screen

  Specifies the appropriate Screen structure.

Both return the height of the specified screen in pixels.

HeightOfScreen
XHeightOfScreen

`WidthMMOfScreen` screen int `XWidthMMOfScreen` Screen *screen

screen

  Specifies the appropriate Screen structure.

Both return the width of the specified screen in millimeters.

WidthMMOfScreen
XWidthMMOfScreen

`HeightMMOfScreen` screen int `XHeightMMOfScreen` Screen *screen

screen

  Specifies the appropriate Screen structure.

Both return the height of the specified screen in millimeters.

HeightMMOfScreen
XHeightMMOfScreen

`MaxCmapsOfScreen` screen int `XMaxCmapsOfScreen` Screen *screen

screen

  Specifies the appropriate Screen structure.

Both return the maximum number of installed colormaps supported by the specified screen (see section 9.3).

MaxCmapsOfScreen
XMaxCmapsOfScreen

`MinCmapsOfScreen` screen int `XMinCmapsOfScreen` Screen *screen

screen

  Specifies the appropriate Screen structure.

Both return the minimum number of installed colormaps supported by the specified screen (see section 9.3).

MinCmapsOfScreen
XMinCmapsOfScreen

`PlanesOfScreen` screen int `XPlanesOfScreen` Screen *screen

screen

  Specifies the appropriate Screen structure.

Both return the depth of the root window.

PlanesOfScreen
XPlanesOfScreen

`RootWindowOfScreen` screen Window `XRootWindowOfScreen` Screen *screen

screen

  Specifies the appropriate Screen structure.

Both return the root window of the specified screen.

RootWindowOfScreen
XRootWindowOfScreen

## Generating a NoOperation Protocol Request

To execute a NoOperation protocol request, use .

XNoOp
`XNoOp` Display *display

display

  Specifies the connection to the X server.

The function sends a NoOperation protocol request to the X server, thereby exercising the connection.

## Freeing Client-Created Data

To free in-memory data that was created by an Xlib function, use .

XFree
`XFree` void *data

data

  Specifies the data that is to be freed.

The function is a general-purpose Xlib routine that frees the specified data. You must use it to free any objects that were allocated by Xlib, unless an alternate function is explicitly specified for the object. A NULL pointer cannot be passed to this function.

## Closing the Display

To close a display or disconnect from the X server, use `XCloseDisplay`.

XCloseDisplay

`XCloseDisplay` Display *display

display

  Specifies the connection to the X server.

The `XCloseDisplay` function closes the connection to the X server for the display specified in the Display structure and destroys all windows, resource IDs (Window, Font, Pixmap, Colormap, Cursor, and GContext), or other resources that the client has created on this display, unless the close-down mode of the client has been changed (see ). Therefore, these windows, resource IDs, and other resources should never be referenced again or an error will be generated. Before exiting, you should call `XCloseDisplay` explicitly so that any pending errors are reported as `XCloseDisplay` performs a final operation.

Resource IDs
XCloseDisplay

`XCloseDisplay` can generate a BadGC error.

Xlib provides a function to permit the resources owned by a client to survive after the client's connection is closed. To change a client's close-down mode, use .

XSetCloseDownMode
`XSetCloseDownMode` Display *display int close_mode

display

  Specifies the connection to the X server.

close_mode

  Specifies the client close-down mode. You can pass DestroyAll, RetainPermanent, or RetainTemporary.

The function defines what will happen to the client's resources at connection close. A connection starts in DestroyAll mode. For information on what happens to the client's resources when the close_mode argument is RetainPermanent or RetainTemporary, see section 2.6.

can generate a BadValue error.

## Using X Server Connection Close Operations

When the X server's connection to a client is closed either by an explicit call to `XCloseDisplay` or by a process that exits, the X server performs the following automatic operations:

- It disowns all selections owned by the client (see ).

- It performs an and if the client has actively grabbed the pointer or the keyboard.

- It performs an if the client has grabbed the server.

- It releases all passive grabs made by the client.

- It marks all resources (including colormap entries) allocated by the client either as permanent or temporary, depending on whether the close-down mode is RetainPermanent or RetainTemporary. However, this does not prevent other client applications from explicitly destroying the resources (see ).

When the close-down mode is DestroyAll, the X server destroys all of a client's resources as follows:

- It examines each window in the client's save-set to determine if it is an inferior (subwindow) of a window created by the client. (The save-set is a list of other clients' windows that are referred to as save-set windows.) If so, the X server reparents the save-set window to the closest ancestor so that the save-set window is not an inferior of a window created by the client. The reparenting leaves unchanged the absolute coordinates (with respect to the root window) of the upper-left outer corner of the save-set window.

- It performs a MapWindow request on the save-set window if the save-set window is unmapped. The X server does this even if the save-set window was not an inferior of a window created by the client.

- It destroys all windows created by the client.

- It performs the appropriate free request on each nonwindow resource created by the client in the server (for example, Font, Pixmap, Cursor, Colormap, and GContext).

- It frees all colors and colormap entries allocated by a client application.

Additional processing occurs when the last connection to the X server closes. An X server goes through a cycle of having no connections and having some connections. When the last connection to the X server closes as a result of a connection closing with the close_mode of DestroyAll, the X server does the following:

- It resets its state as if it had just been started. The X server begins by destroying all lingering resources from clients that have terminated in RetainPermanent or RetainTemporary mode.

- It deletes all but the predefined atom identifiers.

- It deletes all properties on all root windows (see section 4.3).

- It resets all device maps and attributes (for example, key click, bell volume, and acceleration) as well as the access control list.

- It restores the standard root tiles and cursors.

- It restores the default font path.

- It restores the input focus to state PointerRoot.

However, the X server does not reset if you close a connection with a close-down mode set to RetainPermanent or RetainTemporary.

## Using Xlib with Threads

On systems that have threads, support may be provided to permit multiple threads to use Xlib concurrently.

To initialize support for concurrent threads, use `XInitThreads`.

XInitThreads
Status `XInitThreads`

The `XInitThreads` function initializes Xlib support for concurrent threads. This function must be the first Xlib function a multi-threaded program calls, and it must complete before any other Xlib call is made. This function returns a nonzero status if initialization was successful; otherwise, it returns zero. On systems that do not support threads, this function always returns zero.

It is only necessary to call this function if multiple threads might use Xlib concurrently. If all calls to Xlib functions are protected by some other access mechanism (for example, a mutual exclusion lock in a toolkit or through explicit client programming), Xlib thread initialization is not required. It is recommended that single-threaded programs not call this function.

To lock a display across several Xlib calls, use `XLockDisplay`.

XLockDisplay
`XLockDisplay` Display *display

display

  Specifies the connection to the X server.

The `XLockDisplay` function locks out all other threads from using the specified display. Other threads attempting to use the display will block until the display is unlocked by this thread. Nested calls to `XLockDisplay` work correctly; the display will not actually be unlocked until has been called the same number of times as `XLockDisplay`. This function has no effect unless Xlib was successfully initialized for threads using `XInitThreads`.

To unlock a display, use .

XUnlockDisplay
`XUnlockDisplay` Display *display

display

  Specifies the connection to the X server.

The function allows other threads to use the specified display again. Any threads that have blocked on the display are allowed to continue. Nested locking works correctly; if `XLockDisplay` has been called multiple times by a thread, then must be called an equal number of times before the display is actually unlocked. This function has no effect unless Xlib was successfully initialized for threads using `XInitThreads`.

## Using Internal Connections

In addition to the connection to the X server, an Xlib implementation may require connections to other kinds of servers (for example, to input method servers as described in chapter 13). Toolkits and clients that use multiple displays, or that use displays in combination with other inputs, need to obtain these additional connections to correctly block until input is available and need to process that input when it is available. Simple clients that use a single display and block for input in an Xlib event function do not need to use these facilities.

To track internal connections for a display, use .

typedef void (*XConnectionWatchProc) Display *display XPointer client_data int fd Bool opening XPointer *watch_data Status `XAddConnectionWatch` Display *display XConnectionWatchProc procedure XPointer client_data

display

  Specifies the connection to the X server.

procedure

  Specifies the procedure to be called.

client_data

  Specifies the additional client data.

The function registers a procedure to be called each time Xlib opens or closes an internal connection for the specified display. The procedure is passed the display, the specified client_data, the file descriptor for the connection, a Boolean indicating whether the connection is being opened or closed, and a pointer to a location for private watch data. If opening is True, the procedure can store a pointer to private data in the location pointed to by watch_data; when the procedure is later called for this same connection and opening is False, the location pointed to by watch_data will hold this same private data pointer.

This function can be called at any time after a display is opened. If internal connections already exist, the registered procedure will immediately be called for each of them, before returns. returns a nonzero status if the procedure is successfully registered; otherwise, it returns zero.

The registered procedure should not call any Xlib functions. If the procedure directly or indirectly causes the state of internal connections or watch procedures to change, the result is not defined. If Xlib has been initialized for threads, the procedure is called with the display locked and the result of a call by the procedure to any Xlib function that locks the display is not defined unless the executing thread has externally locked the display using `XLockDisplay`.

To stop tracking internal connections for a display, use `XRemoveConnectionWatch`.

XRemoveConnectionWatch

Status `XRemoveConnectionWatch` Display *display XConnectionWatchProc procedure XPointer client_data

display

  Specifies the connection to the X server.

procedure

  Specifies the procedure to be called.

client_data

  Specifies the additional client data.

The `XRemoveConnectionWatch` function removes a previously registered connection watch procedure. The client_data must match the client_data used when the procedure was initially registered.

To process input on an internal connection, use .

XProcessInternalConnection

void `XProcessInternalConnection` Display *display int fd

display

  Specifies the connection to the X server.

fd

  Specifies the file descriptor.

The function processes input available on an internal connection. This function should be called for an internal connection only after an operating system facility (for example, `select` or `poll`) has indicated that input is available; otherwise, the effect is not defined.

To obtain all of the current internal connections for a display, use .

XInternalConnectionNumbers

Status `XInternalConnectionNumbers` Display *display int ** fd int * count_return

display

  Specifies the connection to the X server.

fd_return

  Returns the file descriptors.

count_return

  Returns the number of file descriptors.

The function returns a list of the file descriptors for all internal connections currently open for the specified display. When the allocated list is no longer needed, free it by using . This functions returns a nonzero status if the list is successfully allocated; otherwise, it returns zero.