1
1
// SPDX-License-Identifier: GPL-2.0-only
2
+ #define _POSIX_C_SOURCE 200809L
3
+ #include <ctype.h>
2
4
#include "keyboard-layouts.h"
3
5
#include "state.h"
4
6
#include "stack-lang.h"
5
7
#include "theme.h"
6
8
#include "xml.h"
7
9
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
+
8
82
void
9
83
stack_lang_init (struct state * state , GtkWidget * stack )
10
84
{
@@ -29,7 +103,7 @@ stack_lang_init(struct state *state, GtkWidget *stack)
29
103
gtk_grid_attach (GTK_GRID (grid ), widget , 0 , row , 1 , 1 );
30
104
state -> widgets .keyboard_layout = gtk_combo_box_text_new ();
31
105
32
- char * active_id = "gb" ;
106
+ char * active_id = environment_get ( "XKB_DEFAULT_LAYOUT" ) ;
33
107
int active = -1 ;
34
108
35
109
GList * iter ;
0 commit comments