Thursday, February 24, 2011

Xlib problem solved

Today, I successfully performed my X Window system experiment................

Here is the code........

/**
  * This program will create a window and type a message
  * Author: Prakhar Mishra
  * Date: 24th Feb, 2011
  */

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

int main()
{
    Display *display; //Used to store and refer to the display configuration
    Window win; //Used to refer to the window which is to be displayed at XServer
    XEvent event; //Used to fetch an event from XServer
    int screen; //Used to store screen-number

    display = XOpenDisplay(NULL); //It opens the connection and returns Display *
    screen = DefaultScreen(display); //It returns screen number

    //It creates a simple window with its root window specified with x, y position,
    //width, height, border width, border pixel value and background pixel value
    win = XCreateSimpleWindow(display, RootWindow(display, screen), 0, 0, 400, 400, 1, BlackPixel(display, screen), WhitePixel(display, screen));

    XSelectInput(display, win, ExposureMask | KeyPressMask); //Select for what input you want to make your program react

    XMapWindow(display, win); //It draws window on the screen

    while(1)
    {
        XNextEvent(display, &event); //It fetches next event

        switch(event.type)
        {
            case Expose: //If window is exposed(restored after minimize)
            XDrawString(display, win, DefaultGC(display, screen), 150, 200, "Hello World! I am Prakhar Mishra", strlen("Hello World! I am Prakhar Mishra")); //This draws a string on Window win specified location
            break;
            default:
                exit(0);
        } //Here we have judged type of the input, based on which we make our program react
    }
}

No comments:

Post a Comment