wifi.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. package Wifi
  2. //wifi_list.py
  3. import (
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. //"os"
  8. // "os/exec"
  9. // gotime "time"
  10. "log"
  11. //"github.com/godbus/dbus"
  12. "database/sql"
  13. _ "github.com/mattn/go-sqlite3"
  14. "github.com/veandco/go-sdl2/ttf"
  15. "github.com/clockworkpi/LauncherGoDev/sysgo"
  16. "github.com/clockworkpi/LauncherGoDev/sysgo/UI"
  17. "github.com/cuu/gogame/color"
  18. "github.com/cuu/gogame/draw"
  19. "github.com/cuu/gogame/event"
  20. "github.com/cuu/gogame/font"
  21. "github.com/cuu/gogame/rect"
  22. "github.com/cuu/gogame/surface"
  23. "github.com/cuu/gogame/time"
  24. wifi "github.com/cuu/wpa-connect"
  25. )
  26. const EMPTY_NETWORK = "00:00:00:00:00:00"
  27. type WifiDisconnectConfirmPage struct {
  28. UI.ConfirmPage
  29. Parent *WifiInfoPage
  30. }
  31. func NewWifiDisconnectConfirmPage() *WifiDisconnectConfirmPage {
  32. p := &WifiDisconnectConfirmPage{}
  33. p.ListFont = UI.Fonts["veramono20"]
  34. p.FootMsg = [5]string{"Nav", "", "", "Cancel", "Yes"}
  35. p.ConfirmText = "Confirm Disconnect?"
  36. return p
  37. }
  38. func (self *WifiDisconnectConfirmPage) KeyDown(ev *event.Event) {
  39. if ev.Data["Key"] == UI.CurKeys["A"] || ev.Data["Key"] == UI.CurKeys["Menu"] {
  40. self.ReturnToUpLevelPage()
  41. self.Screen.Draw()
  42. self.Screen.SwapAndShow()
  43. }
  44. if ev.Data["Key"] == UI.CurKeys["B"] {
  45. fmt.Println("Disconnecting..")
  46. self.SnapMsg("Disconnecting...")
  47. self.Screen.Draw()
  48. self.Screen.SwapAndShow()
  49. self.Parent.Parent.Disconnect()
  50. time.BlockDelay(400)
  51. self.ReturnToUpLevelPage()
  52. self.Screen.Draw()
  53. self.Screen.SwapAndShow()
  54. self.Parent.Parent.Rescan(false)
  55. }
  56. }
  57. type WifiInfoPage struct {
  58. UI.Page
  59. ListFontObj *ttf.Font
  60. Bss *wifi.BSS
  61. AList map[string]map[string]string
  62. ESSID string
  63. BSSID string
  64. MyList []UI.ListItemInterface
  65. DisconnectConfirmPage *WifiDisconnectConfirmPage //child page
  66. Parent *WifiList
  67. }
  68. func NewWifiInfoPage() *WifiInfoPage {
  69. p := &WifiInfoPage{}
  70. p.FootMsg = [5]string{"Nav", "Disconnect", "", "Back", ""}
  71. p.ListFontObj = UI.Fonts["varela15"]
  72. p.AList = make(map[string]map[string]string)
  73. p.BSSID = ""
  74. p.ESSID = ""
  75. return p
  76. }
  77. func (self *WifiInfoPage) GenList() {
  78. self.MyList = nil
  79. self.MyList = make([]UI.ListItemInterface, 0)
  80. if self.BSSID != "" {
  81. self.AList["ip"]["value"] = "Not Connected"
  82. if self.BSSID == self.Parent.CurBssid {
  83. var ip string
  84. ip = self.Parent.GetWirelessIP()
  85. if len(ip) > 0 {
  86. self.AList["ip"]["value"] = ip
  87. }
  88. } else {
  89. fmt.Println(self.BSSID)
  90. }
  91. self.AList["ssid"]["value"] = self.ESSID
  92. }
  93. start_x := 0
  94. start_y := 0
  95. i := 0
  96. for k, _ := range self.AList {
  97. li := UI.NewInfoPageListItem()
  98. li.Parent = self
  99. li.PosX = start_x
  100. li.PosY = start_y + i*li.Height //default is 30
  101. li.Width = UI.Width
  102. li.Fonts["normal"] = self.ListFontObj
  103. li.Fonts["small"] = UI.Fonts["varela12"]
  104. if self.AList[k]["label"] != "" {
  105. li.Init(self.AList[k]["label"])
  106. } else {
  107. li.Init(self.AList[k]["key"])
  108. }
  109. li.Flag = self.AList[k]["key"]
  110. li.SetSmallText(self.AList[k]["value"])
  111. self.MyList = append(self.MyList, li)
  112. i += 1
  113. }
  114. }
  115. func (self *WifiInfoPage) Init() {
  116. if self.Screen != nil {
  117. if self.Screen.CanvasHWND != nil && self.CanvasHWND == nil {
  118. self.CanvasHWND = self.Screen.CanvasHWND
  119. }
  120. }
  121. self.PosX = self.Index * self.Screen.Width
  122. self.Width = self.Screen.Width
  123. self.Height = self.Screen.Height
  124. ps := UI.NewInfoPageSelector()
  125. ps.Parent = self
  126. ps.PosX = 2
  127. self.Ps = ps
  128. self.PsIndex = 0
  129. ip := make(map[string]string) // ip = {}
  130. ip["key"] = "ip"
  131. ip["label"] = "IP"
  132. ip["value"] = "Not Connected"
  133. ssid := make(map[string]string) // ssid = {}
  134. ssid["key"] = "ssid"
  135. ssid["label"] = "SSID"
  136. ssid["value"] = ""
  137. self.AList["ip"] = ip
  138. self.AList["ssid"] = ssid
  139. self.DisconnectConfirmPage = NewWifiDisconnectConfirmPage()
  140. self.DisconnectConfirmPage.Screen = self.Screen
  141. self.DisconnectConfirmPage.Name = "Confirm Disconnect"
  142. self.DisconnectConfirmPage.Parent = self
  143. self.DisconnectConfirmPage.Init()
  144. }
  145. func (self *WifiInfoPage) ScrollUp() {
  146. if len(self.MyList) == 0 {
  147. return
  148. }
  149. self.PsIndex -= 1
  150. if self.PsIndex < 0 {
  151. self.PsIndex = 0
  152. }
  153. cur_li := self.MyList[self.PsIndex]
  154. x, y := cur_li.Coord()
  155. if x < 0 {
  156. for i := 0; i < len(self.MyList); i++ {
  157. _, h := self.MyList[i].Size()
  158. x, y = self.MyList[i].Coord()
  159. self.MyList[i].NewCoord(x, y+h)
  160. }
  161. }
  162. }
  163. func (self *WifiInfoPage) ScrollDown() {
  164. if len(self.MyList) == 0 {
  165. return
  166. }
  167. self.PsIndex += 1
  168. if self.PsIndex >= len(self.MyList) {
  169. self.PsIndex = len(self.MyList) - 1
  170. }
  171. cur_li := self.MyList[self.PsIndex]
  172. x, y := cur_li.Coord()
  173. _, h := cur_li.Size()
  174. if y+h > self.Height {
  175. for i := 0; i < len(self.MyList); i++ {
  176. _, h = self.MyList[i].Size()
  177. x, y = self.MyList[i].Coord()
  178. self.MyList[i].NewCoord(x, y-h)
  179. }
  180. }
  181. }
  182. func (self *WifiInfoPage) Click() {
  183. /*
  184. cur_li = self._MyList[self._PsIndex]
  185. print(cur_li._Flag)
  186. */
  187. }
  188. func (self *WifiInfoPage) TryDisconnect() {
  189. var ip string
  190. ip = self.Parent.GetWirelessIP()
  191. if len(ip) > 6 {
  192. self.Screen.PushPage(self.DisconnectConfirmPage)
  193. self.Screen.Draw()
  194. self.Screen.SwapAndShow()
  195. } else {
  196. fmt.Println("WifiInfoPage TryDisconnect can not get IP,maybe you are offline")
  197. return
  198. }
  199. }
  200. func (self *WifiInfoPage) OnLoadCb() {
  201. /*
  202. self.FootMsg[1]="Disconnect"
  203. self.FootMsg[1] = ""
  204. */
  205. self.GenList()
  206. }
  207. func (self *WifiInfoPage) OnReturnBackCb() {
  208. self.ReturnToUpLevelPage()
  209. self.Screen.Draw()
  210. self.Screen.SwapAndShow()
  211. }
  212. func (self *WifiInfoPage) KeyDown(ev *event.Event) {
  213. if ev.Data["Key"] == UI.CurKeys["A"] || ev.Data["Key"] == UI.CurKeys["Menu"] {
  214. self.ReturnToUpLevelPage()
  215. self.Screen.Draw()
  216. self.Screen.SwapAndShow()
  217. }
  218. if ev.Data["Key"] == UI.CurKeys["Up"] {
  219. self.ScrollUp()
  220. self.Screen.Draw()
  221. self.Screen.SwapAndShow()
  222. }
  223. if ev.Data["Key"] == UI.CurKeys["Down"] {
  224. self.ScrollDown()
  225. self.Screen.Draw()
  226. self.Screen.SwapAndShow()
  227. }
  228. if ev.Data["Key"] == UI.CurKeys["Enter"] {
  229. self.Click()
  230. }
  231. if ev.Data["Key"] == UI.CurKeys["X"] {
  232. self.TryDisconnect()
  233. }
  234. }
  235. func (self *WifiInfoPage) Draw() {
  236. self.ClearCanvas()
  237. self.Ps.Draw()
  238. for i := 0; i < len(self.MyList); i++ {
  239. self.MyList[i].Draw()
  240. }
  241. }
  242. type WifiListSelector struct {
  243. UI.PageSelector
  244. BackgroundColor *color.Color
  245. Parent *WifiList
  246. }
  247. func NewWifiListSelector() *WifiListSelector {
  248. p := &WifiListSelector{}
  249. p.BackgroundColor = &color.Color{131, 199, 219, 255} //SkinManager().GiveColor('Front')
  250. return p
  251. }
  252. func (self *WifiListSelector) Draw() {
  253. idx := self.Parent.PsIndex
  254. if idx < len(self.Parent.MyList) {
  255. x := self.Parent.MyList[idx].PosX + 11
  256. y := self.Parent.MyList[idx].PosY + 1
  257. h := self.Parent.MyList[idx].Height - 3
  258. self.PosX = x
  259. self.PosY = y
  260. self.Height = h
  261. rect_ := rect.Rect(x, y, self.Width, h)
  262. draw.AARoundRect(self.Parent.CanvasHWND, &rect_, self.BackgroundColor, 4, 0, self.BackgroundColor)
  263. }
  264. }
  265. type WifiListMessageBox struct {
  266. UI.Label
  267. Parent *WifiList
  268. }
  269. func NewWifiListMessageBox() *WifiListMessageBox {
  270. p := &WifiListMessageBox{}
  271. p.Color = &color.Color{83, 83, 83, 255}
  272. return p
  273. }
  274. func (self *WifiListMessageBox) Draw() {
  275. my_text := font.Render(self.FontObj, self.Text, true, self.Color, nil)
  276. w := surface.GetWidth(my_text)
  277. h := surface.GetHeight(my_text)
  278. x := (self.Parent.Width - w) / 2
  279. y := (self.Parent.Height - h) / 2
  280. padding := 10
  281. white := &color.Color{255, 255, 255, 255}
  282. black := &color.Color{0, 0, 0, 255}
  283. rect_ := rect.Rect(x-padding, y-padding, w+padding*2, h+padding*2)
  284. draw.Rect(self.CanvasHWND, white, &rect_, 0)
  285. draw.Rect(self.CanvasHWND, black, &rect_, 1)
  286. rect_2 := rect.Rect(x, y, w, h)
  287. surface.Blit(self.CanvasHWND, my_text, &rect_2, nil)
  288. my_text.Free()
  289. }
  290. //---------WifiList---------------------------------
  291. type BlockCbFunc func()
  292. type WifiList struct {
  293. UI.Page
  294. WifiPassword string
  295. Connecting bool
  296. Scanning bool
  297. ShowingMessageBox bool
  298. MsgBox *WifiListMessageBox
  299. ConnectTry int
  300. BlockingUI bool
  301. BlockCb BlockCbFunc
  302. LastStatusMsg string
  303. Scroller *UI.ListScroller
  304. ListFontObj *ttf.Font
  305. InfoPage *WifiInfoPage
  306. MyList []*NetItem
  307. CurEssid string ///SomeWifi
  308. CurBssid string //00:00:00:00:00:00
  309. CurIP string
  310. CurSig string
  311. }
  312. func NewWifiList() *WifiList {
  313. p := &WifiList{}
  314. p.ListFontObj = UI.Fonts["notosanscjk15"]
  315. p.FootMsg = [5]string{"Nav.", "Scan", "Info", "Back", "Enter"}
  316. return p
  317. }
  318. func (self *WifiList) ShowBox(msg string) {
  319. self.MsgBox.Text = msg
  320. self.ShowingMessageBox = true
  321. self.Screen.Draw()
  322. self.MsgBox.Draw()
  323. self.Screen.SwapAndShow()
  324. }
  325. func (self *WifiList) HideBox() {
  326. self.Draw()
  327. self.ShowingMessageBox = false
  328. self.Screen.SwapAndShow()
  329. }
  330. func (self *WifiList) GenNetworkList() {
  331. self.MyList = self.MyList[:0]
  332. start_x := 0
  333. start_y := 0
  334. var is_active bool
  335. var li_idx int
  336. li_idx = 0
  337. self.WifiScanStarted()
  338. if bssList, err := GsScanManager.Scan(); err == nil {
  339. self.CurEssid = GsScanManager.GetCurrentSSID()
  340. self.CurBssid = GsScanManager.GetCurrentBSSID()
  341. for _, bss := range bssList {
  342. is_active = false
  343. fmt.Println(bss.SSID, " ", bss.BSSID, " ", bss.Signal, bss.KeyMgmt)
  344. ni := NewNetItem()
  345. ni.Parent = self
  346. ni.PosX = start_x
  347. ni.PosY = start_y + li_idx*NetItemDefaultHeight
  348. ni.Width = UI.Width
  349. ni.FontObj = self.ListFontObj
  350. ni.Essid = bss.SSID
  351. ni.Bssid = bss.BSSID
  352. ni.Signal = bss.Signal
  353. if self.CurBssid == ni.Bssid {
  354. is_active = true
  355. }
  356. ni.Init(is_active)
  357. self.MyList = append(self.MyList, ni)
  358. li_idx++
  359. }
  360. }
  361. self.WifiScanFinished()
  362. self.PsIndex = 0
  363. }
  364. func (self *WifiList) Disconnect() {
  365. self.Connecting = false
  366. //nmcli -t -f NAME c show --active
  367. //nmcli con down
  368. cli := "nmcli -t -f NAME c show --active"
  369. out := UI.SystemTrim(cli)
  370. cli = fmt.Sprintf("sudo nmcli con down \"%s\"",out)
  371. out = UI.System(cli)
  372. log.Println(out)
  373. self.CurEssid = ""
  374. self.CurBssid = ""
  375. }
  376. func (self *WifiList) ShutDownConnecting() {
  377. self.Connecting = false
  378. self.Disconnect()
  379. }
  380. func (self *WifiList) Rescan(sync bool) { // sync default should be false
  381. fmt.Println("start Rescan")
  382. self.GenNetworkList()
  383. }
  384. // dbus signal functions
  385. func (self *WifiList) WifiScanFinished() {
  386. if self.Screen.CurrentPage != self {
  387. return
  388. }
  389. self.ResetPageSelector()
  390. self.Scanning = false
  391. self.HideBox()
  392. self.BlockingUI = false
  393. fmt.Println("dbus says scan finished")
  394. }
  395. func (self *WifiList) WifiScanStarted() {
  396. if self.Screen.CurrentPage != self {
  397. return
  398. }
  399. self.Scanning = true
  400. self.ShowBox("Wifi scanning...")
  401. self.BlockingUI = true
  402. fmt.Println("dbus says start scan")
  403. }
  404. func (self *WifiList) SaveNetworkList() {
  405. }
  406. func (self *WifiList) SaveWifiPassword(essid,password string) {
  407. db, err := sql.Open("sqlite3", sysgo.SQLDB)
  408. if err != nil {
  409. log.Fatal(err)
  410. return
  411. }
  412. defer db.Close()
  413. stmt, err := db.Prepare("select count(*) from wifi where essid = ?")
  414. if err != nil {
  415. log.Println(err)
  416. return
  417. }
  418. defer stmt.Close()
  419. var count string
  420. err = stmt.QueryRow(essid).Scan(&count)
  421. if err != nil {
  422. log.Println(err)
  423. count = "0"
  424. }
  425. cnt,_ := strconv.Atoi(count)
  426. if cnt > 0 {
  427. _,err = db.Exec("update wifi set pass= :pass where essid = :essid",sql.Named("pass",password),sql.Named("essid",essid))
  428. if err != nil {
  429. log.Println(err)
  430. }
  431. }else {
  432. _,err = db.Exec("insert into wifi(essid,pass) values(:essid,:pass)",sql.Named("essid",essid),sql.Named("pass",password))
  433. if err != nil {
  434. log.Println(err)
  435. }
  436. }
  437. }
  438. func (self *WifiList) LoadWifiPassword(essid string) string {
  439. db, err := sql.Open("sqlite3", sysgo.SQLDB)
  440. if err != nil {
  441. log.Fatal(err)
  442. return ""
  443. }
  444. defer db.Close()
  445. password := ""
  446. stmt, err := db.Prepare("select pass from wifi where essid = ?")
  447. defer stmt.Close()
  448. if err != nil {
  449. log.Println(err)
  450. }else {
  451. err = stmt.QueryRow(essid).Scan(&password)
  452. if err != nil {
  453. log.Println(err)
  454. }
  455. }
  456. return password
  457. }
  458. //----------------------------------------------------------------------------------
  459. func (self *WifiList) UpdateNetList(state int, info []string, force_check bool, firstrun bool) { //force_check default ==false, firstrun default == false
  460. if force_check == true {
  461. self.GenNetworkList()
  462. self.SaveNetworkList()
  463. }
  464. }
  465. func (self *WifiList) UpdateListActive() {
  466. for i := 0; i < len(self.MyList); i++ {
  467. if self.MyList[i].Bssid == self.CurBssid {
  468. self.MyList[i].IsActive = true
  469. } else {
  470. self.MyList[i].IsActive = false
  471. }
  472. }
  473. }
  474. func (self *WifiList) ConfigWireless(password string) {
  475. ssid := self.MyList[self.PsIndex].Essid
  476. fmt.Println(ssid)
  477. fmt.Println(password)
  478. self.ShowBox("Connecting...")
  479. self.Connecting = true
  480. cli := fmt.Sprintf("sudo nmcli dev wifi connect %s password \"%s\"", ssid, password)
  481. log.Println(cli)
  482. out := UI.System(cli)
  483. log.Println(out)
  484. if strings.Contains(out, "successfully") {
  485. self.CurEssid = self.MyList[self.PsIndex].Essid
  486. self.CurBssid = self.MyList[self.PsIndex].Bssid
  487. self.MyList[self.PsIndex].Password = password
  488. self.SaveWifiPassword(ssid,password)
  489. self.ShowBox("Connected")
  490. } else {
  491. self.ShowBox("Wifi connect error")
  492. self.CurEssid = ""
  493. self.CurBssid = ""
  494. }
  495. self.Connecting = false
  496. self.UpdateListActive()
  497. }
  498. func (self *WifiList) GetWirelessIP() string {
  499. cli := fmt.Sprintf("ip -4 addr show %s | grep -oP '(?<=inet\\s)\\d+(\\.\\d+){3}'", sysgo.WifiDev)
  500. out := UI.SystemTrim(cli)
  501. return out
  502. }
  503. func (self *WifiList) ScrollUp() {
  504. if len(self.MyList) == 0 {
  505. return
  506. }
  507. self.PsIndex -= 1
  508. if self.PsIndex < 0 {
  509. self.PsIndex = 0
  510. }
  511. cur_ni := self.MyList[self.PsIndex] //*NetItem
  512. if cur_ni.PosY < 0 {
  513. for i := 0; i < len(self.MyList); i++ {
  514. self.MyList[i].PosY += self.MyList[i].Height
  515. }
  516. }
  517. }
  518. func (self *WifiList) ScrollDown() {
  519. if len(self.MyList) == 0 {
  520. return
  521. }
  522. self.PsIndex += 1
  523. if self.PsIndex >= len(self.MyList) {
  524. self.PsIndex = len(self.MyList) - 1
  525. }
  526. cur_ni := self.MyList[self.PsIndex]
  527. if cur_ni.PosY+cur_ni.Height > self.Height {
  528. for i := 0; i < len(self.MyList); i++ {
  529. self.MyList[i].PosY -= self.MyList[i].Height
  530. }
  531. }
  532. }
  533. func (self *WifiList) AbortedAndReturnToUpLevel() {
  534. self.HideBox()
  535. self.Screen.FootBar.ResetNavText()
  536. self.ReturnToUpLevelPage()
  537. self.Screen.Draw()
  538. self.Screen.SwapAndShow()
  539. }
  540. func (self *WifiList) OnKbdReturnBackCb() {
  541. password_inputed := strings.Join(APIOBJ.PasswordPage.Textarea.MyWords, "")
  542. fmt.Println("Password inputed: ", password_inputed)
  543. if len(password_inputed) > 4 {
  544. self.ConfigWireless(password_inputed)
  545. }else {
  546. log.Println("wifi password length too short ",len(password_inputed))
  547. }
  548. }
  549. func (self *WifiList) OnReturnBackCb() {
  550. //fmt.Println("return back")
  551. }
  552. func (self *WifiList) KeyDown(ev *event.Event) {
  553. if ev.Data["Key"] == UI.CurKeys["A"] || ev.Data["Key"] == UI.CurKeys["Menu"] {
  554. //self.ShutDownConnecting()
  555. //self.ShowBox("ShutDownConnecting...")
  556. self.AbortedAndReturnToUpLevel()
  557. }
  558. if ev.Data["Key"] == UI.CurKeys["Up"] {
  559. self.ScrollUp()
  560. self.Screen.Draw()
  561. self.Screen.SwapAndShow()
  562. }
  563. if ev.Data["Key"] == UI.CurKeys["Down"] {
  564. self.ScrollDown()
  565. self.Screen.Draw()
  566. self.Screen.SwapAndShow()
  567. }
  568. if ev.Data["Key"] == UI.CurKeys["Enter"] { // enter to set password,enter is B on GM
  569. if len(self.MyList) == 0 {
  570. return
  571. }
  572. if self.MyList[self.PsIndex].IsActive == true {
  573. var ip string
  574. ip = self.GetWirelessIP()
  575. self.ShowBox(ip)
  576. } else {
  577. self.Screen.PushCurPage()
  578. self.Screen.SetCurPage(APIOBJ.PasswordPage)
  579. thepass := self.LoadWifiPassword(self.MyList[self.PsIndex].Essid)
  580. fmt.Println("APIOBJ.PasswordPage.SetPassword ", thepass, len(thepass))
  581. APIOBJ.PasswordPage.SetPassword(thepass)
  582. self.Screen.Draw()
  583. self.Screen.SwapAndShow()
  584. }
  585. }
  586. if ev.Data["Key"] == UI.CurKeys["X"] {
  587. self.Rescan(false)
  588. }
  589. if ev.Data["Key"] == UI.CurKeys["Y"] {
  590. if len(self.MyList) == 0 {
  591. return
  592. }
  593. self.InfoPage.BSSID = self.MyList[self.PsIndex].Bssid
  594. self.InfoPage.ESSID = self.MyList[self.PsIndex].Essid
  595. self.Screen.PushPage(self.InfoPage)
  596. self.Screen.Draw()
  597. self.Screen.SwapAndShow()
  598. }
  599. }
  600. func (self *WifiList) OnLoadCb() {
  601. ip := self.GetWirelessIP()
  602. if len(ip) < 6 {
  603. self.CurEssid = ""
  604. self.CurBssid = ""
  605. self.CurIP = ip
  606. }
  607. self.Rescan(false)
  608. }
  609. func (self *WifiList) Init() {
  610. self.PosX = self.Index * self.Screen.Width
  611. self.Width = self.Screen.Width
  612. self.Height = self.Screen.Height
  613. self.CanvasHWND = self.Screen.CanvasHWND
  614. ps := NewWifiListSelector()
  615. ps.Parent = self
  616. ps.Width = UI.Width - 12
  617. self.Ps = ps
  618. self.PsIndex = 0
  619. msgbox := NewWifiListMessageBox()
  620. msgbox.CanvasHWND = self.CanvasHWND
  621. msgbox.Init(" ", UI.Fonts["veramono12"], nil)
  622. msgbox.Parent = self
  623. self.MsgBox = msgbox
  624. /*
  625. {
  626. 'fields': [],
  627. 'name': 'WPA 1/2 (Passphrase)',
  628. 'optional': [],
  629. 'protected': [
  630. ['apsk', 'Preshared_Key'],
  631. ],
  632. 'required': [
  633. ['apsk', 'Preshared_Key'],
  634. ],
  635. 'type': 'wpa-psk',
  636. },
  637. */
  638. self.Scroller = UI.NewListScroller()
  639. self.Scroller.Parent = self
  640. self.Scroller.PosX = 2
  641. self.Scroller.PosY = 2
  642. self.Scroller.Init()
  643. self.InfoPage = NewWifiInfoPage()
  644. self.InfoPage.Screen = self.Screen
  645. self.InfoPage.Name = "Wifi info"
  646. self.InfoPage.Parent = self
  647. self.InfoPage.Init()
  648. }
  649. func (self *WifiList) Draw() {
  650. self.ClearCanvas()
  651. if len(self.MyList) == 0 {
  652. return
  653. }
  654. self.Ps.Draw()
  655. for _, v := range self.MyList {
  656. v.Draw()
  657. }
  658. self.Scroller.UpdateSize(len(self.MyList)*NetItemDefaultHeight, self.PsIndex*NetItemDefaultHeight)
  659. self.Scroller.Draw()
  660. }