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.
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.
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:~>
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.
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).
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;
}
精彩评论