import sys
sys.path.append("/opt/tuxdroid/api/python")
import os, string
import doctest

from tux import *

def str2int(s) :
    pos = 0
    if s[0] in '-+' :
        pos = 1
    while pos < len(s):
        if s[pos] not in '1234567890':
            return int(s[0:pos])
        pos = pos + 1 

def get_volume():
    cmd=os.popen("aumix -v q")
    s=cmd.readline()
    cmd.close()
    vol_get = string.split(s, " ")
    return (str2int(vol_get[1]), str2int(vol_get[2]))
    

def increase_volume():
    vol = get_volume()[1]
    new_vol = vol+5
    os.system("aumix -v " + str(new_vol))

def decrease_volume():
    vol = get_volume()[1]
    new_vol = vol-5
    os.system("aumix -v " + str(new_vol))

def set_mute():
    os.system("aumix -v M")
    

if __name__ == "__main__":
    tux.event.set_on_remote_bt(K_VOLUMEPLUS,increase_volume)
    tux.event.set_on_remote_bt(K_VOLUMEMINUS,decrease_volume)
    tux.event.set_on_remote_bt(K_MUTE,set_mute)