diff --git a/.appveyor.yml b/.appveyor.yml
index c367a4c..db3c3a0 100644
--- a/.appveyor.yml
+++ b/.appveyor.yml
@@ -1,25 +1,51 @@
-image: 'Visual Studio 2017'
+image:
+ - macOS
+ - 'Visual Studio 2019'
+
+branches:
+ only:
+ - master
+ - develop
+ - /^v\d.*$/
+ - /^deploy-test(-.*)?$/
configuration:
- Release
-install:
- - git submodule update --init --recursive
+for:
+ - matrix:
+ only:
+ - image: 'Visual Studio 2019'
-build_script:
- - call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat" x64
- - C:\msys64\usr\bin\bash -lc "cd \"%APPVEYOR_BUILD_FOLDER%\" && scripts/appveyor.sh"
+ install:
+ - git submodule update --init --recursive
-artifacts:
- - path: Chiaki
- name: Chiaki
+ build_script:
+ - call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" x64
+ - C:\msys64\usr\bin\bash -lc "cd \"%APPVEYOR_BUILD_FOLDER%\" && scripts/appveyor-win.sh"
-deploy:
- description: 'Chiaki Binaries'
- provider: GitHub
- auth_token:
- secure: Amvzm3PMM5nv+iFsqaU7TZ9fgyYt/YIrOLV0QMiCyOoUlLRIaiYxWiJ7maTpxhZ9
- artifact: "Chiaki"
- on:
- appveyor_repo_tag: true
+ artifacts:
+ - path: Chiaki
+ name: Chiaki
+ - path: Chiaki-PDB
+ name: Chiaki-PDB
+ - matrix:
+ only:
+ - image: macOS
+
+ install:
+ - git submodule update --init --recursive
+ - sudo pip3 install protobuf
+ - HOMEBREW_NO_AUTO_UPDATE=1 brew install qt@5 opus openssl@1.1 nasm sdl2 protobuf
+ - scripts/build-ffmpeg.sh
+
+ build_script:
+ - export CMAKE_PREFIX_PATH="`pwd`/ffmpeg-prefix;/usr/local/opt/openssl@1.1;/usr/local/opt/qt@5"
+ - scripts/build-common.sh
+ - cp -a build/gui/chiaki.app Chiaki.app
+ - /usr/local/opt/qt@5/bin/macdeployqt Chiaki.app -dmg
+
+ artifacts:
+ - path: Chiaki.dmg
+ name: Chiaki
diff --git a/.builds/android.yml b/.builds/android.yml
new file mode 100644
index 0000000..20cd88b
--- /dev/null
+++ b/.builds/android.yml
@@ -0,0 +1,28 @@
+image: alpine/latest
+
+packages:
+- docker
+
+sources:
+- https://git.sr.ht/~thestr4ng3r/chiaki
+
+artifacts:
+- Chiaki.apk
+- Chiaki.aab
+
+secrets:
+- 163950ff-2ac4-423d-a280-d2d9edbef000
+- f4bce41f-f96b-4633-80d8-0ff5dd74dc2a
+
+tasks:
+- build: |
+ cp -v ~/chiaki-local.properties chiaki/android/local.properties || echo "Secrets not available"
+ sudo service docker start
+ timeout 15 sh -c "until sudo docker info; do sleep 0.5; done"
+ sudo docker run \
+ -v /home/build:/home/build \
+ -u $(id -u):$(id -g) \
+ thestr4ng3r/android:90d826e \
+ /bin/bash -c "cd /home/build/chiaki/android && ./gradlew assembleRelease bundleRelease"
+ cp chiaki/android/app/build/outputs/apk/release/app-release*.apk Chiaki.apk
+ cp chiaki/android/app/build/outputs/bundle/release/app-release*.aab Chiaki.aab
diff --git a/.builds/common.yml b/.builds/common.yml
new file mode 100644
index 0000000..b692b08
--- /dev/null
+++ b/.builds/common.yml
@@ -0,0 +1,55 @@
+
+image: alpine/latest
+
+sources:
+ - https://git.sr.ht/~thestr4ng3r/chiaki
+
+packages:
+ - cmake
+ - ninja
+ - protoc
+ - py3-protobuf
+ - py3-setuptools
+ - opus-dev
+ - qt5-qtbase-dev
+ - qt5-qtsvg-dev
+ - qt5-qtmultimedia-dev
+ - ffmpeg-dev
+ - sdl2-dev
+ - podman
+ - fuse
+ - udev
+ - argp-standalone
+
+artifacts:
+ - chiaki.nro
+ - Chiaki.AppImage
+
+tasks:
+ - setup_podman: |
+ sudo rc-service udev start
+ sudo rc-service cgroups start
+ sudo rc-service fuse start # Fuse for AppImages
+ echo build:100000:65536 | sudo tee /etc/subuid
+ echo build:100000:65536 | sudo tee /etc/subgid
+ # https://www.kernel.org/doc/Documentation/networking/tuntap.txt
+ # for slirp4netns
+ sudo mkdir -p /dev/net
+ sudo mknod /dev/net/tun c 10 200
+ sudo chmod 0666 /dev/net/tun
+ - local_build_and_test: |
+ cd chiaki
+ cmake -Bbuild -GNinja -DCHIAKI_ENABLE_CLI=ON -DCHIAKI_ENABLE_GUI=ON -DCHIAKI_CLI_ARGP_STANDALONE=ON
+ ninja -C build
+ build/test/chiaki-unit
+ - appimage: |
+ cd chiaki
+ scripts/run-podman-build-appimage.sh
+ cp appimage/Chiaki.AppImage ../Chiaki.AppImage
+ - switch: |
+ cd chiaki
+ scripts/switch/run-podman-build-chiaki.sh
+ cp build_switch/switch/chiaki.nro ../chiaki.nro
+ - bullseye: |
+ cd chiaki
+ scripts/run-podman-build-bullseye.sh
diff --git a/.builds/freebsd.yml b/.builds/freebsd.yml
new file mode 100644
index 0000000..72109b5
--- /dev/null
+++ b/.builds/freebsd.yml
@@ -0,0 +1,32 @@
+
+image: freebsd/14.x
+
+sources:
+ - https://git.sr.ht/~thestr4ng3r/chiaki
+
+packages:
+ - cmake
+ - protobuf
+ - py311-setuptools # should not be needed with nanopb >= 0.4.9
+ - py311-protobuf
+ - opus
+ - qt5-core
+ - qt5-qmake
+ - qt5-buildtools
+ - qt5-gui
+ - qt5-widgets
+ - qt5-concurrent
+ - qt5-svg
+ - qt5-opengl
+ - qt5-multimedia
+ - ffmpeg
+ - sdl2
+
+tasks:
+ - build: |
+ cd chiaki
+ mkdir build && cd build
+ cmake ..
+ make -j4
+ test/chiaki-unit
+
diff --git a/.builds/openbsd.yml b/.builds/openbsd.yml
new file mode 100644
index 0000000..e1595ac
--- /dev/null
+++ b/.builds/openbsd.yml
@@ -0,0 +1,26 @@
+
+image: openbsd/latest
+
+sources:
+ - https://git.sr.ht/~thestr4ng3r/chiaki
+
+packages:
+ - cmake
+ - protobuf
+ - py3-setuptools # should not be needed with nanopb >= 0.4.9
+ - py3-protobuf
+ - opus
+ - qtbase
+ - qtsvg
+ - qtmultimedia
+ - ffmpeg
+ - sdl2
+
+tasks:
+ - build: |
+ cd chiaki
+ mkdir build && cd build
+ cmake -DCMAKE_PREFIX_PATH="/usr/local/lib;/usr/local/lib/qt5/cmake" ..
+ make -j4
+ test/chiaki-unit
+
diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 0000000..0f9852d
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1,2 @@
+/test/*.inl linguist-generated=true
+/lib/*.h linguist-language=C
diff --git a/.gitignore b/.gitignore
index 7069241..f30ef42 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,6 +3,8 @@
.idea
build
cmake-build-*
+/build_*
+/build-*
.DS_store
*.AppImage
appdir
@@ -11,5 +13,21 @@ appdir
/SDL2-*
/opus*
/ffmpeg*
+/sdl2-*
/protoc*
/openssl*
+.vs
+CMakeSettings.json
+chiaki.rb
+*.jks
+secret.tar
+keystore-env.sh
+compile_commands.json
+.ccls-cache
+.cache/
+.gdb_history
+chiaki.conf
+/appimage
+.cache/
+/*.app
+/*.dmg
diff --git a/.gitmodules b/.gitmodules
index ce87b05..250f8fe 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -6,7 +6,13 @@
url = https://github.com/nanopb/nanopb.git
[submodule "third-party/jerasure"]
path = third-party/jerasure
- url = https://github.com/thestr4ng3r/jerasure.git
+ url = https://git.sr.ht/~thestr4ng3r/jerasure
[submodule "third-party/gf-complete"]
path = third-party/gf-complete
- url = https://github.com/thestr4ng3r/gf-complete.git
+ url = https://git.sr.ht/~thestr4ng3r/gf-complete
+[submodule "android/app/src/main/cpp/oboe"]
+ path = android/app/src/main/cpp/oboe
+ url = https://github.com/google/oboe
+[submodule "switch/borealis"]
+ path = switch/borealis
+ url = https://git.sr.ht/~thestr4ng3r/borealis
diff --git a/.travis.yml b/.travis.yml
deleted file mode 100644
index eca6016..0000000
--- a/.travis.yml
+++ /dev/null
@@ -1,81 +0,0 @@
-
-language: cpp
-
-matrix:
- include:
- - os: linux
- dist: bionic
- addons:
- apt:
- sources:
- - sourceline: "ppa:beineri/opt-qt-5.12.0-bionic"
- packages:
- - libprotoc-dev
- - protobuf-compiler
- - python3-protobuf
- - libopus-dev
- - qt512base
- - qt512multimedia
- - qt512gamepad
- - qt512svg
- - libgl1-mesa-dev
- - nasm
- - libsdl2-dev
- env:
- - CMAKE_PREFIX_PATH="$TRAVIS_BUILD_DIR/ffmpeg-prefix;/opt/qt512"
- - CMAKE_EXTRA_ARGS="-DCMAKE_INSTALL_PREFIX=/usr"
- - DEPLOY_FILE="Chiaki-x86_64.AppImage"
- after_success:
- - make install DESTDIR=../appdir
- - cd ..
- - wget https://github.com/linuxdeploy/linuxdeploy-plugin-qt/releases/download/continuous/linuxdeploy-x86_64.AppImage && chmod +x linuxdeploy-x86_64.AppImage
- - wget https://github.com/linuxdeploy/linuxdeploy-plugin-qt/releases/download/continuous/linuxdeploy-plugin-qt-x86_64.AppImage && chmod +x linuxdeploy-plugin-qt-x86_64.AppImage
- - source /opt/qt512/bin/qt512-env.sh
- - ./linuxdeploy-x86_64.AppImage --appdir=appdir -e appdir/usr/bin/chiaki -d appdir/usr/share/applications/chiaki.desktop --plugin qt --output appimage
- - mv Chiaki-*-x86_64.AppImage Chiaki-x86_64.AppImage
-
- - os: osx
- osx_image: xcode11
- addons:
- homebrew:
- packages:
- - qt
- - opus
- - openssl@1.1
- - nasm
- - sdl2
- env:
- - CMAKE_PREFIX_PATH="$TRAVIS_BUILD_DIR/ffmpeg-prefix;/usr/local/opt/openssl@1.1;/usr/local/opt/qt"
- - CMAKE_EXTRA_ARGS=""
- - DEPLOY_FILE="Chiaki.dmg"
- after_success:
- - cd ..
- - cp -a build/gui/chiaki.app Chiaki.app
- - /usr/local/opt/qt/bin/macdeployqt Chiaki.app -dmg
-
-install:
- - scripts/build-ffmpeg.sh
-
-script:
- - mkdir build && cd build
- - cmake
- -DCMAKE_BUILD_TYPE=Release
- -DCMAKE_VERBOSE_MAKEFILE=ON
- -DCMAKE_PREFIX_PATH=$CMAKE_PREFIX_PATH
- -DCHIAKI_ENABLE_TESTS=ON
- -DCHIAKI_ENABLE_CLI=OFF
- -DCHIAKI_GUI_ENABLE_QT_GAMEPAD=OFF
- -DCHIAKI_GUI_ENABLE_SDL_GAMECONTROLLER=ON
- $CMAKE_EXTRA_ARGS
- ..
- - make -j4
- - test/chiaki-unit
-
-deploy:
- skip_cleanup: true
- provider: releases
- api_key:
- secure: R7RjLOuGFda05EJeNX2lNG135xKU2w9IQn7p1H1P2zw4zlQMgSBpNRaW8hE408x5KJUjptJTF6QaYYmPWbHlf9VEPFVIcVzSp8YSd2Cdr+GKhmFgWF+fJPBj5y9NNqohwxvK3Nrugh0v6yVQiEYEGF7WArU6dvymSNNTw/EqXtfrOvwUgSf1bDAzQAsXn3E6Ptzf9DrQU8+mOgMSqT/3Wy5207KLmWTtwBWDgkskKwS9OEXk3tDd6U4uT7NFHHmcw+ZjQXRD+yHSHUWYs1oKR4IfgPFxQfEK0Txhkxdf3yj1aNweuk7GGC3cfRaarUfRQpoYqYYCxhTfGZ2b4rVgX3XpssMY7ZmSZHRi/SX08ETXF/c7PZGzr0RPFXZLgAGjgN6O2Dbb9agc3tOUGDUuqKEWX9sALm82WS0FRAFrFLENgMFsj5hu+DKIIkAU2yEsadYKjjhC+q+mTAEkxKKknvM50Xpx3tE1TlP/31Z53v4/NydHIHXPJ72V3mnuoTacwhG2SkGtjMbLCnEZDCtu9C4556oa7Z29cqafv90ZD7lTQMV+ijKvjxgOC9u1GeemmZLofRGDFyYSqKxOpYxxxXGOhs+7FMAdKP00h++MTLwRwIebKQs0fW0XiNKmwushWOUU8sXI1jxTbwe9dPQsspxHRv/mVo6l2vUcBjC19K0=
- file: $DEPLOY_FILE
- on:
- tags: true
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0ae441e..64caaee 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -3,31 +3,199 @@ cmake_minimum_required(VERSION 3.2)
project(chiaki)
+# Like option(), but the value can also be AUTO
+macro(tri_option name desc default)
+ set("${name}" "${default}" CACHE STRING "${desc}")
+ set_property(CACHE "${name}" PROPERTY STRINGS AUTO ON OFF)
+endmacro()
+
option(CHIAKI_ENABLE_TESTS "Enable tests for Chiaki" ON)
option(CHIAKI_ENABLE_CLI "Enable CLI for Chiaki" OFF)
-option(CHIAKI_GUI_ENABLE_QT_GAMEPAD "Use QtGamepad for Input" OFF)
+option(CHIAKI_ENABLE_GUI "Enable Qt GUI" ON)
+option(CHIAKI_ENABLE_ANDROID "Enable Android (Use only as part of the Gradle Project)" OFF)
+option(CHIAKI_ENABLE_BOREALIS "Enable Borealis GUI (For Nintendo Switch or PC)" OFF)
+tri_option(CHIAKI_ENABLE_SETSU "Enable libsetsu for touchpad input from controller" AUTO)
+option(CHIAKI_LIB_ENABLE_OPUS "Use Opus as part of Chiaki Lib" ON)
+if(CHIAKI_ENABLE_GUI OR CHIAKI_ENABLE_BOREALIS)
+ set(CHIAKI_FFMPEG_DEFAULT ON)
+else()
+ set(CHIAKI_FFMPEG_DEFAULT AUTO)
+endif()
+tri_option(CHIAKI_ENABLE_FFMPEG_DECODER "Enable FFMPEG video decoder" ${CHIAKI_FFMPEG_DEFAULT})
+tri_option(CHIAKI_ENABLE_PI_DECODER "Enable Raspberry Pi-specific video decoder (requires libraspberrypi0 and libraspberrypi-doc)" AUTO)
+option(CHIAKI_LIB_ENABLE_MBEDTLS "Use mbedtls instead of OpenSSL as part of Chiaki Lib" OFF)
+option(CHIAKI_LIB_MBEDTLS_EXTERNAL_PROJECT "Fetch Mbed TLS instead of using system-provided libs" OFF)
+option(CHIAKI_LIB_OPENSSL_EXTERNAL_PROJECT "Use OpenSSL as CMake external project" OFF)
option(CHIAKI_GUI_ENABLE_SDL_GAMECONTROLLER "Use SDL Gamecontroller for Input" ON)
+option(CHIAKI_CLI_ARGP_STANDALONE "Search for standalone argp lib for CLI" OFF)
+tri_option(CHIAKI_USE_SYSTEM_JERASURE "Use system-provided jerasure instead of submodule" AUTO)
+tri_option(CHIAKI_USE_SYSTEM_NANOPB "Use system-provided nanopb instead of submodule" AUTO)
-set(CHIAKI_VERSION_MAJOR 1)
-set(CHIAKI_VERSION_MINOR 0)
-set(CHIAKI_VERSION_PATCH 1)
+set(CHIAKI_VERSION_MAJOR 2)
+set(CHIAKI_VERSION_MINOR 2)
+set(CHIAKI_VERSION_PATCH 0)
set(CHIAKI_VERSION ${CHIAKI_VERSION_MAJOR}.${CHIAKI_VERSION_MINOR}.${CHIAKI_VERSION_PATCH})
-list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
+set(CPACK_PACKAGE_NAME "chiaki")
+set(CPACK_PACKAGE_DESCRIPTION "Open Source PS4 remote play client")
+set(CPACK_PACKAGE_VERSION_MAJOR ${CHIAKI_VERSION_MAJOR})
+set(CPACK_PACKAGE_VERSION_MINOR ${CHIAKI_VERSION_MINOR})
+set(CPACK_PACKAGE_VERSION_PATCH ${CHIAKI_VERSION_PATCH})
+set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON)
+set(CPACK_DEBIAN_PACKAGE_DESCRIPTION ${CPACK_PACKAGE_DESCRIPTION})
+set(CPACK_DEBIAN_PACKAGE_SECTION "games")
+include(CPack)
+
+set(CHIAKI_IS_SWITCH ${NSWITCH})
+
+list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake;${CMAKE_CURRENT_SOURCE_DIR}/setsu/cmake")
+
+set(CMAKE_CXX_STANDARD 11)
+
+if(CHIAKI_IS_SWITCH)
+ # force mbedtls as crypto lib
+ set(CHIAKI_LIB_ENABLE_MBEDTLS ON)
+ add_definitions(-D__SWITCH__)
+endif()
+
+if(CHIAKI_USE_SYSTEM_JERASURE)
+ if(CHIAKI_USE_SYSTEM_JERASURE STREQUAL AUTO)
+ find_package(Jerasure QUIET)
+ set(CHIAKI_USE_SYSTEM_JERASURE ${Jerasure_FOUND})
+ else()
+ find_package(Jerasure REQUIRED)
+ set(CHIAKI_USE_SYSTEM_JERASURE ON)
+ endif()
+endif()
+
+find_package(PythonInterp 3 REQUIRED) # Make sure nanopb doesn't find Python 2.7 because Python 2 should just die.
+
+if(CHIAKI_USE_SYSTEM_NANOPB)
+ if(CHIAKI_USE_SYSTEM_NANOPB STREQUAL AUTO)
+ find_package(Nanopb QUIET)
+ set(CHIAKI_USE_SYSTEM_NANOPB ${Nanopb_FOUND})
+ else()
+ find_package(Nanopb REQUIRED)
+ set(CHIAKI_USE_SYSTEM_NANOPB ON)
+ endif()
+endif()
add_subdirectory(third-party)
add_definitions(-DCHIAKI_VERSION_MAJOR=${CHIAKI_VERSION_MAJOR} -DCHIAKI_VERSION_MINOR=${CHIAKI_VERSION_MINOR} -DCHIAKI_VERSION_PATCH=${CHIAKI_VERSION_PATCH} -DCHIAKI_VERSION=\"${CHIAKI_VERSION}\")
+if(CHIAKI_LIB_OPENSSL_EXTERNAL_PROJECT)
+ include(OpenSSLExternalProject)
+endif()
+
+if(CHIAKI_LIB_ENABLE_MBEDTLS)
+ add_definitions(-DCHIAKI_LIB_ENABLE_MBEDTLS)
+ if(CHIAKI_LIB_MBEDTLS_EXTERNAL_PROJECT)
+ set(FETCHCONTENT_QUIET CACHE BOOL FALSE)
+ include(FetchContent)
+ set(ENABLE_TESTING CACHE INTERNAL OFF)
+ set(ENABLE_PROGRAMS CACHE INTERNAL OFF)
+ set(USE_SHARED_MBEDTLS_LIBRARY CACHE INTERNAL OFF)
+ FetchContent_Declare(
+ mbedtls
+ GIT_REPOSITORY https://github.com/Mbed-TLS/mbedtls.git
+ GIT_TAG 8b3f26a5ac38d4fdccbc5c5366229f3e01dafcc0 # v2.28.0
+ GIT_PROGRESS TRUE
+ )
+ FetchContent_MakeAvailable(mbedtls)
+ endif()
+endif()
+
+if(CHIAKI_ENABLE_FFMPEG_DECODER)
+ find_package(FFMPEG COMPONENTS avcodec avutil)
+ if(FFMPEG_FOUND)
+ set(CHIAKI_ENABLE_FFMPEG_DECODER ON)
+ else()
+ if(NOT CHIAKI_ENABLE_FFMPEG_DECODER STREQUAL AUTO)
+ message(FATAL_ERROR "CHIAKI_ENABLE_FFMPEG_DECODER is set to ON, but ffmpeg could not be found.")
+ endif()
+ set(CHIAKI_ENABLE_FFMPEG_DECODER OFF)
+ endif()
+endif()
+
+if(CHIAKI_ENABLE_FFMPEG_DECODER)
+ message(STATUS "FFMPEG Decoder enabled")
+else()
+ message(STATUS "FFMPEG Decoder disabled")
+endif()
+
+if(CHIAKI_ENABLE_PI_DECODER)
+ find_package(ILClient)
+ if(ILClient_FOUND)
+ set(CHIAKI_ENABLE_PI_DECODER ON)
+ else()
+ if(NOT CHIAKI_ENABLE_PI_DECODER STREQUAL AUTO)
+ message(FATAL_ERROR "
+CHIAKI_ENABLE_PI_DECODER is set to ON, but its dependencies (ilclient source and libs) could not be resolved.
+The Raspberry Pi Decoder is only supported on Raspberry Pi OS and requires libraspberrypi0 and libraspberrypi-doc.")
+ endif()
+ set(CHIAKI_ENABLE_PI_DECODER OFF)
+ endif()
+endif()
+
+if(CHIAKI_ENABLE_PI_DECODER)
+ message(STATUS "Pi Decoder enabled")
+else()
+ message(STATUS "Pi Decoder disabled")
+endif()
+
add_subdirectory(lib)
if(CHIAKI_ENABLE_CLI)
add_subdirectory(cli)
endif()
-add_subdirectory(gui)
+if(CHIAKI_ENABLE_GUI AND CHIAKI_GUI_ENABLE_SDL_GAMECONTROLLER)
+ find_package(SDL2 MODULE REQUIRED)
+endif()
+
+if(CHIAKI_ENABLE_SETSU)
+ if(CHIAKI_ENABLE_SETSU STREQUAL AUTO AND SDL2_FOUND AND (SDL2_VERSION_MINOR GREATER 0 OR SDL2_VERSION_PATCH GREATER_EQUAL 14))
+ message(STATUS "SDL version ${SDL2_VERSION} is >= 2.0.14, disabling Setsu")
+ set(CHIAKI_ENABLE_SETSU OFF)
+ else()
+ find_package(Udev QUIET)
+ find_package(Evdev QUIET)
+ if(Udev_FOUND AND Evdev_FOUND)
+ set(CHIAKI_ENABLE_SETSU ON)
+ else()
+ if(NOT CHIAKI_ENABLE_SETSU STREQUAL AUTO)
+ message(FATAL_ERROR "
+ CHIAKI_ENABLE_SETSU is set to ON, but its dependencies (udev and evdev) could not be resolved.
+ Keep in mind that setsu is only supported on Linux!")
+ endif()
+ set(CHIAKI_ENABLE_SETSU OFF)
+ endif()
+ if(CHIAKI_ENABLE_SETSU)
+ add_subdirectory(setsu)
+ endif()
+ endif()
+endif()
+
+if(CHIAKI_ENABLE_SETSU)
+ message(STATUS "Setsu enabled")
+else()
+ message(STATUS "Setsu disabled")
+endif()
+
+if(CHIAKI_ENABLE_GUI)
+ add_subdirectory(gui)
+endif()
if(CHIAKI_ENABLE_TESTS)
enable_testing()
add_subdirectory(test)
endif()
+
+if(CHIAKI_ENABLE_ANDROID)
+ add_subdirectory(android/app)
+endif()
+
+if(CHIAKI_ENABLE_BOREALIS)
+ add_subdirectory(switch)
+endif()
diff --git a/COPYING b/COPYING
index f288702..c3ef0cb 100644
--- a/COPYING
+++ b/COPYING
@@ -1,5 +1,5 @@
- GNU GENERAL PUBLIC LICENSE
- Version 3, 29 June 2007
+ GNU AFFERO GENERAL PUBLIC LICENSE
+ Version 3, 19 November 2007
Copyright (C) 2007 Free Software Foundation, Inc.
Everyone is permitted to copy and distribute verbatim copies
@@ -7,17 +7,15 @@
Preamble
- The GNU General Public License is a free, copyleft license for
-software and other kinds of works.
+ The GNU Affero General Public License is a free, copyleft license for
+software and other kinds of works, specifically designed to ensure
+cooperation with the community in the case of network server software.
The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
-the GNU General Public License is intended to guarantee your freedom to
+our General Public Licenses are intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
-software for all its users. We, the Free Software Foundation, use the
-GNU General Public License for most of our software; it applies also to
-any other work released this way by its authors. You can apply it to
-your programs, too.
+software for all its users.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
@@ -26,44 +24,34 @@ them if you wish), that you receive source code or can get it if you
want it, that you can change the software or use pieces of it in new
free programs, and that you know you can do these things.
- To protect your rights, we need to prevent others from denying you
-these rights or asking you to surrender the rights. Therefore, you have
-certain responsibilities if you distribute copies of the software, or if
-you modify it: responsibilities to respect the freedom of others.
+ Developers that use our General Public Licenses protect your rights
+with two steps: (1) assert copyright on the software, and (2) offer
+you this License which gives you legal permission to copy, distribute
+and/or modify the software.
- For example, if you distribute copies of such a program, whether
-gratis or for a fee, you must pass on to the recipients the same
-freedoms that you received. You must make sure that they, too, receive
-or can get the source code. And you must show them these terms so they
-know their rights.
+ A secondary benefit of defending all users' freedom is that
+improvements made in alternate versions of the program, if they
+receive widespread use, become available for other developers to
+incorporate. Many developers of free software are heartened and
+encouraged by the resulting cooperation. However, in the case of
+software used on network servers, this result may fail to come about.
+The GNU General Public License permits making a modified version and
+letting the public access it on a server without ever releasing its
+source code to the public.
- Developers that use the GNU GPL protect your rights with two steps:
-(1) assert copyright on the software, and (2) offer you this License
-giving you legal permission to copy, distribute and/or modify it.
+ The GNU Affero General Public License is designed specifically to
+ensure that, in such cases, the modified source code becomes available
+to the community. It requires the operator of a network server to
+provide the source code of the modified version running there to the
+users of that server. Therefore, public use of a modified version, on
+a publicly accessible server, gives the public access to the source
+code of the modified version.
- For the developers' and authors' protection, the GPL clearly explains
-that there is no warranty for this free software. For both users' and
-authors' sake, the GPL requires that modified versions be marked as
-changed, so that their problems will not be attributed erroneously to
-authors of previous versions.
-
- Some devices are designed to deny users access to install or run
-modified versions of the software inside them, although the manufacturer
-can do so. This is fundamentally incompatible with the aim of
-protecting users' freedom to change the software. The systematic
-pattern of such abuse occurs in the area of products for individuals to
-use, which is precisely where it is most unacceptable. Therefore, we
-have designed this version of the GPL to prohibit the practice for those
-products. If such problems arise substantially in other domains, we
-stand ready to extend this provision to those domains in future versions
-of the GPL, as needed to protect the freedom of users.
-
- Finally, every program is threatened constantly by software patents.
-States should not allow patents to restrict development and use of
-software on general-purpose computers, but in those that do, we wish to
-avoid the special danger that patents applied to a free program could
-make it effectively proprietary. To prevent this, the GPL assures that
-patents cannot be used to render the program non-free.
+ An older license, called the Affero General Public License and
+published by Affero, was designed to accomplish similar goals. This is
+a different license, not a version of the Affero GPL, but Affero has
+released a new version of the Affero GPL which permits relicensing under
+this license.
The precise terms and conditions for copying, distribution and
modification follow.
@@ -72,7 +60,7 @@ modification follow.
0. Definitions.
- "This License" refers to version 3 of the GNU General Public License.
+ "This License" refers to version 3 of the GNU Affero General Public License.
"Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
@@ -549,35 +537,45 @@ to collect a royalty for further conveying from those to whom you convey
the Program, the only way you could satisfy both those terms and this
License would be to refrain entirely from conveying the Program.
- 13. Use with the GNU Affero General Public License.
+ 13. Remote Network Interaction; Use with the GNU General Public License.
+
+ Notwithstanding any other provision of this License, if you modify the
+Program, your modified version must prominently offer all users
+interacting with it remotely through a computer network (if your version
+supports such interaction) an opportunity to receive the Corresponding
+Source of your version by providing access to the Corresponding Source
+from a network server at no charge, through some standard or customary
+means of facilitating copying of software. This Corresponding Source
+shall include the Corresponding Source for any work covered by version 3
+of the GNU General Public License that is incorporated pursuant to the
+following paragraph.
Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
-under version 3 of the GNU Affero General Public License into a single
+under version 3 of the GNU General Public License into a single
combined work, and to convey the resulting work. The terms of this
License will continue to apply to the part which is the covered work,
-but the special requirements of the GNU Affero General Public License,
-section 13, concerning interaction through a network will apply to the
-combination as such.
+but the work with which it is combined will remain governed by version
+3 of the GNU General Public License.
14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of
-the GNU General Public License from time to time. Such new versions will
-be similar in spirit to the present version, but may differ in detail to
+the GNU Affero General Public License from time to time. Such new versions
+will be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the
-Program specifies that a certain numbered version of the GNU General
+Program specifies that a certain numbered version of the GNU Affero General
Public License "or any later version" applies to it, you have the
option of following the terms and conditions either of that numbered
version or of any later version published by the Free Software
Foundation. If the Program does not specify a version number of the
-GNU General Public License, you may choose any version ever published
+GNU Affero General Public License, you may choose any version ever published
by the Free Software Foundation.
If the Program specifies that a proxy can decide which future
-versions of the GNU General Public License can be used, that proxy's
+versions of the GNU Affero General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you
to choose that version for the Program.
@@ -635,40 +633,40 @@ the "copyright" line and a pointer to where the full notice is found.
Copyright (C)
This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
+ it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
+ GNU Affero General Public License for more details.
- You should have received a copy of the GNU General Public License
+ You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
Also add information on how to contact you by electronic and paper mail.
- If the program does terminal interaction, make it output a short
-notice like this when it starts in an interactive mode:
-
- Copyright (C)
- This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
- This is free software, and you are welcome to redistribute it
- under certain conditions; type `show c' for details.
-
-The hypothetical commands `show w' and `show c' should show the appropriate
-parts of the General Public License. Of course, your program's commands
-might be different; for a GUI interface, you would use an "about box".
+ If your software can interact with users remotely through a computer
+network, you should also make sure that it provides a way for users to
+get its source. For example, if your program is a web application, its
+interface could display a "Source" link that leads users to an archive
+of the code. There are many ways you could offer source, and different
+solutions will be better for different programs; see section 13 for the
+specific requirements.
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
-For more information on this, and how to apply and follow the GNU GPL, see
+For more information on this, and how to apply and follow the GNU AGPL, see
.
- The GNU General Public License does not permit incorporating your program
-into proprietary programs. If your program is a subroutine library, you
-may consider it more useful to permit linking proprietary applications with
-the library. If this is what you want to do, use the GNU Lesser General
-Public License instead of this License. But first, please read
-.
+Additional permission under GNU AGPL version 3 section 7
+
+If you modify this program, or any covered work, by linking or
+combining it with the OpenSSL project's OpenSSL library (or a
+modified version of that library), containing parts covered by the
+terms of the OpenSSL or SSLeay licenses, the Free Software Foundation
+grants you additional permission to convey the resulting work.
+Corresponding Source for a non-source form of such a combination
+shall include the source code for the parts of OpenSSL used as well
+as that of the covered work.
diff --git a/LICENSES/AGPL-3.0-only-OpenSSL.txt b/LICENSES/AGPL-3.0-only-OpenSSL.txt
new file mode 100644
index 0000000..c3ef0cb
--- /dev/null
+++ b/LICENSES/AGPL-3.0-only-OpenSSL.txt
@@ -0,0 +1,672 @@
+ GNU AFFERO GENERAL PUBLIC LICENSE
+ Version 3, 19 November 2007
+
+ Copyright (C) 2007 Free Software Foundation, Inc.
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+ Preamble
+
+ The GNU Affero General Public License is a free, copyleft license for
+software and other kinds of works, specifically designed to ensure
+cooperation with the community in the case of network server software.
+
+ The licenses for most software and other practical works are designed
+to take away your freedom to share and change the works. By contrast,
+our General Public Licenses are intended to guarantee your freedom to
+share and change all versions of a program--to make sure it remains free
+software for all its users.
+
+ When we speak of free software, we are referring to freedom, not
+price. Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+them if you wish), that you receive source code or can get it if you
+want it, that you can change the software or use pieces of it in new
+free programs, and that you know you can do these things.
+
+ Developers that use our General Public Licenses protect your rights
+with two steps: (1) assert copyright on the software, and (2) offer
+you this License which gives you legal permission to copy, distribute
+and/or modify the software.
+
+ A secondary benefit of defending all users' freedom is that
+improvements made in alternate versions of the program, if they
+receive widespread use, become available for other developers to
+incorporate. Many developers of free software are heartened and
+encouraged by the resulting cooperation. However, in the case of
+software used on network servers, this result may fail to come about.
+The GNU General Public License permits making a modified version and
+letting the public access it on a server without ever releasing its
+source code to the public.
+
+ The GNU Affero General Public License is designed specifically to
+ensure that, in such cases, the modified source code becomes available
+to the community. It requires the operator of a network server to
+provide the source code of the modified version running there to the
+users of that server. Therefore, public use of a modified version, on
+a publicly accessible server, gives the public access to the source
+code of the modified version.
+
+ An older license, called the Affero General Public License and
+published by Affero, was designed to accomplish similar goals. This is
+a different license, not a version of the Affero GPL, but Affero has
+released a new version of the Affero GPL which permits relicensing under
+this license.
+
+ The precise terms and conditions for copying, distribution and
+modification follow.
+
+ TERMS AND CONDITIONS
+
+ 0. Definitions.
+
+ "This License" refers to version 3 of the GNU Affero General Public License.
+
+ "Copyright" also means copyright-like laws that apply to other kinds of
+works, such as semiconductor masks.
+
+ "The Program" refers to any copyrightable work licensed under this
+License. Each licensee is addressed as "you". "Licensees" and
+"recipients" may be individuals or organizations.
+
+ To "modify" a work means to copy from or adapt all or part of the work
+in a fashion requiring copyright permission, other than the making of an
+exact copy. The resulting work is called a "modified version" of the
+earlier work or a work "based on" the earlier work.
+
+ A "covered work" means either the unmodified Program or a work based
+on the Program.
+
+ To "propagate" a work means to do anything with it that, without
+permission, would make you directly or secondarily liable for
+infringement under applicable copyright law, except executing it on a
+computer or modifying a private copy. Propagation includes copying,
+distribution (with or without modification), making available to the
+public, and in some countries other activities as well.
+
+ To "convey" a work means any kind of propagation that enables other
+parties to make or receive copies. Mere interaction with a user through
+a computer network, with no transfer of a copy, is not conveying.
+
+ An interactive user interface displays "Appropriate Legal Notices"
+to the extent that it includes a convenient and prominently visible
+feature that (1) displays an appropriate copyright notice, and (2)
+tells the user that there is no warranty for the work (except to the
+extent that warranties are provided), that licensees may convey the
+work under this License, and how to view a copy of this License. If
+the interface presents a list of user commands or options, such as a
+menu, a prominent item in the list meets this criterion.
+
+ 1. Source Code.
+
+ The "source code" for a work means the preferred form of the work
+for making modifications to it. "Object code" means any non-source
+form of a work.
+
+ A "Standard Interface" means an interface that either is an official
+standard defined by a recognized standards body, or, in the case of
+interfaces specified for a particular programming language, one that
+is widely used among developers working in that language.
+
+ The "System Libraries" of an executable work include anything, other
+than the work as a whole, that (a) is included in the normal form of
+packaging a Major Component, but which is not part of that Major
+Component, and (b) serves only to enable use of the work with that
+Major Component, or to implement a Standard Interface for which an
+implementation is available to the public in source code form. A
+"Major Component", in this context, means a major essential component
+(kernel, window system, and so on) of the specific operating system
+(if any) on which the executable work runs, or a compiler used to
+produce the work, or an object code interpreter used to run it.
+
+ The "Corresponding Source" for a work in object code form means all
+the source code needed to generate, install, and (for an executable
+work) run the object code and to modify the work, including scripts to
+control those activities. However, it does not include the work's
+System Libraries, or general-purpose tools or generally available free
+programs which are used unmodified in performing those activities but
+which are not part of the work. For example, Corresponding Source
+includes interface definition files associated with source files for
+the work, and the source code for shared libraries and dynamically
+linked subprograms that the work is specifically designed to require,
+such as by intimate data communication or control flow between those
+subprograms and other parts of the work.
+
+ The Corresponding Source need not include anything that users
+can regenerate automatically from other parts of the Corresponding
+Source.
+
+ The Corresponding Source for a work in source code form is that
+same work.
+
+ 2. Basic Permissions.
+
+ All rights granted under this License are granted for the term of
+copyright on the Program, and are irrevocable provided the stated
+conditions are met. This License explicitly affirms your unlimited
+permission to run the unmodified Program. The output from running a
+covered work is covered by this License only if the output, given its
+content, constitutes a covered work. This License acknowledges your
+rights of fair use or other equivalent, as provided by copyright law.
+
+ You may make, run and propagate covered works that you do not
+convey, without conditions so long as your license otherwise remains
+in force. You may convey covered works to others for the sole purpose
+of having them make modifications exclusively for you, or provide you
+with facilities for running those works, provided that you comply with
+the terms of this License in conveying all material for which you do
+not control copyright. Those thus making or running the covered works
+for you must do so exclusively on your behalf, under your direction
+and control, on terms that prohibit them from making any copies of
+your copyrighted material outside their relationship with you.
+
+ Conveying under any other circumstances is permitted solely under
+the conditions stated below. Sublicensing is not allowed; section 10
+makes it unnecessary.
+
+ 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
+
+ No covered work shall be deemed part of an effective technological
+measure under any applicable law fulfilling obligations under article
+11 of the WIPO copyright treaty adopted on 20 December 1996, or
+similar laws prohibiting or restricting circumvention of such
+measures.
+
+ When you convey a covered work, you waive any legal power to forbid
+circumvention of technological measures to the extent such circumvention
+is effected by exercising rights under this License with respect to
+the covered work, and you disclaim any intention to limit operation or
+modification of the work as a means of enforcing, against the work's
+users, your or third parties' legal rights to forbid circumvention of
+technological measures.
+
+ 4. Conveying Verbatim Copies.
+
+ You may convey verbatim copies of the Program's source code as you
+receive it, in any medium, provided that you conspicuously and
+appropriately publish on each copy an appropriate copyright notice;
+keep intact all notices stating that this License and any
+non-permissive terms added in accord with section 7 apply to the code;
+keep intact all notices of the absence of any warranty; and give all
+recipients a copy of this License along with the Program.
+
+ You may charge any price or no price for each copy that you convey,
+and you may offer support or warranty protection for a fee.
+
+ 5. Conveying Modified Source Versions.
+
+ You may convey a work based on the Program, or the modifications to
+produce it from the Program, in the form of source code under the
+terms of section 4, provided that you also meet all of these conditions:
+
+ a) The work must carry prominent notices stating that you modified
+ it, and giving a relevant date.
+
+ b) The work must carry prominent notices stating that it is
+ released under this License and any conditions added under section
+ 7. This requirement modifies the requirement in section 4 to
+ "keep intact all notices".
+
+ c) You must license the entire work, as a whole, under this
+ License to anyone who comes into possession of a copy. This
+ License will therefore apply, along with any applicable section 7
+ additional terms, to the whole of the work, and all its parts,
+ regardless of how they are packaged. This License gives no
+ permission to license the work in any other way, but it does not
+ invalidate such permission if you have separately received it.
+
+ d) If the work has interactive user interfaces, each must display
+ Appropriate Legal Notices; however, if the Program has interactive
+ interfaces that do not display Appropriate Legal Notices, your
+ work need not make them do so.
+
+ A compilation of a covered work with other separate and independent
+works, which are not by their nature extensions of the covered work,
+and which are not combined with it such as to form a larger program,
+in or on a volume of a storage or distribution medium, is called an
+"aggregate" if the compilation and its resulting copyright are not
+used to limit the access or legal rights of the compilation's users
+beyond what the individual works permit. Inclusion of a covered work
+in an aggregate does not cause this License to apply to the other
+parts of the aggregate.
+
+ 6. Conveying Non-Source Forms.
+
+ You may convey a covered work in object code form under the terms
+of sections 4 and 5, provided that you also convey the
+machine-readable Corresponding Source under the terms of this License,
+in one of these ways:
+
+ a) Convey the object code in, or embodied in, a physical product
+ (including a physical distribution medium), accompanied by the
+ Corresponding Source fixed on a durable physical medium
+ customarily used for software interchange.
+
+ b) Convey the object code in, or embodied in, a physical product
+ (including a physical distribution medium), accompanied by a
+ written offer, valid for at least three years and valid for as
+ long as you offer spare parts or customer support for that product
+ model, to give anyone who possesses the object code either (1) a
+ copy of the Corresponding Source for all the software in the
+ product that is covered by this License, on a durable physical
+ medium customarily used for software interchange, for a price no
+ more than your reasonable cost of physically performing this
+ conveying of source, or (2) access to copy the
+ Corresponding Source from a network server at no charge.
+
+ c) Convey individual copies of the object code with a copy of the
+ written offer to provide the Corresponding Source. This
+ alternative is allowed only occasionally and noncommercially, and
+ only if you received the object code with such an offer, in accord
+ with subsection 6b.
+
+ d) Convey the object code by offering access from a designated
+ place (gratis or for a charge), and offer equivalent access to the
+ Corresponding Source in the same way through the same place at no
+ further charge. You need not require recipients to copy the
+ Corresponding Source along with the object code. If the place to
+ copy the object code is a network server, the Corresponding Source
+ may be on a different server (operated by you or a third party)
+ that supports equivalent copying facilities, provided you maintain
+ clear directions next to the object code saying where to find the
+ Corresponding Source. Regardless of what server hosts the
+ Corresponding Source, you remain obligated to ensure that it is
+ available for as long as needed to satisfy these requirements.
+
+ e) Convey the object code using peer-to-peer transmission, provided
+ you inform other peers where the object code and Corresponding
+ Source of the work are being offered to the general public at no
+ charge under subsection 6d.
+
+ A separable portion of the object code, whose source code is excluded
+from the Corresponding Source as a System Library, need not be
+included in conveying the object code work.
+
+ A "User Product" is either (1) a "consumer product", which means any
+tangible personal property which is normally used for personal, family,
+or household purposes, or (2) anything designed or sold for incorporation
+into a dwelling. In determining whether a product is a consumer product,
+doubtful cases shall be resolved in favor of coverage. For a particular
+product received by a particular user, "normally used" refers to a
+typical or common use of that class of product, regardless of the status
+of the particular user or of the way in which the particular user
+actually uses, or expects or is expected to use, the product. A product
+is a consumer product regardless of whether the product has substantial
+commercial, industrial or non-consumer uses, unless such uses represent
+the only significant mode of use of the product.
+
+ "Installation Information" for a User Product means any methods,
+procedures, authorization keys, or other information required to install
+and execute modified versions of a covered work in that User Product from
+a modified version of its Corresponding Source. The information must
+suffice to ensure that the continued functioning of the modified object
+code is in no case prevented or interfered with solely because
+modification has been made.
+
+ If you convey an object code work under this section in, or with, or
+specifically for use in, a User Product, and the conveying occurs as
+part of a transaction in which the right of possession and use of the
+User Product is transferred to the recipient in perpetuity or for a
+fixed term (regardless of how the transaction is characterized), the
+Corresponding Source conveyed under this section must be accompanied
+by the Installation Information. But this requirement does not apply
+if neither you nor any third party retains the ability to install
+modified object code on the User Product (for example, the work has
+been installed in ROM).
+
+ The requirement to provide Installation Information does not include a
+requirement to continue to provide support service, warranty, or updates
+for a work that has been modified or installed by the recipient, or for
+the User Product in which it has been modified or installed. Access to a
+network may be denied when the modification itself materially and
+adversely affects the operation of the network or violates the rules and
+protocols for communication across the network.
+
+ Corresponding Source conveyed, and Installation Information provided,
+in accord with this section must be in a format that is publicly
+documented (and with an implementation available to the public in
+source code form), and must require no special password or key for
+unpacking, reading or copying.
+
+ 7. Additional Terms.
+
+ "Additional permissions" are terms that supplement the terms of this
+License by making exceptions from one or more of its conditions.
+Additional permissions that are applicable to the entire Program shall
+be treated as though they were included in this License, to the extent
+that they are valid under applicable law. If additional permissions
+apply only to part of the Program, that part may be used separately
+under those permissions, but the entire Program remains governed by
+this License without regard to the additional permissions.
+
+ When you convey a copy of a covered work, you may at your option
+remove any additional permissions from that copy, or from any part of
+it. (Additional permissions may be written to require their own
+removal in certain cases when you modify the work.) You may place
+additional permissions on material, added by you to a covered work,
+for which you have or can give appropriate copyright permission.
+
+ Notwithstanding any other provision of this License, for material you
+add to a covered work, you may (if authorized by the copyright holders of
+that material) supplement the terms of this License with terms:
+
+ a) Disclaiming warranty or limiting liability differently from the
+ terms of sections 15 and 16 of this License; or
+
+ b) Requiring preservation of specified reasonable legal notices or
+ author attributions in that material or in the Appropriate Legal
+ Notices displayed by works containing it; or
+
+ c) Prohibiting misrepresentation of the origin of that material, or
+ requiring that modified versions of such material be marked in
+ reasonable ways as different from the original version; or
+
+ d) Limiting the use for publicity purposes of names of licensors or
+ authors of the material; or
+
+ e) Declining to grant rights under trademark law for use of some
+ trade names, trademarks, or service marks; or
+
+ f) Requiring indemnification of licensors and authors of that
+ material by anyone who conveys the material (or modified versions of
+ it) with contractual assumptions of liability to the recipient, for
+ any liability that these contractual assumptions directly impose on
+ those licensors and authors.
+
+ All other non-permissive additional terms are considered "further
+restrictions" within the meaning of section 10. If the Program as you
+received it, or any part of it, contains a notice stating that it is
+governed by this License along with a term that is a further
+restriction, you may remove that term. If a license document contains
+a further restriction but permits relicensing or conveying under this
+License, you may add to a covered work material governed by the terms
+of that license document, provided that the further restriction does
+not survive such relicensing or conveying.
+
+ If you add terms to a covered work in accord with this section, you
+must place, in the relevant source files, a statement of the
+additional terms that apply to those files, or a notice indicating
+where to find the applicable terms.
+
+ Additional terms, permissive or non-permissive, may be stated in the
+form of a separately written license, or stated as exceptions;
+the above requirements apply either way.
+
+ 8. Termination.
+
+ You may not propagate or modify a covered work except as expressly
+provided under this License. Any attempt otherwise to propagate or
+modify it is void, and will automatically terminate your rights under
+this License (including any patent licenses granted under the third
+paragraph of section 11).
+
+ However, if you cease all violation of this License, then your
+license from a particular copyright holder is reinstated (a)
+provisionally, unless and until the copyright holder explicitly and
+finally terminates your license, and (b) permanently, if the copyright
+holder fails to notify you of the violation by some reasonable means
+prior to 60 days after the cessation.
+
+ Moreover, your license from a particular copyright holder is
+reinstated permanently if the copyright holder notifies you of the
+violation by some reasonable means, this is the first time you have
+received notice of violation of this License (for any work) from that
+copyright holder, and you cure the violation prior to 30 days after
+your receipt of the notice.
+
+ Termination of your rights under this section does not terminate the
+licenses of parties who have received copies or rights from you under
+this License. If your rights have been terminated and not permanently
+reinstated, you do not qualify to receive new licenses for the same
+material under section 10.
+
+ 9. Acceptance Not Required for Having Copies.
+
+ You are not required to accept this License in order to receive or
+run a copy of the Program. Ancillary propagation of a covered work
+occurring solely as a consequence of using peer-to-peer transmission
+to receive a copy likewise does not require acceptance. However,
+nothing other than this License grants you permission to propagate or
+modify any covered work. These actions infringe copyright if you do
+not accept this License. Therefore, by modifying or propagating a
+covered work, you indicate your acceptance of this License to do so.
+
+ 10. Automatic Licensing of Downstream Recipients.
+
+ Each time you convey a covered work, the recipient automatically
+receives a license from the original licensors, to run, modify and
+propagate that work, subject to this License. You are not responsible
+for enforcing compliance by third parties with this License.
+
+ An "entity transaction" is a transaction transferring control of an
+organization, or substantially all assets of one, or subdividing an
+organization, or merging organizations. If propagation of a covered
+work results from an entity transaction, each party to that
+transaction who receives a copy of the work also receives whatever
+licenses to the work the party's predecessor in interest had or could
+give under the previous paragraph, plus a right to possession of the
+Corresponding Source of the work from the predecessor in interest, if
+the predecessor has it or can get it with reasonable efforts.
+
+ You may not impose any further restrictions on the exercise of the
+rights granted or affirmed under this License. For example, you may
+not impose a license fee, royalty, or other charge for exercise of
+rights granted under this License, and you may not initiate litigation
+(including a cross-claim or counterclaim in a lawsuit) alleging that
+any patent claim is infringed by making, using, selling, offering for
+sale, or importing the Program or any portion of it.
+
+ 11. Patents.
+
+ A "contributor" is a copyright holder who authorizes use under this
+License of the Program or a work on which the Program is based. The
+work thus licensed is called the contributor's "contributor version".
+
+ A contributor's "essential patent claims" are all patent claims
+owned or controlled by the contributor, whether already acquired or
+hereafter acquired, that would be infringed by some manner, permitted
+by this License, of making, using, or selling its contributor version,
+but do not include claims that would be infringed only as a
+consequence of further modification of the contributor version. For
+purposes of this definition, "control" includes the right to grant
+patent sublicenses in a manner consistent with the requirements of
+this License.
+
+ Each contributor grants you a non-exclusive, worldwide, royalty-free
+patent license under the contributor's essential patent claims, to
+make, use, sell, offer for sale, import and otherwise run, modify and
+propagate the contents of its contributor version.
+
+ In the following three paragraphs, a "patent license" is any express
+agreement or commitment, however denominated, not to enforce a patent
+(such as an express permission to practice a patent or covenant not to
+sue for patent infringement). To "grant" such a patent license to a
+party means to make such an agreement or commitment not to enforce a
+patent against the party.
+
+ If you convey a covered work, knowingly relying on a patent license,
+and the Corresponding Source of the work is not available for anyone
+to copy, free of charge and under the terms of this License, through a
+publicly available network server or other readily accessible means,
+then you must either (1) cause the Corresponding Source to be so
+available, or (2) arrange to deprive yourself of the benefit of the
+patent license for this particular work, or (3) arrange, in a manner
+consistent with the requirements of this License, to extend the patent
+license to downstream recipients. "Knowingly relying" means you have
+actual knowledge that, but for the patent license, your conveying the
+covered work in a country, or your recipient's use of the covered work
+in a country, would infringe one or more identifiable patents in that
+country that you have reason to believe are valid.
+
+ If, pursuant to or in connection with a single transaction or
+arrangement, you convey, or propagate by procuring conveyance of, a
+covered work, and grant a patent license to some of the parties
+receiving the covered work authorizing them to use, propagate, modify
+or convey a specific copy of the covered work, then the patent license
+you grant is automatically extended to all recipients of the covered
+work and works based on it.
+
+ A patent license is "discriminatory" if it does not include within
+the scope of its coverage, prohibits the exercise of, or is
+conditioned on the non-exercise of one or more of the rights that are
+specifically granted under this License. You may not convey a covered
+work if you are a party to an arrangement with a third party that is
+in the business of distributing software, under which you make payment
+to the third party based on the extent of your activity of conveying
+the work, and under which the third party grants, to any of the
+parties who would receive the covered work from you, a discriminatory
+patent license (a) in connection with copies of the covered work
+conveyed by you (or copies made from those copies), or (b) primarily
+for and in connection with specific products or compilations that
+contain the covered work, unless you entered into that arrangement,
+or that patent license was granted, prior to 28 March 2007.
+
+ Nothing in this License shall be construed as excluding or limiting
+any implied license or other defenses to infringement that may
+otherwise be available to you under applicable patent law.
+
+ 12. No Surrender of Others' Freedom.
+
+ If conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License. If you cannot convey a
+covered work so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you may
+not convey it at all. For example, if you agree to terms that obligate you
+to collect a royalty for further conveying from those to whom you convey
+the Program, the only way you could satisfy both those terms and this
+License would be to refrain entirely from conveying the Program.
+
+ 13. Remote Network Interaction; Use with the GNU General Public License.
+
+ Notwithstanding any other provision of this License, if you modify the
+Program, your modified version must prominently offer all users
+interacting with it remotely through a computer network (if your version
+supports such interaction) an opportunity to receive the Corresponding
+Source of your version by providing access to the Corresponding Source
+from a network server at no charge, through some standard or customary
+means of facilitating copying of software. This Corresponding Source
+shall include the Corresponding Source for any work covered by version 3
+of the GNU General Public License that is incorporated pursuant to the
+following paragraph.
+
+ Notwithstanding any other provision of this License, you have
+permission to link or combine any covered work with a work licensed
+under version 3 of the GNU General Public License into a single
+combined work, and to convey the resulting work. The terms of this
+License will continue to apply to the part which is the covered work,
+but the work with which it is combined will remain governed by version
+3 of the GNU General Public License.
+
+ 14. Revised Versions of this License.
+
+ The Free Software Foundation may publish revised and/or new versions of
+the GNU Affero General Public License from time to time. Such new versions
+will be similar in spirit to the present version, but may differ in detail to
+address new problems or concerns.
+
+ Each version is given a distinguishing version number. If the
+Program specifies that a certain numbered version of the GNU Affero General
+Public License "or any later version" applies to it, you have the
+option of following the terms and conditions either of that numbered
+version or of any later version published by the Free Software
+Foundation. If the Program does not specify a version number of the
+GNU Affero General Public License, you may choose any version ever published
+by the Free Software Foundation.
+
+ If the Program specifies that a proxy can decide which future
+versions of the GNU Affero General Public License can be used, that proxy's
+public statement of acceptance of a version permanently authorizes you
+to choose that version for the Program.
+
+ Later license versions may give you additional or different
+permissions. However, no additional obligations are imposed on any
+author or copyright holder as a result of your choosing to follow a
+later version.
+
+ 15. Disclaimer of Warranty.
+
+ THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
+APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
+HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
+OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
+THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
+IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
+ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
+
+ 16. Limitation of Liability.
+
+ IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
+WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
+THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
+GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
+USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
+DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
+PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
+EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGES.
+
+ 17. Interpretation of Sections 15 and 16.
+
+ If the disclaimer of warranty and limitation of liability provided
+above cannot be given local legal effect according to their terms,
+reviewing courts shall apply local law that most closely approximates
+an absolute waiver of all civil liability in connection with the
+Program, unless a warranty or assumption of liability accompanies a
+copy of the Program in return for a fee.
+
+ END OF TERMS AND CONDITIONS
+
+ How to Apply These Terms to Your New Programs
+
+ If you develop a new program, and you want it to be of the greatest
+possible use to the public, the best way to achieve this is to make it
+free software which everyone can redistribute and change under these terms.
+
+ To do so, attach the following notices to the program. It is safest
+to attach them to the start of each source file to most effectively
+state the exclusion of warranty; and each file should have at least
+the "copyright" line and a pointer to where the full notice is found.
+
+
+ Copyright (C)
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see .
+
+Also add information on how to contact you by electronic and paper mail.
+
+ If your software can interact with users remotely through a computer
+network, you should also make sure that it provides a way for users to
+get its source. For example, if your program is a web application, its
+interface could display a "Source" link that leads users to an archive
+of the code. There are many ways you could offer source, and different
+solutions will be better for different programs; see section 13 for the
+specific requirements.
+
+ You should also get your employer (if you work as a programmer) or school,
+if any, to sign a "copyright disclaimer" for the program, if necessary.
+For more information on this, and how to apply and follow the GNU AGPL, see
+.
+
+Additional permission under GNU AGPL version 3 section 7
+
+If you modify this program, or any covered work, by linking or
+combining it with the OpenSSL project's OpenSSL library (or a
+modified version of that library), containing parts covered by the
+terms of the OpenSSL or SSLeay licenses, the Free Software Foundation
+grants you additional permission to convey the resulting work.
+Corresponding Source for a non-source form of such a combination
+shall include the source code for the parts of OpenSSL used as well
+as that of the covered work.
diff --git a/README.md b/README.md
index 11c5435..1a94b66 100644
--- a/README.md
+++ b/README.md
@@ -5,68 +5,110 @@
**Disclaimer:** This project is not endorsed or certified by Sony Interactive Entertainment LLC.
-[](https://travis-ci.com/thestr4ng3r/chiaki) [](https://ci.appveyor.com/project/thestr4ng3r/chiaki)
+[](https://ci.appveyor.com/project/thestr4ng3r/chiaki) [](https://builds.sr.ht/~thestr4ng3r/chiaki?)
-Chiaki is a Free and Open Source Software Client for PlayStation 4 Remote Play
-for Linux, macOS, Windows and potentially even more platforms.
+Chiaki is a Free and Open Source Software Client for PlayStation 4 and PlayStation 5 Remote Play
+for Linux, FreeBSD, OpenBSD, NetBSD, Android, macOS, Windows, Nintendo Switch and potentially even more platforms.
+
+## Project Status and Contributing
+
+As all relevant features are implemented, this project is considered to be finished and in maintenance mode only.
+No major updates are planned and contributions are only accepted in special cases such as security issues.
+The objective is to keep a stable base and not break existing support for less mainstream platforms such as BSDs.
+
+**For a more active, fast moving and community-oriented project, refer
+to [chiaki-ng](https://streetpea.github.io/chiaki-ng/) ("next generation").
+If you would like to contribute, this will likely also be the best place to do so.**

-## Features
+## Installing
-Everything necessary for a full streaming session, including the initial
-registration and wakeup of the console, is supported.
-The following features however are yet to be implemented:
-* Congestion Control
-* H264 Error Concealment (FEC and active error recovery however are implemented)
-* Make the console send in higher Bitrate (if possible)
-* Touchpad support (Triggering the Touchpad Button is currently possible by pressing `T` on the keyboard)
-* Configurable Keybindings
+You can either download a pre-built release or build Chiaki from source.
-## Downloading a Release
+### Downloading a Release
-Builds are provided for Linux, macOS and Windows. You can find them [here](https://github.com/thestr4ng3r/chiaki/releases).
+Builds are provided for Linux, Android, macOS, Nintendo Switch and Windows.
+
+You can download them [here](https://git.sr.ht/~thestr4ng3r/chiaki/refs).
* **Linux**: The provided file is an [AppImage](https://appimage.org/). Simply make it executable (`chmod +x .AppImage`) and run it.
+* **Android**: Install from [F-Droid](https://f-droid.org/packages/com.metallic.chiaki/) or download the APK from Sourcehut.
* **macOS**: Drag the application from the `.dmg` into your Applications folder.
* **Windows**: Extract the `.zip` file and execute `chiaki.exe`.
+* **Switch**: Download the `.nro` file and copy it into the `switch/` directory on your SD card.
-## Building from Source
+### Building from Source
-Dependencies are CMake, Qt 5 with QtMultimedia, QtOpenGL and QtSvg, FFMPEG (libavcodec with H264 is enough), libopus, OpenSSL 1.1,
-protoc and the protobuf Python library (only used during compilation for Nanopb).
-Then, Chiaki builds just like any other CMake project:
+Dependencies are CMake, Qt 5 with QtMultimedia, QtOpenGL and QtSvg, FFMPEG (libavcodec with H264 is enough), libopus, OpenSSL 1.1, SDL 2,
+protoc and the protobuf Python library (only used during compilation for Nanopb). Then, Chiaki builds just like any other CMake project:
```
+git submodule update --init
mkdir build && cd build
cmake ..
make
```
+For more detailed platform-specific instructions, see [doc/platform-build.md](doc/platform-build.md) or [switch/](./switch/README.md) for Nintendo Switch.
+
+## Usage
+
+If your Console is on your local network, is turned on or in standby mode and does not have Discovery explicitly disabled, Chiaki should find it.
+Otherwise, you can add it manually.
+To do so, click the "+" icon in the top right, and enter your Console's IP address.
+
+You will then need to register your Console with Chiaki. You will need two more pieces of information to do this.
+
+### Obtaining your PSN AccountID
+
+Starting with PS4 7.0, it is necessary to use a so-called "AccountID" as opposed to the "Online-ID" for registration (streaming itself did not change).
+This ID seems to be a unique identifier for a PSN Account and it can be obtained from the PSN after logging in using OAuth.
+A Python 3 script which does this is provided in [scripts/psn-account-id.py](scripts/psn-account-id.py).
+Simply run it in a terminal and follow the instructions. Once you know your ID, write it down. You will likely never have to do this process again.
+
+### Obtaining a Registration PIN
+
+To register a Console with a PIN, it must be put into registration mode. To do this on a PS4, simply go to:
+Settings -> Remote Play -> Add Device, or on a PS5: Settings -> System -> Remote Play -> Link Device.
+
+You can now double-click your Console in Chiaki's main window to start Remote Play.
+
## Acknowledgements
This project has only been made possible because of the following Open Source projects:
-[radare2](https://github.com/radare/radare2),
-[Cutter](https://cutter.re/),
-[Frida](https://www.frida.re/) and
-[x64dbg](https://x64dbg.com/).
+[Rizin](https://rizin.re),
+[Cutter](https://cutter.re),
+[Frida](https://www.frida.re) and
+[x64dbg](https://x64dbg.com).
Also thanks to [delroth](https://github.com/delroth) for analyzing the registration and wakeup protocol,
+[grill2010](https://github.com/grill2010) for analyzing the PSN's OAuth Login,
as well as a huge thank you to [FioraAeterna](https://github.com/FioraAeterna) for giving me some
extremely helpful information about FEC and error correction.
## About
-Created by Florian Märkl.
+Created by Florian Märkl
This program is free software: you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation, either version 3 of the License, or
-(at your option) any later version.
+it under the terms of the GNU Affero General Public License version 3
+as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU General Public License for more details.
+GNU Affero General Public License for more details.
-You should have received a copy of the GNU General Public License
-along with this program. If not, see .
\ No newline at end of file
+You should have received a copy of the GNU Affero General Public License
+along with this program. If not, see .
+
+Additional permission under GNU AGPL version 3 section 7
+
+If you modify this program, or any covered work, by linking or
+combining it with the OpenSSL project's OpenSSL library (or a
+modified version of that library), containing parts covered by the
+terms of the OpenSSL or SSLeay licenses, the Free Software Foundation
+grants you additional permission to convey the resulting work.
+Corresponding Source for a non-source form of such a combination
+shall include the source code for the parts of OpenSSL used as well
+as that of the covered work.
diff --git a/android/.gitignore b/android/.gitignore
new file mode 100644
index 0000000..a0bf76f
--- /dev/null
+++ b/android/.gitignore
@@ -0,0 +1,16 @@
+*.iml
+.gradle
+/local.properties
+/.idea/caches
+/.idea/libraries
+/.idea/modules.xml
+/.idea/workspace.xml
+/.idea/navEditor.xml
+/.idea/assetWizardSettings.xml
+.DS_Store
+/build
+/captures
+.externalNativeBuild
+.cxx
+/app/release
+keystore.jks
diff --git a/android/app/.gitignore b/android/app/.gitignore
new file mode 100644
index 0000000..796b96d
--- /dev/null
+++ b/android/app/.gitignore
@@ -0,0 +1 @@
+/build
diff --git a/android/app/CMakeLists.txt b/android/app/CMakeLists.txt
new file mode 100644
index 0000000..a1ef29e
--- /dev/null
+++ b/android/app/CMakeLists.txt
@@ -0,0 +1,25 @@
+
+cmake_minimum_required(VERSION 3.2)
+
+set(CMAKE_CXX_STANDARD 14)
+
+add_library(chiaki-jni SHARED
+ src/main/cpp/chiaki-jni.c
+ src/main/cpp/log.h
+ src/main/cpp/log.c
+ src/main/cpp/video-decoder.h
+ src/main/cpp/video-decoder.c
+ src/main/cpp/audio-decoder.h
+ src/main/cpp/audio-decoder.c
+ src/main/cpp/audio-output.h
+ src/main/cpp/audio-output.cpp
+ src/main/cpp/circular-fifo.hpp)
+target_link_libraries(chiaki-jni chiaki-lib)
+
+find_library(ANDROID_LIB_LOG log)
+find_library(ANDROID_LIB_MEDIANDK mediandk)
+find_library(ANDROID_LIB_ANDROID android)
+target_link_libraries(chiaki-jni "${ANDROID_LIB_LOG}" "${ANDROID_LIB_MEDIANDK}" "${ANDROID_LIB_ANDROID}")
+
+add_subdirectory(src/main/cpp/oboe)
+target_link_libraries(chiaki-jni oboe)
\ No newline at end of file
diff --git a/android/app/build.gradle b/android/app/build.gradle
new file mode 100644
index 0000000..21a89f3
--- /dev/null
+++ b/android/app/build.gradle
@@ -0,0 +1,116 @@
+apply plugin: 'com.android.application'
+apply plugin: 'kotlin-android'
+apply plugin: 'kotlin-parcelize'
+apply plugin: 'kotlin-kapt'
+
+def rootCMakeLists = "../../CMakeLists.txt"
+def rootCMakeListsContent = file(rootCMakeLists).text
+static def grepVersionComponent(content, varname) {
+ def match = content =~ /set\($varname ([0-9]+)\)/
+ if(!match.find())
+ throw new GradleException("Failed to find $varname in CMakeLists.txt")
+ return match.group(1)
+}
+def chiakiVersionMajor = grepVersionComponent(rootCMakeListsContent, "CHIAKI_VERSION_MAJOR")
+def chiakiVersionMinor = grepVersionComponent(rootCMakeListsContent, "CHIAKI_VERSION_MINOR")
+def chiakiVersionPatch = grepVersionComponent(rootCMakeListsContent, "CHIAKI_VERSION_PATCH")
+def chiakiVersion = "$chiakiVersionMajor.$chiakiVersionMinor.$chiakiVersionPatch"
+println("Determined Chiaki Version: $chiakiVersion")
+
+android {
+ compileSdkVersion 33
+ defaultConfig {
+ applicationId "com.metallic.chiaki"
+ minSdkVersion 21
+ targetSdkVersion 33
+ versionCode 12
+ versionName chiakiVersion
+ externalNativeBuild {
+ cmake {
+ arguments "-DCHIAKI_ENABLE_TESTS=OFF",
+ "-DCHIAKI_ENABLE_CLI=OFF",
+ "-DCHIAKI_ENABLE_GUI=OFF",
+ "-DCHIAKI_ENABLE_SETSU=OFF",
+ "-DCHIAKI_ENABLE_ANDROID=ON",
+ "-DCHIAKI_ENABLE_FFMPEG_DECODER=OFF",
+ "-DCHIAKI_ENABLE_PI_DECODER=OFF",
+ "-DCHIAKI_LIB_ENABLE_OPUS=OFF",
+ "-DCHIAKI_LIB_OPENSSL_EXTERNAL_PROJECT=ON"
+ }
+ }
+ }
+ buildFeatures {
+ viewBinding true
+ }
+ compileOptions {
+ sourceCompatibility JavaVersion.VERSION_1_8
+ targetCompatibility JavaVersion.VERSION_1_8
+ }
+ kotlinOptions {
+ jvmTarget = "1.8"
+ }
+ externalNativeBuild {
+ cmake {
+ version "3.22.1"
+ path rootCMakeLists
+ }
+ }
+ signingConfigs {
+ release
+ }
+ buildTypes {
+ release {
+ minifyEnabled true
+ proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
+ }
+ }
+
+
+ Properties properties = new Properties()
+ def propertiesFile = file("../local.properties")
+ if (propertiesFile.exists()) {
+ properties.load(propertiesFile.newDataInputStream())
+ }
+
+ if(properties.containsKey("chiakiKeystore")) {
+ println("Enabling Local Signing")
+ buildTypes.release.signingConfig = signingConfigs.release
+ buildTypes.debug.signingConfig = signingConfigs.release
+ signingConfigs.release.storeFile = file(properties.get("chiakiKeystore"))
+ signingConfigs.release.storePassword = properties.get("chiakiKeystorePW")
+ signingConfigs.release.keyAlias = properties.get("chiakiKeyAlias")
+ signingConfigs.release.keyPassword = properties.get("chiakiKeyPW")
+ } else {
+ println("Signing not enabled")
+ }
+}
+
+tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
+ kotlinOptions {
+ freeCompilerArgs += "-Xuse-experimental=kotlin.ExperimentalUnsignedTypes"
+ }
+}
+
+dependencies {
+ implementation fileTree(dir: 'libs', include: ['*.jar'])
+ implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
+ implementation 'androidx.appcompat:appcompat:1.6.0'
+ implementation 'androidx.core:core-ktx:1.9.0'
+ implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
+ implementation 'androidx.recyclerview:recyclerview:1.2.1'
+ implementation 'androidx.preference:preference:1.2.0'
+ implementation 'com.google.android.material:material:1.8.0'
+ implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
+ implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1'
+ implementation 'androidx.lifecycle:lifecycle-reactivestreams:2.5.1'
+ implementation "io.reactivex.rxjava2:rxjava:2.2.20"
+ implementation "io.reactivex.rxjava2:rxkotlin:2.4.0"
+ implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
+ def room_version = "2.5.0"
+ implementation "androidx.room:room-runtime:$room_version"
+ kapt "androidx.room:room-compiler:$room_version"
+ implementation "androidx.room:room-ktx:$room_version"
+ implementation "androidx.room:room-rxjava2:$room_version"
+ implementation "com.squareup.moshi:moshi:1.14.0"
+ kapt "com.squareup.moshi:moshi-kotlin-codegen:1.14.0"
+}
diff --git a/android/app/proguard-rules.pro b/android/app/proguard-rules.pro
new file mode 100644
index 0000000..ccbe180
--- /dev/null
+++ b/android/app/proguard-rules.pro
@@ -0,0 +1,91 @@
+# Add project specific ProGuard rules here.
+# You can control the set of applied configuration files using the
+# proguardFiles setting in build.gradle.
+#
+# For more details, see
+# http://developer.android.com/guide/developing/tools/proguard.html
+
+# If your project uses WebView with JS, uncomment the following
+# and specify the fully qualified class name to the JavaScript interface
+# class:
+#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
+# public *;
+#}
+
+# Uncomment this to preserve the line number information for
+# debugging stack traces.
+#-keepattributes SourceFile,LineNumberTable
+
+# If you keep the line number information, uncomment this to
+# hide the original source file name.
+#-renamesourcefileattribute SourceFile
+
+-dontobfuscate
+-keep class com.metallic.chiaki.** { *; }
+
+
+##########################################
+# Moshi
+##########################################
+
+# JSR 305 annotations are for embedding nullability information.
+-dontwarn javax.annotation.**
+
+-keepclasseswithmembers class * {
+ @com.squareup.moshi.* ;
+}
+
+-keep @com.squareup.moshi.JsonQualifier interface *
+
+# Enum field names are used by the integrated EnumJsonAdapter.
+# values() is synthesized by the Kotlin compiler and is used by EnumJsonAdapter indirectly
+# Annotate enums with @JsonClass(generateAdapter = false) to use them with Moshi.
+-keepclassmembers @com.squareup.moshi.JsonClass class * extends java.lang.Enum {
+ ;
+ **[] values();
+}
+
+# The name of @JsonClass types is used to look up the generated adapter.
+-keepnames @com.squareup.moshi.JsonClass class *
+
+# Retain generated target class's synthetic defaults constructor and keep DefaultConstructorMarker's
+# name. We will look this up reflectively to invoke the type's constructor.
+#
+# We can't _just_ keep the defaults constructor because Proguard/R8's spec doesn't allow wildcard
+# matching preceding parameters.
+-keepnames class kotlin.jvm.internal.DefaultConstructorMarker
+-keepclassmembers @com.squareup.moshi.JsonClass @kotlin.Metadata class * {
+ synthetic (...);
+}
+
+# Retain generated JsonAdapters if annotated type is retained.
+-if @com.squareup.moshi.JsonClass class *
+-keep class <1>JsonAdapter {
+ (...);
+ ;
+}
+-if @com.squareup.moshi.JsonClass class **$*
+-keep class <1>_<2>JsonAdapter {
+ (...);
+ ;
+}
+-if @com.squareup.moshi.JsonClass class **$*$*
+-keep class <1>_<2>_<3>JsonAdapter {
+ (...);
+ ;
+}
+-if @com.squareup.moshi.JsonClass class **$*$*$*
+-keep class <1>_<2>_<3>_<4>JsonAdapter {
+ (...);
+ ;
+}
+-if @com.squareup.moshi.JsonClass class **$*$*$*$*
+-keep class <1>_<2>_<3>_<4>_<5>JsonAdapter {
+ (...);
+ ;
+}
+-if @com.squareup.moshi.JsonClass class **$*$*$*$*$*
+-keep class <1>_<2>_<3>_<4>_<5>_<6>JsonAdapter {
+ (...);
+ ;
+}
diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml
new file mode 100644
index 0000000..a0d28cb
--- /dev/null
+++ b/android/app/src/main/AndroidManifest.xml
@@ -0,0 +1,66 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/android/app/src/main/cpp/audio-decoder.c b/android/app/src/main/cpp/audio-decoder.c
new file mode 100644
index 0000000..35f97e9
--- /dev/null
+++ b/android/app/src/main/cpp/audio-decoder.c
@@ -0,0 +1,205 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+#include "audio-decoder.h"
+
+#include
+
+#include
+#include
+
+#include
+
+#define INPUT_BUFFER_TIMEOUT_MS 10
+
+static void *android_chiaki_audio_decoder_output_thread_func(void *user);
+static void android_chiaki_audio_decoder_header(ChiakiAudioHeader *header, void *user);
+static void android_chiaki_audio_decoder_frame(uint8_t *buf, size_t buf_size, void *user);
+
+ChiakiErrorCode android_chiaki_audio_decoder_init(AndroidChiakiAudioDecoder *decoder, ChiakiLog *log)
+{
+ decoder->log = log;
+ memset(&decoder->audio_header, 0, sizeof(decoder->audio_header));
+ decoder->codec = NULL;
+ decoder->timestamp_cur = 0;
+
+ decoder->cb_user = NULL;
+ decoder->settings_cb = NULL;
+ decoder->frame_cb = NULL;
+
+ return chiaki_mutex_init(&decoder->codec_mutex, true);
+}
+
+void android_chiaki_audio_decoder_shutdown_codec(AndroidChiakiAudioDecoder *decoder)
+{
+ chiaki_mutex_lock(&decoder->codec_mutex);
+ ssize_t codec_buf_index = AMediaCodec_dequeueInputBuffer(decoder->codec, -1);
+ if(codec_buf_index >= 0)
+ {
+ CHIAKI_LOGI(decoder->log, "Audio Decoder sending EOS buffer");
+ AMediaCodec_queueInputBuffer(decoder->codec, (size_t)codec_buf_index, 0, 0, decoder->timestamp_cur++, AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM);
+ }
+ else
+ CHIAKI_LOGE(decoder->log, "Failed to get input buffer for shutting down Audio Decoder!");
+ chiaki_mutex_unlock(&decoder->codec_mutex);
+ chiaki_thread_join(&decoder->output_thread, NULL);
+ AMediaCodec_delete(decoder->codec);
+ decoder->codec = NULL;
+}
+
+void android_chiaki_audio_decoder_fini(AndroidChiakiAudioDecoder *decoder)
+{
+ if(decoder->codec)
+ android_chiaki_audio_decoder_shutdown_codec(decoder);
+ chiaki_mutex_fini(&decoder->codec_mutex);
+}
+
+void android_chiaki_audio_decoder_get_sink(AndroidChiakiAudioDecoder *decoder, ChiakiAudioSink *sink)
+{
+ sink->user = decoder;
+ sink->header_cb = android_chiaki_audio_decoder_header;
+ sink->frame_cb = android_chiaki_audio_decoder_frame;
+}
+
+static void *android_chiaki_audio_decoder_output_thread_func(void *user)
+{
+ AndroidChiakiAudioDecoder *decoder = user;
+
+ while(1)
+ {
+ AMediaCodecBufferInfo info;
+ ssize_t codec_buf_index = AMediaCodec_dequeueOutputBuffer(decoder->codec, &info, -1);
+ if(codec_buf_index >= 0)
+ {
+ if(decoder->settings_cb)
+ {
+ size_t codec_buf_size;
+ uint8_t *codec_buf = AMediaCodec_getOutputBuffer(decoder->codec, (size_t)codec_buf_index, &codec_buf_size);
+ size_t samples_count = info.size / sizeof(int16_t);
+ //CHIAKI_LOGD(decoder->log, "Got %llu samples => %f ms of audio", (unsigned long long)samples_count, 1000.0f * (float)(samples_count / 2) / (float)decoder->audio_header.rate);
+ decoder->frame_cb((int16_t *)codec_buf, samples_count, decoder->cb_user);
+ }
+ AMediaCodec_releaseOutputBuffer(decoder->codec, (size_t)codec_buf_index, false);
+ if(info.flags & AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM)
+ {
+ CHIAKI_LOGI(decoder->log, "AMediaCodec for Audio Decoder reported EOS");
+ break;
+ }
+ }
+ }
+
+ CHIAKI_LOGI(decoder->log, "Audio Decoder Output Thread exiting");
+
+ return NULL;
+}
+
+static void android_chiaki_audio_decoder_header(ChiakiAudioHeader *header, void *user)
+{
+ AndroidChiakiAudioDecoder *decoder = user;
+ chiaki_mutex_lock(&decoder->codec_mutex);
+ memcpy(&decoder->audio_header, header, sizeof(decoder->audio_header));
+
+ if(decoder->codec)
+ {
+ CHIAKI_LOGI(decoder->log, "Audio decoder already initialized, shutting down the old one");
+ android_chiaki_audio_decoder_shutdown_codec(decoder);
+ }
+
+ const char *mime = "audio/opus";
+ decoder->codec = AMediaCodec_createDecoderByType(mime);
+ if(!decoder->codec)
+ {
+ CHIAKI_LOGE(decoder->log, "Failed to create AMediaCodec for mime type %s", mime);
+ goto beach;
+ }
+
+ AMediaFormat *format = AMediaFormat_new();
+ AMediaFormat_setString(format, AMEDIAFORMAT_KEY_MIME, mime);
+ AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_CHANNEL_COUNT, header->channels);
+ AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_SAMPLE_RATE, header->rate);
+ // AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_PCM_ENCODING)
+
+ AMediaCodec_configure(decoder->codec, format, NULL, NULL, 0); // TODO: check result
+ AMediaCodec_start(decoder->codec); // TODO: check result
+
+ AMediaFormat_delete(format);
+
+ ChiakiErrorCode err = chiaki_thread_create(&decoder->output_thread, android_chiaki_audio_decoder_output_thread_func, decoder);
+ if(err != CHIAKI_ERR_SUCCESS)
+ {
+ CHIAKI_LOGE(decoder->log, "Failed to create output thread for AMediaCodec");
+ AMediaCodec_delete(decoder->codec);
+ decoder->codec = NULL;
+ }
+
+ uint8_t opus_id_head[0x13];
+ memcpy(opus_id_head, "OpusHead", 8);
+ opus_id_head[0x8] = 1; // version
+ opus_id_head[0x9] = header->channels;
+ uint16_t pre_skip = 3840;
+ opus_id_head[0xa] = (uint8_t)(pre_skip & 0xff);
+ opus_id_head[0xb] = (uint8_t)(pre_skip >> 8);
+ opus_id_head[0xc] = (uint8_t)(header->rate & 0xff);
+ opus_id_head[0xd] = (uint8_t)((header->rate >> 0x8) & 0xff);
+ opus_id_head[0xe] = (uint8_t)((header->rate >> 0x10) & 0xff);
+ opus_id_head[0xf] = (uint8_t)(header->rate >> 0x18);
+ uint16_t output_gain = 0;
+ opus_id_head[0x10] = (uint8_t)(output_gain & 0xff);
+ opus_id_head[0x11] = (uint8_t)(output_gain >> 8);
+ opus_id_head[0x12] = 0; // channel map
+ //AMediaFormat_setBuffer(format, AMEDIAFORMAT_KEY_CSD_0, opus_id_head, sizeof(opus_id_head));
+ android_chiaki_audio_decoder_frame(opus_id_head, sizeof(opus_id_head), decoder);
+
+ uint64_t pre_skip_ns = 0;
+ uint8_t csd1[8] = { (uint8_t)(pre_skip_ns & 0xff), (uint8_t)((pre_skip_ns >> 0x8) & 0xff), (uint8_t)((pre_skip_ns >> 0x10) & 0xff), (uint8_t)((pre_skip_ns >> 0x18) & 0xff),
+ (uint8_t)((pre_skip_ns >> 0x20) & 0xff), (uint8_t)((pre_skip_ns >> 0x28) & 0xff), (uint8_t)((pre_skip_ns >> 0x30) & 0xff), (uint8_t)(pre_skip_ns >> 0x38)};
+ android_chiaki_audio_decoder_frame(csd1, sizeof(csd1), decoder);
+
+ uint64_t pre_roll_ns = 0;
+ uint8_t csd2[8] = { (uint8_t)(pre_roll_ns & 0xff), (uint8_t)((pre_roll_ns >> 0x8) & 0xff), (uint8_t)((pre_roll_ns >> 0x10) & 0xff), (uint8_t)((pre_roll_ns >> 0x18) & 0xff),
+ (uint8_t)((pre_roll_ns >> 0x20) & 0xff), (uint8_t)((pre_roll_ns >> 0x28) & 0xff), (uint8_t)((pre_roll_ns >> 0x30) & 0xff), (uint8_t)(pre_roll_ns >> 0x38)};
+ android_chiaki_audio_decoder_frame(csd2, sizeof(csd2), decoder);
+
+ if(decoder->settings_cb)
+ decoder->settings_cb(header->channels, header->rate, decoder->cb_user);
+
+beach:
+ chiaki_mutex_unlock(&decoder->codec_mutex);
+}
+
+static void android_chiaki_audio_decoder_frame(uint8_t *buf, size_t buf_size, void *user)
+{
+ AndroidChiakiAudioDecoder *decoder = user;
+ chiaki_mutex_lock(&decoder->codec_mutex);
+
+ if(!decoder->codec)
+ {
+ CHIAKI_LOGE(decoder->log, "Received audio data, but decoder is not initialized!");
+ goto beach;
+ }
+
+ while(buf_size > 0)
+ {
+ ssize_t codec_buf_index = AMediaCodec_dequeueInputBuffer(decoder->codec, INPUT_BUFFER_TIMEOUT_MS * 1000);
+ if(codec_buf_index < 0)
+ {
+ CHIAKI_LOGE(decoder->log, "Failed to get input audio buffer");
+ return;
+ }
+
+ size_t codec_buf_size;
+ uint8_t *codec_buf = AMediaCodec_getInputBuffer(decoder->codec, (size_t)codec_buf_index, &codec_buf_size);
+ size_t codec_sample_size = buf_size;
+ if(codec_sample_size > codec_buf_size)
+ {
+ CHIAKI_LOGD(decoder->log, "Sample is bigger than audio buffer, splitting");
+ codec_sample_size = codec_buf_size;
+ }
+ memcpy(codec_buf, buf, codec_sample_size);
+ AMediaCodec_queueInputBuffer(decoder->codec, (size_t)codec_buf_index, 0, codec_sample_size, decoder->timestamp_cur++, 0); // timestamp just raised by 1 for maximum realtime
+ buf += codec_sample_size;
+ buf_size -= codec_sample_size;
+ }
+
+beach:
+ chiaki_mutex_unlock(&decoder->codec_mutex);
+}
\ No newline at end of file
diff --git a/android/app/src/main/cpp/audio-decoder.h b/android/app/src/main/cpp/audio-decoder.h
new file mode 100644
index 0000000..c365cd2
--- /dev/null
+++ b/android/app/src/main/cpp/audio-decoder.h
@@ -0,0 +1,41 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+#ifndef CHIAKI_JNI_AUDIO_DECODER_H
+#define CHIAKI_JNI_AUDIO_DECODER_H
+
+#include
+
+#include
+#include
+#include
+
+typedef void (*AndroidChiakiAudioDecoderSettingsCallback)(uint32_t channels, uint32_t rate, void *user);
+typedef void (*AndroidChiakiAudioDecoderFrameCallback)(int16_t *buf, size_t samples_count, void *user);
+
+typedef struct android_chiaki_audio_decoder_t
+{
+ ChiakiLog *log;
+ ChiakiAudioHeader audio_header;
+
+ ChiakiMutex codec_mutex;
+ struct AMediaCodec *codec;
+ uint64_t timestamp_cur;
+ ChiakiThread output_thread;
+
+ AndroidChiakiAudioDecoderSettingsCallback settings_cb;
+ AndroidChiakiAudioDecoderFrameCallback frame_cb;
+ void *cb_user;
+} AndroidChiakiAudioDecoder;
+
+ChiakiErrorCode android_chiaki_audio_decoder_init(AndroidChiakiAudioDecoder *decoder, ChiakiLog *log);
+void android_chiaki_audio_decoder_fini(AndroidChiakiAudioDecoder *decoder);
+void android_chiaki_audio_decoder_get_sink(AndroidChiakiAudioDecoder *decoder, ChiakiAudioSink *sink);
+
+static inline void android_chiaki_audio_decoder_set_cb(AndroidChiakiAudioDecoder *decoder, AndroidChiakiAudioDecoderSettingsCallback settings_cb, AndroidChiakiAudioDecoderFrameCallback frame_cb, void *user)
+{
+ decoder->settings_cb = settings_cb;
+ decoder->frame_cb = frame_cb;
+ decoder->cb_user = user;
+}
+
+#endif
\ No newline at end of file
diff --git a/android/app/src/main/cpp/audio-output.cpp b/android/app/src/main/cpp/audio-output.cpp
new file mode 100644
index 0000000..53157f1
--- /dev/null
+++ b/android/app/src/main/cpp/audio-output.cpp
@@ -0,0 +1,124 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+#include "audio-output.h"
+
+#include "circular-buf.hpp"
+
+#include
+#include
+
+#include
+
+#define BUFFER_CHUNK_SIZE 1024
+#define BUFFER_CHUNKS_COUNT 32
+
+using AudioBuffer = CircularBuffer;
+
+class AudioOutput;
+
+class AudioOutputCallback: public oboe::AudioStreamCallback
+{
+private:
+ AudioOutput *audio_output;
+
+public:
+ AudioOutputCallback(AudioOutput *audio_output) : audio_output(audio_output) {}
+ oboe::DataCallbackResult onAudioReady(oboe::AudioStream *stream, void *audioData, int32_t numFrames) override;
+ void onErrorBeforeClose(oboe::AudioStream *stream, oboe::Result error) override;
+ void onErrorAfterClose(oboe::AudioStream *stream, oboe::Result error) override;
+};
+
+struct AudioOutput
+{
+ ChiakiLog *log;
+ oboe::ManagedStream stream;
+ AudioOutputCallback stream_callback;
+ AudioBuffer buf;
+
+ AudioOutput() : stream_callback(this) {}
+};
+
+extern "C" void *android_chiaki_audio_output_new(ChiakiLog *log)
+{
+ auto r = new AudioOutput();
+ r->log = log;
+ return r;
+}
+
+extern "C" void android_chiaki_audio_output_free(void *audio_output)
+{
+ if(!audio_output)
+ return;
+ auto ao = reinterpret_cast(audio_output);
+ ao->stream = nullptr;
+ delete ao;
+}
+
+extern "C" void android_chiaki_audio_output_settings(uint32_t channels, uint32_t rate, void *audio_output)
+{
+ auto ao = reinterpret_cast(audio_output);
+
+ oboe::AudioStreamBuilder builder;
+ builder.setPerformanceMode(oboe::PerformanceMode::LowLatency)
+ ->setSharingMode(oboe::SharingMode::Exclusive)
+ ->setFormat(oboe::AudioFormat::I16)
+ ->setChannelCount(channels)
+ ->setSampleRate(rate)
+ ->setCallback(&ao->stream_callback);
+
+ auto result = builder.openManagedStream(ao->stream);
+ if(result == oboe::Result::OK)
+ CHIAKI_LOGI(ao->log, "Audio Output opened Oboe stream");
+ else
+ CHIAKI_LOGE(ao->log, "Audio Output failed to open Oboe stream: %s", oboe::convertToText(result));
+
+ result = ao->stream->start();
+ if(result == oboe::Result::OK)
+ CHIAKI_LOGI(ao->log, "Audio Output started Oboe stream");
+ else
+ CHIAKI_LOGE(ao->log, "Audio Output failed to start Oboe stream: %s", oboe::convertToText(result));
+}
+
+extern "C" void android_chiaki_audio_output_frame(int16_t *buf, size_t samples_count, void *audio_output)
+{
+ auto ao = reinterpret_cast(audio_output);
+
+ size_t buf_size = samples_count * sizeof(int16_t);
+ size_t pushed = ao->buf.Push(reinterpret_cast(buf), buf_size);
+ if(pushed < buf_size)
+ CHIAKI_LOGW(ao->log, "Audio Output Buffer Overflow!");
+}
+
+oboe::DataCallbackResult AudioOutputCallback::onAudioReady(oboe::AudioStream *stream, void *audio_data, int32_t num_frames)
+{
+ if(stream->getFormat() != oboe::AudioFormat::I16)
+ {
+ CHIAKI_LOGE(audio_output->log, "Oboe stream has invalid format in callback");
+ return oboe::DataCallbackResult::Stop;
+ }
+
+ int32_t bytes_per_frame = stream->getBytesPerFrame();
+ size_t buf_size_requested = static_cast(bytes_per_frame * num_frames);
+ auto buf = reinterpret_cast(audio_data);
+
+ size_t buf_size_delivered = audio_output->buf.Pop(buf, buf_size_requested);
+ //CHIAKI_LOGW(audio_output->log, "Delivered %llu", (unsigned long long)buf_size_delivered);
+
+ if(buf_size_delivered < buf_size_requested)
+ {
+ CHIAKI_LOGV(audio_output->log, "Audio Output Buffer Underflow!");
+ memset(buf + buf_size_delivered, 0, buf_size_requested - buf_size_delivered);
+ }
+
+ return oboe::DataCallbackResult::Continue;
+}
+
+void AudioOutputCallback::onErrorBeforeClose(oboe::AudioStream *stream, oboe::Result error)
+{
+ CHIAKI_LOGE(audio_output->log, "Oboe reported error before close: %s", oboe::convertToText(error));
+}
+
+void AudioOutputCallback::onErrorAfterClose(oboe::AudioStream *stream, oboe::Result error)
+{
+ CHIAKI_LOGE(audio_output->log, "Oboe reported error after close: %s", oboe::convertToText(error));
+}
diff --git a/android/app/src/main/cpp/audio-output.h b/android/app/src/main/cpp/audio-output.h
new file mode 100644
index 0000000..b521ebe
--- /dev/null
+++ b/android/app/src/main/cpp/audio-output.h
@@ -0,0 +1,22 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+#ifndef CHIAKI_JNI_AUDIO_OUTPUT_H
+#define CHIAKI_JNI_AUDIO_OUTPUT_H
+
+#include
+#include
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void *android_chiaki_audio_output_new(ChiakiLog *log);
+void android_chiaki_audio_output_free(void *audio_output);
+void android_chiaki_audio_output_settings(uint32_t channels, uint32_t rate, void *audio_output);
+void android_chiaki_audio_output_frame(int16_t *buf, size_t samples_count, void *audio_output);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif //CHIAKI_JNI_AUDIO_OUTPUT_H
diff --git a/android/app/src/main/cpp/chiaki-jni.c b/android/app/src/main/cpp/chiaki-jni.c
new file mode 100644
index 0000000..9b14b60
--- /dev/null
+++ b/android/app/src/main/cpp/chiaki-jni.c
@@ -0,0 +1,834 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+#include
+
+#include
+
+#include
+#include
+#include
+#include
+#include
+
+#include
+#include
+#include
+#include
+
+#include "video-decoder.h"
+#include "audio-decoder.h"
+#include "audio-output.h"
+#include "log.h"
+#include "chiaki-jni.h"
+
+static char *strdup_jni(const char *str)
+{
+ if(!str)
+ return NULL;
+ char *r = strdup(str);
+ if(!r)
+ return NULL;
+ for(char *c=r; *c; c++)
+ {
+ if(*c & (1 << 7))
+ *c = '?';
+ }
+ return r;
+}
+
+jobject jnistr_from_ascii(JNIEnv *env, const char *str)
+{
+ if(!str)
+ return NULL;
+ char *s = strdup_jni(str);
+ if(!s)
+ return NULL;
+ jobject r = E->NewStringUTF(env, s);
+ free(s);
+ return r;
+}
+
+static jbyteArray jnibytearray_create(JNIEnv *env, const uint8_t *buf, size_t buf_size)
+{
+ jbyteArray r = E->NewByteArray(env, buf_size);
+ E->SetByteArrayRegion(env, r, 0, buf_size, (const jbyte *)buf);
+ return r;
+}
+
+static jobject get_kotlin_global_object(JNIEnv *env, const char *id)
+{
+ size_t idlen = strlen(id);
+ char *sig = malloc(idlen + 3);
+ if(!sig)
+ return NULL;
+ sig[0] = 'L';
+ memcpy(sig + 1, id, idlen);
+ sig[1 + idlen] = ';';
+ sig[1 + idlen + 1] = '\0';
+ jclass cls = E->FindClass(env, id);
+ jfieldID field_id = E->GetStaticFieldID(env, cls, "INSTANCE", sig);
+ jobject r = E->GetStaticObjectField(env, cls, field_id);
+ free(sig);
+ return r;
+}
+
+static ChiakiLog global_log;
+JavaVM *global_vm;
+
+JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved)
+{
+ global_vm = vm;
+
+ android_chiaki_file_log_init(&global_log, CHIAKI_LOG_ALL & ~CHIAKI_LOG_VERBOSE, NULL);
+ CHIAKI_LOGI(&global_log, "Loading Chiaki Library");
+ ChiakiErrorCode err = chiaki_lib_init();
+ CHIAKI_LOGI(&global_log, "Chiaki Library Init Result: %s\n", chiaki_error_string(err));
+ return JNI_VERSION;
+}
+
+JNIEnv *attach_thread_jni()
+{
+ JNIEnv *env;
+ int r = (*global_vm)->GetEnv(global_vm, (void **)&env, JNI_VERSION);
+ if(r == JNI_OK)
+ return env;
+
+ if((*global_vm)->AttachCurrentThread(global_vm, &env, NULL) == 0)
+ return env;
+
+ CHIAKI_LOGE(&global_log, "Failed to get JNIEnv from JavaVM or attach");
+ return NULL;
+}
+
+JNIEXPORT jstring JNICALL JNI_FCN(errorCodeToString)(JNIEnv *env, jobject obj, jint value)
+{
+ return E->NewStringUTF(env, chiaki_error_string((ChiakiErrorCode)value));
+}
+
+JNIEXPORT jstring JNICALL JNI_FCN(quitReasonToString)(JNIEnv *env, jobject obj, jint value)
+{
+ return E->NewStringUTF(env, chiaki_quit_reason_string((ChiakiQuitReason)value));
+}
+
+JNIEXPORT jboolean JNICALL JNI_FCN(quitReasonIsError)(JNIEnv *env, jobject obj, jint value)
+{
+ return chiaki_quit_reason_is_error(value);
+}
+
+JNIEXPORT jobject JNICALL JNI_FCN(videoProfilePreset)(JNIEnv *env, jobject obj, jint resolution_preset, jint fps_preset, jobject codec)
+{
+ ChiakiConnectVideoProfile profile = { 0 };
+ chiaki_connect_video_profile_preset(&profile, (ChiakiVideoResolutionPreset)resolution_preset, (ChiakiVideoFPSPreset)fps_preset);
+ jclass profile_class = E->FindClass(env, BASE_PACKAGE"/ConnectVideoProfile");
+ jmethodID profile_ctor = E->GetMethodID(env, profile_class, "", "(IIIIL"BASE_PACKAGE"/Codec;)V");
+ return E->NewObject(env, profile_class, profile_ctor, profile.width, profile.height, profile.max_fps, profile.bitrate, codec);
+}
+
+typedef struct android_chiaki_session_t
+{
+ ChiakiSession session;
+ ChiakiLog *log;
+ jobject java_session;
+ jclass java_session_class;
+ jmethodID java_session_event_connected_meth;
+ jmethodID java_session_event_login_pin_request_meth;
+ jmethodID java_session_event_quit_meth;
+ jmethodID java_session_event_rumble_meth;
+ jfieldID java_controller_state_buttons;
+ jfieldID java_controller_state_l2_state;
+ jfieldID java_controller_state_r2_state;
+ jfieldID java_controller_state_left_x;
+ jfieldID java_controller_state_left_y;
+ jfieldID java_controller_state_right_x;
+ jfieldID java_controller_state_right_y;
+ jfieldID java_controller_state_touches;
+ jfieldID java_controller_state_gyro_x;
+ jfieldID java_controller_state_gyro_y;
+ jfieldID java_controller_state_gyro_z;
+ jfieldID java_controller_state_accel_x;
+ jfieldID java_controller_state_accel_y;
+ jfieldID java_controller_state_accel_z;
+ jfieldID java_controller_state_orient_x;
+ jfieldID java_controller_state_orient_y;
+ jfieldID java_controller_state_orient_z;
+ jfieldID java_controller_state_orient_w;
+ jfieldID java_controller_touch_x;
+ jfieldID java_controller_touch_y;
+ jfieldID java_controller_touch_id;
+
+ AndroidChiakiVideoDecoder video_decoder;
+ AndroidChiakiAudioDecoder audio_decoder;
+ void *audio_output;
+} AndroidChiakiSession;
+
+static void android_chiaki_event_cb(ChiakiEvent *event, void *user)
+{
+ AndroidChiakiSession *session = user;
+
+ JNIEnv *env = attach_thread_jni();
+ if(!env)
+ return;
+
+ switch(event->type)
+ {
+ case CHIAKI_EVENT_CONNECTED:
+ E->CallVoidMethod(env, session->java_session,
+ session->java_session_event_connected_meth);
+ break;
+ case CHIAKI_EVENT_LOGIN_PIN_REQUEST:
+ E->CallVoidMethod(env, session->java_session,
+ session->java_session_event_login_pin_request_meth,
+ (jboolean)event->login_pin_request.pin_incorrect);
+ break;
+ case CHIAKI_EVENT_QUIT:
+ {
+ char *reason_str = strdup_jni(event->quit.reason_str);
+ jstring reason_str_java = reason_str ? E->NewStringUTF(env, reason_str) : NULL;
+ E->CallVoidMethod(env, session->java_session,
+ session->java_session_event_quit_meth,
+ (jint)event->quit.reason,
+ reason_str_java);
+ if(reason_str_java)
+ E->DeleteLocalRef(env, reason_str_java);
+ free(reason_str);
+ break;
+ }
+ case CHIAKI_EVENT_RUMBLE:
+ E->CallVoidMethod(env, session->java_session,
+ session->java_session_event_rumble_meth,
+ (jint)event->rumble.left,
+ (jint)event->rumble.right);
+ break;
+ default:
+ break;
+ }
+
+ (*global_vm)->DetachCurrentThread(global_vm);
+}
+
+JNIEXPORT void JNICALL JNI_FCN(sessionCreate)(JNIEnv *env, jobject obj, jobject result, jobject connect_info_obj, jstring log_file_str, jboolean log_verbose, jobject java_session)
+{
+ AndroidChiakiSession *session = NULL;
+ ChiakiLog *log = malloc(sizeof(ChiakiLog));
+ const char *log_file = log_file_str ? E->GetStringUTFChars(env, log_file_str, NULL) : NULL;
+ android_chiaki_file_log_init(log, log_verbose ? CHIAKI_LOG_ALL : (CHIAKI_LOG_ALL & ~CHIAKI_LOG_VERBOSE), log_file);
+ if(log_file)
+ E->ReleaseStringUTFChars(env, log_file_str, log_file);
+
+ ChiakiErrorCode err = CHIAKI_ERR_SUCCESS;
+ char *host_str = NULL;
+
+ jclass result_class = E->GetObjectClass(env, result);
+
+ jclass connect_info_class = E->GetObjectClass(env, connect_info_obj);
+ jboolean ps5 = E->GetBooleanField(env, connect_info_obj, E->GetFieldID(env, connect_info_class, "ps5", "Z"));
+ jstring host_string = E->GetObjectField(env, connect_info_obj, E->GetFieldID(env, connect_info_class, "host", "Ljava/lang/String;"));
+ jbyteArray regist_key_array = E->GetObjectField(env, connect_info_obj, E->GetFieldID(env, connect_info_class, "registKey", "[B"));
+ jbyteArray morning_array = E->GetObjectField(env, connect_info_obj, E->GetFieldID(env, connect_info_class, "morning", "[B"));
+ jobject connect_video_profile_obj = E->GetObjectField(env, connect_info_obj, E->GetFieldID(env, connect_info_class, "videoProfile", "L"BASE_PACKAGE"/ConnectVideoProfile;"));
+ jclass connect_video_profile_class = E->GetObjectClass(env, connect_video_profile_obj);
+
+ ChiakiConnectInfo connect_info = { 0 };
+ connect_info.ps5 = ps5;
+
+ const char *str_borrow = E->GetStringUTFChars(env, host_string, NULL);
+ connect_info.host = host_str = strdup(str_borrow);
+ E->ReleaseStringUTFChars(env, host_string, str_borrow);
+ if(!connect_info.host)
+ {
+ err = CHIAKI_ERR_MEMORY;
+ goto beach;
+ }
+
+ if(E->GetArrayLength(env, regist_key_array) != sizeof(connect_info.regist_key))
+ {
+ CHIAKI_LOGE(log, "Regist Key passed from Java has invalid length");
+ err = CHIAKI_ERR_INVALID_DATA;
+ goto beach;
+ }
+ jbyte *bytes = E->GetByteArrayElements(env, regist_key_array, NULL);
+ memcpy(connect_info.regist_key, bytes, sizeof(connect_info.regist_key));
+ E->ReleaseByteArrayElements(env, regist_key_array, bytes, JNI_ABORT);
+
+ if(E->GetArrayLength(env, morning_array) != sizeof(connect_info.morning))
+ {
+ CHIAKI_LOGE(log, "Morning passed from Java has invalid length");
+ err = CHIAKI_ERR_INVALID_DATA;
+ goto beach;
+ }
+ bytes = E->GetByteArrayElements(env, morning_array, NULL);
+ memcpy(connect_info.morning, bytes, sizeof(connect_info.morning));
+ E->ReleaseByteArrayElements(env, morning_array, bytes, JNI_ABORT);
+
+ connect_info.video_profile.width = (unsigned int)E->GetIntField(env, connect_video_profile_obj, E->GetFieldID(env, connect_video_profile_class, "width", "I"));
+ connect_info.video_profile.height = (unsigned int)E->GetIntField(env, connect_video_profile_obj, E->GetFieldID(env, connect_video_profile_class, "height", "I"));
+ connect_info.video_profile.max_fps = (unsigned int)E->GetIntField(env, connect_video_profile_obj, E->GetFieldID(env, connect_video_profile_class, "maxFPS", "I"));
+ connect_info.video_profile.bitrate = (unsigned int)E->GetIntField(env, connect_video_profile_obj, E->GetFieldID(env, connect_video_profile_class, "bitrate", "I"));
+
+ jobject codec_obj = E->GetObjectField(env, connect_video_profile_obj, E->GetFieldID(env, connect_video_profile_class, "codec", "L"BASE_PACKAGE"/Codec;"));
+ jclass codec_class = E->GetObjectClass(env, codec_obj);
+ jint target_value = E->GetIntField(env, codec_obj, E->GetFieldID(env, codec_class, "value", "I"));
+ connect_info.video_profile.codec = (ChiakiCodec)target_value;
+
+ connect_info.video_profile_auto_downgrade = true;
+
+ session = CHIAKI_NEW(AndroidChiakiSession);
+ if(!session)
+ {
+ err = CHIAKI_ERR_MEMORY;
+ goto beach;
+ }
+ memset(session, 0, sizeof(AndroidChiakiSession));
+ session->log = log;
+ err = android_chiaki_video_decoder_init(&session->video_decoder, log, connect_info.video_profile.width, connect_info.video_profile.height,
+ connect_info.ps5 ? connect_info.video_profile.codec : CHIAKI_CODEC_H264);
+ if(err != CHIAKI_ERR_SUCCESS)
+ {
+ free(session);
+ session = NULL;
+ goto beach;
+ }
+
+ err = android_chiaki_audio_decoder_init(&session->audio_decoder, log);
+ if(err != CHIAKI_ERR_SUCCESS)
+ {
+ android_chiaki_video_decoder_fini(&session->video_decoder);
+ free(session);
+ session = NULL;
+ goto beach;
+ }
+
+ session->audio_output = android_chiaki_audio_output_new(log);
+
+ android_chiaki_audio_decoder_set_cb(&session->audio_decoder, android_chiaki_audio_output_settings, android_chiaki_audio_output_frame, session->audio_output);
+
+ err = chiaki_session_init(&session->session, &connect_info, log);
+ if(err != CHIAKI_ERR_SUCCESS)
+ {
+ CHIAKI_LOGE(log, "JNI ChiakiSession failed to init");
+ android_chiaki_video_decoder_fini(&session->video_decoder);
+ android_chiaki_audio_decoder_fini(&session->audio_decoder);
+ android_chiaki_audio_output_free(session->audio_output);
+ free(session);
+ session = NULL;
+ goto beach;
+ }
+
+ session->java_session = E->NewGlobalRef(env, java_session);
+ session->java_session_class = E->NewGlobalRef(env, E->GetObjectClass(env, session->java_session));
+ session->java_session_event_connected_meth = E->GetMethodID(env, session->java_session_class, "eventConnected", "()V");
+ session->java_session_event_login_pin_request_meth = E->GetMethodID(env, session->java_session_class, "eventLoginPinRequest", "(Z)V");
+ session->java_session_event_quit_meth = E->GetMethodID(env, session->java_session_class, "eventQuit", "(ILjava/lang/String;)V");
+ session->java_session_event_rumble_meth = E->GetMethodID(env, session->java_session_class, "eventRumble", "(II)V");
+
+ jclass controller_state_class = E->FindClass(env, BASE_PACKAGE"/ControllerState");
+ session->java_controller_state_buttons = E->GetFieldID(env, controller_state_class, "buttons", "I");
+ session->java_controller_state_l2_state = E->GetFieldID(env, controller_state_class, "l2State", "B");
+ session->java_controller_state_r2_state = E->GetFieldID(env, controller_state_class, "r2State", "B");
+ session->java_controller_state_left_x = E->GetFieldID(env, controller_state_class, "leftX", "S");
+ session->java_controller_state_left_y = E->GetFieldID(env, controller_state_class, "leftY", "S");
+ session->java_controller_state_right_x = E->GetFieldID(env, controller_state_class, "rightX", "S");
+ session->java_controller_state_right_y = E->GetFieldID(env, controller_state_class, "rightY", "S");
+ session->java_controller_state_touches = E->GetFieldID(env, controller_state_class, "touches", "[L"BASE_PACKAGE"/ControllerTouch;");
+ session->java_controller_state_gyro_x = E->GetFieldID(env, controller_state_class, "gyroX", "F");
+ session->java_controller_state_gyro_y = E->GetFieldID(env, controller_state_class, "gyroY", "F");
+ session->java_controller_state_gyro_z = E->GetFieldID(env, controller_state_class, "gyroZ", "F");
+ session->java_controller_state_accel_x = E->GetFieldID(env, controller_state_class, "accelX", "F");
+ session->java_controller_state_accel_y = E->GetFieldID(env, controller_state_class, "accelY", "F");
+ session->java_controller_state_accel_z = E->GetFieldID(env, controller_state_class, "accelZ", "F");
+ session->java_controller_state_orient_x = E->GetFieldID(env, controller_state_class, "orientX", "F");
+ session->java_controller_state_orient_y = E->GetFieldID(env, controller_state_class, "orientY", "F");
+ session->java_controller_state_orient_z = E->GetFieldID(env, controller_state_class, "orientZ", "F");
+ session->java_controller_state_orient_w = E->GetFieldID(env, controller_state_class, "orientW", "F");
+
+ jclass controller_touch_class = E->FindClass(env, BASE_PACKAGE"/ControllerTouch");
+ session->java_controller_touch_x = E->GetFieldID(env, controller_touch_class, "x", "S");
+ session->java_controller_touch_y = E->GetFieldID(env, controller_touch_class, "y", "S");
+ session->java_controller_touch_id = E->GetFieldID(env, controller_touch_class, "id", "B");
+
+ chiaki_session_set_event_cb(&session->session, android_chiaki_event_cb, session);
+ chiaki_session_set_video_sample_cb(&session->session, android_chiaki_video_decoder_video_sample, &session->video_decoder);
+
+ ChiakiAudioSink audio_sink;
+ android_chiaki_audio_decoder_get_sink(&session->audio_decoder, &audio_sink);
+ chiaki_session_set_audio_sink(&session->session, &audio_sink);
+
+beach:
+ if(!session && log)
+ {
+ android_chiaki_file_log_fini(log);
+ free(log);
+ }
+
+ free(host_str);
+ E->SetIntField(env, result, E->GetFieldID(env, result_class, "errorCode", "I"), (jint)err);
+ E->SetLongField(env, result, E->GetFieldID(env, result_class, "ptr", "J"), (jlong)session);
+}
+
+JNIEXPORT void JNICALL JNI_FCN(sessionFree)(JNIEnv *env, jobject obj, jlong ptr)
+{
+ AndroidChiakiSession *session = (AndroidChiakiSession *)ptr;
+ if(!session)
+ return;
+ CHIAKI_LOGI(session->log, "Shutting down JNI Session");
+ chiaki_session_fini(&session->session);
+ android_chiaki_video_decoder_fini(&session->video_decoder);
+ android_chiaki_audio_decoder_fini(&session->audio_decoder);
+ android_chiaki_audio_output_free(session->audio_output);
+ E->DeleteGlobalRef(env, session->java_session);
+ E->DeleteGlobalRef(env, session->java_session_class);
+ CHIAKI_LOGI(session->log, "JNI Session has quit");
+ android_chiaki_file_log_fini(session->log);
+ free(session->log);
+ free(session);
+}
+
+JNIEXPORT jint JNICALL JNI_FCN(sessionStart)(JNIEnv *env, jobject obj, jlong ptr)
+{
+ AndroidChiakiSession *session = (AndroidChiakiSession *)ptr;
+ CHIAKI_LOGI(session->log, "Start JNI Session");
+ return chiaki_session_start(&session->session);
+}
+
+JNIEXPORT jint JNICALL JNI_FCN(sessionStop)(JNIEnv *env, jobject obj, jlong ptr)
+{
+ AndroidChiakiSession *session = (AndroidChiakiSession *)ptr;
+ CHIAKI_LOGI(session->log, "Stop JNI Session");
+ return chiaki_session_stop(&session->session);
+}
+
+JNIEXPORT jint JNICALL JNI_FCN(sessionJoin)(JNIEnv *env, jobject obj, jlong ptr)
+{
+ AndroidChiakiSession *session = (AndroidChiakiSession *)ptr;
+ CHIAKI_LOGI(session->log, "Join JNI Session");
+ return chiaki_session_join(&session->session);
+}
+
+JNIEXPORT void JNICALL JNI_FCN(sessionSetSurface)(JNIEnv *env, jobject obj, jlong ptr, jobject surface)
+{
+ AndroidChiakiSession *session = (AndroidChiakiSession *)ptr;
+ android_chiaki_video_decoder_set_surface(&session->video_decoder, env, surface);
+}
+
+JNIEXPORT void JNICALL JNI_FCN(sessionSetControllerState)(JNIEnv *env, jobject obj, jlong ptr, jobject controller_state_java)
+{
+ AndroidChiakiSession *session = (AndroidChiakiSession *)ptr;
+ ChiakiControllerState controller_state;
+ chiaki_controller_state_set_idle(&controller_state);
+ controller_state.buttons = (uint32_t)E->GetIntField(env, controller_state_java, session->java_controller_state_buttons);
+ controller_state.l2_state = (uint8_t)E->GetByteField(env, controller_state_java, session->java_controller_state_l2_state);
+ controller_state.r2_state = (uint8_t)E->GetByteField(env, controller_state_java, session->java_controller_state_r2_state);
+ controller_state.left_x = (int16_t)E->GetShortField(env, controller_state_java, session->java_controller_state_left_x);
+ controller_state.left_y = (int16_t)E->GetShortField(env, controller_state_java, session->java_controller_state_left_y);
+ controller_state.right_x = (int16_t)E->GetShortField(env, controller_state_java, session->java_controller_state_right_x);
+ controller_state.right_y = (int16_t)E->GetShortField(env, controller_state_java, session->java_controller_state_right_y);
+ jobjectArray touch_array = E->GetObjectField(env, controller_state_java, session->java_controller_state_touches);
+ size_t touch_array_len = (size_t)E->GetArrayLength(env, touch_array);
+ for(size_t i = 0; i < CHIAKI_CONTROLLER_TOUCHES_MAX; i++)
+ {
+ if(i < touch_array_len)
+ {
+ jobject touch = E->GetObjectArrayElement(env, touch_array, i);
+ controller_state.touches[i].x = (uint16_t)E->GetShortField(env, touch, session->java_controller_touch_x);
+ controller_state.touches[i].y = (uint16_t)E->GetShortField(env, touch, session->java_controller_touch_y);
+ controller_state.touches[i].id = (int8_t)E->GetByteField(env, touch, session->java_controller_touch_id);
+ }
+ else
+ {
+ controller_state.touches[i].x = 0;
+ controller_state.touches[i].y = 0;
+ controller_state.touches[i].id = -1;
+ }
+ }
+ controller_state.gyro_x = E->GetFloatField(env, controller_state_java, session->java_controller_state_gyro_x);
+ controller_state.gyro_y = E->GetFloatField(env, controller_state_java, session->java_controller_state_gyro_y);
+ controller_state.gyro_z = E->GetFloatField(env, controller_state_java, session->java_controller_state_gyro_z);
+ controller_state.accel_x = E->GetFloatField(env, controller_state_java, session->java_controller_state_accel_x);
+ controller_state.accel_y = E->GetFloatField(env, controller_state_java, session->java_controller_state_accel_y);
+ controller_state.accel_z = E->GetFloatField(env, controller_state_java, session->java_controller_state_accel_z);
+ controller_state.orient_x = E->GetFloatField(env, controller_state_java, session->java_controller_state_orient_x);
+ controller_state.orient_y = E->GetFloatField(env, controller_state_java, session->java_controller_state_orient_y);
+ controller_state.orient_z = E->GetFloatField(env, controller_state_java, session->java_controller_state_orient_z);
+ controller_state.orient_w = E->GetFloatField(env, controller_state_java, session->java_controller_state_orient_w);
+ chiaki_session_set_controller_state(&session->session, &controller_state);
+}
+
+JNIEXPORT void JNICALL JNI_FCN(sessionSetLoginPin)(JNIEnv *env, jobject obj, jlong ptr, jstring pin_java)
+{
+ AndroidChiakiSession *session = (AndroidChiakiSession *)ptr;
+ const char *pin = E->GetStringUTFChars(env, pin_java, NULL);
+ chiaki_session_set_login_pin(&session->session, (const uint8_t *)pin, strlen(pin));
+ E->ReleaseStringUTFChars(env, pin_java, pin);
+}
+
+typedef struct android_discovery_service_t
+{
+ ChiakiDiscoveryService service;
+ jobject java_service;
+ jclass java_service_class;
+ jmethodID java_service_hosts_updated_meth;
+
+ jclass host_class;
+ jmethodID host_ctor;
+ jobject host_state_unknown;
+ jobject host_state_ready;
+ jobject host_state_standby;
+} AndroidDiscoveryService;
+
+static void android_discovery_service_cb(ChiakiDiscoveryHost *hosts, size_t hosts_count, void *user)
+{
+ AndroidDiscoveryService *service = user;
+
+ CHIAKI_LOGI(&global_log, "JNI Discovery Callback got %llu hosts", (unsigned long long)hosts_count);
+
+ JNIEnv *env = attach_thread_jni();
+ if(!env)
+ return;
+
+ jobjectArray r = E->NewObjectArray(env, hosts_count, service->host_class, NULL);
+
+ for(size_t i=0; istate)
+ {
+ case CHIAKI_DISCOVERY_HOST_STATE_STANDBY:
+ state = service->host_state_standby;
+ break;
+ case CHIAKI_DISCOVERY_HOST_STATE_READY:
+ state = service->host_state_ready;
+ break;
+ default:
+ state = service->host_state_unknown;
+ break;
+ }
+
+ jobject o = E->NewObject(env, service->host_class, service->host_ctor,
+ state,
+ host->host_request_port,
+ jnistr_from_ascii(env, host->host_addr),
+ jnistr_from_ascii(env, host->system_version),
+ jnistr_from_ascii(env, host->device_discovery_protocol_version),
+ jnistr_from_ascii(env, host->host_name),
+ jnistr_from_ascii(env, host->host_type),
+ jnistr_from_ascii(env, host->host_id),
+ jnistr_from_ascii(env, host->running_app_titleid),
+ jnistr_from_ascii(env, host->running_app_name));
+
+ E->SetObjectArrayElement(env, r, i, o);
+ }
+
+ E->CallVoidMethod(env, service->java_service, service->java_service_hosts_updated_meth, r);
+
+ (*global_vm)->DetachCurrentThread(global_vm);
+}
+
+static ChiakiErrorCode sockaddr_from_java(JNIEnv *env, jobject /*InetSocketAddress*/ sockaddr_obj, struct sockaddr **addr, size_t *addr_size)
+{
+ jclass sockaddr_class = E->GetObjectClass(env, sockaddr_obj);
+ uint16_t port = (uint16_t)E->CallIntMethod(env, sockaddr_obj, E->GetMethodID(env, sockaddr_class, "getPort", "()I"));
+ jobject addr_obj = E->CallObjectMethod(env, sockaddr_obj, E->GetMethodID(env, sockaddr_class, "getAddress", "()Ljava/net/InetAddress;"));
+ jclass addr_class = E->GetObjectClass(env, addr_obj);
+ jbyteArray addr_byte_array = E->CallObjectMethod(env, addr_obj, E->GetMethodID(env, addr_class, "getAddress", "()[B"));
+ jsize addr_byte_array_len = E->GetArrayLength(env, addr_byte_array);
+
+ if(addr_byte_array_len == 4)
+ {
+ struct sockaddr_in *inaddr = CHIAKI_NEW(struct sockaddr_in);
+ if(!inaddr)
+ return CHIAKI_ERR_MEMORY;
+ memset(inaddr, 0, sizeof(*inaddr));
+ inaddr->sin_family = AF_INET;
+ jbyte *bytes = E->GetByteArrayElements(env, addr_byte_array, NULL);
+ memcpy(&inaddr->sin_addr.s_addr, bytes, sizeof(inaddr->sin_addr.s_addr));
+ E->ReleaseByteArrayElements(env, addr_byte_array, bytes, JNI_ABORT);
+ inaddr->sin_port = htons(port);
+
+ *addr = (struct sockaddr *)inaddr;
+ *addr_size = sizeof(*inaddr);
+ }
+ else if(addr_byte_array_len == 0x10)
+ {
+ struct sockaddr_in6 *inaddr6 = CHIAKI_NEW(struct sockaddr_in6);
+ if(!inaddr6)
+ return CHIAKI_ERR_MEMORY;
+ memset(inaddr6, 0, sizeof(*inaddr6));
+ inaddr6->sin6_family = AF_INET6;
+ jbyte *bytes = E->GetByteArrayElements(env, addr_byte_array, NULL);
+ memcpy(&inaddr6->sin6_addr.in6_u, bytes, sizeof(inaddr6->sin6_addr.in6_u));
+ E->ReleaseByteArrayElements(env, addr_byte_array, bytes, JNI_ABORT);
+ inaddr6->sin6_port = htons(port);
+
+ *addr = (struct sockaddr *)inaddr6;
+ *addr_size = sizeof(*inaddr6);
+ }
+ else
+ return CHIAKI_ERR_INVALID_DATA;
+
+ return CHIAKI_ERR_SUCCESS;
+}
+
+JNIEXPORT void JNICALL JNI_FCN(discoveryServiceCreate)(JNIEnv *env, jobject obj, jobject result, jobject options_obj, jobject java_service)
+{
+ jclass result_class = E->GetObjectClass(env, result);
+ ChiakiErrorCode err = CHIAKI_ERR_SUCCESS;
+ ChiakiDiscoveryServiceOptions options = { 0 };
+
+ AndroidDiscoveryService *service = CHIAKI_NEW(AndroidDiscoveryService);
+ if(!service)
+ {
+ err = CHIAKI_ERR_MEMORY;
+ goto beach;
+ }
+
+ jclass options_class = E->GetObjectClass(env, options_obj);
+
+ options.hosts_max = (size_t)E->GetLongField(env, options_obj, E->GetFieldID(env, options_class, "hostsMax", "J"));
+ options.host_drop_pings = (uint64_t)E->GetLongField(env, options_obj, E->GetFieldID(env, options_class, "hostDropPings", "J"));
+ options.ping_ms = (uint64_t)E->GetLongField(env, options_obj, E->GetFieldID(env, options_class, "pingMs", "J"));
+ options.cb = android_discovery_service_cb;
+ options.cb_user = service;
+
+ err = sockaddr_from_java(env, E->GetObjectField(env, options_obj, E->GetFieldID(env, options_class, "sendAddr", "Ljava/net/InetSocketAddress;")), &options.send_addr, &options.send_addr_size);
+ if(err != CHIAKI_ERR_SUCCESS)
+ {
+ CHIAKI_LOGE(&global_log, "Failed to get sockaddr from InetSocketAddress");
+ goto beach;
+ }
+
+ service->java_service = E->NewGlobalRef(env, java_service);
+ service->java_service_class = E->GetObjectClass(env, service->java_service);
+ service->java_service_hosts_updated_meth = E->GetMethodID(env, service->java_service_class, "hostsUpdated", "([L"BASE_PACKAGE"/DiscoveryHost;)V");
+
+ service->host_class = E->NewGlobalRef(env, E->FindClass(env, BASE_PACKAGE"/DiscoveryHost"));
+ service->host_ctor = E->GetMethodID(env, service->host_class, "", "("
+ "L"BASE_PACKAGE"/DiscoveryHost$State;"
+ "S" // hostRequestPort: UShort
+ "Ljava/lang/String;" // hostAddr: String?,
+ "Ljava/lang/String;" // systemVersion: String?,
+ "Ljava/lang/String;" // deviceDiscoveryProtocolVersion: String?,
+ "Ljava/lang/String;" // hostName: String?,
+ "Ljava/lang/String;" // hostType: String?,
+ "Ljava/lang/String;" // hostId: String?,
+ "Ljava/lang/String;" // runningAppTitleid: String?,
+ "Ljava/lang/String;" // runningAppName: String?
+ ")V");
+
+ jclass host_state_class = E->FindClass(env, BASE_PACKAGE"/DiscoveryHost$State");
+ service->host_state_unknown = E->NewGlobalRef(env, E->GetStaticObjectField(env, host_state_class, E->GetStaticFieldID(env, host_state_class, "UNKNOWN", "L"BASE_PACKAGE"/DiscoveryHost$State;")));
+ service->host_state_standby = E->NewGlobalRef(env, E->GetStaticObjectField(env, host_state_class, E->GetStaticFieldID(env, host_state_class, "STANDBY", "L"BASE_PACKAGE"/DiscoveryHost$State;")));
+ service->host_state_ready = E->NewGlobalRef(env, E->GetStaticObjectField(env, host_state_class, E->GetStaticFieldID(env, host_state_class, "READY", "L"BASE_PACKAGE"/DiscoveryHost$State;")));
+
+
+ err = chiaki_discovery_service_init(&service->service, &options, &global_log);
+ if(err != CHIAKI_ERR_SUCCESS)
+ {
+ CHIAKI_LOGE(&global_log, "Failed to create discovery service (JNI)");
+ E->DeleteGlobalRef(env, service->java_service);
+ E->DeleteGlobalRef(env, service->host_state_unknown);
+ E->DeleteGlobalRef(env, service->host_state_standby);
+ E->DeleteGlobalRef(env, service->host_state_ready);
+ E->DeleteGlobalRef(env, service->host_class);
+ free(service);
+ goto beach;
+ }
+
+beach:
+ free(options.send_addr);
+ E->SetIntField(env, result, E->GetFieldID(env, result_class, "errorCode", "I"), (jint)err);
+ E->SetLongField(env, result, E->GetFieldID(env, result_class, "ptr", "J"), (jlong)service);
+}
+
+JNIEXPORT void JNICALL JNI_FCN(discoveryServiceFree)(JNIEnv *env, jobject obj, jlong ptr)
+{
+ AndroidDiscoveryService *service = (AndroidDiscoveryService *)ptr;
+ if(!service)
+ return;
+ chiaki_discovery_service_fini(&service->service);
+ E->DeleteGlobalRef(env, service->java_service);
+ E->DeleteGlobalRef(env, service->host_state_unknown);
+ E->DeleteGlobalRef(env, service->host_state_standby);
+ E->DeleteGlobalRef(env, service->host_state_ready);
+ E->DeleteGlobalRef(env, service->host_class);
+ free(service);
+}
+
+JNIEXPORT jint JNICALL JNI_FCN(discoveryServiceWakeup)(JNIEnv *env, jobject obj, jlong ptr, jstring host_string, jlong user_credential, jboolean ps5)
+{
+ AndroidDiscoveryService *service = (AndroidDiscoveryService *)ptr;
+ const char *host = E->GetStringUTFChars(env, host_string, NULL);
+ ChiakiErrorCode r = chiaki_discovery_wakeup(&global_log, service ? &service->service.discovery : NULL, host, (uint64_t)user_credential, ps5);
+ E->ReleaseStringUTFChars(env, host_string, host);
+ return r;
+}
+
+
+typedef struct android_chiaki_regist_t
+{
+ AndroidChiakiJNILog log;
+ ChiakiRegist regist;
+
+ jobject java_regist;
+ jmethodID java_regist_event_meth;
+
+ jclass java_target_class;
+
+ jobject java_regist_event_canceled;
+ jobject java_regist_event_failed;
+ jclass java_regist_event_success_class;
+ jmethodID java_regist_event_success_ctor;
+
+ jclass java_regist_host_class;
+ jmethodID java_regist_host_ctor;
+} AndroidChiakiRegist;
+
+static jobject create_jni_target(JNIEnv *env, jclass target_class, ChiakiTarget target)
+{
+ jmethodID meth = E->GetStaticMethodID(env, target_class, "fromValue", "(I)L"BASE_PACKAGE"/Target;");
+ return E->CallStaticObjectMethod(env, target_class, meth, (jint)target);
+}
+
+static void android_chiaki_regist_cb(ChiakiRegistEvent *event, void *user)
+{
+ AndroidChiakiRegist *regist = user;
+
+ JNIEnv *env = attach_thread_jni();
+ if(!env)
+ return;
+
+ jobject java_event = NULL;
+ switch(event->type)
+ {
+ case CHIAKI_REGIST_EVENT_TYPE_FINISHED_CANCELED:
+ java_event = regist->java_regist_event_canceled;
+ break;
+ case CHIAKI_REGIST_EVENT_TYPE_FINISHED_FAILED:
+ java_event = regist->java_regist_event_failed;
+ break;
+ case CHIAKI_REGIST_EVENT_TYPE_FINISHED_SUCCESS:
+ {
+ ChiakiRegisteredHost *host = event->registered_host;
+ jobject java_host = E->NewObject(env, regist->java_regist_host_class, regist->java_regist_host_ctor,
+ create_jni_target(env, regist->java_target_class, host->target),
+ jnistr_from_ascii(env, host->ap_ssid),
+ jnistr_from_ascii(env, host->ap_bssid),
+ jnistr_from_ascii(env, host->ap_key),
+ jnistr_from_ascii(env, host->ap_name),
+ jnibytearray_create(env, host->server_mac, sizeof(host->server_mac)),
+ jnistr_from_ascii(env, host->server_nickname),
+ jnibytearray_create(env, (const uint8_t *)host->rp_regist_key, sizeof(host->rp_regist_key)),
+ (jint)host->rp_key_type,
+ jnibytearray_create(env, host->rp_key, sizeof(host->rp_key)));
+ java_event = E->NewObject(env, regist->java_regist_event_success_class, regist->java_regist_event_success_ctor, java_host);
+ break;
+ }
+ }
+
+ if(java_event)
+ E->CallVoidMethod(env, regist->java_regist, regist->java_regist_event_meth, java_event);
+
+ (*global_vm)->DetachCurrentThread(global_vm);
+}
+
+static void android_chiaki_regist_fini_partial(JNIEnv *env, AndroidChiakiRegist *regist)
+{
+ android_chiaki_jni_log_fini(®ist->log, env);
+ E->DeleteGlobalRef(env, regist->java_regist);
+ E->DeleteGlobalRef(env, regist->java_target_class);
+ E->DeleteGlobalRef(env, regist->java_regist_event_canceled);
+ E->DeleteGlobalRef(env, regist->java_regist_event_failed);
+ E->DeleteGlobalRef(env, regist->java_regist_event_success_class);
+ E->DeleteGlobalRef(env, regist->java_regist_host_class);
+}
+
+JNIEXPORT void JNICALL JNI_FCN(registStart)(JNIEnv *env, jobject obj, jobject result, jobject regist_info_obj, jobject log_obj, jobject java_regist)
+{
+ jclass result_class = E->GetObjectClass(env, result);
+ ChiakiErrorCode err = CHIAKI_ERR_SUCCESS;
+ AndroidChiakiRegist *regist = CHIAKI_NEW(AndroidChiakiRegist);
+ if(!regist)
+ {
+ err = CHIAKI_ERR_MEMORY;
+ goto beach;
+ }
+
+ android_chiaki_jni_log_init(®ist->log, env, log_obj);
+
+ regist->java_regist = E->NewGlobalRef(env, java_regist);
+ regist->java_regist_event_meth = E->GetMethodID(env, E->GetObjectClass(env, regist->java_regist), "event", "(L"BASE_PACKAGE"/RegistEvent;)V");
+
+ regist->java_target_class = E->NewGlobalRef(env, E->FindClass(env, BASE_PACKAGE"/Target"));
+
+ regist->java_regist_event_canceled = E->NewGlobalRef(env, get_kotlin_global_object(env, BASE_PACKAGE"/RegistEventCanceled"));
+ regist->java_regist_event_failed = E->NewGlobalRef(env, get_kotlin_global_object(env, BASE_PACKAGE"/RegistEventFailed"));
+ regist->java_regist_event_success_class = E->NewGlobalRef(env, E->FindClass(env, BASE_PACKAGE"/RegistEventSuccess"));
+ regist->java_regist_event_success_ctor = E->GetMethodID(env, regist->java_regist_event_success_class, "", "(L"BASE_PACKAGE"/RegistHost;)V");
+
+ regist->java_regist_host_class = E->NewGlobalRef(env, E->FindClass(env, BASE_PACKAGE"/RegistHost"));
+ regist->java_regist_host_ctor = E->GetMethodID(env, regist->java_regist_host_class, "", "("
+ "L"BASE_PACKAGE"/Target;" // target: Target
+ "Ljava/lang/String;" // apSsid: String
+ "Ljava/lang/String;" // apBssid: String
+ "Ljava/lang/String;" // apKey: String
+ "Ljava/lang/String;" // apName: String
+ "[B" // serverMac: ByteArray
+ "Ljava/lang/String;" // serverNickname: String
+ "[B" // rpRegistKey: ByteArray
+ "I" // rpKeyType: UInt
+ "[B" // rpKey: ByteArray
+ ")V");
+
+ jclass regist_info_class = E->GetObjectClass(env, regist_info_obj);
+
+ jobject target_obj = E->GetObjectField(env, regist_info_obj, E->GetFieldID(env, regist_info_class, "target", "L"BASE_PACKAGE"/Target;"));
+ jclass target_class = E->GetObjectClass(env, target_obj);
+ jint target_value = E->GetIntField(env, target_obj, E->GetFieldID(env, target_class, "value", "I"));
+
+ jstring host_string = E->GetObjectField(env, regist_info_obj, E->GetFieldID(env, regist_info_class, "host", "Ljava/lang/String;"));
+ jboolean broadcast = E->GetBooleanField(env, regist_info_obj, E->GetFieldID(env, regist_info_class, "broadcast", "Z"));
+ jstring psn_online_id_string = E->GetObjectField(env, regist_info_obj, E->GetFieldID(env, regist_info_class, "psnOnlineId", "Ljava/lang/String;"));
+ jbyteArray psn_account_id_array = E->GetObjectField(env, regist_info_obj, E->GetFieldID(env, regist_info_class, "psnAccountId", "[B"));
+ jint pin = E->GetIntField(env, regist_info_obj, E->GetFieldID(env, regist_info_class, "pin", "I"));
+
+ ChiakiRegistInfo regist_info = { 0 };
+ regist_info.target = (ChiakiTarget)target_value;
+ regist_info.host = E->GetStringUTFChars(env, host_string, NULL);
+ regist_info.broadcast = broadcast;
+ if(psn_online_id_string)
+ regist_info.psn_online_id = E->GetStringUTFChars(env, psn_online_id_string, NULL);
+ if(psn_account_id_array && E->GetArrayLength(env, psn_account_id_array) == sizeof(regist_info.psn_account_id))
+ E->GetByteArrayRegion(env, psn_account_id_array, 0, sizeof(regist_info.psn_account_id), (jbyte *)regist_info.psn_account_id);
+ regist_info.pin = (uint32_t)pin;
+
+ err = chiaki_regist_start(®ist->regist, ®ist->log.log, ®ist_info, android_chiaki_regist_cb, regist);
+
+ E->ReleaseStringUTFChars(env, host_string, regist_info.host);
+ if(regist_info.psn_online_id)
+ E->ReleaseStringUTFChars(env, psn_online_id_string, regist_info.psn_online_id);
+
+ if(err != CHIAKI_ERR_SUCCESS)
+ {
+ android_chiaki_regist_fini_partial(env, regist);
+ free(regist);
+ regist = NULL;
+ }
+
+beach:
+ E->SetIntField(env, result, E->GetFieldID(env, result_class, "errorCode", "I"), (jint)err);
+ E->SetLongField(env, result, E->GetFieldID(env, result_class, "ptr", "J"), (jlong)regist);
+}
+
+JNIEXPORT void JNICALL JNI_FCN(registStop)(JNIEnv *env, jobject obj, jlong ptr)
+{
+ AndroidChiakiRegist *regist = (AndroidChiakiRegist *)ptr;
+ chiaki_regist_stop(®ist->regist);
+}
+
+JNIEXPORT void JNICALL JNI_FCN(registFree)(JNIEnv *env, jobject obj, jlong ptr)
+{
+ AndroidChiakiRegist *regist = (AndroidChiakiRegist *)ptr;
+ chiaki_regist_fini(®ist->regist);
+ android_chiaki_regist_fini_partial(env, regist);
+ free(regist);
+}
diff --git a/android/app/src/main/cpp/chiaki-jni.h b/android/app/src/main/cpp/chiaki-jni.h
new file mode 100644
index 0000000..99383f4
--- /dev/null
+++ b/android/app/src/main/cpp/chiaki-jni.h
@@ -0,0 +1,18 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+#ifndef CHIAKI_JNI_H
+#define CHIAKI_JNI_H
+
+#define JNI_VERSION JNI_VERSION_1_6
+
+#define BASE_PACKAGE "com/metallic/chiaki/lib"
+#define JNI_FCN(name) Java_com_metallic_chiaki_lib_ChiakiNative_##name
+
+#define E (*env)
+
+JNIEnv *attach_thread_jni();
+jobject jnistr_from_ascii(JNIEnv *env, const char *str);
+
+extern JavaVM *global_vm;
+
+#endif
\ No newline at end of file
diff --git a/android/app/src/main/cpp/circular-buf.hpp b/android/app/src/main/cpp/circular-buf.hpp
new file mode 100644
index 0000000..21baf9c
--- /dev/null
+++ b/android/app/src/main/cpp/circular-buf.hpp
@@ -0,0 +1,138 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+#ifndef CHIAKI_JNI_CIRCULARBUF_HPP
+#define CHIAKI_JNI_CIRCULARBUF_HPP
+
+#include "circular-fifo.hpp"
+
+#include
+#include
+
+#include
+
+template
+class CircularBuffer
+{
+ static_assert(ChunksCount > 0, "ChunksCount > 0");
+ static_assert(ChunkSize > 0, "ChunkSize > 0");
+
+ private:
+ using Queue = memory_relaxed_aquire_release::CircularFifo;
+ Queue full_queue;
+ Queue free_queue;
+ uint8_t *buffer;
+
+ uint8_t *push_chunk;
+ size_t push_chunk_size; // written bytes from the start of the chunk
+
+ uint8_t *pop_chunk;
+ size_t pop_chunk_size; // remaining bytes until the end of the chunk
+
+ void FlushChunks()
+ {
+ for(size_t i=1; i remaining_space)
+ to_push = remaining_space;
+ memcpy(push_chunk + push_chunk_size, buf + pushed, to_push);
+ pushed += to_push;
+ push_chunk_size += to_push;
+
+ if(push_chunk_size == ChunkSize)
+ {
+ bool success = full_queue.push(push_chunk);
+ assert(success); // We should have made the queues big enough
+ push_chunk = nullptr;
+ }
+ }
+
+ return pushed;
+ }
+
+ /**
+ * @return bytes that were popped
+ */
+ size_t Pop(uint8_t *buf, size_t buf_size)
+ {
+ size_t popped = 0;
+ while(popped < buf_size)
+ {
+ if(!pop_chunk)
+ {
+ if(!full_queue.pop(pop_chunk))
+ pop_chunk = nullptr;
+ if(!pop_chunk)
+ return popped;
+ pop_chunk_size = ChunkSize;
+ }
+
+ size_t to_pop = buf_size - popped;
+ if(to_pop > pop_chunk_size)
+ to_pop = pop_chunk_size;
+ memcpy(buf + popped, pop_chunk + (ChunkSize - pop_chunk_size), to_pop);
+ popped += to_pop;
+ pop_chunk_size -= to_pop;
+
+ if(pop_chunk_size == 0)
+ {
+ bool success = free_queue.push(pop_chunk);
+ assert(success);// We should have made the queues big enough
+ pop_chunk = nullptr;
+ }
+ }
+
+ return popped;
+ }
+};
+
+
+#endif //CHIAKI_JNI_CIRCULARBUF_HPP
diff --git a/android/app/src/main/cpp/circular-fifo.hpp b/android/app/src/main/cpp/circular-fifo.hpp
new file mode 100644
index 0000000..f4e0e61
--- /dev/null
+++ b/android/app/src/main/cpp/circular-fifo.hpp
@@ -0,0 +1,108 @@
+/*
+* Not any company's property but Public-Domain
+* Do with source-code as you will. No requirement to keep this
+* header if need to use it/change it/ or do whatever with it
+*
+* Note that there is No guarantee that this code will work
+* and I take no responsibility for this code and any problems you
+* might get if using it.
+*
+* Code & platform dependent issues with it was originally
+* published at http://www.kjellkod.cc/threadsafecircularqueue
+* 2012-16-19 @author Kjell Hedström, hedstrom@kjellkod.cc */
+
+// should be mentioned the thinking of what goes where
+// it is a "controversy" whether what is tail and what is head
+// http://en.wikipedia.org/wiki/FIFO#Head_or_tail_first
+
+#ifndef CIRCULARFIFO_AQUIRE_RELEASE_H_
+#define CIRCULARFIFO_AQUIRE_RELEASE_H_
+
+#include
+#include
+namespace memory_relaxed_aquire_release {
+template
+class CircularFifo{
+public:
+ enum { Capacity = Size+1 };
+
+ CircularFifo() : _tail(0), _head(0){}
+ virtual ~CircularFifo() {}
+
+ bool push(const Element& item); // pushByMOve?
+ bool pop(Element& item);
+
+ bool wasEmpty() const;
+ bool wasFull() const;
+ bool isLockFree() const;
+
+private:
+ size_t increment(size_t idx) const;
+
+ std::atomic _tail; // tail(input) index
+ Element _array[Capacity];
+ std::atomic _head; // head(output) index
+};
+
+template
+bool CircularFifo::push(const Element& item)
+{
+ const auto current_tail = _tail.load(std::memory_order_relaxed);
+ const auto next_tail = increment(current_tail);
+ if(next_tail != _head.load(std::memory_order_acquire))
+ {
+ _array[current_tail] = item;
+ _tail.store(next_tail, std::memory_order_release);
+ return true;
+ }
+
+ return false; // full queue
+
+}
+
+
+// Pop by Consumer can only update the head (load with relaxed, store with release)
+// the tail must be accessed with at least aquire
+template
+bool CircularFifo::pop(Element& item)
+{
+ const auto current_head = _head.load(std::memory_order_relaxed);
+ if(current_head == _tail.load(std::memory_order_acquire))
+ return false; // empty queue
+
+ item = _array[current_head];
+ _head.store(increment(current_head), std::memory_order_release);
+ return true;
+}
+
+template
+bool CircularFifo::wasEmpty() const
+{
+ // snapshot with acceptance of that this comparison operation is not atomic
+ return (_head.load() == _tail.load());
+}
+
+
+// snapshot with acceptance that this comparison is not atomic
+template
+bool CircularFifo::wasFull() const
+{
+ const auto next_tail = increment(_tail.load()); // aquire, we dont know who call
+ return (next_tail == _head.load());
+}
+
+
+template
+bool CircularFifo::isLockFree() const
+{
+ return (_tail.is_lock_free() && _head.is_lock_free());
+}
+
+template
+size_t CircularFifo::increment(size_t idx) const
+{
+ return (idx + 1) % Capacity;
+}
+
+} // memory_relaxed_aquire_release
+#endif /* CIRCULARFIFO_AQUIRE_RELEASE_H_ */
diff --git a/android/app/src/main/cpp/log.c b/android/app/src/main/cpp/log.c
new file mode 100644
index 0000000..cbb3c7e
--- /dev/null
+++ b/android/app/src/main/cpp/log.c
@@ -0,0 +1,131 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+#include "log.h"
+
+#include
+
+#include
+#include
+#include
+
+#include "chiaki-jni.h"
+
+#define LOG_TAG "Chiaki"
+
+void log_cb_android(ChiakiLogLevel level, const char *msg, void *user)
+{
+ int prio;
+ switch(level)
+ {
+ case CHIAKI_LOG_DEBUG:
+ prio = ANDROID_LOG_DEBUG;
+ break;
+ case CHIAKI_LOG_VERBOSE:
+ prio = ANDROID_LOG_VERBOSE;
+ break;
+ case CHIAKI_LOG_INFO:
+ prio = ANDROID_LOG_INFO;
+ break;
+ case CHIAKI_LOG_WARNING:
+ prio = ANDROID_LOG_ERROR;
+ break;
+ case CHIAKI_LOG_ERROR:
+ prio = ANDROID_LOG_ERROR;
+ break;
+ default:
+ prio = ANDROID_LOG_INFO;
+ break;
+ }
+ __android_log_write(prio, LOG_TAG, msg);
+}
+
+static void log_cb_android_file(ChiakiLogLevel level, const char *msg, void *user)
+{
+ log_cb_android(level, msg, user);
+ FILE *f = user;
+ if(!f)
+ return;
+ switch(level)
+ {
+ case CHIAKI_LOG_DEBUG:
+ fwrite("[D] ", 4, 1, f);
+ break;
+ case CHIAKI_LOG_VERBOSE:
+ fwrite("[V] ", 4, 1, f);
+ break;
+ case CHIAKI_LOG_INFO:
+ fwrite("[I] ", 4, 1, f);
+ break;
+ case CHIAKI_LOG_WARNING:
+ fwrite("[W] ", 4, 1, f);
+ break;
+ case CHIAKI_LOG_ERROR:
+ fwrite("[E] ", 4, 1, f);
+ break;
+ default:
+ fwrite("[?] ", 4, 1, f);
+ break;
+ }
+ fwrite(msg, strlen(msg), 1, f);
+ fwrite("\n", 1, 1, f);
+}
+
+ChiakiErrorCode android_chiaki_file_log_init(ChiakiLog *log, uint32_t level, const char *file)
+{
+ chiaki_log_init(log, level, log_cb_android, NULL);
+ if(file)
+ {
+ FILE *f = fopen(file, "w+");
+ if(!f)
+ {
+ CHIAKI_LOGE(log, "Failed to open log file %s for writing: %s", file, strerror(errno));
+ return CHIAKI_ERR_UNKNOWN;
+ }
+ else
+ {
+ log->user = f;
+ log->cb = log_cb_android_file;
+ CHIAKI_LOGI(log, "Logging to file %s", file);
+ }
+ }
+ return CHIAKI_ERR_SUCCESS;
+}
+
+void android_chiaki_file_log_fini(ChiakiLog *log)
+{
+ if(log->user)
+ {
+ FILE *f = log->user;
+ fclose(f);
+ log->user = NULL;
+ }
+}
+
+
+
+static void android_chiaki_log_cb(ChiakiLogLevel level, const char *msg, void *user)
+{
+ log_cb_android(level, msg, NULL);
+
+ AndroidChiakiJNILog *log = user;
+ JNIEnv *env = attach_thread_jni();
+ if(!env)
+ return;
+ E->CallVoidMethod(env, log->java_log, log->java_log_meth, (jint)level, jnistr_from_ascii(env, msg));
+ (*global_vm)->DetachCurrentThread(global_vm);
+}
+
+void android_chiaki_jni_log_init(AndroidChiakiJNILog *log, JNIEnv *env, jobject java_log)
+{
+ log->java_log = E->NewGlobalRef(env, java_log);
+ jclass log_class = E->GetObjectClass(env, log->java_log);
+ log->java_log_meth = E->GetMethodID(env, log_class, "log", "(ILjava/lang/String;)V");
+ log->log.level_mask = (uint32_t)E->GetIntField(env, log->java_log, E->GetFieldID(env, log_class, "levelMask", "I"));
+ log->log.cb = android_chiaki_log_cb;
+ log->log.user = log;
+}
+
+void android_chiaki_jni_log_fini(AndroidChiakiJNILog *log, JNIEnv *env)
+{
+ E->DeleteGlobalRef(env, log->java_log);
+}
\ No newline at end of file
diff --git a/android/app/src/main/cpp/log.h b/android/app/src/main/cpp/log.h
new file mode 100644
index 0000000..19caf35
--- /dev/null
+++ b/android/app/src/main/cpp/log.h
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+#ifndef CHIAKI_JNI_LOG_H
+#define CHIAKI_JNI_LOG_H
+
+#include
+
+#include
+
+typedef struct android_jni_chiaki_log_t
+{
+ jobject java_log;
+ jmethodID java_log_meth;
+ ChiakiLog log;
+} AndroidChiakiJNILog;
+
+ChiakiErrorCode android_chiaki_file_log_init(ChiakiLog *log, uint32_t level, const char *file);
+void android_chiaki_file_log_fini(ChiakiLog *log);
+
+void android_chiaki_jni_log_init(AndroidChiakiJNILog *log, JNIEnv *env, jobject java_log);
+void android_chiaki_jni_log_fini(AndroidChiakiJNILog *log, JNIEnv *env);
+
+#endif
diff --git a/android/app/src/main/cpp/oboe b/android/app/src/main/cpp/oboe
new file mode 160000
index 0000000..8740d0f
--- /dev/null
+++ b/android/app/src/main/cpp/oboe
@@ -0,0 +1 @@
+Subproject commit 8740d0fc321a55489dbbf6067298201b7d2e106d
diff --git a/android/app/src/main/cpp/video-decoder.c b/android/app/src/main/cpp/video-decoder.c
new file mode 100644
index 0000000..1146ad8
--- /dev/null
+++ b/android/app/src/main/cpp/video-decoder.c
@@ -0,0 +1,221 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+#include "video-decoder.h"
+
+#include
+
+#include
+#include
+#include
+
+#include
+
+#define INPUT_BUFFER_TIMEOUT_MS 10
+
+static void *android_chiaki_video_decoder_output_thread_func(void *user);
+
+ChiakiErrorCode android_chiaki_video_decoder_init(AndroidChiakiVideoDecoder *decoder, ChiakiLog *log, int32_t target_width, int32_t target_height, ChiakiCodec codec)
+{
+ decoder->log = log;
+ decoder->codec = NULL;
+ decoder->timestamp_cur = 0;
+ decoder->target_width = target_width;
+ decoder->target_height = target_height;
+ decoder->target_codec = codec;
+ decoder->shutdown_output = false;
+ return chiaki_mutex_init(&decoder->codec_mutex, false);
+}
+
+static void kill_decoder(AndroidChiakiVideoDecoder *decoder)
+{
+ chiaki_mutex_lock(&decoder->codec_mutex);
+ decoder->shutdown_output = true;
+ ssize_t codec_buf_index = AMediaCodec_dequeueInputBuffer(decoder->codec, 1000);
+ if(codec_buf_index >= 0)
+ {
+ CHIAKI_LOGI(decoder->log, "Video Decoder sending EOS buffer");
+ AMediaCodec_queueInputBuffer(decoder->codec, (size_t)codec_buf_index, 0, 0, decoder->timestamp_cur++, AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM);
+ AMediaCodec_stop(decoder->codec);
+ chiaki_mutex_unlock(&decoder->codec_mutex);
+ chiaki_thread_join(&decoder->output_thread, NULL);
+ }
+ else
+ {
+ CHIAKI_LOGE(decoder->log, "Failed to get input buffer for shutting down Video Decoder!");
+ AMediaCodec_stop(decoder->codec);
+ chiaki_mutex_unlock(&decoder->codec_mutex);
+ }
+ AMediaCodec_delete(decoder->codec);
+ decoder->codec = NULL;
+ decoder->shutdown_output = false;
+}
+
+void android_chiaki_video_decoder_fini(AndroidChiakiVideoDecoder *decoder)
+{
+ if(decoder->codec)
+ kill_decoder(decoder);
+ chiaki_mutex_fini(&decoder->codec_mutex);
+}
+
+void android_chiaki_video_decoder_set_surface(AndroidChiakiVideoDecoder *decoder, JNIEnv *env, jobject surface)
+{
+ chiaki_mutex_lock(&decoder->codec_mutex);
+
+ if(!surface)
+ {
+ if(decoder->codec)
+ {
+ kill_decoder(decoder);
+ CHIAKI_LOGI(decoder->log, "Decoder shut down after surface was removed");
+ }
+ return;
+ }
+
+ if(decoder->codec)
+ {
+#if __ANDROID_API__ >= 23
+ CHIAKI_LOGI(decoder->log, "Video decoder already initialized, swapping surface");
+ ANativeWindow *new_window = surface ? ANativeWindow_fromSurface(env, surface) : NULL;
+ AMediaCodec_setOutputSurface(decoder->codec, new_window);
+ ANativeWindow_release(decoder->window);
+ decoder->window = new_window;
+#else
+ CHIAKI_LOGE(decoder->log, "Video Decoder already initialized");
+#endif
+ goto beach;
+ }
+
+ decoder->window = ANativeWindow_fromSurface(env, surface);
+
+ const char *mime = chiaki_codec_is_h265(decoder->target_codec) ? "video/hevc" : "video/avc";
+ CHIAKI_LOGI(decoder->log, "Initializing decoder with mime %s", mime);
+
+ decoder->codec = AMediaCodec_createDecoderByType(mime);
+ if(!decoder->codec)
+ {
+ CHIAKI_LOGE(decoder->log, "Failed to create AMediaCodec for mime type %s", mime);
+ goto error_surface;
+ }
+
+ AMediaFormat *format = AMediaFormat_new();
+ AMediaFormat_setString(format, AMEDIAFORMAT_KEY_MIME, mime);
+ AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_WIDTH, decoder->target_width);
+ AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_HEIGHT, decoder->target_height);
+
+ media_status_t r = AMediaCodec_configure(decoder->codec, format, decoder->window, NULL, 0);
+ if(r != AMEDIA_OK)
+ {
+ CHIAKI_LOGE(decoder->log, "AMediaCodec_configure() failed: %d", (int)r);
+ AMediaFormat_delete(format);
+ goto error_codec;
+ }
+
+ r = AMediaCodec_start(decoder->codec);
+ AMediaFormat_delete(format);
+ if(r != AMEDIA_OK)
+ {
+ CHIAKI_LOGE(decoder->log, "AMediaCodec_start() failed: %d", (int)r);
+ goto error_codec;
+ }
+
+ ChiakiErrorCode err = chiaki_thread_create(&decoder->output_thread, android_chiaki_video_decoder_output_thread_func, decoder);
+ if(err != CHIAKI_ERR_SUCCESS)
+ {
+ CHIAKI_LOGE(decoder->log, "Failed to create output thread for AMediaCodec");
+ goto error_codec;
+ }
+
+ goto beach;
+
+error_codec:
+ AMediaCodec_delete(decoder->codec);
+ decoder->codec = NULL;
+
+error_surface:
+ ANativeWindow_release(decoder->window);
+ decoder->window = NULL;
+
+beach:
+ chiaki_mutex_unlock(&decoder->codec_mutex);
+}
+
+bool android_chiaki_video_decoder_video_sample(uint8_t *buf, size_t buf_size, void *user)
+{
+ bool r = true;
+ AndroidChiakiVideoDecoder *decoder = user;
+ chiaki_mutex_lock(&decoder->codec_mutex);
+
+ if(!decoder->codec)
+ {
+ CHIAKI_LOGE(decoder->log, "Received video data, but decoder is not initialized!");
+ goto beach;
+ }
+
+ while(buf_size > 0)
+ {
+ ssize_t codec_buf_index = AMediaCodec_dequeueInputBuffer(decoder->codec, INPUT_BUFFER_TIMEOUT_MS * 1000);
+ if(codec_buf_index < 0)
+ {
+ CHIAKI_LOGE(decoder->log, "Failed to get input buffer");
+ r = false;
+ goto beach;
+ }
+
+ size_t codec_buf_size;
+ uint8_t *codec_buf = AMediaCodec_getInputBuffer(decoder->codec, (size_t)codec_buf_index, &codec_buf_size);
+ size_t codec_sample_size = buf_size;
+ if(codec_sample_size > codec_buf_size)
+ {
+ //CHIAKI_LOGD(decoder->log, "Sample is bigger than buffer, splitting");
+ codec_sample_size = codec_buf_size;
+ }
+ memcpy(codec_buf, buf, codec_sample_size);
+ media_status_t r = AMediaCodec_queueInputBuffer(decoder->codec, (size_t)codec_buf_index, 0, codec_sample_size, decoder->timestamp_cur++, 0); // timestamp just raised by 1 for maximum realtime
+ if(r != AMEDIA_OK)
+ {
+ CHIAKI_LOGE(decoder->log, "AMediaCodec_queueInputBuffer() failed: %d", (int)r);
+ }
+ buf += codec_sample_size;
+ buf_size -= codec_sample_size;
+
+ }
+
+beach:
+ chiaki_mutex_unlock(&decoder->codec_mutex);
+ return r;
+}
+
+static void *android_chiaki_video_decoder_output_thread_func(void *user)
+{
+ AndroidChiakiVideoDecoder *decoder = user;
+
+ while(1)
+ {
+ AMediaCodecBufferInfo info;
+ ssize_t status = AMediaCodec_dequeueOutputBuffer(decoder->codec, &info, -1);
+ if(status >= 0)
+ {
+ AMediaCodec_releaseOutputBuffer(decoder->codec, (size_t)status, info.size != 0);
+ if(info.flags & AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM)
+ {
+ CHIAKI_LOGI(decoder->log, "AMediaCodec reported EOS");
+ break;
+ }
+ }
+ else
+ {
+ chiaki_mutex_lock(&decoder->codec_mutex);
+ bool shutdown = decoder->shutdown_output;
+ chiaki_mutex_unlock(&decoder->codec_mutex);
+ if(shutdown)
+ {
+ CHIAKI_LOGI(decoder->log, "Video Decoder Output Thread detected shutdown after reported error");
+ break;
+ }
+ }
+ }
+
+ CHIAKI_LOGI(decoder->log, "Video Decoder Output Thread exiting");
+
+ return NULL;
+}
\ No newline at end of file
diff --git a/android/app/src/main/cpp/video-decoder.h b/android/app/src/main/cpp/video-decoder.h
new file mode 100644
index 0000000..b12ba85
--- /dev/null
+++ b/android/app/src/main/cpp/video-decoder.h
@@ -0,0 +1,33 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+#ifndef CHIAKI_JNI_VIDEO_DECODER_H
+#define CHIAKI_JNI_VIDEO_DECODER_H
+
+#include
+
+#include
+#include
+
+typedef struct AMediaCodec AMediaCodec;
+typedef struct ANativeWindow ANativeWindow;
+
+typedef struct android_chiaki_video_decoder_t
+{
+ ChiakiLog *log;
+ ChiakiMutex codec_mutex;
+ AMediaCodec *codec;
+ ANativeWindow *window;
+ uint64_t timestamp_cur;
+ ChiakiThread output_thread;
+ bool shutdown_output;
+ int32_t target_width;
+ int32_t target_height;
+ ChiakiCodec target_codec;
+} AndroidChiakiVideoDecoder;
+
+ChiakiErrorCode android_chiaki_video_decoder_init(AndroidChiakiVideoDecoder *decoder, ChiakiLog *log, int32_t target_width, int32_t target_height, ChiakiCodec codec);
+void android_chiaki_video_decoder_fini(AndroidChiakiVideoDecoder *decoder);
+void android_chiaki_video_decoder_set_surface(AndroidChiakiVideoDecoder *decoder, JNIEnv *env, jobject surface);
+bool android_chiaki_video_decoder_video_sample(uint8_t *buf, size_t buf_size, void *user);
+
+#endif
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/common/AppDatabase.kt b/android/app/src/main/java/com/metallic/chiaki/common/AppDatabase.kt
new file mode 100644
index 0000000..8398f62
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/common/AppDatabase.kt
@@ -0,0 +1,63 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.common
+
+import android.content.Context
+import androidx.room.*
+import androidx.room.migration.Migration
+import androidx.sqlite.db.SupportSQLiteDatabase
+import com.metallic.chiaki.lib.Target
+
+@Database(
+ version = 2,
+ entities = [RegisteredHost::class, ManualHost::class])
+@TypeConverters(Converters::class)
+abstract class AppDatabase: RoomDatabase()
+{
+ abstract fun registeredHostDao(): RegisteredHostDao
+ abstract fun manualHostDao(): ManualHostDao
+ abstract fun importDao(): ImportDao
+}
+
+val MIGRATION_1_2 = object : Migration(1, 2)
+{
+ override fun migrate(database: SupportSQLiteDatabase)
+ {
+ database.execSQL("ALTER TABLE registered_host ADD target INTEGER NOT NULL DEFAULT 1000")
+ database.execSQL("CREATE TABLE `new_registered_host` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `target` INTEGER NOT NULL, `ap_ssid` TEXT, `ap_bssid` TEXT, `ap_key` TEXT, `ap_name` TEXT, `server_mac` INTEGER NOT NULL, `server_nickname` TEXT, `rp_regist_key` BLOB NOT NULL, `rp_key_type` INTEGER NOT NULL, `rp_key` BLOB NOT NULL)");
+ database.execSQL("INSERT INTO `new_registered_host` SELECT `id`, `target`, `ap_ssid`, `ap_bssid`, `ap_key`, `ap_name`, `ps4_mac`, `ps4_nickname`, `rp_regist_key`, `rp_key_type`, `rp_key` FROM `registered_host`")
+ database.execSQL("DROP TABLE registered_host")
+ database.execSQL("ALTER TABLE new_registered_host RENAME TO registered_host")
+ }
+}
+
+private var database: AppDatabase? = null
+fun getDatabase(context: Context): AppDatabase
+{
+ val currentDb = database
+ if(currentDb != null)
+ return currentDb
+ val db = Room.databaseBuilder(
+ context.applicationContext,
+ AppDatabase::class.java,
+ "chiaki")
+ .addMigrations(MIGRATION_1_2)
+ .build()
+ database = db
+ return db
+}
+
+private class Converters
+{
+ @TypeConverter
+ fun macFromValue(v: Long) = MacAddress(v)
+
+ @TypeConverter
+ fun macToValue(addr: MacAddress) = addr.value
+
+ @TypeConverter
+ fun targetFromValue(v: Int) = Target.fromValue(v)
+
+ @TypeConverter
+ fun targetToValue(target: Target) = target.value
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/common/DisplayHost.kt b/android/app/src/main/java/com/metallic/chiaki/common/DisplayHost.kt
new file mode 100644
index 0000000..a25f0df
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/common/DisplayHost.kt
@@ -0,0 +1,58 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.common
+
+import com.metallic.chiaki.lib.DiscoveryHost
+
+sealed class DisplayHost
+{
+ abstract val registeredHost: RegisteredHost?
+ abstract val host: String
+ abstract val name: String?
+ abstract val id: String?
+ abstract val isPS5: Boolean
+
+ val isRegistered get() = registeredHost != null
+}
+
+class DiscoveredDisplayHost(
+ override val registeredHost: RegisteredHost?,
+ val discoveredHost: DiscoveryHost
+): DisplayHost()
+{
+ override val host get() = discoveredHost.hostAddr ?: ""
+ override val name get() = discoveredHost.hostName ?: registeredHost?.serverNickname
+ override val id get() = discoveredHost.hostId ?: registeredHost?.serverMac?.toString()
+ override val isPS5 get() = discoveredHost.isPS5
+
+ override fun equals(other: Any?): Boolean =
+ if(other !is DiscoveredDisplayHost)
+ false
+ else
+ other.discoveredHost == discoveredHost && other.registeredHost == registeredHost
+
+ override fun hashCode() = 31 * (registeredHost?.hashCode() ?: 0) + discoveredHost.hashCode()
+
+ override fun toString() = "DiscoveredDisplayHost{${registeredHost}, ${discoveredHost}}"
+}
+
+class ManualDisplayHost(
+ override val registeredHost: RegisteredHost?,
+ val manualHost: ManualHost
+): DisplayHost()
+{
+ override val host get() = manualHost.host
+ override val name get() = registeredHost?.serverNickname
+ override val id get() = registeredHost?.serverMac?.toString()
+ override val isPS5: Boolean get() = registeredHost?.target?.isPS5 ?: false
+
+ override fun equals(other: Any?): Boolean =
+ if(other !is ManualDisplayHost)
+ false
+ else
+ other.manualHost == manualHost && other.registeredHost == registeredHost
+
+ override fun hashCode() = 31 * (registeredHost?.hashCode() ?: 0) + manualHost.hashCode()
+
+ override fun toString() = "ManualDisplayHost{${registeredHost}, ${manualHost}}"
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/common/LogManager.kt b/android/app/src/main/java/com/metallic/chiaki/common/LogManager.kt
new file mode 100644
index 0000000..22c4c0c
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/common/LogManager.kt
@@ -0,0 +1,61 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.common
+
+import android.content.Context
+import java.io.File
+import java.io.FilenameFilter
+import java.text.SimpleDateFormat
+import java.util.*
+import java.util.regex.Pattern
+
+val fileProviderAuthority = "com.metallic.chiaki.fileprovider"
+private const val baseDirName = "session_logs" // must be in sync with filepaths.xml
+private val dateFormat = SimpleDateFormat("yyyy-MM-dd_HH-mm-ss-SSS", Locale.US)
+private const val filePrefix = "chiaki_session_"
+private const val filePostfix = ".log"
+private val fileRegex = Regex("$filePrefix(.*)$filePostfix")
+private const val keepLogFilesCount = 5
+
+class LogFile private constructor(val logManager: LogManager, val filename: String)
+{
+ val date = fileRegex.matchEntire(filename)?.groupValues?.get(1)?.let {
+ dateFormat.parse(it)
+ } ?: throw IllegalArgumentException()
+
+ val file get() = File(logManager.baseDir, filename)
+
+ companion object
+ {
+ fun fromFilename(logManager: LogManager, filename: String) = try { LogFile(logManager, filename) } catch(e: IllegalArgumentException) { null }
+ }
+}
+
+class LogManager(context: Context)
+{
+ val baseDir = File(context.filesDir, baseDirName).also {
+ it.mkdirs()
+ }
+
+ val files: List get() =
+ (baseDir.list { _, s -> s.matches(fileRegex) }?.toList() ?: listOf()).mapNotNull {
+ LogFile.fromFilename(this, it)
+ }.sortedByDescending {
+ it.date
+ }
+
+ fun createNewFile(): LogFile
+ {
+ val currentFiles = files
+ if(currentFiles.size > keepLogFilesCount)
+ {
+ currentFiles.subList(keepLogFilesCount, currentFiles.size).forEach {
+ it.file.delete()
+ }
+ }
+
+ val date = Date()
+ val filename = "$filePrefix${dateFormat.format(date)}$filePostfix"
+ return LogFile.fromFilename(this, filename)!!
+ }
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/common/MacAddress.kt b/android/app/src/main/java/com/metallic/chiaki/common/MacAddress.kt
new file mode 100644
index 0000000..7927d77
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/common/MacAddress.kt
@@ -0,0 +1,61 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.common
+
+import com.squareup.moshi.FromJson
+import com.squareup.moshi.JsonDataException
+import com.squareup.moshi.ToJson
+import java.nio.ByteBuffer
+import java.nio.ByteOrder
+
+class MacAddress(v: Long)
+{
+ companion object
+ {
+ val LENGTH = 6
+ }
+
+ constructor(data: ByteArray) : this(
+ if(data.size != LENGTH)
+ throw IllegalArgumentException("Data has invalid length for MAC")
+ else
+ data.let {
+ val buf = ByteBuffer.allocate(8)
+ buf.put(it, 0, LENGTH)
+ buf.order(ByteOrder.LITTLE_ENDIAN)
+ buf.getLong(0)
+ })
+
+ constructor(string: String) : this(
+ (Regex("([0-9A-Fa-f]{2})[:-]([0-9A-Fa-f]{2})[:-]([0-9A-Fa-f]{2})[:-]([0-9A-Fa-f]{2})[:-]([0-9A-Fa-f]{2})[:-]([0-9A-Fa-f]{2})").matchEntire(string)
+ ?: throw IllegalArgumentException("Invalid MAC Address String"))
+ .groupValues
+ .subList(1, 7)
+ .map { it.toUByte(16).toByte() }
+ .toByteArray())
+
+ val value: Long = v and 0xffffffffffff
+
+ override fun equals(other: Any?): Boolean =
+ if(other is MacAddress)
+ other.value == value
+ else
+ super.equals(other)
+
+ override fun hashCode() = value.hashCode()
+
+ override fun toString(): String = "%02x:%02x:%02x:%02x:%02x:%02x".format(
+ value and 0xff,
+ (value shr 0x8) and 0xff,
+ (value shr 0x10) and 0xff,
+ (value shr 0x18) and 0xff,
+ (value shr 0x20) and 0xff,
+ (value shr 0x28) and 0xff
+ )
+}
+
+class MacAddressJsonAdapter
+{
+ @ToJson fun toJson(macAddress: MacAddress) = macAddress.toString()
+ @FromJson fun fromJson(string: String) = try { MacAddress(string) } catch(e: IllegalArgumentException) { throw JsonDataException(e.message) }
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/common/ManualHost.kt b/android/app/src/main/java/com/metallic/chiaki/common/ManualHost.kt
new file mode 100644
index 0000000..7065610
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/common/ManualHost.kt
@@ -0,0 +1,59 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.common
+
+import androidx.room.*
+import androidx.room.ForeignKey.Companion.SET_NULL
+import io.reactivex.Completable
+import io.reactivex.Flowable
+import io.reactivex.Single
+
+@Entity(tableName = "manual_host",
+ foreignKeys = [
+ ForeignKey(
+ entity = RegisteredHost::class,
+ parentColumns = ["id"],
+ childColumns = ["registered_host"],
+ onDelete = SET_NULL
+ )
+])
+data class ManualHost(
+ @PrimaryKey(autoGenerate = true) val id: Long = 0,
+ val host: String,
+ @ColumnInfo(name = "registered_host") val registeredHost: Long?
+)
+
+data class ManualHostAndRegisteredHost(
+ @Embedded(prefix = "manual_host_") val manualHost: ManualHost,
+ @Embedded val registeredHost: RegisteredHost?
+)
+
+@Dao
+interface ManualHostDao
+{
+ @Query("SELECT * FROM manual_host WHERE id = :id")
+ fun getById(id: Long): Single
+
+ @Query("""SELECT
+ manual_host.id as manual_host_id,
+ manual_host.host as manual_host_host,
+ manual_host.registered_host as manual_host_registered_host,
+ registered_host.*
+ FROM manual_host LEFT OUTER JOIN registered_host ON manual_host.registered_host = registered_host.id WHERE manual_host.id = :id""")
+ fun getByIdWithRegisteredHost(id: Long): Single
+
+ @Query("SELECT * FROM manual_host")
+ fun getAll(): Flowable>
+
+ @Query("UPDATE manual_host SET registered_host = :registeredHostId WHERE id = :manualHostId")
+ fun assignRegisteredHost(manualHostId: Long, registeredHostId: Long?): Completable
+
+ @Insert
+ fun insert(host: ManualHost): Completable
+
+ @Delete
+ fun delete(host: ManualHost): Completable
+
+ @Update
+ fun update(host: ManualHost): Completable
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/common/Preferences.kt b/android/app/src/main/java/com/metallic/chiaki/common/Preferences.kt
new file mode 100644
index 0000000..3c4f2f9
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/common/Preferences.kt
@@ -0,0 +1,139 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.common
+
+import android.content.Context
+import android.content.SharedPreferences
+import androidx.annotation.StringRes
+import androidx.preference.PreferenceManager
+import com.metallic.chiaki.R
+import com.metallic.chiaki.lib.Codec
+import com.metallic.chiaki.lib.ConnectVideoProfile
+import com.metallic.chiaki.lib.VideoFPSPreset
+import com.metallic.chiaki.lib.VideoResolutionPreset
+import io.reactivex.Observable
+import io.reactivex.subjects.BehaviorSubject
+import kotlin.math.max
+import kotlin.math.min
+
+class Preferences(context: Context)
+{
+ enum class Resolution(val value: String, @StringRes val title: Int, val preset: VideoResolutionPreset)
+ {
+ RES_360P("360p", R.string.preferences_resolution_title_360p, VideoResolutionPreset.RES_360P),
+ RES_540P("540p", R.string.preferences_resolution_title_540p, VideoResolutionPreset.RES_540P),
+ RES_720P("720p", R.string.preferences_resolution_title_720p, VideoResolutionPreset.RES_720P),
+ RES_1080P("1080p", R.string.preferences_resolution_title_1080p, VideoResolutionPreset.RES_1080P),
+ }
+
+ enum class FPS(val value: String, @StringRes val title: Int, val preset: VideoFPSPreset)
+ {
+ FPS_30("30", R.string.preferences_fps_title_30, VideoFPSPreset.FPS_30),
+ FPS_60("60", R.string.preferences_fps_title_60, VideoFPSPreset.FPS_60)
+ }
+
+ enum class Codec(val value: String, @StringRes val title: Int, val codec: com.metallic.chiaki.lib.Codec)
+ {
+ CODEC_H264("h264", R.string.preferences_codec_title_h264, com.metallic.chiaki.lib.Codec.CODEC_H264),
+ CODEC_H265("h265", R.string.preferences_codec_title_h265, com.metallic.chiaki.lib.Codec.CODEC_H265)
+ }
+
+ companion object
+ {
+ val resolutionDefault = Resolution.RES_720P
+ val resolutionAll = Resolution.values()
+ val fpsDefault = FPS.FPS_60
+ val fpsAll = FPS.values()
+ val codecDefault = Codec.CODEC_H265
+ val codecAll = Codec.values()
+ }
+
+ private val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
+ private val sharedPreferenceChangeListener = SharedPreferences.OnSharedPreferenceChangeListener { _, key ->
+ when(key)
+ {
+ resolutionKey -> bitrateAutoSubject.onNext(bitrateAuto)
+ }
+ }.also { sharedPreferences.registerOnSharedPreferenceChangeListener(it) }
+
+ private val resources = context.resources
+
+ val discoveryEnabledKey get() = resources.getString(R.string.preferences_discovery_enabled_key)
+ var discoveryEnabled
+ get() = sharedPreferences.getBoolean(discoveryEnabledKey, true)
+ set(value) { sharedPreferences.edit().putBoolean(discoveryEnabledKey, value).apply() }
+
+ val onScreenControlsEnabledKey get() = resources.getString(R.string.preferences_on_screen_controls_enabled_key)
+ var onScreenControlsEnabled
+ get() = sharedPreferences.getBoolean(onScreenControlsEnabledKey, true)
+ set(value) { sharedPreferences.edit().putBoolean(onScreenControlsEnabledKey, value).apply() }
+
+ val touchpadOnlyEnabledKey get() = resources.getString(R.string.preferences_touchpad_only_enabled_key)
+ var touchpadOnlyEnabled
+ get() = sharedPreferences.getBoolean(touchpadOnlyEnabledKey, false)
+ set(value) { sharedPreferences.edit().putBoolean(touchpadOnlyEnabledKey, value).apply() }
+
+ val rumbleEnabledKey get() = resources.getString(R.string.preferences_rumble_enabled_key)
+ var rumbleEnabled
+ get() = sharedPreferences.getBoolean(rumbleEnabledKey, true)
+ set(value) { sharedPreferences.edit().putBoolean(rumbleEnabledKey, value).apply() }
+
+ val motionEnabledKey get() = resources.getString(R.string.preferences_motion_enabled_key)
+ var motionEnabled
+ get() = sharedPreferences.getBoolean(motionEnabledKey, true)
+ set(value) { sharedPreferences.edit().putBoolean(motionEnabledKey, value).apply() }
+
+ val buttonHapticEnabledKey get() = resources.getString(R.string.preferences_button_haptic_enabled_key)
+ var buttonHapticEnabled
+ get() = sharedPreferences.getBoolean(buttonHapticEnabledKey, true)
+ set(value) { sharedPreferences.edit().putBoolean(buttonHapticEnabledKey, value).apply() }
+
+ val logVerboseKey get() = resources.getString(R.string.preferences_log_verbose_key)
+ var logVerbose
+ get() = sharedPreferences.getBoolean(logVerboseKey, false)
+ set(value) { sharedPreferences.edit().putBoolean(logVerboseKey, value).apply() }
+
+ val swapCrossMoonKey get() = resources.getString(R.string.preferences_swap_cross_moon_key)
+ var swapCrossMoon
+ get() = sharedPreferences.getBoolean(swapCrossMoonKey, false)
+ set(value) { sharedPreferences.edit().putBoolean(swapCrossMoonKey, value).apply() }
+
+ val resolutionKey get() = resources.getString(R.string.preferences_resolution_key)
+ var resolution
+ get() = sharedPreferences.getString(resolutionKey, resolutionDefault.value)?.let { value ->
+ Resolution.values().firstOrNull { it.value == value }
+ } ?: resolutionDefault
+ set(value) { sharedPreferences.edit().putString(resolutionKey, value.value).apply() }
+
+ val fpsKey get() = resources.getString(R.string.preferences_fps_key)
+ var fps
+ get() = sharedPreferences.getString(fpsKey, fpsDefault.value)?.let { value ->
+ FPS.values().firstOrNull { it.value == value }
+ } ?: fpsDefault
+ set(value) { sharedPreferences.edit().putString(fpsKey, value.value).apply() }
+
+ fun validateBitrate(bitrate: Int) = max(2000, min(50000, bitrate))
+ val bitrateKey get() = resources.getString(R.string.preferences_bitrate_key)
+ var bitrate
+ get() = sharedPreferences.getInt(bitrateKey, 0).let { if(it == 0) null else validateBitrate(it) }
+ set(value) { sharedPreferences.edit().putInt(bitrateKey, if(value != null) validateBitrate(value) else 0).apply() }
+ val bitrateAuto get() = videoProfileDefaultBitrate.bitrate
+ private val bitrateAutoSubject by lazy { BehaviorSubject.createDefault(bitrateAuto) }
+ val bitrateAutoObservable: Observable get() = bitrateAutoSubject
+
+ val codecKey get() = resources.getString(R.string.preferences_codec_key)
+ var codec
+ get() = sharedPreferences.getString(codecKey, codecDefault.value)?.let { value ->
+ Codec.values().firstOrNull { it.value == value }
+ } ?: codecDefault
+ set(value) { sharedPreferences.edit().putString(codecKey, value.value).apply() }
+
+ private val videoProfileDefaultBitrate get() = ConnectVideoProfile.preset(resolution.preset, fps.preset, codec.codec)
+ val videoProfile get() = videoProfileDefaultBitrate.let {
+ val bitrate = bitrate
+ if(bitrate == null)
+ it
+ else
+ it.copy(bitrate = bitrate)
+ }
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/common/RegisteredHost.kt b/android/app/src/main/java/com/metallic/chiaki/common/RegisteredHost.kt
new file mode 100644
index 0000000..d920a8c
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/common/RegisteredHost.kt
@@ -0,0 +1,101 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.common
+
+import androidx.room.*
+import androidx.room.ColumnInfo.Companion.BLOB
+import com.metallic.chiaki.lib.RegistHost
+import com.metallic.chiaki.lib.Target
+import io.reactivex.Completable
+import io.reactivex.Flowable
+import io.reactivex.Maybe
+import io.reactivex.Single
+
+@Entity(tableName = "registered_host")
+data class RegisteredHost(
+ @PrimaryKey(autoGenerate = true) val id: Long = 0,
+ @ColumnInfo(name = "target") val target: Target,
+ @ColumnInfo(name = "ap_ssid") val apSsid: String?,
+ @ColumnInfo(name = "ap_bssid") val apBssid: String?,
+ @ColumnInfo(name = "ap_key") val apKey: String?,
+ @ColumnInfo(name = "ap_name") val apName: String?,
+ @ColumnInfo(name = "server_mac") val serverMac: MacAddress,
+ @ColumnInfo(name = "server_nickname") val serverNickname: String?,
+ @ColumnInfo(name = "rp_regist_key", typeAffinity = BLOB) val rpRegistKey: ByteArray, // CHIAKI_SESSION_AUTH_SIZE
+ @ColumnInfo(name = "rp_key_type") val rpKeyType: Int,
+ @ColumnInfo(name = "rp_key", typeAffinity = BLOB) val rpKey: ByteArray // 0x10
+)
+{
+ constructor(registHost: RegistHost) : this(
+ target = registHost.target,
+ apSsid = registHost.apSsid,
+ apBssid = registHost.apBssid,
+ apKey = registHost.apKey,
+ apName = registHost.apName,
+ serverMac = MacAddress(registHost.serverMac),
+ serverNickname = registHost.serverNickname,
+ rpRegistKey = registHost.rpRegistKey,
+ rpKeyType = registHost.rpKeyType.toInt(),
+ rpKey = registHost.rpKey
+ )
+
+ override fun equals(other: Any?): Boolean
+ {
+ if(this === other) return true
+ if(javaClass != other?.javaClass) return false
+
+ other as RegisteredHost
+
+ if(id != other.id) return false
+ if(target != other.target) return false
+ if(apSsid != other.apSsid) return false
+ if(apBssid != other.apBssid) return false
+ if(apKey != other.apKey) return false
+ if(apName != other.apName) return false
+ if(serverMac != other.serverMac) return false
+ if(serverNickname != other.serverNickname) return false
+ if(!rpRegistKey.contentEquals(other.rpRegistKey)) return false
+ if(rpKeyType != other.rpKeyType) return false
+ if(!rpKey.contentEquals(other.rpKey)) return false
+
+ return true
+ }
+
+ override fun hashCode(): Int
+ {
+ var result = id.hashCode()
+ result = 31 * result + target.hashCode()
+ result = 31 * result + (apSsid?.hashCode() ?: 0)
+ result = 31 * result + (apBssid?.hashCode() ?: 0)
+ result = 31 * result + (apKey?.hashCode() ?: 0)
+ result = 31 * result + (apName?.hashCode() ?: 0)
+ result = 31 * result + serverMac.hashCode()
+ result = 31 * result + (serverNickname?.hashCode() ?: 0)
+ result = 31 * result + rpRegistKey.contentHashCode()
+ result = 31 * result + rpKeyType
+ result = 31 * result + rpKey.contentHashCode()
+ return result
+ }
+}
+
+@Dao
+interface RegisteredHostDao
+{
+ @Query("SELECT * FROM registered_host")
+ fun getAll(): Flowable>
+
+ @Query("SELECT * FROM registered_host WHERE server_mac == :mac LIMIT 1")
+ fun getByMac(mac: MacAddress): Maybe
+
+ @Query("DELETE FROM registered_host WHERE server_mac == :mac")
+ fun deleteByMac(mac: MacAddress): Completable
+
+ @Delete
+ fun delete(host: RegisteredHost): Completable
+
+ @Query("SELECT COUNT(*) FROM registered_host")
+ fun count(): Flowable
+
+ @Insert
+ fun insert(host: RegisteredHost): Single
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/common/SerializedSettings.kt b/android/app/src/main/java/com/metallic/chiaki/common/SerializedSettings.kt
new file mode 100644
index 0000000..74257ac
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/common/SerializedSettings.kt
@@ -0,0 +1,289 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.common
+
+import android.app.Activity
+import android.content.ClipData
+import android.content.Intent
+import android.net.Uri
+import android.util.Base64
+import android.util.Log
+import androidx.core.content.FileProvider
+import androidx.room.*
+import com.google.android.material.dialog.MaterialAlertDialogBuilder
+import com.metallic.chiaki.R
+import com.metallic.chiaki.lib.Target
+import com.squareup.moshi.*
+import io.reactivex.Completable
+import io.reactivex.Flowable
+import io.reactivex.Single
+import io.reactivex.android.schedulers.AndroidSchedulers
+import io.reactivex.disposables.CompositeDisposable
+import io.reactivex.disposables.Disposable
+import io.reactivex.rxkotlin.Singles
+import io.reactivex.rxkotlin.addTo
+import io.reactivex.schedulers.Schedulers
+import okio.Buffer
+import okio.Okio
+import okio.buffer
+import okio.source
+import java.io.File
+import java.io.IOException
+
+@JsonClass(generateAdapter = true)
+class SerializedRegisteredHost(
+ @Json(name = "target") val target: Target,
+ @Json(name = "ap_ssid") val apSsid: String?,
+ @Json(name = "ap_bssid") val apBssid: String?,
+ @Json(name = "ap_key") val apKey: String?,
+ @Json(name = "ap_name") val apName: String?,
+ @Json(name = "server_mac") val serverMac: MacAddress,
+ @Json(name = "server_nickname") val serverNickname: String?,
+ @Json(name = "rp_regist_key") val rpRegistKey: ByteArray,
+ @Json(name = "rp_key_type") val rpKeyType: Int,
+ @Json(name = "rp_key") val rpKey: ByteArray
+){
+ constructor(registeredHost: RegisteredHost) : this(
+ registeredHost.target,
+ registeredHost.apSsid,
+ registeredHost.apBssid,
+ registeredHost.apKey,
+ registeredHost.apName,
+ registeredHost.serverMac,
+ registeredHost.serverNickname,
+ registeredHost.rpRegistKey,
+ registeredHost.rpKeyType,
+ registeredHost.rpKey
+ )
+}
+
+@JsonClass(generateAdapter = true)
+class SerializedManualHost(
+ @Json(name = "host") val host: String,
+ @Json(name = "server_mac") val serverMac: MacAddress?
+)
+
+@JsonClass(generateAdapter = true)
+data class SerializedSettings(
+ @Json(name = "registered_hosts") val registeredHosts: List,
+ @Json(name = "manual_hosts") val manualHosts: List
+)
+{
+ companion object
+ {
+ fun fromDatabase(db: AppDatabase) = Singles.zip(
+ db.registeredHostDao().getAll().firstOrError(),
+ db.manualHostDao().getAll().firstOrError()
+ ) { registeredHosts, manualHosts ->
+ SerializedSettings(
+ registeredHosts.map { SerializedRegisteredHost(it) },
+ manualHosts.map { manualHost ->
+ SerializedManualHost(
+ manualHost.host,
+ manualHost.registeredHost?.let { registeredHostId ->
+ registeredHosts.firstOrNull { it.id == registeredHostId }
+ }?.serverMac
+ )
+ })
+ }
+ }
+}
+
+private class ByteArrayJsonAdapter
+{
+ @ToJson fun toJson(byteArray: ByteArray) = Base64.encodeToString(byteArray, Base64.NO_WRAP)
+ @FromJson fun fromJson(string: String) = Base64.decode(string, Base64.DEFAULT)
+}
+
+private fun moshi() =
+ Moshi.Builder()
+ .add(MacAddressJsonAdapter())
+ .add(ByteArrayJsonAdapter())
+ .build()
+
+private fun Moshi.serializedSettingsAdapter() =
+ adapter(SerializedSettings::class.java)
+ .serializeNulls()
+
+private const val KEY_FORMAT = "format"
+private const val FORMAT = "chiaki-settings"
+private const val KEY_VERSION = "version"
+private const val VERSION = 2
+private const val KEY_SETTINGS = "settings"
+
+fun exportAllSettings(db: AppDatabase) = SerializedSettings.fromDatabase(db)
+ .subscribeOn(Schedulers.io())
+ .map {
+ val buffer = Buffer()
+ val writer = JsonWriter.of(buffer)
+ val adapter = moshi().serializedSettingsAdapter()
+ writer.indent = " "
+ writer.
+ beginObject()
+ .name(KEY_FORMAT).value(FORMAT)
+ .name(KEY_VERSION).value(VERSION)
+ writer.name(KEY_SETTINGS)
+ adapter.toJson(writer, it)
+ writer.endObject()
+ buffer.readUtf8()
+ }
+
+fun exportAndShareAllSettings(activity: Activity): Disposable
+{
+ val db = getDatabase(activity)
+ val dir = File(activity.cacheDir, "export_settings")
+ dir.mkdirs()
+ val file = File(dir, "chiaki-settings.json")
+ return exportAllSettings(db)
+ .map {
+ file.writeText(it, Charsets.UTF_8)
+ file
+ }
+ .observeOn(AndroidSchedulers.mainThread())
+ .subscribe { _ ->
+ val uri = FileProvider.getUriForFile(activity, fileProviderAuthority, file)
+ Intent(Intent.ACTION_SEND).also {
+ it.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
+ it.type = "application/json"
+ it.putExtra(Intent.EXTRA_STREAM, uri)
+ it.clipData = ClipData.newRawUri("", uri)
+ activity.startActivity(Intent.createChooser(it, activity.getString(R.string.action_share_log)))
+ }
+ }
+}
+
+fun importSettingsFromUri(activity: Activity, uri: Uri, disposable: CompositeDisposable)
+{
+ fun loadFail(msg: String)
+ {
+ MaterialAlertDialogBuilder(activity)
+ .setMessage(activity.getString(R.string.alert_message_import_failed, msg))
+ .setPositiveButton(R.string.action_import_failed_ack) { _, _ -> }
+ .create()
+ .show()
+ }
+
+ try
+ {
+ val inputStream = activity.contentResolver.openInputStream(uri) ?: throw IOException()
+ val buffer = inputStream.source().buffer()
+ val reader = JsonReader.of(buffer)
+ val adapter = moshi().serializedSettingsAdapter()
+
+ var format: String? = null
+ var version: Int? = null
+ var settingsValue: Any? = null
+
+ reader.beginObject()
+ while(reader.hasNext())
+ {
+ when(reader.nextName())
+ {
+ KEY_FORMAT -> format = reader.nextString()
+ KEY_VERSION -> version = reader.nextInt()
+ KEY_SETTINGS -> settingsValue = reader.readJsonValue()
+ }
+ }
+ reader.endObject()
+
+ if(format == null || version == null || settingsValue == null)
+ throw IOException("Missing format, version or settings from JSON")
+ if(format != FORMAT)
+ throw IOException("Value of format is invalid")
+ if(version != VERSION) // Add migrations here when necessary
+ throw IOException("Value of version is invalid")
+
+ val settings = adapter.fromJsonValue(settingsValue) ?: throw JsonDataException("Failed to parse Settings JSON")
+ Log.i("SerializedSettings", "would import: $settings")
+
+ MaterialAlertDialogBuilder(activity)
+ .setMessage(activity.getString(R.string.alert_message_import,
+ settings.registeredHosts.let {
+ if(it.isEmpty())
+ "-"
+ else
+ it.joinToString(separator = "") { host -> "\n - ${host.serverNickname ?: "?"} / ${host.serverMac}" }
+ },
+ settings.manualHosts.let {
+ if(it.isEmpty())
+ "-"
+ else
+ it.joinToString(separator = "") { host -> "\n - ${host.host} / ${host.serverMac ?: "unregistered"}" }
+ }
+ ))
+ .setTitle(R.string.alert_title_import)
+ .setNegativeButton(R.string.action_import_cancel) { _, _ -> }
+ .setPositiveButton(R.string.action_import_import) { _, _ ->
+ getDatabase(activity).importDao()
+ .importCompletable(settings)
+ .subscribeOn(Schedulers.io())
+ .observeOn(AndroidSchedulers.mainThread())
+ .subscribe()
+ .addTo(disposable)
+ }
+ .create()
+ .show()
+ }
+ catch(e: IOException)
+ {
+ e.printStackTrace()
+ loadFail(e.message ?: "")
+ }
+ catch(e: JsonDataException)
+ {
+ e.printStackTrace()
+ loadFail(e.message ?: "")
+ }
+}
+
+@Dao
+abstract class ImportDao
+{
+ @Insert
+ abstract fun insertRegisteredHosts(hosts: List)
+
+ @Insert
+ abstract fun insertManualHosts(hosts: List)
+
+ class IdWithMac(val id: Long, val mac: MacAddress)
+
+ @Query("SELECT id, server_mac AS mac FROM registered_host WHERE server_mac IN (:macs)")
+ abstract fun registeredHostsByMac(macs: List): List
+
+ @Transaction
+ fun import(settings: SerializedSettings)
+ {
+ insertRegisteredHosts(
+ settings.registeredHosts.map {
+ RegisteredHost(
+ target = it.target,
+ apSsid = it.apSsid,
+ apBssid = it.apBssid,
+ apKey = it.apKey,
+ apName = it.apName,
+ serverMac = it.serverMac,
+ serverNickname = it.serverNickname,
+ rpRegistKey = it.rpRegistKey,
+ rpKeyType = it.rpKeyType,
+ rpKey = it.rpKey
+ )
+ })
+
+ val macs = settings.manualHosts.mapNotNull { it.serverMac }
+ val idMacs =
+ if(macs.isNotEmpty())
+ registeredHostsByMac(macs)
+ else
+ listOf()
+
+ insertManualHosts(
+ settings.manualHosts.map {
+ ManualHost(
+ host = it.host,
+ registeredHost = idMacs.firstOrNull { regHost -> regHost.mac == it.serverMac }?.id
+ )
+ })
+ }
+}
+
+private fun ImportDao.importCompletable(settings: SerializedSettings) = Completable.fromCallable { import(settings) }
diff --git a/android/app/src/main/java/com/metallic/chiaki/common/ext/RevealActivity.kt b/android/app/src/main/java/com/metallic/chiaki/common/ext/RevealActivity.kt
new file mode 100644
index 0000000..321e0bd
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/common/ext/RevealActivity.kt
@@ -0,0 +1,63 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.common.ext
+
+import android.content.Intent
+import android.graphics.Rect
+import android.view.*
+import android.view.animation.AccelerateInterpolator
+import kotlin.math.max
+
+interface RevealActivity
+{
+ companion object
+ {
+ const val EXTRA_REVEAL_X = "reveal_x"
+ const val EXTRA_REVEAL_Y = "reveal_y"
+ }
+
+ val revealRootLayout: View
+ val revealIntent: Intent
+ val revealWindow: Window
+
+ private fun revealActivity(x: Int, y: Int)
+ {
+ val finalRadius = max(revealRootLayout.width, revealRootLayout.height).toFloat()
+ val reveal = ViewAnimationUtils.createCircularReveal(revealRootLayout, x, y, 0f, finalRadius)
+ reveal.interpolator = AccelerateInterpolator()
+ revealRootLayout.visibility = View.VISIBLE
+ reveal.start()
+ }
+
+ fun handleReveal()
+ {
+ if(!revealIntent.hasExtra(EXTRA_REVEAL_X) || !revealIntent.hasExtra(EXTRA_REVEAL_Y))
+ return
+ revealWindow.setBackgroundDrawableResource(android.R.color.transparent)
+ val revealX = revealIntent.getIntExtra(EXTRA_REVEAL_X, 0)
+ val revealY = revealIntent.getIntExtra(EXTRA_REVEAL_Y, 0)
+ revealRootLayout.visibility = View.INVISIBLE
+
+ revealRootLayout.viewTreeObserver.also {
+ if(it.isAlive)
+ {
+ it.addOnGlobalLayoutListener(object: ViewTreeObserver.OnGlobalLayoutListener {
+ override fun onGlobalLayout()
+ {
+ revealActivity(revealX, revealY)
+ revealRootLayout.viewTreeObserver.removeOnGlobalLayoutListener(this)
+ }
+ })
+ }
+ }
+ }
+}
+
+fun Intent.putRevealExtra(originView: View, rootLayout: ViewGroup)
+{
+ val offsetRect = Rect()
+ originView.getDrawingRect(offsetRect)
+ rootLayout.offsetDescendantRectToMyCoords(originView, offsetRect)
+ putExtra(RevealActivity.EXTRA_REVEAL_X, offsetRect.left)
+ putExtra(RevealActivity.EXTRA_REVEAL_Y, offsetRect.top)
+}
diff --git a/android/app/src/main/java/com/metallic/chiaki/common/ext/RxLiveData.kt b/android/app/src/main/java/com/metallic/chiaki/common/ext/RxLiveData.kt
new file mode 100644
index 0000000..bd98458
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/common/ext/RxLiveData.kt
@@ -0,0 +1,13 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.common.ext
+
+import androidx.lifecycle.LiveDataReactiveStreams
+import io.reactivex.BackpressureStrategy
+import io.reactivex.Observable
+import io.reactivex.Single
+import org.reactivestreams.Publisher
+
+fun Publisher.toLiveData() = LiveDataReactiveStreams.fromPublisher(this)
+fun Observable.toLiveData() = this.toFlowable(BackpressureStrategy.LATEST).toLiveData()
+fun Single.toLiveData() = this.toFlowable().toLiveData()
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/common/ext/StringHex.kt b/android/app/src/main/java/com/metallic/chiaki/common/ext/StringHex.kt
new file mode 100644
index 0000000..6212ddc
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/common/ext/StringHex.kt
@@ -0,0 +1,7 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.common.ext
+
+fun String.hexToByteArray(): ByteArray? = ByteArray(this.length / 2) {
+ this.substring(it * 2, it * 2 + 2).toIntOrNull(16)?.toByte() ?: return null
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/common/ext/ViewGroupInflate.kt b/android/app/src/main/java/com/metallic/chiaki/common/ext/ViewGroupInflate.kt
new file mode 100644
index 0000000..0b613cc
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/common/ext/ViewGroupInflate.kt
@@ -0,0 +1,13 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.common.ext
+
+import android.view.LayoutInflater
+import android.view.View
+import android.view.ViewGroup
+import androidx.annotation.LayoutRes
+
+fun ViewGroup.inflate(@LayoutRes layoutRes: Int, attachToRoot: Boolean = false): View
+{
+ return LayoutInflater.from(context).inflate(layoutRes, this, attachToRoot)
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/common/ext/ViewModelFactory.kt b/android/app/src/main/java/com/metallic/chiaki/common/ext/ViewModelFactory.kt
new file mode 100644
index 0000000..addfab3
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/common/ext/ViewModelFactory.kt
@@ -0,0 +1,11 @@
+package com.metallic.chiaki.common.ext
+
+import androidx.lifecycle.ViewModel
+import androidx.lifecycle.ViewModelProvider
+
+inline fun viewModelFactory(crossinline f: () -> T) =
+ object : ViewModelProvider.Factory
+ {
+ @Suppress("UNCHECKED_CAST")
+ override fun create(aClass: Class): T = f() as T
+ }
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/discovery/DiscoveryManager.kt b/android/app/src/main/java/com/metallic/chiaki/discovery/DiscoveryManager.kt
new file mode 100644
index 0000000..e7b2e3e
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/discovery/DiscoveryManager.kt
@@ -0,0 +1,130 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.discovery
+
+import android.util.Log
+import com.metallic.chiaki.common.MacAddress
+import com.metallic.chiaki.common.ext.hexToByteArray
+import com.metallic.chiaki.lib.CreateError
+import com.metallic.chiaki.lib.DiscoveryHost
+import com.metallic.chiaki.lib.DiscoveryService
+import com.metallic.chiaki.lib.DiscoveryServiceOptions
+import io.reactivex.Observable
+import io.reactivex.android.schedulers.AndroidSchedulers
+import io.reactivex.disposables.CompositeDisposable
+import io.reactivex.rxkotlin.addTo
+import io.reactivex.subjects.BehaviorSubject
+import io.reactivex.subjects.Subject
+import java.lang.NumberFormatException
+import java.net.InetSocketAddress
+import java.nio.charset.Charset
+import java.nio.charset.StandardCharsets
+import java.util.concurrent.TimeUnit
+
+val DiscoveryHost.serverMac get() = this.hostId?.hexToByteArray()?.let {
+ if(it.size == MacAddress.LENGTH)
+ MacAddress(it)
+ else
+ null
+}
+
+class DiscoveryManager
+{
+ companion object
+ {
+ const val HOSTS_MAX: ULong = 16U
+ const val DROP_PINGS: ULong = 3U
+ const val PING_MS: ULong = 500U
+ const val PORT = 987
+
+ const val DEBOUNCE_EMPTY_MS = 1000L
+ }
+
+ private var discoveryService: DiscoveryService? = null
+
+ private val discoveryActiveSubject: Subject = BehaviorSubject.create().also { it.onNext(false) }
+ val discoveryActive: Observable get() = discoveryActiveSubject
+ var active = false
+ set(value)
+ {
+ field = value
+ discoveryActiveSubject.onNext(value)
+ updateService()
+ }
+ private var paused = false
+
+ private val disposable = CompositeDisposable()
+
+ private var discoveredHostsSubjectDebounced: Subject> = BehaviorSubject.create>().also {
+ it.onNext(listOf())
+ }.toSerialized()
+
+ private var discoveredHostsSubjectRaw: Subject> = BehaviorSubject.create>().also { subject ->
+ subject.debounce { hosts ->
+ if(hosts.isEmpty())
+ Observable.timer(DEBOUNCE_EMPTY_MS, TimeUnit.MILLISECONDS)
+ else
+ Observable.empty()
+ }
+ .subscribe { hosts ->
+ discoveredHostsSubjectDebounced.onNext(hosts)
+ }
+ .addTo(disposable)
+ }
+
+ val discoveredHosts: Observable> get() = discoveredHostsSubjectDebounced
+
+ fun resume()
+ {
+ paused = false
+ updateService()
+ }
+
+ fun pause()
+ {
+ paused = true
+ updateService()
+ }
+
+ fun dispose()
+ {
+ active = false
+ disposable.dispose()
+ }
+
+ fun sendWakeup(host: String, registKey: ByteArray, ps5: Boolean)
+ {
+ val registKeyString = registKey.indexOfFirst { it == 0.toByte() }.let { end -> registKey.copyOfRange(0, if(end >= 0) end else registKey.size) }.toString(StandardCharsets.UTF_8)
+ val credential = try { registKeyString.toULong(16) } catch(e: NumberFormatException) {
+ Log.e("DiscoveryManager", "Failed to convert registKey to int", e)
+ return
+ }
+ DiscoveryService.wakeup(discoveryService, host, credential, ps5)
+ }
+
+ private fun updateService()
+ {
+ if(active && !paused && discoveryService == null)
+ {
+ discoveredHostsSubjectRaw.onNext(listOf())
+ try
+ {
+ discoveryService = DiscoveryService(DiscoveryServiceOptions(
+ HOSTS_MAX, DROP_PINGS, PING_MS, InetSocketAddress("255.255.255.255", PORT)
+ ), discoveredHostsSubjectRaw::onNext)
+ }
+ catch(e: CreateError)
+ {
+ Log.e("DiscoveryManager", "Failed to start Discovery Service: $e")
+ }
+ }
+ else if((!active || paused) && discoveryService != null)
+ {
+ val service = discoveryService ?: return
+ service.dispose()
+ discoveryService = null
+ if(!active)
+ discoveredHostsSubjectRaw.onNext(listOf())
+ }
+ }
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/lib/Chiaki.kt b/android/app/src/main/java/com/metallic/chiaki/lib/Chiaki.kt
new file mode 100644
index 0000000..bb0b0a6
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/lib/Chiaki.kt
@@ -0,0 +1,534 @@
+package com.metallic.chiaki.lib
+
+import android.os.Parcelable
+import android.util.Log
+import android.view.Surface
+import kotlinx.parcelize.Parcelize
+import java.lang.Exception
+import java.net.InetSocketAddress
+import kotlin.math.abs
+
+enum class Target(val value: Int)
+{
+ PS4_UNKNOWN(0),
+ PS4_8(800),
+ PS4_9(900),
+ PS4_10(1000),
+ PS5_UNKNOWN(1000000),
+ PS5_1(1000100);
+
+ companion object
+ {
+ @JvmStatic
+ fun fromValue(value: Int) = values().firstOrNull { it.value == value } ?: PS4_10
+ }
+
+ val isPS5 get() = value >= PS5_UNKNOWN.value
+}
+
+enum class VideoResolutionPreset(val value: Int)
+{
+ RES_360P(1),
+ RES_540P(2),
+ RES_720P(3),
+ RES_1080P(4)
+}
+
+enum class VideoFPSPreset(val value: Int)
+{
+ FPS_30(30),
+ FPS_60(60)
+}
+
+enum class Codec(val value: Int)
+{
+ CODEC_H264(0),
+ CODEC_H265(1),
+ CODEC_H265_HDR(2)
+}
+
+@Parcelize
+data class ConnectVideoProfile(
+ val width: Int,
+ val height: Int,
+ val maxFPS: Int,
+ val bitrate: Int,
+ val codec: Codec
+): Parcelable
+{
+ companion object
+ {
+ fun preset(resolutionPreset: VideoResolutionPreset, fpsPreset: VideoFPSPreset, codec: Codec)
+ = ChiakiNative.videoProfilePreset(resolutionPreset.value, fpsPreset.value, codec)
+ }
+}
+
+@Parcelize
+data class ConnectInfo(
+ val ps5: Boolean,
+ val host: String,
+ val registKey: ByteArray,
+ val morning: ByteArray,
+ val videoProfile: ConnectVideoProfile
+): Parcelable
+
+private class ChiakiNative
+{
+ data class CreateResult(var errorCode: Int, var ptr: Long)
+ companion object
+ {
+ init
+ {
+ System.loadLibrary("chiaki-jni")
+ }
+ @JvmStatic external fun errorCodeToString(value: Int): String
+ @JvmStatic external fun quitReasonToString(value: Int): String
+ @JvmStatic external fun quitReasonIsError(value: Int): Boolean
+ @JvmStatic external fun videoProfilePreset(resolutionPreset: Int, fpsPreset: Int, codec: Codec): ConnectVideoProfile
+ @JvmStatic external fun sessionCreate(result: CreateResult, connectInfo: ConnectInfo, logFile: String?, logVerbose: Boolean, javaSession: Session)
+ @JvmStatic external fun sessionFree(ptr: Long)
+ @JvmStatic external fun sessionStart(ptr: Long): Int
+ @JvmStatic external fun sessionStop(ptr: Long): Int
+ @JvmStatic external fun sessionJoin(ptr: Long): Int
+ @JvmStatic external fun sessionSetSurface(ptr: Long, surface: Surface?)
+ @JvmStatic external fun sessionSetControllerState(ptr: Long, controllerState: ControllerState)
+ @JvmStatic external fun sessionSetLoginPin(ptr: Long, pin: String)
+ @JvmStatic external fun discoveryServiceCreate(result: CreateResult, options: DiscoveryServiceOptions, javaService: DiscoveryService)
+ @JvmStatic external fun discoveryServiceFree(ptr: Long)
+ @JvmStatic external fun discoveryServiceWakeup(ptr: Long, host: String, userCredential: Long, ps5: Boolean)
+ @JvmStatic external fun registStart(result: CreateResult, registInfo: RegistInfo, javaLog: ChiakiLog, javaRegist: Regist)
+ @JvmStatic external fun registStop(ptr: Long)
+ @JvmStatic external fun registFree(ptr: Long)
+ }
+}
+
+class ErrorCode(val value: Int)
+{
+ override fun toString() = ChiakiNative.errorCodeToString(value)
+ var isSuccess = value == 0
+}
+
+class ChiakiLog(val levelMask: Int, val callback: (level: Int, text: String) -> Unit)
+{
+ companion object
+ {
+ fun formatLog(level: Int, text: String) =
+ "[${when(level)
+ {
+ Level.DEBUG.value -> "D"
+ Level.VERBOSE.value -> "V"
+ Level.INFO.value -> "I"
+ Level.WARNING.value -> "W"
+ Level.ERROR.value -> "E"
+ else -> "?"
+ }
+ }] $text"
+ }
+
+ enum class Level(val value: Int)
+ {
+ DEBUG(1 shl 4),
+ VERBOSE(1 shl 3),
+ INFO(1 shl 2),
+ WARNING(1 shl 1),
+ ERROR(1 shl 0),
+ ALL(0.inv())
+ }
+
+ private fun log(level: Int, text: String)
+ {
+ callback(level, text)
+ }
+
+ fun d(text: String) = log(Level.DEBUG.value, text)
+ fun v(text: String) = log(Level.VERBOSE.value, text)
+ fun i(text: String) = log(Level.INFO.value, text)
+ fun w(text: String) = log(Level.WARNING.value, text)
+ fun e(text: String) = log(Level.ERROR.value, text)
+}
+
+private fun maxAbs(a: Short, b: Short) = if(abs(a.toInt()) > abs(b.toInt())) a else b
+
+private val CONTROLLER_TOUCHES_MAX = 2 // must be the same as CHIAKI_CONTROLLER_TOUCHES_MAX
+
+data class ControllerTouch(
+ var x: UShort = 0U,
+ var y: UShort = 0U,
+ var id: Byte = -1 // -1 = up
+)
+
+data class ControllerState constructor(
+ var buttons: UInt = 0U,
+ var l2State: UByte = 0U,
+ var r2State: UByte = 0U,
+ var leftX: Short = 0,
+ var leftY: Short = 0,
+ var rightX: Short = 0,
+ var rightY: Short = 0,
+ private var touchIdNext: UByte = 0U,
+ var touches: Array = arrayOf(ControllerTouch(), ControllerTouch()),
+ var gyroX: Float = 0.0f,
+ var gyroY: Float = 0.0f,
+ var gyroZ: Float = 0.0f,
+ var accelX: Float = 0.0f,
+ var accelY: Float = 1.0f,
+ var accelZ: Float = 0.0f,
+ var orientX: Float = 0.0f,
+ var orientY: Float = 0.0f,
+ var orientZ: Float = 0.0f,
+ var orientW: Float = 1.0f
+){
+ companion object
+ {
+ val BUTTON_CROSS = (1 shl 0).toUInt()
+ val BUTTON_MOON = (1 shl 1).toUInt()
+ val BUTTON_BOX = (1 shl 2).toUInt()
+ val BUTTON_PYRAMID = (1 shl 3).toUInt()
+ val BUTTON_DPAD_LEFT = (1 shl 4).toUInt()
+ val BUTTON_DPAD_RIGHT = (1 shl 5).toUInt()
+ val BUTTON_DPAD_UP = (1 shl 6).toUInt()
+ val BUTTON_DPAD_DOWN = (1 shl 7).toUInt()
+ val BUTTON_L1 = (1 shl 8).toUInt()
+ val BUTTON_R1 = (1 shl 9).toUInt()
+ val BUTTON_L3 = (1 shl 10).toUInt()
+ val BUTTON_R3 = (1 shl 11).toUInt()
+ val BUTTON_OPTIONS = (1 shl 12).toUInt()
+ val BUTTON_SHARE = (1 shl 13).toUInt()
+ val BUTTON_TOUCHPAD = (1 shl 14).toUInt()
+ val BUTTON_PS = (1 shl 15).toUInt()
+ val TOUCHPAD_WIDTH: UShort = 1920U
+ val TOUCHPAD_HEIGHT: UShort = 942U
+ }
+
+ infix fun or(o: ControllerState) = ControllerState(
+ buttons = buttons or o.buttons,
+ l2State = maxOf(l2State, o.l2State),
+ r2State = maxOf(r2State, o.r2State),
+ leftX = maxAbs(leftX, o.leftX),
+ leftY = maxAbs(leftY, o.leftY),
+ rightX = maxAbs(rightX, o.rightX),
+ rightY = maxAbs(rightY, o.rightY),
+ touches = touches.zip(o.touches) { a, b -> if(a.id >= 0) a else b }.toTypedArray(),
+ gyroX = gyroX,
+ gyroY = gyroY,
+ gyroZ = gyroZ,
+ accelX = accelX,
+ accelY = accelY,
+ accelZ = accelZ,
+ orientX = orientX,
+ orientY = orientY,
+ orientZ = orientZ,
+ orientW = orientW
+ )
+
+ override fun equals(other: Any?): Boolean
+ {
+ if(this === other) return true
+ if(javaClass != other?.javaClass) return false
+
+ other as ControllerState
+
+ if(buttons != other.buttons) return false
+ if(l2State != other.l2State) return false
+ if(r2State != other.r2State) return false
+ if(leftX != other.leftX) return false
+ if(leftY != other.leftY) return false
+ if(rightX != other.rightX) return false
+ if(rightY != other.rightY) return false
+ if(touchIdNext != other.touchIdNext) return false
+ if(!touches.contentEquals(other.touches)) return false
+ if(gyroX != other.gyroX) return false
+ if(gyroY != other.gyroY) return false
+ if(gyroZ != other.gyroZ) return false
+ if(accelX != other.accelX) return false
+ if(accelY != other.accelY) return false
+ if(accelZ != other.accelZ) return false
+ if(orientX != other.orientX) return false
+ if(orientY != other.orientY) return false
+ if(orientZ != other.orientZ) return false
+ if(orientW != other.orientW) return false
+
+ return true
+ }
+
+ override fun hashCode(): Int
+ {
+ var result = buttons.hashCode()
+ result = 31 * result + l2State.hashCode()
+ result = 31 * result + r2State.hashCode()
+ result = 31 * result + leftX
+ result = 31 * result + leftY
+ result = 31 * result + rightX
+ result = 31 * result + rightY
+ result = 31 * result + touchIdNext.hashCode()
+ result = 31 * result + touches.contentHashCode()
+ result = 31 * result + gyroX.hashCode()
+ result = 31 * result + gyroY.hashCode()
+ result = 31 * result + gyroZ.hashCode()
+ result = 31 * result + accelX.hashCode()
+ result = 31 * result + accelY.hashCode()
+ result = 31 * result + accelZ.hashCode()
+ result = 31 * result + orientX.hashCode()
+ result = 31 * result + orientY.hashCode()
+ result = 31 * result + orientZ.hashCode()
+ result = 31 * result + orientW.hashCode()
+ return result
+ }
+
+ fun startTouch(x: UShort, y: UShort): UByte? =
+ touches
+ .find { it.id < 0 }
+ ?.also {
+ it.id = touchIdNext.toByte()
+ it.x = x
+ it.y = y
+ touchIdNext = ((touchIdNext + 1U) and 0x7fU).toUByte()
+ }?.id?.toUByte()
+
+ fun stopTouch(id: UByte)
+ {
+ touches.find {
+ it.id >= 0 && it.id == id.toByte()
+ }?.let {
+ it.id = -1
+ }
+ }
+
+ fun setTouchPos(id: UByte, x: UShort, y: UShort): Boolean
+ = touches.find {
+ it.id >= 0 && it.id == id.toByte()
+ }?.let {
+ val r = it.x != x || it.y != y
+ it.x = x
+ it.y = y
+ r
+ } ?: false
+}
+
+class QuitReason(val value: Int)
+{
+ override fun toString() = ChiakiNative.quitReasonToString(value)
+
+ val isError = ChiakiNative.quitReasonIsError(value)
+}
+
+sealed class Event
+object ConnectedEvent: Event()
+data class LoginPinRequestEvent(val pinIncorrect: Boolean): Event()
+data class QuitEvent(val reason: QuitReason, val reasonString: String?): Event()
+data class RumbleEvent(val left: UByte, val right: UByte): Event()
+
+class CreateError(val errorCode: ErrorCode): Exception("Failed to create a native object: $errorCode")
+
+class Session(connectInfo: ConnectInfo, logFile: String?, logVerbose: Boolean)
+{
+ interface EventCallback
+ {
+ fun sessionEvent(event: Event)
+ }
+
+ private var nativePtr: Long
+ var eventCallback: ((event: Event) -> Unit)? = null
+
+ init
+ {
+ val result = ChiakiNative.CreateResult(0, 0)
+ ChiakiNative.sessionCreate(result, connectInfo, logFile, logVerbose, this)
+ val errorCode = ErrorCode(result.errorCode)
+ if(!errorCode.isSuccess)
+ throw CreateError(errorCode)
+ nativePtr = result.ptr
+ }
+
+ fun start() = ErrorCode(ChiakiNative.sessionStart(nativePtr))
+ fun stop() = ErrorCode(ChiakiNative.sessionStop(nativePtr))
+
+ fun dispose()
+ {
+ if(nativePtr == 0L)
+ return
+ ChiakiNative.sessionJoin(nativePtr)
+ ChiakiNative.sessionFree(nativePtr)
+ nativePtr = 0L
+ }
+
+ private fun event(event: Event)
+ {
+ eventCallback?.let { it(event) }
+ }
+
+ private fun eventConnected()
+ {
+ event(ConnectedEvent)
+ }
+
+ private fun eventLoginPinRequest(pinIncorrect: Boolean)
+ {
+ event(LoginPinRequestEvent(pinIncorrect))
+ }
+
+ private fun eventQuit(reasonValue: Int, reasonString: String?)
+ {
+ event(QuitEvent(QuitReason(reasonValue), reasonString))
+ }
+
+ private fun eventRumble(left: Int, right: Int)
+ {
+ event(RumbleEvent(left.toUByte(), right.toUByte()))
+ }
+
+ fun setSurface(surface: Surface?)
+ {
+ ChiakiNative.sessionSetSurface(nativePtr, surface)
+ }
+
+ fun setControllerState(controllerState: ControllerState)
+ {
+ ChiakiNative.sessionSetControllerState(nativePtr, controllerState)
+ }
+
+ fun setLoginPin(pin: String)
+ {
+ ChiakiNative.sessionSetLoginPin(nativePtr, pin)
+ }
+}
+
+data class DiscoveryHost(
+ val state: State,
+ val hostRequestPort: UShort,
+ val hostAddr: String?,
+ val systemVersion: String?,
+ val deviceDiscoveryProtocolVersion: String?,
+ val hostName: String?,
+ val hostType: String?,
+ val hostId: String?,
+ val runningAppTitleid: String?,
+ val runningAppName: String?)
+{
+ enum class State
+ {
+ UNKNOWN,
+ READY,
+ STANDBY
+ }
+
+ val isPS5 get() = deviceDiscoveryProtocolVersion == "00030010"
+}
+
+
+data class DiscoveryServiceOptions(
+ val hostsMax: ULong,
+ val hostDropPings: ULong,
+ val pingMs: ULong,
+ val sendAddr: InetSocketAddress
+)
+
+class DiscoveryService(
+ options: DiscoveryServiceOptions,
+ val callback: ((hosts: List) -> Unit)?)
+{
+ companion object
+ {
+ fun wakeup(service: DiscoveryService?, host: String, userCredential: ULong, ps5: Boolean) =
+ ChiakiNative.discoveryServiceWakeup(service?.nativePtr ?: 0, host, userCredential.toLong(), ps5)
+ }
+
+ private var nativePtr: Long
+
+ init
+ {
+ val result = ChiakiNative.CreateResult(0, 0)
+ ChiakiNative.discoveryServiceCreate(result, options, this)
+ val errorCode = ErrorCode(result.errorCode)
+ if(!errorCode.isSuccess)
+ throw CreateError(errorCode)
+ nativePtr = result.ptr
+ }
+
+ fun dispose()
+ {
+ if(nativePtr == 0L)
+ return
+ ChiakiNative.discoveryServiceFree(nativePtr)
+ nativePtr = 0L
+ }
+
+ private fun hostsUpdated(hosts: Array)
+ {
+ val hostsList = hosts.toList()
+ Log.i("Chiaki", "got hosts from native: $hostsList")
+ callback?.let { it(hostsList) }
+ }
+
+}
+
+@Parcelize
+data class RegistInfo(
+ val target: Target,
+ val host: String,
+ val broadcast: Boolean,
+ val psnOnlineId: String?,
+ val psnAccountId: ByteArray?,
+ val pin: Int
+): Parcelable
+{
+ companion object
+ {
+ const val ACCOUNT_ID_SIZE = 8
+ }
+}
+
+data class RegistHost(
+ val target: Target,
+ val apSsid: String,
+ val apBssid: String,
+ val apKey: String,
+ val apName: String,
+ val serverMac: ByteArray,
+ val serverNickname: String,
+ val rpRegistKey: ByteArray,
+ val rpKeyType: UInt,
+ val rpKey: ByteArray
+)
+
+sealed class RegistEvent
+object RegistEventCanceled: RegistEvent()
+object RegistEventFailed: RegistEvent()
+class RegistEventSuccess(val host: RegistHost): RegistEvent()
+
+class Regist(
+ info: RegistInfo,
+ log: ChiakiLog,
+ val callback: (RegistEvent) -> Unit
+)
+{
+ private var nativePtr: Long
+
+ init
+ {
+ val result = ChiakiNative.CreateResult(0, 0)
+ ChiakiNative.registStart(result, info, log, this)
+ val errorCode = ErrorCode(result.errorCode)
+ if(!errorCode.isSuccess)
+ throw CreateError(errorCode)
+ nativePtr = result.ptr
+ }
+
+ fun stop()
+ {
+ ChiakiNative.registStop(nativePtr)
+ }
+
+ fun dispose()
+ {
+ if(nativePtr == 0L)
+ return
+ ChiakiNative.registFree(nativePtr)
+ nativePtr = 0L
+ }
+
+ private fun event(event: RegistEvent)
+ {
+ callback(event)
+ }
+}
diff --git a/android/app/src/main/java/com/metallic/chiaki/main/DisplayHostRecyclerViewAdapter.kt b/android/app/src/main/java/com/metallic/chiaki/main/DisplayHostRecyclerViewAdapter.kt
new file mode 100644
index 0000000..465a2a7
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/main/DisplayHostRecyclerViewAdapter.kt
@@ -0,0 +1,124 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.main
+
+import android.util.Log
+import android.view.LayoutInflater
+import android.view.View
+import android.view.ViewGroup
+import android.view.animation.AnimationUtils
+import android.widget.PopupMenu
+import androidx.core.view.isGone
+import androidx.core.view.isVisible
+import androidx.recyclerview.widget.DiffUtil
+import androidx.recyclerview.widget.RecyclerView
+import com.metallic.chiaki.R
+import com.metallic.chiaki.common.DiscoveredDisplayHost
+import com.metallic.chiaki.common.DisplayHost
+import com.metallic.chiaki.common.ManualDisplayHost
+import com.metallic.chiaki.common.ext.inflate
+import com.metallic.chiaki.databinding.ItemDisplayHostBinding
+import com.metallic.chiaki.lib.DiscoveryHost
+
+class DisplayHostDiffCallback(val old: List, val new: List): DiffUtil.Callback()
+{
+ override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int) = (old[oldItemPosition] == new[newItemPosition])
+ override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int) = (old[oldItemPosition] == new[newItemPosition])
+ override fun getOldListSize() = old.size
+ override fun getNewListSize() = new.size
+}
+
+class DisplayHostRecyclerViewAdapter(
+ val clickCallback: (DisplayHost) -> Unit,
+ val wakeupCallback: (DisplayHost) -> Unit,
+ val editCallback: (DisplayHost) -> Unit,
+ val deleteCallback: (DisplayHost) -> Unit
+): RecyclerView.Adapter()
+{
+ var hosts: List = listOf()
+ set(value)
+ {
+ val diff = DiffUtil.calculateDiff(DisplayHostDiffCallback(field, value))
+ field = value
+ diff.dispatchUpdatesTo(this)
+ }
+
+ class ViewHolder(val binding: ItemDisplayHostBinding): RecyclerView.ViewHolder(binding.root)
+
+ override fun onCreateViewHolder(parent: ViewGroup, viewType: Int)
+ = ViewHolder(ItemDisplayHostBinding.inflate(LayoutInflater.from(parent.context), parent, false))
+
+ override fun getItemCount() = hosts.count()
+
+ override fun onBindViewHolder(holder: ViewHolder, position: Int)
+ {
+ val context = holder.itemView.context
+ val host = hosts[position]
+ holder.binding.also {
+ it.nameTextView.text = host.name
+ it.hostTextView.text = context.getString(R.string.display_host_host, host.host)
+ val id = host.id
+ it.idTextView.text =
+ if(id != null)
+ context.getString(
+ if(host.isRegistered)
+ R.string.display_host_id_registered
+ else
+ R.string.display_host_id_unregistered,
+ id)
+ else
+ ""
+ it.bottomInfoTextView.text = (host as? DiscoveredDisplayHost)?.discoveredHost?.let { discoveredHost ->
+ if(discoveredHost.runningAppName != null || discoveredHost.runningAppTitleid != null)
+ context.getString(R.string.display_host_app_title_id, discoveredHost.runningAppName ?: "", discoveredHost.runningAppTitleid ?: "")
+ else
+ ""
+ } ?: ""
+ it.discoveredIndicatorLayout.visibility = if(host is DiscoveredDisplayHost) View.VISIBLE else View.GONE
+ it.stateIndicatorImageView.setImageResource(
+ when
+ {
+ host is DiscoveredDisplayHost -> when(host.discoveredHost.state)
+ {
+ DiscoveryHost.State.STANDBY -> if(host.isPS5) R.drawable.ic_console_ps5_standby else R.drawable.ic_console_standby
+ DiscoveryHost.State.READY -> if(host.isPS5) R.drawable.ic_console_ps5_ready else R.drawable.ic_console_ready
+ else -> if(host.isPS5) R.drawable.ic_console_ps5 else R.drawable.ic_console
+ }
+ host.isPS5 -> R.drawable.ic_console_ps5
+ else -> R.drawable.ic_console
+ }
+ )
+ it.root.setOnClickListener { clickCallback(host) }
+
+ val canWakeup = host.registeredHost != null
+ val canEditDelete = host is ManualDisplayHost
+ if(canWakeup || canEditDelete)
+ {
+ it.menuButton.isVisible = true
+ it.menuButton.setOnClickListener { _ ->
+ val menu = PopupMenu(context, it.menuButton)
+ menu.menuInflater.inflate(R.menu.display_host, menu.menu)
+ menu.menu.findItem(R.id.action_wakeup).isVisible = canWakeup
+ menu.menu.findItem(R.id.action_edit).isVisible = canEditDelete
+ menu.menu.findItem(R.id.action_delete).isVisible = canEditDelete
+ menu.setOnMenuItemClickListener { menuItem ->
+ when(menuItem.itemId)
+ {
+ R.id.action_wakeup -> wakeupCallback(host)
+ R.id.action_edit -> editCallback(host)
+ R.id.action_delete -> deleteCallback(host)
+ else -> return@setOnMenuItemClickListener false
+ }
+ true
+ }
+ menu.show()
+ }
+ }
+ else
+ {
+ it.menuButton.isGone = true
+ it.menuButton.setOnClickListener(null)
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/main/FloatingActionButtonBackgroundBehavior.kt b/android/app/src/main/java/com/metallic/chiaki/main/FloatingActionButtonBackgroundBehavior.kt
new file mode 100644
index 0000000..3babfa9
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/main/FloatingActionButtonBackgroundBehavior.kt
@@ -0,0 +1,87 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.main
+
+import android.animation.Animator
+import android.animation.AnimatorSet
+import android.animation.ObjectAnimator
+import android.animation.PropertyValuesHolder
+import android.content.Context
+import android.content.res.Resources
+import android.util.AttributeSet
+import android.view.View
+import android.view.ViewGroup
+import android.view.animation.AccelerateInterpolator
+import android.view.animation.DecelerateInterpolator
+import androidx.coordinatorlayout.widget.CoordinatorLayout
+import androidx.core.animation.addListener
+import androidx.core.view.children
+import androidx.core.view.isGone
+import androidx.core.view.isInvisible
+import androidx.core.view.isVisible
+import com.google.android.material.floatingactionbutton.FloatingActionButton
+import com.google.android.material.transformation.ExpandableTransformationBehavior
+import com.metallic.chiaki.R
+
+class FloatingActionButtonBackgroundBehavior @JvmOverloads constructor(context: Context? = null, attrs: AttributeSet? = null) : ExpandableTransformationBehavior(context, attrs)
+{
+ companion object
+ {
+ private const val DURATION = 150L
+ }
+
+ override fun layoutDependsOn(parent: CoordinatorLayout, child: View, dependency: View)
+ = dependency is FloatingActionButton
+
+ override fun onCreateExpandedStateChangeAnimation(dependency: View, child: View, expanded: Boolean, isAnimating: Boolean): AnimatorSet
+ = AnimatorSet().also {
+ it.playTogether(listOf(
+ if(expanded)
+ createExpandAnimation(child, isAnimating)
+ else
+ createCollapseAnimation(child)
+
+ ))
+ it.addListener(
+ onStart = {
+ if(expanded)
+ child.isVisible = true
+ },
+ onEnd = {
+ if(!expanded)
+ child.isGone = true
+ }
+ )
+ }
+
+ private fun createExpandAnimation(child: View, currentlyAnimating: Boolean): Animator
+ {
+ if(!currentlyAnimating)
+ child.alpha = 0f
+
+ val animator = ObjectAnimator.ofPropertyValuesHolder(
+ child,
+ PropertyValuesHolder.ofFloat(View.ALPHA, 1f)
+ ).apply {
+ duration = DURATION
+ }
+
+ return AnimatorSet().apply {
+ playTogether(listOf(animator))
+ }
+ }
+
+ private fun createCollapseAnimation(child: View): Animator
+ {
+ val animator = ObjectAnimator.ofPropertyValuesHolder(
+ child,
+ PropertyValuesHolder.ofFloat(View.ALPHA, 0f)
+ ).apply {
+ duration = DURATION
+ }
+
+ return AnimatorSet().apply {
+ playTogether(listOf(animator))
+ }
+ }
+}
diff --git a/android/app/src/main/java/com/metallic/chiaki/main/FloatingActionButtonSpeedDialBehavior.kt b/android/app/src/main/java/com/metallic/chiaki/main/FloatingActionButtonSpeedDialBehavior.kt
new file mode 100644
index 0000000..59a2003
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/main/FloatingActionButtonSpeedDialBehavior.kt
@@ -0,0 +1,115 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.main
+
+import android.animation.Animator
+import android.animation.AnimatorSet
+import android.animation.ObjectAnimator
+import android.animation.PropertyValuesHolder
+import android.content.Context
+import android.content.res.Resources
+import android.util.AttributeSet
+import android.view.View
+import android.view.ViewGroup
+import android.view.animation.AccelerateInterpolator
+import android.view.animation.DecelerateInterpolator
+import androidx.coordinatorlayout.widget.CoordinatorLayout
+import androidx.core.animation.addListener
+import androidx.core.view.children
+import androidx.core.view.isInvisible
+import androidx.core.view.isVisible
+import com.google.android.material.floatingactionbutton.FloatingActionButton
+import com.google.android.material.transformation.ExpandableTransformationBehavior
+import com.metallic.chiaki.R
+
+// see https://github.com/lcdsmao/ExpandableFABExample
+
+class FloatingActionButtonSpeedDialBehavior @JvmOverloads constructor(context: Context? = null, attrs: AttributeSet? = null) : ExpandableTransformationBehavior(context, attrs)
+{
+ companion object
+ {
+ private const val DELAY = 30L
+ private const val DURATION = 150L
+ }
+
+ override fun layoutDependsOn(parent: CoordinatorLayout, child: View, dependency: View)
+ = dependency is FloatingActionButton && child is ViewGroup
+
+ override fun onCreateExpandedStateChangeAnimation(dependency: View, child: View, expanded: Boolean, isAnimating: Boolean): AnimatorSet
+ = if(child !is ViewGroup)
+ AnimatorSet()
+ else
+ AnimatorSet().also {
+ it.playTogether(listOf(
+ if(expanded)
+ createExpandAnimation(child, isAnimating)
+ else
+ createCollapseAnimation(child)
+
+ ))
+ it.addListener(
+ onStart = {
+ if(expanded)
+ child.isVisible = true
+ },
+ onEnd = {
+ if(!expanded)
+ child.isInvisible = true
+ }
+ )
+ }
+
+ private fun offset(resources: Resources) = resources.getDimension(R.dimen.floating_action_button_speed_dial_anim_offset)
+
+ private fun createExpandAnimation(child: ViewGroup, currentlyAnimating: Boolean): Animator
+ {
+ if(!currentlyAnimating)
+ {
+ child.children.forEach {
+ it.alpha = 0f
+ it.translationY = this.offset(child.resources)
+ }
+ }
+
+ val translationYHolder = PropertyValuesHolder.ofFloat(View.TRANSLATION_Y, 0f)
+ val alphaHolder = PropertyValuesHolder.ofFloat(View.ALPHA, 1f)
+
+ val animators = child.children.mapIndexed { index, view ->
+ ObjectAnimator.ofPropertyValuesHolder(
+ view,
+ translationYHolder,
+ alphaHolder
+ ).apply {
+ duration = DURATION
+ startDelay = (child.childCount - index - 1) * DELAY
+ interpolator = DecelerateInterpolator()
+ }
+ }.toList()
+
+ return AnimatorSet().apply {
+ playTogether(animators)
+ }
+ }
+
+ private fun createCollapseAnimation(child: ViewGroup): Animator
+ {
+ val translationYHolder = PropertyValuesHolder.ofFloat(View.TRANSLATION_Y, this.offset(child.resources))
+ val alphaHolder = PropertyValuesHolder.ofFloat(View.ALPHA, 0f)
+
+ val animators = child.children.mapIndexed { index, view ->
+ ObjectAnimator.ofPropertyValuesHolder(
+ view,
+ translationYHolder,
+ alphaHolder
+ ).apply {
+ duration = DURATION
+ startDelay = index * DELAY
+ interpolator = AccelerateInterpolator()
+ }
+ }.toList()
+
+ return AnimatorSet().apply {
+ playTogether(animators)
+ }
+ }
+}
diff --git a/android/app/src/main/java/com/metallic/chiaki/main/MainActivity.kt b/android/app/src/main/java/com/metallic/chiaki/main/MainActivity.kt
new file mode 100644
index 0000000..5bf1f49
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/main/MainActivity.kt
@@ -0,0 +1,240 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.main
+
+import android.app.ActivityOptions
+import android.content.Intent
+import android.os.Bundle
+import android.view.Menu
+import android.view.MenuItem
+import android.view.View
+import androidx.appcompat.app.AppCompatActivity
+import androidx.lifecycle.Observer
+import androidx.lifecycle.ViewModelProvider
+import androidx.recyclerview.widget.LinearLayoutManager
+import com.google.android.material.dialog.MaterialAlertDialogBuilder
+import com.metallic.chiaki.R
+import com.metallic.chiaki.common.*
+import com.metallic.chiaki.common.ext.putRevealExtra
+import com.metallic.chiaki.common.ext.viewModelFactory
+import com.metallic.chiaki.databinding.ActivityMainBinding
+import com.metallic.chiaki.lib.ConnectInfo
+import com.metallic.chiaki.lib.DiscoveryHost
+import com.metallic.chiaki.manualconsole.EditManualConsoleActivity
+import com.metallic.chiaki.regist.RegistActivity
+import com.metallic.chiaki.settings.SettingsActivity
+import com.metallic.chiaki.stream.StreamActivity
+
+class MainActivity : AppCompatActivity()
+{
+ private lateinit var viewModel: MainViewModel
+
+ private lateinit var binding: ActivityMainBinding
+ private var discoveryMenuItem: MenuItem? = null
+
+ override fun onCreate(savedInstanceState: Bundle?)
+ {
+ super.onCreate(savedInstanceState)
+ binding = ActivityMainBinding.inflate(layoutInflater)
+ setContentView(binding.root)
+
+ title = ""
+ setSupportActionBar(binding.toolbar)
+
+ binding.floatingActionButton.setOnClickListener {
+ expandFloatingActionButton(!binding.floatingActionButton.isExpanded)
+ }
+ binding.floatingActionButtonDialBackground.setOnClickListener {
+ expandFloatingActionButton(false)
+ }
+
+ binding.addManualButton.setOnClickListener { addManualConsole() }
+ binding.addManualLabelButton.setOnClickListener { addManualConsole() }
+
+ binding.registerButton.setOnClickListener { showRegistration() }
+ binding.registerLabelButton.setOnClickListener { showRegistration() }
+
+ viewModel = ViewModelProvider(this, viewModelFactory { MainViewModel(getDatabase(this), Preferences(this)) })
+ .get(MainViewModel::class.java)
+
+ val recyclerViewAdapter = DisplayHostRecyclerViewAdapter(this::hostTriggered, this::wakeupHost, this::editHost, this::deleteHost)
+ binding.hostsRecyclerView.adapter = recyclerViewAdapter
+ binding.hostsRecyclerView.layoutManager = LinearLayoutManager(this)
+ viewModel.displayHosts.observe(this, Observer {
+ val top = binding.hostsRecyclerView.computeVerticalScrollOffset() == 0
+ recyclerViewAdapter.hosts = it
+ if(top)
+ binding.hostsRecyclerView.scrollToPosition(0)
+ updateEmptyInfo()
+ })
+
+ viewModel.discoveryActive.observe(this, Observer { active ->
+ discoveryMenuItem?.let { updateDiscoveryMenuItem(it, active) }
+ updateEmptyInfo()
+ })
+ }
+
+ private fun updateEmptyInfo()
+ {
+ if(viewModel.displayHosts.value?.isEmpty() ?: true)
+ {
+ binding.emptyInfoLayout.visibility = View.VISIBLE
+ val discoveryActive = viewModel.discoveryActive.value ?: false
+ binding.emptyInfoImageView.setImageResource(if(discoveryActive) R.drawable.ic_discover_on else R.drawable.ic_discover_off)
+ binding.emptyInfoTextView.setText(if(discoveryActive) R.string.display_hosts_empty_discovery_on_info else R.string.display_hosts_empty_discovery_off_info)
+ }
+ else
+ binding.emptyInfoLayout.visibility = View.GONE
+ }
+
+ private fun expandFloatingActionButton(expand: Boolean)
+ {
+ binding.floatingActionButton.isExpanded = expand
+ binding.floatingActionButton.isActivated = binding.floatingActionButton.isExpanded
+ }
+
+ override fun onStart()
+ {
+ super.onStart()
+ viewModel.discoveryManager.resume()
+ }
+
+ override fun onStop()
+ {
+ super.onStop()
+ viewModel.discoveryManager.pause()
+ }
+
+ override fun onBackPressed()
+ {
+ if(binding.floatingActionButton.isExpanded)
+ {
+ expandFloatingActionButton(false)
+ return
+ }
+ super.onBackPressed()
+ }
+
+ override fun onCreateOptionsMenu(menu: Menu): Boolean
+ {
+ menuInflater.inflate(R.menu.main, menu)
+ val discoveryItem = menu.findItem(R.id.action_discover)
+ discoveryMenuItem = discoveryItem
+ val discoveryActive = viewModel.discoveryActive.value ?: false
+ updateDiscoveryMenuItem(discoveryItem, discoveryActive)
+ return true
+ }
+
+ private fun updateDiscoveryMenuItem(item: MenuItem, active: Boolean)
+ {
+ item.isChecked = active
+ item.setIcon(if(active) R.drawable.ic_discover_on else R.drawable.ic_discover_off)
+ }
+
+ override fun onOptionsItemSelected(item: MenuItem): Boolean = when(item.itemId)
+ {
+ R.id.action_discover ->
+ {
+ viewModel.discoveryManager.active = !(viewModel.discoveryActive.value ?: false)
+ true
+ }
+
+ R.id.action_settings ->
+ {
+ Intent(this, SettingsActivity::class.java).also {
+ startActivity(it)
+ }
+ true
+ }
+
+ else -> super.onOptionsItemSelected(item)
+ }
+
+ private fun addManualConsole()
+ {
+ Intent(this, EditManualConsoleActivity::class.java).also {
+ it.putRevealExtra(binding.addManualButton, binding.rootLayout)
+ startActivity(it, ActivityOptions.makeSceneTransitionAnimation(this).toBundle())
+ }
+ }
+
+ private fun showRegistration()
+ {
+ Intent(this, RegistActivity::class.java).also {
+ it.putRevealExtra(binding.registerButton, binding.rootLayout)
+ startActivity(it, ActivityOptions.makeSceneTransitionAnimation(this).toBundle())
+ }
+ }
+
+ private fun hostTriggered(host: DisplayHost)
+ {
+ val registeredHost = host.registeredHost
+ if(registeredHost != null)
+ {
+ fun connect() {
+ val connectInfo = ConnectInfo(host.isPS5, host.host, registeredHost.rpRegistKey, registeredHost.rpKey, Preferences(this).videoProfile)
+ Intent(this, StreamActivity::class.java).let {
+ it.putExtra(StreamActivity.EXTRA_CONNECT_INFO, connectInfo)
+ startActivity(it)
+ }
+ }
+
+ if(host is DiscoveredDisplayHost && host.discoveredHost.state == DiscoveryHost.State.STANDBY)
+ {
+ MaterialAlertDialogBuilder(this)
+ .setMessage(R.string.alert_message_standby_wakeup)
+ .setPositiveButton(R.string.action_wakeup) { _, _ ->
+ wakeupHost(host)
+ }
+ .setNeutralButton(R.string.action_connect_immediately) { _, _ ->
+ connect()
+ }
+ .setNegativeButton(R.string.action_connect_cancel_connect) { _, _ -> }
+ .create()
+ .show()
+ }
+ else
+ connect()
+ }
+ else
+ {
+ Intent(this, RegistActivity::class.java).let {
+ it.putExtra(RegistActivity.EXTRA_HOST, host.host)
+ it.putExtra(RegistActivity.EXTRA_BROADCAST, false)
+ if(host is ManualDisplayHost)
+ it.putExtra(RegistActivity.EXTRA_ASSIGN_MANUAL_HOST_ID, host.manualHost.id)
+ startActivity(it)
+ }
+ }
+ }
+
+ private fun wakeupHost(host: DisplayHost)
+ {
+ val registeredHost = host.registeredHost ?: return
+ viewModel.discoveryManager.sendWakeup(host.host, registeredHost.rpRegistKey, registeredHost.target.isPS5)
+ }
+
+ private fun editHost(host: DisplayHost)
+ {
+ if(host !is ManualDisplayHost)
+ return
+ Intent(this, EditManualConsoleActivity::class.java).also {
+ it.putExtra(EditManualConsoleActivity.EXTRA_MANUAL_HOST_ID, host.manualHost.id)
+ startActivity(it)
+ }
+ }
+
+ private fun deleteHost(host: DisplayHost)
+ {
+ if(host !is ManualDisplayHost)
+ return
+ MaterialAlertDialogBuilder(this)
+ .setMessage(getString(R.string.alert_message_delete_manual_host, host.manualHost.host))
+ .setPositiveButton(R.string.action_delete) { _, _ ->
+ viewModel.deleteManualHost(host.manualHost)
+ }
+ .setNegativeButton(R.string.action_keep) { _, _ -> }
+ .create()
+ .show()
+ }
+}
diff --git a/android/app/src/main/java/com/metallic/chiaki/main/MainViewModel.kt b/android/app/src/main/java/com/metallic/chiaki/main/MainViewModel.kt
new file mode 100644
index 0000000..b4fe9c8
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/main/MainViewModel.kt
@@ -0,0 +1,66 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.main
+
+import androidx.lifecycle.ViewModel
+import com.metallic.chiaki.common.*
+import com.metallic.chiaki.common.ext.toLiveData
+import com.metallic.chiaki.discovery.DiscoveryManager
+import com.metallic.chiaki.discovery.serverMac
+import io.reactivex.android.schedulers.AndroidSchedulers
+import io.reactivex.disposables.CompositeDisposable
+import io.reactivex.rxkotlin.Observables
+import io.reactivex.rxkotlin.addTo
+import io.reactivex.schedulers.Schedulers
+
+class MainViewModel(val database: AppDatabase, val preferences: Preferences): ViewModel()
+{
+ private val disposable = CompositeDisposable()
+
+ val discoveryManager = DiscoveryManager().also {
+ it.active = preferences.discoveryEnabled
+ it.discoveryActive
+ .observeOn(AndroidSchedulers.mainThread())
+ .subscribe { preferences.discoveryEnabled = it }
+ .addTo(disposable)
+ }
+
+ val displayHosts by lazy {
+ Observables.combineLatest(
+ database.manualHostDao().getAll().toObservable(),
+ database.registeredHostDao().getAll().toObservable(),
+ discoveryManager.discoveredHosts)
+ { manualHosts, registeredHosts, discoveredHosts ->
+ val macRegisteredHosts = registeredHosts.associateBy { it.serverMac }
+ val idRegisteredHosts = registeredHosts.associateBy { it.id }
+ discoveredHosts.map {
+ DiscoveredDisplayHost(it.serverMac?.let { mac -> macRegisteredHosts[mac] }, it)
+ } +
+ manualHosts.map {
+ ManualDisplayHost(it.registeredHost?.let { id -> idRegisteredHosts[id] }, it)
+ }
+ }
+ .toLiveData()
+ }
+
+ val discoveryActive by lazy {
+ discoveryManager.discoveryActive.toLiveData()
+ }
+
+ fun deleteManualHost(manualHost: ManualHost)
+ {
+ database.manualHostDao()
+ .delete(manualHost)
+ .onErrorComplete()
+ .subscribeOn(Schedulers.io())
+ .subscribe()
+ .addTo(disposable)
+ }
+
+ override fun onCleared()
+ {
+ super.onCleared()
+ disposable.dispose()
+ discoveryManager.dispose()
+ }
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/manualconsole/EditManualConsoleActivity.kt b/android/app/src/main/java/com/metallic/chiaki/manualconsole/EditManualConsoleActivity.kt
new file mode 100644
index 0000000..bb64b87
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/manualconsole/EditManualConsoleActivity.kt
@@ -0,0 +1,104 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.manualconsole
+
+import android.content.Intent
+import android.os.Bundle
+import android.view.View
+import android.view.Window
+import android.widget.AdapterView
+import android.widget.ArrayAdapter
+import androidx.appcompat.app.AppCompatActivity
+import androidx.lifecycle.Observer
+import androidx.lifecycle.ViewModelProvider
+import com.metallic.chiaki.R
+import com.metallic.chiaki.common.RegisteredHost
+import com.metallic.chiaki.common.ext.RevealActivity
+import com.metallic.chiaki.common.ext.viewModelFactory
+import com.metallic.chiaki.common.getDatabase
+import com.metallic.chiaki.databinding.ActivityEditManualBinding
+import io.reactivex.android.schedulers.AndroidSchedulers
+import io.reactivex.disposables.CompositeDisposable
+import io.reactivex.rxkotlin.addTo
+
+class EditManualConsoleActivity: AppCompatActivity(), RevealActivity
+{
+ companion object
+ {
+ const val EXTRA_MANUAL_HOST_ID = "manual_host_id"
+ }
+
+ private lateinit var viewModel: EditManualConsoleViewModel
+ private lateinit var binding: ActivityEditManualBinding
+
+ override val revealIntent: Intent get() = intent
+ override val revealRootLayout: View get() = binding.rootLayout
+ override val revealWindow: Window get() = window
+
+ private val disposable = CompositeDisposable()
+
+ override fun onCreate(savedInstanceState: Bundle?)
+ {
+ super.onCreate(savedInstanceState)
+ binding = ActivityEditManualBinding.inflate(layoutInflater)
+ setContentView(binding.root)
+ handleReveal()
+
+ viewModel = ViewModelProvider(this, viewModelFactory {
+ EditManualConsoleViewModel(getDatabase(this),
+ if(intent.hasExtra(EXTRA_MANUAL_HOST_ID))
+ intent.getLongExtra(EXTRA_MANUAL_HOST_ID, 0)
+ else
+ null)
+ })
+ .get(EditManualConsoleViewModel::class.java)
+
+ viewModel.existingHost?.observe(this, Observer {
+ binding.hostEditText.setText(it.host)
+ })
+
+ viewModel.selectedRegisteredHost.observe(this, Observer {
+ binding.registeredHostTextView.setText(titleForRegisteredHost(it))
+ })
+
+ viewModel.registeredHosts.observe(this, Observer { hosts ->
+ binding.registeredHostTextView.setAdapter(ArrayAdapter(this, R.layout.dropdown_menu_popup_item,
+ hosts.map { titleForRegisteredHost(it) }))
+ binding.registeredHostTextView.onItemClickListener = object: AdapterView.OnItemClickListener {
+ override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long)
+ {
+ if(position >= hosts.size)
+ return
+ val host = hosts[position]
+ viewModel.selectedRegisteredHost.value = host
+ }
+ }
+ })
+
+ binding.saveButton.setOnClickListener { saveHost() }
+ }
+
+ private fun titleForRegisteredHost(registeredHost: RegisteredHost?) =
+ if(registeredHost == null)
+ getString(R.string.add_manual_regist_on_connect)
+ else
+ "${registeredHost.serverNickname ?: ""} (${registeredHost.serverMac})"
+
+ private fun saveHost()
+ {
+ val host = binding.hostEditText.text.toString().trim()
+ if(host.isEmpty())
+ {
+ binding.hostEditText.error = getString(R.string.entered_host_invalid)
+ return
+ }
+
+ binding.saveButton.isEnabled = false
+ viewModel.saveHost(host)
+ .observeOn(AndroidSchedulers.mainThread())
+ .subscribe {
+ finish()
+ }
+ .addTo(disposable)
+ }
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/manualconsole/EditManualConsoleViewModel.kt b/android/app/src/main/java/com/metallic/chiaki/manualconsole/EditManualConsoleViewModel.kt
new file mode 100644
index 0000000..160e4a3
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/manualconsole/EditManualConsoleViewModel.kt
@@ -0,0 +1,60 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.manualconsole
+
+import android.util.Log
+import androidx.lifecycle.LiveData
+import androidx.lifecycle.MutableLiveData
+import androidx.lifecycle.ViewModel
+import com.metallic.chiaki.common.AppDatabase
+import com.metallic.chiaki.common.ManualHost
+import com.metallic.chiaki.common.RegisteredHost
+import com.metallic.chiaki.common.ext.toLiveData
+import io.reactivex.android.schedulers.AndroidSchedulers
+import io.reactivex.schedulers.Schedulers
+
+class EditManualConsoleViewModel(val database: AppDatabase, manualHostId: Long?): ViewModel()
+{
+ val registeredHosts by lazy {
+ database.registeredHostDao().getAll().observeOn(AndroidSchedulers.mainThread())
+ .doOnNext { hosts ->
+ val selectedHost = selectedRegisteredHost.value
+ if(selectedHost != null)
+ selectedRegisteredHost.value = hosts.firstOrNull { it.id == selectedHost.id }
+ }
+ .map { listOf(null) + it }
+ .toLiveData()
+ }
+
+ val existingHost: LiveData? =
+ if(manualHostId != null)
+ database.manualHostDao()
+ .getByIdWithRegisteredHost(manualHostId)
+ .toFlowable()
+ .doOnError {
+ Log.e("EditManualConsole", "Failed to fetch existing manual host", it)
+ }
+ .subscribeOn(Schedulers.io())
+ .observeOn(AndroidSchedulers.mainThread())
+ .doOnNext { hosts ->
+ selectedRegisteredHost.value = hosts.registeredHost
+ }
+ .map { hosts -> hosts.manualHost }
+ .toLiveData()
+ else
+ null
+
+ var selectedRegisteredHost = MutableLiveData(null)
+
+ fun saveHost(host: String) =
+ database.manualHostDao()
+ .let {
+ val registeredHost = selectedRegisteredHost.value?.id
+ val existingHost = existingHost?.value
+ if(existingHost != null)
+ it.update(ManualHost(id = existingHost.id, host = host, registeredHost = registeredHost))
+ else
+ it.insert(ManualHost(host = host, registeredHost = registeredHost))
+ }
+ .subscribeOn(Schedulers.io())
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/regist/ChiakiRxLog.kt b/android/app/src/main/java/com/metallic/chiaki/regist/ChiakiRxLog.kt
new file mode 100644
index 0000000..f43695f
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/regist/ChiakiRxLog.kt
@@ -0,0 +1,25 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.regist
+
+import com.metallic.chiaki.lib.ChiakiLog
+import io.reactivex.Observable
+import io.reactivex.subjects.BehaviorSubject
+import java.util.concurrent.locks.ReentrantLock
+import kotlin.concurrent.withLock
+
+class ChiakiRxLog(levelMask: Int)
+{
+ private val accSubject: BehaviorSubject = BehaviorSubject.create().also {
+ it.onNext("")
+ }
+ private val accMutex = ReentrantLock()
+ val logText: Observable get() = accSubject
+
+ val log = ChiakiLog(levelMask, callback = { level, text ->
+ accMutex.withLock {
+ val cur = accSubject.value ?: ""
+ accSubject.onNext(cur + (if(cur.isEmpty()) "" else "\n") + ChiakiLog.formatLog(level, text))
+ }
+ })
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/regist/RegistActivity.kt b/android/app/src/main/java/com/metallic/chiaki/regist/RegistActivity.kt
new file mode 100644
index 0000000..9190f9c
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/regist/RegistActivity.kt
@@ -0,0 +1,148 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.regist
+
+import android.content.Intent
+import android.os.Bundle
+import android.util.Base64
+import android.view.View
+import android.view.Window
+import androidx.appcompat.app.AppCompatActivity
+import androidx.lifecycle.Observer
+import androidx.lifecycle.ViewModelProvider
+import com.metallic.chiaki.R
+import com.metallic.chiaki.common.ext.RevealActivity
+import com.metallic.chiaki.databinding.ActivityRegistBinding
+import com.metallic.chiaki.lib.RegistInfo
+import com.metallic.chiaki.lib.Target
+import java.lang.IllegalArgumentException
+
+class RegistActivity: AppCompatActivity(), RevealActivity
+{
+ companion object
+ {
+ const val EXTRA_HOST = "regist_host"
+ const val EXTRA_BROADCAST = "regist_broadcast"
+ const val EXTRA_ASSIGN_MANUAL_HOST_ID = "assign_manual_host_id"
+
+ private const val PIN_LENGTH = 8
+
+ private const val REQUEST_REGIST = 1
+ }
+
+ private lateinit var viewModel: RegistViewModel
+ private lateinit var binding: ActivityRegistBinding
+
+ override val revealWindow: Window get() = window
+ override val revealIntent: Intent get() = intent
+ override val revealRootLayout: View get() = binding.rootLayout
+
+ override fun onCreate(savedInstanceState: Bundle?)
+ {
+ super.onCreate(savedInstanceState)
+ binding = ActivityRegistBinding.inflate(layoutInflater)
+ setContentView(binding.root)
+ handleReveal()
+
+ viewModel = ViewModelProvider(this).get(RegistViewModel::class.java)
+
+ binding.hostEditText.setText(intent.getStringExtra(EXTRA_HOST) ?: "255.255.255.255")
+ binding.broadcastCheckBox.isChecked = intent.getBooleanExtra(EXTRA_BROADCAST, true)
+
+ binding.registButton.setOnClickListener { doRegist() }
+
+ binding.ps4VersionRadioGroup.check(when(viewModel.ps4Version.value ?: RegistViewModel.ConsoleVersion.PS5) {
+ RegistViewModel.ConsoleVersion.PS5 -> R.id.ps5RadioButton
+ RegistViewModel.ConsoleVersion.PS4_GE_8 -> R.id.ps4VersionGE8RadioButton
+ RegistViewModel.ConsoleVersion.PS4_GE_7 -> R.id.ps4VersionGE7RadioButton
+ RegistViewModel.ConsoleVersion.PS4_LT_7 -> R.id.ps4VersionLT7RadioButton
+ })
+
+ binding.ps4VersionRadioGroup.setOnCheckedChangeListener { _, checkedId ->
+ viewModel.ps4Version.value = when(checkedId)
+ {
+ R.id.ps5RadioButton -> RegistViewModel.ConsoleVersion.PS5
+ R.id.ps4VersionGE8RadioButton -> RegistViewModel.ConsoleVersion.PS4_GE_8
+ R.id.ps4VersionGE7RadioButton -> RegistViewModel.ConsoleVersion.PS4_GE_7
+ R.id.ps4VersionLT7RadioButton -> RegistViewModel.ConsoleVersion.PS4_LT_7
+ else -> RegistViewModel.ConsoleVersion.PS5
+ }
+ }
+
+ viewModel.ps4Version.observe(this, Observer {
+ binding.psnAccountIdHelpGroup.visibility = if(it == RegistViewModel.ConsoleVersion.PS4_LT_7) View.GONE else View.VISIBLE
+ binding.psnIdTextInputLayout.hint = getString(when(it!!)
+ {
+ RegistViewModel.ConsoleVersion.PS4_LT_7 -> R.string.hint_regist_psn_online_id
+ else -> R.string.hint_regist_psn_account_id
+ })
+ binding.pinHelpBeforeTextView.setText(if(it.isPS5) R.string.regist_pin_instructions_ps5_before else R.string.regist_pin_instructions_ps4_before)
+ binding.pinHelpNavigationTextView.setText(if(it.isPS5) R.string.regist_pin_instructions_ps5_navigation else R.string.regist_pin_instructions_ps4_navigation)
+ })
+ }
+
+ private fun doRegist()
+ {
+ val ps4Version = viewModel.ps4Version.value ?: RegistViewModel.ConsoleVersion.PS5
+
+ val host = binding.hostEditText.text.toString().trim()
+ val hostValid = host.isNotEmpty()
+ val broadcast = binding.broadcastCheckBox.isChecked
+
+ val psnId = binding.psnIdEditText.text.toString().trim()
+ val psnOnlineId: String? = if(ps4Version == RegistViewModel.ConsoleVersion.PS4_LT_7) psnId else null
+ val psnAccountId: ByteArray? =
+ if(ps4Version != RegistViewModel.ConsoleVersion.PS4_LT_7)
+ try { Base64.decode(psnId, Base64.DEFAULT) } catch(e: IllegalArgumentException) { null }
+ else
+ null
+ val psnIdValid = when(ps4Version)
+ {
+ RegistViewModel.ConsoleVersion.PS4_LT_7 -> psnOnlineId?.isNotEmpty() ?: false
+ else -> psnAccountId != null && psnAccountId.size == RegistInfo.ACCOUNT_ID_SIZE
+ }
+
+
+ val pin = binding.pinEditText.text.toString()
+ val pinValid = pin.length == PIN_LENGTH
+
+ binding.hostEditText.error = if(!hostValid) getString(R.string.entered_host_invalid) else null
+ binding.psnIdEditText.error =
+ if(!psnIdValid)
+ getString(when(ps4Version)
+ {
+ RegistViewModel.ConsoleVersion.PS4_LT_7 -> R.string.regist_psn_online_id_invalid
+ else -> R.string.regist_psn_account_id_invalid
+ })
+ else
+ null
+ binding.pinEditText.error = if(!pinValid) getString(R.string.regist_pin_invalid, PIN_LENGTH) else null
+
+ if(!hostValid || !psnIdValid || !pinValid)
+ return
+
+ val target = when(ps4Version)
+ {
+ RegistViewModel.ConsoleVersion.PS5 -> Target.PS5_1
+ RegistViewModel.ConsoleVersion.PS4_GE_8 -> Target.PS4_10
+ RegistViewModel.ConsoleVersion.PS4_GE_7 -> Target.PS4_9
+ RegistViewModel.ConsoleVersion.PS4_LT_7 -> Target.PS4_8
+ }
+
+ val registInfo = RegistInfo(target, host, broadcast, psnOnlineId, psnAccountId, pin.toInt())
+
+ Intent(this, RegistExecuteActivity::class.java).also {
+ it.putExtra(RegistExecuteActivity.EXTRA_REGIST_INFO, registInfo)
+ if(intent.hasExtra(EXTRA_ASSIGN_MANUAL_HOST_ID))
+ it.putExtra(RegistExecuteActivity.EXTRA_ASSIGN_MANUAL_HOST_ID, intent.getLongExtra(EXTRA_ASSIGN_MANUAL_HOST_ID, 0L))
+ startActivityForResult(it, REQUEST_REGIST)
+ }
+ }
+
+ override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?)
+ {
+ super.onActivityResult(requestCode, resultCode, data)
+ if(requestCode == REQUEST_REGIST && resultCode == RESULT_OK)
+ finish()
+ }
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/regist/RegistExecuteActivity.kt b/android/app/src/main/java/com/metallic/chiaki/regist/RegistExecuteActivity.kt
new file mode 100644
index 0000000..16f545a
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/regist/RegistExecuteActivity.kt
@@ -0,0 +1,131 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.regist
+
+import android.app.Activity
+import android.content.Intent
+import android.os.Bundle
+import android.text.method.ScrollingMovementMethod
+import android.view.View
+import androidx.appcompat.app.AlertDialog
+import androidx.appcompat.app.AppCompatActivity
+import androidx.lifecycle.Observer
+import androidx.lifecycle.ViewModelProvider
+import com.google.android.material.dialog.MaterialAlertDialogBuilder
+import com.metallic.chiaki.R
+import com.metallic.chiaki.common.MacAddress
+import com.metallic.chiaki.common.ext.viewModelFactory
+import com.metallic.chiaki.common.getDatabase
+import com.metallic.chiaki.databinding.ActivityRegistExecuteBinding
+import com.metallic.chiaki.lib.RegistInfo
+import kotlin.math.max
+
+class RegistExecuteActivity: AppCompatActivity()
+{
+ companion object
+ {
+ const val EXTRA_REGIST_INFO = "regist_info"
+ const val EXTRA_ASSIGN_MANUAL_HOST_ID = "assign_manual_host_id"
+
+ const val RESULT_FAILED = Activity.RESULT_FIRST_USER
+ }
+
+ private lateinit var viewModel: RegistExecuteViewModel
+ private lateinit var binding: ActivityRegistExecuteBinding
+
+ override fun onCreate(savedInstanceState: Bundle?)
+ {
+ super.onCreate(savedInstanceState)
+ binding = ActivityRegistExecuteBinding.inflate(layoutInflater)
+ setContentView(binding.root)
+
+ viewModel = ViewModelProvider(this, viewModelFactory { RegistExecuteViewModel(getDatabase(this)) })
+ .get(RegistExecuteViewModel::class.java)
+
+ binding.logTextView.setHorizontallyScrolling(true)
+ binding.logTextView.movementMethod = ScrollingMovementMethod()
+ viewModel.logText.observe(this, Observer {
+ val textLayout = binding.logTextView.layout ?: return@Observer
+ val lineCount = textLayout.lineCount
+ if(lineCount < 1)
+ return@Observer
+ binding.logTextView.text = it
+ val scrollY = textLayout.getLineBottom(lineCount - 1) - binding.logTextView.height + binding.logTextView.paddingTop + binding.logTextView.paddingBottom
+ binding.logTextView.scrollTo(0, max(scrollY, 0))
+ })
+
+ viewModel.state.observe(this, Observer {
+ binding.progressBar.visibility = if(it == RegistExecuteViewModel.State.RUNNING) View.VISIBLE else View.GONE
+ when(it)
+ {
+ RegistExecuteViewModel.State.FAILED ->
+ {
+ binding.infoTextView.visibility = View.VISIBLE
+ binding.infoTextView.setText(R.string.regist_info_failed)
+ setResult(RESULT_FAILED)
+ }
+ RegistExecuteViewModel.State.SUCCESSFUL, RegistExecuteViewModel.State.SUCCESSFUL_DUPLICATE ->
+ {
+ binding.infoTextView.visibility = View.VISIBLE
+ binding.infoTextView.setText(R.string.regist_info_success)
+ setResult(RESULT_OK)
+ if(it == RegistExecuteViewModel.State.SUCCESSFUL_DUPLICATE)
+ showDuplicateDialog()
+ }
+ RegistExecuteViewModel.State.STOPPED ->
+ {
+ binding.infoTextView.visibility = View.GONE
+ setResult(Activity.RESULT_CANCELED)
+ }
+ else -> binding.infoTextView.visibility = View.GONE
+ }
+ })
+
+ binding.shareLogButton.setOnClickListener {
+ val log = viewModel.logText.value ?: ""
+ Intent(Intent.ACTION_SEND).also {
+ it.type = "text/plain"
+ it.putExtra(Intent.EXTRA_TEXT, log)
+ startActivity(Intent.createChooser(it, resources.getString(R.string.action_share_log)))
+ }
+ }
+
+ val registInfo = intent.getParcelableExtra(EXTRA_REGIST_INFO)
+ if(registInfo == null)
+ {
+ finish()
+ return
+ }
+ viewModel.start(registInfo,
+ if(intent.hasExtra(EXTRA_ASSIGN_MANUAL_HOST_ID))
+ intent.getLongExtra(EXTRA_ASSIGN_MANUAL_HOST_ID, 0)
+ else
+ null)
+ }
+
+ override fun onStop()
+ {
+ super.onStop()
+ viewModel.stop()
+ }
+
+ private var dialog: AlertDialog? = null
+
+ private fun showDuplicateDialog()
+ {
+ if(dialog != null)
+ return
+
+ val macStr = viewModel.host?.serverMac?.let { MacAddress(it).toString() } ?: ""
+
+ dialog = MaterialAlertDialogBuilder(this)
+ .setMessage(getString(R.string.alert_regist_duplicate, macStr))
+ .setNegativeButton(R.string.action_regist_discard) { _, _ -> }
+ .setPositiveButton(R.string.action_regist_overwrite) { _, _ ->
+ viewModel.saveHost()
+ }
+ .create()
+ .also { it.show() }
+
+ }
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/regist/RegistExecuteViewModel.kt b/android/app/src/main/java/com/metallic/chiaki/regist/RegistExecuteViewModel.kt
new file mode 100644
index 0000000..822fb8e
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/regist/RegistExecuteViewModel.kt
@@ -0,0 +1,126 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.regist
+
+import android.util.Log
+import androidx.lifecycle.LiveData
+import androidx.lifecycle.MutableLiveData
+import androidx.lifecycle.ViewModel
+import com.metallic.chiaki.common.AppDatabase
+import com.metallic.chiaki.common.MacAddress
+import com.metallic.chiaki.common.RegisteredHost
+import com.metallic.chiaki.common.ext.toLiveData
+import com.metallic.chiaki.lib.*
+import io.reactivex.android.schedulers.AndroidSchedulers
+import io.reactivex.disposables.CompositeDisposable
+import io.reactivex.rxkotlin.addTo
+import io.reactivex.schedulers.Schedulers
+
+class RegistExecuteViewModel(val database: AppDatabase): ViewModel()
+{
+ enum class State
+ {
+ IDLE,
+ RUNNING,
+ STOPPED,
+ FAILED,
+ SUCCESSFUL,
+ SUCCESSFUL_DUPLICATE,
+ }
+
+ private val _state = MutableLiveData(State.IDLE)
+ val state: LiveData get() = _state
+
+ private val log = ChiakiRxLog(ChiakiLog.Level.ALL.value/* and ChiakiLog.Level.VERBOSE.value.inv()*/)
+ private var regist: Regist? = null
+
+ val logText: LiveData = log.logText.toLiveData()
+
+ private val disposable = CompositeDisposable()
+
+ var host: RegistHost? = null
+ private set
+
+ private var assignManualHostId: Long? = null
+
+ fun start(info: RegistInfo, assignManualHostId: Long?)
+ {
+ if(regist != null)
+ return
+ try
+ {
+ regist = Regist(info, log.log, this::registEvent)
+ this.assignManualHostId = assignManualHostId
+ _state.value = State.RUNNING
+ }
+ catch(error: CreateError)
+ {
+ log.log.e("Failed to create Regist: ${error.errorCode}")
+ _state.value = State.FAILED
+ }
+ }
+
+ fun stop()
+ {
+ regist?.stop()
+ }
+
+ private fun registEvent(event: RegistEvent)
+ {
+ when(event)
+ {
+ is RegistEventCanceled -> _state.postValue(State.STOPPED)
+ is RegistEventFailed -> _state.postValue(State.FAILED)
+ is RegistEventSuccess -> registSuccess(event.host)
+ }
+ }
+
+ private fun registSuccess(host: RegistHost)
+ {
+ this.host = host
+ database.registeredHostDao().getByMac(MacAddress(host.serverMac))
+ .subscribeOn(Schedulers.io())
+ .observeOn(AndroidSchedulers.mainThread())
+ .doOnSuccess {
+ _state.value = State.SUCCESSFUL_DUPLICATE
+ }
+ .doOnComplete {
+ saveHost()
+ }
+ .subscribe()
+ .addTo(disposable)
+ }
+
+ fun saveHost()
+ {
+ val host = host ?: return
+ val assignManualHostId = assignManualHostId
+ val dao = database.registeredHostDao()
+ val manualHostDao = database.manualHostDao()
+ val registeredHost = RegisteredHost(host)
+ dao.deleteByMac(registeredHost.serverMac)
+ .andThen(dao.insert(registeredHost))
+ .let {
+ if(assignManualHostId != null)
+ it.flatMapCompletable { registeredHostId ->
+ manualHostDao.assignRegisteredHost(assignManualHostId, registeredHostId)
+ }
+ else
+ it.ignoreElement()
+ }
+ .subscribeOn(Schedulers.io())
+ .observeOn(AndroidSchedulers.mainThread())
+ .subscribe {
+ Log.i("RegistExecute", "Registered Host saved in db")
+ _state.value = State.SUCCESSFUL
+ }
+ .addTo(disposable)
+ }
+
+ override fun onCleared()
+ {
+ super.onCleared()
+ regist?.dispose()
+ disposable.dispose()
+ }
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/regist/RegistViewModel.kt b/android/app/src/main/java/com/metallic/chiaki/regist/RegistViewModel.kt
new file mode 100644
index 0000000..ab7253f
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/regist/RegistViewModel.kt
@@ -0,0 +1,20 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.regist
+
+import androidx.lifecycle.MutableLiveData
+import androidx.lifecycle.ViewModel
+
+class RegistViewModel: ViewModel()
+{
+ enum class ConsoleVersion {
+ PS5,
+ PS4_GE_8,
+ PS4_GE_7,
+ PS4_LT_7;
+
+ val isPS5 get() = this == PS5
+ }
+
+ val ps4Version = MutableLiveData(ConsoleVersion.PS5)
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/session/StreamInput.kt b/android/app/src/main/java/com/metallic/chiaki/session/StreamInput.kt
new file mode 100644
index 0000000..822fe76
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/session/StreamInput.kt
@@ -0,0 +1,206 @@
+package com.metallic.chiaki.session
+
+import android.content.Context
+import android.hardware.*
+import android.view.*
+import androidx.lifecycle.Lifecycle
+import androidx.lifecycle.LifecycleObserver
+import androidx.lifecycle.LifecycleOwner
+import androidx.lifecycle.OnLifecycleEvent
+import com.metallic.chiaki.common.Preferences
+import com.metallic.chiaki.lib.ControllerState
+
+class StreamInput(val context: Context, val preferences: Preferences)
+{
+ var controllerStateChangedCallback: ((ControllerState) -> Unit)? = null
+
+ val controllerState: ControllerState get()
+ {
+ val controllerState = sensorControllerState or keyControllerState or motionControllerState
+
+ val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
+ @Suppress("DEPRECATION")
+ when(windowManager.defaultDisplay.rotation)
+ {
+ Surface.ROTATION_90 -> {
+ controllerState.accelX *= -1.0f
+ controllerState.accelZ *= -1.0f
+ controllerState.gyroX *= -1.0f
+ controllerState.gyroZ *= -1.0f
+ controllerState.orientX *= -1.0f
+ controllerState.orientZ *= -1.0f
+ }
+ else -> {}
+ }
+
+ // prioritize motion controller's l2 and r2 over key
+ // (some controllers send only key, others both but key earlier than full press)
+ if(motionControllerState.l2State > 0U)
+ controllerState.l2State = motionControllerState.l2State
+ if(motionControllerState.r2State > 0U)
+ controllerState.r2State = motionControllerState.r2State
+
+ return controllerState or touchControllerState
+ }
+
+ private val sensorControllerState = ControllerState() // from Motion Sensors
+ private val keyControllerState = ControllerState() // from KeyEvents
+ private val motionControllerState = ControllerState() // from MotionEvents
+ var touchControllerState = ControllerState()
+ set(value)
+ {
+ field = value
+ controllerStateUpdated()
+ }
+
+ private val swapCrossMoon = preferences.swapCrossMoon
+
+ private val sensorEventListener = object: SensorEventListener {
+ override fun onSensorChanged(event: SensorEvent)
+ {
+ when(event.sensor.type)
+ {
+ Sensor.TYPE_ACCELEROMETER -> {
+ sensorControllerState.accelX = event.values[1] / SensorManager.GRAVITY_EARTH
+ sensorControllerState.accelY = event.values[2] / SensorManager.GRAVITY_EARTH
+ sensorControllerState.accelZ = event.values[0] / SensorManager.GRAVITY_EARTH
+ }
+ Sensor.TYPE_GYROSCOPE -> {
+ sensorControllerState.gyroX = event.values[1]
+ sensorControllerState.gyroY = event.values[2]
+ sensorControllerState.gyroZ = event.values[0]
+ }
+ Sensor.TYPE_ROTATION_VECTOR -> {
+ val q = floatArrayOf(0f, 0f, 0f, 0f)
+ SensorManager.getQuaternionFromVector(q, event.values)
+ sensorControllerState.orientX = q[2]
+ sensorControllerState.orientY = q[3]
+ sensorControllerState.orientZ = q[1]
+ sensorControllerState.orientW = q[0]
+ }
+ else -> return
+ }
+ controllerStateUpdated()
+ }
+
+ override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) {}
+ }
+
+ private val motionLifecycleObserver = object: LifecycleObserver {
+ @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
+ fun onResume()
+ {
+ val samplingPeriodUs = 4000
+ val sensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager
+ listOfNotNull(
+ sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
+ sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE),
+ sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR)
+ ).forEach {
+ sensorManager.registerListener(sensorEventListener, it, samplingPeriodUs)
+ }
+ }
+
+ @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
+ fun onPause()
+ {
+ val sensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager
+ sensorManager.unregisterListener(sensorEventListener)
+ }
+ }
+
+ fun observe(lifecycleOwner: LifecycleOwner)
+ {
+ if(preferences.motionEnabled)
+ lifecycleOwner.lifecycle.addObserver(motionLifecycleObserver)
+ }
+
+ private fun controllerStateUpdated()
+ {
+ controllerStateChangedCallback?.let { it(controllerState) }
+ }
+
+ fun dispatchKeyEvent(event: KeyEvent): Boolean
+ {
+ //Log.i("StreamSession", "key event $event")
+ if(event.action != KeyEvent.ACTION_DOWN && event.action != KeyEvent.ACTION_UP)
+ return false
+
+ when(event.keyCode)
+ {
+ KeyEvent.KEYCODE_BUTTON_L2 -> {
+ keyControllerState.l2State = if(event.action == KeyEvent.ACTION_DOWN) UByte.MAX_VALUE else 0U
+ return true
+ }
+ KeyEvent.KEYCODE_BUTTON_R2 -> {
+ keyControllerState.r2State = if(event.action == KeyEvent.ACTION_DOWN) UByte.MAX_VALUE else 0U
+ return true
+ }
+ }
+
+ val buttonMask: UInt = when(event.keyCode)
+ {
+ // dpad handled by MotionEvents
+ //KeyEvent.KEYCODE_DPAD_LEFT -> ControllerState.BUTTON_DPAD_LEFT
+ //KeyEvent.KEYCODE_DPAD_RIGHT -> ControllerState.BUTTON_DPAD_RIGHT
+ //KeyEvent.KEYCODE_DPAD_UP -> ControllerState.BUTTON_DPAD_UP
+ //KeyEvent.KEYCODE_DPAD_DOWN -> ControllerState.BUTTON_DPAD_DOWN
+ KeyEvent.KEYCODE_BUTTON_A -> if(swapCrossMoon) ControllerState.BUTTON_MOON else ControllerState.BUTTON_CROSS
+ KeyEvent.KEYCODE_BUTTON_B -> if(swapCrossMoon) ControllerState.BUTTON_CROSS else ControllerState.BUTTON_MOON
+ KeyEvent.KEYCODE_BUTTON_X -> if(swapCrossMoon) ControllerState.BUTTON_PYRAMID else ControllerState.BUTTON_BOX
+ KeyEvent.KEYCODE_BUTTON_Y -> if(swapCrossMoon) ControllerState.BUTTON_BOX else ControllerState.BUTTON_PYRAMID
+ KeyEvent.KEYCODE_BUTTON_L1 -> ControllerState.BUTTON_L1
+ KeyEvent.KEYCODE_BUTTON_R1 -> ControllerState.BUTTON_R1
+ KeyEvent.KEYCODE_BUTTON_THUMBL -> ControllerState.BUTTON_L3
+ KeyEvent.KEYCODE_BUTTON_THUMBR -> ControllerState.BUTTON_R3
+ KeyEvent.KEYCODE_BUTTON_SELECT -> ControllerState.BUTTON_SHARE
+ KeyEvent.KEYCODE_BUTTON_START -> ControllerState.BUTTON_OPTIONS
+ KeyEvent.KEYCODE_BUTTON_C -> ControllerState.BUTTON_PS
+ KeyEvent.KEYCODE_BUTTON_MODE -> ControllerState.BUTTON_PS
+ else -> return false
+ }
+
+ keyControllerState.buttons = keyControllerState.buttons.run {
+ when(event.action)
+ {
+ KeyEvent.ACTION_DOWN -> this or buttonMask
+ KeyEvent.ACTION_UP -> this and buttonMask.inv()
+ else -> this
+ }
+ }
+
+ controllerStateUpdated()
+ return true
+ }
+
+ fun onGenericMotionEvent(event: MotionEvent): Boolean
+ {
+ if(event.source and InputDevice.SOURCE_CLASS_JOYSTICK != InputDevice.SOURCE_CLASS_JOYSTICK)
+ return false
+ fun Float.signedAxis() = (this * Short.MAX_VALUE).toInt().toShort()
+ fun Float.unsignedAxis() = (this * UByte.MAX_VALUE.toFloat()).toUInt().toUByte()
+ motionControllerState.leftX = event.getAxisValue(MotionEvent.AXIS_X).signedAxis()
+ motionControllerState.leftY = event.getAxisValue(MotionEvent.AXIS_Y).signedAxis()
+ motionControllerState.rightX = event.getAxisValue(MotionEvent.AXIS_Z).signedAxis()
+ motionControllerState.rightY = event.getAxisValue(MotionEvent.AXIS_RZ).signedAxis()
+ motionControllerState.l2State = event.getAxisValue(MotionEvent.AXIS_LTRIGGER).unsignedAxis()
+ motionControllerState.r2State = event.getAxisValue(MotionEvent.AXIS_RTRIGGER).unsignedAxis()
+ motionControllerState.buttons = motionControllerState.buttons.let {
+ val dpadX = event.getAxisValue(MotionEvent.AXIS_HAT_X)
+ val dpadY = event.getAxisValue(MotionEvent.AXIS_HAT_Y)
+ val dpadButtons =
+ (if(dpadX > 0.5f) ControllerState.BUTTON_DPAD_RIGHT else 0U) or
+ (if(dpadX < -0.5f) ControllerState.BUTTON_DPAD_LEFT else 0U) or
+ (if(dpadY > 0.5f) ControllerState.BUTTON_DPAD_DOWN else 0U) or
+ (if(dpadY < -0.5f) ControllerState.BUTTON_DPAD_UP else 0U)
+ it and (ControllerState.BUTTON_DPAD_RIGHT or
+ ControllerState.BUTTON_DPAD_LEFT or
+ ControllerState.BUTTON_DPAD_DOWN or
+ ControllerState.BUTTON_DPAD_UP).inv() or
+ dpadButtons
+ }
+ //Log.i("StreamSession", "motionEvent => $motionControllerState")
+ controllerStateUpdated()
+ return true
+ }
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/session/StreamSession.kt b/android/app/src/main/java/com/metallic/chiaki/session/StreamSession.kt
new file mode 100644
index 0000000..dd32a40
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/session/StreamSession.kt
@@ -0,0 +1,147 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.session
+
+import android.graphics.SurfaceTexture
+import android.util.Log
+import android.view.*
+import androidx.lifecycle.LiveData
+import androidx.lifecycle.MutableLiveData
+import com.metallic.chiaki.common.LogManager
+import com.metallic.chiaki.lib.*
+
+sealed class StreamState
+object StreamStateIdle: StreamState()
+object StreamStateConnecting: StreamState()
+object StreamStateConnected: StreamState()
+data class StreamStateCreateError(val error: CreateError): StreamState()
+data class StreamStateQuit(val reason: QuitReason, val reasonString: String?): StreamState()
+data class StreamStateLoginPinRequest(val pinIncorrect: Boolean): StreamState()
+
+class StreamSession(val connectInfo: ConnectInfo, val logManager: LogManager, val logVerbose: Boolean, val input: StreamInput)
+{
+ var session: Session? = null
+ private set
+
+ private val _state = MutableLiveData(StreamStateIdle)
+ val state: LiveData get() = _state
+ private val _rumbleState = MutableLiveData(RumbleEvent(0U, 0U))
+ val rumbleState: LiveData get() = _rumbleState
+
+ private var surfaceTexture: SurfaceTexture? = null
+ private var surface: Surface? = null
+
+ init
+ {
+ input.controllerStateChangedCallback = {
+ session?.setControllerState(it)
+ }
+ }
+
+ fun shutdown()
+ {
+ session?.stop()
+ session?.dispose()
+ session = null
+ _state.value = StreamStateIdle
+ //surfaceTexture?.release()
+ }
+
+ fun pause()
+ {
+ shutdown()
+ }
+
+ fun resume()
+ {
+ if(session != null)
+ return
+ try
+ {
+ val session = Session(connectInfo, logManager.createNewFile().file.absolutePath, logVerbose)
+ _state.value = StreamStateConnecting
+ session.eventCallback = this::eventCallback
+ session.start()
+ val surface = surface
+ if(surface != null)
+ session.setSurface(surface)
+ this.session = session
+ }
+ catch(e: CreateError)
+ {
+ _state.value = StreamStateCreateError(e)
+ }
+ }
+
+ private fun eventCallback(event: Event)
+ {
+ when(event)
+ {
+ is ConnectedEvent -> _state.postValue(StreamStateConnected)
+ is QuitEvent -> _state.postValue(
+ StreamStateQuit(
+ event.reason,
+ event.reasonString
+ )
+ )
+ is LoginPinRequestEvent -> _state.postValue(
+ StreamStateLoginPinRequest(
+ event.pinIncorrect
+ )
+ )
+ is RumbleEvent -> _rumbleState.postValue(event)
+ }
+ }
+
+ fun attachToSurfaceView(surfaceView: SurfaceView)
+ {
+ surfaceView.holder.addCallback(object: SurfaceHolder.Callback {
+ override fun surfaceCreated(holder: SurfaceHolder) { }
+
+ override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int)
+ {
+ val surface = holder.surface
+ this@StreamSession.surface = surface
+ session?.setSurface(surface)
+ }
+
+ override fun surfaceDestroyed(holder: SurfaceHolder)
+ {
+ this@StreamSession.surface = null
+ session?.setSurface(null)
+ }
+ })
+ }
+
+ fun attachToTextureView(textureView: TextureView)
+ {
+ textureView.surfaceTextureListener = object: TextureView.SurfaceTextureListener {
+ override fun onSurfaceTextureAvailable(surface: SurfaceTexture, width: Int, height: Int)
+ {
+ if(surfaceTexture != null)
+ return
+ surfaceTexture = surface
+ this@StreamSession.surface = Surface(surfaceTexture)
+ session?.setSurface(Surface(surface))
+ }
+
+ override fun onSurfaceTextureDestroyed(surface: SurfaceTexture): Boolean
+ {
+ // return false if we want to keep the surface texture
+ return surfaceTexture == null
+ }
+
+ override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture, width: Int, height: Int) { }
+ override fun onSurfaceTextureUpdated(surface: SurfaceTexture) {}
+ }
+
+ val surfaceTexture = surfaceTexture
+ if(surfaceTexture != null)
+ textureView.setSurfaceTexture(surfaceTexture)
+ }
+
+ fun setLoginPin(pin: String)
+ {
+ session?.setLoginPin(pin)
+ }
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/settings/ItemTouchSwipeCallback.kt b/android/app/src/main/java/com/metallic/chiaki/settings/ItemTouchSwipeCallback.kt
new file mode 100644
index 0000000..7168bce
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/settings/ItemTouchSwipeCallback.kt
@@ -0,0 +1,51 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.settings
+
+import android.content.Context
+import android.graphics.Canvas
+import android.graphics.Rect
+import androidx.core.graphics.withClip
+import androidx.recyclerview.widget.ItemTouchHelper
+import androidx.recyclerview.widget.RecyclerView
+import com.metallic.chiaki.R
+
+abstract class ItemTouchSwipeCallback(context: Context): ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT)
+{
+ private val backgroundDrawable = context.getDrawable(R.color.item_delete_background)
+ private val icon = context.getDrawable(R.drawable.ic_delete_row)
+
+ override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder) = false
+
+ override fun onChildDraw(canvas: Canvas, recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, dX: Float, dY: Float, actionState: Int, isCurrentlyActive: Boolean)
+ {
+ val itemView = viewHolder.itemView
+ val itemHeight = itemView.bottom - itemView.top
+
+ val bounds = Rect(
+ itemView.right + dX.toInt(),
+ itemView.top,
+ itemView.right,
+ itemView.bottom
+ )
+
+ backgroundDrawable?.bounds = bounds
+ backgroundDrawable?.draw(canvas)
+
+ val icon = icon
+ if(icon != null)
+ {
+ val iconMargin = (itemHeight - icon.intrinsicHeight) / 2
+ val iconTop = itemView.top + iconMargin
+ val iconLeft = itemView.right - iconMargin - icon.intrinsicWidth
+ val iconRight = itemView.right - iconMargin
+ val iconBottom = iconTop + icon.intrinsicHeight
+ canvas.withClip(bounds) {
+ icon.setBounds(iconLeft, iconTop, iconRight, iconBottom)
+ icon.draw(canvas)
+ }
+ }
+
+ super.onChildDraw(canvas, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
+ }
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/settings/SettingsActivity.kt b/android/app/src/main/java/com/metallic/chiaki/settings/SettingsActivity.kt
new file mode 100644
index 0000000..1cf224f
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/settings/SettingsActivity.kt
@@ -0,0 +1,60 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.settings
+
+import android.content.res.Resources
+import android.os.Bundle
+import androidx.appcompat.app.AppCompatActivity
+import androidx.fragment.app.Fragment
+import androidx.preference.Preference
+import androidx.preference.PreferenceFragmentCompat
+import com.metallic.chiaki.R
+import com.metallic.chiaki.databinding.ActivitySettingsBinding
+
+interface TitleFragment
+{
+ fun getTitle(resources: Resources): String
+}
+
+class SettingsActivity: AppCompatActivity(), PreferenceFragmentCompat.OnPreferenceStartFragmentCallback
+{
+ private lateinit var binding: ActivitySettingsBinding
+
+ override fun onCreate(savedInstanceState: Bundle?)
+ {
+ super.onCreate(savedInstanceState)
+ binding = ActivitySettingsBinding.inflate(layoutInflater)
+ setContentView(binding.root)
+ title = ""
+ setSupportActionBar(binding.toolbar)
+
+ val rootFragment = SettingsFragment()
+ replaceFragment(rootFragment, false)
+ supportFragmentManager.addOnBackStackChangedListener {
+ val titleFragment = supportFragmentManager.findFragmentById(R.id.settingsFragment) as? TitleFragment ?: return@addOnBackStackChangedListener
+ binding.titleTextView.text = titleFragment.getTitle(resources)
+ }
+ binding.titleTextView.text = rootFragment.getTitle(resources)
+ }
+
+ override fun onPreferenceStartFragment(caller: PreferenceFragmentCompat, pref: Preference) = when(pref.fragment)
+ {
+ SettingsRegisteredHostsFragment::class.java.canonicalName -> {
+ replaceFragment(SettingsRegisteredHostsFragment(), true)
+ true
+ }
+ else -> false
+ }
+
+ private fun replaceFragment(fragment: Fragment, addToBackStack: Boolean)
+ {
+ supportFragmentManager.beginTransaction()
+ .setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out)
+ .replace(R.id.settingsFragment, fragment)
+ .also {
+ if(addToBackStack)
+ it.addToBackStack(null)
+ }
+ .commit()
+ }
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/settings/SettingsFragment.kt b/android/app/src/main/java/com/metallic/chiaki/settings/SettingsFragment.kt
new file mode 100644
index 0000000..f574d09
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/settings/SettingsFragment.kt
@@ -0,0 +1,174 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.settings
+
+import android.app.Activity
+import android.content.Intent
+import android.content.res.Resources
+import android.os.Bundle
+import android.text.InputType
+import androidx.lifecycle.Observer
+import androidx.lifecycle.ViewModelProvider
+import androidx.preference.*
+import com.metallic.chiaki.R
+import com.metallic.chiaki.common.Preferences
+import com.metallic.chiaki.common.exportAndShareAllSettings
+import com.metallic.chiaki.common.ext.viewModelFactory
+import com.metallic.chiaki.common.getDatabase
+import com.metallic.chiaki.common.importSettingsFromUri
+import io.reactivex.disposables.CompositeDisposable
+import io.reactivex.rxkotlin.addTo
+
+class DataStore(val preferences: Preferences): PreferenceDataStore()
+{
+ override fun getBoolean(key: String?, defValue: Boolean) = when(key)
+ {
+ preferences.logVerboseKey -> preferences.logVerbose
+ preferences.swapCrossMoonKey -> preferences.swapCrossMoon
+ preferences.rumbleEnabledKey -> preferences.rumbleEnabled
+ preferences.motionEnabledKey -> preferences.motionEnabled
+ preferences.buttonHapticEnabledKey -> preferences.buttonHapticEnabled
+ else -> defValue
+ }
+
+ override fun putBoolean(key: String?, value: Boolean)
+ {
+ when(key)
+ {
+ preferences.logVerboseKey -> preferences.logVerbose = value
+ preferences.swapCrossMoonKey -> preferences.swapCrossMoon = value
+ preferences.rumbleEnabledKey -> preferences.rumbleEnabled = value
+ preferences.motionEnabledKey -> preferences.motionEnabled = value
+ preferences.buttonHapticEnabledKey -> preferences.buttonHapticEnabled = value
+ }
+ }
+
+ override fun getString(key: String, defValue: String?) = when(key)
+ {
+ preferences.resolutionKey -> preferences.resolution.value
+ preferences.fpsKey -> preferences.fps.value
+ preferences.bitrateKey -> preferences.bitrate?.toString() ?: ""
+ preferences.codecKey -> preferences.codec.value
+ else -> defValue
+ }
+
+ override fun putString(key: String, value: String?)
+ {
+ when(key)
+ {
+ preferences.resolutionKey ->
+ {
+ val resolution = Preferences.Resolution.values().firstOrNull { it.value == value } ?: return
+ preferences.resolution = resolution
+ }
+ preferences.fpsKey ->
+ {
+ val fps = Preferences.FPS.values().firstOrNull { it.value == value } ?: return
+ preferences.fps = fps
+ }
+ preferences.bitrateKey -> preferences.bitrate = value?.toIntOrNull()
+ preferences.codecKey ->
+ {
+ val codec = Preferences.Codec.values().firstOrNull { it.value == value } ?: return
+ preferences.codec = codec
+ }
+ }
+ }
+}
+
+class SettingsFragment: PreferenceFragmentCompat(), TitleFragment
+{
+ companion object
+ {
+ private const val PICK_SETTINGS_JSON_REQUEST = 1
+ }
+
+ private var disposable = CompositeDisposable()
+ private var exportDisposable = CompositeDisposable().also { it.addTo(disposable) }
+
+ override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?)
+ {
+ val context = context ?: return
+
+ val viewModel = ViewModelProvider(this, viewModelFactory { SettingsViewModel(getDatabase(context), Preferences(context)) })
+ .get(SettingsViewModel::class.java)
+
+ val preferences = viewModel.preferences
+ preferenceManager.preferenceDataStore = DataStore(preferences)
+ setPreferencesFromResource(R.xml.preferences, rootKey)
+
+ preferenceScreen.findPreference(getString(R.string.preferences_resolution_key))?.let {
+ it.entryValues = Preferences.resolutionAll.map { res -> res.value }.toTypedArray()
+ it.entries = Preferences.resolutionAll.map { res -> getString(res.title) }.toTypedArray()
+ }
+
+ preferenceScreen.findPreference(getString(R.string.preferences_fps_key))?.let {
+ it.entryValues = Preferences.fpsAll.map { fps -> fps.value }.toTypedArray()
+ it.entries = Preferences.fpsAll.map { fps -> getString(fps.title) }.toTypedArray()
+ }
+
+ val bitratePreference = preferenceScreen.findPreference(getString(R.string.preferences_bitrate_key))
+ val bitrateSummaryProvider = Preference.SummaryProvider {
+ preferences.bitrate?.toString() ?: getString(R.string.preferences_bitrate_auto, preferences.bitrateAuto)
+ }
+ bitratePreference?.let {
+ it.summaryProvider = bitrateSummaryProvider
+ it.setOnBindEditTextListener { editText ->
+ editText.hint = getString(R.string.preferences_bitrate_auto, preferences.bitrateAuto)
+ editText.inputType = InputType.TYPE_CLASS_NUMBER
+ editText.setText(preferences.bitrate?.toString() ?: "")
+ }
+ }
+ viewModel.bitrateAuto.observe(this, Observer {
+ bitratePreference?.summaryProvider = bitrateSummaryProvider
+ })
+
+ preferenceScreen.findPreference(getString(R.string.preferences_codec_key))?.let {
+ it.entryValues = Preferences.codecAll.map { codec -> codec.value }.toTypedArray()
+ it.entries = Preferences.codecAll.map { codec -> getString(codec.title) }.toTypedArray()
+ }
+
+ val registeredHostsPreference = preferenceScreen.findPreference("registered_hosts")
+ viewModel.registeredHostsCount.observe(this, Observer {
+ registeredHostsPreference?.summary = getString(R.string.preferences_registered_hosts_summary, it)
+ })
+
+ preferenceScreen.findPreference(getString(R.string.preferences_export_settings_key))?.setOnPreferenceClickListener { exportSettings(); true }
+ preferenceScreen.findPreference(getString(R.string.preferences_import_settings_key))?.setOnPreferenceClickListener { importSettings(); true }
+ }
+
+ override fun onDestroy()
+ {
+ super.onDestroy()
+ disposable.dispose()
+ }
+
+ override fun getTitle(resources: Resources): String = resources.getString(R.string.title_settings)
+
+ private fun exportSettings()
+ {
+ val activity = activity ?: return
+ exportDisposable.clear()
+ exportAndShareAllSettings(activity).addTo(exportDisposable)
+ }
+
+ private fun importSettings()
+ {
+ val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
+ addCategory(Intent.CATEGORY_OPENABLE)
+ type = "application/json"
+ }
+ startActivityForResult(intent, PICK_SETTINGS_JSON_REQUEST)
+ }
+
+ override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?)
+ {
+ if(requestCode == PICK_SETTINGS_JSON_REQUEST && resultCode == Activity.RESULT_OK)
+ {
+ val activity = activity ?: return
+ data?.data?.also {
+ importSettingsFromUri(activity, it, disposable)
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/settings/SettingsLogsAdapter.kt b/android/app/src/main/java/com/metallic/chiaki/settings/SettingsLogsAdapter.kt
new file mode 100644
index 0000000..6acf547
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/settings/SettingsLogsAdapter.kt
@@ -0,0 +1,44 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.settings
+
+import android.view.LayoutInflater
+import android.view.ViewGroup
+import androidx.recyclerview.widget.RecyclerView
+import com.metallic.chiaki.R
+import com.metallic.chiaki.common.LogFile
+import com.metallic.chiaki.common.ext.inflate
+import com.metallic.chiaki.databinding.ItemLogFileBinding
+import java.text.DateFormat
+import java.text.SimpleDateFormat
+import java.util.*
+
+class SettingsLogsAdapter: RecyclerView.Adapter()
+{
+ var shareCallback: ((LogFile) -> Unit)? = null
+
+ private val dateFormat: DateFormat = DateFormat.getDateInstance(DateFormat.SHORT)
+ private val timeFormat = SimpleDateFormat("HH:mm:ss:SSS", Locale.getDefault())
+
+ class ViewHolder(val binding: ItemLogFileBinding): RecyclerView.ViewHolder(binding.root)
+
+ var logFiles: List = listOf()
+ set(value)
+ {
+ field = value
+ notifyDataSetChanged()
+ }
+
+ override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
+ ViewHolder(ItemLogFileBinding.inflate(LayoutInflater.from(parent.context), parent, false))
+
+ override fun getItemCount() = logFiles.size
+
+ override fun onBindViewHolder(holder: ViewHolder, position: Int)
+ {
+ val logFile = logFiles[position]
+ holder.binding.nameTextView.text = "${dateFormat.format(logFile.date)} ${timeFormat.format(logFile.date)}"
+ holder.binding.summaryTextView.text = logFile.filename
+ holder.binding.shareButton.setOnClickListener { shareCallback?.let { it(logFile) } }
+ }
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/settings/SettingsLogsFragment.kt b/android/app/src/main/java/com/metallic/chiaki/settings/SettingsLogsFragment.kt
new file mode 100644
index 0000000..0397172
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/settings/SettingsLogsFragment.kt
@@ -0,0 +1,82 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.settings
+
+import android.content.ClipData
+import android.content.Intent
+import android.content.res.Resources
+import android.os.Bundle
+import android.view.LayoutInflater
+import android.view.View
+import android.view.ViewGroup
+import androidx.appcompat.app.AppCompatDialogFragment
+import androidx.core.content.FileProvider
+import androidx.lifecycle.Observer
+import androidx.lifecycle.ViewModelProvider
+import androidx.recyclerview.widget.ItemTouchHelper
+import androidx.recyclerview.widget.LinearLayoutManager
+import androidx.recyclerview.widget.RecyclerView
+import com.metallic.chiaki.R
+import com.metallic.chiaki.common.LogFile
+import com.metallic.chiaki.common.LogManager
+import com.metallic.chiaki.common.ext.viewModelFactory
+import com.metallic.chiaki.common.fileProviderAuthority
+import com.metallic.chiaki.databinding.FragmentSettingsLogsBinding
+
+class SettingsLogsFragment: AppCompatDialogFragment(), TitleFragment
+{
+ private lateinit var viewModel: SettingsLogsViewModel
+
+ private var _binding: FragmentSettingsLogsBinding? = null
+ private val binding get() = _binding!!
+
+ override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View =
+ FragmentSettingsLogsBinding.inflate(inflater, container, false).let {
+ _binding = it
+ it.root
+ }
+
+ override fun onViewCreated(view: View, savedInstanceState: Bundle?)
+ {
+ val context = requireContext()
+
+ viewModel = ViewModelProvider(this, viewModelFactory { SettingsLogsViewModel(LogManager(context)) })
+ .get(SettingsLogsViewModel::class.java)
+
+ val adapter = SettingsLogsAdapter()
+ binding.logsRecyclerView.layoutManager = LinearLayoutManager(context)
+ binding.logsRecyclerView.adapter = adapter
+ adapter.shareCallback = this::shareLogFile
+ viewModel.sessionLogs.observe(viewLifecycleOwner, Observer {
+ adapter.logFiles = it
+ binding.emptyInfoGroup.visibility = if(it.isEmpty()) View.VISIBLE else View.GONE
+ })
+
+ val itemTouchSwipeCallback = object : ItemTouchSwipeCallback(context)
+ {
+ override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int)
+ {
+ val pos = viewHolder.adapterPosition
+ val file = viewModel.sessionLogs.value?.getOrNull(pos) ?: return
+ viewModel.deleteLog(file)
+ }
+ }
+ ItemTouchHelper(itemTouchSwipeCallback).attachToRecyclerView(binding.logsRecyclerView)
+ }
+
+ override fun getTitle(resources: Resources): String = resources.getString(R.string.preferences_logs_title)
+
+ private fun shareLogFile(file: LogFile)
+ {
+ val activity = activity ?: return
+ val uri = FileProvider.getUriForFile(activity, fileProviderAuthority, file.file)
+ Intent(Intent.ACTION_SEND).also {
+ it.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
+ it.type = "text/plain"
+ it.putExtra(Intent.EXTRA_STREAM, uri)
+ it.clipData = ClipData.newRawUri("", uri)
+ startActivity(Intent.createChooser(it, resources.getString(R.string.action_share_log)))
+ }
+
+ }
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/settings/SettingsLogsViewModel.kt b/android/app/src/main/java/com/metallic/chiaki/settings/SettingsLogsViewModel.kt
new file mode 100644
index 0000000..89a58de
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/settings/SettingsLogsViewModel.kt
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.settings
+
+import androidx.lifecycle.LiveData
+import androidx.lifecycle.MutableLiveData
+import androidx.lifecycle.ViewModel
+import com.metallic.chiaki.common.AppDatabase
+import com.metallic.chiaki.common.LogFile
+import com.metallic.chiaki.common.LogManager
+import com.metallic.chiaki.common.RegisteredHost
+import com.metallic.chiaki.common.ext.toLiveData
+import io.reactivex.disposables.CompositeDisposable
+import io.reactivex.rxkotlin.addTo
+import io.reactivex.schedulers.Schedulers
+
+class SettingsLogsViewModel(val logManager: LogManager): ViewModel()
+{
+ private val _sessionLogs = MutableLiveData>(logManager.files)
+ val sessionLogs: LiveData> get() = _sessionLogs
+
+ private fun updateLogs()
+ {
+ _sessionLogs.value = logManager.files
+ }
+
+ fun deleteLog(file: LogFile)
+ {
+ file.file.delete()
+ updateLogs()
+ }
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/settings/SettingsRegisteredHostsAdapter.kt b/android/app/src/main/java/com/metallic/chiaki/settings/SettingsRegisteredHostsAdapter.kt
new file mode 100644
index 0000000..509d594
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/settings/SettingsRegisteredHostsAdapter.kt
@@ -0,0 +1,33 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.settings
+
+import android.view.LayoutInflater
+import android.view.ViewGroup
+import androidx.recyclerview.widget.RecyclerView
+import com.metallic.chiaki.common.RegisteredHost
+import com.metallic.chiaki.databinding.ItemRegisteredHostBinding
+
+class SettingsRegisteredHostsAdapter: RecyclerView.Adapter()
+{
+ class ViewHolder(val binding: ItemRegisteredHostBinding): RecyclerView.ViewHolder(binding.root)
+
+ var hosts: List = listOf()
+ set(value)
+ {
+ field = value
+ notifyDataSetChanged()
+ }
+
+ override fun onCreateViewHolder(parent: ViewGroup, viewType: Int)
+ = ViewHolder(ItemRegisteredHostBinding.inflate(LayoutInflater.from(parent.context), parent, false))
+
+ override fun getItemCount() = hosts.size
+
+ override fun onBindViewHolder(holder: ViewHolder, position: Int)
+ {
+ val host = hosts[position]
+ holder.binding.nameTextView.text = "${host.serverNickname} (${if(host.target.isPS5) "PS5" else "PS4"})"
+ holder.binding.summaryTextView.text = host.serverMac.toString()
+ }
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/settings/SettingsRegisteredHostsFragment.kt b/android/app/src/main/java/com/metallic/chiaki/settings/SettingsRegisteredHostsFragment.kt
new file mode 100644
index 0000000..229b37c
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/settings/SettingsRegisteredHostsFragment.kt
@@ -0,0 +1,81 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.settings
+
+import android.app.ActivityOptions
+import android.content.Intent
+import android.content.res.Resources
+import android.os.Bundle
+import android.view.LayoutInflater
+import android.view.View
+import android.view.ViewGroup
+import androidx.appcompat.app.AppCompatDialogFragment
+import androidx.lifecycle.Observer
+import androidx.lifecycle.ViewModelProvider
+import androidx.recyclerview.widget.ItemTouchHelper
+import androidx.recyclerview.widget.LinearLayoutManager
+import androidx.recyclerview.widget.RecyclerView
+import com.google.android.material.dialog.MaterialAlertDialogBuilder
+import com.metallic.chiaki.R
+import com.metallic.chiaki.common.ext.putRevealExtra
+import com.metallic.chiaki.common.ext.viewModelFactory
+import com.metallic.chiaki.common.getDatabase
+import com.metallic.chiaki.databinding.FragmentSettingsRegisteredHostsBinding
+import com.metallic.chiaki.regist.RegistActivity
+
+class SettingsRegisteredHostsFragment: AppCompatDialogFragment(), TitleFragment
+{
+ private lateinit var viewModel: SettingsRegisteredHostsViewModel
+
+ private var _binding: FragmentSettingsRegisteredHostsBinding? = null
+ private val binding get() = _binding!!
+
+ override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View =
+ FragmentSettingsRegisteredHostsBinding.inflate(inflater, container, false).let {
+ _binding = it
+ it.root
+ }
+
+ override fun onViewCreated(view: View, savedInstanceState: Bundle?)
+ {
+ val context = requireContext()
+ viewModel = ViewModelProvider(this, viewModelFactory { SettingsRegisteredHostsViewModel(getDatabase(context)) })
+ .get(SettingsRegisteredHostsViewModel::class.java)
+
+ val adapter = SettingsRegisteredHostsAdapter()
+ binding.hostsRecyclerView.layoutManager = LinearLayoutManager(context)
+ binding.hostsRecyclerView.adapter = adapter
+ val itemTouchSwipeCallback = object : ItemTouchSwipeCallback(context)
+ {
+ override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int)
+ {
+ val pos = viewHolder.adapterPosition
+ val host = viewModel.registeredHosts.value?.getOrNull(pos) ?: return
+ MaterialAlertDialogBuilder(viewHolder.itemView.context)
+ .setMessage(getString(R.string.alert_message_delete_registered_host, host.serverNickname, host.serverMac.toString()))
+ .setPositiveButton(R.string.action_delete) { _, _ ->
+ viewModel.deleteHost(host)
+ }
+ .setNegativeButton(R.string.action_keep) { _, _ ->
+ adapter.notifyItemChanged(pos) // to reset the swipe
+ }
+ .create()
+ .show()
+ }
+ }
+ ItemTouchHelper(itemTouchSwipeCallback).attachToRecyclerView(binding.hostsRecyclerView)
+ viewModel.registeredHosts.observe(this, Observer {
+ adapter.hosts = it
+ binding.emptyInfoGroup.visibility = if(it.isEmpty()) View.VISIBLE else View.GONE
+ })
+
+ binding.floatingActionButton.setOnClickListener {
+ Intent(context, RegistActivity::class.java).also {
+ it.putRevealExtra(binding.floatingActionButton, binding.rootLayout)
+ startActivity(it, ActivityOptions.makeSceneTransitionAnimation(activity).toBundle())
+ }
+ }
+ }
+
+ override fun getTitle(resources: Resources): String = resources.getString(R.string.preferences_registered_hosts_title)
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/settings/SettingsRegisteredHostsViewModel.kt b/android/app/src/main/java/com/metallic/chiaki/settings/SettingsRegisteredHostsViewModel.kt
new file mode 100644
index 0000000..a00e897
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/settings/SettingsRegisteredHostsViewModel.kt
@@ -0,0 +1,35 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.settings
+
+import androidx.lifecycle.ViewModel
+import com.metallic.chiaki.common.AppDatabase
+import com.metallic.chiaki.common.RegisteredHost
+import com.metallic.chiaki.common.ext.toLiveData
+import io.reactivex.disposables.CompositeDisposable
+import io.reactivex.rxkotlin.addTo
+import io.reactivex.schedulers.Schedulers
+
+class SettingsRegisteredHostsViewModel(val database: AppDatabase): ViewModel()
+{
+ private val disposable = CompositeDisposable()
+
+ val registeredHosts by lazy {
+ database.registeredHostDao().getAll().toLiveData()
+ }
+
+ fun deleteHost(host: RegisteredHost)
+ {
+ database.registeredHostDao()
+ .delete(host)
+ .subscribeOn(Schedulers.io())
+ .subscribe()
+ .addTo(disposable)
+ }
+
+ override fun onCleared()
+ {
+ super.onCleared()
+ disposable.dispose()
+ }
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/settings/SettingsViewModel.kt b/android/app/src/main/java/com/metallic/chiaki/settings/SettingsViewModel.kt
new file mode 100644
index 0000000..3df021c
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/settings/SettingsViewModel.kt
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.settings
+
+import androidx.lifecycle.ViewModel
+import com.metallic.chiaki.common.AppDatabase
+import com.metallic.chiaki.common.Preferences
+import com.metallic.chiaki.common.ext.toLiveData
+
+class SettingsViewModel(val database: AppDatabase, val preferences: Preferences): ViewModel()
+{
+ val registeredHostsCount by lazy {
+ database.registeredHostDao().count().toLiveData()
+ }
+
+ val bitrateAuto by lazy {
+ preferences.bitrateAutoObservable.toLiveData()
+ }
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/stream/AspectRatioFrameLayout.kt b/android/app/src/main/java/com/metallic/chiaki/stream/AspectRatioFrameLayout.kt
new file mode 100644
index 0000000..5394f02
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/stream/AspectRatioFrameLayout.kt
@@ -0,0 +1,68 @@
+package com.metallic.chiaki.stream
+
+import android.content.Context
+import android.util.AttributeSet
+import android.widget.FrameLayout
+
+// see ExoPlayer's AspectRatioFrameLayout
+class AspectRatioFrameLayout @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null): FrameLayout(context, attrs)
+{
+ companion object
+ {
+ private const val MAX_ASPECT_RATIO_DEFORMATION_FRACTION = 0.01f
+ }
+
+ var aspectRatio = 0f
+ set(value)
+ {
+ if(field != value)
+ {
+ field = value
+ requestLayout()
+ }
+ }
+
+ var mode: TransformMode = TransformMode.FIT
+ set(value)
+ {
+ if(field != value)
+ {
+ field = value
+ requestLayout()
+ }
+ }
+
+ override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int)
+ {
+ super.onMeasure(widthMeasureSpec, heightMeasureSpec)
+ if(aspectRatio <= 0)
+ {
+ // Aspect ratio not set.
+ return
+ }
+ var width = measuredWidth
+ var height = measuredHeight
+ val viewAspectRatio = width.toFloat() / height
+ val aspectDeformation = aspectRatio / viewAspectRatio - 1
+ if(Math.abs(aspectDeformation) <= MAX_ASPECT_RATIO_DEFORMATION_FRACTION)
+ return
+ when(mode)
+ {
+ TransformMode.ZOOM ->
+ if(aspectDeformation > 0)
+ width = (height * aspectRatio).toInt()
+ else
+ height = (width / aspectRatio).toInt()
+ TransformMode.FIT ->
+ if(aspectDeformation > 0)
+ height = (width / aspectRatio).toInt()
+ else
+ width = (height * aspectRatio).toInt()
+ TransformMode.STRETCH -> {}
+ }
+ super.onMeasure(
+ MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
+ MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)
+ )
+ }
+}
diff --git a/android/app/src/main/java/com/metallic/chiaki/stream/StreamActivity.kt b/android/app/src/main/java/com/metallic/chiaki/stream/StreamActivity.kt
new file mode 100644
index 0000000..bd77e0c
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/stream/StreamActivity.kt
@@ -0,0 +1,400 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.stream
+
+import android.animation.Animator
+import android.animation.AnimatorListenerAdapter
+import android.app.AlertDialog
+import android.graphics.Matrix
+import android.os.*
+import android.view.*
+import android.widget.EditText
+import androidx.appcompat.app.AppCompatActivity
+import androidx.core.view.isGone
+import androidx.core.view.isVisible
+import androidx.fragment.app.Fragment
+import androidx.lifecycle.*
+import com.google.android.material.dialog.MaterialAlertDialogBuilder
+import com.metallic.chiaki.R
+import com.metallic.chiaki.common.Preferences
+import com.metallic.chiaki.common.ext.viewModelFactory
+import com.metallic.chiaki.databinding.ActivityStreamBinding
+import com.metallic.chiaki.lib.ConnectInfo
+import com.metallic.chiaki.lib.ConnectVideoProfile
+import com.metallic.chiaki.session.*
+import com.metallic.chiaki.touchcontrols.DefaultTouchControlsFragment
+import com.metallic.chiaki.touchcontrols.TouchControlsFragment
+import com.metallic.chiaki.touchcontrols.TouchpadOnlyFragment
+import io.reactivex.disposables.CompositeDisposable
+import io.reactivex.rxkotlin.addTo
+import kotlin.math.min
+
+private sealed class DialogContents
+private object StreamQuitDialog: DialogContents()
+private object CreateErrorDialog: DialogContents()
+private object PinRequestDialog: DialogContents()
+
+class StreamActivity : AppCompatActivity(), View.OnSystemUiVisibilityChangeListener
+{
+ companion object
+ {
+ const val EXTRA_CONNECT_INFO = "connect_info"
+ private const val HIDE_UI_TIMEOUT_MS = 2000L
+ }
+
+ private lateinit var viewModel: StreamViewModel
+ private lateinit var binding: ActivityStreamBinding
+
+ private val uiVisibilityHandler = Handler()
+
+ override fun onCreate(savedInstanceState: Bundle?)
+ {
+ super.onCreate(savedInstanceState)
+
+ val connectInfo = intent.getParcelableExtra(EXTRA_CONNECT_INFO)
+ if(connectInfo == null)
+ {
+ finish()
+ return
+ }
+
+ viewModel = ViewModelProvider(this, viewModelFactory {
+ StreamViewModel(application, connectInfo)
+ })[StreamViewModel::class.java]
+
+ viewModel.input.observe(this)
+
+ binding = ActivityStreamBinding.inflate(layoutInflater)
+ setContentView(binding.root)
+ window.decorView.setOnSystemUiVisibilityChangeListener(this)
+
+ viewModel.onScreenControlsEnabled.observe(this, Observer {
+ if(binding.onScreenControlsSwitch.isChecked != it)
+ binding.onScreenControlsSwitch.isChecked = it
+ if(binding.onScreenControlsSwitch.isChecked)
+ binding.touchpadOnlySwitch.isChecked = false
+ })
+ binding.onScreenControlsSwitch.setOnCheckedChangeListener { _, isChecked ->
+ viewModel.setOnScreenControlsEnabled(isChecked)
+ showOverlay()
+ }
+
+ viewModel.touchpadOnlyEnabled.observe(this, Observer {
+ if(binding.touchpadOnlySwitch.isChecked != it)
+ binding.touchpadOnlySwitch.isChecked = it
+ if(binding.touchpadOnlySwitch.isChecked)
+ binding.onScreenControlsSwitch.isChecked = false
+ })
+ binding.touchpadOnlySwitch.setOnCheckedChangeListener { _, isChecked ->
+ viewModel.setTouchpadOnlyEnabled(isChecked)
+ showOverlay()
+ }
+
+ binding.displayModeToggle.addOnButtonCheckedListener { _, _, _ ->
+ adjustStreamViewAspect()
+ showOverlay()
+ }
+
+ //viewModel.session.attachToTextureView(textureView)
+ viewModel.session.attachToSurfaceView(binding.surfaceView)
+ viewModel.session.state.observe(this, Observer { this.stateChanged(it) })
+ adjustStreamViewAspect()
+
+ if(Preferences(this).rumbleEnabled)
+ {
+ val vibrator = getSystemService(VIBRATOR_SERVICE) as Vibrator
+ viewModel.session.rumbleState.observe(this, Observer {
+ val amplitude = min(255, (it.left.toInt() + it.right.toInt()) / 2)
+ vibrator.cancel()
+ if(amplitude == 0)
+ return@Observer
+ if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
+ vibrator.vibrate(VibrationEffect.createOneShot(1000, amplitude))
+ else
+ vibrator.vibrate(1000)
+ })
+ }
+ }
+
+ private val controlsDisposable = CompositeDisposable()
+
+ override fun onAttachFragment(fragment: Fragment)
+ {
+ super.onAttachFragment(fragment)
+ if(fragment is TouchControlsFragment)
+ {
+ fragment.controllerState
+ .subscribe { viewModel.input.touchControllerState = it }
+ .addTo(controlsDisposable)
+ fragment.onScreenControlsEnabled = viewModel.onScreenControlsEnabled
+ if(fragment is TouchpadOnlyFragment)
+ fragment.touchpadOnlyEnabled = viewModel.touchpadOnlyEnabled
+ }
+ }
+
+ override fun onResume()
+ {
+ super.onResume()
+ hideSystemUI()
+ viewModel.session.resume()
+ }
+
+ override fun onPause()
+ {
+ super.onPause()
+ viewModel.session.pause()
+ }
+
+ override fun onDestroy()
+ {
+ super.onDestroy()
+ controlsDisposable.dispose()
+ }
+
+ private fun reconnect()
+ {
+ viewModel.session.shutdown()
+ viewModel.session.resume()
+ }
+
+ private val hideSystemUIRunnable = Runnable { hideSystemUI() }
+
+ override fun onSystemUiVisibilityChange(visibility: Int)
+ {
+ if(visibility and View.SYSTEM_UI_FLAG_FULLSCREEN == 0)
+ showOverlay()
+ else
+ hideOverlay()
+ }
+
+ private fun showOverlay()
+ {
+ binding.overlay.isVisible = true
+ binding.overlay.animate()
+ .alpha(1.0f)
+ .setListener(object: AnimatorListenerAdapter()
+ {
+ override fun onAnimationEnd(animation: Animator)
+ {
+ binding.overlay.alpha = 1.0f
+ }
+ })
+ uiVisibilityHandler.removeCallbacks(hideSystemUIRunnable)
+ uiVisibilityHandler.postDelayed(hideSystemUIRunnable, HIDE_UI_TIMEOUT_MS)
+ }
+
+ private fun hideOverlay()
+ {
+ binding.overlay.animate()
+ .alpha(0.0f)
+ .setListener(object: AnimatorListenerAdapter()
+ {
+ override fun onAnimationEnd(animation: Animator)
+ {
+ binding.overlay.isGone = true
+ }
+ })
+ }
+
+ override fun onWindowFocusChanged(hasFocus: Boolean)
+ {
+ super.onWindowFocusChanged(hasFocus)
+ if(hasFocus)
+ hideSystemUI()
+ }
+
+ private fun hideSystemUI()
+ {
+ window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE
+ or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
+ or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
+ or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
+ or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
+ or View.SYSTEM_UI_FLAG_FULLSCREEN)
+ }
+
+ private var dialogContents: DialogContents? = null
+ private var dialog: AlertDialog? = null
+ set(value)
+ {
+ field = value
+ if(value == null)
+ dialogContents = null
+ }
+
+ private fun stateChanged(state: StreamState)
+ {
+ binding.progressBar.visibility = if(state == StreamStateConnecting) View.VISIBLE else View.GONE
+
+ when(state)
+ {
+ is StreamStateQuit ->
+ {
+ if(dialogContents != StreamQuitDialog)
+ {
+ if(state.reason.isError)
+ {
+ dialog?.dismiss()
+ val reasonStr = state.reasonString
+ val dialog = MaterialAlertDialogBuilder(this)
+ .setMessage(getString(R.string.alert_message_session_quit, state.reason.toString())
+ + (if(reasonStr != null) "\n$reasonStr" else ""))
+ .setPositiveButton(R.string.action_reconnect) { _, _ ->
+ dialog = null
+ reconnect()
+ }
+ .setOnCancelListener {
+ dialog = null
+ finish()
+ }
+ .setNegativeButton(R.string.action_quit_session) { _, _ ->
+ dialog = null
+ finish()
+ }
+ .create()
+ dialogContents = StreamQuitDialog
+ dialog.show()
+ }
+ else
+ finish()
+ }
+ }
+
+ is StreamStateCreateError ->
+ {
+ if(dialogContents != CreateErrorDialog)
+ {
+ dialog?.dismiss()
+ val dialog = MaterialAlertDialogBuilder(this)
+ .setMessage(getString(R.string.alert_message_session_create_error, state.error.errorCode.toString()))
+ .setOnDismissListener {
+ dialog = null
+ finish()
+ }
+ .setNegativeButton(R.string.action_quit_session) { _, _ -> }
+ .create()
+ dialogContents = CreateErrorDialog
+ dialog.show()
+ }
+ }
+
+ is StreamStateLoginPinRequest ->
+ {
+ if(dialogContents != PinRequestDialog)
+ {
+ dialog?.dismiss()
+
+ val view = layoutInflater.inflate(R.layout.dialog_login_pin, null)
+ val pinEditText = view.findViewById(R.id.pinEditText)
+
+ val dialog = MaterialAlertDialogBuilder(this)
+ .setMessage(
+ if(state.pinIncorrect)
+ R.string.alert_message_login_pin_request_incorrect
+ else
+ R.string.alert_message_login_pin_request)
+ .setView(view)
+ .setPositiveButton(R.string.action_login_pin_connect) { _, _ ->
+ dialog = null
+ viewModel.session.setLoginPin(pinEditText.text.toString())
+ }
+ .setOnCancelListener {
+ dialog = null
+ finish()
+ }
+ .setNegativeButton(R.string.action_quit_session) { _, _ ->
+ dialog = null
+ finish()
+ }
+ .create()
+ dialogContents = PinRequestDialog
+ dialog.show()
+ }
+ }
+
+ else -> {}
+ }
+ }
+
+ private fun adjustTextureViewAspect(textureView: TextureView)
+ {
+ val trans = TextureViewTransform(viewModel.session.connectInfo.videoProfile, textureView)
+ val resolution = trans.resolutionFor(TransformMode.fromButton(binding.displayModeToggle.checkedButtonId))
+ Matrix().also {
+ textureView.getTransform(it)
+ it.setScale(resolution.width / trans.viewWidth, resolution.height / trans.viewHeight)
+ it.postTranslate((trans.viewWidth - resolution.width) * 0.5f, (trans.viewHeight - resolution.height) * 0.5f)
+ textureView.setTransform(it)
+ }
+ }
+
+ private fun adjustSurfaceViewAspect()
+ {
+ val videoProfile = viewModel.session.connectInfo.videoProfile
+ binding.aspectRatioLayout.aspectRatio = videoProfile.width.toFloat() / videoProfile.height.toFloat()
+ binding.aspectRatioLayout.mode = TransformMode.fromButton(binding.displayModeToggle.checkedButtonId)
+ }
+
+ private fun adjustStreamViewAspect() = adjustSurfaceViewAspect()
+
+ override fun dispatchKeyEvent(event: KeyEvent) = viewModel.input.dispatchKeyEvent(event) || super.dispatchKeyEvent(event)
+ override fun onGenericMotionEvent(event: MotionEvent) = viewModel.input.onGenericMotionEvent(event) || super.onGenericMotionEvent(event)
+}
+
+enum class TransformMode
+{
+ FIT,
+ STRETCH,
+ ZOOM;
+
+ companion object
+ {
+ fun fromButton(displayModeButtonId: Int)
+ = when (displayModeButtonId)
+ {
+ R.id.display_mode_stretch_button -> STRETCH
+ R.id.display_mode_zoom_button -> ZOOM
+ else -> FIT
+ }
+ }
+}
+
+class TextureViewTransform(private val videoProfile: ConnectVideoProfile, private val textureView: TextureView)
+{
+ private val contentWidth : Float get() = videoProfile.width.toFloat()
+ private val contentHeight : Float get() = videoProfile.height.toFloat()
+ val viewWidth : Float get() = textureView.width.toFloat()
+ val viewHeight : Float get() = textureView.height.toFloat()
+ private val contentAspect : Float get() = contentHeight / contentWidth
+
+ fun resolutionFor(mode: TransformMode): Resolution
+ = when(mode)
+ {
+ TransformMode.STRETCH -> strechedResolution
+ TransformMode.ZOOM -> zoomedResolution
+ TransformMode.FIT -> normalResolution
+ }
+
+ private val strechedResolution get() = Resolution(viewWidth, viewHeight)
+
+ private val zoomedResolution get() =
+ if(viewHeight > viewWidth * contentAspect)
+ {
+ val zoomFactor = viewHeight / contentHeight
+ Resolution(contentWidth * zoomFactor, viewHeight)
+ }
+ else
+ {
+ val zoomFactor = viewWidth / contentWidth
+ Resolution(viewWidth, contentHeight * zoomFactor)
+ }
+
+ private val normalResolution get() =
+ if(viewHeight > viewWidth * contentAspect)
+ Resolution(viewWidth, viewWidth * contentAspect)
+ else
+ Resolution(viewHeight / contentAspect, viewHeight)
+}
+
+
+data class Resolution(val width: Float, val height: Float)
diff --git a/android/app/src/main/java/com/metallic/chiaki/stream/StreamViewModel.kt b/android/app/src/main/java/com/metallic/chiaki/stream/StreamViewModel.kt
new file mode 100644
index 0000000..df69378
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/stream/StreamViewModel.kt
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.stream
+
+import android.app.Application
+import android.content.Context
+import androidx.lifecycle.*
+import com.metallic.chiaki.common.LogManager
+import com.metallic.chiaki.session.StreamSession
+import com.metallic.chiaki.common.Preferences
+import com.metallic.chiaki.lib.*
+import com.metallic.chiaki.session.StreamInput
+
+class StreamViewModel(val application: Application, val connectInfo: ConnectInfo): ViewModel()
+{
+ val preferences = Preferences(application)
+ val logManager = LogManager(application)
+
+ private var _session: StreamSession? = null
+ val input = StreamInput(application, preferences)
+ val session = StreamSession(connectInfo, logManager, preferences.logVerbose, input)
+
+ private var _onScreenControlsEnabled = MutableLiveData(preferences.onScreenControlsEnabled)
+ val onScreenControlsEnabled: LiveData get() = _onScreenControlsEnabled
+
+ private var _touchpadOnlyEnabled = MutableLiveData(preferences.touchpadOnlyEnabled)
+ val touchpadOnlyEnabled: LiveData get() = _touchpadOnlyEnabled
+
+ override fun onCleared()
+ {
+ super.onCleared()
+ _session?.shutdown()
+ }
+
+ fun setOnScreenControlsEnabled(enabled: Boolean)
+ {
+ preferences.onScreenControlsEnabled = enabled
+ _onScreenControlsEnabled.value = enabled
+ }
+
+ fun setTouchpadOnlyEnabled(enabled: Boolean)
+ {
+ preferences.touchpadOnlyEnabled = enabled
+ _touchpadOnlyEnabled.value = enabled
+ }
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/touchcontrols/AnalogStickView.kt b/android/app/src/main/java/com/metallic/chiaki/touchcontrols/AnalogStickView.kt
new file mode 100644
index 0000000..0c0a310
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/touchcontrols/AnalogStickView.kt
@@ -0,0 +1,122 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.touchcontrols
+
+import android.content.Context
+import android.graphics.Canvas
+import android.graphics.Color
+import android.graphics.Paint
+import android.graphics.Rect
+import android.graphics.drawable.Drawable
+import android.util.AttributeSet
+import android.view.MotionEvent
+import android.view.View
+import com.metallic.chiaki.R
+import kotlin.math.abs
+
+class AnalogStickView @JvmOverloads constructor(
+ context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
+) : View(context, attrs, defStyleAttr)
+{
+ val radius: Float
+ private val handleRadius: Float
+ private val drawableBase: Drawable?
+ private val drawableHandle: Drawable?
+
+ var state = Vector(0f, 0f)
+ private set(value)
+ {
+ field = value
+ stateChangedCallback?.let { it(field) }
+ }
+
+ var stateChangedCallback: ((Vector) -> Unit)? = null
+
+ private val touchTracker = TouchTracker().also {
+ it.positionChangedCallback = this::updateState
+ }
+
+ private var center: Vector? = null
+
+ /**
+ * Same as state, but scaled to the circle
+ */
+ private var handlePosition: Vector = Vector(0f, 0f)
+
+ private val clipBoundsTmp = Rect()
+
+ init
+ {
+ context.theme.obtainStyledAttributes(attrs, R.styleable.AnalogStickView, 0, 0).apply {
+ radius = getDimension(R.styleable.AnalogStickView_radius, 0f)
+ handleRadius = getDimension(R.styleable.AnalogStickView_handleRadius, 0f)
+ drawableBase = getDrawable(R.styleable.AnalogStickView_drawableBase)
+ drawableHandle = getDrawable(R.styleable.AnalogStickView_drawableHandle)
+ recycle()
+ }
+ }
+
+ override fun onDraw(canvas: Canvas)
+ {
+ super.onDraw(canvas)
+
+ val center = center
+ if(center != null)
+ {
+ val circleRadius = radius + handleRadius
+ drawableBase?.setBounds((center.x - circleRadius).toInt(), (center.y - circleRadius).toInt(), (center.x + circleRadius).toInt(), (center.y + circleRadius).toInt())
+ drawableBase?.draw(canvas)
+
+ val handleX = center.x + handlePosition.x * radius
+ val handleY = center.y + handlePosition.y * radius
+ drawableHandle?.setBounds((handleX - handleRadius).toInt(), (handleY - handleRadius).toInt(), (handleX + handleRadius).toInt(),(handleY + handleRadius).toInt())
+ drawableHandle?.draw(canvas)
+ }
+ }
+
+ private fun updateState(position: Vector?)
+ {
+ if(radius <= 0f)
+ return
+
+ if(position == null)
+ {
+ center = null
+ state = Vector(0f, 0f)
+ handlePosition = Vector(0f, 0f)
+ invalidate()
+ return
+ }
+
+ val center: Vector = this.center ?: position
+ this.center = center
+
+ val dir = position - center
+ val length = dir.length
+ if(length > 0)
+ {
+ val strength = if(length > radius) 1.0f else length / radius
+ val dirNormalized = dir / length
+ handlePosition = dirNormalized * strength
+ val dirBoxNormalized =
+ if(abs(dirNormalized.x) > abs(dirNormalized.y))
+ dirNormalized / abs(dirNormalized.x)
+ else
+ dirNormalized / abs(dirNormalized.y)
+ state = dirBoxNormalized * strength
+ }
+ else
+ {
+ handlePosition = Vector(0f, 0f)
+ state = Vector(0f, 0f)
+ }
+
+ invalidate()
+ }
+
+ override fun onTouchEvent(event: MotionEvent): Boolean
+ {
+ touchTracker.touchEvent(event)
+ return true
+ }
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/touchcontrols/ButtonHaptics.kt b/android/app/src/main/java/com/metallic/chiaki/touchcontrols/ButtonHaptics.kt
new file mode 100644
index 0000000..909eab4
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/touchcontrols/ButtonHaptics.kt
@@ -0,0 +1,29 @@
+package com.metallic.chiaki.touchcontrols
+
+import android.content.Context
+import android.os.Build
+import android.os.VibrationEffect
+import android.os.Vibrator
+import androidx.appcompat.app.AppCompatActivity
+import com.metallic.chiaki.common.Preferences
+
+class ButtonHaptics(val context: Context)
+{
+ private val enabled = Preferences(context).buttonHapticEnabled
+
+ fun trigger(harder: Boolean = false)
+ {
+ if(!enabled)
+ return
+ val vibrator = context.getSystemService(AppCompatActivity.VIBRATOR_SERVICE) as Vibrator
+ if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
+ vibrator.vibrate(
+ if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
+ VibrationEffect.createPredefined(if(harder) VibrationEffect.EFFECT_CLICK else VibrationEffect.EFFECT_TICK)
+ else
+ VibrationEffect.createOneShot(10, if(harder) 200 else 100)
+ )
+ else
+ vibrator.vibrate(10)
+ }
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/touchcontrols/ButtonView.kt b/android/app/src/main/java/com/metallic/chiaki/touchcontrols/ButtonView.kt
new file mode 100644
index 0000000..66a9afe
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/touchcontrols/ButtonView.kt
@@ -0,0 +1,92 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.touchcontrols
+
+import android.content.Context
+import android.graphics.Canvas
+import android.graphics.drawable.Drawable
+import android.util.AttributeSet
+import android.util.Log
+import android.view.MotionEvent
+import android.view.View
+import android.view.ViewGroup
+import androidx.core.view.children
+import com.metallic.chiaki.R
+
+class ButtonView @JvmOverloads constructor(
+ context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
+) : View(context, attrs, defStyleAttr)
+{
+ private val haptics = ButtonHaptics(context)
+
+ var buttonPressed = false
+ private set(value)
+ {
+ val diff = field != value
+ field = value
+ if(diff)
+ {
+ if(value)
+ haptics.trigger()
+ invalidate()
+ buttonPressedCallback?.let { it(field) }
+ }
+ }
+
+ var buttonPressedCallback: ((Boolean) -> Unit)? = null
+
+ private val drawableIdle: Drawable?
+ private val drawablePressed: Drawable?
+
+ init
+ {
+ context.theme.obtainStyledAttributes(attrs, R.styleable.ButtonView, 0, 0).apply {
+ drawableIdle = getDrawable(R.styleable.ButtonView_drawableIdle)
+ drawablePressed = getDrawable(R.styleable.ButtonView_drawablePressed)
+ recycle()
+ }
+
+ isClickable = true
+ }
+
+ override fun onDraw(canvas: Canvas)
+ {
+ super.onDraw(canvas)
+ val drawable = if(buttonPressed) drawablePressed else drawableIdle
+ drawable?.setBounds(paddingLeft, paddingTop, width - paddingRight, height - paddingBottom)
+ drawable?.draw(canvas)
+ }
+
+ /**
+ * If this button overlaps with others in the same layout,
+ * let the one whose center is closest to the touch handle it.
+ */
+ private fun bestFittingTouchView(x: Float, y: Float): View
+ {
+ val loc = locationOnScreen + Vector(x, y)
+ return (parent as? ViewGroup)?.children?.filter {
+ it is ButtonView
+ }?.filter {
+ val pos = it.locationOnScreen
+ loc.x >= pos.x && loc.x < pos.x + it.width && loc.y >= pos.y && loc.y < pos.y + it.height
+ }?.sortedBy {
+ (loc - (it.locationOnScreen + Vector(it.width.toFloat(), it.height.toFloat()) * 0.5f)).lengthSq
+ }?.firstOrNull() ?: this
+ }
+
+ override fun onTouchEvent(event: MotionEvent): Boolean
+ {
+ when(event.actionMasked)
+ {
+ MotionEvent.ACTION_DOWN, MotionEvent.ACTION_POINTER_DOWN -> {
+ if(bestFittingTouchView(event.getX(event.actionIndex), event.getY(event.actionIndex)) != this)
+ return false
+ buttonPressed = true
+ }
+ MotionEvent.ACTION_UP, MotionEvent.ACTION_POINTER_UP -> {
+ buttonPressed = false
+ }
+ }
+ return true
+ }
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/touchcontrols/ControlsBackgroundView.kt b/android/app/src/main/java/com/metallic/chiaki/touchcontrols/ControlsBackgroundView.kt
new file mode 100644
index 0000000..3edbd86
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/touchcontrols/ControlsBackgroundView.kt
@@ -0,0 +1,15 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.touchcontrols
+
+import android.content.Context
+import android.util.AttributeSet
+import android.view.MotionEvent
+import android.view.View
+
+class ControlsBackgroundView @JvmOverloads constructor(
+ context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
+) : View(context, attrs, defStyleAttr)
+{
+ override fun onTouchEvent(event: MotionEvent) = true
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/touchcontrols/DPadView.kt b/android/app/src/main/java/com/metallic/chiaki/touchcontrols/DPadView.kt
new file mode 100644
index 0000000..9c359d8
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/touchcontrols/DPadView.kt
@@ -0,0 +1,132 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.touchcontrols
+
+import android.content.Context
+import android.graphics.Canvas
+import android.util.AttributeSet
+import android.view.MotionEvent
+import android.view.View
+import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat
+import com.metallic.chiaki.R
+import kotlin.math.PI
+import kotlin.math.atan2
+
+class DPadView @JvmOverloads constructor(
+ context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
+) : View(context, attrs, defStyleAttr)
+{
+ private val haptics = ButtonHaptics(context)
+
+ enum class Direction {
+ LEFT,
+ RIGHT,
+ UP,
+ DOWN,
+ LEFT_UP,
+ RIGHT_UP,
+ LEFT_DOWN,
+ RIGHT_DOWN;
+
+ val isDiagonal get() = when(this) {
+ LEFT_UP, RIGHT_UP, LEFT_DOWN, RIGHT_DOWN -> true
+ else -> false
+ }
+ }
+
+ var state: Direction? = null
+ private set
+
+ var stateChangeCallback: ((Direction?) -> Unit)? = null
+
+ /**
+ * Radius (as a fraction of the entire DPad Radius)
+ * to be used as a deadzone in the center on move events
+ */
+ private val deadzoneRadius = 0.3f
+
+ private val dpadIdleDrawable = VectorDrawableCompat.create(resources, R.drawable.control_dpad_idle, null)
+ private val dpadLeftDrawable = VectorDrawableCompat.create(resources, R.drawable.control_dpad_left, null)
+ private val dpadLeftUpDrawable = VectorDrawableCompat.create(resources, R.drawable.control_dpad_left_up, null)
+
+ private val touchTracker = TouchTracker().also {
+ it.positionChangedCallback = this::updateState
+ }
+
+ override fun onDraw(canvas: Canvas)
+ {
+ super.onDraw(canvas)
+
+ val state = state
+ val drawable: VectorDrawableCompat?
+ if(state != null)
+ {
+ drawable = if(state.isDiagonal) dpadLeftUpDrawable else dpadLeftDrawable
+ when(state)
+ {
+ Direction.UP, Direction.RIGHT_UP -> canvas.rotate(90f, width.toFloat() * 0.5f, height.toFloat() * 0.5f)
+ Direction.DOWN, Direction.LEFT_DOWN -> canvas.rotate(90f*3f, width.toFloat() * 0.5f, height.toFloat() * 0.5f)
+ Direction.LEFT, Direction.LEFT_UP -> {}
+ Direction.RIGHT, Direction.RIGHT_DOWN -> canvas.rotate(90f*2f, width.toFloat() * 0.5f, height.toFloat() * 0.5f)
+ }
+ }
+ else
+ drawable = dpadIdleDrawable
+
+ drawable?.setBounds(paddingLeft, paddingTop, width - paddingRight, height - paddingBottom)
+ //drawable?.alpha = 127
+ drawable?.draw(canvas)
+ }
+
+ private fun directionForPosition(position: Vector): Direction
+ {
+ val dir = (position / Vector(width.toFloat(), height.toFloat()) - 0.5f) * 2.0f
+ val angleSection = PI.toFloat() * 2.0f / 8.0f
+ val angle = atan2(dir.x, dir.y) + PI + angleSection * 0.5f
+ return when
+ {
+ angle < 1.0f * angleSection -> Direction.UP
+ angle < 2.0f * angleSection -> Direction.LEFT_UP
+ angle < 3.0f * angleSection -> Direction.LEFT
+ angle < 4.0f * angleSection -> Direction.LEFT_DOWN
+ angle < 5.0f * angleSection -> Direction.DOWN
+ angle < 6.0f * angleSection -> Direction.RIGHT_DOWN
+ angle < 7.0f * angleSection -> Direction.RIGHT
+ angle < 8.0f * angleSection -> Direction.RIGHT_UP
+ else -> Direction.UP
+ }
+ }
+
+ private fun updateState(position: Vector?)
+ {
+ val newState =
+ if(position == null)
+ null
+ else
+ {
+ val xFrac = 2.0f * (position.x / width.toFloat() - 0.5f)
+ val yFrac = 2.0f * (position.y / height.toFloat() - 0.5f)
+ val radiusSq = xFrac * xFrac + yFrac * yFrac
+ if(radiusSq < deadzoneRadius * deadzoneRadius && state != null)
+ state
+ else
+ directionForPosition(position)
+ }
+
+ if(state != newState)
+ {
+ if(newState != null)
+ haptics.trigger()
+ state = newState
+ invalidate()
+ stateChangeCallback?.let { it(state) }
+ }
+ }
+
+ override fun onTouchEvent(event: MotionEvent): Boolean
+ {
+ touchTracker.touchEvent(event)
+ return true
+ }
+
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/touchcontrols/TouchControlsFragment.kt b/android/app/src/main/java/com/metallic/chiaki/touchcontrols/TouchControlsFragment.kt
new file mode 100644
index 0000000..b1ecfbe
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/touchcontrols/TouchControlsFragment.kt
@@ -0,0 +1,127 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.touchcontrols
+
+import android.os.Bundle
+import android.view.LayoutInflater
+import android.view.View
+import android.view.ViewGroup
+import androidx.fragment.app.Fragment
+import androidx.lifecycle.LiveData
+import androidx.lifecycle.Observer
+import com.metallic.chiaki.databinding.FragmentControlsBinding
+import com.metallic.chiaki.lib.ControllerState
+import io.reactivex.Observable
+import io.reactivex.rxkotlin.Observables.combineLatest
+import io.reactivex.subjects.BehaviorSubject
+import io.reactivex.subjects.Subject
+
+abstract class TouchControlsFragment : Fragment()
+{
+ protected var ownControllerState = ControllerState()
+ set(value)
+ {
+ val diff = field != value
+ field = value
+ if(diff)
+ ownControllerStateSubject.onNext(ownControllerState)
+ }
+
+ protected val ownControllerStateSubject: Subject
+ = BehaviorSubject.create().also { it.onNext(ownControllerState) }
+
+ // to delay attaching to the touchpadView until it's available
+ protected val controllerStateProxy: Subject>
+ = BehaviorSubject.create>().also { it.onNext(ownControllerStateSubject) }
+ val controllerState: Observable get() =
+ controllerStateProxy.flatMap { it }
+
+ var onScreenControlsEnabled: LiveData? = null
+}
+
+class DefaultTouchControlsFragment : TouchControlsFragment()
+{
+ private var _binding: FragmentControlsBinding? = null
+ private val binding get() = _binding!!
+
+ override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View =
+ FragmentControlsBinding.inflate(inflater, container, false).let {
+ _binding = it
+ controllerStateProxy.onNext(
+ combineLatest(ownControllerStateSubject, binding.touchpadView.controllerState) { a, b -> a or b }
+ )
+ it.root
+ }
+
+ override fun onViewCreated(view: View, savedInstanceState: Bundle?)
+ {
+ super.onViewCreated(view, savedInstanceState)
+ binding.dpadView.stateChangeCallback = this::dpadStateChanged
+ binding.crossButtonView.buttonPressedCallback = buttonStateChanged(ControllerState.BUTTON_CROSS)
+ binding.moonButtonView.buttonPressedCallback = buttonStateChanged(ControllerState.BUTTON_MOON)
+ binding.pyramidButtonView.buttonPressedCallback = buttonStateChanged(ControllerState.BUTTON_PYRAMID)
+ binding.boxButtonView.buttonPressedCallback = buttonStateChanged(ControllerState.BUTTON_BOX)
+ binding.l1ButtonView.buttonPressedCallback = buttonStateChanged(ControllerState.BUTTON_L1)
+ binding.r1ButtonView.buttonPressedCallback = buttonStateChanged(ControllerState.BUTTON_R1)
+ binding.l3ButtonView.buttonPressedCallback = buttonStateChanged(ControllerState.BUTTON_L3)
+ binding.r3ButtonView.buttonPressedCallback = buttonStateChanged(ControllerState.BUTTON_R3)
+ binding.optionsButtonView.buttonPressedCallback = buttonStateChanged(ControllerState.BUTTON_OPTIONS)
+ binding.shareButtonView.buttonPressedCallback = buttonStateChanged(ControllerState.BUTTON_SHARE)
+ binding.psButtonView.buttonPressedCallback = buttonStateChanged(ControllerState.BUTTON_PS)
+
+ binding.l2ButtonView.buttonPressedCallback = { ownControllerState = ownControllerState.copy().apply { l2State = if(it) 255U else 0U } }
+ binding.r2ButtonView.buttonPressedCallback = { ownControllerState = ownControllerState.copy().apply { r2State = if(it) 255U else 0U } }
+
+ val quantizeStick = { f: Float ->
+ (Short.MAX_VALUE * f).toInt().toShort()
+ }
+
+ binding.leftAnalogStickView.stateChangedCallback = { ownControllerState = ownControllerState.copy().apply {
+ leftX = quantizeStick(it.x)
+ leftY = quantizeStick(it.y)
+ }}
+
+ binding.rightAnalogStickView.stateChangedCallback = { ownControllerState = ownControllerState.copy().apply {
+ rightX = quantizeStick(it.x)
+ rightY = quantizeStick(it.y)
+ }}
+
+ onScreenControlsEnabled?.observe(viewLifecycleOwner, Observer {
+ view.visibility = if(it) View.VISIBLE else View.GONE
+ })
+ }
+
+ private fun dpadStateChanged(direction: DPadView.Direction?)
+ {
+ ownControllerState = ownControllerState.copy().apply {
+ buttons = ((buttons
+ and ControllerState.BUTTON_DPAD_LEFT.inv()
+ and ControllerState.BUTTON_DPAD_RIGHT.inv()
+ and ControllerState.BUTTON_DPAD_UP.inv()
+ and ControllerState.BUTTON_DPAD_DOWN.inv())
+ or when(direction)
+ {
+ DPadView.Direction.UP -> ControllerState.BUTTON_DPAD_UP
+ DPadView.Direction.DOWN -> ControllerState.BUTTON_DPAD_DOWN
+ DPadView.Direction.LEFT -> ControllerState.BUTTON_DPAD_LEFT
+ DPadView.Direction.RIGHT -> ControllerState.BUTTON_DPAD_RIGHT
+ DPadView.Direction.LEFT_UP -> ControllerState.BUTTON_DPAD_LEFT or ControllerState.BUTTON_DPAD_UP
+ DPadView.Direction.LEFT_DOWN -> ControllerState.BUTTON_DPAD_LEFT or ControllerState.BUTTON_DPAD_DOWN
+ DPadView.Direction.RIGHT_UP -> ControllerState.BUTTON_DPAD_RIGHT or ControllerState.BUTTON_DPAD_UP
+ DPadView.Direction.RIGHT_DOWN -> ControllerState.BUTTON_DPAD_RIGHT or ControllerState.BUTTON_DPAD_DOWN
+ null -> 0U
+ })
+ }
+ }
+
+ private fun buttonStateChanged(buttonMask: UInt) = { pressed: Boolean ->
+ ownControllerState = ownControllerState.copy().apply {
+ buttons =
+ if(pressed)
+ buttons or buttonMask
+ else
+ buttons and buttonMask.inv()
+
+ }
+ }
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/touchcontrols/TouchTracker.kt b/android/app/src/main/java/com/metallic/chiaki/touchcontrols/TouchTracker.kt
new file mode 100644
index 0000000..a1ead71
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/touchcontrols/TouchTracker.kt
@@ -0,0 +1,54 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.touchcontrols
+
+import android.view.MotionEvent
+
+class TouchTracker
+{
+ var currentPosition: Vector? = null
+ private set(value)
+ {
+ field = value
+ positionChangedCallback?.let { it(field) }
+ }
+
+ var positionChangedCallback: ((Vector?) -> Unit)? = null
+
+ private var pointerId: Int? = null
+
+ fun touchEvent(event: MotionEvent)
+ {
+ when(event.actionMasked)
+ {
+ MotionEvent.ACTION_DOWN, MotionEvent.ACTION_POINTER_DOWN ->
+ {
+ if(pointerId == null)
+ {
+ pointerId = event.getPointerId(event.actionIndex)
+ currentPosition = Vector(event.getX(event.actionIndex), event.getY(event.actionIndex))
+ }
+ }
+
+ MotionEvent.ACTION_UP, MotionEvent.ACTION_POINTER_UP ->
+ {
+ if(event.getPointerId(event.actionIndex) == pointerId)
+ {
+ pointerId = null
+ currentPosition = null
+ }
+ }
+
+ MotionEvent.ACTION_MOVE ->
+ {
+ val pointerId = pointerId
+ if(pointerId != null)
+ {
+ val pointerIndex = event.findPointerIndex(pointerId)
+ if(pointerIndex >= 0)
+ currentPosition = Vector(event.getX(pointerIndex), event.getY(pointerIndex))
+ }
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/touchcontrols/TouchpadOnlyFragment.kt b/android/app/src/main/java/com/metallic/chiaki/touchcontrols/TouchpadOnlyFragment.kt
new file mode 100644
index 0000000..8910ef7
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/touchcontrols/TouchpadOnlyFragment.kt
@@ -0,0 +1,47 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.touchcontrols
+
+import android.os.Bundle
+import android.view.LayoutInflater
+import android.view.View
+import android.view.ViewGroup
+import androidx.lifecycle.LiveData
+import androidx.lifecycle.Observer
+import com.metallic.chiaki.databinding.FragmentTouchpadOnlyBinding
+import io.reactivex.rxkotlin.Observables.combineLatest
+
+class TouchpadOnlyFragment : TouchControlsFragment()
+{
+ var touchpadOnlyEnabled: LiveData? = null
+
+ private var _binding: FragmentTouchpadOnlyBinding? = null
+ private val binding get() = _binding!!
+
+ override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View =
+ FragmentTouchpadOnlyBinding.inflate(inflater, container, false).let {
+ _binding = it
+ controllerStateProxy.onNext(
+ combineLatest(ownControllerStateSubject, binding.touchpadView.controllerState) { a, b -> a or b }
+ )
+ it.root
+ }
+
+ override fun onViewCreated(view: View, savedInstanceState: Bundle?)
+ {
+ super.onViewCreated(view, savedInstanceState)
+ touchpadOnlyEnabled?.observe(viewLifecycleOwner, Observer {
+ view.visibility = if(it) View.VISIBLE else View.GONE
+ })
+ }
+
+ private fun buttonStateChanged(buttonMask: UInt) = { pressed: Boolean ->
+ ownControllerState = ownControllerState.copy().apply {
+ buttons =
+ if(pressed)
+ buttons or buttonMask
+ else
+ buttons and buttonMask.inv()
+ }
+ }
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/touchcontrols/TouchpadView.kt b/android/app/src/main/java/com/metallic/chiaki/touchcontrols/TouchpadView.kt
new file mode 100644
index 0000000..a7e7b85
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/touchcontrols/TouchpadView.kt
@@ -0,0 +1,168 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.touchcontrols
+
+import android.content.Context
+import android.graphics.Canvas
+import android.graphics.drawable.Drawable
+import android.util.AttributeSet
+import android.view.MotionEvent
+import android.view.View
+import com.metallic.chiaki.R
+import com.metallic.chiaki.lib.ControllerState
+import io.reactivex.Observable
+import io.reactivex.subjects.BehaviorSubject
+import io.reactivex.subjects.Subject
+import kotlin.math.max
+
+class TouchpadView @JvmOverloads constructor(
+ context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
+) : View(context, attrs, defStyleAttr)
+{
+ companion object
+ {
+ private const val BUTTON_PRESS_MAX_MOVE_DIST_DP = 32.0f
+ private const val SHORT_BUTTON_PRESS_DURATION_MS = 200L
+ private const val BUTTON_HOLD_DELAY_MS = 500L
+ }
+
+ private val haptics = ButtonHaptics(context)
+
+ private val drawableIdle: Drawable?
+ private val drawablePressed: Drawable?
+
+ private val state: ControllerState = ControllerState()
+
+ inner class Touch(
+ val stateId: UByte,
+ private val startX: Float,
+ private val startY: Float)
+ {
+ var lifted = false // will be true but touch still in list when only relevant for short touch
+ private var maxDist: Float = 0.0f
+ val moveInsignificant: Boolean get() = maxDist < BUTTON_PRESS_MAX_MOVE_DIST_DP
+
+ fun onMove(x: Float, y: Float)
+ {
+ val d = (Vector(x, y) - Vector(startX, startY)).length / resources.displayMetrics.density
+ maxDist = max(d, maxDist)
+ }
+
+ val startButtonHoldRunnable = Runnable {
+ if(!moveInsignificant || buttonHeld)
+ return@Runnable
+ haptics.trigger(true)
+ state.buttons = state.buttons or ControllerState.BUTTON_TOUCHPAD
+ buttonHeld = true
+ triggerStateChanged()
+ }
+ }
+ private val pointerTouches = mutableMapOf()
+
+ private val stateSubject: Subject
+ = BehaviorSubject.create().also { it.onNext(state) }
+ val controllerState: Observable get() = stateSubject
+
+ private var shortPressingTouches = listOf()
+ private val shortButtonPressLiftRunnable = Runnable {
+ state.buttons = state.buttons and ControllerState.BUTTON_TOUCHPAD.inv()
+ shortPressingTouches.forEach {
+ state.stopTouch(it.stateId)
+ }
+ shortPressingTouches = listOf()
+ triggerStateChanged()
+ }
+
+ private var buttonHeld = false
+
+ init
+ {
+ context.theme.obtainStyledAttributes(attrs, R.styleable.TouchpadView, 0, 0).apply {
+ drawableIdle = getDrawable(R.styleable.TouchpadView_drawableIdle)
+ drawablePressed = getDrawable(R.styleable.TouchpadView_drawablePressed)
+ recycle()
+ }
+ isClickable = true
+ }
+
+ override fun onDraw(canvas: Canvas)
+ {
+ super.onDraw(canvas)
+ if(pointerTouches.values.find { !it.lifted } == null)
+ return
+ val drawable = if(state.buttons and ControllerState.BUTTON_TOUCHPAD != 0U) drawablePressed else drawableIdle
+ drawable?.setBounds(paddingLeft, paddingTop, width - paddingRight, height - paddingBottom)
+ drawable?.draw(canvas)
+ }
+
+ private fun touchX(event: MotionEvent, index: Int): UShort =
+ maxOf(0U.toUShort(), minOf((ControllerState.TOUCHPAD_WIDTH - 1u).toUShort(),
+ (ControllerState.TOUCHPAD_WIDTH.toFloat() * event.getX(index) / width.toFloat()).toUInt().toUShort()))
+
+ private fun touchY(event: MotionEvent, index: Int): UShort =
+ maxOf(0U.toUShort(), minOf((ControllerState.TOUCHPAD_HEIGHT - 1u).toUShort(),
+ (ControllerState.TOUCHPAD_HEIGHT.toFloat() * event.getY(index) / height.toFloat()).toUInt().toUShort()))
+
+ override fun onTouchEvent(event: MotionEvent): Boolean
+ {
+ when(event.actionMasked)
+ {
+ MotionEvent.ACTION_DOWN, MotionEvent.ACTION_POINTER_DOWN -> {
+ state.startTouch(touchX(event, event.actionIndex), touchY(event, event.actionIndex))?.let {
+ haptics.trigger()
+ val touch = Touch(it, event.getX(event.actionIndex), event.getY(event.actionIndex))
+ pointerTouches[event.getPointerId(event.actionIndex)] = touch
+ if(!buttonHeld)
+ postDelayed(touch.startButtonHoldRunnable, BUTTON_HOLD_DELAY_MS)
+ triggerStateChanged()
+ }
+ }
+ MotionEvent.ACTION_UP, MotionEvent.ACTION_POINTER_UP -> {
+ pointerTouches.remove(event.getPointerId(event.actionIndex))?.let {
+ removeCallbacks(it.startButtonHoldRunnable)
+ when
+ {
+ buttonHeld ->
+ {
+ buttonHeld = false
+ state.buttons = state.buttons and ControllerState.BUTTON_TOUCHPAD.inv()
+ state.stopTouch(it.stateId)
+ }
+ it.moveInsignificant -> triggerShortButtonPress(it)
+ else -> state.stopTouch(it.stateId)
+ }
+ triggerStateChanged()
+ }
+ }
+ MotionEvent.ACTION_MOVE -> {
+ val changed = pointerTouches.entries.fold(false) { acc, it ->
+ val index = event.findPointerIndex(it.key)
+ if(index < 0)
+ acc
+ else
+ {
+ it.value.onMove(event.getX(event.actionIndex), event.getY(event.actionIndex))
+ acc || state.setTouchPos(it.value.stateId, touchX(event, index), touchY(event, index))
+ }
+ }
+ if(changed)
+ triggerStateChanged()
+ }
+ }
+ return true
+ }
+
+ private fun triggerShortButtonPress(touch: Touch)
+ {
+ shortPressingTouches = shortPressingTouches + listOf(touch)
+ removeCallbacks(shortButtonPressLiftRunnable)
+ state.buttons = state.buttons or ControllerState.BUTTON_TOUCHPAD
+ postDelayed(shortButtonPressLiftRunnable, SHORT_BUTTON_PRESS_DURATION_MS)
+ }
+
+ private fun triggerStateChanged()
+ {
+ invalidate()
+ stateSubject.onNext(state)
+ }
+}
\ No newline at end of file
diff --git a/android/app/src/main/java/com/metallic/chiaki/touchcontrols/Vector.kt b/android/app/src/main/java/com/metallic/chiaki/touchcontrols/Vector.kt
new file mode 100644
index 0000000..208c41a
--- /dev/null
+++ b/android/app/src/main/java/com/metallic/chiaki/touchcontrols/Vector.kt
@@ -0,0 +1,28 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+package com.metallic.chiaki.touchcontrols
+
+import android.view.View
+import kotlin.math.sqrt
+
+data class Vector(val x: Float, val y: Float)
+{
+ operator fun plus(o: Vector) = Vector(x + o.x, y + o.y)
+ operator fun minus(o: Vector) = Vector(x - o.x, y - o.y)
+ operator fun plus(s: Float) = Vector(x + s, y + s)
+ operator fun minus(s: Float) = Vector(x - s, y - s)
+ operator fun times(s: Float) = Vector(x * s, y * s)
+ operator fun div(s: Float) = this * (1f / s)
+ operator fun times(o: Vector) = Vector(x * o.x, y * o.y)
+ operator fun div(o: Vector) = this * Vector(1.0f / o.x, 1.0f / o.y)
+
+ val lengthSq get() = x*x + y*y
+ val length get() = sqrt(lengthSq)
+ val normalized get() = this / length
+}
+
+val View.locationOnScreen: Vector get() {
+ val v = intArrayOf(0, 0)
+ this.getLocationOnScreen(v)
+ return Vector(v[0].toFloat(), v[1].toFloat())
+}
\ No newline at end of file
diff --git a/android/app/src/main/res/color/mage_text_input_box_stroke.xml b/android/app/src/main/res/color/mage_text_input_box_stroke.xml
new file mode 100644
index 0000000..31fab55
--- /dev/null
+++ b/android/app/src/main/res/color/mage_text_input_box_stroke.xml
@@ -0,0 +1,7 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/android/app/src/main/res/color/stream_material_button_icon_tint.xml b/android/app/src/main/res/color/stream_material_button_icon_tint.xml
new file mode 100644
index 0000000..fbfbf5c
--- /dev/null
+++ b/android/app/src/main/res/color/stream_material_button_icon_tint.xml
@@ -0,0 +1,5 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/android/app/src/main/res/drawable/avd_add_to_close.xml b/android/app/src/main/res/drawable/avd_add_to_close.xml
new file mode 100644
index 0000000..1616538
--- /dev/null
+++ b/android/app/src/main/res/drawable/avd_add_to_close.xml
@@ -0,0 +1,53 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/android/app/src/main/res/drawable/avd_close_to_add.xml b/android/app/src/main/res/drawable/avd_close_to_add.xml
new file mode 100644
index 0000000..9831e19
--- /dev/null
+++ b/android/app/src/main/res/drawable/avd_close_to_add.xml
@@ -0,0 +1,51 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/android/app/src/main/res/drawable/control_analog_stick_base.xml b/android/app/src/main/res/drawable/control_analog_stick_base.xml
new file mode 100644
index 0000000..bbe2070
--- /dev/null
+++ b/android/app/src/main/res/drawable/control_analog_stick_base.xml
@@ -0,0 +1,15 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/control_analog_stick_handle.xml b/android/app/src/main/res/drawable/control_analog_stick_handle.xml
new file mode 100644
index 0000000..109df6a
--- /dev/null
+++ b/android/app/src/main/res/drawable/control_analog_stick_handle.xml
@@ -0,0 +1,15 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/control_button_box.xml b/android/app/src/main/res/drawable/control_button_box.xml
new file mode 100644
index 0000000..b642683
--- /dev/null
+++ b/android/app/src/main/res/drawable/control_button_box.xml
@@ -0,0 +1,13 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/control_button_box_pressed.xml b/android/app/src/main/res/drawable/control_button_box_pressed.xml
new file mode 100644
index 0000000..bd8c5ab
--- /dev/null
+++ b/android/app/src/main/res/drawable/control_button_box_pressed.xml
@@ -0,0 +1,13 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/control_button_cross.xml b/android/app/src/main/res/drawable/control_button_cross.xml
new file mode 100644
index 0000000..58b4c53
--- /dev/null
+++ b/android/app/src/main/res/drawable/control_button_cross.xml
@@ -0,0 +1,13 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/control_button_cross_pressed.xml b/android/app/src/main/res/drawable/control_button_cross_pressed.xml
new file mode 100644
index 0000000..e55249c
--- /dev/null
+++ b/android/app/src/main/res/drawable/control_button_cross_pressed.xml
@@ -0,0 +1,13 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/control_button_home.xml b/android/app/src/main/res/drawable/control_button_home.xml
new file mode 100644
index 0000000..e67ed8b
--- /dev/null
+++ b/android/app/src/main/res/drawable/control_button_home.xml
@@ -0,0 +1,10 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/control_button_home_pressed.xml b/android/app/src/main/res/drawable/control_button_home_pressed.xml
new file mode 100644
index 0000000..c525e35
--- /dev/null
+++ b/android/app/src/main/res/drawable/control_button_home_pressed.xml
@@ -0,0 +1,10 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/control_button_l1.xml b/android/app/src/main/res/drawable/control_button_l1.xml
new file mode 100644
index 0000000..17f9594
--- /dev/null
+++ b/android/app/src/main/res/drawable/control_button_l1.xml
@@ -0,0 +1,12 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/control_button_l1_pressed.xml b/android/app/src/main/res/drawable/control_button_l1_pressed.xml
new file mode 100644
index 0000000..0267190
--- /dev/null
+++ b/android/app/src/main/res/drawable/control_button_l1_pressed.xml
@@ -0,0 +1,12 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/control_button_l2.xml b/android/app/src/main/res/drawable/control_button_l2.xml
new file mode 100644
index 0000000..8e75f40
--- /dev/null
+++ b/android/app/src/main/res/drawable/control_button_l2.xml
@@ -0,0 +1,12 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/control_button_l2_pressed.xml b/android/app/src/main/res/drawable/control_button_l2_pressed.xml
new file mode 100644
index 0000000..b120891
--- /dev/null
+++ b/android/app/src/main/res/drawable/control_button_l2_pressed.xml
@@ -0,0 +1,12 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/control_button_l3.xml b/android/app/src/main/res/drawable/control_button_l3.xml
new file mode 100644
index 0000000..34c3382
--- /dev/null
+++ b/android/app/src/main/res/drawable/control_button_l3.xml
@@ -0,0 +1,12 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/control_button_l3_pressed.xml b/android/app/src/main/res/drawable/control_button_l3_pressed.xml
new file mode 100644
index 0000000..43f239a
--- /dev/null
+++ b/android/app/src/main/res/drawable/control_button_l3_pressed.xml
@@ -0,0 +1,12 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/control_button_moon.xml b/android/app/src/main/res/drawable/control_button_moon.xml
new file mode 100644
index 0000000..efc7434
--- /dev/null
+++ b/android/app/src/main/res/drawable/control_button_moon.xml
@@ -0,0 +1,13 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/control_button_moon_pressed.xml b/android/app/src/main/res/drawable/control_button_moon_pressed.xml
new file mode 100644
index 0000000..4f8c4b3
--- /dev/null
+++ b/android/app/src/main/res/drawable/control_button_moon_pressed.xml
@@ -0,0 +1,13 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/control_button_options.xml b/android/app/src/main/res/drawable/control_button_options.xml
new file mode 100644
index 0000000..116ca88
--- /dev/null
+++ b/android/app/src/main/res/drawable/control_button_options.xml
@@ -0,0 +1,12 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/control_button_options_pressed.xml b/android/app/src/main/res/drawable/control_button_options_pressed.xml
new file mode 100644
index 0000000..35d224f
--- /dev/null
+++ b/android/app/src/main/res/drawable/control_button_options_pressed.xml
@@ -0,0 +1,12 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/control_button_pyramid.xml b/android/app/src/main/res/drawable/control_button_pyramid.xml
new file mode 100644
index 0000000..4a856a0
--- /dev/null
+++ b/android/app/src/main/res/drawable/control_button_pyramid.xml
@@ -0,0 +1,13 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/control_button_pyramid_pressed.xml b/android/app/src/main/res/drawable/control_button_pyramid_pressed.xml
new file mode 100644
index 0000000..2ebfe4a
--- /dev/null
+++ b/android/app/src/main/res/drawable/control_button_pyramid_pressed.xml
@@ -0,0 +1,13 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/control_button_r1.xml b/android/app/src/main/res/drawable/control_button_r1.xml
new file mode 100644
index 0000000..d614419
--- /dev/null
+++ b/android/app/src/main/res/drawable/control_button_r1.xml
@@ -0,0 +1,12 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/control_button_r1_pressed.xml b/android/app/src/main/res/drawable/control_button_r1_pressed.xml
new file mode 100644
index 0000000..8d51983
--- /dev/null
+++ b/android/app/src/main/res/drawable/control_button_r1_pressed.xml
@@ -0,0 +1,12 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/control_button_r2.xml b/android/app/src/main/res/drawable/control_button_r2.xml
new file mode 100644
index 0000000..dd16994
--- /dev/null
+++ b/android/app/src/main/res/drawable/control_button_r2.xml
@@ -0,0 +1,12 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/control_button_r2_pressed.xml b/android/app/src/main/res/drawable/control_button_r2_pressed.xml
new file mode 100644
index 0000000..7cc84b1
--- /dev/null
+++ b/android/app/src/main/res/drawable/control_button_r2_pressed.xml
@@ -0,0 +1,12 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/control_button_r3.xml b/android/app/src/main/res/drawable/control_button_r3.xml
new file mode 100644
index 0000000..0f2bcfd
--- /dev/null
+++ b/android/app/src/main/res/drawable/control_button_r3.xml
@@ -0,0 +1,12 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/control_button_r3_pressed.xml b/android/app/src/main/res/drawable/control_button_r3_pressed.xml
new file mode 100644
index 0000000..08635e8
--- /dev/null
+++ b/android/app/src/main/res/drawable/control_button_r3_pressed.xml
@@ -0,0 +1,12 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/control_button_share.xml b/android/app/src/main/res/drawable/control_button_share.xml
new file mode 100644
index 0000000..bb8d30e
--- /dev/null
+++ b/android/app/src/main/res/drawable/control_button_share.xml
@@ -0,0 +1,12 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/control_button_share_pressed.xml b/android/app/src/main/res/drawable/control_button_share_pressed.xml
new file mode 100644
index 0000000..5b9a5ee
--- /dev/null
+++ b/android/app/src/main/res/drawable/control_button_share_pressed.xml
@@ -0,0 +1,12 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/control_dpad_idle.xml b/android/app/src/main/res/drawable/control_dpad_idle.xml
new file mode 100644
index 0000000..6f44fb5
--- /dev/null
+++ b/android/app/src/main/res/drawable/control_dpad_idle.xml
@@ -0,0 +1,34 @@
+
+
+
+
+
+
diff --git a/android/app/src/main/res/drawable/control_dpad_left.xml b/android/app/src/main/res/drawable/control_dpad_left.xml
new file mode 100644
index 0000000..025e610
--- /dev/null
+++ b/android/app/src/main/res/drawable/control_dpad_left.xml
@@ -0,0 +1,34 @@
+
+
+
+
+
+
diff --git a/android/app/src/main/res/drawable/control_dpad_left_up.xml b/android/app/src/main/res/drawable/control_dpad_left_up.xml
new file mode 100644
index 0000000..69644e7
--- /dev/null
+++ b/android/app/src/main/res/drawable/control_dpad_left_up.xml
@@ -0,0 +1,34 @@
+
+
+
+
+
+
diff --git a/android/app/src/main/res/drawable/control_touchpad.xml b/android/app/src/main/res/drawable/control_touchpad.xml
new file mode 100644
index 0000000..509b8a3
--- /dev/null
+++ b/android/app/src/main/res/drawable/control_touchpad.xml
@@ -0,0 +1,12 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/control_touchpad_pressed.xml b/android/app/src/main/res/drawable/control_touchpad_pressed.xml
new file mode 100644
index 0000000..b4f3f16
--- /dev/null
+++ b/android/app/src/main/res/drawable/control_touchpad_pressed.xml
@@ -0,0 +1,12 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/ic_add.xml b/android/app/src/main/res/drawable/ic_add.xml
new file mode 100644
index 0000000..0258249
--- /dev/null
+++ b/android/app/src/main/res/drawable/ic_add.xml
@@ -0,0 +1,9 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/ic_add_close.xml b/android/app/src/main/res/drawable/ic_add_close.xml
new file mode 100644
index 0000000..903c3b3
--- /dev/null
+++ b/android/app/src/main/res/drawable/ic_add_close.xml
@@ -0,0 +1,29 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/android/app/src/main/res/drawable/ic_add_manual.xml b/android/app/src/main/res/drawable/ic_add_manual.xml
new file mode 100644
index 0000000..d9ae0a1
--- /dev/null
+++ b/android/app/src/main/res/drawable/ic_add_manual.xml
@@ -0,0 +1,23 @@
+
+
+
+
diff --git a/android/app/src/main/res/drawable/ic_bitrate.xml b/android/app/src/main/res/drawable/ic_bitrate.xml
new file mode 100644
index 0000000..0bc089e
--- /dev/null
+++ b/android/app/src/main/res/drawable/ic_bitrate.xml
@@ -0,0 +1,9 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/ic_button_haptic.xml b/android/app/src/main/res/drawable/ic_button_haptic.xml
new file mode 100644
index 0000000..fea4c99
--- /dev/null
+++ b/android/app/src/main/res/drawable/ic_button_haptic.xml
@@ -0,0 +1,9 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/ic_close.xml b/android/app/src/main/res/drawable/ic_close.xml
new file mode 100644
index 0000000..ede4b71
--- /dev/null
+++ b/android/app/src/main/res/drawable/ic_close.xml
@@ -0,0 +1,9 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/ic_codec.xml b/android/app/src/main/res/drawable/ic_codec.xml
new file mode 100644
index 0000000..e173d90
--- /dev/null
+++ b/android/app/src/main/res/drawable/ic_codec.xml
@@ -0,0 +1,15 @@
+
+
+
+
+
diff --git a/android/app/src/main/res/drawable/ic_console.xml b/android/app/src/main/res/drawable/ic_console.xml
new file mode 100644
index 0000000..0ed1bc8
--- /dev/null
+++ b/android/app/src/main/res/drawable/ic_console.xml
@@ -0,0 +1,13 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/ic_console_ps5.xml b/android/app/src/main/res/drawable/ic_console_ps5.xml
new file mode 100644
index 0000000..488213b
--- /dev/null
+++ b/android/app/src/main/res/drawable/ic_console_ps5.xml
@@ -0,0 +1,9 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/ic_console_ps5_ready.xml b/android/app/src/main/res/drawable/ic_console_ps5_ready.xml
new file mode 100644
index 0000000..d2a3ab0
--- /dev/null
+++ b/android/app/src/main/res/drawable/ic_console_ps5_ready.xml
@@ -0,0 +1,16 @@
+
+
+
+
diff --git a/android/app/src/main/res/drawable/ic_console_ps5_standby.xml b/android/app/src/main/res/drawable/ic_console_ps5_standby.xml
new file mode 100644
index 0000000..8d49bbd
--- /dev/null
+++ b/android/app/src/main/res/drawable/ic_console_ps5_standby.xml
@@ -0,0 +1,16 @@
+
+
+
+
diff --git a/android/app/src/main/res/drawable/ic_console_ready.xml b/android/app/src/main/res/drawable/ic_console_ready.xml
new file mode 100644
index 0000000..7d7068f
--- /dev/null
+++ b/android/app/src/main/res/drawable/ic_console_ready.xml
@@ -0,0 +1,20 @@
+
+
+
+
diff --git a/android/app/src/main/res/drawable/ic_console_simple.xml b/android/app/src/main/res/drawable/ic_console_simple.xml
new file mode 100644
index 0000000..75b92c6
--- /dev/null
+++ b/android/app/src/main/res/drawable/ic_console_simple.xml
@@ -0,0 +1,9 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/ic_console_standby.xml b/android/app/src/main/res/drawable/ic_console_standby.xml
new file mode 100644
index 0000000..dff307c
--- /dev/null
+++ b/android/app/src/main/res/drawable/ic_console_standby.xml
@@ -0,0 +1,20 @@
+
+
+
+
diff --git a/android/app/src/main/res/drawable/ic_delete_row.xml b/android/app/src/main/res/drawable/ic_delete_row.xml
new file mode 100644
index 0000000..55c8fa4
--- /dev/null
+++ b/android/app/src/main/res/drawable/ic_delete_row.xml
@@ -0,0 +1,9 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/ic_discover_off.xml b/android/app/src/main/res/drawable/ic_discover_off.xml
new file mode 100644
index 0000000..be1a950
--- /dev/null
+++ b/android/app/src/main/res/drawable/ic_discover_off.xml
@@ -0,0 +1,4 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/ic_discover_on.xml b/android/app/src/main/res/drawable/ic_discover_on.xml
new file mode 100644
index 0000000..1c8b3be
--- /dev/null
+++ b/android/app/src/main/res/drawable/ic_discover_on.xml
@@ -0,0 +1,4 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/ic_discovered_on_secondary.xml b/android/app/src/main/res/drawable/ic_discovered_on_secondary.xml
new file mode 100644
index 0000000..6eb9b72
--- /dev/null
+++ b/android/app/src/main/res/drawable/ic_discovered_on_secondary.xml
@@ -0,0 +1,9 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/ic_display_normal.xml b/android/app/src/main/res/drawable/ic_display_normal.xml
new file mode 100644
index 0000000..4ffbf8f
--- /dev/null
+++ b/android/app/src/main/res/drawable/ic_display_normal.xml
@@ -0,0 +1,9 @@
+
+
+
\ No newline at end of file
diff --git a/android/app/src/main/res/drawable/ic_display_stretch.xml b/android/app/src/main/res/drawable/ic_display_stretch.xml
new file mode 100644
index 0000000..605cf01
--- /dev/null
+++ b/android/app/src/main/res/drawable/ic_display_stretch.xml
@@ -0,0 +1,9 @@
+
+
+
\ No newline at end of file
diff --git a/android/app/src/main/res/drawable/ic_display_zoom.xml b/android/app/src/main/res/drawable/ic_display_zoom.xml
new file mode 100644
index 0000000..c260698
--- /dev/null
+++ b/android/app/src/main/res/drawable/ic_display_zoom.xml
@@ -0,0 +1,13 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/android/app/src/main/res/drawable/ic_export.xml b/android/app/src/main/res/drawable/ic_export.xml
new file mode 100644
index 0000000..f92f317
--- /dev/null
+++ b/android/app/src/main/res/drawable/ic_export.xml
@@ -0,0 +1,9 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/ic_fps.xml b/android/app/src/main/res/drawable/ic_fps.xml
new file mode 100644
index 0000000..6af4280
--- /dev/null
+++ b/android/app/src/main/res/drawable/ic_fps.xml
@@ -0,0 +1,9 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/ic_gamepad.xml b/android/app/src/main/res/drawable/ic_gamepad.xml
new file mode 100644
index 0000000..00918bf
--- /dev/null
+++ b/android/app/src/main/res/drawable/ic_gamepad.xml
@@ -0,0 +1,9 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/ic_host.xml b/android/app/src/main/res/drawable/ic_host.xml
new file mode 100644
index 0000000..fa79931
--- /dev/null
+++ b/android/app/src/main/res/drawable/ic_host.xml
@@ -0,0 +1,9 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/ic_import.xml b/android/app/src/main/res/drawable/ic_import.xml
new file mode 100644
index 0000000..fceae15
--- /dev/null
+++ b/android/app/src/main/res/drawable/ic_import.xml
@@ -0,0 +1,9 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/ic_launcher_foreground.xml b/android/app/src/main/res/drawable/ic_launcher_foreground.xml
new file mode 100644
index 0000000..f526a91
--- /dev/null
+++ b/android/app/src/main/res/drawable/ic_launcher_foreground.xml
@@ -0,0 +1,46 @@
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/android/app/src/main/res/drawable/ic_log.xml b/android/app/src/main/res/drawable/ic_log.xml
new file mode 100644
index 0000000..ddb91a0
--- /dev/null
+++ b/android/app/src/main/res/drawable/ic_log.xml
@@ -0,0 +1,9 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/ic_motion.xml b/android/app/src/main/res/drawable/ic_motion.xml
new file mode 100644
index 0000000..c5a4ea1
--- /dev/null
+++ b/android/app/src/main/res/drawable/ic_motion.xml
@@ -0,0 +1,9 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/ic_overflow.xml b/android/app/src/main/res/drawable/ic_overflow.xml
new file mode 100644
index 0000000..ecd0f0c
--- /dev/null
+++ b/android/app/src/main/res/drawable/ic_overflow.xml
@@ -0,0 +1,9 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/ic_pin.xml b/android/app/src/main/res/drawable/ic_pin.xml
new file mode 100644
index 0000000..a77ef95
--- /dev/null
+++ b/android/app/src/main/res/drawable/ic_pin.xml
@@ -0,0 +1,9 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/ic_psn_id.xml b/android/app/src/main/res/drawable/ic_psn_id.xml
new file mode 100644
index 0000000..b2cb337
--- /dev/null
+++ b/android/app/src/main/res/drawable/ic_psn_id.xml
@@ -0,0 +1,9 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/ic_regist_console.xml b/android/app/src/main/res/drawable/ic_regist_console.xml
new file mode 100644
index 0000000..0b1762a
--- /dev/null
+++ b/android/app/src/main/res/drawable/ic_regist_console.xml
@@ -0,0 +1,17 @@
+
+
+
+
diff --git a/android/app/src/main/res/drawable/ic_register.xml b/android/app/src/main/res/drawable/ic_register.xml
new file mode 100644
index 0000000..538c5bd
--- /dev/null
+++ b/android/app/src/main/res/drawable/ic_register.xml
@@ -0,0 +1,9 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/ic_resolution.xml b/android/app/src/main/res/drawable/ic_resolution.xml
new file mode 100644
index 0000000..00a6d47
--- /dev/null
+++ b/android/app/src/main/res/drawable/ic_resolution.xml
@@ -0,0 +1,9 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/ic_rumble.xml b/android/app/src/main/res/drawable/ic_rumble.xml
new file mode 100644
index 0000000..4ecdb61
--- /dev/null
+++ b/android/app/src/main/res/drawable/ic_rumble.xml
@@ -0,0 +1,9 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/ic_settings.xml b/android/app/src/main/res/drawable/ic_settings.xml
new file mode 100644
index 0000000..59b8712
--- /dev/null
+++ b/android/app/src/main/res/drawable/ic_settings.xml
@@ -0,0 +1,4 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/ic_share.xml b/android/app/src/main/res/drawable/ic_share.xml
new file mode 100644
index 0000000..e3fe874
--- /dev/null
+++ b/android/app/src/main/res/drawable/ic_share.xml
@@ -0,0 +1,9 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/ic_triangle.xml b/android/app/src/main/res/drawable/ic_triangle.xml
new file mode 100644
index 0000000..fe7ccda
--- /dev/null
+++ b/android/app/src/main/res/drawable/ic_triangle.xml
@@ -0,0 +1,15 @@
+
+
+
diff --git a/android/app/src/main/res/drawable/selector_add_fab.xml b/android/app/src/main/res/drawable/selector_add_fab.xml
new file mode 100644
index 0000000..51931e3
--- /dev/null
+++ b/android/app/src/main/res/drawable/selector_add_fab.xml
@@ -0,0 +1,24 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/android/app/src/main/res/layout/activity_edit_manual.xml b/android/app/src/main/res/layout/activity_edit_manual.xml
new file mode 100644
index 0000000..0550aa9
--- /dev/null
+++ b/android/app/src/main/res/layout/activity_edit_manual.xml
@@ -0,0 +1,83 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/android/app/src/main/res/layout/activity_main.xml b/android/app/src/main/res/layout/activity_main.xml
new file mode 100644
index 0000000..e16f368
--- /dev/null
+++ b/android/app/src/main/res/layout/activity_main.xml
@@ -0,0 +1,167 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/android/app/src/main/res/layout/activity_regist.xml b/android/app/src/main/res/layout/activity_regist.xml
new file mode 100644
index 0000000..7a8398b
--- /dev/null
+++ b/android/app/src/main/res/layout/activity_regist.xml
@@ -0,0 +1,207 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/android/app/src/main/res/layout/activity_regist_execute.xml b/android/app/src/main/res/layout/activity_regist_execute.xml
new file mode 100644
index 0000000..b5c0ab5
--- /dev/null
+++ b/android/app/src/main/res/layout/activity_regist_execute.xml
@@ -0,0 +1,67 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/android/app/src/main/res/layout/activity_settings.xml b/android/app/src/main/res/layout/activity_settings.xml
new file mode 100644
index 0000000..fffe0ce
--- /dev/null
+++ b/android/app/src/main/res/layout/activity_settings.xml
@@ -0,0 +1,32 @@
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/android/app/src/main/res/layout/activity_stream.xml b/android/app/src/main/res/layout/activity_stream.xml
new file mode 100644
index 0000000..6a7f9a3
--- /dev/null
+++ b/android/app/src/main/res/layout/activity_stream.xml
@@ -0,0 +1,120 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/android/app/src/main/res/layout/dialog_login_pin.xml b/android/app/src/main/res/layout/dialog_login_pin.xml
new file mode 100644
index 0000000..2dfff05
--- /dev/null
+++ b/android/app/src/main/res/layout/dialog_login_pin.xml
@@ -0,0 +1,15 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/android/app/src/main/res/layout/dropdown_menu_popup_item.xml b/android/app/src/main/res/layout/dropdown_menu_popup_item.xml
new file mode 100644
index 0000000..2b175c8
--- /dev/null
+++ b/android/app/src/main/res/layout/dropdown_menu_popup_item.xml
@@ -0,0 +1,8 @@
+
+
\ No newline at end of file
diff --git a/android/app/src/main/res/layout/fragment_controls.xml b/android/app/src/main/res/layout/fragment_controls.xml
new file mode 100644
index 0000000..7a6b3af
--- /dev/null
+++ b/android/app/src/main/res/layout/fragment_controls.xml
@@ -0,0 +1,246 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/android/app/src/main/res/layout/fragment_settings_logs.xml b/android/app/src/main/res/layout/fragment_settings_logs.xml
new file mode 100644
index 0000000..e35072c
--- /dev/null
+++ b/android/app/src/main/res/layout/fragment_settings_logs.xml
@@ -0,0 +1,44 @@
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/android/app/src/main/res/layout/fragment_settings_registered_hosts.xml b/android/app/src/main/res/layout/fragment_settings_registered_hosts.xml
new file mode 100644
index 0000000..b616832
--- /dev/null
+++ b/android/app/src/main/res/layout/fragment_settings_registered_hosts.xml
@@ -0,0 +1,55 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/android/app/src/main/res/layout/fragment_touchpad_only.xml b/android/app/src/main/res/layout/fragment_touchpad_only.xml
new file mode 100644
index 0000000..505612a
--- /dev/null
+++ b/android/app/src/main/res/layout/fragment_touchpad_only.xml
@@ -0,0 +1,30 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/android/app/src/main/res/layout/item_display_host.xml b/android/app/src/main/res/layout/item_display_host.xml
new file mode 100644
index 0000000..116e0bd
--- /dev/null
+++ b/android/app/src/main/res/layout/item_display_host.xml
@@ -0,0 +1,115 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/android/app/src/main/res/layout/item_log_file.xml b/android/app/src/main/res/layout/item_log_file.xml
new file mode 100644
index 0000000..894685e
--- /dev/null
+++ b/android/app/src/main/res/layout/item_log_file.xml
@@ -0,0 +1,69 @@
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/android/app/src/main/res/layout/item_registered_host.xml b/android/app/src/main/res/layout/item_registered_host.xml
new file mode 100644
index 0000000..002dd78
--- /dev/null
+++ b/android/app/src/main/res/layout/item_registered_host.xml
@@ -0,0 +1,53 @@
+
+
+
+
+
+
+
+
+
+
diff --git a/android/app/src/main/res/menu/display_host.xml b/android/app/src/main/res/menu/display_host.xml
new file mode 100644
index 0000000..310dc54
--- /dev/null
+++ b/android/app/src/main/res/menu/display_host.xml
@@ -0,0 +1,9 @@
+
+
\ No newline at end of file
diff --git a/android/app/src/main/res/menu/main.xml b/android/app/src/main/res/menu/main.xml
new file mode 100644
index 0000000..724958c
--- /dev/null
+++ b/android/app/src/main/res/menu/main.xml
@@ -0,0 +1,17 @@
+
+
\ No newline at end of file
diff --git a/android/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml b/android/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml
new file mode 100644
index 0000000..7353dbd
--- /dev/null
+++ b/android/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml
@@ -0,0 +1,5 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/android/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml b/android/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml
new file mode 100644
index 0000000..7353dbd
--- /dev/null
+++ b/android/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml
@@ -0,0 +1,5 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/android/app/src/main/res/mipmap-hdpi/ic_launcher.png b/android/app/src/main/res/mipmap-hdpi/ic_launcher.png
new file mode 100644
index 0000000..9ef9160
Binary files /dev/null and b/android/app/src/main/res/mipmap-hdpi/ic_launcher.png differ
diff --git a/android/app/src/main/res/mipmap-hdpi/ic_launcher_round.png b/android/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
new file mode 100644
index 0000000..ef2abcb
Binary files /dev/null and b/android/app/src/main/res/mipmap-hdpi/ic_launcher_round.png differ
diff --git a/android/app/src/main/res/mipmap-mdpi/ic_launcher.png b/android/app/src/main/res/mipmap-mdpi/ic_launcher.png
new file mode 100644
index 0000000..8b72f4a
Binary files /dev/null and b/android/app/src/main/res/mipmap-mdpi/ic_launcher.png differ
diff --git a/android/app/src/main/res/mipmap-mdpi/ic_launcher_round.png b/android/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
new file mode 100644
index 0000000..6edce34
Binary files /dev/null and b/android/app/src/main/res/mipmap-mdpi/ic_launcher_round.png differ
diff --git a/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png b/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png
new file mode 100644
index 0000000..987a20f
Binary files /dev/null and b/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png differ
diff --git a/android/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png b/android/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
new file mode 100644
index 0000000..c7bf47e
Binary files /dev/null and b/android/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png differ
diff --git a/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png b/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
new file mode 100644
index 0000000..5206036
Binary files /dev/null and b/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png differ
diff --git a/android/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png b/android/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
new file mode 100644
index 0000000..57569e7
Binary files /dev/null and b/android/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png differ
diff --git a/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png b/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
new file mode 100644
index 0000000..30ae3d8
Binary files /dev/null and b/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png differ
diff --git a/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png b/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
new file mode 100644
index 0000000..2e50811
Binary files /dev/null and b/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png differ
diff --git a/android/app/src/main/res/values/attrs.xml b/android/app/src/main/res/values/attrs.xml
new file mode 100644
index 0000000..04f738a
--- /dev/null
+++ b/android/app/src/main/res/values/attrs.xml
@@ -0,0 +1,22 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/android/app/src/main/res/values/colors.xml b/android/app/src/main/res/values/colors.xml
new file mode 100644
index 0000000..2ee5181
--- /dev/null
+++ b/android/app/src/main/res/values/colors.xml
@@ -0,0 +1,18 @@
+
+
+ #162d50
+ #000128
+ #ffaaee
+
+ @android:color/white
+ @android:color/black
+ #77000000
+
+ #fafafa
+ #333333
+
+ #22ffffff
+ #88ffffff
+
+ #B71C1C
+
diff --git a/android/app/src/main/res/values/dimens.xml b/android/app/src/main/res/values/dimens.xml
new file mode 100644
index 0000000..9ee2e9b
--- /dev/null
+++ b/android/app/src/main/res/values/dimens.xml
@@ -0,0 +1,11 @@
+
+
+ 88dp
+ 176dp
+ 24dp
+ 16dp
+ 64dp
+ 48dp
+ 32dp
+ 48dp
+
\ No newline at end of file
diff --git a/android/app/src/main/res/values/ic_launcher_background.xml b/android/app/src/main/res/values/ic_launcher_background.xml
new file mode 100644
index 0000000..a41951a
--- /dev/null
+++ b/android/app/src/main/res/values/ic_launcher_background.xml
@@ -0,0 +1,4 @@
+
+
+ #CFFF0D
+
\ No newline at end of file
diff --git a/android/app/src/main/res/values/strings.xml b/android/app/src/main/res/values/strings.xml
new file mode 100644
index 0000000..254042b
--- /dev/null
+++ b/android/app/src/main/res/values/strings.xml
@@ -0,0 +1,122 @@
+
+ Chiaki
+ Chiaki 千秋
+ Settings
+ The Console is currently in standby mode. Do you want to send a Wakeup packet instead of trying to connect immediately?
+ Session has quit: %s
+ Failed to create Session: %s
+ Login PIN:
+ Entered PIN was incorrect!\nLogin PIN:
+ Reconnect
+ Wakeup
+ Connect
+ Cancel
+ Quit
+ Connect
+ Settings
+ Discover Consoles Automatically
+ Address: %s
+ ID: %s
+ ID: %s (registered)
+ App: %s\nTitle ID: %s
+ Register Console
+ Add Console Manually
+ On the PS4, navigate to
+ Settings →\nRemote Play Connection Settings →\nAdd Device
+ On the PS5, navigate to
+ Settings →\nSystem → Remote Play →\nLink Device
+ to obtain the PIN
+ Register Console
+ Host
+ Broadcast
+ PS4 < 7.0
+ PS4 ≥ 7.0, < 8
+ PS4 ≥ 8.0
+ PS5
+ About obtaining your Account ID, see
+ https://git.sr.ht/~thestr4ng3r/chiaki/tree/master/item/README.md#obtaining-your-psn-accountid
+ PSN Online ID (username, case-sensitive)
+ PSN Account ID (8 bytes, base64)
+ PIN
+ Register
+ Please enter a valid host name
+ Please enter a valid PSN ID
+ Please enter a valid 8-byte Account ID in Base64
+ Please enter a valid %d-digit PIN
+ Share Log
+ Export Settings
+ Regist successful.
+ Regist failed.
+ The console with MAC %s has already been registered. Should the previous record be overwritten?
+ Overwrite
+ Cancel
+ Import
+ Cancel
+ Import
+ Registered Hosts: %s\n\nManual Hosts: %s
+ Failed to import settings from file:\n%s
+ Dismiss
+ Manual Console Entry
+ Save
+ Registered Console
+ Register on first Connection
+ General
+ Stream
+ Export
+ Registered Consoles
+ Session Logs
+ Collected log files from previous sessions for debugging
+ Export Settings
+ Warning: These resulting file can contain your secret Remote Play keys! Do not share them.
+ Import Settings
+ Import Settings from JSON
+ Currently registered: %d
+ Resolution
+ FPS
+ Bitrate
+ Codec
+ Verbose Logging
+ Warning: This logs a LOT! Don\'t enable for regular use.
+ 360p
+ 540p
+ 720p
+ 1080p
+ 30
+ 60
+ Auto (%d)
+ H264
+ H265 (PS5 only)
+ Swap Cross/Moon and Box/Pyramid Buttons
+ Swap face buttons if default mapping is incorrect (e.g. for 8BitDo controllers)
+ Rumble
+ Use phone vibration motor for rumble
+ Motion
+ Use device\'s motion sensors for controller motion
+ Touch Haptics
+ Use phone vibration motor for short haptic feedback on button touches
+ Are you sure you want to delete the registered console %s with ID %s?
+ Are you sure you want to delete the console entry for %s?
+ Keep
+ Delete
+ Edit
+ No Session Logs recorded.
+ No Consoles registered.
+ No consoles added yet.\nEnable Discovery to automatically find consoles on your local network.
+ No consoles added or discovered.
+
+
+ discovery_enabled
+ on_screen_controls_enabled
+ touchpad_only_enabled
+ rumble_enabled
+ motion_enabled
+ button_haptic_enabled
+ log_verbose
+ import_settings
+ export_settings
+ swap_cross_moon
+ stream_resolution
+ stream_fps
+ stream_bitrate
+ stream_codec
+
diff --git a/android/app/src/main/res/values/styles.xml b/android/app/src/main/res/values/styles.xml
new file mode 100644
index 0000000..4eae563
--- /dev/null
+++ b/android/app/src/main/res/values/styles.xml
@@ -0,0 +1,115 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/android/app/src/main/res/xml/filepaths.xml b/android/app/src/main/res/xml/filepaths.xml
new file mode 100644
index 0000000..038db8c
--- /dev/null
+++ b/android/app/src/main/res/xml/filepaths.xml
@@ -0,0 +1,9 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/android/app/src/main/res/xml/preferences.xml b/android/app/src/main/res/xml/preferences.xml
new file mode 100644
index 0000000..49989db
--- /dev/null
+++ b/android/app/src/main/res/xml/preferences.xml
@@ -0,0 +1,96 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/android/build.gradle b/android/build.gradle
new file mode 100644
index 0000000..cdfc5d6
--- /dev/null
+++ b/android/build.gradle
@@ -0,0 +1,28 @@
+// Top-level build file where you can add configuration options common to all sub-projects/modules.
+
+buildscript {
+ ext.kotlin_version = '1.8.0'
+ repositories {
+ google()
+ jcenter()
+
+ }
+ dependencies {
+ classpath 'com.android.tools.build:gradle:7.4.1'
+ classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
+ // NOTE: Do not place your application dependencies here; they belong
+ // in the individual module build.gradle files
+ }
+}
+
+allprojects {
+ repositories {
+ google()
+ jcenter()
+
+ }
+}
+
+task clean(type: Delete) {
+ delete rootProject.buildDir
+}
diff --git a/android/gradle.properties b/android/gradle.properties
new file mode 100644
index 0000000..23339e0
--- /dev/null
+++ b/android/gradle.properties
@@ -0,0 +1,21 @@
+# Project-wide Gradle settings.
+# IDE (e.g. Android Studio) users:
+# Gradle settings configured through the IDE *will override*
+# any settings specified in this file.
+# For more details on how to configure your build environment visit
+# http://www.gradle.org/docs/current/userguide/build_environment.html
+# Specifies the JVM arguments used for the daemon process.
+# The setting is particularly useful for tweaking memory settings.
+org.gradle.jvmargs=-Xmx1536m
+# When configured, Gradle will run in incubating parallel mode.
+# This option should only be used with decoupled projects. More details, visit
+# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
+# org.gradle.parallel=true
+# AndroidX package structure to make it clearer which packages are bundled with the
+# Android operating system, and which are packaged with your app's APK
+# https://developer.android.com/topic/libraries/support-library/androidx-rn
+android.useAndroidX=true
+# Automatically convert third-party libraries to use AndroidX
+android.enableJetifier=true
+# Kotlin code style for this project: "official" or "obsolete":
+kotlin.code.style=official
diff --git a/android/gradle/wrapper/gradle-wrapper.jar b/android/gradle/wrapper/gradle-wrapper.jar
new file mode 100644
index 0000000..f6b961f
Binary files /dev/null and b/android/gradle/wrapper/gradle-wrapper.jar differ
diff --git a/android/gradle/wrapper/gradle-wrapper.properties b/android/gradle/wrapper/gradle-wrapper.properties
new file mode 100644
index 0000000..7a0d628
--- /dev/null
+++ b/android/gradle/wrapper/gradle-wrapper.properties
@@ -0,0 +1,6 @@
+#Sun Feb 05 16:25:19 CET 2023
+distributionBase=GRADLE_USER_HOME
+distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip
+distributionPath=wrapper/dists
+zipStorePath=wrapper/dists
+zipStoreBase=GRADLE_USER_HOME
diff --git a/android/gradlew b/android/gradlew
new file mode 100755
index 0000000..cccdd3d
--- /dev/null
+++ b/android/gradlew
@@ -0,0 +1,172 @@
+#!/usr/bin/env sh
+
+##############################################################################
+##
+## Gradle start up script for UN*X
+##
+##############################################################################
+
+# Attempt to set APP_HOME
+# Resolve links: $0 may be a link
+PRG="$0"
+# Need this for relative symlinks.
+while [ -h "$PRG" ] ; do
+ ls=`ls -ld "$PRG"`
+ link=`expr "$ls" : '.*-> \(.*\)$'`
+ if expr "$link" : '/.*' > /dev/null; then
+ PRG="$link"
+ else
+ PRG=`dirname "$PRG"`"/$link"
+ fi
+done
+SAVED="`pwd`"
+cd "`dirname \"$PRG\"`/" >/dev/null
+APP_HOME="`pwd -P`"
+cd "$SAVED" >/dev/null
+
+APP_NAME="Gradle"
+APP_BASE_NAME=`basename "$0"`
+
+# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
+DEFAULT_JVM_OPTS=""
+
+# Use the maximum available, or set MAX_FD != -1 to use that value.
+MAX_FD="maximum"
+
+warn () {
+ echo "$*"
+}
+
+die () {
+ echo
+ echo "$*"
+ echo
+ exit 1
+}
+
+# OS specific support (must be 'true' or 'false').
+cygwin=false
+msys=false
+darwin=false
+nonstop=false
+case "`uname`" in
+ CYGWIN* )
+ cygwin=true
+ ;;
+ Darwin* )
+ darwin=true
+ ;;
+ MINGW* )
+ msys=true
+ ;;
+ NONSTOP* )
+ nonstop=true
+ ;;
+esac
+
+CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
+
+# Determine the Java command to use to start the JVM.
+if [ -n "$JAVA_HOME" ] ; then
+ if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
+ # IBM's JDK on AIX uses strange locations for the executables
+ JAVACMD="$JAVA_HOME/jre/sh/java"
+ else
+ JAVACMD="$JAVA_HOME/bin/java"
+ fi
+ if [ ! -x "$JAVACMD" ] ; then
+ die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
+
+Please set the JAVA_HOME variable in your environment to match the
+location of your Java installation."
+ fi
+else
+ JAVACMD="java"
+ which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
+
+Please set the JAVA_HOME variable in your environment to match the
+location of your Java installation."
+fi
+
+# Increase the maximum file descriptors if we can.
+if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
+ MAX_FD_LIMIT=`ulimit -H -n`
+ if [ $? -eq 0 ] ; then
+ if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
+ MAX_FD="$MAX_FD_LIMIT"
+ fi
+ ulimit -n $MAX_FD
+ if [ $? -ne 0 ] ; then
+ warn "Could not set maximum file descriptor limit: $MAX_FD"
+ fi
+ else
+ warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
+ fi
+fi
+
+# For Darwin, add options to specify how the application appears in the dock
+if $darwin; then
+ GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
+fi
+
+# For Cygwin, switch paths to Windows format before running java
+if $cygwin ; then
+ APP_HOME=`cygpath --path --mixed "$APP_HOME"`
+ CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
+ JAVACMD=`cygpath --unix "$JAVACMD"`
+
+ # We build the pattern for arguments to be converted via cygpath
+ ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
+ SEP=""
+ for dir in $ROOTDIRSRAW ; do
+ ROOTDIRS="$ROOTDIRS$SEP$dir"
+ SEP="|"
+ done
+ OURCYGPATTERN="(^($ROOTDIRS))"
+ # Add a user-defined pattern to the cygpath arguments
+ if [ "$GRADLE_CYGPATTERN" != "" ] ; then
+ OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
+ fi
+ # Now convert the arguments - kludge to limit ourselves to /bin/sh
+ i=0
+ for arg in "$@" ; do
+ CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
+ CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
+
+ if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
+ eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
+ else
+ eval `echo args$i`="\"$arg\""
+ fi
+ i=$((i+1))
+ done
+ case $i in
+ (0) set -- ;;
+ (1) set -- "$args0" ;;
+ (2) set -- "$args0" "$args1" ;;
+ (3) set -- "$args0" "$args1" "$args2" ;;
+ (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
+ (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
+ (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
+ (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
+ (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
+ (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
+ esac
+fi
+
+# Escape application args
+save () {
+ for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
+ echo " "
+}
+APP_ARGS=$(save "$@")
+
+# Collect all arguments for the java command, following the shell quoting and substitution rules
+eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
+
+# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
+if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
+ cd "$(dirname "$0")"
+fi
+
+exec "$JAVACMD" "$@"
diff --git a/android/gradlew.bat b/android/gradlew.bat
new file mode 100644
index 0000000..e95643d
--- /dev/null
+++ b/android/gradlew.bat
@@ -0,0 +1,84 @@
+@if "%DEBUG%" == "" @echo off
+@rem ##########################################################################
+@rem
+@rem Gradle startup script for Windows
+@rem
+@rem ##########################################################################
+
+@rem Set local scope for the variables with windows NT shell
+if "%OS%"=="Windows_NT" setlocal
+
+set DIRNAME=%~dp0
+if "%DIRNAME%" == "" set DIRNAME=.
+set APP_BASE_NAME=%~n0
+set APP_HOME=%DIRNAME%
+
+@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
+set DEFAULT_JVM_OPTS=
+
+@rem Find java.exe
+if defined JAVA_HOME goto findJavaFromJavaHome
+
+set JAVA_EXE=java.exe
+%JAVA_EXE% -version >NUL 2>&1
+if "%ERRORLEVEL%" == "0" goto init
+
+echo.
+echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
+echo.
+echo Please set the JAVA_HOME variable in your environment to match the
+echo location of your Java installation.
+
+goto fail
+
+:findJavaFromJavaHome
+set JAVA_HOME=%JAVA_HOME:"=%
+set JAVA_EXE=%JAVA_HOME%/bin/java.exe
+
+if exist "%JAVA_EXE%" goto init
+
+echo.
+echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
+echo.
+echo Please set the JAVA_HOME variable in your environment to match the
+echo location of your Java installation.
+
+goto fail
+
+:init
+@rem Get command-line arguments, handling Windows variants
+
+if not "%OS%" == "Windows_NT" goto win9xME_args
+
+:win9xME_args
+@rem Slurp the command line arguments.
+set CMD_LINE_ARGS=
+set _SKIP=2
+
+:win9xME_args_slurp
+if "x%~1" == "x" goto execute
+
+set CMD_LINE_ARGS=%*
+
+:execute
+@rem Setup the command line
+
+set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
+
+@rem Execute Gradle
+"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
+
+:end
+@rem End local scope for the variables with windows NT shell
+if "%ERRORLEVEL%"=="0" goto mainEnd
+
+:fail
+rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
+rem the _cmd.exe /c_ return code!
+if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
+exit /b 1
+
+:mainEnd
+if "%OS%"=="Windows_NT" endlocal
+
+:omega
diff --git a/android/settings.gradle b/android/settings.gradle
new file mode 100644
index 0000000..1f50fd9
--- /dev/null
+++ b/android/settings.gradle
@@ -0,0 +1,2 @@
+include ':app'
+rootProject.name='Chiaki'
diff --git a/assets/.gitignore b/assets/.gitignore
new file mode 100644
index 0000000..19af48d
--- /dev/null
+++ b/assets/.gitignore
@@ -0,0 +1,3 @@
+screenshots
+chiaki_square.png
+chiaki_function.png
diff --git a/assets/add_console.svg b/assets/add_console.svg
new file mode 100644
index 0000000..2536aad
--- /dev/null
+++ b/assets/add_console.svg
@@ -0,0 +1,81 @@
+
+
diff --git a/assets/chiaki_android_fg.svg b/assets/chiaki_android_fg.svg
new file mode 100644
index 0000000..821a36b
--- /dev/null
+++ b/assets/chiaki_android_fg.svg
@@ -0,0 +1,175 @@
+
+
+
+
diff --git a/assets/chiaki_function.svg b/assets/chiaki_function.svg
new file mode 100644
index 0000000..a2e49bd
--- /dev/null
+++ b/assets/chiaki_function.svg
@@ -0,0 +1,258 @@
+
+
diff --git a/assets/chiaki_macos.svg b/assets/chiaki_macos.svg
new file mode 100644
index 0000000..a4ce8f4
--- /dev/null
+++ b/assets/chiaki_macos.svg
@@ -0,0 +1,266 @@
+
+
+
+
diff --git a/assets/chiaki_macos_simple.svg b/assets/chiaki_macos_simple.svg
new file mode 100644
index 0000000..7f1359d
--- /dev/null
+++ b/assets/chiaki_macos_simple.svg
@@ -0,0 +1,102 @@
+
+
+
+
diff --git a/assets/chiaki_square.svg b/assets/chiaki_square.svg
new file mode 100644
index 0000000..ecadc76
--- /dev/null
+++ b/assets/chiaki_square.svg
@@ -0,0 +1,227 @@
+
+
diff --git a/assets/chiaki_wide_simple.svg b/assets/chiaki_wide_simple.svg
new file mode 100644
index 0000000..4ce93ac
--- /dev/null
+++ b/assets/chiaki_wide_simple.svg
@@ -0,0 +1,147 @@
+
+
diff --git a/assets/console.svg b/assets/console.svg
new file mode 100644
index 0000000..99eadb9
--- /dev/null
+++ b/assets/console.svg
@@ -0,0 +1,78 @@
+
+
diff --git a/assets/console2.svg b/assets/console2.svg
new file mode 100644
index 0000000..720fa37
--- /dev/null
+++ b/assets/console2.svg
@@ -0,0 +1,71 @@
+
+
diff --git a/assets/controls/buttons.svg b/assets/controls/buttons.svg
new file mode 100644
index 0000000..70bdcaf
--- /dev/null
+++ b/assets/controls/buttons.svg
@@ -0,0 +1,182 @@
+
+
diff --git a/assets/controls/dpad.svg b/assets/controls/dpad.svg
new file mode 100644
index 0000000..0884b63
--- /dev/null
+++ b/assets/controls/dpad.svg
@@ -0,0 +1,153 @@
+
+
+
+
diff --git a/assets/controls/l1.svg b/assets/controls/l1.svg
new file mode 100644
index 0000000..9d3c3f9
--- /dev/null
+++ b/assets/controls/l1.svg
@@ -0,0 +1,36 @@
+
+
diff --git a/assets/controls/l2.svg b/assets/controls/l2.svg
new file mode 100644
index 0000000..4d561e0
--- /dev/null
+++ b/assets/controls/l2.svg
@@ -0,0 +1,36 @@
+
+
diff --git a/assets/controls/l2_raw.svg b/assets/controls/l2_raw.svg
new file mode 100644
index 0000000..6b39ce5
--- /dev/null
+++ b/assets/controls/l2_raw.svg
@@ -0,0 +1,74 @@
+
+
diff --git a/assets/controls/l3.svg b/assets/controls/l3.svg
new file mode 100644
index 0000000..464e440
--- /dev/null
+++ b/assets/controls/l3.svg
@@ -0,0 +1,36 @@
+
+
diff --git a/assets/controls/l3_raw.svg b/assets/controls/l3_raw.svg
new file mode 100644
index 0000000..f04135f
--- /dev/null
+++ b/assets/controls/l3_raw.svg
@@ -0,0 +1,44 @@
+
+
diff --git a/assets/controls/lr12.svg b/assets/controls/lr12.svg
new file mode 100644
index 0000000..874475f
--- /dev/null
+++ b/assets/controls/lr12.svg
@@ -0,0 +1,124 @@
+
+
diff --git a/assets/controls/options_share.svg b/assets/controls/options_share.svg
new file mode 100644
index 0000000..59ce749
--- /dev/null
+++ b/assets/controls/options_share.svg
@@ -0,0 +1,105 @@
+
+
diff --git a/assets/controls/r1.svg b/assets/controls/r1.svg
new file mode 100644
index 0000000..70f881c
--- /dev/null
+++ b/assets/controls/r1.svg
@@ -0,0 +1,36 @@
+
+
diff --git a/assets/controls/r2.svg b/assets/controls/r2.svg
new file mode 100644
index 0000000..c32639d
--- /dev/null
+++ b/assets/controls/r2.svg
@@ -0,0 +1,37 @@
+
+
diff --git a/assets/controls/r3.svg b/assets/controls/r3.svg
new file mode 100644
index 0000000..b5f1fa7
--- /dev/null
+++ b/assets/controls/r3.svg
@@ -0,0 +1,36 @@
+
+
diff --git a/assets/controls/r3_raw.svg b/assets/controls/r3_raw.svg
new file mode 100644
index 0000000..5f47c8e
--- /dev/null
+++ b/assets/controls/r3_raw.svg
@@ -0,0 +1,44 @@
+
+
diff --git a/assets/controls/stick.svg b/assets/controls/stick.svg
new file mode 100644
index 0000000..96bbe17
--- /dev/null
+++ b/assets/controls/stick.svg
@@ -0,0 +1,64 @@
+
+
+
+
diff --git a/assets/controls/touchpad.svg b/assets/controls/touchpad.svg
new file mode 100644
index 0000000..0b7752e
--- /dev/null
+++ b/assets/controls/touchpad.svg
@@ -0,0 +1,149 @@
+
+
diff --git a/assets/controls/touchpad_surface.svg b/assets/controls/touchpad_surface.svg
new file mode 100644
index 0000000..7476128
--- /dev/null
+++ b/assets/controls/touchpad_surface.svg
@@ -0,0 +1,65 @@
+
+
diff --git a/assets/ic_launcher-web.png b/assets/ic_launcher-web.png
new file mode 100644
index 0000000..664eacf
Binary files /dev/null and b/assets/ic_launcher-web.png differ
diff --git a/assets/regist_console.svg b/assets/regist_console.svg
new file mode 100644
index 0000000..1361592
--- /dev/null
+++ b/assets/regist_console.svg
@@ -0,0 +1,75 @@
+
+
+
+
diff --git a/assets/triangle.svg b/assets/triangle.svg
new file mode 100644
index 0000000..89d0e25
--- /dev/null
+++ b/assets/triangle.svg
@@ -0,0 +1,63 @@
+
+
+
+
diff --git a/cli/CMakeLists.txt b/cli/CMakeLists.txt
index b44d0f5..83de1f9 100644
--- a/cli/CMakeLists.txt
+++ b/cli/CMakeLists.txt
@@ -1,11 +1,18 @@
set(SOURCE
include/chiaki-cli.h
- src/discover.c)
+ src/discover.c
+ src/wakeup.c)
add_library(chiaki-cli-lib STATIC ${SOURCE})
target_include_directories(chiaki-cli-lib PUBLIC "include")
target_link_libraries(chiaki-cli-lib chiaki-lib)
+if(CHIAKI_CLI_ARGP_STANDALONE)
+ find_package(Argp REQUIRED)
+ target_link_libraries(chiaki-cli-lib Argp::Argp)
+endif()
+
add_executable(chiaki-cli src/main.c)
target_link_libraries(chiaki-cli chiaki-cli-lib)
+install(TARGETS chiaki-cli)
diff --git a/cli/include/chiaki-cli.h b/cli/include/chiaki-cli.h
index 0bd1e1b..91ee0dd 100644
--- a/cli/include/chiaki-cli.h
+++ b/cli/include/chiaki-cli.h
@@ -1,19 +1,4 @@
-/*
- * This file is part of Chiaki.
- *
- * Chiaki is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Chiaki is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Chiaki. If not, see .
- */
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
#ifndef CHIAKI_CHIAKI_CLI_H
#define CHIAKI_CHIAKI_CLI_H
@@ -26,6 +11,7 @@ extern "C" {
#endif
CHIAKI_EXPORT int chiaki_cli_cmd_discover(ChiakiLog *log, int argc, char *argv[]);
+CHIAKI_EXPORT int chiaki_cli_cmd_wakeup(ChiakiLog *log, int argc, char *argv[]);
#ifdef __cplusplus
}
diff --git a/cli/src/discover.c b/cli/src/discover.c
index 2f2e854..cf9319f 100644
--- a/cli/src/discover.c
+++ b/cli/src/discover.c
@@ -1,19 +1,4 @@
-/*
- * This file is part of Chiaki.
- *
- * Chiaki is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Chiaki is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Chiaki. If not, see .
- */
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
#include
@@ -24,6 +9,7 @@
#include
#include
#include
+#include
static char doc[] = "Send a PS4 discovery request.";
@@ -156,16 +142,22 @@ CHIAKI_EXPORT int chiaki_cli_cmd_discover(ChiakiLog *log, int argc, char *argv[]
return 1;
}
- ((struct sockaddr_in *)host_addr)->sin_port = htons(CHIAKI_DISCOVERY_PORT); // TODO: IPv6
-
ChiakiDiscoveryPacket packet;
memset(&packet, 0, sizeof(packet));
packet.cmd = CHIAKI_DISCOVERY_CMD_SRCH;
-
- chiaki_discovery_send(&discovery, &packet, host_addr, host_addr_len);
+ packet.protocol_version = CHIAKI_DISCOVERY_PROTOCOL_VERSION_PS4;
+ ((struct sockaddr_in *)host_addr)->sin_port = htons(CHIAKI_DISCOVERY_PORT_PS4);
+ err = chiaki_discovery_send(&discovery, &packet, host_addr, host_addr_len);
+ if(err != CHIAKI_ERR_SUCCESS)
+ CHIAKI_LOGE(log, "Failed to send discovery packet for PS4: %s", chiaki_error_string(err));
+ packet.protocol_version = CHIAKI_DISCOVERY_PROTOCOL_VERSION_PS5;
+ ((struct sockaddr_in *)host_addr)->sin_port = htons(CHIAKI_DISCOVERY_PORT_PS5);
+ err = chiaki_discovery_send(&discovery, &packet, host_addr, host_addr_len);
+ if(err != CHIAKI_ERR_SUCCESS)
+ CHIAKI_LOGE(log, "Failed to send discovery packet for PS5: %s", chiaki_error_string(err));
while(1)
- sleep(1);
+ sleep(1); // TODO: wtf
return 0;
-}
\ No newline at end of file
+}
diff --git a/cli/src/main.c b/cli/src/main.c
index 7395ba8..3f7747a 100644
--- a/cli/src/main.c
+++ b/cli/src/main.c
@@ -1,19 +1,4 @@
-/*
-* This file is part of Chiaki.
-*
-* Chiaki is free software: you can redistribute it and/or modify
-* it under the terms of the GNU General Public License as published by
-* the Free Software Foundation, either version 3 of the License, or
-* (at your option) any later version.
-*
-* Chiaki is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-* GNU General Public License for more details.
-*
-* You should have received a copy of the GNU General Public License
-* along with Chiaki. If not, see .
-*/
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
#include
@@ -24,10 +9,11 @@
#include
static const char doc[] =
- "CLI for Chiaki (PS4 Remote Play Client)"
+ "CLI for Chiaki (PlayStation Remote Play Client)"
"\v"
"Supported commands are:\n"
- " discover Discover Consoles.\n";
+ " discover Discover Consoles.\n"
+ " wakeup Send Wakeup Packet.\n";
#define ARG_KEY_VERBOSE 'v'
@@ -73,6 +59,8 @@ static int parse_opt(int key, char *arg, struct argp_state *state)
case ARGP_KEY_ARG:
if(strcmp(arg, "discover") == 0)
exit(call_subcmd(state, "discover", chiaki_cli_cmd_discover));
+ else if(strcmp(arg, "wakeup") == 0)
+ exit(call_subcmd(state, "wakeup", chiaki_cli_cmd_wakeup));
// fallthrough
case ARGP_KEY_END:
argp_usage(state);
diff --git a/cli/src/wakeup.c b/cli/src/wakeup.c
new file mode 100644
index 0000000..cea2792
--- /dev/null
+++ b/cli/src/wakeup.c
@@ -0,0 +1,89 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+#include
+
+#include
+
+#include
+#include
+
+static char doc[] = "Send a PS4 wakeup packet.";
+
+#define ARG_KEY_HOST 'h'
+#define ARG_KEY_REGISTKEY 'r'
+#define ARG_KEY_PS4 '4'
+#define ARG_KEY_PS5 '5'
+
+static struct argp_option options[] = {
+ { "host", ARG_KEY_HOST, "Host", 0, "Host to send wakeup packet to", 0 },
+ { "registkey", ARG_KEY_REGISTKEY, "RegistKey", 0, "Remote Play registration key (plaintext)", 0 },
+ { "ps4", ARG_KEY_PS4, NULL, 0, "PlayStation 4", 0 },
+ { "ps5", ARG_KEY_PS5, NULL, 0, "PlayStation 5 (default)", 0 },
+ { 0 }
+};
+
+typedef struct arguments
+{
+ const char *host;
+ const char *registkey;
+ bool ps5;
+} Arguments;
+
+static int parse_opt(int key, char *arg, struct argp_state *state)
+{
+ Arguments *arguments = state->input;
+
+ switch(key)
+ {
+ case ARG_KEY_HOST:
+ arguments->host = arg;
+ break;
+ case ARG_KEY_REGISTKEY:
+ arguments->registkey = arg;
+ break;
+ case ARGP_KEY_ARG:
+ argp_usage(state);
+ break;
+ case ARG_KEY_PS4:
+ arguments->ps5 = false;
+ break;
+ case ARG_KEY_PS5:
+ arguments->ps5 = true;
+ break;
+ default:
+ return ARGP_ERR_UNKNOWN;
+ }
+
+ return 0;
+}
+
+static struct argp argp = { options, parse_opt, 0, doc, 0, 0, 0 };
+
+CHIAKI_EXPORT int chiaki_cli_cmd_wakeup(ChiakiLog *log, int argc, char *argv[])
+{
+ Arguments arguments = { 0 };
+ arguments.ps5 = true;
+ error_t argp_r = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, NULL, &arguments);
+ if(argp_r != 0)
+ return 1;
+
+ if(!arguments.host)
+ {
+ fprintf(stderr, "No host specified, see --help.\n");
+ return 1;
+ }
+ if(!arguments.registkey)
+ {
+ fprintf(stderr, "No registration key specified, see --help.\n");
+ return 1;
+ }
+ if(strlen(arguments.registkey) > 8)
+ {
+ fprintf(stderr, "Given registkey is too long.\n");
+ return 1;
+ }
+
+ uint64_t credential = (uint64_t)strtoull(arguments.registkey, NULL, 16);
+
+ return chiaki_discovery_wakeup(log, NULL, arguments.host, credential, arguments.ps5);
+}
diff --git a/cmake/FindArgp.cmake b/cmake/FindArgp.cmake
new file mode 100644
index 0000000..d88fc2f
--- /dev/null
+++ b/cmake/FindArgp.cmake
@@ -0,0 +1,20 @@
+# FindArgp
+# Will define Target Argp::Argp
+
+find_path(Argp_INCLUDE_DIR
+ NAMES argp.h)
+
+find_library(Argp_LIBRARY
+ NAMES argp)
+
+include(FindPackageHandleStandardArgs)
+find_package_handle_standard_args(Argp
+ REQUIRED_VARS Argp_LIBRARY Argp_INCLUDE_DIR)
+
+if(Argp_FOUND AND NOT TARGET Argp::Argp)
+ add_library(Argp::Argp UNKNOWN IMPORTED)
+ set_target_properties(Argp::Argp PROPERTIES
+ IMPORTED_LOCATION "${Argp_LIBRARY}"
+ INTERFACE_INCLUDE_DIRECTORIES "${Argp_INCLUDE_DIR}")
+endif()
+
diff --git a/cmake/FindFFMPEG.cmake b/cmake/FindFFMPEG.cmake
index 8ad5ec9..bdb66aa 100644
--- a/cmake/FindFFMPEG.cmake
+++ b/cmake/FindFFMPEG.cmake
@@ -42,25 +42,45 @@ function(join OUTPUT GLUE)
endfunction()
function (_ffmpeg_find component headername)
+ if(TARGET "FFMPEG::${component}")
+ # already found before
+ return()
+ endif()
+
# Try pkg-config first
if(PKG_CONFIG_FOUND)
- pkg_check_modules(FFMPEG_${component} lib${component} IMPORTED_TARGET)
+ if(CMAKE_VERSION VERSION_LESS "3.6")
+ pkg_check_modules(FFMPEG_${component} lib${component})
+ else()
+ pkg_check_modules(FFMPEG_${component} lib${component} IMPORTED_TARGET)
+ endif()
if(FFMPEG_${component}_FOUND)
- if(APPLE)
- join(FFMPEG_LDFLAGS_STRING " " ${FFMPEG_${component}_LDFLAGS})
- string(REGEX REPLACE "-Wl,-framework,([^ ]+)" "-framework \\1" FFMPEG_LDFLAGS_STRING_CLEAN ${FFMPEG_LDFLAGS_STRING})
- string(REGEX MATCHALL "-framework [^ ]+" FFMPEG_FRAMEWORKS ${FFMPEG_LDFLAGS_STRING_CLEAN})
- list(APPEND FFMPEG_${component}_LIBRARIES ${FFMPEG_FRAMEWORKS})
- set_target_properties(PkgConfig::FFMPEG_${component} PROPERTIES
- INTERFACE_LINK_DIRECTORIES "${FFMPEG_${component}_LIBRARY_DIRS}"
- INTERFACE_LINK_LIBRARIES "${FFMPEG_${component}_LIBRARIES}"
- INTERFACE_LINK_OPTIONS "")
- message("set libs to \"${FFMPEG_${component}_LIBRARIES}\"")
- message("set lib dirs to \"${FFMPEG_${component}_LIBRARY_DIRS}\"")
- message("set lib otps not to \"${FFMPEG_${component}_LDFLAGS}\"")
+ if((TARGET PkgConfig::FFMPEG_${component}) AND (NOT CMAKE_VERSION VERSION_LESS "3.11.0"))
+ if(APPLE)
+ join(FFMPEG_LDFLAGS_STRING " " ${FFMPEG_${component}_LDFLAGS})
+ string(REGEX REPLACE "-Wl,-framework,([^ ]+)" "-framework \\1" FFMPEG_LDFLAGS_STRING_CLEAN ${FFMPEG_LDFLAGS_STRING})
+ string(REGEX MATCHALL "-framework [^ ]+" FFMPEG_FRAMEWORKS ${FFMPEG_LDFLAGS_STRING_CLEAN})
+ list(APPEND FFMPEG_${component}_LIBRARIES ${FFMPEG_FRAMEWORKS})
+ set_target_properties(PkgConfig::FFMPEG_${component} PROPERTIES
+ INTERFACE_LINK_DIRECTORIES "${FFMPEG_${component}_LIBRARY_DIRS}"
+ INTERFACE_LINK_LIBRARIES "${FFMPEG_${component}_LIBRARIES}"
+ INTERFACE_LINK_OPTIONS "")
+ message("set libs to \"${FFMPEG_${component}_LIBRARIES}\"")
+ message("set lib dirs to \"${FFMPEG_${component}_LIBRARY_DIRS}\"")
+ message("set lib otps not to \"${FFMPEG_${component}_LDFLAGS}\"")
+ endif()
+ set_target_properties(PkgConfig::FFMPEG_${component} PROPERTIES IMPORTED_GLOBAL TRUE)
+ add_library(FFMPEG::${component} ALIAS PkgConfig::FFMPEG_${component})
+ else()
+ add_library("FFMPEG::${component}" INTERFACE IMPORTED)
+ if(CMAKE_VERSION VERSION_LESS "3.6")
+ link_directories("${FFMPEG_${component}_LIBRARY_DIRS}")
+ endif()
+ set_target_properties("FFMPEG::${component}" PROPERTIES
+ INTERFACE_LINK_DIRECTORIES "${FFMPEG_${component}_LIBRARY_DIRS}"
+ INTERFACE_INCLUDE_DIRECTORIES "${FFMPEG_${component}_INCLUDE_DIRS}"
+ INTERFACE_LINK_LIBRARIES "${FFMPEG_${component}_LIBRARIES}")
endif()
- set_target_properties(PkgConfig::FFMPEG_${component} PROPERTIES IMPORTED_GLOBAL TRUE)
- add_library(FFMPEG::${component} ALIAS PkgConfig::FFMPEG_${component})
return()
endif()
endif()
@@ -211,10 +231,14 @@ foreach (_ffmpeg_component IN LISTS FFMPEG_FIND_COMPONENTS)
list(APPEND _ffmpeg_required_vars
"FFMPEG_${_ffmpeg_component}_LIBRARIES")
else()
- set(FFMPEG_${_ffmpeg_component}_INCLUDE_DIRS
- "${FFMPEG_${_ffmpeg_component}_INCLUDE_DIR}")
- set(FFMPEG_${_ffmpeg_component}_LIBRARIES
- "${FFMPEG_${_ffmpeg_component}_LIBRARY}")
+ if(NOT FFMPEG_${_ffmpeg_component}_INCLUDE_DIRS)
+ set(FFMPEG_${_ffmpeg_component}_INCLUDE_DIRS
+ "${FFMPEG_${_ffmpeg_component}_INCLUDE_DIR}")
+ endif()
+ if(NOT FFMPEG_${_ffmpeg_component}_LIBRARIES)
+ set(FFMPEG_${_ffmpeg_component}_LIBRARIES
+ "${FFMPEG_${_ffmpeg_component}_LIBRARY}")
+ endif()
list(APPEND FFMPEG_INCLUDE_DIRS
"${FFMPEG_${_ffmpeg_component}_INCLUDE_DIRS}")
list(APPEND FFMPEG_LIBRARIES
diff --git a/cmake/FindILClient.cmake b/cmake/FindILClient.cmake
new file mode 100644
index 0000000..b24fccd
--- /dev/null
+++ b/cmake/FindILClient.cmake
@@ -0,0 +1,57 @@
+# Provides ILClient::ILClient
+# (Raspberry Pi-specific video decoding stuff, very specific for libraspberrypi0 and libraspberrypi-doc)
+
+set(_required_libs
+ /opt/vc/lib/libbcm_host.so
+ /opt/vc/lib/libvcilcs.a
+ /opt/vc/lib/libvchiq_arm.so
+ /opt/vc/lib/libvcos.so)
+
+unset(_libvars)
+foreach(_lib ${_required_libs})
+ get_filename_component(_libname "${_lib}" NAME_WE)
+ set(_libvar "ILClient_${_libname}_LIBRARY")
+ list(APPEND _libvars "${_libvar}")
+ if(EXISTS "${_lib}")
+ set("${_libvar}" "${_lib}")
+ else()
+ set("${_libvar}" "${_libvar}-NOTFOUND")
+ endif()
+endforeach()
+
+find_path(ILClient_INCLUDE_DIR bcm_host.h
+ PATHS /opt/vc/include
+ NO_DEFAULT_PATH)
+
+find_path(ILClient_SOURCE_DIR ilclient.c
+ PATHS /opt/vc/src/hello_pi/libs/ilclient)
+
+include(FindPackageHandleStandardArgs)
+find_package_handle_standard_args(ILClient
+ FOUND_VAR ILClient_FOUND
+ REQUIRED_VARS
+ ${_libvars}
+ ILClient_INCLUDE_DIR
+ ILClient_SOURCE_DIR)
+
+if(ILClient_FOUND)
+ if(NOT TARGET ILClient::ILClient)
+ # see /opt/vc/src/hello_pi/libs/ilclient/Makefile
+ add_library(ilclient STATIC
+ "${ILClient_SOURCE_DIR}/ilclient.c"
+ "${ILClient_SOURCE_DIR}/ilcore.c")
+ target_include_directories(ilclient PUBLIC
+ "${ILClient_INCLUDE_DIR}"
+ "${ILClient_SOURCE_DIR}")
+ target_compile_definitions(ilclient PUBLIC
+ HAVE_LIBOPENMAX=2
+ OMX
+ OMX_SKIP64BIT
+ USE_EXTERNAL_OMX
+ HAVE_LIBBCM_HOST
+ USE_EXTERNAL_LIBBCM_HOST
+ USE_VCHIQ_ARM)
+ target_link_libraries(ilclient PUBLIC ${_required_libs})
+ add_library(ILClient::ILClient ALIAS ilclient)
+ endif()
+endif()
diff --git a/cmake/FindJerasure.cmake b/cmake/FindJerasure.cmake
new file mode 100644
index 0000000..973b5a6
--- /dev/null
+++ b/cmake/FindJerasure.cmake
@@ -0,0 +1,26 @@
+# Provides Jerasure::Jerasure
+
+find_path(Jerasure_INCLUDE_DIR NAMES jerasure.h)
+find_path(Jerasure_INCLUDE_DIR2 NAMES galois.h PATH_SUFFIXES jerasure)
+find_library(Jerasure_LIBRARY NAMES Jerasure)
+
+include(FindPackageHandleStandardArgs)
+find_package_handle_standard_args(Jerasure
+ FOUND_VAR Jerasure_FOUND
+ REQUIRED_VARS
+ Jerasure_LIBRARY
+ Jerasure_INCLUDE_DIR
+ Jerasure_INCLUDE_DIR2
+)
+
+if(Jerasure_FOUND)
+ set(Jerasure_LIBRARIES ${Jerasure_LIBRARY})
+ set(Jerasure_INCLUDE_DIRS ${Jerasure_INCLUDE_DIR} ${Jerasure_INCLUDE_DIR2})
+ if(NOT TARGET Jerasure::Jerasure)
+ add_library(Jerasure::Jerasure UNKNOWN IMPORTED)
+ set_target_properties(Jerasure::Jerasure PROPERTIES
+ IMPORTED_LOCATION "${Jerasure_LIBRARY}"
+ INTERFACE_INCLUDE_DIRECTORIES "${Jerasure_INCLUDE_DIRS}"
+ )
+ endif()
+endif()
diff --git a/cmake/FindNanopb.cmake b/cmake/FindNanopb.cmake
new file mode 100644
index 0000000..ed923f2
--- /dev/null
+++ b/cmake/FindNanopb.cmake
@@ -0,0 +1,18 @@
+# Provides Nanopb::nanopb and NANOPB_GENERATOR_PY
+
+find_package(nanopb CONFIG)
+find_file(NANOPB_GENERATOR_PY nanopb_generator.py PATH_SUFFIXES bin)
+
+include(FindPackageHandleStandardArgs)
+find_package_handle_standard_args(Nanopb
+ FOUND_VAR Nanopb_FOUND
+ REQUIRED_VARS
+ nanopb_FOUND
+ NANOPB_GENERATOR_PY
+)
+
+if(Nanopb_FOUND)
+ if(NOT TARGET Nanopb::nanopb)
+ add_library(Nanopb::nanopb ALIAS nanopb::protobuf-nanopb-static)
+ endif()
+endif()
diff --git a/cmake/FindSDL2.cmake b/cmake/FindSDL2.cmake
index 2bc61b5..03717b0 100644
--- a/cmake/FindSDL2.cmake
+++ b/cmake/FindSDL2.cmake
@@ -1,6 +1,26 @@
find_package(SDL2 NO_MODULE QUIET)
+# Adapted from libsdl-org/SDL_ttf: https://github.com/libsdl-org/SDL_ttf/blob/main/cmake/FindPrivateSDL2.cmake#L19-L31
+# Copyright (C) 1997-2022 Sam Lantinga
+# Licensed under the zlib license (https://github.com/libsdl-org/SDL_ttf/blob/main/LICENSE.txt)
+set(SDL2_VERSION_MAJOR)
+set(SDL2_VERSION_MINOR)
+set(SDL2_VERSION_PATCH)
+set(SDL2_VERSION)
+if(SDL2_INCLUDE_DIR)
+ file(READ "${SDL2_INCLUDE_DIR}/SDL_version.h" _sdl_version_h)
+ string(REGEX MATCH "#define[ \t]+SDL_MAJOR_VERSION[ \t]+([0-9]+)" _sdl2_major_re "${_sdl_version_h}")
+ set(SDL2_VERSION_MAJOR "${CMAKE_MATCH_1}")
+ string(REGEX MATCH "#define[ \t]+SDL_MINOR_VERSION[ \t]+([0-9]+)" _sdl2_minor_re "${_sdl_version_h}")
+ set(SDL2_VERSION_MINOR "${CMAKE_MATCH_1}")
+ string(REGEX MATCH "#define[ \t]+SDL_PATCHLEVEL[ \t]+([0-9]+)" _sdl2_patch_re "${_sdl_version_h}")
+ set(SDL2_VERSION_PATCH "${CMAKE_MATCH_1}")
+ if(_sdl2_major_re AND _sdl2_minor_re AND _sdl2_patch_re)
+ set(SDL2_VERSION "${SDL2_VERSION_MAJOR}.${SDL2_VERSION_MINOR}.${SDL2_VERSION_PATCH}")
+ endif()
+endif()
+
if(SDL2_FOUND AND (NOT TARGET SDL2::SDL2))
add_library(SDL2::SDL2 UNKNOWN IMPORTED GLOBAL)
if(NOT SDL2_LIBDIR)
@@ -8,6 +28,7 @@ if(SDL2_FOUND AND (NOT TARGET SDL2::SDL2))
endif()
find_library(SDL2_LIBRARY SDL2 PATHS "${SDL2_LIBDIR}" NO_DEFAULT_PATH)
if(SDL2_LIBRARY)
+ string(STRIP "${SDL2_LIBRARIES}" SDL2_LIBRARIES)
set_target_properties(SDL2::SDL2 PROPERTIES
IMPORTED_LOCATION "${SDL2_LIBRARY}"
IMPORTED_LINK_INTERFACE_LIBRARIES "${SDL2_LIBRARIES}"
@@ -19,4 +40,4 @@ if(SDL2_FOUND AND (NOT TARGET SDL2::SDL2))
endif()
include(FindPackageHandleStandardArgs)
-find_package_handle_standard_args(SDL2 DEFAULT_MSG)
+find_package_handle_standard_args(SDL2 CONFIG_MODE)
diff --git a/cmake/OpenSSLExternalProject.cmake b/cmake/OpenSSLExternalProject.cmake
new file mode 100644
index 0000000..1e1370d
--- /dev/null
+++ b/cmake/OpenSSLExternalProject.cmake
@@ -0,0 +1,52 @@
+
+include(ExternalProject)
+
+set(OPENSSL_INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/openssl-install-prefix")
+
+unset(OPENSSL_OS_COMPILER)
+unset(OPENSSL_CONFIG_EXTRA_ARGS)
+unset(OPENSSL_BUILD_ENV)
+
+if(ANDROID_ABI)
+ if(ANDROID_ABI STREQUAL "armeabi-v7a")
+ set(OPENSSL_OS_COMPILER "android-arm")
+ elseif(ANDROID_ABI STREQUAL "arm64-v8a")
+ set(OPENSSL_OS_COMPILER "android-arm64")
+ elseif(ANDROID_ABI STREQUAL "x86")
+ set(OPENSSL_OS_COMPILER "android-x86")
+ elseif(ANDROID_ABI STREQUAL "x86_64")
+ set(OPENSSL_OS_COMPILER "android-x86_64")
+ endif()
+
+ set(OPENSSL_CONFIG_EXTRA_ARGS "-D__ANDROID_API__=${ANDROID_NATIVE_API_LEVEL}")
+ get_filename_component(ANDROID_NDK_BIN_PATH "${CMAKE_C_COMPILER}" DIRECTORY)
+ set(OPENSSL_BUILD_ENV "ANDROID_NDK_HOME=${ANDROID_NDK}" "PATH=${ANDROID_NDK_BIN_PATH}:$ENV{PATH}")
+else()
+ if(UNIX AND NOT APPLE AND CMAKE_SIZEOF_VOID_P STREQUAL "8")
+ set(OPENSSL_OS_COMPILER "linux-x86_64")
+ endif()
+endif()
+
+if(NOT OPENSSL_OS_COMPILER)
+ message(FATAL_ERROR "Failed to match OPENSSL_OS_COMPILER")
+endif()
+
+find_program(MAKE_EXE NAMES gmake make)
+ExternalProject_Add(OpenSSL-ExternalProject
+ URL https://www.openssl.org/source/openssl-1.1.1s.tar.gz
+ URL_HASH SHA256=c5ac01e760ee6ff0dab61d6b2bbd30146724d063eb322180c6f18a6f74e4b6aa
+ INSTALL_DIR "${OPENSSL_INSTALL_DIR}"
+ CONFIGURE_COMMAND ${CMAKE_COMMAND} -E env ${OPENSSL_BUILD_ENV}
+ "/Configure" "--prefix=" no-shared ${OPENSSL_CONFIG_EXTRA_ARGS} "${OPENSSL_OS_COMPILER}"
+ BUILD_COMMAND ${CMAKE_COMMAND} -E env ${OPENSSL_BUILD_ENV} "${MAKE_EXE}" -j4 build_libs
+ INSTALL_COMMAND ${CMAKE_COMMAND} -E env ${OPENSSL_BUILD_ENV} "${MAKE_EXE}" install_dev)
+
+add_library(OpenSSL_Crypto INTERFACE)
+add_dependencies(OpenSSL_Crypto OpenSSL-ExternalProject)
+if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.13.0")
+ target_link_directories(OpenSSL_Crypto INTERFACE "${OPENSSL_INSTALL_DIR}/lib")
+else()
+ link_directories("${OPENSSL_INSTALL_DIR}/lib")
+endif()
+target_link_libraries(OpenSSL_Crypto INTERFACE crypto)
+target_include_directories(OpenSSL_Crypto INTERFACE "${OPENSSL_INSTALL_DIR}/include")
diff --git a/cmake/switch.cmake b/cmake/switch.cmake
new file mode 100644
index 0000000..7e600be
--- /dev/null
+++ b/cmake/switch.cmake
@@ -0,0 +1,62 @@
+
+# Find DEVKITPRO
+set(DEVKITPRO "$ENV{DEVKITPRO}" CACHE PATH "Path to DevKitPro")
+if(NOT DEVKITPRO)
+ message(FATAL_ERROR "Please set DEVKITPRO env before calling cmake. https://devkitpro.org/wiki/Getting_Started")
+endif()
+
+# include devkitpro toolchain
+include("${DEVKITPRO}/cmake/Switch.cmake")
+
+set(NSWITCH TRUE)
+
+# troubleshoot
+message(STATUS "CMAKE_FIND_ROOT_PATH = ${CMAKE_FIND_ROOT_PATH}")
+message(STATUS "PKG_CONFIG_EXECUTABLE = ${PKG_CONFIG_EXECUTABLE}")
+message(STATUS "CMAKE_EXE_LINKER_FLAGS = ${CMAKE_EXE_LINKER_FLAGS}")
+get_property(include_directories DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
+message(STATUS "INCLUDE_DIRECTORIES = ${include_directories}")
+message(STATUS "CMAKE_C_COMPILER = ${CMAKE_C_COMPILER}")
+message(STATUS "CMAKE_CXX_COMPILER = ${CMAKE_CXX_COMPILER}")
+
+find_program(ELF2NRO elf2nro ${DEVKITPRO}/tools/bin)
+if (ELF2NRO)
+ message(STATUS "elf2nro: ${ELF2NRO} - found")
+else()
+ message(WARNING "elf2nro - not found")
+endif()
+
+find_program(NACPTOOL nacptool ${DEVKITPRO}/tools/bin)
+if (NACPTOOL)
+ message(STATUS "nacptool: ${NACPTOOL} - found")
+else()
+ message(WARNING "nacptool - not found")
+endif()
+
+function(__add_nacp target APP_TITLE APP_AUTHOR APP_VERSION)
+ set(__NACP_COMMAND ${NACPTOOL} --create ${APP_TITLE} ${APP_AUTHOR} ${APP_VERSION} ${CMAKE_CURRENT_BINARY_DIR}/${target})
+
+ add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${target}
+ COMMAND ${__NACP_COMMAND}
+ WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
+ VERBATIM
+ )
+endfunction()
+
+function(add_nro_target output_name target title author version icon romfs)
+ if(NOT ${output_name}.nacp)
+ __add_nacp(${output_name}.nacp ${title} ${author} ${version})
+ endif()
+ add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${output_name}.nro
+ COMMAND ${ELF2NRO} $
+ ${CMAKE_CURRENT_BINARY_DIR}/${output_name}.nro
+ --icon=${icon}
+ --nacp=${CMAKE_CURRENT_BINARY_DIR}/${output_name}.nacp
+ --romfsdir=${romfs}
+ DEPENDS ${target} ${CMAKE_CURRENT_BINARY_DIR}/${output_name}.nacp
+ VERBATIM
+ )
+ add_custom_target(${output_name}_nro ALL SOURCES ${CMAKE_CURRENT_BINARY_DIR}/${output_name}.nro)
+endfunction()
+
+set(CMAKE_USE_SYSTEM_ENVIRONMENT_PATH OFF)
diff --git a/doc/platform-build.md b/doc/platform-build.md
new file mode 100644
index 0000000..942cc6b
--- /dev/null
+++ b/doc/platform-build.md
@@ -0,0 +1,30 @@
+
+# Platform-specific build instructions
+
+## Fedora
+
+On Fedora, build dependencies can be installed via:
+
+```
+sudo dnf install cmake make qt5-qtmultimedia-devel qt5-qtsvg-devel qt5-qtbase-gui ffmpeg-devel opus-devel openssl-devel python3-protobuf protobuf-c protobuf-devel qt5-rpm-macros SDL2-devel libevdev-devel systemd-devel
+```
+
+Then, Chiaki builds just like any other CMake project:
+```
+git submodule update --init
+mkdir build && cd build
+cmake ..
+make
+```
+
+In order to utilize hardware decoding, necessary VA-API component needs to be installed separately depending on your GPU. For example on Fedora:
+
+* **Intel**: `libva-intel-driver`(majority laptop and desktop) OR `libva-intel-hybrid-driver`(most netbook with Atom processor)
+* **AMD**: Already part of default installation
+* **Nvidia**: `libva-vdpau-driver`
+
+## Windows
+
+Windows support is reduced to the absolute minimum for maintainability.
+Official Windows builds are done on AppVeyor within MSYS2 using this script, which can also work as a template for building locally: [scripts/appveyor.sh](../scripts/appveyor.sh).
+Other methods of building may work, but will not be officially supported.
diff --git a/gui/CMakeLists.txt b/gui/CMakeLists.txt
index 45bc9df..443150f 100644
--- a/gui/CMakeLists.txt
+++ b/gui/CMakeLists.txt
@@ -2,23 +2,15 @@
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
-find_package(Qt5 REQUIRED COMPONENTS Core Widgets Gui Multimedia OpenGL Svg)
-if(CHIAKI_GUI_ENABLE_QT_GAMEPAD AND CHIAKI_GUI_ENABLE_SDL_GAMECONTROLLER)
- message(FATAL_ERROR "Only one input method may be enabled. Disable either CHIAKI_GUI_ENABLE_SDL_GAMECONTROLLER or CHIAKI_GUI_ENABLE_QT_GAMEPAD.")
-endif()
-if(CHIAKI_GUI_ENABLE_QT_GAMEPAD)
- find_package(Qt5 REQUIRED COMPONENTS Gamepad)
-endif()
-if(CHIAKI_GUI_ENABLE_SDL_GAMECONTROLLER)
- find_package(SDL2 MODULE REQUIRED)
+find_package(Qt5 REQUIRED COMPONENTS Core Widgets Gui Concurrent Multimedia OpenGL Svg)
+if(APPLE)
+ find_package(Qt5 REQUIRED COMPONENTS MacExtras)
endif()
if(WIN32)
add_definitions(-DWIN32_LEAN_AND_MEAN)
endif()
-find_package(FFMPEG REQUIRED COMPONENTS avcodec avutil)
-
set(RESOURCE_FILES "")
if(APPLE)
@@ -31,8 +23,6 @@ add_executable(chiaki WIN32
src/main.cpp
include/streamwindow.h
src/streamwindow.cpp
- include/videodecoder.h
- src/videodecoder.cpp
include/mainwindow.h
src/mainwindow.cpp
include/dynamicgridwidget.h
@@ -57,38 +47,48 @@ add_executable(chiaki WIN32
src/registdialog.cpp
include/host.h
src/host.cpp
+ include/settingskeycapturedialog.h
+ src/settingskeycapturedialog.cpp
include/settingsdialog.h
src/settingsdialog.cpp
include/manualhostdialog.h
src/manualhostdialog.cpp
res/resources.qrc
include/controllermanager.h
- src/controllermanager.cpp)
+ src/controllermanager.cpp
+ include/loginpindialog.h
+ src/loginpindialog.cpp)
target_include_directories(chiaki PRIVATE include)
target_link_libraries(chiaki chiaki-lib)
if(CHIAKI_ENABLE_CLI)
- add_definitions(CHIAKI_ENABLE_CLI)
+ add_definitions(-DCHIAKI_ENABLE_CLI)
target_link_libraries(chiaki chiaki-cli-lib)
endif()
-target_link_libraries(chiaki FFMPEG::avcodec FFMPEG::avutil)
-target_link_libraries(chiaki Qt5::Core Qt5::Widgets Qt5::Gui Qt5::Multimedia Qt5::OpenGL Qt5::Svg)
-if(CHIAKI_GUI_ENABLE_QT_GAMEPAD)
- target_link_libraries(chiaki Qt5::Gamepad)
- target_compile_definitions(chiaki PRIVATE CHIAKI_GUI_ENABLE_QT_GAMEPAD)
+target_link_libraries(chiaki Qt5::Core Qt5::Widgets Qt5::Gui Qt5::Concurrent Qt5::Multimedia Qt5::OpenGL Qt5::Svg)
+target_link_libraries(chiaki SDL2::SDL2)
+if(APPLE)
+ target_link_libraries(chiaki Qt5::MacExtras)
+ target_compile_definitions(chiaki PRIVATE CHIAKI_GUI_ENABLE_QT_MACEXTRAS)
endif()
if(CHIAKI_GUI_ENABLE_SDL_GAMECONTROLLER)
- target_link_libraries(chiaki SDL2::SDL2)
target_compile_definitions(chiaki PRIVATE CHIAKI_GUI_ENABLE_SDL_GAMECONTROLLER)
endif()
+if(CHIAKI_ENABLE_SETSU)
+ target_link_libraries(chiaki setsu)
+ target_compile_definitions(chiaki PRIVATE CHIAKI_GUI_ENABLE_SETSU)
+endif()
+if(NOT CHIAKI_ENABLE_FFMPEG_DECODER)
+ message(FATAL_ERROR "Chiaki GUI requires CHIAKI_ENABLE_FFMPEG_DECODER=ON")
+endif()
set_target_properties(chiaki PROPERTIES
MACOSX_BUNDLE TRUE
MACOSX_BUNDLE_INFO_PLIST "${CMAKE_CURRENT_SOURCE_DIR}/MacOSXBundleInfo.plist.in"
MACOSX_BUNDLE_BUNDLE_NAME Chiaki
MACOSX_BUNDLE_BUNDLE_VERSION ${CHIAKI_VERSION}
- MACOSX_BUNDLE_COPYRIGHT "thestr4ng3r (GPLv3)"
+ MACOSX_BUNDLE_COPYRIGHT "thestr4ng3r (AGPLv3)"
MACOSX_BUNDLE_GUI_IDENTIFIER "org.chiaki.chiaki"
MACOSX_BUNDLE_ICON_FILE chiaki.icns
RESOURCE "${RESOURCE_FILES}")
@@ -97,4 +97,5 @@ install(TARGETS chiaki
RUNTIME DESTINATION bin
BUNDLE DESTINATION bin)
install(FILES chiaki.desktop DESTINATION share/applications)
-install(FILES chiaki.png DESTINATION share/icons/hicolor/512x512)
+install(FILES chiaki.png DESTINATION share/icons/hicolor/512x512/apps)
+install(FILES re.chiaki.Chiaki.appdata.xml DESTINATION share/metainfo)
diff --git a/gui/chiaki.desktop b/gui/chiaki.desktop
index 6ee0326..53f3be2 100644
--- a/gui/chiaki.desktop
+++ b/gui/chiaki.desktop
@@ -1,7 +1,7 @@
[Desktop Entry]
Type=Application
Name=Chiaki
-Comment=PlayStation 4 Remote Play Client
+Comment=PlayStation Remote Play Client
Exec=chiaki
Icon=chiaki
-Categories=Game;
\ No newline at end of file
+Categories=Game;
diff --git a/gui/chiaki.icns b/gui/chiaki.icns
index 13613ff..e51fa65 100644
Binary files a/gui/chiaki.icns and b/gui/chiaki.icns differ
diff --git a/gui/include/avopenglframeuploader.h b/gui/include/avopenglframeuploader.h
index ff7e0d0..1e37054 100644
--- a/gui/include/avopenglframeuploader.h
+++ b/gui/include/avopenglframeuploader.h
@@ -1,19 +1,4 @@
-/*
- * This file is part of Chiaki.
- *
- * Chiaki is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Chiaki is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Chiaki. If not, see .
- */
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
#ifndef CHIAKI_AVOPENGLFRAMEUPLOADER_H
#define CHIAKI_AVOPENGLFRAMEUPLOADER_H
@@ -21,8 +6,10 @@
#include
#include
+#include
+
+class StreamSession;
class AVOpenGLWidget;
-class VideoDecoder;
class QSurface;
class AVOpenGLFrameUploader: public QObject
@@ -30,16 +17,16 @@ class AVOpenGLFrameUploader: public QObject
Q_OBJECT
private:
- VideoDecoder *decoder;
+ StreamSession *session;
AVOpenGLWidget *widget;
QOpenGLContext *context;
QSurface *surface;
private slots:
- void UpdateFrame();
+ void UpdateFrameFromDecoder();
public:
- AVOpenGLFrameUploader(VideoDecoder *decoder, AVOpenGLWidget *widget, QOpenGLContext *context, QSurface *surface);
+ AVOpenGLFrameUploader(StreamSession *session, AVOpenGLWidget *widget, QOpenGLContext *context, QSurface *surface);
};
#endif // CHIAKI_AVOPENGLFRAMEUPLOADER_H
diff --git a/gui/include/avopenglwidget.h b/gui/include/avopenglwidget.h
index 9583b4d..19bd287 100644
--- a/gui/include/avopenglwidget.h
+++ b/gui/include/avopenglwidget.h
@@ -1,23 +1,10 @@
-/*
- * This file is part of Chiaki.
- *
- * Chiaki is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Chiaki is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Chiaki. If not, see .
- */
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
#ifndef CHIAKI_AVOPENGLWIDGET_H
#define CHIAKI_AVOPENGLWIDGET_H
+#include "transformmode.h"
+
#include
#include
@@ -28,15 +15,37 @@ extern "C"
#include
}
-class VideoDecoder;
+#define MAX_PANES 3
+
+class StreamSession;
class AVOpenGLFrameUploader;
+class QOffscreenSurface;
+
+struct PlaneConfig
+{
+ unsigned int width_divider;
+ unsigned int height_divider;
+ unsigned int data_per_pixel;
+ GLint internal_format;
+ GLenum format;
+};
+
+struct ConversionConfig
+{
+ enum AVPixelFormat pixel_format;
+ const char *shader_vert_glsl;
+ const char *shader_frag_glsl;
+ unsigned int planes;
+ struct PlaneConfig plane_configs[MAX_PANES];
+};
struct AVOpenGLFrame
{
- GLuint pbo[3];
- GLuint tex[3];
+ GLuint pbo[MAX_PANES];
+ GLuint tex[MAX_PANES];
unsigned int width;
unsigned int height;
+ ConversionConfig *conversion_config;
bool Update(AVFrame *frame, ChiakiLog *log);
};
@@ -46,7 +55,7 @@ class AVOpenGLWidget: public QOpenGLWidget
Q_OBJECT
private:
- VideoDecoder *decoder;
+ StreamSession *session;
GLuint program;
GLuint vbo;
@@ -55,22 +64,37 @@ class AVOpenGLWidget: public QOpenGLWidget
AVOpenGLFrame frames[2];
int frame_fg;
QMutex frames_mutex;
+ QOffscreenSurface *frame_uploader_surface;
QOpenGLContext *frame_uploader_context;
AVOpenGLFrameUploader *frame_uploader;
QThread *frame_uploader_thread;
+ QTimer *mouse_timer;
+
+ ConversionConfig *conversion_config;
+
public:
static QSurfaceFormat CreateSurfaceFormat();
- explicit AVOpenGLWidget(VideoDecoder *decoder, QWidget *parent = nullptr);
+ explicit AVOpenGLWidget(StreamSession *session, QWidget *parent = nullptr, TransformMode transform_mode = TransformMode::Fit);
~AVOpenGLWidget() override;
void SwapFrames();
AVOpenGLFrame *GetBackgroundFrame() { return &frames[1 - frame_fg]; }
+ void SetTransformMode(TransformMode mode) { transform_mode = mode; }
+ TransformMode GetTransformMode() const { return transform_mode; }
+
protected:
+ TransformMode transform_mode;
+ void mouseMoveEvent(QMouseEvent *event) override;
+
void initializeGL() override;
void paintGL() override;
+
+ public slots:
+ void ResetMouseTimeout();
+ void HideMouse();
};
#endif // CHIAKI_AVOPENGLWIDGET_H
diff --git a/gui/include/controllermanager.h b/gui/include/controllermanager.h
index 952275e..eee0120 100644
--- a/gui/include/controllermanager.h
+++ b/gui/include/controllermanager.h
@@ -1,19 +1,4 @@
-/*
- * This file is part of Chiaki.
- *
- * Chiaki is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Chiaki is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Chiaki. If not, see .
- */
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
#ifndef CHIAKI_CONTROLLERMANAGER_H
#define CHIAKI_CONTROLLERMANAGER_H
@@ -27,8 +12,12 @@
#ifdef CHIAKI_GUI_ENABLE_SDL_GAMECONTROLLER
#include
+#include
#endif
+#define PS_TOUCHPAD_MAX_X 1920
+#define PS_TOUCHPAD_MAX_Y 1079
+
class Controller;
class ControllerManager : public QObject
@@ -48,7 +37,9 @@ class ControllerManager : public QObject
private slots:
void UpdateAvailableControllers();
void HandleEvents();
- void ControllerEvent(int device_id);
+#ifdef CHIAKI_GUI_ENABLE_SDL_GAMECONTROLLER
+ void ControllerEvent(SDL_Event evt);
+#endif
public:
static ControllerManager *GetInstance();
@@ -56,7 +47,7 @@ class ControllerManager : public QObject
ControllerManager(QObject *parent = nullptr);
~ControllerManager();
- QList GetAvailableControllers();
+ QSet GetAvailableControllers();
Controller *OpenController(int device_id);
signals:
@@ -72,12 +63,24 @@ class Controller : public QObject
private:
Controller(int device_id, ControllerManager *manager);
- void UpdateState();
+#ifdef CHIAKI_GUI_ENABLE_SDL_GAMECONTROLLER
+ void UpdateState(SDL_Event event);
+ bool HandleButtonEvent(SDL_ControllerButtonEvent event);
+ bool HandleAxisEvent(SDL_ControllerAxisEvent event);
+#if SDL_VERSION_ATLEAST(2, 0, 14)
+ bool HandleSensorEvent(SDL_ControllerSensorEvent event);
+ bool HandleTouchpadEvent(SDL_ControllerTouchpadEvent event);
+#endif
+#endif
ControllerManager *manager;
int id;
+ ChiakiOrientationTracker orientation_tracker;
+ ChiakiControllerState state;
+ bool is_dualsense;
#ifdef CHIAKI_GUI_ENABLE_SDL_GAMECONTROLLER
+ QMap, uint8_t> touch_ids;
SDL_GameController *controller;
#endif
@@ -88,9 +91,44 @@ class Controller : public QObject
int GetDeviceID();
QString GetName();
ChiakiControllerState GetState();
+ void SetRumble(uint8_t left, uint8_t right);
+ void SetTriggerEffects(uint8_t type_left, const uint8_t *data_left, uint8_t type_right, const uint8_t *data_right);
+ bool IsDualSense();
signals:
void StateChanged();
};
+/* PS5 trigger effect documentation:
+ https://controllers.fandom.com/wiki/Sony_DualSense#FFB_Trigger_Modes
+
+ Taken from SDL2, licensed under the zlib license,
+ Copyright (C) 1997-2022 Sam Lantinga
+ https://github.com/libsdl-org/SDL/blob/release-2.24.1/test/testgamecontroller.c#L263-L289
+*/
+typedef struct
+{
+ Uint8 ucEnableBits1; /* 0 */
+ Uint8 ucEnableBits2; /* 1 */
+ Uint8 ucRumbleRight; /* 2 */
+ Uint8 ucRumbleLeft; /* 3 */
+ Uint8 ucHeadphoneVolume; /* 4 */
+ Uint8 ucSpeakerVolume; /* 5 */
+ Uint8 ucMicrophoneVolume; /* 6 */
+ Uint8 ucAudioEnableBits; /* 7 */
+ Uint8 ucMicLightMode; /* 8 */
+ Uint8 ucAudioMuteBits; /* 9 */
+ Uint8 rgucRightTriggerEffect[11]; /* 10 */
+ Uint8 rgucLeftTriggerEffect[11]; /* 21 */
+ Uint8 rgucUnknown1[6]; /* 32 */
+ Uint8 ucLedFlags; /* 38 */
+ Uint8 rgucUnknown2[2]; /* 39 */
+ Uint8 ucLedAnim; /* 41 */
+ Uint8 ucLedBrightness; /* 42 */
+ Uint8 ucPadLights; /* 43 */
+ Uint8 ucLedRed; /* 44 */
+ Uint8 ucLedGreen; /* 45 */
+ Uint8 ucLedBlue; /* 46 */
+} DS5EffectsState_t;
+
#endif // CHIAKI_CONTROLLERMANAGER_H
diff --git a/gui/include/discoverymanager.h b/gui/include/discoverymanager.h
index 05cb0f7..be3462c 100644
--- a/gui/include/discoverymanager.h
+++ b/gui/include/discoverymanager.h
@@ -1,19 +1,4 @@
-/*
- * This file is part of Chiaki.
- *
- * Chiaki is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Chiaki is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Chiaki. If not, see .
- */
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
#ifndef CHIAKI_DISCOVERYMANAGER_H
#define CHIAKI_DISCOVERYMANAGER_H
@@ -27,6 +12,7 @@
struct DiscoveryHost
{
+ bool ps5;
ChiakiDiscoveryHostState state;
uint16_t host_request_port;
#define STRING_MEMBER(name) QString name;
@@ -59,7 +45,7 @@ class DiscoveryManager : public QObject
void SetActive(bool active);
- void SendWakeup(const QString &host, const QByteArray ®ist_key);
+ void SendWakeup(const QString &host, const QByteArray ®ist_key, bool ps5);
const QList GetHosts() const { return hosts; }
diff --git a/gui/include/dynamicgridwidget.h b/gui/include/dynamicgridwidget.h
index efae63f..6a9330d 100644
--- a/gui/include/dynamicgridwidget.h
+++ b/gui/include/dynamicgridwidget.h
@@ -1,19 +1,4 @@
-/*
- * This file is part of Chiaki.
- *
- * Chiaki is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Chiaki is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Chiaki. If not, see .
- */
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
#ifndef CHIAKI_DYNAMICGRIDWIDGET_H
#define CHIAKI_DYNAMICGRIDWIDGET_H
diff --git a/gui/include/exception.h b/gui/include/exception.h
index 7d0a615..2ffe45c 100644
--- a/gui/include/exception.h
+++ b/gui/include/exception.h
@@ -1,19 +1,4 @@
-/*
- * This file is part of Chiaki.
- *
- * Chiaki is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Chiaki is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Chiaki. If not, see .
- */
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
#ifndef CHIAKI_EXCEPTION_H
#define CHIAKI_EXCEPTION_H
diff --git a/gui/include/host.h b/gui/include/host.h
index 7d143f4..8ff292e 100644
--- a/gui/include/host.h
+++ b/gui/include/host.h
@@ -1,19 +1,4 @@
-/*
- * This file is part of Chiaki.
- *
- * Chiaki is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Chiaki is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Chiaki. If not, see .
- */
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
#ifndef CHIAKI_HOST_H
#define CHIAKI_HOST_H
@@ -53,12 +38,13 @@ static bool operator<(const HostMAC &a, const HostMAC &b) { return a.GetValue()
class RegisteredHost
{
private:
+ ChiakiTarget target;
QString ap_ssid;
QString ap_bssid;
QString ap_key;
QString ap_name;
- HostMAC ps4_mac;
- QString ps4_nickname;
+ HostMAC server_mac;
+ QString server_nickname;
char rp_regist_key[CHIAKI_SESSION_AUTH_SIZE];
uint32_t rp_key_type;
uint8_t rp_key[0x10];
@@ -69,8 +55,9 @@ class RegisteredHost
RegisteredHost(const ChiakiRegisteredHost &chiaki_host);
- const HostMAC &GetPS4MAC() const { return ps4_mac; }
- const QString &GetPS4Nickname() const { return ps4_nickname; }
+ ChiakiTarget GetTarget() const { return target; }
+ const HostMAC &GetServerMAC() const { return server_mac; }
+ const QString &GetServerNickname() const { return server_nickname; }
const QByteArray GetRPRegistKey() const { return QByteArray(rp_regist_key, sizeof(rp_regist_key)); }
const QByteArray GetRPKey() const { return QByteArray((const char *)rp_key, sizeof(rp_key)); }
@@ -96,7 +83,7 @@ class ManualHost
bool GetRegistered() const { return registered; }
HostMAC GetMAC() const { return registered_mac; }
- void Register(const RegisteredHost ®istered_host) { this->registered = true; this->registered_mac = registered_host.GetPS4MAC(); }
+ void Register(const RegisteredHost ®istered_host) { this->registered = true; this->registered_mac = registered_host.GetServerMAC(); }
void SaveToSettings(QSettings *settings) const;
static ManualHost LoadFromSettings(QSettings *settings);
diff --git a/gui/include/loginpindialog.h b/gui/include/loginpindialog.h
new file mode 100644
index 0000000..cc3a411
--- /dev/null
+++ b/gui/include/loginpindialog.h
@@ -0,0 +1,29 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+#ifndef CHIAKI_LOGINPINDIALOG_H
+#define CHIAKI_LOGINPINDIALOG_H
+
+#include
+
+class QLineEdit;
+class QDialogButtonBox;
+
+class LoginPINDialog : public QDialog
+{
+ Q_OBJECT
+
+ private:
+ QString pin;
+
+ QLineEdit *pin_edit;
+ QDialogButtonBox *button_box;
+
+ void UpdateButtons();
+
+ public:
+ explicit LoginPINDialog(bool incorrect, QWidget *parent = nullptr);
+
+ QString GetPIN() { return pin; }
+};
+
+#endif // CHIAKI_LOGINPINDIALOG_H
diff --git a/gui/include/logsniffer.h b/gui/include/logsniffer.h
new file mode 100644
index 0000000..aa398a0
--- /dev/null
+++ b/gui/include/logsniffer.h
@@ -0,0 +1,81 @@
+// SPDX-License-Identifier: LicenseRef-GPL-3.0-or-later-OpenSSL
+
+#ifndef CHIAKI_LOG_SNIFFER_H
+#define CHIAKI_LOG_SNIFFER_H
+
+#include
+
+#include
+#include
+#include
+#include
+
+#ifdef CHIAKI_GUI_ENABLE_SDL_GAMECONTROLLER
+#include
+#endif
+
+class Controller;
+
+class ControllerManager : public QObject
+{
+ Q_OBJECT
+
+ friend class Controller;
+
+ private:
+#ifdef CHIAKI_GUI_ENABLE_SDL_GAMECONTROLLER
+ QSet available_controllers;
+#endif
+ QMap open_controllers;
+
+ void ControllerClosed(Controller *controller);
+
+ private slots:
+ void UpdateAvailableControllers();
+ void HandleEvents();
+ void ControllerEvent(int device_id);
+
+ public:
+ static ControllerManager *GetInstance();
+
+ ControllerManager(QObject *parent = nullptr);
+ ~ControllerManager();
+
+ QSet GetAvailableControllers();
+ Controller *OpenController(int device_id);
+
+ signals:
+ void AvailableControllersUpdated();
+};
+
+class Controller : public QObject
+{
+ Q_OBJECT
+
+ friend class ControllerManager;
+
+ private:
+ Controller(int device_id, ControllerManager *manager);
+
+ void UpdateState();
+
+ ControllerManager *manager;
+ int id;
+
+#ifdef CHIAKI_GUI_ENABLE_SDL_GAMECONTROLLER
+ SDL_GameController *controller;
+#endif
+
+ public:
+ ~Controller();
+
+ bool IsConnected();
+ int GetDeviceID();
+ QString GetName();
+ ChiakiControllerState GetState();
+
+ signals:
+ void StateChanged();
+};
+
+#endif // CHIAKI_CONTROLLERMANAGER_H
diff --git a/gui/include/mainwindow.h b/gui/include/mainwindow.h
index a91c3c5..4112c19 100644
--- a/gui/include/mainwindow.h
+++ b/gui/include/mainwindow.h
@@ -1,19 +1,4 @@
-/*
- * This file is part of Chiaki.
- *
- * Chiaki is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Chiaki is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Chiaki. If not, see .
- */
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
#ifndef CHIAKI_MAINWINDOW_H
#define CHIAKI_MAINWINDOW_H
@@ -37,6 +22,8 @@ struct DisplayServer
bool registered;
QString GetHostAddr() const { return discovered ? discovery_host.host_addr : manual_host.GetHost(); }
+ bool IsPS5() const { return discovered ? discovery_host.ps5 :
+ (registered ? chiaki_target_is_ps5(registered_host.GetTarget()) : false); }
};
class MainWindow : public QMainWindow
@@ -46,6 +33,8 @@ class MainWindow : public QMainWindow
private:
Settings *settings;
+ QIcon discover_action_icon;
+ QIcon discover_action_off_icon;
QAction *discover_action;
DynamicGridWidget *grid_widget;
@@ -66,6 +55,7 @@ class MainWindow : public QMainWindow
void UpdateDiscoveryEnabled();
void ShowSettings();
+ void Quit();
void UpdateDisplayServers();
void UpdateServerWidgets();
diff --git a/gui/include/manualhostdialog.h b/gui/include/manualhostdialog.h
index eeb3c03..b56cc8e 100644
--- a/gui/include/manualhostdialog.h
+++ b/gui/include/manualhostdialog.h
@@ -1,19 +1,4 @@
-/*
- * This file is part of Chiaki.
- *
- * Chiaki is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Chiaki is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Chiaki. If not, see .
- */
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
#ifndef CHIAKI_MANUALHOSTDIALOG_H
#define CHIAKI_MANUALHOSTDIALOG_H
diff --git a/gui/include/registdialog.h b/gui/include/registdialog.h
index fea4578..7d16b0f 100644
--- a/gui/include/registdialog.h
+++ b/gui/include/registdialog.h
@@ -1,19 +1,4 @@
-/*
- * This file is part of Chiaki.
- *
- * Chiaki is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Chiaki is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Chiaki. If not, see .
- */
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
#ifndef CHIAKI_REGISTDIALOG_H
#define CHIAKI_REGISTDIALOG_H
@@ -30,6 +15,7 @@ class QLineEdit;
class QPlainTextEdit;
class QDialogButtonBox;
class QCheckBox;
+class QRadioButton;
class RegistDialog : public QDialog
{
@@ -40,13 +26,20 @@ class RegistDialog : public QDialog
QLineEdit *host_edit;
QCheckBox *broadcast_check_box;
- QLineEdit *psn_id_edit;
+ QRadioButton *ps4_pre9_radio_button;
+ QRadioButton *ps4_pre10_radio_button;
+ QRadioButton *ps4_10_radio_button;
+ QRadioButton *ps5_radio_button;
+ QLineEdit *psn_online_id_edit;
+ QLineEdit *psn_account_id_edit;
QLineEdit *pin_edit;
QDialogButtonBox *button_box;
QPushButton *register_button;
RegisteredHost registered_host;
+ bool NeedAccountId();
+
private slots:
void ValidateInput();
diff --git a/gui/include/servericonwidget.h b/gui/include/servericonwidget.h
index 0a714b4..364cb4b 100644
--- a/gui/include/servericonwidget.h
+++ b/gui/include/servericonwidget.h
@@ -1,19 +1,4 @@
-/*
- * This file is part of Chiaki.
- *
- * Chiaki is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Chiaki is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Chiaki. If not, see .
- */
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
#ifndef CHIAKI_SERVERICONWIDGET_H
#define CHIAKI_SERVERICONWIDGET_H
@@ -21,13 +6,18 @@
#include
#include
+#include
class ServerIconWidget : public QWidget
{
Q_OBJECT
private:
+ bool ps5 = false;
ChiakiDiscoveryHostState state = CHIAKI_DISCOVERY_HOST_STATE_UNKNOWN;
+ QSvgRenderer svg_renderer;
+
+ void LoadSvg();
protected:
void paintEvent(QPaintEvent *event) override;
@@ -35,7 +25,7 @@ class ServerIconWidget : public QWidget
public:
explicit ServerIconWidget(QWidget *parent = nullptr);
- void SetState(ChiakiDiscoveryHostState state) { this->state = state; update(); }
+ void SetState(bool ps5, ChiakiDiscoveryHostState state);
};
#endif // CHIAKI_SERVERICONWIDGET_H
diff --git a/gui/include/serveritemwidget.h b/gui/include/serveritemwidget.h
index 76f4258..24a8ba1 100644
--- a/gui/include/serveritemwidget.h
+++ b/gui/include/serveritemwidget.h
@@ -1,19 +1,4 @@
-/*
- * This file is part of Chiaki.
- *
- * Chiaki is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Chiaki is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Chiaki. If not, see .
- */
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
#ifndef CHIAKI_SERVERITEMWIDGET_H
#define CHIAKI_SERVERITEMWIDGET_H
diff --git a/gui/include/sessionlog.h b/gui/include/sessionlog.h
index 6977460..e498bb3 100644
--- a/gui/include/sessionlog.h
+++ b/gui/include/sessionlog.h
@@ -1,19 +1,4 @@
-/*
- * This file is part of Chiaki.
- *
- * Chiaki is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Chiaki is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Chiaki. If not, see .
- */
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
#ifndef CHIAKI_SESSIONLOG_H
#define CHIAKI_SESSIONLOG_H
diff --git a/gui/include/settings.h b/gui/include/settings.h
index a533f6a..00f38ab 100644
--- a/gui/include/settings.h
+++ b/gui/include/settings.h
@@ -1,19 +1,4 @@
-/*
- * This file is part of Chiaki.
- *
- * Chiaki is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Chiaki is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Chiaki. If not, see .
- */
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
#ifndef CHIAKI_SETTINGS_H
#define CHIAKI_SETTINGS_H
@@ -23,6 +8,33 @@
#include "host.h"
#include
+#include
+
+enum class ControllerButtonExt
+{
+ // must not overlap with ChiakiControllerButton and ChiakiControllerAnalogButton
+ ANALOG_STICK_LEFT_X_UP = (1 << 18),
+ ANALOG_STICK_LEFT_X_DOWN = (1 << 19),
+ ANALOG_STICK_LEFT_Y_UP = (1 << 20),
+ ANALOG_STICK_LEFT_Y_DOWN = (1 << 21),
+ ANALOG_STICK_RIGHT_X_UP = (1 << 22),
+ ANALOG_STICK_RIGHT_X_DOWN = (1 << 23),
+ ANALOG_STICK_RIGHT_Y_UP = (1 << 24),
+ ANALOG_STICK_RIGHT_Y_DOWN = (1 << 25),
+};
+
+enum class DisconnectAction
+{
+ AlwaysNothing,
+ AlwaysSleep,
+ Ask
+};
+
+enum class Decoder
+{
+ Ffmpeg,
+ Pi
+};
class Settings : public QObject
{
@@ -51,12 +63,51 @@ class Settings : public QObject
void SetLogVerbose(bool enabled) { settings.setValue("settings/log_verbose", enabled); }
uint32_t GetLogLevelMask();
+ bool GetDualSenseEnabled() const { return settings.value("settings/dualsense_enabled", false).toBool(); }
+ void SetDualSenseEnabled(bool enabled) { settings.setValue("settings/dualsense_enabled", enabled); }
+
ChiakiVideoResolutionPreset GetResolution() const;
void SetResolution(ChiakiVideoResolutionPreset resolution);
+ /**
+ * @return 0 if set to "automatic"
+ */
ChiakiVideoFPSPreset GetFPS() const;
void SetFPS(ChiakiVideoFPSPreset fps);
+ unsigned int GetBitrate() const;
+ void SetBitrate(unsigned int bitrate);
+
+ ChiakiCodec GetCodec() const;
+ void SetCodec(ChiakiCodec codec);
+
+ Decoder GetDecoder() const;
+ void SetDecoder(Decoder decoder);
+
+ QString GetHardwareDecoder() const;
+ void SetHardwareDecoder(const QString &hw_decoder);
+
+ unsigned int GetAudioBufferSizeDefault() const;
+
+ /**
+ * @return 0 if set to "automatic"
+ */
+ unsigned int GetAudioBufferSizeRaw() const;
+
+ /**
+ * @return actual size to be used, default value if GetAudioBufferSizeRaw() would return 0
+ */
+ unsigned int GetAudioBufferSize() const;
+ void SetAudioBufferSize(unsigned int size);
+
+ QString GetAudioOutDevice() const;
+ void SetAudioOutDevice(QString device_name);
+
+ ChiakiConnectVideoProfile GetVideoProfile();
+
+ DisconnectAction GetDisconnectAction();
+ void SetDisconnectAction(DisconnectAction action);
+
QList GetRegisteredHosts() const { return registered_hosts.values(); }
void AddRegisteredHost(const RegisteredHost &host);
void RemoveRegisteredHost(const HostMAC &mac);
@@ -69,6 +120,11 @@ class Settings : public QObject
bool GetManualHostExists(int id) { return manual_hosts.contains(id); }
ManualHost GetManualHost(int id) const { return manual_hosts[id]; }
+ static QString GetChiakiControllerButtonName(int);
+ void SetControllerButtonMapping(int, Qt::Key);
+ QMap GetControllerMapping();
+ QMap GetControllerMappingForDecoding();
+
signals:
void RegisteredHostsUpdated();
void ManualHostsUpdated();
diff --git a/gui/include/settingsdialog.h b/gui/include/settingsdialog.h
index f879931..00af648 100644
--- a/gui/include/settingsdialog.h
+++ b/gui/include/settingsdialog.h
@@ -1,19 +1,4 @@
-/*
- * This file is part of Chiaki.
- *
- * Chiaki is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Chiaki is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Chiaki. If not, see .
- */
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
#ifndef CHIAKI_SETTINGSDIALOG_H
#define CHIAKI_SETTINGSDIALOG_H
@@ -24,6 +9,7 @@ class Settings;
class QListWidget;
class QComboBox;
class QCheckBox;
+class QLineEdit;
class SettingsDialog : public QDialog
{
@@ -33,18 +19,36 @@ class SettingsDialog : public QDialog
Settings *settings;
QCheckBox *log_verbose_check_box;
+ QComboBox *disconnect_action_combo_box;
+ QCheckBox *dualsense_check_box;
QComboBox *resolution_combo_box;
QComboBox *fps_combo_box;
+ QLineEdit *bitrate_edit;
+ QComboBox *codec_combo_box;
+ QLineEdit *audio_buffer_size_edit;
+ QComboBox *audio_device_combo_box;
+ QCheckBox *pi_decoder_check_box;
+ QComboBox *hw_decoder_combo_box;
QListWidget *registered_hosts_list_widget;
QPushButton *delete_registered_host_button;
+ void UpdateBitratePlaceholder();
+
private slots:
void LogVerboseChanged();
+ void DualSenseChanged();
+ void DisconnectActionSelected();
void ResolutionSelected();
void FPSSelected();
+ void BitrateEdited();
+ void CodecSelected();
+ void AudioBufferSizeEdited();
+ void AudioOutputSelected();
+ void HardwareDecodeEngineSelected();
+ void UpdateHardwareDecodeEngineComboBox();
void UpdateRegisteredHosts();
void UpdateRegisteredHostsButtons();
diff --git a/gui/include/settingskeycapturedialog.h b/gui/include/settingskeycapturedialog.h
new file mode 100644
index 0000000..05ddd5f
--- /dev/null
+++ b/gui/include/settingskeycapturedialog.h
@@ -0,0 +1,22 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+#ifndef CHIAKI_SETTINGSKEYCAPTUREDIALOG_H
+#define CHIAKI_SETTINGSKEYCAPTUREDIALOG_H
+
+#include
+
+class SettingsKeyCaptureDialog : public QDialog
+{
+ Q_OBJECT
+
+ signals:
+ void KeyCaptured(Qt::Key);
+
+ protected:
+ void keyReleaseEvent(QKeyEvent *event) override;
+
+ public:
+ explicit SettingsKeyCaptureDialog(QWidget *parent = nullptr);
+};
+
+#endif // CHIAKI_SETTINGSKEYCAPTUREDIALOG_H
diff --git a/gui/include/streamsession.h b/gui/include/streamsession.h
index b555577..4b0ba01 100644
--- a/gui/include/streamsession.h
+++ b/gui/include/streamsession.h
@@ -1,36 +1,31 @@
-/*
- * This file is part of Chiaki.
- *
- * Chiaki is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Chiaki is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Chiaki. If not, see .
- */
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
#ifndef CHIAKI_STREAMSESSION_H
#define CHIAKI_STREAMSESSION_H
#include
+#include
+#include
+
+#if CHIAKI_LIB_ENABLE_PI_DECODER
+#include
+#endif
+
+#if CHIAKI_GUI_ENABLE_SETSU
+#include
+#include
+#endif
-#include "videodecoder.h"
#include "exception.h"
#include "sessionlog.h"
#include "controllermanager.h"
+#include "settings.h"
+#include "transformmode.h"
#include
#include
-
-#if CHIAKI_GUI_ENABLE_QT_GAMEPAD
-class QGamepad;
-#endif
+#include
+#include
class QAudioOutput;
class QIODevice;
@@ -45,15 +40,32 @@ class ChiakiException: public Exception
struct StreamSessionConnectInfo
{
+ Settings *settings;
+ QMap key_map;
+ Decoder decoder;
+ QString hw_decoder;
+ QString audio_out_device;
uint32_t log_level_mask;
QString log_file;
+ ChiakiTarget target;
QString host;
QByteArray regist_key;
QByteArray morning;
ChiakiConnectVideoProfile video_profile;
+ unsigned int audio_buffer_size;
+ bool fullscreen;
+ TransformMode transform_mode;
+ bool enable_keyboard;
+ bool enable_dualsense;
- StreamSessionConnectInfo();
- StreamSessionConnectInfo(Settings *settings, QString host, QByteArray regist_key, QByteArray morning);
+ StreamSessionConnectInfo(
+ Settings *settings,
+ ChiakiTarget target,
+ QString host,
+ QByteArray regist_key,
+ QByteArray morning,
+ bool fullscreen,
+ TransformMode transform_mode);
};
class StreamSession : public QObject
@@ -65,44 +77,75 @@ class StreamSession : public QObject
private:
SessionLog log;
ChiakiSession session;
+ ChiakiOpusDecoder opus_decoder;
+ bool connected;
-#if CHIAKI_GUI_ENABLE_QT_GAMEPAD
- QGamepad *gamepad;
+ QHash controllers;
+#if CHIAKI_GUI_ENABLE_SETSU
+ Setsu *setsu;
+ QMap, uint8_t> setsu_ids;
+ ChiakiControllerState setsu_state;
+ SetsuDevice *setsu_motion_device;
+ ChiakiOrientationTracker orient_tracker;
+ bool orient_dirty;
#endif
- Controller *controller;
ChiakiControllerState keyboard_state;
- VideoDecoder video_decoder;
+ ChiakiFfmpegDecoder *ffmpeg_decoder;
+ void TriggerFfmpegFrameAvailable();
+#if CHIAKI_LIB_ENABLE_PI_DECODER
+ ChiakiPiDecoder *pi_decoder;
+#endif
+ QAudioDeviceInfo audio_out_device_info;
+ unsigned int audio_buffer_size;
QAudioOutput *audio_output;
QIODevice *audio_io;
+ SDL_AudioDeviceID haptics_output;
+ uint8_t *haptics_resampler_buf;
+
+ QMap key_map;
void PushAudioFrame(int16_t *buf, size_t samples_count);
- void PushVideoSample(uint8_t *buf, size_t buf_size);
- void Event(ChiakiEvent *event);
+ void PushHapticsFrame(uint8_t *buf, size_t buf_size);
+#if CHIAKI_GUI_ENABLE_SETSU
+ void HandleSetsuEvent(SetsuEvent *event);
+#endif
private slots:
void InitAudio(unsigned int channels, unsigned int rate);
+ void InitHaptics();
+ void Event(ChiakiEvent *event);
+ void DisconnectHaptics();
+ void ConnectHaptics();
public:
explicit StreamSession(const StreamSessionConnectInfo &connect_info, QObject *parent = nullptr);
~StreamSession();
+ bool IsConnected() { return connected; }
+
void Start();
void Stop();
+ void GoToBed();
-#if CHIAKI_GUI_ENABLE_QT_GAMEPAD
- QGamepad *GetGamepad() { return gamepad; }
+ void SetLoginPIN(const QString &pin);
+
+ ChiakiLog *GetChiakiLog() { return log.GetChiakiLog(); }
+ QList GetControllers() { return controllers.values(); }
+ ChiakiFfmpegDecoder *GetFfmpegDecoder() { return ffmpeg_decoder; }
+#if CHIAKI_LIB_ENABLE_PI_DECODER
+ ChiakiPiDecoder *GetPiDecoder() { return pi_decoder; }
#endif
- Controller *GetController() { return controller; }
- VideoDecoder *GetVideoDecoder() { return &video_decoder; }
void HandleKeyboardEvent(QKeyEvent *event);
+ bool HandleMouseEvent(QMouseEvent *event);
signals:
- void CurrentImageUpdated();
+ void FfmpegFrameAvailable();
void SessionQuit(ChiakiQuitReason reason, const QString &reason_str);
+ void LoginPINRequested(bool incorrect);
private slots:
void UpdateGamepads();
diff --git a/gui/include/streamwindow.h b/gui/include/streamwindow.h
index 45d1e1b..f3bd8bb 100644
--- a/gui/include/streamwindow.h
+++ b/gui/include/streamwindow.h
@@ -1,19 +1,4 @@
-/*
- * This file is part of Chiaki.
- *
- * Chiaki is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Chiaki is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Chiaki. If not, see .
- */
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
#ifndef CHIAKI_GUI_STREAMWINDOW_H
#define CHIAKI_GUI_STREAMWINDOW_H
@@ -34,17 +19,36 @@ class StreamWindow: public QMainWindow
~StreamWindow() override;
private:
+ const StreamSessionConnectInfo connect_info;
StreamSession *session;
+ QAction *fullscreen_action;
+ QAction *stretch_action;
+ QAction *zoom_action;
AVOpenGLWidget *av_widget;
+ void Init();
+ void UpdateVideoTransform();
+ void UpdateTransformModeActions();
+
protected:
void keyPressEvent(QKeyEvent *event) override;
void keyReleaseEvent(QKeyEvent *event) override;
void closeEvent(QCloseEvent *event) override;
+ void mousePressEvent(QMouseEvent *event) override;
+ void mouseReleaseEvent(QMouseEvent *event) override;
+ void mouseDoubleClickEvent(QMouseEvent *event) override;
+ void resizeEvent(QResizeEvent *event) override;
+ void moveEvent(QMoveEvent *event) override;
+ void changeEvent(QEvent *event) override;
private slots:
void SessionQuit(ChiakiQuitReason reason, const QString &reason_str);
+ void LoginPINRequested(bool incorrect);
+ void ToggleFullscreen();
+ void ToggleStretch();
+ void ToggleZoom();
+ void Quit();
};
#endif // CHIAKI_GUI_STREAMWINDOW_H
diff --git a/gui/include/transformmode.h b/gui/include/transformmode.h
new file mode 100644
index 0000000..5c01d87
--- /dev/null
+++ b/gui/include/transformmode.h
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
+
+#ifndef CHIAKI_TRANSFORMMODE_H
+#define CHIAKI_TRANSFORMMODE_H
+
+enum class TransformMode {
+ Fit,
+ Zoom,
+ Stretch
+};
+
+#endif
diff --git a/gui/include/videodecoder.h b/gui/include/videodecoder.h
deleted file mode 100644
index e71e3c6..0000000
--- a/gui/include/videodecoder.h
+++ /dev/null
@@ -1,65 +0,0 @@
-/*
- * This file is part of Chiaki.
- *
- * Chiaki is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Chiaki is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Chiaki. If not, see .
- */
-
-#ifndef CHIAKI_VIDEODECODER_H
-#define CHIAKI_VIDEODECODER_H
-
-#include
-
-#include "exception.h"
-
-#include
-#include
-
-extern "C"
-{
-#include
-}
-
-#include
-
-class VideoDecoderException: public Exception
-{
- public:
- explicit VideoDecoderException(const QString &msg) : Exception(msg) {};
-};
-
-class VideoDecoder: public QObject
-{
- Q_OBJECT
-
- public:
- VideoDecoder(ChiakiLog *log);
- ~VideoDecoder();
-
- void PushFrame(uint8_t *buf, size_t buf_size);
- AVFrame *PullFrame();
-
- ChiakiLog *GetChiakiLog() { return log; }
-
- signals:
- void FramesAvailable();
-
- private:
- ChiakiLog *log;
- QMutex mutex;
-
- AVCodec *codec;
- AVCodecContext *codec_context;
-};
-
-#endif // CHIAKI_VIDEODECODER_H
diff --git a/gui/re.chiaki.Chiaki.appdata.xml b/gui/re.chiaki.Chiaki.appdata.xml
new file mode 100644
index 0000000..adb10ba
--- /dev/null
+++ b/gui/re.chiaki.Chiaki.appdata.xml
@@ -0,0 +1,25 @@
+
+
+
+ re.chiaki.Chiaki
+ chiaki.desktop
+ Chiaki
+ Free and Open Source Client for PlayStation Remote Play
+ CC0-1.0
+ AGPL-3.0-only
+ Florian Märkl
+ https://chiaki.re
+ https://git.sr.ht/~thestr4ng3r/chiaki/tree/master/item/README.md#usage
+
+
+ Chiaki is a Free and Open Source Client for PlayStation 4 and PlayStation 5 Remote Play. It can be used to play in real time on a PlayStation as long as there is a network connection.
+