Add support for some GLFW init hints

This commit is contained in:
JeremyStar™ 2024-07-23 20:35:24 +02:00
parent 81535ad701
commit d49d54df3a
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
3 changed files with 267 additions and 1 deletions

View file

@ -106,7 +106,10 @@ public class GlfwSubsystem implements SubsystemMainClass {
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public void initializeSubsystem() {} public void initializeSubsystem() {
// Initialize configuration
new GlfwSubsystemConfiguration();
}
/** {@inheritDoc} */ /** {@inheritDoc} */
@NotNull @NotNull
@ -131,6 +134,17 @@ public class GlfwSubsystem implements SubsystemMainClass {
// Set error callback // Set error callback
errorCallback.set(); errorCallback.set();
// Set init hints
switch (GlfwSubsystemConfiguration.getInstance().getPlatform()) {
case ANY -> glfwInitHint(GLFW_PLATFORM, GLFW_ANY_PLATFORM);
case WAYLAND -> tryPlatform(GLFW_PLATFORM_WAYLAND);
case X11 -> tryPlatform(GLFW_PLATFORM_X11);
case WIN32 -> tryPlatform(GLFW_PLATFORM_WIN32);
case COCOA -> tryPlatform(GLFW_PLATFORM_COCOA);
case NONE -> glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_NULL);
}
glfwInitHint(GLFW_WAYLAND_LIBDECOR, GlfwSubsystemConfiguration.getInstance().isDisableLibdecor() ? GLFW_WAYLAND_DISABLE_LIBDECOR : GLFW_WAYLAND_PREFER_LIBDECOR);
// Initialize GLFW // Initialize GLFW
if (!glfwInit()) if (!glfwInit())
throw new GlfwInitializationException(); throw new GlfwInitializationException();
@ -152,4 +166,18 @@ public class GlfwSubsystem implements SubsystemMainClass {
glfwTerminate(); glfwTerminate();
errorCallback.free(); errorCallback.free();
} }
/**
* Checks if the specified platform is compatible,
* and if so, specifies it as the platform to use.
*
* @param platform platform to try
* @since v1-alpha2
*/
private void tryPlatform(int platform) {
if (glfwPlatformSupported(platform))
glfwInitHint(GLFW_PLATFORM, platform);
else
glfwInitHint(GLFW_PLATFORM, GLFW_ANY_PLATFORM);
}
} }

View file

@ -0,0 +1,166 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Contributors
* Licensed under the GNU Affero General Public License v3
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.staropensource.sosengine.graphics.glfw;
import de.staropensource.sosengine.base.classes.SubsystemConfiguration;
import de.staropensource.sosengine.base.logging.Logger;
import de.staropensource.sosengine.base.types.CodePart;
import de.staropensource.sosengine.base.types.logging.LogIssuer;
import de.staropensource.sosengine.base.utility.parser.PropertyParser;
import de.staropensource.sosengine.graphics.glfw.types.GlfwPlatform;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Properties;
/**
* Provides the subsystem configuration for all classes in a centralized place.
*
* @since v1-alpha0
*/
@SuppressWarnings({ "unused", "JavadocDeclaration", "JavadocBlankLines" })
@Getter
public final class GlfwSubsystemConfiguration implements SubsystemConfiguration {
/**
* Contains the class instance.
*
* @since v1-alpha0
*
* -- GETTER --
* Returns the class instance.
*
* @return class instance unless {@link GlfwSubsystem} is uninitialized
* @since v1-alpha0
*/
@Getter
private static GlfwSubsystemConfiguration instance;
/**
* Defines the group every property must start with to be recognized as a subsystem configuration setting.
*
* @since v1-alpha0
*
* -- GETTER --
* Returns the group that every property must start with to be recognized as a subsystem configuration setting.
*
* @return property group
* @since v1-alpha0
*/
@NotNull
@Getter
private static final String group = "sosengine.graphics.glfw.";
/**
* Contains the platform GLFW will try to initialize with.
*
* @since v1-alpha2
*
* -- GETTER --
* Gets the value for {@link #platform}.
*
* @return variable value
* @see #platform
* @since v1-alpha2
*/
private GlfwPlatform platform;
/**
* If {@code true}, will disable support for
* <a href="https://gitlab.freedesktop.org/libdecor/libdecor">libdecor</a>.
* <p>
* Only affects the {@link GlfwPlatform#WAYLAND} platform.
*
* @since v1-alpha2
*
* -- GETTER --
* Gets the value for {@#link disableLibdecor}.
*
* @return variable value
* @see #disableLibdecor
* @since v1-alpha2
*/
private boolean disableLibdecor;
/**
* Constructs this class.
*
* @see GlfwSubsystem
* @since v1-alpha0
*/
public GlfwSubsystemConfiguration() {
// Only allow one instance
if (instance == null)
instance = this;
else
Logger.crash(new LogIssuer(getClass(), CodePart.ENGINE), "Tried initializing " + getClass().getName() + " twice");
loadDefaultConfiguration();
}
/** {@inheritDoc} */
public void loadConfiguration(@NotNull Properties properties) {
// Define variables
PropertyParser parser = new PropertyParser(properties);
for (String property : properties.stringPropertyNames()) {
// Check if property name starts with group
if (!property.startsWith(group)) continue;
// Skip to important stuff
property = property.substring(group.length());
// Match settings
try {
switch (property) {
case "platform" -> {
try {
platform = GlfwPlatform.valueOf(parser.getString(group + property).toUpperCase());
} catch (IllegalArgumentException ignored) {
System.out.println("Platform " + parser.getString(group + property) + " is not valid");
}
}
case "disableLibdecor" -> disableLibdecor = parser.getBoolean(group + property);
}
} catch (NullPointerException ignored) {}
}
}
/** {@inheritDoc} */
public void loadConfiguration() {
loadConfiguration(System.getProperties());
}
/** {@inheritDoc} */
public void loadDefaultConfiguration() {
platform = GlfwPlatform.ANY;
disableLibdecor = false;
}
/** {@inheritDoc} */
@Nullable
public Object getSetting(@NotNull String setting) {
switch (setting) {
case "platform" -> { return platform; }
case "disableLibdecor" -> { return disableLibdecor; }
default -> { return null; }
}
}
}

View file

@ -0,0 +1,72 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Contributors
* Licensed under the GNU Affero General Public License v3
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.staropensource.sosengine.graphics.glfw.types;
/**
* Contains all available platforms
* which GLFW can be initialized with.
*
* @since v1-alpha2
*/
@SuppressWarnings({ "unused" })
public enum GlfwPlatform {
/**
* Allows GLFW to initialize with any platform.
*
* @since v1-alpha2
*/
ANY,
/**
* Prefer initializing with the Wayland platform.
*
* @since v1-alpha2
*/
WAYLAND,
/**
* Prefer initializing with the X11 platform.
*
* @since v1-alpha2
*/
X11,
/**
* Prefer initializing with the Win32 platform.
*
* @since v1-alpha2
*/
WIN32,
/**
* Prefer initializing with the Cocoa platform.
*
* @since v1-alpha2
*/
COCOA,
/**
* Prefer initializing without any platform.
* This will disable any functionality.
*
* @since v1-alpha2
*/
NONE
}