Skip to content

Instantly share code, notes, and snippets.

@lierdakil
Last active February 10, 2020 01:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lierdakil/463bd62f077bb81ac3975257c81df60c to your computer and use it in GitHub Desktop.
Save lierdakil/463bd62f077bb81ac3975257c81df60c to your computer and use it in GitHub Desktop.
Enables Razer BlackWidow 2013 macro keys in Linux
clang
-I/usr/include/libusb-1.0
.ccls-cache
razer-bwu13-macro
Enables the M1-5 and FN keys to send scancodes on the Razer BlackWidow
and BlackWidow Ultimate keyboards.
You can use 'xev' and 'xbindkeys' to assign actions to the macro keys.
From my experience, M1 gets mapped to XF86Tools, and M2..M5 to
XF86Launch5..XF86Launch8. YMMV
There are two versions of the code, one for C using libusb, and another for
python 2.7 with pyUSB. The latter is provided mostly for historical purposes,
since python 2.7 reached its end-of-life.
Python code (c) 2016 Nikolay "Lierdakil" Yakimov
C code (c) 2020 Nikolay "Lierdakil" Yakimov
Based on code by Michael Fincham <michael@finch.am> 2012-03-05
https://finch.am/projects/blackwidow/blackwidow_enable.py
This code is released under the MIT license. See LICENSE for details.
Copyright 2016 Nikolay "Lierdakil" Yakimov
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
USBFLAGS=$(shell pkg-config --cflags --libs libusb-1.0)
all:
gcc razer-bwu13-macro.c -o razer-bwu13-macro $(USBFLAGS)
/*******************************************************************************
* Enables the M1-5 and FN keys to send scancodes on the Razer BlackWidow
* and BlackWidow Ultimate keyboards.
*
* Requires libusb-1.0
*
* (c) 2020 Nikolay "Lierdakil" Yakimov
*
* Based on code by Michael Fincham <michael@finch.am> 2012-03-05
* https://finch.am/projects/blackwidow/blackwidow_enable.py
*
* This code is released under the MIT license.
******************************************************************************/
#include <stdio.h>
#include <libusb.h>
static const uint16_t USB_VENDOR = 0x1532; // Razer
// BlackWidow Ultimate 2013 // try '0x011a' if it doesn't work for you.
static const uint16_t USB_PRODUCT = 0x011b;
// These values are from the USB HID 1.11 spec section 7.2.
static const uint8_t USB_REQUEST_TYPE = LIBUSB_ENDPOINT_OUT|LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE;
static const uint8_t USB_REQUEST = 0x09; // SET_REPORT
// These values are from the manufacturer's driver.
static const uint16_t USB_VALUE = 0x0300;
static const uint8_t USB_INDEX = 0x2;
static const uint8_t USB_INTERFACE = 2;
#define CHECK_RESULT(m, v, f) \
r = f; \
if (r != v) { \
printf(m " failed\n"); \
return -1; \
} \
int main() {
unsigned char buffer[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x04, 0x02, 0x00
, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00
};
libusb_device_handle *handle;
libusb_device *dev;
libusb_context *ctx;
int r;
libusb_init(&ctx);
handle = libusb_open_device_with_vid_pid(NULL, USB_VENDOR, USB_PRODUCT);
if (handle == NULL) {
printf("Open failed\n");
return -1;
}
CHECK_RESULT("Detach", LIBUSB_SUCCESS, libusb_set_auto_detach_kernel_driver(handle, 1));
CHECK_RESULT("Claim", LIBUSB_SUCCESS, libusb_claim_interface(handle, USB_INTERFACE));
CHECK_RESULT("Control", sizeof(buffer), libusb_control_transfer(handle, USB_REQUEST_TYPE, USB_REQUEST, USB_VALUE, USB_INDEX, buffer, sizeof(buffer), 1000));
CHECK_RESULT("Release", LIBUSB_SUCCESS, libusb_release_interface(handle, USB_INTERFACE));
libusb_exit(ctx);
printf("Success.\n");
return 0;
}
#!/usr/bin/env nix-shell
#!nix-shell --pure -i python -p "python2.withPackages(ps: [ ps.pyusb ])"
# Persist environment: nix-shell -p 'python2.withPackages(ps: [ ps.pyusb ])' --indirect --add-root $HOME/.config/nixpkgs/gcroots/pythonUsb
# blackwidow_enable.py
#
# Enables the M1-5 and FN keys to send scancodes on the Razer BlackWidow
# and BlackWidow Ultimate keyboards.
#
# You can use 'xev' and 'xbindkeys' to assign actions to the macro keys.
# From my experience, M1 gets mapped to XF86Tools, and M2..M5 to
# XF86Launch5..XF86Launch8. YMMV
#
# Requires the PyUSB library 1.x
#
# Designed to work with nix-shell. If you don't use Nix package manager,
# install Python 2 and PyUSB 1.x -- pyusb-1.0.2 with Python 2.7 is known to work.
# Then change shebang (first line) to be:
#
#!/usr/bin/env python2
#
# or, if you only have Python 2,
#
#!/usr/bin/env python
#
# (c) 2016 Nikolay "Lierdakil" Yakimov
# Based on code by Michael Fincham <michael@finch.am> 2012-03-05
# This code is released under the MIT license.
#
# Original code: <https://finch.am/projects/blackwidow/blackwidow_enable.py>
import sys
import usb
from usb.util import *
USB_VENDOR = 0x1532 # Razer
USB_PRODUCT = 0x011b # BlackWidow Ultimate 2013 // try '0x011a' if it doesn't work for you.
# These values are from the USB HID 1.11 spec section 7.2.
USB_REQUEST_TYPE = build_request_type(CTRL_OUT, CTRL_TYPE_CLASS, CTRL_RECIPIENT_INTERFACE)
USB_REQUEST = 0x09 # SET_REPORT
# These values are from the manufacturer's driver.
USB_VALUE = 0x0300
USB_INDEX = 0x2
USB_INTERFACE = 2
USB_BUFFER = b"\x00\x00\x00\x00\x00\x02\x00\x04\x02\x00\x00\x00\x00\x00\
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00"
# actual code
device = usb.core.find(idVendor=USB_VENDOR, idProduct=USB_PRODUCT)
if device is None:
sys.stderr.write("BlackWidow not found.\n")
sys.exit(1)
try:
device.detach_kernel_driver(USB_INTERFACE)
except usb.USBError: #This usually means that kernel driver is already detached
pass
result = device.ctrl_transfer(bmRequestType = USB_REQUEST_TYPE,
bRequest = USB_REQUEST,
wValue = USB_VALUE,
wIndex = USB_INDEX,
data_or_wLength = USB_BUFFER)
if result == len(USB_BUFFER):
sys.stderr.write("Configured BlackWidow.\n")
else:
sys.stderr.write("Configuration failed.\n")
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment