@@ -48,66 +48,56 @@ const char* ffDetectKeyboard(FFlist* devices /* List of FFKeyboardDevice */)
4848
4949 uint64_t flags = 0 ;
5050 FF_STRBUF_AUTO_DESTROY path = ffStrbufCreate ();
51+ FFstrbuf kbd = ffStrbufCreateStatic ("kbd" );
5152
52- const char * line = content .chars ;
53- while (line && * line )
53+ char * line = NULL ;
54+ size_t len = 0 ;
55+ while (ffStrbufGetline (& line , & len , & content ))
5456 {
55- if (ffStrStartsWith (line , "H: Handlers=" ))
57+ if (!ffStrStartsWith (line , "H: Handlers=" ))
58+ continue ;
59+
60+ const char * handlers = line + strlen ("H: Handlers=" );
61+
62+ if (!ffStrbufMatchSeparatedS (& kbd , handlers , ' ' ))
63+ continue ;
64+
65+ // Find "eventN" token and extract the index
66+ const char * eventStr = strstr (handlers , "event" );
67+ if (!eventStr )
68+ continue ;
69+
70+ char * pend = NULL ;
71+ uint32_t eventIndex = (uint32_t ) strtoul (eventStr + strlen ("event" ), & pend , 10 );
72+ if (pend == eventStr + strlen ("event" ))
73+ continue ;
74+
75+ // Skip duplicates (dedup bitmap covers indices 0-63; higher indices are not deduped)
76+ if (eventIndex < 64 && (flags & (1ULL << eventIndex )))
77+ continue ;
78+
79+ if (!isRealKeyboard (eventIndex , & path ))
80+ continue ;
81+
82+ ffStrbufSetF (& path , "/sys/class/input/event%u/device/name" , (unsigned ) eventIndex );
83+
84+ FF_STRBUF_AUTO_DESTROY name = ffStrbufCreate ();
85+ if (ffAppendFileBuffer (path .chars , & name ))
5686 {
57- const char * handlers = line + strlen ("H: Handlers=" );
58- bool hasKbd = false;
59- uint32_t eventIndex = UINT32_MAX ;
60-
61- // Parse space-separated handler names
62- const char * p = handlers ;
63- while (* p && * p != '\n' )
64- {
65- while (* p == ' ' ) p ++ ;
66- if (* p == '\n' || * p == '\0' ) break ;
67-
68- const char * wordStart = p ;
69- while (* p && * p != ' ' && * p != '\n' ) p ++ ;
70- uint32_t wordLen = (uint32_t )(p - wordStart );
71-
72- if (wordLen == 3 && memcmp (wordStart , "kbd" , 3 ) == 0 )
73- hasKbd = true;
74- else if (wordLen > strlen ("event" ) && memcmp (wordStart , "event" , strlen ("event" )) == 0 )
75- {
76- char * pend = NULL ;
77- eventIndex = (uint32_t ) strtoul (wordStart + strlen ("event" ), & pend , 10 );
78- if (pend == wordStart + strlen ("event" )) eventIndex = UINT32_MAX ;
79- }
80- }
81-
82- // Skip duplicates (dedup bitmap covers indices 0-63; higher indices are not deduped)
83- bool seen = eventIndex < 64 && (flags & (1ULL << eventIndex ));
84-
85- if (hasKbd && eventIndex != UINT32_MAX && !seen && isRealKeyboard (eventIndex , & path ))
86- {
87- ffStrbufSetF (& path , "/sys/class/input/event%u/device/name" , (unsigned ) eventIndex );
88-
89- FF_STRBUF_AUTO_DESTROY name = ffStrbufCreate ();
90- if (ffAppendFileBuffer (path .chars , & name ))
91- {
92- if (eventIndex < 64 )
93- flags |= (1ULL << eventIndex );
94-
95- ffStrbufTrimRightSpace (& name );
96- ffStrbufSubstrBefore (& path , path .length - (uint32_t ) strlen ("name" ));
97-
98- FFKeyboardDevice * device = (FFKeyboardDevice * ) ffListAdd (devices );
99- ffStrbufInitMove (& device -> name , & name );
100- ffStrbufInit (& device -> serial );
101-
102- ffStrbufAppendS (& path , "uniq" );
103- if (ffAppendFileBuffer (path .chars , & device -> serial ))
104- ffStrbufTrimRightSpace (& device -> serial );
105- }
106- }
107- }
87+ if (eventIndex < 64 )
88+ flags |= (1ULL << eventIndex );
10889
109- const char * next = strchr (line , '\n' );
110- line = next ? next + 1 : NULL ;
90+ ffStrbufTrimRightSpace (& name );
91+ ffStrbufSubstrBefore (& path , path .length - (uint32_t ) strlen ("name" ));
92+
93+ FFKeyboardDevice * device = (FFKeyboardDevice * ) ffListAdd (devices );
94+ ffStrbufInitMove (& device -> name , & name );
95+ ffStrbufInit (& device -> serial );
96+
97+ ffStrbufAppendS (& path , "uniq" );
98+ if (ffAppendFileBuffer (path .chars , & device -> serial ))
99+ ffStrbufTrimRightSpace (& device -> serial );
100+ }
111101 }
112102
113103 return NULL ;
0 commit comments