From 0613e8984f3a71f9ce06e195af115c451076a7ae Mon Sep 17 00:00:00 2001 From: Elie Gambache Date: Thu, 9 Apr 2026 17:22:55 +0300 Subject: [PATCH 01/12] Restructure build system following Nucleus patterns - Move native source from maclib/, winlib/, linuxlib/ to src/native/{macos,windows,linux}/ - Remove git submodules, inline native code directly - Move Kotlin sources from src/commonMain/ to src/jvmMain/ - Add composetray/native/ namespace for native resources (gitignored) - Add NativeLibraryLoader utility for JNA classpath extraction - Add GraalVM reachability metadata and native-image.properties - Create reusable build-natives.yaml CI workflow - Refactor pr-build-check.yml and publish-on-maven.yml to use it - Configure demo with Nucleus plugin and GraalVM native-image support - Add Detekt + Ktlint code quality tools - Add .editorconfig for style consistency - Upgrade Gradle to 9.4.1 --- .editorconfig | 19 + .github/workflows/build-natives.yaml | 112 +++ .github/workflows/pr-build-check.yml | 47 +- .github/workflows/publish-on-maven.yml | 124 +-- .gitignore | 23 +- .gitmodules | 6 - build.gradle.kts | 75 +- config/detekt/detekt.yml | 459 +++++++++++ demo/build.gradle.kts | 63 +- gradle/libs.versions.toml | 11 +- gradle/wrapper/gradle-wrapper.jar | Bin 60756 -> 43583 bytes gradle/wrapper/gradle-wrapper.properties | 5 +- gradlew | 43 +- gradlew.bat | 37 +- linuxlib | 1 - settings.gradle.kts | 2 - .../composetray/lib/linux/LinuxLibTray.kt | 7 +- .../lib/linux/LinuxOutsideClickWatcher.kt | 0 .../composetray/lib/linux/LinuxTrayManager.kt | 0 .../composetray/lib/mac/MacNativeBridge.kt | 2 +- .../lib/mac/MacOSMenuBarThemeDetector.kt | 0 .../composetray/lib/mac/MacOsWindowManager.kt | 0 .../lib/mac/MacOutsideClickWatcher.kt | 0 .../composetray/lib/mac/MacTrayManager.kt | 0 .../lib/windows/StdCallCallback.kt | 0 .../lib/windows/WindowsNativeTray.kt | 0 .../lib/windows/WindowsNativeTrayLibrary.kt | 5 +- .../lib/windows/WindowsNativeTrayMenuItem.kt | 0 .../lib/windows/WindowsOutsideClickWatcher.kt | 0 .../lib/windows/WindowsTrayManager.kt | 0 .../composetray/menu/api/TrayMenuBuilder.kt | 0 .../menu/impl/AwtTrayMenuBuilderImpl.kt | 0 .../menu/impl/LinuxTrayMenuBuilderImpl.kt | 0 .../menu/impl/MacTrayMenuBuilderImpl.kt | 0 .../menu/impl/WindowsTrayMenuBuilderImpl.kt | 0 .../tray/api/ExperimentalTrayAppApi.kt | 0 .../kdroid/composetray/tray/api/NativeTray.kt | 0 .../kdroid/composetray/tray/api/TrayApp.kt | 0 .../composetray/tray/api/TrayAppState.kt | 0 .../tray/api/TrayWindowDismissMode.kt | 0 .../tray/impl/AwtTrayInitializer.kt | 0 .../tray/impl/LinuxTrayInitializer.kt | 0 .../tray/impl/MacTrayInitializer.kt | 0 .../tray/impl/WindowsTrayInitializer.kt | 0 .../com/kdroid/composetray/utils/AppId.kt | 0 .../composetray/utils/ComposableIconUtils.kt | 0 .../composetray/utils/DarkModeDetector.kt | 0 .../com/kdroid/composetray/utils/DebugLn.kt | 0 .../composetray/utils/IconRenderProperties.kt | 0 .../composetray/utils/JarResourceExtractor.kt | 0 .../composetray/utils/MenuContentHash.kt | 0 .../composetray/utils/NativeLibraryLoader.kt | 77 ++ .../utils/PersistentAnimatedVisibility.kt | 0 .../utils/SingleInstanceManager.kt | 0 .../kdroid/composetray/utils/TrayPosition.kt | 0 .../kdroid/composetray/utils/WindowRaise.kt | 0 .../utils/WindowVisibilityMonitor.kt | 0 .../composenativetray/native-image.properties | 1 + .../reachability-metadata.json | 31 + src/native/linux/Makefile | 37 + src/native/linux/build.sh | 34 + src/native/linux/go.mod | 27 + src/native/linux/go.sum | 21 + src/native/linux/icon/icon.png | Bin 0 -> 2238 bytes src/native/linux/icon/iconunix.go | 196 +++++ src/native/linux/icon/logo.png | Bin 0 -> 1851 bytes src/native/linux/internal/DbusMenu.xml | 60 ++ .../linux/internal/StatusNotifierItem.xml | 85 ++ .../internal/generated/menu/dbus_menu.go | 484 +++++++++++ .../notifier/status_notifier_item.go | 633 +++++++++++++++ src/native/linux/jna/bridge_linux.go | 297 +++++++ src/native/linux/jna/main.go | 5 + src/native/linux/systray.go | 404 ++++++++++ src/native/linux/systray.h | 27 + src/native/linux/systray_menu_unix.go | 345 ++++++++ src/native/linux/systray_other.go | 183 +++++ src/native/linux/systray_unix.go | 511 ++++++++++++ {maclib => src/native/macos}/MacTrayBridge.m | 0 {maclib => src/native/macos}/build.sh | 2 +- {maclib => src/native/macos}/tray.h | 0 {maclib => src/native/macos}/tray.swift | 0 src/native/windows/CMakeLists.txt | 79 ++ src/native/windows/build.bat | 40 + src/native/windows/tray.h | 66 ++ src/native/windows/tray_windows.c | 753 ++++++++++++++++++ winlib | 1 - 86 files changed, 5164 insertions(+), 276 deletions(-) create mode 100644 .editorconfig create mode 100644 .github/workflows/build-natives.yaml delete mode 100644 .gitmodules create mode 100644 config/detekt/detekt.yml delete mode 160000 linuxlib rename src/{commonMain => jvmMain}/kotlin/com/kdroid/composetray/lib/linux/LinuxLibTray.kt (92%) rename src/{commonMain => jvmMain}/kotlin/com/kdroid/composetray/lib/linux/LinuxOutsideClickWatcher.kt (100%) rename src/{commonMain => jvmMain}/kotlin/com/kdroid/composetray/lib/linux/LinuxTrayManager.kt (100%) rename src/{commonMain => jvmMain}/kotlin/com/kdroid/composetray/lib/mac/MacNativeBridge.kt (98%) rename src/{commonMain => jvmMain}/kotlin/com/kdroid/composetray/lib/mac/MacOSMenuBarThemeDetector.kt (100%) rename src/{commonMain => jvmMain}/kotlin/com/kdroid/composetray/lib/mac/MacOsWindowManager.kt (100%) rename src/{commonMain => jvmMain}/kotlin/com/kdroid/composetray/lib/mac/MacOutsideClickWatcher.kt (100%) rename src/{commonMain => jvmMain}/kotlin/com/kdroid/composetray/lib/mac/MacTrayManager.kt (100%) rename src/{commonMain => jvmMain}/kotlin/com/kdroid/composetray/lib/windows/StdCallCallback.kt (100%) rename src/{commonMain => jvmMain}/kotlin/com/kdroid/composetray/lib/windows/WindowsNativeTray.kt (100%) rename src/{commonMain => jvmMain}/kotlin/com/kdroid/composetray/lib/windows/WindowsNativeTrayLibrary.kt (89%) rename src/{commonMain => jvmMain}/kotlin/com/kdroid/composetray/lib/windows/WindowsNativeTrayMenuItem.kt (100%) rename src/{commonMain => jvmMain}/kotlin/com/kdroid/composetray/lib/windows/WindowsOutsideClickWatcher.kt (100%) rename src/{commonMain => jvmMain}/kotlin/com/kdroid/composetray/lib/windows/WindowsTrayManager.kt (100%) rename src/{commonMain => jvmMain}/kotlin/com/kdroid/composetray/menu/api/TrayMenuBuilder.kt (100%) rename src/{commonMain => jvmMain}/kotlin/com/kdroid/composetray/menu/impl/AwtTrayMenuBuilderImpl.kt (100%) rename src/{commonMain => jvmMain}/kotlin/com/kdroid/composetray/menu/impl/LinuxTrayMenuBuilderImpl.kt (100%) rename src/{commonMain => jvmMain}/kotlin/com/kdroid/composetray/menu/impl/MacTrayMenuBuilderImpl.kt (100%) rename src/{commonMain => jvmMain}/kotlin/com/kdroid/composetray/menu/impl/WindowsTrayMenuBuilderImpl.kt (100%) rename src/{commonMain => jvmMain}/kotlin/com/kdroid/composetray/tray/api/ExperimentalTrayAppApi.kt (100%) rename src/{commonMain => jvmMain}/kotlin/com/kdroid/composetray/tray/api/NativeTray.kt (100%) rename src/{commonMain => jvmMain}/kotlin/com/kdroid/composetray/tray/api/TrayApp.kt (100%) rename src/{commonMain => jvmMain}/kotlin/com/kdroid/composetray/tray/api/TrayAppState.kt (100%) rename src/{commonMain => jvmMain}/kotlin/com/kdroid/composetray/tray/api/TrayWindowDismissMode.kt (100%) rename src/{commonMain => jvmMain}/kotlin/com/kdroid/composetray/tray/impl/AwtTrayInitializer.kt (100%) rename src/{commonMain => jvmMain}/kotlin/com/kdroid/composetray/tray/impl/LinuxTrayInitializer.kt (100%) rename src/{commonMain => jvmMain}/kotlin/com/kdroid/composetray/tray/impl/MacTrayInitializer.kt (100%) rename src/{commonMain => jvmMain}/kotlin/com/kdroid/composetray/tray/impl/WindowsTrayInitializer.kt (100%) rename src/{commonMain => jvmMain}/kotlin/com/kdroid/composetray/utils/AppId.kt (100%) rename src/{commonMain => jvmMain}/kotlin/com/kdroid/composetray/utils/ComposableIconUtils.kt (100%) rename src/{commonMain => jvmMain}/kotlin/com/kdroid/composetray/utils/DarkModeDetector.kt (100%) rename src/{commonMain => jvmMain}/kotlin/com/kdroid/composetray/utils/DebugLn.kt (100%) rename src/{commonMain => jvmMain}/kotlin/com/kdroid/composetray/utils/IconRenderProperties.kt (100%) rename src/{commonMain => jvmMain}/kotlin/com/kdroid/composetray/utils/JarResourceExtractor.kt (100%) rename src/{commonMain => jvmMain}/kotlin/com/kdroid/composetray/utils/MenuContentHash.kt (100%) create mode 100644 src/jvmMain/kotlin/com/kdroid/composetray/utils/NativeLibraryLoader.kt rename src/{commonMain => jvmMain}/kotlin/com/kdroid/composetray/utils/PersistentAnimatedVisibility.kt (100%) rename src/{commonMain => jvmMain}/kotlin/com/kdroid/composetray/utils/SingleInstanceManager.kt (100%) rename src/{commonMain => jvmMain}/kotlin/com/kdroid/composetray/utils/TrayPosition.kt (100%) rename src/{commonMain => jvmMain}/kotlin/com/kdroid/composetray/utils/WindowRaise.kt (100%) rename src/{commonMain => jvmMain}/kotlin/com/kdroid/composetray/utils/WindowVisibilityMonitor.kt (100%) create mode 100644 src/jvmMain/resources/META-INF/native-image/io.github.kdroidfilter/composenativetray/native-image.properties create mode 100644 src/jvmMain/resources/META-INF/native-image/io.github.kdroidfilter/composenativetray/reachability-metadata.json create mode 100644 src/native/linux/Makefile create mode 100755 src/native/linux/build.sh create mode 100644 src/native/linux/go.mod create mode 100644 src/native/linux/go.sum create mode 100644 src/native/linux/icon/icon.png create mode 100644 src/native/linux/icon/iconunix.go create mode 100644 src/native/linux/icon/logo.png create mode 100644 src/native/linux/internal/DbusMenu.xml create mode 100644 src/native/linux/internal/StatusNotifierItem.xml create mode 100644 src/native/linux/internal/generated/menu/dbus_menu.go create mode 100644 src/native/linux/internal/generated/notifier/status_notifier_item.go create mode 100644 src/native/linux/jna/bridge_linux.go create mode 100644 src/native/linux/jna/main.go create mode 100644 src/native/linux/systray.go create mode 100644 src/native/linux/systray.h create mode 100644 src/native/linux/systray_menu_unix.go create mode 100644 src/native/linux/systray_other.go create mode 100644 src/native/linux/systray_unix.go rename {maclib => src/native/macos}/MacTrayBridge.m (100%) rename {maclib => src/native/macos}/build.sh (95%) rename {maclib => src/native/macos}/tray.h (100%) rename {maclib => src/native/macos}/tray.swift (100%) create mode 100644 src/native/windows/CMakeLists.txt create mode 100644 src/native/windows/build.bat create mode 100644 src/native/windows/tray.h create mode 100644 src/native/windows/tray_windows.c delete mode 160000 winlib diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..0c3cd955 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,19 @@ +root = true + +[*] +charset = utf-8 +indent_size = 4 +insert_final_newline = true +trim_trailing_whitespace = true + +[*{.yml,yaml}] +indent_style = space +indent_size = 2 + +[*.{kt,kts}] +max_line_length = 120 +ktlint_standard_backing-property-naming = disabled +ktlint_standard_function-naming = disabled + +[**/generated/**] +ktlint = disabled diff --git a/.github/workflows/build-natives.yaml b/.github/workflows/build-natives.yaml new file mode 100644 index 00000000..dda5789e --- /dev/null +++ b/.github/workflows/build-natives.yaml @@ -0,0 +1,112 @@ +name: Build Native Libraries + +on: + workflow_call: + +jobs: + build-native-macos: + runs-on: macos-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up JDK + uses: actions/setup-java@v4 + with: + java-version: '17' + distribution: 'temurin' + + - name: Build macOS native libraries + working-directory: src/native/macos + run: bash build.sh + env: + NATIVE_LIBS_OUTPUT_DIR: ${{ github.workspace }}/build/nativeLibs + + - name: Verify macOS natives + run: | + test -f build/nativeLibs/darwin-aarch64/libMacTray.dylib + test -f build/nativeLibs/darwin-x86-64/libMacTray.dylib + ls -la build/nativeLibs/darwin-aarch64/ + ls -la build/nativeLibs/darwin-x86-64/ + + - name: Upload macOS ARM64 library + uses: actions/upload-artifact@v4 + with: + name: native-darwin-aarch64 + path: build/nativeLibs/darwin-aarch64/libMacTray.dylib + retention-days: 1 + + - name: Upload macOS x86_64 library + uses: actions/upload-artifact@v4 + with: + name: native-darwin-x86-64 + path: build/nativeLibs/darwin-x86-64/libMacTray.dylib + retention-days: 1 + + build-native-linux: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup Go + uses: actions/setup-go@v5 + with: + go-version: 'stable' + + - name: Install dependencies + run: | + sudo apt-get update + sudo apt-get install -y libgtk-3-dev libappindicator3-dev + + - name: Build Linux native library + working-directory: src/native/linux + run: bash build.sh + env: + NATIVE_LIBS_OUTPUT_DIR: ${{ github.workspace }}/build/nativeLibs + + - name: Verify Linux natives + run: | + test -f build/nativeLibs/linux-x86-64/libsystray.so + ls -la build/nativeLibs/linux-x86-64/ + + - name: Upload Linux x86_64 library + uses: actions/upload-artifact@v4 + with: + name: native-linux-x86-64 + path: build/nativeLibs/linux-x86-64/libsystray.so + retention-days: 1 + + build-native-windows: + runs-on: windows-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Build Windows native libraries + working-directory: src/native/windows + run: cmd /c build.bat + env: + NATIVE_LIBS_OUTPUT_DIR: ${{ github.workspace }}/build/nativeLibs + + - name: Verify Windows natives + shell: bash + run: | + test -f build/nativeLibs/win32-x86-64/tray.dll + test -f build/nativeLibs/win32-arm64/tray.dll + ls -la build/nativeLibs/win32-x86-64/ + ls -la build/nativeLibs/win32-arm64/ + + - name: Upload Windows x64 library + uses: actions/upload-artifact@v4 + with: + name: native-win32-x86-64 + path: build/nativeLibs/win32-x86-64/tray.dll + retention-days: 1 + + - name: Upload Windows ARM64 library + uses: actions/upload-artifact@v4 + with: + name: native-win32-arm64 + path: build/nativeLibs/win32-arm64/tray.dll + retention-days: 1 diff --git a/.github/workflows/pr-build-check.yml b/.github/workflows/pr-build-check.yml index a18d2d3e..3de77a2a 100644 --- a/.github/workflows/pr-build-check.yml +++ b/.github/workflows/pr-build-check.yml @@ -5,48 +5,5 @@ on: branches: [master] jobs: - build-native-macos: - runs-on: macos-latest - steps: - - name: Checkout code - uses: actions/checkout@v4 - with: - submodules: recursive - - - name: Build macOS native libraries - working-directory: maclib - run: bash build.sh - - build-native-linux: - runs-on: ubuntu-latest - steps: - - name: Checkout code - uses: actions/checkout@v4 - with: - submodules: recursive - - - name: Setup Go - uses: actions/setup-go@v5 - with: - go-version: 'stable' - - - name: Install dependencies - run: | - sudo apt-get update - sudo apt-get install -y libgtk-3-dev libappindicator3-dev - - - name: Build Linux native library - working-directory: linuxlib - run: bash build.sh - - build-native-windows: - runs-on: windows-latest - steps: - - name: Checkout code - uses: actions/checkout@v4 - with: - submodules: recursive - - - name: Build Windows native libraries - working-directory: winlib - run: cmd /c build.bat + build-natives: + uses: ./.github/workflows/build-natives.yaml diff --git a/.github/workflows/publish-on-maven.yml b/.github/workflows/publish-on-maven.yml index 964ace08..cf9bc799 100644 --- a/.github/workflows/publish-on-maven.yml +++ b/.github/workflows/publish-on-maven.yml @@ -5,154 +5,56 @@ on: types: [published] jobs: - build-native-macos: + build-natives: if: startsWith(github.event.release.tag_name, 'v') - runs-on: macos-latest - steps: - - name: Checkout code - uses: actions/checkout@v4 - with: - submodules: recursive - - - name: Build macOS native libraries - working-directory: maclib - run: bash build.sh - env: - NATIVE_LIBS_OUTPUT_DIR: ${{ github.workspace }}/build/nativeLibs - - - name: Upload macOS ARM64 library - uses: actions/upload-artifact@v4 - with: - name: native-darwin-aarch64 - path: build/nativeLibs/darwin-aarch64/libMacTray.dylib - retention-days: 1 - - - name: Upload macOS x86_64 library - uses: actions/upload-artifact@v4 - with: - name: native-darwin-x86-64 - path: build/nativeLibs/darwin-x86-64/libMacTray.dylib - retention-days: 1 - - build-native-linux: - if: startsWith(github.event.release.tag_name, 'v') - runs-on: ubuntu-latest - steps: - - name: Checkout code - uses: actions/checkout@v4 - with: - submodules: recursive - - - name: Setup Go - uses: actions/setup-go@v5 - with: - go-version: 'stable' - - - name: Install dependencies - run: | - sudo apt-get update - sudo apt-get install -y libgtk-3-dev libappindicator3-dev - - - name: Build Linux native library - working-directory: linuxlib - run: bash build.sh - env: - NATIVE_LIBS_OUTPUT_DIR: ${{ github.workspace }}/build/nativeLibs - - - name: Upload Linux x86_64 library - uses: actions/upload-artifact@v4 - with: - name: native-linux-x86-64 - path: build/nativeLibs/linux-x86-64/libsystray.so - retention-days: 1 - - build-native-windows: - if: startsWith(github.event.release.tag_name, 'v') - runs-on: windows-latest - steps: - - name: Checkout code - uses: actions/checkout@v4 - with: - submodules: recursive - - - name: Build Windows native libraries - working-directory: winlib - run: cmd /c build.bat - env: - NATIVE_LIBS_OUTPUT_DIR: ${{ github.workspace }}/build/nativeLibs - - - name: Upload Windows x64 library - uses: actions/upload-artifact@v4 - with: - name: native-win32-x86-64 - path: build/nativeLibs/win32-x86-64/tray.dll - retention-days: 1 - - - name: Upload Windows ARM64 library - uses: actions/upload-artifact@v4 - with: - name: native-win32-arm64 - path: build/nativeLibs/win32-arm64/tray.dll - retention-days: 1 + uses: ./.github/workflows/build-natives.yaml publish: if: startsWith(github.event.release.tag_name, 'v') - needs: - - build-native-macos - - build-native-linux - - build-native-windows + needs: [build-natives] runs-on: macos-latest steps: - name: Checkout code uses: actions/checkout@v4 - - name: Set version from tag - shell: bash - run: | - TAG="${{ github.event.release.tag_name }}" - VERSION_NAME="${TAG#v}" - echo "VERSION_NAME=$VERSION_NAME" >> "$GITHUB_ENV" - sed -i.bak "s/^VERSION_NAME=.*/VERSION_NAME=$VERSION_NAME/" gradle.properties - rm -f gradle.properties.bak - - name: Download macOS ARM64 library uses: actions/download-artifact@v4 with: name: native-darwin-aarch64 - path: src/commonMain/resources/darwin-aarch64/ + path: src/jvmMain/resources/composetray/native/darwin-aarch64/ - name: Download macOS x86_64 library uses: actions/download-artifact@v4 with: name: native-darwin-x86-64 - path: src/commonMain/resources/darwin-x86-64/ + path: src/jvmMain/resources/composetray/native/darwin-x86-64/ - name: Download Linux x86_64 library uses: actions/download-artifact@v4 with: name: native-linux-x86-64 - path: src/commonMain/resources/linux-x86-64/ + path: src/jvmMain/resources/composetray/native/linux-x86-64/ - name: Download Windows x64 library uses: actions/download-artifact@v4 with: name: native-win32-x86-64 - path: src/commonMain/resources/win32-x86-64/ + path: src/jvmMain/resources/composetray/native/win32-x86-64/ - name: Download Windows ARM64 library uses: actions/download-artifact@v4 with: name: native-win32-arm64 - path: src/commonMain/resources/win32-arm64/ + path: src/jvmMain/resources/composetray/native/win32-arm64/ - name: Verify native libraries run: | echo "=== Native libraries in resources ===" - ls -la src/commonMain/resources/darwin-aarch64/ - ls -la src/commonMain/resources/darwin-x86-64/ - ls -la src/commonMain/resources/linux-x86-64/ - ls -la src/commonMain/resources/win32-x86-64/ - ls -la src/commonMain/resources/win32-arm64/ + ls -la src/jvmMain/resources/composetray/native/darwin-aarch64/ + ls -la src/jvmMain/resources/composetray/native/darwin-x86-64/ + ls -la src/jvmMain/resources/composetray/native/linux-x86-64/ + ls -la src/jvmMain/resources/composetray/native/win32-x86-64/ + ls -la src/jvmMain/resources/composetray/native/win32-arm64/ - name: Set up JDK uses: actions/setup-java@v4 diff --git a/.gitignore b/.gitignore index e40ad6ac..5c8a7924 100644 --- a/.gitignore +++ b/.gitignore @@ -5,10 +5,7 @@ build/ !**/src/test/**/build/ ### IntelliJ IDEA ### -.idea/modules.xml -.idea/jarRepositories.xml -.idea/compiler.xml -.idea/libraries/ +.idea *.iws *.iml *.ipr @@ -44,13 +41,17 @@ bin/ ### Mac OS ### .DS_Store -.idea +### Runtime ### tray_position.properties -linuxlib/build-x86-64 -linuxlibdbus/build-x86-64 +### Native libraries (built on CI via build-natives.yaml) ### +**/src/jvmMain/resources/composetray/native/ + +### MSVC build artifacts ### +*.obj +*.exp -# don't commit compiled native binaries -src/commonMain/resources/**/*.so -src/commonMain/resources/**/*.dll -src/commonMain/resources/**/*.dylib +### Native build directories ### +src/native/windows/build-x64/ +src/native/windows/build-arm64/ +src/native/linux/dist/ diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 57a3b130..00000000 --- a/.gitmodules +++ /dev/null @@ -1,6 +0,0 @@ -[submodule "winlib"] - path = winlib - url = https://github.com/kdroidFilter/LibWindowsTray.git -[submodule "linuxlib"] - path = linuxlib - url = https://github.com/kdroidFilter/ComposeNativeTrayLinuxLib diff --git a/build.gradle.kts b/build.gradle.kts index c2cd31a1..14b2297c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,5 +1,5 @@ +import org.apache.tools.ant.taskdefs.condition.Os import org.jetbrains.dokka.gradle.DokkaTask -import java.util.Locale plugins { alias(libs.plugins.multiplatform) @@ -7,6 +7,8 @@ plugins { alias(libs.plugins.compose) alias(libs.plugins.vannitktech.maven.publish) alias(libs.plugins.dokka) + alias(libs.plugins.detekt) + alias(libs.plugins.ktlint) } group = "com.kdroid.composenativetray" @@ -20,7 +22,6 @@ repositories { mavenCentral() maven("https://maven.pkg.jetbrains.space/public/p/compose/dev") google() - } tasks.withType().configureEach { @@ -28,14 +29,12 @@ tasks.withType().configureEach { offlineMode.set(true) } - - kotlin { jvmToolchain(17) jvm() sourceSets { - commonMain.dependencies { + jvmMain.dependencies { implementation(compose.runtime) implementation(compose.foundation) implementation(compose.ui) @@ -46,40 +45,64 @@ kotlin { implementation(libs.kotlinx.coroutines.swing) implementation(libs.platformtools.core) implementation(libs.platformtools.darkmodedetector) - } } +} + +// ── Native build tasks ────────────────────────────────────────────────────────── +val buildNativeMacOs by tasks.registering(Exec::class) { + workingDir = file("src/native/macos") + commandLine("bash", "build.sh") + onlyIf { Os.isFamily(Os.FAMILY_MAC) } } -val buildWin: TaskProvider = tasks.register("buildNativeWin") { - onlyIf { System.getProperty("os.name").startsWith("Windows") } - workingDir(rootDir.resolve("winlib")) +val buildNativeWindows by tasks.registering(Exec::class) { + workingDir = file("src/native/windows") commandLine("cmd", "/c", "build.bat") + onlyIf { Os.isFamily(Os.FAMILY_WINDOWS) } } -val buildMac: TaskProvider = tasks.register("buildNativeMac") { - onlyIf { System.getProperty("os.name").startsWith("Mac") } - workingDir(rootDir.resolve("maclib")) - commandLine("sh", "build.sh") +val buildNativeLinux by tasks.registering(Exec::class) { + workingDir = file("src/native/linux") + commandLine("bash", "build.sh") + onlyIf { Os.isFamily(Os.FAMILY_UNIX) && !Os.isFamily(Os.FAMILY_MAC) } } -val buildLinux: TaskProvider = tasks.register("buildNativeLinux") { - onlyIf { System.getProperty("os.name").lowercase(Locale.getDefault()).contains("linux") } - workingDir(rootDir.resolve("linuxlib")) - commandLine("./build.sh") +tasks.register("buildNativeLibraries") { + dependsOn(buildNativeMacOs, buildNativeWindows, buildNativeLinux) } -tasks.register("buildNativeLibraries") { - dependsOn(buildWin, buildLinux, buildMac) +tasks.named("jvmProcessResources") { + dependsOn(buildNativeMacOs, buildNativeWindows, buildNativeLinux) } -if (System.getenv("CI") == null) { - tasks.named("jvmProcessResources") { - dependsOn("buildNativeLibraries") +// ── Code quality ──────────────────────────────────────────────────────────────── + +detekt { + config.setFrom(files("config/detekt/detekt.yml")) + buildUponDefaultConfig = true +} + +allprojects { + apply(plugin = "org.jlleitschuh.gradle.ktlint") + + ktlint { + debug.set(false) + verbose.set(true) + android.set(false) + outputToConsole.set(true) + ignoreFailures.set(false) + enableExperimentalRules.set(true) + filter { + exclude("**/generated/**") + include("**/kotlin/**") + } } } +// ── Maven publishing ──────────────────────────────────────────────────────────── + mavenPublishing { coordinates( groupId = "io.github.kdroidfilter", @@ -87,7 +110,6 @@ mavenPublishing { version = version ) - // Configure POM metadata for the published artifact pom { name.set("Compose Native Tray") description.set("ComposeTray is a Kotlin library that provides a simple way to create system tray applications with native support for Linux and Windows. This library allows you to add a system tray icon, tooltip, and menu with various options in a Kotlin DSL-style syntax.") @@ -101,7 +123,6 @@ mavenPublishing { } } - // Specify developers information developers { developer { id.set("kdroidfilter") @@ -110,17 +131,11 @@ mavenPublishing { } } - // Specify SCM information scm { url.set("https://github.com/kdroidFilter/ComposeNativeTray") } } - // Configure publishing to Maven Central publishToMavenCentral() - - - // Enable GPG signing for all publications signAllPublications() } - diff --git a/config/detekt/detekt.yml b/config/detekt/detekt.yml new file mode 100644 index 00000000..43ce38cb --- /dev/null +++ b/config/detekt/detekt.yml @@ -0,0 +1,459 @@ +config: + validation: true + warningsAsErrors: false + checkExhaustiveness: false + excludes: [] + +processors: + active: true + exclude: + - 'DetektProgressListener' + +console-reports: + active: true + exclude: + - 'ProjectStatisticsReport' + - 'ComplexityReport' + - 'NotificationReport' + - 'IssuesReport' + - 'FileBasedIssuesReport' + +comments: + active: true + AbsentOrWrongFileLicense: + active: false + DeprecatedBlockTag: + active: false + DocumentationOverPrivateFunction: + active: false + DocumentationOverPrivateProperty: + active: false + EndOfSentenceFormat: + active: false + KDocReferencesNonPublicProperty: + active: false + OutdatedDocumentation: + active: false + UndocumentedPublicClass: + active: false + UndocumentedPublicFunction: + active: false + UndocumentedPublicProperty: + active: false + +complexity: + active: true + CognitiveComplexMethod: + active: false + ComplexCondition: + active: true + allowedConditions: 4 + ComplexInterface: + active: false + CyclomaticComplexMethod: + active: true + allowedComplexity: 20 + ignoreSingleWhenExpression: false + ignoreSimpleWhenEntries: false + LabeledExpression: + active: false + LargeClass: + active: true + allowedLines: 600 + LongMethod: + active: true + excludes: ['**/demo/**'] + allowedLines: 100 + LongParameterList: + active: true + allowedFunctionParameters: 7 + allowedConstructorParameters: 7 + ignoreDefaultParameters: false + ignoreDataClasses: true + MethodOverloading: + active: false + NamedArguments: + active: false + NestedBlockDepth: + active: true + allowedDepth: 4 + NestedScopeFunctions: + active: false + ReplaceSafeCallChainWithRun: + active: false + StringLiteralDuplication: + active: false + TooManyFunctions: + active: true + excludes: ['**/test/**', '**/demo/**'] + allowedFunctionsPerFile: 11 + allowedFunctionsPerClass: 30 + allowedFunctionsPerInterface: 11 + allowedFunctionsPerObject: 11 + allowedFunctionsPerEnum: 11 + ignoreDeprecated: false + ignorePrivate: false + ignoreOverridden: false + +coroutines: + active: true + GlobalCoroutineUsage: + active: false + InjectDispatcher: + active: true + dispatcherNames: + - 'IO' + - 'Default' + - 'Unconfined' + RedundantSuspendModifier: + active: true + SleepInsteadOfDelay: + active: true + SuspendFunWithFlowReturnType: + active: true + +empty-blocks: + active: true + EmptyCatchBlock: + active: true + allowedExceptionNameRegex: '_|(ignore|expected).*' + EmptyClassBlock: + active: true + EmptyDefaultConstructor: + active: true + EmptyDoWhileBlock: + active: true + EmptyElseBlock: + active: true + EmptyFinallyBlock: + active: true + EmptyForBlock: + active: true + EmptyFunctionBlock: + active: true + ignoreOverridden: false + EmptyIfBlock: + active: true + EmptyInitBlock: + active: true + EmptyKotlinFile: + active: true + EmptySecondaryConstructor: + active: true + EmptyTryBlock: + active: true + EmptyWhenBlock: + active: true + EmptyWhileBlock: + active: true + +exceptions: + active: true + ExceptionRaisedInUnexpectedLocation: + active: true + methodNames: + - 'equals' + - 'finalize' + - 'hashCode' + - 'toString' + InstanceOfCheckForException: + active: true + NotImplementedDeclaration: + active: false + PrintStackTrace: + active: true + RethrowCaughtException: + active: true + ReturnFromFinally: + active: true + SwallowedException: + active: true + ignoredExceptionTypes: + - 'InterruptedException' + - 'MalformedURLException' + - 'NumberFormatException' + - 'ParseException' + allowedExceptionNameRegex: '_|(ignore|expected).*' + ThrowingExceptionFromFinally: + active: true + ThrowingExceptionsWithoutMessageOrCause: + active: true + exceptions: + - 'ArrayIndexOutOfBoundsException' + - 'Exception' + - 'IllegalArgumentException' + - 'IllegalStateException' + - 'IndexOutOfBoundsException' + - 'NullPointerException' + - 'RuntimeException' + - 'Throwable' + ThrowingNewInstanceOfSameException: + active: true + TooGenericExceptionCaught: + active: true + exceptionNames: + - 'ArrayIndexOutOfBoundsException' + - 'Error' + - 'Exception' + - 'IllegalMonitorStateException' + - 'IndexOutOfBoundsException' + - 'NullPointerException' + - 'RuntimeException' + - 'Throwable' + allowedExceptionNameRegex: '_|(ignore|expected).*' + TooGenericExceptionThrown: + active: true + exceptionNames: + - 'Error' + - 'Exception' + - 'RuntimeException' + - 'Throwable' + +naming: + active: true + BooleanPropertyNaming: + active: false + ClassNaming: + active: true + classPattern: '[A-Z][a-zA-Z0-9]*' + ConstructorParameterNaming: + active: true + parameterPattern: '[a-z][A-Za-z0-9]*' + privateParameterPattern: '[a-z][A-Za-z0-9]*' + EnumNaming: + active: true + enumEntryPattern: '[A-Z][_a-zA-Z0-9]*' + FunctionNaming: + active: true + excludes: ['**/test/**'] + functionPattern: '[a-zA-Z][a-zA-Z0-9]*' + FunctionParameterNaming: + active: true + parameterPattern: '[a-z][A-Za-z0-9]*' + InvalidPackageDeclaration: + active: true + rootPackage: '' + requireRootInDeclaration: false + excludes: ['**/build/**'] + MatchingDeclarationName: + active: true + mustBeFirst: true + MemberNameEqualsClassName: + active: true + ignoreOverridden: true + NoNameShadowing: + active: true + ObjectPropertyNaming: + active: true + constantPattern: '[A-Za-z][_A-Za-z0-9]*' + propertyPattern: '[A-Za-z][_A-Za-z0-9]*' + privatePropertyPattern: '(_)?[A-Za-z][_A-Za-z0-9]*' + PackageNaming: + active: true + packagePattern: '[a-z]+(\.[a-z][A-Za-z0-9]*)*' + TopLevelPropertyNaming: + active: true + constantPattern: '[A-Z][_A-Z0-9]*' + propertyPattern: '[A-Za-z][_A-Za-z0-9]*' + privatePropertyPattern: '_?[A-Za-z][_A-Za-z0-9]*' + VariableNaming: + active: true + variablePattern: '[a-z][A-Za-z0-9]*' + privateVariablePattern: '(_)?[a-z][A-Za-z0-9]*' + +performance: + active: true + ArrayPrimitive: + active: true + ForEachOnRange: + active: true + SpreadOperator: + active: true + excludes: ['**/test/**'] + UnnecessaryTemporaryInstantiation: + active: true + +potential-bugs: + active: true + AvoidReferentialEquality: + active: true + forbiddenTypePatterns: + - 'kotlin.String' + DoubleMutabilityForCollection: + active: true + EqualsAlwaysReturnsTrueOrFalse: + active: true + EqualsWithHashCodeExist: + active: true + ExplicitGarbageCollectionCall: + active: true + HasPlatformType: + active: true + IgnoredReturnValue: + active: true + restrictToConfig: true + returnValueAnnotations: + - 'CheckResult' + - '*.CheckResult' + - 'CheckReturnValue' + - '*.CheckReturnValue' + ImplicitDefaultLocale: + active: true + InvalidRange: + active: true + IteratorHasNextCallsNextMethod: + active: true + IteratorNotThrowingNoSuchElementException: + active: true + MapGetWithNotNullAssertionOperator: + active: true + UnnecessaryNotNullOperator: + active: true + UnnecessarySafeCall: + active: true + UnreachableCatchBlock: + active: true + UnreachableCode: + active: true + UnsafeCallOnNullableType: + active: true + excludes: ['**/test/**'] + UnsafeCast: + active: true + UnusedUnaryOperator: + active: true + UselessPostfixExpression: + active: true + WrongEqualsTypeParameter: + active: true + +style: + active: true + DestructuringDeclarationWithTooManyEntries: + active: true + maxDestructuringEntries: 3 + EqualsNullCall: + active: true + ExplicitItLambdaMultipleParameters: + active: true + ExplicitItLambdaParameter: + active: true + ForbiddenComment: + active: true + comments: + - reason: 'Forbidden FIXME todo marker in comment, please fix the problem.' + value: 'FIXME:' + - reason: 'Forbidden STOPSHIP todo marker in comment, please address the problem before shipping the code.' + value: 'STOPSHIP:' + ForbiddenVoid: + active: true + ignoreOverridden: false + ignoreUsageInGenerics: false + FunctionOnlyReturningConstant: + active: true + ignoreOverridableFunction: true + ignoreActualFunction: true + LoopWithTooManyJumpStatements: + active: true + maxJumpCount: 1 + MagicNumber: + active: true + excludes: ['**/test/**', '**/*.kts', '**/demo/**'] + ignoreNumbers: + - '-1' + - '0' + - '1' + - '2' + ignoreHashCodeFunction: true + ignorePropertyDeclaration: false + ignoreConstantDeclaration: true + ignoreCompanionObjectPropertyDeclaration: true + ignoreAnnotation: false + ignoreNamedArgument: true + ignoreEnums: false + ignoreExtensionFunctions: true + MaxLineLength: + active: true + maxLineLength: 120 + excludePackageStatements: true + excludeImportStatements: true + excludeCommentStatements: false + excludeRawStrings: true + MayBeConstant: + active: true + ModifierOrder: + active: true + NestedClassesVisibility: + active: true + NewLineAtEndOfFile: + active: true + ObjectLiteralToLambda: + active: true + OptionalAbstractKeyword: + active: true + ProtectedMemberInFinalClass: + active: true + RedundantHigherOrderMapUsage: + active: true + ReturnCount: + active: true + max: 6 + excludedFunctions: + - 'equals' + excludeLabeled: false + excludeReturnFromLambda: true + excludeGuardClauses: true + SafeCast: + active: true + SerialVersionUIDInSerializableClass: + active: true + ThrowsCount: + active: true + max: 2 + UnnecessaryApply: + active: true + UnnecessaryFilter: + active: true + UnnecessaryInheritance: + active: true + UnusedParameter: + active: true + allowedNames: 'ignored|expected' + UnusedPrivateClass: + active: true + UnusedPrivateFunction: + active: true + allowedNames: '' + UnusedPrivateProperty: + active: true + allowedNames: 'ignored|expected|serialVersionUID' + UnusedVariable: + active: true + allowedNames: 'ignored|_' + UseAnyOrNoneInsteadOfFind: + active: true + UseArrayLiteralsInAnnotations: + active: true + UseCheckNotNull: + active: true + UseCheckOrError: + active: true + UseIsNullOrEmpty: + active: true + UseOrEmpty: + active: true + UseRequire: + active: true + UseRequireNotNull: + active: true + UselessCallOnNotNull: + active: true + UtilityClassWithPublicConstructor: + active: true + VarCouldBeVal: + active: true + ignoreLateinitVar: false + WildcardImport: + active: true + excludeImports: + - 'java.util.*' diff --git a/demo/build.gradle.kts b/demo/build.gradle.kts index 596e35b5..e8880d7d 100644 --- a/demo/build.gradle.kts +++ b/demo/build.gradle.kts @@ -1,11 +1,10 @@ -import org.jetbrains.compose.desktop.application.dsl.TargetFormat -import org.gradle.api.tasks.JavaExec +import io.github.kdroidfilter.nucleus.desktop.application.dsl.TargetFormat plugins { alias(libs.plugins.multiplatform) alias(libs.plugins.compose.compiler) alias(libs.plugins.compose) - id("io.github.kdroidfilter.compose.linux.packagedeps") version "0.2.2" + alias(libs.plugins.nucleus) } kotlin { @@ -27,43 +26,55 @@ kotlin { } } +nucleus.application { + mainClass = "com.kdroid.composetray.demo.DynamicTrayMenuKt" -compose.desktop { - application { - mainClass = "com.kdroid.composetray.demo.DynamicTrayMenuKt" - nativeDistributions { - targetFormats(TargetFormat.Msi, TargetFormat.Deb, TargetFormat.Dmg) - packageName = "tray-demo" - packageVersion = "1.0.0" + buildTypes { + release { + proguard { + isEnabled = true + optimize = true + obfuscate = false + configurationFiles.from(project.file("proguard-rules.pro")) + } } - buildTypes.release.proguard { - isEnabled = true - obfuscate.set(false) - optimize.set(true) - configurationFiles.from(project.file("proguard-rules.pro")) + } + + graalvm { + isEnabled = true + javaLanguageVersion = 25 + jvmVendor = JvmVendorSpec.BELLSOFT + imageName = "composetray-demo" + march = providers.gradleProperty("nativeMarch").getOrElse("compatibility") + buildArgs.addAll( + "-H:+AddAllCharsets", + "-Djava.awt.headless=false", + "-Os", + "-H:-IncludeMethodData", + ) + } + + nativeDistributions { + targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb) + packageName = "tray-demo" + packageVersion = "1.0.0" + + macOS { + bundleID = "com.kdroid.composetray.demo" + appCategory = "public.app-category.utilities" + dockName = "TrayDemo" } } } // Task to build native libraries and run the demo tasks.register("buildAndRunDemo") { - // Depend on the buildNativeLibraries task from the root project dependsOn(rootProject.tasks.named("buildNativeLibraries")) - - // This task doesn't do anything by itself, it just depends on buildNativeLibraries - // and will be followed by the run task doLast { println("Native libraries built successfully. Starting demo application...") } - - // Make sure the run task is executed after this task finalizedBy(tasks.named("run")) - - // Description for the task description = "Builds the native libraries and then runs the demo application" group = "application" } -linuxDebConfig { - startupWMClass.set("com.kdroid.composetray.demo.DynamicTrayMenuKt") -} \ No newline at end of file diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 4c3f1cec..ccac2f54 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -5,6 +5,9 @@ kotlinx-coroutines = "1.10.2" compose = "1.10.0" jna = "5.18.1" platformtools = "0.7.5" +detekt = "1.23.7" +ktlint = "12.1.2" +nucleus = "1.9.0" [libraries] kermit = { module = "co.touchlab:kermit", version.ref = "kermit" } @@ -16,9 +19,11 @@ platformtools-core = { module = "io.github.kdroidfilter:platformtools.core", ver platformtools-darkmodedetector = { module = "io.github.kdroidfilter:platformtools.darkmodedetector", version.ref = "platformtools" } [plugins] - multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" } compose = { id = "org.jetbrains.compose", version.ref = "compose" } compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" } -vannitktech-maven-publish = {id = "com.vanniktech.maven.publish", version = "0.33.0"} -dokka = { id = "org.jetbrains.dokka" , version = "2.0.0"} +vannitktech-maven-publish = { id = "com.vanniktech.maven.publish", version = "0.33.0" } +dokka = { id = "org.jetbrains.dokka", version = "2.0.0" } +detekt = { id = "io.gitlab.arturbosch.detekt", version.ref = "detekt" } +ktlint = { id = "org.jlleitschuh.gradle.ktlint", version.ref = "ktlint" } +nucleus = { id = "io.github.kdroidfilter.nucleus", version.ref = "nucleus" } diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index 249e5832f090a2944b7473328c07c9755baa3196..a4b76b9530d66f5e68d973ea569d8e19de379189 100644 GIT binary patch literal 43583 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 yu?e%!_ptzGB|8GrCfu%p?(_ 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 GZ~q4|mN%#X literal 60756 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 /dev/null && printf '%s\n' "$PWD" ) || exit # Use the maximum available, or set MAX_FD != -1 to use that value. MAX_FD=maximum @@ -133,22 +133,29 @@ 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. + if ! command -v java >/dev/null 2>&1 + then + 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 fi # Increase the maximum file descriptors if we can. if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then case $MAX_FD in #( max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 MAX_FD=$( ulimit -H -n ) || warn "Could not query maximum file descriptor limit" esac case $MAX_FD in #( '' | soft) :;; #( *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 ulimit -n "$MAX_FD" || warn "Could not set maximum file descriptor limit to $MAX_FD" esac @@ -193,11 +200,15 @@ if "$cygwin" || "$msys" ; then done fi -# Collect all arguments for the java command; -# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of -# shell script including quotes and variable substitutions, so put them in -# double quotes to make sure that they get re-expanded; and -# * put everything else in single quotes, so that it's not re-expanded. + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. set -- \ "-Dorg.gradle.appname=$APP_BASE_NAME" \ @@ -205,6 +216,12 @@ set -- \ org.gradle.wrapper.GradleWrapperMain \ "$@" +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + # Use "xargs" to parse quoted args. # # With -n1 it outputs one arg per line, with the quotes and backslashes removed. diff --git a/gradlew.bat b/gradlew.bat index 107acd32..9d21a218 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -13,8 +13,10 @@ @rem See the License for the specific language governing permissions and @rem limitations under the License. @rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem -@if "%DEBUG%" == "" @echo off +@if "%DEBUG%"=="" @echo off @rem ########################################################################## @rem @rem Gradle startup script for Windows @@ -25,7 +27,8 @@ if "%OS%"=="Windows_NT" setlocal set DIRNAME=%~dp0 -if "%DIRNAME%" == "" set DIRNAME=. +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused set APP_BASE_NAME=%~n0 set APP_HOME=%DIRNAME% @@ -40,13 +43,13 @@ if defined JAVA_HOME goto findJavaFromJavaHome set JAVA_EXE=java.exe %JAVA_EXE% -version >NUL 2>&1 -if "%ERRORLEVEL%" == "0" goto execute +if %ERRORLEVEL% equ 0 goto execute -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. +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 goto fail @@ -56,11 +59,11 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe if exist "%JAVA_EXE%" goto execute -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. +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 goto fail @@ -75,13 +78,15 @@ set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar :end @rem End local scope for the variables with windows NT shell -if "%ERRORLEVEL%"=="0" goto mainEnd +if %ERRORLEVEL% equ 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 +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% :mainEnd if "%OS%"=="Windows_NT" endlocal diff --git a/linuxlib b/linuxlib deleted file mode 160000 index c0d6c8a8..00000000 --- a/linuxlib +++ /dev/null @@ -1 +0,0 @@ -Subproject commit c0d6c8a855765e27446b35674d7f107af41529b8 diff --git a/settings.gradle.kts b/settings.gradle.kts index ecc3fa48..5321d954 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,7 +1,5 @@ rootProject.name = "ComposeNativeTray" -include(":demo") - pluginManagement { repositories { google { diff --git a/src/commonMain/kotlin/com/kdroid/composetray/lib/linux/LinuxLibTray.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/LinuxLibTray.kt similarity index 92% rename from src/commonMain/kotlin/com/kdroid/composetray/lib/linux/LinuxLibTray.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/LinuxLibTray.kt index 0153a4bf..7b9626e5 100644 --- a/src/commonMain/kotlin/com/kdroid/composetray/lib/linux/LinuxLibTray.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/LinuxLibTray.kt @@ -1,11 +1,11 @@ package com.kdroid.composetray.lib.linux +import com.kdroid.composetray.utils.NativeLibraryLoader import com.sun.jna.Callback -import com.sun.jna.Native import com.sun.jna.ptr.IntByReference /** - * JNA direct mapping to the Go-based systray bridge (linuxlibnew/jna/bridge_linux.go). + * JNA direct mapping to the Go-based systray bridge (src/native/linux/). */ internal object LinuxLibTray { // Callback types ------------------------------------------------------------- @@ -14,8 +14,7 @@ internal object LinuxLibTray { // Library registration logic (simple and consistent with Windows/Mac) -------- init { - // Register the native library "systray" for direct calls - Native.register("systray") + NativeLibraryLoader.extractAndRegister("systray", LinuxLibTray::class.java) } // Helpers to fetch last click xy diff --git a/src/commonMain/kotlin/com/kdroid/composetray/lib/linux/LinuxOutsideClickWatcher.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/LinuxOutsideClickWatcher.kt similarity index 100% rename from src/commonMain/kotlin/com/kdroid/composetray/lib/linux/LinuxOutsideClickWatcher.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/LinuxOutsideClickWatcher.kt diff --git a/src/commonMain/kotlin/com/kdroid/composetray/lib/linux/LinuxTrayManager.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/LinuxTrayManager.kt similarity index 100% rename from src/commonMain/kotlin/com/kdroid/composetray/lib/linux/LinuxTrayManager.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/LinuxTrayManager.kt diff --git a/src/commonMain/kotlin/com/kdroid/composetray/lib/mac/MacNativeBridge.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacNativeBridge.kt similarity index 98% rename from src/commonMain/kotlin/com/kdroid/composetray/lib/mac/MacNativeBridge.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacNativeBridge.kt index 6d30c1ec..97b8d295 100644 --- a/src/commonMain/kotlin/com/kdroid/composetray/lib/mac/MacNativeBridge.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacNativeBridge.kt @@ -20,7 +20,7 @@ internal object MacNativeBridge { arch.contains("aarch64") || arch.contains("arm64") -> "darwin-aarch64" else -> "darwin-x86-64" } - val resourcePath = "$resourceDir/libMacTray.dylib" + val resourcePath = "composetray/native/$resourceDir/libMacTray.dylib" // Try to find the dylib on the classpath (inside a JAR or on disk) val url = MacNativeBridge::class.java.classLoader?.getResource(resourcePath) diff --git a/src/commonMain/kotlin/com/kdroid/composetray/lib/mac/MacOSMenuBarThemeDetector.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacOSMenuBarThemeDetector.kt similarity index 100% rename from src/commonMain/kotlin/com/kdroid/composetray/lib/mac/MacOSMenuBarThemeDetector.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacOSMenuBarThemeDetector.kt diff --git a/src/commonMain/kotlin/com/kdroid/composetray/lib/mac/MacOsWindowManager.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacOsWindowManager.kt similarity index 100% rename from src/commonMain/kotlin/com/kdroid/composetray/lib/mac/MacOsWindowManager.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacOsWindowManager.kt diff --git a/src/commonMain/kotlin/com/kdroid/composetray/lib/mac/MacOutsideClickWatcher.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacOutsideClickWatcher.kt similarity index 100% rename from src/commonMain/kotlin/com/kdroid/composetray/lib/mac/MacOutsideClickWatcher.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacOutsideClickWatcher.kt diff --git a/src/commonMain/kotlin/com/kdroid/composetray/lib/mac/MacTrayManager.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacTrayManager.kt similarity index 100% rename from src/commonMain/kotlin/com/kdroid/composetray/lib/mac/MacTrayManager.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacTrayManager.kt diff --git a/src/commonMain/kotlin/com/kdroid/composetray/lib/windows/StdCallCallback.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/StdCallCallback.kt similarity index 100% rename from src/commonMain/kotlin/com/kdroid/composetray/lib/windows/StdCallCallback.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/StdCallCallback.kt diff --git a/src/commonMain/kotlin/com/kdroid/composetray/lib/windows/WindowsNativeTray.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsNativeTray.kt similarity index 100% rename from src/commonMain/kotlin/com/kdroid/composetray/lib/windows/WindowsNativeTray.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsNativeTray.kt diff --git a/src/commonMain/kotlin/com/kdroid/composetray/lib/windows/WindowsNativeTrayLibrary.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsNativeTrayLibrary.kt similarity index 89% rename from src/commonMain/kotlin/com/kdroid/composetray/lib/windows/WindowsNativeTrayLibrary.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsNativeTrayLibrary.kt index dbfcd91a..42bfb939 100644 --- a/src/commonMain/kotlin/com/kdroid/composetray/lib/windows/WindowsNativeTrayLibrary.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsNativeTrayLibrary.kt @@ -1,6 +1,6 @@ package com.kdroid.composetray.lib.windows -import com.sun.jna.Native +import com.kdroid.composetray.utils.NativeLibraryLoader import com.sun.jna.Pointer import com.sun.jna.ptr.IntByReference import com.sun.jna.win32.StdCallLibrary @@ -16,8 +16,7 @@ internal object WindowsNativeTrayLibrary : StdCallLibrary { if (System.getProperty(key).isNullOrBlank()) { System.setProperty(key, "UTF-8") } - // Register the native library "tray" for direct calls - Native.register("tray") + NativeLibraryLoader.extractAndRegister("tray", WindowsNativeTrayLibrary::class.java) } @JvmStatic external fun tray_get_instance(): Pointer? diff --git a/src/commonMain/kotlin/com/kdroid/composetray/lib/windows/WindowsNativeTrayMenuItem.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsNativeTrayMenuItem.kt similarity index 100% rename from src/commonMain/kotlin/com/kdroid/composetray/lib/windows/WindowsNativeTrayMenuItem.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsNativeTrayMenuItem.kt diff --git a/src/commonMain/kotlin/com/kdroid/composetray/lib/windows/WindowsOutsideClickWatcher.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsOutsideClickWatcher.kt similarity index 100% rename from src/commonMain/kotlin/com/kdroid/composetray/lib/windows/WindowsOutsideClickWatcher.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsOutsideClickWatcher.kt diff --git a/src/commonMain/kotlin/com/kdroid/composetray/lib/windows/WindowsTrayManager.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsTrayManager.kt similarity index 100% rename from src/commonMain/kotlin/com/kdroid/composetray/lib/windows/WindowsTrayManager.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsTrayManager.kt diff --git a/src/commonMain/kotlin/com/kdroid/composetray/menu/api/TrayMenuBuilder.kt b/src/jvmMain/kotlin/com/kdroid/composetray/menu/api/TrayMenuBuilder.kt similarity index 100% rename from src/commonMain/kotlin/com/kdroid/composetray/menu/api/TrayMenuBuilder.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/menu/api/TrayMenuBuilder.kt diff --git a/src/commonMain/kotlin/com/kdroid/composetray/menu/impl/AwtTrayMenuBuilderImpl.kt b/src/jvmMain/kotlin/com/kdroid/composetray/menu/impl/AwtTrayMenuBuilderImpl.kt similarity index 100% rename from src/commonMain/kotlin/com/kdroid/composetray/menu/impl/AwtTrayMenuBuilderImpl.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/menu/impl/AwtTrayMenuBuilderImpl.kt diff --git a/src/commonMain/kotlin/com/kdroid/composetray/menu/impl/LinuxTrayMenuBuilderImpl.kt b/src/jvmMain/kotlin/com/kdroid/composetray/menu/impl/LinuxTrayMenuBuilderImpl.kt similarity index 100% rename from src/commonMain/kotlin/com/kdroid/composetray/menu/impl/LinuxTrayMenuBuilderImpl.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/menu/impl/LinuxTrayMenuBuilderImpl.kt diff --git a/src/commonMain/kotlin/com/kdroid/composetray/menu/impl/MacTrayMenuBuilderImpl.kt b/src/jvmMain/kotlin/com/kdroid/composetray/menu/impl/MacTrayMenuBuilderImpl.kt similarity index 100% rename from src/commonMain/kotlin/com/kdroid/composetray/menu/impl/MacTrayMenuBuilderImpl.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/menu/impl/MacTrayMenuBuilderImpl.kt diff --git a/src/commonMain/kotlin/com/kdroid/composetray/menu/impl/WindowsTrayMenuBuilderImpl.kt b/src/jvmMain/kotlin/com/kdroid/composetray/menu/impl/WindowsTrayMenuBuilderImpl.kt similarity index 100% rename from src/commonMain/kotlin/com/kdroid/composetray/menu/impl/WindowsTrayMenuBuilderImpl.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/menu/impl/WindowsTrayMenuBuilderImpl.kt diff --git a/src/commonMain/kotlin/com/kdroid/composetray/tray/api/ExperimentalTrayAppApi.kt b/src/jvmMain/kotlin/com/kdroid/composetray/tray/api/ExperimentalTrayAppApi.kt similarity index 100% rename from src/commonMain/kotlin/com/kdroid/composetray/tray/api/ExperimentalTrayAppApi.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/tray/api/ExperimentalTrayAppApi.kt diff --git a/src/commonMain/kotlin/com/kdroid/composetray/tray/api/NativeTray.kt b/src/jvmMain/kotlin/com/kdroid/composetray/tray/api/NativeTray.kt similarity index 100% rename from src/commonMain/kotlin/com/kdroid/composetray/tray/api/NativeTray.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/tray/api/NativeTray.kt diff --git a/src/commonMain/kotlin/com/kdroid/composetray/tray/api/TrayApp.kt b/src/jvmMain/kotlin/com/kdroid/composetray/tray/api/TrayApp.kt similarity index 100% rename from src/commonMain/kotlin/com/kdroid/composetray/tray/api/TrayApp.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/tray/api/TrayApp.kt diff --git a/src/commonMain/kotlin/com/kdroid/composetray/tray/api/TrayAppState.kt b/src/jvmMain/kotlin/com/kdroid/composetray/tray/api/TrayAppState.kt similarity index 100% rename from src/commonMain/kotlin/com/kdroid/composetray/tray/api/TrayAppState.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/tray/api/TrayAppState.kt diff --git a/src/commonMain/kotlin/com/kdroid/composetray/tray/api/TrayWindowDismissMode.kt b/src/jvmMain/kotlin/com/kdroid/composetray/tray/api/TrayWindowDismissMode.kt similarity index 100% rename from src/commonMain/kotlin/com/kdroid/composetray/tray/api/TrayWindowDismissMode.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/tray/api/TrayWindowDismissMode.kt diff --git a/src/commonMain/kotlin/com/kdroid/composetray/tray/impl/AwtTrayInitializer.kt b/src/jvmMain/kotlin/com/kdroid/composetray/tray/impl/AwtTrayInitializer.kt similarity index 100% rename from src/commonMain/kotlin/com/kdroid/composetray/tray/impl/AwtTrayInitializer.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/tray/impl/AwtTrayInitializer.kt diff --git a/src/commonMain/kotlin/com/kdroid/composetray/tray/impl/LinuxTrayInitializer.kt b/src/jvmMain/kotlin/com/kdroid/composetray/tray/impl/LinuxTrayInitializer.kt similarity index 100% rename from src/commonMain/kotlin/com/kdroid/composetray/tray/impl/LinuxTrayInitializer.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/tray/impl/LinuxTrayInitializer.kt diff --git a/src/commonMain/kotlin/com/kdroid/composetray/tray/impl/MacTrayInitializer.kt b/src/jvmMain/kotlin/com/kdroid/composetray/tray/impl/MacTrayInitializer.kt similarity index 100% rename from src/commonMain/kotlin/com/kdroid/composetray/tray/impl/MacTrayInitializer.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/tray/impl/MacTrayInitializer.kt diff --git a/src/commonMain/kotlin/com/kdroid/composetray/tray/impl/WindowsTrayInitializer.kt b/src/jvmMain/kotlin/com/kdroid/composetray/tray/impl/WindowsTrayInitializer.kt similarity index 100% rename from src/commonMain/kotlin/com/kdroid/composetray/tray/impl/WindowsTrayInitializer.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/tray/impl/WindowsTrayInitializer.kt diff --git a/src/commonMain/kotlin/com/kdroid/composetray/utils/AppId.kt b/src/jvmMain/kotlin/com/kdroid/composetray/utils/AppId.kt similarity index 100% rename from src/commonMain/kotlin/com/kdroid/composetray/utils/AppId.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/utils/AppId.kt diff --git a/src/commonMain/kotlin/com/kdroid/composetray/utils/ComposableIconUtils.kt b/src/jvmMain/kotlin/com/kdroid/composetray/utils/ComposableIconUtils.kt similarity index 100% rename from src/commonMain/kotlin/com/kdroid/composetray/utils/ComposableIconUtils.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/utils/ComposableIconUtils.kt diff --git a/src/commonMain/kotlin/com/kdroid/composetray/utils/DarkModeDetector.kt b/src/jvmMain/kotlin/com/kdroid/composetray/utils/DarkModeDetector.kt similarity index 100% rename from src/commonMain/kotlin/com/kdroid/composetray/utils/DarkModeDetector.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/utils/DarkModeDetector.kt diff --git a/src/commonMain/kotlin/com/kdroid/composetray/utils/DebugLn.kt b/src/jvmMain/kotlin/com/kdroid/composetray/utils/DebugLn.kt similarity index 100% rename from src/commonMain/kotlin/com/kdroid/composetray/utils/DebugLn.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/utils/DebugLn.kt diff --git a/src/commonMain/kotlin/com/kdroid/composetray/utils/IconRenderProperties.kt b/src/jvmMain/kotlin/com/kdroid/composetray/utils/IconRenderProperties.kt similarity index 100% rename from src/commonMain/kotlin/com/kdroid/composetray/utils/IconRenderProperties.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/utils/IconRenderProperties.kt diff --git a/src/commonMain/kotlin/com/kdroid/composetray/utils/JarResourceExtractor.kt b/src/jvmMain/kotlin/com/kdroid/composetray/utils/JarResourceExtractor.kt similarity index 100% rename from src/commonMain/kotlin/com/kdroid/composetray/utils/JarResourceExtractor.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/utils/JarResourceExtractor.kt diff --git a/src/commonMain/kotlin/com/kdroid/composetray/utils/MenuContentHash.kt b/src/jvmMain/kotlin/com/kdroid/composetray/utils/MenuContentHash.kt similarity index 100% rename from src/commonMain/kotlin/com/kdroid/composetray/utils/MenuContentHash.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/utils/MenuContentHash.kt diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/utils/NativeLibraryLoader.kt b/src/jvmMain/kotlin/com/kdroid/composetray/utils/NativeLibraryLoader.kt new file mode 100644 index 00000000..35521c39 --- /dev/null +++ b/src/jvmMain/kotlin/com/kdroid/composetray/utils/NativeLibraryLoader.kt @@ -0,0 +1,77 @@ +package com.kdroid.composetray.utils + +import com.sun.jna.Native +import com.sun.jna.NativeLibrary +import java.io.File +import java.nio.file.Files +import java.nio.file.StandardCopyOption + +/** + * Extracts a native library from the classpath (composetray/native//) + * to a temporary directory and registers it with JNA. + * + * This is needed because JNA's default classpath search doesn't look inside + * the composetray/native/ namespace prefix. + */ +internal object NativeLibraryLoader { + + private val tempDir: File by lazy { + Files.createTempDirectory("composetray-natives").toFile().apply { deleteOnExit() } + } + + /** + * Extracts the native library from the classpath and registers it with JNA. + * + * @param libraryName the library name (e.g. "systray" or "tray") + * @param caller a class from the same classloader to use for resource lookup + */ + fun extractAndRegister(libraryName: String, caller: Class<*>) { + val platform = detectPlatform() + val fileName = mapLibraryName(libraryName) + val resourcePath = "composetray/native/$platform/$fileName" + + val url = caller.classLoader?.getResource(resourcePath) + if (url != null) { + val targetFile = File(tempDir, fileName) + if (!targetFile.exists()) { + url.openStream().use { input -> + Files.copy(input, targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING) + } + targetFile.deleteOnExit() + } + // Add the temp directory to JNA's search path + NativeLibrary.addSearchPath(libraryName, tempDir.absolutePath) + } + + Native.register(caller, libraryName) + } + + private fun detectPlatform(): String { + val os = System.getProperty("os.name")?.lowercase() ?: "" + val arch = System.getProperty("os.arch") ?: "" + return when { + os.contains("win") -> when { + arch.contains("aarch64") || arch.contains("arm") -> "win32-arm64" + else -> "win32-x86-64" + } + os.contains("linux") -> when { + arch.contains("aarch64") || arch.contains("arm") -> "linux-aarch64" + else -> "linux-x86-64" + } + os.contains("mac") -> when { + arch.contains("aarch64") || arch.contains("arm") -> "darwin-aarch64" + else -> "darwin-x86-64" + } + else -> "unknown" + } + } + + private fun mapLibraryName(name: String): String { + val os = System.getProperty("os.name")?.lowercase() ?: "" + return when { + os.contains("win") -> "$name.dll" + os.contains("mac") -> "lib$name.dylib" + else -> "lib$name.so" + } + } +} diff --git a/src/commonMain/kotlin/com/kdroid/composetray/utils/PersistentAnimatedVisibility.kt b/src/jvmMain/kotlin/com/kdroid/composetray/utils/PersistentAnimatedVisibility.kt similarity index 100% rename from src/commonMain/kotlin/com/kdroid/composetray/utils/PersistentAnimatedVisibility.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/utils/PersistentAnimatedVisibility.kt diff --git a/src/commonMain/kotlin/com/kdroid/composetray/utils/SingleInstanceManager.kt b/src/jvmMain/kotlin/com/kdroid/composetray/utils/SingleInstanceManager.kt similarity index 100% rename from src/commonMain/kotlin/com/kdroid/composetray/utils/SingleInstanceManager.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/utils/SingleInstanceManager.kt diff --git a/src/commonMain/kotlin/com/kdroid/composetray/utils/TrayPosition.kt b/src/jvmMain/kotlin/com/kdroid/composetray/utils/TrayPosition.kt similarity index 100% rename from src/commonMain/kotlin/com/kdroid/composetray/utils/TrayPosition.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/utils/TrayPosition.kt diff --git a/src/commonMain/kotlin/com/kdroid/composetray/utils/WindowRaise.kt b/src/jvmMain/kotlin/com/kdroid/composetray/utils/WindowRaise.kt similarity index 100% rename from src/commonMain/kotlin/com/kdroid/composetray/utils/WindowRaise.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/utils/WindowRaise.kt diff --git a/src/commonMain/kotlin/com/kdroid/composetray/utils/WindowVisibilityMonitor.kt b/src/jvmMain/kotlin/com/kdroid/composetray/utils/WindowVisibilityMonitor.kt similarity index 100% rename from src/commonMain/kotlin/com/kdroid/composetray/utils/WindowVisibilityMonitor.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/utils/WindowVisibilityMonitor.kt diff --git a/src/jvmMain/resources/META-INF/native-image/io.github.kdroidfilter/composenativetray/native-image.properties b/src/jvmMain/resources/META-INF/native-image/io.github.kdroidfilter/composenativetray/native-image.properties new file mode 100644 index 00000000..e13f6440 --- /dev/null +++ b/src/jvmMain/resources/META-INF/native-image/io.github.kdroidfilter/composenativetray/native-image.properties @@ -0,0 +1 @@ +Args = -H:IncludeResources=composetray/native/.* diff --git a/src/jvmMain/resources/META-INF/native-image/io.github.kdroidfilter/composenativetray/reachability-metadata.json b/src/jvmMain/resources/META-INF/native-image/io.github.kdroidfilter/composenativetray/reachability-metadata.json new file mode 100644 index 00000000..59dfede4 --- /dev/null +++ b/src/jvmMain/resources/META-INF/native-image/io.github.kdroidfilter/composenativetray/reachability-metadata.json @@ -0,0 +1,31 @@ +{ + "reflection": [ + { + "type": "com.kdroid.composetray.lib.mac.MacNativeBridge", + "jniAccessible": true + }, + { + "type": "com.kdroid.composetray.lib.mac.MacNativeBridge$ThemeChangeCallback", + "jniAccessible": true, + "methods": [ + { + "name": "onThemeChanged", + "parameterTypes": ["int"] + } + ] + }, + { + "type": "java.lang.Runnable", + "jniAccessible": true, + "methods": [ + { + "name": "run", + "parameterTypes": [] + } + ] + } + ], + "resources": [ + {"glob": "composetray/native/**"} + ] +} diff --git a/src/native/linux/Makefile b/src/native/linux/Makefile new file mode 100644 index 00000000..309e72d7 --- /dev/null +++ b/src/native/linux/Makefile @@ -0,0 +1,37 @@ +tag-changelog: require-version require-gh-token + echo "Tagging..." && \ + git tag -a "$$VERSION" -f --annotate -m"Tagged $$VERSION" && \ + git push --tags -f && \ + git checkout master && \ + git pull && \ + github_changelog_generator --no-issues --max-issues 100 --token "${GH_TOKEN}" --user getlantern --project systray && \ + git add CHANGELOG.md && \ + git commit -m "Updated changelog for $$VERSION" && \ + git push origin HEAD && \ + git checkout - + +guard-%: + @ if [ -z '${${*}}' ]; then echo 'Environment variable $* not set' && exit 1; fi + +require-version: guard-VERSION + +require-gh-token: guard-GH_TOKEN + +.PHONY: build-so +build-so: + GOOS=linux GOARCH=$${GOARCH:-amd64} CGO_ENABLED=1 \ + CGO_CFLAGS="-Os -ffunction-sections -fdata-sections -fPIC" \ + CGO_LDFLAGS="-Wl,-O1 -Wl,--as-needed -Wl,--gc-sections -Wl,-s" \ + go build -buildmode=c-shared -trimpath -gcflags=all=-l -ldflags "-s -w -buildid= -linkmode=external -extldflags '-Wl,-O1 -Wl,--as-needed -Wl,-s -Wl,--gc-sections'" -o dist/libsystray.so ./jna + strip --strip-unneeded -R .comment -R .note.GNU-stack -R .note.gnu.property -R .note.go.buildid dist/libsystray.so || true + @echo "Built dist/libsystray.so and dist/libsystray.h (extra size-optimized)" + + +.PHONY: build-so-tinygo +build-so-tinygo: + @mkdir -p dist + @ARCH=$${GOARCH:-amd64}; \ + TARGET=linux-$${ARCH}; \ + echo "Building TinyGo shared library for target $$TARGET..."; \ + tinygo build -buildmode=c-shared -target=$$TARGET -no-debug -opt=z -o dist/libsystray_tinygo.so ./jna && \ + echo "Built dist/libsystray_tinygo.so and dist/libsystray_tinygo.h (TinyGo)" diff --git a/src/native/linux/build.sh b/src/native/linux/build.sh new file mode 100755 index 00000000..0ee46796 --- /dev/null +++ b/src/native/linux/build.sh @@ -0,0 +1,34 @@ +#!/bin/bash + +# Exit on any error +set -e + +echo "Building Linux systray shared library..." + +# Ensure we run from this script's directory (linuxlib) +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" +cd "$SCRIPT_DIR" + +# Use NATIVE_LIBS_OUTPUT_DIR env var if set, otherwise default to src/jvmMain/resources/composetray/native +OUTPUT_DIR="${NATIVE_LIBS_OUTPUT_DIR:-$SCRIPT_DIR/../../jvmMain/resources/composetray/native}" +echo "Output dir for linux is: $OUTPUT_DIR" + +# Inform about architecture if provided +if [[ -n "$GOARCH" ]]; then + echo "Using GOARCH=$GOARCH" +else + echo "GOARCH not set, defaulting to amd64 (as per Makefile)" +fi + +# Build the shared library using the provided Makefile target +make build-so + +# Destination directory +DEST_DIR="$OUTPUT_DIR/linux-x86-64" +mkdir -p "$DEST_DIR" + +# Copy the generated .so to the destination directory +cp -f dist/libsystray.so "$DEST_DIR/libsystray.so" + +echo "Copied dist/libsystray.so to $DEST_DIR/libsystray.so" +echo "Linux build completed successfully." diff --git a/src/native/linux/go.mod b/src/native/linux/go.mod new file mode 100644 index 00000000..4fe3b17a --- /dev/null +++ b/src/native/linux/go.mod @@ -0,0 +1,27 @@ +module github.com/energye/systray + +go 1.23.0 + +toolchain go1.24.5 + +require golang.org/x/sys v0.34.0 + +require ( + github.com/godbus/dbus/v5 v5.0.4 + github.com/tevino/abool v0.0.0-20220530134649-2bfc934cb23c +) + +require ( + github.com/google/go-cmp v0.6.0 // indirect + github.com/yuin/goldmark v1.4.13 // indirect + golang.org/x/crypto v0.40.0 // indirect + golang.org/x/image v0.30.0 // indirect + golang.org/x/mod v0.26.0 // indirect + golang.org/x/net v0.42.0 // indirect + golang.org/x/sync v0.16.0 // indirect + golang.org/x/telemetry v0.0.0-20250710130107-8d8967aff50b // indirect + golang.org/x/term v0.33.0 // indirect + golang.org/x/text v0.28.0 // indirect + golang.org/x/tools v0.35.0 // indirect + golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 // indirect +) diff --git a/src/native/linux/go.sum b/src/native/linux/go.sum new file mode 100644 index 00000000..de4e7f32 --- /dev/null +++ b/src/native/linux/go.sum @@ -0,0 +1,21 @@ +github.com/godbus/dbus/v5 v5.0.4 h1:9349emZab16e7zQvpmsbtjc18ykshndd8y2PG3sgJbA= +github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/tevino/abool v0.0.0-20220530134649-2bfc934cb23c h1:coVla7zpsycc+kA9NXpcvv2E4I7+ii6L5hZO2S6C3kw= +github.com/tevino/abool v0.0.0-20220530134649-2bfc934cb23c/go.mod h1:qc66Pna1RiIsPa7O4Egxxs9OqkuxDX55zznh9K07Tzg= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/crypto v0.40.0/go.mod h1:Qr1vMER5WyS2dfPHAlsOj01wgLbsyWtFn/aY+5+ZdxY= +golang.org/x/image v0.30.0 h1:jD5RhkmVAnjqaCUXfbGBrn3lpxbknfN9w2UhHHU+5B4= +golang.org/x/image v0.30.0/go.mod h1:SAEUTxCCMWSrJcCy/4HwavEsfZZJlYxeHLc6tTiAe/c= +golang.org/x/mod v0.26.0/go.mod h1:/j6NAhSk8iQ723BGAUyoAcn7SlD7s15Dp9Nd/SfeaFQ= +golang.org/x/net v0.42.0/go.mod h1:FF1RA5d3u7nAYA4z2TkclSCKh68eSXtiFwcWQpPXdt8= +golang.org/x/sync v0.16.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= +golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.34.0 h1:H5Y5sJ2L2JRdyv7ROF1he/lPdvFsd0mJHFw2ThKHxLA= +golang.org/x/sys v0.34.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= +golang.org/x/telemetry v0.0.0-20250710130107-8d8967aff50b/go.mod h1:4ZwOYna0/zsOKwuR5X/m0QFOJpSZvAxFfkQT+Erd9D4= +golang.org/x/term v0.33.0/go.mod h1:s18+ql9tYWp1IfpV9DmCtQDDSRBUjKaw9M1eAv5UeF0= +golang.org/x/text v0.28.0/go.mod h1:U8nCwOR8jO/marOQ0QbDiOngZVEBB7MAiitBuMjXiNU= +golang.org/x/tools v0.35.0/go.mod h1:NKdj5HkL/73byiZSJjqJgKn3ep7KjFkBOkR/Hps3VPw= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/src/native/linux/icon/icon.png b/src/native/linux/icon/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..420c618771688b74da662ab95e5e95d87aa7bbe6 GIT binary patch literal 2238 zcmbVOX;f2Z8on$=77?t7*os_(C?k-4BV|iSqM%q-K^99$E+Im4LlzPOn6Nv7ENY8_ zAW#t!?PWLYa?MaV+nc$tDDfdD!Wj)OoyA%_nILmXae zatGuA07mhG&>7fgVa4LLh`>98f7t5=m)FPt;pp8nV|PV^H8* z6YY;&0fC?|EP+508tcZzlCdBehsF_z6bi)^#A9(-3>J^U;oWc)8UatkV!`(h z3b7{P#nXbB?Dw`1$rF_zlZj~92$!e3iWZl)t1VF zp+DUCsJ1jTRSaQ*At@}EaFKe%J1&8d-2J&By&~cb?MsOODGDcv33KH_NF?)PdZLgU zG*7^zktuj0gM}lpn0PFiio;Q-;12|lPNH6Ql^DbH@BoU-QJhlWDf^YLn6MV!z3kjC!OQy3}|10iySl<7WGYsMk zM&BI&)htU#NC)b-A6g#~J`4{eLMBFnj7H3J(l7uRPx&$Fp~}8%;XQB|%g%Fdi~?_9hJ9Y9ZiWz+!%MbKGM#qtIf6O>|mGw>i1- zq-litUu?{UPbV)=cUQjXy3$z3_{e1@u4oi!MKNcnxT zxi*8{Kbe2K=&p9*zH{PZboyM|bXgRGXfop}BoF!a?Jt=vnSfg@HdOH!57zdi+!XKm z-A64qxv{ytC55XMhsy}i?S1g6<`K=%HM|+J;kG&tQd7OT~Ag8zDb2v&f4e2m6vPx*DH#QD~X@8 z`Z9F`A(aZev0v8y_52^sP==Wv?jAX(KR<8_xYXuM`q$MHYcLt$Ibr+g18bHwc-!EW zS*>mEs$ZRHpE=8qZddmV>>9J4(UFDz=E*kx_D%b`X%||zOrQb}tt`2H;E_dZ`}z}U zZMVH9KG75l7SmHQiyY;3a$JIyCcc4LZbpsL9{Q!Hci`;0tgk}{BO*u6(pLQXBS*3$ zBznbW{&#UH&S61i|1R7<&$@apZS;y`Q{RRl>+mf`)bqOOV?((lV*ct)iWT3w?~&fz z7kNGEM333M)oTNffuY@|LknGI z$rWJy+4}~^9-SNy&k-%Zb5xf-pCWWrr<=dBzJ^=0p*RG*2wK&^D60%C_{oKXEurU? zJ$YSasOe+F;RV4%guCP37FM-9%|TjDe6DNz;U3d94=3b4K}!6qY>s9|7rn921ccmw zy1y7~WL*FHLCz3r%D#84`dqhW7E{Vk9eFwvYn@|BhMFC=^+I4mwVk7p8rrH{-ySw- z`8q58Y3hL6*ZqwwyM=j|dKC(FF}uX#@FL!Q)R2B0b9KJjChg1dq3?RraRmUSlH0`<=>EU|yn z{%7~&Few*lKpDU8*w04@oyOud8F9IrtI!%oPQ$V(FM|egQYRrfYgJ&x7%g_!{hH$` z7o`sE_cG*@QOf`;&BNFqTD-58U-*KVe2RZlQr6foxQ8{>9eG~oa<(+-Nde%jD#a*{ z`Kuk9DJm1-;gy3STJNLO{=UW>c0f-U;ll;a6p+^9Ivy zX*zAmlr>WR&RHAcuHr+p={|6VrS;30BKc;!!dyZ&`Sl*d%hoZ8q9 Y7!v)$-59Qc^?ww8tewoiGh(y;16!JrJ^%m! literal 0 HcmV?d00001 diff --git a/src/native/linux/icon/iconunix.go b/src/native/linux/icon/iconunix.go new file mode 100644 index 00000000..f062f5e8 --- /dev/null +++ b/src/native/linux/icon/iconunix.go @@ -0,0 +1,196 @@ +//go:build linux || freebsd || openbsd || netbsd +// +build linux freebsd openbsd netbsd + +// File generated by 2goarray (http://github.com/cratonica/2goarray) + +package icon + +var Data []byte = []byte{ + 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, + 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x20, + 0x08, 0x06, 0x00, 0x00, 0x00, 0x73, 0x7a, 0x7a, 0xf4, 0x00, 0x00, 0x00, + 0x19, 0x74, 0x45, 0x58, 0x74, 0x53, 0x6f, 0x66, 0x74, 0x77, 0x61, 0x72, + 0x65, 0x00, 0x41, 0x64, 0x6f, 0x62, 0x65, 0x20, 0x49, 0x6d, 0x61, 0x67, + 0x65, 0x52, 0x65, 0x61, 0x64, 0x79, 0x71, 0xc9, 0x65, 0x3c, 0x00, 0x00, + 0x03, 0x66, 0x69, 0x54, 0x58, 0x74, 0x58, 0x4d, 0x4c, 0x3a, 0x63, 0x6f, + 0x6d, 0x2e, 0x61, 0x64, 0x6f, 0x62, 0x65, 0x2e, 0x78, 0x6d, 0x70, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x3c, 0x3f, 0x78, 0x70, 0x61, 0x63, 0x6b, 0x65, + 0x74, 0x20, 0x62, 0x65, 0x67, 0x69, 0x6e, 0x3d, 0x22, 0xef, 0xbb, 0xbf, + 0x22, 0x20, 0x69, 0x64, 0x3d, 0x22, 0x57, 0x35, 0x4d, 0x30, 0x4d, 0x70, + 0x43, 0x65, 0x68, 0x69, 0x48, 0x7a, 0x72, 0x65, 0x53, 0x7a, 0x4e, 0x54, + 0x63, 0x7a, 0x6b, 0x63, 0x39, 0x64, 0x22, 0x3f, 0x3e, 0x20, 0x3c, 0x78, + 0x3a, 0x78, 0x6d, 0x70, 0x6d, 0x65, 0x74, 0x61, 0x20, 0x78, 0x6d, 0x6c, + 0x6e, 0x73, 0x3a, 0x78, 0x3d, 0x22, 0x61, 0x64, 0x6f, 0x62, 0x65, 0x3a, + 0x6e, 0x73, 0x3a, 0x6d, 0x65, 0x74, 0x61, 0x2f, 0x22, 0x20, 0x78, 0x3a, + 0x78, 0x6d, 0x70, 0x74, 0x6b, 0x3d, 0x22, 0x41, 0x64, 0x6f, 0x62, 0x65, + 0x20, 0x58, 0x4d, 0x50, 0x20, 0x43, 0x6f, 0x72, 0x65, 0x20, 0x35, 0x2e, + 0x30, 0x2d, 0x63, 0x30, 0x36, 0x30, 0x20, 0x36, 0x31, 0x2e, 0x31, 0x33, + 0x34, 0x37, 0x37, 0x37, 0x2c, 0x20, 0x32, 0x30, 0x31, 0x30, 0x2f, 0x30, + 0x32, 0x2f, 0x31, 0x32, 0x2d, 0x31, 0x37, 0x3a, 0x33, 0x32, 0x3a, 0x30, + 0x30, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x22, 0x3e, 0x20, + 0x3c, 0x72, 0x64, 0x66, 0x3a, 0x52, 0x44, 0x46, 0x20, 0x78, 0x6d, 0x6c, + 0x6e, 0x73, 0x3a, 0x72, 0x64, 0x66, 0x3d, 0x22, 0x68, 0x74, 0x74, 0x70, + 0x3a, 0x2f, 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x77, 0x33, 0x2e, 0x6f, 0x72, + 0x67, 0x2f, 0x31, 0x39, 0x39, 0x39, 0x2f, 0x30, 0x32, 0x2f, 0x32, 0x32, + 0x2d, 0x72, 0x64, 0x66, 0x2d, 0x73, 0x79, 0x6e, 0x74, 0x61, 0x78, 0x2d, + 0x6e, 0x73, 0x23, 0x22, 0x3e, 0x20, 0x3c, 0x72, 0x64, 0x66, 0x3a, 0x44, + 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x72, + 0x64, 0x66, 0x3a, 0x61, 0x62, 0x6f, 0x75, 0x74, 0x3d, 0x22, 0x22, 0x20, + 0x78, 0x6d, 0x6c, 0x6e, 0x73, 0x3a, 0x78, 0x6d, 0x70, 0x4d, 0x4d, 0x3d, + 0x22, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x6e, 0x73, 0x2e, 0x61, + 0x64, 0x6f, 0x62, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x78, 0x61, 0x70, + 0x2f, 0x31, 0x2e, 0x30, 0x2f, 0x6d, 0x6d, 0x2f, 0x22, 0x20, 0x78, 0x6d, + 0x6c, 0x6e, 0x73, 0x3a, 0x73, 0x74, 0x52, 0x65, 0x66, 0x3d, 0x22, 0x68, + 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x6e, 0x73, 0x2e, 0x61, 0x64, 0x6f, + 0x62, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x78, 0x61, 0x70, 0x2f, 0x31, + 0x2e, 0x30, 0x2f, 0x73, 0x54, 0x79, 0x70, 0x65, 0x2f, 0x52, 0x65, 0x73, + 0x6f, 0x75, 0x72, 0x63, 0x65, 0x52, 0x65, 0x66, 0x23, 0x22, 0x20, 0x78, + 0x6d, 0x6c, 0x6e, 0x73, 0x3a, 0x78, 0x6d, 0x70, 0x3d, 0x22, 0x68, 0x74, + 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x6e, 0x73, 0x2e, 0x61, 0x64, 0x6f, 0x62, + 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x78, 0x61, 0x70, 0x2f, 0x31, 0x2e, + 0x30, 0x2f, 0x22, 0x20, 0x78, 0x6d, 0x70, 0x4d, 0x4d, 0x3a, 0x4f, 0x72, + 0x69, 0x67, 0x69, 0x6e, 0x61, 0x6c, 0x44, 0x6f, 0x63, 0x75, 0x6d, 0x65, + 0x6e, 0x74, 0x49, 0x44, 0x3d, 0x22, 0x78, 0x6d, 0x70, 0x2e, 0x64, 0x69, + 0x64, 0x3a, 0x36, 0x37, 0x32, 0x34, 0x42, 0x45, 0x31, 0x35, 0x45, 0x44, + 0x32, 0x30, 0x36, 0x38, 0x31, 0x31, 0x38, 0x38, 0x43, 0x36, 0x46, 0x32, + 0x38, 0x31, 0x35, 0x44, 0x41, 0x33, 0x43, 0x35, 0x35, 0x35, 0x22, 0x20, + 0x78, 0x6d, 0x70, 0x4d, 0x4d, 0x3a, 0x44, 0x6f, 0x63, 0x75, 0x6d, 0x65, + 0x6e, 0x74, 0x49, 0x44, 0x3d, 0x22, 0x78, 0x6d, 0x70, 0x2e, 0x64, 0x69, + 0x64, 0x3a, 0x41, 0x33, 0x42, 0x34, 0x46, 0x42, 0x36, 0x36, 0x33, 0x41, + 0x41, 0x38, 0x31, 0x31, 0x45, 0x32, 0x42, 0x32, 0x43, 0x41, 0x39, 0x37, + 0x42, 0x44, 0x33, 0x34, 0x34, 0x31, 0x45, 0x46, 0x33, 0x32, 0x22, 0x20, + 0x78, 0x6d, 0x70, 0x4d, 0x4d, 0x3a, 0x49, 0x6e, 0x73, 0x74, 0x61, 0x6e, + 0x63, 0x65, 0x49, 0x44, 0x3d, 0x22, 0x78, 0x6d, 0x70, 0x2e, 0x69, 0x69, + 0x64, 0x3a, 0x41, 0x33, 0x42, 0x34, 0x46, 0x42, 0x36, 0x35, 0x33, 0x41, + 0x41, 0x38, 0x31, 0x31, 0x45, 0x32, 0x42, 0x32, 0x43, 0x41, 0x39, 0x37, + 0x42, 0x44, 0x33, 0x34, 0x34, 0x31, 0x45, 0x46, 0x33, 0x32, 0x22, 0x20, + 0x78, 0x6d, 0x70, 0x3a, 0x43, 0x72, 0x65, 0x61, 0x74, 0x6f, 0x72, 0x54, + 0x6f, 0x6f, 0x6c, 0x3d, 0x22, 0x41, 0x64, 0x6f, 0x62, 0x65, 0x20, 0x50, + 0x68, 0x6f, 0x74, 0x6f, 0x73, 0x68, 0x6f, 0x70, 0x20, 0x43, 0x53, 0x35, + 0x20, 0x4d, 0x61, 0x63, 0x69, 0x6e, 0x74, 0x6f, 0x73, 0x68, 0x22, 0x3e, + 0x20, 0x3c, 0x78, 0x6d, 0x70, 0x4d, 0x4d, 0x3a, 0x44, 0x65, 0x72, 0x69, + 0x76, 0x65, 0x64, 0x46, 0x72, 0x6f, 0x6d, 0x20, 0x73, 0x74, 0x52, 0x65, + 0x66, 0x3a, 0x69, 0x6e, 0x73, 0x74, 0x61, 0x6e, 0x63, 0x65, 0x49, 0x44, + 0x3d, 0x22, 0x78, 0x6d, 0x70, 0x2e, 0x69, 0x69, 0x64, 0x3a, 0x45, 0x36, + 0x38, 0x31, 0x34, 0x43, 0x36, 0x41, 0x45, 0x45, 0x32, 0x30, 0x36, 0x38, + 0x31, 0x31, 0x38, 0x38, 0x43, 0x36, 0x46, 0x32, 0x38, 0x31, 0x35, 0x44, + 0x41, 0x33, 0x43, 0x35, 0x35, 0x35, 0x22, 0x20, 0x73, 0x74, 0x52, 0x65, + 0x66, 0x3a, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65, 0x6e, 0x74, 0x49, 0x44, + 0x3d, 0x22, 0x78, 0x6d, 0x70, 0x2e, 0x64, 0x69, 0x64, 0x3a, 0x36, 0x37, + 0x32, 0x34, 0x42, 0x45, 0x31, 0x35, 0x45, 0x44, 0x32, 0x30, 0x36, 0x38, + 0x31, 0x31, 0x38, 0x38, 0x43, 0x36, 0x46, 0x32, 0x38, 0x31, 0x35, 0x44, + 0x41, 0x33, 0x43, 0x35, 0x35, 0x35, 0x22, 0x2f, 0x3e, 0x20, 0x3c, 0x2f, + 0x72, 0x64, 0x66, 0x3a, 0x44, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, + 0x69, 0x6f, 0x6e, 0x3e, 0x20, 0x3c, 0x2f, 0x72, 0x64, 0x66, 0x3a, 0x52, + 0x44, 0x46, 0x3e, 0x20, 0x3c, 0x2f, 0x78, 0x3a, 0x78, 0x6d, 0x70, 0x6d, + 0x65, 0x74, 0x61, 0x3e, 0x20, 0x3c, 0x3f, 0x78, 0x70, 0x61, 0x63, 0x6b, + 0x65, 0x74, 0x20, 0x65, 0x6e, 0x64, 0x3d, 0x22, 0x72, 0x22, 0x3f, 0x3e, + 0x5d, 0xed, 0x35, 0xe2, 0x00, 0x00, 0x04, 0xee, 0x49, 0x44, 0x41, 0x54, + 0x78, 0xda, 0xc4, 0x57, 0xcf, 0x6f, 0x55, 0x45, 0x18, 0x3d, 0xf3, 0xe3, + 0xfe, 0xea, 0x7b, 0xaf, 0xa5, 0x6d, 0x0a, 0xd8, 0x34, 0xbe, 0x16, 0x83, + 0x69, 0x8c, 0x2e, 0x04, 0xe2, 0x86, 0xb8, 0x70, 0xe1, 0x06, 0x35, 0x18, + 0x13, 0x5d, 0x60, 0x8c, 0xd1, 0x68, 0xe2, 0xca, 0xb8, 0x33, 0x31, 0xf1, + 0x6f, 0x70, 0x67, 0x5c, 0xb1, 0x62, 0xe1, 0x46, 0x42, 0x8c, 0x0b, 0xe3, + 0x46, 0x34, 0x25, 0x11, 0x41, 0x14, 0xa4, 0x24, 0xa4, 0x08, 0x58, 0x0a, + 0x29, 0x14, 0x0a, 0x6d, 0xe9, 0xeb, 0xbb, 0xef, 0xce, 0x9d, 0xf1, 0xcc, + 0xbd, 0xaf, 0xa5, 0x44, 0x63, 0x49, 0xee, 0x4b, 0x78, 0xc9, 0xf7, 0xee, + 0x9d, 0x3b, 0x33, 0x77, 0xce, 0x77, 0xbe, 0xf3, 0x7d, 0x33, 0x57, 0x38, + 0xe7, 0xf0, 0x38, 0x7f, 0x7a, 0xab, 0x01, 0xe2, 0xd9, 0x37, 0xff, 0xeb, + 0xb1, 0xa2, 0x7d, 0x46, 0xdb, 0xeb, 0x87, 0xd0, 0x8e, 0xd3, 0xbe, 0xf8, + 0xd7, 0x28, 0x6b, 0xe1, 0x2e, 0x7c, 0xf3, 0xbf, 0xef, 0x97, 0x5b, 0x42, + 0x34, 0x06, 0xf0, 0x2c, 0x6d, 0x36, 0xe0, 0x43, 0xda, 0x88, 0x90, 0xf2, + 0x90, 0xea, 0x6f, 0xbc, 0x0b, 0x21, 0x9e, 0x67, 0xfb, 0x8d, 0xa2, 0xcf, + 0x76, 0xc7, 0x70, 0x5e, 0xff, 0x40, 0x7f, 0x75, 0x06, 0xc6, 0x27, 0x9a, + 0xb8, 0x76, 0x63, 0xbe, 0x70, 0x53, 0xf0, 0x2f, 0xe7, 0x02, 0xd6, 0xda, + 0x71, 0x36, 0xaf, 0xd1, 0x0e, 0xcb, 0x38, 0x16, 0x5c, 0xf4, 0x7a, 0xbe, + 0xbc, 0xd2, 0x14, 0x71, 0x04, 0x19, 0x68, 0xf8, 0xb0, 0x4a, 0xda, 0x2e, + 0xce, 0xad, 0x0c, 0x60, 0xf7, 0x53, 0xbb, 0x90, 0x87, 0x11, 0x76, 0x8f, + 0xee, 0x40, 0xa8, 0x35, 0xfe, 0x9a, 0xbf, 0x35, 0x36, 0x73, 0xf9, 0xea, + 0x24, 0xd2, 0xf4, 0x20, 0x5d, 0x2d, 0xbc, 0x15, 0x49, 0x0c, 0x1d, 0x86, + 0xdf, 0x09, 0x25, 0x7f, 0x80, 0x14, 0xd3, 0x8e, 0x20, 0x93, 0x30, 0x44, + 0xa3, 0xd1, 0xd8, 0x12, 0x80, 0xdc, 0x3a, 0x02, 0x06, 0xa4, 0xba, 0xb0, + 0x5a, 0x12, 0x2b, 0x2e, 0xf4, 0x35, 0xb4, 0x3e, 0x58, 0x84, 0xde, 0xb3, + 0x91, 0xa6, 0x64, 0x86, 0xf7, 0x4a, 0xbe, 0x4a, 0xcf, 0x8f, 0xe4, 0x26, + 0x0f, 0x42, 0xa5, 0xf0, 0xcc, 0xe8, 0x13, 0x50, 0xfe, 0x79, 0x65, 0x11, + 0xf2, 0x1d, 0x86, 0x62, 0x9a, 0x9a, 0xbe, 0x88, 0xa7, 0x77, 0x8e, 0x04, + 0x9d, 0x34, 0x1b, 0x45, 0xda, 0x81, 0xf7, 0xde, 0x53, 0x9d, 0x77, 0x32, + 0x04, 0x49, 0x82, 0x88, 0x1e, 0x67, 0xb9, 0xa9, 0x37, 0xe2, 0x44, 0x3c, + 0x39, 0x3c, 0x84, 0xa8, 0x1b, 0x8a, 0xca, 0x00, 0xba, 0xbf, 0x28, 0x35, + 0xe6, 0xc0, 0x9f, 0x17, 0x2f, 0x7d, 0x20, 0xad, 0x6d, 0xc2, 0xe4, 0xd8, + 0x10, 0x45, 0x10, 0x20, 0xd0, 0x01, 0xfa, 0x09, 0xa2, 0x16, 0x85, 0x13, + 0xf5, 0x28, 0x3a, 0x1a, 0x28, 0x75, 0x98, 0x4b, 0x7f, 0xcf, 0xde, 0x56, + 0xe5, 0x10, 0xf0, 0xf7, 0x36, 0x6d, 0x4a, 0x0a, 0x71, 0x14, 0x4a, 0x1d, + 0xb0, 0x7e, 0xce, 0x3a, 0xb5, 0xbc, 0x2a, 0xea, 0x22, 0x50, 0x92, 0x11, + 0x90, 0xd0, 0x8a, 0xdc, 0x0b, 0xbc, 0xc2, 0x1e, 0x9f, 0x7b, 0xbf, 0xd0, + 0x3e, 0xea, 0x05, 0x80, 0x8f, 0x69, 0xfb, 0x7c, 0x76, 0x81, 0x8e, 0x23, + 0x75, 0xa5, 0x75, 0x31, 0x68, 0x0f, 0x80, 0x66, 0xac, 0x44, 0x9a, 0x09, + 0x38, 0x5b, 0xbe, 0x92, 0xdd, 0xcf, 0xd1, 0xde, 0xab, 0x1c, 0x82, 0x95, + 0x54, 0xdf, 0x58, 0x5a, 0xe3, 0xab, 0x3a, 0x0e, 0xf5, 0xc8, 0x61, 0x72, + 0x4c, 0x53, 0x5c, 0x0e, 0x27, 0x67, 0xb2, 0x62, 0x76, 0x28, 0x55, 0x51, + 0x97, 0xf6, 0x8c, 0x4b, 0xf4, 0x45, 0xc0, 0xad, 0x7b, 0xe4, 0xbd, 0x23, + 0xb0, 0xda, 0x21, 0x51, 0x10, 0x57, 0x2a, 0x03, 0x38, 0xb4, 0xf7, 0xef, + 0x99, 0xe0, 0x85, 0x35, 0x34, 0x87, 0x15, 0x26, 0x77, 0x0e, 0xa3, 0x39, + 0x5e, 0x73, 0xc7, 0x7e, 0x5a, 0xc5, 0x5b, 0x9f, 0xcf, 0x09, 0xd4, 0x15, + 0x19, 0x50, 0x94, 0x84, 0xc4, 0xfb, 0x2f, 0x25, 0x78, 0x6d, 0x9f, 0xc4, + 0xed, 0x85, 0x0c, 0x73, 0xf7, 0x52, 0xcc, 0x2e, 0x38, 0x5c, 0x5f, 0x74, + 0xe7, 0x2a, 0x03, 0xf8, 0xe4, 0xe5, 0x9b, 0xe7, 0xa0, 0xb7, 0xd1, 0xc9, + 0x41, 0x0a, 0xbf, 0x8f, 0x2e, 0xf7, 0x09, 0xa9, 0x38, 0x4d, 0xcc, 0x16, + 0x9e, 0xfb, 0xb0, 0x07, 0x5a, 0x50, 0x8b, 0x75, 0x48, 0x1d, 0x61, 0x64, + 0xb0, 0x8d, 0xed, 0x43, 0x6d, 0xec, 0x99, 0xa0, 0xfe, 0x4c, 0x6b, 0xba, + 0x32, 0x80, 0xd4, 0xec, 0xb8, 0x2c, 0x5c, 0x9d, 0x45, 0xbd, 0x21, 0x21, + 0x6b, 0x2c, 0x46, 0x35, 0xa6, 0x9d, 0x7d, 0xa0, 0x01, 0x0a, 0x30, 0x24, + 0x0b, 0x51, 0x5c, 0x67, 0x23, 0x41, 0x26, 0x42, 0x6a, 0xc5, 0x9b, 0x36, + 0x70, 0xe1, 0xb5, 0xb0, 0x72, 0x1d, 0x08, 0x86, 0x66, 0xa1, 0xe2, 0x25, + 0xe8, 0x81, 0x41, 0xa1, 0x6a, 0x64, 0xa0, 0x1f, 0x41, 0xdc, 0x59, 0xef, + 0xa5, 0xfa, 0x15, 0x42, 0x0f, 0xa2, 0x00, 0xe0, 0x59, 0x08, 0xe0, 0xf8, + 0xcc, 0x09, 0x71, 0x9b, 0x20, 0x66, 0xab, 0xd7, 0x01, 0x99, 0xdc, 0xa4, + 0xe7, 0x57, 0x84, 0x6e, 0x0c, 0xd2, 0x98, 0xf7, 0x83, 0xf4, 0x76, 0x6d, + 0x23, 0x7f, 0x7c, 0x0a, 0xfa, 0x10, 0xc4, 0x31, 0xfb, 0x14, 0x37, 0x1f, + 0x4d, 0xf1, 0x51, 0x13, 0xac, 0x42, 0x97, 0x9d, 0x50, 0x8b, 0xd5, 0x2b, + 0x61, 0x30, 0x90, 0x41, 0x86, 0x97, 0xe8, 0xfd, 0x9e, 0x02, 0x80, 0xda, + 0x46, 0x6f, 0x57, 0x8b, 0x52, 0xe0, 0x33, 0xd3, 0xe7, 0x3f, 0x0b, 0x0f, + 0xa2, 0x64, 0x80, 0x8d, 0x3a, 0x84, 0x66, 0x85, 0x2c, 0xc2, 0x93, 0xcf, + 0x08, 0x17, 0xd9, 0xea, 0x75, 0x40, 0x51, 0x78, 0x32, 0xfa, 0x83, 0x61, + 0x80, 0xf0, 0xf7, 0x5c, 0x24, 0x8c, 0x06, 0x20, 0x65, 0x39, 0xd5, 0xd7, + 0xfb, 0x52, 0x03, 0x04, 0xa7, 0xfd, 0xd8, 0x84, 0xe0, 0x22, 0xee, 0x1d, + 0xd1, 0x19, 0x7f, 0xad, 0xce, 0x80, 0xf2, 0x2f, 0x91, 0x67, 0x79, 0xe3, + 0xe9, 0xf0, 0x60, 0x10, 0x84, 0x0d, 0x36, 0x65, 0xb1, 0x1f, 0x48, 0xd1, + 0x65, 0x20, 0x68, 0xa0, 0x18, 0x23, 0x03, 0x7f, 0x65, 0x47, 0x78, 0x1e, + 0xc2, 0x55, 0xdf, 0x0d, 0xfd, 0x82, 0x7c, 0xe9, 0x79, 0xde, 0x2d, 0x95, + 0xdb, 0xaf, 0x45, 0x18, 0xf8, 0xf4, 0x2b, 0xa7, 0x7a, 0x22, 0x22, 0xb6, + 0x83, 0x90, 0x0b, 0x93, 0xfb, 0x32, 0x39, 0xe4, 0x02, 0x41, 0x9c, 0x2f, + 0xc0, 0xf4, 0xa0, 0x14, 0x7b, 0x4f, 0xe7, 0xe0, 0xb2, 0x0b, 0xb0, 0x54, + 0x7f, 0xbe, 0x46, 0x00, 0xe9, 0x03, 0x00, 0x3e, 0x04, 0xdc, 0xf9, 0x22, + 0xc5, 0xca, 0xc8, 0x7e, 0xe7, 0x7c, 0xbd, 0xce, 0x09, 0x58, 0x2c, 0x6c, + 0xe4, 0x6a, 0x25, 0x00, 0x8e, 0x2f, 0x76, 0xc6, 0x72, 0xe3, 0x3f, 0xed, + 0xf2, 0x55, 0x96, 0xe4, 0x65, 0xc4, 0xaa, 0xc5, 0xb8, 0xcb, 0x82, 0x10, + 0x81, 0x32, 0x0b, 0x22, 0xc1, 0xcc, 0x30, 0x34, 0xdb, 0x26, 0x88, 0xec, + 0xd7, 0x62, 0x2f, 0x76, 0xb6, 0x3a, 0x00, 0x97, 0x67, 0xa5, 0x99, 0xd6, + 0x94, 0x33, 0xcb, 0x04, 0x70, 0x17, 0x7d, 0x62, 0x85, 0x27, 0x9e, 0x2e, + 0x80, 0x42, 0x84, 0xac, 0x07, 0xee, 0x3e, 0x01, 0xac, 0x70, 0x6c, 0xcb, + 0x33, 0x71, 0x82, 0x13, 0x50, 0x58, 0xe5, 0x3a, 0x60, 0x56, 0xd7, 0xa1, + 0x9c, 0x76, 0xb6, 0x73, 0x1f, 0xc2, 0xd4, 0x7d, 0x75, 0xeb, 0x5b, 0x07, + 0x00, 0x0f, 0x80, 0xd9, 0x60, 0xb9, 0xb8, 0x3f, 0xc0, 0x9a, 0xb5, 0x3b, + 0x44, 0x71, 0xa6, 0x67, 0xc7, 0x72, 0x97, 0xaf, 0xac, 0xdf, 0x5e, 0x45, + 0xee, 0xce, 0x5a, 0xb4, 0xf7, 0x2b, 0xab, 0x91, 0x6c, 0xe8, 0x8b, 0x00, + 0x28, 0x7a, 0x91, 0x2f, 0x77, 0x99, 0x4a, 0x7f, 0x23, 0xb1, 0x37, 0x7a, + 0x06, 0x00, 0xd9, 0xbd, 0x8d, 0x53, 0xbe, 0x3f, 0x98, 0x38, 0xdb, 0xda, + 0xaf, 0x84, 0x46, 0xac, 0x5d, 0xa9, 0x7a, 0x86, 0x40, 0x4b, 0x02, 0x30, + 0x4b, 0x3c, 0x2d, 0xb3, 0xfc, 0x5b, 0x73, 0xbc, 0xa7, 0x1f, 0x26, 0x96, + 0x9e, 0x6d, 0xa2, 0xe3, 0x47, 0x61, 0xe5, 0xa7, 0x42, 0x05, 0x3c, 0x03, + 0x5a, 0x94, 0x25, 0xcf, 0x33, 0xc0, 0xfb, 0x9c, 0x59, 0x9a, 0x53, 0xac, + 0xce, 0xfd, 0xfc, 0x28, 0xea, 0x7f, 0xf4, 0x10, 0x64, 0xf3, 0x9b, 0x9b, + 0xa7, 0xb8, 0xc9, 0xcc, 0x91, 0x81, 0xb1, 0x7a, 0x68, 0x4a, 0x00, 0xc2, + 0xef, 0x86, 0x06, 0x8a, 0xa1, 0xe2, 0xfa, 0x97, 0xa8, 0x86, 0xdf, 0x7b, + 0xca, 0x80, 0xcb, 0x16, 0x36, 0x37, 0x17, 0x73, 0x67, 0x4e, 0xf2, 0x14, + 0x34, 0x56, 0x8f, 0x92, 0xf2, 0x7c, 0x40, 0x6f, 0x7d, 0x0d, 0x10, 0xf9, + 0x5d, 0x8e, 0x75, 0x27, 0x18, 0x8c, 0x56, 0x6f, 0x01, 0x98, 0xf9, 0x87, + 0xdb, 0xce, 0x1c, 0x81, 0x69, 0xbf, 0x58, 0x8b, 0x9b, 0xdb, 0x81, 0x7a, + 0x91, 0xc9, 0xa1, 0x6c, 0x51, 0x03, 0x77, 0xce, 0xb8, 0x5c, 0x7f, 0xe5, + 0x8a, 0xcf, 0xc6, 0x1e, 0x02, 0x78, 0x38, 0x9e, 0xfe, 0xde, 0x1c, 0x83, + 0x5d, 0x38, 0x55, 0x0b, 0x87, 0x5f, 0x67, 0xfb, 0x1d, 0x3e, 0x68, 0x2b, + 0x61, 0xbe, 0x84, 0x6b, 0x7f, 0xeb, 0x50, 0x6b, 0x97, 0x63, 0x1e, 0xfd, + 0x8b, 0x5b, 0x3c, 0xee, 0xcf, 0xf3, 0x7f, 0x04, 0x18, 0x00, 0xe0, 0x6e, + 0xdd, 0x63, 0x24, 0x57, 0x80, 0x6f, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, + 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82, +} diff --git a/src/native/linux/icon/logo.png b/src/native/linux/icon/logo.png new file mode 100644 index 0000000000000000000000000000000000000000..9d332bd085471035d18adf52b49fa8ca1fa49982 GIT binary patch literal 1851 zcmV-B2gLY^P)Px#1ZP1_K>z@;j|==^1poj532;bRa{vGi!vFvd!vV){sAK>D2GB`FK~z{rl~+ql z6KfQnVtl`d#&{L*Hj2+8HWsKngwd)2W3;^CqB0Rhomu5)(;`UAWM& zpc1JTjg`0cscm8Q-I%R0|BquZT|L6bz^Plsb$4Eq!dHeS5 z!1nFiRgcG`Ub=KC%wv)T@i>LUa{TkRi8?xDI&>&VE?1C7N4;Y3#EBDuxpU{z?%li9 zQ>RX;EO++o*|3;QAySk8u^#*OsnpjOm`zbDqWXFtWoBXpnD>}BkI3&=q&<7~(18O7 zqKVDUpFbbUWFaC=!#IiCyIiVRoZy{12^+H__PCfOiM0g-9#SJ$t6`IGf&dcrx>C^7BJ<^CoZ3+M5)j>1n5s ztE}X`1yOHrn61Gsi11M zwCFdrVg}*k$B#7tdOTY0NbvRR>?UghiX$dr=3TppYHHZTCZDl;4k04ety>2%vjVV` z$SZ=AbBDvhQ80V*q6-%cUsb3kcyXZb@g(_*2*S_xFdI zo13kZSD5`34Em|9O(Y4^VrE&^6P6VX4h||oULU_D6&D+!2T)EROp#~sDG^%^$`~%@ z!*ejWb?a8@?C$1Rm#Mtm*nnA4O#Z8=9RDN!I)fPLrnNOS;m*#^5Kkre)~#EH0+%me z=9m=|Y(^2|$^OgBBl`I>ZO%_7gn{8sn~nY(8}sMq=ZpQBIa9J5GK4Tp_WJc}J@k;l zrs`v{Kc!Ef48UY%J+;lBKN2kgVApQeBKo+R_K$Q!KkY%Bo-$axmF1O0@1m1 z=Fn?s2pv0iOa~t;^6J$qBh$9Ewds=?85smD_$`vhuY+&%W^*#b;aKmzc=2L{4~Ml) zsW>AiC&!M1#YbUb;pG13&!RBO$a+^oi-Xsi}z`K745El=+=nTYYFQHB7nPvlk;w*g0f`AhVXKL`4HO z^ge)N*yVDGcPlO^C_u9#Qn|_0G&Q9p+-XUi=dpufs-7NUs?pI=Q<6G)@}%%R3XkvL z!Gp8*1sqY_wmo_B#N-}J(z{f=3g*23<%Xt2A)E6&a zC{$QzF44&h#o=e=9$Go%j7Vx%%`V}xf9={eANP-^PoEBR`%18no|^$a4GSs7M`#;= z&KhFO_|r-#kEe_upCAL5Ke@#7I1{R2q_VP-B>XO^w0gBlKYqvp3l2v_6qDJ)B~Cmq zfa~#?6+kG*MRYC6wy90c%@J-(yfkNywwQ6i?DPF{@nPb>TqJ_1 zSUXFmyL0D`$fO*?D_5?>9oLJZWaCL_<&;WFS|!v4sjTc@TTjm*kN+<5?er$Yf^OWn p;d}r7eMMGQmIOuq?&|9L{68D9fzs!Q)L8%k002ovPDHLkV1f#YjBfw{ literal 0 HcmV?d00001 diff --git a/src/native/linux/internal/DbusMenu.xml b/src/native/linux/internal/DbusMenu.xml new file mode 100644 index 00000000..db695984 --- /dev/null +++ b/src/native/linux/internal/DbusMenu.xml @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/native/linux/internal/StatusNotifierItem.xml b/src/native/linux/internal/StatusNotifierItem.xml new file mode 100644 index 00000000..1093d3d1 --- /dev/null +++ b/src/native/linux/internal/StatusNotifierItem.xml @@ -0,0 +1,85 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/native/linux/internal/generated/menu/dbus_menu.go b/src/native/linux/internal/generated/menu/dbus_menu.go new file mode 100644 index 00000000..1b896d3e --- /dev/null +++ b/src/native/linux/internal/generated/menu/dbus_menu.go @@ -0,0 +1,484 @@ +// Code generated by dbus-codegen-go DO NOT EDIT. +package menu + +import ( + "context" + "errors" + "fmt" + + "github.com/godbus/dbus/v5" + "github.com/godbus/dbus/v5/introspect" +) + +var ( + // Introspection for com.canonical.dbusmenu + IntrospectDataDbusmenu = introspect.Interface{ + Name: "com.canonical.dbusmenu", + Methods: []introspect.Method{{Name: "GetLayout", Args: []introspect.Arg{ + {Name: "parentId", Type: "i", Direction: "in"}, + {Name: "recursionDepth", Type: "i", Direction: "in"}, + {Name: "propertyNames", Type: "as", Direction: "in"}, + {Name: "revision", Type: "u", Direction: "out"}, + {Name: "layout", Type: "(ia{sv}av)", Direction: "out"}, + }}, + {Name: "GetGroupProperties", Args: []introspect.Arg{ + {Name: "ids", Type: "ai", Direction: "in"}, + {Name: "propertyNames", Type: "as", Direction: "in"}, + {Name: "properties", Type: "a(ia{sv})", Direction: "out"}, + }}, + {Name: "GetProperty", Args: []introspect.Arg{ + {Name: "id", Type: "i", Direction: "in"}, + {Name: "name", Type: "s", Direction: "in"}, + {Name: "value", Type: "v", Direction: "out"}, + }}, + {Name: "Event", Args: []introspect.Arg{ + {Name: "id", Type: "i", Direction: "in"}, + {Name: "eventId", Type: "s", Direction: "in"}, + {Name: "data", Type: "v", Direction: "in"}, + {Name: "timestamp", Type: "u", Direction: "in"}, + }}, + {Name: "EventGroup", Args: []introspect.Arg{ + {Name: "events", Type: "a(isvu)", Direction: "in"}, + {Name: "idErrors", Type: "ai", Direction: "out"}, + }}, + {Name: "AboutToShow", Args: []introspect.Arg{ + {Name: "id", Type: "i", Direction: "in"}, + {Name: "needUpdate", Type: "b", Direction: "out"}, + }}, + {Name: "AboutToShowGroup", Args: []introspect.Arg{ + {Name: "ids", Type: "ai", Direction: "in"}, + {Name: "updatesNeeded", Type: "ai", Direction: "out"}, + {Name: "idErrors", Type: "ai", Direction: "out"}, + }}, + }, + Signals: []introspect.Signal{{Name: "ItemsPropertiesUpdated", Args: []introspect.Arg{ + {Name: "updatedProps", Type: "a(ia{sv})", Direction: "out"}, + {Name: "removedProps", Type: "a(ias)", Direction: "out"}, + }}, + {Name: "LayoutUpdated", Args: []introspect.Arg{ + {Name: "revision", Type: "u", Direction: "out"}, + {Name: "parent", Type: "i", Direction: "out"}, + }}, + {Name: "ItemActivationRequested", Args: []introspect.Arg{ + {Name: "id", Type: "i", Direction: "out"}, + {Name: "timestamp", Type: "u", Direction: "out"}, + }}, + }, + Properties: []introspect.Property{{Name: "Version", Type: "u", Access: "read"}, + {Name: "TextDirection", Type: "s", Access: "read"}, + {Name: "Status", Type: "s", Access: "read"}, + {Name: "IconThemePath", Type: "as", Access: "read"}, + }, + Annotations: []introspect.Annotation{}, + } +) + +// Signal is a common interface for all signals. +type Signal interface { + Name() string + Interface() string + Sender() string + + path() dbus.ObjectPath + values() []interface{} +} + +// Emit sends the given signal to the bus. +func Emit(conn *dbus.Conn, s Signal) error { + return conn.Emit(s.path(), s.Interface()+"."+s.Name(), s.values()...) +} + +// ErrUnknownSignal is returned by LookupSignal when a signal cannot be resolved. +var ErrUnknownSignal = errors.New("unknown signal") + +// LookupSignal converts the given raw D-Bus signal with variable body +// into one with typed structured body or returns ErrUnknownSignal error. +func LookupSignal(signal *dbus.Signal) (Signal, error) { + switch signal.Name { + case InterfaceDbusmenu + "." + "ItemsPropertiesUpdated": + v0, ok := signal.Body[0].([]struct { + V0 int32 + V1 map[string]dbus.Variant + }) + if !ok { + return nil, fmt.Errorf("prop .UpdatedProps is %T, not []struct {V0 int32;V1 map[string]dbus.Variant}", signal.Body[0]) + } + v1, ok := signal.Body[1].([]struct { + V0 int32 + V1 []string + }) + if !ok { + return nil, fmt.Errorf("prop .RemovedProps is %T, not []struct {V0 int32;V1 []string}", signal.Body[1]) + } + return &Dbusmenu_ItemsPropertiesUpdatedSignal{ + sender: signal.Sender, + Path: signal.Path, + Body: &Dbusmenu_ItemsPropertiesUpdatedSignalBody{ + UpdatedProps: v0, + RemovedProps: v1, + }, + }, nil + case InterfaceDbusmenu + "." + "LayoutUpdated": + v0, ok := signal.Body[0].(uint32) + if !ok { + return nil, fmt.Errorf("prop .Revision is %T, not uint32", signal.Body[0]) + } + v1, ok := signal.Body[1].(int32) + if !ok { + return nil, fmt.Errorf("prop .Parent is %T, not int32", signal.Body[1]) + } + return &Dbusmenu_LayoutUpdatedSignal{ + sender: signal.Sender, + Path: signal.Path, + Body: &Dbusmenu_LayoutUpdatedSignalBody{ + Revision: v0, + Parent: v1, + }, + }, nil + case InterfaceDbusmenu + "." + "ItemActivationRequested": + v0, ok := signal.Body[0].(int32) + if !ok { + return nil, fmt.Errorf("prop .Id is %T, not int32", signal.Body[0]) + } + v1, ok := signal.Body[1].(uint32) + if !ok { + return nil, fmt.Errorf("prop .Timestamp is %T, not uint32", signal.Body[1]) + } + return &Dbusmenu_ItemActivationRequestedSignal{ + sender: signal.Sender, + Path: signal.Path, + Body: &Dbusmenu_ItemActivationRequestedSignalBody{ + Id: v0, + Timestamp: v1, + }, + }, nil + default: + return nil, ErrUnknownSignal + } +} + +// AddMatchSignal registers a match rule for the given signal, +// opts are appended to the automatically generated signal's rules. +func AddMatchSignal(conn *dbus.Conn, s Signal, opts ...dbus.MatchOption) error { + return conn.AddMatchSignal(append([]dbus.MatchOption{ + dbus.WithMatchInterface(s.Interface()), + dbus.WithMatchMember(s.Name()), + }, opts...)...) +} + +// RemoveMatchSignal unregisters the previously registered subscription. +func RemoveMatchSignal(conn *dbus.Conn, s Signal, opts ...dbus.MatchOption) error { + return conn.RemoveMatchSignal(append([]dbus.MatchOption{ + dbus.WithMatchInterface(s.Interface()), + dbus.WithMatchMember(s.Name()), + }, opts...)...) +} + +// Interface name constants. +const ( + InterfaceDbusmenu = "com.canonical.dbusmenu" +) + +// Dbusmenuer is com.canonical.dbusmenu interface. +type Dbusmenuer interface { + // GetLayout is com.canonical.dbusmenu.GetLayout method. + GetLayout(parentId int32, recursionDepth int32, propertyNames []string) (revision uint32, layout struct { + V0 int32 + V1 map[string]dbus.Variant + V2 []dbus.Variant + }, err *dbus.Error) + // GetGroupProperties is com.canonical.dbusmenu.GetGroupProperties method. + GetGroupProperties(ids []int32, propertyNames []string) (properties []struct { + V0 int32 + V1 map[string]dbus.Variant + }, err *dbus.Error) + // GetProperty is com.canonical.dbusmenu.GetProperty method. + GetProperty(id int32, name string) (value dbus.Variant, err *dbus.Error) + // Event is com.canonical.dbusmenu.Event method. + Event(id int32, eventId string, data dbus.Variant, timestamp uint32) (err *dbus.Error) + // EventGroup is com.canonical.dbusmenu.EventGroup method. + EventGroup(events []struct { + V0 int32 + V1 string + V2 dbus.Variant + V3 uint32 + }) (idErrors []int32, err *dbus.Error) + // AboutToShow is com.canonical.dbusmenu.AboutToShow method. + AboutToShow(id int32) (needUpdate bool, err *dbus.Error) + // AboutToShowGroup is com.canonical.dbusmenu.AboutToShowGroup method. + AboutToShowGroup(ids []int32) (updatesNeeded []int32, idErrors []int32, err *dbus.Error) +} + +// ExportDbusmenu exports the given object that implements com.canonical.dbusmenu on the bus. +func ExportDbusmenu(conn *dbus.Conn, path dbus.ObjectPath, v Dbusmenuer) error { + return conn.ExportSubtreeMethodTable(map[string]interface{}{ + "GetLayout": v.GetLayout, + "GetGroupProperties": v.GetGroupProperties, + "GetProperty": v.GetProperty, + "Event": v.Event, + "EventGroup": v.EventGroup, + "AboutToShow": v.AboutToShow, + "AboutToShowGroup": v.AboutToShowGroup, + }, path, InterfaceDbusmenu) +} + +// UnexportDbusmenu unexports com.canonical.dbusmenu interface on the named path. +func UnexportDbusmenu(conn *dbus.Conn, path dbus.ObjectPath) error { + return conn.Export(nil, path, InterfaceDbusmenu) +} + +// UnimplementedDbusmenu can be embedded to have forward compatible server implementations. +type UnimplementedDbusmenu struct{} + +func (*UnimplementedDbusmenu) iface() string { + return InterfaceDbusmenu +} + +func (*UnimplementedDbusmenu) GetLayout(parentId int32, recursionDepth int32, propertyNames []string) (revision uint32, layout struct { + V0 int32 + V1 map[string]dbus.Variant + V2 []dbus.Variant +}, err *dbus.Error) { + err = &dbus.ErrMsgUnknownMethod + return +} + +func (*UnimplementedDbusmenu) GetGroupProperties(ids []int32, propertyNames []string) (properties []struct { + V0 int32 + V1 map[string]dbus.Variant +}, err *dbus.Error) { + err = &dbus.ErrMsgUnknownMethod + return +} + +func (*UnimplementedDbusmenu) GetProperty(id int32, name string) (value dbus.Variant, err *dbus.Error) { + err = &dbus.ErrMsgUnknownMethod + return +} + +func (*UnimplementedDbusmenu) Event(id int32, eventId string, data dbus.Variant, timestamp uint32) (err *dbus.Error) { + err = &dbus.ErrMsgUnknownMethod + return +} + +func (*UnimplementedDbusmenu) EventGroup(events []struct { + V0 int32 + V1 string + V2 dbus.Variant + V3 uint32 +}) (idErrors []int32, err *dbus.Error) { + err = &dbus.ErrMsgUnknownMethod + return +} + +func (*UnimplementedDbusmenu) AboutToShow(id int32) (needUpdate bool, err *dbus.Error) { + err = &dbus.ErrMsgUnknownMethod + return +} + +func (*UnimplementedDbusmenu) AboutToShowGroup(ids []int32) (updatesNeeded []int32, idErrors []int32, err *dbus.Error) { + err = &dbus.ErrMsgUnknownMethod + return +} + +// NewDbusmenu creates and allocates com.canonical.dbusmenu. +func NewDbusmenu(object dbus.BusObject) *Dbusmenu { + return &Dbusmenu{object} +} + +// Dbusmenu implements com.canonical.dbusmenu D-Bus interface. +type Dbusmenu struct { + object dbus.BusObject +} + +// GetLayout calls com.canonical.dbusmenu.GetLayout method. +func (o *Dbusmenu) GetLayout(ctx context.Context, parentId int32, recursionDepth int32, propertyNames []string) (revision uint32, layout struct { + V0 int32 + V1 map[string]dbus.Variant + V2 []dbus.Variant +}, err error) { + err = o.object.CallWithContext(ctx, InterfaceDbusmenu+".GetLayout", 0, parentId, recursionDepth, propertyNames).Store(&revision, &layout) + return +} + +// GetGroupProperties calls com.canonical.dbusmenu.GetGroupProperties method. +func (o *Dbusmenu) GetGroupProperties(ctx context.Context, ids []int32, propertyNames []string) (properties []struct { + V0 int32 + V1 map[string]dbus.Variant +}, err error) { + err = o.object.CallWithContext(ctx, InterfaceDbusmenu+".GetGroupProperties", 0, ids, propertyNames).Store(&properties) + return +} + +// GetProperty calls com.canonical.dbusmenu.GetProperty method. +func (o *Dbusmenu) GetProperty(ctx context.Context, id int32, name string) (value dbus.Variant, err error) { + err = o.object.CallWithContext(ctx, InterfaceDbusmenu+".GetProperty", 0, id, name).Store(&value) + return +} + +// Event calls com.canonical.dbusmenu.Event method. +func (o *Dbusmenu) Event(ctx context.Context, id int32, eventId string, data dbus.Variant, timestamp uint32) (err error) { + err = o.object.CallWithContext(ctx, InterfaceDbusmenu+".Event", 0, id, eventId, data, timestamp).Store() + return +} + +// EventGroup calls com.canonical.dbusmenu.EventGroup method. +func (o *Dbusmenu) EventGroup(ctx context.Context, events []struct { + V0 int32 + V1 string + V2 dbus.Variant + V3 uint32 +}) (idErrors []int32, err error) { + err = o.object.CallWithContext(ctx, InterfaceDbusmenu+".EventGroup", 0, events).Store(&idErrors) + return +} + +// AboutToShow calls com.canonical.dbusmenu.AboutToShow method. +func (o *Dbusmenu) AboutToShow(ctx context.Context, id int32) (needUpdate bool, err error) { + err = o.object.CallWithContext(ctx, InterfaceDbusmenu+".AboutToShow", 0, id).Store(&needUpdate) + return +} + +// AboutToShowGroup calls com.canonical.dbusmenu.AboutToShowGroup method. +func (o *Dbusmenu) AboutToShowGroup(ctx context.Context, ids []int32) (updatesNeeded []int32, idErrors []int32, err error) { + err = o.object.CallWithContext(ctx, InterfaceDbusmenu+".AboutToShowGroup", 0, ids).Store(&updatesNeeded, &idErrors) + return +} + +// GetVersion gets com.canonical.dbusmenu.Version property. +func (o *Dbusmenu) GetVersion(ctx context.Context) (version uint32, err error) { + err = o.object.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, InterfaceDbusmenu, "Version").Store(&version) + return +} + +// GetTextDirection gets com.canonical.dbusmenu.TextDirection property. +func (o *Dbusmenu) GetTextDirection(ctx context.Context) (textDirection string, err error) { + err = o.object.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, InterfaceDbusmenu, "TextDirection").Store(&textDirection) + return +} + +// GetStatus gets com.canonical.dbusmenu.Status property. +func (o *Dbusmenu) GetStatus(ctx context.Context) (status string, err error) { + err = o.object.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, InterfaceDbusmenu, "Status").Store(&status) + return +} + +// GetIconThemePath gets com.canonical.dbusmenu.IconThemePath property. +func (o *Dbusmenu) GetIconThemePath(ctx context.Context) (iconThemePath []string, err error) { + err = o.object.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, InterfaceDbusmenu, "IconThemePath").Store(&iconThemePath) + return +} + +// Dbusmenu_ItemsPropertiesUpdatedSignal represents com.canonical.dbusmenu.ItemsPropertiesUpdated signal. +type Dbusmenu_ItemsPropertiesUpdatedSignal struct { + sender string + Path dbus.ObjectPath + Body *Dbusmenu_ItemsPropertiesUpdatedSignalBody +} + +// Name returns the signal's name. +func (s *Dbusmenu_ItemsPropertiesUpdatedSignal) Name() string { + return "ItemsPropertiesUpdated" +} + +// Interface returns the signal's interface. +func (s *Dbusmenu_ItemsPropertiesUpdatedSignal) Interface() string { + return InterfaceDbusmenu +} + +// Sender returns the signal's sender unique name. +func (s *Dbusmenu_ItemsPropertiesUpdatedSignal) Sender() string { + return s.sender +} + +func (s *Dbusmenu_ItemsPropertiesUpdatedSignal) path() dbus.ObjectPath { + return s.Path +} + +func (s *Dbusmenu_ItemsPropertiesUpdatedSignal) values() []interface{} { + return []interface{}{s.Body.UpdatedProps, s.Body.RemovedProps} +} + +// Dbusmenu_ItemsPropertiesUpdatedSignalBody is body container. +type Dbusmenu_ItemsPropertiesUpdatedSignalBody struct { + UpdatedProps []struct { + V0 int32 + V1 map[string]dbus.Variant + } + RemovedProps []struct { + V0 int32 + V1 []string + } +} + +// Dbusmenu_LayoutUpdatedSignal represents com.canonical.dbusmenu.LayoutUpdated signal. +type Dbusmenu_LayoutUpdatedSignal struct { + sender string + Path dbus.ObjectPath + Body *Dbusmenu_LayoutUpdatedSignalBody +} + +// Name returns the signal's name. +func (s *Dbusmenu_LayoutUpdatedSignal) Name() string { + return "LayoutUpdated" +} + +// Interface returns the signal's interface. +func (s *Dbusmenu_LayoutUpdatedSignal) Interface() string { + return InterfaceDbusmenu +} + +// Sender returns the signal's sender unique name. +func (s *Dbusmenu_LayoutUpdatedSignal) Sender() string { + return s.sender +} + +func (s *Dbusmenu_LayoutUpdatedSignal) path() dbus.ObjectPath { + return s.Path +} + +func (s *Dbusmenu_LayoutUpdatedSignal) values() []interface{} { + return []interface{}{s.Body.Revision, s.Body.Parent} +} + +// Dbusmenu_LayoutUpdatedSignalBody is body container. +type Dbusmenu_LayoutUpdatedSignalBody struct { + Revision uint32 + Parent int32 +} + +// Dbusmenu_ItemActivationRequestedSignal represents com.canonical.dbusmenu.ItemActivationRequested signal. +type Dbusmenu_ItemActivationRequestedSignal struct { + sender string + Path dbus.ObjectPath + Body *Dbusmenu_ItemActivationRequestedSignalBody +} + +// Name returns the signal's name. +func (s *Dbusmenu_ItemActivationRequestedSignal) Name() string { + return "ItemActivationRequested" +} + +// Interface returns the signal's interface. +func (s *Dbusmenu_ItemActivationRequestedSignal) Interface() string { + return InterfaceDbusmenu +} + +// Sender returns the signal's sender unique name. +func (s *Dbusmenu_ItemActivationRequestedSignal) Sender() string { + return s.sender +} + +func (s *Dbusmenu_ItemActivationRequestedSignal) path() dbus.ObjectPath { + return s.Path +} + +func (s *Dbusmenu_ItemActivationRequestedSignal) values() []interface{} { + return []interface{}{s.Body.Id, s.Body.Timestamp} +} + +// Dbusmenu_ItemActivationRequestedSignalBody is body container. +type Dbusmenu_ItemActivationRequestedSignalBody struct { + Id int32 + Timestamp uint32 +} diff --git a/src/native/linux/internal/generated/notifier/status_notifier_item.go b/src/native/linux/internal/generated/notifier/status_notifier_item.go new file mode 100644 index 00000000..230c40d6 --- /dev/null +++ b/src/native/linux/internal/generated/notifier/status_notifier_item.go @@ -0,0 +1,633 @@ +// Code generated by dbus-codegen-go DO NOT EDIT. +package notifier + +import ( + "context" + "errors" + "fmt" + + "github.com/godbus/dbus/v5" + "github.com/godbus/dbus/v5/introspect" +) + +var ( + // Introspection for org.kde.StatusNotifierItem + IntrospectDataStatusNotifierItem = introspect.Interface{ + Name: "org.kde.StatusNotifierItem", + Methods: []introspect.Method{{Name: "ContextMenu", Args: []introspect.Arg{ + {Name: "x", Type: "i", Direction: "in"}, + {Name: "y", Type: "i", Direction: "in"}, + }}, + {Name: "Activate", Args: []introspect.Arg{ + {Name: "x", Type: "i", Direction: "in"}, + {Name: "y", Type: "i", Direction: "in"}, + }}, + {Name: "SecondaryActivate", Args: []introspect.Arg{ + {Name: "x", Type: "i", Direction: "in"}, + {Name: "y", Type: "i", Direction: "in"}, + }}, + {Name: "Scroll", Args: []introspect.Arg{ + {Name: "delta", Type: "i", Direction: "in"}, + {Name: "orientation", Type: "s", Direction: "in"}, + }}, + }, + Signals: []introspect.Signal{{Name: "NewTitle"}, + {Name: "NewIcon"}, + {Name: "NewAttentionIcon"}, + {Name: "NewOverlayIcon"}, + {Name: "NewStatus", Args: []introspect.Arg{ + {Name: "status", Type: "s", Direction: ""}, + }}, + {Name: "NewIconThemePath", Args: []introspect.Arg{ + {Name: "icon_theme_path", Type: "s", Direction: "out"}, + }}, + {Name: "NewMenu"}, + }, + Properties: []introspect.Property{{Name: "Category", Type: "s", Access: "read"}, + {Name: "Id", Type: "s", Access: "read"}, + {Name: "Title", Type: "s", Access: "read"}, + {Name: "Status", Type: "s", Access: "read"}, + {Name: "WindowId", Type: "i", Access: "read"}, + {Name: "IconThemePath", Type: "s", Access: "read"}, + {Name: "Menu", Type: "o", Access: "read"}, + {Name: "ItemIsMenu", Type: "b", Access: "read"}, + {Name: "IconName", Type: "s", Access: "read"}, + {Name: "IconPixmap", Type: "a(iiay)", Access: "read", Annotations: []introspect.Annotation{ + {Name: "org.qtproject.QtDBus.QtTypeName", Value: "KDbusImageVector"}, + }}, + {Name: "OverlayIconName", Type: "s", Access: "read"}, + {Name: "OverlayIconPixmap", Type: "a(iiay)", Access: "read", Annotations: []introspect.Annotation{ + {Name: "org.qtproject.QtDBus.QtTypeName", Value: "KDbusImageVector"}, + }}, + {Name: "AttentionIconName", Type: "s", Access: "read"}, + {Name: "AttentionIconPixmap", Type: "a(iiay)", Access: "read", Annotations: []introspect.Annotation{ + {Name: "org.qtproject.QtDBus.QtTypeName", Value: "KDbusImageVector"}, + }}, + {Name: "AttentionMovieName", Type: "s", Access: "read"}, + {Name: "ToolTip", Type: "(sa(iiay)ss)", Access: "read", Annotations: []introspect.Annotation{ + {Name: "org.qtproject.QtDBus.QtTypeName", Value: "KDbusToolTipStruct"}, + }}, + }, + Annotations: []introspect.Annotation{}, + } +) + +// Signal is a common interface for all signals. +type Signal interface { + Name() string + Interface() string + Sender() string + + path() dbus.ObjectPath + values() []interface{} +} + +// Emit sends the given signal to the bus. +func Emit(conn *dbus.Conn, s Signal) error { + return conn.Emit(s.path(), s.Interface()+"."+s.Name(), s.values()...) +} + +// ErrUnknownSignal is returned by LookupSignal when a signal cannot be resolved. +var ErrUnknownSignal = errors.New("unknown signal") + +// LookupSignal converts the given raw D-Bus signal with variable body +// into one with typed structured body or returns ErrUnknownSignal error. +func LookupSignal(signal *dbus.Signal) (Signal, error) { + switch signal.Name { + case InterfaceStatusNotifierItem + "." + "NewTitle": + return &StatusNotifierItem_NewTitleSignal{ + sender: signal.Sender, + Path: signal.Path, + Body: &StatusNotifierItem_NewTitleSignalBody{}, + }, nil + case InterfaceStatusNotifierItem + "." + "NewIcon": + return &StatusNotifierItem_NewIconSignal{ + sender: signal.Sender, + Path: signal.Path, + Body: &StatusNotifierItem_NewIconSignalBody{}, + }, nil + case InterfaceStatusNotifierItem + "." + "NewAttentionIcon": + return &StatusNotifierItem_NewAttentionIconSignal{ + sender: signal.Sender, + Path: signal.Path, + Body: &StatusNotifierItem_NewAttentionIconSignalBody{}, + }, nil + case InterfaceStatusNotifierItem + "." + "NewOverlayIcon": + return &StatusNotifierItem_NewOverlayIconSignal{ + sender: signal.Sender, + Path: signal.Path, + Body: &StatusNotifierItem_NewOverlayIconSignalBody{}, + }, nil + case InterfaceStatusNotifierItem + "." + "NewStatus": + v0, ok := signal.Body[0].(string) + if !ok { + return nil, fmt.Errorf("prop .Status is %T, not string", signal.Body[0]) + } + return &StatusNotifierItem_NewStatusSignal{ + sender: signal.Sender, + Path: signal.Path, + Body: &StatusNotifierItem_NewStatusSignalBody{ + Status: v0, + }, + }, nil + case InterfaceStatusNotifierItem + "." + "NewIconThemePath": + v0, ok := signal.Body[0].(string) + if !ok { + return nil, fmt.Errorf("prop .IconThemePath is %T, not string", signal.Body[0]) + } + return &StatusNotifierItem_NewIconThemePathSignal{ + sender: signal.Sender, + Path: signal.Path, + Body: &StatusNotifierItem_NewIconThemePathSignalBody{ + IconThemePath: v0, + }, + }, nil + case InterfaceStatusNotifierItem + "." + "NewMenu": + return &StatusNotifierItem_NewMenuSignal{ + sender: signal.Sender, + Path: signal.Path, + Body: &StatusNotifierItem_NewMenuSignalBody{}, + }, nil + default: + return nil, ErrUnknownSignal + } +} + +// AddMatchSignal registers a match rule for the given signal, +// opts are appended to the automatically generated signal's rules. +func AddMatchSignal(conn *dbus.Conn, s Signal, opts ...dbus.MatchOption) error { + return conn.AddMatchSignal(append([]dbus.MatchOption{ + dbus.WithMatchInterface(s.Interface()), + dbus.WithMatchMember(s.Name()), + }, opts...)...) +} + +// RemoveMatchSignal unregisters the previously registered subscription. +func RemoveMatchSignal(conn *dbus.Conn, s Signal, opts ...dbus.MatchOption) error { + return conn.RemoveMatchSignal(append([]dbus.MatchOption{ + dbus.WithMatchInterface(s.Interface()), + dbus.WithMatchMember(s.Name()), + }, opts...)...) +} + +// Interface name constants. +const ( + InterfaceStatusNotifierItem = "org.kde.StatusNotifierItem" +) + +// StatusNotifierItemer is org.kde.StatusNotifierItem interface. +type StatusNotifierItemer interface { + // ContextMenu is org.kde.StatusNotifierItem.ContextMenu method. + ContextMenu(x int32, y int32) (err *dbus.Error) + // Activate is org.kde.StatusNotifierItem.Activate method. + Activate(x int32, y int32) (err *dbus.Error) + // SecondaryActivate is org.kde.StatusNotifierItem.SecondaryActivate method. + SecondaryActivate(x int32, y int32) (err *dbus.Error) + // Scroll is org.kde.StatusNotifierItem.Scroll method. + Scroll(delta int32, orientation string) (err *dbus.Error) +} + +// ExportStatusNotifierItem exports the given object that implements org.kde.StatusNotifierItem on the bus. +func ExportStatusNotifierItem(conn *dbus.Conn, path dbus.ObjectPath, v StatusNotifierItemer) error { + return conn.ExportSubtreeMethodTable(map[string]interface{}{ + "ContextMenu": v.ContextMenu, + "Activate": v.Activate, + "SecondaryActivate": v.SecondaryActivate, + "Scroll": v.Scroll, + }, path, InterfaceStatusNotifierItem) +} + +// UnexportStatusNotifierItem unexports org.kde.StatusNotifierItem interface on the named path. +func UnexportStatusNotifierItem(conn *dbus.Conn, path dbus.ObjectPath) error { + return conn.Export(nil, path, InterfaceStatusNotifierItem) +} + +// UnimplementedStatusNotifierItem can be embedded to have forward compatible server implementations. +type UnimplementedStatusNotifierItem struct{} + +func (*UnimplementedStatusNotifierItem) iface() string { + return InterfaceStatusNotifierItem +} + +func (*UnimplementedStatusNotifierItem) ContextMenu(x int32, y int32) (err *dbus.Error) { + err = &dbus.ErrMsgUnknownMethod + return +} + +func (*UnimplementedStatusNotifierItem) Activate(x int32, y int32) (err *dbus.Error) { + err = &dbus.ErrMsgUnknownMethod + return +} + +func (*UnimplementedStatusNotifierItem) SecondaryActivate(x int32, y int32) (err *dbus.Error) { + err = &dbus.ErrMsgUnknownMethod + return +} + +func (*UnimplementedStatusNotifierItem) Scroll(delta int32, orientation string) (err *dbus.Error) { + err = &dbus.ErrMsgUnknownMethod + return +} + +// NewStatusNotifierItem creates and allocates org.kde.StatusNotifierItem. +func NewStatusNotifierItem(object dbus.BusObject) *StatusNotifierItem { + return &StatusNotifierItem{object} +} + +// StatusNotifierItem implements org.kde.StatusNotifierItem D-Bus interface. +type StatusNotifierItem struct { + object dbus.BusObject +} + +// ContextMenu calls org.kde.StatusNotifierItem.ContextMenu method. +func (o *StatusNotifierItem) ContextMenu(ctx context.Context, x int32, y int32) (err error) { + err = o.object.CallWithContext(ctx, InterfaceStatusNotifierItem+".ContextMenu", 0, x, y).Store() + return +} + +// Activate calls org.kde.StatusNotifierItem.Activate method. +func (o *StatusNotifierItem) Activate(ctx context.Context, x int32, y int32) (err error) { + err = o.object.CallWithContext(ctx, InterfaceStatusNotifierItem+".Activate", 0, x, y).Store() + return +} + +// SecondaryActivate calls org.kde.StatusNotifierItem.SecondaryActivate method. +func (o *StatusNotifierItem) SecondaryActivate(ctx context.Context, x int32, y int32) (err error) { + err = o.object.CallWithContext(ctx, InterfaceStatusNotifierItem+".SecondaryActivate", 0, x, y).Store() + return +} + +// Scroll calls org.kde.StatusNotifierItem.Scroll method. +func (o *StatusNotifierItem) Scroll(ctx context.Context, delta int32, orientation string) (err error) { + err = o.object.CallWithContext(ctx, InterfaceStatusNotifierItem+".Scroll", 0, delta, orientation).Store() + return +} + +// GetCategory gets org.kde.StatusNotifierItem.Category property. +func (o *StatusNotifierItem) GetCategory(ctx context.Context) (category string, err error) { + err = o.object.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, InterfaceStatusNotifierItem, "Category").Store(&category) + return +} + +// GetId gets org.kde.StatusNotifierItem.Id property. +func (o *StatusNotifierItem) GetId(ctx context.Context) (id string, err error) { + err = o.object.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, InterfaceStatusNotifierItem, "Id").Store(&id) + return +} + +// GetTitle gets org.kde.StatusNotifierItem.Title property. +func (o *StatusNotifierItem) GetTitle(ctx context.Context) (title string, err error) { + err = o.object.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, InterfaceStatusNotifierItem, "Title").Store(&title) + return +} + +// GetStatus gets org.kde.StatusNotifierItem.Status property. +func (o *StatusNotifierItem) GetStatus(ctx context.Context) (status string, err error) { + err = o.object.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, InterfaceStatusNotifierItem, "Status").Store(&status) + return +} + +// GetWindowId gets org.kde.StatusNotifierItem.WindowId property. +func (o *StatusNotifierItem) GetWindowId(ctx context.Context) (windowId int32, err error) { + err = o.object.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, InterfaceStatusNotifierItem, "WindowId").Store(&windowId) + return +} + +// GetIconThemePath gets org.kde.StatusNotifierItem.IconThemePath property. +func (o *StatusNotifierItem) GetIconThemePath(ctx context.Context) (iconThemePath string, err error) { + err = o.object.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, InterfaceStatusNotifierItem, "IconThemePath").Store(&iconThemePath) + return +} + +// GetMenu gets org.kde.StatusNotifierItem.Menu property. +func (o *StatusNotifierItem) GetMenu(ctx context.Context) (menu dbus.ObjectPath, err error) { + err = o.object.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, InterfaceStatusNotifierItem, "Menu").Store(&menu) + return +} + +// GetItemIsMenu gets org.kde.StatusNotifierItem.ItemIsMenu property. +func (o *StatusNotifierItem) GetItemIsMenu(ctx context.Context) (itemIsMenu bool, err error) { + err = o.object.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, InterfaceStatusNotifierItem, "ItemIsMenu").Store(&itemIsMenu) + return +} + +// GetIconName gets org.kde.StatusNotifierItem.IconName property. +func (o *StatusNotifierItem) GetIconName(ctx context.Context) (iconName string, err error) { + err = o.object.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, InterfaceStatusNotifierItem, "IconName").Store(&iconName) + return +} + +// GetIconPixmap gets org.kde.StatusNotifierItem.IconPixmap property. +// +// Annotations: +// @org.qtproject.QtDBus.QtTypeName = KDbusImageVector +func (o *StatusNotifierItem) GetIconPixmap(ctx context.Context) (iconPixmap []struct { + V0 int32 + V1 int32 + V2 []byte +}, err error) { + err = o.object.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, InterfaceStatusNotifierItem, "IconPixmap").Store(&iconPixmap) + return +} + +// GetOverlayIconName gets org.kde.StatusNotifierItem.OverlayIconName property. +func (o *StatusNotifierItem) GetOverlayIconName(ctx context.Context) (overlayIconName string, err error) { + err = o.object.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, InterfaceStatusNotifierItem, "OverlayIconName").Store(&overlayIconName) + return +} + +// GetOverlayIconPixmap gets org.kde.StatusNotifierItem.OverlayIconPixmap property. +// +// Annotations: +// @org.qtproject.QtDBus.QtTypeName = KDbusImageVector +func (o *StatusNotifierItem) GetOverlayIconPixmap(ctx context.Context) (overlayIconPixmap []struct { + V0 int32 + V1 int32 + V2 []byte +}, err error) { + err = o.object.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, InterfaceStatusNotifierItem, "OverlayIconPixmap").Store(&overlayIconPixmap) + return +} + +// GetAttentionIconName gets org.kde.StatusNotifierItem.AttentionIconName property. +func (o *StatusNotifierItem) GetAttentionIconName(ctx context.Context) (attentionIconName string, err error) { + err = o.object.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, InterfaceStatusNotifierItem, "AttentionIconName").Store(&attentionIconName) + return +} + +// GetAttentionIconPixmap gets org.kde.StatusNotifierItem.AttentionIconPixmap property. +// +// Annotations: +// @org.qtproject.QtDBus.QtTypeName = KDbusImageVector +func (o *StatusNotifierItem) GetAttentionIconPixmap(ctx context.Context) (attentionIconPixmap []struct { + V0 int32 + V1 int32 + V2 []byte +}, err error) { + err = o.object.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, InterfaceStatusNotifierItem, "AttentionIconPixmap").Store(&attentionIconPixmap) + return +} + +// GetAttentionMovieName gets org.kde.StatusNotifierItem.AttentionMovieName property. +func (o *StatusNotifierItem) GetAttentionMovieName(ctx context.Context) (attentionMovieName string, err error) { + err = o.object.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, InterfaceStatusNotifierItem, "AttentionMovieName").Store(&attentionMovieName) + return +} + +// GetToolTip gets org.kde.StatusNotifierItem.ToolTip property. +// +// Annotations: +// @org.qtproject.QtDBus.QtTypeName = KDbusToolTipStruct +func (o *StatusNotifierItem) GetToolTip(ctx context.Context) (toolTip struct { + V0 string + V1 []struct { + V0 int32 + V1 int32 + V2 []byte + } + V2 string + V3 string +}, err error) { + err = o.object.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, InterfaceStatusNotifierItem, "ToolTip").Store(&toolTip) + return +} + +// StatusNotifierItem_NewTitleSignal represents org.kde.StatusNotifierItem.NewTitle signal. +type StatusNotifierItem_NewTitleSignal struct { + sender string + Path dbus.ObjectPath + Body *StatusNotifierItem_NewTitleSignalBody +} + +// Name returns the signal's name. +func (s *StatusNotifierItem_NewTitleSignal) Name() string { + return "NewTitle" +} + +// Interface returns the signal's interface. +func (s *StatusNotifierItem_NewTitleSignal) Interface() string { + return InterfaceStatusNotifierItem +} + +// Sender returns the signal's sender unique name. +func (s *StatusNotifierItem_NewTitleSignal) Sender() string { + return s.sender +} + +func (s *StatusNotifierItem_NewTitleSignal) path() dbus.ObjectPath { + return s.Path +} + +func (s *StatusNotifierItem_NewTitleSignal) values() []interface{} { + return []interface{}{} +} + +// StatusNotifierItem_NewTitleSignalBody is body container. +type StatusNotifierItem_NewTitleSignalBody struct { +} + +// StatusNotifierItem_NewIconSignal represents org.kde.StatusNotifierItem.NewIcon signal. +type StatusNotifierItem_NewIconSignal struct { + sender string + Path dbus.ObjectPath + Body *StatusNotifierItem_NewIconSignalBody +} + +// Name returns the signal's name. +func (s *StatusNotifierItem_NewIconSignal) Name() string { + return "NewIcon" +} + +// Interface returns the signal's interface. +func (s *StatusNotifierItem_NewIconSignal) Interface() string { + return InterfaceStatusNotifierItem +} + +// Sender returns the signal's sender unique name. +func (s *StatusNotifierItem_NewIconSignal) Sender() string { + return s.sender +} + +func (s *StatusNotifierItem_NewIconSignal) path() dbus.ObjectPath { + return s.Path +} + +func (s *StatusNotifierItem_NewIconSignal) values() []interface{} { + return []interface{}{} +} + +// StatusNotifierItem_NewIconSignalBody is body container. +type StatusNotifierItem_NewIconSignalBody struct { +} + +// StatusNotifierItem_NewAttentionIconSignal represents org.kde.StatusNotifierItem.NewAttentionIcon signal. +type StatusNotifierItem_NewAttentionIconSignal struct { + sender string + Path dbus.ObjectPath + Body *StatusNotifierItem_NewAttentionIconSignalBody +} + +// Name returns the signal's name. +func (s *StatusNotifierItem_NewAttentionIconSignal) Name() string { + return "NewAttentionIcon" +} + +// Interface returns the signal's interface. +func (s *StatusNotifierItem_NewAttentionIconSignal) Interface() string { + return InterfaceStatusNotifierItem +} + +// Sender returns the signal's sender unique name. +func (s *StatusNotifierItem_NewAttentionIconSignal) Sender() string { + return s.sender +} + +func (s *StatusNotifierItem_NewAttentionIconSignal) path() dbus.ObjectPath { + return s.Path +} + +func (s *StatusNotifierItem_NewAttentionIconSignal) values() []interface{} { + return []interface{}{} +} + +// StatusNotifierItem_NewAttentionIconSignalBody is body container. +type StatusNotifierItem_NewAttentionIconSignalBody struct { +} + +// StatusNotifierItem_NewOverlayIconSignal represents org.kde.StatusNotifierItem.NewOverlayIcon signal. +type StatusNotifierItem_NewOverlayIconSignal struct { + sender string + Path dbus.ObjectPath + Body *StatusNotifierItem_NewOverlayIconSignalBody +} + +// Name returns the signal's name. +func (s *StatusNotifierItem_NewOverlayIconSignal) Name() string { + return "NewOverlayIcon" +} + +// Interface returns the signal's interface. +func (s *StatusNotifierItem_NewOverlayIconSignal) Interface() string { + return InterfaceStatusNotifierItem +} + +// Sender returns the signal's sender unique name. +func (s *StatusNotifierItem_NewOverlayIconSignal) Sender() string { + return s.sender +} + +func (s *StatusNotifierItem_NewOverlayIconSignal) path() dbus.ObjectPath { + return s.Path +} + +func (s *StatusNotifierItem_NewOverlayIconSignal) values() []interface{} { + return []interface{}{} +} + +// StatusNotifierItem_NewOverlayIconSignalBody is body container. +type StatusNotifierItem_NewOverlayIconSignalBody struct { +} + +// StatusNotifierItem_NewStatusSignal represents org.kde.StatusNotifierItem.NewStatus signal. +type StatusNotifierItem_NewStatusSignal struct { + sender string + Path dbus.ObjectPath + Body *StatusNotifierItem_NewStatusSignalBody +} + +// Name returns the signal's name. +func (s *StatusNotifierItem_NewStatusSignal) Name() string { + return "NewStatus" +} + +// Interface returns the signal's interface. +func (s *StatusNotifierItem_NewStatusSignal) Interface() string { + return InterfaceStatusNotifierItem +} + +// Sender returns the signal's sender unique name. +func (s *StatusNotifierItem_NewStatusSignal) Sender() string { + return s.sender +} + +func (s *StatusNotifierItem_NewStatusSignal) path() dbus.ObjectPath { + return s.Path +} + +func (s *StatusNotifierItem_NewStatusSignal) values() []interface{} { + return []interface{}{s.Body.Status} +} + +// StatusNotifierItem_NewStatusSignalBody is body container. +type StatusNotifierItem_NewStatusSignalBody struct { + Status string +} + +// StatusNotifierItem_NewIconThemePathSignal represents org.kde.StatusNotifierItem.NewIconThemePath signal. +type StatusNotifierItem_NewIconThemePathSignal struct { + sender string + Path dbus.ObjectPath + Body *StatusNotifierItem_NewIconThemePathSignalBody +} + +// Name returns the signal's name. +func (s *StatusNotifierItem_NewIconThemePathSignal) Name() string { + return "NewIconThemePath" +} + +// Interface returns the signal's interface. +func (s *StatusNotifierItem_NewIconThemePathSignal) Interface() string { + return InterfaceStatusNotifierItem +} + +// Sender returns the signal's sender unique name. +func (s *StatusNotifierItem_NewIconThemePathSignal) Sender() string { + return s.sender +} + +func (s *StatusNotifierItem_NewIconThemePathSignal) path() dbus.ObjectPath { + return s.Path +} + +func (s *StatusNotifierItem_NewIconThemePathSignal) values() []interface{} { + return []interface{}{s.Body.IconThemePath} +} + +// StatusNotifierItem_NewIconThemePathSignalBody is body container. +type StatusNotifierItem_NewIconThemePathSignalBody struct { + IconThemePath string +} + +// StatusNotifierItem_NewMenuSignal represents org.kde.StatusNotifierItem.NewMenu signal. +type StatusNotifierItem_NewMenuSignal struct { + sender string + Path dbus.ObjectPath + Body *StatusNotifierItem_NewMenuSignalBody +} + +// Name returns the signal's name. +func (s *StatusNotifierItem_NewMenuSignal) Name() string { + return "NewMenu" +} + +// Interface returns the signal's interface. +func (s *StatusNotifierItem_NewMenuSignal) Interface() string { + return InterfaceStatusNotifierItem +} + +// Sender returns the signal's sender unique name. +func (s *StatusNotifierItem_NewMenuSignal) Sender() string { + return s.sender +} + +func (s *StatusNotifierItem_NewMenuSignal) path() dbus.ObjectPath { + return s.Path +} + +func (s *StatusNotifierItem_NewMenuSignal) values() []interface{} { + return []interface{}{} +} + +// StatusNotifierItem_NewMenuSignalBody is body container. +type StatusNotifierItem_NewMenuSignalBody struct { +} diff --git a/src/native/linux/jna/bridge_linux.go b/src/native/linux/jna/bridge_linux.go new file mode 100644 index 00000000..1a6bdda1 --- /dev/null +++ b/src/native/linux/jna/bridge_linux.go @@ -0,0 +1,297 @@ +//go:build linux + +package main + +/* +#include + +typedef void (*void_cb)(); +typedef void (*menu_item_cb)(uint32_t id); + +static void call_void(void_cb f) { if (f) { f(); } } +static void call_menu_item(menu_item_cb f, uint32_t id) { if (f) { f(id); } } +*/ +import "C" +import ( + "log" + "unsafe" + + systraypkg "github.com/energye/systray" +) + +var ( + cbReady C.void_cb + cbExit C.void_cb + cbOnClick C.void_cb + cbOnRClick C.void_cb + cbOnMenuSel C.menu_item_cb + + startFn func() + endFn func() + + running bool +) + +//export Systray_InitCallbacks +func Systray_InitCallbacks(ready C.void_cb, exit C.void_cb, onClick C.void_cb, onRClick C.void_cb, onMenuItem C.menu_item_cb) { + cbReady = ready + cbExit = exit + cbOnClick = onClick + cbOnRClick = onRClick + cbOnMenuSel = onMenuItem + + // Wire Go-level callbacks to call out to the provided C callbacks + systraypkg.SetOnClick(func(menu systraypkg.IMenu) { C.call_void(cbOnClick) }) + systraypkg.SetOnRClick(func(menu systraypkg.IMenu) { C.call_void(cbOnRClick) }) + systraypkg.SetOnDClick(func(menu systraypkg.IMenu) { C.call_void(cbOnClick) }) // reuse onClick for double click + systraypkg.SetOnMenuItemSelected(func(id uint32) { C.call_menu_item(cbOnMenuSel, C.uint(id)) }) +} + +//export Systray_Run +func Systray_Run() { + systraypkg.Run( + func() { running = true; C.call_void(cbReady) }, + func() { running = false; C.call_void(cbExit) }, + ) +} + +//export Systray_PrepareExternalLoop +func Systray_PrepareExternalLoop() { + startFn, endFn = systraypkg.RunWithExternalLoop( + func() { running = true; C.call_void(cbReady) }, + func() { running = false; C.call_void(cbExit) }, + ) +} + +//export Systray_NativeStart +func Systray_NativeStart() { + if startFn != nil { + startFn() + } +} + +//export Systray_NativeEnd +func Systray_NativeEnd() { + if endFn != nil { + endFn() + } + // ensure state cleaned for next run + running = false + startFn = nil + endFn = nil +} + +//export Systray_Quit +func Systray_Quit() { running = false; systraypkg.Quit() } + +//export Systray_SetIcon +func Systray_SetIcon(iconBytes *C.char, length C.int) { + defer func() { if r := recover(); r != nil { log.Printf("[systray] recovered panic in SetIcon: %v", r) } }() + if !running { + return + } + if iconBytes == nil || length <= 0 { + return + } + b := C.GoBytes(unsafe.Pointer(iconBytes), length) + systraypkg.SetIcon(b) +} + +//export Systray_SetTitle +func Systray_SetTitle(title *C.char) { + defer func() { if r := recover(); r != nil { log.Printf("[systray] recovered panic in SetTitle: %v", r) } }() + if !running { + return + } + if title == nil { + return + } + systraypkg.SetTitle(C.GoString(title)) +} + +//export Systray_SetTooltip +func Systray_SetTooltip(tooltip *C.char) { + defer func() { if r := recover(); r != nil { log.Printf("[systray] recovered panic in SetTooltip: %v", r) } }() + if !running { + return + } + if tooltip == nil { + return + } + systraypkg.SetTooltip(C.GoString(tooltip)) +} + +//export Systray_AddMenuItem +func Systray_AddMenuItem(title *C.char, tooltip *C.char) C.uint { + var t, tt string + if title != nil { + t = C.GoString(title) + } + if tooltip != nil { + tt = C.GoString(tooltip) + } + item := systraypkg.AddMenuItem(t, tt) + return C.uint(item.ID()) +} + +//export Systray_AddMenuItemCheckbox +func Systray_AddMenuItemCheckbox(title *C.char, tooltip *C.char, checked C.int) C.uint { + defer func() { if r := recover(); r != nil { log.Printf("[systray] recovered panic in AddMenuItemCheckbox: %v", r) } }() + if !running { + return 0 + } + var t, tt string + if title != nil { + t = C.GoString(title) + } + if tooltip != nil { + tt = C.GoString(tooltip) + } + item := systraypkg.AddMenuItemCheckbox(t, tt, checked != 0) + return C.uint(item.ID()) +} + +//export Systray_AddSeparator +func Systray_AddSeparator() { + defer func() { if r := recover(); r != nil { log.Printf("[systray] recovered panic in AddSeparator: %v", r) } }() + if !running { + return + } + systraypkg.AddSeparator() +} + +//export Systray_ResetMenu +func Systray_ResetMenu() { + defer func() { if r := recover(); r != nil { log.Printf("[systray] recovered panic in ResetMenu: %v", r) } }() + if !running { + return + } + systraypkg.ResetMenu() +} + +// Submenu creation +// +//export Systray_AddSubMenuItem +func Systray_AddSubMenuItem(parentID C.uint, title *C.char, tooltip *C.char) C.uint { + defer func() { if r := recover(); r != nil { log.Printf("[systray] recovered panic in AddSubMenuItem: %v", r) } }() + if !running { + return 0 + } + var t, tt string + if title != nil { + t = C.GoString(title) + } + if tooltip != nil { + tt = C.GoString(tooltip) + } + id := systraypkg.AddSubMenuItemByID(uint32(parentID), t, tt) + return C.uint(id) +} + +//export Systray_AddSubMenuItemCheckbox +func Systray_AddSubMenuItemCheckbox(parentID C.uint, title *C.char, tooltip *C.char, checked C.int) C.uint { + defer func() { if r := recover(); r != nil { log.Printf("[systray] recovered panic in AddSubMenuItemCheckbox: %v", r) } }() + if !running { + return 0 + } + var t, tt string + if title != nil { + t = C.GoString(title) + } + if tooltip != nil { + tt = C.GoString(tooltip) + } + id := systraypkg.AddSubMenuItemCheckboxByID(uint32(parentID), t, tt, checked != 0) + return C.uint(id) +} + +//export Systray_AddSubMenuSeparator +func Systray_AddSubMenuSeparator(parentID C.uint) { + defer func() { if r := recover(); r != nil { log.Printf("[systray] recovered panic in AddSubMenuSeparator: %v", r) } }() + if !running { + return + } + systraypkg.AddSeparatorByID(uint32(parentID)) +} + +// Per-item operations +// +//export Systray_MenuItem_SetTitle +func Systray_MenuItem_SetTitle(id C.uint, title *C.char) C.int { + defer func() { if r := recover(); r != nil { log.Printf("[systray] recovered panic in MenuItem_SetTitle: %v", r) } }() + if !running { + return 0 + } + if title == nil { + return 0 + } + ok := systraypkg.SetMenuItemTitleByID(uint32(id), C.GoString(title)) + if ok { + return 1 + } + return 0 +} + +//export Systray_MenuItem_Enable +func Systray_MenuItem_Enable(id C.uint) { + defer func() { if r := recover(); r != nil { log.Printf("[systray] recovered panic in MenuItem_Enable: %v", r) } }() + if !running { return } + _ = systraypkg.EnableMenuItemByID(uint32(id)) +} + +//export Systray_MenuItem_Disable +func Systray_MenuItem_Disable(id C.uint) { + defer func() { if r := recover(); r != nil { log.Printf("[systray] recovered panic in MenuItem_Disable: %v", r) } }() + if !running { return } + _ = systraypkg.DisableMenuItemByID(uint32(id)) +} + +//export Systray_MenuItem_Show +func Systray_MenuItem_Show(id C.uint) { + defer func() { if r := recover(); r != nil { log.Printf("[systray] recovered panic in MenuItem_Show: %v", r) } }() + if !running { return } + _ = systraypkg.ShowMenuItemByID(uint32(id)) +} + +//export Systray_MenuItem_Hide +func Systray_MenuItem_Hide(id C.uint) { + defer func() { if r := recover(); r != nil { log.Printf("[systray] recovered panic in MenuItem_Hide: %v", r) } }() + if !running { return } + _ = systraypkg.HideMenuItemByID(uint32(id)) +} + +//export Systray_MenuItem_Check +func Systray_MenuItem_Check(id C.uint) { + defer func() { if r := recover(); r != nil { log.Printf("[systray] recovered panic in MenuItem_Check: %v", r) } }() + if !running { return } + _ = systraypkg.CheckMenuItemByID(uint32(id)) +} + +//export Systray_MenuItem_Uncheck +func Systray_MenuItem_Uncheck(id C.uint) { + defer func() { if r := recover(); r != nil { log.Printf("[systray] recovered panic in MenuItem_Uncheck: %v", r) } }() + if !running { return } + _ = systraypkg.UncheckMenuItemByID(uint32(id)) +} + +//export Systray_SetMenuItemIcon +func Systray_SetMenuItemIcon(iconBytes *C.char, length C.int, id C.uint) { + defer func() { if r := recover(); r != nil { log.Printf("[systray] recovered panic in SetMenuItemIcon: %v", r) } }() + if !running { return } + if iconBytes == nil || length <= 0 { + return + } + b := C.GoBytes(unsafe.Pointer(iconBytes), length) + _ = systraypkg.SetMenuItemIconByID(uint32(id), b) +} + +//export Systray_GetLastClickXY +func Systray_GetLastClickXY(outX *C.int, outY *C.int) { + if outX == nil || outY == nil { + return + } + // No running check required; last coordinates may be queried anytime during runtime. + x, y := systraypkg.GetLastClickXY() + *outX = C.int(x) + *outY = C.int(y) +} diff --git a/src/native/linux/jna/main.go b/src/native/linux/jna/main.go new file mode 100644 index 00000000..34a9cd31 --- /dev/null +++ b/src/native/linux/jna/main.go @@ -0,0 +1,5 @@ +//go:build linux + +package main + +func main() {} diff --git a/src/native/linux/systray.go b/src/native/linux/systray.go new file mode 100644 index 00000000..0e162159 --- /dev/null +++ b/src/native/linux/systray.go @@ -0,0 +1,404 @@ +// Package systray is a Linux-only Go library to place an icon and menu in the notification area. +package systray + +import ( + "fmt" + "log" + "runtime" + "sync" + "sync/atomic" +) + +var ( + systrayReady func() + systrayExit func() + menuItems = make(map[uint32]*MenuItem) + menuItemsLock sync.RWMutex + + currentID = uint32(0) + quitOnce sync.Once + dClickTimeMinInterval int64 = 500 +) + +func init() { + runtime.LockOSThread() +} + +type IMenu interface { + ShowMenu() error +} + +// MenuItem is used to keep track each menu item of systray. +// Don't create it directly, use the one systray.AddMenuItem() returned +type MenuItem struct { + // ClickedCh is the channel which will be notified when the menu item is clicked + click func() + + // id uniquely identify a menu item, not supposed to be modified + id uint32 + // title is the text shown on menu item + title string + // tooltip is the text shown when pointing to menu item + tooltip string + // shortcutKey Menu shortcut key + shortcutKey string + // disabled menu item is grayed out and has no effect when clicked + disabled bool + // checked menu item has a tick before the title + checked bool + // has the menu item a checkbox (Linux) + isCheckable bool + // parent item, for sub menus + parent *MenuItem +} + +// ID returns the unique ID of the menu item +func (item *MenuItem) ID() uint32 { return item.id } + +func (item *MenuItem) Click(fn func()) { + item.click = fn +} + +func (item *MenuItem) String() string { + if item.parent == nil { + return fmt.Sprintf("MenuItem[%d, %q]", item.id, item.title) + } + return fmt.Sprintf("MenuItem[%d, parent %d, %q]", item.id, item.parent.id, item.title) +} + +// newMenuItem returns a populated MenuItem object +func newMenuItem(title string, tooltip string, parent *MenuItem) *MenuItem { + return &MenuItem{ + id: atomic.AddUint32(¤tID, 1), + title: title, + tooltip: tooltip, + shortcutKey: "", + disabled: false, + checked: false, + isCheckable: false, + parent: parent, + } +} + +// Run initializes GUI and starts the event loop, then invokes the onReady +// callback. It blocks until systray.Quit() is called. +func Run(onReady, onExit func()) { + setInternalLoop(true) + Register(onReady, onExit) + + nativeLoop() +} + +// 设置鼠标左键双击事件的时间间隔 默认500毫秒 +func SetDClickTimeMinInterval(value int64) { + dClickTimeMinInterval = value +} + +// 设置托盘鼠标左键点击事件 +func SetOnClick(fn func(menu IMenu)) { + setOnClick(fn) +} + +// 设置托盘鼠标左键双击事件 +func SetOnDClick(fn func(menu IMenu)) { + setOnDClick(fn) +} + +// 设置托盘鼠标右键事件反馈回调 +// 支持windows 和 macosx,不支持linux +// 设置事件,菜单默认将不展示,通过menu.ShowMenu()函数显示 +// 未设置事件,默认右键显示托盘菜单 +// macosx ShowMenu()只支持OnRClick函数内调用 +func SetOnRClick(fn func(menu IMenu)) { + setOnRClick(fn) +} + +// RunWithExternalLoop allows the systemtray module to operate with other tookits. +// The returned start and end functions should be called by the toolkit when the application has started and will end. +func RunWithExternalLoop(onReady, onExit func()) (start, end func()) { + Register(onReady, onExit) + + return nativeStart, func() { + // External loop: no nativeLoop is running, so perform full teardown here. + // First signal quit (idempotent), then run nativeEnd() to unexport and release resources. + Quit() + nativeEnd() + } +} + +// Register initializes GUI and registers the callbacks but relies on the +// caller to run the event loop somewhere else. It's useful if the program +// needs to show other UI elements, for example, webview. +// To overcome some OS weirdness, On macOS versions before Catalina, calling +// this does exactly the same as Run(). +func Register(onReady func(), onExit func()) { + if onReady == nil { + systrayReady = nil + } else { + var readyCh = make(chan interface{}) + // Run onReady on separate goroutine to avoid blocking event loop + go func() { + <-readyCh + onReady() + }() + systrayReady = func() { + systrayReady = nil + close(readyCh) + } + } + // unlike onReady, onExit runs in the event loop to make sure it has time to + // finish before the process terminates + if onExit == nil { + onExit = func() {} + } + systrayExit = onExit + registerSystray() +} + +// ResetMenu will remove all menu items +func ResetMenu() { + resetMenu() +} + +// Quit the systray +func Quit() { + quitOnce.Do(quit) +} + +// AddMenuItem adds a menu item with the designated title and tooltip. +// It can be safely invoked from different goroutines. +// Created menu items are checkable on Windows and OSX by default. For Linux you have to use AddMenuItemCheckbox +func AddMenuItem(title string, tooltip string) *MenuItem { + item := newMenuItem(title, tooltip, nil) + item.update() + return item +} + +// AddMenuItemCheckbox adds a menu item with the designated title and tooltip and a checkbox for Linux. +// It can be safely invoked from different goroutines. +// On Windows and OSX this is the same as calling AddMenuItem +func AddMenuItemCheckbox(title string, tooltip string, checked bool) *MenuItem { + item := newMenuItem(title, tooltip, nil) + item.isCheckable = true + item.checked = checked + item.update() + return item +} + +// AddSeparator adds a separator bar to the menu +func AddSeparator() { + addSeparator(atomic.AddUint32(¤tID, 1)) +} + +// AddSubMenuItem adds a nested sub-menu item with the designated title and tooltip. +// It can be safely invoked from different goroutines. +// Created menu items are checkable on Windows and OSX by default. For Linux you have to use AddSubMenuItemCheckbox +func (item *MenuItem) AddSubMenuItem(title string, tooltip string) *MenuItem { + child := newMenuItem(title, tooltip, item) + child.update() + return child +} + +// AddSubMenuItemCheckbox adds a nested sub-menu item with the designated title and tooltip and a checkbox for Linux. +// It can be safely invoked from different goroutines. +// On Windows and OSX this is the same as calling AddSubMenuItem +func (item *MenuItem) AddSubMenuItemCheckbox(title string, tooltip string, checked bool) *MenuItem { + child := newMenuItem(title, tooltip, item) + child.isCheckable = true + child.checked = checked + child.update() + return child +} + +// SetTitle set the text to display on a menu item +func (item *MenuItem) SetTitle(title string) { + item.title = title + item.update() +} + +// SetTooltip set the tooltip to show when mouse hover +func (item *MenuItem) SetTooltip(tooltip string) { + item.tooltip = tooltip + item.update() +} + +// Disabled checks if the menu item is disabled +func (item *MenuItem) Disabled() bool { + return item.disabled +} + +// Enable a menu item regardless if it's previously enabled or not +func (item *MenuItem) Enable() { + item.disabled = false + item.update() +} + +// Disable a menu item regardless if it's previously disabled or not +func (item *MenuItem) Disable() { + item.disabled = true + item.update() +} + +// Hide hides a menu item +func (item *MenuItem) Hide() { + hideMenuItem(item) +} + +// Show shows a previously hidden menu item +func (item *MenuItem) Show() { + showMenuItem(item) +} + +// Checked returns if the menu item has a check mark +func (item *MenuItem) Checked() bool { + return item.checked +} + +// Check a menu item regardless if it's previously checked or not +func (item *MenuItem) Check() { + item.checked = true + item.update() +} + +// Uncheck a menu item regardless if it's previously unchecked or not +func (item *MenuItem) Uncheck() { + item.checked = false + item.update() +} + +// update propagates changes on a menu item to systray +func (item *MenuItem) update() { + menuItemsLock.Lock() + menuItems[item.id] = item + menuItemsLock.Unlock() + addOrUpdateMenuItem(item) +} + +var menuItemSelected func(id uint32) + +func SetOnMenuItemSelected(fn func(id uint32)) { + menuItemSelected = fn +} + +func systrayMenuItemSelected(id uint32) { + menuItemsLock.RLock() + item, ok := menuItems[id] + menuItemsLock.RUnlock() + if !ok { + log.Printf("systray error: no menu item with ID %d\n", id) + return + } + if item.click != nil { + item.click() + } + if menuItemSelected != nil { + menuItemSelected(id) + } +} + +// --- Helpers to control menu items by ID for external (JNA) callers --- + +// getMenuItem returns the menu item and whether it exists +func getMenuItem(id uint32) (*MenuItem, bool) { + menuItemsLock.RLock() + defer menuItemsLock.RUnlock() + item, ok := menuItems[id] + return item, ok +} + +// AddSubMenuItemByID creates a child under the given parent id and returns the new id; 0 on failure +func AddSubMenuItemByID(parentID uint32, title, tooltip string) uint32 { + parent, ok := getMenuItem(parentID) + if !ok || parent == nil { + return 0 + } + child := parent.AddSubMenuItem(title, tooltip) + return child.ID() +} + +// AddSubMenuItemCheckboxByID creates a checkable child under the given parent id and returns the new id; 0 on failure +func AddSubMenuItemCheckboxByID(parentID uint32, title, tooltip string, checked bool) uint32 { + parent, ok := getMenuItem(parentID) + if !ok || parent == nil { + return 0 + } + child := parent.AddSubMenuItemCheckbox(title, tooltip, checked) + return child.ID() +} + +// AddSeparatorByID adds a separator under the given parent item id. If parentID is 0, it adds to the root menu. +func AddSeparatorByID(parentID uint32) { + addSeparatorUnder(parentID, atomic.AddUint32(¤tID, 1)) +} + +// SetMenuItemTitleByID sets the title of a menu item; returns false if not found +func SetMenuItemTitleByID(id uint32, title string) bool { + if item, ok := getMenuItem(id); ok { + item.SetTitle(title) + return true + } + return false +} + +// EnableMenuItemByID enables a menu item; returns false if not found +func EnableMenuItemByID(id uint32) bool { + if item, ok := getMenuItem(id); ok { + item.Enable() + return true + } + return false +} + +// DisableMenuItemByID disables a menu item; returns false if not found +func DisableMenuItemByID(id uint32) bool { + if item, ok := getMenuItem(id); ok { + item.Disable() + return true + } + return false +} + +// ShowMenuItemByID shows a menu item; returns false if not found +func ShowMenuItemByID(id uint32) bool { + if item, ok := getMenuItem(id); ok { + item.Show() + return true + } + return false +} + +// HideMenuItemByID hides a menu item; returns false if not found +func HideMenuItemByID(id uint32) bool { + if item, ok := getMenuItem(id); ok { + item.Hide() + return true + } + return false +} + +// CheckMenuItemByID checks a menu item; returns false if not found +func CheckMenuItemByID(id uint32) bool { + if item, ok := getMenuItem(id); ok { + item.Check() + return true + } + return false +} + +// UncheckMenuItemByID unchecks a menu item; returns false if not found +func UncheckMenuItemByID(id uint32) bool { + if item, ok := getMenuItem(id); ok { + item.Uncheck() + return true + } + return false +} + +// SetMenuItemIconByID sets the icon of a menu item; returns false if not found +func SetMenuItemIconByID(id uint32, iconBytes []byte) bool { + if item, ok := getMenuItem(id); ok { + item.SetIcon(iconBytes) + return true + } + return false +} diff --git a/src/native/linux/systray.h b/src/native/linux/systray.h new file mode 100644 index 00000000..774f618e --- /dev/null +++ b/src/native/linux/systray.h @@ -0,0 +1,27 @@ +#include "stdbool.h" + +extern void systray_ready(); +extern void systray_on_exit(); +extern void systray_menu_item_selected(int menu_id); +extern void systray_on_click(); +extern void systray_on_rclick(); + +void registerSystray(void); +void nativeEnd(void); +int nativeLoop(void); +void nativeStart(void); + +void setIcon(const char* iconBytes, int length, bool template); +void setMenuItemIcon(const char* iconBytes, int length, int menuId, bool template); +void setTitle(char* title); +void setTooltip(char* tooltip); +void add_or_update_menu_item(int menuId, int parentMenuId, char* title, char* tooltip, char* shortcutKey, short disabled, short checked, short isCheckable); +void add_separator(int menuId); +void hide_menu_item(int menuId); +void show_menu_item(int menuId); +void reset_menu(); +void create_menu(); +void show_menu(); +void set_menu_nil(); +void quit(); +void enable_on_click(); diff --git a/src/native/linux/systray_menu_unix.go b/src/native/linux/systray_menu_unix.go new file mode 100644 index 00000000..1433fac1 --- /dev/null +++ b/src/native/linux/systray_menu_unix.go @@ -0,0 +1,345 @@ +//go:build linux || freebsd || openbsd || netbsd +// +build linux freebsd openbsd netbsd + +package systray + +import ( + "log" + + "github.com/godbus/dbus/v5" + "github.com/godbus/dbus/v5/prop" + + "github.com/energye/systray/internal/generated/menu" +) + +// SetIcon sets the icon of a menu item. +// iconBytes should be the content of .ico/.jpg/.png +func (item *MenuItem) SetIcon(iconBytes []byte) { + instance.menuLock.Lock() + defer instance.menuLock.Unlock() + m, exists := findLayout(int32(item.id)) + if exists { + m.V1["icon-data"] = dbus.MakeVariant(iconBytes) + refresh() + } +} + +// copyLayout makes full copy of layout +func copyLayout(in *menuLayout, depth int32) *menuLayout { + out := menuLayout{ + V0: in.V0, + V1: make(map[string]dbus.Variant, len(in.V1)), + } + for k, v := range in.V1 { + out.V1[k] = v + } + if depth != 0 { + depth-- + out.V2 = make([]dbus.Variant, len(in.V2)) + for i, v := range in.V2 { + out.V2[i] = dbus.MakeVariant(copyLayout(v.Value().(*menuLayout), depth)) + } + } else { + out.V2 = []dbus.Variant{} + } + return &out +} + +// GetLayout is com.canonical.dbusmenu.GetLayout method. +func (t *tray) GetLayout(parentID int32, recursionDepth int32, propertyNames []string) (revision uint32, layout menuLayout, err *dbus.Error) { + instance.menuLock.Lock() + defer instance.menuLock.Unlock() + if m, ok := findLayout(parentID); ok { + // return copy of menu layout to prevent panic from cuncurrent access to layout + return instance.menuVersion, *copyLayout(m, recursionDepth), nil + } + return +} + +// GetGroupProperties is com.canonical.dbusmenu.GetGroupProperties method. +func (t *tray) GetGroupProperties(ids []int32, propertyNames []string) (properties []struct { + V0 int32 + V1 map[string]dbus.Variant +}, err *dbus.Error) { + instance.menuLock.Lock() + defer instance.menuLock.Unlock() + for _, id := range ids { + if m, ok := findLayout(id); ok { + p := struct { + V0 int32 + V1 map[string]dbus.Variant + }{ + V0: m.V0, + V1: make(map[string]dbus.Variant, len(m.V1)), + } + for k, v := range m.V1 { + p.V1[k] = v + } + properties = append(properties, p) + } + } + return +} + +// GetProperty is com.canonical.dbusmenu.GetProperty method. +func (t *tray) GetProperty(id int32, name string) (value dbus.Variant, err *dbus.Error) { + instance.menuLock.Lock() + defer instance.menuLock.Unlock() + if m, ok := findLayout(id); ok { + if p, ok := m.V1[name]; ok { + return p, nil + } + } + return +} + +// Event is com.canonical.dbusmenu.Event method. +func (t *tray) Event(id int32, eventID string, data dbus.Variant, timestamp uint32) (err *dbus.Error) { + if eventID == "clicked" { + systrayMenuItemSelected(uint32(id)) + } + return +} + +// EventGroup is com.canonical.dbusmenu.EventGroup method. +func (t *tray) EventGroup(events []struct { + V0 int32 + V1 string + V2 dbus.Variant + V3 uint32 +}) (idErrors []int32, err *dbus.Error) { + for _, event := range events { + if event.V1 == "clicked" { + systrayMenuItemSelected(uint32(event.V0)) + } + } + return +} + +// AboutToShow is com.canonical.dbusmenu.AboutToShow method. +func (t *tray) AboutToShow(id int32) (needUpdate bool, err *dbus.Error) { + return +} + +// AboutToShowGroup is com.canonical.dbusmenu.AboutToShowGroup method. +func (t *tray) AboutToShowGroup(ids []int32) (updatesNeeded []int32, idErrors []int32, err *dbus.Error) { + return +} + +func createMenuPropSpec() map[string]map[string]*prop.Prop { + instance.menuLock.Lock() + defer instance.menuLock.Unlock() + return map[string]map[string]*prop.Prop{ + "com.canonical.dbusmenu": { + "Version": { + Value: instance.menuVersion, + Writable: true, + Emit: prop.EmitTrue, + Callback: nil, + }, + "TextDirection": { + Value: "ltr", + Writable: false, + Emit: prop.EmitTrue, + Callback: nil, + }, + "Status": { + Value: "normal", + Writable: false, + Emit: prop.EmitTrue, + Callback: nil, + }, + "IconThemePath": { + Value: []string{}, + Writable: false, + Emit: prop.EmitTrue, + Callback: nil, + }, + }, + } +} + +// menuLayout is a named struct to map into generated bindings. It represents the layout of a menu item +type menuLayout = struct { + V0 int32 // the unique ID of this item + V1 map[string]dbus.Variant // properties for this menu item layout + V2 []dbus.Variant // child menu item layouts +} + +func addOrUpdateMenuItem(item *MenuItem) { + var layout *menuLayout + instance.menuLock.Lock() + defer instance.menuLock.Unlock() + m, exists := findLayout(int32(item.id)) + if exists { + layout = m + } else { + layout = &menuLayout{ + V0: int32(item.id), + V1: map[string]dbus.Variant{}, + V2: []dbus.Variant{}, + } + + parent := instance.menu + if item.parent != nil { + m, ok := findLayout(int32(item.parent.id)) + if ok { + parent = m + parent.V1["children-display"] = dbus.MakeVariant("submenu") + } + } + parent.V2 = append(parent.V2, dbus.MakeVariant(layout)) + } + + applyItemToLayout(item, layout) + // Always refresh after applying layout so KDE receives LayoutUpdated even for newly created items + refresh() + // When running under GNOME, ensure the SNI Menu prop points to the real menu path (exit no-menu state) + if noMenuPathForEnvironment() == dbus.ObjectPath("/") { + setMenuPropTo(dbus.ObjectPath(menuPath)) + } +} + +func addSeparator(id uint32) { + instance.menuLock.Lock() + defer instance.menuLock.Unlock() + layout := &menuLayout{ + V0: int32(id), + V1: map[string]dbus.Variant{ + "type": dbus.MakeVariant("separator"), + }, + V2: []dbus.Variant{}, + } + instance.menu.V2 = append(instance.menu.V2, dbus.MakeVariant(layout)) + refresh() + // On GNOME, ensure SNI Menu points to the real menu when items are present (exit no-menu state) + if noMenuPathForEnvironment() == dbus.ObjectPath("/") { + setMenuPropTo(dbus.ObjectPath(menuPath)) + } +} + +// addSeparatorUnder appends a separator under the given parent menu item id (0 targets root) +func addSeparatorUnder(parentID uint32, id uint32) { + instance.menuLock.Lock() + defer instance.menuLock.Unlock() + var parent *menuLayout + if parentID == 0 { + parent = instance.menu + } else { + p, ok := findLayout(int32(parentID)) + if !ok || p == nil { + return + } + parent = p + parent.V1["children-display"] = dbus.MakeVariant("submenu") + } + layout := &menuLayout{ + V0: int32(id), + V1: map[string]dbus.Variant{ + "type": dbus.MakeVariant("separator"), + }, + V2: []dbus.Variant{}, + } + parent.V2 = append(parent.V2, dbus.MakeVariant(layout)) + refresh() + if noMenuPathForEnvironment() == dbus.ObjectPath("/") { + setMenuPropTo(dbus.ObjectPath(menuPath)) + } +} + +func applyItemToLayout(in *MenuItem, out *menuLayout) { + out.V1["enabled"] = dbus.MakeVariant(!in.disabled) + out.V1["label"] = dbus.MakeVariant(in.title) + + if in.isCheckable { + out.V1["toggle-type"] = dbus.MakeVariant("checkmark") + if in.checked { + out.V1["toggle-state"] = dbus.MakeVariant(1) + } else { + out.V1["toggle-state"] = dbus.MakeVariant(0) + } + } else { + out.V1["toggle-type"] = dbus.MakeVariant("") + out.V1["toggle-state"] = dbus.MakeVariant(0) + } +} + +func findLayout(id int32) (*menuLayout, bool) { + if id == 0 { + return instance.menu, true + } + return findSubLayout(id, instance.menu.V2) +} + +func findSubLayout(id int32, vals []dbus.Variant) (*menuLayout, bool) { + for _, i := range vals { + item := i.Value().(*menuLayout) + if item.V0 == id { + return item, true + } + + if len(item.V2) > 0 { + child, ok := findSubLayout(id, item.V2) + if ok { + return child, true + } + } + } + + return nil, false +} + +func hideMenuItem(item *MenuItem) { + instance.menuLock.Lock() + defer instance.menuLock.Unlock() + m, exists := findLayout(int32(item.id)) + if exists { + m.V1["visible"] = dbus.MakeVariant(false) + refresh() + } +} + +func showMenuItem(item *MenuItem) { + instance.menuLock.Lock() + defer instance.menuLock.Unlock() + m, exists := findLayout(int32(item.id)) + if exists { + m.V1["visible"] = dbus.MakeVariant(true) + refresh() + } +} + +func refresh() { + if instance.conn == nil || instance.menuProps == nil { + return + } + instance.menuVersion++ + dbusErr := instance.menuProps.Set("com.canonical.dbusmenu", "Version", + dbus.MakeVariant(instance.menuVersion)) + if dbusErr != nil { + log.Printf("systray error: failed to update menu version: %s\n", dbusErr) + return + } + err := menu.Emit(instance.conn, &menu.Dbusmenu_LayoutUpdatedSignal{ + Path: menuPath, + Body: &menu.Dbusmenu_LayoutUpdatedSignalBody{ + Revision: instance.menuVersion, + }, + }) + if err != nil { + log.Printf("systray error: failed to emit layout updated signal: %s\n", err) + } + +} + +func resetMenu() { + instance.menuLock.Lock() + defer instance.menuLock.Unlock() + instance.menu = &menuLayout{} + instance.menuVersion++ + refresh() + // GNOME-only: advertise "/" when there's no menu; do not modify the property for other environments + if noMenuPathForEnvironment() == dbus.ObjectPath("/") { + setMenuPropTo(dbus.ObjectPath("/")) + } +} diff --git a/src/native/linux/systray_other.go b/src/native/linux/systray_other.go new file mode 100644 index 00000000..0d70bcbb --- /dev/null +++ b/src/native/linux/systray_other.go @@ -0,0 +1,183 @@ +//go:build !linux && !freebsd && !openbsd && !netbsd +// +build !linux,!freebsd,!openbsd,!netbsd + +package systray + +import ( + "errors" + "fmt" + "runtime" +) + +var errUnsupportedPlatform = errors.New("systray: unsupported platform, this library is Linux-only") + +// IMenu interface +type IMenu interface { + ShowMenu() error +} + +// Run initializes GUI and starts the event loop, then invokes the onReady +// callback. It blocks until systray.Quit() is called. +func Run(onReady, onExit func()) { + fmt.Printf("systray: unsupported platform '%s', this library is Linux-only\n", runtime.GOOS) + panic(errUnsupportedPlatform) +} + +// RunWithExternalLoop allows the systemtray module to operate with other tookits. +// The returned start and end functions should be called by the toolkit when the application has started and will end. +func RunWithExternalLoop(onReady, onExit func()) (start, end func()) { + fmt.Printf("systray: unsupported platform '%s', this library is Linux-only\n", runtime.GOOS) + panic(errUnsupportedPlatform) +} + +// CreateMenu is a no-op on non-Linux platforms +func CreateMenu() { + panic(errUnsupportedPlatform) +} + +// SetMenuNil is a no-op on non-Linux platforms +func SetMenuNil() { + panic(errUnsupportedPlatform) +} + +// SetIcon sets the systray icon. +func SetIcon(iconBytes []byte) { + panic(errUnsupportedPlatform) +} + +// SetTemplateIcon sets the systray icon as a template icon. +func SetTemplateIcon(templateIconBytes []byte, regularIconBytes []byte) { + panic(errUnsupportedPlatform) +} + +// SetTitle sets the systray title. +func SetTitle(title string) { + panic(errUnsupportedPlatform) +} + +// SetTooltip sets the systray tooltip. +func SetTooltip(tooltip string) { + panic(errUnsupportedPlatform) +} + +// SetOnClick sets the on click callback. +func SetOnClick(fn func(menu IMenu)) { + panic(errUnsupportedPlatform) +} + +// SetOnDClick sets the on double click callback. +func SetOnDClick(fn func(menu IMenu)) { + panic(errUnsupportedPlatform) +} + +// SetOnRClick sets the on right click callback. +func SetOnRClick(fn func(menu IMenu)) { + panic(errUnsupportedPlatform) +} + +// SetDClickTimeMinInterval sets the minimum time interval for double clicks. +func SetDClickTimeMinInterval(value int64) { + panic(errUnsupportedPlatform) +} + +// Quit the systray. +func Quit() { + panic(errUnsupportedPlatform) +} + +// AddMenuItem adds a menu item. +func AddMenuItem(title string, tooltip string) *MenuItem { + panic(errUnsupportedPlatform) +} + +// AddMenuItemCheckbox adds a menu item with a checkbox. +func AddMenuItemCheckbox(title string, tooltip string, checked bool) *MenuItem { + panic(errUnsupportedPlatform) +} + +// AddSeparator adds a separator. +func AddSeparator() { + panic(errUnsupportedPlatform) +} + +// ResetMenu resets the menu. +func ResetMenu() { + panic(errUnsupportedPlatform) +} + +// MenuItem methods + +// AddSubMenuItem adds a submenu item. +func (item *MenuItem) AddSubMenuItem(title string, tooltip string) *MenuItem { + panic(errUnsupportedPlatform) +} + +// AddSubMenuItemCheckbox adds a submenu item with a checkbox. +func (item *MenuItem) AddSubMenuItemCheckbox(title string, tooltip string, checked bool) *MenuItem { + panic(errUnsupportedPlatform) +} + +// SetTitle sets the menu item title. +func (item *MenuItem) SetTitle(title string) { + panic(errUnsupportedPlatform) +} + +// SetTooltip sets the menu item tooltip. +func (item *MenuItem) SetTooltip(tooltip string) { + panic(errUnsupportedPlatform) +} + +// Disabled checks if the menu item is disabled. +func (item *MenuItem) Disabled() bool { + panic(errUnsupportedPlatform) +} + +// Enable enables the menu item. +func (item *MenuItem) Enable() { + panic(errUnsupportedPlatform) +} + +// Disable disables the menu item. +func (item *MenuItem) Disable() { + panic(errUnsupportedPlatform) +} + +// Hide hides the menu item. +func (item *MenuItem) Hide() { + panic(errUnsupportedPlatform) +} + +// Show shows the menu item. +func (item *MenuItem) Show() { + panic(errUnsupportedPlatform) +} + +// Checked returns if the menu item is checked. +func (item *MenuItem) Checked() bool { + panic(errUnsupportedPlatform) +} + +// Check checks the menu item. +func (item *MenuItem) Check() { + panic(errUnsupportedPlatform) +} + +// Uncheck unchecks the menu item. +func (item *MenuItem) Uncheck() { + panic(errUnsupportedPlatform) +} + +// Click sets the click callback. +func (item *MenuItem) Click(fn func()) { + panic(errUnsupportedPlatform) +} + +// SetIcon sets the menu item icon. +func (item *MenuItem) SetIcon(iconBytes []byte) { + panic(errUnsupportedPlatform) +} + +// SetTemplateIcon sets the menu item template icon. +func (item *MenuItem) SetTemplateIcon(templateIconBytes []byte, regularIconBytes []byte) { + panic(errUnsupportedPlatform) +} diff --git a/src/native/linux/systray_unix.go b/src/native/linux/systray_unix.go new file mode 100644 index 00000000..aaa2576b --- /dev/null +++ b/src/native/linux/systray_unix.go @@ -0,0 +1,511 @@ +//go:build linux || freebsd || openbsd || netbsd +// +build linux freebsd openbsd netbsd + +// Note: you need github.com/knightpp/dbus-codegen-go installed from "custom" branch +//go:generate dbus-codegen-go -prefix org.kde -package notifier -output internal/generated/notifier/status_notifier_item.go internal/StatusNotifierItem.xml +//go:generate dbus-codegen-go -prefix com.canonical -package menu -output internal/generated/menu/dbus_menu.go internal/DbusMenu.xml + +package systray + +import ( + "bytes" + "fmt" + "image" + _ "image/jpeg" + _ "image/png" + "log" + "os" + "strings" + "sync" + "time" + + "github.com/godbus/dbus/v5/introspect" + "github.com/godbus/dbus/v5/prop" + + "github.com/energye/systray/internal/generated/menu" + "github.com/energye/systray/internal/generated/notifier" + dbus "github.com/godbus/dbus/v5" + + "golang.org/x/image/draw" +) + +const ( + path = "/StatusNotifierItem" + menuPath = "/StatusNotifierMenu" +) + +var ( + // to signal quitting the internal main loop + quitChan = make(chan struct{}) + + // instance is the current instance of our DBus tray server + instance = &tray{menu: &menuLayout{}, menuVersion: 1} +) + +// SetTemplateIcon sets the systray icon as a template icon (on macOS), falling back +// to a regular icon on other platforms. On Linux we just set the regular icon. +func SetTemplateIcon(_ []byte, regularIconBytes []byte) { + SetIcon(regularIconBytes) +} + +// SetIcon sets the systray icon (PNG/JPG/ICO supported by image/*). +func SetIcon(iconBytes []byte) { + instance.lock.Lock() + instance.iconData = iconBytes + props := instance.props + conn := instance.conn + instance.lock.Unlock() + + if props == nil { + return + } + + // Build a multi-resolution pixmap list (ARGB32 big-endian) for better quality. + pixmaps := buildPixmaps(iconBytes) + props.SetMust("org.kde.StatusNotifierItem", "IconPixmap", pixmaps) + + // Keep tooltip icon consistent with main icon + props.SetMust("org.kde.StatusNotifierItem", "ToolTip", + dbus.MakeVariant(tooltip{V2: instance.tooltipTitle, V1: pixmaps})) + + if conn == nil { + return + } + + if err := notifier.Emit(conn, ¬ifier.StatusNotifierItem_NewIconSignal{ + Path: path, + Body: ¬ifier.StatusNotifierItem_NewIconSignalBody{}, + }); err != nil { + log.Printf("systray error: failed to emit new icon signal: %s\n", err) + } +} + +// SetTitle sets the systray title. +func SetTitle(t string) { + instance.lock.Lock() + instance.title = t + props := instance.props + conn := instance.conn + instance.lock.Unlock() + + if props == nil { + return + } + if dbusErr := props.Set("org.kde.StatusNotifierItem", "Title", dbus.MakeVariant(t)); dbusErr != nil { + log.Printf("systray error: failed to set Title prop: %s\n", dbusErr) + return + } + if conn == nil { + return + } + if err := notifier.Emit(conn, ¬ifier.StatusNotifierItem_NewTitleSignal{ + Path: path, + Body: ¬ifier.StatusNotifierItem_NewTitleSignalBody{}, + }); err != nil { + log.Printf("systray error: failed to emit new title signal: %s\n", err) + } +} + +// SetTooltip sets the systray tooltip text and keeps the icon consistent. +func SetTooltip(tooltipTitle string) { + instance.lock.Lock() + instance.tooltipTitle = tooltipTitle + props := instance.props + iconData := instance.iconData + instance.lock.Unlock() + + if props == nil { + return + } + props.SetMust("org.kde.StatusNotifierItem", "ToolTip", + dbus.MakeVariant(tooltip{ + V2: tooltipTitle, + V1: buildPixmaps(iconData), // show same icon in tooltip if host uses it + }), + ) +} + +func (item *MenuItem) SetTemplateIcon(_ []byte, regularIconBytes []byte) { + item.SetIcon(regularIconBytes) +} + +func setInternalLoop(_ bool) {} + +// CreateMenu creates the tray menu (no-op on Linux). +func CreateMenu() {} + +// SetMenuNil sets the tray menu to nil (no-op on Linux). +func SetMenuNil() {} + +func registerSystray() {} + +func nativeLoop() int { + nativeStart() + <-quitChan + nativeEnd() + return 0 +} + +func nativeEnd() { + systrayExit() + + // Snapshot and clear shared state under lock to make teardown idempotent. + instance.lock.Lock() + conn := instance.conn + busName := instance.name + props := instance.props + menuProps := instance.menuProps + _ = menuProps + instance.conn = nil + instance.props = nil + instance.menuProps = nil + instance.iconData = nil + instance.title = "" + instance.tooltipTitle = "" + instance.lock.Unlock() + + if conn == nil { + return + } + + // Best-effort: mark status passive before unexport (optional). + if props != nil { + props.SetMust("org.kde.StatusNotifierItem", "Status", "Passive") + } + + // Unexport SNI and menu interfaces so the host removes our icon/menu immediately. + _ = notifier.UnexportStatusNotifierItem(conn, path) + _ = menu.UnexportDbusmenu(conn, menuPath) + + // Unexport Properties and Introspectable on both paths. + _ = conn.Export(nil, path, "org.freedesktop.DBus.Properties") + _ = conn.Export(nil, menuPath, "org.freedesktop.DBus.Properties") + _ = conn.Export(nil, path, "org.freedesktop.DBus.Introspectable") + _ = conn.Export(nil, menuPath, "org.freedesktop.DBus.Introspectable") + + // Release our well-known bus name so watchers drop the item instantly. + if busName != "" { + _, _ = conn.ReleaseName(busName) + } + + // Finally, close the connection asynchronously to avoid freezes. + done := make(chan struct{}) + go func() { + _ = conn.Close() + close(done) + }() + select { + case <-done: + case <-time.After(300 * time.Millisecond): + // Give up waiting; the background goroutine will finish later. + } +} + +func quit() { + close(quitChan) +} + +var usni = &UnimplementedStatusNotifierItem{} + +var ( + lastClickMu sync.Mutex + lastClickX int32 + lastClickY int32 +) + +type UnimplementedStatusNotifierItem struct { + contextMenu func(x int32, y int32) + activate func(x int32, y int32) + dActivate func(x int32, y int32) + secondaryActivate func(x int32, y int32) + scroll func(delta int32, orientation string) + dActivateTime int64 +} + +func (*UnimplementedStatusNotifierItem) iface() string { + return notifier.InterfaceStatusNotifierItem +} + +func (m *UnimplementedStatusNotifierItem) ContextMenu(x int32, y int32) (err *dbus.Error) { + // remember last right-click coordinates too (for context menu) + lastClickMu.Lock() + lastClickX, lastClickY = x, y + lastClickMu.Unlock() + if m.contextMenu != nil { + m.contextMenu(x, y) + } else { + err = &dbus.ErrMsgUnknownMethod + } + return +} + +func (m *UnimplementedStatusNotifierItem) Activate(x int32, y int32) (err *dbus.Error) { + // remember last click coordinates + lastClickMu.Lock() + lastClickX, lastClickY = x, y + lastClickMu.Unlock() + + if m.dActivateTime == 0 { + m.dActivateTime = time.Now().UnixMilli() + } else { + nowMilli := time.Now().UnixMilli() + if nowMilli-m.dActivateTime < dClickTimeMinInterval { + m.dActivateTime = dClickTimeMinInterval + if m.dActivate != nil { + m.dActivate(x, y) + return + } + } else { + m.dActivateTime = nowMilli + } + } + if m.activate != nil { + m.activate(x, y) + } else { + err = &dbus.ErrMsgUnknownMethod + } + return +} + +func (m *UnimplementedStatusNotifierItem) SecondaryActivate(x int32, y int32) (err *dbus.Error) { + // remember last click coordinates for secondary activation as well + lastClickMu.Lock() + lastClickX, lastClickY = x, y + lastClickMu.Unlock() + if m.secondaryActivate != nil { + m.secondaryActivate(x, y) + } else { + err = &dbus.ErrMsgUnknownMethod + } + return +} + +func (m *UnimplementedStatusNotifierItem) Scroll(delta int32, orientation string) (err *dbus.Error) { + if m.scroll != nil { + m.scroll(delta, orientation) + } else { + err = &dbus.ErrMsgUnknownMethod + } + return +} + +func setOnClick(fn func(menu IMenu)) { usni.activate = func(int32, int32) { fn(nil) } } +func setOnDClick(fn func(menu IMenu)) { usni.dActivate = func(int32, int32) { fn(nil) } } +func setOnRClick(dClick func(IMenu)) {} + +// GetLastClickXY returns the last click coordinates captured from Activate/ContextMenu/SecondaryActivate. +func GetLastClickXY() (int32, int32) { + lastClickMu.Lock() + x, y := lastClickX, lastClickY + lastClickMu.Unlock() + return x, y +} + +func nativeStart() { + if systrayReady != nil { + systrayReady() + } + conn, _ := dbus.ConnectSessionBus() + if err := notifier.ExportStatusNotifierItem(conn, path, usni); err != nil { + log.Printf("systray error: failed to export status notifier item: %s\n", err) + } + if err := menu.ExportDbusmenu(conn, menuPath, instance); err != nil { + log.Printf("systray error: failed to export status notifier item: %s\n", err) + } + + name := fmt.Sprintf("org.kde.StatusNotifierItem-%d-1", os.Getpid()) // id 1 for this process + if _, err := conn.RequestName(name, dbus.NameFlagDoNotQueue); err != nil { + log.Printf("systray error: failed to request name: %s\n", err) + } + // store requested bus name for later immediate release on dispose + instance.lock.Lock() + instance.name = name + instance.lock.Unlock() + props, err := prop.Export(conn, path, instance.createPropSpec()) + if err != nil { + log.Printf("systray error: failed to export notifier item properties to bus: %s\n", err) + return + } + menuProps, err := prop.Export(conn, menuPath, createMenuPropSpec()) + if err != nil { + log.Printf("systray error: failed to export notifier menu properties to bus: %s\n", err) + return + } + + node := introspect.Node{ + Name: path, + Interfaces: []introspect.Interface{ + introspect.IntrospectData, + prop.IntrospectData, + notifier.IntrospectDataStatusNotifierItem, + }, + } + if err := conn.Export(introspect.NewIntrospectable(&node), path, "org.freedesktop.DBus.Introspectable"); err != nil { + log.Printf("systray error: failed to export node introspection: %s\n", err) + return + } + + menuNode := introspect.Node{ + Name: menuPath, + Interfaces: []introspect.Interface{ + introspect.IntrospectData, + prop.IntrospectData, + menu.IntrospectDataDbusmenu, + }, + } + if err := conn.Export(introspect.NewIntrospectable(&menuNode), menuPath, "org.freedesktop.DBus.Introspectable"); err != nil { + log.Printf("systray error: failed to export menu node introspection: %s\n", err) + return + } + + instance.lock.Lock() + instance.conn = conn + instance.props = props + instance.menuProps = menuProps + instance.lock.Unlock() + + obj := conn.Object("org.kde.StatusNotifierWatcher", "/StatusNotifierWatcher") + call := obj.Call("org.kde.StatusNotifierWatcher.RegisterStatusNotifierItem", 0, path) + if call.Err != nil { + log.Printf("systray error: failed to register our icon with the notifier watcher (maybe no tray is running?): %s\n", call.Err) + } +} + +// tray holds DBus state +type tray struct { + conn *dbus.Conn + name string + iconData []byte + title string + tooltipTitle string + lock sync.Mutex + menu *menuLayout + menuLock sync.RWMutex + props *prop.Properties + menuProps *prop.Properties + menuVersion uint32 +} + +func (*tray) iface() string { return notifier.InterfaceStatusNotifierItem } + +func (t *tray) createPropSpec() map[string]map[string]*prop.Prop { + t.lock.Lock() + icon := t.iconData + title := t.title + tooltipTitle := t.tooltipTitle + t.lock.Unlock() + + return map[string]map[string]*prop.Prop{ + "org.kde.StatusNotifierItem": { + "Status": {Value: "Active", Writable: false, Emit: prop.EmitTrue}, + "Title": {Value: title, Writable: true, Emit: prop.EmitTrue}, + "Id": {Value: "1", Writable: false, Emit: prop.EmitTrue}, + "Category": {Value: "ApplicationStatus", Writable: false, Emit: prop.EmitTrue}, + "IconName": {Value: "", Writable: false, Emit: prop.EmitTrue}, + "IconPixmap": {Value: buildPixmaps(icon), Writable: true, Emit: prop.EmitTrue}, + "IconThemePath": {Value: "", Writable: false, Emit: prop.EmitTrue}, + "ItemIsMenu": {Value: true, Writable: false, Emit: prop.EmitTrue}, + "Menu": {Value: dbus.ObjectPath(menuPath), Writable: true, Emit: prop.EmitTrue}, + "ToolTip": {Value: tooltip{V2: tooltipTitle, V1: buildPixmaps(icon)}, Writable: true, Emit: prop.EmitTrue}, + }, + } +} + +// PX is a pixmap with width, height and ARGB32 big-endian data +type PX struct { + W, H int + Pix []byte +} + +// tooltip maps the SNI tooltip struct (name, icons, title, description) +type tooltip = struct { + V0 string // name + V1 []PX // icons + V2 string // title + V3 string // description +} + +// buildPixmaps decodes the source icon and produces multiple high-quality +// resized pixmaps encoded as ARGB32 big-endian. +func buildPixmaps(data []byte) []PX { + if len(data) == 0 { + return nil + } + img, _, err := image.Decode(bytes.NewReader(data)) + if err != nil { + log.Printf("systray: failed to decode icon: %v", err) + return nil + } + + // Common tray sizes across environments (match C++ behavior of providing multiple sizes) + targets := []int{16, 22, 24, 32, 48, 64, 128} + + out := make([]PX, 0, len(targets)) + for _, s := range targets { + pm := resizeTo(img, s, s) + out = append(out, PX{W: s, H: s, Pix: toARGB32BigEndian(pm)}) + } + return out +} + +// resizeTo resizes with CatmullRom for crisp yet smooth icons. +func resizeTo(src image.Image, w, h int) *image.RGBA { + dst := image.NewRGBA(image.Rect(0, 0, w, h)) + draw.CatmullRom.Scale(dst, dst.Bounds(), src, src.Bounds(), draw.Over, nil) + return dst +} + +// toARGB32BigEndian converts *image.RGBA to a byte slice of ARGB32 in big-endian +// order (A, R, G, B per byte) as required by SNI over D-Bus. +// This mirrors the C++ implementation that enforces big-endian layout. :contentReference[oaicite:4]{index=4} +func toARGB32BigEndian(img *image.RGBA) []byte { + w, h := img.Bounds().Dx(), img.Bounds().Dy() + buf := make([]byte, w*h*4) + + i := 0 + for y := 0; y < h; y++ { + off := img.PixOffset(0, y) + row := img.Pix[off : off+w*4] + for x := 0; x < w; x++ { + r := row[x*4+0] + g := row[x*4+1] + b := row[x*4+2] + a := row[x*4+3] + + // 0xAARRGGBB big-endian -> bytes [AA][RR][GG][BB] + buf[i+0] = a + buf[i+1] = r + buf[i+2] = g + buf[i+3] = b + i += 4 + } + } + return buf +} + + +// noMenuPathForEnvironment returns the DBus object path to advertise when there is NO menu. +// - GNOME: "/" +// - Others (including KDE/Plasma): "/NO_DBUSMENU" +// Simple detection via environment variables. +func noMenuPathForEnvironment() dbus.ObjectPath { + xdg := strings.ToLower(os.Getenv("XDG_CURRENT_DESKTOP")) + sess := strings.ToLower(os.Getenv("DESKTOP_SESSION")) + if strings.Contains(xdg, "gnome") || strings.Contains(sess, "gnome") { + return dbus.ObjectPath("/") + } + return dbus.ObjectPath("/NO_DBUSMENU") +} + +// setMenuPropTo updates the StatusNotifierItem "Menu" property to the given path. +func setMenuPropTo(p dbus.ObjectPath) { + instance.lock.Lock() + props := instance.props + instance.lock.Unlock() + if props == nil { + return + } + if dbusErr := props.Set("org.kde.StatusNotifierItem", "Menu", dbus.MakeVariant(p)); dbusErr != nil { + log.Printf("systray error: failed to set Menu prop: %s\n", dbusErr) + } +} diff --git a/maclib/MacTrayBridge.m b/src/native/macos/MacTrayBridge.m similarity index 100% rename from maclib/MacTrayBridge.m rename to src/native/macos/MacTrayBridge.m diff --git a/maclib/build.sh b/src/native/macos/build.sh similarity index 95% rename from maclib/build.sh rename to src/native/macos/build.sh index 64c4e027..5266f5e9 100755 --- a/maclib/build.sh +++ b/src/native/macos/build.sh @@ -6,7 +6,7 @@ set -e echo "Building MacTray library (Swift + JNI bridge)..." SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" -OUTPUT_DIR="${NATIVE_LIBS_OUTPUT_DIR:-$SCRIPT_DIR/../src/commonMain/resources}" +OUTPUT_DIR="${NATIVE_LIBS_OUTPUT_DIR:-$SCRIPT_DIR/../../jvmMain/resources/composetray/native}" echo "Output dir for mac is: $OUTPUT_DIR" # Detect JAVA_HOME diff --git a/maclib/tray.h b/src/native/macos/tray.h similarity index 100% rename from maclib/tray.h rename to src/native/macos/tray.h diff --git a/maclib/tray.swift b/src/native/macos/tray.swift similarity index 100% rename from maclib/tray.swift rename to src/native/macos/tray.swift diff --git a/src/native/windows/CMakeLists.txt b/src/native/windows/CMakeLists.txt new file mode 100644 index 00000000..ad54c013 --- /dev/null +++ b/src/native/windows/CMakeLists.txt @@ -0,0 +1,79 @@ +cmake_minimum_required(VERSION 3.13 FATAL_ERROR) + +project(tray LANGUAGES C CXX) + +# Define the C++ standard +set(CMAKE_CXX_STANDARD 17) + +# Allow overriding the output base directory via environment variable +# (used by CI to write artifacts to build/nativeLibs instead of src/jvmMain/resources) +if(DEFINED ENV{NATIVE_LIBS_OUTPUT_DIR}) + set(BASE_OUTPUT_DIR "$ENV{NATIVE_LIBS_OUTPUT_DIR}") +else() + set(BASE_OUTPUT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../jvmMain/resources/composetray/native") +endif() + +# Check target architecture +if(CMAKE_GENERATOR_PLATFORM STREQUAL "x64" OR CMAKE_GENERATOR_PLATFORM STREQUAL "") + set(TARGET_ARCH "x64") + set(OUTPUT_DIR "${BASE_OUTPUT_DIR}/win32-x86-64") + add_compile_options("/arch:AVX2") +elseif(CMAKE_GENERATOR_PLATFORM STREQUAL "ARM64") + set(TARGET_ARCH "ARM64") + set(OUTPUT_DIR "${BASE_OUTPUT_DIR}/win32-arm64") + add_compile_options("/arch:arm64") +else() + message(FATAL_ERROR "Unsupported architecture: ${CMAKE_GENERATOR_PLATFORM}") +endif() + +# Ensure output directory exists +file(MAKE_DIRECTORY ${OUTPUT_DIR}) + +# Add sources for libtray +list(APPEND SRCS ${CMAKE_CURRENT_SOURCE_DIR}/tray_windows.c) + +# Create the shared library +add_library(tray SHARED ${SRCS}) +set_property(TARGET tray PROPERTY C_STANDARD 99) +target_compile_definitions(tray PRIVATE TRAY_EXPORTS) +set_target_properties(tray PROPERTIES C_VISIBILITY_PRESET hidden) + +# Set necessary preprocessor definitions +target_compile_definitions(tray PRIVATE TRAY_WINAPI=1 WIN32_LEAN_AND_MEAN NOMINMAX) + +# Link to Shell32.lib for Windows platform +if(WIN32) + target_link_libraries(tray PRIVATE shell32) +endif() + +# Define the installation path +INSTALL(TARGETS tray DESTINATION lib) + +IF (NOT WIN32) + INSTALL(FILES tray.h DESTINATION include) +ENDIF () + +# Ensure the output directory exists for the architecture +message(STATUS "Target architecture: ${TARGET_ARCH}") +message(STATUS "Output directory: ${OUTPUT_DIR}") + +# Ensure the output directory exists +file(MAKE_DIRECTORY ${OUTPUT_DIR}) + +# Post-build step to copy DLL +add_custom_command(TARGET tray POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy $ ${OUTPUT_DIR}/tray.dll) + +# Set MinSizeRel options to reduce size +set(CMAKE_BUILD_TYPE MinSizeRel) + +# Add any further optimizations for MinSizeRel +if(CMAKE_BUILD_TYPE STREQUAL "MinSizeRel") + target_compile_options(tray PRIVATE "/O1") # Optimize for size + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /OPT:REF /OPT:ICF") # Remove unused code and merge identical functions +endif() + +# Ajout de l'exemple en français +add_executable(exemple ${CMAKE_CURRENT_SOURCE_DIR}/example.c) +target_link_libraries(exemple PRIVATE tray) +set_property(TARGET exemple PROPERTY C_STANDARD 99) diff --git a/src/native/windows/build.bat b/src/native/windows/build.bat new file mode 100644 index 00000000..c35b23b3 --- /dev/null +++ b/src/native/windows/build.bat @@ -0,0 +1,40 @@ +@echo off +echo === Starting compilation for x64 and ARM64 in MinSizeRel mode === + +echo. +echo === x64 Configuration (MinSizeRel) === +cmake -B build-x64 -A x64 -DCMAKE_BUILD_TYPE=MinSizeRel . +if %ERRORLEVEL% neq 0 ( + echo Error during x64 configuration + exit /b %ERRORLEVEL% +) + +echo. +echo === x64 Compilation (MinSizeRel) === +cmake --build build-x64 --config MinSizeRel +if %ERRORLEVEL% neq 0 ( + echo Error during x64 compilation + exit /b %ERRORLEVEL% +) + +echo. +echo === ARM64 Configuration (MinSizeRel) === +cmake -B build-arm64 -A ARM64 -DCMAKE_BUILD_TYPE=MinSizeRel . +if %ERRORLEVEL% neq 0 ( + echo Error during ARM64 configuration + exit /b %ERRORLEVEL% +) + +echo. +echo === ARM64 Compilation (MinSizeRel) === +cmake --build build-arm64 --config MinSizeRel +if %ERRORLEVEL% neq 0 ( + echo Error during ARM64 compilation + exit /b %ERRORLEVEL% +) + +echo. +echo === Compilation completed successfully for both architectures in MinSizeRel mode === +echo. +echo x64 DLL: ..\..\jvmMain\resources\composetray\native\win32-x86-64\tray.dll +echo ARM64 DLL: ..\..\jvmMain\resources\composetray\native\win32-arm64\tray.dll diff --git a/src/native/windows/tray.h b/src/native/windows/tray.h new file mode 100644 index 00000000..d9008dc4 --- /dev/null +++ b/src/native/windows/tray.h @@ -0,0 +1,66 @@ +/* tray.h + * Public API – remains C99/C++98 compatible + */ +#ifndef TRAY_H +#define TRAY_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* -------------------------------------------------------------------------- */ +/* Export */ +/* -------------------------------------------------------------------------- */ +#ifdef _WIN32 +# ifdef TRAY_EXPORTS +# define TRAY_EXPORT __declspec(dllexport) +# else +# define TRAY_EXPORT __declspec(dllimport) +# endif +#else +# if __GNUC__ >= 4 || defined(__clang__) +# define TRAY_EXPORT extern __attribute__((visibility("default"))) +# else +# define TRAY_EXPORT extern +# endif +#endif + +/* -------------------------------------------------------------------------- */ +/* Structures */ +/* -------------------------------------------------------------------------- */ +struct tray_menu_item; + +struct tray { + const char *icon_filepath; /* Path to the .ico icon */ + const char *tooltip; /* Tooltip text */ + void (*cb)(struct tray *); /* Left-click callback (NULL → menu) */ + struct tray_menu_item *menu; /* Root menu */ +}; + +struct tray_menu_item { + char *text; + char *icon_path; // Path to icon file (PNG, ICO, etc.) + int disabled; + int checked; + void (*cb)(struct tray_menu_item *); + struct tray_menu_item *submenu; +}; + +/* -------------------------------------------------------------------------- */ +/* API */ +/* -------------------------------------------------------------------------- */ +TRAY_EXPORT struct tray *tray_get_instance(void); + +TRAY_EXPORT int tray_init (struct tray *tray); +TRAY_EXPORT int tray_loop (int blocking); /* 0 = still running, -1 = finished */ +TRAY_EXPORT void tray_update(struct tray *tray); /* Refresh menu/info */ +TRAY_EXPORT void tray_exit (void); /* Free all resources */ + +/* Notification area information */ +TRAY_EXPORT int tray_get_notification_icons_position(int *x, int *y); +TRAY_EXPORT const char *tray_get_notification_icons_region(void); + +#ifdef __cplusplus +} /* extern "C" */ +#endif +#endif /* TRAY_H */ \ No newline at end of file diff --git a/src/native/windows/tray_windows.c b/src/native/windows/tray_windows.c new file mode 100644 index 00000000..9cb1a6e7 --- /dev/null +++ b/src/native/windows/tray_windows.c @@ -0,0 +1,753 @@ +/* tray.c - Windows implementation with full Unicode support */ +#define COBJMACROS +#define UNICODE +#define _UNICODE +#include +#include +#include +#include +#include +#include "tray.h" + +/* -------------------------------------------------------------------------- */ +/* Helpers: opt-in dark mode */ +/* -------------------------------------------------------------------------- */ +typedef enum { + AppMode_Default, + AppMode_AllowDark, + AppMode_ForceDark, + AppMode_ForceLight, + AppMode_Max +} PreferredAppMode; + +static void tray_enable_dark_mode(void) +{ + HMODULE hUx = LoadLibraryW(L"uxtheme.dll"); + if (!hUx) return; + + typedef PreferredAppMode (WINAPI *SetPreferredAppMode_t)(PreferredAppMode); + SetPreferredAppMode_t SetPreferredAppMode = + (SetPreferredAppMode_t)GetProcAddress(hUx, MAKEINTRESOURCEA(135)); + if (SetPreferredAppMode) + SetPreferredAppMode(AppMode_AllowDark); +} + +/* -------------------------------------------------------------------------- */ +/* UTF-8 to UTF-16 conversion helper */ +/* -------------------------------------------------------------------------- */ +static LPWSTR utf8_to_wide(const char *utf8_str) +{ + if (!utf8_str || !*utf8_str) return NULL; + + int wlen = MultiByteToWideChar(CP_UTF8, 0, utf8_str, -1, NULL, 0); + if (wlen <= 0) return NULL; + + LPWSTR wide_str = (LPWSTR)malloc(wlen * sizeof(WCHAR)); + if (!wide_str) return NULL; + + MultiByteToWideChar(CP_UTF8, 0, utf8_str, -1, wide_str, wlen); + return wide_str; +} + +/* -------------------------------------------------------------------------- */ +/* UTF-16 to UTF-8 conversion helper */ +/* -------------------------------------------------------------------------- */ +static char* wide_to_utf8(const WCHAR *wide_str) +{ + if (!wide_str || !*wide_str) return NULL; + + int utf8_len = WideCharToMultiByte(CP_UTF8, 0, wide_str, -1, NULL, 0, NULL, NULL); + if (utf8_len <= 0) return NULL; + + char *utf8_str = (char*)malloc(utf8_len); + if (!utf8_str) return NULL; + + WideCharToMultiByte(CP_UTF8, 0, wide_str, -1, utf8_str, utf8_len, NULL, NULL); + return utf8_str; +} + +/* -------------------------------------------------------------------------- */ +/* Internal constants */ +/* -------------------------------------------------------------------------- */ +#define WM_TRAY_CALLBACK_MESSAGE (WM_USER + 1) +#define WC_TRAY_CLASS_NAME L"TRAY" +#define ID_TRAY_FIRST 1000 + +/* -------------------------------------------------------------------------- */ +/* Internal variables */ +/* -------------------------------------------------------------------------- */ +static struct tray *tray_instance = NULL; /* unused in multi-instance mode */ +static WNDCLASSEXW wc = {0}; +static NOTIFYICONDATAW nid = {0}; /* unused in multi-instance mode */ +static HWND hwnd = NULL; /* unused in multi-instance mode */ +static HMENU hmenu = NULL; /* unused in multi-instance mode */ +static UINT wm_taskbarcreated; +static BOOL exit_called = FALSE; /* unused in multi-instance mode */ +static CRITICAL_SECTION tray_cs; +static BOOL cs_initialized = FALSE; + +/* Multi-instance support: one context per tray */ +typedef struct TrayContext { + struct tray *tray; /* public tray pointer (key) */ + HWND hwnd; /* hidden window for messages */ + HMENU hmenu; /* root menu */ + NOTIFYICONDATAW nid; /* per-icon notify data */ + UINT uID; /* unique id for Shell_NotifyIcon */ + DWORD threadId; /* thread that owns this context */ + BOOL exiting; /* exit requested for this context */ + struct TrayContext *next; /* linked list */ +} TrayContext; + +static TrayContext *g_ctx_head = NULL; +static UINT g_next_uid = ID_TRAY_FIRST; + +static void ensure_critical_section(void); +static TrayContext* find_ctx_by_tray(struct tray *t); +static TrayContext* find_ctx_by_hwnd(HWND h); +static TrayContext* find_ctx_by_uid(UINT uid); +static TrayContext* find_ctx_by_thread(DWORD tid); +static TrayContext* create_ctx(struct tray *t); +static void destroy_ctx(TrayContext *ctx); + +/* -------------------------------------------------------------------------- */ +/* Internal prototypes */ +/* -------------------------------------------------------------------------- */ +static HMENU tray_menu_item(struct tray_menu_item *m, UINT *id); +static void ensure_critical_section(void); +static HBITMAP load_icon_bitmap(const char *icon_path); + +/* -------------------------------------------------------------------------- */ +/* Critical section helper */ +/* -------------------------------------------------------------------------- */ +static void ensure_critical_section(void) +{ + if (!cs_initialized) { + InitializeCriticalSection(&tray_cs); + cs_initialized = TRUE; + } +} + +/* -------------------------------------------------------------------------- */ +/* Context helpers */ +/* -------------------------------------------------------------------------- */ +static TrayContext* find_ctx_by_tray(struct tray *t) +{ + TrayContext *p = g_ctx_head; + while (p) { + if (p->tray == t) return p; + p = p->next; + } + return NULL; +} + +static TrayContext* find_ctx_by_hwnd(HWND h) +{ + TrayContext *p = g_ctx_head; + while (p) { + if (p->hwnd == h) return p; + p = p->next; + } + return NULL; +} + +static TrayContext* find_ctx_by_uid(UINT uid) +{ + TrayContext *p = g_ctx_head; + while (p) { + if (p->uID == uid) return p; + p = p->next; + } + return NULL; +} + +static TrayContext* find_ctx_by_thread(DWORD tid) +{ + TrayContext *p = g_ctx_head; + while (p) { + if (p->threadId == tid) return p; + p = p->next; + } + return NULL; +} + +static TrayContext* create_ctx(struct tray *t) +{ + TrayContext *ctx = (TrayContext*)calloc(1, sizeof(TrayContext)); + if (!ctx) return NULL; + ctx->tray = t; + ctx->hwnd = NULL; + ctx->hmenu = NULL; + ZeroMemory(&ctx->nid, sizeof(ctx->nid)); + ctx->uID = g_next_uid++; + ctx->threadId = GetCurrentThreadId(); + ctx->exiting = FALSE; + + /* Insert at head */ + ctx->next = g_ctx_head; + g_ctx_head = ctx; + return ctx; +} + +static void destroy_ctx(TrayContext *ctx) +{ + if (!ctx) return; + + /* Remove from list */ + if (g_ctx_head == ctx) { + g_ctx_head = ctx->next; + } else { + TrayContext *prev = g_ctx_head; + while (prev && prev->next != ctx) prev = prev->next; + if (prev) prev->next = ctx->next; + } + + /* Free menu */ + if (ctx->hmenu) { + MENUITEMINFOW item = {0}; + item.cbSize = sizeof(item); + item.fMask = MIIM_BITMAP; + int count = GetMenuItemCount(ctx->hmenu); + for (int i = 0; i < count; i++) { + if (GetMenuItemInfoW(ctx->hmenu, i, TRUE, &item)) { + if (item.hbmpItem && item.hbmpItem != HBMMENU_CALLBACK) { + DeleteObject(item.hbmpItem); + } + } + } + DestroyMenu(ctx->hmenu); + ctx->hmenu = NULL; + } + + /* Destroy window */ + if (ctx->hwnd) { + DestroyWindow(ctx->hwnd); + ctx->hwnd = NULL; + } + + /* Free icon handle if any */ + if (ctx->nid.hIcon) { + DestroyIcon(ctx->nid.hIcon); + ctx->nid.hIcon = NULL; + } + + free(ctx); +} + +/* ------------------------------------------------------------------ */ +/* Safe conversion of an HICON to ARGB 32-bit bitmap */ +/* ------------------------------------------------------------------ */ +static HBITMAP bitmap_from_icon(HICON hIcon, int cx, int cy) +{ + if (!hIcon) return NULL; + + BITMAPINFO bi = {0}; + bi.bmiHeader.biSize = sizeof(bi.bmiHeader); + bi.bmiHeader.biWidth = cx; + bi.bmiHeader.biHeight = -cy; /* top-down orientation */ + bi.bmiHeader.biPlanes = 1; + bi.bmiHeader.biBitCount = 32; /* BGRA */ + bi.bmiHeader.biCompression = BI_RGB; + + void *bits = NULL; + HDC hdc = GetDC(NULL); + HBITMAP hbmp = CreateDIBSection(hdc, &bi, DIB_RGB_COLORS, &bits, NULL, 0); + if (hbmp) { + HDC hdcMem = CreateCompatibleDC(hdc); + HBITMAP hold = (HBITMAP)SelectObject(hdcMem, hbmp); + + DrawIconEx(hdcMem, 0, 0, hIcon, cx, cy, 0, NULL, DI_NORMAL); + + SelectObject(hdcMem, hold); + DeleteDC(hdcMem); + } + ReleaseDC(NULL, hdc); + return hbmp; +} + +/* ------------------------------------------------------------------ */ +/* Generic loading of icon/bitmap from disk → ARGB bitmap */ +/* ------------------------------------------------------------------ */ +static HBITMAP load_icon_bitmap(const char *icon_path) +{ + if (!icon_path || !*icon_path) return NULL; + + /* Convert UTF-8 path to Wide */ + LPWSTR wpath = utf8_to_wide(icon_path); + if (!wpath) return NULL; + + /* 1st: try direct .bmp/.png as 32-bit DIB */ + HBITMAP hbmp = (HBITMAP)LoadImageW( + NULL, wpath, + IMAGE_BITMAP, + 16, 16, + LR_LOADFROMFILE | LR_CREATEDIBSECTION | LR_DEFAULTSIZE + ); + if (hbmp) { + free(wpath); + return hbmp; + } + + /* 2nd: try .ico → ARGB conversion */ + HICON hIcon = (HICON)LoadImageW( + NULL, wpath, + IMAGE_ICON, + 16, 16, + LR_LOADFROMFILE | LR_DEFAULTSIZE + ); + free(wpath); + + if (hIcon) { + hbmp = bitmap_from_icon(hIcon, 16, 16); + DestroyIcon(hIcon); + } + return hbmp; +} + +/* -------------------------------------------------------------------------- */ +/* Invisible window procedure */ +/* -------------------------------------------------------------------------- */ +static LRESULT CALLBACK tray_wnd_proc(HWND h, UINT msg, WPARAM w, LPARAM l) +{ + TrayContext *ctx = find_ctx_by_hwnd(h); + switch (msg) + { + case WM_CLOSE: + DestroyWindow(h); + return 0; + + case WM_DESTROY: + PostQuitMessage(0); + return 0; + + case WM_TRAY_CALLBACK_MESSAGE: + if (l == WM_LBUTTONUP && ctx && ctx->tray && ctx->tray->cb) { + EnterCriticalSection(&tray_cs); + if (ctx && ctx->tray && ctx->tray->cb) { + ctx->tray->cb(ctx->tray); + } + LeaveCriticalSection(&tray_cs); + return 0; + } + if (l == WM_LBUTTONUP || l == WM_RBUTTONUP) { + POINT p; + GetCursorPos(&p); + SetForegroundWindow(h); + + EnterCriticalSection(&tray_cs); + if (ctx && ctx->hmenu) { + WORD cmd = TrackPopupMenu(ctx->hmenu, + TPM_LEFTALIGN | TPM_RIGHTBUTTON | + TPM_RETURNCMD | TPM_NONOTIFY, + p.x, p.y, 0, h, NULL); + SendMessage(h, WM_COMMAND, cmd, 0); + } + LeaveCriticalSection(&tray_cs); + return 0; + } + break; + + case WM_COMMAND: + if (w >= ID_TRAY_FIRST) { + EnterCriticalSection(&tray_cs); + if (ctx && ctx->hmenu) { + MENUITEMINFOW item = { 0 }; + item.cbSize = sizeof(item); + item.fMask = MIIM_ID | MIIM_DATA; + if (GetMenuItemInfoW(ctx->hmenu, (UINT)w, FALSE, &item)) { + struct tray_menu_item *mi = (struct tray_menu_item *)item.dwItemData; + if (mi && mi->cb) mi->cb(mi); + } + } + LeaveCriticalSection(&tray_cs); + return 0; + } + break; + + default: + if (msg == wm_taskbarcreated) { + if (ctx) { + Shell_NotifyIconW(NIM_ADD, &ctx->nid); + } + return 0; + } + } + return DefWindowProcW(h, msg, w, l); +} + +/* -------------------------------------------------------------------------- */ +/* Recursive HMENU construction with safe icon support */ +/* -------------------------------------------------------------------------- */ +static HMENU tray_menu_item(struct tray_menu_item *m, UINT *id) +{ + HMENU menu = CreatePopupMenu(); + if (!menu) return NULL; + + for (; m && m->text; ++m) { + + /* Separator "-" */ + if (strcmp(m->text, "-") == 0) { + AppendMenuW(menu, MF_SEPARATOR, 0, NULL); + continue; + } + + /* Normal item (text + optional icon + submenu) */ + MENUITEMINFOW info; + ZeroMemory(&info, sizeof(info)); + info.cbSize = sizeof(info); + + /* Convert UTF-8 text to Wide */ + LPWSTR wtext = utf8_to_wide(m->text); + if (!wtext) continue; + + /* Text: MIIM_STRING + MFT_STRING instead of MIIM_TYPE */ + info.fMask = MIIM_ID | MIIM_STRING | MIIM_STATE | + MIIM_FTYPE | MIIM_DATA; + info.fType = MFT_STRING; + info.dwTypeData = wtext; + info.cch = (UINT)wcslen(wtext); + + /* Unique identifier */ + info.wID = (*id)++; + info.dwItemData = (ULONG_PTR)m; + + /* Optional submenu */ + if (m->submenu) { + info.fMask |= MIIM_SUBMENU; + info.hSubMenu = tray_menu_item(m->submenu, id); + } + + /* State (disabled / checked) */ + if (m->disabled) info.fState |= MFS_DISABLED; + if (m->checked) info.fState |= MFS_CHECKED; + + /* Optional icon */ + if (m->icon_path && *m->icon_path) { + HBITMAP hBmp = load_icon_bitmap(m->icon_path); + if (hBmp) { + info.fMask |= MIIM_BITMAP; + info.hbmpItem = hBmp; + } + } + + /* Append at end of menu to avoid out-of-range indexes */ + InsertMenuItemW(menu, (UINT)-1, TRUE, &info); + + free(wtext); + } + return menu; +} + +/* -------------------------------------------------------------------------- */ +/* Public API */ +/* -------------------------------------------------------------------------- */ +struct tray *tray_get_instance(void) { + ensure_critical_section(); + EnterCriticalSection(&tray_cs); + TrayContext *ctx = find_ctx_by_thread(GetCurrentThreadId()); + if (!ctx) ctx = g_ctx_head; /* fallback */ + struct tray *t = ctx ? ctx->tray : NULL; + LeaveCriticalSection(&tray_cs); + return t; +} + +/* -------------------------------------------------------------------------- */ +/* Initializes the tray icon and creates the hidden message window */ +/* -------------------------------------------------------------------------- */ +int tray_init(struct tray *tray) +{ + if (!tray) return -1; + + ensure_critical_section(); + + /* Wrap in SEH: tray_enable_dark_mode uses undocumented ordinal 135 of + uxtheme.dll which may cause an access violation on some Windows 10 builds. */ + __try { + tray_enable_dark_mode(); + } __except(EXCEPTION_EXECUTE_HANDLER) { + /* Silently ignore – dark-mode theming is cosmetic only. */ + } + wm_taskbarcreated = RegisterWindowMessageW(L"TaskbarCreated"); + + // Register (ignore if the class already exists) + ZeroMemory(&wc, sizeof(wc)); + wc.cbSize = sizeof(wc); + wc.lpfnWndProc = tray_wnd_proc; + wc.hInstance = GetModuleHandleW(NULL); + wc.lpszClassName = WC_TRAY_CLASS_NAME; + if (!RegisterClassExW(&wc) && GetLastError() != ERROR_CLASS_ALREADY_EXISTS) + return -1; + + EnterCriticalSection(&tray_cs); + TrayContext *ctx = find_ctx_by_tray(tray); + if (!ctx) ctx = create_ctx(tray); + LeaveCriticalSection(&tray_cs); + if (!ctx) return -1; + + ctx->hwnd = CreateWindowExW( + 0, + WC_TRAY_CLASS_NAME, + NULL, + 0, + 0, 0, 0, 0, + 0, + 0, + GetModuleHandleW(NULL), + NULL); + if (!ctx->hwnd) return -1; + + ZeroMemory(&ctx->nid, sizeof(ctx->nid)); + ctx->nid.cbSize = sizeof(ctx->nid); + ctx->nid.hWnd = ctx->hwnd; + ctx->nid.uID = ctx->uID; + ctx->nid.uFlags = NIF_ICON | NIF_MESSAGE; + ctx->nid.uCallbackMessage = WM_TRAY_CALLBACK_MESSAGE; + Shell_NotifyIconW(NIM_ADD, &ctx->nid); + + tray_update(tray); + return 0; +} + +/* Message loop: blocking = 1 -> GetMessage, 0 -> PeekMessage */ +int tray_loop(int blocking) +{ + MSG msg; + + /* Ensure there is at least one context for this thread */ + DWORD tid = GetCurrentThreadId(); + EnterCriticalSection(&tray_cs); + TrayContext *ctx = find_ctx_by_thread(tid); + LeaveCriticalSection(&tray_cs); + if (!ctx) return -1; + + if (blocking) { + int ret = GetMessageW(&msg, NULL, 0, 0); + if (ret == 0) { + return -1; /* WM_QUIT */ + } else if (ret < 0) { + return -1; /* Error */ + } + } else { + if (!PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE)) + return 0; + } + + if (msg.message == WM_QUIT) + return -1; + + TranslateMessage(&msg); + DispatchMessageW(&msg); + return 0; +} + +/* Updates icon, tooltip and menu */ +void tray_update(struct tray *tray) +{ + if (!tray) return; + + ensure_critical_section(); + EnterCriticalSection(&tray_cs); + + /* Prefer thread-local context to tolerate different struct pointers across updates */ + TrayContext *ctx = find_ctx_by_thread(GetCurrentThreadId()); + if (!ctx) ctx = find_ctx_by_tray(tray); + if (!ctx) { LeaveCriticalSection(&tray_cs); return; } + + /* Update pointer to reflect latest struct (callbacks, etc.) */ + ctx->tray = tray; + + // Clean up old menu and its bitmaps + if (ctx->hmenu) { + MENUITEMINFOW item = {0}; + item.cbSize = sizeof(item); + item.fMask = MIIM_BITMAP; + + int count = GetMenuItemCount(ctx->hmenu); + for (int i = 0; i < count; i++) { + if (GetMenuItemInfoW(ctx->hmenu, i, TRUE, &item)) { + if (item.hbmpItem && item.hbmpItem != HBMMENU_CALLBACK) { + DeleteObject(item.hbmpItem); + } + } + } + DestroyMenu(ctx->hmenu); + ctx->hmenu = NULL; + } + + UINT id = ID_TRAY_FIRST; + ctx->hmenu = tray_menu_item(tray->menu, &id); + + /* Icon */ + HICON icon = NULL; + if (tray->icon_filepath && *tray->icon_filepath) { + LPWSTR wpath = utf8_to_wide(tray->icon_filepath); + if (wpath) { + ExtractIconExW(wpath, 0, NULL, &icon, 1); + free(wpath); + } + } + if (ctx->nid.hIcon && ctx->nid.hIcon != icon) { + DestroyIcon(ctx->nid.hIcon); + } + ctx->nid.hIcon = icon; + + /* Tooltip */ + ctx->nid.uFlags = NIF_ICON | NIF_MESSAGE; + if (tray->tooltip && *tray->tooltip) { + LPWSTR wtooltip = utf8_to_wide(tray->tooltip); + if (wtooltip) { + wcsncpy_s(ctx->nid.szTip, sizeof(ctx->nid.szTip)/sizeof(WCHAR), wtooltip, _TRUNCATE); + ctx->nid.uFlags |= NIF_TIP; + free(wtooltip); + } + } + + /* Update the tray */ + Shell_NotifyIconW(NIM_MODIFY, &ctx->nid); + + LeaveCriticalSection(&tray_cs); +} + +/* -------------------------------------------------------------------------- */ +/* Cleanly shuts down and unregisters everything */ +/* -------------------------------------------------------------------------- */ +void tray_exit(void) +{ + ensure_critical_section(); + EnterCriticalSection(&tray_cs); + + TrayContext *ctx = find_ctx_by_thread(GetCurrentThreadId()); + if (!ctx) ctx = g_ctx_head; /* fallback */ + if (!ctx) { + LeaveCriticalSection(&tray_cs); + return; + } + + /* Remove tray icon */ + Shell_NotifyIconW(NIM_DELETE, &ctx->nid); + if (ctx->nid.hIcon) { + DestroyIcon(ctx->nid.hIcon); + ctx->nid.hIcon = NULL; + } + + /* Post WM_QUIT to unblock any blocking GetMessage call and destroy window */ + if (ctx->hwnd) { + PostMessageW(ctx->hwnd, WM_QUIT, 0, 0); + DestroyWindow(ctx->hwnd); + ctx->hwnd = NULL; + } + + /* Destroy context (frees menu bitmaps, etc.) */ + destroy_ctx(ctx); + + /* If no more contexts, unregister class and optionally release critical section */ + if (!g_ctx_head) { + UnregisterClassW(WC_TRAY_CLASS_NAME, GetModuleHandleW(NULL)); + LeaveCriticalSection(&tray_cs); + DeleteCriticalSection(&tray_cs); + cs_initialized = FALSE; + return; + } + + LeaveCriticalSection(&tray_cs); +} + +static BOOL get_tray_icon_rect(RECT *r) +{ + /* Use per-thread context to identify the correct tray icon */ + ensure_critical_section(); + EnterCriticalSection(&tray_cs); + TrayContext *ctx = find_ctx_by_thread(GetCurrentThreadId()); + if (!ctx) ctx = g_ctx_head; /* fallback to first */ + HWND l_hwnd = ctx ? ctx->hwnd : NULL; + UINT l_uid = ctx ? ctx->nid.uID : 0; + LeaveCriticalSection(&tray_cs); + + if (!l_hwnd) return FALSE; + + HMODULE hShell = GetModuleHandleW(L"shell32.dll"); + if (!hShell) return FALSE; + + typedef HRESULT (WINAPI *NIGetRect_t)(const NOTIFYICONIDENTIFIER*, RECT*); + NIGetRect_t pGetRect = (NIGetRect_t)GetProcAddress(hShell, "Shell_NotifyIconGetRect"); + if (!pGetRect) return FALSE; /* OS too old */ + + /* CRITICAL FIX: Properly zero-initialize the entire structure */ + /* This ensures all fields including guidItem are zeroed */ + NOTIFYICONIDENTIFIER nii; + ZeroMemory(&nii, sizeof(nii)); + nii.cbSize = sizeof(nii); + nii.hWnd = l_hwnd; /* owner window */ + nii.uID = l_uid; /* id */ + + /* The guidItem field is now safely zeroed. + * When guidItem is zero (NULL GUID), Windows will use hWnd/uID for identification */ + + return SUCCEEDED(pGetRect(&nii, r)); +} + +/* -------------------------------------------------------------------------- */ +/* Notification area info */ +/* -------------------------------------------------------------------------- */ +int tray_get_notification_icons_position(int *x, int *y) +{ + if (!x || !y) return 0; /* Safety check for null pointers */ + + RECT r = {0}; + BOOL precise = get_tray_icon_rect(&r); /* TRUE if modern API OK */ + + if (precise) { + /* Use the actual icon rectangle to compute a better anchor */ + int cx = (r.left + r.right) / 2; /* center X of the icon */ + int cy = 0; /* anchor Y: bottom for top tray, top for bottom tray */ + + HMONITOR hMon = MonitorFromRect(&r, MONITOR_DEFAULTTOPRIMARY); + MONITORINFO mi = {0}; + mi.cbSize = sizeof(mi); + if (GetMonitorInfoW(hMon, &mi)) { + LONG midY = (mi.rcMonitor.bottom + mi.rcMonitor.top) / 2; + /* If icon is on top half of monitor, anchor below it; otherwise above */ + cy = (r.top < midY) ? r.bottom : r.top; + } else { + /* Fallback if monitor info is unavailable: use bottom as a reasonable default */ + cy = r.bottom; + } + *x = cx; + *y = cy; + return 1; /* precise */ + } else { + /* Fallback: use notification area window rect (top-left) */ + HWND hTray = FindWindowW(L"Shell_TrayWnd", NULL); + HWND hNotif = FindWindowExW(hTray, NULL, L"TrayNotifyWnd", NULL); + if (!hNotif || !GetWindowRect(hNotif, &r)) { + *x = *y = 0; + return 0; /* nothing reliable */ + } + *x = r.left; + *y = r.top; + return 0; /* not precise */ + } +} + +const char *tray_get_notification_icons_region(void) +{ + RECT r; + POINT p = {0, 0}; + HWND hTray = FindWindowW(L"Shell_TrayWnd", NULL); + HWND hNotif = FindWindowExW(hTray, NULL, L"TrayNotifyWnd", NULL); + + if (hNotif && GetWindowRect(hNotif, &r)) { + p.x = r.left; p.y = r.top; + } + + HMONITOR hMon = MonitorFromWindow(hNotif, MONITOR_DEFAULTTOPRIMARY); + MONITORINFO mi = { .cbSize = sizeof(mi) }; + GetMonitorInfoW(hMon, &mi); + + LONG midX = (mi.rcMonitor.right + mi.rcMonitor.left) / 2; + LONG midY = (mi.rcMonitor.bottom + mi.rcMonitor.top) / 2; + + if (p.x < midX && p.y < midY) return "top-left"; + if (p.x >= midX && p.y < midY) return "top-right"; + if (p.x < midX && p.y >= midY) return "bottom-left"; + return "bottom-right"; +} \ No newline at end of file diff --git a/winlib b/winlib deleted file mode 160000 index e28f6a04..00000000 --- a/winlib +++ /dev/null @@ -1 +0,0 @@ -Subproject commit e28f6a049cd4fe0603bbeaf22682e7a5a1ad828c From ba03b592a4c05e93c55dd1bb45a015a43498dc08 Mon Sep 17 00:00:00 2001 From: Elie Gambache Date: Thu, 9 Apr 2026 17:26:18 +0300 Subject: [PATCH 02/12] Add GraalVmInitializer.initialize() call in demo main and graalvm-runtime dependency --- demo/build.gradle.kts | 1 + .../kotlin/com/kdroid/composetray/demo/DynamicTrayMenu.kt | 6 +++++- gradle/libs.versions.toml | 1 + 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/demo/build.gradle.kts b/demo/build.gradle.kts index e8880d7d..74d8e7eb 100644 --- a/demo/build.gradle.kts +++ b/demo/build.gradle.kts @@ -22,6 +22,7 @@ kotlin { implementation(compose.materialIconsExtended) implementation(libs.kermit) implementation(libs.platformtools.darkmodedetector) + implementation(libs.nucleus.graalvm.runtime) } } } diff --git a/demo/src/jvmMain/kotlin/com/kdroid/composetray/demo/DynamicTrayMenu.kt b/demo/src/jvmMain/kotlin/com/kdroid/composetray/demo/DynamicTrayMenu.kt index 41f42786..5352fc9c 100644 --- a/demo/src/jvmMain/kotlin/com/kdroid/composetray/demo/DynamicTrayMenu.kt +++ b/demo/src/jvmMain/kotlin/com/kdroid/composetray/demo/DynamicTrayMenu.kt @@ -8,6 +8,7 @@ import androidx.compose.runtime.setValue import androidx.compose.ui.window.Window import androidx.compose.ui.window.application import com.kdroid.composetray.tray.api.Tray +import io.github.kdroidfilter.nucleus.graalvm.GraalVmInitializer import com.kdroid.composetray.utils.ComposeNativeTrayLoggingLevel import com.kdroid.composetray.utils.SingleInstanceManager import com.kdroid.composetray.utils.allowComposeNativeTrayLogging @@ -26,7 +27,9 @@ private enum class ServiceStatus { RUNNING, STOPPED } -fun main() = application { +fun main() { + GraalVmInitializer.initialize() + application { val logTag = "NativeTray" allowComposeNativeTrayLogging = true composeNativeTrayLoggingLevel = ComposeNativeTrayLoggingLevel.DEBUG @@ -166,4 +169,5 @@ fun main() = application { hideOnClose = hideOnCloseState } } +} } \ No newline at end of file diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index ccac2f54..a36d90ce 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -17,6 +17,7 @@ jna = { module = "net.java.dev.jna:jna-jpms", version.ref = "jna" } jna-platform = { module = "net.java.dev.jna:jna-platform-jpms", version.ref = "jna" } platformtools-core = { module = "io.github.kdroidfilter:platformtools.core", version.ref = "platformtools" } platformtools-darkmodedetector = { module = "io.github.kdroidfilter:platformtools.darkmodedetector", version.ref = "platformtools" } +nucleus-graalvm-runtime = { module = "io.github.kdroidfilter:nucleus.graalvm-runtime", version.ref = "nucleus" } [plugins] multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" } From 00726a761cd2072918d6d56b97f67717c695184a Mon Sep 17 00:00:00 2001 From: Elie Gambache Date: Thu, 9 Apr 2026 17:36:41 +0300 Subject: [PATCH 03/12] Fix native library loading for GraalVM native-image mode --- .../composetray/lib/mac/MacNativeBridge.kt | 38 ++++++++++++------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacNativeBridge.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacNativeBridge.kt index 97b8d295..fcce4142 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacNativeBridge.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacNativeBridge.kt @@ -2,6 +2,8 @@ package com.kdroid.composetray.lib.mac import com.kdroid.composetray.utils.extractToTempIfDifferent import java.io.File +import java.nio.file.Files +import java.nio.file.StandardCopyOption /** * JNI bridge to the native macOS tray library (libMacTray.dylib). @@ -22,22 +24,32 @@ internal object MacNativeBridge { } val resourcePath = "composetray/native/$resourceDir/libMacTray.dylib" - // Try to find the dylib on the classpath (inside a JAR or on disk) + // Try to find the dylib on the classpath (inside a JAR, on disk, or embedded in native-image) val url = MacNativeBridge::class.java.classLoader?.getResource(resourcePath) if (url != null) { - val protocol = url.protocol - if (protocol == "jar") { - // Extract from JAR to a temp file - val tempFile = extractToTempIfDifferent(url.toString()) - if (tempFile != null) { - System.load(tempFile.absolutePath) - return + when (url.protocol) { + "jar" -> { + val tempFile = extractToTempIfDifferent(url.toString()) + if (tempFile != null) { + System.load(tempFile.absolutePath) + return + } } - } else if (protocol == "file") { - // Direct file on disk (development mode) - val file = File(url.toURI()) - if (file.exists()) { - System.load(file.absolutePath) + "file" -> { + val file = File(url.toURI()) + if (file.exists()) { + System.load(file.absolutePath) + return + } + } + else -> { + // GraalVM native-image or other embedded resource: extract via stream + val tempFile = Files.createTempFile("libMacTray", ".dylib").toFile() + tempFile.deleteOnExit() + url.openStream().use { input -> + Files.copy(input, tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING) + } + System.load(tempFile.absolutePath) return } } From ef9541f421895ff675bf86115a146cc73d5836a0 Mon Sep 17 00:00:00 2001 From: Elie Gambache Date: Thu, 9 Apr 2026 17:43:35 +0300 Subject: [PATCH 04/12] Replace platformtools-darkmodedetector with Nucleus darkmode-detector (JNI, no JNA) --- build.gradle.kts | 2 +- demo/build.gradle.kts | 2 +- .../kotlin/com/kdroid/composetray/demo/App.kt | 2 +- .../kdroid/composetray/demo/TrayAppDemo.kt | 7 +- gradle/libs.versions.toml | 2 +- .../composetray/lib/mac/MacNativeBridge.kt | 55 +------- .../menu/impl/MacTrayMenuBuilderImpl.kt | 2 +- .../kdroid/composetray/tray/api/NativeTray.kt | 2 +- .../composetray/utils/DarkModeDetector.kt | 2 +- .../composetray/utils/NativeLibraryLoader.kt | 126 +++++++++++++----- 10 files changed, 103 insertions(+), 99 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 14b2297c..9e740871 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -44,7 +44,7 @@ kotlin { implementation(libs.kotlinx.coroutines.core) implementation(libs.kotlinx.coroutines.swing) implementation(libs.platformtools.core) - implementation(libs.platformtools.darkmodedetector) + implementation(libs.nucleus.darkmode.detector) } } } diff --git a/demo/build.gradle.kts b/demo/build.gradle.kts index 74d8e7eb..afa13154 100644 --- a/demo/build.gradle.kts +++ b/demo/build.gradle.kts @@ -21,7 +21,7 @@ kotlin { implementation(compose.material3) implementation(compose.materialIconsExtended) implementation(libs.kermit) - implementation(libs.platformtools.darkmodedetector) + implementation(libs.nucleus.darkmode.detector) implementation(libs.nucleus.graalvm.runtime) } } diff --git a/demo/src/jvmMain/kotlin/com/kdroid/composetray/demo/App.kt b/demo/src/jvmMain/kotlin/com/kdroid/composetray/demo/App.kt index 5bedf0cd..05068b6d 100644 --- a/demo/src/jvmMain/kotlin/com/kdroid/composetray/demo/App.kt +++ b/demo/src/jvmMain/kotlin/com/kdroid/composetray/demo/App.kt @@ -8,7 +8,7 @@ import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp -import io.github.kdroidfilter.platformtools.darkmodedetector.isSystemInDarkMode +import io.github.kdroidfilter.nucleus.darkmodedetector.isSystemInDarkMode import kotlin.system.exitProcess @OptIn(ExperimentalMaterial3Api::class) diff --git a/demo/src/jvmMain/kotlin/com/kdroid/composetray/demo/TrayAppDemo.kt b/demo/src/jvmMain/kotlin/com/kdroid/composetray/demo/TrayAppDemo.kt index a557caf2..0e6b587a 100644 --- a/demo/src/jvmMain/kotlin/com/kdroid/composetray/demo/TrayAppDemo.kt +++ b/demo/src/jvmMain/kotlin/com/kdroid/composetray/demo/TrayAppDemo.kt @@ -25,16 +25,13 @@ import com.kdroid.composetray.utils.WindowRaise import com.kdroid.composetray.utils.allowComposeNativeTrayLogging import composenativetray.demo.generated.resources.Res import composenativetray.demo.generated.resources.icon -import io.github.kdroidfilter.platformtools.darkmodedetector.isSystemInDarkMode -import io.github.kdroidfilter.platformtools.darkmodedetector.mac.setMacOsAdaptiveTitleBar -import io.github.kdroidfilter.platformtools.darkmodedetector.windows.setWindowsAdaptiveTitleBar +import io.github.kdroidfilter.nucleus.darkmodedetector.isSystemInDarkMode import kotlinx.coroutines.launch import org.jetbrains.compose.resources.painterResource @OptIn(ExperimentalTrayAppApi::class) fun main() { allowComposeNativeTrayLogging = true - setMacOsAdaptiveTitleBar() application { var isWindowVisible by remember { mutableStateOf(true) } val coroutineScope = rememberCoroutineScope() @@ -154,8 +151,6 @@ fun main() { title = "Main App", icon = painterResource(Res.drawable.icon), ) { - window.setWindowsAdaptiveTitleBar() - MaterialTheme( colorScheme = if (isSystemInDarkMode()) darkColorScheme() else lightColorScheme() ) { diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index a36d90ce..36eb9325 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -16,7 +16,7 @@ kotlinx-coroutines-swing = { module = "org.jetbrains.kotlinx:kotlinx-coroutines- jna = { module = "net.java.dev.jna:jna-jpms", version.ref = "jna" } jna-platform = { module = "net.java.dev.jna:jna-platform-jpms", version.ref = "jna" } platformtools-core = { module = "io.github.kdroidfilter:platformtools.core", version.ref = "platformtools" } -platformtools-darkmodedetector = { module = "io.github.kdroidfilter:platformtools.darkmodedetector", version.ref = "platformtools" } +nucleus-darkmode-detector = { module = "io.github.kdroidfilter:nucleus.darkmode-detector", version.ref = "nucleus" } nucleus-graalvm-runtime = { module = "io.github.kdroidfilter:nucleus.graalvm-runtime", version.ref = "nucleus" } [plugins] diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacNativeBridge.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacNativeBridge.kt index fcce4142..0875f324 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacNativeBridge.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacNativeBridge.kt @@ -1,63 +1,16 @@ package com.kdroid.composetray.lib.mac -import com.kdroid.composetray.utils.extractToTempIfDifferent -import java.io.File -import java.nio.file.Files -import java.nio.file.StandardCopyOption +import com.kdroid.composetray.utils.NativeLibraryLoader /** * JNI bridge to the native macOS tray library (libMacTray.dylib). - * Replaces the previous JNA direct-mapping approach. * All methods are static JNI calls into MacTrayBridge.m. */ internal object MacNativeBridge { - init { - loadNativeLibrary() - } - - private fun loadNativeLibrary() { - val arch = System.getProperty("os.arch") ?: "aarch64" - val resourceDir = when { - arch.contains("aarch64") || arch.contains("arm64") -> "darwin-aarch64" - else -> "darwin-x86-64" - } - val resourcePath = "composetray/native/$resourceDir/libMacTray.dylib" - - // Try to find the dylib on the classpath (inside a JAR, on disk, or embedded in native-image) - val url = MacNativeBridge::class.java.classLoader?.getResource(resourcePath) - if (url != null) { - when (url.protocol) { - "jar" -> { - val tempFile = extractToTempIfDifferent(url.toString()) - if (tempFile != null) { - System.load(tempFile.absolutePath) - return - } - } - "file" -> { - val file = File(url.toURI()) - if (file.exists()) { - System.load(file.absolutePath) - return - } - } - else -> { - // GraalVM native-image or other embedded resource: extract via stream - val tempFile = Files.createTempFile("libMacTray", ".dylib").toFile() - tempFile.deleteOnExit() - url.openStream().use { input -> - Files.copy(input, tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING) - } - System.load(tempFile.absolutePath) - return - } - } - } - - // Fallback: let the JVM find it on java.library.path - System.loadLibrary("MacTray") - } + private const val LIBRARY_NAME = "MacTray" + private val loaded = NativeLibraryLoader.load(LIBRARY_NAME, MacNativeBridge::class.java) + val isLoaded: Boolean get() = loaded // ── Tray lifecycle ────────────────────────────────────────────────── diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/menu/impl/MacTrayMenuBuilderImpl.kt b/src/jvmMain/kotlin/com/kdroid/composetray/menu/impl/MacTrayMenuBuilderImpl.kt index d1485022..73ceadfa 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/menu/impl/MacTrayMenuBuilderImpl.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/menu/impl/MacTrayMenuBuilderImpl.kt @@ -13,7 +13,7 @@ import com.kdroid.composetray.menu.api.TrayMenuBuilder import com.kdroid.composetray.utils.ComposableIconUtils import com.kdroid.composetray.utils.IconRenderProperties import com.kdroid.composetray.utils.isMenuBarInDarkMode -import io.github.kdroidfilter.platformtools.darkmodedetector.isSystemInDarkMode +import io.github.kdroidfilter.nucleus.darkmodedetector.isSystemInDarkMode import org.jetbrains.compose.resources.DrawableResource import org.jetbrains.compose.resources.painterResource import java.util.concurrent.locks.ReentrantLock diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/tray/api/NativeTray.kt b/src/jvmMain/kotlin/com/kdroid/composetray/tray/api/NativeTray.kt index 42f40aaf..c4d20d8e 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/tray/api/NativeTray.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/tray/api/NativeTray.kt @@ -20,7 +20,7 @@ import com.kdroid.composetray.tray.impl.MacTrayInitializer import com.kdroid.composetray.tray.impl.WindowsTrayInitializer import com.kdroid.composetray.utils.* import io.github.kdroidfilter.platformtools.OperatingSystem.* -import io.github.kdroidfilter.platformtools.darkmodedetector.isSystemInDarkMode +import io.github.kdroidfilter.nucleus.darkmodedetector.isSystemInDarkMode import io.github.kdroidfilter.platformtools.getOperatingSystem import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/utils/DarkModeDetector.kt b/src/jvmMain/kotlin/com/kdroid/composetray/utils/DarkModeDetector.kt index d19820a3..9cc924d7 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/utils/DarkModeDetector.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/utils/DarkModeDetector.kt @@ -7,7 +7,7 @@ import androidx.compose.runtime.remember import com.kdroid.composetray.lib.mac.MacOSMenuBarThemeDetector import io.github.kdroidfilter.platformtools.LinuxDesktopEnvironment import io.github.kdroidfilter.platformtools.OperatingSystem.* -import io.github.kdroidfilter.platformtools.darkmodedetector.isSystemInDarkMode +import io.github.kdroidfilter.nucleus.darkmodedetector.isSystemInDarkMode import io.github.kdroidfilter.platformtools.detectLinuxDesktopEnvironment import io.github.kdroidfilter.platformtools.getOperatingSystem import java.util.function.Consumer diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/utils/NativeLibraryLoader.kt b/src/jvmMain/kotlin/com/kdroid/composetray/utils/NativeLibraryLoader.kt index 35521c39..78a87f6a 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/utils/NativeLibraryLoader.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/utils/NativeLibraryLoader.kt @@ -7,61 +7,117 @@ import java.nio.file.Files import java.nio.file.StandardCopyOption /** - * Extracts a native library from the classpath (composetray/native//) - * to a temporary directory and registers it with JNA. - * - * This is needed because JNA's default classpath search doesn't look inside - * the composetray/native/ namespace prefix. + * Loads native libraries following the Nucleus pattern: + * 1. Try [System.loadLibrary] (works for packaged apps / GraalVM native-image + * where the lib sits on `java.library.path`). + * 2. Fallback: extract from the classpath (`composetray/native//`) + * into a persistent cache (`~/.cache/composetray/native//`) + * and load from there. */ internal object NativeLibraryLoader { - private val tempDir: File by lazy { - Files.createTempDirectory("composetray-natives").toFile().apply { deleteOnExit() } + private const val RESOURCE_PREFIX = "composetray/native" + private val loadedLibraries = mutableSetOf() + + // ── JNI loading (macOS) ───────────────────────────────────────────── + + /** + * Loads a JNI library via [System.load] / [System.loadLibrary]. + * Returns `true` if the library was loaded successfully. + */ + @Synchronized + fun load(libraryName: String, callerClass: Class<*>): Boolean { + if (libraryName in loadedLibraries) return true + + // 1. Try system library path (packaged app / GraalVM native-image) + try { + System.loadLibrary(libraryName) + loadedLibraries += libraryName + return true + } catch (_: UnsatisfiedLinkError) { + // Not on java.library.path, try classpath extraction + } + + // 2. Extract from classpath to persistent cache + val file = extractToCache(libraryName, callerClass) ?: return false + System.load(file.absolutePath) + loadedLibraries += libraryName + return true } + // ── JNA loading (Linux / Windows) ─────────────────────────────────── + /** * Extracts the native library from the classpath and registers it with JNA. - * - * @param libraryName the library name (e.g. "systray" or "tray") - * @param caller a class from the same classloader to use for resource lookup */ + @Synchronized fun extractAndRegister(libraryName: String, caller: Class<*>) { + // 1. Try JNA's default resolution (system path) + try { + Native.register(caller, libraryName) + return + } catch (_: UnsatisfiedLinkError) { + // Not on system path, extract first + } + + // 2. Extract from classpath to persistent cache, then register + val file = extractToCache(libraryName, caller) + if (file != null) { + NativeLibrary.addSearchPath(libraryName, file.parentFile.absolutePath) + } + Native.register(caller, libraryName) + } + + // ── Shared extraction logic ───────────────────────────────────────── + + private fun extractToCache(libraryName: String, callerClass: Class<*>): File? { val platform = detectPlatform() val fileName = mapLibraryName(libraryName) - val resourcePath = "composetray/native/$platform/$fileName" - - val url = caller.classLoader?.getResource(resourcePath) - if (url != null) { - val targetFile = File(tempDir, fileName) - if (!targetFile.exists()) { - url.openStream().use { input -> - Files.copy(input, targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING) - } - targetFile.deleteOnExit() + val resourcePath = "$RESOURCE_PREFIX/$platform/$fileName" + + val resourceUrl = callerClass.classLoader?.getResource(resourcePath) ?: return null + + val cacheDir = resolveCacheDir(platform) + cacheDir.mkdirs() + val cachedFile = File(cacheDir, fileName) + + // Validate cache: re-extract if size differs or file is missing + val resourceSize = resourceUrl.openConnection().contentLengthLong + if (cachedFile.exists() && cachedFile.length() == resourceSize) { + return cachedFile + } + + // Atomic extract: write to temp then move + val tmpFile = File(cacheDir, "$fileName.tmp") + try { + resourceUrl.openStream().use { input -> + Files.copy(input, tmpFile.toPath(), StandardCopyOption.REPLACE_EXISTING) } - // Add the temp directory to JNA's search path - NativeLibrary.addSearchPath(libraryName, tempDir.absolutePath) + Files.move(tmpFile.toPath(), cachedFile.toPath(), StandardCopyOption.REPLACE_EXISTING) + cachedFile.setExecutable(true) + } finally { + tmpFile.delete() } - Native.register(caller, libraryName) + return cachedFile + } + + private fun resolveCacheDir(platform: String): File { + val os = System.getProperty("os.name")?.lowercase() ?: "" + val base = when { + os.contains("win") -> File(System.getenv("LOCALAPPDATA") ?: System.getProperty("user.home")) + else -> File(System.getProperty("user.home"), ".cache") + } + return File(base, "composetray/native/$platform") } private fun detectPlatform(): String { val os = System.getProperty("os.name")?.lowercase() ?: "" val arch = System.getProperty("os.arch") ?: "" return when { - os.contains("win") -> when { - arch.contains("aarch64") || arch.contains("arm") -> "win32-arm64" - else -> "win32-x86-64" - } - os.contains("linux") -> when { - arch.contains("aarch64") || arch.contains("arm") -> "linux-aarch64" - else -> "linux-x86-64" - } - os.contains("mac") -> when { - arch.contains("aarch64") || arch.contains("arm") -> "darwin-aarch64" - else -> "darwin-x86-64" - } + os.contains("win") -> if (arch.contains("aarch64") || arch.contains("arm")) "win32-arm64" else "win32-x86-64" + os.contains("linux") -> if (arch.contains("aarch64") || arch.contains("arm")) "linux-aarch64" else "linux-x86-64" + os.contains("mac") -> if (arch.contains("aarch64") || arch.contains("arm")) "darwin-aarch64" else "darwin-x86-64" else -> "unknown" } } From 5525156414406dac41fa9d62e77b7050265a9958 Mon Sep 17 00:00:00 2001 From: Elie Gambache Date: Thu, 9 Apr 2026 17:53:01 +0300 Subject: [PATCH 05/12] Add nucleus core-runtime dependency to fix Platform class resolution --- demo/build.gradle.kts | 1 + gradle/libs.versions.toml | 1 + 2 files changed, 2 insertions(+) diff --git a/demo/build.gradle.kts b/demo/build.gradle.kts index afa13154..392a5ff0 100644 --- a/demo/build.gradle.kts +++ b/demo/build.gradle.kts @@ -21,6 +21,7 @@ kotlin { implementation(compose.material3) implementation(compose.materialIconsExtended) implementation(libs.kermit) + implementation(libs.nucleus.core.runtime) implementation(libs.nucleus.darkmode.detector) implementation(libs.nucleus.graalvm.runtime) } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 36eb9325..83d3b4d7 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -16,6 +16,7 @@ kotlinx-coroutines-swing = { module = "org.jetbrains.kotlinx:kotlinx-coroutines- jna = { module = "net.java.dev.jna:jna-jpms", version.ref = "jna" } jna-platform = { module = "net.java.dev.jna:jna-platform-jpms", version.ref = "jna" } platformtools-core = { module = "io.github.kdroidfilter:platformtools.core", version.ref = "platformtools" } +nucleus-core-runtime = { module = "io.github.kdroidfilter:nucleus.core-runtime", version.ref = "nucleus" } nucleus-darkmode-detector = { module = "io.github.kdroidfilter:nucleus.darkmode-detector", version.ref = "nucleus" } nucleus-graalvm-runtime = { module = "io.github.kdroidfilter:nucleus.graalvm-runtime", version.ref = "nucleus" } From 3c095d568557ff2b1dbbcb1be9ab78511a5d3f95 Mon Sep 17 00:00:00 2001 From: Elie Gambache Date: Thu, 9 Apr 2026 18:09:49 +0300 Subject: [PATCH 06/12] fix(jni): resolve GraalVM method lookup via FindClass on interfaces instead of GetObjectClass --- .gitignore | 4 +- .../lib/mac/MacOSMenuBarThemeDetector.kt | 4 +- src/native/macos/MacTrayBridge.m | 42 ++++++++++++++++--- 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 5c8a7924..67b1d87b 100644 --- a/.gitignore +++ b/.gitignore @@ -40,6 +40,7 @@ bin/ ### Mac OS ### .DS_Store +local.properties ### Runtime ### tray_position.properties @@ -47,9 +48,10 @@ tray_position.properties ### Native libraries (built on CI via build-natives.yaml) ### **/src/jvmMain/resources/composetray/native/ -### MSVC build artifacts ### +### Native compilation artifacts ### *.obj *.exp +*.o ### Native build directories ### src/native/windows/build-x64/ diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacOSMenuBarThemeDetector.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacOSMenuBarThemeDetector.kt index 64ab2784..291579c2 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacOSMenuBarThemeDetector.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacOSMenuBarThemeDetector.kt @@ -12,7 +12,7 @@ object MacOSMenuBarThemeDetector { Thread(r, "MacOS MenuBar Theme Detector Thread").apply { isDaemon = true } } - private val themeChangedCallback = object : MacNativeBridge.ThemeChangeCallback { + private class ThemeCallback : MacNativeBridge.ThemeChangeCallback { override fun onThemeChanged(isDark: Int) { callbackExecutor.execute { val dark = isDark != 0 @@ -21,6 +21,8 @@ object MacOSMenuBarThemeDetector { } } + private val themeChangedCallback = ThemeCallback() + init { MacNativeBridge.nativeSetThemeCallback(themeChangedCallback) } diff --git a/src/native/macos/MacTrayBridge.m b/src/native/macos/MacTrayBridge.m index 86eefba4..b42ccaa6 100644 --- a/src/native/macos/MacTrayBridge.m +++ b/src/native/macos/MacTrayBridge.m @@ -123,14 +123,44 @@ static void clearAllCallbacks(CallbackEntry **list) { /* C callback trampolines */ /* ========================================================================== */ +/* + * Cached method IDs — resolved once via the interface class so that + * GraalVM native-image only needs the interface registered for JNI, + * not every lambda / anonymous class that implements it. + */ +static jmethodID g_runMethodID = NULL; +static jmethodID g_onThemeChangedMethodID = NULL; + +static jmethodID getRunnableRunMethod(JNIEnv *env) { + if (g_runMethodID == NULL) { + jclass cls = (*env)->FindClass(env, "java/lang/Runnable"); + if (cls) { + g_runMethodID = (*env)->GetMethodID(env, cls, "run", "()V"); + (*env)->DeleteLocalRef(env, cls); + } + } + return g_runMethodID; +} + +static jmethodID getThemeChangedMethod(JNIEnv *env) { + if (g_onThemeChangedMethodID == NULL) { + jclass cls = (*env)->FindClass(env, "com/kdroid/composetray/lib/mac/MacNativeBridge$ThemeChangeCallback"); + if (cls) { + g_onThemeChangedMethodID = (*env)->GetMethodID(env, cls, "onThemeChanged", "(I)V"); + (*env)->DeleteLocalRef(env, cls); + } + } + return g_onThemeChangedMethodID; +} + /* Called by the Swift tray_callback when the tray icon is left-clicked */ static void trayCbTrampoline(struct tray *t) { JNIEnv *env = getJNIEnv(); if (!env) return; jobject runnable = findCallback(g_trayCallbacks, t); if (!runnable) return; - jclass cls = (*env)->GetObjectClass(env, runnable); - jmethodID run = (*env)->GetMethodID(env, cls, "run", "()V"); + jmethodID run = getRunnableRunMethod(env); + if (!run) return; (*env)->CallVoidMethod(env, runnable, run); if ((*env)->ExceptionCheck(env)) (*env)->ExceptionClear(env); } @@ -141,8 +171,8 @@ static void menuItemCbTrampoline(struct tray_menu_item *item) { if (!env) return; jobject runnable = findCallback(g_menuCallbacks, item); if (!runnable) return; - jclass cls = (*env)->GetObjectClass(env, runnable); - jmethodID run = (*env)->GetMethodID(env, cls, "run", "()V"); + jmethodID run = getRunnableRunMethod(env); + if (!run) return; (*env)->CallVoidMethod(env, runnable, run); if ((*env)->ExceptionCheck(env)) (*env)->ExceptionClear(env); } @@ -154,8 +184,8 @@ static void themeCbTrampoline(int isDark) { if (!g_themeCallback) return; jobject cb = g_themeCallback->globalRef; if (!cb) return; - jclass cls = (*env)->GetObjectClass(env, cb); - jmethodID method = (*env)->GetMethodID(env, cls, "onThemeChanged", "(I)V"); + jmethodID method = getThemeChangedMethod(env); + if (!method) return; (*env)->CallVoidMethod(env, cb, method, (jint)isDark); if ((*env)->ExceptionCheck(env)) (*env)->ExceptionClear(env); } From 4b334b38339a6a5d44f80508681b713f533fd830 Mon Sep 17 00:00:00 2001 From: Elie Gambache Date: Thu, 9 Apr 2026 18:12:51 +0300 Subject: [PATCH 07/12] remove obsolete jna comment --- src/native/macos/tray.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/native/macos/tray.swift b/src/native/macos/tray.swift index 3e0a3407..17b8f29f 100644 --- a/src/native/macos/tray.swift +++ b/src/native/macos/tray.swift @@ -459,7 +459,7 @@ public func tray_get_status_item_region() -> UnsafeMutablePointer? { ) let midX = screen.frame.midX let region = rect.minX < midX ? "top-left" : "top-right" - return strdup(region) // to be freed on JVM/JNA side + return strdup(region) // to be freed on JVM/JNI side } // Per-instance geometry exports From aef04bb9c44b44c0ac13a81346b67dbef0e8d353 Mon Sep 17 00:00:00 2001 From: Elie Gambache Date: Thu, 9 Apr 2026 18:22:25 +0300 Subject: [PATCH 08/12] Remove unused Linux native code, dependencies, and dbusmenu integration. --- .../composetray/lib/linux/JniRunnable.kt | 11 + .../composetray/lib/linux/LinuxLibTray.kt | 54 - .../lib/linux/LinuxNativeBridge.kt | 69 + .../composetray/lib/linux/LinuxTrayManager.kt | 206 +- .../reachability-metadata.json | 14 + src/native/linux/Makefile | 37 - src/native/linux/build.sh | 86 +- src/native/linux/go.mod | 27 - src/native/linux/go.sum | 21 - src/native/linux/icon/icon.png | Bin 2238 -> 0 bytes src/native/linux/icon/iconunix.go | 196 - src/native/linux/icon/logo.png | Bin 1851 -> 0 bytes src/native/linux/internal/DbusMenu.xml | 60 - .../linux/internal/StatusNotifierItem.xml | 85 - .../internal/generated/menu/dbus_menu.go | 484 - .../notifier/status_notifier_item.go | 633 - src/native/linux/jna/bridge_linux.go | 297 - src/native/linux/jna/main.go | 5 - src/native/linux/jni_bridge.c | 491 + src/native/linux/sni.c | 1281 ++ src/native/linux/sni.h | 102 + src/native/linux/stb_image.h | 7988 ++++++++++++ src/native/linux/stb_image_resize2.h | 10679 ++++++++++++++++ src/native/linux/systray.go | 404 - src/native/linux/systray.h | 27 - src/native/linux/systray_menu_unix.go | 345 - src/native/linux/systray_other.go | 183 - src/native/linux/systray_unix.go | 511 - 28 files changed, 20793 insertions(+), 3503 deletions(-) create mode 100644 src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/JniRunnable.kt delete mode 100644 src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/LinuxLibTray.kt create mode 100644 src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/LinuxNativeBridge.kt delete mode 100644 src/native/linux/Makefile delete mode 100644 src/native/linux/go.mod delete mode 100644 src/native/linux/go.sum delete mode 100644 src/native/linux/icon/icon.png delete mode 100644 src/native/linux/icon/iconunix.go delete mode 100644 src/native/linux/icon/logo.png delete mode 100644 src/native/linux/internal/DbusMenu.xml delete mode 100644 src/native/linux/internal/StatusNotifierItem.xml delete mode 100644 src/native/linux/internal/generated/menu/dbus_menu.go delete mode 100644 src/native/linux/internal/generated/notifier/status_notifier_item.go delete mode 100644 src/native/linux/jna/bridge_linux.go delete mode 100644 src/native/linux/jna/main.go create mode 100644 src/native/linux/jni_bridge.c create mode 100644 src/native/linux/sni.c create mode 100644 src/native/linux/sni.h create mode 100644 src/native/linux/stb_image.h create mode 100644 src/native/linux/stb_image_resize2.h delete mode 100644 src/native/linux/systray.go delete mode 100644 src/native/linux/systray.h delete mode 100644 src/native/linux/systray_menu_unix.go delete mode 100644 src/native/linux/systray_other.go delete mode 100644 src/native/linux/systray_unix.go diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/JniRunnable.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/JniRunnable.kt new file mode 100644 index 00000000..0d4509fe --- /dev/null +++ b/src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/JniRunnable.kt @@ -0,0 +1,11 @@ +package com.kdroid.composetray.lib.linux + +/** + * Concrete Runnable wrapper for JNI callbacks. + * GraalVM native-image cannot resolve run() on SAM-converted lambda classes + * because they are dynamically generated and not registered for JNI access. + * This wrapper provides a known, statically registered class. + */ +internal class JniRunnable(private val action: () -> Unit) : Runnable { + override fun run() = action() +} diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/LinuxLibTray.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/LinuxLibTray.kt deleted file mode 100644 index 7b9626e5..00000000 --- a/src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/LinuxLibTray.kt +++ /dev/null @@ -1,54 +0,0 @@ -package com.kdroid.composetray.lib.linux - -import com.kdroid.composetray.utils.NativeLibraryLoader -import com.sun.jna.Callback -import com.sun.jna.ptr.IntByReference - -/** - * JNA direct mapping to the Go-based systray bridge (src/native/linux/). - */ -internal object LinuxLibTray { - // Callback types ------------------------------------------------------------- - interface VoidCallback : Callback { fun invoke() } - interface MenuItemCallback : Callback { fun invoke(menuId: Int) } - - // Library registration logic (simple and consistent with Windows/Mac) -------- - init { - NativeLibraryLoader.extractAndRegister("systray", LinuxLibTray::class.java) - } - - // Helpers to fetch last click xy - @JvmStatic external fun Systray_GetLastClickXY(outX: IntByReference, outY: IntByReference) - - // Lifecycle / loop ----------------------------------------------------------- - @JvmStatic external fun Systray_InitCallbacks(ready: VoidCallback?, exit: VoidCallback?, onClick: VoidCallback?, onRClick: VoidCallback?, onMenuItem: MenuItemCallback?) - @JvmStatic external fun Systray_PrepareExternalLoop() - @JvmStatic external fun Systray_NativeStart() - @JvmStatic external fun Systray_NativeEnd() - @JvmStatic external fun Systray_Quit() - - // Tray properties ------------------------------------------------------------ - @JvmStatic external fun Systray_SetIcon(iconBytes: ByteArray, length: Int) - @JvmStatic external fun Systray_SetTitle(title: String?) - @JvmStatic external fun Systray_SetTooltip(tooltip: String?) - - // Menu building -------------------------------------------------------------- - @JvmStatic external fun Systray_ResetMenu() - @JvmStatic external fun Systray_AddSeparator() - @JvmStatic external fun Systray_AddMenuItem(title: String?, tooltip: String?): Int - @JvmStatic external fun Systray_AddMenuItemCheckbox(title: String?, tooltip: String?, checked: Int): Int - @JvmStatic external fun Systray_AddSubMenuItem(parentID: Int, title: String?, tooltip: String?): Int - @JvmStatic external fun Systray_AddSubMenuItemCheckbox(parentID: Int, title: String?, tooltip: String?, checked: Int): Int - @JvmStatic external fun Systray_AddSubMenuSeparator(parentID: Int) - - // Per-item operations -------------------------------------------------------- - @JvmStatic external fun Systray_MenuItem_SetTitle(id: Int, title: String?): Int - @JvmStatic external fun Systray_MenuItem_Enable(id: Int) - @JvmStatic external fun Systray_MenuItem_Disable(id: Int) - @JvmStatic external fun Systray_MenuItem_Show(id: Int) - @JvmStatic external fun Systray_MenuItem_Hide(id: Int) - @JvmStatic external fun Systray_MenuItem_Check(id: Int) - @JvmStatic external fun Systray_MenuItem_Uncheck(id: Int) - - @JvmStatic external fun Systray_SetMenuItemIcon(iconBytes: ByteArray, length: Int, id: Int) -} \ No newline at end of file diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/LinuxNativeBridge.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/LinuxNativeBridge.kt new file mode 100644 index 00000000..1273e486 --- /dev/null +++ b/src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/LinuxNativeBridge.kt @@ -0,0 +1,69 @@ +package com.kdroid.composetray.lib.linux + +import com.kdroid.composetray.utils.NativeLibraryLoader + +/** + * JNI bridge to the native Linux tray library (libLinuxTray.so). + * Replaces the previous JNA/Go-based approach with a pure C + sd-bus implementation. + * Follows the same patterns as MacNativeBridge. + */ +internal object LinuxNativeBridge { + + init { + NativeLibraryLoader.load("LinuxTray", LinuxNativeBridge::class.java) + } + + // -- Lifecycle --------------------------------------------------------------- + + /** Create a tray instance. Returns a native handle (pointer as long). */ + @JvmStatic external fun nativeCreate(iconBytes: ByteArray?, tooltip: String?): Long + + /** Start the D-Bus event loop (blocks until nativeQuit is called). */ + @JvmStatic external fun nativeRun(handle: Long): Int + + /** Signal the event loop to stop. Thread-safe. */ + @JvmStatic external fun nativeQuit(handle: Long) + + /** Destroy the tray and release all resources. Call after nativeRun returns. */ + @JvmStatic external fun nativeDestroy(handle: Long) + + // -- Tray properties --------------------------------------------------------- + + @JvmStatic external fun nativeSetIcon(handle: Long, iconBytes: ByteArray) + @JvmStatic external fun nativeSetTitle(handle: Long, title: String?) + @JvmStatic external fun nativeSetTooltip(handle: Long, tooltip: String?) + + // -- Callbacks --------------------------------------------------------------- + + @JvmStatic external fun nativeSetClickCallback(handle: Long, callback: Runnable?) + @JvmStatic external fun nativeSetRClickCallback(handle: Long, callback: Runnable?) + + /** Register a per-menu-item callback. The callback is keyed by menuId. */ + @JvmStatic external fun nativeSetMenuItemCallback(handle: Long, menuId: Int, callback: Runnable?) + + // -- Click position ---------------------------------------------------------- + + /** Writes [x, y] into outXY. */ + @JvmStatic external fun nativeGetLastClickXY(handle: Long, outXY: IntArray) + + // -- Menu management --------------------------------------------------------- + + @JvmStatic external fun nativeResetMenu(handle: Long) + @JvmStatic external fun nativeAddMenuItem(handle: Long, title: String?, tooltip: String?): Int + @JvmStatic external fun nativeAddMenuItemCheckbox(handle: Long, title: String?, tooltip: String?, checked: Boolean): Int + @JvmStatic external fun nativeAddSeparator(handle: Long) + @JvmStatic external fun nativeAddSubMenuItem(handle: Long, parentId: Int, title: String?, tooltip: String?): Int + @JvmStatic external fun nativeAddSubMenuItemCheckbox(handle: Long, parentId: Int, title: String?, tooltip: String?, checked: Boolean): Int + @JvmStatic external fun nativeAddSubSeparator(handle: Long, parentId: Int) + + // -- Per-item operations ----------------------------------------------------- + + @JvmStatic external fun nativeItemSetTitle(handle: Long, id: Int, title: String?): Int + @JvmStatic external fun nativeItemEnable(handle: Long, id: Int) + @JvmStatic external fun nativeItemDisable(handle: Long, id: Int) + @JvmStatic external fun nativeItemShow(handle: Long, id: Int) + @JvmStatic external fun nativeItemHide(handle: Long, id: Int) + @JvmStatic external fun nativeItemCheck(handle: Long, id: Int) + @JvmStatic external fun nativeItemUncheck(handle: Long, id: Int) + @JvmStatic external fun nativeItemSetIcon(handle: Long, id: Int, iconBytes: ByteArray) +} diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/LinuxTrayManager.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/LinuxTrayManager.kt index d381d095..a41b8274 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/LinuxTrayManager.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/LinuxTrayManager.kt @@ -14,10 +14,10 @@ import java.util.concurrent.locks.ReentrantLock import kotlin.concurrent.withLock /** - * Go-based Linux tray manager using the linuxlibnew JNA bridge. + * C/sd-bus-based Linux tray manager using JNI bridge. * - * Intentionally mirrors the public surface used by LinuxSNITrayInitializer and LinuxTrayMenuBuilderImpl, - * while delegating to GoSystray under the hood. + * Replaces the previous Go/JNA implementation with a direct C + sd-bus approach. + * Follows the same handle-based pattern as macOS (MacTrayManager). */ internal class LinuxTrayManager( private val instanceId: String, @@ -25,17 +25,11 @@ internal class LinuxTrayManager( private var tooltip: String = "", private var onLeftClick: (() -> Unit)? = null ) { - // Keep strong references to JNA callbacks to prevent GC from dropping them (which would stop events) - private var cbReady: LinuxLibTray.VoidCallback? = null - private var cbExit: LinuxLibTray.VoidCallback? = null - private var cbOnClick: LinuxLibTray.VoidCallback? = null - private var cbOnRClick: LinuxLibTray.VoidCallback? = null - private var cbOnMenuItem: LinuxLibTray.MenuItemCallback? = null companion object { - // Ensures only one systray runtime is active at a time; allows release from any thread + // Ensures only one systray runtime is active at a time private val lifecyclePermit = java.util.concurrent.Semaphore(1, true) } - // We reuse the existing MenuItem model from LinuxTrayManager to avoid touching menu builder code + data class MenuItem( val text: String, val isEnabled: Boolean = true, @@ -43,31 +37,32 @@ internal class LinuxTrayManager( val isChecked: Boolean = false, val iconPath: String? = null, val onClick: (() -> Unit)? = null, - val subMenuItems: List = emptyList() + val subMenuItems: List = emptyList() ) - private val go = LinuxLibTray + private val native = LinuxNativeBridge private val lock = ReentrantLock() private val running = AtomicBoolean(false) private val permitHeld = AtomicBoolean(false) // Menu state built by builder - private val menuItems: MutableList = mutableListOf() + private val menuItems: MutableList = mutableListOf() - // Mapping from menu item title to Go-assigned IDs (best-effort; titles should be unique) + // Mapping from menu item title to native IDs private val idByTitle: MutableMap = mutableMapOf() private val actionById: MutableMap Unit> = mutableMapOf() - // KDE environment detection to mirror C++ backend behavior private fun isKDEDesktop(): Boolean = detectLinuxDesktopEnvironment() == LinuxDesktopEnvironment.KDE + // Native handle + private var trayHandle: Long = 0L + // Thread / lifecycle private var shutdownHook: Thread? = null private var loopThread: Thread? = null - private var exitLatch: CountDownLatch? = null - fun addMenuItem(menuItem: LinuxTrayManager.MenuItem) { + fun addMenuItem(menuItem: MenuItem) { lock.withLock { menuItems.add(menuItem) } } @@ -80,9 +75,10 @@ internal class LinuxTrayManager( menuItems[idx] = current.copy(isChecked = isChecked) } val id = idByTitle[label] - if (id != null) { + if (id != null && trayHandle != 0L) { try { - if (isChecked) go.Systray_MenuItem_Check(id) else go.Systray_MenuItem_Uncheck(id) + if (isChecked) native.nativeItemCheck(trayHandle, id) + else native.nativeItemUncheck(trayHandle, id) } catch (_: Throwable) { fallback = true } } else { fallback = true @@ -95,7 +91,7 @@ internal class LinuxTrayManager( newIconPath: String, newTooltip: String, newOnLeftClick: (() -> Unit)?, - newMenuItems: List? + newMenuItems: List? ) { val iconChanged: Boolean val tooltipChanged: Boolean @@ -113,93 +109,88 @@ internal class LinuxTrayManager( } if (iconChanged) setIconFromFileSafe(iconPath) - if (tooltipChanged) runCatching { go.Systray_SetTooltip(tooltip) } - .onFailure { e -> warnln { "[LinuxGoTrayManager] Failed to set tooltip: ${e.message}" } } + if (tooltipChanged) runCatching { native.nativeSetTooltip(trayHandle, tooltip) } + .onFailure { e -> warnln { "[LinuxTrayManager] Failed to set tooltip: ${e.message}" } } if (newMenuItems != null) rebuildMenu() } fun startTray() { - // Acquire global lifecycle permit to prevent overlap with a previous instance teardown try { lifecyclePermit.acquire() permitHeld.set(true) } catch (t: Throwable) { - warnln { "[LinuxGoTrayManager] Failed to acquire lifecycle permit: ${t.message}" } + warnln { "[LinuxTrayManager] Failed to acquire lifecycle permit: ${t.message}" } return } var started = false try { if (!running.compareAndSet(false, true)) { - // Already running for this instance; release permit we just acquired lifecyclePermit.release() permitHeld.set(false) return } - // Shutdown hook shutdownHook = Thread { stopTray() }.also { Runtime.getRuntime().addShutdownHook(it) } val readyLatch = CountDownLatch(1) - exitLatch = CountDownLatch(1) - - // Register callbacks (keep strong references to avoid GC) - cbReady = object : LinuxLibTray.VoidCallback { override fun invoke() { - infoln { "[LinuxGoTrayManager] systray ready" } - readyLatch.countDown() - } } - cbExit = object : LinuxLibTray.VoidCallback { override fun invoke() { - infoln { "[LinuxGoTrayManager] systray exit" } - try { exitLatch?.countDown() } catch (_: Throwable) {} - } } - cbOnClick = object : LinuxLibTray.VoidCallback { override fun invoke() { - // Try to fetch the last click xy from native Go layer and store it for positioning + + // Read initial icon bytes + val iconBytes = runCatching { File(iconPath).takeIf { it.isFile }?.readBytes() } + .getOrNull() + + // Create native tray + trayHandle = native.nativeCreate(iconBytes, tooltip) + if (trayHandle == 0L) { + errorln { "[LinuxTrayManager] Failed to create native tray" } + return + } + + // Set title + runCatching { native.nativeSetTitle(trayHandle, tooltip) } + + // Set click callback + native.nativeSetClickCallback(trayHandle, JniRunnable { try { - val xRef = com.sun.jna.ptr.IntByReference() - val yRef = com.sun.jna.ptr.IntByReference() - go.Systray_GetLastClickXY(xRef, yRef) - val x = xRef.value - val y = yRef.value - // Use multi-monitor aware position tracking that resolves the correct screen bounds - TrayClickTracker.updateClickPosition(x, y) - } catch (_: Throwable) { - // ignore, still invoke user callback - } + val xy = IntArray(2) + native.nativeGetLastClickXY(trayHandle, xy) + TrayClickTracker.updateClickPosition(xy[0], xy[1]) + } catch (_: Throwable) { } onLeftClick?.invoke() - } } - cbOnRClick = object : LinuxLibTray.VoidCallback { override fun invoke() { /* right click unhandled for now */ } } - cbOnMenuItem = object : LinuxLibTray.MenuItemCallback { override fun invoke(menuId: Int) { actionById[menuId]?.invoke() } } + }) - go.Systray_InitCallbacks(cbReady, cbExit, cbOnClick, cbOnRClick, cbOnMenuItem) + // Build menu before starting the loop + rebuildMenu() - // Prepare external loop and start it in a daemon thread - go.Systray_PrepareExternalLoop() + // Start event loop in daemon thread loopThread = Thread({ - try { go.Systray_NativeStart() } catch (t: Throwable) { errorln { "[LinuxGoTrayManager] loop error: $t" } } - }, "LinuxGoTray-Loop").apply { + try { + readyLatch.countDown() + native.nativeRun(trayHandle) + } catch (t: Throwable) { + errorln { "[LinuxTrayManager] loop error: $t" } + } + }, "LinuxTray-Loop").apply { isDaemon = true start() } - // Wait until ready then set properties and build menu try { readyLatch.await() } catch (_: InterruptedException) { Thread.currentThread().interrupt() } - runCatching { go.Systray_SetTitle("Compose Tray") } - setIconFromFileSafe(iconPath) - runCatching { go.Systray_SetTooltip(tooltip) } - rebuildMenu() started = true } catch (t: Throwable) { - errorln { "[LinuxGoTrayManager] startTray failed: $t" } + errorln { "[LinuxTrayManager] startTray failed: $t" } } finally { if (!started) { - // cleanup partial start and release permit running.set(false) - try { go.Systray_NativeEnd() } catch (_: Throwable) {} - try { loopThread?.join(500) } catch (_: Throwable) {} + if (trayHandle != 0L) { + try { native.nativeQuit(trayHandle) } catch (_: Throwable) {} + try { loopThread?.join(500) } catch (_: Throwable) {} + try { native.nativeDestroy(trayHandle) } catch (_: Throwable) {} + trayHandle = 0L + } loopThread = null - exitLatch = null try { shutdownHook?.let { Runtime.getRuntime().removeShutdownHook(it) } } catch (_: Throwable) {} shutdownHook = null if (permitHeld.compareAndSet(true, false)) { @@ -211,36 +202,25 @@ internal class LinuxTrayManager( fun stopTray() { if (!running.compareAndSet(true, false)) return - val latch = exitLatch - try { - // 1) Ask Go to quit its loop - go.Systray_Quit() - } catch (_: Throwable) {} - - try { - // 2) End native side immediately so Go can invoke systrayExit (exit callback) - go.Systray_NativeEnd() - } catch (_: Throwable) {} - try { - // 3) Wait a short time for the exit callback to arrive (it will countDown the latch) - // No need to wait seconds — 150–300ms is enough on healthy paths. - latch?.await(150, MILLISECONDS) + if (trayHandle != 0L) native.nativeQuit(trayHandle) } catch (_: Throwable) {} try { - // 4) Join the loop thread briefly; it's a daemon anyway loopThread?.join(500) if (loopThread?.isAlive == true) { - // We can't interrupt native waits; just log and move on - warnln { "[LinuxGoTrayManager] loop thread still alive after join timeout" } + warnln { "[LinuxTrayManager] loop thread still alive after join timeout" } } } catch (_: InterruptedException) { Thread.currentThread().interrupt() } + try { + if (trayHandle != 0L) native.nativeDestroy(trayHandle) + } catch (_: Throwable) {} + + trayHandle = 0L loopThread = null - exitLatch = null idByTitle.clear() actionById.clear() try { shutdownHook?.let { Runtime.getRuntime().removeShutdownHook(it) } } catch (_: Throwable) {} @@ -250,66 +230,68 @@ internal class LinuxTrayManager( } } - // ---------------------------------------------------------------------------------------- private fun setIconFromFileSafe(path: String) { runCatching { val file = File(path) - if (file.isFile) { - val bytes = file.readBytes() - go.Systray_SetIcon(bytes, bytes.size) + if (file.isFile && trayHandle != 0L) { + native.nativeSetIcon(trayHandle, file.readBytes()) } else { - warnln { "[LinuxGoTrayManager] Icon file not found: $path" } + warnln { "[LinuxTrayManager] Icon file not found: $path" } } - }.onFailure { e -> warnln { "[LinuxGoTrayManager] Failed to set icon from $path: ${e.message}" } } + }.onFailure { e -> warnln { "[LinuxTrayManager] Failed to set icon from $path: ${e.message}" } } } private fun rebuildMenu() { - infoln { "[LinuxGoTrayManager] Rebuilding menu" } + if (trayHandle == 0L) return + infoln { "[LinuxTrayManager] Rebuilding menu" } idByTitle.clear() actionById.clear() - runCatching { go.Systray_ResetMenu() } + runCatching { native.nativeResetMenu(trayHandle) } val items = lock.withLock { menuItems.toList() } - val effectiveItems = if (items.isEmpty() && isKDEDesktop()) listOf(LinuxTrayManager.MenuItem("-")) else items + // KDE quirk: empty menu causes issues, add dummy separator + val effectiveItems = if (items.isEmpty() && isKDEDesktop()) listOf(MenuItem("-")) else items effectiveItems.forEach { addMenuItemRecursive(null, it) } } - private fun addMenuItemRecursive(parentId: Int?, item: LinuxTrayManager.MenuItem) { + private fun addMenuItemRecursive(parentId: Int?, item: MenuItem) { try { if (item.text == "-") { if (parentId == null) { - go.Systray_AddSeparator() + native.nativeAddSeparator(trayHandle) } else { - // Proper submenu separator through native bridge - runCatching { go.Systray_AddSubMenuSeparator(parentId) } - .onFailure { _ -> - // Fallback (should not happen if bridge supports it): disabled dash item - val id = go.Systray_AddSubMenuItem(parentId, "-", null) - go.Systray_MenuItem_Disable(id) + runCatching { native.nativeAddSubSeparator(trayHandle, parentId) } + .onFailure { + val id = native.nativeAddSubMenuItem(trayHandle, parentId, "-", null) + native.nativeItemDisable(trayHandle, id) } } return } val id = if (parentId == null) { - if (item.isCheckable) go.Systray_AddMenuItemCheckbox(item.text, null, if (item.isChecked) 1 else 0) - else go.Systray_AddMenuItem(item.text, null) + if (item.isCheckable) native.nativeAddMenuItemCheckbox(trayHandle, item.text, null, item.isChecked) + else native.nativeAddMenuItem(trayHandle, item.text, null) } else { - if (item.isCheckable) go.Systray_AddSubMenuItemCheckbox(parentId, item.text, null, if (item.isChecked) 1 else 0) - else go.Systray_AddSubMenuItem(parentId, item.text, null) + if (item.isCheckable) native.nativeAddSubMenuItemCheckbox(trayHandle, parentId, item.text, null, item.isChecked) + else native.nativeAddSubMenuItem(trayHandle, parentId, item.text, null) } idByTitle[item.text] = id - item.onClick?.let { actionById[id] = it } + item.onClick?.let { action -> + actionById[id] = action + native.nativeSetMenuItemCallback(trayHandle, id, JniRunnable { action() }) + } // Enable/Disable - if (item.isEnabled) go.Systray_MenuItem_Enable(id) else go.Systray_MenuItem_Disable(id) + if (item.isEnabled) native.nativeItemEnable(trayHandle, id) + else native.nativeItemDisable(trayHandle, id) // Icon item.iconPath?.let { iconPath -> runCatching { val bytes = File(iconPath).takeIf { it.isFile }?.readBytes() - if (bytes != null) go.Systray_SetMenuItemIcon(bytes, bytes.size, id) - }.onFailure { e -> warnln { "[LinuxGoTrayManager] Failed to set menu item icon: ${e.message}" } } + if (bytes != null) native.nativeItemSetIcon(trayHandle, id, bytes) + }.onFailure { e -> warnln { "[LinuxTrayManager] Failed to set menu item icon: ${e.message}" } } } // Submenu @@ -317,7 +299,7 @@ internal class LinuxTrayManager( item.subMenuItems.forEach { sub -> addMenuItemRecursive(id, sub) } } } catch (t: Throwable) { - errorln { "[LinuxGoTrayManager] Error adding menu item '${item.text}': $t" } + errorln { "[LinuxTrayManager] Error adding menu item '${item.text}': $t" } } } } diff --git a/src/jvmMain/resources/META-INF/native-image/io.github.kdroidfilter/composenativetray/reachability-metadata.json b/src/jvmMain/resources/META-INF/native-image/io.github.kdroidfilter/composenativetray/reachability-metadata.json index 59dfede4..4672dbca 100644 --- a/src/jvmMain/resources/META-INF/native-image/io.github.kdroidfilter/composenativetray/reachability-metadata.json +++ b/src/jvmMain/resources/META-INF/native-image/io.github.kdroidfilter/composenativetray/reachability-metadata.json @@ -14,6 +14,20 @@ } ] }, + { + "type": "com.kdroid.composetray.lib.linux.LinuxNativeBridge", + "jniAccessible": true + }, + { + "type": "com.kdroid.composetray.lib.linux.JniRunnable", + "jniAccessible": true, + "methods": [ + { + "name": "run", + "parameterTypes": [] + } + ] + }, { "type": "java.lang.Runnable", "jniAccessible": true, diff --git a/src/native/linux/Makefile b/src/native/linux/Makefile deleted file mode 100644 index 309e72d7..00000000 --- a/src/native/linux/Makefile +++ /dev/null @@ -1,37 +0,0 @@ -tag-changelog: require-version require-gh-token - echo "Tagging..." && \ - git tag -a "$$VERSION" -f --annotate -m"Tagged $$VERSION" && \ - git push --tags -f && \ - git checkout master && \ - git pull && \ - github_changelog_generator --no-issues --max-issues 100 --token "${GH_TOKEN}" --user getlantern --project systray && \ - git add CHANGELOG.md && \ - git commit -m "Updated changelog for $$VERSION" && \ - git push origin HEAD && \ - git checkout - - -guard-%: - @ if [ -z '${${*}}' ]; then echo 'Environment variable $* not set' && exit 1; fi - -require-version: guard-VERSION - -require-gh-token: guard-GH_TOKEN - -.PHONY: build-so -build-so: - GOOS=linux GOARCH=$${GOARCH:-amd64} CGO_ENABLED=1 \ - CGO_CFLAGS="-Os -ffunction-sections -fdata-sections -fPIC" \ - CGO_LDFLAGS="-Wl,-O1 -Wl,--as-needed -Wl,--gc-sections -Wl,-s" \ - go build -buildmode=c-shared -trimpath -gcflags=all=-l -ldflags "-s -w -buildid= -linkmode=external -extldflags '-Wl,-O1 -Wl,--as-needed -Wl,-s -Wl,--gc-sections'" -o dist/libsystray.so ./jna - strip --strip-unneeded -R .comment -R .note.GNU-stack -R .note.gnu.property -R .note.go.buildid dist/libsystray.so || true - @echo "Built dist/libsystray.so and dist/libsystray.h (extra size-optimized)" - - -.PHONY: build-so-tinygo -build-so-tinygo: - @mkdir -p dist - @ARCH=$${GOARCH:-amd64}; \ - TARGET=linux-$${ARCH}; \ - echo "Building TinyGo shared library for target $$TARGET..."; \ - tinygo build -buildmode=c-shared -target=$$TARGET -no-debug -opt=z -o dist/libsystray_tinygo.so ./jna && \ - echo "Built dist/libsystray_tinygo.so and dist/libsystray_tinygo.h (TinyGo)" diff --git a/src/native/linux/build.sh b/src/native/linux/build.sh index 0ee46796..4fea7383 100755 --- a/src/native/linux/build.sh +++ b/src/native/linux/build.sh @@ -1,34 +1,76 @@ #!/bin/bash -# Exit on any error -set -e +# Build libLinuxTray.so – C-based Linux system tray library with JNI bridge. +# Dependencies: libsystemd-dev (for sd-bus), JDK (for jni.h) -echo "Building Linux systray shared library..." +set -e -# Ensure we run from this script's directory (linuxlib) SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" -cd "$SCRIPT_DIR" - -# Use NATIVE_LIBS_OUTPUT_DIR env var if set, otherwise default to src/jvmMain/resources/composetray/native OUTPUT_DIR="${NATIVE_LIBS_OUTPUT_DIR:-$SCRIPT_DIR/../../jvmMain/resources/composetray/native}" -echo "Output dir for linux is: $OUTPUT_DIR" -# Inform about architecture if provided -if [[ -n "$GOARCH" ]]; then - echo "Using GOARCH=$GOARCH" -else - echo "GOARCH not set, defaulting to amd64 (as per Makefile)" +echo "Building LinuxTray library (C + sd-bus + JNI)..." +echo "Output dir: $OUTPUT_DIR" + +# Detect JAVA_HOME +if [ -z "$JAVA_HOME" ]; then + # Try common locations + for jdk in /usr/lib/jvm/java-*-openjdk-amd64 /usr/lib/jvm/java-*-openjdk /usr/lib/jvm/default-java; do + if [ -d "$jdk" ] && [ -f "$jdk/include/jni.h" ]; then + JAVA_HOME="$jdk" + break + fi + done +fi +if [ -z "$JAVA_HOME" ] || [ ! -f "$JAVA_HOME/include/jni.h" ]; then + echo "ERROR: JAVA_HOME not found or jni.h missing. Install a JDK or set JAVA_HOME." + exit 1 +fi +echo "Using JAVA_HOME: $JAVA_HOME" + +JNI_INCLUDE="$JAVA_HOME/include" +JNI_INCLUDE_LINUX="$JAVA_HOME/include/linux" + +# Verify sd-bus is available +if ! pkg-config --exists libsystemd 2>/dev/null; then + echo "ERROR: libsystemd-dev not found. Install it: sudo apt install libsystemd-dev" + exit 1 fi -# Build the shared library using the provided Makefile target -make build-so +SDBUS_CFLAGS=$(pkg-config --cflags libsystemd) +SDBUS_LIBS=$(pkg-config --libs libsystemd) + +mkdir -p "$OUTPUT_DIR/linux-x86-64" + +# Compile sni.c (includes stb_image implementation) +echo "Compiling sni.c..." +gcc -c -o "$SCRIPT_DIR/sni.o" \ + -fPIC -O2 -Wall -Wextra -Wno-unused-parameter \ + -I "$SCRIPT_DIR" \ + $SDBUS_CFLAGS \ + "$SCRIPT_DIR/sni.c" + +# Compile jni_bridge.c +echo "Compiling jni_bridge.c..." +gcc -c -o "$SCRIPT_DIR/jni_bridge.o" \ + -fPIC -O2 -Wall -Wextra -Wno-unused-parameter \ + -I "$SCRIPT_DIR" \ + -I "$JNI_INCLUDE" \ + -I "$JNI_INCLUDE_LINUX" \ + "$SCRIPT_DIR/jni_bridge.c" + +# Link into shared library +echo "Linking libLinuxTray.so..." +gcc -shared -o "$OUTPUT_DIR/linux-x86-64/libLinuxTray.so" \ + "$SCRIPT_DIR/sni.o" \ + "$SCRIPT_DIR/jni_bridge.o" \ + $SDBUS_LIBS \ + -lpthread -lm -# Destination directory -DEST_DIR="$OUTPUT_DIR/linux-x86-64" -mkdir -p "$DEST_DIR" +# Strip debug symbols for smaller binary +strip --strip-unneeded "$OUTPUT_DIR/linux-x86-64/libLinuxTray.so" -# Copy the generated .so to the destination directory -cp -f dist/libsystray.so "$DEST_DIR/libsystray.so" +# Clean up object files +rm -f "$SCRIPT_DIR/sni.o" "$SCRIPT_DIR/jni_bridge.o" -echo "Copied dist/libsystray.so to $DEST_DIR/libsystray.so" -echo "Linux build completed successfully." +echo "Build completed: $OUTPUT_DIR/linux-x86-64/libLinuxTray.so" +ls -lh "$OUTPUT_DIR/linux-x86-64/libLinuxTray.so" diff --git a/src/native/linux/go.mod b/src/native/linux/go.mod deleted file mode 100644 index 4fe3b17a..00000000 --- a/src/native/linux/go.mod +++ /dev/null @@ -1,27 +0,0 @@ -module github.com/energye/systray - -go 1.23.0 - -toolchain go1.24.5 - -require golang.org/x/sys v0.34.0 - -require ( - github.com/godbus/dbus/v5 v5.0.4 - github.com/tevino/abool v0.0.0-20220530134649-2bfc934cb23c -) - -require ( - github.com/google/go-cmp v0.6.0 // indirect - github.com/yuin/goldmark v1.4.13 // indirect - golang.org/x/crypto v0.40.0 // indirect - golang.org/x/image v0.30.0 // indirect - golang.org/x/mod v0.26.0 // indirect - golang.org/x/net v0.42.0 // indirect - golang.org/x/sync v0.16.0 // indirect - golang.org/x/telemetry v0.0.0-20250710130107-8d8967aff50b // indirect - golang.org/x/term v0.33.0 // indirect - golang.org/x/text v0.28.0 // indirect - golang.org/x/tools v0.35.0 // indirect - golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 // indirect -) diff --git a/src/native/linux/go.sum b/src/native/linux/go.sum deleted file mode 100644 index de4e7f32..00000000 --- a/src/native/linux/go.sum +++ /dev/null @@ -1,21 +0,0 @@ -github.com/godbus/dbus/v5 v5.0.4 h1:9349emZab16e7zQvpmsbtjc18ykshndd8y2PG3sgJbA= -github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= -github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/tevino/abool v0.0.0-20220530134649-2bfc934cb23c h1:coVla7zpsycc+kA9NXpcvv2E4I7+ii6L5hZO2S6C3kw= -github.com/tevino/abool v0.0.0-20220530134649-2bfc934cb23c/go.mod h1:qc66Pna1RiIsPa7O4Egxxs9OqkuxDX55zznh9K07Tzg= -github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -golang.org/x/crypto v0.40.0/go.mod h1:Qr1vMER5WyS2dfPHAlsOj01wgLbsyWtFn/aY+5+ZdxY= -golang.org/x/image v0.30.0 h1:jD5RhkmVAnjqaCUXfbGBrn3lpxbknfN9w2UhHHU+5B4= -golang.org/x/image v0.30.0/go.mod h1:SAEUTxCCMWSrJcCy/4HwavEsfZZJlYxeHLc6tTiAe/c= -golang.org/x/mod v0.26.0/go.mod h1:/j6NAhSk8iQ723BGAUyoAcn7SlD7s15Dp9Nd/SfeaFQ= -golang.org/x/net v0.42.0/go.mod h1:FF1RA5d3u7nAYA4z2TkclSCKh68eSXtiFwcWQpPXdt8= -golang.org/x/sync v0.16.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= -golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= -golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.34.0 h1:H5Y5sJ2L2JRdyv7ROF1he/lPdvFsd0mJHFw2ThKHxLA= -golang.org/x/sys v0.34.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= -golang.org/x/telemetry v0.0.0-20250710130107-8d8967aff50b/go.mod h1:4ZwOYna0/zsOKwuR5X/m0QFOJpSZvAxFfkQT+Erd9D4= -golang.org/x/term v0.33.0/go.mod h1:s18+ql9tYWp1IfpV9DmCtQDDSRBUjKaw9M1eAv5UeF0= -golang.org/x/text v0.28.0/go.mod h1:U8nCwOR8jO/marOQ0QbDiOngZVEBB7MAiitBuMjXiNU= -golang.org/x/tools v0.35.0/go.mod h1:NKdj5HkL/73byiZSJjqJgKn3ep7KjFkBOkR/Hps3VPw= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/src/native/linux/icon/icon.png b/src/native/linux/icon/icon.png deleted file mode 100644 index 420c618771688b74da662ab95e5e95d87aa7bbe6..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2238 zcmbVOX;f2Z8on$=77?t7*os_(C?k-4BV|iSqM%q-K^99$E+Im4LlzPOn6Nv7ENY8_ zAW#t!?PWLYa?MaV+nc$tDDfdD!Wj)OoyA%_nILmXae zatGuA07mhG&>7fgVa4LLh`>98f7t5=m)FPt;pp8nV|PV^H8* z6YY;&0fC?|EP+508tcZzlCdBehsF_z6bi)^#A9(-3>J^U;oWc)8UatkV!`(h z3b7{P#nXbB?Dw`1$rF_zlZj~92$!e3iWZl)t1VF zp+DUCsJ1jTRSaQ*At@}EaFKe%J1&8d-2J&By&~cb?MsOODGDcv33KH_NF?)PdZLgU zG*7^zktuj0gM}lpn0PFiio;Q-;12|lPNH6Ql^DbH@BoU-QJhlWDf^YLn6MV!z3kjC!OQy3}|10iySl<7WGYsMk zM&BI&)htU#NC)b-A6g#~J`4{eLMBFnj7H3J(l7uRPx&$Fp~}8%;XQB|%g%Fdi~?_9hJ9Y9ZiWz+!%MbKGM#qtIf6O>|mGw>i1- zq-litUu?{UPbV)=cUQjXy3$z3_{e1@u4oi!MKNcnxT zxi*8{Kbe2K=&p9*zH{PZboyM|bXgRGXfop}BoF!a?Jt=vnSfg@HdOH!57zdi+!XKm z-A64qxv{ytC55XMhsy}i?S1g6<`K=%HM|+J;kG&tQd7OT~Ag8zDb2v&f4e2m6vPx*DH#QD~X@8 z`Z9F`A(aZev0v8y_52^sP==Wv?jAX(KR<8_xYXuM`q$MHYcLt$Ibr+g18bHwc-!EW zS*>mEs$ZRHpE=8qZddmV>>9J4(UFDz=E*kx_D%b`X%||zOrQb}tt`2H;E_dZ`}z}U zZMVH9KG75l7SmHQiyY;3a$JIyCcc4LZbpsL9{Q!Hci`;0tgk}{BO*u6(pLQXBS*3$ zBznbW{&#UH&S61i|1R7<&$@apZS;y`Q{RRl>+mf`)bqOOV?((lV*ct)iWT3w?~&fz z7kNGEM333M)oTNffuY@|LknGI z$rWJy+4}~^9-SNy&k-%Zb5xf-pCWWrr<=dBzJ^=0p*RG*2wK&^D60%C_{oKXEurU? zJ$YSasOe+F;RV4%guCP37FM-9%|TjDe6DNz;U3d94=3b4K}!6qY>s9|7rn921ccmw zy1y7~WL*FHLCz3r%D#84`dqhW7E{Vk9eFwvYn@|BhMFC=^+I4mwVk7p8rrH{-ySw- z`8q58Y3hL6*ZqwwyM=j|dKC(FF}uX#@FL!Q)R2B0b9KJjChg1dq3?RraRmUSlH0`<=>EU|yn z{%7~&Few*lKpDU8*w04@oyOud8F9IrtI!%oPQ$V(FM|egQYRrfYgJ&x7%g_!{hH$` z7o`sE_cG*@QOf`;&BNFqTD-58U-*KVe2RZlQr6foxQ8{>9eG~oa<(+-Nde%jD#a*{ z`Kuk9DJm1-;gy3STJNLO{=UW>c0f-U;ll;a6p+^9Ivy zX*zAmlr>WR&RHAcuHr+p={|6VrS;30BKc;!!dyZ&`Sl*d%hoZ8q9 Y7!v)$-59Qc^?ww8tewoiGh(y;16!JrJ^%m! diff --git a/src/native/linux/icon/iconunix.go b/src/native/linux/icon/iconunix.go deleted file mode 100644 index f062f5e8..00000000 --- a/src/native/linux/icon/iconunix.go +++ /dev/null @@ -1,196 +0,0 @@ -//go:build linux || freebsd || openbsd || netbsd -// +build linux freebsd openbsd netbsd - -// File generated by 2goarray (http://github.com/cratonica/2goarray) - -package icon - -var Data []byte = []byte{ - 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, - 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x20, - 0x08, 0x06, 0x00, 0x00, 0x00, 0x73, 0x7a, 0x7a, 0xf4, 0x00, 0x00, 0x00, - 0x19, 0x74, 0x45, 0x58, 0x74, 0x53, 0x6f, 0x66, 0x74, 0x77, 0x61, 0x72, - 0x65, 0x00, 0x41, 0x64, 0x6f, 0x62, 0x65, 0x20, 0x49, 0x6d, 0x61, 0x67, - 0x65, 0x52, 0x65, 0x61, 0x64, 0x79, 0x71, 0xc9, 0x65, 0x3c, 0x00, 0x00, - 0x03, 0x66, 0x69, 0x54, 0x58, 0x74, 0x58, 0x4d, 0x4c, 0x3a, 0x63, 0x6f, - 0x6d, 0x2e, 0x61, 0x64, 0x6f, 0x62, 0x65, 0x2e, 0x78, 0x6d, 0x70, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x3c, 0x3f, 0x78, 0x70, 0x61, 0x63, 0x6b, 0x65, - 0x74, 0x20, 0x62, 0x65, 0x67, 0x69, 0x6e, 0x3d, 0x22, 0xef, 0xbb, 0xbf, - 0x22, 0x20, 0x69, 0x64, 0x3d, 0x22, 0x57, 0x35, 0x4d, 0x30, 0x4d, 0x70, - 0x43, 0x65, 0x68, 0x69, 0x48, 0x7a, 0x72, 0x65, 0x53, 0x7a, 0x4e, 0x54, - 0x63, 0x7a, 0x6b, 0x63, 0x39, 0x64, 0x22, 0x3f, 0x3e, 0x20, 0x3c, 0x78, - 0x3a, 0x78, 0x6d, 0x70, 0x6d, 0x65, 0x74, 0x61, 0x20, 0x78, 0x6d, 0x6c, - 0x6e, 0x73, 0x3a, 0x78, 0x3d, 0x22, 0x61, 0x64, 0x6f, 0x62, 0x65, 0x3a, - 0x6e, 0x73, 0x3a, 0x6d, 0x65, 0x74, 0x61, 0x2f, 0x22, 0x20, 0x78, 0x3a, - 0x78, 0x6d, 0x70, 0x74, 0x6b, 0x3d, 0x22, 0x41, 0x64, 0x6f, 0x62, 0x65, - 0x20, 0x58, 0x4d, 0x50, 0x20, 0x43, 0x6f, 0x72, 0x65, 0x20, 0x35, 0x2e, - 0x30, 0x2d, 0x63, 0x30, 0x36, 0x30, 0x20, 0x36, 0x31, 0x2e, 0x31, 0x33, - 0x34, 0x37, 0x37, 0x37, 0x2c, 0x20, 0x32, 0x30, 0x31, 0x30, 0x2f, 0x30, - 0x32, 0x2f, 0x31, 0x32, 0x2d, 0x31, 0x37, 0x3a, 0x33, 0x32, 0x3a, 0x30, - 0x30, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x22, 0x3e, 0x20, - 0x3c, 0x72, 0x64, 0x66, 0x3a, 0x52, 0x44, 0x46, 0x20, 0x78, 0x6d, 0x6c, - 0x6e, 0x73, 0x3a, 0x72, 0x64, 0x66, 0x3d, 0x22, 0x68, 0x74, 0x74, 0x70, - 0x3a, 0x2f, 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x77, 0x33, 0x2e, 0x6f, 0x72, - 0x67, 0x2f, 0x31, 0x39, 0x39, 0x39, 0x2f, 0x30, 0x32, 0x2f, 0x32, 0x32, - 0x2d, 0x72, 0x64, 0x66, 0x2d, 0x73, 0x79, 0x6e, 0x74, 0x61, 0x78, 0x2d, - 0x6e, 0x73, 0x23, 0x22, 0x3e, 0x20, 0x3c, 0x72, 0x64, 0x66, 0x3a, 0x44, - 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x72, - 0x64, 0x66, 0x3a, 0x61, 0x62, 0x6f, 0x75, 0x74, 0x3d, 0x22, 0x22, 0x20, - 0x78, 0x6d, 0x6c, 0x6e, 0x73, 0x3a, 0x78, 0x6d, 0x70, 0x4d, 0x4d, 0x3d, - 0x22, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x6e, 0x73, 0x2e, 0x61, - 0x64, 0x6f, 0x62, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x78, 0x61, 0x70, - 0x2f, 0x31, 0x2e, 0x30, 0x2f, 0x6d, 0x6d, 0x2f, 0x22, 0x20, 0x78, 0x6d, - 0x6c, 0x6e, 0x73, 0x3a, 0x73, 0x74, 0x52, 0x65, 0x66, 0x3d, 0x22, 0x68, - 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x6e, 0x73, 0x2e, 0x61, 0x64, 0x6f, - 0x62, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x78, 0x61, 0x70, 0x2f, 0x31, - 0x2e, 0x30, 0x2f, 0x73, 0x54, 0x79, 0x70, 0x65, 0x2f, 0x52, 0x65, 0x73, - 0x6f, 0x75, 0x72, 0x63, 0x65, 0x52, 0x65, 0x66, 0x23, 0x22, 0x20, 0x78, - 0x6d, 0x6c, 0x6e, 0x73, 0x3a, 0x78, 0x6d, 0x70, 0x3d, 0x22, 0x68, 0x74, - 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x6e, 0x73, 0x2e, 0x61, 0x64, 0x6f, 0x62, - 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x78, 0x61, 0x70, 0x2f, 0x31, 0x2e, - 0x30, 0x2f, 0x22, 0x20, 0x78, 0x6d, 0x70, 0x4d, 0x4d, 0x3a, 0x4f, 0x72, - 0x69, 0x67, 0x69, 0x6e, 0x61, 0x6c, 0x44, 0x6f, 0x63, 0x75, 0x6d, 0x65, - 0x6e, 0x74, 0x49, 0x44, 0x3d, 0x22, 0x78, 0x6d, 0x70, 0x2e, 0x64, 0x69, - 0x64, 0x3a, 0x36, 0x37, 0x32, 0x34, 0x42, 0x45, 0x31, 0x35, 0x45, 0x44, - 0x32, 0x30, 0x36, 0x38, 0x31, 0x31, 0x38, 0x38, 0x43, 0x36, 0x46, 0x32, - 0x38, 0x31, 0x35, 0x44, 0x41, 0x33, 0x43, 0x35, 0x35, 0x35, 0x22, 0x20, - 0x78, 0x6d, 0x70, 0x4d, 0x4d, 0x3a, 0x44, 0x6f, 0x63, 0x75, 0x6d, 0x65, - 0x6e, 0x74, 0x49, 0x44, 0x3d, 0x22, 0x78, 0x6d, 0x70, 0x2e, 0x64, 0x69, - 0x64, 0x3a, 0x41, 0x33, 0x42, 0x34, 0x46, 0x42, 0x36, 0x36, 0x33, 0x41, - 0x41, 0x38, 0x31, 0x31, 0x45, 0x32, 0x42, 0x32, 0x43, 0x41, 0x39, 0x37, - 0x42, 0x44, 0x33, 0x34, 0x34, 0x31, 0x45, 0x46, 0x33, 0x32, 0x22, 0x20, - 0x78, 0x6d, 0x70, 0x4d, 0x4d, 0x3a, 0x49, 0x6e, 0x73, 0x74, 0x61, 0x6e, - 0x63, 0x65, 0x49, 0x44, 0x3d, 0x22, 0x78, 0x6d, 0x70, 0x2e, 0x69, 0x69, - 0x64, 0x3a, 0x41, 0x33, 0x42, 0x34, 0x46, 0x42, 0x36, 0x35, 0x33, 0x41, - 0x41, 0x38, 0x31, 0x31, 0x45, 0x32, 0x42, 0x32, 0x43, 0x41, 0x39, 0x37, - 0x42, 0x44, 0x33, 0x34, 0x34, 0x31, 0x45, 0x46, 0x33, 0x32, 0x22, 0x20, - 0x78, 0x6d, 0x70, 0x3a, 0x43, 0x72, 0x65, 0x61, 0x74, 0x6f, 0x72, 0x54, - 0x6f, 0x6f, 0x6c, 0x3d, 0x22, 0x41, 0x64, 0x6f, 0x62, 0x65, 0x20, 0x50, - 0x68, 0x6f, 0x74, 0x6f, 0x73, 0x68, 0x6f, 0x70, 0x20, 0x43, 0x53, 0x35, - 0x20, 0x4d, 0x61, 0x63, 0x69, 0x6e, 0x74, 0x6f, 0x73, 0x68, 0x22, 0x3e, - 0x20, 0x3c, 0x78, 0x6d, 0x70, 0x4d, 0x4d, 0x3a, 0x44, 0x65, 0x72, 0x69, - 0x76, 0x65, 0x64, 0x46, 0x72, 0x6f, 0x6d, 0x20, 0x73, 0x74, 0x52, 0x65, - 0x66, 0x3a, 0x69, 0x6e, 0x73, 0x74, 0x61, 0x6e, 0x63, 0x65, 0x49, 0x44, - 0x3d, 0x22, 0x78, 0x6d, 0x70, 0x2e, 0x69, 0x69, 0x64, 0x3a, 0x45, 0x36, - 0x38, 0x31, 0x34, 0x43, 0x36, 0x41, 0x45, 0x45, 0x32, 0x30, 0x36, 0x38, - 0x31, 0x31, 0x38, 0x38, 0x43, 0x36, 0x46, 0x32, 0x38, 0x31, 0x35, 0x44, - 0x41, 0x33, 0x43, 0x35, 0x35, 0x35, 0x22, 0x20, 0x73, 0x74, 0x52, 0x65, - 0x66, 0x3a, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65, 0x6e, 0x74, 0x49, 0x44, - 0x3d, 0x22, 0x78, 0x6d, 0x70, 0x2e, 0x64, 0x69, 0x64, 0x3a, 0x36, 0x37, - 0x32, 0x34, 0x42, 0x45, 0x31, 0x35, 0x45, 0x44, 0x32, 0x30, 0x36, 0x38, - 0x31, 0x31, 0x38, 0x38, 0x43, 0x36, 0x46, 0x32, 0x38, 0x31, 0x35, 0x44, - 0x41, 0x33, 0x43, 0x35, 0x35, 0x35, 0x22, 0x2f, 0x3e, 0x20, 0x3c, 0x2f, - 0x72, 0x64, 0x66, 0x3a, 0x44, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, - 0x69, 0x6f, 0x6e, 0x3e, 0x20, 0x3c, 0x2f, 0x72, 0x64, 0x66, 0x3a, 0x52, - 0x44, 0x46, 0x3e, 0x20, 0x3c, 0x2f, 0x78, 0x3a, 0x78, 0x6d, 0x70, 0x6d, - 0x65, 0x74, 0x61, 0x3e, 0x20, 0x3c, 0x3f, 0x78, 0x70, 0x61, 0x63, 0x6b, - 0x65, 0x74, 0x20, 0x65, 0x6e, 0x64, 0x3d, 0x22, 0x72, 0x22, 0x3f, 0x3e, - 0x5d, 0xed, 0x35, 0xe2, 0x00, 0x00, 0x04, 0xee, 0x49, 0x44, 0x41, 0x54, - 0x78, 0xda, 0xc4, 0x57, 0xcf, 0x6f, 0x55, 0x45, 0x18, 0x3d, 0xf3, 0xe3, - 0xfe, 0xea, 0x7b, 0xaf, 0xa5, 0x6d, 0x0a, 0xd8, 0x34, 0xbe, 0x16, 0x83, - 0x69, 0x8c, 0x2e, 0x04, 0xe2, 0x86, 0xb8, 0x70, 0xe1, 0x06, 0x35, 0x18, - 0x13, 0x5d, 0x60, 0x8c, 0xd1, 0x68, 0xe2, 0xca, 0xb8, 0x33, 0x31, 0xf1, - 0x6f, 0x70, 0x67, 0x5c, 0xb1, 0x62, 0xe1, 0x46, 0x42, 0x8c, 0x0b, 0xe3, - 0x46, 0x34, 0x25, 0x11, 0x41, 0x14, 0xa4, 0x24, 0xa4, 0x08, 0x58, 0x0a, - 0x29, 0x14, 0x0a, 0x6d, 0xe9, 0xeb, 0xbb, 0xef, 0xce, 0x9d, 0xf1, 0xcc, - 0xbd, 0xaf, 0xa5, 0x44, 0x63, 0x49, 0xee, 0x4b, 0x78, 0xc9, 0xf7, 0xee, - 0x9d, 0x3b, 0x33, 0x77, 0xce, 0x77, 0xbe, 0xf3, 0x7d, 0x33, 0x57, 0x38, - 0xe7, 0xf0, 0x38, 0x7f, 0x7a, 0xab, 0x01, 0xe2, 0xd9, 0x37, 0xff, 0xeb, - 0xb1, 0xa2, 0x7d, 0x46, 0xdb, 0xeb, 0x87, 0xd0, 0x8e, 0xd3, 0xbe, 0xf8, - 0xd7, 0x28, 0x6b, 0xe1, 0x2e, 0x7c, 0xf3, 0xbf, 0xef, 0x97, 0x5b, 0x42, - 0x34, 0x06, 0xf0, 0x2c, 0x6d, 0x36, 0xe0, 0x43, 0xda, 0x88, 0x90, 0xf2, - 0x90, 0xea, 0x6f, 0xbc, 0x0b, 0x21, 0x9e, 0x67, 0xfb, 0x8d, 0xa2, 0xcf, - 0x76, 0xc7, 0x70, 0x5e, 0xff, 0x40, 0x7f, 0x75, 0x06, 0xc6, 0x27, 0x9a, - 0xb8, 0x76, 0x63, 0xbe, 0x70, 0x53, 0xf0, 0x2f, 0xe7, 0x02, 0xd6, 0xda, - 0x71, 0x36, 0xaf, 0xd1, 0x0e, 0xcb, 0x38, 0x16, 0x5c, 0xf4, 0x7a, 0xbe, - 0xbc, 0xd2, 0x14, 0x71, 0x04, 0x19, 0x68, 0xf8, 0xb0, 0x4a, 0xda, 0x2e, - 0xce, 0xad, 0x0c, 0x60, 0xf7, 0x53, 0xbb, 0x90, 0x87, 0x11, 0x76, 0x8f, - 0xee, 0x40, 0xa8, 0x35, 0xfe, 0x9a, 0xbf, 0x35, 0x36, 0x73, 0xf9, 0xea, - 0x24, 0xd2, 0xf4, 0x20, 0x5d, 0x2d, 0xbc, 0x15, 0x49, 0x0c, 0x1d, 0x86, - 0xdf, 0x09, 0x25, 0x7f, 0x80, 0x14, 0xd3, 0x8e, 0x20, 0x93, 0x30, 0x44, - 0xa3, 0xd1, 0xd8, 0x12, 0x80, 0xdc, 0x3a, 0x02, 0x06, 0xa4, 0xba, 0xb0, - 0x5a, 0x12, 0x2b, 0x2e, 0xf4, 0x35, 0xb4, 0x3e, 0x58, 0x84, 0xde, 0xb3, - 0x91, 0xa6, 0x64, 0x86, 0xf7, 0x4a, 0xbe, 0x4a, 0xcf, 0x8f, 0xe4, 0x26, - 0x0f, 0x42, 0xa5, 0xf0, 0xcc, 0xe8, 0x13, 0x50, 0xfe, 0x79, 0x65, 0x11, - 0xf2, 0x1d, 0x86, 0x62, 0x9a, 0x9a, 0xbe, 0x88, 0xa7, 0x77, 0x8e, 0x04, - 0x9d, 0x34, 0x1b, 0x45, 0xda, 0x81, 0xf7, 0xde, 0x53, 0x9d, 0x77, 0x32, - 0x04, 0x49, 0x82, 0x88, 0x1e, 0x67, 0xb9, 0xa9, 0x37, 0xe2, 0x44, 0x3c, - 0x39, 0x3c, 0x84, 0xa8, 0x1b, 0x8a, 0xca, 0x00, 0xba, 0xbf, 0x28, 0x35, - 0xe6, 0xc0, 0x9f, 0x17, 0x2f, 0x7d, 0x20, 0xad, 0x6d, 0xc2, 0xe4, 0xd8, - 0x10, 0x45, 0x10, 0x20, 0xd0, 0x01, 0xfa, 0x09, 0xa2, 0x16, 0x85, 0x13, - 0xf5, 0x28, 0x3a, 0x1a, 0x28, 0x75, 0x98, 0x4b, 0x7f, 0xcf, 0xde, 0x56, - 0xe5, 0x10, 0xf0, 0xf7, 0x36, 0x6d, 0x4a, 0x0a, 0x71, 0x14, 0x4a, 0x1d, - 0xb0, 0x7e, 0xce, 0x3a, 0xb5, 0xbc, 0x2a, 0xea, 0x22, 0x50, 0x92, 0x11, - 0x90, 0xd0, 0x8a, 0xdc, 0x0b, 0xbc, 0xc2, 0x1e, 0x9f, 0x7b, 0xbf, 0xd0, - 0x3e, 0xea, 0x05, 0x80, 0x8f, 0x69, 0xfb, 0x7c, 0x76, 0x81, 0x8e, 0x23, - 0x75, 0xa5, 0x75, 0x31, 0x68, 0x0f, 0x80, 0x66, 0xac, 0x44, 0x9a, 0x09, - 0x38, 0x5b, 0xbe, 0x92, 0xdd, 0xcf, 0xd1, 0xde, 0xab, 0x1c, 0x82, 0x95, - 0x54, 0xdf, 0x58, 0x5a, 0xe3, 0xab, 0x3a, 0x0e, 0xf5, 0xc8, 0x61, 0x72, - 0x4c, 0x53, 0x5c, 0x0e, 0x27, 0x67, 0xb2, 0x62, 0x76, 0x28, 0x55, 0x51, - 0x97, 0xf6, 0x8c, 0x4b, 0xf4, 0x45, 0xc0, 0xad, 0x7b, 0xe4, 0xbd, 0x23, - 0xb0, 0xda, 0x21, 0x51, 0x10, 0x57, 0x2a, 0x03, 0x38, 0xb4, 0xf7, 0xef, - 0x99, 0xe0, 0x85, 0x35, 0x34, 0x87, 0x15, 0x26, 0x77, 0x0e, 0xa3, 0x39, - 0x5e, 0x73, 0xc7, 0x7e, 0x5a, 0xc5, 0x5b, 0x9f, 0xcf, 0x09, 0xd4, 0x15, - 0x19, 0x50, 0x94, 0x84, 0xc4, 0xfb, 0x2f, 0x25, 0x78, 0x6d, 0x9f, 0xc4, - 0xed, 0x85, 0x0c, 0x73, 0xf7, 0x52, 0xcc, 0x2e, 0x38, 0x5c, 0x5f, 0x74, - 0xe7, 0x2a, 0x03, 0xf8, 0xe4, 0xe5, 0x9b, 0xe7, 0xa0, 0xb7, 0xd1, 0xc9, - 0x41, 0x0a, 0xbf, 0x8f, 0x2e, 0xf7, 0x09, 0xa9, 0x38, 0x4d, 0xcc, 0x16, - 0x9e, 0xfb, 0xb0, 0x07, 0x5a, 0x50, 0x8b, 0x75, 0x48, 0x1d, 0x61, 0x64, - 0xb0, 0x8d, 0xed, 0x43, 0x6d, 0xec, 0x99, 0xa0, 0xfe, 0x4c, 0x6b, 0xba, - 0x32, 0x80, 0xd4, 0xec, 0xb8, 0x2c, 0x5c, 0x9d, 0x45, 0xbd, 0x21, 0x21, - 0x6b, 0x2c, 0x46, 0x35, 0xa6, 0x9d, 0x7d, 0xa0, 0x01, 0x0a, 0x30, 0x24, - 0x0b, 0x51, 0x5c, 0x67, 0x23, 0x41, 0x26, 0x42, 0x6a, 0xc5, 0x9b, 0x36, - 0x70, 0xe1, 0xb5, 0xb0, 0x72, 0x1d, 0x08, 0x86, 0x66, 0xa1, 0xe2, 0x25, - 0xe8, 0x81, 0x41, 0xa1, 0x6a, 0x64, 0xa0, 0x1f, 0x41, 0xdc, 0x59, 0xef, - 0xa5, 0xfa, 0x15, 0x42, 0x0f, 0xa2, 0x00, 0xe0, 0x59, 0x08, 0xe0, 0xf8, - 0xcc, 0x09, 0x71, 0x9b, 0x20, 0x66, 0xab, 0xd7, 0x01, 0x99, 0xdc, 0xa4, - 0xe7, 0x57, 0x84, 0x6e, 0x0c, 0xd2, 0x98, 0xf7, 0x83, 0xf4, 0x76, 0x6d, - 0x23, 0x7f, 0x7c, 0x0a, 0xfa, 0x10, 0xc4, 0x31, 0xfb, 0x14, 0x37, 0x1f, - 0x4d, 0xf1, 0x51, 0x13, 0xac, 0x42, 0x97, 0x9d, 0x50, 0x8b, 0xd5, 0x2b, - 0x61, 0x30, 0x90, 0x41, 0x86, 0x97, 0xe8, 0xfd, 0x9e, 0x02, 0x80, 0xda, - 0x46, 0x6f, 0x57, 0x8b, 0x52, 0xe0, 0x33, 0xd3, 0xe7, 0x3f, 0x0b, 0x0f, - 0xa2, 0x64, 0x80, 0x8d, 0x3a, 0x84, 0x66, 0x85, 0x2c, 0xc2, 0x93, 0xcf, - 0x08, 0x17, 0xd9, 0xea, 0x75, 0x40, 0x51, 0x78, 0x32, 0xfa, 0x83, 0x61, - 0x80, 0xf0, 0xf7, 0x5c, 0x24, 0x8c, 0x06, 0x20, 0x65, 0x39, 0xd5, 0xd7, - 0xfb, 0x52, 0x03, 0x04, 0xa7, 0xfd, 0xd8, 0x84, 0xe0, 0x22, 0xee, 0x1d, - 0xd1, 0x19, 0x7f, 0xad, 0xce, 0x80, 0xf2, 0x2f, 0x91, 0x67, 0x79, 0xe3, - 0xe9, 0xf0, 0x60, 0x10, 0x84, 0x0d, 0x36, 0x65, 0xb1, 0x1f, 0x48, 0xd1, - 0x65, 0x20, 0x68, 0xa0, 0x18, 0x23, 0x03, 0x7f, 0x65, 0x47, 0x78, 0x1e, - 0xc2, 0x55, 0xdf, 0x0d, 0xfd, 0x82, 0x7c, 0xe9, 0x79, 0xde, 0x2d, 0x95, - 0xdb, 0xaf, 0x45, 0x18, 0xf8, 0xf4, 0x2b, 0xa7, 0x7a, 0x22, 0x22, 0xb6, - 0x83, 0x90, 0x0b, 0x93, 0xfb, 0x32, 0x39, 0xe4, 0x02, 0x41, 0x9c, 0x2f, - 0xc0, 0xf4, 0xa0, 0x14, 0x7b, 0x4f, 0xe7, 0xe0, 0xb2, 0x0b, 0xb0, 0x54, - 0x7f, 0xbe, 0x46, 0x00, 0xe9, 0x03, 0x00, 0x3e, 0x04, 0xdc, 0xf9, 0x22, - 0xc5, 0xca, 0xc8, 0x7e, 0xe7, 0x7c, 0xbd, 0xce, 0x09, 0x58, 0x2c, 0x6c, - 0xe4, 0x6a, 0x25, 0x00, 0x8e, 0x2f, 0x76, 0xc6, 0x72, 0xe3, 0x3f, 0xed, - 0xf2, 0x55, 0x96, 0xe4, 0x65, 0xc4, 0xaa, 0xc5, 0xb8, 0xcb, 0x82, 0x10, - 0x81, 0x32, 0x0b, 0x22, 0xc1, 0xcc, 0x30, 0x34, 0xdb, 0x26, 0x88, 0xec, - 0xd7, 0x62, 0x2f, 0x76, 0xb6, 0x3a, 0x00, 0x97, 0x67, 0xa5, 0x99, 0xd6, - 0x94, 0x33, 0xcb, 0x04, 0x70, 0x17, 0x7d, 0x62, 0x85, 0x27, 0x9e, 0x2e, - 0x80, 0x42, 0x84, 0xac, 0x07, 0xee, 0x3e, 0x01, 0xac, 0x70, 0x6c, 0xcb, - 0x33, 0x71, 0x82, 0x13, 0x50, 0x58, 0xe5, 0x3a, 0x60, 0x56, 0xd7, 0xa1, - 0x9c, 0x76, 0xb6, 0x73, 0x1f, 0xc2, 0xd4, 0x7d, 0x75, 0xeb, 0x5b, 0x07, - 0x00, 0x0f, 0x80, 0xd9, 0x60, 0xb9, 0xb8, 0x3f, 0xc0, 0x9a, 0xb5, 0x3b, - 0x44, 0x71, 0xa6, 0x67, 0xc7, 0x72, 0x97, 0xaf, 0xac, 0xdf, 0x5e, 0x45, - 0xee, 0xce, 0x5a, 0xb4, 0xf7, 0x2b, 0xab, 0x91, 0x6c, 0xe8, 0x8b, 0x00, - 0x28, 0x7a, 0x91, 0x2f, 0x77, 0x99, 0x4a, 0x7f, 0x23, 0xb1, 0x37, 0x7a, - 0x06, 0x00, 0xd9, 0xbd, 0x8d, 0x53, 0xbe, 0x3f, 0x98, 0x38, 0xdb, 0xda, - 0xaf, 0x84, 0x46, 0xac, 0x5d, 0xa9, 0x7a, 0x86, 0x40, 0x4b, 0x02, 0x30, - 0x4b, 0x3c, 0x2d, 0xb3, 0xfc, 0x5b, 0x73, 0xbc, 0xa7, 0x1f, 0x26, 0x96, - 0x9e, 0x6d, 0xa2, 0xe3, 0x47, 0x61, 0xe5, 0xa7, 0x42, 0x05, 0x3c, 0x03, - 0x5a, 0x94, 0x25, 0xcf, 0x33, 0xc0, 0xfb, 0x9c, 0x59, 0x9a, 0x53, 0xac, - 0xce, 0xfd, 0xfc, 0x28, 0xea, 0x7f, 0xf4, 0x10, 0x64, 0xf3, 0x9b, 0x9b, - 0xa7, 0xb8, 0xc9, 0xcc, 0x91, 0x81, 0xb1, 0x7a, 0x68, 0x4a, 0x00, 0xc2, - 0xef, 0x86, 0x06, 0x8a, 0xa1, 0xe2, 0xfa, 0x97, 0xa8, 0x86, 0xdf, 0x7b, - 0xca, 0x80, 0xcb, 0x16, 0x36, 0x37, 0x17, 0x73, 0x67, 0x4e, 0xf2, 0x14, - 0x34, 0x56, 0x8f, 0x92, 0xf2, 0x7c, 0x40, 0x6f, 0x7d, 0x0d, 0x10, 0xf9, - 0x5d, 0x8e, 0x75, 0x27, 0x18, 0x8c, 0x56, 0x6f, 0x01, 0x98, 0xf9, 0x87, - 0xdb, 0xce, 0x1c, 0x81, 0x69, 0xbf, 0x58, 0x8b, 0x9b, 0xdb, 0x81, 0x7a, - 0x91, 0xc9, 0xa1, 0x6c, 0x51, 0x03, 0x77, 0xce, 0xb8, 0x5c, 0x7f, 0xe5, - 0x8a, 0xcf, 0xc6, 0x1e, 0x02, 0x78, 0x38, 0x9e, 0xfe, 0xde, 0x1c, 0x83, - 0x5d, 0x38, 0x55, 0x0b, 0x87, 0x5f, 0x67, 0xfb, 0x1d, 0x3e, 0x68, 0x2b, - 0x61, 0xbe, 0x84, 0x6b, 0x7f, 0xeb, 0x50, 0x6b, 0x97, 0x63, 0x1e, 0xfd, - 0x8b, 0x5b, 0x3c, 0xee, 0xcf, 0xf3, 0x7f, 0x04, 0x18, 0x00, 0xe0, 0x6e, - 0xdd, 0x63, 0x24, 0x57, 0x80, 0x6f, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, - 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82, -} diff --git a/src/native/linux/icon/logo.png b/src/native/linux/icon/logo.png deleted file mode 100644 index 9d332bd085471035d18adf52b49fa8ca1fa49982..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1851 zcmV-B2gLY^P)Px#1ZP1_K>z@;j|==^1poj532;bRa{vGi!vFvd!vV){sAK>D2GB`FK~z{rl~+ql z6KfQnVtl`d#&{L*Hj2+8HWsKngwd)2W3;^CqB0Rhomu5)(;`UAWM& zpc1JTjg`0cscm8Q-I%R0|BquZT|L6bz^Plsb$4Eq!dHeS5 z!1nFiRgcG`Ub=KC%wv)T@i>LUa{TkRi8?xDI&>&VE?1C7N4;Y3#EBDuxpU{z?%li9 zQ>RX;EO++o*|3;QAySk8u^#*OsnpjOm`zbDqWXFtWoBXpnD>}BkI3&=q&<7~(18O7 zqKVDUpFbbUWFaC=!#IiCyIiVRoZy{12^+H__PCfOiM0g-9#SJ$t6`IGf&dcrx>C^7BJ<^CoZ3+M5)j>1n5s ztE}X`1yOHrn61Gsi11M zwCFdrVg}*k$B#7tdOTY0NbvRR>?UghiX$dr=3TppYHHZTCZDl;4k04ety>2%vjVV` z$SZ=AbBDvhQ80V*q6-%cUsb3kcyXZb@g(_*2*S_xFdI zo13kZSD5`34Em|9O(Y4^VrE&^6P6VX4h||oULU_D6&D+!2T)EROp#~sDG^%^$`~%@ z!*ejWb?a8@?C$1Rm#Mtm*nnA4O#Z8=9RDN!I)fPLrnNOS;m*#^5Kkre)~#EH0+%me z=9m=|Y(^2|$^OgBBl`I>ZO%_7gn{8sn~nY(8}sMq=ZpQBIa9J5GK4Tp_WJc}J@k;l zrs`v{Kc!Ef48UY%J+;lBKN2kgVApQeBKo+R_K$Q!KkY%Bo-$axmF1O0@1m1 z=Fn?s2pv0iOa~t;^6J$qBh$9Ewds=?85smD_$`vhuY+&%W^*#b;aKmzc=2L{4~Ml) zsW>AiC&!M1#YbUb;pG13&!RBO$a+^oi-Xsi}z`K745El=+=nTYYFQHB7nPvlk;w*g0f`AhVXKL`4HO z^ge)N*yVDGcPlO^C_u9#Qn|_0G&Q9p+-XUi=dpufs-7NUs?pI=Q<6G)@}%%R3XkvL z!Gp8*1sqY_wmo_B#N-}J(z{f=3g*23<%Xt2A)E6&a zC{$QzF44&h#o=e=9$Go%j7Vx%%`V}xf9={eANP-^PoEBR`%18no|^$a4GSs7M`#;= z&KhFO_|r-#kEe_upCAL5Ke@#7I1{R2q_VP-B>XO^w0gBlKYqvp3l2v_6qDJ)B~Cmq zfa~#?6+kG*MRYC6wy90c%@J-(yfkNywwQ6i?DPF{@nPb>TqJ_1 zSUXFmyL0D`$fO*?D_5?>9oLJZWaCL_<&;WFS|!v4sjTc@TTjm*kN+<5?er$Yf^OWn p;d}r7eMMGQmIOuq?&|9L{68D9fzs!Q)L8%k002ovPDHLkV1f#YjBfw{ diff --git a/src/native/linux/internal/DbusMenu.xml b/src/native/linux/internal/DbusMenu.xml deleted file mode 100644 index db695984..00000000 --- a/src/native/linux/internal/DbusMenu.xml +++ /dev/null @@ -1,60 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/native/linux/internal/StatusNotifierItem.xml b/src/native/linux/internal/StatusNotifierItem.xml deleted file mode 100644 index 1093d3d1..00000000 --- a/src/native/linux/internal/StatusNotifierItem.xml +++ /dev/null @@ -1,85 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/native/linux/internal/generated/menu/dbus_menu.go b/src/native/linux/internal/generated/menu/dbus_menu.go deleted file mode 100644 index 1b896d3e..00000000 --- a/src/native/linux/internal/generated/menu/dbus_menu.go +++ /dev/null @@ -1,484 +0,0 @@ -// Code generated by dbus-codegen-go DO NOT EDIT. -package menu - -import ( - "context" - "errors" - "fmt" - - "github.com/godbus/dbus/v5" - "github.com/godbus/dbus/v5/introspect" -) - -var ( - // Introspection for com.canonical.dbusmenu - IntrospectDataDbusmenu = introspect.Interface{ - Name: "com.canonical.dbusmenu", - Methods: []introspect.Method{{Name: "GetLayout", Args: []introspect.Arg{ - {Name: "parentId", Type: "i", Direction: "in"}, - {Name: "recursionDepth", Type: "i", Direction: "in"}, - {Name: "propertyNames", Type: "as", Direction: "in"}, - {Name: "revision", Type: "u", Direction: "out"}, - {Name: "layout", Type: "(ia{sv}av)", Direction: "out"}, - }}, - {Name: "GetGroupProperties", Args: []introspect.Arg{ - {Name: "ids", Type: "ai", Direction: "in"}, - {Name: "propertyNames", Type: "as", Direction: "in"}, - {Name: "properties", Type: "a(ia{sv})", Direction: "out"}, - }}, - {Name: "GetProperty", Args: []introspect.Arg{ - {Name: "id", Type: "i", Direction: "in"}, - {Name: "name", Type: "s", Direction: "in"}, - {Name: "value", Type: "v", Direction: "out"}, - }}, - {Name: "Event", Args: []introspect.Arg{ - {Name: "id", Type: "i", Direction: "in"}, - {Name: "eventId", Type: "s", Direction: "in"}, - {Name: "data", Type: "v", Direction: "in"}, - {Name: "timestamp", Type: "u", Direction: "in"}, - }}, - {Name: "EventGroup", Args: []introspect.Arg{ - {Name: "events", Type: "a(isvu)", Direction: "in"}, - {Name: "idErrors", Type: "ai", Direction: "out"}, - }}, - {Name: "AboutToShow", Args: []introspect.Arg{ - {Name: "id", Type: "i", Direction: "in"}, - {Name: "needUpdate", Type: "b", Direction: "out"}, - }}, - {Name: "AboutToShowGroup", Args: []introspect.Arg{ - {Name: "ids", Type: "ai", Direction: "in"}, - {Name: "updatesNeeded", Type: "ai", Direction: "out"}, - {Name: "idErrors", Type: "ai", Direction: "out"}, - }}, - }, - Signals: []introspect.Signal{{Name: "ItemsPropertiesUpdated", Args: []introspect.Arg{ - {Name: "updatedProps", Type: "a(ia{sv})", Direction: "out"}, - {Name: "removedProps", Type: "a(ias)", Direction: "out"}, - }}, - {Name: "LayoutUpdated", Args: []introspect.Arg{ - {Name: "revision", Type: "u", Direction: "out"}, - {Name: "parent", Type: "i", Direction: "out"}, - }}, - {Name: "ItemActivationRequested", Args: []introspect.Arg{ - {Name: "id", Type: "i", Direction: "out"}, - {Name: "timestamp", Type: "u", Direction: "out"}, - }}, - }, - Properties: []introspect.Property{{Name: "Version", Type: "u", Access: "read"}, - {Name: "TextDirection", Type: "s", Access: "read"}, - {Name: "Status", Type: "s", Access: "read"}, - {Name: "IconThemePath", Type: "as", Access: "read"}, - }, - Annotations: []introspect.Annotation{}, - } -) - -// Signal is a common interface for all signals. -type Signal interface { - Name() string - Interface() string - Sender() string - - path() dbus.ObjectPath - values() []interface{} -} - -// Emit sends the given signal to the bus. -func Emit(conn *dbus.Conn, s Signal) error { - return conn.Emit(s.path(), s.Interface()+"."+s.Name(), s.values()...) -} - -// ErrUnknownSignal is returned by LookupSignal when a signal cannot be resolved. -var ErrUnknownSignal = errors.New("unknown signal") - -// LookupSignal converts the given raw D-Bus signal with variable body -// into one with typed structured body or returns ErrUnknownSignal error. -func LookupSignal(signal *dbus.Signal) (Signal, error) { - switch signal.Name { - case InterfaceDbusmenu + "." + "ItemsPropertiesUpdated": - v0, ok := signal.Body[0].([]struct { - V0 int32 - V1 map[string]dbus.Variant - }) - if !ok { - return nil, fmt.Errorf("prop .UpdatedProps is %T, not []struct {V0 int32;V1 map[string]dbus.Variant}", signal.Body[0]) - } - v1, ok := signal.Body[1].([]struct { - V0 int32 - V1 []string - }) - if !ok { - return nil, fmt.Errorf("prop .RemovedProps is %T, not []struct {V0 int32;V1 []string}", signal.Body[1]) - } - return &Dbusmenu_ItemsPropertiesUpdatedSignal{ - sender: signal.Sender, - Path: signal.Path, - Body: &Dbusmenu_ItemsPropertiesUpdatedSignalBody{ - UpdatedProps: v0, - RemovedProps: v1, - }, - }, nil - case InterfaceDbusmenu + "." + "LayoutUpdated": - v0, ok := signal.Body[0].(uint32) - if !ok { - return nil, fmt.Errorf("prop .Revision is %T, not uint32", signal.Body[0]) - } - v1, ok := signal.Body[1].(int32) - if !ok { - return nil, fmt.Errorf("prop .Parent is %T, not int32", signal.Body[1]) - } - return &Dbusmenu_LayoutUpdatedSignal{ - sender: signal.Sender, - Path: signal.Path, - Body: &Dbusmenu_LayoutUpdatedSignalBody{ - Revision: v0, - Parent: v1, - }, - }, nil - case InterfaceDbusmenu + "." + "ItemActivationRequested": - v0, ok := signal.Body[0].(int32) - if !ok { - return nil, fmt.Errorf("prop .Id is %T, not int32", signal.Body[0]) - } - v1, ok := signal.Body[1].(uint32) - if !ok { - return nil, fmt.Errorf("prop .Timestamp is %T, not uint32", signal.Body[1]) - } - return &Dbusmenu_ItemActivationRequestedSignal{ - sender: signal.Sender, - Path: signal.Path, - Body: &Dbusmenu_ItemActivationRequestedSignalBody{ - Id: v0, - Timestamp: v1, - }, - }, nil - default: - return nil, ErrUnknownSignal - } -} - -// AddMatchSignal registers a match rule for the given signal, -// opts are appended to the automatically generated signal's rules. -func AddMatchSignal(conn *dbus.Conn, s Signal, opts ...dbus.MatchOption) error { - return conn.AddMatchSignal(append([]dbus.MatchOption{ - dbus.WithMatchInterface(s.Interface()), - dbus.WithMatchMember(s.Name()), - }, opts...)...) -} - -// RemoveMatchSignal unregisters the previously registered subscription. -func RemoveMatchSignal(conn *dbus.Conn, s Signal, opts ...dbus.MatchOption) error { - return conn.RemoveMatchSignal(append([]dbus.MatchOption{ - dbus.WithMatchInterface(s.Interface()), - dbus.WithMatchMember(s.Name()), - }, opts...)...) -} - -// Interface name constants. -const ( - InterfaceDbusmenu = "com.canonical.dbusmenu" -) - -// Dbusmenuer is com.canonical.dbusmenu interface. -type Dbusmenuer interface { - // GetLayout is com.canonical.dbusmenu.GetLayout method. - GetLayout(parentId int32, recursionDepth int32, propertyNames []string) (revision uint32, layout struct { - V0 int32 - V1 map[string]dbus.Variant - V2 []dbus.Variant - }, err *dbus.Error) - // GetGroupProperties is com.canonical.dbusmenu.GetGroupProperties method. - GetGroupProperties(ids []int32, propertyNames []string) (properties []struct { - V0 int32 - V1 map[string]dbus.Variant - }, err *dbus.Error) - // GetProperty is com.canonical.dbusmenu.GetProperty method. - GetProperty(id int32, name string) (value dbus.Variant, err *dbus.Error) - // Event is com.canonical.dbusmenu.Event method. - Event(id int32, eventId string, data dbus.Variant, timestamp uint32) (err *dbus.Error) - // EventGroup is com.canonical.dbusmenu.EventGroup method. - EventGroup(events []struct { - V0 int32 - V1 string - V2 dbus.Variant - V3 uint32 - }) (idErrors []int32, err *dbus.Error) - // AboutToShow is com.canonical.dbusmenu.AboutToShow method. - AboutToShow(id int32) (needUpdate bool, err *dbus.Error) - // AboutToShowGroup is com.canonical.dbusmenu.AboutToShowGroup method. - AboutToShowGroup(ids []int32) (updatesNeeded []int32, idErrors []int32, err *dbus.Error) -} - -// ExportDbusmenu exports the given object that implements com.canonical.dbusmenu on the bus. -func ExportDbusmenu(conn *dbus.Conn, path dbus.ObjectPath, v Dbusmenuer) error { - return conn.ExportSubtreeMethodTable(map[string]interface{}{ - "GetLayout": v.GetLayout, - "GetGroupProperties": v.GetGroupProperties, - "GetProperty": v.GetProperty, - "Event": v.Event, - "EventGroup": v.EventGroup, - "AboutToShow": v.AboutToShow, - "AboutToShowGroup": v.AboutToShowGroup, - }, path, InterfaceDbusmenu) -} - -// UnexportDbusmenu unexports com.canonical.dbusmenu interface on the named path. -func UnexportDbusmenu(conn *dbus.Conn, path dbus.ObjectPath) error { - return conn.Export(nil, path, InterfaceDbusmenu) -} - -// UnimplementedDbusmenu can be embedded to have forward compatible server implementations. -type UnimplementedDbusmenu struct{} - -func (*UnimplementedDbusmenu) iface() string { - return InterfaceDbusmenu -} - -func (*UnimplementedDbusmenu) GetLayout(parentId int32, recursionDepth int32, propertyNames []string) (revision uint32, layout struct { - V0 int32 - V1 map[string]dbus.Variant - V2 []dbus.Variant -}, err *dbus.Error) { - err = &dbus.ErrMsgUnknownMethod - return -} - -func (*UnimplementedDbusmenu) GetGroupProperties(ids []int32, propertyNames []string) (properties []struct { - V0 int32 - V1 map[string]dbus.Variant -}, err *dbus.Error) { - err = &dbus.ErrMsgUnknownMethod - return -} - -func (*UnimplementedDbusmenu) GetProperty(id int32, name string) (value dbus.Variant, err *dbus.Error) { - err = &dbus.ErrMsgUnknownMethod - return -} - -func (*UnimplementedDbusmenu) Event(id int32, eventId string, data dbus.Variant, timestamp uint32) (err *dbus.Error) { - err = &dbus.ErrMsgUnknownMethod - return -} - -func (*UnimplementedDbusmenu) EventGroup(events []struct { - V0 int32 - V1 string - V2 dbus.Variant - V3 uint32 -}) (idErrors []int32, err *dbus.Error) { - err = &dbus.ErrMsgUnknownMethod - return -} - -func (*UnimplementedDbusmenu) AboutToShow(id int32) (needUpdate bool, err *dbus.Error) { - err = &dbus.ErrMsgUnknownMethod - return -} - -func (*UnimplementedDbusmenu) AboutToShowGroup(ids []int32) (updatesNeeded []int32, idErrors []int32, err *dbus.Error) { - err = &dbus.ErrMsgUnknownMethod - return -} - -// NewDbusmenu creates and allocates com.canonical.dbusmenu. -func NewDbusmenu(object dbus.BusObject) *Dbusmenu { - return &Dbusmenu{object} -} - -// Dbusmenu implements com.canonical.dbusmenu D-Bus interface. -type Dbusmenu struct { - object dbus.BusObject -} - -// GetLayout calls com.canonical.dbusmenu.GetLayout method. -func (o *Dbusmenu) GetLayout(ctx context.Context, parentId int32, recursionDepth int32, propertyNames []string) (revision uint32, layout struct { - V0 int32 - V1 map[string]dbus.Variant - V2 []dbus.Variant -}, err error) { - err = o.object.CallWithContext(ctx, InterfaceDbusmenu+".GetLayout", 0, parentId, recursionDepth, propertyNames).Store(&revision, &layout) - return -} - -// GetGroupProperties calls com.canonical.dbusmenu.GetGroupProperties method. -func (o *Dbusmenu) GetGroupProperties(ctx context.Context, ids []int32, propertyNames []string) (properties []struct { - V0 int32 - V1 map[string]dbus.Variant -}, err error) { - err = o.object.CallWithContext(ctx, InterfaceDbusmenu+".GetGroupProperties", 0, ids, propertyNames).Store(&properties) - return -} - -// GetProperty calls com.canonical.dbusmenu.GetProperty method. -func (o *Dbusmenu) GetProperty(ctx context.Context, id int32, name string) (value dbus.Variant, err error) { - err = o.object.CallWithContext(ctx, InterfaceDbusmenu+".GetProperty", 0, id, name).Store(&value) - return -} - -// Event calls com.canonical.dbusmenu.Event method. -func (o *Dbusmenu) Event(ctx context.Context, id int32, eventId string, data dbus.Variant, timestamp uint32) (err error) { - err = o.object.CallWithContext(ctx, InterfaceDbusmenu+".Event", 0, id, eventId, data, timestamp).Store() - return -} - -// EventGroup calls com.canonical.dbusmenu.EventGroup method. -func (o *Dbusmenu) EventGroup(ctx context.Context, events []struct { - V0 int32 - V1 string - V2 dbus.Variant - V3 uint32 -}) (idErrors []int32, err error) { - err = o.object.CallWithContext(ctx, InterfaceDbusmenu+".EventGroup", 0, events).Store(&idErrors) - return -} - -// AboutToShow calls com.canonical.dbusmenu.AboutToShow method. -func (o *Dbusmenu) AboutToShow(ctx context.Context, id int32) (needUpdate bool, err error) { - err = o.object.CallWithContext(ctx, InterfaceDbusmenu+".AboutToShow", 0, id).Store(&needUpdate) - return -} - -// AboutToShowGroup calls com.canonical.dbusmenu.AboutToShowGroup method. -func (o *Dbusmenu) AboutToShowGroup(ctx context.Context, ids []int32) (updatesNeeded []int32, idErrors []int32, err error) { - err = o.object.CallWithContext(ctx, InterfaceDbusmenu+".AboutToShowGroup", 0, ids).Store(&updatesNeeded, &idErrors) - return -} - -// GetVersion gets com.canonical.dbusmenu.Version property. -func (o *Dbusmenu) GetVersion(ctx context.Context) (version uint32, err error) { - err = o.object.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, InterfaceDbusmenu, "Version").Store(&version) - return -} - -// GetTextDirection gets com.canonical.dbusmenu.TextDirection property. -func (o *Dbusmenu) GetTextDirection(ctx context.Context) (textDirection string, err error) { - err = o.object.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, InterfaceDbusmenu, "TextDirection").Store(&textDirection) - return -} - -// GetStatus gets com.canonical.dbusmenu.Status property. -func (o *Dbusmenu) GetStatus(ctx context.Context) (status string, err error) { - err = o.object.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, InterfaceDbusmenu, "Status").Store(&status) - return -} - -// GetIconThemePath gets com.canonical.dbusmenu.IconThemePath property. -func (o *Dbusmenu) GetIconThemePath(ctx context.Context) (iconThemePath []string, err error) { - err = o.object.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, InterfaceDbusmenu, "IconThemePath").Store(&iconThemePath) - return -} - -// Dbusmenu_ItemsPropertiesUpdatedSignal represents com.canonical.dbusmenu.ItemsPropertiesUpdated signal. -type Dbusmenu_ItemsPropertiesUpdatedSignal struct { - sender string - Path dbus.ObjectPath - Body *Dbusmenu_ItemsPropertiesUpdatedSignalBody -} - -// Name returns the signal's name. -func (s *Dbusmenu_ItemsPropertiesUpdatedSignal) Name() string { - return "ItemsPropertiesUpdated" -} - -// Interface returns the signal's interface. -func (s *Dbusmenu_ItemsPropertiesUpdatedSignal) Interface() string { - return InterfaceDbusmenu -} - -// Sender returns the signal's sender unique name. -func (s *Dbusmenu_ItemsPropertiesUpdatedSignal) Sender() string { - return s.sender -} - -func (s *Dbusmenu_ItemsPropertiesUpdatedSignal) path() dbus.ObjectPath { - return s.Path -} - -func (s *Dbusmenu_ItemsPropertiesUpdatedSignal) values() []interface{} { - return []interface{}{s.Body.UpdatedProps, s.Body.RemovedProps} -} - -// Dbusmenu_ItemsPropertiesUpdatedSignalBody is body container. -type Dbusmenu_ItemsPropertiesUpdatedSignalBody struct { - UpdatedProps []struct { - V0 int32 - V1 map[string]dbus.Variant - } - RemovedProps []struct { - V0 int32 - V1 []string - } -} - -// Dbusmenu_LayoutUpdatedSignal represents com.canonical.dbusmenu.LayoutUpdated signal. -type Dbusmenu_LayoutUpdatedSignal struct { - sender string - Path dbus.ObjectPath - Body *Dbusmenu_LayoutUpdatedSignalBody -} - -// Name returns the signal's name. -func (s *Dbusmenu_LayoutUpdatedSignal) Name() string { - return "LayoutUpdated" -} - -// Interface returns the signal's interface. -func (s *Dbusmenu_LayoutUpdatedSignal) Interface() string { - return InterfaceDbusmenu -} - -// Sender returns the signal's sender unique name. -func (s *Dbusmenu_LayoutUpdatedSignal) Sender() string { - return s.sender -} - -func (s *Dbusmenu_LayoutUpdatedSignal) path() dbus.ObjectPath { - return s.Path -} - -func (s *Dbusmenu_LayoutUpdatedSignal) values() []interface{} { - return []interface{}{s.Body.Revision, s.Body.Parent} -} - -// Dbusmenu_LayoutUpdatedSignalBody is body container. -type Dbusmenu_LayoutUpdatedSignalBody struct { - Revision uint32 - Parent int32 -} - -// Dbusmenu_ItemActivationRequestedSignal represents com.canonical.dbusmenu.ItemActivationRequested signal. -type Dbusmenu_ItemActivationRequestedSignal struct { - sender string - Path dbus.ObjectPath - Body *Dbusmenu_ItemActivationRequestedSignalBody -} - -// Name returns the signal's name. -func (s *Dbusmenu_ItemActivationRequestedSignal) Name() string { - return "ItemActivationRequested" -} - -// Interface returns the signal's interface. -func (s *Dbusmenu_ItemActivationRequestedSignal) Interface() string { - return InterfaceDbusmenu -} - -// Sender returns the signal's sender unique name. -func (s *Dbusmenu_ItemActivationRequestedSignal) Sender() string { - return s.sender -} - -func (s *Dbusmenu_ItemActivationRequestedSignal) path() dbus.ObjectPath { - return s.Path -} - -func (s *Dbusmenu_ItemActivationRequestedSignal) values() []interface{} { - return []interface{}{s.Body.Id, s.Body.Timestamp} -} - -// Dbusmenu_ItemActivationRequestedSignalBody is body container. -type Dbusmenu_ItemActivationRequestedSignalBody struct { - Id int32 - Timestamp uint32 -} diff --git a/src/native/linux/internal/generated/notifier/status_notifier_item.go b/src/native/linux/internal/generated/notifier/status_notifier_item.go deleted file mode 100644 index 230c40d6..00000000 --- a/src/native/linux/internal/generated/notifier/status_notifier_item.go +++ /dev/null @@ -1,633 +0,0 @@ -// Code generated by dbus-codegen-go DO NOT EDIT. -package notifier - -import ( - "context" - "errors" - "fmt" - - "github.com/godbus/dbus/v5" - "github.com/godbus/dbus/v5/introspect" -) - -var ( - // Introspection for org.kde.StatusNotifierItem - IntrospectDataStatusNotifierItem = introspect.Interface{ - Name: "org.kde.StatusNotifierItem", - Methods: []introspect.Method{{Name: "ContextMenu", Args: []introspect.Arg{ - {Name: "x", Type: "i", Direction: "in"}, - {Name: "y", Type: "i", Direction: "in"}, - }}, - {Name: "Activate", Args: []introspect.Arg{ - {Name: "x", Type: "i", Direction: "in"}, - {Name: "y", Type: "i", Direction: "in"}, - }}, - {Name: "SecondaryActivate", Args: []introspect.Arg{ - {Name: "x", Type: "i", Direction: "in"}, - {Name: "y", Type: "i", Direction: "in"}, - }}, - {Name: "Scroll", Args: []introspect.Arg{ - {Name: "delta", Type: "i", Direction: "in"}, - {Name: "orientation", Type: "s", Direction: "in"}, - }}, - }, - Signals: []introspect.Signal{{Name: "NewTitle"}, - {Name: "NewIcon"}, - {Name: "NewAttentionIcon"}, - {Name: "NewOverlayIcon"}, - {Name: "NewStatus", Args: []introspect.Arg{ - {Name: "status", Type: "s", Direction: ""}, - }}, - {Name: "NewIconThemePath", Args: []introspect.Arg{ - {Name: "icon_theme_path", Type: "s", Direction: "out"}, - }}, - {Name: "NewMenu"}, - }, - Properties: []introspect.Property{{Name: "Category", Type: "s", Access: "read"}, - {Name: "Id", Type: "s", Access: "read"}, - {Name: "Title", Type: "s", Access: "read"}, - {Name: "Status", Type: "s", Access: "read"}, - {Name: "WindowId", Type: "i", Access: "read"}, - {Name: "IconThemePath", Type: "s", Access: "read"}, - {Name: "Menu", Type: "o", Access: "read"}, - {Name: "ItemIsMenu", Type: "b", Access: "read"}, - {Name: "IconName", Type: "s", Access: "read"}, - {Name: "IconPixmap", Type: "a(iiay)", Access: "read", Annotations: []introspect.Annotation{ - {Name: "org.qtproject.QtDBus.QtTypeName", Value: "KDbusImageVector"}, - }}, - {Name: "OverlayIconName", Type: "s", Access: "read"}, - {Name: "OverlayIconPixmap", Type: "a(iiay)", Access: "read", Annotations: []introspect.Annotation{ - {Name: "org.qtproject.QtDBus.QtTypeName", Value: "KDbusImageVector"}, - }}, - {Name: "AttentionIconName", Type: "s", Access: "read"}, - {Name: "AttentionIconPixmap", Type: "a(iiay)", Access: "read", Annotations: []introspect.Annotation{ - {Name: "org.qtproject.QtDBus.QtTypeName", Value: "KDbusImageVector"}, - }}, - {Name: "AttentionMovieName", Type: "s", Access: "read"}, - {Name: "ToolTip", Type: "(sa(iiay)ss)", Access: "read", Annotations: []introspect.Annotation{ - {Name: "org.qtproject.QtDBus.QtTypeName", Value: "KDbusToolTipStruct"}, - }}, - }, - Annotations: []introspect.Annotation{}, - } -) - -// Signal is a common interface for all signals. -type Signal interface { - Name() string - Interface() string - Sender() string - - path() dbus.ObjectPath - values() []interface{} -} - -// Emit sends the given signal to the bus. -func Emit(conn *dbus.Conn, s Signal) error { - return conn.Emit(s.path(), s.Interface()+"."+s.Name(), s.values()...) -} - -// ErrUnknownSignal is returned by LookupSignal when a signal cannot be resolved. -var ErrUnknownSignal = errors.New("unknown signal") - -// LookupSignal converts the given raw D-Bus signal with variable body -// into one with typed structured body or returns ErrUnknownSignal error. -func LookupSignal(signal *dbus.Signal) (Signal, error) { - switch signal.Name { - case InterfaceStatusNotifierItem + "." + "NewTitle": - return &StatusNotifierItem_NewTitleSignal{ - sender: signal.Sender, - Path: signal.Path, - Body: &StatusNotifierItem_NewTitleSignalBody{}, - }, nil - case InterfaceStatusNotifierItem + "." + "NewIcon": - return &StatusNotifierItem_NewIconSignal{ - sender: signal.Sender, - Path: signal.Path, - Body: &StatusNotifierItem_NewIconSignalBody{}, - }, nil - case InterfaceStatusNotifierItem + "." + "NewAttentionIcon": - return &StatusNotifierItem_NewAttentionIconSignal{ - sender: signal.Sender, - Path: signal.Path, - Body: &StatusNotifierItem_NewAttentionIconSignalBody{}, - }, nil - case InterfaceStatusNotifierItem + "." + "NewOverlayIcon": - return &StatusNotifierItem_NewOverlayIconSignal{ - sender: signal.Sender, - Path: signal.Path, - Body: &StatusNotifierItem_NewOverlayIconSignalBody{}, - }, nil - case InterfaceStatusNotifierItem + "." + "NewStatus": - v0, ok := signal.Body[0].(string) - if !ok { - return nil, fmt.Errorf("prop .Status is %T, not string", signal.Body[0]) - } - return &StatusNotifierItem_NewStatusSignal{ - sender: signal.Sender, - Path: signal.Path, - Body: &StatusNotifierItem_NewStatusSignalBody{ - Status: v0, - }, - }, nil - case InterfaceStatusNotifierItem + "." + "NewIconThemePath": - v0, ok := signal.Body[0].(string) - if !ok { - return nil, fmt.Errorf("prop .IconThemePath is %T, not string", signal.Body[0]) - } - return &StatusNotifierItem_NewIconThemePathSignal{ - sender: signal.Sender, - Path: signal.Path, - Body: &StatusNotifierItem_NewIconThemePathSignalBody{ - IconThemePath: v0, - }, - }, nil - case InterfaceStatusNotifierItem + "." + "NewMenu": - return &StatusNotifierItem_NewMenuSignal{ - sender: signal.Sender, - Path: signal.Path, - Body: &StatusNotifierItem_NewMenuSignalBody{}, - }, nil - default: - return nil, ErrUnknownSignal - } -} - -// AddMatchSignal registers a match rule for the given signal, -// opts are appended to the automatically generated signal's rules. -func AddMatchSignal(conn *dbus.Conn, s Signal, opts ...dbus.MatchOption) error { - return conn.AddMatchSignal(append([]dbus.MatchOption{ - dbus.WithMatchInterface(s.Interface()), - dbus.WithMatchMember(s.Name()), - }, opts...)...) -} - -// RemoveMatchSignal unregisters the previously registered subscription. -func RemoveMatchSignal(conn *dbus.Conn, s Signal, opts ...dbus.MatchOption) error { - return conn.RemoveMatchSignal(append([]dbus.MatchOption{ - dbus.WithMatchInterface(s.Interface()), - dbus.WithMatchMember(s.Name()), - }, opts...)...) -} - -// Interface name constants. -const ( - InterfaceStatusNotifierItem = "org.kde.StatusNotifierItem" -) - -// StatusNotifierItemer is org.kde.StatusNotifierItem interface. -type StatusNotifierItemer interface { - // ContextMenu is org.kde.StatusNotifierItem.ContextMenu method. - ContextMenu(x int32, y int32) (err *dbus.Error) - // Activate is org.kde.StatusNotifierItem.Activate method. - Activate(x int32, y int32) (err *dbus.Error) - // SecondaryActivate is org.kde.StatusNotifierItem.SecondaryActivate method. - SecondaryActivate(x int32, y int32) (err *dbus.Error) - // Scroll is org.kde.StatusNotifierItem.Scroll method. - Scroll(delta int32, orientation string) (err *dbus.Error) -} - -// ExportStatusNotifierItem exports the given object that implements org.kde.StatusNotifierItem on the bus. -func ExportStatusNotifierItem(conn *dbus.Conn, path dbus.ObjectPath, v StatusNotifierItemer) error { - return conn.ExportSubtreeMethodTable(map[string]interface{}{ - "ContextMenu": v.ContextMenu, - "Activate": v.Activate, - "SecondaryActivate": v.SecondaryActivate, - "Scroll": v.Scroll, - }, path, InterfaceStatusNotifierItem) -} - -// UnexportStatusNotifierItem unexports org.kde.StatusNotifierItem interface on the named path. -func UnexportStatusNotifierItem(conn *dbus.Conn, path dbus.ObjectPath) error { - return conn.Export(nil, path, InterfaceStatusNotifierItem) -} - -// UnimplementedStatusNotifierItem can be embedded to have forward compatible server implementations. -type UnimplementedStatusNotifierItem struct{} - -func (*UnimplementedStatusNotifierItem) iface() string { - return InterfaceStatusNotifierItem -} - -func (*UnimplementedStatusNotifierItem) ContextMenu(x int32, y int32) (err *dbus.Error) { - err = &dbus.ErrMsgUnknownMethod - return -} - -func (*UnimplementedStatusNotifierItem) Activate(x int32, y int32) (err *dbus.Error) { - err = &dbus.ErrMsgUnknownMethod - return -} - -func (*UnimplementedStatusNotifierItem) SecondaryActivate(x int32, y int32) (err *dbus.Error) { - err = &dbus.ErrMsgUnknownMethod - return -} - -func (*UnimplementedStatusNotifierItem) Scroll(delta int32, orientation string) (err *dbus.Error) { - err = &dbus.ErrMsgUnknownMethod - return -} - -// NewStatusNotifierItem creates and allocates org.kde.StatusNotifierItem. -func NewStatusNotifierItem(object dbus.BusObject) *StatusNotifierItem { - return &StatusNotifierItem{object} -} - -// StatusNotifierItem implements org.kde.StatusNotifierItem D-Bus interface. -type StatusNotifierItem struct { - object dbus.BusObject -} - -// ContextMenu calls org.kde.StatusNotifierItem.ContextMenu method. -func (o *StatusNotifierItem) ContextMenu(ctx context.Context, x int32, y int32) (err error) { - err = o.object.CallWithContext(ctx, InterfaceStatusNotifierItem+".ContextMenu", 0, x, y).Store() - return -} - -// Activate calls org.kde.StatusNotifierItem.Activate method. -func (o *StatusNotifierItem) Activate(ctx context.Context, x int32, y int32) (err error) { - err = o.object.CallWithContext(ctx, InterfaceStatusNotifierItem+".Activate", 0, x, y).Store() - return -} - -// SecondaryActivate calls org.kde.StatusNotifierItem.SecondaryActivate method. -func (o *StatusNotifierItem) SecondaryActivate(ctx context.Context, x int32, y int32) (err error) { - err = o.object.CallWithContext(ctx, InterfaceStatusNotifierItem+".SecondaryActivate", 0, x, y).Store() - return -} - -// Scroll calls org.kde.StatusNotifierItem.Scroll method. -func (o *StatusNotifierItem) Scroll(ctx context.Context, delta int32, orientation string) (err error) { - err = o.object.CallWithContext(ctx, InterfaceStatusNotifierItem+".Scroll", 0, delta, orientation).Store() - return -} - -// GetCategory gets org.kde.StatusNotifierItem.Category property. -func (o *StatusNotifierItem) GetCategory(ctx context.Context) (category string, err error) { - err = o.object.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, InterfaceStatusNotifierItem, "Category").Store(&category) - return -} - -// GetId gets org.kde.StatusNotifierItem.Id property. -func (o *StatusNotifierItem) GetId(ctx context.Context) (id string, err error) { - err = o.object.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, InterfaceStatusNotifierItem, "Id").Store(&id) - return -} - -// GetTitle gets org.kde.StatusNotifierItem.Title property. -func (o *StatusNotifierItem) GetTitle(ctx context.Context) (title string, err error) { - err = o.object.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, InterfaceStatusNotifierItem, "Title").Store(&title) - return -} - -// GetStatus gets org.kde.StatusNotifierItem.Status property. -func (o *StatusNotifierItem) GetStatus(ctx context.Context) (status string, err error) { - err = o.object.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, InterfaceStatusNotifierItem, "Status").Store(&status) - return -} - -// GetWindowId gets org.kde.StatusNotifierItem.WindowId property. -func (o *StatusNotifierItem) GetWindowId(ctx context.Context) (windowId int32, err error) { - err = o.object.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, InterfaceStatusNotifierItem, "WindowId").Store(&windowId) - return -} - -// GetIconThemePath gets org.kde.StatusNotifierItem.IconThemePath property. -func (o *StatusNotifierItem) GetIconThemePath(ctx context.Context) (iconThemePath string, err error) { - err = o.object.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, InterfaceStatusNotifierItem, "IconThemePath").Store(&iconThemePath) - return -} - -// GetMenu gets org.kde.StatusNotifierItem.Menu property. -func (o *StatusNotifierItem) GetMenu(ctx context.Context) (menu dbus.ObjectPath, err error) { - err = o.object.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, InterfaceStatusNotifierItem, "Menu").Store(&menu) - return -} - -// GetItemIsMenu gets org.kde.StatusNotifierItem.ItemIsMenu property. -func (o *StatusNotifierItem) GetItemIsMenu(ctx context.Context) (itemIsMenu bool, err error) { - err = o.object.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, InterfaceStatusNotifierItem, "ItemIsMenu").Store(&itemIsMenu) - return -} - -// GetIconName gets org.kde.StatusNotifierItem.IconName property. -func (o *StatusNotifierItem) GetIconName(ctx context.Context) (iconName string, err error) { - err = o.object.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, InterfaceStatusNotifierItem, "IconName").Store(&iconName) - return -} - -// GetIconPixmap gets org.kde.StatusNotifierItem.IconPixmap property. -// -// Annotations: -// @org.qtproject.QtDBus.QtTypeName = KDbusImageVector -func (o *StatusNotifierItem) GetIconPixmap(ctx context.Context) (iconPixmap []struct { - V0 int32 - V1 int32 - V2 []byte -}, err error) { - err = o.object.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, InterfaceStatusNotifierItem, "IconPixmap").Store(&iconPixmap) - return -} - -// GetOverlayIconName gets org.kde.StatusNotifierItem.OverlayIconName property. -func (o *StatusNotifierItem) GetOverlayIconName(ctx context.Context) (overlayIconName string, err error) { - err = o.object.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, InterfaceStatusNotifierItem, "OverlayIconName").Store(&overlayIconName) - return -} - -// GetOverlayIconPixmap gets org.kde.StatusNotifierItem.OverlayIconPixmap property. -// -// Annotations: -// @org.qtproject.QtDBus.QtTypeName = KDbusImageVector -func (o *StatusNotifierItem) GetOverlayIconPixmap(ctx context.Context) (overlayIconPixmap []struct { - V0 int32 - V1 int32 - V2 []byte -}, err error) { - err = o.object.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, InterfaceStatusNotifierItem, "OverlayIconPixmap").Store(&overlayIconPixmap) - return -} - -// GetAttentionIconName gets org.kde.StatusNotifierItem.AttentionIconName property. -func (o *StatusNotifierItem) GetAttentionIconName(ctx context.Context) (attentionIconName string, err error) { - err = o.object.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, InterfaceStatusNotifierItem, "AttentionIconName").Store(&attentionIconName) - return -} - -// GetAttentionIconPixmap gets org.kde.StatusNotifierItem.AttentionIconPixmap property. -// -// Annotations: -// @org.qtproject.QtDBus.QtTypeName = KDbusImageVector -func (o *StatusNotifierItem) GetAttentionIconPixmap(ctx context.Context) (attentionIconPixmap []struct { - V0 int32 - V1 int32 - V2 []byte -}, err error) { - err = o.object.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, InterfaceStatusNotifierItem, "AttentionIconPixmap").Store(&attentionIconPixmap) - return -} - -// GetAttentionMovieName gets org.kde.StatusNotifierItem.AttentionMovieName property. -func (o *StatusNotifierItem) GetAttentionMovieName(ctx context.Context) (attentionMovieName string, err error) { - err = o.object.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, InterfaceStatusNotifierItem, "AttentionMovieName").Store(&attentionMovieName) - return -} - -// GetToolTip gets org.kde.StatusNotifierItem.ToolTip property. -// -// Annotations: -// @org.qtproject.QtDBus.QtTypeName = KDbusToolTipStruct -func (o *StatusNotifierItem) GetToolTip(ctx context.Context) (toolTip struct { - V0 string - V1 []struct { - V0 int32 - V1 int32 - V2 []byte - } - V2 string - V3 string -}, err error) { - err = o.object.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, InterfaceStatusNotifierItem, "ToolTip").Store(&toolTip) - return -} - -// StatusNotifierItem_NewTitleSignal represents org.kde.StatusNotifierItem.NewTitle signal. -type StatusNotifierItem_NewTitleSignal struct { - sender string - Path dbus.ObjectPath - Body *StatusNotifierItem_NewTitleSignalBody -} - -// Name returns the signal's name. -func (s *StatusNotifierItem_NewTitleSignal) Name() string { - return "NewTitle" -} - -// Interface returns the signal's interface. -func (s *StatusNotifierItem_NewTitleSignal) Interface() string { - return InterfaceStatusNotifierItem -} - -// Sender returns the signal's sender unique name. -func (s *StatusNotifierItem_NewTitleSignal) Sender() string { - return s.sender -} - -func (s *StatusNotifierItem_NewTitleSignal) path() dbus.ObjectPath { - return s.Path -} - -func (s *StatusNotifierItem_NewTitleSignal) values() []interface{} { - return []interface{}{} -} - -// StatusNotifierItem_NewTitleSignalBody is body container. -type StatusNotifierItem_NewTitleSignalBody struct { -} - -// StatusNotifierItem_NewIconSignal represents org.kde.StatusNotifierItem.NewIcon signal. -type StatusNotifierItem_NewIconSignal struct { - sender string - Path dbus.ObjectPath - Body *StatusNotifierItem_NewIconSignalBody -} - -// Name returns the signal's name. -func (s *StatusNotifierItem_NewIconSignal) Name() string { - return "NewIcon" -} - -// Interface returns the signal's interface. -func (s *StatusNotifierItem_NewIconSignal) Interface() string { - return InterfaceStatusNotifierItem -} - -// Sender returns the signal's sender unique name. -func (s *StatusNotifierItem_NewIconSignal) Sender() string { - return s.sender -} - -func (s *StatusNotifierItem_NewIconSignal) path() dbus.ObjectPath { - return s.Path -} - -func (s *StatusNotifierItem_NewIconSignal) values() []interface{} { - return []interface{}{} -} - -// StatusNotifierItem_NewIconSignalBody is body container. -type StatusNotifierItem_NewIconSignalBody struct { -} - -// StatusNotifierItem_NewAttentionIconSignal represents org.kde.StatusNotifierItem.NewAttentionIcon signal. -type StatusNotifierItem_NewAttentionIconSignal struct { - sender string - Path dbus.ObjectPath - Body *StatusNotifierItem_NewAttentionIconSignalBody -} - -// Name returns the signal's name. -func (s *StatusNotifierItem_NewAttentionIconSignal) Name() string { - return "NewAttentionIcon" -} - -// Interface returns the signal's interface. -func (s *StatusNotifierItem_NewAttentionIconSignal) Interface() string { - return InterfaceStatusNotifierItem -} - -// Sender returns the signal's sender unique name. -func (s *StatusNotifierItem_NewAttentionIconSignal) Sender() string { - return s.sender -} - -func (s *StatusNotifierItem_NewAttentionIconSignal) path() dbus.ObjectPath { - return s.Path -} - -func (s *StatusNotifierItem_NewAttentionIconSignal) values() []interface{} { - return []interface{}{} -} - -// StatusNotifierItem_NewAttentionIconSignalBody is body container. -type StatusNotifierItem_NewAttentionIconSignalBody struct { -} - -// StatusNotifierItem_NewOverlayIconSignal represents org.kde.StatusNotifierItem.NewOverlayIcon signal. -type StatusNotifierItem_NewOverlayIconSignal struct { - sender string - Path dbus.ObjectPath - Body *StatusNotifierItem_NewOverlayIconSignalBody -} - -// Name returns the signal's name. -func (s *StatusNotifierItem_NewOverlayIconSignal) Name() string { - return "NewOverlayIcon" -} - -// Interface returns the signal's interface. -func (s *StatusNotifierItem_NewOverlayIconSignal) Interface() string { - return InterfaceStatusNotifierItem -} - -// Sender returns the signal's sender unique name. -func (s *StatusNotifierItem_NewOverlayIconSignal) Sender() string { - return s.sender -} - -func (s *StatusNotifierItem_NewOverlayIconSignal) path() dbus.ObjectPath { - return s.Path -} - -func (s *StatusNotifierItem_NewOverlayIconSignal) values() []interface{} { - return []interface{}{} -} - -// StatusNotifierItem_NewOverlayIconSignalBody is body container. -type StatusNotifierItem_NewOverlayIconSignalBody struct { -} - -// StatusNotifierItem_NewStatusSignal represents org.kde.StatusNotifierItem.NewStatus signal. -type StatusNotifierItem_NewStatusSignal struct { - sender string - Path dbus.ObjectPath - Body *StatusNotifierItem_NewStatusSignalBody -} - -// Name returns the signal's name. -func (s *StatusNotifierItem_NewStatusSignal) Name() string { - return "NewStatus" -} - -// Interface returns the signal's interface. -func (s *StatusNotifierItem_NewStatusSignal) Interface() string { - return InterfaceStatusNotifierItem -} - -// Sender returns the signal's sender unique name. -func (s *StatusNotifierItem_NewStatusSignal) Sender() string { - return s.sender -} - -func (s *StatusNotifierItem_NewStatusSignal) path() dbus.ObjectPath { - return s.Path -} - -func (s *StatusNotifierItem_NewStatusSignal) values() []interface{} { - return []interface{}{s.Body.Status} -} - -// StatusNotifierItem_NewStatusSignalBody is body container. -type StatusNotifierItem_NewStatusSignalBody struct { - Status string -} - -// StatusNotifierItem_NewIconThemePathSignal represents org.kde.StatusNotifierItem.NewIconThemePath signal. -type StatusNotifierItem_NewIconThemePathSignal struct { - sender string - Path dbus.ObjectPath - Body *StatusNotifierItem_NewIconThemePathSignalBody -} - -// Name returns the signal's name. -func (s *StatusNotifierItem_NewIconThemePathSignal) Name() string { - return "NewIconThemePath" -} - -// Interface returns the signal's interface. -func (s *StatusNotifierItem_NewIconThemePathSignal) Interface() string { - return InterfaceStatusNotifierItem -} - -// Sender returns the signal's sender unique name. -func (s *StatusNotifierItem_NewIconThemePathSignal) Sender() string { - return s.sender -} - -func (s *StatusNotifierItem_NewIconThemePathSignal) path() dbus.ObjectPath { - return s.Path -} - -func (s *StatusNotifierItem_NewIconThemePathSignal) values() []interface{} { - return []interface{}{s.Body.IconThemePath} -} - -// StatusNotifierItem_NewIconThemePathSignalBody is body container. -type StatusNotifierItem_NewIconThemePathSignalBody struct { - IconThemePath string -} - -// StatusNotifierItem_NewMenuSignal represents org.kde.StatusNotifierItem.NewMenu signal. -type StatusNotifierItem_NewMenuSignal struct { - sender string - Path dbus.ObjectPath - Body *StatusNotifierItem_NewMenuSignalBody -} - -// Name returns the signal's name. -func (s *StatusNotifierItem_NewMenuSignal) Name() string { - return "NewMenu" -} - -// Interface returns the signal's interface. -func (s *StatusNotifierItem_NewMenuSignal) Interface() string { - return InterfaceStatusNotifierItem -} - -// Sender returns the signal's sender unique name. -func (s *StatusNotifierItem_NewMenuSignal) Sender() string { - return s.sender -} - -func (s *StatusNotifierItem_NewMenuSignal) path() dbus.ObjectPath { - return s.Path -} - -func (s *StatusNotifierItem_NewMenuSignal) values() []interface{} { - return []interface{}{} -} - -// StatusNotifierItem_NewMenuSignalBody is body container. -type StatusNotifierItem_NewMenuSignalBody struct { -} diff --git a/src/native/linux/jna/bridge_linux.go b/src/native/linux/jna/bridge_linux.go deleted file mode 100644 index 1a6bdda1..00000000 --- a/src/native/linux/jna/bridge_linux.go +++ /dev/null @@ -1,297 +0,0 @@ -//go:build linux - -package main - -/* -#include - -typedef void (*void_cb)(); -typedef void (*menu_item_cb)(uint32_t id); - -static void call_void(void_cb f) { if (f) { f(); } } -static void call_menu_item(menu_item_cb f, uint32_t id) { if (f) { f(id); } } -*/ -import "C" -import ( - "log" - "unsafe" - - systraypkg "github.com/energye/systray" -) - -var ( - cbReady C.void_cb - cbExit C.void_cb - cbOnClick C.void_cb - cbOnRClick C.void_cb - cbOnMenuSel C.menu_item_cb - - startFn func() - endFn func() - - running bool -) - -//export Systray_InitCallbacks -func Systray_InitCallbacks(ready C.void_cb, exit C.void_cb, onClick C.void_cb, onRClick C.void_cb, onMenuItem C.menu_item_cb) { - cbReady = ready - cbExit = exit - cbOnClick = onClick - cbOnRClick = onRClick - cbOnMenuSel = onMenuItem - - // Wire Go-level callbacks to call out to the provided C callbacks - systraypkg.SetOnClick(func(menu systraypkg.IMenu) { C.call_void(cbOnClick) }) - systraypkg.SetOnRClick(func(menu systraypkg.IMenu) { C.call_void(cbOnRClick) }) - systraypkg.SetOnDClick(func(menu systraypkg.IMenu) { C.call_void(cbOnClick) }) // reuse onClick for double click - systraypkg.SetOnMenuItemSelected(func(id uint32) { C.call_menu_item(cbOnMenuSel, C.uint(id)) }) -} - -//export Systray_Run -func Systray_Run() { - systraypkg.Run( - func() { running = true; C.call_void(cbReady) }, - func() { running = false; C.call_void(cbExit) }, - ) -} - -//export Systray_PrepareExternalLoop -func Systray_PrepareExternalLoop() { - startFn, endFn = systraypkg.RunWithExternalLoop( - func() { running = true; C.call_void(cbReady) }, - func() { running = false; C.call_void(cbExit) }, - ) -} - -//export Systray_NativeStart -func Systray_NativeStart() { - if startFn != nil { - startFn() - } -} - -//export Systray_NativeEnd -func Systray_NativeEnd() { - if endFn != nil { - endFn() - } - // ensure state cleaned for next run - running = false - startFn = nil - endFn = nil -} - -//export Systray_Quit -func Systray_Quit() { running = false; systraypkg.Quit() } - -//export Systray_SetIcon -func Systray_SetIcon(iconBytes *C.char, length C.int) { - defer func() { if r := recover(); r != nil { log.Printf("[systray] recovered panic in SetIcon: %v", r) } }() - if !running { - return - } - if iconBytes == nil || length <= 0 { - return - } - b := C.GoBytes(unsafe.Pointer(iconBytes), length) - systraypkg.SetIcon(b) -} - -//export Systray_SetTitle -func Systray_SetTitle(title *C.char) { - defer func() { if r := recover(); r != nil { log.Printf("[systray] recovered panic in SetTitle: %v", r) } }() - if !running { - return - } - if title == nil { - return - } - systraypkg.SetTitle(C.GoString(title)) -} - -//export Systray_SetTooltip -func Systray_SetTooltip(tooltip *C.char) { - defer func() { if r := recover(); r != nil { log.Printf("[systray] recovered panic in SetTooltip: %v", r) } }() - if !running { - return - } - if tooltip == nil { - return - } - systraypkg.SetTooltip(C.GoString(tooltip)) -} - -//export Systray_AddMenuItem -func Systray_AddMenuItem(title *C.char, tooltip *C.char) C.uint { - var t, tt string - if title != nil { - t = C.GoString(title) - } - if tooltip != nil { - tt = C.GoString(tooltip) - } - item := systraypkg.AddMenuItem(t, tt) - return C.uint(item.ID()) -} - -//export Systray_AddMenuItemCheckbox -func Systray_AddMenuItemCheckbox(title *C.char, tooltip *C.char, checked C.int) C.uint { - defer func() { if r := recover(); r != nil { log.Printf("[systray] recovered panic in AddMenuItemCheckbox: %v", r) } }() - if !running { - return 0 - } - var t, tt string - if title != nil { - t = C.GoString(title) - } - if tooltip != nil { - tt = C.GoString(tooltip) - } - item := systraypkg.AddMenuItemCheckbox(t, tt, checked != 0) - return C.uint(item.ID()) -} - -//export Systray_AddSeparator -func Systray_AddSeparator() { - defer func() { if r := recover(); r != nil { log.Printf("[systray] recovered panic in AddSeparator: %v", r) } }() - if !running { - return - } - systraypkg.AddSeparator() -} - -//export Systray_ResetMenu -func Systray_ResetMenu() { - defer func() { if r := recover(); r != nil { log.Printf("[systray] recovered panic in ResetMenu: %v", r) } }() - if !running { - return - } - systraypkg.ResetMenu() -} - -// Submenu creation -// -//export Systray_AddSubMenuItem -func Systray_AddSubMenuItem(parentID C.uint, title *C.char, tooltip *C.char) C.uint { - defer func() { if r := recover(); r != nil { log.Printf("[systray] recovered panic in AddSubMenuItem: %v", r) } }() - if !running { - return 0 - } - var t, tt string - if title != nil { - t = C.GoString(title) - } - if tooltip != nil { - tt = C.GoString(tooltip) - } - id := systraypkg.AddSubMenuItemByID(uint32(parentID), t, tt) - return C.uint(id) -} - -//export Systray_AddSubMenuItemCheckbox -func Systray_AddSubMenuItemCheckbox(parentID C.uint, title *C.char, tooltip *C.char, checked C.int) C.uint { - defer func() { if r := recover(); r != nil { log.Printf("[systray] recovered panic in AddSubMenuItemCheckbox: %v", r) } }() - if !running { - return 0 - } - var t, tt string - if title != nil { - t = C.GoString(title) - } - if tooltip != nil { - tt = C.GoString(tooltip) - } - id := systraypkg.AddSubMenuItemCheckboxByID(uint32(parentID), t, tt, checked != 0) - return C.uint(id) -} - -//export Systray_AddSubMenuSeparator -func Systray_AddSubMenuSeparator(parentID C.uint) { - defer func() { if r := recover(); r != nil { log.Printf("[systray] recovered panic in AddSubMenuSeparator: %v", r) } }() - if !running { - return - } - systraypkg.AddSeparatorByID(uint32(parentID)) -} - -// Per-item operations -// -//export Systray_MenuItem_SetTitle -func Systray_MenuItem_SetTitle(id C.uint, title *C.char) C.int { - defer func() { if r := recover(); r != nil { log.Printf("[systray] recovered panic in MenuItem_SetTitle: %v", r) } }() - if !running { - return 0 - } - if title == nil { - return 0 - } - ok := systraypkg.SetMenuItemTitleByID(uint32(id), C.GoString(title)) - if ok { - return 1 - } - return 0 -} - -//export Systray_MenuItem_Enable -func Systray_MenuItem_Enable(id C.uint) { - defer func() { if r := recover(); r != nil { log.Printf("[systray] recovered panic in MenuItem_Enable: %v", r) } }() - if !running { return } - _ = systraypkg.EnableMenuItemByID(uint32(id)) -} - -//export Systray_MenuItem_Disable -func Systray_MenuItem_Disable(id C.uint) { - defer func() { if r := recover(); r != nil { log.Printf("[systray] recovered panic in MenuItem_Disable: %v", r) } }() - if !running { return } - _ = systraypkg.DisableMenuItemByID(uint32(id)) -} - -//export Systray_MenuItem_Show -func Systray_MenuItem_Show(id C.uint) { - defer func() { if r := recover(); r != nil { log.Printf("[systray] recovered panic in MenuItem_Show: %v", r) } }() - if !running { return } - _ = systraypkg.ShowMenuItemByID(uint32(id)) -} - -//export Systray_MenuItem_Hide -func Systray_MenuItem_Hide(id C.uint) { - defer func() { if r := recover(); r != nil { log.Printf("[systray] recovered panic in MenuItem_Hide: %v", r) } }() - if !running { return } - _ = systraypkg.HideMenuItemByID(uint32(id)) -} - -//export Systray_MenuItem_Check -func Systray_MenuItem_Check(id C.uint) { - defer func() { if r := recover(); r != nil { log.Printf("[systray] recovered panic in MenuItem_Check: %v", r) } }() - if !running { return } - _ = systraypkg.CheckMenuItemByID(uint32(id)) -} - -//export Systray_MenuItem_Uncheck -func Systray_MenuItem_Uncheck(id C.uint) { - defer func() { if r := recover(); r != nil { log.Printf("[systray] recovered panic in MenuItem_Uncheck: %v", r) } }() - if !running { return } - _ = systraypkg.UncheckMenuItemByID(uint32(id)) -} - -//export Systray_SetMenuItemIcon -func Systray_SetMenuItemIcon(iconBytes *C.char, length C.int, id C.uint) { - defer func() { if r := recover(); r != nil { log.Printf("[systray] recovered panic in SetMenuItemIcon: %v", r) } }() - if !running { return } - if iconBytes == nil || length <= 0 { - return - } - b := C.GoBytes(unsafe.Pointer(iconBytes), length) - _ = systraypkg.SetMenuItemIconByID(uint32(id), b) -} - -//export Systray_GetLastClickXY -func Systray_GetLastClickXY(outX *C.int, outY *C.int) { - if outX == nil || outY == nil { - return - } - // No running check required; last coordinates may be queried anytime during runtime. - x, y := systraypkg.GetLastClickXY() - *outX = C.int(x) - *outY = C.int(y) -} diff --git a/src/native/linux/jna/main.go b/src/native/linux/jna/main.go deleted file mode 100644 index 34a9cd31..00000000 --- a/src/native/linux/jna/main.go +++ /dev/null @@ -1,5 +0,0 @@ -//go:build linux - -package main - -func main() {} diff --git a/src/native/linux/jni_bridge.c b/src/native/linux/jni_bridge.c new file mode 100644 index 00000000..0d0dd9f9 --- /dev/null +++ b/src/native/linux/jni_bridge.c @@ -0,0 +1,491 @@ +/* + * jni_bridge.c – JNI bridge for ComposeNativeTray Linux native library. + * + * Target Kotlin class: com.kdroid.composetray.lib.linux.LinuxNativeBridge + * JNI prefix: Java_com_kdroid_composetray_lib_linux_LinuxNativeBridge_ + * + * Follows the same patterns as macOS MacTrayBridge.m: + * - JavaVM cache with JNI_OnLoad + * - GlobalRef-based callback storage + * - Runnable trampolines for callbacks + * - Handle-based state (sni_tray* as jlong) + */ + +#include +#include +#include +#include + +#include "sni.h" + +/* ========================================================================== */ +/* JavaVM cache */ +/* ========================================================================== */ + +static JavaVM *g_jvm = NULL; + +JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) { + (void)reserved; + g_jvm = vm; + return JNI_VERSION_1_8; +} + +static JNIEnv *getJNIEnv(void) { + JNIEnv *env = NULL; + if (g_jvm == NULL) return NULL; + jint rc = (*g_jvm)->GetEnv(g_jvm, (void **)&env, JNI_VERSION_1_8); + if (rc == JNI_EDETACHED) { + (*g_jvm)->AttachCurrentThread(g_jvm, (void **)&env, NULL); + } + return env; +} + +/* ========================================================================== */ +/* Callback storage (GlobalRef linked list, same as MacTrayBridge.m) */ +/* ========================================================================== */ + +typedef struct CallbackEntry { + uintptr_t key; + jobject globalRef; + struct CallbackEntry *next; +} CallbackEntry; + +static CallbackEntry *g_clickCallback = NULL; +static CallbackEntry *g_rclickCallback = NULL; +static CallbackEntry *g_menuCallbacks = NULL; + +static void storeCallback(CallbackEntry **list, uintptr_t key, JNIEnv *env, jobject callback) { + /* Remove existing entry for this key */ + CallbackEntry **pp = list; + while (*pp) { + if ((*pp)->key == key) { + CallbackEntry *old = *pp; + *pp = old->next; + (*env)->DeleteGlobalRef(env, old->globalRef); + free(old); + break; + } + pp = &(*pp)->next; + } + if (callback == NULL) return; + CallbackEntry *entry = malloc(sizeof(CallbackEntry)); + entry->key = key; + entry->globalRef = (*env)->NewGlobalRef(env, callback); + entry->next = *list; + *list = entry; +} + +static jobject findCallback(CallbackEntry *list, uintptr_t key) { + for (CallbackEntry *e = list; e; e = e->next) { + if (e->key == key) return e->globalRef; + } + return NULL; +} + +static void clearAllCallbacks(CallbackEntry **list) { + JNIEnv *env = getJNIEnv(); + CallbackEntry *e = *list; + while (e) { + CallbackEntry *next = e->next; + if (env) (*env)->DeleteGlobalRef(env, e->globalRef); + free(e); + e = next; + } + *list = NULL; +} + +/* ========================================================================== */ +/* Runnable invocation helper */ +/* ========================================================================== */ + +/* Cached Runnable class and run() method ID. + * Using the interface class (java.lang.Runnable) instead of GetObjectClass() + * so GraalVM native-image can resolve the method without needing to register + * every lambda class for JNI access. */ +static jclass g_runnableClass = NULL; +static jmethodID g_runMethod = NULL; + +static void ensureRunnableCached(JNIEnv *env) { + if (g_runnableClass) return; + jclass cls = (*env)->FindClass(env, "java/lang/Runnable"); + if (!cls) return; + g_runnableClass = (*env)->NewGlobalRef(env, cls); + g_runMethod = (*env)->GetMethodID(env, g_runnableClass, "run", "()V"); +} + +static void invokeRunnable(jobject runnable) { + JNIEnv *env = getJNIEnv(); + if (!env || !runnable) return; + ensureRunnableCached(env); + if (!g_runMethod) return; + (*env)->CallVoidMethod(env, runnable, g_runMethod); + if ((*env)->ExceptionCheck(env)) (*env)->ExceptionClear(env); +} + +/* ========================================================================== */ +/* C callback trampolines */ +/* ========================================================================== */ + +static void click_trampoline(int32_t x, int32_t y, void *userdata) { + (void)x; (void)y; + uintptr_t key = (uintptr_t)userdata; + jobject runnable = findCallback(g_clickCallback, key); + if (runnable) invokeRunnable(runnable); +} + +static void rclick_trampoline(int32_t x, int32_t y, void *userdata) { + (void)x; (void)y; + uintptr_t key = (uintptr_t)userdata; + jobject runnable = findCallback(g_rclickCallback, key); + if (runnable) invokeRunnable(runnable); +} + +static void menu_item_trampoline(uint32_t id, void *userdata) { + (void)userdata; + jobject runnable = findCallback(g_menuCallbacks, (uintptr_t)id); + if (runnable) invokeRunnable(runnable); +} + +/* ========================================================================== */ +/* JNI exports */ +/* ========================================================================== */ + +/* ── Lifecycle ──────────────────────────────────────────────────────── */ + +JNIEXPORT jlong JNICALL +Java_com_kdroid_composetray_lib_linux_LinuxNativeBridge_nativeCreate( + JNIEnv *env, jclass clazz, jbyteArray iconBytes, jstring tooltip) +{ + (void)clazz; + + const char *tip = NULL; + if (tooltip) { + tip = (*env)->GetStringUTFChars(env, tooltip, NULL); + } + + const uint8_t *icon_data = NULL; + jsize icon_len = 0; + jbyte *icon_buf = NULL; + if (iconBytes) { + icon_len = (*env)->GetArrayLength(env, iconBytes); + icon_buf = (*env)->GetByteArrayElements(env, iconBytes, NULL); + icon_data = (const uint8_t *)icon_buf; + } + + sni_tray *tray = sni_tray_create(icon_data, (size_t)icon_len, tip); + + if (icon_buf) (*env)->ReleaseByteArrayElements(env, iconBytes, icon_buf, JNI_ABORT); + if (tip) (*env)->ReleaseStringUTFChars(env, tooltip, tip); + + return (jlong)(uintptr_t)tray; +} + +JNIEXPORT jint JNICALL +Java_com_kdroid_composetray_lib_linux_LinuxNativeBridge_nativeRun( + JNIEnv *env, jclass clazz, jlong handle) +{ + (void)env; (void)clazz; + sni_tray *tray = (sni_tray *)(uintptr_t)handle; + if (!tray) return -1; + return (jint)sni_tray_run(tray); +} + +JNIEXPORT void JNICALL +Java_com_kdroid_composetray_lib_linux_LinuxNativeBridge_nativeQuit( + JNIEnv *env, jclass clazz, jlong handle) +{ + (void)env; (void)clazz; + sni_tray *tray = (sni_tray *)(uintptr_t)handle; + sni_tray_quit(tray); +} + +JNIEXPORT void JNICALL +Java_com_kdroid_composetray_lib_linux_LinuxNativeBridge_nativeDestroy( + JNIEnv *env, jclass clazz, jlong handle) +{ + (void)clazz; + sni_tray *tray = (sni_tray *)(uintptr_t)handle; + if (!tray) return; + + /* Clean up all callbacks for this tray */ + uintptr_t key = (uintptr_t)tray; + storeCallback(&g_clickCallback, key, env, NULL); + storeCallback(&g_rclickCallback, key, env, NULL); + /* Menu callbacks are keyed by item id, clear all */ + clearAllCallbacks(&g_menuCallbacks); + + sni_tray_destroy(tray); +} + +/* ── Tray properties ────────────────────────────────────────────────── */ + +JNIEXPORT void JNICALL +Java_com_kdroid_composetray_lib_linux_LinuxNativeBridge_nativeSetIcon( + JNIEnv *env, jclass clazz, jlong handle, jbyteArray iconBytes) +{ + (void)clazz; + sni_tray *tray = (sni_tray *)(uintptr_t)handle; + if (!tray || !iconBytes) return; + + jsize len = (*env)->GetArrayLength(env, iconBytes); + jbyte *buf = (*env)->GetByteArrayElements(env, iconBytes, NULL); + sni_tray_set_icon(tray, (const uint8_t *)buf, (size_t)len); + (*env)->ReleaseByteArrayElements(env, iconBytes, buf, JNI_ABORT); +} + +JNIEXPORT void JNICALL +Java_com_kdroid_composetray_lib_linux_LinuxNativeBridge_nativeSetTitle( + JNIEnv *env, jclass clazz, jlong handle, jstring title) +{ + (void)clazz; + sni_tray *tray = (sni_tray *)(uintptr_t)handle; + if (!tray) return; + const char *utf = title ? (*env)->GetStringUTFChars(env, title, NULL) : NULL; + sni_tray_set_title(tray, utf); + if (utf) (*env)->ReleaseStringUTFChars(env, title, utf); +} + +JNIEXPORT void JNICALL +Java_com_kdroid_composetray_lib_linux_LinuxNativeBridge_nativeSetTooltip( + JNIEnv *env, jclass clazz, jlong handle, jstring tooltip) +{ + (void)clazz; + sni_tray *tray = (sni_tray *)(uintptr_t)handle; + if (!tray) return; + const char *utf = tooltip ? (*env)->GetStringUTFChars(env, tooltip, NULL) : NULL; + sni_tray_set_tooltip(tray, utf); + if (utf) (*env)->ReleaseStringUTFChars(env, tooltip, utf); +} + +/* ── Callbacks ──────────────────────────────────────────────────────── */ + +JNIEXPORT void JNICALL +Java_com_kdroid_composetray_lib_linux_LinuxNativeBridge_nativeSetClickCallback( + JNIEnv *env, jclass clazz, jlong handle, jobject callback) +{ + (void)clazz; + sni_tray *tray = (sni_tray *)(uintptr_t)handle; + if (!tray) return; + uintptr_t key = (uintptr_t)tray; + storeCallback(&g_clickCallback, key, env, callback); + sni_tray_set_click_callback(tray, + callback ? click_trampoline : NULL, + (void *)key); +} + +JNIEXPORT void JNICALL +Java_com_kdroid_composetray_lib_linux_LinuxNativeBridge_nativeSetRClickCallback( + JNIEnv *env, jclass clazz, jlong handle, jobject callback) +{ + (void)clazz; + sni_tray *tray = (sni_tray *)(uintptr_t)handle; + if (!tray) return; + uintptr_t key = (uintptr_t)tray; + storeCallback(&g_rclickCallback, key, env, callback); + sni_tray_set_rclick_callback(tray, + callback ? rclick_trampoline : NULL, + (void *)key); +} + +JNIEXPORT void JNICALL +Java_com_kdroid_composetray_lib_linux_LinuxNativeBridge_nativeSetMenuItemCallback( + JNIEnv *env, jclass clazz, jlong handle, jint menuId, jobject callback) +{ + (void)clazz; + sni_tray *tray = (sni_tray *)(uintptr_t)handle; + if (!tray) return; + storeCallback(&g_menuCallbacks, (uintptr_t)menuId, env, callback); + /* Ensure the global menu callback trampoline is installed */ + sni_tray_set_menu_callback(tray, menu_item_trampoline, (void *)(uintptr_t)tray); +} + +/* ── Click position ─────────────────────────────────────────────────── */ + +JNIEXPORT void JNICALL +Java_com_kdroid_composetray_lib_linux_LinuxNativeBridge_nativeGetLastClickXY( + JNIEnv *env, jclass clazz, jlong handle, jintArray outXY) +{ + (void)clazz; + sni_tray *tray = (sni_tray *)(uintptr_t)handle; + if (!tray || !outXY) return; + int32_t x = 0, y = 0; + sni_tray_get_last_click_xy(tray, &x, &y); + jint buf[2] = {(jint)x, (jint)y}; + (*env)->SetIntArrayRegion(env, outXY, 0, 2, buf); +} + +/* ── Menu management ────────────────────────────────────────────────── */ + +JNIEXPORT void JNICALL +Java_com_kdroid_composetray_lib_linux_LinuxNativeBridge_nativeResetMenu( + JNIEnv *env, jclass clazz, jlong handle) +{ + (void)env; (void)clazz; + sni_tray *tray = (sni_tray *)(uintptr_t)handle; + if (!tray) return; + /* Clear menu item callbacks */ + clearAllCallbacks(&g_menuCallbacks); + sni_tray_reset_menu(tray); +} + +JNIEXPORT jint JNICALL +Java_com_kdroid_composetray_lib_linux_LinuxNativeBridge_nativeAddMenuItem( + JNIEnv *env, jclass clazz, jlong handle, jstring title, jstring tooltip) +{ + (void)clazz; + sni_tray *tray = (sni_tray *)(uintptr_t)handle; + if (!tray) return 0; + const char *t = title ? (*env)->GetStringUTFChars(env, title, NULL) : NULL; + const char *tt = tooltip ? (*env)->GetStringUTFChars(env, tooltip, NULL) : NULL; + uint32_t id = sni_tray_add_menu_item(tray, t, tt); + if (t) (*env)->ReleaseStringUTFChars(env, title, t); + if (tt) (*env)->ReleaseStringUTFChars(env, tooltip, tt); + return (jint)id; +} + +JNIEXPORT jint JNICALL +Java_com_kdroid_composetray_lib_linux_LinuxNativeBridge_nativeAddMenuItemCheckbox( + JNIEnv *env, jclass clazz, jlong handle, jstring title, jstring tooltip, jboolean checked) +{ + (void)clazz; + sni_tray *tray = (sni_tray *)(uintptr_t)handle; + if (!tray) return 0; + const char *t = title ? (*env)->GetStringUTFChars(env, title, NULL) : NULL; + const char *tt = tooltip ? (*env)->GetStringUTFChars(env, tooltip, NULL) : NULL; + uint32_t id = sni_tray_add_menu_item_checkbox(tray, t, tt, checked ? 1 : 0); + if (t) (*env)->ReleaseStringUTFChars(env, title, t); + if (tt) (*env)->ReleaseStringUTFChars(env, tooltip, tt); + return (jint)id; +} + +JNIEXPORT void JNICALL +Java_com_kdroid_composetray_lib_linux_LinuxNativeBridge_nativeAddSeparator( + JNIEnv *env, jclass clazz, jlong handle) +{ + (void)env; (void)clazz; + sni_tray *tray = (sni_tray *)(uintptr_t)handle; + if (tray) sni_tray_add_separator(tray); +} + +JNIEXPORT jint JNICALL +Java_com_kdroid_composetray_lib_linux_LinuxNativeBridge_nativeAddSubMenuItem( + JNIEnv *env, jclass clazz, jlong handle, jint parentId, + jstring title, jstring tooltip) +{ + (void)clazz; + sni_tray *tray = (sni_tray *)(uintptr_t)handle; + if (!tray) return 0; + const char *t = title ? (*env)->GetStringUTFChars(env, title, NULL) : NULL; + const char *tt = tooltip ? (*env)->GetStringUTFChars(env, tooltip, NULL) : NULL; + uint32_t id = sni_tray_add_sub_menu_item(tray, (uint32_t)parentId, t, tt); + if (t) (*env)->ReleaseStringUTFChars(env, title, t); + if (tt) (*env)->ReleaseStringUTFChars(env, tooltip, tt); + return (jint)id; +} + +JNIEXPORT jint JNICALL +Java_com_kdroid_composetray_lib_linux_LinuxNativeBridge_nativeAddSubMenuItemCheckbox( + JNIEnv *env, jclass clazz, jlong handle, jint parentId, + jstring title, jstring tooltip, jboolean checked) +{ + (void)clazz; + sni_tray *tray = (sni_tray *)(uintptr_t)handle; + if (!tray) return 0; + const char *t = title ? (*env)->GetStringUTFChars(env, title, NULL) : NULL; + const char *tt = tooltip ? (*env)->GetStringUTFChars(env, tooltip, NULL) : NULL; + uint32_t id = sni_tray_add_sub_menu_item_checkbox(tray, (uint32_t)parentId, t, tt, checked ? 1 : 0); + if (t) (*env)->ReleaseStringUTFChars(env, title, t); + if (tt) (*env)->ReleaseStringUTFChars(env, tooltip, tt); + return (jint)id; +} + +JNIEXPORT void JNICALL +Java_com_kdroid_composetray_lib_linux_LinuxNativeBridge_nativeAddSubSeparator( + JNIEnv *env, jclass clazz, jlong handle, jint parentId) +{ + (void)env; (void)clazz; + sni_tray *tray = (sni_tray *)(uintptr_t)handle; + if (tray) sni_tray_add_sub_separator(tray, (uint32_t)parentId); +} + +/* ── Per-item operations ────────────────────────────────────────────── */ + +JNIEXPORT jint JNICALL +Java_com_kdroid_composetray_lib_linux_LinuxNativeBridge_nativeItemSetTitle( + JNIEnv *env, jclass clazz, jlong handle, jint id, jstring title) +{ + (void)clazz; + sni_tray *tray = (sni_tray *)(uintptr_t)handle; + if (!tray) return 0; + const char *t = title ? (*env)->GetStringUTFChars(env, title, NULL) : NULL; + int ok = sni_tray_item_set_title(tray, (uint32_t)id, t); + if (t) (*env)->ReleaseStringUTFChars(env, title, t); + return (jint)ok; +} + +JNIEXPORT void JNICALL +Java_com_kdroid_composetray_lib_linux_LinuxNativeBridge_nativeItemEnable( + JNIEnv *env, jclass clazz, jlong handle, jint id) +{ + (void)env; (void)clazz; + sni_tray *tray = (sni_tray *)(uintptr_t)handle; + if (tray) sni_tray_item_enable(tray, (uint32_t)id); +} + +JNIEXPORT void JNICALL +Java_com_kdroid_composetray_lib_linux_LinuxNativeBridge_nativeItemDisable( + JNIEnv *env, jclass clazz, jlong handle, jint id) +{ + (void)env; (void)clazz; + sni_tray *tray = (sni_tray *)(uintptr_t)handle; + if (tray) sni_tray_item_disable(tray, (uint32_t)id); +} + +JNIEXPORT void JNICALL +Java_com_kdroid_composetray_lib_linux_LinuxNativeBridge_nativeItemShow( + JNIEnv *env, jclass clazz, jlong handle, jint id) +{ + (void)env; (void)clazz; + sni_tray *tray = (sni_tray *)(uintptr_t)handle; + if (tray) sni_tray_item_show(tray, (uint32_t)id); +} + +JNIEXPORT void JNICALL +Java_com_kdroid_composetray_lib_linux_LinuxNativeBridge_nativeItemHide( + JNIEnv *env, jclass clazz, jlong handle, jint id) +{ + (void)env; (void)clazz; + sni_tray *tray = (sni_tray *)(uintptr_t)handle; + if (tray) sni_tray_item_hide(tray, (uint32_t)id); +} + +JNIEXPORT void JNICALL +Java_com_kdroid_composetray_lib_linux_LinuxNativeBridge_nativeItemCheck( + JNIEnv *env, jclass clazz, jlong handle, jint id) +{ + (void)env; (void)clazz; + sni_tray *tray = (sni_tray *)(uintptr_t)handle; + if (tray) sni_tray_item_check(tray, (uint32_t)id); +} + +JNIEXPORT void JNICALL +Java_com_kdroid_composetray_lib_linux_LinuxNativeBridge_nativeItemUncheck( + JNIEnv *env, jclass clazz, jlong handle, jint id) +{ + (void)env; (void)clazz; + sni_tray *tray = (sni_tray *)(uintptr_t)handle; + if (tray) sni_tray_item_uncheck(tray, (uint32_t)id); +} + +JNIEXPORT void JNICALL +Java_com_kdroid_composetray_lib_linux_LinuxNativeBridge_nativeItemSetIcon( + JNIEnv *env, jclass clazz, jlong handle, jint id, jbyteArray iconBytes) +{ + (void)clazz; + sni_tray *tray = (sni_tray *)(uintptr_t)handle; + if (!tray || !iconBytes) return; + jsize len = (*env)->GetArrayLength(env, iconBytes); + jbyte *buf = (*env)->GetByteArrayElements(env, iconBytes, NULL); + sni_tray_item_set_icon(tray, (uint32_t)id, (const uint8_t *)buf, (size_t)len); + (*env)->ReleaseByteArrayElements(env, iconBytes, buf, JNI_ABORT); +} diff --git a/src/native/linux/sni.c b/src/native/linux/sni.c new file mode 100644 index 00000000..d255f62b --- /dev/null +++ b/src/native/linux/sni.c @@ -0,0 +1,1281 @@ +/* + * sni.c – StatusNotifierItem + DBusMenu implementation via sd-bus. + * + * Implements both org.kde.StatusNotifierItem and com.canonical.dbusmenu + * interfaces required for Linux system tray support. + * + * Desktop-environment quirks (GNOME/KDE) are handled inline with comments. + */ + +#include "sni.h" + +#include +#include +#include +#include +#include +#include + +#include + +/* stb_image for PNG/JPG decoding */ +#define STB_IMAGE_IMPLEMENTATION +#define STBI_ONLY_PNG +#define STBI_ONLY_JPEG +#define STBI_NO_STDIO +#include "stb_image.h" + +/* stb_image_resize2 for multi-resolution scaling */ +#define STB_IMAGE_RESIZE_IMPLEMENTATION +#include "stb_image_resize2.h" + +/* ========================================================================== */ +/* Constants */ +/* ========================================================================== */ + +#define SNI_PATH "/StatusNotifierItem" +#define MENU_PATH "/StatusNotifierMenu" +#define SNI_IFACE "org.kde.StatusNotifierItem" +#define MENU_IFACE "com.canonical.dbusmenu" +#define WATCHER_BUS "org.kde.StatusNotifierWatcher" +#define WATCHER_PATH "/StatusNotifierWatcher" +#define WATCHER_IFACE "org.kde.StatusNotifierWatcher" + +#define MAX_MENU_ITEMS 512 +#define DCLICK_INTERVAL 500 /* ms */ + +/* Icon target sizes for multi-resolution pixmap (matches Go implementation) */ +static const int ICON_SIZES[] = {16, 22, 24, 32, 48, 64, 128}; +#define NUM_ICON_SIZES (sizeof(ICON_SIZES) / sizeof(ICON_SIZES[0])) + +/* ========================================================================== */ +/* Menu item */ +/* ========================================================================== */ + +typedef struct menu_item { + int32_t id; + char *label; + char *tooltip; + int disabled; + int checked; + int checkable; + int visible; + int is_separator; + + /* Per-item icon raw PNG/JPG data */ + uint8_t *icon_data; + size_t icon_len; + + /* Tree structure */ + int32_t parent_id; /* 0 = root */ + struct menu_item *children; + int child_count; + int child_capacity; +} menu_item; + +/* ========================================================================== */ +/* Pixmap (ARGB32 big-endian for SNI IconPixmap) */ +/* ========================================================================== */ + +typedef struct { + int width; + int height; + uint8_t *data; /* ARGB32 big-endian: [A][R][G][B] per pixel */ + size_t data_len; +} pixmap; + +typedef struct { + pixmap *entries; + int count; +} pixmap_list; + +/* ========================================================================== */ +/* Desktop environment detection */ +/* ========================================================================== */ + +typedef enum { + DE_UNKNOWN = 0, + DE_GNOME, + DE_KDE, +} desktop_env; + +static desktop_env detect_desktop(void) { + const char *xdg = getenv("XDG_CURRENT_DESKTOP"); + const char *sess = getenv("DESKTOP_SESSION"); + + if (xdg) { + if (strcasestr(xdg, "gnome")) return DE_GNOME; + if (strcasestr(xdg, "kde") || strcasestr(xdg, "plasma")) return DE_KDE; + } + if (sess) { + if (strcasestr(sess, "gnome")) return DE_GNOME; + if (strcasestr(sess, "kde") || strcasestr(sess, "plasma")) return DE_KDE; + } + return DE_UNKNOWN; +} + +/* ========================================================================== */ +/* Tray state */ +/* ========================================================================== */ + +struct sni_tray { + sd_bus *bus; + sd_bus_slot *sni_slot; + sd_bus_slot *menu_slot; + sd_bus_slot *sni_prop_slot; + sd_bus_slot *menu_prop_slot; + char *bus_name; /* org.kde.StatusNotifierItem-{PID}-1 */ + int running; + int quit_pipe[2]; /* write to [1] to wake the event loop */ + + /* SNI properties */ + char *title; + char *tooltip_text; + + /* Icon: decoded pixmap list */ + pixmap_list icon_pixmaps; + + /* Menu state */ + menu_item *items; /* flat array of all items */ + int item_count; + int item_capacity; + uint32_t next_id; + uint32_t menu_version; + + /* Click state */ + pthread_mutex_t click_lock; + int32_t last_click_x; + int32_t last_click_y; + int64_t last_activate_ms; + + /* Callbacks */ + sni_click_cb on_click; + void *on_click_data; + sni_click_cb on_rclick; + void *on_rclick_data; + sni_menu_item_cb on_menu_item; + void *on_menu_item_data; + + /* Desktop environment */ + desktop_env de; + + /* Menu path currently advertised in SNI Menu property. + * GNOME quirk: "/" when no menu, "/StatusNotifierMenu" when items exist. */ + const char *current_menu_path; +}; + +/* ========================================================================== */ +/* Time helpers */ +/* ========================================================================== */ + +#include + +static int64_t now_ms(void) { + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return (int64_t)ts.tv_sec * 1000 + ts.tv_nsec / 1000000; +} + +/* ========================================================================== */ +/* Icon / Pixmap helpers */ +/* ========================================================================== */ + +/* Convert RGBA (stb_image output) to ARGB32 big-endian as required by SNI. */ +static uint8_t *rgba_to_argb32_be(const uint8_t *rgba, int w, int h) { + size_t len = (size_t)w * h * 4; + uint8_t *out = malloc(len); + if (!out) return NULL; + for (int i = 0; i < w * h; i++) { + uint8_t r = rgba[i * 4 + 0]; + uint8_t g = rgba[i * 4 + 1]; + uint8_t b = rgba[i * 4 + 2]; + uint8_t a = rgba[i * 4 + 3]; + out[i * 4 + 0] = a; + out[i * 4 + 1] = r; + out[i * 4 + 2] = g; + out[i * 4 + 3] = b; + } + return out; +} + +static void free_pixmap_list(pixmap_list *pl) { + if (!pl->entries) return; + for (int i = 0; i < pl->count; i++) { + free(pl->entries[i].data); + } + free(pl->entries); + pl->entries = NULL; + pl->count = 0; +} + +/* Build multi-resolution pixmaps from raw PNG/JPG data. */ +static pixmap_list build_pixmaps(const uint8_t *data, size_t len) { + pixmap_list pl = {NULL, 0}; + if (!data || len == 0) return pl; + + int src_w, src_h, channels; + uint8_t *src = stbi_load_from_memory(data, (int)len, &src_w, &src_h, &channels, 4); + if (!src) return pl; + + pl.entries = calloc(NUM_ICON_SIZES, sizeof(pixmap)); + if (!pl.entries) { stbi_image_free(src); return pl; } + + for (size_t i = 0; i < NUM_ICON_SIZES; i++) { + int s = ICON_SIZES[i]; + uint8_t *resized = malloc((size_t)s * s * 4); + if (!resized) continue; + + stbir_resize_uint8_linear(src, src_w, src_h, src_w * 4, + resized, s, s, s * 4, STBIR_RGBA); + + uint8_t *argb = rgba_to_argb32_be(resized, s, s); + free(resized); + if (!argb) continue; + + pl.entries[pl.count].width = s; + pl.entries[pl.count].height = s; + pl.entries[pl.count].data = argb; + pl.entries[pl.count].data_len = (size_t)s * s * 4; + pl.count++; + } + + stbi_image_free(src); + return pl; +} + +/* ========================================================================== */ +/* Menu item helpers */ +/* ========================================================================== */ + +static menu_item *find_item(sni_tray *tray, int32_t id) { + for (int i = 0; i < tray->item_count; i++) { + if (tray->items[i].id == id) return &tray->items[i]; + } + return NULL; +} + +static menu_item *alloc_item(sni_tray *tray) { + if (tray->item_count >= tray->item_capacity) { + int new_cap = tray->item_capacity ? tray->item_capacity * 2 : 32; + menu_item *new_items = realloc(tray->items, (size_t)new_cap * sizeof(menu_item)); + if (!new_items) return NULL; + tray->items = new_items; + tray->item_capacity = new_cap; + } + menu_item *item = &tray->items[tray->item_count++]; + memset(item, 0, sizeof(menu_item)); + item->visible = 1; + return item; +} + +static void free_menu_items(sni_tray *tray) { + for (int i = 0; i < tray->item_count; i++) { + free(tray->items[i].label); + free(tray->items[i].tooltip); + free(tray->items[i].icon_data); + free(tray->items[i].children); + } + free(tray->items); + tray->items = NULL; + tray->item_count = 0; + tray->item_capacity = 0; +} + +/* ========================================================================== */ +/* Menu path quirks */ +/* ========================================================================== */ + +/* GNOME: advertise "/" when no menu items exist, "/StatusNotifierMenu" otherwise. + * KDE/others: always advertise "/StatusNotifierMenu" (or "/NO_DBUSMENU" when empty, + * but KDE needs at least a dummy separator — handled by Kotlin side). */ +static const char *no_menu_path(desktop_env de) { + return (de == DE_GNOME) ? "/" : MENU_PATH; +} + +/* ========================================================================== */ +/* D-Bus: emit signals */ +/* ========================================================================== */ + +static void emit_new_icon(sni_tray *tray) { + if (!tray->bus) return; + sd_bus_emit_signal(tray->bus, SNI_PATH, SNI_IFACE, "NewIcon", ""); +} + +static void emit_new_title(sni_tray *tray) { + if (!tray->bus) return; + sd_bus_emit_signal(tray->bus, SNI_PATH, SNI_IFACE, "NewTitle", ""); +} + +static void emit_layout_updated(sni_tray *tray) { + if (!tray->bus) return; + tray->menu_version++; + sd_bus_emit_signal(tray->bus, MENU_PATH, MENU_IFACE, "LayoutUpdated", + "ui", tray->menu_version, (int32_t)0); + /* Also emit properties changed for Version */ + sd_bus_emit_properties_changed(tray->bus, MENU_PATH, MENU_IFACE, + "Version", NULL); +} + +static void emit_sni_properties_changed(sni_tray *tray, const char *prop) { + if (!tray->bus) return; + sd_bus_emit_properties_changed(tray->bus, SNI_PATH, SNI_IFACE, prop, NULL); +} + +/* ========================================================================== */ +/* D-Bus: write IconPixmap (a(iiay)) into message */ +/* ========================================================================== */ + +static int append_pixmap_list(sd_bus_message *reply, const pixmap_list *pl) { + int r; + r = sd_bus_message_open_container(reply, 'a', "(iiay)"); + if (r < 0) return r; + + for (int i = 0; i < pl->count; i++) { + r = sd_bus_message_open_container(reply, 'r', "iiay"); + if (r < 0) return r; + r = sd_bus_message_append(reply, "ii", pl->entries[i].width, pl->entries[i].height); + if (r < 0) return r; + r = sd_bus_message_append_array(reply, 'y', + pl->entries[i].data, pl->entries[i].data_len); + if (r < 0) return r; + r = sd_bus_message_close_container(reply); + if (r < 0) return r; + } + + return sd_bus_message_close_container(reply); +} + +/* Write empty pixmap array a(iiay) */ +static int append_empty_pixmap_list(sd_bus_message *reply) { + int r; + r = sd_bus_message_open_container(reply, 'a', "(iiay)"); + if (r < 0) return r; + return sd_bus_message_close_container(reply); +} + +/* ========================================================================== */ +/* D-Bus: write ToolTip (sa(iiay)ss) into message */ +/* ========================================================================== */ + +static int append_tooltip(sd_bus_message *reply, sni_tray *tray) { + int r; + /* ToolTip is a struct (sa(iiay)ss) = (name, icon_pixmaps, title, description) */ + r = sd_bus_message_open_container(reply, 'r', "sa(iiay)ss"); + if (r < 0) return r; + + r = sd_bus_message_append(reply, "s", ""); /* name */ + if (r < 0) return r; + + r = append_pixmap_list(reply, &tray->icon_pixmaps); + if (r < 0) return r; + + r = sd_bus_message_append(reply, "ss", + tray->tooltip_text ? tray->tooltip_text : "", + ""); /* description */ + if (r < 0) return r; + + return sd_bus_message_close_container(reply); +} + +/* ========================================================================== */ +/* D-Bus: SNI property getter */ +/* ========================================================================== */ + +static int sni_get_property(sd_bus *bus, const char *path, const char *interface, + const char *property, sd_bus_message *reply, + void *userdata, sd_bus_error *error) { + (void)bus; (void)path; (void)interface; (void)error; + sni_tray *tray = userdata; + + if (strcmp(property, "Category") == 0) + return sd_bus_message_append(reply, "s", "ApplicationStatus"); + if (strcmp(property, "Id") == 0) + return sd_bus_message_append(reply, "s", "1"); + if (strcmp(property, "Title") == 0) + return sd_bus_message_append(reply, "s", tray->title ? tray->title : ""); + if (strcmp(property, "Status") == 0) + return sd_bus_message_append(reply, "s", "Active"); + if (strcmp(property, "WindowId") == 0) + return sd_bus_message_append(reply, "i", 0); + if (strcmp(property, "IconThemePath") == 0) + return sd_bus_message_append(reply, "s", ""); + if (strcmp(property, "Menu") == 0) + return sd_bus_message_append(reply, "o", tray->current_menu_path); + if (strcmp(property, "ItemIsMenu") == 0) + return sd_bus_message_append(reply, "b", 1); + if (strcmp(property, "IconName") == 0) + return sd_bus_message_append(reply, "s", ""); + if (strcmp(property, "IconPixmap") == 0) + return append_pixmap_list(reply, &tray->icon_pixmaps); + if (strcmp(property, "OverlayIconName") == 0) + return sd_bus_message_append(reply, "s", ""); + if (strcmp(property, "OverlayIconPixmap") == 0) + return append_empty_pixmap_list(reply); + if (strcmp(property, "AttentionIconName") == 0) + return sd_bus_message_append(reply, "s", ""); + if (strcmp(property, "AttentionIconPixmap") == 0) + return append_empty_pixmap_list(reply); + if (strcmp(property, "AttentionMovieName") == 0) + return sd_bus_message_append(reply, "s", ""); + if (strcmp(property, "ToolTip") == 0) + return append_tooltip(reply, tray); + + return -ENOENT; +} + +/* ========================================================================== */ +/* D-Bus: SNI methods */ +/* ========================================================================== */ + +static int sni_activate(sd_bus_message *msg, void *userdata, sd_bus_error *error) { + (void)error; + sni_tray *tray = userdata; + int32_t x, y; + sd_bus_message_read(msg, "ii", &x, &y); + + pthread_mutex_lock(&tray->click_lock); + tray->last_click_x = x; + tray->last_click_y = y; + pthread_mutex_unlock(&tray->click_lock); + + /* Double-click detection */ + int64_t now = now_ms(); + if (tray->last_activate_ms > 0 && (now - tray->last_activate_ms) < DCLICK_INTERVAL) { + tray->last_activate_ms = 0; + /* Double click — still invoke on_click */ + } else { + tray->last_activate_ms = now; + } + + if (tray->on_click) + tray->on_click(x, y, tray->on_click_data); + + return sd_bus_reply_method_return(msg, ""); +} + +static int sni_context_menu(sd_bus_message *msg, void *userdata, sd_bus_error *error) { + (void)error; + sni_tray *tray = userdata; + int32_t x, y; + sd_bus_message_read(msg, "ii", &x, &y); + + pthread_mutex_lock(&tray->click_lock); + tray->last_click_x = x; + tray->last_click_y = y; + pthread_mutex_unlock(&tray->click_lock); + + if (tray->on_rclick) + tray->on_rclick(x, y, tray->on_rclick_data); + + return sd_bus_reply_method_return(msg, ""); +} + +static int sni_secondary_activate(sd_bus_message *msg, void *userdata, sd_bus_error *error) { + (void)error; + sni_tray *tray = userdata; + int32_t x, y; + sd_bus_message_read(msg, "ii", &x, &y); + + pthread_mutex_lock(&tray->click_lock); + tray->last_click_x = x; + tray->last_click_y = y; + pthread_mutex_unlock(&tray->click_lock); + + return sd_bus_reply_method_return(msg, ""); +} + +static int sni_scroll(sd_bus_message *msg, void *userdata, sd_bus_error *error) { + (void)error; (void)userdata; + return sd_bus_reply_method_return(msg, ""); +} + +/* ========================================================================== */ +/* D-Bus: SNI vtable */ +/* ========================================================================== */ + +static const sd_bus_vtable sni_vtable[] = { + SD_BUS_VTABLE_START(0), + + /* Properties */ + SD_BUS_PROPERTY("Category", "s", sni_get_property, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), + SD_BUS_PROPERTY("Id", "s", sni_get_property, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), + SD_BUS_PROPERTY("Title", "s", sni_get_property, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), + SD_BUS_PROPERTY("Status", "s", sni_get_property, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), + SD_BUS_PROPERTY("WindowId", "i", sni_get_property, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), + SD_BUS_PROPERTY("IconThemePath", "s", sni_get_property, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), + SD_BUS_PROPERTY("Menu", "o", sni_get_property, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), + SD_BUS_PROPERTY("ItemIsMenu", "b", sni_get_property, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), + SD_BUS_PROPERTY("IconName", "s", sni_get_property, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), + SD_BUS_PROPERTY("IconPixmap", "a(iiay)", sni_get_property, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), + SD_BUS_PROPERTY("OverlayIconName", "s", sni_get_property, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), + SD_BUS_PROPERTY("OverlayIconPixmap", "a(iiay)", sni_get_property, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), + SD_BUS_PROPERTY("AttentionIconName", "s", sni_get_property, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), + SD_BUS_PROPERTY("AttentionIconPixmap","a(iiay)", sni_get_property, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), + SD_BUS_PROPERTY("AttentionMovieName", "s", sni_get_property, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), + SD_BUS_PROPERTY("ToolTip", "(sa(iiay)ss)", sni_get_property, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), + + /* Methods */ + SD_BUS_METHOD("Activate", "ii", "", sni_activate, SD_BUS_VTABLE_UNPRIVILEGED), + SD_BUS_METHOD("ContextMenu", "ii", "", sni_context_menu, SD_BUS_VTABLE_UNPRIVILEGED), + SD_BUS_METHOD("SecondaryActivate", "ii", "", sni_secondary_activate, SD_BUS_VTABLE_UNPRIVILEGED), + SD_BUS_METHOD("Scroll", "is", "", sni_scroll, SD_BUS_VTABLE_UNPRIVILEGED), + + /* Signals */ + SD_BUS_SIGNAL("NewTitle", "", 0), + SD_BUS_SIGNAL("NewIcon", "", 0), + SD_BUS_SIGNAL("NewAttentionIcon", "", 0), + SD_BUS_SIGNAL("NewOverlayIcon", "", 0), + SD_BUS_SIGNAL("NewStatus", "s", 0), + SD_BUS_SIGNAL("NewIconThemePath", "s", 0), + SD_BUS_SIGNAL("NewMenu", "", 0), + + SD_BUS_VTABLE_END +}; + +/* ========================================================================== */ +/* D-Bus: DBusMenu – write layout recursively */ +/* ========================================================================== */ + +/* Write a single menu item layout: (ia{sv}av) */ +static int append_menu_layout(sd_bus_message *reply, sni_tray *tray, + int32_t item_id, int32_t depth); + +/* Get children IDs for a given parent */ +static int get_children(sni_tray *tray, int32_t parent_id, + int32_t *out, int max) { + int count = 0; + for (int i = 0; i < tray->item_count && count < max; i++) { + if (tray->items[i].parent_id == parent_id) { + out[count++] = tray->items[i].id; + } + } + return count; +} + +static int append_menu_layout(sd_bus_message *reply, sni_tray *tray, + int32_t item_id, int32_t depth) { + int r; + + /* Open struct (ia{sv}av) */ + r = sd_bus_message_open_container(reply, 'r', "ia{sv}av"); + if (r < 0) return r; + + /* id */ + r = sd_bus_message_append(reply, "i", item_id); + if (r < 0) return r; + + /* properties: a{sv} */ + r = sd_bus_message_open_container(reply, 'a', "{sv}"); + if (r < 0) return r; + + if (item_id == 0) { + /* Root item: "children-display" = "submenu" */ + r = sd_bus_message_append(reply, "{sv}", "children-display", + "s", "submenu"); + if (r < 0) return r; + } else { + menu_item *item = find_item(tray, item_id); + if (item) { + if (item->is_separator) { + r = sd_bus_message_append(reply, "{sv}", "type", "s", "separator"); + if (r < 0) return r; + } else { + r = sd_bus_message_append(reply, "{sv}", "label", + "s", item->label ? item->label : ""); + if (r < 0) return r; + r = sd_bus_message_append(reply, "{sv}", "enabled", + "b", !item->disabled); + if (r < 0) return r; + + if (item->checkable) { + r = sd_bus_message_append(reply, "{sv}", "toggle-type", + "s", "checkmark"); + if (r < 0) return r; + r = sd_bus_message_append(reply, "{sv}", "toggle-state", + "i", item->checked ? 1 : 0); + if (r < 0) return r; + } + + if (!item->visible) { + r = sd_bus_message_append(reply, "{sv}", "visible", + "b", 0); + if (r < 0) return r; + } + + /* Per-item icon: raw PNG/JPG data as icon-data */ + if (item->icon_data && item->icon_len > 0) { + r = sd_bus_message_open_container(reply, 'e', "sv"); + if (r < 0) return r; + r = sd_bus_message_append(reply, "s", "icon-data"); + if (r < 0) return r; + r = sd_bus_message_open_container(reply, 'v', "ay"); + if (r < 0) return r; + r = sd_bus_message_append_array(reply, 'y', + item->icon_data, item->icon_len); + if (r < 0) return r; + r = sd_bus_message_close_container(reply); /* v */ + if (r < 0) return r; + r = sd_bus_message_close_container(reply); /* e */ + if (r < 0) return r; + } + + /* If this item has children, mark as submenu parent */ + int32_t child_ids[MAX_MENU_ITEMS]; + int child_count = get_children(tray, item_id, child_ids, MAX_MENU_ITEMS); + if (child_count > 0) { + r = sd_bus_message_append(reply, "{sv}", "children-display", + "s", "submenu"); + if (r < 0) return r; + } + } + } + } + + r = sd_bus_message_close_container(reply); /* a{sv} */ + if (r < 0) return r; + + /* children: av */ + r = sd_bus_message_open_container(reply, 'a', "v"); + if (r < 0) return r; + + if (depth != 0) { + int32_t child_ids[MAX_MENU_ITEMS]; + int child_count = get_children(tray, item_id, child_ids, MAX_MENU_ITEMS); + + int32_t next_depth = (depth > 0) ? depth - 1 : -1; + for (int i = 0; i < child_count; i++) { + r = sd_bus_message_open_container(reply, 'v', "(ia{sv}av)"); + if (r < 0) return r; + r = append_menu_layout(reply, tray, child_ids[i], next_depth); + if (r < 0) return r; + r = sd_bus_message_close_container(reply); /* v */ + if (r < 0) return r; + } + } + + r = sd_bus_message_close_container(reply); /* av */ + if (r < 0) return r; + + return sd_bus_message_close_container(reply); /* struct */ +} + +/* ========================================================================== */ +/* D-Bus: DBusMenu methods */ +/* ========================================================================== */ + +static int menu_get_layout(sd_bus_message *msg, void *userdata, sd_bus_error *error) { + (void)error; + sni_tray *tray = userdata; + int32_t parent_id, recursion_depth; + sd_bus_message_read(msg, "ii", &parent_id, &recursion_depth); + /* Skip property names array */ + sd_bus_message_skip(msg, "as"); + + sd_bus_message *reply = NULL; + int r = sd_bus_message_new_method_return(msg, &reply); + if (r < 0) return r; + + /* revision */ + r = sd_bus_message_append(reply, "u", tray->menu_version); + if (r < 0) { sd_bus_message_unref(reply); return r; } + + /* layout */ + r = append_menu_layout(reply, tray, parent_id, recursion_depth); + if (r < 0) { sd_bus_message_unref(reply); return r; } + + r = sd_bus_send(tray->bus, reply, NULL); + sd_bus_message_unref(reply); + return r; +} + +static int menu_get_group_properties(sd_bus_message *msg, void *userdata, sd_bus_error *error) { + (void)error; + sni_tray *tray = userdata; + + /* Read ids array */ + int r = sd_bus_message_enter_container(msg, 'a', "i"); + if (r < 0) return r; + + int32_t ids[MAX_MENU_ITEMS]; + int id_count = 0; + int32_t id; + while (sd_bus_message_read(msg, "i", &id) > 0 && id_count < MAX_MENU_ITEMS) { + ids[id_count++] = id; + } + sd_bus_message_exit_container(msg); + + /* Skip property names */ + sd_bus_message_skip(msg, "as"); + + sd_bus_message *reply = NULL; + r = sd_bus_message_new_method_return(msg, &reply); + if (r < 0) return r; + + r = sd_bus_message_open_container(reply, 'a', "(ia{sv})"); + if (r < 0) { sd_bus_message_unref(reply); return r; } + + for (int i = 0; i < id_count; i++) { + menu_item *item = find_item(tray, ids[i]); + if (!item && ids[i] != 0) continue; + + r = sd_bus_message_open_container(reply, 'r', "ia{sv}"); + if (r < 0) break; + r = sd_bus_message_append(reply, "i", ids[i]); + if (r < 0) break; + r = sd_bus_message_open_container(reply, 'a', "{sv}"); + if (r < 0) break; + + if (item) { + if (item->is_separator) { + sd_bus_message_append(reply, "{sv}", "type", "s", "separator"); + } else { + sd_bus_message_append(reply, "{sv}", "label", "s", item->label ? item->label : ""); + sd_bus_message_append(reply, "{sv}", "enabled", "b", !item->disabled); + if (item->checkable) { + sd_bus_message_append(reply, "{sv}", "toggle-type", "s", "checkmark"); + sd_bus_message_append(reply, "{sv}", "toggle-state", "i", item->checked ? 1 : 0); + } + } + } + + sd_bus_message_close_container(reply); /* a{sv} */ + sd_bus_message_close_container(reply); /* struct */ + } + + sd_bus_message_close_container(reply); /* array */ + + r = sd_bus_send(tray->bus, reply, NULL); + sd_bus_message_unref(reply); + return r; +} + +static int menu_get_property(sd_bus_message *msg, void *userdata, sd_bus_error *error) { + (void)error; + sni_tray *tray = userdata; + int32_t id; + const char *name; + sd_bus_message_read(msg, "is", &id, &name); + + sd_bus_message *reply = NULL; + int r = sd_bus_message_new_method_return(msg, &reply); + if (r < 0) return r; + + menu_item *item = (id == 0) ? NULL : find_item(tray, id); + + if (item && strcmp(name, "label") == 0) { + r = sd_bus_message_append(reply, "v", "s", item->label ? item->label : ""); + } else if (item && strcmp(name, "enabled") == 0) { + r = sd_bus_message_append(reply, "v", "b", !item->disabled); + } else { + /* Return empty variant for unknown properties */ + r = sd_bus_message_append(reply, "v", "s", ""); + } + if (r < 0) { sd_bus_message_unref(reply); return r; } + + r = sd_bus_send(tray->bus, reply, NULL); + sd_bus_message_unref(reply); + return r; +} + +static int menu_event(sd_bus_message *msg, void *userdata, sd_bus_error *error) { + (void)error; + sni_tray *tray = userdata; + int32_t id; + const char *event_id; + sd_bus_message_read(msg, "is", &id, &event_id); + /* Skip data variant and timestamp */ + sd_bus_message_skip(msg, "vu"); + + if (strcmp(event_id, "clicked") == 0 && tray->on_menu_item) { + tray->on_menu_item((uint32_t)id, tray->on_menu_item_data); + } + + return sd_bus_reply_method_return(msg, ""); +} + +static int menu_event_group(sd_bus_message *msg, void *userdata, sd_bus_error *error) { + (void)error; + sni_tray *tray = userdata; + + int r = sd_bus_message_enter_container(msg, 'a', "(isvu)"); + if (r < 0) return sd_bus_reply_method_return(msg, "ai", 0); + + while (sd_bus_message_enter_container(msg, 'r', "isvu") > 0) { + int32_t id; + const char *event_id; + sd_bus_message_read(msg, "is", &id, &event_id); + sd_bus_message_skip(msg, "vu"); + sd_bus_message_exit_container(msg); + + if (strcmp(event_id, "clicked") == 0 && tray->on_menu_item) { + tray->on_menu_item((uint32_t)id, tray->on_menu_item_data); + } + } + sd_bus_message_exit_container(msg); + + /* Return empty idErrors array */ + sd_bus_message *reply = NULL; + r = sd_bus_message_new_method_return(msg, &reply); + if (r < 0) return r; + sd_bus_message_open_container(reply, 'a', "i"); + sd_bus_message_close_container(reply); + r = sd_bus_send(tray->bus, reply, NULL); + sd_bus_message_unref(reply); + return r; +} + +static int menu_about_to_show(sd_bus_message *msg, void *userdata, sd_bus_error *error) { + (void)userdata; (void)error; + int32_t id; + sd_bus_message_read(msg, "i", &id); + return sd_bus_reply_method_return(msg, "b", 0); +} + +static int menu_about_to_show_group(sd_bus_message *msg, void *userdata, sd_bus_error *error) { + (void)error; + sni_tray *tray = userdata; + /* Skip input */ + sd_bus_message_skip(msg, "ai"); + + sd_bus_message *reply = NULL; + int r = sd_bus_message_new_method_return(msg, &reply); + if (r < 0) return r; + + /* Empty updatesNeeded */ + sd_bus_message_open_container(reply, 'a', "i"); + sd_bus_message_close_container(reply); + /* Empty idErrors */ + sd_bus_message_open_container(reply, 'a', "i"); + sd_bus_message_close_container(reply); + + r = sd_bus_send(tray->bus, reply, NULL); + sd_bus_message_unref(reply); + return r; +} + +/* ========================================================================== */ +/* D-Bus: DBusMenu property getter */ +/* ========================================================================== */ + +static int menu_get_prop(sd_bus *bus, const char *path, const char *interface, + const char *property, sd_bus_message *reply, + void *userdata, sd_bus_error *error) { + (void)bus; (void)path; (void)interface; (void)error; + sni_tray *tray = userdata; + + if (strcmp(property, "Version") == 0) + return sd_bus_message_append(reply, "u", tray->menu_version); + if (strcmp(property, "TextDirection") == 0) + return sd_bus_message_append(reply, "s", "ltr"); + if (strcmp(property, "Status") == 0) + return sd_bus_message_append(reply, "s", "normal"); + if (strcmp(property, "IconThemePath") == 0) { + int r = sd_bus_message_open_container(reply, 'a', "s"); + if (r < 0) return r; + return sd_bus_message_close_container(reply); + } + + return -ENOENT; +} + +/* ========================================================================== */ +/* D-Bus: DBusMenu vtable */ +/* ========================================================================== */ + +static const sd_bus_vtable menu_vtable[] = { + SD_BUS_VTABLE_START(0), + + /* Properties */ + SD_BUS_PROPERTY("Version", "u", menu_get_prop, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), + SD_BUS_PROPERTY("TextDirection", "s", menu_get_prop, 0, 0), + SD_BUS_PROPERTY("Status", "s", menu_get_prop, 0, 0), + SD_BUS_PROPERTY("IconThemePath", "as", menu_get_prop, 0, 0), + + /* Methods */ + SD_BUS_METHOD("GetLayout", "iias", "u(ia{sv}av)", menu_get_layout, SD_BUS_VTABLE_UNPRIVILEGED), + SD_BUS_METHOD("GetGroupProperties", "aias", "a(ia{sv})", menu_get_group_properties, SD_BUS_VTABLE_UNPRIVILEGED), + SD_BUS_METHOD("GetProperty", "is", "v", menu_get_property, SD_BUS_VTABLE_UNPRIVILEGED), + SD_BUS_METHOD("Event", "isvu", "", menu_event, SD_BUS_VTABLE_UNPRIVILEGED), + SD_BUS_METHOD("EventGroup", "a(isvu)", "ai", menu_event_group, SD_BUS_VTABLE_UNPRIVILEGED), + SD_BUS_METHOD("AboutToShow", "i", "b", menu_about_to_show, SD_BUS_VTABLE_UNPRIVILEGED), + SD_BUS_METHOD("AboutToShowGroup", "ai", "aiai", menu_about_to_show_group, SD_BUS_VTABLE_UNPRIVILEGED), + + /* Signals */ + SD_BUS_SIGNAL("ItemsPropertiesUpdated", "a(ia{sv})a(ias)", 0), + SD_BUS_SIGNAL("LayoutUpdated", "ui", 0), + SD_BUS_SIGNAL("ItemActivationRequested","iu", 0), + + SD_BUS_VTABLE_END +}; + +/* ========================================================================== */ +/* Public API: Lifecycle */ +/* ========================================================================== */ + +sni_tray *sni_tray_create(const uint8_t *icon_data, size_t icon_len, + const char *tooltip) { + sni_tray *tray = calloc(1, sizeof(sni_tray)); + if (!tray) return NULL; + + pthread_mutex_init(&tray->click_lock, NULL); + tray->next_id = 1; + tray->menu_version = 1; + tray->de = detect_desktop(); + tray->current_menu_path = no_menu_path(tray->de); + + if (tooltip) tray->tooltip_text = strdup(tooltip); + if (icon_data && icon_len > 0) { + tray->icon_pixmaps = build_pixmaps(icon_data, icon_len); + } + + if (pipe(tray->quit_pipe) < 0) { + free(tray->tooltip_text); + free(tray); + return NULL; + } + + return tray; +} + +int sni_tray_run(sni_tray *tray) { + int r; + + r = sd_bus_open_user(&tray->bus); + if (r < 0) { + fprintf(stderr, "sni: failed to connect to session bus: %s\n", strerror(-r)); + return r; + } + + /* Export SNI interface */ + r = sd_bus_add_object_vtable(tray->bus, &tray->sni_slot, SNI_PATH, + SNI_IFACE, sni_vtable, tray); + if (r < 0) { + fprintf(stderr, "sni: failed to add SNI vtable: %s\n", strerror(-r)); + return r; + } + + /* Export DBusMenu interface */ + r = sd_bus_add_object_vtable(tray->bus, &tray->menu_slot, MENU_PATH, + MENU_IFACE, menu_vtable, tray); + if (r < 0) { + fprintf(stderr, "sni: failed to add menu vtable: %s\n", strerror(-r)); + return r; + } + + /* Request bus name */ + char name[128]; + snprintf(name, sizeof(name), "org.kde.StatusNotifierItem-%d-1", getpid()); + tray->bus_name = strdup(name); + + r = sd_bus_request_name(tray->bus, name, 0); + if (r < 0) { + fprintf(stderr, "sni: failed to request bus name '%s': %s\n", name, strerror(-r)); + return r; + } + + /* Register with StatusNotifierWatcher */ + sd_bus_error err = SD_BUS_ERROR_NULL; + r = sd_bus_call_method(tray->bus, + WATCHER_BUS, WATCHER_PATH, WATCHER_IFACE, + "RegisterStatusNotifierItem", + &err, NULL, "s", SNI_PATH); + if (r < 0) { + fprintf(stderr, "sni: failed to register with watcher: %s\n", + err.message ? err.message : strerror(-r)); + sd_bus_error_free(&err); + /* Not fatal — some environments don't have a watcher */ + } + + tray->running = 1; + + /* Event loop: process D-Bus messages until quit is signaled */ + int bus_fd = sd_bus_get_fd(tray->bus); + while (tray->running) { + /* Process pending messages first */ + for (;;) { + r = sd_bus_process(tray->bus, NULL); + if (r < 0) { + fprintf(stderr, "sni: bus process error: %s\n", strerror(-r)); + tray->running = 0; + break; + } + if (r == 0) break; /* no more to process */ + } + if (!tray->running) break; + + /* Wait for bus activity or quit signal */ + fd_set rfds; + FD_ZERO(&rfds); + FD_SET(bus_fd, &rfds); + FD_SET(tray->quit_pipe[0], &rfds); + int maxfd = (bus_fd > tray->quit_pipe[0]) ? bus_fd : tray->quit_pipe[0]; + + struct timeval tv = {.tv_sec = 1, .tv_usec = 0}; + int sel = select(maxfd + 1, &rfds, NULL, NULL, &tv); + if (sel < 0 && errno != EINTR) break; + if (FD_ISSET(tray->quit_pipe[0], &rfds)) { + tray->running = 0; + } + } + + /* Teardown */ + sd_bus_release_name(tray->bus, tray->bus_name); + sd_bus_slot_unref(tray->sni_slot); + sd_bus_slot_unref(tray->menu_slot); + tray->sni_slot = NULL; + tray->menu_slot = NULL; + sd_bus_flush_close_unref(tray->bus); + tray->bus = NULL; + + return 0; +} + +void sni_tray_quit(sni_tray *tray) { + if (!tray) return; + tray->running = 0; + /* Wake the select() */ + char c = 1; + if (write(tray->quit_pipe[1], &c, 1) < 0) { /* ignore */ } +} + +void sni_tray_destroy(sni_tray *tray) { + if (!tray) return; + close(tray->quit_pipe[0]); + close(tray->quit_pipe[1]); + free(tray->title); + free(tray->tooltip_text); + free(tray->bus_name); + free_pixmap_list(&tray->icon_pixmaps); + free_menu_items(tray); + pthread_mutex_destroy(&tray->click_lock); + free(tray); +} + +/* ========================================================================== */ +/* Public API: Tray properties */ +/* ========================================================================== */ + +void sni_tray_set_icon(sni_tray *tray, const uint8_t *icon_data, size_t icon_len) { + if (!tray) return; + free_pixmap_list(&tray->icon_pixmaps); + tray->icon_pixmaps = build_pixmaps(icon_data, icon_len); + emit_new_icon(tray); + /* Keep tooltip icon consistent */ + emit_sni_properties_changed(tray, "ToolTip"); +} + +void sni_tray_set_title(sni_tray *tray, const char *title) { + if (!tray) return; + free(tray->title); + tray->title = title ? strdup(title) : NULL; + emit_new_title(tray); +} + +void sni_tray_set_tooltip(sni_tray *tray, const char *tooltip) { + if (!tray) return; + free(tray->tooltip_text); + tray->tooltip_text = tooltip ? strdup(tooltip) : NULL; + emit_sni_properties_changed(tray, "ToolTip"); +} + +/* ========================================================================== */ +/* Public API: Callbacks */ +/* ========================================================================== */ + +void sni_tray_set_click_callback(sni_tray *tray, sni_click_cb cb, void *userdata) { + if (!tray) return; + tray->on_click = cb; + tray->on_click_data = userdata; +} + +void sni_tray_set_rclick_callback(sni_tray *tray, sni_click_cb cb, void *userdata) { + if (!tray) return; + tray->on_rclick = cb; + tray->on_rclick_data = userdata; +} + +void sni_tray_set_menu_callback(sni_tray *tray, sni_menu_item_cb cb, void *userdata) { + if (!tray) return; + tray->on_menu_item = cb; + tray->on_menu_item_data = userdata; +} + +void sni_tray_get_last_click_xy(sni_tray *tray, int32_t *x, int32_t *y) { + if (!tray) return; + pthread_mutex_lock(&tray->click_lock); + if (x) *x = tray->last_click_x; + if (y) *y = tray->last_click_y; + pthread_mutex_unlock(&tray->click_lock); +} + +/* ========================================================================== */ +/* Public API: Menu management */ +/* ========================================================================== */ + +/* Update menu path after adding items (GNOME quirk) */ +static void update_menu_path_after_add(sni_tray *tray) { + if (tray->de == DE_GNOME && strcmp(tray->current_menu_path, MENU_PATH) != 0) { + tray->current_menu_path = MENU_PATH; + emit_sni_properties_changed(tray, "Menu"); + } + /* KDE: always emit LayoutUpdated so items appear */ + emit_layout_updated(tray); +} + +void sni_tray_reset_menu(sni_tray *tray) { + if (!tray) return; + free_menu_items(tray); + tray->menu_version++; + emit_layout_updated(tray); + + /* GNOME: revert to "/" when menu is empty */ + if (tray->de == DE_GNOME) { + tray->current_menu_path = "/"; + emit_sni_properties_changed(tray, "Menu"); + } +} + +uint32_t sni_tray_add_menu_item(sni_tray *tray, const char *title, + const char *tooltip) { + if (!tray) return 0; + menu_item *item = alloc_item(tray); + if (!item) return 0; + item->id = (int32_t)tray->next_id++; + item->label = title ? strdup(title) : NULL; + item->tooltip = tooltip ? strdup(tooltip) : NULL; + item->parent_id = 0; + update_menu_path_after_add(tray); + return (uint32_t)item->id; +} + +uint32_t sni_tray_add_menu_item_checkbox(sni_tray *tray, const char *title, + const char *tooltip, int checked) { + if (!tray) return 0; + menu_item *item = alloc_item(tray); + if (!item) return 0; + item->id = (int32_t)tray->next_id++; + item->label = title ? strdup(title) : NULL; + item->tooltip = tooltip ? strdup(tooltip) : NULL; + item->parent_id = 0; + item->checkable = 1; + item->checked = checked; + update_menu_path_after_add(tray); + return (uint32_t)item->id; +} + +void sni_tray_add_separator(sni_tray *tray) { + if (!tray) return; + menu_item *item = alloc_item(tray); + if (!item) return; + item->id = (int32_t)tray->next_id++; + item->is_separator = 1; + item->parent_id = 0; + update_menu_path_after_add(tray); +} + +uint32_t sni_tray_add_sub_menu_item(sni_tray *tray, uint32_t parent_id, + const char *title, const char *tooltip) { + if (!tray) return 0; + menu_item *item = alloc_item(tray); + if (!item) return 0; + item->id = (int32_t)tray->next_id++; + item->label = title ? strdup(title) : NULL; + item->tooltip = tooltip ? strdup(tooltip) : NULL; + item->parent_id = (int32_t)parent_id; + update_menu_path_after_add(tray); + return (uint32_t)item->id; +} + +uint32_t sni_tray_add_sub_menu_item_checkbox(sni_tray *tray, uint32_t parent_id, + const char *title, const char *tooltip, + int checked) { + if (!tray) return 0; + menu_item *item = alloc_item(tray); + if (!item) return 0; + item->id = (int32_t)tray->next_id++; + item->label = title ? strdup(title) : NULL; + item->tooltip = tooltip ? strdup(tooltip) : NULL; + item->parent_id = (int32_t)parent_id; + item->checkable = 1; + item->checked = checked; + update_menu_path_after_add(tray); + return (uint32_t)item->id; +} + +void sni_tray_add_sub_separator(sni_tray *tray, uint32_t parent_id) { + if (!tray) return; + menu_item *item = alloc_item(tray); + if (!item) return; + item->id = (int32_t)tray->next_id++; + item->is_separator = 1; + item->parent_id = (int32_t)parent_id; + update_menu_path_after_add(tray); +} + +/* ========================================================================== */ +/* Public API: Per-item operations */ +/* ========================================================================== */ + +int sni_tray_item_set_title(sni_tray *tray, uint32_t id, const char *title) { + if (!tray) return 0; + menu_item *item = find_item(tray, (int32_t)id); + if (!item) return 0; + free(item->label); + item->label = title ? strdup(title) : NULL; + emit_layout_updated(tray); + return 1; +} + +void sni_tray_item_enable(sni_tray *tray, uint32_t id) { + if (!tray) return; + menu_item *item = find_item(tray, (int32_t)id); + if (item) { item->disabled = 0; emit_layout_updated(tray); } +} + +void sni_tray_item_disable(sni_tray *tray, uint32_t id) { + if (!tray) return; + menu_item *item = find_item(tray, (int32_t)id); + if (item) { item->disabled = 1; emit_layout_updated(tray); } +} + +void sni_tray_item_show(sni_tray *tray, uint32_t id) { + if (!tray) return; + menu_item *item = find_item(tray, (int32_t)id); + if (item) { item->visible = 1; emit_layout_updated(tray); } +} + +void sni_tray_item_hide(sni_tray *tray, uint32_t id) { + if (!tray) return; + menu_item *item = find_item(tray, (int32_t)id); + if (item) { item->visible = 0; emit_layout_updated(tray); } +} + +void sni_tray_item_check(sni_tray *tray, uint32_t id) { + if (!tray) return; + menu_item *item = find_item(tray, (int32_t)id); + if (item) { item->checked = 1; emit_layout_updated(tray); } +} + +void sni_tray_item_uncheck(sni_tray *tray, uint32_t id) { + if (!tray) return; + menu_item *item = find_item(tray, (int32_t)id); + if (item) { item->checked = 0; emit_layout_updated(tray); } +} + +void sni_tray_item_set_icon(sni_tray *tray, uint32_t id, + const uint8_t *icon_data, size_t icon_len) { + if (!tray) return; + menu_item *item = find_item(tray, (int32_t)id); + if (!item) return; + free(item->icon_data); + item->icon_data = NULL; + item->icon_len = 0; + if (icon_data && icon_len > 0) { + item->icon_data = malloc(icon_len); + if (item->icon_data) { + memcpy(item->icon_data, icon_data, icon_len); + item->icon_len = icon_len; + } + } + emit_layout_updated(tray); +} diff --git a/src/native/linux/sni.h b/src/native/linux/sni.h new file mode 100644 index 00000000..fd85f2d4 --- /dev/null +++ b/src/native/linux/sni.h @@ -0,0 +1,102 @@ +/* + * sni.h – StatusNotifierItem + DBusMenu over sd-bus for Linux system tray. + * + * Single-header API: the JNI bridge only talks to this. + */ + +#ifndef SNI_H +#define SNI_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* Opaque tray handle */ +typedef struct sni_tray sni_tray; + +/* Callback types */ +typedef void (*sni_click_cb)(int32_t x, int32_t y, void *userdata); +typedef void (*sni_menu_item_cb)(uint32_t id, void *userdata); + +/* ── Lifecycle ─────────────────────────────────────────────────────── */ + +/* Create a new tray instance. Returns NULL on failure. + * icon_data/icon_len: PNG/JPG bytes for the tray icon. + * tooltip: tooltip text (UTF-8). */ +sni_tray *sni_tray_create(const uint8_t *icon_data, size_t icon_len, + const char *tooltip); + +/* Start the D-Bus event loop in the current thread (blocks). + * Call sni_tray_quit() from another thread to unblock. */ +int sni_tray_run(sni_tray *tray); + +/* Signal the event loop to stop. Thread-safe. */ +void sni_tray_quit(sni_tray *tray); + +/* Destroy the tray and release all resources. + * Must be called after sni_tray_run() returns. */ +void sni_tray_destroy(sni_tray *tray); + +/* ── Tray properties ───────────────────────────────────────────────── */ + +void sni_tray_set_icon(sni_tray *tray, const uint8_t *icon_data, size_t icon_len); +void sni_tray_set_title(sni_tray *tray, const char *title); +void sni_tray_set_tooltip(sni_tray *tray, const char *tooltip); + +/* ── Click callbacks ───────────────────────────────────────────────── */ + +void sni_tray_set_click_callback(sni_tray *tray, sni_click_cb cb, void *userdata); +void sni_tray_set_rclick_callback(sni_tray *tray, sni_click_cb cb, void *userdata); +void sni_tray_set_menu_callback(sni_tray *tray, sni_menu_item_cb cb, void *userdata); + +/* Get last click coordinates (from Activate/ContextMenu). */ +void sni_tray_get_last_click_xy(sni_tray *tray, int32_t *x, int32_t *y); + +/* ── Menu management ───────────────────────────────────────────────── */ + +/* Clear all menu items. */ +void sni_tray_reset_menu(sni_tray *tray); + +/* Add a top-level menu item. Returns the item ID (>0), or 0 on failure. */ +uint32_t sni_tray_add_menu_item(sni_tray *tray, const char *title, + const char *tooltip); + +/* Add a top-level checkable menu item. */ +uint32_t sni_tray_add_menu_item_checkbox(sni_tray *tray, const char *title, + const char *tooltip, int checked); + +/* Add a top-level separator. */ +void sni_tray_add_separator(sni_tray *tray); + +/* Add a child item under parent_id. */ +uint32_t sni_tray_add_sub_menu_item(sni_tray *tray, uint32_t parent_id, + const char *title, const char *tooltip); + +/* Add a checkable child item under parent_id. */ +uint32_t sni_tray_add_sub_menu_item_checkbox(sni_tray *tray, uint32_t parent_id, + const char *title, const char *tooltip, + int checked); + +/* Add a separator under parent_id. */ +void sni_tray_add_sub_separator(sni_tray *tray, uint32_t parent_id); + +/* ── Per-item operations ───────────────────────────────────────────── */ + +int sni_tray_item_set_title(sni_tray *tray, uint32_t id, const char *title); +void sni_tray_item_enable(sni_tray *tray, uint32_t id); +void sni_tray_item_disable(sni_tray *tray, uint32_t id); +void sni_tray_item_show(sni_tray *tray, uint32_t id); +void sni_tray_item_hide(sni_tray *tray, uint32_t id); +void sni_tray_item_check(sni_tray *tray, uint32_t id); +void sni_tray_item_uncheck(sni_tray *tray, uint32_t id); +void sni_tray_item_set_icon(sni_tray *tray, uint32_t id, + const uint8_t *icon_data, size_t icon_len); + +#ifdef __cplusplus +} +#endif + +#endif /* SNI_H */ diff --git a/src/native/linux/stb_image.h b/src/native/linux/stb_image.h new file mode 100644 index 00000000..9eedabed --- /dev/null +++ b/src/native/linux/stb_image.h @@ -0,0 +1,7988 @@ +/* stb_image - v2.30 - public domain image loader - http://nothings.org/stb + no warranty implied; use at your own risk + + Do this: + #define STB_IMAGE_IMPLEMENTATION + before you include this file in *one* C or C++ file to create the implementation. + + // i.e. it should look like this: + #include ... + #include ... + #include ... + #define STB_IMAGE_IMPLEMENTATION + #include "stb_image.h" + + You can #define STBI_ASSERT(x) before the #include to avoid using assert.h. + And #define STBI_MALLOC, STBI_REALLOC, and STBI_FREE to avoid using malloc,realloc,free + + + QUICK NOTES: + Primarily of interest to game developers and other people who can + avoid problematic images and only need the trivial interface + + JPEG baseline & progressive (12 bpc/arithmetic not supported, same as stock IJG lib) + PNG 1/2/4/8/16-bit-per-channel + + TGA (not sure what subset, if a subset) + BMP non-1bpp, non-RLE + PSD (composited view only, no extra channels, 8/16 bit-per-channel) + + GIF (*comp always reports as 4-channel) + HDR (radiance rgbE format) + PIC (Softimage PIC) + PNM (PPM and PGM binary only) + + Animated GIF still needs a proper API, but here's one way to do it: + http://gist.github.com/urraka/685d9a6340b26b830d49 + + - decode from memory or through FILE (define STBI_NO_STDIO to remove code) + - decode from arbitrary I/O callbacks + - SIMD acceleration on x86/x64 (SSE2) and ARM (NEON) + + Full documentation under "DOCUMENTATION" below. + + +LICENSE + + See end of file for license information. + +RECENT REVISION HISTORY: + + 2.30 (2024-05-31) avoid erroneous gcc warning + 2.29 (2023-05-xx) optimizations + 2.28 (2023-01-29) many error fixes, security errors, just tons of stuff + 2.27 (2021-07-11) document stbi_info better, 16-bit PNM support, bug fixes + 2.26 (2020-07-13) many minor fixes + 2.25 (2020-02-02) fix warnings + 2.24 (2020-02-02) fix warnings; thread-local failure_reason and flip_vertically + 2.23 (2019-08-11) fix clang static analysis warning + 2.22 (2019-03-04) gif fixes, fix warnings + 2.21 (2019-02-25) fix typo in comment + 2.20 (2019-02-07) support utf8 filenames in Windows; fix warnings and platform ifdefs + 2.19 (2018-02-11) fix warning + 2.18 (2018-01-30) fix warnings + 2.17 (2018-01-29) bugfix, 1-bit BMP, 16-bitness query, fix warnings + 2.16 (2017-07-23) all functions have 16-bit variants; optimizations; bugfixes + 2.15 (2017-03-18) fix png-1,2,4; all Imagenet JPGs; no runtime SSE detection on GCC + 2.14 (2017-03-03) remove deprecated STBI_JPEG_OLD; fixes for Imagenet JPGs + 2.13 (2016-12-04) experimental 16-bit API, only for PNG so far; fixes + 2.12 (2016-04-02) fix typo in 2.11 PSD fix that caused crashes + 2.11 (2016-04-02) 16-bit PNGS; enable SSE2 in non-gcc x64 + RGB-format JPEG; remove white matting in PSD; + allocate large structures on the stack; + correct channel count for PNG & BMP + 2.10 (2016-01-22) avoid warning introduced in 2.09 + 2.09 (2016-01-16) 16-bit TGA; comments in PNM files; STBI_REALLOC_SIZED + + See end of file for full revision history. + + + ============================ Contributors ========================= + + Image formats Extensions, features + Sean Barrett (jpeg, png, bmp) Jetro Lauha (stbi_info) + Nicolas Schulz (hdr, psd) Martin "SpartanJ" Golini (stbi_info) + Jonathan Dummer (tga) James "moose2000" Brown (iPhone PNG) + Jean-Marc Lienher (gif) Ben "Disch" Wenger (io callbacks) + Tom Seddon (pic) Omar Cornut (1/2/4-bit PNG) + Thatcher Ulrich (psd) Nicolas Guillemot (vertical flip) + Ken Miller (pgm, ppm) Richard Mitton (16-bit PSD) + github:urraka (animated gif) Junggon Kim (PNM comments) + Christopher Forseth (animated gif) Daniel Gibson (16-bit TGA) + socks-the-fox (16-bit PNG) + Jeremy Sawicki (handle all ImageNet JPGs) + Optimizations & bugfixes Mikhail Morozov (1-bit BMP) + Fabian "ryg" Giesen Anael Seghezzi (is-16-bit query) + Arseny Kapoulkine Simon Breuss (16-bit PNM) + John-Mark Allen + Carmelo J Fdez-Aguera + + Bug & warning fixes + Marc LeBlanc David Woo Guillaume George Martins Mozeiko + Christpher Lloyd Jerry Jansson Joseph Thomson Blazej Dariusz Roszkowski + Phil Jordan Dave Moore Roy Eltham + Hayaki Saito Nathan Reed Won Chun + Luke Graham Johan Duparc Nick Verigakis the Horde3D community + Thomas Ruf Ronny Chevalier github:rlyeh + Janez Zemva John Bartholomew Michal Cichon github:romigrou + Jonathan Blow Ken Hamada Tero Hanninen github:svdijk + Eugene Golushkov Laurent Gomila Cort Stratton github:snagar + Aruelien Pocheville Sergio Gonzalez Thibault Reuille github:Zelex + Cass Everitt Ryamond Barbiero github:grim210 + Paul Du Bois Engin Manap Aldo Culquicondor github:sammyhw + Philipp Wiesemann Dale Weiler Oriol Ferrer Mesia github:phprus + Josh Tobin Neil Bickford Matthew Gregan github:poppolopoppo + Julian Raschke Gregory Mullen Christian Floisand github:darealshinji + Baldur Karlsson Kevin Schmidt JR Smith github:Michaelangel007 + Brad Weinberger Matvey Cherevko github:mosra + Luca Sas Alexander Veselov Zack Middleton [reserved] + Ryan C. Gordon [reserved] [reserved] + DO NOT ADD YOUR NAME HERE + + Jacko Dirks + + To add your name to the credits, pick a random blank space in the middle and fill it. + 80% of merge conflicts on stb PRs are due to people adding their name at the end + of the credits. +*/ + +#ifndef STBI_INCLUDE_STB_IMAGE_H +#define STBI_INCLUDE_STB_IMAGE_H + +// DOCUMENTATION +// +// Limitations: +// - no 12-bit-per-channel JPEG +// - no JPEGs with arithmetic coding +// - GIF always returns *comp=4 +// +// Basic usage (see HDR discussion below for HDR usage): +// int x,y,n; +// unsigned char *data = stbi_load(filename, &x, &y, &n, 0); +// // ... process data if not NULL ... +// // ... x = width, y = height, n = # 8-bit components per pixel ... +// // ... replace '0' with '1'..'4' to force that many components per pixel +// // ... but 'n' will always be the number that it would have been if you said 0 +// stbi_image_free(data); +// +// Standard parameters: +// int *x -- outputs image width in pixels +// int *y -- outputs image height in pixels +// int *channels_in_file -- outputs # of image components in image file +// int desired_channels -- if non-zero, # of image components requested in result +// +// The return value from an image loader is an 'unsigned char *' which points +// to the pixel data, or NULL on an allocation failure or if the image is +// corrupt or invalid. The pixel data consists of *y scanlines of *x pixels, +// with each pixel consisting of N interleaved 8-bit components; the first +// pixel pointed to is top-left-most in the image. There is no padding between +// image scanlines or between pixels, regardless of format. The number of +// components N is 'desired_channels' if desired_channels is non-zero, or +// *channels_in_file otherwise. If desired_channels is non-zero, +// *channels_in_file has the number of components that _would_ have been +// output otherwise. E.g. if you set desired_channels to 4, you will always +// get RGBA output, but you can check *channels_in_file to see if it's trivially +// opaque because e.g. there were only 3 channels in the source image. +// +// An output image with N components has the following components interleaved +// in this order in each pixel: +// +// N=#comp components +// 1 grey +// 2 grey, alpha +// 3 red, green, blue +// 4 red, green, blue, alpha +// +// If image loading fails for any reason, the return value will be NULL, +// and *x, *y, *channels_in_file will be unchanged. The function +// stbi_failure_reason() can be queried for an extremely brief, end-user +// unfriendly explanation of why the load failed. Define STBI_NO_FAILURE_STRINGS +// to avoid compiling these strings at all, and STBI_FAILURE_USERMSG to get slightly +// more user-friendly ones. +// +// Paletted PNG, BMP, GIF, and PIC images are automatically depalettized. +// +// To query the width, height and component count of an image without having to +// decode the full file, you can use the stbi_info family of functions: +// +// int x,y,n,ok; +// ok = stbi_info(filename, &x, &y, &n); +// // returns ok=1 and sets x, y, n if image is a supported format, +// // 0 otherwise. +// +// Note that stb_image pervasively uses ints in its public API for sizes, +// including sizes of memory buffers. This is now part of the API and thus +// hard to change without causing breakage. As a result, the various image +// loaders all have certain limits on image size; these differ somewhat +// by format but generally boil down to either just under 2GB or just under +// 1GB. When the decoded image would be larger than this, stb_image decoding +// will fail. +// +// Additionally, stb_image will reject image files that have any of their +// dimensions set to a larger value than the configurable STBI_MAX_DIMENSIONS, +// which defaults to 2**24 = 16777216 pixels. Due to the above memory limit, +// the only way to have an image with such dimensions load correctly +// is for it to have a rather extreme aspect ratio. Either way, the +// assumption here is that such larger images are likely to be malformed +// or malicious. If you do need to load an image with individual dimensions +// larger than that, and it still fits in the overall size limit, you can +// #define STBI_MAX_DIMENSIONS on your own to be something larger. +// +// =========================================================================== +// +// UNICODE: +// +// If compiling for Windows and you wish to use Unicode filenames, compile +// with +// #define STBI_WINDOWS_UTF8 +// and pass utf8-encoded filenames. Call stbi_convert_wchar_to_utf8 to convert +// Windows wchar_t filenames to utf8. +// +// =========================================================================== +// +// Philosophy +// +// stb libraries are designed with the following priorities: +// +// 1. easy to use +// 2. easy to maintain +// 3. good performance +// +// Sometimes I let "good performance" creep up in priority over "easy to maintain", +// and for best performance I may provide less-easy-to-use APIs that give higher +// performance, in addition to the easy-to-use ones. Nevertheless, it's important +// to keep in mind that from the standpoint of you, a client of this library, +// all you care about is #1 and #3, and stb libraries DO NOT emphasize #3 above all. +// +// Some secondary priorities arise directly from the first two, some of which +// provide more explicit reasons why performance can't be emphasized. +// +// - Portable ("ease of use") +// - Small source code footprint ("easy to maintain") +// - No dependencies ("ease of use") +// +// =========================================================================== +// +// I/O callbacks +// +// I/O callbacks allow you to read from arbitrary sources, like packaged +// files or some other source. Data read from callbacks are processed +// through a small internal buffer (currently 128 bytes) to try to reduce +// overhead. +// +// The three functions you must define are "read" (reads some bytes of data), +// "skip" (skips some bytes of data), "eof" (reports if the stream is at the end). +// +// =========================================================================== +// +// SIMD support +// +// The JPEG decoder will try to automatically use SIMD kernels on x86 when +// supported by the compiler. For ARM Neon support, you must explicitly +// request it. +// +// (The old do-it-yourself SIMD API is no longer supported in the current +// code.) +// +// On x86, SSE2 will automatically be used when available based on a run-time +// test; if not, the generic C versions are used as a fall-back. On ARM targets, +// the typical path is to have separate builds for NEON and non-NEON devices +// (at least this is true for iOS and Android). Therefore, the NEON support is +// toggled by a build flag: define STBI_NEON to get NEON loops. +// +// If for some reason you do not want to use any of SIMD code, or if +// you have issues compiling it, you can disable it entirely by +// defining STBI_NO_SIMD. +// +// =========================================================================== +// +// HDR image support (disable by defining STBI_NO_HDR) +// +// stb_image supports loading HDR images in general, and currently the Radiance +// .HDR file format specifically. You can still load any file through the existing +// interface; if you attempt to load an HDR file, it will be automatically remapped +// to LDR, assuming gamma 2.2 and an arbitrary scale factor defaulting to 1; +// both of these constants can be reconfigured through this interface: +// +// stbi_hdr_to_ldr_gamma(2.2f); +// stbi_hdr_to_ldr_scale(1.0f); +// +// (note, do not use _inverse_ constants; stbi_image will invert them +// appropriately). +// +// Additionally, there is a new, parallel interface for loading files as +// (linear) floats to preserve the full dynamic range: +// +// float *data = stbi_loadf(filename, &x, &y, &n, 0); +// +// If you load LDR images through this interface, those images will +// be promoted to floating point values, run through the inverse of +// constants corresponding to the above: +// +// stbi_ldr_to_hdr_scale(1.0f); +// stbi_ldr_to_hdr_gamma(2.2f); +// +// Finally, given a filename (or an open file or memory block--see header +// file for details) containing image data, you can query for the "most +// appropriate" interface to use (that is, whether the image is HDR or +// not), using: +// +// stbi_is_hdr(char *filename); +// +// =========================================================================== +// +// iPhone PNG support: +// +// We optionally support converting iPhone-formatted PNGs (which store +// premultiplied BGRA) back to RGB, even though they're internally encoded +// differently. To enable this conversion, call +// stbi_convert_iphone_png_to_rgb(1). +// +// Call stbi_set_unpremultiply_on_load(1) as well to force a divide per +// pixel to remove any premultiplied alpha *only* if the image file explicitly +// says there's premultiplied data (currently only happens in iPhone images, +// and only if iPhone convert-to-rgb processing is on). +// +// =========================================================================== +// +// ADDITIONAL CONFIGURATION +// +// - You can suppress implementation of any of the decoders to reduce +// your code footprint by #defining one or more of the following +// symbols before creating the implementation. +// +// STBI_NO_JPEG +// STBI_NO_PNG +// STBI_NO_BMP +// STBI_NO_PSD +// STBI_NO_TGA +// STBI_NO_GIF +// STBI_NO_HDR +// STBI_NO_PIC +// STBI_NO_PNM (.ppm and .pgm) +// +// - You can request *only* certain decoders and suppress all other ones +// (this will be more forward-compatible, as addition of new decoders +// doesn't require you to disable them explicitly): +// +// STBI_ONLY_JPEG +// STBI_ONLY_PNG +// STBI_ONLY_BMP +// STBI_ONLY_PSD +// STBI_ONLY_TGA +// STBI_ONLY_GIF +// STBI_ONLY_HDR +// STBI_ONLY_PIC +// STBI_ONLY_PNM (.ppm and .pgm) +// +// - If you use STBI_NO_PNG (or _ONLY_ without PNG), and you still +// want the zlib decoder to be available, #define STBI_SUPPORT_ZLIB +// +// - If you define STBI_MAX_DIMENSIONS, stb_image will reject images greater +// than that size (in either width or height) without further processing. +// This is to let programs in the wild set an upper bound to prevent +// denial-of-service attacks on untrusted data, as one could generate a +// valid image of gigantic dimensions and force stb_image to allocate a +// huge block of memory and spend disproportionate time decoding it. By +// default this is set to (1 << 24), which is 16777216, but that's still +// very big. + +#ifndef STBI_NO_STDIO +#include +#endif // STBI_NO_STDIO + +#define STBI_VERSION 1 + +enum +{ + STBI_default = 0, // only used for desired_channels + + STBI_grey = 1, + STBI_grey_alpha = 2, + STBI_rgb = 3, + STBI_rgb_alpha = 4 +}; + +#include +typedef unsigned char stbi_uc; +typedef unsigned short stbi_us; + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef STBIDEF +#ifdef STB_IMAGE_STATIC +#define STBIDEF static +#else +#define STBIDEF extern +#endif +#endif + +////////////////////////////////////////////////////////////////////////////// +// +// PRIMARY API - works on images of any type +// + +// +// load image by filename, open file, or memory buffer +// + +typedef struct +{ + int (*read) (void *user,char *data,int size); // fill 'data' with 'size' bytes. return number of bytes actually read + void (*skip) (void *user,int n); // skip the next 'n' bytes, or 'unget' the last -n bytes if negative + int (*eof) (void *user); // returns nonzero if we are at end of file/data +} stbi_io_callbacks; + +//////////////////////////////////// +// +// 8-bits-per-channel interface +// + +STBIDEF stbi_uc *stbi_load_from_memory (stbi_uc const *buffer, int len , int *x, int *y, int *channels_in_file, int desired_channels); +STBIDEF stbi_uc *stbi_load_from_callbacks(stbi_io_callbacks const *clbk , void *user, int *x, int *y, int *channels_in_file, int desired_channels); + +#ifndef STBI_NO_STDIO +STBIDEF stbi_uc *stbi_load (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels); +STBIDEF stbi_uc *stbi_load_from_file (FILE *f, int *x, int *y, int *channels_in_file, int desired_channels); +// for stbi_load_from_file, file pointer is left pointing immediately after image +#endif + +#ifndef STBI_NO_GIF +STBIDEF stbi_uc *stbi_load_gif_from_memory(stbi_uc const *buffer, int len, int **delays, int *x, int *y, int *z, int *comp, int req_comp); +#endif + +#ifdef STBI_WINDOWS_UTF8 +STBIDEF int stbi_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input); +#endif + +//////////////////////////////////// +// +// 16-bits-per-channel interface +// + +STBIDEF stbi_us *stbi_load_16_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels); +STBIDEF stbi_us *stbi_load_16_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels); + +#ifndef STBI_NO_STDIO +STBIDEF stbi_us *stbi_load_16 (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels); +STBIDEF stbi_us *stbi_load_from_file_16(FILE *f, int *x, int *y, int *channels_in_file, int desired_channels); +#endif + +//////////////////////////////////// +// +// float-per-channel interface +// +#ifndef STBI_NO_LINEAR + STBIDEF float *stbi_loadf_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels); + STBIDEF float *stbi_loadf_from_callbacks (stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels); + + #ifndef STBI_NO_STDIO + STBIDEF float *stbi_loadf (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels); + STBIDEF float *stbi_loadf_from_file (FILE *f, int *x, int *y, int *channels_in_file, int desired_channels); + #endif +#endif + +#ifndef STBI_NO_HDR + STBIDEF void stbi_hdr_to_ldr_gamma(float gamma); + STBIDEF void stbi_hdr_to_ldr_scale(float scale); +#endif // STBI_NO_HDR + +#ifndef STBI_NO_LINEAR + STBIDEF void stbi_ldr_to_hdr_gamma(float gamma); + STBIDEF void stbi_ldr_to_hdr_scale(float scale); +#endif // STBI_NO_LINEAR + +// stbi_is_hdr is always defined, but always returns false if STBI_NO_HDR +STBIDEF int stbi_is_hdr_from_callbacks(stbi_io_callbacks const *clbk, void *user); +STBIDEF int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len); +#ifndef STBI_NO_STDIO +STBIDEF int stbi_is_hdr (char const *filename); +STBIDEF int stbi_is_hdr_from_file(FILE *f); +#endif // STBI_NO_STDIO + + +// get a VERY brief reason for failure +// on most compilers (and ALL modern mainstream compilers) this is threadsafe +STBIDEF const char *stbi_failure_reason (void); + +// free the loaded image -- this is just free() +STBIDEF void stbi_image_free (void *retval_from_stbi_load); + +// get image dimensions & components without fully decoding +STBIDEF int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp); +STBIDEF int stbi_info_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp); +STBIDEF int stbi_is_16_bit_from_memory(stbi_uc const *buffer, int len); +STBIDEF int stbi_is_16_bit_from_callbacks(stbi_io_callbacks const *clbk, void *user); + +#ifndef STBI_NO_STDIO +STBIDEF int stbi_info (char const *filename, int *x, int *y, int *comp); +STBIDEF int stbi_info_from_file (FILE *f, int *x, int *y, int *comp); +STBIDEF int stbi_is_16_bit (char const *filename); +STBIDEF int stbi_is_16_bit_from_file(FILE *f); +#endif + + + +// for image formats that explicitly notate that they have premultiplied alpha, +// we just return the colors as stored in the file. set this flag to force +// unpremultiplication. results are undefined if the unpremultiply overflow. +STBIDEF void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply); + +// indicate whether we should process iphone images back to canonical format, +// or just pass them through "as-is" +STBIDEF void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert); + +// flip the image vertically, so the first pixel in the output array is the bottom left +STBIDEF void stbi_set_flip_vertically_on_load(int flag_true_if_should_flip); + +// as above, but only applies to images loaded on the thread that calls the function +// this function is only available if your compiler supports thread-local variables; +// calling it will fail to link if your compiler doesn't +STBIDEF void stbi_set_unpremultiply_on_load_thread(int flag_true_if_should_unpremultiply); +STBIDEF void stbi_convert_iphone_png_to_rgb_thread(int flag_true_if_should_convert); +STBIDEF void stbi_set_flip_vertically_on_load_thread(int flag_true_if_should_flip); + +// ZLIB client - used by PNG, available for other purposes + +STBIDEF char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen); +STBIDEF char *stbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header); +STBIDEF char *stbi_zlib_decode_malloc(const char *buffer, int len, int *outlen); +STBIDEF int stbi_zlib_decode_buffer(char *obuffer, int olen, const char *ibuffer, int ilen); + +STBIDEF char *stbi_zlib_decode_noheader_malloc(const char *buffer, int len, int *outlen); +STBIDEF int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen); + + +#ifdef __cplusplus +} +#endif + +// +// +//// end header file ///////////////////////////////////////////////////// +#endif // STBI_INCLUDE_STB_IMAGE_H + +#ifdef STB_IMAGE_IMPLEMENTATION + +#if defined(STBI_ONLY_JPEG) || defined(STBI_ONLY_PNG) || defined(STBI_ONLY_BMP) \ + || defined(STBI_ONLY_TGA) || defined(STBI_ONLY_GIF) || defined(STBI_ONLY_PSD) \ + || defined(STBI_ONLY_HDR) || defined(STBI_ONLY_PIC) || defined(STBI_ONLY_PNM) \ + || defined(STBI_ONLY_ZLIB) + #ifndef STBI_ONLY_JPEG + #define STBI_NO_JPEG + #endif + #ifndef STBI_ONLY_PNG + #define STBI_NO_PNG + #endif + #ifndef STBI_ONLY_BMP + #define STBI_NO_BMP + #endif + #ifndef STBI_ONLY_PSD + #define STBI_NO_PSD + #endif + #ifndef STBI_ONLY_TGA + #define STBI_NO_TGA + #endif + #ifndef STBI_ONLY_GIF + #define STBI_NO_GIF + #endif + #ifndef STBI_ONLY_HDR + #define STBI_NO_HDR + #endif + #ifndef STBI_ONLY_PIC + #define STBI_NO_PIC + #endif + #ifndef STBI_ONLY_PNM + #define STBI_NO_PNM + #endif +#endif + +#if defined(STBI_NO_PNG) && !defined(STBI_SUPPORT_ZLIB) && !defined(STBI_NO_ZLIB) +#define STBI_NO_ZLIB +#endif + + +#include +#include // ptrdiff_t on osx +#include +#include +#include + +#if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR) +#include // ldexp, pow +#endif + +#ifndef STBI_NO_STDIO +#include +#endif + +#ifndef STBI_ASSERT +#include +#define STBI_ASSERT(x) assert(x) +#endif + +#ifdef __cplusplus +#define STBI_EXTERN extern "C" +#else +#define STBI_EXTERN extern +#endif + + +#ifndef _MSC_VER + #ifdef __cplusplus + #define stbi_inline inline + #else + #define stbi_inline + #endif +#else + #define stbi_inline __forceinline +#endif + +#ifndef STBI_NO_THREAD_LOCALS + #if defined(__cplusplus) && __cplusplus >= 201103L + #define STBI_THREAD_LOCAL thread_local + #elif defined(__GNUC__) && __GNUC__ < 5 + #define STBI_THREAD_LOCAL __thread + #elif defined(_MSC_VER) + #define STBI_THREAD_LOCAL __declspec(thread) + #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_THREADS__) + #define STBI_THREAD_LOCAL _Thread_local + #endif + + #ifndef STBI_THREAD_LOCAL + #if defined(__GNUC__) + #define STBI_THREAD_LOCAL __thread + #endif + #endif +#endif + +#if defined(_MSC_VER) || defined(__SYMBIAN32__) +typedef unsigned short stbi__uint16; +typedef signed short stbi__int16; +typedef unsigned int stbi__uint32; +typedef signed int stbi__int32; +#else +#include +typedef uint16_t stbi__uint16; +typedef int16_t stbi__int16; +typedef uint32_t stbi__uint32; +typedef int32_t stbi__int32; +#endif + +// should produce compiler error if size is wrong +typedef unsigned char validate_uint32[sizeof(stbi__uint32)==4 ? 1 : -1]; + +#ifdef _MSC_VER +#define STBI_NOTUSED(v) (void)(v) +#else +#define STBI_NOTUSED(v) (void)sizeof(v) +#endif + +#ifdef _MSC_VER +#define STBI_HAS_LROTL +#endif + +#ifdef STBI_HAS_LROTL + #define stbi_lrot(x,y) _lrotl(x,y) +#else + #define stbi_lrot(x,y) (((x) << (y)) | ((x) >> (-(y) & 31))) +#endif + +#if defined(STBI_MALLOC) && defined(STBI_FREE) && (defined(STBI_REALLOC) || defined(STBI_REALLOC_SIZED)) +// ok +#elif !defined(STBI_MALLOC) && !defined(STBI_FREE) && !defined(STBI_REALLOC) && !defined(STBI_REALLOC_SIZED) +// ok +#else +#error "Must define all or none of STBI_MALLOC, STBI_FREE, and STBI_REALLOC (or STBI_REALLOC_SIZED)." +#endif + +#ifndef STBI_MALLOC +#define STBI_MALLOC(sz) malloc(sz) +#define STBI_REALLOC(p,newsz) realloc(p,newsz) +#define STBI_FREE(p) free(p) +#endif + +#ifndef STBI_REALLOC_SIZED +#define STBI_REALLOC_SIZED(p,oldsz,newsz) STBI_REALLOC(p,newsz) +#endif + +// x86/x64 detection +#if defined(__x86_64__) || defined(_M_X64) +#define STBI__X64_TARGET +#elif defined(__i386) || defined(_M_IX86) +#define STBI__X86_TARGET +#endif + +#if defined(__GNUC__) && defined(STBI__X86_TARGET) && !defined(__SSE2__) && !defined(STBI_NO_SIMD) +// gcc doesn't support sse2 intrinsics unless you compile with -msse2, +// which in turn means it gets to use SSE2 everywhere. This is unfortunate, +// but previous attempts to provide the SSE2 functions with runtime +// detection caused numerous issues. The way architecture extensions are +// exposed in GCC/Clang is, sadly, not really suited for one-file libs. +// New behavior: if compiled with -msse2, we use SSE2 without any +// detection; if not, we don't use it at all. +#define STBI_NO_SIMD +#endif + +#if defined(__MINGW32__) && defined(STBI__X86_TARGET) && !defined(STBI_MINGW_ENABLE_SSE2) && !defined(STBI_NO_SIMD) +// Note that __MINGW32__ doesn't actually mean 32-bit, so we have to avoid STBI__X64_TARGET +// +// 32-bit MinGW wants ESP to be 16-byte aligned, but this is not in the +// Windows ABI and VC++ as well as Windows DLLs don't maintain that invariant. +// As a result, enabling SSE2 on 32-bit MinGW is dangerous when not +// simultaneously enabling "-mstackrealign". +// +// See https://github.com/nothings/stb/issues/81 for more information. +// +// So default to no SSE2 on 32-bit MinGW. If you've read this far and added +// -mstackrealign to your build settings, feel free to #define STBI_MINGW_ENABLE_SSE2. +#define STBI_NO_SIMD +#endif + +#if !defined(STBI_NO_SIMD) && (defined(STBI__X86_TARGET) || defined(STBI__X64_TARGET)) +#define STBI_SSE2 +#include + +#ifdef _MSC_VER + +#if _MSC_VER >= 1400 // not VC6 +#include // __cpuid +static int stbi__cpuid3(void) +{ + int info[4]; + __cpuid(info,1); + return info[3]; +} +#else +static int stbi__cpuid3(void) +{ + int res; + __asm { + mov eax,1 + cpuid + mov res,edx + } + return res; +} +#endif + +#define STBI_SIMD_ALIGN(type, name) __declspec(align(16)) type name + +#if !defined(STBI_NO_JPEG) && defined(STBI_SSE2) +static int stbi__sse2_available(void) +{ + int info3 = stbi__cpuid3(); + return ((info3 >> 26) & 1) != 0; +} +#endif + +#else // assume GCC-style if not VC++ +#define STBI_SIMD_ALIGN(type, name) type name __attribute__((aligned(16))) + +#if !defined(STBI_NO_JPEG) && defined(STBI_SSE2) +static int stbi__sse2_available(void) +{ + // If we're even attempting to compile this on GCC/Clang, that means + // -msse2 is on, which means the compiler is allowed to use SSE2 + // instructions at will, and so are we. + return 1; +} +#endif + +#endif +#endif + +// ARM NEON +#if defined(STBI_NO_SIMD) && defined(STBI_NEON) +#undef STBI_NEON +#endif + +#ifdef STBI_NEON +#include +#ifdef _MSC_VER +#define STBI_SIMD_ALIGN(type, name) __declspec(align(16)) type name +#else +#define STBI_SIMD_ALIGN(type, name) type name __attribute__((aligned(16))) +#endif +#endif + +#ifndef STBI_SIMD_ALIGN +#define STBI_SIMD_ALIGN(type, name) type name +#endif + +#ifndef STBI_MAX_DIMENSIONS +#define STBI_MAX_DIMENSIONS (1 << 24) +#endif + +/////////////////////////////////////////////// +// +// stbi__context struct and start_xxx functions + +// stbi__context structure is our basic context used by all images, so it +// contains all the IO context, plus some basic image information +typedef struct +{ + stbi__uint32 img_x, img_y; + int img_n, img_out_n; + + stbi_io_callbacks io; + void *io_user_data; + + int read_from_callbacks; + int buflen; + stbi_uc buffer_start[128]; + int callback_already_read; + + stbi_uc *img_buffer, *img_buffer_end; + stbi_uc *img_buffer_original, *img_buffer_original_end; +} stbi__context; + + +static void stbi__refill_buffer(stbi__context *s); + +// initialize a memory-decode context +static void stbi__start_mem(stbi__context *s, stbi_uc const *buffer, int len) +{ + s->io.read = NULL; + s->read_from_callbacks = 0; + s->callback_already_read = 0; + s->img_buffer = s->img_buffer_original = (stbi_uc *) buffer; + s->img_buffer_end = s->img_buffer_original_end = (stbi_uc *) buffer+len; +} + +// initialize a callback-based context +static void stbi__start_callbacks(stbi__context *s, stbi_io_callbacks *c, void *user) +{ + s->io = *c; + s->io_user_data = user; + s->buflen = sizeof(s->buffer_start); + s->read_from_callbacks = 1; + s->callback_already_read = 0; + s->img_buffer = s->img_buffer_original = s->buffer_start; + stbi__refill_buffer(s); + s->img_buffer_original_end = s->img_buffer_end; +} + +#ifndef STBI_NO_STDIO + +static int stbi__stdio_read(void *user, char *data, int size) +{ + return (int) fread(data,1,size,(FILE*) user); +} + +static void stbi__stdio_skip(void *user, int n) +{ + int ch; + fseek((FILE*) user, n, SEEK_CUR); + ch = fgetc((FILE*) user); /* have to read a byte to reset feof()'s flag */ + if (ch != EOF) { + ungetc(ch, (FILE *) user); /* push byte back onto stream if valid. */ + } +} + +static int stbi__stdio_eof(void *user) +{ + return feof((FILE*) user) || ferror((FILE *) user); +} + +static stbi_io_callbacks stbi__stdio_callbacks = +{ + stbi__stdio_read, + stbi__stdio_skip, + stbi__stdio_eof, +}; + +static void stbi__start_file(stbi__context *s, FILE *f) +{ + stbi__start_callbacks(s, &stbi__stdio_callbacks, (void *) f); +} + +//static void stop_file(stbi__context *s) { } + +#endif // !STBI_NO_STDIO + +static void stbi__rewind(stbi__context *s) +{ + // conceptually rewind SHOULD rewind to the beginning of the stream, + // but we just rewind to the beginning of the initial buffer, because + // we only use it after doing 'test', which only ever looks at at most 92 bytes + s->img_buffer = s->img_buffer_original; + s->img_buffer_end = s->img_buffer_original_end; +} + +enum +{ + STBI_ORDER_RGB, + STBI_ORDER_BGR +}; + +typedef struct +{ + int bits_per_channel; + int num_channels; + int channel_order; +} stbi__result_info; + +#ifndef STBI_NO_JPEG +static int stbi__jpeg_test(stbi__context *s); +static void *stbi__jpeg_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri); +static int stbi__jpeg_info(stbi__context *s, int *x, int *y, int *comp); +#endif + +#ifndef STBI_NO_PNG +static int stbi__png_test(stbi__context *s); +static void *stbi__png_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri); +static int stbi__png_info(stbi__context *s, int *x, int *y, int *comp); +static int stbi__png_is16(stbi__context *s); +#endif + +#ifndef STBI_NO_BMP +static int stbi__bmp_test(stbi__context *s); +static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri); +static int stbi__bmp_info(stbi__context *s, int *x, int *y, int *comp); +#endif + +#ifndef STBI_NO_TGA +static int stbi__tga_test(stbi__context *s); +static void *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri); +static int stbi__tga_info(stbi__context *s, int *x, int *y, int *comp); +#endif + +#ifndef STBI_NO_PSD +static int stbi__psd_test(stbi__context *s); +static void *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri, int bpc); +static int stbi__psd_info(stbi__context *s, int *x, int *y, int *comp); +static int stbi__psd_is16(stbi__context *s); +#endif + +#ifndef STBI_NO_HDR +static int stbi__hdr_test(stbi__context *s); +static float *stbi__hdr_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri); +static int stbi__hdr_info(stbi__context *s, int *x, int *y, int *comp); +#endif + +#ifndef STBI_NO_PIC +static int stbi__pic_test(stbi__context *s); +static void *stbi__pic_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri); +static int stbi__pic_info(stbi__context *s, int *x, int *y, int *comp); +#endif + +#ifndef STBI_NO_GIF +static int stbi__gif_test(stbi__context *s); +static void *stbi__gif_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri); +static void *stbi__load_gif_main(stbi__context *s, int **delays, int *x, int *y, int *z, int *comp, int req_comp); +static int stbi__gif_info(stbi__context *s, int *x, int *y, int *comp); +#endif + +#ifndef STBI_NO_PNM +static int stbi__pnm_test(stbi__context *s); +static void *stbi__pnm_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri); +static int stbi__pnm_info(stbi__context *s, int *x, int *y, int *comp); +static int stbi__pnm_is16(stbi__context *s); +#endif + +static +#ifdef STBI_THREAD_LOCAL +STBI_THREAD_LOCAL +#endif +const char *stbi__g_failure_reason; + +STBIDEF const char *stbi_failure_reason(void) +{ + return stbi__g_failure_reason; +} + +#ifndef STBI_NO_FAILURE_STRINGS +static int stbi__err(const char *str) +{ + stbi__g_failure_reason = str; + return 0; +} +#endif + +static void *stbi__malloc(size_t size) +{ + return STBI_MALLOC(size); +} + +// stb_image uses ints pervasively, including for offset calculations. +// therefore the largest decoded image size we can support with the +// current code, even on 64-bit targets, is INT_MAX. this is not a +// significant limitation for the intended use case. +// +// we do, however, need to make sure our size calculations don't +// overflow. hence a few helper functions for size calculations that +// multiply integers together, making sure that they're non-negative +// and no overflow occurs. + +// return 1 if the sum is valid, 0 on overflow. +// negative terms are considered invalid. +static int stbi__addsizes_valid(int a, int b) +{ + if (b < 0) return 0; + // now 0 <= b <= INT_MAX, hence also + // 0 <= INT_MAX - b <= INTMAX. + // And "a + b <= INT_MAX" (which might overflow) is the + // same as a <= INT_MAX - b (no overflow) + return a <= INT_MAX - b; +} + +// returns 1 if the product is valid, 0 on overflow. +// negative factors are considered invalid. +static int stbi__mul2sizes_valid(int a, int b) +{ + if (a < 0 || b < 0) return 0; + if (b == 0) return 1; // mul-by-0 is always safe + // portable way to check for no overflows in a*b + return a <= INT_MAX/b; +} + +#if !defined(STBI_NO_JPEG) || !defined(STBI_NO_PNG) || !defined(STBI_NO_TGA) || !defined(STBI_NO_HDR) +// returns 1 if "a*b + add" has no negative terms/factors and doesn't overflow +static int stbi__mad2sizes_valid(int a, int b, int add) +{ + return stbi__mul2sizes_valid(a, b) && stbi__addsizes_valid(a*b, add); +} +#endif + +// returns 1 if "a*b*c + add" has no negative terms/factors and doesn't overflow +static int stbi__mad3sizes_valid(int a, int b, int c, int add) +{ + return stbi__mul2sizes_valid(a, b) && stbi__mul2sizes_valid(a*b, c) && + stbi__addsizes_valid(a*b*c, add); +} + +// returns 1 if "a*b*c*d + add" has no negative terms/factors and doesn't overflow +#if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR) || !defined(STBI_NO_PNM) +static int stbi__mad4sizes_valid(int a, int b, int c, int d, int add) +{ + return stbi__mul2sizes_valid(a, b) && stbi__mul2sizes_valid(a*b, c) && + stbi__mul2sizes_valid(a*b*c, d) && stbi__addsizes_valid(a*b*c*d, add); +} +#endif + +#if !defined(STBI_NO_JPEG) || !defined(STBI_NO_PNG) || !defined(STBI_NO_TGA) || !defined(STBI_NO_HDR) +// mallocs with size overflow checking +static void *stbi__malloc_mad2(int a, int b, int add) +{ + if (!stbi__mad2sizes_valid(a, b, add)) return NULL; + return stbi__malloc(a*b + add); +} +#endif + +static void *stbi__malloc_mad3(int a, int b, int c, int add) +{ + if (!stbi__mad3sizes_valid(a, b, c, add)) return NULL; + return stbi__malloc(a*b*c + add); +} + +#if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR) || !defined(STBI_NO_PNM) +static void *stbi__malloc_mad4(int a, int b, int c, int d, int add) +{ + if (!stbi__mad4sizes_valid(a, b, c, d, add)) return NULL; + return stbi__malloc(a*b*c*d + add); +} +#endif + +// returns 1 if the sum of two signed ints is valid (between -2^31 and 2^31-1 inclusive), 0 on overflow. +static int stbi__addints_valid(int a, int b) +{ + if ((a >= 0) != (b >= 0)) return 1; // a and b have different signs, so no overflow + if (a < 0 && b < 0) return a >= INT_MIN - b; // same as a + b >= INT_MIN; INT_MIN - b cannot overflow since b < 0. + return a <= INT_MAX - b; +} + +// returns 1 if the product of two ints fits in a signed short, 0 on overflow. +static int stbi__mul2shorts_valid(int a, int b) +{ + if (b == 0 || b == -1) return 1; // multiplication by 0 is always 0; check for -1 so SHRT_MIN/b doesn't overflow + if ((a >= 0) == (b >= 0)) return a <= SHRT_MAX/b; // product is positive, so similar to mul2sizes_valid + if (b < 0) return a <= SHRT_MIN / b; // same as a * b >= SHRT_MIN + return a >= SHRT_MIN / b; +} + +// stbi__err - error +// stbi__errpf - error returning pointer to float +// stbi__errpuc - error returning pointer to unsigned char + +#ifdef STBI_NO_FAILURE_STRINGS + #define stbi__err(x,y) 0 +#elif defined(STBI_FAILURE_USERMSG) + #define stbi__err(x,y) stbi__err(y) +#else + #define stbi__err(x,y) stbi__err(x) +#endif + +#define stbi__errpf(x,y) ((float *)(size_t) (stbi__err(x,y)?NULL:NULL)) +#define stbi__errpuc(x,y) ((unsigned char *)(size_t) (stbi__err(x,y)?NULL:NULL)) + +STBIDEF void stbi_image_free(void *retval_from_stbi_load) +{ + STBI_FREE(retval_from_stbi_load); +} + +#ifndef STBI_NO_LINEAR +static float *stbi__ldr_to_hdr(stbi_uc *data, int x, int y, int comp); +#endif + +#ifndef STBI_NO_HDR +static stbi_uc *stbi__hdr_to_ldr(float *data, int x, int y, int comp); +#endif + +static int stbi__vertically_flip_on_load_global = 0; + +STBIDEF void stbi_set_flip_vertically_on_load(int flag_true_if_should_flip) +{ + stbi__vertically_flip_on_load_global = flag_true_if_should_flip; +} + +#ifndef STBI_THREAD_LOCAL +#define stbi__vertically_flip_on_load stbi__vertically_flip_on_load_global +#else +static STBI_THREAD_LOCAL int stbi__vertically_flip_on_load_local, stbi__vertically_flip_on_load_set; + +STBIDEF void stbi_set_flip_vertically_on_load_thread(int flag_true_if_should_flip) +{ + stbi__vertically_flip_on_load_local = flag_true_if_should_flip; + stbi__vertically_flip_on_load_set = 1; +} + +#define stbi__vertically_flip_on_load (stbi__vertically_flip_on_load_set \ + ? stbi__vertically_flip_on_load_local \ + : stbi__vertically_flip_on_load_global) +#endif // STBI_THREAD_LOCAL + +static void *stbi__load_main(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri, int bpc) +{ + memset(ri, 0, sizeof(*ri)); // make sure it's initialized if we add new fields + ri->bits_per_channel = 8; // default is 8 so most paths don't have to be changed + ri->channel_order = STBI_ORDER_RGB; // all current input & output are this, but this is here so we can add BGR order + ri->num_channels = 0; + + // test the formats with a very explicit header first (at least a FOURCC + // or distinctive magic number first) + #ifndef STBI_NO_PNG + if (stbi__png_test(s)) return stbi__png_load(s,x,y,comp,req_comp, ri); + #endif + #ifndef STBI_NO_BMP + if (stbi__bmp_test(s)) return stbi__bmp_load(s,x,y,comp,req_comp, ri); + #endif + #ifndef STBI_NO_GIF + if (stbi__gif_test(s)) return stbi__gif_load(s,x,y,comp,req_comp, ri); + #endif + #ifndef STBI_NO_PSD + if (stbi__psd_test(s)) return stbi__psd_load(s,x,y,comp,req_comp, ri, bpc); + #else + STBI_NOTUSED(bpc); + #endif + #ifndef STBI_NO_PIC + if (stbi__pic_test(s)) return stbi__pic_load(s,x,y,comp,req_comp, ri); + #endif + + // then the formats that can end up attempting to load with just 1 or 2 + // bytes matching expectations; these are prone to false positives, so + // try them later + #ifndef STBI_NO_JPEG + if (stbi__jpeg_test(s)) return stbi__jpeg_load(s,x,y,comp,req_comp, ri); + #endif + #ifndef STBI_NO_PNM + if (stbi__pnm_test(s)) return stbi__pnm_load(s,x,y,comp,req_comp, ri); + #endif + + #ifndef STBI_NO_HDR + if (stbi__hdr_test(s)) { + float *hdr = stbi__hdr_load(s, x,y,comp,req_comp, ri); + return stbi__hdr_to_ldr(hdr, *x, *y, req_comp ? req_comp : *comp); + } + #endif + + #ifndef STBI_NO_TGA + // test tga last because it's a crappy test! + if (stbi__tga_test(s)) + return stbi__tga_load(s,x,y,comp,req_comp, ri); + #endif + + return stbi__errpuc("unknown image type", "Image not of any known type, or corrupt"); +} + +static stbi_uc *stbi__convert_16_to_8(stbi__uint16 *orig, int w, int h, int channels) +{ + int i; + int img_len = w * h * channels; + stbi_uc *reduced; + + reduced = (stbi_uc *) stbi__malloc(img_len); + if (reduced == NULL) return stbi__errpuc("outofmem", "Out of memory"); + + for (i = 0; i < img_len; ++i) + reduced[i] = (stbi_uc)((orig[i] >> 8) & 0xFF); // top half of each byte is sufficient approx of 16->8 bit scaling + + STBI_FREE(orig); + return reduced; +} + +static stbi__uint16 *stbi__convert_8_to_16(stbi_uc *orig, int w, int h, int channels) +{ + int i; + int img_len = w * h * channels; + stbi__uint16 *enlarged; + + enlarged = (stbi__uint16 *) stbi__malloc(img_len*2); + if (enlarged == NULL) return (stbi__uint16 *) stbi__errpuc("outofmem", "Out of memory"); + + for (i = 0; i < img_len; ++i) + enlarged[i] = (stbi__uint16)((orig[i] << 8) + orig[i]); // replicate to high and low byte, maps 0->0, 255->0xffff + + STBI_FREE(orig); + return enlarged; +} + +static void stbi__vertical_flip(void *image, int w, int h, int bytes_per_pixel) +{ + int row; + size_t bytes_per_row = (size_t)w * bytes_per_pixel; + stbi_uc temp[2048]; + stbi_uc *bytes = (stbi_uc *)image; + + for (row = 0; row < (h>>1); row++) { + stbi_uc *row0 = bytes + row*bytes_per_row; + stbi_uc *row1 = bytes + (h - row - 1)*bytes_per_row; + // swap row0 with row1 + size_t bytes_left = bytes_per_row; + while (bytes_left) { + size_t bytes_copy = (bytes_left < sizeof(temp)) ? bytes_left : sizeof(temp); + memcpy(temp, row0, bytes_copy); + memcpy(row0, row1, bytes_copy); + memcpy(row1, temp, bytes_copy); + row0 += bytes_copy; + row1 += bytes_copy; + bytes_left -= bytes_copy; + } + } +} + +#ifndef STBI_NO_GIF +static void stbi__vertical_flip_slices(void *image, int w, int h, int z, int bytes_per_pixel) +{ + int slice; + int slice_size = w * h * bytes_per_pixel; + + stbi_uc *bytes = (stbi_uc *)image; + for (slice = 0; slice < z; ++slice) { + stbi__vertical_flip(bytes, w, h, bytes_per_pixel); + bytes += slice_size; + } +} +#endif + +static unsigned char *stbi__load_and_postprocess_8bit(stbi__context *s, int *x, int *y, int *comp, int req_comp) +{ + stbi__result_info ri; + void *result = stbi__load_main(s, x, y, comp, req_comp, &ri, 8); + + if (result == NULL) + return NULL; + + // it is the responsibility of the loaders to make sure we get either 8 or 16 bit. + STBI_ASSERT(ri.bits_per_channel == 8 || ri.bits_per_channel == 16); + + if (ri.bits_per_channel != 8) { + result = stbi__convert_16_to_8((stbi__uint16 *) result, *x, *y, req_comp == 0 ? *comp : req_comp); + ri.bits_per_channel = 8; + } + + // @TODO: move stbi__convert_format to here + + if (stbi__vertically_flip_on_load) { + int channels = req_comp ? req_comp : *comp; + stbi__vertical_flip(result, *x, *y, channels * sizeof(stbi_uc)); + } + + return (unsigned char *) result; +} + +static stbi__uint16 *stbi__load_and_postprocess_16bit(stbi__context *s, int *x, int *y, int *comp, int req_comp) +{ + stbi__result_info ri; + void *result = stbi__load_main(s, x, y, comp, req_comp, &ri, 16); + + if (result == NULL) + return NULL; + + // it is the responsibility of the loaders to make sure we get either 8 or 16 bit. + STBI_ASSERT(ri.bits_per_channel == 8 || ri.bits_per_channel == 16); + + if (ri.bits_per_channel != 16) { + result = stbi__convert_8_to_16((stbi_uc *) result, *x, *y, req_comp == 0 ? *comp : req_comp); + ri.bits_per_channel = 16; + } + + // @TODO: move stbi__convert_format16 to here + // @TODO: special case RGB-to-Y (and RGBA-to-YA) for 8-bit-to-16-bit case to keep more precision + + if (stbi__vertically_flip_on_load) { + int channels = req_comp ? req_comp : *comp; + stbi__vertical_flip(result, *x, *y, channels * sizeof(stbi__uint16)); + } + + return (stbi__uint16 *) result; +} + +#if !defined(STBI_NO_HDR) && !defined(STBI_NO_LINEAR) +static void stbi__float_postprocess(float *result, int *x, int *y, int *comp, int req_comp) +{ + if (stbi__vertically_flip_on_load && result != NULL) { + int channels = req_comp ? req_comp : *comp; + stbi__vertical_flip(result, *x, *y, channels * sizeof(float)); + } +} +#endif + +#ifndef STBI_NO_STDIO + +#if defined(_WIN32) && defined(STBI_WINDOWS_UTF8) +STBI_EXTERN __declspec(dllimport) int __stdcall MultiByteToWideChar(unsigned int cp, unsigned long flags, const char *str, int cbmb, wchar_t *widestr, int cchwide); +STBI_EXTERN __declspec(dllimport) int __stdcall WideCharToMultiByte(unsigned int cp, unsigned long flags, const wchar_t *widestr, int cchwide, char *str, int cbmb, const char *defchar, int *used_default); +#endif + +#if defined(_WIN32) && defined(STBI_WINDOWS_UTF8) +STBIDEF int stbi_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input) +{ + return WideCharToMultiByte(65001 /* UTF8 */, 0, input, -1, buffer, (int) bufferlen, NULL, NULL); +} +#endif + +static FILE *stbi__fopen(char const *filename, char const *mode) +{ + FILE *f; +#if defined(_WIN32) && defined(STBI_WINDOWS_UTF8) + wchar_t wMode[64]; + wchar_t wFilename[1024]; + if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, filename, -1, wFilename, sizeof(wFilename)/sizeof(*wFilename))) + return 0; + + if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, mode, -1, wMode, sizeof(wMode)/sizeof(*wMode))) + return 0; + +#if defined(_MSC_VER) && _MSC_VER >= 1400 + if (0 != _wfopen_s(&f, wFilename, wMode)) + f = 0; +#else + f = _wfopen(wFilename, wMode); +#endif + +#elif defined(_MSC_VER) && _MSC_VER >= 1400 + if (0 != fopen_s(&f, filename, mode)) + f=0; +#else + f = fopen(filename, mode); +#endif + return f; +} + + +STBIDEF stbi_uc *stbi_load(char const *filename, int *x, int *y, int *comp, int req_comp) +{ + FILE *f = stbi__fopen(filename, "rb"); + unsigned char *result; + if (!f) return stbi__errpuc("can't fopen", "Unable to open file"); + result = stbi_load_from_file(f,x,y,comp,req_comp); + fclose(f); + return result; +} + +STBIDEF stbi_uc *stbi_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp) +{ + unsigned char *result; + stbi__context s; + stbi__start_file(&s,f); + result = stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp); + if (result) { + // need to 'unget' all the characters in the IO buffer + fseek(f, - (int) (s.img_buffer_end - s.img_buffer), SEEK_CUR); + } + return result; +} + +STBIDEF stbi__uint16 *stbi_load_from_file_16(FILE *f, int *x, int *y, int *comp, int req_comp) +{ + stbi__uint16 *result; + stbi__context s; + stbi__start_file(&s,f); + result = stbi__load_and_postprocess_16bit(&s,x,y,comp,req_comp); + if (result) { + // need to 'unget' all the characters in the IO buffer + fseek(f, - (int) (s.img_buffer_end - s.img_buffer), SEEK_CUR); + } + return result; +} + +STBIDEF stbi_us *stbi_load_16(char const *filename, int *x, int *y, int *comp, int req_comp) +{ + FILE *f = stbi__fopen(filename, "rb"); + stbi__uint16 *result; + if (!f) return (stbi_us *) stbi__errpuc("can't fopen", "Unable to open file"); + result = stbi_load_from_file_16(f,x,y,comp,req_comp); + fclose(f); + return result; +} + + +#endif //!STBI_NO_STDIO + +STBIDEF stbi_us *stbi_load_16_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels) +{ + stbi__context s; + stbi__start_mem(&s,buffer,len); + return stbi__load_and_postprocess_16bit(&s,x,y,channels_in_file,desired_channels); +} + +STBIDEF stbi_us *stbi_load_16_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels) +{ + stbi__context s; + stbi__start_callbacks(&s, (stbi_io_callbacks *)clbk, user); + return stbi__load_and_postprocess_16bit(&s,x,y,channels_in_file,desired_channels); +} + +STBIDEF stbi_uc *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp) +{ + stbi__context s; + stbi__start_mem(&s,buffer,len); + return stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp); +} + +STBIDEF stbi_uc *stbi_load_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp) +{ + stbi__context s; + stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user); + return stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp); +} + +#ifndef STBI_NO_GIF +STBIDEF stbi_uc *stbi_load_gif_from_memory(stbi_uc const *buffer, int len, int **delays, int *x, int *y, int *z, int *comp, int req_comp) +{ + unsigned char *result; + stbi__context s; + stbi__start_mem(&s,buffer,len); + + result = (unsigned char*) stbi__load_gif_main(&s, delays, x, y, z, comp, req_comp); + if (stbi__vertically_flip_on_load) { + stbi__vertical_flip_slices( result, *x, *y, *z, *comp ); + } + + return result; +} +#endif + +#ifndef STBI_NO_LINEAR +static float *stbi__loadf_main(stbi__context *s, int *x, int *y, int *comp, int req_comp) +{ + unsigned char *data; + #ifndef STBI_NO_HDR + if (stbi__hdr_test(s)) { + stbi__result_info ri; + float *hdr_data = stbi__hdr_load(s,x,y,comp,req_comp, &ri); + if (hdr_data) + stbi__float_postprocess(hdr_data,x,y,comp,req_comp); + return hdr_data; + } + #endif + data = stbi__load_and_postprocess_8bit(s, x, y, comp, req_comp); + if (data) + return stbi__ldr_to_hdr(data, *x, *y, req_comp ? req_comp : *comp); + return stbi__errpf("unknown image type", "Image not of any known type, or corrupt"); +} + +STBIDEF float *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp) +{ + stbi__context s; + stbi__start_mem(&s,buffer,len); + return stbi__loadf_main(&s,x,y,comp,req_comp); +} + +STBIDEF float *stbi_loadf_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp) +{ + stbi__context s; + stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user); + return stbi__loadf_main(&s,x,y,comp,req_comp); +} + +#ifndef STBI_NO_STDIO +STBIDEF float *stbi_loadf(char const *filename, int *x, int *y, int *comp, int req_comp) +{ + float *result; + FILE *f = stbi__fopen(filename, "rb"); + if (!f) return stbi__errpf("can't fopen", "Unable to open file"); + result = stbi_loadf_from_file(f,x,y,comp,req_comp); + fclose(f); + return result; +} + +STBIDEF float *stbi_loadf_from_file(FILE *f, int *x, int *y, int *comp, int req_comp) +{ + stbi__context s; + stbi__start_file(&s,f); + return stbi__loadf_main(&s,x,y,comp,req_comp); +} +#endif // !STBI_NO_STDIO + +#endif // !STBI_NO_LINEAR + +// these is-hdr-or-not is defined independent of whether STBI_NO_LINEAR is +// defined, for API simplicity; if STBI_NO_LINEAR is defined, it always +// reports false! + +STBIDEF int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len) +{ + #ifndef STBI_NO_HDR + stbi__context s; + stbi__start_mem(&s,buffer,len); + return stbi__hdr_test(&s); + #else + STBI_NOTUSED(buffer); + STBI_NOTUSED(len); + return 0; + #endif +} + +#ifndef STBI_NO_STDIO +STBIDEF int stbi_is_hdr (char const *filename) +{ + FILE *f = stbi__fopen(filename, "rb"); + int result=0; + if (f) { + result = stbi_is_hdr_from_file(f); + fclose(f); + } + return result; +} + +STBIDEF int stbi_is_hdr_from_file(FILE *f) +{ + #ifndef STBI_NO_HDR + long pos = ftell(f); + int res; + stbi__context s; + stbi__start_file(&s,f); + res = stbi__hdr_test(&s); + fseek(f, pos, SEEK_SET); + return res; + #else + STBI_NOTUSED(f); + return 0; + #endif +} +#endif // !STBI_NO_STDIO + +STBIDEF int stbi_is_hdr_from_callbacks(stbi_io_callbacks const *clbk, void *user) +{ + #ifndef STBI_NO_HDR + stbi__context s; + stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user); + return stbi__hdr_test(&s); + #else + STBI_NOTUSED(clbk); + STBI_NOTUSED(user); + return 0; + #endif +} + +#ifndef STBI_NO_LINEAR +static float stbi__l2h_gamma=2.2f, stbi__l2h_scale=1.0f; + +STBIDEF void stbi_ldr_to_hdr_gamma(float gamma) { stbi__l2h_gamma = gamma; } +STBIDEF void stbi_ldr_to_hdr_scale(float scale) { stbi__l2h_scale = scale; } +#endif + +static float stbi__h2l_gamma_i=1.0f/2.2f, stbi__h2l_scale_i=1.0f; + +STBIDEF void stbi_hdr_to_ldr_gamma(float gamma) { stbi__h2l_gamma_i = 1/gamma; } +STBIDEF void stbi_hdr_to_ldr_scale(float scale) { stbi__h2l_scale_i = 1/scale; } + + +////////////////////////////////////////////////////////////////////////////// +// +// Common code used by all image loaders +// + +enum +{ + STBI__SCAN_load=0, + STBI__SCAN_type, + STBI__SCAN_header +}; + +static void stbi__refill_buffer(stbi__context *s) +{ + int n = (s->io.read)(s->io_user_data,(char*)s->buffer_start,s->buflen); + s->callback_already_read += (int) (s->img_buffer - s->img_buffer_original); + if (n == 0) { + // at end of file, treat same as if from memory, but need to handle case + // where s->img_buffer isn't pointing to safe memory, e.g. 0-byte file + s->read_from_callbacks = 0; + s->img_buffer = s->buffer_start; + s->img_buffer_end = s->buffer_start+1; + *s->img_buffer = 0; + } else { + s->img_buffer = s->buffer_start; + s->img_buffer_end = s->buffer_start + n; + } +} + +stbi_inline static stbi_uc stbi__get8(stbi__context *s) +{ + if (s->img_buffer < s->img_buffer_end) + return *s->img_buffer++; + if (s->read_from_callbacks) { + stbi__refill_buffer(s); + return *s->img_buffer++; + } + return 0; +} + +#if defined(STBI_NO_JPEG) && defined(STBI_NO_HDR) && defined(STBI_NO_PIC) && defined(STBI_NO_PNM) +// nothing +#else +stbi_inline static int stbi__at_eof(stbi__context *s) +{ + if (s->io.read) { + if (!(s->io.eof)(s->io_user_data)) return 0; + // if feof() is true, check if buffer = end + // special case: we've only got the special 0 character at the end + if (s->read_from_callbacks == 0) return 1; + } + + return s->img_buffer >= s->img_buffer_end; +} +#endif + +#if defined(STBI_NO_JPEG) && defined(STBI_NO_PNG) && defined(STBI_NO_BMP) && defined(STBI_NO_PSD) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF) && defined(STBI_NO_PIC) +// nothing +#else +static void stbi__skip(stbi__context *s, int n) +{ + if (n == 0) return; // already there! + if (n < 0) { + s->img_buffer = s->img_buffer_end; + return; + } + if (s->io.read) { + int blen = (int) (s->img_buffer_end - s->img_buffer); + if (blen < n) { + s->img_buffer = s->img_buffer_end; + (s->io.skip)(s->io_user_data, n - blen); + return; + } + } + s->img_buffer += n; +} +#endif + +#if defined(STBI_NO_PNG) && defined(STBI_NO_TGA) && defined(STBI_NO_HDR) && defined(STBI_NO_PNM) +// nothing +#else +static int stbi__getn(stbi__context *s, stbi_uc *buffer, int n) +{ + if (s->io.read) { + int blen = (int) (s->img_buffer_end - s->img_buffer); + if (blen < n) { + int res, count; + + memcpy(buffer, s->img_buffer, blen); + + count = (s->io.read)(s->io_user_data, (char*) buffer + blen, n - blen); + res = (count == (n-blen)); + s->img_buffer = s->img_buffer_end; + return res; + } + } + + if (s->img_buffer+n <= s->img_buffer_end) { + memcpy(buffer, s->img_buffer, n); + s->img_buffer += n; + return 1; + } else + return 0; +} +#endif + +#if defined(STBI_NO_JPEG) && defined(STBI_NO_PNG) && defined(STBI_NO_PSD) && defined(STBI_NO_PIC) +// nothing +#else +static int stbi__get16be(stbi__context *s) +{ + int z = stbi__get8(s); + return (z << 8) + stbi__get8(s); +} +#endif + +#if defined(STBI_NO_PNG) && defined(STBI_NO_PSD) && defined(STBI_NO_PIC) +// nothing +#else +static stbi__uint32 stbi__get32be(stbi__context *s) +{ + stbi__uint32 z = stbi__get16be(s); + return (z << 16) + stbi__get16be(s); +} +#endif + +#if defined(STBI_NO_BMP) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF) +// nothing +#else +static int stbi__get16le(stbi__context *s) +{ + int z = stbi__get8(s); + return z + (stbi__get8(s) << 8); +} +#endif + +#ifndef STBI_NO_BMP +static stbi__uint32 stbi__get32le(stbi__context *s) +{ + stbi__uint32 z = stbi__get16le(s); + z += (stbi__uint32)stbi__get16le(s) << 16; + return z; +} +#endif + +#define STBI__BYTECAST(x) ((stbi_uc) ((x) & 255)) // truncate int to byte without warnings + +#if defined(STBI_NO_JPEG) && defined(STBI_NO_PNG) && defined(STBI_NO_BMP) && defined(STBI_NO_PSD) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF) && defined(STBI_NO_PIC) && defined(STBI_NO_PNM) +// nothing +#else +////////////////////////////////////////////////////////////////////////////// +// +// generic converter from built-in img_n to req_comp +// individual types do this automatically as much as possible (e.g. jpeg +// does all cases internally since it needs to colorspace convert anyway, +// and it never has alpha, so very few cases ). png can automatically +// interleave an alpha=255 channel, but falls back to this for other cases +// +// assume data buffer is malloced, so malloc a new one and free that one +// only failure mode is malloc failing + +static stbi_uc stbi__compute_y(int r, int g, int b) +{ + return (stbi_uc) (((r*77) + (g*150) + (29*b)) >> 8); +} +#endif + +#if defined(STBI_NO_PNG) && defined(STBI_NO_BMP) && defined(STBI_NO_PSD) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF) && defined(STBI_NO_PIC) && defined(STBI_NO_PNM) +// nothing +#else +static unsigned char *stbi__convert_format(unsigned char *data, int img_n, int req_comp, unsigned int x, unsigned int y) +{ + int i,j; + unsigned char *good; + + if (req_comp == img_n) return data; + STBI_ASSERT(req_comp >= 1 && req_comp <= 4); + + good = (unsigned char *) stbi__malloc_mad3(req_comp, x, y, 0); + if (good == NULL) { + STBI_FREE(data); + return stbi__errpuc("outofmem", "Out of memory"); + } + + for (j=0; j < (int) y; ++j) { + unsigned char *src = data + j * x * img_n ; + unsigned char *dest = good + j * x * req_comp; + + #define STBI__COMBO(a,b) ((a)*8+(b)) + #define STBI__CASE(a,b) case STBI__COMBO(a,b): for(i=x-1; i >= 0; --i, src += a, dest += b) + // convert source image with img_n components to one with req_comp components; + // avoid switch per pixel, so use switch per scanline and massive macros + switch (STBI__COMBO(img_n, req_comp)) { + STBI__CASE(1,2) { dest[0]=src[0]; dest[1]=255; } break; + STBI__CASE(1,3) { dest[0]=dest[1]=dest[2]=src[0]; } break; + STBI__CASE(1,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=255; } break; + STBI__CASE(2,1) { dest[0]=src[0]; } break; + STBI__CASE(2,3) { dest[0]=dest[1]=dest[2]=src[0]; } break; + STBI__CASE(2,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=src[1]; } break; + STBI__CASE(3,4) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2];dest[3]=255; } break; + STBI__CASE(3,1) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); } break; + STBI__CASE(3,2) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); dest[1] = 255; } break; + STBI__CASE(4,1) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); } break; + STBI__CASE(4,2) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); dest[1] = src[3]; } break; + STBI__CASE(4,3) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2]; } break; + default: STBI_ASSERT(0); STBI_FREE(data); STBI_FREE(good); return stbi__errpuc("unsupported", "Unsupported format conversion"); + } + #undef STBI__CASE + } + + STBI_FREE(data); + return good; +} +#endif + +#if defined(STBI_NO_PNG) && defined(STBI_NO_PSD) +// nothing +#else +static stbi__uint16 stbi__compute_y_16(int r, int g, int b) +{ + return (stbi__uint16) (((r*77) + (g*150) + (29*b)) >> 8); +} +#endif + +#if defined(STBI_NO_PNG) && defined(STBI_NO_PSD) +// nothing +#else +static stbi__uint16 *stbi__convert_format16(stbi__uint16 *data, int img_n, int req_comp, unsigned int x, unsigned int y) +{ + int i,j; + stbi__uint16 *good; + + if (req_comp == img_n) return data; + STBI_ASSERT(req_comp >= 1 && req_comp <= 4); + + good = (stbi__uint16 *) stbi__malloc(req_comp * x * y * 2); + if (good == NULL) { + STBI_FREE(data); + return (stbi__uint16 *) stbi__errpuc("outofmem", "Out of memory"); + } + + for (j=0; j < (int) y; ++j) { + stbi__uint16 *src = data + j * x * img_n ; + stbi__uint16 *dest = good + j * x * req_comp; + + #define STBI__COMBO(a,b) ((a)*8+(b)) + #define STBI__CASE(a,b) case STBI__COMBO(a,b): for(i=x-1; i >= 0; --i, src += a, dest += b) + // convert source image with img_n components to one with req_comp components; + // avoid switch per pixel, so use switch per scanline and massive macros + switch (STBI__COMBO(img_n, req_comp)) { + STBI__CASE(1,2) { dest[0]=src[0]; dest[1]=0xffff; } break; + STBI__CASE(1,3) { dest[0]=dest[1]=dest[2]=src[0]; } break; + STBI__CASE(1,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=0xffff; } break; + STBI__CASE(2,1) { dest[0]=src[0]; } break; + STBI__CASE(2,3) { dest[0]=dest[1]=dest[2]=src[0]; } break; + STBI__CASE(2,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=src[1]; } break; + STBI__CASE(3,4) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2];dest[3]=0xffff; } break; + STBI__CASE(3,1) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); } break; + STBI__CASE(3,2) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); dest[1] = 0xffff; } break; + STBI__CASE(4,1) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); } break; + STBI__CASE(4,2) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); dest[1] = src[3]; } break; + STBI__CASE(4,3) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2]; } break; + default: STBI_ASSERT(0); STBI_FREE(data); STBI_FREE(good); return (stbi__uint16*) stbi__errpuc("unsupported", "Unsupported format conversion"); + } + #undef STBI__CASE + } + + STBI_FREE(data); + return good; +} +#endif + +#ifndef STBI_NO_LINEAR +static float *stbi__ldr_to_hdr(stbi_uc *data, int x, int y, int comp) +{ + int i,k,n; + float *output; + if (!data) return NULL; + output = (float *) stbi__malloc_mad4(x, y, comp, sizeof(float), 0); + if (output == NULL) { STBI_FREE(data); return stbi__errpf("outofmem", "Out of memory"); } + // compute number of non-alpha components + if (comp & 1) n = comp; else n = comp-1; + for (i=0; i < x*y; ++i) { + for (k=0; k < n; ++k) { + output[i*comp + k] = (float) (pow(data[i*comp+k]/255.0f, stbi__l2h_gamma) * stbi__l2h_scale); + } + } + if (n < comp) { + for (i=0; i < x*y; ++i) { + output[i*comp + n] = data[i*comp + n]/255.0f; + } + } + STBI_FREE(data); + return output; +} +#endif + +#ifndef STBI_NO_HDR +#define stbi__float2int(x) ((int) (x)) +static stbi_uc *stbi__hdr_to_ldr(float *data, int x, int y, int comp) +{ + int i,k,n; + stbi_uc *output; + if (!data) return NULL; + output = (stbi_uc *) stbi__malloc_mad3(x, y, comp, 0); + if (output == NULL) { STBI_FREE(data); return stbi__errpuc("outofmem", "Out of memory"); } + // compute number of non-alpha components + if (comp & 1) n = comp; else n = comp-1; + for (i=0; i < x*y; ++i) { + for (k=0; k < n; ++k) { + float z = (float) pow(data[i*comp+k]*stbi__h2l_scale_i, stbi__h2l_gamma_i) * 255 + 0.5f; + if (z < 0) z = 0; + if (z > 255) z = 255; + output[i*comp + k] = (stbi_uc) stbi__float2int(z); + } + if (k < comp) { + float z = data[i*comp+k] * 255 + 0.5f; + if (z < 0) z = 0; + if (z > 255) z = 255; + output[i*comp + k] = (stbi_uc) stbi__float2int(z); + } + } + STBI_FREE(data); + return output; +} +#endif + +////////////////////////////////////////////////////////////////////////////// +// +// "baseline" JPEG/JFIF decoder +// +// simple implementation +// - doesn't support delayed output of y-dimension +// - simple interface (only one output format: 8-bit interleaved RGB) +// - doesn't try to recover corrupt jpegs +// - doesn't allow partial loading, loading multiple at once +// - still fast on x86 (copying globals into locals doesn't help x86) +// - allocates lots of intermediate memory (full size of all components) +// - non-interleaved case requires this anyway +// - allows good upsampling (see next) +// high-quality +// - upsampled channels are bilinearly interpolated, even across blocks +// - quality integer IDCT derived from IJG's 'slow' +// performance +// - fast huffman; reasonable integer IDCT +// - some SIMD kernels for common paths on targets with SSE2/NEON +// - uses a lot of intermediate memory, could cache poorly + +#ifndef STBI_NO_JPEG + +// huffman decoding acceleration +#define FAST_BITS 9 // larger handles more cases; smaller stomps less cache + +typedef struct +{ + stbi_uc fast[1 << FAST_BITS]; + // weirdly, repacking this into AoS is a 10% speed loss, instead of a win + stbi__uint16 code[256]; + stbi_uc values[256]; + stbi_uc size[257]; + unsigned int maxcode[18]; + int delta[17]; // old 'firstsymbol' - old 'firstcode' +} stbi__huffman; + +typedef struct +{ + stbi__context *s; + stbi__huffman huff_dc[4]; + stbi__huffman huff_ac[4]; + stbi__uint16 dequant[4][64]; + stbi__int16 fast_ac[4][1 << FAST_BITS]; + +// sizes for components, interleaved MCUs + int img_h_max, img_v_max; + int img_mcu_x, img_mcu_y; + int img_mcu_w, img_mcu_h; + +// definition of jpeg image component + struct + { + int id; + int h,v; + int tq; + int hd,ha; + int dc_pred; + + int x,y,w2,h2; + stbi_uc *data; + void *raw_data, *raw_coeff; + stbi_uc *linebuf; + short *coeff; // progressive only + int coeff_w, coeff_h; // number of 8x8 coefficient blocks + } img_comp[4]; + + stbi__uint32 code_buffer; // jpeg entropy-coded buffer + int code_bits; // number of valid bits + unsigned char marker; // marker seen while filling entropy buffer + int nomore; // flag if we saw a marker so must stop + + int progressive; + int spec_start; + int spec_end; + int succ_high; + int succ_low; + int eob_run; + int jfif; + int app14_color_transform; // Adobe APP14 tag + int rgb; + + int scan_n, order[4]; + int restart_interval, todo; + +// kernels + void (*idct_block_kernel)(stbi_uc *out, int out_stride, short data[64]); + void (*YCbCr_to_RGB_kernel)(stbi_uc *out, const stbi_uc *y, const stbi_uc *pcb, const stbi_uc *pcr, int count, int step); + stbi_uc *(*resample_row_hv_2_kernel)(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs); +} stbi__jpeg; + +static int stbi__build_huffman(stbi__huffman *h, int *count) +{ + int i,j,k=0; + unsigned int code; + // build size list for each symbol (from JPEG spec) + for (i=0; i < 16; ++i) { + for (j=0; j < count[i]; ++j) { + h->size[k++] = (stbi_uc) (i+1); + if(k >= 257) return stbi__err("bad size list","Corrupt JPEG"); + } + } + h->size[k] = 0; + + // compute actual symbols (from jpeg spec) + code = 0; + k = 0; + for(j=1; j <= 16; ++j) { + // compute delta to add to code to compute symbol id + h->delta[j] = k - code; + if (h->size[k] == j) { + while (h->size[k] == j) + h->code[k++] = (stbi__uint16) (code++); + if (code-1 >= (1u << j)) return stbi__err("bad code lengths","Corrupt JPEG"); + } + // compute largest code + 1 for this size, preshifted as needed later + h->maxcode[j] = code << (16-j); + code <<= 1; + } + h->maxcode[j] = 0xffffffff; + + // build non-spec acceleration table; 255 is flag for not-accelerated + memset(h->fast, 255, 1 << FAST_BITS); + for (i=0; i < k; ++i) { + int s = h->size[i]; + if (s <= FAST_BITS) { + int c = h->code[i] << (FAST_BITS-s); + int m = 1 << (FAST_BITS-s); + for (j=0; j < m; ++j) { + h->fast[c+j] = (stbi_uc) i; + } + } + } + return 1; +} + +// build a table that decodes both magnitude and value of small ACs in +// one go. +static void stbi__build_fast_ac(stbi__int16 *fast_ac, stbi__huffman *h) +{ + int i; + for (i=0; i < (1 << FAST_BITS); ++i) { + stbi_uc fast = h->fast[i]; + fast_ac[i] = 0; + if (fast < 255) { + int rs = h->values[fast]; + int run = (rs >> 4) & 15; + int magbits = rs & 15; + int len = h->size[fast]; + + if (magbits && len + magbits <= FAST_BITS) { + // magnitude code followed by receive_extend code + int k = ((i << len) & ((1 << FAST_BITS) - 1)) >> (FAST_BITS - magbits); + int m = 1 << (magbits - 1); + if (k < m) k += (~0U << magbits) + 1; + // if the result is small enough, we can fit it in fast_ac table + if (k >= -128 && k <= 127) + fast_ac[i] = (stbi__int16) ((k * 256) + (run * 16) + (len + magbits)); + } + } + } +} + +static void stbi__grow_buffer_unsafe(stbi__jpeg *j) +{ + do { + unsigned int b = j->nomore ? 0 : stbi__get8(j->s); + if (b == 0xff) { + int c = stbi__get8(j->s); + while (c == 0xff) c = stbi__get8(j->s); // consume fill bytes + if (c != 0) { + j->marker = (unsigned char) c; + j->nomore = 1; + return; + } + } + j->code_buffer |= b << (24 - j->code_bits); + j->code_bits += 8; + } while (j->code_bits <= 24); +} + +// (1 << n) - 1 +static const stbi__uint32 stbi__bmask[17]={0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535}; + +// decode a jpeg huffman value from the bitstream +stbi_inline static int stbi__jpeg_huff_decode(stbi__jpeg *j, stbi__huffman *h) +{ + unsigned int temp; + int c,k; + + if (j->code_bits < 16) stbi__grow_buffer_unsafe(j); + + // look at the top FAST_BITS and determine what symbol ID it is, + // if the code is <= FAST_BITS + c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS)-1); + k = h->fast[c]; + if (k < 255) { + int s = h->size[k]; + if (s > j->code_bits) + return -1; + j->code_buffer <<= s; + j->code_bits -= s; + return h->values[k]; + } + + // naive test is to shift the code_buffer down so k bits are + // valid, then test against maxcode. To speed this up, we've + // preshifted maxcode left so that it has (16-k) 0s at the + // end; in other words, regardless of the number of bits, it + // wants to be compared against something shifted to have 16; + // that way we don't need to shift inside the loop. + temp = j->code_buffer >> 16; + for (k=FAST_BITS+1 ; ; ++k) + if (temp < h->maxcode[k]) + break; + if (k == 17) { + // error! code not found + j->code_bits -= 16; + return -1; + } + + if (k > j->code_bits) + return -1; + + // convert the huffman code to the symbol id + c = ((j->code_buffer >> (32 - k)) & stbi__bmask[k]) + h->delta[k]; + if(c < 0 || c >= 256) // symbol id out of bounds! + return -1; + STBI_ASSERT((((j->code_buffer) >> (32 - h->size[c])) & stbi__bmask[h->size[c]]) == h->code[c]); + + // convert the id to a symbol + j->code_bits -= k; + j->code_buffer <<= k; + return h->values[c]; +} + +// bias[n] = (-1<code_bits < n) stbi__grow_buffer_unsafe(j); + if (j->code_bits < n) return 0; // ran out of bits from stream, return 0s intead of continuing + + sgn = j->code_buffer >> 31; // sign bit always in MSB; 0 if MSB clear (positive), 1 if MSB set (negative) + k = stbi_lrot(j->code_buffer, n); + j->code_buffer = k & ~stbi__bmask[n]; + k &= stbi__bmask[n]; + j->code_bits -= n; + return k + (stbi__jbias[n] & (sgn - 1)); +} + +// get some unsigned bits +stbi_inline static int stbi__jpeg_get_bits(stbi__jpeg *j, int n) +{ + unsigned int k; + if (j->code_bits < n) stbi__grow_buffer_unsafe(j); + if (j->code_bits < n) return 0; // ran out of bits from stream, return 0s intead of continuing + k = stbi_lrot(j->code_buffer, n); + j->code_buffer = k & ~stbi__bmask[n]; + k &= stbi__bmask[n]; + j->code_bits -= n; + return k; +} + +stbi_inline static int stbi__jpeg_get_bit(stbi__jpeg *j) +{ + unsigned int k; + if (j->code_bits < 1) stbi__grow_buffer_unsafe(j); + if (j->code_bits < 1) return 0; // ran out of bits from stream, return 0s intead of continuing + k = j->code_buffer; + j->code_buffer <<= 1; + --j->code_bits; + return k & 0x80000000; +} + +// given a value that's at position X in the zigzag stream, +// where does it appear in the 8x8 matrix coded as row-major? +static const stbi_uc stbi__jpeg_dezigzag[64+15] = +{ + 0, 1, 8, 16, 9, 2, 3, 10, + 17, 24, 32, 25, 18, 11, 4, 5, + 12, 19, 26, 33, 40, 48, 41, 34, + 27, 20, 13, 6, 7, 14, 21, 28, + 35, 42, 49, 56, 57, 50, 43, 36, + 29, 22, 15, 23, 30, 37, 44, 51, + 58, 59, 52, 45, 38, 31, 39, 46, + 53, 60, 61, 54, 47, 55, 62, 63, + // let corrupt input sample past end + 63, 63, 63, 63, 63, 63, 63, 63, + 63, 63, 63, 63, 63, 63, 63 +}; + +// decode one 64-entry block-- +static int stbi__jpeg_decode_block(stbi__jpeg *j, short data[64], stbi__huffman *hdc, stbi__huffman *hac, stbi__int16 *fac, int b, stbi__uint16 *dequant) +{ + int diff,dc,k; + int t; + + if (j->code_bits < 16) stbi__grow_buffer_unsafe(j); + t = stbi__jpeg_huff_decode(j, hdc); + if (t < 0 || t > 15) return stbi__err("bad huffman code","Corrupt JPEG"); + + // 0 all the ac values now so we can do it 32-bits at a time + memset(data,0,64*sizeof(data[0])); + + diff = t ? stbi__extend_receive(j, t) : 0; + if (!stbi__addints_valid(j->img_comp[b].dc_pred, diff)) return stbi__err("bad delta","Corrupt JPEG"); + dc = j->img_comp[b].dc_pred + diff; + j->img_comp[b].dc_pred = dc; + if (!stbi__mul2shorts_valid(dc, dequant[0])) return stbi__err("can't merge dc and ac", "Corrupt JPEG"); + data[0] = (short) (dc * dequant[0]); + + // decode AC components, see JPEG spec + k = 1; + do { + unsigned int zig; + int c,r,s; + if (j->code_bits < 16) stbi__grow_buffer_unsafe(j); + c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS)-1); + r = fac[c]; + if (r) { // fast-AC path + k += (r >> 4) & 15; // run + s = r & 15; // combined length + if (s > j->code_bits) return stbi__err("bad huffman code", "Combined length longer than code bits available"); + j->code_buffer <<= s; + j->code_bits -= s; + // decode into unzigzag'd location + zig = stbi__jpeg_dezigzag[k++]; + data[zig] = (short) ((r >> 8) * dequant[zig]); + } else { + int rs = stbi__jpeg_huff_decode(j, hac); + if (rs < 0) return stbi__err("bad huffman code","Corrupt JPEG"); + s = rs & 15; + r = rs >> 4; + if (s == 0) { + if (rs != 0xf0) break; // end block + k += 16; + } else { + k += r; + // decode into unzigzag'd location + zig = stbi__jpeg_dezigzag[k++]; + data[zig] = (short) (stbi__extend_receive(j,s) * dequant[zig]); + } + } + } while (k < 64); + return 1; +} + +static int stbi__jpeg_decode_block_prog_dc(stbi__jpeg *j, short data[64], stbi__huffman *hdc, int b) +{ + int diff,dc; + int t; + if (j->spec_end != 0) return stbi__err("can't merge dc and ac", "Corrupt JPEG"); + + if (j->code_bits < 16) stbi__grow_buffer_unsafe(j); + + if (j->succ_high == 0) { + // first scan for DC coefficient, must be first + memset(data,0,64*sizeof(data[0])); // 0 all the ac values now + t = stbi__jpeg_huff_decode(j, hdc); + if (t < 0 || t > 15) return stbi__err("can't merge dc and ac", "Corrupt JPEG"); + diff = t ? stbi__extend_receive(j, t) : 0; + + if (!stbi__addints_valid(j->img_comp[b].dc_pred, diff)) return stbi__err("bad delta", "Corrupt JPEG"); + dc = j->img_comp[b].dc_pred + diff; + j->img_comp[b].dc_pred = dc; + if (!stbi__mul2shorts_valid(dc, 1 << j->succ_low)) return stbi__err("can't merge dc and ac", "Corrupt JPEG"); + data[0] = (short) (dc * (1 << j->succ_low)); + } else { + // refinement scan for DC coefficient + if (stbi__jpeg_get_bit(j)) + data[0] += (short) (1 << j->succ_low); + } + return 1; +} + +// @OPTIMIZE: store non-zigzagged during the decode passes, +// and only de-zigzag when dequantizing +static int stbi__jpeg_decode_block_prog_ac(stbi__jpeg *j, short data[64], stbi__huffman *hac, stbi__int16 *fac) +{ + int k; + if (j->spec_start == 0) return stbi__err("can't merge dc and ac", "Corrupt JPEG"); + + if (j->succ_high == 0) { + int shift = j->succ_low; + + if (j->eob_run) { + --j->eob_run; + return 1; + } + + k = j->spec_start; + do { + unsigned int zig; + int c,r,s; + if (j->code_bits < 16) stbi__grow_buffer_unsafe(j); + c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS)-1); + r = fac[c]; + if (r) { // fast-AC path + k += (r >> 4) & 15; // run + s = r & 15; // combined length + if (s > j->code_bits) return stbi__err("bad huffman code", "Combined length longer than code bits available"); + j->code_buffer <<= s; + j->code_bits -= s; + zig = stbi__jpeg_dezigzag[k++]; + data[zig] = (short) ((r >> 8) * (1 << shift)); + } else { + int rs = stbi__jpeg_huff_decode(j, hac); + if (rs < 0) return stbi__err("bad huffman code","Corrupt JPEG"); + s = rs & 15; + r = rs >> 4; + if (s == 0) { + if (r < 15) { + j->eob_run = (1 << r); + if (r) + j->eob_run += stbi__jpeg_get_bits(j, r); + --j->eob_run; + break; + } + k += 16; + } else { + k += r; + zig = stbi__jpeg_dezigzag[k++]; + data[zig] = (short) (stbi__extend_receive(j,s) * (1 << shift)); + } + } + } while (k <= j->spec_end); + } else { + // refinement scan for these AC coefficients + + short bit = (short) (1 << j->succ_low); + + if (j->eob_run) { + --j->eob_run; + for (k = j->spec_start; k <= j->spec_end; ++k) { + short *p = &data[stbi__jpeg_dezigzag[k]]; + if (*p != 0) + if (stbi__jpeg_get_bit(j)) + if ((*p & bit)==0) { + if (*p > 0) + *p += bit; + else + *p -= bit; + } + } + } else { + k = j->spec_start; + do { + int r,s; + int rs = stbi__jpeg_huff_decode(j, hac); // @OPTIMIZE see if we can use the fast path here, advance-by-r is so slow, eh + if (rs < 0) return stbi__err("bad huffman code","Corrupt JPEG"); + s = rs & 15; + r = rs >> 4; + if (s == 0) { + if (r < 15) { + j->eob_run = (1 << r) - 1; + if (r) + j->eob_run += stbi__jpeg_get_bits(j, r); + r = 64; // force end of block + } else { + // r=15 s=0 should write 16 0s, so we just do + // a run of 15 0s and then write s (which is 0), + // so we don't have to do anything special here + } + } else { + if (s != 1) return stbi__err("bad huffman code", "Corrupt JPEG"); + // sign bit + if (stbi__jpeg_get_bit(j)) + s = bit; + else + s = -bit; + } + + // advance by r + while (k <= j->spec_end) { + short *p = &data[stbi__jpeg_dezigzag[k++]]; + if (*p != 0) { + if (stbi__jpeg_get_bit(j)) + if ((*p & bit)==0) { + if (*p > 0) + *p += bit; + else + *p -= bit; + } + } else { + if (r == 0) { + *p = (short) s; + break; + } + --r; + } + } + } while (k <= j->spec_end); + } + } + return 1; +} + +// take a -128..127 value and stbi__clamp it and convert to 0..255 +stbi_inline static stbi_uc stbi__clamp(int x) +{ + // trick to use a single test to catch both cases + if ((unsigned int) x > 255) { + if (x < 0) return 0; + if (x > 255) return 255; + } + return (stbi_uc) x; +} + +#define stbi__f2f(x) ((int) (((x) * 4096 + 0.5))) +#define stbi__fsh(x) ((x) * 4096) + +// derived from jidctint -- DCT_ISLOW +#define STBI__IDCT_1D(s0,s1,s2,s3,s4,s5,s6,s7) \ + int t0,t1,t2,t3,p1,p2,p3,p4,p5,x0,x1,x2,x3; \ + p2 = s2; \ + p3 = s6; \ + p1 = (p2+p3) * stbi__f2f(0.5411961f); \ + t2 = p1 + p3*stbi__f2f(-1.847759065f); \ + t3 = p1 + p2*stbi__f2f( 0.765366865f); \ + p2 = s0; \ + p3 = s4; \ + t0 = stbi__fsh(p2+p3); \ + t1 = stbi__fsh(p2-p3); \ + x0 = t0+t3; \ + x3 = t0-t3; \ + x1 = t1+t2; \ + x2 = t1-t2; \ + t0 = s7; \ + t1 = s5; \ + t2 = s3; \ + t3 = s1; \ + p3 = t0+t2; \ + p4 = t1+t3; \ + p1 = t0+t3; \ + p2 = t1+t2; \ + p5 = (p3+p4)*stbi__f2f( 1.175875602f); \ + t0 = t0*stbi__f2f( 0.298631336f); \ + t1 = t1*stbi__f2f( 2.053119869f); \ + t2 = t2*stbi__f2f( 3.072711026f); \ + t3 = t3*stbi__f2f( 1.501321110f); \ + p1 = p5 + p1*stbi__f2f(-0.899976223f); \ + p2 = p5 + p2*stbi__f2f(-2.562915447f); \ + p3 = p3*stbi__f2f(-1.961570560f); \ + p4 = p4*stbi__f2f(-0.390180644f); \ + t3 += p1+p4; \ + t2 += p2+p3; \ + t1 += p2+p4; \ + t0 += p1+p3; + +static void stbi__idct_block(stbi_uc *out, int out_stride, short data[64]) +{ + int i,val[64],*v=val; + stbi_uc *o; + short *d = data; + + // columns + for (i=0; i < 8; ++i,++d, ++v) { + // if all zeroes, shortcut -- this avoids dequantizing 0s and IDCTing + if (d[ 8]==0 && d[16]==0 && d[24]==0 && d[32]==0 + && d[40]==0 && d[48]==0 && d[56]==0) { + // no shortcut 0 seconds + // (1|2|3|4|5|6|7)==0 0 seconds + // all separate -0.047 seconds + // 1 && 2|3 && 4|5 && 6|7: -0.047 seconds + int dcterm = d[0]*4; + v[0] = v[8] = v[16] = v[24] = v[32] = v[40] = v[48] = v[56] = dcterm; + } else { + STBI__IDCT_1D(d[ 0],d[ 8],d[16],d[24],d[32],d[40],d[48],d[56]) + // constants scaled things up by 1<<12; let's bring them back + // down, but keep 2 extra bits of precision + x0 += 512; x1 += 512; x2 += 512; x3 += 512; + v[ 0] = (x0+t3) >> 10; + v[56] = (x0-t3) >> 10; + v[ 8] = (x1+t2) >> 10; + v[48] = (x1-t2) >> 10; + v[16] = (x2+t1) >> 10; + v[40] = (x2-t1) >> 10; + v[24] = (x3+t0) >> 10; + v[32] = (x3-t0) >> 10; + } + } + + for (i=0, v=val, o=out; i < 8; ++i,v+=8,o+=out_stride) { + // no fast case since the first 1D IDCT spread components out + STBI__IDCT_1D(v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7]) + // constants scaled things up by 1<<12, plus we had 1<<2 from first + // loop, plus horizontal and vertical each scale by sqrt(8) so together + // we've got an extra 1<<3, so 1<<17 total we need to remove. + // so we want to round that, which means adding 0.5 * 1<<17, + // aka 65536. Also, we'll end up with -128 to 127 that we want + // to encode as 0..255 by adding 128, so we'll add that before the shift + x0 += 65536 + (128<<17); + x1 += 65536 + (128<<17); + x2 += 65536 + (128<<17); + x3 += 65536 + (128<<17); + // tried computing the shifts into temps, or'ing the temps to see + // if any were out of range, but that was slower + o[0] = stbi__clamp((x0+t3) >> 17); + o[7] = stbi__clamp((x0-t3) >> 17); + o[1] = stbi__clamp((x1+t2) >> 17); + o[6] = stbi__clamp((x1-t2) >> 17); + o[2] = stbi__clamp((x2+t1) >> 17); + o[5] = stbi__clamp((x2-t1) >> 17); + o[3] = stbi__clamp((x3+t0) >> 17); + o[4] = stbi__clamp((x3-t0) >> 17); + } +} + +#ifdef STBI_SSE2 +// sse2 integer IDCT. not the fastest possible implementation but it +// produces bit-identical results to the generic C version so it's +// fully "transparent". +static void stbi__idct_simd(stbi_uc *out, int out_stride, short data[64]) +{ + // This is constructed to match our regular (generic) integer IDCT exactly. + __m128i row0, row1, row2, row3, row4, row5, row6, row7; + __m128i tmp; + + // dot product constant: even elems=x, odd elems=y + #define dct_const(x,y) _mm_setr_epi16((x),(y),(x),(y),(x),(y),(x),(y)) + + // out(0) = c0[even]*x + c0[odd]*y (c0, x, y 16-bit, out 32-bit) + // out(1) = c1[even]*x + c1[odd]*y + #define dct_rot(out0,out1, x,y,c0,c1) \ + __m128i c0##lo = _mm_unpacklo_epi16((x),(y)); \ + __m128i c0##hi = _mm_unpackhi_epi16((x),(y)); \ + __m128i out0##_l = _mm_madd_epi16(c0##lo, c0); \ + __m128i out0##_h = _mm_madd_epi16(c0##hi, c0); \ + __m128i out1##_l = _mm_madd_epi16(c0##lo, c1); \ + __m128i out1##_h = _mm_madd_epi16(c0##hi, c1) + + // out = in << 12 (in 16-bit, out 32-bit) + #define dct_widen(out, in) \ + __m128i out##_l = _mm_srai_epi32(_mm_unpacklo_epi16(_mm_setzero_si128(), (in)), 4); \ + __m128i out##_h = _mm_srai_epi32(_mm_unpackhi_epi16(_mm_setzero_si128(), (in)), 4) + + // wide add + #define dct_wadd(out, a, b) \ + __m128i out##_l = _mm_add_epi32(a##_l, b##_l); \ + __m128i out##_h = _mm_add_epi32(a##_h, b##_h) + + // wide sub + #define dct_wsub(out, a, b) \ + __m128i out##_l = _mm_sub_epi32(a##_l, b##_l); \ + __m128i out##_h = _mm_sub_epi32(a##_h, b##_h) + + // butterfly a/b, add bias, then shift by "s" and pack + #define dct_bfly32o(out0, out1, a,b,bias,s) \ + { \ + __m128i abiased_l = _mm_add_epi32(a##_l, bias); \ + __m128i abiased_h = _mm_add_epi32(a##_h, bias); \ + dct_wadd(sum, abiased, b); \ + dct_wsub(dif, abiased, b); \ + out0 = _mm_packs_epi32(_mm_srai_epi32(sum_l, s), _mm_srai_epi32(sum_h, s)); \ + out1 = _mm_packs_epi32(_mm_srai_epi32(dif_l, s), _mm_srai_epi32(dif_h, s)); \ + } + + // 8-bit interleave step (for transposes) + #define dct_interleave8(a, b) \ + tmp = a; \ + a = _mm_unpacklo_epi8(a, b); \ + b = _mm_unpackhi_epi8(tmp, b) + + // 16-bit interleave step (for transposes) + #define dct_interleave16(a, b) \ + tmp = a; \ + a = _mm_unpacklo_epi16(a, b); \ + b = _mm_unpackhi_epi16(tmp, b) + + #define dct_pass(bias,shift) \ + { \ + /* even part */ \ + dct_rot(t2e,t3e, row2,row6, rot0_0,rot0_1); \ + __m128i sum04 = _mm_add_epi16(row0, row4); \ + __m128i dif04 = _mm_sub_epi16(row0, row4); \ + dct_widen(t0e, sum04); \ + dct_widen(t1e, dif04); \ + dct_wadd(x0, t0e, t3e); \ + dct_wsub(x3, t0e, t3e); \ + dct_wadd(x1, t1e, t2e); \ + dct_wsub(x2, t1e, t2e); \ + /* odd part */ \ + dct_rot(y0o,y2o, row7,row3, rot2_0,rot2_1); \ + dct_rot(y1o,y3o, row5,row1, rot3_0,rot3_1); \ + __m128i sum17 = _mm_add_epi16(row1, row7); \ + __m128i sum35 = _mm_add_epi16(row3, row5); \ + dct_rot(y4o,y5o, sum17,sum35, rot1_0,rot1_1); \ + dct_wadd(x4, y0o, y4o); \ + dct_wadd(x5, y1o, y5o); \ + dct_wadd(x6, y2o, y5o); \ + dct_wadd(x7, y3o, y4o); \ + dct_bfly32o(row0,row7, x0,x7,bias,shift); \ + dct_bfly32o(row1,row6, x1,x6,bias,shift); \ + dct_bfly32o(row2,row5, x2,x5,bias,shift); \ + dct_bfly32o(row3,row4, x3,x4,bias,shift); \ + } + + __m128i rot0_0 = dct_const(stbi__f2f(0.5411961f), stbi__f2f(0.5411961f) + stbi__f2f(-1.847759065f)); + __m128i rot0_1 = dct_const(stbi__f2f(0.5411961f) + stbi__f2f( 0.765366865f), stbi__f2f(0.5411961f)); + __m128i rot1_0 = dct_const(stbi__f2f(1.175875602f) + stbi__f2f(-0.899976223f), stbi__f2f(1.175875602f)); + __m128i rot1_1 = dct_const(stbi__f2f(1.175875602f), stbi__f2f(1.175875602f) + stbi__f2f(-2.562915447f)); + __m128i rot2_0 = dct_const(stbi__f2f(-1.961570560f) + stbi__f2f( 0.298631336f), stbi__f2f(-1.961570560f)); + __m128i rot2_1 = dct_const(stbi__f2f(-1.961570560f), stbi__f2f(-1.961570560f) + stbi__f2f( 3.072711026f)); + __m128i rot3_0 = dct_const(stbi__f2f(-0.390180644f) + stbi__f2f( 2.053119869f), stbi__f2f(-0.390180644f)); + __m128i rot3_1 = dct_const(stbi__f2f(-0.390180644f), stbi__f2f(-0.390180644f) + stbi__f2f( 1.501321110f)); + + // rounding biases in column/row passes, see stbi__idct_block for explanation. + __m128i bias_0 = _mm_set1_epi32(512); + __m128i bias_1 = _mm_set1_epi32(65536 + (128<<17)); + + // load + row0 = _mm_load_si128((const __m128i *) (data + 0*8)); + row1 = _mm_load_si128((const __m128i *) (data + 1*8)); + row2 = _mm_load_si128((const __m128i *) (data + 2*8)); + row3 = _mm_load_si128((const __m128i *) (data + 3*8)); + row4 = _mm_load_si128((const __m128i *) (data + 4*8)); + row5 = _mm_load_si128((const __m128i *) (data + 5*8)); + row6 = _mm_load_si128((const __m128i *) (data + 6*8)); + row7 = _mm_load_si128((const __m128i *) (data + 7*8)); + + // column pass + dct_pass(bias_0, 10); + + { + // 16bit 8x8 transpose pass 1 + dct_interleave16(row0, row4); + dct_interleave16(row1, row5); + dct_interleave16(row2, row6); + dct_interleave16(row3, row7); + + // transpose pass 2 + dct_interleave16(row0, row2); + dct_interleave16(row1, row3); + dct_interleave16(row4, row6); + dct_interleave16(row5, row7); + + // transpose pass 3 + dct_interleave16(row0, row1); + dct_interleave16(row2, row3); + dct_interleave16(row4, row5); + dct_interleave16(row6, row7); + } + + // row pass + dct_pass(bias_1, 17); + + { + // pack + __m128i p0 = _mm_packus_epi16(row0, row1); // a0a1a2a3...a7b0b1b2b3...b7 + __m128i p1 = _mm_packus_epi16(row2, row3); + __m128i p2 = _mm_packus_epi16(row4, row5); + __m128i p3 = _mm_packus_epi16(row6, row7); + + // 8bit 8x8 transpose pass 1 + dct_interleave8(p0, p2); // a0e0a1e1... + dct_interleave8(p1, p3); // c0g0c1g1... + + // transpose pass 2 + dct_interleave8(p0, p1); // a0c0e0g0... + dct_interleave8(p2, p3); // b0d0f0h0... + + // transpose pass 3 + dct_interleave8(p0, p2); // a0b0c0d0... + dct_interleave8(p1, p3); // a4b4c4d4... + + // store + _mm_storel_epi64((__m128i *) out, p0); out += out_stride; + _mm_storel_epi64((__m128i *) out, _mm_shuffle_epi32(p0, 0x4e)); out += out_stride; + _mm_storel_epi64((__m128i *) out, p2); out += out_stride; + _mm_storel_epi64((__m128i *) out, _mm_shuffle_epi32(p2, 0x4e)); out += out_stride; + _mm_storel_epi64((__m128i *) out, p1); out += out_stride; + _mm_storel_epi64((__m128i *) out, _mm_shuffle_epi32(p1, 0x4e)); out += out_stride; + _mm_storel_epi64((__m128i *) out, p3); out += out_stride; + _mm_storel_epi64((__m128i *) out, _mm_shuffle_epi32(p3, 0x4e)); + } + +#undef dct_const +#undef dct_rot +#undef dct_widen +#undef dct_wadd +#undef dct_wsub +#undef dct_bfly32o +#undef dct_interleave8 +#undef dct_interleave16 +#undef dct_pass +} + +#endif // STBI_SSE2 + +#ifdef STBI_NEON + +// NEON integer IDCT. should produce bit-identical +// results to the generic C version. +static void stbi__idct_simd(stbi_uc *out, int out_stride, short data[64]) +{ + int16x8_t row0, row1, row2, row3, row4, row5, row6, row7; + + int16x4_t rot0_0 = vdup_n_s16(stbi__f2f(0.5411961f)); + int16x4_t rot0_1 = vdup_n_s16(stbi__f2f(-1.847759065f)); + int16x4_t rot0_2 = vdup_n_s16(stbi__f2f( 0.765366865f)); + int16x4_t rot1_0 = vdup_n_s16(stbi__f2f( 1.175875602f)); + int16x4_t rot1_1 = vdup_n_s16(stbi__f2f(-0.899976223f)); + int16x4_t rot1_2 = vdup_n_s16(stbi__f2f(-2.562915447f)); + int16x4_t rot2_0 = vdup_n_s16(stbi__f2f(-1.961570560f)); + int16x4_t rot2_1 = vdup_n_s16(stbi__f2f(-0.390180644f)); + int16x4_t rot3_0 = vdup_n_s16(stbi__f2f( 0.298631336f)); + int16x4_t rot3_1 = vdup_n_s16(stbi__f2f( 2.053119869f)); + int16x4_t rot3_2 = vdup_n_s16(stbi__f2f( 3.072711026f)); + int16x4_t rot3_3 = vdup_n_s16(stbi__f2f( 1.501321110f)); + +#define dct_long_mul(out, inq, coeff) \ + int32x4_t out##_l = vmull_s16(vget_low_s16(inq), coeff); \ + int32x4_t out##_h = vmull_s16(vget_high_s16(inq), coeff) + +#define dct_long_mac(out, acc, inq, coeff) \ + int32x4_t out##_l = vmlal_s16(acc##_l, vget_low_s16(inq), coeff); \ + int32x4_t out##_h = vmlal_s16(acc##_h, vget_high_s16(inq), coeff) + +#define dct_widen(out, inq) \ + int32x4_t out##_l = vshll_n_s16(vget_low_s16(inq), 12); \ + int32x4_t out##_h = vshll_n_s16(vget_high_s16(inq), 12) + +// wide add +#define dct_wadd(out, a, b) \ + int32x4_t out##_l = vaddq_s32(a##_l, b##_l); \ + int32x4_t out##_h = vaddq_s32(a##_h, b##_h) + +// wide sub +#define dct_wsub(out, a, b) \ + int32x4_t out##_l = vsubq_s32(a##_l, b##_l); \ + int32x4_t out##_h = vsubq_s32(a##_h, b##_h) + +// butterfly a/b, then shift using "shiftop" by "s" and pack +#define dct_bfly32o(out0,out1, a,b,shiftop,s) \ + { \ + dct_wadd(sum, a, b); \ + dct_wsub(dif, a, b); \ + out0 = vcombine_s16(shiftop(sum_l, s), shiftop(sum_h, s)); \ + out1 = vcombine_s16(shiftop(dif_l, s), shiftop(dif_h, s)); \ + } + +#define dct_pass(shiftop, shift) \ + { \ + /* even part */ \ + int16x8_t sum26 = vaddq_s16(row2, row6); \ + dct_long_mul(p1e, sum26, rot0_0); \ + dct_long_mac(t2e, p1e, row6, rot0_1); \ + dct_long_mac(t3e, p1e, row2, rot0_2); \ + int16x8_t sum04 = vaddq_s16(row0, row4); \ + int16x8_t dif04 = vsubq_s16(row0, row4); \ + dct_widen(t0e, sum04); \ + dct_widen(t1e, dif04); \ + dct_wadd(x0, t0e, t3e); \ + dct_wsub(x3, t0e, t3e); \ + dct_wadd(x1, t1e, t2e); \ + dct_wsub(x2, t1e, t2e); \ + /* odd part */ \ + int16x8_t sum15 = vaddq_s16(row1, row5); \ + int16x8_t sum17 = vaddq_s16(row1, row7); \ + int16x8_t sum35 = vaddq_s16(row3, row5); \ + int16x8_t sum37 = vaddq_s16(row3, row7); \ + int16x8_t sumodd = vaddq_s16(sum17, sum35); \ + dct_long_mul(p5o, sumodd, rot1_0); \ + dct_long_mac(p1o, p5o, sum17, rot1_1); \ + dct_long_mac(p2o, p5o, sum35, rot1_2); \ + dct_long_mul(p3o, sum37, rot2_0); \ + dct_long_mul(p4o, sum15, rot2_1); \ + dct_wadd(sump13o, p1o, p3o); \ + dct_wadd(sump24o, p2o, p4o); \ + dct_wadd(sump23o, p2o, p3o); \ + dct_wadd(sump14o, p1o, p4o); \ + dct_long_mac(x4, sump13o, row7, rot3_0); \ + dct_long_mac(x5, sump24o, row5, rot3_1); \ + dct_long_mac(x6, sump23o, row3, rot3_2); \ + dct_long_mac(x7, sump14o, row1, rot3_3); \ + dct_bfly32o(row0,row7, x0,x7,shiftop,shift); \ + dct_bfly32o(row1,row6, x1,x6,shiftop,shift); \ + dct_bfly32o(row2,row5, x2,x5,shiftop,shift); \ + dct_bfly32o(row3,row4, x3,x4,shiftop,shift); \ + } + + // load + row0 = vld1q_s16(data + 0*8); + row1 = vld1q_s16(data + 1*8); + row2 = vld1q_s16(data + 2*8); + row3 = vld1q_s16(data + 3*8); + row4 = vld1q_s16(data + 4*8); + row5 = vld1q_s16(data + 5*8); + row6 = vld1q_s16(data + 6*8); + row7 = vld1q_s16(data + 7*8); + + // add DC bias + row0 = vaddq_s16(row0, vsetq_lane_s16(1024, vdupq_n_s16(0), 0)); + + // column pass + dct_pass(vrshrn_n_s32, 10); + + // 16bit 8x8 transpose + { +// these three map to a single VTRN.16, VTRN.32, and VSWP, respectively. +// whether compilers actually get this is another story, sadly. +#define dct_trn16(x, y) { int16x8x2_t t = vtrnq_s16(x, y); x = t.val[0]; y = t.val[1]; } +#define dct_trn32(x, y) { int32x4x2_t t = vtrnq_s32(vreinterpretq_s32_s16(x), vreinterpretq_s32_s16(y)); x = vreinterpretq_s16_s32(t.val[0]); y = vreinterpretq_s16_s32(t.val[1]); } +#define dct_trn64(x, y) { int16x8_t x0 = x; int16x8_t y0 = y; x = vcombine_s16(vget_low_s16(x0), vget_low_s16(y0)); y = vcombine_s16(vget_high_s16(x0), vget_high_s16(y0)); } + + // pass 1 + dct_trn16(row0, row1); // a0b0a2b2a4b4a6b6 + dct_trn16(row2, row3); + dct_trn16(row4, row5); + dct_trn16(row6, row7); + + // pass 2 + dct_trn32(row0, row2); // a0b0c0d0a4b4c4d4 + dct_trn32(row1, row3); + dct_trn32(row4, row6); + dct_trn32(row5, row7); + + // pass 3 + dct_trn64(row0, row4); // a0b0c0d0e0f0g0h0 + dct_trn64(row1, row5); + dct_trn64(row2, row6); + dct_trn64(row3, row7); + +#undef dct_trn16 +#undef dct_trn32 +#undef dct_trn64 + } + + // row pass + // vrshrn_n_s32 only supports shifts up to 16, we need + // 17. so do a non-rounding shift of 16 first then follow + // up with a rounding shift by 1. + dct_pass(vshrn_n_s32, 16); + + { + // pack and round + uint8x8_t p0 = vqrshrun_n_s16(row0, 1); + uint8x8_t p1 = vqrshrun_n_s16(row1, 1); + uint8x8_t p2 = vqrshrun_n_s16(row2, 1); + uint8x8_t p3 = vqrshrun_n_s16(row3, 1); + uint8x8_t p4 = vqrshrun_n_s16(row4, 1); + uint8x8_t p5 = vqrshrun_n_s16(row5, 1); + uint8x8_t p6 = vqrshrun_n_s16(row6, 1); + uint8x8_t p7 = vqrshrun_n_s16(row7, 1); + + // again, these can translate into one instruction, but often don't. +#define dct_trn8_8(x, y) { uint8x8x2_t t = vtrn_u8(x, y); x = t.val[0]; y = t.val[1]; } +#define dct_trn8_16(x, y) { uint16x4x2_t t = vtrn_u16(vreinterpret_u16_u8(x), vreinterpret_u16_u8(y)); x = vreinterpret_u8_u16(t.val[0]); y = vreinterpret_u8_u16(t.val[1]); } +#define dct_trn8_32(x, y) { uint32x2x2_t t = vtrn_u32(vreinterpret_u32_u8(x), vreinterpret_u32_u8(y)); x = vreinterpret_u8_u32(t.val[0]); y = vreinterpret_u8_u32(t.val[1]); } + + // sadly can't use interleaved stores here since we only write + // 8 bytes to each scan line! + + // 8x8 8-bit transpose pass 1 + dct_trn8_8(p0, p1); + dct_trn8_8(p2, p3); + dct_trn8_8(p4, p5); + dct_trn8_8(p6, p7); + + // pass 2 + dct_trn8_16(p0, p2); + dct_trn8_16(p1, p3); + dct_trn8_16(p4, p6); + dct_trn8_16(p5, p7); + + // pass 3 + dct_trn8_32(p0, p4); + dct_trn8_32(p1, p5); + dct_trn8_32(p2, p6); + dct_trn8_32(p3, p7); + + // store + vst1_u8(out, p0); out += out_stride; + vst1_u8(out, p1); out += out_stride; + vst1_u8(out, p2); out += out_stride; + vst1_u8(out, p3); out += out_stride; + vst1_u8(out, p4); out += out_stride; + vst1_u8(out, p5); out += out_stride; + vst1_u8(out, p6); out += out_stride; + vst1_u8(out, p7); + +#undef dct_trn8_8 +#undef dct_trn8_16 +#undef dct_trn8_32 + } + +#undef dct_long_mul +#undef dct_long_mac +#undef dct_widen +#undef dct_wadd +#undef dct_wsub +#undef dct_bfly32o +#undef dct_pass +} + +#endif // STBI_NEON + +#define STBI__MARKER_none 0xff +// if there's a pending marker from the entropy stream, return that +// otherwise, fetch from the stream and get a marker. if there's no +// marker, return 0xff, which is never a valid marker value +static stbi_uc stbi__get_marker(stbi__jpeg *j) +{ + stbi_uc x; + if (j->marker != STBI__MARKER_none) { x = j->marker; j->marker = STBI__MARKER_none; return x; } + x = stbi__get8(j->s); + if (x != 0xff) return STBI__MARKER_none; + while (x == 0xff) + x = stbi__get8(j->s); // consume repeated 0xff fill bytes + return x; +} + +// in each scan, we'll have scan_n components, and the order +// of the components is specified by order[] +#define STBI__RESTART(x) ((x) >= 0xd0 && (x) <= 0xd7) + +// after a restart interval, stbi__jpeg_reset the entropy decoder and +// the dc prediction +static void stbi__jpeg_reset(stbi__jpeg *j) +{ + j->code_bits = 0; + j->code_buffer = 0; + j->nomore = 0; + j->img_comp[0].dc_pred = j->img_comp[1].dc_pred = j->img_comp[2].dc_pred = j->img_comp[3].dc_pred = 0; + j->marker = STBI__MARKER_none; + j->todo = j->restart_interval ? j->restart_interval : 0x7fffffff; + j->eob_run = 0; + // no more than 1<<31 MCUs if no restart_interal? that's plenty safe, + // since we don't even allow 1<<30 pixels +} + +static int stbi__parse_entropy_coded_data(stbi__jpeg *z) +{ + stbi__jpeg_reset(z); + if (!z->progressive) { + if (z->scan_n == 1) { + int i,j; + STBI_SIMD_ALIGN(short, data[64]); + int n = z->order[0]; + // non-interleaved data, we just need to process one block at a time, + // in trivial scanline order + // number of blocks to do just depends on how many actual "pixels" this + // component has, independent of interleaved MCU blocking and such + int w = (z->img_comp[n].x+7) >> 3; + int h = (z->img_comp[n].y+7) >> 3; + for (j=0; j < h; ++j) { + for (i=0; i < w; ++i) { + int ha = z->img_comp[n].ha; + if (!stbi__jpeg_decode_block(z, data, z->huff_dc+z->img_comp[n].hd, z->huff_ac+ha, z->fast_ac[ha], n, z->dequant[z->img_comp[n].tq])) return 0; + z->idct_block_kernel(z->img_comp[n].data+z->img_comp[n].w2*j*8+i*8, z->img_comp[n].w2, data); + // every data block is an MCU, so countdown the restart interval + if (--z->todo <= 0) { + if (z->code_bits < 24) stbi__grow_buffer_unsafe(z); + // if it's NOT a restart, then just bail, so we get corrupt data + // rather than no data + if (!STBI__RESTART(z->marker)) return 1; + stbi__jpeg_reset(z); + } + } + } + return 1; + } else { // interleaved + int i,j,k,x,y; + STBI_SIMD_ALIGN(short, data[64]); + for (j=0; j < z->img_mcu_y; ++j) { + for (i=0; i < z->img_mcu_x; ++i) { + // scan an interleaved mcu... process scan_n components in order + for (k=0; k < z->scan_n; ++k) { + int n = z->order[k]; + // scan out an mcu's worth of this component; that's just determined + // by the basic H and V specified for the component + for (y=0; y < z->img_comp[n].v; ++y) { + for (x=0; x < z->img_comp[n].h; ++x) { + int x2 = (i*z->img_comp[n].h + x)*8; + int y2 = (j*z->img_comp[n].v + y)*8; + int ha = z->img_comp[n].ha; + if (!stbi__jpeg_decode_block(z, data, z->huff_dc+z->img_comp[n].hd, z->huff_ac+ha, z->fast_ac[ha], n, z->dequant[z->img_comp[n].tq])) return 0; + z->idct_block_kernel(z->img_comp[n].data+z->img_comp[n].w2*y2+x2, z->img_comp[n].w2, data); + } + } + } + // after all interleaved components, that's an interleaved MCU, + // so now count down the restart interval + if (--z->todo <= 0) { + if (z->code_bits < 24) stbi__grow_buffer_unsafe(z); + if (!STBI__RESTART(z->marker)) return 1; + stbi__jpeg_reset(z); + } + } + } + return 1; + } + } else { + if (z->scan_n == 1) { + int i,j; + int n = z->order[0]; + // non-interleaved data, we just need to process one block at a time, + // in trivial scanline order + // number of blocks to do just depends on how many actual "pixels" this + // component has, independent of interleaved MCU blocking and such + int w = (z->img_comp[n].x+7) >> 3; + int h = (z->img_comp[n].y+7) >> 3; + for (j=0; j < h; ++j) { + for (i=0; i < w; ++i) { + short *data = z->img_comp[n].coeff + 64 * (i + j * z->img_comp[n].coeff_w); + if (z->spec_start == 0) { + if (!stbi__jpeg_decode_block_prog_dc(z, data, &z->huff_dc[z->img_comp[n].hd], n)) + return 0; + } else { + int ha = z->img_comp[n].ha; + if (!stbi__jpeg_decode_block_prog_ac(z, data, &z->huff_ac[ha], z->fast_ac[ha])) + return 0; + } + // every data block is an MCU, so countdown the restart interval + if (--z->todo <= 0) { + if (z->code_bits < 24) stbi__grow_buffer_unsafe(z); + if (!STBI__RESTART(z->marker)) return 1; + stbi__jpeg_reset(z); + } + } + } + return 1; + } else { // interleaved + int i,j,k,x,y; + for (j=0; j < z->img_mcu_y; ++j) { + for (i=0; i < z->img_mcu_x; ++i) { + // scan an interleaved mcu... process scan_n components in order + for (k=0; k < z->scan_n; ++k) { + int n = z->order[k]; + // scan out an mcu's worth of this component; that's just determined + // by the basic H and V specified for the component + for (y=0; y < z->img_comp[n].v; ++y) { + for (x=0; x < z->img_comp[n].h; ++x) { + int x2 = (i*z->img_comp[n].h + x); + int y2 = (j*z->img_comp[n].v + y); + short *data = z->img_comp[n].coeff + 64 * (x2 + y2 * z->img_comp[n].coeff_w); + if (!stbi__jpeg_decode_block_prog_dc(z, data, &z->huff_dc[z->img_comp[n].hd], n)) + return 0; + } + } + } + // after all interleaved components, that's an interleaved MCU, + // so now count down the restart interval + if (--z->todo <= 0) { + if (z->code_bits < 24) stbi__grow_buffer_unsafe(z); + if (!STBI__RESTART(z->marker)) return 1; + stbi__jpeg_reset(z); + } + } + } + return 1; + } + } +} + +static void stbi__jpeg_dequantize(short *data, stbi__uint16 *dequant) +{ + int i; + for (i=0; i < 64; ++i) + data[i] *= dequant[i]; +} + +static void stbi__jpeg_finish(stbi__jpeg *z) +{ + if (z->progressive) { + // dequantize and idct the data + int i,j,n; + for (n=0; n < z->s->img_n; ++n) { + int w = (z->img_comp[n].x+7) >> 3; + int h = (z->img_comp[n].y+7) >> 3; + for (j=0; j < h; ++j) { + for (i=0; i < w; ++i) { + short *data = z->img_comp[n].coeff + 64 * (i + j * z->img_comp[n].coeff_w); + stbi__jpeg_dequantize(data, z->dequant[z->img_comp[n].tq]); + z->idct_block_kernel(z->img_comp[n].data+z->img_comp[n].w2*j*8+i*8, z->img_comp[n].w2, data); + } + } + } + } +} + +static int stbi__process_marker(stbi__jpeg *z, int m) +{ + int L; + switch (m) { + case STBI__MARKER_none: // no marker found + return stbi__err("expected marker","Corrupt JPEG"); + + case 0xDD: // DRI - specify restart interval + if (stbi__get16be(z->s) != 4) return stbi__err("bad DRI len","Corrupt JPEG"); + z->restart_interval = stbi__get16be(z->s); + return 1; + + case 0xDB: // DQT - define quantization table + L = stbi__get16be(z->s)-2; + while (L > 0) { + int q = stbi__get8(z->s); + int p = q >> 4, sixteen = (p != 0); + int t = q & 15,i; + if (p != 0 && p != 1) return stbi__err("bad DQT type","Corrupt JPEG"); + if (t > 3) return stbi__err("bad DQT table","Corrupt JPEG"); + + for (i=0; i < 64; ++i) + z->dequant[t][stbi__jpeg_dezigzag[i]] = (stbi__uint16)(sixteen ? stbi__get16be(z->s) : stbi__get8(z->s)); + L -= (sixteen ? 129 : 65); + } + return L==0; + + case 0xC4: // DHT - define huffman table + L = stbi__get16be(z->s)-2; + while (L > 0) { + stbi_uc *v; + int sizes[16],i,n=0; + int q = stbi__get8(z->s); + int tc = q >> 4; + int th = q & 15; + if (tc > 1 || th > 3) return stbi__err("bad DHT header","Corrupt JPEG"); + for (i=0; i < 16; ++i) { + sizes[i] = stbi__get8(z->s); + n += sizes[i]; + } + if(n > 256) return stbi__err("bad DHT header","Corrupt JPEG"); // Loop over i < n would write past end of values! + L -= 17; + if (tc == 0) { + if (!stbi__build_huffman(z->huff_dc+th, sizes)) return 0; + v = z->huff_dc[th].values; + } else { + if (!stbi__build_huffman(z->huff_ac+th, sizes)) return 0; + v = z->huff_ac[th].values; + } + for (i=0; i < n; ++i) + v[i] = stbi__get8(z->s); + if (tc != 0) + stbi__build_fast_ac(z->fast_ac[th], z->huff_ac + th); + L -= n; + } + return L==0; + } + + // check for comment block or APP blocks + if ((m >= 0xE0 && m <= 0xEF) || m == 0xFE) { + L = stbi__get16be(z->s); + if (L < 2) { + if (m == 0xFE) + return stbi__err("bad COM len","Corrupt JPEG"); + else + return stbi__err("bad APP len","Corrupt JPEG"); + } + L -= 2; + + if (m == 0xE0 && L >= 5) { // JFIF APP0 segment + static const unsigned char tag[5] = {'J','F','I','F','\0'}; + int ok = 1; + int i; + for (i=0; i < 5; ++i) + if (stbi__get8(z->s) != tag[i]) + ok = 0; + L -= 5; + if (ok) + z->jfif = 1; + } else if (m == 0xEE && L >= 12) { // Adobe APP14 segment + static const unsigned char tag[6] = {'A','d','o','b','e','\0'}; + int ok = 1; + int i; + for (i=0; i < 6; ++i) + if (stbi__get8(z->s) != tag[i]) + ok = 0; + L -= 6; + if (ok) { + stbi__get8(z->s); // version + stbi__get16be(z->s); // flags0 + stbi__get16be(z->s); // flags1 + z->app14_color_transform = stbi__get8(z->s); // color transform + L -= 6; + } + } + + stbi__skip(z->s, L); + return 1; + } + + return stbi__err("unknown marker","Corrupt JPEG"); +} + +// after we see SOS +static int stbi__process_scan_header(stbi__jpeg *z) +{ + int i; + int Ls = stbi__get16be(z->s); + z->scan_n = stbi__get8(z->s); + if (z->scan_n < 1 || z->scan_n > 4 || z->scan_n > (int) z->s->img_n) return stbi__err("bad SOS component count","Corrupt JPEG"); + if (Ls != 6+2*z->scan_n) return stbi__err("bad SOS len","Corrupt JPEG"); + for (i=0; i < z->scan_n; ++i) { + int id = stbi__get8(z->s), which; + int q = stbi__get8(z->s); + for (which = 0; which < z->s->img_n; ++which) + if (z->img_comp[which].id == id) + break; + if (which == z->s->img_n) return 0; // no match + z->img_comp[which].hd = q >> 4; if (z->img_comp[which].hd > 3) return stbi__err("bad DC huff","Corrupt JPEG"); + z->img_comp[which].ha = q & 15; if (z->img_comp[which].ha > 3) return stbi__err("bad AC huff","Corrupt JPEG"); + z->order[i] = which; + } + + { + int aa; + z->spec_start = stbi__get8(z->s); + z->spec_end = stbi__get8(z->s); // should be 63, but might be 0 + aa = stbi__get8(z->s); + z->succ_high = (aa >> 4); + z->succ_low = (aa & 15); + if (z->progressive) { + if (z->spec_start > 63 || z->spec_end > 63 || z->spec_start > z->spec_end || z->succ_high > 13 || z->succ_low > 13) + return stbi__err("bad SOS", "Corrupt JPEG"); + } else { + if (z->spec_start != 0) return stbi__err("bad SOS","Corrupt JPEG"); + if (z->succ_high != 0 || z->succ_low != 0) return stbi__err("bad SOS","Corrupt JPEG"); + z->spec_end = 63; + } + } + + return 1; +} + +static int stbi__free_jpeg_components(stbi__jpeg *z, int ncomp, int why) +{ + int i; + for (i=0; i < ncomp; ++i) { + if (z->img_comp[i].raw_data) { + STBI_FREE(z->img_comp[i].raw_data); + z->img_comp[i].raw_data = NULL; + z->img_comp[i].data = NULL; + } + if (z->img_comp[i].raw_coeff) { + STBI_FREE(z->img_comp[i].raw_coeff); + z->img_comp[i].raw_coeff = 0; + z->img_comp[i].coeff = 0; + } + if (z->img_comp[i].linebuf) { + STBI_FREE(z->img_comp[i].linebuf); + z->img_comp[i].linebuf = NULL; + } + } + return why; +} + +static int stbi__process_frame_header(stbi__jpeg *z, int scan) +{ + stbi__context *s = z->s; + int Lf,p,i,q, h_max=1,v_max=1,c; + Lf = stbi__get16be(s); if (Lf < 11) return stbi__err("bad SOF len","Corrupt JPEG"); // JPEG + p = stbi__get8(s); if (p != 8) return stbi__err("only 8-bit","JPEG format not supported: 8-bit only"); // JPEG baseline + s->img_y = stbi__get16be(s); if (s->img_y == 0) return stbi__err("no header height", "JPEG format not supported: delayed height"); // Legal, but we don't handle it--but neither does IJG + s->img_x = stbi__get16be(s); if (s->img_x == 0) return stbi__err("0 width","Corrupt JPEG"); // JPEG requires + if (s->img_y > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)"); + if (s->img_x > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)"); + c = stbi__get8(s); + if (c != 3 && c != 1 && c != 4) return stbi__err("bad component count","Corrupt JPEG"); + s->img_n = c; + for (i=0; i < c; ++i) { + z->img_comp[i].data = NULL; + z->img_comp[i].linebuf = NULL; + } + + if (Lf != 8+3*s->img_n) return stbi__err("bad SOF len","Corrupt JPEG"); + + z->rgb = 0; + for (i=0; i < s->img_n; ++i) { + static const unsigned char rgb[3] = { 'R', 'G', 'B' }; + z->img_comp[i].id = stbi__get8(s); + if (s->img_n == 3 && z->img_comp[i].id == rgb[i]) + ++z->rgb; + q = stbi__get8(s); + z->img_comp[i].h = (q >> 4); if (!z->img_comp[i].h || z->img_comp[i].h > 4) return stbi__err("bad H","Corrupt JPEG"); + z->img_comp[i].v = q & 15; if (!z->img_comp[i].v || z->img_comp[i].v > 4) return stbi__err("bad V","Corrupt JPEG"); + z->img_comp[i].tq = stbi__get8(s); if (z->img_comp[i].tq > 3) return stbi__err("bad TQ","Corrupt JPEG"); + } + + if (scan != STBI__SCAN_load) return 1; + + if (!stbi__mad3sizes_valid(s->img_x, s->img_y, s->img_n, 0)) return stbi__err("too large", "Image too large to decode"); + + for (i=0; i < s->img_n; ++i) { + if (z->img_comp[i].h > h_max) h_max = z->img_comp[i].h; + if (z->img_comp[i].v > v_max) v_max = z->img_comp[i].v; + } + + // check that plane subsampling factors are integer ratios; our resamplers can't deal with fractional ratios + // and I've never seen a non-corrupted JPEG file actually use them + for (i=0; i < s->img_n; ++i) { + if (h_max % z->img_comp[i].h != 0) return stbi__err("bad H","Corrupt JPEG"); + if (v_max % z->img_comp[i].v != 0) return stbi__err("bad V","Corrupt JPEG"); + } + + // compute interleaved mcu info + z->img_h_max = h_max; + z->img_v_max = v_max; + z->img_mcu_w = h_max * 8; + z->img_mcu_h = v_max * 8; + // these sizes can't be more than 17 bits + z->img_mcu_x = (s->img_x + z->img_mcu_w-1) / z->img_mcu_w; + z->img_mcu_y = (s->img_y + z->img_mcu_h-1) / z->img_mcu_h; + + for (i=0; i < s->img_n; ++i) { + // number of effective pixels (e.g. for non-interleaved MCU) + z->img_comp[i].x = (s->img_x * z->img_comp[i].h + h_max-1) / h_max; + z->img_comp[i].y = (s->img_y * z->img_comp[i].v + v_max-1) / v_max; + // to simplify generation, we'll allocate enough memory to decode + // the bogus oversized data from using interleaved MCUs and their + // big blocks (e.g. a 16x16 iMCU on an image of width 33); we won't + // discard the extra data until colorspace conversion + // + // img_mcu_x, img_mcu_y: <=17 bits; comp[i].h and .v are <=4 (checked earlier) + // so these muls can't overflow with 32-bit ints (which we require) + z->img_comp[i].w2 = z->img_mcu_x * z->img_comp[i].h * 8; + z->img_comp[i].h2 = z->img_mcu_y * z->img_comp[i].v * 8; + z->img_comp[i].coeff = 0; + z->img_comp[i].raw_coeff = 0; + z->img_comp[i].linebuf = NULL; + z->img_comp[i].raw_data = stbi__malloc_mad2(z->img_comp[i].w2, z->img_comp[i].h2, 15); + if (z->img_comp[i].raw_data == NULL) + return stbi__free_jpeg_components(z, i+1, stbi__err("outofmem", "Out of memory")); + // align blocks for idct using mmx/sse + z->img_comp[i].data = (stbi_uc*) (((size_t) z->img_comp[i].raw_data + 15) & ~15); + if (z->progressive) { + // w2, h2 are multiples of 8 (see above) + z->img_comp[i].coeff_w = z->img_comp[i].w2 / 8; + z->img_comp[i].coeff_h = z->img_comp[i].h2 / 8; + z->img_comp[i].raw_coeff = stbi__malloc_mad3(z->img_comp[i].w2, z->img_comp[i].h2, sizeof(short), 15); + if (z->img_comp[i].raw_coeff == NULL) + return stbi__free_jpeg_components(z, i+1, stbi__err("outofmem", "Out of memory")); + z->img_comp[i].coeff = (short*) (((size_t) z->img_comp[i].raw_coeff + 15) & ~15); + } + } + + return 1; +} + +// use comparisons since in some cases we handle more than one case (e.g. SOF) +#define stbi__DNL(x) ((x) == 0xdc) +#define stbi__SOI(x) ((x) == 0xd8) +#define stbi__EOI(x) ((x) == 0xd9) +#define stbi__SOF(x) ((x) == 0xc0 || (x) == 0xc1 || (x) == 0xc2) +#define stbi__SOS(x) ((x) == 0xda) + +#define stbi__SOF_progressive(x) ((x) == 0xc2) + +static int stbi__decode_jpeg_header(stbi__jpeg *z, int scan) +{ + int m; + z->jfif = 0; + z->app14_color_transform = -1; // valid values are 0,1,2 + z->marker = STBI__MARKER_none; // initialize cached marker to empty + m = stbi__get_marker(z); + if (!stbi__SOI(m)) return stbi__err("no SOI","Corrupt JPEG"); + if (scan == STBI__SCAN_type) return 1; + m = stbi__get_marker(z); + while (!stbi__SOF(m)) { + if (!stbi__process_marker(z,m)) return 0; + m = stbi__get_marker(z); + while (m == STBI__MARKER_none) { + // some files have extra padding after their blocks, so ok, we'll scan + if (stbi__at_eof(z->s)) return stbi__err("no SOF", "Corrupt JPEG"); + m = stbi__get_marker(z); + } + } + z->progressive = stbi__SOF_progressive(m); + if (!stbi__process_frame_header(z, scan)) return 0; + return 1; +} + +static stbi_uc stbi__skip_jpeg_junk_at_end(stbi__jpeg *j) +{ + // some JPEGs have junk at end, skip over it but if we find what looks + // like a valid marker, resume there + while (!stbi__at_eof(j->s)) { + stbi_uc x = stbi__get8(j->s); + while (x == 0xff) { // might be a marker + if (stbi__at_eof(j->s)) return STBI__MARKER_none; + x = stbi__get8(j->s); + if (x != 0x00 && x != 0xff) { + // not a stuffed zero or lead-in to another marker, looks + // like an actual marker, return it + return x; + } + // stuffed zero has x=0 now which ends the loop, meaning we go + // back to regular scan loop. + // repeated 0xff keeps trying to read the next byte of the marker. + } + } + return STBI__MARKER_none; +} + +// decode image to YCbCr format +static int stbi__decode_jpeg_image(stbi__jpeg *j) +{ + int m; + for (m = 0; m < 4; m++) { + j->img_comp[m].raw_data = NULL; + j->img_comp[m].raw_coeff = NULL; + } + j->restart_interval = 0; + if (!stbi__decode_jpeg_header(j, STBI__SCAN_load)) return 0; + m = stbi__get_marker(j); + while (!stbi__EOI(m)) { + if (stbi__SOS(m)) { + if (!stbi__process_scan_header(j)) return 0; + if (!stbi__parse_entropy_coded_data(j)) return 0; + if (j->marker == STBI__MARKER_none ) { + j->marker = stbi__skip_jpeg_junk_at_end(j); + // if we reach eof without hitting a marker, stbi__get_marker() below will fail and we'll eventually return 0 + } + m = stbi__get_marker(j); + if (STBI__RESTART(m)) + m = stbi__get_marker(j); + } else if (stbi__DNL(m)) { + int Ld = stbi__get16be(j->s); + stbi__uint32 NL = stbi__get16be(j->s); + if (Ld != 4) return stbi__err("bad DNL len", "Corrupt JPEG"); + if (NL != j->s->img_y) return stbi__err("bad DNL height", "Corrupt JPEG"); + m = stbi__get_marker(j); + } else { + if (!stbi__process_marker(j, m)) return 1; + m = stbi__get_marker(j); + } + } + if (j->progressive) + stbi__jpeg_finish(j); + return 1; +} + +// static jfif-centered resampling (across block boundaries) + +typedef stbi_uc *(*resample_row_func)(stbi_uc *out, stbi_uc *in0, stbi_uc *in1, + int w, int hs); + +#define stbi__div4(x) ((stbi_uc) ((x) >> 2)) + +static stbi_uc *resample_row_1(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs) +{ + STBI_NOTUSED(out); + STBI_NOTUSED(in_far); + STBI_NOTUSED(w); + STBI_NOTUSED(hs); + return in_near; +} + +static stbi_uc* stbi__resample_row_v_2(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs) +{ + // need to generate two samples vertically for every one in input + int i; + STBI_NOTUSED(hs); + for (i=0; i < w; ++i) + out[i] = stbi__div4(3*in_near[i] + in_far[i] + 2); + return out; +} + +static stbi_uc* stbi__resample_row_h_2(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs) +{ + // need to generate two samples horizontally for every one in input + int i; + stbi_uc *input = in_near; + + if (w == 1) { + // if only one sample, can't do any interpolation + out[0] = out[1] = input[0]; + return out; + } + + out[0] = input[0]; + out[1] = stbi__div4(input[0]*3 + input[1] + 2); + for (i=1; i < w-1; ++i) { + int n = 3*input[i]+2; + out[i*2+0] = stbi__div4(n+input[i-1]); + out[i*2+1] = stbi__div4(n+input[i+1]); + } + out[i*2+0] = stbi__div4(input[w-2]*3 + input[w-1] + 2); + out[i*2+1] = input[w-1]; + + STBI_NOTUSED(in_far); + STBI_NOTUSED(hs); + + return out; +} + +#define stbi__div16(x) ((stbi_uc) ((x) >> 4)) + +static stbi_uc *stbi__resample_row_hv_2(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs) +{ + // need to generate 2x2 samples for every one in input + int i,t0,t1; + if (w == 1) { + out[0] = out[1] = stbi__div4(3*in_near[0] + in_far[0] + 2); + return out; + } + + t1 = 3*in_near[0] + in_far[0]; + out[0] = stbi__div4(t1+2); + for (i=1; i < w; ++i) { + t0 = t1; + t1 = 3*in_near[i]+in_far[i]; + out[i*2-1] = stbi__div16(3*t0 + t1 + 8); + out[i*2 ] = stbi__div16(3*t1 + t0 + 8); + } + out[w*2-1] = stbi__div4(t1+2); + + STBI_NOTUSED(hs); + + return out; +} + +#if defined(STBI_SSE2) || defined(STBI_NEON) +static stbi_uc *stbi__resample_row_hv_2_simd(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs) +{ + // need to generate 2x2 samples for every one in input + int i=0,t0,t1; + + if (w == 1) { + out[0] = out[1] = stbi__div4(3*in_near[0] + in_far[0] + 2); + return out; + } + + t1 = 3*in_near[0] + in_far[0]; + // process groups of 8 pixels for as long as we can. + // note we can't handle the last pixel in a row in this loop + // because we need to handle the filter boundary conditions. + for (; i < ((w-1) & ~7); i += 8) { +#if defined(STBI_SSE2) + // load and perform the vertical filtering pass + // this uses 3*x + y = 4*x + (y - x) + __m128i zero = _mm_setzero_si128(); + __m128i farb = _mm_loadl_epi64((__m128i *) (in_far + i)); + __m128i nearb = _mm_loadl_epi64((__m128i *) (in_near + i)); + __m128i farw = _mm_unpacklo_epi8(farb, zero); + __m128i nearw = _mm_unpacklo_epi8(nearb, zero); + __m128i diff = _mm_sub_epi16(farw, nearw); + __m128i nears = _mm_slli_epi16(nearw, 2); + __m128i curr = _mm_add_epi16(nears, diff); // current row + + // horizontal filter works the same based on shifted vers of current + // row. "prev" is current row shifted right by 1 pixel; we need to + // insert the previous pixel value (from t1). + // "next" is current row shifted left by 1 pixel, with first pixel + // of next block of 8 pixels added in. + __m128i prv0 = _mm_slli_si128(curr, 2); + __m128i nxt0 = _mm_srli_si128(curr, 2); + __m128i prev = _mm_insert_epi16(prv0, t1, 0); + __m128i next = _mm_insert_epi16(nxt0, 3*in_near[i+8] + in_far[i+8], 7); + + // horizontal filter, polyphase implementation since it's convenient: + // even pixels = 3*cur + prev = cur*4 + (prev - cur) + // odd pixels = 3*cur + next = cur*4 + (next - cur) + // note the shared term. + __m128i bias = _mm_set1_epi16(8); + __m128i curs = _mm_slli_epi16(curr, 2); + __m128i prvd = _mm_sub_epi16(prev, curr); + __m128i nxtd = _mm_sub_epi16(next, curr); + __m128i curb = _mm_add_epi16(curs, bias); + __m128i even = _mm_add_epi16(prvd, curb); + __m128i odd = _mm_add_epi16(nxtd, curb); + + // interleave even and odd pixels, then undo scaling. + __m128i int0 = _mm_unpacklo_epi16(even, odd); + __m128i int1 = _mm_unpackhi_epi16(even, odd); + __m128i de0 = _mm_srli_epi16(int0, 4); + __m128i de1 = _mm_srli_epi16(int1, 4); + + // pack and write output + __m128i outv = _mm_packus_epi16(de0, de1); + _mm_storeu_si128((__m128i *) (out + i*2), outv); +#elif defined(STBI_NEON) + // load and perform the vertical filtering pass + // this uses 3*x + y = 4*x + (y - x) + uint8x8_t farb = vld1_u8(in_far + i); + uint8x8_t nearb = vld1_u8(in_near + i); + int16x8_t diff = vreinterpretq_s16_u16(vsubl_u8(farb, nearb)); + int16x8_t nears = vreinterpretq_s16_u16(vshll_n_u8(nearb, 2)); + int16x8_t curr = vaddq_s16(nears, diff); // current row + + // horizontal filter works the same based on shifted vers of current + // row. "prev" is current row shifted right by 1 pixel; we need to + // insert the previous pixel value (from t1). + // "next" is current row shifted left by 1 pixel, with first pixel + // of next block of 8 pixels added in. + int16x8_t prv0 = vextq_s16(curr, curr, 7); + int16x8_t nxt0 = vextq_s16(curr, curr, 1); + int16x8_t prev = vsetq_lane_s16(t1, prv0, 0); + int16x8_t next = vsetq_lane_s16(3*in_near[i+8] + in_far[i+8], nxt0, 7); + + // horizontal filter, polyphase implementation since it's convenient: + // even pixels = 3*cur + prev = cur*4 + (prev - cur) + // odd pixels = 3*cur + next = cur*4 + (next - cur) + // note the shared term. + int16x8_t curs = vshlq_n_s16(curr, 2); + int16x8_t prvd = vsubq_s16(prev, curr); + int16x8_t nxtd = vsubq_s16(next, curr); + int16x8_t even = vaddq_s16(curs, prvd); + int16x8_t odd = vaddq_s16(curs, nxtd); + + // undo scaling and round, then store with even/odd phases interleaved + uint8x8x2_t o; + o.val[0] = vqrshrun_n_s16(even, 4); + o.val[1] = vqrshrun_n_s16(odd, 4); + vst2_u8(out + i*2, o); +#endif + + // "previous" value for next iter + t1 = 3*in_near[i+7] + in_far[i+7]; + } + + t0 = t1; + t1 = 3*in_near[i] + in_far[i]; + out[i*2] = stbi__div16(3*t1 + t0 + 8); + + for (++i; i < w; ++i) { + t0 = t1; + t1 = 3*in_near[i]+in_far[i]; + out[i*2-1] = stbi__div16(3*t0 + t1 + 8); + out[i*2 ] = stbi__div16(3*t1 + t0 + 8); + } + out[w*2-1] = stbi__div4(t1+2); + + STBI_NOTUSED(hs); + + return out; +} +#endif + +static stbi_uc *stbi__resample_row_generic(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs) +{ + // resample with nearest-neighbor + int i,j; + STBI_NOTUSED(in_far); + for (i=0; i < w; ++i) + for (j=0; j < hs; ++j) + out[i*hs+j] = in_near[i]; + return out; +} + +// this is a reduced-precision calculation of YCbCr-to-RGB introduced +// to make sure the code produces the same results in both SIMD and scalar +#define stbi__float2fixed(x) (((int) ((x) * 4096.0f + 0.5f)) << 8) +static void stbi__YCbCr_to_RGB_row(stbi_uc *out, const stbi_uc *y, const stbi_uc *pcb, const stbi_uc *pcr, int count, int step) +{ + int i; + for (i=0; i < count; ++i) { + int y_fixed = (y[i] << 20) + (1<<19); // rounding + int r,g,b; + int cr = pcr[i] - 128; + int cb = pcb[i] - 128; + r = y_fixed + cr* stbi__float2fixed(1.40200f); + g = y_fixed + (cr*-stbi__float2fixed(0.71414f)) + ((cb*-stbi__float2fixed(0.34414f)) & 0xffff0000); + b = y_fixed + cb* stbi__float2fixed(1.77200f); + r >>= 20; + g >>= 20; + b >>= 20; + if ((unsigned) r > 255) { if (r < 0) r = 0; else r = 255; } + if ((unsigned) g > 255) { if (g < 0) g = 0; else g = 255; } + if ((unsigned) b > 255) { if (b < 0) b = 0; else b = 255; } + out[0] = (stbi_uc)r; + out[1] = (stbi_uc)g; + out[2] = (stbi_uc)b; + out[3] = 255; + out += step; + } +} + +#if defined(STBI_SSE2) || defined(STBI_NEON) +static void stbi__YCbCr_to_RGB_simd(stbi_uc *out, stbi_uc const *y, stbi_uc const *pcb, stbi_uc const *pcr, int count, int step) +{ + int i = 0; + +#ifdef STBI_SSE2 + // step == 3 is pretty ugly on the final interleave, and i'm not convinced + // it's useful in practice (you wouldn't use it for textures, for example). + // so just accelerate step == 4 case. + if (step == 4) { + // this is a fairly straightforward implementation and not super-optimized. + __m128i signflip = _mm_set1_epi8(-0x80); + __m128i cr_const0 = _mm_set1_epi16( (short) ( 1.40200f*4096.0f+0.5f)); + __m128i cr_const1 = _mm_set1_epi16( - (short) ( 0.71414f*4096.0f+0.5f)); + __m128i cb_const0 = _mm_set1_epi16( - (short) ( 0.34414f*4096.0f+0.5f)); + __m128i cb_const1 = _mm_set1_epi16( (short) ( 1.77200f*4096.0f+0.5f)); + __m128i y_bias = _mm_set1_epi8((char) (unsigned char) 128); + __m128i xw = _mm_set1_epi16(255); // alpha channel + + for (; i+7 < count; i += 8) { + // load + __m128i y_bytes = _mm_loadl_epi64((__m128i *) (y+i)); + __m128i cr_bytes = _mm_loadl_epi64((__m128i *) (pcr+i)); + __m128i cb_bytes = _mm_loadl_epi64((__m128i *) (pcb+i)); + __m128i cr_biased = _mm_xor_si128(cr_bytes, signflip); // -128 + __m128i cb_biased = _mm_xor_si128(cb_bytes, signflip); // -128 + + // unpack to short (and left-shift cr, cb by 8) + __m128i yw = _mm_unpacklo_epi8(y_bias, y_bytes); + __m128i crw = _mm_unpacklo_epi8(_mm_setzero_si128(), cr_biased); + __m128i cbw = _mm_unpacklo_epi8(_mm_setzero_si128(), cb_biased); + + // color transform + __m128i yws = _mm_srli_epi16(yw, 4); + __m128i cr0 = _mm_mulhi_epi16(cr_const0, crw); + __m128i cb0 = _mm_mulhi_epi16(cb_const0, cbw); + __m128i cb1 = _mm_mulhi_epi16(cbw, cb_const1); + __m128i cr1 = _mm_mulhi_epi16(crw, cr_const1); + __m128i rws = _mm_add_epi16(cr0, yws); + __m128i gwt = _mm_add_epi16(cb0, yws); + __m128i bws = _mm_add_epi16(yws, cb1); + __m128i gws = _mm_add_epi16(gwt, cr1); + + // descale + __m128i rw = _mm_srai_epi16(rws, 4); + __m128i bw = _mm_srai_epi16(bws, 4); + __m128i gw = _mm_srai_epi16(gws, 4); + + // back to byte, set up for transpose + __m128i brb = _mm_packus_epi16(rw, bw); + __m128i gxb = _mm_packus_epi16(gw, xw); + + // transpose to interleave channels + __m128i t0 = _mm_unpacklo_epi8(brb, gxb); + __m128i t1 = _mm_unpackhi_epi8(brb, gxb); + __m128i o0 = _mm_unpacklo_epi16(t0, t1); + __m128i o1 = _mm_unpackhi_epi16(t0, t1); + + // store + _mm_storeu_si128((__m128i *) (out + 0), o0); + _mm_storeu_si128((__m128i *) (out + 16), o1); + out += 32; + } + } +#endif + +#ifdef STBI_NEON + // in this version, step=3 support would be easy to add. but is there demand? + if (step == 4) { + // this is a fairly straightforward implementation and not super-optimized. + uint8x8_t signflip = vdup_n_u8(0x80); + int16x8_t cr_const0 = vdupq_n_s16( (short) ( 1.40200f*4096.0f+0.5f)); + int16x8_t cr_const1 = vdupq_n_s16( - (short) ( 0.71414f*4096.0f+0.5f)); + int16x8_t cb_const0 = vdupq_n_s16( - (short) ( 0.34414f*4096.0f+0.5f)); + int16x8_t cb_const1 = vdupq_n_s16( (short) ( 1.77200f*4096.0f+0.5f)); + + for (; i+7 < count; i += 8) { + // load + uint8x8_t y_bytes = vld1_u8(y + i); + uint8x8_t cr_bytes = vld1_u8(pcr + i); + uint8x8_t cb_bytes = vld1_u8(pcb + i); + int8x8_t cr_biased = vreinterpret_s8_u8(vsub_u8(cr_bytes, signflip)); + int8x8_t cb_biased = vreinterpret_s8_u8(vsub_u8(cb_bytes, signflip)); + + // expand to s16 + int16x8_t yws = vreinterpretq_s16_u16(vshll_n_u8(y_bytes, 4)); + int16x8_t crw = vshll_n_s8(cr_biased, 7); + int16x8_t cbw = vshll_n_s8(cb_biased, 7); + + // color transform + int16x8_t cr0 = vqdmulhq_s16(crw, cr_const0); + int16x8_t cb0 = vqdmulhq_s16(cbw, cb_const0); + int16x8_t cr1 = vqdmulhq_s16(crw, cr_const1); + int16x8_t cb1 = vqdmulhq_s16(cbw, cb_const1); + int16x8_t rws = vaddq_s16(yws, cr0); + int16x8_t gws = vaddq_s16(vaddq_s16(yws, cb0), cr1); + int16x8_t bws = vaddq_s16(yws, cb1); + + // undo scaling, round, convert to byte + uint8x8x4_t o; + o.val[0] = vqrshrun_n_s16(rws, 4); + o.val[1] = vqrshrun_n_s16(gws, 4); + o.val[2] = vqrshrun_n_s16(bws, 4); + o.val[3] = vdup_n_u8(255); + + // store, interleaving r/g/b/a + vst4_u8(out, o); + out += 8*4; + } + } +#endif + + for (; i < count; ++i) { + int y_fixed = (y[i] << 20) + (1<<19); // rounding + int r,g,b; + int cr = pcr[i] - 128; + int cb = pcb[i] - 128; + r = y_fixed + cr* stbi__float2fixed(1.40200f); + g = y_fixed + cr*-stbi__float2fixed(0.71414f) + ((cb*-stbi__float2fixed(0.34414f)) & 0xffff0000); + b = y_fixed + cb* stbi__float2fixed(1.77200f); + r >>= 20; + g >>= 20; + b >>= 20; + if ((unsigned) r > 255) { if (r < 0) r = 0; else r = 255; } + if ((unsigned) g > 255) { if (g < 0) g = 0; else g = 255; } + if ((unsigned) b > 255) { if (b < 0) b = 0; else b = 255; } + out[0] = (stbi_uc)r; + out[1] = (stbi_uc)g; + out[2] = (stbi_uc)b; + out[3] = 255; + out += step; + } +} +#endif + +// set up the kernels +static void stbi__setup_jpeg(stbi__jpeg *j) +{ + j->idct_block_kernel = stbi__idct_block; + j->YCbCr_to_RGB_kernel = stbi__YCbCr_to_RGB_row; + j->resample_row_hv_2_kernel = stbi__resample_row_hv_2; + +#ifdef STBI_SSE2 + if (stbi__sse2_available()) { + j->idct_block_kernel = stbi__idct_simd; + j->YCbCr_to_RGB_kernel = stbi__YCbCr_to_RGB_simd; + j->resample_row_hv_2_kernel = stbi__resample_row_hv_2_simd; + } +#endif + +#ifdef STBI_NEON + j->idct_block_kernel = stbi__idct_simd; + j->YCbCr_to_RGB_kernel = stbi__YCbCr_to_RGB_simd; + j->resample_row_hv_2_kernel = stbi__resample_row_hv_2_simd; +#endif +} + +// clean up the temporary component buffers +static void stbi__cleanup_jpeg(stbi__jpeg *j) +{ + stbi__free_jpeg_components(j, j->s->img_n, 0); +} + +typedef struct +{ + resample_row_func resample; + stbi_uc *line0,*line1; + int hs,vs; // expansion factor in each axis + int w_lores; // horizontal pixels pre-expansion + int ystep; // how far through vertical expansion we are + int ypos; // which pre-expansion row we're on +} stbi__resample; + +// fast 0..255 * 0..255 => 0..255 rounded multiplication +static stbi_uc stbi__blinn_8x8(stbi_uc x, stbi_uc y) +{ + unsigned int t = x*y + 128; + return (stbi_uc) ((t + (t >>8)) >> 8); +} + +static stbi_uc *load_jpeg_image(stbi__jpeg *z, int *out_x, int *out_y, int *comp, int req_comp) +{ + int n, decode_n, is_rgb; + z->s->img_n = 0; // make stbi__cleanup_jpeg safe + + // validate req_comp + if (req_comp < 0 || req_comp > 4) return stbi__errpuc("bad req_comp", "Internal error"); + + // load a jpeg image from whichever source, but leave in YCbCr format + if (!stbi__decode_jpeg_image(z)) { stbi__cleanup_jpeg(z); return NULL; } + + // determine actual number of components to generate + n = req_comp ? req_comp : z->s->img_n >= 3 ? 3 : 1; + + is_rgb = z->s->img_n == 3 && (z->rgb == 3 || (z->app14_color_transform == 0 && !z->jfif)); + + if (z->s->img_n == 3 && n < 3 && !is_rgb) + decode_n = 1; + else + decode_n = z->s->img_n; + + // nothing to do if no components requested; check this now to avoid + // accessing uninitialized coutput[0] later + if (decode_n <= 0) { stbi__cleanup_jpeg(z); return NULL; } + + // resample and color-convert + { + int k; + unsigned int i,j; + stbi_uc *output; + stbi_uc *coutput[4] = { NULL, NULL, NULL, NULL }; + + stbi__resample res_comp[4]; + + for (k=0; k < decode_n; ++k) { + stbi__resample *r = &res_comp[k]; + + // allocate line buffer big enough for upsampling off the edges + // with upsample factor of 4 + z->img_comp[k].linebuf = (stbi_uc *) stbi__malloc(z->s->img_x + 3); + if (!z->img_comp[k].linebuf) { stbi__cleanup_jpeg(z); return stbi__errpuc("outofmem", "Out of memory"); } + + r->hs = z->img_h_max / z->img_comp[k].h; + r->vs = z->img_v_max / z->img_comp[k].v; + r->ystep = r->vs >> 1; + r->w_lores = (z->s->img_x + r->hs-1) / r->hs; + r->ypos = 0; + r->line0 = r->line1 = z->img_comp[k].data; + + if (r->hs == 1 && r->vs == 1) r->resample = resample_row_1; + else if (r->hs == 1 && r->vs == 2) r->resample = stbi__resample_row_v_2; + else if (r->hs == 2 && r->vs == 1) r->resample = stbi__resample_row_h_2; + else if (r->hs == 2 && r->vs == 2) r->resample = z->resample_row_hv_2_kernel; + else r->resample = stbi__resample_row_generic; + } + + // can't error after this so, this is safe + output = (stbi_uc *) stbi__malloc_mad3(n, z->s->img_x, z->s->img_y, 1); + if (!output) { stbi__cleanup_jpeg(z); return stbi__errpuc("outofmem", "Out of memory"); } + + // now go ahead and resample + for (j=0; j < z->s->img_y; ++j) { + stbi_uc *out = output + n * z->s->img_x * j; + for (k=0; k < decode_n; ++k) { + stbi__resample *r = &res_comp[k]; + int y_bot = r->ystep >= (r->vs >> 1); + coutput[k] = r->resample(z->img_comp[k].linebuf, + y_bot ? r->line1 : r->line0, + y_bot ? r->line0 : r->line1, + r->w_lores, r->hs); + if (++r->ystep >= r->vs) { + r->ystep = 0; + r->line0 = r->line1; + if (++r->ypos < z->img_comp[k].y) + r->line1 += z->img_comp[k].w2; + } + } + if (n >= 3) { + stbi_uc *y = coutput[0]; + if (z->s->img_n == 3) { + if (is_rgb) { + for (i=0; i < z->s->img_x; ++i) { + out[0] = y[i]; + out[1] = coutput[1][i]; + out[2] = coutput[2][i]; + out[3] = 255; + out += n; + } + } else { + z->YCbCr_to_RGB_kernel(out, y, coutput[1], coutput[2], z->s->img_x, n); + } + } else if (z->s->img_n == 4) { + if (z->app14_color_transform == 0) { // CMYK + for (i=0; i < z->s->img_x; ++i) { + stbi_uc m = coutput[3][i]; + out[0] = stbi__blinn_8x8(coutput[0][i], m); + out[1] = stbi__blinn_8x8(coutput[1][i], m); + out[2] = stbi__blinn_8x8(coutput[2][i], m); + out[3] = 255; + out += n; + } + } else if (z->app14_color_transform == 2) { // YCCK + z->YCbCr_to_RGB_kernel(out, y, coutput[1], coutput[2], z->s->img_x, n); + for (i=0; i < z->s->img_x; ++i) { + stbi_uc m = coutput[3][i]; + out[0] = stbi__blinn_8x8(255 - out[0], m); + out[1] = stbi__blinn_8x8(255 - out[1], m); + out[2] = stbi__blinn_8x8(255 - out[2], m); + out += n; + } + } else { // YCbCr + alpha? Ignore the fourth channel for now + z->YCbCr_to_RGB_kernel(out, y, coutput[1], coutput[2], z->s->img_x, n); + } + } else + for (i=0; i < z->s->img_x; ++i) { + out[0] = out[1] = out[2] = y[i]; + out[3] = 255; // not used if n==3 + out += n; + } + } else { + if (is_rgb) { + if (n == 1) + for (i=0; i < z->s->img_x; ++i) + *out++ = stbi__compute_y(coutput[0][i], coutput[1][i], coutput[2][i]); + else { + for (i=0; i < z->s->img_x; ++i, out += 2) { + out[0] = stbi__compute_y(coutput[0][i], coutput[1][i], coutput[2][i]); + out[1] = 255; + } + } + } else if (z->s->img_n == 4 && z->app14_color_transform == 0) { + for (i=0; i < z->s->img_x; ++i) { + stbi_uc m = coutput[3][i]; + stbi_uc r = stbi__blinn_8x8(coutput[0][i], m); + stbi_uc g = stbi__blinn_8x8(coutput[1][i], m); + stbi_uc b = stbi__blinn_8x8(coutput[2][i], m); + out[0] = stbi__compute_y(r, g, b); + out[1] = 255; + out += n; + } + } else if (z->s->img_n == 4 && z->app14_color_transform == 2) { + for (i=0; i < z->s->img_x; ++i) { + out[0] = stbi__blinn_8x8(255 - coutput[0][i], coutput[3][i]); + out[1] = 255; + out += n; + } + } else { + stbi_uc *y = coutput[0]; + if (n == 1) + for (i=0; i < z->s->img_x; ++i) out[i] = y[i]; + else + for (i=0; i < z->s->img_x; ++i) { *out++ = y[i]; *out++ = 255; } + } + } + } + stbi__cleanup_jpeg(z); + *out_x = z->s->img_x; + *out_y = z->s->img_y; + if (comp) *comp = z->s->img_n >= 3 ? 3 : 1; // report original components, not output + return output; + } +} + +static void *stbi__jpeg_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri) +{ + unsigned char* result; + stbi__jpeg* j = (stbi__jpeg*) stbi__malloc(sizeof(stbi__jpeg)); + if (!j) return stbi__errpuc("outofmem", "Out of memory"); + memset(j, 0, sizeof(stbi__jpeg)); + STBI_NOTUSED(ri); + j->s = s; + stbi__setup_jpeg(j); + result = load_jpeg_image(j, x,y,comp,req_comp); + STBI_FREE(j); + return result; +} + +static int stbi__jpeg_test(stbi__context *s) +{ + int r; + stbi__jpeg* j = (stbi__jpeg*)stbi__malloc(sizeof(stbi__jpeg)); + if (!j) return stbi__err("outofmem", "Out of memory"); + memset(j, 0, sizeof(stbi__jpeg)); + j->s = s; + stbi__setup_jpeg(j); + r = stbi__decode_jpeg_header(j, STBI__SCAN_type); + stbi__rewind(s); + STBI_FREE(j); + return r; +} + +static int stbi__jpeg_info_raw(stbi__jpeg *j, int *x, int *y, int *comp) +{ + if (!stbi__decode_jpeg_header(j, STBI__SCAN_header)) { + stbi__rewind( j->s ); + return 0; + } + if (x) *x = j->s->img_x; + if (y) *y = j->s->img_y; + if (comp) *comp = j->s->img_n >= 3 ? 3 : 1; + return 1; +} + +static int stbi__jpeg_info(stbi__context *s, int *x, int *y, int *comp) +{ + int result; + stbi__jpeg* j = (stbi__jpeg*) (stbi__malloc(sizeof(stbi__jpeg))); + if (!j) return stbi__err("outofmem", "Out of memory"); + memset(j, 0, sizeof(stbi__jpeg)); + j->s = s; + result = stbi__jpeg_info_raw(j, x, y, comp); + STBI_FREE(j); + return result; +} +#endif + +// public domain zlib decode v0.2 Sean Barrett 2006-11-18 +// simple implementation +// - all input must be provided in an upfront buffer +// - all output is written to a single output buffer (can malloc/realloc) +// performance +// - fast huffman + +#ifndef STBI_NO_ZLIB + +// fast-way is faster to check than jpeg huffman, but slow way is slower +#define STBI__ZFAST_BITS 9 // accelerate all cases in default tables +#define STBI__ZFAST_MASK ((1 << STBI__ZFAST_BITS) - 1) +#define STBI__ZNSYMS 288 // number of symbols in literal/length alphabet + +// zlib-style huffman encoding +// (jpegs packs from left, zlib from right, so can't share code) +typedef struct +{ + stbi__uint16 fast[1 << STBI__ZFAST_BITS]; + stbi__uint16 firstcode[16]; + int maxcode[17]; + stbi__uint16 firstsymbol[16]; + stbi_uc size[STBI__ZNSYMS]; + stbi__uint16 value[STBI__ZNSYMS]; +} stbi__zhuffman; + +stbi_inline static int stbi__bitreverse16(int n) +{ + n = ((n & 0xAAAA) >> 1) | ((n & 0x5555) << 1); + n = ((n & 0xCCCC) >> 2) | ((n & 0x3333) << 2); + n = ((n & 0xF0F0) >> 4) | ((n & 0x0F0F) << 4); + n = ((n & 0xFF00) >> 8) | ((n & 0x00FF) << 8); + return n; +} + +stbi_inline static int stbi__bit_reverse(int v, int bits) +{ + STBI_ASSERT(bits <= 16); + // to bit reverse n bits, reverse 16 and shift + // e.g. 11 bits, bit reverse and shift away 5 + return stbi__bitreverse16(v) >> (16-bits); +} + +static int stbi__zbuild_huffman(stbi__zhuffman *z, const stbi_uc *sizelist, int num) +{ + int i,k=0; + int code, next_code[16], sizes[17]; + + // DEFLATE spec for generating codes + memset(sizes, 0, sizeof(sizes)); + memset(z->fast, 0, sizeof(z->fast)); + for (i=0; i < num; ++i) + ++sizes[sizelist[i]]; + sizes[0] = 0; + for (i=1; i < 16; ++i) + if (sizes[i] > (1 << i)) + return stbi__err("bad sizes", "Corrupt PNG"); + code = 0; + for (i=1; i < 16; ++i) { + next_code[i] = code; + z->firstcode[i] = (stbi__uint16) code; + z->firstsymbol[i] = (stbi__uint16) k; + code = (code + sizes[i]); + if (sizes[i]) + if (code-1 >= (1 << i)) return stbi__err("bad codelengths","Corrupt PNG"); + z->maxcode[i] = code << (16-i); // preshift for inner loop + code <<= 1; + k += sizes[i]; + } + z->maxcode[16] = 0x10000; // sentinel + for (i=0; i < num; ++i) { + int s = sizelist[i]; + if (s) { + int c = next_code[s] - z->firstcode[s] + z->firstsymbol[s]; + stbi__uint16 fastv = (stbi__uint16) ((s << 9) | i); + z->size [c] = (stbi_uc ) s; + z->value[c] = (stbi__uint16) i; + if (s <= STBI__ZFAST_BITS) { + int j = stbi__bit_reverse(next_code[s],s); + while (j < (1 << STBI__ZFAST_BITS)) { + z->fast[j] = fastv; + j += (1 << s); + } + } + ++next_code[s]; + } + } + return 1; +} + +// zlib-from-memory implementation for PNG reading +// because PNG allows splitting the zlib stream arbitrarily, +// and it's annoying structurally to have PNG call ZLIB call PNG, +// we require PNG read all the IDATs and combine them into a single +// memory buffer + +typedef struct +{ + stbi_uc *zbuffer, *zbuffer_end; + int num_bits; + int hit_zeof_once; + stbi__uint32 code_buffer; + + char *zout; + char *zout_start; + char *zout_end; + int z_expandable; + + stbi__zhuffman z_length, z_distance; +} stbi__zbuf; + +stbi_inline static int stbi__zeof(stbi__zbuf *z) +{ + return (z->zbuffer >= z->zbuffer_end); +} + +stbi_inline static stbi_uc stbi__zget8(stbi__zbuf *z) +{ + return stbi__zeof(z) ? 0 : *z->zbuffer++; +} + +static void stbi__fill_bits(stbi__zbuf *z) +{ + do { + if (z->code_buffer >= (1U << z->num_bits)) { + z->zbuffer = z->zbuffer_end; /* treat this as EOF so we fail. */ + return; + } + z->code_buffer |= (unsigned int) stbi__zget8(z) << z->num_bits; + z->num_bits += 8; + } while (z->num_bits <= 24); +} + +stbi_inline static unsigned int stbi__zreceive(stbi__zbuf *z, int n) +{ + unsigned int k; + if (z->num_bits < n) stbi__fill_bits(z); + k = z->code_buffer & ((1 << n) - 1); + z->code_buffer >>= n; + z->num_bits -= n; + return k; +} + +static int stbi__zhuffman_decode_slowpath(stbi__zbuf *a, stbi__zhuffman *z) +{ + int b,s,k; + // not resolved by fast table, so compute it the slow way + // use jpeg approach, which requires MSbits at top + k = stbi__bit_reverse(a->code_buffer, 16); + for (s=STBI__ZFAST_BITS+1; ; ++s) + if (k < z->maxcode[s]) + break; + if (s >= 16) return -1; // invalid code! + // code size is s, so: + b = (k >> (16-s)) - z->firstcode[s] + z->firstsymbol[s]; + if (b >= STBI__ZNSYMS) return -1; // some data was corrupt somewhere! + if (z->size[b] != s) return -1; // was originally an assert, but report failure instead. + a->code_buffer >>= s; + a->num_bits -= s; + return z->value[b]; +} + +stbi_inline static int stbi__zhuffman_decode(stbi__zbuf *a, stbi__zhuffman *z) +{ + int b,s; + if (a->num_bits < 16) { + if (stbi__zeof(a)) { + if (!a->hit_zeof_once) { + // This is the first time we hit eof, insert 16 extra padding btis + // to allow us to keep going; if we actually consume any of them + // though, that is invalid data. This is caught later. + a->hit_zeof_once = 1; + a->num_bits += 16; // add 16 implicit zero bits + } else { + // We already inserted our extra 16 padding bits and are again + // out, this stream is actually prematurely terminated. + return -1; + } + } else { + stbi__fill_bits(a); + } + } + b = z->fast[a->code_buffer & STBI__ZFAST_MASK]; + if (b) { + s = b >> 9; + a->code_buffer >>= s; + a->num_bits -= s; + return b & 511; + } + return stbi__zhuffman_decode_slowpath(a, z); +} + +static int stbi__zexpand(stbi__zbuf *z, char *zout, int n) // need to make room for n bytes +{ + char *q; + unsigned int cur, limit, old_limit; + z->zout = zout; + if (!z->z_expandable) return stbi__err("output buffer limit","Corrupt PNG"); + cur = (unsigned int) (z->zout - z->zout_start); + limit = old_limit = (unsigned) (z->zout_end - z->zout_start); + if (UINT_MAX - cur < (unsigned) n) return stbi__err("outofmem", "Out of memory"); + while (cur + n > limit) { + if(limit > UINT_MAX / 2) return stbi__err("outofmem", "Out of memory"); + limit *= 2; + } + q = (char *) STBI_REALLOC_SIZED(z->zout_start, old_limit, limit); + STBI_NOTUSED(old_limit); + if (q == NULL) return stbi__err("outofmem", "Out of memory"); + z->zout_start = q; + z->zout = q + cur; + z->zout_end = q + limit; + return 1; +} + +static const int stbi__zlength_base[31] = { + 3,4,5,6,7,8,9,10,11,13, + 15,17,19,23,27,31,35,43,51,59, + 67,83,99,115,131,163,195,227,258,0,0 }; + +static const int stbi__zlength_extra[31]= +{ 0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0,0,0 }; + +static const int stbi__zdist_base[32] = { 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193, +257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577,0,0}; + +static const int stbi__zdist_extra[32] = +{ 0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13}; + +static int stbi__parse_huffman_block(stbi__zbuf *a) +{ + char *zout = a->zout; + for(;;) { + int z = stbi__zhuffman_decode(a, &a->z_length); + if (z < 256) { + if (z < 0) return stbi__err("bad huffman code","Corrupt PNG"); // error in huffman codes + if (zout >= a->zout_end) { + if (!stbi__zexpand(a, zout, 1)) return 0; + zout = a->zout; + } + *zout++ = (char) z; + } else { + stbi_uc *p; + int len,dist; + if (z == 256) { + a->zout = zout; + if (a->hit_zeof_once && a->num_bits < 16) { + // The first time we hit zeof, we inserted 16 extra zero bits into our bit + // buffer so the decoder can just do its speculative decoding. But if we + // actually consumed any of those bits (which is the case when num_bits < 16), + // the stream actually read past the end so it is malformed. + return stbi__err("unexpected end","Corrupt PNG"); + } + return 1; + } + if (z >= 286) return stbi__err("bad huffman code","Corrupt PNG"); // per DEFLATE, length codes 286 and 287 must not appear in compressed data + z -= 257; + len = stbi__zlength_base[z]; + if (stbi__zlength_extra[z]) len += stbi__zreceive(a, stbi__zlength_extra[z]); + z = stbi__zhuffman_decode(a, &a->z_distance); + if (z < 0 || z >= 30) return stbi__err("bad huffman code","Corrupt PNG"); // per DEFLATE, distance codes 30 and 31 must not appear in compressed data + dist = stbi__zdist_base[z]; + if (stbi__zdist_extra[z]) dist += stbi__zreceive(a, stbi__zdist_extra[z]); + if (zout - a->zout_start < dist) return stbi__err("bad dist","Corrupt PNG"); + if (len > a->zout_end - zout) { + if (!stbi__zexpand(a, zout, len)) return 0; + zout = a->zout; + } + p = (stbi_uc *) (zout - dist); + if (dist == 1) { // run of one byte; common in images. + stbi_uc v = *p; + if (len) { do *zout++ = v; while (--len); } + } else { + if (len) { do *zout++ = *p++; while (--len); } + } + } + } +} + +static int stbi__compute_huffman_codes(stbi__zbuf *a) +{ + static const stbi_uc length_dezigzag[19] = { 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15 }; + stbi__zhuffman z_codelength; + stbi_uc lencodes[286+32+137];//padding for maximum single op + stbi_uc codelength_sizes[19]; + int i,n; + + int hlit = stbi__zreceive(a,5) + 257; + int hdist = stbi__zreceive(a,5) + 1; + int hclen = stbi__zreceive(a,4) + 4; + int ntot = hlit + hdist; + + memset(codelength_sizes, 0, sizeof(codelength_sizes)); + for (i=0; i < hclen; ++i) { + int s = stbi__zreceive(a,3); + codelength_sizes[length_dezigzag[i]] = (stbi_uc) s; + } + if (!stbi__zbuild_huffman(&z_codelength, codelength_sizes, 19)) return 0; + + n = 0; + while (n < ntot) { + int c = stbi__zhuffman_decode(a, &z_codelength); + if (c < 0 || c >= 19) return stbi__err("bad codelengths", "Corrupt PNG"); + if (c < 16) + lencodes[n++] = (stbi_uc) c; + else { + stbi_uc fill = 0; + if (c == 16) { + c = stbi__zreceive(a,2)+3; + if (n == 0) return stbi__err("bad codelengths", "Corrupt PNG"); + fill = lencodes[n-1]; + } else if (c == 17) { + c = stbi__zreceive(a,3)+3; + } else if (c == 18) { + c = stbi__zreceive(a,7)+11; + } else { + return stbi__err("bad codelengths", "Corrupt PNG"); + } + if (ntot - n < c) return stbi__err("bad codelengths", "Corrupt PNG"); + memset(lencodes+n, fill, c); + n += c; + } + } + if (n != ntot) return stbi__err("bad codelengths","Corrupt PNG"); + if (!stbi__zbuild_huffman(&a->z_length, lencodes, hlit)) return 0; + if (!stbi__zbuild_huffman(&a->z_distance, lencodes+hlit, hdist)) return 0; + return 1; +} + +static int stbi__parse_uncompressed_block(stbi__zbuf *a) +{ + stbi_uc header[4]; + int len,nlen,k; + if (a->num_bits & 7) + stbi__zreceive(a, a->num_bits & 7); // discard + // drain the bit-packed data into header + k = 0; + while (a->num_bits > 0) { + header[k++] = (stbi_uc) (a->code_buffer & 255); // suppress MSVC run-time check + a->code_buffer >>= 8; + a->num_bits -= 8; + } + if (a->num_bits < 0) return stbi__err("zlib corrupt","Corrupt PNG"); + // now fill header the normal way + while (k < 4) + header[k++] = stbi__zget8(a); + len = header[1] * 256 + header[0]; + nlen = header[3] * 256 + header[2]; + if (nlen != (len ^ 0xffff)) return stbi__err("zlib corrupt","Corrupt PNG"); + if (a->zbuffer + len > a->zbuffer_end) return stbi__err("read past buffer","Corrupt PNG"); + if (a->zout + len > a->zout_end) + if (!stbi__zexpand(a, a->zout, len)) return 0; + memcpy(a->zout, a->zbuffer, len); + a->zbuffer += len; + a->zout += len; + return 1; +} + +static int stbi__parse_zlib_header(stbi__zbuf *a) +{ + int cmf = stbi__zget8(a); + int cm = cmf & 15; + /* int cinfo = cmf >> 4; */ + int flg = stbi__zget8(a); + if (stbi__zeof(a)) return stbi__err("bad zlib header","Corrupt PNG"); // zlib spec + if ((cmf*256+flg) % 31 != 0) return stbi__err("bad zlib header","Corrupt PNG"); // zlib spec + if (flg & 32) return stbi__err("no preset dict","Corrupt PNG"); // preset dictionary not allowed in png + if (cm != 8) return stbi__err("bad compression","Corrupt PNG"); // DEFLATE required for png + // window = 1 << (8 + cinfo)... but who cares, we fully buffer output + return 1; +} + +static const stbi_uc stbi__zdefault_length[STBI__ZNSYMS] = +{ + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, + 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, + 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, + 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8 +}; +static const stbi_uc stbi__zdefault_distance[32] = +{ + 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5 +}; +/* +Init algorithm: +{ + int i; // use <= to match clearly with spec + for (i=0; i <= 143; ++i) stbi__zdefault_length[i] = 8; + for ( ; i <= 255; ++i) stbi__zdefault_length[i] = 9; + for ( ; i <= 279; ++i) stbi__zdefault_length[i] = 7; + for ( ; i <= 287; ++i) stbi__zdefault_length[i] = 8; + + for (i=0; i <= 31; ++i) stbi__zdefault_distance[i] = 5; +} +*/ + +static int stbi__parse_zlib(stbi__zbuf *a, int parse_header) +{ + int final, type; + if (parse_header) + if (!stbi__parse_zlib_header(a)) return 0; + a->num_bits = 0; + a->code_buffer = 0; + a->hit_zeof_once = 0; + do { + final = stbi__zreceive(a,1); + type = stbi__zreceive(a,2); + if (type == 0) { + if (!stbi__parse_uncompressed_block(a)) return 0; + } else if (type == 3) { + return 0; + } else { + if (type == 1) { + // use fixed code lengths + if (!stbi__zbuild_huffman(&a->z_length , stbi__zdefault_length , STBI__ZNSYMS)) return 0; + if (!stbi__zbuild_huffman(&a->z_distance, stbi__zdefault_distance, 32)) return 0; + } else { + if (!stbi__compute_huffman_codes(a)) return 0; + } + if (!stbi__parse_huffman_block(a)) return 0; + } + } while (!final); + return 1; +} + +static int stbi__do_zlib(stbi__zbuf *a, char *obuf, int olen, int exp, int parse_header) +{ + a->zout_start = obuf; + a->zout = obuf; + a->zout_end = obuf + olen; + a->z_expandable = exp; + + return stbi__parse_zlib(a, parse_header); +} + +STBIDEF char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen) +{ + stbi__zbuf a; + char *p = (char *) stbi__malloc(initial_size); + if (p == NULL) return NULL; + a.zbuffer = (stbi_uc *) buffer; + a.zbuffer_end = (stbi_uc *) buffer + len; + if (stbi__do_zlib(&a, p, initial_size, 1, 1)) { + if (outlen) *outlen = (int) (a.zout - a.zout_start); + return a.zout_start; + } else { + STBI_FREE(a.zout_start); + return NULL; + } +} + +STBIDEF char *stbi_zlib_decode_malloc(char const *buffer, int len, int *outlen) +{ + return stbi_zlib_decode_malloc_guesssize(buffer, len, 16384, outlen); +} + +STBIDEF char *stbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header) +{ + stbi__zbuf a; + char *p = (char *) stbi__malloc(initial_size); + if (p == NULL) return NULL; + a.zbuffer = (stbi_uc *) buffer; + a.zbuffer_end = (stbi_uc *) buffer + len; + if (stbi__do_zlib(&a, p, initial_size, 1, parse_header)) { + if (outlen) *outlen = (int) (a.zout - a.zout_start); + return a.zout_start; + } else { + STBI_FREE(a.zout_start); + return NULL; + } +} + +STBIDEF int stbi_zlib_decode_buffer(char *obuffer, int olen, char const *ibuffer, int ilen) +{ + stbi__zbuf a; + a.zbuffer = (stbi_uc *) ibuffer; + a.zbuffer_end = (stbi_uc *) ibuffer + ilen; + if (stbi__do_zlib(&a, obuffer, olen, 0, 1)) + return (int) (a.zout - a.zout_start); + else + return -1; +} + +STBIDEF char *stbi_zlib_decode_noheader_malloc(char const *buffer, int len, int *outlen) +{ + stbi__zbuf a; + char *p = (char *) stbi__malloc(16384); + if (p == NULL) return NULL; + a.zbuffer = (stbi_uc *) buffer; + a.zbuffer_end = (stbi_uc *) buffer+len; + if (stbi__do_zlib(&a, p, 16384, 1, 0)) { + if (outlen) *outlen = (int) (a.zout - a.zout_start); + return a.zout_start; + } else { + STBI_FREE(a.zout_start); + return NULL; + } +} + +STBIDEF int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen) +{ + stbi__zbuf a; + a.zbuffer = (stbi_uc *) ibuffer; + a.zbuffer_end = (stbi_uc *) ibuffer + ilen; + if (stbi__do_zlib(&a, obuffer, olen, 0, 0)) + return (int) (a.zout - a.zout_start); + else + return -1; +} +#endif + +// public domain "baseline" PNG decoder v0.10 Sean Barrett 2006-11-18 +// simple implementation +// - only 8-bit samples +// - no CRC checking +// - allocates lots of intermediate memory +// - avoids problem of streaming data between subsystems +// - avoids explicit window management +// performance +// - uses stb_zlib, a PD zlib implementation with fast huffman decoding + +#ifndef STBI_NO_PNG +typedef struct +{ + stbi__uint32 length; + stbi__uint32 type; +} stbi__pngchunk; + +static stbi__pngchunk stbi__get_chunk_header(stbi__context *s) +{ + stbi__pngchunk c; + c.length = stbi__get32be(s); + c.type = stbi__get32be(s); + return c; +} + +static int stbi__check_png_header(stbi__context *s) +{ + static const stbi_uc png_sig[8] = { 137,80,78,71,13,10,26,10 }; + int i; + for (i=0; i < 8; ++i) + if (stbi__get8(s) != png_sig[i]) return stbi__err("bad png sig","Not a PNG"); + return 1; +} + +typedef struct +{ + stbi__context *s; + stbi_uc *idata, *expanded, *out; + int depth; +} stbi__png; + + +enum { + STBI__F_none=0, + STBI__F_sub=1, + STBI__F_up=2, + STBI__F_avg=3, + STBI__F_paeth=4, + // synthetic filter used for first scanline to avoid needing a dummy row of 0s + STBI__F_avg_first +}; + +static stbi_uc first_row_filter[5] = +{ + STBI__F_none, + STBI__F_sub, + STBI__F_none, + STBI__F_avg_first, + STBI__F_sub // Paeth with b=c=0 turns out to be equivalent to sub +}; + +static int stbi__paeth(int a, int b, int c) +{ + // This formulation looks very different from the reference in the PNG spec, but is + // actually equivalent and has favorable data dependencies and admits straightforward + // generation of branch-free code, which helps performance significantly. + int thresh = c*3 - (a + b); + int lo = a < b ? a : b; + int hi = a < b ? b : a; + int t0 = (hi <= thresh) ? lo : c; + int t1 = (thresh <= lo) ? hi : t0; + return t1; +} + +static const stbi_uc stbi__depth_scale_table[9] = { 0, 0xff, 0x55, 0, 0x11, 0,0,0, 0x01 }; + +// adds an extra all-255 alpha channel +// dest == src is legal +// img_n must be 1 or 3 +static void stbi__create_png_alpha_expand8(stbi_uc *dest, stbi_uc *src, stbi__uint32 x, int img_n) +{ + int i; + // must process data backwards since we allow dest==src + if (img_n == 1) { + for (i=x-1; i >= 0; --i) { + dest[i*2+1] = 255; + dest[i*2+0] = src[i]; + } + } else { + STBI_ASSERT(img_n == 3); + for (i=x-1; i >= 0; --i) { + dest[i*4+3] = 255; + dest[i*4+2] = src[i*3+2]; + dest[i*4+1] = src[i*3+1]; + dest[i*4+0] = src[i*3+0]; + } + } +} + +// create the png data from post-deflated data +static int stbi__create_png_image_raw(stbi__png *a, stbi_uc *raw, stbi__uint32 raw_len, int out_n, stbi__uint32 x, stbi__uint32 y, int depth, int color) +{ + int bytes = (depth == 16 ? 2 : 1); + stbi__context *s = a->s; + stbi__uint32 i,j,stride = x*out_n*bytes; + stbi__uint32 img_len, img_width_bytes; + stbi_uc *filter_buf; + int all_ok = 1; + int k; + int img_n = s->img_n; // copy it into a local for later + + int output_bytes = out_n*bytes; + int filter_bytes = img_n*bytes; + int width = x; + + STBI_ASSERT(out_n == s->img_n || out_n == s->img_n+1); + a->out = (stbi_uc *) stbi__malloc_mad3(x, y, output_bytes, 0); // extra bytes to write off the end into + if (!a->out) return stbi__err("outofmem", "Out of memory"); + + // note: error exits here don't need to clean up a->out individually, + // stbi__do_png always does on error. + if (!stbi__mad3sizes_valid(img_n, x, depth, 7)) return stbi__err("too large", "Corrupt PNG"); + img_width_bytes = (((img_n * x * depth) + 7) >> 3); + if (!stbi__mad2sizes_valid(img_width_bytes, y, img_width_bytes)) return stbi__err("too large", "Corrupt PNG"); + img_len = (img_width_bytes + 1) * y; + + // we used to check for exact match between raw_len and img_len on non-interlaced PNGs, + // but issue #276 reported a PNG in the wild that had extra data at the end (all zeros), + // so just check for raw_len < img_len always. + if (raw_len < img_len) return stbi__err("not enough pixels","Corrupt PNG"); + + // Allocate two scan lines worth of filter workspace buffer. + filter_buf = (stbi_uc *) stbi__malloc_mad2(img_width_bytes, 2, 0); + if (!filter_buf) return stbi__err("outofmem", "Out of memory"); + + // Filtering for low-bit-depth images + if (depth < 8) { + filter_bytes = 1; + width = img_width_bytes; + } + + for (j=0; j < y; ++j) { + // cur/prior filter buffers alternate + stbi_uc *cur = filter_buf + (j & 1)*img_width_bytes; + stbi_uc *prior = filter_buf + (~j & 1)*img_width_bytes; + stbi_uc *dest = a->out + stride*j; + int nk = width * filter_bytes; + int filter = *raw++; + + // check filter type + if (filter > 4) { + all_ok = stbi__err("invalid filter","Corrupt PNG"); + break; + } + + // if first row, use special filter that doesn't sample previous row + if (j == 0) filter = first_row_filter[filter]; + + // perform actual filtering + switch (filter) { + case STBI__F_none: + memcpy(cur, raw, nk); + break; + case STBI__F_sub: + memcpy(cur, raw, filter_bytes); + for (k = filter_bytes; k < nk; ++k) + cur[k] = STBI__BYTECAST(raw[k] + cur[k-filter_bytes]); + break; + case STBI__F_up: + for (k = 0; k < nk; ++k) + cur[k] = STBI__BYTECAST(raw[k] + prior[k]); + break; + case STBI__F_avg: + for (k = 0; k < filter_bytes; ++k) + cur[k] = STBI__BYTECAST(raw[k] + (prior[k]>>1)); + for (k = filter_bytes; k < nk; ++k) + cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k-filter_bytes])>>1)); + break; + case STBI__F_paeth: + for (k = 0; k < filter_bytes; ++k) + cur[k] = STBI__BYTECAST(raw[k] + prior[k]); // prior[k] == stbi__paeth(0,prior[k],0) + for (k = filter_bytes; k < nk; ++k) + cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-filter_bytes], prior[k], prior[k-filter_bytes])); + break; + case STBI__F_avg_first: + memcpy(cur, raw, filter_bytes); + for (k = filter_bytes; k < nk; ++k) + cur[k] = STBI__BYTECAST(raw[k] + (cur[k-filter_bytes] >> 1)); + break; + } + + raw += nk; + + // expand decoded bits in cur to dest, also adding an extra alpha channel if desired + if (depth < 8) { + stbi_uc scale = (color == 0) ? stbi__depth_scale_table[depth] : 1; // scale grayscale values to 0..255 range + stbi_uc *in = cur; + stbi_uc *out = dest; + stbi_uc inb = 0; + stbi__uint32 nsmp = x*img_n; + + // expand bits to bytes first + if (depth == 4) { + for (i=0; i < nsmp; ++i) { + if ((i & 1) == 0) inb = *in++; + *out++ = scale * (inb >> 4); + inb <<= 4; + } + } else if (depth == 2) { + for (i=0; i < nsmp; ++i) { + if ((i & 3) == 0) inb = *in++; + *out++ = scale * (inb >> 6); + inb <<= 2; + } + } else { + STBI_ASSERT(depth == 1); + for (i=0; i < nsmp; ++i) { + if ((i & 7) == 0) inb = *in++; + *out++ = scale * (inb >> 7); + inb <<= 1; + } + } + + // insert alpha=255 values if desired + if (img_n != out_n) + stbi__create_png_alpha_expand8(dest, dest, x, img_n); + } else if (depth == 8) { + if (img_n == out_n) + memcpy(dest, cur, x*img_n); + else + stbi__create_png_alpha_expand8(dest, cur, x, img_n); + } else if (depth == 16) { + // convert the image data from big-endian to platform-native + stbi__uint16 *dest16 = (stbi__uint16*)dest; + stbi__uint32 nsmp = x*img_n; + + if (img_n == out_n) { + for (i = 0; i < nsmp; ++i, ++dest16, cur += 2) + *dest16 = (cur[0] << 8) | cur[1]; + } else { + STBI_ASSERT(img_n+1 == out_n); + if (img_n == 1) { + for (i = 0; i < x; ++i, dest16 += 2, cur += 2) { + dest16[0] = (cur[0] << 8) | cur[1]; + dest16[1] = 0xffff; + } + } else { + STBI_ASSERT(img_n == 3); + for (i = 0; i < x; ++i, dest16 += 4, cur += 6) { + dest16[0] = (cur[0] << 8) | cur[1]; + dest16[1] = (cur[2] << 8) | cur[3]; + dest16[2] = (cur[4] << 8) | cur[5]; + dest16[3] = 0xffff; + } + } + } + } + } + + STBI_FREE(filter_buf); + if (!all_ok) return 0; + + return 1; +} + +static int stbi__create_png_image(stbi__png *a, stbi_uc *image_data, stbi__uint32 image_data_len, int out_n, int depth, int color, int interlaced) +{ + int bytes = (depth == 16 ? 2 : 1); + int out_bytes = out_n * bytes; + stbi_uc *final; + int p; + if (!interlaced) + return stbi__create_png_image_raw(a, image_data, image_data_len, out_n, a->s->img_x, a->s->img_y, depth, color); + + // de-interlacing + final = (stbi_uc *) stbi__malloc_mad3(a->s->img_x, a->s->img_y, out_bytes, 0); + if (!final) return stbi__err("outofmem", "Out of memory"); + for (p=0; p < 7; ++p) { + int xorig[] = { 0,4,0,2,0,1,0 }; + int yorig[] = { 0,0,4,0,2,0,1 }; + int xspc[] = { 8,8,4,4,2,2,1 }; + int yspc[] = { 8,8,8,4,4,2,2 }; + int i,j,x,y; + // pass1_x[4] = 0, pass1_x[5] = 1, pass1_x[12] = 1 + x = (a->s->img_x - xorig[p] + xspc[p]-1) / xspc[p]; + y = (a->s->img_y - yorig[p] + yspc[p]-1) / yspc[p]; + if (x && y) { + stbi__uint32 img_len = ((((a->s->img_n * x * depth) + 7) >> 3) + 1) * y; + if (!stbi__create_png_image_raw(a, image_data, image_data_len, out_n, x, y, depth, color)) { + STBI_FREE(final); + return 0; + } + for (j=0; j < y; ++j) { + for (i=0; i < x; ++i) { + int out_y = j*yspc[p]+yorig[p]; + int out_x = i*xspc[p]+xorig[p]; + memcpy(final + out_y*a->s->img_x*out_bytes + out_x*out_bytes, + a->out + (j*x+i)*out_bytes, out_bytes); + } + } + STBI_FREE(a->out); + image_data += img_len; + image_data_len -= img_len; + } + } + a->out = final; + + return 1; +} + +static int stbi__compute_transparency(stbi__png *z, stbi_uc tc[3], int out_n) +{ + stbi__context *s = z->s; + stbi__uint32 i, pixel_count = s->img_x * s->img_y; + stbi_uc *p = z->out; + + // compute color-based transparency, assuming we've + // already got 255 as the alpha value in the output + STBI_ASSERT(out_n == 2 || out_n == 4); + + if (out_n == 2) { + for (i=0; i < pixel_count; ++i) { + p[1] = (p[0] == tc[0] ? 0 : 255); + p += 2; + } + } else { + for (i=0; i < pixel_count; ++i) { + if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2]) + p[3] = 0; + p += 4; + } + } + return 1; +} + +static int stbi__compute_transparency16(stbi__png *z, stbi__uint16 tc[3], int out_n) +{ + stbi__context *s = z->s; + stbi__uint32 i, pixel_count = s->img_x * s->img_y; + stbi__uint16 *p = (stbi__uint16*) z->out; + + // compute color-based transparency, assuming we've + // already got 65535 as the alpha value in the output + STBI_ASSERT(out_n == 2 || out_n == 4); + + if (out_n == 2) { + for (i = 0; i < pixel_count; ++i) { + p[1] = (p[0] == tc[0] ? 0 : 65535); + p += 2; + } + } else { + for (i = 0; i < pixel_count; ++i) { + if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2]) + p[3] = 0; + p += 4; + } + } + return 1; +} + +static int stbi__expand_png_palette(stbi__png *a, stbi_uc *palette, int len, int pal_img_n) +{ + stbi__uint32 i, pixel_count = a->s->img_x * a->s->img_y; + stbi_uc *p, *temp_out, *orig = a->out; + + p = (stbi_uc *) stbi__malloc_mad2(pixel_count, pal_img_n, 0); + if (p == NULL) return stbi__err("outofmem", "Out of memory"); + + // between here and free(out) below, exitting would leak + temp_out = p; + + if (pal_img_n == 3) { + for (i=0; i < pixel_count; ++i) { + int n = orig[i]*4; + p[0] = palette[n ]; + p[1] = palette[n+1]; + p[2] = palette[n+2]; + p += 3; + } + } else { + for (i=0; i < pixel_count; ++i) { + int n = orig[i]*4; + p[0] = palette[n ]; + p[1] = palette[n+1]; + p[2] = palette[n+2]; + p[3] = palette[n+3]; + p += 4; + } + } + STBI_FREE(a->out); + a->out = temp_out; + + STBI_NOTUSED(len); + + return 1; +} + +static int stbi__unpremultiply_on_load_global = 0; +static int stbi__de_iphone_flag_global = 0; + +STBIDEF void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply) +{ + stbi__unpremultiply_on_load_global = flag_true_if_should_unpremultiply; +} + +STBIDEF void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert) +{ + stbi__de_iphone_flag_global = flag_true_if_should_convert; +} + +#ifndef STBI_THREAD_LOCAL +#define stbi__unpremultiply_on_load stbi__unpremultiply_on_load_global +#define stbi__de_iphone_flag stbi__de_iphone_flag_global +#else +static STBI_THREAD_LOCAL int stbi__unpremultiply_on_load_local, stbi__unpremultiply_on_load_set; +static STBI_THREAD_LOCAL int stbi__de_iphone_flag_local, stbi__de_iphone_flag_set; + +STBIDEF void stbi_set_unpremultiply_on_load_thread(int flag_true_if_should_unpremultiply) +{ + stbi__unpremultiply_on_load_local = flag_true_if_should_unpremultiply; + stbi__unpremultiply_on_load_set = 1; +} + +STBIDEF void stbi_convert_iphone_png_to_rgb_thread(int flag_true_if_should_convert) +{ + stbi__de_iphone_flag_local = flag_true_if_should_convert; + stbi__de_iphone_flag_set = 1; +} + +#define stbi__unpremultiply_on_load (stbi__unpremultiply_on_load_set \ + ? stbi__unpremultiply_on_load_local \ + : stbi__unpremultiply_on_load_global) +#define stbi__de_iphone_flag (stbi__de_iphone_flag_set \ + ? stbi__de_iphone_flag_local \ + : stbi__de_iphone_flag_global) +#endif // STBI_THREAD_LOCAL + +static void stbi__de_iphone(stbi__png *z) +{ + stbi__context *s = z->s; + stbi__uint32 i, pixel_count = s->img_x * s->img_y; + stbi_uc *p = z->out; + + if (s->img_out_n == 3) { // convert bgr to rgb + for (i=0; i < pixel_count; ++i) { + stbi_uc t = p[0]; + p[0] = p[2]; + p[2] = t; + p += 3; + } + } else { + STBI_ASSERT(s->img_out_n == 4); + if (stbi__unpremultiply_on_load) { + // convert bgr to rgb and unpremultiply + for (i=0; i < pixel_count; ++i) { + stbi_uc a = p[3]; + stbi_uc t = p[0]; + if (a) { + stbi_uc half = a / 2; + p[0] = (p[2] * 255 + half) / a; + p[1] = (p[1] * 255 + half) / a; + p[2] = ( t * 255 + half) / a; + } else { + p[0] = p[2]; + p[2] = t; + } + p += 4; + } + } else { + // convert bgr to rgb + for (i=0; i < pixel_count; ++i) { + stbi_uc t = p[0]; + p[0] = p[2]; + p[2] = t; + p += 4; + } + } + } +} + +#define STBI__PNG_TYPE(a,b,c,d) (((unsigned) (a) << 24) + ((unsigned) (b) << 16) + ((unsigned) (c) << 8) + (unsigned) (d)) + +static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp) +{ + stbi_uc palette[1024], pal_img_n=0; + stbi_uc has_trans=0, tc[3]={0}; + stbi__uint16 tc16[3]; + stbi__uint32 ioff=0, idata_limit=0, i, pal_len=0; + int first=1,k,interlace=0, color=0, is_iphone=0; + stbi__context *s = z->s; + + z->expanded = NULL; + z->idata = NULL; + z->out = NULL; + + if (!stbi__check_png_header(s)) return 0; + + if (scan == STBI__SCAN_type) return 1; + + for (;;) { + stbi__pngchunk c = stbi__get_chunk_header(s); + switch (c.type) { + case STBI__PNG_TYPE('C','g','B','I'): + is_iphone = 1; + stbi__skip(s, c.length); + break; + case STBI__PNG_TYPE('I','H','D','R'): { + int comp,filter; + if (!first) return stbi__err("multiple IHDR","Corrupt PNG"); + first = 0; + if (c.length != 13) return stbi__err("bad IHDR len","Corrupt PNG"); + s->img_x = stbi__get32be(s); + s->img_y = stbi__get32be(s); + if (s->img_y > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)"); + if (s->img_x > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)"); + z->depth = stbi__get8(s); if (z->depth != 1 && z->depth != 2 && z->depth != 4 && z->depth != 8 && z->depth != 16) return stbi__err("1/2/4/8/16-bit only","PNG not supported: 1/2/4/8/16-bit only"); + color = stbi__get8(s); if (color > 6) return stbi__err("bad ctype","Corrupt PNG"); + if (color == 3 && z->depth == 16) return stbi__err("bad ctype","Corrupt PNG"); + if (color == 3) pal_img_n = 3; else if (color & 1) return stbi__err("bad ctype","Corrupt PNG"); + comp = stbi__get8(s); if (comp) return stbi__err("bad comp method","Corrupt PNG"); + filter= stbi__get8(s); if (filter) return stbi__err("bad filter method","Corrupt PNG"); + interlace = stbi__get8(s); if (interlace>1) return stbi__err("bad interlace method","Corrupt PNG"); + if (!s->img_x || !s->img_y) return stbi__err("0-pixel image","Corrupt PNG"); + if (!pal_img_n) { + s->img_n = (color & 2 ? 3 : 1) + (color & 4 ? 1 : 0); + if ((1 << 30) / s->img_x / s->img_n < s->img_y) return stbi__err("too large", "Image too large to decode"); + } else { + // if paletted, then pal_n is our final components, and + // img_n is # components to decompress/filter. + s->img_n = 1; + if ((1 << 30) / s->img_x / 4 < s->img_y) return stbi__err("too large","Corrupt PNG"); + } + // even with SCAN_header, have to scan to see if we have a tRNS + break; + } + + case STBI__PNG_TYPE('P','L','T','E'): { + if (first) return stbi__err("first not IHDR", "Corrupt PNG"); + if (c.length > 256*3) return stbi__err("invalid PLTE","Corrupt PNG"); + pal_len = c.length / 3; + if (pal_len * 3 != c.length) return stbi__err("invalid PLTE","Corrupt PNG"); + for (i=0; i < pal_len; ++i) { + palette[i*4+0] = stbi__get8(s); + palette[i*4+1] = stbi__get8(s); + palette[i*4+2] = stbi__get8(s); + palette[i*4+3] = 255; + } + break; + } + + case STBI__PNG_TYPE('t','R','N','S'): { + if (first) return stbi__err("first not IHDR", "Corrupt PNG"); + if (z->idata) return stbi__err("tRNS after IDAT","Corrupt PNG"); + if (pal_img_n) { + if (scan == STBI__SCAN_header) { s->img_n = 4; return 1; } + if (pal_len == 0) return stbi__err("tRNS before PLTE","Corrupt PNG"); + if (c.length > pal_len) return stbi__err("bad tRNS len","Corrupt PNG"); + pal_img_n = 4; + for (i=0; i < c.length; ++i) + palette[i*4+3] = stbi__get8(s); + } else { + if (!(s->img_n & 1)) return stbi__err("tRNS with alpha","Corrupt PNG"); + if (c.length != (stbi__uint32) s->img_n*2) return stbi__err("bad tRNS len","Corrupt PNG"); + has_trans = 1; + // non-paletted with tRNS = constant alpha. if header-scanning, we can stop now. + if (scan == STBI__SCAN_header) { ++s->img_n; return 1; } + if (z->depth == 16) { + for (k = 0; k < s->img_n && k < 3; ++k) // extra loop test to suppress false GCC warning + tc16[k] = (stbi__uint16)stbi__get16be(s); // copy the values as-is + } else { + for (k = 0; k < s->img_n && k < 3; ++k) + tc[k] = (stbi_uc)(stbi__get16be(s) & 255) * stbi__depth_scale_table[z->depth]; // non 8-bit images will be larger + } + } + break; + } + + case STBI__PNG_TYPE('I','D','A','T'): { + if (first) return stbi__err("first not IHDR", "Corrupt PNG"); + if (pal_img_n && !pal_len) return stbi__err("no PLTE","Corrupt PNG"); + if (scan == STBI__SCAN_header) { + // header scan definitely stops at first IDAT + if (pal_img_n) + s->img_n = pal_img_n; + return 1; + } + if (c.length > (1u << 30)) return stbi__err("IDAT size limit", "IDAT section larger than 2^30 bytes"); + if ((int)(ioff + c.length) < (int)ioff) return 0; + if (ioff + c.length > idata_limit) { + stbi__uint32 idata_limit_old = idata_limit; + stbi_uc *p; + if (idata_limit == 0) idata_limit = c.length > 4096 ? c.length : 4096; + while (ioff + c.length > idata_limit) + idata_limit *= 2; + STBI_NOTUSED(idata_limit_old); + p = (stbi_uc *) STBI_REALLOC_SIZED(z->idata, idata_limit_old, idata_limit); if (p == NULL) return stbi__err("outofmem", "Out of memory"); + z->idata = p; + } + if (!stbi__getn(s, z->idata+ioff,c.length)) return stbi__err("outofdata","Corrupt PNG"); + ioff += c.length; + break; + } + + case STBI__PNG_TYPE('I','E','N','D'): { + stbi__uint32 raw_len, bpl; + if (first) return stbi__err("first not IHDR", "Corrupt PNG"); + if (scan != STBI__SCAN_load) return 1; + if (z->idata == NULL) return stbi__err("no IDAT","Corrupt PNG"); + // initial guess for decoded data size to avoid unnecessary reallocs + bpl = (s->img_x * z->depth + 7) / 8; // bytes per line, per component + raw_len = bpl * s->img_y * s->img_n /* pixels */ + s->img_y /* filter mode per row */; + z->expanded = (stbi_uc *) stbi_zlib_decode_malloc_guesssize_headerflag((char *) z->idata, ioff, raw_len, (int *) &raw_len, !is_iphone); + if (z->expanded == NULL) return 0; // zlib should set error + STBI_FREE(z->idata); z->idata = NULL; + if ((req_comp == s->img_n+1 && req_comp != 3 && !pal_img_n) || has_trans) + s->img_out_n = s->img_n+1; + else + s->img_out_n = s->img_n; + if (!stbi__create_png_image(z, z->expanded, raw_len, s->img_out_n, z->depth, color, interlace)) return 0; + if (has_trans) { + if (z->depth == 16) { + if (!stbi__compute_transparency16(z, tc16, s->img_out_n)) return 0; + } else { + if (!stbi__compute_transparency(z, tc, s->img_out_n)) return 0; + } + } + if (is_iphone && stbi__de_iphone_flag && s->img_out_n > 2) + stbi__de_iphone(z); + if (pal_img_n) { + // pal_img_n == 3 or 4 + s->img_n = pal_img_n; // record the actual colors we had + s->img_out_n = pal_img_n; + if (req_comp >= 3) s->img_out_n = req_comp; + if (!stbi__expand_png_palette(z, palette, pal_len, s->img_out_n)) + return 0; + } else if (has_trans) { + // non-paletted image with tRNS -> source image has (constant) alpha + ++s->img_n; + } + STBI_FREE(z->expanded); z->expanded = NULL; + // end of PNG chunk, read and skip CRC + stbi__get32be(s); + return 1; + } + + default: + // if critical, fail + if (first) return stbi__err("first not IHDR", "Corrupt PNG"); + if ((c.type & (1 << 29)) == 0) { + #ifndef STBI_NO_FAILURE_STRINGS + // not threadsafe + static char invalid_chunk[] = "XXXX PNG chunk not known"; + invalid_chunk[0] = STBI__BYTECAST(c.type >> 24); + invalid_chunk[1] = STBI__BYTECAST(c.type >> 16); + invalid_chunk[2] = STBI__BYTECAST(c.type >> 8); + invalid_chunk[3] = STBI__BYTECAST(c.type >> 0); + #endif + return stbi__err(invalid_chunk, "PNG not supported: unknown PNG chunk type"); + } + stbi__skip(s, c.length); + break; + } + // end of PNG chunk, read and skip CRC + stbi__get32be(s); + } +} + +static void *stbi__do_png(stbi__png *p, int *x, int *y, int *n, int req_comp, stbi__result_info *ri) +{ + void *result=NULL; + if (req_comp < 0 || req_comp > 4) return stbi__errpuc("bad req_comp", "Internal error"); + if (stbi__parse_png_file(p, STBI__SCAN_load, req_comp)) { + if (p->depth <= 8) + ri->bits_per_channel = 8; + else if (p->depth == 16) + ri->bits_per_channel = 16; + else + return stbi__errpuc("bad bits_per_channel", "PNG not supported: unsupported color depth"); + result = p->out; + p->out = NULL; + if (req_comp && req_comp != p->s->img_out_n) { + if (ri->bits_per_channel == 8) + result = stbi__convert_format((unsigned char *) result, p->s->img_out_n, req_comp, p->s->img_x, p->s->img_y); + else + result = stbi__convert_format16((stbi__uint16 *) result, p->s->img_out_n, req_comp, p->s->img_x, p->s->img_y); + p->s->img_out_n = req_comp; + if (result == NULL) return result; + } + *x = p->s->img_x; + *y = p->s->img_y; + if (n) *n = p->s->img_n; + } + STBI_FREE(p->out); p->out = NULL; + STBI_FREE(p->expanded); p->expanded = NULL; + STBI_FREE(p->idata); p->idata = NULL; + + return result; +} + +static void *stbi__png_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri) +{ + stbi__png p; + p.s = s; + return stbi__do_png(&p, x,y,comp,req_comp, ri); +} + +static int stbi__png_test(stbi__context *s) +{ + int r; + r = stbi__check_png_header(s); + stbi__rewind(s); + return r; +} + +static int stbi__png_info_raw(stbi__png *p, int *x, int *y, int *comp) +{ + if (!stbi__parse_png_file(p, STBI__SCAN_header, 0)) { + stbi__rewind( p->s ); + return 0; + } + if (x) *x = p->s->img_x; + if (y) *y = p->s->img_y; + if (comp) *comp = p->s->img_n; + return 1; +} + +static int stbi__png_info(stbi__context *s, int *x, int *y, int *comp) +{ + stbi__png p; + p.s = s; + return stbi__png_info_raw(&p, x, y, comp); +} + +static int stbi__png_is16(stbi__context *s) +{ + stbi__png p; + p.s = s; + if (!stbi__png_info_raw(&p, NULL, NULL, NULL)) + return 0; + if (p.depth != 16) { + stbi__rewind(p.s); + return 0; + } + return 1; +} +#endif + +// Microsoft/Windows BMP image + +#ifndef STBI_NO_BMP +static int stbi__bmp_test_raw(stbi__context *s) +{ + int r; + int sz; + if (stbi__get8(s) != 'B') return 0; + if (stbi__get8(s) != 'M') return 0; + stbi__get32le(s); // discard filesize + stbi__get16le(s); // discard reserved + stbi__get16le(s); // discard reserved + stbi__get32le(s); // discard data offset + sz = stbi__get32le(s); + r = (sz == 12 || sz == 40 || sz == 56 || sz == 108 || sz == 124); + return r; +} + +static int stbi__bmp_test(stbi__context *s) +{ + int r = stbi__bmp_test_raw(s); + stbi__rewind(s); + return r; +} + + +// returns 0..31 for the highest set bit +static int stbi__high_bit(unsigned int z) +{ + int n=0; + if (z == 0) return -1; + if (z >= 0x10000) { n += 16; z >>= 16; } + if (z >= 0x00100) { n += 8; z >>= 8; } + if (z >= 0x00010) { n += 4; z >>= 4; } + if (z >= 0x00004) { n += 2; z >>= 2; } + if (z >= 0x00002) { n += 1;/* >>= 1;*/ } + return n; +} + +static int stbi__bitcount(unsigned int a) +{ + a = (a & 0x55555555) + ((a >> 1) & 0x55555555); // max 2 + a = (a & 0x33333333) + ((a >> 2) & 0x33333333); // max 4 + a = (a + (a >> 4)) & 0x0f0f0f0f; // max 8 per 4, now 8 bits + a = (a + (a >> 8)); // max 16 per 8 bits + a = (a + (a >> 16)); // max 32 per 8 bits + return a & 0xff; +} + +// extract an arbitrarily-aligned N-bit value (N=bits) +// from v, and then make it 8-bits long and fractionally +// extend it to full full range. +static int stbi__shiftsigned(unsigned int v, int shift, int bits) +{ + static unsigned int mul_table[9] = { + 0, + 0xff/*0b11111111*/, 0x55/*0b01010101*/, 0x49/*0b01001001*/, 0x11/*0b00010001*/, + 0x21/*0b00100001*/, 0x41/*0b01000001*/, 0x81/*0b10000001*/, 0x01/*0b00000001*/, + }; + static unsigned int shift_table[9] = { + 0, 0,0,1,0,2,4,6,0, + }; + if (shift < 0) + v <<= -shift; + else + v >>= shift; + STBI_ASSERT(v < 256); + v >>= (8-bits); + STBI_ASSERT(bits >= 0 && bits <= 8); + return (int) ((unsigned) v * mul_table[bits]) >> shift_table[bits]; +} + +typedef struct +{ + int bpp, offset, hsz; + unsigned int mr,mg,mb,ma, all_a; + int extra_read; +} stbi__bmp_data; + +static int stbi__bmp_set_mask_defaults(stbi__bmp_data *info, int compress) +{ + // BI_BITFIELDS specifies masks explicitly, don't override + if (compress == 3) + return 1; + + if (compress == 0) { + if (info->bpp == 16) { + info->mr = 31u << 10; + info->mg = 31u << 5; + info->mb = 31u << 0; + } else if (info->bpp == 32) { + info->mr = 0xffu << 16; + info->mg = 0xffu << 8; + info->mb = 0xffu << 0; + info->ma = 0xffu << 24; + info->all_a = 0; // if all_a is 0 at end, then we loaded alpha channel but it was all 0 + } else { + // otherwise, use defaults, which is all-0 + info->mr = info->mg = info->mb = info->ma = 0; + } + return 1; + } + return 0; // error +} + +static void *stbi__bmp_parse_header(stbi__context *s, stbi__bmp_data *info) +{ + int hsz; + if (stbi__get8(s) != 'B' || stbi__get8(s) != 'M') return stbi__errpuc("not BMP", "Corrupt BMP"); + stbi__get32le(s); // discard filesize + stbi__get16le(s); // discard reserved + stbi__get16le(s); // discard reserved + info->offset = stbi__get32le(s); + info->hsz = hsz = stbi__get32le(s); + info->mr = info->mg = info->mb = info->ma = 0; + info->extra_read = 14; + + if (info->offset < 0) return stbi__errpuc("bad BMP", "bad BMP"); + + if (hsz != 12 && hsz != 40 && hsz != 56 && hsz != 108 && hsz != 124) return stbi__errpuc("unknown BMP", "BMP type not supported: unknown"); + if (hsz == 12) { + s->img_x = stbi__get16le(s); + s->img_y = stbi__get16le(s); + } else { + s->img_x = stbi__get32le(s); + s->img_y = stbi__get32le(s); + } + if (stbi__get16le(s) != 1) return stbi__errpuc("bad BMP", "bad BMP"); + info->bpp = stbi__get16le(s); + if (hsz != 12) { + int compress = stbi__get32le(s); + if (compress == 1 || compress == 2) return stbi__errpuc("BMP RLE", "BMP type not supported: RLE"); + if (compress >= 4) return stbi__errpuc("BMP JPEG/PNG", "BMP type not supported: unsupported compression"); // this includes PNG/JPEG modes + if (compress == 3 && info->bpp != 16 && info->bpp != 32) return stbi__errpuc("bad BMP", "bad BMP"); // bitfields requires 16 or 32 bits/pixel + stbi__get32le(s); // discard sizeof + stbi__get32le(s); // discard hres + stbi__get32le(s); // discard vres + stbi__get32le(s); // discard colorsused + stbi__get32le(s); // discard max important + if (hsz == 40 || hsz == 56) { + if (hsz == 56) { + stbi__get32le(s); + stbi__get32le(s); + stbi__get32le(s); + stbi__get32le(s); + } + if (info->bpp == 16 || info->bpp == 32) { + if (compress == 0) { + stbi__bmp_set_mask_defaults(info, compress); + } else if (compress == 3) { + info->mr = stbi__get32le(s); + info->mg = stbi__get32le(s); + info->mb = stbi__get32le(s); + info->extra_read += 12; + // not documented, but generated by photoshop and handled by mspaint + if (info->mr == info->mg && info->mg == info->mb) { + // ?!?!? + return stbi__errpuc("bad BMP", "bad BMP"); + } + } else + return stbi__errpuc("bad BMP", "bad BMP"); + } + } else { + // V4/V5 header + int i; + if (hsz != 108 && hsz != 124) + return stbi__errpuc("bad BMP", "bad BMP"); + info->mr = stbi__get32le(s); + info->mg = stbi__get32le(s); + info->mb = stbi__get32le(s); + info->ma = stbi__get32le(s); + if (compress != 3) // override mr/mg/mb unless in BI_BITFIELDS mode, as per docs + stbi__bmp_set_mask_defaults(info, compress); + stbi__get32le(s); // discard color space + for (i=0; i < 12; ++i) + stbi__get32le(s); // discard color space parameters + if (hsz == 124) { + stbi__get32le(s); // discard rendering intent + stbi__get32le(s); // discard offset of profile data + stbi__get32le(s); // discard size of profile data + stbi__get32le(s); // discard reserved + } + } + } + return (void *) 1; +} + + +static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri) +{ + stbi_uc *out; + unsigned int mr=0,mg=0,mb=0,ma=0, all_a; + stbi_uc pal[256][4]; + int psize=0,i,j,width; + int flip_vertically, pad, target; + stbi__bmp_data info; + STBI_NOTUSED(ri); + + info.all_a = 255; + if (stbi__bmp_parse_header(s, &info) == NULL) + return NULL; // error code already set + + flip_vertically = ((int) s->img_y) > 0; + s->img_y = abs((int) s->img_y); + + if (s->img_y > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)"); + if (s->img_x > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)"); + + mr = info.mr; + mg = info.mg; + mb = info.mb; + ma = info.ma; + all_a = info.all_a; + + if (info.hsz == 12) { + if (info.bpp < 24) + psize = (info.offset - info.extra_read - 24) / 3; + } else { + if (info.bpp < 16) + psize = (info.offset - info.extra_read - info.hsz) >> 2; + } + if (psize == 0) { + // accept some number of extra bytes after the header, but if the offset points either to before + // the header ends or implies a large amount of extra data, reject the file as malformed + int bytes_read_so_far = s->callback_already_read + (int)(s->img_buffer - s->img_buffer_original); + int header_limit = 1024; // max we actually read is below 256 bytes currently. + int extra_data_limit = 256*4; // what ordinarily goes here is a palette; 256 entries*4 bytes is its max size. + if (bytes_read_so_far <= 0 || bytes_read_so_far > header_limit) { + return stbi__errpuc("bad header", "Corrupt BMP"); + } + // we established that bytes_read_so_far is positive and sensible. + // the first half of this test rejects offsets that are either too small positives, or + // negative, and guarantees that info.offset >= bytes_read_so_far > 0. this in turn + // ensures the number computed in the second half of the test can't overflow. + if (info.offset < bytes_read_so_far || info.offset - bytes_read_so_far > extra_data_limit) { + return stbi__errpuc("bad offset", "Corrupt BMP"); + } else { + stbi__skip(s, info.offset - bytes_read_so_far); + } + } + + if (info.bpp == 24 && ma == 0xff000000) + s->img_n = 3; + else + s->img_n = ma ? 4 : 3; + if (req_comp && req_comp >= 3) // we can directly decode 3 or 4 + target = req_comp; + else + target = s->img_n; // if they want monochrome, we'll post-convert + + // sanity-check size + if (!stbi__mad3sizes_valid(target, s->img_x, s->img_y, 0)) + return stbi__errpuc("too large", "Corrupt BMP"); + + out = (stbi_uc *) stbi__malloc_mad3(target, s->img_x, s->img_y, 0); + if (!out) return stbi__errpuc("outofmem", "Out of memory"); + if (info.bpp < 16) { + int z=0; + if (psize == 0 || psize > 256) { STBI_FREE(out); return stbi__errpuc("invalid", "Corrupt BMP"); } + for (i=0; i < psize; ++i) { + pal[i][2] = stbi__get8(s); + pal[i][1] = stbi__get8(s); + pal[i][0] = stbi__get8(s); + if (info.hsz != 12) stbi__get8(s); + pal[i][3] = 255; + } + stbi__skip(s, info.offset - info.extra_read - info.hsz - psize * (info.hsz == 12 ? 3 : 4)); + if (info.bpp == 1) width = (s->img_x + 7) >> 3; + else if (info.bpp == 4) width = (s->img_x + 1) >> 1; + else if (info.bpp == 8) width = s->img_x; + else { STBI_FREE(out); return stbi__errpuc("bad bpp", "Corrupt BMP"); } + pad = (-width)&3; + if (info.bpp == 1) { + for (j=0; j < (int) s->img_y; ++j) { + int bit_offset = 7, v = stbi__get8(s); + for (i=0; i < (int) s->img_x; ++i) { + int color = (v>>bit_offset)&0x1; + out[z++] = pal[color][0]; + out[z++] = pal[color][1]; + out[z++] = pal[color][2]; + if (target == 4) out[z++] = 255; + if (i+1 == (int) s->img_x) break; + if((--bit_offset) < 0) { + bit_offset = 7; + v = stbi__get8(s); + } + } + stbi__skip(s, pad); + } + } else { + for (j=0; j < (int) s->img_y; ++j) { + for (i=0; i < (int) s->img_x; i += 2) { + int v=stbi__get8(s),v2=0; + if (info.bpp == 4) { + v2 = v & 15; + v >>= 4; + } + out[z++] = pal[v][0]; + out[z++] = pal[v][1]; + out[z++] = pal[v][2]; + if (target == 4) out[z++] = 255; + if (i+1 == (int) s->img_x) break; + v = (info.bpp == 8) ? stbi__get8(s) : v2; + out[z++] = pal[v][0]; + out[z++] = pal[v][1]; + out[z++] = pal[v][2]; + if (target == 4) out[z++] = 255; + } + stbi__skip(s, pad); + } + } + } else { + int rshift=0,gshift=0,bshift=0,ashift=0,rcount=0,gcount=0,bcount=0,acount=0; + int z = 0; + int easy=0; + stbi__skip(s, info.offset - info.extra_read - info.hsz); + if (info.bpp == 24) width = 3 * s->img_x; + else if (info.bpp == 16) width = 2*s->img_x; + else /* bpp = 32 and pad = 0 */ width=0; + pad = (-width) & 3; + if (info.bpp == 24) { + easy = 1; + } else if (info.bpp == 32) { + if (mb == 0xff && mg == 0xff00 && mr == 0x00ff0000 && ma == 0xff000000) + easy = 2; + } + if (!easy) { + if (!mr || !mg || !mb) { STBI_FREE(out); return stbi__errpuc("bad masks", "Corrupt BMP"); } + // right shift amt to put high bit in position #7 + rshift = stbi__high_bit(mr)-7; rcount = stbi__bitcount(mr); + gshift = stbi__high_bit(mg)-7; gcount = stbi__bitcount(mg); + bshift = stbi__high_bit(mb)-7; bcount = stbi__bitcount(mb); + ashift = stbi__high_bit(ma)-7; acount = stbi__bitcount(ma); + if (rcount > 8 || gcount > 8 || bcount > 8 || acount > 8) { STBI_FREE(out); return stbi__errpuc("bad masks", "Corrupt BMP"); } + } + for (j=0; j < (int) s->img_y; ++j) { + if (easy) { + for (i=0; i < (int) s->img_x; ++i) { + unsigned char a; + out[z+2] = stbi__get8(s); + out[z+1] = stbi__get8(s); + out[z+0] = stbi__get8(s); + z += 3; + a = (easy == 2 ? stbi__get8(s) : 255); + all_a |= a; + if (target == 4) out[z++] = a; + } + } else { + int bpp = info.bpp; + for (i=0; i < (int) s->img_x; ++i) { + stbi__uint32 v = (bpp == 16 ? (stbi__uint32) stbi__get16le(s) : stbi__get32le(s)); + unsigned int a; + out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mr, rshift, rcount)); + out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mg, gshift, gcount)); + out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mb, bshift, bcount)); + a = (ma ? stbi__shiftsigned(v & ma, ashift, acount) : 255); + all_a |= a; + if (target == 4) out[z++] = STBI__BYTECAST(a); + } + } + stbi__skip(s, pad); + } + } + + // if alpha channel is all 0s, replace with all 255s + if (target == 4 && all_a == 0) + for (i=4*s->img_x*s->img_y-1; i >= 0; i -= 4) + out[i] = 255; + + if (flip_vertically) { + stbi_uc t; + for (j=0; j < (int) s->img_y>>1; ++j) { + stbi_uc *p1 = out + j *s->img_x*target; + stbi_uc *p2 = out + (s->img_y-1-j)*s->img_x*target; + for (i=0; i < (int) s->img_x*target; ++i) { + t = p1[i]; p1[i] = p2[i]; p2[i] = t; + } + } + } + + if (req_comp && req_comp != target) { + out = stbi__convert_format(out, target, req_comp, s->img_x, s->img_y); + if (out == NULL) return out; // stbi__convert_format frees input on failure + } + + *x = s->img_x; + *y = s->img_y; + if (comp) *comp = s->img_n; + return out; +} +#endif + +// Targa Truevision - TGA +// by Jonathan Dummer +#ifndef STBI_NO_TGA +// returns STBI_rgb or whatever, 0 on error +static int stbi__tga_get_comp(int bits_per_pixel, int is_grey, int* is_rgb16) +{ + // only RGB or RGBA (incl. 16bit) or grey allowed + if (is_rgb16) *is_rgb16 = 0; + switch(bits_per_pixel) { + case 8: return STBI_grey; + case 16: if(is_grey) return STBI_grey_alpha; + // fallthrough + case 15: if(is_rgb16) *is_rgb16 = 1; + return STBI_rgb; + case 24: // fallthrough + case 32: return bits_per_pixel/8; + default: return 0; + } +} + +static int stbi__tga_info(stbi__context *s, int *x, int *y, int *comp) +{ + int tga_w, tga_h, tga_comp, tga_image_type, tga_bits_per_pixel, tga_colormap_bpp; + int sz, tga_colormap_type; + stbi__get8(s); // discard Offset + tga_colormap_type = stbi__get8(s); // colormap type + if( tga_colormap_type > 1 ) { + stbi__rewind(s); + return 0; // only RGB or indexed allowed + } + tga_image_type = stbi__get8(s); // image type + if ( tga_colormap_type == 1 ) { // colormapped (paletted) image + if (tga_image_type != 1 && tga_image_type != 9) { + stbi__rewind(s); + return 0; + } + stbi__skip(s,4); // skip index of first colormap entry and number of entries + sz = stbi__get8(s); // check bits per palette color entry + if ( (sz != 8) && (sz != 15) && (sz != 16) && (sz != 24) && (sz != 32) ) { + stbi__rewind(s); + return 0; + } + stbi__skip(s,4); // skip image x and y origin + tga_colormap_bpp = sz; + } else { // "normal" image w/o colormap - only RGB or grey allowed, +/- RLE + if ( (tga_image_type != 2) && (tga_image_type != 3) && (tga_image_type != 10) && (tga_image_type != 11) ) { + stbi__rewind(s); + return 0; // only RGB or grey allowed, +/- RLE + } + stbi__skip(s,9); // skip colormap specification and image x/y origin + tga_colormap_bpp = 0; + } + tga_w = stbi__get16le(s); + if( tga_w < 1 ) { + stbi__rewind(s); + return 0; // test width + } + tga_h = stbi__get16le(s); + if( tga_h < 1 ) { + stbi__rewind(s); + return 0; // test height + } + tga_bits_per_pixel = stbi__get8(s); // bits per pixel + stbi__get8(s); // ignore alpha bits + if (tga_colormap_bpp != 0) { + if((tga_bits_per_pixel != 8) && (tga_bits_per_pixel != 16)) { + // when using a colormap, tga_bits_per_pixel is the size of the indexes + // I don't think anything but 8 or 16bit indexes makes sense + stbi__rewind(s); + return 0; + } + tga_comp = stbi__tga_get_comp(tga_colormap_bpp, 0, NULL); + } else { + tga_comp = stbi__tga_get_comp(tga_bits_per_pixel, (tga_image_type == 3) || (tga_image_type == 11), NULL); + } + if(!tga_comp) { + stbi__rewind(s); + return 0; + } + if (x) *x = tga_w; + if (y) *y = tga_h; + if (comp) *comp = tga_comp; + return 1; // seems to have passed everything +} + +static int stbi__tga_test(stbi__context *s) +{ + int res = 0; + int sz, tga_color_type; + stbi__get8(s); // discard Offset + tga_color_type = stbi__get8(s); // color type + if ( tga_color_type > 1 ) goto errorEnd; // only RGB or indexed allowed + sz = stbi__get8(s); // image type + if ( tga_color_type == 1 ) { // colormapped (paletted) image + if (sz != 1 && sz != 9) goto errorEnd; // colortype 1 demands image type 1 or 9 + stbi__skip(s,4); // skip index of first colormap entry and number of entries + sz = stbi__get8(s); // check bits per palette color entry + if ( (sz != 8) && (sz != 15) && (sz != 16) && (sz != 24) && (sz != 32) ) goto errorEnd; + stbi__skip(s,4); // skip image x and y origin + } else { // "normal" image w/o colormap + if ( (sz != 2) && (sz != 3) && (sz != 10) && (sz != 11) ) goto errorEnd; // only RGB or grey allowed, +/- RLE + stbi__skip(s,9); // skip colormap specification and image x/y origin + } + if ( stbi__get16le(s) < 1 ) goto errorEnd; // test width + if ( stbi__get16le(s) < 1 ) goto errorEnd; // test height + sz = stbi__get8(s); // bits per pixel + if ( (tga_color_type == 1) && (sz != 8) && (sz != 16) ) goto errorEnd; // for colormapped images, bpp is size of an index + if ( (sz != 8) && (sz != 15) && (sz != 16) && (sz != 24) && (sz != 32) ) goto errorEnd; + + res = 1; // if we got this far, everything's good and we can return 1 instead of 0 + +errorEnd: + stbi__rewind(s); + return res; +} + +// read 16bit value and convert to 24bit RGB +static void stbi__tga_read_rgb16(stbi__context *s, stbi_uc* out) +{ + stbi__uint16 px = (stbi__uint16)stbi__get16le(s); + stbi__uint16 fiveBitMask = 31; + // we have 3 channels with 5bits each + int r = (px >> 10) & fiveBitMask; + int g = (px >> 5) & fiveBitMask; + int b = px & fiveBitMask; + // Note that this saves the data in RGB(A) order, so it doesn't need to be swapped later + out[0] = (stbi_uc)((r * 255)/31); + out[1] = (stbi_uc)((g * 255)/31); + out[2] = (stbi_uc)((b * 255)/31); + + // some people claim that the most significant bit might be used for alpha + // (possibly if an alpha-bit is set in the "image descriptor byte") + // but that only made 16bit test images completely translucent.. + // so let's treat all 15 and 16bit TGAs as RGB with no alpha. +} + +static void *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri) +{ + // read in the TGA header stuff + int tga_offset = stbi__get8(s); + int tga_indexed = stbi__get8(s); + int tga_image_type = stbi__get8(s); + int tga_is_RLE = 0; + int tga_palette_start = stbi__get16le(s); + int tga_palette_len = stbi__get16le(s); + int tga_palette_bits = stbi__get8(s); + int tga_x_origin = stbi__get16le(s); + int tga_y_origin = stbi__get16le(s); + int tga_width = stbi__get16le(s); + int tga_height = stbi__get16le(s); + int tga_bits_per_pixel = stbi__get8(s); + int tga_comp, tga_rgb16=0; + int tga_inverted = stbi__get8(s); + // int tga_alpha_bits = tga_inverted & 15; // the 4 lowest bits - unused (useless?) + // image data + unsigned char *tga_data; + unsigned char *tga_palette = NULL; + int i, j; + unsigned char raw_data[4] = {0}; + int RLE_count = 0; + int RLE_repeating = 0; + int read_next_pixel = 1; + STBI_NOTUSED(ri); + STBI_NOTUSED(tga_x_origin); // @TODO + STBI_NOTUSED(tga_y_origin); // @TODO + + if (tga_height > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)"); + if (tga_width > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)"); + + // do a tiny bit of precessing + if ( tga_image_type >= 8 ) + { + tga_image_type -= 8; + tga_is_RLE = 1; + } + tga_inverted = 1 - ((tga_inverted >> 5) & 1); + + // If I'm paletted, then I'll use the number of bits from the palette + if ( tga_indexed ) tga_comp = stbi__tga_get_comp(tga_palette_bits, 0, &tga_rgb16); + else tga_comp = stbi__tga_get_comp(tga_bits_per_pixel, (tga_image_type == 3), &tga_rgb16); + + if(!tga_comp) // shouldn't really happen, stbi__tga_test() should have ensured basic consistency + return stbi__errpuc("bad format", "Can't find out TGA pixelformat"); + + // tga info + *x = tga_width; + *y = tga_height; + if (comp) *comp = tga_comp; + + if (!stbi__mad3sizes_valid(tga_width, tga_height, tga_comp, 0)) + return stbi__errpuc("too large", "Corrupt TGA"); + + tga_data = (unsigned char*)stbi__malloc_mad3(tga_width, tga_height, tga_comp, 0); + if (!tga_data) return stbi__errpuc("outofmem", "Out of memory"); + + // skip to the data's starting position (offset usually = 0) + stbi__skip(s, tga_offset ); + + if ( !tga_indexed && !tga_is_RLE && !tga_rgb16 ) { + for (i=0; i < tga_height; ++i) { + int row = tga_inverted ? tga_height -i - 1 : i; + stbi_uc *tga_row = tga_data + row*tga_width*tga_comp; + stbi__getn(s, tga_row, tga_width * tga_comp); + } + } else { + // do I need to load a palette? + if ( tga_indexed) + { + if (tga_palette_len == 0) { /* you have to have at least one entry! */ + STBI_FREE(tga_data); + return stbi__errpuc("bad palette", "Corrupt TGA"); + } + + // any data to skip? (offset usually = 0) + stbi__skip(s, tga_palette_start ); + // load the palette + tga_palette = (unsigned char*)stbi__malloc_mad2(tga_palette_len, tga_comp, 0); + if (!tga_palette) { + STBI_FREE(tga_data); + return stbi__errpuc("outofmem", "Out of memory"); + } + if (tga_rgb16) { + stbi_uc *pal_entry = tga_palette; + STBI_ASSERT(tga_comp == STBI_rgb); + for (i=0; i < tga_palette_len; ++i) { + stbi__tga_read_rgb16(s, pal_entry); + pal_entry += tga_comp; + } + } else if (!stbi__getn(s, tga_palette, tga_palette_len * tga_comp)) { + STBI_FREE(tga_data); + STBI_FREE(tga_palette); + return stbi__errpuc("bad palette", "Corrupt TGA"); + } + } + // load the data + for (i=0; i < tga_width * tga_height; ++i) + { + // if I'm in RLE mode, do I need to get a RLE stbi__pngchunk? + if ( tga_is_RLE ) + { + if ( RLE_count == 0 ) + { + // yep, get the next byte as a RLE command + int RLE_cmd = stbi__get8(s); + RLE_count = 1 + (RLE_cmd & 127); + RLE_repeating = RLE_cmd >> 7; + read_next_pixel = 1; + } else if ( !RLE_repeating ) + { + read_next_pixel = 1; + } + } else + { + read_next_pixel = 1; + } + // OK, if I need to read a pixel, do it now + if ( read_next_pixel ) + { + // load however much data we did have + if ( tga_indexed ) + { + // read in index, then perform the lookup + int pal_idx = (tga_bits_per_pixel == 8) ? stbi__get8(s) : stbi__get16le(s); + if ( pal_idx >= tga_palette_len ) { + // invalid index + pal_idx = 0; + } + pal_idx *= tga_comp; + for (j = 0; j < tga_comp; ++j) { + raw_data[j] = tga_palette[pal_idx+j]; + } + } else if(tga_rgb16) { + STBI_ASSERT(tga_comp == STBI_rgb); + stbi__tga_read_rgb16(s, raw_data); + } else { + // read in the data raw + for (j = 0; j < tga_comp; ++j) { + raw_data[j] = stbi__get8(s); + } + } + // clear the reading flag for the next pixel + read_next_pixel = 0; + } // end of reading a pixel + + // copy data + for (j = 0; j < tga_comp; ++j) + tga_data[i*tga_comp+j] = raw_data[j]; + + // in case we're in RLE mode, keep counting down + --RLE_count; + } + // do I need to invert the image? + if ( tga_inverted ) + { + for (j = 0; j*2 < tga_height; ++j) + { + int index1 = j * tga_width * tga_comp; + int index2 = (tga_height - 1 - j) * tga_width * tga_comp; + for (i = tga_width * tga_comp; i > 0; --i) + { + unsigned char temp = tga_data[index1]; + tga_data[index1] = tga_data[index2]; + tga_data[index2] = temp; + ++index1; + ++index2; + } + } + } + // clear my palette, if I had one + if ( tga_palette != NULL ) + { + STBI_FREE( tga_palette ); + } + } + + // swap RGB - if the source data was RGB16, it already is in the right order + if (tga_comp >= 3 && !tga_rgb16) + { + unsigned char* tga_pixel = tga_data; + for (i=0; i < tga_width * tga_height; ++i) + { + unsigned char temp = tga_pixel[0]; + tga_pixel[0] = tga_pixel[2]; + tga_pixel[2] = temp; + tga_pixel += tga_comp; + } + } + + // convert to target component count + if (req_comp && req_comp != tga_comp) + tga_data = stbi__convert_format(tga_data, tga_comp, req_comp, tga_width, tga_height); + + // the things I do to get rid of an error message, and yet keep + // Microsoft's C compilers happy... [8^( + tga_palette_start = tga_palette_len = tga_palette_bits = + tga_x_origin = tga_y_origin = 0; + STBI_NOTUSED(tga_palette_start); + // OK, done + return tga_data; +} +#endif + +// ************************************************************************************************* +// Photoshop PSD loader -- PD by Thatcher Ulrich, integration by Nicolas Schulz, tweaked by STB + +#ifndef STBI_NO_PSD +static int stbi__psd_test(stbi__context *s) +{ + int r = (stbi__get32be(s) == 0x38425053); + stbi__rewind(s); + return r; +} + +static int stbi__psd_decode_rle(stbi__context *s, stbi_uc *p, int pixelCount) +{ + int count, nleft, len; + + count = 0; + while ((nleft = pixelCount - count) > 0) { + len = stbi__get8(s); + if (len == 128) { + // No-op. + } else if (len < 128) { + // Copy next len+1 bytes literally. + len++; + if (len > nleft) return 0; // corrupt data + count += len; + while (len) { + *p = stbi__get8(s); + p += 4; + len--; + } + } else if (len > 128) { + stbi_uc val; + // Next -len+1 bytes in the dest are replicated from next source byte. + // (Interpret len as a negative 8-bit int.) + len = 257 - len; + if (len > nleft) return 0; // corrupt data + val = stbi__get8(s); + count += len; + while (len) { + *p = val; + p += 4; + len--; + } + } + } + + return 1; +} + +static void *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri, int bpc) +{ + int pixelCount; + int channelCount, compression; + int channel, i; + int bitdepth; + int w,h; + stbi_uc *out; + STBI_NOTUSED(ri); + + // Check identifier + if (stbi__get32be(s) != 0x38425053) // "8BPS" + return stbi__errpuc("not PSD", "Corrupt PSD image"); + + // Check file type version. + if (stbi__get16be(s) != 1) + return stbi__errpuc("wrong version", "Unsupported version of PSD image"); + + // Skip 6 reserved bytes. + stbi__skip(s, 6 ); + + // Read the number of channels (R, G, B, A, etc). + channelCount = stbi__get16be(s); + if (channelCount < 0 || channelCount > 16) + return stbi__errpuc("wrong channel count", "Unsupported number of channels in PSD image"); + + // Read the rows and columns of the image. + h = stbi__get32be(s); + w = stbi__get32be(s); + + if (h > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)"); + if (w > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)"); + + // Make sure the depth is 8 bits. + bitdepth = stbi__get16be(s); + if (bitdepth != 8 && bitdepth != 16) + return stbi__errpuc("unsupported bit depth", "PSD bit depth is not 8 or 16 bit"); + + // Make sure the color mode is RGB. + // Valid options are: + // 0: Bitmap + // 1: Grayscale + // 2: Indexed color + // 3: RGB color + // 4: CMYK color + // 7: Multichannel + // 8: Duotone + // 9: Lab color + if (stbi__get16be(s) != 3) + return stbi__errpuc("wrong color format", "PSD is not in RGB color format"); + + // Skip the Mode Data. (It's the palette for indexed color; other info for other modes.) + stbi__skip(s,stbi__get32be(s) ); + + // Skip the image resources. (resolution, pen tool paths, etc) + stbi__skip(s, stbi__get32be(s) ); + + // Skip the reserved data. + stbi__skip(s, stbi__get32be(s) ); + + // Find out if the data is compressed. + // Known values: + // 0: no compression + // 1: RLE compressed + compression = stbi__get16be(s); + if (compression > 1) + return stbi__errpuc("bad compression", "PSD has an unknown compression format"); + + // Check size + if (!stbi__mad3sizes_valid(4, w, h, 0)) + return stbi__errpuc("too large", "Corrupt PSD"); + + // Create the destination image. + + if (!compression && bitdepth == 16 && bpc == 16) { + out = (stbi_uc *) stbi__malloc_mad3(8, w, h, 0); + ri->bits_per_channel = 16; + } else + out = (stbi_uc *) stbi__malloc(4 * w*h); + + if (!out) return stbi__errpuc("outofmem", "Out of memory"); + pixelCount = w*h; + + // Initialize the data to zero. + //memset( out, 0, pixelCount * 4 ); + + // Finally, the image data. + if (compression) { + // RLE as used by .PSD and .TIFF + // Loop until you get the number of unpacked bytes you are expecting: + // Read the next source byte into n. + // If n is between 0 and 127 inclusive, copy the next n+1 bytes literally. + // Else if n is between -127 and -1 inclusive, copy the next byte -n+1 times. + // Else if n is 128, noop. + // Endloop + + // The RLE-compressed data is preceded by a 2-byte data count for each row in the data, + // which we're going to just skip. + stbi__skip(s, h * channelCount * 2 ); + + // Read the RLE data by channel. + for (channel = 0; channel < 4; channel++) { + stbi_uc *p; + + p = out+channel; + if (channel >= channelCount) { + // Fill this channel with default data. + for (i = 0; i < pixelCount; i++, p += 4) + *p = (channel == 3 ? 255 : 0); + } else { + // Read the RLE data. + if (!stbi__psd_decode_rle(s, p, pixelCount)) { + STBI_FREE(out); + return stbi__errpuc("corrupt", "bad RLE data"); + } + } + } + + } else { + // We're at the raw image data. It's each channel in order (Red, Green, Blue, Alpha, ...) + // where each channel consists of an 8-bit (or 16-bit) value for each pixel in the image. + + // Read the data by channel. + for (channel = 0; channel < 4; channel++) { + if (channel >= channelCount) { + // Fill this channel with default data. + if (bitdepth == 16 && bpc == 16) { + stbi__uint16 *q = ((stbi__uint16 *) out) + channel; + stbi__uint16 val = channel == 3 ? 65535 : 0; + for (i = 0; i < pixelCount; i++, q += 4) + *q = val; + } else { + stbi_uc *p = out+channel; + stbi_uc val = channel == 3 ? 255 : 0; + for (i = 0; i < pixelCount; i++, p += 4) + *p = val; + } + } else { + if (ri->bits_per_channel == 16) { // output bpc + stbi__uint16 *q = ((stbi__uint16 *) out) + channel; + for (i = 0; i < pixelCount; i++, q += 4) + *q = (stbi__uint16) stbi__get16be(s); + } else { + stbi_uc *p = out+channel; + if (bitdepth == 16) { // input bpc + for (i = 0; i < pixelCount; i++, p += 4) + *p = (stbi_uc) (stbi__get16be(s) >> 8); + } else { + for (i = 0; i < pixelCount; i++, p += 4) + *p = stbi__get8(s); + } + } + } + } + } + + // remove weird white matte from PSD + if (channelCount >= 4) { + if (ri->bits_per_channel == 16) { + for (i=0; i < w*h; ++i) { + stbi__uint16 *pixel = (stbi__uint16 *) out + 4*i; + if (pixel[3] != 0 && pixel[3] != 65535) { + float a = pixel[3] / 65535.0f; + float ra = 1.0f / a; + float inv_a = 65535.0f * (1 - ra); + pixel[0] = (stbi__uint16) (pixel[0]*ra + inv_a); + pixel[1] = (stbi__uint16) (pixel[1]*ra + inv_a); + pixel[2] = (stbi__uint16) (pixel[2]*ra + inv_a); + } + } + } else { + for (i=0; i < w*h; ++i) { + unsigned char *pixel = out + 4*i; + if (pixel[3] != 0 && pixel[3] != 255) { + float a = pixel[3] / 255.0f; + float ra = 1.0f / a; + float inv_a = 255.0f * (1 - ra); + pixel[0] = (unsigned char) (pixel[0]*ra + inv_a); + pixel[1] = (unsigned char) (pixel[1]*ra + inv_a); + pixel[2] = (unsigned char) (pixel[2]*ra + inv_a); + } + } + } + } + + // convert to desired output format + if (req_comp && req_comp != 4) { + if (ri->bits_per_channel == 16) + out = (stbi_uc *) stbi__convert_format16((stbi__uint16 *) out, 4, req_comp, w, h); + else + out = stbi__convert_format(out, 4, req_comp, w, h); + if (out == NULL) return out; // stbi__convert_format frees input on failure + } + + if (comp) *comp = 4; + *y = h; + *x = w; + + return out; +} +#endif + +// ************************************************************************************************* +// Softimage PIC loader +// by Tom Seddon +// +// See http://softimage.wiki.softimage.com/index.php/INFO:_PIC_file_format +// See http://ozviz.wasp.uwa.edu.au/~pbourke/dataformats/softimagepic/ + +#ifndef STBI_NO_PIC +static int stbi__pic_is4(stbi__context *s,const char *str) +{ + int i; + for (i=0; i<4; ++i) + if (stbi__get8(s) != (stbi_uc)str[i]) + return 0; + + return 1; +} + +static int stbi__pic_test_core(stbi__context *s) +{ + int i; + + if (!stbi__pic_is4(s,"\x53\x80\xF6\x34")) + return 0; + + for(i=0;i<84;++i) + stbi__get8(s); + + if (!stbi__pic_is4(s,"PICT")) + return 0; + + return 1; +} + +typedef struct +{ + stbi_uc size,type,channel; +} stbi__pic_packet; + +static stbi_uc *stbi__readval(stbi__context *s, int channel, stbi_uc *dest) +{ + int mask=0x80, i; + + for (i=0; i<4; ++i, mask>>=1) { + if (channel & mask) { + if (stbi__at_eof(s)) return stbi__errpuc("bad file","PIC file too short"); + dest[i]=stbi__get8(s); + } + } + + return dest; +} + +static void stbi__copyval(int channel,stbi_uc *dest,const stbi_uc *src) +{ + int mask=0x80,i; + + for (i=0;i<4; ++i, mask>>=1) + if (channel&mask) + dest[i]=src[i]; +} + +static stbi_uc *stbi__pic_load_core(stbi__context *s,int width,int height,int *comp, stbi_uc *result) +{ + int act_comp=0,num_packets=0,y,chained; + stbi__pic_packet packets[10]; + + // this will (should...) cater for even some bizarre stuff like having data + // for the same channel in multiple packets. + do { + stbi__pic_packet *packet; + + if (num_packets==sizeof(packets)/sizeof(packets[0])) + return stbi__errpuc("bad format","too many packets"); + + packet = &packets[num_packets++]; + + chained = stbi__get8(s); + packet->size = stbi__get8(s); + packet->type = stbi__get8(s); + packet->channel = stbi__get8(s); + + act_comp |= packet->channel; + + if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (reading packets)"); + if (packet->size != 8) return stbi__errpuc("bad format","packet isn't 8bpp"); + } while (chained); + + *comp = (act_comp & 0x10 ? 4 : 3); // has alpha channel? + + for(y=0; ytype) { + default: + return stbi__errpuc("bad format","packet has bad compression type"); + + case 0: {//uncompressed + int x; + + for(x=0;xchannel,dest)) + return 0; + break; + } + + case 1://Pure RLE + { + int left=width, i; + + while (left>0) { + stbi_uc count,value[4]; + + count=stbi__get8(s); + if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (pure read count)"); + + if (count > left) + count = (stbi_uc) left; + + if (!stbi__readval(s,packet->channel,value)) return 0; + + for(i=0; ichannel,dest,value); + left -= count; + } + } + break; + + case 2: {//Mixed RLE + int left=width; + while (left>0) { + int count = stbi__get8(s), i; + if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (mixed read count)"); + + if (count >= 128) { // Repeated + stbi_uc value[4]; + + if (count==128) + count = stbi__get16be(s); + else + count -= 127; + if (count > left) + return stbi__errpuc("bad file","scanline overrun"); + + if (!stbi__readval(s,packet->channel,value)) + return 0; + + for(i=0;ichannel,dest,value); + } else { // Raw + ++count; + if (count>left) return stbi__errpuc("bad file","scanline overrun"); + + for(i=0;ichannel,dest)) + return 0; + } + left-=count; + } + break; + } + } + } + } + + return result; +} + +static void *stbi__pic_load(stbi__context *s,int *px,int *py,int *comp,int req_comp, stbi__result_info *ri) +{ + stbi_uc *result; + int i, x,y, internal_comp; + STBI_NOTUSED(ri); + + if (!comp) comp = &internal_comp; + + for (i=0; i<92; ++i) + stbi__get8(s); + + x = stbi__get16be(s); + y = stbi__get16be(s); + + if (y > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)"); + if (x > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)"); + + if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (pic header)"); + if (!stbi__mad3sizes_valid(x, y, 4, 0)) return stbi__errpuc("too large", "PIC image too large to decode"); + + stbi__get32be(s); //skip `ratio' + stbi__get16be(s); //skip `fields' + stbi__get16be(s); //skip `pad' + + // intermediate buffer is RGBA + result = (stbi_uc *) stbi__malloc_mad3(x, y, 4, 0); + if (!result) return stbi__errpuc("outofmem", "Out of memory"); + memset(result, 0xff, x*y*4); + + if (!stbi__pic_load_core(s,x,y,comp, result)) { + STBI_FREE(result); + result=0; + } + *px = x; + *py = y; + if (req_comp == 0) req_comp = *comp; + result=stbi__convert_format(result,4,req_comp,x,y); + + return result; +} + +static int stbi__pic_test(stbi__context *s) +{ + int r = stbi__pic_test_core(s); + stbi__rewind(s); + return r; +} +#endif + +// ************************************************************************************************* +// GIF loader -- public domain by Jean-Marc Lienher -- simplified/shrunk by stb + +#ifndef STBI_NO_GIF +typedef struct +{ + stbi__int16 prefix; + stbi_uc first; + stbi_uc suffix; +} stbi__gif_lzw; + +typedef struct +{ + int w,h; + stbi_uc *out; // output buffer (always 4 components) + stbi_uc *background; // The current "background" as far as a gif is concerned + stbi_uc *history; + int flags, bgindex, ratio, transparent, eflags; + stbi_uc pal[256][4]; + stbi_uc lpal[256][4]; + stbi__gif_lzw codes[8192]; + stbi_uc *color_table; + int parse, step; + int lflags; + int start_x, start_y; + int max_x, max_y; + int cur_x, cur_y; + int line_size; + int delay; +} stbi__gif; + +static int stbi__gif_test_raw(stbi__context *s) +{ + int sz; + if (stbi__get8(s) != 'G' || stbi__get8(s) != 'I' || stbi__get8(s) != 'F' || stbi__get8(s) != '8') return 0; + sz = stbi__get8(s); + if (sz != '9' && sz != '7') return 0; + if (stbi__get8(s) != 'a') return 0; + return 1; +} + +static int stbi__gif_test(stbi__context *s) +{ + int r = stbi__gif_test_raw(s); + stbi__rewind(s); + return r; +} + +static void stbi__gif_parse_colortable(stbi__context *s, stbi_uc pal[256][4], int num_entries, int transp) +{ + int i; + for (i=0; i < num_entries; ++i) { + pal[i][2] = stbi__get8(s); + pal[i][1] = stbi__get8(s); + pal[i][0] = stbi__get8(s); + pal[i][3] = transp == i ? 0 : 255; + } +} + +static int stbi__gif_header(stbi__context *s, stbi__gif *g, int *comp, int is_info) +{ + stbi_uc version; + if (stbi__get8(s) != 'G' || stbi__get8(s) != 'I' || stbi__get8(s) != 'F' || stbi__get8(s) != '8') + return stbi__err("not GIF", "Corrupt GIF"); + + version = stbi__get8(s); + if (version != '7' && version != '9') return stbi__err("not GIF", "Corrupt GIF"); + if (stbi__get8(s) != 'a') return stbi__err("not GIF", "Corrupt GIF"); + + stbi__g_failure_reason = ""; + g->w = stbi__get16le(s); + g->h = stbi__get16le(s); + g->flags = stbi__get8(s); + g->bgindex = stbi__get8(s); + g->ratio = stbi__get8(s); + g->transparent = -1; + + if (g->w > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)"); + if (g->h > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)"); + + if (comp != 0) *comp = 4; // can't actually tell whether it's 3 or 4 until we parse the comments + + if (is_info) return 1; + + if (g->flags & 0x80) + stbi__gif_parse_colortable(s,g->pal, 2 << (g->flags & 7), -1); + + return 1; +} + +static int stbi__gif_info_raw(stbi__context *s, int *x, int *y, int *comp) +{ + stbi__gif* g = (stbi__gif*) stbi__malloc(sizeof(stbi__gif)); + if (!g) return stbi__err("outofmem", "Out of memory"); + if (!stbi__gif_header(s, g, comp, 1)) { + STBI_FREE(g); + stbi__rewind( s ); + return 0; + } + if (x) *x = g->w; + if (y) *y = g->h; + STBI_FREE(g); + return 1; +} + +static void stbi__out_gif_code(stbi__gif *g, stbi__uint16 code) +{ + stbi_uc *p, *c; + int idx; + + // recurse to decode the prefixes, since the linked-list is backwards, + // and working backwards through an interleaved image would be nasty + if (g->codes[code].prefix >= 0) + stbi__out_gif_code(g, g->codes[code].prefix); + + if (g->cur_y >= g->max_y) return; + + idx = g->cur_x + g->cur_y; + p = &g->out[idx]; + g->history[idx / 4] = 1; + + c = &g->color_table[g->codes[code].suffix * 4]; + if (c[3] > 128) { // don't render transparent pixels; + p[0] = c[2]; + p[1] = c[1]; + p[2] = c[0]; + p[3] = c[3]; + } + g->cur_x += 4; + + if (g->cur_x >= g->max_x) { + g->cur_x = g->start_x; + g->cur_y += g->step; + + while (g->cur_y >= g->max_y && g->parse > 0) { + g->step = (1 << g->parse) * g->line_size; + g->cur_y = g->start_y + (g->step >> 1); + --g->parse; + } + } +} + +static stbi_uc *stbi__process_gif_raster(stbi__context *s, stbi__gif *g) +{ + stbi_uc lzw_cs; + stbi__int32 len, init_code; + stbi__uint32 first; + stbi__int32 codesize, codemask, avail, oldcode, bits, valid_bits, clear; + stbi__gif_lzw *p; + + lzw_cs = stbi__get8(s); + if (lzw_cs > 12) return NULL; + clear = 1 << lzw_cs; + first = 1; + codesize = lzw_cs + 1; + codemask = (1 << codesize) - 1; + bits = 0; + valid_bits = 0; + for (init_code = 0; init_code < clear; init_code++) { + g->codes[init_code].prefix = -1; + g->codes[init_code].first = (stbi_uc) init_code; + g->codes[init_code].suffix = (stbi_uc) init_code; + } + + // support no starting clear code + avail = clear+2; + oldcode = -1; + + len = 0; + for(;;) { + if (valid_bits < codesize) { + if (len == 0) { + len = stbi__get8(s); // start new block + if (len == 0) + return g->out; + } + --len; + bits |= (stbi__int32) stbi__get8(s) << valid_bits; + valid_bits += 8; + } else { + stbi__int32 code = bits & codemask; + bits >>= codesize; + valid_bits -= codesize; + // @OPTIMIZE: is there some way we can accelerate the non-clear path? + if (code == clear) { // clear code + codesize = lzw_cs + 1; + codemask = (1 << codesize) - 1; + avail = clear + 2; + oldcode = -1; + first = 0; + } else if (code == clear + 1) { // end of stream code + stbi__skip(s, len); + while ((len = stbi__get8(s)) > 0) + stbi__skip(s,len); + return g->out; + } else if (code <= avail) { + if (first) { + return stbi__errpuc("no clear code", "Corrupt GIF"); + } + + if (oldcode >= 0) { + p = &g->codes[avail++]; + if (avail > 8192) { + return stbi__errpuc("too many codes", "Corrupt GIF"); + } + + p->prefix = (stbi__int16) oldcode; + p->first = g->codes[oldcode].first; + p->suffix = (code == avail) ? p->first : g->codes[code].first; + } else if (code == avail) + return stbi__errpuc("illegal code in raster", "Corrupt GIF"); + + stbi__out_gif_code(g, (stbi__uint16) code); + + if ((avail & codemask) == 0 && avail <= 0x0FFF) { + codesize++; + codemask = (1 << codesize) - 1; + } + + oldcode = code; + } else { + return stbi__errpuc("illegal code in raster", "Corrupt GIF"); + } + } + } +} + +// this function is designed to support animated gifs, although stb_image doesn't support it +// two back is the image from two frames ago, used for a very specific disposal format +static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, int req_comp, stbi_uc *two_back) +{ + int dispose; + int first_frame; + int pi; + int pcount; + STBI_NOTUSED(req_comp); + + // on first frame, any non-written pixels get the background colour (non-transparent) + first_frame = 0; + if (g->out == 0) { + if (!stbi__gif_header(s, g, comp,0)) return 0; // stbi__g_failure_reason set by stbi__gif_header + if (!stbi__mad3sizes_valid(4, g->w, g->h, 0)) + return stbi__errpuc("too large", "GIF image is too large"); + pcount = g->w * g->h; + g->out = (stbi_uc *) stbi__malloc(4 * pcount); + g->background = (stbi_uc *) stbi__malloc(4 * pcount); + g->history = (stbi_uc *) stbi__malloc(pcount); + if (!g->out || !g->background || !g->history) + return stbi__errpuc("outofmem", "Out of memory"); + + // image is treated as "transparent" at the start - ie, nothing overwrites the current background; + // background colour is only used for pixels that are not rendered first frame, after that "background" + // color refers to the color that was there the previous frame. + memset(g->out, 0x00, 4 * pcount); + memset(g->background, 0x00, 4 * pcount); // state of the background (starts transparent) + memset(g->history, 0x00, pcount); // pixels that were affected previous frame + first_frame = 1; + } else { + // second frame - how do we dispose of the previous one? + dispose = (g->eflags & 0x1C) >> 2; + pcount = g->w * g->h; + + if ((dispose == 3) && (two_back == 0)) { + dispose = 2; // if I don't have an image to revert back to, default to the old background + } + + if (dispose == 3) { // use previous graphic + for (pi = 0; pi < pcount; ++pi) { + if (g->history[pi]) { + memcpy( &g->out[pi * 4], &two_back[pi * 4], 4 ); + } + } + } else if (dispose == 2) { + // restore what was changed last frame to background before that frame; + for (pi = 0; pi < pcount; ++pi) { + if (g->history[pi]) { + memcpy( &g->out[pi * 4], &g->background[pi * 4], 4 ); + } + } + } else { + // This is a non-disposal case eithe way, so just + // leave the pixels as is, and they will become the new background + // 1: do not dispose + // 0: not specified. + } + + // background is what out is after the undoing of the previou frame; + memcpy( g->background, g->out, 4 * g->w * g->h ); + } + + // clear my history; + memset( g->history, 0x00, g->w * g->h ); // pixels that were affected previous frame + + for (;;) { + int tag = stbi__get8(s); + switch (tag) { + case 0x2C: /* Image Descriptor */ + { + stbi__int32 x, y, w, h; + stbi_uc *o; + + x = stbi__get16le(s); + y = stbi__get16le(s); + w = stbi__get16le(s); + h = stbi__get16le(s); + if (((x + w) > (g->w)) || ((y + h) > (g->h))) + return stbi__errpuc("bad Image Descriptor", "Corrupt GIF"); + + g->line_size = g->w * 4; + g->start_x = x * 4; + g->start_y = y * g->line_size; + g->max_x = g->start_x + w * 4; + g->max_y = g->start_y + h * g->line_size; + g->cur_x = g->start_x; + g->cur_y = g->start_y; + + // if the width of the specified rectangle is 0, that means + // we may not see *any* pixels or the image is malformed; + // to make sure this is caught, move the current y down to + // max_y (which is what out_gif_code checks). + if (w == 0) + g->cur_y = g->max_y; + + g->lflags = stbi__get8(s); + + if (g->lflags & 0x40) { + g->step = 8 * g->line_size; // first interlaced spacing + g->parse = 3; + } else { + g->step = g->line_size; + g->parse = 0; + } + + if (g->lflags & 0x80) { + stbi__gif_parse_colortable(s,g->lpal, 2 << (g->lflags & 7), g->eflags & 0x01 ? g->transparent : -1); + g->color_table = (stbi_uc *) g->lpal; + } else if (g->flags & 0x80) { + g->color_table = (stbi_uc *) g->pal; + } else + return stbi__errpuc("missing color table", "Corrupt GIF"); + + o = stbi__process_gif_raster(s, g); + if (!o) return NULL; + + // if this was the first frame, + pcount = g->w * g->h; + if (first_frame && (g->bgindex > 0)) { + // if first frame, any pixel not drawn to gets the background color + for (pi = 0; pi < pcount; ++pi) { + if (g->history[pi] == 0) { + g->pal[g->bgindex][3] = 255; // just in case it was made transparent, undo that; It will be reset next frame if need be; + memcpy( &g->out[pi * 4], &g->pal[g->bgindex], 4 ); + } + } + } + + return o; + } + + case 0x21: // Comment Extension. + { + int len; + int ext = stbi__get8(s); + if (ext == 0xF9) { // Graphic Control Extension. + len = stbi__get8(s); + if (len == 4) { + g->eflags = stbi__get8(s); + g->delay = 10 * stbi__get16le(s); // delay - 1/100th of a second, saving as 1/1000ths. + + // unset old transparent + if (g->transparent >= 0) { + g->pal[g->transparent][3] = 255; + } + if (g->eflags & 0x01) { + g->transparent = stbi__get8(s); + if (g->transparent >= 0) { + g->pal[g->transparent][3] = 0; + } + } else { + // don't need transparent + stbi__skip(s, 1); + g->transparent = -1; + } + } else { + stbi__skip(s, len); + break; + } + } + while ((len = stbi__get8(s)) != 0) { + stbi__skip(s, len); + } + break; + } + + case 0x3B: // gif stream termination code + return (stbi_uc *) s; // using '1' causes warning on some compilers + + default: + return stbi__errpuc("unknown code", "Corrupt GIF"); + } + } +} + +static void *stbi__load_gif_main_outofmem(stbi__gif *g, stbi_uc *out, int **delays) +{ + STBI_FREE(g->out); + STBI_FREE(g->history); + STBI_FREE(g->background); + + if (out) STBI_FREE(out); + if (delays && *delays) STBI_FREE(*delays); + return stbi__errpuc("outofmem", "Out of memory"); +} + +static void *stbi__load_gif_main(stbi__context *s, int **delays, int *x, int *y, int *z, int *comp, int req_comp) +{ + if (stbi__gif_test(s)) { + int layers = 0; + stbi_uc *u = 0; + stbi_uc *out = 0; + stbi_uc *two_back = 0; + stbi__gif g; + int stride; + int out_size = 0; + int delays_size = 0; + + STBI_NOTUSED(out_size); + STBI_NOTUSED(delays_size); + + memset(&g, 0, sizeof(g)); + if (delays) { + *delays = 0; + } + + do { + u = stbi__gif_load_next(s, &g, comp, req_comp, two_back); + if (u == (stbi_uc *) s) u = 0; // end of animated gif marker + + if (u) { + *x = g.w; + *y = g.h; + ++layers; + stride = g.w * g.h * 4; + + if (out) { + void *tmp = (stbi_uc*) STBI_REALLOC_SIZED( out, out_size, layers * stride ); + if (!tmp) + return stbi__load_gif_main_outofmem(&g, out, delays); + else { + out = (stbi_uc*) tmp; + out_size = layers * stride; + } + + if (delays) { + int *new_delays = (int*) STBI_REALLOC_SIZED( *delays, delays_size, sizeof(int) * layers ); + if (!new_delays) + return stbi__load_gif_main_outofmem(&g, out, delays); + *delays = new_delays; + delays_size = layers * sizeof(int); + } + } else { + out = (stbi_uc*)stbi__malloc( layers * stride ); + if (!out) + return stbi__load_gif_main_outofmem(&g, out, delays); + out_size = layers * stride; + if (delays) { + *delays = (int*) stbi__malloc( layers * sizeof(int) ); + if (!*delays) + return stbi__load_gif_main_outofmem(&g, out, delays); + delays_size = layers * sizeof(int); + } + } + memcpy( out + ((layers - 1) * stride), u, stride ); + if (layers >= 2) { + two_back = out - 2 * stride; + } + + if (delays) { + (*delays)[layers - 1U] = g.delay; + } + } + } while (u != 0); + + // free temp buffer; + STBI_FREE(g.out); + STBI_FREE(g.history); + STBI_FREE(g.background); + + // do the final conversion after loading everything; + if (req_comp && req_comp != 4) + out = stbi__convert_format(out, 4, req_comp, layers * g.w, g.h); + + *z = layers; + return out; + } else { + return stbi__errpuc("not GIF", "Image was not as a gif type."); + } +} + +static void *stbi__gif_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri) +{ + stbi_uc *u = 0; + stbi__gif g; + memset(&g, 0, sizeof(g)); + STBI_NOTUSED(ri); + + u = stbi__gif_load_next(s, &g, comp, req_comp, 0); + if (u == (stbi_uc *) s) u = 0; // end of animated gif marker + if (u) { + *x = g.w; + *y = g.h; + + // moved conversion to after successful load so that the same + // can be done for multiple frames. + if (req_comp && req_comp != 4) + u = stbi__convert_format(u, 4, req_comp, g.w, g.h); + } else if (g.out) { + // if there was an error and we allocated an image buffer, free it! + STBI_FREE(g.out); + } + + // free buffers needed for multiple frame loading; + STBI_FREE(g.history); + STBI_FREE(g.background); + + return u; +} + +static int stbi__gif_info(stbi__context *s, int *x, int *y, int *comp) +{ + return stbi__gif_info_raw(s,x,y,comp); +} +#endif + +// ************************************************************************************************* +// Radiance RGBE HDR loader +// originally by Nicolas Schulz +#ifndef STBI_NO_HDR +static int stbi__hdr_test_core(stbi__context *s, const char *signature) +{ + int i; + for (i=0; signature[i]; ++i) + if (stbi__get8(s) != signature[i]) + return 0; + stbi__rewind(s); + return 1; +} + +static int stbi__hdr_test(stbi__context* s) +{ + int r = stbi__hdr_test_core(s, "#?RADIANCE\n"); + stbi__rewind(s); + if(!r) { + r = stbi__hdr_test_core(s, "#?RGBE\n"); + stbi__rewind(s); + } + return r; +} + +#define STBI__HDR_BUFLEN 1024 +static char *stbi__hdr_gettoken(stbi__context *z, char *buffer) +{ + int len=0; + char c = '\0'; + + c = (char) stbi__get8(z); + + while (!stbi__at_eof(z) && c != '\n') { + buffer[len++] = c; + if (len == STBI__HDR_BUFLEN-1) { + // flush to end of line + while (!stbi__at_eof(z) && stbi__get8(z) != '\n') + ; + break; + } + c = (char) stbi__get8(z); + } + + buffer[len] = 0; + return buffer; +} + +static void stbi__hdr_convert(float *output, stbi_uc *input, int req_comp) +{ + if ( input[3] != 0 ) { + float f1; + // Exponent + f1 = (float) ldexp(1.0f, input[3] - (int)(128 + 8)); + if (req_comp <= 2) + output[0] = (input[0] + input[1] + input[2]) * f1 / 3; + else { + output[0] = input[0] * f1; + output[1] = input[1] * f1; + output[2] = input[2] * f1; + } + if (req_comp == 2) output[1] = 1; + if (req_comp == 4) output[3] = 1; + } else { + switch (req_comp) { + case 4: output[3] = 1; /* fallthrough */ + case 3: output[0] = output[1] = output[2] = 0; + break; + case 2: output[1] = 1; /* fallthrough */ + case 1: output[0] = 0; + break; + } + } +} + +static float *stbi__hdr_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri) +{ + char buffer[STBI__HDR_BUFLEN]; + char *token; + int valid = 0; + int width, height; + stbi_uc *scanline; + float *hdr_data; + int len; + unsigned char count, value; + int i, j, k, c1,c2, z; + const char *headerToken; + STBI_NOTUSED(ri); + + // Check identifier + headerToken = stbi__hdr_gettoken(s,buffer); + if (strcmp(headerToken, "#?RADIANCE") != 0 && strcmp(headerToken, "#?RGBE") != 0) + return stbi__errpf("not HDR", "Corrupt HDR image"); + + // Parse header + for(;;) { + token = stbi__hdr_gettoken(s,buffer); + if (token[0] == 0) break; + if (strcmp(token, "FORMAT=32-bit_rle_rgbe") == 0) valid = 1; + } + + if (!valid) return stbi__errpf("unsupported format", "Unsupported HDR format"); + + // Parse width and height + // can't use sscanf() if we're not using stdio! + token = stbi__hdr_gettoken(s,buffer); + if (strncmp(token, "-Y ", 3)) return stbi__errpf("unsupported data layout", "Unsupported HDR format"); + token += 3; + height = (int) strtol(token, &token, 10); + while (*token == ' ') ++token; + if (strncmp(token, "+X ", 3)) return stbi__errpf("unsupported data layout", "Unsupported HDR format"); + token += 3; + width = (int) strtol(token, NULL, 10); + + if (height > STBI_MAX_DIMENSIONS) return stbi__errpf("too large","Very large image (corrupt?)"); + if (width > STBI_MAX_DIMENSIONS) return stbi__errpf("too large","Very large image (corrupt?)"); + + *x = width; + *y = height; + + if (comp) *comp = 3; + if (req_comp == 0) req_comp = 3; + + if (!stbi__mad4sizes_valid(width, height, req_comp, sizeof(float), 0)) + return stbi__errpf("too large", "HDR image is too large"); + + // Read data + hdr_data = (float *) stbi__malloc_mad4(width, height, req_comp, sizeof(float), 0); + if (!hdr_data) + return stbi__errpf("outofmem", "Out of memory"); + + // Load image data + // image data is stored as some number of sca + if ( width < 8 || width >= 32768) { + // Read flat data + for (j=0; j < height; ++j) { + for (i=0; i < width; ++i) { + stbi_uc rgbe[4]; + main_decode_loop: + stbi__getn(s, rgbe, 4); + stbi__hdr_convert(hdr_data + j * width * req_comp + i * req_comp, rgbe, req_comp); + } + } + } else { + // Read RLE-encoded data + scanline = NULL; + + for (j = 0; j < height; ++j) { + c1 = stbi__get8(s); + c2 = stbi__get8(s); + len = stbi__get8(s); + if (c1 != 2 || c2 != 2 || (len & 0x80)) { + // not run-length encoded, so we have to actually use THIS data as a decoded + // pixel (note this can't be a valid pixel--one of RGB must be >= 128) + stbi_uc rgbe[4]; + rgbe[0] = (stbi_uc) c1; + rgbe[1] = (stbi_uc) c2; + rgbe[2] = (stbi_uc) len; + rgbe[3] = (stbi_uc) stbi__get8(s); + stbi__hdr_convert(hdr_data, rgbe, req_comp); + i = 1; + j = 0; + STBI_FREE(scanline); + goto main_decode_loop; // yes, this makes no sense + } + len <<= 8; + len |= stbi__get8(s); + if (len != width) { STBI_FREE(hdr_data); STBI_FREE(scanline); return stbi__errpf("invalid decoded scanline length", "corrupt HDR"); } + if (scanline == NULL) { + scanline = (stbi_uc *) stbi__malloc_mad2(width, 4, 0); + if (!scanline) { + STBI_FREE(hdr_data); + return stbi__errpf("outofmem", "Out of memory"); + } + } + + for (k = 0; k < 4; ++k) { + int nleft; + i = 0; + while ((nleft = width - i) > 0) { + count = stbi__get8(s); + if (count > 128) { + // Run + value = stbi__get8(s); + count -= 128; + if ((count == 0) || (count > nleft)) { STBI_FREE(hdr_data); STBI_FREE(scanline); return stbi__errpf("corrupt", "bad RLE data in HDR"); } + for (z = 0; z < count; ++z) + scanline[i++ * 4 + k] = value; + } else { + // Dump + if ((count == 0) || (count > nleft)) { STBI_FREE(hdr_data); STBI_FREE(scanline); return stbi__errpf("corrupt", "bad RLE data in HDR"); } + for (z = 0; z < count; ++z) + scanline[i++ * 4 + k] = stbi__get8(s); + } + } + } + for (i=0; i < width; ++i) + stbi__hdr_convert(hdr_data+(j*width + i)*req_comp, scanline + i*4, req_comp); + } + if (scanline) + STBI_FREE(scanline); + } + + return hdr_data; +} + +static int stbi__hdr_info(stbi__context *s, int *x, int *y, int *comp) +{ + char buffer[STBI__HDR_BUFLEN]; + char *token; + int valid = 0; + int dummy; + + if (!x) x = &dummy; + if (!y) y = &dummy; + if (!comp) comp = &dummy; + + if (stbi__hdr_test(s) == 0) { + stbi__rewind( s ); + return 0; + } + + for(;;) { + token = stbi__hdr_gettoken(s,buffer); + if (token[0] == 0) break; + if (strcmp(token, "FORMAT=32-bit_rle_rgbe") == 0) valid = 1; + } + + if (!valid) { + stbi__rewind( s ); + return 0; + } + token = stbi__hdr_gettoken(s,buffer); + if (strncmp(token, "-Y ", 3)) { + stbi__rewind( s ); + return 0; + } + token += 3; + *y = (int) strtol(token, &token, 10); + while (*token == ' ') ++token; + if (strncmp(token, "+X ", 3)) { + stbi__rewind( s ); + return 0; + } + token += 3; + *x = (int) strtol(token, NULL, 10); + *comp = 3; + return 1; +} +#endif // STBI_NO_HDR + +#ifndef STBI_NO_BMP +static int stbi__bmp_info(stbi__context *s, int *x, int *y, int *comp) +{ + void *p; + stbi__bmp_data info; + + info.all_a = 255; + p = stbi__bmp_parse_header(s, &info); + if (p == NULL) { + stbi__rewind( s ); + return 0; + } + if (x) *x = s->img_x; + if (y) *y = s->img_y; + if (comp) { + if (info.bpp == 24 && info.ma == 0xff000000) + *comp = 3; + else + *comp = info.ma ? 4 : 3; + } + return 1; +} +#endif + +#ifndef STBI_NO_PSD +static int stbi__psd_info(stbi__context *s, int *x, int *y, int *comp) +{ + int channelCount, dummy, depth; + if (!x) x = &dummy; + if (!y) y = &dummy; + if (!comp) comp = &dummy; + if (stbi__get32be(s) != 0x38425053) { + stbi__rewind( s ); + return 0; + } + if (stbi__get16be(s) != 1) { + stbi__rewind( s ); + return 0; + } + stbi__skip(s, 6); + channelCount = stbi__get16be(s); + if (channelCount < 0 || channelCount > 16) { + stbi__rewind( s ); + return 0; + } + *y = stbi__get32be(s); + *x = stbi__get32be(s); + depth = stbi__get16be(s); + if (depth != 8 && depth != 16) { + stbi__rewind( s ); + return 0; + } + if (stbi__get16be(s) != 3) { + stbi__rewind( s ); + return 0; + } + *comp = 4; + return 1; +} + +static int stbi__psd_is16(stbi__context *s) +{ + int channelCount, depth; + if (stbi__get32be(s) != 0x38425053) { + stbi__rewind( s ); + return 0; + } + if (stbi__get16be(s) != 1) { + stbi__rewind( s ); + return 0; + } + stbi__skip(s, 6); + channelCount = stbi__get16be(s); + if (channelCount < 0 || channelCount > 16) { + stbi__rewind( s ); + return 0; + } + STBI_NOTUSED(stbi__get32be(s)); + STBI_NOTUSED(stbi__get32be(s)); + depth = stbi__get16be(s); + if (depth != 16) { + stbi__rewind( s ); + return 0; + } + return 1; +} +#endif + +#ifndef STBI_NO_PIC +static int stbi__pic_info(stbi__context *s, int *x, int *y, int *comp) +{ + int act_comp=0,num_packets=0,chained,dummy; + stbi__pic_packet packets[10]; + + if (!x) x = &dummy; + if (!y) y = &dummy; + if (!comp) comp = &dummy; + + if (!stbi__pic_is4(s,"\x53\x80\xF6\x34")) { + stbi__rewind(s); + return 0; + } + + stbi__skip(s, 88); + + *x = stbi__get16be(s); + *y = stbi__get16be(s); + if (stbi__at_eof(s)) { + stbi__rewind( s); + return 0; + } + if ( (*x) != 0 && (1 << 28) / (*x) < (*y)) { + stbi__rewind( s ); + return 0; + } + + stbi__skip(s, 8); + + do { + stbi__pic_packet *packet; + + if (num_packets==sizeof(packets)/sizeof(packets[0])) + return 0; + + packet = &packets[num_packets++]; + chained = stbi__get8(s); + packet->size = stbi__get8(s); + packet->type = stbi__get8(s); + packet->channel = stbi__get8(s); + act_comp |= packet->channel; + + if (stbi__at_eof(s)) { + stbi__rewind( s ); + return 0; + } + if (packet->size != 8) { + stbi__rewind( s ); + return 0; + } + } while (chained); + + *comp = (act_comp & 0x10 ? 4 : 3); + + return 1; +} +#endif + +// ************************************************************************************************* +// Portable Gray Map and Portable Pixel Map loader +// by Ken Miller +// +// PGM: http://netpbm.sourceforge.net/doc/pgm.html +// PPM: http://netpbm.sourceforge.net/doc/ppm.html +// +// Known limitations: +// Does not support comments in the header section +// Does not support ASCII image data (formats P2 and P3) + +#ifndef STBI_NO_PNM + +static int stbi__pnm_test(stbi__context *s) +{ + char p, t; + p = (char) stbi__get8(s); + t = (char) stbi__get8(s); + if (p != 'P' || (t != '5' && t != '6')) { + stbi__rewind( s ); + return 0; + } + return 1; +} + +static void *stbi__pnm_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri) +{ + stbi_uc *out; + STBI_NOTUSED(ri); + + ri->bits_per_channel = stbi__pnm_info(s, (int *)&s->img_x, (int *)&s->img_y, (int *)&s->img_n); + if (ri->bits_per_channel == 0) + return 0; + + if (s->img_y > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)"); + if (s->img_x > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)"); + + *x = s->img_x; + *y = s->img_y; + if (comp) *comp = s->img_n; + + if (!stbi__mad4sizes_valid(s->img_n, s->img_x, s->img_y, ri->bits_per_channel / 8, 0)) + return stbi__errpuc("too large", "PNM too large"); + + out = (stbi_uc *) stbi__malloc_mad4(s->img_n, s->img_x, s->img_y, ri->bits_per_channel / 8, 0); + if (!out) return stbi__errpuc("outofmem", "Out of memory"); + if (!stbi__getn(s, out, s->img_n * s->img_x * s->img_y * (ri->bits_per_channel / 8))) { + STBI_FREE(out); + return stbi__errpuc("bad PNM", "PNM file truncated"); + } + + if (req_comp && req_comp != s->img_n) { + if (ri->bits_per_channel == 16) { + out = (stbi_uc *) stbi__convert_format16((stbi__uint16 *) out, s->img_n, req_comp, s->img_x, s->img_y); + } else { + out = stbi__convert_format(out, s->img_n, req_comp, s->img_x, s->img_y); + } + if (out == NULL) return out; // stbi__convert_format frees input on failure + } + return out; +} + +static int stbi__pnm_isspace(char c) +{ + return c == ' ' || c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r'; +} + +static void stbi__pnm_skip_whitespace(stbi__context *s, char *c) +{ + for (;;) { + while (!stbi__at_eof(s) && stbi__pnm_isspace(*c)) + *c = (char) stbi__get8(s); + + if (stbi__at_eof(s) || *c != '#') + break; + + while (!stbi__at_eof(s) && *c != '\n' && *c != '\r' ) + *c = (char) stbi__get8(s); + } +} + +static int stbi__pnm_isdigit(char c) +{ + return c >= '0' && c <= '9'; +} + +static int stbi__pnm_getinteger(stbi__context *s, char *c) +{ + int value = 0; + + while (!stbi__at_eof(s) && stbi__pnm_isdigit(*c)) { + value = value*10 + (*c - '0'); + *c = (char) stbi__get8(s); + if((value > 214748364) || (value == 214748364 && *c > '7')) + return stbi__err("integer parse overflow", "Parsing an integer in the PPM header overflowed a 32-bit int"); + } + + return value; +} + +static int stbi__pnm_info(stbi__context *s, int *x, int *y, int *comp) +{ + int maxv, dummy; + char c, p, t; + + if (!x) x = &dummy; + if (!y) y = &dummy; + if (!comp) comp = &dummy; + + stbi__rewind(s); + + // Get identifier + p = (char) stbi__get8(s); + t = (char) stbi__get8(s); + if (p != 'P' || (t != '5' && t != '6')) { + stbi__rewind(s); + return 0; + } + + *comp = (t == '6') ? 3 : 1; // '5' is 1-component .pgm; '6' is 3-component .ppm + + c = (char) stbi__get8(s); + stbi__pnm_skip_whitespace(s, &c); + + *x = stbi__pnm_getinteger(s, &c); // read width + if(*x == 0) + return stbi__err("invalid width", "PPM image header had zero or overflowing width"); + stbi__pnm_skip_whitespace(s, &c); + + *y = stbi__pnm_getinteger(s, &c); // read height + if (*y == 0) + return stbi__err("invalid width", "PPM image header had zero or overflowing width"); + stbi__pnm_skip_whitespace(s, &c); + + maxv = stbi__pnm_getinteger(s, &c); // read max value + if (maxv > 65535) + return stbi__err("max value > 65535", "PPM image supports only 8-bit and 16-bit images"); + else if (maxv > 255) + return 16; + else + return 8; +} + +static int stbi__pnm_is16(stbi__context *s) +{ + if (stbi__pnm_info(s, NULL, NULL, NULL) == 16) + return 1; + return 0; +} +#endif + +static int stbi__info_main(stbi__context *s, int *x, int *y, int *comp) +{ + #ifndef STBI_NO_JPEG + if (stbi__jpeg_info(s, x, y, comp)) return 1; + #endif + + #ifndef STBI_NO_PNG + if (stbi__png_info(s, x, y, comp)) return 1; + #endif + + #ifndef STBI_NO_GIF + if (stbi__gif_info(s, x, y, comp)) return 1; + #endif + + #ifndef STBI_NO_BMP + if (stbi__bmp_info(s, x, y, comp)) return 1; + #endif + + #ifndef STBI_NO_PSD + if (stbi__psd_info(s, x, y, comp)) return 1; + #endif + + #ifndef STBI_NO_PIC + if (stbi__pic_info(s, x, y, comp)) return 1; + #endif + + #ifndef STBI_NO_PNM + if (stbi__pnm_info(s, x, y, comp)) return 1; + #endif + + #ifndef STBI_NO_HDR + if (stbi__hdr_info(s, x, y, comp)) return 1; + #endif + + // test tga last because it's a crappy test! + #ifndef STBI_NO_TGA + if (stbi__tga_info(s, x, y, comp)) + return 1; + #endif + return stbi__err("unknown image type", "Image not of any known type, or corrupt"); +} + +static int stbi__is_16_main(stbi__context *s) +{ + #ifndef STBI_NO_PNG + if (stbi__png_is16(s)) return 1; + #endif + + #ifndef STBI_NO_PSD + if (stbi__psd_is16(s)) return 1; + #endif + + #ifndef STBI_NO_PNM + if (stbi__pnm_is16(s)) return 1; + #endif + return 0; +} + +#ifndef STBI_NO_STDIO +STBIDEF int stbi_info(char const *filename, int *x, int *y, int *comp) +{ + FILE *f = stbi__fopen(filename, "rb"); + int result; + if (!f) return stbi__err("can't fopen", "Unable to open file"); + result = stbi_info_from_file(f, x, y, comp); + fclose(f); + return result; +} + +STBIDEF int stbi_info_from_file(FILE *f, int *x, int *y, int *comp) +{ + int r; + stbi__context s; + long pos = ftell(f); + stbi__start_file(&s, f); + r = stbi__info_main(&s,x,y,comp); + fseek(f,pos,SEEK_SET); + return r; +} + +STBIDEF int stbi_is_16_bit(char const *filename) +{ + FILE *f = stbi__fopen(filename, "rb"); + int result; + if (!f) return stbi__err("can't fopen", "Unable to open file"); + result = stbi_is_16_bit_from_file(f); + fclose(f); + return result; +} + +STBIDEF int stbi_is_16_bit_from_file(FILE *f) +{ + int r; + stbi__context s; + long pos = ftell(f); + stbi__start_file(&s, f); + r = stbi__is_16_main(&s); + fseek(f,pos,SEEK_SET); + return r; +} +#endif // !STBI_NO_STDIO + +STBIDEF int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp) +{ + stbi__context s; + stbi__start_mem(&s,buffer,len); + return stbi__info_main(&s,x,y,comp); +} + +STBIDEF int stbi_info_from_callbacks(stbi_io_callbacks const *c, void *user, int *x, int *y, int *comp) +{ + stbi__context s; + stbi__start_callbacks(&s, (stbi_io_callbacks *) c, user); + return stbi__info_main(&s,x,y,comp); +} + +STBIDEF int stbi_is_16_bit_from_memory(stbi_uc const *buffer, int len) +{ + stbi__context s; + stbi__start_mem(&s,buffer,len); + return stbi__is_16_main(&s); +} + +STBIDEF int stbi_is_16_bit_from_callbacks(stbi_io_callbacks const *c, void *user) +{ + stbi__context s; + stbi__start_callbacks(&s, (stbi_io_callbacks *) c, user); + return stbi__is_16_main(&s); +} + +#endif // STB_IMAGE_IMPLEMENTATION + +/* + revision history: + 2.20 (2019-02-07) support utf8 filenames in Windows; fix warnings and platform ifdefs + 2.19 (2018-02-11) fix warning + 2.18 (2018-01-30) fix warnings + 2.17 (2018-01-29) change sbti__shiftsigned to avoid clang -O2 bug + 1-bit BMP + *_is_16_bit api + avoid warnings + 2.16 (2017-07-23) all functions have 16-bit variants; + STBI_NO_STDIO works again; + compilation fixes; + fix rounding in unpremultiply; + optimize vertical flip; + disable raw_len validation; + documentation fixes + 2.15 (2017-03-18) fix png-1,2,4 bug; now all Imagenet JPGs decode; + warning fixes; disable run-time SSE detection on gcc; + uniform handling of optional "return" values; + thread-safe initialization of zlib tables + 2.14 (2017-03-03) remove deprecated STBI_JPEG_OLD; fixes for Imagenet JPGs + 2.13 (2016-11-29) add 16-bit API, only supported for PNG right now + 2.12 (2016-04-02) fix typo in 2.11 PSD fix that caused crashes + 2.11 (2016-04-02) allocate large structures on the stack + remove white matting for transparent PSD + fix reported channel count for PNG & BMP + re-enable SSE2 in non-gcc 64-bit + support RGB-formatted JPEG + read 16-bit PNGs (only as 8-bit) + 2.10 (2016-01-22) avoid warning introduced in 2.09 by STBI_REALLOC_SIZED + 2.09 (2016-01-16) allow comments in PNM files + 16-bit-per-pixel TGA (not bit-per-component) + info() for TGA could break due to .hdr handling + info() for BMP to shares code instead of sloppy parse + can use STBI_REALLOC_SIZED if allocator doesn't support realloc + code cleanup + 2.08 (2015-09-13) fix to 2.07 cleanup, reading RGB PSD as RGBA + 2.07 (2015-09-13) fix compiler warnings + partial animated GIF support + limited 16-bpc PSD support + #ifdef unused functions + bug with < 92 byte PIC,PNM,HDR,TGA + 2.06 (2015-04-19) fix bug where PSD returns wrong '*comp' value + 2.05 (2015-04-19) fix bug in progressive JPEG handling, fix warning + 2.04 (2015-04-15) try to re-enable SIMD on MinGW 64-bit + 2.03 (2015-04-12) extra corruption checking (mmozeiko) + stbi_set_flip_vertically_on_load (nguillemot) + fix NEON support; fix mingw support + 2.02 (2015-01-19) fix incorrect assert, fix warning + 2.01 (2015-01-17) fix various warnings; suppress SIMD on gcc 32-bit without -msse2 + 2.00b (2014-12-25) fix STBI_MALLOC in progressive JPEG + 2.00 (2014-12-25) optimize JPG, including x86 SSE2 & NEON SIMD (ryg) + progressive JPEG (stb) + PGM/PPM support (Ken Miller) + STBI_MALLOC,STBI_REALLOC,STBI_FREE + GIF bugfix -- seemingly never worked + STBI_NO_*, STBI_ONLY_* + 1.48 (2014-12-14) fix incorrectly-named assert() + 1.47 (2014-12-14) 1/2/4-bit PNG support, both direct and paletted (Omar Cornut & stb) + optimize PNG (ryg) + fix bug in interlaced PNG with user-specified channel count (stb) + 1.46 (2014-08-26) + fix broken tRNS chunk (colorkey-style transparency) in non-paletted PNG + 1.45 (2014-08-16) + fix MSVC-ARM internal compiler error by wrapping malloc + 1.44 (2014-08-07) + various warning fixes from Ronny Chevalier + 1.43 (2014-07-15) + fix MSVC-only compiler problem in code changed in 1.42 + 1.42 (2014-07-09) + don't define _CRT_SECURE_NO_WARNINGS (affects user code) + fixes to stbi__cleanup_jpeg path + added STBI_ASSERT to avoid requiring assert.h + 1.41 (2014-06-25) + fix search&replace from 1.36 that messed up comments/error messages + 1.40 (2014-06-22) + fix gcc struct-initialization warning + 1.39 (2014-06-15) + fix to TGA optimization when req_comp != number of components in TGA; + fix to GIF loading because BMP wasn't rewinding (whoops, no GIFs in my test suite) + add support for BMP version 5 (more ignored fields) + 1.38 (2014-06-06) + suppress MSVC warnings on integer casts truncating values + fix accidental rename of 'skip' field of I/O + 1.37 (2014-06-04) + remove duplicate typedef + 1.36 (2014-06-03) + convert to header file single-file library + if de-iphone isn't set, load iphone images color-swapped instead of returning NULL + 1.35 (2014-05-27) + various warnings + fix broken STBI_SIMD path + fix bug where stbi_load_from_file no longer left file pointer in correct place + fix broken non-easy path for 32-bit BMP (possibly never used) + TGA optimization by Arseny Kapoulkine + 1.34 (unknown) + use STBI_NOTUSED in stbi__resample_row_generic(), fix one more leak in tga failure case + 1.33 (2011-07-14) + make stbi_is_hdr work in STBI_NO_HDR (as specified), minor compiler-friendly improvements + 1.32 (2011-07-13) + support for "info" function for all supported filetypes (SpartanJ) + 1.31 (2011-06-20) + a few more leak fixes, bug in PNG handling (SpartanJ) + 1.30 (2011-06-11) + added ability to load files via callbacks to accomidate custom input streams (Ben Wenger) + removed deprecated format-specific test/load functions + removed support for installable file formats (stbi_loader) -- would have been broken for IO callbacks anyway + error cases in bmp and tga give messages and don't leak (Raymond Barbiero, grisha) + fix inefficiency in decoding 32-bit BMP (David Woo) + 1.29 (2010-08-16) + various warning fixes from Aurelien Pocheville + 1.28 (2010-08-01) + fix bug in GIF palette transparency (SpartanJ) + 1.27 (2010-08-01) + cast-to-stbi_uc to fix warnings + 1.26 (2010-07-24) + fix bug in file buffering for PNG reported by SpartanJ + 1.25 (2010-07-17) + refix trans_data warning (Won Chun) + 1.24 (2010-07-12) + perf improvements reading from files on platforms with lock-heavy fgetc() + minor perf improvements for jpeg + deprecated type-specific functions so we'll get feedback if they're needed + attempt to fix trans_data warning (Won Chun) + 1.23 fixed bug in iPhone support + 1.22 (2010-07-10) + removed image *writing* support + stbi_info support from Jetro Lauha + GIF support from Jean-Marc Lienher + iPhone PNG-extensions from James Brown + warning-fixes from Nicolas Schulz and Janez Zemva (i.stbi__err. Janez (U+017D)emva) + 1.21 fix use of 'stbi_uc' in header (reported by jon blow) + 1.20 added support for Softimage PIC, by Tom Seddon + 1.19 bug in interlaced PNG corruption check (found by ryg) + 1.18 (2008-08-02) + fix a threading bug (local mutable static) + 1.17 support interlaced PNG + 1.16 major bugfix - stbi__convert_format converted one too many pixels + 1.15 initialize some fields for thread safety + 1.14 fix threadsafe conversion bug + header-file-only version (#define STBI_HEADER_FILE_ONLY before including) + 1.13 threadsafe + 1.12 const qualifiers in the API + 1.11 Support installable IDCT, colorspace conversion routines + 1.10 Fixes for 64-bit (don't use "unsigned long") + optimized upsampling by Fabian "ryg" Giesen + 1.09 Fix format-conversion for PSD code (bad global variables!) + 1.08 Thatcher Ulrich's PSD code integrated by Nicolas Schulz + 1.07 attempt to fix C++ warning/errors again + 1.06 attempt to fix C++ warning/errors again + 1.05 fix TGA loading to return correct *comp and use good luminance calc + 1.04 default float alpha is 1, not 255; use 'void *' for stbi_image_free + 1.03 bugfixes to STBI_NO_STDIO, STBI_NO_HDR + 1.02 support for (subset of) HDR files, float interface for preferred access to them + 1.01 fix bug: possible bug in handling right-side up bmps... not sure + fix bug: the stbi__bmp_load() and stbi__tga_load() functions didn't work at all + 1.00 interface to zlib that skips zlib header + 0.99 correct handling of alpha in palette + 0.98 TGA loader by lonesock; dynamically add loaders (untested) + 0.97 jpeg errors on too large a file; also catch another malloc failure + 0.96 fix detection of invalid v value - particleman@mollyrocket forum + 0.95 during header scan, seek to markers in case of padding + 0.94 STBI_NO_STDIO to disable stdio usage; rename all #defines the same + 0.93 handle jpegtran output; verbose errors + 0.92 read 4,8,16,24,32-bit BMP files of several formats + 0.91 output 24-bit Windows 3.0 BMP files + 0.90 fix a few more warnings; bump version number to approach 1.0 + 0.61 bugfixes due to Marc LeBlanc, Christopher Lloyd + 0.60 fix compiling as c++ + 0.59 fix warnings: merge Dave Moore's -Wall fixes + 0.58 fix bug: zlib uncompressed mode len/nlen was wrong endian + 0.57 fix bug: jpg last huffman symbol before marker was >9 bits but less than 16 available + 0.56 fix bug: zlib uncompressed mode len vs. nlen + 0.55 fix bug: restart_interval not initialized to 0 + 0.54 allow NULL for 'int *comp' + 0.53 fix bug in png 3->4; speedup png decoding + 0.52 png handles req_comp=3,4 directly; minor cleanup; jpeg comments + 0.51 obey req_comp requests, 1-component jpegs return as 1-component, + on 'test' only check type, not whether we support this variant + 0.50 (2006-11-19) + first released version +*/ + + +/* +------------------------------------------------------------------------------ +This software is available under 2 licenses -- choose whichever you prefer. +------------------------------------------------------------------------------ +ALTERNATIVE A - MIT License +Copyright (c) 2017 Sean Barrett +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +------------------------------------------------------------------------------ +ALTERNATIVE B - Public Domain (www.unlicense.org) +This is free and unencumbered software released into the public domain. +Anyone is free to copy, modify, publish, use, compile, sell, or distribute this +software, either in source code form or as a compiled binary, for any purpose, +commercial or non-commercial, and by any means. +In jurisdictions that recognize copyright laws, the author or authors of this +software dedicate any and all copyright interest in the software to the public +domain. We make this dedication for the benefit of the public at large and to +the detriment of our heirs and successors. We intend this dedication to be an +overt act of relinquishment in perpetuity of all present and future rights to +this software under copyright law. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +------------------------------------------------------------------------------ +*/ diff --git a/src/native/linux/stb_image_resize2.h b/src/native/linux/stb_image_resize2.h new file mode 100644 index 00000000..07989765 --- /dev/null +++ b/src/native/linux/stb_image_resize2.h @@ -0,0 +1,10679 @@ +/* stb_image_resize2 - v2.18 - public domain image resizing + + by Jeff Roberts (v2) and Jorge L Rodriguez + http://github.com/nothings/stb + + Can be threaded with the extended API. SSE2, AVX, Neon and WASM SIMD support. Only + scaling and translation is supported, no rotations or shears. + + COMPILING & LINKING + In one C/C++ file that #includes this file, do this: + #define STB_IMAGE_RESIZE_IMPLEMENTATION + before the #include. That will create the implementation in that file. + + EASY API CALLS: + Easy API downsamples w/Mitchell filter, upsamples w/cubic interpolation, clamps to edge. + + stbir_resize_uint8_srgb( input_pixels, input_w, input_h, input_stride_in_bytes, + output_pixels, output_w, output_h, output_stride_in_bytes, + pixel_layout_enum ) + + stbir_resize_uint8_linear( input_pixels, input_w, input_h, input_stride_in_bytes, + output_pixels, output_w, output_h, output_stride_in_bytes, + pixel_layout_enum ) + + stbir_resize_float_linear( input_pixels, input_w, input_h, input_stride_in_bytes, + output_pixels, output_w, output_h, output_stride_in_bytes, + pixel_layout_enum ) + + If you pass NULL or zero for the output_pixels, we will allocate the output buffer + for you and return it from the function (free with free() or STBIR_FREE). + As a special case, XX_stride_in_bytes of 0 means packed continuously in memory. + + API LEVELS + There are three levels of API - easy-to-use, medium-complexity and extended-complexity. + + See the "header file" section of the source for API documentation. + + ADDITIONAL DOCUMENTATION + + MEMORY ALLOCATION + By default, we use malloc and free for memory allocation. To override the + memory allocation, before the implementation #include, add a: + + #define STBIR_MALLOC(size,user_data) ... + #define STBIR_FREE(ptr,user_data) ... + + Each resize makes exactly one call to malloc/free (unless you use the + extended API where you can do one allocation for many resizes). Under + address sanitizer, we do separate allocations to find overread/writes. + + PERFORMANCE + This library was written with an emphasis on performance. When testing + stb_image_resize with RGBA, the fastest mode is STBIR_4CHANNEL with + STBIR_TYPE_UINT8 pixels and CLAMPed edges (which is what many other resize + libs do by default). Also, make sure SIMD is turned on of course (default + for 64-bit targets). Avoid WRAP edge mode if you want the fastest speed. + + This library also comes with profiling built-in. If you define STBIR_PROFILE, + you can use the advanced API and get low-level profiling information by + calling stbir_resize_extended_profile_info() or stbir_resize_split_profile_info() + after a resize. + + SIMD + Most of the routines have optimized SSE2, AVX, NEON and WASM versions. + + On Microsoft compilers, we automatically turn on SIMD for 64-bit x64 and + ARM; for 32-bit x86 and ARM, you select SIMD mode by defining STBIR_SSE2 or + STBIR_NEON. For AVX and AVX2, we auto-select it by detecting the /arch:AVX + or /arch:AVX2 switches. You can also always manually turn SSE2, AVX or AVX2 + support on by defining STBIR_SSE2, STBIR_AVX or STBIR_AVX2. + + On Linux, SSE2 and Neon is on by default for 64-bit x64 or ARM64. For 32-bit, + we select x86 SIMD mode by whether you have -msse2, -mavx or -mavx2 enabled + on the command line. For 32-bit ARM, you must pass -mfpu=neon-vfpv4 for both + clang and GCC, but GCC also requires an additional -mfp16-format=ieee to + automatically enable NEON. + + On x86 platforms, you can also define STBIR_FP16C to turn on FP16C instructions + for converting back and forth to half-floats. This is autoselected when we + are using AVX2. Clang and GCC also require the -mf16c switch. ARM always uses + the built-in half float hardware NEON instructions. + + You can also tell us to use multiply-add instructions with STBIR_USE_FMA. + Because x86 doesn't always have fma, we turn it off by default to maintain + determinism across all platforms. If you don't care about non-FMA determinism + and are willing to restrict yourself to more recent x86 CPUs (around the AVX + timeframe), then fma will give you around a 15% speedup. + + You can force off SIMD in all cases by defining STBIR_NO_SIMD. You can turn + off AVX or AVX2 specifically with STBIR_NO_AVX or STBIR_NO_AVX2. AVX is 10% + to 40% faster, and AVX2 is generally another 12%. + + ALPHA CHANNEL + Most of the resizing functions provide the ability to control how the alpha + channel of an image is processed. + + When alpha represents transparency, it is important that when combining + colors with filtering, the pixels should not be treated equally; they + should use a weighted average based on their alpha values. For example, + if a pixel is 1% opaque bright green and another pixel is 99% opaque + black and you average them, the average will be 50% opaque, but the + unweighted average and will be a middling green color, while the weighted + average will be nearly black. This means the unweighted version introduced + green energy that didn't exist in the source image. + + (If you want to know why this makes sense, you can work out the math for + the following: consider what happens if you alpha composite a source image + over a fixed color and then average the output, vs. if you average the + source image pixels and then composite that over the same fixed color. + Only the weighted average produces the same result as the ground truth + composite-then-average result.) + + Therefore, it is in general best to "alpha weight" the pixels when applying + filters to them. This essentially means multiplying the colors by the alpha + values before combining them, and then dividing by the alpha value at the + end. + + The computer graphics industry introduced a technique called "premultiplied + alpha" or "associated alpha" in which image colors are stored in image files + already multiplied by their alpha. This saves some math when compositing, + and also avoids the need to divide by the alpha at the end (which is quite + inefficient). However, while premultiplied alpha is common in the movie CGI + industry, it is not commonplace in other industries like videogames, and most + consumer file formats are generally expected to contain not-premultiplied + colors. For example, Photoshop saves PNG files "unpremultiplied", and web + browsers like Chrome and Firefox expect PNG images to be unpremultiplied. + + Note that there are three possibilities that might describe your image + and resize expectation: + + 1. images are not premultiplied, alpha weighting is desired + 2. images are not premultiplied, alpha weighting is not desired + 3. images are premultiplied + + Both case #2 and case #3 require the exact same math: no alpha weighting + should be applied or removed. Only case 1 requires extra math operations; + the other two cases can be handled identically. + + stb_image_resize expects case #1 by default, applying alpha weighting to + images, expecting the input images to be unpremultiplied. This is what the + COLOR+ALPHA buffer types tell the resizer to do. + + When you use the pixel layouts STBIR_RGBA, STBIR_BGRA, STBIR_ARGB, + STBIR_ABGR, STBIR_RA, or STBIR_AR you are telling us that the pixels are + non-premultiplied. In these cases, the resizer will alpha weight the colors + (effectively creating the premultiplied image), do the filtering, and then + convert back to non-premult on exit. + + When you use the pixel layouts STBIR_RGBA_PM, STBIR_BGRA_PM, STBIR_ARGB_PM, + STBIR_ABGR_PM, STBIR_RA_PM or STBIR_AR_PM, you are telling that the pixels + ARE premultiplied. In this case, the resizer doesn't have to do the + premultipling - it can filter directly on the input. This about twice as + fast as the non-premultiplied case, so it's the right option if your data is + already setup correctly. + + When you use the pixel layout STBIR_4CHANNEL or STBIR_2CHANNEL, you are + telling us that there is no channel that represents transparency; it may be + RGB and some unrelated fourth channel that has been stored in the alpha + channel, but it is actually not alpha. No special processing will be + performed. + + The difference between the generic 4 or 2 channel layouts, and the + specialized _PM versions is with the _PM versions you are telling us that + the data *is* alpha, just don't premultiply it. That's important when + using SRGB pixel formats, we need to know where the alpha is, because + it is converted linearly (rather than with the SRGB converters). + + Because alpha weighting produces the same effect as premultiplying, you + even have the option with non-premultiplied inputs to let the resizer + produce a premultiplied output. Because the intially computed alpha-weighted + output image is effectively premultiplied, this is actually more performant + than the normal path which un-premultiplies the output image as a final step. + + Finally, when converting both in and out of non-premulitplied space (for + example, when using STBIR_RGBA), we go to somewhat heroic measures to + ensure that areas with zero alpha value pixels get something reasonable + in the RGB values. If you don't care about the RGB values of zero alpha + pixels, you can call the stbir_set_non_pm_alpha_speed_over_quality() + function - this runs a premultiplied resize about 25% faster. That said, + when you really care about speed, using premultiplied pixels for both in + and out (STBIR_RGBA_PM, etc) much faster than both of these premultiplied + options. + + PIXEL LAYOUT CONVERSION + The resizer can convert from some pixel layouts to others. When using the + stbir_set_pixel_layouts(), you can, for example, specify STBIR_RGBA + on input, and STBIR_ARGB on output, and it will re-organize the channels + during the resize. Currently, you can only convert between two pixel + layouts with the same number of channels. + + DETERMINISM + We commit to being deterministic (from x64 to ARM to scalar to SIMD, etc). + This requires compiling with fast-math off (using at least /fp:precise). + Also, you must turn off fp-contracting (which turns mult+adds into fmas)! + We attempt to do this with pragmas, but with Clang, you usually want to add + -ffp-contract=off to the command line as well. + + For 32-bit x86, you must use SSE and SSE2 codegen for determinism. That is, + if the scalar x87 unit gets used at all, we immediately lose determinism. + On Microsoft Visual Studio 2008 and earlier, from what we can tell there is + no way to be deterministic in 32-bit x86 (some x87 always leaks in, even + with fp:strict). On 32-bit x86 GCC, determinism requires both -msse2 and + -fpmath=sse. + + Note that we will not be deterministic with float data containing NaNs - + the NaNs will propagate differently on different SIMD and platforms. + + If you turn on STBIR_USE_FMA, then we will be deterministic with other + fma targets, but we will differ from non-fma targets (this is unavoidable, + because a fma isn't simply an add with a mult - it also introduces a + rounding difference compared to non-fma instruction sequences. + + FLOAT PIXEL FORMAT RANGE + Any range of values can be used for the non-alpha float data that you pass + in (0 to 1, -1 to 1, whatever). However, if you are inputting float values + but *outputting* bytes or shorts, you must use a range of 0 to 1 so that we + scale back properly. The alpha channel must also be 0 to 1 for any format + that does premultiplication prior to resizing. + + Note also that with float output, using filters with negative lobes, the + output filtered values might go slightly out of range. You can define + STBIR_FLOAT_LOW_CLAMP and/or STBIR_FLOAT_HIGH_CLAMP to specify the range + to clamp to on output, if that's important. + + MAX/MIN SCALE FACTORS + The input pixel resolutions are in integers, and we do the internal pointer + resolution in size_t sized integers. However, the scale ratio from input + resolution to output resolution is calculated in float form. This means + the effective possible scale ratio is limited to 24 bits (or 16 million + to 1). As you get close to the size of the float resolution (again, 16 + million pixels wide or high), you might start seeing float inaccuracy + issues in general in the pipeline. If you have to do extreme resizes, + you can usually do this is multiple stages (using float intermediate + buffers). + + FLIPPED IMAGES + Stride is just the delta from one scanline to the next. This means you can + use a negative stride to handle inverted images (point to the final + scanline and use a negative stride). You can invert the input or output, + using negative strides. + + DEFAULT FILTERS + For functions which don't provide explicit control over what filters to + use, you can change the compile-time defaults with: + + #define STBIR_DEFAULT_FILTER_UPSAMPLE STBIR_FILTER_something + #define STBIR_DEFAULT_FILTER_DOWNSAMPLE STBIR_FILTER_something + + See stbir_filter in the header-file section for the list of filters. + + NEW FILTERS + A number of 1D filter kernels are supplied. For a list of supported + filters, see the stbir_filter enum. You can install your own filters by + using the stbir_set_filter_callbacks function. + + PROGRESS + For interactive use with slow resize operations, you can use the + scanline callbacks in the extended API. It would have to be a *very* large + image resample to need progress though - we're very fast. + + CEIL and FLOOR + In scalar mode, the only functions we use from math.h are ceilf and floorf, + but if you have your own versions, you can define the STBIR_CEILF(v) and + STBIR_FLOORF(v) macros and we'll use them instead. In SIMD, we just use + our own versions. + + ASSERT + Define STBIR_ASSERT(boolval) to override assert() and not use assert.h + + PORTING FROM VERSION 1 + The API has changed. You can continue to use the old version of stb_image_resize.h, + which is available in the "deprecated/" directory. + + If you're using the old simple-to-use API, porting is straightforward. + (For more advanced APIs, read the documentation.) + + stbir_resize_uint8(): + - call `stbir_resize_uint8_linear`, cast channel count to `stbir_pixel_layout` + + stbir_resize_float(): + - call `stbir_resize_float_linear`, cast channel count to `stbir_pixel_layout` + + stbir_resize_uint8_srgb(): + - function name is unchanged + - cast channel count to `stbir_pixel_layout` + - above is sufficient unless your image has alpha and it's not RGBA/BGRA + - in that case, follow the below instructions for stbir_resize_uint8_srgb_edgemode + + stbir_resize_uint8_srgb_edgemode() + - switch to the "medium complexity" API + - stbir_resize(), very similar API but a few more parameters: + - pixel_layout: cast channel count to `stbir_pixel_layout` + - data_type: STBIR_TYPE_UINT8_SRGB + - edge: unchanged (STBIR_EDGE_WRAP, etc.) + - filter: STBIR_FILTER_DEFAULT + - which channel is alpha is specified in stbir_pixel_layout, see enum for details + + FUTURE TODOS + * For polyphase integral filters, we just memcpy the coeffs to dupe + them, but we should indirect and use the same coeff memory. + * Add pixel layout conversions for sensible different channel counts + (maybe, 1->3/4, 3->4, 4->1, 3->1). + * For SIMD encode and decode scanline routines, do any pre-aligning + for bad input/output buffer alignments and pitch? + * For very wide scanlines, we should we do vertical strips to stay within + L2 cache. Maybe do chunks of 1K pixels at a time. There would be + some pixel reconversion, but probably dwarfed by things falling out + of cache. Probably also something possible with alternating between + scattering and gathering at high resize scales? + * Should we have a multiple MIPs at the same time function (could keep + more memory in cache during multiple resizes)? + * Rewrite the coefficient generator to do many at once. + * AVX-512 vertical kernels - worried about downclocking here. + * Convert the reincludes to macros when we know they aren't changing. + * Experiment with pivoting the horizontal and always using the + vertical filters (which are faster, but perhaps not enough to overcome + the pivot cost and the extra memory touches). Need to buffer the whole + image so have to balance memory use. + * Most of our code is internally function pointers, should we compile + all the SIMD stuff always and dynamically dispatch? + + CONTRIBUTORS + Jeff Roberts: 2.0 implementation, optimizations, SIMD + Martins Mozeiko: NEON simd, WASM simd, clang and GCC whisperer + Fabian Giesen: half float and srgb converters + Sean Barrett: API design, optimizations + Jorge L Rodriguez: Original 1.0 implementation + Aras Pranckevicius: bugfixes + Nathan Reed: warning fixes for 1.0 + + REVISIONS + 2.18 (2026-03-25) fixed coefficient calculation when skipping a coefficient off + the left side of the window, added non-aligned access safe + memcpy mode for scalar path, fixed various typos, and fixed + define error in the float clamp output mode. + 2.17 (2025-10-25) silly format bug in easy-to-use APIs. + 2.16 (2025-10-21) fixed the easy-to-use APIs to allow inverted bitmaps (negative + strides), fix vertical filter kernel callback, fix threaded + gather buffer priming (and assert). + (thanks adipose, TainZerL, and Harrison Green) + 2.15 (2025-07-17) fixed an assert in debug mode when using floats with input + callbacks, work around GCC warning when adding to null ptr + (thanks Johannes Spohr and Pyry Kovanen). + 2.14 (2025-05-09) fixed a bug using downsampling gather horizontal first, and + scatter with vertical first. + 2.13 (2025-02-27) fixed a bug when using input callbacks, turned off simd for + tiny-c, fixed some variables that should have been static, + fixes a bug when calculating temp memory with resizes that + exceed 2GB of temp memory (very large resizes). + 2.12 (2024-10-18) fix incorrect use of user_data with STBIR_FREE + 2.11 (2024-09-08) fix harmless asan warnings in 2-channel and 3-channel mode + with AVX-2, fix some weird scaling edge conditions with + point sample mode. + 2.10 (2024-07-27) fix the defines GCC and mingw for loop unroll control, + fix MSVC 32-bit arm half float routines. + 2.09 (2024-06-19) fix the defines for 32-bit ARM GCC builds (was selecting + hardware half floats). + 2.08 (2024-06-10) fix for RGB->BGR three channel flips and add SIMD (thanks + to Ryan Salsbury), fix for sub-rect resizes, use the + pragmas to control unrolling when they are available. + 2.07 (2024-05-24) fix for slow final split during threaded conversions of very + wide scanlines when downsampling (caused by extra input + converting), fix for wide scanline resamples with many + splits (int overflow), fix GCC warning. + 2.06 (2024-02-10) fix for identical width/height 3x or more down-scaling + undersampling a single row on rare resize ratios (about 1%). + 2.05 (2024-02-07) fix for 2 pixel to 1 pixel resizes with wrap (thanks Aras), + fix for output callback (thanks Julien Koenen). + 2.04 (2023-11-17) fix for rare AVX bug, shadowed symbol (thanks Nikola Smiljanic). + 2.03 (2023-11-01) ASAN and TSAN warnings fixed, minor tweaks. + 2.00 (2023-10-10) mostly new source: new api, optimizations, simd, vertical-first, etc + 2x-5x faster without simd, 4x-12x faster with simd, + in some cases, 20x to 40x faster esp resizing large to very small. + 0.96 (2019-03-04) fixed warnings + 0.95 (2017-07-23) fixed warnings + 0.94 (2017-03-18) fixed warnings + 0.93 (2017-03-03) fixed bug with certain combinations of heights + 0.92 (2017-01-02) fix integer overflow on large (>2GB) images + 0.91 (2016-04-02) fix warnings; fix handling of subpixel regions + 0.90 (2014-09-17) first released version + + LICENSE + See end of file for license information. +*/ + +#if !defined(STB_IMAGE_RESIZE_DO_HORIZONTALS) && !defined(STB_IMAGE_RESIZE_DO_VERTICALS) && !defined(STB_IMAGE_RESIZE_DO_CODERS) // for internal re-includes + +#ifndef STBIR_INCLUDE_STB_IMAGE_RESIZE2_H +#define STBIR_INCLUDE_STB_IMAGE_RESIZE2_H + +#include +#ifdef _MSC_VER +typedef unsigned char stbir_uint8; +typedef unsigned short stbir_uint16; +typedef unsigned int stbir_uint32; +typedef unsigned __int64 stbir_uint64; +#else +#include +typedef uint8_t stbir_uint8; +typedef uint16_t stbir_uint16; +typedef uint32_t stbir_uint32; +typedef uint64_t stbir_uint64; +#endif + +#ifndef STBIRDEF +#ifdef STB_IMAGE_RESIZE_STATIC +#define STBIRDEF static +#else +#ifdef __cplusplus +#define STBIRDEF extern "C" +#else +#define STBIRDEF extern +#endif +#endif +#endif + +////////////////////////////////////////////////////////////////////////////// +//// start "header file" /////////////////////////////////////////////////// +// +// Easy-to-use API: +// +// * stride is the offset between successive rows of image data +// in memory, in bytes. specify 0 for packed continuously in memory +// * colorspace is linear or sRGB as specified by function name +// * Uses the default filters +// * Uses edge mode clamped +// * returned result is 1 for success or 0 in case of an error. + + +// stbir_pixel_layout specifies: +// number of channels +// order of channels +// whether color is premultiplied by alpha +// for back compatibility, you can cast the old channel count to an stbir_pixel_layout +typedef enum +{ + STBIR_1CHANNEL = 1, + STBIR_2CHANNEL = 2, + STBIR_RGB = 3, // 3-chan, with order specified (for channel flipping) + STBIR_BGR = 0, // 3-chan, with order specified (for channel flipping) + STBIR_4CHANNEL = 5, + + STBIR_RGBA = 4, // alpha formats, where alpha is NOT premultiplied into color channels + STBIR_BGRA = 6, + STBIR_ARGB = 7, + STBIR_ABGR = 8, + STBIR_RA = 9, + STBIR_AR = 10, + + STBIR_RGBA_PM = 11, // alpha formats, where alpha is premultiplied into color channels + STBIR_BGRA_PM = 12, + STBIR_ARGB_PM = 13, + STBIR_ABGR_PM = 14, + STBIR_RA_PM = 15, + STBIR_AR_PM = 16, + + STBIR_RGBA_NO_AW = 11, // alpha formats, where NO alpha weighting is applied at all! + STBIR_BGRA_NO_AW = 12, // these are just synonyms for the _PM flags (which also do + STBIR_ARGB_NO_AW = 13, // no alpha weighting). These names just make it more clear + STBIR_ABGR_NO_AW = 14, // for some folks). + STBIR_RA_NO_AW = 15, + STBIR_AR_NO_AW = 16, + +} stbir_pixel_layout; + +//=============================================================== +// Simple-complexity API +// +// If output_pixels is NULL (0), then we will allocate the buffer and return it to you. +//-------------------------------- + +STBIRDEF unsigned char * stbir_resize_uint8_srgb( const unsigned char *input_pixels , int input_w , int input_h, int input_stride_in_bytes, + unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes, + stbir_pixel_layout pixel_type ); + +STBIRDEF unsigned char * stbir_resize_uint8_linear( const unsigned char *input_pixels , int input_w , int input_h, int input_stride_in_bytes, + unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes, + stbir_pixel_layout pixel_type ); + +STBIRDEF float * stbir_resize_float_linear( const float *input_pixels , int input_w , int input_h, int input_stride_in_bytes, + float *output_pixels, int output_w, int output_h, int output_stride_in_bytes, + stbir_pixel_layout pixel_type ); +//=============================================================== + +//=============================================================== +// Medium-complexity API +// +// This extends the easy-to-use API as follows: +// +// * Can specify the datatype - U8, U8_SRGB, U16, FLOAT, HALF_FLOAT +// * Edge wrap can selected explicitly +// * Filter can be selected explicitly +//-------------------------------- + +typedef enum +{ + STBIR_EDGE_CLAMP = 0, + STBIR_EDGE_REFLECT = 1, + STBIR_EDGE_WRAP = 2, // this edge mode is slower and uses more memory + STBIR_EDGE_ZERO = 3, +} stbir_edge; + +typedef enum +{ + STBIR_FILTER_DEFAULT = 0, // use same filter type that easy-to-use API chooses + STBIR_FILTER_BOX = 1, // A trapezoid w/1-pixel wide ramps, same result as box for integer scale ratios + STBIR_FILTER_TRIANGLE = 2, // On upsampling, produces same results as bilinear texture filtering + STBIR_FILTER_CUBICBSPLINE = 3, // The cubic b-spline (aka Mitchell-Netrevalli with B=1,C=0), gaussian-esque + STBIR_FILTER_CATMULLROM = 4, // An interpolating cubic spline + STBIR_FILTER_MITCHELL = 5, // Mitchell-Netrevalli filter with B=1/3, C=1/3 + STBIR_FILTER_POINT_SAMPLE = 6, // Simple point sampling + STBIR_FILTER_OTHER = 7, // User callback specified +} stbir_filter; + +typedef enum +{ + STBIR_TYPE_UINT8 = 0, + STBIR_TYPE_UINT8_SRGB = 1, + STBIR_TYPE_UINT8_SRGB_ALPHA = 2, // alpha channel, when present, should also be SRGB (this is very unusual) + STBIR_TYPE_UINT16 = 3, + STBIR_TYPE_FLOAT = 4, + STBIR_TYPE_HALF_FLOAT = 5 +} stbir_datatype; + +// medium api +STBIRDEF void * stbir_resize( const void *input_pixels , int input_w , int input_h, int input_stride_in_bytes, + void *output_pixels, int output_w, int output_h, int output_stride_in_bytes, + stbir_pixel_layout pixel_layout, stbir_datatype data_type, + stbir_edge edge, stbir_filter filter ); +//=============================================================== + + + +//=============================================================== +// Extended-complexity API +// +// This API exposes all resize functionality. +// +// * Separate filter types for each axis +// * Separate edge modes for each axis +// * Separate input and output data types +// * Can specify regions with subpixel correctness +// * Can specify alpha flags +// * Can specify a memory callback +// * Can specify a callback data type for pixel input and output +// * Can be threaded for a single resize +// * Can be used to resize many frames without recalculating the sampler info +// +// Use this API as follows: +// 1) Call the stbir_resize_init function on a local STBIR_RESIZE structure +// 2) Call any of the stbir_set functions +// 3) Optionally call stbir_build_samplers() if you are going to resample multiple times +// with the same input and output dimensions (like resizing video frames) +// 4) Resample by calling stbir_resize_extended(). +// 5) Call stbir_free_samplers() if you called stbir_build_samplers() +//-------------------------------- + + +// Types: + +// INPUT CALLBACK: this callback is used for input scanlines +typedef void const * stbir_input_callback( void * optional_output, void const * input_ptr, int num_pixels, int x, int y, void * context ); + +// OUTPUT CALLBACK: this callback is used for output scanlines +typedef void stbir_output_callback( void const * output_ptr, int num_pixels, int y, void * context ); + +// callbacks for user installed filters +typedef float stbir__kernel_callback( float x, float scale, void * user_data ); // centered at zero +typedef float stbir__support_callback( float scale, void * user_data ); + +// internal structure with precomputed scaling +typedef struct stbir__info stbir__info; + +typedef struct STBIR_RESIZE // use the stbir_resize_init and stbir_override functions to set these values for future compatibility +{ + void * user_data; + void const * input_pixels; + int input_w, input_h; + double input_s0, input_t0, input_s1, input_t1; + stbir_input_callback * input_cb; + void * output_pixels; + int output_w, output_h; + int output_subx, output_suby, output_subw, output_subh; + stbir_output_callback * output_cb; + int input_stride_in_bytes; + int output_stride_in_bytes; + int splits; + int fast_alpha; + int needs_rebuild; + int called_alloc; + stbir_pixel_layout input_pixel_layout_public; + stbir_pixel_layout output_pixel_layout_public; + stbir_datatype input_data_type; + stbir_datatype output_data_type; + stbir_filter horizontal_filter, vertical_filter; + stbir_edge horizontal_edge, vertical_edge; + stbir__kernel_callback * horizontal_filter_kernel; stbir__support_callback * horizontal_filter_support; + stbir__kernel_callback * vertical_filter_kernel; stbir__support_callback * vertical_filter_support; + stbir__info * samplers; +} STBIR_RESIZE; + +// extended complexity api + + +// First off, you must ALWAYS call stbir_resize_init on your resize structure before any of the other calls! +STBIRDEF void stbir_resize_init( STBIR_RESIZE * resize, + const void *input_pixels, int input_w, int input_h, int input_stride_in_bytes, // stride can be zero + void *output_pixels, int output_w, int output_h, int output_stride_in_bytes, // stride can be zero + stbir_pixel_layout pixel_layout, stbir_datatype data_type ); + +//=============================================================== +// You can update these parameters any time after resize_init and there is no cost +//-------------------------------- + +STBIRDEF void stbir_set_datatypes( STBIR_RESIZE * resize, stbir_datatype input_type, stbir_datatype output_type ); +STBIRDEF void stbir_set_pixel_callbacks( STBIR_RESIZE * resize, stbir_input_callback * input_cb, stbir_output_callback * output_cb ); // no callbacks by default +STBIRDEF void stbir_set_user_data( STBIR_RESIZE * resize, void * user_data ); // pass back STBIR_RESIZE* by default +STBIRDEF void stbir_set_buffer_ptrs( STBIR_RESIZE * resize, const void * input_pixels, int input_stride_in_bytes, void * output_pixels, int output_stride_in_bytes ); + +//=============================================================== + + +//=============================================================== +// If you call any of these functions, you will trigger a sampler rebuild! +//-------------------------------- + +STBIRDEF int stbir_set_pixel_layouts( STBIR_RESIZE * resize, stbir_pixel_layout input_pixel_layout, stbir_pixel_layout output_pixel_layout ); // sets new buffer layouts +STBIRDEF int stbir_set_edgemodes( STBIR_RESIZE * resize, stbir_edge horizontal_edge, stbir_edge vertical_edge ); // CLAMP by default + +STBIRDEF int stbir_set_filters( STBIR_RESIZE * resize, stbir_filter horizontal_filter, stbir_filter vertical_filter ); // STBIR_DEFAULT_FILTER_UPSAMPLE/DOWNSAMPLE by default +STBIRDEF int stbir_set_filter_callbacks( STBIR_RESIZE * resize, stbir__kernel_callback * horizontal_filter, stbir__support_callback * horizontal_support, stbir__kernel_callback * vertical_filter, stbir__support_callback * vertical_support ); + +STBIRDEF int stbir_set_pixel_subrect( STBIR_RESIZE * resize, int subx, int suby, int subw, int subh ); // sets both sub-regions (full regions by default) +STBIRDEF int stbir_set_input_subrect( STBIR_RESIZE * resize, double s0, double t0, double s1, double t1 ); // sets input sub-region (full region by default) +STBIRDEF int stbir_set_output_pixel_subrect( STBIR_RESIZE * resize, int subx, int suby, int subw, int subh ); // sets output sub-region (full region by default) + +// when inputting AND outputting non-premultiplied alpha pixels, we use a slower but higher quality technique +// that fills the zero alpha pixel's RGB values with something plausible. If you don't care about areas of +// zero alpha, you can call this function to get about a 25% speed improvement for STBIR_RGBA to STBIR_RGBA +// types of resizes. +STBIRDEF int stbir_set_non_pm_alpha_speed_over_quality( STBIR_RESIZE * resize, int non_pma_alpha_speed_over_quality ); +//=============================================================== + + +//=============================================================== +// You can call build_samplers to prebuild all the internal data we need to resample. +// Then, if you call resize_extended many times with the same resize, you only pay the +// cost once. +// If you do call build_samplers, you MUST call free_samplers eventually. +//-------------------------------- + +// This builds the samplers and does one allocation +STBIRDEF int stbir_build_samplers( STBIR_RESIZE * resize ); + +// You MUST call this, if you call stbir_build_samplers or stbir_build_samplers_with_splits +STBIRDEF void stbir_free_samplers( STBIR_RESIZE * resize ); +//=============================================================== + + +// And this is the main function to perform the resize synchronously on one thread. +STBIRDEF int stbir_resize_extended( STBIR_RESIZE * resize ); + + +//=============================================================== +// Use these functions for multithreading. +// 1) You call stbir_build_samplers_with_splits first on the main thread +// 2) Then stbir_resize_with_split on each thread +// 3) stbir_free_samplers when done on the main thread +//-------------------------------- + +// This will build samplers for threading. +// You can pass in the number of threads you'd like to use (try_splits). +// It returns the number of splits (threads) that you can call it with. +/// It might be less if the image resize can't be split up that many ways. + +STBIRDEF int stbir_build_samplers_with_splits( STBIR_RESIZE * resize, int try_splits ); + +// This function does a split of the resizing (you call this fuction for each +// split, on multiple threads). A split is a piece of the output resize pixel space. + +// Note that you MUST call stbir_build_samplers_with_splits before stbir_resize_extended_split! + +// Usually, you will always call stbir_resize_split with split_start as the thread_index +// and "1" for the split_count. +// But, if you have a weird situation where you MIGHT want 8 threads, but sometimes +// only 4 threads, you can use 0,2,4,6 for the split_start's and use "2" for the +// split_count each time to turn in into a 4 thread resize. (This is unusual). + +STBIRDEF int stbir_resize_extended_split( STBIR_RESIZE * resize, int split_start, int split_count ); +//=============================================================== + + +//=============================================================== +// Pixel Callbacks info: +//-------------------------------- + +// The input callback is super flexible - it calls you with the input address +// (based on the stride and base pointer), it gives you an optional_output +// pointer that you can fill, or you can just return your own pointer into +// your own data. +// +// You can also do conversion from non-supported data types if necessary - in +// this case, you ignore the input_ptr and just use the x and y parameters to +// calculate your own input_ptr based on the size of each non-supported pixel. +// (Something like the third example below.) +// +// You can also install just an input or just an output callback by setting the +// callback that you don't want to zero. +// +// First example, progress: (getting a callback that you can monitor the progress): +// void const * my_callback( void * optional_output, void const * input_ptr, int num_pixels, int x, int y, void * context ) +// { +// percentage_done = y / input_height; +// return input_ptr; // use buffer from call +// } +// +// Next example, copying: (copy from some other buffer or stream): +// void const * my_callback( void * optional_output, void const * input_ptr, int num_pixels, int x, int y, void * context ) +// { +// CopyOrStreamData( optional_output, other_data_src, num_pixels * pixel_width_in_bytes ); +// return optional_output; // return the optional buffer that we filled +// } +// +// Third example, input another buffer without copying: (zero-copy from other buffer): +// void const * my_callback( void * optional_output, void const * input_ptr, int num_pixels, int x, int y, void * context ) +// { +// void * pixels = ( (char*) other_image_base ) + ( y * other_image_stride ) + ( x * other_pixel_width_in_bytes ); +// return pixels; // return pointer to your data without copying +// } +// +// +// The output callback is considerably simpler - it just calls you so that you can dump +// out each scanline. You could even directly copy out to disk if you have a simple format +// like TGA or BMP. You can also convert to other output types here if you want. +// +// Simple example: +// void const * my_output( void * output_ptr, int num_pixels, int y, void * context ) +// { +// percentage_done = y / output_height; +// fwrite( output_ptr, pixel_width_in_bytes, num_pixels, output_file ); +// } +//=============================================================== + + + + +//=============================================================== +// optional built-in profiling API +//-------------------------------- + +#ifdef STBIR_PROFILE + +typedef struct STBIR_PROFILE_INFO +{ + stbir_uint64 total_clocks; + + // how many clocks spent (of total_clocks) in the various resize routines, along with a string description + // there are "resize_count" number of zones + stbir_uint64 clocks[ 8 ]; + char const ** descriptions; + + // count of clocks and descriptions + stbir_uint32 count; +} STBIR_PROFILE_INFO; + +// use after calling stbir_resize_extended (or stbir_build_samplers or stbir_build_samplers_with_splits) +STBIRDEF void stbir_resize_build_profile_info( STBIR_PROFILE_INFO * out_info, STBIR_RESIZE const * resize ); + +// use after calling stbir_resize_extended +STBIRDEF void stbir_resize_extended_profile_info( STBIR_PROFILE_INFO * out_info, STBIR_RESIZE const * resize ); + +// use after calling stbir_resize_extended_split +STBIRDEF void stbir_resize_split_profile_info( STBIR_PROFILE_INFO * out_info, STBIR_RESIZE const * resize, int split_start, int split_num ); + +//=============================================================== + +#endif + + +//// end header file ///////////////////////////////////////////////////// +#endif // STBIR_INCLUDE_STB_IMAGE_RESIZE2_H + +#if defined(STB_IMAGE_RESIZE_IMPLEMENTATION) || defined(STB_IMAGE_RESIZE2_IMPLEMENTATION) + +#ifndef STBIR_ASSERT +#include +#define STBIR_ASSERT(x) assert(x) +#endif + +#ifndef STBIR_MALLOC +#include +#define STBIR_MALLOC(size,user_data) ((void)(user_data), malloc(size)) +#define STBIR_FREE(ptr,user_data) ((void)(user_data), free(ptr)) +// (we used the comma operator to evaluate user_data, to avoid "unused parameter" warnings) +#endif + +#ifdef _MSC_VER + +#define stbir__inline __forceinline + +#else + +#define stbir__inline __inline__ + +// Clang address sanitizer +#if defined(__has_feature) + #if __has_feature(address_sanitizer) || __has_feature(memory_sanitizer) + #ifndef STBIR__SEPARATE_ALLOCATIONS + #define STBIR__SEPARATE_ALLOCATIONS + #endif + #endif +#endif + +#endif + +// GCC and MSVC +#if defined(__SANITIZE_ADDRESS__) + #ifndef STBIR__SEPARATE_ALLOCATIONS + #define STBIR__SEPARATE_ALLOCATIONS + #endif +#endif + +// Always turn off automatic FMA use - use STBIR_USE_FMA if you want. +// Otherwise, this is a determinism disaster. +#ifndef STBIR_DONT_CHANGE_FP_CONTRACT // override in case you don't want this behavior +#if defined(_MSC_VER) && !defined(__clang__) +#if _MSC_VER > 1200 +#pragma fp_contract(off) +#endif +#elif defined(__GNUC__) && !defined(__clang__) +#pragma GCC optimize("fp-contract=off") +#else +#pragma STDC FP_CONTRACT OFF +#endif +#endif + +#ifdef _MSC_VER +#define STBIR__UNUSED(v) (void)(v) +#else +#define STBIR__UNUSED(v) (void)sizeof(v) +#endif + +#define STBIR__ARRAY_SIZE(a) (sizeof((a))/sizeof((a)[0])) + + +#ifndef STBIR_DEFAULT_FILTER_UPSAMPLE +#define STBIR_DEFAULT_FILTER_UPSAMPLE STBIR_FILTER_CATMULLROM +#endif + +#ifndef STBIR_DEFAULT_FILTER_DOWNSAMPLE +#define STBIR_DEFAULT_FILTER_DOWNSAMPLE STBIR_FILTER_MITCHELL +#endif + + +#ifndef STBIR__HEADER_FILENAME +#define STBIR__HEADER_FILENAME "stb_image_resize2.h" +#endif + +// the internal pixel layout enums are in a different order, so we can easily do range comparisons of types +// the public pixel layout is ordered in a way that if you cast num_channels (1-4) to the enum, you get something sensible +typedef enum +{ + STBIRI_1CHANNEL = 0, + STBIRI_2CHANNEL = 1, + STBIRI_RGB = 2, + STBIRI_BGR = 3, + STBIRI_4CHANNEL = 4, + + STBIRI_RGBA = 5, + STBIRI_BGRA = 6, + STBIRI_ARGB = 7, + STBIRI_ABGR = 8, + STBIRI_RA = 9, + STBIRI_AR = 10, + + STBIRI_RGBA_PM = 11, + STBIRI_BGRA_PM = 12, + STBIRI_ARGB_PM = 13, + STBIRI_ABGR_PM = 14, + STBIRI_RA_PM = 15, + STBIRI_AR_PM = 16, +} stbir_internal_pixel_layout; + +// define the public pixel layouts to not compile inside the implementation (to avoid accidental use) +#define STBIR_BGR bad_dont_use_in_implementation +#define STBIR_1CHANNEL STBIR_BGR +#define STBIR_2CHANNEL STBIR_BGR +#define STBIR_RGB STBIR_BGR +#define STBIR_RGBA STBIR_BGR +#define STBIR_4CHANNEL STBIR_BGR +#define STBIR_BGRA STBIR_BGR +#define STBIR_ARGB STBIR_BGR +#define STBIR_ABGR STBIR_BGR +#define STBIR_RA STBIR_BGR +#define STBIR_AR STBIR_BGR +#define STBIR_RGBA_PM STBIR_BGR +#define STBIR_BGRA_PM STBIR_BGR +#define STBIR_ARGB_PM STBIR_BGR +#define STBIR_ABGR_PM STBIR_BGR +#define STBIR_RA_PM STBIR_BGR +#define STBIR_AR_PM STBIR_BGR + +// must match stbir_datatype +static unsigned char stbir__type_size[] = { + 1,1,1,2,4,2 // STBIR_TYPE_UINT8,STBIR_TYPE_UINT8_SRGB,STBIR_TYPE_UINT8_SRGB_ALPHA,STBIR_TYPE_UINT16,STBIR_TYPE_FLOAT,STBIR_TYPE_HALF_FLOAT +}; + +// When gathering, the contributors are which source pixels contribute. +// When scattering, the contributors are which destination pixels are contributed to. +typedef struct +{ + int n0; // First contributing pixel + int n1; // Last contributing pixel +} stbir__contributors; + +typedef struct +{ + int lowest; // First sample index for whole filter + int highest; // Last sample index for whole filter + int widest; // widest single set of samples for an output +} stbir__filter_extent_info; + +typedef struct +{ + int n0; // First pixel of decode buffer to write to + int n1; // Last pixel of decode that will be written to + int pixel_offset_for_input; // Pixel offset into input_scanline +} stbir__span; + +typedef struct stbir__scale_info +{ + int input_full_size; + int output_sub_size; + float scale; + float inv_scale; + float pixel_shift; // starting shift in output pixel space (in pixels) + int scale_is_rational; + stbir_uint32 scale_numerator, scale_denominator; +} stbir__scale_info; + +typedef struct +{ + stbir__contributors * contributors; + float* coefficients; + stbir__contributors * gather_prescatter_contributors; + float * gather_prescatter_coefficients; + stbir__scale_info scale_info; + float support; + stbir_filter filter_enum; + stbir__kernel_callback * filter_kernel; + stbir__support_callback * filter_support; + stbir_edge edge; + int coefficient_width; + int filter_pixel_width; + int filter_pixel_margin; + int num_contributors; + int contributors_size; + int coefficients_size; + stbir__filter_extent_info extent_info; + int is_gather; // 0 = scatter, 1 = gather with scale >= 1, 2 = gather with scale < 1 + int gather_prescatter_num_contributors; + int gather_prescatter_coefficient_width; + int gather_prescatter_contributors_size; + int gather_prescatter_coefficients_size; +} stbir__sampler; + +typedef struct +{ + stbir__contributors conservative; + int edge_sizes[2]; // this can be less than filter_pixel_margin, if the filter and scaling falls off + stbir__span spans[2]; // can be two spans, if doing input subrect with clamp mode WRAP +} stbir__extents; + +typedef struct +{ +#ifdef STBIR_PROFILE + union + { + struct { stbir_uint64 total, looping, vertical, horizontal, decode, encode, alpha, unalpha; } named; + stbir_uint64 array[8]; + } profile; + stbir_uint64 * current_zone_excluded_ptr; +#endif + float* decode_buffer; + + int ring_buffer_first_scanline; + int ring_buffer_last_scanline; + int ring_buffer_begin_index; // first_scanline is at this index in the ring buffer + int start_output_y, end_output_y; + int start_input_y, end_input_y; // used in scatter only + + #ifdef STBIR__SEPARATE_ALLOCATIONS + float** ring_buffers; // one pointer for each ring buffer + #else + float* ring_buffer; // one big buffer that we index into + #endif + + float* vertical_buffer; + + char no_cache_straddle[64]; +} stbir__per_split_info; + +typedef float * stbir__decode_pixels_func( float * decode, int width_times_channels, void const * input ); +typedef void stbir__alpha_weight_func( float * decode_buffer, int width_times_channels ); +typedef void stbir__horizontal_gather_channels_func( float * output_buffer, unsigned int output_sub_size, float const * decode_buffer, + stbir__contributors const * horizontal_contributors, float const * horizontal_coefficients, int coefficient_width ); +typedef void stbir__alpha_unweight_func(float * encode_buffer, int width_times_channels ); +typedef void stbir__encode_pixels_func( void * output, int width_times_channels, float const * encode ); + +struct stbir__info +{ +#ifdef STBIR_PROFILE + union + { + struct { stbir_uint64 total, build, alloc, horizontal, vertical, cleanup, pivot; } named; + stbir_uint64 array[7]; + } profile; + stbir_uint64 * current_zone_excluded_ptr; +#endif + stbir__sampler horizontal; + stbir__sampler vertical; + + void const * input_data; + void * output_data; + + int input_stride_bytes; + int output_stride_bytes; + int ring_buffer_length_bytes; // The length of an individual entry in the ring buffer. The total number of ring buffers is stbir__get_filter_pixel_width(filter) + int ring_buffer_num_entries; // Total number of entries in the ring buffer. + + stbir_datatype input_type; + stbir_datatype output_type; + + stbir_input_callback * in_pixels_cb; + void * user_data; + stbir_output_callback * out_pixels_cb; + + stbir__extents scanline_extents; + + void * alloced_mem; + stbir__per_split_info * split_info; // by default 1, but there will be N of these allocated based on the thread init you did + + stbir__decode_pixels_func * decode_pixels; + stbir__alpha_weight_func * alpha_weight; + stbir__horizontal_gather_channels_func * horizontal_gather_channels; + stbir__alpha_unweight_func * alpha_unweight; + stbir__encode_pixels_func * encode_pixels; + + int alloc_ring_buffer_num_entries; // Number of entries in the ring buffer that will be allocated + int splits; // count of splits + + stbir_internal_pixel_layout input_pixel_layout_internal; + stbir_internal_pixel_layout output_pixel_layout_internal; + + int input_color_and_type; + int offset_x, offset_y; // offset within output_data + int vertical_first; + int channels; + int effective_channels; // same as channels, except on RGBA/ARGB (7), or XA/AX (3) + size_t alloced_total; +}; + + +#define stbir__max_uint8_as_float 255.0f +#define stbir__max_uint16_as_float 65535.0f +#define stbir__max_uint8_as_float_inverted 3.9215689e-03f // (1.0f/255.0f) +#define stbir__max_uint16_as_float_inverted 1.5259022e-05f // (1.0f/65535.0f) +#define stbir__small_float ((float)1 / (1 << 20) / (1 << 20) / (1 << 20) / (1 << 20) / (1 << 20) / (1 << 20)) + +// min/max friendly +#define STBIR_CLAMP(x, xmin, xmax) for(;;) { \ + if ( (x) < (xmin) ) (x) = (xmin); \ + if ( (x) > (xmax) ) (x) = (xmax); \ + break; \ +} + +static stbir__inline int stbir__min(int a, int b) +{ + return a < b ? a : b; +} + +static stbir__inline int stbir__max(int a, int b) +{ + return a > b ? a : b; +} + +static float stbir__srgb_uchar_to_linear_float[256] = { + 0.000000f, 0.000304f, 0.000607f, 0.000911f, 0.001214f, 0.001518f, 0.001821f, 0.002125f, 0.002428f, 0.002732f, 0.003035f, + 0.003347f, 0.003677f, 0.004025f, 0.004391f, 0.004777f, 0.005182f, 0.005605f, 0.006049f, 0.006512f, 0.006995f, 0.007499f, + 0.008023f, 0.008568f, 0.009134f, 0.009721f, 0.010330f, 0.010960f, 0.011612f, 0.012286f, 0.012983f, 0.013702f, 0.014444f, + 0.015209f, 0.015996f, 0.016807f, 0.017642f, 0.018500f, 0.019382f, 0.020289f, 0.021219f, 0.022174f, 0.023153f, 0.024158f, + 0.025187f, 0.026241f, 0.027321f, 0.028426f, 0.029557f, 0.030713f, 0.031896f, 0.033105f, 0.034340f, 0.035601f, 0.036889f, + 0.038204f, 0.039546f, 0.040915f, 0.042311f, 0.043735f, 0.045186f, 0.046665f, 0.048172f, 0.049707f, 0.051269f, 0.052861f, + 0.054480f, 0.056128f, 0.057805f, 0.059511f, 0.061246f, 0.063010f, 0.064803f, 0.066626f, 0.068478f, 0.070360f, 0.072272f, + 0.074214f, 0.076185f, 0.078187f, 0.080220f, 0.082283f, 0.084376f, 0.086500f, 0.088656f, 0.090842f, 0.093059f, 0.095307f, + 0.097587f, 0.099899f, 0.102242f, 0.104616f, 0.107023f, 0.109462f, 0.111932f, 0.114435f, 0.116971f, 0.119538f, 0.122139f, + 0.124772f, 0.127438f, 0.130136f, 0.132868f, 0.135633f, 0.138432f, 0.141263f, 0.144128f, 0.147027f, 0.149960f, 0.152926f, + 0.155926f, 0.158961f, 0.162029f, 0.165132f, 0.168269f, 0.171441f, 0.174647f, 0.177888f, 0.181164f, 0.184475f, 0.187821f, + 0.191202f, 0.194618f, 0.198069f, 0.201556f, 0.205079f, 0.208637f, 0.212231f, 0.215861f, 0.219526f, 0.223228f, 0.226966f, + 0.230740f, 0.234551f, 0.238398f, 0.242281f, 0.246201f, 0.250158f, 0.254152f, 0.258183f, 0.262251f, 0.266356f, 0.270498f, + 0.274677f, 0.278894f, 0.283149f, 0.287441f, 0.291771f, 0.296138f, 0.300544f, 0.304987f, 0.309469f, 0.313989f, 0.318547f, + 0.323143f, 0.327778f, 0.332452f, 0.337164f, 0.341914f, 0.346704f, 0.351533f, 0.356400f, 0.361307f, 0.366253f, 0.371238f, + 0.376262f, 0.381326f, 0.386430f, 0.391573f, 0.396755f, 0.401978f, 0.407240f, 0.412543f, 0.417885f, 0.423268f, 0.428691f, + 0.434154f, 0.439657f, 0.445201f, 0.450786f, 0.456411f, 0.462077f, 0.467784f, 0.473532f, 0.479320f, 0.485150f, 0.491021f, + 0.496933f, 0.502887f, 0.508881f, 0.514918f, 0.520996f, 0.527115f, 0.533276f, 0.539480f, 0.545725f, 0.552011f, 0.558340f, + 0.564712f, 0.571125f, 0.577581f, 0.584078f, 0.590619f, 0.597202f, 0.603827f, 0.610496f, 0.617207f, 0.623960f, 0.630757f, + 0.637597f, 0.644480f, 0.651406f, 0.658375f, 0.665387f, 0.672443f, 0.679543f, 0.686685f, 0.693872f, 0.701102f, 0.708376f, + 0.715694f, 0.723055f, 0.730461f, 0.737911f, 0.745404f, 0.752942f, 0.760525f, 0.768151f, 0.775822f, 0.783538f, 0.791298f, + 0.799103f, 0.806952f, 0.814847f, 0.822786f, 0.830770f, 0.838799f, 0.846873f, 0.854993f, 0.863157f, 0.871367f, 0.879622f, + 0.887923f, 0.896269f, 0.904661f, 0.913099f, 0.921582f, 0.930111f, 0.938686f, 0.947307f, 0.955974f, 0.964686f, 0.973445f, + 0.982251f, 0.991102f, 1.0f +}; + +typedef union +{ + unsigned int u; + float f; +} stbir__FP32; + +// From https://gist.github.com/rygorous/2203834 + +static const stbir_uint32 fp32_to_srgb8_tab4[104] = { + 0x0073000d, 0x007a000d, 0x0080000d, 0x0087000d, 0x008d000d, 0x0094000d, 0x009a000d, 0x00a1000d, + 0x00a7001a, 0x00b4001a, 0x00c1001a, 0x00ce001a, 0x00da001a, 0x00e7001a, 0x00f4001a, 0x0101001a, + 0x010e0033, 0x01280033, 0x01410033, 0x015b0033, 0x01750033, 0x018f0033, 0x01a80033, 0x01c20033, + 0x01dc0067, 0x020f0067, 0x02430067, 0x02760067, 0x02aa0067, 0x02dd0067, 0x03110067, 0x03440067, + 0x037800ce, 0x03df00ce, 0x044600ce, 0x04ad00ce, 0x051400ce, 0x057b00c5, 0x05dd00bc, 0x063b00b5, + 0x06970158, 0x07420142, 0x07e30130, 0x087b0120, 0x090b0112, 0x09940106, 0x0a1700fc, 0x0a9500f2, + 0x0b0f01cb, 0x0bf401ae, 0x0ccb0195, 0x0d950180, 0x0e56016e, 0x0f0d015e, 0x0fbc0150, 0x10630143, + 0x11070264, 0x1238023e, 0x1357021d, 0x14660201, 0x156601e9, 0x165a01d3, 0x174401c0, 0x182401af, + 0x18fe0331, 0x1a9602fe, 0x1c1502d2, 0x1d7e02ad, 0x1ed4028d, 0x201a0270, 0x21520256, 0x227d0240, + 0x239f0443, 0x25c003fe, 0x27bf03c4, 0x29a10392, 0x2b6a0367, 0x2d1d0341, 0x2ebe031f, 0x304d0300, + 0x31d105b0, 0x34a80555, 0x37520507, 0x39d504c5, 0x3c37048b, 0x3e7c0458, 0x40a8042a, 0x42bd0401, + 0x44c20798, 0x488e071e, 0x4c1c06b6, 0x4f76065d, 0x52a50610, 0x55ac05cc, 0x5892058f, 0x5b590559, + 0x5e0c0a23, 0x631c0980, 0x67db08f6, 0x6c55087f, 0x70940818, 0x74a007bd, 0x787d076c, 0x7c330723, +}; + +static stbir__inline stbir_uint8 stbir__linear_to_srgb_uchar(float in) +{ + static const stbir__FP32 almostone = { 0x3f7fffff }; // 1-eps + static const stbir__FP32 minval = { (127-13) << 23 }; + stbir_uint32 tab,bias,scale,t; + stbir__FP32 f; + + // Clamp to [2^(-13), 1-eps]; these two values map to 0 and 1, respectively. + // The tests are carefully written so that NaNs map to 0, same as in the reference + // implementation. + if (!(in > minval.f)) // written this way to catch NaNs + return 0; + if (in > almostone.f) + return 255; + + // Do the table lookup and unpack bias, scale + f.f = in; + tab = fp32_to_srgb8_tab4[(f.u - minval.u) >> 20]; + bias = (tab >> 16) << 9; + scale = tab & 0xffff; + + // Grab next-highest mantissa bits and perform linear interpolation + t = (f.u >> 12) & 0xff; + return (unsigned char) ((bias + scale*t) >> 16); +} + +#ifndef STBIR_FORCE_GATHER_FILTER_SCANLINES_AMOUNT +#define STBIR_FORCE_GATHER_FILTER_SCANLINES_AMOUNT 32 // when downsampling and <= 32 scanlines of buffering, use gather. gather used down to 1/8th scaling for 25% win. +#endif + +#ifndef STBIR_FORCE_MINIMUM_SCANLINES_FOR_SPLITS +#define STBIR_FORCE_MINIMUM_SCANLINES_FOR_SPLITS 4 // when threading, what is the minimum number of scanlines for a split? +#endif + +#define STBIR_INPUT_CALLBACK_PADDING 3 + +#ifdef _M_IX86_FP +#if ( _M_IX86_FP >= 1 ) +#ifndef STBIR_SSE +#define STBIR_SSE +#endif +#endif +#endif + +#ifdef __TINYC__ + // tiny c has no intrinsics yet - this can become a version check if they add them + #define STBIR_NO_SIMD +#endif + +#if defined(_x86_64) || defined( __x86_64__ ) || defined( _M_X64 ) || defined(__x86_64) || defined(_M_AMD64) || defined(__SSE2__) || defined(STBIR_SSE) || defined(STBIR_SSE2) + #ifndef STBIR_SSE2 + #define STBIR_SSE2 + #endif + #if defined(__AVX__) || defined(STBIR_AVX2) + #ifndef STBIR_AVX + #ifndef STBIR_NO_AVX + #define STBIR_AVX + #endif + #endif + #endif + #if defined(__AVX2__) || defined(STBIR_AVX2) + #ifndef STBIR_NO_AVX2 + #ifndef STBIR_AVX2 + #define STBIR_AVX2 + #endif + #if defined( _MSC_VER ) && !defined(__clang__) + #ifndef STBIR_FP16C // FP16C instructions are on all AVX2 cpus, so we can autoselect it here on microsoft - clang needs -mf16c + #define STBIR_FP16C + #endif + #endif + #endif + #endif + #ifdef __F16C__ + #ifndef STBIR_FP16C // turn on FP16C instructions if the define is set (for clang and gcc) + #define STBIR_FP16C + #endif + #endif +#endif + +#if defined( _M_ARM64 ) || defined( __aarch64__ ) || defined( __arm64__ ) || ((__ARM_NEON_FP & 4) != 0) || defined(__ARM_NEON__) +#ifndef STBIR_NEON +#define STBIR_NEON +#endif +#endif + +#if defined(_M_ARM) || defined(__arm__) +#ifdef STBIR_USE_FMA +#undef STBIR_USE_FMA // no FMA for 32-bit arm on MSVC +#endif +#endif + +#if defined(__wasm__) && defined(__wasm_simd128__) +#ifndef STBIR_WASM +#define STBIR_WASM +#endif +#endif + +// restrict pointers for the output pointers, other loop and unroll control +#if defined( _MSC_VER ) && !defined(__clang__) + #define STBIR_STREAMOUT_PTR( star ) star __restrict + #define STBIR_NO_UNROLL( ptr ) __assume(ptr) // this oddly keeps msvc from unrolling a loop + #if _MSC_VER >= 1900 + #define STBIR_NO_UNROLL_LOOP_START __pragma(loop( no_vector )) + #else + #define STBIR_NO_UNROLL_LOOP_START + #endif +#elif defined( __clang__ ) + #define STBIR_STREAMOUT_PTR( star ) star __restrict__ + #define STBIR_NO_UNROLL( ptr ) __asm__ (""::"r"(ptr)) + #if ( __clang_major__ >= 4 ) || ( ( __clang_major__ >= 3 ) && ( __clang_minor__ >= 5 ) ) + #define STBIR_NO_UNROLL_LOOP_START _Pragma("clang loop unroll(disable)") _Pragma("clang loop vectorize(disable)") + #else + #define STBIR_NO_UNROLL_LOOP_START + #endif +#elif defined( __GNUC__ ) + #define STBIR_STREAMOUT_PTR( star ) star __restrict__ + #define STBIR_NO_UNROLL( ptr ) __asm__ (""::"r"(ptr)) + #if __GNUC__ >= 14 + #define STBIR_NO_UNROLL_LOOP_START _Pragma("GCC unroll 0") _Pragma("GCC novector") + #else + #define STBIR_NO_UNROLL_LOOP_START + #endif + #define STBIR_NO_UNROLL_LOOP_START_INF_FOR +#else + #define STBIR_STREAMOUT_PTR( star ) star + #define STBIR_NO_UNROLL( ptr ) + #define STBIR_NO_UNROLL_LOOP_START +#endif + +#ifndef STBIR_NO_UNROLL_LOOP_START_INF_FOR +#define STBIR_NO_UNROLL_LOOP_START_INF_FOR STBIR_NO_UNROLL_LOOP_START +#endif + +#ifdef STBIR_NO_SIMD // force simd off for whatever reason + +// force simd off overrides everything else, so clear it all + +#ifdef STBIR_SSE2 +#undef STBIR_SSE2 +#endif + +#ifdef STBIR_AVX +#undef STBIR_AVX +#endif + +#ifdef STBIR_NEON +#undef STBIR_NEON +#endif + +#ifdef STBIR_AVX2 +#undef STBIR_AVX2 +#endif + +#ifdef STBIR_FP16C +#undef STBIR_FP16C +#endif + +#ifdef STBIR_WASM +#undef STBIR_WASM +#endif + +#ifdef STBIR_SIMD +#undef STBIR_SIMD +#endif + +#else // STBIR_SIMD + +#ifdef STBIR_SSE2 + #include + + #define stbir__simdf __m128 + #define stbir__simdi __m128i + + #define stbir_simdi_castf( reg ) _mm_castps_si128(reg) + #define stbir_simdf_casti( reg ) _mm_castsi128_ps(reg) + + #define stbir__simdf_load( reg, ptr ) (reg) = _mm_loadu_ps( (float const*)(ptr) ) + #define stbir__simdi_load( reg, ptr ) (reg) = _mm_loadu_si128 ( (stbir__simdi const*)(ptr) ) + #define stbir__simdf_load1( out, ptr ) (out) = _mm_load_ss( (float const*)(ptr) ) // top values can be random (not denormal or nan for perf) + #define stbir__simdi_load1( out, ptr ) (out) = _mm_castps_si128( _mm_load_ss( (float const*)(ptr) )) + #define stbir__simdf_load1z( out, ptr ) (out) = _mm_load_ss( (float const*)(ptr) ) // top values must be zero + #define stbir__simdf_frep4( fvar ) _mm_set_ps1( fvar ) + #define stbir__simdf_load1frep4( out, fvar ) (out) = _mm_set_ps1( fvar ) + #define stbir__simdf_load2( out, ptr ) (out) = _mm_castsi128_ps( _mm_loadl_epi64( (__m128i*)(ptr)) ) // top values can be random (not denormal or nan for perf) + #define stbir__simdf_load2z( out, ptr ) (out) = _mm_castsi128_ps( _mm_loadl_epi64( (__m128i*)(ptr)) ) // top values must be zero + #define stbir__simdf_load2hmerge( out, reg, ptr ) (out) = _mm_castpd_ps(_mm_loadh_pd( _mm_castps_pd(reg), (double*)(ptr) )) + + #define stbir__simdf_zeroP() _mm_setzero_ps() + #define stbir__simdf_zero( reg ) (reg) = _mm_setzero_ps() + + #define stbir__simdf_store( ptr, reg ) _mm_storeu_ps( (float*)(ptr), reg ) + #define stbir__simdf_store1( ptr, reg ) _mm_store_ss( (float*)(ptr), reg ) + #define stbir__simdf_store2( ptr, reg ) _mm_storel_epi64( (__m128i*)(ptr), _mm_castps_si128(reg) ) + #define stbir__simdf_store2h( ptr, reg ) _mm_storeh_pd( (double*)(ptr), _mm_castps_pd(reg) ) + + #define stbir__simdi_store( ptr, reg ) _mm_storeu_si128( (__m128i*)(ptr), reg ) + #define stbir__simdi_store1( ptr, reg ) _mm_store_ss( (float*)(ptr), _mm_castsi128_ps(reg) ) + #define stbir__simdi_store2( ptr, reg ) _mm_storel_epi64( (__m128i*)(ptr), (reg) ) + + #define stbir__prefetch( ptr ) _mm_prefetch((char*)(ptr), _MM_HINT_T0 ) + + #define stbir__simdi_expand_u8_to_u32(out0,out1,out2,out3,ireg) \ + { \ + stbir__simdi zero = _mm_setzero_si128(); \ + out2 = _mm_unpacklo_epi8( ireg, zero ); \ + out3 = _mm_unpackhi_epi8( ireg, zero ); \ + out0 = _mm_unpacklo_epi16( out2, zero ); \ + out1 = _mm_unpackhi_epi16( out2, zero ); \ + out2 = _mm_unpacklo_epi16( out3, zero ); \ + out3 = _mm_unpackhi_epi16( out3, zero ); \ + } + +#define stbir__simdi_expand_u8_to_1u32(out,ireg) \ + { \ + stbir__simdi zero = _mm_setzero_si128(); \ + out = _mm_unpacklo_epi8( ireg, zero ); \ + out = _mm_unpacklo_epi16( out, zero ); \ + } + + #define stbir__simdi_expand_u16_to_u32(out0,out1,ireg) \ + { \ + stbir__simdi zero = _mm_setzero_si128(); \ + out0 = _mm_unpacklo_epi16( ireg, zero ); \ + out1 = _mm_unpackhi_epi16( ireg, zero ); \ + } + + #define stbir__simdf_convert_float_to_i32( i, f ) (i) = _mm_cvttps_epi32(f) + #define stbir__simdf_convert_float_to_int( f ) _mm_cvtt_ss2si(f) + #define stbir__simdf_convert_float_to_uint8( f ) ((unsigned char)_mm_cvtsi128_si32(_mm_cvttps_epi32(_mm_max_ps(_mm_min_ps(f,STBIR__CONSTF(STBIR_max_uint8_as_float)),_mm_setzero_ps())))) + #define stbir__simdf_convert_float_to_short( f ) ((unsigned short)_mm_cvtsi128_si32(_mm_cvttps_epi32(_mm_max_ps(_mm_min_ps(f,STBIR__CONSTF(STBIR_max_uint16_as_float)),_mm_setzero_ps())))) + + #define stbir__simdi_to_int( i ) _mm_cvtsi128_si32(i) + #define stbir__simdi_convert_i32_to_float(out, ireg) (out) = _mm_cvtepi32_ps( ireg ) + #define stbir__simdf_add( out, reg0, reg1 ) (out) = _mm_add_ps( reg0, reg1 ) + #define stbir__simdf_mult( out, reg0, reg1 ) (out) = _mm_mul_ps( reg0, reg1 ) + #define stbir__simdf_mult_mem( out, reg, ptr ) (out) = _mm_mul_ps( reg, _mm_loadu_ps( (float const*)(ptr) ) ) + #define stbir__simdf_mult1_mem( out, reg, ptr ) (out) = _mm_mul_ss( reg, _mm_load_ss( (float const*)(ptr) ) ) + #define stbir__simdf_add_mem( out, reg, ptr ) (out) = _mm_add_ps( reg, _mm_loadu_ps( (float const*)(ptr) ) ) + #define stbir__simdf_add1_mem( out, reg, ptr ) (out) = _mm_add_ss( reg, _mm_load_ss( (float const*)(ptr) ) ) + + #ifdef STBIR_USE_FMA // not on by default to maintain bit identical simd to non-simd + #include + #define stbir__simdf_madd( out, add, mul1, mul2 ) (out) = _mm_fmadd_ps( mul1, mul2, add ) + #define stbir__simdf_madd1( out, add, mul1, mul2 ) (out) = _mm_fmadd_ss( mul1, mul2, add ) + #define stbir__simdf_madd_mem( out, add, mul, ptr ) (out) = _mm_fmadd_ps( mul, _mm_loadu_ps( (float const*)(ptr) ), add ) + #define stbir__simdf_madd1_mem( out, add, mul, ptr ) (out) = _mm_fmadd_ss( mul, _mm_load_ss( (float const*)(ptr) ), add ) + #else + #define stbir__simdf_madd( out, add, mul1, mul2 ) (out) = _mm_add_ps( add, _mm_mul_ps( mul1, mul2 ) ) + #define stbir__simdf_madd1( out, add, mul1, mul2 ) (out) = _mm_add_ss( add, _mm_mul_ss( mul1, mul2 ) ) + #define stbir__simdf_madd_mem( out, add, mul, ptr ) (out) = _mm_add_ps( add, _mm_mul_ps( mul, _mm_loadu_ps( (float const*)(ptr) ) ) ) + #define stbir__simdf_madd1_mem( out, add, mul, ptr ) (out) = _mm_add_ss( add, _mm_mul_ss( mul, _mm_load_ss( (float const*)(ptr) ) ) ) + #endif + + #define stbir__simdf_add1( out, reg0, reg1 ) (out) = _mm_add_ss( reg0, reg1 ) + #define stbir__simdf_mult1( out, reg0, reg1 ) (out) = _mm_mul_ss( reg0, reg1 ) + + #define stbir__simdf_and( out, reg0, reg1 ) (out) = _mm_and_ps( reg0, reg1 ) + #define stbir__simdf_or( out, reg0, reg1 ) (out) = _mm_or_ps( reg0, reg1 ) + + #define stbir__simdf_min( out, reg0, reg1 ) (out) = _mm_min_ps( reg0, reg1 ) + #define stbir__simdf_max( out, reg0, reg1 ) (out) = _mm_max_ps( reg0, reg1 ) + #define stbir__simdf_min1( out, reg0, reg1 ) (out) = _mm_min_ss( reg0, reg1 ) + #define stbir__simdf_max1( out, reg0, reg1 ) (out) = _mm_max_ss( reg0, reg1 ) + + #define stbir__simdf_0123ABCDto3ABx( out, reg0, reg1 ) (out)=_mm_castsi128_ps( _mm_shuffle_epi32( _mm_castps_si128( _mm_shuffle_ps( reg1,reg0, (0<<0) + (1<<2) + (2<<4) + (3<<6) )), (3<<0) + (0<<2) + (1<<4) + (2<<6) ) ) + #define stbir__simdf_0123ABCDto23Ax( out, reg0, reg1 ) (out)=_mm_castsi128_ps( _mm_shuffle_epi32( _mm_castps_si128( _mm_shuffle_ps( reg1,reg0, (0<<0) + (1<<2) + (2<<4) + (3<<6) )), (2<<0) + (3<<2) + (0<<4) + (1<<6) ) ) + + static const stbir__simdf STBIR_zeroones = { 0.0f,1.0f,0.0f,1.0f }; + static const stbir__simdf STBIR_onezeros = { 1.0f,0.0f,1.0f,0.0f }; + #define stbir__simdf_aaa1( out, alp, ones ) (out)=_mm_castsi128_ps( _mm_shuffle_epi32( _mm_castps_si128( _mm_movehl_ps( ones, alp ) ), (1<<0) + (1<<2) + (1<<4) + (2<<6) ) ) + #define stbir__simdf_1aaa( out, alp, ones ) (out)=_mm_castsi128_ps( _mm_shuffle_epi32( _mm_castps_si128( _mm_movelh_ps( ones, alp ) ), (0<<0) + (2<<2) + (2<<4) + (2<<6) ) ) + #define stbir__simdf_a1a1( out, alp, ones) (out) = _mm_or_ps( _mm_castsi128_ps( _mm_srli_epi64( _mm_castps_si128(alp), 32 ) ), STBIR_zeroones ) + #define stbir__simdf_1a1a( out, alp, ones) (out) = _mm_or_ps( _mm_castsi128_ps( _mm_slli_epi64( _mm_castps_si128(alp), 32 ) ), STBIR_onezeros ) + + #define stbir__simdf_swiz( reg, one, two, three, four ) _mm_castsi128_ps( _mm_shuffle_epi32( _mm_castps_si128( reg ), (one<<0) + (two<<2) + (three<<4) + (four<<6) ) ) + + #define stbir__simdi_and( out, reg0, reg1 ) (out) = _mm_and_si128( reg0, reg1 ) + #define stbir__simdi_or( out, reg0, reg1 ) (out) = _mm_or_si128( reg0, reg1 ) + #define stbir__simdi_16madd( out, reg0, reg1 ) (out) = _mm_madd_epi16( reg0, reg1 ) + + #define stbir__simdf_pack_to_8bytes(out,aa,bb) \ + { \ + stbir__simdf af,bf; \ + stbir__simdi a,b; \ + af = _mm_min_ps( aa, STBIR_max_uint8_as_float ); \ + bf = _mm_min_ps( bb, STBIR_max_uint8_as_float ); \ + af = _mm_max_ps( af, _mm_setzero_ps() ); \ + bf = _mm_max_ps( bf, _mm_setzero_ps() ); \ + a = _mm_cvttps_epi32( af ); \ + b = _mm_cvttps_epi32( bf ); \ + a = _mm_packs_epi32( a, b ); \ + out = _mm_packus_epi16( a, a ); \ + } + + #define stbir__simdf_load4_transposed( o0, o1, o2, o3, ptr ) \ + stbir__simdf_load( o0, (ptr) ); \ + stbir__simdf_load( o1, (ptr)+4 ); \ + stbir__simdf_load( o2, (ptr)+8 ); \ + stbir__simdf_load( o3, (ptr)+12 ); \ + { \ + __m128 tmp0, tmp1, tmp2, tmp3; \ + tmp0 = _mm_unpacklo_ps(o0, o1); \ + tmp2 = _mm_unpacklo_ps(o2, o3); \ + tmp1 = _mm_unpackhi_ps(o0, o1); \ + tmp3 = _mm_unpackhi_ps(o2, o3); \ + o0 = _mm_movelh_ps(tmp0, tmp2); \ + o1 = _mm_movehl_ps(tmp2, tmp0); \ + o2 = _mm_movelh_ps(tmp1, tmp3); \ + o3 = _mm_movehl_ps(tmp3, tmp1); \ + } + + #define stbir__interleave_pack_and_store_16_u8( ptr, r0, r1, r2, r3 ) \ + r0 = _mm_packs_epi32( r0, r1 ); \ + r2 = _mm_packs_epi32( r2, r3 ); \ + r1 = _mm_unpacklo_epi16( r0, r2 ); \ + r3 = _mm_unpackhi_epi16( r0, r2 ); \ + r0 = _mm_unpacklo_epi16( r1, r3 ); \ + r2 = _mm_unpackhi_epi16( r1, r3 ); \ + r0 = _mm_packus_epi16( r0, r2 ); \ + stbir__simdi_store( ptr, r0 ); \ + + #define stbir__simdi_32shr( out, reg, imm ) out = _mm_srli_epi32( reg, imm ) + + #if defined(_MSC_VER) && !defined(__clang__) + // msvc inits with 8 bytes + #define STBIR__CONST_32_TO_8( v ) (char)(unsigned char)((v)&255),(char)(unsigned char)(((v)>>8)&255),(char)(unsigned char)(((v)>>16)&255),(char)(unsigned char)(((v)>>24)&255) + #define STBIR__CONST_4_32i( v ) STBIR__CONST_32_TO_8( v ), STBIR__CONST_32_TO_8( v ), STBIR__CONST_32_TO_8( v ), STBIR__CONST_32_TO_8( v ) + #define STBIR__CONST_4d_32i( v0, v1, v2, v3 ) STBIR__CONST_32_TO_8( v0 ), STBIR__CONST_32_TO_8( v1 ), STBIR__CONST_32_TO_8( v2 ), STBIR__CONST_32_TO_8( v3 ) + #else + // everything else inits with long long's + #define STBIR__CONST_4_32i( v ) (long long)((((stbir_uint64)(stbir_uint32)(v))<<32)|((stbir_uint64)(stbir_uint32)(v))),(long long)((((stbir_uint64)(stbir_uint32)(v))<<32)|((stbir_uint64)(stbir_uint32)(v))) + #define STBIR__CONST_4d_32i( v0, v1, v2, v3 ) (long long)((((stbir_uint64)(stbir_uint32)(v1))<<32)|((stbir_uint64)(stbir_uint32)(v0))),(long long)((((stbir_uint64)(stbir_uint32)(v3))<<32)|((stbir_uint64)(stbir_uint32)(v2))) + #endif + + #define STBIR__SIMDF_CONST(var, x) stbir__simdf var = { x, x, x, x } + #define STBIR__SIMDI_CONST(var, x) stbir__simdi var = { STBIR__CONST_4_32i(x) } + #define STBIR__CONSTF(var) (var) + #define STBIR__CONSTI(var) (var) + + #if defined(STBIR_AVX) || defined(__SSE4_1__) + #include + #define stbir__simdf_pack_to_8words(out,reg0,reg1) out = _mm_packus_epi32(_mm_cvttps_epi32(_mm_max_ps(_mm_min_ps(reg0,STBIR__CONSTF(STBIR_max_uint16_as_float)),_mm_setzero_ps())), _mm_cvttps_epi32(_mm_max_ps(_mm_min_ps(reg1,STBIR__CONSTF(STBIR_max_uint16_as_float)),_mm_setzero_ps()))) + #else + static STBIR__SIMDI_CONST(stbir__s32_32768, 32768); + static STBIR__SIMDI_CONST(stbir__s16_32768, ((32768<<16)|32768)); + + #define stbir__simdf_pack_to_8words(out,reg0,reg1) \ + { \ + stbir__simdi tmp0,tmp1; \ + tmp0 = _mm_cvttps_epi32(_mm_max_ps(_mm_min_ps(reg0,STBIR__CONSTF(STBIR_max_uint16_as_float)),_mm_setzero_ps())); \ + tmp1 = _mm_cvttps_epi32(_mm_max_ps(_mm_min_ps(reg1,STBIR__CONSTF(STBIR_max_uint16_as_float)),_mm_setzero_ps())); \ + tmp0 = _mm_sub_epi32( tmp0, stbir__s32_32768 ); \ + tmp1 = _mm_sub_epi32( tmp1, stbir__s32_32768 ); \ + out = _mm_packs_epi32( tmp0, tmp1 ); \ + out = _mm_sub_epi16( out, stbir__s16_32768 ); \ + } + + #endif + + #define STBIR_SIMD + + // if we detect AVX, set the simd8 defines + #ifdef STBIR_AVX + #include + #define STBIR_SIMD8 + #define stbir__simdf8 __m256 + #define stbir__simdi8 __m256i + #define stbir__simdf8_load( out, ptr ) (out) = _mm256_loadu_ps( (float const *)(ptr) ) + #define stbir__simdi8_load( out, ptr ) (out) = _mm256_loadu_si256( (__m256i const *)(ptr) ) + #define stbir__simdf8_mult( out, a, b ) (out) = _mm256_mul_ps( (a), (b) ) + #define stbir__simdf8_store( ptr, out ) _mm256_storeu_ps( (float*)(ptr), out ) + #define stbir__simdi8_store( ptr, reg ) _mm256_storeu_si256( (__m256i*)(ptr), reg ) + #define stbir__simdf8_frep8( fval ) _mm256_set1_ps( fval ) + + #define stbir__simdf8_min( out, reg0, reg1 ) (out) = _mm256_min_ps( reg0, reg1 ) + #define stbir__simdf8_max( out, reg0, reg1 ) (out) = _mm256_max_ps( reg0, reg1 ) + + #define stbir__simdf8_add4halves( out, bot4, top8 ) (out) = _mm_add_ps( bot4, _mm256_extractf128_ps( top8, 1 ) ) + #define stbir__simdf8_mult_mem( out, reg, ptr ) (out) = _mm256_mul_ps( reg, _mm256_loadu_ps( (float const*)(ptr) ) ) + #define stbir__simdf8_add_mem( out, reg, ptr ) (out) = _mm256_add_ps( reg, _mm256_loadu_ps( (float const*)(ptr) ) ) + #define stbir__simdf8_add( out, a, b ) (out) = _mm256_add_ps( a, b ) + #define stbir__simdf8_load1b( out, ptr ) (out) = _mm256_broadcast_ss( ptr ) + #define stbir__simdf_load1rep4( out, ptr ) (out) = _mm_broadcast_ss( ptr ) // avx load instruction + + #define stbir__simdi8_convert_i32_to_float(out, ireg) (out) = _mm256_cvtepi32_ps( ireg ) + #define stbir__simdf8_convert_float_to_i32( i, f ) (i) = _mm256_cvttps_epi32(f) + + #define stbir__simdf8_bot4s( out, a, b ) (out) = _mm256_permute2f128_ps(a,b, (0<<0)+(2<<4) ) + #define stbir__simdf8_top4s( out, a, b ) (out) = _mm256_permute2f128_ps(a,b, (1<<0)+(3<<4) ) + + #define stbir__simdf8_gettop4( reg ) _mm256_extractf128_ps(reg,1) + + #ifdef STBIR_AVX2 + + #define stbir__simdi8_expand_u8_to_u32(out0,out1,ireg) \ + { \ + stbir__simdi8 a, zero =_mm256_setzero_si256();\ + a = _mm256_permute4x64_epi64( _mm256_unpacklo_epi8( _mm256_permute4x64_epi64(_mm256_castsi128_si256(ireg),(0<<0)+(2<<2)+(1<<4)+(3<<6)), zero ),(0<<0)+(2<<2)+(1<<4)+(3<<6)); \ + out0 = _mm256_unpacklo_epi16( a, zero ); \ + out1 = _mm256_unpackhi_epi16( a, zero ); \ + } + + #define stbir__simdf8_pack_to_16bytes(out,aa,bb) \ + { \ + stbir__simdi8 t; \ + stbir__simdf8 af,bf; \ + stbir__simdi8 a,b; \ + af = _mm256_min_ps( aa, STBIR_max_uint8_as_floatX ); \ + bf = _mm256_min_ps( bb, STBIR_max_uint8_as_floatX ); \ + af = _mm256_max_ps( af, _mm256_setzero_ps() ); \ + bf = _mm256_max_ps( bf, _mm256_setzero_ps() ); \ + a = _mm256_cvttps_epi32( af ); \ + b = _mm256_cvttps_epi32( bf ); \ + t = _mm256_permute4x64_epi64( _mm256_packs_epi32( a, b ), (0<<0)+(2<<2)+(1<<4)+(3<<6) ); \ + out = _mm256_castsi256_si128( _mm256_permute4x64_epi64( _mm256_packus_epi16( t, t ), (0<<0)+(2<<2)+(1<<4)+(3<<6) ) ); \ + } + + #define stbir__simdi8_expand_u16_to_u32(out,ireg) out = _mm256_unpacklo_epi16( _mm256_permute4x64_epi64(_mm256_castsi128_si256(ireg),(0<<0)+(2<<2)+(1<<4)+(3<<6)), _mm256_setzero_si256() ); + + #define stbir__simdf8_pack_to_16words(out,aa,bb) \ + { \ + stbir__simdf8 af,bf; \ + stbir__simdi8 a,b; \ + af = _mm256_min_ps( aa, STBIR_max_uint16_as_floatX ); \ + bf = _mm256_min_ps( bb, STBIR_max_uint16_as_floatX ); \ + af = _mm256_max_ps( af, _mm256_setzero_ps() ); \ + bf = _mm256_max_ps( bf, _mm256_setzero_ps() ); \ + a = _mm256_cvttps_epi32( af ); \ + b = _mm256_cvttps_epi32( bf ); \ + (out) = _mm256_permute4x64_epi64( _mm256_packus_epi32(a, b), (0<<0)+(2<<2)+(1<<4)+(3<<6) ); \ + } + + #else + + #define stbir__simdi8_expand_u8_to_u32(out0,out1,ireg) \ + { \ + stbir__simdi a,zero = _mm_setzero_si128(); \ + a = _mm_unpacklo_epi8( ireg, zero ); \ + out0 = _mm256_setr_m128i( _mm_unpacklo_epi16( a, zero ), _mm_unpackhi_epi16( a, zero ) ); \ + a = _mm_unpackhi_epi8( ireg, zero ); \ + out1 = _mm256_setr_m128i( _mm_unpacklo_epi16( a, zero ), _mm_unpackhi_epi16( a, zero ) ); \ + } + + #define stbir__simdf8_pack_to_16bytes(out,aa,bb) \ + { \ + stbir__simdi t; \ + stbir__simdf8 af,bf; \ + stbir__simdi8 a,b; \ + af = _mm256_min_ps( aa, STBIR_max_uint8_as_floatX ); \ + bf = _mm256_min_ps( bb, STBIR_max_uint8_as_floatX ); \ + af = _mm256_max_ps( af, _mm256_setzero_ps() ); \ + bf = _mm256_max_ps( bf, _mm256_setzero_ps() ); \ + a = _mm256_cvttps_epi32( af ); \ + b = _mm256_cvttps_epi32( bf ); \ + out = _mm_packs_epi32( _mm256_castsi256_si128(a), _mm256_extractf128_si256( a, 1 ) ); \ + out = _mm_packus_epi16( out, out ); \ + t = _mm_packs_epi32( _mm256_castsi256_si128(b), _mm256_extractf128_si256( b, 1 ) ); \ + t = _mm_packus_epi16( t, t ); \ + out = _mm_castps_si128( _mm_shuffle_ps( _mm_castsi128_ps(out), _mm_castsi128_ps(t), (0<<0)+(1<<2)+(0<<4)+(1<<6) ) ); \ + } + + #define stbir__simdi8_expand_u16_to_u32(out,ireg) \ + { \ + stbir__simdi a,b,zero = _mm_setzero_si128(); \ + a = _mm_unpacklo_epi16( ireg, zero ); \ + b = _mm_unpackhi_epi16( ireg, zero ); \ + out = _mm256_insertf128_si256( _mm256_castsi128_si256( a ), b, 1 ); \ + } + + #define stbir__simdf8_pack_to_16words(out,aa,bb) \ + { \ + stbir__simdi t0,t1; \ + stbir__simdf8 af,bf; \ + stbir__simdi8 a,b; \ + af = _mm256_min_ps( aa, STBIR_max_uint16_as_floatX ); \ + bf = _mm256_min_ps( bb, STBIR_max_uint16_as_floatX ); \ + af = _mm256_max_ps( af, _mm256_setzero_ps() ); \ + bf = _mm256_max_ps( bf, _mm256_setzero_ps() ); \ + a = _mm256_cvttps_epi32( af ); \ + b = _mm256_cvttps_epi32( bf ); \ + t0 = _mm_packus_epi32( _mm256_castsi256_si128(a), _mm256_extractf128_si256( a, 1 ) ); \ + t1 = _mm_packus_epi32( _mm256_castsi256_si128(b), _mm256_extractf128_si256( b, 1 ) ); \ + out = _mm256_setr_m128i( t0, t1 ); \ + } + + #endif + + static __m256i stbir_00001111 = { STBIR__CONST_4d_32i( 0, 0, 0, 0 ), STBIR__CONST_4d_32i( 1, 1, 1, 1 ) }; + #define stbir__simdf8_0123to00001111( out, in ) (out) = _mm256_permutevar_ps ( in, stbir_00001111 ) + + static __m256i stbir_22223333 = { STBIR__CONST_4d_32i( 2, 2, 2, 2 ), STBIR__CONST_4d_32i( 3, 3, 3, 3 ) }; + #define stbir__simdf8_0123to22223333( out, in ) (out) = _mm256_permutevar_ps ( in, stbir_22223333 ) + + #define stbir__simdf8_0123to2222( out, in ) (out) = stbir__simdf_swiz(_mm256_castps256_ps128(in), 2,2,2,2 ) + + #define stbir__simdf8_load4b( out, ptr ) (out) = _mm256_broadcast_ps( (__m128 const *)(ptr) ) + + static __m256i stbir_00112233 = { STBIR__CONST_4d_32i( 0, 0, 1, 1 ), STBIR__CONST_4d_32i( 2, 2, 3, 3 ) }; + #define stbir__simdf8_0123to00112233( out, in ) (out) = _mm256_permutevar_ps ( in, stbir_00112233 ) + #define stbir__simdf8_add4( out, a8, b ) (out) = _mm256_add_ps( a8, _mm256_castps128_ps256( b ) ) + + static __m256i stbir_load6 = { STBIR__CONST_4_32i( 0x80000000 ), STBIR__CONST_4d_32i( 0x80000000, 0x80000000, 0, 0 ) }; + #define stbir__simdf8_load6z( out, ptr ) (out) = _mm256_maskload_ps( ptr, stbir_load6 ) + + #define stbir__simdf8_0123to00000000( out, in ) (out) = _mm256_shuffle_ps ( in, in, (0<<0)+(0<<2)+(0<<4)+(0<<6) ) + #define stbir__simdf8_0123to11111111( out, in ) (out) = _mm256_shuffle_ps ( in, in, (1<<0)+(1<<2)+(1<<4)+(1<<6) ) + #define stbir__simdf8_0123to22222222( out, in ) (out) = _mm256_shuffle_ps ( in, in, (2<<0)+(2<<2)+(2<<4)+(2<<6) ) + #define stbir__simdf8_0123to33333333( out, in ) (out) = _mm256_shuffle_ps ( in, in, (3<<0)+(3<<2)+(3<<4)+(3<<6) ) + #define stbir__simdf8_0123to21032103( out, in ) (out) = _mm256_shuffle_ps ( in, in, (2<<0)+(1<<2)+(0<<4)+(3<<6) ) + #define stbir__simdf8_0123to32103210( out, in ) (out) = _mm256_shuffle_ps ( in, in, (3<<0)+(2<<2)+(1<<4)+(0<<6) ) + #define stbir__simdf8_0123to12301230( out, in ) (out) = _mm256_shuffle_ps ( in, in, (1<<0)+(2<<2)+(3<<4)+(0<<6) ) + #define stbir__simdf8_0123to10321032( out, in ) (out) = _mm256_shuffle_ps ( in, in, (1<<0)+(0<<2)+(3<<4)+(2<<6) ) + #define stbir__simdf8_0123to30123012( out, in ) (out) = _mm256_shuffle_ps ( in, in, (3<<0)+(0<<2)+(1<<4)+(2<<6) ) + + #define stbir__simdf8_0123to11331133( out, in ) (out) = _mm256_shuffle_ps ( in, in, (1<<0)+(1<<2)+(3<<4)+(3<<6) ) + #define stbir__simdf8_0123to00220022( out, in ) (out) = _mm256_shuffle_ps ( in, in, (0<<0)+(0<<2)+(2<<4)+(2<<6) ) + + #define stbir__simdf8_aaa1( out, alp, ones ) (out) = _mm256_blend_ps( alp, ones, (1<<0)+(1<<1)+(1<<2)+(0<<3)+(1<<4)+(1<<5)+(1<<6)+(0<<7)); (out)=_mm256_shuffle_ps( out,out, (3<<0) + (3<<2) + (3<<4) + (0<<6) ) + #define stbir__simdf8_1aaa( out, alp, ones ) (out) = _mm256_blend_ps( alp, ones, (0<<0)+(1<<1)+(1<<2)+(1<<3)+(0<<4)+(1<<5)+(1<<6)+(1<<7)); (out)=_mm256_shuffle_ps( out,out, (1<<0) + (0<<2) + (0<<4) + (0<<6) ) + #define stbir__simdf8_a1a1( out, alp, ones) (out) = _mm256_blend_ps( alp, ones, (1<<0)+(0<<1)+(1<<2)+(0<<3)+(1<<4)+(0<<5)+(1<<6)+(0<<7)); (out)=_mm256_shuffle_ps( out,out, (1<<0) + (0<<2) + (3<<4) + (2<<6) ) + #define stbir__simdf8_1a1a( out, alp, ones) (out) = _mm256_blend_ps( alp, ones, (0<<0)+(1<<1)+(0<<2)+(1<<3)+(0<<4)+(1<<5)+(0<<6)+(1<<7)); (out)=_mm256_shuffle_ps( out,out, (1<<0) + (0<<2) + (3<<4) + (2<<6) ) + + #define stbir__simdf8_zero( reg ) (reg) = _mm256_setzero_ps() + + #ifdef STBIR_USE_FMA // not on by default to maintain bit identical simd to non-simd + #define stbir__simdf8_madd( out, add, mul1, mul2 ) (out) = _mm256_fmadd_ps( mul1, mul2, add ) + #define stbir__simdf8_madd_mem( out, add, mul, ptr ) (out) = _mm256_fmadd_ps( mul, _mm256_loadu_ps( (float const*)(ptr) ), add ) + #define stbir__simdf8_madd_mem4( out, add, mul, ptr )(out) = _mm256_fmadd_ps( _mm256_setr_m128( mul, _mm_setzero_ps() ), _mm256_setr_m128( _mm_loadu_ps( (float const*)(ptr) ), _mm_setzero_ps() ), add ) + #else + #define stbir__simdf8_madd( out, add, mul1, mul2 ) (out) = _mm256_add_ps( add, _mm256_mul_ps( mul1, mul2 ) ) + #define stbir__simdf8_madd_mem( out, add, mul, ptr ) (out) = _mm256_add_ps( add, _mm256_mul_ps( mul, _mm256_loadu_ps( (float const*)(ptr) ) ) ) + #define stbir__simdf8_madd_mem4( out, add, mul, ptr ) (out) = _mm256_add_ps( add, _mm256_setr_m128( _mm_mul_ps( mul, _mm_loadu_ps( (float const*)(ptr) ) ), _mm_setzero_ps() ) ) + #endif + #define stbir__if_simdf8_cast_to_simdf4( val ) _mm256_castps256_ps128( val ) + + #endif + + #ifdef STBIR_FLOORF + #undef STBIR_FLOORF + #endif + #define STBIR_FLOORF stbir_simd_floorf + static stbir__inline float stbir_simd_floorf(float x) // martins floorf + { + #if defined(STBIR_AVX) || defined(__SSE4_1__) || defined(STBIR_SSE41) + __m128 t = _mm_set_ss(x); + return _mm_cvtss_f32( _mm_floor_ss(t, t) ); + #else + __m128 f = _mm_set_ss(x); + __m128 t = _mm_cvtepi32_ps(_mm_cvttps_epi32(f)); + __m128 r = _mm_add_ss(t, _mm_and_ps(_mm_cmplt_ss(f, t), _mm_set_ss(-1.0f))); + return _mm_cvtss_f32(r); + #endif + } + + #ifdef STBIR_CEILF + #undef STBIR_CEILF + #endif + #define STBIR_CEILF stbir_simd_ceilf + static stbir__inline float stbir_simd_ceilf(float x) // martins ceilf + { + #if defined(STBIR_AVX) || defined(__SSE4_1__) || defined(STBIR_SSE41) + __m128 t = _mm_set_ss(x); + return _mm_cvtss_f32( _mm_ceil_ss(t, t) ); + #else + __m128 f = _mm_set_ss(x); + __m128 t = _mm_cvtepi32_ps(_mm_cvttps_epi32(f)); + __m128 r = _mm_add_ss(t, _mm_and_ps(_mm_cmplt_ss(t, f), _mm_set_ss(1.0f))); + return _mm_cvtss_f32(r); + #endif + } + +#elif defined(STBIR_NEON) + + #include + + #define stbir__simdf float32x4_t + #define stbir__simdi uint32x4_t + + #define stbir_simdi_castf( reg ) vreinterpretq_u32_f32(reg) + #define stbir_simdf_casti( reg ) vreinterpretq_f32_u32(reg) + + #define stbir__simdf_load( reg, ptr ) (reg) = vld1q_f32( (float const*)(ptr) ) + #define stbir__simdi_load( reg, ptr ) (reg) = vld1q_u32( (uint32_t const*)(ptr) ) + #define stbir__simdf_load1( out, ptr ) (out) = vld1q_dup_f32( (float const*)(ptr) ) // top values can be random (not denormal or nan for perf) + #define stbir__simdi_load1( out, ptr ) (out) = vld1q_dup_u32( (uint32_t const*)(ptr) ) + #define stbir__simdf_load1z( out, ptr ) (out) = vld1q_lane_f32( (float const*)(ptr), vdupq_n_f32(0), 0 ) // top values must be zero + #define stbir__simdf_frep4( fvar ) vdupq_n_f32( fvar ) + #define stbir__simdf_load1frep4( out, fvar ) (out) = vdupq_n_f32( fvar ) + #define stbir__simdf_load2( out, ptr ) (out) = vcombine_f32( vld1_f32( (float const*)(ptr) ), vcreate_f32(0) ) // top values can be random (not denormal or nan for perf) + #define stbir__simdf_load2z( out, ptr ) (out) = vcombine_f32( vld1_f32( (float const*)(ptr) ), vcreate_f32(0) ) // top values must be zero + #define stbir__simdf_load2hmerge( out, reg, ptr ) (out) = vcombine_f32( vget_low_f32(reg), vld1_f32( (float const*)(ptr) ) ) + + #define stbir__simdf_zeroP() vdupq_n_f32(0) + #define stbir__simdf_zero( reg ) (reg) = vdupq_n_f32(0) + + #define stbir__simdf_store( ptr, reg ) vst1q_f32( (float*)(ptr), reg ) + #define stbir__simdf_store1( ptr, reg ) vst1q_lane_f32( (float*)(ptr), reg, 0) + #define stbir__simdf_store2( ptr, reg ) vst1_f32( (float*)(ptr), vget_low_f32(reg) ) + #define stbir__simdf_store2h( ptr, reg ) vst1_f32( (float*)(ptr), vget_high_f32(reg) ) + + #define stbir__simdi_store( ptr, reg ) vst1q_u32( (uint32_t*)(ptr), reg ) + #define stbir__simdi_store1( ptr, reg ) vst1q_lane_u32( (uint32_t*)(ptr), reg, 0 ) + #define stbir__simdi_store2( ptr, reg ) vst1_u32( (uint32_t*)(ptr), vget_low_u32(reg) ) + + #define stbir__prefetch( ptr ) + + #define stbir__simdi_expand_u8_to_u32(out0,out1,out2,out3,ireg) \ + { \ + uint16x8_t l = vmovl_u8( vget_low_u8 ( vreinterpretq_u8_u32(ireg) ) ); \ + uint16x8_t h = vmovl_u8( vget_high_u8( vreinterpretq_u8_u32(ireg) ) ); \ + out0 = vmovl_u16( vget_low_u16 ( l ) ); \ + out1 = vmovl_u16( vget_high_u16( l ) ); \ + out2 = vmovl_u16( vget_low_u16 ( h ) ); \ + out3 = vmovl_u16( vget_high_u16( h ) ); \ + } + + #define stbir__simdi_expand_u8_to_1u32(out,ireg) \ + { \ + uint16x8_t tmp = vmovl_u8( vget_low_u8( vreinterpretq_u8_u32(ireg) ) ); \ + out = vmovl_u16( vget_low_u16( tmp ) ); \ + } + + #define stbir__simdi_expand_u16_to_u32(out0,out1,ireg) \ + { \ + uint16x8_t tmp = vreinterpretq_u16_u32(ireg); \ + out0 = vmovl_u16( vget_low_u16 ( tmp ) ); \ + out1 = vmovl_u16( vget_high_u16( tmp ) ); \ + } + + #define stbir__simdf_convert_float_to_i32( i, f ) (i) = vreinterpretq_u32_s32( vcvtq_s32_f32(f) ) + #define stbir__simdf_convert_float_to_int( f ) vgetq_lane_s32(vcvtq_s32_f32(f), 0) + #define stbir__simdi_to_int( i ) (int)vgetq_lane_u32(i, 0) + #define stbir__simdf_convert_float_to_uint8( f ) ((unsigned char)vgetq_lane_s32(vcvtq_s32_f32(vmaxq_f32(vminq_f32(f,STBIR__CONSTF(STBIR_max_uint8_as_float)),vdupq_n_f32(0))), 0)) + #define stbir__simdf_convert_float_to_short( f ) ((unsigned short)vgetq_lane_s32(vcvtq_s32_f32(vmaxq_f32(vminq_f32(f,STBIR__CONSTF(STBIR_max_uint16_as_float)),vdupq_n_f32(0))), 0)) + #define stbir__simdi_convert_i32_to_float(out, ireg) (out) = vcvtq_f32_s32( vreinterpretq_s32_u32(ireg) ) + #define stbir__simdf_add( out, reg0, reg1 ) (out) = vaddq_f32( reg0, reg1 ) + #define stbir__simdf_mult( out, reg0, reg1 ) (out) = vmulq_f32( reg0, reg1 ) + #define stbir__simdf_mult_mem( out, reg, ptr ) (out) = vmulq_f32( reg, vld1q_f32( (float const*)(ptr) ) ) + #define stbir__simdf_mult1_mem( out, reg, ptr ) (out) = vmulq_f32( reg, vld1q_dup_f32( (float const*)(ptr) ) ) + #define stbir__simdf_add_mem( out, reg, ptr ) (out) = vaddq_f32( reg, vld1q_f32( (float const*)(ptr) ) ) + #define stbir__simdf_add1_mem( out, reg, ptr ) (out) = vaddq_f32( reg, vld1q_dup_f32( (float const*)(ptr) ) ) + + #ifdef STBIR_USE_FMA // not on by default to maintain bit identical simd to non-simd (and also x64 no madd to arm madd) + #define stbir__simdf_madd( out, add, mul1, mul2 ) (out) = vfmaq_f32( add, mul1, mul2 ) + #define stbir__simdf_madd1( out, add, mul1, mul2 ) (out) = vfmaq_f32( add, mul1, mul2 ) + #define stbir__simdf_madd_mem( out, add, mul, ptr ) (out) = vfmaq_f32( add, mul, vld1q_f32( (float const*)(ptr) ) ) + #define stbir__simdf_madd1_mem( out, add, mul, ptr ) (out) = vfmaq_f32( add, mul, vld1q_dup_f32( (float const*)(ptr) ) ) + #else + #define stbir__simdf_madd( out, add, mul1, mul2 ) (out) = vaddq_f32( add, vmulq_f32( mul1, mul2 ) ) + #define stbir__simdf_madd1( out, add, mul1, mul2 ) (out) = vaddq_f32( add, vmulq_f32( mul1, mul2 ) ) + #define stbir__simdf_madd_mem( out, add, mul, ptr ) (out) = vaddq_f32( add, vmulq_f32( mul, vld1q_f32( (float const*)(ptr) ) ) ) + #define stbir__simdf_madd1_mem( out, add, mul, ptr ) (out) = vaddq_f32( add, vmulq_f32( mul, vld1q_dup_f32( (float const*)(ptr) ) ) ) + #endif + + #define stbir__simdf_add1( out, reg0, reg1 ) (out) = vaddq_f32( reg0, reg1 ) + #define stbir__simdf_mult1( out, reg0, reg1 ) (out) = vmulq_f32( reg0, reg1 ) + + #define stbir__simdf_and( out, reg0, reg1 ) (out) = vreinterpretq_f32_u32( vandq_u32( vreinterpretq_u32_f32(reg0), vreinterpretq_u32_f32(reg1) ) ) + #define stbir__simdf_or( out, reg0, reg1 ) (out) = vreinterpretq_f32_u32( vorrq_u32( vreinterpretq_u32_f32(reg0), vreinterpretq_u32_f32(reg1) ) ) + + #define stbir__simdf_min( out, reg0, reg1 ) (out) = vminq_f32( reg0, reg1 ) + #define stbir__simdf_max( out, reg0, reg1 ) (out) = vmaxq_f32( reg0, reg1 ) + #define stbir__simdf_min1( out, reg0, reg1 ) (out) = vminq_f32( reg0, reg1 ) + #define stbir__simdf_max1( out, reg0, reg1 ) (out) = vmaxq_f32( reg0, reg1 ) + + #define stbir__simdf_0123ABCDto3ABx( out, reg0, reg1 ) (out) = vextq_f32( reg0, reg1, 3 ) + #define stbir__simdf_0123ABCDto23Ax( out, reg0, reg1 ) (out) = vextq_f32( reg0, reg1, 2 ) + + #define stbir__simdf_a1a1( out, alp, ones ) (out) = vzipq_f32(vuzpq_f32(alp, alp).val[1], ones).val[0] + #define stbir__simdf_1a1a( out, alp, ones ) (out) = vzipq_f32(ones, vuzpq_f32(alp, alp).val[0]).val[0] + + #if defined( _M_ARM64 ) || defined( __aarch64__ ) || defined( __arm64__ ) + + #define stbir__simdf_aaa1( out, alp, ones ) (out) = vcopyq_laneq_f32(vdupq_n_f32(vgetq_lane_f32(alp, 3)), 3, ones, 3) + #define stbir__simdf_1aaa( out, alp, ones ) (out) = vcopyq_laneq_f32(vdupq_n_f32(vgetq_lane_f32(alp, 0)), 0, ones, 0) + + #if defined( _MSC_VER ) && !defined(__clang__) + #define stbir_make16(a,b,c,d) vcombine_u8( \ + vcreate_u8( (4*a+0) | ((4*a+1)<<8) | ((4*a+2)<<16) | ((4*a+3)<<24) | \ + ((stbir_uint64)(4*b+0)<<32) | ((stbir_uint64)(4*b+1)<<40) | ((stbir_uint64)(4*b+2)<<48) | ((stbir_uint64)(4*b+3)<<56)), \ + vcreate_u8( (4*c+0) | ((4*c+1)<<8) | ((4*c+2)<<16) | ((4*c+3)<<24) | \ + ((stbir_uint64)(4*d+0)<<32) | ((stbir_uint64)(4*d+1)<<40) | ((stbir_uint64)(4*d+2)<<48) | ((stbir_uint64)(4*d+3)<<56) ) ) + + static stbir__inline uint8x16x2_t stbir_make16x2(float32x4_t rega,float32x4_t regb) + { + uint8x16x2_t r = { vreinterpretq_u8_f32(rega), vreinterpretq_u8_f32(regb) }; + return r; + } + #else + #define stbir_make16(a,b,c,d) (uint8x16_t){4*a+0,4*a+1,4*a+2,4*a+3,4*b+0,4*b+1,4*b+2,4*b+3,4*c+0,4*c+1,4*c+2,4*c+3,4*d+0,4*d+1,4*d+2,4*d+3} + #define stbir_make16x2(a,b) (uint8x16x2_t){{vreinterpretq_u8_f32(a),vreinterpretq_u8_f32(b)}} + #endif + + #define stbir__simdf_swiz( reg, one, two, three, four ) vreinterpretq_f32_u8( vqtbl1q_u8( vreinterpretq_u8_f32(reg), stbir_make16(one, two, three, four) ) ) + #define stbir__simdf_swiz2( rega, regb, one, two, three, four ) vreinterpretq_f32_u8( vqtbl2q_u8( stbir_make16x2(rega,regb), stbir_make16(one, two, three, four) ) ) + + #define stbir__simdi_16madd( out, reg0, reg1 ) \ + { \ + int16x8_t r0 = vreinterpretq_s16_u32(reg0); \ + int16x8_t r1 = vreinterpretq_s16_u32(reg1); \ + int32x4_t tmp0 = vmull_s16( vget_low_s16(r0), vget_low_s16(r1) ); \ + int32x4_t tmp1 = vmull_s16( vget_high_s16(r0), vget_high_s16(r1) ); \ + (out) = vreinterpretq_u32_s32( vpaddq_s32(tmp0, tmp1) ); \ + } + + #else + + #define stbir__simdf_aaa1( out, alp, ones ) (out) = vsetq_lane_f32(1.0f, vdupq_n_f32(vgetq_lane_f32(alp, 3)), 3) + #define stbir__simdf_1aaa( out, alp, ones ) (out) = vsetq_lane_f32(1.0f, vdupq_n_f32(vgetq_lane_f32(alp, 0)), 0) + + #if defined( _MSC_VER ) && !defined(__clang__) + static stbir__inline uint8x8x2_t stbir_make8x2(float32x4_t reg) + { + uint8x8x2_t r = { { vget_low_u8(vreinterpretq_u8_f32(reg)), vget_high_u8(vreinterpretq_u8_f32(reg)) } }; + return r; + } + #define stbir_make8(a,b) vcreate_u8( \ + (4*a+0) | ((4*a+1)<<8) | ((4*a+2)<<16) | ((4*a+3)<<24) | \ + ((stbir_uint64)(4*b+0)<<32) | ((stbir_uint64)(4*b+1)<<40) | ((stbir_uint64)(4*b+2)<<48) | ((stbir_uint64)(4*b+3)<<56) ) + #else + #define stbir_make8x2(reg) (uint8x8x2_t){ { vget_low_u8(vreinterpretq_u8_f32(reg)), vget_high_u8(vreinterpretq_u8_f32(reg)) } } + #define stbir_make8(a,b) (uint8x8_t){4*a+0,4*a+1,4*a+2,4*a+3,4*b+0,4*b+1,4*b+2,4*b+3} + #endif + + #define stbir__simdf_swiz( reg, one, two, three, four ) vreinterpretq_f32_u8( vcombine_u8( \ + vtbl2_u8( stbir_make8x2( reg ), stbir_make8( one, two ) ), \ + vtbl2_u8( stbir_make8x2( reg ), stbir_make8( three, four ) ) ) ) + + #define stbir__simdi_16madd( out, reg0, reg1 ) \ + { \ + int16x8_t r0 = vreinterpretq_s16_u32(reg0); \ + int16x8_t r1 = vreinterpretq_s16_u32(reg1); \ + int32x4_t tmp0 = vmull_s16( vget_low_s16(r0), vget_low_s16(r1) ); \ + int32x4_t tmp1 = vmull_s16( vget_high_s16(r0), vget_high_s16(r1) ); \ + int32x2_t out0 = vpadd_s32( vget_low_s32(tmp0), vget_high_s32(tmp0) ); \ + int32x2_t out1 = vpadd_s32( vget_low_s32(tmp1), vget_high_s32(tmp1) ); \ + (out) = vreinterpretq_u32_s32( vcombine_s32(out0, out1) ); \ + } + + #endif + + #define stbir__simdi_and( out, reg0, reg1 ) (out) = vandq_u32( reg0, reg1 ) + #define stbir__simdi_or( out, reg0, reg1 ) (out) = vorrq_u32( reg0, reg1 ) + + #define stbir__simdf_pack_to_8bytes(out,aa,bb) \ + { \ + float32x4_t af = vmaxq_f32( vminq_f32(aa,STBIR__CONSTF(STBIR_max_uint8_as_float) ), vdupq_n_f32(0) ); \ + float32x4_t bf = vmaxq_f32( vminq_f32(bb,STBIR__CONSTF(STBIR_max_uint8_as_float) ), vdupq_n_f32(0) ); \ + int16x4_t ai = vqmovn_s32( vcvtq_s32_f32( af ) ); \ + int16x4_t bi = vqmovn_s32( vcvtq_s32_f32( bf ) ); \ + uint8x8_t out8 = vqmovun_s16( vcombine_s16(ai, bi) ); \ + out = vreinterpretq_u32_u8( vcombine_u8(out8, out8) ); \ + } + + #define stbir__simdf_pack_to_8words(out,aa,bb) \ + { \ + float32x4_t af = vmaxq_f32( vminq_f32(aa,STBIR__CONSTF(STBIR_max_uint16_as_float) ), vdupq_n_f32(0) ); \ + float32x4_t bf = vmaxq_f32( vminq_f32(bb,STBIR__CONSTF(STBIR_max_uint16_as_float) ), vdupq_n_f32(0) ); \ + int32x4_t ai = vcvtq_s32_f32( af ); \ + int32x4_t bi = vcvtq_s32_f32( bf ); \ + out = vreinterpretq_u32_u16( vcombine_u16(vqmovun_s32(ai), vqmovun_s32(bi)) ); \ + } + + #define stbir__interleave_pack_and_store_16_u8( ptr, r0, r1, r2, r3 ) \ + { \ + int16x4x2_t tmp0 = vzip_s16( vqmovn_s32(vreinterpretq_s32_u32(r0)), vqmovn_s32(vreinterpretq_s32_u32(r2)) ); \ + int16x4x2_t tmp1 = vzip_s16( vqmovn_s32(vreinterpretq_s32_u32(r1)), vqmovn_s32(vreinterpretq_s32_u32(r3)) ); \ + uint8x8x2_t out = \ + { { \ + vqmovun_s16( vcombine_s16(tmp0.val[0], tmp0.val[1]) ), \ + vqmovun_s16( vcombine_s16(tmp1.val[0], tmp1.val[1]) ), \ + } }; \ + vst2_u8(ptr, out); \ + } + + #define stbir__simdf_load4_transposed( o0, o1, o2, o3, ptr ) \ + { \ + float32x4x4_t tmp = vld4q_f32(ptr); \ + o0 = tmp.val[0]; \ + o1 = tmp.val[1]; \ + o2 = tmp.val[2]; \ + o3 = tmp.val[3]; \ + } + + #define stbir__simdi_32shr( out, reg, imm ) out = vshrq_n_u32( reg, imm ) + + #if defined( _MSC_VER ) && !defined(__clang__) + #define STBIR__SIMDF_CONST(var, x) __declspec(align(8)) float var[] = { x, x, x, x } + #define STBIR__SIMDI_CONST(var, x) __declspec(align(8)) uint32_t var[] = { x, x, x, x } + #define STBIR__CONSTF(var) (*(const float32x4_t*)var) + #define STBIR__CONSTI(var) (*(const uint32x4_t*)var) + #else + #define STBIR__SIMDF_CONST(var, x) stbir__simdf var = { x, x, x, x } + #define STBIR__SIMDI_CONST(var, x) stbir__simdi var = { x, x, x, x } + #define STBIR__CONSTF(var) (var) + #define STBIR__CONSTI(var) (var) + #endif + + #ifdef STBIR_FLOORF + #undef STBIR_FLOORF + #endif + #define STBIR_FLOORF stbir_simd_floorf + static stbir__inline float stbir_simd_floorf(float x) + { + #if defined( _M_ARM64 ) || defined( __aarch64__ ) || defined( __arm64__ ) + return vget_lane_f32( vrndm_f32( vdup_n_f32(x) ), 0); + #else + float32x2_t f = vdup_n_f32(x); + float32x2_t t = vcvt_f32_s32(vcvt_s32_f32(f)); + uint32x2_t a = vclt_f32(f, t); + uint32x2_t b = vreinterpret_u32_f32(vdup_n_f32(-1.0f)); + float32x2_t r = vadd_f32(t, vreinterpret_f32_u32(vand_u32(a, b))); + return vget_lane_f32(r, 0); + #endif + } + + #ifdef STBIR_CEILF + #undef STBIR_CEILF + #endif + #define STBIR_CEILF stbir_simd_ceilf + static stbir__inline float stbir_simd_ceilf(float x) + { + #if defined( _M_ARM64 ) || defined( __aarch64__ ) || defined( __arm64__ ) + return vget_lane_f32( vrndp_f32( vdup_n_f32(x) ), 0); + #else + float32x2_t f = vdup_n_f32(x); + float32x2_t t = vcvt_f32_s32(vcvt_s32_f32(f)); + uint32x2_t a = vclt_f32(t, f); + uint32x2_t b = vreinterpret_u32_f32(vdup_n_f32(1.0f)); + float32x2_t r = vadd_f32(t, vreinterpret_f32_u32(vand_u32(a, b))); + return vget_lane_f32(r, 0); + #endif + } + + #define STBIR_SIMD + +#elif defined(STBIR_WASM) + + #include + + #define stbir__simdf v128_t + #define stbir__simdi v128_t + + #define stbir_simdi_castf( reg ) (reg) + #define stbir_simdf_casti( reg ) (reg) + + #define stbir__simdf_load( reg, ptr ) (reg) = wasm_v128_load( (void const*)(ptr) ) + #define stbir__simdi_load( reg, ptr ) (reg) = wasm_v128_load( (void const*)(ptr) ) + #define stbir__simdf_load1( out, ptr ) (out) = wasm_v128_load32_splat( (void const*)(ptr) ) // top values can be random (not denormal or nan for perf) + #define stbir__simdi_load1( out, ptr ) (out) = wasm_v128_load32_splat( (void const*)(ptr) ) + #define stbir__simdf_load1z( out, ptr ) (out) = wasm_v128_load32_zero( (void const*)(ptr) ) // top values must be zero + #define stbir__simdf_frep4( fvar ) wasm_f32x4_splat( fvar ) + #define stbir__simdf_load1frep4( out, fvar ) (out) = wasm_f32x4_splat( fvar ) + #define stbir__simdf_load2( out, ptr ) (out) = wasm_v128_load64_splat( (void const*)(ptr) ) // top values can be random (not denormal or nan for perf) + #define stbir__simdf_load2z( out, ptr ) (out) = wasm_v128_load64_zero( (void const*)(ptr) ) // top values must be zero + #define stbir__simdf_load2hmerge( out, reg, ptr ) (out) = wasm_v128_load64_lane( (void const*)(ptr), reg, 1 ) + + #define stbir__simdf_zeroP() wasm_f32x4_const_splat(0) + #define stbir__simdf_zero( reg ) (reg) = wasm_f32x4_const_splat(0) + + #define stbir__simdf_store( ptr, reg ) wasm_v128_store( (void*)(ptr), reg ) + #define stbir__simdf_store1( ptr, reg ) wasm_v128_store32_lane( (void*)(ptr), reg, 0 ) + #define stbir__simdf_store2( ptr, reg ) wasm_v128_store64_lane( (void*)(ptr), reg, 0 ) + #define stbir__simdf_store2h( ptr, reg ) wasm_v128_store64_lane( (void*)(ptr), reg, 1 ) + + #define stbir__simdi_store( ptr, reg ) wasm_v128_store( (void*)(ptr), reg ) + #define stbir__simdi_store1( ptr, reg ) wasm_v128_store32_lane( (void*)(ptr), reg, 0 ) + #define stbir__simdi_store2( ptr, reg ) wasm_v128_store64_lane( (void*)(ptr), reg, 0 ) + + #define stbir__prefetch( ptr ) + + #define stbir__simdi_expand_u8_to_u32(out0,out1,out2,out3,ireg) \ + { \ + v128_t l = wasm_u16x8_extend_low_u8x16 ( ireg ); \ + v128_t h = wasm_u16x8_extend_high_u8x16( ireg ); \ + out0 = wasm_u32x4_extend_low_u16x8 ( l ); \ + out1 = wasm_u32x4_extend_high_u16x8( l ); \ + out2 = wasm_u32x4_extend_low_u16x8 ( h ); \ + out3 = wasm_u32x4_extend_high_u16x8( h ); \ + } + + #define stbir__simdi_expand_u8_to_1u32(out,ireg) \ + { \ + v128_t tmp = wasm_u16x8_extend_low_u8x16(ireg); \ + out = wasm_u32x4_extend_low_u16x8(tmp); \ + } + + #define stbir__simdi_expand_u16_to_u32(out0,out1,ireg) \ + { \ + out0 = wasm_u32x4_extend_low_u16x8 ( ireg ); \ + out1 = wasm_u32x4_extend_high_u16x8( ireg ); \ + } + + #define stbir__simdf_convert_float_to_i32( i, f ) (i) = wasm_i32x4_trunc_sat_f32x4(f) + #define stbir__simdf_convert_float_to_int( f ) wasm_i32x4_extract_lane(wasm_i32x4_trunc_sat_f32x4(f), 0) + #define stbir__simdi_to_int( i ) wasm_i32x4_extract_lane(i, 0) + #define stbir__simdf_convert_float_to_uint8( f ) ((unsigned char)wasm_i32x4_extract_lane(wasm_i32x4_trunc_sat_f32x4(wasm_f32x4_max(wasm_f32x4_min(f,STBIR_max_uint8_as_float),wasm_f32x4_const_splat(0))), 0)) + #define stbir__simdf_convert_float_to_short( f ) ((unsigned short)wasm_i32x4_extract_lane(wasm_i32x4_trunc_sat_f32x4(wasm_f32x4_max(wasm_f32x4_min(f,STBIR_max_uint16_as_float),wasm_f32x4_const_splat(0))), 0)) + #define stbir__simdi_convert_i32_to_float(out, ireg) (out) = wasm_f32x4_convert_i32x4(ireg) + #define stbir__simdf_add( out, reg0, reg1 ) (out) = wasm_f32x4_add( reg0, reg1 ) + #define stbir__simdf_mult( out, reg0, reg1 ) (out) = wasm_f32x4_mul( reg0, reg1 ) + #define stbir__simdf_mult_mem( out, reg, ptr ) (out) = wasm_f32x4_mul( reg, wasm_v128_load( (void const*)(ptr) ) ) + #define stbir__simdf_mult1_mem( out, reg, ptr ) (out) = wasm_f32x4_mul( reg, wasm_v128_load32_splat( (void const*)(ptr) ) ) + #define stbir__simdf_add_mem( out, reg, ptr ) (out) = wasm_f32x4_add( reg, wasm_v128_load( (void const*)(ptr) ) ) + #define stbir__simdf_add1_mem( out, reg, ptr ) (out) = wasm_f32x4_add( reg, wasm_v128_load32_splat( (void const*)(ptr) ) ) + + #define stbir__simdf_madd( out, add, mul1, mul2 ) (out) = wasm_f32x4_add( add, wasm_f32x4_mul( mul1, mul2 ) ) + #define stbir__simdf_madd1( out, add, mul1, mul2 ) (out) = wasm_f32x4_add( add, wasm_f32x4_mul( mul1, mul2 ) ) + #define stbir__simdf_madd_mem( out, add, mul, ptr ) (out) = wasm_f32x4_add( add, wasm_f32x4_mul( mul, wasm_v128_load( (void const*)(ptr) ) ) ) + #define stbir__simdf_madd1_mem( out, add, mul, ptr ) (out) = wasm_f32x4_add( add, wasm_f32x4_mul( mul, wasm_v128_load32_splat( (void const*)(ptr) ) ) ) + + #define stbir__simdf_add1( out, reg0, reg1 ) (out) = wasm_f32x4_add( reg0, reg1 ) + #define stbir__simdf_mult1( out, reg0, reg1 ) (out) = wasm_f32x4_mul( reg0, reg1 ) + + #define stbir__simdf_and( out, reg0, reg1 ) (out) = wasm_v128_and( reg0, reg1 ) + #define stbir__simdf_or( out, reg0, reg1 ) (out) = wasm_v128_or( reg0, reg1 ) + + #define stbir__simdf_min( out, reg0, reg1 ) (out) = wasm_f32x4_min( reg0, reg1 ) + #define stbir__simdf_max( out, reg0, reg1 ) (out) = wasm_f32x4_max( reg0, reg1 ) + #define stbir__simdf_min1( out, reg0, reg1 ) (out) = wasm_f32x4_min( reg0, reg1 ) + #define stbir__simdf_max1( out, reg0, reg1 ) (out) = wasm_f32x4_max( reg0, reg1 ) + + #define stbir__simdf_0123ABCDto3ABx( out, reg0, reg1 ) (out) = wasm_i32x4_shuffle( reg0, reg1, 3, 4, 5, -1 ) + #define stbir__simdf_0123ABCDto23Ax( out, reg0, reg1 ) (out) = wasm_i32x4_shuffle( reg0, reg1, 2, 3, 4, -1 ) + + #define stbir__simdf_aaa1(out,alp,ones) (out) = wasm_i32x4_shuffle(alp, ones, 3, 3, 3, 4) + #define stbir__simdf_1aaa(out,alp,ones) (out) = wasm_i32x4_shuffle(alp, ones, 4, 0, 0, 0) + #define stbir__simdf_a1a1(out,alp,ones) (out) = wasm_i32x4_shuffle(alp, ones, 1, 4, 3, 4) + #define stbir__simdf_1a1a(out,alp,ones) (out) = wasm_i32x4_shuffle(alp, ones, 4, 0, 4, 2) + + #define stbir__simdf_swiz( reg, one, two, three, four ) wasm_i32x4_shuffle(reg, reg, one, two, three, four) + + #define stbir__simdi_and( out, reg0, reg1 ) (out) = wasm_v128_and( reg0, reg1 ) + #define stbir__simdi_or( out, reg0, reg1 ) (out) = wasm_v128_or( reg0, reg1 ) + #define stbir__simdi_16madd( out, reg0, reg1 ) (out) = wasm_i32x4_dot_i16x8( reg0, reg1 ) + + #define stbir__simdf_pack_to_8bytes(out,aa,bb) \ + { \ + v128_t af = wasm_f32x4_max( wasm_f32x4_min(aa, STBIR_max_uint8_as_float), wasm_f32x4_const_splat(0) ); \ + v128_t bf = wasm_f32x4_max( wasm_f32x4_min(bb, STBIR_max_uint8_as_float), wasm_f32x4_const_splat(0) ); \ + v128_t ai = wasm_i32x4_trunc_sat_f32x4( af ); \ + v128_t bi = wasm_i32x4_trunc_sat_f32x4( bf ); \ + v128_t out16 = wasm_i16x8_narrow_i32x4( ai, bi ); \ + out = wasm_u8x16_narrow_i16x8( out16, out16 ); \ + } + + #define stbir__simdf_pack_to_8words(out,aa,bb) \ + { \ + v128_t af = wasm_f32x4_max( wasm_f32x4_min(aa, STBIR_max_uint16_as_float), wasm_f32x4_const_splat(0)); \ + v128_t bf = wasm_f32x4_max( wasm_f32x4_min(bb, STBIR_max_uint16_as_float), wasm_f32x4_const_splat(0)); \ + v128_t ai = wasm_i32x4_trunc_sat_f32x4( af ); \ + v128_t bi = wasm_i32x4_trunc_sat_f32x4( bf ); \ + out = wasm_u16x8_narrow_i32x4( ai, bi ); \ + } + + #define stbir__interleave_pack_and_store_16_u8( ptr, r0, r1, r2, r3 ) \ + { \ + v128_t tmp0 = wasm_i16x8_narrow_i32x4(r0, r1); \ + v128_t tmp1 = wasm_i16x8_narrow_i32x4(r2, r3); \ + v128_t tmp = wasm_u8x16_narrow_i16x8(tmp0, tmp1); \ + tmp = wasm_i8x16_shuffle(tmp, tmp, 0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15); \ + wasm_v128_store( (void*)(ptr), tmp); \ + } + + #define stbir__simdf_load4_transposed( o0, o1, o2, o3, ptr ) \ + { \ + v128_t t0 = wasm_v128_load( ptr ); \ + v128_t t1 = wasm_v128_load( ptr+4 ); \ + v128_t t2 = wasm_v128_load( ptr+8 ); \ + v128_t t3 = wasm_v128_load( ptr+12 ); \ + v128_t s0 = wasm_i32x4_shuffle(t0, t1, 0, 4, 2, 6); \ + v128_t s1 = wasm_i32x4_shuffle(t0, t1, 1, 5, 3, 7); \ + v128_t s2 = wasm_i32x4_shuffle(t2, t3, 0, 4, 2, 6); \ + v128_t s3 = wasm_i32x4_shuffle(t2, t3, 1, 5, 3, 7); \ + o0 = wasm_i32x4_shuffle(s0, s2, 0, 1, 4, 5); \ + o1 = wasm_i32x4_shuffle(s1, s3, 0, 1, 4, 5); \ + o2 = wasm_i32x4_shuffle(s0, s2, 2, 3, 6, 7); \ + o3 = wasm_i32x4_shuffle(s1, s3, 2, 3, 6, 7); \ + } + + #define stbir__simdi_32shr( out, reg, imm ) out = wasm_u32x4_shr( reg, imm ) + + typedef float stbir__f32x4 __attribute__((__vector_size__(16), __aligned__(16))); + #define STBIR__SIMDF_CONST(var, x) stbir__simdf var = (v128_t)(stbir__f32x4){ x, x, x, x } + #define STBIR__SIMDI_CONST(var, x) stbir__simdi var = { x, x, x, x } + #define STBIR__CONSTF(var) (var) + #define STBIR__CONSTI(var) (var) + + #ifdef STBIR_FLOORF + #undef STBIR_FLOORF + #endif + #define STBIR_FLOORF stbir_simd_floorf + static stbir__inline float stbir_simd_floorf(float x) + { + return wasm_f32x4_extract_lane( wasm_f32x4_floor( wasm_f32x4_splat(x) ), 0); + } + + #ifdef STBIR_CEILF + #undef STBIR_CEILF + #endif + #define STBIR_CEILF stbir_simd_ceilf + static stbir__inline float stbir_simd_ceilf(float x) + { + return wasm_f32x4_extract_lane( wasm_f32x4_ceil( wasm_f32x4_splat(x) ), 0); + } + + #define STBIR_SIMD + +#endif // SSE2/NEON/WASM + +#endif // NO SIMD + +#ifdef STBIR_SIMD8 + #define stbir__simdfX stbir__simdf8 + #define stbir__simdiX stbir__simdi8 + #define stbir__simdfX_load stbir__simdf8_load + #define stbir__simdiX_load stbir__simdi8_load + #define stbir__simdfX_mult stbir__simdf8_mult + #define stbir__simdfX_add_mem stbir__simdf8_add_mem + #define stbir__simdfX_madd_mem stbir__simdf8_madd_mem + #define stbir__simdfX_store stbir__simdf8_store + #define stbir__simdiX_store stbir__simdi8_store + #define stbir__simdf_frepX stbir__simdf8_frep8 + #define stbir__simdfX_madd stbir__simdf8_madd + #define stbir__simdfX_min stbir__simdf8_min + #define stbir__simdfX_max stbir__simdf8_max + #define stbir__simdfX_aaa1 stbir__simdf8_aaa1 + #define stbir__simdfX_1aaa stbir__simdf8_1aaa + #define stbir__simdfX_a1a1 stbir__simdf8_a1a1 + #define stbir__simdfX_1a1a stbir__simdf8_1a1a + #define stbir__simdfX_convert_float_to_i32 stbir__simdf8_convert_float_to_i32 + #define stbir__simdfX_pack_to_words stbir__simdf8_pack_to_16words + #define stbir__simdfX_zero stbir__simdf8_zero + #define STBIR_onesX STBIR_ones8 + #define STBIR_max_uint8_as_floatX STBIR_max_uint8_as_float8 + #define STBIR_max_uint16_as_floatX STBIR_max_uint16_as_float8 + #define STBIR_simd_point5X STBIR_simd_point58 + #define stbir__simdfX_float_count 8 + #define stbir__simdfX_0123to1230 stbir__simdf8_0123to12301230 + #define stbir__simdfX_0123to2103 stbir__simdf8_0123to21032103 + static const stbir__simdf8 STBIR_max_uint16_as_float_inverted8 = { stbir__max_uint16_as_float_inverted,stbir__max_uint16_as_float_inverted,stbir__max_uint16_as_float_inverted,stbir__max_uint16_as_float_inverted,stbir__max_uint16_as_float_inverted,stbir__max_uint16_as_float_inverted,stbir__max_uint16_as_float_inverted,stbir__max_uint16_as_float_inverted }; + static const stbir__simdf8 STBIR_max_uint8_as_float_inverted8 = { stbir__max_uint8_as_float_inverted,stbir__max_uint8_as_float_inverted,stbir__max_uint8_as_float_inverted,stbir__max_uint8_as_float_inverted,stbir__max_uint8_as_float_inverted,stbir__max_uint8_as_float_inverted,stbir__max_uint8_as_float_inverted,stbir__max_uint8_as_float_inverted }; + static const stbir__simdf8 STBIR_ones8 = { 1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0 }; + static const stbir__simdf8 STBIR_simd_point58 = { 0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5 }; + static const stbir__simdf8 STBIR_max_uint8_as_float8 = { stbir__max_uint8_as_float,stbir__max_uint8_as_float,stbir__max_uint8_as_float,stbir__max_uint8_as_float, stbir__max_uint8_as_float,stbir__max_uint8_as_float,stbir__max_uint8_as_float,stbir__max_uint8_as_float }; + static const stbir__simdf8 STBIR_max_uint16_as_float8 = { stbir__max_uint16_as_float,stbir__max_uint16_as_float,stbir__max_uint16_as_float,stbir__max_uint16_as_float, stbir__max_uint16_as_float,stbir__max_uint16_as_float,stbir__max_uint16_as_float,stbir__max_uint16_as_float }; +#else + #define stbir__simdfX stbir__simdf + #define stbir__simdiX stbir__simdi + #define stbir__simdfX_load stbir__simdf_load + #define stbir__simdiX_load stbir__simdi_load + #define stbir__simdfX_mult stbir__simdf_mult + #define stbir__simdfX_add_mem stbir__simdf_add_mem + #define stbir__simdfX_madd_mem stbir__simdf_madd_mem + #define stbir__simdfX_store stbir__simdf_store + #define stbir__simdiX_store stbir__simdi_store + #define stbir__simdf_frepX stbir__simdf_frep4 + #define stbir__simdfX_madd stbir__simdf_madd + #define stbir__simdfX_min stbir__simdf_min + #define stbir__simdfX_max stbir__simdf_max + #define stbir__simdfX_aaa1 stbir__simdf_aaa1 + #define stbir__simdfX_1aaa stbir__simdf_1aaa + #define stbir__simdfX_a1a1 stbir__simdf_a1a1 + #define stbir__simdfX_1a1a stbir__simdf_1a1a + #define stbir__simdfX_convert_float_to_i32 stbir__simdf_convert_float_to_i32 + #define stbir__simdfX_pack_to_words stbir__simdf_pack_to_8words + #define stbir__simdfX_zero stbir__simdf_zero + #define STBIR_onesX STBIR__CONSTF(STBIR_ones) + #define STBIR_simd_point5X STBIR__CONSTF(STBIR_simd_point5) + #define STBIR_max_uint8_as_floatX STBIR__CONSTF(STBIR_max_uint8_as_float) + #define STBIR_max_uint16_as_floatX STBIR__CONSTF(STBIR_max_uint16_as_float) + #define stbir__simdfX_float_count 4 + #define stbir__if_simdf8_cast_to_simdf4( val ) ( val ) + #define stbir__simdfX_0123to1230 stbir__simdf_0123to1230 + #define stbir__simdfX_0123to2103 stbir__simdf_0123to2103 +#endif + + +#if defined(STBIR_NEON) && !defined(_M_ARM) && !defined(__arm__) + + #if defined( _MSC_VER ) && !defined(__clang__) + typedef __int16 stbir__FP16; + #else + typedef float16_t stbir__FP16; + #endif + +#else // no NEON, or 32-bit ARM for MSVC + + typedef union stbir__FP16 + { + unsigned short u; + } stbir__FP16; + +#endif + +#if (!defined(STBIR_NEON) && !defined(STBIR_FP16C)) || (defined(STBIR_NEON) && defined(_M_ARM)) || (defined(STBIR_NEON) && defined(__arm__)) + + // Fabian's half float routines, see: https://gist.github.com/rygorous/2156668 + + static stbir__inline float stbir__half_to_float( stbir__FP16 h ) + { + static const stbir__FP32 magic = { (254 - 15) << 23 }; + static const stbir__FP32 was_infnan = { (127 + 16) << 23 }; + stbir__FP32 o; + + o.u = (h.u & 0x7fff) << 13; // exponent/mantissa bits + o.f *= magic.f; // exponent adjust + if (o.f >= was_infnan.f) // make sure Inf/NaN survive + o.u |= 255 << 23; + o.u |= (h.u & 0x8000) << 16; // sign bit + return o.f; + } + + static stbir__inline stbir__FP16 stbir__float_to_half(float val) + { + stbir__FP32 f32infty = { 255 << 23 }; + stbir__FP32 f16max = { (127 + 16) << 23 }; + stbir__FP32 denorm_magic = { ((127 - 15) + (23 - 10) + 1) << 23 }; + unsigned int sign_mask = 0x80000000u; + stbir__FP16 o = { 0 }; + stbir__FP32 f; + unsigned int sign; + + f.f = val; + sign = f.u & sign_mask; + f.u ^= sign; + + if (f.u >= f16max.u) // result is Inf or NaN (all exponent bits set) + o.u = (f.u > f32infty.u) ? 0x7e00 : 0x7c00; // NaN->qNaN and Inf->Inf + else // (De)normalized number or zero + { + if (f.u < (113 << 23)) // resulting FP16 is subnormal or zero + { + // use a magic value to align our 10 mantissa bits at the bottom of + // the float. as long as FP addition is round-to-nearest-even this + // just works. + f.f += denorm_magic.f; + // and one integer subtract of the bias later, we have our final float! + o.u = (unsigned short) ( f.u - denorm_magic.u ); + } + else + { + unsigned int mant_odd = (f.u >> 13) & 1; // resulting mantissa is odd + // update exponent, rounding bias part 1 + f.u = f.u + ((15u - 127) << 23) + 0xfff; + // rounding bias part 2 + f.u += mant_odd; + // take the bits! + o.u = (unsigned short) ( f.u >> 13 ); + } + } + + o.u |= sign >> 16; + return o; + } + +#endif + + +#if defined(STBIR_FP16C) + + #include + + static stbir__inline void stbir__half_to_float_SIMD(float * output, stbir__FP16 const * input) + { + _mm256_storeu_ps( (float*)output, _mm256_cvtph_ps( _mm_loadu_si128( (__m128i const* )input ) ) ); + } + + static stbir__inline void stbir__float_to_half_SIMD(stbir__FP16 * output, float const * input) + { + _mm_storeu_si128( (__m128i*)output, _mm256_cvtps_ph( _mm256_loadu_ps( input ), 0 ) ); + } + + static stbir__inline float stbir__half_to_float( stbir__FP16 h ) + { + return _mm_cvtss_f32( _mm_cvtph_ps( _mm_cvtsi32_si128( (int)h.u ) ) ); + } + + static stbir__inline stbir__FP16 stbir__float_to_half( float f ) + { + stbir__FP16 h; + h.u = (unsigned short) _mm_cvtsi128_si32( _mm_cvtps_ph( _mm_set_ss( f ), 0 ) ); + return h; + } + +#elif defined(STBIR_SSE2) + + // Fabian's half float routines, see: https://gist.github.com/rygorous/2156668 + stbir__inline static void stbir__half_to_float_SIMD(float * output, void const * input) + { + static const STBIR__SIMDI_CONST(mask_nosign, 0x7fff); + static const STBIR__SIMDI_CONST(smallest_normal, 0x0400); + static const STBIR__SIMDI_CONST(infinity, 0x7c00); + static const STBIR__SIMDI_CONST(expadjust_normal, (127 - 15) << 23); + static const STBIR__SIMDI_CONST(magic_denorm, 113 << 23); + + __m128i i = _mm_loadu_si128 ( (__m128i const*)(input) ); + __m128i h = _mm_unpacklo_epi16 ( i, _mm_setzero_si128() ); + __m128i mnosign = STBIR__CONSTI(mask_nosign); + __m128i eadjust = STBIR__CONSTI(expadjust_normal); + __m128i smallest = STBIR__CONSTI(smallest_normal); + __m128i infty = STBIR__CONSTI(infinity); + __m128i expmant = _mm_and_si128(mnosign, h); + __m128i justsign = _mm_xor_si128(h, expmant); + __m128i b_notinfnan = _mm_cmpgt_epi32(infty, expmant); + __m128i b_isdenorm = _mm_cmpgt_epi32(smallest, expmant); + __m128i shifted = _mm_slli_epi32(expmant, 13); + __m128i adj_infnan = _mm_andnot_si128(b_notinfnan, eadjust); + __m128i adjusted = _mm_add_epi32(eadjust, shifted); + __m128i den1 = _mm_add_epi32(shifted, STBIR__CONSTI(magic_denorm)); + __m128i adjusted2 = _mm_add_epi32(adjusted, adj_infnan); + __m128 den2 = _mm_sub_ps(_mm_castsi128_ps(den1), *(const __m128 *)&magic_denorm); + __m128 adjusted3 = _mm_and_ps(den2, _mm_castsi128_ps(b_isdenorm)); + __m128 adjusted4 = _mm_andnot_ps(_mm_castsi128_ps(b_isdenorm), _mm_castsi128_ps(adjusted2)); + __m128 adjusted5 = _mm_or_ps(adjusted3, adjusted4); + __m128i sign = _mm_slli_epi32(justsign, 16); + __m128 final = _mm_or_ps(adjusted5, _mm_castsi128_ps(sign)); + stbir__simdf_store( output + 0, final ); + + h = _mm_unpackhi_epi16 ( i, _mm_setzero_si128() ); + expmant = _mm_and_si128(mnosign, h); + justsign = _mm_xor_si128(h, expmant); + b_notinfnan = _mm_cmpgt_epi32(infty, expmant); + b_isdenorm = _mm_cmpgt_epi32(smallest, expmant); + shifted = _mm_slli_epi32(expmant, 13); + adj_infnan = _mm_andnot_si128(b_notinfnan, eadjust); + adjusted = _mm_add_epi32(eadjust, shifted); + den1 = _mm_add_epi32(shifted, STBIR__CONSTI(magic_denorm)); + adjusted2 = _mm_add_epi32(adjusted, adj_infnan); + den2 = _mm_sub_ps(_mm_castsi128_ps(den1), *(const __m128 *)&magic_denorm); + adjusted3 = _mm_and_ps(den2, _mm_castsi128_ps(b_isdenorm)); + adjusted4 = _mm_andnot_ps(_mm_castsi128_ps(b_isdenorm), _mm_castsi128_ps(adjusted2)); + adjusted5 = _mm_or_ps(adjusted3, adjusted4); + sign = _mm_slli_epi32(justsign, 16); + final = _mm_or_ps(adjusted5, _mm_castsi128_ps(sign)); + stbir__simdf_store( output + 4, final ); + + // ~38 SSE2 ops for 8 values + } + + // Fabian's round-to-nearest-even float to half + // ~48 SSE2 ops for 8 output + stbir__inline static void stbir__float_to_half_SIMD(void * output, float const * input) + { + static const STBIR__SIMDI_CONST(mask_sign, 0x80000000u); + static const STBIR__SIMDI_CONST(c_f16max, (127 + 16) << 23); // all FP32 values >=this round to +inf + static const STBIR__SIMDI_CONST(c_nanbit, 0x200); + static const STBIR__SIMDI_CONST(c_infty_as_fp16, 0x7c00); + static const STBIR__SIMDI_CONST(c_min_normal, (127 - 14) << 23); // smallest FP32 that yields a normalized FP16 + static const STBIR__SIMDI_CONST(c_subnorm_magic, ((127 - 15) + (23 - 10) + 1) << 23); + static const STBIR__SIMDI_CONST(c_normal_bias, 0xfff - ((127 - 15) << 23)); // adjust exponent and add mantissa rounding + + __m128 f = _mm_loadu_ps(input); + __m128 msign = _mm_castsi128_ps(STBIR__CONSTI(mask_sign)); + __m128 justsign = _mm_and_ps(msign, f); + __m128 absf = _mm_xor_ps(f, justsign); + __m128i absf_int = _mm_castps_si128(absf); // the cast is "free" (extra bypass latency, but no thruput hit) + __m128i f16max = STBIR__CONSTI(c_f16max); + __m128 b_isnan = _mm_cmpunord_ps(absf, absf); // is this a NaN? + __m128i b_isregular = _mm_cmpgt_epi32(f16max, absf_int); // (sub)normalized or special? + __m128i nanbit = _mm_and_si128(_mm_castps_si128(b_isnan), STBIR__CONSTI(c_nanbit)); + __m128i inf_or_nan = _mm_or_si128(nanbit, STBIR__CONSTI(c_infty_as_fp16)); // output for specials + + __m128i min_normal = STBIR__CONSTI(c_min_normal); + __m128i b_issub = _mm_cmpgt_epi32(min_normal, absf_int); + + // "result is subnormal" path + __m128 subnorm1 = _mm_add_ps(absf, _mm_castsi128_ps(STBIR__CONSTI(c_subnorm_magic))); // magic value to round output mantissa + __m128i subnorm2 = _mm_sub_epi32(_mm_castps_si128(subnorm1), STBIR__CONSTI(c_subnorm_magic)); // subtract out bias + + // "result is normal" path + __m128i mantoddbit = _mm_slli_epi32(absf_int, 31 - 13); // shift bit 13 (mantissa LSB) to sign + __m128i mantodd = _mm_srai_epi32(mantoddbit, 31); // -1 if FP16 mantissa odd, else 0 + + __m128i round1 = _mm_add_epi32(absf_int, STBIR__CONSTI(c_normal_bias)); + __m128i round2 = _mm_sub_epi32(round1, mantodd); // if mantissa LSB odd, bias towards rounding up (RTNE) + __m128i normal = _mm_srli_epi32(round2, 13); // rounded result + + // combine the two non-specials + __m128i nonspecial = _mm_or_si128(_mm_and_si128(subnorm2, b_issub), _mm_andnot_si128(b_issub, normal)); + + // merge in specials as well + __m128i joined = _mm_or_si128(_mm_and_si128(nonspecial, b_isregular), _mm_andnot_si128(b_isregular, inf_or_nan)); + + __m128i sign_shift = _mm_srai_epi32(_mm_castps_si128(justsign), 16); + __m128i final2, final= _mm_or_si128(joined, sign_shift); + + f = _mm_loadu_ps(input+4); + justsign = _mm_and_ps(msign, f); + absf = _mm_xor_ps(f, justsign); + absf_int = _mm_castps_si128(absf); // the cast is "free" (extra bypass latency, but no thruput hit) + b_isnan = _mm_cmpunord_ps(absf, absf); // is this a NaN? + b_isregular = _mm_cmpgt_epi32(f16max, absf_int); // (sub)normalized or special? + nanbit = _mm_and_si128(_mm_castps_si128(b_isnan), c_nanbit); + inf_or_nan = _mm_or_si128(nanbit, STBIR__CONSTI(c_infty_as_fp16)); // output for specials + + b_issub = _mm_cmpgt_epi32(min_normal, absf_int); + + // "result is subnormal" path + subnorm1 = _mm_add_ps(absf, _mm_castsi128_ps(STBIR__CONSTI(c_subnorm_magic))); // magic value to round output mantissa + subnorm2 = _mm_sub_epi32(_mm_castps_si128(subnorm1), STBIR__CONSTI(c_subnorm_magic)); // subtract out bias + + // "result is normal" path + mantoddbit = _mm_slli_epi32(absf_int, 31 - 13); // shift bit 13 (mantissa LSB) to sign + mantodd = _mm_srai_epi32(mantoddbit, 31); // -1 if FP16 mantissa odd, else 0 + + round1 = _mm_add_epi32(absf_int, STBIR__CONSTI(c_normal_bias)); + round2 = _mm_sub_epi32(round1, mantodd); // if mantissa LSB odd, bias towards rounding up (RTNE) + normal = _mm_srli_epi32(round2, 13); // rounded result + + // combine the two non-specials + nonspecial = _mm_or_si128(_mm_and_si128(subnorm2, b_issub), _mm_andnot_si128(b_issub, normal)); + + // merge in specials as well + joined = _mm_or_si128(_mm_and_si128(nonspecial, b_isregular), _mm_andnot_si128(b_isregular, inf_or_nan)); + + sign_shift = _mm_srai_epi32(_mm_castps_si128(justsign), 16); + final2 = _mm_or_si128(joined, sign_shift); + final = _mm_packs_epi32(final, final2); + stbir__simdi_store( output,final ); + } + +#elif defined(STBIR_NEON) && defined(_MSC_VER) && defined(_M_ARM64) && !defined(__clang__) // 64-bit ARM on MSVC (not clang) + + static stbir__inline void stbir__half_to_float_SIMD(float * output, stbir__FP16 const * input) + { + float16x4_t in0 = vld1_f16(input + 0); + float16x4_t in1 = vld1_f16(input + 4); + vst1q_f32(output + 0, vcvt_f32_f16(in0)); + vst1q_f32(output + 4, vcvt_f32_f16(in1)); + } + + static stbir__inline void stbir__float_to_half_SIMD(stbir__FP16 * output, float const * input) + { + float16x4_t out0 = vcvt_f16_f32(vld1q_f32(input + 0)); + float16x4_t out1 = vcvt_f16_f32(vld1q_f32(input + 4)); + vst1_f16(output+0, out0); + vst1_f16(output+4, out1); + } + + static stbir__inline float stbir__half_to_float( stbir__FP16 h ) + { + return vgetq_lane_f32(vcvt_f32_f16(vld1_dup_f16(&h)), 0); + } + + static stbir__inline stbir__FP16 stbir__float_to_half( float f ) + { + return vget_lane_f16(vcvt_f16_f32(vdupq_n_f32(f)), 0).n16_u16[0]; + } + +#elif defined(STBIR_NEON) && ( defined( _M_ARM64 ) || defined( __aarch64__ ) || defined( __arm64__ ) ) // 64-bit ARM + + static stbir__inline void stbir__half_to_float_SIMD(float * output, stbir__FP16 const * input) + { + float16x8_t in = vld1q_f16(input); + vst1q_f32(output + 0, vcvt_f32_f16(vget_low_f16(in))); + vst1q_f32(output + 4, vcvt_f32_f16(vget_high_f16(in))); + } + + static stbir__inline void stbir__float_to_half_SIMD(stbir__FP16 * output, float const * input) + { + float16x4_t out0 = vcvt_f16_f32(vld1q_f32(input + 0)); + float16x4_t out1 = vcvt_f16_f32(vld1q_f32(input + 4)); + vst1q_f16(output, vcombine_f16(out0, out1)); + } + + static stbir__inline float stbir__half_to_float( stbir__FP16 h ) + { + return vgetq_lane_f32(vcvt_f32_f16(vdup_n_f16(h)), 0); + } + + static stbir__inline stbir__FP16 stbir__float_to_half( float f ) + { + return vget_lane_f16(vcvt_f16_f32(vdupq_n_f32(f)), 0); + } + +#elif defined(STBIR_WASM) || (defined(STBIR_NEON) && (defined(_MSC_VER) || defined(_M_ARM) || defined(__arm__))) // WASM or 32-bit ARM on MSVC/clang + + static stbir__inline void stbir__half_to_float_SIMD(float * output, stbir__FP16 const * input) + { + for (int i=0; i<8; i++) + { + output[i] = stbir__half_to_float(input[i]); + } + } + static stbir__inline void stbir__float_to_half_SIMD(stbir__FP16 * output, float const * input) + { + for (int i=0; i<8; i++) + { + output[i] = stbir__float_to_half(input[i]); + } + } + +#endif + + +#ifdef STBIR_SIMD + +#define stbir__simdf_0123to3333( out, reg ) (out) = stbir__simdf_swiz( reg, 3,3,3,3 ) +#define stbir__simdf_0123to2222( out, reg ) (out) = stbir__simdf_swiz( reg, 2,2,2,2 ) +#define stbir__simdf_0123to1111( out, reg ) (out) = stbir__simdf_swiz( reg, 1,1,1,1 ) +#define stbir__simdf_0123to0000( out, reg ) (out) = stbir__simdf_swiz( reg, 0,0,0,0 ) +#define stbir__simdf_0123to0003( out, reg ) (out) = stbir__simdf_swiz( reg, 0,0,0,3 ) +#define stbir__simdf_0123to0001( out, reg ) (out) = stbir__simdf_swiz( reg, 0,0,0,1 ) +#define stbir__simdf_0123to1122( out, reg ) (out) = stbir__simdf_swiz( reg, 1,1,2,2 ) +#define stbir__simdf_0123to2333( out, reg ) (out) = stbir__simdf_swiz( reg, 2,3,3,3 ) +#define stbir__simdf_0123to0023( out, reg ) (out) = stbir__simdf_swiz( reg, 0,0,2,3 ) +#define stbir__simdf_0123to1230( out, reg ) (out) = stbir__simdf_swiz( reg, 1,2,3,0 ) +#define stbir__simdf_0123to2103( out, reg ) (out) = stbir__simdf_swiz( reg, 2,1,0,3 ) +#define stbir__simdf_0123to3210( out, reg ) (out) = stbir__simdf_swiz( reg, 3,2,1,0 ) +#define stbir__simdf_0123to2301( out, reg ) (out) = stbir__simdf_swiz( reg, 2,3,0,1 ) +#define stbir__simdf_0123to3012( out, reg ) (out) = stbir__simdf_swiz( reg, 3,0,1,2 ) +#define stbir__simdf_0123to0011( out, reg ) (out) = stbir__simdf_swiz( reg, 0,0,1,1 ) +#define stbir__simdf_0123to1100( out, reg ) (out) = stbir__simdf_swiz( reg, 1,1,0,0 ) +#define stbir__simdf_0123to2233( out, reg ) (out) = stbir__simdf_swiz( reg, 2,2,3,3 ) +#define stbir__simdf_0123to1133( out, reg ) (out) = stbir__simdf_swiz( reg, 1,1,3,3 ) +#define stbir__simdf_0123to0022( out, reg ) (out) = stbir__simdf_swiz( reg, 0,0,2,2 ) +#define stbir__simdf_0123to1032( out, reg ) (out) = stbir__simdf_swiz( reg, 1,0,3,2 ) + +typedef union stbir__simdi_u32 +{ + stbir_uint32 m128i_u32[4]; + int m128i_i32[4]; + stbir__simdi m128i_i128; +} stbir__simdi_u32; + +static const int STBIR_mask[9] = { 0,0,0,-1,-1,-1,0,0,0 }; + +static const STBIR__SIMDF_CONST(STBIR_max_uint8_as_float, stbir__max_uint8_as_float); +static const STBIR__SIMDF_CONST(STBIR_max_uint16_as_float, stbir__max_uint16_as_float); +static const STBIR__SIMDF_CONST(STBIR_max_uint8_as_float_inverted, stbir__max_uint8_as_float_inverted); +static const STBIR__SIMDF_CONST(STBIR_max_uint16_as_float_inverted, stbir__max_uint16_as_float_inverted); + +static const STBIR__SIMDF_CONST(STBIR_simd_point5, 0.5f); +static const STBIR__SIMDF_CONST(STBIR_ones, 1.0f); +static const STBIR__SIMDI_CONST(STBIR_almost_zero, (127 - 13) << 23); +static const STBIR__SIMDI_CONST(STBIR_almost_one, 0x3f7fffff); +static const STBIR__SIMDI_CONST(STBIR_mantissa_mask, 0xff); +static const STBIR__SIMDI_CONST(STBIR_topscale, 0x02000000); + +// Basically, in simd mode, we unroll the proper amount, and we don't want +// the non-simd remnant loops to be unroll because they only run a few times +// Adding this switch saves about 5K on clang which is Captain Unroll the 3rd. +#define STBIR_SIMD_STREAMOUT_PTR( star ) STBIR_STREAMOUT_PTR( star ) +#define STBIR_SIMD_NO_UNROLL(ptr) STBIR_NO_UNROLL(ptr) +#define STBIR_SIMD_NO_UNROLL_LOOP_START STBIR_NO_UNROLL_LOOP_START +#define STBIR_SIMD_NO_UNROLL_LOOP_START_INF_FOR STBIR_NO_UNROLL_LOOP_START_INF_FOR + +#ifdef STBIR_MEMCPY +#undef STBIR_MEMCPY +#endif +#define STBIR_MEMCPY stbir_simd_memcpy + +// override normal use of memcpy with much simpler copy (faster and smaller with our sized copies) +static void stbir_simd_memcpy( void * dest, void const * src, size_t bytes ) +{ + char STBIR_SIMD_STREAMOUT_PTR (*) d = (char*) dest; + char STBIR_SIMD_STREAMOUT_PTR( * ) d_end = ((char*) dest) + bytes; + ptrdiff_t ofs_to_src = (char*)src - (char*)dest; + + // check overlaps + STBIR_ASSERT( ( ( d >= ( (char*)src) + bytes ) ) || ( ( d + bytes ) <= (char*)src ) ); + + if ( bytes < (16*stbir__simdfX_float_count) ) + { + if ( bytes < 16 ) + { + if ( bytes ) + { + STBIR_SIMD_NO_UNROLL_LOOP_START + do + { + STBIR_SIMD_NO_UNROLL(d); + d[ 0 ] = d[ ofs_to_src ]; + ++d; + } while ( d < d_end ); + } + } + else + { + stbir__simdf x; + // do one unaligned to get us aligned for the stream out below + stbir__simdf_load( x, ( d + ofs_to_src ) ); + stbir__simdf_store( d, x ); + d = (char*)( ( ( (size_t)d ) + 16 ) & ~15 ); + + STBIR_SIMD_NO_UNROLL_LOOP_START_INF_FOR + for(;;) + { + STBIR_SIMD_NO_UNROLL(d); + + if ( d > ( d_end - 16 ) ) + { + if ( d == d_end ) + return; + d = d_end - 16; + } + + stbir__simdf_load( x, ( d + ofs_to_src ) ); + stbir__simdf_store( d, x ); + d += 16; + } + } + } + else + { + stbir__simdfX x0,x1,x2,x3; + + // do one unaligned to get us aligned for the stream out below + stbir__simdfX_load( x0, ( d + ofs_to_src ) + 0*stbir__simdfX_float_count ); + stbir__simdfX_load( x1, ( d + ofs_to_src ) + 4*stbir__simdfX_float_count ); + stbir__simdfX_load( x2, ( d + ofs_to_src ) + 8*stbir__simdfX_float_count ); + stbir__simdfX_load( x3, ( d + ofs_to_src ) + 12*stbir__simdfX_float_count ); + stbir__simdfX_store( d + 0*stbir__simdfX_float_count, x0 ); + stbir__simdfX_store( d + 4*stbir__simdfX_float_count, x1 ); + stbir__simdfX_store( d + 8*stbir__simdfX_float_count, x2 ); + stbir__simdfX_store( d + 12*stbir__simdfX_float_count, x3 ); + d = (char*)( ( ( (size_t)d ) + (16*stbir__simdfX_float_count) ) & ~((16*stbir__simdfX_float_count)-1) ); + + STBIR_SIMD_NO_UNROLL_LOOP_START_INF_FOR + for(;;) + { + STBIR_SIMD_NO_UNROLL(d); + + if ( d > ( d_end - (16*stbir__simdfX_float_count) ) ) + { + if ( d == d_end ) + return; + d = d_end - (16*stbir__simdfX_float_count); + } + + stbir__simdfX_load( x0, ( d + ofs_to_src ) + 0*stbir__simdfX_float_count ); + stbir__simdfX_load( x1, ( d + ofs_to_src ) + 4*stbir__simdfX_float_count ); + stbir__simdfX_load( x2, ( d + ofs_to_src ) + 8*stbir__simdfX_float_count ); + stbir__simdfX_load( x3, ( d + ofs_to_src ) + 12*stbir__simdfX_float_count ); + stbir__simdfX_store( d + 0*stbir__simdfX_float_count, x0 ); + stbir__simdfX_store( d + 4*stbir__simdfX_float_count, x1 ); + stbir__simdfX_store( d + 8*stbir__simdfX_float_count, x2 ); + stbir__simdfX_store( d + 12*stbir__simdfX_float_count, x3 ); + d += (16*stbir__simdfX_float_count); + } + } +} + +// memcpy that is specically intentionally overlapping (src is smaller then dest, so can be +// a normal forward copy, bytes is divisible by 4 and bytes is greater than or equal to +// the diff between dest and src) +static void stbir_overlapping_memcpy( void * dest, void const * src, size_t bytes ) +{ + char STBIR_SIMD_STREAMOUT_PTR (*) sd = (char*) src; + char STBIR_SIMD_STREAMOUT_PTR( * ) s_end = ((char*) src) + bytes; + ptrdiff_t ofs_to_dest = (char*)dest - (char*)src; + + if ( ofs_to_dest >= 16 ) // is the overlap more than 16 away? + { + char STBIR_SIMD_STREAMOUT_PTR( * ) s_end16 = ((char*) src) + (bytes&~15); + STBIR_SIMD_NO_UNROLL_LOOP_START + do + { + stbir__simdf x; + STBIR_SIMD_NO_UNROLL(sd); + stbir__simdf_load( x, sd ); + stbir__simdf_store( ( sd + ofs_to_dest ), x ); + sd += 16; + } while ( sd < s_end16 ); + + if ( sd == s_end ) + return; + } + + do + { + STBIR_SIMD_NO_UNROLL(sd); + *(int*)( sd + ofs_to_dest ) = *(int*) sd; + sd += 4; + } while ( sd < s_end ); +} + +#else // no SSE2 + +// when in scalar mode, we let unrolling happen, so this macro just does the __restrict +#define STBIR_SIMD_STREAMOUT_PTR( star ) STBIR_STREAMOUT_PTR( star ) +#define STBIR_SIMD_NO_UNROLL(ptr) +#define STBIR_SIMD_NO_UNROLL_LOOP_START +#define STBIR_SIMD_NO_UNROLL_LOOP_START_INF_FOR + +#endif // SSE2 + + +#ifdef STBIR_PROFILE + +#ifndef STBIR_PROFILE_FUNC + +#if defined(_x86_64) || defined( __x86_64__ ) || defined( _M_X64 ) || defined(__x86_64) || defined(__SSE2__) || defined(STBIR_SSE) || defined( _M_IX86_FP ) || defined(__i386) || defined( __i386__ ) || defined( _M_IX86 ) || defined( _X86_ ) + +#ifdef _MSC_VER + + STBIRDEF stbir_uint64 __rdtsc(); + #define STBIR_PROFILE_FUNC() __rdtsc() + +#else // non msvc + + static stbir__inline stbir_uint64 STBIR_PROFILE_FUNC() + { + stbir_uint32 lo, hi; + asm volatile ("rdtsc" : "=a" (lo), "=d" (hi) ); + return ( ( (stbir_uint64) hi ) << 32 ) | ( (stbir_uint64) lo ); + } + +#endif // msvc + +#elif defined( _M_ARM64 ) || defined( __aarch64__ ) || defined( __arm64__ ) || defined(__ARM_NEON__) + +#if defined( _MSC_VER ) && !defined(__clang__) + + #define STBIR_PROFILE_FUNC() _ReadStatusReg(ARM64_CNTVCT) + +#else + + static stbir__inline stbir_uint64 STBIR_PROFILE_FUNC() + { + stbir_uint64 tsc; + asm volatile("mrs %0, cntvct_el0" : "=r" (tsc)); + return tsc; + } + +#endif + +#else // x64, arm + +#error Unknown platform for profiling. + +#endif // x64, arm + +#endif // STBIR_PROFILE_FUNC + +#define STBIR_ONLY_PROFILE_GET_SPLIT_INFO ,stbir__per_split_info * split_info +#define STBIR_ONLY_PROFILE_SET_SPLIT_INFO ,split_info + +#define STBIR_ONLY_PROFILE_BUILD_GET_INFO ,stbir__info * profile_info +#define STBIR_ONLY_PROFILE_BUILD_SET_INFO ,profile_info + +// super light-weight micro profiler +#define STBIR_PROFILE_START_ll( info, wh ) { stbir_uint64 wh##thiszonetime = STBIR_PROFILE_FUNC(); stbir_uint64 * wh##save_parent_excluded_ptr = info->current_zone_excluded_ptr; stbir_uint64 wh##current_zone_excluded = 0; info->current_zone_excluded_ptr = &wh##current_zone_excluded; +#define STBIR_PROFILE_END_ll( info, wh ) wh##thiszonetime = STBIR_PROFILE_FUNC() - wh##thiszonetime; info->profile.named.wh += wh##thiszonetime - wh##current_zone_excluded; *wh##save_parent_excluded_ptr += wh##thiszonetime; info->current_zone_excluded_ptr = wh##save_parent_excluded_ptr; } +#define STBIR_PROFILE_FIRST_START_ll( info, wh ) { int i; info->current_zone_excluded_ptr = &info->profile.named.total; for(i=0;iprofile.array);i++) info->profile.array[i]=0; } STBIR_PROFILE_START_ll( info, wh ); +#define STBIR_PROFILE_CLEAR_EXTRAS_ll( info, num ) { int extra; for(extra=1;extra<(num);extra++) { int i; for(i=0;iprofile.array);i++) (info)[extra].profile.array[i]=0; } } + +// for thread data +#define STBIR_PROFILE_START( wh ) STBIR_PROFILE_START_ll( split_info, wh ) +#define STBIR_PROFILE_END( wh ) STBIR_PROFILE_END_ll( split_info, wh ) +#define STBIR_PROFILE_FIRST_START( wh ) STBIR_PROFILE_FIRST_START_ll( split_info, wh ) +#define STBIR_PROFILE_CLEAR_EXTRAS() STBIR_PROFILE_CLEAR_EXTRAS_ll( split_info, split_count ) + +// for build data +#define STBIR_PROFILE_BUILD_START( wh ) STBIR_PROFILE_START_ll( profile_info, wh ) +#define STBIR_PROFILE_BUILD_END( wh ) STBIR_PROFILE_END_ll( profile_info, wh ) +#define STBIR_PROFILE_BUILD_FIRST_START( wh ) STBIR_PROFILE_FIRST_START_ll( profile_info, wh ) +#define STBIR_PROFILE_BUILD_CLEAR( info ) { int i; for(i=0;iprofile.array);i++) info->profile.array[i]=0; } + +#else // no profile + +#define STBIR_ONLY_PROFILE_GET_SPLIT_INFO +#define STBIR_ONLY_PROFILE_SET_SPLIT_INFO + +#define STBIR_ONLY_PROFILE_BUILD_GET_INFO +#define STBIR_ONLY_PROFILE_BUILD_SET_INFO + +#define STBIR_PROFILE_START( wh ) +#define STBIR_PROFILE_END( wh ) +#define STBIR_PROFILE_FIRST_START( wh ) +#define STBIR_PROFILE_CLEAR_EXTRAS( ) + +#define STBIR_PROFILE_BUILD_START( wh ) +#define STBIR_PROFILE_BUILD_END( wh ) +#define STBIR_PROFILE_BUILD_FIRST_START( wh ) +#define STBIR_PROFILE_BUILD_CLEAR( info ) + +#endif // stbir_profile + +#ifndef STBIR_CEILF +#include +#if _MSC_VER <= 1200 // support VC6 for Sean +#define STBIR_CEILF(x) ((float)ceil((float)(x))) +#define STBIR_FLOORF(x) ((float)floor((float)(x))) +#else +#define STBIR_CEILF(x) ceilf(x) +#define STBIR_FLOORF(x) floorf(x) +#endif +#endif + +#ifndef STBIR_MEMCPY +// For memcpy +#include +#define STBIR_MEMCPY( dest, src, len ) memcpy( dest, src, len ) +#endif + +#ifndef STBIR_SIMD + +// memcpy that is specifically intentionally overlapping (src is smaller then dest, so can be +// a normal forward copy, bytes is divisible by 4 and bytes is greater than or equal to +// the diff between dest and src) +static void stbir_overlapping_memcpy( void * dest, void const * src, size_t bytes ) +{ + char STBIR_SIMD_STREAMOUT_PTR (*) sd = (char*) src; + char STBIR_SIMD_STREAMOUT_PTR( * ) s_end = ((char*) src) + bytes; + ptrdiff_t ofs_to_dest = (char*)dest - (char*)src; + + if ( ofs_to_dest >= 8 ) // is the overlap more than 8 away + { + char STBIR_SIMD_STREAMOUT_PTR( * ) s_end8 = ((char*) src) + (bytes&~7); + + if ( ( ( ((ptrdiff_t)dest)|((ptrdiff_t)src) ) & 7 ) == 0 ) // is it 8byte aligned? + { + STBIR_NO_UNROLL_LOOP_START + do + { + STBIR_NO_UNROLL(sd); + *(stbir_uint64*)( sd + ofs_to_dest ) = *(stbir_uint64*) sd; + sd += 8; + } while ( sd < s_end8 ); + } + else + { + STBIR_NO_UNROLL_LOOP_START + do + { + int a,b; + STBIR_NO_UNROLL(sd); + a = ((int*)sd)[0]; + b = ((int*)sd)[1]; + ((int*)( sd + ofs_to_dest ))[0] = a; + ((int*)( sd + ofs_to_dest ))[1] = b; + sd += 8; + } while ( sd < s_end8 ); + } + + if ( sd == s_end ) + return; + } + + STBIR_NO_UNROLL_LOOP_START + do + { + STBIR_NO_UNROLL(sd); + *(int*)( sd + ofs_to_dest ) = *(int*) sd; + sd += 4; + } while ( sd < s_end ); +} + +#endif + +static float stbir__filter_trapezoid(float x, float scale, void * user_data) +{ + float halfscale = scale / 2; + float t = 0.5f + halfscale; + STBIR_ASSERT(scale <= 1); + STBIR__UNUSED(user_data); + + if ( x < 0.0f ) x = -x; + + if (x >= t) + return 0.0f; + else + { + float r = 0.5f - halfscale; + if (x <= r) + return 1.0f; + else + return (t - x) / scale; + } +} + +static float stbir__support_trapezoid(float scale, void * user_data) +{ + STBIR__UNUSED(user_data); + return 0.5f + scale / 2.0f; +} + +static float stbir__filter_triangle(float x, float s, void * user_data) +{ + STBIR__UNUSED(s); + STBIR__UNUSED(user_data); + + if ( x < 0.0f ) x = -x; + + if (x <= 1.0f) + return 1.0f - x; + else + return 0.0f; +} + +static float stbir__filter_point(float x, float s, void * user_data) +{ + STBIR__UNUSED(x); + STBIR__UNUSED(s); + STBIR__UNUSED(user_data); + + return 1.0f; +} + +static float stbir__filter_cubic(float x, float s, void * user_data) +{ + STBIR__UNUSED(s); + STBIR__UNUSED(user_data); + + if ( x < 0.0f ) x = -x; + + if (x < 1.0f) + return (4.0f + x*x*(3.0f*x - 6.0f))/6.0f; + else if (x < 2.0f) + return (8.0f + x*(-12.0f + x*(6.0f - x)))/6.0f; + + return (0.0f); +} + +static float stbir__filter_catmullrom(float x, float s, void * user_data) +{ + STBIR__UNUSED(s); + STBIR__UNUSED(user_data); + + if ( x < 0.0f ) x = -x; + + if (x < 1.0f) + return 1.0f - x*x*(2.5f - 1.5f*x); + else if (x < 2.0f) + return 2.0f - x*(4.0f + x*(0.5f*x - 2.5f)); + + return (0.0f); +} + +static float stbir__filter_mitchell(float x, float s, void * user_data) +{ + STBIR__UNUSED(s); + STBIR__UNUSED(user_data); + + if ( x < 0.0f ) x = -x; + + if (x < 1.0f) + return (16.0f + x*x*(21.0f * x - 36.0f))/18.0f; + else if (x < 2.0f) + return (32.0f + x*(-60.0f + x*(36.0f - 7.0f*x)))/18.0f; + + return (0.0f); +} + +static float stbir__support_zeropoint5(float s, void * user_data) +{ + STBIR__UNUSED(s); + STBIR__UNUSED(user_data); + return 0.5f; +} + +static float stbir__support_one(float s, void * user_data) +{ + STBIR__UNUSED(s); + STBIR__UNUSED(user_data); + return 1; +} + +static float stbir__support_two(float s, void * user_data) +{ + STBIR__UNUSED(s); + STBIR__UNUSED(user_data); + return 2; +} + +// This is the maximum number of input samples that can affect an output sample +// with the given filter from the output pixel's perspective +static int stbir__get_filter_pixel_width(stbir__support_callback * support, float scale, void * user_data) +{ + STBIR_ASSERT(support != 0); + + if ( scale >= ( 1.0f-stbir__small_float ) ) // upscale + return (int)STBIR_CEILF(support(1.0f/scale,user_data) * 2.0f); + else + return (int)STBIR_CEILF(support(scale,user_data) * 2.0f / scale); +} + +// this is how many coefficents per run of the filter (which is different +// from the filter_pixel_width depending on if we are scattering or gathering) +static int stbir__get_coefficient_width(stbir__sampler * samp, int is_gather, void * user_data) +{ + float scale = samp->scale_info.scale; + stbir__support_callback * support = samp->filter_support; + + switch( is_gather ) + { + case 1: + return (int)STBIR_CEILF(support(1.0f / scale, user_data) * 2.0f); + case 2: + return (int)STBIR_CEILF(support(scale, user_data) * 2.0f / scale); + case 0: + return (int)STBIR_CEILF(support(scale, user_data) * 2.0f); + default: + STBIR_ASSERT( (is_gather >= 0 ) && (is_gather <= 2 ) ); + return 0; + } +} + +static int stbir__get_contributors(stbir__sampler * samp, int is_gather) +{ + if (is_gather) + return samp->scale_info.output_sub_size; + else + return (samp->scale_info.input_full_size + samp->filter_pixel_margin * 2); +} + +static int stbir__edge_zero_full( int n, int max ) +{ + STBIR__UNUSED(n); + STBIR__UNUSED(max); + return 0; // NOTREACHED +} + +static int stbir__edge_clamp_full( int n, int max ) +{ + if (n < 0) + return 0; + + if (n >= max) + return max - 1; + + return n; // NOTREACHED +} + +static int stbir__edge_reflect_full( int n, int max ) +{ + if (n < 0) + { + if (n > -max) + return -n; + else + return max - 1; + } + + if (n >= max) + { + int max2 = max * 2; + if (n >= max2) + return 0; + else + return max2 - n - 1; + } + + return n; // NOTREACHED +} + +static int stbir__edge_wrap_full( int n, int max ) +{ + if (n >= 0) + return (n % max); + else + { + int m = (-n) % max; + + if (m != 0) + m = max - m; + + return (m); + } +} + +typedef int stbir__edge_wrap_func( int n, int max ); +static stbir__edge_wrap_func * stbir__edge_wrap_slow[] = +{ + stbir__edge_clamp_full, // STBIR_EDGE_CLAMP + stbir__edge_reflect_full, // STBIR_EDGE_REFLECT + stbir__edge_wrap_full, // STBIR_EDGE_WRAP + stbir__edge_zero_full, // STBIR_EDGE_ZERO +}; + +stbir__inline static int stbir__edge_wrap(stbir_edge edge, int n, int max) +{ + // avoid per-pixel switch + if (n >= 0 && n < max) + return n; + return stbir__edge_wrap_slow[edge]( n, max ); +} + +#define STBIR__MERGE_RUNS_PIXEL_THRESHOLD 16 + +// get information on the extents of a sampler +static void stbir__get_extents( stbir__sampler * samp, stbir__extents * scanline_extents ) +{ + int j, stop; + int left_margin, right_margin; + int min_n = 0x7fffffff, max_n = -0x7fffffff; + int min_left = 0x7fffffff, max_left = -0x7fffffff; + int min_right = 0x7fffffff, max_right = -0x7fffffff; + stbir_edge edge = samp->edge; + stbir__contributors* contributors = samp->contributors; + int output_sub_size = samp->scale_info.output_sub_size; + int input_full_size = samp->scale_info.input_full_size; + int filter_pixel_margin = samp->filter_pixel_margin; + + STBIR_ASSERT( samp->is_gather ); + + stop = output_sub_size; + for (j = 0; j < stop; j++ ) + { + STBIR_ASSERT( contributors[j].n1 >= contributors[j].n0 ); + if ( contributors[j].n0 < min_n ) + { + min_n = contributors[j].n0; + stop = j + filter_pixel_margin; // if we find a new min, only scan another filter width + if ( stop > output_sub_size ) stop = output_sub_size; + } + } + + stop = 0; + for (j = output_sub_size - 1; j >= stop; j-- ) + { + STBIR_ASSERT( contributors[j].n1 >= contributors[j].n0 ); + if ( contributors[j].n1 > max_n ) + { + max_n = contributors[j].n1; + stop = j - filter_pixel_margin; // if we find a new max, only scan another filter width + if (stop<0) stop = 0; + } + } + + STBIR_ASSERT( scanline_extents->conservative.n0 <= min_n ); + STBIR_ASSERT( scanline_extents->conservative.n1 >= max_n ); + + // now calculate how much into the margins we really read + left_margin = 0; + if ( min_n < 0 ) + { + left_margin = -min_n; + min_n = 0; + } + + right_margin = 0; + if ( max_n >= input_full_size ) + { + right_margin = max_n - input_full_size + 1; + max_n = input_full_size - 1; + } + + // index 1 is margin pixel extents (how many pixels we hang over the edge) + scanline_extents->edge_sizes[0] = left_margin; + scanline_extents->edge_sizes[1] = right_margin; + + // index 2 is pixels read from the input + scanline_extents->spans[0].n0 = min_n; + scanline_extents->spans[0].n1 = max_n; + scanline_extents->spans[0].pixel_offset_for_input = min_n; + + // default to no other input range + scanline_extents->spans[1].n0 = 0; + scanline_extents->spans[1].n1 = -1; + scanline_extents->spans[1].pixel_offset_for_input = 0; + + // don't have to do edge calc for zero clamp + if ( edge == STBIR_EDGE_ZERO ) + return; + + // convert margin pixels to the pixels within the input (min and max) + for( j = -left_margin ; j < 0 ; j++ ) + { + int p = stbir__edge_wrap( edge, j, input_full_size ); + if ( p < min_left ) + min_left = p; + if ( p > max_left ) + max_left = p; + } + + for( j = input_full_size ; j < (input_full_size + right_margin) ; j++ ) + { + int p = stbir__edge_wrap( edge, j, input_full_size ); + if ( p < min_right ) + min_right = p; + if ( p > max_right ) + max_right = p; + } + + // merge the left margin pixel region if it connects within 4 pixels of main pixel region + if ( min_left != 0x7fffffff ) + { + if ( ( ( min_left <= min_n ) && ( ( max_left + STBIR__MERGE_RUNS_PIXEL_THRESHOLD ) >= min_n ) ) || + ( ( min_n <= min_left ) && ( ( max_n + STBIR__MERGE_RUNS_PIXEL_THRESHOLD ) >= max_left ) ) ) + { + scanline_extents->spans[0].n0 = min_n = stbir__min( min_n, min_left ); + scanline_extents->spans[0].n1 = max_n = stbir__max( max_n, max_left ); + scanline_extents->spans[0].pixel_offset_for_input = min_n; + left_margin = 0; + } + } + + // merge the right margin pixel region if it connects within 4 pixels of main pixel region + if ( min_right != 0x7fffffff ) + { + if ( ( ( min_right <= min_n ) && ( ( max_right + STBIR__MERGE_RUNS_PIXEL_THRESHOLD ) >= min_n ) ) || + ( ( min_n <= min_right ) && ( ( max_n + STBIR__MERGE_RUNS_PIXEL_THRESHOLD ) >= max_right ) ) ) + { + scanline_extents->spans[0].n0 = min_n = stbir__min( min_n, min_right ); + scanline_extents->spans[0].n1 = max_n = stbir__max( max_n, max_right ); + scanline_extents->spans[0].pixel_offset_for_input = min_n; + right_margin = 0; + } + } + + STBIR_ASSERT( scanline_extents->conservative.n0 <= min_n ); + STBIR_ASSERT( scanline_extents->conservative.n1 >= max_n ); + + // you get two ranges when you have the WRAP edge mode and you are doing just the a piece of the resize + // so you need to get a second run of pixels from the opposite side of the scanline (which you + // wouldn't need except for WRAP) + + + // if we can't merge the min_left range, add it as a second range + if ( ( left_margin ) && ( min_left != 0x7fffffff ) ) + { + stbir__span * newspan = scanline_extents->spans + 1; + STBIR_ASSERT( right_margin == 0 ); + if ( min_left < scanline_extents->spans[0].n0 ) + { + scanline_extents->spans[1].pixel_offset_for_input = scanline_extents->spans[0].n0; + scanline_extents->spans[1].n0 = scanline_extents->spans[0].n0; + scanline_extents->spans[1].n1 = scanline_extents->spans[0].n1; + --newspan; + } + newspan->pixel_offset_for_input = min_left; + newspan->n0 = -left_margin; + newspan->n1 = ( max_left - min_left ) - left_margin; + scanline_extents->edge_sizes[0] = 0; // don't need to copy the left margin, since we are directly decoding into the margin + } + // if we can't merge the min_right range, add it as a second range + else + if ( ( right_margin ) && ( min_right != 0x7fffffff ) ) + { + stbir__span * newspan = scanline_extents->spans + 1; + if ( min_right < scanline_extents->spans[0].n0 ) + { + scanline_extents->spans[1].pixel_offset_for_input = scanline_extents->spans[0].n0; + scanline_extents->spans[1].n0 = scanline_extents->spans[0].n0; + scanline_extents->spans[1].n1 = scanline_extents->spans[0].n1; + --newspan; + } + newspan->pixel_offset_for_input = min_right; + newspan->n0 = scanline_extents->spans[1].n1 + 1; + newspan->n1 = scanline_extents->spans[1].n1 + 1 + ( max_right - min_right ); + scanline_extents->edge_sizes[1] = 0; // don't need to copy the right margin, since we are directly decoding into the margin + } + + // sort the spans into write output order + if ( ( scanline_extents->spans[1].n1 > scanline_extents->spans[1].n0 ) && ( scanline_extents->spans[0].n0 > scanline_extents->spans[1].n0 ) ) + { + stbir__span tspan = scanline_extents->spans[0]; + scanline_extents->spans[0] = scanline_extents->spans[1]; + scanline_extents->spans[1] = tspan; + } +} + +static void stbir__calculate_in_pixel_range( int * first_pixel, int * last_pixel, float out_pixel_center, float out_filter_radius, float inv_scale, float out_shift, int input_size, stbir_edge edge ) +{ + int first, last; + float out_pixel_influence_lowerbound = out_pixel_center - out_filter_radius; + float out_pixel_influence_upperbound = out_pixel_center + out_filter_radius; + + float in_pixel_influence_lowerbound = (out_pixel_influence_lowerbound + out_shift) * inv_scale; + float in_pixel_influence_upperbound = (out_pixel_influence_upperbound + out_shift) * inv_scale; + + first = (int)(STBIR_FLOORF(in_pixel_influence_lowerbound + 0.5f)); + last = (int)(STBIR_FLOORF(in_pixel_influence_upperbound - 0.5f)); + if ( last < first ) last = first; // point sample mode can span a value *right* at 0.5, and cause these to cross + + if ( edge == STBIR_EDGE_WRAP ) + { + if ( first < -input_size ) + first = -input_size; + if ( last >= (input_size*2)) + last = (input_size*2) - 1; + } + + *first_pixel = first; + *last_pixel = last; +} + +static void stbir__calculate_coefficients_for_gather_upsample( float out_filter_radius, stbir__kernel_callback * kernel, stbir__scale_info * scale_info, int num_contributors, stbir__contributors* contributors, float* coefficient_group, int coefficient_width, stbir_edge edge, void * user_data ) +{ + int n, end; + float inv_scale = scale_info->inv_scale; + float out_shift = scale_info->pixel_shift; + int input_size = scale_info->input_full_size; + int numerator = scale_info->scale_numerator; + int polyphase = ( ( scale_info->scale_is_rational ) && ( numerator < num_contributors ) ); + + // Looping through out pixels + end = num_contributors; if ( polyphase ) end = numerator; + for (n = 0; n < end; n++) + { + int i; + int last_non_zero; + float out_pixel_center = (float)n + 0.5f; + float in_center_of_out = (out_pixel_center + out_shift) * inv_scale; + + int in_first_pixel, in_last_pixel; + + stbir__calculate_in_pixel_range( &in_first_pixel, &in_last_pixel, out_pixel_center, out_filter_radius, inv_scale, out_shift, input_size, edge ); + + // make sure we never generate a range larger than our precalculated coeff width + // this only happens in point sample mode, but it's a good safe thing to do anyway + if ( ( in_last_pixel - in_first_pixel + 1 ) > coefficient_width ) + in_last_pixel = in_first_pixel + coefficient_width - 1; + + last_non_zero = -1; + for (i = 0; i <= in_last_pixel - in_first_pixel; i++) + { + float in_pixel_center = (float)(i + in_first_pixel) + 0.5f; + float coeff = kernel(in_center_of_out - in_pixel_center, inv_scale, user_data); + + // kill denormals + if ( ( ( coeff < stbir__small_float ) && ( coeff > -stbir__small_float ) ) ) + { + if ( i == 0 ) // if we're at the front, just eat zero contributors + { + STBIR_ASSERT ( ( in_last_pixel - in_first_pixel ) != 0 ); // there should be at least one contrib + ++in_first_pixel; + i--; + continue; + } + coeff = 0; // make sure is fully zero (should keep denormals away) + } + else + last_non_zero = i; + + coefficient_group[i] = coeff; + } + + in_last_pixel = last_non_zero+in_first_pixel; // kills trailing zeros + contributors->n0 = in_first_pixel; + contributors->n1 = in_last_pixel; + + STBIR_ASSERT(contributors->n1 >= contributors->n0); + + ++contributors; + coefficient_group += coefficient_width; + } +} + +static void stbir__insert_coeff( stbir__contributors * contribs, float * coeffs, int new_pixel, float new_coeff, int max_width ) +{ + if ( contribs->n1 < contribs->n0 ) // this first clause should never happen, but handle in case + { + contribs->n0 = contribs->n1 = new_pixel; + coeffs[0] = new_coeff; + } + else if ( new_pixel <= contribs->n1 ) // before the end + { + if ( new_pixel < contribs->n0 ) // before the front? + { + if ( ( contribs->n1 - new_pixel + 1 ) <= max_width ) + { + int j, o = contribs->n0 - new_pixel; + for ( j = contribs->n1 - contribs->n0 ; j >= 0 ; j-- ) + coeffs[ j + o ] = coeffs[ j ]; + for ( j = 1 ; j < o ; j++ ) + coeffs[ j ] = 0; + coeffs[ 0 ] = new_coeff; + contribs->n0 = new_pixel; + } + } + else + { + // add new weight to existing coeff if already there + coeffs[ new_pixel - contribs->n0 ] += new_coeff; + } + } + else + { + if ( ( new_pixel - contribs->n0 + 1 ) <= max_width ) + { + int j, e = new_pixel - contribs->n0; + for( j = ( contribs->n1 - contribs->n0 ) + 1 ; j < e ; j++ ) // clear in-betweens coeffs if there are any + coeffs[j] = 0; + + coeffs[ e ] = new_coeff; + contribs->n1 = new_pixel; + } + } +} + +static void stbir__calculate_out_pixel_range( int * first_pixel, int * last_pixel, float in_pixel_center, float in_pixels_radius, float scale, float out_shift, int out_size ) +{ + float in_pixel_influence_lowerbound = in_pixel_center - in_pixels_radius; + float in_pixel_influence_upperbound = in_pixel_center + in_pixels_radius; + float out_pixel_influence_lowerbound = in_pixel_influence_lowerbound * scale - out_shift; + float out_pixel_influence_upperbound = in_pixel_influence_upperbound * scale - out_shift; + int out_first_pixel = (int)(STBIR_FLOORF(out_pixel_influence_lowerbound + 0.5f)); + int out_last_pixel = (int)(STBIR_FLOORF(out_pixel_influence_upperbound - 0.5f)); + + if ( out_first_pixel < 0 ) + out_first_pixel = 0; + if ( out_last_pixel >= out_size ) + out_last_pixel = out_size - 1; + *first_pixel = out_first_pixel; + *last_pixel = out_last_pixel; +} + +static void stbir__calculate_coefficients_for_gather_downsample( int start, int end, float in_pixels_radius, stbir__kernel_callback * kernel, stbir__scale_info * scale_info, int coefficient_width, int num_contributors, stbir__contributors * contributors, float * coefficient_group, void * user_data ) +{ + int in_pixel; + int i; + int first_out_inited = -1; + float scale = scale_info->scale; + float out_shift = scale_info->pixel_shift; + int out_size = scale_info->output_sub_size; + int numerator = scale_info->scale_numerator; + int polyphase = ( ( scale_info->scale_is_rational ) && ( numerator < out_size ) ); + + STBIR__UNUSED(num_contributors); + + // Loop through the input pixels + for (in_pixel = start; in_pixel < end; in_pixel++) + { + float in_pixel_center = (float)in_pixel + 0.5f; + float out_center_of_in = in_pixel_center * scale - out_shift; + int out_first_pixel, out_last_pixel; + + stbir__calculate_out_pixel_range( &out_first_pixel, &out_last_pixel, in_pixel_center, in_pixels_radius, scale, out_shift, out_size ); + + if ( out_first_pixel > out_last_pixel ) + continue; + + // clamp or exit if we are using polyphase filtering, and the limit is up + if ( polyphase ) + { + // when polyphase, you only have to do coeffs up to the numerator count + if ( out_first_pixel == numerator ) + break; + + // don't do any extra work, clamp last pixel at numerator too + if ( out_last_pixel >= numerator ) + out_last_pixel = numerator - 1; + } + + for (i = 0; i <= out_last_pixel - out_first_pixel; i++) + { + float out_pixel_center = (float)(i + out_first_pixel) + 0.5f; + float x = out_pixel_center - out_center_of_in; + float coeff = kernel(x, scale, user_data) * scale; + + // kill the coeff if it's too small (avoid denormals) + if ( ( ( coeff < stbir__small_float ) && ( coeff > -stbir__small_float ) ) ) + coeff = 0.0f; + + { + int out = i + out_first_pixel; + float * coeffs = coefficient_group + out * coefficient_width; + stbir__contributors * contribs = contributors + out; + + // is this the first time this output pixel has been seen? Init it. + if ( out > first_out_inited ) + { + STBIR_ASSERT( out == ( first_out_inited + 1 ) ); // ensure we have only advanced one at time + first_out_inited = out; + contribs->n0 = in_pixel; + contribs->n1 = in_pixel; + coeffs[0] = coeff; + } + else + { + // insert on end (always in order) + if ( coeffs[0] == 0.0f ) // if the first coefficent is zero, then zap it for this coeffs + { + STBIR_ASSERT( ( in_pixel - contribs->n0 ) == 1 ); // ensure that when we zap, we're at the 2nd pos + contribs->n0 = in_pixel; + } + contribs->n1 = in_pixel; + STBIR_ASSERT( ( in_pixel - contribs->n0 ) < coefficient_width ); + coeffs[in_pixel - contribs->n0] = coeff; + } + } + } + } +} + +#ifdef STBIR_RENORMALIZE_IN_FLOAT +#define STBIR_RENORM_TYPE float +#else +#define STBIR_RENORM_TYPE double +#endif + +static void stbir__cleanup_gathered_coefficients( stbir_edge edge, stbir__filter_extent_info* filter_info, stbir__scale_info * scale_info, int num_contributors, stbir__contributors* contributors, float * coefficient_group, int coefficient_width ) +{ + int input_size = scale_info->input_full_size; + int input_last_n1 = input_size - 1; + int n, end; + int lowest = 0x7fffffff; + int highest = -0x7fffffff; + int widest = -1; + int numerator = scale_info->scale_numerator; + int denominator = scale_info->scale_denominator; + int polyphase = ( ( scale_info->scale_is_rational ) && ( numerator < num_contributors ) ); + float * coeffs; + stbir__contributors * contribs; + + // weight all the coeffs for each sample + coeffs = coefficient_group; + contribs = contributors; + end = num_contributors; if ( polyphase ) end = numerator; + for (n = 0; n < end; n++) + { + int i; + STBIR_RENORM_TYPE filter_scale, total_filter = 0; + int e; + + // add all contribs + e = contribs->n1 - contribs->n0; + for( i = 0 ; i <= e ; i++ ) + { + total_filter += (STBIR_RENORM_TYPE) coeffs[i]; + STBIR_ASSERT( ( coeffs[i] >= -2.0f ) && ( coeffs[i] <= 2.0f ) ); // check for wonky weights + } + + // rescale + if ( ( total_filter < stbir__small_float ) && ( total_filter > -stbir__small_float ) ) + { + // all coeffs are extremely small, just zero it + contribs->n1 = contribs->n0; + coeffs[0] = 0.0f; + } + else + { + // if the total isn't 1.0, rescale everything + if ( ( total_filter < (1.0f-stbir__small_float) ) || ( total_filter > (1.0f+stbir__small_float) ) ) + { + filter_scale = ((STBIR_RENORM_TYPE)1.0) / total_filter; + + // scale them all + for (i = 0; i <= e; i++) + coeffs[i] = (float) ( coeffs[i] * filter_scale ); + } + } + ++contribs; + coeffs += coefficient_width; + } + + // if we have a rational for the scale, we can exploit the polyphaseness to not calculate + // most of the coefficients, so we copy them here + if ( polyphase ) + { + stbir__contributors * prev_contribs = contributors; + stbir__contributors * cur_contribs = contributors + numerator; + + for( n = numerator ; n < num_contributors ; n++ ) + { + cur_contribs->n0 = prev_contribs->n0 + denominator; + cur_contribs->n1 = prev_contribs->n1 + denominator; + ++cur_contribs; + ++prev_contribs; + } + stbir_overlapping_memcpy( coefficient_group + numerator * coefficient_width, coefficient_group, ( num_contributors - numerator ) * coefficient_width * sizeof( coeffs[ 0 ] ) ); + } + + coeffs = coefficient_group; + contribs = contributors; + + for (n = 0; n < num_contributors; n++) + { + int i; + + // in zero edge mode, just remove out of bounds contribs completely (since their weights are accounted for now) + if ( edge == STBIR_EDGE_ZERO ) + { + // shrink the right side if necessary + if ( contribs->n1 > input_last_n1 ) + contribs->n1 = input_last_n1; + + // shrink the left side + if ( contribs->n0 < 0 ) + { + int j, left, skips = 0; + + skips = -contribs->n0; + contribs->n0 = 0; + + // now move down the weights + left = contribs->n1 - contribs->n0 + 1; + if ( left > 0 ) + { + for( j = 0 ; j < left ; j++ ) + coeffs[ j ] = coeffs[ j + skips ]; + } + } + } + else if ( ( edge == STBIR_EDGE_CLAMP ) || ( edge == STBIR_EDGE_REFLECT ) ) + { + // for clamp and reflect, calculate the true inbounds position (based on edge type) and just add that to the existing weight + + // right hand side first + if ( contribs->n1 > input_last_n1 ) + { + int start = contribs->n0; + int endi = contribs->n1; + contribs->n1 = input_last_n1; + for( i = input_size; i <= endi; i++ ) + stbir__insert_coeff( contribs, coeffs, stbir__edge_wrap_slow[edge]( i, input_size ), coeffs[i-start], coefficient_width ); + } + + // now check left hand edge + if ( contribs->n0 < 0 ) + { + int save_n0; + float save_n0_coeff; + float * c = coeffs - ( contribs->n0 + 1 ); + + // reinsert the coeffs with it reflected or clamped (insert accumulates, if the coeffs exist) + for( i = -1 ; i > contribs->n0 ; i-- ) + stbir__insert_coeff( contribs, coeffs, stbir__edge_wrap_slow[edge]( i, input_size ), *c--, coefficient_width ); + save_n0 = contribs->n0; + save_n0_coeff = c[0]; // save it, since we didn't do the final one (i==n0), because there might be too many coeffs to hold (before we resize)! + + // now slide all the coeffs down (since we have accumulated them in the positive contribs) and reset the first contrib + contribs->n0 = 0; + for(i = 0 ; i <= contribs->n1 ; i++ ) + coeffs[i] = coeffs[i-save_n0]; + + // now that we have shrunk down the contribs, we insert the first one safely + stbir__insert_coeff( contribs, coeffs, stbir__edge_wrap_slow[edge]( save_n0, input_size ), save_n0_coeff, coefficient_width ); + } + } + + if ( contribs->n0 <= contribs->n1 ) + { + int diff = contribs->n1 - contribs->n0 + 1; + while ( diff && ( coeffs[ diff-1 ] == 0.0f ) ) + --diff; + + contribs->n1 = contribs->n0 + diff - 1; + + if ( contribs->n0 <= contribs->n1 ) + { + if ( contribs->n0 < lowest ) + lowest = contribs->n0; + if ( contribs->n1 > highest ) + highest = contribs->n1; + if ( diff > widest ) + widest = diff; + } + + // re-zero out unused coefficients (if any) + for( i = diff ; i < coefficient_width ; i++ ) + coeffs[i] = 0.0f; + } + + ++contribs; + coeffs += coefficient_width; + } + filter_info->lowest = lowest; + filter_info->highest = highest; + filter_info->widest = widest; +} + +#undef STBIR_RENORM_TYPE + +static int stbir__pack_coefficients( int num_contributors, stbir__contributors* contributors, float * coefficents, int coefficient_width, int widest, int row0, int row1 ) +{ + #define STBIR_MOVE_1( dest, src ) { STBIR_NO_UNROLL(dest); ((stbir_uint32*)(dest))[0] = ((stbir_uint32*)(src))[0]; } + #define STBIR_MOVE_2( dest, src ) { STBIR_NO_UNROLL(dest); ((stbir_uint64*)(dest))[0] = ((stbir_uint64*)(src))[0]; } + #ifdef STBIR_SIMD + #define STBIR_MOVE_4( dest, src ) { stbir__simdf t; STBIR_NO_UNROLL(dest); stbir__simdf_load( t, src ); stbir__simdf_store( dest, t ); } + #else + #define STBIR_MOVE_4( dest, src ) { STBIR_NO_UNROLL(dest); ((stbir_uint64*)(dest))[0] = ((stbir_uint64*)(src))[0]; ((stbir_uint64*)(dest))[1] = ((stbir_uint64*)(src))[1]; } + #endif + + int row_end = row1 + 1; + STBIR__UNUSED( row0 ); // only used in an assert + + if ( coefficient_width != widest ) + { + float * pc = coefficents; + float * coeffs = coefficents; + float * pc_end = coefficents + num_contributors * widest; + switch( widest ) + { + case 1: + STBIR_NO_UNROLL_LOOP_START + do { + STBIR_MOVE_1( pc, coeffs ); + ++pc; + coeffs += coefficient_width; + } while ( pc < pc_end ); + break; + case 2: + STBIR_NO_UNROLL_LOOP_START + do { + STBIR_MOVE_2( pc, coeffs ); + pc += 2; + coeffs += coefficient_width; + } while ( pc < pc_end ); + break; + case 3: + STBIR_NO_UNROLL_LOOP_START + do { + STBIR_MOVE_2( pc, coeffs ); + STBIR_MOVE_1( pc+2, coeffs+2 ); + pc += 3; + coeffs += coefficient_width; + } while ( pc < pc_end ); + break; + case 4: + STBIR_NO_UNROLL_LOOP_START + do { + STBIR_MOVE_4( pc, coeffs ); + pc += 4; + coeffs += coefficient_width; + } while ( pc < pc_end ); + break; + case 5: + STBIR_NO_UNROLL_LOOP_START + do { + STBIR_MOVE_4( pc, coeffs ); + STBIR_MOVE_1( pc+4, coeffs+4 ); + pc += 5; + coeffs += coefficient_width; + } while ( pc < pc_end ); + break; + case 6: + STBIR_NO_UNROLL_LOOP_START + do { + STBIR_MOVE_4( pc, coeffs ); + STBIR_MOVE_2( pc+4, coeffs+4 ); + pc += 6; + coeffs += coefficient_width; + } while ( pc < pc_end ); + break; + case 7: + STBIR_NO_UNROLL_LOOP_START + do { + STBIR_MOVE_4( pc, coeffs ); + STBIR_MOVE_2( pc+4, coeffs+4 ); + STBIR_MOVE_1( pc+6, coeffs+6 ); + pc += 7; + coeffs += coefficient_width; + } while ( pc < pc_end ); + break; + case 8: + STBIR_NO_UNROLL_LOOP_START + do { + STBIR_MOVE_4( pc, coeffs ); + STBIR_MOVE_4( pc+4, coeffs+4 ); + pc += 8; + coeffs += coefficient_width; + } while ( pc < pc_end ); + break; + case 9: + STBIR_NO_UNROLL_LOOP_START + do { + STBIR_MOVE_4( pc, coeffs ); + STBIR_MOVE_4( pc+4, coeffs+4 ); + STBIR_MOVE_1( pc+8, coeffs+8 ); + pc += 9; + coeffs += coefficient_width; + } while ( pc < pc_end ); + break; + case 10: + STBIR_NO_UNROLL_LOOP_START + do { + STBIR_MOVE_4( pc, coeffs ); + STBIR_MOVE_4( pc+4, coeffs+4 ); + STBIR_MOVE_2( pc+8, coeffs+8 ); + pc += 10; + coeffs += coefficient_width; + } while ( pc < pc_end ); + break; + case 11: + STBIR_NO_UNROLL_LOOP_START + do { + STBIR_MOVE_4( pc, coeffs ); + STBIR_MOVE_4( pc+4, coeffs+4 ); + STBIR_MOVE_2( pc+8, coeffs+8 ); + STBIR_MOVE_1( pc+10, coeffs+10 ); + pc += 11; + coeffs += coefficient_width; + } while ( pc < pc_end ); + break; + case 12: + STBIR_NO_UNROLL_LOOP_START + do { + STBIR_MOVE_4( pc, coeffs ); + STBIR_MOVE_4( pc+4, coeffs+4 ); + STBIR_MOVE_4( pc+8, coeffs+8 ); + pc += 12; + coeffs += coefficient_width; + } while ( pc < pc_end ); + break; + default: + STBIR_NO_UNROLL_LOOP_START + do { + float * copy_end = pc + widest - 4; + float * c = coeffs; + do { + STBIR_NO_UNROLL( pc ); + STBIR_MOVE_4( pc, c ); + pc += 4; + c += 4; + } while ( pc <= copy_end ); + copy_end += 4; + STBIR_NO_UNROLL_LOOP_START + while ( pc < copy_end ) + { + STBIR_MOVE_1( pc, c ); + ++pc; ++c; + } + coeffs += coefficient_width; + } while ( pc < pc_end ); + break; + } + } + + // some horizontal routines read one float off the end (which is then masked off), so put in a sentinel so we don't read an snan or denormal + coefficents[ widest * num_contributors ] = 8888.0f; + + // the minimum we might read for unrolled filters widths is 12. So, we need to + // make sure we never read outside the decode buffer, by possibly moving + // the sample area back into the scanline, and putting zeros weights first. + // we start on the right edge and check until we're well past the possible + // clip area (2*widest). + { + stbir__contributors * contribs = contributors + num_contributors - 1; + float * coeffs = coefficents + widest * ( num_contributors - 1 ); + + // go until no chance of clipping (this is usually less than 8 lops) + while ( ( contribs >= contributors ) && ( ( contribs->n0 + widest*2 ) >= row_end ) ) + { + // might we clip?? + if ( ( contribs->n0 + widest ) > row_end ) + { + int stop_range = widest; + + // if range is larger than 12, it will be handled by generic loops that can terminate on the exact length + // of this contrib n1, instead of a fixed widest amount - so calculate this + if ( widest > 12 ) + { + int mod; + + // how far will be read in the n_coeff loop (which depends on the widest count mod4); + mod = widest & 3; + stop_range = ( ( ( contribs->n1 - contribs->n0 + 1 ) - mod + 3 ) & ~3 ) + mod; + + // the n_coeff loops do a minimum amount of coeffs, so factor that in! + if ( stop_range < ( 8 + mod ) ) stop_range = 8 + mod; + } + + // now see if we still clip with the refined range + if ( ( contribs->n0 + stop_range ) > row_end ) + { + int new_n0 = row_end - stop_range; + int num = contribs->n1 - contribs->n0 + 1; + int backup = contribs->n0 - new_n0; + float * from_co = coeffs + num - 1; + float * to_co = from_co + backup; + + STBIR_ASSERT( ( new_n0 >= row0 ) && ( new_n0 < contribs->n0 ) ); + + // move the coeffs over + while( num ) + { + *to_co-- = *from_co--; + --num; + } + // zero new positions + while ( to_co >= coeffs ) + *to_co-- = 0; + // set new start point + contribs->n0 = new_n0; + if ( widest > 12 ) + { + int mod; + + // how far will be read in the n_coeff loop (which depends on the widest count mod4); + mod = widest & 3; + stop_range = ( ( ( contribs->n1 - contribs->n0 + 1 ) - mod + 3 ) & ~3 ) + mod; + + // the n_coeff loops do a minimum amount of coeffs, so factor that in! + if ( stop_range < ( 8 + mod ) ) stop_range = 8 + mod; + } + } + } + --contribs; + coeffs -= widest; + } + } + + return widest; + #undef STBIR_MOVE_1 + #undef STBIR_MOVE_2 + #undef STBIR_MOVE_4 +} + +static void stbir__calculate_filters( stbir__sampler * samp, stbir__sampler * other_axis_for_pivot, void * user_data STBIR_ONLY_PROFILE_BUILD_GET_INFO ) +{ + int n; + float scale = samp->scale_info.scale; + stbir__kernel_callback * kernel = samp->filter_kernel; + stbir__support_callback * support = samp->filter_support; + float inv_scale = samp->scale_info.inv_scale; + int input_full_size = samp->scale_info.input_full_size; + int gather_num_contributors = samp->num_contributors; + stbir__contributors* gather_contributors = samp->contributors; + float * gather_coeffs = samp->coefficients; + int gather_coefficient_width = samp->coefficient_width; + + switch ( samp->is_gather ) + { + case 1: // gather upsample + { + float out_pixels_radius = support(inv_scale,user_data) * scale; + + stbir__calculate_coefficients_for_gather_upsample( out_pixels_radius, kernel, &samp->scale_info, gather_num_contributors, gather_contributors, gather_coeffs, gather_coefficient_width, samp->edge, user_data ); + + STBIR_PROFILE_BUILD_START( cleanup ); + stbir__cleanup_gathered_coefficients( samp->edge, &samp->extent_info, &samp->scale_info, gather_num_contributors, gather_contributors, gather_coeffs, gather_coefficient_width ); + STBIR_PROFILE_BUILD_END( cleanup ); + } + break; + + case 0: // scatter downsample (only on vertical) + case 2: // gather downsample + { + float in_pixels_radius = support(scale,user_data) * inv_scale; + int filter_pixel_margin = samp->filter_pixel_margin; + int input_end = input_full_size + filter_pixel_margin; + + // if this is a scatter, we do a downsample gather to get the coeffs, and then pivot after + if ( !samp->is_gather ) + { + // check if we are using the same gather downsample on the horizontal as this vertical, + // if so, then we don't have to generate them, we can just pivot from the horizontal. + if ( other_axis_for_pivot ) + { + gather_contributors = other_axis_for_pivot->contributors; + gather_coeffs = other_axis_for_pivot->coefficients; + gather_coefficient_width = other_axis_for_pivot->coefficient_width; + gather_num_contributors = other_axis_for_pivot->num_contributors; + samp->extent_info.lowest = other_axis_for_pivot->extent_info.lowest; + samp->extent_info.highest = other_axis_for_pivot->extent_info.highest; + samp->extent_info.widest = other_axis_for_pivot->extent_info.widest; + goto jump_right_to_pivot; + } + + gather_contributors = samp->gather_prescatter_contributors; + gather_coeffs = samp->gather_prescatter_coefficients; + gather_coefficient_width = samp->gather_prescatter_coefficient_width; + gather_num_contributors = samp->gather_prescatter_num_contributors; + } + + stbir__calculate_coefficients_for_gather_downsample( -filter_pixel_margin, input_end, in_pixels_radius, kernel, &samp->scale_info, gather_coefficient_width, gather_num_contributors, gather_contributors, gather_coeffs, user_data ); + + STBIR_PROFILE_BUILD_START( cleanup ); + stbir__cleanup_gathered_coefficients( samp->edge, &samp->extent_info, &samp->scale_info, gather_num_contributors, gather_contributors, gather_coeffs, gather_coefficient_width ); + STBIR_PROFILE_BUILD_END( cleanup ); + + if ( !samp->is_gather ) + { + // if this is a scatter (vertical only), then we need to pivot the coeffs + stbir__contributors * scatter_contributors; + int highest_set; + + jump_right_to_pivot: + + STBIR_PROFILE_BUILD_START( pivot ); + + highest_set = (-filter_pixel_margin) - 1; + for (n = 0; n < gather_num_contributors; n++) + { + int k; + int gn0 = gather_contributors->n0, gn1 = gather_contributors->n1; + int scatter_coefficient_width = samp->coefficient_width; + float * scatter_coeffs = samp->coefficients + ( gn0 + filter_pixel_margin ) * scatter_coefficient_width; + float * g_coeffs = gather_coeffs; + scatter_contributors = samp->contributors + ( gn0 + filter_pixel_margin ); + + for (k = gn0 ; k <= gn1 ; k++ ) + { + float gc = *g_coeffs++; + + // skip zero and denormals - must skip zeros to avoid adding coeffs beyond scatter_coefficient_width + // (which happens when pivoting from horizontal, which might have dummy zeros) + if ( ( ( gc >= stbir__small_float ) || ( gc <= -stbir__small_float ) ) ) + { + if ( ( k > highest_set ) || ( scatter_contributors->n0 > scatter_contributors->n1 ) ) + { + { + // if we are skipping over several contributors, we need to clear the skipped ones + stbir__contributors * clear_contributors = samp->contributors + ( highest_set + filter_pixel_margin + 1); + while ( clear_contributors < scatter_contributors ) + { + clear_contributors->n0 = 0; + clear_contributors->n1 = -1; + ++clear_contributors; + } + } + scatter_contributors->n0 = n; + scatter_contributors->n1 = n; + scatter_coeffs[0] = gc; + highest_set = k; + } + else + { + stbir__insert_coeff( scatter_contributors, scatter_coeffs, n, gc, scatter_coefficient_width ); + } + STBIR_ASSERT( ( scatter_contributors->n1 - scatter_contributors->n0 + 1 ) <= scatter_coefficient_width ); + } + ++scatter_contributors; + scatter_coeffs += scatter_coefficient_width; + } + + ++gather_contributors; + gather_coeffs += gather_coefficient_width; + } + + // now clear any unset contribs + { + stbir__contributors * clear_contributors = samp->contributors + ( highest_set + filter_pixel_margin + 1); + stbir__contributors * end_contributors = samp->contributors + samp->num_contributors; + while ( clear_contributors < end_contributors ) + { + clear_contributors->n0 = 0; + clear_contributors->n1 = -1; + ++clear_contributors; + } + } + + STBIR_PROFILE_BUILD_END( pivot ); + } + } + break; + } +} + + +//======================================================================================================== +// scanline decoders and encoders + +#define stbir__coder_min_num 1 +#define STB_IMAGE_RESIZE_DO_CODERS +#include STBIR__HEADER_FILENAME + +#define stbir__decode_suffix BGRA +#define stbir__decode_swizzle +#define stbir__decode_order0 2 +#define stbir__decode_order1 1 +#define stbir__decode_order2 0 +#define stbir__decode_order3 3 +#define stbir__encode_order0 2 +#define stbir__encode_order1 1 +#define stbir__encode_order2 0 +#define stbir__encode_order3 3 +#define stbir__coder_min_num 4 +#define STB_IMAGE_RESIZE_DO_CODERS +#include STBIR__HEADER_FILENAME + +#define stbir__decode_suffix ARGB +#define stbir__decode_swizzle +#define stbir__decode_order0 1 +#define stbir__decode_order1 2 +#define stbir__decode_order2 3 +#define stbir__decode_order3 0 +#define stbir__encode_order0 3 +#define stbir__encode_order1 0 +#define stbir__encode_order2 1 +#define stbir__encode_order3 2 +#define stbir__coder_min_num 4 +#define STB_IMAGE_RESIZE_DO_CODERS +#include STBIR__HEADER_FILENAME + +#define stbir__decode_suffix ABGR +#define stbir__decode_swizzle +#define stbir__decode_order0 3 +#define stbir__decode_order1 2 +#define stbir__decode_order2 1 +#define stbir__decode_order3 0 +#define stbir__encode_order0 3 +#define stbir__encode_order1 2 +#define stbir__encode_order2 1 +#define stbir__encode_order3 0 +#define stbir__coder_min_num 4 +#define STB_IMAGE_RESIZE_DO_CODERS +#include STBIR__HEADER_FILENAME + +#define stbir__decode_suffix AR +#define stbir__decode_swizzle +#define stbir__decode_order0 1 +#define stbir__decode_order1 0 +#define stbir__decode_order2 3 +#define stbir__decode_order3 2 +#define stbir__encode_order0 1 +#define stbir__encode_order1 0 +#define stbir__encode_order2 3 +#define stbir__encode_order3 2 +#define stbir__coder_min_num 2 +#define STB_IMAGE_RESIZE_DO_CODERS +#include STBIR__HEADER_FILENAME + + +// fancy alpha means we expand to keep both premultipied and non-premultiplied color channels +static void stbir__fancy_alpha_weight_4ch( float * out_buffer, int width_times_channels ) +{ + float STBIR_STREAMOUT_PTR(*) out = out_buffer; + float const * end_decode = out_buffer + ( width_times_channels / 4 ) * 7; // decode buffer aligned to end of out_buffer + float STBIR_STREAMOUT_PTR(*) decode = (float*)end_decode - width_times_channels; + + // fancy alpha is stored internally as R G B A Rpm Gpm Bpm + + #ifdef STBIR_SIMD + + #ifdef STBIR_SIMD8 + decode += 16; + STBIR_NO_UNROLL_LOOP_START + while ( decode <= end_decode ) + { + stbir__simdf8 d0,d1,a0,a1,p0,p1; + STBIR_NO_UNROLL(decode); + stbir__simdf8_load( d0, decode-16 ); + stbir__simdf8_load( d1, decode-16+8 ); + stbir__simdf8_0123to33333333( a0, d0 ); + stbir__simdf8_0123to33333333( a1, d1 ); + stbir__simdf8_mult( p0, a0, d0 ); + stbir__simdf8_mult( p1, a1, d1 ); + stbir__simdf8_bot4s( a0, d0, p0 ); + stbir__simdf8_bot4s( a1, d1, p1 ); + stbir__simdf8_top4s( d0, d0, p0 ); + stbir__simdf8_top4s( d1, d1, p1 ); + stbir__simdf8_store ( out, a0 ); + stbir__simdf8_store ( out+7, d0 ); + stbir__simdf8_store ( out+14, a1 ); + stbir__simdf8_store ( out+21, d1 ); + decode += 16; + out += 28; + } + decode -= 16; + #else + decode += 8; + STBIR_NO_UNROLL_LOOP_START + while ( decode <= end_decode ) + { + stbir__simdf d0,a0,d1,a1,p0,p1; + STBIR_NO_UNROLL(decode); + stbir__simdf_load( d0, decode-8 ); + stbir__simdf_load( d1, decode-8+4 ); + stbir__simdf_0123to3333( a0, d0 ); + stbir__simdf_0123to3333( a1, d1 ); + stbir__simdf_mult( p0, a0, d0 ); + stbir__simdf_mult( p1, a1, d1 ); + stbir__simdf_store ( out, d0 ); + stbir__simdf_store ( out+4, p0 ); + stbir__simdf_store ( out+7, d1 ); + stbir__simdf_store ( out+7+4, p1 ); + decode += 8; + out += 14; + } + decode -= 8; + #endif + + // might be one last odd pixel + #ifdef STBIR_SIMD8 + STBIR_NO_UNROLL_LOOP_START + while ( decode < end_decode ) + #else + if ( decode < end_decode ) + #endif + { + stbir__simdf d,a,p; + STBIR_NO_UNROLL(decode); + stbir__simdf_load( d, decode ); + stbir__simdf_0123to3333( a, d ); + stbir__simdf_mult( p, a, d ); + stbir__simdf_store ( out, d ); + stbir__simdf_store ( out+4, p ); + decode += 4; + out += 7; + } + + #else + + while( decode < end_decode ) + { + float r = decode[0], g = decode[1], b = decode[2], alpha = decode[3]; + out[0] = r; + out[1] = g; + out[2] = b; + out[3] = alpha; + out[4] = r * alpha; + out[5] = g * alpha; + out[6] = b * alpha; + out += 7; + decode += 4; + } + + #endif +} + +static void stbir__fancy_alpha_weight_2ch( float * out_buffer, int width_times_channels ) +{ + float STBIR_STREAMOUT_PTR(*) out = out_buffer; + float const * end_decode = out_buffer + ( width_times_channels / 2 ) * 3; + float STBIR_STREAMOUT_PTR(*) decode = (float*)end_decode - width_times_channels; + + // for fancy alpha, turns into: [X A Xpm][X A Xpm],etc + + #ifdef STBIR_SIMD + + decode += 8; + if ( decode <= end_decode ) + { + STBIR_NO_UNROLL_LOOP_START + do { + #ifdef STBIR_SIMD8 + stbir__simdf8 d0,a0,p0; + STBIR_NO_UNROLL(decode); + stbir__simdf8_load( d0, decode-8 ); + stbir__simdf8_0123to11331133( p0, d0 ); + stbir__simdf8_0123to00220022( a0, d0 ); + stbir__simdf8_mult( p0, p0, a0 ); + + stbir__simdf_store2( out, stbir__if_simdf8_cast_to_simdf4( d0 ) ); + stbir__simdf_store( out+2, stbir__if_simdf8_cast_to_simdf4( p0 ) ); + stbir__simdf_store2h( out+3, stbir__if_simdf8_cast_to_simdf4( d0 ) ); + + stbir__simdf_store2( out+6, stbir__simdf8_gettop4( d0 ) ); + stbir__simdf_store( out+8, stbir__simdf8_gettop4( p0 ) ); + stbir__simdf_store2h( out+9, stbir__simdf8_gettop4( d0 ) ); + #else + stbir__simdf d0,a0,d1,a1,p0,p1; + STBIR_NO_UNROLL(decode); + stbir__simdf_load( d0, decode-8 ); + stbir__simdf_load( d1, decode-8+4 ); + stbir__simdf_0123to1133( p0, d0 ); + stbir__simdf_0123to1133( p1, d1 ); + stbir__simdf_0123to0022( a0, d0 ); + stbir__simdf_0123to0022( a1, d1 ); + stbir__simdf_mult( p0, p0, a0 ); + stbir__simdf_mult( p1, p1, a1 ); + + stbir__simdf_store2( out, d0 ); + stbir__simdf_store( out+2, p0 ); + stbir__simdf_store2h( out+3, d0 ); + + stbir__simdf_store2( out+6, d1 ); + stbir__simdf_store( out+8, p1 ); + stbir__simdf_store2h( out+9, d1 ); + #endif + decode += 8; + out += 12; + } while ( decode <= end_decode ); + } + decode -= 8; + #endif + + STBIR_SIMD_NO_UNROLL_LOOP_START + while( decode < end_decode ) + { + float x = decode[0], y = decode[1]; + STBIR_SIMD_NO_UNROLL(decode); + out[0] = x; + out[1] = y; + out[2] = x * y; + out += 3; + decode += 2; + } +} + +static void stbir__fancy_alpha_unweight_4ch( float * encode_buffer, int width_times_channels ) +{ + float STBIR_SIMD_STREAMOUT_PTR(*) encode = encode_buffer; + float STBIR_SIMD_STREAMOUT_PTR(*) input = encode_buffer; + float const * end_output = encode_buffer + width_times_channels; + + // fancy RGBA is stored internally as R G B A Rpm Gpm Bpm + + STBIR_SIMD_NO_UNROLL_LOOP_START + do { + float alpha = input[3]; +#ifdef STBIR_SIMD + stbir__simdf i,ia; + STBIR_SIMD_NO_UNROLL(encode); + if ( alpha < stbir__small_float ) + { + stbir__simdf_load( i, input ); + stbir__simdf_store( encode, i ); + } + else + { + stbir__simdf_load1frep4( ia, 1.0f / alpha ); + stbir__simdf_load( i, input+4 ); + stbir__simdf_mult( i, i, ia ); + stbir__simdf_store( encode, i ); + encode[3] = alpha; + } +#else + if ( alpha < stbir__small_float ) + { + encode[0] = input[0]; + encode[1] = input[1]; + encode[2] = input[2]; + } + else + { + float ialpha = 1.0f / alpha; + encode[0] = input[4] * ialpha; + encode[1] = input[5] * ialpha; + encode[2] = input[6] * ialpha; + } + encode[3] = alpha; +#endif + + input += 7; + encode += 4; + } while ( encode < end_output ); +} + +// format: [X A Xpm][X A Xpm] etc +static void stbir__fancy_alpha_unweight_2ch( float * encode_buffer, int width_times_channels ) +{ + float STBIR_SIMD_STREAMOUT_PTR(*) encode = encode_buffer; + float STBIR_SIMD_STREAMOUT_PTR(*) input = encode_buffer; + float const * end_output = encode_buffer + width_times_channels; + + do { + float alpha = input[1]; + encode[0] = input[0]; + if ( alpha >= stbir__small_float ) + encode[0] = input[2] / alpha; + encode[1] = alpha; + + input += 3; + encode += 2; + } while ( encode < end_output ); +} + +static void stbir__simple_alpha_weight_4ch( float * decode_buffer, int width_times_channels ) +{ + float STBIR_STREAMOUT_PTR(*) decode = decode_buffer; + float const * end_decode = decode_buffer + width_times_channels; + + #ifdef STBIR_SIMD + { + decode += 2 * stbir__simdfX_float_count; + STBIR_NO_UNROLL_LOOP_START + while ( decode <= end_decode ) + { + stbir__simdfX d0,a0,d1,a1; + STBIR_NO_UNROLL(decode); + stbir__simdfX_load( d0, decode-2*stbir__simdfX_float_count ); + stbir__simdfX_load( d1, decode-2*stbir__simdfX_float_count+stbir__simdfX_float_count ); + stbir__simdfX_aaa1( a0, d0, STBIR_onesX ); + stbir__simdfX_aaa1( a1, d1, STBIR_onesX ); + stbir__simdfX_mult( d0, d0, a0 ); + stbir__simdfX_mult( d1, d1, a1 ); + stbir__simdfX_store ( decode-2*stbir__simdfX_float_count, d0 ); + stbir__simdfX_store ( decode-2*stbir__simdfX_float_count+stbir__simdfX_float_count, d1 ); + decode += 2 * stbir__simdfX_float_count; + } + decode -= 2 * stbir__simdfX_float_count; + + // few last pixels remnants + #ifdef STBIR_SIMD8 + STBIR_NO_UNROLL_LOOP_START + while ( decode < end_decode ) + #else + if ( decode < end_decode ) + #endif + { + stbir__simdf d,a; + stbir__simdf_load( d, decode ); + stbir__simdf_aaa1( a, d, STBIR__CONSTF(STBIR_ones) ); + stbir__simdf_mult( d, d, a ); + stbir__simdf_store ( decode, d ); + decode += 4; + } + } + + #else + + while( decode < end_decode ) + { + float alpha = decode[3]; + decode[0] *= alpha; + decode[1] *= alpha; + decode[2] *= alpha; + decode += 4; + } + + #endif +} + +static void stbir__simple_alpha_weight_2ch( float * decode_buffer, int width_times_channels ) +{ + float STBIR_STREAMOUT_PTR(*) decode = decode_buffer; + float const * end_decode = decode_buffer + width_times_channels; + + #ifdef STBIR_SIMD + decode += 2 * stbir__simdfX_float_count; + STBIR_NO_UNROLL_LOOP_START + while ( decode <= end_decode ) + { + stbir__simdfX d0,a0,d1,a1; + STBIR_NO_UNROLL(decode); + stbir__simdfX_load( d0, decode-2*stbir__simdfX_float_count ); + stbir__simdfX_load( d1, decode-2*stbir__simdfX_float_count+stbir__simdfX_float_count ); + stbir__simdfX_a1a1( a0, d0, STBIR_onesX ); + stbir__simdfX_a1a1( a1, d1, STBIR_onesX ); + stbir__simdfX_mult( d0, d0, a0 ); + stbir__simdfX_mult( d1, d1, a1 ); + stbir__simdfX_store ( decode-2*stbir__simdfX_float_count, d0 ); + stbir__simdfX_store ( decode-2*stbir__simdfX_float_count+stbir__simdfX_float_count, d1 ); + decode += 2 * stbir__simdfX_float_count; + } + decode -= 2 * stbir__simdfX_float_count; + #endif + + STBIR_SIMD_NO_UNROLL_LOOP_START + while( decode < end_decode ) + { + float alpha = decode[1]; + STBIR_SIMD_NO_UNROLL(decode); + decode[0] *= alpha; + decode += 2; + } +} + +static void stbir__simple_alpha_unweight_4ch( float * encode_buffer, int width_times_channels ) +{ + float STBIR_SIMD_STREAMOUT_PTR(*) encode = encode_buffer; + float const * end_output = encode_buffer + width_times_channels; + + STBIR_SIMD_NO_UNROLL_LOOP_START + do { + float alpha = encode[3]; + +#ifdef STBIR_SIMD + stbir__simdf i,ia; + STBIR_SIMD_NO_UNROLL(encode); + if ( alpha >= stbir__small_float ) + { + stbir__simdf_load1frep4( ia, 1.0f / alpha ); + stbir__simdf_load( i, encode ); + stbir__simdf_mult( i, i, ia ); + stbir__simdf_store( encode, i ); + encode[3] = alpha; + } +#else + if ( alpha >= stbir__small_float ) + { + float ialpha = 1.0f / alpha; + encode[0] *= ialpha; + encode[1] *= ialpha; + encode[2] *= ialpha; + } +#endif + encode += 4; + } while ( encode < end_output ); +} + +static void stbir__simple_alpha_unweight_2ch( float * encode_buffer, int width_times_channels ) +{ + float STBIR_SIMD_STREAMOUT_PTR(*) encode = encode_buffer; + float const * end_output = encode_buffer + width_times_channels; + + do { + float alpha = encode[1]; + if ( alpha >= stbir__small_float ) + encode[0] /= alpha; + encode += 2; + } while ( encode < end_output ); +} + + +// only used in RGB->BGR or BGR->RGB +static void stbir__simple_flip_3ch( float * decode_buffer, int width_times_channels ) +{ + float STBIR_STREAMOUT_PTR(*) decode = decode_buffer; + float const * end_decode = decode_buffer + width_times_channels; + +#ifdef STBIR_SIMD + #ifdef stbir__simdf_swiz2 // do we have two argument swizzles? + end_decode -= 12; + STBIR_NO_UNROLL_LOOP_START + while( decode <= end_decode ) + { + // on arm64 8 instructions, no overlapping stores + stbir__simdf a,b,c,na,nb; + STBIR_SIMD_NO_UNROLL(decode); + stbir__simdf_load( a, decode ); + stbir__simdf_load( b, decode+4 ); + stbir__simdf_load( c, decode+8 ); + + na = stbir__simdf_swiz2( a, b, 2, 1, 0, 5 ); + b = stbir__simdf_swiz2( a, b, 4, 3, 6, 7 ); + nb = stbir__simdf_swiz2( b, c, 0, 1, 4, 3 ); + c = stbir__simdf_swiz2( b, c, 2, 7, 6, 5 ); + + stbir__simdf_store( decode, na ); + stbir__simdf_store( decode+4, nb ); + stbir__simdf_store( decode+8, c ); + decode += 12; + } + end_decode += 12; + #else + end_decode -= 24; + STBIR_NO_UNROLL_LOOP_START + while( decode <= end_decode ) + { + // 26 instructions on x64 + stbir__simdf a,b,c,d,e,f,g; + float i21, i23; + STBIR_SIMD_NO_UNROLL(decode); + stbir__simdf_load( a, decode ); + stbir__simdf_load( b, decode+3 ); + stbir__simdf_load( c, decode+6 ); + stbir__simdf_load( d, decode+9 ); + stbir__simdf_load( e, decode+12 ); + stbir__simdf_load( f, decode+15 ); + stbir__simdf_load( g, decode+18 ); + + a = stbir__simdf_swiz( a, 2, 1, 0, 3 ); + b = stbir__simdf_swiz( b, 2, 1, 0, 3 ); + c = stbir__simdf_swiz( c, 2, 1, 0, 3 ); + d = stbir__simdf_swiz( d, 2, 1, 0, 3 ); + e = stbir__simdf_swiz( e, 2, 1, 0, 3 ); + f = stbir__simdf_swiz( f, 2, 1, 0, 3 ); + g = stbir__simdf_swiz( g, 2, 1, 0, 3 ); + + // stores overlap, need to be in order, + stbir__simdf_store( decode, a ); + i21 = decode[21]; + stbir__simdf_store( decode+3, b ); + i23 = decode[23]; + stbir__simdf_store( decode+6, c ); + stbir__simdf_store( decode+9, d ); + stbir__simdf_store( decode+12, e ); + stbir__simdf_store( decode+15, f ); + stbir__simdf_store( decode+18, g ); + decode[21] = i23; + decode[23] = i21; + decode += 24; + } + end_decode += 24; + #endif +#else + end_decode -= 12; + STBIR_NO_UNROLL_LOOP_START + while( decode <= end_decode ) + { + // 16 instructions + float t0,t1,t2,t3; + STBIR_NO_UNROLL(decode); + t0 = decode[0]; t1 = decode[3]; t2 = decode[6]; t3 = decode[9]; + decode[0] = decode[2]; decode[3] = decode[5]; decode[6] = decode[8]; decode[9] = decode[11]; + decode[2] = t0; decode[5] = t1; decode[8] = t2; decode[11] = t3; + decode += 12; + } + end_decode += 12; +#endif + + STBIR_NO_UNROLL_LOOP_START + while( decode < end_decode ) + { + float t = decode[0]; + STBIR_NO_UNROLL(decode); + decode[0] = decode[2]; + decode[2] = t; + decode += 3; + } +} + + + +static void stbir__decode_scanline(stbir__info const * stbir_info, int n, float * output_buffer STBIR_ONLY_PROFILE_GET_SPLIT_INFO ) +{ + int channels = stbir_info->channels; + int effective_channels = stbir_info->effective_channels; + int input_sample_in_bytes = stbir__type_size[stbir_info->input_type] * channels; + stbir_edge edge_horizontal = stbir_info->horizontal.edge; + stbir_edge edge_vertical = stbir_info->vertical.edge; + int row = stbir__edge_wrap(edge_vertical, n, stbir_info->vertical.scale_info.input_full_size); + const void* input_plane_data = ( (char *) stbir_info->input_data ) + (size_t)row * (size_t) stbir_info->input_stride_bytes; + stbir__span const * spans = stbir_info->scanline_extents.spans; + float * full_decode_buffer = output_buffer - stbir_info->scanline_extents.conservative.n0 * effective_channels; + float * last_decoded = 0; + + // if we are on edge_zero, and we get in here with an out of bounds n, then the calculate filters has failed + STBIR_ASSERT( !(edge_vertical == STBIR_EDGE_ZERO && (n < 0 || n >= stbir_info->vertical.scale_info.input_full_size)) ); + + do + { + float * decode_buffer; + void const * input_data; + float * end_decode; + int width_times_channels; + int width; + + if ( spans->n1 < spans->n0 ) + break; + + width = spans->n1 + 1 - spans->n0; + decode_buffer = full_decode_buffer + spans->n0 * effective_channels; + end_decode = full_decode_buffer + ( spans->n1 + 1 ) * effective_channels; + width_times_channels = width * channels; + + // read directly out of input plane by default + input_data = ( (char*)input_plane_data ) + spans->pixel_offset_for_input * input_sample_in_bytes; + + // if we have an input callback, call it to get the input data + if ( stbir_info->in_pixels_cb ) + { + // call the callback with a temp buffer (that they can choose to use or not). the temp is just right aligned memory in the decode_buffer itself + input_data = stbir_info->in_pixels_cb( ( (char*) end_decode ) - ( width * input_sample_in_bytes ) + ( ( stbir_info->input_type != STBIR_TYPE_FLOAT ) ? ( sizeof(float)*STBIR_INPUT_CALLBACK_PADDING ) : 0 ), input_plane_data, width, spans->pixel_offset_for_input, row, stbir_info->user_data ); + } + + STBIR_PROFILE_START( decode ); + // convert the pixels info the float decode_buffer, (we index from end_decode, so that when channelsdecode_pixels( (float*)end_decode - width_times_channels, width_times_channels, input_data ); + STBIR_PROFILE_END( decode ); + + if (stbir_info->alpha_weight) + { + STBIR_PROFILE_START( alpha ); + stbir_info->alpha_weight( decode_buffer, width_times_channels ); + STBIR_PROFILE_END( alpha ); + } + + ++spans; + } while ( spans <= ( &stbir_info->scanline_extents.spans[1] ) ); + + // handle the edge_wrap filter (all other types are handled back out at the calculate_filter stage) + // basically the idea here is that if we have the whole scanline in memory, we don't redecode the + // wrapped edge pixels, and instead just memcpy them from the scanline into the edge positions + if ( ( edge_horizontal == STBIR_EDGE_WRAP ) && ( stbir_info->scanline_extents.edge_sizes[0] | stbir_info->scanline_extents.edge_sizes[1] ) ) + { + // this code only runs if we're in edge_wrap, and we're doing the entire scanline + int e, start_x[2]; + int input_full_size = stbir_info->horizontal.scale_info.input_full_size; + + start_x[0] = -stbir_info->scanline_extents.edge_sizes[0]; // left edge start x + start_x[1] = input_full_size; // right edge + + for( e = 0; e < 2 ; e++ ) + { + // do each margin + int margin = stbir_info->scanline_extents.edge_sizes[e]; + if ( margin ) + { + int x = start_x[e]; + float * marg = full_decode_buffer + x * effective_channels; + float const * src = full_decode_buffer + stbir__edge_wrap(edge_horizontal, x, input_full_size) * effective_channels; + STBIR_MEMCPY( marg, src, margin * effective_channels * sizeof(float) ); + if ( e == 1 ) last_decoded = marg + margin * effective_channels; + } + } + } + + // some of the horizontal gathers read one float off the edge (which is masked out), but we force a zero here to make sure no NaNs leak in + // (we can't pre-zero it, because the input callback can use that area as padding) + last_decoded[0] = 0.0f; + + // we clear this extra float, because the final output pixel filter kernel might have used one less coeff than the max filter width + // when this happens, we do read that pixel from the input, so it too could be Nan, so just zero an extra one. + // this fits because each scanline is padded by three floats (STBIR_INPUT_CALLBACK_PADDING) + last_decoded[1] = 0.0f; +} + + +//================= +// Do 1 channel horizontal routines + +#ifdef STBIR_SIMD + +#define stbir__1_coeff_only() \ + stbir__simdf tot,c; \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf_load1( c, hc ); \ + stbir__simdf_mult1_mem( tot, c, decode ); + +#define stbir__2_coeff_only() \ + stbir__simdf tot,c,d; \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf_load2z( c, hc ); \ + stbir__simdf_load2( d, decode ); \ + stbir__simdf_mult( tot, c, d ); \ + stbir__simdf_0123to1230( c, tot ); \ + stbir__simdf_add1( tot, tot, c ); + +#define stbir__3_coeff_only() \ + stbir__simdf tot,c,t; \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf_load( c, hc ); \ + stbir__simdf_mult_mem( tot, c, decode ); \ + stbir__simdf_0123to1230( c, tot ); \ + stbir__simdf_0123to2301( t, tot ); \ + stbir__simdf_add1( tot, tot, c ); \ + stbir__simdf_add1( tot, tot, t ); + +#define stbir__store_output_tiny() \ + stbir__simdf_store1( output, tot ); \ + horizontal_coefficients += coefficient_width; \ + ++horizontal_contributors; \ + output += 1; + +#define stbir__4_coeff_start() \ + stbir__simdf tot,c; \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf_load( c, hc ); \ + stbir__simdf_mult_mem( tot, c, decode ); \ + +#define stbir__4_coeff_continue_from_4( ofs ) \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf_load( c, hc + (ofs) ); \ + stbir__simdf_madd_mem( tot, tot, c, decode+(ofs) ); + +#define stbir__1_coeff_remnant( ofs ) \ + { stbir__simdf d; \ + stbir__simdf_load1z( c, hc + (ofs) ); \ + stbir__simdf_load1( d, decode + (ofs) ); \ + stbir__simdf_madd( tot, tot, d, c ); } + +#define stbir__2_coeff_remnant( ofs ) \ + { stbir__simdf d; \ + stbir__simdf_load2z( c, hc+(ofs) ); \ + stbir__simdf_load2( d, decode+(ofs) ); \ + stbir__simdf_madd( tot, tot, d, c ); } + +#define stbir__3_coeff_setup() \ + stbir__simdf mask; \ + stbir__simdf_load( mask, STBIR_mask + 3 ); + +#define stbir__3_coeff_remnant( ofs ) \ + stbir__simdf_load( c, hc+(ofs) ); \ + stbir__simdf_and( c, c, mask ); \ + stbir__simdf_madd_mem( tot, tot, c, decode+(ofs) ); + +#define stbir__store_output() \ + stbir__simdf_0123to2301( c, tot ); \ + stbir__simdf_add( tot, tot, c ); \ + stbir__simdf_0123to1230( c, tot ); \ + stbir__simdf_add1( tot, tot, c ); \ + stbir__simdf_store1( output, tot ); \ + horizontal_coefficients += coefficient_width; \ + ++horizontal_contributors; \ + output += 1; + +#else + +#define stbir__1_coeff_only() \ + float tot; \ + tot = decode[0]*hc[0]; + +#define stbir__2_coeff_only() \ + float tot; \ + tot = decode[0] * hc[0]; \ + tot += decode[1] * hc[1]; + +#define stbir__3_coeff_only() \ + float tot; \ + tot = decode[0] * hc[0]; \ + tot += decode[1] * hc[1]; \ + tot += decode[2] * hc[2]; + +#define stbir__store_output_tiny() \ + output[0] = tot; \ + horizontal_coefficients += coefficient_width; \ + ++horizontal_contributors; \ + output += 1; + +#define stbir__4_coeff_start() \ + float tot0,tot1,tot2,tot3; \ + tot0 = decode[0] * hc[0]; \ + tot1 = decode[1] * hc[1]; \ + tot2 = decode[2] * hc[2]; \ + tot3 = decode[3] * hc[3]; + +#define stbir__4_coeff_continue_from_4( ofs ) \ + tot0 += decode[0+(ofs)] * hc[0+(ofs)]; \ + tot1 += decode[1+(ofs)] * hc[1+(ofs)]; \ + tot2 += decode[2+(ofs)] * hc[2+(ofs)]; \ + tot3 += decode[3+(ofs)] * hc[3+(ofs)]; + +#define stbir__1_coeff_remnant( ofs ) \ + tot0 += decode[0+(ofs)] * hc[0+(ofs)]; + +#define stbir__2_coeff_remnant( ofs ) \ + tot0 += decode[0+(ofs)] * hc[0+(ofs)]; \ + tot1 += decode[1+(ofs)] * hc[1+(ofs)]; \ + +#define stbir__3_coeff_remnant( ofs ) \ + tot0 += decode[0+(ofs)] * hc[0+(ofs)]; \ + tot1 += decode[1+(ofs)] * hc[1+(ofs)]; \ + tot2 += decode[2+(ofs)] * hc[2+(ofs)]; + +#define stbir__store_output() \ + output[0] = (tot0+tot2)+(tot1+tot3); \ + horizontal_coefficients += coefficient_width; \ + ++horizontal_contributors; \ + output += 1; + +#endif + +#define STBIR__horizontal_channels 1 +#define STB_IMAGE_RESIZE_DO_HORIZONTALS +#include STBIR__HEADER_FILENAME + + +//================= +// Do 2 channel horizontal routines + +#ifdef STBIR_SIMD + +#define stbir__1_coeff_only() \ + stbir__simdf tot,c,d; \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf_load1z( c, hc ); \ + stbir__simdf_0123to0011( c, c ); \ + stbir__simdf_load2( d, decode ); \ + stbir__simdf_mult( tot, d, c ); + +#define stbir__2_coeff_only() \ + stbir__simdf tot,c; \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf_load2( c, hc ); \ + stbir__simdf_0123to0011( c, c ); \ + stbir__simdf_mult_mem( tot, c, decode ); + +#define stbir__3_coeff_only() \ + stbir__simdf tot,c,cs,d; \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf_load( cs, hc ); \ + stbir__simdf_0123to0011( c, cs ); \ + stbir__simdf_mult_mem( tot, c, decode ); \ + stbir__simdf_0123to2222( c, cs ); \ + stbir__simdf_load2z( d, decode+4 ); \ + stbir__simdf_madd( tot, tot, d, c ); + +#define stbir__store_output_tiny() \ + stbir__simdf_0123to2301( c, tot ); \ + stbir__simdf_add( tot, tot, c ); \ + stbir__simdf_store2( output, tot ); \ + horizontal_coefficients += coefficient_width; \ + ++horizontal_contributors; \ + output += 2; + +#ifdef STBIR_SIMD8 + +#define stbir__4_coeff_start() \ + stbir__simdf8 tot0,c,cs; \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf8_load4b( cs, hc ); \ + stbir__simdf8_0123to00112233( c, cs ); \ + stbir__simdf8_mult_mem( tot0, c, decode ); + +#define stbir__4_coeff_continue_from_4( ofs ) \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf8_load4b( cs, hc + (ofs) ); \ + stbir__simdf8_0123to00112233( c, cs ); \ + stbir__simdf8_madd_mem( tot0, tot0, c, decode+(ofs)*2 ); + +#define stbir__1_coeff_remnant( ofs ) \ + { stbir__simdf t,d; \ + stbir__simdf_load1z( t, hc + (ofs) ); \ + stbir__simdf_load2( d, decode + (ofs) * 2 ); \ + stbir__simdf_0123to0011( t, t ); \ + stbir__simdf_mult( t, t, d ); \ + stbir__simdf8_add4( tot0, tot0, t ); } + +#define stbir__2_coeff_remnant( ofs ) \ + { stbir__simdf t; \ + stbir__simdf_load2( t, hc + (ofs) ); \ + stbir__simdf_0123to0011( t, t ); \ + stbir__simdf_mult_mem( t, t, decode+(ofs)*2 ); \ + stbir__simdf8_add4( tot0, tot0, t ); } + +#define stbir__3_coeff_remnant( ofs ) \ + { stbir__simdf8 d; \ + stbir__simdf8_load4b( cs, hc + (ofs) ); \ + stbir__simdf8_0123to00112233( c, cs ); \ + stbir__simdf8_load6z( d, decode+(ofs)*2 ); \ + stbir__simdf8_madd( tot0, tot0, c, d ); } + +#define stbir__store_output() \ + { stbir__simdf t,d; \ + stbir__simdf8_add4halves( t, stbir__if_simdf8_cast_to_simdf4(tot0), tot0 ); \ + stbir__simdf_0123to2301( d, t ); \ + stbir__simdf_add( t, t, d ); \ + stbir__simdf_store2( output, t ); \ + horizontal_coefficients += coefficient_width; \ + ++horizontal_contributors; \ + output += 2; } + +#else + +#define stbir__4_coeff_start() \ + stbir__simdf tot0,tot1,c,cs; \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf_load( cs, hc ); \ + stbir__simdf_0123to0011( c, cs ); \ + stbir__simdf_mult_mem( tot0, c, decode ); \ + stbir__simdf_0123to2233( c, cs ); \ + stbir__simdf_mult_mem( tot1, c, decode+4 ); + +#define stbir__4_coeff_continue_from_4( ofs ) \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf_load( cs, hc + (ofs) ); \ + stbir__simdf_0123to0011( c, cs ); \ + stbir__simdf_madd_mem( tot0, tot0, c, decode+(ofs)*2 ); \ + stbir__simdf_0123to2233( c, cs ); \ + stbir__simdf_madd_mem( tot1, tot1, c, decode+(ofs)*2+4 ); + +#define stbir__1_coeff_remnant( ofs ) \ + { stbir__simdf d; \ + stbir__simdf_load1z( cs, hc + (ofs) ); \ + stbir__simdf_0123to0011( c, cs ); \ + stbir__simdf_load2( d, decode + (ofs) * 2 ); \ + stbir__simdf_madd( tot0, tot0, d, c ); } + +#define stbir__2_coeff_remnant( ofs ) \ + stbir__simdf_load2( cs, hc + (ofs) ); \ + stbir__simdf_0123to0011( c, cs ); \ + stbir__simdf_madd_mem( tot0, tot0, c, decode+(ofs)*2 ); + +#define stbir__3_coeff_remnant( ofs ) \ + { stbir__simdf d; \ + stbir__simdf_load( cs, hc + (ofs) ); \ + stbir__simdf_0123to0011( c, cs ); \ + stbir__simdf_madd_mem( tot0, tot0, c, decode+(ofs)*2 ); \ + stbir__simdf_0123to2222( c, cs ); \ + stbir__simdf_load2z( d, decode + (ofs) * 2 + 4 ); \ + stbir__simdf_madd( tot1, tot1, d, c ); } + +#define stbir__store_output() \ + stbir__simdf_add( tot0, tot0, tot1 ); \ + stbir__simdf_0123to2301( c, tot0 ); \ + stbir__simdf_add( tot0, tot0, c ); \ + stbir__simdf_store2( output, tot0 ); \ + horizontal_coefficients += coefficient_width; \ + ++horizontal_contributors; \ + output += 2; + +#endif + +#else + +#define stbir__1_coeff_only() \ + float tota,totb,c; \ + c = hc[0]; \ + tota = decode[0]*c; \ + totb = decode[1]*c; + +#define stbir__2_coeff_only() \ + float tota,totb,c; \ + c = hc[0]; \ + tota = decode[0]*c; \ + totb = decode[1]*c; \ + c = hc[1]; \ + tota += decode[2]*c; \ + totb += decode[3]*c; + +// this weird order of add matches the simd +#define stbir__3_coeff_only() \ + float tota,totb,c; \ + c = hc[0]; \ + tota = decode[0]*c; \ + totb = decode[1]*c; \ + c = hc[2]; \ + tota += decode[4]*c; \ + totb += decode[5]*c; \ + c = hc[1]; \ + tota += decode[2]*c; \ + totb += decode[3]*c; + +#define stbir__store_output_tiny() \ + output[0] = tota; \ + output[1] = totb; \ + horizontal_coefficients += coefficient_width; \ + ++horizontal_contributors; \ + output += 2; + +#define stbir__4_coeff_start() \ + float tota0,tota1,tota2,tota3,totb0,totb1,totb2,totb3,c; \ + c = hc[0]; \ + tota0 = decode[0]*c; \ + totb0 = decode[1]*c; \ + c = hc[1]; \ + tota1 = decode[2]*c; \ + totb1 = decode[3]*c; \ + c = hc[2]; \ + tota2 = decode[4]*c; \ + totb2 = decode[5]*c; \ + c = hc[3]; \ + tota3 = decode[6]*c; \ + totb3 = decode[7]*c; + +#define stbir__4_coeff_continue_from_4( ofs ) \ + c = hc[0+(ofs)]; \ + tota0 += decode[0+(ofs)*2]*c; \ + totb0 += decode[1+(ofs)*2]*c; \ + c = hc[1+(ofs)]; \ + tota1 += decode[2+(ofs)*2]*c; \ + totb1 += decode[3+(ofs)*2]*c; \ + c = hc[2+(ofs)]; \ + tota2 += decode[4+(ofs)*2]*c; \ + totb2 += decode[5+(ofs)*2]*c; \ + c = hc[3+(ofs)]; \ + tota3 += decode[6+(ofs)*2]*c; \ + totb3 += decode[7+(ofs)*2]*c; + +#define stbir__1_coeff_remnant( ofs ) \ + c = hc[0+(ofs)]; \ + tota0 += decode[0+(ofs)*2] * c; \ + totb0 += decode[1+(ofs)*2] * c; + +#define stbir__2_coeff_remnant( ofs ) \ + c = hc[0+(ofs)]; \ + tota0 += decode[0+(ofs)*2] * c; \ + totb0 += decode[1+(ofs)*2] * c; \ + c = hc[1+(ofs)]; \ + tota1 += decode[2+(ofs)*2] * c; \ + totb1 += decode[3+(ofs)*2] * c; + +#define stbir__3_coeff_remnant( ofs ) \ + c = hc[0+(ofs)]; \ + tota0 += decode[0+(ofs)*2] * c; \ + totb0 += decode[1+(ofs)*2] * c; \ + c = hc[1+(ofs)]; \ + tota1 += decode[2+(ofs)*2] * c; \ + totb1 += decode[3+(ofs)*2] * c; \ + c = hc[2+(ofs)]; \ + tota2 += decode[4+(ofs)*2] * c; \ + totb2 += decode[5+(ofs)*2] * c; + +#define stbir__store_output() \ + output[0] = (tota0+tota2)+(tota1+tota3); \ + output[1] = (totb0+totb2)+(totb1+totb3); \ + horizontal_coefficients += coefficient_width; \ + ++horizontal_contributors; \ + output += 2; + +#endif + +#define STBIR__horizontal_channels 2 +#define STB_IMAGE_RESIZE_DO_HORIZONTALS +#include STBIR__HEADER_FILENAME + + +//================= +// Do 3 channel horizontal routines + +#ifdef STBIR_SIMD + +#define stbir__1_coeff_only() \ + stbir__simdf tot,c,d; \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf_load1z( c, hc ); \ + stbir__simdf_0123to0001( c, c ); \ + stbir__simdf_load( d, decode ); \ + stbir__simdf_mult( tot, d, c ); + +#define stbir__2_coeff_only() \ + stbir__simdf tot,c,cs,d; \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf_load2( cs, hc ); \ + stbir__simdf_0123to0000( c, cs ); \ + stbir__simdf_load( d, decode ); \ + stbir__simdf_mult( tot, d, c ); \ + stbir__simdf_0123to1111( c, cs ); \ + stbir__simdf_load( d, decode+3 ); \ + stbir__simdf_madd( tot, tot, d, c ); + +#define stbir__3_coeff_only() \ + stbir__simdf tot,c,d,cs; \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf_load( cs, hc ); \ + stbir__simdf_0123to0000( c, cs ); \ + stbir__simdf_load( d, decode ); \ + stbir__simdf_mult( tot, d, c ); \ + stbir__simdf_0123to1111( c, cs ); \ + stbir__simdf_load( d, decode+3 ); \ + stbir__simdf_madd( tot, tot, d, c ); \ + stbir__simdf_0123to2222( c, cs ); \ + stbir__simdf_load( d, decode+6 ); \ + stbir__simdf_madd( tot, tot, d, c ); + +#define stbir__store_output_tiny() \ + stbir__simdf_store2( output, tot ); \ + stbir__simdf_0123to2301( tot, tot ); \ + stbir__simdf_store1( output+2, tot ); \ + horizontal_coefficients += coefficient_width; \ + ++horizontal_contributors; \ + output += 3; + +#ifdef STBIR_SIMD8 + +// we're loading from the XXXYYY decode by -1 to get the XXXYYY into different halves of the AVX reg fyi +#define stbir__4_coeff_start() \ + stbir__simdf8 tot0,tot1,c,cs; stbir__simdf t; \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf8_load4b( cs, hc ); \ + stbir__simdf8_0123to00001111( c, cs ); \ + stbir__simdf8_mult_mem( tot0, c, decode - 1 ); \ + stbir__simdf8_0123to22223333( c, cs ); \ + stbir__simdf8_mult_mem( tot1, c, decode+6 - 1 ); + +#define stbir__4_coeff_continue_from_4( ofs ) \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf8_load4b( cs, hc + (ofs) ); \ + stbir__simdf8_0123to00001111( c, cs ); \ + stbir__simdf8_madd_mem( tot0, tot0, c, decode+(ofs)*3 - 1 ); \ + stbir__simdf8_0123to22223333( c, cs ); \ + stbir__simdf8_madd_mem( tot1, tot1, c, decode+(ofs)*3 + 6 - 1 ); + +#define stbir__1_coeff_remnant( ofs ) \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf_load1rep4( t, hc + (ofs) ); \ + stbir__simdf8_madd_mem4( tot0, tot0, t, decode+(ofs)*3 - 1 ); + +#define stbir__2_coeff_remnant( ofs ) \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf8_load4b( cs, hc + (ofs) - 2 ); \ + stbir__simdf8_0123to22223333( c, cs ); \ + stbir__simdf8_madd_mem( tot0, tot0, c, decode+(ofs)*3 - 1 ); + + #define stbir__3_coeff_remnant( ofs ) \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf8_load4b( cs, hc + (ofs) ); \ + stbir__simdf8_0123to00001111( c, cs ); \ + stbir__simdf8_madd_mem( tot0, tot0, c, decode+(ofs)*3 - 1 ); \ + stbir__simdf8_0123to2222( t, cs ); \ + stbir__simdf8_madd_mem4( tot1, tot1, t, decode+(ofs)*3 + 6 - 1 ); + +#define stbir__store_output() \ + stbir__simdf8_add( tot0, tot0, tot1 ); \ + stbir__simdf_0123to1230( t, stbir__if_simdf8_cast_to_simdf4( tot0 ) ); \ + stbir__simdf8_add4halves( t, t, tot0 ); \ + horizontal_coefficients += coefficient_width; \ + ++horizontal_contributors; \ + output += 3; \ + if ( output < output_end ) \ + { \ + stbir__simdf_store( output-3, t ); \ + continue; \ + } \ + { stbir__simdf tt; stbir__simdf_0123to2301( tt, t ); \ + stbir__simdf_store2( output-3, t ); \ + stbir__simdf_store1( output+2-3, tt ); } \ + break; + + +#else + +#define stbir__4_coeff_start() \ + stbir__simdf tot0,tot1,tot2,c,cs; \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf_load( cs, hc ); \ + stbir__simdf_0123to0001( c, cs ); \ + stbir__simdf_mult_mem( tot0, c, decode ); \ + stbir__simdf_0123to1122( c, cs ); \ + stbir__simdf_mult_mem( tot1, c, decode+4 ); \ + stbir__simdf_0123to2333( c, cs ); \ + stbir__simdf_mult_mem( tot2, c, decode+8 ); + +#define stbir__4_coeff_continue_from_4( ofs ) \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf_load( cs, hc + (ofs) ); \ + stbir__simdf_0123to0001( c, cs ); \ + stbir__simdf_madd_mem( tot0, tot0, c, decode+(ofs)*3 ); \ + stbir__simdf_0123to1122( c, cs ); \ + stbir__simdf_madd_mem( tot1, tot1, c, decode+(ofs)*3+4 ); \ + stbir__simdf_0123to2333( c, cs ); \ + stbir__simdf_madd_mem( tot2, tot2, c, decode+(ofs)*3+8 ); + +#define stbir__1_coeff_remnant( ofs ) \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf_load1z( c, hc + (ofs) ); \ + stbir__simdf_0123to0001( c, c ); \ + stbir__simdf_madd_mem( tot0, tot0, c, decode+(ofs)*3 ); + +#define stbir__2_coeff_remnant( ofs ) \ + { stbir__simdf d; \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf_load2z( cs, hc + (ofs) ); \ + stbir__simdf_0123to0001( c, cs ); \ + stbir__simdf_madd_mem( tot0, tot0, c, decode+(ofs)*3 ); \ + stbir__simdf_0123to1122( c, cs ); \ + stbir__simdf_load2z( d, decode+(ofs)*3+4 ); \ + stbir__simdf_madd( tot1, tot1, c, d ); } + +#define stbir__3_coeff_remnant( ofs ) \ + { stbir__simdf d; \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf_load( cs, hc + (ofs) ); \ + stbir__simdf_0123to0001( c, cs ); \ + stbir__simdf_madd_mem( tot0, tot0, c, decode+(ofs)*3 ); \ + stbir__simdf_0123to1122( c, cs ); \ + stbir__simdf_madd_mem( tot1, tot1, c, decode+(ofs)*3+4 ); \ + stbir__simdf_0123to2222( c, cs ); \ + stbir__simdf_load1z( d, decode+(ofs)*3+8 ); \ + stbir__simdf_madd( tot2, tot2, c, d ); } + +#define stbir__store_output() \ + stbir__simdf_0123ABCDto3ABx( c, tot0, tot1 ); \ + stbir__simdf_0123ABCDto23Ax( cs, tot1, tot2 ); \ + stbir__simdf_0123to1230( tot2, tot2 ); \ + stbir__simdf_add( tot0, tot0, cs ); \ + stbir__simdf_add( c, c, tot2 ); \ + stbir__simdf_add( tot0, tot0, c ); \ + horizontal_coefficients += coefficient_width; \ + ++horizontal_contributors; \ + output += 3; \ + if ( output < output_end ) \ + { \ + stbir__simdf_store( output-3, tot0 ); \ + continue; \ + } \ + stbir__simdf_0123to2301( tot1, tot0 ); \ + stbir__simdf_store2( output-3, tot0 ); \ + stbir__simdf_store1( output+2-3, tot1 ); \ + break; + +#endif + +#else + +#define stbir__1_coeff_only() \ + float tot0, tot1, tot2, c; \ + c = hc[0]; \ + tot0 = decode[0]*c; \ + tot1 = decode[1]*c; \ + tot2 = decode[2]*c; + +#define stbir__2_coeff_only() \ + float tot0, tot1, tot2, c; \ + c = hc[0]; \ + tot0 = decode[0]*c; \ + tot1 = decode[1]*c; \ + tot2 = decode[2]*c; \ + c = hc[1]; \ + tot0 += decode[3]*c; \ + tot1 += decode[4]*c; \ + tot2 += decode[5]*c; + +#define stbir__3_coeff_only() \ + float tot0, tot1, tot2, c; \ + c = hc[0]; \ + tot0 = decode[0]*c; \ + tot1 = decode[1]*c; \ + tot2 = decode[2]*c; \ + c = hc[1]; \ + tot0 += decode[3]*c; \ + tot1 += decode[4]*c; \ + tot2 += decode[5]*c; \ + c = hc[2]; \ + tot0 += decode[6]*c; \ + tot1 += decode[7]*c; \ + tot2 += decode[8]*c; + +#define stbir__store_output_tiny() \ + output[0] = tot0; \ + output[1] = tot1; \ + output[2] = tot2; \ + horizontal_coefficients += coefficient_width; \ + ++horizontal_contributors; \ + output += 3; + +#define stbir__4_coeff_start() \ + float tota0,tota1,tota2,totb0,totb1,totb2,totc0,totc1,totc2,totd0,totd1,totd2,c; \ + c = hc[0]; \ + tota0 = decode[0]*c; \ + tota1 = decode[1]*c; \ + tota2 = decode[2]*c; \ + c = hc[1]; \ + totb0 = decode[3]*c; \ + totb1 = decode[4]*c; \ + totb2 = decode[5]*c; \ + c = hc[2]; \ + totc0 = decode[6]*c; \ + totc1 = decode[7]*c; \ + totc2 = decode[8]*c; \ + c = hc[3]; \ + totd0 = decode[9]*c; \ + totd1 = decode[10]*c; \ + totd2 = decode[11]*c; + +#define stbir__4_coeff_continue_from_4( ofs ) \ + c = hc[0+(ofs)]; \ + tota0 += decode[0+(ofs)*3]*c; \ + tota1 += decode[1+(ofs)*3]*c; \ + tota2 += decode[2+(ofs)*3]*c; \ + c = hc[1+(ofs)]; \ + totb0 += decode[3+(ofs)*3]*c; \ + totb1 += decode[4+(ofs)*3]*c; \ + totb2 += decode[5+(ofs)*3]*c; \ + c = hc[2+(ofs)]; \ + totc0 += decode[6+(ofs)*3]*c; \ + totc1 += decode[7+(ofs)*3]*c; \ + totc2 += decode[8+(ofs)*3]*c; \ + c = hc[3+(ofs)]; \ + totd0 += decode[9+(ofs)*3]*c; \ + totd1 += decode[10+(ofs)*3]*c; \ + totd2 += decode[11+(ofs)*3]*c; + +#define stbir__1_coeff_remnant( ofs ) \ + c = hc[0+(ofs)]; \ + tota0 += decode[0+(ofs)*3]*c; \ + tota1 += decode[1+(ofs)*3]*c; \ + tota2 += decode[2+(ofs)*3]*c; + +#define stbir__2_coeff_remnant( ofs ) \ + c = hc[0+(ofs)]; \ + tota0 += decode[0+(ofs)*3]*c; \ + tota1 += decode[1+(ofs)*3]*c; \ + tota2 += decode[2+(ofs)*3]*c; \ + c = hc[1+(ofs)]; \ + totb0 += decode[3+(ofs)*3]*c; \ + totb1 += decode[4+(ofs)*3]*c; \ + totb2 += decode[5+(ofs)*3]*c; \ + +#define stbir__3_coeff_remnant( ofs ) \ + c = hc[0+(ofs)]; \ + tota0 += decode[0+(ofs)*3]*c; \ + tota1 += decode[1+(ofs)*3]*c; \ + tota2 += decode[2+(ofs)*3]*c; \ + c = hc[1+(ofs)]; \ + totb0 += decode[3+(ofs)*3]*c; \ + totb1 += decode[4+(ofs)*3]*c; \ + totb2 += decode[5+(ofs)*3]*c; \ + c = hc[2+(ofs)]; \ + totc0 += decode[6+(ofs)*3]*c; \ + totc1 += decode[7+(ofs)*3]*c; \ + totc2 += decode[8+(ofs)*3]*c; + +#define stbir__store_output() \ + output[0] = (tota0+totc0)+(totb0+totd0); \ + output[1] = (tota1+totc1)+(totb1+totd1); \ + output[2] = (tota2+totc2)+(totb2+totd2); \ + horizontal_coefficients += coefficient_width; \ + ++horizontal_contributors; \ + output += 3; + +#endif + +#define STBIR__horizontal_channels 3 +#define STB_IMAGE_RESIZE_DO_HORIZONTALS +#include STBIR__HEADER_FILENAME + +//================= +// Do 4 channel horizontal routines + +#ifdef STBIR_SIMD + +#define stbir__1_coeff_only() \ + stbir__simdf tot,c; \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf_load1( c, hc ); \ + stbir__simdf_0123to0000( c, c ); \ + stbir__simdf_mult_mem( tot, c, decode ); + +#define stbir__2_coeff_only() \ + stbir__simdf tot,c,cs; \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf_load2( cs, hc ); \ + stbir__simdf_0123to0000( c, cs ); \ + stbir__simdf_mult_mem( tot, c, decode ); \ + stbir__simdf_0123to1111( c, cs ); \ + stbir__simdf_madd_mem( tot, tot, c, decode+4 ); + +#define stbir__3_coeff_only() \ + stbir__simdf tot,c,cs; \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf_load( cs, hc ); \ + stbir__simdf_0123to0000( c, cs ); \ + stbir__simdf_mult_mem( tot, c, decode ); \ + stbir__simdf_0123to1111( c, cs ); \ + stbir__simdf_madd_mem( tot, tot, c, decode+4 ); \ + stbir__simdf_0123to2222( c, cs ); \ + stbir__simdf_madd_mem( tot, tot, c, decode+8 ); + +#define stbir__store_output_tiny() \ + stbir__simdf_store( output, tot ); \ + horizontal_coefficients += coefficient_width; \ + ++horizontal_contributors; \ + output += 4; + +#ifdef STBIR_SIMD8 + +#define stbir__4_coeff_start() \ + stbir__simdf8 tot0,c,cs; stbir__simdf t; \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf8_load4b( cs, hc ); \ + stbir__simdf8_0123to00001111( c, cs ); \ + stbir__simdf8_mult_mem( tot0, c, decode ); \ + stbir__simdf8_0123to22223333( c, cs ); \ + stbir__simdf8_madd_mem( tot0, tot0, c, decode+8 ); + +#define stbir__4_coeff_continue_from_4( ofs ) \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf8_load4b( cs, hc + (ofs) ); \ + stbir__simdf8_0123to00001111( c, cs ); \ + stbir__simdf8_madd_mem( tot0, tot0, c, decode+(ofs)*4 ); \ + stbir__simdf8_0123to22223333( c, cs ); \ + stbir__simdf8_madd_mem( tot0, tot0, c, decode+(ofs)*4+8 ); + +#define stbir__1_coeff_remnant( ofs ) \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf_load1rep4( t, hc + (ofs) ); \ + stbir__simdf8_madd_mem4( tot0, tot0, t, decode+(ofs)*4 ); + +#define stbir__2_coeff_remnant( ofs ) \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf8_load4b( cs, hc + (ofs) - 2 ); \ + stbir__simdf8_0123to22223333( c, cs ); \ + stbir__simdf8_madd_mem( tot0, tot0, c, decode+(ofs)*4 ); + + #define stbir__3_coeff_remnant( ofs ) \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf8_load4b( cs, hc + (ofs) ); \ + stbir__simdf8_0123to00001111( c, cs ); \ + stbir__simdf8_madd_mem( tot0, tot0, c, decode+(ofs)*4 ); \ + stbir__simdf8_0123to2222( t, cs ); \ + stbir__simdf8_madd_mem4( tot0, tot0, t, decode+(ofs)*4+8 ); + +#define stbir__store_output() \ + stbir__simdf8_add4halves( t, stbir__if_simdf8_cast_to_simdf4(tot0), tot0 ); \ + stbir__simdf_store( output, t ); \ + horizontal_coefficients += coefficient_width; \ + ++horizontal_contributors; \ + output += 4; + +#else + +#define stbir__4_coeff_start() \ + stbir__simdf tot0,tot1,c,cs; \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf_load( cs, hc ); \ + stbir__simdf_0123to0000( c, cs ); \ + stbir__simdf_mult_mem( tot0, c, decode ); \ + stbir__simdf_0123to1111( c, cs ); \ + stbir__simdf_mult_mem( tot1, c, decode+4 ); \ + stbir__simdf_0123to2222( c, cs ); \ + stbir__simdf_madd_mem( tot0, tot0, c, decode+8 ); \ + stbir__simdf_0123to3333( c, cs ); \ + stbir__simdf_madd_mem( tot1, tot1, c, decode+12 ); + +#define stbir__4_coeff_continue_from_4( ofs ) \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf_load( cs, hc + (ofs) ); \ + stbir__simdf_0123to0000( c, cs ); \ + stbir__simdf_madd_mem( tot0, tot0, c, decode+(ofs)*4 ); \ + stbir__simdf_0123to1111( c, cs ); \ + stbir__simdf_madd_mem( tot1, tot1, c, decode+(ofs)*4+4 ); \ + stbir__simdf_0123to2222( c, cs ); \ + stbir__simdf_madd_mem( tot0, tot0, c, decode+(ofs)*4+8 ); \ + stbir__simdf_0123to3333( c, cs ); \ + stbir__simdf_madd_mem( tot1, tot1, c, decode+(ofs)*4+12 ); + +#define stbir__1_coeff_remnant( ofs ) \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf_load1( c, hc + (ofs) ); \ + stbir__simdf_0123to0000( c, c ); \ + stbir__simdf_madd_mem( tot0, tot0, c, decode+(ofs)*4 ); + +#define stbir__2_coeff_remnant( ofs ) \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf_load2( cs, hc + (ofs) ); \ + stbir__simdf_0123to0000( c, cs ); \ + stbir__simdf_madd_mem( tot0, tot0, c, decode+(ofs)*4 ); \ + stbir__simdf_0123to1111( c, cs ); \ + stbir__simdf_madd_mem( tot1, tot1, c, decode+(ofs)*4+4 ); + +#define stbir__3_coeff_remnant( ofs ) \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf_load( cs, hc + (ofs) ); \ + stbir__simdf_0123to0000( c, cs ); \ + stbir__simdf_madd_mem( tot0, tot0, c, decode+(ofs)*4 ); \ + stbir__simdf_0123to1111( c, cs ); \ + stbir__simdf_madd_mem( tot1, tot1, c, decode+(ofs)*4+4 ); \ + stbir__simdf_0123to2222( c, cs ); \ + stbir__simdf_madd_mem( tot0, tot0, c, decode+(ofs)*4+8 ); + +#define stbir__store_output() \ + stbir__simdf_add( tot0, tot0, tot1 ); \ + stbir__simdf_store( output, tot0 ); \ + horizontal_coefficients += coefficient_width; \ + ++horizontal_contributors; \ + output += 4; + +#endif + +#else + +#define stbir__1_coeff_only() \ + float p0,p1,p2,p3,c; \ + STBIR_SIMD_NO_UNROLL(decode); \ + c = hc[0]; \ + p0 = decode[0] * c; \ + p1 = decode[1] * c; \ + p2 = decode[2] * c; \ + p3 = decode[3] * c; + +#define stbir__2_coeff_only() \ + float p0,p1,p2,p3,c; \ + STBIR_SIMD_NO_UNROLL(decode); \ + c = hc[0]; \ + p0 = decode[0] * c; \ + p1 = decode[1] * c; \ + p2 = decode[2] * c; \ + p3 = decode[3] * c; \ + c = hc[1]; \ + p0 += decode[4] * c; \ + p1 += decode[5] * c; \ + p2 += decode[6] * c; \ + p3 += decode[7] * c; + +#define stbir__3_coeff_only() \ + float p0,p1,p2,p3,c; \ + STBIR_SIMD_NO_UNROLL(decode); \ + c = hc[0]; \ + p0 = decode[0] * c; \ + p1 = decode[1] * c; \ + p2 = decode[2] * c; \ + p3 = decode[3] * c; \ + c = hc[1]; \ + p0 += decode[4] * c; \ + p1 += decode[5] * c; \ + p2 += decode[6] * c; \ + p3 += decode[7] * c; \ + c = hc[2]; \ + p0 += decode[8] * c; \ + p1 += decode[9] * c; \ + p2 += decode[10] * c; \ + p3 += decode[11] * c; + +#define stbir__store_output_tiny() \ + output[0] = p0; \ + output[1] = p1; \ + output[2] = p2; \ + output[3] = p3; \ + horizontal_coefficients += coefficient_width; \ + ++horizontal_contributors; \ + output += 4; + +#define stbir__4_coeff_start() \ + float x0,x1,x2,x3,y0,y1,y2,y3,c; \ + STBIR_SIMD_NO_UNROLL(decode); \ + c = hc[0]; \ + x0 = decode[0] * c; \ + x1 = decode[1] * c; \ + x2 = decode[2] * c; \ + x3 = decode[3] * c; \ + c = hc[1]; \ + y0 = decode[4] * c; \ + y1 = decode[5] * c; \ + y2 = decode[6] * c; \ + y3 = decode[7] * c; \ + c = hc[2]; \ + x0 += decode[8] * c; \ + x1 += decode[9] * c; \ + x2 += decode[10] * c; \ + x3 += decode[11] * c; \ + c = hc[3]; \ + y0 += decode[12] * c; \ + y1 += decode[13] * c; \ + y2 += decode[14] * c; \ + y3 += decode[15] * c; + +#define stbir__4_coeff_continue_from_4( ofs ) \ + STBIR_SIMD_NO_UNROLL(decode); \ + c = hc[0+(ofs)]; \ + x0 += decode[0+(ofs)*4] * c; \ + x1 += decode[1+(ofs)*4] * c; \ + x2 += decode[2+(ofs)*4] * c; \ + x3 += decode[3+(ofs)*4] * c; \ + c = hc[1+(ofs)]; \ + y0 += decode[4+(ofs)*4] * c; \ + y1 += decode[5+(ofs)*4] * c; \ + y2 += decode[6+(ofs)*4] * c; \ + y3 += decode[7+(ofs)*4] * c; \ + c = hc[2+(ofs)]; \ + x0 += decode[8+(ofs)*4] * c; \ + x1 += decode[9+(ofs)*4] * c; \ + x2 += decode[10+(ofs)*4] * c; \ + x3 += decode[11+(ofs)*4] * c; \ + c = hc[3+(ofs)]; \ + y0 += decode[12+(ofs)*4] * c; \ + y1 += decode[13+(ofs)*4] * c; \ + y2 += decode[14+(ofs)*4] * c; \ + y3 += decode[15+(ofs)*4] * c; + +#define stbir__1_coeff_remnant( ofs ) \ + STBIR_SIMD_NO_UNROLL(decode); \ + c = hc[0+(ofs)]; \ + x0 += decode[0+(ofs)*4] * c; \ + x1 += decode[1+(ofs)*4] * c; \ + x2 += decode[2+(ofs)*4] * c; \ + x3 += decode[3+(ofs)*4] * c; + +#define stbir__2_coeff_remnant( ofs ) \ + STBIR_SIMD_NO_UNROLL(decode); \ + c = hc[0+(ofs)]; \ + x0 += decode[0+(ofs)*4] * c; \ + x1 += decode[1+(ofs)*4] * c; \ + x2 += decode[2+(ofs)*4] * c; \ + x3 += decode[3+(ofs)*4] * c; \ + c = hc[1+(ofs)]; \ + y0 += decode[4+(ofs)*4] * c; \ + y1 += decode[5+(ofs)*4] * c; \ + y2 += decode[6+(ofs)*4] * c; \ + y3 += decode[7+(ofs)*4] * c; + +#define stbir__3_coeff_remnant( ofs ) \ + STBIR_SIMD_NO_UNROLL(decode); \ + c = hc[0+(ofs)]; \ + x0 += decode[0+(ofs)*4] * c; \ + x1 += decode[1+(ofs)*4] * c; \ + x2 += decode[2+(ofs)*4] * c; \ + x3 += decode[3+(ofs)*4] * c; \ + c = hc[1+(ofs)]; \ + y0 += decode[4+(ofs)*4] * c; \ + y1 += decode[5+(ofs)*4] * c; \ + y2 += decode[6+(ofs)*4] * c; \ + y3 += decode[7+(ofs)*4] * c; \ + c = hc[2+(ofs)]; \ + x0 += decode[8+(ofs)*4] * c; \ + x1 += decode[9+(ofs)*4] * c; \ + x2 += decode[10+(ofs)*4] * c; \ + x3 += decode[11+(ofs)*4] * c; + +#define stbir__store_output() \ + output[0] = x0 + y0; \ + output[1] = x1 + y1; \ + output[2] = x2 + y2; \ + output[3] = x3 + y3; \ + horizontal_coefficients += coefficient_width; \ + ++horizontal_contributors; \ + output += 4; + +#endif + +#define STBIR__horizontal_channels 4 +#define STB_IMAGE_RESIZE_DO_HORIZONTALS +#include STBIR__HEADER_FILENAME + + + +//================= +// Do 7 channel horizontal routines + +#ifdef STBIR_SIMD + +#define stbir__1_coeff_only() \ + stbir__simdf tot0,tot1,c; \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf_load1( c, hc ); \ + stbir__simdf_0123to0000( c, c ); \ + stbir__simdf_mult_mem( tot0, c, decode ); \ + stbir__simdf_mult_mem( tot1, c, decode+3 ); + +#define stbir__2_coeff_only() \ + stbir__simdf tot0,tot1,c,cs; \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf_load2( cs, hc ); \ + stbir__simdf_0123to0000( c, cs ); \ + stbir__simdf_mult_mem( tot0, c, decode ); \ + stbir__simdf_mult_mem( tot1, c, decode+3 ); \ + stbir__simdf_0123to1111( c, cs ); \ + stbir__simdf_madd_mem( tot0, tot0, c, decode+7 ); \ + stbir__simdf_madd_mem( tot1, tot1, c,decode+10 ); + +#define stbir__3_coeff_only() \ + stbir__simdf tot0,tot1,c,cs; \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf_load( cs, hc ); \ + stbir__simdf_0123to0000( c, cs ); \ + stbir__simdf_mult_mem( tot0, c, decode ); \ + stbir__simdf_mult_mem( tot1, c, decode+3 ); \ + stbir__simdf_0123to1111( c, cs ); \ + stbir__simdf_madd_mem( tot0, tot0, c, decode+7 ); \ + stbir__simdf_madd_mem( tot1, tot1, c, decode+10 ); \ + stbir__simdf_0123to2222( c, cs ); \ + stbir__simdf_madd_mem( tot0, tot0, c, decode+14 ); \ + stbir__simdf_madd_mem( tot1, tot1, c, decode+17 ); + +#define stbir__store_output_tiny() \ + stbir__simdf_store( output+3, tot1 ); \ + stbir__simdf_store( output, tot0 ); \ + horizontal_coefficients += coefficient_width; \ + ++horizontal_contributors; \ + output += 7; + +#ifdef STBIR_SIMD8 + +#define stbir__4_coeff_start() \ + stbir__simdf8 tot0,tot1,c,cs; \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf8_load4b( cs, hc ); \ + stbir__simdf8_0123to00000000( c, cs ); \ + stbir__simdf8_mult_mem( tot0, c, decode ); \ + stbir__simdf8_0123to11111111( c, cs ); \ + stbir__simdf8_mult_mem( tot1, c, decode+7 ); \ + stbir__simdf8_0123to22222222( c, cs ); \ + stbir__simdf8_madd_mem( tot0, tot0, c, decode+14 ); \ + stbir__simdf8_0123to33333333( c, cs ); \ + stbir__simdf8_madd_mem( tot1, tot1, c, decode+21 ); + +#define stbir__4_coeff_continue_from_4( ofs ) \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf8_load4b( cs, hc + (ofs) ); \ + stbir__simdf8_0123to00000000( c, cs ); \ + stbir__simdf8_madd_mem( tot0, tot0, c, decode+(ofs)*7 ); \ + stbir__simdf8_0123to11111111( c, cs ); \ + stbir__simdf8_madd_mem( tot1, tot1, c, decode+(ofs)*7+7 ); \ + stbir__simdf8_0123to22222222( c, cs ); \ + stbir__simdf8_madd_mem( tot0, tot0, c, decode+(ofs)*7+14 ); \ + stbir__simdf8_0123to33333333( c, cs ); \ + stbir__simdf8_madd_mem( tot1, tot1, c, decode+(ofs)*7+21 ); + +#define stbir__1_coeff_remnant( ofs ) \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf8_load1b( c, hc + (ofs) ); \ + stbir__simdf8_madd_mem( tot0, tot0, c, decode+(ofs)*7 ); + +#define stbir__2_coeff_remnant( ofs ) \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf8_load1b( c, hc + (ofs) ); \ + stbir__simdf8_madd_mem( tot0, tot0, c, decode+(ofs)*7 ); \ + stbir__simdf8_load1b( c, hc + (ofs)+1 ); \ + stbir__simdf8_madd_mem( tot1, tot1, c, decode+(ofs)*7+7 ); + +#define stbir__3_coeff_remnant( ofs ) \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf8_load4b( cs, hc + (ofs) ); \ + stbir__simdf8_0123to00000000( c, cs ); \ + stbir__simdf8_madd_mem( tot0, tot0, c, decode+(ofs)*7 ); \ + stbir__simdf8_0123to11111111( c, cs ); \ + stbir__simdf8_madd_mem( tot1, tot1, c, decode+(ofs)*7+7 ); \ + stbir__simdf8_0123to22222222( c, cs ); \ + stbir__simdf8_madd_mem( tot0, tot0, c, decode+(ofs)*7+14 ); + +#define stbir__store_output() \ + stbir__simdf8_add( tot0, tot0, tot1 ); \ + horizontal_coefficients += coefficient_width; \ + ++horizontal_contributors; \ + output += 7; \ + if ( output < output_end ) \ + { \ + stbir__simdf8_store( output-7, tot0 ); \ + continue; \ + } \ + stbir__simdf_store( output-7+3, stbir__simdf_swiz(stbir__simdf8_gettop4(tot0),0,0,1,2) ); \ + stbir__simdf_store( output-7, stbir__if_simdf8_cast_to_simdf4(tot0) ); \ + break; + +#else + +#define stbir__4_coeff_start() \ + stbir__simdf tot0,tot1,tot2,tot3,c,cs; \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf_load( cs, hc ); \ + stbir__simdf_0123to0000( c, cs ); \ + stbir__simdf_mult_mem( tot0, c, decode ); \ + stbir__simdf_mult_mem( tot1, c, decode+3 ); \ + stbir__simdf_0123to1111( c, cs ); \ + stbir__simdf_mult_mem( tot2, c, decode+7 ); \ + stbir__simdf_mult_mem( tot3, c, decode+10 ); \ + stbir__simdf_0123to2222( c, cs ); \ + stbir__simdf_madd_mem( tot0, tot0, c, decode+14 ); \ + stbir__simdf_madd_mem( tot1, tot1, c, decode+17 ); \ + stbir__simdf_0123to3333( c, cs ); \ + stbir__simdf_madd_mem( tot2, tot2, c, decode+21 ); \ + stbir__simdf_madd_mem( tot3, tot3, c, decode+24 ); + +#define stbir__4_coeff_continue_from_4( ofs ) \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf_load( cs, hc + (ofs) ); \ + stbir__simdf_0123to0000( c, cs ); \ + stbir__simdf_madd_mem( tot0, tot0, c, decode+(ofs)*7 ); \ + stbir__simdf_madd_mem( tot1, tot1, c, decode+(ofs)*7+3 ); \ + stbir__simdf_0123to1111( c, cs ); \ + stbir__simdf_madd_mem( tot2, tot2, c, decode+(ofs)*7+7 ); \ + stbir__simdf_madd_mem( tot3, tot3, c, decode+(ofs)*7+10 ); \ + stbir__simdf_0123to2222( c, cs ); \ + stbir__simdf_madd_mem( tot0, tot0, c, decode+(ofs)*7+14 ); \ + stbir__simdf_madd_mem( tot1, tot1, c, decode+(ofs)*7+17 ); \ + stbir__simdf_0123to3333( c, cs ); \ + stbir__simdf_madd_mem( tot2, tot2, c, decode+(ofs)*7+21 ); \ + stbir__simdf_madd_mem( tot3, tot3, c, decode+(ofs)*7+24 ); + +#define stbir__1_coeff_remnant( ofs ) \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf_load1( c, hc + (ofs) ); \ + stbir__simdf_0123to0000( c, c ); \ + stbir__simdf_madd_mem( tot0, tot0, c, decode+(ofs)*7 ); \ + stbir__simdf_madd_mem( tot1, tot1, c, decode+(ofs)*7+3 ); \ + +#define stbir__2_coeff_remnant( ofs ) \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf_load2( cs, hc + (ofs) ); \ + stbir__simdf_0123to0000( c, cs ); \ + stbir__simdf_madd_mem( tot0, tot0, c, decode+(ofs)*7 ); \ + stbir__simdf_madd_mem( tot1, tot1, c, decode+(ofs)*7+3 ); \ + stbir__simdf_0123to1111( c, cs ); \ + stbir__simdf_madd_mem( tot2, tot2, c, decode+(ofs)*7+7 ); \ + stbir__simdf_madd_mem( tot3, tot3, c, decode+(ofs)*7+10 ); + +#define stbir__3_coeff_remnant( ofs ) \ + STBIR_SIMD_NO_UNROLL(decode); \ + stbir__simdf_load( cs, hc + (ofs) ); \ + stbir__simdf_0123to0000( c, cs ); \ + stbir__simdf_madd_mem( tot0, tot0, c, decode+(ofs)*7 ); \ + stbir__simdf_madd_mem( tot1, tot1, c, decode+(ofs)*7+3 ); \ + stbir__simdf_0123to1111( c, cs ); \ + stbir__simdf_madd_mem( tot2, tot2, c, decode+(ofs)*7+7 ); \ + stbir__simdf_madd_mem( tot3, tot3, c, decode+(ofs)*7+10 ); \ + stbir__simdf_0123to2222( c, cs ); \ + stbir__simdf_madd_mem( tot0, tot0, c, decode+(ofs)*7+14 ); \ + stbir__simdf_madd_mem( tot1, tot1, c, decode+(ofs)*7+17 ); + +#define stbir__store_output() \ + stbir__simdf_add( tot0, tot0, tot2 ); \ + stbir__simdf_add( tot1, tot1, tot3 ); \ + stbir__simdf_store( output+3, tot1 ); \ + stbir__simdf_store( output, tot0 ); \ + horizontal_coefficients += coefficient_width; \ + ++horizontal_contributors; \ + output += 7; + +#endif + +#else + +#define stbir__1_coeff_only() \ + float tot0, tot1, tot2, tot3, tot4, tot5, tot6, c; \ + c = hc[0]; \ + tot0 = decode[0]*c; \ + tot1 = decode[1]*c; \ + tot2 = decode[2]*c; \ + tot3 = decode[3]*c; \ + tot4 = decode[4]*c; \ + tot5 = decode[5]*c; \ + tot6 = decode[6]*c; + +#define stbir__2_coeff_only() \ + float tot0, tot1, tot2, tot3, tot4, tot5, tot6, c; \ + c = hc[0]; \ + tot0 = decode[0]*c; \ + tot1 = decode[1]*c; \ + tot2 = decode[2]*c; \ + tot3 = decode[3]*c; \ + tot4 = decode[4]*c; \ + tot5 = decode[5]*c; \ + tot6 = decode[6]*c; \ + c = hc[1]; \ + tot0 += decode[7]*c; \ + tot1 += decode[8]*c; \ + tot2 += decode[9]*c; \ + tot3 += decode[10]*c; \ + tot4 += decode[11]*c; \ + tot5 += decode[12]*c; \ + tot6 += decode[13]*c; \ + +#define stbir__3_coeff_only() \ + float tot0, tot1, tot2, tot3, tot4, tot5, tot6, c; \ + c = hc[0]; \ + tot0 = decode[0]*c; \ + tot1 = decode[1]*c; \ + tot2 = decode[2]*c; \ + tot3 = decode[3]*c; \ + tot4 = decode[4]*c; \ + tot5 = decode[5]*c; \ + tot6 = decode[6]*c; \ + c = hc[1]; \ + tot0 += decode[7]*c; \ + tot1 += decode[8]*c; \ + tot2 += decode[9]*c; \ + tot3 += decode[10]*c; \ + tot4 += decode[11]*c; \ + tot5 += decode[12]*c; \ + tot6 += decode[13]*c; \ + c = hc[2]; \ + tot0 += decode[14]*c; \ + tot1 += decode[15]*c; \ + tot2 += decode[16]*c; \ + tot3 += decode[17]*c; \ + tot4 += decode[18]*c; \ + tot5 += decode[19]*c; \ + tot6 += decode[20]*c; \ + +#define stbir__store_output_tiny() \ + output[0] = tot0; \ + output[1] = tot1; \ + output[2] = tot2; \ + output[3] = tot3; \ + output[4] = tot4; \ + output[5] = tot5; \ + output[6] = tot6; \ + horizontal_coefficients += coefficient_width; \ + ++horizontal_contributors; \ + output += 7; + +#define stbir__4_coeff_start() \ + float x0,x1,x2,x3,x4,x5,x6,y0,y1,y2,y3,y4,y5,y6,c; \ + STBIR_SIMD_NO_UNROLL(decode); \ + c = hc[0]; \ + x0 = decode[0] * c; \ + x1 = decode[1] * c; \ + x2 = decode[2] * c; \ + x3 = decode[3] * c; \ + x4 = decode[4] * c; \ + x5 = decode[5] * c; \ + x6 = decode[6] * c; \ + c = hc[1]; \ + y0 = decode[7] * c; \ + y1 = decode[8] * c; \ + y2 = decode[9] * c; \ + y3 = decode[10] * c; \ + y4 = decode[11] * c; \ + y5 = decode[12] * c; \ + y6 = decode[13] * c; \ + c = hc[2]; \ + x0 += decode[14] * c; \ + x1 += decode[15] * c; \ + x2 += decode[16] * c; \ + x3 += decode[17] * c; \ + x4 += decode[18] * c; \ + x5 += decode[19] * c; \ + x6 += decode[20] * c; \ + c = hc[3]; \ + y0 += decode[21] * c; \ + y1 += decode[22] * c; \ + y2 += decode[23] * c; \ + y3 += decode[24] * c; \ + y4 += decode[25] * c; \ + y5 += decode[26] * c; \ + y6 += decode[27] * c; + +#define stbir__4_coeff_continue_from_4( ofs ) \ + STBIR_SIMD_NO_UNROLL(decode); \ + c = hc[0+(ofs)]; \ + x0 += decode[0+(ofs)*7] * c; \ + x1 += decode[1+(ofs)*7] * c; \ + x2 += decode[2+(ofs)*7] * c; \ + x3 += decode[3+(ofs)*7] * c; \ + x4 += decode[4+(ofs)*7] * c; \ + x5 += decode[5+(ofs)*7] * c; \ + x6 += decode[6+(ofs)*7] * c; \ + c = hc[1+(ofs)]; \ + y0 += decode[7+(ofs)*7] * c; \ + y1 += decode[8+(ofs)*7] * c; \ + y2 += decode[9+(ofs)*7] * c; \ + y3 += decode[10+(ofs)*7] * c; \ + y4 += decode[11+(ofs)*7] * c; \ + y5 += decode[12+(ofs)*7] * c; \ + y6 += decode[13+(ofs)*7] * c; \ + c = hc[2+(ofs)]; \ + x0 += decode[14+(ofs)*7] * c; \ + x1 += decode[15+(ofs)*7] * c; \ + x2 += decode[16+(ofs)*7] * c; \ + x3 += decode[17+(ofs)*7] * c; \ + x4 += decode[18+(ofs)*7] * c; \ + x5 += decode[19+(ofs)*7] * c; \ + x6 += decode[20+(ofs)*7] * c; \ + c = hc[3+(ofs)]; \ + y0 += decode[21+(ofs)*7] * c; \ + y1 += decode[22+(ofs)*7] * c; \ + y2 += decode[23+(ofs)*7] * c; \ + y3 += decode[24+(ofs)*7] * c; \ + y4 += decode[25+(ofs)*7] * c; \ + y5 += decode[26+(ofs)*7] * c; \ + y6 += decode[27+(ofs)*7] * c; + +#define stbir__1_coeff_remnant( ofs ) \ + STBIR_SIMD_NO_UNROLL(decode); \ + c = hc[0+(ofs)]; \ + x0 += decode[0+(ofs)*7] * c; \ + x1 += decode[1+(ofs)*7] * c; \ + x2 += decode[2+(ofs)*7] * c; \ + x3 += decode[3+(ofs)*7] * c; \ + x4 += decode[4+(ofs)*7] * c; \ + x5 += decode[5+(ofs)*7] * c; \ + x6 += decode[6+(ofs)*7] * c; \ + +#define stbir__2_coeff_remnant( ofs ) \ + STBIR_SIMD_NO_UNROLL(decode); \ + c = hc[0+(ofs)]; \ + x0 += decode[0+(ofs)*7] * c; \ + x1 += decode[1+(ofs)*7] * c; \ + x2 += decode[2+(ofs)*7] * c; \ + x3 += decode[3+(ofs)*7] * c; \ + x4 += decode[4+(ofs)*7] * c; \ + x5 += decode[5+(ofs)*7] * c; \ + x6 += decode[6+(ofs)*7] * c; \ + c = hc[1+(ofs)]; \ + y0 += decode[7+(ofs)*7] * c; \ + y1 += decode[8+(ofs)*7] * c; \ + y2 += decode[9+(ofs)*7] * c; \ + y3 += decode[10+(ofs)*7] * c; \ + y4 += decode[11+(ofs)*7] * c; \ + y5 += decode[12+(ofs)*7] * c; \ + y6 += decode[13+(ofs)*7] * c; \ + +#define stbir__3_coeff_remnant( ofs ) \ + STBIR_SIMD_NO_UNROLL(decode); \ + c = hc[0+(ofs)]; \ + x0 += decode[0+(ofs)*7] * c; \ + x1 += decode[1+(ofs)*7] * c; \ + x2 += decode[2+(ofs)*7] * c; \ + x3 += decode[3+(ofs)*7] * c; \ + x4 += decode[4+(ofs)*7] * c; \ + x5 += decode[5+(ofs)*7] * c; \ + x6 += decode[6+(ofs)*7] * c; \ + c = hc[1+(ofs)]; \ + y0 += decode[7+(ofs)*7] * c; \ + y1 += decode[8+(ofs)*7] * c; \ + y2 += decode[9+(ofs)*7] * c; \ + y3 += decode[10+(ofs)*7] * c; \ + y4 += decode[11+(ofs)*7] * c; \ + y5 += decode[12+(ofs)*7] * c; \ + y6 += decode[13+(ofs)*7] * c; \ + c = hc[2+(ofs)]; \ + x0 += decode[14+(ofs)*7] * c; \ + x1 += decode[15+(ofs)*7] * c; \ + x2 += decode[16+(ofs)*7] * c; \ + x3 += decode[17+(ofs)*7] * c; \ + x4 += decode[18+(ofs)*7] * c; \ + x5 += decode[19+(ofs)*7] * c; \ + x6 += decode[20+(ofs)*7] * c; \ + +#define stbir__store_output() \ + output[0] = x0 + y0; \ + output[1] = x1 + y1; \ + output[2] = x2 + y2; \ + output[3] = x3 + y3; \ + output[4] = x4 + y4; \ + output[5] = x5 + y5; \ + output[6] = x6 + y6; \ + horizontal_coefficients += coefficient_width; \ + ++horizontal_contributors; \ + output += 7; + +#endif + +#define STBIR__horizontal_channels 7 +#define STB_IMAGE_RESIZE_DO_HORIZONTALS +#include STBIR__HEADER_FILENAME + + +// include all of the vertical resamplers (both scatter and gather versions) + +#define STBIR__vertical_channels 1 +#define STB_IMAGE_RESIZE_DO_VERTICALS +#include STBIR__HEADER_FILENAME + +#define STBIR__vertical_channels 1 +#define STB_IMAGE_RESIZE_DO_VERTICALS +#define STB_IMAGE_RESIZE_VERTICAL_CONTINUE +#include STBIR__HEADER_FILENAME + +#define STBIR__vertical_channels 2 +#define STB_IMAGE_RESIZE_DO_VERTICALS +#include STBIR__HEADER_FILENAME + +#define STBIR__vertical_channels 2 +#define STB_IMAGE_RESIZE_DO_VERTICALS +#define STB_IMAGE_RESIZE_VERTICAL_CONTINUE +#include STBIR__HEADER_FILENAME + +#define STBIR__vertical_channels 3 +#define STB_IMAGE_RESIZE_DO_VERTICALS +#include STBIR__HEADER_FILENAME + +#define STBIR__vertical_channels 3 +#define STB_IMAGE_RESIZE_DO_VERTICALS +#define STB_IMAGE_RESIZE_VERTICAL_CONTINUE +#include STBIR__HEADER_FILENAME + +#define STBIR__vertical_channels 4 +#define STB_IMAGE_RESIZE_DO_VERTICALS +#include STBIR__HEADER_FILENAME + +#define STBIR__vertical_channels 4 +#define STB_IMAGE_RESIZE_DO_VERTICALS +#define STB_IMAGE_RESIZE_VERTICAL_CONTINUE +#include STBIR__HEADER_FILENAME + +#define STBIR__vertical_channels 5 +#define STB_IMAGE_RESIZE_DO_VERTICALS +#include STBIR__HEADER_FILENAME + +#define STBIR__vertical_channels 5 +#define STB_IMAGE_RESIZE_DO_VERTICALS +#define STB_IMAGE_RESIZE_VERTICAL_CONTINUE +#include STBIR__HEADER_FILENAME + +#define STBIR__vertical_channels 6 +#define STB_IMAGE_RESIZE_DO_VERTICALS +#include STBIR__HEADER_FILENAME + +#define STBIR__vertical_channels 6 +#define STB_IMAGE_RESIZE_DO_VERTICALS +#define STB_IMAGE_RESIZE_VERTICAL_CONTINUE +#include STBIR__HEADER_FILENAME + +#define STBIR__vertical_channels 7 +#define STB_IMAGE_RESIZE_DO_VERTICALS +#include STBIR__HEADER_FILENAME + +#define STBIR__vertical_channels 7 +#define STB_IMAGE_RESIZE_DO_VERTICALS +#define STB_IMAGE_RESIZE_VERTICAL_CONTINUE +#include STBIR__HEADER_FILENAME + +#define STBIR__vertical_channels 8 +#define STB_IMAGE_RESIZE_DO_VERTICALS +#include STBIR__HEADER_FILENAME + +#define STBIR__vertical_channels 8 +#define STB_IMAGE_RESIZE_DO_VERTICALS +#define STB_IMAGE_RESIZE_VERTICAL_CONTINUE +#include STBIR__HEADER_FILENAME + +typedef void STBIR_VERTICAL_GATHERFUNC( float * output, float const * coeffs, float const ** inputs, float const * input0_end ); + +static STBIR_VERTICAL_GATHERFUNC * stbir__vertical_gathers[ 8 ] = +{ + stbir__vertical_gather_with_1_coeffs,stbir__vertical_gather_with_2_coeffs,stbir__vertical_gather_with_3_coeffs,stbir__vertical_gather_with_4_coeffs,stbir__vertical_gather_with_5_coeffs,stbir__vertical_gather_with_6_coeffs,stbir__vertical_gather_with_7_coeffs,stbir__vertical_gather_with_8_coeffs +}; + +static STBIR_VERTICAL_GATHERFUNC * stbir__vertical_gathers_continues[ 8 ] = +{ + stbir__vertical_gather_with_1_coeffs_cont,stbir__vertical_gather_with_2_coeffs_cont,stbir__vertical_gather_with_3_coeffs_cont,stbir__vertical_gather_with_4_coeffs_cont,stbir__vertical_gather_with_5_coeffs_cont,stbir__vertical_gather_with_6_coeffs_cont,stbir__vertical_gather_with_7_coeffs_cont,stbir__vertical_gather_with_8_coeffs_cont +}; + +typedef void STBIR_VERTICAL_SCATTERFUNC( float ** outputs, float const * coeffs, float const * input, float const * input_end ); + +static STBIR_VERTICAL_SCATTERFUNC * stbir__vertical_scatter_sets[ 8 ] = +{ + stbir__vertical_scatter_with_1_coeffs,stbir__vertical_scatter_with_2_coeffs,stbir__vertical_scatter_with_3_coeffs,stbir__vertical_scatter_with_4_coeffs,stbir__vertical_scatter_with_5_coeffs,stbir__vertical_scatter_with_6_coeffs,stbir__vertical_scatter_with_7_coeffs,stbir__vertical_scatter_with_8_coeffs +}; + +static STBIR_VERTICAL_SCATTERFUNC * stbir__vertical_scatter_blends[ 8 ] = +{ + stbir__vertical_scatter_with_1_coeffs_cont,stbir__vertical_scatter_with_2_coeffs_cont,stbir__vertical_scatter_with_3_coeffs_cont,stbir__vertical_scatter_with_4_coeffs_cont,stbir__vertical_scatter_with_5_coeffs_cont,stbir__vertical_scatter_with_6_coeffs_cont,stbir__vertical_scatter_with_7_coeffs_cont,stbir__vertical_scatter_with_8_coeffs_cont +}; + + +static void stbir__encode_scanline( stbir__info const * stbir_info, void *output_buffer_data, float * encode_buffer, int row STBIR_ONLY_PROFILE_GET_SPLIT_INFO ) +{ + int num_pixels = stbir_info->horizontal.scale_info.output_sub_size; + int channels = stbir_info->channels; + int width_times_channels = num_pixels * channels; + void * output_buffer; + + // un-alpha weight if we need to + if ( stbir_info->alpha_unweight ) + { + STBIR_PROFILE_START( unalpha ); + stbir_info->alpha_unweight( encode_buffer, width_times_channels ); + STBIR_PROFILE_END( unalpha ); + } + + // write directly into output by default + output_buffer = output_buffer_data; + + // if we have an output callback, we first convert the decode buffer in place (and then hand that to the callback) + if ( stbir_info->out_pixels_cb ) + output_buffer = encode_buffer; + + STBIR_PROFILE_START( encode ); + // convert into the output buffer + stbir_info->encode_pixels( output_buffer, width_times_channels, encode_buffer ); + STBIR_PROFILE_END( encode ); + + // if we have an output callback, call it to send the data + if ( stbir_info->out_pixels_cb ) + stbir_info->out_pixels_cb( output_buffer, num_pixels, row, stbir_info->user_data ); +} + + +// Get the ring buffer pointer for an index +static float* stbir__get_ring_buffer_entry(stbir__info const * stbir_info, stbir__per_split_info const * split_info, int index ) +{ + STBIR_ASSERT( index < stbir_info->ring_buffer_num_entries ); + + #ifdef STBIR__SEPARATE_ALLOCATIONS + return split_info->ring_buffers[ index ]; + #else + return (float*) ( ( (char*) split_info->ring_buffer ) + ( index * stbir_info->ring_buffer_length_bytes ) ); + #endif +} + +// Get the specified scan line from the ring buffer +static float* stbir__get_ring_buffer_scanline(stbir__info const * stbir_info, stbir__per_split_info const * split_info, int get_scanline) +{ + int ring_buffer_index = (split_info->ring_buffer_begin_index + (get_scanline - split_info->ring_buffer_first_scanline)) % stbir_info->ring_buffer_num_entries; + return stbir__get_ring_buffer_entry( stbir_info, split_info, ring_buffer_index ); +} + +static void stbir__resample_horizontal_gather(stbir__info const * stbir_info, float* output_buffer, float const * input_buffer STBIR_ONLY_PROFILE_GET_SPLIT_INFO ) +{ + float const * decode_buffer = input_buffer - ( stbir_info->scanline_extents.conservative.n0 * stbir_info->effective_channels ); + + STBIR_PROFILE_START( horizontal ); + if ( ( stbir_info->horizontal.filter_enum == STBIR_FILTER_POINT_SAMPLE ) && ( stbir_info->horizontal.scale_info.scale == 1.0f ) ) + STBIR_MEMCPY( output_buffer, input_buffer, stbir_info->horizontal.scale_info.output_sub_size * sizeof( float ) * stbir_info->effective_channels ); + else + stbir_info->horizontal_gather_channels( output_buffer, stbir_info->horizontal.scale_info.output_sub_size, decode_buffer, stbir_info->horizontal.contributors, stbir_info->horizontal.coefficients, stbir_info->horizontal.coefficient_width ); + STBIR_PROFILE_END( horizontal ); +} + +static void stbir__resample_vertical_gather(stbir__info const * stbir_info, stbir__per_split_info* split_info, int n, int contrib_n0, int contrib_n1, float const * vertical_coefficients ) +{ + float* encode_buffer = split_info->vertical_buffer; + float* decode_buffer = split_info->decode_buffer; + int vertical_first = stbir_info->vertical_first; + int width = (vertical_first) ? ( stbir_info->scanline_extents.conservative.n1-stbir_info->scanline_extents.conservative.n0+1 ) : stbir_info->horizontal.scale_info.output_sub_size; + int width_times_channels = stbir_info->effective_channels * width; + + STBIR_ASSERT( stbir_info->vertical.is_gather ); + + // loop over the contributing scanlines and scale into the buffer + STBIR_PROFILE_START( vertical ); + { + int k = 0, total = contrib_n1 - contrib_n0 + 1; + STBIR_ASSERT( total > 0 ); + do { + float const * inputs[8]; + int i, cnt = total; if ( cnt > 8 ) cnt = 8; + for( i = 0 ; i < cnt ; i++ ) + inputs[ i ] = stbir__get_ring_buffer_scanline(stbir_info, split_info, k+i+contrib_n0 ); + + // call the N scanlines at a time function (up to 8 scanlines of blending at once) + ((k==0)?stbir__vertical_gathers:stbir__vertical_gathers_continues)[cnt-1]( (vertical_first) ? decode_buffer : encode_buffer, vertical_coefficients + k, inputs, inputs[0] + width_times_channels ); + k += cnt; + total -= cnt; + } while ( total ); + } + STBIR_PROFILE_END( vertical ); + + if ( vertical_first ) + { + // Now resample the gathered vertical data in the horizontal axis into the encode buffer + decode_buffer[ width_times_channels ] = 0.0f; // clear two over for horizontals with a remnant of 3 + decode_buffer[ width_times_channels+1 ] = 0.0f; + stbir__resample_horizontal_gather(stbir_info, encode_buffer, decode_buffer STBIR_ONLY_PROFILE_SET_SPLIT_INFO ); + } + + stbir__encode_scanline( stbir_info, ( (char *) stbir_info->output_data ) + ((size_t)n * (size_t)stbir_info->output_stride_bytes), + encode_buffer, n STBIR_ONLY_PROFILE_SET_SPLIT_INFO ); +} + +static void stbir__decode_and_resample_for_vertical_gather_loop(stbir__info const * stbir_info, stbir__per_split_info* split_info, int n) +{ + int ring_buffer_index; + float* ring_buffer; + + // Decode the nth scanline from the source image into the decode buffer. + stbir__decode_scanline( stbir_info, n, split_info->decode_buffer STBIR_ONLY_PROFILE_SET_SPLIT_INFO ); + + // update new end scanline + split_info->ring_buffer_last_scanline = n; + + // get ring buffer + ring_buffer_index = (split_info->ring_buffer_begin_index + (split_info->ring_buffer_last_scanline - split_info->ring_buffer_first_scanline)) % stbir_info->ring_buffer_num_entries; + ring_buffer = stbir__get_ring_buffer_entry(stbir_info, split_info, ring_buffer_index); + + // Now resample it into the ring buffer. + stbir__resample_horizontal_gather( stbir_info, ring_buffer, split_info->decode_buffer STBIR_ONLY_PROFILE_SET_SPLIT_INFO ); + + // Now it's sitting in the ring buffer ready to be used as source for the vertical sampling. +} + +static void stbir__vertical_gather_loop( stbir__info const * stbir_info, stbir__per_split_info* split_info, int split_count ) +{ + int y, start_output_y, end_output_y; + stbir__contributors* vertical_contributors = stbir_info->vertical.contributors; + float const * vertical_coefficients = stbir_info->vertical.coefficients; + + STBIR_ASSERT( stbir_info->vertical.is_gather ); + + start_output_y = split_info->start_output_y; + end_output_y = split_info[split_count-1].end_output_y; + + vertical_contributors += start_output_y; + vertical_coefficients += start_output_y * stbir_info->vertical.coefficient_width; + + // initialize the ring buffer for gathering + split_info->ring_buffer_begin_index = 0; + split_info->ring_buffer_first_scanline = vertical_contributors->n0; + split_info->ring_buffer_last_scanline = split_info->ring_buffer_first_scanline - 1; // means "empty" + + for (y = start_output_y; y < end_output_y; y++) + { + int in_first_scanline, in_last_scanline; + + in_first_scanline = vertical_contributors->n0; + in_last_scanline = vertical_contributors->n1; + + // make sure the indexing hasn't broken + STBIR_ASSERT( in_first_scanline >= split_info->ring_buffer_first_scanline ); + + // Load in new scanlines + while (in_last_scanline > split_info->ring_buffer_last_scanline) + { + STBIR_ASSERT( ( split_info->ring_buffer_last_scanline - split_info->ring_buffer_first_scanline + 1 ) <= stbir_info->ring_buffer_num_entries ); + + // make sure there was room in the ring buffer when we add new scanlines + if ( ( split_info->ring_buffer_last_scanline - split_info->ring_buffer_first_scanline + 1 ) == stbir_info->ring_buffer_num_entries ) + { + split_info->ring_buffer_first_scanline++; + split_info->ring_buffer_begin_index++; + } + + if ( stbir_info->vertical_first ) + { + float * ring_buffer = stbir__get_ring_buffer_scanline( stbir_info, split_info, ++split_info->ring_buffer_last_scanline ); + // Decode the nth scanline from the source image into the decode buffer. + stbir__decode_scanline( stbir_info, split_info->ring_buffer_last_scanline, ring_buffer STBIR_ONLY_PROFILE_SET_SPLIT_INFO ); + } + else + { + stbir__decode_and_resample_for_vertical_gather_loop(stbir_info, split_info, split_info->ring_buffer_last_scanline + 1); + } + } + + // Now all buffers should be ready to write a row of vertical sampling, so do it. + stbir__resample_vertical_gather(stbir_info, split_info, y, in_first_scanline, in_last_scanline, vertical_coefficients ); + + ++vertical_contributors; + vertical_coefficients += stbir_info->vertical.coefficient_width; + } +} + +#define STBIR__FLOAT_EMPTY_MARKER 3.0e+38F +#define STBIR__FLOAT_BUFFER_IS_EMPTY(ptr) ((ptr)[0]==STBIR__FLOAT_EMPTY_MARKER) + +static void stbir__encode_first_scanline_from_scatter(stbir__info const * stbir_info, stbir__per_split_info* split_info) +{ + // evict a scanline out into the output buffer + float* ring_buffer_entry = stbir__get_ring_buffer_entry(stbir_info, split_info, split_info->ring_buffer_begin_index ); + + // dump the scanline out + stbir__encode_scanline( stbir_info, ( (char *)stbir_info->output_data ) + ( (size_t)split_info->ring_buffer_first_scanline * (size_t)stbir_info->output_stride_bytes ), ring_buffer_entry, split_info->ring_buffer_first_scanline STBIR_ONLY_PROFILE_SET_SPLIT_INFO ); + + // mark it as empty + ring_buffer_entry[ 0 ] = STBIR__FLOAT_EMPTY_MARKER; + + // advance the first scanline + split_info->ring_buffer_first_scanline++; + if ( ++split_info->ring_buffer_begin_index == stbir_info->ring_buffer_num_entries ) + split_info->ring_buffer_begin_index = 0; +} + +static void stbir__horizontal_resample_and_encode_first_scanline_from_scatter(stbir__info const * stbir_info, stbir__per_split_info* split_info) +{ + // evict a scanline out into the output buffer + + float* ring_buffer_entry = stbir__get_ring_buffer_entry(stbir_info, split_info, split_info->ring_buffer_begin_index ); + + // Now resample it into the buffer. + stbir__resample_horizontal_gather( stbir_info, split_info->vertical_buffer, ring_buffer_entry STBIR_ONLY_PROFILE_SET_SPLIT_INFO ); + + // dump the scanline out + stbir__encode_scanline( stbir_info, ( (char *)stbir_info->output_data ) + ( (size_t)split_info->ring_buffer_first_scanline * (size_t)stbir_info->output_stride_bytes ), split_info->vertical_buffer, split_info->ring_buffer_first_scanline STBIR_ONLY_PROFILE_SET_SPLIT_INFO ); + + // mark it as empty + ring_buffer_entry[ 0 ] = STBIR__FLOAT_EMPTY_MARKER; + + // advance the first scanline + split_info->ring_buffer_first_scanline++; + if ( ++split_info->ring_buffer_begin_index == stbir_info->ring_buffer_num_entries ) + split_info->ring_buffer_begin_index = 0; +} + +static void stbir__resample_vertical_scatter(stbir__info const * stbir_info, stbir__per_split_info* split_info, int n0, int n1, float const * vertical_coefficients, float const * vertical_buffer, float const * vertical_buffer_end ) +{ + STBIR_ASSERT( !stbir_info->vertical.is_gather ); + + STBIR_PROFILE_START( vertical ); + { + int k = 0, total = n1 - n0 + 1; + STBIR_ASSERT( total > 0 ); + do { + float * outputs[8]; + int i, n = total; if ( n > 8 ) n = 8; + for( i = 0 ; i < n ; i++ ) + { + outputs[ i ] = stbir__get_ring_buffer_scanline(stbir_info, split_info, k+i+n0 ); + if ( ( i ) && ( STBIR__FLOAT_BUFFER_IS_EMPTY( outputs[i] ) != STBIR__FLOAT_BUFFER_IS_EMPTY( outputs[0] ) ) ) // make sure runs are of the same type + { + n = i; + break; + } + } + // call the scatter to N scanlines at a time function (up to 8 scanlines of scattering at once) + ((STBIR__FLOAT_BUFFER_IS_EMPTY( outputs[0] ))?stbir__vertical_scatter_sets:stbir__vertical_scatter_blends)[n-1]( outputs, vertical_coefficients + k, vertical_buffer, vertical_buffer_end ); + k += n; + total -= n; + } while ( total ); + } + + STBIR_PROFILE_END( vertical ); +} + +typedef void stbir__handle_scanline_for_scatter_func(stbir__info const * stbir_info, stbir__per_split_info* split_info); + +static void stbir__vertical_scatter_loop( stbir__info const * stbir_info, stbir__per_split_info* split_info, int split_count ) +{ + int y, start_output_y, end_output_y, start_input_y, end_input_y; + stbir__contributors* vertical_contributors = stbir_info->vertical.contributors; + float const * vertical_coefficients = stbir_info->vertical.coefficients; + stbir__handle_scanline_for_scatter_func * handle_scanline_for_scatter; + void * scanline_scatter_buffer; + void * scanline_scatter_buffer_end; + int on_first_input_y, last_input_y; + int width = (stbir_info->vertical_first) ? ( stbir_info->scanline_extents.conservative.n1-stbir_info->scanline_extents.conservative.n0+1 ) : stbir_info->horizontal.scale_info.output_sub_size; + int width_times_channels = stbir_info->effective_channels * width; + + STBIR_ASSERT( !stbir_info->vertical.is_gather ); + + start_output_y = split_info->start_output_y; + end_output_y = split_info[split_count-1].end_output_y; // may do multiple split counts + + start_input_y = split_info->start_input_y; + end_input_y = split_info[split_count-1].end_input_y; + + // adjust for starting offset start_input_y + y = start_input_y + stbir_info->vertical.filter_pixel_margin; + vertical_contributors += y ; + vertical_coefficients += stbir_info->vertical.coefficient_width * y; + + if ( stbir_info->vertical_first ) + { + handle_scanline_for_scatter = stbir__horizontal_resample_and_encode_first_scanline_from_scatter; + scanline_scatter_buffer = split_info->decode_buffer; + scanline_scatter_buffer_end = ( (char*) scanline_scatter_buffer ) + sizeof( float ) * stbir_info->effective_channels * (stbir_info->scanline_extents.conservative.n1-stbir_info->scanline_extents.conservative.n0+1); + } + else + { + handle_scanline_for_scatter = stbir__encode_first_scanline_from_scatter; + scanline_scatter_buffer = split_info->vertical_buffer; + scanline_scatter_buffer_end = ( (char*) scanline_scatter_buffer ) + sizeof( float ) * stbir_info->effective_channels * stbir_info->horizontal.scale_info.output_sub_size; + } + + // initialize the ring buffer for scattering + split_info->ring_buffer_first_scanline = start_output_y; + split_info->ring_buffer_last_scanline = -1; + split_info->ring_buffer_begin_index = -1; + + // mark all the buffers as empty to start + for( y = 0 ; y < stbir_info->ring_buffer_num_entries ; y++ ) + { + float * decode_buffer = stbir__get_ring_buffer_entry( stbir_info, split_info, y ); + decode_buffer[ width_times_channels ] = 0.0f; // clear two over for horizontals with a remnant of 3 + decode_buffer[ width_times_channels+1 ] = 0.0f; + decode_buffer[0] = STBIR__FLOAT_EMPTY_MARKER; // only used on scatter + } + + // do the loop in input space + on_first_input_y = 1; last_input_y = start_input_y; + for (y = start_input_y ; y < end_input_y; y++) + { + int out_first_scanline, out_last_scanline; + + out_first_scanline = vertical_contributors->n0; + out_last_scanline = vertical_contributors->n1; + + STBIR_ASSERT(out_last_scanline - out_first_scanline + 1 <= stbir_info->ring_buffer_num_entries); + + if ( ( out_last_scanline >= out_first_scanline ) && ( ( ( out_first_scanline >= start_output_y ) && ( out_first_scanline < end_output_y ) ) || ( ( out_last_scanline >= start_output_y ) && ( out_last_scanline < end_output_y ) ) ) ) + { + float const * vc = vertical_coefficients; + + // keep track of the range actually seen for the next resize + last_input_y = y; + if ( ( on_first_input_y ) && ( y > start_input_y ) ) + split_info->start_input_y = y; + on_first_input_y = 0; + + // clip the region + if ( out_first_scanline < start_output_y ) + { + vc += start_output_y - out_first_scanline; + out_first_scanline = start_output_y; + } + + if ( out_last_scanline >= end_output_y ) + out_last_scanline = end_output_y - 1; + + // if very first scanline, init the index + if (split_info->ring_buffer_begin_index < 0) + split_info->ring_buffer_begin_index = out_first_scanline - start_output_y; + + STBIR_ASSERT( split_info->ring_buffer_begin_index <= out_first_scanline ); + + // Decode the nth scanline from the source image into the decode buffer. + stbir__decode_scanline( stbir_info, y, split_info->decode_buffer STBIR_ONLY_PROFILE_SET_SPLIT_INFO ); + + // When horizontal first, we resample horizontally into the vertical buffer before we scatter it out + if ( !stbir_info->vertical_first ) + stbir__resample_horizontal_gather( stbir_info, split_info->vertical_buffer, split_info->decode_buffer STBIR_ONLY_PROFILE_SET_SPLIT_INFO ); + + // Now it's sitting in the buffer ready to be distributed into the ring buffers. + + // evict from the ringbuffer, if we need are full + if ( ( ( split_info->ring_buffer_last_scanline - split_info->ring_buffer_first_scanline + 1 ) == stbir_info->ring_buffer_num_entries ) && + ( out_last_scanline > split_info->ring_buffer_last_scanline ) ) + handle_scanline_for_scatter( stbir_info, split_info ); + + // Now the horizontal buffer is ready to write to all ring buffer rows, so do it. + stbir__resample_vertical_scatter(stbir_info, split_info, out_first_scanline, out_last_scanline, vc, (float*)scanline_scatter_buffer, (float*)scanline_scatter_buffer_end ); + + // update the end of the buffer + if ( out_last_scanline > split_info->ring_buffer_last_scanline ) + split_info->ring_buffer_last_scanline = out_last_scanline; + } + ++vertical_contributors; + vertical_coefficients += stbir_info->vertical.coefficient_width; + } + + // now evict the scanlines that are left over in the ring buffer + while ( split_info->ring_buffer_first_scanline < end_output_y ) + handle_scanline_for_scatter(stbir_info, split_info); + + // update the end_input_y if we do multiple resizes with the same data + ++last_input_y; + for( y = 0 ; y < split_count; y++ ) + if ( split_info[y].end_input_y > last_input_y ) + split_info[y].end_input_y = last_input_y; +} + + +static stbir__kernel_callback * stbir__builtin_kernels[] = { 0, stbir__filter_trapezoid, stbir__filter_triangle, stbir__filter_cubic, stbir__filter_catmullrom, stbir__filter_mitchell, stbir__filter_point }; +static stbir__support_callback * stbir__builtin_supports[] = { 0, stbir__support_trapezoid, stbir__support_one, stbir__support_two, stbir__support_two, stbir__support_two, stbir__support_zeropoint5 }; + +static void stbir__set_sampler(stbir__sampler * samp, stbir_filter filter, stbir__kernel_callback * kernel, stbir__support_callback * support, stbir_edge edge, stbir__scale_info * scale_info, int always_gather, void * user_data ) +{ + // set filter + if (filter == 0) + { + filter = STBIR_DEFAULT_FILTER_DOWNSAMPLE; // default to downsample + if (scale_info->scale >= ( 1.0f - stbir__small_float ) ) + { + if ( (scale_info->scale <= ( 1.0f + stbir__small_float ) ) && ( STBIR_CEILF(scale_info->pixel_shift) == scale_info->pixel_shift ) ) + filter = STBIR_FILTER_POINT_SAMPLE; + else + filter = STBIR_DEFAULT_FILTER_UPSAMPLE; + } + } + samp->filter_enum = filter; + + STBIR_ASSERT(samp->filter_enum != 0); + STBIR_ASSERT((unsigned)samp->filter_enum < STBIR_FILTER_OTHER); + samp->filter_kernel = stbir__builtin_kernels[ filter ]; + samp->filter_support = stbir__builtin_supports[ filter ]; + + if ( kernel && support ) + { + samp->filter_kernel = kernel; + samp->filter_support = support; + samp->filter_enum = STBIR_FILTER_OTHER; + } + + samp->edge = edge; + samp->filter_pixel_width = stbir__get_filter_pixel_width (samp->filter_support, scale_info->scale, user_data ); + // Gather is always better, but in extreme downsamples, you have to most or all of the data in memory + // For horizontal, we always have all the pixels, so we always use gather here (always_gather==1). + // For vertical, we use gather if scaling up (which means we will have samp->filter_pixel_width + // scanlines in memory at once). + samp->is_gather = 0; + if ( scale_info->scale >= ( 1.0f - stbir__small_float ) ) + samp->is_gather = 1; + else if ( ( always_gather ) || ( samp->filter_pixel_width <= STBIR_FORCE_GATHER_FILTER_SCANLINES_AMOUNT ) ) + samp->is_gather = 2; + + // pre calculate stuff based on the above + samp->coefficient_width = stbir__get_coefficient_width(samp, samp->is_gather, user_data); + + // filter_pixel_width is the conservative size in pixels of input that affect an output pixel. + // In rare cases (only with 2 pix to 1 pix with the default filters), it's possible that the + // filter will extend before or after the scanline beyond just one extra entire copy of the + // scanline (we would hit the edge twice). We don't let you do that, so we clamp the total + // width to 3x the total of input pixel (once for the scanline, once for the left side + // overhang, and once for the right side). We only do this for edge mode, since the other + // modes can just re-edge clamp back in again. + if ( edge == STBIR_EDGE_WRAP ) + if ( samp->filter_pixel_width > ( scale_info->input_full_size * 3 ) ) + samp->filter_pixel_width = scale_info->input_full_size * 3; + + // This is how much to expand buffers to account for filters seeking outside + // the image boundaries. + samp->filter_pixel_margin = samp->filter_pixel_width / 2; + + // filter_pixel_margin is the amount that this filter can overhang on just one side of either + // end of the scanline (left or the right). Since we only allow you to overhang 1 scanline's + // worth of pixels, we clamp this one side of overhang to the input scanline size. Again, + // this clamping only happens in rare cases with the default filters (2 pix to 1 pix). + if ( edge == STBIR_EDGE_WRAP ) + if ( samp->filter_pixel_margin > scale_info->input_full_size ) + samp->filter_pixel_margin = scale_info->input_full_size; + + samp->num_contributors = stbir__get_contributors(samp, samp->is_gather); + + samp->contributors_size = samp->num_contributors * sizeof(stbir__contributors); + samp->coefficients_size = samp->num_contributors * samp->coefficient_width * sizeof(float) + sizeof(float)*STBIR_INPUT_CALLBACK_PADDING; // extra sizeof(float) is padding + + samp->gather_prescatter_contributors = 0; + samp->gather_prescatter_coefficients = 0; + if ( samp->is_gather == 0 ) + { + samp->gather_prescatter_coefficient_width = samp->filter_pixel_width; + samp->gather_prescatter_num_contributors = stbir__get_contributors(samp, 2); + samp->gather_prescatter_contributors_size = samp->gather_prescatter_num_contributors * sizeof(stbir__contributors); + samp->gather_prescatter_coefficients_size = samp->gather_prescatter_num_contributors * samp->gather_prescatter_coefficient_width * sizeof(float); + } +} + +static void stbir__get_conservative_extents( stbir__sampler * samp, stbir__contributors * range, void * user_data ) +{ + float scale = samp->scale_info.scale; + float out_shift = samp->scale_info.pixel_shift; + stbir__support_callback * support = samp->filter_support; + int input_full_size = samp->scale_info.input_full_size; + stbir_edge edge = samp->edge; + float inv_scale = samp->scale_info.inv_scale; + + STBIR_ASSERT( samp->is_gather != 0 ); + + if ( samp->is_gather == 1 ) + { + int in_first_pixel, in_last_pixel; + float out_filter_radius = support(inv_scale, user_data) * scale; + + stbir__calculate_in_pixel_range( &in_first_pixel, &in_last_pixel, 0.5, out_filter_radius, inv_scale, out_shift, input_full_size, edge ); + range->n0 = in_first_pixel; + stbir__calculate_in_pixel_range( &in_first_pixel, &in_last_pixel, ( (float)(samp->scale_info.output_sub_size-1) ) + 0.5f, out_filter_radius, inv_scale, out_shift, input_full_size, edge ); + range->n1 = in_last_pixel; + } + else if ( samp->is_gather == 2 ) // downsample gather, refine + { + float in_pixels_radius = support(scale, user_data) * inv_scale; + int filter_pixel_margin = samp->filter_pixel_margin; + int output_sub_size = samp->scale_info.output_sub_size; + int input_end; + int n; + int in_first_pixel, in_last_pixel; + + // get a conservative area of the input range + stbir__calculate_in_pixel_range( &in_first_pixel, &in_last_pixel, 0, 0, inv_scale, out_shift, input_full_size, edge ); + range->n0 = in_first_pixel; + stbir__calculate_in_pixel_range( &in_first_pixel, &in_last_pixel, (float)output_sub_size, 0, inv_scale, out_shift, input_full_size, edge ); + range->n1 = in_last_pixel; + + // now go through the margin to the start of area to find bottom + n = range->n0 + 1; + input_end = -filter_pixel_margin; + while( n >= input_end ) + { + int out_first_pixel, out_last_pixel; + stbir__calculate_out_pixel_range( &out_first_pixel, &out_last_pixel, ((float)n)+0.5f, in_pixels_radius, scale, out_shift, output_sub_size ); + if ( out_first_pixel > out_last_pixel ) + break; + + if ( ( out_first_pixel < output_sub_size ) || ( out_last_pixel >= 0 ) ) + range->n0 = n; + --n; + } + + // now go through the end of the area through the margin to find top + n = range->n1 - 1; + input_end = n + 1 + filter_pixel_margin; + while( n <= input_end ) + { + int out_first_pixel, out_last_pixel; + stbir__calculate_out_pixel_range( &out_first_pixel, &out_last_pixel, ((float)n)+0.5f, in_pixels_radius, scale, out_shift, output_sub_size ); + if ( out_first_pixel > out_last_pixel ) + break; + if ( ( out_first_pixel < output_sub_size ) || ( out_last_pixel >= 0 ) ) + range->n1 = n; + ++n; + } + } + + if ( samp->edge == STBIR_EDGE_WRAP ) + { + // if we are wrapping, and we are very close to the image size (so the edges might merge), just use the scanline up to the edge + if ( ( range->n0 > 0 ) && ( range->n1 >= input_full_size ) ) + { + int marg = range->n1 - input_full_size + 1; + if ( ( marg + STBIR__MERGE_RUNS_PIXEL_THRESHOLD ) >= range->n0 ) + range->n0 = 0; + } + if ( ( range->n0 < 0 ) && ( range->n1 < (input_full_size-1) ) ) + { + int marg = -range->n0; + if ( ( input_full_size - marg - STBIR__MERGE_RUNS_PIXEL_THRESHOLD - 1 ) <= range->n1 ) + range->n1 = input_full_size - 1; + } + } + else + { + // for non-edge-wrap modes, we never read over the edge, so clamp + if ( range->n0 < 0 ) + range->n0 = 0; + if ( range->n1 >= input_full_size ) + range->n1 = input_full_size - 1; + } +} + +static void stbir__get_split_info( stbir__per_split_info* split_info, int splits, int output_height, int vertical_pixel_margin, int input_full_height, int is_gather, stbir__contributors * contribs ) +{ + int i, cur; + int left = output_height; + + cur = 0; + for( i = 0 ; i < splits ; i++ ) + { + int each; + + split_info[i].start_output_y = cur; + each = left / ( splits - i ); + split_info[i].end_output_y = cur + each; + + // ok, when we are gathering, we need to make sure we are starting on a y offset that doesn't have + // a "special" set of coefficients. Basically, with exactly the right filter at exactly the right + // resize at exactly the right phase, some of the coefficents can be zero. When they are zero, we + // don't process them at all. But this leads to a tricky thing with the thread splits, where we + // might have a set of two coeffs like this for example: (4,4) and (3,6). The 4,4 means there was + // just one single coeff because things worked out perfectly (normally, they all have 4 coeffs + // like the range 3,6. The problem is that if we start right on the (4,4) on a brand new thread, + // then when we get to (3,6), we don't have the "3" sample in memory (because we didn't load + // it on the initial (4,4) range because it didn't have a 3 (we only add new samples that are + // larger than our existing samples - it's just how the eviction works). So, our solution here + // is pretty simple, if we start right on a range that has samples that start earlier, then we + // simply bump up our previous thread split range to include it, and then start this threads + // range with the smaller sample. It just moves one scanline from one thread split to another, + // so that we end with the unusual one, instead of start with it. To do this, we check 2-4 + // sample at each thread split start and then occassionally move them. + + if ( ( is_gather ) && ( i ) ) + { + stbir__contributors * small_contribs; + int j, smallest, stop, start_n0; + stbir__contributors * split_contribs = contribs + cur; + + // scan for a max of 3x the filter width or until the next thread split + stop = vertical_pixel_margin * 3; + if ( each < stop ) + stop = each; + + // loops a few times before early out + smallest = 0; + small_contribs = split_contribs; + start_n0 = small_contribs->n0; + for( j = 1 ; j <= stop ; j++ ) + { + ++split_contribs; + if ( split_contribs->n0 > start_n0 ) + break; + if ( split_contribs->n0 < small_contribs->n0 ) + { + small_contribs = split_contribs; + smallest = j; + } + } + + split_info[i-1].end_output_y += smallest; + split_info[i].start_output_y += smallest; + } + + cur += each; + left -= each; + + // scatter range (updated to minimum as you run it) + split_info[i].start_input_y = -vertical_pixel_margin; + split_info[i].end_input_y = input_full_height + vertical_pixel_margin; + } +} + +static void stbir__free_internal_mem( stbir__info *info ) +{ + #define STBIR__FREE_AND_CLEAR( ptr ) { if ( ptr ) { void * p = (ptr); (ptr) = 0; STBIR_FREE( p, info->user_data); } } + + if ( info ) + { + #ifndef STBIR__SEPARATE_ALLOCATIONS + STBIR__FREE_AND_CLEAR( info->alloced_mem ); + #else + int i,j; + + if ( ( info->vertical.gather_prescatter_contributors ) && ( (void*)info->vertical.gather_prescatter_contributors != (void*)info->split_info[0].decode_buffer ) ) + { + STBIR__FREE_AND_CLEAR( info->vertical.gather_prescatter_coefficients ); + STBIR__FREE_AND_CLEAR( info->vertical.gather_prescatter_contributors ); + } + for( i = 0 ; i < info->splits ; i++ ) + { + for( j = 0 ; j < info->alloc_ring_buffer_num_entries ; j++ ) + { + #ifdef STBIR_SIMD8 + if ( info->effective_channels == 3 ) + --info->split_info[i].ring_buffers[j]; // avx in 3 channel mode needs one float at the start of the buffer + #endif + STBIR__FREE_AND_CLEAR( info->split_info[i].ring_buffers[j] ); + } + + #ifdef STBIR_SIMD8 + if ( info->effective_channels == 3 ) + --info->split_info[i].decode_buffer; // avx in 3 channel mode needs one float at the start of the buffer + #endif + STBIR__FREE_AND_CLEAR( info->split_info[i].decode_buffer ); + STBIR__FREE_AND_CLEAR( info->split_info[i].ring_buffers ); + STBIR__FREE_AND_CLEAR( info->split_info[i].vertical_buffer ); + } + STBIR__FREE_AND_CLEAR( info->split_info ); + if ( info->vertical.coefficients != info->horizontal.coefficients ) + { + STBIR__FREE_AND_CLEAR( info->vertical.coefficients ); + STBIR__FREE_AND_CLEAR( info->vertical.contributors ); + } + STBIR__FREE_AND_CLEAR( info->horizontal.coefficients ); + STBIR__FREE_AND_CLEAR( info->horizontal.contributors ); + STBIR__FREE_AND_CLEAR( info->alloced_mem ); + STBIR_FREE( info, info->user_data ); + #endif + } + + #undef STBIR__FREE_AND_CLEAR +} + +static int stbir__get_max_split( int splits, int height ) +{ + int i; + int max = 0; + + for( i = 0 ; i < splits ; i++ ) + { + int each = height / ( splits - i ); + if ( each > max ) + max = each; + height -= each; + } + return max; +} + +static stbir__horizontal_gather_channels_func ** stbir__horizontal_gather_n_coeffs_funcs[8] = +{ + 0, stbir__horizontal_gather_1_channels_with_n_coeffs_funcs, stbir__horizontal_gather_2_channels_with_n_coeffs_funcs, stbir__horizontal_gather_3_channels_with_n_coeffs_funcs, stbir__horizontal_gather_4_channels_with_n_coeffs_funcs, 0,0, stbir__horizontal_gather_7_channels_with_n_coeffs_funcs +}; + +static stbir__horizontal_gather_channels_func ** stbir__horizontal_gather_channels_funcs[8] = +{ + 0, stbir__horizontal_gather_1_channels_funcs, stbir__horizontal_gather_2_channels_funcs, stbir__horizontal_gather_3_channels_funcs, stbir__horizontal_gather_4_channels_funcs, 0,0, stbir__horizontal_gather_7_channels_funcs +}; + +// there are six resize classifications: 0 == vertical scatter, 1 == vertical gather < 1x scale, 2 == vertical gather 1x-2x scale, 4 == vertical gather < 3x scale, 4 == vertical gather > 3x scale, 5 == <=4 pixel height, 6 == <=4 pixel wide column +#define STBIR_RESIZE_CLASSIFICATIONS 8 + +static float stbir__compute_weights[5][STBIR_RESIZE_CLASSIFICATIONS][4]= // 5 = 0=1chan, 1=2chan, 2=3chan, 3=4chan, 4=7chan +{ + { + { 1.00000f, 1.00000f, 0.31250f, 1.00000f }, + { 0.56250f, 0.59375f, 0.00000f, 0.96875f }, + { 1.00000f, 0.06250f, 0.00000f, 1.00000f }, + { 0.00000f, 0.09375f, 1.00000f, 1.00000f }, + { 1.00000f, 1.00000f, 0.31250f, 1.00000f }, + { 0.03125f, 0.12500f, 1.00000f, 1.00000f }, + { 1.00000f, 1.00000f, 0.06250f, 1.00000f }, + { 0.00000f, 1.00000f, 0.00000f, 0.03125f }, + }, { + { 0.00000f, 0.84375f, 0.00000f, 0.03125f }, + { 0.09375f, 0.93750f, 0.00000f, 0.78125f }, + { 0.87500f, 0.21875f, 0.00000f, 0.96875f }, + { 0.09375f, 0.09375f, 1.00000f, 1.00000f }, + { 0.00000f, 0.84375f, 0.00000f, 0.03125f }, + { 0.03125f, 0.12500f, 1.00000f, 1.00000f }, + { 1.00000f, 1.00000f, 0.06250f, 1.00000f }, + { 0.00000f, 1.00000f, 0.00000f, 0.53125f }, + }, { + { 0.00000f, 0.53125f, 0.00000f, 0.03125f }, + { 0.06250f, 0.96875f, 0.00000f, 0.53125f }, + { 0.87500f, 0.18750f, 0.00000f, 0.93750f }, + { 0.00000f, 0.09375f, 1.00000f, 1.00000f }, + { 0.00000f, 0.53125f, 0.00000f, 0.03125f }, + { 0.03125f, 0.12500f, 1.00000f, 1.00000f }, + { 1.00000f, 1.00000f, 0.06250f, 1.00000f }, + { 0.00000f, 1.00000f, 0.00000f, 0.56250f }, + }, { + { 0.00000f, 0.50000f, 0.00000f, 0.71875f }, + { 0.06250f, 0.84375f, 0.00000f, 0.87500f }, + { 1.00000f, 0.50000f, 0.50000f, 0.96875f }, + { 1.00000f, 0.09375f, 0.31250f, 0.50000f }, + { 0.00000f, 0.50000f, 0.00000f, 0.71875f }, + { 1.00000f, 0.03125f, 0.03125f, 0.53125f }, + { 1.00000f, 1.00000f, 0.06250f, 1.00000f }, + { 0.00000f, 1.00000f, 0.03125f, 0.18750f }, + }, { + { 0.00000f, 0.59375f, 0.00000f, 0.96875f }, + { 0.06250f, 0.81250f, 0.06250f, 0.59375f }, + { 0.75000f, 0.43750f, 0.12500f, 0.96875f }, + { 0.87500f, 0.06250f, 0.18750f, 0.43750f }, + { 0.00000f, 0.59375f, 0.00000f, 0.96875f }, + { 0.15625f, 0.12500f, 1.00000f, 1.00000f }, + { 1.00000f, 1.00000f, 0.06250f, 1.00000f }, + { 0.00000f, 1.00000f, 0.03125f, 0.34375f }, + } +}; + +// structure that allow us to query and override info for training the costs +typedef struct STBIR__V_FIRST_INFO +{ + double v_cost, h_cost; + int control_v_first; // 0 = no control, 1 = force hori, 2 = force vert + int v_first; + int v_resize_classification; + int is_gather; +} STBIR__V_FIRST_INFO; + +#ifdef STBIR__V_FIRST_INFO_BUFFER +static STBIR__V_FIRST_INFO STBIR__V_FIRST_INFO_BUFFER = {0}; +#define STBIR__V_FIRST_INFO_POINTER &STBIR__V_FIRST_INFO_BUFFER +#else +#define STBIR__V_FIRST_INFO_POINTER 0 +#endif + +// Figure out whether to scale along the horizontal or vertical first. +// This only *super* important when you are scaling by a massively +// different amount in the vertical vs the horizontal (for example, if +// you are scaling by 2x in the width, and 0.5x in the height, then you +// want to do the vertical scale first, because it's around 3x faster +// in that order. +// +// In more normal circumstances, this makes a 20-40% differences, so +// it's good to get right, but not critical. The normal way that you +// decide which direction goes first is just figuring out which +// direction does more multiplies. But with modern CPUs with their +// fancy caches and SIMD and high IPC abilities, so there's just a lot +// more that goes into it. +// +// My handwavy sort of solution is to have an app that does a whole +// bunch of timing for both vertical and horizontal first modes, +// and then another app that can read lots of these timing files +// and try to search for the best weights to use. Dotimings.c +// is the app that does a bunch of timings, and vf_train.c is the +// app that solves for the best weights (and shows how well it +// does currently). + +static int stbir__should_do_vertical_first( float weights_table[STBIR_RESIZE_CLASSIFICATIONS][4], int horizontal_filter_pixel_width, float horizontal_scale, int horizontal_output_size, int vertical_filter_pixel_width, float vertical_scale, int vertical_output_size, int is_gather, STBIR__V_FIRST_INFO * info ) +{ + double v_cost, h_cost; + float * weights; + int vertical_first; + int v_classification; + + // categorize the resize into buckets + if ( ( vertical_output_size <= 4 ) || ( horizontal_output_size <= 4 ) ) + v_classification = ( vertical_output_size < horizontal_output_size ) ? 6 : 7; + else if ( ( !is_gather ) && ( ( vertical_output_size <= 16 ) || ( horizontal_output_size <= 16 ) ) ) + v_classification = 4; + else if ( vertical_scale <= 1.0f ) + v_classification = ( is_gather ) ? 1 : 0; + else if ( vertical_scale <= 2.0f) + v_classification = 2; + else if ( vertical_scale <= 3.0f) + v_classification = 3; + else + v_classification = 5; // everything bigger than 3x + + // use the right weights + weights = weights_table[ v_classification ]; + + // this is the costs when you don't take into account modern CPUs with high ipc and simd and caches - wish we had a better estimate + h_cost = (float)horizontal_filter_pixel_width * weights[0] + horizontal_scale * (float)vertical_filter_pixel_width * weights[1]; + v_cost = (float)vertical_filter_pixel_width * weights[2] + vertical_scale * (float)horizontal_filter_pixel_width * weights[3]; + + // use computation estimate to decide vertical first or not + vertical_first = ( v_cost <= h_cost ) ? 1 : 0; + + // save these, if requested + if ( info ) + { + info->h_cost = h_cost; + info->v_cost = v_cost; + info->v_resize_classification = v_classification; + info->v_first = vertical_first; + info->is_gather = is_gather; + } + + // and this allows us to override everything for testing (see dotiming.c) + if ( ( info ) && ( info->control_v_first ) ) + vertical_first = ( info->control_v_first == 2 ) ? 1 : 0; + + return vertical_first; +} + +// layout lookups - must match stbir_internal_pixel_layout +static unsigned char stbir__pixel_channels[] = { + 1,2,3,3,4, // 1ch, 2ch, rgb, bgr, 4ch + 4,4,4,4,2,2, // RGBA,BGRA,ARGB,ABGR,RA,AR + 4,4,4,4,2,2, // RGBA_PM,BGRA_PM,ARGB_PM,ABGR_PM,RA_PM,AR_PM +}; + +// the internal pixel layout enums are in a different order, so we can easily do range comparisons of types +// the public pixel layout is ordered in a way that if you cast num_channels (1-4) to the enum, you get something sensible +static stbir_internal_pixel_layout stbir__pixel_layout_convert_public_to_internal[] = { + STBIRI_BGR, STBIRI_1CHANNEL, STBIRI_2CHANNEL, STBIRI_RGB, STBIRI_RGBA, + STBIRI_4CHANNEL, STBIRI_BGRA, STBIRI_ARGB, STBIRI_ABGR, STBIRI_RA, STBIRI_AR, + STBIRI_RGBA_PM, STBIRI_BGRA_PM, STBIRI_ARGB_PM, STBIRI_ABGR_PM, STBIRI_RA_PM, STBIRI_AR_PM, +}; + +static stbir__info * stbir__alloc_internal_mem_and_build_samplers( stbir__sampler * horizontal, stbir__sampler * vertical, stbir__contributors * conservative, stbir_pixel_layout input_pixel_layout_public, stbir_pixel_layout output_pixel_layout_public, int splits, int new_x, int new_y, int fast_alpha, void * user_data STBIR_ONLY_PROFILE_BUILD_GET_INFO ) +{ + static char stbir_channel_count_index[8]={ 9,0,1,2, 3,9,9,4 }; + + stbir__info * info = 0; + void * alloced = 0; + size_t alloced_total = 0; + int vertical_first; + size_t decode_buffer_size, ring_buffer_length_bytes, ring_buffer_size, vertical_buffer_size; + int alloc_ring_buffer_num_entries; + + int alpha_weighting_type = 0; // 0=none, 1=simple, 2=fancy + int conservative_split_output_size = stbir__get_max_split( splits, vertical->scale_info.output_sub_size ); + stbir_internal_pixel_layout input_pixel_layout = stbir__pixel_layout_convert_public_to_internal[ input_pixel_layout_public ]; + stbir_internal_pixel_layout output_pixel_layout = stbir__pixel_layout_convert_public_to_internal[ output_pixel_layout_public ]; + int channels = stbir__pixel_channels[ input_pixel_layout ]; + int effective_channels = channels; + + // first figure out what type of alpha weighting to use (if any) + if ( ( horizontal->filter_enum != STBIR_FILTER_POINT_SAMPLE ) || ( vertical->filter_enum != STBIR_FILTER_POINT_SAMPLE ) ) // no alpha weighting on point sampling + { + if ( ( input_pixel_layout >= STBIRI_RGBA ) && ( input_pixel_layout <= STBIRI_AR ) && ( output_pixel_layout >= STBIRI_RGBA ) && ( output_pixel_layout <= STBIRI_AR ) ) + { + if ( fast_alpha ) + { + alpha_weighting_type = 4; + } + else + { + static int fancy_alpha_effective_cnts[6] = { 7, 7, 7, 7, 3, 3 }; + alpha_weighting_type = 2; + effective_channels = fancy_alpha_effective_cnts[ input_pixel_layout - STBIRI_RGBA ]; + } + } + else if ( ( input_pixel_layout >= STBIRI_RGBA_PM ) && ( input_pixel_layout <= STBIRI_AR_PM ) && ( output_pixel_layout >= STBIRI_RGBA ) && ( output_pixel_layout <= STBIRI_AR ) ) + { + // input premult, output non-premult + alpha_weighting_type = 3; + } + else if ( ( input_pixel_layout >= STBIRI_RGBA ) && ( input_pixel_layout <= STBIRI_AR ) && ( output_pixel_layout >= STBIRI_RGBA_PM ) && ( output_pixel_layout <= STBIRI_AR_PM ) ) + { + // input non-premult, output premult + alpha_weighting_type = 1; + } + } + + // channel in and out count must match currently + if ( channels != stbir__pixel_channels[ output_pixel_layout ] ) + return 0; + + // get vertical first + vertical_first = stbir__should_do_vertical_first( stbir__compute_weights[ (int)stbir_channel_count_index[ effective_channels ] ], horizontal->filter_pixel_width, horizontal->scale_info.scale, horizontal->scale_info.output_sub_size, vertical->filter_pixel_width, vertical->scale_info.scale, vertical->scale_info.output_sub_size, vertical->is_gather, STBIR__V_FIRST_INFO_POINTER ); + + // sometimes read one float off in some of the unrolled loops (with a weight of zero coeff, so it doesn't have an effect) + // we use a few extra floats instead of just 1, so that input callback buffer can overlap with the decode buffer without + // the conversion routines overwriting the callback input data. + decode_buffer_size = ( conservative->n1 - conservative->n0 + 1 ) * effective_channels * sizeof(float) + sizeof(float)*STBIR_INPUT_CALLBACK_PADDING; // extra floats for input callback stagger + +#if defined( STBIR__SEPARATE_ALLOCATIONS ) && defined(STBIR_SIMD8) + if ( effective_channels == 3 ) + decode_buffer_size += sizeof(float); // avx in 3 channel mode needs one float at the start of the buffer (only with separate allocations) +#endif + + ring_buffer_length_bytes = (size_t)horizontal->scale_info.output_sub_size * (size_t)effective_channels * sizeof(float) + sizeof(float)*STBIR_INPUT_CALLBACK_PADDING; // extra floats for padding + + // if we do vertical first, the ring buffer holds a whole decoded line + if ( vertical_first ) + ring_buffer_length_bytes = ( decode_buffer_size + 15 ) & ~15; + + if ( ( ring_buffer_length_bytes & 4095 ) == 0 ) ring_buffer_length_bytes += 64*3; // avoid 4k alias + + // One extra entry because floating point precision problems sometimes cause an extra to be necessary. + alloc_ring_buffer_num_entries = vertical->filter_pixel_width + 1; + + // we never need more ring buffer entries than the scanlines we're outputting when in scatter mode + if ( ( !vertical->is_gather ) && ( alloc_ring_buffer_num_entries > conservative_split_output_size ) ) + alloc_ring_buffer_num_entries = conservative_split_output_size; + + ring_buffer_size = (size_t)alloc_ring_buffer_num_entries * (size_t)ring_buffer_length_bytes; + + // The vertical buffer is used differently, depending on whether we are scattering + // the vertical scanlines, or gathering them. + // If scattering, it's used at the temp buffer to accumulate each output. + // If gathering, it's just the output buffer. + vertical_buffer_size = (size_t)horizontal->scale_info.output_sub_size * (size_t)effective_channels * sizeof(float) + sizeof(float); // extra float for padding + + // we make two passes through this loop, 1st to add everything up, 2nd to allocate and init + for(;;) + { + int i; + void * advance_mem = alloced; + int copy_horizontal = 0; + stbir__sampler * possibly_use_horizontal_for_pivot = 0; + +#ifdef STBIR__SEPARATE_ALLOCATIONS + #define STBIR__NEXT_PTR( ptr, size, ntype ) if ( alloced ) { void * p = STBIR_MALLOC( size, user_data); if ( p == 0 ) { stbir__free_internal_mem( info ); return 0; } (ptr) = (ntype*)p; } +#else + #define STBIR__NEXT_PTR( ptr, size, ntype ) advance_mem = (void*) ( ( ((size_t)advance_mem) + 15 ) & ~15 ); if ( alloced ) ptr = (ntype*)advance_mem; advance_mem = (char*)(((size_t)advance_mem) + (size)); +#endif + + STBIR__NEXT_PTR( info, sizeof( stbir__info ), stbir__info ); + + STBIR__NEXT_PTR( info->split_info, sizeof( stbir__per_split_info ) * splits, stbir__per_split_info ); + + if ( info ) + { + static stbir__alpha_weight_func * fancy_alpha_weights[6] = { stbir__fancy_alpha_weight_4ch, stbir__fancy_alpha_weight_4ch, stbir__fancy_alpha_weight_4ch, stbir__fancy_alpha_weight_4ch, stbir__fancy_alpha_weight_2ch, stbir__fancy_alpha_weight_2ch }; + static stbir__alpha_unweight_func * fancy_alpha_unweights[6] = { stbir__fancy_alpha_unweight_4ch, stbir__fancy_alpha_unweight_4ch, stbir__fancy_alpha_unweight_4ch, stbir__fancy_alpha_unweight_4ch, stbir__fancy_alpha_unweight_2ch, stbir__fancy_alpha_unweight_2ch }; + static stbir__alpha_weight_func * simple_alpha_weights[6] = { stbir__simple_alpha_weight_4ch, stbir__simple_alpha_weight_4ch, stbir__simple_alpha_weight_4ch, stbir__simple_alpha_weight_4ch, stbir__simple_alpha_weight_2ch, stbir__simple_alpha_weight_2ch }; + static stbir__alpha_unweight_func * simple_alpha_unweights[6] = { stbir__simple_alpha_unweight_4ch, stbir__simple_alpha_unweight_4ch, stbir__simple_alpha_unweight_4ch, stbir__simple_alpha_unweight_4ch, stbir__simple_alpha_unweight_2ch, stbir__simple_alpha_unweight_2ch }; + + // initialize info fields + info->alloced_mem = alloced; + info->alloced_total = alloced_total; + + info->channels = channels; + info->effective_channels = effective_channels; + + info->offset_x = new_x; + info->offset_y = new_y; + info->alloc_ring_buffer_num_entries = (int)alloc_ring_buffer_num_entries; + info->ring_buffer_num_entries = 0; + info->ring_buffer_length_bytes = (int)ring_buffer_length_bytes; + info->splits = splits; + info->vertical_first = vertical_first; + + info->input_pixel_layout_internal = input_pixel_layout; + info->output_pixel_layout_internal = output_pixel_layout; + + // setup alpha weight functions + info->alpha_weight = 0; + info->alpha_unweight = 0; + + // handle alpha weighting functions and overrides + if ( alpha_weighting_type == 2 ) + { + // high quality alpha multiplying on the way in, dividing on the way out + info->alpha_weight = fancy_alpha_weights[ input_pixel_layout - STBIRI_RGBA ]; + info->alpha_unweight = fancy_alpha_unweights[ output_pixel_layout - STBIRI_RGBA ]; + } + else if ( alpha_weighting_type == 4 ) + { + // fast alpha multiplying on the way in, dividing on the way out + info->alpha_weight = simple_alpha_weights[ input_pixel_layout - STBIRI_RGBA ]; + info->alpha_unweight = simple_alpha_unweights[ output_pixel_layout - STBIRI_RGBA ]; + } + else if ( alpha_weighting_type == 1 ) + { + // fast alpha on the way in, leave in premultiplied form on way out + info->alpha_weight = simple_alpha_weights[ input_pixel_layout - STBIRI_RGBA ]; + } + else if ( alpha_weighting_type == 3 ) + { + // incoming is premultiplied, fast alpha dividing on the way out - non-premultiplied output + info->alpha_unweight = simple_alpha_unweights[ output_pixel_layout - STBIRI_RGBA ]; + } + + // handle 3-chan color flipping, using the alpha weight path + if ( ( ( input_pixel_layout == STBIRI_RGB ) && ( output_pixel_layout == STBIRI_BGR ) ) || + ( ( input_pixel_layout == STBIRI_BGR ) && ( output_pixel_layout == STBIRI_RGB ) ) ) + { + // do the flipping on the smaller of the two ends + if ( horizontal->scale_info.scale < 1.0f ) + info->alpha_unweight = stbir__simple_flip_3ch; + else + info->alpha_weight = stbir__simple_flip_3ch; + } + + } + + // get all the per-split buffers + for( i = 0 ; i < splits ; i++ ) + { + STBIR__NEXT_PTR( info->split_info[i].decode_buffer, decode_buffer_size, float ); + +#ifdef STBIR__SEPARATE_ALLOCATIONS + + #ifdef STBIR_SIMD8 + if ( ( info ) && ( effective_channels == 3 ) ) + ++info->split_info[i].decode_buffer; // avx in 3 channel mode needs one float at the start of the buffer + #endif + + STBIR__NEXT_PTR( info->split_info[i].ring_buffers, alloc_ring_buffer_num_entries * sizeof(float*), float* ); + { + int j; + for( j = 0 ; j < alloc_ring_buffer_num_entries ; j++ ) + { + STBIR__NEXT_PTR( info->split_info[i].ring_buffers[j], ring_buffer_length_bytes, float ); + #ifdef STBIR_SIMD8 + if ( ( info ) && ( effective_channels == 3 ) ) + ++info->split_info[i].ring_buffers[j]; // avx in 3 channel mode needs one float at the start of the buffer + #endif + } + } +#else + STBIR__NEXT_PTR( info->split_info[i].ring_buffer, ring_buffer_size, float ); +#endif + STBIR__NEXT_PTR( info->split_info[i].vertical_buffer, vertical_buffer_size, float ); + } + + // alloc memory for to-be-pivoted coeffs (if necessary) + if ( vertical->is_gather == 0 ) + { + size_t both; + size_t temp_mem_amt; + + // when in vertical scatter mode, we first build the coefficients in gather mode, and then pivot after, + // that means we need two buffers, so we try to use the decode buffer and ring buffer for this. if that + // is too small, we just allocate extra memory to use as this temp. + + both = (size_t)vertical->gather_prescatter_contributors_size + (size_t)vertical->gather_prescatter_coefficients_size; + +#ifdef STBIR__SEPARATE_ALLOCATIONS + temp_mem_amt = decode_buffer_size; + + #ifdef STBIR_SIMD8 + if ( effective_channels == 3 ) + --temp_mem_amt; // avx in 3 channel mode needs one float at the start of the buffer + #endif +#else + temp_mem_amt = (size_t)( decode_buffer_size + ring_buffer_size + vertical_buffer_size ) * (size_t)splits; +#endif + if ( temp_mem_amt >= both ) + { + if ( info ) + { + vertical->gather_prescatter_contributors = (stbir__contributors*)info->split_info[0].decode_buffer; + vertical->gather_prescatter_coefficients = (float*) ( ( (char*)info->split_info[0].decode_buffer ) + vertical->gather_prescatter_contributors_size ); + } + } + else + { + // ring+decode memory is too small, so allocate temp memory + STBIR__NEXT_PTR( vertical->gather_prescatter_contributors, vertical->gather_prescatter_contributors_size, stbir__contributors ); + STBIR__NEXT_PTR( vertical->gather_prescatter_coefficients, vertical->gather_prescatter_coefficients_size, float ); + } + } + + STBIR__NEXT_PTR( horizontal->contributors, horizontal->contributors_size, stbir__contributors ); + STBIR__NEXT_PTR( horizontal->coefficients, horizontal->coefficients_size, float ); + + // are the two filters identical?? (happens a lot with mipmap generation) + if ( ( horizontal->filter_kernel == vertical->filter_kernel ) && ( horizontal->filter_support == vertical->filter_support ) && ( horizontal->edge == vertical->edge ) && ( horizontal->scale_info.output_sub_size == vertical->scale_info.output_sub_size ) ) + { + float diff_scale = horizontal->scale_info.scale - vertical->scale_info.scale; + float diff_shift = horizontal->scale_info.pixel_shift - vertical->scale_info.pixel_shift; + if ( diff_scale < 0.0f ) diff_scale = -diff_scale; + if ( diff_shift < 0.0f ) diff_shift = -diff_shift; + if ( ( diff_scale <= stbir__small_float ) && ( diff_shift <= stbir__small_float ) ) + { + if ( horizontal->is_gather == vertical->is_gather ) + { + copy_horizontal = 1; + goto no_vert_alloc; + } + // everything matches, but vertical is scatter, horizontal is gather, use horizontal coeffs for vertical pivot coeffs + possibly_use_horizontal_for_pivot = horizontal; + } + } + + STBIR__NEXT_PTR( vertical->contributors, vertical->contributors_size, stbir__contributors ); + STBIR__NEXT_PTR( vertical->coefficients, vertical->coefficients_size, float ); + + no_vert_alloc: + + if ( info ) + { + STBIR_PROFILE_BUILD_START( horizontal ); + + stbir__calculate_filters( horizontal, 0, user_data STBIR_ONLY_PROFILE_BUILD_SET_INFO ); + + // setup the horizontal gather functions + // start with defaulting to the n_coeffs functions (specialized on channels and remnant leftover) + info->horizontal_gather_channels = stbir__horizontal_gather_n_coeffs_funcs[ effective_channels ][ horizontal->extent_info.widest & 3 ]; + // but if the number of coeffs <= 12, use another set of special cases. <=12 coeffs is any enlarging resize, or shrinking resize down to about 1/3 size + if ( horizontal->extent_info.widest <= 12 ) + info->horizontal_gather_channels = stbir__horizontal_gather_channels_funcs[ effective_channels ][ horizontal->extent_info.widest - 1 ]; + + info->scanline_extents.conservative.n0 = conservative->n0; + info->scanline_extents.conservative.n1 = conservative->n1; + + // get exact extents + stbir__get_extents( horizontal, &info->scanline_extents ); + + // pack the horizontal coeffs + horizontal->coefficient_width = stbir__pack_coefficients(horizontal->num_contributors, horizontal->contributors, horizontal->coefficients, horizontal->coefficient_width, horizontal->extent_info.widest, info->scanline_extents.conservative.n0, info->scanline_extents.conservative.n1 ); + + STBIR_MEMCPY( &info->horizontal, horizontal, sizeof( stbir__sampler ) ); + + STBIR_PROFILE_BUILD_END( horizontal ); + + if ( copy_horizontal ) + { + STBIR_MEMCPY( &info->vertical, horizontal, sizeof( stbir__sampler ) ); + } + else + { + STBIR_PROFILE_BUILD_START( vertical ); + + stbir__calculate_filters( vertical, possibly_use_horizontal_for_pivot, user_data STBIR_ONLY_PROFILE_BUILD_SET_INFO ); + STBIR_MEMCPY( &info->vertical, vertical, sizeof( stbir__sampler ) ); + + STBIR_PROFILE_BUILD_END( vertical ); + } + + // setup the vertical split ranges + stbir__get_split_info( info->split_info, info->splits, info->vertical.scale_info.output_sub_size, info->vertical.filter_pixel_margin, info->vertical.scale_info.input_full_size, info->vertical.is_gather, info->vertical.contributors ); + + // now we know precisely how many entries we need + info->ring_buffer_num_entries = info->vertical.extent_info.widest; + + // we never need more ring buffer entries than the scanlines we're outputting + if ( ( !info->vertical.is_gather ) && ( info->ring_buffer_num_entries > conservative_split_output_size ) ) + info->ring_buffer_num_entries = conservative_split_output_size; + STBIR_ASSERT( info->ring_buffer_num_entries <= info->alloc_ring_buffer_num_entries ); + } + #undef STBIR__NEXT_PTR + + + // is this the first time through loop? + if ( info == 0 ) + { + alloced_total = ( 15 + (size_t)advance_mem ); + alloced = STBIR_MALLOC( alloced_total, user_data ); + if ( alloced == 0 ) + return 0; + } + else + return info; // success + } +} + +static int stbir__perform_resize( stbir__info const * info, int split_start, int split_count ) +{ + stbir__per_split_info * split_info = info->split_info + split_start; + + STBIR_PROFILE_CLEAR_EXTRAS(); + + STBIR_PROFILE_FIRST_START( looping ); + if (info->vertical.is_gather) + stbir__vertical_gather_loop( info, split_info, split_count ); + else + stbir__vertical_scatter_loop( info, split_info, split_count ); + STBIR_PROFILE_END( looping ); + + return 1; +} + +static void stbir__update_info_from_resize( stbir__info * info, STBIR_RESIZE * resize ) +{ + static stbir__decode_pixels_func * decode_simple[STBIR_TYPE_HALF_FLOAT-STBIR_TYPE_UINT8_SRGB+1]= + { + /* 1ch-4ch */ stbir__decode_uint8_srgb, stbir__decode_uint8_srgb, 0, stbir__decode_float_linear, stbir__decode_half_float_linear, + }; + + static stbir__decode_pixels_func * decode_alphas[STBIRI_AR-STBIRI_RGBA+1][STBIR_TYPE_HALF_FLOAT-STBIR_TYPE_UINT8_SRGB+1]= + { + { /* RGBA */ stbir__decode_uint8_srgb4_linearalpha, stbir__decode_uint8_srgb, 0, stbir__decode_float_linear, stbir__decode_half_float_linear }, + { /* BGRA */ stbir__decode_uint8_srgb4_linearalpha_BGRA, stbir__decode_uint8_srgb_BGRA, 0, stbir__decode_float_linear_BGRA, stbir__decode_half_float_linear_BGRA }, + { /* ARGB */ stbir__decode_uint8_srgb4_linearalpha_ARGB, stbir__decode_uint8_srgb_ARGB, 0, stbir__decode_float_linear_ARGB, stbir__decode_half_float_linear_ARGB }, + { /* ABGR */ stbir__decode_uint8_srgb4_linearalpha_ABGR, stbir__decode_uint8_srgb_ABGR, 0, stbir__decode_float_linear_ABGR, stbir__decode_half_float_linear_ABGR }, + { /* RA */ stbir__decode_uint8_srgb2_linearalpha, stbir__decode_uint8_srgb, 0, stbir__decode_float_linear, stbir__decode_half_float_linear }, + { /* AR */ stbir__decode_uint8_srgb2_linearalpha_AR, stbir__decode_uint8_srgb_AR, 0, stbir__decode_float_linear_AR, stbir__decode_half_float_linear_AR }, + }; + + static stbir__decode_pixels_func * decode_simple_scaled_or_not[2][2]= + { + { stbir__decode_uint8_linear_scaled, stbir__decode_uint8_linear }, { stbir__decode_uint16_linear_scaled, stbir__decode_uint16_linear }, + }; + + static stbir__decode_pixels_func * decode_alphas_scaled_or_not[STBIRI_AR-STBIRI_RGBA+1][2][2]= + { + { /* RGBA */ { stbir__decode_uint8_linear_scaled, stbir__decode_uint8_linear }, { stbir__decode_uint16_linear_scaled, stbir__decode_uint16_linear } }, + { /* BGRA */ { stbir__decode_uint8_linear_scaled_BGRA, stbir__decode_uint8_linear_BGRA }, { stbir__decode_uint16_linear_scaled_BGRA, stbir__decode_uint16_linear_BGRA } }, + { /* ARGB */ { stbir__decode_uint8_linear_scaled_ARGB, stbir__decode_uint8_linear_ARGB }, { stbir__decode_uint16_linear_scaled_ARGB, stbir__decode_uint16_linear_ARGB } }, + { /* ABGR */ { stbir__decode_uint8_linear_scaled_ABGR, stbir__decode_uint8_linear_ABGR }, { stbir__decode_uint16_linear_scaled_ABGR, stbir__decode_uint16_linear_ABGR } }, + { /* RA */ { stbir__decode_uint8_linear_scaled, stbir__decode_uint8_linear }, { stbir__decode_uint16_linear_scaled, stbir__decode_uint16_linear } }, + { /* AR */ { stbir__decode_uint8_linear_scaled_AR, stbir__decode_uint8_linear_AR }, { stbir__decode_uint16_linear_scaled_AR, stbir__decode_uint16_linear_AR } } + }; + + static stbir__encode_pixels_func * encode_simple[STBIR_TYPE_HALF_FLOAT-STBIR_TYPE_UINT8_SRGB+1]= + { + /* 1ch-4ch */ stbir__encode_uint8_srgb, stbir__encode_uint8_srgb, 0, stbir__encode_float_linear, stbir__encode_half_float_linear, + }; + + static stbir__encode_pixels_func * encode_alphas[STBIRI_AR-STBIRI_RGBA+1][STBIR_TYPE_HALF_FLOAT-STBIR_TYPE_UINT8_SRGB+1]= + { + { /* RGBA */ stbir__encode_uint8_srgb4_linearalpha, stbir__encode_uint8_srgb, 0, stbir__encode_float_linear, stbir__encode_half_float_linear }, + { /* BGRA */ stbir__encode_uint8_srgb4_linearalpha_BGRA, stbir__encode_uint8_srgb_BGRA, 0, stbir__encode_float_linear_BGRA, stbir__encode_half_float_linear_BGRA }, + { /* ARGB */ stbir__encode_uint8_srgb4_linearalpha_ARGB, stbir__encode_uint8_srgb_ARGB, 0, stbir__encode_float_linear_ARGB, stbir__encode_half_float_linear_ARGB }, + { /* ABGR */ stbir__encode_uint8_srgb4_linearalpha_ABGR, stbir__encode_uint8_srgb_ABGR, 0, stbir__encode_float_linear_ABGR, stbir__encode_half_float_linear_ABGR }, + { /* RA */ stbir__encode_uint8_srgb2_linearalpha, stbir__encode_uint8_srgb, 0, stbir__encode_float_linear, stbir__encode_half_float_linear }, + { /* AR */ stbir__encode_uint8_srgb2_linearalpha_AR, stbir__encode_uint8_srgb_AR, 0, stbir__encode_float_linear_AR, stbir__encode_half_float_linear_AR } + }; + + static stbir__encode_pixels_func * encode_simple_scaled_or_not[2][2]= + { + { stbir__encode_uint8_linear_scaled, stbir__encode_uint8_linear }, { stbir__encode_uint16_linear_scaled, stbir__encode_uint16_linear }, + }; + + static stbir__encode_pixels_func * encode_alphas_scaled_or_not[STBIRI_AR-STBIRI_RGBA+1][2][2]= + { + { /* RGBA */ { stbir__encode_uint8_linear_scaled, stbir__encode_uint8_linear }, { stbir__encode_uint16_linear_scaled, stbir__encode_uint16_linear } }, + { /* BGRA */ { stbir__encode_uint8_linear_scaled_BGRA, stbir__encode_uint8_linear_BGRA }, { stbir__encode_uint16_linear_scaled_BGRA, stbir__encode_uint16_linear_BGRA } }, + { /* ARGB */ { stbir__encode_uint8_linear_scaled_ARGB, stbir__encode_uint8_linear_ARGB }, { stbir__encode_uint16_linear_scaled_ARGB, stbir__encode_uint16_linear_ARGB } }, + { /* ABGR */ { stbir__encode_uint8_linear_scaled_ABGR, stbir__encode_uint8_linear_ABGR }, { stbir__encode_uint16_linear_scaled_ABGR, stbir__encode_uint16_linear_ABGR } }, + { /* RA */ { stbir__encode_uint8_linear_scaled, stbir__encode_uint8_linear }, { stbir__encode_uint16_linear_scaled, stbir__encode_uint16_linear } }, + { /* AR */ { stbir__encode_uint8_linear_scaled_AR, stbir__encode_uint8_linear_AR }, { stbir__encode_uint16_linear_scaled_AR, stbir__encode_uint16_linear_AR } } + }; + + stbir__decode_pixels_func * decode_pixels = 0; + stbir__encode_pixels_func * encode_pixels = 0; + stbir_datatype input_type, output_type; + + input_type = resize->input_data_type; + output_type = resize->output_data_type; + info->input_data = resize->input_pixels; + info->input_stride_bytes = resize->input_stride_in_bytes; + info->output_stride_bytes = resize->output_stride_in_bytes; + + // if we're completely point sampling, then we can turn off SRGB + if ( ( info->horizontal.filter_enum == STBIR_FILTER_POINT_SAMPLE ) && ( info->vertical.filter_enum == STBIR_FILTER_POINT_SAMPLE ) ) + { + if ( ( ( input_type == STBIR_TYPE_UINT8_SRGB ) || ( input_type == STBIR_TYPE_UINT8_SRGB_ALPHA ) ) && + ( ( output_type == STBIR_TYPE_UINT8_SRGB ) || ( output_type == STBIR_TYPE_UINT8_SRGB_ALPHA ) ) ) + { + input_type = STBIR_TYPE_UINT8; + output_type = STBIR_TYPE_UINT8; + } + } + + // recalc the output and input strides + if ( info->input_stride_bytes == 0 ) + info->input_stride_bytes = info->channels * info->horizontal.scale_info.input_full_size * stbir__type_size[input_type]; + + if ( info->output_stride_bytes == 0 ) + info->output_stride_bytes = info->channels * info->horizontal.scale_info.output_sub_size * stbir__type_size[output_type]; + + // calc offset + info->output_data = ( (char*) resize->output_pixels ) + ( (size_t) info->offset_y * (size_t) resize->output_stride_in_bytes ) + ( info->offset_x * info->channels * stbir__type_size[output_type] ); + + info->in_pixels_cb = resize->input_cb; + info->user_data = resize->user_data; + info->out_pixels_cb = resize->output_cb; + + // setup the input format converters + if ( ( input_type == STBIR_TYPE_UINT8 ) || ( input_type == STBIR_TYPE_UINT16 ) ) + { + int non_scaled = 0; + + // check if we can run unscaled - 0-255.0/0-65535.0 instead of 0-1.0 (which is a tiny bit faster when doing linear 8->8 or 16->16) + if ( ( !info->alpha_weight ) && ( !info->alpha_unweight ) ) // don't short circuit when alpha weighting (get everything to 0-1.0 as usual) + if ( ( ( input_type == STBIR_TYPE_UINT8 ) && ( output_type == STBIR_TYPE_UINT8 ) ) || ( ( input_type == STBIR_TYPE_UINT16 ) && ( output_type == STBIR_TYPE_UINT16 ) ) ) + non_scaled = 1; + + if ( info->input_pixel_layout_internal <= STBIRI_4CHANNEL ) + decode_pixels = decode_simple_scaled_or_not[ input_type == STBIR_TYPE_UINT16 ][ non_scaled ]; + else + decode_pixels = decode_alphas_scaled_or_not[ ( info->input_pixel_layout_internal - STBIRI_RGBA ) % ( STBIRI_AR-STBIRI_RGBA+1 ) ][ input_type == STBIR_TYPE_UINT16 ][ non_scaled ]; + } + else + { + if ( info->input_pixel_layout_internal <= STBIRI_4CHANNEL ) + decode_pixels = decode_simple[ input_type - STBIR_TYPE_UINT8_SRGB ]; + else + decode_pixels = decode_alphas[ ( info->input_pixel_layout_internal - STBIRI_RGBA ) % ( STBIRI_AR-STBIRI_RGBA+1 ) ][ input_type - STBIR_TYPE_UINT8_SRGB ]; + } + + // setup the output format converters + if ( ( output_type == STBIR_TYPE_UINT8 ) || ( output_type == STBIR_TYPE_UINT16 ) ) + { + int non_scaled = 0; + + // check if we can run unscaled - 0-255.0/0-65535.0 instead of 0-1.0 (which is a tiny bit faster when doing linear 8->8 or 16->16) + if ( ( !info->alpha_weight ) && ( !info->alpha_unweight ) ) // don't short circuit when alpha weighting (get everything to 0-1.0 as usual) + if ( ( ( input_type == STBIR_TYPE_UINT8 ) && ( output_type == STBIR_TYPE_UINT8 ) ) || ( ( input_type == STBIR_TYPE_UINT16 ) && ( output_type == STBIR_TYPE_UINT16 ) ) ) + non_scaled = 1; + + if ( info->output_pixel_layout_internal <= STBIRI_4CHANNEL ) + encode_pixels = encode_simple_scaled_or_not[ output_type == STBIR_TYPE_UINT16 ][ non_scaled ]; + else + encode_pixels = encode_alphas_scaled_or_not[ ( info->output_pixel_layout_internal - STBIRI_RGBA ) % ( STBIRI_AR-STBIRI_RGBA+1 ) ][ output_type == STBIR_TYPE_UINT16 ][ non_scaled ]; + } + else + { + if ( info->output_pixel_layout_internal <= STBIRI_4CHANNEL ) + encode_pixels = encode_simple[ output_type - STBIR_TYPE_UINT8_SRGB ]; + else + encode_pixels = encode_alphas[ ( info->output_pixel_layout_internal - STBIRI_RGBA ) % ( STBIRI_AR-STBIRI_RGBA+1 ) ][ output_type - STBIR_TYPE_UINT8_SRGB ]; + } + + info->input_type = input_type; + info->output_type = output_type; + info->decode_pixels = decode_pixels; + info->encode_pixels = encode_pixels; +} + +static void stbir__clip( int * outx, int * outsubw, int outw, double * u0, double * u1 ) +{ + double per, adj; + int over; + + // do left/top edge + if ( *outx < 0 ) + { + per = ( (double)*outx ) / ( (double)*outsubw ); // is negative + adj = per * ( *u1 - *u0 ); + *u0 -= adj; // increases u0 + *outx = 0; + } + + // do right/bot edge + over = outw - ( *outx + *outsubw ); + if ( over < 0 ) + { + per = ( (double)over ) / ( (double)*outsubw ); // is negative + adj = per * ( *u1 - *u0 ); + *u1 += adj; // decrease u1 + *outsubw = outw - *outx; + } +} + +// converts a double to a rational that has less than one float bit of error (returns 0 if unable to do so) +static int stbir__double_to_rational(double f, stbir_uint32 limit, stbir_uint32 *numer, stbir_uint32 *denom, int limit_denom ) // limit_denom (1) or limit numer (0) +{ + double err; + stbir_uint64 top, bot; + stbir_uint64 numer_last = 0; + stbir_uint64 denom_last = 1; + stbir_uint64 numer_estimate = 1; + stbir_uint64 denom_estimate = 0; + + // scale to past float error range + top = (stbir_uint64)( f * (double)(1 << 25) ); + bot = 1 << 25; + + // keep refining, but usually stops in a few loops - usually 5 for bad cases + for(;;) + { + stbir_uint64 est, temp; + + // hit limit, break out and do best full range estimate + if ( ( ( limit_denom ) ? denom_estimate : numer_estimate ) >= limit ) + break; + + // is the current error less than 1 bit of a float? if so, we're done + if ( denom_estimate ) + { + err = ( (double)numer_estimate / (double)denom_estimate ) - f; + if ( err < 0.0 ) err = -err; + if ( err < ( 1.0 / (double)(1<<24) ) ) + { + // yup, found it + *numer = (stbir_uint32) numer_estimate; + *denom = (stbir_uint32) denom_estimate; + return 1; + } + } + + // no more refinement bits left? break out and do full range estimate + if ( bot == 0 ) + break; + + // gcd the estimate bits + est = top / bot; + temp = top % bot; + top = bot; + bot = temp; + + // move remainders + temp = est * denom_estimate + denom_last; + denom_last = denom_estimate; + denom_estimate = temp; + + // move remainders + temp = est * numer_estimate + numer_last; + numer_last = numer_estimate; + numer_estimate = temp; + } + + // we didn't find anything good enough for float, use a full range estimate + if ( limit_denom ) + { + numer_estimate= (stbir_uint64)( f * (double)limit + 0.5 ); + denom_estimate = limit; + } + else + { + numer_estimate = limit; + denom_estimate = (stbir_uint64)( ( (double)limit / f ) + 0.5 ); + } + + *numer = (stbir_uint32) numer_estimate; + *denom = (stbir_uint32) denom_estimate; + + err = ( denom_estimate ) ? ( ( (double)(stbir_uint32)numer_estimate / (double)(stbir_uint32)denom_estimate ) - f ) : 1.0; + if ( err < 0.0 ) err = -err; + return ( err < ( 1.0 / (double)(1<<24) ) ) ? 1 : 0; +} + +static int stbir__calculate_region_transform( stbir__scale_info * scale_info, int output_full_range, int * output_offset, int output_sub_range, int input_full_range, double input_s0, double input_s1 ) +{ + double output_range, input_range, output_s, input_s, ratio, scale; + + input_s = input_s1 - input_s0; + + // null area + if ( ( output_full_range == 0 ) || ( input_full_range == 0 ) || + ( output_sub_range == 0 ) || ( input_s <= stbir__small_float ) ) + return 0; + + // are either of the ranges completely out of bounds? + if ( ( *output_offset >= output_full_range ) || ( ( *output_offset + output_sub_range ) <= 0 ) || ( input_s0 >= (1.0f-stbir__small_float) ) || ( input_s1 <= stbir__small_float ) ) + return 0; + + output_range = (double)output_full_range; + input_range = (double)input_full_range; + + output_s = ( (double)output_sub_range) / output_range; + + // figure out the scaling to use + ratio = output_s / input_s; + + // save scale before clipping + scale = ( output_range / input_range ) * ratio; + scale_info->scale = (float)scale; + scale_info->inv_scale = (float)( 1.0 / scale ); + + // clip output area to left/right output edges (and adjust input area) + stbir__clip( output_offset, &output_sub_range, output_full_range, &input_s0, &input_s1 ); + + // recalc input area + input_s = input_s1 - input_s0; + + // after clipping do we have zero input area? + if ( input_s <= stbir__small_float ) + return 0; + + // calculate and store the starting source offsets in output pixel space + scale_info->pixel_shift = (float) ( input_s0 * ratio * output_range ); + + scale_info->scale_is_rational = stbir__double_to_rational( scale, ( scale <= 1.0 ) ? output_full_range : input_full_range, &scale_info->scale_numerator, &scale_info->scale_denominator, ( scale >= 1.0 ) ); + + scale_info->input_full_size = input_full_range; + scale_info->output_sub_size = output_sub_range; + + return 1; +} + + +static void stbir__init_and_set_layout( STBIR_RESIZE * resize, stbir_pixel_layout pixel_layout, stbir_datatype data_type ) +{ + resize->input_cb = 0; + resize->output_cb = 0; + resize->user_data = resize; + resize->samplers = 0; + resize->called_alloc = 0; + resize->horizontal_filter = STBIR_FILTER_DEFAULT; + resize->horizontal_filter_kernel = 0; resize->horizontal_filter_support = 0; + resize->vertical_filter = STBIR_FILTER_DEFAULT; + resize->vertical_filter_kernel = 0; resize->vertical_filter_support = 0; + resize->horizontal_edge = STBIR_EDGE_CLAMP; + resize->vertical_edge = STBIR_EDGE_CLAMP; + resize->input_s0 = 0; resize->input_t0 = 0; resize->input_s1 = 1; resize->input_t1 = 1; + resize->output_subx = 0; resize->output_suby = 0; resize->output_subw = resize->output_w; resize->output_subh = resize->output_h; + resize->input_data_type = data_type; + resize->output_data_type = data_type; + resize->input_pixel_layout_public = pixel_layout; + resize->output_pixel_layout_public = pixel_layout; + resize->needs_rebuild = 1; +} + +STBIRDEF void stbir_resize_init( STBIR_RESIZE * resize, + const void *input_pixels, int input_w, int input_h, int input_stride_in_bytes, // stride can be zero + void *output_pixels, int output_w, int output_h, int output_stride_in_bytes, // stride can be zero + stbir_pixel_layout pixel_layout, stbir_datatype data_type ) +{ + resize->input_pixels = input_pixels; + resize->input_w = input_w; + resize->input_h = input_h; + resize->input_stride_in_bytes = input_stride_in_bytes; + resize->output_pixels = output_pixels; + resize->output_w = output_w; + resize->output_h = output_h; + resize->output_stride_in_bytes = output_stride_in_bytes; + resize->fast_alpha = 0; + + stbir__init_and_set_layout( resize, pixel_layout, data_type ); +} + +// You can update parameters any time after resize_init +STBIRDEF void stbir_set_datatypes( STBIR_RESIZE * resize, stbir_datatype input_type, stbir_datatype output_type ) // by default, datatype from resize_init +{ + resize->input_data_type = input_type; + resize->output_data_type = output_type; + if ( ( resize->samplers ) && ( !resize->needs_rebuild ) ) + stbir__update_info_from_resize( resize->samplers, resize ); +} + +STBIRDEF void stbir_set_pixel_callbacks( STBIR_RESIZE * resize, stbir_input_callback * input_cb, stbir_output_callback * output_cb ) // no callbacks by default +{ + resize->input_cb = input_cb; + resize->output_cb = output_cb; + + if ( ( resize->samplers ) && ( !resize->needs_rebuild ) ) + { + resize->samplers->in_pixels_cb = input_cb; + resize->samplers->out_pixels_cb = output_cb; + } +} + +STBIRDEF void stbir_set_user_data( STBIR_RESIZE * resize, void * user_data ) // pass back STBIR_RESIZE* by default +{ + resize->user_data = user_data; + if ( ( resize->samplers ) && ( !resize->needs_rebuild ) ) + resize->samplers->user_data = user_data; +} + +STBIRDEF void stbir_set_buffer_ptrs( STBIR_RESIZE * resize, const void * input_pixels, int input_stride_in_bytes, void * output_pixels, int output_stride_in_bytes ) +{ + resize->input_pixels = input_pixels; + resize->input_stride_in_bytes = input_stride_in_bytes; + resize->output_pixels = output_pixels; + resize->output_stride_in_bytes = output_stride_in_bytes; + if ( ( resize->samplers ) && ( !resize->needs_rebuild ) ) + stbir__update_info_from_resize( resize->samplers, resize ); +} + + +STBIRDEF int stbir_set_edgemodes( STBIR_RESIZE * resize, stbir_edge horizontal_edge, stbir_edge vertical_edge ) // CLAMP by default +{ + resize->horizontal_edge = horizontal_edge; + resize->vertical_edge = vertical_edge; + resize->needs_rebuild = 1; + return 1; +} + +STBIRDEF int stbir_set_filters( STBIR_RESIZE * resize, stbir_filter horizontal_filter, stbir_filter vertical_filter ) // STBIR_DEFAULT_FILTER_UPSAMPLE/DOWNSAMPLE by default +{ + resize->horizontal_filter = horizontal_filter; + resize->vertical_filter = vertical_filter; + resize->needs_rebuild = 1; + return 1; +} + +STBIRDEF int stbir_set_filter_callbacks( STBIR_RESIZE * resize, stbir__kernel_callback * horizontal_filter, stbir__support_callback * horizontal_support, stbir__kernel_callback * vertical_filter, stbir__support_callback * vertical_support ) +{ + resize->horizontal_filter_kernel = horizontal_filter; resize->horizontal_filter_support = horizontal_support; + resize->vertical_filter_kernel = vertical_filter; resize->vertical_filter_support = vertical_support; + resize->needs_rebuild = 1; + return 1; +} + +STBIRDEF int stbir_set_pixel_layouts( STBIR_RESIZE * resize, stbir_pixel_layout input_pixel_layout, stbir_pixel_layout output_pixel_layout ) // sets new pixel layouts +{ + resize->input_pixel_layout_public = input_pixel_layout; + resize->output_pixel_layout_public = output_pixel_layout; + resize->needs_rebuild = 1; + return 1; +} + + +STBIRDEF int stbir_set_non_pm_alpha_speed_over_quality( STBIR_RESIZE * resize, int non_pma_alpha_speed_over_quality ) // sets alpha speed +{ + resize->fast_alpha = non_pma_alpha_speed_over_quality; + resize->needs_rebuild = 1; + return 1; +} + +STBIRDEF int stbir_set_input_subrect( STBIR_RESIZE * resize, double s0, double t0, double s1, double t1 ) // sets input region (full region by default) +{ + resize->input_s0 = s0; + resize->input_t0 = t0; + resize->input_s1 = s1; + resize->input_t1 = t1; + resize->needs_rebuild = 1; + + // are we inbounds? + if ( ( s1 < stbir__small_float ) || ( (s1-s0) < stbir__small_float ) || + ( t1 < stbir__small_float ) || ( (t1-t0) < stbir__small_float ) || + ( s0 > (1.0f-stbir__small_float) ) || + ( t0 > (1.0f-stbir__small_float) ) ) + return 0; + + return 1; +} + +STBIRDEF int stbir_set_output_pixel_subrect( STBIR_RESIZE * resize, int subx, int suby, int subw, int subh ) // sets input region (full region by default) +{ + resize->output_subx = subx; + resize->output_suby = suby; + resize->output_subw = subw; + resize->output_subh = subh; + resize->needs_rebuild = 1; + + // are we inbounds? + if ( ( subx >= resize->output_w ) || ( ( subx + subw ) <= 0 ) || ( suby >= resize->output_h ) || ( ( suby + subh ) <= 0 ) || ( subw == 0 ) || ( subh == 0 ) ) + return 0; + + return 1; +} + +STBIRDEF int stbir_set_pixel_subrect( STBIR_RESIZE * resize, int subx, int suby, int subw, int subh ) // sets both regions (full regions by default) +{ + double s0, t0, s1, t1; + + s0 = ( (double)subx ) / ( (double)resize->output_w ); + t0 = ( (double)suby ) / ( (double)resize->output_h ); + s1 = ( (double)(subx+subw) ) / ( (double)resize->output_w ); + t1 = ( (double)(suby+subh) ) / ( (double)resize->output_h ); + + resize->input_s0 = s0; + resize->input_t0 = t0; + resize->input_s1 = s1; + resize->input_t1 = t1; + resize->output_subx = subx; + resize->output_suby = suby; + resize->output_subw = subw; + resize->output_subh = subh; + resize->needs_rebuild = 1; + + // are we inbounds? + if ( ( subx >= resize->output_w ) || ( ( subx + subw ) <= 0 ) || ( suby >= resize->output_h ) || ( ( suby + subh ) <= 0 ) || ( subw == 0 ) || ( subh == 0 ) ) + return 0; + + return 1; +} + +static int stbir__perform_build( STBIR_RESIZE * resize, int splits ) +{ + stbir__contributors conservative = { 0, 0 }; + stbir__sampler horizontal, vertical; + int new_output_subx, new_output_suby; + stbir__info * out_info; + #ifdef STBIR_PROFILE + stbir__info profile_infod; // used to contain building profile info before everything is allocated + stbir__info * profile_info = &profile_infod; + #endif + + // have we already built the samplers? + if ( resize->samplers ) + return 0; + + #define STBIR_RETURN_ERROR_AND_ASSERT( exp ) STBIR_ASSERT( !(exp) ); if (exp) return 0; + STBIR_RETURN_ERROR_AND_ASSERT( (unsigned)resize->horizontal_filter >= STBIR_FILTER_OTHER) + STBIR_RETURN_ERROR_AND_ASSERT( (unsigned)resize->vertical_filter >= STBIR_FILTER_OTHER) + #undef STBIR_RETURN_ERROR_AND_ASSERT + + if ( splits <= 0 ) + return 0; + + STBIR_PROFILE_BUILD_FIRST_START( build ); + + new_output_subx = resize->output_subx; + new_output_suby = resize->output_suby; + + // do horizontal clip and scale calcs + if ( !stbir__calculate_region_transform( &horizontal.scale_info, resize->output_w, &new_output_subx, resize->output_subw, resize->input_w, resize->input_s0, resize->input_s1 ) ) + return 0; + + // do vertical clip and scale calcs + if ( !stbir__calculate_region_transform( &vertical.scale_info, resize->output_h, &new_output_suby, resize->output_subh, resize->input_h, resize->input_t0, resize->input_t1 ) ) + return 0; + + // if nothing to do, just return + if ( ( horizontal.scale_info.output_sub_size == 0 ) || ( vertical.scale_info.output_sub_size == 0 ) ) + return 0; + + stbir__set_sampler(&horizontal, resize->horizontal_filter, resize->horizontal_filter_kernel, resize->horizontal_filter_support, resize->horizontal_edge, &horizontal.scale_info, 1, resize->user_data ); + stbir__get_conservative_extents( &horizontal, &conservative, resize->user_data ); + stbir__set_sampler(&vertical, resize->vertical_filter, resize->vertical_filter_kernel, resize->vertical_filter_support, resize->vertical_edge, &vertical.scale_info, 0, resize->user_data ); + + if ( ( vertical.scale_info.output_sub_size / splits ) < STBIR_FORCE_MINIMUM_SCANLINES_FOR_SPLITS ) // each split should be a minimum of 4 scanlines (handwavey choice) + { + splits = vertical.scale_info.output_sub_size / STBIR_FORCE_MINIMUM_SCANLINES_FOR_SPLITS; + if ( splits == 0 ) splits = 1; + } + + STBIR_PROFILE_BUILD_START( alloc ); + out_info = stbir__alloc_internal_mem_and_build_samplers( &horizontal, &vertical, &conservative, resize->input_pixel_layout_public, resize->output_pixel_layout_public, splits, new_output_subx, new_output_suby, resize->fast_alpha, resize->user_data STBIR_ONLY_PROFILE_BUILD_SET_INFO ); + STBIR_PROFILE_BUILD_END( alloc ); + STBIR_PROFILE_BUILD_END( build ); + + if ( out_info ) + { + resize->splits = splits; + resize->samplers = out_info; + resize->needs_rebuild = 0; + #ifdef STBIR_PROFILE + STBIR_MEMCPY( &out_info->profile, &profile_infod.profile, sizeof( out_info->profile ) ); + #endif + + // update anything that can be changed without recalcing samplers + stbir__update_info_from_resize( out_info, resize ); + + return splits; + } + + return 0; +} + +STBIRDEF void stbir_free_samplers( STBIR_RESIZE * resize ) +{ + if ( resize->samplers ) + { + stbir__free_internal_mem( resize->samplers ); + resize->samplers = 0; + resize->called_alloc = 0; + } +} + +STBIRDEF int stbir_build_samplers_with_splits( STBIR_RESIZE * resize, int splits ) +{ + if ( ( resize->samplers == 0 ) || ( resize->needs_rebuild ) ) + { + if ( resize->samplers ) + stbir_free_samplers( resize ); + + resize->called_alloc = 1; + return stbir__perform_build( resize, splits ); + } + + STBIR_PROFILE_BUILD_CLEAR( resize->samplers ); + + return 1; +} + +STBIRDEF int stbir_build_samplers( STBIR_RESIZE * resize ) +{ + return stbir_build_samplers_with_splits( resize, 1 ); +} + +STBIRDEF int stbir_resize_extended( STBIR_RESIZE * resize ) +{ + int result; + + if ( ( resize->samplers == 0 ) || ( resize->needs_rebuild ) ) + { + int alloc_state = resize->called_alloc; // remember allocated state + + if ( resize->samplers ) + { + stbir__free_internal_mem( resize->samplers ); + resize->samplers = 0; + } + + if ( !stbir_build_samplers( resize ) ) + return 0; + + resize->called_alloc = alloc_state; + + // if build_samplers succeeded (above), but there are no samplers set, then + // the area to stretch into was zero pixels, so don't do anything and return + // success + if ( resize->samplers == 0 ) + return 1; + } + else + { + // didn't build anything - clear it + STBIR_PROFILE_BUILD_CLEAR( resize->samplers ); + } + + // do resize + result = stbir__perform_resize( resize->samplers, 0, resize->splits ); + + // if we alloced, then free + if ( !resize->called_alloc ) + { + stbir_free_samplers( resize ); + resize->samplers = 0; + } + + return result; +} + +STBIRDEF int stbir_resize_extended_split( STBIR_RESIZE * resize, int split_start, int split_count ) +{ + STBIR_ASSERT( resize->samplers ); + + // if we're just doing the whole thing, call full + if ( ( split_start == -1 ) || ( ( split_start == 0 ) && ( split_count == resize->splits ) ) ) + return stbir_resize_extended( resize ); + + // you **must** build samplers first when using split resize + if ( ( resize->samplers == 0 ) || ( resize->needs_rebuild ) ) + return 0; + + if ( ( split_start >= resize->splits ) || ( split_start < 0 ) || ( ( split_start + split_count ) > resize->splits ) || ( split_count <= 0 ) ) + return 0; + + // do resize + return stbir__perform_resize( resize->samplers, split_start, split_count ); +} + + +static void * stbir_quick_resize_helper( const void *input_pixels , int input_w , int input_h, int input_stride_in_bytes, + void *output_pixels, int output_w, int output_h, int output_stride_in_bytes, + stbir_pixel_layout pixel_layout, stbir_datatype data_type, stbir_edge edge, stbir_filter filter ) +{ + STBIR_RESIZE resize; + int scanline_output_in_bytes; + int positive_output_stride_in_bytes; + void * start_ptr; + void * free_ptr; + + scanline_output_in_bytes = output_w * stbir__type_size[ data_type ] * stbir__pixel_channels[ stbir__pixel_layout_convert_public_to_internal[ pixel_layout ] ]; + if ( scanline_output_in_bytes == 0 ) + return 0; + + // if zero stride, use scanline output + if ( output_stride_in_bytes == 0 ) + output_stride_in_bytes = scanline_output_in_bytes; + + // abs value for inverted images (negative pitches) + positive_output_stride_in_bytes = output_stride_in_bytes; + if ( positive_output_stride_in_bytes < 0 ) + positive_output_stride_in_bytes = -positive_output_stride_in_bytes; + + // is the requested stride smaller than the scanline output? if so, just fail + if ( positive_output_stride_in_bytes < scanline_output_in_bytes ) + return 0; + + start_ptr = output_pixels; + free_ptr = 0; // no free pointer, since they passed buffer to use + + // did they pass a zero for the dest? if so, allocate the buffer + if ( output_pixels == 0 ) + { + size_t size; + char * ptr; + + size = (size_t)positive_output_stride_in_bytes * (size_t)output_h; + if ( size == 0 ) + return 0; + + ptr = (char*) STBIR_MALLOC( size, 0 ); + if ( ptr == 0 ) + return 0; + + free_ptr = ptr; + + // point at the last scanline, if they requested a flipped image + if ( output_stride_in_bytes < 0 ) + start_ptr = ptr + ( (size_t)positive_output_stride_in_bytes * (size_t)( output_h - 1 ) ); + else + start_ptr = ptr; + } + + // ok, now do the resize + stbir_resize_init( &resize, + input_pixels, input_w, input_h, input_stride_in_bytes, + start_ptr, output_w, output_h, output_stride_in_bytes, + pixel_layout, data_type ); + + resize.horizontal_edge = edge; + resize.vertical_edge = edge; + resize.horizontal_filter = filter; + resize.vertical_filter = filter; + + if ( !stbir_resize_extended( &resize ) ) + { + if ( free_ptr ) + STBIR_FREE( free_ptr, 0 ); + return 0; + } + + return (free_ptr) ? free_ptr : start_ptr; +} + + + +STBIRDEF unsigned char * stbir_resize_uint8_linear( const unsigned char *input_pixels , int input_w , int input_h, int input_stride_in_bytes, + unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes, + stbir_pixel_layout pixel_layout ) +{ + return (unsigned char *) stbir_quick_resize_helper( input_pixels , input_w , input_h, input_stride_in_bytes, + output_pixels, output_w, output_h, output_stride_in_bytes, + pixel_layout, STBIR_TYPE_UINT8, STBIR_EDGE_CLAMP, STBIR_FILTER_DEFAULT ); +} + +STBIRDEF unsigned char * stbir_resize_uint8_srgb( const unsigned char *input_pixels , int input_w , int input_h, int input_stride_in_bytes, + unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes, + stbir_pixel_layout pixel_layout ) +{ + return (unsigned char *) stbir_quick_resize_helper( input_pixels , input_w , input_h, input_stride_in_bytes, + output_pixels, output_w, output_h, output_stride_in_bytes, + pixel_layout, STBIR_TYPE_UINT8_SRGB, STBIR_EDGE_CLAMP, STBIR_FILTER_DEFAULT ); +} + + +STBIRDEF float * stbir_resize_float_linear( const float *input_pixels , int input_w , int input_h, int input_stride_in_bytes, + float *output_pixels, int output_w, int output_h, int output_stride_in_bytes, + stbir_pixel_layout pixel_layout ) +{ + return (float *) stbir_quick_resize_helper( input_pixels , input_w , input_h, input_stride_in_bytes, + output_pixels, output_w, output_h, output_stride_in_bytes, + pixel_layout, STBIR_TYPE_FLOAT, STBIR_EDGE_CLAMP, STBIR_FILTER_DEFAULT ); +} + + +STBIRDEF void * stbir_resize( const void *input_pixels , int input_w , int input_h, int input_stride_in_bytes, + void *output_pixels, int output_w, int output_h, int output_stride_in_bytes, + stbir_pixel_layout pixel_layout, stbir_datatype data_type, + stbir_edge edge, stbir_filter filter ) +{ + return (void *) stbir_quick_resize_helper( input_pixels , input_w , input_h, input_stride_in_bytes, + output_pixels, output_w, output_h, output_stride_in_bytes, + pixel_layout, data_type, edge, filter ); +} + +#ifdef STBIR_PROFILE + +STBIRDEF void stbir_resize_build_profile_info( STBIR_PROFILE_INFO * info, STBIR_RESIZE const * resize ) +{ + static char const * bdescriptions[6] = { "Building", "Allocating", "Horizontal sampler", "Vertical sampler", "Coefficient cleanup", "Coefficient pivot" } ; + stbir__info* samp = resize->samplers; + int i; + + typedef int testa[ (STBIR__ARRAY_SIZE( bdescriptions ) == (STBIR__ARRAY_SIZE( samp->profile.array )-1) )?1:-1]; + typedef int testb[ (sizeof( samp->profile.array ) == (sizeof(samp->profile.named)) )?1:-1]; + typedef int testc[ (sizeof( info->clocks ) >= (sizeof(samp->profile.named)) )?1:-1]; + + for( i = 0 ; i < STBIR__ARRAY_SIZE( bdescriptions ) ; i++) + info->clocks[i] = samp->profile.array[i+1]; + + info->total_clocks = samp->profile.named.total; + info->descriptions = bdescriptions; + info->count = STBIR__ARRAY_SIZE( bdescriptions ); +} + +STBIRDEF void stbir_resize_split_profile_info( STBIR_PROFILE_INFO * info, STBIR_RESIZE const * resize, int split_start, int split_count ) +{ + static char const * descriptions[7] = { "Looping", "Vertical sampling", "Horizontal sampling", "Scanline input", "Scanline output", "Alpha weighting", "Alpha unweighting" }; + stbir__per_split_info * split_info; + int s, i; + + typedef int testa[ (STBIR__ARRAY_SIZE( descriptions ) == (STBIR__ARRAY_SIZE( split_info->profile.array )-1) )?1:-1]; + typedef int testb[ (sizeof( split_info->profile.array ) == (sizeof(split_info->profile.named)) )?1:-1]; + typedef int testc[ (sizeof( info->clocks ) >= (sizeof(split_info->profile.named)) )?1:-1]; + + if ( split_start == -1 ) + { + split_start = 0; + split_count = resize->samplers->splits; + } + + if ( ( split_start >= resize->splits ) || ( split_start < 0 ) || ( ( split_start + split_count ) > resize->splits ) || ( split_count <= 0 ) ) + { + info->total_clocks = 0; + info->descriptions = 0; + info->count = 0; + return; + } + + split_info = resize->samplers->split_info + split_start; + + // sum up the profile from all the splits + for( i = 0 ; i < STBIR__ARRAY_SIZE( descriptions ) ; i++ ) + { + stbir_uint64 sum = 0; + for( s = 0 ; s < split_count ; s++ ) + sum += split_info[s].profile.array[i+1]; + info->clocks[i] = sum; + } + + info->total_clocks = split_info->profile.named.total; + info->descriptions = descriptions; + info->count = STBIR__ARRAY_SIZE( descriptions ); +} + +STBIRDEF void stbir_resize_extended_profile_info( STBIR_PROFILE_INFO * info, STBIR_RESIZE const * resize ) +{ + stbir_resize_split_profile_info( info, resize, -1, 0 ); +} + +#endif // STBIR_PROFILE + +#undef STBIR_BGR +#undef STBIR_1CHANNEL +#undef STBIR_2CHANNEL +#undef STBIR_RGB +#undef STBIR_RGBA +#undef STBIR_4CHANNEL +#undef STBIR_BGRA +#undef STBIR_ARGB +#undef STBIR_ABGR +#undef STBIR_RA +#undef STBIR_AR +#undef STBIR_RGBA_PM +#undef STBIR_BGRA_PM +#undef STBIR_ARGB_PM +#undef STBIR_ABGR_PM +#undef STBIR_RA_PM +#undef STBIR_AR_PM + +#endif // STB_IMAGE_RESIZE_IMPLEMENTATION + +#else // STB_IMAGE_RESIZE_HORIZONTALS&STB_IMAGE_RESIZE_DO_VERTICALS + +// we reinclude the header file to define all the horizontal functions +// specializing each function for the number of coeffs is 20-40% faster *OVERALL* + +// by including the header file again this way, we can still debug the functions + +#define STBIR_strs_join2( start, mid, end ) start##mid##end +#define STBIR_strs_join1( start, mid, end ) STBIR_strs_join2( start, mid, end ) + +#define STBIR_strs_join24( start, mid1, mid2, end ) start##mid1##mid2##end +#define STBIR_strs_join14( start, mid1, mid2, end ) STBIR_strs_join24( start, mid1, mid2, end ) + +#ifdef STB_IMAGE_RESIZE_DO_CODERS + +#ifdef stbir__decode_suffix +#define STBIR__CODER_NAME( name ) STBIR_strs_join1( name, _, stbir__decode_suffix ) +#else +#define STBIR__CODER_NAME( name ) name +#endif + +#ifdef stbir__decode_swizzle +#define stbir__decode_simdf8_flip(reg) STBIR_strs_join1( STBIR_strs_join1( STBIR_strs_join1( STBIR_strs_join1( stbir__simdf8_0123to,stbir__decode_order0,stbir__decode_order1),stbir__decode_order2,stbir__decode_order3),stbir__decode_order0,stbir__decode_order1),stbir__decode_order2,stbir__decode_order3)(reg, reg) +#define stbir__decode_simdf4_flip(reg) STBIR_strs_join1( STBIR_strs_join1( stbir__simdf_0123to,stbir__decode_order0,stbir__decode_order1),stbir__decode_order2,stbir__decode_order3)(reg, reg) +#define stbir__encode_simdf8_unflip(reg) STBIR_strs_join1( STBIR_strs_join1( STBIR_strs_join1( STBIR_strs_join1( stbir__simdf8_0123to,stbir__encode_order0,stbir__encode_order1),stbir__encode_order2,stbir__encode_order3),stbir__encode_order0,stbir__encode_order1),stbir__encode_order2,stbir__encode_order3)(reg, reg) +#define stbir__encode_simdf4_unflip(reg) STBIR_strs_join1( STBIR_strs_join1( stbir__simdf_0123to,stbir__encode_order0,stbir__encode_order1),stbir__encode_order2,stbir__encode_order3)(reg, reg) +#else +#define stbir__decode_order0 0 +#define stbir__decode_order1 1 +#define stbir__decode_order2 2 +#define stbir__decode_order3 3 +#define stbir__encode_order0 0 +#define stbir__encode_order1 1 +#define stbir__encode_order2 2 +#define stbir__encode_order3 3 +#define stbir__decode_simdf8_flip(reg) +#define stbir__decode_simdf4_flip(reg) +#define stbir__encode_simdf8_unflip(reg) +#define stbir__encode_simdf4_unflip(reg) +#endif + +#ifdef STBIR_SIMD8 +#define stbir__encode_simdfX_unflip stbir__encode_simdf8_unflip +#else +#define stbir__encode_simdfX_unflip stbir__encode_simdf4_unflip +#endif + +static float * STBIR__CODER_NAME( stbir__decode_uint8_linear_scaled )( float * decodep, int width_times_channels, void const * inputp ) +{ + float STBIR_STREAMOUT_PTR( * ) decode = decodep; + float * decode_end = (float*) decode + width_times_channels; + unsigned char const * input = (unsigned char const*)inputp; + + #ifdef STBIR_SIMD + unsigned char const * end_input_m16 = input + width_times_channels - 16; + if ( width_times_channels >= 16 ) + { + decode_end -= 16; + STBIR_NO_UNROLL_LOOP_START_INF_FOR + for(;;) + { + #ifdef STBIR_SIMD8 + stbir__simdi i; stbir__simdi8 o0,o1; + stbir__simdf8 of0, of1; + STBIR_NO_UNROLL(decode); + stbir__simdi_load( i, input ); + stbir__simdi8_expand_u8_to_u32( o0, o1, i ); + stbir__simdi8_convert_i32_to_float( of0, o0 ); + stbir__simdi8_convert_i32_to_float( of1, o1 ); + stbir__simdf8_mult( of0, of0, STBIR_max_uint8_as_float_inverted8); + stbir__simdf8_mult( of1, of1, STBIR_max_uint8_as_float_inverted8); + stbir__decode_simdf8_flip( of0 ); + stbir__decode_simdf8_flip( of1 ); + stbir__simdf8_store( decode + 0, of0 ); + stbir__simdf8_store( decode + 8, of1 ); + #else + stbir__simdi i, o0, o1, o2, o3; + stbir__simdf of0, of1, of2, of3; + STBIR_NO_UNROLL(decode); + stbir__simdi_load( i, input ); + stbir__simdi_expand_u8_to_u32( o0,o1,o2,o3,i); + stbir__simdi_convert_i32_to_float( of0, o0 ); + stbir__simdi_convert_i32_to_float( of1, o1 ); + stbir__simdi_convert_i32_to_float( of2, o2 ); + stbir__simdi_convert_i32_to_float( of3, o3 ); + stbir__simdf_mult( of0, of0, STBIR__CONSTF(STBIR_max_uint8_as_float_inverted) ); + stbir__simdf_mult( of1, of1, STBIR__CONSTF(STBIR_max_uint8_as_float_inverted) ); + stbir__simdf_mult( of2, of2, STBIR__CONSTF(STBIR_max_uint8_as_float_inverted) ); + stbir__simdf_mult( of3, of3, STBIR__CONSTF(STBIR_max_uint8_as_float_inverted) ); + stbir__decode_simdf4_flip( of0 ); + stbir__decode_simdf4_flip( of1 ); + stbir__decode_simdf4_flip( of2 ); + stbir__decode_simdf4_flip( of3 ); + stbir__simdf_store( decode + 0, of0 ); + stbir__simdf_store( decode + 4, of1 ); + stbir__simdf_store( decode + 8, of2 ); + stbir__simdf_store( decode + 12, of3 ); + #endif + decode += 16; + input += 16; + if ( decode <= decode_end ) + continue; + if ( decode == ( decode_end + 16 ) ) + break; + decode = decode_end; // backup and do last couple + input = end_input_m16; + } + return decode_end + 16; + } + #endif + + // try to do blocks of 4 when you can + #if stbir__coder_min_num != 3 // doesn't divide cleanly by four + decode += 4; + STBIR_SIMD_NO_UNROLL_LOOP_START + while( decode <= decode_end ) + { + STBIR_SIMD_NO_UNROLL(decode); + decode[0-4] = ((float)(input[stbir__decode_order0])) * stbir__max_uint8_as_float_inverted; + decode[1-4] = ((float)(input[stbir__decode_order1])) * stbir__max_uint8_as_float_inverted; + decode[2-4] = ((float)(input[stbir__decode_order2])) * stbir__max_uint8_as_float_inverted; + decode[3-4] = ((float)(input[stbir__decode_order3])) * stbir__max_uint8_as_float_inverted; + decode += 4; + input += 4; + } + decode -= 4; + #endif + + // do the remnants + #if stbir__coder_min_num < 4 + STBIR_NO_UNROLL_LOOP_START + while( decode < decode_end ) + { + STBIR_NO_UNROLL(decode); + decode[0] = ((float)(input[stbir__decode_order0])) * stbir__max_uint8_as_float_inverted; + #if stbir__coder_min_num >= 2 + decode[1] = ((float)(input[stbir__decode_order1])) * stbir__max_uint8_as_float_inverted; + #endif + #if stbir__coder_min_num >= 3 + decode[2] = ((float)(input[stbir__decode_order2])) * stbir__max_uint8_as_float_inverted; + #endif + decode += stbir__coder_min_num; + input += stbir__coder_min_num; + } + #endif + + return decode_end; +} + +static void STBIR__CODER_NAME( stbir__encode_uint8_linear_scaled )( void * outputp, int width_times_channels, float const * encode ) +{ + unsigned char STBIR_SIMD_STREAMOUT_PTR( * ) output = (unsigned char *) outputp; + unsigned char * end_output = ( (unsigned char *) output ) + width_times_channels; + + #ifdef STBIR_SIMD + if ( width_times_channels >= stbir__simdfX_float_count*2 ) + { + float const * end_encode_m8 = encode + width_times_channels - stbir__simdfX_float_count*2; + end_output -= stbir__simdfX_float_count*2; + STBIR_NO_UNROLL_LOOP_START_INF_FOR + for(;;) + { + stbir__simdfX e0, e1; + stbir__simdi i; + STBIR_SIMD_NO_UNROLL(encode); + stbir__simdfX_madd_mem( e0, STBIR_simd_point5X, STBIR_max_uint8_as_floatX, encode ); + stbir__simdfX_madd_mem( e1, STBIR_simd_point5X, STBIR_max_uint8_as_floatX, encode+stbir__simdfX_float_count ); + stbir__encode_simdfX_unflip( e0 ); + stbir__encode_simdfX_unflip( e1 ); + #ifdef STBIR_SIMD8 + stbir__simdf8_pack_to_16bytes( i, e0, e1 ); + stbir__simdi_store( output, i ); + #else + stbir__simdf_pack_to_8bytes( i, e0, e1 ); + stbir__simdi_store2( output, i ); + #endif + encode += stbir__simdfX_float_count*2; + output += stbir__simdfX_float_count*2; + if ( output <= end_output ) + continue; + if ( output == ( end_output + stbir__simdfX_float_count*2 ) ) + break; + output = end_output; // backup and do last couple + encode = end_encode_m8; + } + return; + } + + // try to do blocks of 4 when you can + #if stbir__coder_min_num != 3 // doesn't divide cleanly by four + output += 4; + STBIR_NO_UNROLL_LOOP_START + while( output <= end_output ) + { + stbir__simdf e0; + stbir__simdi i0; + STBIR_NO_UNROLL(encode); + stbir__simdf_load( e0, encode ); + stbir__simdf_madd( e0, STBIR__CONSTF(STBIR_simd_point5), STBIR__CONSTF(STBIR_max_uint8_as_float), e0 ); + stbir__encode_simdf4_unflip( e0 ); + stbir__simdf_pack_to_8bytes( i0, e0, e0 ); // only use first 4 + *(int*)(output-4) = stbir__simdi_to_int( i0 ); + output += 4; + encode += 4; + } + output -= 4; + #endif + + // do the remnants + #if stbir__coder_min_num < 4 + STBIR_NO_UNROLL_LOOP_START + while( output < end_output ) + { + stbir__simdf e0; + STBIR_NO_UNROLL(encode); + stbir__simdf_madd1_mem( e0, STBIR__CONSTF(STBIR_simd_point5), STBIR__CONSTF(STBIR_max_uint8_as_float), encode+stbir__encode_order0 ); output[0] = stbir__simdf_convert_float_to_uint8( e0 ); + #if stbir__coder_min_num >= 2 + stbir__simdf_madd1_mem( e0, STBIR__CONSTF(STBIR_simd_point5), STBIR__CONSTF(STBIR_max_uint8_as_float), encode+stbir__encode_order1 ); output[1] = stbir__simdf_convert_float_to_uint8( e0 ); + #endif + #if stbir__coder_min_num >= 3 + stbir__simdf_madd1_mem( e0, STBIR__CONSTF(STBIR_simd_point5), STBIR__CONSTF(STBIR_max_uint8_as_float), encode+stbir__encode_order2 ); output[2] = stbir__simdf_convert_float_to_uint8( e0 ); + #endif + output += stbir__coder_min_num; + encode += stbir__coder_min_num; + } + #endif + + #else + + // try to do blocks of 4 when you can + #if stbir__coder_min_num != 3 // doesn't divide cleanly by four + output += 4; + while( output <= end_output ) + { + float f; + f = encode[stbir__encode_order0] * stbir__max_uint8_as_float + 0.5f; STBIR_CLAMP(f, 0, 255); output[0-4] = (unsigned char)f; + f = encode[stbir__encode_order1] * stbir__max_uint8_as_float + 0.5f; STBIR_CLAMP(f, 0, 255); output[1-4] = (unsigned char)f; + f = encode[stbir__encode_order2] * stbir__max_uint8_as_float + 0.5f; STBIR_CLAMP(f, 0, 255); output[2-4] = (unsigned char)f; + f = encode[stbir__encode_order3] * stbir__max_uint8_as_float + 0.5f; STBIR_CLAMP(f, 0, 255); output[3-4] = (unsigned char)f; + output += 4; + encode += 4; + } + output -= 4; + #endif + + // do the remnants + #if stbir__coder_min_num < 4 + STBIR_NO_UNROLL_LOOP_START + while( output < end_output ) + { + float f; + STBIR_NO_UNROLL(encode); + f = encode[stbir__encode_order0] * stbir__max_uint8_as_float + 0.5f; STBIR_CLAMP(f, 0, 255); output[0] = (unsigned char)f; + #if stbir__coder_min_num >= 2 + f = encode[stbir__encode_order1] * stbir__max_uint8_as_float + 0.5f; STBIR_CLAMP(f, 0, 255); output[1] = (unsigned char)f; + #endif + #if stbir__coder_min_num >= 3 + f = encode[stbir__encode_order2] * stbir__max_uint8_as_float + 0.5f; STBIR_CLAMP(f, 0, 255); output[2] = (unsigned char)f; + #endif + output += stbir__coder_min_num; + encode += stbir__coder_min_num; + } + #endif + #endif +} + +static float * STBIR__CODER_NAME(stbir__decode_uint8_linear)( float * decodep, int width_times_channels, void const * inputp ) +{ + float STBIR_STREAMOUT_PTR( * ) decode = decodep; + float * decode_end = (float*) decode + width_times_channels; + unsigned char const * input = (unsigned char const*)inputp; + + #ifdef STBIR_SIMD + unsigned char const * end_input_m16 = input + width_times_channels - 16; + if ( width_times_channels >= 16 ) + { + decode_end -= 16; + STBIR_NO_UNROLL_LOOP_START_INF_FOR + for(;;) + { + #ifdef STBIR_SIMD8 + stbir__simdi i; stbir__simdi8 o0,o1; + stbir__simdf8 of0, of1; + STBIR_NO_UNROLL(decode); + stbir__simdi_load( i, input ); + stbir__simdi8_expand_u8_to_u32( o0, o1, i ); + stbir__simdi8_convert_i32_to_float( of0, o0 ); + stbir__simdi8_convert_i32_to_float( of1, o1 ); + stbir__decode_simdf8_flip( of0 ); + stbir__decode_simdf8_flip( of1 ); + stbir__simdf8_store( decode + 0, of0 ); + stbir__simdf8_store( decode + 8, of1 ); + #else + stbir__simdi i, o0, o1, o2, o3; + stbir__simdf of0, of1, of2, of3; + STBIR_NO_UNROLL(decode); + stbir__simdi_load( i, input ); + stbir__simdi_expand_u8_to_u32( o0,o1,o2,o3,i); + stbir__simdi_convert_i32_to_float( of0, o0 ); + stbir__simdi_convert_i32_to_float( of1, o1 ); + stbir__simdi_convert_i32_to_float( of2, o2 ); + stbir__simdi_convert_i32_to_float( of3, o3 ); + stbir__decode_simdf4_flip( of0 ); + stbir__decode_simdf4_flip( of1 ); + stbir__decode_simdf4_flip( of2 ); + stbir__decode_simdf4_flip( of3 ); + stbir__simdf_store( decode + 0, of0 ); + stbir__simdf_store( decode + 4, of1 ); + stbir__simdf_store( decode + 8, of2 ); + stbir__simdf_store( decode + 12, of3 ); +#endif + decode += 16; + input += 16; + if ( decode <= decode_end ) + continue; + if ( decode == ( decode_end + 16 ) ) + break; + decode = decode_end; // backup and do last couple + input = end_input_m16; + } + return decode_end + 16; + } + #endif + + // try to do blocks of 4 when you can + #if stbir__coder_min_num != 3 // doesn't divide cleanly by four + decode += 4; + STBIR_SIMD_NO_UNROLL_LOOP_START + while( decode <= decode_end ) + { + STBIR_SIMD_NO_UNROLL(decode); + decode[0-4] = ((float)(input[stbir__decode_order0])); + decode[1-4] = ((float)(input[stbir__decode_order1])); + decode[2-4] = ((float)(input[stbir__decode_order2])); + decode[3-4] = ((float)(input[stbir__decode_order3])); + decode += 4; + input += 4; + } + decode -= 4; + #endif + + // do the remnants + #if stbir__coder_min_num < 4 + STBIR_NO_UNROLL_LOOP_START + while( decode < decode_end ) + { + STBIR_NO_UNROLL(decode); + decode[0] = ((float)(input[stbir__decode_order0])); + #if stbir__coder_min_num >= 2 + decode[1] = ((float)(input[stbir__decode_order1])); + #endif + #if stbir__coder_min_num >= 3 + decode[2] = ((float)(input[stbir__decode_order2])); + #endif + decode += stbir__coder_min_num; + input += stbir__coder_min_num; + } + #endif + return decode_end; +} + +static void STBIR__CODER_NAME( stbir__encode_uint8_linear )( void * outputp, int width_times_channels, float const * encode ) +{ + unsigned char STBIR_SIMD_STREAMOUT_PTR( * ) output = (unsigned char *) outputp; + unsigned char * end_output = ( (unsigned char *) output ) + width_times_channels; + + #ifdef STBIR_SIMD + if ( width_times_channels >= stbir__simdfX_float_count*2 ) + { + float const * end_encode_m8 = encode + width_times_channels - stbir__simdfX_float_count*2; + end_output -= stbir__simdfX_float_count*2; + STBIR_SIMD_NO_UNROLL_LOOP_START_INF_FOR + for(;;) + { + stbir__simdfX e0, e1; + stbir__simdi i; + STBIR_SIMD_NO_UNROLL(encode); + stbir__simdfX_add_mem( e0, STBIR_simd_point5X, encode ); + stbir__simdfX_add_mem( e1, STBIR_simd_point5X, encode+stbir__simdfX_float_count ); + stbir__encode_simdfX_unflip( e0 ); + stbir__encode_simdfX_unflip( e1 ); + #ifdef STBIR_SIMD8 + stbir__simdf8_pack_to_16bytes( i, e0, e1 ); + stbir__simdi_store( output, i ); + #else + stbir__simdf_pack_to_8bytes( i, e0, e1 ); + stbir__simdi_store2( output, i ); + #endif + encode += stbir__simdfX_float_count*2; + output += stbir__simdfX_float_count*2; + if ( output <= end_output ) + continue; + if ( output == ( end_output + stbir__simdfX_float_count*2 ) ) + break; + output = end_output; // backup and do last couple + encode = end_encode_m8; + } + return; + } + + // try to do blocks of 4 when you can + #if stbir__coder_min_num != 3 // doesn't divide cleanly by four + output += 4; + STBIR_NO_UNROLL_LOOP_START + while( output <= end_output ) + { + stbir__simdf e0; + stbir__simdi i0; + STBIR_NO_UNROLL(encode); + stbir__simdf_load( e0, encode ); + stbir__simdf_add( e0, STBIR__CONSTF(STBIR_simd_point5), e0 ); + stbir__encode_simdf4_unflip( e0 ); + stbir__simdf_pack_to_8bytes( i0, e0, e0 ); // only use first 4 + *(int*)(output-4) = stbir__simdi_to_int( i0 ); + output += 4; + encode += 4; + } + output -= 4; + #endif + + #else + + // try to do blocks of 4 when you can + #if stbir__coder_min_num != 3 // doesn't divide cleanly by four + output += 4; + while( output <= end_output ) + { + float f; + f = encode[stbir__encode_order0] + 0.5f; STBIR_CLAMP(f, 0, 255); output[0-4] = (unsigned char)f; + f = encode[stbir__encode_order1] + 0.5f; STBIR_CLAMP(f, 0, 255); output[1-4] = (unsigned char)f; + f = encode[stbir__encode_order2] + 0.5f; STBIR_CLAMP(f, 0, 255); output[2-4] = (unsigned char)f; + f = encode[stbir__encode_order3] + 0.5f; STBIR_CLAMP(f, 0, 255); output[3-4] = (unsigned char)f; + output += 4; + encode += 4; + } + output -= 4; + #endif + + #endif + + // do the remnants + #if stbir__coder_min_num < 4 + STBIR_NO_UNROLL_LOOP_START + while( output < end_output ) + { + float f; + STBIR_NO_UNROLL(encode); + f = encode[stbir__encode_order0] + 0.5f; STBIR_CLAMP(f, 0, 255); output[0] = (unsigned char)f; + #if stbir__coder_min_num >= 2 + f = encode[stbir__encode_order1] + 0.5f; STBIR_CLAMP(f, 0, 255); output[1] = (unsigned char)f; + #endif + #if stbir__coder_min_num >= 3 + f = encode[stbir__encode_order2] + 0.5f; STBIR_CLAMP(f, 0, 255); output[2] = (unsigned char)f; + #endif + output += stbir__coder_min_num; + encode += stbir__coder_min_num; + } + #endif +} + +static float * STBIR__CODER_NAME(stbir__decode_uint8_srgb)( float * decodep, int width_times_channels, void const * inputp ) +{ + float STBIR_STREAMOUT_PTR( * ) decode = decodep; + float * decode_end = (float*) decode + width_times_channels; + unsigned char const * input = (unsigned char const *)inputp; + + // try to do blocks of 4 when you can + #if stbir__coder_min_num != 3 // doesn't divide cleanly by four + decode += 4; + while( decode <= decode_end ) + { + decode[0-4] = stbir__srgb_uchar_to_linear_float[ input[ stbir__decode_order0 ] ]; + decode[1-4] = stbir__srgb_uchar_to_linear_float[ input[ stbir__decode_order1 ] ]; + decode[2-4] = stbir__srgb_uchar_to_linear_float[ input[ stbir__decode_order2 ] ]; + decode[3-4] = stbir__srgb_uchar_to_linear_float[ input[ stbir__decode_order3 ] ]; + decode += 4; + input += 4; + } + decode -= 4; + #endif + + // do the remnants + #if stbir__coder_min_num < 4 + STBIR_NO_UNROLL_LOOP_START + while( decode < decode_end ) + { + STBIR_NO_UNROLL(decode); + decode[0] = stbir__srgb_uchar_to_linear_float[ input[ stbir__decode_order0 ] ]; + #if stbir__coder_min_num >= 2 + decode[1] = stbir__srgb_uchar_to_linear_float[ input[ stbir__decode_order1 ] ]; + #endif + #if stbir__coder_min_num >= 3 + decode[2] = stbir__srgb_uchar_to_linear_float[ input[ stbir__decode_order2 ] ]; + #endif + decode += stbir__coder_min_num; + input += stbir__coder_min_num; + } + #endif + return decode_end; +} + +#define stbir__min_max_shift20( i, f ) \ + stbir__simdf_max( f, f, stbir_simdf_casti(STBIR__CONSTI( STBIR_almost_zero )) ); \ + stbir__simdf_min( f, f, stbir_simdf_casti(STBIR__CONSTI( STBIR_almost_one )) ); \ + stbir__simdi_32shr( i, stbir_simdi_castf( f ), 20 ); + +#define stbir__scale_and_convert( i, f ) \ + stbir__simdf_madd( f, STBIR__CONSTF( STBIR_simd_point5 ), STBIR__CONSTF( STBIR_max_uint8_as_float ), f ); \ + stbir__simdf_max( f, f, stbir__simdf_zeroP() ); \ + stbir__simdf_min( f, f, STBIR__CONSTF( STBIR_max_uint8_as_float ) ); \ + stbir__simdf_convert_float_to_i32( i, f ); + +#define stbir__linear_to_srgb_finish( i, f ) \ +{ \ + stbir__simdi temp; \ + stbir__simdi_32shr( temp, stbir_simdi_castf( f ), 12 ) ; \ + stbir__simdi_and( temp, temp, STBIR__CONSTI(STBIR_mantissa_mask) ); \ + stbir__simdi_or( temp, temp, STBIR__CONSTI(STBIR_topscale) ); \ + stbir__simdi_16madd( i, i, temp ); \ + stbir__simdi_32shr( i, i, 16 ); \ +} + +#define stbir__simdi_table_lookup2( v0,v1, table ) \ +{ \ + stbir__simdi_u32 temp0,temp1; \ + temp0.m128i_i128 = v0; \ + temp1.m128i_i128 = v1; \ + temp0.m128i_u32[0] = table[temp0.m128i_i32[0]]; temp0.m128i_u32[1] = table[temp0.m128i_i32[1]]; temp0.m128i_u32[2] = table[temp0.m128i_i32[2]]; temp0.m128i_u32[3] = table[temp0.m128i_i32[3]]; \ + temp1.m128i_u32[0] = table[temp1.m128i_i32[0]]; temp1.m128i_u32[1] = table[temp1.m128i_i32[1]]; temp1.m128i_u32[2] = table[temp1.m128i_i32[2]]; temp1.m128i_u32[3] = table[temp1.m128i_i32[3]]; \ + v0 = temp0.m128i_i128; \ + v1 = temp1.m128i_i128; \ +} + +#define stbir__simdi_table_lookup3( v0,v1,v2, table ) \ +{ \ + stbir__simdi_u32 temp0,temp1,temp2; \ + temp0.m128i_i128 = v0; \ + temp1.m128i_i128 = v1; \ + temp2.m128i_i128 = v2; \ + temp0.m128i_u32[0] = table[temp0.m128i_i32[0]]; temp0.m128i_u32[1] = table[temp0.m128i_i32[1]]; temp0.m128i_u32[2] = table[temp0.m128i_i32[2]]; temp0.m128i_u32[3] = table[temp0.m128i_i32[3]]; \ + temp1.m128i_u32[0] = table[temp1.m128i_i32[0]]; temp1.m128i_u32[1] = table[temp1.m128i_i32[1]]; temp1.m128i_u32[2] = table[temp1.m128i_i32[2]]; temp1.m128i_u32[3] = table[temp1.m128i_i32[3]]; \ + temp2.m128i_u32[0] = table[temp2.m128i_i32[0]]; temp2.m128i_u32[1] = table[temp2.m128i_i32[1]]; temp2.m128i_u32[2] = table[temp2.m128i_i32[2]]; temp2.m128i_u32[3] = table[temp2.m128i_i32[3]]; \ + v0 = temp0.m128i_i128; \ + v1 = temp1.m128i_i128; \ + v2 = temp2.m128i_i128; \ +} + +#define stbir__simdi_table_lookup4( v0,v1,v2,v3, table ) \ +{ \ + stbir__simdi_u32 temp0,temp1,temp2,temp3; \ + temp0.m128i_i128 = v0; \ + temp1.m128i_i128 = v1; \ + temp2.m128i_i128 = v2; \ + temp3.m128i_i128 = v3; \ + temp0.m128i_u32[0] = table[temp0.m128i_i32[0]]; temp0.m128i_u32[1] = table[temp0.m128i_i32[1]]; temp0.m128i_u32[2] = table[temp0.m128i_i32[2]]; temp0.m128i_u32[3] = table[temp0.m128i_i32[3]]; \ + temp1.m128i_u32[0] = table[temp1.m128i_i32[0]]; temp1.m128i_u32[1] = table[temp1.m128i_i32[1]]; temp1.m128i_u32[2] = table[temp1.m128i_i32[2]]; temp1.m128i_u32[3] = table[temp1.m128i_i32[3]]; \ + temp2.m128i_u32[0] = table[temp2.m128i_i32[0]]; temp2.m128i_u32[1] = table[temp2.m128i_i32[1]]; temp2.m128i_u32[2] = table[temp2.m128i_i32[2]]; temp2.m128i_u32[3] = table[temp2.m128i_i32[3]]; \ + temp3.m128i_u32[0] = table[temp3.m128i_i32[0]]; temp3.m128i_u32[1] = table[temp3.m128i_i32[1]]; temp3.m128i_u32[2] = table[temp3.m128i_i32[2]]; temp3.m128i_u32[3] = table[temp3.m128i_i32[3]]; \ + v0 = temp0.m128i_i128; \ + v1 = temp1.m128i_i128; \ + v2 = temp2.m128i_i128; \ + v3 = temp3.m128i_i128; \ +} + +static void STBIR__CODER_NAME( stbir__encode_uint8_srgb )( void * outputp, int width_times_channels, float const * encode ) +{ + unsigned char STBIR_SIMD_STREAMOUT_PTR( * ) output = (unsigned char*) outputp; + unsigned char * end_output = ( (unsigned char*) output ) + width_times_channels; + + #ifdef STBIR_SIMD + + if ( width_times_channels >= 16 ) + { + float const * end_encode_m16 = encode + width_times_channels - 16; + end_output -= 16; + STBIR_SIMD_NO_UNROLL_LOOP_START_INF_FOR + for(;;) + { + stbir__simdf f0, f1, f2, f3; + stbir__simdi i0, i1, i2, i3; + STBIR_SIMD_NO_UNROLL(encode); + + stbir__simdf_load4_transposed( f0, f1, f2, f3, encode ); + + stbir__min_max_shift20( i0, f0 ); + stbir__min_max_shift20( i1, f1 ); + stbir__min_max_shift20( i2, f2 ); + stbir__min_max_shift20( i3, f3 ); + + stbir__simdi_table_lookup4( i0, i1, i2, i3, ( fp32_to_srgb8_tab4 - (127-13)*8 ) ); + + stbir__linear_to_srgb_finish( i0, f0 ); + stbir__linear_to_srgb_finish( i1, f1 ); + stbir__linear_to_srgb_finish( i2, f2 ); + stbir__linear_to_srgb_finish( i3, f3 ); + + stbir__interleave_pack_and_store_16_u8( output, STBIR_strs_join1(i, ,stbir__encode_order0), STBIR_strs_join1(i, ,stbir__encode_order1), STBIR_strs_join1(i, ,stbir__encode_order2), STBIR_strs_join1(i, ,stbir__encode_order3) ); + + encode += 16; + output += 16; + if ( output <= end_output ) + continue; + if ( output == ( end_output + 16 ) ) + break; + output = end_output; // backup and do last couple + encode = end_encode_m16; + } + return; + } + #endif + + // try to do blocks of 4 when you can + #if stbir__coder_min_num != 3 // doesn't divide cleanly by four + output += 4; + STBIR_SIMD_NO_UNROLL_LOOP_START + while ( output <= end_output ) + { + STBIR_SIMD_NO_UNROLL(encode); + + output[0-4] = stbir__linear_to_srgb_uchar( encode[stbir__encode_order0] ); + output[1-4] = stbir__linear_to_srgb_uchar( encode[stbir__encode_order1] ); + output[2-4] = stbir__linear_to_srgb_uchar( encode[stbir__encode_order2] ); + output[3-4] = stbir__linear_to_srgb_uchar( encode[stbir__encode_order3] ); + + output += 4; + encode += 4; + } + output -= 4; + #endif + + // do the remnants + #if stbir__coder_min_num < 4 + STBIR_NO_UNROLL_LOOP_START + while( output < end_output ) + { + STBIR_NO_UNROLL(encode); + output[0] = stbir__linear_to_srgb_uchar( encode[stbir__encode_order0] ); + #if stbir__coder_min_num >= 2 + output[1] = stbir__linear_to_srgb_uchar( encode[stbir__encode_order1] ); + #endif + #if stbir__coder_min_num >= 3 + output[2] = stbir__linear_to_srgb_uchar( encode[stbir__encode_order2] ); + #endif + output += stbir__coder_min_num; + encode += stbir__coder_min_num; + } + #endif +} + +#if ( stbir__coder_min_num == 4 ) || ( ( stbir__coder_min_num == 1 ) && ( !defined(stbir__decode_swizzle) ) ) + +static float * STBIR__CODER_NAME(stbir__decode_uint8_srgb4_linearalpha)( float * decodep, int width_times_channels, void const * inputp ) +{ + float STBIR_STREAMOUT_PTR( * ) decode = decodep; + float * decode_end = (float*) decode + width_times_channels; + unsigned char const * input = (unsigned char const *)inputp; + + do { + decode[0] = stbir__srgb_uchar_to_linear_float[ input[stbir__decode_order0] ]; + decode[1] = stbir__srgb_uchar_to_linear_float[ input[stbir__decode_order1] ]; + decode[2] = stbir__srgb_uchar_to_linear_float[ input[stbir__decode_order2] ]; + decode[3] = ( (float) input[stbir__decode_order3] ) * stbir__max_uint8_as_float_inverted; + input += 4; + decode += 4; + } while( decode < decode_end ); + return decode_end; +} + + +static void STBIR__CODER_NAME( stbir__encode_uint8_srgb4_linearalpha )( void * outputp, int width_times_channels, float const * encode ) +{ + unsigned char STBIR_SIMD_STREAMOUT_PTR( * ) output = (unsigned char*) outputp; + unsigned char * end_output = ( (unsigned char*) output ) + width_times_channels; + + #ifdef STBIR_SIMD + + if ( width_times_channels >= 16 ) + { + float const * end_encode_m16 = encode + width_times_channels - 16; + end_output -= 16; + STBIR_SIMD_NO_UNROLL_LOOP_START_INF_FOR + for(;;) + { + stbir__simdf f0, f1, f2, f3; + stbir__simdi i0, i1, i2, i3; + + STBIR_SIMD_NO_UNROLL(encode); + stbir__simdf_load4_transposed( f0, f1, f2, f3, encode ); + + stbir__min_max_shift20( i0, f0 ); + stbir__min_max_shift20( i1, f1 ); + stbir__min_max_shift20( i2, f2 ); + stbir__scale_and_convert( i3, f3 ); + + stbir__simdi_table_lookup3( i0, i1, i2, ( fp32_to_srgb8_tab4 - (127-13)*8 ) ); + + stbir__linear_to_srgb_finish( i0, f0 ); + stbir__linear_to_srgb_finish( i1, f1 ); + stbir__linear_to_srgb_finish( i2, f2 ); + + stbir__interleave_pack_and_store_16_u8( output, STBIR_strs_join1(i, ,stbir__encode_order0), STBIR_strs_join1(i, ,stbir__encode_order1), STBIR_strs_join1(i, ,stbir__encode_order2), STBIR_strs_join1(i, ,stbir__encode_order3) ); + + output += 16; + encode += 16; + + if ( output <= end_output ) + continue; + if ( output == ( end_output + 16 ) ) + break; + output = end_output; // backup and do last couple + encode = end_encode_m16; + } + return; + } + #endif + + STBIR_SIMD_NO_UNROLL_LOOP_START + do { + float f; + STBIR_SIMD_NO_UNROLL(encode); + + output[stbir__decode_order0] = stbir__linear_to_srgb_uchar( encode[0] ); + output[stbir__decode_order1] = stbir__linear_to_srgb_uchar( encode[1] ); + output[stbir__decode_order2] = stbir__linear_to_srgb_uchar( encode[2] ); + + f = encode[3] * stbir__max_uint8_as_float + 0.5f; + STBIR_CLAMP(f, 0, 255); + output[stbir__decode_order3] = (unsigned char) f; + + output += 4; + encode += 4; + } while( output < end_output ); +} + +#endif + +#if ( stbir__coder_min_num == 2 ) || ( ( stbir__coder_min_num == 1 ) && ( !defined(stbir__decode_swizzle) ) ) + +static float * STBIR__CODER_NAME(stbir__decode_uint8_srgb2_linearalpha)( float * decodep, int width_times_channels, void const * inputp ) +{ + float STBIR_STREAMOUT_PTR( * ) decode = decodep; + float * decode_end = (float*) decode + width_times_channels; + unsigned char const * input = (unsigned char const *)inputp; + + decode += 4; + while( decode <= decode_end ) + { + decode[0-4] = stbir__srgb_uchar_to_linear_float[ input[stbir__decode_order0] ]; + decode[1-4] = ( (float) input[stbir__decode_order1] ) * stbir__max_uint8_as_float_inverted; + decode[2-4] = stbir__srgb_uchar_to_linear_float[ input[stbir__decode_order0+2] ]; + decode[3-4] = ( (float) input[stbir__decode_order1+2] ) * stbir__max_uint8_as_float_inverted; + input += 4; + decode += 4; + } + decode -= 4; + if( decode < decode_end ) + { + decode[0] = stbir__srgb_uchar_to_linear_float[ input[stbir__decode_order0] ]; + decode[1] = ( (float) input[stbir__decode_order1] ) * stbir__max_uint8_as_float_inverted; + } + return decode_end; +} + +static void STBIR__CODER_NAME( stbir__encode_uint8_srgb2_linearalpha )( void * outputp, int width_times_channels, float const * encode ) +{ + unsigned char STBIR_SIMD_STREAMOUT_PTR( * ) output = (unsigned char*) outputp; + unsigned char * end_output = ( (unsigned char*) output ) + width_times_channels; + + #ifdef STBIR_SIMD + + if ( width_times_channels >= 16 ) + { + float const * end_encode_m16 = encode + width_times_channels - 16; + end_output -= 16; + STBIR_SIMD_NO_UNROLL_LOOP_START_INF_FOR + for(;;) + { + stbir__simdf f0, f1, f2, f3; + stbir__simdi i0, i1, i2, i3; + + STBIR_SIMD_NO_UNROLL(encode); + stbir__simdf_load4_transposed( f0, f1, f2, f3, encode ); + + stbir__min_max_shift20( i0, f0 ); + stbir__scale_and_convert( i1, f1 ); + stbir__min_max_shift20( i2, f2 ); + stbir__scale_and_convert( i3, f3 ); + + stbir__simdi_table_lookup2( i0, i2, ( fp32_to_srgb8_tab4 - (127-13)*8 ) ); + + stbir__linear_to_srgb_finish( i0, f0 ); + stbir__linear_to_srgb_finish( i2, f2 ); + + stbir__interleave_pack_and_store_16_u8( output, STBIR_strs_join1(i, ,stbir__encode_order0), STBIR_strs_join1(i, ,stbir__encode_order1), STBIR_strs_join1(i, ,stbir__encode_order2), STBIR_strs_join1(i, ,stbir__encode_order3) ); + + output += 16; + encode += 16; + if ( output <= end_output ) + continue; + if ( output == ( end_output + 16 ) ) + break; + output = end_output; // backup and do last couple + encode = end_encode_m16; + } + return; + } + #endif + + STBIR_SIMD_NO_UNROLL_LOOP_START + do { + float f; + STBIR_SIMD_NO_UNROLL(encode); + + output[stbir__decode_order0] = stbir__linear_to_srgb_uchar( encode[0] ); + + f = encode[1] * stbir__max_uint8_as_float + 0.5f; + STBIR_CLAMP(f, 0, 255); + output[stbir__decode_order1] = (unsigned char) f; + + output += 2; + encode += 2; + } while( output < end_output ); +} + +#endif + +static float * STBIR__CODER_NAME(stbir__decode_uint16_linear_scaled)( float * decodep, int width_times_channels, void const * inputp ) +{ + float STBIR_STREAMOUT_PTR( * ) decode = decodep; + float * decode_end = (float*) decode + width_times_channels; + unsigned short const * input = (unsigned short const *)inputp; + + #ifdef STBIR_SIMD + unsigned short const * end_input_m8 = input + width_times_channels - 8; + if ( width_times_channels >= 8 ) + { + decode_end -= 8; + STBIR_NO_UNROLL_LOOP_START_INF_FOR + for(;;) + { + #ifdef STBIR_SIMD8 + stbir__simdi i; stbir__simdi8 o; + stbir__simdf8 of; + STBIR_NO_UNROLL(decode); + stbir__simdi_load( i, input ); + stbir__simdi8_expand_u16_to_u32( o, i ); + stbir__simdi8_convert_i32_to_float( of, o ); + stbir__simdf8_mult( of, of, STBIR_max_uint16_as_float_inverted8); + stbir__decode_simdf8_flip( of ); + stbir__simdf8_store( decode + 0, of ); + #else + stbir__simdi i, o0, o1; + stbir__simdf of0, of1; + STBIR_NO_UNROLL(decode); + stbir__simdi_load( i, input ); + stbir__simdi_expand_u16_to_u32( o0,o1,i ); + stbir__simdi_convert_i32_to_float( of0, o0 ); + stbir__simdi_convert_i32_to_float( of1, o1 ); + stbir__simdf_mult( of0, of0, STBIR__CONSTF(STBIR_max_uint16_as_float_inverted) ); + stbir__simdf_mult( of1, of1, STBIR__CONSTF(STBIR_max_uint16_as_float_inverted)); + stbir__decode_simdf4_flip( of0 ); + stbir__decode_simdf4_flip( of1 ); + stbir__simdf_store( decode + 0, of0 ); + stbir__simdf_store( decode + 4, of1 ); + #endif + decode += 8; + input += 8; + if ( decode <= decode_end ) + continue; + if ( decode == ( decode_end + 8 ) ) + break; + decode = decode_end; // backup and do last couple + input = end_input_m8; + } + return decode_end + 8; + } + #endif + + // try to do blocks of 4 when you can + #if stbir__coder_min_num != 3 // doesn't divide cleanly by four + decode += 4; + STBIR_SIMD_NO_UNROLL_LOOP_START + while( decode <= decode_end ) + { + STBIR_SIMD_NO_UNROLL(decode); + decode[0-4] = ((float)(input[stbir__decode_order0])) * stbir__max_uint16_as_float_inverted; + decode[1-4] = ((float)(input[stbir__decode_order1])) * stbir__max_uint16_as_float_inverted; + decode[2-4] = ((float)(input[stbir__decode_order2])) * stbir__max_uint16_as_float_inverted; + decode[3-4] = ((float)(input[stbir__decode_order3])) * stbir__max_uint16_as_float_inverted; + decode += 4; + input += 4; + } + decode -= 4; + #endif + + // do the remnants + #if stbir__coder_min_num < 4 + STBIR_NO_UNROLL_LOOP_START + while( decode < decode_end ) + { + STBIR_NO_UNROLL(decode); + decode[0] = ((float)(input[stbir__decode_order0])) * stbir__max_uint16_as_float_inverted; + #if stbir__coder_min_num >= 2 + decode[1] = ((float)(input[stbir__decode_order1])) * stbir__max_uint16_as_float_inverted; + #endif + #if stbir__coder_min_num >= 3 + decode[2] = ((float)(input[stbir__decode_order2])) * stbir__max_uint16_as_float_inverted; + #endif + decode += stbir__coder_min_num; + input += stbir__coder_min_num; + } + #endif + return decode_end; +} + + +static void STBIR__CODER_NAME(stbir__encode_uint16_linear_scaled)( void * outputp, int width_times_channels, float const * encode ) +{ + unsigned short STBIR_SIMD_STREAMOUT_PTR( * ) output = (unsigned short*) outputp; + unsigned short * end_output = ( (unsigned short*) output ) + width_times_channels; + + #ifdef STBIR_SIMD + { + if ( width_times_channels >= stbir__simdfX_float_count*2 ) + { + float const * end_encode_m8 = encode + width_times_channels - stbir__simdfX_float_count*2; + end_output -= stbir__simdfX_float_count*2; + STBIR_SIMD_NO_UNROLL_LOOP_START_INF_FOR + for(;;) + { + stbir__simdfX e0, e1; + stbir__simdiX i; + STBIR_SIMD_NO_UNROLL(encode); + stbir__simdfX_madd_mem( e0, STBIR_simd_point5X, STBIR_max_uint16_as_floatX, encode ); + stbir__simdfX_madd_mem( e1, STBIR_simd_point5X, STBIR_max_uint16_as_floatX, encode+stbir__simdfX_float_count ); + stbir__encode_simdfX_unflip( e0 ); + stbir__encode_simdfX_unflip( e1 ); + stbir__simdfX_pack_to_words( i, e0, e1 ); + stbir__simdiX_store( output, i ); + encode += stbir__simdfX_float_count*2; + output += stbir__simdfX_float_count*2; + if ( output <= end_output ) + continue; + if ( output == ( end_output + stbir__simdfX_float_count*2 ) ) + break; + output = end_output; // backup and do last couple + encode = end_encode_m8; + } + return; + } + } + + // try to do blocks of 4 when you can + #if stbir__coder_min_num != 3 // doesn't divide cleanly by four + output += 4; + STBIR_NO_UNROLL_LOOP_START + while( output <= end_output ) + { + stbir__simdf e; + stbir__simdi i; + STBIR_NO_UNROLL(encode); + stbir__simdf_load( e, encode ); + stbir__simdf_madd( e, STBIR__CONSTF(STBIR_simd_point5), STBIR__CONSTF(STBIR_max_uint16_as_float), e ); + stbir__encode_simdf4_unflip( e ); + stbir__simdf_pack_to_8words( i, e, e ); // only use first 4 + stbir__simdi_store2( output-4, i ); + output += 4; + encode += 4; + } + output -= 4; + #endif + + // do the remnants + #if stbir__coder_min_num < 4 + STBIR_NO_UNROLL_LOOP_START + while( output < end_output ) + { + stbir__simdf e; + STBIR_NO_UNROLL(encode); + stbir__simdf_madd1_mem( e, STBIR__CONSTF(STBIR_simd_point5), STBIR__CONSTF(STBIR_max_uint16_as_float), encode+stbir__encode_order0 ); output[0] = stbir__simdf_convert_float_to_short( e ); + #if stbir__coder_min_num >= 2 + stbir__simdf_madd1_mem( e, STBIR__CONSTF(STBIR_simd_point5), STBIR__CONSTF(STBIR_max_uint16_as_float), encode+stbir__encode_order1 ); output[1] = stbir__simdf_convert_float_to_short( e ); + #endif + #if stbir__coder_min_num >= 3 + stbir__simdf_madd1_mem( e, STBIR__CONSTF(STBIR_simd_point5), STBIR__CONSTF(STBIR_max_uint16_as_float), encode+stbir__encode_order2 ); output[2] = stbir__simdf_convert_float_to_short( e ); + #endif + output += stbir__coder_min_num; + encode += stbir__coder_min_num; + } + #endif + + #else + + // try to do blocks of 4 when you can + #if stbir__coder_min_num != 3 // doesn't divide cleanly by four + output += 4; + STBIR_SIMD_NO_UNROLL_LOOP_START + while( output <= end_output ) + { + float f; + STBIR_SIMD_NO_UNROLL(encode); + f = encode[stbir__encode_order0] * stbir__max_uint16_as_float + 0.5f; STBIR_CLAMP(f, 0, 65535); output[0-4] = (unsigned short)f; + f = encode[stbir__encode_order1] * stbir__max_uint16_as_float + 0.5f; STBIR_CLAMP(f, 0, 65535); output[1-4] = (unsigned short)f; + f = encode[stbir__encode_order2] * stbir__max_uint16_as_float + 0.5f; STBIR_CLAMP(f, 0, 65535); output[2-4] = (unsigned short)f; + f = encode[stbir__encode_order3] * stbir__max_uint16_as_float + 0.5f; STBIR_CLAMP(f, 0, 65535); output[3-4] = (unsigned short)f; + output += 4; + encode += 4; + } + output -= 4; + #endif + + // do the remnants + #if stbir__coder_min_num < 4 + STBIR_NO_UNROLL_LOOP_START + while( output < end_output ) + { + float f; + STBIR_NO_UNROLL(encode); + f = encode[stbir__encode_order0] * stbir__max_uint16_as_float + 0.5f; STBIR_CLAMP(f, 0, 65535); output[0] = (unsigned short)f; + #if stbir__coder_min_num >= 2 + f = encode[stbir__encode_order1] * stbir__max_uint16_as_float + 0.5f; STBIR_CLAMP(f, 0, 65535); output[1] = (unsigned short)f; + #endif + #if stbir__coder_min_num >= 3 + f = encode[stbir__encode_order2] * stbir__max_uint16_as_float + 0.5f; STBIR_CLAMP(f, 0, 65535); output[2] = (unsigned short)f; + #endif + output += stbir__coder_min_num; + encode += stbir__coder_min_num; + } + #endif + #endif +} + +static float * STBIR__CODER_NAME(stbir__decode_uint16_linear)( float * decodep, int width_times_channels, void const * inputp ) +{ + float STBIR_STREAMOUT_PTR( * ) decode = decodep; + float * decode_end = (float*) decode + width_times_channels; + unsigned short const * input = (unsigned short const *)inputp; + + #ifdef STBIR_SIMD + unsigned short const * end_input_m8 = input + width_times_channels - 8; + if ( width_times_channels >= 8 ) + { + decode_end -= 8; + STBIR_NO_UNROLL_LOOP_START_INF_FOR + for(;;) + { + #ifdef STBIR_SIMD8 + stbir__simdi i; stbir__simdi8 o; + stbir__simdf8 of; + STBIR_NO_UNROLL(decode); + stbir__simdi_load( i, input ); + stbir__simdi8_expand_u16_to_u32( o, i ); + stbir__simdi8_convert_i32_to_float( of, o ); + stbir__decode_simdf8_flip( of ); + stbir__simdf8_store( decode + 0, of ); + #else + stbir__simdi i, o0, o1; + stbir__simdf of0, of1; + STBIR_NO_UNROLL(decode); + stbir__simdi_load( i, input ); + stbir__simdi_expand_u16_to_u32( o0, o1, i ); + stbir__simdi_convert_i32_to_float( of0, o0 ); + stbir__simdi_convert_i32_to_float( of1, o1 ); + stbir__decode_simdf4_flip( of0 ); + stbir__decode_simdf4_flip( of1 ); + stbir__simdf_store( decode + 0, of0 ); + stbir__simdf_store( decode + 4, of1 ); + #endif + decode += 8; + input += 8; + if ( decode <= decode_end ) + continue; + if ( decode == ( decode_end + 8 ) ) + break; + decode = decode_end; // backup and do last couple + input = end_input_m8; + } + return decode_end + 8; + } + #endif + + // try to do blocks of 4 when you can + #if stbir__coder_min_num != 3 // doesn't divide cleanly by four + decode += 4; + STBIR_SIMD_NO_UNROLL_LOOP_START + while( decode <= decode_end ) + { + STBIR_SIMD_NO_UNROLL(decode); + decode[0-4] = ((float)(input[stbir__decode_order0])); + decode[1-4] = ((float)(input[stbir__decode_order1])); + decode[2-4] = ((float)(input[stbir__decode_order2])); + decode[3-4] = ((float)(input[stbir__decode_order3])); + decode += 4; + input += 4; + } + decode -= 4; + #endif + + // do the remnants + #if stbir__coder_min_num < 4 + STBIR_NO_UNROLL_LOOP_START + while( decode < decode_end ) + { + STBIR_NO_UNROLL(decode); + decode[0] = ((float)(input[stbir__decode_order0])); + #if stbir__coder_min_num >= 2 + decode[1] = ((float)(input[stbir__decode_order1])); + #endif + #if stbir__coder_min_num >= 3 + decode[2] = ((float)(input[stbir__decode_order2])); + #endif + decode += stbir__coder_min_num; + input += stbir__coder_min_num; + } + #endif + return decode_end; +} + +static void STBIR__CODER_NAME(stbir__encode_uint16_linear)( void * outputp, int width_times_channels, float const * encode ) +{ + unsigned short STBIR_SIMD_STREAMOUT_PTR( * ) output = (unsigned short*) outputp; + unsigned short * end_output = ( (unsigned short*) output ) + width_times_channels; + + #ifdef STBIR_SIMD + { + if ( width_times_channels >= stbir__simdfX_float_count*2 ) + { + float const * end_encode_m8 = encode + width_times_channels - stbir__simdfX_float_count*2; + end_output -= stbir__simdfX_float_count*2; + STBIR_SIMD_NO_UNROLL_LOOP_START_INF_FOR + for(;;) + { + stbir__simdfX e0, e1; + stbir__simdiX i; + STBIR_SIMD_NO_UNROLL(encode); + stbir__simdfX_add_mem( e0, STBIR_simd_point5X, encode ); + stbir__simdfX_add_mem( e1, STBIR_simd_point5X, encode+stbir__simdfX_float_count ); + stbir__encode_simdfX_unflip( e0 ); + stbir__encode_simdfX_unflip( e1 ); + stbir__simdfX_pack_to_words( i, e0, e1 ); + stbir__simdiX_store( output, i ); + encode += stbir__simdfX_float_count*2; + output += stbir__simdfX_float_count*2; + if ( output <= end_output ) + continue; + if ( output == ( end_output + stbir__simdfX_float_count*2 ) ) + break; + output = end_output; // backup and do last couple + encode = end_encode_m8; + } + return; + } + } + + // try to do blocks of 4 when you can + #if stbir__coder_min_num != 3 // doesn't divide cleanly by four + output += 4; + STBIR_NO_UNROLL_LOOP_START + while( output <= end_output ) + { + stbir__simdf e; + stbir__simdi i; + STBIR_NO_UNROLL(encode); + stbir__simdf_load( e, encode ); + stbir__simdf_add( e, STBIR__CONSTF(STBIR_simd_point5), e ); + stbir__encode_simdf4_unflip( e ); + stbir__simdf_pack_to_8words( i, e, e ); // only use first 4 + stbir__simdi_store2( output-4, i ); + output += 4; + encode += 4; + } + output -= 4; + #endif + + #else + + // try to do blocks of 4 when you can + #if stbir__coder_min_num != 3 // doesn't divide cleanly by four + output += 4; + STBIR_SIMD_NO_UNROLL_LOOP_START + while( output <= end_output ) + { + float f; + STBIR_SIMD_NO_UNROLL(encode); + f = encode[stbir__encode_order0] + 0.5f; STBIR_CLAMP(f, 0, 65535); output[0-4] = (unsigned short)f; + f = encode[stbir__encode_order1] + 0.5f; STBIR_CLAMP(f, 0, 65535); output[1-4] = (unsigned short)f; + f = encode[stbir__encode_order2] + 0.5f; STBIR_CLAMP(f, 0, 65535); output[2-4] = (unsigned short)f; + f = encode[stbir__encode_order3] + 0.5f; STBIR_CLAMP(f, 0, 65535); output[3-4] = (unsigned short)f; + output += 4; + encode += 4; + } + output -= 4; + #endif + + #endif + + // do the remnants + #if stbir__coder_min_num < 4 + STBIR_NO_UNROLL_LOOP_START + while( output < end_output ) + { + float f; + STBIR_NO_UNROLL(encode); + f = encode[stbir__encode_order0] + 0.5f; STBIR_CLAMP(f, 0, 65535); output[0] = (unsigned short)f; + #if stbir__coder_min_num >= 2 + f = encode[stbir__encode_order1] + 0.5f; STBIR_CLAMP(f, 0, 65535); output[1] = (unsigned short)f; + #endif + #if stbir__coder_min_num >= 3 + f = encode[stbir__encode_order2] + 0.5f; STBIR_CLAMP(f, 0, 65535); output[2] = (unsigned short)f; + #endif + output += stbir__coder_min_num; + encode += stbir__coder_min_num; + } + #endif +} + +static float * STBIR__CODER_NAME(stbir__decode_half_float_linear)( float * decodep, int width_times_channels, void const * inputp ) +{ + float STBIR_STREAMOUT_PTR( * ) decode = decodep; + float * decode_end = (float*) decode + width_times_channels; + stbir__FP16 const * input = (stbir__FP16 const *)inputp; + + #ifdef STBIR_SIMD + if ( width_times_channels >= 8 ) + { + stbir__FP16 const * end_input_m8 = input + width_times_channels - 8; + decode_end -= 8; + STBIR_NO_UNROLL_LOOP_START_INF_FOR + for(;;) + { + STBIR_NO_UNROLL(decode); + + stbir__half_to_float_SIMD( decode, input ); + #ifdef stbir__decode_swizzle + #ifdef STBIR_SIMD8 + { + stbir__simdf8 of; + stbir__simdf8_load( of, decode ); + stbir__decode_simdf8_flip( of ); + stbir__simdf8_store( decode, of ); + } + #else + { + stbir__simdf of0,of1; + stbir__simdf_load( of0, decode ); + stbir__simdf_load( of1, decode+4 ); + stbir__decode_simdf4_flip( of0 ); + stbir__decode_simdf4_flip( of1 ); + stbir__simdf_store( decode, of0 ); + stbir__simdf_store( decode+4, of1 ); + } + #endif + #endif + decode += 8; + input += 8; + if ( decode <= decode_end ) + continue; + if ( decode == ( decode_end + 8 ) ) + break; + decode = decode_end; // backup and do last couple + input = end_input_m8; + } + return decode_end + 8; + } + #endif + + // try to do blocks of 4 when you can + #if stbir__coder_min_num != 3 // doesn't divide cleanly by four + decode += 4; + STBIR_SIMD_NO_UNROLL_LOOP_START + while( decode <= decode_end ) + { + STBIR_SIMD_NO_UNROLL(decode); + decode[0-4] = stbir__half_to_float(input[stbir__decode_order0]); + decode[1-4] = stbir__half_to_float(input[stbir__decode_order1]); + decode[2-4] = stbir__half_to_float(input[stbir__decode_order2]); + decode[3-4] = stbir__half_to_float(input[stbir__decode_order3]); + decode += 4; + input += 4; + } + decode -= 4; + #endif + + // do the remnants + #if stbir__coder_min_num < 4 + STBIR_NO_UNROLL_LOOP_START + while( decode < decode_end ) + { + STBIR_NO_UNROLL(decode); + decode[0] = stbir__half_to_float(input[stbir__decode_order0]); + #if stbir__coder_min_num >= 2 + decode[1] = stbir__half_to_float(input[stbir__decode_order1]); + #endif + #if stbir__coder_min_num >= 3 + decode[2] = stbir__half_to_float(input[stbir__decode_order2]); + #endif + decode += stbir__coder_min_num; + input += stbir__coder_min_num; + } + #endif + return decode_end; +} + +static void STBIR__CODER_NAME( stbir__encode_half_float_linear )( void * outputp, int width_times_channels, float const * encode ) +{ + stbir__FP16 STBIR_SIMD_STREAMOUT_PTR( * ) output = (stbir__FP16*) outputp; + stbir__FP16 * end_output = ( (stbir__FP16*) output ) + width_times_channels; + + #ifdef STBIR_SIMD + if ( width_times_channels >= 8 ) + { + float const * end_encode_m8 = encode + width_times_channels - 8; + end_output -= 8; + STBIR_SIMD_NO_UNROLL_LOOP_START_INF_FOR + for(;;) + { + STBIR_SIMD_NO_UNROLL(encode); + #ifdef stbir__decode_swizzle + #ifdef STBIR_SIMD8 + { + stbir__simdf8 of; + stbir__simdf8_load( of, encode ); + stbir__encode_simdf8_unflip( of ); + stbir__float_to_half_SIMD( output, (float*)&of ); + } + #else + { + stbir__simdf of[2]; + stbir__simdf_load( of[0], encode ); + stbir__simdf_load( of[1], encode+4 ); + stbir__encode_simdf4_unflip( of[0] ); + stbir__encode_simdf4_unflip( of[1] ); + stbir__float_to_half_SIMD( output, (float*)of ); + } + #endif + #else + stbir__float_to_half_SIMD( output, encode ); + #endif + encode += 8; + output += 8; + if ( output <= end_output ) + continue; + if ( output == ( end_output + 8 ) ) + break; + output = end_output; // backup and do last couple + encode = end_encode_m8; + } + return; + } + #endif + + // try to do blocks of 4 when you can + #if stbir__coder_min_num != 3 // doesn't divide cleanly by four + output += 4; + STBIR_SIMD_NO_UNROLL_LOOP_START + while( output <= end_output ) + { + STBIR_SIMD_NO_UNROLL(output); + output[0-4] = stbir__float_to_half(encode[stbir__encode_order0]); + output[1-4] = stbir__float_to_half(encode[stbir__encode_order1]); + output[2-4] = stbir__float_to_half(encode[stbir__encode_order2]); + output[3-4] = stbir__float_to_half(encode[stbir__encode_order3]); + output += 4; + encode += 4; + } + output -= 4; + #endif + + // do the remnants + #if stbir__coder_min_num < 4 + STBIR_NO_UNROLL_LOOP_START + while( output < end_output ) + { + STBIR_NO_UNROLL(output); + output[0] = stbir__float_to_half(encode[stbir__encode_order0]); + #if stbir__coder_min_num >= 2 + output[1] = stbir__float_to_half(encode[stbir__encode_order1]); + #endif + #if stbir__coder_min_num >= 3 + output[2] = stbir__float_to_half(encode[stbir__encode_order2]); + #endif + output += stbir__coder_min_num; + encode += stbir__coder_min_num; + } + #endif +} + +static float * STBIR__CODER_NAME(stbir__decode_float_linear)( float * decodep, int width_times_channels, void const * inputp ) +{ + #ifdef stbir__decode_swizzle + float STBIR_STREAMOUT_PTR( * ) decode = decodep; + float * decode_end = (float*) decode + width_times_channels; + float const * input = (float const *)inputp; + + #ifdef STBIR_SIMD + if ( width_times_channels >= 16 ) + { + float const * end_input_m16 = input + width_times_channels - 16; + decode_end -= 16; + STBIR_NO_UNROLL_LOOP_START_INF_FOR + for(;;) + { + STBIR_NO_UNROLL(decode); + #ifdef stbir__decode_swizzle + #ifdef STBIR_SIMD8 + { + stbir__simdf8 of0,of1; + stbir__simdf8_load( of0, input ); + stbir__simdf8_load( of1, input+8 ); + stbir__decode_simdf8_flip( of0 ); + stbir__decode_simdf8_flip( of1 ); + stbir__simdf8_store( decode, of0 ); + stbir__simdf8_store( decode+8, of1 ); + } + #else + { + stbir__simdf of0,of1,of2,of3; + stbir__simdf_load( of0, input ); + stbir__simdf_load( of1, input+4 ); + stbir__simdf_load( of2, input+8 ); + stbir__simdf_load( of3, input+12 ); + stbir__decode_simdf4_flip( of0 ); + stbir__decode_simdf4_flip( of1 ); + stbir__decode_simdf4_flip( of2 ); + stbir__decode_simdf4_flip( of3 ); + stbir__simdf_store( decode, of0 ); + stbir__simdf_store( decode+4, of1 ); + stbir__simdf_store( decode+8, of2 ); + stbir__simdf_store( decode+12, of3 ); + } + #endif + #endif + decode += 16; + input += 16; + if ( decode <= decode_end ) + continue; + if ( decode == ( decode_end + 16 ) ) + break; + decode = decode_end; // backup and do last couple + input = end_input_m16; + } + return decode_end + 16; + } + #endif + + // try to do blocks of 4 when you can + #if stbir__coder_min_num != 3 // doesn't divide cleanly by four + decode += 4; + STBIR_SIMD_NO_UNROLL_LOOP_START + while( decode <= decode_end ) + { + STBIR_SIMD_NO_UNROLL(decode); + decode[0-4] = input[stbir__decode_order0]; + decode[1-4] = input[stbir__decode_order1]; + decode[2-4] = input[stbir__decode_order2]; + decode[3-4] = input[stbir__decode_order3]; + decode += 4; + input += 4; + } + decode -= 4; + #endif + + // do the remnants + #if stbir__coder_min_num < 4 + STBIR_NO_UNROLL_LOOP_START + while( decode < decode_end ) + { + STBIR_NO_UNROLL(decode); + decode[0] = input[stbir__decode_order0]; + #if stbir__coder_min_num >= 2 + decode[1] = input[stbir__decode_order1]; + #endif + #if stbir__coder_min_num >= 3 + decode[2] = input[stbir__decode_order2]; + #endif + decode += stbir__coder_min_num; + input += stbir__coder_min_num; + } + #endif + return decode_end; + + #else + + if ( (void*)decodep != inputp ) + STBIR_MEMCPY( decodep, inputp, width_times_channels * sizeof( float ) ); + + return decodep + width_times_channels; + + #endif +} + +static void STBIR__CODER_NAME( stbir__encode_float_linear )( void * outputp, int width_times_channels, float const * encode ) +{ + #if !defined( STBIR_FLOAT_HIGH_CLAMP ) && !defined(STBIR_FLOAT_LOW_CLAMP) && !defined(stbir__decode_swizzle) + + if ( (void*)outputp != (void*) encode ) + STBIR_MEMCPY( outputp, encode, width_times_channels * sizeof( float ) ); + + #else + + float STBIR_SIMD_STREAMOUT_PTR( * ) output = (float*) outputp; + float * end_output = ( (float*) output ) + width_times_channels; + + #ifdef STBIR_FLOAT_HIGH_CLAMP + #define stbir_scalar_hi_clamp( v ) if ( v > STBIR_FLOAT_HIGH_CLAMP ) v = STBIR_FLOAT_HIGH_CLAMP; + #else + #define stbir_scalar_hi_clamp( v ) + #endif + #ifdef STBIR_FLOAT_LOW_CLAMP + #define stbir_scalar_lo_clamp( v ) if ( v < STBIR_FLOAT_LOW_CLAMP ) v = STBIR_FLOAT_LOW_CLAMP; + #else + #define stbir_scalar_lo_clamp( v ) + #endif + + #ifdef STBIR_SIMD + + #ifdef STBIR_FLOAT_HIGH_CLAMP + const stbir__simdfX high_clamp = stbir__simdf_frepX(STBIR_FLOAT_HIGH_CLAMP); + #endif + #ifdef STBIR_FLOAT_LOW_CLAMP + const stbir__simdfX low_clamp = stbir__simdf_frepX(STBIR_FLOAT_LOW_CLAMP); + #endif + + if ( width_times_channels >= ( stbir__simdfX_float_count * 2 ) ) + { + float const * end_encode_m8 = encode + width_times_channels - ( stbir__simdfX_float_count * 2 ); + end_output -= ( stbir__simdfX_float_count * 2 ); + STBIR_SIMD_NO_UNROLL_LOOP_START_INF_FOR + for(;;) + { + stbir__simdfX e0, e1; + STBIR_SIMD_NO_UNROLL(encode); + stbir__simdfX_load( e0, encode ); + stbir__simdfX_load( e1, encode+stbir__simdfX_float_count ); +#ifdef STBIR_FLOAT_HIGH_CLAMP + stbir__simdfX_min( e0, e0, high_clamp ); + stbir__simdfX_min( e1, e1, high_clamp ); +#endif +#ifdef STBIR_FLOAT_LOW_CLAMP + stbir__simdfX_max( e0, e0, low_clamp ); + stbir__simdfX_max( e1, e1, low_clamp ); +#endif + stbir__encode_simdfX_unflip( e0 ); + stbir__encode_simdfX_unflip( e1 ); + stbir__simdfX_store( output, e0 ); + stbir__simdfX_store( output+stbir__simdfX_float_count, e1 ); + encode += stbir__simdfX_float_count * 2; + output += stbir__simdfX_float_count * 2; + if ( output <= end_output ) + continue; + if ( output == ( end_output + ( stbir__simdfX_float_count * 2 ) ) ) + break; + output = end_output; // backup and do last couple + encode = end_encode_m8; + } + return; + } + + // try to do blocks of 4 when you can + #if stbir__coder_min_num != 3 // doesn't divide cleanly by four + output += 4; + STBIR_NO_UNROLL_LOOP_START + while( output <= end_output ) + { + stbir__simdf e0; + STBIR_NO_UNROLL(encode); + stbir__simdf_load( e0, encode ); +#ifdef STBIR_FLOAT_HIGH_CLAMP + stbir__simdf_min( e0, e0, high_clamp ); +#endif +#ifdef STBIR_FLOAT_LOW_CLAMP + stbir__simdf_max( e0, e0, low_clamp ); +#endif + stbir__encode_simdf4_unflip( e0 ); + stbir__simdf_store( output-4, e0 ); + output += 4; + encode += 4; + } + output -= 4; + #endif + + #else + + // try to do blocks of 4 when you can + #if stbir__coder_min_num != 3 // doesn't divide cleanly by four + output += 4; + STBIR_SIMD_NO_UNROLL_LOOP_START + while( output <= end_output ) + { + float e; + STBIR_SIMD_NO_UNROLL(encode); + e = encode[ stbir__encode_order0 ]; stbir_scalar_hi_clamp( e ); stbir_scalar_lo_clamp( e ); output[0-4] = e; + e = encode[ stbir__encode_order1 ]; stbir_scalar_hi_clamp( e ); stbir_scalar_lo_clamp( e ); output[1-4] = e; + e = encode[ stbir__encode_order2 ]; stbir_scalar_hi_clamp( e ); stbir_scalar_lo_clamp( e ); output[2-4] = e; + e = encode[ stbir__encode_order3 ]; stbir_scalar_hi_clamp( e ); stbir_scalar_lo_clamp( e ); output[3-4] = e; + output += 4; + encode += 4; + } + output -= 4; + + #endif + + #endif + + // do the remnants + #if stbir__coder_min_num < 4 + STBIR_NO_UNROLL_LOOP_START + while( output < end_output ) + { + float e; + STBIR_NO_UNROLL(encode); + e = encode[ stbir__encode_order0 ]; stbir_scalar_hi_clamp( e ); stbir_scalar_lo_clamp( e ); output[0] = e; + #if stbir__coder_min_num >= 2 + e = encode[ stbir__encode_order1 ]; stbir_scalar_hi_clamp( e ); stbir_scalar_lo_clamp( e ); output[1] = e; + #endif + #if stbir__coder_min_num >= 3 + e = encode[ stbir__encode_order2 ]; stbir_scalar_hi_clamp( e ); stbir_scalar_lo_clamp( e ); output[2] = e; + #endif + output += stbir__coder_min_num; + encode += stbir__coder_min_num; + } + #endif + + #endif +} + +#undef stbir__decode_suffix +#undef stbir__decode_simdf8_flip +#undef stbir__decode_simdf4_flip +#undef stbir__decode_order0 +#undef stbir__decode_order1 +#undef stbir__decode_order2 +#undef stbir__decode_order3 +#undef stbir__encode_order0 +#undef stbir__encode_order1 +#undef stbir__encode_order2 +#undef stbir__encode_order3 +#undef stbir__encode_simdf8_unflip +#undef stbir__encode_simdf4_unflip +#undef stbir__encode_simdfX_unflip +#undef STBIR__CODER_NAME +#undef stbir__coder_min_num +#undef stbir__decode_swizzle +#undef stbir_scalar_hi_clamp +#undef stbir_scalar_lo_clamp +#undef STB_IMAGE_RESIZE_DO_CODERS + +#elif defined( STB_IMAGE_RESIZE_DO_VERTICALS) + +#ifdef STB_IMAGE_RESIZE_VERTICAL_CONTINUE +#define STBIR_chans( start, end ) STBIR_strs_join14(start,STBIR__vertical_channels,end,_cont) +#else +#define STBIR_chans( start, end ) STBIR_strs_join1(start,STBIR__vertical_channels,end) +#endif + +#if STBIR__vertical_channels >= 1 +#define stbIF0( code ) code +#else +#define stbIF0( code ) +#endif +#if STBIR__vertical_channels >= 2 +#define stbIF1( code ) code +#else +#define stbIF1( code ) +#endif +#if STBIR__vertical_channels >= 3 +#define stbIF2( code ) code +#else +#define stbIF2( code ) +#endif +#if STBIR__vertical_channels >= 4 +#define stbIF3( code ) code +#else +#define stbIF3( code ) +#endif +#if STBIR__vertical_channels >= 5 +#define stbIF4( code ) code +#else +#define stbIF4( code ) +#endif +#if STBIR__vertical_channels >= 6 +#define stbIF5( code ) code +#else +#define stbIF5( code ) +#endif +#if STBIR__vertical_channels >= 7 +#define stbIF6( code ) code +#else +#define stbIF6( code ) +#endif +#if STBIR__vertical_channels >= 8 +#define stbIF7( code ) code +#else +#define stbIF7( code ) +#endif + +static void STBIR_chans( stbir__vertical_scatter_with_,_coeffs)( float ** outputs, float const * vertical_coefficients, float const * input, float const * input_end ) +{ + stbIF0( float STBIR_SIMD_STREAMOUT_PTR( * ) output0 = outputs[0]; float c0s = vertical_coefficients[0]; ) + stbIF1( float STBIR_SIMD_STREAMOUT_PTR( * ) output1 = outputs[1]; float c1s = vertical_coefficients[1]; ) + stbIF2( float STBIR_SIMD_STREAMOUT_PTR( * ) output2 = outputs[2]; float c2s = vertical_coefficients[2]; ) + stbIF3( float STBIR_SIMD_STREAMOUT_PTR( * ) output3 = outputs[3]; float c3s = vertical_coefficients[3]; ) + stbIF4( float STBIR_SIMD_STREAMOUT_PTR( * ) output4 = outputs[4]; float c4s = vertical_coefficients[4]; ) + stbIF5( float STBIR_SIMD_STREAMOUT_PTR( * ) output5 = outputs[5]; float c5s = vertical_coefficients[5]; ) + stbIF6( float STBIR_SIMD_STREAMOUT_PTR( * ) output6 = outputs[6]; float c6s = vertical_coefficients[6]; ) + stbIF7( float STBIR_SIMD_STREAMOUT_PTR( * ) output7 = outputs[7]; float c7s = vertical_coefficients[7]; ) + + #ifdef STBIR_SIMD + { + stbIF0(stbir__simdfX c0 = stbir__simdf_frepX( c0s ); ) + stbIF1(stbir__simdfX c1 = stbir__simdf_frepX( c1s ); ) + stbIF2(stbir__simdfX c2 = stbir__simdf_frepX( c2s ); ) + stbIF3(stbir__simdfX c3 = stbir__simdf_frepX( c3s ); ) + stbIF4(stbir__simdfX c4 = stbir__simdf_frepX( c4s ); ) + stbIF5(stbir__simdfX c5 = stbir__simdf_frepX( c5s ); ) + stbIF6(stbir__simdfX c6 = stbir__simdf_frepX( c6s ); ) + stbIF7(stbir__simdfX c7 = stbir__simdf_frepX( c7s ); ) + STBIR_SIMD_NO_UNROLL_LOOP_START + while ( ( (char*)input_end - (char*) input ) >= (16*stbir__simdfX_float_count) ) + { + stbir__simdfX o0, o1, o2, o3, r0, r1, r2, r3; + STBIR_SIMD_NO_UNROLL(output0); + + stbir__simdfX_load( r0, input ); stbir__simdfX_load( r1, input+stbir__simdfX_float_count ); stbir__simdfX_load( r2, input+(2*stbir__simdfX_float_count) ); stbir__simdfX_load( r3, input+(3*stbir__simdfX_float_count) ); + + #ifdef STB_IMAGE_RESIZE_VERTICAL_CONTINUE + stbIF0( stbir__simdfX_load( o0, output0 ); stbir__simdfX_load( o1, output0+stbir__simdfX_float_count ); stbir__simdfX_load( o2, output0+(2*stbir__simdfX_float_count) ); stbir__simdfX_load( o3, output0+(3*stbir__simdfX_float_count) ); + stbir__simdfX_madd( o0, o0, r0, c0 ); stbir__simdfX_madd( o1, o1, r1, c0 ); stbir__simdfX_madd( o2, o2, r2, c0 ); stbir__simdfX_madd( o3, o3, r3, c0 ); + stbir__simdfX_store( output0, o0 ); stbir__simdfX_store( output0+stbir__simdfX_float_count, o1 ); stbir__simdfX_store( output0+(2*stbir__simdfX_float_count), o2 ); stbir__simdfX_store( output0+(3*stbir__simdfX_float_count), o3 ); ) + stbIF1( stbir__simdfX_load( o0, output1 ); stbir__simdfX_load( o1, output1+stbir__simdfX_float_count ); stbir__simdfX_load( o2, output1+(2*stbir__simdfX_float_count) ); stbir__simdfX_load( o3, output1+(3*stbir__simdfX_float_count) ); + stbir__simdfX_madd( o0, o0, r0, c1 ); stbir__simdfX_madd( o1, o1, r1, c1 ); stbir__simdfX_madd( o2, o2, r2, c1 ); stbir__simdfX_madd( o3, o3, r3, c1 ); + stbir__simdfX_store( output1, o0 ); stbir__simdfX_store( output1+stbir__simdfX_float_count, o1 ); stbir__simdfX_store( output1+(2*stbir__simdfX_float_count), o2 ); stbir__simdfX_store( output1+(3*stbir__simdfX_float_count), o3 ); ) + stbIF2( stbir__simdfX_load( o0, output2 ); stbir__simdfX_load( o1, output2+stbir__simdfX_float_count ); stbir__simdfX_load( o2, output2+(2*stbir__simdfX_float_count) ); stbir__simdfX_load( o3, output2+(3*stbir__simdfX_float_count) ); + stbir__simdfX_madd( o0, o0, r0, c2 ); stbir__simdfX_madd( o1, o1, r1, c2 ); stbir__simdfX_madd( o2, o2, r2, c2 ); stbir__simdfX_madd( o3, o3, r3, c2 ); + stbir__simdfX_store( output2, o0 ); stbir__simdfX_store( output2+stbir__simdfX_float_count, o1 ); stbir__simdfX_store( output2+(2*stbir__simdfX_float_count), o2 ); stbir__simdfX_store( output2+(3*stbir__simdfX_float_count), o3 ); ) + stbIF3( stbir__simdfX_load( o0, output3 ); stbir__simdfX_load( o1, output3+stbir__simdfX_float_count ); stbir__simdfX_load( o2, output3+(2*stbir__simdfX_float_count) ); stbir__simdfX_load( o3, output3+(3*stbir__simdfX_float_count) ); + stbir__simdfX_madd( o0, o0, r0, c3 ); stbir__simdfX_madd( o1, o1, r1, c3 ); stbir__simdfX_madd( o2, o2, r2, c3 ); stbir__simdfX_madd( o3, o3, r3, c3 ); + stbir__simdfX_store( output3, o0 ); stbir__simdfX_store( output3+stbir__simdfX_float_count, o1 ); stbir__simdfX_store( output3+(2*stbir__simdfX_float_count), o2 ); stbir__simdfX_store( output3+(3*stbir__simdfX_float_count), o3 ); ) + stbIF4( stbir__simdfX_load( o0, output4 ); stbir__simdfX_load( o1, output4+stbir__simdfX_float_count ); stbir__simdfX_load( o2, output4+(2*stbir__simdfX_float_count) ); stbir__simdfX_load( o3, output4+(3*stbir__simdfX_float_count) ); + stbir__simdfX_madd( o0, o0, r0, c4 ); stbir__simdfX_madd( o1, o1, r1, c4 ); stbir__simdfX_madd( o2, o2, r2, c4 ); stbir__simdfX_madd( o3, o3, r3, c4 ); + stbir__simdfX_store( output4, o0 ); stbir__simdfX_store( output4+stbir__simdfX_float_count, o1 ); stbir__simdfX_store( output4+(2*stbir__simdfX_float_count), o2 ); stbir__simdfX_store( output4+(3*stbir__simdfX_float_count), o3 ); ) + stbIF5( stbir__simdfX_load( o0, output5 ); stbir__simdfX_load( o1, output5+stbir__simdfX_float_count ); stbir__simdfX_load( o2, output5+(2*stbir__simdfX_float_count)); stbir__simdfX_load( o3, output5+(3*stbir__simdfX_float_count) ); + stbir__simdfX_madd( o0, o0, r0, c5 ); stbir__simdfX_madd( o1, o1, r1, c5 ); stbir__simdfX_madd( o2, o2, r2, c5 ); stbir__simdfX_madd( o3, o3, r3, c5 ); + stbir__simdfX_store( output5, o0 ); stbir__simdfX_store( output5+stbir__simdfX_float_count, o1 ); stbir__simdfX_store( output5+(2*stbir__simdfX_float_count), o2 ); stbir__simdfX_store( output5+(3*stbir__simdfX_float_count), o3 ); ) + stbIF6( stbir__simdfX_load( o0, output6 ); stbir__simdfX_load( o1, output6+stbir__simdfX_float_count ); stbir__simdfX_load( o2, output6+(2*stbir__simdfX_float_count) ); stbir__simdfX_load( o3, output6+(3*stbir__simdfX_float_count) ); + stbir__simdfX_madd( o0, o0, r0, c6 ); stbir__simdfX_madd( o1, o1, r1, c6 ); stbir__simdfX_madd( o2, o2, r2, c6 ); stbir__simdfX_madd( o3, o3, r3, c6 ); + stbir__simdfX_store( output6, o0 ); stbir__simdfX_store( output6+stbir__simdfX_float_count, o1 ); stbir__simdfX_store( output6+(2*stbir__simdfX_float_count), o2 ); stbir__simdfX_store( output6+(3*stbir__simdfX_float_count), o3 ); ) + stbIF7( stbir__simdfX_load( o0, output7 ); stbir__simdfX_load( o1, output7+stbir__simdfX_float_count ); stbir__simdfX_load( o2, output7+(2*stbir__simdfX_float_count) ); stbir__simdfX_load( o3, output7+(3*stbir__simdfX_float_count) ); + stbir__simdfX_madd( o0, o0, r0, c7 ); stbir__simdfX_madd( o1, o1, r1, c7 ); stbir__simdfX_madd( o2, o2, r2, c7 ); stbir__simdfX_madd( o3, o3, r3, c7 ); + stbir__simdfX_store( output7, o0 ); stbir__simdfX_store( output7+stbir__simdfX_float_count, o1 ); stbir__simdfX_store( output7+(2*stbir__simdfX_float_count), o2 ); stbir__simdfX_store( output7+(3*stbir__simdfX_float_count), o3 ); ) + #else + stbIF0( stbir__simdfX_mult( o0, r0, c0 ); stbir__simdfX_mult( o1, r1, c0 ); stbir__simdfX_mult( o2, r2, c0 ); stbir__simdfX_mult( o3, r3, c0 ); + stbir__simdfX_store( output0, o0 ); stbir__simdfX_store( output0+stbir__simdfX_float_count, o1 ); stbir__simdfX_store( output0+(2*stbir__simdfX_float_count), o2 ); stbir__simdfX_store( output0+(3*stbir__simdfX_float_count), o3 ); ) + stbIF1( stbir__simdfX_mult( o0, r0, c1 ); stbir__simdfX_mult( o1, r1, c1 ); stbir__simdfX_mult( o2, r2, c1 ); stbir__simdfX_mult( o3, r3, c1 ); + stbir__simdfX_store( output1, o0 ); stbir__simdfX_store( output1+stbir__simdfX_float_count, o1 ); stbir__simdfX_store( output1+(2*stbir__simdfX_float_count), o2 ); stbir__simdfX_store( output1+(3*stbir__simdfX_float_count), o3 ); ) + stbIF2( stbir__simdfX_mult( o0, r0, c2 ); stbir__simdfX_mult( o1, r1, c2 ); stbir__simdfX_mult( o2, r2, c2 ); stbir__simdfX_mult( o3, r3, c2 ); + stbir__simdfX_store( output2, o0 ); stbir__simdfX_store( output2+stbir__simdfX_float_count, o1 ); stbir__simdfX_store( output2+(2*stbir__simdfX_float_count), o2 ); stbir__simdfX_store( output2+(3*stbir__simdfX_float_count), o3 ); ) + stbIF3( stbir__simdfX_mult( o0, r0, c3 ); stbir__simdfX_mult( o1, r1, c3 ); stbir__simdfX_mult( o2, r2, c3 ); stbir__simdfX_mult( o3, r3, c3 ); + stbir__simdfX_store( output3, o0 ); stbir__simdfX_store( output3+stbir__simdfX_float_count, o1 ); stbir__simdfX_store( output3+(2*stbir__simdfX_float_count), o2 ); stbir__simdfX_store( output3+(3*stbir__simdfX_float_count), o3 ); ) + stbIF4( stbir__simdfX_mult( o0, r0, c4 ); stbir__simdfX_mult( o1, r1, c4 ); stbir__simdfX_mult( o2, r2, c4 ); stbir__simdfX_mult( o3, r3, c4 ); + stbir__simdfX_store( output4, o0 ); stbir__simdfX_store( output4+stbir__simdfX_float_count, o1 ); stbir__simdfX_store( output4+(2*stbir__simdfX_float_count), o2 ); stbir__simdfX_store( output4+(3*stbir__simdfX_float_count), o3 ); ) + stbIF5( stbir__simdfX_mult( o0, r0, c5 ); stbir__simdfX_mult( o1, r1, c5 ); stbir__simdfX_mult( o2, r2, c5 ); stbir__simdfX_mult( o3, r3, c5 ); + stbir__simdfX_store( output5, o0 ); stbir__simdfX_store( output5+stbir__simdfX_float_count, o1 ); stbir__simdfX_store( output5+(2*stbir__simdfX_float_count), o2 ); stbir__simdfX_store( output5+(3*stbir__simdfX_float_count), o3 ); ) + stbIF6( stbir__simdfX_mult( o0, r0, c6 ); stbir__simdfX_mult( o1, r1, c6 ); stbir__simdfX_mult( o2, r2, c6 ); stbir__simdfX_mult( o3, r3, c6 ); + stbir__simdfX_store( output6, o0 ); stbir__simdfX_store( output6+stbir__simdfX_float_count, o1 ); stbir__simdfX_store( output6+(2*stbir__simdfX_float_count), o2 ); stbir__simdfX_store( output6+(3*stbir__simdfX_float_count), o3 ); ) + stbIF7( stbir__simdfX_mult( o0, r0, c7 ); stbir__simdfX_mult( o1, r1, c7 ); stbir__simdfX_mult( o2, r2, c7 ); stbir__simdfX_mult( o3, r3, c7 ); + stbir__simdfX_store( output7, o0 ); stbir__simdfX_store( output7+stbir__simdfX_float_count, o1 ); stbir__simdfX_store( output7+(2*stbir__simdfX_float_count), o2 ); stbir__simdfX_store( output7+(3*stbir__simdfX_float_count), o3 ); ) + #endif + + input += (4*stbir__simdfX_float_count); + stbIF0( output0 += (4*stbir__simdfX_float_count); ) stbIF1( output1 += (4*stbir__simdfX_float_count); ) stbIF2( output2 += (4*stbir__simdfX_float_count); ) stbIF3( output3 += (4*stbir__simdfX_float_count); ) stbIF4( output4 += (4*stbir__simdfX_float_count); ) stbIF5( output5 += (4*stbir__simdfX_float_count); ) stbIF6( output6 += (4*stbir__simdfX_float_count); ) stbIF7( output7 += (4*stbir__simdfX_float_count); ) + } + STBIR_SIMD_NO_UNROLL_LOOP_START + while ( ( (char*)input_end - (char*) input ) >= 16 ) + { + stbir__simdf o0, r0; + STBIR_SIMD_NO_UNROLL(output0); + + stbir__simdf_load( r0, input ); + + #ifdef STB_IMAGE_RESIZE_VERTICAL_CONTINUE + stbIF0( stbir__simdf_load( o0, output0 ); stbir__simdf_madd( o0, o0, r0, stbir__if_simdf8_cast_to_simdf4( c0 ) ); stbir__simdf_store( output0, o0 ); ) + stbIF1( stbir__simdf_load( o0, output1 ); stbir__simdf_madd( o0, o0, r0, stbir__if_simdf8_cast_to_simdf4( c1 ) ); stbir__simdf_store( output1, o0 ); ) + stbIF2( stbir__simdf_load( o0, output2 ); stbir__simdf_madd( o0, o0, r0, stbir__if_simdf8_cast_to_simdf4( c2 ) ); stbir__simdf_store( output2, o0 ); ) + stbIF3( stbir__simdf_load( o0, output3 ); stbir__simdf_madd( o0, o0, r0, stbir__if_simdf8_cast_to_simdf4( c3 ) ); stbir__simdf_store( output3, o0 ); ) + stbIF4( stbir__simdf_load( o0, output4 ); stbir__simdf_madd( o0, o0, r0, stbir__if_simdf8_cast_to_simdf4( c4 ) ); stbir__simdf_store( output4, o0 ); ) + stbIF5( stbir__simdf_load( o0, output5 ); stbir__simdf_madd( o0, o0, r0, stbir__if_simdf8_cast_to_simdf4( c5 ) ); stbir__simdf_store( output5, o0 ); ) + stbIF6( stbir__simdf_load( o0, output6 ); stbir__simdf_madd( o0, o0, r0, stbir__if_simdf8_cast_to_simdf4( c6 ) ); stbir__simdf_store( output6, o0 ); ) + stbIF7( stbir__simdf_load( o0, output7 ); stbir__simdf_madd( o0, o0, r0, stbir__if_simdf8_cast_to_simdf4( c7 ) ); stbir__simdf_store( output7, o0 ); ) + #else + stbIF0( stbir__simdf_mult( o0, r0, stbir__if_simdf8_cast_to_simdf4( c0 ) ); stbir__simdf_store( output0, o0 ); ) + stbIF1( stbir__simdf_mult( o0, r0, stbir__if_simdf8_cast_to_simdf4( c1 ) ); stbir__simdf_store( output1, o0 ); ) + stbIF2( stbir__simdf_mult( o0, r0, stbir__if_simdf8_cast_to_simdf4( c2 ) ); stbir__simdf_store( output2, o0 ); ) + stbIF3( stbir__simdf_mult( o0, r0, stbir__if_simdf8_cast_to_simdf4( c3 ) ); stbir__simdf_store( output3, o0 ); ) + stbIF4( stbir__simdf_mult( o0, r0, stbir__if_simdf8_cast_to_simdf4( c4 ) ); stbir__simdf_store( output4, o0 ); ) + stbIF5( stbir__simdf_mult( o0, r0, stbir__if_simdf8_cast_to_simdf4( c5 ) ); stbir__simdf_store( output5, o0 ); ) + stbIF6( stbir__simdf_mult( o0, r0, stbir__if_simdf8_cast_to_simdf4( c6 ) ); stbir__simdf_store( output6, o0 ); ) + stbIF7( stbir__simdf_mult( o0, r0, stbir__if_simdf8_cast_to_simdf4( c7 ) ); stbir__simdf_store( output7, o0 ); ) + #endif + + input += 4; + stbIF0( output0 += 4; ) stbIF1( output1 += 4; ) stbIF2( output2 += 4; ) stbIF3( output3 += 4; ) stbIF4( output4 += 4; ) stbIF5( output5 += 4; ) stbIF6( output6 += 4; ) stbIF7( output7 += 4; ) + } + } + #else + STBIR_NO_UNROLL_LOOP_START + while ( ( (char*)input_end - (char*) input ) >= 16 ) + { + float r0, r1, r2, r3; + STBIR_NO_UNROLL(input); + + r0 = input[0], r1 = input[1], r2 = input[2], r3 = input[3]; + + #ifdef STB_IMAGE_RESIZE_VERTICAL_CONTINUE + stbIF0( output0[0] += ( r0 * c0s ); output0[1] += ( r1 * c0s ); output0[2] += ( r2 * c0s ); output0[3] += ( r3 * c0s ); ) + stbIF1( output1[0] += ( r0 * c1s ); output1[1] += ( r1 * c1s ); output1[2] += ( r2 * c1s ); output1[3] += ( r3 * c1s ); ) + stbIF2( output2[0] += ( r0 * c2s ); output2[1] += ( r1 * c2s ); output2[2] += ( r2 * c2s ); output2[3] += ( r3 * c2s ); ) + stbIF3( output3[0] += ( r0 * c3s ); output3[1] += ( r1 * c3s ); output3[2] += ( r2 * c3s ); output3[3] += ( r3 * c3s ); ) + stbIF4( output4[0] += ( r0 * c4s ); output4[1] += ( r1 * c4s ); output4[2] += ( r2 * c4s ); output4[3] += ( r3 * c4s ); ) + stbIF5( output5[0] += ( r0 * c5s ); output5[1] += ( r1 * c5s ); output5[2] += ( r2 * c5s ); output5[3] += ( r3 * c5s ); ) + stbIF6( output6[0] += ( r0 * c6s ); output6[1] += ( r1 * c6s ); output6[2] += ( r2 * c6s ); output6[3] += ( r3 * c6s ); ) + stbIF7( output7[0] += ( r0 * c7s ); output7[1] += ( r1 * c7s ); output7[2] += ( r2 * c7s ); output7[3] += ( r3 * c7s ); ) + #else + stbIF0( output0[0] = ( r0 * c0s ); output0[1] = ( r1 * c0s ); output0[2] = ( r2 * c0s ); output0[3] = ( r3 * c0s ); ) + stbIF1( output1[0] = ( r0 * c1s ); output1[1] = ( r1 * c1s ); output1[2] = ( r2 * c1s ); output1[3] = ( r3 * c1s ); ) + stbIF2( output2[0] = ( r0 * c2s ); output2[1] = ( r1 * c2s ); output2[2] = ( r2 * c2s ); output2[3] = ( r3 * c2s ); ) + stbIF3( output3[0] = ( r0 * c3s ); output3[1] = ( r1 * c3s ); output3[2] = ( r2 * c3s ); output3[3] = ( r3 * c3s ); ) + stbIF4( output4[0] = ( r0 * c4s ); output4[1] = ( r1 * c4s ); output4[2] = ( r2 * c4s ); output4[3] = ( r3 * c4s ); ) + stbIF5( output5[0] = ( r0 * c5s ); output5[1] = ( r1 * c5s ); output5[2] = ( r2 * c5s ); output5[3] = ( r3 * c5s ); ) + stbIF6( output6[0] = ( r0 * c6s ); output6[1] = ( r1 * c6s ); output6[2] = ( r2 * c6s ); output6[3] = ( r3 * c6s ); ) + stbIF7( output7[0] = ( r0 * c7s ); output7[1] = ( r1 * c7s ); output7[2] = ( r2 * c7s ); output7[3] = ( r3 * c7s ); ) + #endif + + input += 4; + stbIF0( output0 += 4; ) stbIF1( output1 += 4; ) stbIF2( output2 += 4; ) stbIF3( output3 += 4; ) stbIF4( output4 += 4; ) stbIF5( output5 += 4; ) stbIF6( output6 += 4; ) stbIF7( output7 += 4; ) + } + #endif + STBIR_NO_UNROLL_LOOP_START + while ( input < input_end ) + { + float r = input[0]; + STBIR_NO_UNROLL(output0); + + #ifdef STB_IMAGE_RESIZE_VERTICAL_CONTINUE + stbIF0( output0[0] += ( r * c0s ); ) + stbIF1( output1[0] += ( r * c1s ); ) + stbIF2( output2[0] += ( r * c2s ); ) + stbIF3( output3[0] += ( r * c3s ); ) + stbIF4( output4[0] += ( r * c4s ); ) + stbIF5( output5[0] += ( r * c5s ); ) + stbIF6( output6[0] += ( r * c6s ); ) + stbIF7( output7[0] += ( r * c7s ); ) + #else + stbIF0( output0[0] = ( r * c0s ); ) + stbIF1( output1[0] = ( r * c1s ); ) + stbIF2( output2[0] = ( r * c2s ); ) + stbIF3( output3[0] = ( r * c3s ); ) + stbIF4( output4[0] = ( r * c4s ); ) + stbIF5( output5[0] = ( r * c5s ); ) + stbIF6( output6[0] = ( r * c6s ); ) + stbIF7( output7[0] = ( r * c7s ); ) + #endif + + ++input; + stbIF0( ++output0; ) stbIF1( ++output1; ) stbIF2( ++output2; ) stbIF3( ++output3; ) stbIF4( ++output4; ) stbIF5( ++output5; ) stbIF6( ++output6; ) stbIF7( ++output7; ) + } +} + +static void STBIR_chans( stbir__vertical_gather_with_,_coeffs)( float * outputp, float const * vertical_coefficients, float const ** inputs, float const * input0_end ) +{ + float STBIR_SIMD_STREAMOUT_PTR( * ) output = outputp; + + stbIF0( float const * input0 = inputs[0]; float c0s = vertical_coefficients[0]; ) + stbIF1( float const * input1 = inputs[1]; float c1s = vertical_coefficients[1]; ) + stbIF2( float const * input2 = inputs[2]; float c2s = vertical_coefficients[2]; ) + stbIF3( float const * input3 = inputs[3]; float c3s = vertical_coefficients[3]; ) + stbIF4( float const * input4 = inputs[4]; float c4s = vertical_coefficients[4]; ) + stbIF5( float const * input5 = inputs[5]; float c5s = vertical_coefficients[5]; ) + stbIF6( float const * input6 = inputs[6]; float c6s = vertical_coefficients[6]; ) + stbIF7( float const * input7 = inputs[7]; float c7s = vertical_coefficients[7]; ) + +#if ( STBIR__vertical_channels == 1 ) && !defined(STB_IMAGE_RESIZE_VERTICAL_CONTINUE) + // check single channel one weight + if ( ( c0s >= (1.0f-0.000001f) ) && ( c0s <= (1.0f+0.000001f) ) ) + { + STBIR_MEMCPY( output, input0, (char*)input0_end - (char*)input0 ); + return; + } +#endif + + #ifdef STBIR_SIMD + { + stbIF0(stbir__simdfX c0 = stbir__simdf_frepX( c0s ); ) + stbIF1(stbir__simdfX c1 = stbir__simdf_frepX( c1s ); ) + stbIF2(stbir__simdfX c2 = stbir__simdf_frepX( c2s ); ) + stbIF3(stbir__simdfX c3 = stbir__simdf_frepX( c3s ); ) + stbIF4(stbir__simdfX c4 = stbir__simdf_frepX( c4s ); ) + stbIF5(stbir__simdfX c5 = stbir__simdf_frepX( c5s ); ) + stbIF6(stbir__simdfX c6 = stbir__simdf_frepX( c6s ); ) + stbIF7(stbir__simdfX c7 = stbir__simdf_frepX( c7s ); ) + + STBIR_SIMD_NO_UNROLL_LOOP_START + while ( ( (char*)input0_end - (char*) input0 ) >= (16*stbir__simdfX_float_count) ) + { + stbir__simdfX o0, o1, o2, o3, r0, r1, r2, r3; + STBIR_SIMD_NO_UNROLL(output); + + // prefetch four loop iterations ahead (doesn't affect much for small resizes, but helps with big ones) + stbIF0( stbir__prefetch( input0 + (16*stbir__simdfX_float_count) ); ) + stbIF1( stbir__prefetch( input1 + (16*stbir__simdfX_float_count) ); ) + stbIF2( stbir__prefetch( input2 + (16*stbir__simdfX_float_count) ); ) + stbIF3( stbir__prefetch( input3 + (16*stbir__simdfX_float_count) ); ) + stbIF4( stbir__prefetch( input4 + (16*stbir__simdfX_float_count) ); ) + stbIF5( stbir__prefetch( input5 + (16*stbir__simdfX_float_count) ); ) + stbIF6( stbir__prefetch( input6 + (16*stbir__simdfX_float_count) ); ) + stbIF7( stbir__prefetch( input7 + (16*stbir__simdfX_float_count) ); ) + + #ifdef STB_IMAGE_RESIZE_VERTICAL_CONTINUE + stbIF0( stbir__simdfX_load( o0, output ); stbir__simdfX_load( o1, output+stbir__simdfX_float_count ); stbir__simdfX_load( o2, output+(2*stbir__simdfX_float_count) ); stbir__simdfX_load( o3, output+(3*stbir__simdfX_float_count) ); + stbir__simdfX_load( r0, input0 ); stbir__simdfX_load( r1, input0+stbir__simdfX_float_count ); stbir__simdfX_load( r2, input0+(2*stbir__simdfX_float_count) ); stbir__simdfX_load( r3, input0+(3*stbir__simdfX_float_count) ); + stbir__simdfX_madd( o0, o0, r0, c0 ); stbir__simdfX_madd( o1, o1, r1, c0 ); stbir__simdfX_madd( o2, o2, r2, c0 ); stbir__simdfX_madd( o3, o3, r3, c0 ); ) + #else + stbIF0( stbir__simdfX_load( r0, input0 ); stbir__simdfX_load( r1, input0+stbir__simdfX_float_count ); stbir__simdfX_load( r2, input0+(2*stbir__simdfX_float_count) ); stbir__simdfX_load( r3, input0+(3*stbir__simdfX_float_count) ); + stbir__simdfX_mult( o0, r0, c0 ); stbir__simdfX_mult( o1, r1, c0 ); stbir__simdfX_mult( o2, r2, c0 ); stbir__simdfX_mult( o3, r3, c0 ); ) + #endif + + stbIF1( stbir__simdfX_load( r0, input1 ); stbir__simdfX_load( r1, input1+stbir__simdfX_float_count ); stbir__simdfX_load( r2, input1+(2*stbir__simdfX_float_count) ); stbir__simdfX_load( r3, input1+(3*stbir__simdfX_float_count) ); + stbir__simdfX_madd( o0, o0, r0, c1 ); stbir__simdfX_madd( o1, o1, r1, c1 ); stbir__simdfX_madd( o2, o2, r2, c1 ); stbir__simdfX_madd( o3, o3, r3, c1 ); ) + stbIF2( stbir__simdfX_load( r0, input2 ); stbir__simdfX_load( r1, input2+stbir__simdfX_float_count ); stbir__simdfX_load( r2, input2+(2*stbir__simdfX_float_count) ); stbir__simdfX_load( r3, input2+(3*stbir__simdfX_float_count) ); + stbir__simdfX_madd( o0, o0, r0, c2 ); stbir__simdfX_madd( o1, o1, r1, c2 ); stbir__simdfX_madd( o2, o2, r2, c2 ); stbir__simdfX_madd( o3, o3, r3, c2 ); ) + stbIF3( stbir__simdfX_load( r0, input3 ); stbir__simdfX_load( r1, input3+stbir__simdfX_float_count ); stbir__simdfX_load( r2, input3+(2*stbir__simdfX_float_count) ); stbir__simdfX_load( r3, input3+(3*stbir__simdfX_float_count) ); + stbir__simdfX_madd( o0, o0, r0, c3 ); stbir__simdfX_madd( o1, o1, r1, c3 ); stbir__simdfX_madd( o2, o2, r2, c3 ); stbir__simdfX_madd( o3, o3, r3, c3 ); ) + stbIF4( stbir__simdfX_load( r0, input4 ); stbir__simdfX_load( r1, input4+stbir__simdfX_float_count ); stbir__simdfX_load( r2, input4+(2*stbir__simdfX_float_count) ); stbir__simdfX_load( r3, input4+(3*stbir__simdfX_float_count) ); + stbir__simdfX_madd( o0, o0, r0, c4 ); stbir__simdfX_madd( o1, o1, r1, c4 ); stbir__simdfX_madd( o2, o2, r2, c4 ); stbir__simdfX_madd( o3, o3, r3, c4 ); ) + stbIF5( stbir__simdfX_load( r0, input5 ); stbir__simdfX_load( r1, input5+stbir__simdfX_float_count ); stbir__simdfX_load( r2, input5+(2*stbir__simdfX_float_count) ); stbir__simdfX_load( r3, input5+(3*stbir__simdfX_float_count) ); + stbir__simdfX_madd( o0, o0, r0, c5 ); stbir__simdfX_madd( o1, o1, r1, c5 ); stbir__simdfX_madd( o2, o2, r2, c5 ); stbir__simdfX_madd( o3, o3, r3, c5 ); ) + stbIF6( stbir__simdfX_load( r0, input6 ); stbir__simdfX_load( r1, input6+stbir__simdfX_float_count ); stbir__simdfX_load( r2, input6+(2*stbir__simdfX_float_count) ); stbir__simdfX_load( r3, input6+(3*stbir__simdfX_float_count) ); + stbir__simdfX_madd( o0, o0, r0, c6 ); stbir__simdfX_madd( o1, o1, r1, c6 ); stbir__simdfX_madd( o2, o2, r2, c6 ); stbir__simdfX_madd( o3, o3, r3, c6 ); ) + stbIF7( stbir__simdfX_load( r0, input7 ); stbir__simdfX_load( r1, input7+stbir__simdfX_float_count ); stbir__simdfX_load( r2, input7+(2*stbir__simdfX_float_count) ); stbir__simdfX_load( r3, input7+(3*stbir__simdfX_float_count) ); + stbir__simdfX_madd( o0, o0, r0, c7 ); stbir__simdfX_madd( o1, o1, r1, c7 ); stbir__simdfX_madd( o2, o2, r2, c7 ); stbir__simdfX_madd( o3, o3, r3, c7 ); ) + + stbir__simdfX_store( output, o0 ); stbir__simdfX_store( output+stbir__simdfX_float_count, o1 ); stbir__simdfX_store( output+(2*stbir__simdfX_float_count), o2 ); stbir__simdfX_store( output+(3*stbir__simdfX_float_count), o3 ); + output += (4*stbir__simdfX_float_count); + stbIF0( input0 += (4*stbir__simdfX_float_count); ) stbIF1( input1 += (4*stbir__simdfX_float_count); ) stbIF2( input2 += (4*stbir__simdfX_float_count); ) stbIF3( input3 += (4*stbir__simdfX_float_count); ) stbIF4( input4 += (4*stbir__simdfX_float_count); ) stbIF5( input5 += (4*stbir__simdfX_float_count); ) stbIF6( input6 += (4*stbir__simdfX_float_count); ) stbIF7( input7 += (4*stbir__simdfX_float_count); ) + } + + STBIR_SIMD_NO_UNROLL_LOOP_START + while ( ( (char*)input0_end - (char*) input0 ) >= 16 ) + { + stbir__simdf o0, r0; + STBIR_SIMD_NO_UNROLL(output); + + #ifdef STB_IMAGE_RESIZE_VERTICAL_CONTINUE + stbIF0( stbir__simdf_load( o0, output ); stbir__simdf_load( r0, input0 ); stbir__simdf_madd( o0, o0, r0, stbir__if_simdf8_cast_to_simdf4( c0 ) ); ) + #else + stbIF0( stbir__simdf_load( r0, input0 ); stbir__simdf_mult( o0, r0, stbir__if_simdf8_cast_to_simdf4( c0 ) ); ) + #endif + stbIF1( stbir__simdf_load( r0, input1 ); stbir__simdf_madd( o0, o0, r0, stbir__if_simdf8_cast_to_simdf4( c1 ) ); ) + stbIF2( stbir__simdf_load( r0, input2 ); stbir__simdf_madd( o0, o0, r0, stbir__if_simdf8_cast_to_simdf4( c2 ) ); ) + stbIF3( stbir__simdf_load( r0, input3 ); stbir__simdf_madd( o0, o0, r0, stbir__if_simdf8_cast_to_simdf4( c3 ) ); ) + stbIF4( stbir__simdf_load( r0, input4 ); stbir__simdf_madd( o0, o0, r0, stbir__if_simdf8_cast_to_simdf4( c4 ) ); ) + stbIF5( stbir__simdf_load( r0, input5 ); stbir__simdf_madd( o0, o0, r0, stbir__if_simdf8_cast_to_simdf4( c5 ) ); ) + stbIF6( stbir__simdf_load( r0, input6 ); stbir__simdf_madd( o0, o0, r0, stbir__if_simdf8_cast_to_simdf4( c6 ) ); ) + stbIF7( stbir__simdf_load( r0, input7 ); stbir__simdf_madd( o0, o0, r0, stbir__if_simdf8_cast_to_simdf4( c7 ) ); ) + + stbir__simdf_store( output, o0 ); + output += 4; + stbIF0( input0 += 4; ) stbIF1( input1 += 4; ) stbIF2( input2 += 4; ) stbIF3( input3 += 4; ) stbIF4( input4 += 4; ) stbIF5( input5 += 4; ) stbIF6( input6 += 4; ) stbIF7( input7 += 4; ) + } + } + #else + STBIR_NO_UNROLL_LOOP_START + while ( ( (char*)input0_end - (char*) input0 ) >= 16 ) + { + float o0, o1, o2, o3; + STBIR_NO_UNROLL(output); + #ifdef STB_IMAGE_RESIZE_VERTICAL_CONTINUE + stbIF0( o0 = output[0] + input0[0] * c0s; o1 = output[1] + input0[1] * c0s; o2 = output[2] + input0[2] * c0s; o3 = output[3] + input0[3] * c0s; ) + #else + stbIF0( o0 = input0[0] * c0s; o1 = input0[1] * c0s; o2 = input0[2] * c0s; o3 = input0[3] * c0s; ) + #endif + stbIF1( o0 += input1[0] * c1s; o1 += input1[1] * c1s; o2 += input1[2] * c1s; o3 += input1[3] * c1s; ) + stbIF2( o0 += input2[0] * c2s; o1 += input2[1] * c2s; o2 += input2[2] * c2s; o3 += input2[3] * c2s; ) + stbIF3( o0 += input3[0] * c3s; o1 += input3[1] * c3s; o2 += input3[2] * c3s; o3 += input3[3] * c3s; ) + stbIF4( o0 += input4[0] * c4s; o1 += input4[1] * c4s; o2 += input4[2] * c4s; o3 += input4[3] * c4s; ) + stbIF5( o0 += input5[0] * c5s; o1 += input5[1] * c5s; o2 += input5[2] * c5s; o3 += input5[3] * c5s; ) + stbIF6( o0 += input6[0] * c6s; o1 += input6[1] * c6s; o2 += input6[2] * c6s; o3 += input6[3] * c6s; ) + stbIF7( o0 += input7[0] * c7s; o1 += input7[1] * c7s; o2 += input7[2] * c7s; o3 += input7[3] * c7s; ) + output[0] = o0; output[1] = o1; output[2] = o2; output[3] = o3; + output += 4; + stbIF0( input0 += 4; ) stbIF1( input1 += 4; ) stbIF2( input2 += 4; ) stbIF3( input3 += 4; ) stbIF4( input4 += 4; ) stbIF5( input5 += 4; ) stbIF6( input6 += 4; ) stbIF7( input7 += 4; ) + } + #endif + STBIR_NO_UNROLL_LOOP_START + while ( input0 < input0_end ) + { + float o0; + STBIR_NO_UNROLL(output); + #ifdef STB_IMAGE_RESIZE_VERTICAL_CONTINUE + stbIF0( o0 = output[0] + input0[0] * c0s; ) + #else + stbIF0( o0 = input0[0] * c0s; ) + #endif + stbIF1( o0 += input1[0] * c1s; ) + stbIF2( o0 += input2[0] * c2s; ) + stbIF3( o0 += input3[0] * c3s; ) + stbIF4( o0 += input4[0] * c4s; ) + stbIF5( o0 += input5[0] * c5s; ) + stbIF6( o0 += input6[0] * c6s; ) + stbIF7( o0 += input7[0] * c7s; ) + output[0] = o0; + ++output; + stbIF0( ++input0; ) stbIF1( ++input1; ) stbIF2( ++input2; ) stbIF3( ++input3; ) stbIF4( ++input4; ) stbIF5( ++input5; ) stbIF6( ++input6; ) stbIF7( ++input7; ) + } +} + +#undef stbIF0 +#undef stbIF1 +#undef stbIF2 +#undef stbIF3 +#undef stbIF4 +#undef stbIF5 +#undef stbIF6 +#undef stbIF7 +#undef STB_IMAGE_RESIZE_DO_VERTICALS +#undef STBIR__vertical_channels +#undef STB_IMAGE_RESIZE_DO_HORIZONTALS +#undef STBIR_strs_join24 +#undef STBIR_strs_join14 +#undef STBIR_chans +#ifdef STB_IMAGE_RESIZE_VERTICAL_CONTINUE +#undef STB_IMAGE_RESIZE_VERTICAL_CONTINUE +#endif + +#else // !STB_IMAGE_RESIZE_DO_VERTICALS + +#define STBIR_chans( start, end ) STBIR_strs_join1(start,STBIR__horizontal_channels,end) + +#ifndef stbir__2_coeff_only +#define stbir__2_coeff_only() \ + stbir__1_coeff_only(); \ + stbir__1_coeff_remnant(1); +#endif + +#ifndef stbir__2_coeff_remnant +#define stbir__2_coeff_remnant( ofs ) \ + stbir__1_coeff_remnant(ofs); \ + stbir__1_coeff_remnant((ofs)+1); +#endif + +#ifndef stbir__3_coeff_only +#define stbir__3_coeff_only() \ + stbir__2_coeff_only(); \ + stbir__1_coeff_remnant(2); +#endif + +#ifndef stbir__3_coeff_remnant +#define stbir__3_coeff_remnant( ofs ) \ + stbir__2_coeff_remnant(ofs); \ + stbir__1_coeff_remnant((ofs)+2); +#endif + +#ifndef stbir__3_coeff_setup +#define stbir__3_coeff_setup() +#endif + +#ifndef stbir__4_coeff_start +#define stbir__4_coeff_start() \ + stbir__2_coeff_only(); \ + stbir__2_coeff_remnant(2); +#endif + +#ifndef stbir__4_coeff_continue_from_4 +#define stbir__4_coeff_continue_from_4( ofs ) \ + stbir__2_coeff_remnant(ofs); \ + stbir__2_coeff_remnant((ofs)+2); +#endif + +#ifndef stbir__store_output_tiny +#define stbir__store_output_tiny stbir__store_output +#endif + +static void STBIR_chans( stbir__horizontal_gather_,_channels_with_1_coeff)( float * output_buffer, unsigned int output_sub_size, float const * decode_buffer, stbir__contributors const * horizontal_contributors, float const * horizontal_coefficients, int coefficient_width ) +{ + float const * output_end = output_buffer + output_sub_size * STBIR__horizontal_channels; + float STBIR_SIMD_STREAMOUT_PTR( * ) output = output_buffer; + STBIR_SIMD_NO_UNROLL_LOOP_START + do { + float const * decode = decode_buffer + horizontal_contributors->n0 * STBIR__horizontal_channels; + float const * hc = horizontal_coefficients; + stbir__1_coeff_only(); + stbir__store_output_tiny(); + } while ( output < output_end ); +} + +static void STBIR_chans( stbir__horizontal_gather_,_channels_with_2_coeffs)( float * output_buffer, unsigned int output_sub_size, float const * decode_buffer, stbir__contributors const * horizontal_contributors, float const * horizontal_coefficients, int coefficient_width ) +{ + float const * output_end = output_buffer + output_sub_size * STBIR__horizontal_channels; + float STBIR_SIMD_STREAMOUT_PTR( * ) output = output_buffer; + STBIR_SIMD_NO_UNROLL_LOOP_START + do { + float const * decode = decode_buffer + horizontal_contributors->n0 * STBIR__horizontal_channels; + float const * hc = horizontal_coefficients; + stbir__2_coeff_only(); + stbir__store_output_tiny(); + } while ( output < output_end ); +} + +static void STBIR_chans( stbir__horizontal_gather_,_channels_with_3_coeffs)( float * output_buffer, unsigned int output_sub_size, float const * decode_buffer, stbir__contributors const * horizontal_contributors, float const * horizontal_coefficients, int coefficient_width ) +{ + float const * output_end = output_buffer + output_sub_size * STBIR__horizontal_channels; + float STBIR_SIMD_STREAMOUT_PTR( * ) output = output_buffer; + STBIR_SIMD_NO_UNROLL_LOOP_START + do { + float const * decode = decode_buffer + horizontal_contributors->n0 * STBIR__horizontal_channels; + float const * hc = horizontal_coefficients; + stbir__3_coeff_only(); + stbir__store_output_tiny(); + } while ( output < output_end ); +} + +static void STBIR_chans( stbir__horizontal_gather_,_channels_with_4_coeffs)( float * output_buffer, unsigned int output_sub_size, float const * decode_buffer, stbir__contributors const * horizontal_contributors, float const * horizontal_coefficients, int coefficient_width ) +{ + float const * output_end = output_buffer + output_sub_size * STBIR__horizontal_channels; + float STBIR_SIMD_STREAMOUT_PTR( * ) output = output_buffer; + STBIR_SIMD_NO_UNROLL_LOOP_START + do { + float const * decode = decode_buffer + horizontal_contributors->n0 * STBIR__horizontal_channels; + float const * hc = horizontal_coefficients; + stbir__4_coeff_start(); + stbir__store_output(); + } while ( output < output_end ); +} + +static void STBIR_chans( stbir__horizontal_gather_,_channels_with_5_coeffs)( float * output_buffer, unsigned int output_sub_size, float const * decode_buffer, stbir__contributors const * horizontal_contributors, float const * horizontal_coefficients, int coefficient_width ) +{ + float const * output_end = output_buffer + output_sub_size * STBIR__horizontal_channels; + float STBIR_SIMD_STREAMOUT_PTR( * ) output = output_buffer; + STBIR_SIMD_NO_UNROLL_LOOP_START + do { + float const * decode = decode_buffer + horizontal_contributors->n0 * STBIR__horizontal_channels; + float const * hc = horizontal_coefficients; + stbir__4_coeff_start(); + stbir__1_coeff_remnant(4); + stbir__store_output(); + } while ( output < output_end ); +} + +static void STBIR_chans( stbir__horizontal_gather_,_channels_with_6_coeffs)( float * output_buffer, unsigned int output_sub_size, float const * decode_buffer, stbir__contributors const * horizontal_contributors, float const * horizontal_coefficients, int coefficient_width ) +{ + float const * output_end = output_buffer + output_sub_size * STBIR__horizontal_channels; + float STBIR_SIMD_STREAMOUT_PTR( * ) output = output_buffer; + STBIR_SIMD_NO_UNROLL_LOOP_START + do { + float const * decode = decode_buffer + horizontal_contributors->n0 * STBIR__horizontal_channels; + float const * hc = horizontal_coefficients; + stbir__4_coeff_start(); + stbir__2_coeff_remnant(4); + stbir__store_output(); + } while ( output < output_end ); +} + +static void STBIR_chans( stbir__horizontal_gather_,_channels_with_7_coeffs)( float * output_buffer, unsigned int output_sub_size, float const * decode_buffer, stbir__contributors const * horizontal_contributors, float const * horizontal_coefficients, int coefficient_width ) +{ + float const * output_end = output_buffer + output_sub_size * STBIR__horizontal_channels; + float STBIR_SIMD_STREAMOUT_PTR( * ) output = output_buffer; + stbir__3_coeff_setup(); + STBIR_SIMD_NO_UNROLL_LOOP_START + do { + float const * decode = decode_buffer + horizontal_contributors->n0 * STBIR__horizontal_channels; + float const * hc = horizontal_coefficients; + + stbir__4_coeff_start(); + stbir__3_coeff_remnant(4); + stbir__store_output(); + } while ( output < output_end ); +} + +static void STBIR_chans( stbir__horizontal_gather_,_channels_with_8_coeffs)( float * output_buffer, unsigned int output_sub_size, float const * decode_buffer, stbir__contributors const * horizontal_contributors, float const * horizontal_coefficients, int coefficient_width ) +{ + float const * output_end = output_buffer + output_sub_size * STBIR__horizontal_channels; + float STBIR_SIMD_STREAMOUT_PTR( * ) output = output_buffer; + STBIR_SIMD_NO_UNROLL_LOOP_START + do { + float const * decode = decode_buffer + horizontal_contributors->n0 * STBIR__horizontal_channels; + float const * hc = horizontal_coefficients; + stbir__4_coeff_start(); + stbir__4_coeff_continue_from_4(4); + stbir__store_output(); + } while ( output < output_end ); +} + +static void STBIR_chans( stbir__horizontal_gather_,_channels_with_9_coeffs)( float * output_buffer, unsigned int output_sub_size, float const * decode_buffer, stbir__contributors const * horizontal_contributors, float const * horizontal_coefficients, int coefficient_width ) +{ + float const * output_end = output_buffer + output_sub_size * STBIR__horizontal_channels; + float STBIR_SIMD_STREAMOUT_PTR( * ) output = output_buffer; + STBIR_SIMD_NO_UNROLL_LOOP_START + do { + float const * decode = decode_buffer + horizontal_contributors->n0 * STBIR__horizontal_channels; + float const * hc = horizontal_coefficients; + stbir__4_coeff_start(); + stbir__4_coeff_continue_from_4(4); + stbir__1_coeff_remnant(8); + stbir__store_output(); + } while ( output < output_end ); +} + +static void STBIR_chans( stbir__horizontal_gather_,_channels_with_10_coeffs)( float * output_buffer, unsigned int output_sub_size, float const * decode_buffer, stbir__contributors const * horizontal_contributors, float const * horizontal_coefficients, int coefficient_width ) +{ + float const * output_end = output_buffer + output_sub_size * STBIR__horizontal_channels; + float STBIR_SIMD_STREAMOUT_PTR( * ) output = output_buffer; + STBIR_SIMD_NO_UNROLL_LOOP_START + do { + float const * decode = decode_buffer + horizontal_contributors->n0 * STBIR__horizontal_channels; + float const * hc = horizontal_coefficients; + stbir__4_coeff_start(); + stbir__4_coeff_continue_from_4(4); + stbir__2_coeff_remnant(8); + stbir__store_output(); + } while ( output < output_end ); +} + +static void STBIR_chans( stbir__horizontal_gather_,_channels_with_11_coeffs)( float * output_buffer, unsigned int output_sub_size, float const * decode_buffer, stbir__contributors const * horizontal_contributors, float const * horizontal_coefficients, int coefficient_width ) +{ + float const * output_end = output_buffer + output_sub_size * STBIR__horizontal_channels; + float STBIR_SIMD_STREAMOUT_PTR( * ) output = output_buffer; + stbir__3_coeff_setup(); + STBIR_SIMD_NO_UNROLL_LOOP_START + do { + float const * decode = decode_buffer + horizontal_contributors->n0 * STBIR__horizontal_channels; + float const * hc = horizontal_coefficients; + stbir__4_coeff_start(); + stbir__4_coeff_continue_from_4(4); + stbir__3_coeff_remnant(8); + stbir__store_output(); + } while ( output < output_end ); +} + +static void STBIR_chans( stbir__horizontal_gather_,_channels_with_12_coeffs)( float * output_buffer, unsigned int output_sub_size, float const * decode_buffer, stbir__contributors const * horizontal_contributors, float const * horizontal_coefficients, int coefficient_width ) +{ + float const * output_end = output_buffer + output_sub_size * STBIR__horizontal_channels; + float STBIR_SIMD_STREAMOUT_PTR( * ) output = output_buffer; + STBIR_SIMD_NO_UNROLL_LOOP_START + do { + float const * decode = decode_buffer + horizontal_contributors->n0 * STBIR__horizontal_channels; + float const * hc = horizontal_coefficients; + stbir__4_coeff_start(); + stbir__4_coeff_continue_from_4(4); + stbir__4_coeff_continue_from_4(8); + stbir__store_output(); + } while ( output < output_end ); +} + +static void STBIR_chans( stbir__horizontal_gather_,_channels_with_n_coeffs_mod0 )( float * output_buffer, unsigned int output_sub_size, float const * decode_buffer, stbir__contributors const * horizontal_contributors, float const * horizontal_coefficients, int coefficient_width ) +{ + float const * output_end = output_buffer + output_sub_size * STBIR__horizontal_channels; + float STBIR_SIMD_STREAMOUT_PTR( * ) output = output_buffer; + STBIR_SIMD_NO_UNROLL_LOOP_START + do { + float const * decode = decode_buffer + horizontal_contributors->n0 * STBIR__horizontal_channels; + int n = ( ( horizontal_contributors->n1 - horizontal_contributors->n0 + 1 ) - 4 + 3 ) >> 2; + float const * hc = horizontal_coefficients; + + stbir__4_coeff_start(); + STBIR_SIMD_NO_UNROLL_LOOP_START + do { + hc += 4; + decode += STBIR__horizontal_channels * 4; + stbir__4_coeff_continue_from_4( 0 ); + --n; + } while ( n > 0 ); + stbir__store_output(); + } while ( output < output_end ); +} + +static void STBIR_chans( stbir__horizontal_gather_,_channels_with_n_coeffs_mod1 )( float * output_buffer, unsigned int output_sub_size, float const * decode_buffer, stbir__contributors const * horizontal_contributors, float const * horizontal_coefficients, int coefficient_width ) +{ + float const * output_end = output_buffer + output_sub_size * STBIR__horizontal_channels; + float STBIR_SIMD_STREAMOUT_PTR( * ) output = output_buffer; + STBIR_SIMD_NO_UNROLL_LOOP_START + do { + float const * decode = decode_buffer + horizontal_contributors->n0 * STBIR__horizontal_channels; + int n = ( ( horizontal_contributors->n1 - horizontal_contributors->n0 + 1 ) - 5 + 3 ) >> 2; + float const * hc = horizontal_coefficients; + + stbir__4_coeff_start(); + STBIR_SIMD_NO_UNROLL_LOOP_START + do { + hc += 4; + decode += STBIR__horizontal_channels * 4; + stbir__4_coeff_continue_from_4( 0 ); + --n; + } while ( n > 0 ); + stbir__1_coeff_remnant( 4 ); + stbir__store_output(); + } while ( output < output_end ); +} + +static void STBIR_chans( stbir__horizontal_gather_,_channels_with_n_coeffs_mod2 )( float * output_buffer, unsigned int output_sub_size, float const * decode_buffer, stbir__contributors const * horizontal_contributors, float const * horizontal_coefficients, int coefficient_width ) +{ + float const * output_end = output_buffer + output_sub_size * STBIR__horizontal_channels; + float STBIR_SIMD_STREAMOUT_PTR( * ) output = output_buffer; + STBIR_SIMD_NO_UNROLL_LOOP_START + do { + float const * decode = decode_buffer + horizontal_contributors->n0 * STBIR__horizontal_channels; + int n = ( ( horizontal_contributors->n1 - horizontal_contributors->n0 + 1 ) - 6 + 3 ) >> 2; + float const * hc = horizontal_coefficients; + + stbir__4_coeff_start(); + STBIR_SIMD_NO_UNROLL_LOOP_START + do { + hc += 4; + decode += STBIR__horizontal_channels * 4; + stbir__4_coeff_continue_from_4( 0 ); + --n; + } while ( n > 0 ); + stbir__2_coeff_remnant( 4 ); + + stbir__store_output(); + } while ( output < output_end ); +} + +static void STBIR_chans( stbir__horizontal_gather_,_channels_with_n_coeffs_mod3 )( float * output_buffer, unsigned int output_sub_size, float const * decode_buffer, stbir__contributors const * horizontal_contributors, float const * horizontal_coefficients, int coefficient_width ) +{ + float const * output_end = output_buffer + output_sub_size * STBIR__horizontal_channels; + float STBIR_SIMD_STREAMOUT_PTR( * ) output = output_buffer; + stbir__3_coeff_setup(); + STBIR_SIMD_NO_UNROLL_LOOP_START + do { + float const * decode = decode_buffer + horizontal_contributors->n0 * STBIR__horizontal_channels; + int n = ( ( horizontal_contributors->n1 - horizontal_contributors->n0 + 1 ) - 7 + 3 ) >> 2; + float const * hc = horizontal_coefficients; + + stbir__4_coeff_start(); + STBIR_SIMD_NO_UNROLL_LOOP_START + do { + hc += 4; + decode += STBIR__horizontal_channels * 4; + stbir__4_coeff_continue_from_4( 0 ); + --n; + } while ( n > 0 ); + stbir__3_coeff_remnant( 4 ); + + stbir__store_output(); + } while ( output < output_end ); +} + +static stbir__horizontal_gather_channels_func * STBIR_chans(stbir__horizontal_gather_,_channels_with_n_coeffs_funcs)[4]= +{ + STBIR_chans(stbir__horizontal_gather_,_channels_with_n_coeffs_mod0), + STBIR_chans(stbir__horizontal_gather_,_channels_with_n_coeffs_mod1), + STBIR_chans(stbir__horizontal_gather_,_channels_with_n_coeffs_mod2), + STBIR_chans(stbir__horizontal_gather_,_channels_with_n_coeffs_mod3), +}; + +static stbir__horizontal_gather_channels_func * STBIR_chans(stbir__horizontal_gather_,_channels_funcs)[12]= +{ + STBIR_chans(stbir__horizontal_gather_,_channels_with_1_coeff), + STBIR_chans(stbir__horizontal_gather_,_channels_with_2_coeffs), + STBIR_chans(stbir__horizontal_gather_,_channels_with_3_coeffs), + STBIR_chans(stbir__horizontal_gather_,_channels_with_4_coeffs), + STBIR_chans(stbir__horizontal_gather_,_channels_with_5_coeffs), + STBIR_chans(stbir__horizontal_gather_,_channels_with_6_coeffs), + STBIR_chans(stbir__horizontal_gather_,_channels_with_7_coeffs), + STBIR_chans(stbir__horizontal_gather_,_channels_with_8_coeffs), + STBIR_chans(stbir__horizontal_gather_,_channels_with_9_coeffs), + STBIR_chans(stbir__horizontal_gather_,_channels_with_10_coeffs), + STBIR_chans(stbir__horizontal_gather_,_channels_with_11_coeffs), + STBIR_chans(stbir__horizontal_gather_,_channels_with_12_coeffs), +}; + +#undef STBIR__horizontal_channels +#undef STB_IMAGE_RESIZE_DO_HORIZONTALS +#undef stbir__1_coeff_only +#undef stbir__1_coeff_remnant +#undef stbir__2_coeff_only +#undef stbir__2_coeff_remnant +#undef stbir__3_coeff_only +#undef stbir__3_coeff_remnant +#undef stbir__3_coeff_setup +#undef stbir__4_coeff_start +#undef stbir__4_coeff_continue_from_4 +#undef stbir__store_output +#undef stbir__store_output_tiny +#undef STBIR_chans + +#endif // HORIZONALS + +#undef STBIR_strs_join2 +#undef STBIR_strs_join1 + +#endif // STB_IMAGE_RESIZE_DO_HORIZONTALS/VERTICALS/CODERS + +/* +------------------------------------------------------------------------------ +This software is available under 2 licenses -- choose whichever you prefer. +------------------------------------------------------------------------------ +ALTERNATIVE A - MIT License +Copyright (c) 2017 Sean Barrett +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +------------------------------------------------------------------------------ +ALTERNATIVE B - Public Domain (www.unlicense.org) +This is free and unencumbered software released into the public domain. +Anyone is free to copy, modify, publish, use, compile, sell, or distribute this +software, either in source code form or as a compiled binary, for any purpose, +commercial or non-commercial, and by any means. +In jurisdictions that recognize copyright laws, the author or authors of this +software dedicate any and all copyright interest in the software to the public +domain. We make this dedication for the benefit of the public at large and to +the detriment of our heirs and successors. We intend this dedication to be an +overt act of relinquishment in perpetuity of all present and future rights to +this software under copyright law. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +------------------------------------------------------------------------------ +*/ diff --git a/src/native/linux/systray.go b/src/native/linux/systray.go deleted file mode 100644 index 0e162159..00000000 --- a/src/native/linux/systray.go +++ /dev/null @@ -1,404 +0,0 @@ -// Package systray is a Linux-only Go library to place an icon and menu in the notification area. -package systray - -import ( - "fmt" - "log" - "runtime" - "sync" - "sync/atomic" -) - -var ( - systrayReady func() - systrayExit func() - menuItems = make(map[uint32]*MenuItem) - menuItemsLock sync.RWMutex - - currentID = uint32(0) - quitOnce sync.Once - dClickTimeMinInterval int64 = 500 -) - -func init() { - runtime.LockOSThread() -} - -type IMenu interface { - ShowMenu() error -} - -// MenuItem is used to keep track each menu item of systray. -// Don't create it directly, use the one systray.AddMenuItem() returned -type MenuItem struct { - // ClickedCh is the channel which will be notified when the menu item is clicked - click func() - - // id uniquely identify a menu item, not supposed to be modified - id uint32 - // title is the text shown on menu item - title string - // tooltip is the text shown when pointing to menu item - tooltip string - // shortcutKey Menu shortcut key - shortcutKey string - // disabled menu item is grayed out and has no effect when clicked - disabled bool - // checked menu item has a tick before the title - checked bool - // has the menu item a checkbox (Linux) - isCheckable bool - // parent item, for sub menus - parent *MenuItem -} - -// ID returns the unique ID of the menu item -func (item *MenuItem) ID() uint32 { return item.id } - -func (item *MenuItem) Click(fn func()) { - item.click = fn -} - -func (item *MenuItem) String() string { - if item.parent == nil { - return fmt.Sprintf("MenuItem[%d, %q]", item.id, item.title) - } - return fmt.Sprintf("MenuItem[%d, parent %d, %q]", item.id, item.parent.id, item.title) -} - -// newMenuItem returns a populated MenuItem object -func newMenuItem(title string, tooltip string, parent *MenuItem) *MenuItem { - return &MenuItem{ - id: atomic.AddUint32(¤tID, 1), - title: title, - tooltip: tooltip, - shortcutKey: "", - disabled: false, - checked: false, - isCheckable: false, - parent: parent, - } -} - -// Run initializes GUI and starts the event loop, then invokes the onReady -// callback. It blocks until systray.Quit() is called. -func Run(onReady, onExit func()) { - setInternalLoop(true) - Register(onReady, onExit) - - nativeLoop() -} - -// 设置鼠标左键双击事件的时间间隔 默认500毫秒 -func SetDClickTimeMinInterval(value int64) { - dClickTimeMinInterval = value -} - -// 设置托盘鼠标左键点击事件 -func SetOnClick(fn func(menu IMenu)) { - setOnClick(fn) -} - -// 设置托盘鼠标左键双击事件 -func SetOnDClick(fn func(menu IMenu)) { - setOnDClick(fn) -} - -// 设置托盘鼠标右键事件反馈回调 -// 支持windows 和 macosx,不支持linux -// 设置事件,菜单默认将不展示,通过menu.ShowMenu()函数显示 -// 未设置事件,默认右键显示托盘菜单 -// macosx ShowMenu()只支持OnRClick函数内调用 -func SetOnRClick(fn func(menu IMenu)) { - setOnRClick(fn) -} - -// RunWithExternalLoop allows the systemtray module to operate with other tookits. -// The returned start and end functions should be called by the toolkit when the application has started and will end. -func RunWithExternalLoop(onReady, onExit func()) (start, end func()) { - Register(onReady, onExit) - - return nativeStart, func() { - // External loop: no nativeLoop is running, so perform full teardown here. - // First signal quit (idempotent), then run nativeEnd() to unexport and release resources. - Quit() - nativeEnd() - } -} - -// Register initializes GUI and registers the callbacks but relies on the -// caller to run the event loop somewhere else. It's useful if the program -// needs to show other UI elements, for example, webview. -// To overcome some OS weirdness, On macOS versions before Catalina, calling -// this does exactly the same as Run(). -func Register(onReady func(), onExit func()) { - if onReady == nil { - systrayReady = nil - } else { - var readyCh = make(chan interface{}) - // Run onReady on separate goroutine to avoid blocking event loop - go func() { - <-readyCh - onReady() - }() - systrayReady = func() { - systrayReady = nil - close(readyCh) - } - } - // unlike onReady, onExit runs in the event loop to make sure it has time to - // finish before the process terminates - if onExit == nil { - onExit = func() {} - } - systrayExit = onExit - registerSystray() -} - -// ResetMenu will remove all menu items -func ResetMenu() { - resetMenu() -} - -// Quit the systray -func Quit() { - quitOnce.Do(quit) -} - -// AddMenuItem adds a menu item with the designated title and tooltip. -// It can be safely invoked from different goroutines. -// Created menu items are checkable on Windows and OSX by default. For Linux you have to use AddMenuItemCheckbox -func AddMenuItem(title string, tooltip string) *MenuItem { - item := newMenuItem(title, tooltip, nil) - item.update() - return item -} - -// AddMenuItemCheckbox adds a menu item with the designated title and tooltip and a checkbox for Linux. -// It can be safely invoked from different goroutines. -// On Windows and OSX this is the same as calling AddMenuItem -func AddMenuItemCheckbox(title string, tooltip string, checked bool) *MenuItem { - item := newMenuItem(title, tooltip, nil) - item.isCheckable = true - item.checked = checked - item.update() - return item -} - -// AddSeparator adds a separator bar to the menu -func AddSeparator() { - addSeparator(atomic.AddUint32(¤tID, 1)) -} - -// AddSubMenuItem adds a nested sub-menu item with the designated title and tooltip. -// It can be safely invoked from different goroutines. -// Created menu items are checkable on Windows and OSX by default. For Linux you have to use AddSubMenuItemCheckbox -func (item *MenuItem) AddSubMenuItem(title string, tooltip string) *MenuItem { - child := newMenuItem(title, tooltip, item) - child.update() - return child -} - -// AddSubMenuItemCheckbox adds a nested sub-menu item with the designated title and tooltip and a checkbox for Linux. -// It can be safely invoked from different goroutines. -// On Windows and OSX this is the same as calling AddSubMenuItem -func (item *MenuItem) AddSubMenuItemCheckbox(title string, tooltip string, checked bool) *MenuItem { - child := newMenuItem(title, tooltip, item) - child.isCheckable = true - child.checked = checked - child.update() - return child -} - -// SetTitle set the text to display on a menu item -func (item *MenuItem) SetTitle(title string) { - item.title = title - item.update() -} - -// SetTooltip set the tooltip to show when mouse hover -func (item *MenuItem) SetTooltip(tooltip string) { - item.tooltip = tooltip - item.update() -} - -// Disabled checks if the menu item is disabled -func (item *MenuItem) Disabled() bool { - return item.disabled -} - -// Enable a menu item regardless if it's previously enabled or not -func (item *MenuItem) Enable() { - item.disabled = false - item.update() -} - -// Disable a menu item regardless if it's previously disabled or not -func (item *MenuItem) Disable() { - item.disabled = true - item.update() -} - -// Hide hides a menu item -func (item *MenuItem) Hide() { - hideMenuItem(item) -} - -// Show shows a previously hidden menu item -func (item *MenuItem) Show() { - showMenuItem(item) -} - -// Checked returns if the menu item has a check mark -func (item *MenuItem) Checked() bool { - return item.checked -} - -// Check a menu item regardless if it's previously checked or not -func (item *MenuItem) Check() { - item.checked = true - item.update() -} - -// Uncheck a menu item regardless if it's previously unchecked or not -func (item *MenuItem) Uncheck() { - item.checked = false - item.update() -} - -// update propagates changes on a menu item to systray -func (item *MenuItem) update() { - menuItemsLock.Lock() - menuItems[item.id] = item - menuItemsLock.Unlock() - addOrUpdateMenuItem(item) -} - -var menuItemSelected func(id uint32) - -func SetOnMenuItemSelected(fn func(id uint32)) { - menuItemSelected = fn -} - -func systrayMenuItemSelected(id uint32) { - menuItemsLock.RLock() - item, ok := menuItems[id] - menuItemsLock.RUnlock() - if !ok { - log.Printf("systray error: no menu item with ID %d\n", id) - return - } - if item.click != nil { - item.click() - } - if menuItemSelected != nil { - menuItemSelected(id) - } -} - -// --- Helpers to control menu items by ID for external (JNA) callers --- - -// getMenuItem returns the menu item and whether it exists -func getMenuItem(id uint32) (*MenuItem, bool) { - menuItemsLock.RLock() - defer menuItemsLock.RUnlock() - item, ok := menuItems[id] - return item, ok -} - -// AddSubMenuItemByID creates a child under the given parent id and returns the new id; 0 on failure -func AddSubMenuItemByID(parentID uint32, title, tooltip string) uint32 { - parent, ok := getMenuItem(parentID) - if !ok || parent == nil { - return 0 - } - child := parent.AddSubMenuItem(title, tooltip) - return child.ID() -} - -// AddSubMenuItemCheckboxByID creates a checkable child under the given parent id and returns the new id; 0 on failure -func AddSubMenuItemCheckboxByID(parentID uint32, title, tooltip string, checked bool) uint32 { - parent, ok := getMenuItem(parentID) - if !ok || parent == nil { - return 0 - } - child := parent.AddSubMenuItemCheckbox(title, tooltip, checked) - return child.ID() -} - -// AddSeparatorByID adds a separator under the given parent item id. If parentID is 0, it adds to the root menu. -func AddSeparatorByID(parentID uint32) { - addSeparatorUnder(parentID, atomic.AddUint32(¤tID, 1)) -} - -// SetMenuItemTitleByID sets the title of a menu item; returns false if not found -func SetMenuItemTitleByID(id uint32, title string) bool { - if item, ok := getMenuItem(id); ok { - item.SetTitle(title) - return true - } - return false -} - -// EnableMenuItemByID enables a menu item; returns false if not found -func EnableMenuItemByID(id uint32) bool { - if item, ok := getMenuItem(id); ok { - item.Enable() - return true - } - return false -} - -// DisableMenuItemByID disables a menu item; returns false if not found -func DisableMenuItemByID(id uint32) bool { - if item, ok := getMenuItem(id); ok { - item.Disable() - return true - } - return false -} - -// ShowMenuItemByID shows a menu item; returns false if not found -func ShowMenuItemByID(id uint32) bool { - if item, ok := getMenuItem(id); ok { - item.Show() - return true - } - return false -} - -// HideMenuItemByID hides a menu item; returns false if not found -func HideMenuItemByID(id uint32) bool { - if item, ok := getMenuItem(id); ok { - item.Hide() - return true - } - return false -} - -// CheckMenuItemByID checks a menu item; returns false if not found -func CheckMenuItemByID(id uint32) bool { - if item, ok := getMenuItem(id); ok { - item.Check() - return true - } - return false -} - -// UncheckMenuItemByID unchecks a menu item; returns false if not found -func UncheckMenuItemByID(id uint32) bool { - if item, ok := getMenuItem(id); ok { - item.Uncheck() - return true - } - return false -} - -// SetMenuItemIconByID sets the icon of a menu item; returns false if not found -func SetMenuItemIconByID(id uint32, iconBytes []byte) bool { - if item, ok := getMenuItem(id); ok { - item.SetIcon(iconBytes) - return true - } - return false -} diff --git a/src/native/linux/systray.h b/src/native/linux/systray.h deleted file mode 100644 index 774f618e..00000000 --- a/src/native/linux/systray.h +++ /dev/null @@ -1,27 +0,0 @@ -#include "stdbool.h" - -extern void systray_ready(); -extern void systray_on_exit(); -extern void systray_menu_item_selected(int menu_id); -extern void systray_on_click(); -extern void systray_on_rclick(); - -void registerSystray(void); -void nativeEnd(void); -int nativeLoop(void); -void nativeStart(void); - -void setIcon(const char* iconBytes, int length, bool template); -void setMenuItemIcon(const char* iconBytes, int length, int menuId, bool template); -void setTitle(char* title); -void setTooltip(char* tooltip); -void add_or_update_menu_item(int menuId, int parentMenuId, char* title, char* tooltip, char* shortcutKey, short disabled, short checked, short isCheckable); -void add_separator(int menuId); -void hide_menu_item(int menuId); -void show_menu_item(int menuId); -void reset_menu(); -void create_menu(); -void show_menu(); -void set_menu_nil(); -void quit(); -void enable_on_click(); diff --git a/src/native/linux/systray_menu_unix.go b/src/native/linux/systray_menu_unix.go deleted file mode 100644 index 1433fac1..00000000 --- a/src/native/linux/systray_menu_unix.go +++ /dev/null @@ -1,345 +0,0 @@ -//go:build linux || freebsd || openbsd || netbsd -// +build linux freebsd openbsd netbsd - -package systray - -import ( - "log" - - "github.com/godbus/dbus/v5" - "github.com/godbus/dbus/v5/prop" - - "github.com/energye/systray/internal/generated/menu" -) - -// SetIcon sets the icon of a menu item. -// iconBytes should be the content of .ico/.jpg/.png -func (item *MenuItem) SetIcon(iconBytes []byte) { - instance.menuLock.Lock() - defer instance.menuLock.Unlock() - m, exists := findLayout(int32(item.id)) - if exists { - m.V1["icon-data"] = dbus.MakeVariant(iconBytes) - refresh() - } -} - -// copyLayout makes full copy of layout -func copyLayout(in *menuLayout, depth int32) *menuLayout { - out := menuLayout{ - V0: in.V0, - V1: make(map[string]dbus.Variant, len(in.V1)), - } - for k, v := range in.V1 { - out.V1[k] = v - } - if depth != 0 { - depth-- - out.V2 = make([]dbus.Variant, len(in.V2)) - for i, v := range in.V2 { - out.V2[i] = dbus.MakeVariant(copyLayout(v.Value().(*menuLayout), depth)) - } - } else { - out.V2 = []dbus.Variant{} - } - return &out -} - -// GetLayout is com.canonical.dbusmenu.GetLayout method. -func (t *tray) GetLayout(parentID int32, recursionDepth int32, propertyNames []string) (revision uint32, layout menuLayout, err *dbus.Error) { - instance.menuLock.Lock() - defer instance.menuLock.Unlock() - if m, ok := findLayout(parentID); ok { - // return copy of menu layout to prevent panic from cuncurrent access to layout - return instance.menuVersion, *copyLayout(m, recursionDepth), nil - } - return -} - -// GetGroupProperties is com.canonical.dbusmenu.GetGroupProperties method. -func (t *tray) GetGroupProperties(ids []int32, propertyNames []string) (properties []struct { - V0 int32 - V1 map[string]dbus.Variant -}, err *dbus.Error) { - instance.menuLock.Lock() - defer instance.menuLock.Unlock() - for _, id := range ids { - if m, ok := findLayout(id); ok { - p := struct { - V0 int32 - V1 map[string]dbus.Variant - }{ - V0: m.V0, - V1: make(map[string]dbus.Variant, len(m.V1)), - } - for k, v := range m.V1 { - p.V1[k] = v - } - properties = append(properties, p) - } - } - return -} - -// GetProperty is com.canonical.dbusmenu.GetProperty method. -func (t *tray) GetProperty(id int32, name string) (value dbus.Variant, err *dbus.Error) { - instance.menuLock.Lock() - defer instance.menuLock.Unlock() - if m, ok := findLayout(id); ok { - if p, ok := m.V1[name]; ok { - return p, nil - } - } - return -} - -// Event is com.canonical.dbusmenu.Event method. -func (t *tray) Event(id int32, eventID string, data dbus.Variant, timestamp uint32) (err *dbus.Error) { - if eventID == "clicked" { - systrayMenuItemSelected(uint32(id)) - } - return -} - -// EventGroup is com.canonical.dbusmenu.EventGroup method. -func (t *tray) EventGroup(events []struct { - V0 int32 - V1 string - V2 dbus.Variant - V3 uint32 -}) (idErrors []int32, err *dbus.Error) { - for _, event := range events { - if event.V1 == "clicked" { - systrayMenuItemSelected(uint32(event.V0)) - } - } - return -} - -// AboutToShow is com.canonical.dbusmenu.AboutToShow method. -func (t *tray) AboutToShow(id int32) (needUpdate bool, err *dbus.Error) { - return -} - -// AboutToShowGroup is com.canonical.dbusmenu.AboutToShowGroup method. -func (t *tray) AboutToShowGroup(ids []int32) (updatesNeeded []int32, idErrors []int32, err *dbus.Error) { - return -} - -func createMenuPropSpec() map[string]map[string]*prop.Prop { - instance.menuLock.Lock() - defer instance.menuLock.Unlock() - return map[string]map[string]*prop.Prop{ - "com.canonical.dbusmenu": { - "Version": { - Value: instance.menuVersion, - Writable: true, - Emit: prop.EmitTrue, - Callback: nil, - }, - "TextDirection": { - Value: "ltr", - Writable: false, - Emit: prop.EmitTrue, - Callback: nil, - }, - "Status": { - Value: "normal", - Writable: false, - Emit: prop.EmitTrue, - Callback: nil, - }, - "IconThemePath": { - Value: []string{}, - Writable: false, - Emit: prop.EmitTrue, - Callback: nil, - }, - }, - } -} - -// menuLayout is a named struct to map into generated bindings. It represents the layout of a menu item -type menuLayout = struct { - V0 int32 // the unique ID of this item - V1 map[string]dbus.Variant // properties for this menu item layout - V2 []dbus.Variant // child menu item layouts -} - -func addOrUpdateMenuItem(item *MenuItem) { - var layout *menuLayout - instance.menuLock.Lock() - defer instance.menuLock.Unlock() - m, exists := findLayout(int32(item.id)) - if exists { - layout = m - } else { - layout = &menuLayout{ - V0: int32(item.id), - V1: map[string]dbus.Variant{}, - V2: []dbus.Variant{}, - } - - parent := instance.menu - if item.parent != nil { - m, ok := findLayout(int32(item.parent.id)) - if ok { - parent = m - parent.V1["children-display"] = dbus.MakeVariant("submenu") - } - } - parent.V2 = append(parent.V2, dbus.MakeVariant(layout)) - } - - applyItemToLayout(item, layout) - // Always refresh after applying layout so KDE receives LayoutUpdated even for newly created items - refresh() - // When running under GNOME, ensure the SNI Menu prop points to the real menu path (exit no-menu state) - if noMenuPathForEnvironment() == dbus.ObjectPath("/") { - setMenuPropTo(dbus.ObjectPath(menuPath)) - } -} - -func addSeparator(id uint32) { - instance.menuLock.Lock() - defer instance.menuLock.Unlock() - layout := &menuLayout{ - V0: int32(id), - V1: map[string]dbus.Variant{ - "type": dbus.MakeVariant("separator"), - }, - V2: []dbus.Variant{}, - } - instance.menu.V2 = append(instance.menu.V2, dbus.MakeVariant(layout)) - refresh() - // On GNOME, ensure SNI Menu points to the real menu when items are present (exit no-menu state) - if noMenuPathForEnvironment() == dbus.ObjectPath("/") { - setMenuPropTo(dbus.ObjectPath(menuPath)) - } -} - -// addSeparatorUnder appends a separator under the given parent menu item id (0 targets root) -func addSeparatorUnder(parentID uint32, id uint32) { - instance.menuLock.Lock() - defer instance.menuLock.Unlock() - var parent *menuLayout - if parentID == 0 { - parent = instance.menu - } else { - p, ok := findLayout(int32(parentID)) - if !ok || p == nil { - return - } - parent = p - parent.V1["children-display"] = dbus.MakeVariant("submenu") - } - layout := &menuLayout{ - V0: int32(id), - V1: map[string]dbus.Variant{ - "type": dbus.MakeVariant("separator"), - }, - V2: []dbus.Variant{}, - } - parent.V2 = append(parent.V2, dbus.MakeVariant(layout)) - refresh() - if noMenuPathForEnvironment() == dbus.ObjectPath("/") { - setMenuPropTo(dbus.ObjectPath(menuPath)) - } -} - -func applyItemToLayout(in *MenuItem, out *menuLayout) { - out.V1["enabled"] = dbus.MakeVariant(!in.disabled) - out.V1["label"] = dbus.MakeVariant(in.title) - - if in.isCheckable { - out.V1["toggle-type"] = dbus.MakeVariant("checkmark") - if in.checked { - out.V1["toggle-state"] = dbus.MakeVariant(1) - } else { - out.V1["toggle-state"] = dbus.MakeVariant(0) - } - } else { - out.V1["toggle-type"] = dbus.MakeVariant("") - out.V1["toggle-state"] = dbus.MakeVariant(0) - } -} - -func findLayout(id int32) (*menuLayout, bool) { - if id == 0 { - return instance.menu, true - } - return findSubLayout(id, instance.menu.V2) -} - -func findSubLayout(id int32, vals []dbus.Variant) (*menuLayout, bool) { - for _, i := range vals { - item := i.Value().(*menuLayout) - if item.V0 == id { - return item, true - } - - if len(item.V2) > 0 { - child, ok := findSubLayout(id, item.V2) - if ok { - return child, true - } - } - } - - return nil, false -} - -func hideMenuItem(item *MenuItem) { - instance.menuLock.Lock() - defer instance.menuLock.Unlock() - m, exists := findLayout(int32(item.id)) - if exists { - m.V1["visible"] = dbus.MakeVariant(false) - refresh() - } -} - -func showMenuItem(item *MenuItem) { - instance.menuLock.Lock() - defer instance.menuLock.Unlock() - m, exists := findLayout(int32(item.id)) - if exists { - m.V1["visible"] = dbus.MakeVariant(true) - refresh() - } -} - -func refresh() { - if instance.conn == nil || instance.menuProps == nil { - return - } - instance.menuVersion++ - dbusErr := instance.menuProps.Set("com.canonical.dbusmenu", "Version", - dbus.MakeVariant(instance.menuVersion)) - if dbusErr != nil { - log.Printf("systray error: failed to update menu version: %s\n", dbusErr) - return - } - err := menu.Emit(instance.conn, &menu.Dbusmenu_LayoutUpdatedSignal{ - Path: menuPath, - Body: &menu.Dbusmenu_LayoutUpdatedSignalBody{ - Revision: instance.menuVersion, - }, - }) - if err != nil { - log.Printf("systray error: failed to emit layout updated signal: %s\n", err) - } - -} - -func resetMenu() { - instance.menuLock.Lock() - defer instance.menuLock.Unlock() - instance.menu = &menuLayout{} - instance.menuVersion++ - refresh() - // GNOME-only: advertise "/" when there's no menu; do not modify the property for other environments - if noMenuPathForEnvironment() == dbus.ObjectPath("/") { - setMenuPropTo(dbus.ObjectPath("/")) - } -} diff --git a/src/native/linux/systray_other.go b/src/native/linux/systray_other.go deleted file mode 100644 index 0d70bcbb..00000000 --- a/src/native/linux/systray_other.go +++ /dev/null @@ -1,183 +0,0 @@ -//go:build !linux && !freebsd && !openbsd && !netbsd -// +build !linux,!freebsd,!openbsd,!netbsd - -package systray - -import ( - "errors" - "fmt" - "runtime" -) - -var errUnsupportedPlatform = errors.New("systray: unsupported platform, this library is Linux-only") - -// IMenu interface -type IMenu interface { - ShowMenu() error -} - -// Run initializes GUI and starts the event loop, then invokes the onReady -// callback. It blocks until systray.Quit() is called. -func Run(onReady, onExit func()) { - fmt.Printf("systray: unsupported platform '%s', this library is Linux-only\n", runtime.GOOS) - panic(errUnsupportedPlatform) -} - -// RunWithExternalLoop allows the systemtray module to operate with other tookits. -// The returned start and end functions should be called by the toolkit when the application has started and will end. -func RunWithExternalLoop(onReady, onExit func()) (start, end func()) { - fmt.Printf("systray: unsupported platform '%s', this library is Linux-only\n", runtime.GOOS) - panic(errUnsupportedPlatform) -} - -// CreateMenu is a no-op on non-Linux platforms -func CreateMenu() { - panic(errUnsupportedPlatform) -} - -// SetMenuNil is a no-op on non-Linux platforms -func SetMenuNil() { - panic(errUnsupportedPlatform) -} - -// SetIcon sets the systray icon. -func SetIcon(iconBytes []byte) { - panic(errUnsupportedPlatform) -} - -// SetTemplateIcon sets the systray icon as a template icon. -func SetTemplateIcon(templateIconBytes []byte, regularIconBytes []byte) { - panic(errUnsupportedPlatform) -} - -// SetTitle sets the systray title. -func SetTitle(title string) { - panic(errUnsupportedPlatform) -} - -// SetTooltip sets the systray tooltip. -func SetTooltip(tooltip string) { - panic(errUnsupportedPlatform) -} - -// SetOnClick sets the on click callback. -func SetOnClick(fn func(menu IMenu)) { - panic(errUnsupportedPlatform) -} - -// SetOnDClick sets the on double click callback. -func SetOnDClick(fn func(menu IMenu)) { - panic(errUnsupportedPlatform) -} - -// SetOnRClick sets the on right click callback. -func SetOnRClick(fn func(menu IMenu)) { - panic(errUnsupportedPlatform) -} - -// SetDClickTimeMinInterval sets the minimum time interval for double clicks. -func SetDClickTimeMinInterval(value int64) { - panic(errUnsupportedPlatform) -} - -// Quit the systray. -func Quit() { - panic(errUnsupportedPlatform) -} - -// AddMenuItem adds a menu item. -func AddMenuItem(title string, tooltip string) *MenuItem { - panic(errUnsupportedPlatform) -} - -// AddMenuItemCheckbox adds a menu item with a checkbox. -func AddMenuItemCheckbox(title string, tooltip string, checked bool) *MenuItem { - panic(errUnsupportedPlatform) -} - -// AddSeparator adds a separator. -func AddSeparator() { - panic(errUnsupportedPlatform) -} - -// ResetMenu resets the menu. -func ResetMenu() { - panic(errUnsupportedPlatform) -} - -// MenuItem methods - -// AddSubMenuItem adds a submenu item. -func (item *MenuItem) AddSubMenuItem(title string, tooltip string) *MenuItem { - panic(errUnsupportedPlatform) -} - -// AddSubMenuItemCheckbox adds a submenu item with a checkbox. -func (item *MenuItem) AddSubMenuItemCheckbox(title string, tooltip string, checked bool) *MenuItem { - panic(errUnsupportedPlatform) -} - -// SetTitle sets the menu item title. -func (item *MenuItem) SetTitle(title string) { - panic(errUnsupportedPlatform) -} - -// SetTooltip sets the menu item tooltip. -func (item *MenuItem) SetTooltip(tooltip string) { - panic(errUnsupportedPlatform) -} - -// Disabled checks if the menu item is disabled. -func (item *MenuItem) Disabled() bool { - panic(errUnsupportedPlatform) -} - -// Enable enables the menu item. -func (item *MenuItem) Enable() { - panic(errUnsupportedPlatform) -} - -// Disable disables the menu item. -func (item *MenuItem) Disable() { - panic(errUnsupportedPlatform) -} - -// Hide hides the menu item. -func (item *MenuItem) Hide() { - panic(errUnsupportedPlatform) -} - -// Show shows the menu item. -func (item *MenuItem) Show() { - panic(errUnsupportedPlatform) -} - -// Checked returns if the menu item is checked. -func (item *MenuItem) Checked() bool { - panic(errUnsupportedPlatform) -} - -// Check checks the menu item. -func (item *MenuItem) Check() { - panic(errUnsupportedPlatform) -} - -// Uncheck unchecks the menu item. -func (item *MenuItem) Uncheck() { - panic(errUnsupportedPlatform) -} - -// Click sets the click callback. -func (item *MenuItem) Click(fn func()) { - panic(errUnsupportedPlatform) -} - -// SetIcon sets the menu item icon. -func (item *MenuItem) SetIcon(iconBytes []byte) { - panic(errUnsupportedPlatform) -} - -// SetTemplateIcon sets the menu item template icon. -func (item *MenuItem) SetTemplateIcon(templateIconBytes []byte, regularIconBytes []byte) { - panic(errUnsupportedPlatform) -} diff --git a/src/native/linux/systray_unix.go b/src/native/linux/systray_unix.go deleted file mode 100644 index aaa2576b..00000000 --- a/src/native/linux/systray_unix.go +++ /dev/null @@ -1,511 +0,0 @@ -//go:build linux || freebsd || openbsd || netbsd -// +build linux freebsd openbsd netbsd - -// Note: you need github.com/knightpp/dbus-codegen-go installed from "custom" branch -//go:generate dbus-codegen-go -prefix org.kde -package notifier -output internal/generated/notifier/status_notifier_item.go internal/StatusNotifierItem.xml -//go:generate dbus-codegen-go -prefix com.canonical -package menu -output internal/generated/menu/dbus_menu.go internal/DbusMenu.xml - -package systray - -import ( - "bytes" - "fmt" - "image" - _ "image/jpeg" - _ "image/png" - "log" - "os" - "strings" - "sync" - "time" - - "github.com/godbus/dbus/v5/introspect" - "github.com/godbus/dbus/v5/prop" - - "github.com/energye/systray/internal/generated/menu" - "github.com/energye/systray/internal/generated/notifier" - dbus "github.com/godbus/dbus/v5" - - "golang.org/x/image/draw" -) - -const ( - path = "/StatusNotifierItem" - menuPath = "/StatusNotifierMenu" -) - -var ( - // to signal quitting the internal main loop - quitChan = make(chan struct{}) - - // instance is the current instance of our DBus tray server - instance = &tray{menu: &menuLayout{}, menuVersion: 1} -) - -// SetTemplateIcon sets the systray icon as a template icon (on macOS), falling back -// to a regular icon on other platforms. On Linux we just set the regular icon. -func SetTemplateIcon(_ []byte, regularIconBytes []byte) { - SetIcon(regularIconBytes) -} - -// SetIcon sets the systray icon (PNG/JPG/ICO supported by image/*). -func SetIcon(iconBytes []byte) { - instance.lock.Lock() - instance.iconData = iconBytes - props := instance.props - conn := instance.conn - instance.lock.Unlock() - - if props == nil { - return - } - - // Build a multi-resolution pixmap list (ARGB32 big-endian) for better quality. - pixmaps := buildPixmaps(iconBytes) - props.SetMust("org.kde.StatusNotifierItem", "IconPixmap", pixmaps) - - // Keep tooltip icon consistent with main icon - props.SetMust("org.kde.StatusNotifierItem", "ToolTip", - dbus.MakeVariant(tooltip{V2: instance.tooltipTitle, V1: pixmaps})) - - if conn == nil { - return - } - - if err := notifier.Emit(conn, ¬ifier.StatusNotifierItem_NewIconSignal{ - Path: path, - Body: ¬ifier.StatusNotifierItem_NewIconSignalBody{}, - }); err != nil { - log.Printf("systray error: failed to emit new icon signal: %s\n", err) - } -} - -// SetTitle sets the systray title. -func SetTitle(t string) { - instance.lock.Lock() - instance.title = t - props := instance.props - conn := instance.conn - instance.lock.Unlock() - - if props == nil { - return - } - if dbusErr := props.Set("org.kde.StatusNotifierItem", "Title", dbus.MakeVariant(t)); dbusErr != nil { - log.Printf("systray error: failed to set Title prop: %s\n", dbusErr) - return - } - if conn == nil { - return - } - if err := notifier.Emit(conn, ¬ifier.StatusNotifierItem_NewTitleSignal{ - Path: path, - Body: ¬ifier.StatusNotifierItem_NewTitleSignalBody{}, - }); err != nil { - log.Printf("systray error: failed to emit new title signal: %s\n", err) - } -} - -// SetTooltip sets the systray tooltip text and keeps the icon consistent. -func SetTooltip(tooltipTitle string) { - instance.lock.Lock() - instance.tooltipTitle = tooltipTitle - props := instance.props - iconData := instance.iconData - instance.lock.Unlock() - - if props == nil { - return - } - props.SetMust("org.kde.StatusNotifierItem", "ToolTip", - dbus.MakeVariant(tooltip{ - V2: tooltipTitle, - V1: buildPixmaps(iconData), // show same icon in tooltip if host uses it - }), - ) -} - -func (item *MenuItem) SetTemplateIcon(_ []byte, regularIconBytes []byte) { - item.SetIcon(regularIconBytes) -} - -func setInternalLoop(_ bool) {} - -// CreateMenu creates the tray menu (no-op on Linux). -func CreateMenu() {} - -// SetMenuNil sets the tray menu to nil (no-op on Linux). -func SetMenuNil() {} - -func registerSystray() {} - -func nativeLoop() int { - nativeStart() - <-quitChan - nativeEnd() - return 0 -} - -func nativeEnd() { - systrayExit() - - // Snapshot and clear shared state under lock to make teardown idempotent. - instance.lock.Lock() - conn := instance.conn - busName := instance.name - props := instance.props - menuProps := instance.menuProps - _ = menuProps - instance.conn = nil - instance.props = nil - instance.menuProps = nil - instance.iconData = nil - instance.title = "" - instance.tooltipTitle = "" - instance.lock.Unlock() - - if conn == nil { - return - } - - // Best-effort: mark status passive before unexport (optional). - if props != nil { - props.SetMust("org.kde.StatusNotifierItem", "Status", "Passive") - } - - // Unexport SNI and menu interfaces so the host removes our icon/menu immediately. - _ = notifier.UnexportStatusNotifierItem(conn, path) - _ = menu.UnexportDbusmenu(conn, menuPath) - - // Unexport Properties and Introspectable on both paths. - _ = conn.Export(nil, path, "org.freedesktop.DBus.Properties") - _ = conn.Export(nil, menuPath, "org.freedesktop.DBus.Properties") - _ = conn.Export(nil, path, "org.freedesktop.DBus.Introspectable") - _ = conn.Export(nil, menuPath, "org.freedesktop.DBus.Introspectable") - - // Release our well-known bus name so watchers drop the item instantly. - if busName != "" { - _, _ = conn.ReleaseName(busName) - } - - // Finally, close the connection asynchronously to avoid freezes. - done := make(chan struct{}) - go func() { - _ = conn.Close() - close(done) - }() - select { - case <-done: - case <-time.After(300 * time.Millisecond): - // Give up waiting; the background goroutine will finish later. - } -} - -func quit() { - close(quitChan) -} - -var usni = &UnimplementedStatusNotifierItem{} - -var ( - lastClickMu sync.Mutex - lastClickX int32 - lastClickY int32 -) - -type UnimplementedStatusNotifierItem struct { - contextMenu func(x int32, y int32) - activate func(x int32, y int32) - dActivate func(x int32, y int32) - secondaryActivate func(x int32, y int32) - scroll func(delta int32, orientation string) - dActivateTime int64 -} - -func (*UnimplementedStatusNotifierItem) iface() string { - return notifier.InterfaceStatusNotifierItem -} - -func (m *UnimplementedStatusNotifierItem) ContextMenu(x int32, y int32) (err *dbus.Error) { - // remember last right-click coordinates too (for context menu) - lastClickMu.Lock() - lastClickX, lastClickY = x, y - lastClickMu.Unlock() - if m.contextMenu != nil { - m.contextMenu(x, y) - } else { - err = &dbus.ErrMsgUnknownMethod - } - return -} - -func (m *UnimplementedStatusNotifierItem) Activate(x int32, y int32) (err *dbus.Error) { - // remember last click coordinates - lastClickMu.Lock() - lastClickX, lastClickY = x, y - lastClickMu.Unlock() - - if m.dActivateTime == 0 { - m.dActivateTime = time.Now().UnixMilli() - } else { - nowMilli := time.Now().UnixMilli() - if nowMilli-m.dActivateTime < dClickTimeMinInterval { - m.dActivateTime = dClickTimeMinInterval - if m.dActivate != nil { - m.dActivate(x, y) - return - } - } else { - m.dActivateTime = nowMilli - } - } - if m.activate != nil { - m.activate(x, y) - } else { - err = &dbus.ErrMsgUnknownMethod - } - return -} - -func (m *UnimplementedStatusNotifierItem) SecondaryActivate(x int32, y int32) (err *dbus.Error) { - // remember last click coordinates for secondary activation as well - lastClickMu.Lock() - lastClickX, lastClickY = x, y - lastClickMu.Unlock() - if m.secondaryActivate != nil { - m.secondaryActivate(x, y) - } else { - err = &dbus.ErrMsgUnknownMethod - } - return -} - -func (m *UnimplementedStatusNotifierItem) Scroll(delta int32, orientation string) (err *dbus.Error) { - if m.scroll != nil { - m.scroll(delta, orientation) - } else { - err = &dbus.ErrMsgUnknownMethod - } - return -} - -func setOnClick(fn func(menu IMenu)) { usni.activate = func(int32, int32) { fn(nil) } } -func setOnDClick(fn func(menu IMenu)) { usni.dActivate = func(int32, int32) { fn(nil) } } -func setOnRClick(dClick func(IMenu)) {} - -// GetLastClickXY returns the last click coordinates captured from Activate/ContextMenu/SecondaryActivate. -func GetLastClickXY() (int32, int32) { - lastClickMu.Lock() - x, y := lastClickX, lastClickY - lastClickMu.Unlock() - return x, y -} - -func nativeStart() { - if systrayReady != nil { - systrayReady() - } - conn, _ := dbus.ConnectSessionBus() - if err := notifier.ExportStatusNotifierItem(conn, path, usni); err != nil { - log.Printf("systray error: failed to export status notifier item: %s\n", err) - } - if err := menu.ExportDbusmenu(conn, menuPath, instance); err != nil { - log.Printf("systray error: failed to export status notifier item: %s\n", err) - } - - name := fmt.Sprintf("org.kde.StatusNotifierItem-%d-1", os.Getpid()) // id 1 for this process - if _, err := conn.RequestName(name, dbus.NameFlagDoNotQueue); err != nil { - log.Printf("systray error: failed to request name: %s\n", err) - } - // store requested bus name for later immediate release on dispose - instance.lock.Lock() - instance.name = name - instance.lock.Unlock() - props, err := prop.Export(conn, path, instance.createPropSpec()) - if err != nil { - log.Printf("systray error: failed to export notifier item properties to bus: %s\n", err) - return - } - menuProps, err := prop.Export(conn, menuPath, createMenuPropSpec()) - if err != nil { - log.Printf("systray error: failed to export notifier menu properties to bus: %s\n", err) - return - } - - node := introspect.Node{ - Name: path, - Interfaces: []introspect.Interface{ - introspect.IntrospectData, - prop.IntrospectData, - notifier.IntrospectDataStatusNotifierItem, - }, - } - if err := conn.Export(introspect.NewIntrospectable(&node), path, "org.freedesktop.DBus.Introspectable"); err != nil { - log.Printf("systray error: failed to export node introspection: %s\n", err) - return - } - - menuNode := introspect.Node{ - Name: menuPath, - Interfaces: []introspect.Interface{ - introspect.IntrospectData, - prop.IntrospectData, - menu.IntrospectDataDbusmenu, - }, - } - if err := conn.Export(introspect.NewIntrospectable(&menuNode), menuPath, "org.freedesktop.DBus.Introspectable"); err != nil { - log.Printf("systray error: failed to export menu node introspection: %s\n", err) - return - } - - instance.lock.Lock() - instance.conn = conn - instance.props = props - instance.menuProps = menuProps - instance.lock.Unlock() - - obj := conn.Object("org.kde.StatusNotifierWatcher", "/StatusNotifierWatcher") - call := obj.Call("org.kde.StatusNotifierWatcher.RegisterStatusNotifierItem", 0, path) - if call.Err != nil { - log.Printf("systray error: failed to register our icon with the notifier watcher (maybe no tray is running?): %s\n", call.Err) - } -} - -// tray holds DBus state -type tray struct { - conn *dbus.Conn - name string - iconData []byte - title string - tooltipTitle string - lock sync.Mutex - menu *menuLayout - menuLock sync.RWMutex - props *prop.Properties - menuProps *prop.Properties - menuVersion uint32 -} - -func (*tray) iface() string { return notifier.InterfaceStatusNotifierItem } - -func (t *tray) createPropSpec() map[string]map[string]*prop.Prop { - t.lock.Lock() - icon := t.iconData - title := t.title - tooltipTitle := t.tooltipTitle - t.lock.Unlock() - - return map[string]map[string]*prop.Prop{ - "org.kde.StatusNotifierItem": { - "Status": {Value: "Active", Writable: false, Emit: prop.EmitTrue}, - "Title": {Value: title, Writable: true, Emit: prop.EmitTrue}, - "Id": {Value: "1", Writable: false, Emit: prop.EmitTrue}, - "Category": {Value: "ApplicationStatus", Writable: false, Emit: prop.EmitTrue}, - "IconName": {Value: "", Writable: false, Emit: prop.EmitTrue}, - "IconPixmap": {Value: buildPixmaps(icon), Writable: true, Emit: prop.EmitTrue}, - "IconThemePath": {Value: "", Writable: false, Emit: prop.EmitTrue}, - "ItemIsMenu": {Value: true, Writable: false, Emit: prop.EmitTrue}, - "Menu": {Value: dbus.ObjectPath(menuPath), Writable: true, Emit: prop.EmitTrue}, - "ToolTip": {Value: tooltip{V2: tooltipTitle, V1: buildPixmaps(icon)}, Writable: true, Emit: prop.EmitTrue}, - }, - } -} - -// PX is a pixmap with width, height and ARGB32 big-endian data -type PX struct { - W, H int - Pix []byte -} - -// tooltip maps the SNI tooltip struct (name, icons, title, description) -type tooltip = struct { - V0 string // name - V1 []PX // icons - V2 string // title - V3 string // description -} - -// buildPixmaps decodes the source icon and produces multiple high-quality -// resized pixmaps encoded as ARGB32 big-endian. -func buildPixmaps(data []byte) []PX { - if len(data) == 0 { - return nil - } - img, _, err := image.Decode(bytes.NewReader(data)) - if err != nil { - log.Printf("systray: failed to decode icon: %v", err) - return nil - } - - // Common tray sizes across environments (match C++ behavior of providing multiple sizes) - targets := []int{16, 22, 24, 32, 48, 64, 128} - - out := make([]PX, 0, len(targets)) - for _, s := range targets { - pm := resizeTo(img, s, s) - out = append(out, PX{W: s, H: s, Pix: toARGB32BigEndian(pm)}) - } - return out -} - -// resizeTo resizes with CatmullRom for crisp yet smooth icons. -func resizeTo(src image.Image, w, h int) *image.RGBA { - dst := image.NewRGBA(image.Rect(0, 0, w, h)) - draw.CatmullRom.Scale(dst, dst.Bounds(), src, src.Bounds(), draw.Over, nil) - return dst -} - -// toARGB32BigEndian converts *image.RGBA to a byte slice of ARGB32 in big-endian -// order (A, R, G, B per byte) as required by SNI over D-Bus. -// This mirrors the C++ implementation that enforces big-endian layout. :contentReference[oaicite:4]{index=4} -func toARGB32BigEndian(img *image.RGBA) []byte { - w, h := img.Bounds().Dx(), img.Bounds().Dy() - buf := make([]byte, w*h*4) - - i := 0 - for y := 0; y < h; y++ { - off := img.PixOffset(0, y) - row := img.Pix[off : off+w*4] - for x := 0; x < w; x++ { - r := row[x*4+0] - g := row[x*4+1] - b := row[x*4+2] - a := row[x*4+3] - - // 0xAARRGGBB big-endian -> bytes [AA][RR][GG][BB] - buf[i+0] = a - buf[i+1] = r - buf[i+2] = g - buf[i+3] = b - i += 4 - } - } - return buf -} - - -// noMenuPathForEnvironment returns the DBus object path to advertise when there is NO menu. -// - GNOME: "/" -// - Others (including KDE/Plasma): "/NO_DBUSMENU" -// Simple detection via environment variables. -func noMenuPathForEnvironment() dbus.ObjectPath { - xdg := strings.ToLower(os.Getenv("XDG_CURRENT_DESKTOP")) - sess := strings.ToLower(os.Getenv("DESKTOP_SESSION")) - if strings.Contains(xdg, "gnome") || strings.Contains(sess, "gnome") { - return dbus.ObjectPath("/") - } - return dbus.ObjectPath("/NO_DBUSMENU") -} - -// setMenuPropTo updates the StatusNotifierItem "Menu" property to the given path. -func setMenuPropTo(p dbus.ObjectPath) { - instance.lock.Lock() - props := instance.props - instance.lock.Unlock() - if props == nil { - return - } - if dbusErr := props.Set("org.kde.StatusNotifierItem", "Menu", dbus.MakeVariant(p)); dbusErr != nil { - log.Printf("systray error: failed to set Menu prop: %s\n", dbusErr) - } -} From 43e6442c3f4f02eb3cb5b9cbe6928fdf6c8d7fb4 Mon Sep 17 00:00:00 2001 From: Elie Gambache Date: Thu, 9 Apr 2026 18:37:52 +0300 Subject: [PATCH 09/12] Refactor Linux outside-click watcher: replace JNA with JNI, add dynamic X11 support, and update reachability metadata. --- demo/build.gradle.kts | 2 +- .../kdroid/composetray/demo/TrayAppDemo.kt | 28 +++++- .../lib/linux/LinuxNativeBridge.kt | 14 +++ .../lib/linux/LinuxOutsideClickWatcher.kt | 95 ++++-------------- .../reachability-metadata.json | 10 ++ src/native/linux/build.sh | 2 +- src/native/linux/jni_bridge.c | 97 +++++++++++++++++++ 7 files changed, 169 insertions(+), 79 deletions(-) diff --git a/demo/build.gradle.kts b/demo/build.gradle.kts index 392a5ff0..e6bc5476 100644 --- a/demo/build.gradle.kts +++ b/demo/build.gradle.kts @@ -29,7 +29,7 @@ kotlin { } nucleus.application { - mainClass = "com.kdroid.composetray.demo.DynamicTrayMenuKt" + mainClass = "com.kdroid.composetray.demo.TrayAppDemoKt" buildTypes { release { diff --git a/demo/src/jvmMain/kotlin/com/kdroid/composetray/demo/TrayAppDemo.kt b/demo/src/jvmMain/kotlin/com/kdroid/composetray/demo/TrayAppDemo.kt index 0e6b587a..f4fc4d21 100644 --- a/demo/src/jvmMain/kotlin/com/kdroid/composetray/demo/TrayAppDemo.kt +++ b/demo/src/jvmMain/kotlin/com/kdroid/composetray/demo/TrayAppDemo.kt @@ -26,11 +26,20 @@ import com.kdroid.composetray.utils.allowComposeNativeTrayLogging import composenativetray.demo.generated.resources.Res import composenativetray.demo.generated.resources.icon import io.github.kdroidfilter.nucleus.darkmodedetector.isSystemInDarkMode +import io.github.kdroidfilter.nucleus.graalvm.GraalVmInitializer import kotlinx.coroutines.launch import org.jetbrains.compose.resources.painterResource +import java.io.File @OptIn(ExperimentalTrayAppApi::class) fun main() { + GraalVmInitializer.initialize() + if (System.getProperty("skiko.renderApi") == null) { + val os = System.getProperty("os.name")?.lowercase() ?: "" + if (os.contains("linux") && isNvidiaGpuPresent()) { + System.setProperty("skiko.renderApi", "SOFTWARE") + } + } allowComposeNativeTrayLogging = true application { var isWindowVisible by remember { mutableStateOf(true) } @@ -265,4 +274,21 @@ fun main() { } } } -} \ No newline at end of file +} + +private fun isNvidiaGpuPresent(): Boolean { + // Check if NVIDIA driver is loaded by looking for the driver version file + val nvidiaDriverFile = File("/proc/driver/nvidia/version") + if (nvidiaDriverFile.exists()) return true + + // Fallback: try running nvidia-smi + return try { + val process = ProcessBuilder("nvidia-smi", "-L") + .redirectErrorStream(true) + .start() + val exitCode = process.waitFor() + exitCode == 0 + } catch (_: Exception) { + false + } +} diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/LinuxNativeBridge.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/LinuxNativeBridge.kt index 1273e486..8f7b0f53 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/LinuxNativeBridge.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/LinuxNativeBridge.kt @@ -66,4 +66,18 @@ internal object LinuxNativeBridge { @JvmStatic external fun nativeItemCheck(handle: Long, id: Int) @JvmStatic external fun nativeItemUncheck(handle: Long, id: Int) @JvmStatic external fun nativeItemSetIcon(handle: Long, id: Int, iconBytes: ByteArray) + + // -- X11 outside-click watcher ----------------------------------------------- + + /** Open X11 display. Returns handle, or 0 if X11 is unavailable. */ + @JvmStatic external fun nativeX11OpenDisplay(): Long + + /** Get default root window for the display. */ + @JvmStatic external fun nativeX11DefaultRootWindow(displayHandle: Long): Long + + /** Query pointer. Writes [rootX, rootY, mask] into outData. Returns 1 on success. */ + @JvmStatic external fun nativeX11QueryPointer(displayHandle: Long, rootWindow: Long, outData: IntArray): Int + + /** Close X11 display. */ + @JvmStatic external fun nativeX11CloseDisplay(displayHandle: Long) } diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/LinuxOutsideClickWatcher.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/LinuxOutsideClickWatcher.kt index 4741647c..7b77fa41 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/LinuxOutsideClickWatcher.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/LinuxOutsideClickWatcher.kt @@ -1,12 +1,6 @@ package com.kdroid.composetray.lib.linux import com.kdroid.composetray.utils.isPointWithinLinuxStatusItem -import com.sun.jna.Library -import com.sun.jna.Native -import com.sun.jna.NativeLong -import com.sun.jna.Pointer -import com.sun.jna.ptr.IntByReference -import com.sun.jna.ptr.NativeLongByReference import io.github.kdroidfilter.platformtools.OperatingSystem import io.github.kdroidfilter.platformtools.getOperatingSystem import java.awt.Window @@ -14,35 +8,12 @@ import java.util.concurrent.Executors import java.util.concurrent.ScheduledExecutorService import java.util.concurrent.TimeUnit -/** - * Minimal X11 binding for what we need. - */ -internal interface X11 : Library { - companion object { - val INSTANCE: X11 = Native.load("X11", X11::class.java) - } - - fun XOpenDisplay(displayName: String?): Pointer? - fun XDefaultRootWindow(display: Pointer?): NativeLong - fun XQueryPointer( - display: Pointer?, - w: NativeLong, - root_return: NativeLongByReference, - child_return: NativeLongByReference, - root_x_return: IntByReference, - root_y_return: IntByReference, - win_x_return: IntByReference, - win_y_return: IntByReference, - mask_return: IntByReference - ): Int - - fun XCloseDisplay(display: Pointer?): Int -} - /** * LinuxOutsideClickWatcher: X11/XWayland implementation that detects a left-click anywhere, * and if it is outside the supplied window (and not on the tray icon area), invokes a callback. * + * Uses JNI via LinuxNativeBridge for X11 calls (no JNA dependency). + * * Notes: * - Requires X11/XWayland (DISPLAY must be set). Will no-op on Wayland-only sessions without XWayland. * - Polls with XQueryPointer at ~60 Hz, reading Button1Mask for "left pressed". @@ -55,25 +26,20 @@ class LinuxOutsideClickWatcher( private var scheduler: ScheduledExecutorService? = null private var prevLeft = false - // X11 state - private var display: Pointer? = null - private var rootWindow: NativeLong = NativeLong(0) + // X11 state (native handles) + private var displayHandle: Long = 0L + private var rootWindow: Long = 0L fun start() { if (getOperatingSystem() != OperatingSystem.LINUX) return if (scheduler != null) return try { - val x11 = X11.INSTANCE - display = x11.XOpenDisplay(null) - if (display == null) { - // No X11 available (e.g., Wayland-only session) -> do nothing. - return - } - rootWindow = x11.XDefaultRootWindow(display) + displayHandle = LinuxNativeBridge.nativeX11OpenDisplay() + if (displayHandle == 0L) return + rootWindow = LinuxNativeBridge.nativeX11DefaultRootWindow(displayHandle) } catch (_: Throwable) { - // Failed to connect to X11 -> do nothing. - display = null + displayHandle = 0L return } @@ -85,42 +51,21 @@ class LinuxOutsideClickWatcher( } private fun pollOnce() { - val dpy = display ?: return + if (displayHandle == 0L) return try { - val x11 = X11.INSTANCE - - val rootRet = NativeLongByReference() - val childRet = NativeLongByReference() - val rootX = IntByReference() - val rootY = IntByReference() - val winX = IntByReference() - val winY = IntByReference() - val mask = IntByReference() - - // Bool return (0 = False, non-zero = True) - val ok = x11.XQueryPointer( - dpy, - rootWindow, - rootRet, - childRet, - rootX, - rootY, - winX, - winY, - mask - ) != 0 - + val outData = IntArray(3) // [rootX, rootY, mask] + val ok = LinuxNativeBridge.nativeX11QueryPointer(displayHandle, rootWindow, outData) != 0 if (!ok) return - val left = (mask.value and BUTTON1_MASK) != 0 + val px = outData[0] + val py = outData[1] + val mask = outData[2] + val left = (mask and BUTTON1_MASK) != 0 // Rising edge: only act once when the button goes down if (left && left != prevLeft) { val win = windowSupplier.invoke() if (win != null && win.isShowing) { - val px = rootX.value - val py = rootY.value - val winLoc = try { win.locationOnScreen } catch (_: Throwable) { null } if (winLoc != null) { val wx = winLoc.x @@ -150,14 +95,12 @@ class LinuxOutsideClickWatcher( try { scheduler?.shutdownNow() } catch (_: Throwable) {} scheduler = null - // Close X11 display after stopping the poller try { - display?.let { X11.INSTANCE.XCloseDisplay(it) } + if (displayHandle != 0L) LinuxNativeBridge.nativeX11CloseDisplay(displayHandle) } catch (_: Throwable) { - // ignore } finally { - display = null - rootWindow = NativeLong(0) + displayHandle = 0L + rootWindow = 0L } } diff --git a/src/jvmMain/resources/META-INF/native-image/io.github.kdroidfilter/composenativetray/reachability-metadata.json b/src/jvmMain/resources/META-INF/native-image/io.github.kdroidfilter/composenativetray/reachability-metadata.json index 4672dbca..8a7cfdf6 100644 --- a/src/jvmMain/resources/META-INF/native-image/io.github.kdroidfilter/composenativetray/reachability-metadata.json +++ b/src/jvmMain/resources/META-INF/native-image/io.github.kdroidfilter/composenativetray/reachability-metadata.json @@ -28,6 +28,16 @@ } ] }, + { + "type": "sun.awt.X11GraphicsConfig", + "jniAccessible": true, + "methods": [ + { + "name": "createDCM32", + "parameterTypes": ["int", "int", "int", "int", "boolean"] + } + ] + }, { "type": "java.lang.Runnable", "jniAccessible": true, diff --git a/src/native/linux/build.sh b/src/native/linux/build.sh index 4fea7383..29195a62 100755 --- a/src/native/linux/build.sh +++ b/src/native/linux/build.sh @@ -64,7 +64,7 @@ gcc -shared -o "$OUTPUT_DIR/linux-x86-64/libLinuxTray.so" \ "$SCRIPT_DIR/sni.o" \ "$SCRIPT_DIR/jni_bridge.o" \ $SDBUS_LIBS \ - -lpthread -lm + -lpthread -lm -ldl # Strip debug symbols for smaller binary strip --strip-unneeded "$OUTPUT_DIR/linux-x86-64/libLinuxTray.so" diff --git a/src/native/linux/jni_bridge.c b/src/native/linux/jni_bridge.c index 0d0dd9f9..57159e6c 100644 --- a/src/native/linux/jni_bridge.c +++ b/src/native/linux/jni_bridge.c @@ -15,6 +15,7 @@ #include #include #include +#include #include "sni.h" @@ -489,3 +490,99 @@ Java_com_kdroid_composetray_lib_linux_LinuxNativeBridge_nativeItemSetIcon( sni_tray_item_set_icon(tray, (uint32_t)id, (const uint8_t *)buf, (size_t)len); (*env)->ReleaseByteArrayElements(env, iconBytes, buf, JNI_ABORT); } + +/* ========================================================================== */ +/* X11 outside-click watcher (dynamically loaded to avoid hard dependency) */ +/* ========================================================================== */ + +/* X11 types */ +typedef void *X11Display; +typedef unsigned long X11Window; +typedef int X11Bool; + +/* X11 function pointers (loaded via dlopen/dlsym) */ +typedef X11Display (*fn_XOpenDisplay)(const char *); +typedef X11Window (*fn_XDefaultRootWindow)(X11Display); +typedef X11Bool (*fn_XQueryPointer)(X11Display, X11Window, + X11Window *, X11Window *, + int *, int *, int *, int *, + unsigned int *); +typedef int (*fn_XCloseDisplay)(X11Display); + +static void *g_x11_lib = NULL; +static fn_XOpenDisplay g_XOpenDisplay = NULL; +static fn_XDefaultRootWindow g_XDefaultRootWindow = NULL; +static fn_XQueryPointer g_XQueryPointer = NULL; +static fn_XCloseDisplay g_XCloseDisplay = NULL; + +static int ensure_x11(void) { + if (g_x11_lib) return 1; + g_x11_lib = dlopen("libX11.so.6", RTLD_LAZY); + if (!g_x11_lib) g_x11_lib = dlopen("libX11.so", RTLD_LAZY); + if (!g_x11_lib) return 0; + g_XOpenDisplay = (fn_XOpenDisplay)dlsym(g_x11_lib, "XOpenDisplay"); + g_XDefaultRootWindow = (fn_XDefaultRootWindow)dlsym(g_x11_lib, "XDefaultRootWindow"); + g_XQueryPointer = (fn_XQueryPointer)dlsym(g_x11_lib, "XQueryPointer"); + g_XCloseDisplay = (fn_XCloseDisplay)dlsym(g_x11_lib, "XCloseDisplay"); + if (!g_XOpenDisplay || !g_XDefaultRootWindow || !g_XQueryPointer || !g_XCloseDisplay) { + dlclose(g_x11_lib); + g_x11_lib = NULL; + return 0; + } + return 1; +} + +JNIEXPORT jlong JNICALL +Java_com_kdroid_composetray_lib_linux_LinuxNativeBridge_nativeX11OpenDisplay( + JNIEnv *env, jclass clazz) +{ + (void)env; (void)clazz; + if (!ensure_x11()) return 0; + X11Display dpy = g_XOpenDisplay(NULL); + return (jlong)(uintptr_t)dpy; +} + +JNIEXPORT jlong JNICALL +Java_com_kdroid_composetray_lib_linux_LinuxNativeBridge_nativeX11DefaultRootWindow( + JNIEnv *env, jclass clazz, jlong displayHandle) +{ + (void)env; (void)clazz; + if (!g_XDefaultRootWindow) return 0; + X11Display dpy = (X11Display)(uintptr_t)displayHandle; + if (!dpy) return 0; + return (jlong)g_XDefaultRootWindow(dpy); +} + +JNIEXPORT jint JNICALL +Java_com_kdroid_composetray_lib_linux_LinuxNativeBridge_nativeX11QueryPointer( + JNIEnv *env, jclass clazz, jlong displayHandle, jlong rootWindow, jintArray outData) +{ + (void)clazz; + if (!g_XQueryPointer) return 0; + X11Display dpy = (X11Display)(uintptr_t)displayHandle; + if (!dpy || !outData) return 0; + + X11Window root_ret, child_ret; + int root_x, root_y, win_x, win_y; + unsigned int mask; + + X11Bool ok = g_XQueryPointer(dpy, (X11Window)(unsigned long)rootWindow, + &root_ret, &child_ret, + &root_x, &root_y, &win_x, &win_y, &mask); + + /* outData: [rootX, rootY, mask] */ + jint buf[3] = {(jint)root_x, (jint)root_y, (jint)mask}; + (*env)->SetIntArrayRegion(env, outData, 0, 3, buf); + + return (jint)(ok != 0 ? 1 : 0); +} + +JNIEXPORT void JNICALL +Java_com_kdroid_composetray_lib_linux_LinuxNativeBridge_nativeX11CloseDisplay( + JNIEnv *env, jclass clazz, jlong displayHandle) +{ + (void)env; (void)clazz; + if (!g_XCloseDisplay) return; + X11Display dpy = (X11Display)(uintptr_t)displayHandle; + if (dpy) g_XCloseDisplay(dpy); +} From e5a5aea02716ffd1e3080bf0659c3d6ad453c2ad Mon Sep 17 00:00:00 2001 From: "Elie G." Date: Thu, 9 Apr 2026 20:22:44 +0300 Subject: [PATCH 10/12] refactor(windows): migrate from JNA to JNI for native tray and mouse hook Replace JNA-based Windows implementation with pure JNI, following the same patterns as Mac and Linux bridges. Add WindowsNativeBridge.kt with external JNI declarations and jni_bridge.c wrapping the tray.h C API. Rewrite WindowsTrayManager and WindowsOutsideClickWatcher to use JNI calls instead of JNA structures and callbacks. Remove all JNA dependencies from build configuration. Rename DLL output from tray.dll to WinTray.dll. Update GraalVM reachability metadata and CI workflow for JDK requirement. Co-Authored-By: Claude Opus 4.6 (1M context) --- .github/workflows/build-natives.yaml | 14 +- build.gradle.kts | 3 +- gradle/libs.versions.toml | 3 - .../lib/windows/StdCallCallback.kt | 7 - .../lib/windows/WindowsNativeBridge.kt | 57 ++ .../lib/windows/WindowsNativeTray.kt | 34 -- .../lib/windows/WindowsNativeTrayLibrary.kt | 29 - .../lib/windows/WindowsNativeTrayMenuItem.kt | 35 -- .../lib/windows/WindowsOutsideClickWatcher.kt | 138 ++--- .../lib/windows/WindowsTrayManager.kt | 255 ++++----- .../composetray/utils/NativeLibraryLoader.kt | 27 - .../kdroid/composetray/utils/TrayPosition.kt | 4 +- .../reachability-metadata.json | 4 + src/native/windows/CMakeLists.txt | 38 +- src/native/windows/build.bat | 10 +- src/native/windows/jni_bridge.c | 501 ++++++++++++++++++ 16 files changed, 760 insertions(+), 399 deletions(-) delete mode 100644 src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/StdCallCallback.kt create mode 100644 src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsNativeBridge.kt delete mode 100644 src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsNativeTray.kt delete mode 100644 src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsNativeTrayLibrary.kt delete mode 100644 src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsNativeTrayMenuItem.kt create mode 100644 src/native/windows/jni_bridge.c diff --git a/.github/workflows/build-natives.yaml b/.github/workflows/build-natives.yaml index dda5789e..116870f2 100644 --- a/.github/workflows/build-natives.yaml +++ b/.github/workflows/build-natives.yaml @@ -83,6 +83,12 @@ jobs: - name: Checkout code uses: actions/checkout@v4 + - name: Set up JDK + uses: actions/setup-java@v4 + with: + java-version: '17' + distribution: 'temurin' + - name: Build Windows native libraries working-directory: src/native/windows run: cmd /c build.bat @@ -92,8 +98,8 @@ jobs: - name: Verify Windows natives shell: bash run: | - test -f build/nativeLibs/win32-x86-64/tray.dll - test -f build/nativeLibs/win32-arm64/tray.dll + test -f build/nativeLibs/win32-x86-64/WinTray.dll + test -f build/nativeLibs/win32-arm64/WinTray.dll ls -la build/nativeLibs/win32-x86-64/ ls -la build/nativeLibs/win32-arm64/ @@ -101,12 +107,12 @@ jobs: uses: actions/upload-artifact@v4 with: name: native-win32-x86-64 - path: build/nativeLibs/win32-x86-64/tray.dll + path: build/nativeLibs/win32-x86-64/WinTray.dll retention-days: 1 - name: Upload Windows ARM64 library uses: actions/upload-artifact@v4 with: name: native-win32-arm64 - path: build/nativeLibs/win32-arm64/tray.dll + path: build/nativeLibs/win32-arm64/WinTray.dll retention-days: 1 diff --git a/build.gradle.kts b/build.gradle.kts index 9e740871..61fc0783 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -39,11 +39,10 @@ kotlin { implementation(compose.foundation) implementation(compose.ui) implementation(compose.components.resources) - implementation(libs.jna) - implementation(libs.jna.platform) implementation(libs.kotlinx.coroutines.core) implementation(libs.kotlinx.coroutines.swing) implementation(libs.platformtools.core) + implementation(libs.nucleus.core.runtime) implementation(libs.nucleus.darkmode.detector) } } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 83d3b4d7..a252e34b 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -3,7 +3,6 @@ kermit = "2.0.8" kotlin = "2.3.10" kotlinx-coroutines = "1.10.2" compose = "1.10.0" -jna = "5.18.1" platformtools = "0.7.5" detekt = "1.23.7" ktlint = "12.1.2" @@ -13,8 +12,6 @@ nucleus = "1.9.0" kermit = { module = "co.touchlab:kermit", version.ref = "kermit" } kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlinx-coroutines" } kotlinx-coroutines-swing = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-swing", version.ref = "kotlinx-coroutines" } -jna = { module = "net.java.dev.jna:jna-jpms", version.ref = "jna" } -jna-platform = { module = "net.java.dev.jna:jna-platform-jpms", version.ref = "jna" } platformtools-core = { module = "io.github.kdroidfilter:platformtools.core", version.ref = "platformtools" } nucleus-core-runtime = { module = "io.github.kdroidfilter:nucleus.core-runtime", version.ref = "nucleus" } nucleus-darkmode-detector = { module = "io.github.kdroidfilter:nucleus.darkmode-detector", version.ref = "nucleus" } diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/StdCallCallback.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/StdCallCallback.kt deleted file mode 100644 index 39572830..00000000 --- a/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/StdCallCallback.kt +++ /dev/null @@ -1,7 +0,0 @@ -package com.kdroid.composetray.lib.windows - -import com.sun.jna.win32.StdCallLibrary - -internal fun interface StdCallCallback : StdCallLibrary.StdCallCallback { - fun invoke(item: WindowsNativeTrayMenuItem) -} diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsNativeBridge.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsNativeBridge.kt new file mode 100644 index 00000000..1b15190e --- /dev/null +++ b/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsNativeBridge.kt @@ -0,0 +1,57 @@ +package com.kdroid.composetray.lib.windows + +import com.kdroid.composetray.utils.NativeLibraryLoader + +/** + * JNI bridge to the native Windows tray library (WinTray.dll). + * Replaces the previous JNA-based approach. + * Follows the same patterns as MacNativeBridge and LinuxNativeBridge. + */ +internal object WindowsNativeBridge { + + init { + NativeLibraryLoader.load("WinTray", WindowsNativeBridge::class.java) + } + + // -- Tray lifecycle -- + + @JvmStatic external fun nativeCreateTray(iconPath: String, tooltip: String): Long + @JvmStatic external fun nativeFreeTray(handle: Long) + @JvmStatic external fun nativeSetTrayIcon(handle: Long, iconPath: String) + @JvmStatic external fun nativeSetTrayTooltip(handle: Long, tooltip: String) + @JvmStatic external fun nativeSetTrayCallback(handle: Long, callback: Runnable?) + @JvmStatic external fun nativeSetTrayMenu(trayHandle: Long, menuHandle: Long) + @JvmStatic external fun nativeClearTrayMenu(trayHandle: Long) + @JvmStatic external fun nativeInitTray(handle: Long): Int + @JvmStatic external fun nativeLoopTray(blocking: Int): Int + @JvmStatic external fun nativeUpdateTray(handle: Long) + @JvmStatic external fun nativeExitTray() + + // -- Menu items -- + + @JvmStatic external fun nativeCreateMenuItems(count: Int): Long + @JvmStatic external fun nativeSetMenuItem( + menuHandle: Long, + index: Int, + text: String, + iconPath: String?, + disabled: Int, + checked: Int, + ) + @JvmStatic external fun nativeSetMenuItemCallback(menuHandle: Long, index: Int, callback: Runnable?) + @JvmStatic external fun nativeSetMenuItemSubmenu(menuHandle: Long, index: Int, submenuHandle: Long) + @JvmStatic external fun nativeFreeMenuItems(menuHandle: Long, count: Int) + + // -- Position -- + + /** Writes [x, y] into outXY. Returns non-zero if precise. */ + @JvmStatic external fun nativeGetNotificationIconsPosition(outXY: IntArray): Int + @JvmStatic external fun nativeGetNotificationIconsRegion(): String? + + // -- Mouse hook (for outside-click detection) -- + + @JvmStatic external fun nativeInstallMouseHook(callback: Runnable): Long + @JvmStatic external fun nativeRunMouseHookLoop(hookId: Long) + @JvmStatic external fun nativeStopMouseHook(hookId: Long) + @JvmStatic external fun nativeGetLastMouseHookClick(outXY: IntArray) +} diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsNativeTray.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsNativeTray.kt deleted file mode 100644 index 244c920e..00000000 --- a/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsNativeTray.kt +++ /dev/null @@ -1,34 +0,0 @@ -package com.kdroid.composetray.lib.windows - -import com.sun.jna.Pointer -import com.sun.jna.Structure -import com.sun.jna.win32.StdCallLibrary - -@Structure.FieldOrder("icon_filepath", "tooltip", "cb", "menu") -internal class WindowsNativeTray : Structure() { - companion object { - init { - // Ensure UTF-8 encoding for JNA string marshaling before any structure write - val key = "jna.encoding" - if (System.getProperty(key).isNullOrBlank()) { - System.setProperty(key, "UTF-8") - } - } - } - - @JvmField - var icon_filepath: String? = null - - @JvmField - var tooltip: String? = null - - @JvmField - var cb: TrayCallback? = null - - @JvmField - var menu: Pointer? = null - - fun interface TrayCallback : StdCallLibrary.StdCallCallback { - fun invoke(tray: WindowsNativeTray) - } -} diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsNativeTrayLibrary.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsNativeTrayLibrary.kt deleted file mode 100644 index 42bfb939..00000000 --- a/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsNativeTrayLibrary.kt +++ /dev/null @@ -1,29 +0,0 @@ -package com.kdroid.composetray.lib.windows - -import com.kdroid.composetray.utils.NativeLibraryLoader -import com.sun.jna.Pointer -import com.sun.jna.ptr.IntByReference -import com.sun.jna.win32.StdCallLibrary - -// Use JNA direct mapping instead of interface mapping. -// This object registers the native library and exposes external (native) methods. -internal object WindowsNativeTrayLibrary : StdCallLibrary { - init { - // Ensure JNA marshals Java/Kotlin strings as UTF-8. This must be set - // before the first interaction with JNA/native code, otherwise JNA will - // default to the system ANSI code page on Windows and corrupt Unicode. - val key = "jna.encoding" - if (System.getProperty(key).isNullOrBlank()) { - System.setProperty(key, "UTF-8") - } - NativeLibraryLoader.extractAndRegister("tray", WindowsNativeTrayLibrary::class.java) - } - - @JvmStatic external fun tray_get_instance(): Pointer? - @JvmStatic external fun tray_init(tray: WindowsNativeTray): Int - @JvmStatic external fun tray_loop(blocking: Int): Int - @JvmStatic external fun tray_update(tray: WindowsNativeTray) - @JvmStatic external fun tray_exit() - @JvmStatic external fun tray_get_notification_icons_position(x: IntByReference, y: IntByReference) : Int - @JvmStatic external fun tray_get_notification_icons_region(): String? -} diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsNativeTrayMenuItem.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsNativeTrayMenuItem.kt deleted file mode 100644 index 18d1790f..00000000 --- a/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsNativeTrayMenuItem.kt +++ /dev/null @@ -1,35 +0,0 @@ -package com.kdroid.composetray.lib.windows - -import com.sun.jna.Pointer -import com.sun.jna.Structure - -@Structure.FieldOrder("text", "icon_path", "disabled", "checked", "cb", "submenu") -internal open class WindowsNativeTrayMenuItem : Structure() { - companion object { - init { - // Ensure UTF-8 encoding for JNA string marshaling as early as possible - val key = "jna.encoding" - if (System.getProperty(key).isNullOrBlank()) { - System.setProperty(key, "UTF-8") - } - } - } - - @JvmField - var text: String? = null - - @JvmField - var icon_path: String? = null - - @JvmField - var disabled: Int = 0 - - @JvmField - var checked: Int = 0 - - @JvmField - var cb: StdCallCallback? = null - - @JvmField - var submenu: Pointer? = null -} \ No newline at end of file diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsOutsideClickWatcher.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsOutsideClickWatcher.kt index 03e4fa50..d2dd27c1 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsOutsideClickWatcher.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsOutsideClickWatcher.kt @@ -1,17 +1,12 @@ package com.kdroid.composetray.lib.windows -import com.sun.jna.Pointer -import com.sun.jna.platform.win32.Kernel32 -import com.sun.jna.platform.win32.WinDef -import com.sun.jna.platform.win32.WinUser -import com.sun.jna.platform.win32.User32 import io.github.kdroidfilter.platformtools.OperatingSystem import io.github.kdroidfilter.platformtools.getOperatingSystem import java.awt.Window import java.util.concurrent.atomic.AtomicBoolean /** - * WindowsOutsideClickWatcher using a low-level mouse hook (WH_MOUSE_LL). + * WindowsOutsideClickWatcher using a low-level mouse hook (WH_MOUSE_LL) via JNI. * * Behavior: * - Listens for global left-button *down* events. @@ -22,22 +17,13 @@ import java.util.concurrent.atomic.AtomicBoolean class WindowsOutsideClickWatcher( private val windowSupplier: () -> Window?, private val onOutsideClick: () -> Unit, - private val ignorePointPredicate: ((x: Int, y: Int) -> Boolean)? = null + private val ignorePointPredicate: ((x: Int, y: Int) -> Boolean)? = null, ) : AutoCloseable { @Volatile private var hookThread: Thread? = null - @Volatile private var hookHandle: WinUser.HHOOK? = null - @Volatile private var hookThreadId: Int = 0 - @Volatile private var mouseProc: WinUser.LowLevelMouseProc? = null + @Volatile private var hookId: Long = 0L private val stopping = AtomicBoolean(false) - private companion object { - const val WH_MOUSE_LL = 14 - const val WM_LBUTTONDOWN = 0x0201 - const val WM_NCLBUTTONDOWN = 0x00A1 - const val WM_QUIT = 0x0012 - } - /** Start the global low-level mouse hook on a dedicated daemon thread. */ fun start() { if (getOperatingSystem() != OperatingSystem.WINDOWS) return @@ -46,76 +32,47 @@ class WindowsOutsideClickWatcher( stopping.set(false) hookThread = Thread({ - hookThreadId = Kernel32.INSTANCE.GetCurrentThreadId() - - // Strong reference kept on the field to avoid GC of the callback. - mouseProc = WinUser.LowLevelMouseProc { nCode, wParam, lParam -> + val callback = Runnable { try { - if (nCode >= 0) { - val msg = wParam.toInt() // For WH_MOUSE_LL this is the WM_* code. - if (msg == WM_LBUTTONDOWN || msg == WM_NCLBUTTONDOWN) { - // lParam is already the populated MSLLHOOKSTRUCT. - val px = lParam.pt.x - val py = lParam.pt.y - - val win = windowSupplier() - if (win != null && win.isShowing) { - val winLoc = try { win.locationOnScreen } catch (_: Throwable) { null } - if (winLoc != null) { - // Get the graphics configuration to determine the DPI scale - val scale = try { - win.graphicsConfiguration?.defaultTransform?.scaleX ?: 1.0 - } catch (_: Throwable) { 1.0 } - - // Convert window bounds from logical to physical pixels - val wx = (winLoc.x * scale).toInt() - val wy = (winLoc.y * scale).toInt() - val ww = (win.width * scale).toInt() - val wh = (win.height * scale).toInt() - - val insideWindow = px in wx until (wx + ww) && py in wy until (wy + wh) - val ignored = ignorePointPredicate?.invoke(px, py) == true - - if (!insideWindow && !ignored) { - // Let caller decide EDT marshaling. - onOutsideClick() - } - } + val xy = IntArray(2) + WindowsNativeBridge.nativeGetLastMouseHookClick(xy) + val px = xy[0] + val py = xy[1] + + val win = windowSupplier() + if (win != null && win.isShowing) { + val winLoc = try { win.locationOnScreen } catch (_: Throwable) { null } + if (winLoc != null) { + // Get the graphics configuration to determine the DPI scale + val scale = try { + win.graphicsConfiguration?.defaultTransform?.scaleX ?: 1.0 + } catch (_: Throwable) { 1.0 } + + // Convert window bounds from logical to physical pixels + val wx = (winLoc.x * scale).toInt() + val wy = (winLoc.y * scale).toInt() + val ww = (win.width * scale).toInt() + val wh = (win.height * scale).toInt() + + val insideWindow = px in wx until (wx + ww) && py in wy until (wy + wh) + val ignored = ignorePointPredicate?.invoke(px, py) == true + + if (!insideWindow && !ignored) { + // Let caller decide EDT marshaling. + onOutsideClick() } } } } catch (_: Throwable) { - // Never crash the hook; always fall through to CallNextHookEx. + // Never crash the hook callback } - - // Pass original WPARAM and a *pointer* to the struct as LPARAM. - val lParamNative = WinDef.LPARAM(Pointer.nativeValue(lParam.pointer)) - User32.INSTANCE.CallNextHookEx(hookHandle, nCode, wParam, lParamNative) } - // Install the hook (global, threadId = 0). - val hMod = Kernel32.INSTANCE.GetModuleHandle(null) - hookHandle = User32.INSTANCE.SetWindowsHookEx(WH_MOUSE_LL, mouseProc, hMod, 0) + hookId = WindowsNativeBridge.nativeInstallMouseHook(callback) + if (hookId == 0L) return@Thread - if (hookHandle == null) { - mouseProc = null - return@Thread - } - - // Minimal message loop to keep the hook thread alive. - val msg = WinUser.MSG() - while (!stopping.get()) { - val r = User32.INSTANCE.GetMessage(msg, null, 0, 0) - if (r == 0 || r == -1) break // WM_QUIT or error - } - - // Cleanup before thread exits. - try { - hookHandle?.let { User32.INSTANCE.UnhookWindowsHookEx(it) } - } finally { - hookHandle = null - mouseProc = null - } + // Block on message loop until stopped + WindowsNativeBridge.nativeRunMouseHookLoop(hookId) }, "WindowsOutsideClickWatcher-LL").apply { isDaemon = true start() @@ -131,30 +88,15 @@ class WindowsOutsideClickWatcher( synchronized(this) { stopping.set(true) - // Unhook immediately; also helps release if thread is blocked in GetMessage(). - try { - hookHandle?.let { User32.INSTANCE.UnhookWindowsHookEx(it) } - } catch (_: Throwable) { - } finally { - hookHandle = null - } - - // Break GetMessage() with WM_QUIT. - if (hookThreadId != 0) { + val id = hookId + if (id != 0L) { try { - User32.INSTANCE.PostThreadMessage( - hookThreadId, - WM_QUIT, - WinDef.WPARAM(0), - WinDef.LPARAM(0) - ) - } catch (_: Throwable) { - } + WindowsNativeBridge.nativeStopMouseHook(id) + } catch (_: Throwable) { } + hookId = 0L } hookThread = null - hookThreadId = 0 - mouseProc = null } } } diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsTrayManager.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsTrayManager.kt index 108361bf..f76f09bf 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsTrayManager.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsTrayManager.kt @@ -1,12 +1,15 @@ package com.kdroid.composetray.lib.windows -import com.kdroid.composetray.utils.debugln import com.kdroid.composetray.utils.TrayClickTracker -import com.sun.jna.ptr.IntByReference -import kotlinx.coroutines.* +import com.kdroid.composetray.utils.convertPositionToCorner +import com.kdroid.composetray.utils.debugln +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.cancel +import kotlinx.coroutines.launch import java.util.concurrent.CountDownLatch import java.util.concurrent.atomic.AtomicBoolean -import java.util.concurrent.atomic.AtomicReference import java.util.concurrent.locks.ReentrantLock import kotlin.concurrent.withLock @@ -14,20 +17,16 @@ internal class WindowsTrayManager( private val instanceId: String, private var iconPath: String, private var tooltip: String = "", - private var onLeftClick: (() -> Unit)? = null + private var onLeftClick: (() -> Unit)? = null, ) { - private var tray: AtomicReference = AtomicReference(null) + private var trayHandle: Long = 0L private val running = AtomicBoolean(false) private val initialized = AtomicBoolean(false) private val updateLock = ReentrantLock() private val initLatch = CountDownLatch(1) - // Maintain a reference to all callbacks to avoid GC - private val callbackReferences: MutableList = mutableListOf() - private val nativeMenuItemsReferences: MutableList = mutableListOf() - - // Keep a reference to the tray callback - private var trayCallback: WindowsNativeTray.TrayCallback? = null + // Track allocated menu handles for cleanup + private val menuHandles: MutableList> = mutableListOf() // Thread for running the tray (similar to macOS) private var trayThread: Thread? = null @@ -51,18 +50,18 @@ internal class WindowsTrayManager( val iconPath: String, val tooltip: String, val onLeftClick: (() -> Unit)?, - val menuItems: List + val menuItems: List, ) // Top level MenuItem class data class MenuItem( val text: String, - val iconPath: String? = null, // Added icon path support + val iconPath: String? = null, val isEnabled: Boolean = true, val isCheckable: Boolean = false, val isChecked: Boolean = false, val onClick: (() -> Unit)? = null, - val subMenuItems: List = emptyList() + val subMenuItems: List = emptyList(), ) fun initialize(menuItems: List) { @@ -85,26 +84,24 @@ internal class WindowsTrayManager( try { log("Tray thread started") - // Create tray structure on this thread - val newTray = WindowsNativeTray().apply { - icon_filepath = iconPath - tooltip = this@WindowsTrayManager.tooltip - } + // Create tray via JNI + val handle = WindowsNativeBridge.nativeCreateTray(iconPath, tooltip) + if (handle == 0L) throw RuntimeException("Failed to create tray") + trayHandle = handle // Set up callbacks and menu on this thread - setupLeftClickCallback(newTray) - setupMenu(newTray, menuItems) + setupLeftClickCallback(handle) + setupMenu(handle, menuItems) // Initialize the tray on this thread - log("Calling tray_init() on tray thread") - val initResult = WindowsNativeTrayLibrary.tray_init(newTray) - log("tray_init() returned: $initResult") + log("Calling nativeInitTray() on tray thread") + val initResult = WindowsNativeBridge.nativeInitTray(handle) + log("nativeInitTray() returned: $initResult") if (initResult != 0) { throw RuntimeException("Failed to initialize tray: $initResult") } - tray.set(newTray) initialized.set(true) // Signal that initialization is complete before entering the loop @@ -112,7 +109,6 @@ internal class WindowsTrayManager( // Run the blocking message loop on this thread runMessageLoop() - } catch (e: Throwable) { log("Error in tray thread: ${e.message}") e.printStackTrace() @@ -140,7 +136,12 @@ internal class WindowsTrayManager( } } - fun update(newIconPath: String, newTooltip: String, newOnLeftClick: (() -> Unit)?, newMenuItems: List) { + fun update( + newIconPath: String, + newTooltip: String, + newOnLeftClick: (() -> Unit)?, + newMenuItems: List, + ) { log("update() called - icon: $newIconPath, tooltip: $newTooltip, menuItems: ${newMenuItems.size}") if (!initialized.get()) { @@ -165,7 +166,7 @@ internal class WindowsTrayManager( var initialPosCaptured = false var initialPosAttempts = 0 var positionErrorCount = 0 - val maxPositionErrors = 3 // Stop trying after 3 errors to avoid spamming logs + val maxPositionErrors = 3 while (running.get()) { try { @@ -181,12 +182,12 @@ internal class WindowsTrayManager( // Check for pending updates processUpdateQueue() - // Process Windows messages with blocking call for responsiveness - val result = WindowsNativeTrayLibrary.tray_loop(0) + // Process Windows messages with non-blocking call + val result = WindowsNativeBridge.nativeLoopTray(0) when (result) { -1 -> { - log("tray_loop returned -1 (error or quit)") + log("nativeLoopTray returned -1 (error or quit)") if (running.get() && initialized.get()) { consecutiveErrors++ if (consecutiveErrors > 5) { @@ -196,11 +197,11 @@ internal class WindowsTrayManager( Thread.sleep(100) // Try to recover - val currentTray = tray.get() - if (currentTray != null) { + val handle = trayHandle + if (handle != 0L) { try { log("Attempting to recover tray...") - WindowsNativeTrayLibrary.tray_update(currentTray) + WindowsNativeBridge.nativeUpdateTray(handle) consecutiveErrors = 0 } catch (e: Exception) { log("Failed to recover: ${e.message}") @@ -216,7 +217,7 @@ internal class WindowsTrayManager( consecutiveErrors = 0 } else -> { - log("tray_loop returned unexpected value: $result") + log("nativeLoopTray returned unexpected value: $result") consecutiveErrors = 0 } } @@ -240,21 +241,20 @@ internal class WindowsTrayManager( */ private fun safeGetTrayPosition(instanceId: String): Boolean { return try { - val xRef = IntByReference() - val yRef = IntByReference() - val precise = WindowsNativeTrayLibrary.tray_get_notification_icons_position(xRef, yRef) != 0 - log("tray_get_notification_icons_position: precise=$precise, rawX=${xRef.value}, rawY=${yRef.value}") + val outXY = IntArray(2) + val precise = WindowsNativeBridge.nativeGetNotificationIconsPosition(outXY) != 0 + log("nativeGetNotificationIconsPosition: precise=$precise, rawX=${outXY[0]}, rawY=${outXY[1]}") if (precise) { // Native coordinates are in physical pixels, but AWT uses logical pixels. // Convert physical to logical by dividing by the DPI scale factor. - val scale = getDpiScale(xRef.value, yRef.value) - val logicalX = (xRef.value / scale).toInt() - val logicalY = (yRef.value / scale).toInt() + val scale = getDpiScale(outXY[0], outXY[1]) + val logicalX = (outXY[0] / scale).toInt() + val logicalY = (outXY[1] / scale).toInt() val screen = java.awt.Toolkit.getDefaultToolkit().screenSize log("DPI scale=$scale, logicalX=$logicalX, logicalY=$logicalY, screenW=${screen.width}, screenH=${screen.height}") - val corner = com.kdroid.composetray.utils.convertPositionToCorner( - logicalX, logicalY, screen.width, screen.height + val corner = convertPositionToCorner( + logicalX, logicalY, screen.width, screen.height, ) log("Detected corner: $corner") TrayClickTracker.setClickPosition(instanceId, logicalX, logicalY, corner) @@ -304,7 +304,7 @@ internal class WindowsTrayManager( (bounds.x * scale).toInt(), (bounds.y * scale).toInt(), (bounds.width * scale).toInt(), - (bounds.height * scale).toInt() + (bounds.height * scale).toInt(), ) if (physBounds.contains(physicalX, physicalY)) { return scale @@ -345,27 +345,25 @@ internal class WindowsTrayManager( tooltip = update.tooltip onLeftClick = update.onLeftClick - // Clear old references - val oldCallbackCount = callbackReferences.size - callbackReferences.clear() - nativeMenuItemsReferences.clear() - log("Cleared $oldCallbackCount old callbacks") + val handle = trayHandle + if (handle == 0L) return - // Create a new tray structure - val newTray = WindowsNativeTray().apply { - icon_filepath = update.iconPath - tooltip = update.tooltip - } + // Free old menu handles + freeMenuHandles() + + // Update tray properties + WindowsNativeBridge.nativeSetTrayIcon(handle, update.iconPath) + WindowsNativeBridge.nativeSetTrayTooltip(handle, update.tooltip) // Set up new callbacks and menu - setupLeftClickCallback(newTray) - setupMenu(newTray, update.menuItems) + setupLeftClickCallback(handle) + setupMenu(handle, update.menuItems) // Update the native tray - log("Calling tray_update()") + log("Calling nativeUpdateTray()") try { - WindowsNativeTrayLibrary.tray_update(newTray) - log("tray_update() completed") + WindowsNativeBridge.nativeUpdateTray(handle) + log("nativeUpdateTray() completed") } catch (e: Error) { log("Failed to update tray (memory access error): ${e.message}") e.printStackTrace() @@ -373,16 +371,15 @@ internal class WindowsTrayManager( log("Failed to update tray: ${e.message}") e.printStackTrace() } - - // Update the reference - tray.set(newTray) } - private fun setupLeftClickCallback(trayObj: WindowsNativeTray) { - trayCallback = if (onLeftClick != null) { + private fun setupLeftClickCallback(handle: Long) { + val leftClick = onLeftClick + if (leftClick != null) { log("Setting up left click callback") - object : WindowsNativeTray.TrayCallback { - override fun invoke(tray: WindowsNativeTray) { + WindowsNativeBridge.nativeSetTrayCallback( + handle, + Runnable { log("Left click callback invoked") try { // Capture precise tray position on the tray thread (per-instance) @@ -391,61 +388,49 @@ internal class WindowsTrayManager( // Execute callback in IO scope (like macOS) mainScope?.launch { ioScope?.launch { - onLeftClick?.invoke() + leftClick() } } } catch (e: Exception) { log("Error in left click callback: ${e.message}") e.printStackTrace() } - } - } + }, + ) } else { log("No left click callback set") - null - } - trayObj.cb = trayCallback - if (trayCallback != null) { - callbackReferences.add(trayCallback!!) + WindowsNativeBridge.nativeSetTrayCallback(handle, null) } } - private fun setupMenu(trayObj: WindowsNativeTray, menuItems: List) { + private fun setupMenu(handle: Long, menuItems: List) { if (menuItems.isEmpty()) { log("No menu items to set up") - trayObj.menu = null + WindowsNativeBridge.nativeClearTrayMenu(handle) return } log("Setting up ${menuItems.size} menu items") - val menuItemPrototype = WindowsNativeTrayMenuItem() - val nativeMenuItems = menuItemPrototype.toArray(menuItems.size + 1) as Array - - menuItems.forEachIndexed { index, item -> - val nativeItem = nativeMenuItems[index] - initializeNativeMenuItem(nativeItem, item) - nativeItem.write() - nativeMenuItemsReferences.add(nativeItem) - } - - // Last element to mark the end of the menu - nativeMenuItems[menuItems.size].text = null - nativeMenuItems[menuItems.size].write() - - trayObj.menu = nativeMenuItems[0].pointer + val menuHandle = buildMenuItems(menuItems) + WindowsNativeBridge.nativeSetTrayMenu(handle, menuHandle) } - private fun initializeNativeMenuItem(nativeItem: WindowsNativeTrayMenuItem, menuItem: MenuItem) { - nativeItem.text = menuItem.text - nativeItem.icon_path = menuItem.iconPath - nativeItem.disabled = if (menuItem.isEnabled) 0 else 1 - nativeItem.checked = if (menuItem.isChecked) 1 else 0 - - // Create the menu item callback - menuItem.onClick?.let { onClick -> - val callback = object : StdCallCallback { - override fun invoke(item: WindowsNativeTrayMenuItem) { - log("Menu item clicked: ${menuItem.text}") + private fun buildMenuItems(menuItems: List): Long { + val menuHandle = WindowsNativeBridge.nativeCreateMenuItems(menuItems.size) + menuHandles.add(menuHandle to menuItems.size) + + menuItems.forEachIndexed { index, item -> + WindowsNativeBridge.nativeSetMenuItem( + menuHandle, index, + item.text, + item.iconPath, + if (item.isEnabled) 0 else 1, + if (item.isChecked) 1 else 0, + ) + + item.onClick?.let { onClick -> + val callback = Runnable { + log("Menu item clicked: ${item.text}") try { // Capture precise tray position on the tray thread (per-instance) safeGetTrayPosition(instanceId) @@ -455,14 +440,6 @@ internal class WindowsTrayManager( mainScope?.launch { ioScope?.launch { onClick() - if (menuItem.isCheckable) { - // Queue an update to refresh the checked state - synchronized(updateQueueLock) { - // We need to update the menu with the new checked state - // This would require keeping track of menu state - // For now, the update will come from the application - } - } } } } @@ -471,48 +448,50 @@ internal class WindowsTrayManager( e.printStackTrace() } } + WindowsNativeBridge.nativeSetMenuItemCallback(menuHandle, index, callback) } - nativeItem.cb = callback - callbackReferences.add(callback) - } - - // Handle submenus - if (menuItem.subMenuItems.isNotEmpty()) { - val subMenuPrototype = WindowsNativeTrayMenuItem() - val subMenuItemsArray = subMenuPrototype.toArray(menuItem.subMenuItems.size + 1) as Array - menuItem.subMenuItems.forEachIndexed { index, subItem -> - initializeNativeMenuItem(subMenuItemsArray[index], subItem) - subMenuItemsArray[index].write() - nativeMenuItemsReferences.add(subMenuItemsArray[index]) + if (item.subMenuItems.isNotEmpty()) { + val subMenuHandle = buildMenuItems(item.subMenuItems) + WindowsNativeBridge.nativeSetMenuItemSubmenu(menuHandle, index, subMenuHandle) } + } + + return menuHandle + } - // End marker - subMenuItemsArray[menuItem.subMenuItems.size].text = null - subMenuItemsArray[menuItem.subMenuItems.size].write() - nativeItem.submenu = subMenuItemsArray[0].pointer + private fun freeMenuHandles() { + for ((handle, count) in menuHandles) { + WindowsNativeBridge.nativeFreeMenuItems(handle, count) } + menuHandles.clear() } private fun cleanupTray() { if (initialized.get()) { try { - log("Calling tray_exit()") - WindowsNativeTrayLibrary.tray_exit() + log("Calling nativeExitTray()") + WindowsNativeBridge.nativeExitTray() } catch (e: Error) { - log("Error in tray_exit() (memory access): ${e.message}") + log("Error in nativeExitTray() (memory access): ${e.message}") e.printStackTrace() } catch (e: Exception) { - log("Error in tray_exit(): ${e.message}") + log("Error in nativeExitTray(): ${e.message}") e.printStackTrace() } } - // Clear all references - callbackReferences.clear() - nativeMenuItemsReferences.clear() - trayCallback = null - tray.set(null) + // Free menu handles + freeMenuHandles() + + // Free tray struct + val handle = trayHandle + if (handle != 0L) { + try { + WindowsNativeBridge.nativeFreeTray(handle) + } catch (_: Throwable) { } + trayHandle = 0L + } initialized.set(false) } @@ -551,4 +530,4 @@ internal class WindowsTrayManager( log("Tray stopped and cleaned up") } } -} \ No newline at end of file +} diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/utils/NativeLibraryLoader.kt b/src/jvmMain/kotlin/com/kdroid/composetray/utils/NativeLibraryLoader.kt index 78a87f6a..4c3e8dc0 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/utils/NativeLibraryLoader.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/utils/NativeLibraryLoader.kt @@ -1,7 +1,5 @@ package com.kdroid.composetray.utils -import com.sun.jna.Native -import com.sun.jna.NativeLibrary import java.io.File import java.nio.file.Files import java.nio.file.StandardCopyOption @@ -19,8 +17,6 @@ internal object NativeLibraryLoader { private const val RESOURCE_PREFIX = "composetray/native" private val loadedLibraries = mutableSetOf() - // ── JNI loading (macOS) ───────────────────────────────────────────── - /** * Loads a JNI library via [System.load] / [System.loadLibrary]. * Returns `true` if the library was loaded successfully. @@ -45,29 +41,6 @@ internal object NativeLibraryLoader { return true } - // ── JNA loading (Linux / Windows) ─────────────────────────────────── - - /** - * Extracts the native library from the classpath and registers it with JNA. - */ - @Synchronized - fun extractAndRegister(libraryName: String, caller: Class<*>) { - // 1. Try JNA's default resolution (system path) - try { - Native.register(caller, libraryName) - return - } catch (_: UnsatisfiedLinkError) { - // Not on system path, extract first - } - - // 2. Extract from classpath to persistent cache, then register - val file = extractToCache(libraryName, caller) - if (file != null) { - NativeLibrary.addSearchPath(libraryName, file.parentFile.absolutePath) - } - Native.register(caller, libraryName) - } - // ── Shared extraction logic ───────────────────────────────────────── private fun extractToCache(libraryName: String, callerClass: Class<*>): File? { diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/utils/TrayPosition.kt b/src/jvmMain/kotlin/com/kdroid/composetray/utils/TrayPosition.kt index 343ca8cb..dac329aa 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/utils/TrayPosition.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/utils/TrayPosition.kt @@ -1,6 +1,6 @@ package com.kdroid.composetray.utils -import com.kdroid.composetray.lib.windows.WindowsNativeTrayLibrary +import com.kdroid.composetray.lib.windows.WindowsNativeBridge import androidx.compose.ui.unit.dp import androidx.compose.ui.window.WindowPosition import com.kdroid.composetray.lib.mac.MacNativeBridge @@ -214,7 +214,7 @@ internal fun getWindowsTrayPosition(nativeResult: String?): TrayPosition = when /** OS → Tray corner heuristics */ fun getTrayPosition(): TrayPosition { return when (getOperatingSystem()) { - OperatingSystem.WINDOWS -> getWindowsTrayPosition(WindowsNativeTrayLibrary.tray_get_notification_icons_region()) + OperatingSystem.WINDOWS -> getWindowsTrayPosition(WindowsNativeBridge.nativeGetNotificationIconsRegion()) OperatingSystem.MACOS -> getMacTrayPosition(MacNativeBridge.nativeGetStatusItemRegion()) OperatingSystem.LINUX -> { TrayClickTracker.getLastClickPosition()?.position diff --git a/src/jvmMain/resources/META-INF/native-image/io.github.kdroidfilter/composenativetray/reachability-metadata.json b/src/jvmMain/resources/META-INF/native-image/io.github.kdroidfilter/composenativetray/reachability-metadata.json index 8a7cfdf6..b277bf8f 100644 --- a/src/jvmMain/resources/META-INF/native-image/io.github.kdroidfilter/composenativetray/reachability-metadata.json +++ b/src/jvmMain/resources/META-INF/native-image/io.github.kdroidfilter/composenativetray/reachability-metadata.json @@ -14,6 +14,10 @@ } ] }, + { + "type": "com.kdroid.composetray.lib.windows.WindowsNativeBridge", + "jniAccessible": true + }, { "type": "com.kdroid.composetray.lib.linux.LinuxNativeBridge", "jniAccessible": true diff --git a/src/native/windows/CMakeLists.txt b/src/native/windows/CMakeLists.txt index ad54c013..96af1a7a 100644 --- a/src/native/windows/CMakeLists.txt +++ b/src/native/windows/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.13 FATAL_ERROR) -project(tray LANGUAGES C CXX) +project(WinTray LANGUAGES C CXX) # Define the C++ standard set(CMAKE_CXX_STANDARD 17) @@ -29,25 +29,32 @@ endif() # Ensure output directory exists file(MAKE_DIRECTORY ${OUTPUT_DIR}) -# Add sources for libtray -list(APPEND SRCS ${CMAKE_CURRENT_SOURCE_DIR}/tray_windows.c) +# ── JNI headers ───────────────────────────────────────────────────────────────── +find_package(JNI REQUIRED) +include_directories(${JNI_INCLUDE_DIRS}) + +# Add sources +list(APPEND SRCS + ${CMAKE_CURRENT_SOURCE_DIR}/tray_windows.c + ${CMAKE_CURRENT_SOURCE_DIR}/jni_bridge.c +) # Create the shared library -add_library(tray SHARED ${SRCS}) -set_property(TARGET tray PROPERTY C_STANDARD 99) -target_compile_definitions(tray PRIVATE TRAY_EXPORTS) -set_target_properties(tray PROPERTIES C_VISIBILITY_PRESET hidden) +add_library(WinTray SHARED ${SRCS}) +set_property(TARGET WinTray PROPERTY C_STANDARD 99) +target_compile_definitions(WinTray PRIVATE TRAY_EXPORTS) +set_target_properties(WinTray PROPERTIES C_VISIBILITY_PRESET hidden) # Set necessary preprocessor definitions -target_compile_definitions(tray PRIVATE TRAY_WINAPI=1 WIN32_LEAN_AND_MEAN NOMINMAX) +target_compile_definitions(WinTray PRIVATE TRAY_WINAPI=1 WIN32_LEAN_AND_MEAN NOMINMAX) # Link to Shell32.lib for Windows platform if(WIN32) - target_link_libraries(tray PRIVATE shell32) + target_link_libraries(WinTray PRIVATE shell32) endif() # Define the installation path -INSTALL(TARGETS tray DESTINATION lib) +INSTALL(TARGETS WinTray DESTINATION lib) IF (NOT WIN32) INSTALL(FILES tray.h DESTINATION include) @@ -61,19 +68,14 @@ message(STATUS "Output directory: ${OUTPUT_DIR}") file(MAKE_DIRECTORY ${OUTPUT_DIR}) # Post-build step to copy DLL -add_custom_command(TARGET tray POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy $ ${OUTPUT_DIR}/tray.dll) +add_custom_command(TARGET WinTray POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy $ ${OUTPUT_DIR}/WinTray.dll) # Set MinSizeRel options to reduce size set(CMAKE_BUILD_TYPE MinSizeRel) # Add any further optimizations for MinSizeRel if(CMAKE_BUILD_TYPE STREQUAL "MinSizeRel") - target_compile_options(tray PRIVATE "/O1") # Optimize for size + target_compile_options(WinTray PRIVATE "/O1") # Optimize for size set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /OPT:REF /OPT:ICF") # Remove unused code and merge identical functions endif() - -# Ajout de l'exemple en français -add_executable(exemple ${CMAKE_CURRENT_SOURCE_DIR}/example.c) -target_link_libraries(exemple PRIVATE tray) -set_property(TARGET exemple PROPERTY C_STANDARD 99) diff --git a/src/native/windows/build.bat b/src/native/windows/build.bat index c35b23b3..c6c0d2a6 100644 --- a/src/native/windows/build.bat +++ b/src/native/windows/build.bat @@ -1,6 +1,12 @@ @echo off echo === Starting compilation for x64 and ARM64 in MinSizeRel mode === +:: Detect JAVA_HOME for JNI headers +if not defined JAVA_HOME ( + echo WARNING: JAVA_HOME is not set. JNI headers may not be found. + echo Set JAVA_HOME to your JDK installation directory. +) + echo. echo === x64 Configuration (MinSizeRel) === cmake -B build-x64 -A x64 -DCMAKE_BUILD_TYPE=MinSizeRel . @@ -36,5 +42,5 @@ if %ERRORLEVEL% neq 0 ( echo. echo === Compilation completed successfully for both architectures in MinSizeRel mode === echo. -echo x64 DLL: ..\..\jvmMain\resources\composetray\native\win32-x86-64\tray.dll -echo ARM64 DLL: ..\..\jvmMain\resources\composetray\native\win32-arm64\tray.dll +echo x64 DLL: ..\..\jvmMain\resources\composetray\native\win32-x86-64\WinTray.dll +echo ARM64 DLL: ..\..\jvmMain\resources\composetray\native\win32-arm64\WinTray.dll diff --git a/src/native/windows/jni_bridge.c b/src/native/windows/jni_bridge.c new file mode 100644 index 00000000..cb196bd9 --- /dev/null +++ b/src/native/windows/jni_bridge.c @@ -0,0 +1,501 @@ +/* + * jni_bridge.c - JNI bridge for ComposeNativeTray Windows native library. + * + * Target Kotlin class: com.kdroid.composetray.lib.windows.WindowsNativeBridge + * JNI prefix: Java_com_kdroid_composetray_lib_windows_WindowsNativeBridge_ + * + * Follows the same patterns as macOS MacTrayBridge.m and Linux jni_bridge.c: + * - JavaVM cache with JNI_OnLoad + * - GlobalRef-based callback storage + * - Runnable trampolines for callbacks + * - Handle-based state (struct tray* as jlong) + */ + +#include +#include +#include +#include +#include + +#include "tray.h" + +/* ========================================================================== */ +/* JavaVM cache */ +/* ========================================================================== */ + +static JavaVM *g_jvm = NULL; + +JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) { + (void)reserved; + g_jvm = vm; + return JNI_VERSION_1_8; +} + +static JNIEnv *getJNIEnv(void) { + JNIEnv *env = NULL; + if (g_jvm == NULL) return NULL; + jint rc = (*g_jvm)->GetEnv(g_jvm, (void **)&env, JNI_VERSION_1_8); + if (rc == JNI_EDETACHED) { + (*g_jvm)->AttachCurrentThread(g_jvm, (void **)&env, NULL); + } + return env; +} + +/* ========================================================================== */ +/* Callback storage (GlobalRef linked list, same as Linux/Mac bridges) */ +/* ========================================================================== */ + +typedef struct CallbackEntry { + uintptr_t key; + jobject globalRef; + struct CallbackEntry *next; +} CallbackEntry; + +static CallbackEntry *g_trayCallbacks = NULL; +static CallbackEntry *g_menuCallbacks = NULL; + +static void storeCallback(CallbackEntry **list, uintptr_t key, JNIEnv *env, jobject callback) { + CallbackEntry **pp = list; + while (*pp) { + if ((*pp)->key == key) { + CallbackEntry *old = *pp; + *pp = old->next; + (*env)->DeleteGlobalRef(env, old->globalRef); + free(old); + break; + } + pp = &(*pp)->next; + } + if (callback == NULL) return; + CallbackEntry *entry = (CallbackEntry *)malloc(sizeof(CallbackEntry)); + entry->key = key; + entry->globalRef = (*env)->NewGlobalRef(env, callback); + entry->next = *list; + *list = entry; +} + +static jobject findCallback(CallbackEntry *list, uintptr_t key) { + for (CallbackEntry *e = list; e; e = e->next) { + if (e->key == key) return e->globalRef; + } + return NULL; +} + +static void clearAllCallbacks(CallbackEntry **list) { + JNIEnv *env = getJNIEnv(); + CallbackEntry *e = *list; + while (e) { + CallbackEntry *next = e->next; + if (env) (*env)->DeleteGlobalRef(env, e->globalRef); + free(e); + e = next; + } + *list = NULL; +} + +/* ========================================================================== */ +/* Runnable invocation helper */ +/* ========================================================================== */ + +/* Cached Runnable class and run() method ID. + * Using the interface class (java.lang.Runnable) instead of GetObjectClass() + * so GraalVM native-image can resolve the method without needing to register + * every lambda class for JNI access. */ +static jclass g_runnableClass = NULL; +static jmethodID g_runMethod = NULL; + +static void ensureRunnableCached(JNIEnv *env) { + if (g_runnableClass) return; + jclass cls = (*env)->FindClass(env, "java/lang/Runnable"); + if (!cls) return; + g_runnableClass = (*env)->NewGlobalRef(env, cls); + g_runMethod = (*env)->GetMethodID(env, g_runnableClass, "run", "()V"); +} + +static void invokeRunnable(jobject runnable) { + JNIEnv *env = getJNIEnv(); + if (!env || !runnable) return; + ensureRunnableCached(env); + if (!g_runMethod) return; + (*env)->CallVoidMethod(env, runnable, g_runMethod); + if ((*env)->ExceptionCheck(env)) (*env)->ExceptionClear(env); +} + +/* ========================================================================== */ +/* C callback trampolines */ +/* ========================================================================== */ + +static void tray_cb_trampoline(struct tray *t) { + uintptr_t key = (uintptr_t)t; + jobject runnable = findCallback(g_trayCallbacks, key); + if (runnable) invokeRunnable(runnable); +} + +static void menu_item_cb_trampoline(struct tray_menu_item *item) { + uintptr_t key = (uintptr_t)item; + jobject runnable = findCallback(g_menuCallbacks, key); + if (runnable) invokeRunnable(runnable); +} + +/* ========================================================================== */ +/* Helper: duplicate UTF-8 string from JNI */ +/* ========================================================================== */ + +static char *jni_strdup(JNIEnv *env, jstring jstr) { + if (!jstr) return NULL; + const char *utf = (*env)->GetStringUTFChars(env, jstr, NULL); + if (!utf) return NULL; + char *copy = _strdup(utf); + (*env)->ReleaseStringUTFChars(env, jstr, utf); + return copy; +} + +/* ========================================================================== */ +/* JNI exports: Tray lifecycle */ +/* ========================================================================== */ + +JNIEXPORT jlong JNICALL +Java_com_kdroid_composetray_lib_windows_WindowsNativeBridge_nativeCreateTray( + JNIEnv *env, jclass clazz, jstring iconPath, jstring tooltip) +{ + (void)clazz; + struct tray *t = (struct tray *)calloc(1, sizeof(struct tray)); + if (!t) return 0; + t->icon_filepath = jni_strdup(env, iconPath); + t->tooltip = jni_strdup(env, tooltip); + t->cb = NULL; + t->menu = NULL; + return (jlong)(uintptr_t)t; +} + +JNIEXPORT void JNICALL +Java_com_kdroid_composetray_lib_windows_WindowsNativeBridge_nativeFreeTray( + JNIEnv *env, jclass clazz, jlong handle) +{ + (void)clazz; + struct tray *t = (struct tray *)(uintptr_t)handle; + if (!t) return; + + /* Remove tray callback */ + storeCallback(&g_trayCallbacks, (uintptr_t)t, env, NULL); + + free((void *)t->icon_filepath); + free((void *)t->tooltip); + /* menu is freed separately via nativeFreeMenuItems */ + free(t); +} + +JNIEXPORT void JNICALL +Java_com_kdroid_composetray_lib_windows_WindowsNativeBridge_nativeSetTrayIcon( + JNIEnv *env, jclass clazz, jlong handle, jstring iconPath) +{ + (void)clazz; + struct tray *t = (struct tray *)(uintptr_t)handle; + if (!t) return; + free((void *)t->icon_filepath); + t->icon_filepath = jni_strdup(env, iconPath); +} + +JNIEXPORT void JNICALL +Java_com_kdroid_composetray_lib_windows_WindowsNativeBridge_nativeSetTrayTooltip( + JNIEnv *env, jclass clazz, jlong handle, jstring tooltip) +{ + (void)clazz; + struct tray *t = (struct tray *)(uintptr_t)handle; + if (!t) return; + free((void *)t->tooltip); + t->tooltip = jni_strdup(env, tooltip); +} + +JNIEXPORT void JNICALL +Java_com_kdroid_composetray_lib_windows_WindowsNativeBridge_nativeSetTrayCallback( + JNIEnv *env, jclass clazz, jlong handle, jobject callback) +{ + (void)clazz; + struct tray *t = (struct tray *)(uintptr_t)handle; + if (!t) return; + storeCallback(&g_trayCallbacks, (uintptr_t)t, env, callback); + t->cb = callback ? tray_cb_trampoline : NULL; +} + +JNIEXPORT void JNICALL +Java_com_kdroid_composetray_lib_windows_WindowsNativeBridge_nativeSetTrayMenu( + JNIEnv *env, jclass clazz, jlong trayHandle, jlong menuHandle) +{ + (void)env; (void)clazz; + struct tray *t = (struct tray *)(uintptr_t)trayHandle; + struct tray_menu_item *m = (struct tray_menu_item *)(uintptr_t)menuHandle; + if (t) t->menu = m; +} + +JNIEXPORT void JNICALL +Java_com_kdroid_composetray_lib_windows_WindowsNativeBridge_nativeClearTrayMenu( + JNIEnv *env, jclass clazz, jlong trayHandle) +{ + (void)env; (void)clazz; + struct tray *t = (struct tray *)(uintptr_t)trayHandle; + if (t) t->menu = NULL; +} + +JNIEXPORT jint JNICALL +Java_com_kdroid_composetray_lib_windows_WindowsNativeBridge_nativeInitTray( + JNIEnv *env, jclass clazz, jlong handle) +{ + (void)env; (void)clazz; + struct tray *t = (struct tray *)(uintptr_t)handle; + if (!t) return -1; + return (jint)tray_init(t); +} + +JNIEXPORT jint JNICALL +Java_com_kdroid_composetray_lib_windows_WindowsNativeBridge_nativeLoopTray( + JNIEnv *env, jclass clazz, jint blocking) +{ + (void)env; (void)clazz; + return (jint)tray_loop((int)blocking); +} + +JNIEXPORT void JNICALL +Java_com_kdroid_composetray_lib_windows_WindowsNativeBridge_nativeUpdateTray( + JNIEnv *env, jclass clazz, jlong handle) +{ + (void)env; (void)clazz; + struct tray *t = (struct tray *)(uintptr_t)handle; + if (t) tray_update(t); +} + +JNIEXPORT void JNICALL +Java_com_kdroid_composetray_lib_windows_WindowsNativeBridge_nativeExitTray( + JNIEnv *env, jclass clazz) +{ + (void)env; (void)clazz; + tray_exit(); + clearAllCallbacks(&g_menuCallbacks); +} + +/* ========================================================================== */ +/* JNI exports: Menu items */ +/* ========================================================================== */ + +JNIEXPORT jlong JNICALL +Java_com_kdroid_composetray_lib_windows_WindowsNativeBridge_nativeCreateMenuItems( + JNIEnv *env, jclass clazz, jint count) +{ + (void)env; (void)clazz; + /* Allocate count+1 items; last one is zero-terminated (text=NULL) */ + struct tray_menu_item *items = (struct tray_menu_item *)calloc( + (size_t)count + 1, sizeof(struct tray_menu_item)); + return (jlong)(uintptr_t)items; +} + +JNIEXPORT void JNICALL +Java_com_kdroid_composetray_lib_windows_WindowsNativeBridge_nativeSetMenuItem( + JNIEnv *env, jclass clazz, jlong menuHandle, jint index, + jstring text, jstring iconPath, jint disabled, jint checked) +{ + (void)clazz; + struct tray_menu_item *items = (struct tray_menu_item *)(uintptr_t)menuHandle; + if (!items) return; + struct tray_menu_item *item = &items[index]; + + /* Free old strings if any */ + free(item->text); + free(item->icon_path); + + item->text = jni_strdup(env, text); + item->icon_path = jni_strdup(env, iconPath); + item->disabled = (int)disabled; + item->checked = (int)checked; + item->cb = NULL; + item->submenu = NULL; +} + +JNIEXPORT void JNICALL +Java_com_kdroid_composetray_lib_windows_WindowsNativeBridge_nativeSetMenuItemCallback( + JNIEnv *env, jclass clazz, jlong menuHandle, jint index, jobject callback) +{ + (void)clazz; + struct tray_menu_item *items = (struct tray_menu_item *)(uintptr_t)menuHandle; + if (!items) return; + struct tray_menu_item *item = &items[index]; + storeCallback(&g_menuCallbacks, (uintptr_t)item, env, callback); + item->cb = callback ? menu_item_cb_trampoline : NULL; +} + +JNIEXPORT void JNICALL +Java_com_kdroid_composetray_lib_windows_WindowsNativeBridge_nativeSetMenuItemSubmenu( + JNIEnv *env, jclass clazz, jlong menuHandle, jint index, jlong submenuHandle) +{ + (void)env; (void)clazz; + struct tray_menu_item *items = (struct tray_menu_item *)(uintptr_t)menuHandle; + if (!items) return; + items[index].submenu = (struct tray_menu_item *)(uintptr_t)submenuHandle; +} + +JNIEXPORT void JNICALL +Java_com_kdroid_composetray_lib_windows_WindowsNativeBridge_nativeFreeMenuItems( + JNIEnv *env, jclass clazz, jlong menuHandle, jint count) +{ + (void)clazz; + struct tray_menu_item *items = (struct tray_menu_item *)(uintptr_t)menuHandle; + if (!items) return; + for (int i = 0; i < count; i++) { + struct tray_menu_item *item = &items[i]; + storeCallback(&g_menuCallbacks, (uintptr_t)item, env, NULL); + free(item->text); + free(item->icon_path); + /* submenus are freed separately by Java */ + } + free(items); +} + +/* ========================================================================== */ +/* JNI exports: Position */ +/* ========================================================================== */ + +JNIEXPORT jint JNICALL +Java_com_kdroid_composetray_lib_windows_WindowsNativeBridge_nativeGetNotificationIconsPosition( + JNIEnv *env, jclass clazz, jintArray outXY) +{ + (void)clazz; + int x = 0, y = 0; + int precise = tray_get_notification_icons_position(&x, &y); + jint buf[2] = { (jint)x, (jint)y }; + (*env)->SetIntArrayRegion(env, outXY, 0, 2, buf); + return (jint)precise; +} + +JNIEXPORT jstring JNICALL +Java_com_kdroid_composetray_lib_windows_WindowsNativeBridge_nativeGetNotificationIconsRegion( + JNIEnv *env, jclass clazz) +{ + (void)clazz; + const char *result = tray_get_notification_icons_region(); + if (!result) return NULL; + return (*env)->NewStringUTF(env, result); +} + +/* ========================================================================== */ +/* Mouse hook (outside-click watcher, replaces JNA WH_MOUSE_LL) */ +/* ========================================================================== */ + +typedef struct MouseHookContext { + HHOOK hook; + DWORD threadId; + jobject callback; /* Runnable GlobalRef */ + volatile LONG lastClickX; + volatile LONG lastClickY; + volatile BOOL stopping; +} MouseHookContext; + +/* Single global hook context pointer, protected by the hook thread's own + * identity (each hook runs on its dedicated thread). */ +static MouseHookContext *g_mouseHookCtx = NULL; + +static LRESULT CALLBACK lowLevelMouseProc(int nCode, WPARAM wParam, LPARAM lParam) { + MouseHookContext *ctx = g_mouseHookCtx; + HHOOK hook = ctx ? ctx->hook : NULL; + + if (nCode >= 0 && ctx && !ctx->stopping) { + int msg = (int)wParam; + if (msg == WM_LBUTTONDOWN || msg == 0x00A1 /* WM_NCLBUTTONDOWN */) { + MSLLHOOKSTRUCT *p = (MSLLHOOKSTRUCT *)lParam; + InterlockedExchange(&ctx->lastClickX, p->pt.x); + InterlockedExchange(&ctx->lastClickY, p->pt.y); + if (ctx->callback) { + invokeRunnable(ctx->callback); + } + } + } + + return CallNextHookEx(hook, nCode, wParam, lParam); +} + +JNIEXPORT jlong JNICALL +Java_com_kdroid_composetray_lib_windows_WindowsNativeBridge_nativeInstallMouseHook( + JNIEnv *env, jclass clazz, jobject callback) +{ + (void)clazz; + + MouseHookContext *ctx = (MouseHookContext *)calloc(1, sizeof(MouseHookContext)); + if (!ctx) return 0; + + ctx->threadId = GetCurrentThreadId(); + ctx->callback = (*env)->NewGlobalRef(env, callback); + ctx->stopping = FALSE; + ctx->lastClickX = 0; + ctx->lastClickY = 0; + + g_mouseHookCtx = ctx; + + HMODULE hMod = GetModuleHandleW(NULL); + ctx->hook = SetWindowsHookExW(WH_MOUSE_LL, lowLevelMouseProc, hMod, 0); + + if (!ctx->hook) { + g_mouseHookCtx = NULL; + (*env)->DeleteGlobalRef(env, ctx->callback); + free(ctx); + return 0; + } + + return (jlong)(uintptr_t)ctx; +} + +JNIEXPORT void JNICALL +Java_com_kdroid_composetray_lib_windows_WindowsNativeBridge_nativeRunMouseHookLoop( + JNIEnv *env, jclass clazz, jlong hookId) +{ + (void)env; (void)clazz; + MouseHookContext *ctx = (MouseHookContext *)(uintptr_t)hookId; + if (!ctx) return; + + MSG msg; + while (!ctx->stopping) { + BOOL r = GetMessageW(&msg, NULL, 0, 0); + if (r == 0 || r == -1) break; /* WM_QUIT or error */ + } +} + +JNIEXPORT void JNICALL +Java_com_kdroid_composetray_lib_windows_WindowsNativeBridge_nativeStopMouseHook( + JNIEnv *env, jclass clazz, jlong hookId) +{ + (void)clazz; + MouseHookContext *ctx = (MouseHookContext *)(uintptr_t)hookId; + if (!ctx) return; + + ctx->stopping = TRUE; + + if (ctx->hook) { + UnhookWindowsHookEx(ctx->hook); + ctx->hook = NULL; + } + + /* Post WM_QUIT to break GetMessage loop */ + if (ctx->threadId != 0) { + PostThreadMessageW(ctx->threadId, WM_QUIT, 0, 0); + } + + if (g_mouseHookCtx == ctx) g_mouseHookCtx = NULL; + + if (ctx->callback) { + (*env)->DeleteGlobalRef(env, ctx->callback); + ctx->callback = NULL; + } + + free(ctx); +} + +JNIEXPORT void JNICALL +Java_com_kdroid_composetray_lib_windows_WindowsNativeBridge_nativeGetLastMouseHookClick( + JNIEnv *env, jclass clazz, jintArray outXY) +{ + (void)clazz; + MouseHookContext *ctx = g_mouseHookCtx; + jint buf[2] = { 0, 0 }; + if (ctx) { + buf[0] = (jint)InterlockedCompareExchange(&ctx->lastClickX, 0, 0); + buf[1] = (jint)InterlockedCompareExchange(&ctx->lastClickY, 0, 0); + } + (*env)->SetIntArrayRegion(env, outXY, 0, 2, buf); +} From cc4eb866c0734e96a3178d00728c634e730c8e65 Mon Sep 17 00:00:00 2001 From: "Elie G." Date: Thu, 9 Apr 2026 20:46:46 +0300 Subject: [PATCH 11/12] fix(lint): resolve ktlint violations with explicit imports and formatting - Replace all wildcard imports with explicit imports across main module - Break lines exceeding 120 char limit - Move inline comments to separate lines per ktlint rules - Rename MacOsWindowManager.kt to MacOSWindowManager.kt for PascalCase - Rename AppId.kt to AppIdProvider.kt to match top-level declaration - Exclude demo module from ktlint checks - Fix POM description formatting in build.gradle.kts Co-Authored-By: Claude Opus 4.6 (1M context) --- .github/workflows/build-natives.yaml | 28 +- .github/workflows/pr-build-check.yml | 69 +++ .github/workflows/publish-on-maven.yml | 7 + build.gradle.kts | 85 ++- .../demo/svg/DeployedCodeUpdate.kt | 78 +++ .../lib/linux/LinuxNativeBridge.kt | 139 ++++- .../lib/linux/LinuxOutsideClickWatcher.kt | 33 +- .../composetray/lib/linux/LinuxTrayManager.kt | 157 ++++-- .../composetray/lib/mac/MacNativeBridge.kt | 88 ++- .../lib/mac/MacOSMenuBarThemeDetector.kt | 10 +- ...WindowManager.kt => MacOSWindowManager.kt} | 1 - .../lib/mac/MacOutsideClickWatcher.kt | 33 +- .../composetray/lib/mac/MacTrayManager.kt | 130 +++-- .../lib/windows/WindowsNativeBridge.kt | 61 ++- .../lib/windows/WindowsOutsideClickWatcher.kt | 89 +-- .../lib/windows/WindowsTrayManager.kt | 161 +++--- .../composetray/menu/api/TrayMenuBuilder.kt | 511 +++++++++--------- .../menu/impl/AwtTrayMenuBuilderImpl.kt | 69 ++- .../menu/impl/LinuxTrayMenuBuilderImpl.kt | 242 +++++---- .../menu/impl/MacTrayMenuBuilderImpl.kt | 203 ++++--- .../menu/impl/WindowsTrayMenuBuilderImpl.kt | 190 ++++--- .../tray/api/ExperimentalTrayAppApi.kt | 6 +- .../kdroid/composetray/tray/api/NativeTray.kt | 188 +++++-- .../kdroid/composetray/tray/api/TrayApp.kt | 505 ++++++++++------- .../composetray/tray/api/TrayAppState.kt | 8 +- .../tray/api/TrayWindowDismissMode.kt | 6 +- .../tray/impl/AwtTrayInitializer.kt | 55 +- .../tray/impl/LinuxTrayInitializer.kt | 62 ++- .../tray/impl/MacTrayInitializer.kt | 72 ++- .../tray/impl/WindowsTrayInitializer.kt | 30 +- .../utils/{AppId.kt => AppIdProvider.kt} | 0 .../composetray/utils/ComposableIconUtils.kt | 81 +-- .../composetray/utils/DarkModeDetector.kt | 34 +- .../com/kdroid/composetray/utils/DebugLn.kt | 12 +- .../composetray/utils/IconRenderProperties.kt | 44 +- .../composetray/utils/JarResourceExtractor.kt | 27 +- .../composetray/utils/MenuContentHash.kt | 65 ++- .../composetray/utils/NativeLibraryLoader.kt | 32 +- .../utils/PersistentAnimatedVisibility.kt | 145 ++--- .../utils/SingleInstanceManager.kt | 25 +- .../kdroid/composetray/utils/TrayPosition.kt | 397 +++++++++----- .../kdroid/composetray/utils/WindowRaise.kt | 12 +- .../utils/WindowVisibilityMonitor.kt | 41 +- src/native/linux/build.sh | 20 +- src/native/windows/CMakeLists.txt | 3 +- src/native/windows/build.bat | 9 - src/native/windows/tray_windows.c | 4 + 47 files changed, 2672 insertions(+), 1595 deletions(-) create mode 100644 demo/src/jvmMain/kotlin/com/kdroid/composetray/demo/svg/DeployedCodeUpdate.kt rename src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/{MacOsWindowManager.kt => MacOSWindowManager.kt} (99%) rename src/jvmMain/kotlin/com/kdroid/composetray/utils/{AppId.kt => AppIdProvider.kt} (100%) diff --git a/.github/workflows/build-natives.yaml b/.github/workflows/build-natives.yaml index 116870f2..06f816d9 100644 --- a/.github/workflows/build-natives.yaml +++ b/.github/workflows/build-natives.yaml @@ -44,20 +44,28 @@ jobs: retention-days: 1 build-native-linux: - runs-on: ubuntu-latest + runs-on: ${{ matrix.os }} + strategy: + matrix: + include: + - os: ubuntu-latest + arch: x86-64 + - os: ubuntu-24.04-arm + arch: aarch64 steps: - name: Checkout code uses: actions/checkout@v4 - - name: Setup Go - uses: actions/setup-go@v5 + - name: Set up JDK + uses: actions/setup-java@v4 with: - go-version: 'stable' + java-version: '17' + distribution: 'temurin' - name: Install dependencies run: | sudo apt-get update - sudo apt-get install -y libgtk-3-dev libappindicator3-dev + sudo apt-get install -y libsystemd-dev - name: Build Linux native library working-directory: src/native/linux @@ -67,14 +75,14 @@ jobs: - name: Verify Linux natives run: | - test -f build/nativeLibs/linux-x86-64/libsystray.so - ls -la build/nativeLibs/linux-x86-64/ + test -f build/nativeLibs/linux-${{ matrix.arch }}/libLinuxTray.so + ls -la build/nativeLibs/linux-${{ matrix.arch }}/ - - name: Upload Linux x86_64 library + - name: Upload Linux library uses: actions/upload-artifact@v4 with: - name: native-linux-x86-64 - path: build/nativeLibs/linux-x86-64/libsystray.so + name: native-linux-${{ matrix.arch }} + path: build/nativeLibs/linux-${{ matrix.arch }}/libLinuxTray.so retention-days: 1 build-native-windows: diff --git a/.github/workflows/pr-build-check.yml b/.github/workflows/pr-build-check.yml index 3de77a2a..fb9f8bab 100644 --- a/.github/workflows/pr-build-check.yml +++ b/.github/workflows/pr-build-check.yml @@ -1,9 +1,78 @@ name: PR Build Check on: + push: + branches: [master] pull_request: branches: [master] +concurrency: + group: ${{ github.workflow }}-${{ github.head_ref || github.ref }} + cancel-in-progress: true + jobs: build-natives: uses: ./.github/workflows/build-natives.yaml + + gradle: + needs: [build-natives] + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Download macOS ARM64 library + uses: actions/download-artifact@v4 + with: + name: native-darwin-aarch64 + path: src/jvmMain/resources/composetray/native/darwin-aarch64/ + + - name: Download macOS x86_64 library + uses: actions/download-artifact@v4 + with: + name: native-darwin-x86-64 + path: src/jvmMain/resources/composetray/native/darwin-x86-64/ + + - name: Download Linux x86_64 library + uses: actions/download-artifact@v4 + with: + name: native-linux-x86-64 + path: src/jvmMain/resources/composetray/native/linux-x86-64/ + + - name: Download Linux aarch64 library + uses: actions/download-artifact@v4 + with: + name: native-linux-aarch64 + path: src/jvmMain/resources/composetray/native/linux-aarch64/ + + - name: Download Windows x64 library + uses: actions/download-artifact@v4 + with: + name: native-win32-x86-64 + path: src/jvmMain/resources/composetray/native/win32-x86-64/ + + - name: Download Windows ARM64 library + uses: actions/download-artifact@v4 + with: + name: native-win32-arm64 + path: src/jvmMain/resources/composetray/native/win32-arm64/ + + - name: Verify native libraries + run: | + echo "=== Verifying native libraries ===" + test -f src/jvmMain/resources/composetray/native/darwin-aarch64/libMacTray.dylib + test -f src/jvmMain/resources/composetray/native/darwin-x86-64/libMacTray.dylib + test -f src/jvmMain/resources/composetray/native/linux-x86-64/libLinuxTray.so + test -f src/jvmMain/resources/composetray/native/linux-aarch64/libLinuxTray.so + test -f src/jvmMain/resources/composetray/native/win32-x86-64/WinTray.dll + test -f src/jvmMain/resources/composetray/native/win32-arm64/WinTray.dll + echo "All native libraries present." + + - name: Set up JDK + uses: actions/setup-java@v4 + with: + java-version: '17' + distribution: 'temurin' + + - name: Build + run: ./gradlew build --continue diff --git a/.github/workflows/publish-on-maven.yml b/.github/workflows/publish-on-maven.yml index cf9bc799..55cd0bbc 100644 --- a/.github/workflows/publish-on-maven.yml +++ b/.github/workflows/publish-on-maven.yml @@ -35,6 +35,12 @@ jobs: name: native-linux-x86-64 path: src/jvmMain/resources/composetray/native/linux-x86-64/ + - name: Download Linux aarch64 library + uses: actions/download-artifact@v4 + with: + name: native-linux-aarch64 + path: src/jvmMain/resources/composetray/native/linux-aarch64/ + - name: Download Windows x64 library uses: actions/download-artifact@v4 with: @@ -53,6 +59,7 @@ jobs: ls -la src/jvmMain/resources/composetray/native/darwin-aarch64/ ls -la src/jvmMain/resources/composetray/native/darwin-x86-64/ ls -la src/jvmMain/resources/composetray/native/linux-x86-64/ + ls -la src/jvmMain/resources/composetray/native/linux-aarch64/ ls -la src/jvmMain/resources/composetray/native/win32-x86-64/ ls -la src/jvmMain/resources/composetray/native/win32-arm64/ diff --git a/build.gradle.kts b/build.gradle.kts index 61fc0783..7b1f888f 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -13,10 +13,13 @@ plugins { group = "com.kdroid.composenativetray" val ref = System.getenv("GITHUB_REF") ?: "" -val version = if (ref.startsWith("refs/tags/")) { - val tag = ref.removePrefix("refs/tags/") - if (tag.startsWith("v")) tag.substring(1) else tag -} else "dev" +val version = + if (ref.startsWith("refs/tags/")) { + val tag = ref.removePrefix("refs/tags/") + if (tag.startsWith("v")) tag.substring(1) else tag + } else { + "dev" + } repositories { mavenCentral() @@ -50,22 +53,60 @@ kotlin { // ── Native build tasks ────────────────────────────────────────────────────────── +val nativeResourceDir = layout.projectDirectory.dir("src/jvmMain/resources/composetray/native") + val buildNativeMacOs by tasks.registering(Exec::class) { - workingDir = file("src/native/macos") - commandLine("bash", "build.sh") - onlyIf { Os.isFamily(Os.FAMILY_MAC) } + description = "Compiles the Objective-C/Swift JNI bridge into macOS dylibs (arm64 + x64)" + group = "build" + val hasPrebuilt = + nativeResourceDir + .dir("darwin-aarch64") + .file("libMacTray.dylib") + .asFile + .exists() + enabled = Os.isFamily(Os.FAMILY_MAC) && !hasPrebuilt + + val nativeDir = layout.projectDirectory.dir("src/native/macos") + inputs.dir(nativeDir) + outputs.dir(nativeResourceDir) + workingDir(nativeDir) + commandLine("bash", nativeDir.file("build.sh").asFile.absolutePath) } val buildNativeWindows by tasks.registering(Exec::class) { - workingDir = file("src/native/windows") - commandLine("cmd", "/c", "build.bat") - onlyIf { Os.isFamily(Os.FAMILY_WINDOWS) } + description = "Compiles the C JNI bridge into Windows DLLs (x64 + ARM64)" + group = "build" + val hasPrebuilt = + nativeResourceDir + .dir("win32-x86-64") + .file("WinTray.dll") + .asFile + .exists() + enabled = Os.isFamily(Os.FAMILY_WINDOWS) && !hasPrebuilt + + val nativeDir = layout.projectDirectory.dir("src/native/windows") + inputs.dir(nativeDir) + outputs.dir(nativeResourceDir) + workingDir(nativeDir) + commandLine("cmd", "/c", nativeDir.file("build.bat").asFile.absolutePath) } val buildNativeLinux by tasks.registering(Exec::class) { - workingDir = file("src/native/linux") - commandLine("bash", "build.sh") - onlyIf { Os.isFamily(Os.FAMILY_UNIX) && !Os.isFamily(Os.FAMILY_MAC) } + description = "Compiles the C JNI bridge into Linux shared library (x86-64)" + group = "build" + val hasPrebuilt = + nativeResourceDir + .dir("linux-x86-64") + .file("libLinuxTray.so") + .asFile + .exists() + enabled = Os.isFamily(Os.FAMILY_UNIX) && !Os.isFamily(Os.FAMILY_MAC) && !hasPrebuilt + + val nativeDir = layout.projectDirectory.dir("src/native/linux") + inputs.dir(nativeDir) + outputs.dir(nativeResourceDir) + workingDir(nativeDir) + commandLine("bash", nativeDir.file("build.sh").asFile.absolutePath) } tasks.register("buildNativeLibraries") { @@ -76,6 +117,12 @@ tasks.named("jvmProcessResources") { dependsOn(buildNativeMacOs, buildNativeWindows, buildNativeLinux) } +tasks.configureEach { + if (name == "sourcesJar") { + dependsOn(buildNativeMacOs, buildNativeWindows, buildNativeLinux) + } +} + // ── Code quality ──────────────────────────────────────────────────────────────── detekt { @@ -83,7 +130,8 @@ detekt { buildUponDefaultConfig = true } -allprojects { +subprojects { + if (name == "demo") return@subprojects apply(plugin = "org.jlleitschuh.gradle.ktlint") ktlint { @@ -106,12 +154,17 @@ mavenPublishing { coordinates( groupId = "io.github.kdroidfilter", artifactId = "composenativetray", - version = version + version = version, ) pom { name.set("Compose Native Tray") - description.set("ComposeTray is a Kotlin library that provides a simple way to create system tray applications with native support for Linux and Windows. This library allows you to add a system tray icon, tooltip, and menu with various options in a Kotlin DSL-style syntax.") + description.set( + "ComposeTray is a Kotlin library that provides a simple way to create " + + "system tray applications with native support for Linux and Windows. " + + "This library allows you to add a system tray icon, tooltip, and menu " + + "with various options in a Kotlin DSL-style syntax.", + ) inceptionYear.set("2024") url.set("https://github.com/kdroidFilter/ComposeNativeTray") diff --git a/demo/src/jvmMain/kotlin/com/kdroid/composetray/demo/svg/DeployedCodeUpdate.kt b/demo/src/jvmMain/kotlin/com/kdroid/composetray/demo/svg/DeployedCodeUpdate.kt new file mode 100644 index 00000000..9d1362d6 --- /dev/null +++ b/demo/src/jvmMain/kotlin/com/kdroid/composetray/demo/svg/DeployedCodeUpdate.kt @@ -0,0 +1,78 @@ +package com.kdroid.composetray.demo.svg + +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.SolidColor +import androidx.compose.ui.graphics.vector.ImageVector +import androidx.compose.ui.graphics.vector.path +import androidx.compose.ui.unit.dp + +val DeployedCodeUpdate: ImageVector + get() { + if (_deployedCodeUpdate != null) return _deployedCodeUpdate!! + + _deployedCodeUpdate = + ImageVector.Builder( + name = "DeployedCodeUpdate", + defaultWidth = 24.dp, + defaultHeight = 24.dp, + viewportWidth = 960f, + viewportHeight = 960f, + ).apply { + path( + fill = SolidColor(Color(0xFF000000)), + ) { + moveToRelative(720f, -80f) + lineToRelative(120f, -120f) + lineToRelative(-28f, -28f) + lineToRelative(-72f, 72f) + verticalLineToRelative(-164f) + horizontalLineToRelative(-40f) + verticalLineToRelative(164f) + lineToRelative(-72f, -72f) + lineToRelative(-28f, 28f) + close() + moveTo(480f, 160f) + lineTo(243f, 297f) + lineToRelative(237f, 137f) + lineToRelative(237f, -137f) + close() + moveTo(120f, 639f) + verticalLineToRelative(-318f) + quadToRelative(0f, -22f, 10.5f, -40f) + reflectiveQuadToRelative(29.5f, -29f) + lineToRelative(280f, -161f) + quadToRelative(10f, -5f, 19.5f, -8f) + reflectiveQuadToRelative(20.5f, -3f) + reflectiveQuadToRelative(21f, 3f) + reflectiveQuadToRelative(19f, 8f) + lineToRelative(280f, 161f) + quadToRelative(19f, 11f, 29.5f, 29f) + reflectiveQuadToRelative(10.5f, 40f) + verticalLineToRelative(159f) + horizontalLineToRelative(-80f) + verticalLineToRelative(-116f) + lineTo(479f, 526f) + lineTo(200f, 364f) + verticalLineToRelative(274f) + lineToRelative(240f, 139f) + verticalLineToRelative(92f) + lineTo(160f, 708f) + quadToRelative(-19f, -11f, -29.5f, -29f) + reflectiveQuadTo(120f, 639f) + moveTo(720f, 960f) + quadToRelative(-83f, 0f, -141.5f, -58.5f) + reflectiveQuadTo(520f, 760f) + reflectiveQuadToRelative(58.5f, -141.5f) + reflectiveQuadTo(720f, 560f) + reflectiveQuadToRelative(141.5f, 58.5f) + reflectiveQuadTo(920f, 760f) + reflectiveQuadTo(861.5f, 901.5f) + reflectiveQuadTo(720f, 960f) + moveTo(480f, 469f) + } + }.build() + + return _deployedCodeUpdate!! + } + +private var _deployedCodeUpdate: ImageVector? = null diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/LinuxNativeBridge.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/LinuxNativeBridge.kt index 8f7b0f53..05a31c7a 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/LinuxNativeBridge.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/LinuxNativeBridge.kt @@ -8,7 +8,6 @@ import com.kdroid.composetray.utils.NativeLibraryLoader * Follows the same patterns as MacNativeBridge. */ internal object LinuxNativeBridge { - init { NativeLibraryLoader.load("LinuxTray", LinuxNativeBridge::class.java) } @@ -16,7 +15,10 @@ internal object LinuxNativeBridge { // -- Lifecycle --------------------------------------------------------------- /** Create a tray instance. Returns a native handle (pointer as long). */ - @JvmStatic external fun nativeCreate(iconBytes: ByteArray?, tooltip: String?): Long + @JvmStatic external fun nativeCreate( + iconBytes: ByteArray?, + tooltip: String?, + ): Long /** Start the D-Bus event loop (blocks until nativeQuit is called). */ @JvmStatic external fun nativeRun(handle: Long): Int @@ -29,43 +31,130 @@ internal object LinuxNativeBridge { // -- Tray properties --------------------------------------------------------- - @JvmStatic external fun nativeSetIcon(handle: Long, iconBytes: ByteArray) - @JvmStatic external fun nativeSetTitle(handle: Long, title: String?) - @JvmStatic external fun nativeSetTooltip(handle: Long, tooltip: String?) + @JvmStatic external fun nativeSetIcon( + handle: Long, + iconBytes: ByteArray, + ) + + @JvmStatic external fun nativeSetTitle( + handle: Long, + title: String?, + ) + + @JvmStatic external fun nativeSetTooltip( + handle: Long, + tooltip: String?, + ) // -- Callbacks --------------------------------------------------------------- - @JvmStatic external fun nativeSetClickCallback(handle: Long, callback: Runnable?) - @JvmStatic external fun nativeSetRClickCallback(handle: Long, callback: Runnable?) + @JvmStatic external fun nativeSetClickCallback( + handle: Long, + callback: Runnable?, + ) + + @JvmStatic external fun nativeSetRClickCallback( + handle: Long, + callback: Runnable?, + ) /** Register a per-menu-item callback. The callback is keyed by menuId. */ - @JvmStatic external fun nativeSetMenuItemCallback(handle: Long, menuId: Int, callback: Runnable?) + @JvmStatic external fun nativeSetMenuItemCallback( + handle: Long, + menuId: Int, + callback: Runnable?, + ) // -- Click position ---------------------------------------------------------- /** Writes [x, y] into outXY. */ - @JvmStatic external fun nativeGetLastClickXY(handle: Long, outXY: IntArray) + @JvmStatic external fun nativeGetLastClickXY( + handle: Long, + outXY: IntArray, + ) // -- Menu management --------------------------------------------------------- @JvmStatic external fun nativeResetMenu(handle: Long) - @JvmStatic external fun nativeAddMenuItem(handle: Long, title: String?, tooltip: String?): Int - @JvmStatic external fun nativeAddMenuItemCheckbox(handle: Long, title: String?, tooltip: String?, checked: Boolean): Int + + @JvmStatic external fun nativeAddMenuItem( + handle: Long, + title: String?, + tooltip: String?, + ): Int + + @JvmStatic external fun nativeAddMenuItemCheckbox( + handle: Long, + title: String?, + tooltip: String?, + checked: Boolean, + ): Int + @JvmStatic external fun nativeAddSeparator(handle: Long) - @JvmStatic external fun nativeAddSubMenuItem(handle: Long, parentId: Int, title: String?, tooltip: String?): Int - @JvmStatic external fun nativeAddSubMenuItemCheckbox(handle: Long, parentId: Int, title: String?, tooltip: String?, checked: Boolean): Int - @JvmStatic external fun nativeAddSubSeparator(handle: Long, parentId: Int) + + @JvmStatic external fun nativeAddSubMenuItem( + handle: Long, + parentId: Int, + title: String?, + tooltip: String?, + ): Int + + @JvmStatic external fun nativeAddSubMenuItemCheckbox( + handle: Long, + parentId: Int, + title: String?, + tooltip: String?, + checked: Boolean, + ): Int + + @JvmStatic external fun nativeAddSubSeparator( + handle: Long, + parentId: Int, + ) // -- Per-item operations ----------------------------------------------------- - @JvmStatic external fun nativeItemSetTitle(handle: Long, id: Int, title: String?): Int - @JvmStatic external fun nativeItemEnable(handle: Long, id: Int) - @JvmStatic external fun nativeItemDisable(handle: Long, id: Int) - @JvmStatic external fun nativeItemShow(handle: Long, id: Int) - @JvmStatic external fun nativeItemHide(handle: Long, id: Int) - @JvmStatic external fun nativeItemCheck(handle: Long, id: Int) - @JvmStatic external fun nativeItemUncheck(handle: Long, id: Int) - @JvmStatic external fun nativeItemSetIcon(handle: Long, id: Int, iconBytes: ByteArray) + @JvmStatic external fun nativeItemSetTitle( + handle: Long, + id: Int, + title: String?, + ): Int + + @JvmStatic external fun nativeItemEnable( + handle: Long, + id: Int, + ) + + @JvmStatic external fun nativeItemDisable( + handle: Long, + id: Int, + ) + + @JvmStatic external fun nativeItemShow( + handle: Long, + id: Int, + ) + + @JvmStatic external fun nativeItemHide( + handle: Long, + id: Int, + ) + + @JvmStatic external fun nativeItemCheck( + handle: Long, + id: Int, + ) + + @JvmStatic external fun nativeItemUncheck( + handle: Long, + id: Int, + ) + + @JvmStatic external fun nativeItemSetIcon( + handle: Long, + id: Int, + iconBytes: ByteArray, + ) // -- X11 outside-click watcher ----------------------------------------------- @@ -76,7 +165,11 @@ internal object LinuxNativeBridge { @JvmStatic external fun nativeX11DefaultRootWindow(displayHandle: Long): Long /** Query pointer. Writes [rootX, rootY, mask] into outData. Returns 1 on success. */ - @JvmStatic external fun nativeX11QueryPointer(displayHandle: Long, rootWindow: Long, outData: IntArray): Int + @JvmStatic external fun nativeX11QueryPointer( + displayHandle: Long, + rootWindow: Long, + outData: IntArray, + ): Int /** Close X11 display. */ @JvmStatic external fun nativeX11CloseDisplay(displayHandle: Long) diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/LinuxOutsideClickWatcher.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/LinuxOutsideClickWatcher.kt index 7b77fa41..e3e3bdb6 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/LinuxOutsideClickWatcher.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/LinuxOutsideClickWatcher.kt @@ -20,9 +20,8 @@ import java.util.concurrent.TimeUnit */ class LinuxOutsideClickWatcher( private val windowSupplier: () -> Window?, - private val onOutsideClick: () -> Unit + private val onOutsideClick: () -> Unit, ) : AutoCloseable { - private var scheduler: ScheduledExecutorService? = null private var prevLeft = false @@ -43,11 +42,12 @@ class LinuxOutsideClickWatcher( return } - scheduler = Executors.newSingleThreadScheduledExecutor { r -> - Thread(r, "LinuxOutsideClickWatcher").apply { isDaemon = true } - }.also { exec -> - exec.scheduleAtFixedRate({ pollOnce() }, 0, 16, TimeUnit.MILLISECONDS) - } + scheduler = + Executors.newSingleThreadScheduledExecutor { r -> + Thread(r, "LinuxOutsideClickWatcher").apply { isDaemon = true } + }.also { exec -> + exec.scheduleAtFixedRate({ pollOnce() }, 0, 16, TimeUnit.MILLISECONDS) + } } private fun pollOnce() { @@ -66,7 +66,12 @@ class LinuxOutsideClickWatcher( if (left && left != prevLeft) { val win = windowSupplier.invoke() if (win != null && win.isShowing) { - val winLoc = try { win.locationOnScreen } catch (_: Throwable) { null } + val winLoc = + try { + win.locationOnScreen + } catch (_: Throwable) { + null + } if (winLoc != null) { val wx = winLoc.x val wy = winLoc.y @@ -74,7 +79,12 @@ class LinuxOutsideClickWatcher( val wh = win.height val insideWindow = px >= wx && px < wx + ww && py >= wy && py < wy + wh - val onTrayIcon = try { isPointWithinLinuxStatusItem(px, py) } catch (_: Throwable) { false } + val onTrayIcon = + try { + isPointWithinLinuxStatusItem(px, py) + } catch (_: Throwable) { + false + } if (!insideWindow && !onTrayIcon) { onOutsideClick.invoke() @@ -92,7 +102,10 @@ class LinuxOutsideClickWatcher( fun stop() = close() override fun close() { - try { scheduler?.shutdownNow() } catch (_: Throwable) {} + try { + scheduler?.shutdownNow() + } catch (_: Throwable) { + } scheduler = null try { diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/LinuxTrayManager.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/LinuxTrayManager.kt index a41b8274..883d868c 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/LinuxTrayManager.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/lib/linux/LinuxTrayManager.kt @@ -1,14 +1,13 @@ package com.kdroid.composetray.lib.linux +import com.kdroid.composetray.utils.TrayClickTracker import com.kdroid.composetray.utils.errorln import com.kdroid.composetray.utils.infoln import com.kdroid.composetray.utils.warnln -import com.kdroid.composetray.utils.TrayClickTracker import io.github.kdroidfilter.platformtools.LinuxDesktopEnvironment import io.github.kdroidfilter.platformtools.detectLinuxDesktopEnvironment import java.io.File import java.util.concurrent.CountDownLatch -import java.util.concurrent.TimeUnit.MILLISECONDS import java.util.concurrent.atomic.AtomicBoolean import java.util.concurrent.locks.ReentrantLock import kotlin.concurrent.withLock @@ -23,7 +22,7 @@ internal class LinuxTrayManager( private val instanceId: String, private var iconPath: String, private var tooltip: String = "", - private var onLeftClick: (() -> Unit)? = null + private var onLeftClick: (() -> Unit)? = null, ) { companion object { // Ensures only one systray runtime is active at a time @@ -37,7 +36,7 @@ internal class LinuxTrayManager( val isChecked: Boolean = false, val iconPath: String? = null, val onClick: (() -> Unit)? = null, - val subMenuItems: List = emptyList() + val subMenuItems: List = emptyList(), ) private val native = LinuxNativeBridge @@ -66,7 +65,10 @@ internal class LinuxTrayManager( lock.withLock { menuItems.add(menuItem) } } - fun updateMenuItemCheckedState(label: String, isChecked: Boolean) { + fun updateMenuItemCheckedState( + label: String, + isChecked: Boolean, + ) { var fallback = false lock.withLock { val idx = menuItems.indexOfFirst { it.text == label } @@ -77,9 +79,14 @@ internal class LinuxTrayManager( val id = idByTitle[label] if (id != null && trayHandle != 0L) { try { - if (isChecked) native.nativeItemCheck(trayHandle, id) - else native.nativeItemUncheck(trayHandle, id) - } catch (_: Throwable) { fallback = true } + if (isChecked) { + native.nativeItemCheck(trayHandle, id) + } else { + native.nativeItemUncheck(trayHandle, id) + } + } catch (_: Throwable) { + fallback = true + } } else { fallback = true } @@ -91,7 +98,7 @@ internal class LinuxTrayManager( newIconPath: String, newTooltip: String, newOnLeftClick: (() -> Unit)?, - newMenuItems: List? + newMenuItems: List?, ) { val iconChanged: Boolean val tooltipChanged: Boolean @@ -109,8 +116,10 @@ internal class LinuxTrayManager( } if (iconChanged) setIconFromFileSafe(iconPath) - if (tooltipChanged) runCatching { native.nativeSetTooltip(trayHandle, tooltip) } - .onFailure { e -> warnln { "[LinuxTrayManager] Failed to set tooltip: ${e.message}" } } + if (tooltipChanged) { + runCatching { native.nativeSetTooltip(trayHandle, tooltip) } + .onFailure { e -> warnln { "[LinuxTrayManager] Failed to set tooltip: ${e.message}" } } + } if (newMenuItems != null) rebuildMenu() } @@ -137,8 +146,9 @@ internal class LinuxTrayManager( val readyLatch = CountDownLatch(1) // Read initial icon bytes - val iconBytes = runCatching { File(iconPath).takeIf { it.isFile }?.readBytes() } - .getOrNull() + val iconBytes = + runCatching { File(iconPath).takeIf { it.isFile }?.readBytes() } + .getOrNull() // Create native tray trayHandle = native.nativeCreate(iconBytes, tooltip) @@ -151,32 +161,41 @@ internal class LinuxTrayManager( runCatching { native.nativeSetTitle(trayHandle, tooltip) } // Set click callback - native.nativeSetClickCallback(trayHandle, JniRunnable { - try { - val xy = IntArray(2) - native.nativeGetLastClickXY(trayHandle, xy) - TrayClickTracker.updateClickPosition(xy[0], xy[1]) - } catch (_: Throwable) { } - onLeftClick?.invoke() - }) + native.nativeSetClickCallback( + trayHandle, + JniRunnable { + try { + val xy = IntArray(2) + native.nativeGetLastClickXY(trayHandle, xy) + TrayClickTracker.updateClickPosition(xy[0], xy[1]) + } catch (_: Throwable) { + } + onLeftClick?.invoke() + }, + ) // Build menu before starting the loop rebuildMenu() // Start event loop in daemon thread - loopThread = Thread({ - try { - readyLatch.countDown() - native.nativeRun(trayHandle) - } catch (t: Throwable) { - errorln { "[LinuxTrayManager] loop error: $t" } + loopThread = + Thread({ + try { + readyLatch.countDown() + native.nativeRun(trayHandle) + } catch (t: Throwable) { + errorln { "[LinuxTrayManager] loop error: $t" } + } + }, "LinuxTray-Loop").apply { + isDaemon = true + start() } - }, "LinuxTray-Loop").apply { - isDaemon = true - start() - } - try { readyLatch.await() } catch (_: InterruptedException) { Thread.currentThread().interrupt() } + try { + readyLatch.await() + } catch (_: InterruptedException) { + Thread.currentThread().interrupt() + } started = true } catch (t: Throwable) { @@ -185,16 +204,31 @@ internal class LinuxTrayManager( if (!started) { running.set(false) if (trayHandle != 0L) { - try { native.nativeQuit(trayHandle) } catch (_: Throwable) {} - try { loopThread?.join(500) } catch (_: Throwable) {} - try { native.nativeDestroy(trayHandle) } catch (_: Throwable) {} + try { + native.nativeQuit(trayHandle) + } catch (_: Throwable) { + } + try { + loopThread?.join(500) + } catch (_: Throwable) { + } + try { + native.nativeDestroy(trayHandle) + } catch (_: Throwable) { + } trayHandle = 0L } loopThread = null - try { shutdownHook?.let { Runtime.getRuntime().removeShutdownHook(it) } } catch (_: Throwable) {} + try { + shutdownHook?.let { Runtime.getRuntime().removeShutdownHook(it) } + } catch (_: Throwable) { + } shutdownHook = null if (permitHeld.compareAndSet(true, false)) { - try { lifecyclePermit.release() } catch (_: Throwable) {} + try { + lifecyclePermit.release() + } catch (_: Throwable) { + } } } } @@ -204,7 +238,8 @@ internal class LinuxTrayManager( if (!running.compareAndSet(true, false)) return try { if (trayHandle != 0L) native.nativeQuit(trayHandle) - } catch (_: Throwable) {} + } catch (_: Throwable) { + } try { loopThread?.join(500) @@ -217,16 +252,23 @@ internal class LinuxTrayManager( try { if (trayHandle != 0L) native.nativeDestroy(trayHandle) - } catch (_: Throwable) {} + } catch (_: Throwable) { + } trayHandle = 0L loopThread = null idByTitle.clear() actionById.clear() - try { shutdownHook?.let { Runtime.getRuntime().removeShutdownHook(it) } } catch (_: Throwable) {} + try { + shutdownHook?.let { Runtime.getRuntime().removeShutdownHook(it) } + } catch (_: Throwable) { + } shutdownHook = null if (permitHeld.compareAndSet(true, false)) { - try { lifecyclePermit.release() } catch (_: Throwable) {} + try { + lifecyclePermit.release() + } catch (_: Throwable) { + } } } @@ -254,7 +296,10 @@ internal class LinuxTrayManager( effectiveItems.forEach { addMenuItemRecursive(null, it) } } - private fun addMenuItemRecursive(parentId: Int?, item: MenuItem) { + private fun addMenuItemRecursive( + parentId: Int?, + item: MenuItem, + ) { try { if (item.text == "-") { if (parentId == null) { @@ -269,13 +314,20 @@ internal class LinuxTrayManager( return } - val id = if (parentId == null) { - if (item.isCheckable) native.nativeAddMenuItemCheckbox(trayHandle, item.text, null, item.isChecked) - else native.nativeAddMenuItem(trayHandle, item.text, null) - } else { - if (item.isCheckable) native.nativeAddSubMenuItemCheckbox(trayHandle, parentId, item.text, null, item.isChecked) - else native.nativeAddSubMenuItem(trayHandle, parentId, item.text, null) - } + val id = + if (parentId == null) { + if (item.isCheckable) { + native.nativeAddMenuItemCheckbox(trayHandle, item.text, null, item.isChecked) + } else { + native.nativeAddMenuItem(trayHandle, item.text, null) + } + } else { + if (item.isCheckable) { + native.nativeAddSubMenuItemCheckbox(trayHandle, parentId, item.text, null, item.isChecked) + } else { + native.nativeAddSubMenuItem(trayHandle, parentId, item.text, null) + } + } idByTitle[item.text] = id item.onClick?.let { action -> actionById[id] = action @@ -283,8 +335,11 @@ internal class LinuxTrayManager( } // Enable/Disable - if (item.isEnabled) native.nativeItemEnable(trayHandle, id) - else native.nativeItemDisable(trayHandle, id) + if (item.isEnabled) { + native.nativeItemEnable(trayHandle, id) + } else { + native.nativeItemDisable(trayHandle, id) + } // Icon item.iconPath?.let { iconPath -> diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacNativeBridge.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacNativeBridge.kt index 0875f324..1e32d0d9 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacNativeBridge.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacNativeBridge.kt @@ -7,63 +7,123 @@ import com.kdroid.composetray.utils.NativeLibraryLoader * All methods are static JNI calls into MacTrayBridge.m. */ internal object MacNativeBridge { - private const val LIBRARY_NAME = "MacTray" private val loaded = NativeLibraryLoader.load(LIBRARY_NAME, MacNativeBridge::class.java) val isLoaded: Boolean get() = loaded // ── Tray lifecycle ────────────────────────────────────────────────── - @JvmStatic external fun nativeCreateTray(iconPath: String, tooltip: String): Long + @JvmStatic external fun nativeCreateTray( + iconPath: String, + tooltip: String, + ): Long + @JvmStatic external fun nativeFreeTray(handle: Long) - @JvmStatic external fun nativeSetTrayIcon(handle: Long, iconPath: String) - @JvmStatic external fun nativeSetTrayTooltip(handle: Long, tooltip: String) - @JvmStatic external fun nativeSetTrayCallback(handle: Long, callback: Runnable?) - @JvmStatic external fun nativeSetTrayMenu(trayHandle: Long, menuHandle: Long) + + @JvmStatic external fun nativeSetTrayIcon( + handle: Long, + iconPath: String, + ) + + @JvmStatic external fun nativeSetTrayTooltip( + handle: Long, + tooltip: String, + ) + + @JvmStatic external fun nativeSetTrayCallback( + handle: Long, + callback: Runnable?, + ) + + @JvmStatic external fun nativeSetTrayMenu( + trayHandle: Long, + menuHandle: Long, + ) + @JvmStatic external fun nativeClearTrayMenu(trayHandle: Long) + @JvmStatic external fun nativeInitTray(handle: Long): Int + @JvmStatic external fun nativeLoopTray(blocking: Int): Int + @JvmStatic external fun nativeUpdateTray(handle: Long) + @JvmStatic external fun nativeDisposeTray(handle: Long) + @JvmStatic external fun nativeExitTray() // ── Menu items ────────────────────────────────────────────────────── @JvmStatic external fun nativeCreateMenuItems(count: Int): Long + @JvmStatic external fun nativeSetMenuItem( - menuHandle: Long, index: Int, - text: String, iconPath: String?, - disabled: Int, checked: Int + menuHandle: Long, + index: Int, + text: String, + iconPath: String?, + disabled: Int, + checked: Int, + ) + + @JvmStatic external fun nativeSetMenuItemCallback( + menuHandle: Long, + index: Int, + callback: Runnable?, + ) + + @JvmStatic external fun nativeSetMenuItemSubmenu( + menuHandle: Long, + index: Int, + submenuHandle: Long, + ) + + @JvmStatic external fun nativeFreeMenuItems( + menuHandle: Long, + count: Int, ) - @JvmStatic external fun nativeSetMenuItemCallback(menuHandle: Long, index: Int, callback: Runnable?) - @JvmStatic external fun nativeSetMenuItemSubmenu(menuHandle: Long, index: Int, submenuHandle: Long) - @JvmStatic external fun nativeFreeMenuItems(menuHandle: Long, count: Int) // ── Theme ─────────────────────────────────────────────────────────── @JvmStatic external fun nativeSetThemeCallback(callback: ThemeChangeCallback?) + @JvmStatic external fun nativeIsMenuDark(): Int // ── Position ──────────────────────────────────────────────────────── /** Writes [x, y] into outXY. Returns 1 if precise, 0 if fallback. */ @JvmStatic external fun nativeGetStatusItemPosition(outXY: IntArray): Int + @JvmStatic external fun nativeGetStatusItemRegion(): String - @JvmStatic external fun nativeGetStatusItemPositionFor(handle: Long, outXY: IntArray): Int + + @JvmStatic external fun nativeGetStatusItemPositionFor( + handle: Long, + outXY: IntArray, + ): Int + @JvmStatic external fun nativeGetStatusItemRegionFor(handle: Long): String // ── Appearance ────────────────────────────────────────────────────── - @JvmStatic external fun nativeSetIconsForAppearance(handle: Long, lightIcon: String, darkIcon: String) + @JvmStatic external fun nativeSetIconsForAppearance( + handle: Long, + lightIcon: String, + darkIcon: String, + ) // ── Window management ─────────────────────────────────────────────── @JvmStatic external fun nativeShowInDock(): Int + @JvmStatic external fun nativeHideFromDock(): Int + @JvmStatic external fun nativeSetMoveToActiveSpace() + @JvmStatic external fun nativeSetMoveToActiveSpaceForWindow(viewPtr: Long): Int + @JvmStatic external fun nativeIsFloatingWindowOnActiveSpace(): Int + @JvmStatic external fun nativeBringFloatingWindowToFront(): Int + @JvmStatic external fun nativeIsOnActiveSpaceForView(viewPtr: Long): Int // ── Mouse ─────────────────────────────────────────────────────────── diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacOSMenuBarThemeDetector.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacOSMenuBarThemeDetector.kt index 291579c2..0404a9d9 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacOSMenuBarThemeDetector.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacOSMenuBarThemeDetector.kt @@ -1,16 +1,16 @@ package com.kdroid.composetray.lib.mac -import java.util.function.Consumer import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.Executors +import java.util.function.Consumer object MacOSMenuBarThemeDetector { - private val listeners: MutableSet> = ConcurrentHashMap.newKeySet() - private val callbackExecutor = Executors.newSingleThreadExecutor { r -> - Thread(r, "MacOS MenuBar Theme Detector Thread").apply { isDaemon = true } - } + private val callbackExecutor = + Executors.newSingleThreadExecutor { r -> + Thread(r, "MacOS MenuBar Theme Detector Thread").apply { isDaemon = true } + } private class ThemeCallback : MacNativeBridge.ThemeChangeCallback { override fun onThemeChanged(isDark: Int) { diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacOsWindowManager.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacOSWindowManager.kt similarity index 99% rename from src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacOsWindowManager.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacOSWindowManager.kt index bfdb93bc..880d684d 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacOsWindowManager.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacOSWindowManager.kt @@ -5,7 +5,6 @@ import io.github.kdroidfilter.platformtools.OperatingSystem import io.github.kdroidfilter.platformtools.getOperatingSystem class MacOSWindowManager { - // Detect platform once private val isMacOs: Boolean = getOperatingSystem() == OperatingSystem.MACOS diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacOutsideClickWatcher.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacOutsideClickWatcher.kt index 757c548e..81133a49 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacOutsideClickWatcher.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacOutsideClickWatcher.kt @@ -17,20 +17,20 @@ import java.util.concurrent.TimeUnit */ class MacOutsideClickWatcher( private val windowSupplier: () -> Window?, - private val onOutsideClick: () -> Unit + private val onOutsideClick: () -> Unit, ) : AutoCloseable { - private var scheduler: ScheduledExecutorService? = null private var prevLeft = false fun start() { if (getOperatingSystem() != OperatingSystem.MACOS) return if (scheduler != null) return - scheduler = Executors.newSingleThreadScheduledExecutor { r -> - Thread(r, "MacOutsideClickWatcher").apply { isDaemon = true } - }.also { exec -> - exec.scheduleAtFixedRate({ pollOnce() }, 0, 16, TimeUnit.MILLISECONDS) - } + scheduler = + Executors.newSingleThreadScheduledExecutor { r -> + Thread(r, "MacOutsideClickWatcher").apply { isDaemon = true } + }.also { exec -> + exec.scheduleAtFixedRate({ pollOnce() }, 0, 16, TimeUnit.MILLISECONDS) + } } private fun pollOnce() { @@ -40,12 +40,22 @@ class MacOutsideClickWatcher( if (left && left != prevLeft) { val win = windowSupplier.invoke() if (win != null && win.isShowing) { - val pointer = try { MouseInfo.getPointerInfo() } catch (_: Throwable) { null } + val pointer = + try { + MouseInfo.getPointerInfo() + } catch (_: Throwable) { + null + } val loc = pointer?.location if (loc != null) { val px = loc.x val py = loc.y - val winLoc = try { win.locationOnScreen } catch (_: Throwable) { null } + val winLoc = + try { + win.locationOnScreen + } catch (_: Throwable) { + null + } if (winLoc != null) { val wx = winLoc.x val wy = winLoc.y @@ -69,7 +79,10 @@ class MacOutsideClickWatcher( fun stop() = close() override fun close() { - try { scheduler?.shutdownNow() } catch (_: Throwable) {} + try { + scheduler?.shutdownNow() + } catch (_: Throwable) { + } scheduler = null } } diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacTrayManager.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacTrayManager.kt index ef2ef94c..f46aaea9 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacTrayManager.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/lib/mac/MacTrayManager.kt @@ -1,7 +1,9 @@ package com.kdroid.composetray.lib.mac import androidx.compose.runtime.mutableStateOf -import kotlinx.coroutines.* +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.cancel import kotlinx.coroutines.launch import java.util.concurrent.CountDownLatch @@ -12,7 +14,7 @@ import kotlin.concurrent.withLock internal class MacTrayManager( private var iconPath: String, private var tooltip: String = "", - onLeftClick: (() -> Unit)? = null + onLeftClick: (() -> Unit)? = null, ) { private var trayHandle: Long = 0L private var menuHandle: Long = 0L @@ -40,7 +42,7 @@ internal class MacTrayManager( val isCheckable: Boolean = false, val isChecked: Boolean = false, val onClick: (() -> Unit)? = null, - val subMenuItems: List = emptyList() + val subMenuItems: List = emptyList(), ) fun addMenuItem(menuItem: MenuItem) { @@ -50,7 +52,10 @@ internal class MacTrayManager( } // Update a menu item's checked state - fun updateMenuItemCheckedState(label: String, isChecked: Boolean) { + fun updateMenuItemCheckedState( + label: String, + isChecked: Boolean, + ) { lock.withLock { val index = menuItems.indexOfFirst { it.text == label } if (index != -1) { @@ -62,7 +67,12 @@ internal class MacTrayManager( } // Update the tray with new properties and menu items - fun update(newIconPath: String, newTooltip: String, newOnLeftClick: (() -> Unit)?, newMenuItems: List? = null) { + fun update( + newIconPath: String, + newTooltip: String, + newOnLeftClick: (() -> Unit)?, + newMenuItems: List? = null, + ) { lock.withLock { if (!running.get() || trayHandle == 0L) return @@ -140,43 +150,44 @@ internal class MacTrayManager( ioScope = CoroutineScope(Dispatchers.IO + SupervisorJob()) // Create and start the tray thread - trayThread = Thread { - try { - // Create tray structure via JNI - trayHandle = MacNativeBridge.nativeCreateTray(iconPath, tooltip) - if (trayHandle == 0L) { - throw IllegalStateException("Failed to allocate native tray struct") - } + trayThread = + Thread { + try { + // Create tray structure via JNI + trayHandle = MacNativeBridge.nativeCreateTray(iconPath, tooltip) + if (trayHandle == 0L) { + throw IllegalStateException("Failed to allocate native tray struct") + } - initializeOnLeftClickCallback() - initializeTrayMenu() + initializeOnLeftClickCallback() + initializeTrayMenu() - val initResult = MacNativeBridge.nativeInitTray(trayHandle) - if (initResult != 0) { - throw IllegalStateException("Failed to initialize tray: $initResult") - } + val initResult = MacNativeBridge.nativeInitTray(trayHandle) + if (initResult != 0) { + throw IllegalStateException("Failed to initialize tray: $initResult") + } - // Signal that initialization is complete - initLatch.countDown() + // Signal that initialization is complete + initLatch.countDown() - // Run the event loop - while (running.get()) { - val result = MacNativeBridge.nativeLoopTray(0) - if (result != 0) { - break + // Run the event loop + while (running.get()) { + val result = MacNativeBridge.nativeLoopTray(0) + if (result != 0) { + break + } + Thread.sleep(100) } - Thread.sleep(100) + } catch (e: Exception) { + e.printStackTrace() + } finally { + cleanupTray() } - } catch (e: Exception) { - e.printStackTrace() - } finally { - cleanupTray() + }.apply { + name = "MacTray-Thread" + isDaemon = true + start() } - }.apply { - name = "MacTray-Thread" - isDaemon = true - start() - } // Wait for initialization to complete try { @@ -192,13 +203,16 @@ internal class MacTrayManager( val onClick = onLeftClickCallback.value if (onClick != null) { - MacNativeBridge.nativeSetTrayCallback(trayHandle, Runnable { - mainScope?.launch { - ioScope?.launch { - onClick() + MacNativeBridge.nativeSetTrayCallback( + trayHandle, + Runnable { + mainScope?.launch { + ioScope?.launch { + onClick() + } } - } - }) + }, + ) } else { MacNativeBridge.nativeSetTrayCallback(trayHandle, null) } @@ -224,24 +238,33 @@ internal class MacTrayManager( MacNativeBridge.nativeSetTrayMenu(trayHandle, handle) } - private fun initializeNativeMenuItem(parentHandle: Long, index: Int, menuItem: MenuItem) { + private fun initializeNativeMenuItem( + parentHandle: Long, + index: Int, + menuItem: MenuItem, + ) { MacNativeBridge.nativeSetMenuItem( - parentHandle, index, + parentHandle, + index, menuItem.text, menuItem.icon, if (menuItem.isEnabled) 0 else 1, - if (menuItem.isChecked) 1 else 0 + if (menuItem.isChecked) 1 else 0, ) menuItem.onClick?.let { onClick -> - MacNativeBridge.nativeSetMenuItemCallback(parentHandle, index, Runnable { - if (!running.get()) return@Runnable - mainScope?.launch { - ioScope?.launch { - onClick() + MacNativeBridge.nativeSetMenuItemCallback( + parentHandle, + index, + Runnable { + if (!running.get()) return@Runnable + mainScope?.launch { + ioScope?.launch { + onClick() + } } - } - }) + }, + ) } if (menuItem.subMenuItems.isNotEmpty()) { @@ -307,7 +330,10 @@ internal class MacTrayManager( return trayHandle } - fun setAppearanceIcons(lightIconPath: String, darkIconPath: String) { + fun setAppearanceIcons( + lightIconPath: String, + darkIconPath: String, + ) { lock.withLock { if (trayHandle == 0L) return MacNativeBridge.nativeSetIconsForAppearance(trayHandle, lightIconPath, darkIconPath) diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsNativeBridge.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsNativeBridge.kt index 1b15190e..1696aa4c 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsNativeBridge.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsNativeBridge.kt @@ -8,28 +8,53 @@ import com.kdroid.composetray.utils.NativeLibraryLoader * Follows the same patterns as MacNativeBridge and LinuxNativeBridge. */ internal object WindowsNativeBridge { - init { NativeLibraryLoader.load("WinTray", WindowsNativeBridge::class.java) } // -- Tray lifecycle -- - @JvmStatic external fun nativeCreateTray(iconPath: String, tooltip: String): Long + @JvmStatic external fun nativeCreateTray( + iconPath: String, + tooltip: String, + ): Long + @JvmStatic external fun nativeFreeTray(handle: Long) - @JvmStatic external fun nativeSetTrayIcon(handle: Long, iconPath: String) - @JvmStatic external fun nativeSetTrayTooltip(handle: Long, tooltip: String) - @JvmStatic external fun nativeSetTrayCallback(handle: Long, callback: Runnable?) - @JvmStatic external fun nativeSetTrayMenu(trayHandle: Long, menuHandle: Long) + + @JvmStatic external fun nativeSetTrayIcon( + handle: Long, + iconPath: String, + ) + + @JvmStatic external fun nativeSetTrayTooltip( + handle: Long, + tooltip: String, + ) + + @JvmStatic external fun nativeSetTrayCallback( + handle: Long, + callback: Runnable?, + ) + + @JvmStatic external fun nativeSetTrayMenu( + trayHandle: Long, + menuHandle: Long, + ) + @JvmStatic external fun nativeClearTrayMenu(trayHandle: Long) + @JvmStatic external fun nativeInitTray(handle: Long): Int + @JvmStatic external fun nativeLoopTray(blocking: Int): Int + @JvmStatic external fun nativeUpdateTray(handle: Long) + @JvmStatic external fun nativeExitTray() // -- Menu items -- @JvmStatic external fun nativeCreateMenuItems(count: Int): Long + @JvmStatic external fun nativeSetMenuItem( menuHandle: Long, index: Int, @@ -38,20 +63,38 @@ internal object WindowsNativeBridge { disabled: Int, checked: Int, ) - @JvmStatic external fun nativeSetMenuItemCallback(menuHandle: Long, index: Int, callback: Runnable?) - @JvmStatic external fun nativeSetMenuItemSubmenu(menuHandle: Long, index: Int, submenuHandle: Long) - @JvmStatic external fun nativeFreeMenuItems(menuHandle: Long, count: Int) + + @JvmStatic external fun nativeSetMenuItemCallback( + menuHandle: Long, + index: Int, + callback: Runnable?, + ) + + @JvmStatic external fun nativeSetMenuItemSubmenu( + menuHandle: Long, + index: Int, + submenuHandle: Long, + ) + + @JvmStatic external fun nativeFreeMenuItems( + menuHandle: Long, + count: Int, + ) // -- Position -- /** Writes [x, y] into outXY. Returns non-zero if precise. */ @JvmStatic external fun nativeGetNotificationIconsPosition(outXY: IntArray): Int + @JvmStatic external fun nativeGetNotificationIconsRegion(): String? // -- Mouse hook (for outside-click detection) -- @JvmStatic external fun nativeInstallMouseHook(callback: Runnable): Long + @JvmStatic external fun nativeRunMouseHookLoop(hookId: Long) + @JvmStatic external fun nativeStopMouseHook(hookId: Long) + @JvmStatic external fun nativeGetLastMouseHookClick(outXY: IntArray) } diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsOutsideClickWatcher.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsOutsideClickWatcher.kt index d2dd27c1..c0998f81 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsOutsideClickWatcher.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsOutsideClickWatcher.kt @@ -19,8 +19,8 @@ class WindowsOutsideClickWatcher( private val onOutsideClick: () -> Unit, private val ignorePointPredicate: ((x: Int, y: Int) -> Boolean)? = null, ) : AutoCloseable { - @Volatile private var hookThread: Thread? = null + @Volatile private var hookId: Long = 0L private val stopping = AtomicBoolean(false) @@ -31,52 +31,62 @@ class WindowsOutsideClickWatcher( if (hookThread != null) return stopping.set(false) - hookThread = Thread({ - val callback = Runnable { - try { - val xy = IntArray(2) - WindowsNativeBridge.nativeGetLastMouseHookClick(xy) - val px = xy[0] - val py = xy[1] + hookThread = + Thread({ + val callback = + Runnable { + try { + val xy = IntArray(2) + WindowsNativeBridge.nativeGetLastMouseHookClick(xy) + val px = xy[0] + val py = xy[1] - val win = windowSupplier() - if (win != null && win.isShowing) { - val winLoc = try { win.locationOnScreen } catch (_: Throwable) { null } - if (winLoc != null) { - // Get the graphics configuration to determine the DPI scale - val scale = try { - win.graphicsConfiguration?.defaultTransform?.scaleX ?: 1.0 - } catch (_: Throwable) { 1.0 } + val win = windowSupplier() + if (win != null && win.isShowing) { + val winLoc = + try { + win.locationOnScreen + } catch (_: Throwable) { + null + } + if (winLoc != null) { + // Get the graphics configuration to determine the DPI scale + val scale = + try { + win.graphicsConfiguration?.defaultTransform?.scaleX ?: 1.0 + } catch (_: Throwable) { + 1.0 + } - // Convert window bounds from logical to physical pixels - val wx = (winLoc.x * scale).toInt() - val wy = (winLoc.y * scale).toInt() - val ww = (win.width * scale).toInt() - val wh = (win.height * scale).toInt() + // Convert window bounds from logical to physical pixels + val wx = (winLoc.x * scale).toInt() + val wy = (winLoc.y * scale).toInt() + val ww = (win.width * scale).toInt() + val wh = (win.height * scale).toInt() - val insideWindow = px in wx until (wx + ww) && py in wy until (wy + wh) - val ignored = ignorePointPredicate?.invoke(px, py) == true + val insideWindow = px in wx until (wx + ww) && py in wy until (wy + wh) + val ignored = ignorePointPredicate?.invoke(px, py) == true - if (!insideWindow && !ignored) { - // Let caller decide EDT marshaling. - onOutsideClick() + if (!insideWindow && !ignored) { + // Let caller decide EDT marshaling. + onOutsideClick() + } + } } + } catch (_: Throwable) { + // Never crash the hook callback } } - } catch (_: Throwable) { - // Never crash the hook callback - } - } - hookId = WindowsNativeBridge.nativeInstallMouseHook(callback) - if (hookId == 0L) return@Thread + hookId = WindowsNativeBridge.nativeInstallMouseHook(callback) + if (hookId == 0L) return@Thread - // Block on message loop until stopped - WindowsNativeBridge.nativeRunMouseHookLoop(hookId) - }, "WindowsOutsideClickWatcher-LL").apply { - isDaemon = true - start() - } + // Block on message loop until stopped + WindowsNativeBridge.nativeRunMouseHookLoop(hookId) + }, "WindowsOutsideClickWatcher-LL").apply { + isDaemon = true + start() + } } } @@ -92,7 +102,8 @@ class WindowsOutsideClickWatcher( if (id != 0L) { try { WindowsNativeBridge.nativeStopMouseHook(id) - } catch (_: Throwable) { } + } catch (_: Throwable) { + } hookId = 0L } diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsTrayManager.kt b/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsTrayManager.kt index f76f09bf..e4e112ad 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsTrayManager.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/lib/windows/WindowsTrayManager.kt @@ -80,51 +80,52 @@ internal class WindowsTrayManager( ioScope = CoroutineScope(Dispatchers.IO + SupervisorJob()) // Create and start the tray thread - trayThread = Thread { - try { - log("Tray thread started") + trayThread = + Thread { + try { + log("Tray thread started") - // Create tray via JNI - val handle = WindowsNativeBridge.nativeCreateTray(iconPath, tooltip) - if (handle == 0L) throw RuntimeException("Failed to create tray") - trayHandle = handle + // Create tray via JNI + val handle = WindowsNativeBridge.nativeCreateTray(iconPath, tooltip) + if (handle == 0L) throw RuntimeException("Failed to create tray") + trayHandle = handle - // Set up callbacks and menu on this thread - setupLeftClickCallback(handle) - setupMenu(handle, menuItems) + // Set up callbacks and menu on this thread + setupLeftClickCallback(handle) + setupMenu(handle, menuItems) - // Initialize the tray on this thread - log("Calling nativeInitTray() on tray thread") - val initResult = WindowsNativeBridge.nativeInitTray(handle) - log("nativeInitTray() returned: $initResult") + // Initialize the tray on this thread + log("Calling nativeInitTray() on tray thread") + val initResult = WindowsNativeBridge.nativeInitTray(handle) + log("nativeInitTray() returned: $initResult") - if (initResult != 0) { - throw RuntimeException("Failed to initialize tray: $initResult") - } + if (initResult != 0) { + throw RuntimeException("Failed to initialize tray: $initResult") + } - initialized.set(true) + initialized.set(true) - // Signal that initialization is complete before entering the loop - initLatch.countDown() + // Signal that initialization is complete before entering the loop + initLatch.countDown() - // Run the blocking message loop on this thread - runMessageLoop() - } catch (e: Throwable) { - log("Error in tray thread: ${e.message}") - e.printStackTrace() - } finally { - // Safety net: release latch in case an Error prevented the - // countDown above from being reached. CountDownLatch.countDown() - // is a no-op when the count is already 0, so calling it twice - // on the success path is harmless. - initLatch.countDown() - cleanupTray() + // Run the blocking message loop on this thread + runMessageLoop() + } catch (e: Throwable) { + log("Error in tray thread: ${e.message}") + e.printStackTrace() + } finally { + // Safety net: release latch in case an Error prevented the + // countDown above from being reached. CountDownLatch.countDown() + // is a no-op when the count is already 0, so calling it twice + // on the success path is harmless. + initLatch.countDown() + cleanupTray() + } + }.apply { + name = "WindowsTray-Thread" + isDaemon = false // Don't make it daemon so it can clean up properly + start() } - }.apply { - name = "WindowsTray-Thread" - isDaemon = false // Don't make it daemon so it can clean up properly - start() - } // Wait for initialization to complete try { @@ -252,10 +253,17 @@ internal class WindowsTrayManager( val logicalY = (outXY[1] / scale).toInt() val screen = java.awt.Toolkit.getDefaultToolkit().screenSize - log("DPI scale=$scale, logicalX=$logicalX, logicalY=$logicalY, screenW=${screen.width}, screenH=${screen.height}") - val corner = convertPositionToCorner( - logicalX, logicalY, screen.width, screen.height, + log( + "DPI scale=$scale, logicalX=$logicalX, logicalY=$logicalY, " + + "screenW=${screen.width}, screenH=${screen.height}", ) + val corner = + convertPositionToCorner( + logicalX, + logicalY, + screen.width, + screen.height, + ) log("Detected corner: $corner") TrayClickTracker.setClickPosition(instanceId, logicalX, logicalY, corner) true @@ -289,7 +297,10 @@ internal class WindowsTrayManager( * @param physicalX X coordinate in physical pixels (optional, uses primary screen if not provided) * @param physicalY Y coordinate in physical pixels (optional, uses primary screen if not provided) */ - private fun getDpiScale(physicalX: Int? = null, physicalY: Int? = null): Double { + private fun getDpiScale( + physicalX: Int? = null, + physicalY: Int? = null, + ): Double { return try { val ge = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment() @@ -300,12 +311,13 @@ internal class WindowsTrayManager( val scale = config.defaultTransform.scaleX // Convert physical bounds to check containment val bounds = config.bounds - val physBounds = java.awt.Rectangle( - (bounds.x * scale).toInt(), - (bounds.y * scale).toInt(), - (bounds.width * scale).toInt(), - (bounds.height * scale).toInt(), - ) + val physBounds = + java.awt.Rectangle( + (bounds.x * scale).toInt(), + (bounds.y * scale).toInt(), + (bounds.width * scale).toInt(), + (bounds.height * scale).toInt(), + ) if (physBounds.contains(physicalX, physicalY)) { return scale } @@ -325,13 +337,14 @@ internal class WindowsTrayManager( } private fun processUpdateQueue() { - val update = synchronized(updateQueueLock) { - if (updateQueue.isNotEmpty()) { - updateQueue.removeAt(0) - } else { - null + val update = + synchronized(updateQueueLock) { + if (updateQueue.isNotEmpty()) { + updateQueue.removeAt(0) + } else { + null + } } - } if (update != null) { log("Processing update from queue") @@ -403,7 +416,10 @@ internal class WindowsTrayManager( } } - private fun setupMenu(handle: Long, menuItems: List) { + private fun setupMenu( + handle: Long, + menuItems: List, + ) { if (menuItems.isEmpty()) { log("No menu items to set up") WindowsNativeBridge.nativeClearTrayMenu(handle) @@ -421,7 +437,8 @@ internal class WindowsTrayManager( menuItems.forEachIndexed { index, item -> WindowsNativeBridge.nativeSetMenuItem( - menuHandle, index, + menuHandle, + index, item.text, item.iconPath, if (item.isEnabled) 0 else 1, @@ -429,25 +446,26 @@ internal class WindowsTrayManager( ) item.onClick?.let { onClick -> - val callback = Runnable { - log("Menu item clicked: ${item.text}") - try { - // Capture precise tray position on the tray thread (per-instance) - safeGetTrayPosition(instanceId) - - if (running.get()) { - // Execute callback in IO scope (like macOS) - mainScope?.launch { - ioScope?.launch { - onClick() + val callback = + Runnable { + log("Menu item clicked: ${item.text}") + try { + // Capture precise tray position on the tray thread (per-instance) + safeGetTrayPosition(instanceId) + + if (running.get()) { + // Execute callback in IO scope (like macOS) + mainScope?.launch { + ioScope?.launch { + onClick() + } } } + } catch (e: Exception) { + log("Error in menu item callback: ${e.message}") + e.printStackTrace() } - } catch (e: Exception) { - log("Error in menu item callback: ${e.message}") - e.printStackTrace() } - } WindowsNativeBridge.nativeSetMenuItemCallback(menuHandle, index, callback) } @@ -489,7 +507,8 @@ internal class WindowsTrayManager( if (handle != 0L) { try { WindowsNativeBridge.nativeFreeTray(handle) - } catch (_: Throwable) { } + } catch (_: Throwable) { + } trayHandle = 0L } diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/menu/api/TrayMenuBuilder.kt b/src/jvmMain/kotlin/com/kdroid/composetray/menu/api/TrayMenuBuilder.kt index a4c1eac2..88753617 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/menu/api/TrayMenuBuilder.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/menu/api/TrayMenuBuilder.kt @@ -6,7 +6,6 @@ import androidx.compose.ui.graphics.painter.Painter import androidx.compose.ui.graphics.vector.ImageVector import com.kdroid.composetray.utils.IconRenderProperties import org.jetbrains.compose.resources.DrawableResource -import org.jetbrains.compose.resources.painterResource /** * Interface for building tray menus in a platform-independent manner. @@ -15,271 +14,279 @@ import org.jetbrains.compose.resources.painterResource * mechanism for disposing resources when the menu is no longer needed. */ interface TrayMenuBuilder { - /** - * Adds an item to the tray menu. - * - * @param label The text label for the menu item. - * @param isEnabled Indicates whether the menu item is enabled. Defaults to true. - * @param onClick Lambda function to be invoked when the menu item is clicked. Defaults to an empty lambda. - */ - fun Item(label: String, isEnabled: Boolean = true, onClick: () -> Unit = {}) + /** + * Adds an item to the tray menu. + * + * @param label The text label for the menu item. + * @param isEnabled Indicates whether the menu item is enabled. Defaults to true. + * @param onClick Lambda function to be invoked when the menu item is clicked. Defaults to an empty lambda. + */ + fun Item( + label: String, + isEnabled: Boolean = true, + onClick: () -> Unit = {}, + ) - /** - * Adds an item to the tray menu with a Composable icon. - * - * @param label The text label for the menu item. - * @param iconContent A Composable function that defines the icon. - * @param iconRenderProperties Properties for rendering the icon. Defaults to 16x16 for menu items. - * @param isEnabled Indicates whether the menu item is enabled. Defaults to true. - * @param onClick Lambda function to be invoked when the menu item is clicked. Defaults to an empty lambda. - */ - fun Item( - label: String, - iconContent: @Composable () -> Unit, - iconRenderProperties: IconRenderProperties = IconRenderProperties.forMenuItem(), - isEnabled: Boolean = true, - onClick: () -> Unit = {} - ) + /** + * Adds an item to the tray menu with a Composable icon. + * + * @param label The text label for the menu item. + * @param iconContent A Composable function that defines the icon. + * @param iconRenderProperties Properties for rendering the icon. Defaults to 16x16 for menu items. + * @param isEnabled Indicates whether the menu item is enabled. Defaults to true. + * @param onClick Lambda function to be invoked when the menu item is clicked. Defaults to an empty lambda. + */ + fun Item( + label: String, + iconContent: @Composable () -> Unit, + iconRenderProperties: IconRenderProperties = IconRenderProperties.forMenuItem(), + isEnabled: Boolean = true, + onClick: () -> Unit = {}, + ) - /** - * Adds an item to the tray menu with an ImageVector icon. - * - * @param label The text label for the menu item. - * @param icon The ImageVector to display as icon. - * @param iconTint Optional tint color for the icon. If null, adapts to menu theme. - * @param iconRenderProperties Properties for rendering the icon. Defaults to 16x16 for menu items. - * @param isEnabled Indicates whether the menu item is enabled. Defaults to true. - * @param onClick Lambda function to be invoked when the menu item is clicked. Defaults to an empty lambda. - */ - fun Item( - label: String, - icon: ImageVector, - iconTint: Color? = null, - iconRenderProperties: IconRenderProperties = IconRenderProperties.forMenuItem(), - isEnabled: Boolean = true, - onClick: () -> Unit = {} - ) + /** + * Adds an item to the tray menu with an ImageVector icon. + * + * @param label The text label for the menu item. + * @param icon The ImageVector to display as icon. + * @param iconTint Optional tint color for the icon. If null, adapts to menu theme. + * @param iconRenderProperties Properties for rendering the icon. Defaults to 16x16 for menu items. + * @param isEnabled Indicates whether the menu item is enabled. Defaults to true. + * @param onClick Lambda function to be invoked when the menu item is clicked. Defaults to an empty lambda. + */ + fun Item( + label: String, + icon: ImageVector, + iconTint: Color? = null, + iconRenderProperties: IconRenderProperties = IconRenderProperties.forMenuItem(), + isEnabled: Boolean = true, + onClick: () -> Unit = {}, + ) - /** - * Adds an item to the tray menu with a Painter icon. - * - * @param label The text label for the menu item. - * @param icon The Painter to display as icon. - * @param iconRenderProperties Properties for rendering the icon. Defaults to 16x16 for menu items. - * @param isEnabled Indicates whether the menu item is enabled. Defaults to true. - * @param onClick Lambda function to be invoked when the menu item is clicked. Defaults to an empty lambda. - */ - fun Item( - label: String, - icon: Painter, - iconRenderProperties: IconRenderProperties = IconRenderProperties.forMenuItem(), - isEnabled: Boolean = true, - onClick: () -> Unit = {} - ) + /** + * Adds an item to the tray menu with a Painter icon. + * + * @param label The text label for the menu item. + * @param icon The Painter to display as icon. + * @param iconRenderProperties Properties for rendering the icon. Defaults to 16x16 for menu items. + * @param isEnabled Indicates whether the menu item is enabled. Defaults to true. + * @param onClick Lambda function to be invoked when the menu item is clicked. Defaults to an empty lambda. + */ + fun Item( + label: String, + icon: Painter, + iconRenderProperties: IconRenderProperties = IconRenderProperties.forMenuItem(), + isEnabled: Boolean = true, + onClick: () -> Unit = {}, + ) - /** - * Adds an item to the tray menu with a DrawableResource icon. - * This allows calling code like: Item(label, icon = Res.drawable.icon, ...) - */ - fun Item( - label: String, - icon: DrawableResource, - iconRenderProperties: IconRenderProperties = IconRenderProperties.forMenuItem(), - isEnabled: Boolean = true, - onClick: () -> Unit = {} - ) + /** + * Adds an item to the tray menu with a DrawableResource icon. + * This allows calling code like: Item(label, icon = Res.drawable.icon, ...) + */ + fun Item( + label: String, + icon: DrawableResource, + iconRenderProperties: IconRenderProperties = IconRenderProperties.forMenuItem(), + isEnabled: Boolean = true, + onClick: () -> Unit = {}, + ) - /** - * Adds a checkable item to the tray menu. - * This follows Compose's idiomatic pattern for stateful components. - * - * @param label The text label for the checkable menu item. - * @param checked The current checked state of the item. - * @param onCheckedChange A lambda function called when the user toggles the item. The new checked state is passed as a parameter. - * @param isEnabled Determines if the checkable item is enabled. Defaults to true. - */ - fun CheckableItem( - label: String, - checked: Boolean, - onCheckedChange: (Boolean) -> Unit, - isEnabled: Boolean = true - ) + /** + * Adds a checkable item to the tray menu. + * This follows Compose's idiomatic pattern for stateful components. + * + * @param label The text label for the checkable menu item. + * @param checked The current checked state of the item. + * @param onCheckedChange A lambda function called when the user toggles the item. The new checked state is passed as a parameter. + * @param isEnabled Determines if the checkable item is enabled. Defaults to true. + */ + fun CheckableItem( + label: String, + checked: Boolean, + onCheckedChange: (Boolean) -> Unit, + isEnabled: Boolean = true, + ) - /** - * Adds a checkable item to the tray menu with a Composable icon. - * - * @param label The text label for the checkable menu item. - * @param iconContent A Composable function that defines the icon. - * @param iconRenderProperties Properties for rendering the icon. Defaults to 16x16 for menu items. - * @param checked The current checked state of the item. - * @param onCheckedChange A lambda function called when the user toggles the item. - * @param isEnabled Determines if the checkable item is enabled. Defaults to true. - */ - fun CheckableItem( - label: String, - iconContent: @Composable () -> Unit, - iconRenderProperties: IconRenderProperties = IconRenderProperties.forMenuItem(), - checked: Boolean, - onCheckedChange: (Boolean) -> Unit, - isEnabled: Boolean = true - ) + /** + * Adds a checkable item to the tray menu with a Composable icon. + * + * @param label The text label for the checkable menu item. + * @param iconContent A Composable function that defines the icon. + * @param iconRenderProperties Properties for rendering the icon. Defaults to 16x16 for menu items. + * @param checked The current checked state of the item. + * @param onCheckedChange A lambda function called when the user toggles the item. + * @param isEnabled Determines if the checkable item is enabled. Defaults to true. + */ + fun CheckableItem( + label: String, + iconContent: @Composable () -> Unit, + iconRenderProperties: IconRenderProperties = IconRenderProperties.forMenuItem(), + checked: Boolean, + onCheckedChange: (Boolean) -> Unit, + isEnabled: Boolean = true, + ) - /** - * Adds a checkable item to the tray menu with an ImageVector icon. - * - * @param label The text label for the checkable menu item. - * @param icon The ImageVector to display as icon. - * @param iconTint Optional tint color for the icon. If null, adapts to menu theme. - * @param iconRenderProperties Properties for rendering the icon. Defaults to 16x16 for menu items. - * @param checked The current checked state of the item. - * @param onCheckedChange A lambda function called when the user toggles the item. - * @param isEnabled Determines if the checkable item is enabled. Defaults to true. - */ - fun CheckableItem( - label: String, - icon: ImageVector, - iconTint: Color? = null, - iconRenderProperties: IconRenderProperties = IconRenderProperties.forMenuItem(), - checked: Boolean, - onCheckedChange: (Boolean) -> Unit, - isEnabled: Boolean = true - ) + /** + * Adds a checkable item to the tray menu with an ImageVector icon. + * + * @param label The text label for the checkable menu item. + * @param icon The ImageVector to display as icon. + * @param iconTint Optional tint color for the icon. If null, adapts to menu theme. + * @param iconRenderProperties Properties for rendering the icon. Defaults to 16x16 for menu items. + * @param checked The current checked state of the item. + * @param onCheckedChange A lambda function called when the user toggles the item. + * @param isEnabled Determines if the checkable item is enabled. Defaults to true. + */ + fun CheckableItem( + label: String, + icon: ImageVector, + iconTint: Color? = null, + iconRenderProperties: IconRenderProperties = IconRenderProperties.forMenuItem(), + checked: Boolean, + onCheckedChange: (Boolean) -> Unit, + isEnabled: Boolean = true, + ) - /** - * Adds a checkable item to the tray menu with a Painter icon. - * - * @param label The text label for the checkable menu item. - * @param icon The Painter to display as icon. - * @param iconRenderProperties Properties for rendering the icon. Defaults to 16x16 for menu items. - * @param checked The current checked state of the item. - * @param onCheckedChange A lambda function called when the user toggles the item. - * @param isEnabled Determines if the checkable item is enabled. Defaults to true. - */ - fun CheckableItem( - label: String, - icon: Painter, - iconRenderProperties: IconRenderProperties = IconRenderProperties.forMenuItem(), - checked: Boolean, - onCheckedChange: (Boolean) -> Unit, - isEnabled: Boolean = true - ) + /** + * Adds a checkable item to the tray menu with a Painter icon. + * + * @param label The text label for the checkable menu item. + * @param icon The Painter to display as icon. + * @param iconRenderProperties Properties for rendering the icon. Defaults to 16x16 for menu items. + * @param checked The current checked state of the item. + * @param onCheckedChange A lambda function called when the user toggles the item. + * @param isEnabled Determines if the checkable item is enabled. Defaults to true. + */ + fun CheckableItem( + label: String, + icon: Painter, + iconRenderProperties: IconRenderProperties = IconRenderProperties.forMenuItem(), + checked: Boolean, + onCheckedChange: (Boolean) -> Unit, + isEnabled: Boolean = true, + ) - /** - * Adds a checkable item to the tray menu with a DrawableResource icon. - * This allows calling code like: CheckableItem(label, icon = Res.drawable.icon, checked = ..., ...) - */ - fun CheckableItem( - label: String, - icon: DrawableResource, - iconRenderProperties: IconRenderProperties = IconRenderProperties.forMenuItem(), - checked: Boolean, - onCheckedChange: (Boolean) -> Unit, - isEnabled: Boolean = true - ) + /** + * Adds a checkable item to the tray menu with a DrawableResource icon. + * This allows calling code like: CheckableItem(label, icon = Res.drawable.icon, checked = ..., ...) + */ + fun CheckableItem( + label: String, + icon: DrawableResource, + iconRenderProperties: IconRenderProperties = IconRenderProperties.forMenuItem(), + checked: Boolean, + onCheckedChange: (Boolean) -> Unit, + isEnabled: Boolean = true, + ) - /** - * Adds a checkable item to the tray menu with the legacy API. - * @deprecated Use the new API with separate checked and onCheckedChange parameters for better Compose idiomaticity - */ - @Deprecated( - message = "Use CheckableItem with separate checked and onCheckedChange parameters", - replaceWith = ReplaceWith("CheckableItem(label, checked, onCheckedChange, isEnabled)") - ) - fun CheckableItem( - label: String, - checked: Boolean = false, - isEnabled: Boolean = true, - onToggle: (Boolean) -> Unit - ) { - // Delegate to the new API - CheckableItem(label, checked, onToggle, isEnabled) - } + /** + * Adds a checkable item to the tray menu with the legacy API. + * @deprecated Use the new API with separate checked and onCheckedChange parameters for better Compose idiomaticity + */ + @Deprecated( + message = "Use CheckableItem with separate checked and onCheckedChange parameters", + replaceWith = ReplaceWith("CheckableItem(label, checked, onCheckedChange, isEnabled)"), + ) + fun CheckableItem( + label: String, + checked: Boolean = false, + isEnabled: Boolean = true, + onToggle: (Boolean) -> Unit, + ) { + // Delegate to the new API + CheckableItem(label, checked, onToggle, isEnabled) + } - /** - * Adds a submenu to the tray menu. - * - * @param label The text label for the submenu. - * @param isEnabled Indicates whether the submenu is enabled. Defaults to true. - * @param submenuContent A lambda function defining the contents of the submenu. Can be null. - */ - fun SubMenu(label: String, isEnabled: Boolean = true, submenuContent: (TrayMenuBuilder.() -> Unit)?) + /** + * Adds a submenu to the tray menu. + * + * @param label The text label for the submenu. + * @param isEnabled Indicates whether the submenu is enabled. Defaults to true. + * @param submenuContent A lambda function defining the contents of the submenu. Can be null. + */ + fun SubMenu( + label: String, + isEnabled: Boolean = true, + submenuContent: (TrayMenuBuilder.() -> Unit)?, + ) - /** - * Adds a submenu to the tray menu with a Composable icon. - * - * @param label The text label for the submenu. - * @param iconContent A Composable function that defines the icon. - * @param iconRenderProperties Properties for rendering the icon. Defaults to 16x16 for menu items. - * @param isEnabled Indicates whether the submenu is enabled. Defaults to true. - * @param submenuContent A lambda function defining the contents of the submenu. Can be null. - */ - fun SubMenu( - label: String, - iconContent: @Composable () -> Unit, - iconRenderProperties: IconRenderProperties = IconRenderProperties.forMenuItem(), - isEnabled: Boolean = true, - submenuContent: (TrayMenuBuilder.() -> Unit)? - ) + /** + * Adds a submenu to the tray menu with a Composable icon. + * + * @param label The text label for the submenu. + * @param iconContent A Composable function that defines the icon. + * @param iconRenderProperties Properties for rendering the icon. Defaults to 16x16 for menu items. + * @param isEnabled Indicates whether the submenu is enabled. Defaults to true. + * @param submenuContent A lambda function defining the contents of the submenu. Can be null. + */ + fun SubMenu( + label: String, + iconContent: @Composable () -> Unit, + iconRenderProperties: IconRenderProperties = IconRenderProperties.forMenuItem(), + isEnabled: Boolean = true, + submenuContent: (TrayMenuBuilder.() -> Unit)?, + ) - /** - * Adds a submenu to the tray menu with an ImageVector icon. - * - * @param label The text label for the submenu. - * @param icon The ImageVector to display as icon. - * @param iconTint Optional tint color for the icon. If null, adapts to menu theme. - * @param iconRenderProperties Properties for rendering the icon. Defaults to 16x16 for menu items. - * @param isEnabled Indicates whether the submenu is enabled. Defaults to true. - * @param submenuContent A lambda function defining the contents of the submenu. Can be null. - */ - fun SubMenu( - label: String, - icon: ImageVector, - iconTint: Color? = null, - iconRenderProperties: IconRenderProperties = IconRenderProperties.forMenuItem(), - isEnabled: Boolean = true, - submenuContent: (TrayMenuBuilder.() -> Unit)? - ) + /** + * Adds a submenu to the tray menu with an ImageVector icon. + * + * @param label The text label for the submenu. + * @param icon The ImageVector to display as icon. + * @param iconTint Optional tint color for the icon. If null, adapts to menu theme. + * @param iconRenderProperties Properties for rendering the icon. Defaults to 16x16 for menu items. + * @param isEnabled Indicates whether the submenu is enabled. Defaults to true. + * @param submenuContent A lambda function defining the contents of the submenu. Can be null. + */ + fun SubMenu( + label: String, + icon: ImageVector, + iconTint: Color? = null, + iconRenderProperties: IconRenderProperties = IconRenderProperties.forMenuItem(), + isEnabled: Boolean = true, + submenuContent: (TrayMenuBuilder.() -> Unit)?, + ) - /** - * Adds a submenu to the tray menu with a Painter icon. - * - * @param label The text label for the submenu. - * @param icon The Painter to display as icon. - * @param iconRenderProperties Properties for rendering the icon. Defaults to 16x16 for menu items. - * @param isEnabled Indicates whether the submenu is enabled. Defaults to true. - * @param submenuContent A lambda function defining the contents of the submenu. Can be null. - */ - fun SubMenu( - label: String, - icon: Painter, - iconRenderProperties: IconRenderProperties = IconRenderProperties.forMenuItem(), - isEnabled: Boolean = true, - submenuContent: (TrayMenuBuilder.() -> Unit)? - ) + /** + * Adds a submenu to the tray menu with a Painter icon. + * + * @param label The text label for the submenu. + * @param icon The Painter to display as icon. + * @param iconRenderProperties Properties for rendering the icon. Defaults to 16x16 for menu items. + * @param isEnabled Indicates whether the submenu is enabled. Defaults to true. + * @param submenuContent A lambda function defining the contents of the submenu. Can be null. + */ + fun SubMenu( + label: String, + icon: Painter, + iconRenderProperties: IconRenderProperties = IconRenderProperties.forMenuItem(), + isEnabled: Boolean = true, + submenuContent: (TrayMenuBuilder.() -> Unit)?, + ) - /** - * Adds a submenu to the tray menu with a DrawableResource icon. - * This allows calling code like: SubMenu(label, icon = Res.drawable.icon) { ... } - */ - fun SubMenu( - label: String, - icon: DrawableResource, - iconRenderProperties: IconRenderProperties = IconRenderProperties.forMenuItem(), - isEnabled: Boolean = true, - submenuContent: (TrayMenuBuilder.() -> Unit)? - ) + /** + * Adds a submenu to the tray menu with a DrawableResource icon. + * This allows calling code like: SubMenu(label, icon = Res.drawable.icon) { ... } + */ + fun SubMenu( + label: String, + icon: DrawableResource, + iconRenderProperties: IconRenderProperties = IconRenderProperties.forMenuItem(), + isEnabled: Boolean = true, + submenuContent: (TrayMenuBuilder.() -> Unit)?, + ) - /** - * Adds a visual separator (divider) to the tray menu. - * This method is used to group or separate menu items, providing better - * organization and clarity within the menu structure. - */ - fun Divider() + /** + * Adds a visual separator (divider) to the tray menu. + * This method is used to group or separate menu items, providing better + * organization and clarity within the menu structure. + */ + fun Divider() - /** - * Disposes of the resources associated with the tray menu. - * This method should be called when the tray menu is no longer in use - * to release any system resources held by it. - */ - fun dispose() -} \ No newline at end of file + /** + * Disposes of the resources associated with the tray menu. + * This method should be called when the tray menu is no longer in use + * to release any system resources held by it. + */ + fun dispose() +} diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/menu/impl/AwtTrayMenuBuilderImpl.kt b/src/jvmMain/kotlin/com/kdroid/composetray/menu/impl/AwtTrayMenuBuilderImpl.kt index 1a2d1af7..6362d3b1 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/menu/impl/AwtTrayMenuBuilderImpl.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/menu/impl/AwtTrayMenuBuilderImpl.kt @@ -7,14 +7,20 @@ import androidx.compose.ui.graphics.vector.ImageVector import com.kdroid.composetray.menu.api.TrayMenuBuilder import com.kdroid.composetray.utils.IconRenderProperties import org.jetbrains.compose.resources.DrawableResource -import org.jetbrains.compose.resources.painterResource import java.awt.MenuItem import java.awt.PopupMenu import java.awt.SystemTray import java.awt.TrayIcon -internal class AwtTrayMenuBuilderImpl(private val popupMenu: PopupMenu, private val trayIcon: TrayIcon) : TrayMenuBuilder { - override fun Item(label: String, isEnabled: Boolean, onClick: () -> Unit) { +internal class AwtTrayMenuBuilderImpl( + private val popupMenu: PopupMenu, + private val trayIcon: TrayIcon, +) : TrayMenuBuilder { + override fun Item( + label: String, + isEnabled: Boolean, + onClick: () -> Unit, + ) { val menuItem = MenuItem(label) menuItem.isEnabled = isEnabled menuItem.addActionListener { @@ -22,38 +28,38 @@ internal class AwtTrayMenuBuilderImpl(private val popupMenu: PopupMenu, private } popupMenu.add(menuItem) } - + override fun Item( label: String, iconContent: @Composable () -> Unit, iconRenderProperties: IconRenderProperties, isEnabled: Boolean, - onClick: () -> Unit + onClick: () -> Unit, ) { // Minimal implementation to make it compile // Actual icon integration will be handled by the issue creator Item(label, isEnabled, onClick) } - + override fun Item( label: String, icon: ImageVector, iconTint: Color?, iconRenderProperties: IconRenderProperties, isEnabled: Boolean, - onClick: () -> Unit + onClick: () -> Unit, ) { // Minimal implementation to make it compile // Actual icon integration will be handled by the issue creator Item(label, isEnabled, onClick) } - + override fun Item( label: String, icon: Painter, iconRenderProperties: IconRenderProperties, isEnabled: Boolean, - onClick: () -> Unit + onClick: () -> Unit, ) { // Minimal implementation to make it compile // Actual icon integration will be handled by the issue creator @@ -65,7 +71,7 @@ internal class AwtTrayMenuBuilderImpl(private val popupMenu: PopupMenu, private icon: DrawableResource, iconRenderProperties: IconRenderProperties, isEnabled: Boolean, - onClick: () -> Unit + onClick: () -> Unit, ) { // Minimal implementation to make it compile Item(label, isEnabled, onClick) @@ -75,7 +81,7 @@ internal class AwtTrayMenuBuilderImpl(private val popupMenu: PopupMenu, private label: String, checked: Boolean, onCheckedChange: (Boolean) -> Unit, - isEnabled: Boolean + isEnabled: Boolean, ) { var currentChecked = checked val checkableMenuItem = MenuItem(getCheckableLabel(label, currentChecked)) @@ -90,20 +96,20 @@ internal class AwtTrayMenuBuilderImpl(private val popupMenu: PopupMenu, private popupMenu.add(checkableMenuItem) } - + override fun CheckableItem( label: String, iconContent: @Composable () -> Unit, iconRenderProperties: IconRenderProperties, checked: Boolean, onCheckedChange: (Boolean) -> Unit, - isEnabled: Boolean + isEnabled: Boolean, ) { // Minimal implementation to make it compile // Actual icon integration will be handled by the issue creator CheckableItem(label, checked, onCheckedChange, isEnabled) } - + override fun CheckableItem( label: String, icon: ImageVector, @@ -111,20 +117,20 @@ internal class AwtTrayMenuBuilderImpl(private val popupMenu: PopupMenu, private iconRenderProperties: IconRenderProperties, checked: Boolean, onCheckedChange: (Boolean) -> Unit, - isEnabled: Boolean + isEnabled: Boolean, ) { // Minimal implementation to make it compile // Actual icon integration will be handled by the issue creator CheckableItem(label, checked, onCheckedChange, isEnabled) } - + override fun CheckableItem( label: String, icon: Painter, iconRenderProperties: IconRenderProperties, checked: Boolean, onCheckedChange: (Boolean) -> Unit, - isEnabled: Boolean + isEnabled: Boolean, ) { // Minimal implementation to make it compile // Actual icon integration will be handled by the issue creator @@ -137,50 +143,54 @@ internal class AwtTrayMenuBuilderImpl(private val popupMenu: PopupMenu, private iconRenderProperties: IconRenderProperties, checked: Boolean, onCheckedChange: (Boolean) -> Unit, - isEnabled: Boolean + isEnabled: Boolean, ) { // Minimal implementation to make it compile CheckableItem(label, checked, onCheckedChange, isEnabled) } - override fun SubMenu(label: String, isEnabled: Boolean, submenuContent: (TrayMenuBuilder.() -> Unit)?) { + override fun SubMenu( + label: String, + isEnabled: Boolean, + submenuContent: (TrayMenuBuilder.() -> Unit)?, + ) { val subMenu = PopupMenu(label) subMenu.isEnabled = isEnabled submenuContent?.let { AwtTrayMenuBuilderImpl(subMenu, trayIcon).apply(it) } popupMenu.add(subMenu) } - + override fun SubMenu( label: String, iconContent: @Composable () -> Unit, iconRenderProperties: IconRenderProperties, isEnabled: Boolean, - submenuContent: (TrayMenuBuilder.() -> Unit)? + submenuContent: (TrayMenuBuilder.() -> Unit)?, ) { // Minimal implementation to make it compile // Actual icon integration will be handled by the issue creator SubMenu(label, isEnabled, submenuContent) } - + override fun SubMenu( label: String, icon: ImageVector, iconTint: Color?, iconRenderProperties: IconRenderProperties, isEnabled: Boolean, - submenuContent: (TrayMenuBuilder.() -> Unit)? + submenuContent: (TrayMenuBuilder.() -> Unit)?, ) { // Minimal implementation to make it compile // Actual icon integration will be handled by the issue creator SubMenu(label, isEnabled, submenuContent) } - + override fun SubMenu( label: String, icon: Painter, iconRenderProperties: IconRenderProperties, isEnabled: Boolean, - submenuContent: (TrayMenuBuilder.() -> Unit)? + submenuContent: (TrayMenuBuilder.() -> Unit)?, ) { // Minimal implementation to make it compile // Actual icon integration will be handled by the issue creator @@ -192,7 +202,7 @@ internal class AwtTrayMenuBuilderImpl(private val popupMenu: PopupMenu, private icon: DrawableResource, iconRenderProperties: IconRenderProperties, isEnabled: Boolean, - submenuContent: (TrayMenuBuilder.() -> Unit)? + submenuContent: (TrayMenuBuilder.() -> Unit)?, ) { // Minimal implementation to make it compile SubMenu(label, isEnabled, submenuContent) @@ -206,7 +216,10 @@ internal class AwtTrayMenuBuilderImpl(private val popupMenu: PopupMenu, private SystemTray.getSystemTray().remove(trayIcon) } - private fun getCheckableLabel(label: String, isChecked: Boolean): String { + private fun getCheckableLabel( + label: String, + isChecked: Boolean, + ): String { return if (isChecked) "✔ $label" else label } -} \ No newline at end of file +} diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/menu/impl/LinuxTrayMenuBuilderImpl.kt b/src/jvmMain/kotlin/com/kdroid/composetray/menu/impl/LinuxTrayMenuBuilderImpl.kt index bda0e3f3..2687274f 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/menu/impl/LinuxTrayMenuBuilderImpl.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/menu/impl/LinuxTrayMenuBuilderImpl.kt @@ -8,8 +8,8 @@ import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.ColorFilter import androidx.compose.ui.graphics.painter.Painter import androidx.compose.ui.graphics.vector.ImageVector -import com.kdroid.composetray.menu.api.TrayMenuBuilder import com.kdroid.composetray.lib.linux.LinuxTrayManager +import com.kdroid.composetray.menu.api.TrayMenuBuilder import com.kdroid.composetray.utils.ComposableIconUtils import com.kdroid.composetray.utils.IconRenderProperties import com.kdroid.composetray.utils.isMenuBarInDarkMode @@ -22,7 +22,7 @@ internal class LinuxTrayMenuBuilderImpl( private val iconPath: String, private val tooltip: String = "", private val onLeftClick: (() -> Unit)?, - private val trayManager: LinuxTrayManager? = null + private val trayManager: LinuxTrayManager? = null, ) : TrayMenuBuilder { private val menuItems = mutableListOf() private val lock = ReentrantLock() @@ -30,13 +30,18 @@ internal class LinuxTrayMenuBuilderImpl( // Maintain persistent references to prevent GC private val persistentMenuItems = mutableListOf() - override fun Item(label: String, isEnabled: Boolean, onClick: () -> Unit) { + override fun Item( + label: String, + isEnabled: Boolean, + onClick: () -> Unit, + ) { lock.withLock { - val menuItem = LinuxTrayManager.MenuItem( - text = label, - isEnabled = isEnabled, - onClick = onClick - ) + val menuItem = + LinuxTrayManager.MenuItem( + text = label, + isEnabled = isEnabled, + onClick = onClick, + ) menuItems.add(menuItem) persistentMenuItems.add(menuItem) // Store reference } @@ -47,18 +52,19 @@ internal class LinuxTrayMenuBuilderImpl( iconContent: @Composable () -> Unit, iconRenderProperties: IconRenderProperties, isEnabled: Boolean, - onClick: () -> Unit + onClick: () -> Unit, ) { lock.withLock { // Render the composable icon to a PNG file val iconPath = ComposableIconUtils.renderComposableToPngFile(iconRenderProperties, iconContent) - val menuItem = LinuxTrayManager.MenuItem( - text = label, - isEnabled = isEnabled, - iconPath = iconPath, - onClick = onClick - ) + val menuItem = + LinuxTrayManager.MenuItem( + text = label, + isEnabled = isEnabled, + iconPath = iconPath, + onClick = onClick, + ) menuItems.add(menuItem) persistentMenuItems.add(menuItem) } @@ -70,9 +76,8 @@ internal class LinuxTrayMenuBuilderImpl( iconTint: Color?, iconRenderProperties: IconRenderProperties, isEnabled: Boolean, - onClick: () -> Unit + onClick: () -> Unit, ) { - // Create composable content for the icon val iconContent: @Composable () -> Unit = { val isDark = isMenuBarInDarkMode() @@ -81,9 +86,13 @@ internal class LinuxTrayMenuBuilderImpl( imageVector = icon, contentDescription = null, modifier = Modifier.fillMaxSize(), - colorFilter = iconTint?.let { ColorFilter.tint(it) } - ?: if (isDark) ColorFilter.tint(Color.White) - else ColorFilter.tint(Color.Black) + colorFilter = + iconTint?.let { ColorFilter.tint(it) } + ?: if (isDark) { + ColorFilter.tint(Color.White) + } else { + ColorFilter.tint(Color.Black) + }, ) } @@ -96,14 +105,14 @@ internal class LinuxTrayMenuBuilderImpl( icon: Painter, iconRenderProperties: IconRenderProperties, isEnabled: Boolean, - onClick: () -> Unit + onClick: () -> Unit, ) { // Create composable content for the painter val iconContent: @Composable () -> Unit = { Image( painter = icon, contentDescription = null, - modifier = Modifier.fillMaxSize() + modifier = Modifier.fillMaxSize(), ) } @@ -116,13 +125,13 @@ internal class LinuxTrayMenuBuilderImpl( icon: DrawableResource, iconRenderProperties: IconRenderProperties, isEnabled: Boolean, - onClick: () -> Unit + onClick: () -> Unit, ) { val iconContent: @Composable () -> Unit = { Image( painter = painterResource(icon), contentDescription = null, - modifier = Modifier.fillMaxSize() + modifier = Modifier.fillMaxSize(), ) } Item(label, iconContent, iconRenderProperties, isEnabled, onClick) @@ -132,7 +141,7 @@ internal class LinuxTrayMenuBuilderImpl( label: String, checked: Boolean, onCheckedChange: (Boolean) -> Unit, - isEnabled: Boolean + isEnabled: Boolean, ) { lock.withLock { // Create a mutable reference to the current checked state @@ -140,27 +149,28 @@ internal class LinuxTrayMenuBuilderImpl( // instead of capturing the initial state val initialChecked = checked - val menuItem = LinuxTrayManager.MenuItem( - text = label, - isEnabled = isEnabled, - isCheckable = true, - isChecked = initialChecked, - onClick = { - lock.withLock { - // Find the current menu item to get its current state - val currentMenuItem = menuItems.find { it.text == label } - // Toggle based on the current state, not the initial state - val currentChecked = currentMenuItem?.isChecked ?: initialChecked - val newChecked = !currentChecked - - // Call the onCheckedChange callback with the new state - onCheckedChange(newChecked) - - // Update the tray manager to reflect the new state - trayManager?.updateMenuItemCheckedState(label, newChecked) - } - } - ) + val menuItem = + LinuxTrayManager.MenuItem( + text = label, + isEnabled = isEnabled, + isCheckable = true, + isChecked = initialChecked, + onClick = { + lock.withLock { + // Find the current menu item to get its current state + val currentMenuItem = menuItems.find { it.text == label } + // Toggle based on the current state, not the initial state + val currentChecked = currentMenuItem?.isChecked ?: initialChecked + val newChecked = !currentChecked + + // Call the onCheckedChange callback with the new state + onCheckedChange(newChecked) + + // Update the tray manager to reflect the new state + trayManager?.updateMenuItemCheckedState(label, newChecked) + } + }, + ) menuItems.add(menuItem) persistentMenuItems.add(menuItem) // Store reference } @@ -172,7 +182,7 @@ internal class LinuxTrayMenuBuilderImpl( iconRenderProperties: IconRenderProperties, checked: Boolean, onCheckedChange: (Boolean) -> Unit, - isEnabled: Boolean + isEnabled: Boolean, ) { lock.withLock { // Render the composable icon to a PNG file @@ -180,23 +190,24 @@ internal class LinuxTrayMenuBuilderImpl( val initialChecked = checked - val menuItem = LinuxTrayManager.MenuItem( - text = label, - isEnabled = isEnabled, - isCheckable = true, - isChecked = initialChecked, - iconPath = iconPath, - onClick = { - lock.withLock { - val currentMenuItem = menuItems.find { it.text == label } - val currentChecked = currentMenuItem?.isChecked ?: initialChecked - val newChecked = !currentChecked - - onCheckedChange(newChecked) - trayManager?.updateMenuItemCheckedState(label, newChecked) - } - } - ) + val menuItem = + LinuxTrayManager.MenuItem( + text = label, + isEnabled = isEnabled, + isCheckable = true, + isChecked = initialChecked, + iconPath = iconPath, + onClick = { + lock.withLock { + val currentMenuItem = menuItems.find { it.text == label } + val currentChecked = currentMenuItem?.isChecked ?: initialChecked + val newChecked = !currentChecked + + onCheckedChange(newChecked) + trayManager?.updateMenuItemCheckedState(label, newChecked) + } + }, + ) menuItems.add(menuItem) persistentMenuItems.add(menuItem) } @@ -209,9 +220,8 @@ internal class LinuxTrayMenuBuilderImpl( iconRenderProperties: IconRenderProperties, checked: Boolean, onCheckedChange: (Boolean) -> Unit, - isEnabled: Boolean + isEnabled: Boolean, ) { - // Create composable content for the icon val iconContent: @Composable () -> Unit = { val isDark = isMenuBarInDarkMode() @@ -220,9 +230,13 @@ internal class LinuxTrayMenuBuilderImpl( imageVector = icon, contentDescription = null, modifier = Modifier.fillMaxSize(), - colorFilter = iconTint?.let { ColorFilter.tint(it) } - ?: if (isDark) ColorFilter.tint(Color.White) - else ColorFilter.tint(Color.Black) + colorFilter = + iconTint?.let { ColorFilter.tint(it) } + ?: if (isDark) { + ColorFilter.tint(Color.White) + } else { + ColorFilter.tint(Color.Black) + }, ) } @@ -236,14 +250,14 @@ internal class LinuxTrayMenuBuilderImpl( iconRenderProperties: IconRenderProperties, checked: Boolean, onCheckedChange: (Boolean) -> Unit, - isEnabled: Boolean + isEnabled: Boolean, ) { // Create composable content for the painter val iconContent: @Composable () -> Unit = { Image( painter = icon, contentDescription = null, - modifier = Modifier.fillMaxSize() + modifier = Modifier.fillMaxSize(), ) } @@ -257,35 +271,41 @@ internal class LinuxTrayMenuBuilderImpl( iconRenderProperties: IconRenderProperties, checked: Boolean, onCheckedChange: (Boolean) -> Unit, - isEnabled: Boolean + isEnabled: Boolean, ) { val iconContent: @Composable () -> Unit = { Image( painter = painterResource(icon), contentDescription = null, - modifier = Modifier.fillMaxSize() + modifier = Modifier.fillMaxSize(), ) } CheckableItem(label, iconContent, iconRenderProperties, checked, onCheckedChange, isEnabled) } - override fun SubMenu(label: String, isEnabled: Boolean, submenuContent: (TrayMenuBuilder.() -> Unit)?) { + override fun SubMenu( + label: String, + isEnabled: Boolean, + submenuContent: (TrayMenuBuilder.() -> Unit)?, + ) { val subMenuItems = mutableListOf() if (submenuContent != null) { - val subMenuImpl = LinuxTrayMenuBuilderImpl( - iconPath, - tooltip, - onLeftClick, - trayManager - ).apply(submenuContent) + val subMenuImpl = + LinuxTrayMenuBuilderImpl( + iconPath, + tooltip, + onLeftClick, + trayManager, + ).apply(submenuContent) subMenuItems.addAll(subMenuImpl.menuItems) } lock.withLock { - val subMenu = LinuxTrayManager.MenuItem( - text = label, - isEnabled = isEnabled, - subMenuItems = subMenuItems - ) + val subMenu = + LinuxTrayManager.MenuItem( + text = label, + isEnabled = isEnabled, + subMenuItems = subMenuItems, + ) menuItems.add(subMenu) persistentMenuItems.add(subMenu) // Store reference } @@ -296,16 +316,17 @@ internal class LinuxTrayMenuBuilderImpl( iconContent: @Composable () -> Unit, iconRenderProperties: IconRenderProperties, isEnabled: Boolean, - submenuContent: (TrayMenuBuilder.() -> Unit)? + submenuContent: (TrayMenuBuilder.() -> Unit)?, ) { val subMenuItems = mutableListOf() if (submenuContent != null) { - val subMenuImpl = LinuxTrayMenuBuilderImpl( - iconPath, - tooltip, - onLeftClick, - trayManager - ).apply(submenuContent) + val subMenuImpl = + LinuxTrayMenuBuilderImpl( + iconPath, + tooltip, + onLeftClick, + trayManager, + ).apply(submenuContent) subMenuItems.addAll(subMenuImpl.menuItems) } @@ -313,12 +334,14 @@ internal class LinuxTrayMenuBuilderImpl( // Render the composable icon to a PNG file val iconPath = ComposableIconUtils.renderComposableToPngFile(iconRenderProperties, iconContent) - val subMenu = LinuxTrayManager.MenuItem( - text = label, - isEnabled = isEnabled, - iconPath = iconPath, // Maintenant supporté ! - subMenuItems = subMenuItems - ) + val subMenu = + LinuxTrayManager.MenuItem( + text = label, + isEnabled = isEnabled, + // Maintenant supporté ! + iconPath = iconPath, + subMenuItems = subMenuItems, + ) menuItems.add(subMenu) persistentMenuItems.add(subMenu) } @@ -330,9 +353,8 @@ internal class LinuxTrayMenuBuilderImpl( iconTint: Color?, iconRenderProperties: IconRenderProperties, isEnabled: Boolean, - submenuContent: (TrayMenuBuilder.() -> Unit)? + submenuContent: (TrayMenuBuilder.() -> Unit)?, ) { - // Create composable content for the icon val iconContent: @Composable () -> Unit = { val isDark = isMenuBarInDarkMode() @@ -340,9 +362,13 @@ internal class LinuxTrayMenuBuilderImpl( imageVector = icon, contentDescription = null, modifier = Modifier.fillMaxSize(), - colorFilter = iconTint?.let { ColorFilter.tint(it) } - ?: if (isDark) ColorFilter.tint(Color.White) - else ColorFilter.tint(Color.Black) + colorFilter = + iconTint?.let { ColorFilter.tint(it) } + ?: if (isDark) { + ColorFilter.tint(Color.White) + } else { + ColorFilter.tint(Color.Black) + }, ) } @@ -355,14 +381,14 @@ internal class LinuxTrayMenuBuilderImpl( icon: Painter, iconRenderProperties: IconRenderProperties, isEnabled: Boolean, - submenuContent: (TrayMenuBuilder.() -> Unit)? + submenuContent: (TrayMenuBuilder.() -> Unit)?, ) { // Create composable content for the painter val iconContent: @Composable () -> Unit = { Image( painter = icon, contentDescription = null, - modifier = Modifier.fillMaxSize() + modifier = Modifier.fillMaxSize(), ) } @@ -375,13 +401,13 @@ internal class LinuxTrayMenuBuilderImpl( icon: DrawableResource, iconRenderProperties: IconRenderProperties, isEnabled: Boolean, - submenuContent: (TrayMenuBuilder.() -> Unit)? + submenuContent: (TrayMenuBuilder.() -> Unit)?, ) { val iconContent: @Composable () -> Unit = { Image( painter = painterResource(icon), contentDescription = null, - modifier = Modifier.fillMaxSize() + modifier = Modifier.fillMaxSize(), ) } SubMenu(label, iconContent, iconRenderProperties, isEnabled, submenuContent) @@ -403,4 +429,4 @@ internal class LinuxTrayMenuBuilderImpl( } fun build(): List = lock.withLock { menuItems.toList() } -} \ No newline at end of file +} diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/menu/impl/MacTrayMenuBuilderImpl.kt b/src/jvmMain/kotlin/com/kdroid/composetray/menu/impl/MacTrayMenuBuilderImpl.kt index 73ceadfa..826181c0 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/menu/impl/MacTrayMenuBuilderImpl.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/menu/impl/MacTrayMenuBuilderImpl.kt @@ -12,7 +12,6 @@ import com.kdroid.composetray.lib.mac.MacTrayManager import com.kdroid.composetray.menu.api.TrayMenuBuilder import com.kdroid.composetray.utils.ComposableIconUtils import com.kdroid.composetray.utils.IconRenderProperties -import com.kdroid.composetray.utils.isMenuBarInDarkMode import io.github.kdroidfilter.nucleus.darkmodedetector.isSystemInDarkMode import org.jetbrains.compose.resources.DrawableResource import org.jetbrains.compose.resources.painterResource @@ -23,7 +22,7 @@ internal class MacTrayMenuBuilderImpl( private val iconPath: String, private val tooltip: String = "", private val onLeftClick: (() -> Unit)?, - private val trayManager: MacTrayManager? = null + private val trayManager: MacTrayManager? = null, ) : TrayMenuBuilder { private val menuItems = mutableListOf() private val lock = ReentrantLock() @@ -32,14 +31,19 @@ internal class MacTrayMenuBuilderImpl( private val persistentMenuItems = mutableListOf() // Item without icon (existing method) - override fun Item(label: String, isEnabled: Boolean, onClick: () -> Unit) { + override fun Item( + label: String, + isEnabled: Boolean, + onClick: () -> Unit, + ) { lock.withLock { - val menuItem = MacTrayManager.MenuItem( - text = label, - icon = null, - isEnabled = isEnabled, - onClick = onClick - ) + val menuItem = + MacTrayManager.MenuItem( + text = label, + icon = null, + isEnabled = isEnabled, + onClick = onClick, + ) menuItems.add(menuItem) persistentMenuItems.add(menuItem) } @@ -51,18 +55,19 @@ internal class MacTrayMenuBuilderImpl( iconContent: @Composable () -> Unit, iconRenderProperties: IconRenderProperties, isEnabled: Boolean, - onClick: () -> Unit + onClick: () -> Unit, ) { lock.withLock { // Render the composable icon to a PNG file val iconPath = ComposableIconUtils.renderComposableToPngFile(iconRenderProperties, iconContent) - val menuItem = MacTrayManager.MenuItem( - text = label, - icon = iconPath, - isEnabled = isEnabled, - onClick = onClick - ) + val menuItem = + MacTrayManager.MenuItem( + text = label, + icon = iconPath, + isEnabled = isEnabled, + onClick = onClick, + ) menuItems.add(menuItem) persistentMenuItems.add(menuItem) } @@ -75,10 +80,8 @@ internal class MacTrayMenuBuilderImpl( iconTint: Color?, iconRenderProperties: IconRenderProperties, isEnabled: Boolean, - onClick: () -> Unit + onClick: () -> Unit, ) { - - // Create a composable that renders the ImageVector with appropriate tint val iconContent: @Composable () -> Unit = { // Get the current menu bar theme @@ -87,9 +90,13 @@ internal class MacTrayMenuBuilderImpl( imageVector = icon, contentDescription = null, modifier = Modifier.fillMaxSize(), - colorFilter = iconTint?.let { ColorFilter.tint(it) } - ?: if (isDark) ColorFilter.tint(Color.White) - else ColorFilter.tint(Color.Black) + colorFilter = + iconTint?.let { ColorFilter.tint(it) } + ?: if (isDark) { + ColorFilter.tint(Color.White) + } else { + ColorFilter.tint(Color.Black) + }, ) } @@ -103,14 +110,14 @@ internal class MacTrayMenuBuilderImpl( icon: Painter, iconRenderProperties: IconRenderProperties, isEnabled: Boolean, - onClick: () -> Unit + onClick: () -> Unit, ) { // Create a composable that renders the Painter val iconContent: @Composable () -> Unit = { Image( painter = icon, contentDescription = null, - modifier = Modifier.fillMaxSize() + modifier = Modifier.fillMaxSize(), ) } @@ -124,13 +131,13 @@ internal class MacTrayMenuBuilderImpl( icon: DrawableResource, iconRenderProperties: IconRenderProperties, isEnabled: Boolean, - onClick: () -> Unit + onClick: () -> Unit, ) { val iconContent: @Composable () -> Unit = { Image( painter = painterResource(icon), contentDescription = null, - modifier = Modifier.fillMaxSize() + modifier = Modifier.fillMaxSize(), ) } Item(label, iconContent, iconRenderProperties, isEnabled, onClick) @@ -141,26 +148,27 @@ internal class MacTrayMenuBuilderImpl( label: String, checked: Boolean, onCheckedChange: (Boolean) -> Unit, - isEnabled: Boolean + isEnabled: Boolean, ) { lock.withLock { - val menuItem = MacTrayManager.MenuItem( - text = label, - icon = null, - isEnabled = isEnabled, - isCheckable = true, - isChecked = checked, - onClick = { - lock.withLock { - // Toggle the checked state - val newChecked = !checked - onCheckedChange(newChecked) - - // Note: The actual visual update of the check mark - // will happen when the menu is recreated after the state change - } - } - ) + val menuItem = + MacTrayManager.MenuItem( + text = label, + icon = null, + isEnabled = isEnabled, + isCheckable = true, + isChecked = checked, + onClick = { + lock.withLock { + // Toggle the checked state + val newChecked = !checked + onCheckedChange(newChecked) + + // Note: The actual visual update of the check mark + // will happen when the menu is recreated after the state change + } + }, + ) menuItems.add(menuItem) persistentMenuItems.add(menuItem) } @@ -173,25 +181,26 @@ internal class MacTrayMenuBuilderImpl( iconRenderProperties: IconRenderProperties, checked: Boolean, onCheckedChange: (Boolean) -> Unit, - isEnabled: Boolean + isEnabled: Boolean, ) { lock.withLock { // Render the composable icon to a PNG file val iconPath = ComposableIconUtils.renderComposableToPngFile(iconRenderProperties, iconContent) - val menuItem = MacTrayManager.MenuItem( - text = label, - icon = iconPath, - isEnabled = isEnabled, - isCheckable = true, - isChecked = checked, - onClick = { - lock.withLock { - val newChecked = !checked - onCheckedChange(newChecked) - } - } - ) + val menuItem = + MacTrayManager.MenuItem( + text = label, + icon = iconPath, + isEnabled = isEnabled, + isCheckable = true, + isChecked = checked, + onClick = { + lock.withLock { + val newChecked = !checked + onCheckedChange(newChecked) + } + }, + ) menuItems.add(menuItem) persistentMenuItems.add(menuItem) } @@ -205,7 +214,7 @@ internal class MacTrayMenuBuilderImpl( iconRenderProperties: IconRenderProperties, checked: Boolean, onCheckedChange: (Boolean) -> Unit, - isEnabled: Boolean + isEnabled: Boolean, ) { // Create a composable that renders the ImageVector with appropriate tint val iconContent: @Composable () -> Unit = { @@ -215,9 +224,13 @@ internal class MacTrayMenuBuilderImpl( imageVector = icon, contentDescription = null, modifier = Modifier.fillMaxSize(), - colorFilter = iconTint?.let { ColorFilter.tint(it) } - ?: if (isDark) ColorFilter.tint(Color.White) - else ColorFilter.tint(Color.Black) + colorFilter = + iconTint?.let { ColorFilter.tint(it) } + ?: if (isDark) { + ColorFilter.tint(Color.White) + } else { + ColorFilter.tint(Color.Black) + }, ) } @@ -232,14 +245,14 @@ internal class MacTrayMenuBuilderImpl( iconRenderProperties: IconRenderProperties, checked: Boolean, onCheckedChange: (Boolean) -> Unit, - isEnabled: Boolean + isEnabled: Boolean, ) { // Create a composable that renders the Painter val iconContent: @Composable () -> Unit = { Image( painter = icon, contentDescription = null, - modifier = Modifier.fillMaxSize() + modifier = Modifier.fillMaxSize(), ) } @@ -254,20 +267,24 @@ internal class MacTrayMenuBuilderImpl( iconRenderProperties: IconRenderProperties, checked: Boolean, onCheckedChange: (Boolean) -> Unit, - isEnabled: Boolean + isEnabled: Boolean, ) { val iconContent: @Composable () -> Unit = { Image( painter = painterResource(icon), contentDescription = null, - modifier = Modifier.fillMaxSize() + modifier = Modifier.fillMaxSize(), ) } CheckableItem(label, iconContent, iconRenderProperties, checked, onCheckedChange, isEnabled) } // SubMenu without icon (existing method) - override fun SubMenu(label: String, isEnabled: Boolean, submenuContent: (TrayMenuBuilder.() -> Unit)?) { + override fun SubMenu( + label: String, + isEnabled: Boolean, + submenuContent: (TrayMenuBuilder.() -> Unit)?, + ) { createSubMenu(label, null, isEnabled, submenuContent) } @@ -277,7 +294,7 @@ internal class MacTrayMenuBuilderImpl( iconContent: @Composable () -> Unit, iconRenderProperties: IconRenderProperties, isEnabled: Boolean, - submenuContent: (TrayMenuBuilder.() -> Unit)? + submenuContent: (TrayMenuBuilder.() -> Unit)?, ) { // Render the composable icon to a PNG file val iconPath = ComposableIconUtils.renderComposableToPngFile(iconRenderProperties, iconContent) @@ -291,7 +308,7 @@ internal class MacTrayMenuBuilderImpl( iconTint: Color?, iconRenderProperties: IconRenderProperties, isEnabled: Boolean, - submenuContent: (TrayMenuBuilder.() -> Unit)? + submenuContent: (TrayMenuBuilder.() -> Unit)?, ) { // Create a composable that renders the ImageVector with appropriate tint val iconContent: @Composable () -> Unit = { @@ -301,9 +318,13 @@ internal class MacTrayMenuBuilderImpl( imageVector = icon, contentDescription = null, modifier = Modifier.fillMaxSize(), - colorFilter = iconTint?.let { ColorFilter.tint(it) } - ?: if (isDark) ColorFilter.tint(Color.White) - else ColorFilter.tint(Color.Black) + colorFilter = + iconTint?.let { ColorFilter.tint(it) } + ?: if (isDark) { + ColorFilter.tint(Color.White) + } else { + ColorFilter.tint(Color.Black) + }, ) } @@ -317,14 +338,14 @@ internal class MacTrayMenuBuilderImpl( icon: Painter, iconRenderProperties: IconRenderProperties, isEnabled: Boolean, - submenuContent: (TrayMenuBuilder.() -> Unit)? + submenuContent: (TrayMenuBuilder.() -> Unit)?, ) { // Create a composable that renders the Painter val iconContent: @Composable () -> Unit = { Image( painter = icon, contentDescription = null, - modifier = Modifier.fillMaxSize() + modifier = Modifier.fillMaxSize(), ) } @@ -338,13 +359,13 @@ internal class MacTrayMenuBuilderImpl( icon: DrawableResource, iconRenderProperties: IconRenderProperties, isEnabled: Boolean, - submenuContent: (TrayMenuBuilder.() -> Unit)? + submenuContent: (TrayMenuBuilder.() -> Unit)?, ) { val iconContent: @Composable () -> Unit = { Image( painter = painterResource(icon), contentDescription = null, - modifier = Modifier.fillMaxSize() + modifier = Modifier.fillMaxSize(), ) } SubMenu(label, iconContent, iconRenderProperties, isEnabled, submenuContent) @@ -355,25 +376,27 @@ internal class MacTrayMenuBuilderImpl( label: String, iconPath: String?, isEnabled: Boolean, - submenuContent: (TrayMenuBuilder.() -> Unit)? + submenuContent: (TrayMenuBuilder.() -> Unit)?, ) { val subMenuItems = mutableListOf() if (submenuContent != null) { - val subMenuImpl = MacTrayMenuBuilderImpl( - iconPath = this.iconPath, - tooltip = tooltip, - onLeftClick = onLeftClick, - trayManager = trayManager - ).apply(submenuContent) + val subMenuImpl = + MacTrayMenuBuilderImpl( + iconPath = this.iconPath, + tooltip = tooltip, + onLeftClick = onLeftClick, + trayManager = trayManager, + ).apply(submenuContent) subMenuItems.addAll(subMenuImpl.menuItems) } lock.withLock { - val subMenu = MacTrayManager.MenuItem( - text = label, - icon = iconPath, - isEnabled = isEnabled, - subMenuItems = subMenuItems - ) + val subMenu = + MacTrayManager.MenuItem( + text = label, + icon = iconPath, + isEnabled = isEnabled, + subMenuItems = subMenuItems, + ) menuItems.add(subMenu) persistentMenuItems.add(subMenu) } @@ -396,4 +419,4 @@ internal class MacTrayMenuBuilderImpl( } fun build(): List = lock.withLock { menuItems.toList() } -} \ No newline at end of file +} diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/menu/impl/WindowsTrayMenuBuilderImpl.kt b/src/jvmMain/kotlin/com/kdroid/composetray/menu/impl/WindowsTrayMenuBuilderImpl.kt index fa34c021..72de094c 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/menu/impl/WindowsTrayMenuBuilderImpl.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/menu/impl/WindowsTrayMenuBuilderImpl.kt @@ -21,7 +21,7 @@ import kotlin.concurrent.withLock internal class WindowsTrayMenuBuilderImpl( private val iconPath: String, private val tooltip: String = "", - private val onLeftClick: (() -> Unit)? + private val onLeftClick: (() -> Unit)?, ) : TrayMenuBuilder { private val menuItems = mutableListOf() private val lock = ReentrantLock() @@ -29,14 +29,20 @@ internal class WindowsTrayMenuBuilderImpl( // Maintain persistent references to prevent GC private val persistentMenuItems = mutableListOf() - override fun Item(label: String, isEnabled: Boolean, onClick: () -> Unit) { + override fun Item( + label: String, + isEnabled: Boolean, + onClick: () -> Unit, + ) { lock.withLock { - val menuItem = WindowsTrayManager.MenuItem( - text = label, - iconPath = null, // No icon for basic item - isEnabled = isEnabled, - onClick = onClick - ) + val menuItem = + WindowsTrayManager.MenuItem( + text = label, + // No icon for basic item + iconPath = null, + isEnabled = isEnabled, + onClick = onClick, + ) menuItems.add(menuItem) persistentMenuItems.add(menuItem) // Store reference to prevent GC } @@ -47,18 +53,19 @@ internal class WindowsTrayMenuBuilderImpl( iconContent: @Composable () -> Unit, iconRenderProperties: IconRenderProperties, isEnabled: Boolean, - onClick: () -> Unit + onClick: () -> Unit, ) { lock.withLock { // Render the composable icon to a PNG file (for future Windows icon support) val iconPath = ComposableIconUtils.renderComposableToIcoFile(iconRenderProperties, iconContent) - val menuItem = WindowsTrayManager.MenuItem( - text = label, - iconPath = iconPath, - isEnabled = isEnabled, - onClick = onClick - ) + val menuItem = + WindowsTrayManager.MenuItem( + text = label, + iconPath = iconPath, + isEnabled = isEnabled, + onClick = onClick, + ) menuItems.add(menuItem) persistentMenuItems.add(menuItem) } @@ -70,7 +77,7 @@ internal class WindowsTrayMenuBuilderImpl( iconTint: Color?, iconRenderProperties: IconRenderProperties, isEnabled: Boolean, - onClick: () -> Unit + onClick: () -> Unit, ) { // Create composable content for the icon val iconContent: @Composable () -> Unit = { @@ -80,9 +87,13 @@ internal class WindowsTrayMenuBuilderImpl( imageVector = icon, contentDescription = null, modifier = Modifier.fillMaxSize(), - colorFilter = iconTint?.let { ColorFilter.tint(it) } - ?: if (isDark) ColorFilter.tint(Color.White) - else ColorFilter.tint(Color.Black) + colorFilter = + iconTint?.let { ColorFilter.tint(it) } + ?: if (isDark) { + ColorFilter.tint(Color.White) + } else { + ColorFilter.tint(Color.Black) + }, ) } @@ -95,14 +106,14 @@ internal class WindowsTrayMenuBuilderImpl( icon: Painter, iconRenderProperties: IconRenderProperties, isEnabled: Boolean, - onClick: () -> Unit + onClick: () -> Unit, ) { // Create composable content for the painter val iconContent: @Composable () -> Unit = { Image( painter = icon, contentDescription = null, - modifier = Modifier.fillMaxSize() + modifier = Modifier.fillMaxSize(), ) } @@ -115,13 +126,13 @@ internal class WindowsTrayMenuBuilderImpl( icon: DrawableResource, iconRenderProperties: IconRenderProperties, isEnabled: Boolean, - onClick: () -> Unit + onClick: () -> Unit, ) { val iconContent: @Composable () -> Unit = { Image( painter = painterResource(icon), contentDescription = null, - modifier = Modifier.fillMaxSize() + modifier = Modifier.fillMaxSize(), ) } Item(label, iconContent, iconRenderProperties, isEnabled, onClick) @@ -131,21 +142,22 @@ internal class WindowsTrayMenuBuilderImpl( label: String, checked: Boolean, onCheckedChange: (Boolean) -> Unit, - isEnabled: Boolean + isEnabled: Boolean, ) { lock.withLock { - val menuItem = WindowsTrayManager.MenuItem( - text = label, - iconPath = null, - isEnabled = isEnabled, - isCheckable = true, - isChecked = checked, - onClick = { - // Toggle the checked state - val newChecked = !checked - onCheckedChange(newChecked) - } - ) + val menuItem = + WindowsTrayManager.MenuItem( + text = label, + iconPath = null, + isEnabled = isEnabled, + isCheckable = true, + isChecked = checked, + onClick = { + // Toggle the checked state + val newChecked = !checked + onCheckedChange(newChecked) + }, + ) menuItems.add(menuItem) persistentMenuItems.add(menuItem) // Store reference to prevent GC } @@ -157,24 +169,25 @@ internal class WindowsTrayMenuBuilderImpl( iconRenderProperties: IconRenderProperties, checked: Boolean, onCheckedChange: (Boolean) -> Unit, - isEnabled: Boolean + isEnabled: Boolean, ) { lock.withLock { // Render the composable icon to a PNG file (for future Windows icon support) val iconPath = ComposableIconUtils.renderComposableToIcoFile(iconRenderProperties, iconContent) - val menuItem = WindowsTrayManager.MenuItem( - text = label, - iconPath = iconPath, - isEnabled = isEnabled, - isCheckable = true, - isChecked = checked, - onClick = { - // Toggle the checked state - val newChecked = !checked - onCheckedChange(newChecked) - } - ) + val menuItem = + WindowsTrayManager.MenuItem( + text = label, + iconPath = iconPath, + isEnabled = isEnabled, + isCheckable = true, + isChecked = checked, + onClick = { + // Toggle the checked state + val newChecked = !checked + onCheckedChange(newChecked) + }, + ) menuItems.add(menuItem) persistentMenuItems.add(menuItem) } @@ -187,7 +200,7 @@ internal class WindowsTrayMenuBuilderImpl( iconRenderProperties: IconRenderProperties, checked: Boolean, onCheckedChange: (Boolean) -> Unit, - isEnabled: Boolean + isEnabled: Boolean, ) { // Create composable content for the icon val iconContent: @Composable () -> Unit = { @@ -197,9 +210,13 @@ internal class WindowsTrayMenuBuilderImpl( imageVector = icon, contentDescription = null, modifier = Modifier.fillMaxSize(), - colorFilter = iconTint?.let { ColorFilter.tint(it) } - ?: if (isDark) ColorFilter.tint(Color.White) - else ColorFilter.tint(Color.Black) + colorFilter = + iconTint?.let { ColorFilter.tint(it) } + ?: if (isDark) { + ColorFilter.tint(Color.White) + } else { + ColorFilter.tint(Color.Black) + }, ) } @@ -213,14 +230,14 @@ internal class WindowsTrayMenuBuilderImpl( iconRenderProperties: IconRenderProperties, checked: Boolean, onCheckedChange: (Boolean) -> Unit, - isEnabled: Boolean + isEnabled: Boolean, ) { // Create composable content for the painter val iconContent: @Composable () -> Unit = { Image( painter = icon, contentDescription = null, - modifier = Modifier.fillMaxSize() + modifier = Modifier.fillMaxSize(), ) } @@ -234,19 +251,23 @@ internal class WindowsTrayMenuBuilderImpl( iconRenderProperties: IconRenderProperties, checked: Boolean, onCheckedChange: (Boolean) -> Unit, - isEnabled: Boolean + isEnabled: Boolean, ) { val iconContent: @Composable () -> Unit = { Image( painter = painterResource(icon), contentDescription = null, - modifier = Modifier.fillMaxSize() + modifier = Modifier.fillMaxSize(), ) } CheckableItem(label, iconContent, iconRenderProperties, checked, onCheckedChange, isEnabled) } - override fun SubMenu(label: String, isEnabled: Boolean, submenuContent: (TrayMenuBuilder.() -> Unit)?) { + override fun SubMenu( + label: String, + isEnabled: Boolean, + submenuContent: (TrayMenuBuilder.() -> Unit)?, + ) { createSubMenu(label, null, isEnabled, submenuContent) } @@ -255,7 +276,7 @@ internal class WindowsTrayMenuBuilderImpl( iconContent: @Composable () -> Unit, iconRenderProperties: IconRenderProperties, isEnabled: Boolean, - submenuContent: (TrayMenuBuilder.() -> Unit)? + submenuContent: (TrayMenuBuilder.() -> Unit)?, ) { // Render the composable icon to a PNG file (for future Windows icon support) val iconPath = ComposableIconUtils.renderComposableToIcoFile(iconRenderProperties, iconContent) @@ -268,7 +289,7 @@ internal class WindowsTrayMenuBuilderImpl( iconTint: Color?, iconRenderProperties: IconRenderProperties, isEnabled: Boolean, - submenuContent: (TrayMenuBuilder.() -> Unit)? + submenuContent: (TrayMenuBuilder.() -> Unit)?, ) { // Create composable content for the icon val iconContent: @Composable () -> Unit = { @@ -278,9 +299,13 @@ internal class WindowsTrayMenuBuilderImpl( imageVector = icon, contentDescription = null, modifier = Modifier.fillMaxSize(), - colorFilter = iconTint?.let { ColorFilter.tint(it) } - ?: if (isDark) ColorFilter.tint(Color.White) - else ColorFilter.tint(Color.Black) + colorFilter = + iconTint?.let { ColorFilter.tint(it) } + ?: if (isDark) { + ColorFilter.tint(Color.White) + } else { + ColorFilter.tint(Color.Black) + }, ) } @@ -293,14 +318,14 @@ internal class WindowsTrayMenuBuilderImpl( icon: Painter, iconRenderProperties: IconRenderProperties, isEnabled: Boolean, - submenuContent: (TrayMenuBuilder.() -> Unit)? + submenuContent: (TrayMenuBuilder.() -> Unit)?, ) { // Create composable content for the painter val iconContent: @Composable () -> Unit = { Image( painter = icon, contentDescription = null, - modifier = Modifier.fillMaxSize() + modifier = Modifier.fillMaxSize(), ) } @@ -313,13 +338,13 @@ internal class WindowsTrayMenuBuilderImpl( icon: DrawableResource, iconRenderProperties: IconRenderProperties, isEnabled: Boolean, - submenuContent: (TrayMenuBuilder.() -> Unit)? + submenuContent: (TrayMenuBuilder.() -> Unit)?, ) { val iconContent: @Composable () -> Unit = { Image( painter = painterResource(icon), contentDescription = null, - modifier = Modifier.fillMaxSize() + modifier = Modifier.fillMaxSize(), ) } SubMenu(label, iconContent, iconRenderProperties, isEnabled, submenuContent) @@ -330,20 +355,26 @@ internal class WindowsTrayMenuBuilderImpl( label: String, iconPath: String?, isEnabled: Boolean, - submenuContent: (TrayMenuBuilder.() -> Unit)? + submenuContent: (TrayMenuBuilder.() -> Unit)?, ) { val subMenuItems = mutableListOf() if (submenuContent != null) { - val subMenuImpl = WindowsTrayMenuBuilderImpl(this.iconPath, tooltip, onLeftClick = onLeftClick).apply(submenuContent) + val subMenuImpl = + WindowsTrayMenuBuilderImpl( + this.iconPath, + tooltip, + onLeftClick = onLeftClick, + ).apply(submenuContent) subMenuItems.addAll(subMenuImpl.menuItems) } lock.withLock { - val subMenu = WindowsTrayManager.MenuItem( - text = label, - iconPath = iconPath, - isEnabled = isEnabled, - subMenuItems = subMenuItems - ) + val subMenu = + WindowsTrayManager.MenuItem( + text = label, + iconPath = iconPath, + isEnabled = isEnabled, + subMenuItems = subMenuItems, + ) menuItems.add(subMenu) persistentMenuItems.add(subMenu) // Store reference to prevent GC } @@ -359,10 +390,15 @@ internal class WindowsTrayMenuBuilderImpl( override fun dispose() { lock.withLock { - WindowsTrayManager(instanceId = "_temp", iconPath = iconPath, tooltip = tooltip, onLeftClick = onLeftClick).stopTray() + WindowsTrayManager( + instanceId = "_temp", + iconPath = iconPath, + tooltip = tooltip, + onLeftClick = onLeftClick, + ).stopTray() persistentMenuItems.clear() // Clear references when disposing } } fun build(): List = lock.withLock { menuItems.toList() } -} \ No newline at end of file +} diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/tray/api/ExperimentalTrayAppApi.kt b/src/jvmMain/kotlin/com/kdroid/composetray/tray/api/ExperimentalTrayAppApi.kt index 378d70ff..4720e53c 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/tray/api/ExperimentalTrayAppApi.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/tray/api/ExperimentalTrayAppApi.kt @@ -11,13 +11,13 @@ package com.kdroid.composetray.tray.api */ @RequiresOptIn( message = "TrayApp is experimental and may change or be removed without notice.", - level = RequiresOptIn.Level.WARNING + level = RequiresOptIn.Level.WARNING, ) @Retention(AnnotationRetention.BINARY) @Target( AnnotationTarget.CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY, - AnnotationTarget.TYPEALIAS + AnnotationTarget.TYPEALIAS, ) -annotation class ExperimentalTrayAppApi \ No newline at end of file +annotation class ExperimentalTrayAppApi diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/tray/api/NativeTray.kt b/src/jvmMain/kotlin/com/kdroid/composetray/tray/api/NativeTray.kt index c4d20d8e..39325665 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/tray/api/NativeTray.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/tray/api/NativeTray.kt @@ -10,27 +10,35 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.painter.Painter import androidx.compose.ui.graphics.vector.ImageVector -import org.jetbrains.compose.resources.DrawableResource -import org.jetbrains.compose.resources.painterResource import androidx.compose.ui.window.ApplicationScope import com.kdroid.composetray.menu.api.TrayMenuBuilder import com.kdroid.composetray.tray.impl.AwtTrayInitializer import com.kdroid.composetray.tray.impl.LinuxTrayInitializer import com.kdroid.composetray.tray.impl.MacTrayInitializer import com.kdroid.composetray.tray.impl.WindowsTrayInitializer -import com.kdroid.composetray.utils.* -import io.github.kdroidfilter.platformtools.OperatingSystem.* +import com.kdroid.composetray.utils.ComposableIconUtils +import com.kdroid.composetray.utils.IconRenderProperties +import com.kdroid.composetray.utils.MenuContentHash +import com.kdroid.composetray.utils.debugln +import com.kdroid.composetray.utils.errorln +import com.kdroid.composetray.utils.extractToTempIfDifferent +import com.kdroid.composetray.utils.isMenuBarInDarkMode import io.github.kdroidfilter.nucleus.darkmodedetector.isSystemInDarkMode +import io.github.kdroidfilter.platformtools.OperatingSystem.LINUX +import io.github.kdroidfilter.platformtools.OperatingSystem.MACOS +import io.github.kdroidfilter.platformtools.OperatingSystem.UNKNOWN +import io.github.kdroidfilter.platformtools.OperatingSystem.WINDOWS import io.github.kdroidfilter.platformtools.getOperatingSystem import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.delay import kotlinx.coroutines.launch +import org.jetbrains.compose.resources.DrawableResource +import org.jetbrains.compose.resources.painterResource import java.util.concurrent.atomic.AtomicBoolean internal class NativeTray { - private val trayScope = CoroutineScope(Dispatchers.IO + SupervisorJob()) private val awtTrayUsed = AtomicBoolean(false) @@ -47,7 +55,7 @@ internal class NativeTray { windowsIconPath: String = iconPath, tooltip: String, primaryAction: (() -> Unit)?, - menuContent: (TrayMenuBuilder.() -> Unit)? + menuContent: (TrayMenuBuilder.() -> Unit)?, ) { if (!initialized) { initializeTray(iconPath, windowsIconPath, tooltip, primaryAction, menuContent) @@ -58,7 +66,14 @@ internal class NativeTray { try { when (os) { LINUX -> LinuxTrayInitializer.update(instanceId, iconPath, tooltip, primaryAction, menuContent) - WINDOWS -> WindowsTrayInitializer.update(instanceId, windowsIconPath, tooltip, primaryAction, menuContent) + WINDOWS -> + WindowsTrayInitializer.update( + instanceId, + windowsIconPath, + tooltip, + primaryAction, + menuContent, + ) MACOS -> MacTrayInitializer.update(instanceId, iconPath, tooltip, primaryAction, menuContent) UNKNOWN -> { AwtTrayInitializer.update(iconPath, tooltip, primaryAction, menuContent) @@ -92,7 +107,10 @@ internal class NativeTray { trayScope.launch { val rendered = renderIconsWithRetry(iconContent, iconRenderProperties, maxAttempts, backoffMs) if (rendered == null) { - errorln { "[NativeTray] Icon rendering failed after $maxAttempts attempts. Tray will not be created/updated." } + errorln { + "[NativeTray] Icon rendering failed after $maxAttempts attempts. " + + "Tray will not be created/updated." + } return@launch } @@ -104,8 +122,22 @@ internal class NativeTray { } else { try { when (os) { - LINUX -> LinuxTrayInitializer.update(instanceId, pngIconPath, tooltip, primaryAction, menuContent) - WINDOWS -> WindowsTrayInitializer.update(instanceId, windowsIconPath, tooltip, primaryAction, menuContent) + LINUX -> + LinuxTrayInitializer.update( + instanceId, + pngIconPath, + tooltip, + primaryAction, + menuContent, + ) + WINDOWS -> + WindowsTrayInitializer.update( + instanceId, + windowsIconPath, + tooltip, + primaryAction, + menuContent, + ) MACOS -> MacTrayInitializer.update(instanceId, pngIconPath, tooltip, primaryAction, menuContent) UNKNOWN -> { AwtTrayInitializer.update(pngIconPath, tooltip, primaryAction, menuContent) @@ -121,7 +153,11 @@ internal class NativeTray { // On macOS, pre-render light/dark variants for instant appearance switching if (os == MACOS && lightIconContent != null && darkIconContent != null) { try { - val lightPath = ComposableIconUtils.renderComposableToPngFile(iconRenderProperties, lightIconContent) + val lightPath = + ComposableIconUtils.renderComposableToPngFile( + iconRenderProperties, + lightIconContent, + ) val darkPath = ComposableIconUtils.renderComposableToPngFile(iconRenderProperties, darkIconContent) MacTrayInitializer.setAppearanceIcons(instanceId, lightPath, darkPath) } catch (th: Throwable) { @@ -134,7 +170,10 @@ internal class NativeTray { /** * Set macOS appearance icons directly from file paths. */ - fun setMacOSAppearanceIcons(lightPath: String, darkPath: String) { + fun setMacOSAppearanceIcons( + lightPath: String, + darkPath: String, + ) { if (os == MACOS && initialized) { MacTrayInitializer.setAppearanceIcons(instanceId, lightPath, darkPath) } @@ -153,14 +192,23 @@ internal class NativeTray { val pngIconPath = ComposableIconUtils.renderComposableToPngFile(iconRenderProperties, iconContent) // On Windows, also render to ICO; on other OSes reuse PNG path - val windowsIconPath = if (os == WINDOWS) { - ComposableIconUtils.renderComposableToIcoFile(iconRenderProperties, iconContent) - } else pngIconPath + val windowsIconPath = + if (os == WINDOWS) { + ComposableIconUtils.renderComposableToIcoFile(iconRenderProperties, iconContent) + } else { + pngIconPath + } - debugln { "[NativeTray] Rendered tray icons (attempt ${attempt + 1}/$maxAttempts): PNG=$pngIconPath, WIN=$windowsIconPath" } + debugln { + "[NativeTray] Rendered tray icons (attempt ${attempt + 1}/$maxAttempts): " + + "PNG=$pngIconPath, WIN=$windowsIconPath" + } return pngIconPath to windowsIconPath } catch (e: Throwable) { - errorln { "[NativeTray] Icon render attempt ${attempt + 1} failed: ${e.message ?: e::class.simpleName}" } + errorln { + "[NativeTray] Icon render attempt ${attempt + 1} failed: " + + "${e.message ?: e::class.simpleName}" + } attempt++ if (attempt < maxAttempts) delay(backoffMs) } @@ -185,14 +233,14 @@ internal class NativeTray { */ @Deprecated( message = "Use the constructor with composable icon content instead", - replaceWith = ReplaceWith("NativeTray(iconContent, tooltip, primaryAction, menuContent)") + replaceWith = ReplaceWith("NativeTray(iconContent, tooltip, primaryAction, menuContent)"), ) private fun initializeTray( iconPath: String, windowsIconPath: String = iconPath, tooltip: String = "", primaryAction: (() -> Unit)?, - menuContent: (TrayMenuBuilder.() -> Unit)? = null + menuContent: (TrayMenuBuilder.() -> Unit)? = null, ) { trayScope.launch { var trayInitialized = false @@ -206,7 +254,13 @@ internal class NativeTray { } WINDOWS -> { debugln { "[NativeTray] Initializing Windows tray with icon path: $windowsIconPath" } - WindowsTrayInitializer.initialize(instanceId, windowsIconPath, tooltip, primaryAction, menuContent) + WindowsTrayInitializer.initialize( + instanceId, + windowsIconPath, + tooltip, + primaryAction, + menuContent, + ) trayInitialized = true } MACOS -> { @@ -246,14 +300,14 @@ internal class NativeTray { iconRenderProperties: IconRenderProperties = IconRenderProperties(), tooltip: String = "", primaryAction: (() -> Unit)?, - menuContent: (TrayMenuBuilder.() -> Unit)? = null + menuContent: (TrayMenuBuilder.() -> Unit)? = null, ) { updateComposable( iconContent = iconContent, iconRenderProperties = iconRenderProperties, tooltip = tooltip, primaryAction = primaryAction, - menuContent = menuContent + menuContent = menuContent, ) } } @@ -263,7 +317,7 @@ internal class NativeTray { */ @Deprecated( message = "Use the version with composable icon content instead", - replaceWith = ReplaceWith("Tray(iconContent, tooltip, primaryAction, menuContent)") + replaceWith = ReplaceWith("Tray(iconContent, tooltip, primaryAction, menuContent)"), ) @Composable fun ApplicationScope.Tray( @@ -274,10 +328,14 @@ fun ApplicationScope.Tray( menuContent: (TrayMenuBuilder.() -> Unit)? = null, ) { val absoluteIconPath = remember(iconPath) { extractToTempIfDifferent(iconPath)?.absolutePath.orEmpty() } - val absoluteWindowsIconPath = remember(iconPath, windowsIconPath) { - if (windowsIconPath == iconPath) absoluteIconPath - else extractToTempIfDifferent(windowsIconPath)?.absolutePath.orEmpty() - } + val absoluteWindowsIconPath = + remember(iconPath, windowsIconPath) { + if (windowsIconPath == iconPath) { + absoluteIconPath + } else { + extractToTempIfDifferent(windowsIconPath)?.absolutePath.orEmpty() + } + } val tray = remember { NativeTray() } @@ -306,7 +364,7 @@ fun ApplicationScope.Tray( primaryAction: (() -> Unit)? = null, menuContent: (TrayMenuBuilder.() -> Unit)? = null, ) { - val isDark = isMenuBarInDarkMode() // Observe menu bar theme to trigger recomposition on changes + val isDark = isMenuBarInDarkMode() // Observe menu bar theme to trigger recomposition on changes // Calculate a hash of the rendered composable content to detect changes, including theme state val contentHash = ComposableIconUtils.calculateContentHash(iconRenderProperties, iconContent) + isDark.hashCode() @@ -356,40 +414,51 @@ fun ApplicationScope.Tray( imageVector = icon, contentDescription = null, modifier = Modifier.fillMaxSize(), - colorFilter = tint?.let { androidx.compose.ui.graphics.ColorFilter.tint(it) } - ?: if (isDark) androidx.compose.ui.graphics.ColorFilter.tint(Color.White) - else androidx.compose.ui.graphics.ColorFilter.tint(Color.Black) + colorFilter = + tint?.let { androidx.compose.ui.graphics.ColorFilter.tint(it) } + ?: if (isDark) { + androidx.compose.ui.graphics.ColorFilter.tint(Color.White) + } else { + androidx.compose.ui.graphics.ColorFilter.tint(Color.Black) + }, ) } // On macOS with auto-tint, pre-render both light and dark variants for instant switching - val lightIconContent: (@Composable () -> Unit)? = if (tint == null && isMacOS) { - { - Image( - imageVector = icon, - contentDescription = null, - modifier = Modifier.fillMaxSize(), - colorFilter = androidx.compose.ui.graphics.ColorFilter.tint(Color.Black) - ) + val lightIconContent: (@Composable () -> Unit)? = + if (tint == null && isMacOS) { + { + Image( + imageVector = icon, + contentDescription = null, + modifier = Modifier.fillMaxSize(), + colorFilter = androidx.compose.ui.graphics.ColorFilter.tint(Color.Black), + ) + } + } else { + null } - } else null - - val darkIconContent: (@Composable () -> Unit)? = if (tint == null && isMacOS) { - { - Image( - imageVector = icon, - contentDescription = null, - modifier = Modifier.fillMaxSize(), - colorFilter = androidx.compose.ui.graphics.ColorFilter.tint(Color.White) - ) + + val darkIconContent: (@Composable () -> Unit)? = + if (tint == null && isMacOS) { + { + Image( + imageVector = icon, + contentDescription = null, + modifier = Modifier.fillMaxSize(), + colorFilter = androidx.compose.ui.graphics.ColorFilter.tint(Color.White), + ) + } + } else { + null } - } else null // Calculate menu hash to detect changes val menuHash = MenuContentHash.calculateMenuHash(menuContent) // Updated contentHash to include icon and tint for proper recomposition on changes - val contentHash = ComposableIconUtils.calculateContentHash(iconRenderProperties, iconContent) + + val contentHash = + ComposableIconUtils.calculateContentHash(iconRenderProperties, iconContent) + isDark.hashCode() + isSystemInDarkTheme.hashCode() + icon.hashCode() + @@ -427,14 +496,14 @@ fun ApplicationScope.Tray( primaryAction: (() -> Unit)? = null, menuContent: (TrayMenuBuilder.() -> Unit)? = null, ) { - val isDark = isMenuBarInDarkMode() // Included for consistency, even if not used in rendering + val isDark = isMenuBarInDarkMode() // Included for consistency, even if not used in rendering // Define the icon content lambda val iconContent: @Composable () -> Unit = { Image( painter = icon, contentDescription = null, - modifier = Modifier.fillMaxSize() + modifier = Modifier.fillMaxSize(), ) } @@ -442,7 +511,8 @@ fun ApplicationScope.Tray( val menuHash = MenuContentHash.calculateMenuHash(menuContent) // Updated contentHash to include icon for proper recomposition on changes - val contentHash = ComposableIconUtils.calculateContentHash(iconRenderProperties, iconContent) + + val contentHash = + ComposableIconUtils.calculateContentHash(iconRenderProperties, iconContent) + isDark.hashCode() + icon.hashCode() @@ -490,7 +560,7 @@ fun ApplicationScope.Tray( iconRenderProperties = iconRenderProperties, tooltip = tooltip, primaryAction = primaryAction, - menuContent = menuContent + menuContent = menuContent, ) } else { // Use ImageVector for macOS and Linux @@ -500,7 +570,7 @@ fun ApplicationScope.Tray( iconRenderProperties = iconRenderProperties, tooltip = tooltip, primaryAction = primaryAction, - menuContent = menuContent + menuContent = menuContent, ) } } @@ -523,7 +593,7 @@ fun ApplicationScope.Tray( iconRenderProperties = iconRenderProperties, tooltip = tooltip, primaryAction = primaryAction, - menuContent = menuContent + menuContent = menuContent, ) } @@ -547,7 +617,7 @@ fun ApplicationScope.Tray( iconRenderProperties = iconRenderProperties, tooltip = tooltip, primaryAction = primaryAction, - menuContent = menuContent + menuContent = menuContent, ) } else { // Use ImageVector for macOS and Linux @@ -557,7 +627,7 @@ fun ApplicationScope.Tray( iconRenderProperties = iconRenderProperties, tooltip = tooltip, primaryAction = primaryAction, - menuContent = menuContent + menuContent = menuContent, ) } } diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/tray/api/TrayApp.kt b/src/jvmMain/kotlin/com/kdroid/composetray/tray/api/TrayApp.kt index 563fd468..b19a8c70 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/tray/api/TrayApp.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/tray/api/TrayApp.kt @@ -1,17 +1,35 @@ @file:OptIn( ExperimentalTrayAppApi::class, ExperimentalTransitionApi::class, - InternalAnimationApi::class + InternalAnimationApi::class, ) package com.kdroid.composetray.tray.api -import androidx.compose.animation.* -import androidx.compose.animation.core.* +import androidx.compose.animation.EnterTransition +import androidx.compose.animation.ExitTransition +import androidx.compose.animation.animateEnterExit +import androidx.compose.animation.core.EaseInOut +import androidx.compose.animation.core.ExperimentalTransitionApi +import androidx.compose.animation.core.InternalAnimationApi +import androidx.compose.animation.core.MutableTransitionState +import androidx.compose.animation.core.tween +import androidx.compose.animation.fadeIn +import androidx.compose.animation.fadeOut +import androidx.compose.animation.slideInVertically +import androidx.compose.animation.slideOutVertically import androidx.compose.foundation.Image import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxSize -import androidx.compose.runtime.* +import androidx.compose.runtime.Composable +import androidx.compose.runtime.DisposableEffect +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.runtime.snapshotFlow import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.ColorFilter @@ -21,16 +39,30 @@ import androidx.compose.ui.graphics.vector.ImageVector import androidx.compose.ui.input.key.KeyEvent import androidx.compose.ui.unit.DpSize import androidx.compose.ui.unit.dp -import androidx.compose.ui.window.* +import androidx.compose.ui.window.ApplicationScope +import androidx.compose.ui.window.DialogWindow +import androidx.compose.ui.window.DialogWindowScope +import androidx.compose.ui.window.WindowPosition +import androidx.compose.ui.window.rememberDialogState import com.kdroid.composetray.lib.linux.LinuxOutsideClickWatcher -import com.kdroid.composetray.utils.debugln +import com.kdroid.composetray.lib.mac.MacNativeBridge import com.kdroid.composetray.lib.mac.MacOSWindowManager import com.kdroid.composetray.lib.mac.MacOutsideClickWatcher -import com.kdroid.composetray.lib.mac.MacNativeBridge import com.kdroid.composetray.lib.windows.WindowsOutsideClickWatcher -import com.kdroid.composetray.tray.impl.WindowsTrayInitializer import com.kdroid.composetray.menu.api.TrayMenuBuilder -import com.kdroid.composetray.utils.* +import com.kdroid.composetray.tray.impl.WindowsTrayInitializer +import com.kdroid.composetray.utils.ComposableIconUtils +import com.kdroid.composetray.utils.ExperimentalTrayAppApi +import com.kdroid.composetray.utils.IconRenderProperties +import com.kdroid.composetray.utils.MenuContentHash +import com.kdroid.composetray.utils.PersistentAnimatedVisibility +import com.kdroid.composetray.utils.TrayAppState +import com.kdroid.composetray.utils.TrayWindowDismissMode +import com.kdroid.composetray.utils.WindowVisibilityMonitor +import com.kdroid.composetray.utils.debugln +import com.kdroid.composetray.utils.getTrayWindowPositionForInstance +import com.kdroid.composetray.utils.isMenuBarInDarkMode +import com.kdroid.composetray.utils.rememberTrayAppState import io.github.kdroidfilter.platformtools.LinuxDesktopEnvironment import io.github.kdroidfilter.platformtools.OperatingSystem import io.github.kdroidfilter.platformtools.OperatingSystem.MACOS @@ -53,31 +85,47 @@ import java.util.concurrent.atomic.AtomicLong // --------------------- Public API (defaults) --------------------- private val defaultTrayAppEnterTransition = - if (getOperatingSystem() == WINDOWS) + if (getOperatingSystem() == WINDOWS) { slideInVertically( initialOffsetY = { fullHeight -> fullHeight }, - animationSpec = tween(250, easing = EaseInOut) + animationSpec = tween(250, easing = EaseInOut), ) + fadeIn(animationSpec = tween(200, easing = EaseInOut)) - else - fadeIn(animationSpec = tween(if (detectLinuxDesktopEnvironment() == LinuxDesktopEnvironment.KDE) 50 else 200, easing = EaseInOut)) + } else { + fadeIn( + animationSpec = + tween( + if (detectLinuxDesktopEnvironment() == LinuxDesktopEnvironment.KDE) 50 else 200, + easing = EaseInOut, + ), + ) + } private val defaultTrayAppExitTransition = - if (getOperatingSystem() == WINDOWS) + if (getOperatingSystem() == WINDOWS) { slideOutVertically( targetOffsetY = { fullHeight -> fullHeight }, - animationSpec = tween(250, easing = EaseInOut) + animationSpec = tween(250, easing = EaseInOut), ) + fadeOut(animationSpec = tween(200, easing = EaseInOut)) - else - fadeOut(animationSpec = tween(if (detectLinuxDesktopEnvironment() == LinuxDesktopEnvironment.KDE) 50 else 200, easing = EaseInOut)) - -private val defaultVerticalOffset = when (getOperatingSystem()) { - WINDOWS -> -10 - MACOS -> 5 - else -> when (detectLinuxDesktopEnvironment()) { - LinuxDesktopEnvironment.GNOME -> 10 - else -> 0 + } else { + fadeOut( + animationSpec = + tween( + if (detectLinuxDesktopEnvironment() == LinuxDesktopEnvironment.KDE) 50 else 200, + easing = EaseInOut, + ), + ) + } + +private val defaultVerticalOffset = + when (getOperatingSystem()) { + WINDOWS -> -10 + MACOS -> 5 + else -> + when (detectLinuxDesktopEnvironment()) { + LinuxDesktopEnvironment.GNOME -> 10 + else -> 0 + } } -} // --------------------- Public API (overloads) --------------------- @@ -111,8 +159,9 @@ fun ApplicationScope.TrayApp( imageVector = icon, contentDescription = null, modifier = Modifier.fillMaxSize(), - colorFilter = tint?.let { ColorFilter.tint(it) } - ?: if (isDark) ColorFilter.tint(Color.White) else ColorFilter.tint(Color.Black) + colorFilter = + tint?.let { ColorFilter.tint(it) } + ?: if (isDark) ColorFilter.tint(Color.White) else ColorFilter.tint(Color.Black), ) } TrayApp( @@ -404,49 +453,51 @@ fun ApplicationScope.TrayApp( content: @Composable DialogWindowScope.() -> Unit, ) { when (getOperatingSystem()) { - OperatingSystem.LINUX -> TrayAppImplLinux( - iconContent, - iconRenderProperties, - tooltip, - state, - windowSize, - visibleOnStart, - enterTransition, - exitTransition, - transparent, - windowsTitle, - windowIcon, - undecorated, - resizable, - horizontalOffset, - verticalOffset, - onPreviewKeyEvent, - onKeyEvent, - menu, - content - ) - - else -> TrayAppImplOriginal( - iconContent, - iconRenderProperties, - tooltip, - state, - windowSize, - visibleOnStart, - enterTransition, - exitTransition, - transparent, - windowsTitle, - windowIcon, - undecorated, - resizable, - horizontalOffset, - verticalOffset, - onPreviewKeyEvent, - onKeyEvent, - menu, - content - ) + OperatingSystem.LINUX -> + TrayAppImplLinux( + iconContent, + iconRenderProperties, + tooltip, + state, + windowSize, + visibleOnStart, + enterTransition, + exitTransition, + transparent, + windowsTitle, + windowIcon, + undecorated, + resizable, + horizontalOffset, + verticalOffset, + onPreviewKeyEvent, + onKeyEvent, + menu, + content, + ) + + else -> + TrayAppImplOriginal( + iconContent, + iconRenderProperties, + tooltip, + state, + windowSize, + visibleOnStart, + enterTransition, + exitTransition, + transparent, + windowsTitle, + windowIcon, + undecorated, + resizable, + horizontalOffset, + verticalOffset, + onPreviewKeyEvent, + onKeyEvent, + menu, + content, + ) } } @@ -474,11 +525,12 @@ private fun ApplicationScope.TrayAppImplOriginal( menu: (TrayMenuBuilder.() -> Unit)?, content: @Composable DialogWindowScope.() -> Unit, ) { - val trayAppState = state ?: rememberTrayAppState( - initialWindowSize = windowSize ?: DpSize(300.dp, 200.dp), - initiallyVisible = visibleOnStart, - initialDismissMode = TrayWindowDismissMode.AUTO - ) + val trayAppState = + state ?: rememberTrayAppState( + initialWindowSize = windowSize ?: DpSize(300.dp, 200.dp), + initiallyVisible = visibleOnStart, + initialDismissMode = TrayWindowDismissMode.AUTO, + ) val isVisible by trayAppState.isVisible.collectAsState() val currentWindowSize by trayAppState.windowSize.collectAsState() val dismissMode by trayAppState.dismissMode.collectAsState() @@ -486,16 +538,21 @@ private fun ApplicationScope.TrayAppImplOriginal( val tray = remember { NativeTray() } val isDark = isMenuBarInDarkMode() val contentHash = ComposableIconUtils.calculateContentHash(iconRenderProperties, iconContent) + isDark.hashCode() - val pngIconPath = remember(contentHash) { - ComposableIconUtils.renderComposableToPngFile(iconRenderProperties, iconContent) - } - val windowsIconPath = remember(contentHash) { - if (getOperatingSystem() == WINDOWS) ComposableIconUtils.renderComposableToIcoFile( - iconRenderProperties, - iconContent - ) - else pngIconPath - } + val pngIconPath = + remember(contentHash) { + ComposableIconUtils.renderComposableToPngFile(iconRenderProperties, iconContent) + } + val windowsIconPath = + remember(contentHash) { + if (getOperatingSystem() == WINDOWS) { + ComposableIconUtils.renderComposableToIcoFile( + iconRenderProperties, + iconContent, + ) + } else { + pngIconPath + } + } val menuHash = MenuContentHash.calculateMenuHash(menu) var shouldShowWindow by remember { mutableStateOf(false) } @@ -532,7 +589,10 @@ private fun ApplicationScope.TrayAppImplOriginal( val requestHideExplicit: () -> Unit = { val now = System.currentTimeMillis() val sinceShow = now - lastShownAtMs.get() - debugln { "[TrayApp] requestHideExplicit called, sinceShow=${sinceShow}ms, thread=${Thread.currentThread().name}" } + debugln { + "[TrayApp] requestHideExplicit called, sinceShow=${sinceShow}ms, " + + "thread=${Thread.currentThread().name}" + } if (sinceShow >= minVisibleDurationMs) { trayAppState.hide() lastHiddenAtMs.set(System.currentTimeMillis()) @@ -553,14 +613,22 @@ private fun ApplicationScope.TrayAppImplOriginal( // Read directly from StateFlow for thread-safe cross-thread access val isVisibleNow = trayAppState.isVisible.value val timeSinceLastAction = now - lastPrimaryActionAtMs.get() - debugln { "[TrayApp] primaryAction: isVisibleNow=$isVisibleNow, isVisible(compose)=$isVisible, timeSinceLastAction=${timeSinceLastAction}ms, thread=${Thread.currentThread().name}" } + debugln { + "[TrayApp] primaryAction: isVisibleNow=$isVisibleNow, " + + "isVisible(compose)=$isVisible, " + + "timeSinceLastAction=${timeSinceLastAction}ms, " + + "thread=${Thread.currentThread().name}" + } if (timeSinceLastAction >= toggleDebounceMs) { lastPrimaryActionAtMs.set(now) lastPrimaryActionAt = now if (isVisibleNow) { // On macOS, check if the window is on another Space if (getOperatingSystem() == MACOS) { - val onActiveSpace = runCatching { MacOSWindowManager().isFloatingWindowOnActiveSpace() }.getOrElse { true } + val onActiveSpace = + runCatching { + MacOSWindowManager().isFloatingWindowOnActiveSpace() + }.getOrElse { true } debugln { "[TrayApp] primaryAction: onActiveSpace=$onActiveSpace" } if (!onActiveSpace) { // Window is on another Space → move it to current Space via native API @@ -579,7 +647,10 @@ private fun ApplicationScope.TrayAppImplOriginal( } else { val hiddenAgo = now - lastHiddenAtMs.get() val focusLostAgo = now - lastFocusLostAtMs.get() - debugln { "[TrayApp] primaryAction SHOW path: hiddenAgo=${hiddenAgo}ms, focusLostAgo=${focusLostAgo}ms" } + debugln { + "[TrayApp] primaryAction SHOW path: " + + "hiddenAgo=${hiddenAgo}ms, focusLostAgo=${focusLostAgo}ms" + } if (hiddenAgo >= minHiddenDurationMs) { if ((getOperatingSystem() == WINDOWS || getOperatingSystem() == MACOS) && focusLostAgo < 150) { // ignore immediate re-show after focus loss on Windows/macOS @@ -591,14 +662,18 @@ private fun ApplicationScope.TrayAppImplOriginal( runCatching { val widthPx = currentWindowSize.width.value.toInt() val heightPx = currentWindowSize.height.value.toInt() - pendingPosition = getTrayWindowPositionForInstance( - tray.instanceKey(), widthPx, heightPx, horizontalOffset, verticalOffset - ) + pendingPosition = + getTrayWindowPositionForInstance( + tray.instanceKey(), widthPx, heightPx, horizontalOffset, verticalOffset, + ) } trayAppState.show() } } else { - debugln { "[TrayApp] primaryAction -> SHOW BLOCKED (too soon after hide: ${hiddenAgo}ms < ${minHiddenDurationMs}ms)" } + debugln { + "[TrayApp] primaryAction -> SHOW BLOCKED " + + "(too soon after hide: ${hiddenAgo}ms < ${minHiddenDurationMs}ms)" + } } } } else { @@ -615,37 +690,39 @@ private fun ApplicationScope.TrayAppImplOriginal( val preComputed = pendingPosition pendingPosition = null - val position = if (preComputed != null && preComputed !is WindowPosition.PlatformDefault) { - debugln { "[TrayApp] Using preComputed position: $preComputed" } - preComputed - } else { - // Fallback: poll for position (e.g. initiallyVisible or programmatic show) - // Wait for Windows to finish reorganizing tray icons after adding a new one. - // Windows moves icons around after creation, so we need to wait and re-poll. - debugln { "[TrayApp] No preComputed position, waiting for tray to stabilize..." } - delay(400) // Give Windows time to reorganize tray icons - - val widthPx = currentWindowSize.width.value.toInt() - val heightPx = currentWindowSize.height.value.toInt() - - // On Windows, force a fresh position capture via the native API - if (getOperatingSystem() == WINDOWS) { - debugln { "[TrayApp] Re-capturing tray position from native API..." } - WindowsTrayInitializer.refreshPosition(tray.instanceKey()) - delay(50) // Let the position update propagate - } + val position = + if (preComputed != null && preComputed !is WindowPosition.PlatformDefault) { + debugln { "[TrayApp] Using preComputed position: $preComputed" } + preComputed + } else { + // Fallback: poll for position (e.g. initiallyVisible or programmatic show) + // Wait for Windows to finish reorganizing tray icons after adding a new one. + // Windows moves icons around after creation, so we need to wait and re-poll. + debugln { "[TrayApp] No preComputed position, waiting for tray to stabilize..." } + delay(400) // Give Windows time to reorganize tray icons + + val widthPx = currentWindowSize.width.value.toInt() + val heightPx = currentWindowSize.height.value.toInt() + + // On Windows, force a fresh position capture via the native API + if (getOperatingSystem() == WINDOWS) { + debugln { "[TrayApp] Re-capturing tray position from native API..." } + WindowsTrayInitializer.refreshPosition(tray.instanceKey()) + delay(50) // Let the position update propagate + } - var pos: WindowPosition = WindowPosition.PlatformDefault - val deadline = System.currentTimeMillis() + 3000 - while (pos is WindowPosition.PlatformDefault && System.currentTimeMillis() < deadline) { - pos = getTrayWindowPositionForInstance( - tray.instanceKey(), widthPx, heightPx, horizontalOffset, verticalOffset - ) - debugln { "[TrayApp] Polled position: $pos" } - if (pos is WindowPosition.PlatformDefault) delay(250) + var pos: WindowPosition = WindowPosition.PlatformDefault + val deadline = System.currentTimeMillis() + 3000 + while (pos is WindowPosition.PlatformDefault && System.currentTimeMillis() < deadline) { + pos = + getTrayWindowPositionForInstance( + tray.instanceKey(), widthPx, heightPx, horizontalOffset, verticalOffset, + ) + debugln { "[TrayApp] Polled position: $pos" } + if (pos is WindowPosition.PlatformDefault) delay(250) + } + pos } - pos - } debugln { "[TrayApp] Setting dialogState.position = $position" } dialogState.position = position @@ -710,19 +787,32 @@ private fun ApplicationScope.TrayAppImplOriginal( // Store window reference for Space detection on macOS windowRef = window - try { window.name = WindowVisibilityMonitor.TRAY_DIALOG_NAME } catch (_: Throwable) {} + try { + window.name = WindowVisibilityMonitor.TRAY_DIALOG_NAME + } catch (_: Throwable) { + } runCatching { WindowVisibilityMonitor.recompute() } - debugln { "[TrayApp] Window shown at native position: x=${window.x}, y=${window.y}, dialogState.position=${dialogState.position}" } + debugln { + "[TrayApp] Window shown at native position: x=${window.x}, y=${window.y}, " + + "dialogState.position=${dialogState.position}" + } invokeLater { // Move the popup to the current Space before bringing it to front (macOS) if (getOperatingSystem() == MACOS) { debugln { "[TrayApp] Setting up macOS Space behavior on window..." } val nativeResult = runCatching { MacNativeBridge.nativeSetMoveToActiveSpace() } - debugln { "[TrayApp] nativeSetMoveToActiveSpace: ${nativeResult.exceptionOrNull()?.message ?: "OK"}" } + debugln { + "[TrayApp] nativeSetMoveToActiveSpace: " + + "${nativeResult.exceptionOrNull()?.message ?: "OK"}" + } val moveResult = runCatching { MacOSWindowManager().setMoveToActiveSpace(window) } - debugln { "[TrayApp] setMoveToActiveSpace result=${moveResult.getOrNull()}, error=${moveResult.exceptionOrNull()?.message}" } + debugln { + "[TrayApp] setMoveToActiveSpace " + + "result=${moveResult.getOrNull()}, " + + "error=${moveResult.exceptionOrNull()?.message}" + } } debugln { "[TrayApp] After invokeLater: window at x=${window.x}, y=${window.y}" } runCatching { @@ -732,51 +822,70 @@ private fun ApplicationScope.TrayAppImplOriginal( } } - val focusListener = object : WindowFocusListener { - override fun windowGainedFocus(e: WindowEvent?) = Unit - override fun windowLostFocus(e: WindowEvent?) { - val now = System.currentTimeMillis() - lastFocusLostAtMs.set(now) - lastFocusLostAt = now - debugln { "[TrayApp] windowLostFocus at $now, dismissMode=$dismissMode, thread=${Thread.currentThread().name}" } - if (getOperatingSystem() == WINDOWS && now < autoHideEnabledAt) return - // On macOS, don't auto-hide if the window is not on the active Space. - // A Space switch caused the focus loss — let the primary action handle it. - if (getOperatingSystem() == MACOS) { - val onActiveSpace = runCatching { MacOSWindowManager().isFloatingWindowOnActiveSpace() }.getOrElse { true } - debugln { "[TrayApp] windowLostFocus: onActiveSpace=$onActiveSpace" } - if (!onActiveSpace) return + val focusListener = + object : WindowFocusListener { + override fun windowGainedFocus(e: WindowEvent?) = Unit + + override fun windowLostFocus(e: WindowEvent?) { + val now = System.currentTimeMillis() + lastFocusLostAtMs.set(now) + lastFocusLostAt = now + debugln { + "[TrayApp] windowLostFocus at $now, " + + "dismissMode=$dismissMode, " + + "thread=${Thread.currentThread().name}" + } + if (getOperatingSystem() == WINDOWS && now < autoHideEnabledAt) return + // On macOS, don't auto-hide if the window is not on the active Space. + // A Space switch caused the focus loss — let the primary action handle it. + if (getOperatingSystem() == MACOS) { + val onActiveSpace = + runCatching { + MacOSWindowManager().isFloatingWindowOnActiveSpace() + }.getOrElse { true } + debugln { "[TrayApp] windowLostFocus: onActiveSpace=$onActiveSpace" } + if (!onActiveSpace) return + } + if (dismissMode == TrayWindowDismissMode.AUTO) requestHideExplicit() } - if (dismissMode == TrayWindowDismissMode.AUTO) requestHideExplicit() } - } - val macWatcher = if (dismissMode == TrayWindowDismissMode.AUTO && getOperatingSystem() == MACOS) { - MacOutsideClickWatcher( - windowSupplier = { window }, - onOutsideClick = { invokeLater { requestHideExplicit() } } - ).also { it.start() } - } else null + val macWatcher = + if (dismissMode == TrayWindowDismissMode.AUTO && getOperatingSystem() == MACOS) { + MacOutsideClickWatcher( + windowSupplier = { window }, + onOutsideClick = { invokeLater { requestHideExplicit() } }, + ).also { it.start() } + } else { + null + } val linuxWatcher = if (dismissMode == TrayWindowDismissMode.AUTO && getOperatingSystem() == OperatingSystem.LINUX) { LinuxOutsideClickWatcher( windowSupplier = { window }, - onOutsideClick = { invokeLater { requestHideExplicit() } } + onOutsideClick = { invokeLater { requestHideExplicit() } }, ).also { it.start() } - } else null + } else { + null + } - val windowsWatcher = if (dismissMode == TrayWindowDismissMode.AUTO && getOperatingSystem() == WINDOWS) { - WindowsOutsideClickWatcher( - windowSupplier = { window }, - onOutsideClick = { invokeLater { requestHideExplicit() } } - ).also { it.start() } - } else null + val windowsWatcher = + if (dismissMode == TrayWindowDismissMode.AUTO && getOperatingSystem() == WINDOWS) { + WindowsOutsideClickWatcher( + windowSupplier = { window }, + onOutsideClick = { invokeLater { requestHideExplicit() } }, + ).also { it.start() } + } else { + null + } window.addWindowFocusListener(focusListener) onDispose { window.removeWindowFocusListener(focusListener) - macWatcher?.stop(); linuxWatcher?.stop(); windowsWatcher?.stop() + macWatcher?.stop() + linuxWatcher?.stop() + windowsWatcher?.stop() runCatching { WindowVisibilityMonitor.recompute() } windowRef = null } @@ -786,14 +895,15 @@ private fun ApplicationScope.TrayAppImplOriginal( PersistentAnimatedVisibility( visibleState = visibleState, enter = enterTransition, - exit = exitTransition + exit = exitTransition, ) { Box( - modifier = Modifier - .fillMaxSize() - .graphicsLayer() - // Child can still opt into its own per-node enter/exit if desired: - .animateEnterExit() + modifier = + Modifier + .fillMaxSize() + .graphicsLayer() + // Child can still opt into its own per-node enter/exit if desired: + .animateEnterExit(), ) { content() } } } @@ -823,11 +933,12 @@ private fun ApplicationScope.TrayAppImplLinux( menu: (TrayMenuBuilder.() -> Unit)?, content: @Composable DialogWindowScope.() -> Unit, ) { - val trayAppState = state ?: rememberTrayAppState( - initialWindowSize = windowSize ?: DpSize(300.dp, 200.dp), - initiallyVisible = visibleOnStart, - initialDismissMode = TrayWindowDismissMode.AUTO - ) + val trayAppState = + state ?: rememberTrayAppState( + initialWindowSize = windowSize ?: DpSize(300.dp, 200.dp), + initiallyVisible = visibleOnStart, + initialDismissMode = TrayWindowDismissMode.AUTO, + ) val isVisible by trayAppState.isVisible.collectAsState() val currentWindowSize by trayAppState.windowSize.collectAsState() val dismissMode by trayAppState.dismissMode.collectAsState() @@ -850,11 +961,12 @@ private fun ApplicationScope.TrayAppImplLinux( val minVisibleDurationMs = 350L val minHiddenDurationMs = 250L - val initialPositionForFirstFrame = remember(instanceKey, currentWindowSize, horizontalOffset, verticalOffset) { - val w = currentWindowSize.width.value.toInt() - val h = currentWindowSize.height.value.toInt() - getTrayWindowPositionForInstance(instanceKey, w, h, horizontalOffset, verticalOffset) - } + val initialPositionForFirstFrame = + remember(instanceKey, currentWindowSize, horizontalOffset, verticalOffset) { + val w = currentWindowSize.width.value.toInt() + val h = currentWindowSize.height.value.toInt() + getTrayWindowPositionForInstance(instanceKey, w, h, horizontalOffset, verticalOffset) + } val dialogState = rememberDialogState(position = initialPositionForFirstFrame, size = currentWindowSize) LaunchedEffect(currentWindowSize) { dialogState.size = currentWindowSize } @@ -866,12 +978,14 @@ private fun ApplicationScope.TrayAppImplLinux( val now = System.currentTimeMillis() val sinceShow = now - lastShownAt if (sinceShow >= minVisibleDurationMs) { - trayAppState.hide(); lastHiddenAt = now + trayAppState.hide() + lastHiddenAt = now } else { val wait = (minVisibleDurationMs - sinceShow).coerceAtLeast(0) CoroutineScope(Dispatchers.IO).launch { delay(wait) - trayAppState.hide(); lastHiddenAt = System.currentTimeMillis() + trayAppState.hide() + lastHiddenAt = System.currentTimeMillis() } } } @@ -947,19 +1061,25 @@ private fun ApplicationScope.TrayAppImplLinux( getTrayWindowPositionForInstance(instanceKey, w, h, horizontalOffset, verticalOffset) } - val linuxWatcher = if (dismissMode == TrayWindowDismissMode.AUTO) { - LinuxOutsideClickWatcher( - windowSupplier = { window }, - onOutsideClick = { invokeLater { requestHideExplicit() } } - ).also { it.start() } - } else null - - window.addWindowFocusListener(object : WindowFocusListener { - override fun windowGainedFocus(e: WindowEvent?) = Unit - override fun windowLostFocus(e: WindowEvent?) { - if (dismissMode == TrayWindowDismissMode.AUTO) requestHideExplicit() + val linuxWatcher = + if (dismissMode == TrayWindowDismissMode.AUTO) { + LinuxOutsideClickWatcher( + windowSupplier = { window }, + onOutsideClick = { invokeLater { requestHideExplicit() } }, + ).also { it.start() } + } else { + null } - }) + + window.addWindowFocusListener( + object : WindowFocusListener { + override fun windowGainedFocus(e: WindowEvent?) = Unit + + override fun windowLostFocus(e: WindowEvent?) { + if (dismissMode == TrayWindowDismissMode.AUTO) requestHideExplicit() + } + }, + ) onDispose { linuxWatcher?.stop() } } @@ -968,13 +1088,14 @@ private fun ApplicationScope.TrayAppImplLinux( PersistentAnimatedVisibility( visibleState = visibleState, enter = enterTransition, - exit = exitTransition + exit = exitTransition, ) { Box( - modifier = Modifier - .fillMaxSize() - .graphicsLayer() - .animateEnterExit() + modifier = + Modifier + .fillMaxSize() + .graphicsLayer() + .animateEnterExit(), ) { content() } } } diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/tray/api/TrayAppState.kt b/src/jvmMain/kotlin/com/kdroid/composetray/tray/api/TrayAppState.kt index 13c7772e..fdcea3dc 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/tray/api/TrayAppState.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/tray/api/TrayAppState.kt @@ -1,7 +1,6 @@ package com.kdroid.composetray.tray.api import androidx.compose.runtime.Composable -import androidx.compose.runtime.ExperimentalComposeApi import androidx.compose.runtime.remember import androidx.compose.ui.unit.DpSize import androidx.compose.ui.unit.dp @@ -61,7 +60,10 @@ class TrayAppState( } /** Updates the window size */ - fun setWindowSize(width: androidx.compose.ui.unit.Dp, height: androidx.compose.ui.unit.Dp) { + fun setWindowSize( + width: androidx.compose.ui.unit.Dp, + height: androidx.compose.ui.unit.Dp, + ) { _windowSize.value = DpSize(width, height) } @@ -96,7 +98,7 @@ fun rememberTrayAppState( TrayAppState( initialWindowSize = initialWindowSize, initiallyVisible = initiallyVisible, - initialDismissMode = initialDismissMode + initialDismissMode = initialDismissMode, ) } } diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/tray/api/TrayWindowDismissMode.kt b/src/jvmMain/kotlin/com/kdroid/composetray/tray/api/TrayWindowDismissMode.kt index 17ba223a..55d32279 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/tray/api/TrayWindowDismissMode.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/tray/api/TrayWindowDismissMode.kt @@ -10,11 +10,11 @@ enum class TrayWindowDismissMode { * This is the traditional behavior for tray popup windows. */ AUTO, - + /** * The window remains visible until explicitly hidden via TrayAppState.hide() * or by clicking the tray icon again. * Focus loss and outside clicks do not dismiss the window. */ - MANUAL -} \ No newline at end of file + MANUAL, +} diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/tray/impl/AwtTrayInitializer.kt b/src/jvmMain/kotlin/com/kdroid/composetray/tray/impl/AwtTrayInitializer.kt index 38da21d2..a4ede2a9 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/tray/impl/AwtTrayInitializer.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/tray/impl/AwtTrayInitializer.kt @@ -30,7 +30,7 @@ object AwtTrayInitializer { iconPath: String, tooltip: String, onLeftClick: (() -> Unit)?, - menuContent: (TrayMenuBuilder.() -> Unit)? + menuContent: (TrayMenuBuilder.() -> Unit)?, ) { if (!isSupported()) { throw IllegalStateException("System tray is not supported.") @@ -45,33 +45,36 @@ object AwtTrayInitializer { // Create the tray icon val trayImage = Toolkit.getDefaultToolkit().getImage(iconPath) - val newTrayIcon = TrayIcon(trayImage, tooltip, popupMenu).apply { - isImageAutoSize = true + val newTrayIcon = + TrayIcon(trayImage, tooltip, popupMenu).apply { + isImageAutoSize = true - // On macOS, we cannot easily override the default left-click behavior - // when a popup menu is attached. We have two options: - // 1. Accept that left-click opens the menu (macOS convention) - // 2. Use a more complex native implementation via JNA + // On macOS, we cannot easily override the default left-click behavior + // when a popup menu is attached. We have two options: + // 1. Accept that left-click opens the menu (macOS convention) + // 2. Use a more complex native implementation via JNA - if (getOperatingSystem() == OperatingSystem.MACOS) { - // For macOS, add primary action as the first menu item if both menu and action exist - if (onLeftClick != null && menuContent != null) { - // The primary action will be added as the first menu item - // This is handled in the menu building phase - } - } else { - // For other platforms, handle left-click normally - onLeftClick?.let { clickAction -> - addMouseListener(object : MouseAdapter() { - override fun mouseClicked(e: MouseEvent) { - if (e.button == MouseEvent.BUTTON1) { - clickAction() - } - } - }) + if (getOperatingSystem() == OperatingSystem.MACOS) { + // For macOS, add primary action as the first menu item if both menu and action exist + if (onLeftClick != null && menuContent != null) { + // The primary action will be added as the first menu item + // This is handled in the menu building phase + } + } else { + // For other platforms, handle left-click normally + onLeftClick?.let { clickAction -> + addMouseListener( + object : MouseAdapter() { + override fun mouseClicked(e: MouseEvent) { + if (e.button == MouseEvent.BUTTON1) { + clickAction() + } + } + }, + ) + } } } - } // Add the menu content if specified menuContent?.let { @@ -97,7 +100,7 @@ object AwtTrayInitializer { iconPath: String, tooltip: String, onLeftClick: (() -> Unit)?, - menuContent: (TrayMenuBuilder.() -> Unit)? + menuContent: (TrayMenuBuilder.() -> Unit)?, ) { dispose() initialize(iconPath, tooltip, onLeftClick, menuContent) @@ -112,4 +115,4 @@ object AwtTrayInitializer { trayIcon = null } } -} \ No newline at end of file +} diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/tray/impl/LinuxTrayInitializer.kt b/src/jvmMain/kotlin/com/kdroid/composetray/tray/impl/LinuxTrayInitializer.kt index 5a4c657e..7ec0c8ea 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/tray/impl/LinuxTrayInitializer.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/tray/impl/LinuxTrayInitializer.kt @@ -7,7 +7,6 @@ import java.util.concurrent.locks.ReentrantLock import kotlin.concurrent.withLock object LinuxTrayInitializer { - private const val DEFAULT_ID: String = "_default" private val trayMenuImpls: MutableMap = mutableMapOf() @@ -20,7 +19,7 @@ object LinuxTrayInitializer { iconPath: String, tooltip: String, onLeftClick: (() -> Unit)? = null, - menuContent: (TrayMenuBuilder.() -> Unit)? = null + menuContent: (TrayMenuBuilder.() -> Unit)? = null, ) { lock.withLock { val existing = linuxTrayManagers[id] @@ -28,11 +27,14 @@ object LinuxTrayInitializer { val manager = LinuxTrayManager(id, iconPath, tooltip, onLeftClick) linuxTrayManagers[id] = manager - val menuImpl = if (menuContent != null) { - LinuxTrayMenuBuilderImpl(iconPath, tooltip, onLeftClick, trayManager = manager).apply { - menuContent() + val menuImpl = + if (menuContent != null) { + LinuxTrayMenuBuilderImpl(iconPath, tooltip, onLeftClick, trayManager = manager).apply { + menuContent() + } + } else { + null } - } else null menuImpl?.let { impl -> trayMenuImpls[id]?.dispose() trayMenuImpls[id] = impl @@ -52,7 +54,7 @@ object LinuxTrayInitializer { iconPath: String, tooltip: String, onLeftClick: (() -> Unit)? = null, - menuContent: (TrayMenuBuilder.() -> Unit)? = null + menuContent: (TrayMenuBuilder.() -> Unit)? = null, ) { lock.withLock { val manager = linuxTrayManagers[id] @@ -61,14 +63,18 @@ object LinuxTrayInitializer { return } - val newMenuItems = if (menuContent != null) { - val newImpl = LinuxTrayMenuBuilderImpl(iconPath, tooltip, onLeftClick, trayManager = manager).apply { - menuContent() + val newMenuItems = + if (menuContent != null) { + val newImpl = + LinuxTrayMenuBuilderImpl(iconPath, tooltip, onLeftClick, trayManager = manager).apply { + menuContent() + } + trayMenuImpls[id]?.dispose() + trayMenuImpls[id] = newImpl + newImpl.build() + } else { + null } - trayMenuImpls[id]?.dispose() - trayMenuImpls[id] = newImpl - newImpl.build() - } else null manager.update(iconPath, tooltip, onLeftClick, newMenuItems) } @@ -84,11 +90,17 @@ object LinuxTrayInitializer { menuImpl = trayMenuImpls.remove(id) } // Dispose menu builder immediately (cheap) - try { menuImpl?.dispose() } catch (_: Throwable) {} + try { + menuImpl?.dispose() + } catch (_: Throwable) { + } // Stop tray asynchronously to avoid freezing the UI thread and avoid holding the lock manager?.let { m -> Thread({ - try { m.stopTray() } catch (_: Throwable) {} + try { + m.stopTray() + } catch (_: Throwable) { + } }, "LinuxTray-Dispose-$id").apply { isDaemon = true start() @@ -97,11 +109,19 @@ object LinuxTrayInitializer { } // Backward-compatible single-tray API - fun initialize(iconPath: String, tooltip: String, onLeftClick: (() -> Unit)? = null, menuContent: (TrayMenuBuilder.() -> Unit)? = null) = - initialize(DEFAULT_ID, iconPath, tooltip, onLeftClick, menuContent) + fun initialize( + iconPath: String, + tooltip: String, + onLeftClick: (() -> Unit)? = null, + menuContent: (TrayMenuBuilder.() -> Unit)? = null, + ) = initialize(DEFAULT_ID, iconPath, tooltip, onLeftClick, menuContent) - fun update(iconPath: String, tooltip: String, onLeftClick: (() -> Unit)? = null, menuContent: (TrayMenuBuilder.() -> Unit)? = null) = - update(DEFAULT_ID, iconPath, tooltip, onLeftClick, menuContent) + fun update( + iconPath: String, + tooltip: String, + onLeftClick: (() -> Unit)? = null, + menuContent: (TrayMenuBuilder.() -> Unit)? = null, + ) = update(DEFAULT_ID, iconPath, tooltip, onLeftClick, menuContent) fun dispose() = dispose(DEFAULT_ID) -} \ No newline at end of file +} diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/tray/impl/MacTrayInitializer.kt b/src/jvmMain/kotlin/com/kdroid/composetray/tray/impl/MacTrayInitializer.kt index 5b0dd361..02e1c37e 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/tray/impl/MacTrayInitializer.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/tray/impl/MacTrayInitializer.kt @@ -5,7 +5,6 @@ import com.kdroid.composetray.menu.api.TrayMenuBuilder import com.kdroid.composetray.menu.impl.MacTrayMenuBuilderImpl object MacTrayInitializer { - private const val DEFAULT_ID: String = "_default" // Manage multiple tray managers and builders by ID @@ -25,7 +24,7 @@ object MacTrayInitializer { iconPath: String, tooltip: String, onLeftClick: (() -> Unit)? = null, - menuContent: (TrayMenuBuilder.() -> Unit)? = null + menuContent: (TrayMenuBuilder.() -> Unit)? = null, ) { var manager = trayManagers[id] if (manager == null) { @@ -34,14 +33,15 @@ object MacTrayInitializer { trayManagers[id] = manager // Build menu for this manager - val builder = MacTrayMenuBuilderImpl( - iconPath, - tooltip, - onLeftClick, - trayManager = manager - ).apply { - menuContent?.invoke(this) - } + val builder = + MacTrayMenuBuilderImpl( + iconPath, + tooltip, + onLeftClick, + trayManager = manager, + ).apply { + menuContent?.invoke(this) + } // Replace old builder for this ID if any trayMenuBuilders.remove(id)?.dispose() trayMenuBuilders[id] = builder @@ -64,7 +64,7 @@ object MacTrayInitializer { iconPath: String, tooltip: String, onLeftClick: (() -> Unit)? = null, - menuContent: (TrayMenuBuilder.() -> Unit)? = null + menuContent: (TrayMenuBuilder.() -> Unit)? = null, ) { val manager = trayManagers[id] if (manager == null) { @@ -74,25 +74,33 @@ object MacTrayInitializer { } // Rebuild menu if content provided - val newMenuItems = if (menuContent != null) { - val builder = MacTrayMenuBuilderImpl( - iconPath, - tooltip, - onLeftClick, - trayManager = manager - ).apply { - menuContent() + val newMenuItems = + if (menuContent != null) { + val builder = + MacTrayMenuBuilderImpl( + iconPath, + tooltip, + onLeftClick, + trayManager = manager, + ).apply { + menuContent() + } + trayMenuBuilders.remove(id)?.dispose() + trayMenuBuilders[id] = builder + builder.build() + } else { + null } - trayMenuBuilders.remove(id)?.dispose() - trayMenuBuilders[id] = builder - builder.build() - } else null manager.update(iconPath, tooltip, onLeftClick, newMenuItems) } @Synchronized - fun setAppearanceIcons(id: String, lightIconPath: String, darkIconPath: String) { + fun setAppearanceIcons( + id: String, + lightIconPath: String, + darkIconPath: String, + ) { trayManagers[id]?.setAppearanceIcons(lightIconPath, darkIconPath) } @@ -103,11 +111,19 @@ object MacTrayInitializer { } // Backward-compatible API for existing callers (single default tray) - fun initialize(iconPath: String, tooltip: String, onLeftClick: (() -> Unit)? = null, menuContent: (TrayMenuBuilder.() -> Unit)? = null) = - initialize(DEFAULT_ID, iconPath, tooltip, onLeftClick, menuContent) + fun initialize( + iconPath: String, + tooltip: String, + onLeftClick: (() -> Unit)? = null, + menuContent: (TrayMenuBuilder.() -> Unit)? = null, + ) = initialize(DEFAULT_ID, iconPath, tooltip, onLeftClick, menuContent) - fun update(iconPath: String, tooltip: String, onLeftClick: (() -> Unit)? = null, menuContent: (TrayMenuBuilder.() -> Unit)? = null) = - update(DEFAULT_ID, iconPath, tooltip, onLeftClick, menuContent) + fun update( + iconPath: String, + tooltip: String, + onLeftClick: (() -> Unit)? = null, + menuContent: (TrayMenuBuilder.() -> Unit)? = null, + ) = update(DEFAULT_ID, iconPath, tooltip, onLeftClick, menuContent) fun dispose() = dispose(DEFAULT_ID) } diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/tray/impl/WindowsTrayInitializer.kt b/src/jvmMain/kotlin/com/kdroid/composetray/tray/impl/WindowsTrayInitializer.kt index a257a096..a467721f 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/tray/impl/WindowsTrayInitializer.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/tray/impl/WindowsTrayInitializer.kt @@ -5,7 +5,6 @@ import com.kdroid.composetray.menu.api.TrayMenuBuilder import com.kdroid.composetray.menu.impl.WindowsTrayMenuBuilderImpl object WindowsTrayInitializer { - private const val DEFAULT_ID: String = "_default" // Manage multiple tray managers by ID to allow multiple tray icons @@ -17,11 +16,12 @@ object WindowsTrayInitializer { iconPath: String, tooltip: String, onLeftClick: (() -> Unit)? = null, - menuContent: (TrayMenuBuilder.() -> Unit)? = null + menuContent: (TrayMenuBuilder.() -> Unit)? = null, ) { - val menuItems = WindowsTrayMenuBuilderImpl(iconPath, tooltip, onLeftClick).apply { - menuContent?.let { it() } - }.build() + val menuItems = + WindowsTrayMenuBuilderImpl(iconPath, tooltip, onLeftClick).apply { + menuContent?.let { it() } + }.build() val manager = trayManagers[id] if (manager == null) { @@ -39,7 +39,7 @@ object WindowsTrayInitializer { iconPath: String, tooltip: String, onLeftClick: (() -> Unit)? = null, - menuContent: (TrayMenuBuilder.() -> Unit)? = null + menuContent: (TrayMenuBuilder.() -> Unit)? = null, ) { // Same as initialize - it will handle both cases per ID initialize(id, iconPath, tooltip, onLeftClick, menuContent) @@ -60,11 +60,19 @@ object WindowsTrayInitializer { } // Backward-compatible API for existing callers (single default tray) - fun initialize(iconPath: String, tooltip: String, onLeftClick: (() -> Unit)? = null, menuContent: (TrayMenuBuilder.() -> Unit)? = null) = - initialize(DEFAULT_ID, iconPath, tooltip, onLeftClick, menuContent) + fun initialize( + iconPath: String, + tooltip: String, + onLeftClick: (() -> Unit)? = null, + menuContent: (TrayMenuBuilder.() -> Unit)? = null, + ) = initialize(DEFAULT_ID, iconPath, tooltip, onLeftClick, menuContent) - fun update(iconPath: String, tooltip: String, onLeftClick: (() -> Unit)? = null, menuContent: (TrayMenuBuilder.() -> Unit)? = null) = - update(DEFAULT_ID, iconPath, tooltip, onLeftClick, menuContent) + fun update( + iconPath: String, + tooltip: String, + onLeftClick: (() -> Unit)? = null, + menuContent: (TrayMenuBuilder.() -> Unit)? = null, + ) = update(DEFAULT_ID, iconPath, tooltip, onLeftClick, menuContent) fun dispose() = dispose(DEFAULT_ID) -} \ No newline at end of file +} diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/utils/AppId.kt b/src/jvmMain/kotlin/com/kdroid/composetray/utils/AppIdProvider.kt similarity index 100% rename from src/jvmMain/kotlin/com/kdroid/composetray/utils/AppId.kt rename to src/jvmMain/kotlin/com/kdroid/composetray/utils/AppIdProvider.kt diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/utils/ComposableIconUtils.kt b/src/jvmMain/kotlin/com/kdroid/composetray/utils/ComposableIconUtils.kt index ff3b023d..ab11958e 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/utils/ComposableIconUtils.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/utils/ComposableIconUtils.kt @@ -3,7 +3,12 @@ package com.kdroid.composetray.utils import androidx.compose.runtime.Composable import androidx.compose.ui.ImageComposeScene import kotlinx.coroutines.Dispatchers -import org.jetbrains.skia.* +import org.jetbrains.skia.Bitmap +import org.jetbrains.skia.EncodedImageFormat +import org.jetbrains.skia.FilterMipmap +import org.jetbrains.skia.FilterMode +import org.jetbrains.skia.Image +import org.jetbrains.skia.MipmapMode import java.io.File import java.util.zip.CRC32 @@ -11,7 +16,6 @@ import java.util.zip.CRC32 * Utility functions for rendering Composable icons to image files for use in system tray. */ object ComposableIconUtils { - /** * Renders a Composable to a PNG file and returns the path to the file. * @@ -22,7 +26,7 @@ object ComposableIconUtils { */ fun renderComposableToPngFile( iconRenderProperties: IconRenderProperties, - content: @Composable () -> Unit + content: @Composable () -> Unit, ): String { val tempFile = createTempFile(suffix = ".png") val pngData = renderComposableToPngBytes(iconRenderProperties, content) @@ -43,7 +47,7 @@ object ComposableIconUtils { */ fun renderComposableToPngBytes( iconRenderProperties: IconRenderProperties, - content: @Composable () -> Unit + content: @Composable () -> Unit, ): ByteArray { var scene: ImageComposeScene? = null var renderedIcon: Image? = null @@ -53,14 +57,15 @@ object ComposableIconUtils { try { // Try to create and render the scene try { - scene = ImageComposeScene( - width = iconRenderProperties.sceneWidth, - height = iconRenderProperties.sceneHeight, - density = iconRenderProperties.sceneDensity, - coroutineContext = Dispatchers.Unconfined - ) { - content() - } + scene = + ImageComposeScene( + width = iconRenderProperties.sceneWidth, + height = iconRenderProperties.sceneHeight, + density = iconRenderProperties.sceneDensity, + coroutineContext = Dispatchers.Unconfined, + ) { + content() + } renderedIcon = scene.render() } catch (e: Exception) { @@ -70,7 +75,8 @@ object ComposableIconUtils { // Check if it's a DirectX error on Windows if (errorMessage.contains("DirectX12", ignoreCase = true) || - errorMessage.contains("Failed to choose DirectX12 adapter", ignoreCase = true)) { + errorMessage.contains("Failed to choose DirectX12 adapter", ignoreCase = true) + ) { errorln { "[ComposableIconUtils] DirectX12 not available on this system. Scene rendering failed." } } @@ -78,25 +84,27 @@ object ComposableIconUtils { throw e } - val iconData = if (iconRenderProperties.requiresScaling) { - scaledBitmap = Bitmap().apply { - allocN32Pixels(iconRenderProperties.targetWidth, iconRenderProperties.targetHeight) + val iconData = + if (iconRenderProperties.requiresScaling) { + scaledBitmap = + Bitmap().apply { + allocN32Pixels(iconRenderProperties.targetWidth, iconRenderProperties.targetHeight) + } + + renderedIcon.scalePixels( + scaledBitmap.peekPixels()!!, + FilterMipmap(FilterMode.LINEAR, MipmapMode.LINEAR), + true, + ) + + scaledImage = Image.makeFromBitmap(scaledBitmap) + scaledImage.encodeToData(EncodedImageFormat.PNG) + ?: throw Exception("Failed to encode scaled image to PNG") + } else { + renderedIcon.encodeToData(EncodedImageFormat.PNG) + ?: throw Exception("Failed to encode image to PNG") } - renderedIcon.scalePixels( - scaledBitmap.peekPixels()!!, - FilterMipmap(FilterMode.LINEAR, MipmapMode.LINEAR), - true - ) - - scaledImage = Image.makeFromBitmap(scaledBitmap) - scaledImage.encodeToData(EncodedImageFormat.PNG) - ?: throw Exception("Failed to encode scaled image to PNG") - } else { - renderedIcon.encodeToData(EncodedImageFormat.PNG) - ?: throw Exception("Failed to encode image to PNG") - } - return iconData.bytes } finally { // Ensure proper cleanup @@ -121,7 +129,7 @@ object ComposableIconUtils { */ fun renderComposableToIcoFile( iconRenderProperties: IconRenderProperties, - content: @Composable (() -> Unit) + content: @Composable (() -> Unit), ): String { val tempFile = createTempFile(suffix = ".ico") val icoData = renderComposableToIcoBytes(iconRenderProperties, content) @@ -141,7 +149,7 @@ object ComposableIconUtils { */ fun renderComposableToIcoBytes( iconRenderProperties: IconRenderProperties, - content: @Composable () -> Unit + content: @Composable () -> Unit, ): ByteArray { // First render to PNG format (which is supported) val pngBytes = renderComposableToPngBytes(iconRenderProperties, content) @@ -193,7 +201,10 @@ object ComposableIconUtils { /** * Creates a temporary file that will be deleted when the JVM exits. */ - private fun createTempFile(prefix: String = "tray_icon_", suffix: String): File { + private fun createTempFile( + prefix: String = "tray_icon_", + suffix: String, + ): File { val tempFile = File.createTempFile(prefix, suffix) tempFile.deleteOnExit() return tempFile @@ -210,7 +221,7 @@ object ComposableIconUtils { @Composable fun calculateContentHash( iconRenderProperties: IconRenderProperties, - content: @Composable () -> Unit + content: @Composable () -> Unit, ): Long { return try { // Render the composable to PNG bytes @@ -226,4 +237,4 @@ object ComposableIconUtils { System.currentTimeMillis() } } -} \ No newline at end of file +} diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/utils/DarkModeDetector.kt b/src/jvmMain/kotlin/com/kdroid/composetray/utils/DarkModeDetector.kt index 9cc924d7..0f1e02c8 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/utils/DarkModeDetector.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/utils/DarkModeDetector.kt @@ -5,9 +5,11 @@ import androidx.compose.runtime.DisposableEffect import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import com.kdroid.composetray.lib.mac.MacOSMenuBarThemeDetector -import io.github.kdroidfilter.platformtools.LinuxDesktopEnvironment -import io.github.kdroidfilter.platformtools.OperatingSystem.* import io.github.kdroidfilter.nucleus.darkmodedetector.isSystemInDarkMode +import io.github.kdroidfilter.platformtools.LinuxDesktopEnvironment +import io.github.kdroidfilter.platformtools.OperatingSystem.LINUX +import io.github.kdroidfilter.platformtools.OperatingSystem.MACOS +import io.github.kdroidfilter.platformtools.OperatingSystem.WINDOWS import io.github.kdroidfilter.platformtools.detectLinuxDesktopEnvironment import io.github.kdroidfilter.platformtools.getOperatingSystem import java.util.function.Consumer @@ -17,15 +19,16 @@ fun isMenuBarInDarkMode(): Boolean { return when (getOperatingSystem()) { MACOS -> isMacOsMenuBarInDarkMode() WINDOWS -> isSystemInDarkMode() - LINUX -> when (detectLinuxDesktopEnvironment()) { - LinuxDesktopEnvironment.GNOME -> true - LinuxDesktopEnvironment.KDE -> isSystemInDarkMode() - LinuxDesktopEnvironment.XFCE -> true - LinuxDesktopEnvironment.CINNAMON -> true - LinuxDesktopEnvironment.MATE -> true - LinuxDesktopEnvironment.UNKNOWN -> true - null -> isSystemInDarkMode() - } + LINUX -> + when (detectLinuxDesktopEnvironment()) { + LinuxDesktopEnvironment.GNOME -> true + LinuxDesktopEnvironment.KDE -> isSystemInDarkMode() + LinuxDesktopEnvironment.XFCE -> true + LinuxDesktopEnvironment.CINNAMON -> true + LinuxDesktopEnvironment.MATE -> true + LinuxDesktopEnvironment.UNKNOWN -> true + null -> isSystemInDarkMode() + } else -> true } } @@ -34,13 +37,14 @@ fun isMenuBarInDarkMode(): Boolean { internal fun isMacOsMenuBarInDarkMode(): Boolean { val darkModeState = remember { mutableStateOf(MacOSMenuBarThemeDetector.isDark()) } DisposableEffect(Unit) { - val listener = Consumer { newValue -> - darkModeState.value = newValue - } + val listener = + Consumer { newValue -> + darkModeState.value = newValue + } MacOSMenuBarThemeDetector.registerListener(listener) onDispose { MacOSMenuBarThemeDetector.removeListener(listener) } } return darkModeState.value -} \ No newline at end of file +} diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/utils/DebugLn.kt b/src/jvmMain/kotlin/com/kdroid/composetray/utils/DebugLn.kt index 6586ec18..1a001d16 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/utils/DebugLn.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/utils/DebugLn.kt @@ -18,16 +18,19 @@ var composeNativeTrayLoggingLevel: ComposeNativeTrayLoggingLevel = VERBOSE class ComposeNativeTrayLoggingLevel private constructor( private val priority: Int, ) : Comparable { - override fun compareTo(other: ComposeNativeTrayLoggingLevel): Int { return priority.compareTo(other.priority) } companion object { @JvmField val VERBOSE = ComposeNativeTrayLoggingLevel(0) + @JvmField val DEBUG = ComposeNativeTrayLoggingLevel(1) + @JvmField val INFO = ComposeNativeTrayLoggingLevel(2) + @JvmField val WARN = ComposeNativeTrayLoggingLevel(3) + @JvmField val ERROR = ComposeNativeTrayLoggingLevel(4) } } @@ -75,6 +78,9 @@ internal fun errorln(message: () -> String) { } } -private fun println(message: String, color: String) { +private fun println( + message: String, + color: String, +) { println(color + message + COLOR_RESET) -} \ No newline at end of file +} diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/utils/IconRenderProperties.kt b/src/jvmMain/kotlin/com/kdroid/composetray/utils/IconRenderProperties.kt index eb004ae4..413e3e42 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/utils/IconRenderProperties.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/utils/IconRenderProperties.kt @@ -4,7 +4,6 @@ import androidx.compose.ui.unit.Density import io.github.kdroidfilter.platformtools.OperatingSystem import io.github.kdroidfilter.platformtools.getOperatingSystem - /** * Properties for rendering a Composable icon. * @@ -19,12 +18,11 @@ data class IconRenderProperties( val sceneHeight: Int = 192, val sceneDensity: Density = Density(2f), val targetWidth: Int = 192, - val targetHeight: Int = 192 + val targetHeight: Int = 192, ) { val requiresScaling = sceneWidth != targetWidth || sceneHeight != targetHeight companion object { - /** * Provides an [IconRenderProperties] configured for the current operating system. * @@ -41,21 +39,22 @@ data class IconRenderProperties( fun forCurrentOperatingSystem( sceneWidth: Int = 192, sceneHeight: Int = 192, - density: Density = Density(2f) + density: Density = Density(2f), ): IconRenderProperties { - val (targetWidth, targetHeight) = when (getOperatingSystem()) { - OperatingSystem.WINDOWS -> 32 to 32 - OperatingSystem.MACOS -> 44 to 44 - OperatingSystem.LINUX -> 24 to 24 - else -> sceneWidth to sceneHeight - } + val (targetWidth, targetHeight) = + when (getOperatingSystem()) { + OperatingSystem.WINDOWS -> 32 to 32 + OperatingSystem.MACOS -> 44 to 44 + OperatingSystem.LINUX -> 24 to 24 + else -> sceneWidth to sceneHeight + } return IconRenderProperties( sceneWidth = sceneWidth, sceneHeight = sceneHeight, sceneDensity = density, targetWidth = targetWidth, - targetHeight = targetHeight + targetHeight = targetHeight, ) } @@ -70,13 +69,13 @@ data class IconRenderProperties( fun withoutScalingAndAliasing( sceneWidth: Int = 192, sceneHeight: Int = 192, - density: Density = Density(2f) + density: Density = Density(2f), ) = IconRenderProperties( sceneWidth = sceneWidth, sceneHeight = sceneHeight, sceneDensity = density, targetWidth = sceneWidth, - targetHeight = sceneHeight + targetHeight = sceneHeight, ) /** @@ -96,22 +95,23 @@ data class IconRenderProperties( fun forMenuItem( sceneWidth: Int = 64, sceneHeight: Int = 64, - density: Density = Density(2f) + density: Density = Density(2f), ): IconRenderProperties { - val (targetWidth, targetHeight) = when (getOperatingSystem()) { - OperatingSystem.WINDOWS -> 16 to 16 - OperatingSystem.MACOS -> 16 to 16 - OperatingSystem.LINUX -> 16 to 16 - else -> 16 to 16 - } + val (targetWidth, targetHeight) = + when (getOperatingSystem()) { + OperatingSystem.WINDOWS -> 16 to 16 + OperatingSystem.MACOS -> 16 to 16 + OperatingSystem.LINUX -> 16 to 16 + else -> 16 to 16 + } return IconRenderProperties( sceneWidth = sceneWidth, sceneHeight = sceneHeight, sceneDensity = density, targetWidth = targetWidth, - targetHeight = targetHeight + targetHeight = targetHeight, ) } } -} \ No newline at end of file +} diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/utils/JarResourceExtractor.kt b/src/jvmMain/kotlin/com/kdroid/composetray/utils/JarResourceExtractor.kt index 0a5b1ab5..3a4246a0 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/utils/JarResourceExtractor.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/utils/JarResourceExtractor.kt @@ -11,7 +11,6 @@ import java.security.MessageDigest import java.util.jar.JarFile import kotlin.io.path.createTempFile - /** * Extracts a specified file or entry from a JAR archive to a temporary file. * If the specified file or entry is already extracted and matches the content, the existing temporary file is returned. @@ -31,11 +30,12 @@ internal fun extractToTempIfDifferent(jarPath: String): File? { val encodedJarFilePath = correctedJarFilePath.replace(" ", "%20") // Convert the path to File via URI - val jarFile = try { - File(URI(encodedJarFilePath)) - } catch (e: IllegalArgumentException) { - File(correctedJarFilePath.removePrefix("file:")) - } + val jarFile = + try { + File(URI(encodedJarFilePath)) + } catch (e: IllegalArgumentException) { + File(correctedJarFilePath.removePrefix("file:")) + } // Check if the file exists if (!jarFile.exists()) { @@ -46,9 +46,10 @@ internal fun extractToTempIfDifferent(jarPath: String): File? { // If the file is not a JAR, handle it differently if (!correctedJarFilePath.endsWith(".jar")) { - val tempFile = createTempFile("extracted_", ".tmp").toFile().apply { - deleteOnExit() - } + val tempFile = + createTempFile("extracted_", ".tmp").toFile().apply { + deleteOnExit() + } // Copy the file directly if it is not a JAR Files.copy(jarFile.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING) @@ -60,9 +61,10 @@ internal fun extractToTempIfDifferent(jarPath: String): File? { val entry = jar.getJarEntry(entryPath) ?: return null // Create a temporary file to store the extracted resource - val tempFile = createTempFile("extracted_", ".tmp").toFile().apply { - deleteOnExit() - } + val tempFile = + createTempFile("extracted_", ".tmp").toFile().apply { + deleteOnExit() + } // Check if the temporary file already exists and compare the hash if (tempFile.exists()) { @@ -85,7 +87,6 @@ internal fun extractToTempIfDifferent(jarPath: String): File? { } } - // Extension to calculate SHA-256 of a file private fun File.sha256(): String = inputStream().use { it.sha256() } diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/utils/MenuContentHash.kt b/src/jvmMain/kotlin/com/kdroid/composetray/utils/MenuContentHash.kt index da89a9e5..abc749e4 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/utils/MenuContentHash.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/utils/MenuContentHash.kt @@ -1,12 +1,10 @@ package com.kdroid.composetray.utils import androidx.compose.runtime.Composable -import androidx.compose.runtime.currentComposer import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.painter.Painter import androidx.compose.ui.graphics.vector.ImageVector import com.kdroid.composetray.menu.api.TrayMenuBuilder -import com.kdroid.composetray.utils.IconRenderProperties import org.jetbrains.compose.resources.DrawableResource import java.security.MessageDigest @@ -14,7 +12,6 @@ import java.security.MessageDigest * Utility class for calculating a hash of menu content to detect changes */ object MenuContentHash { - /** * Calculates a hash of the menu content by capturing the menu structure * This function should be called from a @Composable context to track state changes @@ -41,37 +38,41 @@ object MenuContentHash { private class CapturingMenuBuilder : TrayMenuBuilder { private val operations = mutableListOf() - override fun Item(label: String, isEnabled: Boolean, onClick: () -> Unit) { + override fun Item( + label: String, + isEnabled: Boolean, + onClick: () -> Unit, + ) { operations.add("Item:$label:$isEnabled") } - + override fun Item( label: String, iconContent: @Composable () -> Unit, iconRenderProperties: IconRenderProperties, isEnabled: Boolean, - onClick: () -> Unit + onClick: () -> Unit, ) { operations.add("ItemWithComposableIcon:$label:$isEnabled") } - + override fun Item( label: String, icon: ImageVector, iconTint: Color?, iconRenderProperties: IconRenderProperties, isEnabled: Boolean, - onClick: () -> Unit + onClick: () -> Unit, ) { operations.add("ItemWithImageVectorIcon:$label:${iconTint != null}:$isEnabled:${icon.hashCode()}") } - + override fun Item( label: String, icon: Painter, iconRenderProperties: IconRenderProperties, isEnabled: Boolean, - onClick: () -> Unit + onClick: () -> Unit, ) { operations.add("ItemWithPainterIcon:$label:$isEnabled:${icon.hashCode()}") } @@ -81,7 +82,7 @@ object MenuContentHash { icon: DrawableResource, iconRenderProperties: IconRenderProperties, isEnabled: Boolean, - onClick: () -> Unit + onClick: () -> Unit, ) { operations.add("ItemWithDrawableIcon:$label:$isEnabled:${icon.hashCode()}") } @@ -90,22 +91,22 @@ object MenuContentHash { label: String, checked: Boolean, onCheckedChange: (Boolean) -> Unit, - isEnabled: Boolean + isEnabled: Boolean, ) { operations.add("CheckableItem:$label:$checked:$isEnabled") } - + override fun CheckableItem( label: String, iconContent: @Composable () -> Unit, iconRenderProperties: IconRenderProperties, checked: Boolean, onCheckedChange: (Boolean) -> Unit, - isEnabled: Boolean + isEnabled: Boolean, ) { operations.add("CheckableItemWithComposableIcon:$label:$checked:$isEnabled") } - + override fun CheckableItem( label: String, icon: ImageVector, @@ -113,18 +114,20 @@ object MenuContentHash { iconRenderProperties: IconRenderProperties, checked: Boolean, onCheckedChange: (Boolean) -> Unit, - isEnabled: Boolean + isEnabled: Boolean, ) { - operations.add("CheckableItemWithImageVectorIcon:$label:${iconTint != null}:$checked:$isEnabled:${icon.hashCode()}") + operations.add( + "CheckableItemWithImageVectorIcon:$label:${iconTint != null}:$checked:$isEnabled:${icon.hashCode()}", + ) } - + override fun CheckableItem( label: String, icon: Painter, iconRenderProperties: IconRenderProperties, checked: Boolean, onCheckedChange: (Boolean) -> Unit, - isEnabled: Boolean + isEnabled: Boolean, ) { operations.add("CheckableItemWithPainterIcon:$label:$checked:$isEnabled:${icon.hashCode()}") } @@ -135,7 +138,7 @@ object MenuContentHash { iconRenderProperties: IconRenderProperties, checked: Boolean, onCheckedChange: (Boolean) -> Unit, - isEnabled: Boolean + isEnabled: Boolean, ) { operations.add("CheckableItemWithDrawableIcon:$label:$checked:$isEnabled:${icon.hashCode()}") } @@ -143,7 +146,7 @@ object MenuContentHash { override fun SubMenu( label: String, isEnabled: Boolean, - submenuContent: (TrayMenuBuilder.() -> Unit)? + submenuContent: (TrayMenuBuilder.() -> Unit)?, ) { operations.add("SubMenu:$label:$isEnabled") if (submenuContent != null) { @@ -152,13 +155,13 @@ object MenuContentHash { operations.add("SubMenuEnd") } } - + override fun SubMenu( label: String, iconContent: @Composable () -> Unit, iconRenderProperties: IconRenderProperties, isEnabled: Boolean, - submenuContent: (TrayMenuBuilder.() -> Unit)? + submenuContent: (TrayMenuBuilder.() -> Unit)?, ) { operations.add("SubMenuWithComposableIcon:$label:$isEnabled") if (submenuContent != null) { @@ -167,14 +170,14 @@ object MenuContentHash { operations.add("SubMenuEnd") } } - + override fun SubMenu( label: String, icon: ImageVector, iconTint: Color?, iconRenderProperties: IconRenderProperties, isEnabled: Boolean, - submenuContent: (TrayMenuBuilder.() -> Unit)? + submenuContent: (TrayMenuBuilder.() -> Unit)?, ) { operations.add("SubMenuWithImageVectorIcon:$label:${iconTint != null}:$isEnabled:${icon.hashCode()}") if (submenuContent != null) { @@ -183,13 +186,13 @@ object MenuContentHash { operations.add("SubMenuEnd") } } - + override fun SubMenu( label: String, icon: Painter, iconRenderProperties: IconRenderProperties, isEnabled: Boolean, - submenuContent: (TrayMenuBuilder.() -> Unit)? + submenuContent: (TrayMenuBuilder.() -> Unit)?, ) { operations.add("SubMenuWithPainterIcon:$label:$isEnabled:${icon.hashCode()}") if (submenuContent != null) { @@ -204,7 +207,7 @@ object MenuContentHash { icon: DrawableResource, iconRenderProperties: IconRenderProperties, isEnabled: Boolean, - submenuContent: (TrayMenuBuilder.() -> Unit)? + submenuContent: (TrayMenuBuilder.() -> Unit)?, ) { operations.add("SubMenuWithDrawableIcon:$label:$isEnabled:${icon.hashCode()}") if (submenuContent != null) { @@ -226,7 +229,9 @@ object MenuContentHash { val content = operations.joinToString("|") val md = MessageDigest.getInstance("SHA-256") val digest = md.digest(content.toByteArray()) - return digest.fold("") { str, it -> str + "%02x".format(it) }.take(16) // Use only first 16 chars for performance + return digest.fold( + "", + ) { str, it -> str + "%02x".format(it) }.take(16) // Use only first 16 chars for performance } } -} \ No newline at end of file +} diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/utils/NativeLibraryLoader.kt b/src/jvmMain/kotlin/com/kdroid/composetray/utils/NativeLibraryLoader.kt index 4c3e8dc0..42ff051c 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/utils/NativeLibraryLoader.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/utils/NativeLibraryLoader.kt @@ -13,7 +13,6 @@ import java.nio.file.StandardCopyOption * and load from there. */ internal object NativeLibraryLoader { - private const val RESOURCE_PREFIX = "composetray/native" private val loadedLibraries = mutableSetOf() @@ -22,7 +21,10 @@ internal object NativeLibraryLoader { * Returns `true` if the library was loaded successfully. */ @Synchronized - fun load(libraryName: String, callerClass: Class<*>): Boolean { + fun load( + libraryName: String, + callerClass: Class<*>, + ): Boolean { if (libraryName in loadedLibraries) return true // 1. Try system library path (packaged app / GraalVM native-image) @@ -43,7 +45,10 @@ internal object NativeLibraryLoader { // ── Shared extraction logic ───────────────────────────────────────── - private fun extractToCache(libraryName: String, callerClass: Class<*>): File? { + private fun extractToCache( + libraryName: String, + callerClass: Class<*>, + ): File? { val platform = detectPlatform() val fileName = mapLibraryName(libraryName) val resourcePath = "$RESOURCE_PREFIX/$platform/$fileName" @@ -77,10 +82,11 @@ internal object NativeLibraryLoader { private fun resolveCacheDir(platform: String): File { val os = System.getProperty("os.name")?.lowercase() ?: "" - val base = when { - os.contains("win") -> File(System.getenv("LOCALAPPDATA") ?: System.getProperty("user.home")) - else -> File(System.getProperty("user.home"), ".cache") - } + val base = + when { + os.contains("win") -> File(System.getenv("LOCALAPPDATA") ?: System.getProperty("user.home")) + else -> File(System.getProperty("user.home"), ".cache") + } return File(base, "composetray/native/$platform") } @@ -88,9 +94,15 @@ internal object NativeLibraryLoader { val os = System.getProperty("os.name")?.lowercase() ?: "" val arch = System.getProperty("os.arch") ?: "" return when { - os.contains("win") -> if (arch.contains("aarch64") || arch.contains("arm")) "win32-arm64" else "win32-x86-64" - os.contains("linux") -> if (arch.contains("aarch64") || arch.contains("arm")) "linux-aarch64" else "linux-x86-64" - os.contains("mac") -> if (arch.contains("aarch64") || arch.contains("arm")) "darwin-aarch64" else "darwin-x86-64" + os.contains( + "win", + ) -> if (arch.contains("aarch64") || arch.contains("arm")) "win32-arm64" else "win32-x86-64" + os.contains( + "linux", + ) -> if (arch.contains("aarch64") || arch.contains("arm")) "linux-aarch64" else "linux-x86-64" + os.contains( + "mac", + ) -> if (arch.contains("aarch64") || arch.contains("arm")) "darwin-aarch64" else "darwin-x86-64" else -> "unknown" } } diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/utils/PersistentAnimatedVisibility.kt b/src/jvmMain/kotlin/com/kdroid/composetray/utils/PersistentAnimatedVisibility.kt index 3ce73154..e688a0fe 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/utils/PersistentAnimatedVisibility.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/utils/PersistentAnimatedVisibility.kt @@ -34,7 +34,6 @@ import androidx.compose.ui.util.fastMap import androidx.compose.ui.util.fastMaxOfOrDefault import kotlin.math.max - @Composable internal fun PersistentAnimatedVisibility( visibleState: MutableTransitionState, @@ -48,21 +47,23 @@ internal fun PersistentAnimatedVisibility( val transition = rememberTransition(visibleState, label) AnimatedVisibilityImplPersistent( transition = transition, - visible = { it }, // Boolean target in MutableTransitionState - modifier = modifier.layout { measurable, constraints -> - // Report 0 size only during lookahead when target is "not visible" - val placeable = measurable.measure(constraints) - val (w, h) = - if (isLookingAhead && !transition.targetState) { - IntSize.Zero - } else { - IntSize(placeable.width, placeable.height) - } - layout(w, h) { placeable.place(0, 0) } - }, + // Boolean target in MutableTransitionState + visible = { it }, + modifier = + modifier.layout { measurable, constraints -> + // Report 0 size only during lookahead when target is "not visible" + val placeable = measurable.measure(constraints) + val (w, h) = + if (isLookingAhead && !transition.targetState) { + IntSize.Zero + } else { + IntSize(placeable.width, placeable.height) + } + layout(w, h) { placeable.place(0, 0) } + }, enter = enter, exit = exit, - content = content + content = content, ) } @@ -83,13 +84,13 @@ private fun AnimatedVisibilityImplPersistent( exit = exit, // <-- never dispose content; it stays composed after exit finishes shouldDisposeBlock = { _, _ -> false }, - content = content + content = content, ) } // --- Scope "maison" (public API only) --- private class AVScopeImpl( - override var transition: Transition + override var transition: Transition, ) : AnimatedVisibilityScope { val targetSize = mutableStateOf(IntSize.Zero) } @@ -98,15 +99,18 @@ private class AVMeasurePolicy(private val scope: AVScopeImpl) : MeasurePolicy { private var hasLookaheadOccurred = false override fun MeasureScope.measure( - measurables: List, constraints: Constraints + measurables: List, + constraints: Constraints, ): MeasureResult { - var maxW = 0; var maxH = 0 - val placeables = measurables.map { - it.measure(constraints).also { p -> - if (p.width > maxW) maxW = p.width - if (p.height > maxH) maxH = p.height + var maxW = 0 + var maxH = 0 + val placeables = + measurables.map { + it.measure(constraints).also { p -> + if (p.width > maxW) maxW = p.width + if (p.height > maxH) maxH = p.height + } } - } if (isLookingAhead) { hasLookaheadOccurred = true scope.targetSize.value = IntSize(maxW, maxH) @@ -116,16 +120,26 @@ private class AVMeasurePolicy(private val scope: AVScopeImpl) : MeasurePolicy { return layout(maxW, maxH) { placeables.forEach { it.place(0, 0) } } } - override fun IntrinsicMeasureScope.minIntrinsicWidth(m: List, h: Int) = - m.maxOfOrNull { it.minIntrinsicWidth(h) } ?: 0 - override fun IntrinsicMeasureScope.minIntrinsicHeight(m: List, w: Int) = - m.maxOfOrNull { it.minIntrinsicHeight(w) } ?: 0 - override fun IntrinsicMeasureScope.maxIntrinsicWidth(m: List, h: Int) = - m.maxOfOrNull { it.maxIntrinsicWidth(h) } ?: 0 - override fun IntrinsicMeasureScope.maxIntrinsicHeight(m: List, w: Int) = - m.maxOfOrNull { it.maxIntrinsicHeight(w) } ?: 0 -} + override fun IntrinsicMeasureScope.minIntrinsicWidth( + m: List, + h: Int, + ) = m.maxOfOrNull { it.minIntrinsicWidth(h) } ?: 0 + + override fun IntrinsicMeasureScope.minIntrinsicHeight( + m: List, + w: Int, + ) = m.maxOfOrNull { it.minIntrinsicHeight(w) } ?: 0 + override fun IntrinsicMeasureScope.maxIntrinsicWidth( + m: List, + h: Int, + ) = m.maxOfOrNull { it.maxIntrinsicWidth(h) } ?: 0 + + override fun IntrinsicMeasureScope.maxIntrinsicHeight( + m: List, + w: Int, + ) = m.maxOfOrNull { it.maxIntrinsicHeight(w) } ?: 0 +} @OptIn(ExperimentalTransitionApi::class, InternalAnimationApi::class) @Composable @@ -135,38 +149,41 @@ private fun AnimatedEnterExitImplPersistent( modifier: Modifier, enter: EnterTransition, exit: ExitTransition, - shouldDisposeBlock: (EnterExitState, EnterExitState) -> Boolean, // kept for API symmetry + // kept for API symmetry + shouldDisposeBlock: (EnterExitState, EnterExitState) -> Boolean, content: @Composable AnimatedVisibilityScope.() -> Unit, ) { - - val child = transition.createChildTransition(label = "EnterExitTransition") { parent -> - transition.targetEnterExit(visible, parent) - } + val child = + transition.createChildTransition(label = "EnterExitTransition") { parent -> + transition.targetEnterExit(visible, parent) + } // Scope is a plain object -> it's fine to remember it keyed by the child transition val scope = remember(child) { AVScopeImpl(child) } // Built-in animateEnterExit (public API) to attach enter/exit effects - val containerModifier = with(scope) { - Modifier.animateEnterExit(enter = enter, exit = exit, label = "Built-in") - } + val containerModifier = + with(scope) { + Modifier.animateEnterExit(enter = enter, exit = exit, label = "Built-in") + } // Keep the subtree always composed; collapse to 0x0 when fully hidden & idle - val collapseModifier = Modifier.layout { measurable, constraints -> - val placeable = measurable.measure(constraints) + val collapseModifier = + Modifier.layout { measurable, constraints -> + val placeable = measurable.measure(constraints) - val isActiveOrAnimating = - visible(transition.currentState) || + val isActiveOrAnimating = + visible(transition.currentState) || visible(transition.targetState) || transition.isSeeking || transition.hasInitialValueAnimations - if (!isActiveOrAnimating) { - layout(0, 0) { /* keep composed, take no space */ } - } else { - layout(placeable.width, placeable.height) { placeable.place(0, 0) } + if (!isActiveOrAnimating) { + layout(0, 0) { /* keep composed, take no space */ } + } else { + layout(placeable.width, placeable.height) { placeable.place(0, 0) } + } } - } Layout( content = { scope.content() }, @@ -175,7 +192,6 @@ private fun AnimatedEnterExitImplPersistent( ) } - private val Transition.exitFinished: Boolean get() = currentState == EnterExitState.PostExit && targetState == EnterExitState.PostExit @@ -236,20 +252,21 @@ private class AnimatedEnterExitMeasurePolicy(val scope: AnimatedVisibilityScopeI private fun Transition.targetEnterExit( visible: (T) -> Boolean, targetState: T, -): EnterExitState = key(this) { - if (this.isSeeking) { - if (visible(targetState)) { - EnterExitState.Visible - } else { - if (visible(this.currentState)) EnterExitState.PostExit else EnterExitState.PreEnter - } - } else { - val hasBeenVisible = remember { mutableStateOf(false) } - if (visible(currentState)) hasBeenVisible.value = true - if (visible(targetState)) { - EnterExitState.Visible +): EnterExitState = + key(this) { + if (this.isSeeking) { + if (visible(targetState)) { + EnterExitState.Visible + } else { + if (visible(this.currentState)) EnterExitState.PostExit else EnterExitState.PreEnter + } } else { - if (hasBeenVisible.value) EnterExitState.PostExit else EnterExitState.PreEnter + val hasBeenVisible = remember { mutableStateOf(false) } + if (visible(currentState)) hasBeenVisible.value = true + if (visible(targetState)) { + EnterExitState.Visible + } else { + if (hasBeenVisible.value) EnterExitState.PostExit else EnterExitState.PreEnter + } } } -} diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/utils/SingleInstanceManager.kt b/src/jvmMain/kotlin/com/kdroid/composetray/utils/SingleInstanceManager.kt index ce738605..bbb8212b 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/utils/SingleInstanceManager.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/utils/SingleInstanceManager.kt @@ -12,7 +12,6 @@ import java.nio.file.Paths import java.nio.file.StandardCopyOption import java.nio.file.StandardWatchEventKinds - /** * Singleton object to manage the single instance of an application. * @@ -20,7 +19,6 @@ import java.nio.file.StandardWatchEventKinds * and provides a mechanism to notify the running instance when another instance attempts to start. */ object SingleInstanceManager { - private const val TAG = "SingleInstanceChecker" /** @@ -36,7 +34,7 @@ object SingleInstanceManager { */ data class Configuration( val lockFilesDir: Path = Paths.get(System.getProperty("java.io.tmpdir")), - val lockIdentifier: String = APP_IDENTIFIER + val lockIdentifier: String = APP_IDENTIFIER, ) { val lockFileName: String = "$lockIdentifier.lock" val restoreRequestFileName: String = "$lockIdentifier.restore_request" @@ -60,7 +58,10 @@ object SingleInstanceManager { * * @param onRestoreRequest A function to be executed if a restore request is received from another instance. */ - fun isSingleInstance(onRestoreFileCreated: (Path.() -> Unit)? = null, onRestoreRequest: Path.() -> Unit): Boolean { + fun isSingleInstance( + onRestoreFileCreated: (Path.() -> Unit)? = null, + onRestoreRequest: Path.() -> Unit, + ): Boolean { // If the lock is already acquired by this process, we are the first instance if (fileLock != null) { debugLog { "The lock is already held by this process" } @@ -78,12 +79,14 @@ object SingleInstanceManager { isWatching = true watchForRestoreRequests(onRestoreRequest) } - Runtime.getRuntime().addShutdownHook(Thread { - releaseLock() - lockFile.delete() - deleteRestoreRequestFile() - debugLog { "Shutdown hook executed" } - }) + Runtime.getRuntime().addShutdownHook( + Thread { + releaseLock() + lockFile.delete() + deleteRestoreRequestFile() + debugLog { "Shutdown hook executed" } + }, + ) true } else { // Another instance is already running @@ -187,4 +190,4 @@ object SingleInstanceManager { private fun errorLog(msg: () -> String) { errorln { "[$TAG] ${msg()}" } } -} \ No newline at end of file +} diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/utils/TrayPosition.kt b/src/jvmMain/kotlin/com/kdroid/composetray/utils/TrayPosition.kt index dac329aa..eb8c3a1d 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/utils/TrayPosition.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/utils/TrayPosition.kt @@ -1,9 +1,9 @@ package com.kdroid.composetray.utils -import com.kdroid.composetray.lib.windows.WindowsNativeBridge import androidx.compose.ui.unit.dp import androidx.compose.ui.window.WindowPosition import com.kdroid.composetray.lib.mac.MacNativeBridge +import com.kdroid.composetray.lib.windows.WindowsNativeBridge import com.kdroid.composetray.tray.impl.MacTrayInitializer import io.github.kdroidfilter.platformtools.LinuxDesktopEnvironment import io.github.kdroidfilter.platformtools.OperatingSystem @@ -13,7 +13,8 @@ import java.awt.GraphicsEnvironment import java.awt.Rectangle import java.awt.Toolkit import java.io.File -import java.util.* +import java.util.Collections +import java.util.Properties import java.util.concurrent.atomic.AtomicReference import kotlin.math.roundToInt @@ -22,7 +23,7 @@ enum class TrayPosition { TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT } data class TrayClickPosition( val x: Int, val y: Int, - val position: TrayPosition + val position: TrayPosition, ) internal object TrayClickTracker { @@ -30,7 +31,10 @@ internal object TrayClickTracker { private val perInstancePositions: MutableMap = Collections.synchronizedMap(mutableMapOf()) - fun updateClickPosition(x: Int, y: Int) { + fun updateClickPosition( + x: Int, + y: Int, + ) { val bounds = getScreenBoundsAt(x, y) val position = convertPositionToCorner(x - bounds.x, y - bounds.y, bounds.width, bounds.height) val pos = TrayClickPosition(x, y, position) @@ -38,7 +42,11 @@ internal object TrayClickTracker { runCatching { saveTrayClickPosition(x, y, position) } } - fun updateClickPosition(instanceId: String, x: Int, y: Int) { + fun updateClickPosition( + instanceId: String, + x: Int, + y: Int, + ) { val bounds = getScreenBoundsAt(x, y) val position = convertPositionToCorner(x - bounds.x, y - bounds.y, bounds.width, bounds.height) val pos = TrayClickPosition(x, y, position) @@ -47,13 +55,22 @@ internal object TrayClickTracker { runCatching { saveTrayClickPosition(x, y, position) } } - fun setClickPosition(x: Int, y: Int, position: TrayPosition) { + fun setClickPosition( + x: Int, + y: Int, + position: TrayPosition, + ) { val pos = TrayClickPosition(x, y, position) lastClickPosition.set(pos) runCatching { saveTrayClickPosition(x, y, position) } } - fun setClickPosition(instanceId: String, x: Int, y: Int, position: TrayPosition) { + fun setClickPosition( + instanceId: String, + x: Int, + y: Int, + position: TrayPosition, + ) { val pos = TrayClickPosition(x, y, position) perInstancePositions[instanceId] = pos lastClickPosition.set(pos) @@ -61,6 +78,7 @@ internal object TrayClickTracker { } fun getLastClickPosition(): TrayClickPosition? = lastClickPosition.get() + fun getLastClickPosition(instanceId: String): TrayClickPosition? = perInstancePositions[instanceId] } @@ -76,7 +94,10 @@ private fun getLogicalScreenSize(): java.awt.Dimension { * Returns the bounds of the screen that contains the given point. * Falls back to the primary screen if the point is not on any screen. */ -private fun getScreenBoundsAt(x: Int, y: Int): Rectangle { +private fun getScreenBoundsAt( + x: Int, + y: Int, +): Rectangle { val ge = GraphicsEnvironment.getLocalGraphicsEnvironment() for (gd in ge.screenDevices) { val bounds = gd.defaultConfiguration.bounds @@ -86,7 +107,12 @@ private fun getScreenBoundsAt(x: Int, y: Int): Rectangle { return Rectangle(0, 0, primary.width, primary.height) } -internal fun convertPositionToCorner(x: Int, y: Int, width: Int, height: Int): TrayPosition { +internal fun convertPositionToCorner( + x: Int, + y: Int, + width: Int, + height: Int, +): TrayPosition { // Use smarter margins based on typical taskbar/panel size // 100px from edge = probably within taskbar/panel area val edgeThreshold = 100 @@ -100,10 +126,10 @@ internal fun convertPositionToCorner(x: Int, y: Int, width: Int, height: Int): T // Strong edge detection first isNearTop && isNearLeft -> TrayPosition.TOP_LEFT isNearTop && isNearRight -> TrayPosition.TOP_RIGHT - isNearTop -> TrayPosition.TOP_RIGHT // Default top to right + isNearTop -> TrayPosition.TOP_RIGHT // Default top to right isNearBottom && isNearLeft -> TrayPosition.BOTTOM_LEFT isNearBottom && isNearRight -> TrayPosition.BOTTOM_RIGHT - isNearBottom -> TrayPosition.BOTTOM_RIGHT // Default bottom to right + isNearBottom -> TrayPosition.BOTTOM_RIGHT // Default bottom to right // Fallback: use quadrant-based detection x >= width / 2 && y < height / 2 -> TrayPosition.TOP_RIGHT x < width / 2 && y < height / 2 -> TrayPosition.TOP_LEFT @@ -156,29 +182,38 @@ private fun loadPropertiesFrom(file: File): Properties? { }.getOrNull() } -private fun storePropertiesTo(file: File, props: Properties) { +private fun storePropertiesTo( + file: File, + props: Properties, +) { file.parentFile?.let { runCatching { if (!it.exists()) it.mkdirs() } } runCatching { file.outputStream().use { props.store(it, null) } } } internal fun saveTrayPosition(position: TrayPosition) { val preferredFile = trayPropertiesFile() - val properties = loadPropertiesFrom(preferredFile) - ?: macCachePropertiesFile()?.let { loadPropertiesFrom(it) } - ?: loadPropertiesFrom(oldTmpPropertiesFile()) - ?: loadPropertiesFrom(legacyPropertiesFile()) - ?: Properties() + val properties = + loadPropertiesFrom(preferredFile) + ?: macCachePropertiesFile()?.let { loadPropertiesFrom(it) } + ?: loadPropertiesFrom(oldTmpPropertiesFile()) + ?: loadPropertiesFrom(legacyPropertiesFile()) + ?: Properties() properties.setProperty(POSITION_KEY, position.name) storePropertiesTo(preferredFile, properties) } -internal fun saveTrayClickPosition(x: Int, y: Int, position: TrayPosition) { +internal fun saveTrayClickPosition( + x: Int, + y: Int, + position: TrayPosition, +) { val preferredFile = trayPropertiesFile() - val properties = loadPropertiesFrom(preferredFile) - ?: macCachePropertiesFile()?.let { loadPropertiesFrom(it) } - ?: loadPropertiesFrom(oldTmpPropertiesFile()) - ?: loadPropertiesFrom(legacyPropertiesFile()) - ?: Properties() + val properties = + loadPropertiesFrom(preferredFile) + ?: macCachePropertiesFile()?.let { loadPropertiesFrom(it) } + ?: loadPropertiesFrom(oldTmpPropertiesFile()) + ?: loadPropertiesFrom(legacyPropertiesFile()) + ?: Properties() properties.setProperty(POSITION_KEY, position.name) properties.setProperty(X_KEY, x.toString()) properties.setProperty(Y_KEY, y.toString()) @@ -186,10 +221,11 @@ internal fun saveTrayClickPosition(x: Int, y: Int, position: TrayPosition) { } internal fun loadTrayClickPosition(): TrayClickPosition? { - val props = loadPropertiesFrom(trayPropertiesFile()) - ?: macCachePropertiesFile()?.let { loadPropertiesFrom(it) } - ?: loadPropertiesFrom(oldTmpPropertiesFile()) - ?: loadPropertiesFrom(legacyPropertiesFile()) ?: return null + val props = + loadPropertiesFrom(trayPropertiesFile()) + ?: macCachePropertiesFile()?.let { loadPropertiesFrom(it) } + ?: loadPropertiesFrom(oldTmpPropertiesFile()) + ?: loadPropertiesFrom(legacyPropertiesFile()) ?: return null val positionStr = props.getProperty(POSITION_KEY) ?: return null val x = props.getProperty(X_KEY)?.toIntOrNull() ?: return null @@ -202,14 +238,15 @@ internal fun loadTrayClickPosition(): TrayClickPosition? { } } -internal fun getWindowsTrayPosition(nativeResult: String?): TrayPosition = when (nativeResult) { - null -> throw IllegalArgumentException("Returned value is null") - "top-left" -> TrayPosition.TOP_LEFT - "top-right" -> TrayPosition.TOP_RIGHT - "bottom-left" -> TrayPosition.BOTTOM_LEFT - "bottom-right" -> TrayPosition.BOTTOM_RIGHT - else -> throw IllegalArgumentException("Unknown value: $nativeResult") -} +internal fun getWindowsTrayPosition(nativeResult: String?): TrayPosition = + when (nativeResult) { + null -> throw IllegalArgumentException("Returned value is null") + "top-left" -> TrayPosition.TOP_LEFT + "top-right" -> TrayPosition.TOP_RIGHT + "bottom-left" -> TrayPosition.BOTTOM_LEFT + "bottom-right" -> TrayPosition.BOTTOM_RIGHT + else -> throw IllegalArgumentException("Unknown value: $nativeResult") + } /** OS → Tray corner heuristics */ fun getTrayPosition(): TrayPosition { @@ -220,21 +257,22 @@ fun getTrayPosition(): TrayPosition { TrayClickTracker.getLastClickPosition()?.position ?: loadTrayClickPosition()?.position ?: run { - val props = loadPropertiesFrom(trayPropertiesFile()) - ?: macCachePropertiesFile()?.let { loadPropertiesFrom(it) } - ?: loadPropertiesFrom(oldTmpPropertiesFile()) - ?: loadPropertiesFrom(legacyPropertiesFile()) + val props = + loadPropertiesFrom(trayPropertiesFile()) + ?: macCachePropertiesFile()?.let { loadPropertiesFrom(it) } + ?: loadPropertiesFrom(oldTmpPropertiesFile()) + ?: loadPropertiesFrom(legacyPropertiesFile()) props?.getProperty(POSITION_KEY)?.let { runCatching { TrayPosition.valueOf(it) }.getOrNull() } } ?: when (detectLinuxDesktopEnvironment()) { - LinuxDesktopEnvironment.KDE -> TrayPosition.BOTTOM_RIGHT + LinuxDesktopEnvironment.KDE -> TrayPosition.BOTTOM_RIGHT LinuxDesktopEnvironment.CINNAMON -> TrayPosition.BOTTOM_RIGHT - LinuxDesktopEnvironment.GNOME -> TrayPosition.TOP_RIGHT - LinuxDesktopEnvironment.MATE -> TrayPosition.TOP_RIGHT - LinuxDesktopEnvironment.XFCE -> TrayPosition.TOP_RIGHT - else -> TrayPosition.TOP_RIGHT + LinuxDesktopEnvironment.GNOME -> TrayPosition.TOP_RIGHT + LinuxDesktopEnvironment.MATE -> TrayPosition.TOP_RIGHT + LinuxDesktopEnvironment.XFCE -> TrayPosition.TOP_RIGHT + else -> TrayPosition.TOP_RIGHT } } OperatingSystem.UNKNOWN -> TrayPosition.TOP_RIGHT @@ -247,29 +285,35 @@ fun getTrayWindowPosition( windowWidth: Int, windowHeight: Int, horizontalOffset: Int = 0, - verticalOffset: Int = 0 + verticalOffset: Int = 0, ): WindowPosition { val screenSize = Toolkit.getDefaultToolkit().screenSize if (getOperatingSystem() == OperatingSystem.WINDOWS) { - val freshPos = TrayClickTracker.getLastClickPosition() - ?: loadTrayClickPosition() - - val posToUse = freshPos ?: run { - // No click yet (e.g., initiallyVisible = true). Synthesize one near the tray corner - val corner = getTrayPosition() - val (sx, sy) = syntheticClickFromCorner(corner, screenSize.width, screenSize.height) - return calculateWindowPositionFromClick( - sx, sy, corner, - windowWidth, windowHeight, - horizontalOffset, verticalOffset - ) - } + val freshPos = + TrayClickTracker.getLastClickPosition() + ?: loadTrayClickPosition() + + val posToUse = + freshPos ?: run { + // No click yet (e.g., initiallyVisible = true). Synthesize one near the tray corner + val corner = getTrayPosition() + val (sx, sy) = syntheticClickFromCorner(corner, screenSize.width, screenSize.height) + return calculateWindowPositionFromClick( + sx, sy, corner, + windowWidth, windowHeight, + horizontalOffset, verticalOffset, + ) + } return calculateWindowPositionFromClick( - posToUse.x, posToUse.y, posToUse.position, - windowWidth, windowHeight, - horizontalOffset, verticalOffset + posToUse.x, + posToUse.y, + posToUse.position, + windowWidth, + windowHeight, + horizontalOffset, + verticalOffset, ) } @@ -281,9 +325,13 @@ fun getTrayWindowPosition( val pos = TrayClickTracker.getLastClickPosition() if (pos != null) { return calculateWindowPositionFromClick( - pos.x, pos.y, pos.position, - windowWidth, windowHeight, - horizontalOffset, verticalOffset + pos.x, + pos.y, + pos.position, + windowWidth, + windowHeight, + horizontalOffset, + verticalOffset, ) } } @@ -292,27 +340,34 @@ fun getTrayWindowPosition( val clickPos = TrayClickTracker.getLastClickPosition() ?: loadTrayClickPosition() if (clickPos != null) { return calculateWindowPositionFromClick( - clickPos.x, clickPos.y, clickPos.position, - windowWidth, windowHeight, - horizontalOffset, verticalOffset + clickPos.x, + clickPos.y, + clickPos.position, + windowWidth, + windowHeight, + horizontalOffset, + verticalOffset, ) } } return when (getTrayPosition()) { TrayPosition.TOP_LEFT -> WindowPosition(x = (0 + horizontalOffset).dp, y = (0 + verticalOffset).dp) - TrayPosition.TOP_RIGHT -> WindowPosition( - x = (screenSize.width - windowWidth + horizontalOffset).dp, - y = (0 + verticalOffset).dp - ) - TrayPosition.BOTTOM_LEFT -> WindowPosition( - x = (0 + horizontalOffset).dp, - y = (screenSize.height - windowHeight + verticalOffset).dp - ) - TrayPosition.BOTTOM_RIGHT -> WindowPosition( - x = (screenSize.width - windowWidth + horizontalOffset).dp, - y = (screenSize.height - windowHeight + verticalOffset).dp - ) + TrayPosition.TOP_RIGHT -> + WindowPosition( + x = (screenSize.width - windowWidth + horizontalOffset).dp, + y = (0 + verticalOffset).dp, + ) + TrayPosition.BOTTOM_LEFT -> + WindowPosition( + x = (0 + horizontalOffset).dp, + y = (screenSize.height - windowHeight + verticalOffset).dp, + ) + TrayPosition.BOTTOM_RIGHT -> + WindowPosition( + x = (screenSize.width - windowWidth + horizontalOffset).dp, + y = (screenSize.height - windowHeight + verticalOffset).dp, + ) } } @@ -322,7 +377,7 @@ fun getTrayWindowPositionForInstance( windowWidth: Int, windowHeight: Int, horizontalOffset: Int = 0, - verticalOffset: Int = 0 + verticalOffset: Int = 0, ): WindowPosition { val os = getOperatingSystem() @@ -330,36 +385,52 @@ fun getTrayWindowPositionForInstance( OperatingSystem.WINDOWS -> { val pos = TrayClickTracker.getLastClickPosition(instanceId) if (pos == null) { - debugln { "[TrayPosition] getTrayWindowPositionForInstance: no position for $instanceId, using fallback" } + debugln { + "[TrayPosition] getTrayWindowPositionForInstance: " + + "no position for $instanceId, using fallback" + } return fallbackCornerPosition(windowWidth, windowHeight, horizontalOffset, verticalOffset) } calculateWindowPositionFromClick( - pos.x, pos.y, pos.position, - windowWidth, windowHeight, - horizontalOffset, verticalOffset + pos.x, + pos.y, + pos.position, + windowWidth, + windowHeight, + horizontalOffset, + verticalOffset, ) } OperatingSystem.MACOS -> { val trayHandle = MacTrayInitializer.getNativeTrayHandle(instanceId) if (trayHandle != 0L) { val outXY = IntArray(2) - val precise = try { - MacNativeBridge.nativeGetStatusItemPositionFor(trayHandle, outXY) != 0 - } catch (_: Throwable) { false } + val precise = + try { + MacNativeBridge.nativeGetStatusItemPositionFor(trayHandle, outXY) != 0 + } catch (_: Throwable) { + false + } val x = outXY[0] val y = outXY[1] if (precise) { val regionStr = runCatching { MacNativeBridge.nativeGetStatusItemRegionFor(trayHandle) }.getOrNull() - val trayPos = if (regionStr != null) getMacTrayPosition(regionStr) - else { - val bounds = getScreenBoundsAt(x, y) - convertPositionToCorner(x - bounds.x, y - bounds.y, bounds.width, bounds.height) - } + val trayPos = + if (regionStr != null) { + getMacTrayPosition(regionStr) + } else { + val bounds = getScreenBoundsAt(x, y) + convertPositionToCorner(x - bounds.x, y - bounds.y, bounds.width, bounds.height) + } TrayClickTracker.setClickPosition(instanceId, x, y, trayPos) return calculateWindowPositionFromClick( - x, y, trayPos, - windowWidth, windowHeight, - horizontalOffset, verticalOffset + x, + y, + trayPos, + windowWidth, + windowHeight, + horizontalOffset, + verticalOffset, ) } } @@ -375,17 +446,24 @@ fun getTrayWindowPositionForInstance( * Uses the screen containing the click point for correct multi-monitor support. */ private fun calculateWindowPositionFromClick( - clickX: Int, clickY: Int, trayPosition: TrayPosition, - windowWidth: Int, windowHeight: Int, + clickX: Int, + clickY: Int, + trayPosition: TrayPosition, + windowWidth: Int, + windowHeight: Int, horizontalOffset: Int, - verticalOffset: Int + verticalOffset: Int, ): WindowPosition { val os = getOperatingSystem() val isTop = trayPosition == TrayPosition.TOP_LEFT || trayPosition == TrayPosition.TOP_RIGHT val isRight = trayPosition == TrayPosition.TOP_RIGHT || trayPosition == TrayPosition.BOTTOM_RIGHT val sb = getScreenBoundsAt(clickX, clickY) - debugln { "[TrayPosition] calculateWindowPositionFromClick: clickX=$clickX, clickY=$clickY, trayPos=$trayPosition, winW=$windowWidth, winH=$windowHeight, screenBounds=$sb" } + debugln { + "[TrayPosition] calculateWindowPositionFromClick: " + + "clickX=$clickX, clickY=$clickY, trayPos=$trayPosition, " + + "winW=$windowWidth, winH=$windowHeight, screenBounds=$sb" + } return if (os == OperatingSystem.WINDOWS) { var x = clickX - (windowWidth / 2) @@ -395,8 +473,16 @@ private fun calculateWindowPositionFromClick( x += horizontalOffset y += verticalOffset - if (x < sb.x) x = sb.x else if (x + windowWidth > sb.x + sb.width) x = sb.x + sb.width - windowWidth - if (y < sb.y) y = sb.y else if (y + windowHeight > sb.y + sb.height) y = sb.y + sb.height - windowHeight + if (x < sb.x) { + x = sb.x + } else if (x + windowWidth > sb.x + sb.width) { + x = sb.x + sb.width - windowWidth + } + if (y < sb.y) { + y = sb.y + } else if (y + windowHeight > sb.y + sb.height) { + y = sb.y + sb.height - windowHeight + } debugln { "[TrayPosition] Windows: final x=$x, y=$y" } WindowPosition(x = x.dp, y = y.dp) } else { @@ -407,39 +493,48 @@ private fun calculateWindowPositionFromClick( var y = if (isTop) anchorY else anchorY - windowHeight x += if (isRight) -horizontalOffset else horizontalOffset - y += if (isTop) verticalOffset else -verticalOffset + y += if (isTop) verticalOffset else -verticalOffset - if (x < sb.x) x = sb.x else if (x + windowWidth > sb.x + sb.width) x = sb.x + sb.width - windowWidth - if (y < sb.y) y = sb.y else if (y + windowHeight > sb.y + sb.height) y = sb.y + sb.height - windowHeight + if (x < sb.x) { + x = sb.x + } else if (x + windowWidth > sb.x + sb.width) { + x = sb.x + sb.width - windowWidth + } + if (y < sb.y) { + y = sb.y + } else if (y + windowHeight > sb.y + sb.height) { + y = sb.y + sb.height - windowHeight + } WindowPosition(x = x.dp, y = y.dp) } } - - /** Position de repli coin + offsets */ private fun fallbackCornerPosition( - w: Int, h: Int, + w: Int, + h: Int, horizontalOffset: Int, - verticalOffset: Int + verticalOffset: Int, ): WindowPosition { val screen = Toolkit.getDefaultToolkit().screenSize return when (getTrayPosition()) { TrayPosition.TOP_LEFT -> WindowPosition((0 + horizontalOffset).dp, (0 + verticalOffset).dp) TrayPosition.TOP_RIGHT -> WindowPosition((screen.width - w + horizontalOffset).dp, (0 + verticalOffset).dp) TrayPosition.BOTTOM_LEFT -> WindowPosition((0 + horizontalOffset).dp, (screen.height - h + verticalOffset).dp) - TrayPosition.BOTTOM_RIGHT -> WindowPosition( - (screen.width - w + horizontalOffset).dp, - (screen.height - h + verticalOffset).dp - ) + TrayPosition.BOTTOM_RIGHT -> + WindowPosition( + (screen.width - w + horizontalOffset).dp, + (screen.height - h + verticalOffset).dp, + ) } } -internal fun getMacTrayPosition(nativeResult: String?): TrayPosition = when (nativeResult) { - "top-left" -> TrayPosition.TOP_LEFT - "top-right" -> TrayPosition.TOP_RIGHT - else -> TrayPosition.TOP_RIGHT -} +internal fun getMacTrayPosition(nativeResult: String?): TrayPosition = + when (nativeResult) { + "top-left" -> TrayPosition.TOP_LEFT + "top-right" -> TrayPosition.TOP_RIGHT + else -> TrayPosition.TOP_RIGHT + } internal fun getStatusItemXYForMac(): Pair { val outXY = IntArray(2) @@ -448,12 +543,13 @@ internal fun getStatusItemXYForMac(): Pair { } fun debugDeleteTrayPropertiesFiles() { - val files = setOfNotNull( - trayPropertiesFile(), - legacyPropertiesFile(), - oldTmpPropertiesFile(), - macCachePropertiesFile() - ) + val files = + setOfNotNull( + trayPropertiesFile(), + legacyPropertiesFile(), + oldTmpPropertiesFile(), + macCachePropertiesFile(), + ) files.filter(File::exists).forEach { runCatching { it.delete() } } } @@ -489,12 +585,15 @@ private fun getWindowsTaskbarHeight(): Int { private fun getSystemBarSize(): Int { return when (getOperatingSystem()) { OperatingSystem.WINDOWS -> getWindowsTaskbarHeight() - OperatingSystem.MACOS -> 25 // macOS menu bar - else -> 28 // Linux panel (GNOME/KDE average) + OperatingSystem.MACOS -> 25 // macOS menu bar + else -> 28 // Linux panel (GNOME/KDE average) } } -internal fun isPointWithinMacStatusItem(px: Int, py: Int): Boolean { +internal fun isPointWithinMacStatusItem( + px: Int, + py: Int, +): Boolean { if (getOperatingSystem() != OperatingSystem.MACOS) return false val (ix, iy) = getStatusItemXYForMac() if (ix == 0 && iy == 0) return false @@ -508,25 +607,29 @@ internal fun isPointWithinMacStatusItem(px: Int, py: Int): Boolean { return px in left..right && py in top..bottom } -internal fun isPointWithinLinuxStatusItem(px: Int, py: Int): Boolean { +internal fun isPointWithinLinuxStatusItem( + px: Int, + py: Int, +): Boolean { if (getOperatingSystem() != OperatingSystem.LINUX) return false val click = TrayClickTracker.getLastClickPosition() ?: loadTrayClickPosition() ?: return false val (ix, iy) = click.x to click.y - val baseIconSizeAt1x = when (detectLinuxDesktopEnvironment()) { - LinuxDesktopEnvironment.KDE -> 22 - LinuxDesktopEnvironment.GNOME -> 24 - LinuxDesktopEnvironment.CINNAMON -> 24 - LinuxDesktopEnvironment.MATE -> 24 - LinuxDesktopEnvironment.XFCE -> 24 - else -> 24 - } + val baseIconSizeAt1x = + when (detectLinuxDesktopEnvironment()) { + LinuxDesktopEnvironment.KDE -> 22 + LinuxDesktopEnvironment.GNOME -> 24 + LinuxDesktopEnvironment.CINNAMON -> 24 + LinuxDesktopEnvironment.MATE -> 24 + LinuxDesktopEnvironment.XFCE -> 24 + else -> 24 + } val dpi = runCatching { Toolkit.getDefaultToolkit().screenResolution }.getOrDefault(96) val scale = (dpi / 96.0).coerceAtLeast(0.5) val half = (baseIconSizeAt1x * 0.5 * scale).toInt().coerceAtLeast(8) val fudge = (4 * scale).toInt().coerceAtLeast(2) - val left = ix - half - fudge - val right = ix + half + fudge - val top = iy - half - fudge + val left = ix - half - fudge + val right = ix + half + fudge + val top = iy - half - fudge val bottom = iy + half + fudge return px in left..right && py in top..bottom } @@ -536,12 +639,20 @@ internal fun isPointWithinLinuxStatusItem(px: Int, py: Int): Boolean { private fun syntheticClickFromCorner( corner: TrayPosition, screenW: Int, - screenH: Int + screenH: Int, ): Pair { val half = dpiAwareHalfIconOffset() // ~half icon in px, DPI-aware - val x = if (corner == TrayPosition.TOP_RIGHT || corner == TrayPosition.BOTTOM_RIGHT) - screenW - half else half - val y = if (corner == TrayPosition.BOTTOM_LEFT || corner == TrayPosition.BOTTOM_RIGHT) - screenH - half else half + val x = + if (corner == TrayPosition.TOP_RIGHT || corner == TrayPosition.BOTTOM_RIGHT) { + screenW - half + } else { + half + } + val y = + if (corner == TrayPosition.BOTTOM_LEFT || corner == TrayPosition.BOTTOM_RIGHT) { + screenH - half + } else { + half + } return x to y } diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/utils/WindowRaise.kt b/src/jvmMain/kotlin/com/kdroid/composetray/utils/WindowRaise.kt index 83ff5a7e..4f6ccf1b 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/utils/WindowRaise.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/utils/WindowRaise.kt @@ -18,15 +18,21 @@ object WindowRaise { } fun unraise(window: Window) { - try { window.isAlwaysOnTop = false } catch (_: Throwable) {} + try { + window.isAlwaysOnTop = false + } catch (_: Throwable) { + } } /** * Convenience helper to raise a window, give the window manager a brief moment, * then revert the temporary always-on-top flag. Useful especially on Windows. */ - suspend fun forceFront(window: Window, windowState: WindowState, delayMs: Long = 250) { - + suspend fun forceFront( + window: Window, + windowState: WindowState, + delayMs: Long = 250, + ) { // Ensure it’s not minimized windowState.isMinimized = false diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/utils/WindowVisibilityMonitor.kt b/src/jvmMain/kotlin/com/kdroid/composetray/utils/WindowVisibilityMonitor.kt index 7ee2ba4a..c5dcf37a 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/utils/WindowVisibilityMonitor.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/utils/WindowVisibilityMonitor.kt @@ -2,13 +2,15 @@ package com.kdroid.composetray.utils import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow -import java.awt.* +import java.awt.AWTEvent +import java.awt.Frame +import java.awt.Toolkit +import java.awt.Window import java.awt.event.AWTEventListener import java.awt.event.ComponentEvent import java.awt.event.WindowEvent object WindowVisibilityMonitor { - // Public marker name used to identify the tray popup dialog window so it can be excluded from checks const val TRAY_DIALOG_NAME: String = "ComposeTrayPopup" @@ -16,19 +18,21 @@ object WindowVisibilityMonitor { private val _hasVisible = MutableStateFlow(false) val hasAnyVisibleWindows: StateFlow = _hasVisible - private val listener = AWTEventListener { event -> - // React to window + component visibility changes - val type = event.id - when (type) { - WindowEvent.WINDOW_OPENED, - WindowEvent.WINDOW_CLOSED, - WindowEvent.WINDOW_ICONIFIED, - WindowEvent.WINDOW_DEICONIFIED, - WindowEvent.WINDOW_STATE_CHANGED, - ComponentEvent.COMPONENT_SHOWN, - ComponentEvent.COMPONENT_HIDDEN -> recompute() + private val listener = + AWTEventListener { event -> + // React to window + component visibility changes + val type = event.id + when (type) { + WindowEvent.WINDOW_OPENED, + WindowEvent.WINDOW_CLOSED, + WindowEvent.WINDOW_ICONIFIED, + WindowEvent.WINDOW_DEICONIFIED, + WindowEvent.WINDOW_STATE_CHANGED, + ComponentEvent.COMPONENT_SHOWN, + ComponentEvent.COMPONENT_HIDDEN, + -> recompute() + } } - } init { // Start listening as soon as the object is loaded @@ -46,9 +50,10 @@ object WindowVisibilityMonitor { /** Recalculate and publish the current visibility state */ fun recompute() { - _hasVisible.value = Window.getWindows() - .asSequence() - .filter { it.name != TRAY_DIALOG_NAME } - .any { it.isEffectivelyVisible() } + _hasVisible.value = + Window.getWindows() + .asSequence() + .filter { it.name != TRAY_DIALOG_NAME } + .any { it.isEffectivelyVisible() } } } diff --git a/src/native/linux/build.sh b/src/native/linux/build.sh index 29195a62..deb70c21 100755 --- a/src/native/linux/build.sh +++ b/src/native/linux/build.sh @@ -39,7 +39,17 @@ fi SDBUS_CFLAGS=$(pkg-config --cflags libsystemd) SDBUS_LIBS=$(pkg-config --libs libsystemd) -mkdir -p "$OUTPUT_DIR/linux-x86-64" +# Detect host architecture +UNAME_ARCH="$(uname -m)" +case "$UNAME_ARCH" in + x86_64) ARCH="x86-64" ;; + aarch64) ARCH="aarch64" ;; + *) echo "ERROR: Unsupported architecture: $UNAME_ARCH"; exit 1 ;; +esac +PLATFORM_DIR="linux-$ARCH" +echo "Detected platform: $PLATFORM_DIR" + +mkdir -p "$OUTPUT_DIR/$PLATFORM_DIR" # Compile sni.c (includes stb_image implementation) echo "Compiling sni.c..." @@ -60,17 +70,17 @@ gcc -c -o "$SCRIPT_DIR/jni_bridge.o" \ # Link into shared library echo "Linking libLinuxTray.so..." -gcc -shared -o "$OUTPUT_DIR/linux-x86-64/libLinuxTray.so" \ +gcc -shared -o "$OUTPUT_DIR/$PLATFORM_DIR/libLinuxTray.so" \ "$SCRIPT_DIR/sni.o" \ "$SCRIPT_DIR/jni_bridge.o" \ $SDBUS_LIBS \ -lpthread -lm -ldl # Strip debug symbols for smaller binary -strip --strip-unneeded "$OUTPUT_DIR/linux-x86-64/libLinuxTray.so" +strip --strip-unneeded "$OUTPUT_DIR/$PLATFORM_DIR/libLinuxTray.so" # Clean up object files rm -f "$SCRIPT_DIR/sni.o" "$SCRIPT_DIR/jni_bridge.o" -echo "Build completed: $OUTPUT_DIR/linux-x86-64/libLinuxTray.so" -ls -lh "$OUTPUT_DIR/linux-x86-64/libLinuxTray.so" +echo "Build completed: $OUTPUT_DIR/$PLATFORM_DIR/libLinuxTray.so" +ls -lh "$OUTPUT_DIR/$PLATFORM_DIR/libLinuxTray.so" diff --git a/src/native/windows/CMakeLists.txt b/src/native/windows/CMakeLists.txt index 96af1a7a..50de6569 100644 --- a/src/native/windows/CMakeLists.txt +++ b/src/native/windows/CMakeLists.txt @@ -21,7 +21,6 @@ if(CMAKE_GENERATOR_PLATFORM STREQUAL "x64" OR CMAKE_GENERATOR_PLATFORM STREQUAL elseif(CMAKE_GENERATOR_PLATFORM STREQUAL "ARM64") set(TARGET_ARCH "ARM64") set(OUTPUT_DIR "${BASE_OUTPUT_DIR}/win32-arm64") - add_compile_options("/arch:arm64") else() message(FATAL_ERROR "Unsupported architecture: ${CMAKE_GENERATOR_PLATFORM}") endif() @@ -50,7 +49,7 @@ target_compile_definitions(WinTray PRIVATE TRAY_WINAPI=1 WIN32_LEAN_AND_MEAN NOM # Link to Shell32.lib for Windows platform if(WIN32) - target_link_libraries(WinTray PRIVATE shell32) + target_link_libraries(WinTray PRIVATE shell32 user32 gdi32) endif() # Define the installation path diff --git a/src/native/windows/build.bat b/src/native/windows/build.bat index c6c0d2a6..f8167f94 100644 --- a/src/native/windows/build.bat +++ b/src/native/windows/build.bat @@ -1,12 +1,6 @@ @echo off echo === Starting compilation for x64 and ARM64 in MinSizeRel mode === -:: Detect JAVA_HOME for JNI headers -if not defined JAVA_HOME ( - echo WARNING: JAVA_HOME is not set. JNI headers may not be found. - echo Set JAVA_HOME to your JDK installation directory. -) - echo. echo === x64 Configuration (MinSizeRel) === cmake -B build-x64 -A x64 -DCMAKE_BUILD_TYPE=MinSizeRel . @@ -41,6 +35,3 @@ if %ERRORLEVEL% neq 0 ( echo. echo === Compilation completed successfully for both architectures in MinSizeRel mode === -echo. -echo x64 DLL: ..\..\jvmMain\resources\composetray\native\win32-x86-64\WinTray.dll -echo ARM64 DLL: ..\..\jvmMain\resources\composetray\native\win32-arm64\WinTray.dll diff --git a/src/native/windows/tray_windows.c b/src/native/windows/tray_windows.c index 9cb1a6e7..26267e98 100644 --- a/src/native/windows/tray_windows.c +++ b/src/native/windows/tray_windows.c @@ -1,7 +1,11 @@ /* tray.c - Windows implementation with full Unicode support */ #define COBJMACROS +#ifndef UNICODE #define UNICODE +#endif +#ifndef _UNICODE #define _UNICODE +#endif #include #include #include From 897f1fc1163e44c7200f1a43c275dce0c5810131 Mon Sep 17 00:00:00 2001 From: "Elie G." Date: Thu, 9 Apr 2026 23:18:16 +0300 Subject: [PATCH 12/12] chore: remove unused imports in TrayApp.kt --- .../kotlin/com/kdroid/composetray/tray/api/TrayApp.kt | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/jvmMain/kotlin/com/kdroid/composetray/tray/api/TrayApp.kt b/src/jvmMain/kotlin/com/kdroid/composetray/tray/api/TrayApp.kt index b19a8c70..4b9fa5e1 100644 --- a/src/jvmMain/kotlin/com/kdroid/composetray/tray/api/TrayApp.kt +++ b/src/jvmMain/kotlin/com/kdroid/composetray/tray/api/TrayApp.kt @@ -8,7 +8,6 @@ package com.kdroid.composetray.tray.api import androidx.compose.animation.EnterTransition import androidx.compose.animation.ExitTransition -import androidx.compose.animation.animateEnterExit import androidx.compose.animation.core.EaseInOut import androidx.compose.animation.core.ExperimentalTransitionApi import androidx.compose.animation.core.InternalAnimationApi @@ -52,17 +51,13 @@ import com.kdroid.composetray.lib.windows.WindowsOutsideClickWatcher import com.kdroid.composetray.menu.api.TrayMenuBuilder import com.kdroid.composetray.tray.impl.WindowsTrayInitializer import com.kdroid.composetray.utils.ComposableIconUtils -import com.kdroid.composetray.utils.ExperimentalTrayAppApi import com.kdroid.composetray.utils.IconRenderProperties import com.kdroid.composetray.utils.MenuContentHash import com.kdroid.composetray.utils.PersistentAnimatedVisibility -import com.kdroid.composetray.utils.TrayAppState -import com.kdroid.composetray.utils.TrayWindowDismissMode import com.kdroid.composetray.utils.WindowVisibilityMonitor import com.kdroid.composetray.utils.debugln import com.kdroid.composetray.utils.getTrayWindowPositionForInstance import com.kdroid.composetray.utils.isMenuBarInDarkMode -import com.kdroid.composetray.utils.rememberTrayAppState import io.github.kdroidfilter.platformtools.LinuxDesktopEnvironment import io.github.kdroidfilter.platformtools.OperatingSystem import io.github.kdroidfilter.platformtools.OperatingSystem.MACOS