X lib Programming

(lecture programs)

Ø  X server:  Controls the Input/Output Resources of a host:

                        Display, Keyboard and Mouse.

Ø  X clients: Applications that runs at any host in the Internet:

May be different from  the X server's host.  

Ø  TCP/IP:  Is used for communications between the clients and the server:

The default port# for the X server is 6000.

Running X clients on remote hosts

To run a X client (e.g., xterm) on a remote Unix host (e.g., cash.cs.odu.edu) and display the interface on your local window machine (e.g., localhost.cs.odu.edu):

Step 0:  run the X server on your local window machine.

Then, You may do one of the following two alternatives:

Alternative 1:

Ø   Use ssh to login to cash.cs.odu.edu

Ø    % setenv DISPLAY localhost.cs.odu.edu:0

Ø    % xterm

Alternative 2:

Ø   start ssh

Ø   click on Settings button (3rd from last)

Ø   check on Request tunnels only option and click OK, then click on that window and login to the remote host.

Ø   click on New Terminal Window button (5th from last) to get a new window and type: xterm

______________________________________________

Examples of X lib Programs

Drawing Points: xpoints.c


main(argc,argv)
int argc;
char **argv;
{
    Display *display;
    Window root, window;
    long fgcolor, bgcolor;
    int screen, pointx, pointy;
   
long eventmaskButtonPressMask | ExposureMask | KeyPressMask;
    XEvent event;
    XGCValues gcval;
    GC draw;
    Colormap cmap;
    XColor color, ignore;
    char *colorname = "red";

 

The above are definitions that will be used throughout the program.

          

________________________________  

 

 if (!(display = XOpenDisplay (argv[1]))) {
        perror("XOpenDisplay");
        exit(1);
  }

 

Ø   Opens a TCP connection to an X server running at the host specified by argv[1].

Ø      If argv[1] is NULL, it contacts the server running at the local machine.

Ø      The format for argv[1] is: host:0

 

 

Examples: 128.82.4.67:0
                   isis.cs.odu.edu:0
                   localhost:0  (same as NULL).

_________________________________  

           
    root = RootWindow (display, screen = DefaultScreen(display));

Ø      Creates a root window.

Ø      In X every window must have a parent and this root is the parent of all other windows.

_________________________________  

   fgcolor =  BlackPixel (display,screen);
  bgcolor =  WhitePixel (display,screen);

Ø      Obtains the pixel values for the black and white colors.

_________________________________  

   window = XCreateSimpleWindow (display, root, 0,0, 200,200, 2,

                                      fgcolor, bgcolor);

Ø      Creates the application main window on display as child for root at position 0,0.

Ø      The window size is 200x200 with border of 2 pixels.

Ø      The window's foreground color is black and its background color is white.

_________________________________  

        char *colorname = "red";

    cmap = DefaultColormap (display, screen);
    XAllocNamedColor (display, cmap, colorname, &color, &ignore);
    fgcolor = color.pixel;
    gcval.foreground = fgcolor;
    gcval.background = bgcolor;
    draw = XCreateGC (display,window,GCForeground|GCBackground,&gcval);

Ø   The above statements are used to create a "red" pen called draw
_________________________________  

   XSelectInput (display, window, eventmask);

Ø      Ask the server to report the events specified by eventmask
_________________________________  

    XMapWindow (display,window);

Ø      Make the window visible on the screen.

_________________________________  

The following loop monitors and process the events sent by the X server

    for (;;) {
        XWindowEvent (display, window, eventmask, &event);

Ø      This is a "blocking" call, i.e., the program will stop here until
an event arrives from the X server.

_________________________________  

       

   switch (event.type) {


       case Expose:


            XClearWindow (display,window);
            break;

Ø      Whenever an Expose event arrives, the window is cleared,
An expose event can be generated by  e.g., covering and uncovering the window,  closing and opening the window.
_________________________________  

          case ButtonPress:


            XDrawPoint (display, window, draw,

                     event.xbutton.x ,event.xbutton.y);
            break;

Ø   Whenever any Button is Pressed a  red point  is drawn at the x,y position where the event occurred.

    _________________________________  

     case KeyPress:
            exit(0);


Ø      Whenever any Key is pressed the program exits.

          default:
            fprintf(stderr,"Unexpected event: %d\n",event.type);
         

Ø    Any other event is unexpected and should not happen. 

 

___________________________________________

Drawing Circles


The program  xcircles.c is similar to  xpoints.c  but it draws  filled circles.

Here is the code  that achieve that:

....
int radious = 6;
.....

case ButtonPress:
     
      pointx = event.xbutton.x - radious;
      pointy = event.xbutton.y - radious;
      
XFillArc(display, window, draw,  pointx, pointy,

                        2*radious, 2*radious,0, 360*64);
      break;

 

________________________________________

 

Drawing Lines


The program  xlines.c is similar to  xpoints.c  but it draws lines.

The user odd clicks (1, 3, ...) draws a point while the even clicks (2, 4, ...) draws lines between the current position and the previous position of the mouse.

Here is the code that achieves that:

case ButtonPress:
      if (FirstPt) {
        FirstPt=FALSE;
        pointx = event.xbutton.x;
        pointy = event.xbutton.y;
        XDrawPoint (display,window,draw, pointx, pointy);
        break;
  

        }
      else {
       FirstPt=TRUE;
       XDrawLine (display,window,draw, pointx,pointy,

                     event.xbutton.x, event.xbutton.y);
       break;

Ø    Odd clicks draw a point while even clicks draw a line between the previous mouse position and the current position.