Skip to content

Commit

Permalink
Linux (GNOME): rework system tray support detection
Browse files Browse the repository at this point in the history
- In addition to the previously cheked extensions also show tray icon if
  any of the TopIcons extensions is enabled
- Show tray icon for older GNOME releases < 3.26, that come with
  out-of-the-box support for tray icons
  • Loading branch information
bwRavencl committed Aug 11, 2024
1 parent 3d13264 commit 0f3d738
Showing 1 changed file with 35 additions and 10 deletions.
45 changes: 35 additions & 10 deletions src/main/java/de/bwravencl/controllerbuddy/gui/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
import java.security.SecureRandom;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.EnumMap;
import java.util.EnumSet;
Expand All @@ -135,6 +136,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.Preferences;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.swing.AbstractAction;
Expand Down Expand Up @@ -298,6 +300,9 @@ public final class Main {
private static final String SINGLE_INSTANCE_INIT = "INIT";
private static final String SINGLE_INSTANCE_ACK = "ACK";
private static final String SINGLE_INSTANCE_EOF = "EOF";
private static final String[] GNOME_SYSTEM_TRAY_EXTENSIONS = { "[email protected]",
"[email protected]", "[email protected]@gmail.com", "topiconsfix@[email protected]",
"[email protected]", "[email protected]" };
static volatile Main main;
static boolean skipMessageDialogs;
private static volatile boolean terminated;
Expand Down Expand Up @@ -1066,6 +1071,18 @@ private static void deleteSingleInstanceLockFile() {
}
}

private static String execCommandForOutput(final String[] cmdarray) {
String output = null;
try (final var inputStream = Runtime.getRuntime().exec(cmdarray).getInputStream();
final var scanner = new Scanner(inputStream, StandardCharsets.UTF_8).useDelimiter("\\A")) {
output = scanner.hasNext() ? scanner.next() : null;
} catch (final IOException e) {
log.log(Level.WARNING, e.getMessage(), e);
}

return output;
}

private static int getExtendedKeyCodeForMenu(final AbstractButton button,
final Set<Integer> alreadyAssignedKeyCodes) {
var keyCode = KeyEvent.VK_UNDEFINED;
Expand Down Expand Up @@ -2071,17 +2088,25 @@ private void onControllersChanged(final List<ControllerInfo> presentControllers,
if (systemTraySupported && isLinux) {
switch (System.getenv("XDG_SESSION_DESKTOP")) {
case "gnome" -> {
String output = null;
try (final var inputStream = Runtime.getRuntime()
.exec(new String[] { "/usr/bin/gnome-extensions", "list", "--enabled" }).getInputStream();
final var scanner = new Scanner(inputStream, StandardCharsets.UTF_8).useDelimiter("\\A")) {
output = scanner.hasNext() ? scanner.next() : null;
} catch (final IOException e) {
log.log(Level.WARNING, e.getMessage(), e);
}
final var gnomeExtensions = execCommandForOutput(
new String[] { "/usr/bin/gnome-extensions", "list", "--enabled" });
if (gnomeExtensions == null
|| Arrays.stream(GNOME_SYSTEM_TRAY_EXTENSIONS).noneMatch(gnomeExtensions::contains)) {
systemTraySupported = false;

final var gnomeVersion = execCommandForOutput(new String[] { "/usr/bin/gnome-shell", "--version" });
if (gnomeVersion == null) {
break;
}

systemTraySupported = (output != null && (output.contains("[email protected]")
|| output.contains("[email protected]")));
final var matcher = Pattern.compile("^GNOME Shell (\\d+)\\.(\\d+).*").matcher(gnomeVersion);
if (matcher.matches()) {
final var majorVersion = Integer.parseInt(matcher.group(1));
final var minorVersion = Integer.parseInt(matcher.group(2));

systemTraySupported = majorVersion < 3 || (majorVersion == 3 && minorVersion < 26);
}
}
}
case "KDE" -> {
final var kdeSessionVersion = System.getenv("KDE_SESSION_VERSION");
Expand Down

0 comments on commit 0f3d738

Please sign in to comment.