# *** Welcome to the Fiji launcher's configuration file! ***
#
# The Fiji launcher is built on Jaunch (https://github.com/scijava/jaunch).
#
# The contents below define Fiji's particular launcher behavior and features,
# on top of Jaunch's "sensible default" functionality.
#
# See the common.toml file for more details about Jaunch configuration.

jaunch-version = 1

program-name = 'Fiji'

includes = ['jvm.toml', 'python.toml']

supported-options = [
    # General options
    '--default-gc|do not use advanced garbage collector settings by default\n(-XX:+UseG1GC)',
    '--gc-g1|use the G1 garbage collector',
    '--debug-gc|show debug info about the garbage collector on stderr',
    '--no-splash|suppress showing a splash screen upon startup',
    '--jdb|launch the jdb debugger',

    # Fiji-specific options
    '--python|launch in Python mode',
    '--allow-multiple|do not reuse existing Fiji instance',
    '--info|informational output',
    '--plugins=<dir>|use <dir> to discover plugins',
    '--run <plugin> [<arg>]|run <plugin> in Fiji, optionally with arguments',
    '--edit [<file>...]|edit the given file in the script editor',

    # Options to run programs other than Fiji
    '--update|start the command-line version of the Updater',
    '--main-class=<class name>|start the given class instead of Fiji',

    # Windows things
    '--console,--attach-console|attempt to attach output to the calling console',
    '--new-console|ensure the launch of a new, dedicated console for output',
    '--set-icon=<exe-file>,<ico-file>|add/replace the icon of the given program', #NO

# Nico: NEED: execute a script with parameters
# ./ImageJ-linux64 --headless --console --run hello.py 'name="Mr Kraken"'
# Can we simplify this further?
]

modes = [
  # Launch mode: Python or Java?
  '--python|LAUNCH:PYTHON',
  '!--python|LAUNCH:JVM',

  # Set INFO level logging for the command line Updater.
  '--update|--info',

  # Are we launching with an alternative main class?
  # This list should match the one in jvm.main-class below.
  '--jdb|MODE:custom-main',
  '--update|MODE:custom-main',
  '--main-class|MODE:custom-main',

  # Disable the splash screen when running a different
  # main class, or when --no-splash or --headless is given.
  'MODE:custom-main|MODE:no-splash',
  '--no-splash|MODE:no-splash',
  '--headless|MODE:no-splash',
]

directives = [
  'LAUNCH:JVM|!--dry-run|apply-update,INIT_THREADS',
  'LAUNCH:PYTHON|!--dry-run|apply-update,INIT_THREADS',
]

# /============================================================================\
# |                               PYTHON CONFIG                                |
# \============================================================================/

python.version-min = '3.8'

python.packages = [
  'pyimagej>=1.4.1',
  'scyjava>=1.9.1',
]

python.script-path = ['${app-dir}/fiji.py']

python.main-args = [
    '${jvm.libjvmPath}',
    '@{jvm.runtimeArgs}',
    '--',
    '${jvm.mainClass}',
    '@{jvm.mainArgs}',
]

# /============================================================================\
# |                                JAVA CONFIG                                 |
# \============================================================================/

jvm.version-min = '8'

jvm.classpath = [
    # Construct the classpath.
    '${app-dir}/jars/*.jar',
    '${app-dir}/jars/*/*.jar',
    '--plugins|${plugins}/*.jar'
    '!--plugins|${app-dir}/plugins/*.jar',
    #'${user.home}/.plugins/*.jar',
    #'${user.home}/.share/fiji/jars/*.jar',
    #'/usr/local/share/fiji/jars/*.jar',
    '--main-class|.', # Weird behavior, but it's what the ImageJ Launcher does.
]

cfg.max-heap = '75%'

jvm.max-heap = '${cfg.max-heap}'

jvm.runtime-args = [
    '--info|-Dscijava.log.level=info',
    '--debug|-Dij.debug=true',
    '--debug|-Dscijava.log.level=debug',

    # In https://forum.image.sc/t/88620, acardona wrote:
    # > When running in a machine with many CPU cores (like 64, or 256) and lots
    # > of RAM (e.g., more than 100 GB, like 500 GB of heap size as defined by -Xmx),
    # > I find that the concurrent garbage collector (G1 GC) works *a lot better*.
    '!--default-gc|JAVA:6+|-XX:+UseG1GC',
    # Handle --gc-g1 option.
    '--gc-g1|-XX:+UseCompressedOops',
    '--gc-g1|-XX:+UnlockExperimentalVMOptions',
    '--gc-g1|-XX:+UseG1GC',
    '--gc-g1|-XX:NewRatio=5',

    # Handle --debug-gc option.
    '--debug-gc|-verbose:gc',

    # If running a custom main class, do not patch ImageJ.
    'MODE:custom-main|-Dpatch.ij1=false',

    # Enable SciJava logging in updater mode.
    '--update|-Dscijava.log.level=info',

    # Convince the Java module system to play nice.
    # Supported since Jigsaw was introduced in Java 9.
    # Necessary for ij1-patcher to work properly with Java 17+.

    # net.imagej.patcher.LegacyInjector uses ClassLoader.findLoadedClass
    # javassist.util.proxy.SecurityActions uses ClassLoader.defineClass
    'JAVA:9+|--add-opens=java.base/java.lang=ALL-UNNAMED',

    # For org.apache.arrow - TODO: document exactly why
    'JAVA:9+|--add-opens=java.base/java.nio=ALL-UNNAMED',

    # org.scijava.util.SizeableArrayList uses ArrayList.size
    'JAVA:9+|--add-opens=java.base/java.util=ALL-UNNAMED',

    # com.jogamp.nativewindow.awt.AppContextInfo uses sun.awt.AppContext
    'JAVA:9+|--add-opens=java.desktop/sun.awt=ALL-UNNAMED',

    #  ini.trakem2.Project.openFSProject uses javax.swing.JTree.expandedState
    'JAVA:9+|--add-opens=java.desktop/javax.swing=ALL-UNNAMED',

    # For accessing internal Swing UI classes used by BVV - TODO: document exactly where
    'JAVA:9+|--add-opens=java.desktop/sun.swing=ALL-UNNAMED',
    'JAVA:9+|--add-opens=java.desktop/com.sun.java.swing=ALL-UNNAMED',

    # HACK: For fiji.debugging.Object_Inspector
    # The Object_Inspector could end up reflecting anything, so this doesn't
    # make it work in all cases, but it helps for inspecting AWT/Swing windows.
    'JAVA:9+|--add-opens=java.desktop/java.awt=ALL-UNNAMED',

    # sc.fiji.compat.DefaultFijiService uses XToolkit.awtAppClassName
    'JAVA:9+|OS:LINUX|--add-opens=java.desktop/sun.awt.X11=ALL-UNNAMED', 

    # TODO: document where this happens on macOS.
    'JAVA:9+|OS:MACOSX|--add-opens=java.desktop/com.apple.eawt=ALL-UNNAMED',

    # For scenery - TODO: document exactly why
    #'JAVA:9+|--add-opens=java.base/java.lang=ALL-UNNAMED',
    'JAVA:9+|--add-opens=java.base/java.lang.invoke=ALL-UNNAMED',
    'JAVA:9+|--add-opens=java.base/java.net=ALL-UNNAMED',
    #'JAVA:9+|--add-opens=java.base/java.nio=ALL-UNNAMED',
    'JAVA:9+|--add-opens=java.base/java.time=ALL-UNNAMED',
    #'JAVA:9+|--add-opens=java.base/java.util=ALL-UNNAMED',
    'JAVA:9+|--add-opens=java.base/java.util.concurrent.atomic=ALL-UNNAMED',
    'JAVA:9+|--add-opens=java.base/sun.nio.ch=ALL-UNNAMED',
    'JAVA:9+|--add-opens=java.base/sun.util.calendar=ALL-UNNAMED',

    # Avoid Jython's huge startup cost.
    '-Dpython.cachedir.skip=true',

    # Set ImageJ's plugins.dir property.
    '--plugins|-Dplugins.dir=${plugins}'
    '!--plugins|-Dplugins.dir=${app-dir}',

    # Set application directory properties.
    '-Dimagej.dir=${app-dir}',
    '-Dij.dir=${app-dir}',
    '-Dfiji.dir=${app-dir}',

    # Set application executable properties.
    '-Dfiji.executable=${executable}',
    '-Dij.executable=${executable}',

    # TODO: is fiji.defaultLibPath property necessary for any reason?

    # Set up platform-specific native library paths.
    # TODO: Link to jna.library.path issue on GitHub.
    'OS:LINUX|ARCH:ARM64|-Djava.library.path=${app-dir}/lib/linux-arm64',
    'OS:LINUX|ARCH:ARM64|-Djna.library.path=${app-dir}/lib/linux-arm64',
    'OS:LINUX|ARCH:X64|-Djava.library.path=${app-dir}/lib/linux64',
    'OS:LINUX|ARCH:X64|-Djna.library.path=${app-dir}/lib/linux64',
    'OS:LINUX|ARCH:X86|-Djava.library.path=${app-dir}/lib/linux32',
    'OS:LINUX|ARCH:X86|-Djna.library.path=${app-dir}/lib/linux32',
    'OS:MACOSX|ARCH:ARM64|-Djava.library.path=${app-dir}/lib/macos-arm64',
    'OS:MACOSX|ARCH:ARM64|-Djna.library.path=${app-dir}/lib/macos-arm64',
    'OS:MACOSX|ARCH:X64|-Djava.library.path=${app-dir}/lib/macosx',
    'OS:MACOSX|ARCH:X64|-Djna.library.path=${app-dir}/lib/macosx',
    'OS:WINDOWS|ARCH:ARM64|-Djava.library.path=${app-dir}\lib\win-arm64',
    'OS:WINDOWS|ARCH:ARM64|-Djna.library.path=${app-dir}\lib\win-arm64',
    'OS:WINDOWS|ARCH:X64|-Djava.library.path=${app-dir}\lib\win64',
    'OS:WINDOWS|ARCH:X64|-Djna.library.path=${app-dir}\lib\win64',
    'OS:WINDOWS|ARCH:X86|-Djava.library.path=${app-dir}\lib\win32',
    'OS:WINDOWS|ARCH:X86|-Djna.library.path=${app-dir}\lib\win32',

    # Set properties favored by the the SciJava app-launcher.
    # See https://github.com/scijava/app-launcher#readme
    '-Dscijava.app.name=Fiji',
    '-Dscijava.app.directory=${app-dir}',
    '!MODE:no-splash|!OS:WINDOWS|-Dscijava.app.splash-image=${app-dir}/images/icon.png',
    '!MODE:no-splash|OS:WINDOWS|-Dscijava.app.splash-image=${app-dir}\images\icon.png',
    '-Dscijava.app.java-version-minimum=1.8',
    '-Dscijava.app.java-version-recommended=1.8.0_172',
    '-Dscijava.app.look-and-feel=com.formdev.flatlaf.FlatLightLaf',
    'OS:WINDOWS|-Dscijava.app.config-file=${app-dir}\config\fiji.cfg',
    '!OS:WINDOWS|-Dscijava.app.config-file=${app-dir}/config/fiji.cfg',
    'OS:LINUX|ARCH:ARM64|-Dscijava.app.java-root=${app-dir}/java/linux-arm64',
    'OS:LINUX|ARCH:X64|-Dscijava.app.java-root=${app-dir}/java/linux64',
    'OS:LINUX|ARCH:X86|-Dscijava.app.java-root=${app-dir}/java/linux32',
    'OS:MACOSX|ARCH:ARM64|-Dscijava.app.java-root=${app-dir}/java/macos-arm64',
    'OS:MACOSX|ARCH:X64|-Dscijava.app.java-root=${app-dir}/java/macosx',
    'OS:WINDOWS|ARCH:ARM64|-Dscijava.app.java-root=${app-dir}\java\windows-arm64',
    'OS:WINDOWS|ARCH:X64|-Dscijava.app.java-root=${app-dir}\java\win64',
    'OS:WINDOWS|ARCH:X86|-Dscijava.app.java-root=${app-dir}\java\win32',
    '-Dscijava.app.java-links=https://downloads.imagej.net/java/jdk-urls.txt',
    'OS:LINUX|ARCH:ARM64|-Dscijava.app.java-platform=linux-arm64',
    'OS:LINUX|ARCH:X64|-Dscijava.app.java-platform=linux64',
    'OS:LINUX|ARCH:X86|-Dscijava.app.java-platform=linux32',
    'OS:MACOSX|ARCH:ARM64|-Dscijava.app.java-platform=macos-arm64',
    'OS:MACOSX|ARCH:X64|-Dscijava.app.java-platform=macosx',
    'OS:WINDOWS|ARCH:ARM64|-Dscijava.app.java-platform=windows-arm64',
    'OS:WINDOWS|ARCH:X64|-Dscijava.app.java-platform=win64',
    'OS:WINDOWS|ARCH:X86|-Dscijava.app.java-platform=win32',

    # Make SciJava startup less brittle.
    '-Dscijava.context.strict=false',

    # Enable JavaFX's JavaScript engine.
    '-Djavafx.allowjs=true',

    # Enable AWT/Swing's HiDPI scaling.
    '-Dsun.java2d.uiScale=true',

    # Avoid the following error message on Windows:
    #
    #   console: Failed to install '': java.nio.charset.UnsupportedCharsetException: cp0.
    #
    # See https://github.com/imagej/imagej-launcher/issues/56
    '-Dpython.console.encoding=UTF-8',
]

jvm.main-class = [
    # This list should match the one in modes above.
    '--jdb|com.sun.tools.example.debug.tty.TTY',
    '--update|net.imagej.updater.CommandLine',
    '--main-class|${main-class}',
    'org.scijava.launcher.ClassLauncher',
]

jvm.main-args = [
    # Pass the actual main class to the ClassLauncher as an argument.
    '!MODE:custom-main|net.imagej.Main',

    # The -batch flag is required when --headless is given.
    '--headless|!MODE:custom-main|-batch',
]
