-
-
Notifications
You must be signed in to change notification settings - Fork 766
Expand file tree
/
Copy pathNVDAState.py
More file actions
393 lines (308 loc) · 10.7 KB
/
NVDAState.py
File metadata and controls
393 lines (308 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# A part of NonVisual Desktop Access (NVDA)
# Copyright (C) 2022-2025 NV Access Limited
# This file may be used under the terms of the GNU General Public License, version 2 or later.
# For more details see: https://www.gnu.org/licenses/gpl-2.0.html
from functools import lru_cache
import os
import platform
import sys
import sysconfig
import time
import winreg
import buildVersion
import globalVars
from functools import cached_property
class _WritePaths:
@property
def configDir(self) -> str:
return globalVars.appArgs.configPath
@configDir.setter
def configDir(self, configPath: str):
globalVars.appArgs.configPath = configPath
return configPath
@property
def addonsDir(self) -> str:
return os.path.join(self.configDir, "addons")
@property
def addonStoreDir(self) -> str:
return os.path.join(self.configDir, "addonStore")
@property
def addonStoreDownloadDir(self) -> str:
return os.path.join(self.addonStoreDir, "_dl")
@property
def profilesDir(self) -> str:
return os.path.join(self.configDir, "profiles")
@property
def remoteAccessDir(self) -> str:
return os.path.join(self.configDir, "remoteAccess")
@property
def scratchpadDir(self) -> str:
return os.path.join(self.configDir, "scratchpad")
@property
def speechDictsDir(self) -> str:
return os.path.join(self.configDir, "speechDicts")
@property
def voiceDictsDir(self) -> str:
return os.path.join(self.speechDictsDir, "voiceDicts.v1")
@property
def voiceDictsBackupDir(self) -> str:
return os.path.join(self.speechDictsDir, "voiceDictsBackup.v0")
@property
def updatesDir(self) -> str:
return os.path.join(self.configDir, "updates")
@property
def modelsDir(self) -> str:
return os.path.join(self.configDir, "models")
@property
def nvdaConfigFile(self) -> str:
return os.path.join(self.configDir, "nvda.ini")
@property
def addonStateFile(self) -> str:
from addonHandler import stateFilename
return os.path.join(self.configDir, stateFilename)
@property
def profileTriggersFile(self) -> str:
return os.path.join(self.configDir, "profileTriggers.ini")
@property
def gesturesConfigFile(self) -> str:
return os.path.join(self.configDir, "gestures.ini")
@property
def speechDictDefaultFile(self) -> str:
return os.path.join(self.speechDictsDir, "default.dic")
@property
def updateCheckStateFile(self) -> str:
return os.path.join(self.configDir, "updateCheckState.pickle")
@property
def guiStateFile(self) -> str:
return os.path.join(self.configDir, "guiState.ini")
@property
def defaultStartMenuFolder(self) -> str:
"""Name of a specific folder in the start menu, not a full path"""
return buildVersion.name
@property
@lru_cache(maxsize=1)
def startMenuFolder(self) -> str | None:
"""Name of a specific folder in the start menu, not a full path"""
from config.registry import RegistryKey
try:
with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, RegistryKey.NVDA.value) as k:
return winreg.QueryValueEx(k, "Start Menu Folder")[0]
except WindowsError:
return None
@property
@lru_cache(maxsize=1)
def _startMenuFolderX86(self) -> str | None:
"""Name of a specific folder in the start menu, not a full path"""
from config.registry import RegistryKey
try:
with winreg.OpenKey(
winreg.HKEY_LOCAL_MACHINE,
RegistryKey.NVDA.value,
access=winreg.KEY_WOW64_32KEY,
) as k:
return winreg.QueryValueEx(k, "Start Menu Folder")[0]
except WindowsError:
return None
@property
@lru_cache(maxsize=1)
def defaultInstallDir(self) -> str:
from config.registry import RegistryKey
with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, RegistryKey.CURRENT_VERSION.value) as k:
programFilesPath = winreg.QueryValueEx(k, "ProgramFilesDir")[0]
return os.path.join(programFilesPath, buildVersion.name)
@property
@lru_cache(maxsize=1)
def _defaultInstallDirX86(self) -> str:
from config.registry import RegistryKey, _RegistryKeyX86
if platform.architecture()[0].startswith("64"):
# We are a 64-bit process, so we want to get the 32-bit view of the registry.
# Using winreg.KEY_WOW64_32KEY in this case raises Access Denied on a non-elevated process.
key = _RegistryKeyX86.CURRENT_VERSION.value
else:
# We are a 32-bit process, so RegistryKey defaults to the 32-bit view of the registry.
key = RegistryKey.CURRENT_VERSION.value
with winreg.OpenKey(
winreg.HKEY_LOCAL_MACHINE,
key,
) as k:
programFilesPath = winreg.QueryValueEx(k, "ProgramFilesDir")[0]
return os.path.join(programFilesPath, buildVersion.name)
@property
@lru_cache(maxsize=1)
def installDir(self) -> str | None:
from config.registry import RegistryKey
try:
with winreg.OpenKey(
winreg.HKEY_LOCAL_MACHINE,
RegistryKey.INSTALLED_COPY.value,
) as k:
return winreg.QueryValueEx(k, "UninstallDirectory")[0]
except WindowsError:
return None
@property
@lru_cache(maxsize=1)
def _installDirX86(self) -> str | None:
from config.registry import RegistryKey
try:
with winreg.OpenKey(
winreg.HKEY_LOCAL_MACHINE,
RegistryKey.INSTALLED_COPY.value,
access=winreg.KEY_WOW64_32KEY,
) as k:
return winreg.QueryValueEx(k, "UninstallDirectory")[0]
except WindowsError:
return None
def getSymbolsConfigFile(self, locale: str) -> str:
return os.path.join(self.configDir, f"symbols-{locale}.dic")
def getProfileConfigFile(self, name: str) -> str:
return os.path.join(self.profilesDir, f"{name}.ini")
class _ReadPaths:
@property
def versionedLibPath(self) -> str:
versionedLibPath = os.path.join(globalVars.appDir, "lib")
if not isRunningAsSource():
# When running as a py2exe build, libraries are in a version-specific directory
versionedLibPath = os.path.join(versionedLibPath, buildVersion.version)
return versionedLibPath
@property
def versionedLibX86Path(self) -> str:
return os.path.join(self.versionedLibPath, "x86")
@cached_property
def versionedLibAMD64Path(self) -> str:
import winVersion
arch = winVersion.getWinVer().processorArchitecture
return os.path.join(
self.versionedLibPath,
(
# On ARM64 Windows, we use arm64ec libraries for interop with x64 code.
"arm64ec" if arch == "ARM64" else "x64"
),
)
@property
def versionedLibARM64Path(self) -> str:
return os.path.join(self.versionedLibPath, "arm64")
@cached_property
def coreArchLibPath(self) -> str:
match sysconfig.get_platform():
case "win-amd64":
return self.versionedLibAMD64Path
case "win-arm64":
return self.versionedLibARM64Path
case "win32":
return self.versionedLibX86Path
case _:
raise RuntimeError("Unsupported platform")
@property
def nvdaHelperRemoteDll(self) -> str:
return os.path.join(self.coreArchLibPath, "nvdaHelperRemote.dll")
@property
def nvdaHelperLocalDll(self) -> str:
return os.path.join(self.coreArchLibPath, "nvdaHelperLocal.dll")
@property
def nvdaHelperLocalWin10Dll(self) -> str:
return os.path.join(self.coreArchLibPath, "nvdaHelperLocalWin10.dll")
@property
def mathCATDir(self) -> str:
"""
Base directory for MathCAT assets (rules etc.).
"""
if isRunningAsSource():
base = os.path.dirname(globalVars.appDir)
else:
base = globalVars.appDir
return os.path.join(
base,
"include",
"nvda-mathcat",
"assets",
)
@property
def UIARemoteDll(self) -> str:
return os.path.join(self.coreArchLibPath, "UIARemote.dll")
@property
def javaAccessBridgeDLL(self) -> str:
return os.path.join(globalVars.appDir, "windowsaccessbridge.dll")
WritePaths = _WritePaths()
ReadPaths = _ReadPaths()
def isRunningAsSource() -> bool:
"""
True if NVDA is running as a source copy.
When running as an installed copy, py2exe sets sys.frozen to 'windows_exe'.
"""
return getattr(sys, "frozen", None) is None
def _allowDeprecatedAPI() -> bool:
"""
Used for marking code as deprecated.
This should never be False in released code.
Making this False may be useful for testing if code is compliant without using deprecated APIs.
Note that deprecated code may be imported at runtime,
and as such, this value cannot be changed at runtime to test compliance.
"""
return True
def getStartTime() -> float:
return globalVars.startTime
def _initializeStartTime() -> None:
assert globalVars.startTime == 0
globalVars.startTime = time.time()
def _getExitCode() -> int:
return globalVars.exitCode
def _setExitCode(exitCode: int) -> None:
globalVars.exitCode = exitCode
def shouldWriteToDisk() -> bool:
"""
Never save config or state if running securely or if running from the launcher.
When running from the launcher we don't save settings because the user may decide not to
install this version, and these settings may not be compatible with the already
installed version. See #7688
"""
return not (globalVars.appArgs.secure or globalVars.appArgs.launcher)
class _TrackNVDAInitialization:
"""
During NVDA initialization,
core._initializeObjectCaches needs to cache the desktop object,
regardless of lock state.
Security checks may cause the desktop object to not be set if NVDA starts on the lock screen.
As such, during initialization, NVDA should behave as if Windows is unlocked,
i.e. winAPI.sessionTracking.isLockScreenModeActive should return False.
"""
_isNVDAInitialized = False
"""When False, isLockScreenModeActive is forced to return False.
"""
@staticmethod
def markInitializationComplete():
assert not _TrackNVDAInitialization._isNVDAInitialized
_TrackNVDAInitialization._isNVDAInitialized = True
@staticmethod
def isInitializationComplete() -> bool:
return _TrackNVDAInitialization._isNVDAInitialized
def _forceSecureModeEnabled() -> bool:
# Avoid circular import
from config.registry import RegistryKey
try:
with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, RegistryKey.NVDA.value) as k:
return bool(winreg.QueryValueEx(k, RegistryKey.FORCE_SECURE_MODE_SUBKEY.value)[0])
except WindowsError:
# Expected state by default, forceSecureMode parameter not set
return False
def _serviceDebugEnabled() -> bool:
# Avoid circular import
from config.registry import RegistryKey
try:
with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, RegistryKey.NVDA.value) as k:
return bool(winreg.QueryValueEx(k, RegistryKey.SERVICE_DEBUG_SUBKEY.value)[0])
except WindowsError:
# Expected state by default, serviceDebug parameter not set
return False
def _configInLocalAppDataEnabled() -> bool:
# Avoid circular imports
from config.registry import RegistryKey
from logHandler import log
try:
with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, RegistryKey.NVDA.value) as k:
return bool(winreg.QueryValueEx(k, RegistryKey.CONFIG_IN_LOCAL_APPDATA_SUBKEY.value)[0])
except FileNotFoundError:
log.debug("Installed user config is not in local app data")
return False
except WindowsError:
# Expected state by default, configInLocalAppData parameter not set
return False