Xmsgtrace

Home Features Examples FAQ Download Links
SourceForge.net Logo

Examples
5. Xmsgtrace Recording of a Black Box Drawing Program

This example is an X protocol recording of a small X program that draws a black box in a window. A screenshot of the program output along with the source code and Xmsgtrace log file is shown below. Here's a breakdown of how the source code correlates with the protocol recording:

Screenshot of Example 5 Program Output

Screenshot of example 5

Source Code for Example5.c

/*
 * Example5.c
 */

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

main(argc,argv)
    int argc;
    char **argv;
{
    Display    		*dpy;				/* X server connection */
    Window      	win;				/* Window ID */
    GC          	gc;				/* GC to draw with */
    unsigned long 	white, black;			/* Pixel values */
    unsigned long	background_color;
    unsigned long	border_color;
    XGCValues   	gcv;				/* Struct for creating GC */
    XEvent      	event;				/* Event received */
    int 		x,y;				/* window position */
    unsigned int 	width, height, border_width;

    /*
     * Open the display using the $DISPLAY environment variable to locate
     * the X server.
     */
    if ((dpy = XOpenDisplay(NULL)) == NULL) 
    {
	fprintf(stderr, "%s: can't open %s\n", argv[0], XDisplayName(NULL));
	exit(1);
    }

    /*
     * Get the pixel value for white and black
     */
    white = WhitePixel(dpy, DefaultScreen(dpy));
    black = BlackPixel(dpy, DefaultScreen(dpy));
    
    /*
     *	Create a window to display on the screen
     */
    x=0;
    y=0;
    width=90;
    height=90;
    border_width = 1;
    border_color = white;
    background_color = white;
    win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy),
			      x,y,width,height,
			      border_width,  border_color, background_color);

    /*
     * Create the GC for the rectangle.
     */
    gcv.foreground = black;
    gcv.background = white;
    gc = XCreateGC(dpy, win, (GCForeground | GCBackground), &gcv);

    /*
     * Specify the event types we're interested in - only Exposures.
     */
    XSelectInput(dpy, win, ExposureMask);
    		
    /*
     * Map the window to make it visible on the screen
     */
    XMapWindow(dpy, win);

    /*
     * Loop forever,  examining each event.
     */
    while (1) 
    {
	/*
	 * Get the next event
	 */
	XNextEvent(dpy, &event);

	/*
	 * On the last of each group of Expose events,  repaint the entire
	 * window.
	 */
	if (event.type == Expose && event.xexpose.count == 0) 
	{
	    /*
	     * Remove any other pending Expose events from the queue to
	     * avoid multiple repaints.
	     */
	    while (XCheckTypedEvent(dpy, Expose, &event));

	    
	    // Paint the window with the window background color
	    XClearWindow(dpy, win);

	    /*
	     * Fill the center of the window which has a white background
	     * with a black box
	     */
	    XFillRectangle(dpy, win, gc, 30, 30, 30, 30);
	}
    }

    exit(1);
}


Xmsgtrace Recording of Example 5

1 ##########################################################################################
2 xmsgtrace.log (version 1.0)
3 Mon Jan  7 12:47:44 2002
4 
5 XRecordStartOfData
6  
7 ***************Client=18, Name=Unknown**************************************************************************************************
8 >>>>> Client 18 Connection Setup Reply: swapped=FALSE, protocol-major-version=11, protocol-minor-version=0, resource-id-base=0x2400000
9 		release_number=4003, resource-id-mask=0x1FFFFF
10 Request(CreateGC[55]): client=18, seq=1, cid=0x2400000, drawable=0x2B, value-mask=0x8
11  background:0xFFFF
12 Request(QueryExtension[98]): client=18, seq=2, name=BIG-REQUESTS
13  Reply(QueryExtension[98]): client=18, seq=2, present=TRUE, major_opcode=134, first_event=0, first_error=0
14 Request(GetProperty[20]): client=18, seq=3, delete==0, Window=0x2B, property=23, type=31, long-offset=0, long-offset=100000000
15  Reply(GetProperty[20]): client=18, seq=3, format=0, atom=0(0x0), bytes-after=0, format_units=0
16 Request(BIG-REQUESTS extension): client=18, seq=4
17  Reply(BIG-REQUESTS extension): client=18, seq=4, max_request_length(in 4-byte units)=1048575
18 Request(InternAtom[16]): client=18, seq=5, only-if-exists=FALSE, name=Compose
19  Reply(InternAtom[16]): seq=5, atom=198(0xC6)
20 Request(QueryExtension[98]): client=18, seq=6, name=XKEYBOARD
21  Reply(QueryExtension[98]): client=18, seq=6, present=TRUE, major_opcode=140, first_event=92, first_error=161
22 Request(CreateWindow[1]): client=18, seq=8, depth=0, windowid=37748737(0x2400001), parent=43(0x2B),
23    x=0, y=0, w=90, h=90, border_width=1,
24    window_class=CopyFromParent, visual_id=0(0x0), value-mask=0xA,
25    background-pixel=0xFFFF, border-pixel=0xFFFF
26 Request(CreateGC[55]): client=18, seq=9, cid=0x2400002, drawable=0x2400001, value-mask=0x8
27  background:0xFFFF
28 Request(ChangeWindowAttributes[2]): client=18 seq=10 windowid=0x2400001 value-mask=0x800,
29    event-mask=0x8000: (ExposureMask)
30 Request(MapWindow[8]): client=18, seq=11, windowid=0x2400001
31 Event(Expose[12]): client=18, seq=11, window=37748737(0x2400001), x=0, y=0, w=90, h=71, count=1
32 Event(Expose[12]): client=18, seq=11, window=37748737(0x2400001), x=19, y=71, w=52, h=19, count=0
33 Request(ClearArea[61]): client=18, seq=12, exposures=FALSE, window=37748737(0x2400001), x=0, y=0, width=0, height=0
34 Request(PolyFillRectangle[70]): client=18, seq=13, drawable=0x2400001, gcID=0x2400002, number_of_rectangles=1
35   x=30, y=30, width=30, height=30
36 Event(Expose[12]): client=18, seq=13, window=37748737(0x2400001), x=71, y=71, w=19, h=19, count=0
37 Event(Expose[12]): client=18, seq=13, window=37748737(0x2400001), x=0, y=71, w=19, h=19, count=0
38 Request(ClearArea[61]): client=18, seq=14, exposures=FALSE, window=37748737(0x2400001), x=0, y=0, width=0, height=0
39 Request(PolyFillRectangle[70]): client=18, seq=15, drawable=0x2400001, gcID=0x2400002, number_of_rectangles=1
40   x=30, y=30, width=30, height=30
41 >>>>> Client 18 died
42 XRecordEndOfData