#!/usr/bin/perl -w

use strict;

$|++;

open my $fh, "/dev/hidraw2";

my $cnt = 0;

my @b;

while (1) {
    my $val;
    my $res = read $fh, $val, 1;
    $val = ord ($val);
    $b[$cnt++] = $val;

    if ($cnt == 4) {
	$cnt = 0;
	# 0,1,0,0 = answer press
	# 0,0,0,0 = release (answer, vol-up, vol-down but not mute!)
	# 2,0,0,0 = vol-down press
	# 1,0,0,0 = vol-up press 
	# 8,4,0,0 = mute press (when not muted)
	# 8,0,0,0 = mute press (when muted)
	if ($b[0] == 0 && $b[1] == 1 && $b[2] == 0 && $b[3] == 0) {
	    system("rhythmbox-client --pause");
	    print "sending answer to linphone\n";
	    kill 10, 30058;
	}
    }

}
