Creating a GUI loader for MAME ROM images

back arrow icon

assembly date

2007, September 8.

author(s)

Balla Marcell

keywords

  • mame
  • scripting

The goal of this topic is to create a GUI that lets you comfortably load and play your MAME games. For this purpose we are going to take advantage of the gdialog program. Before we begin I would like to explain the key components which are part of this topic:

MAME
MAME is the abbreviation for Multiple Arcade Machine Emulator, which is an open source project that enables users to play games they know from arcade halls on their home computer. The core of MAME is a software application, which emulates the required hardware for the arcade games and thereby enables them to be run.
ROM
A ROM is an image of an arcade game that can be passed onto MAME to be processed. The MAME reads the contents of the image, which results in the game made available for the user on the PC screen.
gdialog
Gdialog is a program that lets you display dialog boxes with predefined contents – like a text message or buttons. It gives you the possibility to create windows without actually having to know how to do window based programming.

Having explained these components we can start creating our GUI. For our example we will use the xmame arcade emulator which lets the user supply the ROM image (game) to be run as a command line argument (e.g. xmame pang, would start the arcade video game called “pang”).

What we want is a dialog box with a list of available ROM images that lets the user select a game to play. The name of the selected game gets passed on to the xmame application which then loads it.

To achieve this we need to consider two basic things:

  1. How do we determine which MAME ROMs (games) are available?
    Answer: usually the ROMs are stored in one single directory. So we can simply use the ls unix shell command to get a list of all ROMs.
  2. How do we get the gdialog application to display the list of ROMs?
    Answer: we will tell the gdialog to display a menu list and pass the list of games to the gdialog as parameter.

This sounds very simple but requires some unix shell command knowledge though as you will recognize shortly.

Lets first have a look at the gdialog application and how we create a window with a menu list inside. You can find the manual pages of this program in the “related links” section at the top. We will use the following command to create our menu dialog (The result of this command is shown in Figure 1):

gdialog --title "rom loader" --menu "" 15 20 10

empty gdialog menu list Figure 1: gdialog with an empty menu list

As you may have noticed the menu list shown in Figure 1 has two columns. The gdialog application doesn’t give us the possibility to have only a single column. We would only need one column in our GUI for the name of the games. But as there are two columns lets take advantage of this circumstance and put some extra information in the second column. In this example we will insert the size of the ROM files in the second column.

When the user hits the “OK” button the gdialog returns the content of the first column of the selected row as result. It is important to know that the result is returned to the standard error unix handle (more about how we deal with this follows later).

As next step we need to gather the name and the size of each available ROM file. To get this information we simply rely on the ls unix command with some parameters. What we do is the following:

ls -lh /home/foobar/MameRoms/*.zip

This command returns the filename of each ROM file (which are ZIP files) and the size of each file in a human readable format (e.g. 106KB). The result is shown in Figure 2.

result of our ls -lf command Figure 2: result of the ls -lh command

You have to change the path of this example (“/home/foobar/MameRoms”) to the one where your ROM files are stored.

The output of our ls command (see Figure 2) contains many information of which we only need two things (you guessed it, those that are outlined red).

We will use the AWK programming language for this purpose, which is perfectly suited for this task. To be more specific: we will use the split function of the AWK language to extract the required information. We pass the output of our ls command to AWK by using unix pipes. So what we do now is:

ls -lh /home/foobar/MameRoms/*.zip | awk '{split($9,path,"/"); split(path[5],rom,"."); print rom[1] " " $5}'

This command divides the filenames (represented by $9 which is a placeholder for the ninth column of the result from the ls command), into substrings at each occurrence of the “/” character. Then it takes the part which contains the name of the file (represented by path[5] which is the fifth part in this example) and splits it at the “.” character and takes the first half (represented by rom[1]), which is the filename and returns (prints) it followed by the size of the file (represented by $5 which is a placeholder for the fifth column returned by the ls command).

According to these facts, you might have to change the number in the first pair of brackets (path[5]) depending on the number of subdirectories in which your ROM files are located.

Now we have the information we wanted and only need to pass it to the gdialog as parameter. The output from the command above is already in the correct format and can be passed to our dialog by using the xargs unix command (see the related links section). The final command to build our dialog with the menu list containing all available games looks like this:

ls -lh /home/marcell/MameRoms/*.zip | awk '{split($9,path,"/"); split(path[5],rom,"."); print rom[1] " " $5}' | xargs gdialog --title "rom loader" --menu "" 15 20 10

filled gdialog menu list Figure 3: : gdialog with the available games

Figure 3 shows our dialog with all the available games in the menu list.

The only thing missing now is that the xmame application needs to be called with the selected game when the user clicks the “OK” button. We mentioned before that the name of the selected game is returned by the gdialog command to the standard error unix handle. So if we want to use the result we need to apply some tricky shell syntax. Finally we should encapsulate all of our commands into a shell script which can be easily executed by the user whenever he wants to play an arcade game. Our complete shell script looks like this:

#!/bin/bash
rom=$(
ls -lh /home/foobar/MameRoms/*.zip | awk '{split($9,path,"/"); split(path[5],rom,"."); print rom[1] " " $5}' | xargs gdialog --title "rom loader" --menu "" 15 20 10 2>&1)
xmame -ef 1 /home/foobar/MameRoms/$rom

What we added to create our shell script is:

  1. The first line necessarily has to be the path to the shell script interpreter (bash) preceded by the characters # and !
  2. The “rom” shell variable stores the selected game’s name by redirecting the result of the gdialog (standard error handle = 2) to it
  3. The xmame application is called with the name of the selected game as parameter (-ef 1 is added to double the displayed video size)

Put the three shell command lines in a file and save it as "xmame.sh" for example. Don't forget to set the execute flag of the file to be able to run it:

chmod o+x xmame.sh

You may place a shortcut to this file on your desktop to easily reach your script and MAME games. Have fun!

back arrow icon