here goes my cpp and h files. ive changed a bit both of them. using CGEventCreateMouseEvent instead of CGPostMouseEvent but still having hangs
cpp:
#include "testApp.h"
#define SCROLL_AMT 10
void testApp::setup() {
ofSetFrameRate( 60 );
cwidth = 320;
cheight = 240;
threshold = 60;
lowRange = 0;
highRange = 255;
mouseon=false;
screenw=ofGetScreenWidth();
screenh=ofGetScreenHeight();
bLearnBakground = true;
TUIOSocket.setup( HOST, PORT ); // Set in Header
vidGrabber.initGrabber( cwidth, cheight );
colorImg.allocate( cwidth, cheight );
grayImg.allocate( cwidth, cheight );
bgImg.allocate( cwidth, cheight );
blobTracker.setListener( this );
alivecounter=0;
}
void testApp::update() {
ofBackground( 100, 100, 100 );
vidGrabber.grabFrame();
if( vidGrabber.isFrameNew() ) {
colorImg = vidGrabber.getPixels();
colorImg.mirror( false, true );
grayImg = colorImg;
if( bLearnBakground ) {
bgImg = grayImg;
bLearnBakground = false;
}
//Create Image Level Range 0 - 255
grayImg.convertToRange(lowRange, highRange);
grayImg.absDiff( bgImg );
grayImg.blur( 11 );
grayImg.threshold( threshold );
//findContures( img, minSize, maxSize, nMax, inner contours yes/no )
contourFinder.findContours( grayImg, 50,1800, 10, false );
blobTracker.trackBlobs( contourFinder.blobs );
}
}
void testApp::draw() {
ofSetColor( 0xffffff );
colorImg.draw( 20,20 );
// grayImg.draw(340,20 );
blobTracker.draw( 20,20 );
string strhelp="[space] to learn background\n[1]/[2] to adjust threshold: "+ofToString(threshold, 2)+"\n[m] to toggle system mouse events:"+ofToString(mouseon)+"\n[9]/[0] to adj low level pass:"+ofToString(lowRange)+"\n[-]/[=] to adj high level pass:"+ofToString(highRange);
ofDrawBitmapString( strhelp, 20,300 );
ofxOscMessage m2;
m2.setAddress( "/tuio/2Dcur" );
m2.addStringArg( "alive" );
for (int i = 0; i < blobTracker.blobs.size() ; i++){
// ofCvTrackedBlob blob = blobTracker.getById( id );
ofxOscMessage m1;
m1.setAddress( "/tuio/2Dcur" );
m1.addStringArg( "set" );
m1.addIntArg( blobTracker.blobs[i].id ); //id (id can't be == 0)
m1.addFloatArg( blobTracker.blobs[i].center.x/360 ); // x/camWidth
m1.addFloatArg( blobTracker.blobs[i].center.y/240 ); // y/camHeight
m1.addFloatArg( blobTracker.blobs[i].deltaLoc.x ); //X
m1.addFloatArg( blobTracker.blobs[i].deltaLoc.y ); //Y
m1.addFloatArg( blobTracker.blobs[i].area ); //m
m1.addFloatArg(blobTracker.blobs[i].box.width ); // wd
m1.addFloatArg( blobTracker.blobs[i].box.height );// ht
TUIOSocket.sendMessage( m1 );
m2.addIntArg( blobTracker.blobs[i].id ); //Get list of ALL active IDs
}
TUIOSocket.sendMessage( m2 );//send them
alivecounter= blobTracker.blobs.size();
}
void testApp::keyPressed( int key ) {
switch (key){
case 'm':
if( !mouseon ) {
mouseon = true;
}else{
mouseon=false;
}
break;
case '2':
threshold ++;
if (threshold > 255) threshold = 255;
break;
case '1':
threshold --;
if (threshold < 10) threshold = 10;
//if (threshold < 0) threshold = 0;
break;
case ' ':
bLearnBakground = true;
break;
case '0':
lowRange ++;
if (lowRange > 255) lowRange = 255;
break;
case '9':
lowRange --;
if (lowRange < 0) lowRange = 0;
break;
case '=':
highRange ++;
if (highRange > 255) highRange = 255;
break;
case '-':
highRange --;
if (highRange < 0) highRange = 0;
break;
}
}
void testApp::mouseMoved( int x, int y ) {}
void testApp::mouseDragged( int x, int y, int button ) {}
void testApp::mousePressed( int x, int y, int button ) {}
void testApp::mouseReleased() {}
//--------------------------------------------------------------
void testApp::setmouse(float x, float y,float dx,float dy,eventtype event ){
CGSize mainScreenSize = CGDisplayScreenSize (CGMainDisplayID ());
//Boolean debug=1;
CGPoint pt;
CGPoint newloc;
CGEventRef eventRef=NULL;
pt.x =x/360*screenw;
pt.y = y/240*screenh;
int xDelta=0, yDelta=0;
switch(event){
case EVENT_TYPE_MOUSE_MOVE://move
//CGPostMouseEvent( pt, 1, 1, 0 );
break;
case EVENT_TYPE_MOUSE_DOWN://press
eventRef=CGEventCreateMouseEvent (NULL,kCGEventLeftMouseDown,pt,0);
CGEventPost (kCGHIDEventTap,eventRef);
CFRelease(eventRef);
break;
case EVENT_TYPE_MOUSE_UP://release
eventRef=CGEventCreateMouseEvent (NULL,kCGEventLeftMouseUp,pt,0);
CGEventPost (kCGHIDEventTap,eventRef);
CFRelease(eventRef);
break;
};
}
void testApp::blobOn( const ofCvTrackedBlob& b ) {
int order= blobTracker.findOrder(b.id);
if(mouseon && alivecounter==0 && order==0 ){
blobmouse=b.id;
setmouse(blobTracker.getById(blobmouse).center.x,blobTracker.getById(blobmouse).center.y,blobTracker.getById(blobmouse).deltaLoc.x,blobTracker.getById(blobmouse).deltaLoc.y,EVENT_TYPE_MOUSE_DOWN );
cout << "blobOn() - id:" << b.id << " order:" << order << endl;
}
}
void testApp::blobMoved( const ofCvTrackedBlob& b) {
int order= blobTracker.findOrder(b.id);
if( order==0){
cout << "blobMoved() - id:" << b.id << " order:" << order << endl;
//setmouse(blobTracker.getById(blobmouse).center.x,blobTracker.getById(blobmouse).center.y,blobTracker.getById(blobmouse).deltaLoc.x,blobTracker.getById(blobmouse).deltaLoc.y,EVENT_TYPE_MOUSE_MOVE );
//cout << "blobMoved() - id:" << b.id << " order:" << order << endl;
}
}
void testApp::blobOff( const ofCvTrackedBlob& b ) {
int order= blobTracker.findOrder(b.id);
//
if(order==0 && blobmouse== b.id){
setmouse(blobTracker.getById(blobmouse).center.x,blobTracker.getById(blobmouse).center.y,blobTracker.getById(blobmouse).deltaLoc.x,blobTracker.getById(blobmouse).deltaLoc.y,EVENT_TYPE_MOUSE_UP );
cout << "blobOff() - id:" << b.id << " order:" << order << endl;
}
}
void testApp::blobAlive( vector <int> a ) {
}
and h:
#include "ofMain.h"
#include "ofCvMain.h"
#include "ofxOsc.h"
#define HOST "localhost"
#define PORT 3333
class testApp : public ofSimpleApp, public ofCvBlobListener {
public:
int alivecounter;
int cwidth;
int cheight;
int screenw;
int screenh;
ofVideoGrabber vidGrabber;
ofCvColorImage colorImg;
ofCvGrayscaleImage grayImg;
ofCvGrayscaleImage bgImg;
ofCvContourFinder contourFinder;
ofCvBlobTracker blobTracker;
int threshold;
bool bLearnBakground;
bool mouseon;
int blobmouse;
int lowRange;
int highRange;
void setup();
void update();
void draw();
void keyPressed( int key );
void mouseMoved( int x, int y );
void mouseDragged( int x, int y, int button );
void mousePressed( int x, int y, int button );
void mouseReleased();
void blobOn(const ofCvTrackedBlob& b);
void blobMoved( const ofCvTrackedBlob& b);
void blobOff( const ofCvTrackedBlob& b );
void blobAlive( vector <int> a );
typedef enum
{
EVENT_TYPE_MOUSE_DOWN,
EVENT_TYPE_MOUSE_UP,
EVENT_TYPE_MOUSE_MOVE,
EVENT_TYPE_MOUSE_SCROLL_MOVE,
EVENT_TYPE_KEY_DOWN,
EVENT_TYPE_KEY_UP
} eventtype;
void setmouse(float x,float y,float dx,float dy,eventtype event);
private:
ofxOscSender TUIOSocket;
};