Jump to content

Xlib Programming Basics: Difference between revisions

From IdeaWazaWiki
wikademia>Darklama
+Computer programming
wikademia>Darklama
+ open and close connection sections
Line 1: Line 1:
Xlib is an application programming interface (API) for X Window. A program which uses Xlib can be broken down into 4 parts:
Xlib is an application programming interface (API) for X Window. A program which uses Xlib can be broken down into 4 parts:


* Opening a connection to a X Window server, using the default display or specifying a specific screen and display to use.
* Open a connection to a X Window server.
* Create windows for the user interface and map them so they are shown to the user
* Create windows for the user interface and map them so they are shown to the user
* Respond to events received from the X Window server, by drawing on windows or taking other actions. This includes the initial event to draw the content of each window for the first time.
* Respond to events received from the X Window server. Drawing the user interface is included in this step.
* Close a connection to a X Window server in response to a user's action when the time is right. Like for example when clicking quit from a menu.
* Close a connection to a X Window server in response to a user's action when the time is right. Like for example when clicking quit from a menu.
== Open A Connection ==
<source lang="c">
#include <X11/Xlib.h>
int main(int argc, char **argv) {
  Display *d = XOpenDisplay(argc > 1 ? argv[1] : NULL);
  return 0;
}
</source>
XOpenDisplay is a function that opens a connection to a X Window server. XOpenDisplay gets passed a string (e.g char*) that lets you control what display to connect to. You can also tell Xlib to attempt a connection with the default display by passing NULL instead. If a connection to a X Window server is successful, XOpenDisplay returns an allocated pointer to a Display structure, otherwise a null pointer (e.g (Display*)NULL) is returned.
You should check whether the connection was successful or not before calling any other functions from Xlib. Most other Xlib functions depend on knowing which display is being used. You may also need to know which display is being used in order to get characteristics of the display, such as how many screens there are, what resolutions does each screen support, and what color depths does each screen resolution support. A typical PC only has one screen, but supports several different screen resolutions and color depths for example.
== Close A Connection ==
<source lang="c">
#include <X11/Xlib.h>
int main(int argc, char **argv) {
  Display *d = XOpenDisplay(argc > 1 ? argv[1] : NULL);
  if (!d) {
    return -1;
  }
  int err = XCloseDisplay(d);
  return err;
}
</source>


[[Category:Computer programming]]
[[Category:Computer programming]]

Revision as of 14:43, 18 December 2008

Xlib is an application programming interface (API) for X Window. A program which uses Xlib can be broken down into 4 parts:

  • Open a connection to a X Window server.
  • Create windows for the user interface and map them so they are shown to the user
  • Respond to events received from the X Window server. Drawing the user interface is included in this step.
  • Close a connection to a X Window server in response to a user's action when the time is right. Like for example when clicking quit from a menu.

Open A Connection

<source lang="c">

  1. include <X11/Xlib.h>

int main(int argc, char **argv) {

 Display *d = XOpenDisplay(argc > 1 ? argv[1] : NULL);
 return 0;

} </source>

XOpenDisplay is a function that opens a connection to a X Window server. XOpenDisplay gets passed a string (e.g char*) that lets you control what display to connect to. You can also tell Xlib to attempt a connection with the default display by passing NULL instead. If a connection to a X Window server is successful, XOpenDisplay returns an allocated pointer to a Display structure, otherwise a null pointer (e.g (Display*)NULL) is returned.

You should check whether the connection was successful or not before calling any other functions from Xlib. Most other Xlib functions depend on knowing which display is being used. You may also need to know which display is being used in order to get characteristics of the display, such as how many screens there are, what resolutions does each screen support, and what color depths does each screen resolution support. A typical PC only has one screen, but supports several different screen resolutions and color depths for example.

Close A Connection

<source lang="c">

  1. include <X11/Xlib.h>

int main(int argc, char **argv) {

 Display *d = XOpenDisplay(argc > 1 ? argv[1] : NULL);
 if (!d) {
   return -1;
 }
 int err = XCloseDisplay(d);
 return err;

} </source>