Sunday, March 23, 2008

Adjusting wallpapers on dual monitor configuration easy


I have TFT monitor with 1280x1024 resolution and TV connected in nvidia twin view configuration with TV's resolution of 800x600. Whenever I want to set up a desktop wallpaper on Gnome, I must adjust it, because Gnome "sees" my desktop as one large 2080x1024 desktop. So, I must manually open wallpaper in Gimp and create another picture that contains two wallpaper next to each other, one with 1280x1024 dimension, and another one, with 800x600 dimensions right of the first one. So, I decided that that's enough. So I utilized imagemagick to accomplish this and put convinient function in my .bashrc file. Here it is:
make_wallpaper(){
    convert -background white "$1" -resize 1280x1024 \( "$1" -resize 800x600 \) +append "$2";
}

Of course, background color isn't neccesary since it is not visible, I just put it as a reminder. All I have to do now is to call:
make_wallpaper original.png my.png

Sweet, ain't it;)

Saturday, March 15, 2008

JspWiki export from OpenOffice 2.3

If you have Open Office 2.3, you probably noticed that you can export your document to Media Wiki format (with File->Export). I needed JspWiki format, and it turned out, they are fairly similar, so I changed corresponding XSLT file, and present you here way to add JspWiki export. It will work on both Linux and Windows, and it will only work on Open Office version >= 2.3.

First download this zip. This is zip file that should be extracted to your $INSTALL_DIR/share directory, so it should end up probably in:
C:\Program Files\Open Office\share\

if you are using Windows, or, if you are using Linux, it should be:
/usr/lib/openoffice/share


Of course, if this directory doesn't exist on your system, find out where Open Office 2.3 is installed. This will add XSLT files, so Open Office knows how to export to JspWiki, and it will overwrite two files, so this export can be registered in Open Office (so that you will be able to go to File->Export and select JspWiki export from drop down list).

Advanced


How this works? Media Wiki and JspWiki seems to be very different, but they are pretty much the same, except for different markup characters. If you diff against jspwiki/odt2jspwiki.xsl and wiki/odt2jspwiki.xsl, you will find that they differ by only few lines that needs to be modified in order to have JspWiki export that does as much as Media Wiki export. I started by basically copying Media Wiki .xslt and modifying it. This is also the way you should start if you want to add support to Open Office so it can export to wiki format you need. Have fun!

Saturday, March 08, 2008

Changing power button functionality

How often do you use power button to shut down your computer? I never use it, and, even if I would have need to shutdown or restart my computer, I would use GUI way to do it (simply because, it is something that is rarely used, I never remember I could use power button in that split second when I want to shut it down)

But, if you (like me) don't have keyboards with fancy shortcut buttons, why not use that same power button for some shortcut. Or, you can make fun of your Windows friends just by showing what power and mind-boggling functionality Linux have and just waits for yours ideas to unleash it. I think this is great example of that.

I will use Ubuntu to set it up, but, with minor (or even none) modification, this should work on any distro. First you will need, is to remove annoying gnome log-off screen that shows up when you press power button. For this, we will need to change two keys in gconf. Start gconf-editor and change those two keys:
  • /apps/gnome-power-manager/action_button_power
  • /apps/gnome-power-manager/buttons/power
to "nothing" (without quotes). This will disable log-off screen. Now, it's all about editing /etc/acpi/powerbtn.sh to suit your needs. I will give you two possible scenarios, and there are no limits what you can do.

Enable/disable internet (forwarding)

I already had two little script to enable or disable internet to some users I am routing internet to. These are some iptables stuff (something like `iptables -I FORWARD -p ALL -s 192.168.0.122 -j DROP`) and I had to open console, execute it and later enable it. This can be done with this in /etc/acpi/powerbtn.sh:
#! /bin/sh
if [ -f /var/tmp/internet_off ]; then
    /etc/rc.start_internet
    rm /var/tmp/internet_off
else
    /etc/rc.stop_internet
    touch /var/tmp/internet_off
fi
This is stupid script. It checks for existence of file /var/tmp/internet_off that signalizes if forwarding is on. It then executes appropriate scripts. If you have someone dumb enough, you can tell them that they have no internet because your computer is turned off, and what they see on monitor is just a picture, or drop them a story about virtual (wau!) machine that run even when computer is off (everyone heard of vmware) - something like that:) You can even confirm this by pressing button in front of them to enable or disable their internet access.

Starting firefox


I use firefox all day. If I ever want to dedicate any button to any program, that is certainly firefox. But to start GUI program in shell script that is run as root, we need some workarounds. For example, you need to set DISPLAY and XAUTHORITY environment variable.
#! /bin/sh
export DISPLAY=:0
export XAUTHORITY=/home/yourusername/.Xauthority
sudo -u yourusername firefox
But, this script have few defects. If firefox is running, it will launch another windows, and we see 'yourusername' is all around. This is improved version:
#! /bin/sh
USERNAME=yourusername
export DISPLAY=:0
export XAUTHORITY=/home/$USERNAME/.Xauthority

if ps -U $USERNAME -u $USERNAME|grep firefox; then
    sudo -u $USERNAME firefox -new-tab about:blank
else
    sudo -u $USERNAME firefox
fi
If firefox is running, it will just open new empty tab, otherwise it will launch it normally. Also, your username is pulled up as variable, so you just need to change second line to your username and this script will work. Have fun!

Further ideas

Only one function for button is nothing. Maybe we can split two different functionality by defining click and double-click on power button. Simple creating file on first click, and checking if file was created 2 seconds before current time (I will not go into details here). Also, you could use that button to open CD tray, why not:) Like anything in Linux, your creativity is your only limit. Do you have any ideas what to do else with this button? What would you put in /etc/acpi/powerbtn.sh?