donderdag 6 september 2012

File type associations with xdg-open and xdg-mime (for openbox) example


Just so I don't forget, here is how to do it - it's very simple, really:

xdg-mime default xarchiver.desktop application/x-compressed-tar

Why ?


When I clicked on a .tar.gz file in chromium, my desktop environment would hang.

This was caused the fact that chromium calls xdg-open to know which program to use to open the .tar.gz file. xdg-open used "xdg-mime query default application/x-compressed-tar" to figure out what application to use (which was none, because I hadn't configured any default application).

After that, xdg-open doesn't stop. If /etc/debian-version exists, it additionally uses run-mailcap to figure out which program to use. And mailcap was configured to run "less" somewhere down the line, which stops and apparently, stops it's parent processes for some unknown reason when run in this configuration.

So I deleted /etc/debian-version because I don't see the point of having it. We'll see if something what breaks.

How ?

xdg-mime will create the file ~/.local/share/applications/mimeapps.list which then contains:

[Default Applications]
application/x-compressed-tar=xarchiver.desktop

zondag 17 juni 2012

Rudimentary PDF to LaTeX conversion in Linux

I finally got around to trying a rudimentary PDF to LaTeX conversion in Linux.

"It's like turning a hamburger into a cow" :-)

Usage:

./pdftolatex.sh "filename.pdf"

Example output:

# pdftolatex.sh test.pdf 

PDF to LaTeX conversion script
Copyleft 2012 (c) Tom Van Braeckel <tomvanbraeckel@gmail.com>

WARNING: this is a rudimentary first stab. Proceed with caution.

Checking if all dependencies are found...
/usr/bin/pdftohtml
Dependency pdftohtml found.
/usr/bin/gnuhtml2latex
Dependency gnuhtml2latex found.
/usr/bin/pdflatex
Dependency pdflatex found.

Converting test.pdf to test.pdfs.html
Page-1
Page-2
Page-3
Page-4
Page-5
Page-6
Page-7
Page-8
Page-9
Page-10
Page-11

Fixing up test.pdfs.html to test.pdf_fixedup.html...

Readying test.pdf_fixedup.html for tex conversion in test.pdf_fixedup_ready_for_tex_conversion.html

Converting test.pdf_fixedup_ready_for_tex_conversion.html to test.pdf_frompdf.tex
The resulting file is in test.pdf_frompdf.tex

Fixing up test.pdf_frompdf.tex to test.pdf_frompdf_fixedup.tex

Converting test.pdf_frompdf_fixedup.tex to test.pdf_frompdf_fixedup.pdf for inspection...

Opening test.pdf_frompdf_fixedup.pdf with Evince - you can try another PDF viewer if you like...

Script source code:

#!/bin/sh
file="$1"
dependencies="pdftohtml gnuhtml2latex pdflatex"
echo "PDF to LaTeX conversion script"
echo "Copyleft 2012 (c) Tom Van Braeckel <tomvanbraeckel@gmail.com>"
echo
echo "WARNING: this is a rudimentary first stab. Proceed with caution."
echo
if [ -z "$file" ]; then
echo "Usage: $0 <pdf file>"
echo "The resulting .tex file will be stored somewhere here."
exit 1
fi
echo

echo "Checking if all dependencies are found..."
for dependency in $dependencies; do
which $dependency
if [ $? -ne 0 ]; then
echo "Dependency $dependency not found, install it using:"
echo "sudo apt-get install $dependency"
exit 1
else
echo "Dependency $dependency found."
fi
done
echo

echo "Converting $file to ${file}s.html"
pdftohtml -nomerge "$file" "$file".html
echo

echo "Fixing up ${file}s.html to ${file}_fixedup.html..."
# This nasty br in a b causes problems later on
sed "s,<br/></b>,</b><br/>,g" "${file}s.html" > "${file}_fixedup.html"
# ending with bold text menas it is the end of a title and can be on a newline
sed -i "s,</b><br/>\$,</b><br/><br/>,g" "${file}_fixedup.html"
# starting with bold text means it is the start of a title so can be on a new line
sed -i "s,^<b>,<br/><b>,g" "${file}_fixedup.html"
# spaces ?
sed -i "s,\&#160;, ,g" "${file}_fixedup.html"
# there is no use in a space before a newline, and it causes a bogus indent when converting to .tex later on
sed -i "s, <br/>,<br/>,g" "${file}_fixedup.html"
# encoding, although gnuhtml2latex ignores this
sed -i "s,<HEAD>,<HEAD><meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />," "${file}_fixedup.html"
echo

echo "Readying ${file}_fixedup.html for tex conversion in ${file}_fixedup_ready_for_tex_conversion.html"
cp "${file}_fixedup.html" "${file}_fixedup_ready_for_tex_conversion.html"
# br is ignored and should be replaced by a newline
sed -i "s,<br/>,<p></p>,g" "${file}_fixedup_ready_for_tex_conversion.html"
# Remove bogus links - this fixes the empty } problem
sed -i "s/<A name=[0-9]\+><\/a>//g" "${file}_fixedup_ready_for_tex_conversion.html"
echo

echo "Converting ${file}_fixedup_ready_for_tex_conversion.html to ${file}_frompdf.tex"
# -c = table of contents
# -s = write to standard out
# -p  Break page after title / table of contents
# -H  use hyperref package to process anchors
# -g images
# -n  Use numbered sections
gnuhtml2latex -c -s -p -H -n "${file}_fixedup_ready_for_tex_conversion.html" > "$file"_frompdf.tex
echo "The resulting file is in ${file}_frompdf.tex"
echo

echo "Fixing up ${file}_frompdf.tex to ${file}_frompdf_fixedup.tex"
sed -i 's/\\par/\\newline/g' "${file}_frompdf.tex"
( cat header.inc ; tail -n +7 "${file}_frompdf.tex" ) > "${file}_frompdf_fixedup.tex"
echo

echo "Converting ${file}_frompdf_fixedup.tex to ${file}_frompdf_fixedup.pdf for inspection..."
pdflatex -interaction nonstopmode "${file}_frompdf_fixedup.tex" > tex_to_pdf_errors_and_warnings.txt
echo

echo "Opening ${file}_frompdf_fixedup.pdf with Evince - you can try another PDF viewer if you like..."
evince "$file"_frompdf_fixedup.pdf

The script uses one extra file, header.inc, which contains customizations:

\documentclass[a4paper,11pt,oneside]{article}
\usepackage{a4wide}                     % Iets meer tekst op een bladzijde
\usepackage[dutch]{babel}               % Voor nederlandstalige hyphenatie (woordsplitsing) en het euro-symbool
\usepackage{amsmath}                    % Uitgebreide wiskundige mogelijkheden
\usepackage{amssymb}                    % Voor speciale symbolen zoals de verzameling Z, R...
\usepackage{url}                        % Om url's te verwerken
\usepackage{graphicx}                   % Om figuren te kunnen verwerken
\usepackage[small,bf,hang]{caption}    % Om de captions wat te verbeteren
\usepackage{xspace}                     % Magische spaties na een commando
\usepackage[utf8]{inputenc}           % Om niet ascii karakters rechtstreeks te kunnen typen
\usepackage{float}                      % Om nieuwe float environments aan te maken. Ook optie H!
\usepackage{flafter}                    % Opdat floats niet zouden voorsteken
\usepackage{listings}                   % Voor het weergeven van letterlijke text en codelistings
\usepackage{marvosym}                   % Om het euro symbool te krijgen
\usepackage{eurosym}                   % Om het euro symbool te krijgen
\usepackage{textcomp}                   % Voor onder andere graden celsius
\usepackage{fancyhdr}                   % Voor fancy headers en footers.
\usepackage{graphics}                   % Om figuren te verwerken.
\usepackage[a4paper,plainpages=false]{hyperref}    % Om hyperlinks te hebben in het pdfdocument.
\usepackage[usenames,dvipsnames]{xcolor}

% Definitie algemene macro's
\newcommand{\npar}{\par \vspace{0.2ex }}

\setlength\textheight{9.75in}
\setlength\textwidth{7in}

\topmargin -0.5in 
\headheight 0.0in
\oddsidemargin -.25in 


[Update] You can also try the following method, using abiword, but the method above yields better results, in my opinion:

abiword --to=tex "filename.pdf"

zaterdag 5 mei 2012

Please don't blame the weather

Please don't blame the weather
It’s got nothing to do with the weater.

Blame me and sincerely blame yourself.


Try waking up in the morning and
feeling better than the day before.


Wash off your guilt, get over yourself,
and stop wasting time persuing shit.


Contemplate life’s meaning and
the sadness of existence.

donderdag 1 maart 2012

Keycodes and coffeeshops in the Linux kernel

We are using gpio-keys to configure 2 GPIO's coming from 2 sensors (left and right on our device) as keyboard interrupt lines.

So I had to choose 2 obscure keycodes to map on "proximity meter left" and "proximity meter right"...

Here are the results:

        {
                .gpio           = INT_PROX_L,
                .code           = KEY_COFFEE,
                .desc           = "Proximity Left",
        },
        {
                .gpio           = INT_PROX_R,
                .code           = KEY_SHOP,
                .desc           = "Proximity Right",
        },

These keynames and their order are easy to memorize now, because (since we read from left to right) we know that coffee = left and shop = right :-)

zondag 8 januari 2012

rsync and scp autocompletion

Apparantly, rsync and scp boast tab autocompletion !

When you have configured passwordless SSH access (by copying over your public key) and you use these tools, tab autocompletion works as expected.

For example, typing the following on a bash shell:

rsync localfile.txt exampleserver:/examp<TAB>

will automatically autocomplete the foldername.
In the example's case, it becomes:


rsync localfile.txt exampleserver:/examplefolder/


The same goes for scp, like:


scp localfile.txt exampleserver:/examp<TAB>


becomes


rsync localfile.txt exampleserver:/examplefolder


rsync and scp are able to do this because the passwordless SSH allows them to see what files can be found on the other server without asking for a password.

On my LAN network, it takes about 1 second to autocomplete, which is perfectly acceptable for me. It certainly beats having to open a seperate SSH session and manually making a lising ;-)

Super-sleek shell script as a simple touchscreen controller for Linux

#!/bin/sh

# Beautifully simple shell script to detect screen touches

# I use it to toggle the music player on my FriendlyARM 2440 Mini
# but it can be used for any Linux touch screen


# Copyleft 2011 - t-Omicr0n

while true; do
     head -c 1 /dev/input/event0
     echo "You touched it !"
done &

Hunting advice

Let's say you're a hunter. If a deer takes your gun, shoots itself and then straps itself to the roof of your car..... you have to take it home and eat it ! -- Two and a Half Men

donderdag 15 september 2011

Calculating significance indicates insignificance

Calculating significance indicates insignificance -- Jasper Nuyens, 2011

dinsdag 6 september 2011

Joan Halifax's "Compassion"

Equanimity is a state of mental or emotional stability or composure arising from a deep awareness and acceptance of the present moment.

Great TED talk: Joan Halifax: Compassion and the true meaning of empathy

woensdag 29 juni 2011

How to replace a failed disk of a RAID 5 array with mdadm on Linux

This is easy, once you know how it's done :-) These instructions were made on Ubuntu but they apply to many Linux distributions.

First of all, physically install your new disk and partition it so that it has the same (or a similar) structure as the old one you are replacing.

Then, install mdadm if you haven't already:
sudo apt-get install mdadm

Optional but might be a good idea if you can: reboot to make sure the whole md (multi-disk) software stack is loaded.

Let's check the status of our disks - mdadm should have discovered at least *something* on boot:

evy@evy-server:~$ cat /proc/mdstat
Personalities : [linear] [multipath] [raid0] [raid1] [raid6] [raid5] [raid4] [raid10]
md0 : inactive sdb2[1](S) sdc2[2](S)
3858200832 blocks

So this looks like it created one RAID device (/dev/md0) that consists of /dev/sdb2 and /dev/sdc2. We are need at least 3 disks for our RAID 5 so let's add another one:

evy@evy-server:~$ sudo mdadm --manage /dev/md0 --add /dev/sda3
mdadm: cannot get array info for /dev/md0

Hmm, this doesn't work because the RAID array in /dev/md0 is not active.
Let's activate it then:

evy@evy-server:~$ sudo mdadm --assemble /dev/md0
mdadm: no devices found for /dev/md0

Hmm, mdadm is not aware of the physical disks that /dev/md0 consists of. That's strange, because that information is clearly visible in /proc/mdstat, remember ?

A look at the man page reveals the --scan option, that tells mdadm to scan /proc/mdstat (and others) for RAID device information.

Let's try again:

evy@evy-server:~$ sudo mdadm --assemble /dev/md0 --scan
mdadm: /dev/md0 assembled from 2 drives - not enough to start the array while not clean - consider --force.

Ok, so we knew that - the RAID 5 is missing a disk.
But we need to activate it anyhow, to be able to add the missing disk !

So we force it:

evy@evy-server:~$ sudo mdadm --assemble /dev/md0 --scan --force
mdadm: SET_ARRAY_INFO failed for /dev/md0: Device or resource busy

Hmm, something's keeping our RAID array busy...
Let's just restart that RAID volume to fix that:

evy@evy-server:~$ sudo mdadm --stop /dev/md0
mdadm: stopped /dev/md0

And try again:

evy@evy-server:~$ sudo mdadm --assemble /dev/md0 --scan --force
mdadm: /dev/md0 has been started with 2 drives (out of 3).

Success !
Let's see what happened just now: 

evy@evy-server:~$ cat /proc/mdstat
Personalities : [linear] [multipath] [raid0] [raid1] [raid6] [raid5] [raid4] [raid10]
md0 : active raid5 sdb2[1] sdc2[2]
      3858200832 blocks level 5, 64k chunk, algorithm 2 [3/2] [_UU]

Aha, there is movement !
The RAID 5 array is online, and the [_UU] flags show us that one disk is missing.

Here's some more info that demonstrates this nicely:


evy@evy-server:~$ sudo mdadm --detail  /dev/md0
/dev/md0:
        Version : 00.90
  Creation Time : Mon Feb  7 18:27:14 2011
     Raid Level : raid5
     Array Size : 3858200832 (3679.47 GiB 3950.80 GB)
  Used Dev Size : 1929100416 (1839.73 GiB 1975.40 GB)
   Raid Devices : 3
  Total Devices : 2
Preferred Minor : 0
    Persistence : Superblock is persistent

    Update Time : Mon Apr 25 13:04:09 2011
          State : clean, degraded
 Active Devices : 2
Working Devices : 2
 Failed Devices : 0
  Spare Devices : 0

         Layout : left-symmetric
     Chunk Size : 64K

           UUID : 90b591c9:2fd9536f:86e10ba8:fe4e4a37
         Events : 0.172

    Number   Major   Minor   RaidDevice State
       0       0        0        0      removed
       1       8       18        1      active sync   /dev/sdb2
       2       8       34        2      active sync   /dev/sdc2

So let's try to add that missing disk, which had been our intent all along.


evy@evy-server:~$ sudo mdadm --manage /dev/md0 --add /dev/sda3
mdadm: added /dev/sda3

And this works.
Check it:

evy@evy-server:~$ sudo mdadm --detail /dev/md0
/dev/md0:
Version : 00.90
Creation Time : Mon Feb 7 18:27:14 2011
Raid Level : raid5
Array Size : 3858200832 (3679.47 GiB 3950.80 GB)
Used Dev Size : 1929100416 (1839.73 GiB 1975.40 GB)
Raid Devices : 3
Total Devices : 3
Preferred Minor : 0
Persistence : Superblock is persistent

Update Time : Wed Jun 29 23:39:54 2011
State : clean, degraded, recovering
Active Devices : 2
Working Devices : 3
Failed Devices : 0
Spare Devices : 1

Layout : left-symmetric
Chunk Size : 64K

Rebuild Status : 0% complete

UUID : 90b591c9:2fd9536f:86e10ba8:fe4e4a37
Events : 0.176

Number Major Minor RaidDevice State
3 8 3 0 spare rebuilding /dev/sda3
1 8 18 1 active sync /dev/sdb2
2 8 34 2 active sync /dev/sdc2

The RAID has already started rebuilding!

We can watch the progress like this:

evy@evy-server:~$ cat /proc/mdstat
Personalities : [linear] [multipath] [raid0] [raid1] [raid6] [raid5] [raid4] [raid10]
md0 : active raid5 sda3[3] sdb2[1] sdc2[2]
3858200832 blocks level 5, 64k chunk, algorithm 2 [3/2] [_UU]
[>....................] recovery = 0.0% (673180/1929100416) finish=429.6min speed=74797K/sec

Aaaaaaah, there's no greater time to go to bed than when your RAID 5 is recovering :-)

dinsdag 28 juni 2011

Reverse engineered Logitech C110 color changing driver

I reverse engineered the windows driver of my Logitech C110 keyboard and remade it for Linux (with libusb) so that we can now change the color of the led lights in any shade between red and blue.

Attached is a proof of concept that I keep running all the time. It slowly, gradually makes the backlight color change from blue to red and back.

This open source Linux driver outperforms the proprietary windows driver because it enables continuously changing the backlight color in addition to setting just one, fixed color.

You can find it here: http://code.google.com/p/logitech-keyboard-change-color/downloads/list

The most interesting line of code (MILOC) is this one:
int written = usb_control_msg(handle, CONTROLFLAGS, 0x00000009, 0x00000307, 0x00000000, buffer, 0x00000005, 5000);

where:  
  • handle is a pointer to the (properly initialized) USB device 046d:c22b
  • #define CONTROLFLAGS 0x21 hold the RequestTypeReservedBits (as per the USB specs)
  • unsigned char buffer [] = {0x07, 0x00, 0x00, 0x00, 0xFF}; is sent to the keyboard where:
    • buffer[1] determines the led color (between 0x00 = blue and 0xFF = red)
    • buffer[4] determines the led intensity  (between 0x00 = off and 0xFF = maximal) 

I hope you enjoy this keyboard as much as I do, and I hope Logitech starts shipping more of their products with Linux compatibility !

Or at least throwing some documentation online so we don't have to reverse engineer these things all the time :-)

Enjoy !

woensdag 9 februari 2011

A friend made an image with Acronis in an undocumented? proprietary format that looks like this:
00000000  b4 6e 68 44 ae 2e 9e f2  01 00 00 00 66 63 66 60  |.nhD........fcf`|
00000010  60 70 66 b0 62 88 61 60  04 b2 18 00 4f f6 0e eb  |`pf.b.a`....O...|
00000020  01 13 63 60 60 00 00 5f  d7 36 54 05 93 fe e6 c8  |..c``.._.6T.....|
00000030  a6 c7 c8 c0 b0 41 f0 91  ba 26 90 06 00 2c 3c 71  |.....A...&...,mmI...|
000000c0  f0 e8 0f a7 64 56 ad 15  c5 24 91 25 3e 5e 3e d7  |....dV...$.%>^>.|
000000d0  e1 a4 b1 2b ac 61 6f e6  bb 81 99 92 50 c6 16 e6  |...+.ao.....P...|
000000e0  86 f9 bf 4b f3 07 f9 ad  cc 0f a5 24 82 29 88 ab  |...K.......$.)..|
000000f0  ed 4b 53 9a df 1c bb 24  0d 8e c1 d6 ef 9c 1c 92  |.KS....$........|
00000100  3e 39 2e be f4 4a 4e ce  c9 59 e9 c7 bb 0f 83 bb  |>9...JN..Y......|
00000110  41 58 fb a4 4d 9a 64 29  79 2c f0 cc 24 45 95 d3  |AX..M.d)y,..$E..|
00000120  9e 24 da 3c 7d 2e c3 48  fc da 4c 54 be 00 e2 cb  |.$.<}..H..LT....|
00000130  f1 d9 59 0e 3e 9d 06 43  bd ee 8c 1a 16 d8 97 9f  |..Y.>..C........|
00000140  44 65 a7 3e ed 9c 5c 94  23 72 14 6c 36 4a 06 7f  |De.>..\.#r.l6J..|
00000150  7c b0 39 24 a7 f4 20 d3  d3 f0 1e 07 1f 7f d1 3f  ||.9$.. ........?|
00000160  4d c0 5c b3 df 2a 68 ce  fb 24 ef 85 ea cc bf 8f  |M.\..*h..$......|
00000170  56 f1 5d 9d 59 63 51 85  fd b5 3a 4b 13 26 ce d4  |V.].YcQ...:K.&..|
00000180  b6 45 57 b8 07 95 1c 46  4d 3f 60 9c 82 e6 47 6d  |.EW....FM?`...Gm|
00000190  8d ec 49 0e c3 74 60 71  fd ab 87 d7 17 75 05 ea  |..I..t`q.....u..|
000001a0  0a d4 15 a8 2b 50 57 e0  33 56 a0 6b 92 ff 0f e2  |....+PW.3V.k....|
000001b0  5b 21 54 ca f3 33 66 2a  ca af 24 4f 7a ce c6 25  |[!T..3f*..$Oz..%|
000001c0  a3 7d 22 fd cf 11 33 7d  35 1f 43 0c bf 1b 18 fb  |.}"...3}5.C.....|
000001d0  a3 2a e5 6f 60 b4 25 d2  27 58 de 5b 89 aa 20 c6  |.*.o`.%.'X.[.. .|
000001e0  48 00 77 2d fc 5d 8d 06  97 b1 45 f8 62 89 52 fe  |H.w-.]....E.b.R.|
000001f0  8e c5 1d c2 67 d6 04 d6  b4 1d c3 20 6e cb f2 0f  |....g...... n...|
00000200  71 99 1d 79 36 3f 57 62  4e d8 f3 fc 7f 0c d7 ef  |q..y6?WbN.......|
00000210  80 99 c1 9a 98 db 17 c9  4b c2 77 00 a3 cb e6 05  |........K.w.....|
00000220  6b 74 5c fa 9a 6b 5c f6  00 b7 68 71 9d 3f b7 ac  |kt\..k\...hq.?..|
00000230  e6 a7 06 e4 3a ee 79 f2  b7 3d f7 66 e0 7c e7 23  |....:.y..=.f.|.#|
00000240  e7 9e ac 27 d7 e0 6b 87  e6 e3 f3 9c 4f 8d 07 94  |...'..k.....O...|
00000250  27 f9 a6 1a 3e f3 62 1c  31 a3 67 16 df 79 e3 c0  |'...>.b.1.g..y..|
00000260  28 ff 14 97 d5 53 d4 07  3e db 27 e2 ba 3e 69 07  |(....S..>.'..>i.|
00000270  5e ec 13 f5 25 2e 9f 02  fb 7c 31 ae d7 ec 43 0f  |^...%....|1...C.|
00000280  fa 40 1f eb 18 01 e6 32  78 b8 d6 b1 21 73 66 11  |.@.....2x...!sf.|
00000290  39 9d 88 e3 f7 2e b9 14  60 e7 cc bb 51 c2 fa 0f  |9.......`...Q...|
000002a0  f6 d5 de a9 41 ac b2 6b  4c 1e e3 7f 85 8f f7 6c  |....A..kL......l|
000002b0  8c 5c 17 b8 53 9b 56 a6  4f b4 22 86 5c 87 c1 61  |.\..S.V.O.".\..a|
000002c0  c6 e2 11 fb e3 3b fa 00  be 21 c4 a4 a6 e7 c7 63  |.....;...!.....c|
000002d0  fc 66 e0 91 1f f1 22 d0  b9 d8 1f 97 0e c4 9f 41  |.f...."........A|

Now to convert this to something usable...

The first line is:
00000000  b4 6e 68 44 ae 2e 9e f2  01 00 00 00 66 63 66 60  |.nhD........fcf`|

Which looks like:
b4 6e 68 44 = signature
ae 2e 9e f2 = file size backwards (0xF29E2EAE = 4GB)
01 00 00 00 = file number for split-images or total number of files in archive
66 63 66 60 60 70 66 b0 62 88 61 60 04 = meta data ?
b2 18 00 4f f6 0e eb 01 13 = ?
63 60 60 00 = more meta data
00 5f d7 36 54 05 93 fe e6 c8 = start of raw image data ?

zondag 19 december 2010

Ideal Linux screensaver with a cool effect and little CPU/GPU use

My Gefore 8400 GS graphics card's fan is broken, so my GPU's core temperature is going up to 95°C in normal use. Gaming brings it up to 125°C sometimes, which is the "slowdown threshold" (thanks, Nvidia!) to protect against burning plastic.

So here's my new, ultra-tweaked screensaver command:
wormhole -root -delay 34426 -zspeed 2 -stars 7 -fps

Wormhole is part of the "xscreensaver" package (on my Ubuntu it installs in /usr/lib/xscreensaver/wormhole).

It runs at 28fps at 3% CPU use of my Intel Dual Core 2 something something.
Don't know how much GPU it uses but it stays pretty cool now (about 92°C at the moment).
Pretty good, right ?