add lib of DBUS

This commit is contained in:
cuu 2018-06-16 21:48:28 +08:00
parent d4e83803d8
commit 30c7c675ad
5 changed files with 343 additions and 21 deletions

163
sysgo/DBUS/dbus.go Normal file
View File

@ -0,0 +1,163 @@
package DBUS
import (
"github.com/godbus/dbus"
)
type DbusInterface struct {
Dest string
Path dbus.ObjectPath
Iface string
Obj *dbus.Object
}
func NewDbusInterface(conn *dbus.Conn,dest string, path dbus.ObjectPath ,iface string) *DbusInterface {
m := &DbusInterface{}
o := conn.Object(dest,path)
m.Obj = o.(*dbus.Object)
m.Dest = dest
m.Path = path
if len(iface) > 2 {
m.Iface = iface
}
return m
}
func (self *DbusInterface) Method(name string, args...interface{} ) *dbus.Call {
var method string
if self.Iface != "" {
method = fmt.Sprintf("%s.%s.%s", self.Dest, self.Iface,name)
}else {
method = fmt.Sprintf("%s.%s", self.Dest,name)
}
if args != nil {
return self.Obj.Call( method , 0,args...)
}else {
return self.Obj.Call( method, 0)
}
}
func (self *DbusInterface) Get( thecall *dbus.Call, retvalues ...interface{}) {
if len(thecall.Body) == 0 {
return
}
err:= thecall.Store(retvalues...)
if err != nil {
panic(fmt.Sprintf("Failed: %s",err))
}
}
type DBusInterface interface {
WifiStrength() int
IsWifiConnectedNow() bool
}
type DBus struct {
Conn *dbus.Conn
Daemon *DbusInterface
Wifi *DbusInterface
}
func (self *DBus) Init() {
conn, err := dbus.SystemBus()
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to connect to session bus:", err)
os.Exit(1)
}
self.Conn = conn
self.Daemon = NewDbusInterface(conn, "org.wicd.daemon","/org/wicd/daemon" ,"",)
self.Wifi = NewDbusInterface(conn, "org.wicd.daemon","/org/wicd/daemon/wireless","wireless")
}
func (self *DBus) WifiStrength() int {
var fast bool
var iwconfig string
var sig_display_type int
var strength int
self.Daemon.Get( self.Daemon.Method("NeedsExternalCalls"), &fast)
if fast == false {
self.Wifi.Get( self.Wifi.Method("GetIwconfig"), &iwconfig )
}else{
iwconfig = ""
}
self.Daemon.Get( self.Daemon.Method("GetSignalDisplayType"), &sig_display_type )
if sig_display_type == 0 {
self.Wifi.Get( self.Wifi.Method("GetCurrentSignalStrength",iwconfig), &strength)
} else{
self.Wifi.Get( self.Wifi.Method("GetCurrentDBMStrength",iwconfig), &strength)
}
return strength
}
func (self *DBus) check_for_wireless(iwconfig string, wireless_ip string) bool {
var network string
var sig_display_type int
var strength int
if wireless_ip == "" {
return false
}
self.Wifi.Get( self.Wifi.Method("GetCurrentNetwork",iwconfig), &network)
self.Daemon.Get( self.Daemon.Method("GetSignalDisplayType"), &sig_display_type )
if sig_display_type == 0 {
self.Wifi.Get( self.Wifi.Method("GetCurrentSignalStrength",iwconfig), &strength)
}else {
self.Wifi.Get( self.Wifi.Method("GetCurrentDBMStrength",iwconfig), &strength)
}
if strength == 0 {
return false
}
strength_str := strconv.Itoa(strength)
self.Daemon.Get( self.Daemon.Method("FormatSignalForPrinting",strength_str), &strength_str)
return true
}
func (self *DBus) IsWifiConnectedNow() bool {
var fast bool
var iwconfig string
var wireless_connecting bool
var wireless_ip string
self.Wifi.Get( self.Wifi.Method("CheckIfWirelessConnecting"), &wireless_connecting )
self.Daemon.Get( self.Daemon.Method("NeedsExternalCalls"), &fast)
if wireless_connecting == true {
return false
}else {
if fast == false {
self.Wifi.Get( self.Wifi.Method("GetIwconfig"), &iwconfig )
}else {
iwconfig = ""
}
self.Wifi.Get( self.Wifi.Method("GetWirelessIP", iwconfig), &wireless_ip)
if self.check_for_wireless(iwconfig,wireless_ip) == true {
return true
}else {
return false
}
}
}
func (self *DBus) ListenSignal() {
c := make(chan *dbus.Signal, 10)
self.Conn.Signal(c)
for v := range c {
fmt.Printf("%+v %#v\n",v,v)
}
}

View File

@ -11,13 +11,16 @@ import (
type IconItemInterface interface { type IconItemInterface interface {
Init(x,y,w,h,at int) Init(x,y,w,h,at int)
Adjust(x,y,w,h,at int)
GetCmdPath() string GetCmdPath() string
SetCmdPath( path string) SetCmdPath( path string)
SetMyType( thetype int ) SetMyType( thetype int )
GetMyType() int GetMyType() int
GetIconIndex() int
SetIconIndex(idx int)
GetIndex() int GetIndex() int
SetIndex(i int) SetIndex(i int)
@ -49,6 +52,7 @@ type IconItem struct {
ImgSurf *sdl.Surface ImgSurf *sdl.Surface
Parent PageInterface Parent PageInterface
Index int Index int
IconIndex int
MyType int MyType int
CmdPath string CmdPath string
CmdInvoke PluginInterface CmdInvoke PluginInterface
@ -81,6 +85,23 @@ func (self *IconItem) Init(x,y,w,h,at int) {
self.AnimationTime = at self.AnimationTime = at
} }
func (self *IconItem) Init(x,y,w,h,at int) {
self.PosX = x
self.PosY = y
self.Width = w
self.Height = h
self.AnimationTime = at
if self.Label != nil {
self.Label.SetCanvasHWND(self.Parent.GetCanvasHWND())
}
self.CreateImageSurf()
self.AdjustLinkPage()
}
func (self *IconItem) GetCmdPath() string { func (self *IconItem) GetCmdPath() string {
return self.CmdPath return self.CmdPath
} }
@ -97,6 +118,14 @@ func (self *IconItem) GetMyType() int {
return self.MyType return self.MyType
} }
func (self *IconItem) GetIconIndex() int {
return self.IconIndex
}
func (self *IconItem) SetIconIndex( idx int) {
self.IconIndex = idx
}
func (self *IconItem) GetIndex() int { func (self *IconItem) GetIndex() int {
return self.Index return self.Index
} }

View File

@ -10,7 +10,7 @@ type MultiIconItem struct {
IconWidth int IconWidth int
IconHeight int IconHeight int
IconIndex int
} }
func NewMultiIconItem() *MultiIconItem { func NewMultiIconItem() *MultiIconItem {

View File

@ -18,6 +18,44 @@ import (
var TitleBar_BarHeight = 24 var TitleBar_BarHeight = 24
type TitleBarIconItem struct {
MultiIconItem
Parent *TitleBar
}
func NewTitleBarIconItem() *TitleBarIconItem {
m := &TitleBarIconItem{}
return m
}
func (self *TitleBarIconItem) Draw() {
parent_x,parent_y := self.Parent.PosX,self.Parent.PosY
if self.Label != nil {
// lab_x,lab_y := self.Label.Coord()
lab_w,lab_h:= self.Label.Size()
if self.Align == ALIGN["VCenter"] {
self.Label.NewCoord( self.PosX - lab_w/2 + parent_x, self.PosY + self.Height/2+6 + parent_y)
}else if self.Align == ALIGN["HLeft"] {
self.Label.NewCoord( self.PosX + self.Width/2+3 + parent_x, self.PosY - lab_h/2 + parent_y )
}
self.Label.Draw()
}
if self.ImgSurf != nil {
portion := rect.Rect(0,self.IconIndex*self.IconHeight,self.IconWidth,self.IconHeight)
surface.Blit(self.Parent.GetCanvasHWND(),
self.ImgSurf,draw.MidRect(self.PosX + parent_x, self.PosY + parent_y,
self.Width,self.Height, Width, Height),&portion)
}
}
type TitleBar struct { type TitleBar struct {
PosX int PosX int
@ -27,15 +65,15 @@ type TitleBar struct {
BarHeight int BarHeight int
LOffset int LOffset int
ROffset int ROffset int
Icons map[string]interface{} Icons map[string]IconItemInterface
IconWidth IconWidth
IconHeight IconHeight
BorderWidth BorderWidth
CanvasHWND *sdl.Surface CanvasHWND *sdl.Surface
HWND interface{} HWND *sdl.Surface
Title string Title string
InLowBackLight int InLowBackLight int
SkinManager interface{} SkinManager *SkinManager //set by MainScreen
icon_base_path string /// SkinMap("gameshell/titlebar_icons/") icon_base_path string /// SkinMap("gameshell/titlebar_icons/")
} }
@ -52,9 +90,15 @@ func NewTitleBar() *TitleBar {
t.Width = Width t.Width = Width
t.Icons = make(map[string]interface{}) t.IconWidth = 18
t.IconHeight = 18
//t.icon_base_path = SkinMap("gameshell/titlebar_icons/") t.LOffset = 3
t.ROffset = 3
t.Icons = make(map[string]IconItemInterface)
t.icon_base_path = SkinMap("gameshell/titlebar_icons/")
} }
func (t *TitleBar) RoundRobinCheck { func (t *TitleBar) RoundRobinCheck {
@ -83,7 +127,7 @@ func (t *TitleBar) GetWifiStrength(stren string) int {
return ge return ge
} }
func (t *TitleBar) SyncSoundVolume() { func (self *TitleBar) SyncSoundVolume() {
vol, err := volume.GetVolume() vol, err := volume.GetVolume()
if err != nil { if err != nil {
@ -102,6 +146,8 @@ func (t *TitleBar) SyncSoundVolume() {
} }
} }
self.Icons["soundvolume"].SetIconIndex(ge)
self.Icons["sound"] = self.Icons["soundvolume"]
// //
} }
@ -109,13 +155,13 @@ func (t *TitleBar) SetSoundVolume(vol int) {
//pass //pass
} }
func (t *TitleBar) CheckBatteryStat() { func (self *TitleBar) CheckBatteryStat() {
bat_segs:= [][]int{[]int{0,6},[]int{7,15},[]int{16,20},[]int{21,30},[]int{31,50},[]int{51,60},[]int{61,80},[]int{81,90},[]int{91,100}} bat_segs:= [][]int{[]int{0,6},[]int{7,15},[]int{16,20},[]int{21,30},[]int{31,50},[]int{51,60},[]int{61,80},[]int{81,90},[]int{91,100}}
file, err := os.Open( sysgo.Battery ) file, err := os.Open( sysgo.Battery )
if err != nil { if err != nil {
fmt.Println("Could not open file ", sysgo.Battery) fmt.Println("Could not open file ", sysgo.Battery)
t.Icons["battery"] = t.Icons["battery_unknown"] self.Icons["battery"] = self.Icons["battery_unknown"]
return return
} }
@ -154,27 +200,88 @@ func (t *TitleBar) CheckBatteryStat() {
if val, ok := bat_uevent["POWER_SUPPLY_STATUS"]; ok { if val, ok := bat_uevent["POWER_SUPPLY_STATUS"]; ok {
if val == "Charging" { if val == "Charging" {
t.Icons["battery_charging"].IconIndex = cap_ge self.Icons["battery_charging"].SetIconIndex(cap_ge)
t.Icons["battery"] = t.Icons["battery_charging"] self.Icons["battery"] = self.Icons["battery_charging"]
}else { }else {
t.Icons["battery_charging"].IconIndex = cap_ge self.Icons["battery_charging"].SetIconIndex(cap_ge)
t.Icons["battery"] = t.Icons["battery_discharging"] self.Icons["battery"] = self.Icons["battery_discharging"]
} }
} }
} }
func (t *TitleBar) SetBatteryStat( bat int) { func (self *TitleBar) SetBatteryStat( bat int) {
} }
func (t *TitleBar) Init(screen *MainScreen) { func (self *TitleBar) Init(main_screen *MainScreen) {
start_x := 0 start_x := 0
t.CanvasHWND = surface.Surface(t.Width,t.Height) self.CanvasHWND = surface.Surface(self.Width,self.Height)
t.HWND = screen self.HWND = main_screen.HWND
self.SkinManager = main_screen.SkinManager
icon_wifi_statu := NewMultiIconItem()
icon_wifi_status := NewTitleBarIconItem()
icon_wifi_status.MyType = ICON_TYPES["STAT"]
icon_wifi_status.ImageName = self.icon_base_path+"wifi.png"
icon_wifi_status.Parent = self
icon_wifi_status.Adjust(start_x+self.IconWidth+5,self.IconHeight/2+(self.BarHeight-self.IconHeight)/2,self.IconWidth,self.IconHeight,0)
self.Icons["wifistatus"] = icon_wifi_status
battery_charging := NewTitleBarIconItem()
battery_charging.MyType = ICON_TYPES["STAT"]
battery_charging.Parent = self
battery_charging.ImageName = self.icon_base_path+"withcharging.png"
battery_charging.Adjust(start_x+self.IconWidth+self.IconWidth+8,self.IconHeight/2+(self.BarHeight-self.IconHeight)/2,self.IconWidth,self.IconHeight,0)
self.Icons["battery_charging"] = battery_charging
battery_discharging := NewTitleBarIconItem()
battery_discharging.MyType = ICON_TYPES["STAT"]
battery_discharging.Parent = self
battery_discharging.ImageName = self.icon_base_path+"without_charging.png"
battery_discharging.Adjust(start_x+self.IconWidth+self.IconWidth+8,self.IconHeight/2+(self.BarHeight-self.IconHeight)/2,self.IconWidth,self.IconHeight,0)
self.Icons["battery_discharging"] = battery_discharging
battery_unknown := NewTitleBarIconItem()
battery_unknown.MyType = ICON_TYPES["STAT"]
battery_unknown.Parent = self
battery_unknown.ImageName = self.icon_base_path+"battery_unknown.png"
battery_unknown.Adjust(start_x+self.IconWidth+self.IconWidth+8,self.IconHeight/2+(self.BarHeight-self.IconHeight)/2,self.IconWidth,self.IconHeight,0)
self.Icons["battery_unknown"] = battery_unknown
self.CheckBatteryStat()
sound_volume := NewTitleBarIconItem()
sound_volume.MyType = ICON_TYPES["STAT"]
sound_volume.Parent = self
sound_volume.ImageName = self.icon_base_path+"soundvolume.png"
sound_volume.Adjust(start_x+self.IconWidth+self.IconWidth+8,self.IconHeight/2+(self.BarHeight-self.IconHeight)/2,self.IconWidth,self.IconHeight,0)
self.Icons["soundvolume"] = sound_volume
self.SyncSoundVolume()
round_corners := NewTitleBarIconItem()
round_corners.IconWidth = 10
round_corners.IconHeight = 10
round_corners.MyType = ICON_TYPES["STAT"]
round_corners.Parent = self
round_corners.ImgSurf = MyIconPool.GetImageSurf["roundcorners"]
round_corners.Adjust(0,0,10,10,0)
self.Icons["round_corners"] = round_corners
if is_wifi_connected_now() {
print("wifi is connected")
print( wifi_strength())
}
} }

View File

@ -8,8 +8,31 @@ import (
"github.com/cuu/gogame/display" "github.com/cuu/gogame/display"
"../../sysgo"
) )
func SkinMap(orig_file_or_dir string) string {
DefaultSkin := "default"
ret := ""
if strings.HasPrefix(orig_file_or_dir, "..") {
ret = strings.Replace(orig_file_or_dir,"..","../skin/"+sysgo.SKIN,-1)
if FileExists(ret) == false {
ret = strings.Replace(orig_file_or_dir,"..", "../skin/"+DefaultSkin)
}
}else {
ret = "../skin/"+sysgo.SKIN+"/sysgo/"+orig_file_or_dir
if FileExists(ret) == false {
ret = "../skin/"+DefaultSkin+"/sysgo/"+orig_file_or_dir
}
}
if FileExists(ret) {
return ret
}else { // if not existed both in default or custom skin ,return where it is
return orig_file_or_dir
}
}
func CmdClean(cmdpath string) string { func CmdClean(cmdpath string) string {
spchars := "\\`$();|{}&'\"*?<>[]!^~-#\n\r " spchars := "\\`$();|{}&'\"*?<>[]!^~-#\n\r "
for _,v:= range spchars { for _,v:= range spchars {