Wednesday, May 17, 2006

Beginning with Java on Linux

It seems that you can't go anywhere on the web without running into some form of Java, this is why I am now going to try to explain not only what Java is, but give some examples of programs that you can make, modify and learn from.
What is Java?
Java was originally developed by Sun Microsystems in an attempt to create an architecturally neutral programming language that would not have to be complied for various CPU architectures. Oak (as it was originally called, although the name was changed in 1995) was developed in 1991 for such things as home appliances which would not all run on the same type of processors. Just then the web was taking off and it was obvious that an programming language that could be used for many different operating systems and CPU architectures without compling many times would be of great importance. The final solution was to use bytecode. Unlike C++, Java code is not executable, it is code that is run by a Java Virtual Machine (JVM), so once a JVM is introduced for a platform all the Java programs can be run on it. There are two types of Java programs, the applications and the applets. The applications are what are written on a computer and run on a computer without the Internet connected in anyway. An applet is a program made for use on the internet and is the programs that runs in your browser. Sun also gave Java some buzzwords.
Simple
You might get some arguments from beginners on this, but Java remains a fairly simple language.
Secure
If you ever try to save from a notepad program (or any program) in Java you will get something saying
Quote:
This application has requested read/write access to a file on the local filesystem. Allowing this action will only give the application access to the file(s) selected in the following file dialog box. Do you want to allow this action?
The Java code runs within the JVM and prompts you if the bytecode wants to read or write.
Portable
Since it is architecturally neutral it can run on PCs, Macs, PDAs, Cellphones, and about anything else if there is a JVM for it.
Object-Oriented
While some languages are based around commands, Object-Oriented programming focuses on that data. For a more complete definition I highly recommend going to Google Glossary to learn more.
Robust
Powerful. This is in part due to the fact that the Java complier will check the code and will not complie it if has errors.
Multithreaded[b/]
Java has built-in support for multi-threaded programming.
[b]Architecture-neutral
Java is not made for a specific architecture or operating system.
Interpreted
Thanks to bytecode Java can be used on many different platforms.
High Performace
Java isn't going to be used for 1st person shooters but it does run fast.
Distributed
It can be used on many platforms
Dynamic
Can evolve to changing needs.

How Java is like C/C++
A Java programmer would be able to learn C/C++ quickly and a C/C++ programmer would be able to learn Java quickly because they are similar. When Java was made it was not to be a programming language that was better then C/C++ but was made to meet the goals of the interenet age. Java also has differences with C/C++, for example, someone could not write C/C++ code and complie it as Java for Internet use, nor could someone take Java code and complie it into C/C++.
Getting started writing Java
First you must go and get Java. You can download the JRE, which is the Java Runtime Environment, this is good for using Java but not what we need to compile Java applications. You need to download the SDK, which is the Software Development Kit. Once you have installed this free download you will have two important tools. The first is the javac command which is for compiling the program, and there is the java command for running your program. Once the SDK is installed you try typing javac, if you get an unrecognized error you should put the line
PATH=$PATH:/usr/java/j2sdk1.4.2/bin (or replace /usr/java/j2sdk1.4.2/bin[/i] in whatever is the place to javac (this can be found with locate javac)in your /etc/profile file. This way the commands are accessible from anywhere. For writing the programs, most text editors will work (not word processors though, they format the text) but I prefer Kwrite because after you save it as a java file it colors all the text and makes blocks of code collaspable and expandable. First we are going to do an analysis of a simple program.

/*
This is a simple, simple app.
They will get more fun in time
:)
*/
class First {
public static void main(String args[]) {
System.out.println("Yea! I wrote JAVA");
}
}

Starting at the top you will see the /* and */ markings. This is for a multi-line comment, anything inside of here will be ignored by the Java compiler. You can also add singal line comments with the // markings with everything after the // as a comment.
class is the part of the program that everything is inside of.
First is the title of the program, you have to save it as whatever you have after class, and this case-sensitive.
public is specifying main(String args[]) as being accessable to code outside of its class.
static allows main(String args[]) to be used before any objects have been created.
void Saying that main(String args[]) itself doesn't give output
main(String args[]) { is a method, this is where the code starts executing, you don't need the Sting args for this program but you will need it later so get used to typing it. :)
System.out.println is simply telling the system to print and the ln is telling it to make a new line afterwards. You could also just put print instead of println. Everything in parentheses is where you can type messages.
} The first one is closing the public static void main() { line and the second is closing the class First {.
Once you have this done this, save your file, but make sure to save it as First.java. Next, get a command prompt and go into the folder where you saved your Java file and type
javac First.java
Nothing fancy should happen. If something does, just copy and paste the program off of this document and it should compile fine. Nearly all of my errors with Java are typos that the compiler will let me know about. After this, you should have a file called First.class. Make sure you are in the same directory as First.class and type
java First
and you should see
Yea! I wrote JAVA.
You do not need to include .class when you are running the program.

Next, we get started with variables. Variables can be any sort of things that you assign a value to.

class var {
public static void main(String args[]) {
int v;
v = 5;
System.out.println("v is " + v);
}
}

The output should be v is 5
Since I have already explained most of the things in the previous program I will explain what the new things do.
int v; This is declaring that there will be an integer variable. You must declare a variable before you use it. This variable is call v. The names can be longer then one character and are case sensitive.
v = 5; v is now being assigned the value 5.
System.out.println("v is " + v); Like before, the System.out.println command is being used, everything inside of quotes is what you type. To add the value of v just a the + v outside of the quotes.
Once you have complied the program and ran it you should get.
v is 5
You can also do math with Java programs, like in the next example.

class math {
public static void main(String args[]) {
int a;
int b;
int c;
a = 5;
b = 9;
c = a * b;
System.out.println( a + " times " + b + " is " + c);
}
}

The output will be 5 times 9 is 45
Along with *, you can also use the +, -, and / signs for math. You can also do things like b = b * a where what the variable equals includes itself. The next program demonstrates a loop.

class loop {
public static void main(String args[]) {
double gallons, cups;
for(gallons = 1; gallons <=10; gallons++) {
cups = gallons * 16;
System.out.println(gallons + " gallons is " + cups + " cups.");
}
}
}

The output will be

1.0 gallons is 16.0 cups.
2.0 gallons is 32.0 cups.
3.0 gallons is 48.0 cups.
4.0 gallons is 64.0 cups.
5.0 gallons is 80.0 cups.
6.0 gallons is 96.0 cups.
7.0 gallons is 112.0 cups.
8.0 gallons is 128.0 cups.
9.0 gallons is 144.0 cups.
10.0 gallons is 160.0 cups.

The first thing different about this program is double instead of int. Int declares an integer, these work for a lot of things but loose precision if you were to divide 9 by 2, or dealing with anything that has a decimal. For things with decimals you can use float or double. There are also different types of integers other then int. Int is 32 bits, so it covers from 2,147,483,647 to -2,147,483,648. As its name suggests, long is a very long integer, 64 bit, it can handle numbers slightly over 9,200,000,000,000,000,000 and slightly under the negative. For the smaller numbers you might want to look into short (16 bit, 32,867 through -32,768) and byte(8 bit, 127 through -128). And for characters, you use char.
Getting back on track, the next thing you will notice it the two variables being declared are separated by a comma. This saves time, I can write
double a, b, c, d;
instead of writing out
double a;
double b;
double c;
double d;
The line with for is the loop itself. The basic form of for is for(starting; restrictions; count by) statement;
The gallons = 1; is saying we want the loop starting at 1. You could start it at 57 or -23 if you wanted. gallons <= 10; is saying count everything less then or equal to 10. Here are some important things that will come in handy many times
== equal to
!= not equal to
< less than
> greater than
<= less than or equal to
>= greater than or equal to
And gallons++ is the same as writing out count = count+1 If you want to count by 2s use count = count+2 or 3s use count = count+3 and so on. The { starts a new block of code, inside we assign cups the value and what to display when the loop is complete.
This next program will use the if statement.

class ifif {
public static void main(String args[]) {
double a, b;
a = 5
b = 4
if(a == b) System.out.println("Since 4 will never equal 5 this won't be displayed, if it does, buy a new CPU");
if(a != b) System.out.println("Since 4 isn't equal to 5 this will be displayed");
if(a < b) System.out.println("5 isn't less then 4, this will not be seen");
if(a > b) System.out.println("I think you get it by now");
}
}

If statements are very useful in all types of situations. The if statement can also be used as a block of code, for example

if(5 == 5) {
double e;
e = 5;
System.out.println("e is " + e);
}
This may not seem like a very useful tool, but in time it will become very important. Say for example, you are writing a temperature conversion program. You want to prompt the user "Press A to convert Fahrenheit to Celsius or B to convert Celsius to Fahrenheit" You would have something like
if(input == A) {
Here is the program to convert Fahrenheit to Celsius
}
if(input == B {
Here is the program to convert Celsius to Fahrenheit
}
This way only the code needed is executed. Of course, you won't actually use [i]input, that is just easy to understand for now.
Here is a program that uses user input to find weight on the moon.

import java.io.*;
class moon {
public static void main(String args[])
throws java.io.IOException {
double e;
double m;
System.out.println("Please enter your weight to get the moon equivalent.");
String strA = new BufferedReader(new InputStreamReader(System.in)).readLine();
e = Double.parseDouble(strA);
m = e * .17;
System.out.println("Your weight on the moon would be " + m + " pounds");
}
}

This one is more complex. import java.io.*; is bringing in things needed for input. The throws java.io.IOException is for error handling. String strA = new BufferedReader(new InputStreamReader(System.in)).readLine(); is going to get the input and the next line is going to assign e the input. From there it is easy. So knowing most of this you can create simple, but useful applications like this.

import java.io.*;
public class triangle {
public static void main(String args[]) throws java.io.IOException {
double a;
double b;
double c;
System.out.println("A is? "); //asking for a
String strA = new BufferedReader(new InputStreamReader(System.in)).readLine();
a = Double.parseDouble(strA);
System.out.println("B is? "); //asking for b
String strB = new BufferedReader(new InputStreamReader(System.in)).readLine();
b = Double.parseDouble(strB);
System.out.println("C is? "); //asking for c
String strC = new BufferedReader(new InputStreamReader(System.in)).readLine();
c = Double.parseDouble(strC);
if(c == 0) { //the block that finds out what c is
b = b * b; //getting b squared
a = a * a; //getting a squared
c = a + b; //a squared + b squared equals c squared
double x=Math.sqrt(c); //finding the square root
System.out.println("C is " + x); //telling what c is
}
if(b == 0) {
c = c * c;
a = a * a;
b = a - c;
if(b <= 0) b = b * -1; //ensuring that the program will not to try to find the square root of a negative number
double y=Math.sqrt(b);
System.out.println("B is " + y);
}
if(a == 0) {
b = b * b;
c = c * c;
a = c - b;
if(a <= 0) a = a * -1;
double z=Math.sqrt(a);
System.out.println("A is " + z);
}
}
}

You get prompted for A,B and C side of a right triangle, if you don't know one side, enter in 0 for that one. The only new stuff is double x=Math.sqrt(c); this is just declaring x and at the same time saying it is the square root of c. Thanks to
moeminhtun on help with the input. This is only scratching the surface of what can be done with Java so here are some more sources that have great information.
Sun has some a lot of documentation on there website.
Java 2: A Beginner's Guide is a great book. This is not a for Dummies book though. It has a steeper, yet easy to follow learning curve. On the right hand side of this page you will also see a link called "Free downloadable code", download this code and look though it, you can learn a lot.
A complete explanation of the Java buzzwords
Some more information from Sun
Beginning Java 2 SDK 1.4 Edition
Learn to program with Java

Save your laptop from crashing when power source is changed

Like most people who have complained about this problem I had recently upgraded to mandrake 9.2 and was surprised by the lack of problems usually associated with upgrading distro's. My surprise turned to horror when I realized the my laptop a Dell Inspiron 8000 would freeze as soon as I unplugged the a/c adapter or plugged in the a/c adapter. After searching for days for the fix and listening to contradictory advice I had finally sorted out the good from the bad information and concluded that a kernel recompile was necessary.

Imagine my surprise after recompiling and realizing that the very cool and professional looking graphical boot was gone. This took a whole 8 hours to sort out. This tutorial is geared specifically for Mandrake users, however the information and the steps for recompiling the kernel to fix the power problem will work on any distro. The tutorial will also seek to explain how the frame buffer sections of the kernel source work, so that you're new kernel will have a clean looking high resolution console. The tutorial will also show mandrake users and users of other distro's which use Bootsplash how to modify their initrd image to boot up with a nice jpeg image.

First things first, the cause of the freezing is that you're particular laptop does not implement the apm bios correctly. So when the kernel boots up and uses apm the calls lock your bios. Unlike most posts in forums across the Internet, the problem is not the acpi interface but the apm. The problem also exacerbated by the use of the local apic on uniprocessor systems and systems with Pentium 4 processors.

So, if you want to recompile the kernel the first thing to do is to download the kernel sources for your distro. If you have a distro like Mandrake which heavily modifies their kernel with programs like supermount you may want to use their kernel instead of the kernels at www.kernel.org. If you are using mandrake an easy way to get the latest kernel sources is to click on your start menu in either KDE or Gnome then mouse over configuration then mouse over packages and finally click on install software. Once the RPMDrake window comes up you can type kernel into the search field. The search should bring up a number of things click on the latest kernel source file it should be an srpm. For those of you who do not want to use the distro's kernel or for people who do not have an easy way of getting their kernel installed you will have to download either a rpm or a tar ball.

Once the kernel is installed you will have to switch to the kernel directory you can do this by typing cd /usr/src/linux (the following assumes you are using the root account). This command will take you to the source directory. If you have compiled a kernel previously you can type: make mrproper. Once this command is done you will have to configure the kernel, which you can do in several different ways. The first is to type make xconfig which will let you configure the kernel inside Xwindows. The second way is to type make menuconfig which will give a text based menu to configure the kernel. For the purposes of this tutorial I will be using make xconfig. So now the menu is on the screen in front of you and looking through all those options can seem like a daunting task. I will try to briefly explain hat each of the options is for and which ones we want to set for our purposes.

The menu is divided into many different sections which at first seem random but actually it is a very ordered system. The kernel is the machine which connects all of your programs to the hardware to put it in an over simplified way. You will notice that the options are split up with the processor and motherboard features in once section. Memory and block devices, eg Hard drives, in another. You will also notice a section for all of the networking options and so on. The first section which we need to modify to fix our freezing problem is the processor type and features section. Click on this section. You will notice this brings up another window with more options in it. The first option allows you to select what processor you are using, it is probably set to i386 generic. We are not here to change this setting, but since we are here it wont hurt to set it appropriately which might even give you a slight performance boost. Further on down you will see to options one option is to enable Toshiba laptop support, If you have a Toshiba laptop enable this feature. The next option asks whether you want to enable Dell laptop support, since I have a Dell I enabled this feature.

A quick note about modules: The debate rages over whether a monolithic or modularized kernel is better. The truth is that each type of kernel has its own advantages and I find myself somewhere in the middle. By using all modules you will find that for instance loading a pcmcia card with module support will take a little longer to load and will use a little more Memory, however if you rarely use this pcmcia card it would be to your advantage to compile it as a module that way you do not have to keep the driver in memory the entire time. I will leave the decision on whether to make a monolithic or modularized kernel up to you, However certain thing must be compiled in and not used as a module. For instance the file system that your root partition is on should be compiled into the kernel, compiling as a module will result in a kernel panic during boot.

Further down you will see and important option it says Local apic support on uniprocessors we ant to disable this feature as it is one of the three things that contribute to the lockups. After selecting no to this question we can close this window by clicking main menu. The next section that we need to look at is the general setup section. Click on General Setup to enter this section now. The next thing that we must enable is pci hotplug support. Click on PCI hotplug Support now to enter the sub menu. Now in the new menu disable the first option Support for PCI Hotplug (experimental) after you have disabled this click main menu to return. Now scroll further down this window we want to disable Advanced Power Management Bios Support. After this is disabled scroll further down until you see acpi support which is the last thing on the list. Click on acpi support which brings up another menu. In this menu acpi support should be enabled and everything else as either a y or a m. Now that you are done with this window click main menu and click main menu again to return to the first menu. Now you have done everything you need to fix the crashing problems. You might also ant to check out the file system tab and make sure what ever file system you are using as root is compiled in and not as just a module. After doing all of the following you might want to also go through each menu and turn off the drivers for hardware you don't have or don't plan to get. This will decrease the compiling time and size of your kernel tremendously. After going through the rest of the source code and setting everything to your liking the next step is to click save and exit. The window will disappear and save all your setting. The next step is to go back to the prompt and compile the kernel.

The kernel can be compile by type two very simple lines at the prompt. Make sure you are in the source directory /usr/src/linux if you are not then get there.
[root@localhost Linux]# make dep && make clean && make bzImage && make modules
after this you will need to type one more thing:
[root@localhost linux] # make modules_install && make install

After this completed which could take anywhere from 15 to 45 minutes you're new kernel will be complete. You next have to edit your lilo.conf file /etc/lilo.conf and add a couple things.
Your lilo.conf file should have a new entry at the bottom that has your new kernel number eg
image=/boot/vmlinuz-2.4.xx-xxmdkcustom (or something similar)
label=(new name)
root=/dev/hda1 (or your equivilent)
read-only
optional
append=?noapic devfs=mount splash=silent? (make sure to add ***noapic****)
init
=/boot/vmlinuz-2.4.xx-xxmdkcustom.img
vga=791 (this is my setting however depending on resolution your framebuffer may vary)

After adding noapic to the append line save the file and exit. But do not run lilo yet. If you are running a mandrake 9.2 kernel and want to put that pretty splash bootup screen back you have some more work to do.
[root@localhost linux]# cd /usr/share/bootsplash/scripts

this will take you to the scripts directory Where you will use this script to put back your boot image
[root@localhost linux]# sh make-boot-splash /boot/initrd-2.4.xx-xxmdkcustom.img (where this is the name of your new kernel.)
when you are done with this command type:
[root@localhost linux]# lilo (this will update your boot loader and insure that the new kernel with splash screen is used)
after this you are done reboot your os and make sure that it works alright. Have fun and good luck. I hope this solves the problem with freezing of the laptops when the power source is changed. I will write a second tutorial later covering the frame buffer in more detail.

How to configure TV card on Linux

Like quite a few people I had lots of problems getting my new tv card to work. From what I can tell hotplug usually doesn?t detect and assign the correct card number.

Assuming you have a standard kernel it should have all the modules you need.

Firstly you must identify your card number by finding your card on this list, or googling if its not there.

card=0 - *** UNKNOWN/GENERIC ***
card=1 - MIRO PCTV
card=2 - Hauppauge (bt848)
card=3 - STB
card=4 - Intel Create and Share PCI/ Smart Video Recorder III
card=5 - Diamond DTV2000
card=6 - AVerMedia TVPhone
card=7 - MATRIX-Vision MV-Delta
card=8 - Lifeview FlyVideo II (Bt848) LR26
card=9 - IMS/IXmicro TurboTV
card=10 - Hauppauge (bt878)
card=11 - MIRO PCTV pro
card=12 - ADS Technologies Channel Surfer TV (bt848)
card=13 - AVerMedia TVCapture 98
card=14 - Aimslab Video Highway Xtreme (VHX)
card=15 - Zoltrix TV-Max
card=16 - Prolink Pixelview PlayTV (bt878)
card=17 - Leadtek WinView 601
card=18 - AVEC Intercapture
card=19 - Lifeview FlyVideo II EZ /FlyKit LR38 Bt848 (capture only)
card=20 - CEI Raffles Card
card=21 - Lifeview FlyVideo 98/ Lucky Star Image World ConferenceTV LR50
card=22 - Askey CPH050/ Phoebe Tv Master + FM
card=23 - Modular Technology MM205 PCTV, bt878
card=24 - Askey CPH05X/06X (bt878) [many vendors]
card=25 - Terratec Terra TV+ Version 1.0 (Bt848)/Vobis TV-Boostar
card=26 - Hauppauge WinCam newer (bt878)
card=27 - Lifeview FlyVideo 98/ MAXI TV Video PCI2 LR50
card=28 - Terratec TerraTV+
card=29 - Imagenation PXC200
card=30 - Lifeview FlyVideo 98 LR50
card=31 - Formac iProTV
card=32 - Intel Create and Share PCI/ Smart Video Recorder III
card=33 - Terratec TerraTValue
card=34 - Leadtek WinFast 2000
card=35 - Lifeview FlyVideo 98 LR50 / Chronos Video Shuttle II
card=36 - Lifeview FlyVideo 98FM LR50 / Typhoon TView TV/FM Tuner
card=37 - Prolink PixelView PlayTV pro
card=38 - Askey CPH06X TView99
card=39 - Pinnacle PCTV Studio/Rave
card=40 - STB2
card=41 - AVerMedia TVPhone 98
card=42 - ProVideo PV951
card=43 - Little OnAir TV
card=44 - Sigma TVII-FM
card=45 - MATRIX-Vision MV-Delta 2
card=46 - Zoltrix Genie TV/FM
card=47 - Terratec TV/Radio+
card=48 - Askey CPH03x/ Dynalink Magic TView
card=49 - IODATA GV-BCTV3/PCI
card=50 - Prolink PV-BT878P+4E / PixelView PlayTV PAK / Lenco MXTV-9578 CP
card=51 - Eagle Wireless Capricorn2 (bt878A)
card=52 - Pinnacle PCTV Studio Pro
card=53 - Typhoon TView RDS + FM Stereo / KNC1 TV Station RDS
card=54 - Lifeview FlyVideo 2000 /FlyVideo A2/ Lifetec LT 9415 TV [LR90]
card=55 - Askey CPH031/ BESTBUY Easy TV
card=56 - Lifeview FlyVideo 98FM LR50
card=57 - GrandTec 'Grand Video Capture' (Bt848)
card=58 - Askey CPH060/ Phoebe TV Master Only (No FM)
card=59 - Askey CPH03x TV Capturer
card=60 - Modular Technology MM100PCTV
card=61 - AG Electronics GMV1
card=62 - Askey CPH061/ BESTBUY Easy TV (bt878)
card=63 - ATI TV-Wonder
card=64 - ATI TV-Wonder VE
card=65 - Lifeview FlyVideo 2000S LR90
card=66 - Terratec TValueRadio
card=67 - IODATA GV-BCTV4/PCI
card=68 - 3Dfx VoodooTV FM (Euro), VoodooTV 200 (USA)
card=69 - Active Imaging AIMMS
card=70 - Prolink Pixelview PV-BT878P+ (Rev.4C)
card=71 - Lifeview FlyVideo 98EZ (capture only) LR51
card=72 - Prolink Pixelview PV-BT878P+9B (PlayTV Pro rev.9B FM+NICAM)
card=73 - Sensoray 311
card=74 - RemoteVision MX (RV605)
card=75 - Powercolor MTV878/ MTV878R/ MTV878F
card=76 - Canopus WinDVR PCI (COMPAQ Presario 3524JP, 5112JP)

card=77 - GrandTec Multi Capture Card (Bt878)
card=78 - Jetway TV/Capture JW-TV878-FBK, Kworld KW-TV878RF
card=79 - DSP Design TCVIDEO
card=80 - Hauppauge WinTV PVR


Write down this number.

Find your tuner type:


Tuner number

type=0 - Temic PAL (4002 FH5)
type=1 - Philips PAL_I (FI1246 and compatibles)
type=2 - Philips NTSC (FI1236 and compatibles)
type=3 - Philips (SECAM+PAL_BG) (FI1216MF, FM1216MF, FR1216MF)
type=4 - NoTuner
type=5 - Philips PAL_BG (FI1216 and compatibles)
type=6 - Temic NTSC (4032 FY5)
type=7 - Temic PAL_I (4062 FY5)
type=8 - Temic NTSC (4036 FY5)
type=9 - Alps HSBH1
type=10 - Alps TSBE1
type=11 - Alps TSBB5
type=12 - Alps TSBE5
type=13 - Alps TSBC5
type=14 - Temic PAL_BG (4006FH5)
type=15 - Alps TSCH6
type=16 - Temic PAL_DK (4016 FY5)
type=17 - Philips NTSC_M (MK2)
type=18 - Temic PAL_I (4066 FY5)
type=19 - Temic PAL* auto (4006 FN5)
type=20 - Temic PAL_BG (4009 FR5) or PAL_I (4069 FR5)
type=21 - Temic NTSC (4039 FR5)
type=22 - Temic PAL/SECAM multi (4046 FM5)
type=23 - Philips PAL_DK (FI1256 and compatibles)
type=24 - Philips PAL/SECAM multi (FQ1216ME)
type=25 - LG PAL_I+FM (TAPC-I001D)
type=26 - LG PAL_I (TAPC-I701D)
type=27 - LG NTSC+FM (TPI8NSR01F)
type=28 - LG PAL_BG+FM (TPI8PSB01D)
type=29 - LG PAL_BG (TPI8PSB11D)
type=30 - Temic PAL* auto + FM (4009 FN5)
type=31 - SHARP NTSC_JP (2U5JF5540)
type=32 - Samsung PAL TCPM9091PD27
type=33 - MT2032 universal
type=34 - Temic PAL_BG (4106 FH5)
type=35 - Temic PAL_DK/SECAM_L (4012 FY5)
type=36 - Temic NTSC (4136 FY5)
type=37 - LG PAL (newer TAPC series)
type=38 - Philips PAL/SECAM multi (FM1216ME MK3)
type=39 - LG NTSC (newer TAPC series)

There is also a radio number but I am not sure where there is a complete list of cards and radio numbers, although you can try here.
http://www.linuxquestions.org/hcl/index.php?cat=120

Now depending on weather your system uses a 2.4.x or 2.6.x kernel we need to change either the /etc/modules.conf (2.4.x) or /etc/modprobe.conf (2.6.x)

Open your favorite text editor as root and open /etc/?correct from above? and add the following subsittuting the card=(your card number) tuner=(your tuner number) and radio=(your radio number)


#TV Card
alias char-major-89 i2c-dev
alias char-major-81 bttv
options bttv card=78 tuner=32 radio=1

Save and exit.

Now restart your computer and install your favorite tv viewing program for example Xawtv or Tvtime and enjoy.

I hope this helps someone.

How to configure LCD/TFT Monitor

LCD/TFT Monitor Configuration in X.Org
**DISCLAIMER**

I only have one monitor to test this with. The results back have been mixed so far from others. If there is nothing wrong with your display, please don't go mucking about with your .fonts.conf file (at least, back it up first). If you do have problems with your LCD display, then this is a good bet to try. If you have any success or failure, please do report this back, and changes, if any, that you made to your .fonts.conf file and any other file (perhaps /etc/X11/xorg.conf?) Comments, corrections and thoughts are always welcome.

This may well also work with XFree86 > 3.3, since they use the same font configuration files as this. Older versions have a non-XML font configuration setup ( I don't know when the switch over was made though), and I will *not* cover this.

Contents

1) Prologue
2) Introduction
3) Thanks
4) What You'll Need
5) Physical Setup
6) Settings
7) Thoughts
Non TrueType fonts
Gtk/qt
8) Appendix A
9) Bibliography

1. Prologue

The scenario: On 10th November 2004, my Sony CPD-210EST 17" CRT monitor reached the end of its life. At 11:13am (GMT) 11th November, it was declared DOA. In its place, since no one really sells decent CRT monitors any more, I replaced it with a Hyundai Q17+ 17" LCD/TFT monitor.

2. Introduction

LCD/TFTs display fonts very differently to a CRT. If you have a distribution that can deal with this all, then great. If not, with default settings on X.Org/Freetype, the results for fonts are painful to say the least. This is further compounded by the fact that all the 'helpful' HOWTOs out there are badly out of date, referring to ancient versions of the now defunct XFree86. Whilst there are a few sites out there with bits of information, no one site has put together a workable solution specifically for an LCD on a modern distribution.

This exists, therefore, to show the naysayers that they're wrong, and that you _can_ enjoy nice looking fonts in Linux.

(Please note, this is _not_ an exhaustive HOWTO, and it is _not_ a HOWTO about .fonts.conf (please see my Bibliography for a site detailing the tags available in .fonts.conf. This HOWTO is only about configuring an LCD and what works best for one))

3. Thanks

Transformer, The ? For blowing up on my last monitor and leading me to an LCD.
synaptical ? First person to report back that my .fonts.conf actually worked elsewhere!
J.W. - Suggested that I actually write this up on LQ.org

4. What You'll Need

Your favorite text editor (not vi, obviously =:-P)
X.Org (or a later edition of XFree86 >3.3? Not tested on XFree86 though)
An LCD/TFT monitor (obviously! I haven't tested this on laptops, but I'm told it does make a difference)

5. Physical Setup

?It's alive!?

(I humbly apologize in advance for this section, but better safe than sorry.)

Plug up your LCD. Where possible, use the DVI-D (i.e. The digital, not analogue VGA) connector. Then, ensure the viewing angle of your LCD is correct. Play around with it until you are happy.

6. Settings

?Treachery, faith and the X server?

As far as I know, there is no special configuration of X needed for an LCD monitor. Just create the necessary entry for a monitor in /etc/X11/xorg.conf with the correct refresh rates as normal and enjoy (the monitor may even be able to give you these, mine does.) I won't cover this in any more detail, as it's pretty standard and covered better elsewhere.

X will also detect the correct sub-pixel alignment, so you don't need to deal with that either.

?Damned, dirty fonts?

The real problems begins with font rendering in X. If you are switching over on your machine from a CRT to an LCD, I can guarantee that what is facing you at first sight will be unpleasant to say the least.

Of course, this isn't down to X, as in all modern X servers from X.Org (and the later ones from XFree86) Freetype is responsible for all font rendering (any other modules loaded in your /etc/X11/xorg.conf for font rendering are anachronisms: they are no longer used, and X tells you they don't exist.) Therefore, we need to throw everything that Freetype has against these fonts to cure this.

If you're using KDE or GNOME, you'll find they have tools for doing what I'm going to describe. However, I would recommend _against_ using them. I've found their attempts and generating and editing a .fonts.conf file to be dubious to say the least. It is much more preferable to edit the file yourself, and since it is now done through a well defined XML file, this isn't as bad as it once appeared to be (judging from some of those ancient HOWTOs).

Crank up your editor and load ~/.fonts.conf.

If it exists: Just add these sections to it, inside the tags.
If it doesn't exist: Use my .fonts.conf from Appendix A as your .fonts.conf and edit as necessary. I have annotated the file, and you can also easily follow my instructions below (but all these sections are already in my example file)

The steps are:

1. Ensure that the sub pixel order is _not_ specified.

This goes against everything that all the other HOWTOs put together say, but there is yet method in my madness.

X knows the sub pixel order already, and if this is enabled as well, Freetype produces some very strange results. However, if you do still have problems, consider replacing 'none' with 'rgb' (the standard for LCD monitors), 'bgr' (unusual), 'vrgb' (vertical rgb, if you have a monitor that has been rotated by 90 degrees[1]), 'vgbr' (as vrgb, but very rare).


none


2. Enable sub-pixel hinting

This is what really starts to make the difference. Visit http://grc.com/ctwhat.htm for the technical explanation of why we're doing all of this.


true


3. Set the hinting amount.

Usually, this should be full. Valid options are 'none', 'hintslight', 'hintmedium' and 'hintfull' which should be fairly self explanatory if you've read the article at the link above.


hintfull


4. Enable anti aliasing

This makes the fonts start to look nice, but some of the shapes will be distorted, so we need the Freetype autohinter.


true


5. Enable the Freetype autohinter

Autohinter is not turned on automatically. Only disable this if you have recompiled Freetype with the bytecode interpreter, which is run automatically.

Although to be honest, Freetype are right, and the autohinter is actually better. Note that OpenOffice.org binaries from the OOo website are built against the bytecode interpreter, so even if you have compiled Freetype with the bytecode interpreter and override it with the autohinter, OOo will still use the bytecode interpreter.


true


6. Testing

Just load any application up, and the changes should be immediately obvious (I used Mozilla Firefox, by opening and closing it after every tweak of .fonts.conf, I was able to see the changes immediately).

7. Make changes system wide

If you like your users (however ungrateful they may be), you can apply these changes system wide, using /etc/fonts/local.conf

If it exists, just append the sections you added above to it.
If it doesn't, use your .fonts.conf as /etc/fonts/local.conf, and edit it as necessary

(The order of precedence for processing is /etc/fonts/fonts.conf (automatically generated, don't edit it), /etc/fonts/local.conf then ~/.fonts.conf - meaning your local .fonts.conf will always override the system settings).

7. Thoughts

Non TrueType fonts

Non TrueType fonts don't render well on LCDs as they don't support anti aliasing. For these fonts (e.g. Helvetica), it is recommended that you specify an alternative TrueType font (that is hopefully similar, but it doesn't have to be!) to use instead (in this case, whatever the default sans-serif font on the system is):


Helvetica


sans-serif


You can easily reuse this for other fonts. Just copy and paste, then replace 'Helvetica' with the font to replace, and 'sans-serif' with the font you want to replace it with (or you can use the generic descriptions 'serif', 'sans-serif' or 'monospace').

Gtk/qt

Some programs (usually older ones) using these libraries may not always use anti aliased fonts, even if you've specified them in .fonts.conf. This can fixed by:

Gtk: Add the following to /etc/profile:
export GDK_USE_XFT='1'

qt: In ~/.qt/qtrc, ensure the following entry is present under [General]:
enableXft=true
useXft=true

Programs coded using Gtk 1.x don't appear to support anti aliasing properly ? you may want to consider replacing them with ones that use Gtk 2.x (e.g. Replacing XMMS with its Gtk2 fork, Beep Media Player)

8. Appendix A

The following is my .fonts.conf file. Just drop it into your home directory, load up a program and compare it to your already running ones. If the results are good enough, then you can add the contents of this file to /etc/fonts/local.conf (if it doesn't exist, just create the file with the .fonts.conf. If it does, copy what you need inside the tags) to override the settings for all your users, and save them from having to figure this out.

If it doesn't work, then I have annotated the file to indicate what you might need to change.

How To Track Bandwidth Usage with RTG

I?d like to monitor bandwidth usage. What should I use?

If you?re interested in monitoring bandwidth and have done a little research, you probably have run across MRTG, RRDtool, and Cricket. While these three packages are all fantastic and popular tools, you should also consider a lesser known tool named RTG. (Learn about all of your options, and then choose the utility that best suits your needs.) As of this writing, the current version of RTG, which is available from http://rtg.sourceforge.net and is licensed under the GPL, is 0.7.4.

Designed for enterprises and service providers that need to collect time-series SNMP data from a large number of targets, RTG is a flexible, scalable, high-performance SNMP statistics monitoring system. All collected data is inserted into a relational database that provides a common interface for applications to generate complex queries and reports. Currently the only officially supported database is MySQL, but PostgreSQL support is available in CVS.
Written in C, RTG runs as a multi-threaded daemon, making it extremely lightweight and fast. Unlike tools such as MRTG, it does no data averaging. The up-side to this is that you can get information from arbitrary time intervals and get exact information suitable for billing. The downside is that, as time goes on, you have more and more data to store (MRTG data files never grow due to the data consolidation algorithm it uses).
Installation is the usual ./configure && make && make install. You?ll need the MySQL libraries, often called mysql-devel by distributions, and Net-SNMP libraries to perform the install. After the make install is complete, run the included script to create the necessary MySQL database, tables, and user.

$ /usr/local/rtg/etc/createdb mysql_root_password

Now that the database is setup, add any routers you want to monitor to /usr/local/rtg/etc/routers. The format is device:community:bits, where device is the DNS name or IP address of the device you?d like to monitor, community is the SNMP community name, and bits is an optional parameter to allow you to change the default behavior of using 32-bit SNMP counters. After you?ve entered each device on its own line, run /usr/local/rtg/etc/rtgtargmkr.pl, which creates a targets.cfg file.
You are now ready to run the RTG poller:

$ /usr/local/rtg/bin/rtgpoll ?v ?t /usr/local/rtg/etc/targets.cfg

If the poller fails to find a configuration file, it creates one named rtg.conf in the current directory. (You may need to edit this file if your installation is non-standard.) By default, rtgpoll stays in the foreground. Once you have everything configured and working correctly, add the appropriate line to run rtgpoll in the background to your startup scripts.
Now that you have the poller running, take a look at the data. RTG provides a variety of web scripts to display the information. Additionally, since the data is simply stored in the database, you are free to write a script in your preferred language to present the data in a way that? most useful to you.
For this article, let?s assume that you already have a PHP-enabled web server running. First, copy the included scripts to a directory accessible from your web server.

# cp /usr/local/rtg/web/* /var/www/html/rtg/
# cp /usr/local/rtg/bin/rtgplot /var/www/html/rtg/rtgplot.cgi

You can now view Interactive Reports at http:// your_machine /rtg/rtg.php; 95th percentile queries at http:// your_machine /rtg/95.php; and MRTG-style overview and daily, weekly, and monthly plots at http:// your_machine /rtg/view.php.
You can also call rtgplot.cgi directly by passing in the appropriate parameters.
Additionally, RTG provides a command-line Perl script that yields plain, text reports. For instance, typing $./report.pl customername 01/01/2005 01/02/2005
generates a report like that shown in Figure One.

FIGURE ONE: The output of RTG?s reporting script

customername Traffic
Period: [01/01/2005 00:00:00 to 01/03/2005 00:00:00]

In Out Avg In Avg Out Util Util Max In Max Out Max Ut Max Ut
Connection MBytes MBytes Mbps Mbps In % Out% Mbps Mbps In% Out%
----------------------------------------------------------------------------------------------------------
Ethernet0/1 x.x.x.x 106 212 0.08 0.16 0.80 1.60 0.70 0.90 7.00 14.20

Total: 106 212 0.08 0.16 0.80 1.60 0.70 0.90 7.00 14.20

By using the included scripts and leveraging the ability to write your own custom scripts, RTG will quickly and easily allow you to not only monitor bandwidth usage, but also setup billing scripts, trigger alerts based on custom monitoring thresholds and almost anything else you can imagine.

Live CD Clustering Using ParallelKnoppix

If you’ve run across Beowulf or another cluster implementation, but thought that assembling your own cluster was either too complicated or too resource-intensive, cheer up! Given five minutes, a specialized, live Linux distribution called ParallelKnoppix, and a handful of ordinary personal computers, you too can build your very own mini-mini-mini-supercomputer.
ParallelKnoppix, a remaster of the Knoppix (http://www.knoppix.org/) live CD distribution, allows you to construct a parallel processing cluster using off-the-shelf desktops, laptops, and servers, and the LAM-MPI and/or MPICH implementations of the Message Passing Interface (and PVM). Moreover, because ParallelKnoppix is a live CD, you can convert a room full of machines — even those running Windows — into a Linux cluster without affecting the natively-installed operating system. Getting a cluster up and running takes about five minutes if all of your machines have PXE network cards. Clusters from two to 200 machines are supported.
Download, Burn, Boot
The first thing to do is download the ParallelKnoppix ISO image from http://pareto.uab.es/mcreel/ParallelKnoppix/ and burn one CD for each computer you’d like to include in the cluster. Next, boot one of the machines you’ll be using with the CD. (Keep in mind that you’ll need at least one Linux accessible partition on this machine. If the machine in question only has NTFS partitions, you can use a USB drive formatted as FAT32 to gain the needed space.) The machine should follow the normal Knoppix boot sequence.
Once the machine is booted, select ParallelKnoppix –> Setup ParallelKnoppix from the KDE menu to start the configuration script. Once in the configuration script, click OK to start the Terminal Server. The next dialog box will ask you how many nodes will be in the cluster, including the master node you’re using at the moment. Next, you’ll be asked to select all of the network drivers needed for the cluster. To simplify things, ensure that each slave machine is setup to PXE boot. While it’s possible to work around this, it complicates the setup and is beyond the scope of this article.
The next screen gives you a couple of cluster options. Keep the default of textmode and do not check the secure box. (See the sidebar “ParallelKnoppix Precautions.”) Next, provide additional boot options, if any. (You can normally leave this blank.)
ParallelKnoppix Precautions
ParallelKnoppix is an extremely insecure distribution. It is not intended for desktop or server use; instead, ParallelKnoppix is designed to be easy-to-use in an environment that can be restored quickly if any disaster occurs.
It’s highly recommended that you run ParallelKnoppix and your entire cluster on a dedicated network that is disconnected from the Internet.
You’re now ready to start the terminal server and are at the point where you’ll need a read/write mountable partition. Select the partition you’d like to use and click OK. A working directory with the name parallel_knoppix_working is created and exported by NFS. Anything you want to be accessible to the cluster should be placed in this directory.
Slaving Away
Now it’s time to boot each of your slaves. Once you’re sure all of the slave node machines are booted, click OK to have them mount the working directory. You should now have a working Linux cluster.
The ParallelKnoppix ISO has some example cluster applications in /home/knoppix/Desktop/ParallelKnoppix/Examples. To run one of them, copy the entire subdirectory (for example, /Octave/) into your working directory. From there, each example should have a README that explains how to run the program on the cluster. One great thing about being a Knoppix derivative is that fact that you can further remaster ParallelKnoppix to suit your needs, which could include your own custom application and data.
With this article and a couple ParallelKnoppix discs, you should be able to have a Linux cluster up and running in no time. This is a great way to get your feet wet with clustering or to prototype your next custom clustering application. Make sure not to forget about the inherent insecurities in this setup. Have fun and enjoy the rocket science.

How to secure your inbox

Not too long ago, email was a wonderful thing. It provided a fast and easy method to communicate with family, friends, and co-workers, regardless of timezone or location. Unfortunately, due to spam and viruses, many people now find email almost unusable.
Spam

The first item on the agenda is eliminating spam. Spam, or unsolicited commercial email (UCE), is not only a nuisance, it's a productivity killer.

SpamAssassin (SA), which is distributed under the same license as Perl, helps put an end to this problem. Using its rule base, SA performs a wide range of heuristic tests on email headers and body text to identify and score spam. SA can also use blacklists and optional modules such as Razor, Pyzor, and a built-in Bayesian filter that learns new spam characteristics.

One of SpamAssassin's greatest assets is its flexibility. You can install SA in a wide variety of configurations, from a local install in your home directory (on a machine where you do not have root access), to a system-wide install that affects all users. You can also configure SA to allow each individual user to set their own rules, thresholds, and settings. And, because SpamAssassin tags messages by adding additional headers, it allows you to control what happens to each message.

To install SA, do the following as root:

# perl ?MCPAN ?e shell
cpan> install Mail::SpamAssassin

Alternatively, if you don't have root access, you can download the source from http://www.spamassassin.org and do the following after unpacking the tarball:

% cd Mail-SpamAssassin-*
% perl Makefile.PL PREFIX=~/sausr
SYSCONFDIR=~/saetc; make; make install

After you install SA, look at the configuration file called local.cf. This file allows you to whitelist certain addresses, tweak rules, add custom rules, enable/disable specific tests, and change a variety of other options.

You can also choose how you'd like to integrate SA into your MTA (if site-wide), or how you'd like to process your mail with SA (local install). SA works well with sendmail, qmail, PostFix, Exim, and most others. It can even be called via procmail, milter, AMaViS, MIMEDefang, or QMAILQUEUE.

If you installed SA in your home directory, you can put the following two rules in your procmailrc file to run SA on your mail and sort spam into a folder named caughtspam:

:0fw: spamassassin.lock
/home/user/sausr/bin/spamassassin
:0:
* ^X-Spam-Status: Yes
caughtspam

While running SpamAssassin as above is fine for small setups, most large or system-wide configurations should consider running spamd/spamc, which improves performance by avoiding the overhead of starting Perl for each message.

Viruses

You may be thinking, "I use Linux, why do I need a virus scanner?" While it's true that not many viruses have targeted Linux, as Linux's popularity grows, it's likely that the number of viruses will increase. Beyond that, many people who run a Linux machine may have a mail server setup for a few friends and family. Some of these users likely use an operating system that is more prone to viruses. By scanning for viruses, you're not only doing them a favor, but are helping stop the spreading of viruses. After all, if everyone had an up-to-date virus scanner, the outbreaks that we've come to accept would be much less common.

Luckily, there is a free GPLed virus scanner called ClamAV (available from http://clamav.sourceforge.net/) that keeps updated definitions. Like SpamAssassin, ClamAV can be run in both system-wide and local configurations, and allows easy integration with many MTAs. It can also be called via procmail, milter, AMaViS, MIMEDefang, or QMAILQUEUE, and allows you to either reject or quarantine infected messages.

As ClamAV integration can be quite specific to your environment, specific installation and configuration instructions are beyond the scope of this article, but the install is the standard ./configure && make && make install. After installation, become acquainted with the configuration file clamav. conf, and choose between using clamscan or clamd/clamdscan. ClamAV also comes with freshclam. It can be run as a daemon or via cron to keep virus definitions up-to-date.