[osg-submissions] [osg-users] Single Windowed OsgViewer on multiheaded

David Fries david at fries.net
Sat Mar 29 13:06:56 PDT 2008


On Fri, Mar 28, 2008 at 07:14:30PM +0000, Robert Osfield wrote:
> Hi Gordon,
> 
> Using env vars:
> 
> set OSG_SCREEN=1
> osgviewer cow.osg
> 
> Will open it up on screen 1.
> 
> Using command line:
> 
> osgviewer cow.osg --screen 1
> 
> In windowed mode:
> 
> set OSG_WINDOW="100 100 500 500"
> osgviewer cow.osg
> 
> osgviewer cow.osg --window 100 100 500 500
> 
> Or just use a osgViewer .view config file
> 
> osgviewer cow.osg -c OpenSceneGaph-Data/Configuration/SmallWindow.view
> 
> Or using a Producer .cfg (with 2.4.x)
> 
> osgviewer cow.osg -c myconfig.cfg
> 
> Or using env var:
> 
> export OSG_CONFIG_FILE=OpenSceneGaph-Data/Configuration/SmallWindow.view
> 
> osgviewer cow.osg
> 
> So...got enough options :-)
> 
> Robert.

No, but here is a patch (full files attached).

Don't dictate initial window position.
--window -1 -1 width height
OSG_WINDOW="-1 -1 width height"

The special value -1 -1 will now let the window manager position the
window, which is the window manager's job anyway.


I was really tempted to do --window width height, and
OSG_WINDOW="width height", and internally using INT_MIN or some other
way to identify that x & y were not specified.  It would probably be
cleaner, let me know and I'll rework the patch.

I only implemented it in X windows, as that's all I have available to
me and I don't like submitting patches I can't test.



Make cursor grab on startup optional,
--grab
OSG_GRAB=ON
OSG_GRAB=OFF

to enable or disable mouse grabs when the window is being setup.  Most
programs aren't anti-social and grab the mouse when they startup,
unless the window manager or click to focus dictates it, in which case
it is just given the focus, it doesn't grab it.  If someone does want
it to grab focus, they still can.  Just grabbing focus is very
annoying on focus follows mouse, you are typing away and oops, there
goes the focus, Esc out of osgviewer, move mouse outside of current
window, move back to current window, continue typing.

-- 
David Fries <david at fries.net>
http://fries.net/~david/ (PGP encryption key available)


Index: include/osgViewer/Viewer
===================================================================
--- include/osgViewer/Viewer	(revision 8011)
+++ include/osgViewer/Viewer	(working copy)
@@ -109,8 +109,8 @@
         virtual void viewerInit() { init(); }
 
         osg::observer_ptr<osg::Camera>              _cameraWithFocus;
-        
 
+        bool _grabFocus;
 };
 
 
Index: src/osgViewer/Viewer.cpp
===================================================================
--- src/osgViewer/Viewer.cpp	(revision 8011)
+++ src/osgViewer/Viewer.cpp	(working copy)
@@ -34,6 +34,7 @@
 Viewer::Viewer()
 {
     _viewerBase = this;
+    _grabFocus = false;
 
     constructorInit();
 }
@@ -41,6 +42,7 @@
 Viewer::Viewer(osg::ArgumentParser& arguments)
 {
     _viewerBase = this;
+    _grabFocus = false;
 
     constructorInit();
     
@@ -75,6 +77,15 @@
     int x = -1, y = -1, width = -1, height = -1;
     while (arguments.read("--window",x,y,width,height)) {}
     
+    if(const char *ptr = getenv("OSG_GRAB"))
+    {
+        if(!strcmp(ptr, "OFF"))
+            _grabFocus=false;
+        else if(!strcmp(ptr, "ON"))
+            _grabFocus=true;
+    }
+    while (arguments.read("--grab")) { _grabFocus=true; }
+
     bool ss3d = false;
     if ((ss3d=arguments.read("--3d-sd")) || arguments.read("--panoramic-sd"))
     {
@@ -412,8 +423,7 @@
         }
     }
     
-    bool grabFocus = true;
-    if (grabFocus)
+    if (_grabFocus)
     {
         for(Contexts::iterator citr = contexts.begin();
             citr != contexts.end();
Index: src/osgViewer/GraphicsWindowX11.cpp
===================================================================
--- src/osgViewer/GraphicsWindowX11.cpp	(revision 8011)
+++ src/osgViewer/GraphicsWindowX11.cpp	(working copy)
@@ -385,7 +385,11 @@
     
     Display* display = getDisplayToUse();
     
-    XMoveResizeWindow(display, _window, x, y, width, height);
+    // Let the window manager specify the position if x and y are -1.
+    if(x==-1 && y==-1)
+        XResizeWindow(display, _window, width, height);
+    else
+        XMoveResizeWindow(display, _window, x, y, width, height);
     
     XFlush(display);
     XSync(display, 0);
@@ -678,7 +682,9 @@
     sh.flags = 0;
     sh.flags |= USSize;
     sh.flags &= 0x7;
-    sh.flags |= USPosition;
+    // Let the window manager specify the position if x and y are -1.
+    if(_traits->x!=-1 || _traits->y!=-1)
+        sh.flags |= USPosition;
     sh.flags &= 0xB;
     sh.x = _traits->x;
     sh.y = _traits->y;
Index: src/osgViewer/ViewerBase.cpp
===================================================================
--- src/osgViewer/ViewerBase.cpp	(revision 8011)
+++ src/osgViewer/ViewerBase.cpp	(working copy)
@@ -32,7 +32,8 @@
 static osg::ApplicationUsageProxy ViewerBase_e0(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_CONFIG_FILE <filename>","Specify a viewer configuration file to load by default.");
 static osg::ApplicationUsageProxy ViewerBase_e1(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_THREADING <value>","Set the threading model using by Viewer, <value> can be SingleThreaded, CullDrawThreadPerContext, DrawThreadPerContext or CullThreadPerCameraDrawThreadPerContext.");
 static osg::ApplicationUsageProxy ViewerBase_e2(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_SCREEN <value>","Set the default screen that windows should open up on.");
-static osg::ApplicationUsageProxy ViewerBase_e3(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_WINDOW x y width height","Set the default window dimensions that windows should open up on.");
+static osg::ApplicationUsageProxy ViewerBase_e3(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_WINDOW x y width height","Set the default window dimensions that windows should open up on.  -1 -1 width height don't force position (when supported).");
+static osg::ApplicationUsageProxy ViewerBase_e4(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_GRAB <mode>", "OFF | ON Disable/Enable grabbing focus to the window.");
 
 
 using namespace osgViewer;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: GraphicsWindowX11.cpp
Type: text/x-c++src
Size: 57836 bytes
Desc: not available
Url : http://lists.openscenegraph.org/pipermail/osg-submissions-openscenegraph.org/attachments/20080329/e89bd2c5/attachment-0003.cpp 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ViewerBase.cpp
Type: text/x-c++src
Size: 21458 bytes
Desc: not available
Url : http://lists.openscenegraph.org/pipermail/osg-submissions-openscenegraph.org/attachments/20080329/e89bd2c5/attachment-0004.cpp 
-------------- next part --------------
/* -*-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 OSGVIEWER_Viewer
#define OSGVIEWER_Viewer 1

#include <osg/ArgumentParser>
#include <osgGA/EventVisitor>
#include <osgUtil/UpdateVisitor>
#include <osgViewer/GraphicsWindow>
#include <osgViewer/View>


namespace osgViewer {

/** Viewer holds a single view on to a single scene.*/
class OSGVIEWER_EXPORT Viewer : public ViewerBase, public osgViewer::View
{
    public:

        Viewer();

        Viewer(osg::ArgumentParser& arguments);

        Viewer(const osgViewer::Viewer& viewer, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);

        virtual ~Viewer();
        
        META_Object(osgViewer,Viewer);

        /** Take all the settings, Camera and Slaves from the passed in view(er), leaving it empty. */
        virtual void take(View& rhs);

        /** read the viewer configuration from a configuration file.*/
        virtual bool readConfiguration(const std::string& filename);
        
        /** Get whether at least of one of this viewers windows are realized.*/
        virtual bool isRealized() const;

        /** set up windows and associated threads.*/
        virtual void realize();

        virtual void setStartTick(osg::Timer_t tick);
        void setReferenceTime(double time=0.0);

        /** Set the sene graph data that viewer with view.*/
        virtual void setSceneData(osg::Node* node);


        /** Convenience method for setting up the viewer so it can be used embedded in an external managed window.
          * Returns the GraphicsWindowEmbedded that can be used by applications to pass in events to the viewer. */
        virtual GraphicsWindowEmbedded* setUpViewerAsEmbeddedInWindow(int x, int y, int width, int height);


        virtual double elapsedTime();
        
        virtual osg::FrameStamp* getViewerFrameStamp() { return getFrameStamp(); }


        /** Execute a main frame loop.
          * Equivalent to while (!viewer.done()) viewer.frame();
          * Also calls realize() if the viewer is not already realized,
          * and installs trackball manipulator if one is not already assigned.
          */
        virtual int run();

        virtual void advance(double simulationTime=USE_REFERENCE_TIME);

        virtual void eventTraversal();

        virtual void updateTraversal();
        
        void setCameraWithFocus(osg::Camera* camera) { _cameraWithFocus = camera; }
        osg::Camera* getCameraWithFocus() { return _cameraWithFocus.get(); }
        const osg::Camera* getCameraWithFocus() const { return _cameraWithFocus.get(); }
        
        virtual void getCameras(Cameras& cameras, bool onlyActive=true);
        
        virtual void getContexts(Contexts& contexts, bool onlyValid=true);

        virtual void getWindows(Windows& windows, bool onlyValid=true);

        virtual void getAllThreads(Threads& threads, bool onlyActive=true);

        virtual void getOperationThreads(OperationThreads& threads, bool onlyActive=true);

        virtual void getScenes(Scenes& scenes, bool onlyValid=true);

        virtual void getViews(Views& views, bool onlyValid=true);

        /** Get the keyboard and mouse usage of this viewer.*/
        virtual void getUsage(osg::ApplicationUsage& usage) const;


    protected:
    
        void constructorInit();
        
        virtual void viewerInit() { init(); }

        osg::observer_ptr<osg::Camera>              _cameraWithFocus;

        bool _grabFocus;
};


}

#endif
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Viewer.cpp
Type: text/x-c++src
Size: 36371 bytes
Desc: not available
Url : http://lists.openscenegraph.org/pipermail/osg-submissions-openscenegraph.org/attachments/20080329/e89bd2c5/attachment-0005.cpp 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://lists.openscenegraph.org/pipermail/osg-submissions-openscenegraph.org/attachments/20080329/e89bd2c5/attachment-0001.pgp 


More information about the osg-submissions mailing list