Skip to content

Commit

Permalink
libretro: support mouse retropad.
Browse files Browse the repository at this point in the history
  • Loading branch information
audetto committed Nov 24, 2021
1 parent 6175dc8 commit f080599
Show file tree
Hide file tree
Showing 13 changed files with 123 additions and 21 deletions.
2 changes: 2 additions & 0 deletions source/frontends/libretro/CMakeLists.txt
Expand Up @@ -5,6 +5,7 @@ set(SOURCE_FILES
game.cpp
joypadbase.cpp
joypad.cpp
mouse.cpp
analog.cpp
rdirectsound.cpp
retroregistry.cpp
Expand All @@ -17,6 +18,7 @@ set(HEADER_FILES
game.h
joypadbase.h
joypad.h
mouse.h
analog.h
rdirectsound.h
retroregistry.h
Expand Down
7 changes: 4 additions & 3 deletions source/frontends/libretro/analog.cpp
Expand Up @@ -9,8 +9,9 @@
namespace ra2
{

Analog::Analog()
: myAxisCodes(2)
Analog::Analog(unsigned device)
: JoypadBase(device)
, myAxisCodes(2)
{
myAxisCodes[0] = std::make_pair(RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X);
myAxisCodes[1] = std::make_pair(RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y);
Expand All @@ -19,7 +20,7 @@ namespace ra2
double Analog::getAxis(int i) const
{
const auto & code = myAxisCodes[i];
const int value = input_state_cb(0, RETRO_DEVICE_ANALOG, code.first, code.second);
const int value = input_state_cb(0, myDevice, code.first, code.second);
const double axis = 2.0 * double(value - AXIS_MIN) / double(AXIS_MAX - AXIS_MIN) - 1.0;
return axis;
}
Expand Down
2 changes: 1 addition & 1 deletion source/frontends/libretro/analog.h
Expand Up @@ -10,7 +10,7 @@ namespace ra2
class Analog : public JoypadBase
{
public:
Analog();
Analog(unsigned device);

double getAxis(int i) const override;

Expand Down
22 changes: 22 additions & 0 deletions source/frontends/libretro/game.cpp
Expand Up @@ -12,6 +12,7 @@
#include "CPU.h"
#include "NTSC.h"
#include "Utilities.h"
#include "Interface.h"

#include "linux/keyboard.h"
#include "linux/registry.h"
Expand Down Expand Up @@ -76,6 +77,11 @@ namespace ra2

SetFrame(myFrame);
myFrame->Initialize();

Video & video = GetVideo();
// should the user be allowed to tweak 0.75
myMouse[0] = {0.0, 0.75 / video.GetFrameBufferBorderlessWidth(), RETRO_DEVICE_ID_MOUSE_X};
myMouse[1] = {0.0, 0.75 / video.GetFrameBufferBorderlessHeight(), RETRO_DEVICE_ID_MOUSE_Y};
}

Game::~Game()
Expand Down Expand Up @@ -107,6 +113,7 @@ namespace ra2
{
input_poll_cb();
keyboardEmulation();
mouseEmulation();
}

void Game::keyboardCallback(bool down, unsigned keycode, uint32_t character, uint16_t key_modifiers)
Expand Down Expand Up @@ -276,6 +283,21 @@ namespace ra2
}
}

void Game::mouseEmulation()
{
for (auto & mouse : myMouse)
{
const int16_t x = input_state_cb(0, RETRO_DEVICE_MOUSE, 0, mouse.id);
mouse.position += x * mouse.multiplier;
mouse.position = std::min(1.0, std::max(mouse.position, -1.0));
}
}

double Game::getMousePosition(int i) const
{
return myMouse[i].position;
}

bool Game::loadGame(const std::string & path)
{
const bool ok = insertDisk(path);
Expand Down
12 changes: 12 additions & 0 deletions source/frontends/libretro/game.h
Expand Up @@ -27,6 +27,8 @@ namespace ra2

void drawVideoBuffer();

double getMousePosition(int i) const;

static void keyboardCallback(bool down, unsigned keycode, uint32_t character, uint16_t key_modifiers);

static void frameTimeCallback(retro_usec_t usec);
Expand All @@ -44,8 +46,18 @@ namespace ra2

std::vector<int> myButtonStates;

struct MousePosition_t
{
double position; // -1 to 1
double multiplier;
unsigned id;
};

MousePosition_t myMouse[2];

bool checkButtonPressed(unsigned id);
void keyboardEmulation();
void mouseEmulation();

static void processKeyDown(unsigned keycode, uint32_t character, uint16_t key_modifiers);
static void processKeyUp(unsigned keycode, uint32_t character, uint16_t key_modifiers);
Expand Down
7 changes: 4 additions & 3 deletions source/frontends/libretro/joypad.cpp
Expand Up @@ -6,8 +6,9 @@
namespace ra2
{

Joypad::Joypad()
: myAxisCodes(2)
Joypad::Joypad(unsigned device)
: JoypadBase(device)
, myAxisCodes(2)
{
myAxisCodes[0][RETRO_DEVICE_ID_JOYPAD_LEFT] = -1.0;
myAxisCodes[0][RETRO_DEVICE_ID_JOYPAD_RIGHT] = 1.0;
Expand All @@ -19,7 +20,7 @@ namespace ra2
{
for (const auto & axis : myAxisCodes[i])
{
const int value = input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, axis.first);
const int value = input_state_cb(0, myDevice, 0, axis.first);
if (value)
{
return axis.second;
Expand Down
2 changes: 1 addition & 1 deletion source/frontends/libretro/joypad.h
Expand Up @@ -11,7 +11,7 @@ namespace ra2
class Joypad : public JoypadBase
{
public:
Joypad();
Joypad(unsigned device);

double getAxis(int i) const override;

Expand Down
15 changes: 9 additions & 6 deletions source/frontends/libretro/joypadbase.cpp
Expand Up @@ -6,17 +6,20 @@
namespace ra2
{

JoypadBase::JoypadBase()
: myButtonCodes(2)
ControllerBase::ControllerBase(unsigned device, const std::vector<unsigned> & buttonCodes)
: myDevice(device), myButtonCodes(buttonCodes)
{
myButtonCodes[0] = RETRO_DEVICE_ID_JOYPAD_A;
myButtonCodes[1] = RETRO_DEVICE_ID_JOYPAD_B;
}

bool JoypadBase::getButton(int i) const
bool ControllerBase::getButton(int i) const
{
const int value = input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, myButtonCodes[i]);
const int value = input_state_cb(0, myDevice, 0, myButtonCodes[i]);
return value != 0;
}

JoypadBase::JoypadBase(unsigned device) : ControllerBase(device, {RETRO_DEVICE_ID_JOYPAD_A, RETRO_DEVICE_ID_JOYPAD_B})
{

}

}
16 changes: 13 additions & 3 deletions source/frontends/libretro/joypadbase.h
Expand Up @@ -8,15 +8,25 @@
namespace ra2
{

class JoypadBase : public Paddle
class ControllerBase : public Paddle
{
public:
JoypadBase();
ControllerBase(unsigned device, const std::vector<unsigned> & buttonCodes);

bool getButton(int i) const override;

protected:
const unsigned myDevice;

private:
std::vector<unsigned> myButtonCodes;
const std::vector<unsigned> myButtonCodes;
};

class JoypadBase : public ControllerBase
{
public:
JoypadBase(unsigned device);
};


}
13 changes: 10 additions & 3 deletions source/frontends/libretro/libretro.cpp
Expand Up @@ -18,6 +18,7 @@
#include "frontends/libretro/retroregistry.h"
#include "frontends/libretro/joypad.h"
#include "frontends/libretro/analog.h"
#include "frontends/libretro/mouse.h"

namespace
{
Expand Down Expand Up @@ -67,15 +68,20 @@ void retro_set_controller_port_device(unsigned port, unsigned device)
switch (device)
{
case RETRO_DEVICE_NONE:
Paddle::instance.reset();
break;
case RETRO_DEVICE_JOYPAD:
Paddle::instance.reset(new ra2::Joypad);
Paddle::instance.reset(new ra2::Joypad(device));
Paddle::setSquaring(false);
break;
case RETRO_DEVICE_ANALOG:
Paddle::instance.reset(new ra2::Analog);
Paddle::instance.reset(new ra2::Analog(device));
Paddle::setSquaring(true);
break;
case RETRO_DEVICE_MOUSE:
Paddle::instance.reset(new ra2::Mouse(device, &ourGame));
Paddle::setSquaring(false);
break;
default:
break;
}
Expand Down Expand Up @@ -124,7 +130,8 @@ void retro_set_environment(retro_environment_t cb)
static const struct retro_controller_description controllers[] =
{
{ "Standard Joypad", RETRO_DEVICE_JOYPAD },
{ "Analog Joypad", RETRO_DEVICE_ANALOG }
{ "Analog Joypad", RETRO_DEVICE_ANALOG },
{ "Mouse", RETRO_DEVICE_MOUSE },
};

static const struct retro_controller_info ports[] =
Expand Down
21 changes: 21 additions & 0 deletions source/frontends/libretro/mouse.cpp
@@ -0,0 +1,21 @@
#include "frontends/libretro/mouse.h"
#include "frontends/libretro/environment.h"
#include "frontends/libretro/game.h"

#include "libretro.h"

namespace ra2
{

Mouse::Mouse(unsigned device, const std::unique_ptr<Game> * game)
: ControllerBase(device, {RETRO_DEVICE_ID_MOUSE_LEFT, RETRO_DEVICE_ID_MOUSE_RIGHT})
, myGame(game)
{
}

double Mouse::getAxis(int i) const
{
return *myGame ? (*myGame)->getMousePosition(i) : 0.0;
}

}
23 changes: 23 additions & 0 deletions source/frontends/libretro/mouse.h
@@ -0,0 +1,23 @@
#pragma once

#include "frontends/libretro/joypadbase.h"

#include <memory>

namespace ra2
{

class Game;

class Mouse : public ControllerBase
{
public:
Mouse(unsigned device, const std::unique_ptr<Game> * game);

double getAxis(int i) const override;

private:
const std::unique_ptr<Game> * myGame;
};

}
2 changes: 1 addition & 1 deletion source/linux/paddle.h
Expand Up @@ -11,7 +11,7 @@ class Paddle
virtual ~Paddle();

virtual bool getButton(int i) const;
virtual double getAxis(int i) const;
virtual double getAxis(int i) const; // -> [-1, 1]

int getAxisValue(int i) const;

Expand Down

0 comments on commit f080599

Please sign in to comment.