| 1 | // Copyright (C) Microsoft Corporation. All rights reserved. |
| 2 | |
| 3 | namespace WslSettings.Contracts.Services; |
| 4 | |
| 5 | public interface IWslConfigService |
| 6 | { |
| 7 | IWslConfigSetting GetWslConfigSetting(WslConfigEntry wslConfigEntry, bool defaultSetting = false); |
| 8 | |
| 9 | /// <summary> |
| 10 | /// Stages a config value change in memory. The value is only written to disk when |
| 11 | /// <see cref="CommitPendingChanges"/> is called. |
| 12 | /// </summary> |
| 13 | uint SetWslConfigSetting(IWslConfigSetting setting); |
| 14 | |
| 15 | /// <summary> |
| 16 | /// True when there are in-app changes that have not yet been committed to disk. |
| 17 | /// </summary> |
| 18 | bool HasPendingChanges { get; } |
| 19 | |
| 20 | /// <summary> |
| 21 | /// Returns pending changes as (entry, new value) pairs for UI display. |
| 22 | /// </summary> |
| 23 | IReadOnlyList<WslConfigPendingChange> GetPendingChanges(); |
| 24 | uint CommitPendingChanges(); |
| 25 | public delegate void WslConfigChangedEventHandler(); |
| 26 | event WslConfigChangedEventHandler WslConfigChanged; |
| 27 | public delegate void PendingChangesChangedEventHandler(); |
| 28 | event PendingChangesChangedEventHandler PendingChangesChanged; |
| 29 | } |
| 30 | |
| 31 | public sealed class WslConfigPendingChange |
| 32 | { |
| 33 | public WslConfigEntry ConfigEntry { get; init; } |
| 34 | public required object PendingValue { get; init; } |
| 35 | } |
| 36 | |
| 37 | public interface IWslConfigSetting |
| 38 | { |
| 39 | WslConfigEntry ConfigEntry { get; } |
| 40 | string StringValue { get; } |
| 41 | ulong UInt64Value { get; } |
| 42 | int Int32Value { get; } |
| 43 | bool BoolValue { get; } |
| 44 | NetworkingConfiguration NetworkingConfigurationValue { get; } |
| 45 | MemoryReclaimMode MemoryReclaimModeValue { get; } |
| 46 | uint SetValue(object? value); |
| 47 | bool Equals(object? obj); |
| 48 | } |
| 49 | |
| 50 | /// <summary> |
| 51 | /// Describes which union field a <see cref="WslConfigEntry"/> uses inside the native WslConfigSetting struct. |
| 52 | /// </summary> |
| 53 | public enum WslConfigValueKind |
| 54 | { |
| 55 | Bool, |
| 56 | Int32, |
| 57 | UInt64, |
| 58 | String, |
| 59 | NetworkingConfiguration, |
| 60 | MemoryReclaimMode, |
| 61 | } |
| 62 | |
| 63 | public static class WslConfigEntryExtensions |
| 64 | { |
| 65 | /// <summary> |
| 66 | /// Returns the <see cref="WslConfigValueKind"/> that describes which union field this entry uses. |
| 67 | /// Keep this in sync with the native WslConfigSetting union in WslCoreConfigInterface.h. |
| 68 | /// </summary> |
| 69 | public static WslConfigValueKind GetValueKind(this WslConfigEntry entry) => entry switch |
| 70 | { |
| 71 | WslConfigEntry.SwapFilePath or |
| 72 | WslConfigEntry.IgnoredPorts or |
| 73 | WslConfigEntry.KernelPath or |
| 74 | WslConfigEntry.SystemDistroPath or |
| 75 | WslConfigEntry.KernelModulesPath => WslConfigValueKind.String, |
| 76 | |
| 77 | WslConfigEntry.ProcessorCount or |
| 78 | WslConfigEntry.InitialAutoProxyTimeout or |
| 79 | WslConfigEntry.InstanceIdleTimeout or |
| 80 | WslConfigEntry.VMIdleTimeout => WslConfigValueKind.Int32, |
| 81 | |
| 82 | WslConfigEntry.MemorySizeBytes or |
| 83 | WslConfigEntry.SwapSizeBytes or |
| 84 | WslConfigEntry.VhdSizeBytes => WslConfigValueKind.UInt64, |
| 85 | |
| 86 | WslConfigEntry.NetworkingMode => WslConfigValueKind.NetworkingConfiguration, |
| 87 | |
| 88 | WslConfigEntry.AutoMemoryReclaim => WslConfigValueKind.MemoryReclaimMode, |
| 89 | |
| 90 | _ => WslConfigValueKind.Bool, |
| 91 | }; |
| 92 | } |