Mailing List Archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[tlug] Stupid Perl Tricks



Earlier today, I needed to take a screenshot of a piece of the UI of the
project on which I am currently working, to deliver to the tech writer.
In of itself, this was not hard, a simple matter of:

sudo emerge media-gfx/imagemagick
import screenshot.jpg

However, I used to have a couple of entries in my .bbkeysrc that set up
hotkeys for screen captures:

KeyToGrab(s), WithModifier(Mod1+Control), WithAction(ExecCommand), DoThis(import -window root screenshot_`date +%Y-%m-%d_%H-%M-%S`.jpg)
KeyToGrab(w), WithModifier(Mod1+Control), WithAction(ExecCommand), DoThis(import screenshot_`date +%Y-%m-%d_%H-%M-%S`.jpg)

But C-M-s and C-M-w were slightly unintuitive, and I really wanted to put
the bloody Print Screen / SysRq key to good use, so I decided to change
the hotkeys to M-PrintScreen for full screen captures, and C-M-PrintScreen
for mouse-defined captures. So, I blissfully changed KeyToGrab(s) to
KeyToGrab(PrintScreen) (yes, chuckle away, ye hoary hackers of X11 and
XEmacs ninjas yes I mean Steve Turnbull!) and restarted bbkeys. No dice.
Steve knows why. I did not. :)

So I try "SysRq". No dice. "PrintScrn", "PrntScrn", no dice. I Google, to
no avail.

So I break out the trusty LXR[1], for which I had just written a
webapp-config[2] ebuild[3]. Start browsing the source, find that bbkeys
reads in the KeyToGrab stuff and hands it off to a neat little X11
function called XStringToKeysym()[4]. OK, Google it, see that I can find
all the symbolic names in X11/keysymdef.h "by removing the XK_ prefix from
each name". Steve nods.

Now, I easily find that "Print" is the bloody name for the Print Screen /
SysRq key, which would have been quite obvious had I been coding X11 apps
for a few years. Steve winces.

Hurrah, bbkeys responds to C-M-Print (as I now know it) by spitting out
a JPG.

So, at this point, I decide to create a web page documenting these eldritch
symbols, so that the next time a hard drive explodes and my backups are
worth sod all, I don't drive myself into a frothing-at-the-mouth frenzy
trying to figure this out again.

Being a True Hacker(TM and I wish), I decide not to do with cut, paste, and
C-M-% what I can do with Perl. So I whip up an HTML skeleton, xkeysyms.html
and the following Perl one-liner:

perl -ne 'next unless /^#define\sXK_(\w+)\s+0x[0-9A-F]{4}\s+(\/\*\s+(.+)\s+\*\/)*/; print "      <tr>\n        <td>$1</td>\n        <td>".($3 ? $3 : "&nbsp;")."</td>\n      </tr>\n"' /usr/X11R6/include/X11/keysymdef.h >>xkeysyms.html

But now I am back to cut and paste. No good. After another 20 minutes of
pounding away, I come up with the following:

http://www.jmglov.net/unix/xkeysyms.html

Feel free to drool. I certainly did once I was done.

And then, after several hours of work, I emailed off the screenshot that
was as simple as:

sudo emerge media-gfx/imagemagick
import screenshot.jpg

Score one for the True Hacker. :)

-Josh

[1] http://sourceforge.net/projects/lxr
[2] http://www.jmglov.net/unix/webapp-config.html
[3] http://bugs.gentoo.org/show_bug.cgi?id=48459
[4] http://tronche.com/gui/x/xlib/utilities/keyboard/XStringToKeysym.html

-- 
Josh Glover

GPG keyID 0xDE8A3103 (C3E4 FA9E 1E07 BBDB 6D8B  07AB 2BF1 67A1 DE8A 3103)
gpg --keyserver pgp.mit.edu --recv-keys DE8A3103

Attachment: pgp00041.pgp
Description: PGP signature


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links