-
Notifications
You must be signed in to change notification settings - Fork 60
feat: implement keyboard device management #134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ static int is_mouse_device(int deviceid); | |
| static int is_touchpad_device(int deviceid); | ||
| static int is_touchscreen_device(int deviceid); | ||
| static int is_wacom_device(int deviceid); | ||
| static int is_keyboard_device(int deviceid); | ||
| static XIDeviceInfo* get_xdevice_by_id(int deviceid); | ||
|
|
||
| static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; | ||
|
|
@@ -64,6 +65,11 @@ query_device_type(int deviceid) | |
| return TYPE_MOUSE; | ||
| } | ||
|
|
||
| if (is_keyboard_device(deviceid)) { | ||
| return TYPE_KEYBOARD; | ||
| } | ||
|
|
||
|
|
||
| return TYPE_UNKNOWN; | ||
| } | ||
|
|
||
|
|
@@ -127,6 +133,49 @@ is_touchpad_device(int deviceid) | |
| is_property_exist(deviceid, "libinput Tapping Enabled")); | ||
| } | ||
|
|
||
| static int | ||
| is_keyboard_device(int deviceid) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (complexity): Consider replacing the is_keyboard_device() implementation with a call to get_xdevice_by_id() to simplify resource management and error handling. // Replace the current implementation of is_keyboard_device() with a call to
// get_xdevice_by_id(), which already handles the X11 connection, locking,
// error‐checking and cleanup for you.
static int
is_keyboard_device(int deviceid)
{
XIDeviceInfo *info = get_xdevice_by_id(deviceid);
if (!info)
return 0;
int is_kbd = (info->use == XISlaveKeyboard);
XIFreeDeviceInfo(info);
return is_kbd;
}By doing this you:
|
||
| { | ||
| Display *display; | ||
| int num_devices, i; | ||
|
|
||
| pthread_mutex_lock(&mutex); | ||
| // 打开 X11 显示 | ||
| display = XOpenDisplay(NULL); | ||
| if (display == NULL) { | ||
| fprintf(stderr, "Open display failed at check prop exist\n"); | ||
| pthread_mutex_unlock(&mutex); | ||
| return 0; | ||
| } | ||
|
|
||
| // 获取所有输入设备 | ||
| XIDeviceInfo *devices = XIQueryDevice(display, deviceid, &num_devices); | ||
| if (devices == NULL || num_devices != 1) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (bug_risk): Free Call |
||
| fprintf(stderr, "Error getting device information.\n"); | ||
| pthread_mutex_unlock(&mutex); | ||
| XCloseDisplay(display); | ||
| return 0; | ||
| } | ||
|
|
||
| if(devices[0].use != XISlaveKeyboard) | ||
| { | ||
| fprintf(stderr, "Device is not keyboard.\n"); | ||
| pthread_mutex_unlock(&mutex); | ||
| XIFreeDeviceInfo(devices); | ||
| XCloseDisplay(display); | ||
| return 0; | ||
| } | ||
|
|
||
| // 释放设备信息内存 | ||
| XIFreeDeviceInfo(devices); | ||
|
|
||
| // 关闭 X11 显示 | ||
| XCloseDisplay(display); | ||
| pthread_mutex_unlock(&mutex); | ||
|
|
||
| return 1; | ||
| } | ||
|
|
||
| // TODO: support libinput | ||
| static int | ||
| is_wacom_device(int deviceid) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Use 'ID' instead of 'Id' for Go naming consistency
Consider renaming the
Idfield toIDand updating all references for consistency with Go conventions.Suggested implementation:
If there are any other references to the
Idfield ofKeyboardelsewhere in this file (or in other files), they should also be updated to useIDfor consistency.