#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Small utility that displays notifications when a device* is connected
# *device -> Sixaxis, Keypads and remotes
#
# Made by falkTX, 2009

import commands, os, sys, time

if (len(sys.argv) == 2):
  if (sys.argv[1] == "--stop"): os.system("rm -f /tmp/.sixa-notify")
  exit(1)

if os.path.exists("/tmp/.sixa-notify"):
  print "sixa-notify is already running"
  print "run: \"sixa-notify --stop\" to stop it"
  exit(-1)
else:
  os.system("touch /tmp/.sixa-notify")


DeviceMAC="00:00:00:00:00"
DeviceName="Null"
DeviceIcon="sixaxis"  # FIXME - there's only a Sixaxis icon...
Filter='grep -e "PLAYSTATION(R)3 Controller" -e "PLAYSTATION(R)3 Remote" -e "Wireless Keypad" -e "new interface driver btusb" -e "054C:0268" -e "054C:03A0" -e "054C:0306" -e "0079:0006"'

last_dmesg = commands.getoutput("dmesg | tail -n 1")
os.system('notify-send --icon=/usr/share/qtsixa/icons/qtsixa.png "QtSixA" "Device notifications have been started"')

while (1 == 1): #Always loop program until file removed, or killed

  #This file controls wherever sixa-notify is started or not
  if not os.path.exists("/tmp/.sixa-notify"):
    print "Notifications are now stopped"
    exit(-2)
  else:

    if (commands.getoutput('dmesg | tail -n 1 | '+Filter) != ""):
	if (commands.getoutput('dmesg | tail -n 1 | '+Filter) != last_dmesg):
	    #New Device!
	    final_dmesg = commands.getoutput("dmesg | tail -n 1") #new
	    if ("054C:0268" in final_dmesg):
		if ("054C:" in final_dmesg.split()[1]): DeviceMAC = final_dmesg.split()[1]
		if ("054C:" in final_dmesg.split()[2]): DeviceMAC = final_dmesg.split()[2]
		if ("054C:" in final_dmesg.split()[3]): DeviceMAC = final_dmesg.split()[3]
		DeviceName = "PLAYSTATION(R)3 Controller (USB)"
		DeviceIcon = "sixaxis"
	    elif ("PLAYSTATION(R)3 Controller" in final_dmesg):
		DeviceMAC = commands.getoutput("hcitool con | tail -n 1 | awk '{printf$3}'")
		DeviceName = "PLAYSTATION(R)3 Controller (Bluetooth)"
		DeviceIcon = "sixaxis"
	    elif ("054C:03A0" in final_dmesg and "USB" in final_dmesg):
		if ("054C:" in final_dmesg.split()[1]): DeviceMAC = final_dmesg.split()[1]
		if ("054C:" in final_dmesg.split()[2]): DeviceMAC = final_dmesg.split()[2]
		if ("054C:" in final_dmesg.split()[3]): DeviceMAC = final_dmesg.split()[3]
		DeviceName = "Wireless Keypad (USB)"
		#DeviceIcon = "keypad"
	    elif ("Wireless Keypad" in final_dmesg):
		DeviceMAC = commands.getoutput("hcitool con | tail -n 1 | awk '{printf$3}'")
		DeviceName = "Wireless Keypad (Bluetooth)"
		#DeviceIcon = "keypad"
	    elif ("054C:0306" in final_dmesg):
		if ("054C:" in final_dmesg.split()[1]): DeviceMAC = final_dmesg.split()[1]
		if ("054C:" in final_dmesg.split()[2]): DeviceMAC = final_dmesg.split()[2]
		if ("054C:" in final_dmesg.split()[3]): DeviceMAC = final_dmesg.split()[3]
		DeviceName = "PLAYSTATION(R)3 Remote (USB)"
		#DeviceIcon = "remote"
	    elif ("PLAYSTATION(R)3 Remote" in final_dmesg):
		DeviceMAC = commands.getoutput("hcitool con | tail -n 1 | awk '{printf$3}'")
		DeviceName = "PLAYSTATION(R)3 Remote (Bluetooth)"
		#DeviceIcon = "remote"
	    elif ("0079:0006" in final_dmesg):
		if ("0079:" in final_dmesg.split()[1]): DeviceMAC = final_dmesg.split()[1]
		if ("0079:" in final_dmesg.split()[2]): DeviceMAC = final_dmesg.split()[2]
		if ("0079:" in final_dmesg.split()[3]): DeviceMAC = final_dmesg.split()[3]
		DeviceName = "Generic USB Joystick"
		DeviceIcon = "sixaxis"
	    elif ("new interface driver btusb" in final_dmesg):
		time.sleep(1)
		DeviceMAC = commands.getoutput("hcitool dev | tail -n 1 | awk '{printf$2}'")
		DeviceName = "Bluetooth dongle"
		#DeviceIcon = "bt-dongle"

	    print "New Device: "+DeviceMAC+" "+DeviceName
	    os.system('notify-send --icon=/usr/share/qtsixa/icons/'+DeviceIcon+'.png "QtSixA" "New Device Connected:\n '+DeviceMAC+' '+DeviceName+'"')

	    last_dmesg = commands.getoutput("dmesg | tail -n 1")
	else:
	    last_dmesg = commands.getoutput("dmesg | tail -n 1")
    else:
	last_dmesg = commands.getoutput("dmesg | tail -n 1")

    time.sleep(1)

