| 1 | // Copyright (C) 2006-2009 by Tor Andersson. |
| 2 | // Copyright (C) 2010 by Ben Cressey. |
| 3 | // |
| 4 | // This file is part of Gargoyle. |
| 5 | // |
| 6 | // Gargoyle is free software; you can redistribute it and/or modify |
| 7 | // it under the terms of the GNU General Public License as published by |
| 8 | // the Free Software Foundation; either version 2 of the License, or |
| 9 | // (at your option) any later version. |
| 10 | // |
| 11 | // Gargoyle is distributed in the hope that it will be useful, |
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | // GNU General Public License for more details. |
| 15 | // |
| 16 | // You should have received a copy of the GNU General Public License |
| 17 | // along with Gargoyle; if not, write to the Free Software |
| 18 | // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | |
| 20 | #include <algorithm> |
| 21 | #include <array> |
| 22 | #include <cctype> |
| 23 | #include <cmath> |
| 24 | #include <cstdio> |
| 25 | #include <cstdlib> |
| 26 | #include <deque> |
| 27 | #include <fstream> |
| 28 | #include <functional> |
| 29 | #include <iomanip> |
| 30 | #include <ios> |
| 31 | #include <locale> |
| 32 | #include <sstream> |
| 33 | #include <stdexcept> |
| 34 | #include <string> |
| 35 | #include <unordered_map> |
| 36 | #include <vector> |
| 37 | |
| 38 | #if __cplusplus >= 201703L |
| 39 | #include <filesystem> |
| 40 | #endif |
| 41 | |
| 42 | #ifdef _WIN32 |
| 43 | #define WIN32_LEAN_AND_MEAN |
| 44 | #ifndef NOMINMAX |
| 45 | #define NOMINMAX |
| 46 | #endif |
| 47 | #include <shlwapi.h> |
| 48 | #else |
| 49 | #include <fnmatch.h> |
| 50 | #endif |
| 51 | |
| 52 | #ifdef __HAIKU__ |
| 53 | #include <FindDirectory.h> |
| 54 | #endif |
| 55 | |
| 56 | #include "format.h" |
| 57 | #include "optional.hpp" |
| 58 | |
| 59 | #include "glk.h" |
| 60 | #include "glkstart.h" |
| 61 | #include "garglk.h" |
| 62 | |
| 63 | #include GARGLKINI_H |
| 64 | |
| 65 | struct ConfigError : public std::exception { |
| 66 | explicit ConfigError(std::string message_) : message(std::move(message_)) { |
| 67 | } |
| 68 | |
| 69 | std::string message; |
| 70 | }; |
| 71 | |
| 72 | std::string garglk::ConfigFile::format_type() const { |
| 73 | std::string status = ""; |
| 74 | std::ifstream f(path); |
| 75 | if (!f.is_open()) { |
| 76 | status = ", non-existent"; |
| 77 | } |
| 78 | |
| 79 | switch (type) { |
| 80 | case Type::System: |
| 81 | return Format("[system{}]", status); |
| 82 | case Type::User: |
| 83 | return Format("[user{}]", status); |
| 84 | case Type::PerGame: |
| 85 | return Format("[game specific{}]", status); |
| 86 | default: |
| 87 | return ""; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | double gli_backingscalefactor = 1.0; |
| 92 | double gli_zoom = 1.0; |
| 93 | |
| 94 | FontFiles gli_conf_prop, gli_conf_mono; |
| 95 | |
| 96 | std::string gli_conf_monofont = DEFAULT_MONO_FONT; |
| 97 | std::string gli_conf_propfont = DEFAULT_PROP_FONT; |
| 98 | double gli_conf_monosize = 12.6; // good size for Gargoyle Mono |
| 99 | double gli_conf_propsize = 14.7; // good size for Gargoyle Serif |
| 100 | |
| 101 | Styles gli_tstyles{{ |
| 102 | {FontFace::propr(), Color(0xff, 0xff, 0xff), Color(0x00, 0x00, 0x00), false}, // Normal |
| 103 | {FontFace::propi(), Color(0xff, 0xff, 0xff), Color(0x00, 0x00, 0x00), false}, // Emphasized |
| 104 | {FontFace::monor(), Color(0xff, 0xff, 0xff), Color(0x00, 0x00, 0x00), false}, // Preformatted |
| 105 | {FontFace::propb(), Color(0xff, 0xff, 0xff), Color(0x00, 0x00, 0x00), false}, // Header |
| 106 | {FontFace::propb(), Color(0xff, 0xff, 0xff), Color(0x00, 0x00, 0x00), false}, // Subheader |
| 107 | {FontFace::propz(), Color(0xff, 0xff, 0xff), Color(0x00, 0x00, 0x00), false}, // Alert |
| 108 | {FontFace::propi(), Color(0xff, 0xff, 0xff), Color(0x00, 0x00, 0x00), false}, // Note |
| 109 | {FontFace::propr(), Color(0xff, 0xff, 0xff), Color(0x00, 0x00, 0x00), false}, // BlockQuote |
| 110 | {FontFace::propb(), Color(0xff, 0xff, 0xff), Color(0x00, 0x00, 0x00), false}, // Input |
| 111 | {FontFace::propr(), Color(0xff, 0xff, 0xff), Color(0x00, 0x00, 0x00), false}, // User1 |
| 112 | {FontFace::propr(), Color(0xff, 0xff, 0xff), Color(0x00, 0x00, 0x00), false}, // User2 |
| 113 | }}; |
| 114 | |
| 115 | Styles gli_gstyles{{ |
| 116 | {FontFace::monor(), Color(0xff, 0xff, 0xff), Color(0x60, 0x60, 0x60), false}, // Normal |
| 117 | {FontFace::monoi(), Color(0xff, 0xff, 0xff), Color(0x60, 0x60, 0x60), false}, // Emphasized |
| 118 | {FontFace::monor(), Color(0xff, 0xff, 0xff), Color(0x60, 0x60, 0x60), false}, // Preformatted |
| 119 | {FontFace::monob(), Color(0xff, 0xff, 0xff), Color(0x60, 0x60, 0x60), false}, // Header |
| 120 | {FontFace::monob(), Color(0xff, 0xff, 0xff), Color(0x60, 0x60, 0x60), false}, // Subheader |
| 121 | {FontFace::monoz(), Color(0xff, 0xff, 0xff), Color(0x60, 0x60, 0x60), false}, // Alert |
| 122 | {FontFace::monoi(), Color(0xff, 0xff, 0xff), Color(0x60, 0x60, 0x60), false}, // Note |
| 123 | {FontFace::monor(), Color(0xff, 0xff, 0xff), Color(0x60, 0x60, 0x60), false}, // BlockQuote |
| 124 | {FontFace::monob(), Color(0xff, 0xff, 0xff), Color(0x60, 0x60, 0x60), false}, // Input |
| 125 | {FontFace::monor(), Color(0xff, 0xff, 0xff), Color(0x60, 0x60, 0x60), false}, // User1 |
| 126 | {FontFace::monor(), Color(0xff, 0xff, 0xff), Color(0x60, 0x60, 0x60), false}, // User2 |
| 127 | }}; |
| 128 | |
| 129 | Styles gli_tstyles_def = gli_tstyles; |
| 130 | Styles gli_gstyles_def = gli_gstyles; |
| 131 | |
| 132 | std::vector<garglk::ConfigFile> garglk::all_configs; |
| 133 | |
| 134 | static FontFace font2idx(const std::string &font) |
| 135 | { |
| 136 | const static std::unordered_map<std::string, FontFace> facemap = { |
| 137 | {"monor", FontFace::monor()}, |
| 138 | {"monob", FontFace::monob()}, |
| 139 | {"monoi", FontFace::monoi()}, |
| 140 | {"monoz", FontFace::monoz()}, |
| 141 | {"propr", FontFace::propr()}, |
| 142 | {"propb", FontFace::propb()}, |
| 143 | {"propi", FontFace::propi()}, |
| 144 | {"propz", FontFace::propz()}, |
| 145 | }; |
| 146 | |
| 147 | try { |
| 148 | return facemap.at(font); |
| 149 | } catch (const std::out_of_range &) { |
| 150 | throw ConfigError(Format("invalid font: {}", font)); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | double gli_conf_gamma = 1.0; |
| 155 | |
| 156 | Color gli_window_color(0xff, 0xff, 0xff); |
| 157 | Color gli_caret_color(0x00, 0x00, 0x00); |
| 158 | Color gli_border_color(0x00, 0x00, 0x00); |
| 159 | Color gli_more_color(0x00, 0x60, 0x00); |
| 160 | Color gli_link_color(0x00, 0x00, 0x60); |
| 161 | |
| 162 | Color gli_window_save(0xff, 0xff, 0xff); |
| 163 | Color gli_caret_save(0x00, 0x00, 0x00); |
| 164 | Color gli_border_save(0x00, 0x00, 0x00); |
| 165 | Color gli_more_save(0x00, 0x60, 0x00); |
| 166 | Color gli_link_save(0x00, 0x00, 0x60); |
| 167 | |
| 168 | nonstd::optional<Color> gli_override_fg; |
| 169 | nonstd::optional<Color> gli_override_bg; |
| 170 | bool gli_override_reverse = false; |
| 171 | |
| 172 | static std::string base_more_prompt = "— more —"; |
| 173 | std::vector<glui32> gli_more_prompt; |
| 174 | glui32 gli_more_prompt_len; |
| 175 | int gli_more_align = 0; |
| 176 | FontFace gli_more_font = FontFace::propb(); |
| 177 | |
| 178 | Color gli_scroll_bg(0xb0, 0xb0, 0xb0); |
| 179 | Color gli_scroll_fg(0x80, 0x80, 0x80); |
| 180 | int gli_scroll_width = 0; |
| 181 | |
| 182 | int gli_caret_shape = 2; |
| 183 | bool gli_underline_hyperlinks = true; |
| 184 | |
| 185 | bool gli_conf_lcd = true; |
| 186 | std::array<unsigned char, 5> gli_conf_lcd_weights = {28, 56, 85, 56, 28}; |
| 187 | |
| 188 | int gli_wmarginx = 20; |
| 189 | int gli_wmarginx_save = gli_wmarginx; |
| 190 | int gli_wmarginy = 20; |
| 191 | int gli_wmarginy_save = gli_wmarginy; |
| 192 | int gli_wpaddingx = 0; |
| 193 | int gli_wpaddingy = 0; |
| 194 | int gli_wborderx = 0; |
| 195 | int gli_wbordery = 0; |
| 196 | int gli_tmarginx = 0; |
| 197 | int gli_tmarginy = 7; |
| 198 | |
| 199 | |
| 200 | int gli_cols = 60; |
| 201 | int gli_rows = 25; |
| 202 | |
| 203 | bool gli_conf_lockcols = false; |
| 204 | bool gli_conf_lockrows = false; |
| 205 | |
| 206 | bool gli_conf_save_window_size = false; |
| 207 | bool gli_conf_save_window_location = false; |
| 208 | |
| 209 | double gli_conf_propaspect = 1.0; |
| 210 | double gli_conf_monoaspect = 1.0; |
| 211 | |
| 212 | int gli_baseline = 15; |
| 213 | int gli_leading = 20; |
| 214 | |
| 215 | bool gli_conf_justify = false; |
| 216 | int gli_conf_quotes = 1; |
| 217 | int gli_conf_dashes = 1; |
| 218 | int gli_conf_spaces = 1; |
| 219 | bool gli_conf_caps = false; |
| 220 | |
| 221 | bool gli_conf_graphics = true; |
| 222 | bool gli_conf_sound = true; |
| 223 | bool gli_conf_speak = false; |
| 224 | bool gli_conf_speak_input = false; |
| 225 | std::string gli_conf_speak_language; |
| 226 | |
| 227 | #ifdef GARGLK_DEFAULT_SOUNDFONT |
| 228 | std::deque<std::string> gli_conf_soundfonts = {GARGLK_DEFAULT_SOUNDFONT}; |
| 229 | #else |
| 230 | std::deque<std::string> gli_conf_soundfonts; |
| 231 | #endif |
| 232 | |
| 233 | bool gli_conf_fluidsynth_chorus = true; |
| 234 | bool gli_conf_fluidsynth_reverb = true; |
| 235 | |
| 236 | bool gli_conf_fullscreen = false; |
| 237 | |
| 238 | bool gli_wait_on_quit = true; |
| 239 | |
| 240 | bool gli_conf_stylehint = true; |
| 241 | bool gli_conf_safeclicks = false; |
| 242 | |
| 243 | bool gli_conf_per_game_config = true; |
| 244 | |
| 245 | GamedataLocation gli_conf_gamedata_location = GamedataLocation::Default; |
| 246 | |
| 247 | Scaler gli_conf_scaler = Scaler::None; |
| 248 | |
| 249 | std::string garglk::downcase(const std::string &string) |
| 250 | { |
| 251 | std::string lowered; |
| 252 | |
| 253 | for (unsigned char c : string) { |
| 254 | lowered.push_back(std::tolower(c)); |
| 255 | } |
| 256 | |
| 257 | return lowered; |
| 258 | } |
| 259 | |
| 260 | static void parsecolor(const std::string &str, Color &rgb) |
| 261 | { |
| 262 | try { |
| 263 | rgb = gli_parse_color(str); |
| 264 | } catch (const std::runtime_error &) { |
| 265 | throw ConfigError(Format("invalid color: {}", str)); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | // Return a vector of all possible config files. This is in order of |
| 270 | // highest priority to lowest (i.e. earlier entries should take |
| 271 | // precedence over later entries). |
| 272 | // |
| 273 | // The following is the list of locations tried: |
| 274 | // |
| 275 | // 1. Name of game file with extension replaced by .ini (e.g. zork1.z3 |
| 276 | // becomes zork1.ini) |
| 277 | // 2. <directory containing game file>/garglk.ini |
| 278 | // 3. Platform-specific locations: |
| 279 | // $XDG_CONFIG_HOME/garglk.ini or $HOME/.config/garglk.ini (Unix only) |
| 280 | // $HOME/.garglkrc (Unix only) |
| 281 | // <user settings directory>/Gargoyle (Haiku only) |
| 282 | // %APPDATA%/Gargoyle/garglk.ini (Windows only) |
| 283 | // <current directory>/garglk.ini (Windows only) |
| 284 | // $HOME/garglk.ini (macOS only) |
| 285 | // 4. $GARGLK_RESOURCES/garglk.ini (macOS only) |
| 286 | // 5. /etc/garglk.ini (or other location set at build time, Unix only) |
| 287 | // 6. <directory containing gargoyle/interpreter executable>/garglk.ini (Windows only) |
| 288 | // |
| 289 | // gamepath is the path to the game file being run |
| 290 | std::vector<garglk::ConfigFile> garglk::configs(const nonstd::optional<std::string> &gamepath = nonstd::nullopt) |
| 291 | { |
| 292 | std::vector<ConfigFile> configs; |
| 293 | if (gamepath.has_value()) { |
| 294 | #if __cplusplus >= 201703L |
| 295 | // game file .ini |
| 296 | std::filesystem::path config(*gamepath); |
| 297 | config.replace_extension(".ini"); |
| 298 | configs.emplace_back(config.string(), ConfigFile::Type::PerGame); |
| 299 | |
| 300 | // game directory .ini |
| 301 | config = *gamepath; |
| 302 | config = config.parent_path() / "garglk.ini"; |
| 303 | configs.emplace_back(config.string(), ConfigFile::Type::PerGame); |
| 304 | #else |
| 305 | std::string config; |
| 306 | |
| 307 | // game file .ini |
| 308 | config = *gamepath; |
| 309 | auto dot = config.rfind('.'); |
| 310 | if (dot != std::string::npos) { |
| 311 | config.replace(dot, std::string::npos, ".ini"); |
| 312 | } else { |
| 313 | config += ".ini"; |
| 314 | } |
| 315 | |
| 316 | configs.emplace_back(config, ConfigFile::Type::PerGame); |
| 317 | |
| 318 | // game directory .ini |
| 319 | config = *gamepath; |
| 320 | auto slash = config.find_last_of("/\\"); |
| 321 | if (slash != std::string::npos) { |
| 322 | config.replace(slash + 1, std::string::npos, "garglk.ini"); |
| 323 | } else { |
| 324 | config = "garglk.ini"; |
| 325 | } |
| 326 | |
| 327 | configs.emplace_back(config, ConfigFile::Type::PerGame); |
| 328 | #endif |
| 329 | } |
| 330 | |
| 331 | #if defined(__HAIKU__) |
| 332 | char settings_dir[PATH_MAX + 1]; |
| 333 | if (find_directory(B_USER_SETTINGS_DIRECTORY, -1, false, settings_dir, sizeof settings_dir) == B_OK) { |
| 334 | configs.emplace_back(Format("{}/Gargoyle", settings_dir), ConfigFile::Type::User); |
| 335 | } |
| 336 | #elif defined(_WIN32) |
| 337 | // $APPDATA/Gargoyle/garglk.ini (Windows only). This has a higher |
| 338 | // priority than $PWD/garglk.ini since it's a more "proper" location. |
| 339 | const char *appdata = std::getenv("APPDATA"); |
| 340 | if (appdata != nullptr) { |
| 341 | configs.emplace_back(Format("{}/Gargoyle/garglk.ini", appdata), ConfigFile::Type::User); |
| 342 | } |
| 343 | |
| 344 | // current directory .ini |
| 345 | // Historically this has been the location of garglk.ini on Windows, |
| 346 | // so treat it as a user config there. |
| 347 | configs.emplace_back("garglk.ini", ConfigFile::Type::User); |
| 348 | #else |
| 349 | |
| 350 | const char *home = std::getenv("HOME"); |
| 351 | |
| 352 | #ifdef __APPLE__ |
| 353 | // On macOS, when the config file is edited via the UI, it is placed in |
| 354 | // $HOME/garglk.ini. At some point this probably should move to somewhere in |
| 355 | // $HOME/Library, but for now, make sure this config file is used. |
| 356 | if (home != nullptr) { |
| 357 | configs.emplace_back(Format("{}/garglk.ini", home), ConfigFile::Type::User); |
| 358 | } |
| 359 | #endif |
| 360 | |
| 361 | // XDG Base Directory Specification |
| 362 | std::string xdg_path; |
| 363 | const char *xdg = std::getenv("XDG_CONFIG_HOME"); |
| 364 | if (xdg != nullptr && xdg[0] == '/') { |
| 365 | xdg_path = xdg; |
| 366 | } else if (home != nullptr) { |
| 367 | xdg_path = Format("{}/.config", home); |
| 368 | } |
| 369 | |
| 370 | if (!xdg_path.empty()) { |
| 371 | configs.emplace_back(Format("{}/garglk.ini", xdg_path), ConfigFile::Type::User); |
| 372 | } |
| 373 | |
| 374 | // $HOME/.garglkrc |
| 375 | if (home != nullptr) { |
| 376 | configs.emplace_back(Format("{}/.garglkrc", home), ConfigFile::Type::User); |
| 377 | } |
| 378 | #endif |
| 379 | |
| 380 | #ifdef __APPLE__ |
| 381 | // macOS sets $GARGLK_RESOURCES to the bundle directory containing a |
| 382 | // default garglk.ini. |
| 383 | const char *garglkini = std::getenv("GARGLK_RESOURCES"); |
| 384 | if (garglkini != nullptr) { |
| 385 | configs.emplace_back(Format("{}/garglk.ini", garglkini), ConfigFile::Type::System); |
| 386 | } |
| 387 | #endif |
| 388 | |
| 389 | #ifdef GARGLKINI |
| 390 | // system directory |
| 391 | configs.emplace_back(GARGLKINI, ConfigFile::Type::System); |
| 392 | #endif |
| 393 | |
| 394 | #ifdef _WIN32 |
| 395 | // install directory |
| 396 | auto exedir = garglk::winappdir(); |
| 397 | if (exedir.has_value()) { |
| 398 | configs.emplace_back(Format("{}/garglk.ini", *exedir), ConfigFile::Type::System); |
| 399 | } |
| 400 | #endif |
| 401 | |
| 402 | return configs; |
| 403 | } |
| 404 | |
| 405 | std::string garglk::user_config() |
| 406 | { |
| 407 | auto cfgs = configs(); |
| 408 | |
| 409 | // Filter out non-user configs. |
| 410 | cfgs.erase(std::remove_if(cfgs.begin(), |
| 411 | cfgs.end(), |
| 412 | [](const ConfigFile &config) { |
| 413 | return config.type != ConfigFile::Type::User; |
| 414 | }), |
| 415 | cfgs.end()); |
| 416 | |
| 417 | if (cfgs.empty()) { |
| 418 | throw std::runtime_error("No valid configuration files found."); |
| 419 | } |
| 420 | |
| 421 | // Find first user config which already exists, if any. |
| 422 | auto cfg = std::find_if(cfgs.begin(), cfgs.end(), [](const ConfigFile &config) { |
| 423 | return std::ifstream(config.path).good(); |
| 424 | }); |
| 425 | |
| 426 | if (cfg != cfgs.end()) { |
| 427 | return cfg->path; |
| 428 | } |
| 429 | |
| 430 | // No config exists, so create the highest-priority config. |
| 431 | auto path = cfgs.front().path; |
| 432 | |
| 433 | // If building with C++17, ensure the parent directory exists. This |
| 434 | // is difficult to do portably before C++17, so just don't do it. |
| 435 | #if __cplusplus >= 201703L |
| 436 | std::filesystem::path fspath(path); |
| 437 | try { |
| 438 | if (!fspath.parent_path().empty()) { |
| 439 | std::filesystem::create_directories(fspath.parent_path()); |
| 440 | } |
| 441 | } catch (const std::runtime_error &e) { |
| 442 | throw std::runtime_error(Format("Unable to create parent directory for configuration file {}: {}", path, e.what())); |
| 443 | } |
| 444 | #endif |
| 445 | |
| 446 | std::ofstream f(path); |
| 447 | if (!f.is_open()) { |
| 448 | throw std::runtime_error(Format("Unable to open configuration file {} for writing.", path)); |
| 449 | } |
| 450 | |
| 451 | f << garglkini; |
| 452 | |
| 453 | if (f.bad()) { |
| 454 | std::remove(path.c_str()); |
| 455 | throw std::runtime_error(Format("Error writing to configuration file {}.", path)); |
| 456 | } |
| 457 | |
| 458 | return path; |
| 459 | } |
| 460 | |
| 461 | void garglk::config_entries(const std::string &fname, bool accept_bare, const std::vector<std::string> &matches, const std::function<void(const std::string &cmd, const std::string &arg, int lineno)> &callback) |
| 462 | { |
| 463 | std::string line; |
| 464 | bool accept = accept_bare; |
| 465 | |
| 466 | std::ifstream f(fname); |
| 467 | if (!f.is_open()) { |
| 468 | return; |
| 469 | } |
| 470 | |
| 471 | int lineno = 0; |
| 472 | while (std::getline(f, line)) { |
| 473 | lineno++; |
| 474 | |
| 475 | // Strip leading whitespace |
| 476 | line.erase(line.begin(), std::find_if(line.begin(), line.end(), [](unsigned char c) { |
| 477 | return !std::isspace(c); |
| 478 | })); |
| 479 | |
| 480 | auto comment = line.find('#'); |
| 481 | if (comment != std::string::npos) { |
| 482 | line.erase(comment); |
| 483 | } |
| 484 | line.erase(line.find_last_not_of(" \t\r") + 1); |
| 485 | |
| 486 | if (line.empty()) { |
| 487 | continue; |
| 488 | } |
| 489 | |
| 490 | if (line[0] == '[' && line.back() == ']') { |
| 491 | accept = std::any_of(matches.begin(), matches.end(), [&line](const std::string &match) { |
| 492 | std::istringstream s(line.substr(1, line.size() - 2)); |
| 493 | std::string pattern; |
| 494 | while (s >> std::quoted(pattern)) { |
| 495 | #ifdef _WIN32 |
| 496 | if (PathMatchSpecA(garglk::downcase(match).c_str(), garglk::downcase(pattern).c_str())) { |
| 497 | #else |
| 498 | if (fnmatch(garglk::downcase(pattern).c_str(), garglk::downcase(match).c_str(), 0) == 0) { |
| 499 | #endif |
| 500 | return true; |
| 501 | } |
| 502 | } |
| 503 | return false; |
| 504 | }); |
| 505 | continue; |
| 506 | } |
| 507 | |
| 508 | if (!accept) { |
| 509 | continue; |
| 510 | } |
| 511 | |
| 512 | std::string cmd; |
| 513 | std::istringstream linestream(line); |
| 514 | |
| 515 | if (linestream >> cmd) { |
| 516 | std::string arg; |
| 517 | std::getline(linestream >> std::ws, arg); |
| 518 | |
| 519 | if (linestream) { |
| 520 | callback(cmd, arg, lineno); |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | template <typename T> |
| 527 | T parse_number(const std::string &s) |
| 528 | { |
| 529 | // C++17: std::from_chars; but check compatibility, as it seems some |
| 530 | // C++17 implementations didn't (initially) support doubles. |
| 531 | std::istringstream ss(s); |
| 532 | ss.imbue(std::locale::classic()); |
| 533 | |
| 534 | T val; |
| 535 | |
| 536 | // Force decimal: this is for end-users, not programmers. If a user |
| 537 | // types 09, he probably expects 9, not an error; or 010 to mean 10 |
| 538 | // rather than 8. And in the config file, there's no place where hex |
| 539 | // is the "natural" representation. |
| 540 | ss >> std::dec >> val; |
| 541 | |
| 542 | // This check isn't completely perfect; a string like "+" will be |
| 543 | // considered out of range rather than invalid. istringstream |
| 544 | // doesn't have discrete enough error reporting to disginguish "all |
| 545 | // valid characters but not a complete number" from "all valid |
| 546 | // characters but out of range". This will be fixed with |
| 547 | // std::from_chars, if that's ever implemented, but at least an |
| 548 | // error will be given, even if it's slightly misleading. |
| 549 | if (s.empty() || !ss.eof()) { |
| 550 | throw ConfigError(Format("invalid number: {}", s)); |
| 551 | } else if (ss.fail() && ss.eof()) { |
| 552 | throw ConfigError(Format("out of range: {}", s)); |
| 553 | } |
| 554 | |
| 555 | return val; |
| 556 | } |
| 557 | |
| 558 | double parse_double(std::string s) |
| 559 | { |
| 560 | // Earlier versions of Gargoyle used std::stod which is locale |
| 561 | // aware; and while Gargoyle doesn't call setlocale(), it can't stop |
| 562 | // libraries from doing so, meaning the user's locale was consulted |
| 563 | // for the decimal separator character. The default config uses a |
| 564 | // period no matter what, meaning parse errors for users whose |
| 565 | // locale prefers a comma as the separator. |
| 566 | // |
| 567 | // Now parsing is always done in the classic (C) locale, but since |
| 568 | // config files out there might contain commas (and just because |
| 569 | // it's more user friendly in general), treat commas as periods. |
| 570 | std::replace(s.begin(), s.end(), ',', '.'); |
| 571 | |
| 572 | return parse_number<double>(s); |
| 573 | } |
| 574 | |
| 575 | int parse_int(const std::string &s) |
| 576 | { |
| 577 | return parse_number<int>(s); |
| 578 | } |
| 579 | |
| 580 | template <typename T> |
| 581 | const T &config_range(const T &value, const T &min, const T &max) |
| 582 | { |
| 583 | if (value < min || value > max) { |
| 584 | throw ConfigError(Format("invalid value: {} (must be in the range {}-{})", value, min, max)); |
| 585 | } |
| 586 | |
| 587 | return value; |
| 588 | } |
| 589 | |
| 590 | template <typename T> |
| 591 | constexpr const T &config_atleast(const T &value, const T &min) |
| 592 | { |
| 593 | if (value < min) { |
| 594 | throw ConfigError(Format("invalid value: {} (must be at least {})", value, min)); |
| 595 | } |
| 596 | |
| 597 | return value; |
| 598 | } |
| 599 | |
| 600 | static void readoneconfig(const std::string &fname, const std::string &argv0, const nonstd::optional<std::string> &gamefile) |
| 601 | { |
| 602 | std::vector<std::string> matches = {argv0}; |
| 603 | if (gamefile.has_value()) { |
| 604 | matches.push_back(*gamefile); |
| 605 | } |
| 606 | |
| 607 | garglk::config_entries(fname, true, matches, [&fname](const std::string &cmd, const std::string &arg, int lineno) { |
| 608 | auto asbool = [](const std::string &arg) { |
| 609 | if (arg == "0" || arg == "1") { |
| 610 | return arg == "1"; |
| 611 | } |
| 612 | |
| 613 | throw ConfigError(Format("invalid value: {} (must be 0 or 1)", arg)); |
| 614 | }; |
| 615 | |
| 616 | auto warn = [&fname, &lineno](const std::string &msg) { |
| 617 | std::cerr << Format("{}:{}: {}", fname, lineno, msg) << std::endl; |
| 618 | }; |
| 619 | |
| 620 | try { |
| 621 | if (cmd == "moreprompt") { |
| 622 | base_more_prompt = arg; |
| 623 | } else if (cmd == "morecolor") { |
| 624 | parsecolor(arg, gli_more_color); |
| 625 | parsecolor(arg, gli_more_save); |
| 626 | } else if (cmd == "morefont") { |
| 627 | gli_more_font = font2idx(arg); |
| 628 | } else if (cmd == "morealign") { |
| 629 | gli_more_align = config_range(parse_int(arg), 0, 2); |
| 630 | } else if (cmd == "monoaspect") { |
| 631 | gli_conf_monoaspect = parse_double(arg); |
| 632 | } else if (cmd == "propaspect") { |
| 633 | gli_conf_propaspect = parse_double(arg); |
| 634 | } else if (cmd == "monosize") { |
| 635 | gli_conf_monosize = parse_double(arg); |
| 636 | } else if (cmd == "monor") { |
| 637 | gli_conf_mono.r.override = arg; |
| 638 | } else if (cmd == "monob") { |
| 639 | gli_conf_mono.b.override = arg; |
| 640 | } else if (cmd == "monoi") { |
| 641 | gli_conf_mono.i.override = arg; |
| 642 | } else if (cmd == "monoz") { |
| 643 | gli_conf_mono.z.override = arg; |
| 644 | } else if (cmd == "monofont") { |
| 645 | gli_conf_monofont = arg; |
| 646 | } else if (cmd == "propsize") { |
| 647 | gli_conf_propsize = parse_double(arg); |
| 648 | } else if (cmd == "propr") { |
| 649 | gli_conf_prop.r.override = arg; |
| 650 | } else if (cmd == "propb") { |
| 651 | gli_conf_prop.b.override = arg; |
| 652 | } else if (cmd == "propi") { |
| 653 | gli_conf_prop.i.override = arg; |
| 654 | } else if (cmd == "propz") { |
| 655 | gli_conf_prop.z.override = arg; |
| 656 | } else if (cmd == "propfont") { |
| 657 | gli_conf_propfont = arg; |
| 658 | } else if (cmd == "leading") { |
| 659 | gli_leading = config_atleast(parse_int(arg), 1); |
| 660 | } else if (cmd == "baseline") { |
| 661 | gli_baseline = config_atleast(parse_int(arg), 0); |
| 662 | } else if (cmd == "rows") { |
| 663 | gli_rows = config_atleast(parse_int(arg), 1); |
| 664 | } else if (cmd == "cols") { |
| 665 | gli_cols = config_atleast(parse_int(arg), 1); |
| 666 | } else if (cmd == "minrows") { |
| 667 | int r = config_atleast(parse_int(arg), 0); |
| 668 | if (gli_rows < r) { |
| 669 | gli_rows = r; |
| 670 | } |
| 671 | } else if (cmd == "maxrows") { |
| 672 | int r = config_atleast(parse_int(arg), 0); |
| 673 | if (gli_rows > r) { |
| 674 | gli_rows = r; |
| 675 | } |
| 676 | } else if (cmd == "mincols") { |
| 677 | int r = config_atleast(parse_int(arg), 0); |
| 678 | if (gli_cols < r) { |
| 679 | gli_cols = r; |
| 680 | } |
| 681 | } else if (cmd == "maxcols") { |
| 682 | int r = config_atleast(parse_int(arg), 0); |
| 683 | if (gli_cols > r) { |
| 684 | gli_cols = r; |
| 685 | } |
| 686 | } else if (cmd == "lockrows") { |
| 687 | gli_conf_lockrows = asbool(arg); |
| 688 | } else if (cmd == "lockcols") { |
| 689 | gli_conf_lockcols = asbool(arg); |
| 690 | } else if (cmd == "save_window") { |
| 691 | std::stringstream ss(arg); |
| 692 | std::string entry; |
| 693 | |
| 694 | while (ss >> entry) { |
| 695 | entry = garglk::downcase(entry); |
| 696 | if (entry == "size") { |
| 697 | gli_conf_save_window_size = true; |
| 698 | } else if (entry == "location") { |
| 699 | gli_conf_save_window_location = true; |
| 700 | } else { |
| 701 | throw ConfigError(Format("invalid value: {}", entry)); |
| 702 | } |
| 703 | } |
| 704 | } else if (cmd == "wmarginx") { |
| 705 | gli_wmarginx = config_atleast(parse_int(arg), 0); |
| 706 | gli_wmarginx_save = gli_wmarginx; |
| 707 | } else if (cmd == "wmarginy") { |
| 708 | gli_wmarginy = config_atleast(parse_int(arg), 0); |
| 709 | gli_wmarginy_save = gli_wmarginy; |
| 710 | } else if (cmd == "wpaddingx") { |
| 711 | gli_wpaddingx = config_atleast(parse_int(arg), 0); |
| 712 | } else if (cmd == "wpaddingy") { |
| 713 | gli_wpaddingy = config_atleast(parse_int(arg), 0); |
| 714 | } else if (cmd == "wborderx") { |
| 715 | gli_wborderx = config_atleast(parse_int(arg), 0); |
| 716 | } else if (cmd == "wbordery") { |
| 717 | gli_wbordery = config_atleast(parse_int(arg), 0); |
| 718 | } else if (cmd == "tmarginx") { |
| 719 | gli_tmarginx = config_atleast(parse_int(arg), 0); |
| 720 | } else if (cmd == "tmarginy") { |
| 721 | gli_tmarginy = config_atleast(parse_int(arg), 0); |
| 722 | } else if (cmd == "gamma") { |
| 723 | gli_conf_gamma = config_atleast(parse_double(arg), 0.0); |
| 724 | } else if (cmd == "caretcolor") { |
| 725 | parsecolor(arg, gli_caret_color); |
| 726 | parsecolor(arg, gli_caret_save); |
| 727 | } else if (cmd == "linkcolor") { |
| 728 | parsecolor(arg, gli_link_color); |
| 729 | parsecolor(arg, gli_link_save); |
| 730 | } else if (cmd == "bordercolor") { |
| 731 | parsecolor(arg, gli_border_color); |
| 732 | parsecolor(arg, gli_border_save); |
| 733 | } else if (cmd == "windowcolor") { |
| 734 | parsecolor(arg, gli_window_color); |
| 735 | parsecolor(arg, gli_window_save); |
| 736 | } else if (cmd == "lcd") { |
| 737 | gli_conf_lcd = asbool(arg); |
| 738 | } else if (cmd == "lcdfilter") { |
| 739 | if (!garglk::set_lcdfilter(arg)) { |
| 740 | throw ConfigError(Format("invalid value: {}", arg)); |
| 741 | } |
| 742 | } else if (cmd == "lcdweights") { |
| 743 | std::istringstream argstream(arg); |
| 744 | std::string weight; |
| 745 | std::vector<unsigned char> weights; |
| 746 | |
| 747 | while (argstream >> weight) { |
| 748 | weights.push_back(config_range(parse_int(weight), 0, 255)); |
| 749 | } |
| 750 | |
| 751 | if (weights.size() == gli_conf_lcd_weights.size()) { |
| 752 | std::copy(weights.begin(), weights.end(), gli_conf_lcd_weights.begin()); |
| 753 | } else { |
| 754 | throw ConfigError(Format("invalid value: {} (must be 5 bytes)", arg)); |
| 755 | } |
| 756 | } else if (cmd == "caretshape") { |
| 757 | gli_caret_shape = config_range(parse_int(arg), 0, 4); |
| 758 | } else if (cmd == "linkstyle") { |
| 759 | gli_underline_hyperlinks = asbool(arg); |
| 760 | } else if (cmd == "scrollwidth") { |
| 761 | gli_scroll_width = config_atleast(parse_int(arg), 0); |
| 762 | } else if (cmd == "scrollbg") { |
| 763 | parsecolor(arg, gli_scroll_bg); |
| 764 | } else if (cmd == "scrollfg") { |
| 765 | parsecolor(arg, gli_scroll_fg); |
| 766 | } else if (cmd == "justify") { |
| 767 | gli_conf_justify = asbool(arg); |
| 768 | } else if (cmd == "quotes") { |
| 769 | gli_conf_quotes = config_range(parse_int(arg), 0, 2); |
| 770 | } else if (cmd == "dashes") { |
| 771 | gli_conf_dashes = config_range(parse_int(arg), 0, 2); |
| 772 | } else if (cmd == "spaces") { |
| 773 | gli_conf_spaces = config_range(parse_int(arg), 0, 2); |
| 774 | } else if (cmd == "caps") { |
| 775 | gli_conf_caps = asbool(arg); |
| 776 | } else if (cmd == "graphics") { |
| 777 | gli_conf_graphics = asbool(arg); |
| 778 | } else if (cmd == "sound") { |
| 779 | gli_conf_sound = asbool(arg); |
| 780 | } else if (cmd == "zbleep") { |
| 781 | std::istringstream argstream(arg); |
| 782 | std::string number, frequency, duration; |
| 783 | |
| 784 | if (argstream >> number >> duration >> frequency) { |
| 785 | gli_bleeps.update( |
| 786 | config_range(parse_int(number), 1, 2), |
| 787 | parse_double(duration), |
| 788 | parse_int(frequency)); |
| 789 | } |
| 790 | } else if (cmd == "zbleep_file") { |
| 791 | std::istringstream argstream(arg); |
| 792 | std::string number, path; |
| 793 | |
| 794 | if (argstream >> number >> path) { |
| 795 | gli_bleeps.update(config_range(parse_int(number), 1, 2), path); |
| 796 | } |
| 797 | } else if (cmd == "soundfont") { |
| 798 | gli_conf_soundfonts.clear(); |
| 799 | |
| 800 | std::istringstream argstream(arg); |
| 801 | std::string soundfont; |
| 802 | |
| 803 | while (argstream >> std::quoted(soundfont)) { |
| 804 | gli_conf_soundfonts.push_front(soundfont); |
| 805 | } |
| 806 | } else if (cmd == "fluidsynth_reverb") { |
| 807 | gli_conf_fluidsynth_reverb = asbool(arg); |
| 808 | } else if (cmd == "fluidsynth_chorus") { |
| 809 | gli_conf_fluidsynth_chorus = asbool(arg); |
| 810 | } else if (cmd == "fullscreen") { |
| 811 | gli_conf_fullscreen = asbool(arg); |
| 812 | } else if (cmd == "zoom") { |
| 813 | gli_zoom = config_atleast(parse_double(arg), 0.1); |
| 814 | } else if (cmd == "scaler") { |
| 815 | #ifdef GARGLK_CONFIG_SCALERS |
| 816 | if (arg == "none") { |
| 817 | gli_conf_scaler = Scaler::None; |
| 818 | } else if (arg == "hqx") { |
| 819 | gli_conf_scaler = Scaler::HQX; |
| 820 | } else if (arg == "xbrz") { |
| 821 | gli_conf_scaler = Scaler::XBRZ; |
| 822 | } else { |
| 823 | throw ConfigError(Format("invalid value: {} (must be one of none, hqx, xbrz)", arg)); |
| 824 | } |
| 825 | #else |
| 826 | throw ConfigError("this build of Gargoyle does not have support for scalers"); |
| 827 | #endif |
| 828 | } else if (cmd == "wait_on_quit") { |
| 829 | gli_wait_on_quit = asbool(arg); |
| 830 | } else if (cmd == "speak") { |
| 831 | gli_conf_speak = asbool(arg); |
| 832 | } else if (cmd == "speak_input") { |
| 833 | gli_conf_speak_input = asbool(arg); |
| 834 | } else if (cmd == "speak_language") { |
| 835 | gli_conf_speak_language = arg; |
| 836 | } else if (cmd == "stylehint") { |
| 837 | gli_conf_stylehint = asbool(arg); |
| 838 | } else if (cmd == "safeclicks") { |
| 839 | gli_conf_safeclicks = asbool(arg); |
| 840 | } else if (cmd == "theme") { |
| 841 | if (!garglk::theme::set(arg)) { |
| 842 | throw ConfigError(Format("unknown theme: {}", arg)); |
| 843 | } |
| 844 | } else if (cmd == "tcolor" || cmd == "gcolor") { |
| 845 | std::istringstream argstream(arg); |
| 846 | std::string style, fg, bg; |
| 847 | |
| 848 | if (argstream >> style >> fg >> bg) { |
| 849 | Styles &styles = cmd[0] == 't' ? gli_tstyles : gli_gstyles; |
| 850 | |
| 851 | if (style == "*") { |
| 852 | for (int i = 0; i < style_NUMSTYLES; i++) { |
| 853 | parsecolor(fg, styles[i].fg); |
| 854 | parsecolor(bg, styles[i].bg); |
| 855 | } |
| 856 | } else { |
| 857 | int i = parse_int(style); |
| 858 | |
| 859 | if (i >= 0 && i < style_NUMSTYLES) { |
| 860 | parsecolor(fg, styles[i].fg); |
| 861 | parsecolor(bg, styles[i].bg); |
| 862 | } |
| 863 | } |
| 864 | } |
| 865 | } else if (cmd == "tfont" || cmd == "gfont") { |
| 866 | std::istringstream argstream(arg); |
| 867 | std::string style, font; |
| 868 | |
| 869 | if (argstream >> style >> font) { |
| 870 | int i = config_range(parse_int(style), 0, style_NUMSTYLES - 1); |
| 871 | |
| 872 | if (i >= 0 && i < style_NUMSTYLES) { |
| 873 | if (cmd[0] == 't') { |
| 874 | gli_tstyles[i].font = font2idx(font); |
| 875 | } else { |
| 876 | gli_gstyles[i].font = font2idx(font); |
| 877 | } |
| 878 | } |
| 879 | } |
| 880 | } else if (cmd == "game_config") { |
| 881 | gli_conf_per_game_config = asbool(arg); |
| 882 | } else if (cmd == "gamedata_location") { |
| 883 | if (arg == "default") { |
| 884 | gli_conf_gamedata_location = GamedataLocation::Default; |
| 885 | } else if (arg == "dedicated") { |
| 886 | gli_conf_gamedata_location = GamedataLocation::Dedicated; |
| 887 | } else if (arg == "gamedir") { |
| 888 | gli_conf_gamedata_location = GamedataLocation::Gamedir; |
| 889 | } else { |
| 890 | throw ConfigError(Format("unknown gamedata location: {}", arg)); |
| 891 | } |
| 892 | } else if (cmd == "redraw_hack") { |
| 893 | gli_conf_redraw_hack = asbool(arg); |
| 894 | } else if (cmd == "glyph_substitution_file") { |
| 895 | std::istringstream argstream(arg); |
| 896 | std::string style, file; |
| 897 | static const std::unordered_map<std::string, std::vector<FontFace>> facemap = { |
| 898 | {"*", {FontFace::monor(), FontFace::monob(), FontFace::monoi(), FontFace::monoz(), |
| 899 | FontFace::propr(), FontFace::propb(), FontFace::propi(), FontFace::propz()}}, |
| 900 | |
| 901 | {"*r", {FontFace::monor(), FontFace::propr()}}, |
| 902 | {"*b", {FontFace::monob(), FontFace::propb()}}, |
| 903 | {"*i", {FontFace::monoi(), FontFace::propi()}}, |
| 904 | {"*z", {FontFace::monoz(), FontFace::propz()}}, |
| 905 | |
| 906 | {"mono", {FontFace::monor(), FontFace::monob(), FontFace::monoi(), FontFace::monoz()}}, |
| 907 | {"prop", {FontFace::propr(), FontFace::propb(), FontFace::propi(), FontFace::propz()}}, |
| 908 | |
| 909 | {"monor", {FontFace::monor()}}, |
| 910 | {"monob", {FontFace::monob()}}, |
| 911 | {"monoi", {FontFace::monoi()}}, |
| 912 | {"monoz", {FontFace::monoz()}}, |
| 913 | {"propr", {FontFace::propr()}}, |
| 914 | {"propb", {FontFace::propb()}}, |
| 915 | {"propi", {FontFace::propi()}}, |
| 916 | {"propz", {FontFace::propz()}}, |
| 917 | }; |
| 918 | |
| 919 | if (argstream >> style) { |
| 920 | while (argstream >> std::quoted(file)) { |
| 921 | try { |
| 922 | for (const auto &fontface : facemap.at(style)) { |
| 923 | gli_conf_glyph_substitution_files[fontface].push_back(file); |
| 924 | } |
| 925 | } catch (const std::out_of_range &) { |
| 926 | throw ConfigError(Format("unknown font style: {}", style)); |
| 927 | } |
| 928 | } |
| 929 | } |
| 930 | } else if (cmd == "terp") { |
| 931 | // Ignore: this is used by the launcher. |
| 932 | } else { |
| 933 | warn(Format("unknown configuration option: {}", cmd)); |
| 934 | } |
| 935 | } catch (const ConfigError &e) { |
| 936 | warn(Format("{}: {}", cmd, e.message)); |
| 937 | } |
| 938 | }); |
| 939 | |
| 940 | gli_tstyles_def = gli_tstyles; |
| 941 | gli_gstyles_def = gli_gstyles; |
| 942 | } |
| 943 | |
| 944 | void gli_read_config(int argc, char **argv) |
| 945 | { |
| 946 | #if __cplusplus >= 201703L |
| 947 | // load argv0 with name of executable without suffix |
| 948 | std::string argv0 = std::filesystem::path(argv[0]) |
| 949 | .filename() |
| 950 | .replace_extension() |
| 951 | .string(); |
| 952 | |
| 953 | // load gamefile with basename of last argument |
| 954 | nonstd::optional<std::string> gamefile; |
| 955 | if (argc > 1) { |
| 956 | gamefile = std::filesystem::path(argv[argc - 1]) |
| 957 | .filename() |
| 958 | .string(); |
| 959 | } |
| 960 | #else |
| 961 | auto basename = [](std::string path) { |
| 962 | auto slash = path.find_last_of("/\\"); |
| 963 | if (slash != std::string::npos) { |
| 964 | path.erase(0, slash + 1); |
| 965 | } |
| 966 | |
| 967 | return path; |
| 968 | }; |
| 969 | |
| 970 | // load argv0 with name of executable without suffix |
| 971 | std::string argv0 = basename(argv[0]); |
| 972 | auto dot = argv0.rfind('.'); |
| 973 | if (dot != std::string::npos) { |
| 974 | argv0.erase(dot); |
| 975 | } |
| 976 | |
| 977 | // load gamefile with basename of last argument |
| 978 | nonstd::optional<std::string> gamefile; |
| 979 | if (argc > 1) { |
| 980 | gamefile = basename(argv[argc - 1]); |
| 981 | } |
| 982 | #endif |
| 983 | |
| 984 | // load gamepath with the path to the story file itself |
| 985 | nonstd::optional<std::string> gamepath; |
| 986 | if (argc > 1) { |
| 987 | gamepath = argv[argc - 1]; |
| 988 | } |
| 989 | |
| 990 | // load from all config files |
| 991 | auto configs = garglk::configs(gamepath); |
| 992 | std::reverse(configs.begin(), configs.end()); |
| 993 | |
| 994 | garglk::all_configs.clear(); |
| 995 | for (const auto &config : configs) { |
| 996 | if (config.type != garglk::ConfigFile::Type::PerGame || gli_conf_per_game_config) { |
| 997 | readoneconfig(config.path, argv0, gamefile); |
| 998 | garglk::all_configs.push_back(config); |
| 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | // store so other parts of the code have access to full config information, |
| 1003 | // including the gamepath. |
| 1004 | std::reverse(garglk::all_configs.begin(), garglk::all_configs.end()); |
| 1005 | } |
| 1006 | |
| 1007 | strid_t glkunix_stream_open_pathname_gen(char *pathname, glui32 writemode, glui32 textmode, glui32 rock) |
| 1008 | { |
| 1009 | return gli_stream_open_pathname(pathname, (writemode != 0), (textmode != 0), rock); |
| 1010 | } |
| 1011 | |
| 1012 | strid_t glkunix_stream_open_pathname(char *pathname, glui32 textmode, glui32 rock) |
| 1013 | { |
| 1014 | return gli_stream_open_pathname(pathname, false, (textmode != 0), rock); |
| 1015 | } |
| 1016 | |
| 1017 | void garglk_startup(int argc, char *argv[]) |
| 1018 | { |
| 1019 | static bool initialized = false; |
| 1020 | |
| 1021 | if (argc == 0) { |
| 1022 | std::cerr << "argv[0] is null, aborting\n"; |
| 1023 | std::exit(EXIT_FAILURE); |
| 1024 | } |
| 1025 | |
| 1026 | if (initialized) { |
| 1027 | gli_strict_warning("garglk_startup called multiple times"); |
| 1028 | return; |
| 1029 | } |
| 1030 | |
| 1031 | initialized = true; |
| 1032 | |
| 1033 | wininit(); |
| 1034 | |
| 1035 | if (argc > 1) { |
| 1036 | glkunix_set_base_file(argv[argc - 1]); |
| 1037 | } |
| 1038 | |
| 1039 | garglk::theme::init(); |
| 1040 | |
| 1041 | gli_read_config(argc, argv); |
| 1042 | |
| 1043 | gli_more_prompt.resize(base_more_prompt.size() + 1); |
| 1044 | gli_more_prompt_len = gli_parse_utf8(reinterpret_cast<const unsigned char *>(base_more_prompt.data()), base_more_prompt.size(), gli_more_prompt.data(), base_more_prompt.size()); |
| 1045 | |
| 1046 | if (gli_baseline == 0) { |
| 1047 | gli_baseline = std::round(gli_conf_propsize); |
| 1048 | } |
| 1049 | |
| 1050 | gli_zoom *= gli_backingscalefactor; |
| 1051 | gli_baseline = gli_zoom_int(gli_baseline); |
| 1052 | gli_conf_monosize = gli_conf_monosize * gli_zoom; |
| 1053 | gli_conf_propsize = gli_conf_propsize * gli_zoom; |
| 1054 | gli_leading = gli_zoom_int(gli_leading); |
| 1055 | gli_tmarginx = gli_zoom_int(gli_tmarginx); |
| 1056 | gli_tmarginy = gli_zoom_int(gli_tmarginy); |
| 1057 | gli_wborderx = gli_zoom_int(gli_wborderx); |
| 1058 | gli_wbordery = gli_zoom_int(gli_wbordery); |
| 1059 | gli_wmarginx = gli_zoom_int(gli_wmarginx); |
| 1060 | gli_wmarginx_save = gli_zoom_int(gli_wmarginx_save); |
| 1061 | gli_wmarginy = gli_zoom_int(gli_wmarginy); |
| 1062 | gli_wmarginy_save = gli_zoom_int(gli_wmarginy_save); |
| 1063 | gli_wpaddingx = gli_zoom_int(gli_wpaddingx); |
| 1064 | gli_wpaddingy = gli_zoom_int(gli_wpaddingy); |
| 1065 | |
| 1066 | gli_initialize_tts(); |
| 1067 | if (gli_conf_speak) { |
| 1068 | gli_conf_quotes = 0; |
| 1069 | } |
| 1070 | |
| 1071 | gli_initialize_misc(); |
| 1072 | gli_initialize_fonts(); |
| 1073 | gli_initialize_windows(); |
| 1074 | gli_initialize_sound(); |
| 1075 | |
| 1076 | winopen(); |
| 1077 | |
| 1078 | if (gli_workfile.has_value()) { |
| 1079 | gli_initialize_babel(*gli_workfile); |
| 1080 | } |
| 1081 | |
| 1082 | // atexit() handlers should run before static destructors (see C++14 |
| 1083 | // 3.6.3p3): |
| 1084 | // |
| 1085 | // "If the completion of the initialization of an object with static |
| 1086 | // storage duration is sequenced before a call to std::atexit [...], |
| 1087 | // the call to the function passed to std::atexit is sequenced |
| 1088 | // before the call to the destructor for the object." |
| 1089 | // |
| 1090 | // In general, this ought to obviate the need for setting |
| 1091 | // gli_exiting in gli_exit(), but it's possible for atexit() to |
| 1092 | // fail, so do it in both places. |
| 1093 | if (std::atexit([]() { gli_exiting = true; }) != 0) { |
| 1094 | gli_strict_warning("garglk_startup: unable to register atexit handler"); |
| 1095 | } |
| 1096 | } |