Debian Codegraph @

src/Main/fg_scene_commands.cxx

raw symbols SCIP
1 /*
2 * SPDX-FileName: fg_scene_commands.cxx
3 * SPDX-FileComment: internal FGFS commands
4 */
5
6 #include "config.h"
7
8 #include <string.h> // strcmp()
9
10 #include <simgear/compiler.h>
11
12 #include <string>
13 #include <fstream>
14
15 #include <simgear/sg_inlines.h>
16 #include <simgear/debug/logstream.hxx>
17 #include <simgear/math/sg_random.hxx>
18 #include <simgear/io/iostreams/sgstream.hxx>
19 #include <simgear/scene/material/mat.hxx>
20 #include <simgear/scene/material/matlib.hxx>
21 #include <simgear/structure/exception.hxx>
22 #include <simgear/structure/commands.hxx>
23 #include <simgear/props/props.hxx>
24 #include <simgear/props/props_io.hxx>
25 #include <simgear/structure/event_mgr.hxx>
26 #include <simgear/sound/soundmgr.hxx>
27 #include <simgear/timing/sg_time.hxx>
28 #include <Network/RemoteXMLRequest.hxx>
29
30 #include <FDM/flight.hxx>
31 #include <GUI/gui.h>
32 #include <GUI/new_gui.hxx>
33 #include <GUI/dialog.hxx>
34 #include <Aircraft/replay.hxx>
35 #include <Scenery/scenery.hxx>
36 #include <Scripting/NasalSys.hxx>
37 #include <Sound/sample_queue.hxx>
38 #include <Airports/xmlloader.hxx>
39 #include <Network/HTTPClient.hxx>
40 #include <Viewer/CameraGroup.hxx>
41 #include <Viewer/viewmgr.hxx>
42 #include <Viewer/view.hxx>
43 #include <Environment/presets.hxx>
44 #include <Navaids/NavDataCache.hxx>
45
46 #include "fg_init.hxx"
47 #include "fg_io.hxx"
48 #include "fg_os.hxx"
49 #include "fg_commands.hxx"
50 #include "fg_props.hxx"
51 #include "globals.hxx"
52 #include "logger.hxx"
53 #include "util.hxx"
54 #include "main.hxx"
55 #include "positioninit.hxx"
56
57 #if FG_HAVE_GPERFTOOLS
58 # include <google/profiler.h>
59 #endif
60
61 #if defined(HAVE_QT)
62 #include <GUI/QtLauncher.hxx>
63 #endif
64
65
66 ////////////////////////////////////////////////////////////////////////
67 // Command implementations.
68 ////////////////////////////////////////////////////////////////////////
69
70 /**
71 * Built-in command: exit FlightGear.
72 *
73 * status: the exit status to return to the operating system (defaults to 0)
74 */
75 static bool
76 do_exit (const SGPropertyNode * arg, SGPropertyNode * root)
77 {
78 SG_LOG(SG_INPUT, SG_INFO, "Program exit requested.");
79 fgSetBool("/sim/signals/exit", true);
80 globals->saveUserSettings();
81 fgOSExit(arg->getIntValue("status", 0));
82 return true;
83 }
84
85
86 /**
87 * Reset FlightGear (Shift-Escape or Menu->File->Reset)
88 */
89 static bool
90 do_reset (const SGPropertyNode * arg, SGPropertyNode * root)
91 {
92 fgResetIdleState();
93 return true;
94 }
95
96
97 /**
98 * Change aircraft
99 */
100 static bool
101 do_switch_aircraft (const SGPropertyNode * arg, SGPropertyNode * root)
102 {
103 fgSetString("/sim/aircraft", arg->getStringValue("aircraft"));
104
105 // we could validate the new aircraft is find-able, and if not, bail out
106 // of the command at this point? Might give a better user experience?
107 // would need something like bool fgIsValidAircraft(name, dir);
108 // which could be cooked up by modifying FindAndCacheAircraft
109
110 // We assume that we are changing the aircraft, so we remove the aircraft path.
111 // this forces aircraft-dir to be recomputed by a full search as the user hopefully
112 // expects.
113 // We could allow pasing the dir as an optional argument to this command, if that
114 // is ever needed in the future.
115 fgGetNode("/sim")->removeChild("aircraft-dir");
116
117 // start a reset
118 fgResetIdleState();
119 return true;
120 }
121
122 /**
123 */
124 static bool
125 do_reposition (const SGPropertyNode * arg, SGPropertyNode * root)
126 {
127 fgStartReposition();
128 return true;
129 }
130
131 /**
132 * Built-in command: (re)load the panel.
133 *
134 * path (optional): the file name to load the panel from
135 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
136 * and if that's unspecified, to "Panels/Default/default.xml".
137 */
138 static bool
139 do_panel_load (const SGPropertyNode * arg, SGPropertyNode * root)
140 {
141 std::string panel_path = arg->getStringValue("path");
142 if (!panel_path.empty()) {
143 // write to the standard property, which will force a load
144 fgSetString("/sim/panel/path", panel_path.c_str());
145 }
146
147 return true;
148 }
149
150 /**
151 * Built-in command: (re)load preferences.
152 *
153 * path (optional): the file name to load the panel from (relative
154 * to FG_ROOT). Defaults to "preferences.xml".
155 */
156 static bool
157 do_preferences_load (const SGPropertyNode * arg, SGPropertyNode * root)
158 {
159 // disabling this command which was formerly used to reload 'preferences.xml'
160 // reloading the defaults doesn't make sense (better to reset the simulator),
161 // and loading arbitrary XML docs into the property-tree can be done via
162 // the standard load-xml command.
163 SG_LOG(SG_GENERAL, SG_ALERT, "preferences-load command is deprecated and non-functional");
164 return false;
165 }
166
167 /**
168 * An fgcommand to toggle fullscreen mode.
169 * No parameters.
170 */
171 static bool
172 do_toggle_fullscreen(const SGPropertyNode*, SGPropertyNode*)
173 {
174 fgOSFullScreen();
175 return true;
176 }
177
178 /**
179 * Built-in command: capture screen.
180 */
181 static bool
182 do_screen_capture(const SGPropertyNode*, SGPropertyNode*)
183 {
184 return fgDumpSnapShot();
185 }
186
187 static bool
188 do_reload_shaders (const SGPropertyNode*, SGPropertyNode*)
189 {
190 simgear::reload_shaders();
191 return true;
192 }
193
194 static bool
195 do_dump_scene_graph (const SGPropertyNode*, SGPropertyNode*)
196 {
197 fgDumpSceneGraph();
198 return true;
199 }
200
201 static bool
202 do_dump_terrain_branch (const SGPropertyNode*, SGPropertyNode*)
203 {
204 fgDumpTerrainBranch();
205
206 double lon_deg = fgGetDouble("/position/longitude-deg");
207 double lat_deg = fgGetDouble("/position/latitude-deg");
208 SGGeod geodPos = SGGeod::fromDegFt(lon_deg, lat_deg, 0.0);
209 SGVec3d zero = SGVec3d::fromGeod(geodPos);
210
211 SG_LOG(SG_INPUT, SG_INFO, "Model parameters:");
212 SG_LOG(SG_INPUT, SG_INFO, "Center: " << zero.x() << ", " << zero.y() << ", " << zero.z() );
213 SG_LOG(SG_INPUT, SG_INFO, "Rotation: " << lat_deg << ", " << lon_deg );
214
215 return true;
216 }
217
218 static bool
219 do_print_visible_scene_info(const SGPropertyNode*, SGPropertyNode*)
220 {
221 fgPrintVisibleSceneInfoCommand();
222 return true;
223 }
224
225 /**
226 * Built-in command: hires capture screen.
227 */
228 static bool
229 do_hires_screen_capture (const SGPropertyNode * arg, SGPropertyNode * root)
230 {
231 fgHiResDump();
232 return true;
233 }
234
235 /**
236 * Reload all Compositor instances in the default CameraGroup.
237 */
238 static bool
239 do_reload_compositor(const SGPropertyNode*, SGPropertyNode*)
240 {
241 flightgear::reloadCompositors(flightgear::CameraGroup::getDefault());
242 return true;
243 }
244
245 /**
246 * Reload the tile cache.
247 */
248 static bool
249 do_tile_cache_reload (const SGPropertyNode * arg, SGPropertyNode * root)
250 {
251 SGPropertyNode *master_freeze = fgGetNode("/sim/freeze/master");
252 bool freeze = master_freeze->getBoolValue();
253 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
254 if ( !freeze ) {
255 master_freeze->setBoolValue(true);
256 }
257
258 globals->get_subsystem<FGScenery>()->reinit();
259
260 if ( !freeze ) {
261 master_freeze->setBoolValue(false);
262 }
263 return true;
264 }
265
266 /**
267 * Reload the materials definition
268 */
269 static bool
270 do_materials_reload (const SGPropertyNode * arg, SGPropertyNode * root)
271 {
272 SG_LOG(SG_INPUT, SG_INFO, "Reloading Materials");
273 SGMaterialLib* new_matlib = new SGMaterialLib;
274 SGPath mpath( globals->get_fg_root() );
275 mpath.append( fgGetString("/sim/rendering/materials-file") );
276 bool loaded = new_matlib->load(globals->get_fg_root(),
277 mpath,
278 globals->get_props());
279
280 if ( ! loaded ) {
281 SG_LOG( SG_GENERAL, SG_ALERT,
282 "Error loading materials file " << mpath );
283 return false;
284 }
285
286 globals->set_matlib(new_matlib);
287 FGScenery* scenery = static_cast<FGScenery*>(globals->get_scenery());
288 scenery->materialLibChanged();
289
290 return true;
291 }
292
293 /**
294 * Built-in command: Add a dialog to the GUI system. Does *not*
295 * display the dialog. The property node should have the same format
296 * as a dialog XML configuration. It must include:
297 *
298 * name: the name of the GUI dialog for future reference.
299 */
300 static bool
301 do_dialog_new (const SGPropertyNode * arg, SGPropertyNode * root)
302 {
303 auto gui = globals->get_subsystem<NewGUI>();
304 if (!gui) {
305 return false;
306 }
307
308 // Note the casting away of const: this is *real*. Doing a
309 // "dialog-apply" command later on will mutate this property node.
310 // I'm not convinced that this isn't the Right Thing though; it
311 // allows client to create a node, pass it to dialog-new, and get
312 // the values back from the dialog by reading the same node.
313 // Perhaps command arguments are not as "const" as they would
314 // seem?
315 gui->newDialog((SGPropertyNode*)arg);
316 return true;
317 }
318
319 /**
320 * Built-in command: Show an XML-configured dialog.
321 *
322 * dialog-name: the name of the GUI dialog to display.
323 */
324 static bool
325 do_dialog_show (const SGPropertyNode * arg, SGPropertyNode * root)
326 {
327 auto gui = globals->get_subsystem<NewGUI>();
328 gui->showDialog(arg->getStringValue("dialog-name"));
329 return true;
330 }
331
332 /**
333 * Built-in command: Show an XML-configured dialog.
334 *
335 * dialog-name: the name of the GUI dialog to display.
336 */
337 static bool
338 do_dialog_toggle (const SGPropertyNode * arg, SGPropertyNode * root)
339 {
340 auto gui = globals->get_subsystem<NewGUI>();
341 gui->toggleDialog(arg->getStringValue("dialog-name"));
342 return true;
343 }
344
345
346 /**
347 * Built-in Command: Hide the active XML-configured dialog.
348 */
349 static bool
350 do_dialog_close (const SGPropertyNode * arg, SGPropertyNode * root)
351 {
352 auto gui = globals->get_subsystem<NewGUI>();
353 if(arg->hasValue("dialog-name"))
354 return gui->closeDialog(arg->getStringValue("dialog-name"));
355 return gui->closeActiveDialog();
356 }
357
358
359 /**
360 * Update a value in the active XML-configured dialog.
361 *
362 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
363 */
364 static bool
365 do_dialog_update (const SGPropertyNode * arg, SGPropertyNode * root)
366 {
367 auto gui = globals->get_subsystem<NewGUI>();
368 FGDialog * dialog;
369 if (arg->hasValue("dialog-name"))
370 dialog = gui->getDialog(arg->getStringValue("dialog-name"));
371 else
372 dialog = gui->getActiveDialog();
373
374 if (dialog != 0) {
375 dialog->updateValues(arg->getStringValue("object-name"));
376 return true;
377 } else {
378 return false;
379 }
380 }
381
382 static bool
383 do_open_browser (const SGPropertyNode * arg, SGPropertyNode * root)
384 {
385 std::string path;
386 if (arg->hasValue("path"))
387 path = arg->getStringValue("path");
388 else
389 if (arg->hasValue("url"))
390 path = arg->getStringValue("url");
391 else
392 return false;
393
394 return openBrowser(path);
395 }
396
397 static bool
398 do_open_launcher(const SGPropertyNode*, SGPropertyNode*)
399 {
400 #if defined(HAVE_QT)
401 bool ok = flightgear::runInAppLauncherDialog();
402 if (ok) {
403 // start a full reset
404 fgResetIdleState();
405 }
406 return true; // don't report failure
407 #else
408 return false;
409 #endif
410 }
411
412 /**
413 * Apply a value in the active XML-configured dialog.
414 *
415 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
416 */
417 static bool
418 do_dialog_apply (const SGPropertyNode * arg, SGPropertyNode * root)
419 {
420 auto gui = globals->get_subsystem<NewGUI>();
421 FGDialog* dialog = nullptr;
422 if (arg->hasValue("dialog-name"))
423 dialog = gui->getDialog(arg->getStringValue("dialog-name"));
424 else
425 dialog = gui->getActiveDialog();
426
427 if (dialog) {
428 dialog->applyValues(arg->getStringValue("object-name"));
429 return true;
430 } else {
431 return false;
432 }
433 }
434
435
436 /**
437 * Redraw GUI (applying new widget colors). Doesn't reload the dialogs,
438 * unlike reinit().
439 */
440 static bool
441 do_gui_redraw (const SGPropertyNode * arg, SGPropertyNode * root)
442 {
443 auto gui = globals->get_subsystem<NewGUI>();
444 gui->redraw();
445 return true;
446 }
447
448
449 /**
450 * Adds model to the scenery. The path to the added branch (/models/model[*])
451 * is returned in property "property".
452 */
453 static bool
454 do_add_model (const SGPropertyNode * arg, SGPropertyNode * root)
455 {
456 SGPropertyNode * model = fgGetNode("models", true);
457 int i;
458 for (i = 0; model->hasChild("model",i); i++);
459 model = model->getChild("model", i, true);
460 copyProperties(arg, model);
461 if (model->hasValue("elevation-m"))
462 model->setDoubleValue("elevation-ft", model->getDoubleValue("elevation-m")
463 * SG_METER_TO_FEET);
464 model->getNode("load", true);
465 model->removeChildren("load");
466 const_cast<SGPropertyNode *>(arg)->setStringValue("property", model->getPath());
467 return true;
468 }
469
470 /**
471 * Built-in command: commit presets (read from in /sim/presets/)
472 */
473 static bool
474 do_presets_commit (const SGPropertyNode * arg, SGPropertyNode * root)
475 {
476 if (fgGetBool("/sim/initialized", false)) {
477 fgResetIdleState();
478 } else {
479 // Nasal can trigger this during initial init, which confuses
480 // the logic in ReInitSubsystems, since initial state has not been
481 // saved at that time. Short-circuit everything here.
482 flightgear::initPosition();
483 }
484
485 return true;
486 }
487
488 static bool
489 do_press_cockpit_button (const SGPropertyNode * arg, SGPropertyNode * root)
490 {
491 const std::string prefix = arg->getStringValue("prefix");
492
493 if (arg->getBoolValue("guarded") && fgGetDouble((prefix + "-guard").c_str()) < 1)
494 return true;
495
496 std::string prop = prefix + "-button";
497 double value;
498
499 if (arg->getBoolValue("latching"))
500 value = fgGetDouble(prop.c_str()) > 0 ? 0 : 1;
501 else
502 value = 1;
503
504 fgSetDouble(prop.c_str(), value);
505 fgSetBool(arg->getStringValue("discrete"), value > 0);
506
507 return true;
508 }
509
510 static bool
511 do_release_cockpit_button (const SGPropertyNode * arg, SGPropertyNode * root)
512 {
513 const std::string prefix = arg->getStringValue("prefix");
514
515 if (arg->getBoolValue("guarded")) {
516 std::string prop = prefix + "-guard";
517 if (fgGetDouble(prop.c_str()) < 1) {
518 fgSetDouble(prop.c_str(), 1);
519 return true;
520 }
521 }
522
523 if (! arg->getBoolValue("latching")) {
524 fgSetDouble((prefix + "-button").c_str(), 0);
525 fgSetBool(arg->getStringValue("discrete"), false);
526 }
527
528 return true;
529 }
530
531 ////////////////////////////////////////////////////////////////////////
532 // Command setup.
533 ////////////////////////////////////////////////////////////////////////
534
535
536 /**
537 * Table of built-in commands.
538 *
539 * New commands do not have to be added here; any module in the application
540 * can add a new command using globals->get_commands()->addCommand(...).
541 */
542 static struct {
543 const char * name;
544 SGCommandMgr::command_t command;
545 } built_ins [] = {
546 { "exit", do_exit },
547 { "reset", do_reset },
548 { "reposition", do_reposition },
549 { "switch-aircraft", do_switch_aircraft },
550 { "panel-load", do_panel_load },
551 { "preferences-load", do_preferences_load },
552 { "toggle-fullscreen", do_toggle_fullscreen },
553 { "screen-capture", do_screen_capture },
554 { "hires-screen-capture", do_hires_screen_capture },
555 { "tile-cache-reload", do_tile_cache_reload },
556 { "dialog-new", do_dialog_new },
557 { "dialog-show", do_dialog_show },
558 { "dialog-toggle", do_dialog_toggle },
559 { "dialog-close", do_dialog_close },
560 { "dialog-update", do_dialog_update },
561 { "dialog-apply", do_dialog_apply },
562 { "open-browser", do_open_browser },
563 { "gui-redraw", do_gui_redraw },
564 { "add-model", do_add_model },
565 { "presets-commit", do_presets_commit },
566 { "press-cockpit-button", do_press_cockpit_button },
567 { "release-cockpit-button", do_release_cockpit_button },
568 { "dump-scenegraph", do_dump_scene_graph },
569 { "dump-terrainbranch", do_dump_terrain_branch },
570 { "print-visible-scene", do_print_visible_scene_info },
571 { "reload-shaders", do_reload_shaders },
572 { "reload-materials", do_materials_reload },
573 { "reload-compositor", do_reload_compositor },
574 { "open-launcher", do_open_launcher },
575 { 0, 0 } // zero-terminated
576 };
577
578
579 /**
580 * Initialize the default built-in commands.
581 *
582 * Other commands may be added by other parts of the application.
583 */
584 void
585 fgInitSceneCommands ()
586 {
587 SG_LOG(SG_GENERAL, SG_BULK, "Initializing scene built-in commands:");
588 for (int i = 0; built_ins[i].name != 0; i++) {
589 SG_LOG(SG_GENERAL, SG_BULK, " " << built_ins[i].name);
590 globals->get_commands()->addCommand(built_ins[i].name,
591 built_ins[i].command);
592 }
593
594 }