# Controlling openMSX from External Applications

## Introduction

The architecture of openMSX is that the main emulation code is all in a separate program, the main openMSX executable. Debugger GUI's, launcher GUI's, etc., can be external programs that control openMSX. This document explains you how you can control openMSX from your own application.

Note: This document was not written for normal end users, but only for developers who are interested in writing their own application that controls openMSX.
Disclaimer: it is possible that some update events are still missing and it is also possible that the structure of the replies and commands change. We will do our best to be backwards compatible, though.

## Connecting

There are multiple ways to connect to openMSX. The first (and oldest) way is using a pipe. On Windows you can use a named pipe, on other systems you use `stdio`. To enable this, start openMSX like this:

openmsx -control stdio

or for Windows:

openmsx -control pipe

The second method is using a socket. Connecting on non-Windows systems goes with a UNIX domain socket. openMSX puts the socket in `/tmp/openmsx-<username>/socket.<pid>`. The `/tmp/` dir can be overridden by environment variables `TMPDIR`, `TMP` or `TEMP` (in that order).

On Windows (which does not support UNIX domain sockets), the socket is a normal TCP socket. The port number is random between 9938 and 9958. This is done to enforce applications to deal with multiple running openMSX processes. The port number will be put in the following text file:

%USERPROFILE%\Documents and Settings\<username>\Local Settings\Temp\openmsx-default\socket.<pid>

or, when `%USERPROFILE%` does not exist:

`%TMPDIR%\openmsx-default` or
`%TMP%\openmsx-default` or
`%TEMP%\openmsx-default` or as a last resort:
`C:\WINDOWS\TEMP`

After connecting, openMSX expects XML input on the channel and it will also give you output. This is explained in the next section.

## Communication

After connecting, openMSX expects XML input on the channel (pipe or socket) and it will also give you output in XML format. The first output it gives is this:

    <openmsx-output>
    <log level="info">Using default machine: Boosted_MSX2_EN</log>

On non Windows systems you can easily try it out by just starting openMSX via the `stdio` method, like explained above. You give XML commands via the keyboard in the terminal and openMSX will print its responses on the terminal as well.

This first output is the opening tag (`<openmsx-output>`) and a log message. All messages that are normally printed on stdout in the terminal from which you start openMSX are in a `<log>` tag. The level can be "info" or "warning" and the message is in the text node itself.

When you want to start communicating back, you always have to start with the opening tag first:

<openmsx-control>

When starting openMSX with the `-control` option, it will not show a window: it starts with the 'none' renderer. So, a nice example (if you're still experimenting on the command line) would be to type this:

<command>set renderer SDL</command>

With the `<command>` tag you can give any openMSX console command to openMSX. The commands are documented in the Console Commands Reference.

Every `<command>` will result in a reply from openMSX. In the above case it will be:

    <reply result="ok">SDL</reply>

The order is guaranteed, i.e. the replies will follow in the same order as in which you gave the commands to openMSX. In this reply example, you see that the command succeeded (result=ok) and it also gives you the actual result text that would also be printed on the console. In this case, the value of the renderer setting. When a command fails, you get something like this:

    <command>biep</command>
    <reply result="nok">invalid command name "biep"
    </reply>

"biep" is not a valid command, and openMSX tells you this via a "nok" reply with the error message in the text node.

The next important thing is events. When you use this interface to control openMSX, you want to know when things change. For this, you can enable events for certain event classes.

An example:

<command>openmsx_update enable led</command>

This command will enable updates about LED events. So, when a LED changes, you get messages like this:

    <update type="led" machine="machine1" name="power">on</update>

So, when you get <update> tags, openMSX tells you that something changed. In this case, it is a LED update, for the machine with ID "machine1". The name of the LED is "power" and the value is in the text node: on.

Here is a list of the currently available event types and when they are sent:

`hardware` | hardware changes occurred, like a change of machine
`led` | LED status changed
`media` | media (disk images, cartridges, etc.) changed
`plug` | a pluggable got plugged
`unplug` | a pluggable got unplugged
`setting` | the value of a setting changed
`setting-info` | the properties of a setting changed (e.g. number of options changed)
`status` | status changed, currently this is pause and debug break status
`extension` | extensions changed (add/remove)
`sounddevice` | sounddevices changed (add/remove)
`connector` | connectors changed (add/remove)

### Update Examples

Someone changed machines from Boosted MSX2 to Toshiba HX-10 at run time:

    <update type="hardware" name="machine2">add</update>
    <update type="hardware" machine="machine2" name="carta">add</update>
    <update type="hardware" machine="machine2" name="cartb">add</update>
    <update type="hardware" machine="machine2" name="cassetteplayer">add</update>
    <update type="hardware" machine="machine1" name="diskb">remove</update>
    <update type="hardware" machine="machine1" name="diska">remove</update>
    <update type="hardware" machine="machine1" name="carta">remove</update>
    <update type="hardware" machine="machine1" name="cartb">remove</update>
    <update type="hardware" machine="machine1" name="cartc">remove</update>
    <update type="hardware" machine="machine1" name="cassetteplayer">remove</update>
    <update type="hardware" name="machine1">remove</update>
    <update type="hardware" name="machine2">select</update>

CAPS LED went to OFF:

    <update type="led" machine="machine2" name="caps">off</update>

A tape was inserted in the cassetteplayer:

    <update type="media" machine="machine2" name="cassetteplayer">/home/manuel/msx-soft/tapes/Zoids.zip</update>

The cassetteplayer got into play mode:

    <update type="status" machine="machine2" name="cassetteplayer">play</update>

Someone plugged in a joystick:

    <update type="plug" machine="machine2" name="joyporta">joystick1</update>

And unplugs it again:

    <update type="unplug" machine="machine2" name="joyporta"></update>

Someone set the maxframeskip setting to 12:

    <update type="setting" name="maxframeskip">12</update>

openMSX got paused:

    <update type="status" name="paused">true</update>

openMSX got into a debug break state:

    <update type="status" machine="machine1" name="cpu">suspended</update>

openMSX got out of debug break state:

    <update type="status" machine="machine1" name="cpu">running</update>

Someone inserted a Philips NMS-1205 Music Module:

    <update type="sounddevice" machine="machine2" name="Philips NMS 1205 Music Module MSX-Audio 8-bit DAC">add</update>
    <update type="connector" machine="machine2" name="audiokeyboardport">add</update>
    <update type="sounddevice" machine="machine2" name="Philips NMS 1205 Music Module MSX-Audio DAC">add</update>
    <update type="sounddevice" machine="machine2" name="Philips NMS 1205 Music Module MSX-Audio">add</update>
    <update type="extension" machine="machine2" name="Philips_NMS_1205">add</update>

And with this, you should have all info that you need to make any external application that can control openMSX.

More real world examples can be found here:

- in the Contrib directory of openMSX (openmsx-control*)
- in the code of the old openMSX Catapult (C++ via pipe)
- in the code of the new openMSX Catapult (Python, still via pipe)
- in the code of the openMSX GUI Debugger (C++ via socket)