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.