44#include <stdio.h>
55#include <fcntl.h>
66#include <usbhid.h>
7+ #include <sys/kbio.h>
78
89#if __has_include (< dev /usb /usb_ioctl .h > )
910 #include <dev/usb/usb_ioctl.h> // FreeBSD
1011#else
1112 #include <bus/u4b/usb_ioctl.h> // DragonFly
1213#endif
1314
14- #define MAX_UHID_JOYS 64
15+ static const char * detectByIoctl (FFlist * devices )
16+ {
17+ keyboard_info_t kbdInfo ;
18+ if (ioctl (STDIN_FILENO , KDGKBINFO , & kbdInfo ) != 0 )
19+ return "ioctl(KDGKBINFO) failed" ;
1520
16- const char * ffDetectKeyboard (FFlist * devices /* List of FFKeyboardDevice */ )
21+ FFKeyboardDevice * device = (FFKeyboardDevice * ) ffListAdd (devices );
22+
23+ switch (kbdInfo .kb_type ) {
24+ case KB_84 :
25+ ffStrbufInitS (& device -> name , "AT 84-key keyboard" );
26+ break ;
27+ case KB_101 :
28+ ffStrbufInitS (& device -> name , "AT 101/102-key keyboard" );
29+ break ;
30+ default :
31+ ffStrbufInitS (& device -> name , "Unknown keyboard" );
32+ break ;
33+ }
34+
35+ ffStrbufAppendF (& device -> name , " (kbd%d)" , kbdInfo .kb_index );
36+
37+ ffStrbufInit (& device -> serial );
38+ return NULL ;
39+ }
40+
41+ #define MAX_UHID_KBDS 64
42+
43+ static const char * detectByUsbhid (FFlist * devices )
1744{
1845 char path [16 ];
19- for (int i = 0 ; i < MAX_UHID_JOYS ; i ++ )
46+ for (int i = 0 ; i < MAX_UHID_KBDS ; i ++ )
2047 {
2148 snprintf (path , ARRAY_SIZE (path ), "/dev/uhid%d" , i );
2249 FF_AUTO_CLOSE_FD int fd = open (path , O_RDONLY | O_CLOEXEC );
@@ -43,10 +70,19 @@ const char* ffDetectKeyboard(FFlist* devices /* List of FFKeyboardDevice */)
4370 ffStrbufInitS (& device -> name , di .udi_product );
4471 }
4572 }
73+ hid_end_parse (hData );
4674 }
4775
4876 hid_dispose_report_desc (repDesc );
4977 }
5078
5179 return NULL ;
5280}
81+
82+ const char * ffDetectKeyboard (FFlist * devices /* List of FFKeyboardDevice */ )
83+ {
84+ detectByUsbhid (devices );
85+ if (devices -> length > 0 )
86+ return NULL ;
87+ return detectByIoctl (devices );
88+ }
0 commit comments