@@ -94,11 +94,13 @@ static inline const char* toString(bool value) {
9494
9595EventHub::device_t::device_t (int32_t _id, const char * _path, const char * name)
9696 : id(_id), path(_path), name(name), classes(0 )
97- , keyBitmask(NULL ), layoutMap(new KeyLayoutMap()), fd(-1 ), next(NULL ) {
97+ , keyBitmask(NULL ), switchBitmask(NULL )
98+ , layoutMap(new KeyLayoutMap()), fd(-1 ), next(NULL ) {
9899}
99100
100101EventHub::device_t::~device_t () {
101102 delete [] keyBitmask;
103+ delete [] switchBitmask;
102104 delete layoutMap;
103105}
104106
@@ -243,11 +245,14 @@ int32_t EventHub::getSwitchState(int32_t deviceId, int32_t sw) const {
243245}
244246
245247int32_t EventHub::getSwitchStateLocked (device_t * device, int32_t sw) const {
246- uint8_t sw_bitmask[sizeof_bit_array (SW_MAX + 1 )];
247- memset (sw_bitmask, 0 , sizeof (sw_bitmask));
248- if (ioctl (device->fd ,
249- EVIOCGSW (sizeof (sw_bitmask)), sw_bitmask) >= 0 ) {
250- return test_bit (sw, sw_bitmask) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
248+ if (device->switchBitmask != NULL
249+ && test_bit (sw, device->switchBitmask )) {
250+ uint8_t sw_bitmask[sizeof_bit_array (SW_MAX + 1 )];
251+ memset (sw_bitmask, 0 , sizeof (sw_bitmask));
252+ if (ioctl (device->fd ,
253+ EVIOCGSW (sizeof (sw_bitmask)), sw_bitmask) >= 0 ) {
254+ return test_bit (sw, sw_bitmask) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
255+ }
251256 }
252257 return AKEY_STATE_UNKNOWN;
253258}
@@ -759,6 +764,14 @@ int EventHub::openDevice(const char *deviceName) {
759764 }
760765 if (hasSwitches) {
761766 device->classes |= INPUT_DEVICE_CLASS_SWITCH;
767+ device->switchBitmask = new uint8_t [sizeof (sw_bitmask)];
768+ if (device->switchBitmask != NULL ) {
769+ memcpy (device->switchBitmask , sw_bitmask, sizeof (sw_bitmask));
770+ } else {
771+ delete device;
772+ LOGE (" out of memory allocating switch bitmask" );
773+ return -1 ;
774+ }
762775 }
763776#endif
764777
0 commit comments