/home/jpf/code/cc/project/cmil/src/InputNode.cpp

Go to the documentation of this file.
00001 // =====================================================================================
00002 //       Filename:  InputNode.cpp 
00003 //    Description:  
00004 //        Created:  02/15/2007 08:47:46 PM EST
00005 //       Revision:  none
00006 //       Compiler:  gcc 3.4.6
00007 // 
00008 //         Author:  John P. Feltz 
00009 //          Email:  jfeltz@gmail.com
00010 //        License:  Copyright (c) 2006-2007, John P. Feltz 
00011 //                  This program is free software; you can redistribute it and/or
00012 //                  modify it under the terms of the GNU General Public License as
00013 //                  published by the Free Software Foundation, version 2 of the
00014 //                  License.
00015 //                  This program is distributed in the hope that it will be
00016 //                  useful, but WITHOUT ANY WARRANTY; without even the implied
00017 //                  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00018 //                  PURPOSE.  //                  See the GNU General Public License version 2 for more details.
00019 // 
00020 // =====================================================================================
00021 
00022 #include <System.h>
00023 
00024 #include <InputSequencer.h>
00025 #include <InputNode.h>
00026 #include <InputContext.h>
00027 
00028 
00029 namespace cmil {
00030   using std::string;
00031 
00032   InputNode::InputNode(InputAction* action, System* const sys, const NodeIndex& index, InputNode* parent, const int id) : 
00033     SystemClient(sys),
00034     m_action(action), 
00035     m_index(new NodeIndex(index)), 
00036     m_parent(parent),
00037     m_id(id), 
00038     m_time_margin(0), 
00039     m_time(0), 
00040     m_activechild(NULL) 
00041   {
00042       std::ostringstream debug_ss;
00043       debug_ss << "index initialized with size of: " << m_index->size() ; 
00044       log_debug(debug_ss.str());
00045   }
00046 
00047   InputNode::InputNode(InputAction* action, System* const sys, InputNode* parent, const int id) : 
00048     SystemClient(sys),
00049     m_action(action), 
00050     m_index(NULL), 
00051     m_parent(parent), 
00052     m_id(id), 
00053     m_activechild(NULL)
00054   {}
00055 
00056   void InputNode::cleanup() {
00057     std::ostringstream debug_ss;
00058 
00059     if(m_index) {
00060       debug_ss << "cleanup() m_index found of size:" << m_index->size();
00061       log_debug(debug_ss.str());
00062 
00063       for(InputNode::NodeIndex::iterator i = m_index->begin(); i != m_index->end(); ++i) {
00064         if(*i) {
00065           log_debug("cleanup() found non-null node in m_index, deleting ");
00066           delete (*i);
00067         }
00068       }
00069 
00070       delete m_index;
00071     }
00072   }
00073 
00074   InputNode* InputNode::createRoot(System* sys, unsigned long int index_size) { 
00075     //InputNode* test_node = new InputNode(NULL, NULL, NodeIndex(55), NULL, 0);
00076 
00077     return new InputNode(NULL, sys, NodeIndex(index_size), NULL, 0);
00078   }
00079 
00081   InputNode* const InputNode::createChild(bool indexing, int id) {
00082     if(!m_index) {
00083       log_error("createChild(bool,int) returning NULL due to lack of an m_index to work with.");
00084       return NULL;
00085     }
00086 
00087     if(indexing) {
00088       (*m_index)[id] = new InputNode(NULL, getSystem(), NodeIndex(SystemClient::getSize()), this, id);
00089     }
00090     else
00091       (*m_index)[id] = new InputNode(NULL, getSystem(), this, id);
00092 
00093     return (*m_index)[id];
00094   }
00095 
00099 
00100   InputNode* const InputNode::createChild(bool forced_index, const Ev& event) {
00101     int id;
00102     std::ostringstream debug_ss;
00103     const bool with_index = true;
00104     const bool without_index = false;
00105 
00106     if(!m_index) {
00107       log_error("createChild(bool,event) returning NULL due to lack of an m_index to work with.");
00108       return NULL;
00109     }
00110 
00111     debug_ss << "createChild(Event), this m_id: " << m_id;
00112     log_debug(debug_ss.str());
00113 
00115     log_debug("createChild(Event) locating ID for given event");
00116 
00117     if( (id = SystemClient::getID(event) )) {
00119       if(getChild(id)) {
00120         log_debug("creatChild(Event) returning pre-exisiting node");
00121         return getChild(id);  
00122       }
00123 
00124       if(!forced_index) {
00125         log_debug( std::string("createChild(Event) determining kind of child to create for event:").append( event.getName() ));
00126         if(!(dynamic_cast<ModData*>(getData(id)))) 
00127           return createChild(without_index, id);  
00128         else  
00129           return createChild(with_index, id); 
00130       }
00131       else
00132         return createChild(with_index, id); 
00133     }
00134 
00135     log_error("createChild(Event) returning NULL");
00136     return NULL;
00137   }
00138 
00140   bool InputNode::deleteChild(int id) {
00141     InputNode* child;
00142     if( (child = getChild(id)) ) {  
00143       delete child;
00144      ((*m_index)[id]) = NULL;
00145 
00146       return true;
00147     }
00148 
00149     return false;
00150   }
00151 
00152   bool NormalHandler::handle(const InputData& input) {
00153     InputNode* child;
00154     std::ostringstream debug_ss;
00155   
00156     if(!canHandle(input)) 
00157       return false;
00158   
00160     for(m_i = m_controlnodes.begin() ; m_i != m_controlnodes.end() ; m_i++) {
00161       //log_debug( "handle() iterating through control nodes.."); 
00162       //check for the child by input index value 
00163   
00164       if((child = (*(*m_i)->getIndex())[input.getID()])  != NULL) {
00165         if(child->getAction())
00166           input.run(child->getAction());
00167   
00168         debug_ss <<" handle( " << input.getName() << ") handled. ";
00169         log_debug(debug_ss.str());
00170         debug_ss.str("");
00171 
00172         return true;
00173       }
00174     }
00175   
00177     if(m_root)
00178       if((child = (*m_root->getIndex())[input.getID()]) != NULL) {
00179         input.run(child->getAction());
00180         log_debug(string("handle(InputData) : ").append(input.getName()).append(" handled."));
00181         return true;
00182       }
00183   
00184     log_debug(string("handle(InputData) : ").append(input.getName()).append(" not handled."));
00185     return false;
00186   }
00187 
00188 
00189   //TODO This needs major cleanup.  
00190   bool NormalHandler::handle(const ModData& input) {
00191     if(!canHandle(input)) 
00192       return false;
00193 
00194     log_debug(string("Handle(ModData) : ").append(input.getName()));
00195 
00196     if(m_active[input.getID()]) {
00197       log_debug(string("Handle(ModData) : found in active active index: ").append(input.getName()));
00198 
00200       if(!input.getState())  {
00201         (((m_active[input.getID()])->getParent())->setActiveChild(NULL));
00202 
00205         RemoveControl(m_active[input.getID()]);
00206       }
00207 
00209       if((m_active[input.getID()])->getAction()) 
00210         input.run((m_active[input.getID()])->getAction());
00211     
00213       m_active[input.getID()] = NULL;
00214 
00215       log_debug(string("Handle(ModData) : ").append(input.getName()).append(" handled, inactivity set."));
00216       return true;
00217     }
00218     else {
00221       if(input.getState()) {
00222 
00223         if((m_active[input.getID()] = HandleActivity(input)) != NULL) {
00224           log_debug(string("Handle(ModData) : ").append(input.getName()).append(" handled, activity set"));
00225           return true;
00226         }
00227         else
00228           log_debug(string("Handle(ModData) : ").append(input.getName()).append(" not handled, no activity set"));
00229 
00230         log_debug(string("Handle(ModData) : ").append(input.getName()).append(" not handled due to no sequence found, "));
00231         return false;
00232       }
00233       return false;
00234     }
00235   }
00236 
00240 
00241   bool ComboHandler::isCombo(const Ev::Sequence& seq) const {
00242     InputData*   tmp_input;
00243     InputAction* tmp_action;
00244 
00246     if( !(  seq.back().getAction() )  ) { 
00247       log_error("isCombo() : event is not part of a valid sequence due to missing action"); 
00248       return false;
00249     }
00250 
00251     for(Ev::Sequence::const_iterator i = seq.begin() ; i!= seq.end(); i++) {
00252       log_debug(string("isCombo() : iteratoring to event named: ").append((*i).getName())); 
00253       
00255       if( ( tmp_input = getData((*i))) == NULL ) {
00256         log_error(string("isCombo() : sequence found invalid due no InputData found for event: ").append((*i).getName()));
00257         return false;
00258       }
00259       
00261       if((tmp_action = (*i).getAction()) != NULL) {
00263         if( i != --(seq.end()) ) {
00264           log_error(string("isCombo() : action found in wrong position of sequence. Ev: ").append((*i).getName())); 
00265           return false;
00266         }
00267         else if(!tmp_input->isActionCompatible(tmp_action)) {
00268           log_error("isCombo() : action found incompatible with event. "); 
00269           return false;
00270         }
00271 
00272         log_debug("isCombo() : returning true "); 
00273         return true;
00274       }
00275     }
00276 
00277     log_error("isCombo() : returning false ");
00278     return false;
00279   }
00280 
00281   bool NormalHandler::isNormal(const Ev::Sequence& seq) const {
00282     InputData*   tmp_input;
00283     InputAction* tmp_action;
00284 
00286     if( !( seq.back().getAction() )  ) { 
00287       log_error("isNormal() : event is not part of a valid sequence due to missing action"); 
00288       return false;
00289     }
00290 
00291     for(Ev::Sequence::const_iterator i = seq.begin() ; i!= seq.end(); i++) {
00292       log_debug(string("isNormal() : iteratoring to event named: ").append((*i).getName())); 
00294         if(Ev::getEventCount(seq, *(i)) > 1) {
00295           log_debug("isNormal() : returning false"); 
00296           return false;
00297         }
00298       
00300        if( ( tmp_input = getData(*i)) == NULL ) {
00301          log_error(string("isNormal() : sequence found invalid due no InputData found for event: ").append((*i).getName()));
00302          return false;
00303        }
00304        else if( i != --(seq.end()) ) {
00305           if(dynamic_cast<ModData*>(tmp_input) == NULL) {
00306           log_error("isNormal() : sequence found invalid due to a non-ModData type having dependant events.");
00307           return false;
00308         }
00309        }
00310       
00312       if((tmp_action = (*i).getAction()) != NULL) {
00314         if( i != --(seq.end()) ) {
00315           log_error(string("isNormal() : action found in wrong position of sequence. Ev: ").append((*i).getName())); 
00316           return false;
00317         }
00318         else if(!tmp_input->isActionCompatible(tmp_action)) {
00319           log_error("isNormal() : action found incompatible with event. "); 
00320           return false;
00321         }
00322 
00323         log_debug("isNormal() : returning true "); 
00324         return true;
00325       }
00326     }
00327 
00328     return false;
00329   }
00330   
00331   bool NormalHandler::add(const Ev::Sequence& seq) {
00332     std::cout << std::endl;
00333 
00334     if( m_added_seq.find(seq) != m_added_seq.end()) {
00335       log_error(" add() found matching pre-existing sequence, not adding."); 
00336       return false;
00337     }
00338 
00339     log_debug("add(seq) : defering sequence to merge function."); 
00340     return Merge(seq);
00341   }
00342 
00343   bool NormalHandler::remove(const Ev::Sequence& seq) {
00344     std::cout << std::endl;
00345 
00346     if( m_added_seq.find(seq) == m_added_seq.end()) {
00347       log_error(" remove(seq) no existing sequence found, not removing."); 
00348       return false;
00349     }
00350 
00351     log_debug("remove(seq) : defering sequence to unmerge function.");  
00352     return unMerge(seq);
00353   }
00354 
00355   bool ComboHandler::add(const Ev::Sequence& seq) {
00356       std::cout << std::endl;
00357       log_debug("add(seq) : defering sequence to merge function."); 
00358       return Merge(seq);
00359   }
00360 
00361   bool ComboHandler::canHandle(const InputData& input) const {
00362     if(!m_root) {
00363       //log_debug(std::string("canHandle() ").append(input.getName()).append(" not handled due to no sequences stored."));
00364       return false;
00365     }
00366     return true;
00367   }
00368 
00369   bool ComboHandler::check_time(const InputNode* child) const { 
00370     unsigned long child_time   = child->getTime();
00371     unsigned long child_margin = child->getTimeMargin();
00372     unsigned long parent_time  = 0;
00373 
00375     if(!child->getParent()) {
00376       log_error("check_time() failing due to being called on a root node.");
00377       return false;
00378     }
00379 
00381     if(!(child->getParent())->getParent()) {
00382       log_debug("check_time returning true due to calling from m_root node");
00383       return true;
00384     }
00385 
00386     parent_time = child->getParent()->getTime();
00387 
00388     if((child_time - parent_time) <= child_margin) { 
00389       log_debug("check_time() returning true because of time verification.");
00390       return true;
00391     }
00392 
00393     log_debug("check_time() returning false due to time check failure.");
00394     return false;
00395   }
00396 
00397   bool ComboHandler::handle(const InputData& input) {
00398     InputNode* child;
00399 
00400     if(!canHandle(input) || !input.getState()) 
00401       return false;
00402     
00404   
00405     if ( (child = m_controlling->getChild(input.getID())) != NULL) {
00406       child->setTime(input.getTime());
00407       if(check_time(child))
00408       {
00409         if(child->getAction()) {
00411            input.run(child->getAction());
00412            m_controlling = m_root;
00413         }
00414         else  {
00415           log_debug(string("handle(InputData) moving to next child in sequence: ").append(input.getName()));  
00417           child->setTime(input.getTime());  
00418           m_controlling = child; 
00419         }
00420 
00421         log_debug(string("handle(InputData) : ").append(input.getName()).append(" handled."));
00422         return true; 
00423       }
00424     }
00425 
00426     m_controlling = m_root;
00427     log_debug(string("handle(InputData) : ").append(input.getName()).append(" not handled."));
00428     return false; 
00429   }
00430 
00431   bool ComboHandler::Merge(const Ev::Sequence& seq) {
00432     std::ostringstream debug_ss;
00433     InputAction* tmp_action;
00434     InputNode* current;
00435 
00436     if(!m_root) {
00437       log_debug("Merge() creating root node");
00438       m_root = InputNode::createRoot(getSystem(), getSize());
00439       m_controlling = m_root;
00440     }
00441 
00442     current = m_root;
00443 
00444     for(Ev::Sequence::const_iterator i = seq.begin() ; i!=seq.end(); i++) {
00445       //log_debug(string("  Merge() iterating through event sequence, name: ").append((*i).getName()));
00446 
00447       if((current = current->createChild(true, *i)) == NULL) {
00448         log_error("  Merge() Unable to merge sequence due to InputNode creation error.");
00449         return false;
00450       }
00451 
00452       current->setTimeMargin( (*i).getTimeMargin() );
00453 
00455       if((tmp_action = (*i).getAction()) != NULL) {
00456         log_debug(string("  Merge() found action at event, assigning: ").append((*i).getName())); 
00457 
00458         current->setAction(tmp_action);
00459 
00460         return true;
00461       }
00462       else 
00463         log_debug(string("  Merge() did not find an action at event: ").append((*i).getName()));
00464     }
00465     
00467     log_error("Merge() Unable to merge sequence due to no action being found.");
00468     return false;
00469   }
00470 
00471   bool NormalHandler::canHandle(const InputData& input) const {
00472     if(!m_root) {
00473       log_debug(std::string("canHandle() ").append(input.getName()).append(" not handled due to no sequences stored."));
00474       return false;
00475     }
00476     if(m_active.size()) {
00477       if(m_active.size() <= input.getID()) {
00478         log_error(std::string("canHandle() ").append(input.getName()).append(" not handled due to input not being availabile in present index."));
00479         return false;
00480       }
00481     }
00482     else {
00483       log_debug(std::string("canHandle() ").append(input.getName()).append(" not handled due to no seqeuences stored."));
00484       return false;
00485     }
00486 
00487     //log_debug(std::string("canHandle() ").append(input.getName()).append(" returning true"));
00488     return true;
00489   }
00490 
00491   bool NormalHandler::Merge(const Ev::Sequence& seq) {
00492     std::ostringstream debug_ss;
00493     InputAction* tmp_action;
00494     InputNode* current;
00495 
00496     if(!m_root) {
00497       log_debug("Merge() creating root node");
00498       m_root = InputNode::createRoot(getSystem(), getSize());
00499     }
00500 
00501     if(!m_active.size()) { 
00502       log_debug("Merge() setting m_active" );
00503       m_active = InputNode::NodeIndex(SystemClient::getSize());
00504     }
00505 
00506     current = m_root;
00507 
00508     for(Ev::Sequence::const_iterator i = seq.begin() ; i!=seq.end(); i++) {
00509       //log_debug(string("  Merge() iterating through event sequence, name: ").append((*i).getName()));
00510 
00512       if((current = current->createChild(false, (*i))) == NULL)
00513         return false;
00514 
00516       if((tmp_action = (*i).getAction()) != NULL) {
00517         log_debug(string("  Merge() found action at event: ").append((*i).getName())); 
00518 
00519         current->setAction(tmp_action);
00520 
00522         m_added_seq[seq] = new NodeSequence(current);
00523         return true;
00524       }
00525       else 
00526         log_debug(string("  Merge() did not find an action at event: ").append((*i).getName()));
00527     }
00528     
00530     log_error("Merge() Unable to merge sequence due to no action being found.");
00531     return false;
00532   }
00533 
00535   bool NormalHandler::unMerge(const Ev::Sequence& seq) {
00536     InputNode* parent;
00537     InputNode* current;
00538 
00539     if(!m_root) {
00540       log_debug("unMerge() found no root node, returning false");
00541       return false; 
00542     }
00543 
00544     if(m_added_seq[seq]) {
00545       current = m_added_seq[seq]->getLastNode();
00546       m_added_seq.erase(seq);
00547     }
00548     else {
00549       log_error("unMerge() didn't find the sequence on what should be second check.");
00550       return false;
00551     }
00552   
00553     for(Ev::Sequence::const_iterator i = seq.end() ; i!=seq.begin(); --i) {
00554       if( (current->getChildren()) || (!current->getParent()) ) 
00555         continue;
00556 
00557       parent = current->getParent();
00558       parent->deleteChild(current->getID());
00559 
00560       current = parent;
00561     }
00562 
00563     log_debug("unMerge() finishing removal"); 
00564     return true;
00565   }
00566 
00569   
00570   InputNode* NormalHandler::HandleActivity(const ModData& input) {
00571     InputNode* child;
00572 
00573 //    log_debug(string("HandleActivity() : ").append(input.getName()));
00574 
00576     for(m_i = m_controlnodes.begin() ; m_i != m_controlnodes.end() ; m_i++) {
00578       if((child = (*(*m_i)->getIndex())[input.getID()]) != NULL) {
00579         if(child->getAction())
00580           input.run(child->getAction());
00581 
00583         (*m_i)->setActiveChild(child);
00584         (*m_i) = recurChildController(child);
00585 
00586         log_debug(string("HandleActivity(ModData) ").append(input.getName()).append( "successfully handled."));
00587         return child;
00588       }
00589     }
00590 
00591     log_debug(" HandleActivity() checking root since no control nodes found.");
00592 
00593     if(m_root)
00594       if((child = (*m_root->getIndex())[input.getID()]) != NULL) {
00595         if(child->getAction()) {
00596           log_debug("HandleActivity() running found action.");
00597           input.run(child->getAction());
00598         }
00599 
00601         if(child->getIndex() != NULL) 
00604           m_controlnodes.push_front(child);
00605 
00606         return child;
00607       }
00608 
00609     log_debug(string("HandleActivity(ModeData) ").append(input.getName()).append(" not handled."));
00610     return NULL;
00611   }
00612 
00616 
00617   InputNode* NormalHandler::recurChildController(InputNode* const node) {
00620     if((node->getIndex() != NULL) ) { 
00621       if(node->getActiveChild() != NULL) 
00622         return recurChildController(node->getActiveChild());
00623       else 
00624         return node;
00625     }
00626 
00629     return node->getParent(); 
00630   }
00631 
00634   InputNode* NormalHandler::recurParentController(InputNode* const node) {
00635     if(!node->getParent())
00637       return NULL;
00638 
00639     if(m_active[node->getID()]) 
00641       return node;
00642 
00644     return recurParentController(node->getParent());
00645   }
00646 
00648   void NormalHandler::RemoveControl(InputNode* node) {
00650     InputNode* controller = recurChildController(node);
00651 
00653     for(m_i = m_controlnodes.begin() ; m_i != m_controlnodes.end() ; m_i++) 
00654       if((*m_i)->getID() == controller->getID()) {
00655         
00657     
00658       InputNode* new_controller = recurParentController(node->getParent());
00659 
00660       if (new_controller== NULL)
00662         m_controlnodes.erase(m_i);
00663       else {
00665         (*m_i) = new_controller; 
00666       }
00667       break;
00668     }
00669   }
00670 
00672   Ev::Sequence* NodeSequencer::capture(const ModData& input) {
00674     if(input.getState()) {
00675       if(m_captured == NULL) 
00676         m_captured = new Ev::Sequence();
00677 
00678       m_captured->push_back(input.createEv());  
00679     }
00680     else {
00682     
00683     input.run(m_stop_capture_action); 
00684 
00685      if(m_captured) 
00686        return m_captured;
00687     }
00688 
00690     return NULL;
00691   }; 
00692 }

(c) 2006-2007 John P. Feltz
Generated on Wed Jul 25 16:08:02 2007 for Common Media Input Layer by doxygen 1.4.7

SourceForge.net Logo