Eduardo
Eduardo

Reputation: 77

Determine whether a program is a GUI or console application in Linux

I am programming in C++ using Qt-4 as framework, given a directory (i.e. /usr/bin) i would like to know whether the program is a GUI or console application.

I came across some information on how to do this windows and also about the inexistence of a similar (to Win) identification on Linux (within elf). Is there an alternative way to perform this on Linux?

I thought on:

EDIT0: This project is an app launcher

EDIT1: Once i have the list of programs categorized, i could launch a terminal emulator whenever the user chooses a non-graphical application

FINAL CONCLUSION:

After people answers and extra search, it really is not possible to reliably discern between console and gui applications. My best bet is to make several considerations like search for .desktop files, make a few assumptions like tools listed in utilities-only places like /bin, /sbin and /usr/sbin and so on.

Perhaps parse the output of ldd for each entry found.

Thanks.

Upvotes: 3

Views: 1974

Answers (2)

Jan Kundrát
Jan Kundrát

Reputation: 3816

If you're writing (yet another) app launcher, please follow what the people who have created all of the other app launchers did and use the .desktop files. That specification already defines the Terminal option which is exactly what you're looking for.

Upvotes: 2

A program could try to start as a GUI program, and else switch to console. (And actually some programs do exhibit such behavior, e.g. emacs). It could even do that at random, or because of some specific configuration...

For example, assuming that vi is a console program and emacs a graphical one, the following simple program may randomly be GUI or console:

 #include <unistd.h>

 int main(int argc, char**argv) {
    if (getpid()%2 == 0)
      { argv[0]="vi"; execv("/usr/bin/vi", argv); }
    else
      { argv[0]="emacs"; execv("/usr/bin/emacs", argv); }
    return EXIT_FAILURE;
 }

The simplest (but not foolproof) way of doing that is testing if getenv("DISPLAY") returns NULL. A more elaborate way would be to call XOpenDisplay which returns NULL on failure (and several X11 toolkits do that).

So, your question does not have a precise answer, and does not really make sense.

You could use ldd .... and add manually exceptions like firefox.

Upvotes: 1

Related Questions