|
| 1 | +/* |
| 2 | + * Copyright 2026 JetBrains s.r.o. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | + |
| 26 | +package sun.awt.wl.im.text_input_unstable_v3; |
| 27 | + |
| 28 | +import sun.awt.UNIXToolkit; |
| 29 | + |
| 30 | +import java.awt.Toolkit; |
| 31 | +import java.util.Objects; |
| 32 | +import java.util.concurrent.atomic.AtomicReference; |
| 33 | + |
| 34 | +final class CurrentDesktopInfo { |
| 35 | + |
| 36 | + private CurrentDesktopInfo() {} |
| 37 | + |
| 38 | + |
| 39 | + public static boolean isGnome() { |
| 40 | + Boolean result = isGnome.get(); |
| 41 | + |
| 42 | + if (result == null) { |
| 43 | + synchronized (CurrentDesktopInfo.class) { |
| 44 | + if (Toolkit.getDefaultToolkit() instanceof UNIXToolkit unixToolkit) { |
| 45 | + result = "gnome".equals(unixToolkit.getDesktop()); |
| 46 | + } else { |
| 47 | + result = false; |
| 48 | + } |
| 49 | + |
| 50 | + isGnome.set(result); |
| 51 | + } |
| 52 | + |
| 53 | + return result; |
| 54 | + } |
| 55 | + |
| 56 | + return result; |
| 57 | + } |
| 58 | + // {@code null} if not initialized yet |
| 59 | + private static final AtomicReference<Boolean> isGnome = new AtomicReference<>(null); |
| 60 | + |
| 61 | + /** negative if couldn't obtain or the desktop is not GNOME */ |
| 62 | + public static int getGnomeShellMajorVersion() { |
| 63 | + Integer result = gnomeVersion.get(); |
| 64 | + |
| 65 | + if (result == null) { |
| 66 | + synchronized (CurrentDesktopInfo.class) { |
| 67 | + if (!isGnome()) { |
| 68 | + result = -1; |
| 69 | + } else if (Toolkit.getDefaultToolkit() instanceof UNIXToolkit unixToolkit) { |
| 70 | + try { |
| 71 | + result = Objects.requireNonNullElse(unixToolkit.getGnomeShellMajorVersion(), -1); |
| 72 | + } catch (Exception ignored) { |
| 73 | + result = -1; |
| 74 | + } |
| 75 | + } else { |
| 76 | + result = -1; |
| 77 | + } |
| 78 | + |
| 79 | + gnomeVersion.set(result); |
| 80 | + } |
| 81 | + |
| 82 | + return result; |
| 83 | + } |
| 84 | + |
| 85 | + return result; |
| 86 | + } |
| 87 | + // {@code null} if not initialized yet, negative if couldn't obtain or the desktop is not GNOME |
| 88 | + private static final AtomicReference<Integer> gnomeVersion = new AtomicReference<>(null); |
| 89 | +} |
0 commit comments