#!/usr/bin/env python import wx, sys, os, pwd from subprocess import * WIFILOC = "/proc/acpi/asus/wlan" BTLOC = "/proc/acpi/asus/bt" SH_WIFIOFF=("ifconfig ra0 down","rmmod rt2860sta","sleep 1","echo 0 > /proc/acpi/asus/wlan","modprobe -r pciehp") SH_WIFION=("modprobe pciehp pciehp_force=1 pciehp_debug=1","echo 1 > /proc/acpi/asus/wlan","sleep 1","modprobe rt2860sta") SH_BTON=("echo 1 > /proc/acpi/asus/bt","sleep 1","modprobe bluetooth","modprobe hci_usb") SH_BTOFF=("modprobe -r hci_usb","modprobe -r bluetooth","sleep 1","echo 0 > /proc/acpi/asus/bt") #################################################### #################################################### # # Misc utilities # #################################################### #################################################### def getval(loc): """Function to get a true or false that would normally be achieved by (e.g.) cat /proc/acpi/wlan Returns a boolean""" lfile = open(loc, "r") if lfile.read().strip() == "1": return True return False def getuser(): if os.environ['LOGNAME']=="root": return os.environ['SUDO_USER'] else: return os.environ['LOGNAME'] #################################################### #################################################### # # OSD # #################################################### #################################################### class TempFrame(wx.Frame): def __init__(self, text, time): wx.Frame.__init__(self,None,wx.ID_ANY,'', style=wx.FRAME_NO_TASKBAR, pos=(0,0), size=(200,100)) #initialise self.TopSizer=wx.BoxSizer(wx.HORIZONTAL) self.TopSizer.Add(wx.StaticText(self,wx.ID_ANY,text, style=wx.ALIGN_CENTRE ),1,wx.ALIGN_CENTRE) self.SetSizer(self.TopSizer) #self.TopSizer.Fit(self) self.tmr=wx.Timer(self, wx.ID_ANY) wx.EVT_TIMER(self,wx.ID_ANY,self.OnTimer) wx.EVT_CLOSE(self, self.OnClose) self.tmr.Start(time, True) def OnClose(self,e): #wx.MessageBox("Closing") self.Destroy() def OnTimer(self,e): self.Close(True) wx.Exit() #TODO: This is a hack, a better workaround needs to be found def DoOSD(text, time=1000): """Shows osd briefly Arguments: text - string for text to show in display time - time for display to be up in msec (default 1sec) """ #TODO: OSD with graphics app=wx.App() frame=TempFrame(text,time) frame.Show(True) app.SetTopWindow(frame) app.MainLoop() del app #################################################### #################################################### # # Hardware Interface Functions # #################################################### #################################################### def BTOff(): for s in SH_BTOFF: os.system(s) def BTOn(): for s in SH_BTON: os.system(s) def WifiOn(): for s in SH_WIFION: os.system(s) def WifiOff(): for s in SH_WIFIOFF: os.system(s) #################################################### #################################################### # # Deal with Hkey event # #################################################### #################################################### #f=open("/home/wwykeham/log.txt",'w') #for s in os.environ: # f.write(s) # f.write(" ") # f.write(os.environ[s]) # f.write('\n') #f.close() #DoOSD(getuser()) hkey=sys.argv[3] cfg=wx.Config("eee-hkey") cmdtype=cfg.Read("/map/"+hkey+"/action", "none") #TODO: better handling of no BT in proc if cmdtype=="4WayToggle": bt_status=getval(BTLOC) wlan_status=getval(WIFILOC) if bt_status and wlan_status: BTOff() WifiOff() DoOSD("BT: Off Wifi: Off") if bt_status and not wlan_status: WifiOn() DoOSD("BT:On Wifi: On") if not bt_status and wlan_status: WifiOff() BTOn() DoOSD("BT: On Wifi: Off") if not bt_status and not wlan_status: WifiOn() DoOSD("BT: Off Wifi: On") elif cmdtype=="BTToggle": if getval(BTLOC): BTOff() DoOSD("Bluetooth Off") else: BTOn() DoOSD("Bluetooth On") elif cmdtype=="WifiToggle": if getval(WIFILOC): WifiOff() DoOSD("Wifi Off") else: WifiOn() DoOSD("Wifi On") #elif cmdtype=="WebcamToggle": #TODO elif cmdtype=="VolMute": # check whether Master is already muted amix = Popen("amixer get Master", shell=True, stdout=PIPE) amix.wait() muted = False for ln in amix.stdout: if ln.find("[off]") > -1: muted = True if muted: Popen("amixer set Master unmute", shell=True, stdout=PIPE) else: Popen("amixer set Master mute", shell=True, stdout=PIPE) elif cmdtype=="VolUp": Popen("amixer set Master 10%+", shell=True, stdout=PIPE) elif cmdtype=="VolDown": Popen("amixer set Master 10%-", shell=True, stdout=PIPE) elif cmdtype=="cmd": str="su "+getuser()+ " -c " + cfg.Read("/map/"+hkey+"/cmd") Popen(str, shell=True) #TODO call programs using su and sh -c to set user. Where do we get user from?