libretro.session¶
High-level harness that drives a Core through its libretro lifecycle.
See also
libretro.builderThe
SessionBuilderfactory used to construct a configuredSession.
Classes
A configured libretro core paired with the drivers that satisfy its environment calls. |
- class Session[source]¶
Bases:
CompositeEnvironmentDriver,GenericA configured libretro core paired with the drivers that satisfy its environment calls.
Use
Sessionas a context manager: entering it loads the core (and game, if any) and wires up callbacks; exiting it unloads the game and deinitializes the core. Each constructor argument selects which driver implementation to use for one libretro subsystem; most are optional and default toNone, in which case the matching env-call returns failure.See also
SessionBuilderFluent builder that constructs a
Sessionwith sensible defaults.
- __init__(core, game, audio, input, video, content=None, overscan=None, message=None, options=None, path=None, rumble=None, sensor=None, camera=None, log=None, perf=None, location=None, user=None, vfs=None, led=None, av_enable=None, midi=None, timing=None, preferred_hw=None, driver_switch_enable=None, savestate_context=None, jit_capable=None, mic=None, device_power=None)[source]¶
Initialize the session with a core, optional game content, and driver implementations.
- Parameters:
core (
Core|CDLL|str|PathLike[str] |PathLike[bytes]) – The libretro core to load. Accepts aCore, an already-loadedctypes.CDLL, or a path to a shared library on disk.game (
TypeAliasType|SubsystemContent|None) – The content to pass toretro_load_game(orretro_load_game_special), orNoneto load the core without content.audio (
TypeVar(_Audio, bound=AudioDriver)) – Required audio driver.input (
TypeVar(_Input, bound=InputDriver)) – Required input driver.video (
TypeVar(_Video, bound=VideoDriver)) – Required video driver.content (
TypeVar(_Content, bound=ContentDriver|None)) – Optional content driver that resolvesContentreferences to loaded files.overscan (
bool|None) – Initial value for theoverscanenv-call response, orNoneto leave it unset.message (
TypeVar(_Message, bound=MessageDriver|None)) – Optional driver that handlesRETRO_ENVIRONMENT_SET_MESSAGE.options (
TypeVar(_Option, bound=OptionDriver|None)) – Optional core-options driver.path (
TypeVar(_Path, bound=PathDriver|None)) – Optional driver that supplies system/save/asset directory paths.rumble (
TypeVar(_Rumble, bound=RumbleDriver|None)) – Optional rumble driver.sensor (
TypeVar(_Sensor, bound=SensorDriver|None)) – Optional motion-sensor driver.camera (
TypeVar(_Camera, bound=CameraDriver|None)) – Optional camera driver.log (
TypeVar(_Log, bound=LogDriver|None)) – Optional log driver.perf (
TypeVar(_Perf, bound=PerfDriver|None)) – Optional performance-counter driver.location (
TypeVar(_Location, bound=LocationDriver|None)) – Optional geolocation driver.user (
TypeVar(_User, bound=UserDriver|None)) – Optional driver that exposes username and language to the core.vfs (
TypeVar(_Vfs, bound=FileSystemDriver|None)) – Optional virtual filesystem driver.led (
TypeVar(_Led, bound=LedDriver|None)) – Optional LED driver.av_enable (
AvEnableFlags|None) – Initial value for theaudio_video_enableenv-call response, orNoneto leave it unset.midi (
TypeVar(_Midi, bound=MidiDriver|None)) – Optional MIDI driver.timing (
TypeVar(_Timing, bound=TimingDriver|None)) – Optional timing driver.preferred_hw (
HardwareContext|None) – Initial value for thepreferred_hw_renderenv-call response, orNoneto leave it unset.driver_switch_enable (
bool|None) – WhetherRETRO_ENVIRONMENT_GET_AUDIO_VIDEO_ENABLEshould report driver-switch support, orNoneto leave it unset.savestate_context (
SavestateContext|None) – Initial value for thesavestate_contextenv-call response, orNoneto leave it unset.jit_capable (
bool|None) – Initial value for thejit_capableenv-call response, orNoneto leave it unset.mic (
TypeVar(_Mic, bound=MicrophoneDriver|None)) – Optional microphone driver.device_power (
TypeVar(_Power, bound=PowerDriver|None)) – Optional driver that reports device battery state.
- Raises:
TypeError – If
coreis not aCore,ctypes.CDLL, or a filesystem path.
- __enter__()[source]¶
Initialize the core, register callbacks, and load content.
- Returns:
This session, suitable for use inside a
withblock.- Raises:
RuntimeError – If the core’s API version is incompatible, its system info is incomplete, or content loading fails.
- __exit__(exc_type, exc_val, exc_tb)[source]¶
Unload the game and deinitialize the core, propagating exceptions unless the core shut down.
- Returns:
Trueif aCoreShutDownExceptionshould be suppressed.
- property core¶
The active
CoreInterfacefor this session.- Raises:
CoreShutDownException – If the session has exited or the core has shut down.
- property is_exited¶
Whether this session has exited its
withblock.
- property system_directory¶
The system directory the path driver advertises to the core.
Noneif no path driver was configured.
- property system_dir¶
Alias for
system_directory.
- property save_directory¶
The save directory the path driver advertises to the core.
Noneif no path driver was configured.
- property save_dir¶
Alias for
save_directory.
- property max_users¶
The maximum number of input ports the input driver advertises.
- property content_info_overrides¶
Content-info overrides registered by the core, exposed by the content driver.
Noneif no content driver was configured.
- run()[source]¶
Advance the core by one frame.
Polls per-frame drivers, ticks the timing driver, and calls
retro_run.- Raises:
CoreShutDownException – If the session has exited or the core has shut down.
- Return type:
- reset()[source]¶
Reset the running core, equivalent to flipping the emulated power switch.
- Raises:
CoreShutDownException – If the session has exited or the core has shut down.
- Return type:
- set_controller_port_device(port, device)[source]¶
Bind a controller class to an input port.
- Parameters:
- Raises:
CoreShutDownException – If the session has exited or the core has shut down.
- Return type:
- cheat_reset()[source]¶
Clear all cheats currently registered with the core.
- Raises:
CoreShutDownException – If the session has exited or the core has shut down.
- Return type:
- key in self¶
- iter(self)¶
- Return type:
- len(self)¶
- classmethod __new__(*args, **kwargs)¶
- property audio¶
Return the
AudioDriversupplied at construction time.
- audio_sample(left, right)¶
Receive a single stereo audio sample from the core.
- Parameters:
See also
retro_audio_sample_tThe C function pointer type whose signature this method implements.
- Return type:
- audio_sample_batch(data, frames)¶
Receive a batch of interleaved stereo audio frames from the core.
- Parameters:
data (
LP_c_short) – Pointer to interleaved signed 16-bit stereo samples.frames (
int) – Number of stereo frames available indata.
- Return type:
- Returns:
The number of frames consumed.
See also
retro_audio_sample_batch_tThe C function pointer type whose signature this method implements.
- property av_enable¶
Return the
AvEnableFlagsthe frontend exposes to the core, orNoneif unset.See also
EnvironmentCall.GET_AUDIO_VIDEO_ENABLEThe environment call that queries this value.
- property camera¶
Return the
CameraDriversupplied at construction time, orNoneif absent.
- property can_dupe¶
Return whether the underlying
VideoDriversupports frame duping.See also
EnvironmentCall.GET_CAN_DUPEThe environment call that queries this value.
- property content¶
Return the
ContentDriversupplied at construction time, orNoneif absent.
- property controller_info¶
Return the controller descriptions the core registered, or
Noneif none were set.See also
EnvironmentCall.SET_CONTROLLER_INFOThe environment call that registers this information.
- property core_options_version¶
Return the core-options API version supported by the option driver, or
Noneif absent.See also
EnvironmentCall.GET_CORE_OPTIONS_VERSIONThe environment call that queries this value.
- environment(cmd, data)¶
Dispatch an environment call from the core.
Implementations route
cmdto the appropriate_<env_call_name>helper (e.g._get_variableforRETRO_ENVIRONMENT_GET_VARIABLE) and returnTrueif the call was handled successfully.- Parameters:
- Return type:
- Returns:
Trueif the call was handled,Falseif the command is unsupported or failed.
See also
retro_environment_tThe C function pointer type whose signature this method implements.
- property geometry¶
Return the current frame geometry from the underlying
VideoDriver.See also
EnvironmentCall.SET_GEOMETRYThe environment call that updates this value.
- get(k[, d]) D[k] if k in D, else d. d defaults to None.¶
- get_proc_address(sym, funtype=None)¶
- Overloads:
self, sym (str | bytes), funtype (type[T]) → T | None
self, sym (str | bytes), funtype (None) → retro_proc_address_t | None
self, sym (Literal[b’’, ‘’]), funtype (type[_CFunctionType] | None) → None
self, sym (str | bytes), funtype (type[TypedFunctionPointer[R, P]]) → TypedFunctionPointer[R, P] | None
Look up a function pointer the core exposed via its proc-address interface.
- Parameters:
sym – The name of the symbol to look up; an empty name returns
None.funtype – An optional
ctypesfunction type to cast the returned pointer to; if omitted, a genericretro_proc_address_tis returned.
- Returns:
The requested function pointer cast to
funtype, orNoneif the core has not registered a proc-address interface, the symbol is empty, or the lookup failed.
Return whether the underlying
VideoDriveris configured for a shared HW context.See also
EnvironmentCall.SET_HW_SHARED_CONTEXTThe environment call that enables shared-context mode.
- property input¶
Return the
InputDriversupplied at construction time.
- property input_bitmasks¶
Return whether the underlying
InputDriversupports input bitmasks.See also
EnvironmentCall.GET_INPUT_BITMASKSThe environment call that queries this value.
- property input_descriptors¶
Return the input descriptors registered by the core, or
Noneif none were set.See also
EnvironmentCall.SET_INPUT_DESCRIPTORSThe environment call that registers these descriptors.
- input_poll()¶
Poll input devices for new input state.
Called by the core once per frame before any
input_state()queries.See also
retro_input_poll_tThe C function pointer type whose signature this method implements.
- Return type:
- input_state(port, device, index, id)¶
Return the current input state for a given device control.
- Parameters:
- Return type:
- Returns:
The control’s current state, encoded per libretro’s input conventions.
See also
retro_input_state_tThe C function pointer type whose signature this method implements.
- property is_shutdown¶
Return
Trueif the core has requested a shutdown via the environment call.See also
EnvironmentCall.SHUTDOWNThe environment call that flips this flag.
- items() a set-like object providing a view on D's items¶
- property jit_capable¶
Return whether the frontend permits JIT compilation, or
Noneif unset.See also
EnvironmentCall.GET_JIT_CAPABLEThe environment call that queries this value.
- property keyboard_callback¶
Return the keyboard callback registered by the core, or
Noneif none was set.See also
EnvironmentCall.SET_KEYBOARD_CALLBACKThe environment call that registers this callback.
- keys() a set-like object providing a view on D's keys¶
- property location¶
Return the
LocationDriversupplied at construction time, orNoneif absent.
- property memory_maps¶
Return the memory map the core registered, or
Noneif none was set.See also
EnvironmentCall.SET_MEMORY_MAPSThe environment call that registers this information.
- property message¶
Return the
MessageDriversupplied at construction time, orNoneif absent.
- property mic¶
Return the
MicrophoneDriversupplied at construction time, orNoneif absent.
- property midi¶
Return the
MidiDriversupplied at construction time, orNoneif absent.
- property options¶
Return the
OptionDriversupplied at construction time, orNoneif absent.
- property overscan¶
Return whether the frontend wants overscan to be visible, or
Noneif unset.See also
EnvironmentCall.GET_OVERSCANThe environment call that queries this value.
- property path¶
Return the
PathDriversupplied at construction time, orNoneif absent.
- property perf¶
Return the
PerfDriversupplied at construction time, orNoneif absent.
- property performance_level¶
Return the performance level the core has reported, or
Noneif unset.See also
EnvironmentCall.SET_PERFORMANCE_LEVELThe environment call that sets this value.
- property pixel_format¶
Return the
PixelFormatcurrently set on the underlyingVideoDriver.See also
EnvironmentCall.SET_PIXEL_FORMATThe environment call that updates this value.
- property power¶
Return the
PowerDriversupplied at construction time, orNoneif absent.
- property preferred_hw_render¶
Return the preferred
HardwareContextexposed to the core, orNoneif unset.See also
EnvironmentCall.GET_PREFERRED_HW_RENDERThe environment call that queries this value.
- property proc_address_callback¶
Return the
retro_get_proc_address_interfaceregistered by the core, if any.See also
EnvironmentCall.SET_PROC_ADDRESS_CALLBACKThe environment call that registers this interface.
- static return_on_raise(default)¶
Ctypes doesn’t propagate exceptions out of callbacks, so this is necessary to detect an error in a driver instead of just swallowing it with a warning.
- property rotation¶
Return the screen
Rotationcurrently set on the underlyingVideoDriver.See also
EnvironmentCall.SET_ROTATIONThe environment call that updates this value.
- property rumble¶
Return the
RumbleDriversupplied at construction time, orNoneif absent.
- property savestate_context¶
Return the
SavestateContextthe frontend exposes to the core, orNoneif unset.See also
EnvironmentCall.GET_SAVESTATE_CONTEXTThe environment call that queries this value.
- property sensor¶
Return the
SensorDriversupplied at construction time, orNoneif absent.
- property serialization_quirks¶
Return the
SerializationQuirksthe core declared, orNoneif unset.See also
EnvironmentCall.SET_SERIALIZATION_QUIRKSThe environment call that sets these flags.
- property subsystems¶
Return the subsystem info the core registered, or
Noneif no content driver is set.See also
EnvironmentCall.SET_SUBSYSTEM_INFOThe environment call that registers this information.
- property support_achievements¶
Return whether the core supports achievements, or
Noneif it has not declared.See also
EnvironmentCall.SET_SUPPORT_ACHIEVEMENTSThe environment call that sets this value.
- property support_no_game¶
Return whether the core can run without content, or
Noneif no content driver is set.See also
EnvironmentCall.SET_SUPPORT_NO_GAMEThe environment call that sets this value.
- property timing¶
Return the
TimingDriversupplied at construction time, orNoneif absent.
- property user¶
Return the
UserDriversupplied at construction time, orNoneif absent.
- values() an object providing a view on D's values¶
- property vfs¶
Return the
FileSystemDriversupplied at construction time, orNoneif absent.
- property video¶
Return the
VideoDriversupplied at construction time.
- video_refresh(data, width, height, pitch)¶
Receive one frame of video output from the core.
- Parameters:
data (
c_void_ptr) – Pointer to the frame buffer in the core’s current pixel format, orNoneif the core asked the frontend to duplicate the previous frame.width (
int) – Width of the frame in pixels.height (
int) – Height of the frame in pixels.pitch (
int) – Number of bytes per scanline indata.
See also
retro_video_refresh_tThe C function pointer type whose signature this method implements.
- Return type: