Skip to content

Commit 9e513c8

Browse files
committed
feat: implement keyboard device management
- Added Keyboard struct and associated methods for creating and managing keyboard devices. - Introduced error handling for device creation and validation. - Implemented functions to enable/disable keyboards and check their status. - Added utility function to identify keyboard devices in the X11 environment. Log: This enhancement improves keyboard device handling within the dxinput package. pms: BUG-315763
1 parent 5e9370c commit 9e513c8

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

dxinput/keyboard.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,60 @@
55
package dxinput
66

77
import (
8+
"errors"
9+
"fmt"
10+
11+
. "github.com/linuxdeepin/dde-api/dxinput/common"
12+
"github.com/linuxdeepin/dde-api/dxinput/kwayland"
813
"github.com/linuxdeepin/dde-api/dxinput/utils"
914
)
1015

1116
func SetKeyboardRepeat(enabled bool, delay, interval uint32) error {
1217
return utils.SetKeyboardRepeat(enabled, delay, interval)
1318
}
19+
20+
type Keyboard struct {
21+
Id int32
22+
Name string
23+
}
24+
25+
func NewKeyboard(id int32) (*Keyboard, error) {
26+
infos := utils.ListDevice()
27+
if infos == nil {
28+
return nil, errors.New("no device")
29+
}
30+
31+
info := infos.Get(id)
32+
33+
if info == nil {
34+
return nil, fmt.Errorf("invalid device id: %v", id)
35+
}
36+
return NewKeyboardDevInfo(info)
37+
}
38+
39+
func NewKeyboardDevInfo(dev *DeviceInfo) (*Keyboard, error) {
40+
if dev == nil || dev.Type != DevTypeKeyboard {
41+
return nil, fmt.Errorf("not a keyboard device(%d - %s)", dev.Id, dev.Name)
42+
}
43+
44+
return &Keyboard{
45+
Id: dev.Id,
46+
Name: dev.Name,
47+
}, nil
48+
}
49+
50+
func (m *Keyboard) Enable(enabled bool) error {
51+
if globalWayland {
52+
return kwayland.Enable(fmt.Sprintf("%s%d", kwayland.SysNamePrefix, m.Id), enabled)
53+
}
54+
55+
return enableDevice(m.Id, enabled)
56+
}
57+
58+
func (m *Keyboard) IsEnabled() bool {
59+
if globalWayland {
60+
return kwayland.CanEnabled(fmt.Sprintf("%s%d", kwayland.SysNamePrefix, m.Id))
61+
}
62+
63+
return isDeviceEnabled(m.Id)
64+
}

dxinput/utils/type.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ static int is_mouse_device(int deviceid);
1616
static int is_touchpad_device(int deviceid);
1717
static int is_touchscreen_device(int deviceid);
1818
static int is_wacom_device(int deviceid);
19+
static int is_keyboard_device(int deviceid);
1920
static XIDeviceInfo* get_xdevice_by_id(int deviceid);
2021

2122
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
@@ -64,6 +65,11 @@ query_device_type(int deviceid)
6465
return TYPE_MOUSE;
6566
}
6667

68+
if (is_keyboard_device(deviceid)) {
69+
return TYPE_KEYBOARD;
70+
}
71+
72+
6773
return TYPE_UNKNOWN;
6874
}
6975

@@ -127,6 +133,49 @@ is_touchpad_device(int deviceid)
127133
is_property_exist(deviceid, "libinput Tapping Enabled"));
128134
}
129135

136+
static int
137+
is_keyboard_device(int deviceid)
138+
{
139+
Display *display;
140+
int num_devices, i;
141+
142+
pthread_mutex_lock(&mutex);
143+
// 打开 X11 显示
144+
display = XOpenDisplay(NULL);
145+
if (display == NULL) {
146+
fprintf(stderr, "Open display failed at check prop exist\n");
147+
pthread_mutex_unlock(&mutex);
148+
return 0;
149+
}
150+
151+
// 获取所有输入设备
152+
XIDeviceInfo *devices = XIQueryDevice(display, deviceid, &num_devices);
153+
if (devices == NULL || num_devices != 1) {
154+
fprintf(stderr, "Error getting device information.\n");
155+
pthread_mutex_unlock(&mutex);
156+
XCloseDisplay(display);
157+
return 0;
158+
}
159+
160+
if(devices[0].use != XISlaveKeyboard)
161+
{
162+
fprintf(stderr, "Device is not keyboard.\n");
163+
pthread_mutex_unlock(&mutex);
164+
XIFreeDeviceInfo(devices);
165+
XCloseDisplay(display);
166+
return 0;
167+
}
168+
169+
// 释放设备信息内存
170+
XIFreeDeviceInfo(devices);
171+
172+
// 关闭 X11 显示
173+
XCloseDisplay(display);
174+
pthread_mutex_unlock(&mutex);
175+
176+
return 1;
177+
}
178+
130179
// TODO: support libinput
131180
static int
132181
is_wacom_device(int deviceid)

0 commit comments

Comments
 (0)