Patch webOS Random Wallpaper Switching

From WebOS Internals
Revision as of 19:06, 4 August 2009 by Hparsons (talk | contribs) (Added alternative uses section, and "missingpre" script)
Jump to navigation Jump to search


Homebrew App for Random Wallpaper Switching

There is now a homebrew application (no rooting required) that allows this same function without performing the patch below. It can be found at http://forums.precentral.net/homebrew-apps/195990-switcharoo-random-wallpaper-switching-v0-9-0-7-30-a.html

Goal

On my desktop I have installed desktop drapes and my wallpaper switches every few hours to a random image in a wallpapers folder. I wanted to have the same functionality on my pre.

Requirements

export PATH=$PATH:/bin:/sbin:/usr/bin:/usr/sbin/:/usr/local/bin:/opt/bin:/opt/sbin

Procedure

Note: I prefer doing everything with sudo and not logging in as root via the backdoor. I would recommend getting into the same habit.

Transfering Wallpapers

  • Connect your Pre to your computer as a USB drive.
  • Create a folder named 'wallpapers' on the Pre (if its not already there).
  • Put all your wallpapers there.
  • Disconnect the Pre from your PC. You might want to make sure to disconnect it safely so everything writes to storage okay.

Make sure to remove any wallpapers which you don't want to show up, though, since the switcher will pick a random one.

Enable Write Access

sudo mount -o remount,rw /

Enabling Cron

Go here to enable crond.

The Wallpaper Switcher Script

First lets make a place for the script:

sudo mkdir /opt/share/wallswitcher

Edit wallswitcher.sh inside /opt/share/wallswitcher directory and place the following code there:

#!/bin/sh

main()
{
  DIR='/media/internal/wallpapers/'
  IFS="
"
  NUM_FILES=0
  for cur in `ls -1 $DIR`; do
    if [ -f "$DIR$cur" ]
    then
      NUM_FILES=$((NUM_FILES+1))
      eval "FILE_$NUM_FILES=$cur"
    fi
  done

  RANDOM_NUMBER=`awk "BEGIN{srand();print int($NUM_FILES * rand()) + 1;}"`
  eval "RANDOM_FILE=\$FILE_$((RANDOM_NUMBER))"
  IMPORT_STRING="{ \"target\": \"$DIR$RANDOM_FILE\" }"
  SET_STRING="{ \"wallpaper\": { \"wallpaperName\": \"$RANDOM_FILE\", \"wallpaperFile\": \"$DIR$RANDOM_FILE\" } }"

  echo "Found $NUM_FILES in $DIR"
  echo "Using random file: $RANDOM_FILE"
  echo "JSON Import String: $IMPORT_STRING"
  echo "JSON Set String: $SET_STRING"
  
  eval "luna-send -n 1 palm://com.palm.systemservice/wallpaper/importWallpaper '$IMPORT_STRING'"
  
  eval "luna-send -n 1 palm://com.palm.systemservice/setPreferences '$SET_STRING'"
}

main

Save the file and make sure it has execute permissions. The following command will insure that:

sudo chmod 755 /opt/share/wallswitcher/wallswitcher.sh

Some notes on the script:

  • I am a Java/Groovy developer mostly and am not as good at shell scripting as I would like. Any improvements on the script itself are welcome
  • The main piece of code that performs the switch are the two luna-send commands. T he first command is required if a wallpaper has never been used before and will generate some thumbnails and copy the main image into /media/internal/.wallpapers. The second luna-send command actually performs the switch.
  • This method currently only supports wallpapers sized to exactly 320 x 480 px. The import luna-send call can take more parameters in its JSON string, such as scaling and x/y offsets from center. I have not used these yet since all my wallpapers are correctly sized.
  • Again, being a not-so-good shell developer, the way I obtain random numbers and use them is a bit wrong. Awk is used to get a random number. Instead of arrays (as in bash), dynamic variable naming is used.
  • The DIR variable controls where the script looks for wallpapers—you can change it to where your pictures are instead. Currently the scan is not recursive.

Testing the Script

If you have more then one wallpaper in /media/internal/wallpapers, you can test the switcher with the following command:

sudo /opt/share/wallswitcher/wallswitcher.sh

The response should look something like this:

Found 24 in /media/internal/wallpapers/
Using random file: wall_409.jpg
JSON Import String: { "target": "/media/internal/wallpapers/wall_409.jpg" }
JSON Set String: { "wallpaper": { "wallpaperName": "wall_409.jpg", "wallpaperFile": "/media/internal/wallpapers/wall_409.jpg" } }
** Message: serviceResponse Handling: 2, { "returnValue": true, "wallpaper": { "wallpaperName": "wall_409.jpg", "wallpaperFile": "\/media\/internal\/.wallpapers\/wall_409.jpg", "wallpaperThumbFile": "\/media\/internal\/.wallpapers\/thumbs\/wall_409.jpg" } }
** Message: serviceResponse Handling: 2, { "returnValue": true }

If the two service calls are not returning correctly, something is wrong. Contact me (fxdemolisher[at]gmail[dawt]com) if you are stuck debugging it.

Schedule the Script

This step is pretty easy. Since you have an empty root crontab set up all that needs to be done is a scheduling command. Edit /etc/cron/crontabs/root and place the following scheduling test line:

*/1 * * * * /opt/share/wallswitcher/wallswitcher.sh > /opt/share/wallswitcher/wallswitcher.log 2>&1

This will execute the switcher every minute and log to the /opt/share/wallswitcher/wallswitcher.log log. After you have it switching correctly every minute you can edit the cron definition to run as often as you like. Mine is set to run at the top of the hour using this line:

0 * * * * /opt/share/wallswitcher/wallswitcher.sh > /opt/share/wallswitcher/wallswitcher.log 2>&1

NOTE: Do not use crontab -e as this does not really write to /etc/cron/crontabs/root and will be overriden when the device restarts.

Restore Read-Only Filesystem

sudo mount -o remount,ro /


Tested and verified as written by optik678.

Alternate Uses

Modifications to original script for other uses

"Missing Pre" Wallpaper

This is a simple alternate use to create a wallpaper indicating the Pre is "missing" ("stolen" sounds rather harsh...)

1. Create a PNG file and put in a directory of your choosing ( <YOUR-DIRECTORY> ) naming it "missingpre.png"

2. Create a script file in a location of your choosing, naming it missingpre.sh (I use "/home/scripts") using the following script (be sure to change <YOUR-DIRECTORY> to the actual directory where you stored your missingpre.png file):

#!/bin/sh

main()
{
  MYFILE='<YOUR-DIRECTORY>/missingpre.png'

  IMPORT_STRING="{ \"target\": \"$MYFILE\" }"
  SET_STRING="{ \"wallpaper\": { \"wallpaperName\": \"mypre.png\", \"wallpaperFile\": \"$MYFILE\" } }"

  echo "JSON Import String: $IMPORT_STRING"
  echo "JSON Set String: $SET_STRING"
  
  eval "luna-send -n 1 palm://com.palm.systemservice/wallpaper/importWallpaper '$IMPORT_STRING'"
  eval "luna-send -n 1 palm://com.palm.systemservice/setPreferences '$SET_STRING'"
}

main

3. Add the following to /etc/cron/crontabs/root file (using the directory you chose for your script):

*/1 * * * * /home/scripts/missingpre.sh

4. Restart crond

/sbin/stop crond && /sbin/start crond

Result. Every minute this will over-ride whatever wallpaper was set. Obviously, this won't force anyone to return your device, nor will it work if someone knows what they're doing with the Pre, but if your wallpaper is obnoxious enough, you might get lucky and have them call you back.


Acknowledgements

I would like to thank this site and the wonderful hackers on it. I would not be able to do this without various examples from other pages.