Sunday, January 24, 2021

Making Wine and Box86 work under RetroPie [RetroPie as an OS]

Read this first

This guide is intented to be used with RetroPie as a starting OS. It's not usable for RetroPie inside TwisterOS.

I am a fan of "less is more" and this is why I like RetroPie. No X-Window like for example on LXDE.

This is one of the reasons why I wanted to port my Box86 installation to RetroPie: less X-Window (although you will still need xserver and xinit), more performance.

Is it possible?

Yes but it is quite challenging and it brings its lot of surprises (mostly bad surprises) but it eventually gives a decent result.
 

What is to be expected?

 

Wine and xinit 
 
First of all, Wine is meant to be run inside xinit or startx meaning that you will never run Wine from console mode (no, wineconsole cannot run windows executables, only commands), want it or not. You will however have the choice to start Wine in a minimalist xinit/startx.

Xinit glitches
 
Second, xinit sometimes fail to start the first time because xserver is somewhat not ready. It means that for production purpose, you will have to start your rom mainly with startx which is more reliable.

Legacy driver
 
This is the biggest problem of this topic: legacy driver. It has two problems:
  • You will only be able to change resolution or stretch to full screen thanks to the command tvservice. Example:
tvservice -e "DMT 35"
sleep 1
fbset -g 1280 1024 1280 1024 32
fbset -depth 8
fbset -depth 32
WINEPREFIX=... startx /your/game/binary --with-settings
tvservice -e "DMT 58"
sleep 1
fbset -g 1680 1050 1680 1050 32
fbset -depth 8
fbset -depth 32

You will have to set hdmi_group=2 in /boot/config.txt and reboot to get tvservice to work.
    • Otherwise you can run the game in 1 pixel = 1 pixel and use xdotool to center screen.
      • The background will not be repainted, it means you have to make sure everything is black behind your game.
    • Stretch while keeping aspect ratio is not easy, you will have to use an hybrid of stretching and xdotool

  • GPU performance will be a problem since legacy driver does not support OpenGL well.
Don't get me wrong, Wine will actually run if you set Fake-KMS on EmulationStation and I think, it will be the fastest setting ever. However EmulationStation will not start anymore so you won't be able to start Wine games or config from EmulationStation in Fake-KMS.
 
If you want Wine to work in Fake-KMS from a full console operating system with only a minimalist xserver/xinit, this guide also (partly) works. It is actually not so difficult, it is just a matter of installing the binaries and the drivers.
 
That KMS info might change with the newer Raspberry Pi's (a bit like the 3G/1G kernel that is no longer necessary on Pi 4).

Setting up

 
We leave here the mouthfed automatisation of RetroPie. This is time to go console. First leave EmulationStation.
Install the missing requirements in order to start. For Box86:
 
sudo apt-get update
sudo apt-get install git cmake make

For X-Window

sudo apt-get install xserver-xorg xserver-xorg-video-fbturbo xinit xterm xdotool matchbox-window-manager

Explanation: you need 

  • xserver-xorg for server, 
  • xserver-xorg-video-fbturbo for using legacy driver with xserver, 
  • xinit to start sessions, 
  • xterm to run scripts inside sessions, 
  • xdotool to move windows,
  • and matchbox-window-manager for window stability when running scripts.

Check your X-Window installation with xterm:

startx xterm

You should see a small white terminal in the top left of the screen. Inside it, exit:

exit

You will need to be more comfortable using xterm because you are going to be using it for testing at some point. This might seem secondary but if you spend a little time in xterm, you will most probably notice that the ugly small white screen does not fit your needs

cd ~

nano .Xdefaults

Let's make a big screen with the biggest possible font:

xterm*VT100*geometry:176x54 xdotool

# xterm*VT100*geometry:151x52 tvservice

xterm*VT100*foreground: lightblue

xterm*VT100*background: black

xterm*VT100*font: -misc-fixed-medium-r-normal-*-22-*-*-*-*-*-*-*

Try your screen to see if you are happy with it then exit:

startx xterm

exit

Now time to get your Wine binaries and Box86. I personally got them from another installation (in that case you don't need to unbzip2), you can use 3.20 from playonlinux.

RetroPie Wine emulator item
The goal: Wine working category on RetroPie

 

Wine:

wget https://www.playonlinux.com/wine/binaries/linux-x86/PlayOnLinux-wine-3.20-linux-x86.pol

sudo apt-get install bzip2

tar -jxf PlayOnLinux-wine-3.20-linux-x86.pol --strip-components=1

Box86:

cd ~

git clone https://github.com/ptitSeb/box86

cd box86;mkdir build;cd build; cmake .. -DRPI3=1;make;sudo make install

sudo systemctl restart systemd-binfmt

 Kernel 3G/1G:

 If you want to win time, you can always copy a wineprefix to retropie, otherwise

    WINEPREFIX=/your/new/wineprefix/ startx /path/to/wine/x86/bin/wine winecfg

  • Set virtual desktop to yes, its resolution to 1280x1024, desktop background color to black in "Desktop integration" and save and exit winecfg (so you avoid an ugly background)  
  • Performance-wise, you will lose direct OpenGL support. Setting ddr=gdi with winetricks will only partly answer the issue since gdi will bring screen repainting issues.

    WINEPREFIX=/your/new/wineprefix WINE=/path/to/wine/x86/bin/wine winetricks ddr=gdi

Now the tricky part. It actually works if you type in:
 
WINEPREFIX=/your/new/wineprefix/ startx /path/to/wine/x86/bin/wine /path/to/some/windows/game.exe
 
But it will always be on top left corner.
The xdotool route will center the screen, the tvservice route will stretch the game to the whole TV screen.
 

The xdotool route

 
And now the legacy driver is going to be a problem.
 
Basically, you almost cannot use any of the xrandr family functions with the legacy driver. You can ban xrandr itself. It won't work, even if you manually add resolutions. You will lose hours banging your head on the wall. 
someapp -geometry 1280x1024+320+28 works... but not for Wine. 
 
Your only way to get it to the center is to displace the Wine window after it has been opened. 

You will use xdotool and you will fork your xterm process. The strategy here is to make a script that:
  • Launches the game and at the same time detaches a second terminal
  • In the second terminal wait that the window is started (sleep 15 for example) and moves that window to center.
  • That terminal quits itself afterwards
  • Make sure that once you quit the game (the main script), you kill all the xinit windows, otherwise you will be in no man's land and can reboot your Pi.
An example of script is shown here:
 
xterm -hold -e `cd /home/pi/RetroPie/roms/wine/installed/HOMM4_eng;WINEPREFIX=/home/pi/winex86 /home/pi/wine-pol/bin/wine ./heroes4.exe;killall xinit;exit` &
sleep 15
xdotool search --onlyvisible --name heroes windowmove 320 14
matchbox-window-manager
exit

Text in red is your rom's path. Text in blue must be changed according to the binary name and Wine settings, text in purple depend on game resolution:

  • 320 28 for 1280x1024
  • 448 156 for 1024x768
  • 560 240 for 800x600
  • 640 300 for 640x480
  • 800 440 for 320x200 (but most of these games should actually be playable and played in Dosbox)
  • For exotic resolutions (640x350, 320x240...), just use the closest one. They won't be exactly centered in height but it should not prevent you from playing.
If your game loads faster or slower, you might want to decrease or increase sleep 15 to sleep 10 or sleep 25 (if xdotool is launched before the game is loaded, it will not center the window).

Save it with extension .sh, choose a directory where all your .sh will be (since the change directory command is in the script; your roms will be sorted that way)

First, start this script in console with:

    startx somescript.sh

 

Heroes of might and magic 4 in RetroPie
A started Heroes of Might and Magic IV binary on Retropie/startx
automatically centered after 15 seconds with xdotool


The tvservice route

 
This one is more straightforward. 
  • Do not forget to set hdmi_group = 2 in config.txt and reboot.
tvservice -m DMT 
  • will list you the availlable modes. you should take a screenshot/photo of that list or write it down.
  • The example script is below. fbset will align your settings to tvservice so you will not have a black screen. it is mandatory. Old games will need a 16 bits depth but newer ports like VCMI will need a 32 bits depth to work.
Example, for a 1280x1024 game (script also reverts back to previous resolution):
 
tvservice -e "DMT 35"
sleep 1
fbset -g 1280 1024 1280 1024 32
fbset -depth 8
fbset -depth 32
WINEPREFIX=... startx /your/game/binary --with-settings
tvservice -e "DMT 58"
sleep 1
fbset -g 1680 1050 1680 1050 32
fbset -depth 8
fbset -depth 32
 
This script will start with "bash somescript.sh".

Emulationstation entry

Then you can start making a new emulator by editing /etc/emulationstation/es_systems.cfg

After the last </system> in the file, type in:

For xdotool/aspect ratio:

    <system>
        <name>Wine Box86</name>
        <fullname>Wine Box86</fullname>
        <path>/home/pi/RetroPie/roms/wine</path>
        <extension>.sh</extension>
        <command>startx %ROM%</command>
        <platform>pc</platform>
        <theme>wine</theme>
      </system>

 
For tvservice/stretching:

    <system>
        <name>Wine Box86</name>
        <fullname>Wine Box86</fullname>
        <path>/home/pi/RetroPie/roms/wine</path>
        <extension>.sh</extension>
        <command>bash %ROM%</command>
        <platform>pc</platform>
        <theme>wine</theme>
      </system>

 
Startx is included in the tvservice script family. The script then starts with bash instead of startx. 

Wine theme in EmulationStation

 

Wine theme being unexistant, you will only have a text item as theme. Then you can add an icon. You will need to create a Wine theme to be set.
 
There is a nice guide to create themes there, basically, if you want to win time, best thing to do is to copy an existing theme to Wine theme and change the pictures. Avoid ".svg" image format and stick to ".png".
 
If you want to add transparency to your logo, use Pinta/Drawing. Filling tool has a tolerancy throttle to avoid halos.
 
This is possible... A working Wine section in RetroPie

 

You are all set for Wine games, congrats!

Conclusion

This has upsides and downsides:
  
👍 Better I/O and CPU performance (lesser running processes than in LXDE).
👍 All your games at the same place is possible
👍 Retropie looks nice and theme is not really difficult to make
👍 Although lower than with GL driver, performance stays in the playable family, especially for turn based games

👎 Lack of support of GL driver makes uneasy to manipulate resolutions and gives up some GPU performance. Big OpenGL games after 2000's will suffer slow speed

I cannot wait for the day when the RetroPie guys will give us a full "Fake/Full KMS" support for EmulationStation, if they do, it will most probably make Retropie the best suited OS for Wine games as well.

The pi gamer

No comments:

Post a Comment

Making XWindow, Wine and Box86 work under RetroArch [Raspberry Pi OS 32 bits lite + RetroArch as an app]

Very similar to my other article (" Making Wine and Box86 work under RetroPie [RetroPie as an OS] "), it will nevertheless show mu...