|
| 1 | +""" |
| 2 | +Author: RedFantom |
| 3 | +License: GNU GPLv3 |
| 4 | +Copyright (c) 2017-2018 RedFantom |
| 5 | +""" |
| 6 | +# Standard Library |
| 7 | +import logging |
| 8 | +# Project Modules |
| 9 | +from rgbkeyboards.keyboard import BaseKeyboard |
| 10 | + |
| 11 | +_DEVICES = [ |
| 12 | + "Razer BlackWidow Ultimate 2012", |
| 13 | + "Razer BlackWidow Classic ", |
| 14 | + "Razer Anansi", |
| 15 | + "Razer BlackWidow Ultimate 2013", |
| 16 | + "Razer BlackWidow Stealth", |
| 17 | + "Razer DeathStalker Expert", |
| 18 | + "Razer BlackWidow Chroma", |
| 19 | + "Razer DeathStalker Chroma", |
| 20 | + "Razer Blade Stealth", |
| 21 | + "Razer Orbweaver Chroma", |
| 22 | + "Razer BlackWidow Tournament Edition Chroma", |
| 23 | + "Razer Blade QHD", |
| 24 | + "Razer Blade Pro ", |
| 25 | + "Razer BlackWidow Chroma ", |
| 26 | + "Razer BlackWidow Ultimate 2016", |
| 27 | + "Razer BlackWidow X Chroma", |
| 28 | + "Razer BlackWidow X Ultimate", |
| 29 | + "Razer BlackWidow X Tournament Edition Chroma", |
| 30 | + "Razer Ornata Chroma", |
| 31 | + "Razer Ornata", |
| 32 | + "Razer Blade Stealth ", |
| 33 | + "Razer BlackWidow Chroma V2", |
| 34 | + "Razer Blade ", |
| 35 | + "Razer Cynosa Chroma", |
| 36 | + "Razer Blade Stealth ", |
| 37 | + "Razer Blade Pro ", |
| 38 | + "Razer Blade Pro FullHD ", |
| 39 | + "Razer Blade Stealth ", |
| 40 | + "Razer Blade 15 ", |
| 41 | + "Razer Blade 15 Mercury", |
| 42 | +] |
| 43 | + |
| 44 | + |
| 45 | +class Keyboard(BaseKeyboard): |
| 46 | + """ |
| 47 | + Wrapper around openrazer Python Library |
| 48 | +
|
| 49 | + The openrazer project provides open source kernel drivers for many |
| 50 | + Razer keyboards. Communication happens through DBus, but all of that |
| 51 | + is abstracted away in the Python library. |
| 52 | +
|
| 53 | + In order for this back-end to be used, openrazer must be installed |
| 54 | + per installation instructions, which can be found in the README of |
| 55 | + the project: <github.com/openrazer/openrazer> |
| 56 | +
|
| 57 | + Due to type-hinting, the Python Library of openrazer is only |
| 58 | + compatible with Python 3.5 and up. |
| 59 | + """ |
| 60 | + |
| 61 | + def _setup_lib(self): |
| 62 | + """Initialize the library""" |
| 63 | + try: |
| 64 | + from openrazer.client import DeviceManager |
| 65 | + except ImportError: # The library is not available |
| 66 | + print("Please install the openrazer package for your platform") |
| 67 | + raise |
| 68 | + except SyntaxError: |
| 69 | + print("The openrazer Python library only supports Python 3.5+") |
| 70 | + raise |
| 71 | + self._lib = DeviceManager() |
| 72 | + self._device = None |
| 73 | + |
| 74 | + def _get_device_available(self): |
| 75 | + """Return whether any supported device is available""" |
| 76 | + devices = self._lib.devices |
| 77 | + return len(devices) != 0 |
| 78 | + |
| 79 | + def _enable_control(self): |
| 80 | + """Enable control of the first keyboard detected""" |
| 81 | + devices = self._lib.devices |
| 82 | + if len(devices) == 0: |
| 83 | + return False |
| 84 | + self._device = devices[0] |
| 85 | + return True |
| 86 | + |
| 87 | + def _disable_control(self): |
| 88 | + """Disable control on the controlled keyboard""" |
| 89 | + self._device = None |
| 90 | + return True |
| 91 | + |
| 92 | + def _set_full_color(self, r, g, b): |
| 93 | + """Set the color of all LEDs on the keyboard""" |
| 94 | + if self._device is None: |
| 95 | + return False |
| 96 | + return self._device.fx.static(r, g, b) # bool |
| 97 | + |
| 98 | + def _set_ind_color(self, leds): |
| 99 | + """Set the color of individual LEDs""" |
| 100 | + # TODO: Implement support for individual LED control |
| 101 | + # Notes: |
| 102 | + # Can be implemented using the RazerAdvancedFX class function |
| 103 | + # set_key(column: int, rgb: Tuple[int, int, int], row: int) |
| 104 | + # Requires layout support due to row/column system: row/columns |
| 105 | + # must be mapped to key names as with masterkeys backends |
| 106 | + return False |
| 107 | + |
| 108 | + @staticmethod |
| 109 | + def is_product_supported(product): |
| 110 | + """ |
| 111 | + Return whether a product is supported by the library |
| 112 | +
|
| 113 | + Device support depends on the underlying openrazer kernel |
| 114 | + drivers, and can thus change. Whether a device is actually |
| 115 | + supported can only be determined by checking whether a device |
| 116 | + is available through the DeviceManager().devices property. |
| 117 | +
|
| 118 | + However, if this function is called, the library is not imported |
| 119 | + and initialized yet, and thus whether a specific device is |
| 120 | + supported can only be checked statically. |
| 121 | +
|
| 122 | + Therefore, the product name string is checked against a list |
| 123 | + of supported devices taken from the README of the project. |
| 124 | + """ |
| 125 | + return product in _DEVICES |
0 commit comments