Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Haskell
Wiki community
Recent changes
Random page
HaskellWiki
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
XHB
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Getting started == NOTE: this tutorial is based on [http://xcb.freedesktop.org/tutorial/] === Introduction === The X Window System employs a client-server model for all its interactions, thus X applications are event-driven clients of an X server, which manages some system-wide resources (windows, input devices, etc.) and sends event notifications to clients. Every client (X application) can basically perform two things: * send requests (e.g., generate events, etc.) * receive replies (e.g., event notifications, etc.) === An example === A client may connect to the server, request that it draws a window (or several windows), and ask the server to send it any input the user sends to these windows. Thus, several clients may connect to a single X server (one might be running mail software, one running a WWW browser, etc.). When input is sent by the user to some window, the server sends a message to the client controlling this window for processing. The client decides what to do with this input, and sends the server requests for drawing in the window. === The X protocol === The whole session is carried out using the X message protocol. This protocol was originally carried over the TCP/IP protocol suite, allowing the client to run on any machine connected to the same network that the server is. Later on, the X servers were extended to allow clients running on the local machine with more optimized access to the server (note that an X protocol message may be several hundreds of KB in size), such as using shared memory, or using Unix domain sockets (a method for creating a logical channel on a Unix system between two processes). === The GUI programming model === A GUI program mostly sits idle, waiting for events sent by the X server, and then acts upon these events. An event may say "The user pressed the 1st button mouse in spot (x,y)", or "The window you control needs to be redrawn". In order for the program to be responsive to the user input, as well as to refresh requests, it needs to handle each event in a rather short period of time (e.g. less that 200 milliseconds, as a rule of thumb) This also implies that the program may not perform operations that might take a long time while handling an event (such as opening a network connection to some remote server, or connecting to a database server, or even performing a long file copy operation). Instead, it needs to perform all these operations in an asynchronous manner. This may be done by using various asynchronous models to perform the longish operations, or by performing them in a different process or thread. So the way a GUI program looks is something imperative in the lines of: 1. Open a connection to the X server. 2. Either poll or wait for events (an event pumping loop) 1. Receive the next event from the X server. 2. Handle the event, possibly generating more events or requests (e.g., sending various drawing requests to the X server) 3. If the event was a quit message, exit the loop 3. Close the connection to the X server Note that we omitted program initialization and other boring stuff. === Basic XHB notions === XHB has been created to eliminate the need for programs to actually implement the X protocol layer. This library gives a program a very low-level access to any X server. Since the protocol is standardized, a client using any implementation of XHB may talk with any X server (the same occurs for Xlib, of course). We now give a brief description of the basic XHB notions. They will be detailed later. * The X server connection which abstracts request and reply queues. * Requests and replies which are the means of communicating between X server and client. * The graphics context which holds the current state of graphics variables (what foreground and background colors to use, what font to use when drawing some text, etc.). Actually, this is how we reduce bandwidth consumption. * Events with additional information (e.g., position on the screen where event was generated, mouse button associated with the event, etc.). What is really interesting here, is that XHB is non-blocking (asynchronous). That is, it doesn't force you to wait for reply on your request. When sending a request, XHB returns immediately (with a cookie, "a promise"), even if the reply is not yet available; while Xlib, for instance, locks your program waiting for X server to reply. This can be very slow (and it is, in fact). Note that [[Lazy evaluation]] is good at hiding latency of this sort.
Summary:
Please note that all contributions to HaskellWiki are considered to be released under simple permissive license (see
HaskellWiki:Copyrights
for details). If you don't want your writing to be edited mercilessly and redistributed at will, then don't submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
DO NOT SUBMIT COPYRIGHTED WORK WITHOUT PERMISSION!
Cancel
Editing help
(opens in new window)
Toggle limited content width