summaryrefslogtreecommitdiff
path: root/Source/Core/InputCommon/Src/ControllerInterface/OSX/OSXKeyboard.mm
blob: 1f4b3fc4997cdbc1ad7280881f5b09acf7179edb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "../ControllerInterface.h"

#ifdef CIFACE_USE_OSX

#include "OSXKeyboard.h"
#include <Foundation/Foundation.h>
#include <IOKit/hid/IOHIDLib.h>

namespace ciface
{
namespace OSX
{


struct PrettyKeys
{
	const uint32_t		code;
	const char* const	name;
} named_keys[] =
{
#include "NamedKeys.h"
};

extern void DeviceElementDebugPrint(const void*, void*);


Keyboard::Keyboard(IOHIDDeviceRef device)
	: m_device(device)
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	
	m_device_name = [(NSString *)IOHIDDeviceGetProperty(m_device, CFSTR(kIOHIDProductKey)) UTF8String];
	
	// This class should only recieve Keyboard or Keypad devices
	// Now, filter on just the buttons we can handle sanely
	NSDictionary *matchingElements =
	 [NSDictionary dictionaryWithObjectsAndKeys:
	  [NSNumber numberWithInteger:kIOHIDElementTypeInput_Button], @ kIOHIDElementTypeKey,
	  [NSNumber numberWithInteger:0], @ kIOHIDElementMinKey,
	  [NSNumber numberWithInteger:1], @ kIOHIDElementMaxKey,
	  nil];
	
	CFArrayRef elements =
		IOHIDDeviceCopyMatchingElements(m_device, (CFDictionaryRef)matchingElements, kIOHIDOptionsTypeNone);
	
	if (elements)
	{
		for (int i = 0; i < CFArrayGetCount(elements); i++)
		{
			IOHIDElementRef e = (IOHIDElementRef)CFArrayGetValueAtIndex(elements, i);
			//DeviceElementDebugPrint(e, NULL);
		
			try { inputs.push_back(new Key(e)); }
			catch (std::bad_alloc&) { /*Thrown if the key is reserved*/ }
		}
		CFRelease(elements);
	}
	
	[pool release];
}

ControlState Keyboard::GetInputState( const ControllerInterface::Device::Input* const input )
{
	return ((Input*)input)->GetState(m_device);
}

void Keyboard::SetOutputState( const ControllerInterface::Device::Output* const output, const ControlState state )
{
}

bool Keyboard::UpdateInput()
{
	return true;
}

bool Keyboard::UpdateOutput()
{
	return true;
}

std::string Keyboard::GetName() const
{
	return m_device_name;
}

std::string Keyboard::GetSource() const
{
	return "OSX";
}

int Keyboard::GetId() const
{
	return 0;
}


Keyboard::Key::Key(IOHIDElementRef element)
	: m_element(element)
	, m_name("RESERVED") // for some reason HID Manager gives these to us.
{
	uint32_t keycode = IOHIDElementGetUsage(m_element);
	for (uint32_t i = 0; i < sizeof(named_keys)/sizeof(*named_keys); i++)
	{
		if (named_keys[i].code == keycode)
		{
			m_name = named_keys[i].name;
			return;
		}
	}
	NSLog(@"Got key 0x%x of type RESERVED", IOHIDElementGetUsage(m_element));      
}

ControlState Keyboard::Key::GetState(IOHIDDeviceRef device)
{
	IOHIDValueRef value;
	if (IOHIDDeviceGetValue(device, m_element, &value) == kIOReturnSuccess)
		return IOHIDValueGetIntegerValue(value) > 0;

	return false;
}

std::string Keyboard::Key::GetName() const
{
	return m_name;
}


}
}

#endif