-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsharedComputer.py
291 lines (263 loc) · 11.6 KB
/
sharedComputer.py
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
# -*- coding: UTF-8 -*-
# sharedComputer: Plugin to using shared computers
# https://github.com/nvaccess/nvda/issues/4093
# https://github.com/nvaccess/nvda/pull/7506/files
# Copyright (C) 2018 Noelia Ruiz Martínez, Robert Hänggi
# Released under GPL 2
import globalPluginHandler
import addonHandler
import gui, ui, wx, winUser, config, re
from gui import guiHelper, nvdaControls
from gui.settingsDialogs import SettingsDialog
from keyboardHandler import KeyboardInputGesture
from globalCommands import SCRCAT_CONFIG
from comtypes import HRESULT,GUID,IUnknown, COMMETHOD, POINTER, CoCreateInstance, cast, c_float
from ctypes.wintypes import BOOL, DWORD, UINT
from logHandler import log
from api import processPendingEvents
addonHandler.initTranslation()
helpPath = addonHandler.getCodeAddon().getDocFilePath()
numLockByLayoutDefault = "0" if config.conf['keyboard']['keyboardLayout'] == "desktop" else "2"
confspec = {
"numLockActivationChoice": "integer(default="+numLockByLayoutDefault+")",
"volumeCorrectionChoice": "integer(default=0)",
"volumeLevel": "integer(0, 100, default = 50)"
}
config.conf.spec["sharedComputer"] = confspec
speakers = None
def _getBaseValue(key):
try:
val1 = config.conf.profiles[0]["sharedComputer"].get(key)
except:
val1 = None
val2 = config.conf["sharedComputer"].get(key)
return int(val1) if val1 is not None else int(val2)
class GlobalPlugin(globalPluginHandler.GlobalPlugin):
def handleConfigProfileSwitch(self):
activation = config.conf["sharedComputer"]["numLockActivationChoice"]
if activation < 2 and winUser.getKeyState(winUser.VK_NUMLOCK) != activation:
KeyboardInputGesture.fromName("numLock").send()
def changeVolumeLevel(self, targetLevel, mode):
if speakers is not None:
for attempt in range(2):
processPendingEvents()
level = int(speakers.GetMasterVolumeLevelScalar()*100+0.5)
log.info("Level speakers at Startup: {} Percent".format(level))
if level < targetLevel or (mode == 1 and level > targetLevel):
speakers.SetMasterVolumeLevelScalar(targetLevel/100.0,None)
muteState = speakers.GetMute()
log.info("speakers at Startup: {}".format(("Unmuted","Muted")[muteState]))
if muteState:
speakers.SetMute(0, None)
log.info("speakers after correction: {} Percent, {}".format(
int(speakers.GetMasterVolumeLevelScalar()*100+0.5),
("Unmuted","Muted")[speakers.GetMute()]))
def __init__(self):
global speakers
speakers = getVolumeObject()
super(globalPluginHandler.GlobalPlugin, self).__init__()
volLevel = _getBaseValue("volumeLevel")
volMode = _getBaseValue("volumeCorrectionChoice")
if volMode < 2:
wx.CallAfter(self.changeVolumeLevel, volLevel, volMode)
self.numLockState = winUser.getKeyState(winUser.VK_NUMLOCK)
self.handleConfigProfileSwitch()
try:
config.configProfileSwitched.register(self.handleConfigProfileSwitch)
except AttributeError:
pass
# Gui
self.prefsMenu = gui.mainFrame.sysTrayIcon.preferencesMenu
self.settingsItem = self.prefsMenu.Append(wx.ID_ANY,
# Translators: name of the entry in the preferences menu.
_("Shared Comp&uter..."))
gui.mainFrame.sysTrayIcon.Bind(wx.EVT_MENU, self.onSettings, self.settingsItem)
# first installation: open dialog automatically
# if not (config.conf["sharedComputer"].isSet("volumeCorrectionChoice") or SettingsDialog._hasInstance or gui.isInMessageBox):
# wx.CallAfter(self.onSettings, None)
def terminate(self):
if winUser.getKeyState(winUser.VK_NUMLOCK) != self.numLockState:
KeyboardInputGesture.fromName("numLock").send()
try:
config.configProfileSwitched.unregister(self.handleConfigProfileSwitch)
except AttributeError: # This is for backward compatibility
pass
try:
self.prefsMenu.RemoveItem(self.settingsItem)
except: # Compatible with Python 2 and 3
pass
def onSettings(self, evt):
gui.mainFrame._popupSettingsDialog(AddonSettingsDialog)
def script_settings(self, gesture):
wx.CallAfter(self.onSettings, None)
script_settings.category = SCRCAT_CONFIG
# Translators: message presented in input mode.
script_settings.__doc__ = _("Shows the Shared Computer settings dialog.")
class AddonSettingsDialog(SettingsDialog):
# Translators: Title of a dialog.
title = _("Shared Computer Settings (F1 for Context Help)")
# Translators: title of the browsable help message
helpTitle = _("Help")
# Translators: advice on how to close the browsable help message
hint = _("Press escape to close this message.")
hint = "<p>{}</p>".format(hint)
lastFocus = None
helpDict = {}
with open(helpPath,'r') as helpFile:
helpHTML = helpFile.read().decode("utf8")
sections = re.match('(.*<body>).+(<div.+</div>).+(<div.+</div>).*(<div.+</div>).*(</body>.*)',
helpHTML,
flags=re.DOTALL).groups()
del helpHTML
def makeSettings(self, settingsSizer):
sHelper = guiHelper.BoxSizerHelper(self, sizer=settingsSizer)
# Translators: label of a combo box
activateLabel = _("&Activate NumLock:")
self.activateChoices = (
# Translators: Choice in Activate NumLock combo box
_("Off"),
# Translators: Choice in Activate NumLock combo box
_("On"),
# Translators: Choice in Activate NumLock combo box
_("Never change"))
self.activateList = sHelper.addLabeledControl(activateLabel, wx.Choice, choices=self.activateChoices)
self.activateList.Selection = config.conf["sharedComputer"]["numLockActivationChoice"]
# Translators: label of a combo box
volumeLabel = _("System &Volume at Start:")
self.volumeChoices = (
# Translators: Choice in Volume at Start combo box
_("Ensure a minimum of"),
# Translators: Choice in Volume at Start combo box
_("Set exactly to"),
# Translators: Choice in Volume at Start combo box
_("Never change"))
self.volumeList = sHelper.addLabeledControl(volumeLabel, wx.Choice, choices=self.volumeChoices)
self.volumeList.Selection = _getBaseValue("volumeCorrectionChoice")
# Translators: Label of a spin control
self.volumeLevel = sHelper.addLabeledControl(_("Volume &Level:"),
nvdaControls.SelectOnFocusSpinCtrl,
min = 20 if self.volumeList.Selection==1 else 0,
initial = _getBaseValue("volumeLevel"))
# several event bindings
self.Bind(wx.EVT_ACTIVATE, self.onDialogActivate)
self.volumeList.Bind(wx.EVT_CHOICE, self.onChoice)
self.volumeLevel.Bind(wx.EVT_CHAR_HOOK, self.onKey)
for number, child in enumerate([self.activateList, self.volumeList, self.volumeLevel], 1):
self.helpDict[child.GetId()] = '\n'.join((self.sections[0], self.sections[number], self.hint, self.sections[4]))
child.Bind(wx.EVT_HELP, self.onHelp)
def onDialogActivate(self, evt):
""" Ensures that the current control will be the same after switching to another window and back """
# store focus when the user switches to another window
if not evt.GetActive():
self.lastFocus = self.FindFocus()
elif self.lastFocus:
self.lastFocus.SetFocus()
def onHelp(self, evt):
helpText = self.helpDict.get(evt.GetEventObject().GetId(), None)
if helpText:
ui.browseableMessage(helpText, self.helpTitle, True)
def onKey(self, evt):
global speakers
key = max(evt.GetUnicodeKey(), evt.GetKeyCode())
if key == 32:
val = int(speakers.GetMasterVolumeLevelScalar()*100+0.5)
self.volumeLevel.SetValue(val)
wx.CallLater(50, ui.message, str(self.volumeLevel.Value))
elif key == 366:
self.volumeLevel.SetValue(self.volumeLevel.Value+10)
elif key == 367:
self.volumeLevel.SetValue(self.volumeLevel.Value-10)
else:
evt.Skip()
def onChoice(self, evt):
val = evt.GetSelection()
if val == 0:
self.volumeLevel.SetRange(0, 100)
self.volumeLevel.Enabled = True
if val == 1:
self.volumeLevel.SetRange(20, 100)
self.volumeLevel.Enabled = True
if val == 2:
self.volumeLevel.Enabled = False
def postInit(self):
self.activateList.SetFocus()
def onOk(self,evt):
config.conf["sharedComputer"]["numLockActivationChoice"] = self.activateList.Selection
# write only to the normal configuration
newSettings = {"sharedComputer": {
"volumeCorrectionChoice": self.volumeList.Selection,
"volumeLevel": self.volumeLevel.Value}}
config.conf._profileCache[None].update(newSettings)
config.conf.profiles[0].update(newSettings)
super(AddonSettingsDialog, self).onOk(evt)
# Audio Stuff
def getVolumeObject():
CLSID_MMDeviceEnumerator = GUID('{BCDE0395-E52F-467C-8E3D-C4579291692E}')
deviceEnumerator = CoCreateInstance(CLSID_MMDeviceEnumerator, IMMDeviceEnumerator, 1)
volObj = cast(
deviceEnumerator.GetDefaultAudioEndpoint(0, 1).Activate(IAudioEndpointVolume._iid_, 7, None),
POINTER(IAudioEndpointVolume))
return volObj
# for a ffull-fletched Audio wrapper
# visit https://github.com/AndreMiras/pycaw
class IAudioEndpointVolume(IUnknown):
_iid_ = GUID('{5CDF2C82-841E-4546-9722-0CF74078229A}')
_methods_ = (
COMMETHOD([], HRESULT, 'NotImpl1'),
COMMETHOD([], HRESULT, 'NotImpl2'),
COMMETHOD([], HRESULT, 'GetChannelCount', (['out'], POINTER(UINT), 'pnChannelCount')),
COMMETHOD([], HRESULT, 'SetMasterVolumeLevel',
(['in'], c_float, 'fLevelDB'), (['in'], POINTER(GUID), 'pguidEventContext')),
COMMETHOD([], HRESULT, 'SetMasterVolumeLevelScalar',
(['in'], c_float, 'fLevel'), (['in'], POINTER(GUID), 'pguidEventContext')),
COMMETHOD([], HRESULT, 'GetMasterVolumeLevel', (['out'], POINTER(c_float), 'pfLevelDB')),
COMMETHOD([], HRESULT, 'GetMasterVolumeLevelScalar', (['out'], POINTER(c_float), 'pfLevelDB')),
COMMETHOD([], HRESULT, 'SetChannelVolumeLevel',
(['in'], UINT, 'nChannel'), (['in'], c_float, 'fLevelDB'), (['in'], POINTER(GUID), 'pguidEventContext')),
COMMETHOD([], HRESULT, 'SetChannelVolumeLevelScalar',
(['in'], DWORD, 'nChannel'), (['in'], c_float, 'fLevelDB'), (['in'], POINTER(GUID), 'pguidEventContext')),
COMMETHOD([], HRESULT, 'GetChannelVolumeLevel',
(['in'], UINT, 'nChannel'),
(['out'], POINTER(c_float), 'pfLevelDB')),
COMMETHOD([], HRESULT, 'GetChannelVolumeLevelScalar',
(['in'], DWORD, 'nChannel'),
(['out'], POINTER(c_float), 'pfLevelDB')),
COMMETHOD([], HRESULT, 'SetMute', (['in'], BOOL, 'bMute'), (['in'], POINTER(GUID), 'pguidEventContext')),
COMMETHOD([], HRESULT, 'GetMute', (['out'], POINTER(BOOL), 'pbMute')),
COMMETHOD([], HRESULT, 'GetVolumeStepInfo',
(['out'], POINTER(DWORD), 'pnStep'),
(['out'], POINTER(DWORD), 'pnStepCount')),
COMMETHOD([], HRESULT, 'VolumeStepUp', (['in'], POINTER(GUID), 'pguidEventContext')),
COMMETHOD([], HRESULT, 'VolumeStepDown', (['in'], POINTER(GUID), 'pguidEventContext')),
COMMETHOD([], HRESULT, 'QueryHardwareSupport', (['out'], POINTER(DWORD), 'pdwHardwareSupportMask')),
COMMETHOD([], HRESULT, 'GetVolumeRange',
(['out'], POINTER(c_float), 'pfMin'),
(['out'], POINTER(c_float), 'pfMax'),
(['out'], POINTER(c_float), 'pfIncr')))
class IMMDevice(IUnknown):
_iid_ = GUID('{D666063F-1587-4E43-81F1-B948E807363F}')
_methods_ = (
COMMETHOD([], HRESULT, 'Activate',
(['in'], POINTER(GUID), 'iid'),
(['in'], DWORD, 'dwClsCtx'),
(['in'], POINTER(DWORD), 'pActivationParams'),
(['out'], POINTER(POINTER(IUnknown)), 'ppInterface')),)
class IMMDeviceCollection(IUnknown):
_iid_ = GUID('{0BD7A1BE-7A1A-44DB-8397-CC5392387B5E}')
_methods_ = (
COMMETHOD([], HRESULT, 'GetCount',
(['out'], POINTER(UINT), 'pcDevices')),
COMMETHOD([], HRESULT, 'Item',
(['in'], UINT, 'nDevice'),
(['out'], POINTER(POINTER(IMMDevice)), 'ppDevice')))
class IMMDeviceEnumerator(IUnknown):
_iid_ = GUID('{A95664D2-9614-4F35-A746-DE8DB63617E6}')
_methods_ = (
COMMETHOD([], HRESULT, 'EnumAudioEndpoints',
(['in'], DWORD, 'dataFlow'),
(['in'], DWORD, 'dwStateMask'),
(['out'], POINTER(POINTER(IMMDeviceCollection)), 'ppDevices')),
COMMETHOD([], HRESULT, 'GetDefaultAudioEndpoint',
(['in'], DWORD, 'dataFlow'),
(['in'], DWORD, 'role'),
(['out'], POINTER(POINTER(IMMDevice)), 'ppDevices')),)