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/

Wednesday, August 18, 2010

How to extract .rpm package content

Install rpmdevtools package
  • sudo yum install rpmdevtools
Then extract
  • rpm2cpio somefile.rpm | cpio -idmv

Thursday, August 12, 2010

PolicyKit

PolicyKit is an operating system component for controlling system-wide privileges in Unix-like operating systems. It provides an roganized way for non-privileged processes to communicate with privileged ones. In contrast to systems such as sudo, it does not grant root permission to an entire process, but rather allows a finer level of control of centralized system policy.

Files shipped with PolicyKit and 3rd party packages (e.g. under package manager control) typically have comments (such as “DO NOT EDIT THIS FILE, it will be overwritten on update”) telling the system administrator that changes will be overwritten on update.

Configuring PolicyKit

Configuration for the Local Authority is read from files in the /etc/polkit-1/localauthority.conf.d directory.

The Local Authority reads files with .pkla extension from all directories located inside the /etc/polkit-1/localauthority and /var/lib/polkit-1/localauthority directories. By default, the following sub-directories are installed.

10-vendor.d Intended for use by the OS vendor.
20-org.d Intended for the organization deploying the OS.
30-site.d Intended for the site deploying the system.
50-local.d Intended for local usage.
90-mandatory.d Intended for the organization deploying the OS.

New direcrtories and can be added or removed. The configuration files are the .pkla files. A .pkla file must be named by using a scheme to ensure that the name is unique, e.g. reverse DNS notation or similar. For example com.mycompany.packagekit.pkla .

Setup .pkla files

Each group in a .pkla file must have a name that is unique within the file it belongs to.

The following keys are are recognized:

Identity

  • A semi-colon separated list of globs to match identities. Each glob should start with unix-user: or unix-group: to specify whether to match on a UNIX user name or a UNIX group name.

Action

  • A semi-colon separated list of globs to match action identifiers.

ResultActive

  • The result to return for subjects in an active local session that matches one or more of the given identities. Allowed values are similar to what can be used in the defaults section of .policy files used to define actions, e.g. yes, no, auth_self, auth_self_keep, auth_admin and auth_admin_keep.

ResultInactive

  • Like ResultActive but instead applies to subjects in inactive local sessions.

ResultAny

  • Like ResultActive but instead applies to any subject.

ReturnValue

  • A semi-colon separated list of key/value pairs (of the form key=value) that are added to the details of authorization result on positive matches.

Sample .pkla file

# Allow "standard users" to do some things without being interrupted by
# password dialogs (TODO: not complete)
#
[Desktop User Permissions]
Identity=unix-group:wetab
Action=org.freedesktop.packagekit.package-install;org.freedesktop.packagekit.package-remove;
ResultAny=no
ResultInactive=no
ResultActive=yes

This configuration file allows packagekit to install and remove packages.

Non secure way of configuring

The policies files are located in /usr/share/polkit-1/actions/"DBUS application name" .

Issue

Anything in /usr isn't considered 'configuration', so any time the PolicyKit is updated modifications will be wiped out and the modifications should be done again. Configuration should be set /etc or /var .

References

http://hal.freedesktop.org/docs/polkit/pklocalauthority.8.html

$ man pklocalauthority

PackageKit

Packagekit is an open source and free suite of software applications designed to provide a consistent and high-level front end for a number of different package management systems. PackageKit itself is a system activated daemon called packagekitd, that abstracts out differences between the different systems. The actual nuts-and-bolts distro tool (yum, apt, conary, etc) is used by PackageKit using compiled and scripted helpers. Those tools (yum, apt, conary, etc) are called the backend for PackageKit. PackageKit isn't meant to replace these tools, instead providing a common set of abstractions that can be used by standard GUI and text mode package managers. It provides C++, python APIs which actually at this currently are not well documented and everything presented here is derived from my studying of the code.

Installation

The recent version of PackageKit is 0.6.6, this is not available in repositories and it should be compiled from sources, which can be found here http://www.packagekit.org/releases/PackageKit-0.6.6.tar.gz . Please check if newer versions are available. While configuring PackageKit dont forget to configure it with following flags

./configure --with-default-backend=yum --enable-yum --prefix=/usr --sysconfdir=/etc --disable-static --disable-local
make
sudo make install

PackageKit daemon is a system activated daemon so you have to point dbus the location of the packagekit's daemon by configuring following file

/usr/share/dbus-1/system-services/org.freedesktop.PackageKit.service

Restart dbus service

sudo /etc/init.d/messagebus restart

PackageKit daemon location is

/usr/libexec/packagekitd

Issue

The attached script failed installing packages with "wetab" user privileges, the reason is not clear yet. That to run script successfully launch it with "sudo".

Raw Dbus API

Issues

The DBus API is defined by PackageKit but it's up to the backends themselves to do the right thing, and support all the different ways of using it. Depending on platform/packagekit version Dbus API may differ, for instance Dbus API in Ubuntu for the same version of PackageKit differed from the Dbus API under MeeGo, as authorof PackageKit said "Ubuntu really needs to update PackageKit to something more up to date."

API

Object path : /org/freedesktop/PackageKit

Interfaces :

  • org.freedesktop.PackageKit
  • org.freedesktop.PackageKit.Transaction

API description can be found here

The latest interface is available in the source tree or on-line.

Help

That to see Dbus API on current platform install Dbus debugger called "d-feet". If after launching "d-feet" does not show /org/freedom/PackageKit object path then launch packagekitd daemon manually with super user privilages: sudo /usr/sbin/packagekitd

Connect to "unix:path=/var/run/dbus/system_bus_socket" to debug SystemBus

High Level Python API

There an python class called "PackageKitClient" located in /usr/lib/python2.6/site-packages/packagekit/client.py file, which provides high level interface for package management. Unfortunatelly the class is not well documented and some functions are not working OK. Because of this the packagekit-wrapper-meego.py was implemented, which contains PackageKitClient wrapper class similiar to PackageKitClient implemented in /usr/lib/python2.6/site-packages/packagekit/client.py file. The packagekit-wrapper-meego.py is modifcation/addition of Canonicals packagekit wrapper for MeeGo. Run script with superuser privilegies as currently there is an issue decribed in PackageKit installation section.

The PackageKitClient (Attached) Class is written in python and contains methods described below. Also it contains the usage of all described functionalities.

The PackageKitClient class provides following methods:

Resolve(filter, package) : returns list of matched packages

GetDetails(package) : returns the details of the packages

SearchName(filter, name) : searches and returnes availlable packages

InstallPackage(package, )

RemovePackage(package, )

The packagekit-wrapper-meego.py is attached.

The mechanisms that are used in class for implementing the methods are same for implementing the same methods in C++ as well.

Sample of retrieving Transaction's status using python:

import dbus

try:
bus = dbus.SystemBus()
except dbus.DBusException, e:
print 'Unable to connect to dbus: %s' % str(e)
sys.exit()
try:
proxy = bus.get_object('org.freedesktop.PackageKit', '/org/freedesktop/PackageKit')
iface = dbus.Interface(proxy, 'org.freedesktop.PackageKit')
tid = iface.GetTid()

proxy1 = bus.get_object('org.freedesktop.PackageKit', str(tid))
iface1 = dbus.Interface(proxy1, 'org.freedesktop.DBus.Properties')
lst = iface1.Get('org.freedesktop.PackageKit.Transaction', 'Status')
print str(lst)
except dbus.DBusException, e:
print 'Unable to use PackageKit: %s' % str(e)

High Level C++ API, using libpackagekit

The libpackagekit gobject librarywraps the DBUS interface in a nice glib-style API. This makes designing programs that use libpackagekit can concentrate on core functionality rather that the DBUS and PackageKit internals. PkClient in libpackagekit can be used as easily as:

Code sample

PkClient *client;
client = pk_client_new ();
pk_client_install_package (client, "openoffice-clipart");
g_object_unref (client);

More info can be found here http://packagekit.org/gtk-doc/PkClient.html

Mechanism of retrieving progress information

The org.freedesktop.PackageKit.Transaction interface containes transaction properties (status, percentage, etc) and signal Changed() which is invoked if one of the parameters is changed. So that to get installation/removal progres just connect to the Changed() signal and read off the properties.

PolicyKit and PackageKit

Running DBus methods like InstallPacakges, RemovePackages, UpdateSystem and others requires authorization with root account. That to change authorization policies for this and other DBus methods PolicyKit should be configured. How to configure PolicyKit can be found in this blog. That to set policy the action name for the appropriate name should be known for example "InstallPackages" methods action name is "org.freedesktop.packagekit.install-untrusted".

Action names can be found the packagekit DBus API description here http://www.packagekit.org/gtk-doc/api-reference.html

Issue

Depending on configured policies, platform there might be need to set addtional policies for example that authorize SystemUpdate without root password "sudo" also "org.freedesktop.packagekit.install-untrusted" action should be setResultActive=yes .

Solution

That to see the required action names install package "polkit-gnome" which is available in meego repositories. Polkit-gnome will launch a pop-up window if the application requries the autorization.

Under details you can see the action which required authorization.

Here is a sample which shows action name for package removal

References

http://packagekit.org/

http://packagekit.org/gtk-doc/



Sunday, July 4, 2010

How to use plug-ins for QTWebKit in Linux

Qt 4.6 has a very nice embedded web-engine component called QTWebKit for browsing web-pages. Unfortunately it does not show flash of media content for that purpose there should be set environment QTWEBKIT_PLUGIN_PATH variable to directory which contains compiled plug-ins (.so file).

example : export QTWEBKIT_PLUGIN_PATH=/home/deimus/flash/


Monday, March 15, 2010

Linux Presentation

Prepared presentation of Linux History, Introduction and Overview for the beginners.

Can be found here. Linux-Introduction.pdf

Monday, March 8, 2010

Integral computation using nVidia CUDA technology

Recently I wrote an integral computation program on GPU using nVidia CUDA technology. The acceleration is astonishing for the big accuracy in comparison with the analogue program running on CPU which by the way is provided by Henry Golding and can be found in his personal blog.

However to understand the code you should have some knowledge of nVidia CUDA supporting GPU, which can be founded in nVidia's official webpage. But for the usage imho it is quiet friendly.

source code: main.cu


Wednesday, March 3, 2010

Compiling CUDA with C/C++

Platform: ubuntu 9.10
gcc version: 4.4.1
cuda toolkit: 2.3

While compiling .cu (cuda) file with c/c++ code developer usually will get following error.

/usr/include/c++/4.4/ext/atomicity.h(46): error: identifier "__sync_fetch_and_add" is undefined

/usr/include/c++/4.4/ext/atomicity.h(50): error: identifier "__sync_fetch_and_add" is undefined
It seems still there are problems while compiling cuda with gcc 4.4 and above.
I found only this way to workaround this problem.
nvcc --device-emulation --compiler-bindir=/usr/bin/gcc-4.3 -arch sm_13 main.cu -o run
  • --device-emulation: for cuda supporting device emulation in case if real hardware is not available.
  • --compiler-bindir=/usr/bin/gcc-4.3: to use gcc version 4.3 this will solve the problem.
  • -arch sm_13: in case if atomic function are used in implementation, compiles with 1.3 cuda supporting hardware