[openbeos] Re: started from shell or mouseclick ?

  • From: "Ingo Weinhold" <bonefish@xxxxxxxxxxxxxxx>
  • To: openbeos@xxxxxxxxxxxxx
  • Date: Sat, 03 May 2003 01:04:24 +0200 CEST

scholly@xxxxxxxxxxxx (Ulrich Scholz) wrote:
> moin, moin...
> 
> Is it possible for a program to detect, whether it was started
> from the Terminal or by Icon-Click ?
> 
> It doesn't matter, what Terminal (bash/muterm) or which Tracker.
> 
> In the bash I can use a script:
> 
> ======
> 
> #! /bin/sh
> 
> alert --info $TERM ok
> 
> ======
> 
> It gives my "dumb" when started by doubleclick or "beterm"
> when started from bash.
> 
> Is this "system-variable" $term also readable from an app so
> that the app can decide, whether it was started from terminal
> or tracker ?

You can retrieve environment variables via getenv() (<stdlib.h>).

> Or is there a better way to get this infornation ?

There is isatty() (<stdlib.h>) which tells you whether or not a file 
descriptor points to a terminal -- so you can invoke it with 0, 1, 2 
for stdin, stdout and stderr -- but running `mycommand < /dev/zero &> /
dev/zero' or something similar will deceive you.

BTW, also the TERM strategy is not error proof. If you run the Tracker 
from a terminal it inherits the environment from the shell, and then 
shell scripts started from the Tracker will inherit their environment 
from the Tracker, thus having a TERM variable set to `beterm'.

Assuming that the Tracker uses BRoster::Launch() to start applications, 
the only definitive way I can currently think of is to check where your 
application's initial messages come from. If it is started via 
BRoster::Launch(), they come from that application otherwise from your 
own application.

As a sketch for such a recognition, consider the following:
1) In your app's ReadyToRun() get the B_READY_TO_RUN message via 
CurrentMessage().
2) Get the message's ReturnAddress().Team(). This should be the ID of 
the sending team.
3) a) If this ID is invalid (< 0) or your own team's ID 
(BLooper::Team()), then your app was not started by someone else via 
BRoster::Launch().
3) b) Otherwise BRoster::GetRunningAppInfo() gets you more information 
about the application in question.

CU, Ingo


Other related posts: