Archive for June, 2007

How not to spill coffee on your laptop

Saturday, June 30th, 2007

Is it really that hard not to spill your morning coffee on your laptop? I have never done it myself, but some of my users have a really hard time with this concept. In the last 3 years, I had 3 or 4 cases of “Oh shit, I spilled _____ on my laptop”. Usually the spilled liquid is initially water - but later it usually changes into coffee. Go figure.

Usually, spilling anything on your laptop is more or less fatal. Spilling shit on a regular desktop keyboard can be harmless - your keys may get sticky, and in worst case, you will need to spend maybe $20 on a new keyboard. But laptop keyboard is directly over the mobo - so when you spill something on it, the liquid will most likely hit all the crucial components of your system - the CPU, the onboard graphics card, the sound card, the network card, and etc…

Most people should have enough common sense not to spill shit on their portable machines. But then again, common sense is anything but common. So I decided to write this little tutorial. I will be talking about coffee, but please note that this can apply to any drinkable liquid. Here is a hint: positioning of the coffee cup is crucial. Let me show you a visual example:

Coffee Cup Positioning

This is the usual setup most people use when working on a laptop. We have papers on the right, papers on the left, and the computer in the middle. Note that in most cases, there will be a lot of shuffling between the piles of paper - you will be moving papers from one pile to another.

The points labeled A and B are theoretical coffee cup locations. The location A is a very common choice among the sill-prone laptop users. Notice however, that this position is just asking for trouble. A force applied to the cup from the left will invariably make it tip over, and deposit it’s liquid contents all over the keyboard. Furthermore note that it is relatively easy to accidentally apply such a force to the cup while moving papers from the left pile to the right pile, or even by moving your hand from the pile of papers back to the keyboard.

Spilling any liquid directly on the keyboard is likely to cause instant, irreversible damage to the hardware. Also, if the liquid is a light and sweet latte, chances are that the spill will leave sugar deposits on the hardware, and intense smell. This will prevent hardware repairs under warranty unless you bought the expensive accidental damage protection plan.

Now consider the position B. When the cup at that position is tipped over in any direction, it will likely spill the contents on the table. If the force is applied to the cup from the direction of the right uppermost corner, then the liquid might spill on the back of the laptop, but:

  1. There is a chance that the cup wont tip all the way, but it will be held upright by the laptop display positioned at a 90 degree angle to the table surface
  2. The spill will affect only the laptop casing and the exposed ports in the back
  3. Picking up the laptop will prevent the liquid from penetrating inside the casing, and reaching the mobo
  4. The motherboard and CPU will be shielded from the spill by the raised display
  5. If any of the liquid gets underneath the laptop, the force of gravity will prevent it from traveling upwards where it could damage the motherboard and the CPU

From the above it obviously follows that position B is far superior to position A. Remember about this next time you are enjoying a nice cup of coffee while working on your laptop.

Java: Swing or SWT - which one is better?

Saturday, June 30th, 2007

I’m actually to lazy to write a full argument here, so I will just post my bullshit filler crap from a recent research paper:

5.3 Designing a Graphical User Interface

The discussion of GUI design should start by choosing appropriate widget toolkit. Java offers developers several attractive graphical environments. We will first take a closer look on each of them. Visualization details will then be explained in terms of that chosen toolkit.

5.3.1 Choosing a Widget Toolkit

There are three competing major widget toolkits that can be used for building graphical user interfaces in Java: AWT, Swing and SWT. Both AWT and Swing are currently included in Java Standard Edition, while SWT is an external set of libraries that needs to be downloaded and deployed separately.

The AWT toolkit was the first widget system implemented for Java. It is composed from fairly simple wrapper classes which in turn make calls to the native windowing environment to display GUI elements. Since each platform has a different set of native widgets, Sun only included the most basic ones in AWT [37].

Swing toolkit was developed in 1998 as a replacement of AWT. It abandoned the idea of using the systems native windowing environment, and designed a complete, feature rich widget toolkit entirely in Java. Swing is still in active development, and offers an impressing number of different graphical tools and GUI elements to the developers. For example, the package included in Java 1.4 includes 85 public interfaces, and 451 public classes [40].

The pure Java implementation however has proved to be both a blessing and a curse for this toolkit. Since Swing elements are really Java objects, they can only communicate with the underlying operating system through the JVM. In some cases this can create significant overhead, and thus Java based interfaces will often appear
to be less responsive, or slower than their native counterparts [37].

The Standard Widget Toolkit (SWT) [41] developed by IBM is a hybrid between Swing and AWT. It combines both approaches by including both calls to native widget elements, as well as implementing pure Java based ones. This helps to significantly improve the performance without sacrificing any of the advanced features one may
look for in an enterprise grade product [37].

The difference in performance, and responsiveness between Swing and SWT however is widely disputed and highly controversial. For example some benchmarks claim that Swing can outperform it’s competitor with respect to speed of rendering and re drawing windows on non-windows platforms [42]. Thus is is not very clear if SWT is really faster than Swing, or if overhead of the native calls and communication between Java and non-Java elements diminishes any performance gains stemming from using a native widget implementation.

Benchmarking the two toolkits is out of scope for this paper, and thus we chose Swing as our GUI toolkit because it is a Java standard, and it reduces the complexity of our code by eliminating dependency on a third party library.

Bibliography

[37] S. Holzner, Eclipse: Programming Java Applications. O’Reilly, 2004.
[40] R. Eckstein, J. Elliott, B. Cole, D. Wood, and M. Loy, Java Swing. O’Reilly, 2002.
[41] “The standard widget toolkit,” IBM. [Online]. Available: http://eclipse.org/swt
[42] I. Kriznar, “Swt vs. swing performance comparison,” Cosylab D.O.O, 2005. [Online]. Available Here

© 2007 Lukasz Grzegorz Maciak

I pulled the above from a draft version because it’s late, and I don’t feel like locating the finalized reviewed document. So there might be typos and grammar errors in there - which is of course nothing new around ere.

I ended up using Swing, and perhaps it was not the best choice. Not for an image processing application perhaps. So you tell me. Which one is better in your experience. Is SWK really that much faster than Swing? Is the added speed worth the hassle of bundling it with correct SWK packages for correct systems?

I would love to hear from bit SWK supporters - what are the best selling points of this widget kit over Swing, other than the speed? Anyone knows any good benchmark tests that were done to compare these?

I will have to make this same decision for another project soon, and as usual I’m not entirely convinced which one to choose. I’m leaning towards Swing because I worked with it before, and I know at least some of it’s quirks and pitfalls to be avoided. SWK would be a completely new ballgame.

What do you think?

Top Commentators and Top Posts

Friday, June 29th, 2007

If you hit up the front page you will see two new little things in the sidebar. One is a list of the 10 most popular posts on this blog. All of my posts that got dugg at one point or another will be there, along with some of the big discussion threads that had interesting back-and-forward going on in the comments.

The second addition is a list of Top Commentators - which is you guys. This is my way of sending some love and some Google juice towards my regulars. You guys are awesome!

At the moment my top 10 looks like this:

    * Luke (1151)
    * Matt` (194)
    * ZeWrestler (148)
    * Craig Betts (128)
    * Travis McCrea (97)
    * Wikke (74)
    * Fr3d (65)
    * StarHawk (58)
    * Miloš (52)
    * Ara Pehlivanian (51)

It turns out that I’m the most vocal commentator on my own blog. But it’s not because I like talking to myself all the time - it’s because I always try to respond to comments posted by others.

Btw, Matt` - any luck with the email notifications? I mucked around with various things, but I’m not sure why you’re not getting them. I’m at a loss here. You wanna try a different email address, just for shits and giggles?

Anyway, you guys are awesome!

Google Desktop for Linux

Thursday, June 28th, 2007

I got a nice surprise today. I scrolled through the Slashdot feed, and saw a story about Google finally releasing their desktop search tool on Linux. So I immediately went and downloaded a copy.

I like Google Desktop Search because it simply works, and because it integrates with Gmail. I previously tried Beagle but it just didn’t work for me. I had it installed for over a month now, but even the simplest queries returned odd results. For example, queries for the word thesis would yield no results even after a month of indexing, despite the fact that I had about 20 tex files, and a huge pile of pdf files with the word thesis right in their title. Same goes for Web Timesheet and Replicon - I have about a hundred of back and forward emails with that company regarding the timesheet software, and bunch of proposals in PDF form, and some excel spreadsheets with time-sheet in the name.

I restarted the daemon and reset the index many times - but it would never work. Google Desktop Search on the other hand - just worked. I tried above queries with the index only at 15% and I already got very relevant hits. So I’m sold. Goodbye Beagle, welcome Google Desktop.

Btw, it’s nice of Google to provide both an RPM and a Deb as installation choices. I had no issues installing the deb package on Dapper and Google claims it will work on just about any Ubuntu and Debian system. Nice. Although, I have to say this is probably the first linux application that forced me to “reboot” to complete the installation. Ok, it didn’t really tell me to reboot - it just said the search will start next time I log in. So I logged out, and logged back in and then it kicked into place. Very odd. What would they ever need to do that?

And yes, I know, I know. Google will steal my data, eat my soul, and sell my pr0n to the feds. We went over all of that. I’m not scared. I have yet to see some proof showing that Desktop search transmits private information back to Google.

I’m just glad to see that people at Google do care about Linux users, and when they promise a port, they can deliver it.

Batch Upload Images to ImageShack using Perl

Wednesday, June 27th, 2007

Someone asked about this so I felt compelled to deliver. The question was: “how to batch upload bunch of images to some free image hosting service?” Here is the answer. I picked ImageShack because you don’t need to register it, and from what I remember they had lax rules about allowed content. Anyway, here is the Perl code. You will need WWW::Mechanize from CPAN. The script takes a list of images to be uploaded as arguments:

#!/usr/bin/perl -w
use strict;
use WWW::Mechanize;
 
# suppress warnings about malformed forms
$SIG{__WARN__} = sub {} ;
 
my $url = "http://www.imageshack.us/";
 
my $mech = WWW::Mechanize->new();
 
foreach (@ARGV)
{
    $mech->get($url);
 
    $mech->form_number(2);
    $mech->field('fileupload' => $_);
    $mech->submit();
 
    # follow the link to see the image
    $mech->follow_link( text => 'Show', n => 1 );
    my @im = $mech->images();
 
    # display the URL of the uploaded image
    print $im[0]->url() . "\n";
}

To run it just do something like:

upload.pl ~/img/image1.jpg ~/img/image2.jpg

Alternatively to upload all the files in the current directory you can do:

ls | xargs | upload.pl

The output of the script are the URL’s of the uploaded images, appearing in order in which you specify them in arguments - this way you don’t loose track of your images.

One thing to watch for is the form that you specify in the line:

 $mech->form_number(2);

When I started writing this script, I was using 1 instead of 2 and it was working fine. Then I went to eat something, and when I came back, it no longer worked. Not sure what happened, but looking at the amount of javascript on imageshack website it’s possible that they sometimes move around the search box on the page - possibly to prevent exactly what I’m showing you here. )

Enjoy.

Parsing Excel Files with Perl

Wednesday, June 27th, 2007

My company likes to store tons of useful information locked away in excel files. I understand that not everyone understands how databases work, or how to use them. I have no clue how this happened but at some point Excel became the de-facto standard for your every day data storage needs - despite the fact that flat text files are often much better for this.

For example, given a simple tabulated list in a plain text file, I can grep through it, sort it, re arrange it or analyze it using a myriad of mature and time tested text parsing tools. I can also import it into just about any kind of software, and easily write scripts against it. Excel on the other hand, is much less flexible. But there is something about the neat rows and columns of a spreadsheet that draws people to it.

I much prefer to issue a quick command in bash than to open a bulky office application to get some basic info about an employee, or a client. So I decided to write a quick Perl script to parse through Excel files located on a network share (this probably is a common scenario in most offices). Surprisingly, it was very easy.

First you will need to mount the network share on your box. Next you will need the Spreadsheet::ParseExcel package from CPAN.

Here is the script I hacked up to extract data from a sheet which has a unique (searchable) identifiers (here people’s names) in column D, and relevant data in columns E, F, and H:

#!/usr/bin/perl -w
use strict;
use Spreadsheet::ParseExcel;
 
my $FILE = "/path/to/File.xls";
my $SHEETNAME = "Sheet1";
 
# the column that contains searchable key
my $KEY_COLUMN = 3;
 
my $searchstring = $ARGV[0];
 
my $excel = Spreadsheet::ParseExcel::Workbook->Parse($FILE);
my $sheet = $excel->Worksheet($SHEETNAME);
 
foreach my $row ($sheet->{MinRow} .. $sheet->{MaxRow})
{
  my $key	= $sheet->Cell($row,$KEY_COLUMN);
 
  if($key)
  {
    my $f1	= $sheet->Cell($row,4);
    my $f2	= $sheet->Cell($row,5);
    my $f3	= $sheet->Cell($row,7);
 
    if($key->Value() =~ m/$searchstring/)
    {
      print "\n\n";
      print "Key: " . $key->Value() . "\n";
      print "Field 1: " . $f1->Value() . "\n" if($f1);
      print "Field 2: " . $f2->Value() . "\n" if($f2);
      print "Field 3: " . $f3->Value() . "\n" if($f3);
      print "\n\n";
    }
  }
}

This is of course not the most efficient script since I’m looping through all the rows in the spreadsheet. Can you say O(n)? But it’s good enough for what I need it to do.

How do you indent your code?

Tuesday, June 26th, 2007

Talking about indentation styles is like talking about religion. Everyone thinks there is only one true way to indent code, and everyone else is wrong. But let’s do it anyway. I want to know if my readers are “doing it rite“.

Personally, I use the BSD/Allman style:

if(a==b)
{
	// do something
	foo();
}
else
{
	// do something else
	bar();
}

I find that it produces very clear and readable code. Braces align with each other - for every open brace, there should be a closed brace on the same indentation level. This makes it easy to identify blocks of code. I like the extra white space created by placing the opening brace on a line of it’s own.

The other extremely popular style seems to be K&R:

if(a==b) {
	// do something
	foo();
}
else { 
	// do something else
	bar();
}

I fucking, hate, hate, hate this one. I always feel compelled to fix it by putting a newline before each opening brace. It just looks ugly and does not contribute to code readability. In BSD/Allman, an empty line with the opening brace is very visible cue letting you know that a code block has been opened. It’s hard to miss it - even if you are just skimming through the code. And even if the indentation gets messed up in some deeply nested statements, you can easily figure out what belongs where by simply counting braces.

In K&R counting braces is not that easy - especially if the opening statement is really long and the left brace either drops off the right edge of the screen, or word-wraps around messing up your indentation.

So, which way do you indent your code? Or perhaps you use another style not mentioned here? I know there are bunch of other ones like BSD/KNF, Whitesmiths, GNU, Banner style and etc. But they are all pretty much variations on the two styles above. For example the GNU style is like BSD/Allman but you indent the opening and closing brace by 2 spaces. Go figure.

Anyway, feel free to disagree with whatever I said here, and defend your favorite indentation style. I also dare everyone who uses one of the oddball styles (ie, not BSD/Allman or K&R) to stand up and represent. mrgreen

Windows within windows

Tuesday, June 26th, 2007

I just found this amusing:

Nested Windows
click to enlarge

Let me explain what is going on in this picture:

  • Windows 2000 Profesional is running in VirtualBox under Kubuntu
  • Inside the emulated windows I opened a Remote Desktop connection to Windows XP Professional Workstation
  • Using the Windows XP workstation I connected to a Windows 2000 box via Crossloop
  • Finally I used the remote Windows 2000 workstation to connect to a Windows 2003 server via Remote Desktop

I was bored. Sue me.

PerfMonG 0.2.5 Released

Tuesday, June 26th, 2007

I finally got around to rolling up the installer for the newest version of PerfMonG. Release 0.2.5 has bunch of small updates that improve the usability. I added small tweaks to the interface that now allow you to control the threshold beyond which the CPU counter changes color to blue or red. Mike made sure that the tool works well in dual screen configuration, and doesn’t disappear off the screen when you disconnect one of the monitors.

I merged the release_0.2.5_candidate branch into the HEAD of the trunk, and also created a new tag for this version. At this moment all 3 locations contain the same code, but the trunk will probably start changing soon. If you want the bleeding edge code, check out from the trunk. Otherwise, get it from the 0.2.5 tag.

We still have a long way to go, and much tweaking and refactoring to do before I’m truly happy with it. But, it is slowly starting to shape up into a nifty little tool.

If you are to lazy to click any links, and you have no clue what I’m talking about, let me explain. PerfMonG is my small open source project - a minimalistic performance monitor for windows written in .NET. I mentioned it once before. If you are a windows user, or a .NET programmer definitely check it out, and contribute if you can. )

Linux Fuckup of the Day

Monday, June 25th, 2007

I had to set up a virtual machine on one of the Ubuntu boxes today. I heard good things about Virtualbox so I decided to give it a shot - especially since it’s distributed under GPL, it can can be installed directly via apt by simply adding this line to your sources-list:

deb http://www.virtualbox.org/debian dapper non-free

You will need to add their public key before you install.

During the installation Virtualbox creates a user group called vboxusers. To run emulation you need to be a member of that group. So what did I do? I decided to quickly add myself into the group:

sudo usermod -Gvboxusers username

I happily typed in the password, and hit enter, and decided to log out to make the change go into effect. Only halfway through the log in process I realized what I just did. I forgot -a.

Yep. I just removed myself from all the groups except for vboxusers. Brilliant! I absolutely hate when I do stupid shit like that. It’s not like this was hard to fix - I just didn’t remember of the top of my head what groups I was supposed to belong to. Of course since I was no longer part of the sudo and admin groups I could no longer sudo. Luckily enough, back in the day I decided to enable the root password. So I was able to su to become root, and then usermod myself to admin, and bunch of other groups I needed like audio, video, tty, lp and etc… I wonder what would happen if I did this on a default Ubuntu box without root account. I wonder if I would be able to recover from this that easily.


Bad Behavior has blocked access attempts in the last 7 days.