Sunday, January 24, 2021

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

🕐🕐🕐 Duration: Up to one day (kernel compiling)
🔧🔧🔧🔧 Difficulty: Difficult
🌟🌟🌟🌟 Interest: Hours of fun

Read this first

This guide is intented to be used with RetroPie as a starting OS. It's not usable for RetroPie inside TwisterOS.
[UPDATE October 14, 2021]: xrandr now available for Pi4.

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
 
If you have an older Pi, you will be bumped back to 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.
 
There is confirmation that Pi 4 can now use xrandr -s and other KMS features on RetroPie. There will be a section on this next to tvservice.

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

[UPDATE October 14, 2021] Dont do tvservice if you own a Pi 4, just use xrandr.
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".
 

Xrandr route [UPDATE October 14, 2021]

 
Easiest of all, xrandr only works on latest Pi's, other ones being stuck with tvservice. Please note that if you are trying to use tvservice on modern Pi's, xserver will switch automatically back to maximum resolution upon launching and by that kill your efforts in making your game full screen. 
 
The script is like tvservice one, with a few simpler things and one pitfall.
 
Example, for a 1280x1024 game (script also reverts back to previous resolution):
                
xrandr -s 1280x1024
WINEPREFIX=... startx /your/game/binary --with-settings
xrandr -s 1920x1080
 
This script will start with (pitfall here) "startx 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

8 comments:

  1. hello i'm following this guide but i have some problems. i installed winearm for diablo 2 with pikiss and it works. winearm is located in etc / alterntive and usr / bin and wineprefix in home / pi / .wine I created a folder in home / pi / wine86 with winei386 if I type wine in this folder it starts winearm and not wini386.
    I created the sh script for xdotool by modifying it with my parameters. if I type startx xterm from the command line it works fine but if I launch the sh file I get this error. xinit: unexpect signal 15. I ran the script from an lxterminal window. File sh is in home/pi.

    ReplyDelete
    Replies
    1. The script doesn't work well in RetroPie. When the 15 seconds are up, the game exits. xdotool doesn't seem to work.
      What can be done?
      Best regards:

      Delete
  2. sorry for the previous message. now i can run both winearm and winei386. my problem now is winetricks is connected to winearm and not awinei386. how can i connect winetricks to winei386?

    ReplyDelete
    Replies
    1. Hello,

      Good to know you finally succeeded.
      People often report trouble when trying to make winearm and winex86 run together on a RetroPie as a system starting point.

      To answer your winetricks question, see "Winetricks" section nearly at the end of my other post:
      https://thepigamer.blogspot.com/2021/01/performance-topic-running-x86-games-on.html

      Make sure to disable box86 banner while running Winetricks on your winex86.

      Have fun
      The pi gamer

      Delete
  3. I think this script also has problems.
    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

    if I start I get:
    line 1 $ '\ r' command not found sleep invalid time interval 15 \ r
    line 4 matchbox-window-manager \ r command not found
    line 5 exit \ r command not found

    matchbox-window- manager is installed

    to start the script on the console maybe the command is xinit and not startx?

    ReplyDelete
    Replies
    1. For some reasons it seems that spaces were strangely added to your copy/paste. Did you have these spaces in your script in the first time?

      EG: cd / home / pi / RetroPie / --> cd /home/pi/RetroPie/

      exit should not be a "command not found", exit is builtin.

      Delete
    2. no... spaces are not in my script.
      this is my script:

      xterm -hold -e `cd
      /home/pi/RetroPie/roms/wine/model1; WINEPREFIX=/home/pi/wine86/wine2 /home/pi/wine86/bin/wine ./Modeler.exe;killall xinit;exit` &
      sleep 15
      xdotool search --onlyvisible --name segamodel1 windowmove 560 240
      matchbox-window-manager
      exit

      Delete
    3. Send me a PM. I'll try to troubleshoot that with you on Google chat.

      At first view it shouldn't be a script errors, you would have a syntax error or something like that.

      After it works for you, I'll delete the history of comments and change the guide if necessary.

      TPG

      Delete

Play ET: Legacy on Raspberry Pi [FPS]

🕐🕐 Duration: A few hours 🔧 Difficulty: Easy 🌟🌟🌟🌟 Interest: Hours of fun