0

Our Java/SWT-based application uses a bash startup script on Linux. We need to find out whether to launch with GTK2 (older SWT version) or with GTK3 (newer SWT version), because the latest SWT versions don't support GTK2 any more and cause problems on older systems with no GTK3 support or older GTK3 versions. Especially the GTK3-themes (!= Adwaita or Ambiance) cause problems.

If the system supports GTK3, how to find out (in a bash script) what GTK3 theme is configured? Optional: find out what GTK3 themes are available on the system?

Update: a tiny C application similar to this answer also would be fine.

Thomas S.
  • 5,804
  • 5
  • 37
  • 72
  • 1
    `https://github.com/KittyKatt/screenFetch` -- `screenfetch`, a `bash` program that shows things like CPU, RAM,... and GTK versions installed and GTK theme in use. I suggest that you use `screenfetch` or grab that GTK part. GTK version and theme detection is DE dependent (GOME, XFCE,...) – Bach Lien Jan 25 '19 at 17:52
  • @BachLien Thanks for the link. Wow, more than 250 lines to just get the theme(s). Maybe the option with the tiny C application is shorter. – Thomas S. Jan 25 '19 at 19:21

1 Answers1

2

One-liner:

Gtk3ThemeName=/tmp/$RANDOM$$ && gcc -o $Gtk3ThemeName -include stdio.h -include gtk/gtk.h -xc <(echo 'int main() {gchar *prop; gtk_init(0, 0); g_object_get(gtk_settings_get_default(), "gtk-theme-name", &prop, 0); return !printf("%s\n", prop);}') $(pkg-config gtk+-3.0 --cflags --libs 2>/dev/null) 2>/dev/null && Gtk3ThemeName="$($Gtk3ThemeName && rm $Gtk3ThemeName)" || unset Gtk3ThemeName

Somewhat more readable:

Gtk3ThemeName=/tmp/$RANDOM$$
if gcc -o $Gtk3ThemeName -include stdio.h -include gtk/gtk.h -xc <(echo '
    int main() {
        gchar *prop;
        gtk_init(0, 0);
        g_object_get(gtk_settings_get_default(), "gtk-theme-name", &prop, 0);
        return !printf("%s\n", prop);
    }') $(pkg-config gtk+-3.0 --cflags --libs 2>/dev/null) 2>/dev/null; then
    Gtk3ThemeName="$($Gtk3ThemeName && rm $Gtk3ThemeName)"
else
    unset Gtk3ThemeName
fi

After that you may just echo "$Gtk3ThemeName" to print the theme name.

If there is no GTK3 installed (or if either gcc or pkg-config failed, or if /tmp/ is either not writable or run-protected), the variable would end up empty.

hidefromkgb
  • 5,834
  • 1
  • 13
  • 44
  • This requires gcc on the machine running the script. It's probably best to just compile and ship the program as a pre-compiled C binary for that. – liberforce Jan 28 '19 at 10:12