As has been said many times already - use $ENV{DISPLAY}. If it is set, but there is no actual display at the place pointed to by this environment variable, then the user is set up incorrectly. Lots of applications will fail, not just yours. If it is not set, but there is an actual X server available, it's probably because the user wants to use the text mode version. So don't go looking for it.

I've seen dual-mode applications before. HP's "sam" comes to mind. Don't worry about it - yours won't be the first to "fail" in this way. Think of it this way: by having DISPLAY set, the user is saying, "I want graphical applications to appear in this location I am specifying." And if that fails, who are you to go display it elsewhere? By having DISPLAY not set, the user is saying, "I don't want any graphical application." Who are you to go and create a graphical display somewhere? This is even worse - since you may create a graphical display as user Z, on user Y's display. Talk about a security hole! (Nevermind the security hole that let you put up the application in the first place - but that's user Y's problem, not user Z's.)

Using DISPLAY is just the convention with X. Trying to outsmart convention will merely annoy users who are aware of the convention, and are using it. For example, I routinely run applications as "DISPLAY= my_application arg1 arg2". This is because I prefer the text mode of these applications. Or I run "DISPLAY=host:2 my_application arg1 arg2" if I want the application to appear somewhere else (say a VNC session). Don't outsmart me - I'm the human, you're just the program running on the computer. I told you to do something, don't tell me I was wrong and do something else.

Just my perspective on it :-)

Update: See italicised text in first paragraph - added for clarification.


In reply to Re: How to detect X? by Tanktalus
in thread How to detect X? by blazar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.