- project.yaml android.ffmpeg : ffmpeg 8.0.1 (LGPL, LAME 3.100 pour le MP3) compilé avec le NDK par ABI (tools/android/build.sh build_ffmpeg, cache ~/.jumphp/android-native/ffmpeg), empaqueté en lib/<abi>/libffmpeg.so (useLegacyPackaging : déballé dans nativeLibraryDir), chemin donné à PHP par JUMPHP_FFMPEG (GameActivity) - PHP Android : proc_open revient (le -include de jumphp-android.h passé à make et non plus à configure : il faisait échouer tous les tests de fonctions d'unistd.h, fork compris) ; --enable-posix ; arbre PHP par clé de build - php-sdl : docker/ext/php-sdl-system.patch (SDL_OpenURL, SDL_Set/Get/HasClipboardText) ; SdlProvider sur Android : Shell::openUrl ouvre le navigateur, Clipboard::copy/paste le presse-papiers - project.yaml android.self_update : JumPHP\Native\AndroidApp::install($apk) (ApkProvider content://, REQUEST_INSTALL_PACKAGES, page « sources inconnues » puis reprise), JUMPHP_ANDROID_VERSION_CODE/_NAME - WinZik : android.ffmpeg et android.self_update ; docs/android-native.md Co-Authored-By: Claude Opus 5.5 <[email protected]>
192 lines
7.5 KiB
PHP
192 lines
7.5 KiB
PHP
<?php
|
|
|
|
namespace JumPHP\Native;
|
|
|
|
use JumPHP\Async\Task;
|
|
|
|
/**
|
|
* Games on the desktop (SDL): SDL's own message box (modal: the game waits while it is open, like any system
|
|
* message box), the clipboard, and the system's file choosers (SystemDialogs: zenity / kdialog / PowerShell, another
|
|
* program: the frame goes on). The rest needs a desktop app (Tauri).
|
|
*
|
|
* The Android app (bin/jumphp android): no programs to start, SDL itself does it (php-sdl with JumPHP's
|
|
* docker/ext/php-sdl-system.patch): SDL_OpenURL (the phone's browser: an Intent ACTION_VIEW) and SDL's clipboard
|
|
* (Android's ClipboardManager). No files to open or show there (openPath / reveal: false).
|
|
*/
|
|
final class SdlProvider extends BaseProvider
|
|
{
|
|
protected array $features = ['message', 'clipboard', 'openUrl', 'openPath', 'reveal'];
|
|
|
|
public function name(): string
|
|
{
|
|
return 'sdl';
|
|
}
|
|
|
|
public function supports(string $feature): bool
|
|
{
|
|
if (self::android()) {
|
|
return in_array($feature, ['message', 'clipboard', 'openUrl'], true);
|
|
}
|
|
return parent::supports($feature) || (in_array($feature, ['open', 'folder', 'save'], true) && SystemDialogs::available());
|
|
}
|
|
|
|
public function open(array $options): Task
|
|
{
|
|
return SystemDialogs::available() ? SystemDialogs::ask($options, 'open') : parent::open($options);
|
|
}
|
|
|
|
public function save(array $options): Task
|
|
{
|
|
return SystemDialogs::available() ? SystemDialogs::ask($options, 'save') : parent::save($options);
|
|
}
|
|
|
|
/** The Android app, its php-sdl with SDL_OpenURL and the clipboard. */
|
|
private static function android(): bool
|
|
{
|
|
return \JumPHP\Kernel\Platform::isAndroid() && function_exists('SDL_OpenURL');
|
|
}
|
|
|
|
public static function isAvailable(): bool
|
|
{
|
|
return function_exists('SDL_ShowSimpleMessageBox');
|
|
}
|
|
|
|
public function message(string $message, string $title, string $kind): Task
|
|
{
|
|
$flags = match ($kind) {
|
|
'error' => \SDL_MESSAGEBOX_ERROR,
|
|
'warning' => \SDL_MESSAGEBOX_WARNING,
|
|
default => \SDL_MESSAGEBOX_INFORMATION,
|
|
};
|
|
$window = null;
|
|
try {
|
|
$window = \JumPHP\Kernel\Engine::get()->sdl->getWindow()?->getWindow();
|
|
} catch (\LogicException) {
|
|
}
|
|
$ok = \SDL_ShowSimpleMessageBox($flags, $title, $message, $window instanceof \SDL_Window ? $window : null) === 0;
|
|
return self::done($ok);
|
|
}
|
|
|
|
/** @var array{copy: ?list<string>, paste: ?list<string>}|null the commands that write and read the clipboard here, found once */
|
|
private static ?array $tools = null;
|
|
|
|
/** @return array{copy: ?list<string>, paste: ?list<string>} */
|
|
private static function tools(): array
|
|
{
|
|
if (self::$tools !== null) {
|
|
return self::$tools;
|
|
}
|
|
$has = static fn (string $cmd): bool => trim((string)@shell_exec((PHP_OS_FAMILY === 'Windows' ? 'where ' : 'command -v ') . escapeshellarg($cmd) . ' 2>' . (PHP_OS_FAMILY === 'Windows' ? 'NUL' : '/dev/null'))) !== '';
|
|
$tools = ['copy' => null, 'paste' => null];
|
|
if (PHP_OS_FAMILY === 'Windows') {
|
|
$tools = ['copy' => ['powershell', '-NoProfile', '-Command', '$t = [Console]::In.ReadToEnd(); Set-Clipboard -Value $t'],
|
|
'paste' => ['powershell', '-NoProfile', '-Command', '[Console]::OutputEncoding = [Text.Encoding]::UTF8; Get-Clipboard -Raw']];
|
|
} elseif (PHP_OS_FAMILY === 'Darwin') {
|
|
$tools = ['copy' => ['pbcopy'], 'paste' => ['pbpaste']];
|
|
} elseif (getenv('WAYLAND_DISPLAY') && $has('wl-copy')) {
|
|
$tools = ['copy' => ['wl-copy'], 'paste' => ['wl-paste', '--no-newline']];
|
|
} elseif ($has('xclip')) {
|
|
$tools = ['copy' => ['xclip', '-selection', 'clipboard', '-i'], 'paste' => ['xclip', '-selection', 'clipboard', '-o']];
|
|
} elseif ($has('xsel')) {
|
|
$tools = ['copy' => ['xsel', '--clipboard', '--input'], 'paste' => ['xsel', '--clipboard', '--output']];
|
|
}
|
|
return self::$tools = $tools;
|
|
}
|
|
|
|
/**
|
|
* The system's own program for an address or a file (the browser, the file manager): xdg-open (Linux), open (macOS),
|
|
* the URL handler of Windows; started in the background, never waited for.
|
|
*/
|
|
public function openUrl(string $url): bool
|
|
{
|
|
if (!preg_match('#^(https?|mailto|file):#i', $url)) {
|
|
return false;
|
|
}
|
|
if (self::android()) {
|
|
return \SDL_OpenURL($url) === 0;
|
|
}
|
|
return self::launch($url);
|
|
}
|
|
|
|
public function openPath(string $path): bool
|
|
{
|
|
if (self::android()) {
|
|
return false;
|
|
}
|
|
return file_exists($path) && self::launch($path);
|
|
}
|
|
|
|
public function reveal(string $path): bool
|
|
{
|
|
if (self::android()) {
|
|
return false;
|
|
}
|
|
return file_exists($path) && self::launch(is_dir($path) ? $path : dirname($path));
|
|
}
|
|
|
|
private static function launch(string $target): bool
|
|
{
|
|
$command = match (PHP_OS_FAMILY) {
|
|
'Windows' => ['rundll32', 'url.dll,FileProtocolHandler', $target],
|
|
'Darwin' => ['open', $target],
|
|
default => ['sh', '-c', 'command -v xdg-open >/dev/null && exec xdg-open "$1" || exec gio open "$1"', 'open', $target],
|
|
};
|
|
$null = PHP_OS_FAMILY === 'Windows' ? 'NUL' : '/dev/null';
|
|
$process = @proc_open($command, [0 => ['file', $null, 'r'], 1 => ['file', $null, 'w'], 2 => ['file', $null, 'w']], $pipes, null, null, ['bypass_shell' => true]);
|
|
if (!is_resource($process)) {
|
|
return false;
|
|
}
|
|
// kept (closing it now would wait for the browser); the ones that ended are closed here
|
|
foreach (self::$launched as $k => $old) {
|
|
if (!proc_get_status($old)['running']) {
|
|
proc_close($old);
|
|
unset(self::$launched[$k]);
|
|
}
|
|
}
|
|
self::$launched[] = $process;
|
|
return true;
|
|
}
|
|
|
|
/** @var list<resource> */
|
|
private static array $launched = [];
|
|
|
|
public function copy(string $text): bool
|
|
{
|
|
if (self::android()) {
|
|
return \SDL_SetClipboardText($text) === 0;
|
|
}
|
|
$command = self::tools()['copy'];
|
|
if ($command === null) {
|
|
return false;
|
|
}
|
|
$process = @proc_open($command, [0 => ['pipe', 'r'], 1 => ['file', PHP_OS_FAMILY === 'Windows' ? 'NUL' : '/dev/null', 'w'], 2 => ['file', PHP_OS_FAMILY === 'Windows' ? 'NUL' : '/dev/null', 'w']], $pipes);
|
|
if (!is_resource($process)) {
|
|
return false;
|
|
}
|
|
fwrite($pipes[0], $text);
|
|
fclose($pipes[0]);
|
|
// xclip / wl-copy keep the text for the other programs by staying in the background: not waited for
|
|
return true;
|
|
}
|
|
|
|
public function paste(): Task
|
|
{
|
|
if (self::android()) {
|
|
return self::done(\SDL_GetClipboardText());
|
|
}
|
|
$command = self::tools()['paste'];
|
|
if ($command === null) {
|
|
return $this->unsupported('paste');
|
|
}
|
|
$process = @proc_open($command, [0 => ['file', PHP_OS_FAMILY === 'Windows' ? 'NUL' : '/dev/null', 'r'], 1 => ['pipe', 'w'], 2 => ['file', PHP_OS_FAMILY === 'Windows' ? 'NUL' : '/dev/null', 'w']], $pipes);
|
|
if (!is_resource($process)) {
|
|
return $this->unsupported('paste');
|
|
}
|
|
stream_set_blocking($pipes[1], true);
|
|
stream_set_timeout($pipes[1], 2);
|
|
$text = (string)stream_get_contents($pipes[1]);
|
|
fclose($pipes[1]);
|
|
proc_close($process);
|
|
return self::done($text);
|
|
}
|
|
}
|