Skip to content

Commit c4b1453

Browse files
committed
WSLGd: import keyboard config into weston.ini
Introduce a new "weston-keyboard" section in .wslgconfig where you can configure weston/xkb keymap settings that will be used as a fallback if no xkb mapping can be found for current keyboard layout. Syntax is the same of weston.ini(5). Only keymap related key-value pairs are imported into WSLG system weston.ini, anything else is ignored.
1 parent 4ad2bbf commit c4b1453

File tree

2 files changed

+66
-17
lines changed

2 files changed

+66
-17
lines changed

WSLGd/main.cpp

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ constexpr auto c_installPathEnv = "WSL2_INSTALL_PATH";
4040
constexpr auto c_userProfileEnv = "WSL2_USER_PROFILE";
4141
constexpr auto c_systemDistroEnvSection = "system-distro-env";
4242

43+
constexpr auto c_westonIniFile = "/home/wslg/.config/weston.ini";
44+
constexpr auto c_westonKeyboardSection = "weston-keyboard";
45+
4346
constexpr auto c_windowsSystem32 = "/mnt/c/Windows/System32";
4447

4548
constexpr auto c_westonShellOverrideEnv = "WSL2_WESTON_SHELL_OVERRIDE";
@@ -138,9 +141,61 @@ bool GetEnvBool(const char *EnvName, bool DefaultValue)
138141
return DefaultValue;
139142
}
140143

141-
void SetupOptionalEnv()
142-
{
143144
#if HAVE_WINPR
145+
void SetupOptionalEnv(wIniFile *iniFile)
146+
{
147+
// Set additional environment variables.
148+
int numKeys = 0;
149+
char **keyNames = IniFile_GetSectionKeyNames(iniFile, c_systemDistroEnvSection, &numKeys);
150+
for (int n = 0; keyNames && n < numKeys; n++) {
151+
const char *value = IniFile_GetKeyValueString(iniFile, c_systemDistroEnvSection, keyNames[n]);
152+
if (value) {
153+
setenv(keyNames[n], value, true);
154+
}
155+
}
156+
157+
free(keyNames);
158+
}
159+
160+
void SetupWestonKeyboard(wIniFile *iniFile)
161+
{
162+
// Merge weston-keyboard section from wslgconfig into weston.ini
163+
const char *section_header = "[keyboard]";
164+
165+
std::vector<std::string> valid_keys {
166+
"keymap_rules",
167+
"keymap_model",
168+
"keymap_layout",
169+
"keymap_variant",
170+
"keymap_options",
171+
};
172+
173+
int numKeys = 0;
174+
char **keyNames = IniFile_GetSectionKeyNames(iniFile, c_westonKeyboardSection, &numKeys);
175+
176+
if (numKeys > 0) {
177+
wil::unique_file westonIniFile(fopen(c_westonIniFile, "a"));
178+
if (westonIniFile.get()) {
179+
THROW_LAST_ERROR_IF(fprintf(westonIniFile.get(), "\n%s\n", section_header) < 0);
180+
181+
for (int n = 0; keyNames && n < numKeys; n++) {
182+
if (std::find(valid_keys.begin(), valid_keys.end(), keyNames[n]) == valid_keys.end()) {
183+
continue;
184+
}
185+
186+
const char *value = IniFile_GetKeyValueString(iniFile, c_westonKeyboardSection, keyNames[n]);
187+
if (value) {
188+
THROW_LAST_ERROR_IF(fprintf(westonIniFile.get(), "%s=%s\n", keyNames[n], value) < 0);
189+
}
190+
}
191+
}
192+
}
193+
194+
free(keyNames);
195+
}
196+
197+
void ParseConfig()
198+
{
144199
// Get the path to the WSLG config file.
145200
std::string configFilePath = "/mnt/c/ProgramData/Microsoft/WSL/" CONFIG_FILE;
146201
auto userProfile = getenv(c_userProfileEnv);
@@ -149,28 +204,21 @@ void SetupOptionalEnv()
149204
configFilePath += "/" CONFIG_FILE;
150205
}
151206

152-
// Set additional environment variables.
153207
wIniFile* wslgConfigIniFile = IniFile_New();
154208
if (wslgConfigIniFile) {
155209
if (IniFile_ReadFile(wslgConfigIniFile, configFilePath.c_str()) > 0) {
156-
int numKeys = 0;
157-
char **keyNames = IniFile_GetSectionKeyNames(wslgConfigIniFile, c_systemDistroEnvSection, &numKeys);
158-
for (int n = 0; keyNames && n < numKeys; n++) {
159-
const char *value = IniFile_GetKeyValueString(wslgConfigIniFile, c_systemDistroEnvSection, keyNames[n]);
160-
if (value) {
161-
setenv(keyNames[n], value, true);
162-
}
163-
}
164-
165-
free(keyNames);
210+
SetupOptionalEnv(wslgConfigIniFile);
211+
SetupWestonKeyboard(wslgConfigIniFile);
166212
}
167-
168213
IniFile_Free(wslgConfigIniFile);
169214
}
170-
#endif // HAVE_WINPR
171-
215+
}
216+
#else
217+
void ParseConfig()
218+
{
172219
return;
173220
}
221+
#endif
174222

175223
int SetupReadyNotify(const char *socket_path)
176224
{
@@ -235,7 +283,7 @@ try {
235283
THROW_LAST_ERROR_IF(setenv(var.name, var.value, var.override) < 0);
236284
}
237285

238-
SetupOptionalEnv();
286+
ParseConfig();
239287

240288
// if any components output log to /dev/kmsg, make it writable.
241289
if (GetEnvBool("WSLG_LOG_KMSG", false))

WSLGd/precomp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <map>
3232
#include <new>
3333
#include <vector>
34+
#include <algorithm>
3435
#include "config.h"
3536
#include "lxwil.h"
3637
#if HAVE_WINPR

0 commit comments

Comments
 (0)