| 1 |
# SPDX-License-Identifier: GPL-2.0-or-later |
| 2 |
|
| 3 |
import math |
| 4 |
import struct |
| 5 |
from openrazer_daemon.dbus_services import endpoint |
| 6 |
|
| 7 |
|
| 8 |
@endpoint('razer.device.power', 'getBattery', out_sig='d') |
| 9 |
def get_battery(self): |
| 10 |
""" |
| 11 |
Get mouse's battery level |
| 12 |
""" |
| 13 |
self.logger.debug("DBus call get_battery") |
| 14 |
|
| 15 |
driver_path = self.get_driver_path('charge_level') |
| 16 |
|
| 17 |
with open(driver_path, 'r') as driver_file: |
| 18 |
battery_255 = float(driver_file.read().strip()) |
| 19 |
if battery_255 < 0: |
| 20 |
return -1.0 |
| 21 |
|
| 22 |
battery_100 = (battery_255 / 255) * 100 |
| 23 |
return battery_100 |
| 24 |
|
| 25 |
|
| 26 |
@endpoint('razer.device.power', 'isCharging', out_sig='b') |
| 27 |
def is_charging(self): |
| 28 |
""" |
| 29 |
Get charging status |
| 30 |
""" |
| 31 |
self.logger.debug("DBus call is_charging") |
| 32 |
|
| 33 |
driver_path = self.get_driver_path('charge_status') |
| 34 |
|
| 35 |
with open(driver_path, 'r') as driver_file: |
| 36 |
return bool(int(driver_file.read().strip())) |
| 37 |
|
| 38 |
|
| 39 |
@endpoint('razer.device.power', 'setIdleTime', in_sig='q') |
| 40 |
def set_idle_time(self, idle_time): |
| 41 |
""" |
| 42 |
Set the idle time of the mouse in seconds |
| 43 |
|
| 44 |
:param idle_time: Idle time in seconds (unsigned short) |
| 45 |
:type idle_time: int |
| 46 |
""" |
| 47 |
self.logger.debug("DBus call set_idle_time") |
| 48 |
|
| 49 |
driver_path = self.get_driver_path('device_idle_time') |
| 50 |
|
| 51 |
with open(driver_path, 'w') as driver_file: |
| 52 |
driver_file.write(str(idle_time)) |
| 53 |
|
| 54 |
|
| 55 |
@endpoint('razer.device.power', 'getIdleTime', out_sig='q') |
| 56 |
def get_idle_time(self): |
| 57 |
""" |
| 58 |
Get the idle time of the mouse in seconds |
| 59 |
|
| 60 |
:return: Idle time in seconds (unsigned short) |
| 61 |
:rtype: int |
| 62 |
""" |
| 63 |
self.logger.debug("DBus call get_idle_time") |
| 64 |
|
| 65 |
driver_path = self.get_driver_path('device_idle_time') |
| 66 |
|
| 67 |
with open(driver_path, 'r') as driver_file: |
| 68 |
result = driver_file.read() |
| 69 |
result = int(result.strip()) |
| 70 |
|
| 71 |
return result |
| 72 |
|
| 73 |
|
| 74 |
@endpoint('razer.device.power', 'setLowBatteryThreshold', in_sig='y') |
| 75 |
def set_low_battery_threshold(self, threshold): |
| 76 |
""" |
| 77 |
Set the low battery threshold as a percentage |
| 78 |
|
| 79 |
:param threshold: Battery threshold as a percentage |
| 80 |
:type threshold: int |
| 81 |
""" |
| 82 |
self.logger.debug("DBus call set_low_battery_threshold") |
| 83 |
|
| 84 |
driver_path = self.get_driver_path('charge_low_threshold') |
| 85 |
|
| 86 |
threshold = math.floor((threshold / 100) * 255) |
| 87 |
|
| 88 |
with open(driver_path, 'w') as driver_file: |
| 89 |
driver_file.write(str(threshold)) |
| 90 |
|
| 91 |
|
| 92 |
@endpoint('razer.device.power', 'getLowBatteryThreshold', out_sig='y') |
| 93 |
def get_low_battery_threshold(self): |
| 94 |
""" |
| 95 |
Get the low battery threshold as a percentage |
| 96 |
|
| 97 |
:return: Battery threshold as a percentage |
| 98 |
:rtype: int |
| 99 |
""" |
| 100 |
self.logger.debug("DBus call get_low_battery_threshold") |
| 101 |
|
| 102 |
driver_path = self.get_driver_path('charge_low_threshold') |
| 103 |
|
| 104 |
with open(driver_path, 'r') as driver_file: |
| 105 |
result = driver_file.read() |
| 106 |
result = int(result.strip()) |
| 107 |
|
| 108 |
return round((result / 255) * 100) |
| 109 |
|
| 110 |
|
| 111 |
@endpoint('razer.device.lighting.power', 'setChargeEffect', in_sig='y') |
| 112 |
def set_charge_effect(self, charge_effect): |
| 113 |
""" |
| 114 |
Set the charging effect. |
| 115 |
|
| 116 |
If 0x00 then it will use the current mouse's effect |
| 117 |
If 0x01 it will use the charge colour |
| 118 |
|
| 119 |
:param charge_effect: Charge effect |
| 120 |
:type charge_effect: int |
| 121 |
:return: |
| 122 |
""" |
| 123 |
self.logger.debug("DBus call set_charge_effect") |
| 124 |
|
| 125 |
driver_path = self.get_driver_path('charge_effect') |
| 126 |
|
| 127 |
with open(driver_path, 'wb') as driver_file: |
| 128 |
driver_file.write(bytes([charge_effect])) |
| 129 |
|
| 130 |
|
| 131 |
@endpoint('razer.device.lighting.power', 'setChargeColour', in_sig='yyy') |
| 132 |
def set_charge_colour(self, red, green, blue): |
| 133 |
""" |
| 134 |
Set the charge colour |
| 135 |
|
| 136 |
:param red: Red component |
| 137 |
:type red: int |
| 138 |
|
| 139 |
:param green: Green component |
| 140 |
:type green: int |
| 141 |
|
| 142 |
:param blue: Blue component |
| 143 |
:type blue: int |
| 144 |
""" |
| 145 |
self.logger.debug("DBus call set_charge_colour") |
| 146 |
|
| 147 |
driver_path = self.get_driver_path('charge_colour') |
| 148 |
|
| 149 |
payload = bytes([red, green, blue]) |
| 150 |
|
| 151 |
with open(driver_path, 'wb') as driver_file: |
| 152 |
driver_file.write(payload) |
| 153 |
|
| 154 |
|
| 155 |
@endpoint('razer.device.dpi', 'setDPI', in_sig='qq') |
| 156 |
def set_dpi_xy(self, dpi_x, dpi_y): |
| 157 |
""" |
| 158 |
Set the DPI on the mouse, Takes in 4 bytes big-endian |
| 159 |
|
| 160 |
:param dpi_x: X DPI |
| 161 |
:type dpi_x: int |
| 162 |
:param dpi_y: Y DPI |
| 163 |
:type dpi_x: int |
| 164 |
""" |
| 165 |
self.logger.debug("DBus call set_dpi_xy") |
| 166 |
|
| 167 |
if 'available_dpi' in self.METHODS: |
| 168 |
if dpi_y > 0: |
| 169 |
raise RuntimeError("Devices with available_dpi are expected to have only one DPI value set, got " + str(dpi_x) + ", " + str(dpi_y)) |
| 170 |
if dpi_x not in self.AVAILABLE_DPI: |
| 171 |
raise RuntimeError("Provided DPI " + str(dpi_x) + " is not in available_dpi values: " + str(self.AVAILABLE_DPI)) |
| 172 |
|
| 173 |
# check that DPI is not more than the maximum |
| 174 |
if dpi_x > self.DPI_MAX: |
| 175 |
raise RuntimeError("Provided DPI " + str(dpi_x) + " is larger than maximum of " + str(self.DPI_MAX)) |
| 176 |
if dpi_y > self.DPI_MAX: |
| 177 |
raise RuntimeError("Provided DPI " + str(dpi_x) + " is larger than maximum of " + str(self.DPI_MAX)) |
| 178 |
|
| 179 |
driver_path = self.get_driver_path('dpi') |
| 180 |
|
| 181 |
if self._testing: |
| 182 |
with open(driver_path, 'w') as driver_file: |
| 183 |
if dpi_y <= 0: |
| 184 |
driver_file.write("{}".format(dpi_x)) |
| 185 |
else: |
| 186 |
driver_file.write("{}:{}".format(dpi_x, dpi_y)) |
| 187 |
return |
| 188 |
|
| 189 |
# If the application requests just one value to be written |
| 190 |
if dpi_y <= 0: |
| 191 |
dpi_bytes = struct.pack('>H', dpi_x) |
| 192 |
else: |
| 193 |
dpi_bytes = struct.pack('>HH', dpi_x, dpi_y) |
| 194 |
|
| 195 |
# store to local variable (TODO: can we replace this by getting from persistence?) |
| 196 |
self.dpi[0] = dpi_x |
| 197 |
self.dpi[1] = dpi_y |
| 198 |
|
| 199 |
self.set_persistence(None, "dpi_x", dpi_x) |
| 200 |
self.set_persistence(None, "dpi_y", dpi_y) |
| 201 |
|
| 202 |
with open(driver_path, 'wb') as driver_file: |
| 203 |
driver_file.write(dpi_bytes) |
| 204 |
|
| 205 |
|
| 206 |
@endpoint('razer.device.dpi', 'getDPI', out_sig='ai') |
| 207 |
def get_dpi_xy(self): |
| 208 |
""" |
| 209 |
get the DPI on the mouse |
| 210 |
|
| 211 |
:return: List of X, Y DPI |
| 212 |
:rtype: list of int |
| 213 |
""" |
| 214 |
self.logger.debug("DBus call get_dpi_xy") |
| 215 |
|
| 216 |
driver_path = self.get_driver_path('dpi') |
| 217 |
|
| 218 |
# try retrieving DPI from the hardware. |
| 219 |
# if we can't (e.g. because the mouse has been disconnected) |
| 220 |
# return the value in local storage. |
| 221 |
try: |
| 222 |
with open(driver_path, 'r') as driver_file: |
| 223 |
result = driver_file.read() |
| 224 |
dpi = [int(dpi) for dpi in result.strip().split(':')] |
| 225 |
except FileNotFoundError: |
| 226 |
return self.dpi |
| 227 |
|
| 228 |
if 'available_dpi' in self.METHODS: |
| 229 |
if len(dpi) != 1: |
| 230 |
raise RuntimeError("Devices with available_dpi are expected to have only one DPI value returned from driver, got " + str(dpi)) |
| 231 |
dpi = dpi[0], 0 |
| 232 |
|
| 233 |
return dpi |
| 234 |
|
| 235 |
|
| 236 |
@endpoint('razer.device.dpi', 'setDPIStages', in_sig='ya(qq)') |
| 237 |
def set_dpi_stages(self, active_stage, dpi_stages): |
| 238 |
""" |
| 239 |
Set the DPI on the mouse, Takes in pairs of 2 bytes big-endian |
| 240 |
|
| 241 |
:param active_stage: DPI stage to enable |
| 242 |
:param dpi_stages: pairs of dpi X and dpi Y for each stage |
| 243 |
:type dpi_stages: list of (int, int) |
| 244 |
""" |
| 245 |
self.logger.debug("DBus call set_dpi_stages") |
| 246 |
|
| 247 |
driver_path = self.get_driver_path('dpi_stages') |
| 248 |
|
| 249 |
dpi_bytes = struct.pack('B', active_stage) |
| 250 |
for dpi_x, dpi_y in dpi_stages: |
| 251 |
dpi_bytes += struct.pack('>HH', dpi_x, dpi_y) |
| 252 |
|
| 253 |
with open(driver_path, 'wb') as driver_file: |
| 254 |
driver_file.write(dpi_bytes) |
| 255 |
|
| 256 |
|
| 257 |
@endpoint('razer.device.dpi', 'getDPIStages', out_sig='(ya(qq))') |
| 258 |
def get_dpi_stages(self): |
| 259 |
""" |
| 260 |
get the DPI stages on the mouse |
| 261 |
|
| 262 |
:return: List of X, Y DPI |
| 263 |
:rtype: (int, list of (int, int)) |
| 264 |
""" |
| 265 |
self.logger.debug("DBus call get_dpi_stages") |
| 266 |
|
| 267 |
driver_path = self.get_driver_path('dpi_stages') |
| 268 |
|
| 269 |
dpi_stages = [] |
| 270 |
with open(driver_path, 'rb') as driver_file: |
| 271 |
result = driver_file.read() |
| 272 |
|
| 273 |
(active_stage,) = struct.unpack('B', result[:1]) |
| 274 |
result = result[1:] |
| 275 |
|
| 276 |
while len(result) >= 4: |
| 277 |
(dpi_x, dpi_y) = struct.unpack('>HH', result[:4]) |
| 278 |
dpi_stages.append((dpi_x, dpi_y)) |
| 279 |
result = result[4:] |
| 280 |
|
| 281 |
return (active_stage, dpi_stages) |
| 282 |
|
| 283 |
|
| 284 |
@endpoint('razer.device.dpi', 'maxDPI', out_sig='i') |
| 285 |
def max_dpi(self): |
| 286 |
self.logger.debug("DBus call max_dpi") |
| 287 |
|
| 288 |
return self.DPI_MAX |
| 289 |
|
| 290 |
|
| 291 |
@endpoint('razer.device.dpi', 'availableDPI', out_sig='ai') |
| 292 |
def available_dpi(self): |
| 293 |
self.logger.debug("DBus call available_dpi") |
| 294 |
|
| 295 |
if hasattr(self, 'AVAILABLE_DPI'): |
| 296 |
return self.AVAILABLE_DPI |
| 297 |
|
| 298 |
return [] |
| 299 |
|
| 300 |
|
| 301 |
@endpoint('razer.device.misc', 'setPollRate', in_sig='q') |
| 302 |
def set_poll_rate(self, rate): |
| 303 |
""" |
| 304 |
Set the polling rate on the device, Takes in 4 bytes big-endian |
| 305 |
|
| 306 |
:param rate: Poll rate |
| 307 |
:type rate: int |
| 308 |
""" |
| 309 |
self.logger.debug("DBus call set_poll_rate") |
| 310 |
|
| 311 |
if rate not in self.POLL_RATES: |
| 312 |
raise RuntimeError("Poll rate " + str(rate) + " is not allowed. Allowed values: " + str(self.POLL_RATES)) |
| 313 |
|
| 314 |
driver_path = self.get_driver_path('poll_rate') |
| 315 |
|
| 316 |
# remember poll rate |
| 317 |
self.poll_rate = rate |
| 318 |
|
| 319 |
with open(driver_path, 'w') as driver_file: |
| 320 |
driver_file.write(str(rate)) |
| 321 |
|
| 322 |
|
| 323 |
@endpoint('razer.device.misc', 'getPollRate', out_sig='i') |
| 324 |
def get_poll_rate(self): |
| 325 |
""" |
| 326 |
Get the polling rate from the device |
| 327 |
|
| 328 |
:return: Poll rate |
| 329 |
:rtype: int |
| 330 |
""" |
| 331 |
self.logger.debug("DBus call get_poll_rate") |
| 332 |
|
| 333 |
return int(self.poll_rate) |
| 334 |
|
| 335 |
|
| 336 |
@endpoint('razer.device.misc', 'getSupportedPollRates', out_sig='aq') |
| 337 |
def get_supported_poll_rates(self): |
| 338 |
""" |
| 339 |
Get the polling rates supported by the device |
| 340 |
|
| 341 |
:return: Supported poll rates |
| 342 |
:rtype: list of int |
| 343 |
""" |
| 344 |
self.logger.debug("DBus call get_supported_poll_rates") |
| 345 |
|
| 346 |
return self.POLL_RATES |
| 347 |
|
| 348 |
|
| 349 |
@endpoint('razer.device.misc', 'setHyperPollingLED', in_sig='y') |
| 350 |
def set_hyperpolling_wireless_dongle_indicator_led_mode(self, mode): |
| 351 |
""" |
| 352 |
Set the function of the LED on the dongle, takes in 1 char |
| 353 |
1 = Connection Status (green if connected to mouse) |
| 354 |
2 = Battery Status (green if high battery, yellow if medium battery, red if low battery) |
| 355 |
3 = Battery Warning (red if low battery, off otherwise) |
| 356 |
|
| 357 |
:param mode: LED mode |
| 358 |
:type mode: char |
| 359 |
""" |
| 360 |
self.logger.debug("DBus call set_hyperpolling_wireless_dongle_indicator_led_mode") |
| 361 |
|
| 362 |
driver_path = self.get_driver_path('hyperpolling_wireless_dongle_indicator_led_mode') |
| 363 |
|
| 364 |
with open(driver_path, 'w') as driver_file: |
| 365 |
driver_file.write(str(mode)) |
| 366 |
|
| 367 |
|
| 368 |
@endpoint('razer.device.misc', 'setHyperPollingPair', in_sig='s') |
| 369 |
def set_hyperpolling_wireless_dongle_pair(self, pid): |
| 370 |
""" |
| 371 |
Set Pairing mode, takes in 1 string which is the PID |
| 372 |
|
| 373 |
:param pid: product id |
| 374 |
:type pid: char |
| 375 |
""" |
| 376 |
self.logger.debug("DBus call set_hyperpolling_wireless_dongle_pair") |
| 377 |
|
| 378 |
driver_path = self.get_driver_path('hyperpolling_wireless_dongle_pair') |
| 379 |
|
| 380 |
with open(driver_path, 'w') as driver_file: |
| 381 |
driver_file.write(pid) |
| 382 |
|
| 383 |
|
| 384 |
@endpoint('razer.device.misc', 'setHyperPollingUnpair', in_sig='s') |
| 385 |
def set_hyperpolling_wireless_dongle_unpair(self, pid): |
| 386 |
""" |
| 387 |
Set Unpairing mode, takes in 1 string which is the PID |
| 388 |
|
| 389 |
:param pid: product id |
| 390 |
:type pid: char |
| 391 |
""" |
| 392 |
self.logger.debug("DBus call set_hyperpolling_wireless_dongle_unpair") |
| 393 |
|
| 394 |
driver_path = self.get_driver_path('hyperpolling_wireless_dongle_unpair') |
| 395 |
|
| 396 |
with open(driver_path, 'w') as driver_file: |
| 397 |
driver_file.write(pid) |