Here today ‘m going to demonstrate you how to write a simple X application using
Xlib X-libraries.
down here is the source code.
#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;
}
And save this as ch01.cc and execute this bellow command to compile this program.
$gcc -o ch01 ch01.cc -lm -L/usr/lib/X11 -lX11
and thereafter to run.
$./ch01
It will simply receive the events KeyPress event and Expose event and just write that it’s exposed
to the stdout by using fprintf.

I’ll willing to write another full page about digging and explaining this program.
–Happy Coding–
Advertisement