Sunshine master
Self-hosted game stream host for Moonlight.
input.cpp File Reference

Definitions for macOS input handling. More...

#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstdint>
#include <iostream>
#include <thread>
#include <ApplicationServices/ApplicationServices.h>
import <Carbon/Carbon.h>
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/hidsystem/IOLLEvent.h>
#include <mach/mach.h>
#include "src/display_device.h"
#include "src/input.h"
#include "src/logging.h"
#include "src/platform/common.h"
#include "src/utility.h"
Include dependency graph for input.cpp:

Classes

struct  platf::KeyCodeMap
 Mapping from Sunshine key symbols to macOS virtual key codes. More...
 
struct  platf::macos_input_t
 macOS input source and target display state. More...
 
struct  platf::modifier_flags_t
 macOS modifier flags split into generic and device-specific bits. More...
 

Functions

void platf::abs_mouse (input_t &input, const touch_port_t &touch_port, float x, float y)
 Move the pointer to an absolute client-provided touch coordinate.
 
int platf::alloc_gamepad (input_t &input, const gamepad_id_t &id, const gamepad_arrival_t &metadata, feedback_queue_t feedback_queue)
 Create a new virtual gamepad.
 
std::unique_ptr< client_input_tplatf::allocate_client_input_context (input_t &input)
 Allocate a context to store per-client input data.
 
void platf::button_mouse (input_t &input, int button, bool release)
 Press or release a virtual mouse button.
 
CGEventType platf::event_type_mouse (input_t &input)
 Choose the CoreGraphics mouse event type for the current button action.
 
void platf::free_gamepad (input_t &input, int nr)
 Release gamepad resources.
 
void platf::freeInput (void *)
 Release a platform input backend created by input().
 
void platf::gamepad_battery (input_t &input, const gamepad_battery_t &battery)
 Send a gamepad battery event to the OS.
 
void platf::gamepad_motion (input_t &input, const gamepad_motion_t &motion)
 Send a gamepad motion event to the OS.
 
void platf::gamepad_touch (input_t &input, const gamepad_touch_t &touch)
 Send a gamepad touch event to the OS.
 
void platf::gamepad_update (input_t &input, int nr, const gamepad_state_t &gamepad_state)
 Submit updated Sunshine gamepad state to the virtual device.
 
platform_caps::caps_t platf::get_capabilities ()
 Get the supported platform capabilities to advertise to the client.
 
util::point_t platf::get_mouse_loc (input_t &input)
 Get the current mouse position on screen.
 
int platf::get_scroll_lines_per_detent (double &scrollwheel_scaling)
 Get scroll lines per detent.
 
void platf::hscroll (input_t &input, int distance)
 Apply a horizontal scroll event to the virtual mouse.
 
input_t platf::input ()
 Create the platform input backend for a stream.
 
void platf::keyboard_update (input_t &input, uint16_t modcode, bool release, uint8_t flags)
 Press or release a virtual keyboard key.
 
int platf::keysym (int keycode)
 Translate a platform keycode to a Sunshine key symbol.
 
bool platf::modifier_flags_for_key (int key, modifier_flags_t &flags)
 Resolve the CoreGraphics modifier masks associated with a key code.
 
void platf::move_mouse (input_t &input, int deltaX, int deltaY)
 Move mouse using the backend coordinate system.
 
bool platf::operator< (const KeyCodeMap &a, const KeyCodeMap &b)
 Order key mappings by Sunshine key code for binary search.
 
void platf::pen_update (client_input_t *input, const touch_port_t &touch_port, const pen_input_t &pen)
 Send a pen event to the OS.
 
void platf::post_mouse (input_t &input, const CGMouseButton button, const CGEventType type, const util::point_t raw_location, const util::point_t previous_location, const int click_count)
 Post a mouse event at the clamped display location.
 
void platf::post_scroll (input_t &input, const int wheelY, const int wheelX)
 Post a macOS scroll event to the target display.
 
void platf::scroll (input_t &input, int distance)
 Apply a vertical scroll event to the virtual mouse.
 
int platf::scroll_pixels (const macos_input_t *macos_input, const int high_res_distance)
 Convert a high-resolution wheel delta to CoreGraphics scroll pixels.
 
std::vector< supported_gamepad_t > & platf::supported_gamepads (input_t *input)
 Gets the supported gamepads for this platform backend.
 
void platf::touch_update (client_input_t *input, const touch_port_t &touch_port, const touch_input_t &touch)
 Send a touch event to the OS.
 
void platf::unicode (input_t &input, char *utf8, int size)
 Submit UTF-8 text input to the keyboard backend.
 

Variables

constexpr int platf::DEFAULT_SCROLL_LINES_PER_DETENT = 5
 Protocol or platform constant for default scroll lines per detent.
 
constexpr double platf::DEFAULT_SCROLLWHEEL_SCALING = 0.3125
 Protocol or platform constant for default scrollwheel scaling.
 
const KeyCodeMap platf::kKeyCodesMap []
 Key codes map.
 
constexpr auto MULTICLICK_DELAY_MS = std::chrono::milliseconds(500)
 Maximum gap between clicks that macOS should treat as a double click.
 
constexpr int platf::WHEEL_DELTA = 120
 Protocol or platform constant for wheel delta.
 

Detailed Description

Definitions for macOS input handling.

Function Documentation

◆ abs_mouse()

void platf::abs_mouse ( input_t & input,
const touch_port_t & touch_port,
float x,
float y )

Move the pointer to an absolute client-provided touch coordinate.

Parameters
inputPlatform input backend that receives the event.
touch_portTouch coordinate bounds used for scaling.
xHorizontal absolute coordinate from the client.
yVertical absolute coordinate from the client.

◆ alloc_gamepad()

int platf::alloc_gamepad ( input_t & input,
const gamepad_id_t & id,
const gamepad_arrival_t & metadata,
feedback_queue_t feedback_queue )

Create a new virtual gamepad.

Parameters
inputThe global input context.
idThe gamepad ID.
metadataController metadata from client (empty if none provided).
feedback_queueThe queue for posting messages back to the client.
Returns
0 on success.

◆ allocate_client_input_context()

std::unique_ptr< client_input_t > platf::allocate_client_input_context ( input_t & input)

Allocate a context to store per-client input data.

Allocates a context to store per-client input data.

Parameters
inputThe global input context.
Returns
A unique pointer to a per-client input data context.

◆ button_mouse()

void platf::button_mouse ( input_t & input,
int button,
bool release )

Press or release a virtual mouse button.

Parameters
inputPlatform input backend that receives the event.
buttonMouse button identifier to press or release.
releaseWhether the key or button event is a release.

◆ event_type_mouse()

CGEventType platf::event_type_mouse ( input_t & input)
inline

Choose the CoreGraphics mouse event type for the current button action.

Parameters
inputPlatform input backend that receives the event.
Returns
CoreGraphics mouse event type for button press or release.

◆ free_gamepad()

void platf::free_gamepad ( input_t & input,
int nr )

Release gamepad resources.

Parameters
inputPlatform input backend that receives the event.
nrController index assigned by the client.

◆ freeInput()

void platf::freeInput ( void * p)

Release a platform input backend created by input().

Parameters
pPointer passed to the deleter or conversion helper.

◆ gamepad_battery()

void platf::gamepad_battery ( input_t & input,
const gamepad_battery_t & battery )

Send a gamepad battery event to the OS.

Sends a gamepad battery event to the OS.

Parameters
inputThe global input context.
batteryThe battery event.

◆ gamepad_motion()

void platf::gamepad_motion ( input_t & input,
const gamepad_motion_t & motion )

Send a gamepad motion event to the OS.

Sends a gamepad motion event to the OS.

Parameters
inputThe global input context.
motionThe motion event.

◆ gamepad_touch()

void platf::gamepad_touch ( input_t & input,
const gamepad_touch_t & touch )

Send a gamepad touch event to the OS.

Sends a gamepad touch event to the OS.

Parameters
inputThe global input context.
touchThe touch event.

◆ gamepad_update()

void platf::gamepad_update ( input_t & input,
int nr,
const gamepad_state_t & gamepad_state )

Submit updated Sunshine gamepad state to the virtual device.

Parameters
inputThe input context.
nrThe gamepad index to update.
gamepad_stateThe gamepad button/axis state sent from the client.

◆ get_capabilities()

platform_caps::caps_t platf::get_capabilities ( )

Get the supported platform capabilities to advertise to the client.

Returns the supported platform capabilities to advertise to the client.

Returns
Capability flags.

◆ get_mouse_loc()

util::point_t platf::get_mouse_loc ( input_t & input)

Get the current mouse position on screen.

Parameters
inputThe input_t instance to use.
Returns
Screen coordinates of the mouse.

*Examples**

auto [x, y] = get_mouse_loc(input);

◆ get_scroll_lines_per_detent()

int platf::get_scroll_lines_per_detent ( double & scrollwheel_scaling)

Get scroll lines per detent.

Parameters
scrollwheel_scalingScrollwheel scaling.
Returns
Number of logical lines represented by one wheel detent.

◆ hscroll()

void platf::hscroll ( input_t & input,
int distance )

Apply a horizontal scroll event to the virtual mouse.

Parameters
inputPlatform input backend that receives the event.
distanceHigh-resolution scroll distance reported by the client.

◆ input()

input_t platf::input ( )

Create the platform input backend for a stream.

Returns
Platform-specific input backend for the active stream.

◆ keyboard_update()

void platf::keyboard_update ( input_t & input,
uint16_t modcode,
bool release,
uint8_t flags )

Press or release a virtual keyboard key.

Parameters
inputPlatform input backend that receives the event.
modcodeModifier key code to update.
releaseWhether the key or button event is a release.
flagsBit flags that modify the requested operation.

◆ keysym()

int platf::keysym ( int keycode)

Translate a platform keycode to a Sunshine key symbol.

Parameters
keycodePlatform keycode being translated or emitted.
Returns
Sunshine key symbol, or 0 when the keycode is unmapped.

◆ modifier_flags_for_key()

bool platf::modifier_flags_for_key ( int key,
modifier_flags_t & flags )

Resolve the CoreGraphics modifier masks associated with a key code.

Parameters
keySunshine key code that may represent a modifier key.
flagsOutput masks for the matching CoreGraphics modifier.
Returns
True when modifier flags were found for the key.

◆ move_mouse()

void platf::move_mouse ( input_t & input,
int deltaX,
int deltaY )

Move mouse using the backend coordinate system.

Parameters
inputPlatform input backend that receives the event.
deltaXDelta x.
deltaYDelta y.

◆ operator<()

bool platf::operator< ( const KeyCodeMap & a,
const KeyCodeMap & b )

Order key mappings by Sunshine key code for binary search.

Parameters
aLeft-hand mapping being compared.
bRight-hand mapping being compared.
Returns
True when a has a lower Sunshine key code than b.

◆ pen_update()

void platf::pen_update ( client_input_t * input,
const touch_port_t & touch_port,
const pen_input_t & pen )

Send a pen event to the OS.

Sends a pen event to the OS.

Parameters
inputThe client-specific input context.
touch_portThe current viewport for translating to screen coordinates.
penThe pen event.

◆ post_mouse()

void platf::post_mouse ( input_t & input,
const CGMouseButton button,
const CGEventType type,
const util::point_t raw_location,
const util::point_t previous_location,
const int click_count )

Post a mouse event at the clamped display location.

Parameters
inputPlatform input context.
buttonMouse button.
typeCoreGraphics mouse event type.
raw_locationRequested mouse location.
previous_locationPrevious mouse location.
click_countClick count for the event.

◆ post_scroll()

void platf::post_scroll ( input_t & input,
const int wheelY,
const int wheelX )

Post a macOS scroll event to the target display.

Parameters
inputPlatform input backend that receives the event.
wheelYWheel y.
wheelXWheel x.

◆ scroll()

void platf::scroll ( input_t & input,
int distance )

Apply a vertical scroll event to the virtual mouse.

Parameters
inputPlatform input backend that receives the event.
distanceHigh-resolution scroll distance reported by the client.

◆ scroll_pixels()

int platf::scroll_pixels ( const macos_input_t * macos_input,
const int high_res_distance )

Convert a high-resolution wheel delta to CoreGraphics scroll pixels.

Parameters
macos_inputmacOS input state containing scroll scaling settings.
high_res_distanceWheel delta in Windows high-resolution units.
Returns
Pixel distance to send to CoreGraphics.

◆ supported_gamepads()

std::vector< supported_gamepad_t > & platf::supported_gamepads ( input_t * input)

Gets the supported gamepads for this platform backend.

This may be called prior to platf::input()!

Parameters
inputPointer to the platform's input_t or nullptr.
Returns
Vector of gamepad options and status.

◆ touch_update()

void platf::touch_update ( client_input_t * input,
const touch_port_t & touch_port,
const touch_input_t & touch )

Send a touch event to the OS.

Sends a touch event to the OS.

Parameters
inputThe client-specific input context.
touch_portThe current viewport for translating to screen coordinates.
touchThe touch event.

◆ unicode()

void platf::unicode ( input_t & input,
char * utf8,
int size )

Submit UTF-8 text input to the keyboard backend.

Parameters
inputPlatform input backend that receives the event.
utf8UTF-8 text submitted by the client.
sizeNumber of bytes or elements requested.