Latest

Archive

Community news

C++

Communities and Content

Databases

Editorials

Emacs

General

HTML

Java

Notices

PHP

XML

Apache

C++

Database

General

HTML

Java

Javascript

Linux

Object oriented programming

Open source

Perl

PHP

Python

Ruby

SOAP

XML

Suggest a link

Advertise on zez

Contribute

Contact us

About zez


Xlib tutorial 1 - painting colors, graphics contexts and fonts



Have you ever wondered what is going on behind the scenes of X11 windowmanageres and toolkits? Here is a chance to get into native X programming with Xlib.


Hello world!
This first tutorial will get you into some of the basic elements of Xlib programming. We will create a small program that paints a big cross on the window and prints the famous "Hello world". Ofcourse all the X concepts will be explained along the way. If you want detail information about any of the functions, look at their respective man page (e.g "man XOpenDisplay").

The source code for this tutorial can be found on the bottom of the page.

Opening a display

Concept Display: A display is the physical connection to the X server. It is allways created with a call to XOpenDisplay which returns a Display structure holding information about the connection. Most X functions (those that require server interaction) take this structure as parameter.
Concept Screen: A screen denotes a physical monitor on your system. Under X it is represented by an integer value.

const char *display_name;
    if( ( display_name = (char *) getenv("DISPLAY") ) == NULL )
        display_name = "";

    /* Open a connection to the X server */
    Display *dpy = XOpenDisplay( display_name );
    if( !dpy ) 
    {
        // BSOD: we have no display.
        printf( "Cannot not open display %s\n", XDisplayName( display_name ) );
        exit( 1 );
    }

The call to XOpenDisplay takes one parameter, the display name. It is of the format "host:server.screen" and will ofcourse connect to the host given or localhost if it is ommited. Server is the running server on that computer. For most computers this will be 0 since you only run one instance of X at the time. (However you can start another with the -- :1 parameter to startx, which is very usefull for testing). The last part of the display name is the optional screen part. It is used for systems with more than one physical monitor and denotes which of the monitors will be the default one. This will be 0 for almost all modern computers.
If XOpenDisplay returns 0 we couldn't open a display. Usually this is fatal and so we kill the application.

Attached files:


| < 1 > | 2 | 3 | 4 | Next page >> | Printer-friendly page |

Comment List


There are no comments.


Forgot your password?

Register a new user

Results

Polls