Log   |   Assignments   |   Source   |   Discussion   |   Feedback   |   About Me  |

This page lists the first version (v0.1) of the Mouse Pointer Tracker.

Main File
Windowing Functions

Main File

/*
 * Advanced Computer Architecture
 * Assignment 
 * Read the mouse pointer position using Xlib functions and 
 * plot them in real time. The objective is to show that the
 * data from the mouse is of aribtrary length and so fast
 * that the CPU cannot faithfully capture all the points in
 * a quick move
 *
 * Author: Kurian John (CS10M035)
 *
 * Revision History
 * ----------------
 * 2011-03-14 - New program
 *
 */

#include "windowHelpers.h"

int main(void)
{
	Window theWindow;
	XEvent event;
	GC theGC;

	theDisplay = XOpenDisplay(NULL);
	if(theDisplay == NULL)
	{
		fprintf(stderr,"Error::Given display cannot be opened");
		return -1;
	}

	theScreen = DefaultScreen(theDisplay);
	theDepth = DefaultDepth(theDisplay,theScreen);

	// Use macros to get the screen dimensions
	unsigned int dWidth = DisplayWidth(theDisplay, theScreen);
	unsigned int dHeight = DisplayHeight(theDisplay, theScreen);

	theWindow = OpenWindow(0,0,dWidth,dHeight,0,&theGC);
	XDrawString (theDisplay, theWindow, theGC, dWidth / 2 - 10, 10, "Mouse Pointer Tracer", 20); 
	XDrawString (theDisplay, theWindow, theGC, 
			dWidth / 2 - 45, dHeight-20, "Left click and drag to draw. Right click to exit...", 50); 

	while (1)
	{
		// Get the pointer position
		XQueryPointer(theDisplay, RootWindow(theDisplay, DefaultScreen(theDisplay)),
				&event.xbutton.root, &event.xbutton.window,
				&event.xbutton.x_root, &event.xbutton.y_root,
				&event.xbutton.x, &event.xbutton.y,
				&event.xbutton.state);
		// Plot the position when the left mouse button is pressed
		if (event.xbutton.state == 256)
			XDrawPoint (theDisplay, theWindow, theGC, event.xbutton.x, event.xbutton.y);

		// Exit if the right mouse button was pressed
		if  (event.xbutton.state == 1024) break;

	}

	XCloseDisplay(theDisplay);
	return 0;
}

Windowing Functions

/*
 * Advanced Computer Architecture
 * Assignment 
 * Read the mouse pointer position using Xlib functions and 
 * plot them in real time. The objective is to show that the
 * data from the mouse is of aribtrary length and so fast
 * that the CPU cannot faithfully capture all the points in
 * a quick move
 * 
 * This file contains the window creation functions
 *
 * Author: Kurian John (CS10M035)
 *
 * Revision History
 * ----------------
 * 2011-03-14 - New program
 *
 */

#include "windowHelpers.h"

int createGC(Window theNewWindow,GC *theNewGC)
{
        XGCValues theGCValues;


        *theNewGC = XCreateGC(theDisplay,theNewWindow,(unsigned long) 0,&theGCValues);
        if(*theNewGC == 0)
	{
                return 0;
	}
        else
	{
                XSetBackground(theDisplay,*theNewGC,theWhitePixel);
                XSetForeground(theDisplay,*theNewGC,theBlackPixel);
                return 1;
        }
}



Window OpenWindow(int x, int y, int width, int height, int flag, GC* theNewGC)
{
        XSetWindowAttributes theWindowAttributes;
        unsigned long theWindowMask;
        Window theNewWindow;


        /*Setting the attributes*/
        theWindowAttributes.border_pixel
                =BlackPixel(theDisplay,theScreen);
        theWindowAttributes.background_pixel
                = WhitePixel(theDisplay,theScreen);
        theWindowAttributes.override_redirect = True;



        theWindowMask = CWBackPixel|CWBorderPixel|CWOverrideRedirect;


        theNewWindow = XCreateWindow( theDisplay,
                        RootWindow(theDisplay,theScreen),
                        x,y,width,height,
                        BORDER_WIDTH,theDepth,
                        InputOutput,
                        CopyFromParent,
                        theWindowMask,
                        &theWindowAttributes);

	if( createGC(theNewWindow,theNewGC) == 0)
	{
		XDestroyWindow(theDisplay,theNewWindow);
		return( (Window) 0);
	}


	XMapWindow(theDisplay,theNewWindow);
	XFlush(theDisplay);


	return theNewWindow;
}