1 /* -*-c++-*- Present3D - Copyright (C) 1999-2006 Robert Osfield
2 *
3 * This software is open source and may be redistributed and/or modified under
4 * the terms of the GNU General Public License (GPL) version 2.0.
5 * The full license is in LICENSE.txt file included with this distribution,.
6 *
7 * This software is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * include LICENSE.txt for more details.
11 */
12
13 #include <osgPresentation/KeyEventHandler>
14 #include <osgPresentation/SlideEventHandler>
15
16 #include <osgViewer/Viewer>
17 #include <osg/Notify>
18 #include <osgDB/FileUtils>
19
20 #include <stdlib.h>
21
22 using namespace osgPresentation;
23
KeyEventHandler(int key,osgPresentation::Operation operation,const JumpData & jumpData)24 KeyEventHandler::KeyEventHandler(int key, osgPresentation::Operation operation, const JumpData& jumpData):
25 _key(key),
26 _operation(operation),
27 _jumpData(jumpData)
28 {
29 }
30
KeyEventHandler(int key,const std::string & str,osgPresentation::Operation operation,const JumpData & jumpData)31 KeyEventHandler::KeyEventHandler(int key, const std::string& str, osgPresentation::Operation operation, const JumpData& jumpData):
32 _key(key),
33 _command(str),
34 _operation(operation),
35 _jumpData(jumpData)
36 {
37 }
38
KeyEventHandler(int key,const osgPresentation::KeyPosition & keyPos,const JumpData & jumpData)39 KeyEventHandler::KeyEventHandler(int key, const osgPresentation::KeyPosition& keyPos, const JumpData& jumpData):
40 _key(key),
41 _keyPos(keyPos),
42 _operation(osgPresentation::EVENT),
43 _jumpData(jumpData)
44 {
45 }
46
handle(const osgGA::GUIEventAdapter & ea,osgGA::GUIActionAdapter &,osg::Object *,osg::NodeVisitor *)47 bool KeyEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& /*aa*/, osg::Object*, osg::NodeVisitor* /*nv*/)
48 {
49 if (ea.getHandled()) return false;
50
51 switch(ea.getEventType())
52 {
53 case(osgGA::GUIEventAdapter::KEYDOWN):
54 {
55 if (ea.getKey()==_key)
56 {
57 doOperation();
58 return true;
59 }
60 break;
61 }
62 default:
63 break;
64 }
65 return false;
66 }
67
getUsage(osg::ApplicationUsage &) const68 void KeyEventHandler::getUsage(osg::ApplicationUsage& /*usage*/) const
69 {
70 }
71
doOperation()72 void KeyEventHandler::doOperation()
73 {
74 switch(_operation)
75 {
76 case(osgPresentation::RUN):
77 {
78 OSG_NOTICE<<"Run "<<_command<<std::endl;
79
80 #if 0
81 osgDB::FilePathList& paths = osgDB::getDataFilePathList();
82 if (!paths.empty())
83 {
84 #ifdef _WIN32
85 std::string delimintor(";");
86 #else
87 std::string delimintor(":");
88 #endif
89 std::string filepath("OSG_FILE_PATH=");
90
91 bool needDeliminator = false;
92 for(osgDB::FilePathList::iterator itr = paths.begin();
93 itr != paths.end();
94 ++itr)
95 {
96 if (needDeliminator) filepath += delimintor;
97 filepath += *itr;
98 needDeliminator = true;
99 }
100 putenv( (char*) filepath.c_str());
101
102 std::string binpath("PATH=");
103 char* path = getenv("PATH");
104 if (path) binpath += path;
105
106 needDeliminator = true;
107 for(osgDB::FilePathList::iterator itr = paths.begin();
108 itr != paths.end();
109 ++itr)
110 {
111 if (needDeliminator) binpath += delimintor;
112 binpath += *itr;
113 needDeliminator = true;
114 }
115 putenv( (char*) binpath.c_str());
116
117 }
118 #endif
119
120 bool commandRunsInBackground = (_command.find("&")!=std::string::npos);
121
122 int result = system(_command.c_str());
123
124 OSG_INFO<<"system("<<_command<<") result "<<result<<std::endl;
125
126 if (commandRunsInBackground)
127 {
128 // Sleep briefly while command runs in background to give it a chance to open
129 // a window and obscure this present3D's window avoiding this present3D from
130 // rendering anything new before the new window opens.
131 OpenThreads::Thread::microSleep(500000); // half second sleep.
132 }
133
134 break;
135 }
136 case(osgPresentation::LOAD):
137 {
138 OSG_NOTICE<<"Load "<<_command<<std::endl;
139 break;
140 }
141 case(osgPresentation::EVENT):
142 {
143 OSG_INFO<<"Event "<<_keyPos._key<<" "<<_keyPos._x<<" "<<_keyPos._y<<std::endl;
144 if (SlideEventHandler::instance()) SlideEventHandler::instance()->dispatchEvent(_keyPos);
145 break;
146 }
147 case(osgPresentation::JUMP):
148 {
149 OSG_NOTICE<<"Requires jump "<<std::endl;
150 break;
151 }
152 case(osgPresentation::FORWARD_MOUSE_EVENT):
153 case(osgPresentation::FORWARD_TOUCH_EVENT):
154 break;
155 }
156
157 if (_jumpData.requiresJump())
158 {
159 _jumpData.jump(SlideEventHandler::instance());
160 }
161 else
162 {
163 OSG_NOTICE<<"No jump required."<<std::endl;
164 }
165 }
166
167