Skip to content

Commit

Permalink
Fix command provider. This fixes several broken behaviours.
Browse files Browse the repository at this point in the history
  • Loading branch information
leecbaker committed Mar 13, 2021
1 parent 65e6747 commit f1edb60
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 36 deletions.
55 changes: 23 additions & 32 deletions lib/lb_xplane/command_provider.cpp
@@ -1,51 +1,42 @@
#include "command_provider.h"

#include "logging.h"

CommandProvider::CommandProvider(std::string name, std::string description, std::function<bool(void)> begin_func, std::function<bool(void)> continue_func, std::function<bool(void)> end_func)
: begin_func(std::move(begin_func))
, continue_func(std::move(continue_func))
, end_func(std::move(end_func))
{
cr = XPLMCreateCommand(name.c_str(), description.c_str());

if(bool(this->begin_func)) {
XPLMRegisterCommandHandler(cr, CommandProvider::begin_handler, 1, static_cast<void *>(this));
}

if(bool(this->continue_func)) {
XPLMRegisterCommandHandler(cr, CommandProvider::continue_handler, 1, static_cast<void *>(this));
}

if(bool(this->end_func)) {
XPLMRegisterCommandHandler(cr, CommandProvider::end_handler, 1, static_cast<void *>(this));
}
XPLMRegisterCommandHandler(cr, CommandProvider::handler, 0, static_cast<void *>(this));
}

CommandProvider::~CommandProvider() {
if(bool(begin_func)) {
XPLMUnregisterCommandHandler(cr, CommandProvider::begin_handler, 1, static_cast<void *>(this));
}

if(bool(continue_func)) {
XPLMUnregisterCommandHandler(cr, CommandProvider::continue_handler, 1, static_cast<void *>(this));
}

if(bool(end_func)) {
XPLMUnregisterCommandHandler(cr, CommandProvider::end_handler, 1, static_cast<void *>(this));
}
XPLMUnregisterCommandHandler(cr, CommandProvider::handler, 0, static_cast<void *>(this));
}


int CommandProvider::begin_handler(XPLMCommandRef /* inCommand */, XPLMCommandPhase /* inPhase */, void * inRefcon) {
CommandProvider * pthis = static_cast<CommandProvider *>(inRefcon);
return pthis->begin_func() ? 1 : 0;
}
int CommandProvider::handler(XPLMCommandRef /* inCommand */, XPLMCommandPhase phase, void * inRefcon) {

int CommandProvider::continue_handler(XPLMCommandRef /* inCommand */, XPLMCommandPhase /* inPhase */, void * inRefcon) {
CommandProvider * pthis = static_cast<CommandProvider *>(inRefcon);
return pthis->continue_func() ? 1 : 0;
}
switch(phase) {
case xplm_CommandBegin:
if(bool(pthis->begin_func)) {
return pthis->begin_func() ? 1 : 0;
}
break;
case xplm_CommandContinue:
if(bool(pthis->continue_func)) {
return pthis->continue_func() ? 1 : 0;
}
break;
case xplm_CommandEnd:
if(bool(pthis->end_func)) {
return pthis->end_func() ? 1 : 0;
}
break;
}

int CommandProvider::end_handler(XPLMCommandRef /* inCommand */, XPLMCommandPhase /* inPhase */, void * inRefcon ) {
CommandProvider * pthis = static_cast<CommandProvider *>(inRefcon);
return pthis->end_func() ? 1 : 0;
return 0;
}
7 changes: 3 additions & 4 deletions lib/lb_xplane/command_provider.h
Expand Up @@ -13,10 +13,9 @@ class CommandProvider {
std::function<bool(void)> continue_func;
std::function<bool(void)> end_func;

static int begin_handler( XPLMCommandRef inCommand, XPLMCommandPhase inPhase, void * inRefcon);
static int continue_handler( XPLMCommandRef inCommand, XPLMCommandPhase inPhase, void * inRefcon);
static int end_handler( XPLMCommandRef inCommand, XPLMCommandPhase inPhase, void * inRefcon);
static int handler( XPLMCommandRef inCommand, XPLMCommandPhase inPhase, void * inRefcon);
public:
CommandProvider(std::string name, std::string description, std::function<bool(void)> begin_func, std::function<bool(void)> continue_func = {}, std::function<bool(void)> end_func = {});
CommandProvider(std::string name, std::string description, std::function<bool(void)> func) : CommandProvider(name, description, {}, {}, func) {}
CommandProvider(std::string name, std::string description, std::function<bool(void)> begin_func, std::function<bool(void)> continue_func, std::function<bool(void)> end_func);
~CommandProvider();
};

0 comments on commit f1edb60

Please sign in to comment.