Use the utilities that come with the core X distribution.
There are so many GUI utilities, available either as part of your favorite Window Manager or as a separate installation, that you can forget that the core X distribution also provides several useful and lightweight programs. Do you need to monitor console messages, manage your clipboard, send pop-up messages, or create and view screenshots? Before you hit the ports collection, give the built-in utilities a try.
9.13.1 Seeing Console Messages
In [Hack #42], we saw how to redirect console messages. If you're using an X session, the xconsole utility fulfills this purpose. To start this utility, simply type its name into an xterm or use the Run command provided by your window manager.
By default, only the superuser can start xconsole. A regular user will instead receive a Couldn't open console message. This is a safety precaution on multiuser systems, preventing regular users from viewing system messages. If you're the only user who uses your system, remove the comment (#) from this line in /etc/fbtab:
#/dev/ttyv0 0600 /dev/console
If you spend a lot of your time at an X session, consider adding xconsole to your ~/.xinitrc file so it will start automatically (see [Hack #9]).
9.13.2 Managing Your Clipboard
If you do a lot of copying and pasting, xclipboard is another excellent candidate for automatic startup. This utility stores each of your clipboard selections as a separate entity, allowing you to scroll through them one at a time in a simple GUI window. In addition to the Next and Prev buttons, a Delete button lets you remove unwanted items and a Save button allows you to save all of your items as a file.
9.13.3 Sending Pop-up Messages
Do you find yourself starting a command that takes a while to execute, continuing your work in an X session, then returning periodically to the original terminal or xterm to see how that command is perking along? Wouldn't it be easier to send yourself a pop-up message once the command completes?
For example, suppose I want to know when the script from [Hack #80] finishes. I could execute that script as follows:
#~/bin/mycustomupgrade.sh && xmessage -nearmouse cvsup is complete.
When the upgrade completes, a pop-up message with the text cvsup is complete. will appear in my X session near my mouse. That message will disappear once I click on the Okay button.
If you're in the habit of using su -l to provide a new login when you become the superuser, you'll find that the preceding command will fail to send you a pop-up menu. (I'm assuming you're logged in as a regular user when you start your X session. You should be!) Instead, you'll receive this error message:
Xlib: connection to ":0.0" refused by server Xlib: No protocol specified Error: Can't open display: :0.0
This has to do with the X authorization process. If I start my X session as the user dru and use su to execute a command, I'm still logged in as dru, so I'm allowed to send a message to my display. However, if I use su -l to execute the command, I'm no longer logged in as dru but as root. The X server refuses to let another user interfere with my display, which is a good thing.
A quick workaround is to not use su -l when sending pop-up messages to your regular user account. An alternative is to understand the X authorization process. You can then use this knowledge to enable the superuser to send a message to any user on any display.
188.8.131.52 Understanding X authorization
Your X server uses a token known as an MIT magic cookie to provide authorization. When you start your X session, the server creates and stores this unique cookie in ~/.Xauthority. You can view it at any time using this command:
% xauth list genisis/unix:0 MIT-MAGIC-COOKIE-1 7e7bc20f9413469a7376e2e5c91aa6f1
Take note that you're the only user with access to this file:
% ls -l ~/.Xauthority -rw------- 1 dru wheel 101 Feb 18 13:28 .Xauthority
Always keep in the back of your mind, though, that file ownership does not matter to the superuser. For example, if I need to send an important message to the user dru, I can ssh into the system she's working on and become the superuser. Then:
# cp ~dru/.Xauthority .
I now have a copy of dru's magic cookie. However, before I can use it, I'll first have to change my display. Since I sshed into a terminal, I currently don't have one:
# echo $DISPLAY DISPLAY: Undefined variable.
I don't want just any display, I want the display dru is currently using. I can find the name of her display by reading her magic cookie:
# xauth list genisis/unix:0 MIT-MAGIC-COOKIE-1 7e7bc20f9413469a7376e2e5c91aa6f1
The name of her display is genisis/unix:0, where genisis represents the hostname of the system. I'll now attach to that display and send my message:
# setenv DISPLAY genisis/unix:0 # xmessage -nearmouse Time to go home, Dru... (prompt hangs until dru responds by pressing the "Okay" button)
This cheat works on any system to which you have superuser access. Technically, you can execute any command X understands in a user's X session once you have his cookie and display. Do remember to use your superuser powers for good, though.
9.13.4 Taking Screenshots
Have you ever needed to send a user a screenshot? There are ports available for this purpose, but the built-in X command xwd will suffice. Creating a screenshot is a simple matter of:
% xwd -out screenshot.xwd
The command will appear to hang as it waits for you to click your mouse on the portion of the screen you'd like to capture. Use the -root switch to capture the entire screen and save yourself a click.
You can view and manipulate the resulting file with most third-party image editors, including xv and gimp. For quick viewing, though, nothing beats the built-in xwud:
% xwud -in screenshot.xwd
Your results won't seem that impressive if you use xwud immediately, as your screen still probably looks like your screenshot. When you're finished viewing the screenshot, press Ctrl-c.
9.13.5 See Also