From c57533dde8c84898aa77d9a91c396045f739eec7 Mon Sep 17 00:00:00 2001 From: nathan Date: Sun, 11 Dec 2016 23:37:45 +0100 Subject: [PATCH] Added extra checks in case XDG_CURRENT_DESKTOP is not available as a system environment --- src/dorkbox/systemTray/SystemTray.java | 20 ++++++++++--- src/dorkbox/systemTray/util/ImageUtils.java | 32 +++++++++++++++++++-- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/src/dorkbox/systemTray/SystemTray.java b/src/dorkbox/systemTray/SystemTray.java index 0018ce9..c856589 100644 --- a/src/dorkbox/systemTray/SystemTray.java +++ b/src/dorkbox/systemTray/SystemTray.java @@ -392,8 +392,14 @@ class SystemTray { // quick check, because we know that unity uses app-indicator. Maybe REALLY old versions do not. We support 14.04 LTE at least - String XDG = System.getenv("XDG_CURRENT_DESKTOP"); + // if we are running as ROOT, we *** WILL NOT *** have access to 'XDG_CURRENT_DESKTOP' + // *unless env's are preserved, but they are not guaranteed to be + String XDG = System.getenv("XDG_CURRENT_DESKTOP"); + if (XDG == null) { + // maybe we are running as root??? + XDG = "unknown"; // try to autodetect if we should use app indicator or gtkstatusicon + } // BLEH. if gnome-shell is running, IT'S REALLY GNOME! // we must ALWAYS do this check!! @@ -429,6 +435,7 @@ class SystemTray { } if (trayType == null) { + // Unity is a weird combination. It's "Gnome", but it's not "Gnome Shell". if ("unity".equalsIgnoreCase(XDG)) { trayType = selectTypeQuietly(useNativeMenus, TrayType.AppIndicator); } @@ -524,8 +531,13 @@ class SystemTray { // fallback... if (trayType == null) { trayType = selectTypeQuietly(useNativeMenus, TrayType.GtkStatusIcon); - logger.error("Unable to load the system tray native library. Please write an issue and include your OS type and " + - "configuration"); + logger.info("Unable to determine the system window manager type.Falling back to GtkStatusIcon."); + } + + // this is bad... + if (trayType == null) { + logger.error("Unable to load the system tray native libraries. Please write an issue and include your OS type and configuration"); + throw new RuntimeException("SystemTray initialization failed. Something is seriously wrong."); } } @@ -601,7 +613,7 @@ class SystemTray { if ((isJavaFxLoaded || isSwtLoaded) && SwingUtilities.isEventDispatchThread()) { // oh boy! This WILL NOT WORK. Let the dev know - throw new RuntimeException("SystemTray initialization can not occur on the swing Event Dispatch Thread (EDT)"); + throw new RuntimeException("SystemTray initialization can not occur on the Swing Event Dispatch Thread (EDT)"); } // javaFX and SWT should not start on the EDT!! diff --git a/src/dorkbox/systemTray/util/ImageUtils.java b/src/dorkbox/systemTray/util/ImageUtils.java index 23b5741..4e3fcef 100644 --- a/src/dorkbox/systemTray/util/ImageUtils.java +++ b/src/dorkbox/systemTray/util/ImageUtils.java @@ -184,8 +184,36 @@ class ImageUtils { // AppIndicator MIGHT scale the icon (depends on the OS) - // KDE is bonkers. - if ("kde".equalsIgnoreCase(System.getenv("XDG_CURRENT_DESKTOP"))) { + // KDE is bonkers. Gnome is "ok" + String XDG = System.getenv("XDG_CURRENT_DESKTOP"); + if (XDG == null) { + // we might be root. Check if plasmashell is running, if it is -- then we are most likely KDE + + try { + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(8196); + PrintStream outputStream = new PrintStream(byteArrayOutputStream); + + // ps a | grep [g]nome-shell + final ShellProcessBuilder shell = new ShellProcessBuilder(outputStream); + shell.setExecutable("ps"); + shell.addArgument("a"); + shell.start(); + + + String output = ShellProcessBuilder.getOutput(byteArrayOutputStream); + if (output.contains("plasmashell")) { + XDG = "kde"; + } + } catch (Throwable e) { + // assume we are not KDE. Maybe not the best assumption, but if we get here, something is wrong. + if (SystemTray.DEBUG) { + SystemTray.logger.error("Unable to check if plasmashell is running.", e); + } + } + } + + + if ("kde".equalsIgnoreCase(XDG)) { try { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(8196); PrintStream outputStream = new PrintStream(byteArrayOutputStream);