The standard C method for determining the presence of an X Window:
#include <stdlib.h> if (NULL == getenv("DISPLAY")) is_gui_present = false; else is_gui_present = true;
- this allows you to distinguish between pseudo-terminals in a terminal emulator and the clean start of tty.
If you want to determine if there is a shell at all, or if the application was launched, say, from the file manager, then this is not easy: both cases are simply calling the exec system call from the shell or the file manager / GUI manager (often with the same parameters), you need to explicitly pass the flag to see this.
PS I just found a way to do this: check the environment for the "TERM" variable - it is installed for the shell and is inherited to the Qt program, it is often not installed in the graphical interface. But do not take this as an exact decision!
source share