12.14.2011

Linux - Script to Toggle Touchpad

Short script to toggle the touchpad on/off in Linux.
Comes in real handy if you want to map it to a keybinding in openbox.

#!/bin/bash
if [ $(synclient -l | grep TouchpadOff | awk '{print $3}') == 1 ] ; then
synclient touchpadoff=0;
else
synclient touchpadoff=1;
fi

In openbox I mapped this to my fn + f8 key (the default button to toggle the touchpad on my T420)
I added the following to ~/.config/openbox/rc.xml

<keybind key="XF86TouchpadToggle">
<action name="Execute">
<startupnotify>
<enabled>true</enabled>
<name>toggletouchpad</name>
</startupnotify>
<command></command>~/bin/toggletouchpad.sh
</action>

If you aren't sure what the key presses are that you want to use, there is a program called "xev" that will tell you the xevents keybindings, mousebindings etc. to the actions you are doing.

12.13.2011

Lenovo T420 - Linux - Toggle Touchpad

Here is a helpful little script that toggles the touchpad on or off in Linux.

#!/bin/bash
# My first script
SYNSTATE=$(synclient -l | grep TouchpadOff | awk '{ print $3 }')

# change to the other state
if [ $SYNSTATE -eq 0 ]
then
synclient touchpadoff=1
notify-send "Touchpad" "disabled"
elif [ $SYNSTATE -eq 1 ]
then
synclient touchpadoff=0
notify-send "Touchpad" "enabled"
else
notify-send "Error" "Couldn't get Touchpad status from synclient"
exit 1
fi
exit 0

Thinkpad T420 and Arch Linux

I finally got Archlinux installed on my T420 without much of a headache.

Hardware and General Notes:
I turned off the Nvidia card (I have optimus on this laptop) and I am currently running with only the integrated Intel HD 3000 card. I used the xf86-video-intel driver for the card and everything works great. Sometime in the future I'm going to try to get Bumblebee working.

Sleep is working great.

I haven't tested the webcam or microphone.



thinkpad-acpi:
thinkpad-acpi gives you all kinds of nifty tools to use for controlling different aspects of the Thinkpad series laptops.

This comes default with most kernels (it comes with Arch anyway.) To enable it edit /etc/rc.conf with your favorite text editor and under the MODULES section add the following:
MODULES=(thinkpad-acpi)

Now (probably after a restart) you should be able to control some of your thinkpad's hardware. One of my favorite things is controlling the thinklight through the command line using the following commands:
echo 255 > /sys/class/leds/tpacpi\:\:thinklight/brightness
echo 0 > /sys/class/leds/tpacpi\:\:thinklight/brightness

This will toggle the thinklight on and off. There are all sorts of uses for this in bash scripts etc. There is a nice plugin for pidgin that uses the Thinklight and flashes whenever someone sends you an IM. A nice little addon.

12.02.2011

Thinkpad T420

I recently ordered a new Thinkpad t420 off of Lenovo's website. I figured it was about time to replace my Asus eeepc with an actual notebook, something that I could actually use for programming. I wanted something durable that would last a while, and of course something that could handle Linux well. I opted for the t420 and took advantage of some of Lenovo's black friday deals.

It shipped out on Dec. 2 and should arrive by Dec. 8.

Specs:

CPU:
Intel(R) Core(TM) i5-2520M CPU @ 2.50GHz
GPU:
Intel Corporation 2nd Generation Core Processor Family Integrated Graphics Controller (rev 09) nVidia Corporation Device 1057 (rev a1)
Audio:
ALSA Timer Device
Audio:
ThinkPad Console Audio Control
Audio:
HDA Intel PCH (CONEXANT Analog)
Audio:
HDA Intel PCH(HDA Intel ALSA hardware specific Device)
Audio:
HDA Intel PCH
Network:
Loopback device Interface
Network:
RTL8188CE 802.11b/g/n WiFi Adapter
Network:
82579LM Gigabit Network Connection


I haven't heard the best of things on Lenovo's shipping, but we will see what it looks like. Currently the shipping information is as follows
EDIT:
Wow, the t420 had quite the trip from hong kong! Final shipping times.
Tri-City, IN, United States 12/08/2011 12:46 P.M. Delivered
12/08/2011 4:15 A.M. Out For Delivery
12/08/2011 3:42 A.M. Arrival Scan
Indianapolis, IN, United States 12/08/2011 1:42 A.M. Departure Scan
Indianapolis, IN, United States 12/07/2011 6:45 P.M. Arrival Scan
Louisville, KY, United States 12/07/2011 3:41 P.M. Departure Scan
12/07/2011 2:35 P.M. Import Scan
12/07/2011 2:00 P.M. Arrival Scan
Anchorage, AK, United States 12/06/2011 11:50 P.M. Departure Scan
12/06/2011 1:48 P.M. Arrival Scan
Chek Lap Kok, Hong Kong 12/06/2011 9:27 P.M. Departure Scan
Chek Lap Kok, Hong Kong 12/03/2011 8:30 A.M. Origin Scan
Hong Kong 12/02/2011 7:04 A.M. Order Processed: Ready for UPS

12.01.2011

Nvidia Optimus and Linux - Notes

I have a Lenovo Thinkpad t420 coming in the mail soon. I opted for the version with the Nvidia Optimus technology which switches between an Nvidia 4200m (?) graphics card and the Intel HD 3000 integrated graphics. The Linux support for Optimus isn't entirely there, but I've found a couple interesting fixes.

Basically you can either (from the BIOS) turn off the Nvidia card completely or turn off the Intel HD Graphics completely.
A better option (in my opinion) is the Ironhide project which allows you to launch applications using the Nvidia card and uses the Intel HD Graphics for everything else.

Basically I'm keeping some notes in the bottom of this post and will write a tutorial and review later for Linux on the t420 with Optimus.
Notes:
Ironhide for Ubuntu
Ironhide
Ubuntu Forums Ironhide how-to
Ironhide PPA Ubuntu

Archlinux AUR search for "Bumblebee" : http://aur.archlinux.org/packages.php?O=0&K=bumblebee&do_Search=Go

General Thinkpad / Notebook tips and scripts
Thinkwiki Thinklight
ArchWiki - T420
CPU Frequency Scaling

9.11.2011

Bubblesort a Linked List

Sometimes when you're programming you just need to sort an array. You could use a built in function like any old schmuck; or you could whip out a shiny sorting algorithm and show off a little bit. Ok, so in all reality using a built in sorting function is most likely more efficient than building your own, but it is important to know what is going on behind the built in abstraction layer.

Today I am going to look at the bubblesort sorting algorithm. The main aspects I will be looking at are the Big-O analysis and general benchmarking.

Before I start I should give a basic summary of what big-O notation is and why it is used. Big-O notation is a way that computer scientists and mathematicians express the limits of a certain algorithm. Basically they show how an algorithm reacts to large data sets.

If you would like to read more, Wikipedia has a great article found here.
big-O

BubbleSort
Bubblesort is a simple "exchanging" algorithm. It starts at the first element of the data set and compares it to the second, if the first is greater it swaps them. It will then compare the second element to the third and so on until it reaches the end of the set. At this point we know that the last element is the largest element. The algorithm will then start at the first two elements of the set again and compare until it reaches the element before the end of the list (since we already decided that was the largest.) This pattern continues until the list is sorted.

As you may have guessed, this is not a very efficient algorithm for sorting a large data set. Bubblesort is rarely used due to this fact, however it does offer several small advantages - it uses a relatively small amount of code and can reach decent efficiency with an almost sorted data set.

Here is the code to the bubbleSort method I came up with. It requires the files LinkedList.java and ArrayFromFile.java which I posted about a week ago. The real meat of the program is in the method bubbleSort(LinkedList list). The main method is only there for testing purposes.

download a zip file with all the files you need to run the program

Code:

Bubblesort Stats:
Big-O best case: n
Big-O average case: n^2
Big-O worst case: n^2
Benchmark (100 elements in the collection): 7844 milliseconds (about 7.8 seconds)
This benchmark will likely be something completely different for you since it will be run on a different machine.

9.07.2011

Linked List

Linked lists are important topics in computer science. The theory behind them is something that computer science students learn in classes like data structures and algorithms. I have created a linked list for you to study. It is fairly simple, but has the necessary methods to expand to it with a BubbleSort algorithm etc.

The following picture explains the idea behind a linked list.


Basically you have a list of these things called Nodes that are linked together. Each node is a bucket of sorts that contains a piece of data. A Linked List is a group of these buckets strung together one at a time. The head bucket is linked to the 2nd bucket, the 2nd bucket is linked to the 3rd and so on until you get to the tail bucket which isn't linked to anything.

Currently I am implementing the following methods on the linked list:
-addAtHead(Object) - creates a node and adds it to the beginning of the linked list as the head Node
-addAtTail(Object) - creates a node and adds appends it to the end of the list as the tail Node
-addAtIndex(Object) - creates a node with Object as its data and adds the node at the index
-addAtIndex(Node) - adds the node at the index
-deleteAtIndex(int index) - deletes the node at the index
-switchWithNext(int index) - switches the current index with the one right after it. This is used by the bubble sorting algorithm.
-printList() - prints out the whole LinkedLIst
-find(Node) - returns index
-find(index) - returns Node
-getSize

To Do:
-Sorting by different algorithms(bubble sort, selection sort etc.)