Update to v094r21 release.

byuu says:

This updates ruby to return shared_pointer<HID::Device> objects instead
of HID::Device* objects. It also fixes an ID bug where joypads were
starting at ID# 2+, but mice were also set to ID# 2. I also revised
nall/hid a lot, with getters and setters instead of stabbing at internal
state. I didn't yet patch nall::string to safely consume nullptr const
char* values, though.
This commit is contained in:
Tim Allen 2015-05-24 19:44:28 +10:00
parent 4e0223d590
commit 99b2b4b57c
17 changed files with 289 additions and 302 deletions

View File

@ -3,7 +3,7 @@
namespace Emulator { namespace Emulator {
static const char Name[] = "higan"; static const char Name[] = "higan";
static const char Version[] = "094.20"; static const char Version[] = "094.21";
static const char Author[] = "byuu"; static const char Author[] = "byuu";
static const char License[] = "GPLv3"; static const char License[] = "GPLv3";
static const char Website[] = "http://byuu.org/"; static const char Website[] = "http://byuu.org/";

View File

@ -1,120 +1,110 @@
#ifndef NALL_HID_HPP #ifndef NALL_HID_HPP
#define NALL_HID_HPP #define NALL_HID_HPP
namespace nall { namespace nall { namespace HID {
namespace HID { struct Input {
struct Input { Input(const string& name) : _name(name) {}
string name;
int16_t value = 0;
Input() {} auto name() const -> string { return _name; }
Input(const string& name) : name(name) {} auto value() const -> int16_t { return _value; }
}; auto setValue(int16_t value) -> void { _value = value; }
struct Group { private:
string name; string _name;
vector<Input> input; int16_t _value = 0;
friend class Group;
};
Group() {} struct Group : vector<Input> {
Group(const string& name) : name(name) {} Group(const string& name) : _name(name) {}
void append(const string& name) { auto name() const -> string { return _name; }
input.append({name}); auto input(unsigned id) -> Input& { return operator[](id); }
auto append(const string& name) -> void { vector::append({name}); }
auto find(const string& name) const -> maybe<unsigned> {
for(auto id : range(size())) {
if(operator[](id)._name == name) return id;
} }
return nothing;
}
maybe<unsigned> find(const string& name) { private:
for(unsigned id = 0; id < input.size(); id++) { string _name;
if(input[id].name == name) return id; friend class Device;
} };
return nothing;
struct Device : vector<Group> {
Device(const string& name) : _name(name) {}
auto pathID() const -> uint32_t { return (uint32_t)(_id >> 32); }
auto deviceID() const -> uint32_t { return (uint32_t)(_id >> 0); }
auto vendorID() const -> uint16_t { return (uint16_t)(_id >> 16); }
auto productID() const -> uint16_t { return (uint16_t)(_id >> 0); }
virtual auto isNull() const -> bool { return false; }
virtual auto isKeyboard() const -> bool { return false; }
virtual auto isMouse() const -> bool { return false; }
virtual auto isJoypad() const -> bool { return false; }
auto name() const -> string { return _name; }
auto id() const -> uint64_t { return _id; }
auto setID(uint64_t id) -> void { _id = id; }
auto group(unsigned id) -> Group& { return operator[](id); }
auto append(const string& name) -> void { vector::append({name}); }
auto find(const string& name) const -> maybe<unsigned> {
for(auto id : range(size())) {
if(operator[](id)._name == name) return id;
} }
}; return nothing;
}
struct Device { private:
uint64_t id = 0; string _name;
string name; uint64_t _id = 0;
vector<Group> group; };
uint32_t pathID() const { return (uint32_t)(id >> 32); } struct Null : Device {
uint32_t deviceID() const { return (uint32_t)(id >> 0); } Null() : Device("Null") {}
uint16_t vendorID() const { return (uint16_t)(id >> 16); } auto isNull() const -> bool { return true; }
uint16_t productID() const { return (uint16_t)(id >> 0); } };
virtual bool isNull() const { return false; } struct Keyboard : Device {
virtual bool isKeyboard() const { return false; } enum GroupID : unsigned { Button };
virtual bool isMouse() const { return false; }
virtual bool isJoypad() const { return false; }
void append(const string& name) { Keyboard() : Device("Keyboard") { append("Button"); }
group.append({name}); auto isKeyboard() const -> bool { return true; }
} auto buttons() -> Group& { return group(GroupID::Button); }
};
maybe<unsigned> find(const string& name) { struct Mouse : Device {
for(unsigned id = 0; id < group.size(); id++) { enum GroupID : unsigned { Axis, Button };
if(group[id].name == name) return id;
}
return nothing;
}
};
struct Null : Device { Mouse() : Device("Mouse") { append("Axis"), append("Button"); }
Null() { auto isMouse() const -> bool { return true; }
name = "Null"; auto axes() -> Group& { return group(GroupID::Axis); }
} auto buttons() -> Group& { return group(GroupID::Button); }
};
bool isNull() const { return true; } struct Joypad : Device {
}; enum GroupID : unsigned { Axis, Hat, Trigger, Button };
struct Keyboard : Device { Joypad() : Device("Joypad") { append("Axis"), append("Hat"), append("Trigger"), append("Button"); }
enum GroupID : unsigned { Button }; auto isJoypad() const -> bool { return true; }
auto axes() -> Group& { return group(GroupID::Axis); }
auto hats() -> Group& { return group(GroupID::Hat); }
auto triggers() -> Group& { return group(GroupID::Trigger); }
auto buttons() -> Group& { return group(GroupID::Button); }
Group& button() { return group[GroupID::Button]; } auto rumble() const -> bool { return _rumble; }
auto setRumble(bool rumble) -> void { _rumble = rumble; }
Keyboard() { private:
name = "Keyboard"; bool _rumble = false;
append("Button"); };
}
bool isKeyboard() const { return true; } }}
};
struct Mouse : Device {
enum GroupID : unsigned { Axis, Button };
Group& axis() { return group[GroupID::Axis]; }
Group& button() { return group[GroupID::Button]; }
Mouse() {
name = "Mouse";
append("Axis");
append("Button");
}
bool isMouse() const { return true; }
};
struct Joypad : Device {
enum GroupID : unsigned { Axis, Hat, Trigger, Button };
Group& axis() { return group[GroupID::Axis]; }
Group& hat() { return group[GroupID::Hat]; }
Group& trigger() { return group[GroupID::Trigger]; }
Group& button() { return group[GroupID::Button]; }
bool rumble = false;
Joypad() {
name = "Joypad";
append("Axis");
append("Hat");
append("Trigger");
append("Button");
}
bool isJoypad() const { return true; }
};
}
}
#endif #endif

View File

@ -130,21 +130,21 @@ using namespace nall;
#define DeclareInput(Name) \ #define DeclareInput(Name) \
struct Input##Name : Input { \ struct Input##Name : Input { \
bool cap(const string& name) { return p.cap(name); } \
any get(const string& name) { return p.get(name); } \
bool set(const string& name, const any& value) { return p.set(name, value); } \
\
bool acquire() { return p.acquire(); } \
bool unacquire() { return p.unacquire(); } \
bool acquired() { return p.acquired(); } \
\
vector<HID::Device*> poll() { return p.poll(); } \
bool rumble(uint64_t id, bool enable) { return p.rumble(id, enable); } \
bool init() { return p.init(); } \
void term() { p.term(); } \
\
Input##Name() : p(*new pInput##Name) {} \ Input##Name() : p(*new pInput##Name) {} \
~Input##Name() { delete &p; } \ ~Input##Name() { delete &p; } \
\
auto cap(const string& name) -> bool { return p.cap(name); } \
auto get(const string& name) -> any { return p.get(name); } \
auto set(const string& name, const any& value) -> bool { return p.set(name, value); } \
\
auto acquire() -> bool { return p.acquire(); } \
auto unacquire() -> bool { return p.unacquire(); } \
auto acquired() -> bool { return p.acquired(); } \
\
auto poll() -> vector<shared_pointer<HID::Device>> { return p.poll(); } \
auto rumble(uint64_t id, bool enable) -> bool { return p.rumble(id, enable); } \
auto init() -> bool { return p.init(); } \
auto term() -> void { p.term(); } \
\ \
private: \ private: \
pInput##Name& p; \ pInput##Name& p; \

View File

@ -1,23 +1,23 @@
struct Input { struct Input {
static const char* Handle; static const nall::string Handle;
static const char* KeyboardSupport; static const nall::string KeyboardSupport;
static const char* MouseSupport; static const nall::string MouseSupport;
static const char* JoypadSupport; static const nall::string JoypadSupport;
static const char* JoypadRumbleSupport; static const nall::string JoypadRumbleSupport;
virtual bool cap(const nall::string& name) { return false; } Input() = default;
virtual nall::any get(const nall::string& name) { return false; } virtual ~Input() = default;
virtual bool set(const nall::string& name, const nall::any& value) { return false; }
virtual bool acquire() { return false; } virtual auto cap(const nall::string& name) -> bool { return false; }
virtual bool unacquire() { return false; } virtual auto get(const nall::string& name) -> nall::any { return false; }
virtual bool acquired() { return false; } virtual auto set(const nall::string& name, const nall::any& value) -> bool { return false; }
virtual nall::vector<nall::HID::Device*> poll() { return {}; } virtual auto acquire() -> bool { return false; }
virtual bool rumble(uint64_t id, bool enable) { return false; } virtual auto unacquire() -> bool { return false; }
virtual bool init() { return true; } virtual auto acquired() -> bool { return false; }
virtual void term() {}
Input() {} virtual auto poll() -> nall::vector<nall::shared_pointer<nall::HID::Device>> { return {}; }
virtual ~Input() {} virtual auto rumble(uint64_t id, bool enable) -> bool { return false; }
virtual auto init() -> bool { return true; }
virtual auto term() -> void {}
}; };

View File

@ -5,48 +5,47 @@ namespace ruby {
struct InputJoypadSDL { struct InputJoypadSDL {
struct Joypad { struct Joypad {
HID::Joypad hid; shared_pointer<HID::Joypad> hid{new HID::Joypad};
unsigned id = 0; unsigned id = 0;
SDL_Joystick* handle = nullptr; SDL_Joystick* handle = nullptr;
}; };
vector<Joypad> joypads; vector<Joypad> joypads;
void assign(HID::Joypad& hid, unsigned groupID, unsigned inputID, int16_t value) { auto assign(shared_pointer<HID::Joypad> hid, unsigned groupID, unsigned inputID, int16_t value) -> void {
auto& group = hid.group[groupID]; auto& group = hid->group(groupID);
if(group.input[inputID].value == value) return; if(group.input(inputID).value() == value) return;
if(input.onChange) input.onChange(hid, groupID, inputID, group.input[inputID].value, value); if(input.onChange) input.onChange(hid, groupID, inputID, group.input(inputID).value(), value);
group.input[inputID].value = value; group.input(inputID).setValue(value);
} }
void poll(vector<HID::Device*>& devices) { auto poll(vector<shared_pointer<HID::Device>>& devices) -> void {
SDL_JoystickUpdate(); SDL_JoystickUpdate();
for(auto& jp : joypads) { for(auto& jp : joypads) {
for(unsigned n = 0; n < jp.hid.axis().input.size(); n++) { for(auto n : range(jp.hid->axes())) {
assign(jp.hid, HID::Joypad::GroupID::Axis, n, (int16_t)SDL_JoystickGetAxis(jp.handle, n)); assign(jp.hid, HID::Joypad::GroupID::Axis, n, (int16_t)SDL_JoystickGetAxis(jp.handle, n));
} }
for(signed n = 0; n < (signed)jp.hid.hat().input.size() - 1; n += 2) { for(signed n = 0; n < (signed)jp.hid->hats().size() - 1; n += 2) {
uint8_t state = SDL_JoystickGetHat(jp.handle, n >> 1); uint8_t state = SDL_JoystickGetHat(jp.handle, n >> 1);
assign(jp.hid, HID::Joypad::GroupID::Hat, n + 0, state & SDL_HAT_LEFT ? -32768 : state & SDL_HAT_RIGHT ? +32767 : 0); assign(jp.hid, HID::Joypad::GroupID::Hat, n + 0, state & SDL_HAT_LEFT ? -32768 : state & SDL_HAT_RIGHT ? +32767 : 0);
assign(jp.hid, HID::Joypad::GroupID::Hat, n + 1, state & SDL_HAT_UP ? -32768 : state & SDL_HAT_DOWN ? +32767 : 0); assign(jp.hid, HID::Joypad::GroupID::Hat, n + 1, state & SDL_HAT_UP ? -32768 : state & SDL_HAT_DOWN ? +32767 : 0);
} }
for(unsigned n = 0; n < jp.hid.button().input.size(); n++) { for(auto n : range(jp.hid->buttons())) {
assign(jp.hid, HID::Joypad::GroupID::Button, n, (bool)SDL_JoystickGetButton(jp.handle, n)); assign(jp.hid, HID::Joypad::GroupID::Button, n, (bool)SDL_JoystickGetButton(jp.handle, n));
} }
devices.append(&jp.hid); devices.append(jp.hid);
} }
} }
bool init() { auto init() -> bool {
SDL_InitSubSystem(SDL_INIT_JOYSTICK); SDL_InitSubSystem(SDL_INIT_JOYSTICK);
SDL_JoystickEventState(SDL_IGNORE); SDL_JoystickEventState(SDL_IGNORE);
unsigned joypadCount = SDL_NumJoysticks(); for(auto id : range(SDL_NumJoysticks())) {
for(unsigned id = 0; id < joypadCount; id++) {
Joypad jp; Joypad jp;
jp.id = id; jp.id = id;
jp.handle = SDL_JoystickOpen(id); jp.handle = SDL_JoystickOpen(id);
@ -55,11 +54,11 @@ struct InputJoypadSDL {
unsigned hats = SDL_JoystickNumHats(jp.handle) * 2; unsigned hats = SDL_JoystickNumHats(jp.handle) * 2;
unsigned buttons = 32; //there is no SDL_JoystickNumButtons() unsigned buttons = 32; //there is no SDL_JoystickNumButtons()
jp.hid.id = 2 + jp.id; jp.hid->setID(3 + jp.id);
for(unsigned n = 0; n < axes; n++) jp.hid.axis().append({n}); for(auto n : range(axes)) jp.hid->axes().append(n);
for(unsigned n = 0; n < hats; n++) jp.hid.hat().append({n}); for(auto n : range(hats)) jp.hid->hats().append(n);
for(unsigned n = 0; n < buttons; n++) jp.hid.button().append({n}); for(auto n : range(buttons)) jp.hid->buttons().append(n);
jp.hid.rumble = false; jp.hid->setRumble(false);
joypads.append(jp); joypads.append(jp);
} }
@ -67,7 +66,7 @@ struct InputJoypadSDL {
return true; return true;
} }
void term() { auto term() -> void {
for(auto& jp : joypads) { for(auto& jp : joypads) {
SDL_JoystickClose(jp.handle); SDL_JoystickClose(jp.handle);
} }

View File

@ -4,7 +4,7 @@
namespace ruby { namespace ruby {
struct InputKeyboardXlib { struct InputKeyboardXlib {
HID::Keyboard hid; shared_pointer<HID::Keyboard> hid{new HID::Keyboard};
Display* display = nullptr; Display* display = nullptr;
@ -15,14 +15,14 @@ struct InputKeyboardXlib {
}; };
vector<Key> keys; vector<Key> keys;
void assign(unsigned inputID, bool value) { auto assign(unsigned inputID, bool value) -> void {
auto& group = hid.group[HID::Keyboard::GroupID::Button]; auto& group = hid->buttons();
if(group.input[inputID].value == value) return; if(group.input(inputID).value() == value) return;
if(input.onChange) input.onChange(hid, HID::Keyboard::GroupID::Button, inputID, group.input[inputID].value, value); if(input.onChange) input.onChange(hid, HID::Keyboard::GroupID::Button, inputID, group.input(inputID).value(), value);
group.input[inputID].value = value; group.input(inputID).setValue(value);
} }
void poll(vector<HID::Device*>& devices) { auto poll(vector<shared_pointer<HID::Device>>& devices) -> void {
char state[32]; char state[32];
XQueryKeymap(display, state); XQueryKeymap(display, state);
@ -31,10 +31,10 @@ struct InputKeyboardXlib {
assign(n, value); assign(n, value);
} }
devices.append(&hid); devices.append(hid);
} }
bool init() { auto init() -> bool {
display = XOpenDisplay(0); display = XOpenDisplay(0);
keys.append({"Escape", XK_Escape}); keys.append({"Escape", XK_Escape});
@ -151,17 +151,17 @@ struct InputKeyboardXlib {
keys.append({"RightSuper", XK_Super_R}); keys.append({"RightSuper", XK_Super_R});
keys.append({"Menu", XK_Menu}); keys.append({"Menu", XK_Menu});
hid.id = 1; hid->setID(1);
for(unsigned n = 0; n < keys.size(); n++) { for(unsigned n = 0; n < keys.size(); n++) {
hid.button().append(keys[n].name); hid->buttons().append(keys[n].name);
keys[n].keycode = XKeysymToKeycode(display, keys[n].keysym); keys[n].keycode = XKeysymToKeycode(display, keys[n].keysym);
} }
return true; return true;
} }
void term() { auto term() -> void {
if(display) { if(display) {
XCloseDisplay(display); XCloseDisplay(display);
display = nullptr; display = nullptr;

View File

@ -4,7 +4,7 @@
namespace ruby { namespace ruby {
struct InputMouseXlib { struct InputMouseXlib {
HID::Mouse hid; shared_pointer<HID::Mouse> hid{new HID::Mouse};
uintptr_t handle = 0; uintptr_t handle = 0;
@ -23,7 +23,7 @@ struct InputMouseXlib {
unsigned relativeY = 0; unsigned relativeY = 0;
} ms; } ms;
bool acquire() { auto acquire() -> bool {
if(acquired()) return true; if(acquired()) return true;
if(XGrabPointer(display, handle, True, 0, GrabModeAsync, GrabModeAsync, rootWindow, invisibleCursor, CurrentTime) == GrabSuccess) { if(XGrabPointer(display, handle, True, 0, GrabModeAsync, GrabModeAsync, rootWindow, invisibleCursor, CurrentTime) == GrabSuccess) {
@ -42,7 +42,7 @@ struct InputMouseXlib {
} }
} }
bool unacquire() { auto unacquire() -> bool {
if(acquired()) { if(acquired()) {
//restore cursor acceleration and release cursor //restore cursor acceleration and release cursor
XChangePointerControl(display, True, True, ms.numerator, ms.denominator, ms.threshold); XChangePointerControl(display, True, True, ms.numerator, ms.denominator, ms.threshold);
@ -52,18 +52,18 @@ struct InputMouseXlib {
return true; return true;
} }
bool acquired() { auto acquired() -> bool {
return ms.acquired; return ms.acquired;
} }
void assign(unsigned groupID, unsigned inputID, int16_t value) { auto assign(unsigned groupID, unsigned inputID, int16_t value) -> void {
auto& group = hid.group[groupID]; auto& group = hid->group(groupID);
if(group.input[inputID].value == value) return; if(group.input(inputID).value() == value) return;
if(input.onChange) input.onChange(hid, groupID, inputID, group.input[inputID].value, value); if(input.onChange) input.onChange(hid, groupID, inputID, group.input(inputID).value(), value);
group.input[inputID].value = value; group.input(inputID).setValue(value);
} }
void poll(vector<HID::Device*>& devices) { auto poll(vector<shared_pointer<HID::Device>>& devices) -> void {
Window rootReturn; Window rootReturn;
Window childReturn; Window childReturn;
signed rootXReturn = 0; signed rootXReturn = 0;
@ -81,7 +81,7 @@ struct InputMouseXlib {
assign(HID::Mouse::GroupID::Axis, 0, (int16_t)(rootXReturn - screenWidth / 2)); assign(HID::Mouse::GroupID::Axis, 0, (int16_t)(rootXReturn - screenWidth / 2));
assign(HID::Mouse::GroupID::Axis, 1, (int16_t)(rootYReturn - screenHeight / 2)); assign(HID::Mouse::GroupID::Axis, 1, (int16_t)(rootYReturn - screenHeight / 2));
if(hid.axis().input[0].value != 0 || hid.axis().input[1].value != 0) { if(hid->axes().input(0).value() != 0 || hid->axes().input(1).value() != 0) {
//if mouse moved, re-center mouse for next poll //if mouse moved, re-center mouse for next poll
XWarpPointer(display, None, rootWindow, 0, 0, 0, 0, screenWidth / 2, screenHeight / 2); XWarpPointer(display, None, rootWindow, 0, 0, 0, 0, screenWidth / 2, screenHeight / 2);
} }
@ -99,10 +99,10 @@ struct InputMouseXlib {
assign(HID::Mouse::GroupID::Button, 3, (bool)(maskReturn & Button4Mask)); assign(HID::Mouse::GroupID::Button, 3, (bool)(maskReturn & Button4Mask));
assign(HID::Mouse::GroupID::Button, 4, (bool)(maskReturn & Button5Mask)); assign(HID::Mouse::GroupID::Button, 4, (bool)(maskReturn & Button5Mask));
devices.append(&hid); devices.append(hid);
} }
bool init(uintptr_t handle) { auto init(uintptr_t handle) -> bool {
this->handle = handle; this->handle = handle;
display = XOpenDisplay(0); display = XOpenDisplay(0);
rootWindow = DefaultRootWindow(display); rootWindow = DefaultRootWindow(display);
@ -128,21 +128,21 @@ struct InputMouseXlib {
ms.relativeX = 0; ms.relativeX = 0;
ms.relativeY = 0; ms.relativeY = 0;
hid.id = 2; hid->setID(2);
hid.axis().append({"X"}); hid->axes().append("X");
hid.axis().append({"Y"}); hid->axes().append("Y");
hid.button().append({"Left"}); hid->buttons().append("Left");
hid.button().append({"Middle"}); hid->buttons().append("Middle");
hid.button().append({"Right"}); hid->buttons().append("Right");
hid.button().append({"Up"}); hid->buttons().append("Up");
hid.button().append({"Down"}); hid->buttons().append("Down");
return true; return true;
} }
void term() { auto term() -> void {
unacquire(); unacquire();
XFreeCursor(display, invisibleCursor); XFreeCursor(display, invisibleCursor);
XCloseDisplay(display); XCloseDisplay(display);

View File

@ -17,7 +17,7 @@ struct pInputSDL {
uintptr_t handle = 0; uintptr_t handle = 0;
} settings; } settings;
bool cap(const string& name) { auto cap(const string& name) -> bool {
if(name == Input::Handle) return true; if(name == Input::Handle) return true;
if(name == Input::KeyboardSupport) return true; if(name == Input::KeyboardSupport) return true;
if(name == Input::MouseSupport) return true; if(name == Input::MouseSupport) return true;
@ -25,12 +25,12 @@ struct pInputSDL {
return false; return false;
} }
any get(const string& name) { auto get(const string& name) -> any {
if(name == Input::Handle) return (uintptr_t)settings.handle; if(name == Input::Handle) return (uintptr_t)settings.handle;
return false; return false;
} }
bool set(const string& name, const any &value) { auto set(const string& name, const any& value) -> bool {
if(name == Input::Handle) { if(name == Input::Handle) {
settings.handle = any_cast<uintptr_t>(value); settings.handle = any_cast<uintptr_t>(value);
return true; return true;
@ -39,38 +39,38 @@ struct pInputSDL {
return false; return false;
} }
bool acquire() { auto acquire() -> bool {
return xlibMouse.acquire(); return xlibMouse.acquire();
} }
bool unacquire() { auto unacquire() -> bool {
return xlibMouse.unacquire(); return xlibMouse.unacquire();
} }
bool acquired() { auto acquired() -> bool {
return xlibMouse.acquired(); return xlibMouse.acquired();
} }
vector<HID::Device*> poll() { auto poll() -> vector<shared_pointer<HID::Device>> {
vector<HID::Device*> devices; vector<shared_pointer<HID::Device>> devices;
xlibKeyboard.poll(devices); xlibKeyboard.poll(devices);
xlibMouse.poll(devices); xlibMouse.poll(devices);
sdl.poll(devices); sdl.poll(devices);
return devices; return devices;
} }
bool rumble(uint64_t id, bool enable) { auto rumble(uint64_t id, bool enable) -> bool {
return false; return false;
} }
bool init() { auto init() -> bool {
if(xlibKeyboard.init() == false) return false; if(!xlibKeyboard.init()) return false;
if(xlibMouse.init(settings.handle) == false) return false; if(!xlibMouse.init(settings.handle)) return false;
if(sdl.init() == false) return false; if(!sdl.init()) return false;
return true; return true;
} }
void term() { auto term() -> void {
xlibKeyboard.term(); xlibKeyboard.term();
xlibMouse.term(); xlibMouse.term();
sdl.term(); sdl.term();

View File

@ -17,18 +17,18 @@ struct pInputXlib {
uintptr_t handle = 0; uintptr_t handle = 0;
} settings; } settings;
bool cap(const string& name) { auto cap(const string& name) -> bool {
if(name == Input::KeyboardSupport) return true; if(name == Input::KeyboardSupport) return true;
if(name == Input::MouseSupport) return true; if(name == Input::MouseSupport) return true;
return false; return false;
} }
any get(const string& name) { auto get(const string& name) -> any {
if(name == Input::Handle) return (uintptr_t)settings.handle; if(name == Input::Handle) return (uintptr_t)settings.handle;
return false; return false;
} }
bool set(const string& name, const any &value) { auto set(const string& name, const any& value) -> bool {
if(name == Input::Handle) { if(name == Input::Handle) {
settings.handle = any_cast<uintptr_t>(value); settings.handle = any_cast<uintptr_t>(value);
return true; return true;
@ -37,36 +37,36 @@ struct pInputXlib {
return false; return false;
} }
bool acquire() { auto acquire() -> bool {
return xlibMouse.acquire(); return xlibMouse.acquire();
} }
bool unacquire() { auto unacquire() -> bool {
return xlibMouse.unacquire(); return xlibMouse.unacquire();
} }
bool acquired() { auto acquired() -> bool {
return xlibMouse.acquired(); return xlibMouse.acquired();
} }
vector<HID::Device*> poll() { auto poll() -> vector<shared_pointer<HID::Device>> {
vector<HID::Device*> devices; vector<shared_pointer<HID::Device>> devices;
xlibKeyboard.poll(devices); xlibKeyboard.poll(devices);
xlibMouse.poll(devices); xlibMouse.poll(devices);
return devices; return devices;
} }
bool rumble(uint64_t id, bool enable) { auto rumble(uint64_t id, bool enable) -> bool {
return false; return false;
} }
bool init() { auto init() -> bool {
if(xlibKeyboard.init() == false) return false; if(!xlibKeyboard.init()) return false;
if(xlibMouse.init(settings.handle) == false) return false; if(!xlibMouse.init(settings.handle)) return false;
return true; return true;
} }
void term() { auto term() -> void {
xlibKeyboard.term(); xlibKeyboard.term();
xlibMouse.term(); xlibMouse.term();
} }

View File

@ -363,43 +363,43 @@ AudioInterface::~AudioInterface() { term(); }
/* InputInterface */ /* InputInterface */
const char* Input::Handle = "Handle"; const string Input::Handle = "Handle";
const char* Input::KeyboardSupport = "KeyboardSupport"; const string Input::KeyboardSupport = "KeyboardSupport";
const char* Input::MouseSupport = "MouseSupport"; const string Input::MouseSupport = "MouseSupport";
const char* Input::JoypadSupport = "JoypadSupport"; const string Input::JoypadSupport = "JoypadSupport";
const char* Input::JoypadRumbleSupport = "JoypadRumbleSupport"; const string Input::JoypadRumbleSupport = "JoypadRumbleSupport";
void InputInterface::driver(const char* driver) { auto InputInterface::driver(string driver) -> void {
if(p) term(); if(p) term();
if(!driver || !*driver) driver = optimalDriver(); if(!driver) driver = optimalDriver();
if(0); if(0);
#ifdef INPUT_WINDOWS #ifdef INPUT_WINDOWS
else if(!strcmp(driver, "Windows")) p = new InputWindows(); else if(driver == "Windows") p = new InputWindows();
#endif #endif
#ifdef INPUT_CARBON #ifdef INPUT_CARBON
else if(!strcmp(driver, "Carbon")) p = new InputCarbon(); else if(driver == "Carbon") p = new InputCarbon();
#endif #endif
#ifdef INPUT_UDEV #ifdef INPUT_UDEV
else if(!strcmp(driver, "udev")) p = new InputUdev(); else if(driver == "udev") p = new InputUdev();
#endif #endif
#ifdef INPUT_SDL #ifdef INPUT_SDL
else if(!strcmp(driver, "SDL")) p = new InputSDL(); else if(driver == "SDL") p = new InputSDL();
#endif #endif
#ifdef INPUT_XLIB #ifdef INPUT_XLIB
else if(!strcmp(driver, "Xlib")) p = new InputXlib(); else if(driver == "Xlib") p = new InputXlib();
#endif #endif
else p = new Input(); else p = new Input();
} }
const char* InputInterface::optimalDriver() { auto InputInterface::optimalDriver() -> string {
#if defined(INPUT_WINDOWS) #if defined(INPUT_WINDOWS)
return "Windows"; return "Windows";
@ -418,7 +418,7 @@ const char* InputInterface::optimalDriver() {
#endif #endif
} }
const char* InputInterface::safestDriver() { auto InputInterface::safestDriver() -> string {
#if defined(INPUT_WINDOWS) #if defined(INPUT_WINDOWS)
return "Windows"; return "Windows";
@ -437,7 +437,7 @@ const char* InputInterface::safestDriver() {
#endif #endif
} }
const char* InputInterface::availableDrivers() { auto InputInterface::availableDrivers() -> string {
return return
//Windows //Windows
@ -469,12 +469,12 @@ const char* InputInterface::availableDrivers() {
"None"; "None";
} }
bool InputInterface::init() { auto InputInterface::init() -> bool {
if(!p) driver(); if(!p) driver();
return p->init(); return p->init();
} }
void InputInterface::term() { auto InputInterface::term() -> void {
if(p) { if(p) {
p->term(); p->term();
delete p; delete p;
@ -482,15 +482,14 @@ void InputInterface::term() {
} }
} }
bool InputInterface::cap(const string& name) { return p ? p->cap(name) : false; }
any InputInterface::get(const string& name) { return p ? p->get(name) : false; }
bool InputInterface::set(const string& name, const any& value) { return p ? p->set(name, value) : false; }
bool InputInterface::acquire() { return p ? p->acquire() : false; }
bool InputInterface::unacquire() { return p ? p->unacquire() : false; }
bool InputInterface::acquired() { return p ? p->acquired() : false; }
vector<HID::Device*> InputInterface::poll() { return p ? p->poll() : vector<HID::Device*>(); }
bool InputInterface::rumble(uint64_t id, bool enable) { return p ? p->rumble(id, enable) : false; }
InputInterface::InputInterface() : p(nullptr) {}
InputInterface::~InputInterface() { term(); } InputInterface::~InputInterface() { term(); }
auto InputInterface::cap(const string& name) -> bool { return p ? p->cap(name) : false; }
auto InputInterface::get(const string& name) -> any { return p ? p->get(name) : false; }
auto InputInterface::set(const string& name, const any& value) -> bool { return p ? p->set(name, value) : false; }
auto InputInterface::acquire() -> bool { return p ? p->acquire() : false; }
auto InputInterface::unacquire() -> bool { return p ? p->unacquire() : false; }
auto InputInterface::acquired() -> bool { return p ? p->acquired() : false; }
auto InputInterface::poll() -> vector<shared_pointer<HID::Device>> { return p ? p->poll() : vector<shared_pointer<HID::Device>>(); }
auto InputInterface::rumble(uint64_t id, bool enable) -> bool { return p ? p->rumble(id, enable) : false; }
}; };

View File

@ -1,14 +1,14 @@
/* ruby /* ruby
* author: byuu * author: byuu
* license: ISC * license: ISC
* version: 0.11 (2013-12-19) * version: 0.12 (2015-05-24)
* *
* ruby is a cross-platform hardware abstraction layer * ruby is a cross-platform hardware abstraction layer
* it provides a common interface to video, audio and input devices * it provides a common interface to video, audio and input devices
*/ */
#ifndef RUBY_H #ifndef RUBY_HPP
#define RUBY_H #define RUBY_HPP
#include <nall/nall.hpp> #include <nall/nall.hpp>
@ -65,29 +65,28 @@ private:
}; };
struct InputInterface { struct InputInterface {
nall::function<void (nall::HID::Device& device, unsigned group, unsigned input, int16_t oldValue, int16_t newValue)> onChange; nall::function<void (nall::shared_pointer<nall::HID::Device> device, unsigned group, unsigned input, int16_t oldValue, int16_t newValue)> onChange;
void driver(const char* driver = "");
const char* optimalDriver();
const char* safestDriver();
const char* availableDrivers();
bool init();
void term();
bool cap(const nall::string& name);
nall::any get(const nall::string& name);
bool set(const nall::string& name, const nall::any& value);
bool acquire();
bool unacquire();
bool acquired();
nall::vector<nall::HID::Device*> poll();
bool rumble(uint64_t id, bool enable);
InputInterface();
~InputInterface(); ~InputInterface();
auto driver(nall::string driver = "") -> void;
auto optimalDriver() -> nall::string;
auto safestDriver() -> nall::string;
auto availableDrivers() -> nall::string;
auto init() -> bool;
auto term() -> void;
auto cap(const nall::string& name) -> bool;
auto get(const nall::string& name) -> nall::any;
auto set(const nall::string& name, const nall::any& value) -> bool;
auto acquire() -> bool;
auto unacquire() -> bool;
auto acquired() -> bool;
auto poll() -> nall::vector<nall::shared_pointer<nall::HID::Device>>;
auto rumble(uint64_t id, bool enable) -> bool;
private: private:
Input* p = nullptr; Input* p = nullptr;
}; };

View File

@ -14,19 +14,19 @@ ui_objects += ruby hiro
# platform # platform
ifeq ($(platform),windows) ifeq ($(platform),windows)
ruby := video.direct3d video.wgl video.directdraw video.gdi ruby += video.direct3d video.wgl video.directdraw video.gdi
ruby += audio.xaudio2 audio.directsound ruby += audio.xaudio2 audio.directsound
ruby += input.windows ruby += input.windows
else ifeq ($(platform),macosx) else ifeq ($(platform),macosx)
ruby := video.cgl ruby += video.cgl
ruby += audio.openal ruby += audio.openal
ruby += input.carbon ruby += input.carbon
else ifeq ($(platform),linux) else ifeq ($(platform),linux)
ruby := video.glx video.xv video.xshm video.sdl ruby += video.glx video.xv video.xshm video.sdl
ruby += audio.alsa audio.openal audio.oss audio.pulseaudio audio.pulseaudiosimple audio.ao ruby += audio.alsa audio.openal audio.oss audio.pulseaudio audio.pulseaudiosimple audio.ao
ruby += input.udev input.sdl input.xlib ruby += input.sdl input.xlib #input.udev
else ifeq ($(platform),bsd) else ifeq ($(platform),bsd)
ruby := video.glx video.xv video.xshm video.sdl ruby += video.glx video.xv video.xshm video.sdl
ruby += audio.openal audio.oss ruby += audio.openal audio.oss
ruby += input.sdl input.xlib ruby += input.sdl input.xlib
endif endif

View File

@ -5,13 +5,13 @@ InputManager* inputManager = nullptr;
auto InputMapping::bind() -> void { auto InputMapping::bind() -> void {
auto token = assignment.split("/"); auto token = assignment.split("/");
if(token.size() < 3) return unbind(); if(token.size() < 3) return unbind();
uint64_t id = hex(token[0]); uint64_t id = token[0].decimal();
unsigned group = token[1].decimal(); unsigned group = token[1].decimal();
unsigned input = token[2].decimal(); unsigned input = token[2].decimal();
string qualifier = token(3, "None"); string qualifier = token(3, "None");
for(auto& device : inputManager->devices) { for(auto& device : inputManager->devices) {
if(id != device->id) continue; if(id != device->id()) continue;
this->device = device; this->device = device;
this->group = group; this->group = group;
@ -24,25 +24,25 @@ auto InputMapping::bind() -> void {
} }
} }
auto InputMapping::bind(HID::Device& device, unsigned group, unsigned input, int16 oldValue, int16 newValue) -> bool { auto InputMapping::bind(shared_pointer<HID::Device> device, unsigned group, unsigned input, int16 oldValue, int16 newValue) -> bool {
if(device.isNull() || (device.isKeyboard() && device.group[group].input[input].name == "Escape")) { if(device->isNull() || (device->isKeyboard() && device->group(group).input(input).name() == "Escape")) {
return unbind(), true; return unbind(), true;
} }
string encoding = {hex(device.id), "/", group, "/", input}; string encoding = {"0x", hex(device->id()), "/", group, "/", input};
if(isDigital()) { if(isDigital()) {
if((device.isKeyboard() && group == HID::Keyboard::GroupID::Button) if((device->isKeyboard() && group == HID::Keyboard::GroupID::Button)
|| (device.isMouse() && group == HID::Mouse::GroupID::Button) || (device->isMouse() && group == HID::Mouse::GroupID::Button)
|| (device.isJoypad() && group == HID::Joypad::GroupID::Button)) { || (device->isJoypad() && group == HID::Joypad::GroupID::Button)) {
if(newValue) { if(newValue) {
this->assignment = encoding; this->assignment = encoding;
return bind(), true; return bind(), true;
} }
} }
if((device.isJoypad() && group == HID::Joypad::GroupID::Axis) if((device->isJoypad() && group == HID::Joypad::GroupID::Axis)
|| (device.isJoypad() && group == HID::Joypad::GroupID::Hat)) { || (device->isJoypad() && group == HID::Joypad::GroupID::Hat)) {
if(newValue < -16384) { if(newValue < -16384) {
this->assignment = {encoding, "/Lo"}; this->assignment = {encoding, "/Lo"};
return bind(), true; return bind(), true;
@ -56,9 +56,9 @@ auto InputMapping::bind(HID::Device& device, unsigned group, unsigned input, int
} }
if(isAnalog()) { if(isAnalog()) {
if((device.isMouse() && group == HID::Mouse::GroupID::Axis) if((device->isMouse() && group == HID::Mouse::GroupID::Axis)
|| (device.isJoypad() && group == HID::Joypad::GroupID::Axis) || (device->isJoypad() && group == HID::Joypad::GroupID::Axis)
|| (device.isJoypad() && group == HID::Joypad::GroupID::Hat)) { || (device->isJoypad() && group == HID::Joypad::GroupID::Hat)) {
if(newValue < -16384 || newValue > +16384) { if(newValue < -16384 || newValue > +16384) {
this->assignment = encoding; this->assignment = encoding;
return bind(), true; return bind(), true;
@ -67,7 +67,7 @@ auto InputMapping::bind(HID::Device& device, unsigned group, unsigned input, int
} }
if(isRumble()) { if(isRumble()) {
if(device.isJoypad() && group == HID::Joypad::GroupID::Button) { if(device->isJoypad() && group == HID::Joypad::GroupID::Button) {
if(newValue) { if(newValue) {
encoding = {this->assignment, "/Rumble"}; encoding = {this->assignment, "/Rumble"};
return bind(), true; return bind(), true;
@ -80,7 +80,7 @@ auto InputMapping::bind(HID::Device& device, unsigned group, unsigned input, int
auto InputMapping::poll() -> int16 { auto InputMapping::poll() -> int16 {
if(!device) return 0; if(!device) return 0;
auto value = device->group[group].input[input].value; auto value = device->group(group).input(input).value();
if(isDigital()) { if(isDigital()) {
if(device->isKeyboard() && group == HID::Keyboard::GroupID::Button) return value != 0; if(device->isKeyboard() && group == HID::Keyboard::GroupID::Button) return value != 0;
@ -110,21 +110,21 @@ auto InputMapping::unbind() -> void {
this->qualifier = Qualifier::None; this->qualifier = Qualifier::None;
} }
auto InputMapping::assignmentName() const -> string { auto InputMapping::assignmentName() -> string {
if(!device) return "None"; if(!device) return "None";
string path; string path;
path.append(device->name); path.append(device->name());
path.append(".", device->group[group].name); path.append(".", device->group(group).name());
path.append(".", device->group[group].input[input].name); path.append(".", device->group(group).input(input).name());
if(qualifier == Qualifier::Lo) path.append(".Lo"); if(qualifier == Qualifier::Lo) path.append(".Lo");
if(qualifier == Qualifier::Hi) path.append(".Hi"); if(qualifier == Qualifier::Hi) path.append(".Hi");
if(qualifier == Qualifier::Rumble) path.append(".Rumble"); if(qualifier == Qualifier::Rumble) path.append(".Rumble");
return path; return path;
} }
auto InputMapping::deviceName() const -> string { auto InputMapping::deviceName() -> string {
if(!device) return ""; if(!device) return "";
return device->id; return hex(device->id());
} }
// //
@ -211,7 +211,7 @@ auto InputManager::poll() -> void {
if(presentation && presentation->focused()) pollHotkeys(); if(presentation && presentation->focused()) pollHotkeys();
} }
auto InputManager::onChange(HID::Device& device, unsigned group, unsigned input, int16 oldValue, int16 newValue) -> void { auto InputManager::onChange(shared_pointer<HID::Device> device, unsigned group, unsigned input, int16 oldValue, int16 newValue) -> void {
if(settingsManager->focused()) { if(settingsManager->focused()) {
settingsManager->input.inputEvent(device, group, input, oldValue, newValue); settingsManager->input.inputEvent(device, group, input, oldValue, newValue);
settingsManager->hotkeys.inputEvent(device, group, input, oldValue, newValue); settingsManager->hotkeys.inputEvent(device, group, input, oldValue, newValue);
@ -224,9 +224,9 @@ auto InputManager::quit() -> void {
hotkeys.reset(); hotkeys.reset();
} }
auto InputManager::findMouse() -> HID::Device* { auto InputManager::findMouse() -> shared_pointer<HID::Device> {
for(auto device : devices) { for(auto& device : devices) {
if(device->isMouse()) return device; if(device->isMouse()) return device;
} }
return nullptr; return {};
} }

View File

@ -1,6 +1,6 @@
struct InputMapping { struct InputMapping {
auto bind() -> void; auto bind() -> void;
auto bind(HID::Device& device, unsigned group, unsigned input, int16 oldValue, int16 newValue) -> bool; auto bind(shared_pointer<HID::Device> device, unsigned group, unsigned input, int16 oldValue, int16 newValue) -> bool;
auto poll() -> int16; auto poll() -> int16;
auto unbind() -> void; auto unbind() -> void;
@ -8,13 +8,13 @@ struct InputMapping {
auto isAnalog() const -> bool { return link && link->type == 1; } auto isAnalog() const -> bool { return link && link->type == 1; }
auto isRumble() const -> bool { return link && link->type == 2; } auto isRumble() const -> bool { return link && link->type == 2; }
auto assignmentName() const -> string; auto assignmentName() -> string;
auto deviceName() const -> string; auto deviceName() -> string;
string name; string name;
string assignment = "None"; string assignment = "None";
Emulator::Interface::Device::Input* link = nullptr; Emulator::Interface::Device::Input* link = nullptr;
HID::Device* device = nullptr; shared_pointer<HID::Device> device;
unsigned group = 0; unsigned group = 0;
unsigned input = 0; unsigned input = 0;
enum class Qualifier : unsigned { None, Lo, Hi, Rumble } qualifier = Qualifier::None; enum class Qualifier : unsigned { None, Lo, Hi, Rumble } qualifier = Qualifier::None;
@ -45,16 +45,16 @@ struct InputManager {
InputManager(); InputManager();
auto bind() -> void; auto bind() -> void;
auto poll() -> void; auto poll() -> void;
auto onChange(HID::Device& device, unsigned group, unsigned input, int16 oldValue, int16 newValue) -> void; auto onChange(shared_pointer<HID::Device> device, unsigned group, unsigned input, int16 oldValue, int16 newValue) -> void;
auto quit() -> void; auto quit() -> void;
auto findMouse() -> HID::Device*; auto findMouse() -> shared_pointer<HID::Device>;
//hotkeys.cpp //hotkeys.cpp
auto appendHotkeys() -> void; auto appendHotkeys() -> void;
auto pollHotkeys() -> void; auto pollHotkeys() -> void;
vector<HID::Device*> devices; vector<shared_pointer<HID::Device>> devices;
vector<InputEmulator> emulators; vector<InputEmulator> emulators;
vector<InputHotkey*> hotkeys; vector<InputHotkey*> hotkeys;
Configuration::Document config; Configuration::Document config;

View File

@ -47,9 +47,9 @@ auto HotkeySettings::assignMapping() -> void {
} }
} }
auto HotkeySettings::inputEvent(HID::Device& device, unsigned group, unsigned input, int16 oldValue, int16 newValue) -> void { auto HotkeySettings::inputEvent(shared_pointer<HID::Device> device, unsigned group, unsigned input, int16 oldValue, int16 newValue) -> void {
if(!activeMapping) return; if(!activeMapping) return;
if(!device.isKeyboard() || oldValue != 0 || newValue != 1) return; if(!device->isKeyboard() || oldValue != 0 || newValue != 1) return;
if(activeMapping->bind(device, group, input, oldValue, newValue)) { if(activeMapping->bind(device, group, input, oldValue, newValue)) {
activeMapping = nullptr; activeMapping = nullptr;

View File

@ -115,17 +115,17 @@ auto InputSettings::assignMouseInput(unsigned id) -> void {
activeMapping = activeDevice().mappings[mapping->offset()]; activeMapping = activeDevice().mappings[mapping->offset()];
if(activeMapping->isDigital()) { if(activeMapping->isDigital()) {
return inputEvent(*mouse, HID::Mouse::GroupID::Button, id, 0, 1, true); return inputEvent(mouse, HID::Mouse::GroupID::Button, id, 0, 1, true);
} else if(activeMapping->isAnalog()) { } else if(activeMapping->isAnalog()) {
return inputEvent(*mouse, HID::Mouse::GroupID::Axis, id, 0, +32767, true); return inputEvent(mouse, HID::Mouse::GroupID::Axis, id, 0, +32767, true);
} }
} }
} }
} }
auto InputSettings::inputEvent(HID::Device& device, unsigned group, unsigned input, int16 oldValue, int16 newValue, bool allowMouseInput) -> void { auto InputSettings::inputEvent(shared_pointer<HID::Device> device, unsigned group, unsigned input, int16 oldValue, int16 newValue, bool allowMouseInput) -> void {
if(!activeMapping) return; if(!activeMapping) return;
if(device.isMouse() && !allowMouseInput) return; if(device->isMouse() && !allowMouseInput) return;
if(activeMapping->bind(device, group, input, oldValue, newValue)) { if(activeMapping->bind(device, group, input, oldValue, newValue)) {
activeMapping = nullptr; activeMapping = nullptr;

View File

@ -10,7 +10,7 @@ struct InputSettings : TabFrameItem {
auto refreshMappings() -> void; auto refreshMappings() -> void;
auto assignMapping() -> void; auto assignMapping() -> void;
auto assignMouseInput(unsigned id) -> void; auto assignMouseInput(unsigned id) -> void;
auto inputEvent(HID::Device& device, unsigned group, unsigned input, int16 oldValue, int16 newValue, bool allowMouseInput = false) -> void; auto inputEvent(shared_pointer<HID::Device> device, unsigned group, unsigned input, int16 oldValue, int16 newValue, bool allowMouseInput = false) -> void;
InputMapping* activeMapping = nullptr; InputMapping* activeMapping = nullptr;
@ -34,7 +34,7 @@ struct HotkeySettings : TabFrameItem {
auto reloadMappings() -> void; auto reloadMappings() -> void;
auto refreshMappings() -> void; auto refreshMappings() -> void;
auto assignMapping() -> void; auto assignMapping() -> void;
auto inputEvent(HID::Device& device, unsigned group, unsigned input, int16 oldValue, int16 newValue) -> void; auto inputEvent(shared_pointer<HID::Device> device, unsigned group, unsigned input, int16 oldValue, int16 newValue) -> void;
InputMapping* activeMapping = nullptr; InputMapping* activeMapping = nullptr;