Sunday, January 9, 2011

XBMC and NVidia's TwinView

I was looking for a decent media center - mostly to watch movies with an overhead projector connected to a PC. The PC is running Linux and I would like to be able to select a movie, change the volume, pause etc with my Medion (read: cheap, RF, USB connected) remote. One day I stumbled upon XBMC. It turned out to be a decent piece of software. Configuring the remote was easy. But...

I must mention here that my display setup uses the NVidia's TwinView, which serves me well. My main LCD monitor is connected through a D-Sub while the beamer via HDMI. They form a dual-screen setup with the beamer on the right of the LCD.

But there is always a catch... Actually two of them.
  1. XBMC's fullscreen ignores TwinView, so it spans both displays, or the other display is turned off. 
  2. There is video tearing visible while XBMC is running on the beamber, there is no tearing if it runs on the LCD.
The first issue can be fixed by making XBMC use so-called windowed mode. It appears as a window then. Having a window and using wmctrl it is possible to make it look like fullscreen.
The second issue regard configuring the NVdia in a proper way which makes OpenGL sync to the specified display instead of the first one. It can be achieved by exporting two environment variables __GL_SYNC_TO_VBLANK=1 and __GL_SYNC_DISPLAY=name_of_the_display_to_sync_to.

So here comes my magical bash script which launches the XBMC with a proper syncing enabled and shifts it to the beamer making the window fullscreen (mind that the beamer is on the right of the LCD, and the LCD is 1280x1024).

Enjoy.
#!/bin/bash

move_and_fullscreen(){
  NAME='XBMC Media Center'

  while [ -z "`wmctrl -l | grep \"$NAME\"`" ]
  do
      sleep 1
  done

  wmctrl -r "$NAME" -e '0,1280,-1,-1,-1'
  wmctrl -r "$NAME" -b toggle,fullscreen
}

move_and_fullscreen &
__GL_SYNC_TO_VBLANK=1 __GL_SYNC_DISPLAY_DEVICE=DFP-0 SDL_VIDEO_ALLOW_SCREENSAVER=0 exec xbmc

Update: As of XBMC 10.1 I had to add SDL_VIDEO_ALLOW_SCREENSAVER=0 to disable screensaver while running XBMC. The above approach uses the windowed mode of XBMC which enables system screensaver now - it kicks in while watching movies :( (the screensaver is disabled in the fullscreen mode only).

Monday, November 15, 2010

Linux and a newborn.

Have you ever tired to put a newborn to sleep? It's tough. However it seems that linux can do it for you - actually it works pretty good for my son. Just turn the volume up and issue:

while true ; do cat /boot/vmlinuz-2.6.32-25-generic > /dev/dsp ; done

Good luck.

Sunday, September 26, 2010

C, Java, Erlang, Prolog, Haskell: naive banchmark

Here comes a naive benchmark for C, Java, Erlang, Prolog and Haskell. The benchmark is to compute Pythagorean triples. The triple consists of three positive integers a, b, and c, such that a*a + b*b = c*c and additionally a+b+c<=n, where n=500.

Haskell:

f :: Int -> [ (Int, Int, Int) ]
f n = [ (x, y, z) | 
    x <- [ 1 .. n ],
    y <- [ 1 .. n ],
    z <- [ 1 .. n ],
    x + y + z <= n,
    (x * x) + (y * y) == (z * z) ]
Prolog:
pytriangle(N,A,B,C):-
    between(1,N,A),
    between(1,N,B),
    between(1,N,C),
    A + B + C =< N ,
    A*A + B*B =:= C*C.
Erlang:
-module(test).
-export([pythag/1]).

pythag(N) ->
    [ {A,B,C} ||
        A <- lists:seq(1,N),
        B <- lists:seq(1,N),
        C <- lists:seq(1,N),
        A+B+C =< N,
        A*A+B*B =:= C*C
    ].
C:
int main(int argc,char *argv[]){
    int a,b,c,n;
    n=atoi(argv[1]);

    for (a=1; a<=n; a++) {
        for (b=1; b<=n; b++) {
            for (c=1; c<=n; c++) {
                if ((a+b+c <= n) && (a*a + b*b == c*c)){
                    printf("%d,%d,%d;",a,b,c);
                }
            }
        }
    }
    return 0;
}
Java:
public class triangles
{
    public static void main(String [] args)
    {
        int a,b,c,n;

        n=Integer.parseInt(args[0]);
        for (a=1; a<=n; a++) {
            for (b=1; b<=n; b++) {
                for (c=1; c<=n; c++) {
                    if ((a+b+c <= n) && (a*a + b*b == c*c)){
                        System.out.printf("%d,%d,%d;",a,b,c);
                    }
                }
            }
        }
    }
}

Here comes the results (in seconds).

Haskell (GHCi, version 6.12.1): 380
Haskell compiled (GHCi, version 6.12.1): 32
Prolog (SWI Prolog 5.7.4): 197.2
Erlang (R13B03): 59.5
Erlang HiPE (High Performance Erlang), native: 13.2
C (gcc 4.4.3): 0.8
Java (IcedTea6 1.8.1): 2.2 (with output redirected to /dev/null: 1.3)

The winner was obvious from the begining. But it's quite interesting how the declarative languages perform... hmmm. Kudos to Erlang.

By the way, have you noticed that the code is far better readable with the declarative approach?

Sunday, September 5, 2010

Command line might: useful aliases; BASH tips

I'm a command-line-guy I guess. I use a terminal quite often (mostly with BASH), either for file management, or while working on my projects, just to launch proper application, and optionally to make it read some contents from a given file.

I found myself using quite often two particular functions:
  1. searching for a file which name contains a keyword, and
  2. figuring out what files I recently modified in the current directory.
These two functions can be easily written down as:

ls -l | grep -i some_keyword
ls -lt | head

Right, but since I use them quite often and being rather lazy I wouldn't want to type these every time... So here come aliases.
You can define an alias for a command you often type and use it instead.  So I decided to define two aliases: lsg and lst for searching for a file name containing a keyword and displaying the recently modified files respectively.

So if you type at your command prompt:

alias lsg='ls -l | grep -i'
alias lst='ls -lt | head'

From now one you can use lsg and lst freely.

If you want to make the change permanent you could add the above lines to your ~/.bashrc file, so the aliases would be accessible any time you log in.

Oh one more thing... how to use it.
If I want to find out what files I modified recently in the current directory I just type:

$ lst

and it gives me a list of recntly modified files. If I'm looking for a file which name contains a keyword "book" I issue:

$ lsg book

And I get a list of files which names contain "book" (case insensitive). It's quite useful - at least for me.

PS. Dont forget about: cd -
It will bring you up to the directory you were previously in.


Monday, August 30, 2010

Home DVD, non interlaced

This nice tiny DVD camera of mine captures footage which turns to be interlaced... While it is nice while watching on a TV, it turns to be at least annoying to watch on a computer. There are some so called comb (or mouse teeth) artifacts which annoy a lot. Of course many video players can deinterlace video on-the-fly however it turns that they do that with very different results especially concerning overall video quality.

So I poked around and it seems that one of the nicest deintralcers available is compiled into MPlayer. It is called yadif. So it's really easy to use. I fidlled with some settings for a while and ended up with a command as follows:

mencoder source.mpg -of mpeg -mpegopts tsaf:format=mpeg2:muxrate=30000 -noskip -vf yadif -o dest.mpg -oac copy -ovc lavc -lavcopts vcodec=mpeg2video:mbd=1:aspect=16/9:vbitrate=9100

It turns a file called source.mpg into a nice deinterlaced dest.mpg.

Monday, June 7, 2010

Printing and Acrobat Reader

Have you ever had problems with printing a PDF document with the Adobe Acrobat Reader? Have you seen a non-informative message:"The document could not be printed"?
I have... and it was surprising, frustrating and... well you know how it is.... especially when the printout is due yesterday...
I stumbled upon this issue when I was to print some stuff in a print shop. I made a PDF went to the shop and ordered a color laser printout (70 pages). I was quite surprised when the guy behind the counter, told me that there is no way to print this document, since Adobe Acrobat displayed that wierd message. Surprisingly nobody at the shop knew what the message ment. I went back to my computer (yes, yes, yes, a desktop...) took a look, and everything was fine I could see it and print it... mind that I use mainly Evince or xpdf for PDFs. Desperatelly I converted the document into Postscript and back to PDF (glorious pdftops and ps2pdf) and took it back to the print shop. Surprisingly they could print it now - without any complains from the Acrobat.

I made some reasearch to make sure that such an event won't take place again. And it turns out that guys at Adobe don't know what they are doing (and they don't care for their customers either) and guys at the print shop have no idea how to handle the Acrobat. There is a web forum I stumbled upon, which deals with this issue, especially one message. It turns out that the printing problem appears for some PDF files - however it's hard to tell what exactly causes it.

Anyway, if you ecaountered this issue take a look at the top, right corner of the print dialog window. If you see an option like below (Comments and Forms: Document and Markups) the Acrobat presumably won't print your file.
Just change it to Document, and you should be allowed to print your stuff (see below)!
Bottom line. Adobe is worse and worse. The Acrobat is bloated. Users dont't know how to use it. Solution? Don't use it! There is plenty of other PDF browsers avaliable: evince, xpdf... Long live the free software!!!

Tuesday, March 2, 2010

Pausing music when switching users in Ubuntu (Karmic and Lucid)

If you want to have a user, which currently uses the display, to have access to the sound device with all the benefits from pausing upon user switch, you need to uncheck 'Use audio devices' privileges for this user (System->User Settings->Advanced->User Priviledges).

As a matter of fact I unchecked it for all my users. These privileges are very confusing then. I guess their proper interpretation is that if:
- checked: the user locks the audio device as long as he wants, no sound switching upon user switching,
- unchecked: the user which currently uses the display has access to the audio device, sound switching takes place upon user switching.

The problem is more general and it regards permissions to access certain devices (and files in /dev directory). Historically if a user needs access to particular device he is assigned to a group which group-owns the device file. It has worked for ages in the unix world i.e. access to an audio device, cdrom, cd burner etc.

This group based approach has several well known limitations. Now it seems that such an access is controlled by consolekit/policykit/udev, which makes some sense, at least because of the dynamic nature of /dev directory. The gnome-system-tools and especially users-admin seem not to know anything about acl. I'm not sure whether it's good or bad - it depends, espoecially that gnome is cross platform - on other systems /dev might be static or there might be no acl at all.

See  https://bugs.launchpad.net/ubuntu/+source/pulseaudio/+bug/433654 for more details.
And some discussion here: http://ubuntuforums.org/showthread.php?t=1420051

Mind that the sound switching worked fine in previous Ubuntu release (Jaunty).