__init__.py 11 KB

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