2019-01-21 18:16:41 -05:00
|
|
|
#!/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Fenrir TTY screen reader
|
|
|
|
# By Chrys, Storm Dragon, and contributers.
|
|
|
|
|
|
|
|
from fenrirscreenreader.core import debug
|
2019-02-04 13:05:31 -05:00
|
|
|
from fenrirscreenreader.utils import module_utils
|
2019-02-08 07:39:42 -05:00
|
|
|
import os, inspect, time
|
2019-02-04 13:05:31 -05:00
|
|
|
|
2019-02-04 13:39:36 -05:00
|
|
|
currentdir = os.path.dirname(os.path.realpath(os.path.abspath(inspect.getfile(inspect.currentframe()))))
|
|
|
|
fenrirPath = os.path.dirname(currentdir)
|
2019-01-21 18:16:41 -05:00
|
|
|
|
|
|
|
class vmenuManager():
|
|
|
|
def __init__(self):
|
2019-01-21 18:37:07 -05:00
|
|
|
self.menuDict = {}
|
|
|
|
self.currIndex = None
|
2019-01-22 18:33:11 -05:00
|
|
|
self.currMenu = ''
|
2019-01-21 18:37:07 -05:00
|
|
|
self.active = False
|
2019-02-08 07:39:42 -05:00
|
|
|
self.searchText = ''
|
|
|
|
self.lastSearchTime = time.time()
|
2019-01-21 18:16:41 -05:00
|
|
|
def initialize(self, environment):
|
|
|
|
self.env = environment
|
2019-02-04 15:56:36 -05:00
|
|
|
self.defaultVMenuPath = fenrirPath+ "/commands/vmenu-profiles/" + self.env['runtime']['inputManager'].getShortcutType()
|
2019-02-04 16:49:56 -05:00
|
|
|
self.createMenuTree()
|
2019-02-06 17:51:14 -05:00
|
|
|
self.closeAfterAction = False
|
2019-01-21 18:16:41 -05:00
|
|
|
def shutdown(self):
|
2019-01-21 18:37:07 -05:00
|
|
|
pass
|
2019-02-08 07:39:42 -05:00
|
|
|
def searchEntry(self, value):
|
|
|
|
if self.currIndex == None:
|
|
|
|
return False
|
|
|
|
if time.time() - self.lastSearchTime > 1:
|
|
|
|
self.searchText = ''
|
|
|
|
self.searchText += value
|
|
|
|
self.lastSearchTime = time.time()
|
|
|
|
startIndex = 0
|
|
|
|
while True:
|
|
|
|
entry = self.getCurrentEntry()
|
|
|
|
if entry.startswith(self.searchText):
|
|
|
|
return True
|
|
|
|
if not self.nextIndex():
|
|
|
|
return False
|
|
|
|
if
|
2019-01-28 17:29:49 -05:00
|
|
|
def setCurrMenu(self, currMenu = ''):
|
2019-02-04 16:45:12 -05:00
|
|
|
self.currIndex = None
|
|
|
|
self.currMenu = ''
|
|
|
|
if currMenu != '':
|
2019-02-04 17:11:45 -05:00
|
|
|
currMenu += ' ' + _('Menu')
|
2019-02-04 16:32:48 -05:00
|
|
|
try:
|
|
|
|
t = self.menuDict[currMenu]
|
2019-02-04 17:11:45 -05:00
|
|
|
l = list(self.menuDict.keys())
|
|
|
|
self.currIndex = [l.index(currMenu)]
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
self.currMenu = ''
|
|
|
|
self.currIndex = None
|
2019-02-04 16:32:48 -05:00
|
|
|
return
|
2019-02-04 16:45:12 -05:00
|
|
|
if self.incLevel():
|
|
|
|
self.currMenu = currMenu
|
2019-02-04 17:11:45 -05:00
|
|
|
else:
|
|
|
|
self.currMenu = ''
|
|
|
|
self.currIndex = None
|
2019-01-28 17:29:49 -05:00
|
|
|
def getCurrMenu(self):
|
|
|
|
return self.currMenu
|
2019-01-21 18:37:07 -05:00
|
|
|
def getActive(self):
|
|
|
|
return self.active
|
2019-02-06 17:51:14 -05:00
|
|
|
def togglelVMenuMode(self, closeAfterAction = True):
|
|
|
|
self.setActive(not self.getActive(), closeAfterAction)
|
|
|
|
def setActive(self, active, closeAfterAction = True):
|
2019-01-21 18:37:07 -05:00
|
|
|
self.active = active
|
2019-02-03 18:19:21 -05:00
|
|
|
if self.active:
|
2019-02-06 17:51:14 -05:00
|
|
|
self.closeAfterAction = closeAfterAction
|
2019-02-04 16:49:56 -05:00
|
|
|
try:
|
|
|
|
self.createMenuTree()
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
try:
|
|
|
|
if self.currMenu != '':
|
|
|
|
self.setCurrMenu(self.currMenu)
|
2019-02-04 17:11:45 -05:00
|
|
|
if self.currIndex == None:
|
|
|
|
if len(self.menuDict) > 0:
|
|
|
|
self.currIndex = [0]
|
2019-02-04 16:49:56 -05:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
2019-02-03 18:19:21 -05:00
|
|
|
try:
|
|
|
|
self.env['bindings'][str([1, ['KEY_ESC']])] = 'TOGGLE_VMENU_MODE'
|
|
|
|
self.env['bindings'][str([1, ['KEY_UP']])] = 'PREV_VMENU_ENTRY'
|
|
|
|
self.env['bindings'][str([1, ['KEY_DOWN']])] = 'NEXT_VMENU_ENTRY'
|
|
|
|
self.env['bindings'][str([1, ['KEY_SPACE']])] = 'CURR_VMENU_ENTRY'
|
|
|
|
self.env['bindings'][str([1, ['KEY_LEFT']])] = 'DEC_LEVEL_VMENU'
|
|
|
|
self.env['bindings'][str([1, ['KEY_RIGHT']])] = 'INC_LEVEL_VMENU'
|
|
|
|
self.env['bindings'][str([1, ['KEY_ENTER']])] = 'EXEC_VMENU_ENTRY'
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
2019-01-21 18:16:41 -05:00
|
|
|
else:
|
|
|
|
try:
|
2019-01-22 18:33:11 -05:00
|
|
|
self.currIndex = None
|
2019-01-21 18:16:41 -05:00
|
|
|
del(self.env['bindings'][str([1, ['KEY_ESC']])])
|
|
|
|
del(self.env['bindings'][str([1, ['KEY_UP']])])
|
|
|
|
del(self.env['bindings'][str([1, ['KEY_DOWN']])])
|
|
|
|
del(self.env['bindings'][str([1, ['KEY_SPACE']])])
|
2019-01-22 18:33:11 -05:00
|
|
|
del(self.env['bindings'][str([1, ['KEY_LEFT']])])
|
2019-02-03 18:19:21 -05:00
|
|
|
del(self.env['bindings'][str([1, ['KEY_RIGHT']])])
|
|
|
|
del(self.env['bindings'][str([1, ['KEY_ENTER']])])
|
2019-01-21 18:16:41 -05:00
|
|
|
except:
|
2019-02-03 18:19:21 -05:00
|
|
|
pass
|
2019-01-22 18:33:11 -05:00
|
|
|
def createMenuTree(self):
|
2019-02-04 13:39:36 -05:00
|
|
|
self.currIndex = None
|
|
|
|
menu = self.fs_tree_to_dict( self.defaultVMenuPath)
|
2019-02-03 18:19:21 -05:00
|
|
|
if menu:
|
|
|
|
self.menuDict = menu
|
2019-01-22 18:33:11 -05:00
|
|
|
def executeMenu(self):
|
|
|
|
if self.currIndex == None:
|
2019-02-03 18:19:21 -05:00
|
|
|
return
|
2019-02-04 13:39:36 -05:00
|
|
|
try:
|
2019-02-04 15:21:10 -05:00
|
|
|
command = self.getValueByPath(self.menuDict, self.currIndex)
|
|
|
|
if not command == None:
|
|
|
|
command.run()
|
2019-02-06 17:51:14 -05:00
|
|
|
if self.closeAfterAction:
|
|
|
|
self.setActive(False)
|
2019-02-04 15:56:36 -05:00
|
|
|
except Exception as e:
|
2019-02-04 16:36:35 -05:00
|
|
|
try:
|
|
|
|
self.incLevel()
|
|
|
|
text = self.getCurrentEntry()
|
|
|
|
self.env['runtime']['outputManager'].presentText(text, interrupt=True)
|
|
|
|
except:
|
|
|
|
print(e)
|
|
|
|
|
2019-01-22 18:33:11 -05:00
|
|
|
def incLevel(self):
|
|
|
|
if self.currIndex == None:
|
2019-02-04 16:45:12 -05:00
|
|
|
return False
|
2019-01-22 18:33:11 -05:00
|
|
|
try:
|
2019-01-29 17:19:49 -05:00
|
|
|
r = self.getValueByPath(self.menuDict, self.currIndex +[0])
|
2019-01-22 18:33:11 -05:00
|
|
|
if r == {}:
|
2019-02-04 16:45:12 -05:00
|
|
|
return False
|
2019-01-22 18:33:11 -05:00
|
|
|
except:
|
2019-02-04 16:45:12 -05:00
|
|
|
return False
|
2019-01-22 18:33:11 -05:00
|
|
|
self.currIndex.append(0)
|
2019-02-04 16:45:12 -05:00
|
|
|
return True
|
2019-01-22 18:33:11 -05:00
|
|
|
def decLevel(self):
|
2019-01-21 18:37:07 -05:00
|
|
|
if self.currIndex == None:
|
2019-02-04 16:45:12 -05:00
|
|
|
return False
|
2019-02-04 16:32:48 -05:00
|
|
|
if self.currMenu != '':
|
2019-02-04 16:36:35 -05:00
|
|
|
if len(self.currIndex) <= 2:
|
2019-02-04 16:45:12 -05:00
|
|
|
return False
|
2019-02-04 16:32:48 -05:00
|
|
|
elif len(self.currIndex) == 1:
|
2019-02-04 16:45:12 -05:00
|
|
|
return False
|
2019-02-04 16:26:51 -05:00
|
|
|
self.currIndex = self.currIndex[:len(self.currIndex) - 1]
|
2019-02-04 16:45:12 -05:00
|
|
|
return True
|
2019-01-21 18:16:41 -05:00
|
|
|
def nextIndex(self):
|
2019-01-21 18:37:07 -05:00
|
|
|
if self.currIndex == None:
|
2019-02-04 16:45:12 -05:00
|
|
|
return False
|
2019-02-04 13:39:36 -05:00
|
|
|
if self.currIndex[len(self.currIndex) - 1] + 1 >= len(self.getNestedByPath(self.menuDict, self.currIndex[:-1])):
|
2019-01-22 18:33:11 -05:00
|
|
|
self.currIndex[len(self.currIndex) - 1] = 0
|
2019-02-04 13:39:36 -05:00
|
|
|
else:
|
|
|
|
self.currIndex[len(self.currIndex) - 1] += 1
|
2019-02-04 16:45:12 -05:00
|
|
|
return True
|
2019-02-04 13:39:36 -05:00
|
|
|
|
2019-01-21 18:16:41 -05:00
|
|
|
def prevIndex(self):
|
2019-01-21 18:37:07 -05:00
|
|
|
if self.currIndex == None:
|
2019-02-04 16:45:12 -05:00
|
|
|
return False
|
2019-02-04 13:39:36 -05:00
|
|
|
if self.currIndex[len(self.currIndex) - 1] == 0:
|
2019-01-22 18:33:11 -05:00
|
|
|
self.currIndex[len(self.currIndex) - 1] = len(self.getNestedByPath(self.menuDict, self.currIndex[:-1])) - 1
|
2019-02-04 13:39:36 -05:00
|
|
|
else:
|
|
|
|
self.currIndex[len(self.currIndex) - 1] -= 1
|
2019-02-04 16:45:12 -05:00
|
|
|
return True
|
2019-02-04 13:39:36 -05:00
|
|
|
|
2019-01-28 18:03:31 -05:00
|
|
|
def getCurrentEntry(self):
|
|
|
|
return self.getKeysByPath(self.menuDict, self.currIndex)[self.currIndex[-1]]
|
2019-01-22 15:29:55 -05:00
|
|
|
def fs_tree_to_dict(self, path_):
|
|
|
|
for root, dirs, files in os.walk(path_):
|
2019-02-04 16:26:51 -05:00
|
|
|
tree = {d + ' ' + _('Menu'): self.fs_tree_to_dict(os.path.join(root, d)) for d in dirs if not d.startswith('__')}
|
2019-02-04 15:21:10 -05:00
|
|
|
for f in files:
|
|
|
|
try:
|
2019-02-04 16:26:51 -05:00
|
|
|
fileName, fileExtension = os.path.splitext(f)
|
|
|
|
fileName = fileName.split('/')[-1]
|
|
|
|
if fileName.startswith('__'):
|
|
|
|
continue
|
2019-02-04 15:21:10 -05:00
|
|
|
command = self.env['runtime']['commandManager'].loadFile(root + '/' + f)
|
2019-02-04 16:26:51 -05:00
|
|
|
tree.update({fileName + ' ' + _('Action'): command})
|
2019-02-04 15:21:10 -05:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
2019-01-22 15:29:55 -05:00
|
|
|
return tree # note we discontinue iteration trough os.walk
|
2019-02-04 13:39:36 -05:00
|
|
|
def getNestedByPath(self, complete, path):
|
|
|
|
path = path.copy()
|
|
|
|
if path != []:
|
|
|
|
index = list(complete.keys())[path[0]]
|
2019-02-04 15:21:10 -05:00
|
|
|
nested = self.getNestedByPath(complete[index], path[1:])
|
2019-02-04 13:39:36 -05:00
|
|
|
return nested
|
|
|
|
else:
|
|
|
|
return complete
|
|
|
|
|
|
|
|
|
2019-01-22 18:33:11 -05:00
|
|
|
def getKeysByPath(self, complete, path):
|
|
|
|
if not isinstance(complete, dict):
|
|
|
|
return[]
|
|
|
|
d = complete
|
|
|
|
for i in path[:-1]:
|
|
|
|
d = d[list(d.keys())[i]]
|
|
|
|
return list(d.keys())
|
|
|
|
|
|
|
|
def getValueByPath(self, complete, path):
|
|
|
|
if not isinstance(complete, dict):
|
2019-02-04 16:26:51 -05:00
|
|
|
return complete
|
2019-01-22 18:33:11 -05:00
|
|
|
d = complete.copy()
|
|
|
|
for i in path:
|
|
|
|
d = d[list(d.keys())[i]]
|
|
|
|
return d
|