Index: /OpenSceneGraph/trunk/include/osgDB/ReadFile
===================================================================
--- /OpenSceneGraph/trunk/include/osgDB/ReadFile (revision 13041)
+++ /OpenSceneGraph/trunk/include/osgDB/ReadFile (revision 13172)
@@ -44,6 +44,22 @@
 inline osg::Object* readObjectFile(const std::string& filename)
 {
-    return readObjectFile(filename,Registry::instance()->getOptions());
-}
+    return readObjectFile(filename, Registry::instance()->getOptions());
+}
+
+template<typename T>
+inline T* readFile(const std::string& filename, const Options* options)
+{
+    osg::ref_ptr<osg::Object> object = readObjectFile(filename, options);
+    osg::ref_ptr<T> t = dynamic_cast<T*>(object.get());
+    object = 0;    
+    return t.release();
+}
+
+template<typename T>
+inline T* readFile(const std::string& filename)
+{
+    return readFile<T>(filename, Registry::instance()->getOptions());
+}
+
 
 /** Read an osg::Image from file.
Index: /OpenSceneGraph/trunk/include/osgGA/Device
===================================================================
--- /OpenSceneGraph/trunk/include/osgGA/Device (revision 13172)
+++ /OpenSceneGraph/trunk/include/osgGA/Device (revision 13172)
@@ -0,0 +1,52 @@
+/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
+ *
+ * This library is open source and may be redistributed and/or modified under
+ * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
+ * (at your option) any later version.  The full license is in LICENSE file
+ * included with this distribution, and on the openscenegraph.org website.
+ *
+ * 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
+ * OpenSceneGraph Public License for more details.
+*/
+
+#ifndef OSGGA_EVENTSOURCE
+#define OSGGA_EVENTSOURCE 1
+
+#include <osgGA/EventQueue>
+
+namespace osgGA {
+
+/**
+ * Device base class from abstracting away from devices/windows that can generate events.
+ */
+class OSGGA_EXPORT Device : public osg::Object
+{
+    public:
+
+        Device();
+        Device(const Device& es, const osg::CopyOp& copyop);
+
+        META_Object(osgGA,Device);
+
+        virtual void checkEvents() {};
+
+        void setEventQueue(osgGA::EventQueue* eventQueue) { _eventQueue = eventQueue; }
+        osgGA::EventQueue* getEventQueue() { return _eventQueue.get(); }
+        const osgGA::EventQueue* getEventQueue() const { return _eventQueue.get(); }
+
+    protected:
+
+        virtual ~Device();
+
+        /** Prevent unwanted copy operator.*/
+        Device& operator = (const Device&) { return *this; }
+
+        osg::ref_ptr<osgGA::EventQueue> _eventQueue;
+        
+};
+
+}
+
+#endif
Index: /OpenSceneGraph/trunk/include/osgViewer/View
===================================================================
--- /OpenSceneGraph/trunk/include/osgViewer/View (revision 13053)
+++ /OpenSceneGraph/trunk/include/osgViewer/View (revision 13172)
@@ -25,4 +25,5 @@
 #include <osgGA/EventVisitor>
 #include <osgGA/EventQueue>
+#include <osgGA/Device>
 
 #include <osgViewer/Scene>
@@ -109,4 +110,17 @@
         const osgDB::ImagePager* getImagePager() const;
 
+       
+        /** Add a Device.
+         * The Device is polled on each new frame via it's Device::checkEvents() method and any events generated then collected via Device::getEventQueue()*/
+        void addDevice(osgGA::Device* eventSource);
+
+        /** Remove a Device. /*/
+        void removeDevice(osgGA::Device* eventSource);
+
+        typedef std::vector< osg::ref_ptr<osgGA::Device> > Devices;
+
+        Devices& getDevices() { return _eventSources; }
+        const Devices& getDevices() const { return _eventSources; }
+
 
         /* Set the EventQueue that the View uses to integrate external non window related events.*/
@@ -251,4 +265,6 @@
         osg::Timer_t                            _startTick;
 
+        Devices                            _eventSources;
+
         osg::ref_ptr<osgViewer::Scene>          _scene;
         osg::ref_ptr<osgGA::EventQueue>         _eventQueue;
Index: /OpenSceneGraph/trunk/include/osgViewer/ViewerBase
===================================================================
--- /OpenSceneGraph/trunk/include/osgViewer/ViewerBase (revision 13041)
+++ /OpenSceneGraph/trunk/include/osgViewer/ViewerBase (revision 13172)
@@ -118,5 +118,4 @@
 
 
-
         /** Set the done flag to signal the viewer's work is done and should exit the frame loop.*/
         void setDone(bool done) { _done = done; }
Index: /OpenSceneGraph/trunk/src/osgPlugins/sdl/JoystickDevice.cpp
===================================================================
--- /OpenSceneGraph/trunk/src/osgPlugins/sdl/JoystickDevice.cpp (revision 13172)
+++ /OpenSceneGraph/trunk/src/osgPlugins/sdl/JoystickDevice.cpp (revision 13172)
@@ -0,0 +1,185 @@
+/* -*-c++-*- Present3D - Copyright (C) 1999-2006 Robert Osfield 
+ *
+ * This software is open source and may be redistributed and/or modified under  
+ * the terms of the GNU General Public License (GPL) version 2.0.
+ * The full license is in LICENSE.txt file included with this distribution,.
+ * 
+ * This software 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 
+ * include LICENSE.txt for more details.
+*/
+
+#include <osgViewer/Viewer>
+
+#include "JoystickDevice.h"
+
+#include <SDL.h>
+#include <SDL_events.h>
+#include <SDL_joystick.h>
+
+#include <iostream>
+
+JoystickDevice::JoystickDevice()
+{
+    _verbose = false;
+
+    // init SDL
+    if ( SDL_Init(SDL_INIT_JOYSTICK) < 0 )
+    {
+        fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
+        exit(1);
+    }
+    atexit(SDL_Quit);
+
+    int numJoysticks =  SDL_NumJoysticks();
+    
+    if (_verbose)
+    {
+        std::cout<<"number of joysticks "<<numJoysticks<<std::endl;
+        for(int i=0; i<numJoysticks; ++i)
+        {
+            std::cout<<"Joystick name '"<<SDL_JoystickName(i)<<"'"<<std::endl;
+        }
+    }
+    
+    _joystick = numJoysticks>0 ? SDL_JoystickOpen(0) : 0;
+    
+    _numAxes = _joystick ? SDL_JoystickNumAxes(_joystick) : 0;
+    _numBalls = _joystick ? SDL_JoystickNumBalls(_joystick) : 0;
+    _numHats = _joystick ? SDL_JoystickNumHats(_joystick) : 0;
+    _numButtons = _joystick ? SDL_JoystickNumButtons(_joystick) : 0;
+
+    if (_verbose)
+    {
+        std::cout<<"numAxes = "<<_numAxes<<std::endl;
+        std::cout<<"numBalls = "<<_numBalls<<std::endl;
+        std::cout<<"numHats = "<<_numHats<<std::endl;
+        std::cout<<"numButtons = "<<_numButtons<<std::endl;
+    }
+        
+    addMouseButtonMapping(4, 1); // left
+    addMouseButtonMapping(5, 3); // right
+    addMouseButtonMapping(6, 2); // middle
+
+    addKeyMapping(10, ' '); // R2
+    
+    addKeyMapping(0, '1'); // 1
+    addKeyMapping(1, '2'); // 2
+    addKeyMapping(2, '3'); // 3
+    addKeyMapping(4, '4'); // 4
+
+    addKeyMapping(7, ' '); // home
+
+    addKeyMapping(8, osgGA::GUIEventAdapter::KEY_Page_Up); // Start
+    addKeyMapping(9, osgGA::GUIEventAdapter::KEY_Page_Down); // Start
+    addKeyMapping(10, osgGA::GUIEventAdapter::KEY_Home); // Start
+
+
+    capture(_axisValues, _buttonValues);
+}
+
+JoystickDevice::~JoystickDevice()
+{
+}
+
+void JoystickDevice::capture(ValueList& axisValues, ValueList& buttonValues) const
+{
+    if (_joystick)
+    {
+        SDL_JoystickUpdate();
+        
+        axisValues.resize(_numAxes);
+        for(int ai=0; ai<_numAxes; ++ai)
+        {
+            axisValues[ai] = SDL_JoystickGetAxis(_joystick, ai);
+        }
+
+        buttonValues.resize(_numButtons);
+        for(int bi=0; bi<_numButtons; ++bi)
+        {
+            buttonValues[bi] = SDL_JoystickGetButton(_joystick, bi);
+        }
+    }
+}
+
+void JoystickDevice::checkEvents()
+{
+    if (_joystick)
+    {
+
+        OSG_NOTICE<<"JoystickDevice::checkEvents()"<<std::endl;
+    
+        ValueList newAxisValues;
+        ValueList newButtonValues;
+        
+        capture(newAxisValues, newButtonValues);
+        
+        unsigned int mouseXaxis = 0;
+        unsigned int mouseYaxis = 1;
+        
+        float prev_mx = (float)_axisValues[mouseXaxis]/32767.0f;
+        float prev_my = -(float)_axisValues[mouseYaxis]/32767.0f;
+
+        float mx = (float)newAxisValues[mouseXaxis]/32767.0f;
+        float my = -(float)newAxisValues[mouseYaxis]/32767.0f;
+        
+
+        osgGA::EventQueue* eq = getEventQueue();
+        double time = eq ? eq->getTime() : 0.0;
+        osgGA::GUIEventAdapter* previous_event = eq->getCurrentEventState();
+        float projected_mx = previous_event->getXmin() + (mx+1.0)*0.5*(previous_event->getXmax()-previous_event->getXmin());
+        float projected_my = previous_event->getYmin() + (my+1.0)*0.5*(previous_event->getYmax()-previous_event->getYmin());
+
+        if (mx!=prev_mx || my!=prev_my)
+        {
+            eq->mouseMotion(projected_mx, projected_my, time);
+        }
+
+        OSG_NOTICE<<"mx="<<mx<<", my="<<my<<", projected_mx="<<projected_mx<<", projected_my="<<projected_my<<std::endl;
+
+
+        if (_verbose)
+        {
+            for(int ai=0; ai<_numAxes; ++ai)
+            {
+                if (newAxisValues[ai]!=_axisValues[ai])
+                {
+                    std::cout<<"axis "<<ai<<" moved to "<<newAxisValues[ai]<<std::endl;
+                }
+            }
+        }
+                
+        for(int bi=0; bi<_numButtons; ++bi)
+        {
+            if (newButtonValues[bi]!=_buttonValues[bi])
+            {
+                if (_verbose)
+                {
+                    std::cout<<"button "<<bi<<" changed to "<<newButtonValues[bi]<<std::endl;
+                }
+
+                int key =  getKeyMapping(bi);
+                int mouseButton =  getMouseButtonMapping(bi);
+
+                if (mouseButton>0)
+                {
+                    if (newButtonValues[bi]==0) eq->mouseButtonRelease(projected_mx,projected_my,mouseButton,time);
+                    else  eq->mouseButtonPress(projected_mx,projected_my,mouseButton,time);
+                }
+                else if (key>0)
+                {
+
+                    if (newButtonValues[bi]==0) eq->keyRelease(key,time);
+                    else eq->keyPress(key,time);
+                }
+            }
+        }
+        
+        _axisValues.swap(newAxisValues);
+        _buttonValues.swap(newButtonValues);
+        
+    }
+
+}
+        
Index: /OpenSceneGraph/trunk/src/osgPlugins/sdl/ReaderWriterSDL.cpp
===================================================================
--- /OpenSceneGraph/trunk/src/osgPlugins/sdl/ReaderWriterSDL.cpp (revision 13172)
+++ /OpenSceneGraph/trunk/src/osgPlugins/sdl/ReaderWriterSDL.cpp (revision 13172)
@@ -0,0 +1,38 @@
+// Released under the OSGPL license, as part of the OpenSceneGraph distribution.
+//
+// ReaderWriter for sgi's .rgb format.
+// specification can be found at http://local.wasp.uwa.edu.au/~pbourke/dataformats/sgirgb/sgiversion.html
+
+#include <osg/Notify>
+
+#include <osgDB/FileNameUtils>
+#include <osgDB/Registry>
+
+#include "JoystickDevice.h"
+
+
+class ReaderWriterSDL : public osgDB::ReaderWriter
+{
+    public:
+
+        ReaderWriterSDL()
+        {
+            supportsExtension("sdl","SDL Device Integration");
+        }
+
+        virtual const char* className() const { return "SDL Device Integration plugin"; }
+
+        virtual ReadResult readObject(const std::string& file, const osgDB::ReaderWriter::Options* options =NULL) const
+        {
+            if (file=="joystick.sdl")
+            {
+                return new JoystickDevice;
+            }
+            return ReadResult::FILE_NOT_HANDLED;
+        }
+
+};
+
+// now register with Registry to instantiate the above
+// reader/writer.
+REGISTER_OSGPLUGIN(sdl, ReaderWriterSDL)
Index: /OpenSceneGraph/trunk/src/osgPlugins/sdl/JoystickDevice.h
===================================================================
--- /OpenSceneGraph/trunk/src/osgPlugins/sdl/JoystickDevice.h (revision 13172)
+++ /OpenSceneGraph/trunk/src/osgPlugins/sdl/JoystickDevice.h (revision 13172)
@@ -0,0 +1,79 @@
+/* -*-c++-*- Present3D - Copyright (C) 1999-2006 Robert Osfield 
+ *
+ * This software is open source and may be redistributed and/or modified under  
+ * the terms of the GNU General Public License (GPL) version 2.0.
+ * The full license is in LICENSE.txt file included with this distribution,.
+ * 
+ * This software 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 
+ * include LICENSE.txt for more details.
+*/
+
+#ifndef SDLINTEGRATION
+#define SDLINTEGRATION
+
+#include <osgGA/Device>
+
+#include <SDL.h>
+#include <SDL_events.h>
+#include <SDL_joystick.h>
+
+#include <vector>
+#include <map>
+
+class JoystickDevice : public osgGA::Device
+{
+    public:
+
+        JoystickDevice();
+
+        typedef std::vector<int> ValueList;
+        typedef std::map<int, int> ButtonMap;
+
+        virtual void checkEvents();
+        
+        void addMouseButtonMapping(int joystickButton, int mouseButton)
+        {
+            _mouseButtonMap[joystickButton] = mouseButton;
+        }
+        
+        int getMouseButtonMapping(int joystickButton)
+        {
+            ButtonMap::const_iterator itr = _mouseButtonMap.find(joystickButton);
+            if (itr != _mouseButtonMap.end()) return itr->second;
+            else return -1;
+        }
+
+        void addKeyMapping(int joystickButton, int key)
+        {
+            _keyMap[joystickButton] = key;
+        }
+        
+        int getKeyMapping(int joystickButton)
+        {
+            ButtonMap::const_iterator itr = _keyMap.find(joystickButton);
+            if (itr != _keyMap.end()) return itr->second;
+            else return -1;
+        }
+        
+    protected:
+    
+        virtual ~JoystickDevice();
+
+        void capture(ValueList& axisValues, ValueList& buttonValues) const;
+
+        SDL_Joystick*   _joystick;
+        int             _numAxes;
+        int             _numBalls;
+        int             _numHats;
+        int             _numButtons;
+        bool            _verbose;
+        
+        ValueList       _axisValues;
+        ValueList       _buttonValues;
+        ButtonMap       _mouseButtonMap;
+        ButtonMap       _keyMap;
+};
+
+#endif
Index: /OpenSceneGraph/trunk/src/osgPlugins/sdl/CMakeLists.txt
===================================================================
--- /OpenSceneGraph/trunk/src/osgPlugins/sdl/CMakeLists.txt (revision 13172)
+++ /OpenSceneGraph/trunk/src/osgPlugins/sdl/CMakeLists.txt (revision 13172)
@@ -0,0 +1,16 @@
+SET(TARGET_EXTERNAL_LIBRARIES ${SDL_LIBRARY} )
+INCLUDE_DIRECTORIES(${SDL_INCLUDE_DIR} )
+
+SET(TARGET_SRC
+    JoystickDevice.cpp
+    ReaderWriterSDL.cpp
+)
+
+SET(TARGET_H 
+    JoystickDevice.h
+)
+
+SET(TARGET_ADDED_LIBRARIES osgGA)
+
+#### end var setup  ###
+SETUP_PLUGIN(sdl sdl)
Index: /OpenSceneGraph/trunk/src/osgPlugins/CMakeLists.txt
===================================================================
--- /OpenSceneGraph/trunk/src/osgPlugins/CMakeLists.txt (revision 13161)
+++ /OpenSceneGraph/trunk/src/osgPlugins/CMakeLists.txt (revision 13172)
@@ -257,4 +257,13 @@
 ADD_SUBDIRECTORY(pvr)
 
+####################################################
+#
+# Device integration plugins
+#
+IF   (SDL_FOUND)
+    ADD_SUBDIRECTORY(sdl)
+ENDIF(SDL_FOUND)
+
+
 ##########to get all the variables of Cmake
 #GET_CMAKE_PROPERTY(MYVARS VARIABLES)
Index: /OpenSceneGraph/trunk/src/osgGA/Device.cpp
===================================================================
--- /OpenSceneGraph/trunk/src/osgGA/Device.cpp (revision 13172)
+++ /OpenSceneGraph/trunk/src/osgGA/Device.cpp (revision 13172)
@@ -0,0 +1,31 @@
+/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
+ *
+ * This library is open source and may be redistributed and/or modified under
+ * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
+ * (at your option) any later version.  The full license is in LICENSE file
+ * included with this distribution, and on the openscenegraph.org website.
+ *
+ * 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
+ * OpenSceneGraph Public License for more details.
+*/
+
+#include <osgGA/Device>
+
+using namespace osgGA;
+
+Device::Device()
+{
+    setEventQueue(new EventQueue);
+}
+
+Device::Device(const Device& es, const osg::CopyOp& copyop):
+    osg::Object(es,copyop)
+{
+    setEventQueue(new EventQueue);
+}
+
+Device::~Device()
+{
+}
Index: /OpenSceneGraph/trunk/src/osgGA/CMakeLists.txt
===================================================================
--- /OpenSceneGraph/trunk/src/osgGA/CMakeLists.txt (revision 12208)
+++ /OpenSceneGraph/trunk/src/osgGA/CMakeLists.txt (revision 13172)
@@ -11,4 +11,5 @@
     ${HEADER_PATH}/AnimationPathManipulator
     ${HEADER_PATH}/DriveManipulator
+    ${HEADER_PATH}/Device
     ${HEADER_PATH}/EventQueue
     ${HEADER_PATH}/EventVisitor
@@ -37,4 +38,5 @@
     AnimationPathManipulator.cpp
     DriveManipulator.cpp
+    Device.cpp
     EventQueue.cpp
     EventVisitor.cpp
Index: /OpenSceneGraph/trunk/src/osgViewer/Viewer.cpp
===================================================================
--- /OpenSceneGraph/trunk/src/osgViewer/Viewer.cpp (revision 13136)
+++ /OpenSceneGraph/trunk/src/osgViewer/Viewer.cpp (revision 13172)
@@ -646,4 +646,18 @@
 
 
+    // get events from user Devices attached to Viewer.
+    for(Devices::iterator eitr = _eventSources.begin();
+        eitr != _eventSources.end();
+        ++eitr)
+    {
+        osgGA::Device* es = eitr->get();
+        es->checkEvents();
+
+        // open question, will we need to reproject mouse coordinates into current view's coordinate frame as is down for GraphicsWindow provided events?
+        // for now assume now and just get the events directly without any reprojection.
+        es->getEventQueue()->takeEvents(events, cutOffTime);
+    }
+
+    // get events from all windows attached to Viewer.
     for(Contexts::iterator citr = contexts.begin();
         citr != contexts.end();
Index: /OpenSceneGraph/trunk/src/osgViewer/View.cpp
===================================================================
--- /OpenSceneGraph/trunk/src/osgViewer/View.cpp (revision 13041)
+++ /OpenSceneGraph/trunk/src/osgViewer/View.cpp (revision 13172)
@@ -2218,2 +2218,19 @@
 
 
+void View::addDevice(osgGA::Device* eventSource)
+{
+    Devices::iterator itr = std::find( _eventSources.begin(), _eventSources.end(), eventSource );
+    if (itr==_eventSources.end())
+    {
+        _eventSources.push_back(eventSource);
+    }
+}
+
+void View::removeDevice(osgGA::Device* eventSource)
+{
+    Devices::iterator itr = std::find( _eventSources.begin(), _eventSources.end(), eventSource );
+    if (itr!=_eventSources.end())
+    {
+        _eventSources.erase(itr);
+    }
+}
Index: /OpenSceneGraph/trunk/src/osgViewer/CompositeViewer.cpp
===================================================================
--- /OpenSceneGraph/trunk/src/osgViewer/CompositeViewer.cpp (revision 13111)
+++ /OpenSceneGraph/trunk/src/osgViewer/CompositeViewer.cpp (revision 13172)
@@ -736,4 +736,5 @@
     }
 
+    // get events from all windows attached to Viewer.
     for(Contexts::iterator citr = contexts.begin();
         citr != contexts.end();
@@ -926,4 +927,18 @@
     {
         View* view = vitr->get();
+
+        // get events from user Devices attached to Viewer.
+        for(osgViewer::View::Devices::iterator eitr = view->getDevices().begin();
+            eitr != view->getDevices().end();
+            ++eitr)
+        {
+            osgGA::Device* es = eitr->get();
+            es->checkEvents();
+
+            // open question, will we need to reproject mouse coordinates into current view's coordinate frame as is down for GraphicsWindow provided events?
+            // for now assume now and just get the events directly without any reprojection.
+            es->getEventQueue()->takeEvents(viewEventsMap[view], cutOffTime);
+        }
+
         view->getEventQueue()->takeEvents(viewEventsMap[view], cutOffTime);
     }
