Detecting dis/connect woes
Posted: 04 Nov 2019, 13:17
Hello there. As you can infer from the title, I've been struggling to find some way to detect and handle connects and disconnects (not necessarily in that order) of keyboards or whatever other devices. With little success, I might add.
Now, let me first present my findings. At first, I was trying to "catch" some kind of error, assuming that you'd get some kind of error when trying to set a handler on a device that does not exist. I tried using the pcall and lua_pcall functions, but somehow, the results I got were not the results I expected. Then I started trying the return of the lmc_set_handler function itself, again to no avail.
After a lot of trial and error, I realised the one sad truth: the device list, the one seen under "Devices" using the lmc.statistics flag, never gets updated. Therefore, there is no way to "trap" events such as "disconnect" or "connect".
The other problem is that, once a handler is set, it never goes away, even if the corresponding keyboard is disconnected.
Another problem I found during my tests, where I used a timer function to spawn more handles on the same keyboard, is that you cannot programmatically "unset" a handler. I know there is little point in that, but there's no lmc_delete_handler function.
So, here we come to the "request" part.
First and foremost, I would really like to see something along the lines of "keyboard event traps". Like, new keyboard is connected? Notify me! Some keyboard is disconnected? Notify me again! I want a trigger!
Secondly, if the only way to do an update on the devices is an lmc_reset call, there should also be something like lmc_reset_and_restart function. Like, reset (as it does now) but after reset is finished, restart the script!
Third, if it's not possible to do the first, get messages of device connects and disconnects, at least a function like lmc_rescan_devices would be needed. I bet you can see where I'm going with this one: use a timer to rescan devices, see if the one I want is still there and if it's not, delete the handler. The timer should keep running and scan periodically so, if the device gets reconnected, re-set the handler!
Fourth, the set functions do not return errors. Specifically, the lmc_set_device_name function doesn't report or return any errors whatsoever, if the device does not exist. It's "business as usual", even if I do lmc_set_device_name('spock', 'Space._The_final_frontier.')
Thankfully, the lmc_set_handler function does *something* in case of a missing device, even if that "something" is not what would be expected. Meaning, it does not return any values as a function should. However, it goes on to print an error message on the output pane!
Now, I'm no programming language expert, but shouldn't all functions return something like "0" if it finished successfully and "1" if it failed for whatever reason? Or is it there and I'm not seeing it?
I'd really love to hear your thoughts on those observations. Oh and thank you for an amazing program! That goes without saying.
Now, let me first present my findings. At first, I was trying to "catch" some kind of error, assuming that you'd get some kind of error when trying to set a handler on a device that does not exist. I tried using the pcall and lua_pcall functions, but somehow, the results I got were not the results I expected. Then I started trying the return of the lmc_set_handler function itself, again to no avail.
After a lot of trial and error, I realised the one sad truth: the device list, the one seen under "Devices" using the lmc.statistics flag, never gets updated. Therefore, there is no way to "trap" events such as "disconnect" or "connect".
The other problem is that, once a handler is set, it never goes away, even if the corresponding keyboard is disconnected.
Another problem I found during my tests, where I used a timer function to spawn more handles on the same keyboard, is that you cannot programmatically "unset" a handler. I know there is little point in that, but there's no lmc_delete_handler function.
So, here we come to the "request" part.
First and foremost, I would really like to see something along the lines of "keyboard event traps". Like, new keyboard is connected? Notify me! Some keyboard is disconnected? Notify me again! I want a trigger!
Secondly, if the only way to do an update on the devices is an lmc_reset call, there should also be something like lmc_reset_and_restart function. Like, reset (as it does now) but after reset is finished, restart the script!
Third, if it's not possible to do the first, get messages of device connects and disconnects, at least a function like lmc_rescan_devices would be needed. I bet you can see where I'm going with this one: use a timer to rescan devices, see if the one I want is still there and if it's not, delete the handler. The timer should keep running and scan periodically so, if the device gets reconnected, re-set the handler!
Fourth, the set functions do not return errors. Specifically, the lmc_set_device_name function doesn't report or return any errors whatsoever, if the device does not exist. It's "business as usual", even if I do lmc_set_device_name('spock', 'Space._The_final_frontier.')
Thankfully, the lmc_set_handler function does *something* in case of a missing device, even if that "something" is not what would be expected. Meaning, it does not return any values as a function should. However, it goes on to print an error message on the output pane!
Now, I'm no programming language expert, but shouldn't all functions return something like "0" if it finished successfully and "1" if it failed for whatever reason? Or is it there and I'm not seeing it?
I'd really love to hear your thoughts on those observations. Oh and thank you for an amazing program! That goes without saying.