views:

159

answers:

3

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.

+1  A: 

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:~>
Eike
I am not sure, but I think it works on gentoo, too
Karel Bílek
And it doesn't on Ubuntu.
Job
yes, thank but it doesn't work on Ubuntu, fedora, redhat, suse...
Jessica
A: 

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.

Nathon
maybe I didn't explain myself correctly: is there a way to know whether I am running on a gnome environment or a KDE?
Jessica
Ah. In that case you may want to edit your question to say "desktop environment" instead of "window manager".
Nathon
done. do you have an answer now?
Jessica
Nope. If I knew how, I would have just said so instead of trying to get you to clarify your question.
Nathon
Thanks anyway. This should be easy but for some reason it's rather complex
Jessica
@Jessica: What makes you think it should be easy? Linux has *no system level concept* of "What GUI am I running?" because the GUI is *just another user process*. That said, you might look at [Finding the preferred application for a given file extension via unix shell commands](http://stackoverflow.com/questions/1949531/), and if that isn't sufficient I would just *ask the user*. Really.
dmckee
I'm not talking system level concept, but I find it hard to believe that either Gnome or KDE, bloated as they are with their tons of libraries provide no clue as to whether they are running or not... I guess I am wrong then. Thanks for pointing it out. this question then will remain unanswered...
Jessica
this is not an answer that gives me any solution but since I want to close this question I'll accept this.
Jessica
A: 

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).

bowenl2
I just need to know whether I am running with either desktop. the reason is that I can present the user with an option of running certain programs based on that information, however my program is a simple command line with no linking to QT or GTK
Jessica
Then using processing listing has some bad corner cases. Just because the current user is running [Gnome|KDE] doesn't mean that *this* process is attached to that display (users can run more than one X session and can use different desktop environments and window managers in them)...
dmckee
I know that. hence the question here
Jessica