|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import logging |
| 4 | +import subprocess |
| 5 | +from pathlib import Path |
| 6 | +from ..exceptions import BatoceraException |
| 7 | +from typing import TYPE_CHECKING |
| 8 | + |
| 9 | +if TYPE_CHECKING: |
| 10 | + from ..generators.Generator import Generator |
| 11 | + |
| 12 | +_logger = logging.getLogger(__name__) |
| 13 | + |
| 14 | +def is_GBM_Supported() -> boolean: |
| 15 | + try: |
| 16 | + eglinfo_bin = Path("/usr/bin/eglinfo") |
| 17 | + if not eglinfo_bin.exists(): |
| 18 | + return False |
| 19 | + |
| 20 | + eglCmd = '/usr/bin/eglinfo | grep EGL_KHR_platform_gbm' |
| 21 | + eglCmdResult = subprocess.run(eglCmd, shell=True, capture_output=True, text=True) |
| 22 | + grep_return_code = eglCmdResult.returncode |
| 23 | + |
| 24 | + if grep_return_code == 0: |
| 25 | + return True |
| 26 | + return False |
| 27 | + |
| 28 | + except Exception: |
| 29 | + return False |
| 30 | + |
| 31 | +def add_gamescope_arguments(command: Command, system: Emulator, CurrentResolution: Resolution, /) -> None: |
| 32 | + |
| 33 | + if not system.config.get_bool("gamescope"): |
| 34 | + return |
| 35 | + |
| 36 | + if not is_GBM_Supported(): |
| 37 | + raise BatoceraException(f"GPU driver don't support gamescope") |
| 38 | + |
| 39 | + gamescope_arguments = ["/usr/bin/gamescope"] |
| 40 | + |
| 41 | + # Retrieve output resolution from system options. |
| 42 | + # if not defined, inherit from the current screen resolution |
| 43 | + output_resolution = system.config.get_str("gamescope_output_resolution") |
| 44 | + if output_resolution: |
| 45 | + output_width, _, output_height = output_resolution.partition("x") |
| 46 | + else: |
| 47 | + output_width = f'{CurrentResolution["width"]}' |
| 48 | + output_height = f'{CurrentResolution["height"]}' |
| 49 | + |
| 50 | + gamescope_arguments_cmd.extend([ |
| 51 | + "-W", output_width.strip(), |
| 52 | + "-H", output_height.strip() |
| 53 | + ]) |
| 54 | + |
| 55 | + # Retrieve nested resolution from system options. |
| 56 | + # default undefined inherit from gamescopt_output_resoluton |
| 57 | + # the nested_resolution is upscaled|downscaled to output_resolution |
| 58 | + |
| 59 | + nested_resolution = system.config.get_str("gamescope_nested_resolution") |
| 60 | + if nested_resolution: |
| 61 | + nested_width, _, nested_height = nested_resolution.partition("x") |
| 62 | + gamescope_arguments.extend([ |
| 63 | + "-w", nested_width.strip(), |
| 64 | + "-h", nested_height.strip() |
| 65 | + ]) |
| 66 | + |
| 67 | + # Retrieve nested refresh rate. |
| 68 | + # Default is undefined with output refresh forced to 60hz |
| 69 | + # if nested refresh is defined, output refresh is equal to nested refresh |
| 70 | + nested_refresh = system.config.get_str("gamescope_nested_refresh") |
| 71 | + if nested_refresh: |
| 72 | + gamescope_arguments.extend(["-r", str(nested_refresh)]) |
| 73 | + |
| 74 | + # Upscaler type. |
| 75 | + # default is stretched if undefined |
| 76 | + scaler = system.config.get_str("gamescope_scaler") |
| 77 | + if scaler: |
| 78 | + gamescope_arguments.extend(["-S", scaler]) |
| 79 | + |
| 80 | + # Upscaler filter. |
| 81 | + # default is linear if undefined |
| 82 | + filter = system.config.get_str("gamescope_filter") |
| 83 | + if filter: |
| 84 | + gamescope_arguments.extend(["-F", filter]) |
| 85 | + |
| 86 | + # Upscaler sharpness |
| 87 | + sharpness = system.config.get_str("gamescope_sharpness") |
| 88 | + if sharpness: |
| 89 | + gamescope_arguments.extend(["--sharpness", sharpness]) |
| 90 | + |
| 91 | + if system.config.get_bool("gamescope_hdr"): |
| 92 | + sdr_gamut = system.config.get_str("gamescope_sdr_gamut_wideness") |
| 93 | + if sdr_gamut: |
| 94 | + gamescope_arguments.extend(["--sdr-gamut-wideness", sdr_gamut]) |
| 95 | + |
| 96 | + hdr_sdr_nits = system.config.get_str("gamescope_hdr_sdr_content_nits") |
| 97 | + if hdr_sdr_nits: |
| 98 | + gamescope_arguments.extend(["--hdr-sdr-content-nits", hdr_sdr_nits]) |
| 99 | + |
| 100 | + if system.config.get_bool("gamescope_hdr_itm_enabled"): |
| 101 | + gamescope_arguments.append("--hdr-itm-enabled") |
| 102 | + |
| 103 | + hdr_itm_sdr_nits = system.config.get_str("gamescope_hdr_itm_sdr_nits") |
| 104 | + if hdr_itm_sdr_nits: |
| 105 | + gamescope_arguments.extend(["--hdr-itm-sdr-nits", hdr_itm_sdr_nits]) |
| 106 | + |
| 107 | + hdr_itm_target = system.config.get_str("gamescope_hdr_itm_target_nits") |
| 108 | + if hdr_itm_target: |
| 109 | + gamescope_arguments.extend(["--hdr-itm-target-nits", hdr_itm_target]) |
| 110 | + |
| 111 | + #always fullscreen |
| 112 | + gamescope_arguments.append("-f") |
| 113 | + |
| 114 | + # Reshade effect. |
| 115 | + reshade_effect = system.config.get_str("gamescope_reshade_effect") |
| 116 | + if reshade_effect: |
| 117 | + gamescope_arguments.extend(["--reshade-effect", reshade_effect]) |
| 118 | + |
| 119 | + gamescope_arguments.append("--") |
| 120 | + |
| 121 | + command.array = gamescope_arguments + command.array |
0 commit comments