Skip to content

1.3.1 X11 Configuration

PortalMario edited this page Jan 18, 2023 · 2 revisions

X11 Configuration

As we configured the system to boot directly into the CLI, we now need to autostart a minimal graphical environment that does the following things using x11:

  1. Setup the primary display output for 1080p and at least 60 FPS using xrandr.
  2. Display a custom bootup animation every time the Graphical environment starts.
  3. Launching emulationstation and a minimal window-manager system in which dolphin can be started correctly.

Note: Those things need to happen right after the system lands on the CLI after the boot process. Therefore, we want a specific default behavior as close to an original game console experience as possible. (e.g.: Always show the boot animation first.)

Dependencies/Software Installation

To achieve the steps listed above we first need to install the required software packages:

$ apt install xorg mpv matchbox-window-manager -y
  • xorg

    • We want to make sure to have all relevant x11 components present for our system. This package includes everything we need that is x11 related.
  • mpv

    • Is a minimal video player that can be launched/configured from the command line and will be used to play a custom boot animation.

    Note: This software can’t be executed within the actual boot process! Its only accessible after the system booted. (Which still is fast enough for us due to the highly decreased boot time.)

  • matchbox-window-manager

    • A minimal window manager that can be started/configured from the command line. Emulationstation cannot display dolphin correctly without a window manager to which it can access.

xinitrc configuration

When launching a x11-session, x11 will look into the system-wide xinitrc. X11 will execute all commands listed within this file if an x11-session gets started with the startx command. We want to put the following contents into the system-wide xinitrc at: /etc/X11/xinit/xinitrc:

#!/bin/bash

# Set video output and resolution for X11.
# Important!: The Resolution chosen here MUST match the internal resolution params of the game. 
# otherwise vsync will slow down the games.
xrandr --output HDMI-A-0 --mode 1920x1080 --auto

# Display custom video before emulationstation launch.
mpv --volume=130 --fullscreen  --input-vo-keyboard no --audio-device=auto --osc=no /srv/media/gcn.mp4

# Launch emulationstation.
emulationstation --windowed &
matchbox-window-manager -use_titlebar no -use_cursor no

Let's take a look at the commands within the xinitrc.

Note: those commands get executed in a sequential order, therefore it first configures our screen, plays the custom boot animation and then launches into emulationstation.

  • xrandr

    • xrandr is an X11 utility that lets us configure different video outputs. It also can be used to scan different video output and their current capabilities. In this case we use the first physical HDMI port which is called HDMI-A-0 within x11. So, if you later connect your TV to another possible HDMI, you won’t get any output! (We don't use DisplayPort as most TV's don’t support it.)

    Here we also tell x11 with xrandr that the chosen video output (HDMI-A-0) should be configured for a 1080x1920 resolution with the highest possible FPS (--auto will tell xrandr to configure the highest/best/recommended possible settings according to the connected display and its capabilities.).

    Note: The name of the first HDMI interface differs from each motherboard. In most cases it should be named HDMI-0 but in our case here the system recognizes the first HDMI port of the motherboard that we later use for this project, as HDMI-A-0. If you are unsure about the name, i recommend installing xterm with the command: sudo apt install xterm. Next you boot the system into the cli without the graphical desktop and launch the command: startx xterm. This will launch a graphical minimal terminal. Now you run the comamnd: xrandr. Afterwards xrandr will list you all connected/recognized display outputs and their capabilities. Look for the display output name that is stated as "connected" and "primary" and you have the information you need for the xinitrc configuration. (In this case HDMI-A-0.) (Of course, you need to be connected to the HDMI port which you want to use later which should be the first HDMI port.)

  • mpv

    • mpv is a minimal video player that we want to use to play our custom boot up animation. Make sure you placed your custom boot up animation at /srv/media/gcn.mp4. (I recommend having about 1-2 seconds within the video file before your animation plays to avoid any sound cutoffs.)

      • We set the volume level to 130 and use the --fullscreen option to display the animation on the entire screen.
      • --input-vo-keyboard=no is used to disable any user interaction/input while the video plays.
      • --audio-device=auto Chooses the default audio device which should be HDMI-A-0 (alsa output).
      • --osc=no Disables the on-screen control interface. So, it creates the illusion that the boot screen is not played from within a video player.
      • At the end we point to the file location of our custom boot animation.
  • emulationstation

    • We start emulationstation with --windowed so dolphin can be layed on top of it avoiding possible problems that could occur when switching between full screen applications. The & operator prevents the system to wait for emulationstation to finish (sends the process to the background), freeing the xinitrc script to be able to execute another graphical application in the meantime/simultaneously. (This might not be needed to be done with &.)
  • matchbox-window-manager

    • Launches a minimal window manager that helps/supports the system managing/arranging the dolphin interface as well as the emulationstation interface automatically in the correct way.

      • -use_titlebar no is used to remove the title bar of any window that shows the application name.
      • -use_cursor no removes the mouse cursor from the entire screen.

      Those two options helps in creating a seamless and good looking user experience.

VSync Problem

Why are we not setting the xrandr option --mode to auto so the maximum possible screen resolution of the connected device/screen will be detected and used for X11 automatically?

We configure our games within dolphin to use vertical synchronization (VSync) with an emulated game resolution statically set to 1080p. Let’s take a look at an example: Let's say, X11 sets itself to an 4K resolution that got auto-detected due to a connected 4K display. Now we launch a game that is set to be emulated with a resolution of 1080p with vsync enabled to avoid bad tearing effect. (Note: we use 1080p for games as our APU is too weak to emulate a 4K resolution on all games!) Doing so will result into a very bad/low framerate that makes the emulation unplayable.

Root Cause

The Problem is caused by VSync. VSync now tries to synchronize the emulated game (which is set to an 1080p resolution) with the connected screen (that is set to its maximum resolution, in this case: 4k). As those two resolutions do not match, vsync cannot determine how to sync the game’s 1080p resolution correctly with the connected screen and its 4K resolution.

Resolution

To mitigate this problem, we just have to set X11 to always configure a static resolution that matches the resolution of our game configuration within dolphin. In this case 1080p. Now VSync works as intended and we get higher/desired Framerates!

Deep dive: Window Manager

When starting a x11-session with startx a new, very small x11-"minimal window" opens in which every graphical application would be displayed. With xrandr we tell the x11-server to fill the whole monitor/tv telling x11 how to present the "minimal Window".

A window manager uses this x11-session and enables us to use it to launch multiple applications within different kind of "frames" or "windows" within the x11 graphical system. Without it, only one application could be started without the ability to drag or resize the application during execution. So, you could say a window manager really organizes the room that is available and use it more efficient with multiple applications. We mainly need it for dolphin, which gets started within/from emulationstation. As emulationstation would be the first application occupying the x11 session, dolphin would be blocked by x11 and can’t get launched and layed on top of emulationstatin in full screen as it would meant to work. The window manager just moves dolphin correctly over the emulationstation frontend in full screen while keeping emulationstation present in the background!

Conclusion

If we now execute the command: startx as the primary user of the system, we should first see the custom boot animation followed by the emulationstation user interface. If games are located at the correct path similar to the game box cover art, we can launch dolphin by selecting a game of choice and dolphin should run in the way we expect.

Clone this wiki locally