-
Notifications
You must be signed in to change notification settings - Fork 148
/
keyboard.py
294 lines (267 loc) · 9.02 KB
/
keyboard.py
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
# Copyright 2015 Canonical, Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import logging
import os
import pwd
from typing import Dict, Optional, Sequence, Tuple
import attr
from subiquity.common.apidef import API
from subiquity.common.resources import resource_path
from subiquity.common.serialize import Serializer
from subiquity.common.types import AnyStep, KeyboardSetting, KeyboardSetup
from subiquity.server.controller import SubiquityController
from subiquity.server.curtin import run_curtin_command
from subiquitycore.context import with_context
from subiquitycore.utils import arun_command
log = logging.getLogger("subiquity.server.controllers.keyboard")
# Non-latin keyboard layouts that are handled in a uniform way
standard_non_latin_layouts = set(
(
"af",
"am",
"ara",
"ben",
"bd",
"bg",
"bt",
"by",
"et",
"ge",
"gh",
"gr",
"guj",
"guru",
"il",
"in",
"iq",
"ir",
"iku",
"kan",
"kh",
"kz",
"la",
"lao",
"lk",
"kg",
"ma",
"mk",
"mm",
"mn",
"mv",
"mal",
"np",
"ori",
"pk",
"ru",
"scc",
"sy",
"syr",
"tel",
"th",
"tj",
"tam",
"tib",
"ua",
"ug",
"uz",
)
)
default_desktop_user = "ubuntu"
def latinizable(layout_code, variant_code) -> Optional[Tuple[str, str]]:
"""
If this setting does not allow the typing of latin characters,
return a setting that can be switched to one that can.
"""
if layout_code == "rs":
if variant_code.startswith("latin"):
return None
else:
if variant_code == "yz":
new_variant_code = "latinyz"
elif variant_code == "alternatequotes":
new_variant_code = "latinalternatequotes"
else:
new_variant_code = "latin"
return "rs,rs", new_variant_code + "," + variant_code
elif layout_code == "jp":
if variant_code in ("106", "common", "OADG109A", "nicola_f_bs", ""):
return None
else:
return "jp,jp", "," + variant_code
elif layout_code == "lt":
if variant_code == "us":
return "lt,lt", "us,"
else:
return "lt,lt", variant_code + ",us"
elif layout_code == "me":
if variant_code == "basic" or variant_code.startswith("latin"):
return None
else:
return "me,me", variant_code + ",us"
elif layout_code in standard_non_latin_layouts:
return "us," + layout_code, "," + variant_code
else:
return None
def for_ui(setting):
"""
Attempt to guess a setting the user chose which resulted in the
current config. Basically the inverse of latinizable().
"""
if "," in setting.layout:
layout1, layout2 = setting.layout.split(",", 1)
else:
layout1, layout2 = setting.layout, ""
if "," in setting.variant:
variant1, variant2 = setting.variant.split(",", 1)
else:
variant1, variant2 = setting.variant, ""
if setting.layout == "lt,lt":
layout = layout1
variant = variant1
elif setting.layout in ("rs,rs", "us,rs", "jp,jp", "us,jp"):
layout = layout2
variant = variant2
elif layout1 == "us" and layout2 in standard_non_latin_layouts:
layout = layout2
variant = variant2
elif "," in setting.layout:
# Something unrecognized
layout = "us"
variant = ""
else:
return setting
return KeyboardSetting(layout=layout, variant=variant, toggle=setting.toggle)
class KeyboardController(SubiquityController):
endpoint = API.keyboard
autoinstall_key = model_name = "keyboard"
autoinstall_schema = {
"type": "object",
"properties": {
"layout": {"type": "string"},
"variant": {"type": "string"},
"toggle": {"type": ["string", "null"]},
},
"required": ["layout"],
"additionalProperties": False,
}
def __init__(self, app):
self._kbds_dir = resource_path("kbds")
self.serializer = Serializer(compact=True)
self.pc105_steps = None
self.needs_set_keyboard = False
super().__init__(app)
def load_autoinstall_data(self, data):
if data is None:
return
setting = KeyboardSetting(**data)
if self.model.setting != setting:
self.needs_set_keyboard = True
self.model.setting = setting
@with_context()
async def apply_autoinstall_config(self, context):
if self.needs_set_keyboard:
await self.set_keyboard()
def make_autoinstall(self):
return attr.asdict(self.model.setting)
async def set_keyboard(self):
path = self.model.config_path
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w") as fp:
fp.write(self.model.render_config_file())
cmds = [
["setupcon", "--save", "--force", "--keyboard-only"],
[resource_path("bin/subiquity-loadkeys")],
]
if self.opts.dry_run:
scale = os.environ.get("SUBIQUITY_REPLAY_TIMESCALE", "1")
cmds = [["sleep", str(1 / float(scale))]]
for cmd in cmds:
await arun_command(cmd)
async def GET(self) -> KeyboardSetup:
lang = self.app.base_model.locale.selected_language
self.model.keyboard_list.load_language(lang)
return KeyboardSetup(
setting=for_ui(self.model.setting_for_lang(lang)),
layouts=self.model.keyboard_list.layouts,
)
async def POST(self, data: KeyboardSetting):
log.debug(data)
new = latinizable(data.layout, data.variant)
if new is not None:
data = KeyboardSetting(new[0], new[1], data.toggle)
if data != self.model.setting:
self.model.setting = data
await self.set_keyboard()
await self.configured()
async def needs_toggle_GET(self, layout_code: str, variant_code: str) -> bool:
return latinizable(layout_code, variant_code) is not None
async def steps_GET(self, index: Optional[str]) -> AnyStep:
if self.pc105_steps is None:
path = os.path.join(self._kbds_dir, "pc105.json")
with open(path) as fp:
self.pc105_steps = self.serializer.from_json(
Dict[str, AnyStep], fp.read()
)
if index is None:
index = "0"
return self.pc105_steps[index]
async def input_source_POST(
self, data: KeyboardSetting, user: Optional[str] = None
) -> None:
await self.set_input_source(data.layout, data.variant, user=user)
async def set_input_source(self, layout: str, variant: str, **kwargs):
xkb_value = f"{layout}+{variant}" if variant else layout
gsettings = [
"gsettings",
"set",
"org.gnome.desktop.input-sources",
"sources",
f"[('xkb','{xkb_value}')]",
]
await self._run_gui_command(gsettings, **kwargs)
@with_context(description="configuring keyboard")
async def setup_target(self, context):
await run_curtin_command(
self.app,
context,
"in-target",
"-t",
self.app.base_model.target,
"--",
"setupcon",
"--save-only",
private_mounts=False,
)
async def _run_gui_command(
self, command: Sequence[str], user: Optional[str] = None
):
if self.opts.dry_run:
scale = os.environ.get("SUBIQUITY_REPLAY_TIMESCALE", "1")
cmd = ["sleep", str(1 / float(scale))]
else:
passwd = pwd.getpwnam(user if user else default_desktop_user)
xdg_runtime_dir = f"/run/user/{passwd.pw_uid}"
dbus_session_bus = f"unix:path={xdg_runtime_dir}/bus"
cmd = [
"systemd-run",
"--wait",
f"--uid={passwd.pw_uid}",
f'--setenv=DISPLAY={os.environ.get("DISPLAY", ":0")}',
f"--setenv=XDG_RUNTIME_DIR={xdg_runtime_dir}",
f"--setenv=DBUS_SESSION_BUS_ADDRESS={dbus_session_bus}",
"--",
*command,
]
return await arun_command(cmd)