title_bar.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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
  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 GObjectRoundRobin(self):
  41. if self._InLowBackLight < 0:
  42. self.CheckBatteryStat()
  43. self.SyncSoundVolume()
  44. self.CheckBluetooth()
  45. self.UpdateWifiStrength()
  46. SwapAndShow()
  47. # print("TitleBar Gobjectroundrobin")
  48. elif self._InLowBackLight >= 0:
  49. self._InLowBackLight+=1
  50. if self._InLowBackLight > 10:
  51. self.CheckBatteryStat()
  52. self.SyncSoundVolume()
  53. self.CheckBluetooth()
  54. self.UpdateWifiStrength()
  55. SwapAndShow()
  56. self._InLowBackLight = 0
  57. return True
  58. def UpdateWifiStrength(self):
  59. self.Draw(self._Title)
  60. def GetWifiStrength(self,stren): ##invoke anytime to change title bar icon status
  61. #0-25
  62. #25-50
  63. #50-75
  64. #75-100
  65. segs = [[-2,-1], [0,25],[25,50],[50,75],[75,100] ]
  66. stren = int(stren)
  67. ge = 0
  68. if stren == 0:
  69. return ge
  70. for i,v in enumerate(segs):
  71. if stren >= v[0] and stren <= v[1]:
  72. ge = i
  73. break
  74. return ge
  75. def SyncSoundVolume(self):
  76. try:
  77. m = alsaaudio.Mixer()
  78. vol = m.getvolume()[0]
  79. except Exception,e:
  80. print(str(e))
  81. vol = 0
  82. snd_segs = [ [0,10],[10,30],[30,70],[70,100] ]
  83. ge = 0
  84. for i,v in enumerate(snd_segs):
  85. if vol >= v[0] and vol <= v[1]:
  86. ge = i
  87. break
  88. self._Icons["soundvolume"]._IconIndex = ge
  89. self._Icons["sound"] = self._Icons["soundvolume"]
  90. def SetSoundVolume(self,vol):
  91. pass
  92. def CheckBatteryStat(self): ## 100ms check interval
  93. content = ""
  94. bat_uevent = {}
  95. bat_segs=[ [0,6],[7,15],[16,20], [21,30],[31,50],[51,60], [61,80],[81,90],[91,100] ]
  96. try:
  97. f = open(Battery)
  98. except IOError:
  99. self._Icons["battery"] = self._Icons["battery_unknown"]
  100. # print("CheckBatteryStat open failed")
  101. return False
  102. else:
  103. with f:
  104. content = f.readlines()
  105. content = [x.strip() for x in content]
  106. f.close()
  107. for i in content:
  108. pis = i.split("=")
  109. if len(pis) > 1:
  110. bat_uevent[pis[0]] = pis[1]
  111. # print(bat_uevent["POWER_SUPPLY_CAPACITY"])
  112. """
  113. power_current = int(bat_uevent["POWER_SUPPLY_CURRENT_NOW"])
  114. if power_current < 270000:
  115. self._Icons["battery"] = self._Icons["battery_unknown"]
  116. return False
  117. """
  118. if "POWER_SUPPLY_CAPACITY" in bat_uevent:
  119. cur_cap = int(bat_uevent["POWER_SUPPLY_CAPACITY"])
  120. else:
  121. cur_cap = 0
  122. cap_ge = 0
  123. for i,v in enumerate(bat_segs):
  124. if cur_cap >= v[0] and cur_cap <= v[1]:
  125. cap_ge = i
  126. break
  127. if bat_uevent["POWER_SUPPLY_STATUS"] == "Charging":
  128. self._Icons["battery_charging"]._IconIndex = cap_ge
  129. self._Icons["battery"] = self._Icons["battery_charging"]
  130. print("Charging %d" % cap_ge)
  131. else:
  132. self._Icons["battery_discharging"]._IconIndex = cap_ge
  133. self._Icons["battery"] = self._Icons["battery_discharging"]
  134. print("Discharging %d" % cap_ge)
  135. return True
  136. def SetBatteryStat(self,bat):
  137. pass
  138. def CheckBluetooth(self):
  139. out = commands.getstatusoutput("hcitool dev | grep hci0 |cut -f3")
  140. if len(out[1]) < 17:
  141. print("CheckBluetooth:no bluetooth", out)
  142. self._Icons["bluetooth"]._IconIndex = 2
  143. return
  144. else:
  145. out = commands.getstatusoutput("sudo rfkill list | grep hci0 -A 2 | grep yes")
  146. if len(out[1]) > 10:
  147. self._Icons["bluetooth"]._IconIndex = 1
  148. return
  149. self._Icons["bluetooth"]._IconIndex = 0
  150. def Init(self,screen):
  151. start_x = 0
  152. self._CanvasHWND = pygame.Surface((self._Width,self._Height))
  153. self._HWND = screen
  154. icon_wifi_status = MultiIconItem()
  155. icon_wifi_status._MyType = ICON_TYPES["STAT"]
  156. icon_wifi_status._ImageName = icon_base_path+"wifi.png"
  157. icon_wifi_status._Parent = self
  158. 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)
  159. self._Icons["wifistatus"] = icon_wifi_status
  160. battery_charging = MultiIconItem()
  161. battery_charging._MyType = ICON_TYPES["STAT"]
  162. battery_charging._Parent = self
  163. battery_charging._ImageName = icon_base_path+"withcharging.png"
  164. 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)
  165. self._Icons["battery_charging"] = battery_charging
  166. battery_discharging = MultiIconItem()
  167. battery_discharging._MyType = ICON_TYPES["STAT"]
  168. battery_discharging._Parent = self
  169. battery_discharging._ImageName = icon_base_path+"without_charging.png"
  170. 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)
  171. self._Icons["battery_discharging"] = battery_discharging
  172. battery_unknown = IconItem()
  173. battery_unknown._MyType = ICON_TYPES["STAT"]
  174. battery_unknown._Parent = self
  175. battery_unknown._ImageName = icon_base_path+"battery_unknown.png"
  176. 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)
  177. self._Icons["battery_unknown"] = battery_unknown
  178. self.CheckBatteryStat()
  179. sound_volume = MultiIconItem()
  180. sound_volume._MyType = ICON_TYPES["STAT"]
  181. sound_volume._Parent = self
  182. sound_volume._ImageName = icon_base_path+"soundvolume.png"
  183. 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)
  184. self._Icons["soundvolume"] = sound_volume
  185. self.SyncSoundVolume()
  186. bluetooth = MultiIconItem()
  187. bluetooth._MyType = ICON_TYPES["STAT"]
  188. bluetooth._Parent = self
  189. bluetooth._ImageName = icon_base_path+"bluetooth.png"
  190. 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)
  191. self._Icons["bluetooth"] = bluetooth
  192. self.CheckBluetooth()
  193. round_corners = MultiIconItem()
  194. round_corners._IconWidth = 10
  195. round_corners._IconHeight = 10
  196. round_corners._MyType = ICON_TYPES["STAT"]
  197. round_corners._Parent = self
  198. round_corners._ImgSurf = MyIconPool.GiveIconSurface("roundcorners")
  199. round_corners.Adjust(0,0,10,10,0)
  200. self._Icons["round_corners"] = round_corners
  201. if is_wifi_connected_now():
  202. print("wifi is connected")
  203. print( wifi_strength())
  204. else:
  205. out = commands.getstatusoutput('sudo rfkill list | grep yes | cut -d " " -f3')
  206. if out[1] == "yes":
  207. self._InAirPlaneMode = True
  208. else:
  209. self._InAirPlaneMode = False
  210. def ClearCanvas(self):
  211. self._CanvasHWND.fill( self._SkinManager.GiveColor("TitleBg") )
  212. self._Icons["round_corners"].NewCoord(5,5)
  213. self._Icons["round_corners"]._IconIndex = 0
  214. self._Icons["round_corners"].Draw()
  215. self._Icons["round_corners"].NewCoord(self._Width-5,5)
  216. self._Icons["round_corners"]._IconIndex = 1
  217. self._Icons["round_corners"].Draw()
  218. """
  219. aa_round_rect(self._CanvasHWND,
  220. (0,0,self._Width,self._Height),self._BgColor,8,0, self._BgColor)
  221. pygame.draw.rect(self._CanvasHWND,self._BgColor,(0,self._Height/2,Width,self._BarHeight), 0 )
  222. """
  223. def Draw(self,title):
  224. self.ClearCanvas()
  225. title = MyLangManager.Tr(title)
  226. self._Title = title
  227. cur_time = datetime.now().strftime("%H:%M")
  228. time_text_font = MySkinManager.GiveFont("varela12")
  229. time_text_size = time_text_font.size(cur_time)
  230. title_text_size = MyLangManager.TrFont("varela16").size(title)
  231. self._CanvasHWND.blit(MyLangManager.TrFont("varela16").render(title,True,self._SkinManager.GiveColor("Text")),midRect(title_text_size[0]/2+self._LOffset,
  232. title_text_size[1]/2+(self._BarHeight-title_text_size[1])/2,
  233. title_text_size[0],title_text_size[1],Width,Height))
  234. self._CanvasHWND.blit( time_text_font.render(cur_time,True,self._SkinManager.GiveColor("Text")),midRect(Width-time_text_size[0]/2-self._ROffset,
  235. time_text_size[1]/2+(self._BarHeight-time_text_size[1])/2,
  236. time_text_size[0],time_text_size[1],Width,Height))
  237. start_x = Width-time_text_size[0]-self._ROffset-self._icon_width*3 # near by the time_text
  238. self._Icons["bluetooth"].NewCoord(start_x - self._icon_width,self._icon_height/2+(self._BarHeight-self._icon_height)/2)
  239. self._Icons["sound"].NewCoord(start_x, self._icon_height/2+(self._BarHeight-self._icon_height)/2)
  240. #self._Icons["wifi"].NewCoord(start_x+self._icon_width+5, self._icon_height/2+(self._BarHeight-self._icon_height)/2)
  241. self._Icons["battery"].NewCoord(start_x+self._icon_width+self._icon_width+8,self._icon_height/2+(self._BarHeight-self._icon_height)/2)
  242. if is_wifi_connected_now():
  243. ge = self.GetWifiStrength(wifi_strength())
  244. if ge > 0:
  245. self._Icons["wifistatus"]._IconIndex = ge
  246. self._Icons["wifistatus"].NewCoord(start_x+self._icon_width+5,self._icon_height/2+(self._BarHeight-self._icon_height)/2)
  247. self._Icons["wifistatus"].Draw()
  248. else:
  249. self._Icons["wifistatus"]._IconIndex = 0
  250. self._Icons["wifistatus"].Draw()
  251. print("wifi strength error")
  252. else:
  253. if self._InAirPlaneMode == False:
  254. self._Icons["wifistatus"]._IconIndex = 0
  255. else:
  256. self._Icons["wifistatus"]._IconIndex = 5 ## airplane mode icon
  257. self._Icons["wifistatus"].NewCoord(start_x+self._icon_width+5,self._icon_height/2+(self._BarHeight-self._icon_height)/2)
  258. self._Icons["wifistatus"].Draw()
  259. self._Icons["sound"].Draw()
  260. self._Icons["battery"].Draw()
  261. self._Icons["bluetooth"].Draw()
  262. pygame.draw.line(self._CanvasHWND,self._SkinManager.GiveColor("Line"),(0,self._BarHeight),(self._Width,self._BarHeight),self._BorderWidth)
  263. if self._HWND != None:
  264. self._HWND.blit(self._CanvasHWND,(self._PosX,self._PosY,self._Width,self._Height))