vtkInitializationHelper.cxx 12.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*=========================================================================

Program:   ParaView
Module:    vtkInitializationHelper.cxx

Copyright (c) Kitware, Inc.
All rights reserved.
See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.

This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
#include "vtkInitializationHelper.h"
16

17
#include "vtkClientServerInterpreter.h"
18
#include "vtkClientServerInterpreterInitializer.h"
19
#include "vtkNew.h"
20
#include "vtkOutputWindow.h"
21
#include "vtkProcessModule.h"
22 23
#include "vtkPVConfig.h"
#include "vtkPVInitializer.h"
24
#include "vtkPVOptions.h"
25
#include "vtkPVPluginLoader.h"
Cory Quammen's avatar
Cory Quammen committed
26 27
#include "vtkPVSession.h"
#include "vtkSMSettings.h"
28
#include "vtkSmartPointer.h"
29
#include "vtkSMMessage.h"
30
#include "vtkSMProperty.h"
31
#include "vtkSMProxyManager.h"
32

33
#include <string>
34
#include <vtksys/ios/sstream>
35
#include <vtksys/SystemTools.hxx>
36

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
// Windows-only helper functionality:
#ifdef _WIN32
# include <Windows.h>
#endif

namespace {

#ifdef _WIN32
BOOL CALLBACK listMonitorsCallback(HMONITOR hMonitor, HDC /*hdcMonitor*/,
                                   LPRECT /*lprcMonitor*/, LPARAM dwData)
{
  std::ostringstream *str = reinterpret_cast<std::ostringstream*>(dwData);

  MONITORINFOEX monitorInfo;
  monitorInfo.cbSize = sizeof(monitorInfo);

  if (GetMonitorInfo(hMonitor, &monitorInfo))
    {
      LPRECT rect = &monitorInfo.rcMonitor;
      *str << "Device: \"" << monitorInfo.szDevice << "\" "
           << "Geometry: "
           << std::noshowpos
           << rect->right - rect->left << "x"
           << rect->bottom - rect->top
           << std::showpos
           << rect->left << rect->top << " "
           << ((monitorInfo.dwFlags & MONITORINFOF_PRIMARY)
               ? "(primary)" : "")
           << std::endl;
    }
  return true;
}
#endif // _WIN32

std::string ListAttachedMonitors()
{
#ifndef _WIN32
  return std::string("Monitor detection only implemented for MS Windows.");
#else // _WIN32
  std::ostringstream str;
  EnumDisplayMonitors(NULL, NULL, listMonitorsCallback,
                      reinterpret_cast<LPARAM>(&str));
  return str.str();
#endif // _WIN32
}

} // end anon namespace

85
bool vtkInitializationHelper::LoadSettingsFilesDuringInitialization = true;
86

87
bool vtkInitializationHelper::SaveUserSettingsFileDuringFinalization = false;
88

89
std::string vtkInitializationHelper::OrganizationName = "ParaView";
Cory Quammen's avatar
Cory Quammen committed
90
std::string vtkInitializationHelper::ApplicationName = "GenericParaViewApplication";
91

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
//----------------------------------------------------------------------------
void vtkInitializationHelper::SetLoadSettingsFilesDuringInitialization(bool val)
{
  if (vtkProcessModule::GetProcessModule() &&
    val != vtkInitializationHelper::LoadSettingsFilesDuringInitialization)
    {
    vtkGenericWarningMacro("SetLoadSettingsFilesDuringInitialization should be called "
      "before calling Initialize().");
    }
  else
    {
    vtkInitializationHelper::LoadSettingsFilesDuringInitialization = val;
    }
}

//----------------------------------------------------------------------------
bool vtkInitializationHelper::GetLoadSettingsFilesDuringInitialization()
{
  return vtkInitializationHelper::LoadSettingsFilesDuringInitialization;
}

113 114 115 116 117 118 119 120 121 122 123 124
//----------------------------------------------------------------------------
void vtkInitializationHelper::SetOrganizationName(const std::string & organizationName)
{
  vtkInitializationHelper::OrganizationName = organizationName;
}

//----------------------------------------------------------------------------
const std::string & vtkInitializationHelper::GetOrganizationName()
{
  return vtkInitializationHelper::OrganizationName;
}

125 126 127 128 129 130 131
//----------------------------------------------------------------------------
void vtkInitializationHelper::SetApplicationName(const std::string & appName)
{
  vtkInitializationHelper::ApplicationName = appName;
}

//----------------------------------------------------------------------------
132
const std::string & vtkInitializationHelper::GetApplicationName()
133 134 135 136
{
  return vtkInitializationHelper::ApplicationName;
}

137
//----------------------------------------------------------------------------
138
void vtkInitializationHelper::Initialize(const char* executable, int type)
139
{
140
  vtkInitializationHelper::Initialize(executable, type, NULL);
141 142 143 144
}

//----------------------------------------------------------------------------
void vtkInitializationHelper::Initialize(const char* executable,
145
  int type, vtkPVOptions* options)
146
{
147 148 149 150 151
  if (!executable)
    {
    vtkGenericWarningMacro("Executable name has to be defined.");
    return;
    }
152 153 154 155

  // Pass the program name to make option parser happier
  char* argv = new char[strlen(executable)+1];
  strcpy(argv, executable);
156 157 158 159 160 161

  vtkSmartPointer<vtkPVOptions> newoptions = options;
  if (!options)
    {
    newoptions = vtkSmartPointer<vtkPVOptions>::New();
    }
162
  vtkInitializationHelper::Initialize(1, &argv, type, newoptions);
163 164 165 166
  delete[] argv;
}

//----------------------------------------------------------------------------
167 168
void vtkInitializationHelper::Initialize(int argc, char**argv,
  int type, vtkPVOptions* options)
169
{
170
  if (vtkProcessModule::GetProcessModule())
171
    {
172
    vtkGenericWarningMacro("Process already initialize. Skipping.");
173 174
    return;
    }
175 176 177 178 179 180 181

  if (!options)
    {
    vtkGenericWarningMacro("vtkPVOptions must be specified.");
    return;
    }

182 183 184 185
  // Verify that the version of the library that we linked against is
  // compatible with the version of the headers we compiled against.
  GOOGLE_PROTOBUF_VERIFY_VERSION;

186 187 188 189 190
  vtkProcessModule::Initialize(
    static_cast<vtkProcessModule::ProcessTypes>(type), argc, argv);

  vtksys_ios::ostringstream sscerr;
  if (argv && !options->Parse(argc, argv) )
191
    {
192 193 194 195 196 197 198 199 200
    if ( options->GetUnknownArgument() )
      {
      sscerr << "Got unknown argument: " << options->GetUnknownArgument() << endl;
      }
    if ( options->GetErrorMessage() )
      {
      sscerr << "Error: " << options->GetErrorMessage() << endl;
      }
    options->SetHelpSelected(1);
201
    }
202
  if (options->GetHelpSelected())
203
    {
204 205 206
    sscerr << options->GetHelp() << endl;
    vtkOutputWindow::GetInstance()->DisplayText( sscerr.str().c_str() );
    // TODO: indicate to the caller that application must quit.
207
    }
208

209 210
  if (options->GetTellVersion() )
    {
211 212 213
    vtksys_ios::ostringstream str;
    str << "paraview version " << PARAVIEW_VERSION_FULL << "\n";
    vtkOutputWindow::GetInstance()->DisplayText(str.str().c_str());
214 215
    // TODO: indicate to the caller that application must quit.
    }
216

217 218
  if (options->GetPrintMonitors())
    {
219
      std::string monitors = ListAttachedMonitors();
220 221 222 223
      vtkOutputWindow::GetInstance()->DisplayText(monitors.c_str());
      // TODO: indicate to the caller that application must quit.
    }

224
  vtkProcessModule::GetProcessModule()->SetOptions(options);
225

226 227 228 229
  // this has to happen after process module is initialized and options have
  // been set.
  PARAVIEW_INITIALIZE();

230 231 232 233
  // Set multi-server flag to vtkProcessModule
  vtkProcessModule::GetProcessModule()->SetMultipleSessionsSupport(
        options->GetMultiServerMode() != 0);

234 235
  // Make sure the ProxyManager get created...
  vtkSMProxyManager::GetProxyManager();
236 237 238 239 240

  // Now load any plugins located in the PV_PLUGIN_PATH environment variable.
  // These are always loaded (not merely located).
  vtkNew<vtkPVPluginLoader> loader;
  loader->LoadPluginsFromPluginSearchPath();
Cory Quammen's avatar
Cory Quammen committed
241

242 243 244 245 246 247
  vtkInitializationHelper::SaveUserSettingsFileDuringFinalization = false;
  // Load settings files on client-processes.
  if (!options->GetDisableRegistry() &&
    type != vtkProcessModule::PROCESS_SERVER &&
    type != vtkProcessModule::PROCESS_DATA_SERVER &&
    type != vtkProcessModule::PROCESS_RENDER_SERVER)
248 249 250
    {
    vtkInitializationHelper::LoadSettings();
    }
251 252
}

253
//----------------------------------------------------------------------------
254
void vtkInitializationHelper::StandaloneInitialize()
255 256 257 258 259 260
{
  // Verify that the version of the library that we linked against is
  // compatible with the version of the headers we compiled against.
  GOOGLE_PROTOBUF_VERIFY_VERSION;
}

261 262 263
//----------------------------------------------------------------------------
void vtkInitializationHelper::Finalize()
{
264
  if (vtkInitializationHelper::SaveUserSettingsFileDuringFinalization)
265
    {
266
    // Write out settings file(s)
267 268
    std::string userSettingsFilePath =
      vtkInitializationHelper::GetUserSettingsFilePath();
269
    vtkSMSettings* settings = vtkSMSettings::GetInstance();
270
    bool savingSucceeded = settings->SaveSettingsToFile(userSettingsFilePath.c_str());
271 272
    if (!savingSucceeded)
      {
273 274
      vtkGenericWarningMacro(<< "Saving settings file to '"
                             << userSettingsFilePath << "' failed");
275
      }
276 277
    }

278
  vtkSMProxyManager::Finalize();
279
  vtkProcessModule::Finalize();
280 281 282

  // Optional:  Delete all global objects allocated by libprotobuf.
  google::protobuf::ShutdownProtobufLibrary();
283
}
284 285

//----------------------------------------------------------------------------
286
void vtkInitializationHelper::StandaloneFinalize()
287 288 289 290 291
{
  // Optional:  Delete all global objects allocated by libprotobuf.
  google::protobuf::ShutdownProtobufLibrary();
}

292 293 294
//----------------------------------------------------------------------------
bool vtkInitializationHelper::LoadSettings()
{
295 296 297 298 299
  if (vtkInitializationHelper::LoadSettingsFilesDuringInitialization == false)
    {
    return false;
    }

300
  vtkSMSettings* settings = vtkSMSettings::GetInstance();
301
  int myRank = vtkProcessModule::GetProcessModule()->GetPartitionId();
302 303
  bool success = true;

304 305 306 307 308 309
  if (myRank > 0) // don't read files on satellites.
    {
    settings->DistributeSettings();
    return true;
    }

310
  // Load user-level settings
311 312
  std::string userSettingsFilePath = vtkInitializationHelper::GetUserSettingsFilePath();
  success = success && settings->AddCollectionFromFile(userSettingsFilePath, VTK_DOUBLE_MAX);
313 314

  // Load site-level settings
315 316 317 318 319 320 321 322 323 324 325
  vtkPVOptions* options = vtkProcessModule::GetProcessModule()->GetOptions();
  std::string app_dir = options->GetApplicationPath();
  app_dir = vtksys::SystemTools::GetProgramPath(app_dir.c_str());

  std::vector<std::string> pathsToSearch;
  pathsToSearch.push_back(app_dir);
  pathsToSearch.push_back(app_dir + "/../lib/");
#if defined(__APPLE__)
  // paths for app
  pathsToSearch.push_back(app_dir + "/../../..");
  pathsToSearch.push_back(app_dir + "/../../../../lib");
326

327 328 329 330 331 332
  // paths when doing an unix style install.
  pathsToSearch.push_back(app_dir +"/../lib/paraview-" PARAVIEW_VERSION);
#endif
  // On windows configuration files are in the parent directory
  pathsToSearch.push_back(app_dir + "/../");

333
  std::string filename = vtkInitializationHelper::GetApplicationName() + "-SiteSettings.json";
334 335 336 337 338 339 340 341 342 343 344 345
  std::string siteSettingsFile;

  for (size_t cc = 0; cc < pathsToSearch.size(); cc++)
    {
    std::string path = pathsToSearch[cc];
    if (vtksys::SystemTools::FileExists((path + "/" + filename).c_str(), true))
      {
      siteSettingsFile = path + "/" + filename;
      break;
      }
    }

Cory Quammen's avatar
Cory Quammen committed
346
  success = success && settings->AddCollectionFromFile(siteSettingsFile, 1.0);
347 348 349

  settings->DistributeSettings();

350
  vtkInitializationHelper::SaveUserSettingsFileDuringFinalization = true;
351 352 353 354 355 356
  return success;
}

//----------------------------------------------------------------------------
std::string vtkInitializationHelper::GetUserSettingsDirectory()
{
357
  std::string organizationName(vtkInitializationHelper::GetOrganizationName());
358
  std::string applicationName(vtkInitializationHelper::GetApplicationName());
359 360 361 362 363 364 365
#if defined(WIN32)
  const char* appData = getenv("APPDATA");
  if (!appData)
    {
    return std::string();
    }
  std::string separator("\\");
366 367
  std::string directoryPath(appData);
  if (directoryPath[directoryPath.size()-1] != separator[0])
368
    {
369
    directoryPath.append(separator);
370
    }
371
  directoryPath += applicationName + separator;
372 373 374 375 376 377 378
#else
  const char* home = getenv("HOME");
  if (!home)
    {
    return std::string();
    }
  std::string separator("/");
379 380
  std::string directoryPath(home);
  if (directoryPath[directoryPath.size()-1] != separator[0])
381
    {
382
    directoryPath.append(separator);
383
    }
384
  directoryPath += ".config" + separator + organizationName + separator;
385 386
#endif

387
  return directoryPath;
388 389
}

390 391 392 393 394 395 396 397 398 399
//----------------------------------------------------------------------------
std::string vtkInitializationHelper::GetUserSettingsFilePath()
{
  std::string path = vtkInitializationHelper::GetUserSettingsDirectory();
  path.append(vtkInitializationHelper::GetApplicationName());
  path.append("-UserSettings.json");

  return path;
}

400 401 402 403 404
//----------------------------------------------------------------------------
void vtkInitializationHelper::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
}