title_bar.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. package UI
  2. import (
  3. "fmt"
  4. "os"
  5. "log"
  6. "strconv"
  7. "bufio"
  8. "strings"
  9. "os/exec"
  10. gotime "time"
  11. "github.com/veandco/go-sdl2/sdl"
  12. "github.com/veandco/go-sdl2/ttf"
  13. "github.com/cuu/gogame/surface"
  14. "github.com/cuu/gogame/rect"
  15. "github.com/cuu/gogame/font"
  16. "github.com/cuu/gogame/draw"
  17. "github.com/itchyny/volume-go"
  18. "github.com/vjeantet/jodaTime"
  19. "github.com/cuu/LauncherGoDev/sysgo/DBUS"
  20. "github.com/cuu/LauncherGoDev/sysgo"
  21. )
  22. var TitleBar_BarHeight = 24
  23. type TitleBarIconItem struct {
  24. MultiIconItem
  25. Parent *TitleBar
  26. }
  27. func NewTitleBarIconItem() *TitleBarIconItem {
  28. m := &TitleBarIconItem{}
  29. m.IconIndex = 0
  30. m.IconWidth = 18
  31. m.IconHeight = 18
  32. m.Align = ALIGN["VCenter"]
  33. return m
  34. }
  35. func (self *TitleBarIconItem) Adjust(x,y,w,h,at int) {
  36. self.PosX = x
  37. self.PosY = y
  38. self.Width = w
  39. self.Height = h
  40. self.AnimationTime = at
  41. if self.Label != nil {
  42. self.Label.SetCanvasHWND(self.Parent.CanvasHWND)
  43. }
  44. self.CreateImgSurf()
  45. // self.AdjustLinkPage()
  46. }
  47. func (self *TitleBarIconItem) Draw() {
  48. parent_x,parent_y := self.Parent.PosX,self.Parent.PosY
  49. if self.Label != nil {
  50. // lab_x,lab_y := self.Label.Coord()
  51. lab_w,lab_h:= self.Label.Size()
  52. if self.Align == ALIGN["VCenter"] {
  53. self.Label.NewCoord( self.PosX - lab_w/2 + parent_x, self.PosY + self.Height/2+6 + parent_y)
  54. }else if self.Align == ALIGN["HLeft"] {
  55. self.Label.NewCoord( self.PosX + self.Width/2+3 + parent_x, self.PosY - lab_h/2 + parent_y )
  56. }
  57. self.Label.Draw()
  58. }
  59. if self.ImgSurf != nil {
  60. portion := rect.Rect(0,self.IconIndex*self.IconHeight,self.IconWidth,self.IconHeight)
  61. surface.Blit(self.Parent.CanvasHWND,
  62. self.ImgSurf,draw.MidRect(self.PosX + parent_x, self.PosY + parent_y,
  63. self.Width,self.Height, Width, Height),&portion)
  64. }
  65. }
  66. type TitleBar struct {
  67. Widget
  68. BarHeight int
  69. LOffset int
  70. ROffset int
  71. Icons map[string]IconItemInterface
  72. IconWidth int
  73. IconHeight int
  74. BorderWidth int
  75. CanvasHWND *sdl.Surface
  76. HWND *sdl.Surface
  77. Title string
  78. InLowBackLight int
  79. InAirPlaneMode bool
  80. SkinManager *SkinManager //set by MainScreen
  81. DBusManager DBUS.DBusInterface
  82. icon_base_path string /// SkinMap("gameshell/titlebar_icons/")
  83. TitleFont *ttf.Font
  84. TimeFont *ttf.Font
  85. }
  86. func NewTitleBar() *TitleBar {
  87. t := &TitleBar{}
  88. t.BorderWidth = 1
  89. t.BarHeight = TitleBar_BarHeight
  90. t.Height = t.BarHeight + t.BorderWidth
  91. t.Width = Width
  92. t.IconWidth = 18
  93. t.IconHeight = 18
  94. t.LOffset = 3
  95. t.ROffset = 3
  96. t.Icons = make(map[string]IconItemInterface)
  97. t.icon_base_path = SkinMap("sysgo/gameshell/titlebar_icons/")
  98. t.TitleFont = Fonts["varela16"]
  99. t.TimeFont = Fonts["varela12"]
  100. return t
  101. }
  102. func (self *TitleBar) RoundRobinCheck() {
  103. for {
  104. if self.InLowBackLight < 0 {
  105. self.CheckBatteryStat()
  106. ///self.CheckBluetooth()
  107. self.UpdateWifiStrength()
  108. SwapAndShow()
  109. }else if self.InLowBackLight >= 0 {
  110. self.InLowBackLight +=1
  111. if self.InLowBackLight > 10 {
  112. self.CheckBatteryStat()
  113. self.UpdateWifiStrength()
  114. self.InLowBackLight = 0 // reset
  115. }
  116. }
  117. gotime.Sleep(3000 * gotime.Millisecond)
  118. }
  119. }
  120. func (self *TitleBar) UpdateWifiStrength() {
  121. self.Draw(self.Title)
  122. }
  123. func (t *TitleBar) GetWifiStrength(stren int) int {
  124. segs := [][]int{ []int{-2,-1}, []int{0,25}, []int{25,50}, []int{50,75},[]int{75,100}}
  125. stren_number := stren
  126. ge := 0
  127. if stren_number == 0 {
  128. return ge
  129. }
  130. for i,v := range segs {
  131. if stren_number >= v[0] && stren_number <= v[1] {
  132. ge = i
  133. break
  134. }
  135. }
  136. return ge
  137. }
  138. func (self *TitleBar) SyncSoundVolume() {
  139. vol, err := volume.GetVolume()
  140. if err != nil {
  141. log.Fatalf("TitleBar SyncSoundVolume get volume failed: %+v", err)
  142. vol = 0
  143. }
  144. fmt.Printf("TitleBar SyncSoundVolume current volume: %d\n", vol)
  145. snd_segs := [][]int{ []int{0,10}, []int{10,30}, []int{30,70},[]int{70,100} }
  146. ge := 0
  147. for i,v := range snd_segs {
  148. if vol >= v[0] && vol <= v[1] {
  149. ge = i
  150. break
  151. }
  152. }
  153. self.Icons["soundvolume"].SetIconIndex(ge)
  154. self.Icons["sound"] = self.Icons["soundvolume"]
  155. //
  156. }
  157. // for outside widget to update sound icon
  158. func (self *TitleBar) SetSoundVolume(vol int) {
  159. snd_segs := [][]int{ []int{0,10}, []int{10,30}, []int{30,70},[]int{70,100} }
  160. ge := 0
  161. for i,v := range snd_segs {
  162. if vol >= v[0] && vol <= v[1] {
  163. ge = i
  164. break
  165. }
  166. }
  167. self.Icons["soundvolume"].SetIconIndex(ge)
  168. self.Icons["sound"] = self.Icons["soundvolume"]
  169. }
  170. func (self *TitleBar) CheckBatteryStat() {
  171. 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}}
  172. file, err := os.Open( sysgo.Battery )
  173. if err != nil {
  174. fmt.Println("Could not open file ", sysgo.Battery)
  175. self.Icons["battery"] = self.Icons["battery_unknown"]
  176. return
  177. }
  178. defer file.Close()
  179. bat_uevent := make(map[string]string)
  180. scanner := bufio.NewScanner(file)
  181. scanner.Split(bufio.ScanLines)
  182. for scanner.Scan() {
  183. line := scanner.Text()
  184. line = strings.Trim(line," ")
  185. pis := strings.Split(line,"=")
  186. if len(pis) > 1 {
  187. bat_uevent[ pis[0] ] = pis[1]
  188. }
  189. }
  190. cur_cap := 0
  191. if val, ok := bat_uevent["POWER_SUPPLY_CAPACITY"]; ok {
  192. cur_cap,_ = strconv.Atoi(val)
  193. }else {
  194. cur_cap = 0
  195. }
  196. cap_ge := 0
  197. for i,v := range bat_segs {
  198. if cur_cap >= v[0] && cur_cap <= v[1] {
  199. cap_ge = i
  200. break
  201. }
  202. }
  203. if val, ok := bat_uevent["POWER_SUPPLY_STATUS"]; ok {
  204. if val == "Charging" {
  205. self.Icons["battery_charging"].SetIconIndex(cap_ge)
  206. self.Icons["battery"] = self.Icons["battery_charging"]
  207. }else {
  208. self.Icons["battery_charging"].SetIconIndex(cap_ge)
  209. self.Icons["battery"] = self.Icons["battery_discharging"]
  210. }
  211. }
  212. }
  213. func (self *TitleBar) SetBatteryStat( bat int) {
  214. }
  215. func (self *TitleBar) Init(main_screen *MainScreen) {
  216. start_x := 0
  217. self.CanvasHWND = surface.Surface(self.Width,self.Height)
  218. self.HWND = main_screen.HWND
  219. self.SkinManager = main_screen.SkinManager
  220. self.DBusManager = main_screen.DBusManager
  221. icon_wifi_status := NewTitleBarIconItem()
  222. icon_wifi_status.MyType = ICON_TYPES["STAT"]
  223. icon_wifi_status.ImageName = self.icon_base_path+"wifi.png"
  224. icon_wifi_status.Parent = self
  225. icon_wifi_status.Adjust(start_x+self.IconWidth+5,self.IconHeight/2+(self.BarHeight-self.IconHeight)/2,self.IconWidth,self.IconHeight,0)
  226. self.Icons["wifistatus"] = icon_wifi_status
  227. battery_charging := NewTitleBarIconItem()
  228. battery_charging.MyType = ICON_TYPES["STAT"]
  229. battery_charging.Parent = self
  230. battery_charging.ImageName = self.icon_base_path+"withcharging.png"
  231. battery_charging.Adjust(start_x+self.IconWidth+self.IconWidth+8,self.IconHeight/2+(self.BarHeight-self.IconHeight)/2,self.IconWidth,self.IconHeight,0)
  232. self.Icons["battery_charging"] = battery_charging
  233. battery_discharging := NewTitleBarIconItem()
  234. battery_discharging.MyType = ICON_TYPES["STAT"]
  235. battery_discharging.Parent = self
  236. battery_discharging.ImageName = self.icon_base_path+"without_charging.png"
  237. battery_discharging.Adjust(start_x+self.IconWidth+self.IconWidth+8,self.IconHeight/2+(self.BarHeight-self.IconHeight)/2,self.IconWidth,self.IconHeight,0)
  238. self.Icons["battery_discharging"] = battery_discharging
  239. battery_unknown := NewTitleBarIconItem()
  240. battery_unknown.MyType = ICON_TYPES["STAT"]
  241. battery_unknown.Parent = self
  242. battery_unknown.ImageName = self.icon_base_path+"battery_unknown.png"
  243. battery_unknown.Adjust(start_x+self.IconWidth+self.IconWidth+8,self.IconHeight/2+(self.BarHeight-self.IconHeight)/2,self.IconWidth,self.IconHeight,0)
  244. self.Icons["battery_unknown"] = battery_unknown
  245. self.CheckBatteryStat()
  246. sound_volume := NewTitleBarIconItem()
  247. sound_volume.MyType = ICON_TYPES["STAT"]
  248. sound_volume.Parent = self
  249. sound_volume.ImageName = self.icon_base_path+"soundvolume.png"
  250. sound_volume.Adjust(start_x+self.IconWidth+self.IconWidth+8,self.IconHeight/2+(self.BarHeight-self.IconHeight)/2,self.IconWidth,self.IconHeight,0)
  251. self.Icons["soundvolume"] = sound_volume
  252. self.SyncSoundVolume()
  253. round_corners := NewTitleBarIconItem()
  254. round_corners.IconWidth = 10
  255. round_corners.IconHeight = 10
  256. round_corners.MyType = ICON_TYPES["STAT"]
  257. round_corners.Parent = self
  258. round_corners.ImgSurf = MyIconPool.GetImgSurf("roundcorners")
  259. round_corners.Adjust(0,0,10,10,0)
  260. self.Icons["round_corners"] = round_corners
  261. if self.DBusManager.IsWifiConnectedNow() {
  262. print("wifi is connected")
  263. print( self.DBusManager.WifiStrength())
  264. }else {
  265. cmd := "sudo rfkill list | grep yes | cut -d \" \" -f3" //make sure sudo rfkill needs no password
  266. out, err := exec.Command("bash", "-c", cmd).Output()
  267. if err != nil {
  268. fmt.Printf("Failed to execute command: %s\n", cmd)
  269. }else {
  270. outs := strings.Split(string(out),"\n")
  271. if len(outs) > 0 && outs[0] == "yes" {
  272. self.InAirPlaneMode = true
  273. }else{
  274. self.InAirPlaneMode = false
  275. }
  276. }
  277. }
  278. }
  279. func (self *TitleBar) ClearCanvas() {
  280. surface.Fill(self.CanvasHWND, self.SkinManager.GiveColor("TitleBg"))
  281. self.Icons["round_corners"].NewCoord(5,5)
  282. self.Icons["round_corners"].SetIconIndex(0)
  283. self.Icons["round_corners"].Draw()
  284. self.Icons["round_corners"].NewCoord(self.Width-5, 5)
  285. self.Icons["round_corners"].SetIconIndex(1)
  286. self.Icons["round_corners"].Draw()
  287. }
  288. func (self *TitleBar) Draw(title string) {
  289. self.ClearCanvas()
  290. self.Title = title
  291. cur_time := jodaTime.Format("HH:mm", gotime.Now())
  292. time_text_w, time_text_h := font.Size(self.TimeFont, cur_time)
  293. title_text_w, title_text_h := font.Size(self.TitleFont, self.Title)
  294. title_text_surf := font.Render(self.TitleFont, self.Title, true, self.SkinManager.GiveColor("Text"),nil)
  295. surface.Blit(self.CanvasHWND,title_text_surf, draw.MidRect(title_text_w/2+self.LOffset,title_text_h/2+(self.BarHeight-title_text_h)/2,title_text_w,title_text_h,Width,Height),nil)
  296. time_text_surf := font.Render(self.TimeFont, cur_time,true,self.SkinManager.GiveColor("Text"),nil)
  297. surface.Blit(self.CanvasHWND, time_text_surf, draw.MidRect(Width-time_text_w/2-self.ROffset, time_text_h/2+(self.BarHeight-time_text_h)/2, time_text_w,time_text_h,Width,Height),nil)
  298. start_x := Width - time_text_w - self.ROffset - self.IconWidth*3 // close to the time_text
  299. self.Icons["sound"].NewCoord( start_x, self.IconHeight/2+ (self.BarHeight-self.IconHeight)/2)
  300. self.Icons["battery"].NewCoord(start_x+self.IconWidth+self.IconWidth+8, self.IconHeight/2+(self.BarHeight-self.IconHeight)/2)
  301. if self.DBusManager.IsWifiConnectedNow() == true {
  302. ge := self.GetWifiStrength( self.DBusManager.WifiStrength() )
  303. //fmt.Println("wifi ge: ",ge)
  304. if ge > 0 {
  305. self.Icons["wifistatus"].SetIconIndex(ge)
  306. self.Icons["wifistatus"].NewCoord(start_x+self.IconWidth+5, self.IconHeight/2+(self.BarHeight-self.IconHeight)/2 )
  307. self.Icons["wifistatus"].Draw()
  308. }else {
  309. self.Icons["wifistatus"].SetIconIndex(0)
  310. self.Icons["wifistatus"].Draw()
  311. }
  312. }else {
  313. self.Icons["wifistatus"].SetIconIndex(0)
  314. self.Icons["wifistatus"].NewCoord(start_x+self.IconWidth+5, self.IconHeight/2+(self.BarHeight-self.IconHeight)/2)
  315. self.Icons["wifistatus"].Draw()
  316. }
  317. self.Icons["sound"].Draw()
  318. self.Icons["battery"].Draw()
  319. draw.Line(self.CanvasHWND,self.SkinManager.GiveColor("Line"), 0,self.BarHeight,self.Width,self.BarHeight, self.BorderWidth)
  320. if self.HWND != nil {
  321. rect_ := rect.Rect(self.PosX,self.PosY, self.Width,self.Height)
  322. surface.Blit(self.HWND, self.CanvasHWND, &rect_, nil)
  323. }
  324. }