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_os.hxx>
56 #include <Viewer/renderer.hxx>
57 #include <Viewer/viewmgr.hxx>
58 #include <Viewer/WindowSystemAdapter.hxx>
59 #include <Viewer/CameraGroup.hxx>
60 #include <GUI/new_gui.hxx>
64 # include <shellapi.h>
68 # include <GUI/CocoaHelpers.h> // for cocoaOpenUrl
76 #if defined( TR_HIRES_SNAP)
77 #include <simgear/screen/tr.h>
78 extern void fgUpdateHUD( GLfloat x_start, GLfloat y_start,
79 GLfloat x_end, GLfloat y_end );
83 const __fg_gui_fn_t __fg_gui_fn[] = {
85 {"dumpHiResSnapShot", fgHiResDumpWrapper},
87 {"dumpSnapShot", fgDumpSnapShotWrapper},
91 // Structure termination
96 /* ================ General Purpose Functions ================ */
98 // General Purpose Message Box. Makes sure no more than 5 different
99 // messages are displayed at the same time, and none of them are
100 // duplicates. (5 is a *lot*, but this will hardly ever be reached
101 // and we don't want to miss any, either.)
102 void mkDialog (const char *txt)
104 NewGUI *gui = (NewGUI *)globals->get_subsystem("gui");
107 SGPropertyNode *master = gui->getDialogProperties("message");
111 const int maxdialogs = 5;
113 SGPropertyNode *msg = fgGetNode("/sim/gui/dialogs", true);
115 for (i = 0; i < maxdialogs; i++) {
116 std::ostringstream s;
117 s << "message-" << i;
120 if (!msg->getNode(name.c_str(), false))
123 if (!strcmp(txt, msg->getNode(name.c_str())->getStringValue("message"))) {
124 SG_LOG(SG_GENERAL, SG_WARN, "mkDialog(): duplicate of message " << txt);
130 msg = msg->getNode(name.c_str(), true);
131 msg->setStringValue("message", txt);
132 msg = msg->getNode("dialog", true);
133 copyProperties(master, msg);
134 msg->setStringValue("name", name.c_str());
136 gui->showDialog(name.c_str());
139 // Message Box to report an error.
140 void guiErrorMessage (const char *txt)
142 SG_LOG(SG_GENERAL, SG_ALERT, txt);
146 // Message Box to report a throwable (usually an exception).
147 void guiErrorMessage (const char *txt, const sg_throwable &throwable)
151 msg += throwable.getFormattedMessage();
152 if (std::strlen(throwable.getOrigin()) != 0) {
153 msg += "\n (reported by ";
154 msg += throwable.getOrigin();
157 SG_LOG(SG_GENERAL, SG_ALERT, msg);
158 mkDialog(msg.c_str());
163 /* -----------------------------------------------------------------------
164 the Gui callback functions
165 ____________________________________________________________________*/
169 openBrowser( "Docs/index.html" );
172 bool openBrowser(const std::string& aAddress)
175 string address(aAddress);
177 // do not resolve addresses with given protocol, i.e. "http://...", "ftp://..."
178 if (address.find("://")==string::npos)
180 // resolve local file path
181 SGPath path(address);
182 path = globals->resolve_maybe_aircraft_path(address);
184 address = path.str();
187 mkDialog ("Sorry, file not found!");
188 SG_LOG(SG_GENERAL, SG_ALERT, "openBrowser: Cannot find requested file '"
195 if (address.find("://")==string::npos) {
196 address = "file://" + address;
199 cocoaOpenUrl(address);
202 // Look for favorite browser
203 char win32_name[1024];
205 cygwin32_conv_to_full_win32_path(address.c_str(),win32_name);
207 strncpy(win32_name,address.c_str(), 1024);
209 ShellExecute ( NULL, "open", win32_name, NULL, NULL,
212 // Linux, BSD, SGI etc
213 string command = globals->get_browser();
214 string::size_type pos;
215 if ((pos = command.find("%u", 0)) != string::npos)
216 command.replace(pos, 2, address);
218 command += " \"" + address +"\"";
221 ok = (system( command.c_str() ) == 0);
224 if( fgGetBool("/sim/gui/show-browser-open-hint", true) )
225 mkDialog("The file is shown in your web browser window.");
230 #if defined( TR_HIRES_SNAP)
235 bool menu_status = fgGetBool("/sim/menubar/visibility");
236 char *filename = new char [24];
237 static int count = 1;
239 SGPropertyNode *master_freeze = fgGetNode("/sim/freeze/master");
241 bool freeze = master_freeze->getBoolValue();
243 master_freeze->setBoolValue(true);
246 fgSetBool("/sim/menubar/visibility", false);
247 int mouse = fgGetMouseCursor();
248 fgSetMouseCursor(MOUSE_CURSOR_NONE);
250 FGRenderer *renderer = globals->get_renderer();
252 renderer->resize( fgGetInt("/sim/startup/xsize"),
253 fgGetInt("/sim/startup/ysize") );
255 // we need two render frames here to clear the menu and cursor
256 // ... not sure why but doing an extra fgRenderFrame() shouldn't
258 //renderer->update( true );
259 //renderer->update( true );
261 // This ImageSize stuff is a temporary hack
262 // should probably use 128x128 tile size and
263 // support any image size
265 // This should be a requester to get multiplier from user
266 int multiplier = fgGetInt("/sim/startup/hires-multiplier", 3);
267 int width = fgGetInt("/sim/startup/xsize");
268 int height = fgGetInt("/sim/startup/ysize");
270 /* allocate buffer large enough to store one tile */
271 GLubyte *tile = (GLubyte *)malloc(width * height * 3 * sizeof(GLubyte));
274 printf("Malloc of tile buffer failed!\n");
278 int imageWidth = multiplier*width;
279 int imageHeight = multiplier*height;
281 /* allocate buffer to hold a row of tiles */
283 = (GLubyte *)malloc(imageWidth * height * 3 * sizeof(GLubyte));
287 printf("Malloc of tile row buffer failed!\n");
290 TRcontext *tr = trNew();
291 trTileSize(tr, width, height, 0);
292 trTileBuffer(tr, GL_RGB, GL_UNSIGNED_BYTE, tile);
293 trImageSize(tr, imageWidth, imageHeight);
294 trRowOrder(tr, TR_TOP_TO_BOTTOM);
296 // sgFrustum *frustum = ssgGetFrustum();
298 // frustum->getLeft(), frustum->getRight(),
299 // frustum->getBot(), frustum->getTop(),
300 // frustum->getNear(), frustum->getFar());
302 /* Prepare ppm output file */
303 while (count < 1000) {
304 snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
305 if ( (f = fopen(filename, "r")) == NULL )
309 f = fopen(filename, "wb");
311 printf("Couldn't open image file: %s\n", filename);
318 fprintf(f,"# ppm-file created by %s\n", "trdemo2");
319 fprintf(f,"%i %i\n", imageWidth, imageHeight);
322 /* just to be safe... */
323 glPixelStorei(GL_PACK_ALIGNMENT, 1);
327 /* Because the HUD and Panel change the ViewPort we will
328 * need to handle some lowlevel stuff ourselves */
329 int ncols = trGet(tr, TR_COLUMNS);
330 int nrows = trGet(tr, TR_ROWS);
332 bool do_hud = fgGetBool("/sim/hud/visibility");
333 GLfloat hud_col_step = 640.0 / ncols;
334 GLfloat hud_row_step = 480.0 / nrows;
336 bool do_panel = fgPanelVisible();
337 GLfloat panel_col_step = globals->get_current_panel()->getWidth() / ncols;
338 GLfloat panel_row_step = globals->get_current_panel()->getHeight() / nrows;
340 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
341 glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
342 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
343 glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
344 glHint(GL_FOG_HINT, GL_NICEST);
350 int curColumn = trGet(tr, TR_CURRENT_COLUMN);
351 // int curRow = trGet(tr, TR_CURRENT_ROW);
356 // fgUpdateHUD( curColumn*hud_col_step, curRow*hud_row_step,
357 // (curColumn+1)*hud_col_step, (curRow+1)*hud_row_step );
360 // globals->get_current_panel()->update(
361 // curColumn*panel_col_step, panel_col_step,
362 // curRow*panel_row_step, panel_row_step );
363 more = trEndTile(tr);
365 /* save tile into tile row buffer*/
366 int curTileWidth = trGet(tr, TR_CURRENT_TILE_WIDTH);
367 int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
368 int bytesPerTileRow = (width) * 3*sizeof(GLubyte);
369 int xOffset = curColumn * bytesPerTileRow;
370 int bytesPerCurrentTileRow = (curTileWidth) * 3*sizeof(GLubyte);
372 for (i=0;i<height;i++) {
373 memcpy(buffer + i*bytesPerImageRow + xOffset, /* Dest */
374 tile + i*bytesPerTileRow, /* Src */
375 bytesPerCurrentTileRow); /* Byte count*/
378 if (curColumn == trGet(tr, TR_COLUMNS)-1) {
379 /* write this buffered row of tiles to the file */
380 int curTileHeight = trGet(tr, TR_CURRENT_TILE_HEIGHT);
381 int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
383 for (i=0;i<curTileHeight;i++) {
384 /* Remember, OpenGL images are bottom to top. Have to reverse. */
385 GLubyte *rowPtr = buffer + (curTileHeight-1-i) * bytesPerImageRow;
386 fwrite(rowPtr, 1, imageWidth*3, f);
392 renderer->resize( width, height );
396 glHint(GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE);
397 glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
398 glHint(GL_POINT_SMOOTH_HINT, GL_DONT_CARE);
399 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_DONT_CARE);
400 if ( (!strcmp(fgGetString("/sim/rendering/fog"), "disabled")) ||
401 (!fgGetBool("/sim/rendering/shading"))) {
402 // if fastest fog requested, or if flat shading force fastest
403 glHint ( GL_FOG_HINT, GL_FASTEST );
404 } else if ( !strcmp(fgGetString("/sim/rendering/fog"), "nicest") ) {
405 glHint ( GL_FOG_HINT, GL_DONT_CARE );
410 message = "Snapshot saved to \"";
413 mkDialog (message.c_str());
420 fgSetMouseCursor(mouse);
421 fgSetBool("/sim/menubar/visibility", menu_status);
424 master_freeze->setBoolValue(false);
427 #endif // #if defined( TR_HIRES_SNAP)
429 void fgDumpSnapShotWrapper () {
434 void fgHiResDumpWrapper () {
440 using namespace flightgear;
442 SGPath nextScreenshotPath(const std::string& screenshotDir)
445 static int count = 1;
446 while (count < 1000) {
447 snprintf(filename, 32, "fgfs-screen-%03d.png", count++);
449 SGPath p(screenshotDir);
459 class GUISnapShotOperation :
460 public GraphicsContextOperation
464 // start new snap shot
467 // allow only one snapshot at a time
468 if (_snapShotOp.valid())
470 _snapShotOp = new GUISnapShotOperation();
471 /* register with graphics context so actual snap shot is done
472 * in the graphics context (thread) */
473 osg::Camera* guiCamera = getGUICamera(CameraGroup::getDefault());
474 WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA();
475 osg::GraphicsContext* gc = 0;
477 gc = guiCamera->getGraphicsContext();
479 gc->add(_snapShotOp.get());
481 wsa->windows[0]->gc->add(_snapShotOp.get());
487 // constructor to be executed in main loop's thread
488 GUISnapShotOperation() :
489 flightgear::GraphicsContextOperation(std::string("GUI snap shot")),
490 _master_freeze(fgGetNode("/sim/freeze/master", true)),
491 _freeze(_master_freeze->getBoolValue()),
493 _mouse(fgGetMouseCursor())
496 _master_freeze->setBoolValue(true);
498 fgSetMouseCursor(MOUSE_CURSOR_NONE);
500 string dir = fgGetString("/sim/paths/screenshot-dir");
502 dir = SGPath::desktop().str();
504 _path.set(dir + '/');
505 if (_path.create_dir( 0755 )) {
506 SG_LOG(SG_GENERAL, SG_ALERT, "Cannot create screenshot directory '"
507 << dir << "'. Trying home directory.");
508 dir = globals->get_fg_home();
511 _path = nextScreenshotPath(dir);
512 _xsize = fgGetInt("/sim/startup/xsize");
513 _ysize = fgGetInt("/sim/startup/ysize");
515 FGRenderer *renderer = globals->get_renderer();
516 renderer->resize(_xsize, _ysize);
517 globals->get_event_mgr()->addTask("SnapShotTimer",
518 this, &GUISnapShotOperation::timerExpired,
522 // to be executed in graphics context (maybe separate thread)
523 void run(osg::GraphicsContext* gc)
525 _result = sg_glDumpWindow(_path.c_str(),
530 // timer method, to be executed in main loop's thread
531 virtual void timerExpired()
535 globals->get_event_mgr()->removeTask("SnapShotTimer");
537 fgSetString("/sim/paths/screenshot-last", _path.c_str());
538 fgSetBool("/sim/signals/screenshot", _result);
540 fgSetMouseCursor(_mouse);
543 _master_freeze->setBoolValue(false);
549 static osg::ref_ptr<GUISnapShotOperation> _snapShotOp;
550 SGPropertyNode_ptr _master_freeze;
558 } // of anonymous namespace
560 osg::ref_ptr<GUISnapShotOperation> GUISnapShotOperation::_snapShotOp;
562 // do a screen snap shot
563 bool fgDumpSnapShot ()
565 // start snap shot operation, while needs to be executed in
567 return GUISnapShotOperation::start();
570 // do an entire scenegraph dump
571 void fgDumpSceneGraph()
573 char *filename = new char [24];
575 static int count = 1;
577 SGPropertyNode *master_freeze = fgGetNode("/sim/freeze/master");
579 bool freeze = master_freeze->getBoolValue();
581 master_freeze->setBoolValue(true);
584 while (count < 1000) {
586 snprintf(filename, 24, "fgfs-graph-%03d.osg", count++);
587 if ( (fp = fopen(filename, "r")) == NULL )
592 if ( fgDumpSceneGraphToFile(filename)) {
593 message = "Entire scene graph saved to \"";
597 message = "Failed to save to \"";
602 mkDialog (message.c_str());
607 master_freeze->setBoolValue(false);
612 // do an terrain branch dump
613 void fgDumpTerrainBranch()
615 char *filename = new char [24];
617 static int count = 1;
619 SGPropertyNode *master_freeze = fgGetNode("/sim/freeze/master");
621 bool freeze = master_freeze->getBoolValue();
623 master_freeze->setBoolValue(true);
626 while (count < 1000) {
628 snprintf(filename, 24, "fgfs-graph-%03d.osg", count++);
629 if ( (fp = fopen(filename, "r")) == NULL )
634 if ( fgDumpTerrainBranchToFile(filename)) {
635 message = "Terrain graph saved to \"";
639 message = "Failed to save to \"";
644 mkDialog (message.c_str());
649 master_freeze->setBoolValue(false);
653 void fgPrintVisibleSceneInfoCommand()
655 SGPropertyNode *master_freeze = fgGetNode("/sim/freeze/master");
657 bool freeze = master_freeze->getBoolValue();
659 master_freeze->setBoolValue(true);
662 flightgear::printVisibleSceneInfo(globals->get_renderer());
665 master_freeze->setBoolValue(false);