9
votes

I am trying to detect whether I am running on a Gnome or KDE desktop environment.

I know I can check via a ps -aux and grepping either gnome or KDE but that's not good: 1) what if I am on a gnome desktop but I have the KDE libs loaded? 2) I need to do it from code without using system() or popen() or other fork/exec combination.

I can read files, or env. variables or whatever.

Any ideas?

thanks, any help is appreciated.

4
what if it's xfce? ion? wmii? dwm....? - cthom06
Since you're only trying to detect gnome vs kde, is there a particular set of distros or configurations you're targeting? How general does the solution have to be? - nmichaels
Why do you want to do this? It is possible that you are heading down an ill-considered byway here... - dmckee --- ex-moderator kitten
@Jessica: I can't speak for anyone else, but I ask why because I am trying to help. Sometimes people ask "How do I Foo?" when they mean "I want to accomplish Bar, and have been trying Foo." when Bar is better handled by some non-Foo method. And I have no idea where you dug "malicious" from. - dmckee --- ex-moderator kitten
What programs to you want to present to the users and why? It might be possible to use one of the XDG things to find out which programs the user prefers and present them, that way you ALWAYS get it right even if they are a GNOME user that likes some KDE apps. - Spudd86

4 Answers

2
votes

At least on Opensuse there are the environment variables WINDOWMANAGER, WINDOW_MANAGER

eike@lixie:~> echo $WINDOWMANAGER
/usr/bin/startkde
eike@lixie:~> echo $WINDOW_MANAGER
/usr/bin/startkde
eike@lixie:~>
2
votes

Not sure how standard this is, but it is consistent in Fedora 21, Slackware 14.1 and Ubuntu 14.04. (More welcome)

try

 $ echo $DESKTOP_SESSION

Hope this helps.

1
votes

Pick a set of window managers you care about: metacity, xfwm4, flwm, etc. You can look for those in your grep of ps (or search through /proc). Gnome libraries don't necessarily mean that someone's running the whole gnome desktop environment, but then Gnome and KDE aren't window managers. If WMs are what you care about, look for those.

0
votes

You can statically link your window toolkit if you don't mind an inconsistent-looking UI. It will still work fine. You can also simply bundle the shared libraries and ensure LD_LIBRARY_PATH points to them. If you actually wanted to implement something that would dynamically link to different toolkits, you could try something with dlopen/dlsym, but that would be insane.

If you care about cross-platform / cross-widget toolkit consistency, your best bet would be something that renders native-looking widgets itself; Swing can render the same code to look like GTK or Windows. I know you're not using Java, but there is no easy solution in C (Swing will only get you partway anyway because it doesn't do Qt).