#!/usr/bin/python
# -*- coding: UTF8 -*-

# amarok-rc.py
# allow user to remote control amarok with tux droid's remote control
# it uses dcop so that various kde applications  could be controlled that way
# ( see kdcop)

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
# -----------------------------------------------------------------------------
# $Id: $
# -----------------------------------------------------------------------------

"""
CHANGES
=======
2007/08/05 - version 0.0.1:
	- Initial version
	- amarok control
2007/08/08 - version 0.0.2:
	- added support for kaffeine
	- added switching mechanism
"""

__author__  = "David Coulm <cheos@chez-cheos.org>"
__appname__ = "k_remote_control"
__version__ = "0.0.2"
__date__    = "2007/08/08"
__license__ = "GPL"


# Init

import sys
sys.path.append('/opt/tuxdroid/api/python')
from tux import *

# tux init
def tuxinit():
	tux.cmd.mouth_close()
	tux.cmd.eyes_open()
	tux.cmd.leds_off()

# insert names here that sound approximately like the englisch names when they are spoken
AMAROK = "amarock"
KAFFEINE = "käffiehn"
	
def rc_empty():
	print "Button pressed"

# amarok events

def amarok_mute():
	tux.sys.shell("dcop amarok player mute")

def amarok_volumeplus():
	tux.sys.shell("dcop amarok player volumeUp")

def amarok_volumeminus():
	tux.sys.shell("dcop amarok player volumeDown")

def amarok_fastrewind():
	tux.sys.shell("dcop amarok player seekRelative -2")

def amarok_fastforwad():
	tux.sys.shell("dcop amarok player seekRelative 2")

def amarok_playpause():
	tux.sys.shell("dcop amarok player playPause")

def amarok_stop():
	tux.sys.shell("dcop amarok player stop")

def amarok_menu():
	tux.sys.shell("dcop amarok player showOSD")

def amarok_previous():
	tux.sys.shell("dcop amarok player prev")

def amarok_next():
	tux.sys.shell("dcop amarok player next")
	
#kaffeine events

def kaffeine_menu():
	tux.sys.shell("dcop kaffeine KaffeineIface dvbOSD")
	
def kaffeine_fullscreen():
	tux.sys.shell("dcop kaffeine KaffeineIface fullscreen")
	
def kaffeine_mute():
	tux.sys.shell("dcop kaffeine KaffeineIface mute")
	
def kaffeine_volumeplus():
	tux.sys.shell("dcop kaffeine KaffeineIface volUp")
	
def kaffeine_volumeminus():
	tux.sys.shell("dcop kaffeine KaffeineIface volDown")
	
def kaffeine_set_Number(i):	#currently not used
	tux.sys.shell("dcop kaffeine KaffeineIface setNumber %i"%i)
	
def kaffeine_stop():
	tux.sys.shell("dcop kaffeine KaffeineIface stop")
	
def kaffeine_next():
	tux.sys.shell("dcop kaffeine KaffeineIface next")
	
def kaffeine_play():
	tux.sys.shell("dcop kaffeine KaffeineIface play")
	
def kaffeine_previous():
	tux.sys.shell("dcop kaffeine KaffeineIface previous")
	
#administrative functions
	
def tell_current_app():
	tuxspeak("Momentan gewählt: " + currentApp)
	
def switch_to_amarok():
	global currentApp
	
	currentApp = AMAROK
	tux.event.on_remote_bt[K_MUTE]=amarok_mute
	tux.event.on_remote_bt[K_VOLUMEPLUS]=amarok_volumeplus
	tux.event.on_remote_bt[K_VOLUMEMINUS]=amarok_volumeminus
	tux.event.on_remote_bt[K_FASTREWIND]=amarok_fastrewind
	tux.event.on_remote_bt[K_FASTFORWARD]=amarok_fastforwad
	tux.event.on_remote_bt[K_PLAYPAUSE]=amarok_playpause
	tux.event.on_remote_bt[K_STOP]=amarok_stop
	tux.event.on_remote_bt[K_MENU]=amarok_menu
	tux.event.on_remote_bt[K_PREVIOUS]=amarok_next
	tux.event.on_remote_bt[K_NEXT]=amarok_previous
	tux.event.on_remote_bt[K_RECEIVECALL]=rc_empty
	tux.event.on_remote_bt[K_CHANNELPLUS]=rc_empty
	tux.event.on_remote_bt[K_CHANNELMINUS]=rc_empty
	
	tell_current_app()
	
def switch_to_kaffeine():
	global currentApp
	
	currentApp = KAFFEINE
	tux.event.on_remote_bt[K_MENU]=kaffeine_menu
	tux.event.on_remote_bt[K_RECEIVECALL]=kaffeine_fullscreen
	tux.event.on_remote_bt[K_MUTE]=kaffeine_mute
	tux.event.on_remote_bt[K_VOLUMEPLUS]=kaffeine_volumeplus
	tux.event.on_remote_bt[K_VOLUMEMINUS]=kaffeine_volumeminus
	tux.event.on_remote_bt[K_PLAYPAUSE]=kaffeine_play
	tux.event.on_remote_bt[K_STOP]=kaffeine_stop
	tux.event.on_remote_bt[K_PREVIOUS]=kaffeine_previous
	tux.event.on_remote_bt[K_NEXT]=kaffeine_next
	tux.event.on_remote_bt[K_CHANNELPLUS]=kaffeine_next
	tux.event.on_remote_bt[K_CHANNELMINUS]=kaffeine_previous
	tux.event.on_remote_bt[K_FASTREWIND]=rc_empty
	tux.event.on_remote_bt[K_FASTFORWARD]=rc_empty

	tell_current_app()
		
	
def tuxspeak(text):
	tux.cmd.mouth_on_free(2)
	tux.tts.speak_free(text)


# body
tuxinit()

#default = amarok
currentApp = AMAROK
tux.event.on_remote_bt[K_MUTE]=amarok_mute
tux.event.on_remote_bt[K_VOLUMEPLUS]=amarok_volumeplus
tux.event.on_remote_bt[K_VOLUMEMINUS]=amarok_volumeminus
tux.event.on_remote_bt[K_FASTREWIND]=amarok_fastrewind
tux.event.on_remote_bt[K_FASTFORWARD]=amarok_fastforwad
tux.event.on_remote_bt[K_PLAYPAUSE]=amarok_playpause
tux.event.on_remote_bt[K_STOP]=amarok_stop
tux.event.on_remote_bt[K_MENU]=amarok_menu
tux.event.on_remote_bt[K_PREVIOUS]=amarok_next
tux.event.on_remote_bt[K_NEXT]=amarok_previous

#switching buttons are the color buttons
tux.event.on_remote_bt[K_RED]=switch_to_amarok
tux.event.on_remote_bt[K_GREEN]=switch_to_kaffeine

#alt lets tux tell which program is selected
tux.event.on_remote_bt[K_ALT]=tell_current_app

#removing any application from these events
tux.event.on_remote_bt[K_RECORDING]=rc_empty
tux.event.on_remote_bt[K_ESCAPE]=rc_empty
tux.event.on_remote_bt[K_YES]=rc_empty
tux.event.on_remote_bt[K_NO]=rc_empty
tux.event.on_remote_bt[K_BACKSPACE]=rc_empty
tux.event.on_remote_bt[K_STARTVOIP]=rc_empty
tux.event.on_remote_bt[K_RECEIVECALL]=rc_empty
tux.event.on_remote_bt[K_HANGUP]=rc_empty
tux.event.on_remote_bt[K_STAR]=rc_empty
tux.event.on_remote_bt[K_SHARP]=rc_empty
tux.event.on_remote_bt[K_0]=rc_empty
tux.event.on_remote_bt[K_1]=rc_empty
tux.event.on_remote_bt[K_2]=rc_empty
tux.event.on_remote_bt[K_3]=rc_empty
tux.event.on_remote_bt[K_4]=rc_empty
tux.event.on_remote_bt[K_5]=rc_empty
tux.event.on_remote_bt[K_6]=rc_empty
tux.event.on_remote_bt[K_7]=rc_empty
tux.event.on_remote_bt[K_8]=rc_empty
tux.event.on_remote_bt[K_9]=rc_empty
tux.event.on_remote_bt[K_BLUE]=rc_empty
tux.event.on_remote_bt[K_YELLOW]=rc_empty
tux.event.on_remote_bt[K_CHANNELPLUS]=rc_empty
tux.event.on_remote_bt[K_CHANNELMINUS]=rc_empty
tux.event.on_remote_bt[K_UP]=rc_empty
tux.event.on_remote_bt[K_DOWN]=rc_empty
tux.event.on_remote_bt[K_LEFT]=rc_empty
tux.event.on_remote_bt[K_RIGHT]=rc_empty
tux.event.on_remote_bt[K_OK]=rc_empty
tux.event.on_remote_bt[K_MOUSE]=rc_empty

tux.event.wait_remote_bt(K_STANDBY,9999)
tux.destroy()