title_bar.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. # -*- coding: utf-8 -*-
  2. import pygame
  3. import os
  4. import sys
  5. import commands
  6. from datetime import datetime
  7. #from beeprint import pp
  8. import alsaaudio
  9. ##local import
  10. from constants import ICON_TYPES,Width,Height
  11. from icon_item import IconItem
  12. from multi_icon_item import MultiIconItem
  13. from icon_pool import MyIconPool
  14. from lang_manager import MyLangManager
  15. from util_funcs import midRect,SwapAndShow,FileExists
  16. from skin_manager import MySkinManager
  17. from widget import Widget
  18. from config import Battery,RPC
  19. from libs.roundrects import aa_round_rect
  20. from libs.DBUS import is_wifi_connected_now,wifi_strength
  21. icon_base_path = MySkinManager.GiveIcon("gameshell/titlebar_icons/")
  22. class TitleBar(Widget):
  23. _Width = Width
  24. _Height = 25
  25. _BarHeight = 24.5
  26. _LOffset = 3
  27. _ROffset = 3
  28. _Icons = {}
  29. _icon_width = 18
  30. _icon_height = 18
  31. _BorderWidth = 1
  32. _CanvasHWND = None
  33. _HWND = None
  34. _Title = ""
  35. _InLowBackLight = -1
  36. _SkinManager = None
  37. _InAirPlaneMode = False
  38. def __init__(self):
  39. self._Icons = {}
  40. def Redraw(self):
  41. #self.CheckBatteryStat()
  42. #self.SyncSoundVolume()
  43. #self.CheckBluetooth()
  44. #self.UpdateWifiStrength()
  45. self.UpdateDownloadStatus()
  46. SwapAndShow()
  47. def GObjectRoundRobin(self):
  48. if self._InLowBackLight < 0:
  49. self.CheckBatteryStat()
  50. self.SyncSoundVolume()
  51. self.CheckBluetooth()
  52. self.UpdateWifiStrength()
  53. self.UpdateDownloadStatus()
  54. SwapAndShow()
  55. # print("TitleBar Gobjectroundrobin")
  56. elif self._InLowBackLight >= 0:
  57. self._InLowBackLight+=1
  58. if self._InLowBackLight > 10:
  59. self.CheckBatteryStat()
  60. self.SyncSoundVolume()
  61. self.CheckBluetooth()
  62. self.UpdateWifiStrength()
  63. self.UpdateDownloadStatus()
  64. SwapAndShow()
  65. self._InLowBackLight = 0
  66. return True
  67. def UpdateDownloadStatus(self):
  68. resp = RPC.getGlobalStat()
  69. if( int(resp["numActive"]) > 0):
  70. self._Icons["dlstatus"]._IconIndex = 1
  71. elif( int(resp["numActive"]) == 0):
  72. self._Icons["dlstatus"]._IconIndex = 0
  73. def UpdateWifiStrength(self):
  74. self.Draw(self._Title)
  75. def GetWifiStrength(self,stren): ##invoke anytime to change title bar icon status
  76. #0-25
  77. #25-50
  78. #50-75
  79. #75-100
  80. segs = [[-2,-1], [0,25],[25,50],[50,75],[75,100] ]
  81. stren = int(stren)
  82. ge = 0
  83. if stren == 0:
  84. return ge
  85. for i,v in enumerate(segs):
  86. if stren >= v[0] and stren <= v[1]:
  87. ge = i
  88. break
  89. return ge
  90. def SyncSoundVolume(self):
  91. try:
  92. m = alsaaudio.Mixer()
  93. vol = m.getvolume()[0]
  94. except Exception,e:
  95. print(str(e))
  96. vol = 0
  97. snd_segs = [ [0,10],[10,30],[30,70],[70,100] ]
  98. ge = 0
  99. for i,v in enumerate(snd_segs):
  100. if vol >= v[0] and vol <= v[1]:
  101. ge = i
  102. break
  103. self._Icons["soundvolume"]._IconIndex = ge
  104. self._Icons["sound"] = self._Icons["soundvolume"]
  105. def SetSoundVolume(self,vol):
  106. pass
  107. def CheckBatteryStat(self): ## 100ms check interval
  108. content = ""
  109. bat_uevent = {}
  110. bat_segs=[ [0,6],[7,15],[16,20], [21,30],[31,50],[51,60], [61,80],[81,90],[91,100] ]
  111. try:
  112. f = open(Battery)
  113. except IOError:
  114. self._Icons["battery"] = self._Icons["battery_unknown"]
  115. # print("CheckBatteryStat open failed")
  116. return False
  117. else:
  118. with f:
  119. content = f.readlines()
  120. content = [x.strip() for x in content]
  121. f.close()
  122. for i in content:
  123. pis = i.split("=")
  124. if len(pis) > 1:
  125. bat_uevent[pis[0]] = pis[1]
  126. # print(bat_uevent["POWER_SUPPLY_CAPACITY"])
  127. """
  128. power_current = int(bat_uevent["POWER_SUPPLY_CURRENT_NOW"])
  129. if power_current < 270000:
  130. self._Icons["battery"] = self._Icons["battery_unknown"]
  131. return False
  132. """
  133. if "POWER_SUPPLY_CAPACITY" in bat_uevent:
  134. cur_cap = int(bat_uevent["POWER_SUPPLY_CAPACITY"])
  135. else:
  136. cur_cap = 0
  137. cap_ge = 0
  138. for i,v in enumerate(bat_segs):
  139. if cur_cap >= v[0] and cur_cap <= v[1]:
  140. cap_ge = i
  141. break
  142. if bat_uevent["POWER_SUPPLY_STATUS"] == "Charging":
  143. self._Icons["battery_charging"]._IconIndex = cap_ge
  144. self._Icons["battery"] = self._Icons["battery_charging"]
  145. #print("Charging %d" % cap_ge)
  146. else:
  147. self._Icons["battery_discharging"]._IconIndex = cap_ge
  148. self._Icons["battery"] = self._Icons["battery_discharging"]
  149. #print("Discharging %d" % cap_ge)
  150. return True
  151. def SetBatteryStat(self,bat):
  152. pass
  153. def CheckBluetooth(self):
  154. out = commands.getstatusoutput("hcitool dev | grep hci0 |cut -f3")
  155. if len(out[1]) < 17:
  156. print("CheckBluetooth:no bluetooth", out)
  157. self._Icons["bluetooth"]._IconIndex = 2
  158. return
  159. else:
  160. out = commands.getstatusoutput("sudo rfkill list | grep hci0 -A 2 | grep yes")
  161. if len(out[1]) > 10:
  162. self._Icons["bluetooth"]._IconIndex = 1
  163. return
  164. self._Icons["bluetooth"]._IconIndex = 0
  165. def Init(self,screen):
  166. start_x = 0
  167. self._CanvasHWND = pygame.Surface((self._Width,self._Height))
  168. self._HWND = screen
  169. icon_wifi_status = MultiIconItem()
  170. icon_wifi_status._MyType = ICON_TYPES["STAT"]
  171. icon_wifi_status._ImageName = icon_base_path+"wifi.png"
  172. icon_wifi_status._Parent = self
  173. icon_wifi_status.Adjust(start_x+self._icon_width+5,self._icon_height/2+(self._BarHeight-self._icon_height)/2,self._icon_width,self._icon_height,0)
  174. self._Icons["wifistatus"] = icon_wifi_status
  175. battery_charging = MultiIconItem()
  176. battery_charging._MyType = ICON_TYPES["STAT"]
  177. battery_charging._Parent = self
  178. battery_charging._ImageName = icon_base_path+"withcharging.png"
  179. battery_charging.Adjust(start_x+self._icon_width+self._icon_width+8,self._icon_height/2+(self._BarHeight-self._icon_height)/2,self._icon_width,self._icon_height,0)
  180. self._Icons["battery_charging"] = battery_charging
  181. battery_discharging = MultiIconItem()
  182. battery_discharging._MyType = ICON_TYPES["STAT"]
  183. battery_discharging._Parent = self
  184. battery_discharging._ImageName = icon_base_path+"without_charging.png"
  185. battery_discharging.Adjust(start_x+self._icon_width+self._icon_width+8,self._icon_height/2+(self._BarHeight-self._icon_height)/2,self._icon_width,self._icon_height,0)
  186. self._Icons["battery_discharging"] = battery_discharging
  187. battery_unknown = IconItem()
  188. battery_unknown._MyType = ICON_TYPES["STAT"]
  189. battery_unknown._Parent = self
  190. battery_unknown._ImageName = icon_base_path+"battery_unknown.png"
  191. battery_unknown.Adjust(start_x+self._icon_width+self._icon_width+8,self._icon_height/2+(self._BarHeight-self._icon_height)/2,self._icon_width,self._icon_height,0)
  192. self._Icons["battery_unknown"] = battery_unknown
  193. self.CheckBatteryStat()
  194. sound_volume = MultiIconItem()
  195. sound_volume._MyType = ICON_TYPES["STAT"]
  196. sound_volume._Parent = self
  197. sound_volume._ImageName = icon_base_path+"soundvolume.png"
  198. sound_volume.Adjust(start_x+self._icon_width+self._icon_width+8,self._icon_height/2+(self._BarHeight-self._icon_height)/2,self._icon_width,self._icon_height,0)
  199. self._Icons["soundvolume"] = sound_volume
  200. self.SyncSoundVolume()
  201. bluetooth = MultiIconItem()
  202. bluetooth._MyType = ICON_TYPES["STAT"]
  203. bluetooth._Parent = self
  204. bluetooth._ImageName = icon_base_path+"bluetooth.png"
  205. bluetooth.Adjust(start_x+self._icon_width+self._icon_width+8,self._icon_height/2+(self._BarHeight-self._icon_height)/2,self._icon_width,self._icon_height,0)
  206. self._Icons["bluetooth"] = bluetooth
  207. self.CheckBluetooth()
  208. round_corners = MultiIconItem()
  209. round_corners._IconWidth = 10
  210. round_corners._IconHeight = 10
  211. round_corners._MyType = ICON_TYPES["STAT"]
  212. round_corners._Parent = self
  213. round_corners._ImgSurf = MyIconPool.GiveIconSurface("roundcorners")
  214. round_corners.Adjust(0,0,10,10,0)
  215. self._Icons["round_corners"] = round_corners
  216. dlstatus = MultiIconItem()
  217. dlstatus._MyType = ICON_TYPES["STAT"]
  218. dlstatus._Parent = self
  219. if FileExists(icon_base_path+"dlstatus18.png"):
  220. dlstatus._ImageName = icon_base_path+"dlstatus18.png"
  221. dlstatus.Adjust(start_x+self._icon_width+self._icon_width+8,self._icon_height/2+(self._BarHeight-self._icon_height)/2,self._icon_width,self._icon_height,0)
  222. self._Icons["dlstatus"] = dlstatus
  223. self.UpdateDownloadStatus()
  224. if is_wifi_connected_now():
  225. print("wifi is connected")
  226. print( wifi_strength())
  227. else:
  228. out = commands.getstatusoutput('sudo rfkill list | grep yes | cut -d " " -f3')
  229. if out[1] == "yes":
  230. self._InAirPlaneMode = True
  231. else:
  232. self._InAirPlaneMode = False
  233. def ClearCanvas(self):
  234. self._CanvasHWND.fill( self._SkinManager.GiveColor("TitleBg") )
  235. self._Icons["round_corners"].NewCoord(5,5)
  236. self._Icons["round_corners"]._IconIndex = 0
  237. self._Icons["round_corners"].Draw()
  238. self._Icons["round_corners"].NewCoord(self._Width-5,5)
  239. self._Icons["round_corners"]._IconIndex = 1
  240. self._Icons["round_corners"].Draw()
  241. """
  242. aa_round_rect(self._CanvasHWND,
  243. (0,0,self._Width,self._Height),self._BgColor,8,0, self._BgColor)
  244. pygame.draw.rect(self._CanvasHWND,self._BgColor,(0,self._Height/2,Width,self._BarHeight), 0 )
  245. """
  246. def Draw(self,title):
  247. self.ClearCanvas()
  248. title = MyLangManager.Tr(title)
  249. self._Title = title
  250. # get battery percentage, but not for music spectrum(GameShell RTA)
  251. RTA_title = "GameShell RTA"
  252. bat_pct = ""
  253. if title != RTA_title and title != MyLangManager.Tr(RTA_title):
  254. out = commands.getstatusoutput("upower -i /org/freedesktop/UPower/devices/battery_axp20x_battery | grep percentage | tail -c 5")
  255. bat_pct = "".join(out[1]).strip() + " "
  256. cur_time = datetime.now().strftime("%H:%M")
  257. cur_time = bat_pct + cur_time
  258. time_text_font = MySkinManager.GiveFont("Eurostile12")
  259. time_text_size = time_text_font.size(cur_time)
  260. title_text_size = MyLangManager.TrFont("Eurostile16").size(title)
  261. self._CanvasHWND.blit(MyLangManager.TrFont("Eurostile16").render(title,True,self._SkinManager.GiveColor("Text")),midRect(title_text_size[0]/2+self._LOffset,
  262. title_text_size[1]/2+(self._BarHeight-title_text_size[1])/2,
  263. title_text_size[0],title_text_size[1],Width,Height))
  264. self._CanvasHWND.blit( time_text_font.render(cur_time,True,self._SkinManager.GiveColor("Text")),midRect(Width-time_text_size[0]/2-self._ROffset,
  265. time_text_size[1]/2+(self._BarHeight-time_text_size[1])/2,
  266. time_text_size[0],time_text_size[1],Width,Height))
  267. start_x = Width-time_text_size[0]-self._ROffset-self._icon_width*3 # near by the time_text
  268. self._Icons["dlstatus"].NewCoord(start_x - self._icon_width*2,self._icon_height/2+(self._BarHeight-self._icon_height)/2)
  269. self._Icons["bluetooth"].NewCoord(start_x - self._icon_width,self._icon_height/2+(self._BarHeight-self._icon_height)/2)
  270. self._Icons["sound"].NewCoord(start_x, self._icon_height/2+(self._BarHeight-self._icon_height)/2)
  271. #self._Icons["wifi"].NewCoord(start_x+self._icon_width+5, self._icon_height/2+(self._BarHeight-self._icon_height)/2)
  272. self._Icons["battery"].NewCoord(start_x+self._icon_width+self._icon_width+8,self._icon_height/2+(self._BarHeight-self._icon_height)/2)
  273. if is_wifi_connected_now():
  274. ge = self.GetWifiStrength(wifi_strength())
  275. if ge > 0:
  276. self._Icons["wifistatus"]._IconIndex = ge
  277. self._Icons["wifistatus"].NewCoord(start_x+self._icon_width+5,self._icon_height/2+(self._BarHeight-self._icon_height)/2)
  278. self._Icons["wifistatus"].Draw()
  279. else:
  280. self._Icons["wifistatus"]._IconIndex = 0
  281. self._Icons["wifistatus"].Draw()
  282. print("wifi strength error")
  283. else:
  284. if self._InAirPlaneMode == False:
  285. self._Icons["wifistatus"]._IconIndex = 0
  286. else:
  287. self._Icons["wifistatus"]._IconIndex = 5 ## airplane mode icon
  288. self._Icons["wifistatus"].NewCoord(start_x+self._icon_width+5,self._icon_height/2+(self._BarHeight-self._icon_height)/2)
  289. self._Icons["wifistatus"].Draw()
  290. self._Icons["sound"].Draw()
  291. self._Icons["battery"].Draw()
  292. self._Icons["bluetooth"].Draw()
  293. #self._Icons["dlstatus"].Draw()
  294. pygame.draw.line(self._CanvasHWND,self._SkinManager.GiveColor("Line"),(0,self._BarHeight),(self._Width,self._BarHeight),self._BorderWidth)
  295. if self._HWND != None:
  296. self._HWND.blit(self._CanvasHWND,(self._PosX,self._PosY,self._Width,self._Height))