Categories
Linux Mac

Automatically download iOS firmwares

IPSW

Today I discovered that it takes quite a while to download an iOS IPSW, and when you need IPSWs, you are always in a hurry. So I made this little script that checks for a new release using icj.me’s API and downloads it. The comments in the script itself should make it pretty easy to use.

I added the script to my home server’s crontab, and scheduled it to run at 1 am every night, with a low bandwidth limit not to hog my connection.

Note: to use this on OS X you will have to either install wget (from sources, binaries, brew, ports, whatever) or edit the script to use curl.

Categories
Mac

Run Hazel rule based on the day of the week

Hazel-for-Mac-iconI LOVE HAZEL.

Now that I made that clear, let’s get into the actual stuff.

As many of you will know, Hazel is a great Mac utility that lets you automatically do stuff to files. You can move files based on their name/size/extension/you name it, and do a bunch of cool stuff with them, without ever having to write a single line of code.

Today I needed to write a rule that would act based on the day of the week a file was created1.

Turns out, Hazel can do that (not much of a surprise, huh?), but this time I didn’t find it particularly intuitive how to do it. You have to use the “Occurs after” function on the “Created date” (or any date, actually). In this example, this rules will only run on files that have been created after 00.00 on Mondays only, i.e. any file created on Mondays.

Hazel rule day of week(Don’t be confused by the weird names on the “day of week” submenu, they’re just the Italian names for the days of the week.)

The cool thing is that you can select more than one day, so as always Hazel is very flexible.

  1. I use a Sony ICD PX333 Voice Recorder to record classes, and I want to rename them based on the day of the week, since what we do depends on the day of the week
Categories
Mac

Clipboard new-line format when copying from Preview

Today I helped my brother with a Keyboard Maestro macro he needed. Basically, he wanted to take some text from a PDF created by Notability, which inserts new lines to have text flow around images, and remove these new lines.

Preview

Pretty easy, I thought: pbpaste to tr and remove \n. Nope. For some reason, maybe some old Mac OS 9 heritage, when copying text from Preview new lines are saved as carriage returns, i.e. \r.

This stupid behavior means that if you try to pbpaste something which contains line breaks in your terminal, you only get the last line. You can view the full output by replacing \r with \n:

pbpaste | tr '\r' '\n'

This lame thing had me waste some time, so I hope this short post makes your life easier.

P.S.: The full command I had my brother put in Keyboard Maestro is:

pbpaste | tr '\r' ' ' | pbcopy

Followed by a Paste action. This replaces line breaks with spaces and pastes the result.

Categories
Linux

What I learned about the current state of XBMC and Broadcom Crystal HD

In the last couple days I experimented a little with an old crappy laptop I had lying around for a while. I wanted to use it as a media center, but it was too old to support h264 hardware decoding, so playing 1080p content was basically impossible. So I bought a Broadcom BCM970012 Crystal HD hardware decoder for less than 20 euros on Amazon.

Broadcom Crystal HDThis little Mini PCI-E card handles the video decoding in hardware, and does so very efficiently, drawing almost no power. It became famous to enable 1080p on the 1st-gen Apple TV, which funnily enough has the same crappy GPU I have on my old laptop, an Nvidia GeForce Go 7300.

Broadcom is not supporting this card anymore, with the latest Linux driver available released in 2010. Anyway, I managed to get it working on XBMC 13.1 Gotham running on Debian Wheezy (7.0).

Getting the hardware to work

The first thing to note is that you have to run a 32 bit OS to be able to use this card reliably. I tried to use it on 64 bit installs of both Debian and Ubuntu, with no luck.

Second, support for Crystal HD will be dropped by Kodi (née XBMC) version 14.

Given this assumptions, I’ll briefly outline what was needed to get it to work.

First, I installed Debian. I opted for a net-install, in order to carefully choose only the packages I really needed. The laptop was quite old, so I didn’t want to bog it down with stuff I ultimately didn’t need. I went through the usual dance to install Nvidia’s proprietary drivers (version 304.x, since this is a legacy GPU), and installed alsa-utils in order to enable audio.

Debian doesn’t ship with the required crystalhd kernel module, so I had to compile my own. It was pretty easy (kernel 3.2.0-4-686-pae), I just followed the first part of this guide. Just in case it should disappear from the internet, I took the liberty to upload the source code here, and copy the required steps below:

  1. Install the required dependencies
    sudo apt-get install git automake g++ build-essential linux-headers
  2. Compile the driver
    cd crystalhd/driver/linux
    autoconf
    ./configure
    make
    sudo make install
  3. Compile the library
    cd ../../linux_lib/libcrystalhd/
    make
    sudo make install
  4. Load the driver
    sudo mod probe crystalhd

If all went as it should, you now have a working kernel module for Crystal HD cards.

In Debian Wheezy’s repos, you will find a really old version of XBMC (version 11). It will work, but I always prefer to have the latest stable version of everything. I dug around a bit, and found that in the official wheezy-backports repo there was a fairly recent Gotham release, 13.1RC1. I could have compiled a newer one, but it would have been more trouble than what it’s worth. So I went and added the repo to my sources.list, and installed the version from the repo.

echo "deb http://ftp.debian.org/debian/ wheezy-backports main contrib non-free" | sudo tee -a /etc/apt/sources.list
echo "deb-src http://ftp.debian.org/debian/ wheezy-backports main contrib non-free" | sudo tee -a /etc/apt/sources.list
sudo apt-get update
sudo apt-get -t wheezy-backports install xbmc

That’s it, Crystal HD decoding should now work without further actions.

The only thing was that it stopped working after waking from sleep. The solution was easy: save this script as /etc/pm/sleep.d/80crystalhd and make it executable with sudo chmod +x /etc/pm/sleep.d/80crystalhd.

#!/bin/bash
case $1 in
    hibernate)
        echo "Hey guy, we are going to suspend to disk!"
        modprobe -r crystalhd
        ;;
    suspend)
        echo "Oh, this time we're doing a suspend to RAM. Cool!"
        modprobe -r crystalhd
        ;;
    thaw)
        echo "oh, suspend to disk is over, we are resuming..."
        modprobe crystalhd
        ;;
    resume)
        echo "hey, the suspend to RAM seems to be over..."
        modprobe crystalhd
        ;;
    *)  echo "somebody is calling me totally wrong."
        ;;
esac

What this does, is it unloads the kernel module before suspending/hibernating and reloads it when the system wakes up. I didn’t think it would work even with XBMC still running, but it did.

Final touches

I then focused on cleaning up the experience of using XBMC:

  • It should launch automatically at boot time
  • It should be possibile to sleep/shutdown/restart from XBMC without needing the root password
  • The system should wake up on any USB activity

Autostart at boot

To launch XBMC at boot time you have to put a .xinitrc file in the home directory of XBMC’s user (from now on, I’ll assume that it’s xbmc, which makes sense).

#!/bin/bash
xbmc

Then, save this in /etc/init.d/xbmc and make it executable (sudo chmod +x /etc/init.d/xbmc)

#!/bin/sh
 
### BEGIN INIT INFO
# Provides:          xbmc
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts instance of XBMC
# Description:       starts instance of XBMC using start-stop-daemon and xinit
### END INIT INFO
 
############### EDIT ME ##################
 
# path to xinit exec
DAEMON=/usr/bin/startx
 
# startup args
#DAEMON_OPTS=" /usr/local/bin/xbmc --standalone -- :0"
 
# script name
NAME=xbmc
 
# app name
DESC=XBMC
 
# user
RUN_AS=xbmc
 
# Path of the PID file
PID_FILE=/var/run/xbmc.pid
 
############### END EDIT ME ##################
 
test -x $DAEMON || exit 0
 
set -e
 
case "$1" in
  start)
        echo "Starting $DESC"
        start-stop-daemon --start -c $RUN_AS --background --pidfile $PID_FILE  --make-pidfile --exec $DAEMON -- $DAEMON_OPTS
        ;;
  stop)
        echo "Stopping $DESC"
        start-stop-daemon --stop --pidfile $PID_FILE
        ;;
 
  restart|force-reload)
        echo "Restarting $DESC"
        start-stop-daemon --stop --pidfile $PID_FILE
        sleep 5
        start-stop-daemon --start -c $RUN_AS --background --pidfile $PID_FILE  --make-pidfile --exec $DAEMON -- $DAEMON_OPTS
        ;;
  *)
        N=/etc/init.d/$NAME
        echo "Usage: $N {start|stop|restart|force-reload}" >&2
        exit 1
        ;;
esac
 
exit 0

Then, make it start at boot:

sudo update-rc.d xbmc defaults

Allow shutdown/sleep/reboot from XBMC

To do so, you need a “policy” file. Save this to /etc/polkit-1/localauthority/50-local.d/custom-actions.pkla

[Actions for xbmc user]
Identity=unix-user:xbmc
Action=org.freedesktop.upower.*;org.freedesktop.consolekit.system.*;org.freedesk​top.udisks.*
ResultAny=yes
ResultInactive=yes
ResultActive=yes

Allow wake from any USB peripheral

Add this line to /etc/rc.global, just before exit 0, and make it executable (chmod +x /etc/rc.local)

echo enabled | tee  /sys/bus/usb/devices/*/power/wakeup

 Conclusion

I may have forgotten something while writing this post. What I definitely forgot is when I needed to reboot to apply this changes. I know it is a very Windows-y thing to do, but reboot if things don’t seem to work.