Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WSLGd: load keymap settings from .wslgconfig into weston.ini #1046

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 65 additions & 17 deletions WSLGd/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ constexpr auto c_installPathEnv = "WSL2_INSTALL_PATH";
constexpr auto c_userProfileEnv = "WSL2_USER_PROFILE";
constexpr auto c_systemDistroEnvSection = "system-distro-env";

constexpr auto c_westonIniFile = "/home/wslg/.config/weston.ini";
constexpr auto c_westonKeyboardSection = "weston-keyboard";

constexpr auto c_windowsSystem32 = "/mnt/c/Windows/System32";

constexpr auto c_westonShellOverrideEnv = "WSL2_WESTON_SHELL_OVERRIDE";
Expand Down Expand Up @@ -138,9 +141,61 @@ bool GetEnvBool(const char *EnvName, bool DefaultValue)
return DefaultValue;
}

void SetupOptionalEnv()
{
#if HAVE_WINPR
void SetupOptionalEnv(wIniFile *iniFile)
{
// Set additional environment variables.
int numKeys = 0;
char **keyNames = IniFile_GetSectionKeyNames(iniFile, c_systemDistroEnvSection, &numKeys);
for (int n = 0; keyNames && n < numKeys; n++) {
const char *value = IniFile_GetKeyValueString(iniFile, c_systemDistroEnvSection, keyNames[n]);
if (value) {
setenv(keyNames[n], value, true);
}
}

free(keyNames);
}

void SetupWestonKeyboard(wIniFile *iniFile)
{
// Merge weston-keyboard section from wslgconfig into weston.ini
const char *section_header = "[keyboard]";

std::vector<std::string> valid_keys {
"keymap_rules",
"keymap_model",
"keymap_layout",
"keymap_variant",
"keymap_options",
};

int numKeys = 0;
char **keyNames = IniFile_GetSectionKeyNames(iniFile, c_westonKeyboardSection, &numKeys);

if (numKeys > 0) {
wil::unique_file westonIniFile(fopen(c_westonIniFile, "a"));
if (westonIniFile.get()) {
THROW_LAST_ERROR_IF(fprintf(westonIniFile.get(), "\n%s\n", section_header) < 0);

for (int n = 0; keyNames && n < numKeys; n++) {
if (std::find(valid_keys.begin(), valid_keys.end(), keyNames[n]) == valid_keys.end()) {
continue;
}

const char *value = IniFile_GetKeyValueString(iniFile, c_westonKeyboardSection, keyNames[n]);
if (value) {
THROW_LAST_ERROR_IF(fprintf(westonIniFile.get(), "%s=%s\n", keyNames[n], value) < 0);
}
}
}
}

free(keyNames);
}

void ParseConfig()
{
// Get the path to the WSLG config file.
std::string configFilePath = "/mnt/c/ProgramData/Microsoft/WSL/" CONFIG_FILE;
auto userProfile = getenv(c_userProfileEnv);
Expand All @@ -149,28 +204,21 @@ void SetupOptionalEnv()
configFilePath += "/" CONFIG_FILE;
}

// Set additional environment variables.
wIniFile* wslgConfigIniFile = IniFile_New();
if (wslgConfigIniFile) {
if (IniFile_ReadFile(wslgConfigIniFile, configFilePath.c_str()) > 0) {
int numKeys = 0;
char **keyNames = IniFile_GetSectionKeyNames(wslgConfigIniFile, c_systemDistroEnvSection, &numKeys);
for (int n = 0; keyNames && n < numKeys; n++) {
const char *value = IniFile_GetKeyValueString(wslgConfigIniFile, c_systemDistroEnvSection, keyNames[n]);
if (value) {
setenv(keyNames[n], value, true);
}
}

free(keyNames);
SetupOptionalEnv(wslgConfigIniFile);
SetupWestonKeyboard(wslgConfigIniFile);
}

IniFile_Free(wslgConfigIniFile);
}
#endif // HAVE_WINPR

}
#else
void ParseConfig()
{
return;
}
#endif

int SetupReadyNotify(const char *socket_path)
{
Expand Down Expand Up @@ -235,7 +283,7 @@ try {
THROW_LAST_ERROR_IF(setenv(var.name, var.value, var.override) < 0);
}

SetupOptionalEnv();
ParseConfig();

// if any components output log to /dev/kmsg, make it writable.
if (GetEnvBool("WSLG_LOG_KMSG", false))
Expand Down
1 change: 1 addition & 0 deletions WSLGd/precomp.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <map>
#include <new>
#include <vector>
#include <algorithm>
#include "config.h"
#include "lxwil.h"
#if HAVE_WINPR
Expand Down