Skip to content

Commit

Permalink
Add mac scrolling - Nick12 is a savant.
Browse files Browse the repository at this point in the history
Fixes #207.
  • Loading branch information
TheROPFather committed Aug 9, 2018
1 parent af9cc53 commit a54abd3
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/Screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ bool Screen::PassInputToLua(const InputEventPlus& input)
lua_pushvalue(L, -2);
RString error= "Error running input callback: ";
LuaHelpers::RunScriptOnStack(L, error, 1, 1, true);
//handled= lua_toboolean(L, -1);
handled= lua_toboolean(L, -1);
lua_pop(L, 1);
}
lua_pop(L, 1);
Expand Down
4 changes: 3 additions & 1 deletion src/arch/InputHandler/InputHandler_MacOSX_HID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ void InputHandler_MacOSX_HID::QueueCallback( void *target, int result, void *ref
free( event.longValue );
continue;
}
//LOG->Trace( "Got event with cookie %p, value %d", event.elementCookie, int(event.value) );

dev->GetButtonPresses( vPresses, event.elementCookie, event.value, now );

//LOG->Trace( "Got event with cookie %p, value %d", event.elementCookie, int(event.value) );
}
FOREACH_CONST( DeviceInput, vPresses, i )
INPUTFILTER->ButtonPressed( *i );
Expand Down
2 changes: 1 addition & 1 deletion src/archutils/Darwin/KeyboardDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class KeyboardDevice : public HIDDevice
public:
void GetButtonPresses( vector<DeviceInput>& vPresses, IOHIDElementCookie cookie, int value, const std::chrono::time_point<std::chrono::steady_clock> &now) const;
void GetDevicesAndDescriptions( vector<InputDeviceInfo>& vDevices ) const;

static bool DeviceButtonToMacVirtualKey( DeviceButton button, UInt8 &iMacVKOut );
};

Expand Down
50 changes: 24 additions & 26 deletions src/archutils/Darwin/MouseDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ void MouseDevice::AddElement( int usagePage, int usage, IOHIDElementCookie cooki
break;
case kHIDUsage_GD_Z:
case kHIDUsage_GD_Wheel: // also handle wheel
m.z_axis = cookie;
m.z_min = iMin;
m.z_max = iMax;
m.z_axis.emplace_back(cookie);
m.z_min.emplace_back(iMin);
m.z_max.emplace_back(iMax);
break;
default:
//LOG->Warn( "Unknown usagePage usage pair: (kHIDPage_GenericDesktop, %d).", usage );
Expand Down Expand Up @@ -92,9 +92,19 @@ void MouseDevice::AddElement( int usagePage, int usage, IOHIDElementCookie cooki
void MouseDevice::Open()
{
const Mouse& m = m_Mouse;
#define ADD(x) if( m.x ) AddElementToQueue( m.x )
ADD( x_axis ); ADD( y_axis ); ADD( z_axis );
#undef ADD

if(m.x_axis) {
AddElementToQueue(m.x_axis);
}

if(m.y_axis) {
AddElementToQueue(m.y_axis);
}

for(auto& cookie: m.z_axis) {
AddElementToQueue(cookie);
}

for( hash_map<IOHIDElementCookie,DeviceButton>::const_iterator i = m_Mapping.begin(); i != m_Mapping.end(); ++i )
AddElementToQueue( i->first );
}
Expand All @@ -103,34 +113,22 @@ void MouseDevice::GetButtonPresses( vector<DeviceInput>& vPresses, IOHIDElementC
{

// todo: add mouse axis stuff -aj
const Mouse& m = m_Mouse;

const Mouse& m = m_Mouse;
HRESULT result;
IOHIDEventStruct hidEvent;
// result = (*m_Interface)->getElementValue(hidDeviceInterface,cookie,&hidEvent);

LOG->Trace("X, Y: %f, %f", INPUTFILTER->GetCursorX(), INPUTFILTER->GetCursorY());
float x=MACMouseX(),y=MACMouseY();

if( m.x_axis == cookie )
{
LOG->Trace("Mouse X: Value = %i",value);
}
else if( m.y_axis == cookie )
{
LOG->Trace("Mouse Y: Value = %i",value);
}
else if( m.z_axis == cookie )
{
float level = SCALE( value, m.z_min, m.z_max, -1.0f, 1.0f );
INPUTFILTER->ButtonPressed( DeviceInput(DEVICE_MOUSE, MOUSE_WHEELUP, max(-level,0), now));
INPUTFILTER->ButtonPressed( DeviceInput(DEVICE_MOUSE, MOUSE_WHEELDOWN, max(+level,0), now));
}
else
{
hash_map<IOHIDElementCookie, DeviceButton>::const_iterator iter = m_Mapping.find( cookie );
float level = MACMouseScroll();
INPUTFILTER->ButtonPressed( DeviceInput(DEVICE_MOUSE, MOUSE_WHEELUP, max(-level,0), now));
INPUTFILTER->ButtonPressed( DeviceInput(DEVICE_MOUSE, MOUSE_WHEELDOWN, max(+level,0), now));

hash_map<IOHIDElementCookie, DeviceButton>::const_iterator iter = m_Mapping.find( cookie );
if( iter != m_Mapping.end() )
vPresses.push_back( DeviceInput(DEVICE_MOUSE, iter->second, value, now));
}
INPUTFILTER->UpdateCursorLocation(x,y);
}

Expand Down
7 changes: 5 additions & 2 deletions src/archutils/Darwin/MouseDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ struct Mouse
float x{0};
float y{0};
InputDevice id;
IOHIDElementCookie x_axis, y_axis, z_axis;
IOHIDElementCookie x_axis, y_axis;
int x_min, x_max;
int y_min, y_max;
int z_min, z_max;
vector <int> z_min, z_max;

vector <IOHIDElementCookie> z_axis;

Mouse();
};
float MACMouseX();
float MACMouseY();
float MACMouseScroll();
float MACWindowHeight();
float MACWindowWidth();

Expand Down
17 changes: 17 additions & 0 deletions src/archutils/Darwin/SMMain.mm
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
#include "arch/ArchHooks/ArchHooks.h"
#import "MouseDevice.h"

CGFloat scrolled;

float MACMouseScroll()
{
CGFloat scrolledTmp = scrolled;
scrolled = 0;
return scrolledTmp;
}

float MACMouseX()
{
NSRect frame = [[[NSApplication sharedApplication] mainWindow] frame];
Expand All @@ -24,6 +33,7 @@ float MACMouseY()
return frame.size.height - (mouseLoc.y - frame.origin.y) - 15; //Appears to compensate for titlebar
// This padding should be replaced in the future to use Cocoa calls to content
}

float MACWindowHeight()
{
NSRect frame = [[[NSApplication sharedApplication] mainWindow] frame];
Expand All @@ -49,6 +59,7 @@ - (void) fullscreen:(id)sender;
@end

@implementation SMApplication

- (void)fullscreen:(id)sender
{
// don't use ArchHooks::SetToggleWindowed(), it makes the screen black
Expand All @@ -57,6 +68,12 @@ - (void)fullscreen:(id)sender

- (void)sendEvent:(NSEvent *)event
{

if( [event type] == NSScrollWheel) {
scrolled += [event deltaY];

}

if( [event type] == NSKeyDown )
[[self mainMenu] performKeyEquivalent:event];
else
Expand Down

0 comments on commit a54abd3

Please sign in to comment.