drhelius / Gearsystem

Sega Master System / Game Gear / SG-1000 emulator for macOS, Windows, Linux, BSD and RetroArch.
https://x.com/drhelius
GNU General Public License v3.0
247 stars 45 forks source link

Create Flatpak App for Gearsystem #93

Closed TomChapple closed 2 weeks ago

TomChapple commented 4 months ago

This emulator has been really awesome, especially on Linux, but I'm curious if distributing it as a Flatpak application can make it a bit more accessible. I have been using a (what appears to be) unofficial snap release, but it does have a nasty habit of pestering me to check audio permissions on startup. Setting up an official Flatpak release may not only resolve such odd permission requests but may allow for exploration of other releases of this kind is there is a different preference.

The main advantages I see of having a Flatpak release are:

I must admit, I am relatively new to the Flatpak development environment, so I'm not aware if there is any major constraint which would make such a release not worthwhile. For example, it would be interesting to see how the SDL gamecontrollers work inside a sandbox. At the very least though, I got a manifest together locally which starts Gearsystem but is unable to initialise Glew.

Is there an interest to support such a release for this project?

drhelius commented 4 months ago

Thanks, it would be great for sure but, honestly, I don't have the time nor any experience with Flatpak.

I will leave this open though, just in case someone want to contribute it.

TomChapple commented 4 months ago

That's understandable.

I have been doing some experimentation into packaging a Flatpak application for this and managed to get it packaged, but it is receiving errors from both GTK3 and GLEW during initialisation. I'm struggling to interpret the exact cause, so I'm putting it up here in case someone recognises it.

GTK is of no help as gtk_init_check doesn't return any error codes, but GLEW does return an error code (despite reporting an "Unknown Error"): GLEW_ERROR_NO_GLX_DISPLAY = 4. This is returned if glXGetCurrentDisplay() returns NULL, which implies that there is no display for the current context. I'm not sure if this is a side-effect of Flatpak's sandboxing or not, so this will require some additional investigation.

For context, this is my current Flatpak configuration (assuming a working directory of platforms/flatpak):

id: io.github.drhelius.Gearsystem
runtime: org.freedesktop.Platform
runtime-version: '23.08'
sdk: org.freedesktop.Sdk
command: gearsystem
build-options:
  env:
    DEBUG: "1"
sdk-extensions:
  - org.freedesktop.Platform.GL
modules:
  - shared-modules/glu/glu-9.json
  - shared-modules/glew/glew.json
  - name: gearsystem
    buildsystem: simple
    build-commands:
      - make DEBUG=1
      - make install prefix="$FLATPAK_DEST"
    subdir: platforms/linux
    sources:
      - type: dir
        path: ../..

I tend to build it with flatpak run org.flatpak.Builder --force-clean --user --install build-dir io.github.drhelius.Gearsystem.yml and run it with flatpak run io.github.drhelius.Gearsystem.

TomChapple commented 4 months ago

Awesome, using org.shadered.SHADERed.yaml as a basis (another program that uses GLEW), it looks as though I was missing finish-args that specify the appropriate permissions to X11 and the OpenGL device. Now the Window loads perfectly, but I cannot choose a file to use. Nevertheless, progress!

The following keys were added to the YAML I specified before:

finish-args:
  - --share=ipc
  - --socket=x11
  - --device=dri

More could be added as needed, but I will add those as I need them rather than being too lenient. Some guidelines are specified on Flatpak's documentation.

TomChapple commented 4 months ago

Okay, my latest progress is that when loading in a ROM file, the executable will perpetually wait at SDL_SemWait( free_sem ); inside Sound_Queue.cpp, seemingly because the lock is never released. This happens even if I disable sound, interestingly.

Has such an issue popped up previously in the development of this emulator?

drhelius commented 4 months ago

Not really. But looking at the list of permissions you may be missing one ore two, like --socket=pulseaudio. Just guessing...

https://docs.flatpak.org/en/latest/sandbox-permissions-reference.html

TomChapple commented 4 months ago

That was my first thought as well, but I get the same issue with it specified. Thanks for getting back to me though! I'll see what else is consuming the lock what whether that's held up by something.

TomChapple commented 4 months ago

So from investigation, the fillbuffer callback was never being called. I couldn't find a place to retrieve the error, but I did come across the SDL2 Shared Module and included that module as well. With that (and some temporary permission granting for the file system), I'm able to get a ROM loaded and running.

image

With that, I'll get a draft pull request ready, but it's obviously missing some of the finer details. For one, wide filesystem access set is likely not required, so I'll explore how to use Flatpak's libraries to allow access to specific files from a file choice dialog as well as gaining access to associated SAV files. Additionally, I will need to see whether controllers work as intended and what is needed to gain arbitrary access to them.