Skip to content

Commit

Permalink
init no longer segfaults - for now...
Browse files Browse the repository at this point in the history
  • Loading branch information
pgte committed Nov 28, 2012
1 parent 9ecd2e1 commit 1919e83
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 29 deletions.
2 changes: 1 addition & 1 deletion TODO.md
@@ -1,3 +1,3 @@
# TODO

EVERYTHING!!!
* src/Context.cc: replace POSE_TO_USE with user option.
3 changes: 2 additions & 1 deletion binding.gyp
Expand Up @@ -3,7 +3,8 @@
{
'target_name': 'openni',
'sources': [
'src/Context.cc',
'src/Callbacks.cc',
'src/Context.cc'
],
'include_dirs': [
'/usr/include/ni'
Expand Down
82 changes: 56 additions & 26 deletions src/Context.cc
Expand Up @@ -6,87 +6,116 @@
#include "uv.h"

#include <stdio.h>
#include <stdexcept>
#include <string>

using namespace node;
using namespace v8;

#include <XnCppWrapper.h>

#include "context.h"
#include "Context.h"
#include "Callbacks.h"

namespace nodeopenni {


//// Utils
bool
hasError(XnStatus status) {
hasError(XnStatus status)
{
return status != XN_STATUS_OK;
}

Handle<Value>
error(XnStatus status) {
error(XnStatus status)
{
return ThrowException(Exception::Error(
String::New(xnGetStatusString(status))));
}

//// Callbacks
///// Initialization
Handle<Value>
Context::Init()
{
XnStatus status;

//this->context_ = new xn::Context();
status = this->context_.Init();
if (hasError(status)) return error(status);
// this->context_.AddRef();
status = this->userGenerator_.Create(this->context_);
if (hasError(status)) return error(status);

// Register user callbacks

status = this->userGenerator_.RegisterUserCallbacks(
User_NewUser, User_LostUser, this, this->userCallbackHandle_);
if (hasError(status)) return error(status);

this->userGenerator_.GetPoseDetectionCap().RegisterToPoseCallbacks(
Pose_Detected, NULL, this, this->poseCallbackHandle_);

User_NewUser(xn::UserGenerator& generator, XnUserID nId, void* pCookie) {
printf("New User: %d\n", nId);
Context * context = (Context) pCookie;
context.GetPoseDetectionCap().StartPoseDetection(POSE_TO_USE,
nId);
this->userGenerator_.GetSkeletonCap().RegisterCalibrationCallbacks(
Calibration_Start, Calibration_End, this, this->calibrationCallbackHandle_);

return Undefined();
}

///// Life-cycle

Context *
Context::GetContext(const Arguments &args) {
Context::GetContext(const Arguments &args)
{
return ObjectWrap::Unwrap<Context>(args.This());
}

Context::Context() {
//this->context_ = new xn::Context();
this->context_.AddRef();
this->userGenerator_.Create(this->context_);
Context::Context()
{

// Register user callbacks

this->userGenerator_.RegisterUserCallbacks(
User_NewUser, User_LostUser,this, h1);
}

Handle<Value>
Context::New(const Arguments& args) {
Context::New(const Arguments& args)
{
HandleScope scope;

assert(args.IsConstructCall());

Context *context = new Context();
context->Wrap(args.This());

Handle<Value> initRet = context->Init();

// Check init return for errors
// An error is everything but undefined
if (! initRet->IsUndefined()) {
return initRet;
}

return args.This();
}

void
Context::Close() {
Context::Close()
{
this->context_.Release();
}

Handle<Value>
Context::Close(const Arguments& args) {
Context::Close(const Arguments& args)
{
HandleScope scope;
GetContext(args)->Close();
return Undefined();
}

Context::~Context() {
Context::~Context()
{
Close();
}

void
Context::Initialize(v8::Handle<v8::Object> target) {
Context::Initialize(v8::Handle<v8::Object> target)
{
HandleScope scope;

Local<FunctionTemplate> t = FunctionTemplate::New(New);
Expand All @@ -98,7 +127,8 @@ namespace nodeopenni {
}

void
Initialize(Handle<Object> target) {
Initialize(Handle<Object> target)
{
Context::Initialize(target);
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/Context.h
Expand Up @@ -14,15 +14,21 @@ namespace nodeopenni {
class Context : ObjectWrap {

public:

xn::UserGenerator userGenerator_;

static void Initialize (v8::Handle<v8::Object> target);
Context ();
virtual ~Context ();

private:
xn::Context context_;
xn::UserGenerator userGenerator_;
XnCallbackHandle userCallbackHandle_;
XnCallbackHandle poseCallbackHandle_;
XnCallbackHandle calibrationCallbackHandle_;

static Context* GetContext (const Arguments &args);
Handle<Value> Init ();
void Close ();
static Handle<Value> Close (const Arguments &args);
static Handle<Value> New (const Arguments& args);
Expand Down

0 comments on commit 1919e83

Please sign in to comment.