std::string InputDeviceAdapter::getFeatureName(const InputDeviceFeature& feature) const { /* Return a default feature name: */ return getDefaultFeatureName(feature); }
InputDeviceAdapterPlayback::InputDeviceAdapterPlayback(InputDeviceManager* sInputDeviceManager,const Misc::ConfigurationFileSection& configFileSection) :InputDeviceAdapter(sInputDeviceManager), inputDeviceDataFile(configFileSection.retrieveString("./inputDeviceDataFileName").c_str(),"rb",Misc::File::LittleEndian), mouseCursorFaker(0), synchronizePlayback(configFileSection.retrieveValue<bool>("./synchronizePlayback",false)), quitWhenDone(configFileSection.retrieveValue<bool>("./quitWhenDone",false)), soundPlayer(0), saveMovie(configFileSection.retrieveValue<bool>("./saveMovie",false)), movieWindowIndex(0),movieWindow(0), firstFrame(true),timeStamp(0.0), done(false) { /* Read file header: */ static const char* fileHeader="Vrui Input Device Data File v2.0\n"; char header[34]; inputDeviceDataFile.read<char>(header,34); bool haveFeatureNames=strncmp(header,fileHeader,34)==0; if(!haveFeatureNames) { /* Old file format doesn't have the header text: */ inputDeviceDataFile.rewind(); } /* Read random seed value: */ unsigned int randomSeed=inputDeviceDataFile.read<unsigned int>(); setRandomSeed(randomSeed); /* Read number of saved input devices: */ numInputDevices=inputDeviceDataFile.read<int>(); inputDevices=new InputDevice*[numInputDevices]; deviceFeatureBaseIndices=new int[numInputDevices]; /* Initialize devices: */ for(int i=0;i<numInputDevices;++i) { /* Read device's name and layout from file: */ std::string name=Misc::readCppString(inputDeviceDataFile); int trackType=inputDeviceDataFile.read<int>(); int numButtons=inputDeviceDataFile.read<int>(); int numValuators=inputDeviceDataFile.read<int>(); Vector deviceRayDirection; inputDeviceDataFile.read<Scalar>(deviceRayDirection.getComponents(),3); /* Create new input device: */ InputDevice* newDevice=inputDeviceManager->createInputDevice(name.c_str(),trackType,numButtons,numValuators,true); newDevice->setDeviceRayDirection(deviceRayDirection); /* Initialize the new device's glyph from the current configuration file section: */ Glyph& deviceGlyph=inputDeviceManager->getInputGraphManager()->getInputDeviceGlyph(newDevice); char deviceGlyphTypeTag[20]; snprintf(deviceGlyphTypeTag,sizeof(deviceGlyphTypeTag),"./device%dGlyphType",i); char deviceGlyphMaterialTag[20]; snprintf(deviceGlyphMaterialTag,sizeof(deviceGlyphMaterialTag),"./device%dGlyphMaterial",i); deviceGlyph.configure(configFileSection,deviceGlyphTypeTag,deviceGlyphMaterialTag); /* Store the input device: */ inputDevices[i]=newDevice; /* Read or create the device's feature names: */ deviceFeatureBaseIndices[i]=int(deviceFeatureNames.size()); if(haveFeatureNames) { /* Read feature names from file: */ for(int j=0;j<newDevice->getNumFeatures();++j) deviceFeatureNames.push_back(Misc::readCppString(inputDeviceDataFile)); } else { /* Create default feature names: */ for(int j=0;j<newDevice->getNumFeatures();++j) deviceFeatureNames.push_back(getDefaultFeatureName(InputDeviceFeature(newDevice,j))); } } /* Check if the user wants to use a fake mouse cursor: */ int fakeMouseCursorDevice=configFileSection.retrieveValue<int>("./fakeMouseCursorDevice",-1); if(fakeMouseCursorDevice>=0) { /* Read the cursor file name and nominal size: */ std::string mouseCursorImageFileName=configFileSection.retrieveString("./mouseCursorImageFileName",DEFAULTMOUSECURSORIMAGEFILENAME); unsigned int mouseCursorNominalSize=configFileSection.retrieveValue<unsigned int>("./mouseCursorNominalSize",24); /* Create the mouse cursor faker: */ mouseCursorFaker=new MouseCursorFaker(inputDevices[fakeMouseCursorDevice],mouseCursorImageFileName.c_str(),mouseCursorNominalSize); mouseCursorFaker->setCursorSize(configFileSection.retrieveValue<Size>("./mouseCursorSize",mouseCursorFaker->getCursorSize())); mouseCursorFaker->setCursorHotspot(configFileSection.retrieveValue<Vector>("./mouseCursorHotspot",mouseCursorFaker->getCursorHotspot())); } /* Read time stamp of first data frame: */ try { nextTimeStamp=inputDeviceDataFile.read<double>(); /* Request an update for the next frame: */ requestUpdate(); } catch(Misc::File::ReadError) { done=true; nextTimeStamp=Math::Constants<double>::max; if(quitWhenDone) { /* Request exiting the program: */ shutdown(); } } /* Check if the user wants to play back a commentary sound track: */ std::string soundFileName=configFileSection.retrieveString("./soundFileName",""); if(soundFileName!="") { try { /* Create a sound player for the given sound file name: */ soundPlayer=new Sound::SoundPlayer(soundFileName.c_str()); } catch(std::runtime_error error) { /* Print a message, but carry on: */ std::cerr<<"InputDeviceAdapterPlayback: Disabling sound playback due to exception "<<error.what()<<std::endl; } } /* Check if the user wants to save a movie: */ if(saveMovie) { /* Read the movie image file name template: */ movieFileNameTemplate=configFileSection.retrieveString("./movieFileNameTemplate"); /* Check if the name template has the correct format: */ int numConversions=0; bool hasIntConversion=false; for(std::string::const_iterator mfntIt=movieFileNameTemplate.begin();mfntIt!=movieFileNameTemplate.end();++mfntIt) { if(*mfntIt=='%') { ++mfntIt; if(*mfntIt!='%') { ++numConversions; /* Skip width modifiers: */ while(isdigit(*mfntIt)) ++mfntIt; /* Check for integer conversion: */ if(*mfntIt=='d') hasIntConversion=true; } } else if(*mfntIt=='/') // Only accept conversions in the file name part hasIntConversion=false; } if(numConversions!=1||!hasIntConversion) Misc::throwStdErr("InputDeviceAdapterPlayback::InputDeviceAdapterPlayback: movie file name template \"%s\" does not have exactly one %%d conversion",movieFileNameTemplate.c_str()); /* Get the index of the window from which to save the frames: */ movieWindowIndex=configFileSection.retrieveValue<int>("./movieWindowIndex",movieWindowIndex); /* Get the intended frame rate for the movie: */ double frameRate=configFileSection.retrieveValue<double>("./movieFrameRate",30.0); movieFrameTimeInterval=1.0/frameRate; /* Calculate the first time at which to save a frame: */ nextMovieFrameTime=nextTimeStamp+movieFrameTimeInterval*0.5; nextMovieFrameCounter=0; } }
int main(int argc,char* argv[]) { /* Open the input file: */ IO::SeekableFilePtr inputDeviceDataFile(IO::openSeekableFile(argv[1])); inputDeviceDataFile->setEndianness(Misc::LittleEndian); /* Read the file header: */ static const char* fileHeader="Vrui Input Device Data File v3.0\n"; char header[34]; inputDeviceDataFile->read<char>(header,34); header[33]='\0'; int fileVersion; if(strncmp(header,fileHeader,29)!=0) { /* Pre-versioning file version: */ fileVersion=1; /* Old file format doesn't have the header text: */ inputDeviceDataFile->setReadPosAbs(0); } else if(strcmp(header+29,"2.0\n")==0) { /* File version without ray direction and velocities: */ fileVersion=2; } else if(strcmp(header+29,"3.0\n")==0) { /* File version with ray direction and velocities: */ fileVersion=3; } else { header[32]='\0'; std::cerr<<"Unsupported input device data file version "<<header+29<<std::endl; return 1; } /* Skip random seed value: */ inputDeviceDataFile->read<unsigned int>(); /* Read file header: */ int numInputDevices=inputDeviceDataFile->read<int>(); Vrui::InputDevice** inputDevices=new Vrui::InputDevice*[numInputDevices]; int* deviceFeatureBaseIndices=new int[numInputDevices]; std::vector<std::string> deviceFeatureNames; /* Initialize devices: */ for(int i=0;i<numInputDevices;++i) { /* Read device's name and layout from file: */ std::string name; if(fileVersion>=2) name=Misc::readCppString(*inputDeviceDataFile); else { /* Read a fixed-size string: */ char nameBuffer[40]; inputDeviceDataFile->read(nameBuffer,sizeof(nameBuffer)); name=nameBuffer; } int trackType=inputDeviceDataFile->read<int>(); int numButtons=inputDeviceDataFile->read<int>(); int numValuators=inputDeviceDataFile->read<int>(); /* Create new input device: */ Vrui::InputDevice* newDevice=new Vrui::InputDevice; newDevice->set(name.c_str(),trackType,numButtons,numValuators); if(fileVersion<3) { Vrui::Vector deviceRayDirection; inputDeviceDataFile->read(deviceRayDirection.getComponents(),3); newDevice->setDeviceRay(deviceRayDirection,Vrui::Scalar(0)); } /* Store the input device: */ inputDevices[i]=newDevice; /* Read or create the device's feature names: */ deviceFeatureBaseIndices[i]=int(deviceFeatureNames.size()); if(fileVersion>=2) { /* Read feature names from file: */ for(int j=0;j<newDevice->getNumFeatures();++j) deviceFeatureNames.push_back(Misc::readCppString(*inputDeviceDataFile)); } else { /* Create default feature names: */ for(int j=0;j<newDevice->getNumFeatures();++j) deviceFeatureNames.push_back(getDefaultFeatureName(Vrui::InputDeviceFeature(newDevice,j))); } } /* Read all data frames from the input device data file: */ while(true) { /* Read the next time stamp: */ double timeStamp; try { timeStamp=inputDeviceDataFile->read<double>(); } catch(IO::File::ReadError) { /* At end of file */ break; } std::cout<<"Time stamp: "<<std::fixed<<std::setw(8)<<std::setprecision(3)<<timeStamp; /* Read data for all input devices: */ for(int device=0;device<numInputDevices;++device) { /* Update tracker state: */ if(inputDevices[device]->getTrackType()!=Vrui::InputDevice::TRACK_NONE) { if(fileVersion>=3) { Vrui::Vector deviceRayDir; inputDeviceDataFile->read(deviceRayDir.getComponents(),3); Vrui::Scalar deviceRayStart=inputDeviceDataFile->read<Vrui::Scalar>(); inputDevices[device]->setDeviceRay(deviceRayDir,deviceRayStart); } Vrui::TrackerState::Vector translation; inputDeviceDataFile->read(translation.getComponents(),3); Vrui::Scalar quat[4]; inputDeviceDataFile->read(quat,4); inputDevices[device]->setTransformation(Vrui::TrackerState(translation,Vrui::TrackerState::Rotation(quat))); if(fileVersion>=3) { Vrui::Vector linearVelocity,angularVelocity; inputDeviceDataFile->read(linearVelocity.getComponents(),3); inputDeviceDataFile->read(angularVelocity.getComponents(),3); inputDevices[device]->setLinearVelocity(linearVelocity); inputDevices[device]->setAngularVelocity(angularVelocity); } } /* Update button states: */ if(fileVersion>=3) { unsigned char buttonBits=0x00U; int numBits=0; for(int i=0;i<inputDevices[device]->getNumButtons();++i) { if(numBits==0) { buttonBits=inputDeviceDataFile->read<unsigned char>(); numBits=8; } inputDevices[device]->setButtonState(i,(buttonBits&0x80U)!=0x00U); buttonBits<<=1; --numBits; } } else { for(int i=0;i<inputDevices[device]->getNumButtons();++i) { int buttonState=inputDeviceDataFile->read<int>(); inputDevices[device]->setButtonState(i,buttonState); } } /* Update valuator states: */ for(int i=0;i<inputDevices[device]->getNumValuators();++i) { double valuatorState=inputDeviceDataFile->read<double>(); inputDevices[device]->setValuator(i,valuatorState); } } std::cout<<std::endl; } return 0; }