MacDevCenter    
 Published on MacDevCenter (http://www.macdevcenter.com/)
 See this if you're having trouble printing code examples


Hands on X11

by Dan Benjamin
03/14/2003

Editor's note--X11 for Mac OS X offers a complete X Window System for running X11-based applications on your Mac. This implementation of X11 includes a window server, libraries, and basic utilities such as xterm. In this article, Dan Benjamin shows you how to install and use the latest beta offered by Apple.

Installing X11

In typical Apple fashion, getting and installing X11 for Mac OS X is a breeze. Just go to the X11 for Mac OS X Download Page and download your copy. It's a big download (41.8MB at the time of this writing); unless you've got a fat Internet connection, consider getting a cup of coffee while you wait.

Once the download is complete, the X11UserForMacOSX.dmg file should expand and create an X11 User for Mac OS X folder on your desktop. Be sure that your system meets the minimum requirements listed in the Read Me file prior to installation. Then, just double-click the X11UserForMacOSX.mpkg file inside the folder.

After installing, you can start X11 by launching the X11.app found in the /Applications folder.

The X11 icon will appear in your dock, and an xterm window will appear. An xterm window is a lot like a Terminal window, except with fewer features. It is the window you can use to launch and control your X11 applications by typing commands. There's no reason why you couldn't also use Terminal for this, except that xterm opens with the correct environment variables already set for you. These environment variables are what tells the system where to look for X11 applications, and how and where to display them.

Screen shot.
X11 Dock Icon

Running Your First X11 App

Apple's X11 software includes more than just an xterm. In fact, it is a full distribution of XFree86 4.2.1, and includes all of the standard X11 programs and utilities. Let's experiment with a few of them, using xterm to launch them. If you're not comfortable working with Mac OS X's command line interface, you might want to read Chris Stone's article, Learning the Terminal in Jaguar.

We'll start with a simple and fun application called "xeyes". To launch it, just type xeyes & at the prompt in your xterm window, and press return. The "&" symbol, when typed after a command, tells the system to run the program in the background, which returns control of the xterm back to you (giving you a prompt).

As you'll quickly see, a set of big googly eyes will appear. As you move your mouse, the eyes will follow your cursor around the screen.

Screen shot.
X11 xeyes.

Feel free to resize the window, and watch as the eyes grow and change shape smoothly, thanks to Quartz rendering and the Aqua GUI. It's true that xeyes is completely useless, but it's fun. To close xeyes, just like any other Mac OS X app, click the close button in the upper left hand corner.

There are many X11 utilities, all located in the /usr/X11R6/bin folder (visible only from the xterm or a Terminal window, not from the Finder). Because X11 sets the xterm's path variable, you can launch any of these apps right from the xterm command prompt by typing their name and pressing return. Most of these programs are not of much interest to the OS X user, as they are geared more toward the management and tuning of an X11 system (rather than an OS X system).

Mac OS X Hints

Related Reading

Mac OS X Hints
The 500 Most Amazing Power Tips
By Rob Griffiths

In upcoming articles, we'll discuss installing and using some useful X11 applications that have been ported to Mac OS X as a part of the Fink project, like Gimp, a popular open-source image editing application.

For now, however, we will focus on the X11's networking and "application sharing" capabilities. In order for these examples to work, you'll need either another Mac OS X system running X11 or a Linux (or some other Unix) system with an X11 distribution already installed. If you don't have one of those other machines available, just follow along.

Networking X11

In the old days, your big Unix servers ran all of the applications, and you used an X Terminal (a machine with a screen and keyboard but no hard drive or CPU of its own) to use those applications.

Today most Unix systems (Mac OS X included) still support this functionality. Let's go back to your open xterm window. In order to allow other systems to display their applications on your screen, you must first give them permission to do so. You do this using the xhost program (you can learn more about the xhost command by typing man xhost at the xterm prompt). From the command prompt in xterm, type xhost and press return. Unless you've already modified the default permissions of your system, you'll most likely see a message like this:

access control enabled, only authorized clients can connect

What this tells us is that your system will only let authorized computer systems display X11 applications on your system. For security purposes, this is a very good thing. You wouldn't want someone displaying applications on your system without your authorization. In order to allow other machines to do this, you need to grant them access from the prompt. For example, I'd like to allow Gomez, a FreeBSD server without a monitor or keyboard, to display it's applications on my Mac. Gomez's IP address is 10.0.1.204, so I type xhost +10.0.1.204 at the prompt, and it responds with

10.0.1.204 being added to access control list

Alternatively, if I'm sure that my network is secure behind a firewall, and I'm not worried about possible security issues, I could also have typed xhost + at the prompt. This allows any and all machines to use my Mac's display. The system would respond with

access control disabled, clients can connect from any host

Now I'm ready to launch an application from the remote system. I've ssh'd to Gomez, where an entire X11 distribution and additional applications are available to me. So, let's launch something simple, xclock for example, and watch it appear on my Mac. I type xclock & at the Gomez command prompt and press return, but instead of seeing an xclock appear on the Mac, I get this error:

Error: Can't open display:

The cause of this error is that all X11 systems, by default, will try and use their own display for any X11 applications you launch. In the case of Gomez, there is no keyboard, mouse, or monitor, and no X Server running. So I need to tell Gomez to use my Mac's display instead. The Mac's IP address is 10.0.1.2, so, from Gomez's command prompt, I type:

export DISPLAY=10.0.1.2:0

I use the export command for this because I'm running the bash shell. If I were running the tcsh shell, I'd have typed:

set DISPLAY=10.0.1.2:0

In either case, unless something went wrong, the system should return a command prompt, and you're ready to go. This command tells the remote system to use the first X Windows display running on the machine with the IP address of 10.0.1.2. In other words, it treats the Mac as if it were its very own monitor, keyboard, and mouse. Now, we type:

xclock &

This time, instead of an error, Gomez returns a process number (we ran the program in the background using the & symbol) and we see the xclock display on the Mac.

Screen shot.
X11 xclock.

Neat, but not exciting. And we've already got the xclock application on our Mac. On this Mac, we use Safari, Camino, and Internet Explorer, but don't have Mozilla installed. Wouldn't it be neat to run FreeBSD's Mozilla on the Gomez and have it display on the Mac? From the command line, we type mozilla &.

The server responds with a process number, and after a few moments, Mozilla appears on the Mac's screen, as shown in this screenshot. Notice the un-Mac-like fonts, and the information on the about page which lists "FreeBSD i386" as the build version. This is because Mozilla is actually running on the FreeBSD server and is merely being displayed on the Mac.

X11 can be used to manage many remote Unix systems in this fashion, an unlimited amount, actually, all the while keeping your Mac's system resources low (only the X11 application runs locally). This is very handy when running applications on Unix systems that lack a monitor and keyboard but require a graphical display. The Oracle installation program is an excellent example of this type of application.

Final Thoughts

We've only just scratched the surface of X11's true potential. Not only do you now have the ability to run a huge collection of X11 applications on your Mac, but you can also run an X11 application from any Unix system you can connect to.

Dan Benjamin is a business strategist, programmer, writer, interface designer, and photographer.


Return to the Mac DevCenter.


Copyright © 2009 O'Reilly Media, Inc.