Guide to installing Moonlight (Luna Launcher) witha PS3 Controller

Hi All,

After several weekends of frustration and a ridiculous amount of googling. I tried several different solutions, none of which worked the way I wanted and were always missing bits and pieces. I was able to get Moonlight and a PS3 controller work seamlessly with OSMC. I put together the below guide in hopes that others could spend less time working on this than I did. I do have to say though, I learned a lot from getting this all working. Here’s the full guide to set up Moonghlight, a PS3 Controller, and the ability to go ‘keyboardless’. Complete with my custom scripts and mappings to make things work.

Start by SSHing into OSMC using putty. I personally prefer MobaXTerm as it also allows for file transfers directly through the interface. It is fairly handy later in this guide.

Recommended: Don’t change skins from the default OSMC until everything is done. It will make following these steps a little easier. You can if you want to though,as long as your fairly familiar with Kodi and how to install add-ons

Important note. You will need to install the current beta version of OSMC for Kodi Krypton to make the controller work properly in Kodi. Hopefully this shouldn’t be necessary in the near future. But for now it will need to be done. (Source for latest dev build:: [TESTING] Kodi 17 (Krypton) Beta Builds for all platforms)

sudo -s
echo “deb http://apt.osmc.tv krypton main” >> /etc/apt/sources.list
apt-get update
apt-get -y dist-upgrade && reboot

#Installing SIXAD and your PS3 Controller
**Thanks to il_diavolo over in the raspberry pi forums ([Solved] PS3 controller and Jessie, anybody got it working? - Raspberry Pi Forums). This is a slightly modified version of their DS3 install script.

Install the necessary packages

sudo apt-get -y install bluetooth libusb-dev joystick checkinstall libbluetooth-dev pyqt4-dev-tools libjack0 libjack-dev gcc g++

After the packages have been installed, you need to have a few more pre-requisets installed; sixpair and QtSixA.

Get sixpair

wget http://www.pabr.org/sixlinux/sixpair.c
gcc -o sixpair sixpair.c -lusb

Get QtSixA

wget http://sourceforge.net/projects/qtsixa/files/QtSixA%201.5.1/QtSixA-1.5.1-src.tar.gz
tar xfvz QtSixA-1.5.1-src.tar.gz

Trying to make QtSixA at this point causes several errors. Thankfully these are easily resolvable by doing a small modification on one of the source files. This is done by replacing the file ‘shared.h’ from the ~/QtSixA-1.5.1/sixad folder.

sudo rm QtSixA-1.5.1/sixad/shared.h
sudo nano QtSixA-1.5.1/sixad/shared.h

Replace the contents on this file with the below (Updates source code from qtsixa/shared.h at master · falkTX/qtsixa · GitHub):

/*
 * shared.h
 *
 * This file is part of the QtSixA, the Sixaxis Joystick Manager
 * Copyright 2008-2011 Filipe Coelho <falktx@gmail.com>
 *
 * QtSixA can be redistributed and/or modified under the terms of the GNU General
 * Public License (Version 2), as published by the Free Software Foundation.
 * A copy of the license is included in the QtSixA source code, or can be found
 * online at www.gnu.org/licenses.
 *
 * QtSixA is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 */

#ifndef SHARED_H
#define SHARED_H

#include <unistd.h>

struct dev_led {
	bool enabled;
	bool anim;
	bool auto_sel;
	int number;
};

struct dev_joystick {
	bool enabled;
	bool buttons;
	bool axis;
	bool sbuttons;
	bool accel;
	bool accon;
	bool speed;
	bool pos;
};

struct dev_remote {
	bool enabled;
	bool numeric;
	bool dvd;
	bool directional;
	bool multimedia;
};

struct dev_input {
	bool enabled;
	int key_select, key_l3, key_r3, key_start, key_up, key_right, key_down, key_left;
	int key_l2, key_r2, key_l1, key_r1, key_tri, key_cir, key_squ, key_cro, key_ps;
	int axis_l_type, axis_r_type, axis_speed;
	int axis_l_up, axis_l_right, axis_l_down, axis_l_left;
	int axis_r_up, axis_r_right, axis_r_down, axis_r_left;
	bool use_lr3;
};

struct dev_rumble {
	bool enabled;
	bool old_mode;
};

struct dev_timeout {
	bool enabled;
	int timeout;
};

struct device_settings {
	bool auto_disconnect;
	struct dev_led led;
	struct dev_joystick joystick;
	struct dev_remote remote;
	struct dev_input input;
	struct dev_rumble rumble;
	struct dev_timeout timeout;
};

bool was_active();
void set_active(int active);

bool io_canceled();
void sig_term(int sig);
void open_log(const char *app_name);

struct device_settings init_values(const char *mac);

int get_joystick_number();
void enable_sixaxis(int csk);

#endif // SHARED_H

Make the install for SIXAD
cd QtSixA-1.5.1/sixad
make
sudo mkdir -p /var/lib/sixad/profiles
sudo checkinstall

Now we need to update the SIXAD configuration for SIXAD to allow the DS3 controller to connect and prevent the “Bad Buffer” errors. Simply change the LEGACY=0 to LEGACY=2

sudo nano /etc/default/sixad

Replace the contents of this file with

# System-wide options for sixad
DEBUG=0
LEGACY=2

Now we can actually connect the controller
Connect the DS3 controller to Pi using your USB cable (All 4 lights should start flashing once connected) and run

cd ~
sudo ./sixpair

You should see something like the below:

Current Bluetooth master: b8:27:eb:a2:73:64
Setting master bd_addr to b8:27:eb:a2:73:64

Disconnect the controller from the Pi and run

sudo sixad --start &
sudo hciconfig hci0 pscan

Connect the controller wirelessly by pressing the PS button and turning on the controller.

Note, you may need to turn off the controller by holding the PS button for 10 seconds and then pressing the PS button again. You should see a message pop up like below

 sixad-bin[3012]: Connected Sony Computer Entertainment Wireless Controller (E0:AE:5E:70:10:B2)

If everything worked, you should be able to test the connection by using (assuming your controller is on js0):
jstest /dev/input/js0

At this point you can connect the controller only after running the sixad start commands first. Several other guides will tell you to add the commands to the /etc/rc.local file or have you create a init.d script. Sadly these never worked for me. Regardless, the init.d scripts shouldn’t be used anymore. You should be using services. I built a small service that runs at boot and executes these commands in the background, allowing you to automatically connect the controllers.

Remove the current sixad scripts from the startup process. They will conflict with the script later and I have never been able to get these to work.

sudo update-rc.d sixad stop
sudo update-rc.d sixad remove
sudo rm /etc/init.d/sixad

Now create the service

sudo nano /lib/systemd/system/sixad.service

Insert the contents:

[Unit]
Description = SIXAD Connection Daemon
Requires = bluetooth.target
[Service]
Type = forking
ExecStart = /usr/bin/StartSIXAD.sh
RemainAfterExit=yes
[Install]
WantedBy = bluetooth.target

Make it excutable

sudo chmod +x /lib/systemd/system/sixad.service

Create the StartSIXAD.sh script referenced from the above service.

sudo nano /usr/bin/StartSIXAD.sh

Insert the contents (You can remove the echo lines if you want. They are remnants of my debugging.

#!/bin/bash

touch /var/log/StartSIXAD_log.txt

echo date  > /var/log/StartSIXAD_log.txt
echo "SixAD init script started "  > /var/log/StartSIXAD_log.txt

if [ "$1" != "background" ]; then
		$0 background &
	echo " Not started in background" >> /var/log/StartSIXAD_log.txt
else
	echo " Started in background" >> /var/log/StartSIXAD_log.txt
		#Wait until BT is initialized
	for i in {0..100}
		do

		echo "  Waiting for BT to be initialized" >> /var/log/StartSIXAD_log.txt
				/usr/bin/hciconfig hci0 && break
				usleep 100000

	done

	echo " BT initialized" >> /var/log/StartSIXAD_log.txt

		/usr/bin/hciconfig hci0 up
	echo " HCIConfig hci up" >> /var/log/StartSIXAD_log.txt

	/usr/bin/hciconfig hci0 pscan
	echo " HCIConfig hci pscan" >> /var/log/StartSIXAD_log.txt

		su osmc -c "/usr/bin/sixad -s &"
	echo " SixAD Started" >> /var/log/StartSIXAD_log.txt

fi

echo "SixAD init script completed" >> /var/log/StartSIXAD_log.txt

Make this file executable:

sudo chmod +x /usr/bin/StartSIXAD.sh

Now enable the service

sudo systemctl enable sixad.service

Now reboot

sudo reboot

Your controller will turn off, the system will reboot, but the script should immediately start once OSMC has booted and Kodi has loaded. Press the PS button and it should flash for a second or two, then it should connect!

##Installing Moonlight
Now to install moonlight. This part I actually compiled a ‘custom’ version of moonlight because the default version requires you to have to get up and press Ctrl+Alt+Shift+Q to quit a game. That drove me nuts and didn’t fit my needs for laziness.

####IF YOU DON’T CARE ABOUT BEING ABLE TO QUIT MOONLIGHT USING A CONTROLLER FOLLOW THIS

Add irtimmers moonlight repository to your /etc/apt/sources.list (GitHub - irtimmer/moonlight-embedded: Gamestream client for embedded systems)

sudo nano /etc/apt/sources.list
add “deb http://archive.itimmer.nl/raspbian/moonlight jessie main” to the end of the list (without the qoutes)
sudo apt-get update
sudo apt-get install -y --force-yes moonlight-embedded

Moonlight is now installed. Nothing else to do here, skip the next section titled ‘Installing Luna Launcher in Kodi’

####IF YOU DO CARE ABOUT BEING ABLE TO QUIT MOONLIGHT USING A CONTROLLER FOLLOW THIS

This is where MobaXTerm comes in handy. There are several dev packages that are not available in the OSMC repository that need to be installed. These can be downloaded and dropped directly into the home folder and can then be installed. For reference, this next section is derived from irtimmers ‘Compile guide’ and modified slightly for OSMC (Compilation · irtimmer/moonlight-embedded Wiki · GitHub)

Install the dependencies for compiling moonlight

sudo apt-get install -y libopus0 libexpat1 libasound2 libavahi-client3 libcurl3 libevdev2 libenet7 rbp-userland-osmc libssl-dev libopus-dev libasound2-dev libavahi-client-dev libcurl4-openssl-dev libevdev-dev libexpat1-dev libpulse-dev uuid-dev libenet-dev cmake gcc g++ rbp-userland-dev-osmc fakeroot debhelper git

Download and Install the necessary compile libraries manually from debian. Place all of these directly into your home directory:

Download cmake Debian -- Package Download Selection -- cmake_3.7.2-1_armhf.deb
Download cmake-data Debian -- Package Download Selection -- cmake-data_3.7.2-1_all.deb
Download libjsoncpp Debian -- Package Download Selection -- libjsoncpp1_1.7.4-3_armhf.deb
Download libstdc++6 Debian -- Package Download Selection -- libstdc++6_6.3.0-18+deb9u1_armhf.deb
Download gcc-6-base Debian -- Package Download Selection -- gcc-6-base_6.3.0-18+deb9u1_armhf.deb
Download libuv1 Debian -- Package Download Selection -- libuv1_1.9.1-3_armhf.deb
Download libudev1 Debian -- Package Download Selection -- libudev1_247.2-2_armhf.deb
Download libudev Debian -- Package Download Selection -- libudev-dev_247.2-2_armhf.deb

Starting Installing!

sudo dpkg -i cmake-data_3.7.2-1_all.deb
sudo dpkg -i gcc-6-base_6.2.1-5_armhf.deb
sudo dpkg -i libstdc++6_6.2.1-5_armhf.deb
sudo dpkg -i libjsoncpp1_1.7.4-3_armhf.deb
sudo dpkg -i libuv1_1.9.1-3_armhf.deb
sudo dpkg -i cmake_3.7.2-1_armhf.deb
sudo dpkg -i libudev1_232-14_armhf.deb
sudo dpkg -i libudev-dev_232-14_armhf.deb

Now clone irtimmers moonlight repository

git clone https://github.com/irtimmer/moonlight-embedded.git

Now we need to modify the necessary file to add a quit command. This command requires the left trigger, right right trigger, left bumper, right bumper, and PS button to be pressed at the same time. You can change this is you want, but it works for me. (This is from the following commit from ronushun: https://github.com/runoshun/moonlight-embedded/commit/581715971344f97ed8d27070583fedc3366a1532)

Open the file for editing

sudo nano moonlight-embedded/src/input/evdev.c

add the following lines of code on line 259

if((dev->leftTrigger != 0) &&
   (dev->rightTrigger !=0) &&
   (dev->buttonFlags & LB_FLAG) &&
   (dev->buttonFlags & RB_FLAG) &&
   (dev->buttonFlags & SPECIAL_FLAG)) {
	return false;
}

The surrounding code should look like:

...
 if (mouseCode != 0) {
		LiSendMouseButtonEvent(ev->value?BUTTON_ACTION_PRESS:BUTTON_ACTION_RELEASE, $
	  } else {
		gamepadModified = true;

		if (gamepadCode != 0) {
		  if (ev->value)
			dev->buttonFlags |= gamepadCode;
		  else
			dev->buttonFlags &= ~gamepadCode;
		} else if (ev->code == dev->map.btn_tl2)
		  dev->leftTrigger = ev->value?UCHAR_MAX:0;
		else if (ev->code == dev->map.btn_tr2)
		  dev->rightTrigger = ev->value?UCHAR_MAX:0;
		else {
		  fprintf(stderr, "Unmapped button: %d\n", ev->code);
		  gamepadModified = false;
		}
		if((dev->leftTrigger != 0) &&
		   (dev->rightTrigger !=0) &&
		   (dev->buttonFlags & LB_FLAG) &&
		   (dev->buttonFlags & RB_FLAG) &&
		   (dev->buttonFlags & SPECIAL_FLAG)) {
			return false;
		}
	  }
	}
	break;
  case EV_REL:
 ...

Initialize the git submodules

cd moonlight-embedded/
git submodule update --init

Now compile!

mkdir build
cd build/
cmake …/
make
sudo make install
sudo ldconfig

Success! moonlight is now installed and we can move on to getting moonlight working in Kodi.

##Installing Luna Launcher in Kodi

Ok, small lie. First we should create the map file from moonlight to map the buttons from your DS3 controller. This will be used when entering a game from Luna Launcher later on.

In your terminal:

cd ~
moonlight map ps3.map

Moonlight will then ask you a series of questions to get your controller mapped. Follow the instructions.

Now, to get things working in Kodi - we have to map the controller configs within Kodi itself.

Go back to OSMC

Go to Settings>System settings>Input and select “Configure attached controllers”. You will likely see a message about controller configuration depending on a currently disabled add on. Go ahead and enable it.

Select ‘Get More…’
In the popup, select ‘PlayStation Controller’.
Once it finishes installing, Select the ‘PlayStation Controller’ and hit enter. You will be asked to press the buttons associated with each mapping.
Your controller is now mapped in Kodi and can be used to navigate the menus!!

It should be noted, I had a **** of a time getting the mappings to work properly. I don’t know if it’s just because this is a kodi test build or what, but the mapping interface is terrible. Rather than waiting for a secord or two after each mapping input it immediately skips to the next button and potentially maps the same button you just mapped to the next input. This could actually just be my controller though. If you have this same problem, here is my map file that I used:

Open the mapping file to be modified

sudo nano kodi/userdata/addon_data/peripheral.joystick/resources/buttonmaps/xml/linux/Sony_Computer_Entertainment_Wireless_Controller_19b_27a.xml

Replace contents with

<?xml version="1.0" ?>
<buttonmap>
	<device name="Sony Computer Entertainment Wireless Controller" provider="linux" buttoncount="19" axiscount="27">
		<configuration />
		<controller id="game.controller.default">
			<feature name="x" axis="-8" />
		</controller>
		<controller id="game.controller.ps">
			<feature name="circle" button="13" />
			<feature name="cross" button="14" />
			<feature name="down" button="6" />
			<feature name="l3" button="1" />
			<feature name="left" button="7" />
			<feature name="leftbumper" button="10" />
			<feature name="leftstick">
				<up axis="-1" />
				<down axis="+1" />
				<right axis="+0" />
				<left axis="-0" />
			</feature>
			<feature name="lefttrigger" button="8" />
			<feature name="r3" button="2" />
			<feature name="right" button="5" />
			<feature name="rightbumper" button="11" />
			<feature name="rightstick">
				<up axis="-3" />
				<down axis="+3" />
				<right axis="+2" />
				<left axis="-2" />
			</feature>
			<feature name="righttrigger" button="9" />
			<feature name="select" button="0" />
			<feature name="square" button="15" />
			<feature name="start" button="3" />
			<feature name="triangle" button="12" />
			<feature name="up" button="4" />
		</controller>
	</device>
</buttonmap>

Just to be sure everything took, go ahead and reboot.

sudo reboot

Did it work? Yay!

Ok, NOW get Luna Launcher from github GitHub - wackerl91/luna: Moonlight Launcher for Kodi. Click ‘Download or clone’ and select ‘Download Zip’
Copy the downloaded zip over to your home directory on OSMC
Go back to OSMC
Go to Add-ons >MyAddons>"…">Install from zip file
Select ‘Home Folder’ from the popup menu and then select the file you just downloaded (It should be called ‘luna develop.zip’)
After a few second Luna Launcher will be installed. Go back to your home page at this point
Go to Add-ons>Luna

Before proceeding, lets add controller mappings to Luna Launcher. This is incredibly important. Without this, your controller likely wont work in a game.
Go to ‘Controller Configuration’ and hit ‘Add Controller’
A new line should appear with ‘None(Disabled’. Select that and select your controller. Mine was named ‘Sony Computer Entertainment Wireless Controller’.
Now select ‘Add Mapping’ and then 'Select Mapping
Select the mapping file we created previously. Unless you deviated from the above, it should be in the home directory and named ‘ps3.map’.
Go back to the Luna Launcher home page and go to settings.
Under the input menu, check the box for ‘Enable Custom Input’
Your controller has been added! Next time a game is launched, Luna Launcher will tell moonlight to launch the game using the ps3.map created earlier.

Note: For me, just adding my PS3 controller didn’t work all the time. I had to copy the above steps for the USB receivers as well.

Now we add a host.
Go back to the main page of Luna Launcher and select ‘Add host’
Enter the IP of your streaming server and hit ‘Done’.
A popup should appear telling you it’s attempting to pair. In a few seconds, it will ask you to enter a pin on your streaming server.
Go to the host server and enter the pin. NVidia made it quite easy as it should popup as a notification immediately.
Once paired, the host system should appear as a new selection on the Luna Launcher home page.

THATS IT. WE ARE DONE! Select the host system and it will list out all your games (It may take a few minutes for Luna to do some information gathering). You can even add custom ‘games’ to stream things like Steam Big Picture, or your desktop. That’s outside of the scope of this ‘How-To’ though.

Hopefully this helps anyone attempting to get a nice integration for a full media center.

2 Likes

I tried to follow the instructions no joy :frowning: http://pastebin.com/JE9ikQJm

Was this on a clean install? Or did you already have items installed on this Pi? Just curious. It seems that the checkinstall couldn’t create the directory for bins/sixad-bin

No it wasn’t a clean install. I had already scanned in all my movies and tv shows and one addon

hi there.

would you mind testing if the R1/R2 and L1/L2 are working as intended with your setup?
Because i got a Rpi moonlight setup running w/ Raspbian. But if i hit L1 or R1 in a game, it triggers L2/R2 too

If your config could fix this issue i would give it a try

thanks for tutorial anyways

regards

The only game I can get to work with the controller is Binding of Isaac rebirth, because it supports Xinput. and even then, Down is constantly being held. no other game will do anything. Fix?