__init__.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. # -*- coding: utf-8 -*-
  2. import pygame
  3. #import math
  4. import subprocess
  5. #from beeprint import pp
  6. from libs.roundrects import aa_round_rect
  7. #import gobject
  8. #from wicd import misc
  9. ## local UI import
  10. from UI.constants import Width,Height,ICON_TYPES
  11. from UI.page import Page,PageSelector
  12. from UI.label import Label
  13. from UI.util_funcs import midRect
  14. from UI.keys_def import CurKeys, IsKeyMenuOrB
  15. from UI.scroller import ListScroller
  16. from UI.icon_pool import MyIconPool
  17. from UI.icon_item import IconItem
  18. from UI.multilabel import MultiLabel
  19. from UI.lang_manager import MyLangManager
  20. from UI.skin_manager import MySkinManager
  21. class InfoPageListItem(object):
  22. _PosX = 0
  23. _PosY = 0
  24. _Width = 0
  25. _Height = 20
  26. _Labels = {}
  27. _Icons = {}
  28. _Fonts = {}
  29. _LinkObj = None
  30. def __init__(self):
  31. self._Labels = {}
  32. self._Icons = {}
  33. self._Fonts = {}
  34. def SetSmallText(self,text):
  35. l = MultiLabel()
  36. l.SetCanvasHWND(self._Parent._CanvasHWND)
  37. l.Init(text,self._Fonts["small"])
  38. self._Labels["Small"] = l
  39. #if self._Labels["Small"]._Width > self._Width:
  40. # self._Width = self._Labels["Small"]._Width
  41. if self._Labels["Small"]._Height >= self._Height:
  42. self._Height = self._Labels["Small"]._Height+10
  43. def Init(self,text):
  44. #self._Fonts["normal"] = fonts["veramono12"]
  45. l = Label()
  46. l._PosX = 10
  47. l.SetCanvasHWND(self._Parent._CanvasHWND)
  48. l.Init(text,self._Fonts["normal"])
  49. self._Labels["Text"] = l
  50. def Draw(self):
  51. self._Labels["Text"]._PosY = self._PosY
  52. self._Labels["Text"].Draw()
  53. if "Small" in self._Labels:
  54. self._Labels["Small"]._PosX = self._Labels["Text"]._Width + 16
  55. self._Labels["Small"]._PosY = self._PosY
  56. self._Labels["Small"].Draw()
  57. class AboutPage(Page):
  58. _FootMsg = ["Nav.","","","Back",""]
  59. _MyList = []
  60. _ListFontObj = MyLangManager.TrFont("varela13")
  61. _AList = {}
  62. _Scrolled = 0
  63. _BGwidth = 320
  64. _BGheight = 300
  65. _DrawOnce = False
  66. _Scroller = None
  67. def __init__(self):
  68. Page.__init__(self)
  69. self._Icons = {}
  70. def Uname(self):
  71. out = {}
  72. out["key"]="uname"
  73. out["label"]= "Kernel:"
  74. st = subprocess.check_output(["uname","-srmo"])
  75. st = st.strip("\n")
  76. st = st.strip("\t")
  77. out["value"] = st
  78. self._AList["uname"] = out
  79. return
  80. def CpuMhz(self):
  81. try:
  82. with open("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq") as f:
  83. content = f.readlines()
  84. content = [x.strip() for x in content]
  85. except:
  86. print("open %s failed" % "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq")
  87. content = ["0"]
  88. mhz = int(content[0]) / 1000.0
  89. cpuscalemhz = {}
  90. cpuscalemhz["key"] = "cpuscalemhz"
  91. cpuscalemhz["label"] = "CPU Mhz:"
  92. cpuscalemhz["value"] = str(mhz)
  93. self._AList["cpuscalemhz"] = cpuscalemhz
  94. return
  95. def CpuInfo(self):
  96. last_processor = 0
  97. with open("/proc/cpuinfo") as f:
  98. for line in f:
  99. if line.startswith("processor"):
  100. parts = line.split(":")
  101. cur_processor_number = int( parts[1].strip())
  102. if cur_processor_number > last_processor:
  103. last_processor = cur_processor_number
  104. if line.startswith("model name"):
  105. parts = line.split(":")
  106. # print( parts[1].strip() )
  107. processor = {}
  108. processor["key"]="processor"
  109. processor["label"] = "Processor:"
  110. processor["value"] = parts[1].strip()
  111. self._AList["processor"] = processor
  112. if line.startswith("cpu MHz"):
  113. parts = line.split(":")
  114. # print(parts[1].strip() )
  115. cpumhz = {}
  116. cpumhz["key"] = "cpumhz"
  117. cpumhz["label"] = "CPU MHz:"
  118. cpumhz["value"] = parts[1].strip()
  119. self._AList["cpumhz"] = cpumhz
  120. if line.startswith("cpu cores"):
  121. parts = line.split(":")
  122. # print(parts[1].strip() )
  123. cpucores = {}
  124. cpucores["key"] = "cpucores"
  125. cpucores["label"] = "CPU cores:"
  126. cpucores["value"] = parts[1].strip()
  127. self._AList["cpucores"] = cpucores
  128. if line.startswith("Features"):
  129. parts = line.split(":")
  130. # print(parts[1].strip() )
  131. f_ = {}
  132. f_["key"] = "features"
  133. f_["label"] = "Features:"
  134. f_["value"] = parts[1].strip()
  135. self._AList["features"] = f_
  136. if line.startswith("flags"):
  137. parts = line.split(":")
  138. # print(parts[1].strip() )
  139. flags = {}
  140. flags["key"] = "flags"
  141. flags["label"] = "Flags:"
  142. flags["value"] = parts[1].strip()
  143. self._AList["flags"] = flags
  144. if last_processor > 0:
  145. arm_cores = {}
  146. arm_cores["key"]= "armcores"
  147. arm_cores["label"] = "CPU cores:"
  148. arm_cores["value"] = str(last_processor + 1)
  149. self._AList["armcores"] = arm_cores
  150. def MemInfo(self):
  151. with open("/proc/meminfo") as f:
  152. for line in f:
  153. if line.startswith("MemTotal"):
  154. parts = line.split(":")
  155. parts[1] = parts[1].replace("kB","")
  156. print( parts[1].strip() )
  157. memory = {}
  158. memory["key"] = "memory"
  159. memory["label"] = "Memory:"
  160. memory["value"] = str( int(parts[1].strip())/1000.0) +" MB"
  161. self._AList["memory"] = memory
  162. break
  163. def GenList(self):
  164. self._MyList = []
  165. start_x = 0
  166. start_y = 10
  167. last_height = 0
  168. for i,u in enumerate( ["processor","armcores","cpuscalemhz","features","memory","uname"] ):
  169. #for i,u in enumerate( ["processor","cpucores","cpumhz","flags","memory","uname"] ):
  170. if u not in self._AList:
  171. continue
  172. v = self._AList[u]
  173. li = InfoPageListItem()
  174. li._Parent = self
  175. li._PosX = start_x
  176. li._PosY = start_y + last_height
  177. li._Width = Width
  178. li._Fonts["normal"] = self._ListFontObj
  179. li._Fonts["small"] = MySkinManager.GiveFont("varela12")
  180. if self._AList[u]["label"] != "":
  181. li.Init( self._AList[u]["label"] )
  182. else:
  183. li.Init( self._AList[u]["key"] )
  184. li._Flag = self._AList[u]["key"]
  185. li.SetSmallText( self._AList[u]["value"] )
  186. last_height += li._Height
  187. self._MyList.append(li)
  188. def Init(self):
  189. if self._Screen != None:
  190. if self._Screen._CanvasHWND != None and self._CanvasHWND == None:
  191. self._HWND = self._Screen._CanvasHWND
  192. self._CanvasHWND = pygame.Surface( (self._Screen._Width,self._BGheight) )
  193. self._PosX = self._Index*self._Screen._Width
  194. self._Width = self._Screen._Width ## equal to screen width
  195. self._Height = self._Screen._Height
  196. bgpng = IconItem()
  197. bgpng._ImgSurf = MyIconPool._Icons["about_bg"]
  198. bgpng._MyType = ICON_TYPES["STAT"]
  199. bgpng._Parent = self
  200. bgpng.Adjust(0,0,self._BGwidth,self._BGheight,0)
  201. self._Icons["bg"] = bgpng
  202. self.CpuInfo()
  203. self.MemInfo()
  204. self.CpuMhz()
  205. self.Uname()
  206. self.GenList()
  207. self._Scroller = ListScroller()
  208. self._Scroller._Parent = self
  209. self._Scroller._PosX = self._Width - 10
  210. self._Scroller._PosY = 2
  211. self._Scroller.Init()
  212. self._Scroller.SetCanvasHWND(self._HWND)
  213. def ScrollDown(self):
  214. dis = 10
  215. if abs(self._Scrolled) < (self._BGheight - self._Height)/2 + 50:
  216. self._PosY -= dis
  217. self._Scrolled -= dis
  218. def ScrollUp(self):
  219. dis = 10
  220. if self._PosY < 0:
  221. self._PosY += dis
  222. self._Scrolled += dis
  223. def OnLoadCb(self):
  224. self._Scrolled = 0
  225. self._PosY = 0
  226. self._DrawOnce = False
  227. def OnReturnBackCb(self):
  228. self.ReturnToUpLevelPage()
  229. self._Screen.Draw()
  230. self._Screen.SwapAndShow()
  231. def KeyDown(self,event):
  232. if IsKeyMenuOrB(event.key):
  233. self.ReturnToUpLevelPage()
  234. self._Screen.Draw()
  235. self._Screen.SwapAndShow()
  236. if event.key == CurKeys["Up"]:
  237. self.ScrollUp()
  238. self._Screen.Draw()
  239. self._Screen.SwapAndShow()
  240. if event.key == CurKeys["Down"]:
  241. self.ScrollDown()
  242. self._Screen.Draw()
  243. self._Screen.SwapAndShow()
  244. def Draw(self):
  245. if self._DrawOnce == False:
  246. self.ClearCanvas()
  247. #self._Ps.Draw()
  248. self._Icons["bg"].NewCoord(self._Width/2,self._Height/2 + (self._BGheight - Height)/2 + self._Screen._TitleBar._Height)
  249. self._Icons["bg"].Draw()
  250. for i in self._MyList:
  251. i.Draw()
  252. self._DrawOnce = True
  253. if self._HWND != None:
  254. self._HWND.fill(MySkinManager.GiveColor("White"))
  255. self._HWND.blit(self._CanvasHWND,(self._PosX,self._PosY,self._Width, self._Height ) )
  256. self._Scroller.UpdateSize(self._BGheight,abs(self._Scrolled)*3)
  257. self._Scroller.Draw()
  258. class APIOBJ(object):
  259. _Page = None
  260. def __init__(self):
  261. pass
  262. def Init(self,main_screen):
  263. self._Page = AboutPage()
  264. self._Page._Screen = main_screen
  265. self._Page._Name ="About"
  266. self._Page.Init()
  267. def API(self,main_screen):
  268. if main_screen !=None:
  269. main_screen.PushPage(self._Page)
  270. main_screen.Draw()
  271. main_screen.SwapAndShow()
  272. OBJ = APIOBJ()
  273. def Init(main_screen):
  274. OBJ.Init(main_screen)
  275. def API(main_screen):
  276. OBJ.API(main_screen)