summaryrefslogtreecommitdiffstats
path: root/pyanaconda/keyboard.py
blob: c8ef80e06897ff555b27113a813c246b9abde1af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
#
# Copyright (C) 2012  Red Hat, Inc.
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions of
# the GNU General Public License v.2, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY expressed or implied, including the implied warranties of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
# Public License for more details.  You should have received a copy of the
# GNU General Public License along with this program; if not, write to the
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
# source code or documentation are not subject to the GNU General Public
# License and may only be used or replicated with the express permission of
# Red Hat, Inc.
#
# Red Hat Author(s): Martin Gracik <mgracik@redhat.com>
#                    Vratislav Podzimek <vpodzime@redhat.com>
#

"""
This module include functions and classes for dealing with multiple layouts
in Anaconda. It wraps the libxklavier functionality to protect Anaconda
from dealing with its "nice" API that looks like a Lisp-influenced
"good old C".

It provides a XklWrapper class with several methods that can be used
for listing and various modifications of keyboard layouts settings.

"""

import os
import dbus
from pyanaconda import iutil

# pylint: disable-msg=E0611
from gi.repository import Xkl

import logging
log = logging.getLogger("anaconda")

LOCALED_SERVICE = "org.freedesktop.locale1"
LOCALED_OBJECT_PATH = "/org/freedesktop/locale1"
LOCALED_IFACE = "org.freedesktop.locale1"

class KeyboardConfigError(Exception):
    """Exception class for keyboard configuration related problems"""

    pass

def _parse_layout_variant(layout):
    """
    Parse layout and variant from the string that may look like 'layout' or
    'layout (variant)'.

    @return: the (layout, variant) pair, where variant can be ""
    @rtype: tuple

    """

    variant = ""

    lbracket_idx = layout.find("(")
    rbracket_idx = layout.rfind(")")
    if lbracket_idx != -1:
        variant = layout[(lbracket_idx + 1) : rbracket_idx]
        layout = layout[:lbracket_idx].strip()

    return (layout, variant)

def _join_layout_variant(layout, variant=""):
    """
    Join layout and variant to form the commonly used 'layout (variant)'
    or 'layout' (if variant is missing) format.

    @type layout: string
    @type variant: string
    @return: 'layout (variant)' or 'layout' string
    @rtype: string

    """

    if variant:
        return "%s (%s)" % (layout, variant)
    else:
        return layout

def get_layouts_xorg_conf(keyboard):
    """
    Get the xorg.conf content setting up layouts in the ksdata.

    @param keyboard: ksdata.keyboard object
    @rtype: str

    """

    layouts = list()
    variants = list()

    for layout_variant in keyboard.x_layouts:
        (layout, variant) = _parse_layout_variant(layout_variant)
        layouts.append(layout)
        variants.append(variant)

    ret = "#This file was generated by the Anaconda installer\n"

    #section header
    ret += 'Section "InputClass"\n'\
           '\tIdentifier\t"anaconda-keyboard"\n'\
           '\tMatchIsKeyboard\t"on"\n'

    #layouts
    ret += '\tOption\t"XkbLayout"\t'
    ret += '"' + ','.join(layouts) + '"\n'

    #variants
    if any(variants):
        #write out this line only if some variants are specified
        ret += '\tOption\t"XkbVariant"\t'
        ret += '"' + ','.join(variants) + '"\n'

    #switching
    if any(keyboard.switch_options):
        ret += '\tOption\t"XkbOptions"\t'
        ret += '"' + ','.join(keyboard.switch_options) + '"\n'

    #section footer
    ret += 'EndSection'

    return ret

def populate_missing_items(keyboard):
    """
    Function that populates keyboard.vc_keymap and keyboard.x_layouts if
    they are missing. By invoking LocaledWrapper's methods this function
    MODIFIES CONFIGURATION FILES.

    @type keyboard: ksdata.keyboard object

    """

    localed = LocaledWrapper()

    if keyboard.x_layouts and not keyboard.vc_keymap:
        keyboard.vc_keymap = localed.set_and_convert_layout(keyboard.x_layouts[0])

    if not keyboard.vc_keymap:
        keyboard.vc_keymap = "us"

    if not keyboard.x_layouts:
        c_lay_var = localed.set_and_convert_keymap(keyboard.vc_keymap)
        keyboard.x_layouts.append(c_lay_var)

def write_keyboard_config(keyboard, root, convert=True, weight=0):
    """
    Function that writes files with layouts configuration to
    $root/etc/X11/xorg.conf.d/01-anaconda-layouts.conf and
    $root/etc/vconsole.conf.

    @param keyboard: ksdata.keyboard object
    @param root: path to the root of the installed system
    @param convert: whether to convert specified values to get the missing
                    ones
    @param weight: weight (prefix) of the xorg.conf file written out

    """

    if convert:
        populate_missing_items(keyboard)

    xconf_dir = os.path.normpath(root + "/etc/X11/xorg.conf.d")
    xconf_file = "%0.2d-anaconda-keyboard.conf" % weight

    vcconf_dir = os.path.normpath(root + "/etc")
    vcconf_file = "vconsole.conf"

    errors = []

    try:
        if not os.path.isdir(xconf_dir):
            os.makedirs(xconf_dir)

    except OSError as oserr:
        errors.append("Cannot create directory xorg.conf.d")

    if keyboard.x_layouts:
        try:
            with open(os.path.join(xconf_dir, xconf_file), "w") as fobj:
                fobj.write(get_layouts_xorg_conf(keyboard))
        except IOError as ioerr:
            errors.append("Cannot write X keyboard configuration file")

    if keyboard.vc_keymap:
        try:
            with open(os.path.join(vcconf_dir, vcconf_file), "w") as fobj:
                fobj.write('KEYMAP="%s"\n' % keyboard.vc_keymap)
        except IOError as ioerr:
            errors.append("Cannot write vconsole configuration file")

    if errors:
        raise KeyboardConfigError("\n".join(errors))

def dracut_setup_args(keyboard):
    """
    Function returning dracut setup args for the given keyboard configuration.

    @type keyboard: ksdata.keyboard object

    """

    if not keyboard.vc_keymap:
        populate_missing_items(keyboard)

    args = set()
    args.add("vconsole.keymap=%s" % keyboard.vc_keymap)

    return args

def _try_to_load_keymap(keymap):
    """
    Method that tries to load keymap and returns boolean indicating if it was
    successfull or not. It can be used to test if given string is VConsole
    keymap or not, but in case it is given valid keymap, IT REALLY LOADS IT!.

    @type keymap: string
    @raise KeyboardConfigError: if loadkeys command is not available
    @return: True if given string was a valid keymap and thus was loaded,
             False otherwise

    """

    # BUG: systemd-localed should be able to tell us if we are trying to
    #      activate invalid keymap. Then we will be able to get rid of this
    #      fuction

    ret = 0

    try:
        ret = iutil.execWithRedirect("loadkeys", [keymap])
    except OSError as oserr:
        msg = "'loadkeys' command not available (%s)" % oserr.strerror
        raise KeyboardConfigError(msg)

    return ret == 0

def activate_keyboard(keyboard):
    """
    Try to setup VConsole keymap and X11 layouts as specified in kickstart.

    @param keyboard: ksdata.keyboard object
    @type keyboard: ksdata.keyboard object

    """

    localed = LocaledWrapper()
    c_lay_var = ""
    c_keymap = ""

    if keyboard._keyboard and not (keyboard.vc_keymap or keyboard.x_layouts):
        # we were give only one value in old format of the keyboard command
        # try to guess if we were given VConsole keymap or X11 layout
        is_keymap = _try_to_load_keymap(keyboard._keyboard)

        if is_keymap:
            keyboard.vc_keymap = keyboard._keyboard
        else:
            keyboard.x_layouts.append(keyboard._keyboard)

    if keyboard.vc_keymap:
        valid_keymap = _try_to_load_keymap(keyboard.vc_keymap)
        if not valid_keymap:
            log.error("'%s' is not a valid VConsole keymap, not loading" % \
                        keyboard.vc_keymap)
        else:
            # activate VConsole keymap and get converted layout and variant
            c_lay_var = localed.set_and_convert_keymap(keyboard.vc_keymap)

    if not keyboard.x_layouts and c_lay_var:
        keyboard.x_layouts.append(c_lay_var)

    if keyboard.x_layouts:
        c_keymap = localed.set_and_convert_layout(keyboard.x_layouts[0])

        if not keyboard.vc_keymap:
            keyboard.vc_keymap = c_keymap

        # write out full configuration that will be loaded by X server
        # (systemd-localed writes configuration with only one layout)
        write_keyboard_config(keyboard, root="/", convert=False, weight=99)

def item_str(s):
    """Convert a zero-terminated byte array to a proper str"""

    i = s.find(b'\x00')
    return s[:i].decode("utf-8") #there are some non-ascii layout descriptions

class _Layout(object):
    """Internal class representing a single layout variant"""

    def __init__(self, name, desc):
        self.name = name
        self.desc = desc

    def __str__(self):
        return '%s (%s)' % (self.name, self.desc)

    def __eq__(self, obj):
        return isinstance(obj, self.__class__) and \
            self.name == obj.name

    @property
    def description(self):
        return self.desc

class XklWrapperError(KeyboardConfigError):
    """Exception class for reporting libxklavier-related problems"""

    pass

class XklWrapper(object):
    """
    Class wrapping the libxklavier functionality

    Use this class as a singleton class because it provides read-only data
    and initialization (that takes quite a lot of time) reads always the
    same data. It doesn't have sense to make multiple instances

    """

    _instance = None

    @staticmethod
    def get_instance():
        if not XklWrapper._instance:
            XklWrapper._instance = XklWrapper()

        return XklWrapper._instance

    def __init__(self):
        # pylint: disable-msg=E0611
        from gi.repository import GdkX11

        #initialize Xkl-related stuff
        display = GdkX11.x11_get_default_xdisplay()
        self._engine = Xkl.Engine.get_instance(display)

        self._rec = Xkl.ConfigRec()
        if not self._rec.get_from_server(self._engine):
            raise XklWrapperError("Failed to get configuration from server")

        #X is probably initialized to the 'us' layout without any variant and
        #since we want to add layouts with variants we need the layouts and
        #variants lists to have the same length. Add "" padding to variants.
        #See docstring of the add_layout method for details.
        diff = len(self._rec.layouts) - len(self._rec.variants)
        if diff > 0:
            self._rec.set_variants(self._rec.variants + (diff * [""]))
            if not self._rec.activate(self._engine):
                raise XklWrapperError("Failed to initialize layouts")

        #needed also for Gkbd.KeyboardDrawingDialog
        self.configreg = Xkl.ConfigRegistry.get_instance(self._engine)
        self.configreg.load(False)

        self._language_keyboard_variants = dict()
        self._country_keyboard_variants = dict()
        self._switching_options = list()

        #we want to display layouts as 'language (description)'
        self.name_to_show_str = dict()

        #we want to display layout switching options as e.g. "Alt + Shift" not
        #as "grp:alt_shift_toggle"
        self.switch_to_show_str = dict()

        #this might take quite a long time
        self.configreg.foreach_language(self._get_language_variants, None)
        self.configreg.foreach_country(self._get_country_variants, None)

        #'grp' means that we want layout (group) switching options
        self.configreg.foreach_option('grp', self._get_switch_option, None)

    def _get_variant(self, c_reg, item, subitem, lang):
        if subitem:
            name = item_str(item.name) + " (" + item_str(subitem.name) + ")"
            description = item_str(subitem.description)
        else:
            name = item_str(item.name)
            description = item_str(item.description)

        #if this layout has already been added for some other language,
        #do not add it again (would result in duplicates in our lists)
        if lang and name not in self.name_to_show_str:
            self.name_to_show_str[name] = "%s (%s)" % (lang.encode("utf-8"),
                                                    description.encode("utf-8"))
            self._variants_list.append(_Layout(name, description))
        elif lang is None:
            #if lang is None, we are iterating over countries not languages
            #and should just append the layout to the list
            self._variants_list.append(_Layout(name, description))

    def _get_language_variants(self, c_reg, item, user_data=None):
        #helper "global" variable
        self._variants_list = list()
        lang_name, lang_desc = item_str(item.name), item_str(item.description)

        c_reg.foreach_language_variant(lang_name, self._get_variant, lang_desc)

        self._language_keyboard_variants[lang_desc] = self._variants_list

    def _get_country_variants(self, c_reg, item, user_data=None):
        #helper "global" variable
        self._variants_list = list()
        country_name, country_desc = item_str(item.name), item_str(item.description)

        c_reg.foreach_country_variant(country_name, self._get_variant, None)

        self._country_keyboard_variants[country_name] = self._variants_list

    def _get_switch_option(self, c_reg, item, user_data=None):
        """Helper function storing layout switching options in foreach cycle"""
        desc = item_str(item.description)
        name = item_str(item.name)

        self._switching_options.append(name)
        self.switch_to_show_str[name] = desc.encode("utf-8")

    def get_available_layouts(self):
        """A generator yielding layouts (no need to store them as a bunch)"""

        for lang_desc, variants in sorted(self._language_keyboard_variants.items()):
            for layout in variants:
                yield layout.name

    def get_switching_options(self):
        """Method returning list of available layout switching options"""

        return self._switching_options

    def get_default_language_layout(self, language):
        """Get the default layout for a given language."""

        layouts = self.get_language_layouts(language)
        if layouts:
            #first layout (should exist for every language)
            return layouts[0].name
        else:
            return None

    def get_language_layouts(self, language):
        """Get layouts for a given language."""

        language_layouts = self._language_keyboard_variants.get(language, None)

        if language_layouts:
            return language_layouts

        #else try some magic and if everything fails, return None
        for (lang, layouts) in self._language_keyboard_variants.iteritems():
            #XXX: some languages are returned in a weird form from the
            #     libxklavier iterations (e.g. "Greek, Modern (1453-)")
            if lang.startswith(language):
                return layouts

        return None

    def get_default_lang_country_layout(self, language, country):
        """
        Get default layout matching both language and country. If none such
        layout is found, get default layout for language. If no layout for
        the given language is found but there is layout for the given country,
        return the one for the country.

        """

        language_layouts = self.get_language_layouts(language)
        country_layouts = self._country_keyboard_variants.get(country, None)
        if not language_layouts and not country_layouts:
            return None

        if not country_layouts:
            return language_layouts[0].name

        if not language_layouts:
            return country_layouts[0].name

        matches_both = (layout for layout in language_layouts
                                if layout in country_layouts)

        try:
            return matches_both.next().name
        except StopIteration:
            if country_layouts:
                return country_layouts[0].name
            else:
                return language_layouts[0].name

    def get_current_layout_name(self):
        """
        Get current activated X layout's name

        @return: current activated X layout's name (e.g. "Czech (qwerty)")

        """

        self._engine.start_listen(Xkl.EngineListenModes.TRACK_KEYBOARD_STATE)
        state = self._engine.get_current_state()
        groups_names = self._engine.get_groups_names()
        self._engine.stop_listen(Xkl.EngineListenModes.TRACK_KEYBOARD_STATE)

        return groups_names[state.group]

    def activate_default_layout(self):
        """
        Activates default layout (the first one in the list of configured
        layouts).

        """

        self._engine.lock_group(0)

    def is_valid_layout(self, layout):
        """Return if given layout is valid layout or not"""

        return layout in self.name_to_show_str

    def add_layout(self, layout):
        """
        Method that tries to add a given layout to the current X configuration.

        The X layouts configuration is handled by two lists. A list of layouts
        and a list of variants. Index-matching items in these lists (as if they
        were zipped) are used for the construction of real layouts (e.g.
        'cz (qwerty)').

        @param layout: either 'layout' or 'layout (variant)'
        @raise XklWrapperError: if the given layout cannot be added

        """

        #we can get 'layout' or 'layout (variant)'
        (layout, variant) = _parse_layout_variant(layout)

        #do not add the same layout-variant combinanion multiple times
        if (layout, variant) in zip(self._rec.layouts, self._rec.variants):
            return

        self._rec.set_layouts(self._rec.layouts + [layout])
        self._rec.set_variants(self._rec.variants + [variant])

        if not self._rec.activate(self._engine):
            raise XklWrapperError("Failed to add layout '%s (%s)'" % (layout,
                                                                      variant))

    def remove_layout(self, layout):
        """
        Method that tries to remove a given layout from the current X
        configuration.

        See also the documentation for the add_layout method.

        @param layout: either 'layout' or 'layout (variant)'
        @raise XklWrapperError: if the given layout cannot be removed

        """

        #we can get 'layout' or 'layout (variant)'
        (layout, variant) = _parse_layout_variant(layout)

        layouts_variants = zip(self._rec.layouts, self._rec.variants)

        if not (layout, variant) in layouts_variants:
            msg = "'%s (%s)' not in the list of added layouts" % (layout,
                                                                  variant)
            raise XklWrapperError(msg)

        idx = layouts_variants.index((layout, variant))
        new_layouts = self._rec.layouts[:idx] + self._rec.layouts[(idx + 1):]
        new_variants = self._rec.variants[:idx] + self._rec.variants[(idx + 1):]

        self._rec.set_layouts(new_layouts)
        self._rec.set_variants(new_variants)

        if not self._rec.activate(self._engine):
            raise XklWrapperError("Failed to remove layout '%s (%s)'" % (layout,
                                                                       variant))
    def replace_layouts(self, layouts_list):
        """
        Method that replaces the layouts defined in the current X configuration
        with the new ones given.

        @param layouts_list: list of layouts defined as either 'layout' or
                             'layout (variant)'
        @raise XklWrapperError: if layouts cannot be replaced with the new ones

        """

        new_layouts = list()
        new_variants = list()

        for layout_variant in layouts_list:
            (layout, variant) = _parse_layout_variant(layout_variant)
            new_layouts.append(layout)
            new_variants.append(variant)

        self._rec.set_layouts(new_layouts)
        self._rec.set_variants(new_variants)

        if not self._rec.activate(self._engine):
            msg = "Failed to replace layouts with: %s" % ",".join(layouts_list)
            raise XklWrapperError(msg)

    def set_switching_options(self, options):
        """
        Method that sets options for layout switching. It replaces the old
        options with the new ones.

        @param options: layout switching options to be set
        @type options: list or generator
        @raise XklWrapperError: if the old options cannot be replaced with the
                                new ones

        """

        #preserve old "non-switching options"
        new_options = [opt for opt in self._rec.options if "grp:" not in opt]
        new_options += options

        self._rec.set_options(new_options)

        if not self._rec.activate(self._engine):
            msg = "Failed to set switching options to: %s" % ",".join(options)
            raise XklWrapperError(msg)

class LocaledWrapperError(KeyboardConfigError):
    """Exception class for reporting Localed-related problems"""
    pass

class LocaledWrapper(object):
    """
    Class wrapping systemd-localed daemon functionality.

    """

    def __init__(self):
        bus = dbus.SystemBus()

        try:
            localed = bus.get_object(LOCALED_SERVICE, LOCALED_OBJECT_PATH)
        except dbus.DBusException:
            raise LocaledWrapperError("Failed to get locale object")

        try:
            self._locale_iface = dbus.Interface(localed, LOCALED_IFACE)
        except dbus.DBusException:
            raise LocaledWrapperError("Failed to get locale interface")

        try:
            self._props_iface = dbus.Interface(localed, dbus.PROPERTIES_IFACE)
        except dbus.DBusException:
            raise LocaledWrapperError("Failed to get properties interface")

    def set_and_convert_keymap(self, keymap):
        """
        Method that sets VConsole keymap and returns X11 layout and
        variant that (systemd-localed thinks) match given keymap best.

        @return: string containing "layout (variant)" or "layout" if variant
                 is missing
        @rtype: string

        """

        # args: keymap, keymap_toggle, convert, user_interaction
        # where convert indicates whether the keymap should be converted
        # to X11 layout and user_interaction indicates whether PolicyKit
        # should ask for credentials or not
        try:
            self._locale_iface.SetVConsoleKeyboard(keymap, "", True, False)
        except dbus.DBusException:
            msg = "Failed to call SetVConsoleKeyboard method"
            raise LocaledWrapperError(msg)

        try:
            layout = self._props_iface.Get(LOCALED_IFACE, "X11Layout")
        except dbus.DBusException:
            raise LocaledWrapperError("locale has no X11Layout property")

        try:
            variant = self._props_iface.Get(LOCALED_IFACE, "X11Variant")
        except dbus.DBusException:
            raise LocaledWrapperError("locale has no X11Variant property")

        return _join_layout_variant(layout, variant)

    def set_and_convert_layout(self, layout_variant):
        """
        Method that sets X11 layout and variant (for later X sessions)
        and returns VConsole keymap that (systemd-localed thinks) matches
        given layout and variant best.

        @return: a keymap matching layout and variant best
        @rtype: string

        """

        (layout, variant) = _parse_layout_variant(layout_variant)

        # args: layout, model, variant, options, convert, user_interaction
        # where convert indicates whether the keymap should be converted
        # to X11 layout and user_interaction indicates whether PolicyKit
        # should ask for credentials or not
        try:
            self._locale_iface.SetX11Keyboard(layout, "", variant, "", True, False)
        except dbus.DBusException:
            msg = "Failed to call SetX11Keyboard method"
            raise LocaledWrapperError(msg)

        try:
            return self._props_iface.Get(LOCALED_IFACE, "VConsoleKeymap")
        except dbus.DBusException:
            raise LocaledWrapperError("locale has no VConsoleKeymap property")