void Settings::checkCommandLineOptions(int argc, char *argv[]) { // check if --help occurs, overrides the rest for (int i=1; i < argc; ++i ) { std::string sw = argv[i]; if ( sw == "--help" ) { createHelpInfo(); std::cout << helpinfo.str() << std::endl; exit(0); } } // decode arguments int optind=1; while ((optind < argc)) // && (argv[optind][0]=='-') { if ( argv[optind][0]=='-' ) { std::string sw = argv[optind]; if ( sw=="--profile" ) { if ( argv[++optind] ) loadProfile(argv[optind]); else BE_ERROR("::SETTINGS --profile expects a filename"); } else { parseH.reset(); if ( parseH.beginMatchesStrip( "--", sw ) ) { sw.append(" "); std::string purecmd = parseH.returnUntillStrip( ' ', sw ); if ( isCVar(purecmd) ) { if ( argv[++optind] ) { if ( !setCVar(purecmd, argv[optind]) ) BE_ERROR("::SETTINGS error: could not set cvar '" << purecmd << "', value '" << argv[optind] << "'"); } else BE_ERROR("::SETTINGS error: option '" << purecmd << "' expects an argument"); } else BE_LOG( "warning: unknown commandline option: '" << purecmd << "'" ); } } } ++optind; } if ( optind < argc ) BE_LOG( "warning: unknown commandline option: '" << argv[optind] << "'" ); }
/////////////////////////////////////////////////////////////////////////////// /// \brief If there are lots of history entries, removes all old entries, /// leaving only the most recently created ones. /// /// \details If history entires are removed, the remaining entries are /// compressed so that their indices start at 0 for the oldest entry. /// /// \param id The AssetId of the WindowSettings stack to truncate. /// \param max_history_entries The number of entries to leave in the database. /// /// \ingroup loading void truncateWindowSettingsHistory(const AssetId& id, int max_history_entries) { try { std::shared_ptr<bed::Bed> bed = bed::openWritable(id.bed); if (!bed) throw std::runtime_error("Could not open bed for writing!"); bed::Db& db = bed->getDb(); if (db.getInt(BE_WND_WINDOW_SETTINGS_SQL_TABLE_EXISTS, 0) == 0) throw std::runtime_error("WindowSettings table does not exist!"); bed::Transaction transaction(db, bed::Transaction::Immediate); int history_index = 0; bed::Stmt latest(db, Id(BE_WND_WINDOW_SETTINGS_SQLID_LATEST_INDEX), BE_WND_WINDOW_SETTINGS_SQL_LATEST_INDEX); latest.bind(1, id.asset.value()); if (latest.step()) history_index = latest.getInt(0); history_index -= max_history_entries - 1; // remove old history entries bed::Stmt remove(db, Id(BE_WND_WINDOW_SETTINGS_SQLID_TRUNCATE), BE_WND_WINDOW_SETTINGS_SQL_TRUNCATE); remove.bind(1, id.asset.value()); remove.bind(2, history_index); remove.step(); // compress remaining history indices closer to 0 if (history_index > 1) { bed::Stmt compress(db, Id(BE_WND_WINDOW_SETTINGS_SQLID_COMPRESS), BE_WND_WINDOW_SETTINGS_SQL_COMPRESS); compress.bind(2, id.asset.value()); compress.bind(1, history_index - 1); compress.step(); } transaction.commit(); } catch (const bed::Db::error& err) { BE_LOG(VWarning) << "Database error while truncating window settings history!" << BE_LOG_NL << " Bed: " << id.bed << BE_LOG_NL << "WindowSettings ID: " << id.asset << BE_LOG_NL << " Exception: " << err.what() << BE_LOG_NL << " SQL: " << err.sql() << BE_LOG_END; } catch (const std::runtime_error& err) { BE_LOG(VWarning) << "Exception while truncating window settings history!" << BE_LOG_NL << " Bed: " << id.bed << BE_LOG_NL << "WindowSettings ID: " << id.asset << BE_LOG_NL << " Exception: " << err.what() << BE_LOG_END; } }
/////////////////////////////////////////////////////////////////////////////// /// \brief Updates the position and size properties of an existing saved /// WindowSettings object. /// /// \details The WindowSettings object will be found by looking at the AssetId /// found in window_settings.id. The history index updated will be /// the most recent one. Only the size and position properties will /// be updated, and only if window_settings.save_position_on_close /// is true. /// /// \param window_settings Determines where the WindowSettings are saved and /// the new position/size values to update. /// \return \c true if the size and position were updated successfully. /// /// \ingroup loading bool updateSavedPosition(const WindowSettings& window_settings) { if (!window_settings.save_position_on_close) return false; try { std::shared_ptr<bed::Bed> bed = bed::openWritable(window_settings.id.bed); if (!bed) throw std::runtime_error("Could not open bed for writing!"); bed::Db& db = bed->getDb(); if (db.getInt(BE_WND_WINDOW_SETTINGS_SQL_TABLE_EXISTS, 0) == 0) throw std::runtime_error("WindowSettings table does not exist!"); bed::Transaction transaction(db, bed::Transaction::Immediate); int history_index = 0; bed::Stmt latest(db, Id(BE_WND_WINDOW_SETTINGS_SQLID_LATEST_INDEX), BE_WND_WINDOW_SETTINGS_SQL_LATEST_INDEX); latest.bind(1, window_settings.id.asset.value()); if (latest.step()) history_index = latest.getInt(0); bed::Stmt save(db, Id(BE_WND_WINDOW_SETTINGS_SQLID_SAVE_POS), BE_WND_WINDOW_SETTINGS_SQL_SAVE_POS); save.bind(1, window_settings.position.x); save.bind(2, window_settings.position.y); save.bind(3, window_settings.size.x); save.bind(4, window_settings.size.y); save.bind(5, window_settings.id.asset.value()); save.bind(6, history_index); save.step(); transaction.commit(); return true; } catch (const bed::Db::error& err) { BE_LOG(VWarning) << "Database error while saving window position!" << BE_LOG_NL << " Bed: " << window_settings.id.bed << BE_LOG_NL << "WindowSettings ID: " << window_settings.id.asset << BE_LOG_NL << " Exception: " << err.what() << BE_LOG_NL << " SQL: " << err.sql() << BE_LOG_END; } catch (const std::runtime_error& err) { BE_LOG(VWarning) << "Exception while saving window position!" << BE_LOG_NL << " Bed: " << window_settings.id.bed << BE_LOG_NL << "WindowSettings ID: " << window_settings.id.asset << BE_LOG_NL << " Exception: " << err.what() << BE_LOG_END; } return false; }
bool Settings::setCVar(const std::string& name, const unsigned int& value) { CVar* cvar = cvarlist[name]; if ( cvar ) { if ( cvar->set(value) ) return true; else BE_LOG( "warning failed to set value for cvar '" << name << "': value must be >=" << cvarlist[name]->getIntMin() << " and <=" << cvarlist[name]->getIntMax() ); } else BE_LOG( "unknown cvar: '" << name << "'"); return false; }
/////////////////////////////////////////////////////////////////////////////// /// \brief Deletes the most recent history index of the specified /// WindowSettings object. /// /// \param id The AssetId defining the WindowSettings stack to revert. /// \return The next most recent history index for the WindowSettings object. /// If there is a problem deleting the most recent window settings or /// if there is no next most recent history index, a /// default-constructed WindowSettings object will be returned. /// /// \ingroup loading WindowSettings revertWindowSettings(const AssetId& id) { try { std::shared_ptr<bed::Bed> bed = bed::openWritable(id.bed); if (!bed) throw std::runtime_error("Could not open bed for writing!"); bed::Db& db = bed->getDb(); if (db.getInt(BE_WND_WINDOW_SETTINGS_SQL_TABLE_EXISTS, 0) == 0) throw std::runtime_error("WindowSettings table does not exist!"); bed::Transaction transaction(db, bed::Transaction::Immediate); int history_index = 0; bed::Stmt latest(db, BE_WND_WINDOW_SETTINGS_SQLID_LATEST_INDEX); latest.bind(1, id.asset.value()); if (latest.step()) history_index = latest.getInt(0); bed::Stmt remove(db, BE_WND_WINDOW_SETTINGS_SQLID_REVERT); remove.bind(1, id.asset.value()); remove.bind(2, history_index); remove.step(); transaction.commit(); return loadWindowSettings(*bed, id.asset); } catch (const bed::Db::error& err) { BE_LOG(VWarning) << "Database error while reverting window settings!" << BE_LOG_NL << " Bed: " << id.bed << BE_LOG_NL << "WindowSettings ID: " << id.asset << BE_LOG_NL << " Exception: " << err.what() << BE_LOG_NL << " SQL: " << err.sql() << BE_LOG_END; } catch (const std::runtime_error& err) { BE_LOG(VWarning) << "Exception while reverting window settings!" << BE_LOG_NL << " Bed: " << id.bed << BE_LOG_NL << "WindowSettings ID: " << id.asset << BE_LOG_NL << " Exception: " << err.what() << BE_LOG_END; } return WindowSettings(); }
void Settings::decreaseCVar(const std::string& name, const unsigned int& value) { CVar* cvar = cvarlist[name]; if ( cvar ) { cvar->decrease(value); std::stringstream v; v << cvar->getIntValue(); m_logBuffer->add(name + ": " + v.str()); } else BE_LOG( "unknown cvar: '" << name << "'" ); }
/////////////////////////////////////////////////////////////////////////////// /// \brief Saves a set of WindowSettings to a bed. /// /// \details The bed and Id that are saved to are determined by the id field /// of the WindowSettings object passed in. If the WindowSettings /// already exist in the bed, a new history index will be created. /// /// If there is a problem saving the WindowSettings, a warning will /// be emitted, and false will be returned. /// /// \param window_settings The WindowSettings to save. /// \return \c true if the WindowSettings were saved successfully. /// /// \ingroup loading bool saveWindowSettings(const WindowSettings& window_settings) { try { std::shared_ptr<bed::Bed> bed = bed::openWritable(window_settings.id.bed); if (!bed) throw std::runtime_error("Could not open bed for writing!"); bed::Db& db = bed->getDb(); bed::Transaction transaction(db, bed::Transaction::Immediate); int history_index = 0; if (db.getInt(BE_WND_WINDOW_SETTINGS_SQL_TABLE_EXISTS, 0) == 0) { db.exec(BE_WND_WINDOW_SETTINGS_SQL_CREATE_TABLE); } else { bed::Stmt latest(db, Id(BE_WND_WINDOW_SETTINGS_SQLID_LATEST_INDEX), BE_WND_WINDOW_SETTINGS_SQL_LATEST_INDEX); latest.bind(1, window_settings.id.asset.value()); if (latest.step()) history_index = latest.getInt(0); } bed::Stmt save(db, Id(BE_WND_WINDOW_SETTINGS_SQLID_SAVE), BE_WND_WINDOW_SETTINGS_SQL_SAVE); save.bind(1, window_settings.id.asset.value()); save.bind(2, history_index + 1); save.bind(3, static_cast<int>(window_settings.mode)); save.bind(4, window_settings.system_positioned); save.bind(5, window_settings.save_position_on_close); save.bind(6, window_settings.position.x); save.bind(7, window_settings.position.y); save.bind(8, window_settings.size.x); save.bind(9, window_settings.size.y); save.bind(10, window_settings.monitor_index); save.bind(11, window_settings.refresh_rate); save.bind(12, static_cast<int>(window_settings.v_sync)); save.bind(13, window_settings.msaa_level); save.bind(14, window_settings.red_bits); save.bind(15, window_settings.green_bits); save.bind(16, window_settings.blue_bits); save.bind(17, window_settings.alpha_bits); save.bind(18, window_settings.depth_bits); save.bind(19, window_settings.stencil_bits); save.bind(20, window_settings.srgb_capable); save.bind(21, window_settings.use_custom_gamma); save.bind(22, window_settings.custom_gamma); save.bind(23, window_settings.context_version_major); save.bind(24, window_settings.context_version_minor); save.bind(25, window_settings.forward_compatible_context ? 1 : 0); save.bind(26, window_settings.debug_context ? 1 : 0); save.bind(27, static_cast<int>(window_settings.context_profile_type)); save.step(); transaction.commit(); return true; } catch (const bed::Db::error& err) { BE_LOG(VWarning) << "Database error while saving window settings!" << BE_LOG_NL << " Bed: " << window_settings.id.bed << BE_LOG_NL << "WindowSettings ID: " << window_settings.id.asset << BE_LOG_NL << " Exception: " << err.what() << BE_LOG_NL << " SQL: " << err.sql() << BE_LOG_END; } catch (const std::runtime_error& err) { BE_LOG(VWarning) << "Exception while saving window settings!" << BE_LOG_NL << " Bed: " << window_settings.id.bed << BE_LOG_NL << "WindowSettings ID: " << window_settings.id.asset << BE_LOG_NL << " Exception: " << err.what() << BE_LOG_END; } return false; }
/////////////////////////////////////////////////////////////////////////////// /// \brief Load a set of WindowSettings from a bed. /// /// \details Each WindowSettings Id represents a stack of current and previous /// values. This allows the user to revert to previous settings if /// new settings are tried and not supported. /// /// \param bed The bed to load from. /// \param id The Id of the WindowSettings stack to load. /// /// \ingroup loading WindowSettings loadWindowSettings(bed::Bed& bed, const Id& id) { try { bed::StmtCache& cache = bed.getStmtCache(); bed::CachedStmt& stmt = cache.hold(Id(BE_WND_WINDOW_SETTINGS_SQLID_LOAD), BE_WND_WINDOW_SETTINGS_SQL_LOAD); stmt.bind(1, id.value()); if (stmt.step()) { WindowSettings ws; ws.id.bed = bed.getId(); ws.id.asset = id; ws.mode = static_cast<WindowSettings::Mode>(stmt.getInt(0)); ws.system_positioned = stmt.getBool(1); ws.save_position_on_close = stmt.getBool(2); ws.position.x = stmt.getInt(3); ws.position.y = stmt.getInt(4); ws.size.x = stmt.getInt(5); ws.size.y = stmt.getInt(6); ws.monitor_index = stmt.getInt(7); ws.refresh_rate = stmt.getUInt(8); ws.v_sync = static_cast<WindowSettings::VSyncMode>(stmt.getInt(9)); ws.msaa_level = stmt.getUInt(10); ws.red_bits = stmt.getUInt(11); ws.green_bits = stmt.getUInt(12); ws.blue_bits = stmt.getUInt(13); ws.alpha_bits = stmt.getUInt(14); ws.depth_bits = stmt.getUInt(15); ws.stencil_bits = stmt.getUInt(16); ws.srgb_capable = stmt.getBool(17); ws.use_custom_gamma = stmt.getBool(18); ws.custom_gamma = float(stmt.getDouble(19)); ws.context_version_major = stmt.getUInt(20); ws.context_version_minor = stmt.getUInt(21); ws.forward_compatible_context = stmt.getBool(22); ws.debug_context = stmt.getBool(23); ws.context_profile_type = static_cast<WindowSettings::ContextProfileType>(stmt.getInt(24)); return ws; } else throw std::runtime_error("WindowSettings record not found!"); } catch (const bed::Db::error& err) { BE_LOG(VWarning) << "Database error while loading window settings!" << BE_LOG_NL << " Bed: " << bed.getId() << BE_LOG_NL << "WindowSettings ID: " << id << BE_LOG_NL << " Exception: " << err.what() << BE_LOG_NL << " SQL: " << err.sql() << BE_LOG_END; } catch (const std::runtime_error& err) { BE_LOG(VWarning) << "Exception while loading window settings!" << BE_LOG_NL << " Bed: " << bed.getId() << BE_LOG_NL << "WindowSettings ID: " << id << BE_LOG_NL << " Exception: " << err.what() << BE_LOG_END; } return WindowSettings(); }
void Settings::loadProfile(const std::string& filename) { //m_logDebug << "::SETTINGS loading from '" << filename << "'\n"; BE_LOG("::SETTINGS loading from '" << filename << "'"); BeFile befileProfile; if ( m_filesystem.load( befileProfile, filename ) ) { // update profilename unsigned found = filename.find_last_of("/\\"); std::string file; //strip path if ( found != std::string::npos ) { file = filename.substr(found+1); } else { // no path, all are file file = filename; } //strip ".pro" found = file.rfind(".pro"); if (found != std::string::npos ) { m_profileName = file.erase(found); } else { m_profileName = file; } BE_LOG( "profile name is updated to '" << m_profileName << "'" ); std::string line; while ( befileProfile.getLine(line) ) { parseH.reset(); // trim spaces and comment lines parseH.trimWhitespaces(line); parseH.removeCommentlines(line); if ( !line.empty() ) { std::string sw = parseH.returnUntillStrip( ' ', line ); if ( !sw.empty() ) { if ( isCVar(sw) ) { while ( parseH.beginMatchesStrip( " ", line ) ) {}; std::string arg = parseH.returnUntillStrip( ' ', line ); if ( !setCVar(sw, arg) ) BE_ERROR("::SETTINGS could not set cvar '" << sw << "', value '" << arg << "'"); } else BE_LOG( "warning: unknown option in profile '" << sw << "'" ); } else BE_ERROR("::SETTINGS error: Option without an argument '" << line); } } } else BE_LOG( "warning: cannot open profile '" << filename << "'" ); }