1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE-APACHE file or at:
// https://www.apache.org/licenses/LICENSE-2.0
//! Options
use super::Error;
use kas::draw::DrawSharedImpl;
use kas_theme::{Theme, ThemeConfig};
use log::warn;
use std::env::var;
use std::path::PathBuf;
pub use wgpu::{Backends, PowerPreference};
/// Config mode
///
/// See [`Options::from_env`] documentation.
#[derive(Clone, PartialEq, Hash)]
pub enum ConfigMode {
/// Read-only mode
Read,
/// Read-write mode
///
/// This mode reads config on start and writes changes on exit.
ReadWrite,
/// Use default config and write out
///
/// This mode only writes initial (default) config and does not update.
WriteDefault,
}
/// Shell options
#[derive(Clone, PartialEq, Hash)]
pub struct Options {
/// Config file path. Default: empty. See `KAS_CONFIG` doc.
pub config_path: PathBuf,
/// Theme config path. Default: empty.
pub theme_config_path: PathBuf,
/// Config mode. Default: Read.
pub config_mode: ConfigMode,
/// Adapter power preference. Default value: low power.
pub power_preference: PowerPreference,
/// Adapter backend. Default value: PRIMARY (Vulkan/Metal/DX12).
pub backends: Backends,
/// WGPU's API tracing path
pub wgpu_trace_path: Option<PathBuf>,
}
impl Default for Options {
fn default() -> Self {
Options {
config_path: PathBuf::new(),
theme_config_path: PathBuf::new(),
config_mode: ConfigMode::Read,
power_preference: PowerPreference::LowPower,
backends: Backends::PRIMARY,
wgpu_trace_path: None,
}
}
}
impl Options {
/// Construct a new instance, reading from environment variables
///
/// The following environment variables are read, in case-insensitive mode.
///
/// # Config files
///
/// WARNING: file formats are not stable and may not be compatible across
/// KAS versions (aside from patch versions)!
///
/// The `KAS_CONFIG` variable, if given, provides a path to the KAS config
/// file, which is read or written according to `KAS_CONFIG_MODE`.
/// If `KAS_CONFIG` is not specified, platform-default configuration is used
/// without reading or writing. This may change to use a platform-specific
/// default path in future versions.
///
/// The `KAS_THEME_CONFIG` variable, if given, provides a path to the theme
/// config file, which is read or written according to `KAS_CONFIG_MODE`.
/// If `KAS_THEME_CONFIG` is not specified, platform-default configuration
/// is used without reading or writing. This may change to use a
/// platform-specific default path in future versions.
///
/// The `KAS_CONFIG_MODE` variable determines the read/write mode:
///
/// - `Read` (default): read-only
/// - `ReadWrite`: read on start-up, write on exit
/// - `WriteDefault`: generate platform-default configuration and write
/// it to the config path(s) specified, overwriting any existing config
///
/// Note: in the future, the default will likely change to a read-write mode,
/// allowing changes to be written out.
///
/// # Graphics options
///
/// The `KAS_POWER_PREFERENCE` variable supports:
///
/// - `Default`
/// - `LowPower`
/// - `HighPerformance`
///
/// The `KAS_BACKENDS` variable supports:
///
/// - `Vulkan`
/// - `GL`
/// - `Metal`
/// - `DX11`
/// - `DX12`
/// - `PRIMARY`: any of Vulkan, Metal or DX12
/// - `SECONDARY`: any of GL or DX11
///
/// WGPU has an [API tracing] feature for debugging. To use this, ensure the
/// `wgpu/trace` feature is enabled and set the output path:
/// ```sh
/// export KAS_WGPU_TRACE_PATH="api_trace"
/// ```
///
/// [API tracing]: https://github.com/gfx-rs/wgpu/wiki/Debugging-wgpu-Applications#tracing-infrastructure
pub fn from_env() -> Self {
let mut options = Options::default();
if let Ok(v) = var("KAS_CONFIG") {
options.config_path = v.into();
}
if let Ok(v) = var("KAS_THEME_CONFIG") {
options.theme_config_path = v.into();
}
if let Ok(mut v) = var("KAS_CONFIG_MODE") {
v.make_ascii_uppercase();
options.config_mode = match v.as_str() {
"READ" => ConfigMode::Read,
"READWRITE" => ConfigMode::ReadWrite,
"WRITEDEFAULT" => ConfigMode::WriteDefault,
other => {
warn!("Unexpected environment value: KAS_CONFIG_MODE={}", other);
options.config_mode
}
};
}
if let Ok(mut v) = var("KAS_POWER_PREFERENCE") {
v.make_ascii_uppercase();
options.power_preference = match v.as_str() {
"DEFAULT" | "LOWPOWER" => PowerPreference::LowPower,
"HIGHPERFORMANCE" => PowerPreference::HighPerformance,
other => {
warn!(
"Unexpected environment value: KAS_POWER_PREFERENCE={}",
other
);
options.power_preference
}
}
}
if let Ok(mut v) = var("KAS_BACKENDS") {
v.make_ascii_uppercase();
options.backends = match v.as_str() {
"VULKAN" => Backends::VULKAN,
"GL" => Backends::GL,
"METAL" => Backends::METAL,
"DX11" => Backends::DX11,
"DX12" => Backends::DX12,
"PRIMARY" => Backends::PRIMARY,
"SECONDARY" => Backends::SECONDARY,
other => {
warn!("Unexpected environment value: KAS_BACKENDS={}", other);
options.backends
}
}
}
if let Ok(v) = var("KAS_WGPU_TRACE_PATH") {
options.wgpu_trace_path = Some(v.into());
}
options
}
pub(crate) fn adapter_options(&self) -> wgpu::RequestAdapterOptions {
wgpu::RequestAdapterOptions {
power_preference: self.power_preference,
compatible_surface: None,
}
}
pub(crate) fn backend(&self) -> Backends {
self.backends
}
/// Load/save theme config on start
pub fn init_theme_config<DS: DrawSharedImpl, T: Theme<DS>>(
&self,
theme: &mut T,
) -> Result<(), Error> {
match self.config_mode {
ConfigMode::Read | ConfigMode::ReadWrite if self.theme_config_path.is_file() => {
let config: T::Config =
kas::config::Format::guess_and_read_path(&self.theme_config_path)?;
config.apply_startup();
// Ignore TkAction: UI isn't built yet
let _ = theme.apply_config(&config);
}
ConfigMode::WriteDefault if !self.theme_config_path.as_os_str().is_empty() => {
let config = theme.config();
config.apply_startup();
kas::config::Format::guess_and_write_path(
&self.theme_config_path,
config.as_ref(),
)?;
}
_ => theme.config().apply_startup(),
}
Ok(())
}
/// Load/save KAS config on start
pub fn read_config(&self) -> Result<kas::event::Config, Error> {
if !self.config_path.as_os_str().is_empty() {
match self.config_mode {
ConfigMode::Read | ConfigMode::ReadWrite => {
Ok(kas::config::Format::guess_and_read_path(&self.config_path)?)
}
ConfigMode::WriteDefault => {
let config: kas::event::Config = Default::default();
kas::config::Format::guess_and_write_path(&self.config_path, &config)?;
Ok(config)
}
}
} else {
Ok(Default::default())
}
}
/// Save all config (on exit or after changes)
pub fn write_config<DS: DrawSharedImpl, T: Theme<DS>>(
&self,
config: &kas::event::Config,
theme: &T,
) -> Result<(), Error> {
if self.config_mode == ConfigMode::ReadWrite {
if !self.config_path.as_os_str().is_empty() && config.is_dirty() {
kas::config::Format::guess_and_write_path(&self.config_path, &config)?;
}
let theme_config = theme.config();
if !self.theme_config_path.as_os_str().is_empty() && theme_config.is_dirty() {
kas::config::Format::guess_and_write_path(
&self.theme_config_path,
theme_config.as_ref(),
)?;
}
}
Ok(())
}
}