dolphin/Source/Core/InputCommon/ControllerInterface/Device.h
Michael Maltese c62d83a34b GCPadEmu: only connected if default device connected
This lets Dolphin know if a configured GameCube Controller should actually
be treated as connected or not.

Talked to @JMC47 a bit about this last night. My use-case is that all of
my controllers are the same hardware (Xbox One controllers) so share the
same configuration (modulo device number). Treating them all as always
connected isn't a problem for most games, but in some (Smash Bros.) it
forces me to go find a keyboard/mouse and unconfigure any controllers
that I don't actually have connected. Hotplugging devices (works on macOS,
at least) + this patch remove my need to ever touch the Controller Config
dialog while in a game.

This patch makes the following changes:

- A new `BooleanSetting` in `GCPadEmu` called "Always Connected", which
  defaults to false.
- `ControllerEmu` tracks whether the default device is connected on every
  call to `UpdateReferences()`.
- `GCPadEmu.GetStatus()` now sets err bit to `PAD_ERR_NO_CONTROLLER` if
  the default device isn't connected.
- `SIDevice_GCController` handles `PAD_ERR_NO_CONTROLLER` by imitating the
  behaviour of `SIDevice_Null` (as far as I can tell, this is the only use
  of the error bit from `GCPadStatus`).

I wanted to add an OSD message akin to the ones when Wiimotes get
connected/disconnected, but I haven't yet found where to put the logic.
2017-11-19 16:07:00 +01:00

168 lines
3.8 KiB
C++

// Copyright 2013 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#pragma once
#include <memory>
#include <mutex>
#include <string>
#include <vector>
#include "Common/CommonTypes.h"
// idk in case I wanted to change it to double or something, idk what's best
typedef double ControlState;
namespace ciface
{
namespace Core
{
// Forward declarations
class DeviceQualifier;
//
// Device
//
// A device class
//
class Device
{
public:
class Input;
class Output;
//
// Control
//
// Control includes inputs and outputs
//
class Control // input or output
{
public:
virtual std::string GetName() const = 0;
virtual ~Control() {}
virtual Input* ToInput() { return nullptr; }
virtual Output* ToOutput() { return nullptr; }
};
//
// Input
//
// An input on a device
//
class Input : public Control
{
public:
// things like absolute axes/ absolute mouse position will override this
virtual bool IsDetectable() { return true; }
virtual ControlState GetState() const = 0;
Input* ToInput() override { return this; }
};
//
// Output
//
// An output on a device
//
class Output : public Control
{
public:
virtual ~Output() {}
virtual void SetState(ControlState state) = 0;
Output* ToOutput() override { return this; }
};
virtual ~Device();
int GetId() const { return m_id; }
void SetId(int id) { m_id = id; }
virtual std::string GetName() const = 0;
virtual std::string GetSource() const = 0;
virtual void UpdateInput() {}
virtual bool IsValid() const { return true; }
const std::vector<Input*>& Inputs() const { return m_inputs; }
const std::vector<Output*>& Outputs() const { return m_outputs; }
Input* FindInput(const std::string& name) const;
Output* FindOutput(const std::string& name) const;
protected:
void AddInput(Input* const i);
void AddOutput(Output* const o);
class FullAnalogSurface : public Input
{
public:
FullAnalogSurface(Input* low, Input* high) : m_low(*low), m_high(*high) {}
ControlState GetState() const override
{
return (1 + m_high.GetState() - m_low.GetState()) / 2;
}
std::string GetName() const override { return m_low.GetName() + *m_high.GetName().rbegin(); }
private:
Input& m_low;
Input& m_high;
};
void AddAnalogInputs(Input* low, Input* high)
{
AddInput(low);
AddInput(high);
AddInput(new FullAnalogSurface(low, high));
AddInput(new FullAnalogSurface(high, low));
}
private:
int m_id;
std::vector<Input*> m_inputs;
std::vector<Output*> m_outputs;
};
//
// DeviceQualifier
//
// Device qualifier used to match devices.
// Currently has ( source, id, name ) properties which match a device
//
class DeviceQualifier
{
public:
DeviceQualifier() : cid(-1) {}
DeviceQualifier(const std::string& _source, const int _id, const std::string& _name)
: source(_source), cid(_id), name(_name)
{
}
void FromDevice(const Device* const dev);
void FromString(const std::string& str);
std::string ToString() const;
bool operator==(const DeviceQualifier& devq) const;
bool operator!=(const DeviceQualifier& devq) const;
bool operator==(const Device* dev) const;
bool operator!=(const Device* dev) const;
std::string source;
int cid;
std::string name;
};
class DeviceContainer
{
public:
Device::Input* FindInput(const std::string& name, const Device* def_dev) const;
Device::Output* FindOutput(const std::string& name, const Device* def_dev) const;
std::vector<std::string> GetAllDeviceStrings() const;
std::string GetDefaultDeviceString() const;
std::shared_ptr<Device> FindDevice(const DeviceQualifier& devq) const;
bool HasConnectedDevice(const DeviceQualifier& qualifier) const;
protected:
mutable std::mutex m_devices_mutex;
std::vector<std::shared_ptr<Device>> m_devices;
};
}
}