DESCRIPTION

X11::Protocol is a client-side interface to the X11 Protocol (see X(1) for information about X11), allowing perl programs to display windows and graphics on X11 servers.

A full description of the protocol is beyond the scope of this documentation; for complete information, see the X Window System Protocol, X Version 11, available as Postscript or *roff source from ftp://ftp.x.org, or Volume 0: X Protocol Reference Manual of O'Reilly & Associates's series of books about X (ISBN 1-56592-083-X, http://www.oreilly.com), which contains most of the same information.

DISCLAIMER

``The protocol contains many management mechanisms that are not intended for normal applications. Not all mechanisms are needed to build a particular user interface. It is important to keep in mind that the protocol is intended to provide mechanism, not policy.'' -- Robert W. Scheifler

new_rsrc

$x->new_rsrc;

Returns a new resource identifier. A unique resource ID is required for every object that the server creates on behalf of the client: windows, fonts, cursors, etc. (IDs are chosen by the client instead of the server for efficiency -- the client doesn't have to wait for the server to acknowledge the creation before starting to use the object).

Note that the total number of available resource IDs, while large, is finite. Beginning from the establishment of a connection, resource IDs are allocated sequentially from a range whose size is server dependent (commonly 2**21, about 2 million). If this limit is reached and the server does not support the XC_MISC extension, subsequent calls to new_rsrc will croak. If the server does support this extension, the module will attempt to request a new range of free IDs from the server. This should allow the program to continue, but it is an imperfect solution, as over time the set of available IDs may fragment, requiring increasingly frequent round-trip range requests from the server. For long-running programs, the best approach may be to keep track of free IDs as resources are destroyed. In the current version, however, no special support is provided for this.

handle_input

$x->handle_input;

Get one chunk of information from the server, and do something with it. If it's an error, handle it using the protocol object's handler ('error_handler' -- default is kill the program with an explanatory message). If it's an event, pass it to the chosen event handler, or put it in a queue if the handler is 'queue'. If it's a reply to a request, save using the object's 'replies' hash for further processing.

atom_name

$name = $x->atom_name($atom);

Return the string corresponding to the atom $atom. This is similar to the GetAtomName request, but caches the result for efficiency.

atom

$atom = $x->atom($name);

The inverse operation; Return the (numeric) atom corresponding to $name. This is similar to the InternAtom request, but caches the result.

choose_screen

$x->choose_screen($screen_num);

Indicate that you prefer to use a particular screen of the display. Per-screen information, such as 'root', 'width_in_pixels', and 'white_pixel' will be made available as

$x->{'root'}

instead of

$x->{'screens'}[$screen_num]{'root'}

SYMBOLIC CONSTANTS

Generally, symbolic constants used by the protocol, like 'CopyFromParent' or 'PieSlice' are passed to methods as strings, and converted into numbers by the module. Their names are the same as those in the protocol specification, including capitalization, but with hyphens ('-') changed to underscores ('_') to look more perl-ish. If you want to do the conversion yourself for some reason, the following methods are available:

num

$num = $x->num($type, $str)

Given a string representing a constant and a string specifying what type of constant it is, return the corresponding number. $type should be a name like 'VisualClass' or 'GCLineStyle'. If the name is not recognized, it is returned intact.

interp

$name = $x->interp($type, $num)

The inverse operation; given a number and string specifying its type, return a string representing the constant.

You can disable interp() and the module's internal interpretation of numbers by setting $x->{'do_interp'} to zero. Of course, this isn't very useful, unless you have you own definitions for all the constants.

REQUESTS

request

Send a protocol request to the server, and get the reply, if any. For names of and information about individual requests, see below and/or the protocol reference manual.

robust_req

$x->robust_req('CreateWindow', ...);

Like request(), but if the server returns an error, return the error information rather than calling the error handler (which by default just croaks). If the request succeeds, returns an array reference containing whatever request() would have. Otherwise, returns the error type, the major and minor opcodes of the failed request, and the extra error information, if any. Note that even if the request normally wouldn't have a reply, this method still has to wait for a round-trip time to see whether an error occurred. If you're concerned about performance, you should design your error handling to be asynchronous.

add_reply

$x->add_reply($sequence_num, \$var);

Add a stub for an expected reply to the object's 'replies' hash. When a reply numbered $sequence_num comes, it will be stored in $var.

delete_reply

$x->delete_reply($sequence_num);

Delete the entry in 'replies' for the specified reply. (This should be done after the reply is received).

send

$x->send('CreateWindow', ...);

Send a request, but do not wait for a reply. You must handle the reply, if any, yourself, using add_reply(), handle_input(), delete_reply(), and unpack_reply(), generally in that order.

unpack_reply

$x->unpack_reply('GetWindowAttributes', $data);

Interpret the raw reply data $data, according to the reply format for the named request. Returns data in the same format as request($request_name, ...).

This section includes only a short calling summary for each request; for full descriptions, see the protocol standard. Argument order is usually the same as listed in the spec, but you generally don't have to pass lengths of strings or arrays, since perl keeps track. Symbolic constants are generally passed as strings. Most replies are returned as lists, but when there are many values, a hash is used. Lists usually come last; when there is more than one, each is passed by reference. In lists of multi-part structures, each element is a list ref. Parenthesis are inserted in arg lists for clarity, but are optional. Requests are listed in order by major opcode, so related requests are usually close together. Replies follow the '=>'.

The information in each hash is the same as the the information returned by QueryFont, but without per-character size information. This request is special in that it is the only request that can have more than one reply. This means you should probably only use request() with it, not send(), as the reply counting is complicated. Luckily, you never need this request anyway, as its function is completely duplicated by other requests.

$length specifies the length of the entire useless request, in four byte units, and is optional.

EVENTS

To receive events, first set the 'event_mask' attribute on a window to indicate what types of events you desire (see "pack_event_mask"). Then, set the protocol object's 'event_handler' to a subroutine reference that will handle the events. Alternatively, set 'event_handler' to 'queue', and retrieve events using dequeue_event() or next_event(). In both cases, events are returned as a hash. For instance, a typical MotionNotify event might look like this:

pack_event_mask

Make an event mask (suitable as the 'event_mask' of a window) from a list of strings specifying event types.

unpack_event_mask

@event_types = $x->unpack_event_mask($mask);

The inverse operation; convert an event mask obtained from the server into a list of names of event categories.

dequeue_event

%event = $x->dequeue_event;

If there is an event waiting in the queue, return it.

next_event

%event = $x->next_event;

Like Xlib's XNextEvent(), this function is equivalent to

$x->handle_input until %event = dequeue_event;

pack_event

$data = $x->pack_event(%event);

Given an event in hash form, pack it into a string. This is only useful as an argument to SendEvent().

unpack_event

%event = $x->unpack_event($data);

The inverse operation; given the raw data for an event (32 bytes), unpack it into hash form. Normally, this is done automatically.

EXTENSIONS

Protocol extensions add new requests, event types, and error types to the protocol. Support for them is compartmentalized in modules in the X11::Protocol::Ext:: hierarchy. For an example, see X11::Protocol::Ext::SHAPE. You can tell if the module has loaded an extension by looking at

$x->{'ext'}{$extension_name}

If the extension has been initialized, this value will be an array reference, [$major_request_number, $first_event_number, $first_error_number, $obj], where $obj is an object containing information private to the extension.

init_extension

$x->init_extension($name);

Initialize an extension: query the server to find the extension's request number, then load the corresponding module. Returns 0 if the server does not support the named extension, or if no module to interface with it exists.

init_extensions

$x->init_extensions;

Initialize protocol extensions. This does a ListExtensions request, then calls init_extension() for each extension that the server supports.

WRITING EXTENSIONS

Internally, the X11::Protocol module is table driven. All an extension has to do is to add new add entries to the protocol object's tables. An extension module should use X11::Protocol, and should define an new() method

each element of which is either [\&unpack_sub, \&pack_sub] or [$pack_format, $field, $field, ...], where each $field is 'name', ['name', 'const_type'], or ['name', ['special_name_for_zero', 'special_name_for_one']], where 'special_name_for_one' is optional.

Finally,

$x->{'ext_request'}{$major_request_number}

should be an array of arrays, with each array either [$name, \&packit] or [$name, \&packit, \&unpackit], and

$x->{'ext_request_num'}{$request_name}

should be initialized with [$minor_num, $major_num] for each request the extension defines. For examples of code that does all of this, look at X11::Protocol::Ext::SHAPE.

X11::Protocol exports several functions that might be useful in extensions (note that these are not methods).

padding

$p = padding $x;

Given an integer, compute the number need to round it up to a multiple of 4. For instance, padding(5) is 3.

pad

$p = pad $str;

Given a string, return the number of extra bytes needed to make a multiple of 4. Equivalent to padding(length($str)).

padded

$data = pack(padded($str), $str);

Return a format string, suitable for pack(), for a string padded to a multiple of 4 bytes. For instance, pack(padded('Hello'), 'Hello') gives "Hello\0\0\0".

hexi

$str = hexi $n;

Format a number in hexidecimal, and add a "0x" to the front.

make_num_hash

%hash = make_num_hash(['A', 'B', 'C']);

Given a reference to a list of strings, return a hash mapping the strings onto numbers representing their position in the list, as used by $x->{'ext_const_num'}.

BUGS

This module is too big (~2500 lines), too slow (10 sec to load on a slow machine), too inefficient (request args are copied several times), and takes up too much memory (3000K for basicwin).

If you have more than 65535 replies outstanding at once, sequence numbers can collide.