community
community copied to clipboard
Plug and play support for input devices
Hi
Version
- Python: 2.7
- OS: Raspbian Strech 9
- Kivy: 1.10
- Kivy installation method: pip
Description
I have a KIVY app which requires barcode scanners, NFC card scanners, keyboards, mouse to be disconnected and connected back while the app is running. However, kivy initialises all input devices during startup and does not seem to support plug and play for input devices. Is this a limitation of Kivy or is there a workaround available for this?
This is a limitation, maybe somebody did a workaround for that, but our code doesn't support it. You could be able to rescan somehow the devices and see if there is a new one.
That can probably be done by monitoring the devices using 'udevadm monitor' and removing and adding then from/to EventLoop as needed.
There's another related issue that i am facing. Reconnecting the same device back throws the below exception:
##when disconnected
Exception in thread Thread-10:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
[INFO ] [MTD ] </dev/input/event2> input device disconnected
File "/usr/lib/python2.7/threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File "/usr/local/lib/python2.7/dist-packages/kivy/input/providers/hidinput.py", line 686, in _thread_run
data = fd.read(struct_input_event_sz)
IOError: [Errno 19] No such device
##when connected back
Exception in thread Thread-5:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File "/usr/local/lib/python2.7/dist-packages/kivy/input/providers/mtdev.py", line 290, in _thread_run
_device = Device(_fn)
File "/usr/local/lib/python2.7/dist-packages/kivy/lib/mtdev.py", line 143, in __init__
self._fd = os.open(filename, os.O_NONBLOCK | os.O_RDONLY)
OSError: [Errno 13] Permission denied: '/dev/input/event2'
Could be similar to Issue #6141
in our case (Kivy-1.11.1
, with provider=mtdev
), this patch for kivy/input/providers/mtdev.py
allows the reconnection:
291c291,300
< _device = Device(_fn)
---
>
> while True:
> try:
> _device = Device(_fn)
> except (PermissionError, FileNotFoundError) as err:
> Logger.info('MTD: <%s> %s' % (_fn, err))
> time.sleep(0.5)
> else:
> break
>
Of course, a more robust solution would be to re-scan because the device might connect in another node... :shrug:
@pwoolvett Do you mean row 232 or row 290 in mtdev.py to change _device = Device(_fn) to your code?
The first one (232, first initialization): leave it as it is. The second one (290, reconnection attempt) is the one you should patch...
Perfect, thank you. It is also working for kivy 2.0.0dev0 with python 3.5.3 on raspbian 9 (Stretch)
I'm also having this error with this simple "hello world" example and Kivy 2.1.0, Python 3.10.4 and Tumbleweed 20220303.
kivyhelloworld.py:
import kivy
from kivy.app import App
from kivy.uix.label import Label
# Replace this with your
# current version
kivy.require('1.11.1')
# Defining a class
class MyFirstKivyApp(App):
# Function that returns
# the root widget
def build(self):
# Label with text Hello World is
# returned as root widget
return Label(text ="Hello World !")
# Here our class is initialized
# and its run() method is called.
# This initializes and starts
# our Kivy application.
MyFirstKivyApp().run()
```
The above produces this error:
python3.10 kivyhelloworld.py
[INFO ] [Logger ] Record log in /home/myuser/.kivy/logs/kivy_22-04-09_16.txt
[INFO ] [Kivy ] v2.1.0
[INFO ] [Kivy ] Installed at "/usr/lib64/python3.10/site-packages/kivy/__init__.py"
[INFO ] [Python ] v3.10.4 (main, Mar 26 2022, 21:00:27) [GCC]
[INFO ] [Python ] Interpreter at "/usr/bin/python3.10"
[INFO ] [Logger ] Purge log fired. Processing...
[INFO ] [Logger ] Purge finished!
[INFO ] [Factory ] 189 symbols loaded
[INFO ] [Image ] Providers: img_tex, img_dds, img_sdl2, img_pil (img_ffpyplayer ignored)
[INFO ] [Text ] Provider: sdl2(['text_pango'] ignored)
[INFO ] [Window ] Provider: sdl2
[INFO ] [GL ] Using the "OpenGL" graphics system
[INFO ] [GL ] Backend used <sdl2>
[INFO ] [GL ] OpenGL version <b'3.0 Mesa 21.3.7'>
[INFO ] [GL ] OpenGL vendor <b'Intel Open Source Technology Center'>
[INFO ] [GL ] OpenGL renderer <b'Mesa DRI Intel(R) HD Graphics 4000 (IVB GT2)'>
[INFO ] [GL ] OpenGL parsed version: 3, 0
[INFO ] [GL ] Shading version <b'1.30'>
[INFO ] [GL ] Texture max size <16384>
[INFO ] [GL ] Texture max units <16>
[INFO ] [Window ] auto add sdl2 input provider
[INFO ] [Window ] virtual keyboard not allowed, single mode, not docked
[INFO ] [ProbeSysfs ] device match: /dev/input/event2
[INFO ] [MTD ] Read event from </dev/input/event2>
[INFO ] [Base ] Start application main loop
[INFO ] [GL ] NPOT texture support is available
[WARNING] [MTD ] Unable to open device "/dev/input/event2". Please ensure you have the appropriate permissions.
The patch given above does work for changing the nature of this error, but it doesn't solve the error itself.