Skip to content

Commit 786b505

Browse files
authored
KeyboardLayout: list from xkbregistry (#833)
1 parent 5b41cd3 commit 786b505

File tree

7 files changed

+163
-74
lines changed

7 files changed

+163
-74
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
- name: Install Dependencies
2424
run: |
2525
apt update
26-
apt install -y desktop-file-utils gettext libadwaita-1-dev libdistinst-dev libgee-0.8-dev libgranite-7-dev libgtk-4-dev libxml2-dev libjson-glib-dev libpwquality-dev libxml2-utils meson valac
26+
apt install -y desktop-file-utils gettext libadwaita-1-dev libdistinst-dev libgee-0.8-dev libgranite-7-dev libgtk-4-dev libxkbregistry-dev libjson-glib-dev libpwquality-dev meson valac
2727
- name: Build and Test
2828
env:
2929
DESTDIR: out

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ You'll need the following dependencies:
2222
- libadwaita-1-dev >=1.4.0
2323
- libjson-glib-dev
2424
- libpwquality-dev
25-
- libxml2-dev
26-
- libxml2-utils
25+
- libxkbregistry-dev
2726
- [distinst](https://github.com/pop-os/distinst/)
2827
- valac
2928

meson.build

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ gio_dep = dependency('gio-2.0')
2020
granite_dep = dependency('granite-7', version: '>=7.4.0')
2121
adw_dep = dependency('libadwaita-1', version: '>=1.4.0')
2222
json_glib_dep = dependency('json-glib-1.0')
23-
xml2_dep = dependency('libxml-2.0')
23+
xkbregistry_dep = dependency('xkbregistry')
2424
pwquality_dep = dependency('pwquality')
2525
systemd_dep = dependency('systemd')
2626

src/Config.vala.in

-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@ namespace Build {
33
public const string LANG_LIST = "@LANG_LIST@";
44
public const string LOCALEDIR = @LOCALEDIR@;
55
public const string PREFERRED_LANG_LIST = "@PREFERRED_LANG_LIST@";
6-
public const string XKB_BASE = "@XKB_BASE@";
76
public const string ISO_CODES_LOCATION = "@ISO_CODES_LOCATION@";
87
}

src/Objects/KeyboardLayout.vala

+12-64
Original file line numberDiff line numberDiff line change
@@ -246,79 +246,27 @@ public class Installer.KeyboardLayout : GLib.Object {
246246
return a.display_name.collate (b.display_name);
247247
}
248248

249-
private const string XKB_RULES_FILE = "base.xml";
250-
251-
private static string get_xml_rules_file_path () {
252-
unowned string? base_path = GLib.Environment.get_variable ("XKB_CONFIG_ROOT");
253-
if (base_path == null) {
254-
base_path = Build.XKB_BASE;
255-
}
256-
257-
return Path.build_filename (base_path, "rules", XKB_RULES_FILE);
258-
}
259-
260249
public static GLib.ListStore get_all () {
261250
var layout_store = new GLib.ListStore (typeof (KeyboardLayout));
262-
unowned Xml.Doc* doc = Xml.Parser.read_file (get_xml_rules_file_path ());
263251

264-
Xml.XPath.Context cntx = new Xml.XPath.Context (doc);
265-
unowned Xml.XPath.Object* res = cntx.eval_expression ("/xkbConfigRegistry/layoutList/layout");
266-
if (res == null) {
267-
delete doc;
268-
critical ("Unable to parse 'base.xml'");
269-
return layout_store;
270-
}
271-
272-
if (res->type != Xml.XPath.ObjectType.NODESET || res->nodesetval == null) {
273-
delete res;
274-
delete doc;
275-
critical ("No layouts found in 'base.xml'");
276-
return layout_store;
277-
}
252+
var xkb_context = new Rxkb.Context (NO_FLAGS);
253+
xkb_context.parse_default_ruleset ();
278254

279-
for (int i = 0; i < res->nodesetval->length (); i++) {
280-
unowned Xml.Node* layout_node = res->nodesetval->item (i);
281-
unowned Xml.Node* config_node = get_xml_node_by_name (layout_node, "configItem");
282-
unowned Xml.Node* variant_node = get_xml_node_by_name (layout_node, "variantList");
283-
unowned Xml.Node* description_node = get_xml_node_by_name (config_node, "description");
284-
unowned Xml.Node* name_node = get_xml_node_by_name (config_node, "name");
285-
if (name_node == null || description_node == null) {
286-
continue;
287-
}
288-
289-
var layout = new KeyboardLayout (name_node->children->content, description_node->children->content);
255+
var xkb_layout = xkb_context.get_first_layout ();
256+
while (xkb_layout != null) {
257+
var layout = new KeyboardLayout (xkb_layout.get_name (), xkb_layout.get_description ());
290258
layout_store.insert_sorted (layout, (GLib.CompareDataFunc<GLib.Object>) KeyboardLayout.compare);
291259

292-
if (variant_node != null) {
293-
for (unowned Xml.Node* variant_iter = variant_node->children; variant_iter != null; variant_iter = variant_iter->next) {
294-
if (variant_iter->name == "variant") {
295-
unowned Xml.Node* variant_config_node = get_xml_node_by_name (variant_iter, "configItem");
296-
if (variant_config_node != null) {
297-
unowned Xml.Node* variant_description_node = get_xml_node_by_name (variant_config_node, "description");
298-
unowned Xml.Node* variant_name_node = get_xml_node_by_name (variant_config_node, "name");
299-
if (variant_description_node != null && variant_name_node != null) {
300-
layout.add_variant (variant_name_node->children->content, variant_description_node->children->content);
301-
}
302-
}
303-
}
304-
}
305-
}
306-
}
307-
308-
delete res;
309-
delete doc;
310-
return layout_store;
311-
}
260+
var next_layout = xkb_layout.next ();
261+
while (next_layout != null && next_layout.get_variant () != null) {
262+
layout.add_variant (next_layout.get_variant (), next_layout.get_description ());
312263

313-
private static unowned Xml.Node* get_xml_node_by_name (Xml.Node* root, string name) {
314-
for (unowned Xml.Node* iter = root->children; iter != null; iter = iter->next) {
315-
if (iter->type == Xml.ElementType.ELEMENT_NODE) {
316-
if (iter->name == name) {
317-
return iter;
318-
}
264+
next_layout = next_layout.next ();
319265
}
266+
267+
xkb_layout = next_layout;
320268
}
321269

322-
return null;
270+
return layout_store;
323271
}
324272
}

src/meson.build

+1-5
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ configuration_data.set('GETTEXT_PACKAGE', meson.project_name())
3939
configuration_data.set('LANG_LIST', get_option('supported_languages'))
4040
configuration_data.set_quoted('LOCALEDIR', get_option('prefix') / get_option('localedir'))
4141

42-
xkbconf = dependency('xkeyboard-config')
43-
xkb_base = xkbconf.get_pkgconfig_variable('xkb_base')
44-
configuration_data.set('XKB_BASE', xkb_base)
45-
4642
isocodes = dependency('iso-codes')
4743
isocodes_prefix = isocodes.get_pkgconfig_variable('prefix')
4844
isocodes_location = join_paths(isocodes_prefix, get_option('datadir'), 'iso-codes', 'json')
@@ -65,7 +61,7 @@ gui_dependencies = [
6561
adw_dep,
6662
json_glib_dep,
6763
pwquality_dep,
68-
xml2_dep
64+
xkbregistry_dep
6965
]
7066

7167
executable(meson.project_name(), vala_files, config_file,

vapi/xkbregistry.vapi

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
* Vala bindings for xkbregistry
3+
* Copyright 2022 Corentin Noël <corentin.noel@collabora.com>
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a
6+
* copy of this software and associated documentation files (the "Software"),
7+
* to deal in the Software without restriction, including without limitation
8+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
9+
* and/or sell copies of the Software, and to permit persons to whom the
10+
* Software is furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice (including the next
13+
* paragraph) shall be included in all copies or substantial portions of the
14+
* Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22+
* DEALINGS IN THE SOFTWARE.
23+
*/
24+
25+
[CCode (cheader_filename = "xkbcommon/xkbregistry.h", cprefix = "rxkb_", lower_case_cprefix = "rxkb_")]
26+
namespace Rxkb {
27+
[CCode (cname = "struct rxkb_context", ref_function = "rxkb_context_ref", unref_function = "rxkb_context_unref", has_type_id = false)]
28+
[Compact]
29+
public class Context {
30+
public Context (Rxkb.ContextFlags flags);
31+
public void set_log_level (Rxkb.LogLevel level);
32+
public Rxkb.LogLevel get_log_level ();
33+
public bool parse (string ruleset);
34+
public bool parse_default_ruleset ();
35+
public bool include_path_append (string path);
36+
public bool include_path_append_default ();
37+
public void set_log_fn (LogFn log_fn);
38+
public void set_user_data (void* user_data);
39+
public void* get_user_data ();
40+
[CCode (cname = "rxkb_model_first")]
41+
public unowned Rxkb.Model? get_first_model ();
42+
[CCode (cname = "rxkb_layout_first")]
43+
public unowned Rxkb.Layout? get_first_layout ();
44+
[CCode (cname = "rxkb_option_group_first")]
45+
public unowned Rxkb.OptionGroup? get_first_option_group ();
46+
public unowned Rxkb.Context @ref ();
47+
public void unref ();
48+
}
49+
50+
[CCode (cname = "struct rxkb_model", ref_function = "rxkb_model_ref", unref_function = "rxkb_model_unref", has_type_id = false)]
51+
[Compact]
52+
public class Model {
53+
public unowned Rxkb.Model? next ();
54+
public unowned string get_name ();
55+
public unowned string? get_description ();
56+
public unowned string? get_vendor ();
57+
public Rxkb.Popularity get_popularity ();
58+
public unowned Rxkb.Model @ref ();
59+
public void unref ();
60+
}
61+
62+
[CCode (cname = "struct rxkb_layout", ref_function = "rxkb_layout_ref", unref_function = "rxkb_layout_unref", has_type_id = false)]
63+
[Compact]
64+
public class Layout {
65+
public unowned Rxkb.Layout? next ();
66+
public unowned string get_name ();
67+
public unowned string? get_description ();
68+
public unowned string? get_variant ();
69+
public unowned string? get_brief ();
70+
[CCode (cname = "rxkb_layout_get_iso639_first")]
71+
public unowned Rxkb.Iso639Code? get_first_iso639 ();
72+
[CCode (cname = "rxkb_layout_get_iso3166_first")]
73+
public unowned Rxkb.Iso3166Code? get_first_iso3166 ();
74+
public unowned Rxkb.Layout @ref ();
75+
public void unref ();
76+
}
77+
78+
[CCode (cname = "struct rxkb_option_group", ref_function = "rxkb_option_group_ref", unref_function = "rxkb_option_group_unref", has_type_id = false)]
79+
[Compact]
80+
public class OptionGroup {
81+
public unowned Rxkb.OptionGroup? next ();
82+
public unowned string get_name ();
83+
public unowned string? get_description ();
84+
public bool allows_multiple ();
85+
public Rxkb.Popularity get_popularity ();
86+
[CCode (cname = "rxkb_option_first")]
87+
public unowned Rxkb.Option? get_first_option ();
88+
public unowned Rxkb.OptionGroup @ref ();
89+
public void unref ();
90+
}
91+
92+
[CCode (cname = "struct rxkb_option", ref_function = "rxkb_option_ref", unref_function = "rxkb_option_unref", has_type_id = false)]
93+
[Compact]
94+
public class Option {
95+
public unowned Rxkb.Option? next ();
96+
public unowned string get_name ();
97+
public unowned string? get_description ();
98+
public unowned string? get_brief ();
99+
public Rxkb.Popularity get_popularity ();
100+
public unowned Rxkb.Option @ref ();
101+
public void unref ();
102+
}
103+
104+
[CCode (cname = "struct rxkb_iso639_code", ref_function = "rxkb_iso639_code_ref", unref_function = "rxkb_iso639_code_unref", has_type_id = false)]
105+
[Compact]
106+
public class Iso639Code {
107+
public unowned Rxkb.Iso639Code? next ();
108+
public unowned string get_code ();
109+
public unowned Rxkb.Iso639Code @ref ();
110+
public void unref ();
111+
}
112+
113+
[CCode (cname = "struct rxkb_iso3166_code", ref_function = "rxkb_iso3166_code_ref", unref_function = "rxkb_iso3166_code_unref", has_type_id = false)]
114+
[Compact]
115+
public class Iso3166Code {
116+
public unowned Rxkb.Iso3166Code? next ();
117+
public unowned string get_code ();
118+
public unowned Rxkb.Iso3166Code @ref ();
119+
public void unref ();
120+
}
121+
122+
[CCode (cname = "enum rxkb_context_flags", cprefix = "RXKB_CONTEXT_", has_type_id = false)]
123+
[Flags]
124+
public enum ContextFlags {
125+
NO_FLAGS,
126+
NO_DEFAULT_INCLUDES,
127+
LOAD_EXOTIC_RULES
128+
}
129+
130+
[CCode (cname = "enum rxkb_popularity", cprefix = "RXKB_POPULARITY_", has_type_id = false)]
131+
public enum Popularity {
132+
STANDARD,
133+
EXOTIC
134+
}
135+
136+
[CCode (cname = "enum rxkb_log_level", cprefix = "RXKB_LOG_LEVEL_", has_type_id = false)]
137+
public enum LogLevel {
138+
CRITICAL,
139+
ERROR,
140+
WARNING,
141+
INFO,
142+
DEBUG
143+
}
144+
145+
[CCode (has_target = false, has_typedef = false)]
146+
public delegate void LogFn (Rxkb.Context ctx, Rxkb.LogLevel level, string format, va_list args);
147+
}

0 commit comments

Comments
 (0)