| 1 | // Copyright (C) 2008 Tim Moore |
| 2 | // |
| 3 | // This program is free software; you can redistribute it and/or |
| 4 | // modify it under the terms of the GNU General Public License as |
| 5 | // published by the Free Software Foundation; either version 2 of the |
| 6 | // License, or (at your option) any later version. |
| 7 | // |
| 8 | // This program is distributed in the hope that it will be useful, but |
| 9 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | // General Public License for more details. |
| 12 | // |
| 13 | // You should have received a copy of the GNU General Public License |
| 14 | // along with this program; if not, write to the Free Software |
| 15 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 | |
| 17 | #include "config.h" |
| 18 | |
| 19 | #include "WindowBuilder.hxx" |
| 20 | #include "WindowSystemAdapter.hxx" |
| 21 | #include <Main/fg_props.hxx> |
| 22 | #include <osg/Version> |
| 23 | |
| 24 | #include <GUI/MessageBox.hxx> |
| 25 | |
| 26 | #include <sstream> |
| 27 | #include <limits> |
| 28 | |
| 29 | #if defined(SG_MAC) |
| 30 | #include <osgViewer/api/Cocoa/GraphicsWindowCocoa> |
| 31 | #endif |
| 32 | |
| 33 | using namespace std; |
| 34 | using namespace osg; |
| 35 | |
| 36 | namespace flightgear |
| 37 | { |
| 38 | string makeName(const string& prefix, int num) |
| 39 | { |
| 40 | stringstream stream; |
| 41 | stream << prefix << num; |
| 42 | return stream.str(); |
| 43 | } |
| 44 | |
| 45 | ref_ptr<WindowBuilder> WindowBuilder::windowBuilder; |
| 46 | |
| 47 | string WindowBuilder::defaultWindowName("FlightGear"); |
| 48 | |
| 49 | // default to true (historical behaviour), we will clear the flag if |
| 50 | // we run another GUI. |
| 51 | bool WindowBuilder::poseAsStandaloneApp = true; |
| 52 | |
| 53 | void WindowBuilder::initWindowBuilder(bool stencil) |
| 54 | { |
| 55 | windowBuilder = new WindowBuilder(stencil); |
| 56 | } |
| 57 | |
| 58 | WindowBuilder::WindowBuilder(bool stencil) : defaultCounter(0) |
| 59 | { |
| 60 | makeDefaultTraits(stencil); |
| 61 | } |
| 62 | |
| 63 | void WindowBuilder::makeDefaultTraits(bool stencil) |
| 64 | { |
| 65 | GraphicsContext::WindowingSystemInterface* wsi |
| 66 | = osg::GraphicsContext::getWindowingSystemInterface(); |
| 67 | #if defined(HAVE_QT) && OSG_VERSION_GREATER_THAN(3, 5, 9) |
| 68 | if (usingQtGraphicsWindow) { |
| 69 | // use the correct WSI for OpenSceneGraph >= 3.6 |
| 70 | wsi = osg::GraphicsContext::getWindowingSystemInterface("FlightGearQt5"); |
| 71 | } |
| 72 | #endif |
| 73 | |
| 74 | |
| 75 | defaultTraits = new osg::GraphicsContext::Traits; |
| 76 | auto traits = defaultTraits.get(); |
| 77 | |
| 78 | traits->readDISPLAY(); |
| 79 | if (traits->displayNum < 0) |
| 80 | traits->displayNum = 0; |
| 81 | if (traits->screenNum < 0) |
| 82 | traits->screenNum = 0; |
| 83 | |
| 84 | int bpp = fgGetInt("/sim/rendering/bits-per-pixel"); |
| 85 | int cbits = (bpp <= 16) ? 5 : 8; |
| 86 | int zbits = (bpp <= 16) ? 16 : 24; |
| 87 | traits->red = traits->green = traits->blue = cbits; |
| 88 | traits->depth = zbits; |
| 89 | |
| 90 | if (stencil) |
| 91 | traits->stencil = 8; |
| 92 | |
| 93 | traits->doubleBuffer = true; |
| 94 | traits->mipMapGeneration = true; |
| 95 | traits->windowName = "FlightGear"; |
| 96 | // XXX should check per window too. |
| 97 | traits->sampleBuffers = fgGetInt("/sim/rendering/multi-sample-buffers", traits->sampleBuffers); |
| 98 | traits->samples = fgGetInt("/sim/rendering/multi-samples", traits->samples); |
| 99 | traits->vsync = fgGetBool("/sim/rendering/vsync-enable", traits->vsync); |
| 100 | |
| 101 | // Used by tree.eff |
| 102 | fgSetBool("/sim/rendering/window-has-multi-sample-buffers", traits->sampleBuffers > 0); |
| 103 | |
| 104 | const bool wantFullscreen = fgGetBool("/sim/startup/fullscreen"); |
| 105 | unsigned screenwidth = 0; |
| 106 | unsigned screenheight = 0; |
| 107 | // this is a deprecated method, should be screen-aware. |
| 108 | wsi->getScreenResolution(*traits, screenwidth, screenheight); |
| 109 | |
| 110 | // handle fullscreen manually |
| 111 | traits->windowDecoration = !wantFullscreen; |
| 112 | if (!traits->windowDecoration) { |
| 113 | // fullscreen |
| 114 | traits->supportsResize = false; |
| 115 | traits->width = screenwidth; |
| 116 | traits->height = screenheight; |
| 117 | SG_LOG(SG_VIEW,SG_DEBUG,"Using full screen size for window: " << screenwidth << " x " << screenheight); |
| 118 | } else { |
| 119 | // window |
| 120 | int w = fgGetInt("/sim/startup/xsize"); |
| 121 | int h = fgGetInt("/sim/startup/ysize"); |
| 122 | traits->supportsResize = true; |
| 123 | traits->width = w; |
| 124 | traits->height = h; |
| 125 | if ((w>0)&&(h>0)) |
| 126 | { |
| 127 | traits->x = ((unsigned)w>screenwidth) ? 0 : (screenwidth-w)/3; |
| 128 | traits->y = ((unsigned)h>screenheight) ? 0 : (screenheight-h)/3; |
| 129 | } |
| 130 | SG_LOG(SG_VIEW,SG_DEBUG,"Using initial window size: " << w << " x " << h); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | } // of namespace flightgear |
| 135 | |
| 136 | namespace |
| 137 | { |
| 138 | // Helper functions that set a value based on a property if it exists, |
| 139 | // returning 1 if the value was set. |
| 140 | |
| 141 | inline int setFromProperty(string& place, const SGPropertyNode* node, |
| 142 | const char* name) |
| 143 | { |
| 144 | const SGPropertyNode* valNode = node->getNode(name); |
| 145 | if (valNode) { |
| 146 | place = valNode->getStringValue(); |
| 147 | return 1; |
| 148 | } |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | inline int setFromProperty(int& place, const SGPropertyNode* node, |
| 153 | const char* name) |
| 154 | { |
| 155 | const SGPropertyNode* valNode = node->getNode(name); |
| 156 | if (valNode) { |
| 157 | place = valNode->getIntValue(); |
| 158 | return 1; |
| 159 | } |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | inline int setFromProperty(bool& place, const SGPropertyNode* node, |
| 164 | const char* name) |
| 165 | { |
| 166 | const SGPropertyNode* valNode = node->getNode(name); |
| 167 | if (valNode) { |
| 168 | place = valNode->getBoolValue(); |
| 169 | return 1; |
| 170 | } |
| 171 | return 0; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | namespace flightgear |
| 176 | { |
| 177 | |
| 178 | void WindowBuilder::setFullscreenTraits(const SGPropertyNode* winNode, GraphicsContext::Traits* traits) |
| 179 | { |
| 180 | const SGPropertyNode* orrNode = winNode->getNode("overrideRedirect"); |
| 181 | bool overrideRedirect = orrNode && orrNode->getBoolValue(); |
| 182 | traits->overrideRedirect = overrideRedirect; |
| 183 | |
| 184 | traits->windowDecoration = false; |
| 185 | |
| 186 | unsigned int width = 0; |
| 187 | unsigned int height = 0; |
| 188 | auto wsi = osg::GraphicsContext::getWindowingSystemInterface(); |
| 189 | wsi->getScreenResolution(*traits, width, height); |
| 190 | traits->width = width; |
| 191 | traits->height = height; |
| 192 | traits->supportsResize = false; |
| 193 | traits->x = 0; |
| 194 | traits->y = 0; |
| 195 | } |
| 196 | |
| 197 | bool WindowBuilder::setWindowedTraits(const SGPropertyNode* winNode, GraphicsContext::Traits* traits) |
| 198 | { |
| 199 | bool customTraits = false; |
| 200 | int resizable = 0; |
| 201 | const SGPropertyNode* fullscreenNode = winNode->getNode("fullscreen"); |
| 202 | if (fullscreenNode && !fullscreenNode->getBoolValue()) |
| 203 | { |
| 204 | traits->windowDecoration = true; |
| 205 | resizable = 1; |
| 206 | } |
| 207 | resizable |= setFromProperty(traits->windowDecoration, winNode, "decoration"); |
| 208 | resizable |= setFromProperty(traits->width, winNode, "width"); |
| 209 | resizable |= setFromProperty(traits->height, winNode, "height"); |
| 210 | if (resizable) { |
| 211 | traits->supportsResize = true; |
| 212 | customTraits = true; |
| 213 | } |
| 214 | |
| 215 | return customTraits; |
| 216 | } |
| 217 | |
| 218 | void WindowBuilder::setMacPoseAsStandaloneApp(GraphicsContext::Traits* traits) |
| 219 | { |
| 220 | #if defined(SG_MAC) |
| 221 | // this logic is unecessary if using a Qt window, since everything |
| 222 | // plays together nicely |
| 223 | int flags = osgViewer::GraphicsWindowCocoa::WindowData::CheckForEvents; |
| 224 | |
| 225 | // avoid both QApplication and OSG::CocoaViewer doing single-application |
| 226 | // init (Apple menu, making front process, etc) |
| 227 | if (poseAsStandaloneApp) { |
| 228 | flags |= osgViewer::GraphicsWindowCocoa::WindowData::PoseAsStandaloneApp; |
| 229 | } |
| 230 | traits->inheritedWindowData = new osgViewer::GraphicsWindowCocoa::WindowData(flags); |
| 231 | #endif |
| 232 | } |
| 233 | |
| 234 | GraphicsWindow* WindowBuilder::buildWindow(const SGPropertyNode* winNode, bool isMainWindow) |
| 235 | { |
| 236 | WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA(); |
| 237 | string windowName; |
| 238 | if (winNode->hasChild("window-name")) |
| 239 | windowName = winNode->getStringValue("window-name"); |
| 240 | else if (winNode->hasChild("name")) |
| 241 | windowName = winNode->getStringValue("name"); |
| 242 | if (isMainWindow) { |
| 243 | SG_LOG(SG_GENERAL, SG_DEBUG, "Changing defaultWindowName from " |
| 244 | << defaultWindowName << " to " << windowName); |
| 245 | defaultWindowName = windowName; |
| 246 | } |
| 247 | GraphicsWindow* result = 0; |
| 248 | if (!windowName.empty()) { |
| 249 | // look for an existing window and return that |
| 250 | result = wsa->findWindow(windowName); |
| 251 | if (result) |
| 252 | return result; |
| 253 | } |
| 254 | auto traits = new GraphicsContext::Traits(*defaultTraits); |
| 255 | |
| 256 | // Attempt to share context with the window that was created first |
| 257 | if (!wsa->windows.empty()) |
| 258 | traits->sharedContext = wsa->windows.front()->gc; |
| 259 | |
| 260 | int traitsSet = setFromProperty(traits->hostName, winNode, "host-name"); |
| 261 | traitsSet |= setFromProperty(traits->displayNum, winNode, "display"); |
| 262 | traitsSet |= setFromProperty(traits->screenNum, winNode, "screen"); |
| 263 | |
| 264 | const SGPropertyNode* fullscreenNode = winNode->getNode("fullscreen"); |
| 265 | if (fullscreenNode && fullscreenNode->getBoolValue()) { |
| 266 | setFullscreenTraits(winNode, traits); |
| 267 | traitsSet = 1; |
| 268 | } else { |
| 269 | traitsSet |= setWindowedTraits(winNode, traits); |
| 270 | } |
| 271 | traitsSet |= setFromProperty(traits->x, winNode, "x"); |
| 272 | traitsSet |= setFromProperty(traits->y, winNode, "y"); |
| 273 | if (!windowName.empty() && windowName != traits->windowName) { |
| 274 | traits->windowName = windowName; |
| 275 | traitsSet = 1; |
| 276 | } else if (traitsSet) { |
| 277 | traits->windowName = makeName("FlightGear", defaultCounter++); |
| 278 | } |
| 279 | |
| 280 | setMacPoseAsStandaloneApp(traits); |
| 281 | |
| 282 | bool drawGUI = false; |
| 283 | traitsSet |= setFromProperty(drawGUI, winNode, "gui"); |
| 284 | if (traitsSet) { |
| 285 | GraphicsContext* gc = GraphicsContext::createGraphicsContext(traits); |
| 286 | if (gc) { |
| 287 | GraphicsWindow* window = WindowSystemAdapter::getWSA() |
| 288 | ->registerWindow(gc, traits->windowName); |
| 289 | if (drawGUI) |
| 290 | window->flags |= GraphicsWindow::GUI; |
| 291 | return window; |
| 292 | } else { |
| 293 | return 0; |
| 294 | } |
| 295 | } else { |
| 296 | // XXX What if the window has no traits, but does have a name? |
| 297 | // We should create a "default window" registered with that name. |
| 298 | return getDefaultWindow(); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | GraphicsWindow* WindowBuilder::getDefaultWindow() |
| 303 | { |
| 304 | GraphicsWindow* defaultWindow |
| 305 | = WindowSystemAdapter::getWSA()->findWindow(defaultWindowName); |
| 306 | if (defaultWindow) |
| 307 | return defaultWindow; |
| 308 | |
| 309 | // create if it, if necessary |
| 310 | GraphicsContext::Traits* traits |
| 311 | = new GraphicsContext::Traits(*defaultTraits); |
| 312 | traits->windowName = "FlightGear"; |
| 313 | |
| 314 | setMacPoseAsStandaloneApp(traits); |
| 315 | |
| 316 | // this may be the point, where we discover OpenGL is broken on the |
| 317 | // system. |
| 318 | GraphicsContext* gc = GraphicsContext::createGraphicsContext(traits); |
| 319 | if (!gc) { |
| 320 | flightgear::fatalMessageBoxThenExit("Unable to create window", |
| 321 | "FlightGear was unable to create a window supporting 3D rendering (OpenGL). " |
| 322 | "This is normally due to outdated graphics drivers, please check if updates are available. ", |
| 323 | "Depending on your OS and graphics chipset, updates might come from AMD, nVidia or Intel."); |
| 324 | return nullptr; // unreachable anyway |
| 325 | } |
| 326 | |
| 327 | defaultWindow = WindowSystemAdapter::getWSA()->registerWindow(gc, defaultWindowName); |
| 328 | return defaultWindow; |
| 329 | |
| 330 | } |
| 331 | |
| 332 | void WindowBuilder::setPoseAsStandaloneApp(bool b) |
| 333 | { |
| 334 | poseAsStandaloneApp = b; |
| 335 | } |
| 336 | |
| 337 | } // of namespace flightgear |