1 // subsystemFactory.cxx - factory for subsystems
3 // Copyright (C) 2012 James Turner zakalawe@mac.com
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include "subsystemFactory.hxx"
25 #include <simgear/structure/subsystem_mgr.hxx>
26 #include <simgear/structure/exception.hxx>
27 #include <simgear/debug/logstream.hxx>
28 #include <simgear/structure/commands.hxx>
29 #include <simgear/structure/event_mgr.hxx>
31 #include <Main/globals.hxx>
32 #include <Sound/soundmanager.hxx>
35 #include <Aircraft/controls.hxx>
36 #include <Aircraft/FlightHistory.hxx>
37 #include <Main/fg_props.hxx>
38 #include <Main/FGInterpolator.hxx>
39 #include <Main/fg_io.hxx>
40 #include <FDM/fdm_shell.hxx>
41 #include <Environment/environment_mgr.hxx>
42 #include <Environment/ephemeris.hxx>
43 #include <Instrumentation/instrument_mgr.hxx>
44 #include <Instrumentation/HUD/HUD.hxx>
45 #include <Systems/system_mgr.hxx>
46 #include <Autopilot/route_mgr.hxx>
47 #include <Autopilot/autopilotgroup.hxx>
48 #include <Traffic/TrafficMgr.hxx>
49 #include <Network/fgcom.hxx>
50 #include <Network/HTTPClient.hxx>
51 #include <Cockpit/cockpitDisplayManager.hxx>
52 #include <GUI/new_gui.hxx>
53 #include <Main/logger.hxx>
54 #include <ATC/atc_mgr.hxx>
55 #include <AIModel/AIManager.hxx>
56 #include <MultiPlayer/multiplaymgr.hxx>
57 #include <AIModel/submodel.hxx>
58 #include <Aircraft/controls.hxx>
59 #include <Input/input.hxx>
60 #include <Aircraft/replay.hxx>
61 #include <Sound/voice.hxx>
62 #include <Canvas/canvas_mgr.hxx>
63 #include <Canvas/gui_mgr.hxx>
64 #include <Time/light.hxx>
65 #include <Viewer/viewmgr.hxx>
66 #include <Model/acmodel.hxx>
67 #include <Model/modelmgr.hxx>
74 SGSubsystem* createSubsystemByName(const std::string& name)
76 #define MAKE_SUB(cl, n) \
77 if (name == n) return new cl;
79 MAKE_SUB(FGSoundManager, "sound");
80 MAKE_SUB(FGInterpolator, "prop-interpolator")
81 MAKE_SUB(FGProperties, "properties");
82 MAKE_SUB(FGHTTPClient, "http");
83 MAKE_SUB(FDMShell, "flight");
84 MAKE_SUB(FGEnvironmentMgr, "environment");
85 MAKE_SUB(Ephemeris, "ephemeris");
86 MAKE_SUB(FGSystemMgr, "systems");
87 MAKE_SUB(FGInstrumentMgr, "instrumentation");
89 MAKE_SUB(flightgear::CockpitDisplayManager, "cockpit-displays");
90 MAKE_SUB(FGRouteMgr, "route-manager");
92 MAKE_SUB(FGLogger, "logger");
93 MAKE_SUB(NewGUI, "gui");
94 MAKE_SUB(CanvasMgr, "Canvas");
95 MAKE_SUB(GUIMgr, "CanvasGUI");
96 MAKE_SUB(FGATCManager, "ATC");
97 MAKE_SUB(FGMultiplayMgr, "mp");
98 MAKE_SUB(FGAIManager, "ai-model");
99 MAKE_SUB(FGSubmodelMgr, "submodel-mgr");
100 MAKE_SUB(FGTrafficManager, "traffic-manager");
101 MAKE_SUB(FGControls, "controls");
102 MAKE_SUB(FGInput, "input");
103 MAKE_SUB(FGReplay, "replay");
104 MAKE_SUB(FGFlightHistory, "history");
105 #ifdef ENABLE_AUDIO_SUPPORT
106 MAKE_SUB(FGVoiceMgr, "voice");
109 MAKE_SUB(FGCom, "fgcom");
111 MAKE_SUB(FGLight, "lighting");
112 MAKE_SUB(FGAircraftModel, "aircraft-model");
113 MAKE_SUB(FGModelMgr, "model-manager");
114 MAKE_SUB(FGViewMgr, "view-manager");
117 throw sg_range_exception("unknown subsystem:" + name);
120 SGSubsystemMgr::GroupType mapGroupNameToType(const std::string& s)
122 if (s == "init") return SGSubsystemMgr::INIT;
123 if (s == "general") return SGSubsystemMgr::GENERAL;
124 if (s == "fdm") return SGSubsystemMgr::FDM;
125 if (s == "post-fdm") return SGSubsystemMgr::POST_FDM;
126 if (s == "display") return SGSubsystemMgr::DISPLAY;
127 if (s == "sound") return SGSubsystemMgr::SOUND;
129 SG_LOG(SG_GENERAL, SG_ALERT, "unrecognized subsystem group:" << s);
130 return SGSubsystemMgr::GENERAL;
133 static SGSubsystem* getSubsystem(const SGPropertyNode* arg, bool create)
135 std::string subsystem(arg->getStringValue("subsystem"));
136 std::string name = arg->getStringValue("name");
139 // default name is simply the subsytem's name
143 SGSubsystem* sys = globals->get_subsystem_mgr()->get_subsystem(name);
147 if( subsystem.empty() ) {
150 "do_add_subsystem: no subsystem/name supplied" );
157 "do_add_subsystem: duplicate subsystem name:" << name );
161 std::string groupname = arg->getStringValue("group");
162 SGSubsystemMgr::GroupType group = SGSubsystemMgr::GENERAL;
163 if (!groupname.empty()) {
164 group = mapGroupNameToType(groupname);
168 sys = createSubsystemByName(subsystem);
169 } catch (sg_exception& e) {
172 "subsystem creation failed:" << name
173 << ":" << e.getFormattedMessage() );
177 bool doInit = arg->getBoolValue("do-bind-init", false);
183 double minTime = arg->getDoubleValue("min-time-sec", 0.0);
184 globals->get_subsystem_mgr()
185 ->add(name.c_str(), sys, group, minTime);
191 do_check_subsystem_running(const SGPropertyNode* arg)
193 return getSubsystem(arg, false) != 0;
197 do_add_subsystem (const SGPropertyNode * arg)
199 return getSubsystem(arg, true) != 0;
202 static bool do_remove_subsystem(const SGPropertyNode * arg)
204 std::string name = arg->getStringValue("subsystem");
206 SGSubsystem* instance = globals->get_subsystem_mgr()->get_subsystem(name);
208 SG_LOG(SG_GENERAL, SG_ALERT, "do_remove_subsystem: unknown subsytem:" << name);
212 // is it safe to always call these? let's assume so!
213 instance->shutdown();
216 // unplug from the manager (this also deletes the instance!)
217 globals->get_subsystem_mgr()->remove(name.c_str());
223 * Built-in command: reinitialize one or more subsystems.
225 * subsystem[*]: the name(s) of the subsystem(s) to reinitialize; if
226 * none is specified, reinitialize all of them.
229 do_reinit (const SGPropertyNode * arg)
233 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
234 if (subsystems.empty()) {
235 globals->get_subsystem_mgr()->reinit();
237 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
238 const char * name = subsystems[i]->getStringValue();
239 SGSubsystem * subsystem = globals->get_subsystem(name);
240 if (subsystem == 0) {
242 SG_LOG( SG_GENERAL, SG_ALERT,
243 "Subsystem " << name << " not found" );
250 globals->get_event_mgr()->reinit();
256 * Built-in command: suspend one or more subsystems.
258 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
261 do_suspend (const SGPropertyNode * arg)
265 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
266 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
267 const char * name = subsystems[i]->getStringValue();
268 SGSubsystem * subsystem = globals->get_subsystem(name);
269 if (subsystem == 0) {
271 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << " not found");
273 subsystem->suspend();
280 * Built-in command: suspend one or more subsystems.
282 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
285 do_resume (const SGPropertyNode * arg)
289 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
290 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
291 const char * name = subsystems[i]->getStringValue();
292 SGSubsystem * subsystem = globals->get_subsystem(name);
293 if (subsystem == 0) {
295 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << " not found");
305 SGCommandMgr::command_t command;
307 { "add-subsystem", do_add_subsystem },
308 { "remove-subsystem", do_remove_subsystem },
309 { "subsystem-running", do_check_subsystem_running },
310 { "reinit", do_reinit },
311 { "suspend", do_suspend },
312 { "resume", do_resume },
313 { 0, 0 } // zero-terminated
316 void registerSubsystemCommands(SGCommandMgr* cmdMgr)
318 for (int i = 0; built_ins[i].name != 0; i++) {
319 cmdMgr->addCommand(built_ins[i].name, built_ins[i].command);
323 } // of namepace flightgear