-
Notifications
You must be signed in to change notification settings - Fork 26
/
keyboard.go
312 lines (261 loc) · 8.96 KB
/
keyboard.go
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
// Package keyboard implements the Keyboard gRPC service.
package keyboard
import (
"bufio"
"context"
"encoding/json"
"fmt"
"log/slog"
"regexp"
"strings"
"github.com/canonical/ubuntu-desktop-provision/provd/internal/consts"
pb "github.com/canonical/ubuntu-desktop-provision/provd/protos"
"github.com/godbus/dbus/v5"
"github.com/linuxdeepin/go-gir/gio-2.0"
"github.com/linuxdeepin/go-gir/glib-2.0"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
)
// gSettingsValueSetter is a minimal subset of the GSettings interface to make for easier mocking.
type gSettingsValueSetter interface {
IsWritable(key string) bool
SetValue(key string, variant *glib.Variant) bool
}
// Option is a functional option to set the DBus objects in tests.
type Option func(*Service) error
// Service is the implementation of the Keyboard service.
type Service struct {
pb.UnimplementedKeyboardServiceServer
conn *dbus.Conn
locale dbus.BusObject
gsettings gSettingsValueSetter
keyboardl18nPath string
}
// New returns a new instance of the Keyboard service.
func New(conn *dbus.Conn, opts ...Option) (*Service, error) {
s := &Service{
conn: conn,
}
s.locale = conn.Object("org.freedesktop.locale1", dbus.ObjectPath("/org/freedesktop/locale1"))
// Create GSettings object
s.gsettings = gio.NewSettings("org.gnome.desktop.input-sources")
// Set the path to the keyboard configuration file
s.keyboardl18nPath = "kbds/"
// Applying options, checking for errors in obtaining DBus objects
for _, opt := range opts {
if err := opt(s); err != nil {
return nil, err
}
}
// Check if is writable as a ping test
isWritable := s.gsettings.IsWritable("sources")
if !isWritable {
return nil, status.Errorf(codes.Internal, "failed to connect to gsettings")
}
// Ping object to ensure it is reachable
err := s.locale.Call(consts.DbusPeerPrefix+".Ping", 0).Err
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to ping default DBus locale1 object")
}
return s, nil
}
// SetInputSource sets the input source for the current user via gsettings.
func (s *Service) SetInputSource(ctx context.Context, req *pb.SetInputSourceRequest) (*emptypb.Empty, error) {
slog.Info(fmt.Sprintf("SetInputSource called with request: %v", req))
// Validate request
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "received a nil request")
}
if req.Settings == nil {
return nil, status.Errorf(codes.InvalidArgument, "received a nil settings")
}
if req.Settings.Layout == "" {
return nil, status.Errorf(codes.InvalidArgument, "received an empty layout")
}
var variant string
if req.Settings.Variant != "" {
variant = "+" + req.Settings.Variant
}
xkbString := req.Settings.Layout + variant
sources, err := glib.VariantParse(glib.NewVariantType("a(ss)"), "[('xkb', '"+xkbString+"')]", "", "")
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to parse input source into GVariant: %v", err)
}
if success := s.gsettings.SetValue("sources", sources); !success {
return nil, status.Errorf(codes.Internal, "failed to set input source")
}
slog.Info("SetInputSource completed successfully")
return &emptypb.Empty{}, nil
}
var localRegexp = regexp.MustCompile(`LANG=([a-z]+)_`)
// GetKeyboard returns the current keyboard layout and available layouts.
func (s *Service) GetKeyboard(ctx context.Context, req *emptypb.Empty) (*pb.GetKeyboardResponse, error) {
slog.Info(fmt.Sprintf("GetKeyboard called with request: %v", req))
// Validate request
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "received a nil request")
}
// Get the current keyboard layout & variant
x11Layout, err := s.locale.GetProperty(consts.DbusLocalePrefix + ".X11Layout")
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get X11Layout: %v", err)
}
x11Variant, err := s.locale.GetProperty(consts.DbusLocalePrefix + ".X11Variant")
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get X11Variant: %v", err)
}
// Get the current language
var lang string
l, err := s.locale.GetProperty(consts.DbusLocalePrefix + ".Locale")
if err != nil {
lang = "C"
} else {
matches := localRegexp.FindStringSubmatch(l.String())
if len(matches) < 2 {
return nil, status.Errorf(codes.Internal, "unexpected locale format")
}
lang = matches[1]
}
// Check if locale has a jsonl file avalible
filename := fmt.Sprintf("kbds/%s.jsonl", lang)
f, err := EmbeddedFiles.Open(filename)
if err != nil {
lang = "C"
} else {
f.Close()
}
// Type assertions
layout, ok := x11Layout.Value().(string)
if !ok {
return nil, status.Errorf(codes.Internal, "failed to parse X11Layout: %v", err)
}
variant, ok := x11Variant.Value().(string)
if !ok {
return nil, status.Errorf(codes.Internal, "failed to parse X11Variant: %v", err)
}
// Get available keyboard layouts
layouts, err := getKeyboardLayouts(s.keyboardl18nPath, lang)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get keyboard layouts: %v", err)
}
// Create response
resp := &pb.GetKeyboardResponse{
Setup: &pb.KeyboardSetup{
Settings: &pb.KeyboardSettings{
Layout: layout,
Variant: variant,
},
Layouts: layouts,
},
}
slog.Info("GetKeyboard completed successfully")
return resp, nil
}
// SetKeyboard sets the keyboard layout for the current user via DBus.
func (s *Service) SetKeyboard(ctx context.Context, req *pb.SetKeyboardRequest) (*emptypb.Empty, error) {
slog.Info(fmt.Sprintf("SetKeyboard called with request: %v", req))
// Validate request
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "received a nil request")
}
if req.Settings == nil {
return nil, status.Errorf(codes.InvalidArgument, "received a nil settings")
}
if req.Settings.Layout == "" {
return nil, status.Errorf(codes.InvalidArgument, "received a nil layout")
}
x11Model, err := s.locale.GetProperty(consts.DbusLocalePrefix + ".X11Model")
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get X11Model: %v", err)
}
x11Options, err := s.locale.GetProperty(consts.DbusLocalePrefix + ".X11Options")
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get X11Options: %v", err)
}
x11ModelStr, ok := x11Model.Value().(string)
if !ok {
return nil, status.Errorf(codes.Internal, "failed to get X11Model: %v", err)
}
x11OptionsStr, ok := x11Options.Value().(string)
if !ok {
return nil, status.Errorf(codes.Internal, "failed to get X11Options: %v", err)
}
call := s.locale.Call(consts.DbusLocalePrefix+".SetX11Keyboard", 0, req.Settings.Layout, x11ModelStr, req.Settings.Variant, x11OptionsStr, false, false)
err = call.Err
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to set X11 keyboard: %v", err)
}
slog.Info("SetKeyboard completed successfully")
return &emptypb.Empty{}, nil
}
func getKeyboardLayouts(keyboardl18nPath string, current string) ([]*pb.KeyboardLayout, error) {
var layouts []*pb.KeyboardLayout
// Read in keyboard layouts
f, err := EmbeddedFiles.Open(keyboardl18nPath + current + ".jsonl")
if err != nil {
return nil, fmt.Errorf("failed to read keyboard layouts: %v", err)
}
defer f.Close()
defer func() {
if err != nil {
slog.Error(fmt.Sprintf("Error occurred while processing file %s.jsonl: %v", current, err))
}
}()
s := bufio.NewScanner(f)
// Loop through each line in the jsonl file
for s.Scan() {
line := strings.TrimSpace(s.Text()) // Trim any leading/trailing whitespace
if line == "" {
continue // Skip any empty line
}
// Unmarshal line into a keyboard layout
var rawLayout []interface{}
err := json.Unmarshal([]byte(line), &rawLayout)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal keyboard layout: %v", err)
}
// Get the layout code
c, ok := rawLayout[0].(string)
if !ok {
return nil, fmt.Errorf("failed to parse keyboard layout code: %v", err)
}
// Get the layout name
n, ok := rawLayout[1].(string)
if !ok {
return nil, fmt.Errorf("failed to parse keyboard layout name: %v", err)
}
l := &pb.KeyboardLayout{
Code: c,
Name: n,
}
// Get the layout variants
rawVariants, ok := rawLayout[2].([]interface{})
if !ok {
return nil, fmt.Errorf("failed to parse keyboard layout variant: %v", err)
}
for j := 0; j < len(rawVariants); j++ {
rawVariant, ok := rawVariants[j].([]interface{})
if !ok {
return nil, fmt.Errorf("failed to parse keyboard layout variant: %v", err)
}
c, ok := rawVariant[0].(string)
if !ok {
return nil, fmt.Errorf("failed to parse keyboard layout variant code: %v", err)
}
n, ok := rawVariant[1].(string)
if !ok {
return nil, fmt.Errorf("failed to parse keyboard layout variant name: %v", err)
}
l.Variants = append(l.Variants, &pb.KeyboardVariant{
Code: c,
Name: n,
})
}
layouts = append(layouts, l)
}
if err := s.Err(); err != nil {
return nil, fmt.Errorf("error while scanning jsonl file: %v", err)
}
return layouts, nil
}