Tuesday, March 13, 2012

Controlling Mouse Speed in Ubuntu when System Settings Doesn't Cut It (aka xinput)

My laptop comes with a Targus cordless mouse which moves too fast even when both Accelleration and Sensitivity in Pointer Speed settings is set to the lowest in Ubuntu. 

There's another method to control input speed via the xinput command.

To begin, execute "xinput list" to obtain the list of inputs available:

mike@myhost:~$ xinput list
⎡ Virtual core pointer                        id=2    [master pointer  (3)]
⎜   ↳ Virtual core XTEST pointer                  id=4    [slave  pointer  (2)]
⎜   ↳ SONiX Targus Soft-Touch Cordless Mouse      id=8    [slave  pointer  (2)]
⎣ Virtual core keyboard                       id=3    [master keyboard (2)]
    ↳ Virtual core XTEST keyboard                 id=5    [slave  keyboard (3)]
    ↳ Power Button                                id=6    [slave  keyboard (3)]
    ↳ Power Button                                id=7    [slave  keyboard (3)]
    ↳ Dell Dell USB Entry Keyboard                id=9    [slave  keyboard (3)]
    ↳ Dell WMI hotkeys                            id=10    [slave  keyboard (3)]

From the output above, it's quite obvious which input device we'll be working with (id=8 if you can't spot it).  Next, we'll move on to list down the device properties:

mike@myhost:~$ xinput list-props 8
Device 'SONiX Targus Soft-Touch Cordless Mouse':
        Device Enabled (143):   1
        Coordinate Transformation Matrix (145): 1.000000, 0.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 0.000000, 1.000000
        Device Accel Profile (260):     0
        Device Accel Constant Deceleration (261):       1.000000
        Device Accel Adaptive Deceleration (262):       1.000000
        Device Accel Velocity Scaling (263):    10.000000
        Evdev Axis Inversion (264):     0, 0
        Evdev Axes Swap (266):  0
        Axis Labels (267):      "Rel X" (153), "Rel Y" (154)
        Button Labels (268):    "Button Left" (146), "Button Middle" (147), "Button Right" (148), "Button Wheel Up" (149), "Button Wheel Down" (150), "Button Horiz Wheel Left" (151), "Button Horiz Wheel Right" (152), "Button Side" (524), "Button Extra" (525), "Button Unknown" (259), "Button Unknown" (259), "Button Unknown" (259), "Button Unknown" (259)
        Evdev Middle Button Emulation (269):    0
        Evdev Middle Button Timeout (270):      50
        Evdev Wheel Emulation (271):    0
        Evdev Wheel Emulation Axes (272):       0, 0, 4, 5
        Evdev Wheel Emulation Inertia (273):    10
        Evdev Wheel Emulation Timeout (274):    200
        Evdev Wheel Emulation Button (275):     4
        Evdev Drag Lock Buttons (276):  0

To properly "decelerate" the mouse pointer, we'll need to play around with 2 properties:
  • Device Accel Constant Deceleration (261)
  • Device Accel Adaptive Deceleration (262)
To update a property value, use the "--set-prop" option like below:
mike@myhost:~$ xinput --set-prop 8 261 4
Note the values used:
  • 8: device id
  • 261: property id
  • 4: value of the property to be set
Setting both property value to 4 works for me; YMMV.

Have fun tweaking your input :)



Tuesday, February 21, 2012

Using Python minidom module for XML generation

Creating an XML document in Python using the minidom module is quite straightforward.  For simplicity sake, I'll base the code on Hadoop's XML config file.

import sys
from xml.dom.minidom import Document

doc = Document()

configuration = doc.createElement("configuration")

xsl = doc.createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"configuration.xsl\"")

doc.appendChild(xsl)

prop = doc.createElement("property")

# START LOOP HERE
name = doc.createElement("name")
name_text = doc.createTextNode("my name")
name.appendChild(name_text)

prop.appendChild(name)

value = doc.createElement("value")
value_text = doc.createTextNode("my value")
value.appendChild(value_text)

prop.appendChild(value)
# END LOOP HERE

configuration.appendChild(prop)

doc.appendChild(configuration)

print doc.toprettyxml()

The output is as follows:

<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
    <property>
        <name>
            my name
        </name>
        <value>
            my value
        </value>
    </property>
</configuration>

Friday, February 17, 2012

Scrollback in GNU Screen

This has always evaded me: How do you do scroll up the screenbuffer when using screen?  Found the answer right here:

http://www.linuxscrew.com/2008/11/14/faq-how-to-scrollback-in-gnu-screen/

I've tried the Ctrl + A + [ method; but not Ctrl + Esc as I'm using Windows as my main machine and Ctrl + Esc can also be used as the Windows key.

Just hope I remember this :)

Thursday, February 16, 2012

Measuring network throughput with iperf

The iperf tool has been around for ages and yeah, I've been late to the game :)

To install iperf in Ubuntu, it's a simple apt-get away:
$ sudo apt-get install iperf
To run the test, you'll need 2 machines.  One to act as the server and the other as the client.  Start by starting the server:
$ iperf -s
------------------------------------------------------------
Server listening on TCP port 5001
TCP window size: 85.3 KByte (default)
------------------------------------------------------------
If there are no errors, we can then start the test by executing iperf in the client machine:
$ iperf -c server -t 10
------------------------------------------------------------
Client connecting to server, TCP port 5001
TCP window size: 16.0 KByte (default)
------------------------------------------------------------
[  3] local 192.168.0.100 port 40913 connected with 192.168.0.101 port 5001
[ ID] Interval       Transfer     Bandwidth
[  3]  0.0-10.0 sec    164 MBytes    138 Mbits/sec
The argument -t 10 states that we want the test to run for only 10 seconds. 

There are other options which you can play with like:

    -d ==> Do a bidirectional test simultaneously
    -u ==> use UDP rather than TCP
   
View the list of options by executing:
$ iperf --help
There's also a GUI wrapper written in Java for iperf available at http://code.google.com/p/xjperf/.  It offers a nice GUI which allows you to choose different options to run iperf with and also displays output from iperf in a nice graph:

Friday, December 16, 2011

Logging request parameters in CodeIgniter

The application I'm currently working on provides services to mobile devices.  As such, I need to constantly check what values the mobile devices are POST-ing to the application.  Instead of adding a call to all functions in the controllers to perform this task, I decided to implement a hook in the application.

Hooks are supported in CodeIgniter (more info here).  Here's my hook class to perform the logging:

class Logger {
   
    private $CI;
   
    public function __construct() {
        $this->CI =& get_instance();
    }
   
    public function request_logger() {
        $uri = $this->CI->uri->uri_string();
       
            $params = trim(print_r($this->CI->input->post(), TRUE));
       
        log_message('info', '==============');
        log_message('info', 'URI: ' . $uri);
        log_message('info', '--------------');
        log_message('info', $params);
        log_message('info', '==============');
    }
}
To enable hooks, you'll need to edit the config/hooks.php file.  Here's the content of the file to enable the Logger class above:
$hook['post_controller_constructor'] = array(
                                'class'    => 'Logger',
                                'function' => 'request_logger',
                                'filename' => 'Logger.php',
                                'filepath' => 'hooks'
                            );

I chose to use "post_controller_constructor" as it is called before any methods in the controller class is executed.

Output below shows how the output looks like from the application logs:
INFO  - 2011-12-15 05:09:24 --> URI: services/promos/add_comment
INFO  - 2011-12-15 05:09:24 --> Array
(
    [param1] => value1
    [id] => dj3243hasdgasdg
    [msg] => Testing testing
)