Wednesday, November 3, 2010

Xserver : black screen delay when rotating using xrandr

When rotating X's display orientation on the fly 
using xrandr -o left|normal|inverted|right
command, following transitions are performed with 
black screen delay effect i.e. the display becomes 
black for a moment then displays rotated screen.

The following transitions are performed with a delay 
a black screen is shown with ~1 second duration, then 
rotated screen appears
normal > right|left 
inverted > normal|left|right
left > normal|inverted
right > normal|inverted

The following transitions are performed smoothly with 
no delays, i.e. "immediately"
normal > inverted
left > right
right > left
Reason of the problem
Most time consuming code while rotating the display orientation
with xrandr is 
ret = crtc->funcs->set_mode_major(crtc, mode, rotation, x, y);  
intel video drivers function call. This call is located in 
hw/xfree86/modes/xf86Crtc.c file in xf86CrtcSetModeTransform 
function.

HW Platforms:
Graphics : Intel GMA 3150 graphics
Graphics : Intel Graphics Media Accelerator (GMA) 950
Corresponding bug is opened in Xservers's intel video drivers 
project
https://bugs.freedesktop.org/show_bug.cgi?id=31313

Thursday, October 14, 2010

How to checkout a tag from git

For the people from subversion's world it may be difficult to switch to understandings of git.

Firstly git clone copies "everything" (i.e. commit history) to local machine.


Secondly in git the tag is simply a label to commit.
For listing tags use
      git tag -l
Then you can switch to tag using following command
      git checkout -b

Saturday, October 2, 2010

How to filter accelerometer data from noise


Problem statement

Suppose we have accelerometer with 3 axis and the raw data is quite noisy. Below is the plots of noisy data (right) and (desired/filtered) data. So we need to write a filter using which we’ll get the required signal.


Left image is filtered from sudden bumps and scaled, so if you notice the left signal is the middle part of right plot from approximately from -240 to 240.

Algorithm
For filtration quite simple algorithm was used. As the accelerometer data on appropriate axes X, Y, Z is the projection of gravity so the square root of their square sum will be the module of gravity vector with some error +/- 100 points.

Code
Below is the bash script which was used for filtration above signal.

#!/bin/bash

function tosigned() {
    if [ $1 -gt 32767 ]; then
            echo  $[$1-65536]
    else
            echo $1
    fi
}

function abs() {
        if [ $1 -lt 0 ] ; then
                echo `expr 0 - $1`
        else
                echo $1
        fi
}

sleep 1 # Wait for a second then send accelerometer data

tx=0
ty=0
tz=0

while :
do
    x= "datax from accel"
    y=
"datay from accel"
    z= "dataz from accel"
    x=$(tosigned $x)
    y=$(tosigned $y)
    z=$(tosigned $z)
    if [ $(abs $x) -lt 1000 -a $(abs $y) -lt 1000 -a $(abs $z) -lt 1000 ]; then
        g=$[$x * $x  + $y * $y + $z * $z]
        if [ $g -gt 40000 -a $g -lt 70000 ]; then
            max=0
            if [ $x -gt $max ]; then
            max=$x
            fi
            if [ $y -gt $max ]; then
            max=$y
            fi
            if [ $z -gt $max ]; then
            max=$z
            fi
            if [ $tx -eq 0 -a $ty -eq 0 -a $tz -eq 0 ]; then
                tx=$x   
                ty=$y   
                tz=$z   
                continue
            fi
            if [ $(abs $[$x - $tx]) -gt 200 -o $(abs $[$y - $ty]) -gt 200 -o $(abs $[$z - $tz]) -gt 200 ]; then
                tx=$x   
                ty=$y   
                tz=$z   
                continue
            fi
            echo "$x $y $z"
            break
            #sleep 0.01
        fi
    fi
done

Tuesday, September 7, 2010

How to change the layout of matchbox-keyboard to another language

Unfortunately currently matchbox keyboard supports only on keyboard layout, this is written in README file of package.

Problem statement : We need to load English keyboard layout if the LANG environment variable is set to for example to en_US.UTF-8 and German keyboard layout if LANG is set to de_DE.UTF-8 .

Solution : That to do this we have to do some little coding in matchbox-keyboard.c file.

Change following code

  kb->selected_layout
      = (MBKeyboardLayout *)util_list_get_nth_data(kb->layouts, 0);

to
  char* lang = getenv("LANG");
  if (!strcmp(lang, "en_US.UTF-8")) {
    kb->selected_layout
      = (MBKeyboardLayout *)util_list_get_nth_data(kb->layouts, 1);
  } else {
    kb->selected_layout
      = (MBKeyboardLayout *)util_list_get_nth_data(kb->layouts, 0);
  }
Also edit the keyboard.xml file to contain English and German keyboard layouts in my case. Approximately with this format.



...



 
    
   ...
   

...
 

....

Sorry for not so detailed instructions ... time is killing ...

Adios !

Monday, August 23, 2010

How to simulate CAPS LOCK press in Xlib

#include "stdio.h"
#include "X11/Xlib.h"
#include "X11/keysym.h"
#include "time.h"

Display *display=NULL;
unsigned int keycode;

int main ()
{
display = XOpenDisplay(NULL);
keycode = XKeysymToKeycode(display, XK_Caps_Lock);

printf ("\npressed\n");
XTestFakeKeyEvent(display, keycode, True, CurrentTime);
XFlush(display);
printf ("\nreleased\n");
XTestFakeKeyEvent(display, keycode, False, CurrentTime);
XFlush(display);
sleep(3);

/* type something here */

printf ("\npressed\n");
XTestFakeKeyEvent(display, keycode, True, CurrentTime);
XFlush(display);
printf ("\nreleased\n");
XTestFakeKeyEvent(display, keycode, False, CurrentTime);
XFlush(display);
sleep(3);

return 0;
}


Replace " " -s with < > for header files.
and compile using
gcc main.c -lX11 -lXtst

Retrieving CAPS LOCK info using Xlib

#include
#include

int main() {
Display * d = XOpenDisplay((char*)0);
bool caps_state = false;
if (d) {
unsigned n;
XkbGetIndicatorState(d, XkbUseCoreKbd, &n);
caps_state = (n & 0x01) == 1;
}
std::cout << "caps_state = " << std::endl;
return 0;
}

Compile above code using

g++ main.cpp -L/usr/X11R6/lib -lX11


Thursday, August 19, 2010

DeviceKit

DeviceKit is a modular hardware abstraction layer designed for use in Linux systems that is designed to simplify device management and replace the current monolithic Linux HAL. DeviceKit includes the ability to enumerate system devices and send notifications when hardware is added or removed from the computer system.

DBus Interfaces provided by DeviceKit are following

  • org.freedesktop.DeviceKit.Power - Power interface
  • org.freedesktop.DeviceKit.Power.Device - Device interface
  • org.freedesktop.DeviceKit.Power.QoS- QoS interface

Samples using python

Enumerate all power objects on the system

Running ths script in virtual machine will show only one device AC. Running on real device will show other devices as well e.g. battery etc.

import dbus
bus = dbus.SystemBus()
proxy = bus.get_object('org.freedesktop.DeviceKit.Power', '/org/freedesktop/DeviceKit/Power')
iface = dbus.Interface(proxy, 'org.freedesktop.DeviceKit.Power')
lst = iface.EnumerateDevices()
print str(lst)

Retrieving battery state

State is numberical value with following assignments

  • 0: Unknown
  • 1: Charging
  • 2: Discharging
  • 3: Empty
  • 4: Fully charged
  • 5: Pending charge
  • 6: Pending discharge
    import dbus
    def printPropValue(prop):
    bus = dbus.SystemBus()
    proxy = bus.get_object('org.freedesktop.DeviceKit.Power', '/org/freedesktop/DeviceKit/Power')
    iface = dbus.Interface(proxy, 'org.freedesktop.DeviceKit.Power')
    lst = iface.EnumerateDevices()
    id = len(lst) - 1
    proxy1 = bus.get_object('org.freedesktop.DeviceKit.Power', lst\[id\])
    iface1 = dbus.Interface(proxy1, 'org.freedesktop.DBus.Properties')
    val = iface1.Get(lst\[id\], prop)
    print prop + '\t' + str(val)
    printPropValue('State')

Getting Battery Percentage

That to get the battery percentage we need to read "Percentage" parameter of "org.freedesktop.DeviceKit.Power" interface.

Use the same function above passing "Percentage" argument

printPropValue('Percentage')

Help

http://people.freedesktop.org/~hughsient/DeviceKit-power/Device.html

http://people.freedesktop.org/~hughsient/DeviceKit-power/ref-dbus.html

http://hal.freedesktop.org/docs/DeviceKit/