Sandundhammika's Blog











{August 4, 2010}   Introduction To the Xlib Programming

Here in this note I’m going to explain the first program  that I published before.For the sake of convenient I’m going to list that program again here.

However before  we go depth I need to explain what is Xlib. Well Xlib is a client library to the X-Server.Technically speaking like a JavaScript but not like PHP or  JSP that which is runs on the server. What Xlib does is just preprocess the commands from application programs to the X Server and open a communication channel ,encode them using X protocol and send them to the server.X protocol can operate through TCP/IP where the remote machine is located at other side in the world or in the case of local machine it will use the interprocess communication mechanism.No matter what mechanism that it use it won’t violate the basic server client architecture of the windowing system.

So as I already told you Xlib is like JavaScript that is running on your web-browser not in the web server.But the difference is it’s not written with JavaScript,But with C. So it’s actually a C library.In the helder file /usr/include/ X11/ directory you can find the Xlib.h,which is the declares the main functions that you needed to talk with the X-server.By the way the gcc compilers main include path is /usr/include so therefore what we need is to add the following line in order to include the /usr/inlude/X11/Xlib.h.

 

#define <X11/Xlib.h>

 

So then we are going to write the program , below I have listed the program again.

 

#include <stdio.h>
#include <X11/Xlib.h>

void *__gxx_personality_v0 =0;

int main (int argc,char**argv)
{
    // open a display
    Display *dpy;
    Window win;
    Visual *vis ;
    dpy = XOpenDisplay(NULL);
    vis = DefaultVisual(dpy,0);
    win = XCreateWindow( dpy ,
                         DefaultRootWindow(dpy),
                         100,100,
                         500,500,
                         0,
                         CopyFromParent,
                         CopyFromParent,
                         vis,
                         0,
                         NULL);
    XMapWindow(dpy,win);

    // then say what keys to be notified.
    XSelectInput(dpy, win , ExposureMask|KeyPressMask|ButtonPressMask);
    XEvent report;
    // enter the message processing loop
    while(1)
    {
          XNextEvent(dpy,&report);
          switch(report.type)
          {
                case Expose:
                     fprintf(stdout,"Expose Event.\n");
                     break ;
                case KeyPress:
                     fprintf(stdout,"KeyPress Event.\n");
                     break;

                default:
                     break;

          }
    }
    return 0;
}

Well you may be wonder why is this __gxx_personality_v0 ? I include that line here because some it will get a linking problem when linking with old “C” libraries.Actually it’s a patch.If you compile without this and obtains no error, then

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

et cetera
%d bloggers like this: