1 /**************************************************************************
4 * Based on gui.cxx and renamed on 2002/08/13 by Erik Hofman.
6 * Written 1998 by Durk Talsma, started Juni, 1998. For the flight gear
9 * Additional mouse supported added by David Megginson, 1999.
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of the
14 * License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 **************************************************************************/
37 #include <simgear/compiler.h>
46 #include <simgear/debug/logstream.hxx>
47 #include <simgear/misc/sg_path.hxx>
48 #include <simgear/screen/screen-dump.hxx>
49 #include <simgear/structure/event_mgr.hxx>
50 #include <simgear/props/props_io.hxx>
52 #include <Cockpit/panel.hxx>
53 #include <Main/globals.hxx>
54 #include <Main/fg_props.hxx>
55 #include <Main/fg_init.hxx> // for platformDesktopPath
56 #include <Main/fg_os.hxx>
57 #include <Viewer/renderer.hxx>
58 #include <Viewer/viewmgr.hxx>
59 #include <Viewer/WindowSystemAdapter.hxx>
60 #include <Viewer/CameraGroup.hxx>
61 #include <GUI/new_gui.hxx>
65 # include <shellapi.h>
69 # include "FGCocoaMenuBar.hxx" // for cocoaOpenUrl
77 #if defined( TR_HIRES_SNAP)
78 #include <simgear/screen/tr.h>
79 extern void fgUpdateHUD( GLfloat x_start, GLfloat y_start,
80 GLfloat x_end, GLfloat y_end );
84 const __fg_gui_fn_t __fg_gui_fn[] = {
86 {"dumpHiResSnapShot", fgHiResDumpWrapper},
88 {"dumpSnapShot", fgDumpSnapShotWrapper},
92 // Structure termination
97 /* ================ General Purpose Functions ================ */
99 // General Purpose Message Box. Makes sure no more than 5 different
100 // messages are displayed at the same time, and none of them are
101 // duplicates. (5 is a *lot*, but this will hardly ever be reached
102 // and we don't want to miss any, either.)
103 void mkDialog (const char *txt)
105 NewGUI *gui = (NewGUI *)globals->get_subsystem("gui");
108 SGPropertyNode *master = gui->getDialogProperties("message");
112 const int maxdialogs = 5;
114 SGPropertyNode *msg = fgGetNode("/sim/gui/dialogs", true);
116 for (i = 0; i < maxdialogs; i++) {
117 std::ostringstream s;
118 s << "message-" << i;
121 if (!msg->getNode(name.c_str(), false))
124 if (!strcmp(txt, msg->getNode(name.c_str())->getStringValue("message"))) {
125 SG_LOG(SG_GENERAL, SG_WARN, "mkDialog(): duplicate of message " << txt);
131 msg = msg->getNode(name.c_str(), true);
132 msg->setStringValue("message", txt);
133 msg = msg->getNode("dialog", true);
134 copyProperties(master, msg);
135 msg->setStringValue("name", name.c_str());
137 gui->showDialog(name.c_str());
140 // Message Box to report an error.
141 void guiErrorMessage (const char *txt)
143 SG_LOG(SG_GENERAL, SG_ALERT, txt);
147 // Message Box to report a throwable (usually an exception).
148 void guiErrorMessage (const char *txt, const sg_throwable &throwable)
152 msg += throwable.getFormattedMessage();
153 if (!std::strlen(throwable.getOrigin()) != 0) {
154 msg += "\n (reported by ";
155 msg += throwable.getOrigin();
158 SG_LOG(SG_GENERAL, SG_ALERT, msg);
159 mkDialog(msg.c_str());
164 /* -----------------------------------------------------------------------
165 the Gui callback functions
166 ____________________________________________________________________*/
170 openBrowser( "Docs/index.html" );
173 bool openBrowser(const std::string& aAddress)
176 string address(aAddress);
178 // do not resolve addresses with given protocol, i.e. "http://...", "ftp://..."
179 if (address.find("://")==string::npos)
181 // resolve local file path
182 SGPath path(address);
183 path = globals->resolve_maybe_aircraft_path(address);
185 address = path.str();
188 mkDialog ("Sorry, file not found!");
189 SG_LOG(SG_GENERAL, SG_ALERT, "openBrowser: Cannot find requested file '"
196 if (address.find("://")==string::npos) {
197 address = "file://" + address;
200 cocoaOpenUrl(address);
203 // Look for favorite browser
204 char win32_name[1024];
206 cygwin32_conv_to_full_win32_path(address.c_str(),win32_name);
208 strncpy(win32_name,address.c_str(), 1024);
210 ShellExecute ( NULL, "open", win32_name, NULL, NULL,
213 // Linux, BSD, SGI etc
214 string command = globals->get_browser();
215 string::size_type pos;
216 if ((pos = command.find("%u", 0)) != string::npos)
217 command.replace(pos, 2, address);
219 command += " \"" + address +"\"";
222 ok = (system( command.c_str() ) == 0);
225 mkDialog("The file is shown in your web browser window.");
229 #if defined( TR_HIRES_SNAP)
234 bool menu_status = fgGetBool("/sim/menubar/visibility");
235 char *filename = new char [24];
236 static int count = 1;
238 SGPropertyNode *master_freeze = fgGetNode("/sim/freeze/master");
240 bool freeze = master_freeze->getBoolValue();
242 master_freeze->setBoolValue(true);
245 fgSetBool("/sim/menubar/visibility", false);
246 int mouse = fgGetMouseCursor();
247 fgSetMouseCursor(MOUSE_CURSOR_NONE);
249 FGRenderer *renderer = globals->get_renderer();
251 renderer->resize( fgGetInt("/sim/startup/xsize"),
252 fgGetInt("/sim/startup/ysize") );
254 // we need two render frames here to clear the menu and cursor
255 // ... not sure why but doing an extra fgRenderFrame() shouldn't
257 //renderer->update( true );
258 //renderer->update( true );
260 // This ImageSize stuff is a temporary hack
261 // should probably use 128x128 tile size and
262 // support any image size
264 // This should be a requester to get multiplier from user
265 int multiplier = fgGetInt("/sim/startup/hires-multiplier", 3);
266 int width = fgGetInt("/sim/startup/xsize");
267 int height = fgGetInt("/sim/startup/ysize");
269 /* allocate buffer large enough to store one tile */
270 GLubyte *tile = (GLubyte *)malloc(width * height * 3 * sizeof(GLubyte));
273 printf("Malloc of tile buffer failed!\n");
277 int imageWidth = multiplier*width;
278 int imageHeight = multiplier*height;
280 /* allocate buffer to hold a row of tiles */
282 = (GLubyte *)malloc(imageWidth * height * 3 * sizeof(GLubyte));
286 printf("Malloc of tile row buffer failed!\n");
289 TRcontext *tr = trNew();
290 trTileSize(tr, width, height, 0);
291 trTileBuffer(tr, GL_RGB, GL_UNSIGNED_BYTE, tile);
292 trImageSize(tr, imageWidth, imageHeight);
293 trRowOrder(tr, TR_TOP_TO_BOTTOM);
295 // sgFrustum *frustum = ssgGetFrustum();
297 // frustum->getLeft(), frustum->getRight(),
298 // frustum->getBot(), frustum->getTop(),
299 // frustum->getNear(), frustum->getFar());
301 /* Prepare ppm output file */
302 while (count < 1000) {
303 snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
304 if ( (f = fopen(filename, "r")) == NULL )
308 f = fopen(filename, "wb");
310 printf("Couldn't open image file: %s\n", filename);
317 fprintf(f,"# ppm-file created by %s\n", "trdemo2");
318 fprintf(f,"%i %i\n", imageWidth, imageHeight);
321 /* just to be safe... */
322 glPixelStorei(GL_PACK_ALIGNMENT, 1);
326 /* Because the HUD and Panel change the ViewPort we will
327 * need to handle some lowlevel stuff ourselves */
328 int ncols = trGet(tr, TR_COLUMNS);
329 int nrows = trGet(tr, TR_ROWS);
331 bool do_hud = fgGetBool("/sim/hud/visibility");
332 GLfloat hud_col_step = 640.0 / ncols;
333 GLfloat hud_row_step = 480.0 / nrows;
335 bool do_panel = fgPanelVisible();
336 GLfloat panel_col_step = globals->get_current_panel()->getWidth() / ncols;
337 GLfloat panel_row_step = globals->get_current_panel()->getHeight() / nrows;
339 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
340 glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
341 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
342 glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
343 glHint(GL_FOG_HINT, GL_NICEST);
349 int curColumn = trGet(tr, TR_CURRENT_COLUMN);
350 // int curRow = trGet(tr, TR_CURRENT_ROW);
355 // fgUpdateHUD( curColumn*hud_col_step, curRow*hud_row_step,
356 // (curColumn+1)*hud_col_step, (curRow+1)*hud_row_step );
359 // globals->get_current_panel()->update(
360 // curColumn*panel_col_step, panel_col_step,
361 // curRow*panel_row_step, panel_row_step );
362 more = trEndTile(tr);
364 /* save tile into tile row buffer*/
365 int curTileWidth = trGet(tr, TR_CURRENT_TILE_WIDTH);
366 int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
367 int bytesPerTileRow = (width) * 3*sizeof(GLubyte);
368 int xOffset = curColumn * bytesPerTileRow;
369 int bytesPerCurrentTileRow = (curTileWidth) * 3*sizeof(GLubyte);
371 for (i=0;i<height;i++) {
372 memcpy(buffer + i*bytesPerImageRow + xOffset, /* Dest */
373 tile + i*bytesPerTileRow, /* Src */
374 bytesPerCurrentTileRow); /* Byte count*/
377 if (curColumn == trGet(tr, TR_COLUMNS)-1) {
378 /* write this buffered row of tiles to the file */
379 int curTileHeight = trGet(tr, TR_CURRENT_TILE_HEIGHT);
380 int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
382 for (i=0;i<curTileHeight;i++) {
383 /* Remember, OpenGL images are bottom to top. Have to reverse. */
384 GLubyte *rowPtr = buffer + (curTileHeight-1-i) * bytesPerImageRow;
385 fwrite(rowPtr, 1, imageWidth*3, f);
391 renderer->resize( width, height );
395 glHint(GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE);
396 glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
397 glHint(GL_POINT_SMOOTH_HINT, GL_DONT_CARE);
398 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_DONT_CARE);
399 if ( (!strcmp(fgGetString("/sim/rendering/fog"), "disabled")) ||
400 (!fgGetBool("/sim/rendering/shading"))) {
401 // if fastest fog requested, or if flat shading force fastest
402 glHint ( GL_FOG_HINT, GL_FASTEST );
403 } else if ( !strcmp(fgGetString("/sim/rendering/fog"), "nicest") ) {
404 glHint ( GL_FOG_HINT, GL_DONT_CARE );
409 message = "Snapshot saved to \"";
412 mkDialog (message.c_str());
419 fgSetMouseCursor(mouse);
420 fgSetBool("/sim/menubar/visibility", menu_status);
423 master_freeze->setBoolValue(false);
426 #endif // #if defined( TR_HIRES_SNAP)
428 void fgDumpSnapShotWrapper () {
433 void fgHiResDumpWrapper () {
439 using namespace flightgear;
441 class GUISnapShotOperation :
442 public GraphicsContextOperation
446 // start new snap shot
449 // allow only one snapshot at a time
450 if (_snapShotOp.valid())
452 _snapShotOp = new GUISnapShotOperation();
453 /* register with graphics context so actual snap shot is done
454 * in the graphics context (thread) */
455 osg::Camera* guiCamera = getGUICamera(CameraGroup::getDefault());
456 WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA();
457 osg::GraphicsContext* gc = 0;
459 gc = guiCamera->getGraphicsContext();
461 gc->add(_snapShotOp.get());
463 wsa->windows[0]->gc->add(_snapShotOp.get());
469 // constructor to be executed in main loop's thread
470 GUISnapShotOperation() :
471 flightgear::GraphicsContextOperation(std::string("GUI snap shot")),
472 _master_freeze(fgGetNode("/sim/freeze/master", true)),
473 _freeze(_master_freeze->getBoolValue()),
475 _mouse(fgGetMouseCursor())
478 _master_freeze->setBoolValue(true);
480 fgSetMouseCursor(MOUSE_CURSOR_NONE);
482 string dir = fgGetString("/sim/paths/screenshot-dir");
484 dir = platformDesktopPath().str();
486 _path.set(dir + '/');
487 if (_path.create_dir( 0755 )) {
488 SG_LOG(SG_GENERAL, SG_ALERT, "Cannot create screenshot directory '"
489 << dir << "'. Trying home directory.");
490 dir = globals->get_fg_home();
494 static int count = 1;
495 while (count < 1000) {
496 snprintf(filename, 24, "fgfs-screen-%03d.png", count++);
506 _xsize = fgGetInt("/sim/startup/xsize");
507 _ysize = fgGetInt("/sim/startup/ysize");
509 FGRenderer *renderer = globals->get_renderer();
510 renderer->resize(_xsize, _ysize);
511 globals->get_event_mgr()->addTask("SnapShotTimer",
512 this, &GUISnapShotOperation::timerExpired,
516 // to be executed in graphics context (maybe separate thread)
517 void run(osg::GraphicsContext* gc)
519 _result = sg_glDumpWindow(_path.c_str(),
524 // timer method, to be executed in main loop's thread
525 virtual void timerExpired()
529 globals->get_event_mgr()->removeTask("SnapShotTimer");
531 fgSetString("/sim/paths/screenshot-last", _path.c_str());
532 fgSetBool("/sim/signals/screenshot", _result);
534 fgSetMouseCursor(_mouse);
537 _master_freeze->setBoolValue(false);
543 static osg::ref_ptr<GUISnapShotOperation> _snapShotOp;
544 SGPropertyNode_ptr _master_freeze;
554 osg::ref_ptr<GUISnapShotOperation> GUISnapShotOperation::_snapShotOp;
556 // do a screen snap shot
557 bool fgDumpSnapShot ()
560 // start snap shot operation, while needs to be executed in
562 return GUISnapShotOperation::start();
564 // obsolete code => remove when new code is stable
565 SGPropertyNode_ptr master_freeze = fgGetNode("/sim/freeze/master");
567 bool freeze = master_freeze->getBoolValue();
569 master_freeze->setBoolValue(true);
572 int mouse = fgGetMouseCursor();
573 fgSetMouseCursor(MOUSE_CURSOR_NONE);
575 fgSetBool("/sim/signals/screenshot", true);
577 FGRenderer *renderer = globals->get_renderer();
578 renderer->resize( fgGetInt("/sim/startup/xsize"),
579 fgGetInt("/sim/startup/ysize") );
581 // we need two render frames here to clear the menu and cursor
582 // ... not sure why but doing an extra fgRenderFrame() shouldn't
584 renderer->update( true );
585 renderer->update( true );
587 string dir = fgGetString("/sim/paths/screenshot-dir");
589 dir = fgGetString("/sim/fg-current");
591 SGPath path(dir + '/');
592 if (path.create_dir( 0755 )) {
593 SG_LOG(SG_GENERAL, SG_ALERT, "Cannot create screenshot directory '"
594 << dir << "'. Trying home directory.");
595 dir = globals->get_fg_home();
599 static int count = 1;
600 while (count < 1000) {
601 snprintf(filename, 24, "fgfs-screen-%03d.png", count++);
611 bool result = sg_glDumpWindow(path.c_str(),
612 fgGetInt("/sim/startup/xsize"),
613 fgGetInt("/sim/startup/ysize"));
615 fgSetString("/sim/paths/screenshot-last", path.c_str());
616 fgSetBool("/sim/signals/screenshot", false);
618 fgSetMouseCursor(mouse);
621 master_freeze->setBoolValue(false);
627 // do an entire scenegraph dump
628 void fgDumpSceneGraph()
630 char *filename = new char [24];
632 static int count = 1;
634 SGPropertyNode *master_freeze = fgGetNode("/sim/freeze/master");
636 bool freeze = master_freeze->getBoolValue();
638 master_freeze->setBoolValue(true);
641 while (count < 1000) {
643 snprintf(filename, 24, "fgfs-graph-%03d.osg", count++);
644 if ( (fp = fopen(filename, "r")) == NULL )
649 if ( fgDumpSceneGraphToFile(filename)) {
650 message = "Entire scene graph saved to \"";
654 message = "Failed to save to \"";
659 mkDialog (message.c_str());
664 master_freeze->setBoolValue(false);
669 // do an terrain branch dump
670 void fgDumpTerrainBranch()
672 char *filename = new char [24];
674 static int count = 1;
676 SGPropertyNode *master_freeze = fgGetNode("/sim/freeze/master");
678 bool freeze = master_freeze->getBoolValue();
680 master_freeze->setBoolValue(true);
683 while (count < 1000) {
685 snprintf(filename, 24, "fgfs-graph-%03d.osg", count++);
686 if ( (fp = fopen(filename, "r")) == NULL )
691 if ( fgDumpTerrainBranchToFile(filename)) {
692 message = "Terrain graph saved to \"";
696 message = "Failed to save to \"";
701 mkDialog (message.c_str());
706 master_freeze->setBoolValue(false);
710 void fgPrintVisibleSceneInfoCommand()
712 SGPropertyNode *master_freeze = fgGetNode("/sim/freeze/master");
714 bool freeze = master_freeze->getBoolValue();
716 master_freeze->setBoolValue(true);
719 flightgear::printVisibleSceneInfo(globals->get_renderer());
722 master_freeze->setBoolValue(false);