about.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. package About
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "os/exec"
  7. "github.com/veandco/go-sdl2/ttf"
  8. "github.com/cuu/gogame/surface"
  9. "github.com/cuu/gogame/event"
  10. "github.com/cuu/gogame/rect"
  11. "github.com/cuu/gogame/color"
  12. "github.com/cuu/LauncherGoDev/sysgo/UI"
  13. )
  14. type InfoPageListItem struct{
  15. UI.Widget
  16. Labels map[string]UI.LabelInterface
  17. Icons map[string]UI.IconItemInterface
  18. Fonts map[string]*ttf.Font
  19. Parent UI.PageInterface
  20. Flag string
  21. }
  22. func NewInfoPageListItem() *InfoPageListItem {
  23. i := &InfoPageListItem{}
  24. i.Labels = make(map[string]UI.LabelInterface)
  25. i.Icons = make( map[string]UI.IconItemInterface)
  26. i.Fonts = make(map[string]*ttf.Font)
  27. i.Height = 20
  28. i.Width = 0
  29. return i
  30. }
  31. func (self *InfoPageListItem) Init(text string) {
  32. l := UI.NewLabel()
  33. l.PosX = 10
  34. l.SetCanvasHWND(self.Parent.GetCanvasHWND())
  35. l.Init(text,self.Fonts["normal"],nil)
  36. self.Labels["Text"] = l
  37. }
  38. func (self *InfoPageListItem) SetSmallText( text string) {
  39. l := UI.NewMultiLabel()
  40. l.SetCanvasHWND(self.Parent.GetCanvasHWND())
  41. l.Init(text,self.Fonts["small"],nil)
  42. self.Labels["Small"] = l
  43. _,h_ := self.Labels["Small"].Size()
  44. if h_>= self.Height {
  45. self.Height = h_ + 10
  46. }
  47. }
  48. func (self *InfoPageListItem) Draw() {
  49. x_,_ := self.Labels["Text"].Coord()
  50. self.Labels["Text"].NewCoord(x_,self.PosY)
  51. self.Labels["Text"].Draw()
  52. if _, ok := self.Labels["Small"]; ok {
  53. w_,_ := self.Labels["Text"].Size()
  54. self.Labels["Small"].NewCoord(w_+16,self.PosY)
  55. self.Labels["Small"].Draw()
  56. }
  57. }
  58. type AboutPage struct {
  59. UI.Page
  60. AList map[string]map[string]string
  61. ListFontObj *ttf.Font
  62. Scrolled int
  63. BGwidth int
  64. BGheight int
  65. DrawOnce bool
  66. Scroller *UI.ListScroller
  67. MyList []*InfoPageListItem
  68. Icons map[string]UI.IconItemInterface
  69. }
  70. func NewAboutPage() *AboutPage {
  71. p := &AboutPage{}
  72. p.FootMsg = [5]string{"Nav","","","Back",""}
  73. p.AList = make(map[string]map[string]string)
  74. p.BGwidth = 320
  75. p.BGheight = 300
  76. p.DrawOnce = false
  77. p.MyList = make([]*InfoPageListItem,0)
  78. p.ListFontObj = UI.Fonts["varela13"]
  79. p.Index = 0
  80. p.Icons = make(map[string]UI.IconItemInterface)
  81. return p
  82. }
  83. func (self *AboutPage) Uname() {
  84. out := make(map[string]string)
  85. out["key"] = "uname"
  86. out["label"] = "Kernel:"
  87. out_bytes, err := exec.Command("uname","-srmo").Output()
  88. if err != nil {
  89. fmt.Println(err)
  90. out["value"] = ""
  91. }
  92. out_str := strings.Trim(string(out_bytes), "\t\n")
  93. out["value"]= out_str
  94. self.AList["uname"] = out
  95. }
  96. func (self *AboutPage) CpuMhz() {
  97. lines, err := UI.ReadLines("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq")
  98. UI.ShowErr(err)
  99. mhz ,err := strconv.ParseInt(lines[0], 10, 64)
  100. UI.ShowErr(err)
  101. mhz_float := float64(mhz)/1000.0
  102. out := make(map[string]string)
  103. out["key"] = "cpuscalemhz"
  104. out["label"]="CPU Mhz:"
  105. out["value"] = strconv.FormatFloat(mhz_float, 'f', 2, 64)
  106. self.AList["cpuscalemhz"] = out
  107. }
  108. func (self *AboutPage) CpuInfo() {
  109. last_processor := 0
  110. if UI.FileExists("/proc/cpuinfo") == false {
  111. return
  112. }
  113. cpuinfos,err := UI.ReadLines("/proc/cpuinfo")
  114. if err != nil {
  115. fmt.Println(err)
  116. return
  117. }
  118. for _,v := range cpuinfos {
  119. if strings.HasPrefix(v,"processor") {
  120. parts := strings.Split(v,":")
  121. if cur_processor_number,err := strconv.Atoi(strings.Trim(parts[1],"\r\n ")); err == nil {
  122. if cur_processor_number > last_processor {
  123. last_processor = cur_processor_number
  124. }
  125. }else {
  126. fmt.Println(err)
  127. }
  128. }
  129. if strings.HasPrefix(v,"model name") {
  130. parts := strings.Split(v,":")
  131. processor := make(map[string]string)
  132. processor["key"] = "processor"
  133. processor["label"] = "Processor:"
  134. processor["value"] = strings.Trim(parts[1],"\r\n ")
  135. self.AList["processor"] = processor
  136. }
  137. if strings.HasPrefix(v,"cpu MHz") {
  138. parts := strings.Split(v,":")
  139. cpumhz := make(map[string]string)
  140. cpumhz["key"] = "cpumhz"
  141. cpumhz["label"] = "CPU MHz:"
  142. cpumhz["value"] = strings.Trim(parts[1],"\r\n ")
  143. self.AList["cpumhz"] = cpumhz
  144. }
  145. if strings.HasPrefix(v,"cpu cores") {
  146. parts := strings.Split(v,":")
  147. cpucores := make(map[string]string)
  148. cpucores["key"] = "cpucores"
  149. cpucores["label"] = "CPU cores:"
  150. cpucores["value"] = strings.Trim(parts[1],"\r\n ")
  151. self.AList["cpucores"] = cpucores
  152. }
  153. if strings.HasPrefix(v,"Features") {
  154. parts := strings.Split(v,":")
  155. f_ := make(map[string]string)
  156. f_["key"] = "features"
  157. f_["label"] = "Features:"
  158. f_["value"] = strings.Trim(parts[1],"\r\n ")
  159. self.AList["features"] = f_
  160. }
  161. if strings.HasPrefix(v,"flags") {
  162. parts := strings.Split(v,":")
  163. flags := make(map[string]string)
  164. flags["key"] = "flags"
  165. flags["label"] = "Flags:"
  166. flags["value"] = strings.TrimSpace(parts[1])
  167. self.AList["flags"] = flags
  168. }
  169. }
  170. if last_processor > 0 {
  171. arm_cores := make(map[string]string)
  172. arm_cores["key"]= "armcores"
  173. arm_cores["label"] = "CPU cores:"
  174. arm_cores["value"] = strconv.Itoa(last_processor + 1)
  175. self.AList["armcores"] = arm_cores
  176. }
  177. }
  178. func (self *AboutPage) MemInfo() {
  179. lines, err := UI.ReadLines("/proc/meminfo")
  180. UI.ShowErr(err)
  181. for _,line := range lines {
  182. if strings.HasPrefix(line,"MemTotal") {
  183. parts := strings.Split(line,":")
  184. kb := strings.Replace(parts[1],"KB","",-1)
  185. kb = strings.Replace(kb,"kB","",-1)
  186. kb = strings.TrimSpace(kb)
  187. kb_int,_ := strconv.ParseInt(kb,10,64)
  188. kb_float := float64(kb_int)/1000.0
  189. memory := make(map[string]string)
  190. memory["key"] = "memory"
  191. memory["label"] = "Memory:"
  192. memory["value"] = strconv.FormatFloat(kb_float,'f',2,64) + " MB"
  193. self.AList["memory"] = memory
  194. break
  195. }
  196. }
  197. }
  198. func (self *AboutPage) GenList() {
  199. self.MyList = nil
  200. self.MyList = make([]*InfoPageListItem,0)
  201. start_x := 0
  202. start_y := 10
  203. last_height := 0
  204. for _,u := range ( []string{"processor","armcores","cpuscalemhz","features","memory","uname"} ) {
  205. if val, ok := self.AList[u]; ok {
  206. li := NewInfoPageListItem()
  207. li.Parent = self
  208. li.PosX = start_x
  209. li.PosY = start_y + last_height
  210. li.Width = UI.Width
  211. li.Fonts["normal"] = self.ListFontObj
  212. li.Fonts["small"] = UI.Fonts["varela12"]
  213. if self.AList[u]["label"] != "" {
  214. li.Init( self.AList[u]["label"] )
  215. }else {
  216. li.Init( self.AList[u]["key"])
  217. }
  218. li.Flag = val["key"]
  219. li.SetSmallText(val["value"])
  220. last_height += li.Height
  221. self.MyList = append(self.MyList,li)
  222. }
  223. }
  224. }
  225. func (self *AboutPage) Init() {
  226. if self.Screen != nil {
  227. if self.Screen.CanvasHWND != nil && self.CanvasHWND == nil {
  228. self.HWND = self.Screen.CanvasHWND
  229. self.CanvasHWND = surface.Surface(self.Screen.Width,self.BGheight)
  230. }
  231. self.PosX = self.Index * self.Screen.Width
  232. self.Width = self.Screen.Width
  233. self.Height = self.Screen.Height
  234. bgpng := UI.NewIconItem()
  235. bgpng.ImgSurf = UI.MyIconPool.GetImgSurf("about_bg")
  236. bgpng.MyType = UI.ICON_TYPES["STAT"]
  237. bgpng.Parent = self
  238. bgpng.Adjust(0,0,self.BGwidth,self.BGheight,0)
  239. self.Icons["bg"] = bgpng
  240. self.CpuInfo()
  241. self.MemInfo()
  242. self.CpuMhz()
  243. self.Uname()
  244. self.GenList()
  245. self.Scroller = UI.NewListScroller()
  246. self.Scroller.Parent = self
  247. self.Scroller.PosX = self.Width - 10
  248. self.Scroller.PosY = 2
  249. self.Scroller.Init()
  250. self.Scroller.SetCanvasHWND(self.HWND)
  251. }
  252. }
  253. func (self *AboutPage) ScrollDown() {
  254. dis := 10
  255. if UI.Abs(self.Scrolled) < ( self.BGheight - self.Height)/2 + 50 {
  256. self.PosY -= dis
  257. self.Scrolled -= dis
  258. }
  259. }
  260. func (self *AboutPage) ScrollUp() {
  261. dis := 10
  262. if self.PosY < 0 {
  263. self.PosY += dis
  264. self.Scrolled += dis
  265. }
  266. }
  267. func (self *AboutPage) OnLoadCb() {
  268. self.Scrolled = 0
  269. self.PosY = 0
  270. self.DrawOnce = false
  271. }
  272. func (self *AboutPage) OnReturnBackCb() {
  273. self.ReturnToUpLevelPage()
  274. self.Screen.Draw()
  275. self.Screen.SwapAndShow()
  276. }
  277. func (self *AboutPage) KeyDown( ev *event.Event) {
  278. if ev.Data["Key"] == UI.CurKeys["A"] || ev.Data["Key"] == UI.CurKeys["Menu"] {
  279. self.ReturnToUpLevelPage()
  280. self.Screen.Draw()
  281. self.Screen.SwapAndShow()
  282. }
  283. if ev.Data["Key"] == UI.CurKeys["Up"] {
  284. self.ScrollUp()
  285. self.Screen.Draw()
  286. self.Screen.SwapAndShow()
  287. }
  288. if ev.Data["Key"] == UI.CurKeys["Down"] {
  289. self.ScrollDown()
  290. self.Screen.Draw()
  291. self.Screen.SwapAndShow()
  292. }
  293. }
  294. func (self *AboutPage) Draw() {
  295. if self.DrawOnce == false {
  296. self.ClearCanvas()
  297. self.Icons["bg"].NewCoord(self.Width/2, self.Height/2 + (self.BGheight - UI.Height)/2 + self.Screen.TitleBar.Height)
  298. self.Icons["bg"].Draw()
  299. for _,v := range self.MyList {
  300. v.Draw()
  301. }
  302. self.DrawOnce = true
  303. }
  304. if self.HWND != nil {
  305. surface.Fill(self.HWND, &color.Color{255,255,255,255})
  306. rect_ := rect.Rect(self.PosX,self.PosY,self.Width,self.Height)
  307. surface.Blit(self.HWND,self.CanvasHWND,&rect_, nil)
  308. self.Scroller.UpdateSize(self.BGheight,UI.Abs(self.Scrolled)*3)
  309. self.Scroller.Draw()
  310. }
  311. }
  312. /******************************************************************************/
  313. type AboutPlugin struct {
  314. UI.Plugin
  315. Page UI.PageInterface
  316. }
  317. func (self *AboutPlugin) Init( main_screen *UI.MainScreen ) {
  318. self.Page = NewAboutPage()
  319. self.Page.SetScreen( main_screen)
  320. self.Page.SetName("About")
  321. self.Page.Init()
  322. }
  323. func (self *AboutPlugin) Run( main_screen *UI.MainScreen ) {
  324. if main_screen != nil {
  325. main_screen.PushPage(self.Page)
  326. main_screen.Draw()
  327. main_screen.SwapAndShow()
  328. }
  329. }
  330. var APIOBJ AboutPlugin