gateway_page.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. package GateWay
  2. import (
  3. "fmt"
  4. //"io/ioutil"
  5. //"path/filepath"
  6. "github.com/veandco/go-sdl2/ttf"
  7. "runtime"
  8. "strconv"
  9. "strings"
  10. //"github.com/mitchellh/go-homedir"
  11. "github.com/clockworkpi/LauncherGoDev/sysgo"
  12. "github.com/clockworkpi/LauncherGoDev/sysgo/UI"
  13. "github.com/cuu/gogame/color"
  14. "github.com/cuu/gogame/draw"
  15. "github.com/cuu/gogame/event"
  16. "github.com/cuu/gogame/rect"
  17. "github.com/cuu/gogame/surface"
  18. "github.com/cuu/gogame/time"
  19. )
  20. type ListPageSelector struct {
  21. UI.InfoPageSelector
  22. }
  23. func NewListPageSelector() *ListPageSelector {
  24. p := &ListPageSelector{}
  25. p.Width = UI.Width
  26. p.BackgroundColor = &color.Color{131, 199, 219, 255} //SkinManager().GiveColor('Front')
  27. return p
  28. }
  29. func (self *ListPageSelector) Draw() {
  30. idx := self.Parent.GetPsIndex()
  31. mylist := self.Parent.GetMyList()
  32. if idx < len(mylist) {
  33. x, y := mylist[idx].Coord()
  34. _, h := mylist[idx].Size()
  35. self.PosX = x + 2
  36. self.PosY = y + 1
  37. self.Height = h - 3
  38. canvas_ := self.Parent.GetCanvasHWND()
  39. rect_ := rect.Rect(self.PosX, self.PosY, self.Width-4, self.Height)
  40. draw.AARoundRect(canvas_, &rect_, self.BackgroundColor, 4, 0, self.BackgroundColor)
  41. }
  42. }
  43. type PageListItem struct {
  44. UI.InfoPageListItem
  45. Active bool
  46. Value string
  47. }
  48. func NewPageListItem() *PageListItem {
  49. p := &PageListItem{}
  50. p.Height = UI.DefaultInfoPageListItemHeight
  51. p.ReadOnly = false
  52. p.Labels = make(map[string]UI.LabelInterface)
  53. p.Icons = make(map[string]UI.IconItemInterface)
  54. p.Fonts = make(map[string]*ttf.Font)
  55. return p
  56. }
  57. func (self *PageListItem) Draw() {
  58. x, _ := self.Labels["Text"].Coord()
  59. w, h := self.Labels["Text"].Size()
  60. self.Labels["Text"].NewCoord(x, self.PosY+(self.Height-h)/2)
  61. if self.Active == true {
  62. self.Parent.(*GateWayPage).Icons["done"].NewCoord(self.Parent.(*GateWayPage).Width-30, self.PosY+5)
  63. self.Parent.(*GateWayPage).Icons["done"].Draw()
  64. }
  65. self.Labels["Text"].SetBold(self.Active)
  66. self.Labels["Text"].Draw()
  67. if _, ok := self.Labels["Small"]; ok {
  68. x, _ = self.Labels["Small"].Coord()
  69. w, h = self.Labels["Small"].Size()
  70. self.Labels["Small"].NewCoord(self.Width-w-10, self.PosY+(self.Height-h)/2)
  71. self.Labels["Small"].Draw()
  72. }
  73. canvas_ := self.Parent.GetCanvasHWND()
  74. draw.Line(canvas_, &color.Color{169, 169, 169, 255},
  75. self.PosX, self.PosY+self.Height-1,
  76. self.PosX+self.Width, self.PosY+self.Height-1, 1)
  77. }
  78. type GateWayPage struct {
  79. UI.Page
  80. ListFont *ttf.Font
  81. BGwidth int
  82. BGheight int
  83. DrawOnce bool
  84. Scroller *UI.ListScroller
  85. Icons map[string]UI.IconItemInterface
  86. }
  87. func NewGateWayPage() *GateWayPage {
  88. p := &GateWayPage{}
  89. p.ListFont = UI.Fonts["notosanscjk15"]
  90. p.FootMsg = [5]string{"Nav", "", "Clear All", "Back", "Select"}
  91. p.BGwidth = UI.Width
  92. p.BGheight = UI.Height - 24 - 20
  93. p.Icons = make(map[string]UI.IconItemInterface)
  94. return p
  95. }
  96. func (self *GateWayPage) GenList() {
  97. self.MyList = nil
  98. start_x := 0
  99. start_y := 0
  100. last_height := 0
  101. var drivers = [][2]string{[2]string{"usb0", "USB Ethernet"},
  102. [2]string{sysgo.WifiDev, "Wi-Fi"}}
  103. for _, u := range drivers {
  104. li := NewPageListItem()
  105. li.Parent = self
  106. li.PosX = start_x
  107. li.PosY = start_y + last_height
  108. li.Width = UI.Width
  109. li.Fonts["normal"] = self.ListFont
  110. li.Active = false
  111. li.Value = u[0]
  112. li.Init(u[1])
  113. last_height += li.Height
  114. self.MyList = append(self.MyList, li)
  115. }
  116. }
  117. func (self *GateWayPage) Init() {
  118. if self.Screen != nil {
  119. if self.Screen.CanvasHWND != nil && self.CanvasHWND == nil {
  120. self.HWND = self.Screen.CanvasHWND
  121. self.CanvasHWND = surface.Surface(self.Screen.Width, self.Screen.Height)
  122. }
  123. }
  124. self.PosX = self.Index * self.Screen.Width
  125. self.Width = self.Screen.Width
  126. self.Height = self.Screen.Height
  127. done := UI.NewIconItem()
  128. done.ImgSurf = UI.MyIconPool.GetImgSurf("done")
  129. done.MyType = UI.ICON_TYPES["STAT"]
  130. done.Parent = self
  131. self.Icons["done"] = done
  132. ps := NewListPageSelector()
  133. ps.Parent = self
  134. self.Ps = ps
  135. self.PsIndex = 0
  136. self.GenList()
  137. self.Scroller = UI.NewListScroller()
  138. self.Scroller.Parent = self
  139. self.Scroller.PosX = self.Width - 10
  140. self.Scroller.PosY = 2
  141. self.Scroller.Init()
  142. self.Scroller.SetCanvasHWND(self.HWND)
  143. }
  144. func (self *GateWayPage) Click() {
  145. if len(self.MyList) == 0 {
  146. return
  147. }
  148. if self.PsIndex >= len(self.MyList) {
  149. self.PsIndex = len(self.MyList) - 1
  150. }
  151. cur_li := self.MyList[self.PsIndex].(*PageListItem)
  152. if cur_li.Active == true {
  153. out := UI.System("sudo ip route | grep default | cut -d \" \" -f3")
  154. if len(out) > 7 {
  155. self.Screen.MsgBox.SetText(strings.Trim(out, "\r\n "))
  156. self.Screen.MsgBox.Draw()
  157. self.Screen.SwapAndShow()
  158. }
  159. return
  160. }
  161. if strings.Contains(runtime.GOARCH, "arm") == true {
  162. for i, _ := range self.MyList {
  163. self.MyList[i].(*PageListItem).Active = false
  164. }
  165. cur_li.Active = self.ApplyGateWay(cur_li.Value)
  166. self.Screen.MsgBox.SetText("Applying")
  167. self.Screen.MsgBox.Draw()
  168. self.Screen.SwapAndShow()
  169. time.BlockDelay(1000)
  170. self.Screen.Draw()
  171. self.Screen.SwapAndShow()
  172. } else {
  173. self.Screen.MsgBox.SetText("Do it in GameShell")
  174. self.Screen.MsgBox.Draw()
  175. self.Screen.SwapAndShow()
  176. }
  177. }
  178. func (self *GateWayPage) ClearAllGateways() {
  179. self.Screen.MsgBox.SetText("Cleaning up")
  180. self.Screen.MsgBox.Draw()
  181. self.Screen.SwapAndShow()
  182. UI.System("sudo ip route del 0/0")
  183. time.BlockDelay(800)
  184. for i, _ := range self.MyList {
  185. self.MyList[i].(*PageListItem).Active = false
  186. }
  187. self.Screen.Draw()
  188. self.Screen.SwapAndShow()
  189. }
  190. func (self *GateWayPage) ApplyGateWay(gateway string) bool {
  191. UI.System("sudo ip route del 0/0")
  192. if gateway == "usb0" {
  193. out := UI.System("sudo ifconfig usb0 | grep inet | tr -s \" \"| cut -d \" \" -f3")
  194. if len(out) > 7 {
  195. if strings.Contains(out, "error") == false {
  196. out = strings.Trim(out, "\r\n ")
  197. parts := strings.Split(out, ".")
  198. if len(parts) == 4 { // IPv4
  199. tmp, err := strconv.Atoi(parts[3])
  200. if err == nil {
  201. if tmp == 0 {
  202. tmp = tmp + 1
  203. } else if tmp == 1 {
  204. tmp = tmp + 1
  205. } else if tmp > 1 {
  206. tmp = tmp - 1
  207. }
  208. parts[3] = strconv.Itoa(tmp)
  209. ipaddress := strings.Join(parts, ".")
  210. UI.System(fmt.Sprintf("sudo route add default gw %s", ipaddress))
  211. return true
  212. }
  213. }
  214. }
  215. }
  216. } else { // wlan0
  217. if self.Screen.IsWifiConnectedNow() == true {
  218. UI.System(fmt.Sprintf("sudo dhclient %s", sysgo.WifiDev))
  219. return true
  220. } else {
  221. self.Screen.MsgBox.SetText("Wi-Fi is not connected")
  222. self.Screen.MsgBox.Draw()
  223. self.Screen.SwapAndShow()
  224. return false
  225. }
  226. }
  227. return false
  228. }
  229. func (self *GateWayPage) OnLoadCb() {
  230. self.PosY = 0
  231. self.DrawOnce = false
  232. thedrv := ""
  233. if strings.Contains(runtime.GOARCH, "arm") == true {
  234. out := UI.System("sudo ip route | grep default")
  235. if len(out) > 7 {
  236. if strings.Contains(out, "usb0") {
  237. thedrv = "usb0"
  238. } else if strings.Contains(out, sysgo.WifiDev) {
  239. thedrv = sysgo.WifiDev
  240. }
  241. }
  242. }
  243. for i, _ := range self.MyList {
  244. self.MyList[i].(*PageListItem).Active = false
  245. }
  246. if thedrv != "" {
  247. for i, v := range self.MyList {
  248. if strings.Contains(v.(*PageListItem).Value, thedrv) {
  249. self.MyList[i].(*PageListItem).Active = true
  250. }
  251. }
  252. }
  253. }
  254. func (self *GateWayPage) KeyDown(ev *event.Event) {
  255. if ev.Data["Key"] == UI.CurKeys["A"] || ev.Data["Key"] == UI.CurKeys["Menu"] {
  256. self.ReturnToUpLevelPage()
  257. self.Screen.Draw()
  258. self.Screen.SwapAndShow()
  259. }
  260. if ev.Data["Key"] == UI.CurKeys["B"] {
  261. self.Click()
  262. }
  263. if ev.Data["Key"] == UI.CurKeys["Y"] {
  264. self.ClearAllGateways()
  265. }
  266. if ev.Data["Key"] == UI.CurKeys["Up"] {
  267. self.ScrollUp()
  268. self.Screen.Draw()
  269. self.Screen.SwapAndShow()
  270. }
  271. if ev.Data["Key"] == UI.CurKeys["Down"] {
  272. self.ScrollDown()
  273. self.Screen.Draw()
  274. self.Screen.SwapAndShow()
  275. }
  276. }
  277. func (self *GateWayPage) Draw() {
  278. self.ClearCanvas()
  279. if len(self.MyList) == 0 {
  280. return
  281. }
  282. if len(self.MyList)*UI.DefaultInfoPageListItemHeight > self.Height {
  283. self.Ps.(*ListPageSelector).Width = self.Width - 11
  284. self.Ps.Draw()
  285. for _, v := range self.MyList {
  286. if v.(*PageListItem).PosY > self.Height+self.Height/2 {
  287. break
  288. }
  289. if v.(*PageListItem).PosY < 0 {
  290. continue
  291. }
  292. v.Draw()
  293. }
  294. self.Scroller.UpdateSize(len(self.MyList)*UI.DefaultInfoPageListItemHeight,
  295. self.PsIndex*UI.DefaultInfoPageListItemHeight)
  296. self.Scroller.Draw()
  297. } else {
  298. self.Ps.(*ListPageSelector).Width = self.Width
  299. self.Ps.Draw()
  300. for _, v := range self.MyList {
  301. if v.(*PageListItem).PosY > self.Height+self.Height/2 {
  302. break
  303. }
  304. if v.(*PageListItem).PosY < 0 {
  305. continue
  306. }
  307. v.Draw()
  308. }
  309. }
  310. if self.HWND != nil {
  311. surface.Fill(self.HWND, &color.Color{255, 255, 255, 255})
  312. rect_ := rect.Rect(self.PosX, self.PosY, self.Width, self.Height)
  313. surface.Blit(self.HWND, self.CanvasHWND, &rect_, nil)
  314. }
  315. }