Exemplo n.º 1
0
void InputDeviceAdapter::initializeAdapter(const Misc::ConfigurationFileSection& configFileSection)
	{
	/* Allocate adapter state arrays: */
	typedef std::vector<std::string> StringList;
	StringList inputDeviceNames=configFileSection.retrieveValue<StringList>("./inputDeviceNames");
	numInputDevices=inputDeviceNames.size();
	inputDevices=new InputDevice*[numInputDevices];
	for(int i=0;i<numInputDevices;++i)
		inputDevices[i]=0;
	
	/* Initialize input devices: */
	for(int i=0;i<numInputDevices;++i)
		{
		/* Go to device's section: */
		Misc::ConfigurationFileSection deviceSection=configFileSection.getSection(inputDeviceNames[i].c_str());
		
		/* Initialize input device: */
		createInputDevice(i,deviceSection);
		}
	}
Exemplo n.º 2
0
void InputDeviceManager::initialize(const Misc::ConfigurationFileSection& configFileSection)
	{
	/* Retrieve the list of input device adapters: */
	typedef std::vector<std::string> StringList;
	StringList inputDeviceAdapterNames=configFileSection.retrieveValue<StringList>("./inputDeviceAdapterNames");
	
	/* Remove all duplicates from the list of input device adapters: */
	for(unsigned int i=0;i<inputDeviceAdapterNames.size()-1;++i)
		{
		for(unsigned int j=inputDeviceAdapterNames.size()-1;j>i;--j)
			{
			if(inputDeviceAdapterNames[j]==inputDeviceAdapterNames[i])
				{
				/* Remove the duplicate list entry: */
				inputDeviceAdapterNames.erase(inputDeviceAdapterNames.begin()+j);
				}
			}
		}
	
	/* Initialize the adapter array: */
	numInputDeviceAdapters=inputDeviceAdapterNames.size();
	inputDeviceAdapters=new InputDeviceAdapter*[numInputDeviceAdapters];
	
	/* Initialize input device adapters: */
	int numIgnoredAdapters=0;
	int mouseAdapterIndex=-1;
	for(int i=0;i<numInputDeviceAdapters;++i)
		{
		/* Go to input device adapter's section: */
		Misc::ConfigurationFileSection inputDeviceAdapterSection=configFileSection.getSection(inputDeviceAdapterNames[i].c_str());
		
		/* Determine input device adapter's type: */
		std::string inputDeviceAdapterType=inputDeviceAdapterSection.retrieveString("./inputDeviceAdapterType");
		bool typeFound=true;
		try
			{
			if(inputDeviceAdapterType=="Mouse")
				{
				/* Check if there is already a mouse input device adapter: */
				if(mouseAdapterIndex>=0)
					{
					/* Ignore this input device adapter: */
					inputDeviceAdapters[i]=0;
					++numIgnoredAdapters;
					std::cout<<"InputDeviceManager: Ignoring mouse input device adapter "<<inputDeviceAdapterNames[i]<<" because there is already a mouse input device adapter"<<std::endl;
					}
				else
					{
					/* Create mouse input device adapter: */
					inputDeviceAdapters[i]=new InputDeviceAdapterMouse(this,inputDeviceAdapterSection);
					mouseAdapterIndex=i;
					}
				}
			else if(inputDeviceAdapterType=="DeviceDaemon")
				{
				/* Create device daemon input device adapter: */
				inputDeviceAdapters[i]=new InputDeviceAdapterDeviceDaemon(this,inputDeviceAdapterSection);
				}
			else if(inputDeviceAdapterType=="Trackd")
				{
				/* Create trackd input device adapter: */
				inputDeviceAdapters[i]=new InputDeviceAdapterTrackd(this,inputDeviceAdapterSection);
				}
			else if(inputDeviceAdapterType=="VisBox")
				{
				/* Create VisBox input device adapter: */
				inputDeviceAdapters[i]=new InputDeviceAdapterVisBox(this,inputDeviceAdapterSection);
				}
			else if(inputDeviceAdapterType=="HID")
				{
				/* Create HID input device adapter: */
				inputDeviceAdapters[i]=new InputDeviceAdapterHID(this,inputDeviceAdapterSection);
				}
			else if(inputDeviceAdapterType=="Playback")
				{
				/* Create device daemon input device adapter: */
				inputDeviceAdapters[i]=new InputDeviceAdapterPlayback(this,inputDeviceAdapterSection);
				}
			else
				typeFound=false;
			}
		catch(std::runtime_error err)
			{
			/* Print a warning message: */
			std::cout<<"InputDeviceManager: Ignoring input device adapter "<<inputDeviceAdapterNames[i];
			std::cout<<" due to exception "<<err.what()<<std::endl;
			
			/* Ignore the input device adapter: */
			inputDeviceAdapters[i]=0;
			++numIgnoredAdapters;
			}
		
		if(!typeFound)
			Misc::throwStdErr("InputDeviceManager: Unknown input device adapter type \"%s\"",inputDeviceAdapterType.c_str());
		}
	
	if(numIgnoredAdapters!=0)
		{
		/* Remove any ignored input device adapters from the array: */
		InputDeviceAdapter** newInputDeviceAdapters=new InputDeviceAdapter*[numInputDeviceAdapters-numIgnoredAdapters];
		int newNumInputDeviceAdapters=0;
		for(int i=0;i<numInputDeviceAdapters;++i)
			if(inputDeviceAdapters[i]!=0)
				{
				newInputDeviceAdapters[newNumInputDeviceAdapters]=inputDeviceAdapters[i];
				++newNumInputDeviceAdapters;
				}
		delete[] inputDeviceAdapters;
		numInputDeviceAdapters=newNumInputDeviceAdapters;
		inputDeviceAdapters=newInputDeviceAdapters;
		}
	
	/* If there is a mouse input device adapter, put it last in the list because it might implicitly depend on other input devices: */
	if(mouseAdapterIndex>=0&&mouseAdapterIndex<numInputDeviceAdapters-1)
		std::swap(inputDeviceAdapters[mouseAdapterIndex],inputDeviceAdapters[numInputDeviceAdapters-1]);
	
	/* Check if there are any valid input device adapters: */
	if(numInputDeviceAdapters==0)
		Misc::throwStdErr("InputDeviceManager: No valid input device adapters found; I refuse to work under conditions like these!");
	}
Exemplo n.º 3
0
void InputDeviceManager::initialize(const Misc::ConfigurationFileSection& configFileSection)
	{
	/* Retrieve the list of input device adapters: */
	typedef std::vector<std::string> StringList;
	StringList inputDeviceAdapterNames=configFileSection.retrieveValue<StringList>("./inputDeviceAdapterNames");
	numInputDeviceAdapters=inputDeviceAdapterNames.size();
	inputDeviceAdapters=new InputDeviceAdapter*[numInputDeviceAdapters];
	
	/* Initialize input device adapters: */
	int numIgnoredAdapters=0;
	for(int i=0;i<numInputDeviceAdapters;++i)
		{
		/* Go to input device adapter's section: */
		Misc::ConfigurationFileSection inputDeviceAdapterSection=configFileSection.getSection(inputDeviceAdapterNames[i].c_str());
		
		/* Determine input device adapter's type: */
		std::string inputDeviceAdapterType=inputDeviceAdapterSection.retrieveString("./inputDeviceAdapterType");
		bool typeFound=true;
		try
			{
			if(inputDeviceAdapterType=="Mouse")
				{
				/* Create mouse input device adapter: */
				inputDeviceAdapters[i]=new InputDeviceAdapterMouse(this,inputDeviceAdapterSection);
				}
			else if(inputDeviceAdapterType=="DeviceDaemon")
				{
				/* Create device daemon input device adapter: */
				inputDeviceAdapters[i]=new InputDeviceAdapterDeviceDaemon(this,inputDeviceAdapterSection);
				}
			else if(inputDeviceAdapterType=="Trackd")
				{
				/* Create trackd input device adapter: */
				inputDeviceAdapters[i]=new InputDeviceAdapterTrackd(this,inputDeviceAdapterSection);
				}
			else if(inputDeviceAdapterType=="VisBox")
				{
				/* Create VisBox input device adapter: */
				inputDeviceAdapters[i]=new InputDeviceAdapterVisBox(this,inputDeviceAdapterSection);
				}
			else if(inputDeviceAdapterType=="HID")
				{
				/* Create HID input device adapter: */
				inputDeviceAdapters[i]=new InputDeviceAdapterHID(this,inputDeviceAdapterSection);
				}
			else if(inputDeviceAdapterType=="Playback")
				{
				/* Create device daemon input device adapter: */
				inputDeviceAdapters[i]=new InputDeviceAdapterPlayback(this,inputDeviceAdapterSection);
				}
			else
				typeFound=false;
			}
		catch(std::runtime_error err)
			{
			/* Print a warning message: */
			std::cout<<"InputDeviceManager: Ignoring input device adapter "<<inputDeviceAdapterNames[i];
			std::cout<<" due to exception "<<err.what()<<std::endl;
			
			/* Ignore the input device adapter: */
			inputDeviceAdapters[i]=0;
			++numIgnoredAdapters;
			}
		
		if(!typeFound)
			Misc::throwStdErr("InputDeviceManager: Unknown input device adapter type \"%s\"",inputDeviceAdapterType.c_str());
		}
	
	if(numIgnoredAdapters!=0)
		{
		/* Remove any ignored input device adapters from the array: */
		InputDeviceAdapter** newInputDeviceAdapters=new InputDeviceAdapter*[numInputDeviceAdapters-numIgnoredAdapters];
		int newNumInputDeviceAdapters=0;
		for(int i=0;i<numInputDeviceAdapters;++i)
			if(inputDeviceAdapters[i]!=0)
				{
				newInputDeviceAdapters[newNumInputDeviceAdapters]=inputDeviceAdapters[i];
				++newNumInputDeviceAdapters;
				}
		delete[] inputDeviceAdapters;
		numInputDeviceAdapters=newNumInputDeviceAdapters;
		inputDeviceAdapters=newInputDeviceAdapters;
		}
	
	/* Check if there are any valid input device adapters: */
	if(numInputDeviceAdapters==0)
		Misc::throwStdErr("InputDeviceManager: No valid input device adapters found; I refuse to work under conditions like these!");
	}
InputDeviceAdapterPlayback::InputDeviceAdapterPlayback(InputDeviceManager* sInputDeviceManager,const Misc::ConfigurationFileSection& configFileSection)
	:InputDeviceAdapter(sInputDeviceManager),
	 inputDeviceDataFile(IO::openSeekableFile(configFileSection.retrieveString("./inputDeviceDataFileName").c_str())),
	 mouseCursorFaker(0),
	 synchronizePlayback(configFileSection.retrieveValue<bool>("./synchronizePlayback",false)),
	 quitWhenDone(configFileSection.retrieveValue<bool>("./quitWhenDone",false)),
	 soundPlayer(0),
	 #ifdef VRUI_INPUTDEVICEADAPTERPLAYBACK_USE_KINECT
	 kinectPlayer(0),
	 #endif
	 saveMovie(configFileSection.retrieveValue<bool>("./saveMovie",false)),
	 movieWindowIndex(0),movieWindow(0),
	 firstFrame(true),timeStamp(0.0),
	 done(false)
	{
	/* Read file header: */
	inputDeviceDataFile->setEndianness(Misc::LittleEndian);
	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->setReadPosAbs(0);
		}
	
	/* 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;
		if(haveFeatureNames)
			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>();
		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(IO::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.empty())
		{
		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;
			}
		}
	
	#ifdef VRUI_INPUTDEVICEADAPTERPLAYBACK_USE_KINECT
	/* Check if the user wants to play back 3D video: */
	std::string kinectPlayerSectionName=configFileSection.retrieveString("./kinectPlayer","");
	if(!kinectPlayerSectionName.empty())
		{
		/* Go to the Kinect player's section: */
		Misc::ConfigurationFileSection kinectPlayerSection=configFileSection.getSection(kinectPlayerSectionName.c_str());
		kinectPlayer=new KinectPlayback(nextTimeStamp,kinectPlayerSection);
		}
	#endif
	
	/* 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;
		}
	}
Exemplo n.º 5
0
KinectServer::KinectServer(USB::Context& usbContext,Misc::ConfigurationFileSection& configFileSection)
    :numCameras(0),cameraStates(0),
     listeningSocket(configFileSection.retrieveValue<int>("./listenPortId",26000),1)
{
    /* Read the list of cameras: */
    std::vector<std::string> cameraNames=configFileSection.retrieveValue<std::vector<std::string> >("./cameras",std::vector<std::string>());
    numCameras=cameraNames.size();
    cameraStates=new CameraState*[numCameras];

    /* Enumerate all USB devices: */
#ifdef VERBOSE
    std::cout<<"KinectServer: Enumerating Kinect camera devices on USB bus"<<std::endl;
#endif
    USB::DeviceList usbDevices(usbContext);
    size_t numKinectCameras=usbDevices.getNumDevices(0x045eU,0x02aeU);
    unsigned int numFoundCameras=0;
    for(unsigned int i=0; i<numCameras; ++i)
    {
        /* Read the camera's serial number: */
        Misc::ConfigurationFileSection cameraSection=configFileSection.getSection(cameraNames[i].c_str());
        std::string serialNumber=cameraSection.retrieveValue<std::string>("./serialNumber");

        /* Find a Kinect camera of the specified serial number: */
        unsigned int j;
        for(j=0; j<numKinectCameras; ++j)
        {
            /* Tentatively open the Kinect camera device: */
            USB::Device cam(usbDevices.getDevice(0x045eU,0x02aeU,j));
            if(cam.getSerialNumber()==serialNumber) // Bail out if the desired device was found
                break;
        }
        if(j<numKinectCameras)
        {
            /* Create a streamer for the found camera: */
#ifdef VERBOSE
            std::cout<<"KinectServer: Creating streamer for camera with serial number "<<serialNumber<<std::endl;
#endif
            cameraStates[numFoundCameras]=new CameraState(usbDevices.getDevice(0x045eU,0x02aeU,j),newFrameCond,newFrameCond);

            /* Check if camera is to remove background: */
            if(cameraSection.retrieveValue<bool>("./removeBackground",true))
            {
                Kinect::Camera& camera=cameraStates[numFoundCameras]->camera;

                /* Check whether to load a previously saved background file: */
                std::string backgroundFile=cameraSection.retrieveValue<std::string>("./backgroundFile",std::string());
                if(!backgroundFile.empty())
                {
                    /* Load the background file: */
                    std::string fullBackgroundFileName=KINECT_CONFIG_DIR;
                    fullBackgroundFileName.push_back('/');
                    fullBackgroundFileName.append(backgroundFile);
#ifdef VERBOSE
                    std::cout<<"KinectServer: Loading background depth image file "<<fullBackgroundFileName<<'-'<<serialNumber<<".background"<<std::endl;
#endif
                    camera.loadBackground(fullBackgroundFileName.c_str());
                }

                /* Check whether to capture background: */
                unsigned int captureBackgroundFrames=cameraSection.retrieveValue<unsigned int>("./captureBackgroundFrames",0);
                if(captureBackgroundFrames>0)
                {
                    /* Request background capture: */
#ifdef VERBOSE
                    std::cout<<"KinectServer: Capturing "<<captureBackgroundFrames<<" background depth frames"<<std::endl;
#endif
                    camera.captureBackground(captureBackgroundFrames,false);
                }

                /* Check whether to set a maximum depth value: */
                unsigned int maxDepth=cameraSection.retrieveValue<unsigned int>("./maxDepth",0);
                if(maxDepth>0)
                {
                    /* Set the maximum depth: */
#ifdef VERBOSE
                    std::cout<<"KinectServer: Setting maximum depth value to "<<maxDepth<<std::endl;
#endif
                    camera.setMaxDepth(maxDepth,false);
                }

                /* Set the background removal fuzz value: */
                int backgroundFuzz=cameraSection.retrieveValue<int>("./backgroundFuzz",camera.getBackgroundRemovalFuzz());
#ifdef VERBOSE
                std::cout<<"KinectServer: Setting background depth fuzz value to "<<backgroundFuzz<<std::endl;
#endif
                camera.setBackgroundRemovalFuzz(backgroundFuzz);

                /* Enable background removal: */
                camera.setRemoveBackground(true);
            }

            ++numFoundCameras;
        }
        else
            std::cerr<<"Kinect camera with serial number "<<serialNumber<<" not found on USB bus"<<std::endl;
    }

    /* Initialize streaming state: */
#ifdef VERBOSE
    std::cout<<"KinectServer: "<<numFoundCameras<<" Kinect cameras initialized"<<std::endl;
#endif
    numCameras=numFoundCameras;
    metaFrameIndex=0;
    numMissingColorFrames=numCameras;
    numMissingDepthFrames=numCameras;

    /* Start the listening and streaming threads: */
    listeningThread.start(this,&KinectServer::listeningThreadMethod);
    if(numCameras>0)
        streamingThread.start(this,&KinectServer::streamingThreadMethod);

    /* Start streaming on all connected cameras: */
    for(unsigned int i=0; i<numCameras; ++i)
        cameraStates[i]->startStreaming();
}