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 <Main/fg_props.hxx>
37 #include <Main/FGInterpolator.hxx>
38 #include <Main/fg_io.hxx>
39 #include <FDM/fdm_shell.hxx>
40 #include <Environment/environment_mgr.hxx>
41 #include <Environment/ephemeris.hxx>
42 #include <Instrumentation/instrument_mgr.hxx>
43 #include <Instrumentation/HUD/HUD.hxx>
44 #include <Systems/system_mgr.hxx>
45 #include <Autopilot/route_mgr.hxx>
46 #include <Autopilot/autopilotgroup.hxx>
47 #include <Traffic/TrafficMgr.hxx>
48 #include <Network/HTTPClient.hxx>
49 #include <Cockpit/cockpitDisplayManager.hxx>
50 #include <GUI/new_gui.hxx>
51 #include <Main/logger.hxx>
52 #include <ATC/atc_mgr.hxx>
53 #include <AIModel/AIManager.hxx>
54 #include <MultiPlayer/multiplaymgr.hxx>
55 #include <AIModel/submodel.hxx>
56 #include <Aircraft/controls.hxx>
57 #include <Input/input.hxx>
58 #include <Aircraft/replay.hxx>
59 #include <Sound/voice.hxx>
60 #include <Canvas/canvas_mgr.hxx>
61 #include <Canvas/gui_mgr.hxx>
62 #include <Time/light.hxx>
63 #include <Viewer/viewmgr.hxx>
64 #include <Model/modelmgr.hxx>
71 SGSubsystem* createSubsystemByName(const std::string& name)
73 #define MAKE_SUB(cl, n) \
74 if (name == n) return new cl;
76 MAKE_SUB(FGControls, "controls");
77 MAKE_SUB(FGSoundManager, "sound");
78 MAKE_SUB(FGInterpolator, "prop-interpolator")
79 MAKE_SUB(FGProperties, "properties");
80 MAKE_SUB(FDMShell, "fdm");
81 MAKE_SUB(FGEnvironmentMgr, "environment");
82 MAKE_SUB(Ephemeris, "ephemeris");
83 MAKE_SUB(FGSystemMgr, "aircraft-systems");
84 MAKE_SUB(FGInstrumentMgr, "instruments");
86 MAKE_SUB(flightgear::CockpitDisplayManager, "cockpit-displays");
88 MAKE_SUB(FGHTTPClient, "http");
89 MAKE_SUB(FGRouteMgr, "route-manager");
90 MAKE_SUB(FGLogger, "logger");
91 MAKE_SUB(NewGUI, "gui");
92 MAKE_SUB(FGATCManager, "atc");
93 MAKE_SUB(FGMultiplayMgr, "mp");
94 MAKE_SUB(FGTrafficManager, "traffic-manager");
95 MAKE_SUB(FGAIManager, "ai-manager");
96 MAKE_SUB(FGSubmodelMgr, "submodel-manager");
97 MAKE_SUB(FGControls, "controls");
98 MAKE_SUB(FGInput, "input");
99 MAKE_SUB(FGReplay, "replay");
100 MAKE_SUB(FGVoiceMgr, "voice");
101 MAKE_SUB(FGLight, "lighting");
102 MAKE_SUB(CanvasMgr, "canvas");
103 MAKE_SUB(GUIMgr, "canvas-gui");
104 MAKE_SUB(FGViewMgr, "view-manager");
105 MAKE_SUB(FGModelMgr, "model-manager");
108 throw sg_range_exception("unknown subsystem:" + name);
111 SGSubsystemMgr::GroupType mapGroupNameToType(const std::string& s)
113 if (s == "init") return SGSubsystemMgr::INIT;
114 if (s == "general") return SGSubsystemMgr::GENERAL;
115 if (s == "fdm") return SGSubsystemMgr::FDM;
116 if (s == "post-fdm") return SGSubsystemMgr::POST_FDM;
117 if (s == "display") return SGSubsystemMgr::DISPLAY;
118 if (s == "sound") return SGSubsystemMgr::SOUND;
120 SG_LOG(SG_GENERAL, SG_ALERT, "unrecognized subsystem group:" << s);
121 return SGSubsystemMgr::GENERAL;
125 do_add_subsystem (const SGPropertyNode * arg)
127 std::string subsystem(arg->getStringValue("subsystem"));
128 std::string name = arg->getStringValue("name");
129 if (subsystem.empty()) {
130 SG_LOG(SG_GENERAL, SG_ALERT, "do_add_subsystem:"
131 << "no subsystem/name supplied");
137 // default name is simply the subsytem's name
141 if (globals->get_subsystem_mgr()->get_subsystem(name)) {
142 SG_LOG(SG_GENERAL, SG_ALERT, "do_add_subsystem:"
143 << "duplicate subsystem name:" << name);
147 std::string groupname = arg->getStringValue("group");
148 SGSubsystemMgr::GroupType group = SGSubsystemMgr::GENERAL;
149 if (!groupname.empty()) {
150 group = mapGroupNameToType(groupname);
153 SGSubsystem* instance = NULL;
155 instance = createSubsystemByName(subsystem);
156 } catch (sg_exception& e) {
157 SG_LOG(SG_GENERAL, SG_ALERT, "subsystem creation failed:" <<
158 name << ":" << e.getFormattedMessage());
162 bool doInit = arg->getBoolValue("do-bind-init", false);
168 double minTime = arg->getDoubleValue("min-time-sec", 0.0);
169 globals->get_subsystem_mgr()->add(name.c_str(), instance,
175 static bool do_remove_subsystem(const SGPropertyNode * arg)
177 std::string name = arg->getStringValue("subsystem");
179 SGSubsystem* instance = globals->get_subsystem_mgr()->get_subsystem(name);
181 SG_LOG(SG_GENERAL, SG_ALERT, "do_remove_subsystem: unknown subsytem:" << name);
185 // is it safe to always call these? let's assume so!
186 instance->shutdown();
189 // unplug from the manager
190 globals->get_subsystem_mgr()->remove(name.c_str());
192 // and finally kill off the instance.
199 * Built-in command: reinitialize one or more subsystems.
201 * subsystem[*]: the name(s) of the subsystem(s) to reinitialize; if
202 * none is specified, reinitialize all of them.
205 do_reinit (const SGPropertyNode * arg)
209 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
210 if (subsystems.empty()) {
211 globals->get_subsystem_mgr()->reinit();
213 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
214 const char * name = subsystems[i]->getStringValue();
215 SGSubsystem * subsystem = globals->get_subsystem(name);
216 if (subsystem == 0) {
218 SG_LOG( SG_GENERAL, SG_ALERT,
219 "Subsystem " << name << " not found" );
226 globals->get_event_mgr()->reinit();
232 * Built-in command: suspend one or more subsystems.
234 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
237 do_suspend (const SGPropertyNode * arg)
241 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
242 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
243 const char * name = subsystems[i]->getStringValue();
244 SGSubsystem * subsystem = globals->get_subsystem(name);
245 if (subsystem == 0) {
247 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << " not found");
249 subsystem->suspend();
256 * Built-in command: suspend one or more subsystems.
258 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
261 do_resume (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");
281 SGCommandMgr::command_t command;
283 { "add-subsystem", do_add_subsystem },
284 { "remove-subsystem", do_remove_subsystem },
285 { "reinit", do_reinit },
286 { "suspend", do_suspend },
287 { "resume", do_resume },
288 { 0, 0 } // zero-terminated
291 void registerSubsystemCommands(SGCommandMgr* cmdMgr)
293 for (int i = 0; built_ins[i].name != 0; i++) {
294 cmdMgr->addCommand(built_ins[i].name, built_ins[i].command);
298 } // of namepace flightgear