def Init(self, main_screen): global bus, devices, adapter self._Page = BluetoothPage() self._Page._Dbus = bus self._Page._Devices = devices self._Page._Adapter = adapter self._Page._Screen = main_screen self._Page._Name = "Bluetooth" self._Page._Leader = self self._Page.Init() self._PairPage = BleAgentPairPage() self._PairPage._Screen = main_screen self._PairPage._Name = "Bluetooth" self._PairPage.Init() self._Page3 = Keyboard() self._Page3._Name = "Enter" self._Page3._Screen = main_screen self._Page3.Init() bus.add_signal_receiver( self._Page.DbusPropertiesChanged, dbus_interface="org.freedesktop.DBus.Properties", signal_name="PropertiesChanged", arg0="org.bluez.Device1", path_keyword="path") self.RegisterMyAgent()
def Init(self): if self._Screen != None: if self._Screen._CanvasHWND != None and self._CanvasHWND == None: self._HWND = self._Screen._CanvasHWND self._CanvasHWND = pygame.Surface( (self._Screen._Width, self._BGheight)) self._PosX = self._Index * self._Screen._Width self._Width = self._Screen._Width ## equal to screen width self._Height = self._Screen._Height done = IconItem() done._ImgSurf = MyIconPool.GiveIconSurface("done") done._MyType = ICON_TYPES["STAT"] done._Parent = self self._Icons["done"] = done ps = InfoPageSelector() ps._Parent = self self._Ps = ps self._PsIndex = 0 self.SyncList() self._Scroller = ListScroller() self._Scroller._Parent = self self._Scroller._PosX = self._Width - 10 self._Scroller._PosY = 2 self._Scroller.Init() self._Scroller.SetCanvasHWND(self._CanvasHWND) self._remove_page = YesCancelConfirmPage() self._remove_page._Screen = self._Screen self._remove_page._StartOrA_Event = self.RemoveGame self._remove_page._Name = "Are you sure?" self._remove_page.Init() self._Keyboard = Keyboard() self._Keyboard._Name = "Enter warehouse addr" self._Keyboard._FootMsg = ["Nav.", "Add", "ABC", "Backspace", "Enter"] self._Keyboard._Screen = self._Screen self._Keyboard.Init() self._Keyboard.SetPassword("github.com/clockworkpi/warehouse") self._Keyboard._Caller = self self._PreviewPage = ImageDownloadProcessPage() self._PreviewPage._Screen = self._Screen self._PreviewPage._Name = "Preview" self._PreviewPage.Init() self._LoadHousePage = LoadHousePage() self._LoadHousePage._Screen = self._Screen self._LoadHousePage._Name = "Warehouse" self._LoadHousePage._Caller = self self._LoadHousePage.Init()
def InitPasswordPage(main_screen): myvars.PasswordPage = Keyboard() myvars.PasswordPage._Name = "Enter wifi password" myvars.PasswordPage._Screen = main_screen myvars.PasswordPage.Init()
class APIOBJ(object): _Page = None _PairPage = None _Page3 = None _Prompts = {} # string key,string value _PromptType = None _MyAgent = None def __init__(self): self._Prompts["PIN"] = "" self._Prompts["PASS"] = "" def OnKbdReturnBackCb(self): if self._PromptType == None: return else: if self._PromptType in self._Prompts: inputed = "".join(self._Page3._Textarea._MyWords) self._Prompts[self._PromptType] = inputed self._PromptType = None ##clear def Ask(self, prompt, prompt_type=None): self._Screen.PushPage(self._Page3) self._Page3.SetPassword("") self._Page3._Name = prompt self._Page3._Caller = self self._Screen.Draw() self._Screen.SwapAndShow() if prompt_type != None: self._PromptType = prompt_type def RegisterMyAgent(self): global AGENT_PATH, bus, devices, adapter capability = "KeyboardDisplay" self._MyAgent = BleAgent(bus, AGENT_PATH) self._MyAgent._Leader = self obj = bus.get_object(BUS_NAME, "/org/bluez") manager = dbus.Interface(obj, "org.bluez.AgentManager1") manager.RegisterAgent(AGENT_PATH, capability) print("BleAgent %s registered" % AGENT_PATH) def Init(self, main_screen): global bus, devices, adapter self._Page = BluetoothPage() self._Page._Dbus = bus self._Page._Devices = devices self._Page._Adapter = adapter self._Page._Screen = main_screen self._Page._Name = "Bluetooth" self._Page._Leader = self self._Page.Init() self._PairPage = BleAgentPairPage() self._PairPage._Screen = main_screen self._PairPage._Name = "Bluetooth" self._PairPage.Init() self._Page3 = Keyboard() self._Page3._Name = "Enter" self._Page3._Screen = main_screen self._Page3.Init() bus.add_signal_receiver( self._Page.DbusPropertiesChanged, dbus_interface="org.freedesktop.DBus.Properties", signal_name="PropertiesChanged", arg0="org.bluez.Device1", path_keyword="path") self.RegisterMyAgent() def API(self, main_screen): if main_screen != None: main_screen.PushPage(self._Page) main_screen.Draw() main_screen.SwapAndShow()
class GameStorePage(Page): _FootMsg = ["Nav", "Update", "Up", "Back", "Select"] _MyList = [] _ListFont12 = MyLangManager.TrFont("notosanscjk12") _ListFont15 = MyLangManager.TrFont("varela15") _AList = {} # _Scrolled = 0 _BGwidth = 320 _BGheight = 240 - 24 - 20 _DrawOnce = False _Scroller = None _InfoPage = None _Downloading = None _aria2_db = "aria2tasks.db" _warehouse_db = "warehouse.db" _GobjTimer = -1 _Scrolled_cnt = 0 def __init__(self): Page.__init__(self) self._Icons = {} self._MyStack = RPCStack() #title file type ## Two level url , only github.com repos = [{ "title": "github.com/clockworkpi/warehouse", "file": "https://raw.githubusercontent.com/clockworkpi/warehouse/master/index.json", "type": "source" }] self._MyStack.Push(repos) def GObjectUpdateProcessInterval(self): ret = True dirty = False for x in self._MyList: if x._Type == "launcher" or x._Type == "pico8" or x._Type == "tic80": percent = config.RPC.getPercent(x._Value["file"]) if percent is not None: x.SetSmallText(str(percent) + "%") dirty = True else: x.SetSmallText("") if self._Screen.CurPage() == self and dirty == True: self._Screen.Draw() self._Screen.SwapAndShow() return ret def SyncWarehouse(self): try: conn = sqlite3.connect(self._warehouse_db) conn.row_factory = dict_factory c = conn.cursor() ret = c.execute("SELECT * FROM warehouse").fetchall() conn.close() return ret except Exception as ex: print(ex) return None return None def SyncTasks(self): try: conn = sqlite3.connect(self._aria2_db) conn.row_factory = dict_factory c = conn.cursor() ret = c.execute("SELECT * FROM tasks").fetchall() conn.close() return ret except Exception as ex: print(ex) return None return None def SyncList(self): self._MyList = [] start_x = 0 start_y = 0 last_height = 0 repos = [] stk = self._MyStack.Last() stk_lev = self._MyStack.Length() repos.extend(stk) add_new_house = [{ "title": "Add new warehouse...", "file": "master/index.json", "type": "add_house", "status": "complete" }] if stk_lev == 1: # on top ware_menu = self.SyncWarehouse() if ware_menu != None and len(ware_menu) > 0: #print(ware_menu) repos.extend(ware_menu) tasks_menu = self.SyncTasks() if tasks_menu != None and len(tasks_menu) > 0: #print(tasks_menu) repos.extend(tasks_menu) #print(repos) repos.extend(add_new_house) for i, u in enumerate(repos): #print(i,u) li = GameStoreListItem() li._CanvasHWND = self._CanvasHWND li._Parent = self li._PosX = start_x li._PosY = start_y + last_height li._Width = Width li._Fonts["normal"] = self._ListFont15 li._Fonts["small"] = self._ListFont12 li._Active = False li._ReadOnly = True li._Value = u li._Type = u["type"] li.Init(u["title"]) if stk_lev > 1: remote_file_url = u["file"] menu_file = remote_file_url.split( "raw.githubusercontent.com")[1] local_menu_file = "%s/aria2download%s" % ( os.path.expanduser('~'), menu_file) if FileExists(local_menu_file): li._ReadOnly = False else: li._ReadOnly = True elif stk_lev == 1: if "status" in u: if u["status"] == "complete": li._ReadOnly = False if u["type"] == "source": li._ReadOnly = False last_height += li._Height if li._Type == "launcher" or li._Type == "pico8" or li._Type == "tic80": li.SetSmallText("") self._MyList.append(li) self.RefreshPsIndex() def Init(self): if self._Screen != None: if self._Screen._CanvasHWND != None and self._CanvasHWND == None: self._HWND = self._Screen._CanvasHWND self._CanvasHWND = pygame.Surface( (self._Screen._Width, self._BGheight)) self._PosX = self._Index * self._Screen._Width self._Width = self._Screen._Width ## equal to screen width self._Height = self._Screen._Height done = IconItem() done._ImgSurf = MyIconPool.GiveIconSurface("done") done._MyType = ICON_TYPES["STAT"] done._Parent = self self._Icons["done"] = done ps = InfoPageSelector() ps._Parent = self self._Ps = ps self._PsIndex = 0 self.SyncList() self._Scroller = ListScroller() self._Scroller._Parent = self self._Scroller._PosX = self._Width - 10 self._Scroller._PosY = 2 self._Scroller.Init() self._Scroller.SetCanvasHWND(self._CanvasHWND) self._remove_page = YesCancelConfirmPage() self._remove_page._Screen = self._Screen self._remove_page._StartOrA_Event = self.RemoveGame self._remove_page._Name = "Are you sure?" self._remove_page.Init() self._Keyboard = Keyboard() self._Keyboard._Name = "Enter warehouse addr" self._Keyboard._FootMsg = ["Nav.", "Add", "ABC", "Backspace", "Enter"] self._Keyboard._Screen = self._Screen self._Keyboard.Init() self._Keyboard.SetPassword("github.com/clockworkpi/warehouse") self._Keyboard._Caller = self self._PreviewPage = ImageDownloadProcessPage() self._PreviewPage._Screen = self._Screen self._PreviewPage._Name = "Preview" self._PreviewPage.Init() self._LoadHousePage = LoadHousePage() self._LoadHousePage._Screen = self._Screen self._LoadHousePage._Name = "Warehouse" self._LoadHousePage._Caller = self self._LoadHousePage.Init() def ResetHouse(self): if self._PsIndex > len(self._MyList) - 1: return cur_li = self._MyList[self._PsIndex] if cur_li._Value["type"] == "source": remote_file_url = cur_li._Value["file"] menu_file = remote_file_url.split("raw.githubusercontent.com")[ 1] #assume master branch local_menu_file = "%s/aria2download%s" % (os.path.expanduser('~'), menu_file) local_menu_file_path = os.path.dirname(local_menu_file) print(local_menu_file_path) local_jsons = glob.glob(local_menu_file_path + "/**/*.json") try: if os.path.exists(local_menu_file): os.remove(local_menu_file) if os.path.exists(local_menu_file + ".aria2"): os.remove(local_menu_file + ".aria2") for x in local_jsons: os.remove(x) except Exception as ex: print(ex) self._Screen._MsgBox.SetText("Done") self._Screen._MsgBox.Draw() self._Screen.SwapAndShow() def LoadHouse(self): if self._PsIndex > len(self._MyList) - 1: return cur_li = self._MyList[self._PsIndex] if cur_li._Value["type"] == "source" or cur_li._Value["type"] == "dir": self._LoadHousePage._URL = cur_li._Value["file"] self._Screen.PushPage(self._LoadHousePage) self._Screen.Draw() self._Screen.SwapAndShow() def PreviewGame(self): if self._PsIndex > len(self._MyList) - 1: return cur_li = self._MyList[self._PsIndex] if cur_li._Value["type"] == "launcher" or cur_li._Value[ "type"] == "pico8" or cur_li._Value["type"] == "tic80": if "shots" in cur_li._Value: print(cur_li._Value["shots"]) self._PreviewPage._URL = cur_li._Value["shots"] self._Screen.PushPage(self._PreviewPage) self._Screen.Draw() self._Screen.SwapAndShow() def RemoveGame(self): if self._PsIndex > len(self._MyList) - 1: return cur_li = self._MyList[self._PsIndex] #if cur_li._Active == True: # return print("Remove cur_li._Value", cur_li._Value) if cur_li._Value["type"] == "source": print("remove a source") try: conn = sqlite3.connect(self._warehouse_db) conn.row_factory = dict_factory c = conn.cursor() c.execute("DELETE FROM warehouse WHERE file = '%s'" % cur_li._Value["file"]) conn.commit() conn.close() except Exception as ex: print(ex) elif cur_li._Value["type"] == "launcher" or cur_li._Value[ "type"] == "pico8" or cur_li._Value["type"] == "tic80": remote_file_url = cur_li._Value["file"] menu_file = remote_file_url.split("raw.githubusercontent.com")[1] local_menu_file = "%s/aria2download%s" % (os.path.expanduser('~'), menu_file) local_menu_file_path = os.path.dirname(local_menu_file) gid, ret = config.RPC.urlDownloading(remote_file_url) if ret == True: config.RPC.remove(gid) try: if os.path.exists(local_menu_file): os.remove(local_menu_file) if os.path.exists(local_menu_file + ".aria2"): os.remove(local_menu_file + ".aria2") if os.path.exists( os.path.join(local_menu_file_path, cur_li._Value["title"])): rmtree( os.path.join(local_menu_file_path, cur_li._Value["title"])) except Exception as ex: print(ex) def Click(self): if self._PsIndex > len(self._MyList) - 1: return cur_li = self._MyList[self._PsIndex] #if cur_li._Active == True: # return print("cur_li._Value", cur_li._Value) if cur_li._Value["type"] == "source" or cur_li._Value["type"] == "dir": remote_file_url = cur_li._Value["file"] menu_file = remote_file_url.split("raw.githubusercontent.com")[ 1] #assume master branch local_menu_file = "%s/aria2download%s" % (os.path.expanduser('~'), menu_file) print(local_menu_file) if FileExists(local_menu_file) == False: self.LoadHouse() else: #read the local_menu_file, push into stack,display menu self._Downloading = None try: with open(local_menu_file) as json_file: local_menu_json = json.load(json_file) print(local_menu_json) self._MyStack.Push(local_menu_json["list"]) self.SyncList() self._Screen.Draw() self._Screen.SwapAndShow() except Exception as ex: print(ex) self._Screen._MsgBox.SetText("Open house failed ") self._Screen._MsgBox.Draw() self._Screen.SwapAndShow() elif cur_li._Value["type"] == "add_house": print("show keyboard to add ware house") self._Screen.PushCurPage() self._Screen.SetCurPage(self._Keyboard) else: #download the game probably remote_file_url = cur_li._Value["file"] menu_file = remote_file_url.split("raw.githubusercontent.com")[1] local_menu_file = "%s/aria2download%s" % (os.path.expanduser('~'), menu_file) if FileExists(local_menu_file) == False: gid, ret = config.RPC.urlDownloading(remote_file_url) if ret == False: gid = config.RPC.addUri(remote_file_url, options={"out": menu_file}) self._Downloading = gid print("stack length ", self._MyStack.Length()) """ if self._MyStack.Length() > 1:## not on the top list page try: conn = sqlite3.connect(self._aria2_db) c = conn.cursor() c.execute("INSERT INTO tasks(gid,title,file,type,status,fav) VALUES ('"+gid+"','"+cur_li._Value["title"]+"','"+cur_li._Value["file"]+"','"+cur_li._Value["type"]+"','active','0')") conn.commit() conn.close() except Exception as ex: print("SQLITE3 ",ex) """ else: print( config.RPC.tellStatus( gid, ["status", "totalLength", "completedLength"])) self._Screen._MsgBox.SetText("Getting the game now") self._Screen._MsgBox.Draw() self._Screen.SwapAndShow() pygame.time.delay(800) self._Screen._TitleBar.Redraw() else: print("file downloaded" ) # maybe check it if is installed,then execute it if cur_li._Value[ "type"] == "launcher" and cur_li._ReadOnly == False: local_menu_file_path = os.path.dirname(local_menu_file) game_sh = os.path.join(local_menu_file_path, cur_li._Value["title"], cur_li._Value["title"] + ".sh") #game_sh = reconstruct_broken_string( game_sh) print("run game: ", game_sh, os.path.exists(game_sh)) self._Screen.RunEXE(game_sh) if cur_li._Value[ "type"] == "pico8" and cur_li._ReadOnly == False: if os.path.exists( "/home/cpi/games/PICO-8/pico-8/pico8") == True: game_sh = "/home/cpi/launcher/Menu/GameShell/50_PICO-8/PICO-8.sh" self._Screen.RunEXE(game_sh) else: self._Screen._MsgBox.SetText("Purchase pico8") self._Screen._MsgBox.Draw() self._Screen.SwapAndShow() pygame.time.delay(800) if cur_li._Value[ "type"] == "tic80" and cur_li._ReadOnly == False: game_sh = "/home/cpi/apps/Menu/51_TIC-80/TIC-80.sh" self._Screen.RunEXE(game_sh) def OnAria2CompleteCb(self, gid): print("OnAria2CompleteCb ", gid) self.SyncList() self._Screen.Draw() self._Screen.SwapAndShow() def raw_github_com(self, _url): #eg: github.com/clockworkpi/warehouse if _url.startswith("github.com") == False: return False parts = _url.split("/") if len(parts) != 3: return False return "/".join([ "https://raw.githubusercontent.com", parts[1], parts[2], "master/index.json" ]) def OnKbdReturnBackCb(self): inputed = "".join(self._Keyboard._Textarea._MyWords).strip() inputed = inputed.replace("http://", "") inputed = inputed.replace("https://", "") if inputed.endswith(".git"): inputed = inputed[:len(inputed) - 4] if inputed.endswith("/"): inputed = inputed[:len(inputed) - 1] print("last: ", inputed) try: conn = sqlite3.connect(self._warehouse_db) conn.row_factory = dict_factory c = conn.cursor() ret = c.execute("SELECT * FROM warehouse WHERE title='%s'" % inputed).fetchone() if ret != None: self._Screen._MsgBox.SetText("Warehouse existed!") self._Screen._MsgBox.Draw() self._Screen.SwapAndShow() else: if "github.com/clockworkpi/warehouse" in inputed: self._Screen._MsgBox.SetText("Warehouse existed!") self._Screen._MsgBox.Draw() self._Screen.SwapAndShow() else: valid_url = self.raw_github_com(inputed) if valid_url == False: self._Screen._MsgBox.SetText("Warehouse url error!") self._Screen._MsgBox.Draw() self._Screen.SwapAndShow() else: sql_insert = """ INSERT INTO warehouse(title,file,type) VALUES( '%s', '%s', 'source');""" % (inputed, valid_url) c.execute(sql_insert) conn.commit() self.SyncList() self._Screen.Draw() self._Screen.SwapAndShow() conn.close() except Exception as ex: print(ex) def OnLoadCb(self): self._Scrolled = 0 self._PosY = 0 self._DrawOnce = False #sync print("OnLoadCb") if self._MyStack.Length() == 1: self._FootMsg[2] = "Remove" self._FootMsg[1] = "Update" else: self._FootMsg[2] = "Remove" self._FootMsg[1] = "Preview" self._GobjTimer = gobject.timeout_add( 500, self.GObjectUpdateProcessInterval) self.SyncList() def OnReturnBackCb(self): if self._MyStack.Length() == 1: self._FootMsg[2] = "Remove" self._FootMsg[1] = "Update" else: self._FootMsg[2] = "Remove" self._FootMsg[1] = "Preview" self.SyncList() self.RestoreScrolled() self._Screen.Draw() self._Screen.SwapAndShow() """ self.ReturnToUpLevelPage() self._Screen.Draw() self._Screen.SwapAndShow() """ # def ScrollDown(self): # if len(self._MyList) == 0: # return # self._PsIndex += 1 # if self._PsIndex >= len(self._MyList): # self._PsIndex = len(self._MyList)-1 # cur_li = self._MyList[self._PsIndex] # if cur_li._PosY+cur_li._Height > self._Height: # for i in range(0, len(self._MyList)): # self._MyList[i]._PosY -= self._MyList[i]._Height # self._Scrolled_cnt -= self._MyList[i]._Height # def ScrollUp(self): # if len(self._MyList) == 0: # return # self._PsIndex -= 1 # if self._PsIndex < 0: # self._PsIndex = 0 # cur_li = self._MyList[self._PsIndex] # if cur_li._PosY < 0: # for i in range(0, len(self._MyList)): # self._MyList[i]._PosY += self._MyList[i]._Height # self._Scrolled_cnt += self._MyList[i]._Height def KeyDown(self, event): if IsKeyMenuOrB(event.key): if self._MyStack.Length() > 1: self._MyStack.Pop() if self._MyStack.Length() == 1: self._FootMsg[2] = "Remove" self._FootMsg[1] = "Update" else: self._FootMsg[2] = "Remove" self._FootMsg[1] = "Preview" if self._MyStack.Length() == 2: self._FootMsg[2] = "" self._FootMsg[1] = "" self.SyncList() self._Screen.Draw() self._Screen.SwapAndShow() elif self._MyStack.Length() == 1: self.ReturnToUpLevelPage() self._Screen.Draw() self._Screen.SwapAndShow() gobject.source_remove(self._GobjTimer) if IsKeyStartOrA(event.key): self.Click() if self._MyStack.Length() == 1: self._FootMsg[2] = "Remove" self._FootMsg[1] = "Update" else: self._FootMsg[2] = "Remove" self._FootMsg[1] = "Preview" if self._MyStack.Length() == 2: self._FootMsg[2] = "" self._FootMsg[1] = "" self._Screen.Draw() self._Screen.SwapAndShow() if event.key == CurKeys["X"]: #print(self._MyStack.Length() ) if self._PsIndex <= len(self._MyList) - 1: cur_li = self._MyList[self._PsIndex] if cur_li._Type != "dir": if self._MyStack.Length() == 1 and self._PsIndex == 0: pass #predefined source else: self._Screen.PushPage(self._remove_page) self._remove_page._StartOrA_Event = self.RemoveGame self._Screen.Draw() self._Screen.SwapAndShow() return self.SyncList() self._Screen.Draw() self._Screen.SwapAndShow() if event.key == CurKeys["Y"]: if self._MyStack.Length() == 1: self.ResetHouse() else: self.PreviewGame() if event.key == CurKeys["Up"]: # self.ScrollUp() self.SpeedScroll(event.key) self.ScrollUp(self._ScrollStep) self._Screen.Draw() self._Screen.SwapAndShow() if event.key == CurKeys["Down"]: # self.ScrollDown() self.SpeedScroll(event.key) self.ScrollDown(self._ScrollStep) self._Screen.Draw() self._Screen.SwapAndShow() if event.key == CurKeys["Right"]: # PageDown self.ScrollDown(5) self._Screen.Draw() self._Screen.SwapAndShow() if event.key == CurKeys["Left"]: # PageUp self.ScrollUp(5) self._Screen.Draw() self._Screen.SwapAndShow() def RestoreScrolled(self): for i in range(0, len(self._MyList)): self._MyList[i]._PosY += self._Scrolled_cnt def Draw(self): self.ClearCanvas() if len(self._MyList) == 0: return else: if len(self._MyList) * self._MyList[0]._Height > self._Height: self._Ps._Width = self._Width - 11 self._Ps.Draw() for i in self._MyList: if i._PosY > self._Height + self._Height / 2: break if i._PosY < 0: continue i.Draw() self._Scroller.UpdateSize( len(self._MyList) * self._MyList[0]._Height, self._PsIndex * self._MyList[0]._Height) self._Scroller.Draw() else: self._Ps._Width = self._Width self._Ps.Draw() for i in self._MyList: if i._PosY > self._Height + self._Height / 2: break if i._PosY < 0: continue i.Draw() if self._HWND != None: self._HWND.fill(MySkinManager.GiveColor("White")) self._HWND.blit( self._CanvasHWND, (self._PosX, self._PosY, self._Width, self._Height))