| 1 |
# SPDX-License-Identifier: GPL-2.0-or-later |
| 2 |
|
| 3 |
""" |
| 4 |
Hardware base class |
| 5 |
""" |
| 6 |
import configparser |
| 7 |
import re |
| 8 |
import os |
| 9 |
import types |
| 10 |
import inspect |
| 11 |
import logging |
| 12 |
import time |
| 13 |
import json |
| 14 |
from typing import Optional |
| 15 |
|
| 16 |
from openrazer_daemon.dbus_services.service import DBusService |
| 17 |
import openrazer_daemon.dbus_services.dbus_methods |
| 18 |
from openrazer_daemon.misc import effect_sync |
| 19 |
from openrazer_daemon.misc.battery_notifier import BatteryManager as _BatteryManager |
| 20 |
|
| 21 |
|
| 22 |
# pylint: disable=too-many-instance-attributes |
| 23 |
# pylint: disable=E1102 |
| 24 |
# See https://github.com/PyCQA/pylint/issues/1493 |
| 25 |
class RazerDevice(DBusService): |
| 26 |
""" |
| 27 |
Base class |
| 28 |
|
| 29 |
Sets up the logger, sets up DBus |
| 30 |
""" |
| 31 |
OBJECT_PATH = '/org/razer/device/' |
| 32 |
METHODS: list[str] = [] |
| 33 |
|
| 34 |
EVENT_FILE_REGEX: Optional[re.Pattern] = None |
| 35 |
|
| 36 |
USB_VID: int |
| 37 |
USB_PID: int |
| 38 |
HAS_MATRIX = False |
| 39 |
DEDICATED_MACRO_KEYS = False |
| 40 |
MATRIX_DIMS: Optional[list[int]] = None |
| 41 |
POLL_RATES: Optional[list[int]] = None |
| 42 |
DPI_MAX: Optional[int] = None |
| 43 |
DRIVER_MODE = False |
| 44 |
|
| 45 |
WAVE_DIRS = (1, 2) |
| 46 |
|
| 47 |
ZONES = ('backlight', 'logo', 'scroll', 'left', 'right', 'charging', 'fast_charging', 'fully_charged', 'channel1', 'channel2', 'channel3', 'channel4', 'channel5', 'channel6') |
| 48 |
|
| 49 |
DEVICE_IMAGE: Optional[str] = None |
| 50 |
|
| 51 |
def __init__(self, device_path, device_number, config, persistence, testing, additional_interfaces, additional_methods, unknown_serial_counter): |
| 52 |
|
| 53 |
self.logger = logging.getLogger('razer.device{0}'.format(device_number)) |
| 54 |
self.logger.info("Initialising device.%d %s", device_number, self.__class__.__name__) |
| 55 |
|
| 56 |
# Serial cache |
| 57 |
self._serial = None |
| 58 |
|
| 59 |
# map of vid+pid to counter for serial numbers for unknown devices |
| 60 |
self._unknown_serial_counter: dict[tuple[int, int], int] = unknown_serial_counter |
| 61 |
|
| 62 |
# Local storage key name |
| 63 |
self.storage_name = "UnknownDevice" |
| 64 |
|
| 65 |
self._observer_list = [] |
| 66 |
self._effect_sync_propagate_up = False |
| 67 |
self._disable_notifications = False |
| 68 |
self._disable_persistence = False |
| 69 |
self.additional_interfaces = [] |
| 70 |
if additional_interfaces is not None: |
| 71 |
self.additional_interfaces.extend(additional_interfaces) |
| 72 |
self._battery_manager = None |
| 73 |
|
| 74 |
self.config = config |
| 75 |
self.persistence = persistence |
| 76 |
self._testing = testing |
| 77 |
self._parent = None |
| 78 |
self._device_path = device_path |
| 79 |
self._device_number = device_number |
| 80 |
self.serial = self.get_serial() |
| 81 |
|
| 82 |
if self.USB_PID == 0x0f07: |
| 83 |
self.storage_name = "ChromaMug" |
| 84 |
elif self.USB_PID == 0x0013: |
| 85 |
self.storage_name = "Orochi2011" |
| 86 |
elif self.USB_PID == 0x0016: |
| 87 |
self.storage_name = "DeathAdder35G" |
| 88 |
elif self.USB_PID == 0x0029: |
| 89 |
self.storage_name = "DeathAdder35GBlack" |
| 90 |
elif self.USB_PID == 0x0024 or self.USB_PID == 0x0025: |
| 91 |
self.storage_name = "Mamba2012" |
| 92 |
else: |
| 93 |
self.storage_name = self.serial |
| 94 |
|
| 95 |
self.zone = dict() |
| 96 |
|
| 97 |
for i in self.ZONES: |
| 98 |
self.zone[i] = { |
| 99 |
"present": False, |
| 100 |
"active": True, |
| 101 |
"brightness": 75.0, |
| 102 |
"effect": 'spectrum', |
| 103 |
"colors": [0, 255, 0, 0, 255, 255, 0, 0, 255], |
| 104 |
"speed": 1, |
| 105 |
"wave_dir": 1, |
| 106 |
} |
| 107 |
|
| 108 |
# Check for a DPI X only device since they need a Y value of 0 |
| 109 |
if 'available_dpi' in self.METHODS: |
| 110 |
self.dpi = [1800, 0] |
| 111 |
else: |
| 112 |
self.dpi = [1800, 1800] |
| 113 |
|
| 114 |
self.poll_rate = 500 |
| 115 |
if 'set_poll_rate' in self.METHODS and not self.POLL_RATES: |
| 116 |
self.POLL_RATES = [125, 500, 1000] |
| 117 |
|
| 118 |
self._effect_sync = effect_sync.EffectSync(self, device_number) |
| 119 |
|
| 120 |
self._is_closed = False |
| 121 |
|
| 122 |
# device methods available in all devices |
| 123 |
self.methods_internal = ['get_firmware', 'get_matrix_dims', 'has_matrix', 'get_device_name'] |
| 124 |
self.methods_internal.extend(additional_methods) |
| 125 |
|
| 126 |
# Find event files in /dev/input/by-id/ by matching against regex |
| 127 |
self.event_files = [] |
| 128 |
|
| 129 |
if self._testing: |
| 130 |
search_dir = os.path.join(device_path, 'input') |
| 131 |
else: |
| 132 |
search_dir = '/dev/input/by-id/' |
| 133 |
|
| 134 |
if os.path.exists(search_dir): |
| 135 |
for event_file in os.listdir(search_dir): |
| 136 |
if self.EVENT_FILE_REGEX is not None and self.EVENT_FILE_REGEX.match(event_file) is not None: |
| 137 |
self.event_files.append(os.path.join(search_dir, event_file)) |
| 138 |
|
| 139 |
object_path = os.path.join(self.OBJECT_PATH, self.serial) |
| 140 |
super().__init__(object_path) |
| 141 |
|
| 142 |
# Set up methods to suspend and restore device operation |
| 143 |
self.suspend_args = {} |
| 144 |
self.method_args = {} |
| 145 |
|
| 146 |
methods = { |
| 147 |
# interface, method, callback, in-args, out-args |
| 148 |
('razer.device.misc', 'getSerial', self.get_serial, None, 's'), |
| 149 |
('razer.device.misc', 'suspendDevice', self.suspend_device, None, None), |
| 150 |
('razer.device.misc', 'getDeviceMode', self.get_device_mode, None, 's'), |
| 151 |
('razer.device.misc', 'getDeviceImage', self.get_device_image, None, 's'), |
| 152 |
('razer.device.misc', 'setDeviceMode', self.set_device_mode, 'yy', None), |
| 153 |
('razer.device.misc', 'resumeDevice', self.resume_device, None, None), |
| 154 |
('razer.device.misc', 'getVidPid', self.get_vid_pid, None, 'ai'), |
| 155 |
('razer.device.misc', 'getDriverVersion', openrazer_daemon.dbus_services.dbus_methods.version, None, 's'), |
| 156 |
('razer.device.misc', 'hasDedicatedMacroKeys', self.dedicated_macro_keys, None, 'b'), |
| 157 |
# Deprecated API, but kept for backwards compatibility |
| 158 |
('razer.device.misc', 'getRazerUrls', self.get_image_json, None, 's'), |
| 159 |
|
| 160 |
('razer.device.lighting.chroma', 'restoreLastEffect', self.restore_effect, None, None), |
| 161 |
} |
| 162 |
|
| 163 |
effect_methods = { |
| 164 |
"backlight_chroma": { |
| 165 |
('razer.device.lighting.chroma', 'getEffect', self.get_current_effect, None, 's'), |
| 166 |
('razer.device.lighting.chroma', 'getEffectColors', self.get_current_effect_colors, None, 'ay'), |
| 167 |
('razer.device.lighting.chroma', 'getEffectSpeed', self.get_current_effect_speed, None, 'i'), |
| 168 |
('razer.device.lighting.chroma', 'getWaveDir', self.get_current_wave_dir, None, 'i'), |
| 169 |
}, |
| 170 |
|
| 171 |
"backlight": { |
| 172 |
# Intentionally using the same underlying methods as backlight_chroma. |
| 173 |
# Both refer to the 'backlight' LED internally but partially exist separately due to historical reasons. |
| 174 |
('razer.device.lighting.backlight', 'getBacklightEffect', self.get_current_effect, None, 's'), |
| 175 |
('razer.device.lighting.backlight', 'getBacklightEffectColors', self.get_current_effect_colors, None, 'ay'), |
| 176 |
('razer.device.lighting.backlight', 'getBacklightEffectSpeed', self.get_current_effect_speed, None, 'i'), |
| 177 |
('razer.device.lighting.backlight', 'getBacklightWaveDir', self.get_current_wave_dir, None, 'i'), |
| 178 |
}, |
| 179 |
|
| 180 |
"logo": { |
| 181 |
('razer.device.lighting.logo', 'getLogoEffect', self.get_current_logo_effect, None, 's'), |
| 182 |
('razer.device.lighting.logo', 'getLogoEffectColors', self.get_current_logo_effect_colors, None, 'ay'), |
| 183 |
('razer.device.lighting.logo', 'getLogoEffectSpeed', self.get_current_logo_effect_speed, None, 'i'), |
| 184 |
('razer.device.lighting.logo', 'getLogoWaveDir', self.get_current_logo_wave_dir, None, 'i'), |
| 185 |
}, |
| 186 |
|
| 187 |
"scroll": { |
| 188 |
('razer.device.lighting.scroll', 'getScrollEffect', self.get_current_scroll_effect, None, 's'), |
| 189 |
('razer.device.lighting.scroll', 'getScrollEffectColors', self.get_current_scroll_effect_colors, None, 'ay'), |
| 190 |
('razer.device.lighting.scroll', 'getScrollEffectSpeed', self.get_current_scroll_effect_speed, None, 'i'), |
| 191 |
('razer.device.lighting.scroll', 'getScrollWaveDir', self.get_current_scroll_wave_dir, None, 'i'), |
| 192 |
}, |
| 193 |
|
| 194 |
"left": { |
| 195 |
('razer.device.lighting.left', 'getLeftEffect', self.get_current_left_effect, None, 's'), |
| 196 |
('razer.device.lighting.left', 'getLeftEffectColors', self.get_current_left_effect_colors, None, 'ay'), |
| 197 |
('razer.device.lighting.left', 'getLeftEffectSpeed', self.get_current_left_effect_speed, None, 'i'), |
| 198 |
('razer.device.lighting.left', 'getLeftWaveDir', self.get_current_left_wave_dir, None, 'i'), |
| 199 |
}, |
| 200 |
|
| 201 |
"right": { |
| 202 |
('razer.device.lighting.right', 'getRightEffect', self.get_current_right_effect, None, 's'), |
| 203 |
('razer.device.lighting.right', 'getRightEffectColors', self.get_current_right_effect_colors, None, 'ay'), |
| 204 |
('razer.device.lighting.right', 'getRightEffectSpeed', self.get_current_right_effect_speed, None, 'i'), |
| 205 |
('razer.device.lighting.right', 'getRightWaveDir', self.get_current_right_wave_dir, None, 'i'), |
| 206 |
}, |
| 207 |
|
| 208 |
"charging": { |
| 209 |
('razer.device.lighting.charging', 'getChargingEffect', self.get_current_charging_effect, None, 's'), |
| 210 |
('razer.device.lighting.charging', 'getChargingEffectColors', self.get_current_charging_effect_colors, None, 'ay'), |
| 211 |
('razer.device.lighting.charging', 'getChargingEffectSpeed', self.get_current_charging_effect_speed, None, 'i'), |
| 212 |
('razer.device.lighting.charging', 'getChargingWaveDir', self.get_current_charging_wave_dir, None, 'i'), |
| 213 |
}, |
| 214 |
|
| 215 |
"fast_charging": { |
| 216 |
('razer.device.lighting.fast_charging', 'getFastChargingEffect', self.get_current_fast_charging_effect, None, 's'), |
| 217 |
('razer.device.lighting.fast_charging', 'getFastChargingEffectColors', self.get_current_fast_charging_effect_colors, None, 'ay'), |
| 218 |
('razer.device.lighting.fast_charging', 'getFastChargingEffectSpeed', self.get_current_fast_charging_effect_speed, None, 'i'), |
| 219 |
('razer.device.lighting.fast_charging', 'getFastChargingWaveDir', self.get_current_fast_charging_wave_dir, None, 'i'), |
| 220 |
}, |
| 221 |
|
| 222 |
"fully_charged": { |
| 223 |
('razer.device.lighting.fully_charged', 'getFullyChargedEffect', self.get_current_fully_charged_effect, None, 's'), |
| 224 |
('razer.device.lighting.fully_charged', 'getFullyChargedEffectColors', self.get_current_fully_charged_effect_colors, None, 'ay'), |
| 225 |
('razer.device.lighting.fully_charged', 'getFullyChargedEffectSpeed', self.get_current_fully_charged_effect_speed, None, 'i'), |
| 226 |
('razer.device.lighting.fully_charged', 'getFullyChargedWaveDir', self.get_current_fully_charged_wave_dir, None, 'i'), |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
for m in methods: |
| 231 |
self.logger.debug("Adding {}.{} method to DBus".format(m[0], m[1])) |
| 232 |
self.add_dbus_method(m[0], m[1], m[2], in_signature=m[3], out_signature=m[4]) |
| 233 |
|
| 234 |
# this check is separate from the rest because backlight effects don't have prefixes in their names |
| 235 |
if 'set_static_effect' in self.METHODS or 'bw_set_static' in self.METHODS: |
| 236 |
self.zone["backlight"]["present"] = True |
| 237 |
for m in effect_methods["backlight_chroma"]: |
| 238 |
self.logger.debug("Adding {}.{} method to DBus".format(m[0], m[1])) |
| 239 |
self.add_dbus_method(m[0], m[1], m[2], in_signature=m[3], out_signature=m[4]) |
| 240 |
|
| 241 |
for i in self.ZONES: |
| 242 |
if 'set_' + i + '_static_classic' in self.METHODS \ |
| 243 |
or 'set_' + i + '_static' in self.METHODS \ |
| 244 |
or 'set_' + i + '_active' in self.METHODS \ |
| 245 |
or 'set_' + i + '_on' in self.METHODS: |
| 246 |
self.zone[i]["present"] = True |
| 247 |
for m in effect_methods[i]: |
| 248 |
self.logger.debug("Adding {}.{} method to DBus".format(m[0], m[1])) |
| 249 |
self.add_dbus_method(m[0], m[1], m[2], in_signature=m[3], out_signature=m[4]) |
| 250 |
|
| 251 |
# Load additional DBus methods |
| 252 |
self.load_methods() |
| 253 |
|
| 254 |
# load last DPI/poll rate state |
| 255 |
if self.persistence.has_section(self.storage_name): |
| 256 |
if 'set_dpi_xy' in self.METHODS or 'set_dpi_xy_byte' in self.METHODS: |
| 257 |
try: |
| 258 |
self.dpi[0] = int(self.persistence[self.storage_name]['dpi_x']) |
| 259 |
self.dpi[1] = int(self.persistence[self.storage_name]['dpi_y']) |
| 260 |
except (KeyError, configparser.NoOptionError): |
| 261 |
self.logger.info("Failed to get DPI from persistence storage, using default.") |
| 262 |
|
| 263 |
if 'set_poll_rate' in self.METHODS: |
| 264 |
try: |
| 265 |
self.poll_rate = int(self.persistence[self.storage_name]['poll_rate']) |
| 266 |
except (KeyError, configparser.NoOptionError): |
| 267 |
self.logger.info("Failed to get poll rate from persistence storage, using default.") |
| 268 |
|
| 269 |
# load last effects |
| 270 |
for i in self.ZONES: |
| 271 |
if self.zone[i]["present"]: |
| 272 |
# check if we have the device in the persistence file |
| 273 |
if self.persistence.has_section(self.storage_name): |
| 274 |
# try reading the effect name from the persistence |
| 275 |
try: |
| 276 |
self.zone[i]["effect"] = self.persistence[self.storage_name][i + '_effect'] |
| 277 |
except (KeyError, configparser.NoOptionError): |
| 278 |
self.logger.info("Failed to get " + i + " effect from persistence storage, using default.") |
| 279 |
|
| 280 |
# zone active status |
| 281 |
try: |
| 282 |
self.zone[i]["active"] = self.persistence.getboolean(self.storage_name, i + '_active') |
| 283 |
except (KeyError, configparser.NoOptionError): |
| 284 |
self.logger.info("Failed to get " + i + " active from persistence storage, using default.") |
| 285 |
|
| 286 |
# brightness |
| 287 |
try: |
| 288 |
self.zone[i]["brightness"] = float(self.persistence[self.storage_name][i + '_brightness']) |
| 289 |
except (KeyError, configparser.NoOptionError): |
| 290 |
self.logger.info("Failed to get " + i + " brightness from persistence storage, using default.") |
| 291 |
|
| 292 |
# colors. |
| 293 |
# these are stored as a string that must contain 9 numbers, separated with spaces. |
| 294 |
try: |
| 295 |
for index, item in enumerate(self.persistence[self.storage_name][i + '_colors'].split(" ")): |
| 296 |
self.zone[i]["colors"][index] = int(item) |
| 297 |
# check if the color is in range |
| 298 |
if not 0 <= self.zone[i]["colors"][index] <= 255: |
| 299 |
raise ValueError('Color out of range') |
| 300 |
|
| 301 |
# check if we have exactly 9 colors |
| 302 |
if len(self.zone[i]["colors"]) != 9: |
| 303 |
raise ValueError('There must be exactly 9 colors') |
| 304 |
except ValueError: |
| 305 |
# invalid colors. reinitialize |
| 306 |
self.zone[i]["colors"] = [0, 255, 0, 0, 255, 255, 0, 0, 255] |
| 307 |
self.logger.info("%s: Invalid colors; restoring to defaults.", self.__class__.__name__) |
| 308 |
except (KeyError, configparser.NoOptionError): |
| 309 |
self.logger.info("Failed to get " + i + " colors from persistence storage, using default.") |
| 310 |
|
| 311 |
# speed |
| 312 |
try: |
| 313 |
self.zone[i]["speed"] = int(self.persistence[self.storage_name][i + '_speed']) |
| 314 |
except (KeyError, configparser.NoOptionError): |
| 315 |
self.logger.info("Failed to get " + i + " speed from persistence storage, using default.") |
| 316 |
|
| 317 |
# wave direction |
| 318 |
try: |
| 319 |
self.zone[i]["wave_dir"] = int(self.persistence[self.storage_name][i + '_wave_dir']) |
| 320 |
except (KeyError, configparser.NoOptionError): |
| 321 |
self.logger.info("Failed to get " + i + " wave direction from persistence storage, using default.") |
| 322 |
|
| 323 |
# Initialize battery manager if the device has support |
| 324 |
if 'get_battery' in self.METHODS: |
| 325 |
self._init_battery_manager() |
| 326 |
|
| 327 |
try: |
| 328 |
driver_mode_default = self.DRIVER_MODE |
| 329 |
self.DRIVER_MODE = self.config.getboolean(f"Device:{self.serial}", "driver_mode") |
| 330 |
self.logger.info('Overriding DRIVER_MODE with "%s" from config (default: "%s")', self.DRIVER_MODE, driver_mode_default) |
| 331 |
except (configparser.NoSectionError, configparser.NoOptionError): |
| 332 |
pass |
| 333 |
|
| 334 |
if self.DRIVER_MODE: |
| 335 |
self.logger.info('Setting device to "driver" mode. Daemon will handle special functionality') |
| 336 |
self.set_device_mode(0x03, 0x00) # Driver mode |
| 337 |
|
| 338 |
self.restore_dpi_poll_rate() |
| 339 |
self.restore_brightness() |
| 340 |
|
| 341 |
if self.config.getboolean('Startup', "restore_persistence") is True: |
| 342 |
self.restore_effect() |
| 343 |
|
| 344 |
# Some devices need setting a second time after encountering Razer Synapse on Windows |
| 345 |
if self.config.getboolean('Startup', "persistence_dual_boot_quirk") is True: |
| 346 |
self.logger.debug("Restoring effect persistence again (dual boot quirk)") |
| 347 |
self.restore_effect() |
| 348 |
|
| 349 |
def send_effect_event(self, effect_name, *args): |
| 350 |
""" |
| 351 |
Send effect event |
| 352 |
|
| 353 |
:param effect_name: Effect name |
| 354 |
:type effect_name: str |
| 355 |
|
| 356 |
:param args: Effect arguments |
| 357 |
:type args: list |
| 358 |
""" |
| 359 |
payload = ['effect', self, effect_name] |
| 360 |
payload.extend(args) |
| 361 |
|
| 362 |
self.notify_observers(tuple(payload)) |
| 363 |
|
| 364 |
def dedicated_macro_keys(self): |
| 365 |
""" |
| 366 |
Returns if the device has dedicated macro keys |
| 367 |
|
| 368 |
:return: Macro keys |
| 369 |
:rtype: bool |
| 370 |
""" |
| 371 |
return self.DEDICATED_MACRO_KEYS |
| 372 |
|
| 373 |
def restore_dpi_poll_rate(self): |
| 374 |
""" |
| 375 |
Set the device DPI & poll rate to the saved value |
| 376 |
""" |
| 377 |
dpi_func = getattr(self, "setDPI", None) |
| 378 |
if dpi_func is not None: |
| 379 |
# Constrain value in case the max has changed, e.g. wired/wireless might different maximums |
| 380 |
if self.dpi[0] > self.DPI_MAX: |
| 381 |
self.logger.warning("Constraining DPI X to maximum of " + str(self.DPI_MAX) + " because stored value " + str(self.dpi[0]) + " is larger.") |
| 382 |
self.dpi[0] = self.DPI_MAX |
| 383 |
if self.dpi[1] > self.DPI_MAX: |
| 384 |
self.logger.warning("Constraining DPI Y to maximum of " + str(self.DPI_MAX) + " because stored value " + str(self.dpi[1]) + " is larger.") |
| 385 |
self.dpi[1] = self.DPI_MAX |
| 386 |
|
| 387 |
dpi_func(self.dpi[0], self.dpi[1]) |
| 388 |
|
| 389 |
poll_rate_func = getattr(self, "setPollRate", None) |
| 390 |
if poll_rate_func is not None: |
| 391 |
# Constrain value in case the available values have changed, e.g. wired/wireless might different values available |
| 392 |
if self.poll_rate not in self.POLL_RATES: |
| 393 |
self.logger.warning("Constraining poll rate because stored value " + str(self.poll_rate) + " is not available.") |
| 394 |
self.poll_rate = min(self.POLL_RATES, key=lambda x: abs(x - self.poll_rate)) |
| 395 |
|
| 396 |
poll_rate_func(self.poll_rate) |
| 397 |
|
| 398 |
def restore_brightness(self): |
| 399 |
""" |
| 400 |
Set the device to the current brightness/active state. |
| 401 |
|
| 402 |
This is used at launch time. |
| 403 |
""" |
| 404 |
for i in self.ZONES: |
| 405 |
if self.zone[i]["present"]: |
| 406 |
# load active state |
| 407 |
if 'set_' + i + '_active' in self.METHODS: |
| 408 |
active_func = getattr(self, "set" + self.capitalize_first_char(i) + "Active", None) |
| 409 |
if active_func is not None: |
| 410 |
active_func(self.zone[i]["active"]) |
| 411 |
|
| 412 |
# load brightness level |
| 413 |
bright_func = None |
| 414 |
if i == "backlight": |
| 415 |
bright_func = getattr(self, "setBrightness", None) |
| 416 |
elif 'set_' + i + '_brightness' in self.METHODS: |
| 417 |
bright_func = getattr(self, "set" + self.capitalize_first_char(i) + "Brightness", None) |
| 418 |
|
| 419 |
if bright_func is not None: |
| 420 |
bright_func(self.zone[i]["brightness"]) |
| 421 |
|
| 422 |
def disable_brightness(self): |
| 423 |
""" |
| 424 |
Set brightness to 0 and/or active state to false. |
| 425 |
""" |
| 426 |
for i in self.ZONES: |
| 427 |
if self.zone[i]["present"]: |
| 428 |
# set active state |
| 429 |
if 'set_' + i + '_active' in self.METHODS: |
| 430 |
active_func = getattr(self, "set" + self.capitalize_first_char(i) + "Active", None) |
| 431 |
if active_func is not None: |
| 432 |
active_func(False) |
| 433 |
|
| 434 |
# set brightness level |
| 435 |
bright_func = None |
| 436 |
if i == "backlight": |
| 437 |
bright_func = getattr(self, "setBrightness", None) |
| 438 |
elif 'set_' + i + '_brightness' in self.METHODS: |
| 439 |
bright_func = getattr(self, "set" + self.capitalize_first_char(i) + "Brightness", None) |
| 440 |
|
| 441 |
if bright_func is not None: |
| 442 |
bright_func(0) |
| 443 |
|
| 444 |
def restore_effect(self): |
| 445 |
""" |
| 446 |
Set the device to the current effect |
| 447 |
|
| 448 |
This is used at launch time and can be called by applications |
| 449 |
that use custom matrix frames after they exit |
| 450 |
""" |
| 451 |
for i in self.ZONES: |
| 452 |
if self.zone[i]["present"]: |
| 453 |
# prepare the effect method name |
| 454 |
# yes, we need to handle the backlight zone separately too. |
| 455 |
# the backlight effect methods don't have a prefix. |
| 456 |
if i == "backlight": |
| 457 |
effect_func_name = 'set' + self.capitalize_first_char(self.zone[i]["effect"]) |
| 458 |
else: |
| 459 |
effect_func_name = 'set' + self.handle_underscores(self.capitalize_first_char(i)) + self.capitalize_first_char(self.zone[i]["effect"]) |
| 460 |
|
| 461 |
# find the effect method |
| 462 |
effect_func = getattr(self, effect_func_name, None) |
| 463 |
|
| 464 |
# check if the effect method exists only if we didn't look for spectrum (because resetting to Spectrum when the effect is Spectrum is in vain) |
| 465 |
if effect_func == None and not self.zone[i]["effect"] == "spectrum": |
| 466 |
# not found. restoring to Spectrum |
| 467 |
self.logger.info("%s: Invalid effect name %s; restoring to Spectrum.", self.__class__.__name__, effect_func_name) |
| 468 |
self.zone[i]["effect"] = 'spectrum' |
| 469 |
if i == "backlight": |
| 470 |
effect_func_name = 'setSpectrum' |
| 471 |
else: |
| 472 |
effect_func_name = 'set' + self.capitalize_first_char(i) + 'Spectrum' |
| 473 |
effect_func = getattr(self, effect_func_name, None) |
| 474 |
|
| 475 |
# we check again here because there is a possibility the device may not even have Spectrum |
| 476 |
if effect_func is not None: |
| 477 |
effect = self.zone[i]["effect"] |
| 478 |
colors = self.zone[i]["colors"] |
| 479 |
speed = self.zone[i]["speed"] |
| 480 |
wave_dir = self.zone[i]["wave_dir"] |
| 481 |
if self.get_num_arguments(effect_func) == 0: |
| 482 |
effect_func() |
| 483 |
elif self.get_num_arguments(effect_func) == 1: |
| 484 |
# there are 2 effects which require 1 argument. |
| 485 |
# these are: Starlight (Random) and Wave. |
| 486 |
if effect == 'starlightRandom': |
| 487 |
effect_func(speed) |
| 488 |
elif effect == 'wave': |
| 489 |
effect_func(wave_dir) |
| 490 |
elif effect == 'wheel': |
| 491 |
effect_func(wave_dir) |
| 492 |
elif effect == 'rippleRandomColour': |
| 493 |
# do nothing. this is handled in the ripple manager. |
| 494 |
pass |
| 495 |
else: |
| 496 |
self.logger.error("%s: Effect requires 1 argument but don't know how to handle it!", self.__class__.__name__) |
| 497 |
elif self.get_num_arguments(effect_func) == 3: |
| 498 |
effect_func(colors[0], colors[1], colors[2]) |
| 499 |
elif self.get_num_arguments(effect_func) == 4: |
| 500 |
# starlight/reactive have different arguments. |
| 501 |
if effect == 'starlightSingle' or effect == 'reactive': |
| 502 |
effect_func(colors[0], colors[1], colors[2], speed) |
| 503 |
elif effect == 'ripple': |
| 504 |
# do nothing. this is handled in the ripple manager. |
| 505 |
pass |
| 506 |
else: |
| 507 |
self.logger.error("%s: Effect requires 4 arguments but don't know how to handle it!", self.__class__.__name__) |
| 508 |
elif self.get_num_arguments(effect_func) == 6: |
| 509 |
effect_func(colors[0], colors[1], colors[2], colors[3], colors[4], colors[5]) |
| 510 |
elif self.get_num_arguments(effect_func) == 7: |
| 511 |
effect_func(colors[0], colors[1], colors[2], colors[3], colors[4], colors[5], speed) |
| 512 |
elif self.get_num_arguments(effect_func) == 9: |
| 513 |
effect_func(colors[0], colors[1], colors[2], colors[3], colors[4], colors[5], colors[6], colors[7], colors[8]) |
| 514 |
else: |
| 515 |
self.logger.error("%s: Couldn't detect effect argument count!", self.__class__.__name__) |
| 516 |
|
| 517 |
def set_persistence(self, zone, key, value): |
| 518 |
""" |
| 519 |
Set a device's current state for persisting across sessions. |
| 520 |
|
| 521 |
:param zone: Zone |
| 522 |
:type zone: string |
| 523 |
|
| 524 |
:param key: Key |
| 525 |
:type key: string |
| 526 |
|
| 527 |
:param value: Value |
| 528 |
:type value: string |
| 529 |
""" |
| 530 |
if self._disable_persistence: |
| 531 |
return |
| 532 |
self.logger.debug("Set persistence (%s, %s, %s)", zone, key, value) |
| 533 |
|
| 534 |
self.persistence.status["changed"] = True |
| 535 |
|
| 536 |
if zone: |
| 537 |
self.zone[zone][key] = value |
| 538 |
else: |
| 539 |
self.zone[key] = value |
| 540 |
|
| 541 |
def get_current_effect(self): |
| 542 |
""" |
| 543 |
Get the device's current effect |
| 544 |
|
| 545 |
:return: Effect |
| 546 |
:rtype: string |
| 547 |
""" |
| 548 |
self.logger.debug("DBus call get_current_effect") |
| 549 |
|
| 550 |
return self.zone["backlight"]["effect"] |
| 551 |
|
| 552 |
def get_current_effect_colors(self): |
| 553 |
""" |
| 554 |
Get the device's current effect's colors |
| 555 |
|
| 556 |
:return: 3 colors |
| 557 |
:rtype: list of byte |
| 558 |
""" |
| 559 |
self.logger.debug("DBus call get_current_effect_colors") |
| 560 |
|
| 561 |
return self.zone["backlight"]["colors"] |
| 562 |
|
| 563 |
def get_current_effect_speed(self): |
| 564 |
""" |
| 565 |
Get the device's current effect's speed |
| 566 |
|
| 567 |
:return: Speed |
| 568 |
:rtype: int |
| 569 |
""" |
| 570 |
self.logger.debug("DBus call get_current_effect_speed") |
| 571 |
|
| 572 |
return self.zone["backlight"]["speed"] |
| 573 |
|
| 574 |
def get_current_wave_dir(self): |
| 575 |
""" |
| 576 |
Get the device's current wave direction |
| 577 |
|
| 578 |
:return: Direction |
| 579 |
:rtype: int |
| 580 |
""" |
| 581 |
self.logger.debug("DBus call get_current_wave_dir") |
| 582 |
|
| 583 |
return self.zone["backlight"]["wave_dir"] |
| 584 |
|
| 585 |
def get_current_logo_effect(self): |
| 586 |
""" |
| 587 |
Get the device's current logo effect |
| 588 |
|
| 589 |
:return: Effect |
| 590 |
:rtype: string |
| 591 |
""" |
| 592 |
self.logger.debug("DBus call get_current_logo_effect") |
| 593 |
|
| 594 |
return self.zone["logo"]["effect"] |
| 595 |
|
| 596 |
def get_current_logo_effect_colors(self): |
| 597 |
""" |
| 598 |
Get the device's current logo effect's colors |
| 599 |
|
| 600 |
:return: 3 colors |
| 601 |
:rtype: list of byte |
| 602 |
""" |
| 603 |
self.logger.debug("DBus call get_current_logo_effect_colors") |
| 604 |
|
| 605 |
return self.zone["logo"]["colors"] |
| 606 |
|
| 607 |
def get_current_logo_effect_speed(self): |
| 608 |
""" |
| 609 |
Get the device's current logo effect's speed |
| 610 |
|
| 611 |
:return: Speed |
| 612 |
:rtype: int |
| 613 |
""" |
| 614 |
self.logger.debug("DBus call get_current_logo_effect_speed") |
| 615 |
|
| 616 |
return self.zone["logo"]["speed"] |
| 617 |
|
| 618 |
def get_current_logo_wave_dir(self): |
| 619 |
""" |
| 620 |
Get the device's current logo wave direction |
| 621 |
|
| 622 |
:return: Direction |
| 623 |
:rtype: int |
| 624 |
""" |
| 625 |
self.logger.debug("DBus call get_current_logo_wave_dir") |
| 626 |
|
| 627 |
return self.zone["logo"]["wave_dir"] |
| 628 |
|
| 629 |
def get_current_scroll_effect(self): |
| 630 |
""" |
| 631 |
Get the device's current scroll effect |
| 632 |
|
| 633 |
:return: Effect |
| 634 |
:rtype: string |
| 635 |
""" |
| 636 |
self.logger.debug("DBus call get_current_scroll_effect") |
| 637 |
|
| 638 |
return self.zone["scroll"]["effect"] |
| 639 |
|
| 640 |
def get_current_scroll_effect_colors(self): |
| 641 |
""" |
| 642 |
Get the device's current scroll effect's colors |
| 643 |
|
| 644 |
:return: 3 colors |
| 645 |
:rtype: list of byte |
| 646 |
""" |
| 647 |
self.logger.debug("DBus call get_current_scroll_effect_colors") |
| 648 |
|
| 649 |
return self.zone["scroll"]["colors"] |
| 650 |
|
| 651 |
def get_current_scroll_effect_speed(self): |
| 652 |
""" |
| 653 |
Get the device's current scroll effect's speed |
| 654 |
|
| 655 |
:return: Speed |
| 656 |
:rtype: int |
| 657 |
""" |
| 658 |
self.logger.debug("DBus call get_current_scroll_effect_speed") |
| 659 |
|
| 660 |
return self.zone["scroll"]["speed"] |
| 661 |
|
| 662 |
def get_current_scroll_wave_dir(self): |
| 663 |
""" |
| 664 |
Get the device's current scroll wave direction |
| 665 |
|
| 666 |
:return: Direction |
| 667 |
:rtype: int |
| 668 |
""" |
| 669 |
self.logger.debug("DBus call get_current_scroll_wave_dir") |
| 670 |
|
| 671 |
return self.zone["scroll"]["wave_dir"] |
| 672 |
|
| 673 |
def get_current_left_effect(self): |
| 674 |
""" |
| 675 |
Get the device's current left effect |
| 676 |
|
| 677 |
:return: Effect |
| 678 |
:rtype: string |
| 679 |
""" |
| 680 |
self.logger.debug("DBus call get_current_left_effect") |
| 681 |
|
| 682 |
return self.zone["left"]["effect"] |
| 683 |
|
| 684 |
def get_current_left_effect_colors(self): |
| 685 |
""" |
| 686 |
Get the device's current left effect's colors |
| 687 |
|
| 688 |
:return: 3 colors |
| 689 |
:rtype: list of byte |
| 690 |
""" |
| 691 |
self.logger.debug("DBus call get_current_left_effect_colors") |
| 692 |
|
| 693 |
return self.zone["left"]["colors"] |
| 694 |
|
| 695 |
def get_current_left_effect_speed(self): |
| 696 |
""" |
| 697 |
Get the device's current left effect's speed |
| 698 |
|
| 699 |
:return: Speed |
| 700 |
:rtype: int |
| 701 |
""" |
| 702 |
self.logger.debug("DBus call get_current_left_effect_speed") |
| 703 |
|
| 704 |
return self.zone["left"]["speed"] |
| 705 |
|
| 706 |
def get_current_left_wave_dir(self): |
| 707 |
""" |
| 708 |
Get the device's current left wave direction |
| 709 |
|
| 710 |
:return: Direction |
| 711 |
:rtype: int |
| 712 |
""" |
| 713 |
self.logger.debug("DBus call get_current_left_wave_dir") |
| 714 |
|
| 715 |
return self.zone["left"]["wave_dir"] |
| 716 |
|
| 717 |
def get_current_right_effect(self): |
| 718 |
""" |
| 719 |
Get the device's current right effect |
| 720 |
|
| 721 |
:return: Effect |
| 722 |
:rtype: string |
| 723 |
""" |
| 724 |
self.logger.debug("DBus call get_current_right_effect") |
| 725 |
|
| 726 |
return self.zone["right"]["effect"] |
| 727 |
|
| 728 |
def get_current_right_effect_colors(self): |
| 729 |
""" |
| 730 |
Get the device's current right effect's colors |
| 731 |
|
| 732 |
:return: 3 colors |
| 733 |
:rtype: list of byte |
| 734 |
""" |
| 735 |
self.logger.debug("DBus call get_current_right_effect_colors") |
| 736 |
|
| 737 |
return self.zone["right"]["colors"] |
| 738 |
|
| 739 |
def get_current_right_effect_speed(self): |
| 740 |
""" |
| 741 |
Get the device's current right effect's speed |
| 742 |
|
| 743 |
:return: Speed |
| 744 |
:rtype: int |
| 745 |
""" |
| 746 |
self.logger.debug("DBus call get_current_right_effect_speed") |
| 747 |
|
| 748 |
return self.zone["right"]["speed"] |
| 749 |
|
| 750 |
def get_current_right_wave_dir(self): |
| 751 |
""" |
| 752 |
Get the device's current right wave direction |
| 753 |
|
| 754 |
:return: Direction |
| 755 |
:rtype: int |
| 756 |
""" |
| 757 |
self.logger.debug("DBus call get_current_right_wave_dir") |
| 758 |
|
| 759 |
return self.zone["right"]["wave_dir"] |
| 760 |
|
| 761 |
def get_current_charging_effect(self): |
| 762 |
""" |
| 763 |
Get the device's current charging effect |
| 764 |
|
| 765 |
:return: Effect |
| 766 |
:rtype: string |
| 767 |
""" |
| 768 |
self.logger.debug("DBus call get_current_charging_effect") |
| 769 |
|
| 770 |
return self.zone["charging"]["effect"] |
| 771 |
|
| 772 |
def get_current_charging_effect_colors(self): |
| 773 |
""" |
| 774 |
Get the device's current charging effect's colors |
| 775 |
|
| 776 |
:return: 3 colors |
| 777 |
:rtype: list of byte |
| 778 |
""" |
| 779 |
self.logger.debug("DBus call get_current_charging_effect_colors") |
| 780 |
|
| 781 |
return self.zone["charging"]["colors"] |
| 782 |
|
| 783 |
def get_current_charging_effect_speed(self): |
| 784 |
""" |
| 785 |
Get the device's current charging effect's speed |
| 786 |
|
| 787 |
:return: Speed |
| 788 |
:rtype: int |
| 789 |
""" |
| 790 |
self.logger.debug("DBus call get_current_charging_effect_speed") |
| 791 |
|
| 792 |
return self.zone["charging"]["speed"] |
| 793 |
|
| 794 |
def get_current_charging_wave_dir(self): |
| 795 |
""" |
| 796 |
Get the device's current charging wave direction |
| 797 |
|
| 798 |
:return: Direction |
| 799 |
:rtype: int |
| 800 |
""" |
| 801 |
self.logger.debug("DBus call get_current_charging_wave_dir") |
| 802 |
|
| 803 |
return self.zone["charging"]["wave_dir"] |
| 804 |
|
| 805 |
def get_current_fast_charging_effect(self): |
| 806 |
""" |
| 807 |
Get the device's current fast_charging effect |
| 808 |
|
| 809 |
:return: Effect |
| 810 |
:rtype: string |
| 811 |
""" |
| 812 |
self.logger.debug("DBus call get_current_fast_charging_effect") |
| 813 |
|
| 814 |
return self.zone["fast_charging"]["effect"] |
| 815 |
|
| 816 |
def get_current_fast_charging_effect_colors(self): |
| 817 |
""" |
| 818 |
Get the device's current fast_charging effect's colors |
| 819 |
|
| 820 |
:return: 3 colors |
| 821 |
:rtype: list of byte |
| 822 |
""" |
| 823 |
self.logger.debug("DBus call get_current_fast_charging_effect_colors") |
| 824 |
|
| 825 |
return self.zone["fast_charging"]["colors"] |
| 826 |
|
| 827 |
def get_current_fast_charging_effect_speed(self): |
| 828 |
""" |
| 829 |
Get the device's current fast_charging effect's speed |
| 830 |
|
| 831 |
:return: Speed |
| 832 |
:rtype: int |
| 833 |
""" |
| 834 |
self.logger.debug("DBus call get_current_fast_charging_effect_speed") |
| 835 |
|
| 836 |
return self.zone["fast_charging"]["speed"] |
| 837 |
|
| 838 |
def get_current_fast_charging_wave_dir(self): |
| 839 |
""" |
| 840 |
Get the device's current fast_charging wave direction |
| 841 |
|
| 842 |
:return: Direction |
| 843 |
:rtype: int |
| 844 |
""" |
| 845 |
self.logger.debug("DBus call get_current_fast_charging_wave_dir") |
| 846 |
|
| 847 |
return self.zone["fast_charging"]["wave_dir"] |
| 848 |
|
| 849 |
def get_current_fully_charged_effect(self): |
| 850 |
""" |
| 851 |
Get the device's current fully_charged effect |
| 852 |
|
| 853 |
:return: Effect |
| 854 |
:rtype: string |
| 855 |
""" |
| 856 |
self.logger.debug("DBus call get_current_fully_charged_effect") |
| 857 |
|
| 858 |
return self.zone["fully_charged"]["effect"] |
| 859 |
|
| 860 |
def get_current_fully_charged_effect_colors(self): |
| 861 |
""" |
| 862 |
Get the device's current fully_charged effect's colors |
| 863 |
|
| 864 |
:return: 3 colors |
| 865 |
:rtype: list of byte |
| 866 |
""" |
| 867 |
self.logger.debug("DBus call get_current_fully_charged_effect_colors") |
| 868 |
|
| 869 |
return self.zone["fully_charged"]["colors"] |
| 870 |
|
| 871 |
def get_current_fully_charged_effect_speed(self): |
| 872 |
""" |
| 873 |
Get the device's current fully_charged effect's speed |
| 874 |
|
| 875 |
:return: Speed |
| 876 |
:rtype: int |
| 877 |
""" |
| 878 |
self.logger.debug("DBus call get_current_fully_charged_effect_speed") |
| 879 |
|
| 880 |
return self.zone["fully_charged"]["speed"] |
| 881 |
|
| 882 |
def get_current_fully_charged_wave_dir(self): |
| 883 |
""" |
| 884 |
Get the device's current fully_charged wave direction |
| 885 |
|
| 886 |
:return: Direction |
| 887 |
:rtype: int |
| 888 |
""" |
| 889 |
self.logger.debug("DBus call get_current_fully_charged_wave_dir") |
| 890 |
|
| 891 |
return self.zone["fully_charged"]["wave_dir"] |
| 892 |
|
| 893 |
@property |
| 894 |
def effect_sync(self): |
| 895 |
""" |
| 896 |
Propagate the obsever call upwards, used for syncing effects |
| 897 |
|
| 898 |
:return: Effects sync flag |
| 899 |
:rtype: bool |
| 900 |
""" |
| 901 |
return self._effect_sync_propagate_up |
| 902 |
|
| 903 |
@effect_sync.setter |
| 904 |
def effect_sync(self, value): |
| 905 |
""" |
| 906 |
Setting to true will propagate observer events upwards |
| 907 |
|
| 908 |
:param value: Effect sync |
| 909 |
:type value: bool |
| 910 |
""" |
| 911 |
self._effect_sync_propagate_up = value |
| 912 |
|
| 913 |
@property |
| 914 |
def disable_notify(self): |
| 915 |
""" |
| 916 |
Disable notifications flag |
| 917 |
|
| 918 |
:return: Flag |
| 919 |
:rtype: bool |
| 920 |
""" |
| 921 |
return self._disable_notifications |
| 922 |
|
| 923 |
@disable_notify.setter |
| 924 |
def disable_notify(self, value): |
| 925 |
""" |
| 926 |
Set the disable notifications flag |
| 927 |
|
| 928 |
:param value: Disable |
| 929 |
:type value: bool |
| 930 |
""" |
| 931 |
self._disable_notifications = value |
| 932 |
|
| 933 |
@property |
| 934 |
def disable_persistence(self): |
| 935 |
""" |
| 936 |
Disable persistence flag |
| 937 |
|
| 938 |
:return: Flag |
| 939 |
:rtype: bool |
| 940 |
""" |
| 941 |
return self._disable_persistence |
| 942 |
|
| 943 |
@disable_persistence.setter |
| 944 |
def disable_persistence(self, value): |
| 945 |
""" |
| 946 |
Set the disable persistence flag |
| 947 |
|
| 948 |
:param value: Disable |
| 949 |
:type value: bool |
| 950 |
""" |
| 951 |
self._disable_persistence = value |
| 952 |
|
| 953 |
def get_driver_path(self, driver_filename): |
| 954 |
""" |
| 955 |
Get the path to a driver file |
| 956 |
|
| 957 |
:param driver_filename: Name of driver file |
| 958 |
:type driver_filename: str |
| 959 |
|
| 960 |
:return: Full path to driver |
| 961 |
:rtype: str |
| 962 |
""" |
| 963 |
return os.path.join(self._device_path, driver_filename) |
| 964 |
|
| 965 |
def get_serial(self): |
| 966 |
""" |
| 967 |
Get serial number for device |
| 968 |
|
| 969 |
:return: String of the serial number |
| 970 |
:rtype: str |
| 971 |
""" |
| 972 |
# TODO raise exception if serial can't be got and handle during device add |
| 973 |
if self._serial is None: |
| 974 |
serial_path = os.path.join(self._device_path, 'device_serial') |
| 975 |
count = 0 |
| 976 |
serial = '' |
| 977 |
while len(serial) == 0: |
| 978 |
if count >= 5: |
| 979 |
break |
| 980 |
|
| 981 |
try: |
| 982 |
with open(serial_path, 'r') as f: |
| 983 |
serial = f.read().strip() |
| 984 |
except (PermissionError, OSError) as err: |
| 985 |
self.logger.warning('getting serial: {0}'.format(err)) |
| 986 |
serial = '' |
| 987 |
except UnicodeDecodeError as err: |
| 988 |
self.logger.warning('malformed serial: {0}'.format(err)) |
| 989 |
serial = '' |
| 990 |
|
| 991 |
count += 1 |
| 992 |
|
| 993 |
if len(serial) == 0: |
| 994 |
time.sleep(0.1) |
| 995 |
self.logger.debug('getting serial: {0} count:{1}'.format(serial, count)) |
| 996 |
|
| 997 |
# Known bad serials: |
| 998 |
# - just an empty string |
| 999 |
# - "Default string" |
| 1000 |
# - "empty (NULL)" |
| 1001 |
# - "As printed in the D cover" |
| 1002 |
# - hex: 01 01 01 01 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 |
| 1003 |
if not re.fullmatch(r"[\dA-Z]+", serial): |
| 1004 |
self.logger.warning("Invalid serial number found, using a generated one.") |
| 1005 |
self.logger.warning("Original value: %s" % serial) |
| 1006 |
vid, pid = self.get_vid_pid() |
| 1007 |
idx = self._unknown_serial_counter.get((vid, pid), 0) |
| 1008 |
self._unknown_serial_counter[(vid, pid)] = idx + 1 |
| 1009 |
serial = "UNKNOWN_{0:04X}{1:04X}_{2:04d}".format(vid, pid, idx) |
| 1010 |
|
| 1011 |
self._serial = serial.replace(' ', '_') |
| 1012 |
|
| 1013 |
return self._serial |
| 1014 |
|
| 1015 |
def get_device_mode(self): |
| 1016 |
""" |
| 1017 |
Get device mode |
| 1018 |
|
| 1019 |
:return: String of device mode and arg separated by colon, e.g. 0:0 or 3:0 |
| 1020 |
:rtype: str |
| 1021 |
""" |
| 1022 |
device_mode_path = os.path.join(self._device_path, 'device_mode') |
| 1023 |
with open(device_mode_path, 'rb') as mode_file: |
| 1024 |
count = 0 |
| 1025 |
mode = mode_file.read().strip() |
| 1026 |
while len(mode) == 0: |
| 1027 |
if count >= 3: |
| 1028 |
break |
| 1029 |
mode = mode_file.read().strip() |
| 1030 |
|
| 1031 |
count += 1 |
| 1032 |
time.sleep(0.1) |
| 1033 |
|
| 1034 |
return "{0}:{1}".format(mode[0], mode[1]) |
| 1035 |
|
| 1036 |
def set_device_mode(self, mode_id, param): |
| 1037 |
""" |
| 1038 |
Set device mode |
| 1039 |
|
| 1040 |
:param mode_id: Device mode ID |
| 1041 |
:type mode_id: int |
| 1042 |
|
| 1043 |
:param param: Device mode parameter |
| 1044 |
:type param: int |
| 1045 |
""" |
| 1046 |
device_mode_path = os.path.join(self._device_path, 'device_mode') |
| 1047 |
with open(device_mode_path, 'wb') as mode_file: |
| 1048 |
|
| 1049 |
# Do some validation (even though its in the driver) |
| 1050 |
if mode_id not in (0, 3): |
| 1051 |
mode_id = 0 |
| 1052 |
if param != 0: |
| 1053 |
param = 0 |
| 1054 |
|
| 1055 |
mode_file.write(bytes([mode_id, param])) |
| 1056 |
|
| 1057 |
def _set_custom_effect(self): |
| 1058 |
""" |
| 1059 |
Set the device to use custom LED matrix |
| 1060 |
""" |
| 1061 |
# self.logger.debug("DBus call _set_custom_effect") |
| 1062 |
|
| 1063 |
driver_path = self.get_driver_path('matrix_effect_custom') |
| 1064 |
|
| 1065 |
payload = b'1' |
| 1066 |
|
| 1067 |
with open(driver_path, 'wb') as driver_file: |
| 1068 |
driver_file.write(payload) |
| 1069 |
|
| 1070 |
def _set_key_row(self, payload): |
| 1071 |
""" |
| 1072 |
Set the RGB matrix on the device |
| 1073 |
|
| 1074 |
Byte array like |
| 1075 |
[1, 255, 255, 00, 255, 255, 00, 255, 255, 00, 255, 255, 00, 255, 255, 00, 255, 255, 00, 255, 255, 00, 255, 255, 00, |
| 1076 |
255, 255, 00, 255, 255, 00, 255, 255, 00, 255, 255, 00, 255, 255, 00, 255, 255, 00, 255, 00, 00] |
| 1077 |
|
| 1078 |
First byte is row, on firefly its always 1, on keyboard its 0-5 |
| 1079 |
Then its 3byte groups of RGB |
| 1080 |
:param payload: Binary payload |
| 1081 |
:type payload: bytes |
| 1082 |
""" |
| 1083 |
# self.logger.debug("DBus call set_key_row") |
| 1084 |
|
| 1085 |
driver_path = self.get_driver_path('matrix_custom_frame') |
| 1086 |
|
| 1087 |
with open(driver_path, 'wb') as driver_file: |
| 1088 |
driver_file.write(payload) |
| 1089 |
|
| 1090 |
def _init_battery_manager(self): |
| 1091 |
""" |
| 1092 |
Initializes the BatteryManager using the provided name |
| 1093 |
""" |
| 1094 |
self._battery_manager = _BatteryManager(self, self._device_number, self.getDeviceName()) # pylint: disable=no-member |
| 1095 |
self._battery_manager.active = self.config.getboolean('Startup', 'battery_notifier', fallback=False) |
| 1096 |
self._battery_manager.frequency = self.config.getint('Startup', 'battery_notifier_freq', fallback=10 * 60) |
| 1097 |
self._battery_manager.percent = self.config.getint('Startup', 'battery_notifier_percent', fallback=33) |
| 1098 |
|
| 1099 |
def get_vid_pid(self): |
| 1100 |
""" |
| 1101 |
Get the usb VID PID |
| 1102 |
|
| 1103 |
:return: List of VID PID |
| 1104 |
:rtype: list of int |
| 1105 |
""" |
| 1106 |
result = [self.USB_VID, self.USB_PID] |
| 1107 |
return result |
| 1108 |
|
| 1109 |
def get_image_json(self): |
| 1110 |
# Deprecated API, but kept for backwards compatibility |
| 1111 |
return json.dumps({ |
| 1112 |
"top_img": self.get_device_image(), |
| 1113 |
"side_img": self.get_device_image(), |
| 1114 |
"perspective_img": self.get_device_image() |
| 1115 |
}) |
| 1116 |
|
| 1117 |
def get_device_image(self): |
| 1118 |
return self.DEVICE_IMAGE |
| 1119 |
|
| 1120 |
def load_methods(self): |
| 1121 |
""" |
| 1122 |
Load DBus methods |
| 1123 |
|
| 1124 |
Goes through the list in self.methods_internal and self.METHODS and loads each effect and adds it to DBus |
| 1125 |
""" |
| 1126 |
available_functions = {} |
| 1127 |
methods = dir(openrazer_daemon.dbus_services.dbus_methods) |
| 1128 |
for method in methods: |
| 1129 |
potential_function = getattr(openrazer_daemon.dbus_services.dbus_methods, method) |
| 1130 |
if isinstance(potential_function, types.FunctionType) and hasattr(potential_function, 'endpoint') and potential_function.endpoint: |
| 1131 |
available_functions[potential_function.__name__] = potential_function |
| 1132 |
|
| 1133 |
self.methods_internal.extend(self.METHODS) |
| 1134 |
for method_name in self.methods_internal: |
| 1135 |
try: |
| 1136 |
new_function = available_functions[method_name] |
| 1137 |
self.logger.debug("Adding %s.%s method to DBus", new_function.interface, new_function.name) |
| 1138 |
self.add_dbus_method(new_function.interface, new_function.name, new_function, new_function.in_sig, new_function.out_sig, new_function.byte_arrays) |
| 1139 |
except KeyError as e: |
| 1140 |
raise RuntimeError("Couldn't add method to DBus: " + str(e)) from None |
| 1141 |
|
| 1142 |
def suspend_device(self): |
| 1143 |
""" |
| 1144 |
Suspend device |
| 1145 |
""" |
| 1146 |
self.logger.info("Suspending %s", self.__class__.__name__) |
| 1147 |
self.disable_notify = True |
| 1148 |
self.disable_persistence = True |
| 1149 |
|
| 1150 |
self.disable_brightness() |
| 1151 |
self._suspend_device() |
| 1152 |
|
| 1153 |
self.disable_notify = False |
| 1154 |
self.disable_persistence = False |
| 1155 |
|
| 1156 |
def resume_device(self): |
| 1157 |
""" |
| 1158 |
Resume device |
| 1159 |
""" |
| 1160 |
self.logger.info("Resuming %s", self.__class__.__name__) |
| 1161 |
self.disable_notify = True |
| 1162 |
self.disable_persistence = True |
| 1163 |
|
| 1164 |
# Set device back to driver mode after e.g. suspend which resets the |
| 1165 |
# device to default device mode. |
| 1166 |
# NOTE: This is really the wrong place to put this, since this callback |
| 1167 |
# is for screensaver unlock, and not for 'wake up from suspend' or |
| 1168 |
# similar. Nevertheless for now this seems to be the best place for |
| 1169 |
# this and should resolve some issues with macro keys not working after |
| 1170 |
# suspend. |
| 1171 |
if self.DRIVER_MODE: |
| 1172 |
self.logger.info('Setting device back to "driver" mode.') |
| 1173 |
self.set_device_mode(0x03, 0x00) # Driver mode |
| 1174 |
|
| 1175 |
self.restore_brightness() |
| 1176 |
self._resume_device() |
| 1177 |
|
| 1178 |
self.disable_notify = False |
| 1179 |
self.disable_persistence = False |
| 1180 |
|
| 1181 |
def _suspend_device(self): |
| 1182 |
""" |
| 1183 |
Override to implement custom suspend behavior |
| 1184 |
""" |
| 1185 |
|
| 1186 |
def _resume_device(self): |
| 1187 |
""" |
| 1188 |
Override to implement custom resume behavior |
| 1189 |
""" |
| 1190 |
|
| 1191 |
def _close(self): |
| 1192 |
""" |
| 1193 |
To be overridden by any subclasses to do cleanup |
| 1194 |
""" |
| 1195 |
# Clear observer list |
| 1196 |
self._observer_list.clear() |
| 1197 |
|
| 1198 |
if self._battery_manager: |
| 1199 |
self._battery_manager.close() |
| 1200 |
|
| 1201 |
def close(self): |
| 1202 |
""" |
| 1203 |
Close any resources opened by subclasses |
| 1204 |
""" |
| 1205 |
if not self._is_closed: |
| 1206 |
# If this is a mouse, retrieve current DPI for local storage |
| 1207 |
# in case the user has changed the DPI on-the-fly |
| 1208 |
# (e.g. the DPI buttons) |
| 1209 |
if 'get_dpi_xy' in self.METHODS: |
| 1210 |
dpi_func = getattr(self, "getDPI", None) |
| 1211 |
if dpi_func is not None: |
| 1212 |
self.dpi = dpi_func() |
| 1213 |
|
| 1214 |
if self.DRIVER_MODE: |
| 1215 |
# Set back to device mode |
| 1216 |
try: |
| 1217 |
self.set_device_mode(0x00, 0x00) # Device mode |
| 1218 |
except FileNotFoundError: |
| 1219 |
pass |
| 1220 |
|
| 1221 |
self._close() |
| 1222 |
|
| 1223 |
self._is_closed = True |
| 1224 |
|
| 1225 |
def register_observer(self, observer): |
| 1226 |
""" |
| 1227 |
Observer design pattern, register |
| 1228 |
|
| 1229 |
:param observer: Observer |
| 1230 |
:type observer: object |
| 1231 |
""" |
| 1232 |
if observer not in self._observer_list: |
| 1233 |
self._observer_list.append(observer) |
| 1234 |
|
| 1235 |
def register_parent(self, parent): |
| 1236 |
""" |
| 1237 |
Register the parent as an observer to be optionally notified (sends to other devices) |
| 1238 |
|
| 1239 |
:param parent: Observer |
| 1240 |
:type parent: object |
| 1241 |
""" |
| 1242 |
self._parent = parent |
| 1243 |
|
| 1244 |
def remove_observer(self, observer): |
| 1245 |
""" |
| 1246 |
Obsever design pattern, remove |
| 1247 |
|
| 1248 |
:param observer: Observer |
| 1249 |
:type observer: object |
| 1250 |
""" |
| 1251 |
try: |
| 1252 |
self._observer_list.remove(observer) |
| 1253 |
except ValueError: |
| 1254 |
pass |
| 1255 |
|
| 1256 |
def notify_observers(self, msg): |
| 1257 |
""" |
| 1258 |
Notify observers with msg |
| 1259 |
|
| 1260 |
:param msg: Tuple with first element a string |
| 1261 |
:type msg: tuple |
| 1262 |
""" |
| 1263 |
if not self._disable_notifications: |
| 1264 |
self.logger.debug("Sending observer message: %s", str(msg)) |
| 1265 |
|
| 1266 |
if self._effect_sync_propagate_up and self._parent is not None: |
| 1267 |
self._parent.notify_parent(msg) |
| 1268 |
|
| 1269 |
for observer in self._observer_list: |
| 1270 |
observer.notify(msg) |
| 1271 |
|
| 1272 |
def notify(self, msg): |
| 1273 |
""" |
| 1274 |
Receive observer messages |
| 1275 |
|
| 1276 |
:param msg: Tuple with first element a string |
| 1277 |
:type msg: tuple |
| 1278 |
""" |
| 1279 |
self.logger.debug("Got observer message: %s", str(msg)) |
| 1280 |
|
| 1281 |
for observer in self._observer_list: |
| 1282 |
observer.notify(msg) |
| 1283 |
|
| 1284 |
@classmethod |
| 1285 |
def match(cls, device_id, dev_path): |
| 1286 |
""" |
| 1287 |
Match against the device ID |
| 1288 |
|
| 1289 |
:param device_id: Device ID like 0000:0000:0000.0000 |
| 1290 |
:type device_id: str |
| 1291 |
|
| 1292 |
:param dev_path: Device path. Normally '/sys/bus/hid/devices/0000:0000:0000.0000' |
| 1293 |
:type dev_path: str |
| 1294 |
|
| 1295 |
:return: True if its the correct device ID |
| 1296 |
:rtype: bool |
| 1297 |
""" |
| 1298 |
pattern = r'^[0-9A-F]{4}:' + '{0:04X}'.format(cls.USB_VID) + ':' + '{0:04X}'.format(cls.USB_PID) + r'\.[0-9A-F]{4}$' |
| 1299 |
|
| 1300 |
if re.match(pattern, device_id) is not None: |
| 1301 |
if 'device_type' in os.listdir(dev_path): |
| 1302 |
return True |
| 1303 |
|
| 1304 |
return False |
| 1305 |
|
| 1306 |
@staticmethod |
| 1307 |
def get_num_arguments(func): |
| 1308 |
""" |
| 1309 |
Get number of arguments in a function |
| 1310 |
|
| 1311 |
:param func: Function |
| 1312 |
:type func: callable |
| 1313 |
|
| 1314 |
:return: Number of arguments |
| 1315 |
:rtype: int |
| 1316 |
""" |
| 1317 |
func_sig = inspect.signature(func) |
| 1318 |
return len(func_sig.parameters) |
| 1319 |
|
| 1320 |
@staticmethod |
| 1321 |
def handle_underscores(string): |
| 1322 |
return re.sub(r'[_]+(?P<first>[a-z])', lambda m: m.group('first').upper(), string) |
| 1323 |
|
| 1324 |
@staticmethod |
| 1325 |
def capitalize_first_char(string): |
| 1326 |
return string[0].upper() + string[1:] |
| 1327 |
|
| 1328 |
def __del__(self): |
| 1329 |
self.close() |
| 1330 |
|
| 1331 |
def __repr__(self): |
| 1332 |
return "{0}:{1}".format(self.__class__.__name__, self.serial) |
| 1333 |
|
| 1334 |
|
| 1335 |
class RazerDeviceBrightnessSuspend(RazerDevice): |
| 1336 |
""" |
| 1337 |
Class for devices that have get_brightness and set_brightness |
| 1338 |
""" |
| 1339 |
|
| 1340 |
def __init__(self, *args, **kwargs): |
| 1341 |
if 'additional_methods' in kwargs: |
| 1342 |
kwargs['additional_methods'].extend(['get_brightness', 'set_brightness']) |
| 1343 |
else: |
| 1344 |
kwargs['additional_methods'] = ['get_brightness', 'set_brightness'] |
| 1345 |
super().__init__(*args, **kwargs) |