Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port to pygame_sdl2? #25

Open
cragginstylie opened this issue Jan 28, 2022 · 13 comments
Open

Port to pygame_sdl2? #25

cragginstylie opened this issue Jan 28, 2022 · 13 comments

Comments

@cragginstylie
Copy link

cragginstylie commented Jan 28, 2022

Hi!

I found your project and think it would be perfect to include as part of mine. Here's the project hardware/software I'm using:

  • Raspberry Pi 4
  • 5" HDMI w/capacitive touch interface
  • Raspbian Bullseye 64bit
  • Python 3.9.2
  • Pygame 2.0 compiled w/SDL2

I'm forced to use the python3-pygame-sdl2 package because the default pygame 1.9.6 package does not support the kernel mode video drivers on an RPi 4, consistently throwing errors about unable to initialize the display or unable to open a console. The sdl2 package works with my drivers as expected.

However, I tried to get your module to work, but it throws various errors because of the (I assume significant) differences between pygame 2.x compiled with SDL2 support. Here's what I tried so far:


include pygame_sdl2 as pygame
from pygame_vkeyboard import *

--- do typical initialization ---

Which produces the following error:

. . .
  File "/home/pi/TGR-A8D4-TPL-R1/ui/objs.py", line 48, in <module>
    keyboard = VKeyboard(tft, consumer, layout)
  File "/home/pi/.local/lib/python3.9/site-packages/pygame_vkeyboard/vkeyboard.py", line 533, in __init__
    self.input = VTextInput((0, 0), (10, 10), renderer=self.renderer)
  File "/home/pi/.local/lib/python3.9/site-packages/pygame_vkeyboard/vtextinput.py", line 303, in __init__
    line = VLine((self.size[0] - 2 * self.text_margin,
  File "/home/pi/.local/lib/python3.9/site-packages/pygame_vkeyboard/vtextinput.py", line 184, in __init__
    self.renderer.draw_text(self.image, '')
  File "/home/pi/.local/lib/python3.9/site-packages/pygame_vkeyboard/vrenderers.py", line 284, in draw_text
    self.font_input = fit_font(self.font_name, surface.get_height())
  File "/home/pi/.local/lib/python3.9/site-packages/pygame_vkeyboard/vrenderers.py", line 28, in fit_font
    font = pygame.font.Font(font_name, 1)
pygame.error: font not initialized

I've seen this error before with other pygame modules (pygame_gui, for example). I then edited the .py files within your module package to reference pygame_sdl2 instead of pygame. This produces the following error:

. . .
  File "/home/pi/TGR-A8D4-TPL-R1/ui/objs.py", line 48, in <module>
    keyboard = VKeyboard(tft, consumer, layout)
  File "/home/pi/.local/lib/python3.9/site-packages/pygame_vkeyboard/vkeyboard.py", line 533, in __init__
    self.input = VTextInput((0, 0), (10, 10), renderer=self.renderer)
  File "/home/pi/.local/lib/python3.9/site-packages/pygame_vkeyboard/vtextinput.py", line 314, in __init__
    self.set_line_rect(*self.position + self.size)
  File "/home/pi/.local/lib/python3.9/site-packages/pygame_vkeyboard/vtextinput.py", line 393, in set_line_rect
    if self.sprites.get_clip() != clip_rect:
  File "src/pygame_sdl2/rect.pyx", line 82, in pygame_sdl2.rect.Rect.__richcmp__
  File "src/pygame_sdl2/rect.pyx", line 45, in pygame_sdl2.rect.Rect.__init__
TypeError: object of type 'NoneType' has no len()

At this point, it would be diving too far down the rabbit hole to get it working only to find out it still doesn't fit my needs. I believe it will, but truthfully would need assistance trying to port this over.

Not sure this is something you're even willing & able to tackle, but thought it won't hurt to ask. Totally understandable if not.

@anxuae
Copy link
Collaborator

anxuae commented Jan 28, 2022

Hi,

the first error you got pygame.error: font not initialized is telling that you didn't initialized pygame.

Check in your /home/pi/TGR-A8D4-TPL-R1/ui/objs.py module that you called pygame.init() before creating the keyboard.

Try also pygame.font.init() if still not working

@cragginstylie
Copy link
Author

--- do typical initialization ---

In my op, means I called pygame.init(). Like I said, I've seen this before from other pygame related modules, and it's caused by a mismatch of initialized data structures. I'm including pygame_sdl2, along with pygame_sdl2.init(), which doesn't jive with a module that's including pygame and expecting pygame.init() to have been executed. You can try calling pygame.font.init() till the cows come home, and it will never work due to the mismatch between the actual pygame module being referenced.

@anxuae
Copy link
Collaborator

anxuae commented Jan 28, 2022

Ok, I see. But the lib is not forseen for pygame_sdl2 (I didn't paid attention that it is a fork of pygame for SDL2)

If you need SDL2, you could install pygame>2.0 which is fully compatible with SDL2

@cragginstylie
Copy link
Author

If you need SDL2, you could install pygame>2.0 which is fully compatible with SDL2

I tried that, but apt forces an upgrade of the default installed pygame 1.9.6 package, which doesn't have compatibility with the kmsd video driver needed to support the HDMI touch screen device. The only pygame package that works on this system is the pygame_sdl2. FWIW - this would work fine on an RPi 3B+, which uses the standard fbdev or fbturbo drivers. I migrated to RPi 4B to get more CPU & memory, which is a consequence of adding more & more capabilities to my project. /sigh

@anxuae
Copy link
Collaborator

anxuae commented Jan 28, 2022

I'm able to install it on Pi OS Buster using the commands:

sudo apt-get install libsdl2-*
sudo pip3 install pygame>=2.0

@anxuae
Copy link
Collaborator

anxuae commented Jan 28, 2022

An other way could be tweak python import system. Something like that (I didn't test):

import pygame_sdl2
import sys
sys.modules['pygame']=pygame_sdl2
from pygame_vkeyboard import *

@cragginstylie
Copy link
Author

I'm using Raspbian Bullseye 64bit, and it's not the package installation having issues. It's the lack of kmsd driver support compiled in, which causes pygame.display.init() to throw errors about inability to find a driver.

@anxuae
Copy link
Collaborator

anxuae commented Jan 28, 2022

Understood. I know that Bullseye it's still young and buggy. That why I didn't switched on yet.

@cragginstylie
Copy link
Author

An other way could be tweak python import system. Something like that (I didn't test):

import pygame_sdl2
import sys
sys.modules['pygame']=pygame_sdl2
from pygame_vkeyboard import *

Wouldn't that be similar to editing the vkeyboard .py files as I mentioned in my op? Willing to give it a go, but I'm skeptical...

@cragginstylie
Copy link
Author

Understood. I know that Bullseye it's still young and buggy. That why I didn't switched on yet.

Actually, on an RPi 4, it runs with fewer issues. At least so far.

@anxuae
Copy link
Collaborator

anxuae commented Jan 28, 2022

Yes it will be similar Thant editing files.
The error you point out is probably du to the fact that pygame2 and pygame_sdl2 do not work exactly on the same way.

@anxuae
Copy link
Collaborator

anxuae commented Jan 28, 2022

I think that the main reason is that pygame_sdl2 is compiled using cython. It require to respect some coding rules in order to be compliant.

For instance, the error that you are pointing out is about the comparison:

if self.sprites.get_clip() != clip_rect:

self.sprites.get_clip() return None and clip_rect is a Rect object:

  • In python this comparison is completely acceptable.
  • However it seem that in cython is not acceptable because you can not determine the length of NoneType (I'm only making supposition)

So if you replace the comparison by (which is also completely acceptable in python but more verbose):

if self.sprites.get_clip() is None or  self.sprites.get_clip() != clip_rect:

The error could gone. But probably to go to the next one until all part of the code is made compatible.
It is a fastidious work. But if you want, you can propose a PR.

@cragginstylie
Copy link
Author

It is a fastidious work. But if you want, you can propose a PR.

Haha! Agreed. ATM, I'm going to proceed with my UI design to not include any capabilities requiring keyboard input. I've built my IoT device to support a touchscreen for physical proximity, and a web console for remote access. Functions requiring a keyboard (like naming devices, timers, or presets) will have to remain relegated to the web console (for now).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants