title_bar.py 13 KB

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