in reply to How to detect X?

You seem to be implying that you'd maintain two versions of your application -- one which runs via Tk, and one which is a console app. This is a lot of extra work. Also, consider that X is running may not be the only appropriate time to use Tk -- Windows, for example, can run Tk apps without an X server available.

There are two problems here -- detecting whether to use a GUI, and creating an application that is dual-mode.

Since the former one is relatively straightforward, depending on the OS configuration and detection accuracy you need, let's look closely at the latter.

Maintaining two entire applications is probably not the best use of time. As soon as you need to make a change, you will have to be duplicating logic -- the potential for error is very high. To address that concern, you'll need to split up your application on the logic/interface boundary.

There are really two approaches. First, if your application is simple, you can abstract all of your interface functions (print a message, prompt the user, etc.) into subroutines. Then you can have each routine check to see if you've instantiated a MainWindow or not before deciding whether to use the GUI or text interface component.

The second, and probably better, apprach is to abstract your logic into a module. This module makes events available that a calling program can decide how to handle. Then, you write two pure-interface scripts (one Tk, one console) that present the interface and call the module for logic (and perhaps to poll for events).

The second is likely better in part because it allows a future maintainer to alter logic (say, to fix a bug) in the module without having to update either interface script. It also allows for easy expansion of interfaces -- since all the logic is in a module, adding a web interface (for example) would be simple.

Anima Legato
.oO all things connect through the motion of the mind

Replies are listed 'Best First'.
Re^2: How to detect X?
by blazar (Canon) on Feb 22, 2005 at 13:22 UTC
    You seem to be implying that you'd maintain two versions of your application -- one which runs via Tk, and one which is a console app. This is a lot of extra work. Also, consider that X is running may not be the only appropriate time to use Tk -- Windows, for example, can run Tk apps without an X server available.

    There are two problems here -- detecting whether to use a GUI, and creating an application that is dual-mode.

    Well, for one thing this won't run under Windows in any case. So that's not a problem. Said this, if you read 432975 more carefully you'll notice that I wrote "[...] in that case I would put most of the non-UI code into a module and call it from the respective '.X' and '.cmd' versions.", which is fundamentally what you suggest yourself.

    Taking into account both this circumstance and the fact that after all it will be a relatively simple application, it most certainly won't be "a lot of extra work".

    The only problem here is detecting X and running a GUI version if it is available or a CLI version otherwise, which doesn't seem to be a choice everybody agrees on, but that's another matter...

    In any case thank you for the feedback!

      Aha, I missed the few key words in your node about calling a module, my apologies. As far as "a lot of extra work", it may be a simple application now, but damned if those things don't grow at the worst possible times. ;-)

      As far as X/CLI, the most reasonable algorithm is:

      my $have_X = (!$opt_forceCLI and $ENV{DISPLAY} and try_Xconnection($ENV{DISPLAY}) or $opt_forceX; exec ($have_X && !$opt_useconsole) ? "$app.gui" : "$app.X";

      In other words, check the DISPLAY and try to connect to the X server; if that fails, use the CLI. (Also makes certain to handle the command line options to force X or CLI). Yes, this isn't 100%. Yes, there are more complex ways to do it.

      In reality, since you are allowing the user to force one or the other via a command-line, you needn't even try to connect to X. With unusual configurations where DISPLAY is set without X running (or vice-versa), the user simply needs to specify a force option on the command line.

      Anima Legato
      .oO all things connect through the motion of the mind