Skip to content

Commit

Permalink
daemon: Persist DPI values for 'nagahex' method
Browse files Browse the repository at this point in the history
Includes DeathAdder 2013. Fixes #1350.
  • Loading branch information
lah7 committed Nov 28, 2020
1 parent 604f1ca commit ff7c268
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion daemon/openrazer_daemon/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ def write_persistence(self, persistence_file):

for device in self._razer_devices:
self._persistence[device.dbus.storage_name] = {}
if 'set_dpi_xy' in device.dbus.METHODS:
if 'set_dpi_xy' in device.dbus.METHODS or 'set_dpi_xy_byte' in device.dbus.METHODS:
self._persistence[device.dbus.storage_name]['dpi_x'] = str(device.dbus.dpi[0])
self._persistence[device.dbus.storage_name]['dpi_y'] = str(device.dbus.dpi[1])

Expand Down
27 changes: 19 additions & 8 deletions daemon/openrazer_daemon/dbus_services/dbus_methods/nagahex.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def set_dpi_xy_byte(self, dpi_x, dpi_y):
:param dpi_y: Y DPI
:type dpi_x: int
"""
self.logger.debug("DBus call set_dpi_both")
self.logger.debug("DBus call set_dpi_xy_byte")

driver_path = self.get_driver_path('dpi')

Expand All @@ -31,6 +31,12 @@ def set_dpi_xy_byte(self, dpi_x, dpi_y):
dpi_x_scaled = int(round(dpi_x / 6750 * 255, 2))
dpi_y_scaled = int(round(dpi_y / 6750 * 255, 2))

self.dpi[0] = dpi_x
self.dpi[1] = dpi_y

self.set_persistence(None, "dpi_x", dpi_x_scaled)
self.set_persistence(None, "dpi_y", dpi_y_scaled)

if self._testing:
with open(driver_path, 'w') as driver_file:
driver_file.write("{}:{}".format(dpi_x_scaled, dpi_y_scaled))
Expand All @@ -50,15 +56,20 @@ def get_dpi_xy_byte(self):
:return: List of X, Y DPI
:rtype: list of int
"""
self.logger.debug("DBus call get_dpi_both")
self.logger.debug("DBus call get_dpi_xy_byte")

driver_path = self.get_driver_path('dpi')

with open(driver_path, 'r') as driver_file:
result = driver_file.read()
dpi_x, dpi_y = [int(dpi) for dpi in result.strip().split(':')]

dpi_x = int(round(dpi_x / 255 * 6750, 2))
dpi_y = int(round(dpi_y / 255 * 6750, 2))
# try retrieving DPI from the hardware.
# if we can't (e.g. because the mouse has been disconnected)
# return the value in local storage.
try:
with open(driver_path, 'r') as driver_file:
result = driver_file.read()
dpi_x, dpi_y = [int(dpi) for dpi in result.strip().split(':')]
dpi_x = int(round(dpi_x / 255 * 6750, 2))
dpi_y = int(round(dpi_y / 255 * 6750, 2))
except FileNotFoundError:
dpi_x, dpi_y = self.dpi

return [dpi_x, dpi_y]
2 changes: 1 addition & 1 deletion daemon/openrazer_daemon/hardware/device_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def __init__(self, device_path, device_number, config, persistence, testing=Fals

# load last DPI/poll rate state
if self.persistence.has_section(self.storage_name):
if 'set_dpi_xy' in self.METHODS:
if 'set_dpi_xy' in self.METHODS or 'set_dpi_xy_byte' in self.METHODS:
try:
self.dpi[0] = int(self.persistence[self.storage_name]['dpi_x'])
self.dpi[1] = int(self.persistence[self.storage_name]['dpi_y'])
Expand Down

0 comments on commit ff7c268

Please sign in to comment.