Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions LICENSES/MIT.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2026 UnionTech Software Technology Co., Ltd.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ TEST = \
${GOPKG_PREFIX}/image_effect1 \
${GOPKG_PREFIX}/inputdevices1 \
${GOPKG_PREFIX}/inputdevices1/iso639 \
${GOPKG_PREFIX}/inputdevices1/internal/inputmanager \
${GOPKG_PREFIX}/langselector1 \
${GOPKG_PREFIX}/lastore1 \
${GOPKG_PREFIX}/loader \
Expand Down Expand Up @@ -110,6 +111,15 @@ LANGUAGES = $(basename $(notdir $(wildcard misc/po/*.po)))

all: build

WAYLAND_SCANNER ?= go run ../dde-api/cmd/go-wayland-scanner/main.go
TREELAND_INPUT_MANAGER_XML ?= /usr/share/treeland-protocols/treeland-input-manager-unstable-v1.xml

.PHONY: generate-treeland-input-manager
generate-treeland-input-manager:
$(WAYLAND_SCANNER) -package inputmanager \
-o inputdevices1/internal/inputmanager/wayland-treeland-input-manager-unstable-v1-client-protocol.go \
"$(TREELAND_INPUT_MANAGER_XML)"

prepare:
@mkdir -p out/bin
@mkdir -p ${GOPATH_DIR}/src/$(dir ${GOPKG_PREFIX});
Expand Down
2 changes: 1 addition & 1 deletion debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Section: admin
Priority: optional
Maintainer: Deepin Packages Builder <packages@deepin.com>
Build-Depends:
dde-api-dev (>> 3.17.1+),
dde-api-dev (>= 6.2.2),
debhelper-compat (= 11),
deepin-desktop-schemas,
deepin-gettext-tools,
Expand Down
21 changes: 21 additions & 0 deletions debian/copyright
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,24 @@ License: GPL-3+
.
On Debian systems, the complete text of the GNU General
Public License version 3 can be found in "/usr/share/common-licenses/GPL-3".

Files: inputdevices1/internal/inputmanager/wayland-treeland-input-manager-unstable-v1-client-protocol.go
Copyright: 2026 UnionTech Software Technology Co., Ltd.
License: Expat
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
130 changes: 130 additions & 0 deletions inputdevices1/internal/inputmanager/layout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

package inputmanager

import (
"context"
"fmt"
"time"

"github.com/linuxdeepin/dde-api/wayland"
"github.com/linuxdeepin/dde-api/wayland/core"
)

// SetKeyboardLayout updates the default Treeland seat's XKB layout and variant,
// leaving its current model and options intact.
func SetKeyboardLayout(layout, variant string) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
d, err := core.Connect(ctx)
if err != nil {
return err
}
defer d.Close()
deadline, _ := ctx.Deadline()
if err := d.SetDeadline(deadline); err != nil {
return err
}
Register(d.Conn)

obj, err := d.GetRegistry()
if err != nil {
return err
}
registry := core.WlRegistry{Object: obj}
var managerName uint32
var seats []uint32
registry.OnGlobal(func(name uint32, iface string, version uint32) {
switch iface {
case core.WlSeatInterface.Name:
seats = append(seats, name)
case TreelandInputManagerV1Interface.Name:
if version >= 2 && managerName == 0 {
managerName = name
}
}
})
registry.OnGlobalRemove(func(name uint32) {
if name == managerName {
managerName = 0
}
for i, seat := range seats {
if name == seat {
seats = append(seats[:i], seats[i+1:]...)
break
}
}
})
if err := d.Roundtrip(); err != nil {
return err
}
if managerName == 0 {
return fmt.Errorf("Treeland input manager v2 is not available")
}
if len(seats) == 0 {
return fmt.Errorf("Treeland has no Wayland seat")
}
// Treeland requires the client to bind the default wl_seat before its manager.
for _, name := range seats {
if _, err := registry.Bind(name, core.WlSeatInterface.Name, 1); err != nil {
return err
}
}
obj, err = registry.Bind(managerName, TreelandInputManagerV1Interface.Name, 2)
if err != nil {
return err
}
manager := TreelandInputManagerV1{Object: obj}
var seat *wayland.Object
manager.OnCapabilityAvailable(func(types uint32, obj *wayland.Object) {
if types&TreelandInputManagerV1DeviceTypeKeyboard != 0 && seat == nil {
seat = obj
}
})
manager.OnCapabilityUnavailable(func(types uint32, obj *wayland.Object) {
if types&TreelandInputManagerV1DeviceTypeKeyboard != 0 && seat == obj {
seat = nil
}
})
if err := d.Roundtrip(); err != nil {
return err
}
if seat == nil {
return fmt.Errorf("Treeland seat does not advertise keyboard capability")
}
obj, err = manager.GetKeyboardSettings(seat)
if err != nil {
return err
}
keyboard := TreelandKeyboardSettingsV1{Object: obj}
var model, options string
var ready, gotRules, failed bool
keyboard.OnXkbRules(func(_, currentModel, _, currentOptions string) {
model, options, gotRules = currentModel, currentOptions, true
})
keyboard.OnDone(func() { ready = true })
keyboard.OnFailed(func() { failed = true })
for !ready && !failed && seat != nil {
if err := d.Dispatch(); err != nil {
return err
}
}
if !ready || !gotRules || failed || seat == nil {
return fmt.Errorf("Treeland keyboard settings are not ready")
}
if err := keyboard.SetXkbRules(&layout, &model, &variant, &options); err != nil {
return err
}
if err := keyboard.Apply(); err != nil {
return err
}
if err := d.Roundtrip(); err != nil {
return err
}
if failed || seat == nil {
return fmt.Errorf("Treeland did not apply the keyboard layout")
}
return nil
}
101 changes: 101 additions & 0 deletions inputdevices1/internal/inputmanager/layout_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

package inputmanager

import (
"bytes"
"encoding/binary"
"fmt"
"io"
"net"
"path/filepath"
"testing"
"time"
"unsafe"
)

func TestSetKeyboardLayout(t *testing.T) {
// Wayland uses the host byte order.
var word binary.ByteOrder = binary.LittleEndian
one := uint16(1)
if *(*byte)(unsafe.Pointer(&one)) == 0 {
word = binary.BigEndian
}
for _, rejected := range []bool{false, true} {
t.Run(fmt.Sprintf("rejected=%t", rejected), func(t *testing.T) {
path := filepath.Join(t.TempDir(), "socket")
listener, err := net.Listen("unix", path)
if err != nil {
t.Fatal(err)
}
defer listener.Close()
t.Setenv("WAYLAND_DISPLAY", path)
serverErr := make(chan error, 1)
go func() {
c, err := listener.Accept()
if err != nil {
serverErr <- err
return
}
defer c.Close()
_ = c.SetDeadline(time.Now().Add(2 * time.Second))
words := func(values ...uint32) []byte {
b := make([]byte, 4*len(values))
for i, v := range values {
word.PutUint32(b[4*i:], v)
}
return b
}
text := func(s string) []byte {
b := make([]byte, 4+(len(s)+4)&^3)
word.PutUint32(b, uint32(len(s)+1))
copy(b[4:], s)
return b
}
msg := func(id, op uint32, args []byte) []byte { return append(words(id, uint32(len(args)+8)<<16|op), args...) }
global := func(name uint32, iface string, version uint32) []byte {
return append(append(words(name), text(iface)...), words(version)...)
}
join := func(a, b, c, d string) []byte { return bytes.Join([][]byte{text(a), text(b), text(c), text(d)}, nil) }
var failed []byte
if rejected {
failed = msg(7, 3, nil)
}
for _, step := range []struct{ request, reply []byte }{
{msg(1, 1, words(2)), append(msg(2, 0, global(40, "treeland_input_manager_v1", 2)), msg(2, 0, global(41, "wl_seat", 8))...)},
{msg(1, 0, words(3)), msg(3, 0, words(0))},
{msg(2, 0, append(global(41, "wl_seat", 1), words(4)...)), msg(4, 0, words(2))},
{msg(2, 0, append(global(40, "treeland_input_manager_v1", 2), words(5)...)), msg(5, 0, words(4, 4))},
{msg(1, 0, words(6)), msg(6, 0, words(0))},
{msg(5, 3, words(7, 4)), append(msg(7, 5, join("us", "pc105", "", "grp:alt_shift_toggle")), msg(7, 4, nil)...)},
{msg(7, 4, join("cz,us", "pc105", "qwerty,", "grp:alt_shift_toggle")), nil},
{msg(7, 3, nil), failed},
{msg(1, 0, words(8)), msg(8, 0, words(0))},
} {
got := make([]byte, len(step.request))
if _, err := io.ReadFull(c, got); err != nil {
serverErr <- err
return
}
if !bytes.Equal(got, step.request) {
serverErr <- fmt.Errorf("request %x, want %x", got, step.request)
return
}
if _, err := c.Write(step.reply); err != nil {
serverErr <- err
return
}
}
serverErr <- nil
}()
if err := SetKeyboardLayout("cz,us", "qwerty,"); (err != nil) != rejected {
t.Errorf("SetKeyboardLayout: %v", err)
}
if err := <-serverErr; err != nil {
t.Fatal(err)
}
})
}
}
Loading
Loading