10

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.

Jessica
  • 2,005
  • 4
  • 28
  • 44
  • 6
    what if it's xfce? ion? wmii? dwm....? – cthom06 Aug 16 '10 at 19:51
  • 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 Aug 16 '10 at 20:15
  • Why do you want to do this? It is possible that you are heading down an ill-considered byway here... – dmckee --- ex-moderator kitten Aug 16 '10 at 20:37
  • @cthom06, then if its any of those I'll get a NULL or 0 or whatever and I set it as FAIL. – Jessica Aug 16 '10 at 20:45
  • @dmckee why do people insist on asking why and inmediatelly tagging something as malicious... – Jessica Aug 16 '10 at 20:46
  • 1
    @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 Aug 16 '10 at 20:52
  • from "ill-considered byway" but I guess my english is not that good... – Jessica Aug 16 '10 at 20:57
  • I need to know whether I am running on a gnome desktop or KDE because I need to present with an option of programs to the users, these programs rely heavily on this info. My program is a simple command line. – Jessica Aug 16 '10 at 20:58
  • You might want to consider handling at least XFCE and LXDE as if they are GNOME then – Spudd86 Nov 25 '10 at 19:01
  • 1
    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 Nov 25 '10 at 19:07

5 Answers5

3

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.

Samer
  • 1,923
  • 3
  • 34
  • 54
Luis Pinto
  • 31
  • 2
2

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
  • 2,205
  • 17
  • 10
1

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.

nmichaels
  • 49,466
  • 12
  • 107
  • 135
  • 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 Aug 16 '10 at 20:51
  • Ah. In that case you may want to edit your question to say "desktop environment" instead of "window manager". – nmichaels Aug 16 '10 at 21:02
  • Nope. If I knew how, I would have just said so instead of trying to get you to clarify your question. – nmichaels Aug 17 '10 at 13:40
  • Thanks anyway. This should be easy but for some reason it's rather complex – Jessica Aug 17 '10 at 13:44
  • 1
    @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 --- ex-moderator kitten Aug 17 '10 at 22:10
  • 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 Aug 18 '10 at 17:25
  • this is not an answer that gives me any solution but since I want to close this question I'll accept this. – Jessica Aug 24 '10 at 13:04
0

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

Hut8
  • 6,080
  • 4
  • 42
  • 59
  • 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 Aug 16 '10 at 20:49
  • 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 --- ex-moderator kitten Aug 16 '10 at 20:58
  • I know that. hence the question here – Jessica Aug 16 '10 at 21:02
0

You may find helpfull this list of env variables values corresponding to common DE's:

AskUbuntu answer

In short: check environment variables values:

  • XDG_CURRENT_DESKTOP
  • GDMSESSION

Bash example:

# Corresponding variables in bash which also can be obtained form C++
echo "XDG_CURRENT_DESKTOP=$XDG_CURRENT_DESKTOP"
echo "GDMSESSION=$GDMSESSION"

C++ example (CompilerExplorer link):

#include <cstdlib>
#include <iostream>

int main() {

    const char* const XDG_CURRENT_DESKTOP   = std::getenv("XDG_CURRENT_DESKTOP");
    const char* const GDMSESSION            = std::getenv("GDMSESSION");

    std::cout << "XDG_CURRENT_DESKTOP=\""   << ((nullptr != XDG_CURRENT_DESKTOP) ? XDG_CURRENT_DESKTOP : "<none>") << "\"\n";
    std::cout << "GDMSESSION=\""            << ((nullptr != GDMSESSION)          ? GDMSESSION :          "<none>") << "\"\n";

    return 0;

}
Ihor Baklykov
  • 543
  • 7
  • 24