]> git.mxchange.org Git - flightgear.git/blob - src/Main/subsystemFactory.cxx
A new comm radio and atis implementation
[flightgear.git] / src / Main / subsystemFactory.cxx
1 // subsystemFactory.cxx - factory for subsystems
2 //
3 // Copyright (C) 2012 James Turner  zakalawe@mac.com
4 //
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.
9 //
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.
14 //
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.
18
19 #ifdef HAVE_CONFIG_H
20     #include "config.h"
21 #endif
22
23 #include "subsystemFactory.hxx"
24
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>
30
31 #include <Main/globals.hxx>
32 #include <Sound/soundmanager.hxx>
33
34 // subsystem includes
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>
65
66 using std::vector;
67
68 namespace flightgear
69 {
70   
71 SGSubsystem* createSubsystemByName(const std::string& name)
72 {
73 #define MAKE_SUB(cl, n) \
74     if (name == n) return new cl;
75     
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");
85     MAKE_SUB(HUD, "hud");
86     MAKE_SUB(flightgear::CockpitDisplayManager, "cockpit-displays");
87     MAKE_SUB(FGIO, "io");
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");
106 #undef MAKE_SUB
107     
108     throw sg_range_exception("unknown subsystem:" + name);
109 }
110
111 SGSubsystemMgr::GroupType mapGroupNameToType(const std::string& s)
112 {
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;
119     
120     SG_LOG(SG_GENERAL, SG_ALERT, "unrecognized subsystem group:" << s);
121     return SGSubsystemMgr::GENERAL;
122 }
123
124 static bool
125 do_add_subsystem (const SGPropertyNode * arg)
126 {
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");
132         return false;
133     }
134     
135   
136     if (name.empty()) {
137         // default name is simply the subsytem's name
138         name =  subsystem;
139     }
140   
141     if (globals->get_subsystem_mgr()->get_subsystem(name)) {
142         SG_LOG(SG_GENERAL, SG_ALERT, "do_add_subsystem:" 
143             << "duplicate subsystem name:" << name);
144       return false;
145     }
146     
147     std::string groupname = arg->getStringValue("group");
148     SGSubsystemMgr::GroupType group = SGSubsystemMgr::GENERAL;
149     if (!groupname.empty()) {
150         group = mapGroupNameToType(groupname);
151     }
152     
153     SGSubsystem* instance = NULL;
154     try {
155         instance = createSubsystemByName(subsystem);
156     } catch (sg_exception& e) {
157         SG_LOG(SG_GENERAL, SG_ALERT, "subsystem creation failed:" <<
158             name << ":" << e.getFormattedMessage());
159         return false;
160     }
161     
162     bool doInit = arg->getBoolValue("do-bind-init", false);
163     if (doInit) {
164         instance->bind();
165         instance->init();
166     }
167     
168     double minTime = arg->getDoubleValue("min-time-sec", 0.0);
169     globals->get_subsystem_mgr()->add(name.c_str(), instance,
170         group, minTime);
171     
172     return true;
173 }
174
175 static bool do_remove_subsystem(const SGPropertyNode * arg)
176 {
177   std::string name = arg->getStringValue("subsystem");
178   
179   SGSubsystem* instance = globals->get_subsystem_mgr()->get_subsystem(name);
180   if (!instance) {
181     SG_LOG(SG_GENERAL, SG_ALERT, "do_remove_subsystem: unknown subsytem:" << name);
182     return false;
183   }
184   
185   // is it safe to always call these? let's assume so!
186   instance->shutdown();
187   instance->unbind();
188
189   // unplug from the manager
190   globals->get_subsystem_mgr()->remove(name.c_str());
191   
192   // and finally kill off the instance.
193   delete instance;
194   
195   return true;
196 }
197   
198 /**
199  * Built-in command: reinitialize one or more subsystems.
200  *
201  * subsystem[*]: the name(s) of the subsystem(s) to reinitialize; if
202  * none is specified, reinitialize all of them.
203  */
204 static bool
205 do_reinit (const SGPropertyNode * arg)
206 {
207     bool result = true;
208
209     vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
210     if (subsystems.empty()) {
211         globals->get_subsystem_mgr()->reinit();
212     } else {
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) {
217                 result = false;
218                 SG_LOG( SG_GENERAL, SG_ALERT,
219                         "Subsystem " << name << " not found" );
220             } else {
221                 subsystem->reinit();
222             }
223         }
224     }
225
226     globals->get_event_mgr()->reinit();
227
228     return result;
229 }
230
231 /**
232  * Built-in command: suspend one or more subsystems.
233  *
234  * subsystem[*] - the name(s) of the subsystem(s) to suspend.
235  */
236 static bool
237 do_suspend (const SGPropertyNode * arg)
238 {
239     bool result = true;
240
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) {
246             result = false;
247             SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << " not found");
248         } else {
249             subsystem->suspend();
250         }
251     }
252     return result;
253 }
254
255 /**
256  * Built-in command: suspend one or more subsystems.
257  *
258  * subsystem[*] - the name(s) of the subsystem(s) to suspend.
259  */
260 static bool
261 do_resume (const SGPropertyNode * arg)
262 {
263     bool result = true;
264
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) {
270             result = false;
271             SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << " not found");
272         } else {
273             subsystem->resume();
274         }
275     }
276     return result;
277 }
278
279 static struct {
280   const char * name;
281   SGCommandMgr::command_t command;
282 } built_ins [] = {
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
289 };
290
291 void registerSubsystemCommands(SGCommandMgr* cmdMgr)
292 {
293     for (int i = 0; built_ins[i].name != 0; i++) {
294       cmdMgr->addCommand(built_ins[i].name, built_ins[i].command);
295     }
296 }
297
298 } // of namepace flightgear