Add basic XTest functionality, clean up and refactor other sources

This commit is contained in:
Bradlee Speice
2012-06-26 14:07:03 -04:00
parent 62431b5a18
commit ca39bd90be
10 changed files with 699 additions and 44 deletions

View File

@ -4,8 +4,8 @@ AM_LIBS = -shared
#Lists the libraries to build
lib_LTLIBRARIES = libcvautomation.la
libcvautomation_la_SOURCES = libcvautomation-opencv.c libcvautomation-opencv.h libcvautomation-x11.c libcvautomation-x11.h libcvautomation.h
libcvautomation_la_SOURCES = libcvautomation-opencv.c libcvautomation-opencv.h libcvautomation-xlib.c libcvautomation-xlib.h libcvautomation.h libcvautomation-xtest.c libcvautomation-xtest.h
#libcvautomation_CFLAGS = -shared -I$(top_srcdir)/include -I$(top_srcdir)/libcvautomation -I$(top_srcdir)/include/libcvautomation
#Lists the headers to distribute
pkginclude_HEADERS = $(top_srcdir)/include/libcvautomation/libcvautomation.h $(top_srcdir)/include/libcvautomation/libcvautomation-opencv.h $(top_srcdir)/include/libcvautomation/libcvautomation-x11.h
pkginclude_HEADERS = $(top_srcdir)/include/libcvautomation/libcvautomation.h $(top_srcdir)/include/libcvautomation/libcvautomation-opencv.h $(top_srcdir)/include/libcvautomation/libcvautomation-xlib.h $(top_srcdir)/include/libcvautomation/libcvautomation-xtest.h

View File

@ -1,7 +1,7 @@
/*
* =====================================================================================
*
* Filename: libcvautomation-x11.c
* Filename: libcvautomation-xlib.c
*
* Description:
*
@ -16,7 +16,30 @@
* =====================================================================================
*/
#include <libcvautomation/libcvautomation-x11.h>
#include <libcvautomation/libcvautomation-xlib.h>
/*
* === FUNCTION ======================================================================
* Name: cvaOpenDisplay
* Description: Custom wrapper for XOpenDisplay function
* =====================================================================================
*/
Display* cvaOpenDisplay ( char *displayName )
{
return XOpenDisplay( displayName);
}
/*
* === FUNCTION ======================================================================
* Name: cvaCloseDisplay
* Description: Custom wrapper for XOpenDisplay function
* =====================================================================================
*/
void cvaCloseDisplay ( Display *displayLocation )
{
XCloseDisplay( displayLocation );
}
/*
* === FUNCTION ======================================================================
@ -24,7 +47,7 @@
* Description: Match a sub image using the X11 root window as root
* =====================================================================================
*/
CvPoint matchSubImage_X11( char *displayLocation, IplImage *subImage, int searchMethod, int tolerance )
CvPoint matchSubImage_X11( Display *displayLocation, IplImage *subImage, int searchMethod, int tolerance )
{
/* First things first, grab the root X window and convert it to
* the IplImage format.
@ -36,7 +59,6 @@ CvPoint matchSubImage_X11( char *displayLocation, IplImage *subImage, int search
XImage *rootImage;
XColor color;
Display *display;
Screen *screen;
unsigned long rmask, gmask, bmask;
unsigned long rshift, rbits,
@ -48,18 +70,17 @@ CvPoint matchSubImage_X11( char *displayLocation, IplImage *subImage, int search
unsigned int width, height;
/* Setting up the X screengrab is the first order of business */
display = XOpenDisplay(displayLocation);
screen = DefaultScreenOfDisplay(display);
screen = DefaultScreenOfDisplay(displayLocation);
width = screen->width;
height = screen->height;
rootImage = XGetImage( display, DefaultRootWindow(display),
rootImage = XGetImage( displayLocation, DefaultRootWindow(displayLocation),
startX, startY, width, height,
AllPlanes, ZPixmap );
/* Make sure that we're good to go before going any farther */
if ( rootImage == NULL || display == NULL || screen == NULL )
if ( rootImage == NULL || displayLocation == NULL || screen == NULL )
{
fprintf( stderr, "Couldn't grab the root window!" );
resultPoint.x = -1;
@ -144,11 +165,11 @@ CvPoint matchSubImage_X11( char *displayLocation, IplImage *subImage, int search
/* I don't know how this works either. */
/* TODO: Figure out how this code works and document it */
Colormap colormap;
colormap = DefaultColormap( display, DefaultScreen ( display ));
colormap = DefaultColormap( displayLocation, DefaultScreen( displayLocation ));
for ( x = 0; x < rootImage->width; x++ ) {
for ( y = 0; y < rootImage->height; y++ ) {
color.pixel = XGetPixel ( rootImage, x, y );
XQueryColor( display, colormap, &color );
XQueryColor( displayLocation, colormap, &color );
CV_IMAGE_ELEM(X_IPL, uchar, y, x * X_IPL->nChannels) = color.blue;
CV_IMAGE_ELEM(X_IPL, uchar, y, x * X_IPL->nChannels + 1) = color.green;
CV_IMAGE_ELEM(X_IPL, uchar, y, x * X_IPL->nChannels + 2) = color.red;
@ -164,7 +185,6 @@ CvPoint matchSubImage_X11( char *displayLocation, IplImage *subImage, int search
/* Clean up the CV image we created, as well as all X resources */
XDestroyImage( rootImage );
XCloseDisplay( display );
cvReleaseImage( &X_IPL );
/* Return and be done */
@ -178,7 +198,7 @@ CvPoint matchSubImage_X11( char *displayLocation, IplImage *subImage, int search
* Description: Match a sub image using the X11 root window as root, from filename
* =====================================================================================
*/
CvPoint matchSubImage_X11_location( char *displayLocation, const char *subImage_location, int searchMethod, int tolerance )
CvPoint matchSubImage_X11_location( Display *displayLocation, const char *subImage_location, int searchMethod, int tolerance )
{
/* This is basically a wrapper for matchSubImage_X11( char *display, IplImage )
* All we do is load the sub-image from the given filename, and then
@ -218,7 +238,7 @@ CvPoint matchSubImage_X11_location( char *displayLocation, const char *subImage_
* the top-left corner.
* =====================================================================================
*/
CvPoint matchSubImage_X11_center( char *displayLocation, IplImage *subImage, int searchMethod, int tolerance )
CvPoint matchSubImage_X11_center( Display *displayLocation, IplImage *subImage, int searchMethod, int tolerance )
{
/* First things first, grab the root X window and convert it to
* the IplImage format.
@ -230,7 +250,6 @@ CvPoint matchSubImage_X11_center( char *displayLocation, IplImage *subImage, int
XImage *rootImage;
XColor color;
Display *display;
Screen *screen;
unsigned long rmask, gmask, bmask;
unsigned long rshift, rbits,
@ -242,18 +261,17 @@ CvPoint matchSubImage_X11_center( char *displayLocation, IplImage *subImage, int
unsigned int width, height;
/* Setting up the X screengrab is the first order of business */
display = XOpenDisplay(displayLocation);
screen = DefaultScreenOfDisplay(display);
screen = DefaultScreenOfDisplay(displayLocation);
width = screen->width;
height = screen->height;
rootImage = XGetImage( display, DefaultRootWindow(display),
rootImage = XGetImage( displayLocation, DefaultRootWindow(displayLocation),
startX, startY, width, height,
AllPlanes, ZPixmap );
/* Make sure that we're good to go before going any farther */
if ( rootImage == NULL || display == NULL || screen == NULL )
if ( rootImage == NULL || displayLocation == NULL || screen == NULL )
{
fprintf( stderr, "Couldn't grab the root window!" );
resultPoint.x = -1;
@ -338,11 +356,11 @@ CvPoint matchSubImage_X11_center( char *displayLocation, IplImage *subImage, int
/* I don't know how this works either. */
/* TODO: Figure out how this code works and document it */
Colormap colormap;
colormap = DefaultColormap( display, DefaultScreen ( display ));
colormap = DefaultColormap( displayLocation, DefaultScreen( displayLocation ));
for ( x = 0; x < rootImage->width; x++ ) {
for ( y = 0; y < rootImage->height; y++ ) {
color.pixel = XGetPixel ( rootImage, x, y );
XQueryColor( display, colormap, &color );
XQueryColor( displayLocation, colormap, &color );
CV_IMAGE_ELEM(X_IPL, uchar, y, x * X_IPL->nChannels) = color.blue;
CV_IMAGE_ELEM(X_IPL, uchar, y, x * X_IPL->nChannels + 1) = color.green;
CV_IMAGE_ELEM(X_IPL, uchar, y, x * X_IPL->nChannels + 2) = color.red;
@ -354,11 +372,10 @@ CvPoint matchSubImage_X11_center( char *displayLocation, IplImage *subImage, int
* However, we don't want to do any more work than we have to - send our images off
* to matchSubImage in libopencvautomation-opencv. */
resultPoint = matchSubImage_center ( X_IPL, subImage, searchMethod, tolerance );
resultPoint = matchSubImage_center( X_IPL, subImage, searchMethod, tolerance );
/* Clean up the CV image we created, as well as all X resources */
XDestroyImage( rootImage );
XCloseDisplay( display );
cvReleaseImage( &X_IPL );
/* Return and be done */
@ -372,7 +389,7 @@ CvPoint matchSubImage_X11_center( char *displayLocation, IplImage *subImage, int
* Description: Match a sub image using the X11 root window as root, from filename
* =====================================================================================
*/
CvPoint matchSubImage_X11_location_center( char *displayLocation, const char *subImage_location, int searchMethod, int tolerance )
CvPoint matchSubImage_X11_location_center( Display *displayLocation, const char *subImage_location, int searchMethod, int tolerance )
{
/* This is basically a wrapper for matchSubImage_X11( char *display, IplImage )
* All we do is load the sub-image from the given filename, and then

View File

@ -0,0 +1,417 @@
/*
* =====================================================================================
*
* Filename: libcvautomation-xinput.c
*
* Description:
*
* Version: 1.0
* Created: 06/26/2012 09:09:05 AM
* Revision: none
* Compiler: gcc
*
* Author: Bradlee Speice (), bspeice.nc@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <libcvautomation/libcvautomation-xtest.h>
/*
* === FUNCTION ======================================================================
* Name: xte_xTestSupported
* Description: Make sure that the XTest extension is supported.
* If it's not, return 0 (false)
* =====================================================================================
*/
int xte_XTestSupported ( Display *displayLocation )
{
int event_basep, error_basep, majorp, minorp;
return XTestQueryExtension( displayLocation, &event_basep, &error_basep, &majorp, &minorp );
}
/*
* === FUNCTION ======================================================================
* Name: xte_pointerLocation
* Description: Return the current location of the pointer
* =====================================================================================
*/
cvaPoint xte_pointerLocation ( Display *displayLocation )
{
/* The next few variables we discard, but need them for the function call */
Window root, child;
int rootX, rootY;
unsigned int mask_return;
/* End the discarded variables. We're using the ones that follow */
int pointerX, pointerY;
XQueryPointer( displayLocation, DefaultRootWindow( displayLocation ),
&root, &child, &rootX, &rootY,
/* And then the stuff we actually care about */
&pointerX, &pointerY,
/* And more stuff we don't */
&mask_return );
cvaPoint returnPoint;
returnPoint.x = pointerX;
returnPoint.y = pointerY;
return returnPoint;
}
/*
* === FUNCTION ======================================================================
* Name: xte_clickMouse
* Description: Click the mouse where it is currently at
* =====================================================================================
*/
void xte_clickMouse ( Display *displayLocation, int mouseButton )
{
XTestFakeButtonEvent( displayLocation, mouseButton, 1, CurrentTime );
XTestFakeButtonEvent( displayLocation, mouseButton, 0, CurrentTime );
}
/*
* === FUNCTION ======================================================================
* Name: xte_clickMouseXY
* Description: Click the mouse on a screen location
* =====================================================================================
*/
void xte_clickMouseXY ( Display *displayLocation, int xLocation, int yLocation, int mouseButton )
{
/* To avoid specifying the screen number we're using, calculate the pointer's
* current location, and move to the new wanted location using relative motion */
cvaPoint pointerLocation = xte_pointerLocation( displayLocation );
int xIncrement, yIncrement;
xIncrement = xLocation - pointerLocation.x;
yIncrement = yLocation - pointerLocation.y;
XTestFakeRelativeMotionEvent( displayLocation, xIncrement, yIncrement, CurrentTime );
XTestFakeButtonEvent( displayLocation, mouseButton, 1, CurrentTime );
XTestFakeButtonEvent( displayLocation, mouseButton, 0, CurrentTime );
}
/*
* === FUNCTION ======================================================================
* Name: xte_clickMouseRXY
* Description: Click the mouse on a screen location relative to where it currently is
* =====================================================================================
*/
void xte_clickMouseRXY ( Display *displayLocation, int xIncrement, int yIncrement, int mouseButton )
{
XTestFakeRelativeMotionEvent( displayLocation, xIncrement, yIncrement, CurrentTime );
XTestFakeButtonEvent( displayLocation, mouseButton, 1, CurrentTime );
XTestFakeButtonEvent( displayLocation, mouseButton, 0, CurrentTime );
}
/*
* === FUNCTION ======================================================================
* Name: xte_clickMouseImage
* Description: Click the mouse at the top-left corner of an image on the specified display
* =====================================================================================
*/
void xte_clickMouseImage ( Display *displayLocation, IplImage *subImage, int mouseButton, int searchMethod, int tolerance )
{
/* This one is a bit more in-depth -
* Find where the image is at
* Find where the cursor is currently at
* Move the cursor to the given point using relative motion -
* This method is screen-agnostic */
CvPoint resultPoint;
resultPoint = matchSubImage_X11 ( displayLocation, subImage, searchMethod, tolerance );
cvaPoint pointerLocation;
pointerLocation = xte_pointerLocation( displayLocation );
int movementX, movementY;
movementX = resultPoint.x - pointerLocation.x;
movementY = resultPoint.y - pointerLocation.y;
XTestFakeRelativeMotionEvent( displayLocation, movementX, movementY, CurrentTime );
XTestFakeButtonEvent( displayLocation, mouseButton, 1, CurrentTime );
XTestFakeButtonEvent( displayLocation, mouseButton, 0, CurrentTime );
}
/*
* === FUNCTION ======================================================================
* Name: xte_clickMouseImage_location
* Description: Click the mouse at the top-left corner of an image on the specified display
* where the subImage is a file location
* =====================================================================================
*/
void xte_clickMouseImage_location ( Display *displayLocation, const char *fileName, int mouseButton, int searchMethod, int tolerance )
{
CvPoint resultPoint;
resultPoint = matchSubImage_X11_location( displayLocation, fileName, searchMethod, tolerance );
cvaPoint pointerLocation;
pointerLocation = xte_pointerLocation( displayLocation );
int movementX, movementY;
movementX = resultPoint.x - pointerLocation.x;
movementY = resultPoint.y - pointerLocation.y;
XTestFakeRelativeMotionEvent( displayLocation, movementX, movementY, CurrentTime );
XTestFakeButtonEvent( displayLocation, mouseButton, 1, CurrentTime );
XTestFakeButtonEvent( displayLocation, mouseButton, 0, CurrentTime );
}
/*
* === FUNCTION ======================================================================
* Name: xte_clickMouseImage_center
* Description: Click the mouse at the center of an image on the specified display
* =====================================================================================
*/
void xte_clickMouseImage_center ( Display *displayLocation, IplImage *subImage, int mouseButton, int searchMethod, int tolerance )
{
/* This one is a bit more in-depth -
* Find where the image is at
* Find where the cursor is currently at
* Move the cursor to the given point using relative motion -
* This method is screen-agnostic */
CvPoint resultPoint;
resultPoint = matchSubImage_X11_center ( displayLocation, subImage, searchMethod, tolerance );
cvaPoint pointerLocation;
pointerLocation = xte_pointerLocation( displayLocation );
int movementX, movementY;
movementX = resultPoint.x - pointerLocation.x;
movementY = resultPoint.y - pointerLocation.y;
XTestFakeRelativeMotionEvent( displayLocation, movementX, movementY, CurrentTime );
XTestFakeButtonEvent( displayLocation, mouseButton, 1, CurrentTime );
XTestFakeButtonEvent( displayLocation, mouseButton, 0, CurrentTime );
}
/*
* === FUNCTION ======================================================================
* Name: xte_clickMouseImage_location_center
* Description: Click the mouse at the center of an image on the specified display
* where the subImage is a file location
* =====================================================================================
*/
void xte_clickMouseImage_location_center ( Display *displayLocation, const char *fileName, int mouseButton, int searchMethod, int tolerance )
{
CvPoint resultPoint;
resultPoint = matchSubImage_X11_location_center( displayLocation, fileName, searchMethod, tolerance );
cvaPoint pointerLocation;
pointerLocation = xte_pointerLocation( displayLocation );
int movementX, movementY;
movementX = resultPoint.x - pointerLocation.x;
movementY = resultPoint.y - pointerLocation.y;
XTestFakeRelativeMotionEvent( displayLocation, movementX, movementY, CurrentTime );
XTestFakeButtonEvent( displayLocation, mouseButton, 1, CurrentTime );
XTestFakeButtonEvent( displayLocation, mouseButton, 0, CurrentTime );
}
/*
* === FUNCTION ======================================================================
* Name: xte_hoverMouseXY
* Description: Move the mouse to a location and leave it there
* =====================================================================================
*/
void xte_hoverMouseXY ( Display *displayLocation, int xLocation, int yLocation )
{
cvaPoint pointerLocation;
pointerLocation = xte_pointerLocation( displayLocation );
int xIncrement, yIncrement;
xIncrement = xLocation - pointerLocation.x;
yIncrement = yLocation - pointerLocation.y;
XTestFakeRelativeMotionEvent( displayLocation, xIncrement, yIncrement, CurrentTime );
}
/*
* === FUNCTION ======================================================================
* Name: xte_hoverMouseRXY
* Description: Move the mouse to a location relative to where it currently is
* and leave it there
* =====================================================================================
*/
void xte_hoverMouseRXY ( Display *displayLocation, int xIncrement, int yIncrement )
{
XTestFakeRelativeMotionEvent( displayLocation, xIncrement, yIncrement, CurrentTime );
}
/*
* === FUNCTION ======================================================================
* Name: xte_hoverMouseMouseImage
* Description: Move the mouse to a location at the top-left corner of an image on
* the specified display but don't click the mouse
* =====================================================================================
*/
void xte_hoverMouseImage ( Display *displayLocation, IplImage *subImage, int searchMethod, int tolerance )
{
CvPoint resultPoint;
resultPoint = matchSubImage_X11( displayLocation, subImage, searchMethod, tolerance );
cvaPoint pointerLocation;
pointerLocation = xte_pointerLocation( displayLocation );
int xIncrement, yIncrement;
xIncrement = resultPoint.x - pointerLocation.x;
yIncrement = resultPoint.y - pointerLocation.y;
XTestFakeRelativeMotionEvent( displayLocation, xIncrement, yIncrement, CurrentTime );
}
/*
* === FUNCTION ======================================================================
* Name: xte_hoverMouseMouseImage_location
* Description: Move the mouse to a location at the top-left corner of an image from
* file on the specified display but don't click the mouse
* =====================================================================================
*/
void xte_hoverMouseImage_location ( Display *displayLocation, const char *fileName, int searchMethod, int tolerance )
{
CvPoint resultPoint;
resultPoint = matchSubImage_X11_location( displayLocation, fileName, searchMethod, tolerance );
cvaPoint pointerLocation;
pointerLocation = xte_pointerLocation( displayLocation );
int xIncrement, yIncrement;
xIncrement = resultPoint.x - pointerLocation.x;
yIncrement = resultPoint.y - pointerLocation.y;
XTestFakeRelativeMotionEvent( displayLocation, xIncrement, yIncrement, CurrentTime );
}
/*
* === FUNCTION ======================================================================
* Name: xte_hoverMouseMouseImage_center
* Description: Move the mouse to a location at the center of an image on the
* specified display but don't click the mouse
* =====================================================================================
*/
void xte_hoverMouseImage_center ( Display *displayLocation, IplImage *subImage, int searchMethod, int tolerance )
{
CvPoint resultPoint;
resultPoint = matchSubImage_X11_center( displayLocation, subImage, searchMethod, tolerance );
cvaPoint pointerLocation;
pointerLocation = xte_pointerLocation( displayLocation );
int xIncrement, yIncrement;
xIncrement = resultPoint.x - pointerLocation.x;
yIncrement = resultPoint.y - pointerLocation.y;
XTestFakeRelativeMotionEvent( displayLocation, xIncrement, yIncrement, CurrentTime );
}
/*
* === FUNCTION ======================================================================
* Name: xte_hoverMouseMouseImage_location_center
* Description: Move the mouse to a location at the center of an image from file on
* the specified display but don't click the mouse
* =====================================================================================
*/
void xte_hoverMouseImage_location_center ( Display *displayLocation, const char *fileName, int searchMethod, int tolerance )
{
CvPoint resultPoint;
resultPoint = matchSubImage_X11_location_center( displayLocation, fileName, searchMethod, tolerance );
cvaPoint pointerLocation;
pointerLocation = xte_pointerLocation( displayLocation );
int xIncrement, yIncrement;
xIncrement = resultPoint.x - pointerLocation.x;
yIncrement = resultPoint.y - pointerLocation.y;
XTestFakeRelativeMotionEvent( displayLocation, xIncrement, yIncrement, CurrentTime );
}
/*
* === FUNCTION ======================================================================
* Name: xte_mouseDown
* Description: Push a mouse button down, but don't release it
* =====================================================================================
*/
void xte_mouseDown ( Display *displayLocation, int mouseButton )
{
XTestFakeButtonEvent( displayLocation, mouseButton, 1, CurrentTime );
}
/*
* === FUNCTION ======================================================================
* Name: xte_mouseUp
* Description: Let a mouse button up
* =====================================================================================
*/
void xte_mouseUp ( Display *displayLocation, int mouseButton )
{
XTestFakeButtonEvent( displayLocation, mouseButton, 0, CurrentTime );
}
/*
* === FUNCTION ======================================================================
* Name: xte_mouseJiggle
* Description: Move the mouse a little bit
* =====================================================================================
*/
void xte_mouseJiggle ( Display *displayLocation )
{
XTestFakeRelativeMotionEvent( displayLocation, 1, 1, CurrentTime );
XTestFakeRelativeMotionEvent( displayLocation, -1, -1, CurrentTime );
}
/*
* === FUNCTION ======================================================================
* Name: xte_clickKey
* Description: Press and release a single key
* =====================================================================================
*/
void xte_clickKey ( Display *displayLocation, char key )
{
}
/*
* === FUNCTION ======================================================================
* Name: xte_clickKeyStr
* Description: Press and release keys in a string
* =====================================================================================
*/
void xte_clickKeyStr ( Display *displayLocation, const char *string )
{
}
/*
* === FUNCTION ======================================================================
* Name: xte_keyDown
* Description: Press a key down
* =====================================================================================
*/
void xte_keyDown ( Display *displayLocation, char key )
{
}
/*
* === FUNCTION ======================================================================
* Name: xte_keyUp
* Description: Release a key
* =====================================================================================
*/
void xte_keyUp ( Display *displayLocation, char key )
{
}