Repository: ofTheo/ofxKinect Branch: master Commit: ebb9075bcb5a Files: 71 Total size: 515.5 KB Directory structure: gitextract_ut9eoanc/ ├── .gitignore ├── LICENSE.txt ├── README.md ├── addon_config.mk ├── kinectExample/ │ ├── addons.make │ └── src/ │ ├── main.cpp │ ├── testApp.cpp │ └── testApp.h ├── libs/ │ ├── libfreenect/ │ │ ├── APACHE20 │ │ ├── CONTRIB │ │ ├── GPL2 │ │ ├── include/ │ │ │ ├── libfreenect-registration.h │ │ │ └── libfreenect.h │ │ ├── platform/ │ │ │ ├── linux/ │ │ │ │ └── udev/ │ │ │ │ ├── 51-kinect.rules │ │ │ │ └── README │ │ │ └── windows/ │ │ │ ├── README.TXT │ │ │ ├── inf/ │ │ │ │ ├── xbox nui audio/ │ │ │ │ │ ├── Xbox_NUI_Audio.cat │ │ │ │ │ ├── Xbox_NUI_Audio.inf │ │ │ │ │ ├── amd64/ │ │ │ │ │ │ └── libusb0.sys │ │ │ │ │ ├── ia64/ │ │ │ │ │ │ └── libusb0.sys │ │ │ │ │ ├── libusb-win32-bin-README.txt │ │ │ │ │ ├── license/ │ │ │ │ │ │ └── libusb-win32/ │ │ │ │ │ │ └── installer_license.txt │ │ │ │ │ └── x86/ │ │ │ │ │ └── libusb0.sys │ │ │ │ ├── xbox nui camera/ │ │ │ │ │ ├── Xbox_NUI_Camera.cat │ │ │ │ │ ├── Xbox_NUI_Camera.inf │ │ │ │ │ ├── amd64/ │ │ │ │ │ │ └── libusb0.sys │ │ │ │ │ ├── ia64/ │ │ │ │ │ │ └── libusb0.sys │ │ │ │ │ ├── libusb-win32-bin-README.txt │ │ │ │ │ ├── license/ │ │ │ │ │ │ └── libusb-win32/ │ │ │ │ │ │ └── installer_license.txt │ │ │ │ │ └── x86/ │ │ │ │ │ └── libusb0.sys │ │ │ │ └── xbox nui motor/ │ │ │ │ ├── Xbox_NUI_Motor.cat │ │ │ │ ├── Xbox_NUI_Motor.inf │ │ │ │ ├── amd64/ │ │ │ │ │ └── libusb0.sys │ │ │ │ ├── ia64/ │ │ │ │ │ └── libusb0.sys │ │ │ │ ├── libusb-win32-bin-README.txt │ │ │ │ ├── license/ │ │ │ │ │ └── libusb-win32/ │ │ │ │ │ └── installer_license.txt │ │ │ │ └── x86/ │ │ │ │ └── libusb0.sys │ │ │ ├── libusb10emu/ │ │ │ │ └── libusb-1.0/ │ │ │ │ ├── failguard.cpp │ │ │ │ ├── failguard.h │ │ │ │ ├── libusb.h │ │ │ │ ├── libusbemu.cpp │ │ │ │ ├── libusbemu_internal.h │ │ │ │ ├── libusbemu_threads.h │ │ │ │ └── libusbemu_threads_win32.h │ │ │ ├── stdbool.h │ │ │ └── unistd.h │ │ └── src/ │ │ ├── cameras.c │ │ ├── cameras.h │ │ ├── core.c │ │ ├── freenect_internal.h │ │ ├── loader.h │ │ ├── registration.c │ │ ├── registration.h │ │ ├── tilt.c │ │ ├── usb_libusb10.c │ │ └── usb_libusb10.h │ ├── libusb-1.0/ │ │ ├── include/ │ │ │ └── libusb-1.0/ │ │ │ └── libusb.h │ │ └── lib/ │ │ └── osx/ │ │ └── usb-1.0.a │ └── libusb-win32/ │ ├── AUTHORS.txt │ ├── COPYING_LGPL.txt │ ├── README.txt │ ├── include/ │ │ └── lusb0_usb.h │ └── lib/ │ ├── vs/ │ │ ├── libusb.lib │ │ └── libusbd.lib │ └── win_cb/ │ ├── libusb.lib │ └── libusbd.lib ├── scripts/ │ ├── update_freenect.sh │ └── update_libusb-win32.sh └── src/ ├── ofxBase3DVideo.h ├── ofxKinect.cpp └── ofxKinect.h ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ .svn/ obj/ bin/ build/ !data/ ipch/ # mac Finder index files .DS_* # mac apps *.app* # xcode user project files *.pbxuser *.perspectivev3 xcuserdata *.mode1*v3 # codeblocks project files *.depend *.layout *.cbTemp # visual studio project files *.vcxproj.user *.suo *.sdf *.opensdf ================================================ FILE: LICENSE.txt ================================================ Copyright (c) 2010, 2011 ofxKinect Team The MIT License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ ofxKinect ========= **Note: As of OpenFrameworks 0.8.0, ofxKinect has been integrated into the OF core. All future development of this addon will take place in the [main OF Github repo](https://github.com/openframeworks/openFrameworks) so please log issues there.** **This repo will be left for anyone supporting old (pre-0.8.0+) OF projects and will not be updated. The final master branch is compatible with OF 0.8.0.** Copyright (c) 2010-2013 ofxKinect Team MIT License. For information on usage and redistribution, and for a DISCLAIMER OF ALL WARRANTIES, see the file, "LICENSE.txt," in this distribution. See https://github.com/ofTheo/ofxKinect for documentation as well as the [OF forums](http://forum.openframeworks.cc/index.php). This project uses [libfreenect](https://github.com/OpenKinect/libfreenect), copyrighted by the Open Kinect Project using the Apache License v2. See the file "APACHE20" in libs/libfreenect. Description ----------- ofxKinect is an Open Frameworks addon for the Xbox Kinect that runs on Mac OSX, Linux, and Windows. OpenFrameworks is a cross platform open source toolkit for creative coding in C++. [http://www.openframeworks.cc/](http://www.openframeworks.cc/) #### NOTE: Xbox Kinect models > 1414 (1473, etc) or Kinect4Windows devices that have been plugged into an XBox will not work with ofxKinect on Mac OSX If you have an Xbox Kinect model 1473+ or Kinect4Windows device and want to use it with ofxKinect on OSX, DO NOT plug it into an XBox! If you do, it will receive a firmware update which will cause it to freeze after about 20 secs with libfreenect/ofxKinect on OSX. Linux and Windows are fine. The [fix is in the works](https://github.com/OpenKinect/libfreenect/pull/325). In the meantime, we suggest you get the *original* Xbox Kinect model 1414 and keep those new toys away from an XBox ... Installation ------------ To use ofxKinect, first you need to download and install [Open Frameworks](https://github.com/openframeworks/openFrameworks). To get a copy of the repository you can download the source from [http://github.com/ofTheo/ofxKinect](http://github.com/ofTheo/ofxKinect/) or, alternatively, you can use git clone:
git clone git://github.com/ofTheo/ofxKinect.git
The addon should sit in `openFrameworks/addons/ofxKinect/`. #### Which version to use? If you are using a stable version (0062, 007, ...) of OpenFrameworks then you want to use a git tag of ofxKinect for that version. You can select the tag in the Github menu or clone and check it out using git. For example, the following commands will clone ofxKinect and switch to the OF 0062 tagged version:
git clone git://github.com/ofTheo/ofxKinect.git
cd ofxKinect
git checkout 0062
#### Using the latest ofxKinect The master branch of ofxKinect will work with the current master of OpenFrameworks and can be considered unstable. The develop branch is used for testing new features and bugfixes before they are applied to master. If you want to work with the latest unstable (still in development) ofxKinect, download the source from the develop branch [https://github.com/ofTheo/ofxKinect/tree/develop](https://github.com/ofTheo/ofxKinect/tree/develop) or via git clone:
git clone git://github.com/ofTheo/ofxKinect.git -b develop
Warning: The develop branch will be in flux, so don't be surprised if things do not always work as expected! #### Bug: ofxKinect 0.7.4 + Windows CodeBlocks 12.11 missing pthread If you're using Windows CodeBlocks 12.11 and OF/ofxKinect 0.7.4, you may see an issue where ofxKinect projects cannot compile/link due to missing the "pthread" library. This is due to CodeBlocks removing some built in libraries between versions 10.5 & 12.11. The fix is to manually add the pthread-win32 library which provides "pthread". First, make sure you've followed to "Add files to MinGW guide" in the [CodeBlocks 12.11 setup for OF guide](http://www.openframeworks.cc/setup/codeblocks/). Then, manually add pthreads-win32: 1. download the compiled version: http://www.sourceware.org/pthreads-win32/index.html 2. rename the lib file `Pre-built.2\lib\x86\libpthreadGC2.a` to `libpthread` and copy it into the MinGW folder for your CodeBlocks installation: `C:\Program Files (x86)\CodeBlocks\MinGW\lib` 3. if they do not exist, copy the header (.h) files in `Pre-build.2\include` into your CodeBlocks installation: `C:\Program Files (x86)\CodeBlocks\MinGW\include` Running the Example Project --------------------------- An example project is provided in the `kinectExample` folder. As of OF 0.8.0, ofxKinect no longer ships with the platform project files. Simply use the OpenFrameworks ProjectGenerator in `apps/projectGenerator` to generate the Xcode, VS2012, CodeBlocks projects and/or Makefiles by pointing it to the `kinectExample` folder and making sure to include the following addons: * ofxKinect (duh) * ofxOpenCv (for blob tracking in the example, not required by ofxKinect itself.) ### OSX Xcode4: Open the Xcode project, select the "kinectExample Debug" scheme, and hit "Run". ### Linux Install the libusb-1.0 library. On Ubuntu, you can do this with:
sudo apt-get install libusb-1.0-0-dev
Open the Code::Blocks .cbp and hit F9 to build. Optionally, you can build the example with the Makefile. To run it, use the terminal:
make
cd bin
./example_debug
Also, you can add a set of udev rules which allow you to run a kinect app without root privileges: sudo libs\libfreenect\platform\linux\udev\51-kinect.rules /etc/udev/rules.d ### Windows Precompiled libfreenect drivers and libusb-win32 libs are included for Windows. Make sure to install or update the libfreenect Kinect camera, motor, and audio drivers through Windows Device Manager by pointing it to the driver folder:
ofxKinect/libs/libfreenect/platform/windows/inf
You may need to manually update each driver individually if you've plugged it in before. ofxKinect will not work if the drivers are not installed. **NOTE**: You cannot use the OpenNI drivers and the libfreenect drivers included with ofxKinect at the same time. You must manually uninstall one and reinstall the other in the Device Manager. Sorry, that's just how it is. :P How to Create a New ofxKinect Project ------------------------------------- To develop your own project based on ofxKinect, simply copy the example project and rename it. You probably want to put it in your apps folder, for example, after copying: `openFrameworks/addons/ofxKinect/kinectExample/ => openFrameworks/apps/myApps/kinectExample/` Then after renaming: `openFrameworks/apps/myApps/myKinectProject/` ### Mac (Xcode): Rename the project in Xcode (do not rename the .xcodeproj file in Finder!): Slow double click the project name and rename (Xcode4) ### Codeblocks (Win & Linux): Close Codeblocks and rename the *.cbp and *.workspace files to the same name as the project folder. Open the workspace and readd the renamed project file. ### Windows (Visual Studio): Close Visual Studio and rename the *.sln file. Open the solution, right click on the project in the project browser and select "Rename ...". Adding ofxKinect to an Existing Project --------------------------------------- If you want to add ofxKinect to another project, you need to make sure you include the src folder and the :
openFrameworks/addons/ofxKinect/src
as well as search paths for the addons (ofxOpenCv) and libs (freenect, libusb) required by ofxKinect. Don't forget to include the ofxOpenCv addon sources and opencv libraries. ### Mac (Xcode): In the Xcode project browser: * add the ofxKinect sources to the project: * create a new group "ofxKinect" * drag these directories from ofxKinect into this new group: ofxKinect/src & ofxKinect/libs * add a search path to the libusb headers: `../../../addons/ofxKinect/libs/libusb/include/libusb-1.0` to your project Target build settings * Xcode4: add the lib path to your Project.xconfig, see the example * add the path to the libusb precompiled library: `../../../addons/ofxKinect/libs/libusb-1.0/lib/osx/usb-1.0.a` * Xcode4: add the lib path to your Project.xconfig, see the example ### Linux (Makefiles & Codeblocks): Edit the Makefile config files: * edit addons.make in your project folder and add the following line to the end of the file:
ofxKinect
* edit config.make in your project folder and change the lines for USER_LIBS to:
	USER_LIBS = -lusb-1.0
	
#### Windows (Visual Studio): * add the ofxPd sources to the project: * right click on the addons folder in your project tree and create a new filter named "ofxKinect" * drag the src directory from the ofxKinect addon folder in Explorer into the ofxKinect folder in your project tree * add header and library search paths: * right lick on your project and select "Properties" * choose "Debug" from the "Configurations" drop down box * under C/C++->General, add the following to the "Additional Include Directories":
	..\\..\\..\addons\ofxKinect\src
	..\\..\\..\addons\ofxKinect\libs\libfreenect\src
	..\\..\\..\addons\ofxKinect\libs\libfreenect\include
	..\\..\\..\addons\ofxKinect\libs\libfreenect\platform\windows
	..\\..\\..\addons\ofxKinect\libs\libfreenect\platform\windows\libusb10emu\libusb-1.0
	..\\..\\..\addons\ofxKinect\libs\libusb-win32\include
	
* under Linker->General, add the following to the "Additional Library Directories":
	..\..\..\addons\ofxKinect\libs\libusb-win32\lib\vs
	
* repeat for the "Release" configuration #### Windows (Codeblocks): * add the ofxPd sources to the project: * right-click on your project in the project tree * select "Add Files Recursively ..." * navigate and choose the ofxKinect/src folder * add search paths and libraries to link: * right-click on your project in the project tree * select "Build options..." * make sure the project name is selected in the tree (not release or debug) * select the "Search directories" tab, click add the search paths:
	..\\..\\..\addons\ofxKinect\src
	..\\..\\..\addons\ofxKinect\libs\libfreenect\src
	..\\..\\..\addons\ofxKinect\libs\libfreenect\include
	..\\..\\..\addons\ofxKinect\libs\libfreenect\platform\windows
	..\\..\\..\addons\ofxKinect\libs\libfreenect\platform\windows\libusb10emu\libusb-1.0
	..\\..\\..\addons\ofxKinect\libs\libusb-win32\include
	
* select the "Linker settings" tab, add the following to Link libraries:
	m
	pthread
	
* select the "Linker settings" tab, add the following to Other liker options:
	..\..\..\addons\ofxKinect\libs\libusb-win32\lib\win_cb\libusb.lib
	
Notes ----- ### Using multiple kinects ofxKinect supports multiple kinects, however stability is based on the bandwidth of your usb hardware. If you only require the depth image, it is recommended to disable the video grabbing of the rgb/ir images from your devices:
kinect.init(false, false);  // disable video image (faster fps)
For Kinect4Windows, Microsoft states that only 2 Kinects can be supported on the same USB bus. In practice on OSX, this proves to be the case as, even with the RGB images disabled, there are transfer errors using ofxKinect and 3 Kinects simultaneously. If you need to support many Kinects, you will probably need to add extra USB controllers to your machine … Developing ofxKinect -------------------- Feel free to log bug reports and issues to the ofxKinect Github page: https://github.com/ofTheo/ofxKinect/issues If you wish to help in ofxKinect development, please refer to the wiki: https://github.com/ofTheo/ofxKinect/wiki/Developing-ofxKinect ================================================ FILE: addon_config.mk ================================================ # All variables and this file are optional, if they are not present the PG and the # makefiles will try to parse the correct values from the file system. # # Variables that specify exclusions can use % as a wildcard to specify that anything in # that position will match. A partial path can also be specified to, for example, exclude # a whole folder from the parsed paths from the file system # # Variables can be specified using = or += # = will clear the contents of that variable both specified from the file or the ones parsed # from the file system # += will add the values to the previous ones in the file or the ones parsed from the file # system # # The PG can be used to detect errors in this file, just create a new project with this addon # and the PG will write to the console the kind of error and in which line it is meta: ADDON_NAME = ofxKinect ADDON_DESCRIPTION = Addon for interfacing with microsoft kinect cameras ADDON_AUTHOR = Dan Wilcox, Theo Watson, Kyle McDonald, Arturo Castro ADDON_TAGS = "computer vision" "3D sensing" "kinect" ADDON_URL = http://github.com/ofTheo/ofxKinect common: # dependencies with other addons, a list of them separated by spaces # or use += in several lines # ADDON_DEPENDENCIES = # include search paths, this will be usually parsed from the file system # but if the addon or addon libraries need special search paths they can be # specified here separated by spaces or one per line using += # ADDON_INCLUDES = # any special flag that should be passed to the compiler when using this # addon # ADDON_CFLAGS = # any special flag that should be passed to the linker when using this # addon, also used for system libraries with -lname # ADDON_LDFLAGS = # linux only, any library that should be included in the project using # pkg-config # ADDON_PKG_CONFIG_LIBRARIES = # osx/iOS only, any framework that should be included in the project # ADDON_FRAMEWORKS = # source files, these will be usually parsed from the file system looking # in the src folders in libs and the root of the addon. if your addon needs # to include files in different places or a different set of files per platform # they can be specified here # ADDON_SOURCES = # some addons need resources to be copied to the bin/data folder of the project # specify here any files that need to be copied, you can use wildcards like * and ? # ADDON_DATA = # when parsing the file system looking for libraries exclude this for all or # a specific platform # ADDON_LIBS_EXCLUDE = # when parsing the file system looking for sources exclude this for all or # a specific platform # ADDON_SOURCES_EXCLUDE = # when parsing the file system looking for include paths exclude this for all or # a specific platform # ADDON_INCLUDES_EXCLUDE = linux64: # linux only, any library that should be included in the project using # pkg-config ADDON_PKG_CONFIG_LIBRARIES = libusb-1.0 # when parsing the file system looking for sources exclude this for all or # a specific platform ADDON_SOURCES_EXCLUDE = libs/libfreenect/platform/% ADDON_SOURCES_EXCLUDE += libs/libusb-win32/% # when parsing the file system looking for include paths exclude this for all or # a specific platform ADDON_INCLUDES_EXCLUDE = libs/libfreenect/platform/% ADDON_INCLUDES_EXCLUDE += libs/libusb-win32/% ADDON_INCLUDES_EXCLUDE += libs/libusb-1.0/% linux: # linux only, any library that should be included in the project using # pkg-config ADDON_PKG_CONFIG_LIBRARIES = libusb-1.0 # when parsing the file system looking for sources exclude this for all or # a specific platform ADDON_SOURCES_EXCLUDE = libs/libfreenect/platform/% ADDON_SOURCES_EXCLUDE += libs/libusb-win32/% # when parsing the file system looking for include paths exclude this for all or # a specific platform ADDON_INCLUDES_EXCLUDE = libs/libfreenect/platform/% ADDON_INCLUDES_EXCLUDE += libs/libusb-win32/% ADDON_INCLUDES_EXCLUDE += libs/libusb-1.0/% win_cb: # source files, these will be usually parsed from the file system looking # in the src folders in libs and the root of the addon. if your addon needs # to include files in different places or a different set of files per platform # they can be specified here ADDON_SOURCES += libs/libfreenect/platform/windows/libusb10emu/libusb-1.0/failguard.cpp ADDON_SOURCES += libs/libfreenect/platform/windows/libusb10emu/libusb-1.0/libusbemu.cpp # include search paths, this will be usually parsed from the file system # but if the addon or addon libraries need special search paths they can be # specified here separated by spaces or one per line using += ADDON_INCLUDES += libs/libfreenect/platform/windows # when parsing the file system looking for include paths exclude this for all or # a specific platform ADDON_INCLUDES_EXCLUDE += libs/libusb-1.0/% vs: # source files, these will be usually parsed from the file system looking # in the src folders in libs and the root of the addon. if your addon needs # to include files in different places or a different set of files per platform # they can be specified here ADDON_SOURCES += libs/libfreenect/platform/windows/libusb10emu/libusb-1.0/failguard.cpp ADDON_SOURCES += libs/libfreenect/platform/windows/libusb10emu/libusb-1.0/libusbemu.cpp # include search paths, this will be usually parsed from the file system # but if the addon or addon libraries need special search paths they can be # specified here separated by spaces or one per line using += ADDON_INCLUDES += libs/libfreenect/platform/windows # when parsing the file system looking for include paths exclude this for all or # a specific platform ADDON_INCLUDES_EXCLUDE += libs/libusb-1.0/% linuxarmv6l: # linux only, any library that should be included in the project using # pkg-config ADDON_PKG_CONFIG_LIBRARIES = libusb-1.0 # when parsing the file system looking for sources exclude this for all or # a specific platform ADDON_SOURCES_EXCLUDE = libs/libfreenect/platform/% ADDON_SOURCES_EXCLUDE += libs/libusb-win32/% # when parsing the file system looking for include paths exclude this for all or # a specific platform ADDON_INCLUDES_EXCLUDE = libs/libfreenect/platform/% ADDON_INCLUDES_EXCLUDE += libs/libusb-win32/% ADDON_INCLUDES_EXCLUDE += libs/libusb-1.0/% linuxarmv7l: # linux only, any library that should be included in the project using # pkg-config ADDON_PKG_CONFIG_LIBRARIES = libusb-1.0 # when parsing the file system looking for sources exclude this for all or # a specific platform ADDON_SOURCES_EXCLUDE = libs/libfreenect/platform/% ADDON_SOURCES_EXCLUDE += libs/libusb-win32/% # when parsing the file system looking for include paths exclude this for all or # a specific platform ADDON_INCLUDES_EXCLUDE = libs/libfreenect/platform/% ADDON_INCLUDES_EXCLUDE += libs/libusb-win32/% ADDON_INCLUDES_EXCLUDE += libs/libusb-1.0/% android/armeabi: android/armeabi-v7a: osx: # when parsing the file system looking for sources exclude this for all or # a specific platform ADDON_SOURCES_EXCLUDE = libs/libfreenect/platform/% ADDON_SOURCES_EXCLUDE += libs/libusb-win32/% # when parsing the file system looking for include paths exclude this for all or # a specific platform ADDON_INCLUDES_EXCLUDE = libs/libfreenect/platform/% ADDON_INCLUDES_EXCLUDE += libs/libusb-win32/% ios: ================================================ FILE: kinectExample/addons.make ================================================ ofxKinect ofxOpenCv ================================================ FILE: kinectExample/src/main.cpp ================================================ #include "testApp.h" #include "ofAppGlutWindow.h" int main() { ofAppGlutWindow window; ofSetupOpenGL(&window, 1024, 768, OF_WINDOW); ofRunApp(new testApp()); } ================================================ FILE: kinectExample/src/testApp.cpp ================================================ #include "testApp.h" //-------------------------------------------------------------- void testApp::setup() { ofSetLogLevel(OF_LOG_VERBOSE); // enable depth->video image calibration kinect.setRegistration(true); kinect.init(); //kinect.init(true); // shows infrared instead of RGB video image //kinect.init(false, false); // disable video image (faster fps) kinect.open(); // opens first available kinect //kinect.open(1); // open a kinect by id, starting with 0 (sorted by serial # lexicographically)) //kinect.open("A00362A08602047A"); // open a kinect using it's unique serial # // print the intrinsic IR sensor values if(kinect.isConnected()) { ofLogNotice() << "sensor-emitter dist: " << kinect.getSensorEmitterDistance() << "cm"; ofLogNotice() << "sensor-camera dist: " << kinect.getSensorCameraDistance() << "cm"; ofLogNotice() << "zero plane pixel size: " << kinect.getZeroPlanePixelSize() << "mm"; ofLogNotice() << "zero plane dist: " << kinect.getZeroPlaneDistance() << "mm"; } #ifdef USE_TWO_KINECTS kinect2.init(); kinect2.open(); #endif colorImg.allocate(kinect.width, kinect.height); grayImage.allocate(kinect.width, kinect.height); grayThreshNear.allocate(kinect.width, kinect.height); grayThreshFar.allocate(kinect.width, kinect.height); nearThreshold = 230; farThreshold = 70; bThreshWithOpenCV = true; ofSetFrameRate(60); // zero the tilt on startup angle = 0; kinect.setCameraTiltAngle(angle); // start from the front bDrawPointCloud = false; } //-------------------------------------------------------------- void testApp::update() { ofBackground(100, 100, 100); kinect.update(); // there is a new frame and we are connected if(kinect.isFrameNew()) { // load grayscale depth image from the kinect source grayImage.setFromPixels(kinect.getDepthPixels(), kinect.width, kinect.height); // we do two thresholds - one for the far plane and one for the near plane // we then do a cvAnd to get the pixels which are a union of the two thresholds if(bThreshWithOpenCV) { grayThreshNear = grayImage; grayThreshFar = grayImage; grayThreshNear.threshold(nearThreshold, true); grayThreshFar.threshold(farThreshold); cvAnd(grayThreshNear.getCvImage(), grayThreshFar.getCvImage(), grayImage.getCvImage(), NULL); } else { // or we do it ourselves - show people how they can work with the pixels unsigned char * pix = grayImage.getPixels(); int numPixels = grayImage.getWidth() * grayImage.getHeight(); for(int i = 0; i < numPixels; i++) { if(pix[i] < nearThreshold && pix[i] > farThreshold) { pix[i] = 255; } else { pix[i] = 0; } } } // update the cv images grayImage.flagImageChanged(); // find contours which are between the size of 20 pixels and 1/3 the w*h pixels. // also, find holes is set to true so we will get interior contours as well.... contourFinder.findContours(grayImage, 10, (kinect.width*kinect.height)/2, 20, false); } #ifdef USE_TWO_KINECTS kinect2.update(); #endif } //-------------------------------------------------------------- void testApp::draw() { ofSetColor(255, 255, 255); if(bDrawPointCloud) { easyCam.begin(); drawPointCloud(); easyCam.end(); } else { // draw from the live kinect kinect.drawDepth(10, 10, 400, 300); kinect.draw(420, 10, 400, 300); grayImage.draw(10, 320, 400, 300); contourFinder.draw(10, 320, 400, 300); #ifdef USE_TWO_KINECTS kinect2.draw(420, 320, 400, 300); #endif } // draw instructions ofSetColor(255, 255, 255); stringstream reportStream; if(kinect.hasAccelControl()) { reportStream << "accel is: " << ofToString(kinect.getMksAccel().x, 2) << " / " << ofToString(kinect.getMksAccel().y, 2) << " / " << ofToString(kinect.getMksAccel().z, 2) << endl; } else { reportStream << "Note: this is a newer Xbox Kinect or Kinect For Windows device," << endl << "motor / led / accel controls are not currently supported" << endl << endl; } reportStream << "press p to switch between images and point cloud, rotate the point cloud with the mouse" << endl << "using opencv threshold = " << bThreshWithOpenCV <<" (press spacebar)" << endl << "set near threshold " << nearThreshold << " (press: + -)" << endl << "set far threshold " << farThreshold << " (press: < >) num blobs found " << contourFinder.nBlobs << ", fps: " << ofGetFrameRate() << endl << "press c to close the connection and o to open it again, connection is: " << kinect.isConnected() << endl; if(kinect.hasCamTiltControl()) { reportStream << "press UP and DOWN to change the tilt angle: " << angle << " degrees" << endl << "press 1-5 & 0 to change the led mode" << endl; } ofDrawBitmapString(reportStream.str(), 20, 652); } void testApp::drawPointCloud() { int w = 640; int h = 480; ofMesh mesh; mesh.setMode(OF_PRIMITIVE_POINTS); int step = 2; for(int y = 0; y < h; y += step) { for(int x = 0; x < w; x += step) { if(kinect.getDistanceAt(x, y) > 0) { mesh.addColor(kinect.getColorAt(x,y)); mesh.addVertex(kinect.getWorldCoordinateAt(x, y)); } } } glPointSize(3); ofPushMatrix(); // the projected points are 'upside down' and 'backwards' ofScale(1, -1, -1); ofTranslate(0, 0, -1000); // center the points a bit glEnable(GL_DEPTH_TEST); mesh.drawVertices(); glDisable(GL_DEPTH_TEST); ofPopMatrix(); } //-------------------------------------------------------------- void testApp::exit() { kinect.setCameraTiltAngle(0); // zero the tilt on exit kinect.close(); #ifdef USE_TWO_KINECTS kinect2.close(); #endif } //-------------------------------------------------------------- void testApp::keyPressed (int key) { switch (key) { case ' ': bThreshWithOpenCV = !bThreshWithOpenCV; break; case'p': bDrawPointCloud = !bDrawPointCloud; break; case '>': case '.': farThreshold ++; if (farThreshold > 255) farThreshold = 255; break; case '<': case ',': farThreshold --; if (farThreshold < 0) farThreshold = 0; break; case '+': case '=': nearThreshold ++; if (nearThreshold > 255) nearThreshold = 255; break; case '-': nearThreshold --; if (nearThreshold < 0) nearThreshold = 0; break; case 'w': kinect.enableDepthNearValueWhite(!kinect.isDepthNearValueWhite()); break; case 'o': kinect.setCameraTiltAngle(angle); // go back to prev tilt kinect.open(); break; case 'c': kinect.setCameraTiltAngle(0); // zero the tilt kinect.close(); break; case '1': kinect.setLed(ofxKinect::LED_GREEN); break; case '2': kinect.setLed(ofxKinect::LED_YELLOW); break; case '3': kinect.setLed(ofxKinect::LED_RED); break; case '4': kinect.setLed(ofxKinect::LED_BLINK_GREEN); break; case '5': kinect.setLed(ofxKinect::LED_BLINK_YELLOW_RED); break; case '0': kinect.setLed(ofxKinect::LED_OFF); break; case OF_KEY_UP: angle++; if(angle>30) angle=30; kinect.setCameraTiltAngle(angle); break; case OF_KEY_DOWN: angle--; if(angle<-30) angle=-30; kinect.setCameraTiltAngle(angle); break; } } //-------------------------------------------------------------- void testApp::mouseDragged(int x, int y, int button) {} //-------------------------------------------------------------- void testApp::mousePressed(int x, int y, int button) {} //-------------------------------------------------------------- void testApp::mouseReleased(int x, int y, int button) {} //-------------------------------------------------------------- void testApp::windowResized(int w, int h) {} ================================================ FILE: kinectExample/src/testApp.h ================================================ #pragma once #include "ofMain.h" #include "ofxOpenCv.h" #include "ofxKinect.h" // Windows users: // You MUST install the libfreenect kinect drivers in order to be able to use // ofxKinect. Plug in the kinect and point your Windows Device Manager to the // driver folder in: // // ofxKinect/libs/libfreenect/platform/windows/inf // // This should install the Kinect camera, motor, & audio drivers. // // You CANNOT use this driver and the OpenNI driver with the same device. You // will have to manually update the kinect device to use the libfreenect drivers // and/or uninstall/reinstall it in Device Manager. // // No way around the Windows driver dance, sorry. // uncomment this to read from two kinects simultaneously //#define USE_TWO_KINECTS class testApp : public ofBaseApp { public: void setup(); void update(); void draw(); void exit(); void drawPointCloud(); void keyPressed(int key); void mouseDragged(int x, int y, int button); void mousePressed(int x, int y, int button); void mouseReleased(int x, int y, int button); void windowResized(int w, int h); ofxKinect kinect; #ifdef USE_TWO_KINECTS ofxKinect kinect2; #endif ofxCvColorImage colorImg; ofxCvGrayscaleImage grayImage; // grayscale depth image ofxCvGrayscaleImage grayThreshNear; // the near thresholded image ofxCvGrayscaleImage grayThreshFar; // the far thresholded image ofxCvContourFinder contourFinder; bool bThreshWithOpenCV; bool bDrawPointCloud; int nearThreshold; int farThreshold; int angle; // used for viewing the point cloud ofEasyCam easyCam; }; ================================================ FILE: libs/libfreenect/APACHE20 ================================================ Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ================================================ FILE: libs/libfreenect/CONTRIB ================================================ For a list of libfreenect authors, please see the Git history of the project, such as with 'git log'. This file will contain a list of authors on release snapshots of libfreenect. THIS FILE IS NOT VALID FOR NON-GIT DISTRIBUTIONS. If you wish to redistribute libfreenect source code outside of the Git infrastructure, you MUST either use a release version or generate a complete CONTRIB file valid for the source Git revision tree that you use for your distribution. Redistributions of libfreenect source code without a valid CONTRIB file and without Git metadata are not compliant with the license. ================================================ FILE: libs/libfreenect/GPL2 ================================================ GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc. 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Library General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) year name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Library General Public License instead of this License. ================================================ FILE: libs/libfreenect/include/libfreenect-registration.h ================================================ /* * This file is part of the OpenKinect Project. http://www.openkinect.org * * Copyright (c) 2011 individual OpenKinect contributors. See the CONTRIB file * for details. * * This code is licensed to you under the terms of the Apache License, version * 2.0, or, at your option, the terms of the GNU General Public License, * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses, * or the following URLs: * http://www.apache.org/licenses/LICENSE-2.0 * http://www.gnu.org/licenses/gpl-2.0.txt * * If you redistribute this file in source form, modified or unmodified, you * may: * 1) Leave this header intact and distribute it under the same terms, * accompanying it with the APACHE20 and GPL20 files, or * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file * In all cases you must keep the copyright notice intact and include a copy * of the CONTRIB file. * * Binary distributions must follow the binary distribution requirements of * either License. */ #ifndef LIBFREENECT_REGISTRATION_H #define LIBFREENECT_REGISTRATION_H #include #include #ifdef __cplusplus extern "C" { #endif /// Internal Kinect registration parameters. /// Structure matches that of the line protocol /// of the Kinect. typedef struct { int32_t dx_center; // not used by mapping algorithm int32_t ax; int32_t bx; int32_t cx; int32_t dx; int32_t dx_start; int32_t ay; int32_t by; int32_t cy; int32_t dy; int32_t dy_start; int32_t dx_beta_start; int32_t dy_beta_start; int32_t rollout_blank; // not used by mapping algorithm int32_t rollout_size; // not used by mapping algorithm int32_t dx_beta_inc; int32_t dy_beta_inc; int32_t dxdx_start; int32_t dxdy_start; int32_t dydx_start; int32_t dydy_start; int32_t dxdxdx_start; int32_t dydxdx_start; int32_t dxdxdy_start; int32_t dydxdy_start; int32_t back_comp1; // not used by mapping algorithm int32_t dydydx_start; int32_t back_comp2; // not used by mapping algorithm int32_t dydydy_start; } freenect_reg_info; /// registration padding info (?) typedef struct { uint16_t start_lines; uint16_t end_lines; uint16_t cropping_lines; } freenect_reg_pad_info; /// internal Kinect zero plane data typedef struct { float dcmos_emitter_dist; // Distance between IR camera and IR emitter, in cm. float dcmos_rcmos_dist; // Distance between IR camera and RGB camera, in cm. float reference_distance; // The focal length of the IR camera, in mm. float reference_pixel_size; // The size of a single pixel on the zero plane, in mm. } freenect_zero_plane_info; /// all data needed for depth->RGB mapping typedef struct { freenect_reg_info reg_info; freenect_reg_pad_info reg_pad_info; freenect_zero_plane_info zero_plane_info; double const_shift; uint16_t* raw_to_mm_shift; int32_t* depth_to_rgb_shift; int32_t (*registration_table)[2]; // A table of 640*480 pairs of x,y values. // Index first by pixel, then x:0 and y:1. } freenect_registration; // These allow clients to export registration parameters; proper docs will // come later FREENECTAPI freenect_registration freenect_copy_registration(freenect_device* dev); FREENECTAPI int freenect_destroy_registration(freenect_registration* reg); // convenience function to convert a single x-y coordinate pair from camera // to world coordinates FREENECTAPI void freenect_camera_to_world(freenect_device* dev, int cx, int cy, int wz, double* wx, double* wy); #ifdef __cplusplus } #endif #endif // LIBFREENECT_REGISTRATION_H ================================================ FILE: libs/libfreenect/include/libfreenect.h ================================================ /* * This file is part of the OpenKinect Project. http://www.openkinect.org * * Copyright (c) 2010 individual OpenKinect contributors. See the CONTRIB file * for details. * * This code is licensed to you under the terms of the Apache License, version * 2.0, or, at your option, the terms of the GNU General Public License, * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses, * or the following URLs: * http://www.apache.org/licenses/LICENSE-2.0 * http://www.gnu.org/licenses/gpl-2.0.txt * * If you redistribute this file in source form, modified or unmodified, you * may: * 1) Leave this header intact and distribute it under the same terms, * accompanying it with the APACHE20 and GPL20 files, or * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file * In all cases you must keep the copyright notice intact and include a copy * of the CONTRIB file. * * Binary distributions must follow the binary distribution requirements of * either License. */ #ifndef LIBFREENECT_H #define LIBFREENECT_H #include /* We need struct timeval */ #ifdef _WIN32 #include #else #include #endif #ifdef __cplusplus extern "C" { #endif #define FREENECT_COUNTS_PER_G 819 /**< Ticks per G for accelerometer as set per http://www.kionix.com/Product%20Sheets/KXSD9%20Product%20Brief.pdf */ /// Maximum value that a uint16_t pixel will take on in the buffer of any of the FREENECT_DEPTH_MM or FREENECT_DEPTH_REGISTERED frame callbacks #define FREENECT_DEPTH_MM_MAX_VALUE 10000 /// Value indicating that this pixel has no data, when using FREENECT_DEPTH_MM or FREENECT_DEPTH_REGISTERED depth modes #define FREENECT_DEPTH_MM_NO_VALUE 0 /// Maximum value that a uint16_t pixel will take on in the buffer of any of the FREENECT_DEPTH_11BIT, FREENECT_DEPTH_10BIT, FREENECT_DEPTH_11BIT_PACKED, or FREENECT_DEPTH_10BIT_PACKED frame callbacks #define FREENECT_DEPTH_RAW_MAX_VALUE 2048 /// Value indicating that this pixel has no data, when using FREENECT_DEPTH_11BIT, FREENECT_DEPTH_10BIT, FREENECT_DEPTH_11BIT_PACKED, or FREENECT_DEPTH_10BIT_PACKED #define FREENECT_DEPTH_RAW_NO_VALUE 2047 /// Flags representing devices to open when freenect_open_device() is called. /// In particular, this allows libfreenect to grab only a subset of the devices /// in the Kinect, so you could (for instance) use libfreenect to handle audio /// and motor support while letting OpenNI have access to the cameras. /// If a device is not supported on a particular platform, its flag will be ignored. typedef enum { FREENECT_DEVICE_MOTOR = 0x01, FREENECT_DEVICE_CAMERA = 0x02, FREENECT_DEVICE_AUDIO = 0x04, } freenect_device_flags; /// A struct used in enumeration to give access to serial numbers, so you can /// open a particular device by serial rather than depending on index. This /// is most useful if you have more than one Kinect. struct freenect_device_attributes; struct freenect_device_attributes { struct freenect_device_attributes *next; /**< Next device in the linked list */ const char* camera_serial; /**< Serial number of this device's camera subdevice */ }; /// Enumeration of available resolutions. /// Not all available resolutions are actually supported for all video formats. /// Frame modes may not perfectly match resolutions. For instance, /// FREENECT_RESOLUTION_MEDIUM is 640x488 for the IR camera. typedef enum { FREENECT_RESOLUTION_LOW = 0, /**< QVGA - 320x240 */ FREENECT_RESOLUTION_MEDIUM = 1, /**< VGA - 640x480 */ FREENECT_RESOLUTION_HIGH = 2, /**< SXGA - 1280x1024 */ FREENECT_RESOLUTION_DUMMY = 2147483647, /**< Dummy value to force enum to be 32 bits wide */ } freenect_resolution; /// Enumeration of video frame information states. /// See http://openkinect.org/wiki/Protocol_Documentation#RGB_Camera for more information. typedef enum { FREENECT_VIDEO_RGB = 0, /**< Decompressed RGB mode (demosaicing done by libfreenect) */ FREENECT_VIDEO_BAYER = 1, /**< Bayer compressed mode (raw information from camera) */ FREENECT_VIDEO_IR_8BIT = 2, /**< 8-bit IR mode */ FREENECT_VIDEO_IR_10BIT = 3, /**< 10-bit IR mode */ FREENECT_VIDEO_IR_10BIT_PACKED = 4, /**< 10-bit packed IR mode */ FREENECT_VIDEO_YUV_RGB = 5, /**< YUV RGB mode */ FREENECT_VIDEO_YUV_RAW = 6, /**< YUV Raw mode */ FREENECT_VIDEO_DUMMY = 2147483647, /**< Dummy value to force enum to be 32 bits wide */ } freenect_video_format; /// Enumeration of depth frame states /// See http://openkinect.org/wiki/Protocol_Documentation#RGB_Camera for more information. typedef enum { FREENECT_DEPTH_11BIT = 0, /**< 11 bit depth information in one uint16_t/pixel */ FREENECT_DEPTH_10BIT = 1, /**< 10 bit depth information in one uint16_t/pixel */ FREENECT_DEPTH_11BIT_PACKED = 2, /**< 11 bit packed depth information */ FREENECT_DEPTH_10BIT_PACKED = 3, /**< 10 bit packed depth information */ FREENECT_DEPTH_REGISTERED = 4, /**< processed depth data in mm, aligned to 640x480 RGB */ FREENECT_DEPTH_MM = 5, /**< depth to each pixel in mm, but left unaligned to RGB image */ FREENECT_DEPTH_DUMMY = 2147483647, /**< Dummy value to force enum to be 32 bits wide */ } freenect_depth_format; /// Structure to give information about the width, height, bitrate, /// framerate, and buffer size of a frame in a particular mode, as /// well as the total number of bytes needed to hold a single frame. typedef struct { uint32_t reserved; /**< unique ID used internally. The meaning of values may change without notice. Don't touch or depend on the contents of this field. We mean it. */ freenect_resolution resolution; /**< Resolution this freenect_frame_mode describes, should you want to find it again with freenect_find_*_frame_mode(). */ union { int32_t dummy; freenect_video_format video_format; freenect_depth_format depth_format; }; /**< The video or depth format that this freenect_frame_mode describes. The caller should know which of video_format or depth_format to use, since they called freenect_get_*_frame_mode() */ int32_t bytes; /**< Total buffer size in bytes to hold a single frame of data. Should be equivalent to width * height * (data_bits_per_pixel+padding_bits_per_pixel) / 8 */ int16_t width; /**< Width of the frame, in pixels */ int16_t height; /**< Height of the frame, in pixels */ int8_t data_bits_per_pixel; /**< Number of bits of information needed for each pixel */ int8_t padding_bits_per_pixel; /**< Number of bits of padding for alignment used for each pixel */ int8_t framerate; /**< Approximate expected frame rate, in Hz */ int8_t is_valid; /**< If 0, this freenect_frame_mode is invalid and does not describe a supported mode. Otherwise, the frame_mode is valid. */ } freenect_frame_mode; /// Enumeration of LED states /// See http://openkinect.org/wiki/Protocol_Documentation#Setting_LED for more information. typedef enum { LED_OFF = 0, /**< Turn LED off */ LED_GREEN = 1, /**< Turn LED to Green */ LED_RED = 2, /**< Turn LED to Red */ LED_YELLOW = 3, /**< Turn LED to Yellow */ LED_BLINK_GREEN = 4, /**< Make LED blink Green */ // 5 is same as 4, LED blink Green LED_BLINK_RED_YELLOW = 6, /**< Make LED blink Red/Yellow */ } freenect_led_options; /// Enumeration of tilt motor status typedef enum { TILT_STATUS_STOPPED = 0x00, /**< Tilt motor is stopped */ TILT_STATUS_LIMIT = 0x01, /**< Tilt motor has reached movement limit */ TILT_STATUS_MOVING = 0x04, /**< Tilt motor is currently moving to new position */ } freenect_tilt_status_code; /// Data from the tilt motor and accelerometer typedef struct { int16_t accelerometer_x; /**< Raw accelerometer data for X-axis, see FREENECT_COUNTS_PER_G for conversion */ int16_t accelerometer_y; /**< Raw accelerometer data for Y-axis, see FREENECT_COUNTS_PER_G for conversion */ int16_t accelerometer_z; /**< Raw accelerometer data for Z-axis, see FREENECT_COUNTS_PER_G for conversion */ int8_t tilt_angle; /**< Raw tilt motor angle encoder information */ freenect_tilt_status_code tilt_status; /**< State of the tilt motor (stopped, moving, etc...) */ } freenect_raw_tilt_state; struct _freenect_context; typedef struct _freenect_context freenect_context; /**< Holds information about the usb context. */ struct _freenect_device; typedef struct _freenect_device freenect_device; /**< Holds device information. */ // usb backend specific section typedef void freenect_usb_context; /**< Holds libusb-1.0 context */ // /// If Win32, export all functions for DLL usage #ifndef _WIN32 #define FREENECTAPI /**< DLLExport information for windows, set to nothing on other platforms */ #else /**< DLLExport information for windows, set to nothing on other platforms */ #ifdef __cplusplus #define FREENECTAPI extern "C" __declspec(dllexport) #else // this is required when building from a Win32 port of gcc without being // forced to compile all of the library files (.c) with g++... #define FREENECTAPI __declspec(dllexport) #endif #endif /// Enumeration of message logging levels typedef enum { FREENECT_LOG_FATAL = 0, /**< Log for crashing/non-recoverable errors */ FREENECT_LOG_ERROR, /**< Log for major errors */ FREENECT_LOG_WARNING, /**< Log for warning messages */ FREENECT_LOG_NOTICE, /**< Log for important messages */ FREENECT_LOG_INFO, /**< Log for normal messages */ FREENECT_LOG_DEBUG, /**< Log for useful development messages */ FREENECT_LOG_SPEW, /**< Log for slightly less useful messages */ FREENECT_LOG_FLOOD, /**< Log EVERYTHING. May slow performance. */ } freenect_loglevel; /** * Initialize a freenect context and do any setup required for * platform specific USB libraries. * * @param ctx Address of pointer to freenect context struct to allocate and initialize * @param usb_ctx USB context to initialize. Can be NULL if not using multiple contexts. * * @return 0 on success, < 0 on error */ FREENECTAPI int freenect_init(freenect_context **ctx, freenect_usb_context *usb_ctx); /** * Closes the device if it is open, and frees the context * * @param ctx freenect context to close/free * * @return 0 on success */ FREENECTAPI int freenect_shutdown(freenect_context *ctx); /// Typedef for logging callback functions typedef void (*freenect_log_cb)(freenect_context *dev, freenect_loglevel level, const char *msg); /** * Set the log level for the specified freenect context * * @param ctx context to set log level for * @param level log level to use (see freenect_loglevel enum) */ FREENECTAPI void freenect_set_log_level(freenect_context *ctx, freenect_loglevel level); /** * Callback for log messages (i.e. for rerouting to a file instead of * stdout) * * @param ctx context to set log callback for * @param cb callback function pointer */ FREENECTAPI void freenect_set_log_callback(freenect_context *ctx, freenect_log_cb cb); /** * Calls the platform specific usb event processor * * @param ctx context to process events for * * @return 0 on success, other values on error, platform/library dependant */ FREENECTAPI int freenect_process_events(freenect_context *ctx); /** * Calls the platform specific usb event processor until either an event occurs * or the timeout parameter time has passed. If a zero timeval is passed, this * function will handle any already-pending events, then return immediately. * * @param ctx Context to process events for * @param timeout Pointer to a timeval containing the maximum amount of time to block waiting for events, or zero for nonblocking mode * * @return 0 on success, other values on error, platform/library dependant */ FREENECTAPI int freenect_process_events_timeout(freenect_context *ctx, struct timeval* timeout); /** * Return the number of kinect devices currently connected to the * system * * @param ctx Context to access device count through * * @return Number of devices connected, < 0 on error */ FREENECTAPI int freenect_num_devices(freenect_context *ctx); /** * Scans for kinect devices and produces a linked list of their attributes * (namely, serial numbers), returning the number of devices. * * @param ctx Context to scan for kinect devices with * @param attribute_list Pointer to where this function will store the resultant linked list * * @return Number of devices connected, < 0 on error */ FREENECTAPI int freenect_list_device_attributes(freenect_context *ctx, struct freenect_device_attributes** attribute_list); /** * Free the linked list produced by freenect_list_device_attributes(). * * @param attribute_list Linked list of attributes to free. */ FREENECTAPI void freenect_free_device_attributes(struct freenect_device_attributes* attribute_list); /** * Answer which subdevices this library supports. This is most useful for * wrappers trying to determine whether the underlying library was built with * audio support or not, so the wrapper can avoid calling functions that do not * exist. * * @return Flags representing the subdevices that the library supports opening (see freenect_device_flags) */ FREENECTAPI int freenect_supported_subdevices(void); /** * Set which subdevices any subsequent calls to freenect_open_device() * should open. This will not affect devices which have already been * opened. The default behavior, should you choose not to call this * function at all, is to open all supported subdevices - motor, cameras, * and audio, if supported on the platform. * * @param ctx Context to set future subdevice selection for * @param subdevs Flags representing the subdevices to select */ FREENECTAPI void freenect_select_subdevices(freenect_context *ctx, freenect_device_flags subdevs); /** * Returns the devices that are enabled after calls to freenect_open_device() * On newer kinects the motor and audio are automatically disabled for now * * @param ctx Context to set future subdevice selection for * @return Flags representing the subdevices that were actually opened (see freenect_device_flags) */ FREENECTAPI freenect_device_flags freenect_enabled_subdevices(freenect_context *ctx); /** * Opens a kinect device via a context. Index specifies the index of * the device on the current state of the bus. Bus resets may cause * indexes to shift. * * @param ctx Context to open device through * @param dev Device structure to assign opened device to * @param index Index of the device on the bus * * @return 0 on success, < 0 on error */ FREENECTAPI int freenect_open_device(freenect_context *ctx, freenect_device **dev, int index); /** * Opens a kinect device (via a context) associated with a particular camera * subdevice serial number. This function will fail if no device with a * matching serial number is found. * * @param ctx Context to open device through * @param dev Device structure to assign opened device to * @param camera_serial Null-terminated ASCII string containing the serial number of the camera subdevice in the device to open * * @return 0 on success, < 0 on error */ FREENECTAPI int freenect_open_device_by_camera_serial(freenect_context *ctx, freenect_device **dev, const char* camera_serial); /** * Closes a device that is currently open * * @param dev Device to close * * @return 0 on success */ FREENECTAPI int freenect_close_device(freenect_device *dev); /** * Set the device user data, for passing generic information into * callbacks * * @param dev Device to attach user data to * @param user User data to attach */ FREENECTAPI void freenect_set_user(freenect_device *dev, void *user); /** * Retrieve the pointer to user data from the device struct * * @param dev Device from which to get user data * * @return Pointer to user data */ FREENECTAPI void *freenect_get_user(freenect_device *dev); /// Typedef for depth image received event callbacks typedef void (*freenect_depth_cb)(freenect_device *dev, void *depth, uint32_t timestamp); /// Typedef for video image received event callbacks typedef void (*freenect_video_cb)(freenect_device *dev, void *video, uint32_t timestamp); /** * Set callback for depth information received event * * @param dev Device to set callback for * @param cb Function pointer for processing depth information */ FREENECTAPI void freenect_set_depth_callback(freenect_device *dev, freenect_depth_cb cb); /** * Set callback for video information received event * * @param dev Device to set callback for * @param cb Function pointer for processing video information */ FREENECTAPI void freenect_set_video_callback(freenect_device *dev, freenect_video_cb cb); /** * Set the buffer to store depth information to. Size of buffer is * dependant on depth format. See FREENECT_DEPTH_*_SIZE defines for * more information. * * @param dev Device to set depth buffer for. * @param buf Buffer to store depth information to. * * @return 0 on success, < 0 on error */ FREENECTAPI int freenect_set_depth_buffer(freenect_device *dev, void *buf); /** * Set the buffer to store depth information to. Size of buffer is * dependant on video format. See FREENECT_VIDEO_*_SIZE defines for * more information. * * @param dev Device to set video buffer for. * @param buf Buffer to store video information to. * * @return 0 on success, < 0 on error */ FREENECTAPI int freenect_set_video_buffer(freenect_device *dev, void *buf); /** * Start the depth information stream for a device. * * @param dev Device to start depth information stream for. * * @return 0 on success, < 0 on error */ FREENECTAPI int freenect_start_depth(freenect_device *dev); /** * Start the video information stream for a device. * * @param dev Device to start video information stream for. * * @return 0 on success, < 0 on error */ FREENECTAPI int freenect_start_video(freenect_device *dev); /** * Stop the depth information stream for a device * * @param dev Device to stop depth information stream on. * * @return 0 on success, < 0 on error */ FREENECTAPI int freenect_stop_depth(freenect_device *dev); /** * Stop the video information stream for a device * * @param dev Device to stop video information stream on. * * @return 0 on success, < 0 on error */ FREENECTAPI int freenect_stop_video(freenect_device *dev); /** * Updates the accelerometer state using a blocking control message * call. * * @param dev Device to get accelerometer data from * * @return 0 on success, < 0 on error. Accelerometer data stored to * device struct. */ FREENECTAPI int freenect_update_tilt_state(freenect_device *dev); /** * Retrieve the tilt state from a device * * @param dev Device to retrieve tilt state from * * @return The tilt state struct of the device */ FREENECTAPI freenect_raw_tilt_state* freenect_get_tilt_state(freenect_device *dev); /** * Return the tilt state, in degrees with respect to the horizon * * @param state The tilt state struct from a device * * @return Current degree of tilt of the device */ FREENECTAPI double freenect_get_tilt_degs(freenect_raw_tilt_state *state); /** * Set the tilt state of the device, in degrees with respect to the * horizon. Uses blocking control message call to update * device. Function return does not reflect state of device, device * may still be moving to new position after the function returns. Use * freenect_get_tilt_status() to find current movement state. * * @param dev Device to set tilt state * @param angle Angle the device should tilt to * * @return 0 on success, < 0 on error. */ FREENECTAPI int freenect_set_tilt_degs(freenect_device *dev, double angle); /** * Return the movement state of the tilt motor (moving, stopped, etc...) * * @param state Raw state struct to get the tilt status code from * * @return Status code of the tilt device. See * freenect_tilt_status_code enum for more info. */ FREENECTAPI freenect_tilt_status_code freenect_get_tilt_status(freenect_raw_tilt_state *state); /** * Set the state of the LED. Uses blocking control message call to * update device. * * @param dev Device to set the LED state * @param option LED state to set on device. See freenect_led_options enum. * * @return 0 on success, < 0 on error */ FREENECTAPI int freenect_set_led(freenect_device *dev, freenect_led_options option); /** * Get the axis-based gravity adjusted accelerometer state, as laid * out via the accelerometer data sheet, which is available at * * http://www.kionix.com/Product%20Sheets/KXSD9%20Product%20Brief.pdf * * @param state State to extract accelerometer data from * @param x Stores X-axis accelerometer state * @param y Stores Y-axis accelerometer state * @param z Stores Z-axis accelerometer state */ FREENECTAPI void freenect_get_mks_accel(freenect_raw_tilt_state *state, double* x, double* y, double* z); /** * Get the number of video camera modes supported by the driver. This includes both RGB and IR modes. * * @return Number of video modes supported by the driver */ FREENECTAPI int freenect_get_video_mode_count(); /** * Get the frame descriptor of the nth supported video mode for the * video camera. * * @param mode_num Which of the supported modes to return information about * * @return A freenect_frame_mode describing the nth video mode */ FREENECTAPI freenect_frame_mode freenect_get_video_mode(int mode_num); /** * Get the frame descriptor of the current video mode for the specified * freenect device. * * @param dev Which device to return the currently-set video mode for * * @return A freenect_frame_mode describing the current video mode of the specified device */ FREENECTAPI freenect_frame_mode freenect_get_current_video_mode(freenect_device *dev); /** * Convenience function to return a mode descriptor matching the * specified resolution and video camera pixel format, if one exists. * * @param res Resolution desired * @param fmt Pixel format desired * * @return A freenect_frame_mode that matches the arguments specified, if such a valid mode exists; otherwise, an invalid freenect_frame_mode. */ FREENECTAPI freenect_frame_mode freenect_find_video_mode(freenect_resolution res, freenect_video_format fmt); /** * Sets the current video mode for the specified device. If the * freenect_frame_mode specified is not one provided by the driver * e.g. from freenect_get_video_mode() or freenect_find_video_mode() * then behavior is undefined. The current video mode cannot be * changed while streaming is active. * * @param dev Device for which to set the video mode * @param mode Frame mode to set * * @return 0 on success, < 0 if error */ FREENECTAPI int freenect_set_video_mode(freenect_device* dev, freenect_frame_mode mode); /** * Get the number of depth camera modes supported by the driver. This includes both RGB and IR modes. * * @return Number of depth modes supported by the driver */ FREENECTAPI int freenect_get_depth_mode_count(); /** * Get the frame descriptor of the nth supported depth mode for the * depth camera. * * @param mode_num Which of the supported modes to return information about * * @return A freenect_frame_mode describing the nth depth mode */ FREENECTAPI freenect_frame_mode freenect_get_depth_mode(int mode_num); /** * Get the frame descriptor of the current depth mode for the specified * freenect device. * * @param dev Which device to return the currently-set depth mode for * * @return A freenect_frame_mode describing the current depth mode of the specified device */ FREENECTAPI freenect_frame_mode freenect_get_current_depth_mode(freenect_device *dev); /** * Convenience function to return a mode descriptor matching the * specified resolution and depth camera pixel format, if one exists. * * @param res Resolution desired * @param fmt Pixel format desired * * @return A freenect_frame_mode that matches the arguments specified, if such a valid mode exists; otherwise, an invalid freenect_frame_mode. */ FREENECTAPI freenect_frame_mode freenect_find_depth_mode(freenect_resolution res, freenect_depth_format fmt); /** * Sets the current depth mode for the specified device. The mode * cannot be changed while streaming is active. * * @param dev Device for which to set the depth mode * @param mode Frame mode to set * * @return 0 on success, < 0 if error */ FREENECTAPI int freenect_set_depth_mode(freenect_device* dev, const freenect_frame_mode mode); #ifdef __cplusplus } #endif #endif // ================================================ FILE: libs/libfreenect/platform/linux/udev/51-kinect.rules ================================================ # ATTR{product}=="Xbox NUI Motor" SUBSYSTEM=="usb", ATTR{idVendor}=="045e", ATTR{idProduct}=="02b0", MODE="0666" # ATTR{product}=="Xbox NUI Audio" SUBSYSTEM=="usb", ATTR{idVendor}=="045e", ATTR{idProduct}=="02ad", MODE="0666" # ATTR{product}=="Xbox NUI Camera" SUBSYSTEM=="usb", ATTR{idVendor}=="045e", ATTR{idProduct}=="02ae", MODE="0666" ================================================ FILE: libs/libfreenect/platform/linux/udev/README ================================================ Simply place this file in /etc/udev/rules.d and you'll no longer need to run your apps as root. ================================================ FILE: libs/libfreenect/platform/windows/README.TXT ================================================ ====================================================================================== SUMMARY ******* 1) libfreenect: incompatibilities with Visual C++ This section is here merely for historical reasons. All of the issues documented here were already fixed in the libfreenect repository and CMake should be able to produce a project that is ready to build libfreenect in Windows with Visual Studio. Consider browsing this section if experiencing compilation issues under different platforms, compilers and/or IDEs. 2) libusb-1.0 vs. libusbemu: Issues and Concept The current port of libfreenect for Windows uses a libusb-1.0 emulation layer since a proper port of libusb-1.0 for Windows is not yet available. Such emulation layer allows Windows development to keep in sync with the official development branch of libfreenect, without the need of dedicated drivers/implementations. This section discusses why and how the current libfreenect Windows port moved in this direction. 3) libusbemu: Tips, Hints and Best Practices The current status of libusbemu is quite reliable under normal usage circumstances, but by no means stable: caution is advised. This section provides some guidelines to avoid potential pitfalls and keep the application running safely under Windows. They are simple and natural to follow, so they should not impose any special design considerations for the application. 4) Overall performance of libfreenect in Windows The current Windows port of libfreenect has some performance overhead over other platforms and dedicated Win32 driver implementations due to the libusbemu module. This section contains a benchmark scenario and a also a discussion on the results. In short, the overhead is negligible and should not prevent anyone from using it. ====================================================================================== 1) libfreenect source code: incompatibilities with Visual C++ ********************************************************** ---------------------------------------------------------------------------------- Language issues: The Microsoft C compiler does not implement all the C99 standard. ---------------------------------------------------------------------------------- An attempt to compile the current libfreenect with Visual C++ will trigger a lot of errors. A simple workaround is to tell Visual Studio to force compilation all the ".c" files within the project using the C++ compiler: Project >> Properties >> C/C++ >> Advanced >> Compile As: Compile as C++ Code (/TP) This will get rid of most errors, except those regarding implicit pointer casts. Here are a few examples of such implicit pointer casts from within libfreenect: tilt.c dev->raw_state.tilt_status = buf[9]; core.c *ctx = malloc(sizeof(freenect_context)); cameras.c strm->raw_buf = strm->proc_buf; It seems that it is not possible to force Visual C++ to perform such implicit casts (if anyone knows how, please share! :-) Such implicit casts then have to be made explicit: dev->raw_state.tilt_status = (freenect_tilt_status_code)buf[9]; *ctx = (freenect_context*)malloc(sizeof(freenect_context)); strm->raw_buf = (uint8_t*)strm->proc_buf; Fortunately, they are not many, and it can be done in a couple of minutes. This should impose a minimal burden to the Win32 repository maintainers. NOTE: Sometimes, even when "Compile as C++ Code" is specified, the build will insist on using the C-compiler to compile .c files (this happens with MSVC versions prior to 2010). To work around this, just set the "Compile As" to "Default", click "Apply" and then set it back to "Compile as C++ Code (/TP)", click "Apply" and then "OK". Another problem is that Visual C++ does not offer the and headers. However, they are pretty simple to be emulated; the will actually be a dummy header because C++ already defines the "bool" type semantics, while the has to simply #include and define the "ssize_t" type. The implementation of these headers are located at: "libfreenect\platform\windows" NOTE: MSVC versions prior to 2010 do not provide the header. An ISO C9x compilant header for such MSVC versions can be found at the wikipedia entry of stdint.h (at the "External links" section of the page): http://en.wikipedia.org/wiki/Stdint.h or directly through this link: http://msinttypes.googlecode.com/svn/trunk/stdint.h A copy of such header will be also provided within libfreenect in the future. The "freenect_internal.h" makes use of GCC's "__attribute__" keyword, and there is no such a thing in Visual C++. Fortunately, this header is not exposed for the library user and is just required during the library build. There are a few simple solutions for this issue: a) remove this keyword or b) define a dummy macro for it. Another issue is that since all .c files were forced to be compiled as C++ code, the "libfreenect.h" header no longer requires the "#ifdef __cpluscplus extern C" idiom. Commenting out this guard will do the trick, but better checking is possible. ----------------------------------------------------------------------------------- Library issues: libfreenect uses libusb-1.0 which is not yet available for Windows. ----------------------------------------------------------------------------------- The final issue is regarding the default USB back-end used by libfreenect, libusb-1.0, which is not yet available for Windows. This restriction forces the Windows port of libfreenect to implement its own back-end, which then splits the Windows port from the main development branch of libfreenect as new device features are reverse-engineered and added to the library. Fortunately, such restriction can be alleviated through the libusb-1.0 API "emulator" and keep the Windows port in sync with the current status of libfreenect. More on this subject in the following section. ====================================================================================== 2) libusb-1.0 vs. libusbemu: Issues and Concept ******************************************** The libfreenect uses libusb-1.0 as its default USB back-end to communicate with Kinect but libusb-1.0 is not yet available for Windows. The current libusb-1.0 implementation for Windows is experimental and uses WinUSB as its USB back-end. Unfortunately, WinUSB does not support isochronous transfers, rendering it useless for Kinect purposes. However, all is not lost since the latest version of libusb-win32 has support for the isochronous transfers imposed by Kinect. There are issues too: libusb-win32 is based on the old libusb-0.1 API which is incompatible with the libusb-1.0 API. Some of the initial efforts to port libfreenect to Windows were based on the libusb-win32, being Zephod's Win32 Kinect driver prototype a well-known instance: http://ajaxorg.posterous.com/kinect-driver-for-windows-prototype The problem with a dedicated libusb-win32 driver implementation is that it has to be maintained separately from the current libfreenect development. As new Kinect features are reverse-engineered and implemented in the official libfreenect branch, the Windows port requires additional maintenance to keep it in sync with the newest updates, even when such updates don't involve USB communication at all. One could argue that libfreenect should abandon the use of libusb-1.0 and just adopt the old libusb-0.1 instead, since it is has support in a wider range of platforms... This is out of question! First of all, libusb-0.1 still exists for legacy reasons, and it is highly recommended to move to the new API if possible. Moreover, libusb-0.1 does not have built-in support for asynchronous transfers, which would force libfreenect to hold and manage threads internally, which would then lead to a whole set of new issues that are prone to hurt performance, maintainability and portability. Fortunately, libfreenect only requires a small portion of the libusb-1.0 API, and such subset can be emulated, at some extent, through what is provided from libusb-win32. As a result, the burden of maintaining dedicated development branches for a Windows port is eliminated and the port can keep in synch with updates made in the libfreenect. One may now ask: how can libusb-win32 be of any help if it is based on the libusb-0.1 API, which has no support for asynchronous transfers? Well, that's because it happens that libusb-win32 is more than just a Win32 build of libusb-0.1: it is a special branch of the 0.1 API that extends it to allow asynchronous transfers. The normal execution flow of libfreenect is something like this: Application <-> libfreenect <-> libusb-1.0 <-> Kinect In Windows, the flow is as follows: Application <-> libfreenect <-> libusb-1.0-emu <-> libusb-win32 <-> Kinect The source code of the emulation layer is available at: libfreenect\platform\windows\libusb10emu\libusb-1.0 Keep in mind that libusb-win32 is still required, and can be downloaded from: http://sourceforge.net/apps/trac/libusb-win32/wiki The latest snapshots are recommended, obtained directly from this URL: http://sourceforge.net/projects/libusb-win32/files/libusb-win32-snapshots Emulation of libusb-1.0 on top of libusb-win32 is not trivial, in special because of some Windows-specific issues. There will be performance overhead, but as discussed in the "Overall Performance" section, this should not hurt the application performance at significant levels. Moreover, although the libusbemu is currently quite reliable, it is by no means stable and on par to the real libusb-1.0 semantics. Caution is advised, so be sure to read the following section for some usage considerations. The libusbemu sits completely hidden behind libfreenect: the application does not need to worry or call any special functions. In fact the application is not even aware that libusbemu exists. ====================================================================================== 3) libusbemu: Tips, Hints and Best Practices ***************************************** --------------------------------------------------------------- TIP: Trigger the Fail Guard if the system becomes unresponsive. --------------------------------------------------------------- Since libusbemu is quite experimental, there is a fail guard within it. If for some reason your system renders unresponsive, try focusing any window of the application and hold [CTRL] + [ALT] for a while. This will trigger a special synchronization event within the libusbemu which will interrupt the execution of any internal thread of libusbemu and prompt a message box to the user asking for action. You can either resume execution (if you unintentionally pressed [CTRL] + [ALT] in the console window) or abort libusbemu. The fail guard will never trigger if there is no incoming video or depth streams. The Fail Guard is important since in such situations one would most likely be forced to shutdown the computer (in a not so graceful way, by holding the power button!). ---------------------------------------- TIP: Only use a single freenect context. ---------------------------------------- Multiple freenect contexts should be no problem in the future. Having more than one device attached to the same context should work fine, but no tests were made so far. ----------------------------------------------------- TIP: Yield the rendering thread after each iteration. ----------------------------------------------------- In case your application performs direct rendering (OpenGL, Direct3D), it is highly recommended to yield the rendering thread after finishing rendering each frame. This can alleviate a lot of CPU usage. The ideal place for this yield is right after the swap-buffers call (SwapBuffers(), glutSwapBuffers(), Present(), etc). The best way to yield is by calling Sleep(1). Note that Sleep(0) is also possible, but the former is more "democratic". Note that some graphics drivers or platforms may already yield after swap-buffers. This seems to be the case with OpenGL NVIDIA drivers for Linux. In Windows, however, the same driver (version) does not yield. Maybe in Linux the "yielder" is not the driver itself, but the underlying implementation of glXSwapBuffers()... Anyway, when in doubt, it will not hurt to explicitly yield again. -------------------------------------------------------------------------------------- TIP: Perform stream operations only in the thread that calls freenect_process_events() -------------------------------------------------------------------------------------- By stream operations I mean these: freenect_start_video() freenect_start_depth() freenect_stop_video() freenect_stop_depth() This tip seems to hold for other platforms as well, and it is respected through all of the official libfreenect examples and should be of no burden for the application. This may be due to the underlying semantics of the real libusb-1.0. Unfortunately, although libusb-1.0 has an excellent API documentation it lacks on a proper specification, thus difficulting the task of checking if this is indeed a restriction. In summary, a typical libfreenect application (check the official examples) will have a dedicated thread that calls freenect_process_events(). This function is responsible for querying and dispatching incoming streams (a.k.a. video and depth frames) from the Kinect device to the application (behind the scenes is a call the libusb-1.0 function libusb_handle_events()) and it is advised that any stream operation intended by the application (like switching to some different video format) should happen within the thread that calls the freenect_process_events(). The official libfreenect examples do this, so be sure to check their source code if confused. It is also advised to have only one thread calling freenect_handle_events(). Interestingly, some tests were made with both the libusb-1.0 and libusbemu and mixing stream operations in different threads seem to work well. However, no guarantees are given if executed in such a fashion. Again, libusb-1.0 has no specification document and if such behavior is really to be expected is a hard task to determine. ====================================================================================== 4) Overall performance of libfreenect in Windows ********************************************* --------------------------------------------- THIS SECTION IS OUTDATED, GOTTA REDO IT SOON! --------------------------------------------- Hardware: * Notebook * CPU: Intel Core2 Duo 32bit [T7250] @ 2.0GHz * RAM: 4GB RAM * GPU: GeForce 8600M GT 256MB VRAM Task: display of simultaneous RGB (Bayer-to-RGB) and depth streams (16bit unpadded) on the screen through OpenGL textures. Application source code is identical for all tests (except for the Zephod's version that required some interface adaptation). Results: performance measurements refer to the average frame time (one loop iteration). Linux: Ubuntu Notebook 10.10 -- gcc 4.4.5 * Debug: 1.22ms | CPU @ 77% | video @ 30Hz | depth @ 30Hz * Release: 1.15ms | CPU @ 72% | video @ 30Hz | depth @ 30Hz Win32: Windows 7 Enterprise 32bit -- VC++ (Professional) 2010 1) libfreenect with libusbemu: * Debug: 2.44ms | CPU @ 75% | video @ 30Hz | depth @ 30Hz * Release: 1.93ms | CPU @ 63% | video @ 30Hz | depth @ 30Hz 2) Zephod's dedicated driver (V16): * Debug: 2.87ms | CPU @ 82% | video @ 30Hz | depth @ 30Hz * Release: 2.55ms | CPU @ 77% | video @ 30Hz | depth @ 30Hz Remarks: Debug builds account for the library itself being built in Debug mode (libfreenect or Zaphod's driver, whichever applies). Release builds also imply that the program was started without any debug information embedded. In Windows, time was measured through the Win32 exclusive QueryPerformanceFrequency() and QueryPerformanceCounter() routines. In Linux, the measurement was performed via POSIX gettime() function. A Win32 MinGW-based (gcc/g++ 4.5.0) build of libfreenect with libusbemu using POSIX gettime() also yielded to nearly identical performance results than VC++ 2010 with Performance Counters. All of the Win32 performance results were also double-checked with Fraps. In Windows, a single thread yield call was placed after rendering each screen frame (as recommended in Item 3-3). In Linux, the graphics driver - possibly glXSwapBuffers itself - seems to be already yielding the rendering thread, and forcing it in the code did not incur into any extra impact on performance. Discussion: Even though there are no streaming frequency discrepancies between platforms, one may infer, just from the frame times, that Windows clearly has an overhead disadvantage. However, this does not hold true: there is still plenty of time for the application logic to run. For a steady 60FPS (~16.66ms per frame) real-time performance, a Release build using libusbemu in Windows would still have about 14.70ms per frame available for the client code, while in Linux the available time would be around 15.50ms, a 0.8ms overhead between the platforms. Such a small difference should not impose any special design considerations for the client code. Furthermore, note that the CPU usage in Windows tend to be lower than in Linux. The reason behind this difference is quite difficult to summarize here, but it is probably related to the way Windows and Linux perform asynchronous I/O in USB-mapped files. The interested reader is encouraged to refer to the following links: > http://www.unwesen.de/articles/waitformultipleobjects_considered_expensive > http://softwarecommunity.intel.com/articles/eng/2807.htm > http://software.intel.com/en-us/blogs/2006/10/19/why-windows-threads-are-better-than-posix-threads/ The memory consumption overhead of libusbemu in Windows is negligible, and as far as the VC++ memory leak detection goes, libusbemu has no memory leaks. ====================================================================================== ================================================ FILE: libs/libfreenect/platform/windows/inf/xbox nui audio/libusb-win32-bin-README.txt ================================================ libusb-win32-bin v1.2.2.0 (10/02/2010) - [Package Information] ALL ARCHITECTURES: x86\libusb0_x86.dll: x86 32-bit library. Must be renamed to libusb0.dll On 64 bit, Installs to Windows\syswow64\libusb0.dll. On 32 bit, Installs to Windows\system32\libusb0.dll. x86\inf-wizard.exe: inf-wizard application with embedded libusb-win32 v1.2.2.0 binaries. X86 ONLY ARCHITECTURES: x86\libusb0.sys: x86 32-bit driver. Installs to Windows\system32\drivers\libusb0.sys AMD64-INTEL64 ONLY ARCHITECTURES: amd64\libusb0.sys: x64 64-bit driver. Installs to Windows\system32\drivers\libusb0.sys amd64\libusb0.dll: x64 64-bit library. Installs to Windows\system32\libusb0.dll IA64 ONLY ARCHITECTURES: ia64\libusb0.sys: IA64 64-bit driver. Installs to Windows\system32\drivers\libusb0.sys ia64\libusb0.dll: IA64 64-bit library. Installs to Windows\system32\libusb0.dll ================================================ FILE: libs/libfreenect/platform/windows/inf/xbox nui audio/license/libusb-win32/installer_license.txt ================================================ Copyright (c) 2002-2004 Stephan Meyer, Copyright (c) 2000-2004 Johannes Erdfelt, Copyright (c) 2000-2004 Thomas Sailer, Copyright (c) 2010 Travis Robinson, This software is distributed under the following licenses: Driver: GNU General Public License (GPL) Library, Test Files, Installer: GNU Lesser General Public License (LGPL) *********************************************************************** GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The GNU General Public License is a free, copyleft license for software and other kinds of works. The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. To protect your rights, we need to prevent others from denying you these rights or asking you to surrender the rights. Therefore, you have certain responsibilities if you distribute copies of the software, or if you modify it: responsibilities to respect the freedom of others. For example, if you distribute copies of such a program, whether gratis or for a fee, you must pass on to the recipients the same freedoms that you received. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. Developers that use the GNU GPL protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License giving you legal permission to copy, distribute and/or modify it. For the developers' and authors' protection, the GPL clearly explains that there is no warranty for this free software. For both users' and authors' sake, the GPL requires that modified versions be marked as changed, so that their problems will not be attributed erroneously to authors of previous versions. Some devices are designed to deny users access to install or run modified versions of the software inside them, although the manufacturer can do so. This is fundamentally incompatible with the aim of protecting users' freedom to change the software. The systematic pattern of such abuse occurs in the area of products for individuals to use, which is precisely where it is most unacceptable. Therefore, we have designed this version of the GPL to prohibit the practice for those products. If such problems arise substantially in other domains, we stand ready to extend this provision to those domains in future versions of the GPL, as needed to protect the freedom of users. Finally, every program is threatened constantly by software patents. States should not allow patents to restrict development and use of software on general-purpose computers, but in those that do, we wish to avoid the special danger that patents applied to a free program could make it effectively proprietary. To prevent this, the GPL assures that patents cannot be used to render the program non-free. The precise terms and conditions for copying, distribution and modification follow. TERMS AND CONDITIONS 0. Definitions. "This License" refers to version 3 of the GNU General Public License. "Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. "The Program" refers to any copyrightable work licensed under this License. Each licensee is addressed as "you". "Licensees" and "recipients" may be individuals or organizations. To "modify" a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a "modified version" of the earlier work or a work "based on" the earlier work. A "covered work" means either the unmodified Program or a work based on the Program. To "propagate" a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well. To "convey" a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying. An interactive user interface displays "Appropriate Legal Notices" to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion. 1. Source Code. The "source code" for a work means the preferred form of the work for making modifications to it. "Object code" means any non-source form of a work. A "Standard Interface" means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language. The "System Libraries" of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A "Major Component", in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it. The "Corresponding Source" for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work. The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source. The Corresponding Source for a work in source code form is that same work. 2. Basic Permissions. All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law. You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you. Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary. 3. Protecting Users' Legal Rights From Anti-Circumvention Law. No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures. When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures. 4. Conveying Verbatim Copies. You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program. You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee. 5. Conveying Modified Source Versions. You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions: a) The work must carry prominent notices stating that you modified it, and giving a relevant date. b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to "keep intact all notices". c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it. d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so. A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an "aggregate" if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate. 6. Conveying Non-Source Forms. You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways: a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange. b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge. c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b. d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements. e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d. A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work. A "User Product" is either (1) a "consumer product", which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, "normally used" refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product. "Installation Information" for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made. If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM). The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network. Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying. 7. Additional Terms. "Additional permissions" are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions. When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission. Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms: a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or d) Limiting the use for publicity purposes of names of licensors or authors of the material; or e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors. All other non-permissive additional terms are considered "further restrictions" within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying. If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms. Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way. 8. Termination. You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11). However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice. Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10. 9. Acceptance Not Required for Having Copies. You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so. 10. Automatic Licensing of Downstream Recipients. Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License. An "entity transaction" is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts. You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it. 11. Patents. A "contributor" is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's "contributor version". A contributor's "essential patent claims" are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, "control" includes the right to grant patent sublicenses in a manner consistent with the requirements of this License. Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version. In the following three paragraphs, a "patent license" is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To "grant" such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party. If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. "Knowingly relying" means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid. If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it. A patent license is "discriminatory" if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007. Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law. 12. No Surrender of Others' Freedom. If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. 13. Use with the GNU Affero General Public License. Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, section 13, concerning interaction through a network will apply to the combination as such. 14. Revised Versions of this License. The Free Software Foundation may publish revised and/or new versions of the GNU General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU General Public License, you may choose any version ever published by the Free Software Foundation. If the Program specifies that a proxy can decide which future versions of the GNU General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version. 15. Disclaimer of Warranty. THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 16. Limitation of Liability. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 17. Interpretation of Sections 15 and 16. If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . Also add information on how to contact you by electronic and paper mail. If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode: Copyright (C) This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, your program's commands might be different; for a GUI interface, you would use an "about box". You should also get your employer (if you work as a programmer) or school, if any, to sign a "copyright disclaimer" for the program, if necessary. For more information on this, and how to apply and follow the GNU GPL, see . The GNU General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. But first, please read . GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below. 0. Additional Definitions. As used herein, "this License" refers to version 3 of the GNU Lesser General Public License, and the "GNU GPL" refers to version 3 of the GNU General Public License. "The Library" refers to a covered work governed by this License, other than an Application or a Combined Work as defined below. An "Application" is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library. A "Combined Work" is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the "Linked Version". The "Minimal Corresponding Source" for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version. The "Corresponding Application Code" for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work. 1. Exception to Section 3 of the GNU GPL. You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL. 2. Conveying Modified Versions. If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version: a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy. 3. Object Code Incorporating Material from Library Header Files. The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the object code with a copy of the GNU GPL and this license document. 4. Combined Works. You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following: a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the Combined Work with a copy of the GNU GPL and this license document. c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document. d) Do one of the following: 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source. 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version. e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.) 5. Combined Libraries. You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License. b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 6. Revised Versions of the GNU Lesser General Public License. The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library. ================================================ FILE: libs/libfreenect/platform/windows/inf/xbox nui camera/libusb-win32-bin-README.txt ================================================ libusb-win32-bin v1.2.2.0 (10/02/2010) - [Package Information] ALL ARCHITECTURES: x86\libusb0_x86.dll: x86 32-bit library. Must be renamed to libusb0.dll On 64 bit, Installs to Windows\syswow64\libusb0.dll. On 32 bit, Installs to Windows\system32\libusb0.dll. x86\inf-wizard.exe: inf-wizard application with embedded libusb-win32 v1.2.2.0 binaries. X86 ONLY ARCHITECTURES: x86\libusb0.sys: x86 32-bit driver. Installs to Windows\system32\drivers\libusb0.sys AMD64-INTEL64 ONLY ARCHITECTURES: amd64\libusb0.sys: x64 64-bit driver. Installs to Windows\system32\drivers\libusb0.sys amd64\libusb0.dll: x64 64-bit library. Installs to Windows\system32\libusb0.dll IA64 ONLY ARCHITECTURES: ia64\libusb0.sys: IA64 64-bit driver. Installs to Windows\system32\drivers\libusb0.sys ia64\libusb0.dll: IA64 64-bit library. Installs to Windows\system32\libusb0.dll ================================================ FILE: libs/libfreenect/platform/windows/inf/xbox nui camera/license/libusb-win32/installer_license.txt ================================================ Copyright (c) 2002-2004 Stephan Meyer, Copyright (c) 2000-2004 Johannes Erdfelt, Copyright (c) 2000-2004 Thomas Sailer, Copyright (c) 2010 Travis Robinson, This software is distributed under the following licenses: Driver: GNU General Public License (GPL) Library, Test Files, Installer: GNU Lesser General Public License (LGPL) *********************************************************************** GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The GNU General Public License is a free, copyleft license for software and other kinds of works. The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. To protect your rights, we need to prevent others from denying you these rights or asking you to surrender the rights. Therefore, you have certain responsibilities if you distribute copies of the software, or if you modify it: responsibilities to respect the freedom of others. For example, if you distribute copies of such a program, whether gratis or for a fee, you must pass on to the recipients the same freedoms that you received. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. Developers that use the GNU GPL protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License giving you legal permission to copy, distribute and/or modify it. For the developers' and authors' protection, the GPL clearly explains that there is no warranty for this free software. For both users' and authors' sake, the GPL requires that modified versions be marked as changed, so that their problems will not be attributed erroneously to authors of previous versions. Some devices are designed to deny users access to install or run modified versions of the software inside them, although the manufacturer can do so. This is fundamentally incompatible with the aim of protecting users' freedom to change the software. The systematic pattern of such abuse occurs in the area of products for individuals to use, which is precisely where it is most unacceptable. Therefore, we have designed this version of the GPL to prohibit the practice for those products. If such problems arise substantially in other domains, we stand ready to extend this provision to those domains in future versions of the GPL, as needed to protect the freedom of users. Finally, every program is threatened constantly by software patents. States should not allow patents to restrict development and use of software on general-purpose computers, but in those that do, we wish to avoid the special danger that patents applied to a free program could make it effectively proprietary. To prevent this, the GPL assures that patents cannot be used to render the program non-free. The precise terms and conditions for copying, distribution and modification follow. TERMS AND CONDITIONS 0. Definitions. "This License" refers to version 3 of the GNU General Public License. "Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. "The Program" refers to any copyrightable work licensed under this License. Each licensee is addressed as "you". "Licensees" and "recipients" may be individuals or organizations. To "modify" a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a "modified version" of the earlier work or a work "based on" the earlier work. A "covered work" means either the unmodified Program or a work based on the Program. To "propagate" a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well. To "convey" a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying. An interactive user interface displays "Appropriate Legal Notices" to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion. 1. Source Code. The "source code" for a work means the preferred form of the work for making modifications to it. "Object code" means any non-source form of a work. A "Standard Interface" means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language. The "System Libraries" of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A "Major Component", in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it. The "Corresponding Source" for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work. The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source. The Corresponding Source for a work in source code form is that same work. 2. Basic Permissions. All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law. You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you. Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary. 3. Protecting Users' Legal Rights From Anti-Circumvention Law. No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures. When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures. 4. Conveying Verbatim Copies. You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program. You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee. 5. Conveying Modified Source Versions. You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions: a) The work must carry prominent notices stating that you modified it, and giving a relevant date. b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to "keep intact all notices". c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it. d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so. A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an "aggregate" if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate. 6. Conveying Non-Source Forms. You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways: a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange. b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge. c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b. d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements. e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d. A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work. A "User Product" is either (1) a "consumer product", which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, "normally used" refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product. "Installation Information" for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made. If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM). The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network. Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying. 7. Additional Terms. "Additional permissions" are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions. When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission. Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms: a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or d) Limiting the use for publicity purposes of names of licensors or authors of the material; or e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors. All other non-permissive additional terms are considered "further restrictions" within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying. If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms. Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way. 8. Termination. You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11). However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice. Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10. 9. Acceptance Not Required for Having Copies. You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so. 10. Automatic Licensing of Downstream Recipients. Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License. An "entity transaction" is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts. You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it. 11. Patents. A "contributor" is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's "contributor version". A contributor's "essential patent claims" are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, "control" includes the right to grant patent sublicenses in a manner consistent with the requirements of this License. Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version. In the following three paragraphs, a "patent license" is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To "grant" such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party. If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. "Knowingly relying" means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid. If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it. A patent license is "discriminatory" if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007. Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law. 12. No Surrender of Others' Freedom. If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. 13. Use with the GNU Affero General Public License. Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, section 13, concerning interaction through a network will apply to the combination as such. 14. Revised Versions of this License. The Free Software Foundation may publish revised and/or new versions of the GNU General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU General Public License, you may choose any version ever published by the Free Software Foundation. If the Program specifies that a proxy can decide which future versions of the GNU General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version. 15. Disclaimer of Warranty. THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 16. Limitation of Liability. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 17. Interpretation of Sections 15 and 16. If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . Also add information on how to contact you by electronic and paper mail. If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode: Copyright (C) This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, your program's commands might be different; for a GUI interface, you would use an "about box". You should also get your employer (if you work as a programmer) or school, if any, to sign a "copyright disclaimer" for the program, if necessary. For more information on this, and how to apply and follow the GNU GPL, see . The GNU General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. But first, please read . GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below. 0. Additional Definitions. As used herein, "this License" refers to version 3 of the GNU Lesser General Public License, and the "GNU GPL" refers to version 3 of the GNU General Public License. "The Library" refers to a covered work governed by this License, other than an Application or a Combined Work as defined below. An "Application" is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library. A "Combined Work" is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the "Linked Version". The "Minimal Corresponding Source" for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version. The "Corresponding Application Code" for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work. 1. Exception to Section 3 of the GNU GPL. You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL. 2. Conveying Modified Versions. If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version: a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy. 3. Object Code Incorporating Material from Library Header Files. The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the object code with a copy of the GNU GPL and this license document. 4. Combined Works. You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following: a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the Combined Work with a copy of the GNU GPL and this license document. c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document. d) Do one of the following: 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source. 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version. e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.) 5. Combined Libraries. You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License. b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 6. Revised Versions of the GNU Lesser General Public License. The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library. ================================================ FILE: libs/libfreenect/platform/windows/inf/xbox nui motor/libusb-win32-bin-README.txt ================================================ libusb-win32-bin v1.2.2.0 (10/02/2010) - [Package Information] ALL ARCHITECTURES: x86\libusb0_x86.dll: x86 32-bit library. Must be renamed to libusb0.dll On 64 bit, Installs to Windows\syswow64\libusb0.dll. On 32 bit, Installs to Windows\system32\libusb0.dll. x86\inf-wizard.exe: inf-wizard application with embedded libusb-win32 v1.2.2.0 binaries. X86 ONLY ARCHITECTURES: x86\libusb0.sys: x86 32-bit driver. Installs to Windows\system32\drivers\libusb0.sys AMD64-INTEL64 ONLY ARCHITECTURES: amd64\libusb0.sys: x64 64-bit driver. Installs to Windows\system32\drivers\libusb0.sys amd64\libusb0.dll: x64 64-bit library. Installs to Windows\system32\libusb0.dll IA64 ONLY ARCHITECTURES: ia64\libusb0.sys: IA64 64-bit driver. Installs to Windows\system32\drivers\libusb0.sys ia64\libusb0.dll: IA64 64-bit library. Installs to Windows\system32\libusb0.dll ================================================ FILE: libs/libfreenect/platform/windows/inf/xbox nui motor/license/libusb-win32/installer_license.txt ================================================ Copyright (c) 2002-2004 Stephan Meyer, Copyright (c) 2000-2004 Johannes Erdfelt, Copyright (c) 2000-2004 Thomas Sailer, Copyright (c) 2010 Travis Robinson, This software is distributed under the following licenses: Driver: GNU General Public License (GPL) Library, Test Files, Installer: GNU Lesser General Public License (LGPL) *********************************************************************** GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The GNU General Public License is a free, copyleft license for software and other kinds of works. The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. To protect your rights, we need to prevent others from denying you these rights or asking you to surrender the rights. Therefore, you have certain responsibilities if you distribute copies of the software, or if you modify it: responsibilities to respect the freedom of others. For example, if you distribute copies of such a program, whether gratis or for a fee, you must pass on to the recipients the same freedoms that you received. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. Developers that use the GNU GPL protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License giving you legal permission to copy, distribute and/or modify it. For the developers' and authors' protection, the GPL clearly explains that there is no warranty for this free software. For both users' and authors' sake, the GPL requires that modified versions be marked as changed, so that their problems will not be attributed erroneously to authors of previous versions. Some devices are designed to deny users access to install or run modified versions of the software inside them, although the manufacturer can do so. This is fundamentally incompatible with the aim of protecting users' freedom to change the software. The systematic pattern of such abuse occurs in the area of products for individuals to use, which is precisely where it is most unacceptable. Therefore, we have designed this version of the GPL to prohibit the practice for those products. If such problems arise substantially in other domains, we stand ready to extend this provision to those domains in future versions of the GPL, as needed to protect the freedom of users. Finally, every program is threatened constantly by software patents. States should not allow patents to restrict development and use of software on general-purpose computers, but in those that do, we wish to avoid the special danger that patents applied to a free program could make it effectively proprietary. To prevent this, the GPL assures that patents cannot be used to render the program non-free. The precise terms and conditions for copying, distribution and modification follow. TERMS AND CONDITIONS 0. Definitions. "This License" refers to version 3 of the GNU General Public License. "Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. "The Program" refers to any copyrightable work licensed under this License. Each licensee is addressed as "you". "Licensees" and "recipients" may be individuals or organizations. To "modify" a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a "modified version" of the earlier work or a work "based on" the earlier work. A "covered work" means either the unmodified Program or a work based on the Program. To "propagate" a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well. To "convey" a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying. An interactive user interface displays "Appropriate Legal Notices" to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion. 1. Source Code. The "source code" for a work means the preferred form of the work for making modifications to it. "Object code" means any non-source form of a work. A "Standard Interface" means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language. The "System Libraries" of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A "Major Component", in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it. The "Corresponding Source" for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work. The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source. The Corresponding Source for a work in source code form is that same work. 2. Basic Permissions. All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law. You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you. Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary. 3. Protecting Users' Legal Rights From Anti-Circumvention Law. No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures. When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures. 4. Conveying Verbatim Copies. You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program. You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee. 5. Conveying Modified Source Versions. You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions: a) The work must carry prominent notices stating that you modified it, and giving a relevant date. b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to "keep intact all notices". c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it. d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so. A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an "aggregate" if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate. 6. Conveying Non-Source Forms. You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways: a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange. b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge. c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b. d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements. e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d. A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work. A "User Product" is either (1) a "consumer product", which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, "normally used" refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product. "Installation Information" for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made. If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM). The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network. Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying. 7. Additional Terms. "Additional permissions" are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions. When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission. Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms: a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or d) Limiting the use for publicity purposes of names of licensors or authors of the material; or e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors. All other non-permissive additional terms are considered "further restrictions" within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying. If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms. Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way. 8. Termination. You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11). However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice. Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10. 9. Acceptance Not Required for Having Copies. You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so. 10. Automatic Licensing of Downstream Recipients. Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License. An "entity transaction" is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts. You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it. 11. Patents. A "contributor" is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's "contributor version". A contributor's "essential patent claims" are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, "control" includes the right to grant patent sublicenses in a manner consistent with the requirements of this License. Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version. In the following three paragraphs, a "patent license" is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To "grant" such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party. If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. "Knowingly relying" means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid. If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it. A patent license is "discriminatory" if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007. Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law. 12. No Surrender of Others' Freedom. If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. 13. Use with the GNU Affero General Public License. Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, section 13, concerning interaction through a network will apply to the combination as such. 14. Revised Versions of this License. The Free Software Foundation may publish revised and/or new versions of the GNU General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU General Public License, you may choose any version ever published by the Free Software Foundation. If the Program specifies that a proxy can decide which future versions of the GNU General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version. 15. Disclaimer of Warranty. THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 16. Limitation of Liability. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 17. Interpretation of Sections 15 and 16. If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . Also add information on how to contact you by electronic and paper mail. If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode: Copyright (C) This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, your program's commands might be different; for a GUI interface, you would use an "about box". You should also get your employer (if you work as a programmer) or school, if any, to sign a "copyright disclaimer" for the program, if necessary. For more information on this, and how to apply and follow the GNU GPL, see . The GNU General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. But first, please read . GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below. 0. Additional Definitions. As used herein, "this License" refers to version 3 of the GNU Lesser General Public License, and the "GNU GPL" refers to version 3 of the GNU General Public License. "The Library" refers to a covered work governed by this License, other than an Application or a Combined Work as defined below. An "Application" is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library. A "Combined Work" is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the "Linked Version". The "Minimal Corresponding Source" for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version. The "Corresponding Application Code" for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work. 1. Exception to Section 3 of the GNU GPL. You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL. 2. Conveying Modified Versions. If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version: a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy. 3. Object Code Incorporating Material from Library Header Files. The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the object code with a copy of the GNU GPL and this license document. 4. Combined Works. You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following: a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the Combined Work with a copy of the GNU GPL and this license document. c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document. d) Do one of the following: 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source. 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version. e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.) 5. Combined Libraries. You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License. b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 6. Revised Versions of the GNU Lesser General Public License. The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library. ================================================ FILE: libs/libfreenect/platform/windows/libusb10emu/libusb-1.0/failguard.cpp ================================================ #include "failguard.h" #include "libusbemu_threads.h" int ThreadFailGuardProc(void* params); namespace libusbemu { namespace failguard { static volatile bool boTriggered (false); static QuickEvent hReaction; static volatile int nDecision (0); const bool Check() { if (failguard::boTriggered) return(true); // CTRL + ALT pressed? if ((GetKeyState(VK_CONTROL) & 0x8000) && (GetKeyState(VK_MENU) & 0x8000)) { // only one thread is allowed to activate the guard static QuickMutex mutexFailGuard; if (mutexFailGuard.TryEnter()) { if (!failguard::boTriggered) { failguard::hReaction.Reset(); failguard::boTriggered = true; new QuickThread(ThreadFailGuardProc, NULL, true); } mutexFailGuard.Leave(); } } return(failguard::boTriggered); } void WaitDecision() { failguard::hReaction.Wait(); } const bool Abort() { return(-1 == nDecision); } } } using namespace libusbemu::failguard; int ThreadFailGuardProc(void* params) { int user_option = MessageBoxA(GetDesktopWindow(), "The libusb_handle_events() fail guard of libusbemu was reached!\n" "This was caused by pressing and holding the [CTRL] + [ALT] keys.\n" "If it was unintentional, click Cancel to resume normal execution;\n" "otherwise, click OK to effectively terminate the thread (note that\n" "the host program might run abnormally after such termination).", "WARNING: libusbemu thread fail guard reached!", MB_ICONWARNING | MB_OKCANCEL); if (IDOK == user_option) nDecision = -1; else boTriggered = false; hReaction.Signal(); return(0); } ================================================ FILE: libs/libfreenect/platform/windows/libusb10emu/libusb-1.0/failguard.h ================================================ #ifndef LIBUSBEMU_FAIL_GUARD_H #define LIBUSBEMU_FAIL_GUARD_H namespace libusbemu { namespace failguard { const bool Check(); void WaitDecision(); const bool Abort(); } } #endif//LIBUSBEMU_FAIL_GUARD_H ================================================ FILE: libs/libfreenect/platform/windows/libusb10emu/libusb-1.0/libusb.h ================================================ /* * This file is part of the OpenKinect Project. http://www.openkinect.org * * Copyright (c) 2010 individual OpenKinect contributors. See the CONTRIB file * for details. * * This code is licensed to you under the terms of the Apache License, version * 2.0, or, at your option, the terms of the GNU General Public License, * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses, * or the following URLs: * http://www.apache.org/licenses/LICENSE-2.0 * http://www.gnu.org/licenses/gpl-2.0.txt * * If you redistribute this file in source form, modified or unmodified, you * may: * 1) Leave this header intact and distribute it under the same terms, * accompanying it with the APACHE20 and GPL20 files, or * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file * In all cases you must keep the copyright notice intact and include a copy * of the CONTRIB file. * * Binary distributions must follow the binary distribution requirements of * either License. */ #ifndef LIBUSB_1_0_INTERFACE_EMULATOR_FOR_LIBUSB_0_1_H #define LIBUSB_1_0_INTERFACE_EMULATOR_FOR_LIBUSB_0_1_H // This interface emulator requires the libusb-win32 v1.2.2.1 (snapshot) // or later. Prior win32 versions of the library were not conformal to // the official libusb-0.1 API naming convention // (e.g.: usb_device_t instead of usb_device) // One can either workaround prior releases to match the official naming // or upgrade to the libusb-win32 1.2.2.1 snapshot. #include #if defined(_MSC_VER) typedef unsigned __int8 uint8_t; typedef unsigned __int16 uint16_t; // We need struct timeval. #include #else #include #include #endif #define LIBUSBEMU 1 // guard to enable mix of compiler semantics (C/C++ calling C++); // this should allow builds under VC++ and gcc/g++ profiles #ifdef __cplusplus extern "C" { #endif // gotta use "_t" suffix here... struct libusb_context_t; // ...otherwise C++ complains with such typedef declaration typedef struct libusb_context_t libusb_context; int libusb_init(libusb_context** context); void libusb_exit(libusb_context* ctx); void libusb_set_debug(libusb_context *ctx, int level); struct libusb_device_t; typedef struct libusb_device_t libusb_device; ssize_t libusb_get_device_list(libusb_context* ctx, libusb_device*** list); void libusb_free_device_list(libusb_device** list, int unref_devices); int libusb_get_device_descriptor(libusb_device* dev, struct libusb_device_descriptor* desc); struct libusb_device_handle_t; typedef struct libusb_device_handle_t libusb_device_handle; int libusb_open(libusb_device* dev, libusb_device_handle** handle); void libusb_close(libusb_device_handle* dev_handle); int libusb_get_string_descriptor(libusb_device_handle *dev_handle, uint8_t desc_index, uint16_t langid, unsigned char *data, int length); int libusb_get_string_descriptor_ascii(libusb_device_handle *dev_handle, uint8_t desc_index, unsigned char *data, int length); int libusb_set_configuration(libusb_device_handle *dev, int configuration); int libusb_set_interface_alt_setting(libusb_device_handle *dev,int interface_number,int alternate_setting); int libusb_claim_interface(libusb_device_handle* dev, int interface_number); int libusb_release_interface(libusb_device_handle* dev, int interface_number); libusb_device_handle *libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id, uint16_t product_id); int libusb_control_transfer(libusb_device_handle* dev_handle, uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, unsigned char* data, uint16_t wLength, unsigned int timeout); int libusb_bulk_transfer(libusb_device_handle *dev_handle, unsigned char endpoint, unsigned char *data, int length, int *actual_length, unsigned int timeout); struct libusb_transfer* libusb_alloc_transfer(int iso_packets); void libusb_free_transfer(struct libusb_transfer* transfer); typedef void(*libusb_transfer_cb_fn)(struct libusb_transfer *transfer); void libusb_fill_iso_transfer(struct libusb_transfer* transfer, libusb_device_handle* dev_handle, unsigned char endpoint, unsigned char* buffer, int length, int num_iso_packets, libusb_transfer_cb_fn callback, void* user_data, unsigned int timeout); void libusb_set_iso_packet_lengths(struct libusb_transfer* transfer, unsigned int length); int libusb_submit_transfer(struct libusb_transfer* transfer); int libusb_cancel_transfer(struct libusb_transfer* transfer); int libusb_handle_events(libusb_context* ctx); // WORK IN PROGRESS... int libusb_handle_events_timeout(libusb_context* ctx, struct timeval* timeout); // the signature of libusb_device_descriptor is identical to usb_device_descriptor // which means that the below struct could be replaced by a typedef; however, that // would require #including in this header, polluting the scope and possibly // creating conflicts with existing declarations... struct libusb_device_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint16_t bcdUSB; uint8_t bDeviceClass; uint8_t bDeviceSubClass; uint8_t bDeviceProtocol; uint8_t bMaxPacketSize0; uint16_t idVendor; uint16_t idProduct; uint16_t bcdDevice; uint8_t iManufacturer; uint8_t iProduct; uint8_t iSerialNumber; uint8_t bNumConfigurations; }; enum libusb_endpoint_direction { LIBUSB_ENDPOINT_IN = 0x80, LIBUSB_ENDPOINT_OUT = 0x00 }; enum libusb_transfer_status { LIBUSB_TRANSFER_COMPLETED, LIBUSB_TRANSFER_ERROR, LIBUSB_TRANSFER_TIMED_OUT, LIBUSB_TRANSFER_CANCELLED, LIBUSB_TRANSFER_STALL, LIBUSB_TRANSFER_NO_DEVICE, LIBUSB_TRANSFER_OVERFLOW }; enum libusb_transfer_type { LIBUSB_TRANSFER_TYPE_CONTROL = 0, LIBUSB_TRANSFER_TYPE_ISOCHRONOUS = 1, LIBUSB_TRANSFER_TYPE_BULK = 2, LIBUSB_TRANSFER_TYPE_INTERRUPT = 3, }; enum libusb_transfer_flags { LIBUSB_TRANSFER_SHORT_NOT_OK = 1<<0, LIBUSB_TRANSFER_FREE_BUFFER = 1<<1, LIBUSB_TRANSFER_FREE_TRANSFER = 1<<2, }; struct libusb_transfer { libusb_device_handle *dev_handle; uint8_t flags; unsigned char endpoint; unsigned char type; unsigned int timeout; enum libusb_transfer_status status; int length; int actual_length; libusb_transfer_cb_fn callback; void *user_data; unsigned char *buffer; int num_iso_packets; struct libusb_iso_packet_descriptor* iso_packet_desc; }; struct libusb_iso_packet_descriptor { unsigned int length; unsigned int actual_length; enum libusb_transfer_status status; }; enum libusb_error { LIBUSB_SUCCESS = 0, LIBUSB_ERROR_IO = -1, LIBUSB_ERROR_INVALID_PARAM = -2, LIBUSB_ERROR_ACCESS = -3, LIBUSB_ERROR_NO_DEVICE = -4, LIBUSB_ERROR_NOT_FOUND = -5, LIBUSB_ERROR_BUSY = -6, LIBUSB_ERROR_TIMEOUT = -7, LIBUSB_ERROR_OVERFLOW = -8, LIBUSB_ERROR_PIPE = -9, LIBUSB_ERROR_INTERRUPTED = -10, LIBUSB_ERROR_NO_MEM = -11, LIBUSB_ERROR_NOT_SUPPORTED = -12, LIBUSB_ERROR_OTHER = -99, }; #ifdef __cplusplus } #endif #endif//LIBUSB_1_0_INTERFACE_EMULATOR_FOR_LIBUSB_0_1_H ================================================ FILE: libs/libfreenect/platform/windows/libusb10emu/libusb-1.0/libusbemu.cpp ================================================ /* * This file is part of the OpenKinect Project. http://www.openkinect.org * * Copyright (c) 2010 individual OpenKinect contributors. See the CONTRIB file * for details. * * This code is licensed to you under the terms of the Apache License, version * 2.0, or, at your option, the terms of the GNU General Public License, * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses, * or the following URLs: * http://www.apache.org/licenses/LICENSE-2.0 * http://www.gnu.org/licenses/gpl-2.0.txt * * If you redistribute this file in source form, modified or unmodified, you * may: * 1) Leave this header intact and distribute it under the same terms, * accompanying it with the APACHE20 and GPL20 files, or * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file * In all cases you must keep the copyright notice intact and include a copy * of the CONTRIB file. * * Binary distributions must follow the binary distribution requirements of * either License. */ // Headers required to enable Visual C++ Memory Leak mechanism: // (NOTE: this should only be activated in Debug mode!) #if defined(_MSC_VER) && defined(_DEBUG) // MSVC version predefined macros: // http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/17a84d56-4713-48e4-a36d-763f4dba0c1c/ // _MSC_VER | MSVC Edition // ---------+--------------- // 1300 | MSVC .NET 2002 // 1310 | MSVC .NET 2003 // 1400 | MSVC 2005 // 1500 | MSVC 2008 // 1600 | MSVC 2010 // until VC71 (MSVC2003) the human-readable map/dump mode flag is CRTDBG_MAP_ALLOC: // http://msdn.microsoft.com/en-us/library/x98tx3cf(v=vs.71).aspx // but from VC80 (MSVC2005) and on the flag should be prefixed by a "_": // http://msdn.microsoft.com/en-us/library/x98tx3cf(v=vs.80).aspx #if _MSC_VER >= 1400 #define _CRTDBG_MAP_ALLOC #else #define CRTDBG_MAP_ALLOC #endif #include #include // Also, gotta redefine the new operator ... // http://support.microsoft.com/kb/140858 // but before redefining it, for MSVC version prior to 2010, the STL // header must be included in order to avoid some scary compilation issues: // http://social.msdn.microsoft.com/forums/en-US/vclanguage/thread/a6a148ed-aff1-4ec0-95d2-a82cd4c29cbb #if _MSC_VER < 1600 #include #endif // now it is safe to redefine the new operator #define LIBUSBEMU_DEBUG_NEW new ( _NORMAL_BLOCK, __FILE__, __LINE__) #define new LIBUSBEMU_DEBUG_NEW #endif #include "libusb.h" #include "libusbemu_internal.h" #include "failguard.h" #include #include #include "freenect_internal.h" using namespace libusbemu; #ifdef _DEBUG #define LIBUSBEMU_DEBUG_BUILD #endif//_DEBUG #ifdef LIBUSBEMU_DEBUG_BUILD #define LIBUSB_DEBUG_CMD(cmd) cmd #else #define LIBUSB_DEBUG_CMD(cmd) #endif//LIBUSBEMU_DEBUG_BUILD static libusb_context *default_context = NULL; int libusb_init(libusb_context** context) { usb_init(); LIBUSB_DEBUG_CMD ( const usb_version* version = usb_get_version(); fprintf(stdout, "libusb-win32: dll version %d.%d.%d.%d | driver (libusb0.sys) version %d.%d.%d.%d\n", version->dll.major, version->dll.minor, version->dll.micro, version->dll.nano, version->driver.major, version->driver.minor, version->driver.micro, version->driver.nano); ); // there is no such a thing like 'context' in libusb-0.1... // however, it is wise to emulate such context structure to localize and // keep track of any resource and/or internal data structures, as well as // to be able to clean-up itself at libusb_exit() if (context == NULL) context = &default_context; *context = new libusb_context; // 0 on success; LIBUSB_ERROR on failure return(0); } void libusb_exit(libusb_context* ctx) { if (ctx == NULL) ctx = default_context; ctx->mutex.Enter(); // before deleting the context, delete all devices/transfers still in there: while (!ctx->devices.empty()) { libusb_context::TMapDevices::iterator itDevice (ctx->devices.begin()); libusb_device& device (itDevice->second); if (NULL != device.handles) { // a simple "while(!device.handles->empty())" loop is impossible here // because after a call to libusb_close() the device may be already // destroyed (that's the official libusb-1.0 semantics when the ref // count reaches zero), rendering "device.handles" to a corrupt state. // an accurate "for" loop on the number of handles is the way to go. const int handles = device.handles->size(); for (int h=0; hbegin(); libusb_device_handle* handle (&(itHandle->second)); libusb_close(handle); } } } ctx->mutex.Leave(); delete(ctx); } void libusb_set_debug(libusb_context *ctx, int level) { // Note: libusb-win32 doesn't support context-specific loglevels, so this // sets the loglevel globally. If this is actually an issue for you, I // will be surprised. usb_set_debug(level); return; } ssize_t libusb_get_device_list(libusb_context* ctx, libusb_device*** list) { if (ctx == NULL) ctx = default_context; // libusb_device*** list demystified: // libusb_device*** is the C equivalent to libusb_device**& in C++; such declaration // allows the scope of this function libusb_get_device_list() to write on a variable // of type libusb_device** declared within the function scope of the caller. // libusb_device** is better understood as libusb_device*[], which is an array of // pointers of type libusb_device*, each pointing to a struct that holds actual data. usb_find_busses(); usb_find_devices(); usb_bus* bus = usb_get_busses(); while (bus) { struct usb_device* device = bus->devices; while (device) { libusbemu_register_device(ctx, device); device = device->next; }; bus = bus->next; }; // populate the device list that will be returned to the client: // the list must be NULL-terminated to follow the semantics of libusb-1.0! RAIIMutex lock (ctx->mutex); libusb_device**& devlist = *list; devlist = new libusb_device* [ctx->devices.size()+1]; // +1 is for a finalization mark libusb_context::TMapDevices::iterator it (ctx->devices.begin()); libusb_context::TMapDevices::iterator end (ctx->devices.end()); for (int i=0; it!=end; ++it, ++i) devlist[i] = &it->second; // finalization mark to assist later calls to libusb_free_device_list() devlist[ctx->devices.size()] = NULL; // the number of devices in the outputted list, or LIBUSB_ERROR_NO_MEM on memory allocation failure. return(ctx->devices.size()); } void libusb_free_device_list(libusb_device** list, int unref_devices) { if (NULL == list) return; if (unref_devices) { int i (0); while (list[i] != NULL) { libusbemu_unregister_device(list[i]); ++i; } } delete[](list); } int libusb_get_device_descriptor(libusb_device* dev, struct libusb_device_descriptor* desc) { struct usb_device* device (dev->device); usb_device_descriptor& device_desc (device->descriptor); // plain copy of one descriptor on to another: this is a safe operation because // usb_device_descriptor and libusb_device_descriptor have the same signature memcpy(desc, &device_desc, sizeof(libusb_device_descriptor)); // 0 on success; LIBUSB_ERROR on failure return(0); } int libusb_open(libusb_device* dev, libusb_device_handle** handle) { RAIIMutex lock (dev->ctx->mutex); usb_dev_handle* usb_handle (usb_open(dev->device)); if (NULL == usb_handle) { LIBUSBEMU_ERROR_LIBUSBWIN32(); return(LIBUSB_ERROR_OTHER); } if (NULL == dev->handles) dev->handles = new libusb_device::TMapHandles; libusb_device_handle_t dummy = { dev, usb_handle }; libusb_device::TMapHandles::iterator it = dev->handles->insert(std::make_pair(usb_handle,dummy)).first; *handle = &(it->second); assert((*handle)->dev == dev); assert((*handle)->handle == usb_handle); dev->refcount++; if (NULL == dev->isoTransfers) dev->isoTransfers = new libusb_device::TMapIsocTransfers; //0 on success // LIBUSB_ERROR_NO_MEM on memory allocation failure // LIBUSB_ERROR_ACCESS if the user has insufficient permissions // LIBUSB_ERROR_NO_DEVICE if the device has been disconnected // another LIBUSB_ERROR code on other failure return(0); } void libusb_close(libusb_device_handle* dev_handle) { RAIIMutex lock (dev_handle->dev->ctx->mutex); libusb_device* device (dev_handle->dev); if (device->handles->find(dev_handle->handle) == device->handles->end()) { LIBUSBEMU_ERROR("libusb_close() attempted to close an unregistered handle!\n"); return; } if (0 != usb_close(dev_handle->handle)) { LIBUSBEMU_ERROR_LIBUSBWIN32(); return; } device->handles->erase(dev_handle->handle); libusbemu_unregister_device(device); } int libusb_get_string_descriptor(libusb_device_handle *dev_handle, uint8_t desc_index, uint16_t langid, unsigned char *data, int length) { RAIIMutex lock (dev_handle->dev->ctx->mutex); int bytes = usb_get_string(dev_handle->handle, (int)desc_index, (int)langid, (char*)data, (size_t)length); if (bytes < 0) { LIBUSBEMU_ERROR_LIBUSBWIN32(); } return bytes; } int libusb_get_string_descriptor_ascii(libusb_device_handle *dev_handle, uint8_t desc_index, unsigned char *data, int length) { RAIIMutex lock (dev_handle->dev->ctx->mutex); int bytes = usb_get_string_simple(dev_handle->handle, (int)desc_index, (char*)data, (size_t)length); if (bytes < 0) { LIBUSBEMU_ERROR_LIBUSBWIN32(); } return bytes; } int libusb_set_configuration(libusb_device_handle *dev, int configuration) { RAIIMutex lock (dev->dev->ctx->mutex); int ret = usb_set_configuration(dev->handle, configuration); if (0 != ret) { LIBUSBEMU_ERROR_LIBUSBWIN32(); return ret; } // returns: // 0 on success // LIBUSB_ERROR_NOT_FOUND if the requested configuration does not exist // LIBUSB_ERROR_BUSY if interfaces are currently claimed // LIBUSB_ERROR_NO_DEVICE if the device has been disconnected // another LIBUSB_ERROR code on other failure // return 0; } int libusb_set_interface_alt_setting(libusb_device_handle *dev, int interface_number,int alternate_setting){ RAIIMutex lock (dev->dev->ctx->mutex); if (0 != usb_set_altinterface(dev->handle, alternate_setting)) { LIBUSBEMU_ERROR_LIBUSBWIN32(); return(LIBUSB_ERROR_OTHER); } return(0); } int libusb_claim_interface(libusb_device_handle* dev, int interface_number) { RAIIMutex lock (dev->dev->ctx->mutex); // according to the official libusb-win32 usb_set_configuration() documentation: // http://sourceforge.net/apps/trac/libusb-win32/wiki/libusbwin32_documentation // "Must be called!: usb_set_configuration() must be called with a valid // configuration (not 0) before you can claim the interface. This might // not be be necessary in the future. This behavior is different from // Linux libusb-0.1." if (0 != usb_set_configuration(dev->handle, 1)) { LIBUSBEMU_ERROR_LIBUSBWIN32(); return(LIBUSB_ERROR_OTHER); } if (0 != usb_claim_interface(dev->handle, interface_number)) { LIBUSBEMU_ERROR_LIBUSBWIN32(); return(LIBUSB_ERROR_OTHER); } //if (0 != usb_set_altinterface(dev->handle, 0)) // return(LIBUSB_ERROR_OTHER); // LIBUSB_ERROR_NOT_FOUND if the requested interface does not exist // LIBUSB_ERROR_BUSY if another program or driver has claimed the interface // LIBUSB_ERROR_NO_DEVICE if the device has been disconnected // a LIBUSB_ERROR code on other failure // 0 on success return(0); } int libusb_release_interface(libusb_device_handle* dev, int interface_number) { RAIIMutex lock (dev->dev->ctx->mutex); if (0 != usb_release_interface(dev->handle, interface_number)) { LIBUSBEMU_ERROR_LIBUSBWIN32(); return(LIBUSB_ERROR_OTHER); } // LIBUSB_ERROR_NOT_FOUND if the interface was not claimed // LIBUSB_ERROR_NO_DEVICE if the device has been disconnected // another LIBUSB_ERROR code on other failure // 0 on success return(0); } libusb_device_handle *libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id, uint16_t product_id) { int num_devices; libusb_device** list; libusb_device_handle *dev_handle = NULL; if (ctx == NULL) ctx = default_context; num_devices = libusb_get_device_list(ctx, &list); if (num_devices < 0) return NULL; unsigned int i = 0; while (list[i] != NULL) { struct libusb_device_descriptor desc; int ret = libusb_get_device_descriptor(list[i], &desc); if (ret < 0) break; if (desc.idVendor == vendor_id && desc.idProduct == product_id) { ret = libusb_open(list[i], &dev_handle); if (ret) break; } i++; } libusb_free_device_list(list, 1); return dev_handle; } int libusb_control_transfer(libusb_device_handle* dev_handle, uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, unsigned char* data, uint16_t wLength, unsigned int timeout) { // in libusb-1.0 a timeout of zero it means 'wait indefinitely'; in libusb-0.1, a timeout of zero means 'return immediatelly'! timeout = (0 == timeout) ? 60000 : timeout; // wait 60000ms (60s = 1min) if the transfer is supposed to wait indefinitely... int bytes_transferred = usb_control_msg(dev_handle->handle, bmRequestType, bRequest, wValue, wIndex, (char*)data, wLength, timeout); if (bytes_transferred < 0) { LIBUSBEMU_ERROR_LIBUSBWIN32(); return(LIBUSB_ERROR_OTHER); } // on success, the number of bytes actually transferred // LIBUSB_ERROR_TIMEOUT if the transfer timed out // LIBUSB_ERROR_PIPE if the control request was not supported by the device // LIBUSB_ERROR_NO_DEVICE if the device has been disconnected // another LIBUSB_ERROR code on other failures return(bytes_transferred); } int libusb_bulk_transfer(libusb_device_handle* dev_handle, uint8_t endpoint, uint8_t *data, int length, int *transferred, unsigned int timeout) { // in libusb-1.0 a timeout of zero it means 'wait indefinitely'; in libusb-0.1, a timeout of zero means 'return immediately'! timeout = (0 == timeout) ? 60000 : timeout; // wait 60000ms (60s = 1min) if the transfer is supposed to wait indefinitely... int bytes_transferred; if (endpoint & LIBUSB_ENDPOINT_IN) { // Device to Host bytes_transferred = usb_bulk_read(dev_handle->handle, endpoint, (char*)data, length, timeout); } else { // Host to Device bytes_transferred = usb_bulk_write(dev_handle->handle, endpoint, (char*)data, length, timeout); } if (bytes_transferred < 0) { // 0 on success (and populates transferred) // LIBUSB_ERROR_TIMEOUT if the transfer timed out (and populates transferred) // LIBUSB_ERROR_PIPE if the endpoint halted // LIBUSB_ERROR_OVERFLOW if the device offered more data, see Packets and overflows // LIBUSB_ERROR_NO_DEVICE if the device has been disconnected // another LIBUSB_ERROR code on other failures *transferred = 0; LIBUSBEMU_ERROR_LIBUSBWIN32(); return(LIBUSB_ERROR_OTHER); } *transferred = bytes_transferred; return 0; } // FROM HERE ON CODE BECOMES QUITE MESSY: ASYNCHRONOUS TRANSFERS MANAGEMENT struct libusb_transfer* libusb_alloc_transfer(int iso_packets) { transfer_wrapper* wrapper = new transfer_wrapper; memset(wrapper, 0, sizeof(transfer_wrapper)); libusb_transfer& transfer (wrapper->libusb); transfer.num_iso_packets = iso_packets; transfer.iso_packet_desc = new libusb_iso_packet_descriptor [iso_packets]; memset(transfer.iso_packet_desc, 0, iso_packets*sizeof(libusb_iso_packet_descriptor)); // a newly allocated transfer, or NULL on error return(&transfer); } void libusb_free_transfer(struct libusb_transfer* transfer) { // according to the official libusb_free_transfer() documentation: // "It is legal to call this function with a NULL transfer. // In this case, the function will simply return safely." if (NULL == transfer) return; // according to the official libusb_free_transfer() documentation: // "It is not legal to free an active transfer // (one which has been submitted and has not yet completed)." // that means that only "orphan" transfers can be deleted: transfer_wrapper* wrapper = libusbemu_get_transfer_wrapper(transfer); if (!libusb_device::TListTransfers::Orphan(wrapper)) { LIBUSBEMU_ERROR("libusb_free_transfer() attempted to free an active transfer!"); return; } if (0 != usb_free_async(&wrapper->usb)) { LIBUSBEMU_ERROR_LIBUSBWIN32(); return; } if (NULL != transfer->iso_packet_desc) delete[](transfer->iso_packet_desc); // according to the official libusb_free_transfer() documentation: // "If the LIBUSB_TRANSFER_FREE_BUFFER flag is set and the transfer buffer // is non-NULL, this function will also free the transfer buffer using the // standard system memory allocator (e.g. free())." if (transfer->flags & LIBUSB_TRANSFER_FREE_BUFFER) free(transfer->buffer); delete(wrapper); } void libusb_fill_iso_transfer(struct libusb_transfer* transfer, libusb_device_handle* dev_handle, unsigned char endpoint, unsigned char* buffer, int length, int num_iso_packets, libusb_transfer_cb_fn callback, void* user_data, unsigned int timeout) { // according to the official libusb_fill_iso_transfer() documentation: // "libusb_fill_iso_transfer() is a helper function to populate the required // libusb_transfer fields for an isochronous transfer." // What this means is that the library client is not required to call this // helper function in order to setup the fields within the libusb_transfer // struct. Thus, this is NOT the place for any sort of special processing // because there are no guarantees that such function will ever be invoked. transfer->dev_handle = dev_handle; transfer->endpoint = endpoint; transfer->buffer = buffer; transfer->length = length; transfer->num_iso_packets = num_iso_packets; transfer->callback = callback; transfer->timeout = timeout; transfer->user_data = user_data; transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS; // control some additonal library duties such as: // LIBUSB_TRANSFER_SHORT_NOT_OK, LIBUSB_TRANSFER_FREE_BUFFER, LIBUSB_TRANSFER_FREE_TRANSFER transfer->flags; // these two are output parameters coming from actual transfers... transfer->actual_length; transfer->status; } void libusb_set_iso_packet_lengths(struct libusb_transfer* transfer, unsigned int length) { // according to the official libusb_fill_iso_transfer() documentation: // "Convenience function to set the length of all packets in an isochronous // transfer, based on the num_iso_packets field in the transfer structure." // For the same reasons as in libusb_fill_iso_transfer(), no additional // processing should ever happen withing this function... for (int i=0; i < transfer->num_iso_packets; ++i) transfer->iso_packet_desc[i].length = length; } int ReapThreadProc(void* params); int libusb_submit_transfer(struct libusb_transfer* transfer) { transfer_wrapper* wrapper = libusbemu_get_transfer_wrapper(transfer); // the first time a transfer is submitted, the libusb-0.1 transfer context // (the void*) must be created and initialized with a proper call to one of // the usb_***_setup_async() functions; one could thing of doing this setup // within libusb_fill_***_transfer(), but the latter are just convenience // functions to fill the transfer data structure: the library client is not // forced to call them and could fill the fields directly within the struct. if (NULL == wrapper->usb) libusbemu_setup_transfer(wrapper); libusbemu_clear_transfer(wrapper); int ret = usb_submit_async(wrapper->usb, (char*)transfer->buffer, transfer->length); if (ret < 0) { // TODO: better error handling... // what does usb_submit_async() actually returns on error? // LIBUSB_ERROR_NO_DEVICE if the device has been disconnected // LIBUSB_ERROR_BUSY if the transfer has already been submitted. // another LIBUSB_ERROR code on other failure LIBUSBEMU_ERROR_LIBUSBWIN32(); return(ret); } libusb_device::TMapIsocTransfers& isoTransfers (*transfer->dev_handle->dev->isoTransfers); libusb_device::TMapIsocTransfers::iterator it = isoTransfers.find(transfer->endpoint); if (isoTransfers.end() == it) { libusb_device::isoc_handle dummy = { libusb_device::TListTransfers(), NULL }; it = isoTransfers.insert(std::make_pair(transfer->endpoint, dummy)).first; } libusb_device::isoc_handle& iso (it->second); iso.listTransfers.Append(wrapper); if (NULL == iso.poReapThread) { void** state = new void* [2]; state[0] = transfer->dev_handle; state[1] = (void*)transfer->endpoint; iso.poReapThread = new QuickThread(ReapThreadProc, (void*)state, true); } // 0 on success return(0); } int libusb_cancel_transfer(struct libusb_transfer* transfer) { transfer_wrapper* wrapper = libusbemu_get_transfer_wrapper(transfer); // according to the official libusb_cancel_transfer() documentation: // "This function returns immediately, but this does not indicate // cancellation is complete. Your callback function will be invoked at // some later time with a transfer status of LIBUSB_TRANSFER_CANCELLED." // This semantic can be emulated by setting the transfer->status flag to // LIBUSB_TRANSFER_CANCELLED, leaving the rest to libusb_handle_events(). transfer->status = LIBUSB_TRANSFER_CANCELLED; int ret = usb_cancel_async(wrapper->usb); if (ret != 0) LIBUSBEMU_ERROR_LIBUSBWIN32(); // 0 on success // LIBUSB_ERROR_NOT_FOUND if the transfer is already complete or cancelled. // a LIBUSB_ERROR code on failure return(ret); } // FROM HERE ON CODE BECOMES REALLY REALLY REALLY MESSY: HANDLE EVENTS STUFF int libusbemu_handle_isochronous(libusb_context* ctx, const unsigned int milliseconds) { if (ctx == NULL) ctx = default_context; //QuickThread::Myself().RaisePriority(); RAIIMutex lock (ctx->mutDeliveryPool); int index = ctx->hWantToDeliverPool.WaitAnyUntilTimeout(milliseconds); if (-1 != index) { EventList hDoneDeliveringPoolLocal; while (-1 != (index = ctx->hWantToDeliverPool.CheckAny())) { ctx->hAllowDeliveryPool[index]->Signal(); ctx->hWantToDeliverPool[index]->Reset(); hDoneDeliveringPoolLocal.AttachEvent(ctx->hDoneDeliveringPool[index]); } hDoneDeliveringPoolLocal.WaitAll(); } //QuickThread::Myself().LowerPriority(); return(0); } int libusb_handle_events(libusb_context* ctx) { if (ctx == NULL) ctx = default_context; if (failguard::Abort()) return(LIBUSB_ERROR_INTERRUPTED); RAIIMutex lock (ctx->mutex); libusbemu_handle_isochronous(ctx, 60000); // 0 on success, or a LIBUSB_ERROR code on failure return(0); } int libusb_handle_events_timeout(libusb_context* ctx, struct timeval* timeout) { if (ctx == NULL) ctx = default_context; if (failguard::Abort()) return(LIBUSB_ERROR_INTERRUPTED); RAIIMutex lock (ctx->mutex); if (timeout == NULL) libusbemu_handle_isochronous(ctx, 0); else libusbemu_handle_isochronous(ctx, (timeout->tv_sec * 1000) + (timeout->tv_usec / 1000)); // 0 on success, or a LIBUSB_ERROR code on failure return(0); } void PreprocessTransferNaive(libusb_transfer* transfer, const int read); void PreprocessTransferFreenect(libusb_transfer* transfer, const int read); static void(*PreprocessTransfer)(libusb_transfer*, const int) (PreprocessTransferFreenect); void libusbemu_deliver_transfer(transfer_wrapper* wrapper) { // paranoid check... assert(libusb_device::TListTransfers::Orphan(wrapper)); libusb_transfer* transfer = &wrapper->libusb; // if data is effectively acquired (non-zero bytes transfer), all of the // associated iso packed descriptors must be filled properly; this is an // application specific task and requires knowledge of the logic behind // the streams being transferred: PreprocessTransfer() is an user-defined // "library-injected" routine that should perform this task. if (transfer->actual_length > 0) PreprocessTransfer(transfer, transfer->actual_length); // callback the library client through the callback; at this point, the // client is assumed to do whatever it wants to the data and, possibly, // resubmitting the transfer, which would then place the transfer at the // end of its related asynchronous list (orphan transfer is adopted). transfer->callback(transfer); } int ReapTransfer(transfer_wrapper*, unsigned int, libusb_device::TListTransfers*); int ReapThreadProc(void* params) { LIBUSB_DEBUG_CMD(fprintf(stdout, "Thread execution started.\n")); void** state = (void**)params; libusb_device_handle* dev_handle = (libusb_device_handle*)state[0]; const int endpoint = (int)state[1]; delete[](state); libusb_device::TMapIsocTransfers& isocTransfers = *(dev_handle->dev->isoTransfers); libusb_device::isoc_handle& isocHandle = isocTransfers[endpoint]; libusb_device::TListTransfers& listTransfers (isocHandle.listTransfers); QuickThread*& poThreadObject = isocHandle.poReapThread; libusb_context* ctx (dev_handle->dev->ctx); bool boAbort (false); bool boDeliverRequested (false); QuickEvent wannaDeliver; QuickEvent allowDeliver; QuickEvent doneDelivering; ctx->mutDeliveryPool.Enter(); ctx->hWantToDeliverPool.AttachEvent(&wannaDeliver); ctx->hAllowDeliveryPool.AttachEvent(&allowDeliver); ctx->hDoneDeliveringPool.AttachEvent(&doneDelivering); ctx->mutDeliveryPool.Leave(); libusb_device::TListTransfers listReadyLocal; // isochronous I/O must be handled in high-priority! (at least TIME_CRITICAL) // otherwise, sequence losses are prone to happen... QuickThread::Myself().RaisePriority(); // keep the thread alive as long as there are pending or ready transfers while(!listTransfers.Empty() || !listReadyLocal.Empty()) { // prioritize transfers that are ready to be delivered if (!listReadyLocal.Empty()) { // signal the delivery request, if not signaled yet if (!boDeliverRequested) { doneDelivering.Reset(); wannaDeliver.Signal(); boDeliverRequested = true; } // delivery request already signaled; wait for the delivery permission else if (allowDeliver.WaitUntilTimeout(1)) { boDeliverRequested = false; while (!listReadyLocal.Empty()) { transfer_wrapper* wrapper = listReadyLocal.Head(); listReadyLocal.Remove(wrapper); libusbemu_deliver_transfer(wrapper); } doneDelivering.Signal(); } } // check for pending transfers coming from the device stream if (!listTransfers.Empty()) { transfer_wrapper* wrapper = listTransfers.Head(); int read = ReapTransfer(wrapper, 10000, &listReadyLocal); if (-5 == read) { while (!listTransfers.Empty()) listTransfers.Remove(listTransfers.Head()); while (!listReadyLocal.Empty()) listReadyLocal.Remove(listReadyLocal.Head()); } } // if there are no pending transfers, wait the ready ones to be delivered else { LIBUSB_DEBUG_CMD(fprintf(stdout, "ReapThreadProc(): no pending transfers, sleeping until delivery...\n")); if (!boDeliverRequested) { doneDelivering.Reset(); wannaDeliver.Signal(); boDeliverRequested = true; } allowDeliver.Wait(); } // finally, check the thread failguard if (failguard::Check() && !boAbort) { failguard::WaitDecision(); if (failguard::Abort()) { LIBUSB_DEBUG_CMD(fprintf(stderr, "Thread is aborting: releasing transfers...\n")); QuickThread::Myself().LowerPriority(); boDeliverRequested = true; allowDeliver.Signal(); boAbort = true; } } } QuickThread::Myself().LowerPriority(); LIBUSB_DEBUG_CMD ( if (boAbort) fprintf(stderr, "Thread loop aborted.\n"); ); while(!ctx->mutDeliveryPool.TryEnter()) { wannaDeliver.Signal(); doneDelivering.Signal(); } ctx->hWantToDeliverPool.DetachEvent(&wannaDeliver); ctx->hAllowDeliveryPool.DetachEvent(&allowDeliver); ctx->hDoneDeliveringPool.DetachEvent(&doneDelivering); ctx->mutDeliveryPool.Leave(); poThreadObject = NULL; LIBUSB_DEBUG_CMD(fprintf(stdout, "Thread execution finished.\n")); return(0); } int ReapTransfer(transfer_wrapper* wrapper, unsigned int timeout, libusb_device::TListTransfers* lstReady) { void* context (wrapper->usb); libusb_transfer* transfer (&wrapper->libusb); const int read = usb_reap_async_nocancel(context, timeout); if (read >= 0) { // data successfully acquired (0 bytes is also a go!) transfer->status = LIBUSB_TRANSFER_COMPLETED; // according to the official libusb_transfer struct reference: // "int libusb_transfer::actual_length // Actual length of data that was transferred. // Read-only, and only for use within transfer callback function. // Not valid for isochronous endpoint transfers." // since the client will allegedly not read from this field, we'll be using // it here just to simplify the emulation implementation, more specifically // the libusb_handle_events() and libusbemu_clear_transfer(). transfer->actual_length = read; // the transfer should be removed from the head of the list and put into // an orphan state; it is up to the client code to resubmit the transfer // which will possibly happen during the client callback. libusb_device::TListTransfers::RemoveNode(wrapper); // two possibilities here: either deliver the transfer now or postpone the // delivery to keep it in sync with libusb_handle_events(); in the latter // case, a destination list must be provided. if (NULL != lstReady) lstReady->Append(wrapper); else libusbemu_deliver_transfer(wrapper); } else { // something bad happened: // (a) the timeout passed to usb_reap_async_nocancel() expired; // (b) the transfer was cancelled via usb_cancel_async(); // (c) some fatal error triggered. #undef EIO #undef EINVAL #undef ETIMEOUT enum EReapResult { EIO = -5, EINVAL = -22, ETIMEOUT = -116 }; switch(read) { // When usb_reap_async_nocancel() returns ETIMEOUT, then either: // (a) the timeout indeed expired; // (b) the transfer was cancelled. case ETIMEOUT : // when usb_reap_async_nocancel() returns ETIMEOUT, then either: // (a) the timeout indeed expired: in this case the transfer should // remain as the head of the transfer list (do not remove the node) // and silently return without calling back the client (or perhaps // set the transfer status to LIBUSB_TRANSFER_TIMED_OUT and then // call back - MORE INVESTIGATION REQUIRED) // (b) the transfer was cancelled: in this case the transfer should be // removed from the list and reported back through the callback. if (LIBUSB_TRANSFER_CANCELLED == transfer->status) { libusb_device::TListTransfers::RemoveNode(wrapper); for (int i=0; inum_iso_packets; ++i) transfer->iso_packet_desc[i].status = LIBUSB_TRANSFER_CANCELLED; transfer->callback(transfer); } break; case EINVAL : // I guess -22 is returned if one attempts to reap a context that does // not exist anymore (one that has already been deleted) LIBUSBEMU_ERROR_LIBUSBWIN32(); break; case EIO : // Error code -5 seems to be triggered when the device is lost... LIBUSBEMU_ERROR_LIBUSBWIN32(); libusb_device::TListTransfers::RemoveNode(wrapper); transfer->status = LIBUSB_TRANSFER_NO_DEVICE; transfer->callback(transfer); libusb_cancel_transfer(transfer); transfer->status = LIBUSB_TRANSFER_NO_DEVICE; break; default : // I have not stumbled into any other negative values coming from the // usb_reap_async_nocancel()... Anyway, cancel seems to be a simple yet // plausible preemptive approach... MORE INVESTIGATION NEEDED! LIBUSBEMU_ERROR_LIBUSBWIN32(); libusb_cancel_transfer(transfer); break; } } return(read); } // Naive transfer->iso_packet_desc array filler. It will probably never work // with any device, but it serves as a template and as a default handler... void PreprocessTransferNaive(libusb_transfer* transfer, const int read) { unsigned int remaining (read); const int pkts (transfer->num_iso_packets); for (int i=0; iiso_packet_desc[i]); desc.status = LIBUSB_TRANSFER_COMPLETED; desc.actual_length = MIN(remaining, desc.length); remaining -= desc.actual_length; } } // This is were the transfer->iso_packet_desc array is built. Knowledge of // the underlying device stream protocol is required in order to properly // setup this array. Moreover, it is also necessary to sneak into some of // the libfreenect internals so that the proper length of each iso packet // descriptor can be inferred. Fortunately, libfreenect has this information // avaliable in the "transfer->user_data" field which holds a pointer to a // fnusb_isoc_stream struct with all the information required in there. void PreprocessTransferFreenect(libusb_transfer* transfer, const int read) { fnusb_isoc_stream* xferstrm = (fnusb_isoc_stream*)transfer->user_data; freenect_device* dev = xferstrm->parent->parent; packet_stream* pktstrm = (transfer->endpoint == 0x81) ? &dev->video : &dev->depth; // Kinect Camera Frame Packet Header (12 bytes total): struct pkt_hdr { uint8_t magic[2]; uint8_t pad; uint8_t flag; uint8_t unk1; uint8_t seq; uint8_t unk2; uint8_t unk3; uint32_t timestamp; }; //packet sizes: // first middle last // Bayer 1920 1920 24 // IR 1920 1920 1180 // YUV422 1920 1920 36 // Depth 1760 1760 1144 const unsigned int pktlen = sizeof(pkt_hdr) + pktstrm->pkt_size; const unsigned int pktend = sizeof(pkt_hdr) + pktstrm->last_pkt_size; unsigned int remaining (read); unsigned int leftover (transfer->length); unsigned char* pktbuffer (transfer->buffer); const int pkts (transfer->num_iso_packets); for (int i=0; iiso_packet_desc[i]); desc.status = LIBUSB_TRANSFER_COMPLETED; const pkt_hdr& header (*(pkt_hdr*)pktbuffer); if ((header.magic[0] == 'R') && (header.magic[1] == 'B')) { switch(header.flag & 0x0F) { case 0x01 : // first frame packet case 0x02 : // intermediate frame packets desc.actual_length = MIN(remaining, pktlen); break; case 0x05 : // last frame packet desc.actual_length = MIN(remaining, pktend); break; default : fprintf(stdout, "0x%02X\n", header.flag); break; } } else { desc.actual_length = 0; } remaining -= desc.actual_length; pktbuffer += desc.length; // a.k.a: += 1920 leftover -= desc.length; // a.k.a: -= 1920 } LIBUSB_DEBUG_CMD ( if (remaining > 0) { fprintf(stdout, "%d remaining out of %d\n", remaining, read); if (remaining == read) fprintf(stdout, "no bytes consumed!\n"); } ); } ================================================ FILE: libs/libfreenect/platform/windows/libusb10emu/libusb-1.0/libusbemu_internal.h ================================================ /* * This file is part of the OpenKinect Project. http://www.openkinect.org * * Copyright (c) 2010 individual OpenKinect contributors. See the CONTRIB file * for details. * * This code is licensed to you under the terms of the Apache License, version * 2.0, or, at your option, the terms of the GNU General Public License, * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses, * or the following URLs: * http://www.apache.org/licenses/LICENSE-2.0 * http://www.gnu.org/licenses/gpl-2.0.txt * * If you redistribute this file in source form, modified or unmodified, you * may: * 1) Leave this header intact and distribute it under the same terms, * accompanying it with the APACHE20 and GPL20 files, or * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file * In all cases you must keep the copyright notice intact and include a copy * of the CONTRIB file. * * Binary distributions must follow the binary distribution requirements of * either License. */ #ifndef LIBUSB_EMULATOR_INTERNAL_H #define LIBUSB_EMULATOR_INTERNAL_H #include "libusbemu_threads.h" #include #include #include namespace libusbemu { template inline T*& SAFE_DELETE(T*& p) { if(NULL != p) delete(p); p = NULL; return(p); } template inline T MIN(const T& v1, const T& v2) { return((v1 struct QuickList { T ini; T end; QuickList() { memset(&ini, 0, sizeof(T)); memset(&end, 0, sizeof(T)); ini.prev = NULL; ini.next = &end; end.prev = &ini; end.next = NULL; } // copy-constructor is required to be safely used as a plain std::map value QuickList(const QuickList& rhs) { if (!rhs.Empty()) { fprintf(stdout, "WARNING: Copy-constructin from a non-empty QuickList!\n"); return; } memset(&ini, 0, sizeof(T)); memset(&end, 0, sizeof(T)); ini.prev = NULL; ini.next = &end; end.prev = &ini; end.next = NULL; } ~QuickList() {}; const bool Empty() const { return(ini.next == &end); // could also be (end.prev == &ini) } void Append(T* node) { if (!Orphan(node)) { fprintf(stdout, "WARNING: Appending non-orphan node to list...\n"); Remove(node); } end.prev->next = node; node->prev = end.prev; node->next = &end; end.prev = node; node->list = this; } T* Head() const { T* head (NULL); if (!Empty()) head = ini.next; return(head); } T* Last() const { T* last (NULL); if (!Empty()) last = end.prev; return(last); } const bool Member(T* node) const { return(this == node->list); } static T* Prev(T* node) { T* prev (NULL); if (NULL != node->prev->prev) prev = node->prev; return(prev); } static T* Next(T* node) { T* next (NULL); if (NULL != node->next->next) next = node->next; return(next); } const bool Remove (T* node) { if (!Member(node)) return(false); node->prev->next = node->next; node->next->prev = node->prev; node->prev = NULL; node->next = NULL; node->list = NULL; return(true); } static void RemoveNode(T* node) { if (Orphan(node)) return; node->list->Remove(node); } static const bool Orphan(T* node) { return(NULL == node->list); } }; template struct QuickListMutexed : QuickList { protected: // 'mutable' required to allow operations within 'const methods' mutable QuickMutex mutex; mutable QuickEvent chomp; // signals whether there is (or not) transfers in the list public: QuickListMutexed() {}; QuickListMutexed(const QuickListMutexed& rhs) : QuickList(rhs) {}; ~QuickListMutexed() { /*this->~QuickListMutexed()*/ mutex.Enter(); mutex.Leave(); }; const bool Empty() const { mutex.Enter(); const bool empty = QuickList::Empty(); mutex.Leave(); return(empty); } void Append(T* node) { mutex.Enter(); const bool empty = QuickList::Empty(); QuickList::Append(node); if (empty) chomp.Signal(); mutex.Leave(); } T* Head() const { mutex.Enter(); T* head = QuickList::Head(); mutex.Leave(); return(head); } T* Last() const { mutex.Enter(); T* last = QuickList::Last(); mutex.Leave(); return(last); } const bool Member(T* node) const { mutex.Enter(); const bool member = QuickList::Member(node); mutex.Leave(); return(member); } const bool Remove(T* node) { mutex.Enter(); const bool removed = QuickList::Remove(node); if (QuickList::Empty()) chomp.Reset(); mutex.Leave(); return(removed); } static const bool Orphan(T* node) { //node->list->mutex.Enter(); const bool orphan = QuickList::Orphan(node); //node->list->mutex.Leave(); return(orphan); } const bool WaitUntilTimeout(unsigned int milliseconds) const { return(chomp.WaitUntilTimeout(milliseconds)); } void Wait() const { chomp.Wait(); } const bool Check() const { return(chomp.Check()); } }; } // end of 'namespace libusbemu' using namespace libusbemu; struct transfer_wrapper { transfer_wrapper* prev; transfer_wrapper* next; QuickList* list; void* usb; libusb_transfer libusb; }; struct libusb_device_handle_t { libusb_device* dev; usb_dev_handle* handle; }; struct libusb_device_t { libusb_context* ctx; struct usb_device* device; int refcount; typedef QuickList TListTransfers; struct isoc_handle { TListTransfers listTransfers; QuickThread* poReapThread; }; typedef std::map TMapIsocTransfers; TMapIsocTransfers* isoTransfers; typedef std::map TMapHandles; TMapHandles* handles; }; struct libusb_context_t { typedef std::map TMapDevices; TMapDevices devices; QuickMutex mutex; QuickMutex mutDeliveryPool; EventList hWantToDeliverPool; EventList hAllowDeliveryPool; EventList hDoneDeliveringPool; }; #define LIBUSBEMU_ERROR(msg) libusbemu_report_error(__FILE__, __LINE__, msg) #define LIBUSBEMU_ERROR_LIBUSBWIN32() LIBUSBEMU_ERROR(usb_strerror()) namespace libusbemu { void libusbemu_report_error(const char* file, const int line, const char* msg) { // remove source file path: int i = strlen(file); while (-1 != --i) if ((file[i] == '/') || (file[i] == '\\')) break; file = &file[++i]; fprintf(stderr, "ERROR in libusbemu -- source file '%s' at line %d -- %s\n", file, line, msg); } transfer_wrapper* libusbemu_get_transfer_wrapper(libusb_transfer* transfer) { char* raw_address ((char*)transfer); char* off_address (raw_address - sizeof(void*) - 2*sizeof(transfer_wrapper*) - sizeof(QuickList*)); return((transfer_wrapper*)off_address); } libusb_device* libusbemu_register_device(libusb_context* ctx, struct usb_device* dev) { RAIIMutex lock (ctx->mutex); // register the device (if not already there) ... libusb_device dummy = { ctx, dev, 0, NULL, NULL }; libusb_context::TMapDevices::iterator it = ctx->devices.insert(std::make_pair(dev,dummy)).first; // ... and increment the reference count libusb_device& record (it->second); record.refcount++; // might as well do some paranoid checkings... assert(record.ctx == ctx); assert(record.device == dev); return(&(it->second)); } void libusbemu_unregister_device(libusb_device* dev) { libusb_context* ctx (dev->ctx); RAIIMutex lock (ctx->mutex); // decrement the reference count of the device ... --(dev->refcount); // ... and unregister device if the reference count reaches zero if (0 == dev->refcount) { SAFE_DELETE(dev->handles); // prior to device deletion, all of its transfer lists must be deleted if (NULL != dev->isoTransfers) { libusb_device::TMapIsocTransfers& allTransfers (*(dev->isoTransfers)); while (!allTransfers.empty()) { libusb_device::TMapIsocTransfers::iterator it (allTransfers.begin()); libusb_device::TListTransfers& listTransfers (it->second.listTransfers); while (!listTransfers.Empty()) { transfer_wrapper* transfer (listTransfers.Head()); // make it orphan so that it can be deleted: listTransfers.Remove(transfer); // the following will free the wrapper object as well: libusb_free_transfer(&transfer->libusb); } allTransfers.erase(it); } SAFE_DELETE(dev->isoTransfers); } ctx->devices.erase(dev->device); } } int libusbemu_setup_transfer(transfer_wrapper* wrapper) { void*& context = wrapper->usb; // paranoid check... if (NULL != context) return(LIBUSB_ERROR_OTHER); RAIIMutex lock (wrapper->libusb.dev_handle->dev->ctx->mutex); int ret (LIBUSB_ERROR_OTHER); libusb_transfer* transfer (&wrapper->libusb); usb_dev_handle* handle (transfer->dev_handle->handle); switch(transfer->type) { case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS : ret = usb_isochronous_setup_async(handle, &context, transfer->endpoint, transfer->iso_packet_desc[0].length); break; case LIBUSB_TRANSFER_TYPE_CONTROL : // libusb-0.1 does not actually support asynchronous control transfers, but this should be // very easy to emulate if necessary: just stick the transfer in a special list and then // libusb_handle_events() check if the list is empty or not; if it is not empty, a thread // is created temporarily just to deal with such control transfer requests until the list // becomes eventually empty again and the thread terminates. case LIBUSB_TRANSFER_TYPE_BULK : case LIBUSB_TRANSFER_TYPE_INTERRUPT : // these transfer types are not being used by libfreenect. they should be fairly simple to // emulate with libusb-0.1 since it already provides support for them. // usb_bulk_setup_async(translate(transfer->dev_handle), &context, transfer->endpoint); // usb_interrupt_setup_async(translate(transfer->dev_handle), &context, transfer->endpoint); default : return(LIBUSB_ERROR_INVALID_PARAM); } if (ret < 0) { // TODO: better error handling... // what do the functions usb_***_setup_async() actually return on error? LIBUSBEMU_ERROR_LIBUSBWIN32(); return(ret); } return(LIBUSB_SUCCESS); } void libusbemu_clear_transfer(transfer_wrapper* wrapper) { libusb_transfer* transfer (&wrapper->libusb); if (transfer->actual_length > 0) { transfer->actual_length = 0; memset(transfer->buffer, 0, transfer->length); for (int i=0; inum_iso_packets; ++i) transfer->iso_packet_desc[i].actual_length = 0; } } } // end of 'namespace libusbemu' #endif//LIBUSB_EMULATOR_INTERNAL_H ================================================ FILE: libs/libfreenect/platform/windows/libusb10emu/libusb-1.0/libusbemu_threads.h ================================================ /* * This file is part of the OpenKinect Project. http://www.openkinect.org * * Copyright (c) 2010 individual OpenKinect contributors. See the CONTRIB file * for details. * * This code is licensed to you under the terms of the Apache License, version * 2.0, or, at your option, the terms of the GNU General Public License, * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses, * or the following URLs: * http://www.apache.org/licenses/LICENSE-2.0 * http://www.gnu.org/licenses/gpl-2.0.txt * * If you redistribute this file in source form, modified or unmodified, you * may: * 1) Leave this header intact and distribute it under the same terms, * accompanying it with the APACHE20 and GPL20 files, or * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file * In all cases you must keep the copyright notice intact and include a copy * of the CONTRIB file. * * Binary distributions must follow the binary distribution requirements of * either License. */ #ifndef LIBUSBEMU_THREAD_INTERFACE_WRAPPER_H #define LIBUSBEMU_THREAD_INTERFACE_WRAPPER_H // Wrappers for platform-specific thread/synchronization objects: // * Thread // * Mutex (Critical Section) // * Events (Conditional Variables) #ifdef WIN32 // stick to the Windows scheme for now, but this structure could be easily // replaced by pthread for portability; however if real-time priority turns // out to be a requirement on that platform, the pthread implementation may // not have support for such scheduling. // for a much more lightweight run-time, this could be replaced by dummy // objects, provided that the library client is careful enough to avoid any // sort of race-conditions or dead-locks... #include "libusbemu_threads_win32.h" #else #error LIBUSBEMU PTHREAD WRAPPER NOT YET IMPLEMENTED! // #include "libusbemu_threads_pthread.h" #endif namespace libusbemu { struct RAIIMutex { QuickMutex& m_mutex; RAIIMutex(QuickMutex& mutex) : m_mutex(mutex) { m_mutex.Enter(); } ~RAIIMutex() { m_mutex.Leave(); } }; }; #endif//LIBUSBEMU_THREAD_INTERFACE_WRAPPER_H ================================================ FILE: libs/libfreenect/platform/windows/libusb10emu/libusb-1.0/libusbemu_threads_win32.h ================================================ /* * This file is part of the OpenKinect Project. http://www.openkinect.org * * Copyright (c) 2010 individual OpenKinect contributors. See the CONTRIB file * for details. * * This code is licensed to you under the terms of the Apache License, version * 2.0, or, at your option, the terms of the GNU General Public License, * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses, * or the following URLs: * http://www.apache.org/licenses/LICENSE-2.0 * http://www.gnu.org/licenses/gpl-2.0.txt * * If you redistribute this file in source form, modified or unmodified, you * may: * 1) Leave this header intact and distribute it under the same terms, * accompanying it with the APACHE20 and GPL20 files, or * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file * In all cases you must keep the copyright notice intact and include a copy * of the CONTRIB file. * * Binary distributions must follow the binary distribution requirements of * either License. */ #ifndef LIBUSBEMU_THREAD_INTERFACE_WRAPPER_FOR_WIN32_THREADS_H #define LIBUSBEMU_THREAD_INTERFACE_WRAPPER_FOR_WIN32_THREADS_H #include #include #include #include namespace libusbemu { struct QuickEvent { friend struct EventList; private: HANDLE hEvent; public: inline QuickEvent(const bool signaled=false) : hEvent(NULL) { hEvent = CreateEvent(NULL, TRUE, (BOOL)signaled, NULL); } inline ~QuickEvent() { CloseHandle(hEvent); } inline void Signal() { SetEvent(hEvent); } inline void Reset() { ResetEvent(hEvent); } inline bool Check() { return(WAIT_OBJECT_0 == WaitForSingleObject(hEvent, 0)); } inline const bool WaitUntilTimeout(const unsigned int milliseconds) { return(WAIT_OBJECT_0 == WaitForSingleObject(hEvent, (DWORD)milliseconds)); } inline void Wait() { WaitUntilTimeout(INFINITE); } }; struct QuickThread { private: HANDLE hThread; // Type-safe wrapper that converts arbitrary function signatures into the // required signature of Win32 Thread Procedures (LPTHREAD_START_ROUTINE). // Any Win32 LPTHREAD_START_ROUTINE declared routine can be wrapped as well. // The wrapper is also capable of cleaning up itself upon thread termination. // This wrapper can be extended in the future to support member-functions // to run as thread procedures. template struct ThreadWrapper { struct State { F* routine; void* params; bool release; QuickThread* instance; QuickEvent* sigclone; }; static DWORD WINAPI Thunk(LPVOID lpParameter) { State state = *((State*)lpParameter); // clone state (no heap alloc!) state.sigclone->Signal(); // done cloning, signal back to creator state.sigclone = NULL; // start wrapped thread procedure DWORD ret = (DWORD)state.routine(state.params); // release the associated QuickThread instance if requested if (state.release) delete(state.instance); return(ret); } }; // allow the creation of pseudo-handles to the calling thread // this constructor cannot and should never be called explicitly! // use QuickThread::Myself() to spawn a pseudo-handle QuickThread inline QuickThread() : hThread(GetCurrentThread()) {} public: template inline QuickThread(F* proc, void* params, const bool auto_release=false) : hThread(NULL) { // the 'typename' is required here because of dependent names... // MSVC relaxes this constraint, but it goes against the standard. typename ThreadWrapper::State state; state.routine = proc; state.params = params; state.release = auto_release; state.instance = this; // in order to prevent heap allocation, an event is created so that the // thunk function can signal back when it is done cloning the state; this // may look like unnecessary overhead, but the less heap memory control, // the better becomes the management and maintenance of this class. QuickEvent hWaitThunkCloneState; state.sigclone = &hWaitThunkCloneState; // Ready to issue the thread creation: hThread = CreateThread(NULL, 0, &ThreadWrapper::Thunk, (LPVOID)&state, 0, NULL); // Wait for the thread thunk to clone the state... hWaitThunkCloneState.Wait(); // Event object will then be automatically released upon return } inline ~QuickThread() { // only if not a pseudo-handle... if (hThread == GetCurrentThread()) return; CloseHandle(hThread); } static inline QuickThread Myself() { return(QuickThread()); } inline void Join() { WaitForSingleObject(hThread, INFINITE); } inline bool TryJoin() { return(WAIT_OBJECT_0 == WaitForSingleObject(hThread, 0)); } inline bool LowerPriority() { return(TRUE == SetThreadPriority(hThread, THREAD_PRIORITY_NORMAL)); } inline bool RaisePriority() { return(TRUE == SetThreadPriority(hThread, THREAD_PRIORITY_TIME_CRITICAL)); } static inline void Sleep(int milliseconds) { ::Sleep(milliseconds); } // Yield is already a Win32 macro (WinBase.h)... // http://winapi.freetechsecrets.com/win32/WIN32Yield.htm #ifdef Yield #undef Yield #endif // A pragma push/pop could be used instead, but it does not solve the issues // http://stackoverflow.com/questions/1793800/can-i-redefine-a-c-macro-for-a-few-includes-and-then-define-it-back //#pragma push_macro("Yield") //#undef Yield static inline void Yield() { // Sleep(0) or Sleep(1) ?! // http://stackoverflow.com/questions/1413630/switchtothread-thread-yield-vs-thread-sleep0-vs-thead-sleep1 ::Sleep(1); // could also use the following (but the semantics are quite shady...): // http://msdn.microsoft.com/en-us/library/ms686352(v=vs.85).aspx // SwitchToThread(); } //#pragma pop_macro("Yield") }; struct QuickMutex { private: CRITICAL_SECTION cs; public: inline QuickMutex() { InitializeCriticalSection(&cs); } inline ~QuickMutex() { DeleteCriticalSection(&cs); } inline const bool TryEnter() { return(0 != TryEnterCriticalSection(&cs)); } inline void Enter() { EnterCriticalSection(&cs); } inline void Leave() { LeaveCriticalSection(&cs); } }; struct EventList { QuickMutex mutex; std::vector m_vEvents; std::vector m_vHandles; EventList() {}; ~EventList() {}; const bool AttachEvent(QuickEvent* poEvent) { mutex.Enter(); m_vEvents.push_back(poEvent); m_vHandles.push_back(poEvent->hEvent); mutex.Leave(); return(true); } const bool DetachEvent(QuickEvent* poEvent) { mutex.Enter(); std::vector::iterator it1 = std::find(m_vEvents.begin(), m_vEvents.end(), poEvent); m_vEvents.erase(it1); std::vector::iterator it2 = std::find(m_vHandles.begin(), m_vHandles.end(), poEvent->hEvent); m_vHandles.erase(it2); mutex.Leave(); return(true); } int WaitAnyUntilTimeout(const unsigned int milliseconds) { int index (-1); mutex.Enter(); DWORD ret (WAIT_FAILED); const unsigned int nHandles (m_vHandles.size()); if (nHandles > 0) ret = WaitForMultipleObjects(nHandles, &m_vHandles[0], FALSE, milliseconds); if (ret - WAIT_OBJECT_0 < nHandles) index = (int)(ret - WAIT_OBJECT_0); mutex.Leave(); return(index); } int WaitAny() { return(WaitAnyUntilTimeout(INFINITE)); } int CheckAny() { return(WaitAnyUntilTimeout(0)); } const bool WaitAllUntilTimeout(const unsigned int milliseconds) { bool waited (false); mutex.Enter(); const unsigned int nHandles (m_vHandles.size()); if (nHandles > 0) waited = (WAIT_TIMEOUT != WaitForMultipleObjects(nHandles, &m_vHandles[0], FALSE, milliseconds)); mutex.Leave(); return(waited); } const bool WaitAll() { return(WaitAllUntilTimeout(INFINITE)); } QuickEvent* operator [] (const unsigned int index) { QuickEvent* poEvent (NULL); mutex.Enter(); poEvent = m_vEvents[index]; mutex.Leave(); return(poEvent); } }; } //end of 'namespace libusbemu' #endif//LIBUSBEMU_THREAD_INTERFACE_WRAPPER_FOR_WIN32_THREADS_H ================================================ FILE: libs/libfreenect/platform/windows/stdbool.h ================================================ /* * This file is part of the OpenKinect Project. http://www.openkinect.org * * Copyright (c) 2010 individual OpenKinect contributors. See the CONTRIB file * for details. * * This code is licensed to you under the terms of the Apache License, version * 2.0, or, at your option, the terms of the GNU General Public License, * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses, * or the following URLs: * http://www.apache.org/licenses/LICENSE-2.0 * http://www.gnu.org/licenses/gpl-2.0.txt * * If you redistribute this file in source form, modified or unmodified, you * may: * 1) Leave this header intact and distribute it under the same terms, * accompanying it with the APACHE20 and GPL20 files, or * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file * In all cases you must keep the copyright notice intact and include a copy * of the CONTRIB file. * * Binary distributions must follow the binary distribution requirements of * either License. */ #ifndef _WINDOWS_STDBOOL_EMULATED_H_ #define _WINDOWS_STDBOOL_EMULATED_H_ #endif//_WINDOWS_STDBOOL_EMULATED_H_ ================================================ FILE: libs/libfreenect/platform/windows/unistd.h ================================================ /* * This file is part of the OpenKinect Project. http://www.openkinect.org * * Copyright (c) 2010 individual OpenKinect contributors. See the CONTRIB file * for details. * * This code is licensed to you under the terms of the Apache License, version * 2.0, or, at your option, the terms of the GNU General Public License, * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses, * or the following URLs: * http://www.apache.org/licenses/LICENSE-2.0 * http://www.gnu.org/licenses/gpl-2.0.txt * * If you redistribute this file in source form, modified or unmodified, you * may: * 1) Leave this header intact and distribute it under the same terms, * accompanying it with the APACHE20 and GPL20 files, or * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file * In all cases you must keep the copyright notice intact and include a copy * of the CONTRIB file. * * Binary distributions must follow the binary distribution requirements of * either License. */ #ifndef _WINDOWS_UNISTD_EMULATED_H_ #define _WINDOWS_UNISTD_EMULATED_H_ #include // MinGW defines _SSIZE_T_ in sys/types.h when it defines ssize_t to be a long. // Redefining it causes an error. // MSVC does not define this. #ifndef _SSIZE_T_ #define _SSIZE_T_ typedef long ssize_t; #endif // _SSIZE_T_ #endif//_WINDOWS_UNISTD_EMULATED_H_ ================================================ FILE: libs/libfreenect/src/cameras.c ================================================ /* * This file is part of the OpenKinect Project. http://www.openkinect.org * * Copyright (c) 2010 individual OpenKinect contributors. See the CONTRIB file * for details. * * This code is licensed to you under the terms of the Apache License, version * 2.0, or, at your option, the terms of the GNU General Public License, * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses, * or the following URLs: * http://www.apache.org/licenses/LICENSE-2.0 * http://www.gnu.org/licenses/gpl-2.0.txt * * If you redistribute this file in source form, modified or unmodified, you * may: * 1) Leave this header intact and distribute it under the same terms, * accompanying it with the APACHE20 and GPL20 files, or * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file * In all cases you must keep the copyright notice intact and include a copy * of the CONTRIB file. * * Binary distributions must follow the binary distribution requirements of * either License. */ #include #include #include #include #include "freenect_internal.h" #include "registration.h" #include "cameras.h" #define MAKE_RESERVED(res, fmt) (uint32_t)(((res & 0xff) << 8) | (((fmt & 0xff)))) #define RESERVED_TO_RESOLUTION(reserved) (freenect_resolution)((reserved >> 8) & 0xff) #define RESERVED_TO_FORMAT(reserved) ((reserved) & 0xff) #define video_mode_count 12 static freenect_frame_mode supported_video_modes[video_mode_count] = { // reserved, resolution, format, bytes, width, height, data_bits_per_pixel, padding_bits_per_pixel, framerate, is_valid {MAKE_RESERVED(FREENECT_RESOLUTION_HIGH, FREENECT_VIDEO_RGB), FREENECT_RESOLUTION_HIGH, {FREENECT_VIDEO_RGB}, 1280*1024*3, 1280, 1024, 24, 0, 10, 1 }, {MAKE_RESERVED(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_RGB), FREENECT_RESOLUTION_MEDIUM, {FREENECT_VIDEO_RGB}, 640*480*3, 640, 480, 24, 0, 30, 1 }, {MAKE_RESERVED(FREENECT_RESOLUTION_HIGH, FREENECT_VIDEO_BAYER), FREENECT_RESOLUTION_HIGH, {FREENECT_VIDEO_BAYER}, 1280*1024, 1280, 1024, 8, 0, 10, 1 }, {MAKE_RESERVED(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_BAYER), FREENECT_RESOLUTION_MEDIUM, {FREENECT_VIDEO_BAYER}, 640*480, 640, 480, 8, 0, 30, 1 }, {MAKE_RESERVED(FREENECT_RESOLUTION_HIGH, FREENECT_VIDEO_IR_8BIT), FREENECT_RESOLUTION_HIGH, {FREENECT_VIDEO_IR_8BIT}, 1280*1024, 1280, 1024, 8, 0, 10, 1 }, {MAKE_RESERVED(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_IR_8BIT), FREENECT_RESOLUTION_MEDIUM, {FREENECT_VIDEO_IR_8BIT}, 640*488, 640, 488, 8, 0, 30, 1 }, {MAKE_RESERVED(FREENECT_RESOLUTION_HIGH, FREENECT_VIDEO_IR_10BIT), FREENECT_RESOLUTION_HIGH, {FREENECT_VIDEO_IR_10BIT}, 1280*1024*2, 1280, 1024, 10, 6, 10, 1 }, {MAKE_RESERVED(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_IR_10BIT), FREENECT_RESOLUTION_MEDIUM, {FREENECT_VIDEO_IR_10BIT}, 640*488*2, 640, 488, 10, 6, 30, 1 }, {MAKE_RESERVED(FREENECT_RESOLUTION_HIGH, FREENECT_VIDEO_IR_10BIT_PACKED), FREENECT_RESOLUTION_HIGH, {FREENECT_VIDEO_IR_10BIT_PACKED}, 1280*1024*10/8, 1280, 1024, 10, 0, 10, 1 }, {MAKE_RESERVED(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_IR_10BIT_PACKED), FREENECT_RESOLUTION_MEDIUM, {FREENECT_VIDEO_IR_10BIT_PACKED}, 640*488*10/8, 640, 488, 10, 0, 30, 1 }, {MAKE_RESERVED(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_YUV_RGB), FREENECT_RESOLUTION_MEDIUM, {FREENECT_VIDEO_YUV_RGB}, 640*480*3, 640, 480, 24, 0, 15, 1 }, {MAKE_RESERVED(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_YUV_RAW), FREENECT_RESOLUTION_MEDIUM, {FREENECT_VIDEO_YUV_RAW}, 640*480*2, 640, 480, 16, 0, 15, 1 }, }; #define depth_mode_count 6 static freenect_frame_mode supported_depth_modes[depth_mode_count] = { // reserved, resolution, format, bytes, width, height, data_bits_per_pixel, padding_bits_per_pixel, framerate, is_valid {MAKE_RESERVED(FREENECT_RESOLUTION_MEDIUM, FREENECT_DEPTH_11BIT), FREENECT_RESOLUTION_MEDIUM, {FREENECT_DEPTH_11BIT}, 640*480*2, 640, 480, 11, 5, 30, 1}, {MAKE_RESERVED(FREENECT_RESOLUTION_MEDIUM, FREENECT_DEPTH_10BIT), FREENECT_RESOLUTION_MEDIUM, {FREENECT_DEPTH_10BIT}, 640*480*2, 640, 480, 10, 6, 30, 1}, {MAKE_RESERVED(FREENECT_RESOLUTION_MEDIUM, FREENECT_DEPTH_11BIT_PACKED), FREENECT_RESOLUTION_MEDIUM, {FREENECT_DEPTH_11BIT_PACKED}, 640*480*11/8, 640, 480, 11, 0, 30, 1}, {MAKE_RESERVED(FREENECT_RESOLUTION_MEDIUM, FREENECT_DEPTH_10BIT_PACKED), FREENECT_RESOLUTION_MEDIUM, {FREENECT_DEPTH_10BIT_PACKED}, 640*480*10/8, 640, 480, 10, 0, 30, 1}, {MAKE_RESERVED(FREENECT_RESOLUTION_MEDIUM, FREENECT_DEPTH_REGISTERED), FREENECT_RESOLUTION_MEDIUM, {FREENECT_DEPTH_REGISTERED}, 640*480*2, 640, 480, 16, 0, 30, 1}, {MAKE_RESERVED(FREENECT_RESOLUTION_MEDIUM, FREENECT_DEPTH_MM), FREENECT_RESOLUTION_MEDIUM, {FREENECT_DEPTH_MM}, 640*480*2, 640, 480, 16, 0, 30, 1}, }; static const freenect_frame_mode invalid_mode = {0, (freenect_resolution)0, {(freenect_video_format)0}, 0, 0, 0, 0, 0, 0, 0}; struct pkt_hdr { uint8_t magic[2]; uint8_t pad; uint8_t flag; uint8_t unk1; uint8_t seq; uint8_t unk2; uint8_t unk3; uint32_t timestamp; }; static int stream_process(freenect_context *ctx, packet_stream *strm, uint8_t *pkt, int len) { if (len < 12) return 0; struct pkt_hdr *hdr = (struct pkt_hdr*)pkt; uint8_t *data = pkt + sizeof(*hdr); int datalen = len - sizeof(*hdr); freenect_loglevel l_info = LL_INFO; freenect_loglevel l_notice = LL_NOTICE; freenect_loglevel l_warning = LL_WARNING; if (strm->valid_frames < 2) l_info = l_notice = l_warning = LL_SPEW; if (hdr->magic[0] != 'R' || hdr->magic[1] != 'B') { FN_LOG(l_notice, "[Stream %02x] Invalid magic %02x%02x\n", strm->flag, hdr->magic[0], hdr->magic[1]); return 0; } FN_FLOOD("[Stream %02x] Packet with flag: %02x\n", strm->flag, hdr->flag); uint8_t sof = strm->flag|1; uint8_t mof = strm->flag|2; uint8_t eof = strm->flag|5; // sync if required, dropping packets until SOF if (!strm->synced) { if (hdr->flag != sof) { FN_SPEW("[Stream %02x] Not synced yet...\n", strm->flag); return 0; } strm->synced = 1; strm->seq = hdr->seq; strm->pkt_num = 0; strm->valid_pkts = 0; strm->got_pkts = 0; } int got_frame_size = 0; // handle lost packets if (strm->seq != hdr->seq) { uint8_t lost = hdr->seq - strm->seq; FN_LOG(l_info, "[Stream %02x] Lost %d packets\n", strm->flag, lost); if (lost > 5 || strm->variable_length) { FN_LOG(l_notice, "[Stream %02x] Lost too many packets, resyncing...\n", strm->flag); strm->synced = 0; return 0; } strm->seq = hdr->seq; int left = strm->pkts_per_frame - strm->pkt_num; if (left <= lost) { strm->pkt_num = lost - left; strm->valid_pkts = strm->got_pkts; strm->got_pkts = 0; got_frame_size = strm->frame_size; strm->timestamp = strm->last_timestamp; strm->valid_frames++; } else { strm->pkt_num += lost; } } int expected_pkt_size = (strm->pkt_num == strm->pkts_per_frame-1) ? strm->last_pkt_size : strm->pkt_size; if (!strm->variable_length) { // check the header to make sure it's what we expect if (!(strm->pkt_num == 0 && hdr->flag == sof) && !(strm->pkt_num == strm->pkts_per_frame-1 && hdr->flag == eof) && !(strm->pkt_num > 0 && strm->pkt_num < strm->pkts_per_frame-1 && hdr->flag == mof)) { FN_LOG(l_notice, "[Stream %02x] Inconsistent flag %02x with %d packets in buf (%d total), resyncing...\n", strm->flag, hdr->flag, strm->pkt_num, strm->pkts_per_frame); strm->synced = 0; return got_frame_size; } // check data length if (datalen > expected_pkt_size) { FN_LOG(l_warning, "[Stream %02x] Expected max %d data bytes, but got %d. Dropping...\n", strm->flag, expected_pkt_size, datalen); return got_frame_size; } if (datalen < expected_pkt_size) FN_LOG(l_warning, "[Stream %02x] Expected %d data bytes, but got %d\n", strm->flag, expected_pkt_size, datalen); } else { // check the header to make sure it's what we expect if (!(strm->pkt_num == 0 && hdr->flag == sof) && !(strm->pkt_num < strm->pkts_per_frame && (hdr->flag == eof || hdr->flag == mof))) { FN_LOG(l_notice, "[Stream %02x] Inconsistent flag %02x with %d packets in buf (%d total), resyncing...\n", strm->flag, hdr->flag, strm->pkt_num, strm->pkts_per_frame); strm->synced = 0; return got_frame_size; } // check data length if (datalen > expected_pkt_size) { FN_LOG(l_warning, "[Stream %02x] Expected max %d data bytes, but got %d. Resyncng...\n", strm->flag, expected_pkt_size, datalen); strm->synced = 0; return got_frame_size; } if (datalen < expected_pkt_size && hdr->flag != eof) { FN_LOG(l_warning, "[Stream %02x] Expected %d data bytes, but got %d. Resyncing...\n", strm->flag, expected_pkt_size, datalen); strm->synced = 0; return got_frame_size; } } // copy data uint8_t *dbuf = strm->raw_buf + strm->pkt_num * strm->pkt_size; memcpy(dbuf, data, datalen); strm->pkt_num++; strm->seq++; strm->got_pkts++; strm->last_timestamp = fn_le32(hdr->timestamp); if (hdr->flag == eof) { if (strm->variable_length) got_frame_size = (dbuf - strm->raw_buf) + datalen; else got_frame_size = (dbuf - strm->raw_buf) + strm->last_pkt_size; strm->pkt_num = 0; strm->valid_pkts = strm->got_pkts; strm->got_pkts = 0; strm->timestamp = strm->last_timestamp; strm->valid_frames++; } return got_frame_size; } static void stream_init(freenect_context *ctx, packet_stream *strm, int rlen, int plen) { strm->valid_frames = 0; strm->synced = 0; if (strm->usr_buf) { strm->lib_buf = NULL; strm->proc_buf = strm->usr_buf; } else { strm->lib_buf = malloc(plen); strm->proc_buf = strm->lib_buf; } if (rlen == 0) { strm->split_bufs = 0; strm->raw_buf = (uint8_t*)strm->proc_buf; strm->frame_size = plen; } else { strm->split_bufs = 1; strm->raw_buf = (uint8_t*)malloc(rlen); strm->frame_size = rlen; } strm->last_pkt_size = strm->frame_size % strm->pkt_size; if (strm->last_pkt_size == 0) strm->last_pkt_size = strm->pkt_size; strm->pkts_per_frame = (strm->frame_size + strm->pkt_size - 1) / strm->pkt_size; } static void stream_freebufs(freenect_context *ctx, packet_stream *strm) { if (strm->split_bufs) free(strm->raw_buf); if (strm->lib_buf) free(strm->lib_buf); strm->raw_buf = NULL; strm->proc_buf = NULL; strm->lib_buf = NULL; } static int stream_setbuf(freenect_context *ctx, packet_stream *strm, void *pbuf) { if (!strm->running) { strm->usr_buf = pbuf; return 0; } else { if (!pbuf && !strm->lib_buf) { FN_ERROR("Attempted to set buffer to NULL but stream was started with no internal buffer\n"); return -1; } strm->usr_buf = pbuf; if (!pbuf) strm->proc_buf = strm->lib_buf; else strm->proc_buf = pbuf; if (!strm->split_bufs) strm->raw_buf = (uint8_t*)strm->proc_buf; return 0; } } /** * Convert a packed array of n elements with vw useful bits into array of * zero-padded 16bit elements. * * @param src The source packed array, of size (n * vw / 8) bytes * @param dest The destination unpacked array, of size (n * 2) bytes * @param vw The virtual width of elements, that is the number of useful bits for each of them * @param n The number of elements (in particular, of the destination array), NOT a length in bytes */ static inline void convert_packed_to_16bit(uint8_t *src, uint16_t *dest, int vw, int n) { unsigned int mask = (1 << vw) - 1; uint32_t buffer = 0; int bitsIn = 0; while (n--) { while (bitsIn < vw) { buffer = (buffer << 8) | *(src++); bitsIn += 8; } bitsIn -= vw; *(dest++) = (buffer >> bitsIn) & mask; } } /** * Convert a packed array of n elements with vw useful bits into array of * 8bit elements, dropping LSB. * * @param src The source packed array, of size (n * vw / 8) bytes * @param dest The destination unpacked array, of size (n * 2) bytes * @param vw The virtual width of elements, that is the number of useful bits for each of them * @param n The number of elements (in particular, of the destination array), NOT a length in bytes * * @pre vw is expected to be >= 8. */ static inline void convert_packed_to_8bit(uint8_t *src, uint8_t *dest, int vw, int n) { uint32_t buffer = 0; int bitsIn = 0; while (n--) { while (bitsIn < vw) { buffer = (buffer << 8) | *(src++); bitsIn += 8; } bitsIn -= vw; *(dest++) = buffer >> (bitsIn + vw - 8); } } // Loop-unrolled version of the 11-to-16 bit unpacker. n must be a multiple of 8. static void convert_packed11_to_16bit(uint8_t *raw, uint16_t *frame, int n) { uint16_t baseMask = (1 << 11) - 1; while(n >= 8) { uint8_t r0 = *(raw+0); uint8_t r1 = *(raw+1); uint8_t r2 = *(raw+2); uint8_t r3 = *(raw+3); uint8_t r4 = *(raw+4); uint8_t r5 = *(raw+5); uint8_t r6 = *(raw+6); uint8_t r7 = *(raw+7); uint8_t r8 = *(raw+8); uint8_t r9 = *(raw+9); uint8_t r10 = *(raw+10); frame[0] = (r0<<3) | (r1>>5); frame[1] = ((r1<<6) | (r2>>2) ) & baseMask; frame[2] = ((r2<<9) | (r3<<1) | (r4>>7) ) & baseMask; frame[3] = ((r4<<4) | (r5>>4) ) & baseMask; frame[4] = ((r5<<7) | (r6>>1) ) & baseMask; frame[5] = ((r6<<10) | (r7<<2) | (r8>>6) ) & baseMask; frame[6] = ((r8<<5) | (r9>>3) ) & baseMask; frame[7] = ((r9<<8) | (r10) ) & baseMask; n -= 8; raw += 11; frame += 8; } } static void depth_process(freenect_device *dev, uint8_t *pkt, int len) { freenect_context *ctx = dev->parent; if (len == 0) return; if (!dev->depth.running) return; int got_frame_size = stream_process(ctx, &dev->depth, pkt, len); if (!got_frame_size) return; FN_SPEW("Got depth frame of size %d/%d, %d/%d packets arrived, TS %08x\n", got_frame_size, dev->depth.frame_size, dev->depth.valid_pkts, dev->depth.pkts_per_frame, dev->depth.timestamp); switch (dev->depth_format) { case FREENECT_DEPTH_11BIT: convert_packed11_to_16bit(dev->depth.raw_buf, (uint16_t*)dev->depth.proc_buf, 640*480); break; case FREENECT_DEPTH_REGISTERED: freenect_apply_registration(dev, dev->depth.raw_buf, (uint16_t*)dev->depth.proc_buf ); break; case FREENECT_DEPTH_MM: freenect_apply_depth_to_mm(dev, dev->depth.raw_buf, (uint16_t*)dev->depth.proc_buf ); break; case FREENECT_DEPTH_10BIT: convert_packed_to_16bit(dev->depth.raw_buf, (uint16_t*)dev->depth.proc_buf, 10, 640*480); break; case FREENECT_DEPTH_10BIT_PACKED: case FREENECT_DEPTH_11BIT_PACKED: break; default: FN_ERROR("depth_process() was called, but an invalid depth_format is set\n"); break; } if (dev->depth_cb) dev->depth_cb(dev, dev->depth.proc_buf, dev->depth.timestamp); } #define CLAMP(x) if (x < 0) {x = 0;} if (x > 255) {x = 255;} static void convert_uyvy_to_rgb(uint8_t *raw_buf, uint8_t *proc_buf, freenect_frame_mode frame_mode) { int x, y; for(y = 0; y < frame_mode.height; ++y) { for(x = 0; x < frame_mode.width; x+=2) { int i = (frame_mode.width * y + x); int u = raw_buf[2*i]; int y1 = raw_buf[2*i+1]; int v = raw_buf[2*i+2]; int y2 = raw_buf[2*i+3]; int r1 = (y1-16)*1164/1000 + (v-128)*1596/1000; int g1 = (y1-16)*1164/1000 - (v-128)*813/1000 - (u-128)*391/1000; int b1 = (y1-16)*1164/1000 + (u-128)*2018/1000; int r2 = (y2-16)*1164/1000 + (v-128)*1596/1000; int g2 = (y2-16)*1164/1000 - (v-128)*813/1000 - (u-128)*391/1000; int b2 = (y2-16)*1164/1000 + (u-128)*2018/1000; CLAMP(r1) CLAMP(g1) CLAMP(b1) CLAMP(r2) CLAMP(g2) CLAMP(b2) proc_buf[3*i] =r1; proc_buf[3*i+1]=g1; proc_buf[3*i+2]=b1; proc_buf[3*i+3]=r2; proc_buf[3*i+4]=g2; proc_buf[3*i+5]=b2; } } } #undef CLAMP static void convert_bayer_to_rgb(uint8_t *raw_buf, uint8_t *proc_buf, freenect_frame_mode frame_mode) { int x,y; /* Pixel arrangement: * G R G R G R G R * B G B G B G B G * G R G R G R G R * B G B G B G B G * G R G R G R G R * B G B G B G B G * * To convert a Bayer-pattern into RGB you have to handle four pattern * configurations: * 1) 2) 3) 4) * B1 B1 G1 B2 R1 G1 R2 R1 <- previous line * R1 G1 R2 G2 R1 G3 G2 B1 G3 B1 G1 B2 <- current line * B2 B3 G4 B4 R3 G4 R4 R2 <- next line * ^ ^ ^ * | | next pixel * | current pixel * previous pixel * * The RGB values (r,g,b) for each configuration are calculated as * follows: * * 1) r = (R1 + R2) / 2 * g = G1 * b = (B1 + B2) / 2 * * 2) r = R1 * g = (G1 + G2 + G3 + G4) / 4 * b = (B1 + B2 + B3 + B4) / 4 * * 3) r = (R1 + R2 + R3 + R4) / 4 * g = (G1 + G2 + G3 + G4) / 4 * b = B1 * * 4) r = (R1 + R2) / 2 * g = G1 * b = (B1 + B2) / 2 * * To efficiently calculate these values, two 32bit integers are used * as "shift-buffers". One integer to store the 3 horizontal bayer pixel * values (previous, current, next) of the current line. The other * integer to store the vertical average value of the bayer pixels * (previous, current, next) of the previous and next line. * * The boundary conditions for the first and last line and the first * and last column are solved via mirroring the second and second last * line and the second and second last column. * * To reduce slow memory access, the values of a rgb pixel are packet * into a 32bit variable and transfered together. */ uint8_t *dst = proc_buf; // pointer to destination uint8_t *prevLine; // pointer to previous, current and next line uint8_t *curLine; // of the source bayer pattern uint8_t *nextLine; // storing horizontal values in hVals: // previous << 16, current << 8, next uint32_t hVals; // storing vertical averages in vSums: // previous << 16, current << 8, next uint32_t vSums; // init curLine and nextLine pointers curLine = raw_buf; nextLine = curLine + frame_mode.width; for (y = 0; y < frame_mode.height; ++y) { if ((y > 0) && (y < frame_mode.height-1)) prevLine = curLine - frame_mode.width; // normal case else if (y == 0) prevLine = nextLine; // top boundary case else nextLine = prevLine; // bottom boundary case // init horizontal shift-buffer with current value hVals = (*(curLine++) << 8); // handle left column boundary case hVals |= (*curLine << 16); // init vertical average shift-buffer with current values average vSums = ((*(prevLine++) + *(nextLine++)) << 7) & 0xFF00; // handle left column boundary case vSums |= ((*prevLine + *nextLine) << 15) & 0xFF0000; // store if line is odd or not uint8_t yOdd = y & 1; // the right column boundary case is not handled inside this loop // thus the "639" for (x = 0; x < frame_mode.width-1; ++x) { // place next value in shift buffers hVals |= *(curLine++); vSums |= (*(prevLine++) + *(nextLine++)) >> 1; // calculate the horizontal sum as this sum is needed in // any configuration uint8_t hSum = ((uint8_t)(hVals >> 16) + (uint8_t)(hVals)) >> 1; if (yOdd == 0) { if ((x & 1) == 0) { // Configuration 1 *(dst++) = hSum; // r *(dst++) = hVals >> 8; // g *(dst++) = vSums >> 8; // b } else { // Configuration 2 *(dst++) = hVals >> 8; *(dst++) = (hSum + (uint8_t)(vSums >> 8)) >> 1; *(dst++) = ((uint8_t)(vSums >> 16) + (uint8_t)(vSums)) >> 1; } } else { if ((x & 1) == 0) { // Configuration 3 *(dst++) = ((uint8_t)(vSums >> 16) + (uint8_t)(vSums)) >> 1; *(dst++) = (hSum + (uint8_t)(vSums >> 8)) >> 1; *(dst++) = hVals >> 8; } else { // Configuration 4 *(dst++) = vSums >> 8; *(dst++) = hVals >> 8; *(dst++) = hSum; } } // shift the shift-buffers hVals <<= 8; vSums <<= 8; } // end of for x loop // right column boundary case, mirroring second last column hVals |= (uint8_t)(hVals >> 16); vSums |= (uint8_t)(vSums >> 16); // the horizontal sum simplifies to the second last column value uint8_t hSum = (uint8_t)(hVals); if (yOdd == 0) { if ((x & 1) == 0) { *(dst++) = hSum; *(dst++) = hVals >> 8; *(dst++) = vSums >> 8; } else { *(dst++) = hVals >> 8; *(dst++) = (hSum + (uint8_t)(vSums >> 8)) >> 1; *(dst++) = vSums; } } else { if ((x & 1) == 0) { *(dst++) = vSums; *(dst++) = (hSum + (uint8_t)(vSums >> 8)) >> 1; *(dst++) = hVals >> 8; } else { *(dst++) = vSums >> 8; *(dst++) = hVals >> 8; *(dst++) = hSum; } } } // end of for y loop } static void video_process(freenect_device *dev, uint8_t *pkt, int len) { freenect_context *ctx = dev->parent; if (len == 0) return; if (!dev->video.running) return; int got_frame_size = stream_process(ctx, &dev->video, pkt, len); if (!got_frame_size) return; FN_SPEW("Got video frame of size %d/%d, %d/%d packets arrived, TS %08x\n", got_frame_size, dev->video.frame_size, dev->video.valid_pkts, dev->video.pkts_per_frame, dev->video.timestamp); freenect_frame_mode frame_mode = freenect_get_current_video_mode(dev); switch (dev->video_format) { case FREENECT_VIDEO_RGB: convert_bayer_to_rgb(dev->video.raw_buf, (uint8_t*)dev->video.proc_buf, frame_mode); break; case FREENECT_VIDEO_BAYER: break; case FREENECT_VIDEO_IR_10BIT: convert_packed_to_16bit(dev->video.raw_buf, (uint16_t*)dev->video.proc_buf, 10, frame_mode.width * frame_mode.height); break; case FREENECT_VIDEO_IR_10BIT_PACKED: break; case FREENECT_VIDEO_IR_8BIT: convert_packed_to_8bit(dev->video.raw_buf, (uint8_t*)dev->video.proc_buf, 10, frame_mode.width * frame_mode.height); break; case FREENECT_VIDEO_YUV_RGB: convert_uyvy_to_rgb(dev->video.raw_buf, (uint8_t*)dev->video.proc_buf, frame_mode); break; case FREENECT_VIDEO_YUV_RAW: break; default: FN_ERROR("video_process() was called, but an invalid video_format is set\n"); break; } if (dev->video_cb) dev->video_cb(dev, dev->video.proc_buf, dev->video.timestamp); } typedef struct { uint8_t magic[2]; uint16_t len; uint16_t cmd; uint16_t tag; } cam_hdr; static int send_cmd(freenect_device *dev, uint16_t cmd, void *cmdbuf, unsigned int cmd_len, void *replybuf, int reply_len) { freenect_context *ctx = dev->parent; int res, actual_len; uint8_t obuf[0x400]; uint8_t ibuf[0x200]; cam_hdr *chdr = (cam_hdr*)obuf; cam_hdr *rhdr = (cam_hdr*)ibuf; if (cmd_len & 1 || cmd_len > (0x400 - sizeof(*chdr))) { FN_ERROR("send_cmd: Invalid command length (0x%x)\n", cmd_len); return -1; } chdr->magic[0] = 0x47; chdr->magic[1] = 0x4d; chdr->cmd = fn_le16(cmd); chdr->tag = fn_le16(dev->cam_tag); chdr->len = fn_le16(cmd_len / 2); memcpy(obuf+sizeof(*chdr), cmdbuf, cmd_len); res = fnusb_control(&dev->usb_cam, 0x40, 0, 0, 0, obuf, cmd_len + sizeof(*chdr)); FN_SPEW("Control cmd=%04x tag=%04x len=%04x: %d\n", cmd, dev->cam_tag, cmd_len, res); if (res < 0) { FN_ERROR("send_cmd: Output control transfer failed (%d)\n", res); return res; } do { actual_len = fnusb_control(&dev->usb_cam, 0xc0, 0, 0, 0, ibuf, 0x200); FN_FLOOD("actual_len: %d\n", actual_len); } while ((actual_len == 0) || (actual_len == 0x200)); FN_SPEW("Control reply: %d\n", res); if (actual_len < (int)sizeof(*rhdr)) { FN_ERROR("send_cmd: Input control transfer failed (%d)\n", res); return res; } actual_len -= sizeof(*rhdr); if (rhdr->magic[0] != 0x52 || rhdr->magic[1] != 0x42) { FN_ERROR("send_cmd: Bad magic %02x %02x\n", rhdr->magic[0], rhdr->magic[1]); return -1; } if (rhdr->cmd != chdr->cmd) { FN_ERROR("send_cmd: Bad cmd %02x != %02x\n", rhdr->cmd, chdr->cmd); return -1; } if (rhdr->tag != chdr->tag) { FN_ERROR("send_cmd: Bad tag %04x != %04x\n", rhdr->tag, chdr->tag); return -1; } if (fn_le16(rhdr->len) != (actual_len/2)) { FN_ERROR("send_cmd: Bad len %04x != %04x\n", fn_le16(rhdr->len), (int)(actual_len/2)); return -1; } if (actual_len > reply_len) { FN_WARNING("send_cmd: Data buffer is %d bytes long, but got %d bytes\n", reply_len, actual_len); memcpy(replybuf, ibuf+sizeof(*rhdr), reply_len); } else { memcpy(replybuf, ibuf+sizeof(*rhdr), actual_len); } dev->cam_tag++; return actual_len; } static int write_register(freenect_device *dev, uint16_t reg, uint16_t data) { freenect_context *ctx = dev->parent; uint16_t reply[2]; uint16_t cmd[2]; int res; cmd[0] = fn_le16(reg); cmd[1] = fn_le16(data); FN_DEBUG("Write Reg 0x%04x <= 0x%02x\n", reg, data); res = send_cmd(dev, 0x03, cmd, 4, reply, 4); if (res < 0) return res; if (res != 2) { FN_WARNING("send_cmd returned %d [%04x %04x], 0000 expected\n", res, reply[0], reply[1]); } return 0; } // This function is here for completeness. We don't actually use it for anything right now. static uint16_t read_register(freenect_device *dev, uint16_t reg) { freenect_context *ctx = dev->parent; uint16_t reply[2]; uint16_t cmd; int res; cmd = fn_le16(reg); FN_DEBUG("Read Reg 0x%04x =>\n", reg); res = send_cmd(dev, 0x02, &cmd, 2, reply, 4); if (res < 0) FN_ERROR("read_register: send_cmd() failed: %d\n", res); if (res != 4) FN_WARNING("send_cmd returned %d [%04x %04x], 0000 expected\n", res, reply[0], reply[1]); return reply[1]; } static int freenect_fetch_reg_info(freenect_device *dev) { freenect_context *ctx = dev->parent; char reply[0x200]; uint16_t cmd[5]; freenect_frame_mode mode = freenect_get_current_video_mode(dev); cmd[0] = fn_le16(0x40); // ParamID - in this scenario, XN_HOST_PROTOCOL_ALGORITHM_REGISTRATION cmd[1] = fn_le16(0); // Format cmd[2] = fn_le16((uint16_t)mode.resolution); // Resolution cmd[3] = fn_le16((uint16_t)mode.framerate); // FPS cmd[4] = fn_le16(0); // Offset int res; res = send_cmd(dev, 0x16, cmd, 10, reply, 118); // OPCODE_ALGORITHM_PARAMS if(res != 118) { FN_ERROR("freenect_fetch_reg_info: send_cmd read %d bytes (expected 118)\n", res); return -1; } memcpy(&dev->registration.reg_info, reply + 2, sizeof(dev->registration.reg_info)); dev->registration.reg_info.ax = fn_le32s(dev->registration.reg_info.ax); dev->registration.reg_info.bx = fn_le32s(dev->registration.reg_info.bx); dev->registration.reg_info.cx = fn_le32s(dev->registration.reg_info.cx); dev->registration.reg_info.dx = fn_le32s(dev->registration.reg_info.dx); dev->registration.reg_info.ay = fn_le32s(dev->registration.reg_info.ay); dev->registration.reg_info.by = fn_le32s(dev->registration.reg_info.by); dev->registration.reg_info.cy = fn_le32s(dev->registration.reg_info.cy); dev->registration.reg_info.dy = fn_le32s(dev->registration.reg_info.dy); dev->registration.reg_info.dx_start = fn_le32s(dev->registration.reg_info.dx_start); dev->registration.reg_info.dy_start = fn_le32s(dev->registration.reg_info.dy_start); dev->registration.reg_info.dx_beta_start = fn_le32s(dev->registration.reg_info.dx_beta_start); dev->registration.reg_info.dy_beta_start = fn_le32s(dev->registration.reg_info.dy_beta_start); dev->registration.reg_info.dx_beta_inc = fn_le32s(dev->registration.reg_info.dx_beta_inc); dev->registration.reg_info.dy_beta_inc = fn_le32s(dev->registration.reg_info.dy_beta_inc); dev->registration.reg_info.dxdx_start = fn_le32s(dev->registration.reg_info.dxdx_start); dev->registration.reg_info.dxdy_start = fn_le32s(dev->registration.reg_info.dxdy_start); dev->registration.reg_info.dydx_start = fn_le32s(dev->registration.reg_info.dydx_start); dev->registration.reg_info.dydy_start = fn_le32s(dev->registration.reg_info.dydy_start); dev->registration.reg_info.dxdxdx_start = fn_le32s(dev->registration.reg_info.dxdxdx_start); dev->registration.reg_info.dydxdx_start = fn_le32s(dev->registration.reg_info.dydxdx_start); dev->registration.reg_info.dxdxdy_start = fn_le32s(dev->registration.reg_info.dxdxdy_start); dev->registration.reg_info.dydxdy_start = fn_le32s(dev->registration.reg_info.dydxdy_start); dev->registration.reg_info.dydydx_start = fn_le32s(dev->registration.reg_info.dydydx_start); dev->registration.reg_info.dydydy_start = fn_le32s(dev->registration.reg_info.dydydy_start); FN_SPEW("ax: %d\n", dev->registration.reg_info.ax); FN_SPEW("bx: %d\n", dev->registration.reg_info.bx); FN_SPEW("cx: %d\n", dev->registration.reg_info.cx); FN_SPEW("dx: %d\n", dev->registration.reg_info.dx); FN_SPEW("ay: %d\n", dev->registration.reg_info.ay); FN_SPEW("by: %d\n", dev->registration.reg_info.by); FN_SPEW("cy: %d\n", dev->registration.reg_info.cy); FN_SPEW("dy: %d\n", dev->registration.reg_info.dy); FN_SPEW("dx_start: %d\n", dev->registration.reg_info.dx_start); FN_SPEW("dy_start: %d\n", dev->registration.reg_info.dy_start); FN_SPEW("dx_beta_start: %d\n", dev->registration.reg_info.dx_beta_start); FN_SPEW("dy_beta_start: %d\n", dev->registration.reg_info.dy_beta_start); FN_SPEW("dx_beta_inc: %d\n", dev->registration.reg_info.dx_beta_inc); FN_SPEW("dy_beta_inc: %d\n", dev->registration.reg_info.dy_beta_inc); FN_SPEW("dxdx_start: %d\n", dev->registration.reg_info.dxdx_start); FN_SPEW("dxdy_start: %d\n", dev->registration.reg_info.dxdy_start); FN_SPEW("dydx_start: %d\n", dev->registration.reg_info.dydx_start); FN_SPEW("dydy_start: %d\n", dev->registration.reg_info.dydy_start); FN_SPEW("dxdxdx_start: %d\n", dev->registration.reg_info.dxdxdx_start); FN_SPEW("dydxdx_start: %d\n", dev->registration.reg_info.dydxdx_start); FN_SPEW("dxdxdy_start: %d\n", dev->registration.reg_info.dxdxdy_start); FN_SPEW("dydxdy_start: %d\n", dev->registration.reg_info.dydxdy_start); FN_SPEW("dydydx_start: %d\n", dev->registration.reg_info.dydydx_start); FN_SPEW("dydydy_start: %d\n", dev->registration.reg_info.dydydy_start); /* // NOTE: Not assigned above FN_SPEW("dx_center: %d\n", dev_reg_info->dx_center); FN_SPEW("rollout_blank: %d\n", dev_reg_info->rollout_blank); FN_SPEW("rollout_size: %d\n", dev_reg_info->rollout_size); FN_SPEW("back_comp1: %d\n", dev_reg_info->back_comp1); FN_SPEW("back_comp2: %d\n", dev_reg_info->back_comp2); */ return 0; } static int freenect_fetch_reg_pad_info(freenect_device *dev) { freenect_context *ctx = dev->parent; char reply[0x200]; uint16_t cmd[5]; freenect_frame_mode mode = freenect_get_current_video_mode(dev); cmd[0] = fn_le16(0x41); // ParamID cmd[1] = fn_le16(0); // Format cmd[2] = fn_le16((uint16_t)mode.resolution); // Resolution cmd[3] = fn_le16((uint16_t)mode.framerate); // FPS cmd[4] = fn_le16(0); // Offset int res; res = send_cmd(dev, 0x16, cmd, 10, reply, 8); // OPCODE_ALGORITHM_PARAMS if(res != 8) { FN_ERROR("freenect_fetch_reg_pad_info: send_cmd read %d bytes (expected 8)\n", res); return -1; } memcpy(&dev->registration.reg_pad_info, reply+2, sizeof(dev->registration.reg_pad_info)); dev->registration.reg_pad_info.start_lines = fn_le16s(dev->registration.reg_pad_info.start_lines); dev->registration.reg_pad_info.end_lines = fn_le16s(dev->registration.reg_pad_info.end_lines); dev->registration.reg_pad_info.cropping_lines = fn_le16s(dev->registration.reg_pad_info.cropping_lines); FN_SPEW("start_lines: %u\n",dev->registration.reg_pad_info.start_lines); FN_SPEW("end_lines: %u\n",dev->registration.reg_pad_info.end_lines); FN_SPEW("cropping_lines: %u\n",dev->registration.reg_pad_info.cropping_lines); return 0; } static int freenect_fetch_reg_const_shift(freenect_device *dev) { freenect_context *ctx = dev->parent; char reply[0x200]; uint16_t cmd[5]; freenect_frame_mode mode = freenect_get_current_video_mode(dev); cmd[0] = fn_le16(0x00); // ParamID cmd[1] = fn_le16(0); // Format cmd[2] = fn_le16((uint16_t)mode.resolution); // Resolution cmd[3] = fn_le16((uint16_t)mode.framerate); // FPS cmd[4] = fn_le16(0); // Offset int res; res = send_cmd(dev, 0x16, cmd, 10, reply, 4); // OPCODE_ALGORITHM_PARAMS if(res != 4) { FN_ERROR("freenect_fetch_reg_const_shift: send_cmd read %d bytes (expected 8)\n", res); return -1; } uint16_t shift; memcpy(&shift, reply+2, sizeof(shift)); shift = fn_le16(shift); dev->registration.const_shift = (double)shift; FN_SPEW("const_shift: %f\n",dev->registration.const_shift); return 0; } static int freenect_fetch_zero_plane_info(freenect_device *dev) { freenect_context *ctx = dev->parent; char reply[0x200]; uint16_t cmd[5] = {0}; // Offset is the only field in this command, and it's 0 int res; res = send_cmd(dev, 0x04, cmd, 10, reply, ctx->zero_plane_res); //OPCODE_GET_FIXED_PARAMS = 4, if (res != ctx->zero_plane_res) { FN_ERROR("freenect_fetch_zero_plane_info: send_cmd read %d bytes (expected %d)\n", res,ctx->zero_plane_res); return -1; } memcpy(&(dev->registration.zero_plane_info), reply + 94, sizeof(dev->registration.zero_plane_info)); union { uint32_t ui; float f; } conversion_union; conversion_union.f = dev->registration.zero_plane_info.dcmos_emitter_dist; conversion_union.ui = fn_le32(conversion_union.ui); dev->registration.zero_plane_info.dcmos_emitter_dist = conversion_union.f; conversion_union.f = dev->registration.zero_plane_info.dcmos_rcmos_dist; conversion_union.ui = fn_le32(conversion_union.ui); dev->registration.zero_plane_info.dcmos_rcmos_dist = conversion_union.f; conversion_union.f = dev->registration.zero_plane_info.reference_distance; conversion_union.ui = fn_le32(conversion_union.ui); dev->registration.zero_plane_info.reference_distance = conversion_union.f; conversion_union.f = dev->registration.zero_plane_info.reference_pixel_size; conversion_union.ui = fn_le32(conversion_union.ui); dev->registration.zero_plane_info.reference_pixel_size = conversion_union.f; // WTF is all this data? it's way bigger than sizeof(XnFixedParams)... FN_SPEW("dcmos_emitter_distance: %f\n", dev->registration.zero_plane_info.dcmos_emitter_dist); FN_SPEW("dcmos_rcmos_distance: %f\n", dev->registration.zero_plane_info.dcmos_rcmos_dist); FN_SPEW("reference_distance: %f\n", dev->registration.zero_plane_info.reference_distance); FN_SPEW("reference_pixel_size: %f\n", dev->registration.zero_plane_info.reference_pixel_size); // FIXME: OpenNI seems to use a hardcoded value of 2.4 instead of 2.3 as reported by Kinect dev->registration.zero_plane_info.dcmos_rcmos_dist = 2.4; return 0; } int freenect_start_depth(freenect_device *dev) { freenect_context *ctx = dev->parent; int res; if (dev->depth.running) return -1; dev->depth.pkt_size = DEPTH_PKTDSIZE; dev->depth.flag = 0x70; dev->depth.variable_length = 0; switch (dev->depth_format) { case FREENECT_DEPTH_REGISTERED: case FREENECT_DEPTH_MM: freenect_init_registration(dev); case FREENECT_DEPTH_11BIT: stream_init(ctx, &dev->depth, freenect_find_depth_mode(dev->depth_resolution, FREENECT_DEPTH_11BIT_PACKED).bytes, freenect_find_depth_mode(dev->depth_resolution, FREENECT_DEPTH_11BIT).bytes); break; case FREENECT_DEPTH_10BIT: stream_init(ctx, &dev->depth, freenect_find_depth_mode(dev->depth_resolution, FREENECT_DEPTH_10BIT_PACKED).bytes, freenect_find_depth_mode(dev->depth_resolution, FREENECT_DEPTH_10BIT).bytes); break; case FREENECT_DEPTH_11BIT_PACKED: case FREENECT_DEPTH_10BIT_PACKED: stream_init(ctx, &dev->depth, 0, freenect_find_depth_mode(dev->depth_resolution, dev->depth_format).bytes); break; default: FN_ERROR("freenect_start_depth() called with invalid depth format %d\n", dev->depth_format); return -1; } res = fnusb_start_iso(&dev->usb_cam, &dev->depth_isoc, depth_process, 0x82, NUM_XFERS, PKTS_PER_XFER, DEPTH_PKTBUF); if (res < 0) return res; write_register(dev, 0x105, 0x00); // Disable auto-cycle of projector write_register(dev, 0x06, 0x00); // reset depth stream switch (dev->depth_format) { case FREENECT_DEPTH_11BIT: case FREENECT_DEPTH_11BIT_PACKED: case FREENECT_DEPTH_REGISTERED: case FREENECT_DEPTH_MM: write_register(dev, 0x12, 0x03); break; case FREENECT_DEPTH_10BIT: case FREENECT_DEPTH_10BIT_PACKED: write_register(dev, 0x12, 0x02); break; case FREENECT_DEPTH_DUMMY: // Returned already, hush gcc break; } write_register(dev, 0x13, 0x01); write_register(dev, 0x14, 0x1e); write_register(dev, 0x06, 0x02); // start depth stream write_register(dev, 0x17, 0x00); // disable depth hflip dev->depth.running = 1; return 0; } int freenect_start_video(freenect_device *dev) { freenect_context *ctx = dev->parent; int res; if (dev->video.running) return -1; dev->video.pkt_size = VIDEO_PKTDSIZE; dev->video.flag = 0x80; dev->video.variable_length = 0; uint16_t mode_reg, mode_value; uint16_t res_reg, res_value; uint16_t fps_reg, fps_value; uint16_t hflip_reg; switch(dev->video_format) { case FREENECT_VIDEO_RGB: case FREENECT_VIDEO_BAYER: if(dev->video_resolution == FREENECT_RESOLUTION_HIGH) { mode_value = 0x00; // Bayer res_value = 0x02; // 1280x1024 fps_value = 0x0f; // "15" Hz } else if (dev->video_resolution == FREENECT_RESOLUTION_MEDIUM) { mode_value = 0x00; // Bayer res_value = 0x01; // 640x480 fps_value = 0x1e; // 30 Hz } else { FN_ERROR("freenect_start_video(): called with invalid format/resolution combination\n"); return -1; } mode_reg = 0x0c; res_reg = 0x0d; fps_reg = 0x0e; hflip_reg = 0x47; break; case FREENECT_VIDEO_IR_8BIT: case FREENECT_VIDEO_IR_10BIT: case FREENECT_VIDEO_IR_10BIT_PACKED: if(dev->video_resolution == FREENECT_RESOLUTION_HIGH) { if(dev->depth.running) { FN_ERROR("freenect_start_video(): cannot stream high-resolution IR at same time as depth stream\n"); return -1; } // Due to some ridiculous condition in the firmware, we have to start and stop the // depth stream before the camera will hand us 1280x1024 IR. This is a stupid // workaround, but we've yet to find a better solution. write_register(dev, 0x13, 0x01); // set depth camera resolution (640x480) write_register(dev, 0x14, 0x1e); // set depth camera FPS (30) write_register(dev, 0x06, 0x02); // start depth camera write_register(dev, 0x06, 0x00); // stop depth camera mode_value = 0x00; // Luminance, 10-bit packed res_value = 0x02; // 1280x1024 fps_value = 0x0f; // "15" Hz } else if (dev->video_resolution == FREENECT_RESOLUTION_MEDIUM) { mode_value = 0x00; // Luminance, 10-bit packed res_value = 0x01; // 640x480 fps_value = 0x1e; // 30 Hz } else { FN_ERROR("freenect_start_video(): called with invalid format/resolution combination\n"); return -1; } mode_reg = 0x19; res_reg = 0x1a; fps_reg = 0x1b; hflip_reg = 0x48; break; case FREENECT_VIDEO_YUV_RGB: case FREENECT_VIDEO_YUV_RAW: if(dev->video_resolution == FREENECT_RESOLUTION_MEDIUM) { mode_value = 0x05; // UYUV mode res_value = 0x01; // 640x480 fps_value = 0x0f; // 15Hz } else { FN_ERROR("freenect_start_video(): called with invalid format/resolution combination\n"); return -1; } mode_reg = 0x0c; res_reg = 0x0d; fps_reg = 0x0e; hflip_reg = 0x47; break; default: FN_ERROR("freenect_start_video(): called with invalid video format %d\n", dev->video_format); return -1; } freenect_frame_mode frame_mode = freenect_get_current_video_mode(dev); switch (dev->video_format) { case FREENECT_VIDEO_RGB: stream_init(ctx, &dev->video, freenect_find_video_mode(dev->video_resolution, FREENECT_VIDEO_BAYER).bytes, frame_mode.bytes); break; case FREENECT_VIDEO_BAYER: stream_init(ctx, &dev->video, 0, frame_mode.bytes); break; case FREENECT_VIDEO_IR_8BIT: stream_init(ctx, &dev->video, freenect_find_video_mode(dev->video_resolution, FREENECT_VIDEO_IR_10BIT_PACKED).bytes, frame_mode.bytes); break; case FREENECT_VIDEO_IR_10BIT: stream_init(ctx, &dev->video, freenect_find_video_mode(dev->video_resolution, FREENECT_VIDEO_IR_10BIT_PACKED).bytes, frame_mode.bytes); break; case FREENECT_VIDEO_IR_10BIT_PACKED: stream_init(ctx, &dev->video, 0, frame_mode.bytes); break; case FREENECT_VIDEO_YUV_RGB: stream_init(ctx, &dev->video, freenect_find_video_mode(dev->video_resolution, FREENECT_VIDEO_YUV_RAW).bytes, frame_mode.bytes); break; case FREENECT_VIDEO_YUV_RAW: stream_init(ctx, &dev->video, 0, frame_mode.bytes); break; case FREENECT_VIDEO_DUMMY: // Silence compiler break; } res = fnusb_start_iso(&dev->usb_cam, &dev->video_isoc, video_process, 0x81, NUM_XFERS, PKTS_PER_XFER, VIDEO_PKTBUF); if (res < 0) return res; write_register(dev, mode_reg, mode_value); write_register(dev, res_reg, res_value); write_register(dev, fps_reg, fps_value); switch (dev->video_format) { case FREENECT_VIDEO_RGB: case FREENECT_VIDEO_BAYER: case FREENECT_VIDEO_YUV_RGB: case FREENECT_VIDEO_YUV_RAW: write_register(dev, 0x05, 0x01); // start video stream break; case FREENECT_VIDEO_IR_8BIT: case FREENECT_VIDEO_IR_10BIT: case FREENECT_VIDEO_IR_10BIT_PACKED: write_register(dev, 0x105, 0x00); // Disable auto-cycle of projector write_register(dev, 0x05, 0x03); // start video stream break; case FREENECT_VIDEO_DUMMY: // Silence compiler break; } write_register(dev, hflip_reg, 0x00); // disable Hflip dev->video.running = 1; return 0; } int freenect_stop_depth(freenect_device *dev) { freenect_context *ctx = dev->parent; int res; if (!dev->depth.running) return -1; dev->depth.running = 0; freenect_destroy_registration(&(dev->registration)); write_register(dev, 0x06, 0x00); // stop depth stream res = fnusb_stop_iso(&dev->usb_cam, &dev->depth_isoc); if (res < 0) { FN_ERROR("Failed to stop depth isochronous stream: %d\n", res); return res; } stream_freebufs(ctx, &dev->depth); return 0; } int freenect_stop_video(freenect_device *dev) { freenect_context *ctx = dev->parent; int res; if (!dev->video.running) return -1; dev->video.running = 0; write_register(dev, 0x05, 0x00); // stop video stream res = fnusb_stop_iso(&dev->usb_cam, &dev->video_isoc); if (res < 0) { FN_ERROR("Failed to stop RGB isochronous stream: %d\n", res); return res; } stream_freebufs(ctx, &dev->video); return 0; } void freenect_set_depth_callback(freenect_device *dev, freenect_depth_cb cb) { dev->depth_cb = cb; } void freenect_set_video_callback(freenect_device *dev, freenect_video_cb cb) { dev->video_cb = cb; } int freenect_get_video_mode_count() { return video_mode_count; } freenect_frame_mode freenect_get_video_mode(int mode_num) { if (mode_num >= 0 && mode_num < video_mode_count) return supported_video_modes[mode_num]; freenect_frame_mode retval; retval.is_valid = 0; return retval; } freenect_frame_mode freenect_get_current_video_mode(freenect_device *dev) { return freenect_find_video_mode(dev->video_resolution, dev->video_format); } freenect_frame_mode freenect_find_video_mode(freenect_resolution res, freenect_video_format fmt) { uint32_t unique_id = MAKE_RESERVED(res, fmt); int i; for(i = 0 ; i < video_mode_count; i++) { if (supported_video_modes[i].reserved == unique_id) return supported_video_modes[i]; } freenect_frame_mode retval; retval.is_valid = 0; return retval; } int freenect_set_video_mode(freenect_device* dev, const freenect_frame_mode mode) { freenect_context *ctx = dev->parent; if (dev->video.running) { FN_ERROR("Tried to set video mode while stream is active\n"); return -1; } // Verify that the mode passed in is actually in the supported mode list int found = 0; int i; for(i = 0 ; i < video_mode_count; i++) { if (supported_video_modes[i].reserved == mode.reserved) { found = 1; break; } } if (!found) { FN_ERROR("freenect_set_video_mode: freenect_frame_mode provided is invalid\n"); return -1; } freenect_resolution res = RESERVED_TO_RESOLUTION(mode.reserved); freenect_video_format fmt = (freenect_video_format)RESERVED_TO_FORMAT(mode.reserved); dev->video_format = fmt; dev->video_resolution = res; // Now that we've changed video format and resolution, we need to update // registration tables. freenect_fetch_reg_info(dev); return 0; } int freenect_get_depth_mode_count() { return depth_mode_count; } freenect_frame_mode freenect_get_depth_mode(int mode_num) { if (mode_num >= 0 && mode_num < depth_mode_count) return supported_depth_modes[mode_num]; freenect_frame_mode retval; retval.is_valid = 0; return retval; } freenect_frame_mode freenect_get_current_depth_mode(freenect_device *dev) { return freenect_find_depth_mode(dev->depth_resolution, dev->depth_format); } freenect_frame_mode freenect_find_depth_mode(freenect_resolution res, freenect_depth_format fmt) { uint32_t unique_id = MAKE_RESERVED(res, fmt); int i; for(i = 0 ; i < depth_mode_count; i++) { if (supported_depth_modes[i].reserved == unique_id) return supported_depth_modes[i]; } freenect_frame_mode retval; retval.is_valid = 0; return retval; } int freenect_set_depth_mode(freenect_device* dev, const freenect_frame_mode mode) { freenect_context *ctx = dev->parent; if (dev->depth.running) { FN_ERROR("Tried to set depth mode while stream is active\n"); return -1; } // Verify that the mode passed in is actually in the supported mode list int found = 0; int i; for(i = 0 ; i < depth_mode_count; i++) { if (supported_depth_modes[i].reserved == mode.reserved) { found = 1; break; } } if (!found) { FN_ERROR("freenect_set_depth_mode: freenect_frame_mode provided is invalid\n"); return -1; } freenect_resolution res = RESERVED_TO_RESOLUTION(mode.reserved); freenect_depth_format fmt = (freenect_depth_format)RESERVED_TO_FORMAT(mode.reserved); dev->depth_format = fmt; dev->depth_resolution = res; return 0; } int freenect_set_depth_buffer(freenect_device *dev, void *buf) { return stream_setbuf(dev->parent, &dev->depth, buf); } int freenect_set_video_buffer(freenect_device *dev, void *buf) { return stream_setbuf(dev->parent, &dev->video, buf); } FN_INTERNAL int freenect_camera_init(freenect_device *dev) { freenect_context *ctx = dev->parent; int res; res = freenect_fetch_reg_pad_info(dev); if (res < 0) { FN_ERROR("freenect_camera_init(): Failed to fetch registration pad info for device\n"); return res; } res = freenect_fetch_zero_plane_info(dev); if (res < 0) { FN_ERROR("freenect_camera_init(): Failed to fetch zero plane info for device\n"); return res; } res = freenect_set_video_mode(dev, freenect_find_video_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_RGB)); res = freenect_set_depth_mode(dev, freenect_find_depth_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_DEPTH_11BIT)); res = freenect_fetch_reg_const_shift(dev); if (res < 0) { FN_ERROR("freenect_camera_init(): Failed to fetch const shift for device\n"); return res; } return 0; } FN_INTERNAL int freenect_camera_teardown(freenect_device *dev) { freenect_context *ctx = dev->parent; int res = 0; if (dev->depth.running) { res = freenect_stop_depth(dev); if (res < 0) { FN_ERROR("freenect_camera_teardown(): Failed to stop depth camera\n"); } return res; } if (dev->video.running) { res = freenect_stop_video(dev); if (res < 0) { FN_ERROR("freenect_camera_teardown(): Failed to stop video camera\n"); } return res; } freenect_destroy_registration(&(dev->registration)); return 0; } ================================================ FILE: libs/libfreenect/src/cameras.h ================================================ /* * This file is part of the OpenKinect Project. http://www.openkinect.org * * Copyright (c) 2010-2011 individual OpenKinect contributors. See the CONTRIB * file for details. * * This code is licensed to you under the terms of the Apache License, version * 2.0, or, at your option, the terms of the GNU General Public License, * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses, * or the following URLs: * http://www.apache.org/licenses/LICENSE-2.0 * http://www.gnu.org/licenses/gpl-2.0.txt * * If you redistribute this file in source form, modified or unmodified, you * may: * 1) Leave this header intact and distribute it under the same terms, * accompanying it with the APACHE20 and GPL20 files, or * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file * In all cases you must keep the copyright notice intact and include a copy * of the CONTRIB file. * * Binary distributions must follow the binary distribution requirements of * either License. */ #ifndef CAMERAS_H #define CAMERAS_H #include "libfreenect.h" // Just a couple function declarations. // These are called by core.c to do camera-specific initialization that needs // camera-specific protocol support. int freenect_camera_init(freenect_device *dev); int freenect_camera_teardown(freenect_device *dev); #endif ================================================ FILE: libs/libfreenect/src/core.c ================================================ /* * This file is part of the OpenKinect Project. http://www.openkinect.org * * Copyright (c) 2010 individual OpenKinect contributors. See the CONTRIB file * for details. * * This code is licensed to you under the terms of the Apache License, version * 2.0, or, at your option, the terms of the GNU General Public License, * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses, * or the following URLs: * http://www.apache.org/licenses/LICENSE-2.0 * http://www.gnu.org/licenses/gpl-2.0.txt * * If you redistribute this file in source form, modified or unmodified, you * may: * 1) Leave this header intact and distribute it under the same terms, * accompanying it with the APACHE20 and GPL20 files, or * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file * In all cases you must keep the copyright notice intact and include a copy * of the CONTRIB file. * * Binary distributions must follow the binary distribution requirements of * either License. */ #include #include #include #include #include #include "freenect_internal.h" #include "registration.h" #include "cameras.h" #ifdef BUILD_AUDIO #include "loader.h" #endif FREENECTAPI int freenect_init(freenect_context **ctx, freenect_usb_context *usb_ctx) { int res; *ctx = (freenect_context*)malloc(sizeof(freenect_context)); if (!ctx) return -1; memset(*ctx, 0, sizeof(freenect_context)); (*ctx)->log_level = LL_WARNING; (*ctx)->enabled_subdevices = (freenect_device_flags)(FREENECT_DEVICE_MOTOR | FREENECT_DEVICE_CAMERA #ifdef BUILD_AUDIO | FREENECT_DEVICE_AUDIO #endif ); res = fnusb_init(&(*ctx)->usb, usb_ctx); if (res < 0) { free(*ctx); *ctx = NULL; } return res; } FREENECTAPI int freenect_shutdown(freenect_context *ctx) { while (ctx->first) { FN_NOTICE("Device %p open during shutdown, closing...\n", ctx->first); freenect_close_device(ctx->first); } fnusb_shutdown(&ctx->usb); free(ctx); return 0; } FREENECTAPI int freenect_process_events(freenect_context *ctx) { struct timeval timeout; timeout.tv_sec = 60; timeout.tv_usec = 0; return freenect_process_events_timeout(ctx, &timeout); } FREENECTAPI int freenect_process_events_timeout(freenect_context *ctx, struct timeval *timeout) { int res = fnusb_process_events_timeout(&ctx->usb, timeout); // Iterate over the devices in ctx. If any of them are flagged as freenect_device* dev = ctx->first; while(dev) { if (dev->usb_cam.device_dead) { FN_ERROR("USB camera marked dead, stopping streams\n"); res = -1; freenect_stop_video(dev); freenect_stop_depth(dev); } #ifdef BUILD_AUDIO if (dev->usb_audio.device_dead) { FN_ERROR("USB audio marked dead, stopping streams\n"); res = -1; // Or something else to tell the user that the device just vanished. freenect_stop_audio(dev); } #endif dev = dev->next; } return res; } FREENECTAPI int freenect_num_devices(freenect_context *ctx) { return fnusb_num_devices(&ctx->usb); } FREENECTAPI int freenect_list_device_attributes(freenect_context *ctx, struct freenect_device_attributes **attribute_list) { return fnusb_list_device_attributes(&ctx->usb, attribute_list); } FREENECTAPI void freenect_free_device_attributes(struct freenect_device_attributes *attribute_list) { // Iterate over list, freeing contents of each item as we go. struct freenect_device_attributes* to_free; while(attribute_list != NULL) { to_free = attribute_list; if (attribute_list->camera_serial != NULL) { free((char*)attribute_list->camera_serial); attribute_list->camera_serial = NULL; } attribute_list = attribute_list->next; free(to_free); } return; } FREENECTAPI int freenect_supported_subdevices(void) { #ifdef BUILD_AUDIO return FREENECT_DEVICE_MOTOR | FREENECT_DEVICE_CAMERA | FREENECT_DEVICE_AUDIO; #else return FREENECT_DEVICE_MOTOR | FREENECT_DEVICE_CAMERA; #endif } FREENECTAPI void freenect_select_subdevices(freenect_context *ctx, freenect_device_flags subdevs) { ctx->enabled_subdevices = (freenect_device_flags)(subdevs & (FREENECT_DEVICE_MOTOR | FREENECT_DEVICE_CAMERA #ifdef BUILD_AUDIO | FREENECT_DEVICE_AUDIO #endif )); } FREENECTAPI freenect_device_flags freenect_enabled_subdevices(freenect_context *ctx) { return ctx->enabled_subdevices; } FREENECTAPI int freenect_open_device(freenect_context *ctx, freenect_device **dev, int index) { int res; freenect_device *pdev = (freenect_device*)malloc(sizeof(freenect_device)); if (!pdev) return -1; memset(pdev, 0, sizeof(*pdev)); pdev->parent = ctx; res = fnusb_open_subdevices(pdev, index); if (res < 0) { free(pdev); return res; } if (!ctx->first) { ctx->first = pdev; } else { freenect_device *prev = ctx->first; while (prev->next) prev = prev->next; prev->next = pdev; } *dev = pdev; // Do device-specific initialization if (pdev->usb_cam.dev) { if (freenect_camera_init(pdev) < 0) { return -1; } } return 0; } FREENECTAPI int freenect_open_device_by_camera_serial(freenect_context *ctx, freenect_device **dev, const char* camera_serial) { // This is implemented by listing the devices and seeing which index (if // any) has a camera with a matching serial number, and then punting to // freenect_open_device with that index. struct freenect_device_attributes* attrlist; struct freenect_device_attributes* item; int count = fnusb_list_device_attributes(&ctx->usb, &attrlist); if (count < 0) { FN_ERROR("freenect_open_device_by_camera_serial: Couldn't enumerate serial numbers\n"); return -1; } int index = 0; for(item = attrlist ; item != NULL; item = item->next , index++) { if (strlen(item->camera_serial) == strlen(camera_serial) && strcmp(item->camera_serial, camera_serial) == 0) { freenect_free_device_attributes(attrlist); return freenect_open_device(ctx, dev, index); } } freenect_free_device_attributes(attrlist); FN_ERROR("freenect_open_device_by_camera_serial: Couldn't find a device with serial %s\n", camera_serial); return -1; } FREENECTAPI int freenect_close_device(freenect_device *dev) { freenect_context *ctx = dev->parent; int res; if (dev->usb_cam.dev) { freenect_camera_teardown(dev); } res = fnusb_close_subdevices(dev); if (res < 0) { FN_ERROR("fnusb_close_subdevices failed: %d\n", res); return res; } freenect_device *last = NULL; freenect_device *cur = ctx->first; while (cur && cur != dev) { last = cur; cur = cur->next; } if (!cur) { FN_ERROR("device %p not found in linked list for this context!\n", dev); return -1; } if (last) last->next = cur->next; else ctx->first = cur->next; free(dev); return 0; } FREENECTAPI void freenect_set_user(freenect_device *dev, void *user) { dev->user_data = user; } FREENECTAPI void *freenect_get_user(freenect_device *dev) { return dev->user_data; } FREENECTAPI void freenect_set_log_level(freenect_context *ctx, freenect_loglevel level) { ctx->log_level = level; } FREENECTAPI void freenect_set_log_callback(freenect_context *ctx, freenect_log_cb cb) { ctx->log_cb = cb; } FN_INTERNAL void fn_log(freenect_context *ctx, freenect_loglevel level, const char *fmt, ...) { va_list ap; if (level > ctx->log_level) return; if (ctx->log_cb) { char msgbuf[1024]; va_start(ap, fmt); vsnprintf(msgbuf, 1024, fmt, ap); msgbuf[1023] = 0; va_end(ap); ctx->log_cb(ctx, level, msgbuf); } else { va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); } } ================================================ FILE: libs/libfreenect/src/freenect_internal.h ================================================ /* * This file is part of the OpenKinect Project. http://www.openkinect.org * * Copyright (c) 2010-2011 individual OpenKinect contributors. See the CONTRIB * file for details. * * This code is licensed to you under the terms of the Apache License, version * 2.0, or, at your option, the terms of the GNU General Public License, * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses, * or the following URLs: * http://www.apache.org/licenses/LICENSE-2.0 * http://www.gnu.org/licenses/gpl-2.0.txt * * If you redistribute this file in source form, modified or unmodified, you * may: * 1) Leave this header intact and distribute it under the same terms, * accompanying it with the APACHE20 and GPL20 files, or * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file * In all cases you must keep the copyright notice intact and include a copy * of the CONTRIB file. * * Binary distributions must follow the binary distribution requirements of * either License. */ #ifndef FREENECT_INTERNAL_H #define FREENECT_INTERNAL_H #include #include "libfreenect.h" #include "libfreenect-registration.h" #ifdef BUILD_AUDIO #include "libfreenect-audio.h" #endif #ifdef __ELF__ #define FN_INTERNAL __attribute__ ((visibility ("hidden"))) #else #define FN_INTERNAL #endif typedef void (*fnusb_iso_cb)(freenect_device *dev, uint8_t *buf, int len); #include "usb_libusb10.h" struct _freenect_context { freenect_loglevel log_level; freenect_log_cb log_cb; fnusb_ctx usb; freenect_device_flags enabled_subdevices; freenect_device *first; int zero_plane_res; }; #define LL_FATAL FREENECT_LOG_FATAL #define LL_ERROR FREENECT_LOG_ERROR #define LL_WARNING FREENECT_LOG_WARNING #define LL_NOTICE FREENECT_LOG_NOTICE #define LL_INFO FREENECT_LOG_INFO #define LL_DEBUG FREENECT_LOG_DEBUG #define LL_SPEW FREENECT_LOG_SPEW #define LL_FLOOD FREENECT_LOG_FLOOD #ifdef _WIN32 #include #include void fn_log(freenect_context *ctx, freenect_loglevel level, const char *fmt, ...); #else void fn_log(freenect_context *ctx, freenect_loglevel level, const char *fmt, ...) __attribute__ ((format (printf, 3, 4))); #endif #define FN_LOG(level, ...) fn_log(ctx, level, __VA_ARGS__) #define FN_FATAL(...) FN_LOG(LL_FATAL, __VA_ARGS__) #define FN_ERROR(...) FN_LOG(LL_ERROR, __VA_ARGS__) #define FN_WARNING(...) FN_LOG(LL_WARNING, __VA_ARGS__) #define FN_NOTICE(...) FN_LOG(LL_NOTICE, __VA_ARGS__) #define FN_INFO(...) FN_LOG(LL_INFO, __VA_ARGS__) #define FN_DEBUG(...) FN_LOG(LL_DEBUG, __VA_ARGS__) #define FN_SPEW(...) FN_LOG(LL_SPEW, __VA_ARGS__) #define FN_FLOOD(...) FN_LOG(LL_FLOOD, __VA_ARGS__) #ifdef FN_BIGENDIAN static inline uint16_t fn_le16(uint16_t d) { return (d<<8) | (d>>8); } static inline uint32_t fn_le32(uint32_t d) { return (d<<24) | ((d<<8)&0xFF0000) | ((d>>8)&0xFF00) | (d>>24); } static inline int16_t fn_le16s(int16_t s) { // reinterpret cast to unsigned, use the normal fn_le16, and then reinterpret cast back union { int16_t s; uint16_t u; } conversion_union; conversion_union.s = s; conversion_union.u = fn_le16(conversion_union.u); return conversion_union.s; } static inline int32_t fn_le32s(int32_t s) { // reinterpret cast to unsigned, use the normal fn_le32, and then reinterpret cast back union { int32_t s; uint32_t u; } conversion_union; conversion_union.s = s; conversion_union.u = fn_le32(conversion_union.u); return conversion_union.s; } #else #define fn_le16(x) (x) #define fn_le32(x) (x) #define fn_le16s(x) (x) #define fn_le32s(x) (x) #endif #define DEPTH_PKTSIZE 1760 #define VIDEO_PKTSIZE 1920 #define DEPTH_PKTDSIZE (DEPTH_PKTSIZE-12) #define VIDEO_PKTDSIZE (VIDEO_PKTSIZE-12) #define VID_MICROSOFT 0x45e #define PID_NUI_AUDIO 0x02ad #define PID_NUI_CAMERA 0x02ae #define PID_NUI_MOTOR 0x02b0 #define PID_K4W_CAMERA 0x02bf #define PID_K4W_AUDIO 0x02be typedef struct { int running; uint8_t flag; int synced; uint8_t seq; int got_pkts; int pkt_num; int pkts_per_frame; int pkt_size; int frame_size; int last_pkt_size; int valid_pkts; int valid_frames; int variable_length; uint32_t last_timestamp; uint32_t timestamp; int split_bufs; void *lib_buf; void *usr_buf; uint8_t *raw_buf; void *proc_buf; } packet_stream; #ifdef BUILD_AUDIO typedef struct { int running; freenect_sample_51* audio_out_ring; // TODO: implement sending user-provided data in callbacks int ring_reader_idx; // Index in audio_out_ring of the last sent sample int ring_writer_idx; // Index in audio_out_ring of the next sample we haven't received from the client yet uint16_t out_window; uint8_t out_seq; uint8_t out_counter_within_window; uint16_t out_weird_timestamp; uint8_t out_window_parity; uint16_t in_window; uint16_t last_seen_window[10]; uint8_t in_counter; int32_t* mic_buffer[4]; int16_t* cancelled_buffer; void* in_unknown; // TODO: timestamps } audio_stream; typedef struct { uint32_t magic; // 0x80000080 uint16_t channel; // Values between 0x1 and 0xa indicate audio channel uint16_t len; // packet length uint16_t window; // timestamp uint16_t unknown; // ??? int32_t samples[]; // Size depends on len } audio_in_block; typedef struct { uint16_t window; // Kinda like a timestamp. uint8_t seq; // Values from 0x00 to 0x7f uint8_t weird; // Has an odd cyclic behavior. freenect_sample_51 samples[6]; // Audio samples - 6 samples per transfer } audio_out_block; #endif struct _freenect_device { freenect_context *parent; freenect_device *next; void *user_data; // Cameras fnusb_dev usb_cam; fnusb_isoc_stream depth_isoc; fnusb_isoc_stream video_isoc; freenect_depth_cb depth_cb; freenect_video_cb video_cb; freenect_video_format video_format; freenect_depth_format depth_format; freenect_resolution video_resolution; freenect_resolution depth_resolution; int cam_inited; uint16_t cam_tag; packet_stream depth; packet_stream video; // Registration freenect_registration registration; #ifdef BUILD_AUDIO // Audio fnusb_dev usb_audio; fnusb_isoc_stream audio_out_isoc; fnusb_isoc_stream audio_in_isoc; freenect_audio_in_cb audio_in_cb; freenect_audio_out_cb audio_out_cb; audio_stream audio; uint32_t audio_tag; #endif // Motor fnusb_dev usb_motor; freenect_raw_tilt_state raw_state; }; #endif ================================================ FILE: libs/libfreenect/src/loader.h ================================================ /* * This file is part of the OpenKinect Project. http://www.openkinect.org * * Copyright (c) 2011 individual OpenKinect contributors. See the CONTRIB file * for details. * * This code is licensed to you under the terms of the Apache License, version * 2.0, or, at your option, the terms of the GNU General Public License, * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses, * or the following URLs: * http://www.apache.org/licenses/LICENSE-2.0 * http://www.gnu.org/licenses/gpl-2.0.txt * * If you redistribute this file in source form, modified or unmodified, you * may: * 1) Leave this header intact and distribute it under the same terms, * accompanying it with the APACHE20 and GPL20 files, or * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file * In all cases you must keep the copyright notice intact and include a copy * of the CONTRIB file. * * Binary distributions must follow the binary distribution requirements of * either License. */ #ifndef __LOADER_H__ #define __LOADER_H__ #include #include "usb_libusb10.h" typedef struct { uint32_t magic; uint32_t tag; uint32_t bytes; uint32_t cmd; uint32_t addr; uint32_t unk; } bootloader_command; typedef struct { uint32_t magic; uint32_t tag; uint32_t arg1; // initial command: 0. Firmware blocks: byte count. uint32_t cmd; uint32_t arg2; // initial command: byte count. Firmware blocks: target address. uint32_t zeros[8]; } cemdloader_command; typedef struct { uint32_t magic; uint32_t tag; uint32_t status; } bootloader_status_code; int upload_firmware(fnusb_dev* dev); int upload_cemd_data(fnusb_dev* dev); #endif //__LOADER_H__ ================================================ FILE: libs/libfreenect/src/registration.c ================================================ /* * This file is part of the OpenKinect Project. http://www.openkinect.org * * Copyright (c) 2011 individual OpenKinect contributors. See the CONTRIB file * for details. * * This code is licensed to you under the terms of the Apache License, version * 2.0, or, at your option, the terms of the GNU General Public License, * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses, * or the following URLs: * http://www.apache.org/licenses/LICENSE-2.0 * http://www.gnu.org/licenses/gpl-2.0.txt * * If you redistribute this file in source form, modified or unmodified, you * may: * 1) Leave this header intact and distribute it under the same terms, * accompanying it with the APACHE20 and GPL20 files, or * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file * In all cases you must keep the copyright notice intact and include a copy * of the CONTRIB file. * * Binary distributions must follow the binary distribution requirements of * either License. */ #include #include #include "registration.h" #include #include #include #include #define REG_X_VAL_SCALE 256 // "fixed-point" precision for double -> int32_t conversion #define S2D_PIXEL_CONST 10 #define S2D_CONST_OFFSET 0.375 #define DEPTH_SENSOR_X_RES 1280 #define DEPTH_MIRROR_X 0 #define DEPTH_MAX_METRIC_VALUE FREENECT_DEPTH_MM_MAX_VALUE #define DEPTH_NO_MM_VALUE FREENECT_DEPTH_MM_NO_VALUE #define DEPTH_MAX_RAW_VALUE FREENECT_DEPTH_RAW_MAX_VALUE #define DEPTH_NO_RAW_VALUE FREENECT_DEPTH_RAW_NO_VALUE #define DEPTH_X_OFFSET 1 #define DEPTH_Y_OFFSET 1 #define DEPTH_X_RES 640 #define DEPTH_Y_RES 480 // try to fill single empty pixels AKA "salt-and-pepper noise" // disabled by default, noise removal better handled in later stages // #define DENSE_REGISTRATION /// fill the table of horizontal shift values for metric depth -> RGB conversion static void freenect_init_depth_to_rgb(int32_t* depth_to_rgb, freenect_zero_plane_info* zpi) { uint32_t i,x_scale = DEPTH_SENSOR_X_RES / DEPTH_X_RES; double pixel_size = 1.0 / (zpi->reference_pixel_size * x_scale * S2D_PIXEL_CONST); double pixels_between_rgb_and_ir_cmos = zpi->dcmos_rcmos_dist * pixel_size * S2D_PIXEL_CONST; double reference_distance_in_pixels = zpi->reference_distance * pixel_size * S2D_PIXEL_CONST; memset(depth_to_rgb, DEPTH_NO_MM_VALUE, DEPTH_MAX_METRIC_VALUE * sizeof(int32_t)); for (i = 0; i < DEPTH_MAX_METRIC_VALUE; i++) { double current_depth_in_pixels = i * pixel_size; depth_to_rgb[i] = (( pixels_between_rgb_and_ir_cmos * (current_depth_in_pixels - reference_distance_in_pixels) / current_depth_in_pixels) + S2D_CONST_OFFSET) * REG_X_VAL_SCALE; } } // unrolled inner loop of the 11-bit unpacker static inline void unpack_8_pixels(uint8_t *raw, uint16_t *frame) { uint16_t baseMask = 0x7FF; uint8_t r0 = *(raw+0); uint8_t r1 = *(raw+1); uint8_t r2 = *(raw+2); uint8_t r3 = *(raw+3); uint8_t r4 = *(raw+4); uint8_t r5 = *(raw+5); uint8_t r6 = *(raw+6); uint8_t r7 = *(raw+7); uint8_t r8 = *(raw+8); uint8_t r9 = *(raw+9); uint8_t r10 = *(raw+10); frame[0] = (r0<<3) | (r1>>5); frame[1] = ((r1<<6) | (r2>>2) ) & baseMask; frame[2] = ((r2<<9) | (r3<<1) | (r4>>7) ) & baseMask; frame[3] = ((r4<<4) | (r5>>4) ) & baseMask; frame[4] = ((r5<<7) | (r6>>1) ) & baseMask; frame[5] = ((r6<<10) | (r7<<2) | (r8>>6) ) & baseMask; frame[6] = ((r8<<5) | (r9>>3) ) & baseMask; frame[7] = ((r9<<8) | (r10) ) & baseMask; } // apply registration data to a single packed frame FN_INTERNAL int freenect_apply_registration(freenect_device* dev, uint8_t* input_packed, uint16_t* output_mm) { freenect_registration* reg = &(dev->registration); // set output buffer to zero using pointer-sized memory access (~ 30-40% faster than memset) size_t i, *wipe = (size_t*)output_mm; for (i = 0; i < DEPTH_X_RES * DEPTH_Y_RES * sizeof(uint16_t) / sizeof(size_t); i++) wipe[i] = DEPTH_NO_MM_VALUE; uint16_t unpack[8]; uint32_t target_offset = DEPTH_Y_RES * reg->reg_pad_info.start_lines; uint32_t x,y,source_index = 8; for (y = 0; y < DEPTH_Y_RES; y++) { for (x = 0; x < DEPTH_X_RES; x++) { // get 8 pixels from the packed frame if (source_index == 8) { unpack_8_pixels( input_packed, unpack ); source_index = 0; input_packed += 11; } // get the value at the current depth pixel, convert to millimeters uint16_t metric_depth = reg->raw_to_mm_shift[ unpack[source_index++] ]; // so long as the current pixel has a depth value if (metric_depth == DEPTH_NO_MM_VALUE) continue; if (metric_depth >= DEPTH_MAX_METRIC_VALUE) continue; // calculate the new x and y location for that pixel // using registration_table for the basic rectification // and depth_to_rgb_shift for determining the x shift uint32_t reg_index = DEPTH_MIRROR_X ? ((y + 1) * DEPTH_X_RES - x - 1) : (y * DEPTH_X_RES + x); uint32_t nx = (reg->registration_table[reg_index][0] + reg->depth_to_rgb_shift[metric_depth]) / REG_X_VAL_SCALE; uint32_t ny = reg->registration_table[reg_index][1]; // ignore anything outside the image bounds if (nx >= DEPTH_X_RES) continue; // convert nx, ny to an index in the depth image array uint32_t target_index = (DEPTH_MIRROR_X ? ((ny + 1) * DEPTH_X_RES - nx - 1) : (ny * DEPTH_X_RES + nx)) - target_offset; // get the current value at the new location uint16_t current_depth = output_mm[target_index]; // make sure the new location is empty, or the new value is closer if ((current_depth == DEPTH_NO_MM_VALUE) || (current_depth > metric_depth)) { output_mm[target_index] = metric_depth; // always save depth at current location #ifdef DENSE_REGISTRATION // if we're not on the first row, or the first column if ((nx > 0) && (ny > 0)) { output_mm[target_index - DEPTH_X_RES ] = metric_depth; // save depth at (x,y-1) output_mm[target_index - DEPTH_X_RES - 1] = metric_depth; // save depth at (x-1,y-1) output_mm[target_index - 1] = metric_depth; // save depth at (x-1,y) } else if (ny > 0) { output_mm[target_index - DEPTH_X_RES] = metric_depth; // save depth at (x,y-1) } else if (nx > 0) { output_mm[target_index - 1] = metric_depth; // save depth at (x-1,y) } #endif } } } return 0; } // Same as freenect_apply_registration, but don't bother aligning to the RGB image FN_INTERNAL int freenect_apply_depth_to_mm(freenect_device* dev, uint8_t* input_packed, uint16_t* output_mm) { freenect_registration* reg = &(dev->registration); uint16_t unpack[8]; uint32_t x,y,source_index = 8; for (y = 0; y < DEPTH_Y_RES; y++) { for (x = 0; x < DEPTH_X_RES; x++) { // get 8 pixels from the packed frame if (source_index == 8) { unpack_8_pixels( input_packed, unpack ); source_index = 0; input_packed += 11; } // get the value at the current depth pixel, convert to millimeters uint16_t metric_depth = reg->raw_to_mm_shift[ unpack[source_index++] ]; output_mm[y * DEPTH_X_RES + x] = metric_depth < DEPTH_MAX_METRIC_VALUE ? metric_depth : DEPTH_MAX_METRIC_VALUE; } } return 0; } // create temporary x/y shift tables static void freenect_create_dxdy_tables(double* reg_x_table, double* reg_y_table, int32_t resolution_x, int32_t resolution_y, freenect_reg_info* regdata ) { int64_t AX6 = regdata->ax; int64_t BX6 = regdata->bx; int64_t CX2 = regdata->cx; int64_t DX2 = regdata->dx; int64_t AY6 = regdata->ay; int64_t BY6 = regdata->by; int64_t CY2 = regdata->cy; int64_t DY2 = regdata->dy; // don't merge the shift operations - necessary for proper 32-bit clamping of extracted values int64_t dX0 = (regdata->dx_start << 13) >> 4; int64_t dY0 = (regdata->dy_start << 13) >> 4; int64_t dXdX0 = (regdata->dxdx_start << 11) >> 3; int64_t dXdY0 = (regdata->dxdy_start << 11) >> 3; int64_t dYdX0 = (regdata->dydx_start << 11) >> 3; int64_t dYdY0 = (regdata->dydy_start << 11) >> 3; int64_t dXdXdX0 = (regdata->dxdxdx_start << 5) << 3; int64_t dYdXdX0 = (regdata->dydxdx_start << 5) << 3; int64_t dYdXdY0 = (regdata->dydxdy_start << 5) << 3; int64_t dXdXdY0 = (regdata->dxdxdy_start << 5) << 3; int64_t dYdYdX0 = (regdata->dydydx_start << 5) << 3; int64_t dYdYdY0 = (regdata->dydydy_start << 5) << 3; int32_t row,col,tOffs = 0; for (row = 0 ; row < resolution_y ; row++) { dXdXdX0 += CX2; dXdX0 += dYdXdX0 >> 8; dYdXdX0 += DX2; dX0 += dYdX0 >> 6; dYdX0 += dYdYdX0 >> 8; dYdYdX0 += BX6; dXdXdY0 += CY2; dXdY0 += dYdXdY0 >> 8; dYdXdY0 += DY2; dY0 += dYdY0 >> 6; dYdY0 += dYdYdY0 >> 8; dYdYdY0 += BY6; int64_t coldXdXdY0 = dXdXdY0, coldXdY0 = dXdY0, coldY0 = dY0; int64_t coldXdXdX0 = dXdXdX0, coldXdX0 = dXdX0, coldX0 = dX0; for (col = 0 ; col < resolution_x ; col++, tOffs++) { reg_x_table[tOffs] = coldX0 * (1.0/(1<<17)); reg_y_table[tOffs] = coldY0 * (1.0/(1<<17)); coldX0 += coldXdX0 >> 6; coldXdX0 += coldXdXdX0 >> 8; coldXdXdX0 += AX6; coldY0 += coldXdY0 >> 6; coldXdY0 += coldXdXdY0 >> 8; coldXdXdY0 += AY6; } } } static void freenect_init_registration_table(int32_t (*registration_table)[2], freenect_reg_info* reg_info) { double* regtable_dx = (double*)malloc(DEPTH_X_RES*DEPTH_Y_RES*sizeof(double)); double* regtable_dy = (double*)malloc(DEPTH_X_RES*DEPTH_Y_RES*sizeof(double)); memset(regtable_dx, 0, DEPTH_X_RES*DEPTH_Y_RES * sizeof(double)); memset(regtable_dy, 0, DEPTH_X_RES*DEPTH_Y_RES * sizeof(double)); int32_t x,y,index = 0; // create temporary dx/dy tables freenect_create_dxdy_tables( regtable_dx, regtable_dy, DEPTH_X_RES, DEPTH_Y_RES, reg_info ); for (y = 0; y < DEPTH_Y_RES; y++) { for (x = 0; x < DEPTH_X_RES; x++, index++) { double new_x = x + regtable_dx[index] + DEPTH_X_OFFSET; double new_y = y + regtable_dy[index] + DEPTH_Y_OFFSET; if ((new_x < 0) || (new_y < 0) || (new_x >= DEPTH_X_RES) || (new_y >= DEPTH_Y_RES)) new_x = 2 * DEPTH_X_RES; // intentionally set value outside image bounds registration_table[index][0] = new_x * REG_X_VAL_SCALE; registration_table[index][1] = new_y; } } free(regtable_dx); free(regtable_dy); } // These are just constants. static double parameter_coefficient = 4; static double shift_scale = 10; static double pixel_size_factor = 1; /// convert raw shift value to metric depth (in mm) static uint16_t freenect_raw_to_mm(uint16_t raw, freenect_registration* reg) { freenect_zero_plane_info* zpi = &(reg->zero_plane_info); double fixed_ref_x = ((raw - (parameter_coefficient * reg->const_shift / pixel_size_factor)) / parameter_coefficient) - S2D_CONST_OFFSET; double metric = fixed_ref_x * zpi->reference_pixel_size * pixel_size_factor; return shift_scale * ((metric * zpi->reference_distance / (zpi->dcmos_emitter_dist - metric)) + zpi->reference_distance); } /// Compute registration tables. static void complete_tables(freenect_registration* reg) { uint16_t i; for (i = 0; i < DEPTH_MAX_RAW_VALUE; i++) reg->raw_to_mm_shift[i] = freenect_raw_to_mm( i, reg); reg->raw_to_mm_shift[DEPTH_NO_RAW_VALUE] = DEPTH_NO_MM_VALUE; freenect_init_depth_to_rgb( reg->depth_to_rgb_shift, &(reg->zero_plane_info) ); freenect_init_registration_table( reg->registration_table, &(reg->reg_info) ); } /// camera -> world coordinate helper function void freenect_camera_to_world(freenect_device* dev, int cx, int cy, int wz, double* wx, double* wy) { double ref_pix_size = dev->registration.zero_plane_info.reference_pixel_size; double ref_distance = dev->registration.zero_plane_info.reference_distance; // We multiply cx and cy by these factors because they come from a 640x480 image, // but the zero plane pixel size is for a 1280x1024 image. // However, the 640x480 image is produced by cropping the 1280x1024 image // to 1280x960 and then scaling by .5, so aspect ratio is maintained, and // we should simply multiply by two in each dimension. double factor = 2 * ref_pix_size * wz / ref_distance; *wx = (double)(cx - DEPTH_X_RES/2) * factor; *wy = (double)(cy - DEPTH_Y_RES/2) * factor; } /// Allocate and fill registration tables /// This function should be called every time a new video (not depth!) mode is /// activated. FN_INTERNAL int freenect_init_registration(freenect_device* dev) { freenect_registration* reg = &(dev->registration); // Ensure that we free the previous tables before dropping the pointers, if there were any. freenect_destroy_registration(&(dev->registration)); // Allocate tables. reg->raw_to_mm_shift = (uint16_t*)malloc( sizeof(uint16_t) * DEPTH_MAX_RAW_VALUE ); reg->depth_to_rgb_shift = (int32_t*)malloc( sizeof( int32_t) * DEPTH_MAX_METRIC_VALUE ); reg->registration_table = (int32_t (*)[2])malloc( sizeof( int32_t) * DEPTH_X_RES * DEPTH_Y_RES * 2 ); // Fill tables. complete_tables(reg); return 0; } freenect_registration freenect_copy_registration(freenect_device* dev) { freenect_registration retval; retval.reg_info = dev->registration.reg_info; retval.reg_pad_info = dev->registration.reg_pad_info; retval.zero_plane_info = dev->registration.zero_plane_info; retval.const_shift = dev->registration.const_shift; retval.raw_to_mm_shift = (uint16_t*)malloc( sizeof(uint16_t) * DEPTH_MAX_RAW_VALUE ); retval.depth_to_rgb_shift = (int32_t*)malloc( sizeof( int32_t) * DEPTH_MAX_METRIC_VALUE ); retval.registration_table = (int32_t (*)[2])malloc( sizeof( int32_t) * DEPTH_X_RES * DEPTH_Y_RES * 2 ); complete_tables(&retval); return retval; } int freenect_destroy_registration(freenect_registration* reg) { if (reg->raw_to_mm_shift) { free(reg->raw_to_mm_shift); reg->raw_to_mm_shift = NULL; } if (reg->depth_to_rgb_shift) { free(reg->depth_to_rgb_shift); reg->depth_to_rgb_shift = NULL; } if (reg->registration_table) { free(reg->registration_table); reg->registration_table = NULL; } return 0; } ================================================ FILE: libs/libfreenect/src/registration.h ================================================ /* * This file is part of the OpenKinect Project. http://www.openkinect.org * * Copyright (c) 2011 individual OpenKinect contributors. See the CONTRIB * file for details. * * This code is licensed to you under the terms of the Apache License, version * 2.0, or, at your option, the terms of the GNU General Public License, * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses, * or the following URLs: * http://www.apache.org/licenses/LICENSE-2.0 * http://www.gnu.org/licenses/gpl-2.0.txt * * If you redistribute this file in source form, modified or unmodified, you * may: * 1) Leave this header intact and distribute it under the same terms, * accompanying it with the APACHE20 and GPL20 files, or * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file * In all cases you must keep the copyright notice intact and include a copy * of the CONTRIB file. * * Binary distributions must follow the binary distribution requirements of * either License. */ #ifndef REGISTRATION_H #define REGISTRATION_H #include "libfreenect.h" // Internal function declarations relating to registration int freenect_init_registration(freenect_device* dev); int freenect_apply_registration(freenect_device* dev, uint8_t* input_packed, uint16_t* output_mm); int freenect_apply_depth_to_mm(freenect_device* dev, uint8_t* input_packed, uint16_t* output_mm); #endif ================================================ FILE: libs/libfreenect/src/tilt.c ================================================ /* * This file is part of the OpenKinect Project. http://www.openkinect.org * * Copyright (c) 2010 individual OpenKinect contributors. See the CONTRIB file * for details. * * This code is licensed to you under the terms of the Apache License, version * 2.0, or, at your option, the terms of the GNU General Public License, * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses, * or the following URLs: * http://www.apache.org/licenses/LICENSE-2.0 * http://www.gnu.org/licenses/gpl-2.0.txt * * If you redistribute this file in source form, modified or unmodified, you * may: * 1) Leave this header intact and distribute it under the same terms, * accompanying it with the APACHE20 and GPL20 files, or * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file * In all cases you must keep the copyright notice intact and include a copy * of the CONTRIB file. * * Binary distributions must follow the binary distribution requirements of * either License. */ #include #include #include #include #include #include "freenect_internal.h" // The kinect can tilt from +31 to -31 degrees in what looks like 1 degree increments // The control input looks like 2*desired_degrees #define MAX_TILT_ANGLE 31 #define MIN_TILT_ANGLE (-31) #define GRAVITY 9.80665 freenect_raw_tilt_state* freenect_get_tilt_state(freenect_device *dev) { return &dev->raw_state; } int freenect_update_tilt_state(freenect_device *dev) { freenect_context *ctx = dev->parent; uint8_t buf[10]; uint16_t ux, uy, uz; if(!(ctx->enabled_subdevices & FREENECT_DEVICE_MOTOR)){ //this is needed for OS X. without it the camera timesout within 2-3 seconds #ifdef TARGET_OS_MAC int ret = fnusb_control(&dev->usb_cam, 0xC0, 0x32, 0x0, 0x0, buf, 10); #endif return 0; } int ret = fnusb_control(&dev->usb_motor, 0xC0, 0x32, 0x0, 0x0, buf, 10); if (ret != 10) { FN_ERROR("Error in accelerometer reading, libusb_control_transfer returned %d\n", ret); return ret < 0 ? ret : -1; } ux = ((uint16_t)buf[2] << 8) | buf[3]; uy = ((uint16_t)buf[4] << 8) | buf[5]; uz = ((uint16_t)buf[6] << 8) | buf[7]; dev->raw_state.accelerometer_x = (int16_t)ux; dev->raw_state.accelerometer_y = (int16_t)uy; dev->raw_state.accelerometer_z = (int16_t)uz; dev->raw_state.tilt_angle = (int8_t)buf[8]; dev->raw_state.tilt_status = (freenect_tilt_status_code)buf[9]; return ret; } int freenect_set_tilt_degs(freenect_device *dev, double angle) { freenect_context *ctx = dev->parent; if(!(ctx->enabled_subdevices & FREENECT_DEVICE_MOTOR)) return 0; int ret; uint8_t empty[0x1]; angle = (angleMAX_TILT_ANGLE) ? MAX_TILT_ANGLE : angle); angle = angle * 2; ret = fnusb_control(&dev->usb_motor, 0x40, 0x31, (uint16_t)angle, 0x0, empty, 0x0); return ret; } int freenect_set_led(freenect_device *dev, freenect_led_options option) { freenect_context *ctx = dev->parent; if(!(ctx->enabled_subdevices & FREENECT_DEVICE_MOTOR)) return 0; int ret; uint8_t empty[0x1]; ret = fnusb_control(&dev->usb_motor, 0x40, 0x06, (uint16_t)option, 0x0, empty, 0x0); return ret; } double freenect_get_tilt_degs(freenect_raw_tilt_state *state) { return ((double)state->tilt_angle) / 2.; } freenect_tilt_status_code freenect_get_tilt_status(freenect_raw_tilt_state *state) { return state->tilt_status; } void freenect_get_mks_accel(freenect_raw_tilt_state *state, double* x, double* y, double* z) { //the documentation for the accelerometer (http://www.kionix.com/Product%20Sheets/KXSD9%20Product%20Brief.pdf) //states there are 819 counts/g *x = (double)state->accelerometer_x/FREENECT_COUNTS_PER_G*GRAVITY; *y = (double)state->accelerometer_y/FREENECT_COUNTS_PER_G*GRAVITY; *z = (double)state->accelerometer_z/FREENECT_COUNTS_PER_G*GRAVITY; } ================================================ FILE: libs/libfreenect/src/usb_libusb10.c ================================================ /* * This file is part of the OpenKinect Project. http://www.openkinect.org * * Copyright (c) 2010 individual OpenKinect contributors. See the CONTRIB file * for details. * * This code is licensed to you under the terms of the Apache License, version * 2.0, or, at your option, the terms of the GNU General Public License, * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses, * or the following URLs: * http://www.apache.org/licenses/LICENSE-2.0 * http://www.gnu.org/licenses/gpl-2.0.txt * * If you redistribute this file in source form, modified or unmodified, you * may: * 1) Leave this header intact and distribute it under the same terms, * accompanying it with the APACHE20 and GPL20 files, or * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file * In all cases you must keep the copyright notice intact and include a copy * of the CONTRIB file. * * Binary distributions must follow the binary distribution requirements of * either License. */ #include #include #include #include #include #include #include "freenect_internal.h" #include "loader.h" FN_INTERNAL int fnusb_num_devices(fnusb_ctx *ctx) { libusb_device **devs; //pointer to pointer of device, used to retrieve a list of devices ssize_t cnt = libusb_get_device_list (ctx->ctx, &devs); //get the list of devices if (cnt < 0) return (-1); int nr = 0, i = 0; struct libusb_device_descriptor desc; for (i = 0; i < cnt; ++i) { int r = libusb_get_device_descriptor (devs[i], &desc); if (r < 0) continue; if (desc.idVendor == VID_MICROSOFT && (desc.idProduct == PID_NUI_CAMERA || desc.idProduct == PID_K4W_CAMERA)) nr++; } libusb_free_device_list (devs, 1); // free the list, unref the devices in it return nr; } FN_INTERNAL int fnusb_list_device_attributes(fnusb_ctx *ctx, struct freenect_device_attributes** attribute_list) { *attribute_list = NULL; // initialize some return value in case the user is careless. libusb_device **devs; //pointer to pointer of device, used to retrieve a list of devices ssize_t count = libusb_get_device_list (ctx->ctx, &devs); if (count < 0) return -1; struct freenect_device_attributes** camera_prev_next = attribute_list; // Pass over the list. For each camera seen, if we already have a camera // for the newest_camera device, allocate a new one and append it to the list, // incrementing num_devs. Likewise for each audio device. struct libusb_device_descriptor desc; int num_cams = 0; int i; for (i = 0; i < count; i++) { int r = libusb_get_device_descriptor (devs[i], &desc); if (r < 0) continue; if (desc.idVendor == VID_MICROSOFT && (desc.idProduct == PID_NUI_CAMERA || desc.idProduct == PID_K4W_CAMERA)) { // Verify that a serial number exists to query. If not, don't touch the device. if (desc.iSerialNumber == 0) { continue; } // Open device. int res; libusb_device_handle *this_device; res = libusb_open(devs[i], &this_device); unsigned char string_desc[256]; // String descriptors are at most 256 bytes. if (res != 0) { continue; } // Read string descriptor referring to serial number. res = libusb_get_string_descriptor_ascii(this_device, desc.iSerialNumber, string_desc, 256); libusb_close(this_device); if (res < 0) { continue; } // Add item to linked list. struct freenect_device_attributes* new_dev_attrs = (struct freenect_device_attributes*)malloc(sizeof(struct freenect_device_attributes)); memset(new_dev_attrs, 0, sizeof(*new_dev_attrs)); *camera_prev_next = new_dev_attrs; // Copy string with serial number new_dev_attrs->camera_serial = strdup((char*)string_desc); camera_prev_next = &(new_dev_attrs->next); // Increment number of cameras found num_cams++; } } libusb_free_device_list(devs, 1); return num_cams; } FN_INTERNAL int fnusb_init(fnusb_ctx *ctx, freenect_usb_context *usb_ctx) { int res; if (!usb_ctx) { res = libusb_init(&ctx->ctx); if (res >= 0) { ctx->should_free_ctx = 1; return 0; } else { ctx->should_free_ctx = 0; ctx->ctx = NULL; return res; } } else { // explicit cast required: in WIN32, freenect_usb_context* maps to void* ctx->ctx = (libusb_context*)usb_ctx; ctx->should_free_ctx = 0; return 0; } } FN_INTERNAL int fnusb_shutdown(fnusb_ctx *ctx) { //int res; if (ctx->should_free_ctx) { libusb_exit(ctx->ctx); ctx->ctx = NULL; } return 0; } FN_INTERNAL int fnusb_process_events(fnusb_ctx *ctx) { return libusb_handle_events(ctx->ctx); } FN_INTERNAL int fnusb_process_events_timeout(fnusb_ctx *ctx, struct timeval* timeout) { return libusb_handle_events_timeout(ctx->ctx, timeout); } FN_INTERNAL int fnusb_open_subdevices(freenect_device *dev, int index) { freenect_context *ctx = dev->parent; dev->usb_cam.parent = dev; dev->usb_cam.dev = NULL; dev->usb_motor.parent = dev; dev->usb_motor.dev = NULL; #ifdef BUILD_AUDIO dev->usb_audio.parent = dev; dev->usb_audio.dev = NULL; #endif libusb_device **devs; //pointer to pointer of device, used to retrieve a list of devices ssize_t cnt = libusb_get_device_list (dev->parent->usb.ctx, &devs); //get the list of devices if (cnt < 0) return -1; int i = 0, nr_cam = 0, nr_mot = 0; #ifdef BUILD_AUDIO int nr_audio = 0; #endif int res; struct libusb_device_descriptor desc; for (i = 0; i < cnt; i++) { int r = libusb_get_device_descriptor (devs[i], &desc); if (r < 0) continue; if (desc.idVendor != VID_MICROSOFT) continue; res = 0; // Search for the camera if ((ctx->enabled_subdevices & FREENECT_DEVICE_CAMERA) && !dev->usb_cam.dev && (desc.idProduct == PID_NUI_CAMERA || desc.idProduct == PID_K4W_CAMERA)) { // If the index given by the user matches our camera index if (nr_cam == index) { res = libusb_open (devs[i], &dev->usb_cam.dev); if (res < 0 || !dev->usb_cam.dev) { FN_ERROR("Could not open camera: %d\n", res); dev->usb_cam.dev = NULL; break; } if(desc.idProduct == PID_K4W_CAMERA || desc.bcdDevice != fn_le32(267)){ /* Not the old kinect so we only set up the camera*/ ctx->enabled_subdevices = FREENECT_DEVICE_CAMERA; ctx->zero_plane_res = 334; }else{ /* The good old kinect that tilts and tweets */ ctx->zero_plane_res = 322; } #ifndef _WIN32 // Detach an existing kernel driver for the device res = libusb_kernel_driver_active(dev->usb_cam.dev, 0); if (res == 1) { res = libusb_detach_kernel_driver(dev->usb_cam.dev, 0); if (res < 0) { FN_ERROR("Could not detach kernel driver for camera: %d\n", res); libusb_close(dev->usb_cam.dev); dev->usb_cam.dev = NULL; break; } } #endif res = libusb_claim_interface (dev->usb_cam.dev, 0); if (res < 0) { FN_ERROR("Could not claim interface on camera: %d\n", res); libusb_close(dev->usb_cam.dev); dev->usb_cam.dev = NULL; break; } if(desc.idProduct == PID_K4W_CAMERA){ res = libusb_set_interface_alt_setting(dev->usb_cam.dev, 0, 1); if (res != 0) { FN_ERROR("Failed to set alternate interface #1 for K4W: %d\n", res); libusb_close(dev->usb_cam.dev); dev->usb_cam.dev = NULL; break; } } } else { nr_cam++; } } } if(ctx->enabled_subdevices == FREENECT_DEVICE_CAMERA || res < 0) cnt = 0; // Search for the motor for (i = 0; i < cnt; i++) { int r = libusb_get_device_descriptor (devs[i], &desc); if (r < 0) continue; if (desc.idVendor != VID_MICROSOFT) continue; if ((ctx->enabled_subdevices & FREENECT_DEVICE_MOTOR) && !dev->usb_motor.dev && desc.idProduct == PID_NUI_MOTOR) { // If the index given by the user matches our camera index if (nr_mot == index) { res = libusb_open (devs[i], &dev->usb_motor.dev); if (res < 0 || !dev->usb_motor.dev) { FN_ERROR("Could not open motor: %d\n", res); dev->usb_motor.dev = NULL; break; } res = libusb_claim_interface (dev->usb_motor.dev, 0); if (res < 0) { FN_ERROR("Could not claim interface on motor: %d\n", res); libusb_close(dev->usb_motor.dev); dev->usb_motor.dev = NULL; break; } } else { nr_mot++; } } #ifdef BUILD_AUDIO // TODO: check that the firmware has already been loaded; if not, upload firmware. // Search for the audio if ((ctx->enabled_subdevices & FREENECT_DEVICE_AUDIO) && !dev->usb_audio.dev && (desc.idProduct == PID_NUI_AUDIO || desc.idProduct == PID_K4W_AUDIO)) { // If the index given by the user matches our audio index if (nr_audio == index) { res = libusb_open (devs[i], &dev->usb_audio.dev); if (res < 0 || !dev->usb_audio.dev) { FN_ERROR("Could not open audio: %d\n", res); dev->usb_audio.dev = NULL; break; } res = libusb_claim_interface (dev->usb_audio.dev, 0); if (res < 0) { FN_ERROR("Could not claim interface on audio: %d\n", res); libusb_close(dev->usb_audio.dev); dev->usb_audio.dev = NULL; break; } // Using the device handle that we've claimed, see if this // device has already uploaded firmware (has 2 interfaces). If // not, save the serial number (by reading the appropriate // descriptor), upload the firmware, and then enter a loop // waiting for a device with the same serial number to // reappear. int num_interfaces = fnusb_num_interfaces(&dev->usb_audio); if (num_interfaces == 1) { // Read the serial number from the string descriptor and save it. unsigned char string_desc[256]; // String descriptors are at most 256 bytes res = libusb_get_string_descriptor_ascii(dev->usb_audio.dev, desc.iSerialNumber, string_desc, 256); if (res < 0) { FN_ERROR("Failed to retrieve serial number for audio device in bootloader state\n"); break; } char* audio_serial = strdup((char*)string_desc); FN_SPEW("Uploading firmware to audio device in bootloader state.\n"); res = upload_firmware(&dev->usb_audio); if (res < 0) { FN_ERROR("upload_firmware failed: %d\n", res); break; } libusb_close(dev->usb_audio.dev); dev->usb_audio.dev = NULL; // Wait for the device to reappear. int loops = 0; for (loops = 0; loops < 10; loops++) { // Loop for at most 10 tries. FN_SPEW("Try %d: Looking for new audio device matching serial %s\n", loops, audio_serial); // Scan devices. libusb_device **new_dev_list; int dev_index; ssize_t num_new_devs = libusb_get_device_list(ctx->usb.ctx, &new_dev_list); for (dev_index = 0; dev_index < num_new_devs; ++dev_index) { struct libusb_device_descriptor new_dev_desc; int r; r = libusb_get_device_descriptor (new_dev_list[dev_index], &new_dev_desc); if (r < 0) continue; // If this dev is a Kinect audio device, open device, read serial, and compare. if (new_dev_desc.idVendor == VID_MICROSOFT && new_dev_desc.idProduct == PID_NUI_AUDIO) { FN_SPEW("Matched VID/PID!\n"); libusb_device_handle* new_dev_handle; // Open device r = libusb_open(new_dev_list[dev_index], &new_dev_handle); if (r < 0) continue; // Read serial r = libusb_get_string_descriptor_ascii(new_dev_handle, new_dev_desc.iSerialNumber, string_desc, 256); if (r < 0) { FN_SPEW("Lost new audio device while fetching serial number.\n"); libusb_close(new_dev_handle); continue; } // Compare to expected serial if (r == strlen(audio_serial) && strcmp((char*)string_desc, audio_serial) == 0) { // We found it! r = libusb_claim_interface(new_dev_handle, 0); if (r != 0) { // Ouch, found the device but couldn't claim the interface. FN_SPEW("Device with serial %s reappeared but couldn't claim interface 0\n", audio_serial); libusb_close(new_dev_handle); continue; } // Save the device handle. dev->usb_audio.dev = new_dev_handle; // Verify that we've actually found a device running the right firmware. if (fnusb_num_interfaces(&dev->usb_audio) != 2) { FN_SPEW("Opened audio with matching serial but too few interfaces.\n"); dev->usb_audio.dev = NULL; libusb_close(new_dev_handle); continue; } break; } else { FN_SPEW("Got serial %s, expected serial %s\n", (char*)string_desc, audio_serial); } } } libusb_free_device_list(new_dev_list, 1); // If we found the right device, break out of this loop. if (dev->usb_audio.dev) break; // Sleep for a second to give the device more time to reenumerate. sleep(1); } free(audio_serial); } } else { nr_audio++; } } #endif } libusb_free_device_list (devs, 1); // free the list, unref the devices in it // Check that each subdevice is either opened or not enabled. if ( (dev->usb_cam.dev || !(ctx->enabled_subdevices & FREENECT_DEVICE_CAMERA)) && (dev->usb_motor.dev || !(ctx->enabled_subdevices & FREENECT_DEVICE_MOTOR)) #ifdef BUILD_AUDIO && (dev->usb_audio.dev || !(ctx->enabled_subdevices & FREENECT_DEVICE_AUDIO)) #endif ) { return 0; } else { if (dev->usb_cam.dev) { libusb_release_interface(dev->usb_cam.dev, 0); libusb_close(dev->usb_cam.dev); } if (dev->usb_motor.dev) { libusb_release_interface(dev->usb_motor.dev, 0); libusb_close(dev->usb_motor.dev); } #ifdef BUILD_AUDIO if (dev->usb_audio.dev) { libusb_release_interface(dev->usb_audio.dev, 0); libusb_close(dev->usb_audio.dev); } #endif return -1; } } FN_INTERNAL int fnusb_close_subdevices(freenect_device *dev) { if (dev->usb_cam.dev) { libusb_release_interface(dev->usb_cam.dev, 0); #ifndef _WIN32 libusb_attach_kernel_driver(dev->usb_cam.dev, 0); #endif libusb_close(dev->usb_cam.dev); dev->usb_cam.dev = NULL; } if (dev->usb_motor.dev) { libusb_release_interface(dev->usb_motor.dev, 0); libusb_close(dev->usb_motor.dev); dev->usb_motor.dev = NULL; } #ifdef BUILD_AUDIO if (dev->usb_audio.dev) { libusb_release_interface(dev->usb_audio.dev, 0); libusb_close(dev->usb_audio.dev); dev->usb_audio.dev = NULL; } #endif return 0; } static void iso_callback(struct libusb_transfer *xfer) { int i; fnusb_isoc_stream *strm = (fnusb_isoc_stream*)xfer->user_data; freenect_context *ctx = strm->parent->parent->parent; if (strm->dead) { strm->dead_xfers++; FN_SPEW("EP %02x transfer complete, %d left\n", xfer->endpoint, strm->num_xfers - strm->dead_xfers); return; } switch(xfer->status) { case LIBUSB_TRANSFER_COMPLETED: // Normal operation. { uint8_t *buf = (uint8_t*)xfer->buffer; for (i=0; ipkts; i++) { strm->cb(strm->parent->parent, buf, xfer->iso_packet_desc[i].actual_length); buf += strm->len; } int res; res = libusb_submit_transfer(xfer); if (res != 0) { FN_ERROR("iso_callback(): failed to resubmit transfer after successful completion: %d\n", res); strm->dead_xfers++; if (res == LIBUSB_ERROR_NO_DEVICE) { strm->parent->device_dead = 1; } } break; } case LIBUSB_TRANSFER_NO_DEVICE: { // We lost the device we were talking to. This is a large problem, // and one that we should eventually come up with a way to // properly propagate up to the caller. if(!strm->parent->device_dead) { FN_ERROR("USB device disappeared, cancelling stream %02x :(\n", xfer->endpoint); } strm->dead_xfers++; strm->parent->device_dead = 1; break; } case LIBUSB_TRANSFER_CANCELLED: { if(strm->dead) { FN_SPEW("EP %02x transfer cancelled\n", xfer->endpoint); } else { // This seems to be a libusb bug on OSX - instead of completing // the transfer with LIBUSB_TRANSFER_NO_DEVICE, the transfers // simply come back cancelled by the OS. We can detect this, // though - the stream should be marked dead if we're // intentionally cancelling transfers. if(!strm->parent->device_dead) { FN_ERROR("Got cancelled transfer, but we didn't request it - device disconnected?\n"); } strm->parent->device_dead = 1; } strm->dead_xfers++; break; } default: { // On other errors, resubmit the transfer - in particular, libusb // on OSX tends to hit random errors a lot. If we don't resubmit // the transfers, eventually all of them die and then we don't get // any more data from the Kinect. FN_WARNING("Isochronous transfer error: %d\n", xfer->status); int res; res = libusb_submit_transfer(xfer); if (res != 0) { FN_ERROR("Isochronous transfer resubmission failed after unknown error: %d\n", res); strm->dead_xfers++; if (res == LIBUSB_ERROR_NO_DEVICE) { strm->parent->device_dead = 1; } } break; } } } FN_INTERNAL int fnusb_start_iso(fnusb_dev *dev, fnusb_isoc_stream *strm, fnusb_iso_cb cb, int ep, int xfers, int pkts, int len) { freenect_context *ctx = dev->parent->parent; int ret, i; strm->parent = dev; strm->cb = cb; strm->num_xfers = xfers; strm->pkts = pkts; strm->len = len; strm->buffer = (uint8_t*)malloc(xfers * pkts * len); strm->xfers = (struct libusb_transfer**)malloc(sizeof(struct libusb_transfer*) * xfers); strm->dead = 0; strm->dead_xfers = 0; uint8_t *bufp = strm->buffer; for (i=0; ixfers[i] = libusb_alloc_transfer(pkts); libusb_fill_iso_transfer(strm->xfers[i], dev->dev, ep, bufp, pkts * len, pkts, iso_callback, strm, 0); libusb_set_iso_packet_lengths(strm->xfers[i], len); ret = libusb_submit_transfer(strm->xfers[i]); if (ret < 0) { FN_WARNING("Failed to submit isochronous transfer %d: %d\n", i, ret); strm->dead_xfers++; } bufp += pkts*len; } return 0; } FN_INTERNAL int fnusb_stop_iso(fnusb_dev *dev, fnusb_isoc_stream *strm) { freenect_context *ctx = dev->parent->parent; int i; FN_FLOOD("fnusb_stop_iso() called\n"); strm->dead = 1; for (i=0; inum_xfers; i++) libusb_cancel_transfer(strm->xfers[i]); FN_FLOOD("fnusb_stop_iso() cancelled all transfers\n"); while (strm->dead_xfers < strm->num_xfers) { FN_FLOOD("fnusb_stop_iso() dead = %d\tnum = %d\n", strm->dead_xfers, strm->num_xfers); libusb_handle_events(ctx->usb.ctx); } for (i=0; inum_xfers; i++) libusb_free_transfer(strm->xfers[i]); FN_FLOOD("fnusb_stop_iso() freed all transfers\n"); free(strm->buffer); free(strm->xfers); FN_FLOOD("fnusb_stop_iso() freed buffers and stream\n"); memset(strm, 0, sizeof(*strm)); FN_FLOOD("fnusb_stop_iso() done\n"); return 0; } FN_INTERNAL int fnusb_control(fnusb_dev *dev, uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint8_t *data, uint16_t wLength) { return libusb_control_transfer(dev->dev, bmRequestType, bRequest, wValue, wIndex, data, wLength, 0); } #ifdef BUILD_AUDIO FN_INTERNAL int fnusb_bulk(fnusb_dev *dev, uint8_t endpoint, uint8_t *data, int len, int *transferred) { *transferred = 0; return libusb_bulk_transfer(dev->dev, endpoint, data, len, transferred, 0); } FN_INTERNAL int fnusb_num_interfaces(fnusb_dev *dev) { int retval = 0; int res; libusb_device* d = libusb_get_device(dev->dev); struct libusb_config_descriptor* config; res = libusb_get_active_config_descriptor(d, &config); if (res < 0) // Something went wrong return res; retval = config->bNumInterfaces; libusb_free_config_descriptor(config); return retval; } #endif ================================================ FILE: libs/libfreenect/src/usb_libusb10.h ================================================ /* * This file is part of the OpenKinect Project. http://www.openkinect.org * * Copyright (c) 2010 individual OpenKinect contributors. See the CONTRIB file * for details. * * This code is licensed to you under the terms of the Apache License, version * 2.0, or, at your option, the terms of the GNU General Public License, * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses, * or the following URLs: * http://www.apache.org/licenses/LICENSE-2.0 * http://www.gnu.org/licenses/gpl-2.0.txt * * If you redistribute this file in source form, modified or unmodified, you * may: * 1) Leave this header intact and distribute it under the same terms, * accompanying it with the APACHE20 and GPL20 files, or * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file * In all cases you must keep the copyright notice intact and include a copy * of the CONTRIB file. * * Binary distributions must follow the binary distribution requirements of * either License. */ #ifndef USB_LIBUSB10 #define USB_LIBUSB10 #include "libfreenect.h" #include #if defined(__APPLE__) /* From Github Issue 22 by Roefer - https://github.com/OpenKinect/libfreenect/issues/#issue/22 The current implementation still does not reach 30 Hz on MacOS. This is due to bad scheduling of USB transfers in libusb (Ed Note: libusb 1.0.8). A fix can be found at http://www.informatik.uni-bremen.de/~roefer/libusb/libusb-osx-kinect.diff (Ed Note: patch applies to libusb repo at 7da756e09fd) In camera.c, I use PKTS_PER_XFER = 128, NUM_XFERS = 4. There are a few rules: PKTS_PER_XFER * NUM_XFERS <= 1000, PKTS_PER_XFER % 8 == 0. */ #define PKTS_PER_XFER 128 #define NUM_XFERS 4 #define DEPTH_PKTBUF 2048 #define VIDEO_PKTBUF 2048 #else #ifdef _WIN32 #define PKTS_PER_XFER 32 #define NUM_XFERS 8 #else #define PKTS_PER_XFER 16 #define NUM_XFERS 16 #endif #define DEPTH_PKTBUF 1920 #define VIDEO_PKTBUF 1920 #endif typedef struct { libusb_context *ctx; int should_free_ctx; } fnusb_ctx; typedef struct { freenect_device *parent; //so we can go up from the libusb userdata libusb_device_handle *dev; int device_dead; // set to 1 when the underlying libusb_device_handle vanishes (ie, Kinect was unplugged) } fnusb_dev; typedef struct { fnusb_dev *parent; //so we can go up from the libusb userdata struct libusb_transfer **xfers; uint8_t *buffer; fnusb_iso_cb cb; int num_xfers; int pkts; int len; int dead; int dead_xfers; } fnusb_isoc_stream; int fnusb_num_devices(fnusb_ctx *ctx); int fnusb_list_device_attributes(fnusb_ctx *ctx, struct freenect_device_attributes** attribute_list); int fnusb_init(fnusb_ctx *ctx, freenect_usb_context *usb_ctx); int fnusb_shutdown(fnusb_ctx *ctx); int fnusb_process_events(fnusb_ctx *ctx); int fnusb_process_events_timeout(fnusb_ctx *ctx, struct timeval* timeout); int fnusb_open_subdevices(freenect_device *dev, int index); int fnusb_close_subdevices(freenect_device *dev); int fnusb_start_iso(fnusb_dev *dev, fnusb_isoc_stream *strm, fnusb_iso_cb cb, int ep, int xfers, int pkts, int len); int fnusb_stop_iso(fnusb_dev *dev, fnusb_isoc_stream *strm); int fnusb_control(fnusb_dev *dev, uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint8_t *data, uint16_t wLength); #ifdef BUILD_AUDIO int fnusb_bulk(fnusb_dev *dev, uint8_t endpoint, uint8_t *data, int len, int *transferred); int fnusb_num_interfaces(fnusb_dev *dev); #endif #endif ================================================ FILE: libs/libusb-1.0/include/libusb-1.0/libusb.h ================================================ /* * Public libusb header file * Copyright (C) 2007-2008 Daniel Drake * Copyright (c) 2001 Johannes Erdfelt * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #ifndef __LIBUSB_H__ #define __LIBUSB_H__ /* MSVC doesn't like inline, but does accept __inline ?? */ #ifdef _MSC_VER #define inline __inline #endif #include #include #include #include #if defined(__linux) || defined(__APPLE__) || defined(__CYGWIN__) #include #endif /* 'interface' might be defined as a macro on Windows, so we need to * undefine it so as not to break the current libusb API, because * libusb_config_descriptor has an 'interface' member * As this can be problematic if you include windows.h after libusb.h * in your sources, we force windows.h to be included first. */ #if defined(_WIN32) || defined(__CYGWIN__) #include #if defined(interface) #undef interface #endif #endif /** \def LIBUSB_CALL * \ingroup misc * libusb's Windows calling convention. * * Under Windows, the selection of available compilers and configurations * means that, unlike other platforms, there is not one true calling * convention (calling convention: the manner in which parameters are * passed to funcions in the generated assembly code). * * Matching the Windows API itself, libusb uses the WINAPI convention (which * translates to the stdcall convention) and guarantees that the * library is compiled in this way. The public header file also includes * appropriate annotations so that your own software will use the right * convention, even if another convention is being used by default within * your codebase. * * The one consideration that you must apply in your software is to mark * all functions which you use as libusb callbacks with this LIBUSB_CALL * annotation, so that they too get compiled for the correct calling * convention. * * On non-Windows operating systems, this macro is defined as nothing. This * means that you can apply it to your code without worrying about * cross-platform compatibility. */ /* LIBUSB_CALL must be defined on both definition and declaration of libusb * functions. You'd think that declaration would be enough, but cygwin will * complain about conflicting types unless both are marked this way. * The placement of this macro is important too; it must appear after the * return type, before the function name. See internal documentation for * API_EXPORTED. */ #if defined(_WIN32) || defined(__CYGWIN__) #define LIBUSB_CALL WINAPI #else #define LIBUSB_CALL #endif #ifdef __cplusplus extern "C" { #endif /** \def libusb_cpu_to_le16 * \ingroup misc * Convert a 16-bit value from host-endian to little-endian format. On * little endian systems, this function does nothing. On big endian systems, * the bytes are swapped. * \param x the host-endian value to convert * \returns the value in little-endian byte order */ static inline uint16_t libusb_cpu_to_le16(const uint16_t x) { union { uint8_t b8[2]; uint16_t b16; } _tmp; _tmp.b8[1] = x >> 8; _tmp.b8[0] = x & 0xff; return _tmp.b16; } /** \def libusb_le16_to_cpu * \ingroup misc * Convert a 16-bit value from little-endian to host-endian format. On * little endian systems, this function does nothing. On big endian systems, * the bytes are swapped. * \param x the little-endian value to convert * \returns the value in host-endian byte order */ #define libusb_le16_to_cpu libusb_cpu_to_le16 /* standard USB stuff */ /** \ingroup desc * Device and/or Interface Class codes */ enum libusb_class_code { /** In the context of a \ref libusb_device_descriptor "device descriptor", * this bDeviceClass value indicates that each interface specifies its * own class information and all interfaces operate independently. */ LIBUSB_CLASS_PER_INTERFACE = 0, /** Audio class */ LIBUSB_CLASS_AUDIO = 1, /** Communications class */ LIBUSB_CLASS_COMM = 2, /** Human Interface Device class */ LIBUSB_CLASS_HID = 3, /** Printer dclass */ LIBUSB_CLASS_PRINTER = 7, /** Picture transfer protocol class */ LIBUSB_CLASS_PTP = 6, /** Mass storage class */ LIBUSB_CLASS_MASS_STORAGE = 8, /** Hub class */ LIBUSB_CLASS_HUB = 9, /** Data class */ LIBUSB_CLASS_DATA = 10, /** Wireless class */ LIBUSB_CLASS_WIRELESS = 0xe0, /** Application class */ LIBUSB_CLASS_APPLICATION = 0xfe, /** Class is vendor-specific */ LIBUSB_CLASS_VENDOR_SPEC = 0xff }; /** \ingroup desc * Descriptor types as defined by the USB specification. */ enum libusb_descriptor_type { /** Device descriptor. See libusb_device_descriptor. */ LIBUSB_DT_DEVICE = 0x01, /** Configuration descriptor. See libusb_config_descriptor. */ LIBUSB_DT_CONFIG = 0x02, /** String descriptor */ LIBUSB_DT_STRING = 0x03, /** Interface descriptor. See libusb_interface_descriptor. */ LIBUSB_DT_INTERFACE = 0x04, /** Endpoint descriptor. See libusb_endpoint_descriptor. */ LIBUSB_DT_ENDPOINT = 0x05, /** HID descriptor */ LIBUSB_DT_HID = 0x21, /** HID report descriptor */ LIBUSB_DT_REPORT = 0x22, /** Physical descriptor */ LIBUSB_DT_PHYSICAL = 0x23, /** Hub descriptor */ LIBUSB_DT_HUB = 0x29 }; /* Descriptor sizes per descriptor type */ #define LIBUSB_DT_DEVICE_SIZE 18 #define LIBUSB_DT_CONFIG_SIZE 9 #define LIBUSB_DT_INTERFACE_SIZE 9 #define LIBUSB_DT_ENDPOINT_SIZE 7 #define LIBUSB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */ #define LIBUSB_DT_HUB_NONVAR_SIZE 7 #define LIBUSB_ENDPOINT_ADDRESS_MASK 0x0f /* in bEndpointAddress */ #define LIBUSB_ENDPOINT_DIR_MASK 0x80 /** \ingroup desc * Endpoint direction. Values for bit 7 of the * \ref libusb_endpoint_descriptor::bEndpointAddress "endpoint address" scheme. */ enum libusb_endpoint_direction { /** In: device-to-host */ LIBUSB_ENDPOINT_IN = 0x80, /** Out: host-to-device */ LIBUSB_ENDPOINT_OUT = 0x00 }; #define LIBUSB_TRANSFER_TYPE_MASK 0x03 /* in bmAttributes */ /** \ingroup desc * Endpoint transfer type. Values for bits 0:1 of the * \ref libusb_endpoint_descriptor::bmAttributes "endpoint attributes" field. */ enum libusb_transfer_type { /** Control endpoint */ LIBUSB_TRANSFER_TYPE_CONTROL = 0, /** Isochronous endpoint */ LIBUSB_TRANSFER_TYPE_ISOCHRONOUS = 1, /** Bulk endpoint */ LIBUSB_TRANSFER_TYPE_BULK = 2, /** Interrupt endpoint */ LIBUSB_TRANSFER_TYPE_INTERRUPT = 3 }; /** \ingroup misc * Standard requests, as defined in table 9-3 of the USB2 specifications */ enum libusb_standard_request { /** Request status of the specific recipient */ LIBUSB_REQUEST_GET_STATUS = 0x00, /** Clear or disable a specific feature */ LIBUSB_REQUEST_CLEAR_FEATURE = 0x01, /* 0x02 is reserved */ /** Set or enable a specific feature */ LIBUSB_REQUEST_SET_FEATURE = 0x03, /* 0x04 is reserved */ /** Set device address for all future accesses */ LIBUSB_REQUEST_SET_ADDRESS = 0x05, /** Get the specified descriptor */ LIBUSB_REQUEST_GET_DESCRIPTOR = 0x06, /** Used to update existing descriptors or add new descriptors */ LIBUSB_REQUEST_SET_DESCRIPTOR = 0x07, /** Get the current device configuration value */ LIBUSB_REQUEST_GET_CONFIGURATION = 0x08, /** Set device configuration */ LIBUSB_REQUEST_SET_CONFIGURATION = 0x09, /** Return the selected alternate setting for the specified interface */ LIBUSB_REQUEST_GET_INTERFACE = 0x0A, /** Select an alternate interface for the specified interface */ LIBUSB_REQUEST_SET_INTERFACE = 0x0B, /** Set then report an endpoint's synchronization frame */ LIBUSB_REQUEST_SYNCH_FRAME = 0x0C }; /** \ingroup misc * Request type bits of the * \ref libusb_control_setup::bmRequestType "bmRequestType" field in control * transfers. */ enum libusb_request_type { /** Standard */ LIBUSB_REQUEST_TYPE_STANDARD = (0x00 << 5), /** Class */ LIBUSB_REQUEST_TYPE_CLASS = (0x01 << 5), /** Vendor */ LIBUSB_REQUEST_TYPE_VENDOR = (0x02 << 5), /** Reserved */ LIBUSB_REQUEST_TYPE_RESERVED = (0x03 << 5) }; /** \ingroup misc * Recipient bits of the * \ref libusb_control_setup::bmRequestType "bmRequestType" field in control * transfers. Values 4 through 31 are reserved. */ enum libusb_request_recipient { /** Device */ LIBUSB_RECIPIENT_DEVICE = 0x00, /** Interface */ LIBUSB_RECIPIENT_INTERFACE = 0x01, /** Endpoint */ LIBUSB_RECIPIENT_ENDPOINT = 0x02, /** Other */ LIBUSB_RECIPIENT_OTHER = 0x03 }; #define LIBUSB_ISO_SYNC_TYPE_MASK 0x0C /** \ingroup desc * Synchronization type for isochronous endpoints. Values for bits 2:3 of the * \ref libusb_endpoint_descriptor::bmAttributes "bmAttributes" field in * libusb_endpoint_descriptor. */ enum libusb_iso_sync_type { /** No synchronization */ LIBUSB_ISO_SYNC_TYPE_NONE = 0, /** Asynchronous */ LIBUSB_ISO_SYNC_TYPE_ASYNC = 1, /** Adaptive */ LIBUSB_ISO_SYNC_TYPE_ADAPTIVE = 2, /** Synchronous */ LIBUSB_ISO_SYNC_TYPE_SYNC = 3 }; #define LIBUSB_ISO_USAGE_TYPE_MASK 0x30 /** \ingroup desc * Usage type for isochronous endpoints. Values for bits 4:5 of the * \ref libusb_endpoint_descriptor::bmAttributes "bmAttributes" field in * libusb_endpoint_descriptor. */ enum libusb_iso_usage_type { /** Data endpoint */ LIBUSB_ISO_USAGE_TYPE_DATA = 0, /** Feedback endpoint */ LIBUSB_ISO_USAGE_TYPE_FEEDBACK = 1, /** Implicit feedback Data endpoint */ LIBUSB_ISO_USAGE_TYPE_IMPLICIT = 2 }; /** \ingroup desc * A structure representing the standard USB device descriptor. This * descriptor is documented in section 9.6.1 of the USB 2.0 specification. * All multiple-byte fields are represented in host-endian format. */ struct libusb_device_descriptor { /** Size of this descriptor (in bytes) */ uint8_t bLength; /** Descriptor type. Will have value * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE LIBUSB_DT_DEVICE in this * context. */ uint8_t bDescriptorType; /** USB specification release number in binary-coded decimal. A value of * 0x0200 indicates USB 2.0, 0x0110 indicates USB 1.1, etc. */ uint16_t bcdUSB; /** USB-IF class code for the device. See \ref libusb_class_code. */ uint8_t bDeviceClass; /** USB-IF subclass code for the device, qualified by the bDeviceClass * value */ uint8_t bDeviceSubClass; /** USB-IF protocol code for the device, qualified by the bDeviceClass and * bDeviceSubClass values */ uint8_t bDeviceProtocol; /** Maximum packet size for endpoint 0 */ uint8_t bMaxPacketSize0; /** USB-IF vendor ID */ uint16_t idVendor; /** USB-IF product ID */ uint16_t idProduct; /** Device release number in binary-coded decimal */ uint16_t bcdDevice; /** Index of string descriptor describing manufacturer */ uint8_t iManufacturer; /** Index of string descriptor describing product */ uint8_t iProduct; /** Index of string descriptor containing device serial number */ uint8_t iSerialNumber; /** Number of possible configurations */ uint8_t bNumConfigurations; }; /** \ingroup desc * A structure representing the standard USB endpoint descriptor. This * descriptor is documented in section 9.6.3 of the USB 2.0 specification. * All multiple-byte fields are represented in host-endian format. */ struct libusb_endpoint_descriptor { /** Size of this descriptor (in bytes) */ uint8_t bLength; /** Descriptor type. Will have value * \ref libusb_descriptor_type::LIBUSB_DT_ENDPOINT LIBUSB_DT_ENDPOINT in * this context. */ uint8_t bDescriptorType; /** The address of the endpoint described by this descriptor. Bits 0:3 are * the endpoint number. Bits 4:6 are reserved. Bit 7 indicates direction, * see \ref libusb_endpoint_direction. */ uint8_t bEndpointAddress; /** Attributes which apply to the endpoint when it is configured using * the bConfigurationValue. Bits 0:1 determine the transfer type and * correspond to \ref libusb_transfer_type. Bits 2:3 are only used for * isochronous endpoints and correspond to \ref libusb_iso_sync_type. * Bits 4:5 are also only used for isochronous endpoints and correspond to * \ref libusb_iso_usage_type. Bits 6:7 are reserved. */ uint8_t bmAttributes; /** Maximum packet size this endpoint is capable of sending/receiving. */ uint16_t wMaxPacketSize; /** Interval for polling endpoint for data transfers. */ uint8_t bInterval; /** For audio devices only: the rate at which synchronization feedback * is provided. */ uint8_t bRefresh; /** For audio devices only: the address if the synch endpoint */ uint8_t bSynchAddress; /** Extra descriptors. If libusb encounters unknown endpoint descriptors, * it will store them here, should you wish to parse them. */ const unsigned char *extra; /** Length of the extra descriptors, in bytes. */ int extra_length; }; /** \ingroup desc * A structure representing the standard USB interface descriptor. This * descriptor is documented in section 9.6.5 of the USB 2.0 specification. * All multiple-byte fields are represented in host-endian format. */ struct libusb_interface_descriptor { /** Size of this descriptor (in bytes) */ uint8_t bLength; /** Descriptor type. Will have value * \ref libusb_descriptor_type::LIBUSB_DT_INTERFACE LIBUSB_DT_INTERFACE * in this context. */ uint8_t bDescriptorType; /** Number of this interface */ uint8_t bInterfaceNumber; /** Value used to select this alternate setting for this interface */ uint8_t bAlternateSetting; /** Number of endpoints used by this interface (excluding the control * endpoint). */ uint8_t bNumEndpoints; /** USB-IF class code for this interface. See \ref libusb_class_code. */ uint8_t bInterfaceClass; /** USB-IF subclass code for this interface, qualified by the * bInterfaceClass value */ uint8_t bInterfaceSubClass; /** USB-IF protocol code for this interface, qualified by the * bInterfaceClass and bInterfaceSubClass values */ uint8_t bInterfaceProtocol; /** Index of string descriptor describing this interface */ uint8_t iInterface; /** Array of endpoint descriptors. This length of this array is determined * by the bNumEndpoints field. */ const struct libusb_endpoint_descriptor *endpoint; /** Extra descriptors. If libusb encounters unknown interface descriptors, * it will store them here, should you wish to parse them. */ const unsigned char *extra; /** Length of the extra descriptors, in bytes. */ int extra_length; }; /** \ingroup desc * A collection of alternate settings for a particular USB interface. */ struct libusb_interface { /** Array of interface descriptors. The length of this array is determined * by the num_altsetting field. */ const struct libusb_interface_descriptor *altsetting; /** The number of alternate settings that belong to this interface */ int num_altsetting; }; /** \ingroup desc * A structure representing the standard USB configuration descriptor. This * descriptor is documented in section 9.6.3 of the USB 2.0 specification. * All multiple-byte fields are represented in host-endian format. */ struct libusb_config_descriptor { /** Size of this descriptor (in bytes) */ uint8_t bLength; /** Descriptor type. Will have value * \ref libusb_descriptor_type::LIBUSB_DT_CONFIG LIBUSB_DT_CONFIG * in this context. */ uint8_t bDescriptorType; /** Total length of data returned for this configuration */ uint16_t wTotalLength; /** Number of interfaces supported by this configuration */ uint8_t bNumInterfaces; /** Identifier value for this configuration */ uint8_t bConfigurationValue; /** Index of string descriptor describing this configuration */ uint8_t iConfiguration; /** Configuration characteristics */ uint8_t bmAttributes; /** Maximum power consumption of the USB device from this bus in this * configuration when the device is fully opreation. Expressed in units * of 2 mA. */ uint8_t MaxPower; /** Array of interfaces supported by this configuration. The length of * this array is determined by the bNumInterfaces field. */ const struct libusb_interface *interface; /** Extra descriptors. If libusb encounters unknown configuration * descriptors, it will store them here, should you wish to parse them. */ const unsigned char *extra; /** Length of the extra descriptors, in bytes. */ int extra_length; }; /** \ingroup asyncio * Setup packet for control transfers. */ struct libusb_control_setup { /** Request type. Bits 0:4 determine recipient, see * \ref libusb_request_recipient. Bits 5:6 determine type, see * \ref libusb_request_type. Bit 7 determines data transfer direction, see * \ref libusb_endpoint_direction. */ uint8_t bmRequestType; /** Request. If the type bits of bmRequestType are equal to * \ref libusb_request_type::LIBUSB_REQUEST_TYPE_STANDARD * "LIBUSB_REQUEST_TYPE_STANDARD" then this field refers to * \ref libusb_standard_request. For other cases, use of this field is * application-specific. */ uint8_t bRequest; /** Value. Varies according to request */ uint16_t wValue; /** Index. Varies according to request, typically used to pass an index * or offset */ uint16_t wIndex; /** Number of bytes to transfer */ uint16_t wLength; }; #define LIBUSB_CONTROL_SETUP_SIZE (sizeof(struct libusb_control_setup)) /* libusb */ struct libusb_context; struct libusb_device; struct libusb_device_handle; /** \ingroup lib * Structure representing a libusb session. The concept of individual libusb * sessions allows for your program to use two libraries (or dynamically * load two modules) which both independently use libusb. This will prevent * interference between the individual libusb users - for example * libusb_set_debug() will not affect the other user of the library, and * libusb_exit() will not destroy resources that the other user is still * using. * * Sessions are created by libusb_init() and destroyed through libusb_exit(). * If your application is guaranteed to only ever include a single libusb * user (i.e. you), you do not have to worry about contexts: pass NULL in * every function call where a context is required. The default context * will be used. * * For more information, see \ref contexts. */ typedef struct libusb_context libusb_context; /** \ingroup dev * Structure representing a USB device detected on the system. This is an * opaque type for which you are only ever provided with a pointer, usually * originating from libusb_get_device_list(). * * Certain operations can be performed on a device, but in order to do any * I/O you will have to first obtain a device handle using libusb_open(). * * Devices are reference counted with libusb_device_ref() and * libusb_device_unref(), and are freed when the reference count reaches 0. * New devices presented by libusb_get_device_list() have a reference count of * 1, and libusb_free_device_list() can optionally decrease the reference count * on all devices in the list. libusb_open() adds another reference which is * later destroyed by libusb_close(). */ typedef struct libusb_device libusb_device; /** \ingroup dev * Structure representing a handle on a USB device. This is an opaque type for * which you are only ever provided with a pointer, usually originating from * libusb_open(). * * A device handle is used to perform I/O and other operations. When finished * with a device handle, you should call libusb_close(). */ typedef struct libusb_device_handle libusb_device_handle; /** \ingroup misc * Error codes. Most libusb functions return 0 on success or one of these * codes on failure. * You can use libusb_strerror() to retrieve a short string description of * a libusb_error enumeration value. */ enum libusb_error { /** Success (no error) */ LIBUSB_SUCCESS = 0, /** Input/output error */ LIBUSB_ERROR_IO = -1, /** Invalid parameter */ LIBUSB_ERROR_INVALID_PARAM = -2, /** Access denied (insufficient permissions) */ LIBUSB_ERROR_ACCESS = -3, /** No such device (it may have been disconnected) */ LIBUSB_ERROR_NO_DEVICE = -4, /** Entity not found */ LIBUSB_ERROR_NOT_FOUND = -5, /** Resource busy */ LIBUSB_ERROR_BUSY = -6, /** Operation timed out */ LIBUSB_ERROR_TIMEOUT = -7, /** Overflow */ LIBUSB_ERROR_OVERFLOW = -8, /** Pipe error */ LIBUSB_ERROR_PIPE = -9, /** System call interrupted (perhaps due to signal) */ LIBUSB_ERROR_INTERRUPTED = -10, /** Insufficient memory */ LIBUSB_ERROR_NO_MEM = -11, /** Operation not supported or unimplemented on this platform */ LIBUSB_ERROR_NOT_SUPPORTED = -12, /** Other error */ LIBUSB_ERROR_OTHER = -99 /* IMPORTANT: when adding new values to this enum, remember to update the libusb_strerror() function implementation! */ }; /** \ingroup asyncio * Transfer status codes */ enum libusb_transfer_status { /** Transfer completed without error. Note that this does not indicate * that the entire amount of requested data was transferred. */ LIBUSB_TRANSFER_COMPLETED, /** Transfer failed */ LIBUSB_TRANSFER_ERROR, /** Transfer timed out */ LIBUSB_TRANSFER_TIMED_OUT, /** Transfer was cancelled */ LIBUSB_TRANSFER_CANCELLED, /** For bulk/interrupt endpoints: halt condition detected (endpoint * stalled). For control endpoints: control request not supported. */ LIBUSB_TRANSFER_STALL, /** Device was disconnected */ LIBUSB_TRANSFER_NO_DEVICE, /** Device sent more data than requested */ LIBUSB_TRANSFER_OVERFLOW }; /** \ingroup asyncio * libusb_transfer.flags values */ enum libusb_transfer_flags { /** Report short frames as errors */ LIBUSB_TRANSFER_SHORT_NOT_OK = 1<<0, /** Automatically free() transfer buffer during libusb_free_transfer() */ LIBUSB_TRANSFER_FREE_BUFFER = 1<<1, /** Automatically call libusb_free_transfer() after callback returns. * If this flag is set, it is illegal to call libusb_free_transfer() * from your transfer callback, as this will result in a double-free * when this flag is acted upon. */ LIBUSB_TRANSFER_FREE_TRANSFER = 1<<2 }; /** \ingroup asyncio * Isochronous packet descriptor. */ struct libusb_iso_packet_descriptor { /** Length of data to request in this packet */ unsigned int length; /** Amount of data that was actually transferred */ unsigned int actual_length; /** Status code for this packet */ enum libusb_transfer_status status; }; struct libusb_transfer; /** \ingroup asyncio * Asynchronous transfer callback function type. When submitting asynchronous * transfers, you pass a pointer to a callback function of this type via the * \ref libusb_transfer::callback "callback" member of the libusb_transfer * structure. libusb will call this function later, when the transfer has * completed or failed. See \ref asyncio for more information. * \param transfer The libusb_transfer struct the callback function is being * notified about. */ typedef void (LIBUSB_CALL *libusb_transfer_cb_fn)(struct libusb_transfer *transfer); /** \ingroup asyncio * The generic USB transfer structure. The user populates this structure and * then submits it in order to request a transfer. After the transfer has * completed, the library populates the transfer with the results and passes * it back to the user. */ struct libusb_transfer { /** Handle of the device that this transfer will be submitted to */ libusb_device_handle *dev_handle; /** A bitwise OR combination of \ref libusb_transfer_flags. */ uint8_t flags; /** Address of the endpoint where this transfer will be sent. */ unsigned char endpoint; /** Type of the endpoint from \ref libusb_transfer_type */ unsigned char type; /** Timeout for this transfer in millseconds. A value of 0 indicates no * timeout. */ unsigned int timeout; /** The status of the transfer. Read-only, and only for use within * transfer callback function. * * If this is an isochronous transfer, this field may read COMPLETED even * if there were errors in the frames. Use the * \ref libusb_iso_packet_descriptor::status "status" field in each packet * to determine if errors occurred. */ enum libusb_transfer_status status; /** Length of the data buffer */ int length; /** Actual length of data that was transferred. Read-only, and only for * use within transfer callback function. Not valid for isochronous * endpoint transfers. */ int actual_length; /** Callback function. This will be invoked when the transfer completes, * fails, or is cancelled. */ libusb_transfer_cb_fn callback; /** User context data to pass to the callback function. */ void *user_data; /** Data buffer */ unsigned char *buffer; /** Number of isochronous packets. Only used for I/O with isochronous * endpoints. */ int num_iso_packets; /** Isochronous packet descriptors, for isochronous transfers only. */ struct libusb_iso_packet_descriptor iso_packet_desc #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) [] /* valid C99 code */ #else [0] /* non-standard, but usually working code */ #endif ; }; int LIBUSB_CALL libusb_init(libusb_context **ctx); void LIBUSB_CALL libusb_exit(libusb_context *ctx); void LIBUSB_CALL libusb_set_debug(libusb_context *ctx, int level); const char * LIBUSB_CALL libusb_strerror(enum libusb_error errcode); ssize_t LIBUSB_CALL libusb_get_device_list(libusb_context *ctx, libusb_device ***list); void LIBUSB_CALL libusb_free_device_list(libusb_device **list, int unref_devices); libusb_device * LIBUSB_CALL libusb_ref_device(libusb_device *dev); void LIBUSB_CALL libusb_unref_device(libusb_device *dev); int LIBUSB_CALL libusb_get_configuration(libusb_device_handle *dev, int *config); int LIBUSB_CALL libusb_get_device_descriptor(libusb_device *dev, struct libusb_device_descriptor *desc); int LIBUSB_CALL libusb_get_active_config_descriptor(libusb_device *dev, struct libusb_config_descriptor **config); int LIBUSB_CALL libusb_get_config_descriptor(libusb_device *dev, uint8_t config_index, struct libusb_config_descriptor **config); int LIBUSB_CALL libusb_get_config_descriptor_by_value(libusb_device *dev, uint8_t bConfigurationValue, struct libusb_config_descriptor **config); void LIBUSB_CALL libusb_free_config_descriptor( struct libusb_config_descriptor *config); uint8_t LIBUSB_CALL libusb_get_bus_number(libusb_device *dev); uint8_t LIBUSB_CALL libusb_get_device_address(libusb_device *dev); int LIBUSB_CALL libusb_get_max_packet_size(libusb_device *dev, unsigned char endpoint); int LIBUSB_CALL libusb_get_max_iso_packet_size(libusb_device *dev, unsigned char endpoint); int LIBUSB_CALL libusb_open(libusb_device *dev, libusb_device_handle **handle); void LIBUSB_CALL libusb_close(libusb_device_handle *dev_handle); libusb_device * LIBUSB_CALL libusb_get_device(libusb_device_handle *dev_handle); int LIBUSB_CALL libusb_set_configuration(libusb_device_handle *dev, int configuration); int LIBUSB_CALL libusb_claim_interface(libusb_device_handle *dev, int interface_number); int LIBUSB_CALL libusb_release_interface(libusb_device_handle *dev, int interface_number); libusb_device_handle * LIBUSB_CALL libusb_open_device_with_vid_pid( libusb_context *ctx, uint16_t vendor_id, uint16_t product_id); int LIBUSB_CALL libusb_set_interface_alt_setting(libusb_device_handle *dev, int interface_number, int alternate_setting); int LIBUSB_CALL libusb_clear_halt(libusb_device_handle *dev, unsigned char endpoint); int LIBUSB_CALL libusb_reset_device(libusb_device_handle *dev); int LIBUSB_CALL libusb_kernel_driver_active(libusb_device_handle *dev, int interface_number); int LIBUSB_CALL libusb_detach_kernel_driver(libusb_device_handle *dev, int interface_number); int LIBUSB_CALL libusb_attach_kernel_driver(libusb_device_handle *dev, int interface_number); /* async I/O */ /** \ingroup asyncio * Get the data section of a control transfer. This convenience function is here * to remind you that the data does not start until 8 bytes into the actual * buffer, as the setup packet comes first. * * Calling this function only makes sense from a transfer callback function, * or situations where you have already allocated a suitably sized buffer at * transfer->buffer. * * \param transfer a transfer * \returns pointer to the first byte of the data section */ static inline unsigned char *libusb_control_transfer_get_data( struct libusb_transfer *transfer) { return transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE; } /** \ingroup asyncio * Get the control setup packet of a control transfer. This convenience * function is here to remind you that the control setup occupies the first * 8 bytes of the transfer data buffer. * * Calling this function only makes sense from a transfer callback function, * or situations where you have already allocated a suitably sized buffer at * transfer->buffer. * * \param transfer a transfer * \returns a casted pointer to the start of the transfer data buffer */ static inline struct libusb_control_setup *libusb_control_transfer_get_setup( struct libusb_transfer *transfer) { return (struct libusb_control_setup *) transfer->buffer; } /** \ingroup asyncio * Helper function to populate the setup packet (first 8 bytes of the data * buffer) for a control transfer. The wIndex, wValue and wLength values should * be given in host-endian byte order. * * \param buffer buffer to output the setup packet into * \param bmRequestType see the * \ref libusb_control_setup::bmRequestType "bmRequestType" field of * \ref libusb_control_setup * \param bRequest see the * \ref libusb_control_setup::bRequest "bRequest" field of * \ref libusb_control_setup * \param wValue see the * \ref libusb_control_setup::wValue "wValue" field of * \ref libusb_control_setup * \param wIndex see the * \ref libusb_control_setup::wIndex "wIndex" field of * \ref libusb_control_setup * \param wLength see the * \ref libusb_control_setup::wLength "wLength" field of * \ref libusb_control_setup */ static inline void libusb_fill_control_setup(unsigned char *buffer, uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint16_t wLength) { struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer; setup->bmRequestType = bmRequestType; setup->bRequest = bRequest; setup->wValue = libusb_cpu_to_le16(wValue); setup->wIndex = libusb_cpu_to_le16(wIndex); setup->wLength = libusb_cpu_to_le16(wLength); } struct libusb_transfer * LIBUSB_CALL libusb_alloc_transfer(int iso_packets); int LIBUSB_CALL libusb_submit_transfer(struct libusb_transfer *transfer); int LIBUSB_CALL libusb_cancel_transfer(struct libusb_transfer *transfer); void LIBUSB_CALL libusb_free_transfer(struct libusb_transfer *transfer); /** \ingroup asyncio * Helper function to populate the required \ref libusb_transfer fields * for a control transfer. * * If you pass a transfer buffer to this function, the first 8 bytes will * be interpreted as a control setup packet, and the wLength field will be * used to automatically populate the \ref libusb_transfer::length "length" * field of the transfer. Therefore the recommended approach is: * -# Allocate a suitably sized data buffer (including space for control setup) * -# Call libusb_fill_control_setup() * -# If this is a host-to-device transfer with a data stage, put the data * in place after the setup packet * -# Call this function * -# Call libusb_submit_transfer() * * It is also legal to pass a NULL buffer to this function, in which case this * function will not attempt to populate the length field. Remember that you * must then populate the buffer and length fields later. * * \param transfer the transfer to populate * \param dev_handle handle of the device that will handle the transfer * \param buffer data buffer. If provided, this function will interpret the * first 8 bytes as a setup packet and infer the transfer length from that. * \param callback callback function to be invoked on transfer completion * \param user_data user data to pass to callback function * \param timeout timeout for the transfer in milliseconds */ static inline void libusb_fill_control_transfer( struct libusb_transfer *transfer, libusb_device_handle *dev_handle, unsigned char *buffer, libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout) { struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer; transfer->dev_handle = dev_handle; transfer->endpoint = 0; transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL; transfer->timeout = timeout; transfer->buffer = buffer; if (setup) transfer->length = LIBUSB_CONTROL_SETUP_SIZE + libusb_le16_to_cpu(setup->wLength); transfer->user_data = user_data; transfer->callback = callback; } /** \ingroup asyncio * Helper function to populate the required \ref libusb_transfer fields * for a bulk transfer. * * \param transfer the transfer to populate * \param dev_handle handle of the device that will handle the transfer * \param endpoint address of the endpoint where this transfer will be sent * \param buffer data buffer * \param length length of data buffer * \param callback callback function to be invoked on transfer completion * \param user_data user data to pass to callback function * \param timeout timeout for the transfer in milliseconds */ static inline void libusb_fill_bulk_transfer(struct libusb_transfer *transfer, libusb_device_handle *dev_handle, unsigned char endpoint, unsigned char *buffer, int length, libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout) { transfer->dev_handle = dev_handle; transfer->endpoint = endpoint; transfer->type = LIBUSB_TRANSFER_TYPE_BULK; transfer->timeout = timeout; transfer->buffer = buffer; transfer->length = length; transfer->user_data = user_data; transfer->callback = callback; } /** \ingroup asyncio * Helper function to populate the required \ref libusb_transfer fields * for an interrupt transfer. * * \param transfer the transfer to populate * \param dev_handle handle of the device that will handle the transfer * \param endpoint address of the endpoint where this transfer will be sent * \param buffer data buffer * \param length length of data buffer * \param callback callback function to be invoked on transfer completion * \param user_data user data to pass to callback function * \param timeout timeout for the transfer in milliseconds */ static inline void libusb_fill_interrupt_transfer( struct libusb_transfer *transfer, libusb_device_handle *dev_handle, unsigned char endpoint, unsigned char *buffer, int length, libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout) { transfer->dev_handle = dev_handle; transfer->endpoint = endpoint; transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT; transfer->timeout = timeout; transfer->buffer = buffer; transfer->length = length; transfer->user_data = user_data; transfer->callback = callback; } /** \ingroup asyncio * Helper function to populate the required \ref libusb_transfer fields * for an isochronous transfer. * * \param transfer the transfer to populate * \param dev_handle handle of the device that will handle the transfer * \param endpoint address of the endpoint where this transfer will be sent * \param buffer data buffer * \param length length of data buffer * \param num_iso_packets the number of isochronous packets * \param callback callback function to be invoked on transfer completion * \param user_data user data to pass to callback function * \param timeout timeout for the transfer in milliseconds */ static inline void libusb_fill_iso_transfer(struct libusb_transfer *transfer, libusb_device_handle *dev_handle, unsigned char endpoint, unsigned char *buffer, int length, int num_iso_packets, libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout) { transfer->dev_handle = dev_handle; transfer->endpoint = endpoint; transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS; transfer->timeout = timeout; transfer->buffer = buffer; transfer->length = length; transfer->num_iso_packets = num_iso_packets; transfer->user_data = user_data; transfer->callback = callback; } /** \ingroup asyncio * Convenience function to set the length of all packets in an isochronous * transfer, based on the num_iso_packets field in the transfer structure. * * \param transfer a transfer * \param length the length to set in each isochronous packet descriptor * \see libusb_get_max_packet_size() */ static inline void libusb_set_iso_packet_lengths( struct libusb_transfer *transfer, unsigned int length) { int i; for (i = 0; i < transfer->num_iso_packets; i++) transfer->iso_packet_desc[i].length = length; } /** \ingroup asyncio * Convenience function to locate the position of an isochronous packet * within the buffer of an isochronous transfer. * * This is a thorough function which loops through all preceding packets, * accumulating their lengths to find the position of the specified packet. * Typically you will assign equal lengths to each packet in the transfer, * and hence the above method is sub-optimal. You may wish to use * libusb_get_iso_packet_buffer_simple() instead. * * \param transfer a transfer * \param packet the packet to return the address of * \returns the base address of the packet buffer inside the transfer buffer, * or NULL if the packet does not exist. * \see libusb_get_iso_packet_buffer_simple() */ static inline unsigned char *libusb_get_iso_packet_buffer( struct libusb_transfer *transfer, unsigned int packet) { int i; size_t offset = 0; int _packet; /* oops..slight bug in the API. packet is an unsigned int, but we use * signed integers almost everywhere else. range-check and convert to * signed to avoid compiler warnings. FIXME for libusb-2. */ if (packet > INT_MAX) return NULL; _packet = packet; if (_packet >= transfer->num_iso_packets) return NULL; for (i = 0; i < _packet; i++) offset += transfer->iso_packet_desc[i].length; return transfer->buffer + offset; } /** \ingroup asyncio * Convenience function to locate the position of an isochronous packet * within the buffer of an isochronous transfer, for transfers where each * packet is of identical size. * * This function relies on the assumption that every packet within the transfer * is of identical size to the first packet. Calculating the location of * the packet buffer is then just a simple calculation: * buffer + (packet_size * packet) * * Do not use this function on transfers other than those that have identical * packet lengths for each packet. * * \param transfer a transfer * \param packet the packet to return the address of * \returns the base address of the packet buffer inside the transfer buffer, * or NULL if the packet does not exist. * \see libusb_get_iso_packet_buffer() */ static inline unsigned char *libusb_get_iso_packet_buffer_simple( struct libusb_transfer *transfer, unsigned int packet) { int _packet; /* oops..slight bug in the API. packet is an unsigned int, but we use * signed integers almost everywhere else. range-check and convert to * signed to avoid compiler warnings. FIXME for libusb-2. */ if (packet > INT_MAX) return NULL; _packet = packet; if (_packet >= transfer->num_iso_packets) return NULL; return transfer->buffer + (transfer->iso_packet_desc[0].length * _packet); } /* sync I/O */ int LIBUSB_CALL libusb_control_transfer(libusb_device_handle *dev_handle, uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, unsigned char *data, uint16_t wLength, unsigned int timeout); int LIBUSB_CALL libusb_bulk_transfer(libusb_device_handle *dev_handle, unsigned char endpoint, unsigned char *data, int length, int *actual_length, unsigned int timeout); int LIBUSB_CALL libusb_interrupt_transfer(libusb_device_handle *dev_handle, unsigned char endpoint, unsigned char *data, int length, int *actual_length, unsigned int timeout); /** \ingroup desc * Retrieve a descriptor from the default control pipe. * This is a convenience function which formulates the appropriate control * message to retrieve the descriptor. * * \param dev a device handle * \param desc_type the descriptor type, see \ref libusb_descriptor_type * \param desc_index the index of the descriptor to retrieve * \param data output buffer for descriptor * \param length size of data buffer * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure */ static inline int libusb_get_descriptor(libusb_device_handle *dev, uint8_t desc_type, uint8_t desc_index, unsigned char *data, int length) { return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN, LIBUSB_REQUEST_GET_DESCRIPTOR, (desc_type << 8) | desc_index, 0, data, (uint16_t) length, 1000); } /** \ingroup desc * Retrieve a descriptor from a device. * This is a convenience function which formulates the appropriate control * message to retrieve the descriptor. The string returned is Unicode, as * detailed in the USB specifications. * * \param dev a device handle * \param desc_index the index of the descriptor to retrieve * \param langid the language ID for the string descriptor * \param data output buffer for descriptor * \param length size of data buffer * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure * \see libusb_get_string_descriptor_ascii() */ static inline int libusb_get_string_descriptor(libusb_device_handle *dev, uint8_t desc_index, uint16_t langid, unsigned char *data, int length) { return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN, LIBUSB_REQUEST_GET_DESCRIPTOR, (LIBUSB_DT_STRING << 8) | desc_index, langid, data, (uint16_t) length, 1000); } int LIBUSB_CALL libusb_get_string_descriptor_ascii(libusb_device_handle *dev, uint8_t desc_index, unsigned char *data, int length); /* polling and timeouts */ int LIBUSB_CALL libusb_try_lock_events(libusb_context *ctx); void LIBUSB_CALL libusb_lock_events(libusb_context *ctx); void LIBUSB_CALL libusb_unlock_events(libusb_context *ctx); int LIBUSB_CALL libusb_event_handling_ok(libusb_context *ctx); int LIBUSB_CALL libusb_event_handler_active(libusb_context *ctx); void LIBUSB_CALL libusb_lock_event_waiters(libusb_context *ctx); void LIBUSB_CALL libusb_unlock_event_waiters(libusb_context *ctx); int LIBUSB_CALL libusb_wait_for_event(libusb_context *ctx, struct timeval *tv); int LIBUSB_CALL libusb_handle_events_timeout(libusb_context *ctx, struct timeval *tv); int LIBUSB_CALL libusb_handle_events(libusb_context *ctx); int LIBUSB_CALL libusb_handle_events_locked(libusb_context *ctx, struct timeval *tv); int LIBUSB_CALL libusb_pollfds_handle_timeouts(libusb_context *ctx); int LIBUSB_CALL libusb_get_next_timeout(libusb_context *ctx, struct timeval *tv); /** \ingroup poll * File descriptor for polling */ struct libusb_pollfd { /** Numeric file descriptor */ int fd; /** Event flags to poll for from . POLLIN indicates that you * should monitor this file descriptor for becoming ready to read from, * and POLLOUT indicates that you should monitor this file descriptor for * nonblocking write readiness. */ short events; }; /** \ingroup poll * Callback function, invoked when a new file descriptor should be added * to the set of file descriptors monitored for events. * \param fd the new file descriptor * \param events events to monitor for, see \ref libusb_pollfd for a * description * \param user_data User data pointer specified in * libusb_set_pollfd_notifiers() call * \see libusb_set_pollfd_notifiers() */ typedef void (LIBUSB_CALL *libusb_pollfd_added_cb)(int fd, short events, void *user_data); /** \ingroup poll * Callback function, invoked when a file descriptor should be removed from * the set of file descriptors being monitored for events. After returning * from this callback, do not use that file descriptor again. * \param fd the file descriptor to stop monitoring * \param user_data User data pointer specified in * libusb_set_pollfd_notifiers() call * \see libusb_set_pollfd_notifiers() */ typedef void (LIBUSB_CALL *libusb_pollfd_removed_cb)(int fd, void *user_data); const struct libusb_pollfd ** LIBUSB_CALL libusb_get_pollfds( libusb_context *ctx); void LIBUSB_CALL libusb_set_pollfd_notifiers(libusb_context *ctx, libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb, void *user_data); #ifdef __cplusplus } #endif #endif ================================================ FILE: libs/libusb-win32/AUTHORS.txt ================================================ Library, Test Programs: Stephan Meyer, Johannes Erdfelt, Thomas Sailer, Drivers, Installer: Stephan Meyer, Travis Robinson, Testing, Technical support: Xiaofan Chen, ================================================ FILE: libs/libusb-win32/COPYING_LGPL.txt ================================================ GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below. 0. Additional Definitions. As used herein, "this License" refers to version 3 of the GNU Lesser General Public License, and the "GNU GPL" refers to version 3 of the GNU General Public License. "The Library" refers to a covered work governed by this License, other than an Application or a Combined Work as defined below. An "Application" is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library. A "Combined Work" is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the "Linked Version". The "Minimal Corresponding Source" for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version. The "Corresponding Application Code" for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work. 1. Exception to Section 3 of the GNU GPL. You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL. 2. Conveying Modified Versions. If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version: a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy. 3. Object Code Incorporating Material from Library Header Files. The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the object code with a copy of the GNU GPL and this license document. 4. Combined Works. You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following: a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the Combined Work with a copy of the GNU GPL and this license document. c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document. d) Do one of the following: 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source. 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version. e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.) 5. Combined Libraries. You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License. b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 6. Revised Versions of the GNU Lesser General Public License. The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library. ================================================ FILE: libs/libusb-win32/README.txt ================================================ This is libusb-win32 (http://libusb-win32.sourceforge.net) version 1.2.6.0. Libusb-win32 is a library that allows userspace application to access USB devices on Windows operation systems (Win2k, WinXP, Vista, Win7). It is derived from and fully API compatible to libusb available at http://libusb.sourceforge.net. For more information visit the project's web site at: http://libusb-win32.sourceforge.net http://sourceforge.net/projects/libusb-win32 ================================================ FILE: libs/libusb-win32/include/lusb0_usb.h ================================================ #ifndef __USB_H__ #define __USB_H__ #include #include /* * 'interface' is defined somewhere in the Windows header files. This macro * is deleted here to avoid conflicts and compile errors. */ #ifdef interface #undef interface #endif /* * PATH_MAX from limits.h can't be used on Windows if the dll and * import libraries are build/used by different compilers */ #define LIBUSB_PATH_MAX 512 /* * USB spec information * * This is all stuff grabbed from various USB specs and is pretty much * not subject to change */ /* * Device and/or Interface Class codes */ #define USB_CLASS_PER_INTERFACE 0 /* for DeviceClass */ #define USB_CLASS_AUDIO 1 #define USB_CLASS_COMM 2 #define USB_CLASS_HID 3 #define USB_CLASS_PRINTER 7 #define USB_CLASS_MASS_STORAGE 8 #define USB_CLASS_HUB 9 #define USB_CLASS_DATA 10 #define USB_CLASS_VENDOR_SPEC 0xff /* * Descriptor types */ #define USB_DT_DEVICE 0x01 #define USB_DT_CONFIG 0x02 #define USB_DT_STRING 0x03 #define USB_DT_INTERFACE 0x04 #define USB_DT_ENDPOINT 0x05 #define USB_DT_HID 0x21 #define USB_DT_REPORT 0x22 #define USB_DT_PHYSICAL 0x23 #define USB_DT_HUB 0x29 /* * Descriptor sizes per descriptor type */ #define USB_DT_DEVICE_SIZE 18 #define USB_DT_CONFIG_SIZE 9 #define USB_DT_INTERFACE_SIZE 9 #define USB_DT_ENDPOINT_SIZE 7 #define USB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */ #define USB_DT_HUB_NONVAR_SIZE 7 /* ensure byte-packed structures */ #include /* All standard descriptors have these 2 fields in common */ struct usb_descriptor_header { unsigned char bLength; unsigned char bDescriptorType; }; /* String descriptor */ struct usb_string_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned short wData[1]; }; /* HID descriptor */ struct usb_hid_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned short bcdHID; unsigned char bCountryCode; unsigned char bNumDescriptors; }; /* Endpoint descriptor */ #define USB_MAXENDPOINTS 32 struct usb_endpoint_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned char bEndpointAddress; unsigned char bmAttributes; unsigned short wMaxPacketSize; unsigned char bInterval; unsigned char bRefresh; unsigned char bSynchAddress; unsigned char *extra; /* Extra descriptors */ int extralen; }; #define USB_ENDPOINT_ADDRESS_MASK 0x0f /* in bEndpointAddress */ #define USB_ENDPOINT_DIR_MASK 0x80 #define USB_ENDPOINT_TYPE_MASK 0x03 /* in bmAttributes */ #define USB_ENDPOINT_TYPE_CONTROL 0 #define USB_ENDPOINT_TYPE_ISOCHRONOUS 1 #define USB_ENDPOINT_TYPE_BULK 2 #define USB_ENDPOINT_TYPE_INTERRUPT 3 /* Interface descriptor */ #define USB_MAXINTERFACES 32 struct usb_interface_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned char bInterfaceNumber; unsigned char bAlternateSetting; unsigned char bNumEndpoints; unsigned char bInterfaceClass; unsigned char bInterfaceSubClass; unsigned char bInterfaceProtocol; unsigned char iInterface; struct usb_endpoint_descriptor *endpoint; unsigned char *extra; /* Extra descriptors */ int extralen; }; #define USB_MAXALTSETTING 128 /* Hard limit */ struct usb_interface { struct usb_interface_descriptor *altsetting; int num_altsetting; }; /* Configuration descriptor information.. */ #define USB_MAXCONFIG 8 struct usb_config_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned short wTotalLength; unsigned char bNumInterfaces; unsigned char bConfigurationValue; unsigned char iConfiguration; unsigned char bmAttributes; unsigned char MaxPower; struct usb_interface *interface; unsigned char *extra; /* Extra descriptors */ int extralen; }; /* Device descriptor */ struct usb_device_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned short bcdUSB; unsigned char bDeviceClass; unsigned char bDeviceSubClass; unsigned char bDeviceProtocol; unsigned char bMaxPacketSize0; unsigned short idVendor; unsigned short idProduct; unsigned short bcdDevice; unsigned char iManufacturer; unsigned char iProduct; unsigned char iSerialNumber; unsigned char bNumConfigurations; }; struct usb_ctrl_setup { unsigned char bRequestType; unsigned char bRequest; unsigned short wValue; unsigned short wIndex; unsigned short wLength; }; /* * Standard requests */ #define USB_REQ_GET_STATUS 0x00 #define USB_REQ_CLEAR_FEATURE 0x01 /* 0x02 is reserved */ #define USB_REQ_SET_FEATURE 0x03 /* 0x04 is reserved */ #define USB_REQ_SET_ADDRESS 0x05 #define USB_REQ_GET_DESCRIPTOR 0x06 #define USB_REQ_SET_DESCRIPTOR 0x07 #define USB_REQ_GET_CONFIGURATION 0x08 #define USB_REQ_SET_CONFIGURATION 0x09 #define USB_REQ_GET_INTERFACE 0x0A #define USB_REQ_SET_INTERFACE 0x0B #define USB_REQ_SYNCH_FRAME 0x0C #define USB_TYPE_STANDARD (0x00 << 5) #define USB_TYPE_CLASS (0x01 << 5) #define USB_TYPE_VENDOR (0x02 << 5) #define USB_TYPE_RESERVED (0x03 << 5) #define USB_RECIP_DEVICE 0x00 #define USB_RECIP_INTERFACE 0x01 #define USB_RECIP_ENDPOINT 0x02 #define USB_RECIP_OTHER 0x03 /* * Various libusb API related stuff */ #define USB_ENDPOINT_IN 0x80 #define USB_ENDPOINT_OUT 0x00 /* Error codes */ #define USB_ERROR_BEGIN 500000 /* * This is supposed to look weird. This file is generated from autoconf * and I didn't want to make this too complicated. */ #define USB_LE16_TO_CPU(x) /* * Device reset types for usb_reset_ex. * http://msdn.microsoft.com/en-us/library/ff537269%28VS.85%29.aspx * http://msdn.microsoft.com/en-us/library/ff537243%28v=vs.85%29.aspx */ #define USB_RESET_TYPE_RESET_PORT (1 << 0) #define USB_RESET_TYPE_CYCLE_PORT (1 << 1) #define USB_RESET_TYPE_FULL_RESET (USB_RESET_TYPE_CYCLE_PORT | USB_RESET_TYPE_RESET_PORT) /* Data types */ /* struct usb_device; */ /* struct usb_bus; */ struct usb_device { struct usb_device *next, *prev; char filename[LIBUSB_PATH_MAX]; struct usb_bus *bus; struct usb_device_descriptor descriptor; struct usb_config_descriptor *config; void *dev; /* Darwin support */ unsigned char devnum; unsigned char num_children; struct usb_device **children; }; struct usb_bus { struct usb_bus *next, *prev; char dirname[LIBUSB_PATH_MAX]; struct usb_device *devices; unsigned long location; struct usb_device *root_dev; }; /* Version information, Windows specific */ struct usb_version { struct { int major; int minor; int micro; int nano; } dll; struct { int major; int minor; int micro; int nano; } driver; }; struct usb_dev_handle; typedef struct usb_dev_handle usb_dev_handle; /* Variables */ #ifndef __USB_C__ #define usb_busses usb_get_busses() #endif #include #ifdef __cplusplus extern "C" { #endif /* Function prototypes */ /* usb.c */ usb_dev_handle *usb_open(struct usb_device *dev); int usb_close(usb_dev_handle *dev); int usb_get_string(usb_dev_handle *dev, int index, int langid, char *buf, size_t buflen); int usb_get_string_simple(usb_dev_handle *dev, int index, char *buf, size_t buflen); /* descriptors.c */ int usb_get_descriptor_by_endpoint(usb_dev_handle *udev, int ep, unsigned char type, unsigned char index, void *buf, int size); int usb_get_descriptor(usb_dev_handle *udev, unsigned char type, unsigned char index, void *buf, int size); /* .c */ int usb_bulk_write(usb_dev_handle *dev, int ep, char *bytes, int size, int timeout); int usb_bulk_read(usb_dev_handle *dev, int ep, char *bytes, int size, int timeout); int usb_interrupt_write(usb_dev_handle *dev, int ep, char *bytes, int size, int timeout); int usb_interrupt_read(usb_dev_handle *dev, int ep, char *bytes, int size, int timeout); int usb_control_msg(usb_dev_handle *dev, int requesttype, int request, int value, int index, char *bytes, int size, int timeout); int usb_set_configuration(usb_dev_handle *dev, int configuration); int usb_claim_interface(usb_dev_handle *dev, int interface); int usb_release_interface(usb_dev_handle *dev, int interface); int usb_set_altinterface(usb_dev_handle *dev, int alternate); int usb_resetep(usb_dev_handle *dev, unsigned int ep); int usb_clear_halt(usb_dev_handle *dev, unsigned int ep); int usb_reset(usb_dev_handle *dev); int usb_reset_ex(usb_dev_handle *dev, unsigned int reset_type); char *usb_strerror(void); void usb_init(void); void usb_set_debug(int level); int usb_find_busses(void); int usb_find_devices(void); struct usb_device *usb_device(usb_dev_handle *dev); struct usb_bus *usb_get_busses(void); /* Windows specific functions */ #define LIBUSB_HAS_INSTALL_SERVICE_NP 1 int usb_install_service_np(void); void CALLBACK usb_install_service_np_rundll(HWND wnd, HINSTANCE instance, LPSTR cmd_line, int cmd_show); #define LIBUSB_HAS_UNINSTALL_SERVICE_NP 1 int usb_uninstall_service_np(void); void CALLBACK usb_uninstall_service_np_rundll(HWND wnd, HINSTANCE instance, LPSTR cmd_line, int cmd_show); #define LIBUSB_HAS_INSTALL_DRIVER_NP 1 int usb_install_driver_np(const char *inf_file); void CALLBACK usb_install_driver_np_rundll(HWND wnd, HINSTANCE instance, LPSTR cmd_line, int cmd_show); #define LIBUSB_HAS_TOUCH_INF_FILE_NP 1 int usb_touch_inf_file_np(const char *inf_file); void CALLBACK usb_touch_inf_file_np_rundll(HWND wnd, HINSTANCE instance, LPSTR cmd_line, int cmd_show); #define LIBUSB_HAS_INSTALL_NEEDS_RESTART_NP 1 int usb_install_needs_restart_np(void); #define LIBUSB_HAS_INSTALL_NP 1 int usb_install_npW(HWND hwnd, HINSTANCE instance, LPCWSTR cmd_line, int starg_arg); int usb_install_npA(HWND hwnd, HINSTANCE instance, LPCSTR cmd_line, int starg_arg); #define usb_install_np usb_install_npA void CALLBACK usb_install_np_rundll(HWND wnd, HINSTANCE instance, LPSTR cmd_line, int cmd_show); const struct usb_version *usb_get_version(void); int usb_isochronous_setup_async(usb_dev_handle *dev, void **context, unsigned char ep, int pktsize); int usb_bulk_setup_async(usb_dev_handle *dev, void **context, unsigned char ep); int usb_interrupt_setup_async(usb_dev_handle *dev, void **context, unsigned char ep); int usb_submit_async(void *context, char *bytes, int size); int usb_reap_async(void *context, int timeout); int usb_reap_async_nocancel(void *context, int timeout); int usb_cancel_async(void *context); int usb_free_async(void **context); #ifdef __cplusplus } #endif #endif /* __USB_H__ */ ================================================ FILE: scripts/update_freenect.sh ================================================ #! /bin/bash WD=$(dirname $0) DEST=../libs/libfreenect SRC=libfreenect ### cd $WD # get latest source #git clone git://github.com/OpenKinect/libfreenect.git git clone git://github.com/wizgrav/libfreenect.git # remove uneeded makefiles rm $SRC/src/CMakeLists.txt rm $SRC/src/libfreenect.pc.in # we dont need freenect audio rm $SRC/include/libfreenect-audio.h rm $SRC/src/audio.c rm $SRC/src/loader.c # no python either rm $SRC/src/fwfetcher.py # make folders mkdir -p $DEST/include mkdir -p $DEST/src mkdir -p $DEST/platform # copy licenses, etc cp -v $SRC/APACHE20 $DEST cp -v $SRC/CONTRIB $DEST cp -v $SRC/GPL2 $DEST # copy headers cp -Rv $SRC/include/* $DEST/include # copy sources cp -Rv $SRC/src/* $DEST/src # copy platform specfic stuff cp -Rv $SRC/platform/linux $DEST/platform cp -Rv $SRC/platform/windows $DEST/platform # cleanup rm -rf $SRC ================================================ FILE: scripts/update_libusb-win32.sh ================================================ #! /bin/bash WD=$(dirname $0) VER=1.2.6.0 DEST=../libs/libusb-win32 SRC=libusb-win32-bin-$VER ### cd $WD # make folders mkdir -p $DEST/include mkdir -p $DEST/lib/vs2010 mkdir -p $DEST/lib/win_cb ### Debug # get source curl -LO http://downloads.sourceforge.net/project/libusb-win32/libusb-win32-releases/$VER/libusb-win32-bin-debug-$VER.zip unzip -oq libusb-win32-bin-debug-$VER.zip # copy libs cp -v $SRC/lib/msvc/libusb.lib $DEST/lib/vs2010/libusbd.lib cp -v $SRC/lib/msvc/libusb.lib $DEST/lib/win_cb/libusbd.lib # cleanup rm -rf libusb-win32-bin-debug-$VER rm libusb-win32-bin-debug-$VER.zip ### Release # get source curl -LO http://downloads.sourceforge.net/project/libusb-win32/libusb-win32-releases/$VER/$SRC.zip unzip -oq $SRC.zip # copy licenses, etc cp -v $SRC/AUTHORS.txt $DEST cp -v $SRC/COPYING_LGPL.txt $DEST cp -v $SRC/README.txt $DEST # copy headers cp -Rv $SRC/include/* $DEST/include # copy libs cp -v $SRC/lib/msvc/libusb.lib $DEST/lib/vs2010 cp -v $SRC/lib/msvc/libusb.lib $DEST/lib/win_cb # cleanup rm -rf $SRC rm $SRC.zip ================================================ FILE: src/ofxBase3DVideo.h ================================================ /*============================================================================== Copyright (c) 2010, 2011 ofxKinect Team Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ==============================================================================*/ #pragma once /// \class ofxBase3DVideo /// /// a base class for 3D video devices class ofxBase3DVideo: public ofBaseVideo { public: /// get the pixels of the most recent depth frame virtual unsigned char* getDepthPixels()=0; /// get the distance in millimeters to a given point as a float array virtual float* getDistancePixels()=0; /// get the grayscale depth texture virtual ofTexture& getDepthTextureReference()=0; }; ================================================ FILE: src/ofxKinect.cpp ================================================ /*============================================================================== Copyright (c) 2010, 2011 ofxKinect Team Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ---------------------------------------------------------------------------- This project uses libfreenect, copyrighted by the Open Kinect Project using the Apache License v2. See the file "APACHE20" in libs/libfreenect. See http://www.openkinect.org & https://github.com/OpenKinect/libfreenect for documentation ==============================================================================*/ #include "ofxKinect.h" #include "ofMain.h" #include "libfreenect-registration.h" #include "freenect_internal.h" // for access to freenect_device.registration.zero_plane_info #define OFX_KINECT_GRAVITY 9.80665 // context static ofxKinectContext ofxKinect::kinectContext; //-------------------------------------------------------------------- ofxKinect::ofxKinect() { ofLogVerbose("ofxKinect") <<" creating ofxKinect"; deviceId = -1; serial = ""; bUseTexture = true; bGrabVideo = true; // set defaults bGrabberInited = false; bNeedsUpdateVideo = false; bNeedsUpdateDepth = false; bIsFrameNewVideo = false; bIsFrameNewDepth = false; bIsVideoInfrared = false; videoBytesPerPixel = 3; kinectDevice = NULL; targetTiltAngleDeg = 0; currentTiltAngleDeg = 0; bTiltNeedsApplying = false; currentLed = -1; bLedNeedsApplying = false; bHasMotorControl = false; lastDeviceId = -1; tryCount = 0; timeSinceOpen = 0; bGotData = false; bUseRegistration = false; bNearWhite = true; setDepthClipping(); } //-------------------------------------------------------------------- ofxKinect::~ofxKinect() { close(); clear(); } //-------------------------------------------------------------------- bool ofxKinect::init(bool infrared, bool video, bool texture) { if(isConnected()) { ofLogWarning("ofxKinect") << "init(): do not call init while ofxKinect is running!"; return false; } clear(); bIsVideoInfrared = infrared; bGrabVideo = video; videoBytesPerPixel = infrared?1:3; bUseTexture = texture; // allocate depthPixelsRaw.allocate(width, height, 1); depthPixelsRawBack.allocate(width, height, 1); depthPixelsRawIntra.allocate(width, height, 1); videoPixels.allocate(width, height, videoBytesPerPixel); videoPixelsBack.allocate(width, height, videoBytesPerPixel); videoPixelsIntra.allocate(width, height, videoBytesPerPixel); depthPixels.allocate(width, height, 1); distancePixels.allocate(width, height, 1); // set depthPixelsRaw.set(0); depthPixelsRawBack.set(0); videoPixels.set(0); videoPixelsBack.set(0); depthPixels.set(0); distancePixels.set(0); if(bUseTexture) { depthTex.allocate(width, height, GL_LUMINANCE); videoTex.allocate(width, height, infrared ? GL_LUMINANCE : GL_RGB); } if(!kinectContext.isInited()) { if(!kinectContext.init()) { return false; } } bGrabberInited = true; return bGrabberInited; } //--------------------------------------------------------------------------- void ofxKinect::clear() { if(isConnected()) { ofLogWarning("ofxKinect") << "clear(): do not call clear while ofxKinect is running!"; return; } depthPixelsRaw.clear(); depthPixelsRawBack.clear(); videoPixels.clear(); videoPixelsBack.clear(); depthPixels.clear(); distancePixels.clear(); depthTex.clear(); videoTex.clear(); bGrabberInited = false; } //-------------------------------------------------------------------- void ofxKinect::setRegistration(bool bUseRegistration) { this->bUseRegistration = bUseRegistration; } //-------------------------------------------------------------------- bool ofxKinect::open(int id) { if(!bGrabberInited) { ofLogWarning("ofxKinect") << "open(): cannot open, init not called"; return false; } if(!kinectContext.open(*this, id)) { return false; } if(serial == "0000000000000000") { ofLogVerbose("ofxKinect") << "open(): device " << deviceId << " does not have motor control"; bHasMotorControl = false; } else { bHasMotorControl = true; } lastDeviceId = deviceId; timeSinceOpen = ofGetElapsedTimef(); bGotData = false; freenect_set_user(kinectDevice, this); freenect_set_depth_buffer(kinectDevice, depthPixelsRawBack.getPixels()); freenect_set_video_buffer(kinectDevice, videoPixelsBack.getPixels()); freenect_set_depth_callback(kinectDevice, &grabDepthFrame); freenect_set_video_callback(kinectDevice, &grabVideoFrame); startThread(true, false); // blocking, not verbose return true; } //-------------------------------------------------------------------- bool ofxKinect::open(string serial) { if(!bGrabberInited) { ofLogVerbose("ofxKinect") << "open(): cannot open, init not called"; return false; } if(!kinectContext.open(*this, serial)) { return false; } if(serial == "0000000000000000") { ofLogVerbose("ofxKinect") << "open(): device " << deviceId << " does not have motor control"; bHasMotorControl = false; } else { bHasMotorControl = true; } lastDeviceId = deviceId; timeSinceOpen = ofGetElapsedTimef(); bGotData = false; freenect_set_user(kinectDevice, this); freenect_set_depth_callback(kinectDevice, &grabDepthFrame); freenect_set_video_callback(kinectDevice, &grabVideoFrame); startThread(true, false); // blocking, not verbose return true; } //--------------------------------------------------------------------------- void ofxKinect::close() { if(isThreadRunning()) { stopThread(); ofSleepMillis(10); waitForThread(false); } deviceId = -1; serial = ""; bIsFrameNewVideo = false; bNeedsUpdateVideo = false; bIsFrameNewDepth = false; bNeedsUpdateDepth = false; } //--------------------------------------------------------------------------- bool ofxKinect::isConnected() { return isThreadRunning(); } //-------------------------------------------------------------------- bool ofxKinect::isFrameNew() { return isFrameNewVideo() || isFrameNewDepth(); } //-------------------------------------------------------------------- bool ofxKinect::isFrameNewVideo(){ return bIsFrameNewVideo; } //-------------------------------------------------------------------- bool ofxKinect::isFrameNewDepth(){ return bIsFrameNewDepth; } //---------------------------------------------------------- void ofxKinect::update() { if(!bGrabberInited) { return; } if(!bNeedsUpdateVideo && !bNeedsUpdateDepth && !bGotData && tryCount < 5 && ofGetElapsedTimef() - timeSinceOpen > 2.0 ){ close(); ofLogWarning("ofxKinect") << "update(): device " << lastDeviceId << " isn't delivering data, reconnecting tries: " << tryCount+1; kinectContext.buildDeviceList(); open(lastDeviceId); tryCount++; timeSinceOpen = ofGetElapsedTimef(); return; } if(bNeedsUpdateVideo){ bIsFrameNewVideo = true; bGotData = true; tryCount = 0; if(this->lock()) { swap(videoPixels,videoPixelsIntra); bNeedsUpdateVideo = false; this->unlock(); } if(bUseTexture) { videoTex.loadData(videoPixels.getPixels(), width, height, bIsVideoInfrared?GL_LUMINANCE:GL_RGB); } } else { bIsFrameNewVideo = false; } if(bNeedsUpdateDepth){ bIsFrameNewDepth = true; bGotData = true; tryCount = 0; if(this->lock()) { swap(depthPixelsRaw, depthPixelsRawIntra); bNeedsUpdateDepth = false; this->unlock(); updateDepthPixels(); } if(bUseTexture) { depthTex.loadData(depthPixels.getPixels(), width, height, GL_LUMINANCE); } } else { bIsFrameNewDepth = false; } } //------------------------------------ float ofxKinect::getDistanceAt(int x, int y) { return depthPixelsRaw[y * width + x]; } //------------------------------------ float ofxKinect::getDistanceAt(const ofPoint & p) { return getDistanceAt(p.x, p.y); } //------------------------------------ ofVec3f ofxKinect::getWorldCoordinateAt(int x, int y) { return getWorldCoordinateAt(x, y, getDistanceAt(x, y)); } //------------------------------------ ofVec3f ofxKinect::getWorldCoordinateAt(float cx, float cy, float wz) { double wx, wy; freenect_camera_to_world(kinectDevice, cx, cy, wz, &wx, &wy); return ofVec3f(wx, wy, wz); } //------------------------------------ float ofxKinect::getSensorEmitterDistance() { return kinectDevice->registration.zero_plane_info.dcmos_emitter_dist; } //------------------------------------ float ofxKinect::getSensorCameraDistance() { return kinectDevice->registration.zero_plane_info.dcmos_rcmos_dist; } //------------------------------------ float ofxKinect::getZeroPlanePixelSize() { return kinectDevice->registration.zero_plane_info.reference_pixel_size; } //------------------------------------ float ofxKinect::getZeroPlaneDistance() { return kinectDevice->registration.zero_plane_info.reference_distance; } //------------------------------------ ofColor ofxKinect::getColorAt(int x, int y) { int index = (y * width + x) * videoBytesPerPixel; ofColor c; c.r = videoPixels[index + 0]; c.g = videoPixels[index + (videoBytesPerPixel-1)/2]; c.b = videoPixels[index + (videoBytesPerPixel-1)]; c.a = 255; return c; } //------------------------------------ ofColor ofxKinect::getColorAt(const ofPoint & p) { return getColorAt(p.x, p.y); } //--------------------------------------------------------------------------- unsigned char * ofxKinect::getPixels() { return videoPixels.getPixels(); } //--------------------------------------------------------------------------- unsigned char * ofxKinect::getDepthPixels() { return depthPixels.getPixels(); } //--------------------------------------------------------------------------- unsigned short * ofxKinect::getRawDepthPixels() { return depthPixelsRaw.getPixels(); } //--------------------------------------------------------------------------- float* ofxKinect::getDistancePixels() { return distancePixels.getPixels(); } ofPixels & ofxKinect::getPixelsRef(){ return videoPixels; } ofPixels & ofxKinect::getDepthPixelsRef(){ return depthPixels; } ofShortPixels & ofxKinect::getRawDepthPixelsRef(){ return depthPixelsRaw; } ofFloatPixels & ofxKinect::getDistancePixelsRef(){ return distancePixels; } //------------------------------------ ofTexture& ofxKinect::getTextureReference(){ if(!videoTex.bAllocated()){ ofLogWarning("ofxKinect") << "getTextureReference(): device " << deviceId << " video texture not allocated"; } return videoTex; } //--------------------------------------------------------------------------- ofTexture& ofxKinect::getDepthTextureReference(){ if(!depthTex.bAllocated()){ ofLogWarning("ofxKinect") << "getDepthTextureReference(): device " << deviceId << " depth texture not allocated"; } return depthTex; } //--------------------------------------------------------------------------- void ofxKinect::enableDepthNearValueWhite(bool bEnabled) { bNearWhite = bEnabled; updateDepthLookupTable(); } //--------------------------------------------------------------------------- bool ofxKinect::isDepthNearValueWhite() { return bNearWhite; } //--------------------------------------------------------------------------- void ofxKinect::setDepthClipping(float nearClip, float farClip) { nearClipping = nearClip; farClipping = farClip; updateDepthLookupTable(); } //--------------------------------------------------------------------------- float ofxKinect::getNearClipping() { return nearClipping; } //--------------------------------------------------------------------------- float ofxKinect::getFarClipping() { return farClipping; } //-------------------------------------------------------------------- bool ofxKinect::hasAccelControl() { return bHasMotorControl; // depends on motor for now } bool ofxKinect::hasCamTiltControl() { return bHasMotorControl; // depends on motor for now } bool ofxKinect::hasLedControl() { return bHasMotorControl; // depends on motor for now } //--------------------------------------------------------------------------- ofPoint ofxKinect::getRawAccel() { return rawAccel; } //--------------------------------------------------------------------------- ofPoint ofxKinect::getMksAccel() { return mksAccel; } //--------------------------------------------------------------------------- float ofxKinect::getAccelPitch(){ return ofRadToDeg(asin(getMksAccel().z/OFX_KINECT_GRAVITY)); } //--------------------------------------------------------------------------- float ofxKinect::getAccelRoll(){ return ofRadToDeg(asin(getMksAccel().x/OFX_KINECT_GRAVITY)); } // we update the value here, but apply it in kinect thread //-------------------------------------------------------------------- bool ofxKinect::setCameraTiltAngle(float angleInDegrees) { if(!hasCamTiltControl() || !bGrabberInited) { return false; } targetTiltAngleDeg = ofClamp(angleInDegrees, -30, 30); bTiltNeedsApplying = true; return true; } //-------------------------------------------------------------------- float ofxKinect::getTargetCameraTiltAngle() { return targetTiltAngleDeg; } float ofxKinect::getCurrentCameraTiltAngle() { return currentTiltAngleDeg; } //-------------------------------------------------------------------- void ofxKinect::setLed(ofxKinect::LedMode mode) { if(!hasLedControl() || mode == currentLed) { return; } bLedNeedsApplying = true; currentLed = mode; } //------------------------------------ void ofxKinect::setUseTexture(bool bUse){ bUseTexture = bUse; } //---------------------------------------------------------- void ofxKinect::draw(float _x, float _y, float _w, float _h) { if(bUseTexture && bGrabVideo) { videoTex.draw(_x, _y, _w, _h); } } //---------------------------------------------------------- void ofxKinect::draw(float _x, float _y) { draw(_x, _y, (float)width, (float)height); } //---------------------------------------------------------- void ofxKinect::draw(const ofPoint & point) { draw(point.x, point.y); } //---------------------------------------------------------- void ofxKinect::draw(const ofRectangle & rect) { draw(rect.x, rect.y, rect.width, rect.height); } //---------------------------------------------------------- void ofxKinect::drawDepth(float _x, float _y, float _w, float _h) { if(bUseTexture) { depthTex.draw(_x, _y, _w, _h); } } //--------------------------------------------------------------------------- void ofxKinect::drawDepth(float _x, float _y) { drawDepth(_x, _y, (float)width, (float)height); } //---------------------------------------------------------- void ofxKinect::drawDepth(const ofPoint & point) { drawDepth(point.x, point.y); } //---------------------------------------------------------- void ofxKinect::drawDepth(const ofRectangle & rect) { drawDepth(rect.x, rect.y, rect.width, rect.height); } //--------------------------------------------------------------------------- int ofxKinect::getDeviceId() { return deviceId; } //--------------------------------------------------------------------------- string ofxKinect::getSerial() { return serial; } //---------------------------------------------------------- float ofxKinect::getHeight() { return (float) height; } //--------------------------------------------------------------------------- float ofxKinect::getWidth() { return (float) width; } //---------------------------------------------------------- void ofxKinect::listDevices() { kinectContext.listDevices(); } //--------------------------------------------------------------------------- int ofxKinect::numTotalDevices() { return kinectContext.numTotal(); } //--------------------------------------------------------------------------- int ofxKinect::numAvailableDevices() { return kinectContext.numAvailable(); } //--------------------------------------------------------------------------- int ofxKinect::numConnectedDevices() { return kinectContext.numConnected(); } //--------------------------------------------------------------------------- bool ofxKinect::isDeviceConnected(int id) { return kinectContext.isConnected(id); } //--------------------------------------------------------------------------- bool ofxKinect::isDeviceConnected(string serial) { return kinectContext.isConnected(serial); } //--------------------------------------------------------------------------- int ofxKinect::nextAvailableId() { return kinectContext.nextAvailableId(); } //--------------------------------------------------------------------------- string ofxKinect::nextAvailableSerial() { return kinectContext.nextAvailableSerial(); } /* ***** PRIVATE ***** */ //--------------------------------------------------------------------------- void ofxKinect::updateDepthLookupTable() { unsigned char nearColor = bNearWhite ? 255 : 0; unsigned char farColor = bNearWhite ? 0 : 255; unsigned int maxDepthLevels = 10001; depthLookupTable.resize(maxDepthLevels); depthLookupTable[0] = 0; for(unsigned int i = 1; i < maxDepthLevels; i++) { depthLookupTable[i] = ofMap(i, nearClipping, farClipping, nearColor, farColor, true); } } //---------------------------------------------------------- void ofxKinect::updateDepthPixels() { int n = width * height; for(int i = 0; i < n; i++) { distancePixels[i] = depthPixelsRaw[i]; } for(int i = 0; i < n; i++) { depthPixels[i] = depthLookupTable[depthPixelsRaw[i]]; } } //--------------------------------------------------------------------------- void ofxKinect::grabDepthFrame(freenect_device *dev, void *depth, uint32_t timestamp) { ofxKinect* kinect = kinectContext.getKinect(dev); if(kinect->kinectDevice == dev) { kinect->lock(); swap(kinect->depthPixelsRawBack,kinect->depthPixelsRawIntra); kinect->bNeedsUpdateDepth = true; kinect->unlock(); freenect_set_depth_buffer(kinect->kinectDevice,kinect->depthPixelsRawBack.getPixels()); } } //--------------------------------------------------------------------------- void ofxKinect::grabVideoFrame(freenect_device *dev, void *video, uint32_t timestamp) { ofxKinect* kinect = kinectContext.getKinect(dev); if(kinect->kinectDevice == dev) { kinect->lock(); swap(kinect->videoPixelsBack,kinect->videoPixelsIntra); kinect->bNeedsUpdateVideo = true; kinect->unlock(); freenect_set_video_buffer(kinect->kinectDevice,kinect->videoPixelsBack.getPixels()); } } //--------------------------------------------------------------------------- void ofxKinect::threadedFunction(){ if(currentLed < 0) { freenect_set_led(kinectDevice, (freenect_led_options)ofxKinect::LED_GREEN); } freenect_frame_mode videoMode = freenect_find_video_mode(FREENECT_RESOLUTION_MEDIUM, bIsVideoInfrared?FREENECT_VIDEO_IR_8BIT:FREENECT_VIDEO_RGB); freenect_set_video_mode(kinectDevice, videoMode); freenect_frame_mode depthMode = freenect_find_depth_mode(FREENECT_RESOLUTION_MEDIUM, bUseRegistration?FREENECT_DEPTH_REGISTERED:FREENECT_DEPTH_MM); freenect_set_depth_mode(kinectDevice, depthMode); ofLogVerbose("ofxKinect") << "device " << deviceId << " " << serial << " connection opened"; freenect_start_depth(kinectDevice); if(bGrabVideo) { freenect_start_video(kinectDevice); } while(isThreadRunning() && freenect_process_events(kinectContext.getContext()) >= 0) { if(bTiltNeedsApplying) { freenect_set_tilt_degs(kinectDevice, targetTiltAngleDeg); bTiltNeedsApplying = false; } if(bLedNeedsApplying) { if(currentLed == ofxKinect::LED_DEFAULT) { freenect_set_led(kinectDevice, (freenect_led_options)ofxKinect::LED_GREEN); } else { freenect_set_led(kinectDevice, (freenect_led_options)currentLed); } bLedNeedsApplying = false; } freenect_update_tilt_state(kinectDevice); freenect_raw_tilt_state * tilt = freenect_get_tilt_state(kinectDevice); currentTiltAngleDeg = freenect_get_tilt_degs(tilt); rawAccel.set(tilt->accelerometer_x, tilt->accelerometer_y, tilt->accelerometer_z); double dx,dy,dz; freenect_get_mks_accel(tilt, &dx, &dy, &dz); mksAccel.set(dx, dy, dz); } // finish up a tilt on exit if(bTiltNeedsApplying) { freenect_set_tilt_degs(kinectDevice, targetTiltAngleDeg); bTiltNeedsApplying = false; } freenect_stop_depth(kinectDevice); freenect_stop_video(kinectDevice); if(currentLed < 0) { freenect_set_led(kinectDevice, (freenect_led_options)ofxKinect::LED_YELLOW); } kinectContext.close(*this); ofLogVerbose("ofxKinect") << "device " << deviceId << " connection closed"; } //--------------------------------------------------------------------------- // ofxKinectContext //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- ofxKinectContext::ofxKinectContext() { bInited = false; kinectContext = NULL; } ofxKinectContext::~ofxKinectContext() { closeAll(); clear(); } //--------------------------------------------------------------------------- static bool sortKinectPairs(ofxKinectContext::KinectPair A, ofxKinectContext::KinectPair B){ return A.serial < B.serial; } //--------------------------------------------------------------------------- bool ofxKinectContext::init() { if(freenect_init(&kinectContext, NULL) < 0) { ofLogError("ofxKinect") << "init(): freenect_init failed"; bInited = false; return false; } freenect_set_log_level(kinectContext, FREENECT_LOG_WARNING); freenect_select_subdevices(kinectContext, (freenect_device_flags)(FREENECT_DEVICE_MOTOR | FREENECT_DEVICE_CAMERA)); bInited = true; ofLogVerbose("ofxKinect") << "context inited"; buildDeviceList(); listDevices(true); return true; } //--------------------------------------------------------------------------- void ofxKinectContext::clear() { if(isInited() && numConnected() < 1) { freenect_shutdown(kinectContext); kinectContext = NULL; bInited = false; ofLogVerbose("ofxKinect") << "context cleared"; } } bool ofxKinectContext::isInited() { return bInited; } bool ofxKinectContext::open(ofxKinect& kinect, int id) { // rebuild if necessary (aka new kinects plugged in) buildDeviceList(); if(numConnected() >= numTotal()) { ofLogWarning("ofxKinect") << "no available devices found"; return false; } // is the id available? if(id < 0) { id = nextAvailableId(); } if(isConnected(id)) { ofLogWarning("ofxKinect") << "device " << id << " already connected"; return false; } // open and add to vector if(freenect_open_device(kinectContext, &kinect.kinectDevice, id) < 0) { ofLogError("ofxKinect") << "could not open device " << id; return false; } kinects.insert(pair(id, &kinect)); // set kinect id & serial from bus id kinect.deviceId = id; kinect.serial = deviceList[getDeviceIndex(id)].serial; return true; } bool ofxKinectContext::open(ofxKinect& kinect, string serial) { // rebuild if necessary (aka new kinects plugged in) buildDeviceList(); if(numConnected() >= numTotal()) { ofLogWarning("ofxKinect") << "no available devices found"; return false; } // is the serial available? if(isConnected(serial)) { ofLogWarning("ofxKinect") << "device " << serial << " already connected"; return false; } // open and add to vector if(freenect_open_device_by_camera_serial(kinectContext, &kinect.kinectDevice, serial.c_str()) < 0) { ofLogError("ofxKinect") << "could not open device " << serial; return false; } int index = getDeviceIndex(serial); kinects.insert(pair(deviceList[index].id, &kinect)); kinect.deviceId = deviceList[index].id; kinect.serial = serial; return true; } void ofxKinectContext::close(ofxKinect& kinect) { // check if it's already closed int id = -1; std::map::iterator iter; for(iter = kinects.begin(); iter != kinects.end(); ++iter) { if(iter->second == &kinect) { id = iter->first; break; } } if(id == -1) return; // remove connected device and close iter = kinects.find(id); if(iter != kinects.end()) { kinects.erase(iter); freenect_close_device(kinect.kinectDevice); } } void ofxKinectContext::closeAll() { // make copy of map to avoid invalidating iter when calling close() std::map kinectsCopy(kinects); std::map::iterator iter; for(iter = kinectsCopy.begin(); iter != kinectsCopy.end(); ++iter) { iter->second->close(); } } //--------------------------------------------------------------------------- void ofxKinectContext::buildDeviceList() { deviceList.clear(); // build the device list from freenect freenect_device_attributes * devAttrib; int numDevices = freenect_list_device_attributes(kinectContext, &devAttrib); // save bus ids ... for(int i = 0; i < numDevices; i++){ KinectPair kp; kp.id = i; kp.serial = (string) devAttrib->camera_serial; deviceList.push_back(kp); devAttrib = devAttrib->next; } freenect_free_device_attributes(devAttrib); // sort devices by serial number sort(deviceList.begin(), deviceList.end(), sortKinectPairs); } void ofxKinectContext::listDevices(bool verbose) { if(!isInited()) init(); stringstream stream; if(numTotal() == 0) { stream << "no devices found"; return; } else if(numTotal() == 1) { stream << 1 << " device found"; } else { stream << deviceList.size() << " devices found"; } if(verbose) { ofLogVerbose("ofxKinect") << stream.str(); } else { ofLogNotice("ofxKinect") << stream.str(); } stream.str(""); for(unsigned int i = 0; i < deviceList.size(); ++i) { stream << " id: " << deviceList[i].id << " serial: " << deviceList[i].serial; if(verbose) { ofLogVerbose("ofxKinect") << stream.str(); } else { ofLogNotice("ofxKinect") << stream.str(); } stream.str(""); } } int ofxKinectContext::numTotal() { if(!isInited()) init(); return freenect_num_devices(kinectContext); } int ofxKinectContext::numAvailable() { if(!isInited()) init(); return freenect_num_devices(kinectContext) - kinects.size(); } int ofxKinectContext::numConnected() { return kinects.size(); } ofxKinect* ofxKinectContext::getKinect(freenect_device* dev) { std::map::iterator iter; for(iter = kinects.begin(); iter != kinects.end(); ++iter) { if(iter->second->kinectDevice == dev) return iter->second; } return NULL; } int ofxKinectContext::getDeviceIndex(int id) { for(unsigned int i = 0; i < deviceList.size(); ++i) { if(deviceList[i].id == id) return i; } return -1; } int ofxKinectContext::getDeviceIndex(string serial) { for(unsigned int i = 0; i < deviceList.size(); ++i) { if(deviceList[i].serial == serial) return i; } return -1; } bool ofxKinectContext::isConnected(int id) { std::map::iterator iter = kinects.find(id); return iter != kinects.end(); } bool ofxKinectContext::isConnected(string serial) { std::map::iterator iter; for(iter = kinects.begin(); iter != kinects.end(); ++iter) { if(iter->second->getSerial() == serial) return true; } return false; } int ofxKinectContext::nextAvailableId() { if(!isInited()) init(); // a brute force free index finder :D std::map::iterator iter; for(unsigned int i = 0; i < deviceList.size(); ++i) { iter = kinects.find(deviceList[i].id); if(iter == kinects.end()) return deviceList[i].id; } return -1; } string ofxKinectContext::nextAvailableSerial() { if(!isInited()) init(); int id = nextAvailableId(); if(id == -1) { return ""; } return deviceList[getDeviceIndex(id)].serial; } ================================================ FILE: src/ofxKinect.h ================================================ /*============================================================================== Copyright (c) 2010, 2011 ofxKinect Team Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ---------------------------------------------------------------------------- This project uses libfreenect, copyrighted by the Open Kinect Project using the Apache License v2. See the file "APACHE20" in libs/libfreenect. See http://www.openkinect.org & https://github.com/OpenKinect/libfreenect for documentation ==============================================================================*/ #pragma once #include "ofMain.h" #include "libfreenect.h" #include "ofxBase3DVideo.h" #if defined(_MSC_VER) || defined(_WIN32) || defined(WIN32) || defined(__MINGW32__) // do windows stuff #else // mac and linux need this #include #endif class ofxKinectContext; /// \class ofxKinect /// /// wrapper for a freenect kinect device /// /// references: /// - http://openkinect.org/wiki/Main_Page /// - https://github.com/OpenKinect/libfreenect/blob/master/include/libfreenect.h /// class ofxKinect : public ofxBase3DVideo, protected ofThread { public: ofxKinect(); virtual ~ofxKinect(); /// \section Main /// initialize resources, must be called before open() /// infrared controls whether the video image is rgb or IR /// set video to false to disable video image grabbing (saves bandwidth) /// set texture to false if you don't need to use the internal textures /// /// naturally, if you disable the video image the video pixels and /// RGB color will be 0 bool init(bool infrared=false, bool video=true, bool texture=true); /// clear resources, do not call this while ofxKinect is running! void clear(); /// calibrates the depth image to align with the rgb image, disabled by default /// /// call this before open(), has no effect while the connection is running /// /// note: this calculation uses some cpu, leave off if not needed void setRegistration(bool bUseRegistration=false); /// open the connection and start grabbing images /// /// set the id to choose a kinect, see numAvailableDevices() /// if you don't set the id (ie id=-1), the first available kinect will be used /// /// note: this is the freenct bus id and may change each time the app is run bool open(int id=-1); /// open using a kinect unique serial number /// /// NOTE: currently, libfreenect returns a serial number with all 0s for /// kinect models > 1414, so this will only work with the original xbox kinect bool open(string serial); /// close the connection and stop grabbing images void close(); /// is the connection currently open? bool isConnected(); /// is the current frame new? bool isFrameNew(); bool isFrameNewVideo(); bool isFrameNewDepth(); /// updates the pixel buffers and textures /// /// make sure to call this to update to the latest incoming frames void update(); /// \section Depth Data /// get the calulated distance for a depth point float getDistanceAt(int x, int y); float getDistanceAt(const ofPoint & p); /// calculates the coordinate in the world for the depth point (perspective calculation) /// /// center of image is (0.0) ofVec3f getWorldCoordinateAt(int cx, int cy); ofVec3f getWorldCoordinateAt(float cx, float cy, float wz); /// \section Intrinsic IR Sensor Parameters /// these values are used when depth registration is enabled to align the /// depth image to the rgb image, see http://www.ros.org/wiki/kinect_calibration/technical /// /// they could also be useful for real world accurate point clouds ... weee! /// get the distance between the IR sensor and IR emitter in cm float getSensorEmitterDistance(); /// get the distance between the IR sensor and the RGB camera in cm float getSensorCameraDistance(); /// get the size of a single pixel on the zero plane in mm float getZeroPlanePixelSize(); /// get the focal length of the IR sensor in mm float getZeroPlaneDistance(); /// \section RGB Data /// get the RGB value for a depth point /// /// see setRegistration() for calibrated depth->RGB points ofColor getColorAt(int x, int y); ofColor getColorAt(const ofPoint & p); /// \section Pixel Data /// get the pixels of the most recent rgb frame /// /// see setRegistration() for a calibrated depth->RGB image unsigned char* getPixels(); /// get the pixels of the most recent depth frame unsigned char* getDepthPixels(); ///< grayscale values unsigned short* getRawDepthPixels(); ///< raw 11 bit values /// get the distance in millimeters to a given point as a float array float* getDistancePixels(); /// get the video pixels reference ofPixels & getPixelsRef(); /// get the pixels of the most recent depth frame ofPixels & getDepthPixelsRef(); ///< grayscale values ofShortPixels & getRawDepthPixelsRef(); ///< raw 11 bit values /// get the distance in millimeters to a given point as a float array ofFloatPixels & getDistancePixelsRef(); /// get the video (ir or rgb) texture ofTexture& getTextureReference(); /// get the grayscale depth texture ofTexture& getDepthTextureReference(); /// \section Grayscale Depth Value /// set the near value of the pixels in the grayscale depth image to white /// /// bEnabled = true: pixels closer to the camera are brighter (default) /// bEnabled = false: pixels closer to the camera are darker void enableDepthNearValueWhite(bool bEnabled=true); bool isDepthNearValueWhite(); /// set the clipping planes for the depth calculations in millimeters /// /// these are used for the depth value (12bit) -> grayscale (1 byte) conversion /// ie setting a short range will give you greater sensitivity from 0-255 /// /// default is 50cm - 4m /// note: you won't get any data < 50cm and distances > 4m start to get noisy void setDepthClipping(float nearClip=500, float farClip=4000); float getNearClipping(); float getFarClipping(); /// \section Query Capabilities /// check for device capabilites ... /// motor, led, or accelerometer control isn't currently supported /// by libfreenect with newer Kinect models (> 1414) bool hasAccelControl(); bool hasCamTiltControl(); bool hasLedControl(); /// \section Accelerometer Data /// get the XYZ accelerometer values /// /// ... yes, the kinect has an accelerometer /// raw axis values ofPoint getRawAccel(); /// axis-based gravity adjusted accelerometer values /// /// from libfreeenect: /// /// as laid out via the accelerometer data sheet, which is available at /// /// http://www.kionix.com/Product%20Sheets/KXSD9%20Product%20Brief.pdf /// ofPoint getMksAccel(); /// get the current pitch (x axis) & roll (z axis) of the kinect in degrees /// /// useful to correct the 3d scene based on the camera inclination /// float getAccelPitch(); float getAccelRoll(); /// \section Camera Tilt Motor /// set tilt angle of the camera in degrees /// 0 is flat, the range is -30 to 30 bool setCameraTiltAngle(float angleInDegrees); /// get the current angle float getCurrentCameraTiltAngle(); /// get the target angle (if the camera is currently moving) float getTargetCameraTiltAngle(); /// \section LED enum LedMode { LED_DEFAULT = -1, // yellow when not running, green when running LED_OFF = 0, LED_GREEN = 1, LED_RED = 2, LED_YELLOW = 3, LED_BLINK_GREEN = 4, LED_BLINK_YELLOW_RED = 6 }; /// set the current led color and/or blink mode, /// only applied while the kinect is open void setLed(ofxKinect::LedMode mode); /// \section Draw /// enable/disable frame loading into textures on update() void setUseTexture(bool bUse); /// draw the video texture void draw(float x, float y, float w, float h); void draw(float x, float y); void draw(const ofPoint& point); void draw(const ofRectangle& rect); /// draw the grayscale depth texture void drawDepth(float x, float y, float w, float h); void drawDepth(float x, float y); void drawDepth(const ofPoint& point); void drawDepth(const ofRectangle& rect); /// \section Util /// get the device id /// returns -1 if not connected int getDeviceId(); /// get the unique serial number /// returns an empty string "" if not connected /// /// NOTE: currently, libfreenect returns a serial number with all 0s for /// kinect models > 1414, so this will only work with the original xbox kinect string getSerial(); /// static kinect image size const static int width = 640; const static int height = 480; float getHeight(); float getWidth(); /// \section Static global kinect context functions /// print the device list static void listDevices(); /// get the total number of devices static int numTotalDevices(); /// get the number of available devices (not connected) static int numAvailableDevices(); /// get the number of currently connected devices static int numConnectedDevices(); /// is a device already connected? static bool isDeviceConnected(int id); static bool isDeviceConnected(string serial); /// get the id of the next available device, /// returns -1 if nothing found static int nextAvailableId(); /// get the serial number of the next available device, /// returns an empty string "" if nothing found static string nextAvailableSerial(); protected: int deviceId; ///< -1 when not connected string serial; ///< unique serial number, "" when not connected bool bUseTexture; ofTexture depthTex; ///< the depth texture ofTexture videoTex; ///< the RGB texture bool bGrabberInited; ofPixels videoPixels; ofPixels depthPixels; ofShortPixels depthPixelsRaw; ofFloatPixels distancePixels; ofPoint rawAccel; ofPoint mksAccel; float targetTiltAngleDeg; float currentTiltAngleDeg; bool bTiltNeedsApplying; int currentLed; bool bLedNeedsApplying; bool bHasMotorControl; // cam tilt motor //bool bHasAccelContol; // for future use //bool bHasLedControl; // for future use // for auto connect tries float timeSinceOpen; int lastDeviceId; bool bGotData; int tryCount; private: friend class ofxKinectContext; /// global statics shared between kinect instances static ofxKinectContext kinectContext; freenect_device* kinectDevice; ///< kinect device handle ofShortPixels depthPixelsRawIntra; ///< depth back ofPixels videoPixelsIntra; ///< rgb back ofShortPixels depthPixelsRawBack; ///< depth back ofPixels videoPixelsBack; ///< rgb back vector depthLookupTable; void updateDepthLookupTable(); void updateDepthPixels(); bool bIsFrameNewVideo, bIsFrameNewDepth; bool bNeedsUpdateVideo, bNeedsUpdateDepth; bool bGrabVideo; bool bUseRegistration; bool bNearWhite; float nearClipping, farClipping; bool bIsVideoInfrared; ///< is the video image infrared or RGB? int videoBytesPerPixel; ///< how many bytes per pixel in the video image /// libfreenect callbacks static void grabDepthFrame(freenect_device* dev, void* depth, uint32_t timestamp); static void grabVideoFrame(freenect_device* dev, void* video, uint32_t timestamp); /// thread function void threadedFunction(); }; /// \class ofxKinectContext /// /// wrapper for the freenect context /// /// do not use this directly /// class ofxKinectContext { public: ofxKinectContext(); ~ofxKinectContext(); /// \section Main /// init the freenect context bool init(); /// clear the freenect context /// closes all currently connected devices void clear(); /// is the context inited? bool isInited(); /// open a kinect device /// an id of -1 will open the first available bool open(ofxKinect& kinect, int id=-1); /// open a kinect device by it's unique serial number bool open(ofxKinect& kinect, string serial); /// close a kinect device void close(ofxKinect& kinect); /// closes all currently connected kinects void closeAll(); /// \section Util /// (re)build the list of devices void buildDeviceList(); /// print the device list void listDevices(bool verbose=false); /// get the total number of devices int numTotal(); /// get the number of available devices (not connected) int numAvailable(); /// get the number of currently connected devices int numConnected(); /// get the kinect object from a device pointer /// returns NULL if not found ofxKinect* getKinect(freenect_device* dev); /// get the deviceList index from an id /// returns -1 if not found int getDeviceIndex(int id); /// get the deviceList index from an id /// returns -1 if not found int getDeviceIndex(string serial); /// is a device with this id already connected? bool isConnected(int id); /// is a device with this serial already connected? bool isConnected(string serial); /// get the id of the next available device, /// returns -1 if nothing found int nextAvailableId(); /// get the serial number of the next available device, /// returns an empty string "" if nothing found string nextAvailableSerial(); /// get the raw pointer freenect_context* getContext() {return kinectContext;} // for auto-enumeration struct KinectPair{ string serial; ///< unique serial number int id; ///< freenect bus id }; private: bool bInited; ///< has the context been initialized? freenect_context* kinectContext; ///< kinect context handle std::vector deviceList; ///< list of available devices, sorted by serial lexicographically std::map kinects; ///< the connected kinects };