Skip to content

Commit 31c74db

Browse files
committed
Activate keyboard layout dropdown entry
...based on XKB_DEVFAULT_LAYOUT value in ~/.config/labwc/environment Fixes: labwc/labwc#1400
1 parent 1c79d6a commit 31c74db

File tree

1 file changed

+75
-1
lines changed

1 file changed

+75
-1
lines changed

stack-lang.c

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,84 @@
11
// SPDX-License-Identifier: GPL-2.0-only
2+
#define _POSIX_C_SOURCE 200809L
3+
#include <ctype.h>
24
#include "keyboard-layouts.h"
35
#include "state.h"
46
#include "stack-lang.h"
57
#include "theme.h"
68
#include "xml.h"
79

10+
static void
11+
rtrim(char **s)
12+
{
13+
size_t len = strlen(*s);
14+
if (!len) {
15+
return;
16+
}
17+
char *end = *s + len - 1;
18+
while (end >= *s && isspace(*end)) {
19+
end--;
20+
}
21+
*(end + 1) = '\0';
22+
}
23+
24+
static char *
25+
string_strip(char *s)
26+
{
27+
rtrim(&s);
28+
while (isspace(*s)) {
29+
s++;
30+
}
31+
return s;
32+
}
33+
34+
static char *
35+
get_value(char *line, const char *key)
36+
{
37+
if (!line || !*line || line[0] == '#') {
38+
return NULL;
39+
}
40+
char *p = strchr(line, '=');
41+
if (!p) {
42+
return NULL;
43+
}
44+
*p = '\0';
45+
if (!!strcmp(key, string_strip(line))) {
46+
return NULL;
47+
}
48+
char *value = string_strip(++p);
49+
return value ? value : NULL;
50+
}
51+
52+
static char *
53+
environment_get(const char *key)
54+
{
55+
char filename[4096];
56+
snprintf(filename, sizeof(filename), "%s/%s", getenv("HOME"), ".config/labwc/environment");
57+
58+
char *value = NULL;
59+
char *line = NULL;
60+
size_t len = 0;
61+
FILE *stream = fopen(filename, "r");
62+
if (!stream) {
63+
goto out;
64+
}
65+
66+
while (getline(&line, &len, stream) != -1) {
67+
char *p = strrchr(line, '\n');
68+
if (p) {
69+
*p = '\0';
70+
}
71+
value = get_value(line, key);
72+
if (value) {
73+
goto out;
74+
}
75+
}
76+
out:
77+
free(line);
78+
fclose(stream);
79+
return value ? value : "";
80+
}
81+
882
void
983
stack_lang_init(struct state *state, GtkWidget *stack)
1084
{
@@ -29,7 +103,7 @@ stack_lang_init(struct state *state, GtkWidget *stack)
29103
gtk_grid_attach(GTK_GRID(grid), widget, 0, row, 1, 1);
30104
state->widgets.keyboard_layout = gtk_combo_box_text_new();
31105

32-
char *active_id = "gb";
106+
char *active_id = environment_get("XKB_DEFAULT_LAYOUT");
33107
int active = -1;
34108

35109
GList *iter;

0 commit comments

Comments
 (0)