#!/usr/bin/python
# 
# Zeecontrol ((c) 2010 Ruediger Gad) lets you control your N900 
# via a Zeemote bluetooth remote control. Zeecontrol is 
# based on wiicontrol.
#   wiicontrol copyright notice: 
#     wiicontrol: Control your Internet Tablet from your wiimote
#     (c) 2008 Jose Luis Diaz
#
# Zeecontrol is like wiicontrol released under the terms of the
# GNU General Public License:
#
# 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 3 of the License, 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, see <http://www.gnu.org/licenses/>.

# ================================================================
# Configurable part (see wiicontrol README)

import dbus
import gobject
import gtk
import sys
sys.path.append("/usr/lib/zeecontrol")
from ZeecontrolGUI import ZeecontrolGUI, ZeemoteEventThread

def getBTAdapter():
  bus = dbus.SystemBus()
  root = bus.get_object("org.bluez", "/")
  manager = dbus.Interface(root, "org.bluez.Manager")
  defaultAdapter = manager.DefaultAdapter()
  obj = bus.get_object("org.bluez", defaultAdapter)
  return dbus.Interface(obj, "org.bluez.Adapter")
 
def setBTState(state):
  adapter = getBTAdapter()
  adapter.SetProperty("Powered", state)

def getBTState():
  adapter = getBTAdapter()
  props = adapter.GetProperties()
  state = props["Powered"]
  return state

btInitState = getBTState()
if not btInitState:
  setBTState(True)

gui = ZeecontrolGUI()

gobject.threads_init()
eventThread=ZeemoteEventThread(gui)
eventThread.start()
gtk.main()

#if 'Zee' in globals():
#  print "Disconnecting zeemote..."
#  Zee.disconnect()

if not btInitState:
  setBTState(False)

