Script to disable & enable touchpads

I constantly touch the touchpad when I type. I also normally use an external mouse when I’m on my laptop. I seldom really need it, but I will sometimes want to switch it on (or off).

First you need to find the name of your touchpad in the xinput list:

$ xinput list

⎡ Virtual core pointer                     id=2 [master pointer  (3)]

⎜   ↳ Virtual core XTEST pointer               id=4 [slave  pointer  (2)]

⎜   ↳ Razer Razer Imperator                   id=10 [slave  pointer  (2)]

⎜   ↳ Razer Razer Imperator                   id=11 [slave  pointer  (2)]

⎜   ↳ PS/2 Mouse                               id=13 [slave  pointer  (2)]

⎜   ↳ AlpsPS/2 ALPS GlidePoint                 id=14 [slave  pointer  (2)]

⎣ Virtual core keyboard                   id=3 [master keyboard (2)]

    ↳ Virtual core XTEST keyboard             id=5 [slave  keyboard (3)]

    ↳ Sony Vaio Keys                           id=6 [slave  keyboard (3)]

    ↳ Video Bus                               id=7 [slave  keyboard (3)]

    ↳ Power Button                             id=8 [slave  keyboard (3)]

    ↳ USB 2.0 Camera                           id=9 [slave  keyboard (3)]

    ↳ AT Translated Set 2 keyboard             id=12 [slave  keyboard (3)]

So in my case the touchpad is most likely: PS/2 Mouse on id 13.

You can easily identify if the device is “on” (enabled):

$ xinput list-props “PS/2 Mouse” 

Device ‘PS/2 Mouse’:

Device Enabled (144): 1

Coordinate Transformation Matrix (146): 1.000000, 0.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 0.000000, 1.000000

Device Accel Profile (267): 0

Device Accel Constant Deceleration (268): 1.000000

Device Accel Adaptive Deceleration (269): 1.000000

Device Accel Velocity Scaling (270): 10.000000

Evdev Axis Inversion (271): 0, 0

Evdev Axes Swap (273): 0

Axis Labels (274): “Rel X” (154), “Rel Y” (155)

Button Labels (275): “Button Left” (147), “Button Middle” (148), “Button Right” (149), “Button Wheel Up” (150), “Button Wheel Down” (151)

Evdev Middle Button Emulation (276): 0

Evdev Middle Button Timeout (277): 50

Evdev Wheel Emulation (278): 0

Evdev Wheel Emulation Axes (279): 0, 0, 4, 5

Evdev Wheel Emulation Inertia (280): 10

Evdev Wheel Emulation Timeout (281): 200

Evdev Wheel Emulation Button (282): 4

Evdev Drag Lock Buttons (283): 0

 Now create yourself a little file (eg: “touchpad”):

# toggle synaptic touchpad on/off

SYNSTATE=$(xinput list-props “PS/2 Mouse” | grep Enabled | grep -Eo ‘.$’)

if [ $SYNSTATE = 0 ]; then xinput set-int-prop “PS/2 Mouse” “Device Enabled” 8 1

else xinput set-int-prop “PS/2 Mouse” “Device Enabled” 8 0; fi

dengar@dengar-vaio:/usr/bin$ $ xinput list-props “PS/2 Mouse” 

This should sort you out. Don’t forget to  change the name of your actual device, remember mine is “PS/2 Mouse”

Next you need to make the file executable:

sudo chmod +x touchpad

Now, to have that file available from the command line, simply copy it to /usr/bin.

Some simple MySQL date-time functions

I often need the first of the month, the last of the month or how many days are in the month for reporting and forecasting purposes.

You can, of course, like myself in the past look up how these things are done every time you need them. But this gets frustrating and it’s often easier to surround yourself with your set of own SQL-functions that give you what you need and are easy to remember.

For this purpose I give you:

  1. First of the month date:
  2. drop function if exists fn_getFirstOfMonth;
    create function fn_getFirstOfMonth(date_ datetime) returns datetime
    begin
    set @dater = date_;
    set @dater = DATE_FORMAT(@dater ,’%Y-%m-01′);
    return @dater;
    end

  3. Last of the month date:

    drop function if exists fn_getLastOfMonth;
    create function fn_getLastOfMonth(date_ datetime) returns datetime
    begin
    set @dater = date_;
    set @dater = DATE_FORMAT(last_day(@dater) ,’%Y-%m-%d’);
    return @dater;
    end

  4. Days in the month:

    drop function if exists fn_getDaysInMonth;
    create function fn_getDaysInMonth(date_ datetime) returns int
    begin
    set @dater = date_;
    set @dater = day(last_day(@dater));
    return @dater;
    end

Do let me know if there are other MySQL date and time functions that you would like to know.
Happy to post them here…

Can I have some Thetan for my sandwich? – Buying religion off the shelf religion from the street

Scientologists, although not quite often recognised, have been flogging their religion on the streets for many decades now. And why not? – Other cults and religions are doing it too. Mormons, Heavens Gate and even the Manson Family have successfully advertised their ideology through direct promotion, they know the system works.

I believe people are naturally curious, but would be more careful when they know they are being ‘evaluated’ by a religious cult, so to circumvent the initial resistance that they would have to overcome, Scientology doesn’t advertise with it’s name, in fact, nothing at their meet stand indicated that they are in any way pursuing another agenda than offering a ‘free stress test’ and a giant book to help you through it.

Anyways, there I was, coming out of the tube and seeing the Scientologist stand right in front of the station. I was ready to be Scientologied.

“So this device checks how stressed I am by passing an insignificant amount of electricity through my body?”

I am surprised though how many people go for the “free stress test” offered by L. Ron Hubbard’s minions though. I remember the ‘Churches’ earlier recruitment efforts required their subjects to undergo a more detailed test, that can still be taken online. It’s a fun thing to do when you have absolutely nothing to do for 16 hours of your life. Feel free to do it here.
This time I was promised that the Free Stress Test would last only four minutes.

Soon enough I was handed the handles of the E-Meter, a device that measures changes in the electrical resistance of your body and the conductance of your skin by passing a very tiny current through your body. It did take a while, talking about various levels of potentially stress inducing scenarios until my tester, through turning and twisting of various dials, managed to get the needle to swing. I think about 4 minutes, in fact. It must’ve helped that my hands started to get a bit sweaty, after all that does increase the conductance of my skin. The result: yes, I am stressed – what a surprise.

My antidote was purchasing the Dianetics DVD and book for the little sum of £20. Since I always wanted to have a gander, I negotiated the price down to £5 for the book only, but I am afraid I have not had the chance to flick through it. After the sale, I was of course encouraged to give my name, address and email to the lead Scientologist, but I only left my first name and an old email address that I haven’t used in a long time.

Your soul is possessed by this volcano and only Tom Cruise will be able to show you the way.

I did take the book home, but I was rather surprised when I saw it sodomising actual literature, see here how it forcibly engulfs my favourite book, Cannery Row by John Steinbeck.

Bad El-Ron! Down! Leave the Cannery Row alone!

It did seem rather popular with the crowd in front of this London tube station at 8.30 in the evening. Maybe buying religion off the shelf is a huge business? Maybe I should create a religion eCommerce shop where you can create your own religion by mix and match? Eg: mix some Judaism with Buddhism, top it up with Greek Mythology and sprinkle some Heaven’s Gate over it. £350,000.
See, that’s the clever thing about my religion shop: the product is easy to produce and even easier to source, but if you do pay THAT kinda money, you will treasure it…

Create a home VPN and allow computers to connect with each other

Like most organisations, my workplace has quite strict security policies. For some reason, I wasn’t allowed and able to connect to the data centre through the Cisco VPN that had been set-up by our company. I had to had a static URL at home that needed to be allowed, but I wouldn’t consider changing my provider purely for that benefit.

There are plenty of options to ‘break’ that security restriction, like having a Terminal Server within your corporate network that you could log onto. The slow and clunkiness of that solution turned me off. In addition, I would have to get helpdesk to install all sorts of programmes and, since I’m a Linux man, I would have to compromise on every which way.

The easiest solution, I thought, was, since I can’t VPN onto the network from home, to VPN to my home network from work!

I already have a server for various tools that keeps on running most days and nights, so why not also use it as a VPN server.

Installing and configuring the necessary software is easy and takes just minutes.
First you will need to install the PPTP daemon:

$ sudo apt-get install pptpd

In Ubuntu, the configuration file for the pptp-daemon is: /etc/pptpd.conf and we will need to modify it so there is a separate IP defined for a) the server (which can be the one the server is getting from your router) and b) the client(s). If you have more than one client you will obviously have to make sure you have a range of IPs available for the clients.

In my case, since the networks should be quite separate at home and through VPN, I decided to use a different IP range. My router assigns IPs in the range 192.168.1.XXX, so I will use the range 192.168.0.XXX through my VPN server. You will need to update the last lines of your pptpd.conf like this:

localip 192.168.0.1
remoteip 192.168.0.2-10

This assigns the IP ~.1 to the VPN server (a separate interface) and allows the numbers ~.2 to ~.10 to be assigned to clients.

Next step: create VPN users and their passwords. This is stored in the /etc/ppp/chap-secrets file. Here is an example:

# Secrets for authentication using CHAP
# client server secret IP addresses
CLIENTLOGIN pptpd CLIENTPASSWORD *

You could obviously restrict the IP addresses the client can connect, but a very long password should also be quite secure.
In theory that’s all you need to connect, but you might want to allow clients to use the server’s internet connection and allow it to connect to other IPs within your local network. For this we will need to use NAT and IP forwarding. You can enable this via adding this line to your /etc/rc.local (before exit):

iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

and in /etc/sysctl.conf uncomment the line:

net.ipv4.ip_forward=1

Now you can restart the daemon:

$ sudo /etc/init.d/pptpd restart

If your computer doesn’t use the public IP address, eg. you are behind a modem – router or a firewall, you will need to forward the port 1723 to your server. And to avoid trouble with dynamic IP addresses you get from your ISP, you should use a dynamic DNS service. This will update your current IP to the DNS lookup tables and you get an easy URL name to remember. I think I use DynDNS.org.

Finally, you are on the client, but you can’t see any other computers within the VPN or the local network of the server. This is because the routing is not set correctly on your client computer. You will want to “search” this VPN IP range through the VPN server, not your normal internet connection, so you can add a manual route like this:

route add -net 192.168.0.0 netmask 255.255.255.0 dev ppp0

Tadah! You can find other computers in the VPN!

For more advanced routing, eg: make your client’s local connections available to hosts local connections, I advise to take a look at this page full of knowledge. The beauty is that once the VPN connection is established, you can bridge pretty much all connections through using routing and iptables.

Geo IP lookup – ipinfodb.com API integration using PHP

(this also explains the PHP DOM parser)

For one or the other reason, and be it that you want to determine the home country of your visitor for localisation and language options, you might find yourself looking at integrating an IP lookup through an external API.

I suppose they all work very similar, but I have just integrated my shopping cart with the API from IPInfoDB.com. How it works is explained below…

A word up-front, though: When using third party APIs – think! Do you really need to hit the API with every page request? Especially in this case, would it not be fair to assume that the whole sessions would be coming from the same IP and therefore the same country? To an extend, could you not assume that it is one user you are tracking here and that he would probably come back to your site from within the same country, even though his IP might have changed? Could you therefore store the result from the API in a browser session or in a cookie?
– I actually do store it in the DB per cookie, but that’s not important for this little tutorial.

First up, you will need to have access to the API, so you need an API key, token-code or something to identify yourself with the API. On the IPInfoDB.com site you can register and receive your free key within seconds.

Got the key? – off we go then: Thankfully this is a pretty simple API that doesn’t use SOAP, but requires a simple HTML request and returns a nicely formatted XML. Here is a result:

<?xml version=”1.0″ encoding=”UTF-8″?>
<Response>
<Status>OK</Status>
<CountryCode>GB</CountryCode>
<CountryName>United Kingdom</CountryName>
<RegionCode>P9</RegionCode>
<RegionName>Windsor and Maidenhead</RegionName>
<City>Hillingdon</City>
<ZipPostalCode></ZipPostalCode>
<Latitude>51.5167</Latitude>
<Longitude>-0.45</Longitude>
<Gmtoffset>0</Gmtoffset>
<Dstoffset>0</Dstoffset>
<TimezoneName></TimezoneName>
<Isdst></Isdst>
<Ip>217.42.247.86</Ip>
</Response>

So far so good… Let’s create a corresponding PHP class that will make the usage easy portable to different projects.

  1. I create a class and bunch of variables and add getter methods to the class, so I can return the variables when I need them:

    class geoIP {

    var $apiKey = “XXXXYOURKEYHEREXXXX”;
    var $city;
    var $country;

    function getCity() {
    return $this -> city;

  2. Now I know that API key will be available when I initialise the object. Moving on to making a call…
    It’s quite easy in this instance, because we don’t need to set SOAP headers or anything. It’s a simple API and all information we need to give IPInfoDB can be parsed into the request URL. Here is how it looks:

    http://api.ipinfodb.com/v2/ip_query.php?key=YOURKEY&ip=IPTOCHECK&timezone=false

    So I create a method, let’s say, “makeCityCall” that I can then send the IP to check to:

    function makeCityCall($ip) {

    $urler = ‘http://api.ipinfodb.com/v2/ip_query.php?key=’. $this -> apiKey .’&ip=’. $ip .’&timezone=false’;

    The “$this -> apiKey” will return the API key that we declared and initialised as a global variable for this class. The “$ip” is what I send to the method.

  3. I know when I load this URL, I get an XML response. XML is best parsed via a DOM-parser. The DOM parser comes standard with most versions of PHP and is an object in it’s own rights. We have to instantiate this and can then give it the URL we declared ($urler) to load:

    $parser = new DOMDocument();
    $parser  -> load($urler);

  4. As we saw by the result returned above, the interesting bit is wrapped in a XML node called Response. We only get one, of course, but I will still ask the DOM parser to repeat the process of getting the values from all nodes that it finds:

    $nodes = $parser -> getElementsByTagName(“Response”);
    foreach ($nodes as $node) {

    I now want to get the values of the nodes contained within the Response node:

    $cc  = $node -> getElementsByTagName(‘CountryCode’);
    $cc =  $cc -> item(0)->nodeValue;
    $this -> countryCode = $cc;

    $cty = $node -> getElementsByTagName(‘City’);
    $cty = $cty -> item(0)->nodeValue;
    $this -> city = $cty;

    $ctry = $node -> getElementsByTagName(‘CountryName’);
    $ctry = $ctry -> item(0) -> nodeValue;
    $this -> country = $ctry;

    Thus setting the variables that I later want to retrieve via the getter methods.

  5. Lastly, if I now want to make a call from a PHP application, I simply need to instantiate the new class, get the IP address from the site visitor (or from wherever) and use the method to make a request. I can then retrieve the results from the getter-methods:

    $ip = $_SERVER[‘REMOTE_ADDR’];
    include (‘PATHTOPHPFILE/geoIP.php’);
    $gIP = new geoIP();
    $countCall = $gIP -> makeCityCall($ip);

    I can then write this out onto the page or re-use it as I wish:

    echo $gIP -> getCountry() . ‘<br />’;
    echo $gIP -> getCity() . ‘<br />’;
    echo $gIP -> getCountryCode() . ‘<br />’;

I hope this gets you further…

Java Swing GUI builder for Eclipse

I often struggle to make Swing applications to look good, or even make them vaguely look like I intended. The different JFrame layout options and all the coordinates of where to place what and how long is that makes the creation of Swing interfaces difficult.

Since I don’t like using different development environments and got very used to Eclipse for anything I need to do, I was looking for a good graphical GUI builder that works as a plugin for Eclipse. I came across this really nice one: Jigloo from CloudGarden.

It’s pretty easy to install into Eclipse, via update site: http://cloudgarden1.com/update-site and then creates GUIs by drag and drop in a way that I thought wouldn’t have been possible. The commercial use of this product is prohibited without license, but this shouldn’t stop any hobby programmer, much like myself, from using this great product.

Here is a screenshot I leeched off their site (I might attach one or two myself):

Enjoy

Bear?

From: d3ngar
To: bear
Subject: Bear

Message:
Wow, Bear!

What a nick – what a movie…

You might not know this, Bear, but there is a movie with the same name, Bear, that came out last year. You might have guessed: it features a bear, named Bear…

You might wanna check out Bear, the movie, it’s really great : http://www.imdb.com/title/tt1468703/

Best Regards,

d3ngar
d3ngar.blogspot.com

BadgerComp / Cart & Opilion

Anyhow, so I decided to start using the Java code from Opilion for crawling tasks for BadgerComp.com.
I probably will continue building on the image getter first. I’m thinking that you will want to upload them straight somewhere. Probably via FTP. Or you could download them to your local hard disk first.

I’ll probably make the tool available via the BadgerCart Wiki, but it might be getting annoying to enter the user details all the time. Maybe I need to be able to load settings via XML?

Transdroid – Add torrent files to your home PC from anywhere

I do use my home computer for various things while I’m out. The more important ones features for me are SSH / SFTP and Subsonic, but I also keep Transmission, the default ubuntu bitTorrent client, running throughout the day.

So it does happen, that files are being uploaded or downloaded, while I would like to use the available bandwidth otherwise.
Along comes Transdroid
With transdroid you can control your torrent client remotely, set download limits, delete or pause and resume torrent down and uploads.
But it does one important other thing: it allows you to find and add torrent files. Search for some (if not all) the popular torrent search engines is already build in. All you need to do is select it.
If you are often away from your computer when you remember that you should actually be downloading something, or restrict downloading of a torrent, then this Android app will be of great valuable to you. Here is the QR code to download Transdroid from the Android market it right away:

Feng Office – Team Collaboration & Project Management software

Feng Office is the successor of a software called OpenGoo and probably one of the best pieces of free, open source software that I have come accross. It’s really on par with MediaWiki and has become a real asset in the development of my site.

It combines a powerful project management and tracking system with a collaboration hub.

Using workspaces, you can conveniently manage multiple projects at the same time and handle resources accross the different projects. User rights prevent people from accessing locations that they are not meant to see.

Through a bit of customisation, you can set it up to not only track changes to all your online files, but also see who is working on which file at the moment. While someone is working on a file, and be this a PHP file or a letter to a supplier, it is marked as ‘checked out’. This prevents two people modifying the file at the same time, thus eliminating one parties work…

It also has features for Notes, email, contacts and a calendar. It’s a truly amazing piece of software and deserves the support fo the community…