Tuesday, December 8, 2009

Trying out Kernel Mode Setting (KMS) on Radeon 4850

I've been trying out Kernel Mode Setting (KMS) on my Radeon 4850, since it's extremely cool! The new kernel/mesa/ati-drivers also allows me to run accelerated 3D graphics on the desktop, finally! Well how does it work? Just have a look!
I think I have some more stuff to do ;)

Saturday, November 21, 2009

Listing the size of directories

By doing ls -l, you won't get the size of directories. It will only be listed as 4.0K, while the files are listed correctly. But even the files are actually not listed correctly if you are currently changing the file, for instance during a download. It will list the final size of the file, not the current file size.

If you instead use the 'du' (disk usage) command, you will get a more correct number, even for directories.
du -sh *
will list all the files and directories in the current folder, with the size it occupies on the disk.

Listing which kernel modules you have built

Every now and then I'm confused about which modules I actually have compiled. And what was the name of that module you just built to fix your lm_sensors or wifi-card. This problem is not as common as it used to be, since a lot of software now seems to load the modules it needs, or even the kernel does it itself. But it's always good to have a clue I guess.

A little handy command to run is:
find /lib/modules/$(uname -r)/ -type f -iname '*.o' -or -iname '*.ko'
This will list all the modules in your module directory for the currently running kernel.

Saturday, September 12, 2009

Open Document Format + C# = <3

So you want to be able to manipulate ODF-documents with C#? That was exactly what I wanted to do this morning and after some quick research I found a solution.

Solution 1: The Hard Way (tm)
Turns out that the .odt- and .ods-filetypes (.ods being the one I was interested in) are basically zip-archives which can be decompressed by any archive software supporting it. Inside the archives you'll find a bunch of .xml-files. The file 'content.xml' is where the goodies are. The hard part is parsing the xml-code and getting something useful from it. This I leave to the reader since I wasn't able to get a good result in a hurry.

Solution 2: The Easy Way (tm)
Turns out that someone has already done all the work for us and put it in a small library called AODL . I recommend downloading the binary version (less files), however the documentation resides with the source code in the src-archive so you might as well grab that to while you're at it. Though this library seemed great at first, I quickly ran into a problem. But before we go into that, lets open a document.


using AODL.Document.SpreadsheetDocuments;
using AODL.Document.Content;
using AODL.Document.Content.Tables;

[...]

SpreadsheetDocument doc = new SpreadsheetDocument();
doc.Load("/home/god/blahblah.ods");
RowCollection rows = doc.TableCollection[0].RowCollection; //get rows in first page of doc

foreach(Row row in rows)
{
foreach(Cell cell in row.Cells)
{
Console.WriteLine(cell.OfficeValue); //return the string value of this cell
}
}

[...]

You might expect that this piece of code prints the contents of every cell in the document. You are sadly mistaken. As I found out, it printed everything except string values. That is, floats and ints etc. printed ok, but when a cell contained a string the WriteLine-statement gave an empty string instead. After some fruitless googling and and hour or two of desperate screaming I finally came to the rather embarassing conclusion that the empty string was actually a null string. Then, after some quick IntelliSensing (well, MonoDevelop's version of it anyway) I produced the following.


[...]
foreach(Row row in rows)
{
foreach(Cell cell in row.Cells)
{
string s = cell.OfficeValueType; //get type of value
if(s.Equals("string")
{
Console.WriteLine(cell.Content[0].Node.InnerText);
}
else
{
Console.WriteLine(cell.OfficeValue);
}
}
}
[...]
Why cell.OfficeValue (which is of the type string) doesn't work for strings evades me. But at least there is a quick solution. There are probably others, if so please leave a comment.

Friday, September 11, 2009

New author

I'd like to take this opportunity to present myself. I am manneorama; developer, death metal-connaisseur and all in all pleasant man. Welcome, myself, to volatileint.

Wednesday, March 18, 2009

Creating fdi policy files for HAL

I have been using Gentoo for about 6 years now, and every now and then the system breaks as a result of a large update. This isn't a terrible thing, and usually I just have to scout the http://forums.gentoo.org for a couple of minutes to find the solution to the error, if I can't figure it out myself.

The most recent problem people have been seeing lately (since at least 6 months ago) is the merge from configuring input devices in fdi policy files - as a result of the Xorg-team wanting to move to HAL-based configures - instead of an input section inside the xorg.conf file. This is mainly in the xorg-server 1.5 and onward. If you want to read much more about HAL, more info is located here.

I have had lots of problems getting this to work, and every time I was trying to convert, I grew sick of it and just reverted my changes. But now I stuck to it and came through alive on the other side. As usual, the forums could help me.

First we add a section with server flags to our xorg.conf:

Section "ServerFlags"
Option "AutoAddDevices" "on"
Option "AutoEnableDevices" "on"
Option "AllowEmptyInput" "on"
EndSection

This tells xorg that it is OK that we have removed all input devices in the file. Then we can comment out the devices we have listed (to later remove them for good).

Now we need to convert our input settings to the new fdi config file structure. There are some example files scattered across the web, for example on Ubuntu's wiki. The settings are quite basic for most people, so it makes it easy to write the file.

First off we create our config file in /etc/hal/fdi/policy/ and name it 10-x11-input.fdi. The config in my xorg.conf said:

Section "InputDevice"
Identifier "Keyboard0"
Driver "kbd"
Option "CoreKeyboard"
Option "XkbRules" "xorg"
Option "XkbModel" "pc105"
Option "XkbLayout" "se"
EndSection

Section "InputDevice"
Identifier "Logitech MX510"
Driver "evdev"
Option "Device" "/dev/input/mice"
Option "Protocol" "ExplorerPS/2"
EndSection

This translates to very similar settings in our new 10-X11-input.fdi. I should mention that I also took the opportunity to switch my device handling to "evdev" for both my devices, instead of the xorg handled one.

<?xml version="1.0" encoding="utf-8"?>
<deviceinfo version="0.2">
<device>
<match key="info.capabilities" contains="input.keyboard">
<merge key="input.x11_options.XkbRules" type="string">evdev</merge>
<merge key="input.x11_options.XkbModel" type="string">evdev</merge>
<merge key="input.x11_options.XkbLayout" type="string">se</merge>
<!--<merge key="input.x11_options.XkbLayout" type="string">altwin:menu</merge>-->
</match>
</device>
<device>
<match key="info.capabilities" contains="input.mouse">
<merge key="input.x11_driver" type="string">evdev</merge>
<!--<merge key="input.x11_options.RelHWHEELOptions" type="string">invert</merge>-->
<merge key="input.x11_options.Emulate3Buttons" type="string">false</merge>
</match>
</device>
</deviceinfo>

After creating the file, we restart our hal daemon:
root@skare$ /etc/init.d/hald restart

and unplug/plug in our device. If we monitor devices with hal, it will print out a new device.

root@skare$ lshal -m

Start monitoring devicelist:
-------------------------------------------------
22:52:00.340: usb_device_46d_c01d_noserial_if0_logicaldev_input removed
22:52:00.344: usb_device_46d_c01d_noserial_if0 removed
22:52:00.345: usb_device_46d_c01d_noserial removed
22:52:09.263: usb_device_46d_c01d_noserial added
22:52:09.263: usb_device_46d_c01d_noserial_if0 added
22:52:09.307: usb_device_46d_c01d_noserial_if0_logicaldev_input added

We can see that I first unpluggen my device, and then plugged it in again. To check what devices we have as mouse and keyboard, we run two other commands:

root@skare$ hal-find-by-capability --capability input.mouse |xargs hal-device
udi = '/org/freedesktop/Hal/devices/usb_device_46d_c01d_noserial_if0_logicaldev_input'
info.subsystem = 'input' (string)
info.product = 'Logitech USB-PS/2 Optical Mouse' (string)
linux.device_file = '/dev/input/event3' (string)
info.category = 'input' (string)
info.udi = '/org/freedesktop/Hal/devices/usb_device_46d_c01d_noserial_if0_logicaldev_input' (string)
input.originating_device = '/org/freedesktop/Hal/devices/usb_device_46d_c01d_noserial_if0' (string)
input.device = '/dev/input/event3' (string)
input.x11_driver = 'evdev' (string)
info.capabilities = { 'input', 'input.mouse' } (string list)
input.product = 'Logitech USB-PS/2 Optical Mouse' (string)
input.x11_options.Emulate3Buttons = 'false' (string)
linux.sysfs_path = '/sys/devices/pci0000:00/0000:00:1a.1/usb4/4-2/4-2:1.0/input/input3/event3' (string)
info.parent = '/org/freedesktop/Hal/devices/usb_device_46d_c01d_noserial_if0' (string)
linux.hotplug_type = 2 (0x2) (int)
linux.subsystem = 'input' (string)

root@skare$ hal-find-by-capability --capability input.keys |xargs hal-device
udi = '/org/freedesktop/Hal/devices/platform_i8042_i8042_KBD_port_logicaldev_input'
info.subsystem = 'input' (string)
input.xkb.layout = 'us' (string)
info.product = 'AT Translated Set 2 keyboard' (string)
linux.device_file = '/dev/input/event2' (string)
input.xkb.variant = '' (string)
info.category = 'input' (string)
info.udi = '/org/freedesktop/Hal/devices/platform_i8042_i8042_KBD_port_logicaldev_input' (string)
input.x11_options.XkbRules = 'evdev' (string)
input.originating_device = '/org/freedesktop/Hal/devices/platform_i8042_i8042_KBD_port' (string)
input.x11_options.XkbModel = 'evdev' (string)
input.device = '/dev/input/event2' (string)
input.x11_driver = 'evdev' (string)
input.x11_options.XkbLayout = 'se' (string)
info.capabilities = { 'input', 'input.keyboard', 'input.keypad', 'input.keys', 'button' } (string list)
input.product = 'AT Translated Set 2 keyboard' (string)
linux.sysfs_path = '/sys/devices/platform/i8042/serio0/input/input2/event2' (string)
info.parent = '/org/freedesktop/Hal/devices/platform_i8042_i8042_KBD_port' (string)
info.addons.singleton = { 'hald-addon-input' } (string list)
linux.hotplug_type = 2 (0x2) (int)
input.xkb.rules = 'base' (string)
linux.subsystem = 'input' (string)
input.xkb.model = 'evdev' (string)

Here we can see if our policy file actually did its job, i.e. loaded the right drivers for the devices. It also shows the settings, so we can double check those. The /var/log/Xorg.0.log also displays the changes:

root@skare$ tail -n 20 /var/log/Xorg.0.log
(EE) Logitech USB-PS/2 Optical Mouse: Read error: No such device
(II) config/hal: removing device Logitech USB-PS/2 Optical Mouse
(II) Logitech USB-PS/2 Optical Mouse: Close
(II) UnloadModule: "evdev"
(II) config/hal: Adding input device Logitech USB-PS/2 Optical Mouse
(**) Logitech USB-PS/2 Optical Mouse: always reports core events
(**) Logitech USB-PS/2 Optical Mouse: Device: "/dev/input/event3"
(II) Logitech USB-PS/2 Optical Mouse: Found 8 mouse buttons
(II) Logitech USB-PS/2 Optical Mouse: Found x and y relative axes
(II) Logitech USB-PS/2 Optical Mouse: Found scroll wheel(s)
(II) Logitech USB-PS/2 Optical Mouse: Configuring as mouse
(**) Option "Emulate3Buttons" "false"
(II) Logitech USB-PS/2 Optical Mouse: Forcing middle mouse button emulation off.
(**) Logitech USB-PS/2 Optical Mouse: YAxisMapping: buttons 4 and 5
(**) Logitech USB-PS/2 Optical Mouse: EmulateWheelButton: 4, EmulateWheelInertia: 10, EmulateWheelTimeout: 200
(II) XINPUT: Adding extended input device "Logitech USB-PS/2 Optical Mouse" (type: MOUSE)

One positive thing with this new HAL stuff is that we don't have to restart X when we add a new device, or want to change the settings of the ones we have. Just restart the hald daemon and it re-reads the policy files, and unplug/plug in the device again, and the new settings should be enabled. Good luck!

Thursday, March 12, 2009

Mozilla closing in on Firefox 3.1 beta 3

It seems like the release of Mozilla Firefox 3.1 Beta 3 is closing in. The Mozilla Quality Assurance team is organizing a test round "for the rollout of" the beta release today, and states that to test it, the 3.1 users just need to klick "Check for Updates" in the Help menu. I have tried it, but the update doesn't seem to be out yet. I guess it will arrive later today.

They announced earlier this month that they would probably have yet another Beta candidate before doing the actual release, as well as renaming it Firefox 3.5 as a result of the long wait. The beta3 was originally planned for release months ago, and the reason for the delay was a couple of serious bugs, where the most prominent one was in the TraceMonkey javascript engine. This is the same engine that has been hailed as one of the reasons why the new Firefox is lightning fast, so let's hope they iron out the last creases! I have been running the Beta2 as my main browser for several weeks now, and have not experienced any problems at all, so I am looking forward to the Beta3 to see if there are any noticable changes.

Some of the new stuff in Firefox 3.5 except TraceMonkey and great speed improvements are "Private browsing mode", support for embedded video and audio (without Flash etc), improved rendering (improvements to the Gecko layout engine, including speculative parsing for faster content rendering) and of course lots of bug fixes.

Friday, February 27, 2009

Using regular expressions in Vim

Today I was faced with the task of replacing lots of words in a script file. The problem was that I could not simply replace them by doing a "search-and-replace" (:%s/old_word/new_word/g) because they should only be replaced on some of the lines. For example, the script says

OUTSTANDING
COMMAND Record SetPriority priorityRecording_INI
COMMAND Play SetPriority priorityRecording_INI
COMMAND Record SetDestinationSampleRateL Record_A_006_INI
COMMAND Record SetDestinationBitRateL Record_A_005_INI

My task was to replate the priorityRecording_INI with priorityPlayback_INI for all the lines which had the COMMAND Play on them. While I in this case could have done it by hand, a regular expression is perfect for this task!

First we need to use the "search-and-replace" command like we stated above:

:%s/exp1/exp2/g

This is in more detail the command s (find and substitute), extended with "%" which tells it to look through the entire file. "exp1" is then replaced by "exp2", and "/g" in the end tells it to do it for all occurrences for every line.

To boil the problem down, we first focus on what tells the line apart. We use "Play" as the command to tell the difference. We then search for "Play", and don't care about the rest, at least up until the thing we want to replace. This means we search for

/Play.*priority

since priority is the last word we want to keep. The problem here is that we have two instances of the word priority, but we can tell them apart by the fact that one of them has a TAB in front of it (it could be spaces as well, but I won't go into that flame war). To enter a TAB character in Vim, we press ctrl+v TAB, which looks like ^I.

The command

/Play.*^Ipriority

highlights everything from Play to end of priority. We could also do a case sensitive search, but I have that disabled as default, so this was a smoother solution.

To keep certain parts of our search, and replace others, we use \(exp3\) formatting. This means that the substitution command won't replace that, but instead move it to a new position in the substitution expression. Alll wildcards or search expressions outside of the \(\) will be replaced. The new position for the "exp3" (or several) is noted by using \1 (\2, \3, ..., etc etc) in the order they have been declared in the search expression.

So first we define the "/Play.*^Ipriority" as the first thing we want to keep, and since we also want to keep the "_INI" in the end, we define that as \2. The end result is rather confusing, but it does the job!

:%s/\(Play.*^Ipriority\).*\(_INI\)/\1Playback\2/g

And the result is exactly what I wanted!

COMMAND Record SetPriority priorityRecording_INI
COMMAND Record SetPriority priorityPlayback_INI
COMMAND Record SetDestinationSampleRateL Record_A_006_INI
COMMAND Record SetDestinationBitRateL Record_A_005_INI

Imagine doing this for a file with 2000 rows, allowing us to automate boring manual labour. You can read more about regular expressions in Vim's help page (:help :s or :help pattern) or http://www.addedbytes.com/cheat-sheets/regular-expressions-cheat-sheet/, a great cheat-sheet.

Sunday, January 25, 2009

Configuring the Ralink RT2860 card

This is how I set up my Zepto Znote 6024W (using the Ralink RT2860 wifi card) with my D-Link DI-524 router. It is a really crappy router, and I have had lots of problems with it, but today I seem to have found a solution to my problems. It still seems to disconnect a lot, but if I dhclient it again, it usually reconnects. This is how I set it up.

Go to Ralink's website at http://web.ralinktech.com/ralink/Home/Support/Linux.html. The latest drivers when writing this is 1.8.0. Download them and untar them somewhere.

I really recommend you to read the README_STA that comes with the drivers. It tells you how to configure the drivers for your setup, even if they do leave a lot to wish for. The information that I'm sharing here is a mix between that file, as well as the info that can be found in the manpages for wpa_supplicant, wpa_cli, wpa_supplicant.conf and ifup.

First we change the config for the build so the card is being controlled by wext and network manager. Open the file os/linux/config.mk and set:
'HAS_WPA_SUPPLICANT=y' and "HAS_NATIVE_WPA_SUPPLICANT_SUPPORT=y'.

Save the file, we now move on to configuring the more detailed stuff. The settings for the drivers are entered into the RT2860STA.dat file in the base directory of the drivers. This is then copied to /etc/Wireless/RT2860STA/ when we compile the source and run 'make install'.
Edit the settings that interests you. I don't see why we need to enter our psk (preshared key) both here and in the wpa settings (see further down), but I haven't had the guts to remove my settings. The things I changed are:

#The word of "Default" must not be removed
Default
CountryRegion=5
CountryRegionABand=7
CountryCode=
ChannelGeography=1
SSID=your_ssid
NetworkType=Infra
WirelessMode=9
Channel=0
AuthMode=WPA2
EncrypType=TKIP
WPAPSK=cf3e61b119de1e8bceb45edc60c5b7aa00b240c58bee8ba83cc9448761300cf3

To find the encrypted version of your password, use wpa_passphrase. The output should look something like this:
root@george:# wpa_passphrase your_ssid super_duper_password
network={
ssid="your_ssid"
#psk="super_duper_password"
psk=cf3e61b119de1e8bceb45edc60c5b7aa00b240c58bee8ba83cc9448761300cf3
}

We will do the same again later, but for setting up the wpa_supplicant. Now let's build the driver and load it into the kernel.

root@george:# make all && make install && modprobe rt2860sta

Enable, build and load the kernel modules that allows for encryption (TKIP, AES etc). If you are running Ubuntu, these will be compiled as modules already (which means that we don't have to recompile anything), otherwise enable them as modules in the kernel and compile and install them.

load kernel modules for encryption
root@george:# modprobe ieee80211_crypt_tkip \
ieee80211_crypt_ccmp \
ieee80211_crypt \
aes_x86_64 \
aes_generic

Now it's time to set up the configuration for wpa_supplicant, so we can connect to our encrypted access point. Since you've already set a password on the AP, let us try to use it.

Encrypt password using wpa_passphrase
root@george:# wpa_passphrase your_ssid super_duper_password >> /etc/wpa_supplicant/wpa_supplicant.conf

Edit the created file so we can add some settings. I am running WPA2 with both AES and TKIP encryption, so I'll let the driver use both.

root@george:# vim /etc/wpa_supplicant/wpa_supplicant.conf
network={
ssid="your_ssid"
scan_ssid=0
key_mgmt=WPA-PSK
pairwise=CCMP TKIP
group=CCMP TKIP
#psk="super_duper_password"
psk=cf3e61b119de1e8bceb45edc60c5b7aa00b240c58bee8ba83cc9448761300cf3

}

The file should now look something like above. We now start wpa_supplicant with the arguments that it should use the wext (generic wireless driver for Linux) driver, with our config file. It will also set up the interface as ra0. We also tell it to be verbose, so it is easier to spot errors in our configuration.

root@george: #wpa_supplicant -Dwext -ira0 -c /etc/wpa_supplicant/wpa_supplicant.conf -d

And to receive the correct dhcp info from our access point we run dhclient on it.

root@george: # dhclient ra0

We can check the interface has been started now with ifconfig and iwconfig:

root@George:# ifconfig ra0
ra0 Link encap:Ethernet HWaddr 00:15:af:b7:93:xx
inet addr:192.168.0.116 Bcast:192.168.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:44812 errors:0 dropped:0 overruns:0 frame:0
TX packets:9096 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:4619023 (4.6 MB) TX bytes:369091 (369.0 KB)
Interrupt:18

root@George:# iwconfig ra0
ra0 RT2860 Wireless ESSID:"your_ssid" Nickname:"RT2860STA"
Mode:Managed Frequency=2.452 GHz Access Point: 00:1C:F0:88:A4:02
Bit Rate=54 Mb/s
RTS thr:off Fragment thr:off
Encryption key:50FF-9C3C-6A3B-1EBB-6D50-AF11-8F74-XXXX
Link Quality=100/100 Signal level:-39 dBm Noise level:-81 dBm
Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0
Tx excessive retries:0 Invalid misc:0 Missed beacon:0

My computer keeps disconnecting, and I'm unsure about what to do about that. This does at least get the network up, so I could post this article! First I had lots of settings for the encryption in my /etc/network/interfaces file, but I moved all these to the wpa_supplicant.conf file, which should mean that we can keep a very clean interfaces file. We do need to make it run wpa_supplicant every time we want the interface up though, so we'll add that to the config. Mine now looks like:

root@George:# cat /etc/network/interfaces
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp

auto ra0
iface ra0 inet dhcp
pre-up /sbin/wpa_supplicant -Dwext -ira0 -c /etc/wpa_supplicant/wpa_supplicant.conf -B

Now it should get the interface up and running every time you boot the computer. Good luck!