Advertisement
Guest User

Untitled

a guest
Feb 6th, 2019
250
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.31 KB | None
  1. # -*- coding: utf-8 -*-
  2.  
  3. import pygame
  4. import os
  5. import sys
  6. import commands
  7.  
  8. from datetime import datetime
  9.  
  10. #from beeprint import pp
  11.  
  12. import alsaaudio
  13.  
  14. ##local import
  15. from constants import ICON_TYPES,Width,Height
  16. from fonts import fonts
  17. from icon_item import IconItem
  18. from multi_icon_item import MultiIconItem
  19. from icon_pool import MyIconPool
  20. from lang_manager import MyLangManager
  21. from util_funcs import midRect,SwapAndShow,SkinMap
  22. from widget import Widget
  23. from config import Battery
  24.  
  25. from libs.roundrects import aa_round_rect
  26.  
  27. from libs.DBUS import is_wifi_connected_now,wifi_strength
  28.  
  29. icon_base_path = SkinMap("gameshell/titlebar_icons/")
  30. class TitleBar(Widget):
  31. _Width = Width
  32. _Height = 25
  33. _BarHeight = 24.5
  34. _LOffset = 3
  35. _ROffset = 3
  36. _Icons = {}
  37. _icon_width = 18
  38. _icon_height = 18
  39. _BorderWidth = 1
  40. _CanvasHWND = None
  41. _HWND = None
  42. _Title = ""
  43.  
  44. _Battlevel = "0"
  45.  
  46. _InLowBackLight = -1
  47.  
  48. _SkinManager = None
  49.  
  50. _InAirPlaneMode = False
  51.  
  52. def __init__(self):
  53. self._Icons = {}
  54.  
  55.  
  56. def GObjectRoundRobin(self):
  57. if self._InLowBackLight < 0:
  58. self.CheckBatteryStat()
  59. self.SyncSoundVolume()
  60. self.CheckBluetooth()
  61. self.UpdateWifiStrength()
  62.  
  63. SwapAndShow()
  64. # print("TitleBar Gobjectroundrobin")
  65. elif self._InLowBackLight >= 0:
  66. self._InLowBackLight+=1
  67. if self._InLowBackLight > 10:
  68. self.CheckBatteryStat()
  69. self.SyncSoundVolume()
  70. self.CheckBluetooth()
  71. self.UpdateWifiStrength()
  72.  
  73. self._InLowBackLight = 0
  74.  
  75. return True
  76.  
  77. def UpdateWifiStrength(self):
  78. self.Draw(self._Title)
  79.  
  80. def GetWifiStrength(self,stren): ##invoke anytime to change title bar icon status
  81. #0-25
  82. #25-50
  83. #50-75
  84. #75-100
  85.  
  86. segs = [[-2,-1], [0,25],[25,50],[50,75],[75,100] ]
  87.  
  88. stren = int(stren)
  89. ge = 0
  90.  
  91. if stren == 0:
  92. return ge
  93.  
  94. for i,v in enumerate(segs):
  95. if stren >= v[0] and stren <= v[1]:
  96. ge = i
  97. break
  98.  
  99. return ge
  100.  
  101. def SyncSoundVolume(self):
  102. try:
  103. m = alsaaudio.Mixer()
  104. vol = m.getvolume()[0]
  105. except Exception,e:
  106. print(str(e))
  107. vol = 0
  108.  
  109. snd_segs = [ [0,10],[10,30],[30,70],[70,100] ]
  110.  
  111. ge = 0
  112. for i,v in enumerate(snd_segs):
  113. if vol >= v[0] and vol <= v[1]:
  114. ge = i
  115. break
  116.  
  117. self._Icons["soundvolume"]._IconIndex = ge
  118. self._Icons["sound"] = self._Icons["soundvolume"]
  119.  
  120.  
  121. def SetSoundVolume(self,vol):
  122. pass
  123.  
  124. def CheckBatteryStat(self): ## 100ms check interval
  125. content = ""
  126. bat_uevent = {}
  127. bat_segs=[ [0,6],[7,15],[16,20], [21,30],[31,50],[51,60], [61,80],[81,90],[91,100] ]
  128.  
  129. try:
  130. f = open(Battery)
  131. except IOError:
  132. self._Icons["battery"] = self._Icons["battery_unknown"]
  133. # print("CheckBatteryStat open failed")
  134. return False
  135. else:
  136. with f:
  137. content = f.readlines()
  138. content = [x.strip() for x in content]
  139. f.close()
  140.  
  141. for i in content:
  142. pis = i.split("=")
  143. if len(pis) > 1:
  144. bat_uevent[pis[0]] = pis[1]
  145.  
  146. # print(bat_uevent["POWER_SUPPLY_CAPACITY"])
  147.  
  148. """
  149. power_current = int(bat_uevent["POWER_SUPPLY_CURRENT_NOW"])
  150. if power_current < 270000:
  151. self._Icons["battery"] = self._Icons["battery_unknown"]
  152. return False
  153. """
  154.  
  155. if "POWER_SUPPLY_CAPACITY" in bat_uevent:
  156. cur_cap = int(bat_uevent["POWER_SUPPLY_CAPACITY"])
  157. self._Battlevel = bat_uevent["POWER_SUPPLY_CAPACITY"] + "% "
  158. else:
  159. cur_cap = 0
  160.  
  161. cap_ge = 0
  162.  
  163. for i,v in enumerate(bat_segs):
  164. if cur_cap >= v[0] and cur_cap <= v[1]:
  165. cap_ge = i
  166. break
  167.  
  168. if bat_uevent["POWER_SUPPLY_STATUS"] == "Charging":
  169. self._Icons["battery_charging"]._IconIndex = cap_ge
  170. self._Icons["battery"] = self._Icons["battery_charging"]
  171.  
  172. print("Charging %d" % cap_ge)
  173. else:
  174. self._Icons["battery_discharging"]._IconIndex = cap_ge
  175. self._Icons["battery"] = self._Icons["battery_discharging"]
  176. print("Discharging %d" % cap_ge)
  177.  
  178.  
  179. return True
  180.  
  181. def SetBatteryStat(self,bat):
  182. pass
  183.  
  184. def CheckBluetooth(self):
  185. out = commands.getstatusoutput("hcitool dev | grep hci0 |cut -f3")
  186. if len(out[1]) < 17:
  187. print("CheckBluetooth:no bluetooth", out)
  188. self._Icons["bluetooth"]._IconIndex = 2
  189. return
  190. else:
  191. out = commands.getstatusoutput("sudo rfkill list | grep hci0 -A 2 | grep yes")
  192. if len(out[1]) > 10:
  193. self._Icons["bluetooth"]._IconIndex = 1
  194. return
  195.  
  196. self._Icons["bluetooth"]._IconIndex = 0
  197.  
  198. def Init(self,screen):
  199.  
  200.  
  201. start_x = 0
  202. self._CanvasHWND = pygame.Surface((self._Width,self._Height))
  203. self._HWND = screen
  204.  
  205.  
  206. icon_wifi_status = MultiIconItem()
  207. icon_wifi_status._MyType = ICON_TYPES["STAT"]
  208. icon_wifi_status._ImageName = icon_base_path+"wifi.png"
  209. icon_wifi_status._Parent = self
  210. 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)
  211. self._Icons["wifistatus"] = icon_wifi_status
  212.  
  213. battery_charging = MultiIconItem()
  214. battery_charging._MyType = ICON_TYPES["STAT"]
  215. battery_charging._Parent = self
  216. battery_charging._ImageName = icon_base_path+"withcharging.png"
  217. 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)
  218.  
  219. self._Icons["battery_charging"] = battery_charging
  220.  
  221. battery_discharging = MultiIconItem()
  222. battery_discharging._MyType = ICON_TYPES["STAT"]
  223. battery_discharging._Parent = self
  224. battery_discharging._ImageName = icon_base_path+"without_charging.png"
  225. 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)
  226.  
  227. self._Icons["battery_discharging"] = battery_discharging
  228.  
  229. battery_unknown = IconItem()
  230. battery_unknown._MyType = ICON_TYPES["STAT"]
  231. battery_unknown._Parent = self
  232. battery_unknown._ImageName = icon_base_path+"battery_unknown.png"
  233. 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)
  234.  
  235. self._Icons["battery_unknown"] = battery_unknown
  236.  
  237. self.CheckBatteryStat()
  238.  
  239. sound_volume = MultiIconItem()
  240. sound_volume._MyType = ICON_TYPES["STAT"]
  241. sound_volume._Parent = self
  242. sound_volume._ImageName = icon_base_path+"soundvolume.png"
  243. 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)
  244.  
  245. self._Icons["soundvolume"] = sound_volume
  246.  
  247. self.SyncSoundVolume()
  248.  
  249.  
  250. bluetooth = MultiIconItem()
  251. bluetooth._MyType = ICON_TYPES["STAT"]
  252. bluetooth._Parent = self
  253. bluetooth._ImageName = icon_base_path+"bluetooth.png"
  254. 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)
  255.  
  256. self._Icons["bluetooth"] = bluetooth
  257. self.CheckBluetooth()
  258.  
  259. round_corners = MultiIconItem()
  260. round_corners._IconWidth = 10
  261. round_corners._IconHeight = 10
  262.  
  263. round_corners._MyType = ICON_TYPES["STAT"]
  264. round_corners._Parent = self
  265. round_corners._ImgSurf = MyIconPool._Icons["roundcorners"]
  266. round_corners.Adjust(0,0,10,10,0)
  267.  
  268. self._Icons["round_corners"] = round_corners
  269.  
  270. if is_wifi_connected_now():
  271. print("wifi is connected")
  272. print( wifi_strength())
  273. else:
  274. out = commands.getstatusoutput('sudo rfkill list | grep yes | cut -d " " -f3')
  275. if out[1] == "yes":
  276. self._InAirPlaneMode = True
  277. else:
  278. self._InAirPlaneMode = False
  279.  
  280. def ClearCanvas(self):
  281. self._CanvasHWND.fill( self._SkinManager.GiveColor("TitleBg") )
  282.  
  283. self._Icons["round_corners"].NewCoord(5,5)
  284. self._Icons["round_corners"]._IconIndex = 0
  285. self._Icons["round_corners"].Draw()
  286.  
  287. self._Icons["round_corners"].NewCoord(self._Width-5,5)
  288. self._Icons["round_corners"]._IconIndex = 1
  289. self._Icons["round_corners"].Draw()
  290.  
  291.  
  292. """
  293. aa_round_rect(self._CanvasHWND,
  294. (0,0,self._Width,self._Height),self._BgColor,8,0, self._BgColor)
  295.  
  296. pygame.draw.rect(self._CanvasHWND,self._BgColor,(0,self._Height/2,Width,self._BarHeight), 0 )
  297. """
  298.  
  299. def Draw(self,title):
  300. self.ClearCanvas()
  301. title = MyLangManager.Tr(title)
  302. self._Title = title
  303.  
  304. cur_time = datetime.now().strftime("%H:%M")
  305. #cur_battery = Battery.
  306. cur_battery = self._Battlevel
  307. header_text = cur_battery + cur_time
  308. time_text_size = fonts["varela12"].size(header_text)
  309. title_text_size = MyLangManager.TrFont("varela16").size(title)
  310.  
  311. self._CanvasHWND.blit(MyLangManager.TrFont("varela16").render(title,True,self._SkinManager.GiveColor("Text")),midRect(title_text_size[0]/2+self._LOffset,
  312. title_text_size[1]/2+(self._BarHeight-title_text_size[1])/2,
  313. title_text_size[0],title_text_size[1],Width,Height))
  314. self._CanvasHWND.blit( fonts["varela12"].render(header_text,True,self._SkinManager.GiveColor("Text")),midRect(Width-time_text_size[0]/2-self._ROffset,
  315. time_text_size[1]/2+(self._BarHeight-time_text_size[1])/2,
  316. time_text_size[0],time_text_size[1],Width,Height))
  317.  
  318. start_x = Width-time_text_size[0]-self._ROffset-self._icon_width*3 # near by the time_text
  319.  
  320. self._Icons["bluetooth"].NewCoord(start_x - self._icon_width,self._icon_height/2+(self._BarHeight-self._icon_height)/2)
  321.  
  322. self._Icons["sound"].NewCoord(start_x, self._icon_height/2+(self._BarHeight-self._icon_height)/2)
  323.  
  324. #self._Icons["wifi"].NewCoord(start_x+self._icon_width+5, self._icon_height/2+(self._BarHeight-self._icon_height)/2)
  325.  
  326. self._Icons["battery"].NewCoord(start_x+self._icon_width+self._icon_width+8,self._icon_height/2+(self._BarHeight-self._icon_height)/2)
  327.  
  328.  
  329. if is_wifi_connected_now():
  330. ge = self.GetWifiStrength(wifi_strength())
  331. if ge > 0:
  332. self._Icons["wifistatus"]._IconIndex = ge
  333. self._Icons["wifistatus"].NewCoord(start_x+self._icon_width+5,self._icon_height/2+(self._BarHeight-self._icon_height)/2)
  334. self._Icons["wifistatus"].Draw()
  335. else:
  336. self._Icons["wifistatus"]._IconIndex = 0
  337. self._Icons["wifistatus"].Draw()
  338. print("wifi strength error")
  339. else:
  340. if self._InAirPlaneMode == False:
  341. self._Icons["wifistatus"]._IconIndex = 0
  342. else:
  343. self._Icons["wifistatus"]._IconIndex = 5 ## airplane mode icon
  344.  
  345. self._Icons["wifistatus"].NewCoord(start_x+self._icon_width+5,self._icon_height/2+(self._BarHeight-self._icon_height)/2)
  346. self._Icons["wifistatus"].Draw()
  347.  
  348. self._Icons["sound"].Draw()
  349.  
  350. self._Icons["battery"].Draw()
  351.  
  352. self._Icons["bluetooth"].Draw()
  353.  
  354. pygame.draw.line(self._CanvasHWND,self._SkinManager.GiveColor("Line"),(0,self._BarHeight),(self._Width,self._BarHeight),self._BorderWidth)
  355.  
  356. if self._HWND != None:
  357. self._HWND.blit(self._CanvasHWND,(self._PosX,self._PosY,self._Width,self._Height))
Advertisement
RAW Paste Data Copied
Advertisement