Friday, March 30, 2012

Where's the Beef?

The goodies you seek are here. and/or here.  Read on if you want to know why.

But first, a note about the goodies.  Much of it is intended for either iz2s or iz2jffs.  They're basically the same thing except iz2jffs is much smaller and resides on the internal   flash, whereas iz2s is installed on an SD card.  Both are hacked up systems that make use of the stock kernel that comes pre-installed on the zipit.  It's an ancient kernel, but I like it for my purposes.  Now since the entire thing is hacked together there's no real package management system for beginners.  Sorry about that.  You might just have to learn your way around the linux file system if you want to get a lot out of this.

Anyhow, if you want to use lots of this stuff, then your best bet is to  boot from an iz2s SD card.  Otherwise, if you know what you're doing, you can use all of the goodies with an iz2jffs setup, but it takes a little extra work to get it to recognize the libraries and paths used by the stuff.  The addz goodie pack does this for you for some of the older goodies that can't possibly fit on the jffs.  You'd have to do something similar to the addz pack to adapt the newer goodies to work with the jffs.  I may do it someday, but right now I'm booting from the SD card most of the time.

And now for the story of the goodie bag...

It's been said that the internet giveth and the internet taketh away.  And sometimes that's true.  It's also been said that the internet never forgets, but that's another story.  This one is about give and take.  Previously my cable provider of 10 years decided to eliminate their crappy (but free!) web hosting service because absolutely nobody was using it, except me.  So the Zipit Goodie Bag had to pack up and move.  Rkdavis graciously provided a new home for it, but it's fallen into a bit of disrepair lately.  I suspect he's taken in too much raspberry pi vapor, and will be back when the buzz wears off.

In the meantime I've been keeping things patched together by hosting new goodies at datafilehost.com.  But already some of the links have gone dead due to lack of interest.  Imagine that!  My 40MB IZ2S emacs package has gone missing due to lack of downloads.  Don't you people realize the awsome power of a fully operational emacs?  Even my own son has turned against me, recently declaring allegiance to vi instead of emacs.  I've seen this movie before and let me just say, it doesn't end well for the zipit.

But now Dronz has provided the Zipit Goodie Bag with a new summer home in France.  So it's time for some spring cleaning.  I'm planning to prune all the dead links in the blog and redirect them to this post, where I still have the editing power to keep things up to date.  From now on, this post will tell you to where all the goodies actually reside.

Here's the Zipit Goodie Bag Summer Home, including the new jffs goodies.
And here's the old tried and true (but in need of an update) Zipit Goodie Bag.

Monday, March 12, 2012

In the Zone

I decided to use my renewed focus on the jffs to try and see if I could squeeze gmenu2x onto the stock zipit jffs along with gmu. (I've already fielded a few not so subtle hints from my co-worker that the linux command line is perhaps not the easiest way to use the zipit.)  This wasn't gonna be easy though because gmenu2x is fairly bloated with C++, freetype, and lots of png files.

The first thing I did was to cut it back to a minimal installation with just the default skin, only one translation (french), and fewer icons and the smallest background image file I could find.  See above.  I also ran all the images through pngcrush for good measure.  This resulted in a gmenu2x installation that was almost small enough for the jffs.  Unfortunately it didn't quite work when run from the stock busybox shell.  I had to build a tiny killall executable to enable my previous uclibc workaround from the iz2s build.  Then I had to make sure to "exec gmenu2x" from my gmenu wrapper script or it would complain about various tty ailments whenever it finished running an app.  Weirdness.

Next I attempted to link gmenu2x without libstdc++ according to these instructions.   But gmenu2x uses STL strings, dynamic casts, and more from libstdc++ so the best I could do was skip exceptions and static link with libstdc++.a.  The shared lib is about 800k stripped, and would would use about 300K compressed on the jffs, so I managed to squeeze a little bit out of it.  I recently discovered the jffs seems to garbage collect on boot up, which means I may have a reliable way to compare the space usage.  So I'll probably try to install the full lib someday to see exactly how much I saved.  Actually, I suspect I'll hold off on that until I find another C++ program that I just gotta have...

I also tried static linking libfreetype, but that didn't save anything.  A stripped libfreetype is 400K, or about 200K compressed on the jffs, so I bit the bullet and stuck it on there.  This meant I could also toss in the dgclock program for only 20K if could trick it to use the same font as gmenu2x.  So I moved the /mnt/ffs/gmenu2x/skins/Default/font.ttf file to /mnt/share/fonts/font.ttf and replaced it in it's original location with a soft link.  Then I soft linked the mplus-2p-medium.ttf file used by dgclock to font.ttf and voila!  The dgclock works.  The gmenu2x font file is fairly compact but I can barely tell the difference in the dgclock program.  It might have slightly narrower glyphs.  Maybe it'll be good enough for a few more small SDL-ttf programs.
This fit onto the zipit, but just barely, so I swapped out the wpa-supplicant and wpa-passphrase for my cut down versions.  The ones from the V1 firmware (and IZ2S) are fairly large, and the ancient wpa-supplicant was having troubles connecting reliably to WPA2 routers anyhow.  The new smaller version actually seems to work better.  This got me to about 700K free on the jffs.  That's enough room for some more small goodies.

While working with dgclock, I noticed the zipit appeared to be stuck in a different timezone (somewhere in europe?) instead of EST5EDT.  I'm not sure what caused it, but eventually I deleted the /mnt/ffs/TZ file and rebooted.  The timezone setting script popped up.  I entered EST5EDT, and it recovered.  Then I realized I was off by an hour because the 2007 daylight savings rules weren't incorporated into the iz2s uclibc.  Fortunately Dom pointed out the uclib TimeZone fix for the US 2007 daylight savings change in one of the comments here.    Down at the bottom it mentions the magic EST5EDT,M3.2.0,M11.1.0 setting for the /mnt/ffs/TZ file.  EST5EDT,M3.2.0,M11.1.0 is US Eastern Time with Daylight Savings Time starting on the 3rd month (March), 2nd week, day 0 (Sunday) and continuing until the 11th month (November), 1st week, day 0 (Sunday).

Here's a combo jffs image containing the base, gmu and gmenu2x packages to make it an easier install for newbs.  Note:  I said "easier", not "easy".

gmu-gmenu2x-jffs.tgz   Here's the install instructions.

And apparently the Goodie Bag was offline for a while.  Sorry.  I think I'll try and mirror it...

Wednesday, March 7, 2012

Uncontrollable Urges

In the midst of all my utf8 sleuthing I got the urge to revisit my problem using bash (instead of the busybox shell) to type extended characters with the European keymap.  Running loadkeys eurokeymap.map says, "assuming iso-8859-15 euro".  Hmm, 8859...  I went back to Prototypeur, followed the french wikipedia link and finally realized my error...  Ah ha!  That's not utf8.  It's just a full 8bit codepage.  The french wikipedia page is actually clearer than the english version because the compact table layout makes it obvious at a glance that this is single byte encoding.  The english table is bloated and confusing with octal numbers and redundant hex values.  Who really cares about the octal these days?  Octal's about as useful as Roman numerals.  The future belongs to hex.

To make it work I want bash to run 8bit clean instead of 7bit only.  Maybe that'll help for for utf8 too.  Here's how.

Make a .inputrc file in the home directory and put this in it. Or try a global /etc/inputrc file.

# .inputrc controls how programs using the readline library behave.
# This includes bash.

### Make Bash 8bit clean ###
# Enable 8bit input
set meta-flag on
set input-meta on
# Turns off 8th bit stripping
set convert-meta off
# Keep the 8th bit for display
set output-meta on

As if that weren't enough, while all of this was going on I finally got around to packaging up the gmu music player for the jffs.  This came about because a co-worker suddenly decided he was ready for his zipit that I'd promised to deliver (as soon as it was done).  You know how that goes with software.  It's never really done...  Anyhow, I came up with 3 potential configurations for the jffs on this zipit:  rockbox for mp3s and games, gmu for mp3s and internet radio, or mplayer and some simple bash wrapper scripts like these for mp3s, internet radio, and possibly some video.   The gmu choice was selected so I finally put that together with all the required libs.  There's still about 1.2MB on the jffs for extra goodies, so I really need to identify some programs that make use of all those shared libs to get the most bang for the buck...

Meanwhile, in testing the gmu package, I discovered that my latest base jffs was horribly busted.  The timezone script was missing all the /dev/tty0 redirection that's required for scripts run before owntty in the boot sequence.  Oops, that little mistake left you hanging on a blank screen, waiting for you to select a timezone from an invisible list.  Not good.  Here's a replacement timezone script for the 2 people who downloaded the broken jffs base.

tz-iz2jffs.zip

Here's a gmu package suitable for the stock jffs.  I preloaded the playlist with a few stations.

gmu-0.8-iz2jffs.tar.bz2

Tuesday, March 6, 2012

Terminally Insane

This one is a long rambling mess.  Sorry in advance...

I found a simple program at the Linux Productivity Magazine to display the curses ACS special characters.  When I tried it I discovered that some looked wrong, and some were missing on the zipit.  All of the box drawing characters there but the arrows were garbled, the "lantern" or vertical tab char was missing, and all of the 1 3 5 7 9 single scan line glyphs were missing except for the ones that do double duty as the dash and the underline.

I took a peek at the unicode table from my console font and discovered the unicode mappings for the scan line glyphs were way up in the Chinese section of the giant unicode table.  So I moved them back down into the 0x2xxx range, rebuilt the font file and voila, the scan lines were there.  Turns out ACS_LANTERN was just missing from my mcurses code. The arrows were a different story.  They're not included in the 32 characters of the DEC special graphics set.  I need to print a unicode character in order to display them, so for now I went with the default standby ASCII substitutes of <>^v for the arrows and # for ACS_BOARD and ACS_BLOCK.

It drives me nuts that I've got all these nice glyphs in the font and yet have no way to display them.  So I went looking for some simple C code to print a unicode character.  This turns out to be all tangled up with a monster sized locale and nls system on my desktop linux box. 

There are about a dozen environment variables that control all sorts of settings.  So I tried setting these up for US english with UTF-8 according to the notes on the internet.  Not only did this have no effect, but when I try to set LC_CTYPE something (bash, I think) spits back an error message.  WTF?  It's just an environment variable!  Shut up and set it.  You might think its some sort of conspiracy to force a huge bloated locale database onto the zipit in /usr/lib/locale.  Ok, it's only 2.5M but that's too much for the jffs and I think the FAT filesystem of IZ2S will bloat that way up because it can't use soft or hard links to avoid duplication.  Plus I really don't need the zipit to know what order I want my socks sorted, or what time in the afternoon to serve me tea.  I just want to be able to print all of the characters I loaded into my font.  Is that too much to ask?

Well I did some soul searching, and some google searching and eventually unearthed the magic escape sequences to put the terminal into UTF-8 mode and back (ESC%G and ESC%@).  Armed with this knowledge I wrote a short C program to print the ACS_BLOCK character.  And it worked!

Well, it worked in my gnome term on the desktop linux, and it worked on the zipit linux console in both bash and the busybox ash shell.  But it didn't work in gnu screen or dvtm.  Fooey!.  Actually it turns out I can make it work in screen if I issue the ESC%G from a shell script first and then run "screen -U".  Ok.  I can live with that, but dvtm just eats the ESC% and spits out the G. (I can't remember, but I think this script may have finally let me type UTF-8 at the bash command prompt, instead of just in the busybox ash shell.)

Fortunately the dvtm code is pretty straightforward (unlike screen, which may have a secret built-in nethack mode?). I found the ESC handler code and added a little patch to eat the entire ESC%G sequence and toggle the internal is_utf8 flag.  But then dvtm just prints three nonsense characters for my 0xe2,0x86,0x88 sequence instead of converting to the proper unicode BLOCK glyph.  The man pages tell me mbrtowc and the reverse wcrtomb functions are under the influence of LC_CTYPE.  It's that darn conspiracy again! 

Back to the internet for a workaround.  This sorta spells out the problem and it gives a hint about the leader of the conspiracy: glibc.  I'd bet uclibc doesn't even know about those locale files.  Anyhow, I found some magic c code to convert utf8 to utf16 with no mention of the evil LC_CTYPE conspiracy.  It worked somewhat.  Debugging showed everything converted ok inside dvtm, but ncurses still eats the final result, no matter what form I try to push through.  Probably in league with the conspiracy.  So now what to do?

Maybe I should try to build a small ncurses example and get it working before moving back to dvtm. 

Or maybe I should do something about the nl_langinfo() function.  This function is used by both the dvtm vt.c init code and the ncurses ncurses/tinfo/lib_setup.c file to determine if the tty is in UTF-8 mode.  The nl_langinfo function is in the uClibc libc.so.0 library and I suspect it's just a stub, hardcoded to POSIX and C locales.  According to the uclibc FAQ you can compile in UTF-8 locales but that makes it significantly bigger, depending on the number of locales. (http://uclibc.org/FAQ.html)  The IZ2S libc is pretty small at just under 300K.  Plus you supposedly had to fetch uClibc-locale-030818.tgz manually to build the locale support, so I doubt it's in there.  Grep found the word POSIX, but no mention of UTF, LC, or LANG.  At least uClibc seems to be on my side, resisting the bloat, just not in a useful way that lets me use my font.

It looks like I could undefine HAVE_LANGINFO_CODESET and recompile ncurses so it uses an internal nc_get_locale() function instead.  But then I might have to undefine HAVE_LOCALE_H so nc_get_locale uses getenv instead of setlocal(LC_CTYPE, 0) which always returns "C" instead of "en_US.UTF-8" (or anything.UTF-8).  And what about the NCURSES_NO_UTF8_ACS environment variable hack?  I may need to set that once I'm in utf8 mode in ncurses.  There appear to be some special case hacks in the code to do this for gnu screen and when TERM=linux.

I ended up with a special version of nl_langinfo() that scans the environment vars for UTF-8.  I also bypassed setlocale() in the ncurses lib_setup.c file and now I see a UTF-8 locale.  But the meta and control conversions are taking precedence over the characters.  e2 94 a4 is printed as M-b ~T M-$.  And the block glyph e2 96 88 is printed as M-b ~V ~H.  Darn it!  Ncurses is a tough nut to crack!

Anyhow, dvtm is off the hook.  I can't even get a simple standalone ncursesw program to print utf8.  Worse, I can't even get the test programs that come with ncurses to print utf8!  I'm either building it wrong, or something in uclibc is broken.  Right now, I can't even tell if ncursesw is even using the wide char routines, though it is 20K larger than the non-wide ncurses.  Stupid speed optimization macros make it really hard to decypher the code.  Besides, it's more likely that some other fn like isprint or isascii from uclibc is steering ncurses the wrong way.  Apparently ncurses uses just about every single function that deals with text somewhere.  No wonder it's so fat.  And yes, it uses isprint() in multiple places, and the man page implies isprint() needs a working setlocale() to handle utf8.  However the uclibc implementation of isprint is a friggin ugly macro in ctype.h, so I can't easily replace it.  The macro doesn't look like it even considers utf-8.  That means multiple hacks will be needed in the obtuse ncurses code instead.  Ouch.

Meanwhile the ncurses source directory has a wcwidth.h file that looks suspiciously like the substitute wcwidth fn I dug up.  And there are tantalizing hints in the test directory that just maybe there's a configure option to use libutf8 instead of the libc defaults.  Is that just for the test programs?  Maybe I should try it.  Then if it works try it with my test program, and then dvtm if that works.  Unfortunately I think it's a hidden configure option.  configure --help=short gives no clue how to change it.

My list of hacks was getting sorta big, so I decided to try libutf8 to replace the whole pile of crap.  It's kinda big though at 160K, and the easier to use shared lib LD_PRELOAD plug version wouldn't link in my scratchbox toolchain.  So in the end, all I can say is that I'm still working on on it...

Someday when I get things together I'll post the updated fonts, and maybe an new dvtm-wide.