| 1 |
# SPDX-License-Identifier: GPL-2.0-or-later |
| 2 |
|
| 3 |
""" |
| 4 |
Naga Hex DPI |
| 5 |
""" |
| 6 |
import struct |
| 7 |
from openrazer_daemon.dbus_services import endpoint |
| 8 |
|
| 9 |
|
| 10 |
@endpoint('razer.device.dpi', 'setDPI', in_sig='qq') |
| 11 |
def set_dpi_xy_byte(self, dpi_x, dpi_y): |
| 12 |
""" |
| 13 |
Set the DPI on the mouse, Takes in 4 bytes big-endian and converts it to bytes |
| 14 |
|
| 15 |
:param dpi_x: X DPI |
| 16 |
:type dpi_x: int |
| 17 |
:param dpi_y: Y DPI |
| 18 |
:type dpi_x: int |
| 19 |
""" |
| 20 |
self.logger.debug("DBus call set_dpi_xy_byte") |
| 21 |
|
| 22 |
# check that DPI is not more than the maximum |
| 23 |
if dpi_x > self.DPI_MAX: |
| 24 |
raise RuntimeError("Provided DPI " + str(dpi_x) + " is larger than maximum of " + str(self.DPI_MAX)) |
| 25 |
if dpi_y > self.DPI_MAX: |
| 26 |
raise RuntimeError("Provided DPI " + str(dpi_x) + " is larger than maximum of " + str(self.DPI_MAX)) |
| 27 |
|
| 28 |
driver_path = self.get_driver_path('dpi') |
| 29 |
|
| 30 |
if dpi_x > 6750: |
| 31 |
dpi_x = 6750 |
| 32 |
elif dpi_x < 100: |
| 33 |
dpi_x = 100 |
| 34 |
if dpi_y > 6750: |
| 35 |
dpi_y = 6750 |
| 36 |
elif dpi_y < 100: |
| 37 |
dpi_y = 100 |
| 38 |
|
| 39 |
dpi_x_scaled = int(round(dpi_x / 6750 * 255, 2)) |
| 40 |
dpi_y_scaled = int(round(dpi_y / 6750 * 255, 2)) |
| 41 |
|
| 42 |
self.dpi[0] = dpi_x |
| 43 |
self.dpi[1] = dpi_y |
| 44 |
|
| 45 |
self.set_persistence(None, "dpi_x", dpi_x_scaled) |
| 46 |
self.set_persistence(None, "dpi_y", dpi_y_scaled) |
| 47 |
|
| 48 |
if self._testing: |
| 49 |
with open(driver_path, 'w') as driver_file: |
| 50 |
driver_file.write("{}:{}".format(dpi_x_scaled, dpi_y_scaled)) |
| 51 |
return |
| 52 |
|
| 53 |
dpi_bytes = struct.pack('>BB', dpi_x_scaled, dpi_y_scaled) |
| 54 |
|
| 55 |
with open(driver_path, 'wb') as driver_file: |
| 56 |
driver_file.write(dpi_bytes) |
| 57 |
|
| 58 |
|
| 59 |
@endpoint('razer.device.dpi', 'getDPI', out_sig='ai') |
| 60 |
def get_dpi_xy_byte(self): |
| 61 |
""" |
| 62 |
get the DPI on the mouse |
| 63 |
|
| 64 |
:return: List of X, Y DPI |
| 65 |
:rtype: list of int |
| 66 |
""" |
| 67 |
self.logger.debug("DBus call get_dpi_xy_byte") |
| 68 |
|
| 69 |
driver_path = self.get_driver_path('dpi') |
| 70 |
|
| 71 |
# try retrieving DPI from the hardware. |
| 72 |
# if we can't (e.g. because the mouse has been disconnected) |
| 73 |
# return the value in local storage. |
| 74 |
try: |
| 75 |
with open(driver_path, 'r') as driver_file: |
| 76 |
result = driver_file.read() |
| 77 |
dpi_x, dpi_y = [int(dpi) for dpi in result.strip().split(':')] |
| 78 |
dpi_x = int(round(dpi_x / 255 * 6750, 2)) |
| 79 |
dpi_y = int(round(dpi_y / 255 * 6750, 2)) |
| 80 |
except FileNotFoundError: |
| 81 |
dpi_x, dpi_y = self.dpi |
| 82 |
|
| 83 |
return [dpi_x, dpi_y] |