Xlib, the handsome X.org Library...

Oh... so you want to learn about Xlib?

No, I know you don't want it. But let's explain anyway xD


Ok, before you can work with xlib, you need to understand some concepts first.

The X server was designed with network connections in mind

This fact forced developers to make a Server - Client design which is not usually the way we are "using" it

This is because when X server was born, computers were expensive and shit. So the common way to use a computer was having one computer shared with terminals ( This is also why we have tty drivers ).
Terminals were a keyboard with a screen. Users were connected to the "main pc" and sharing resources.

Having this in mind, the X server developers designed it for being able to accept network connections and they were aware of network delays, the expensive cost to send and receive data, etc.

So they used the Server/Client model and every Xlib application is connecting to the X server and making requests.

The thing is: You as a programmer, create an application. This application connects to the X server, asks to create windows, draw on them, and whatever. You are passing id numbers and saying what you want to do with the things those id numbers refer.

The server will throw to your face all events generated. Things like mouse movement, key press, window changes...

The thing is simple. You connect to the server, create a window, server gives you one int number to refer to that window. You use that int to tell server you want to work with this window. Then you use functions to draw on this window, you tell to the server you want receive events from this window, so server will throw the events you want. Then you check for events and act consequently.

Xlib programming is so easy, just need to know what are the things the developers decided.

For example when I say "application connects to the X server" You are using one function:
XOpenDisplay();

This function gives you an int number and display means the machine you're connecting to.
Remember Xlib applications are Server-Client based, so you can remotely draw things on other machine. When using this function you're conecting to this pc and is the X server running on this pc who will serve you the things you want.

Sadly, remote connections of this "kind" are not used commonly. Usually we want to draw on the same machine we are working.

So Display is the pc you are connecting to. The function to connect to the X server expects one argument, the host machine you want to connect.

Since we rarely will connect to other machine, the only argument we'll provide is NULL

Display *dis;

dis = XOpenDisplay ( NULL );