Sunshine0.0.5917
Sunshine 0.0.5917
Self-hosted game stream host for Moonlight.
Loading...
Searching...
No Matches
misc.mm File Reference

Miscellaneous definitions for macOS platform. More...

#include <algorithm>
#include <future>
#include <memory>
#include <AppKit/AppKit.h>
#include <arpa/inet.h>
#include <AVFoundation/AVFoundation.h>
#include <CoreGraphics/CoreGraphics.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <Foundation/Foundation.h>
#include <ifaddrs.h>
#include <mach-o/dyld.h>
#include <net/if_dl.h>
#include <pwd.h>
#include <sys/qos.h>
#include <UserNotifications/UserNotifications.h>
#include <boost/asio/ip/address.hpp>
#include <boost/asio/ip/host_name.hpp>
#include "misc.h"
#include "src/boost_process_compat.h"
#include "src/config.h"
#include "src/entry_handler.h"
#include "src/logging.h"
#include "src/platform/common.h"
Include dependency graph for misc.mm:

Classes

class  platf::macos_high_precision_timer
 macOS high-precision timer implementation backed by a worker thread. More...
class  platf::qos_t
 Linux QoS state used to tune socket priority while streaming. More...

Macros

#define __APPLE_USE_RFC_3542   1
 Macro for APPLE USE RFC 3542.

Functions

void platf::adjust_thread_priority (thread_priority_e priority)
 Apply the requested scheduling priority to the current thread.
std::filesystem::path platf::appdata ()
 Performs migration if necessary, then returns the appdata directory.
bool platf::CGPreflightScreenCaptureAccess (void) __attribute__((weak_import))
 Query macOS screen-capture permission without prompting the user.
bool platf::CGRequestScreenCaptureAccess (void) __attribute__((weak_import))
 Request macOS screen-capture permission from the user.
std::unique_ptr< high_precision_timer > platf::create_high_precision_timer ()
 Create platform-specific timer capable of high-precision sleep.
void platf::enable_mouse_keys ()
 Enable or disable X11 mouse keys for the current session.
std::unique_ptr< deinit_t > platf::enable_socket_qos (uintptr_t native_socket, boost::asio::ip::address &address, uint16_t port, qos_data_type_e data_type, bool dscp_tagging)
 Enable QoS on the given socket for traffic to the specified destination.
std::string platf::from_sockaddr (const sockaddr *const)
 Convert a socket address to a printable IP address.
std::pair< std::uint16_t, std::string > platf::from_sockaddr_ex (const sockaddr *const)
 Convert a socket address to a port and printable IP address.
std::string platf::get_host_name ()
 Returns the current computer name in UTF-8.
ifaddr_t platf::get_ifaddrs ()
 Read the local interface address list.
std::string platf::get_mac_address (const std::string_view &address)
 Return the hardware MAC address associated with a network address.
std::vector< permission_status_t > platf::get_permission_statuses ()
 Query access needed by Sunshine on the current platform.
std::filesystem::path platf::get_xdg_restore_token_path ()
 XDG restore token path.
library_t * dyn::handle (const std::vector< const char * > &libs)
 Open the first available shared library from a nonempty list.
std::unique_ptr< deinit_t > platf::init ()
 Initialize the platform-specific high precision timer.
bool platf::is_screen_capture_allowed ()
 Check whether macOS has granted screen-capture permission.
int dyn::load (void *handle, const std::vector< std::tuple< apiproc *, const char * > > &funcs, bool strict=true)
 Load persisted state from its backing store.
void platf::open_url (const std::string &url)
 Open a url in the default web browser.
permission_request_action_t platf::permission_request_action (bool granted, int previous_state, bool startup, bool retry_manual_request)
 Choose a privacy action without repeating a macOS startup prompt.
bool platf::process_group_running (std::uintptr_t native_handle)
 Check if a process group still has running children.
bool platf::request_permission (std::string_view id)
 Initiate a native request or settings action for an access item.
bool platf::request_process_group_exit (std::uintptr_t native_handle)
 Attempt to gracefully terminate a process group.
void platf::request_startup_permissions (bool notifications_enabled)
 Request missing permissions needed at startup.
void platf::request_startup_system_audio_permission ()
 Probe system audio once on startup when no custom sink is configured.
std::string platf::resolve_render_device ()
 Resolves the render device path to use for hardware encoding.
void platf::restart ()
 Restart the Sunshine process through the platform launcher.
void platf::restart_on_exit ()
 Request a Sunshine process restart on exit.
bp::child platf::run_command (bool, bool, const std::string &cmd, boost::filesystem::path &working_dir, const bp::environment &env, FILE *file, std::error_code &ec, bp::group *group)
 Launch a command using the caller's Linux privileges and session.
bool platf::send (send_info_t &send_info)
 Send the serialized response over the active socket.
bool platf::send_batch (const batched_send_info_t &send_info)
 Send fixed-size UDP payload blocks without modifying the batch descriptor.
void platf::set_thread_name (std::string_view name)
 Name the current thread for use with development tools.
bool platf::should_request_startup_permission (const permission_status_t &permission, bool notifications_enabled)
 Decide whether startup should initiate a native permission request.
bool platf::should_request_startup_system_audio_permission (bool has_custom_sink, bool previously_requested)
 Decide whether a one-time startup system audio probe is due.
void platf::streaming_will_start ()
 Apply Linux platform state before streaming starts.
void platf::streaming_will_stop ()
 Restore Linux platform state after streaming stops.
bool platf::supports_local_network_privacy (int major_version)
 Check whether the macOS version has Local Network privacy controls.
struct sockaddr_in platf::to_sockaddr (boost::asio::ip::address_v4 address, uint16_t port)
 Convert to sockaddr.
struct sockaddr_in6 platf::to_sockaddr (boost::asio::ip::address_v6 address, uint16_t port)
 Convert to sockaddr.

Detailed Description

Miscellaneous definitions for macOS platform.

Function Documentation

◆ adjust_thread_priority()

void platf::adjust_thread_priority ( thread_priority_e priority)

Apply the requested scheduling priority to the current thread.

Set the calling thread's Windows scheduling priority.

Parameters
priorityThread priority requested from the platform backend.

◆ appdata()

std::filesystem::path platf::appdata ( )

Performs migration if necessary, then returns the appdata directory.

This is used for the log directory, so it cannot invoke Boost logging!

Returns
The path of the appdata directory that should be used.

◆ CGPreflightScreenCaptureAccess()

bool platf::CGPreflightScreenCaptureAccess ( void )

Query macOS screen-capture permission without prompting the user.

Returns
True when screen-capture permission is granted.

◆ CGRequestScreenCaptureAccess()

bool platf::CGRequestScreenCaptureAccess ( void )

Request macOS screen-capture permission from the user.

Returns
True when screen-capture permission is granted.

◆ create_high_precision_timer()

std::unique_ptr< high_precision_timer > platf::create_high_precision_timer ( )

Create platform-specific timer capable of high-precision sleep.

Returns
A unique pointer to timer

◆ enable_socket_qos()

std::unique_ptr< deinit_t > platf::enable_socket_qos ( uintptr_t native_socket,
boost::asio::ip::address & address,
uint16_t port,
qos_data_type_e data_type,
bool dscp_tagging )

Enable QoS on the given socket for traffic to the specified destination.

Enables QoS on the given socket for traffic to the specified destination.

Parameters
native_socketThe native socket handle.
addressThe destination address for traffic sent on this socket.
portThe destination port for traffic sent on this socket.
data_typeThe type of traffic sent on this socket.
dscp_taggingSpecifies whether to enable DSCP tagging on outgoing traffic.
Returns
Cleanup handle that restores or releases QoS state when destroyed.

◆ from_sockaddr()

std::string platf::from_sockaddr ( const sockaddr * const ip_addr)

Convert a socket address to a printable IP address.

Parameters
ip_addrSocket address to format.
Returns
Printable address, or an empty string for an unsupported address family.

◆ from_sockaddr_ex()

std::pair< std::uint16_t, std::string > platf::from_sockaddr_ex ( const sockaddr * const ip_addr)

Convert a socket address to a port and printable IP address.

Parameters
ip_addrSocket address to format.
Returns
Port in network byte order and printable address, or zero and an empty string for an unsupported family.

◆ get_host_name()

std::string platf::get_host_name ( )

Returns the current computer name in UTF-8.

Returns
Computer name or a placeholder upon failure.

◆ get_ifaddrs()

ifaddr_t platf::get_ifaddrs ( )

Read the local interface address list.

Returns
Owning pointer to the interface address list, or nullptr on failure.

◆ get_mac_address()

std::string platf::get_mac_address ( const std::string_view & address)

Return the hardware MAC address associated with a network address.

Parameters
addressNetwork address being parsed or filtered.
Returns
Hardware MAC address string, or an empty string when it cannot be resolved.

◆ get_permission_statuses()

std::vector< permission_status_t > platf::get_permission_statuses ( )

Query access needed by Sunshine on the current platform.

Returns
Permission status records for the Web UI.

◆ get_xdg_restore_token_path()

std::filesystem::path platf::get_xdg_restore_token_path ( )

XDG restore token path.

XDG Portal token path (unused on Windows).

XDG Portal token path (unused on MacOS).

Get path of the XDG Portal restore token.

Returns
Path of token that may include the session desktop as a suffix (e.g. .gnome, .kde).
Path of portal_token in appdata path.

◆ handle()

void * dyn::handle ( const std::vector< const char * > & libs)

Open the first available shared library from a nonempty list.

Return the native handle owned by the wrapper.

Returns
Open library handle, or null if none could be loaded.
Parameters
libsList of libraries to probe for the requested symbol.
Returns
Native dynamic-library handle, or nullptr when no library can be opened.

◆ init()

std::unique_ptr< deinit_t > platf::init ( )
nodiscard

Initialize the platform-specific high precision timer.

Initialize the Linux high-precision timer file descriptor.

Returns
Cleanup handle for initialized platform resources, or null if none are needed.

◆ is_screen_capture_allowed()

bool platf::is_screen_capture_allowed ( )

Check whether macOS has granted screen-capture permission.

Check whether screen capture allowed.

Returns
True when Sunshine can capture the screen.

◆ load()

int dyn::load ( void * handle,
const std::vector< std::tuple< apiproc *, const char * > > & funcs,
bool strict = true )

Load persisted state from its backing store.

Parameters
handleNative library or object handle used by the operation.
funcsFunction table populated from the loaded library.
strictWhether missing functions should be treated as an error.
Returns
0 when all required symbols are loaded; nonzero when loading fails.

◆ open_url()

void platf::open_url ( const std::string & url)

Open a url in the default web browser.

Parameters
urlThe url to open.

◆ permission_request_action()

permission_request_action_t platf::permission_request_action ( bool granted,
int previous_state,
bool startup,
bool retry_manual_request )

Choose a privacy action without repeating a macOS startup prompt.

Parameters
grantedWhether the native preflight check reports access.
previous_stateZero before a request, one after a request, or two after a grant.
startupWhether this action is part of automatic startup checks.
retry_manual_requestWhether a user-initiated action should retry the native request.
Returns
The action to perform.

◆ process_group_running()

bool platf::process_group_running ( std::uintptr_t native_handle)

Check if a process group still has running children.

Parameters
native_handleThe native handle of the process group.
Returns
true if processes are still running.

◆ request_permission()

bool platf::request_permission ( std::string_view id)

Initiate a native request or settings action for an access item.

On macOS, Local Network opens Privacy & Security, where the user selects Local Network.

Parameters
idStable identifier of the access item.
Returns
True when the action was recognized and initiated.

◆ request_process_group_exit()

bool platf::request_process_group_exit ( std::uintptr_t native_handle)

Attempt to gracefully terminate a process group.

Parameters
native_handleThe native handle of the process group.
Returns
true if termination was successfully requested.

◆ request_startup_permissions()

void platf::request_startup_permissions ( bool notifications_enabled)

Request missing permissions needed at startup.

Parameters
notifications_enabledWhether the running build shows tray notifications.

◆ request_startup_system_audio_permission()

void platf::request_startup_system_audio_permission ( )

Probe system audio once on startup when no custom sink is configured.

The operating system has no passive tap authorization query. Later streams may request access again if the user removes permission.

◆ resolve_render_device()

std::string platf::resolve_render_device ( )

Resolves the render device path to use for hardware encoding.

If config::video.adapter_name is set, returns that. Otherwise, auto-detects the GPU with a connected display via find_render_node_with_display(). Falls back to /dev/dri/renderD128 if detection fails.

Returns
Resolved render device path (may be empty on non-Linux platforms).

◆ restart_on_exit()

void platf::restart_on_exit ( )

Request a Sunshine process restart on exit.

This is registered as an atexit handler by restart(). It forks a child process with a fresh PID so that macOS WindowServer treats it as a new application (required for the system tray icon to reinitialize).

The parent process stays alive as a transparent supervisor: it forwards signals (SIGINT, SIGTERM, SIGHUP) to the child and blocks in waitpid(). This keeps the shell tracking the original PID as its foreground job, preserving Ctrl+C and terminal log output.

◆ run_command()

bp::child platf::run_command ( bool elevated,
bool interactive,
const std::string & cmd,
boost::filesystem::path & working_dir,
const bp::environment & env,
FILE * file,
std::error_code & ec,
bp::group * group )

Launch a command using the caller's Linux privileges and session.

Run a command on the users profile.

The common platform signature's elevation and interactivity flags do not apply on Linux.

Launches a child process as the user, using the current user's environment and a specific working directory.

Parameters
elevatedSpecify whether to elevate the process.
interactiveSpecify whether this will run in a window or hidden.
cmdThe command to run.
working_dirThe working directory for the new process.
envThe environment variables to use for the new process.
fileA file object to redirect the child process's output to (may be nullptr).
ecAn error code, set to indicate any errors that occur during the launch process.
groupA pointer to a bp::group object to which the new process should belong (may be nullptr).
Returns
A bp::child object representing the new process, or an empty bp::child object if the launch fails.

◆ send()

bool platf::send ( send_info_t & send_info)

Send the serialized response over the active socket.

Parameters
send_infoSocket addresses, buffers, and sizes for the send operation.
Returns
True when the packet is submitted to the socket.

◆ send_batch()

bool platf::send_batch ( const batched_send_info_t & send_info)

Send fixed-size UDP payload blocks without modifying the batch descriptor.

Send multiple fixed-size UDP payload blocks using the platform backend.

Parameters
send_infoSocket addresses, buffers, and sizes for the send operation.
Returns
True when all requested packet blocks are submitted to the socket.

◆ set_thread_name()

void platf::set_thread_name ( std::string_view name)

Name the current thread for use with development tools.

Note
On Linux this will be truncated after 15 characters.
Parameters
nameHuman-readable name to assign.

◆ should_request_startup_permission()

bool platf::should_request_startup_permission ( const permission_status_t & permission,
bool notifications_enabled )

Decide whether startup should initiate a native permission request.

Parameters
permissionPermission status and requirement for the active configuration.
notifications_enabledWhether the system tray uses notifications.
Returns
True when a missing permission should be requested.

◆ should_request_startup_system_audio_permission()

bool platf::should_request_startup_system_audio_permission ( bool has_custom_sink,
bool previously_requested )

Decide whether a one-time startup system audio probe is due.

Parameters
has_custom_sinkWhether Sunshine uses a custom audio sink.
previously_requestedWhether a startup probe was already attempted.
Returns
True only for the first native system audio startup probe.

◆ supports_local_network_privacy()

bool platf::supports_local_network_privacy ( int major_version)

Check whether the macOS version has Local Network privacy controls.

Parameters
major_versionmacOS major version number.
Returns
True on macOS 15 and newer.

◆ to_sockaddr() [1/2]

SOCKADDR_IN platf::to_sockaddr ( boost::asio::ip::address_v4 address,
uint16_t port )

Convert to sockaddr.

Parameters
addressNetwork address being parsed or filtered.
portTCP or UDP port number.
Returns
Value converted to sockaddr.

◆ to_sockaddr() [2/2]

SOCKADDR_IN6 platf::to_sockaddr ( boost::asio::ip::address_v6 address,
uint16_t port )

Convert to sockaddr.

Parameters
addressNetwork address being parsed or filtered.
portTCP or UDP port number.
Returns
Value converted to sockaddr.