Local

..:: Home
..:: Legal
..:: Contact
..:: About
..:: RSS
..:: Log in

Links

..:: Fake Bill Gates
..:: Tap the Hive

A Random Quote

"Here's to the crazy ones. The misfits. The rebels. The trouble-makers. The round heads in the square holes. The ones who see things differently. They're not fond of rules, and they have no respect for the status-quo. You can quote them, disagree with them, glorify, or vilify them. But the only thing you can't do is ignore them. Because they change things. They push the human race forward. And while some may see them as the crazy ones, we see genius. Because the people who are crazy enough to think they can change the world, are the ones who do." -- Jack Kerouac

Archive for the ‘Code’ Category

Bash Script to Randomly Change Gnome Background

Sunday, January 14th, 2007

This code uses a backgrounds folder, set as $bg_path at the beginning of the script. Just drop it in your cron.hourly folder and let it run. It will change your background on all desktops every hour (or whatever you set in cron). The script supports PNG, JPG, and GIF images, and you can add more by just changing the extensions variable at the top.

The Script

#!/bin/bash
# script: change_backgrounds.sh - bash version
# version 2007.3.7
# description: randomly replace gnome background with one from a directory
# credits: David Loschiavo [http://www.djlosch.com], Steven Van Ingelgem
# license: GPL

bg_path=/mnt/archive/media/pics/backgrounds
extensions=”jpg png gif jpeg JPG GIF PNG”
temp_bg_list=/tmp/bg_change_list

rm -f $temp_bg_list

for extension in $extensions
do
	find $bg_path -iregex “.*.$extension” >> “$temp_bg_list”
done

cnt=`wc -l “$temp_bg_list” | cut -f1 -d” “`
all_bgs=`echo `expr $RANDOM % $cnt + 1“

selected_bg=`head -n$all_bgs “$temp_bg_list” | tail -n1`

logger “Changed desktop to: $selected_bg”

gconftool-2 -t string -s /desktop/gnome/background/picture_filename “$selected_bg”
exit 0

The previous version (2007.1.14) did not account for spaces in filenames. Steven Van Ingelgem made the necessary changes, and I have put them up here. Thanks Steven!

..:: Installation

There are multiple ways to set this to run. The first is to just drop it in your cron.hourly folder and set the permissions.

sudo wget http://www.djlosch.com/scripts/change-background.sh -O /etc/cron.hourly/change-backgrounds.sh

Then change the permissions to allow it to run.

sudo chmod 700 /etc/cron.hourly/change-background.sh

Now edit the file and set bg_path to wherever your backgrounds are stored. DO NOT leave any spaces outside the double quotes.

Acceptable: bg_path=”/home/your_username/backgrounds”
Not Acceptable: bg_path = “/home/your_username/backgrounds”

Finally, to make sure the backgrounds don’t appear funny, right click on the desktop and go to “Change Background”. Then make these setting changes:

Style: Scaled
Desktop Colors: Solid Color
{Color Palette->Color Name}: #333333

The rest of the settings you can leave unchanged. You can customize the <Color Name> to something other than a gun metal color, but that’s what I prefer.

If you want the background to change at a different interval, check one of the many crontab tutorials out on the web.

How To: Get Your Blog Into Social Bookmarking Sites

Sunday, December 24th, 2006

If you blog, either you’ve at least heard of the social bookmarking sites, or you live under a rock. This is in reference particularly to digg, reddit, del.icio.us and stumbleupon. I decided to add these as I’m a regular reader of digg, and I have been receiving a few hits here and there from del.icio.us and stumbleupon directly from user submissions, and some of my articles have popped up in digg comments. The question is how to set up your articles to make it easier for visitors to submit your them to the social bookmarking sites. Make sure that you change the <$var$> for the title and permalink to whatever corresponds to your site’s CMS/scripting. Then just add corresponding text or an icon.

..:: StumbleUpon

StumbleUpon is where I got my first social bookmarking referrals. A few people apparently like my Linux Tutorial, particularly for the PCM volume control script.
Link Syntax:

http://www.stumbleupon.com/submit?url=<$BlogItemPermalinkURL$>&title=<$BlogItemTitle$>

Icon:
You can also use StumbeUpon’s official icons if they interest you.

..:: Digg

More information regarding the URL specifications and the digg icon pack are available directly from Kevin Rose, the creator of digg.
Icon:
Link Syntax:

http://digg.com/submit?phase=2&url=<$BlogItemPermalinkURL$>&title=<$BlogItemTitle$>

..:: Del.icio.us

Link Syntax:

http://del.icio.us/post?v=4&noui&jump=close&url=<$BlogItemPermalinkURL$>&title=<$BlogItemTitle$>

Icon:

..:: Reddit

Reddit engages in a democratic voting system much like Digg as there is a democratic voting concept, but there is also a “karma” system to help cut down on gaming.
Link Syntax:

http://reddit.com/submit?url=<$BlogItemPermalinkUrl$>&title=<$BlogItemTitle$>

Icon:

..:: Fark

Fark tends to be about news of various sorts, rather than informative/explanatory articles. As such, it may not be suited for your site.
Link Syntax:

http://cgi.fark.com/cgi/fark/edit.pl?new_url=<$BlogItemPermalinkURL$>&new_comment=<$BlogItemTitle$>

Icon:

..:: Google Bookmarks

Google Bookmarks doesn’t seem to be as social as the other sites, but it seems useful nonetheless.
Link Syntax:

http://www.google.com/bookmarks/mark?op=edit&output=popup&bkmk=<$BlogItemPermalinkURL$>&title=<$BlogItemTitle$>

For the Google icon, I’m just using the regular Google G.
Icon:

..:: More Syntax Links

There are other guides for social bookmarking links, but that one lacks stumbleupon. The generator it links to appears to be a little buggy (at the time of this posting), but may get better by the time you read this.

C++ MySQL Database Driver: class.mysql.cpp

Wednesday, December 13th, 2006

I started coding (substantially) in C++ yesterday (outside of regular class assignments), and as I looked through a lot of the database driver classes, I couldn’t find any that could compare to the ease of use of current php based database drivers. I coded in C in undergrad in my OS class, but dealing with 30 year old minix code is worthless for the purpose of learning C. Comparatively, OO is incredibly new, and this ease of usability is incomparable to the jumbled mess in minix. This is tested and working. I know that it’s not simple, and a lot of very experienced C++ coders will probably balk at some of this, but after php, I’m spoiled.

#include </usr/include/mysql/mysql.h>
#include <iostream>
using namespace std;
#include <time.h>

class Database
{

private:

        char *host;
        char *database;
        char *username;
        char *password;
        MYSQL *link;

public:

        Database(char *const new_host, char *const new_database, char *const new_username, char *const new_password)
        {
                host = new_host;
                database = new_database;
                username = new_username;
                password = new_password;
                link = mysql_init(NULL);
                link = mysql_real_connect(link, host, username, password, database, 0, NULL, 0);
        }

        void print()
        {
                cout << "Host: " << host << "\n";
                cout << "Database: " << database << "\n";
                cout << "Username: " << username << "\n";
                cout << "Password: " << password << "\n";
        }

        MYSQL_RES query(char *const query)
        {
                MYSQL_RES *res_set;
                cout << "Executing query: " << query << "\n";
                mysql_query(link, query);
                res_set = mysql_store_result(link);
                return *res_set;
        }

        int num_rows(MYSQL_RES *res_set)
        {
                int i =  mysql_num_rows(res_set);
                return i;
        }

        MYSQL_ROW fetch_row(MYSQL_RES *res_set)
        {
                MYSQL_ROW row = mysql_fetch_row(res_set);
                return row;
        }

        void close()
        {
                mysql_close(link);
        }
}; 

int main()
{
        Database db = Database("localhost", "test", "username", "password");
        db.print();
        char *query = "select * from test_table";
        MYSQL_RES result = db.query(query);
        MYSQL_ROW row;
        cout << "row: id | value | timestamp\n";
        while ((row = db.fetch_row(&result)) != NULL)
        {
                cout << "row: " << row[0] << ” | ” << row[1] << ” | ” << row[2] << “\n”;
        }
        db.close();

}

How To: Ubuntu Edgy and Mythtv

Tuesday, December 12th, 2006

The purpose of this install guide is to install MythTV on Ubuntu Edgy, using a Hauppauge PVR-150. Some linked docs here may help you, but this guide is to achieve that purpose with minimal work. Most of the other Edgy tutorials leave off a significant amount of information, and are specifically only usable with cards compatible with DVB (for the nova-T line of cards), but the Hauppauge PVR-xxx line is not compatible at the moment. This is not for the Feint of Mind ™, so your grandma will probably have some difficulty in this. You should have a medium amount of Linux experience before trying this, but do not fret. Excluding Edgy’s install time, I can run through this entire install in about 20 minutes.

PLEASE, READ everything in a section when doing it. SKIMMING will often leave you wondering what is going on, and you’ll learn nothing (which is neither the goal of this guide, nor open source). I also get a bunch of emails from people who missed a key detail simply because they didn’t read. And definitely, COPY and PASTE text from your browser window to your terminal, or you will get typos.

..:: Background

MythTV is Tivo and Windows Media Center on steroids. The most notable features include

  • a TV recorder
  • an optional web based recording manager (that is awesome)
  • automatic commercial skipping for recorded TV
  • no playback restrictions (WinMCE disables playback for what content creators deem is “premium” content after 3 days)
  • no monthly fees
  • allows easy expansion (just add network storage or another internal hard drive)
  • a music manager
  • a DVD player
  • a video file player (that plays every non-DRM’d codec you’ve ever seen)

I love MythTV. Once you have a DVR, regular television is unwatchable. Annoying commercials (that raise the volume 30-50%) are no longer an issue. Rewinding, pausing, and fast forwarding regular TV is spectacular.

..:: My Experiences

This is a little bit of my experience with Ubuntu and MythTV. If you don’t care about this (and most of you shouldn’t as it’s merely my professional opinion on the version changes), just skip to the next section. Basically, I started using Mythtv .18 on Ubuntu 5.10. The only cards I have any experience with are the PVR-150 and the PVR-500. I set the 150 up for myself and the 500 for my older brother. They’re nearly the same install method. I used the hyams MythTV method for Breezy, but when Dapper (6.06) came out, I tried that out, and there were issues with Ubuntu using an upgraded version of MySQL. I knew that it would take less time for me to reinstall on Breezy than it would to fix the Dapper issues. So I went back to Breezy. After a bit, I got tired of playing World of Warcraft on my laptop (5 fps or less is horrendous) so I took down my MythTV box to use it as a WoW box. I’m now ditching WoW to pull up MythTV (that’s my only acceptably fast computer). I downloaded Ubuntu Edgy (6.10), and the MythTV install is amazingly easier than it used to be. I don’t know whether I’m just so familiar with the install, or that it’s just gotten to be so easy. Anyways, here goes.

..:: Install MythTV Backend and Frontend

Get Ubuntu 6.10 and then install it. You can use the default installation parameters if you want. I typically do. Once up and running in the installed version (not the live version), open a terminal and type

sudo su

Enter your password (and hit enter). This will basically log you in as root without permanently destroying your Ubuntu sudo model. Backup your /etc/apt/sources.list:

sudo cp /etc/apt/sources.list /etc/apt/sources.list.default

Then modify your /etc/apt/sources.list file to include these:

deb http://archive.ubuntu.com/ubuntu/ edgy main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ edgy-updates main restricted universe multiverse
deb http://security.ubuntu.com/ubuntu/ edgy-security main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ edgy-backports main restricted universe multiverse

Then type:

apt-get update
apt-get upgrade

After that finishes, type

apt-get install mplayer phpmyadmin apache2 php5 mysql-server libapache2-mod-php5 libapache2-mod-auth-mysql php5-mysql gstreamer0.10-ffmpeg gstreamer0.10-gl gstreamer0.10-plugins-base gstreamer0.10-plugins-good gstreamer0.10-plugins-bad gstreamer0.10-plugins-bad-multiverse gstreamer0.10-plugins-ugly gstreamer0.10-plugins-ugly-multiverse libxine-extracodecs

Once apache, php, mysql, and phpmyadmin have finished installing, go to http://localhost/phpmyadmin and set a root password for MySQL. Do NOT use tilda (a special terminal app) for this next step, because folding up really borks this next installation. The myth installer uses a console GUI to request a few things, and folded tilda doesn’t handle it well (you’ll have to dpkg-reconfigure a few apps if you do). If you don’t know what tilda is, you’re not using it, so don’t worry about it.

The next section is installing mythtv itself. You can’t combine this and the previous line, because you have to have mysql-server installed before mythtv installs. Now, type

apt-get install mythtv-frontend mythtv-backend mythtv-database mythvideo mythweather mythweb mythmusic mythplugins

During this install, MythTV will ask for your MySQL root password. You’ll also need to run the following line, but it often encounters errors because of server issues so keep trying until it succeeds:

apt-get install msttcorefonts

During the install, go to zap2it and register for an account to get free TV listings. For the Certificate Code, use the MythTV project code: ZIYN-DQZO-SBUT. Take note of your account username and password, because you’ll have to put it into MythTV in a few minutes.

..:: Install IVTV module

The next step is going to be to install the IVTV module used for the Hauppauge PVR-xxx video capture cards . I use the PVR-150, but the 500 works well too. If you’re not using this series, check here for your hardware. If you’re using the PVR-xxx series, continue with my instructions. Type:

apt-get install ivtv-source devscripts ivtv-utils

Get, build, and install the IVTV driver:

cd
wget ftp://ftp.shspvr.com/download/wintv-pvr_250-350/inf/pvr_1.18.21.22254_inf.zip
export DEBFULLNAME="Mario Limonciello"
export DEBEMAIL="superm1@ubuntu.com"
ivtv-make-fwpkg pvr_1.18.21.22254_inf.zip
dpkg -i ivtv*firmware*deb

Get the PVR-150 and 500 firmware addition:

wget http://home.eng.iastate.edu/~superm1/contrib/firmware/v4l-cx25840.fw -O /lib/firmware/v4l-cx25840.fw

The next few instructions must be run one at a time:

m-a update,prepare
m-a a-i ivtv
depmod -a
modprobe ivtv

If there are no errors, then you’re good so far, and you should test IVTV by typing:

mplayer /dev/video0

If you get video, that’s fantastic. Otherwise, I don’t know what you did wrong, because this worked the first time for me.

..:: Startup: Set MythTV as the default user to login

edit /etc/gdm/gdm.conf. find these two lines:

AutomaticLoginEnable=false
AutomaticLogin=

and change them to

AutomaticLoginEnable=true
AutomaticLogin=mythtv

..:: Set Up Your Card and Get Your First Set of Listings

All that is really remaining is to configure your capture card, but you should do this as the MythTV user. The MythTV install will create the user, but you will need to set the MythTV user password:

passwd mythtv

Then log out, and when you log back in as MythTV, type:

mythtv-setup

I’m assuming you’re using an NTSC television (in the US), so you can ignore the first bubble. Go to “2. Capture Cards” and select “New Capture Card”. Enter the following settings:

Card type: MPEG-2 Encoder card (PVR-250, PVR 350)
Video device: /dev/video0
Default input: Tuner0

Then go to “3. Video Sources” and select “New Video Source”. Enter the following settings:

Video source name: PVR-150-1
XMLTV listings grabber: NorthAmerica (DataDirect)
User ID: your_zap2it_username
Password: your_zap2it_password

Hit “Retrieve Line-ups” and wait a second. Exit that section and then select “4. Inputs”, and enter this setting:

Video Source: PVR-150-1

Finally, fill your database full of listings:

mythfilldatabase

..:: Startup: Set mythbackend and mythfrontend to run on boot

The default init.d startup script is broken (or at least was for me on both breezy and edgy), so I’m currently using hyam’s backend script. I give full credit for this script to hyam. You can get this script by typing:

wget http://s91928265.onlinehome.us/hfamily/mythtv/mythtv-backend -O /etc/init.d/mythtv-backend

Then restart the myth-backend daemon.

/etc/init.d/mythtv-backend start

On the gnome start menu, go to System->Preferences->Sessions and on the Startup Programs tab, add “mythfrontend” without the quotes. Finally, run the client, and explore the menus:
mythfrontend

..:: Disable the Screensaver

Since you don’t want the screensaver popping up while you’re watching a movie, go to System->Preferences->Screensaver, and disable the screen saver. In the “Advanced” tab, turn off power management.

..:: Option: MythCenter Theme

I personally like the MythCenter theme. To get it type:

wget http://mythtv.fotoniq.nl/files/MythCenter.tar.gz
tar -xzvf MythCenter.tar.gz --directory=/usr/share/mythtv/themes/

It should appear in the Appearances configuration menu when you’re running mythfrontend.

..:: Option: Enable Your Remote

Some Hauppauge packages come with the grey remote. Mine did, and installation is incredibly simple, but I have not made any customizations to it. I actually use my universal remote, and had it just memorize the hauppauge functions. To install the daemon, check the MythTV LIRC on Ubuntu Edgy How-to.

..:: Option: MPD to Control Music

I personally dislike the mythmusic frontend. I think using a remote for playlist manipulation (instead of a mouse) is silly, and way too time consuming. Also, I almost always have my laptop around, so the MPD project seems perfect for the job. Check my MPD MythTV How-To on how to get it installed in Ubuntu Edgy.

..:: Troubleshooting: Static Past Channel X

Many people have reported this error, and I’ve even received it myself. Basically, I’ll be able to view up through channel 13 or 14, and then everything else after is just static. The solution is to go back into myth-setup and make sure that the tuner is set to “us-cable”. You may or may not have to deal with this depending on your cable provider and how Zap2it handles your listing.

..:: Troubleshooting: Changing Channels Doesn’t Actually Change the Channel

The symptom of this is that regardless of what channel you select (using up, down, or typing in a number), the video source doesn’t actually change, but the OSD will. Re-run mythtv-setup as root and delete your cards/tuners. Then exit, and re-run mythtv-setup and add your tuner/card(s) as the mythtv user.

..:: Credits

https://wiki.ubuntu.com/Install_IVTV_Edgy
https://help.ubuntu.com/community/MythTV_Edgy_Backend_Frontend

How To: MPD, Music Player Daemon

Wednesday, November 22nd, 2006

Since MythTV’s music player has an incredibly embarassing interface, and I still like to use my stereo system for my music, I clearly had a problem. The solution is MPD: Music Player Daemon, as suggested by Kevin72594, from Gentoo OTW. MPD is a service that accepts various clients and plays music files on the host computer. This is NOT a program to stream media to the desktop which you’re using. It is designed to control a central server. The particular client I’m interested in was a web based client. This way, anyone on my network could control my music with their laptop.

..:: Software Summary

Title: MPD (Music Player Daemon) and MPC (Music Player Client)
Type: media service daemon
Purpose: control central audio server
Tested OS: Ubuntu 6.10
Screenshots: via [ MPD clients | djlosch UI ]

..:: Installation

Here’s how to install in Ubuntu Edgy. First, make sure you have proper ubuntu repositories (I think only universe and multiverse are needed for this).

sudo apt-get install mpd mpc

Then edit /etc/mpd.conf. You’ll need to uncomment one of the audio output sections. I use the OSS output with the default settings.

audio_output {
  type    "oss"
  name    "my OSS sound card"
  device  "/dev/dsp"         # optional
  format  "44100:16:2"       # optional
}

Also set the playlist and music directory at the top. I pointed the music link at my monster drive that has all of my music on it, and then I save my playlists in my home directory.

..:: Install the Web Client

The web client will allow you to visit your box through any web browser and control your box. You will need apache and php already installed, so get them if you don’t have them yet.

sudo apt-get install apache2 php5 libapache2-mod-php5

Then install the mp2 web client.

sudo wget http://mercury.chem.pitt.edu/~shank/phpMp2-0.11.0.tar.bz2 -O /var/www/phpMp2-0.11.0.tar.bz2

cd /var/www

sudo tar --use-compress-program bzip2 -xvf phpMp2-0.11.0.tar.bz2

sudo chmod 755 phpMp2 -R

..:: Customize the Client

Here’s the stock screenshot from the mp2 client page.

Open up your web browser and navigate to the box running MPD (localhost if you’re on the server right now, or most likely some address on your network like http://192.168.1.200). Once there, click the Update button in the top right to scan for your music. Changing the settings through the Options tab doesn’t seem to save them permanently — only for the current session. I’m about to hit you with a bunch of changes, so you may just want to get my copy of the config file, but read on for an explanation of changes.

sudo wget http://www.djlosch.com/source/phpMp2_config.phptxt -O /var/www/phpMp2/config.php

I personally prefer size 8 font and the ember theme with the classic layout. To make these changes permanent, open the file /var/www/phpMp2/config.php. Go through and change the font_size to 8, layout to classic, and style to ember. I also vigorously maintain my mp3’s ID3 tags, but I don’t ever bother with the track number or album tags, so change the song_display_format, the column_files, and the sort variables to remove the Track and Album tags.

With these changes, this is what my UI looks like:

..:: Amazing Remote

If you have a cell phone, tablet, or PDA with wifi, you can control your music server from ANYWHERE that you get a wireless signal. This is awesome because then you don’t have to worry about bluetooth applications with piss poor interfaces or transfer methods.

..:: Streaming and Icecast2

MPD only plays music on the served box. You can also use MPD to serve files up to Icecast2, the internet radio streamer, but that’s another how-to.