Akom's Tech Ruminations

Various tech outbursts - code and solutions to practical problems

Linux amixer toggle mutes but does not unmute

Posted by Admin • Monday, July 22. 2013 • Category: Linux
It seems that the common approach to mute the audio in Ubuntu 13.04 is amixer -q sset Master toggle ... which works great when muting, but fails to unmute. This command is, for example, the default for openbox's handling of XF86AudioPlay shortcut.

The problem, it would appear, is that muting the Master channel causes other channels (eg Headphone and Front on my machine) to be muted as well. I have no idea why this works this way and why it doesn't undo what it did... and, frankly, I don't really care that much. It's much easier to code around this, see the following workaround script:

Continue reading "amixer toggle mutes but does not unmute"

Switchable Storage Locations in an Android App

Posted by Admin • Saturday, October 13. 2012 • Category: Android
This is also a typical pattern. In my app, I need to offer the user a choice of storage locations without getting too low-level. I am content with offering three options in a ListPreference:
  1. Private to application
  2. Public but on the main storage
  3. Public but on the removable storage (actual SD card)
Here is how I am doing it in a relatively generic and reusable manner (using an enum)

Continue reading "Switchable Storage Locations in an Android App"

Getting location of the removable storage on Android

Posted by Admin • Saturday, October 13. 2012 • Category: Android
Seems like a fairly typical problem, yet there are dozens of implementations and no standard. Here is mine:

        private static File s_removableStoragePath;

        /**
          Utility function that attempts to find the removable storage directory
         
(as opposed to {@link Environment#getExternalStorageDirectory()} which is usually non-removable)
          by running "mount" and parsing the output.   This implementation looks for
         
the strings "vfat" and "vold", eg:
          /dev/block/vold/179:97 /mnt/extSdCard vfat rw,dirsync,nosuid,nodev,noexec,noatime,nodiratime,uid=1000,gid=1023,fmask=0002,dmask=0002,allow_utime=0020,codepage=cp437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro 0 0
         

          The result will be cached indefinitely.
         

         * @return either a valid file or null.  This may not be accessible, but it does exist
         */

        public static File findRemovableStorage() {
                if (s_removableStoragePath != null && s_removableStoragePath.exists()) {
                        return s_removableStoragePath;
                }
                s_removableStoragePath = null;
                String result = null;
                try {
                        Process process = new ProcessBuilder().command("mount")
                                        .redirectErrorStream(true).start();

                        process.waitFor();

                        InputStream is = process.getInputStream();
                        BufferedReader isr = new BufferedReader(new InputStreamReader(is));

                        String line = null;
                        while ((line = isr.readLine()) != null) {
                                if (-1 != line.indexOf("vold") && -1 != line.indexOf("vfat")) {
                                        String[] blocks = line.split("\\s");
                                        if (blocks.length > 2) {
                                                result = blocks[1];
                                        }
                                }
                        }
                        if (result != null) {
                                s_removableStoragePath = new File(result);
                                if (!s_removableStoragePath.exists()) {
                                        s_removableStoragePath = null;
                                }
                        }
                } catch (Throwable t) {
                        Log.e(IntegratorUtils.class.getSimpleName(),
                                        "Unable to find external mount point");
                }
                return s_removableStoragePath;
        }
       
 

Linux motion drops MySQL support, but you can get around it with a shell script

Posted by Admin • Monday, September 24. 2012 • Category: Linux
I use motion as a surveillance system for a sizable office (9 cameras). Motion is good. But the current package of motion in Ubuntu 12.04 is no longer compiled with MySQL support (used to have it in 10.04). Compiling motion from source really didn't sound appealing... but I do want to add events to a database for my own in-house motion history browser. What to do?

Motion supports command execution on events, such as on_movie_end and on_picture_save... which makes it really easy to do a one-liner like this:
on_movie_end bash -c "echo insert into security(camera, filename, frame, file_type, time_stamp, event_time_stamp, event_id) values('%t', '%f', '%q', '%n', '%Y-%m-%d %T', '%C', '%v') | mysql -uUSER -pPASS"
if you'd like to do something fancier, you can break it out into a shell script:

#!/bin/bash

logger -t test "$0 executing with $*"
sql="insert into security(camera, filename, frame, file_type, time_stamp, event_time_stamp, event_id) values('$1', '$2', '$3', '$4', '$5', '$6', '$7')"
echo $sql | mysql -u$U -p$P motion
 
and invoke like this:
on_movie_end U=USER P=PASS bash /usr/local/sbin/motion-insert-event.sh '%t' '%f' '%q' '%n' '%Y-%m-%d %T' '%C' '%v


(same for on_picture_save, if needed). You can also daisy-chain commands with semicolon if you have multiple. Also, note that the query above is for my table, yours may have different columns. The rest are my specific examples of usage:

Continue reading "motion drops MySQL support, but you can get around it with a shell script"

Code and Hacks Converting .NET String.format pattern to Java with a regular expression

Posted by Admin • Monday, September 10. 2012 • Category: Code and Hacks
For this exercise in futility, I want to accomplish the following:

From .NET: "Hello, {0} Today is the {1} day"  
To JAVA: "Hello, {%1$s} Today is the {%2$s} day"


Note that a simple regex would work here if it weren't for the zero-based indexes. We need to increment the number as well do a pattern replace. Since I'm comfortable with PHP, I took a sample from preg_replace_callback description and modified it accordingly:

Continue reading "Converting .NET String.format pattern to Java with a regular expression"

Linux Splunk high CPU utilization starting July 1, 2012

Posted by Admin • Thursday, July 5. 2012 • Category: Linux
This has to be written down for posterity: My single server installation of splunk started using all of the CPU about then. Nagios caught it, but I had no time to debug it. I tried disabling indexes, apps, and inputs - none of that made any difference. I reduced an index size, which again had no effect. I cleaned it out and installed it fresh (empty database), only to see the now familiar steady CPU usage.

In desperation I went to open a question on splunk support forums where it suggested I read a related thread which in fact had the solution: http://splunk-base.splunk.com/answers/52109/universal-forwarder-high-cpu-after-leap-second-correction Apparently the culprit is the leap year second inserted at midnight on June 30. Why that makes splunk go nuts I'm not quite sure, but i did see a warning about it in my dmesg:
[3073222.768708] Clock: inserting leap second 23:59:60 UTC

Apparently this is the same problem that took down Amazon. The solution involves running a perl command to alter the system date ever so slightly in the absence of ntp.

Linux VirtualBox Extension Pack on Ubuntu Precise Pangolin 12.04

Posted by Admin • Thursday, May 31. 2012 • Category: Linux
Not sure why this so unintuitive, but here goes:
  1. You want to use USB 2.0 with Virtual Box on Ubuntu 12.04 (or recent version
  2. You're told to install the extension pack from this page
  3. The download is not the same version as your version of virtual box itself
  4. It doesn't work

Do not despair:
  1. Shorten the extension pack download URL to http://download.virtualbox.org/virtualbox/
  2. Find a directory matching your version ( dpkg -i virtualbox )
  3. Download the extension pack

Yay!

Linux Autodesk Maya 2012 in Virtualbox

Posted by Admin • Wednesday, February 22. 2012 • Category: Linux

Maya does not appear to have a Linux version, so where I have a single machine, I want to run it in VirtualBox - in either Windows XP or Windows 7. I've successfully installed it in both, 32 and 64 bit, and it installs and starts up just fine. The fun comes later. There are two serious problems:

  1. Alt+Mouse doesn't work. Well, this has to do with my window manager more than anything - Alt+Mouse is usually used in Linux. I'm not willing to change that - but fortunately, Alt-Shift+Mouse does something fairly similar in Maya
  2. 3D repainting is completely messed up. In fact, there are two things that don't work immediately - selection on the 3d pane (pointer appears to be below the screen) and the Spacebar popup menu (also below the screen). Fortunately, this can be fixed with a bit of sacrifice: turn off 3d (I turned off 3d and 2d) acceleration in Virtualbox settings.



  3.  
  4. And it works! (not horribly slow either)

Linux Using dual widescreen monitors on old hardware with Ubuntu

Posted by Admin • Tuesday, February 21. 2012 • Category: Linux
My primary development machine is probably 10 years old - it's a Thinkpad T42 in a docking station (Dock II). This is fine - it's fast enough for most purposes, it runs dual monitors, and it uses very little power so I can leave it on at all times.

Not so fine once I got a 24" monitor to go with the standard 20".

First, the DVI hardware in the docking station can't handle high resolutions - it worked fine at 1280x1024, but not so great at 1920x1080. That's OK, I can drive the widescreen with VGA, I can't tell the difference. But now everything on the screen repaints very, very slowly - I mean really slowly - 1/2 a frame per second. This is bad. OK so the video chip in the laptop wasn't intended to drive a combined 3200 x 1080 resolution - but what can I do about it?

Continue reading "Using dual widescreen monitors on old hardware with Ubuntu"

Hardware Hacks Upgrading Dell laptop memory and the blinking CAPS LOCK led

Posted by Admin • Wednesday, January 18. 2012 • Category: Hardware Hacks
Apparently if you have the power-on self-tests set to "Minimal" or "Fast" or anything besides the "Let's check everything for 5 minutes every time the machine boots", you'll have a brick with a blinking CAPS lock instead of a laptop once you upgrade memory.

The only way I found to fix this (other than clearing the BIOS) was to put the old memory back in, go into BIOS, and enable thorough self-test. With that on, the laptop notices the memory size change and does not freak out - merely tells you about it. Once you're done you can disable the self-tests.

(I experienced this on a Dell Precision M4400, but googling seems to indicate that this affects the Inspiron and Latitude lines as well, probably others).

Asterisk Getting Meetme to work in Asterisk 1.6 on Ubuntu Lucid

Posted by Admin • Tuesday, November 15. 2011 • Category: Asterisk
I'm assuming that you are attempting to use the Meetme() application and getting an error like this:
WARNING[10695]: app_meetme.c:1097 build_conf: Unable to open pseudo device
It's rather simple:
$ apt-get install dahdi dahdi-dkms dahdi-linux  #I think dahdi may be enough
$ echo dahdi_dummy >> /etc/modules
$ modprobe dadhi_dummy
$ ls -l /dev/dahdi/pseudo
crw-rw---- 1 root dialout 196, 255 2011-11-15 11:26 /dev/dahdi/pseudo
$ id asterisk
(Should include 'dialout' group)

Yay! Apparently in Asterisk 1.6 the timing source for the Meetme application is no longer ztdummy (fortunately I was unable to use m-a to compile zaptel) but rather relies on DAHDI, not only for timing but for conference mixing - see http://www.russellbryant.net/blog/2008/06/16/asterisk-16-now-with-a-new-timing-api/

Linux Figuring out the card number of a cheap SAA7130 capture card

Posted by Admin • Sunday, June 19. 2011 • Category: Linux
So you bought yourself a cheap Philips SAA7130 (or SAA713X or SAA7134) capture card to record motion, you plugged it in and you are getting a black or grey screen. Looking in dmesg you see the famous saa7134[0]: Huh, no eeprom present (err=-5)? And
saa7134: Congratulations! Your TV card vendor saved a few saa7134: cents for a eeprom, thus your pci board has no saa7134: subsystem ID and I can't identify it automatically saa7134: I feel better now. Ok, here are the good news: saa7134: You can use the card= insmod option to specify saa7134: which board do you have. The list:
What do you do? How do you try 175 card numbers and figure out which one works? Write a script!

Continue reading "Figuring out the card number of a cheap SAA7130 capture card"

Code and Hacks Migrating one Gmail account to another

Posted by Admin • Friday, May 13. 2011 • Category: Code and Hacks
I have a setup where a Google Apps Premier account is used for the active employees, but when they quit I want to archive all their mail but free up a paid user account for the next employee. We archive mail in a different domain, also on google apps - a standard edition, free. For a while now I've gone through a variety of ways of copying the mailboxes from one place to the other, and each approach was either partial, unreliable, or too time-consuming.

So I think I've finally settled on a viable approach that is neither.

Continue reading "Migrating one Gmail account to another"

Hardware Hacks Setting the date/time on the Mini Keychain Spy Camcorder

Posted by Admin • Monday, April 25. 2011 • Category: Hardware Hacks
Keychain Camcorder
The instructions that come with this little gadget are priceless in their verbatim Chinese glory, but are not helpful in their vague "Leave the details as an exercise for the reader" approach.

Here are the exact detailed steps to actually set the clock

Continue reading "Setting the date/time on the Mini Keychain Spy Camcorder"

Linux Backing up cPanel without hitting logout

Posted by Admin • Thursday, April 21. 2011 • Category: Linux
cPanel based hosting presents some challenges for automatic backups - there isn't an automatic way of creating local backups or any standard way of triggering their creation remotely. Numerous scripts exist yet none were quite the solution I was looking for - I am primarily interested in the databases and mail forwarders, though files wouldn't hurt either. Moreover - you can do this in a single line!

My Goal therefore is: Create and retrieve backups nightly. I don't want cPanel to push files to my off-site box, I'd rather initiate everything remotely and not have to open up access to some other system. cPanel does permit one to do this using a browser, hence it can be scripted. Really, scripting isn't even necessary - wget is all that is required! That said, I had a very hard time convincing wget not to visit the logout link on each page - once you log out, you're not getting anywhere anymore. So here is how I did it.

Continue reading "Backing up cPanel without hitting logout"