summaryrefslogtreecommitdiff
path: root/tests/test_gi.py
blob: c71412d0c56e48a8ca99802e9ce850da54cf1da4 (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
#!/usr/bin/python
#
# Test GI binding of libxklavier
# Copyright (C) 2011 Martin Pitt <martin.pitt@ubuntu.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.

import sys
import os

# use source tree typelib
os.environ['GI_TYPELIB_PATH'] = 'libxklavier:' + os.environ.get('GI_TYPELIB_PATH', '')

from gi.repository import Xkl, Gdk, GdkX11

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

    i = s.find(b'\x00')
    return s[:i].decode()

display = GdkX11.x11_get_default_xdisplay()

print('== Engine ==')
engine = Xkl.Engine.get_instance(display)

print('indicator names:', engine.get_indicators_names())
print('group names:', engine.get_groups_names())
print('default layout:', engine.get_groups_names()[engine.get_default_group()])
print('features: %X' % engine.get_features())
st = engine.get_current_state()
print('current state: group: %i, indicators: %u' % (st.group, st.indicators))

# check ConfigItem ctor with data
i = Xkl.ConfigItem()
assert item_str(i.name) == ''
i.set_name('fr')
assert item_str(i.name) == 'fr'

# load registry
registry = Xkl.ConfigRegistry.get_instance(engine)

if not registry.load(False):
    print('Failed to load registry')
    sys.exit(1)

print('\n== Available Layouts ==')
def layout_iter(registry, item, data):
    print('[%s] %s, ' % (item_str(item.name), item_str(item.description)))

registry.foreach_layout(layout_iter, None)
print()

print('\n== ConfigRec ==')
rec = Xkl.ConfigRec()
if not rec.get_from_server(engine):
    print('Failed to get configuration from server')
    sys.exit(1)

print('Curent configuration:')
print('  Model:', rec.model)
print('  Layouts:', rec.layouts)
print('  Variants:', rec.variants)
print('  Options:', rec.options)

print('Adding Danish layout...')
rec.set_layouts(rec.layouts + ['dk'])
rec.set_variants(rec.variants + [''])
if not rec.activate(engine):
    print('Failed to activate new configuration')

print('Curent configuration:')
rec = Xkl.ConfigRec()
if not rec.get_from_server(engine):
    print('Failed to get configuration from server')
    sys.exit(1)
print('  Model:', rec.model)
print('  Layouts:', rec.layouts)
print('  Variants:', rec.variants)
print('  Options:', rec.options)

print('Removing Danish layout...')
rec.set_layouts(rec.layouts[:-1])
rec.set_variants(rec.variants[:-1])
if not rec.activate(engine):
    print('Failed to activate new configuration')

print('Curent configuration:')
rec = Xkl.ConfigRec()
if not rec.get_from_server(engine):
    print('Failed to get configuration from server')
    sys.exit(1)
print('  Model:', rec.model)
print('  Layouts:', rec.layouts)
print('  Variants:', rec.variants)
print('  Options:', rec.options)

print('Changing model to "pc105"...')
original_model = rec.model
rec.set_model("pc105")

print('Curent configuration:')
print('  Model:', rec.model)
print('  Layouts:', rec.layouts)
print('  Variants:', rec.variants)
print('  Options:', rec.options)

print('Changing back to original model...')
rec.set_model(original_model)

print('Curent configuration:')
print('  Model:', rec.model)
print('  Layouts:', rec.layouts)
print('  Variants:', rec.variants)
print('  Options:', rec.options)

print
original_options = rec.options[:]
if not any("toggle" in opt for opt in rec.options):
    print('Adding Alt+Shift as an option for switching layouts...')
    rec.set_options(rec.options + ["grp:alt_shift_toggle"])
    if not rec.activate(engine):
        print('Failed to activate new configuration')

original_layouts = rec.layouts[:]
original_variants = rec.variants[:]
if len(rec.layouts) == 1:
    print('Adding Danish layout...')
    rec.set_layouts(rec.layouts + ['dk'])
    rec.set_variants(rec.variants + [''])
    if not rec.activate(engine):
        print('Failed to activate new configuration')

engine.start_listen(Xkl.EngineListenModes.TRACK_KEYBOARD_STATE)
groups_names = engine.get_groups_names()
state = engine.get_current_state()
engine.start_listen(Xkl.EngineListenModes.TRACK_KEYBOARD_STATE)
curent_layout = groups_names[state.group]

print('Curent active layout: %s' % curent_layout)
raw_input('Please change your layout and hit ENTER')

engine.start_listen(Xkl.EngineListenModes.TRACK_KEYBOARD_STATE)
groups_names = engine.get_groups_names()
state = engine.get_current_state()
engine.start_listen(Xkl.EngineListenModes.TRACK_KEYBOARD_STATE)
curent_layout = groups_names[state.group]

print('Curent active layout: %s' % curent_layout)

print('Restoring original configuration...')
rec.set_layouts(original_layouts)
rec.set_variants(original_variants)
rec.set_options(original_options)
if not rec.activate(engine):
    print('Failed to activate new configuration')