aboutsummaryrefslogtreecommitdiff
path: root/src/lib/razerdevice.c
blob: 8a165c550f61ab6a0989485ba50946091360bd40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#include <stdlib.h>
#include "razerdevice.h"

bool is_razer_device(IOUSBDeviceInterface **dev)
{
	kern_return_t kr;
	UInt16 vendor;
	UInt16 product;
	UInt16 release;

	kr = (*dev)->GetDeviceVendor(dev, &vendor);
	kr = (*dev)->GetDeviceProduct(dev, &product);
	kr = (*dev)->GetDeviceReleaseNumber(dev, &release);

	return vendor == USB_VENDOR_ID_RAZER;
}

bool is_keyboard(IOUSBDeviceInterface **usb_dev)
{
	UInt16 product = -1;
	(*usb_dev)->GetDeviceProduct(usb_dev, &product);

	switch (product)
	{
	case USB_DEVICE_ID_RAZER_NOSTROMO:
	case USB_DEVICE_ID_RAZER_ORBWEAVER:
	case USB_DEVICE_ID_RAZER_ORBWEAVER_CHROMA:
	case USB_DEVICE_ID_RAZER_BLACKWIDOW_STEALTH:
	case USB_DEVICE_ID_RAZER_BLACKWIDOW_STEALTH_EDITION:
	case USB_DEVICE_ID_RAZER_BLACKWIDOW_ULTIMATE_2012:
	case USB_DEVICE_ID_RAZER_BLACKWIDOW_ULTIMATE_2013:
	case USB_DEVICE_ID_RAZER_BLACKWIDOW_ULTIMATE_2016:
	case USB_DEVICE_ID_RAZER_BLACKWIDOW_X_ULTIMATE:
	case USB_DEVICE_ID_RAZER_TARTARUS:
	case USB_DEVICE_ID_RAZER_TARTARUS_CHROMA:
	case USB_DEVICE_ID_RAZER_TARTARUS_V2:
	case USB_DEVICE_ID_RAZER_BLACKWIDOW_OVERWATCH:
	case USB_DEVICE_ID_RAZER_BLACKWIDOW_CHROMA:
	case USB_DEVICE_ID_RAZER_DEATHSTALKER_EXPERT:
	case USB_DEVICE_ID_RAZER_DEATHSTALKER_CHROMA:
	case USB_DEVICE_ID_RAZER_BLACKWIDOW_CHROMA_TE:
	case USB_DEVICE_ID_RAZER_BLACKWIDOW_X_CHROMA:
	case USB_DEVICE_ID_RAZER_BLACKWIDOW_X_CHROMA_TE:
	case USB_DEVICE_ID_RAZER_BLACKWIDOW_LITE:
	case USB_DEVICE_ID_RAZER_BLACKWIDOW_2019:
	case USB_DEVICE_ID_RAZER_BLACKWIDOW_ESSENTIAL:
	case USB_DEVICE_ID_RAZER_ORNATA:
	case USB_DEVICE_ID_RAZER_ORNATA_CHROMA:
	case USB_DEVICE_ID_RAZER_ORNATA_CHROMA_V2:
	case USB_DEVICE_ID_RAZER_HUNTSMAN_ELITE:
	case USB_DEVICE_ID_RAZER_HUNTSMAN_TE:
	case USB_DEVICE_ID_RAZER_BLACKWIDOW_ELITE:
	case USB_DEVICE_ID_RAZER_HUNTSMAN:
	case USB_DEVICE_ID_RAZER_CYNOSA_CHROMA:
    case USB_DEVICE_ID_RAZER_CYNOSA_CHROMA_PRO:
	case USB_DEVICE_ID_RAZER_BLACKWIDOW_CHROMA_V2:
	case USB_DEVICE_ID_RAZER_ANANSI:
	case USB_DEVICE_ID_RAZER_CYNOSA_V2:
	case USB_DEVICE_ID_RAZER_CYNOSA_LITE:
	case USB_DEVICE_ID_RAZER_BLACKWIDOW_V3:
	case USB_DEVICE_ID_RAZER_BLACKWIDOW_V3_TK:
    case USB_DEVICE_ID_RAZER_BLACKWIDOW_V3_PRO_WIRED:
	case USB_DEVICE_ID_RAZER_HUNTSMAN_MINI:
    case USB_DEVICE_ID_RAZER_HUNTSMAN_V2_ANALOG:
    case USB_DEVICE_ID_RAZER_HUNTSMAN_V2:
    case USB_DEVICE_ID_RAZER_HUNTSMAN_V2_TKL:
	case USB_DEVICE_ID_RAZER_BLACKWIDOW_V3_MINI_WIRED:
	case USB_DEVICE_ID_RAZER_BLACKWIDOW_V3_MINI_WIRELESS:
		return true;
	}

	return false;
}

bool is_mouse(IOUSBDeviceInterface **usb_dev)
{
	UInt16 product = -1;
	(*usb_dev)->GetDeviceProduct(usb_dev, &product);

	switch (product)
	{
	case USB_DEVICE_ID_RAZER_OROCHI_2011:
	case USB_DEVICE_ID_RAZER_DEATHADDER_3_5G:
	case USB_DEVICE_ID_RAZER_ABYSSUS_1800:
	case USB_DEVICE_ID_RAZER_MAMBA_2012_WIRED:
	case USB_DEVICE_ID_RAZER_MAMBA_2012_WIRELESS:
	case USB_DEVICE_ID_RAZER_NAGA_2012:
	case USB_DEVICE_ID_RAZER_IMPERATOR:
	case USB_DEVICE_ID_RAZER_OUROBOROS:
	case USB_DEVICE_ID_RAZER_TAIPAN:
	case USB_DEVICE_ID_RAZER_NAGA_HEX_RED:
	case USB_DEVICE_ID_RAZER_DEATHADDER_2013:
	case USB_DEVICE_ID_RAZER_DEATHADDER_1800:
	case USB_DEVICE_ID_RAZER_OROCHI_2013:
	case USB_DEVICE_ID_RAZER_NAGA_2014:
	case USB_DEVICE_ID_RAZER_NAGA_X:
	case USB_DEVICE_ID_RAZER_NAGA_HEX:
	case USB_DEVICE_ID_RAZER_ABYSSUS:
	case USB_DEVICE_ID_RAZER_DEATHADDER_CHROMA:
	case USB_DEVICE_ID_RAZER_MAMBA_WIRED:
	case USB_DEVICE_ID_RAZER_MAMBA_WIRELESS:
	case USB_DEVICE_ID_RAZER_MAMBA_TE_WIRED:
	case USB_DEVICE_ID_RAZER_OROCHI_CHROMA:
	case USB_DEVICE_ID_RAZER_DIAMONDBACK_CHROMA:
	case USB_DEVICE_ID_RAZER_NAGA_HEX_V2:
	case USB_DEVICE_ID_RAZER_NAGA_CHROMA:
	case USB_DEVICE_ID_RAZER_DEATHADDER_3500:
	case USB_DEVICE_ID_RAZER_LANCEHEAD_WIRED:
	case USB_DEVICE_ID_RAZER_LANCEHEAD_WIRELESS:
	case USB_DEVICE_ID_RAZER_ABYSSUS_V2:
	case USB_DEVICE_ID_RAZER_DEATHADDER_ELITE:
	case USB_DEVICE_ID_RAZER_ABYSSUS_2000:
	case USB_DEVICE_ID_RAZER_LANCEHEAD_TE_WIRED:
	case USB_DEVICE_ID_RAZER_BASILISK:
	case USB_DEVICE_ID_RAZER_BASILISK_ULTIMATE:
	case USB_DEVICE_ID_RAZER_BASILISK_ULTIMATE_RECEIVER:
	case USB_DEVICE_ID_RAZER_NAGA_TRINITY:
	case USB_DEVICE_ID_RAZER_NAGA_PRO_WIRELESS:
	case USB_DEVICE_ID_RAZER_NAGA_PRO_WIRED:
	case USB_DEVICE_ID_RAZER_ABYSSUS_ELITE_DVA_EDITION:
	case USB_DEVICE_ID_RAZER_ABYSSUS_ESSENTIAL:
	case USB_DEVICE_ID_RAZER_MAMBA_ELITE:
	case USB_DEVICE_ID_RAZER_DEATHADDER_ESSENTIAL:
	case USB_DEVICE_ID_RAZER_LANCEHEAD_WIRELESS_RECEIVER:
	case USB_DEVICE_ID_RAZER_LANCEHEAD_WIRELESS_WIRED:
	case USB_DEVICE_ID_RAZER_DEATHADDER_ESSENTIAL_WHITE_EDITION:
	case USB_DEVICE_ID_RAZER_MAMBA_WIRELESS_RECEIVER:
	case USB_DEVICE_ID_RAZER_MAMBA_WIRELESS_WIRED:
	case USB_DEVICE_ID_RAZER_VIPER:
	case USB_DEVICE_ID_RAZER_VIPER_8KHZ:
	case USB_DEVICE_ID_RAZER_VIPER_MINI:
	case USB_DEVICE_ID_RAZER_BASILISK_V2:
	case USB_DEVICE_ID_RAZER_VIPER_ULTIMATE_WIRED:
	case USB_DEVICE_ID_RAZER_VIPER_ULTIMATE_WIRELESS:
	case USB_DEVICE_ID_RAZER_DEATHADDER_V2:
	case USB_DEVICE_ID_RAZER_DEATHADDER_V2_PRO_WIRED:
    case USB_DEVICE_ID_RAZER_DEATHADDER_V2_PRO_WIRELESS:
	case USB_DEVICE_ID_RAZER_DEATHADDER_V2_MINI:
	case USB_DEVICE_ID_RAZER_NAGA_LEFT_HANDED_2020:
	case USB_DEVICE_ID_RAZER_ATHERIS_RECEIVER:
    case USB_DEVICE_ID_RAZER_DEATHADDER_ESSENTIAL_2021:
	case USB_DEVICE_ID_RAZER_BASILISK_V3:
	case USB_DEVICE_ID_RAZER_BASILISK_V3_PRO_WIRED:
    case USB_DEVICE_ID_RAZER_BASILISK_V3_PRO_WIRELESS:
	case USB_DEVICE_ID_RAZER_BASILISK_ESSENTIAL:
    case USB_DEVICE_ID_RAZER_OROCHI_V2_RECEIVER:
    case USB_DEVICE_ID_RAZER_OROCHI_V2_BLUETOOTH:
		return true;
	}

	return false;
}

bool is_mouse_dock(IOUSBDeviceInterface **usb_dev)
{
	UInt16 product = -1;
	(*usb_dev)->GetDeviceProduct(usb_dev, &product);

	switch (product)
	{
	case USB_DEVICE_ID_RAZER_MOUSE_CHARGING_DOCK:
		return true;
	}

	return false;
}

bool is_mouse_mat(IOUSBDeviceInterface **usb_dev)
{
	UInt16 product = -1;
	(*usb_dev)->GetDeviceProduct(usb_dev, &product);

	switch (product)
	{
	case USB_DEVICE_ID_RAZER_FIREFLY_HYPERFLUX:
	case USB_DEVICE_ID_RAZER_FIREFLY:
	case USB_DEVICE_ID_RAZER_FIREFLY_V2:
	case USB_DEVICE_ID_RAZER_GOLIATHUS_CHROMA:
	case USB_DEVICE_ID_RAZER_GOLIATHUS_CHROMA_EXTENDED:
		return true;
	}

	return false;
}

bool is_egpu(IOUSBDeviceInterface **usb_dev)
{
	UInt16 product = -1;
	(*usb_dev)->GetDeviceProduct(usb_dev, &product);

	switch (product)
	{
	case USB_DEVICE_ID_RAZER_CORE_X_CHROMA:
		return true;
	}

	return false;
}


bool is_headphone(IOUSBDeviceInterface **usb_dev) 
{
    UInt16 product = -1;
    (*usb_dev)->GetDeviceProduct(usb_dev, &product);

    switch (product) 
    {
        case USB_DEVICE_ID_RAZER_KRAKEN_KITTY_EDITION:
        case USB_DEVICE_ID_RAZER_KRAKEN_V2:
        case USB_DEVICE_ID_RAZER_KRAKEN_ULTIMATE:
            return true;
    }

    return false;
}

bool is_accessory(IOUSBDeviceInterface **usb_dev)
{
    UInt16 product = -1;
    (*usb_dev)->GetDeviceProduct(usb_dev, &product);

    switch (product)
    {
        case USB_DEVICE_ID_RAZER_NOMMO_CHROMA:
        case USB_DEVICE_ID_RAZER_NOMMO_PRO:
        case USB_DEVICE_ID_RAZER_CHROMA_MUG:
        case USB_DEVICE_ID_RAZER_CHROMA_BASE:
        case USB_DEVICE_ID_RAZER_CHROMA_HDK:
        case USB_DEVICE_ID_RAZER_MOUSE_BUNGEE_V3_CHROMA:
        case USB_DEVICE_ID_RAZER_BASE_STATION_V2_CHROMA:
        case USB_DEVICE_ID_RAZER_THUNDERBOLT_4_DOCK_CHROMA:
            return true;
    }

    return false;
}

IOUSBDeviceInterface **getRazerUSBDeviceInterface(int type)
{
	CFMutableDictionaryRef matchingDict;
	matchingDict = IOServiceMatching(kIOUSBDeviceClassName);
	if (matchingDict == NULL)
	{
		return NULL;
	}

	io_iterator_t iter;
	kern_return_t kReturn =
		IOServiceGetMatchingServices(MACH_PORT_NULL, matchingDict, &iter);
	if (kReturn != kIOReturnSuccess)
	{
		return NULL;
	}

	io_service_t usbDevice;
	while ((usbDevice = IOIteratorNext(iter)))
	{
		IOCFPlugInInterface **plugInInterface = NULL;
		SInt32 score;

		kReturn = IOCreatePlugInInterfaceForService(
			usbDevice, kIOUSBDeviceUserClientTypeID, kIOCFPlugInInterfaceID, &plugInInterface, &score);

		IOObjectRelease(usbDevice); // Not needed after plugin created
		if ((kReturn != kIOReturnSuccess) || plugInInterface == NULL)
		{
			// printf("Unable to create plugin (0x%08x)\n", kReturn);
			continue;
		}

		IOUSBDeviceInterface **dev = NULL;
		HRESULT hResult = (*plugInInterface)->QueryInterface(plugInInterface, CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID), (LPVOID *)&dev);

		(*plugInInterface)->Release(plugInInterface); // Not needed after device interface created
		if (hResult || !dev)
		{
			// printf("Couldn’t create a device interface (0x%08x)\n", (int) hResult);
			continue;
		}

		// Filter out non-Razer devices
		if (!is_razer_device(dev))
		{
			(*dev)->Release(dev);
			continue;
		}

		switch (type) 
        {
			case TYPE_KEYBOARD:
			case TYPE_BLADE:
				// Filter out non-keyboards and non-blade laptops
				if (!(is_keyboard(dev) || is_blade_laptop(dev))) 
                {
					(*dev)->Release(dev);
					continue;
				}
				break;
        
			case TYPE_MOUSE:
				// Filter out non-mice
				if (!is_mouse(dev)) 
                {
					(*dev)->Release(dev);
					continue;
				}
				break;
        
			case TYPE_MOUSE_DOCK:
				// Filter out non-mice-mats
				if (!is_mouse_dock(dev)) 
                {
					(*dev)->Release(dev);
					continue;
				}
				break;
        
			case TYPE_MOUSE_MAT:
				// Filter out non-mice-mats
				if (!is_mouse_mat(dev)) 
                {
					(*dev)->Release(dev);
					continue;
				}
				break;

 	        case TYPE_HEADPHONE:
                if (!is_headphone(dev))
                {
                  (*dev)->Release(dev);
                  continue;
                }
                break;

            case TYPE_EGPU:
			    // Filter out non-mice-mats
			    if (!is_egpu(dev))
			    {
				  (*dev)->Release(dev);
				  continue;
			    }
			  break;
            case TYPE_ACCESSORY:
                if (!is_accessory(dev))
                {
                    (*dev)->Release(dev);
                    continue;
                }
                break;
        
		    default:
			    // Unsupported Razer peripheral type
			    (*dev)->Release(dev);
			    continue;
		}

		kReturn = (*dev)->USBDeviceOpen(dev);
		if (kReturn != kIOReturnSuccess)
		{
			printf("Unable to open USB device: %08x\n", kReturn);
			(*dev)->Release(dev);
			continue;
		}

		// Success. We found the Razer USB device.
		// Caller is responsible for closing USB and release device.
		IOObjectRelease(iter);
		return dev;
	}

	IOObjectRelease(iter);
	return NULL;
}

void closeRazerUSBDeviceInterface(IOUSBDeviceInterface **dev)
{
	kern_return_t kr;
	kr = (*dev)->USBDeviceClose(dev);
	kr = (*dev)->Release(dev);
}

RazerDevices getAllRazerDevices()
{
    RazerDevices allDevices = { .devices = NULL, .size = 0 };

    CFMutableDictionaryRef matchingDict;
    matchingDict = IOServiceMatching(kIOUSBDeviceClassName);
    if (matchingDict == NULL)
    {
        return allDevices;
    }

    io_iterator_t iter;
    kern_return_t kReturn =
            IOServiceGetMatchingServices(MACH_PORT_NULL, matchingDict, &iter);
    if (kReturn != kIOReturnSuccess)
    {
        return allDevices;
    }

    int array_size = 0;
    int array_index = -1;
    RazerDevice *razerDevices = malloc(array_size * sizeof(RazerDevice));

    io_service_t usbDevice;
    while ((usbDevice = IOIteratorNext(iter)))
    {
        IOCFPlugInInterface **plugInInterface = NULL;
        SInt32 score;

        kReturn = IOCreatePlugInInterfaceForService(
                usbDevice, kIOUSBDeviceUserClientTypeID, kIOCFPlugInInterfaceID, &plugInInterface, &score);

        IOObjectRelease(usbDevice); // Not needed after plugin created
        if ((kReturn != kIOReturnSuccess) || plugInInterface == NULL)
        {
            // printf("Unable to create plugin (0x%08x)\n", kReturn);
            continue;
        }

        IOUSBDeviceInterface **dev = NULL;
        HRESULT hResult = (*plugInInterface)->QueryInterface(plugInInterface, CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID), (LPVOID *)&dev);

        (*plugInInterface)->Release(plugInInterface); // Not needed after device interface created
        if (hResult || !dev)
        {
            // printf("Couldn’t create a device interface (0x%08x)\n", (int) hResult);
            continue;
        }

        // Filter out non-Razer devices
        if (!is_razer_device(dev))
        {
            (*dev)->Release(dev);
            continue;
        }

        if(!is_keyboard(dev)
        && !is_blade_laptop(dev)
        && !is_mouse(dev)
        && !is_mouse_dock(dev)
        && !is_mouse_dock(dev)
        && !is_mouse_mat(dev)
        && !is_headphone(dev)
        && !is_egpu(dev)
        && !is_accessory(dev)
        ) {
            (*dev)->Release(dev);
            continue;
        }

        kReturn = (*dev)->USBDeviceOpen(dev);
        if (kReturn != kIOReturnSuccess)
        {
            printf("Unable to open USB device: %08x\n", kReturn);
            (*dev)->Release(dev);
            continue;
        }

        // Success. We found the Razer USB device.
        // Caller is responsible for closing USB and release device.
        array_size++;
        razerDevices = realloc(razerDevices, array_size * sizeof(RazerDevice));
        array_index++;

        UInt16 productId;
        (*dev)->GetDeviceProduct(dev, &productId);

        RazerDevice newDevice = { .usbDevice = dev, .internalDeviceId = array_index, .productId = productId };
        razerDevices[array_index] = newDevice;
    }

    IOObjectRelease(iter);
    allDevices.devices = razerDevices;
    allDevices.size = array_size;
    return allDevices;
}

void closeAllRazerDevices(RazerDevices devices)
{
    for(int counter=0; counter < devices.size; ++counter) {
        IOUSBDeviceInterface **dev = devices.devices[counter].usbDevice;
        (*dev)->USBDeviceClose(dev);
        (*dev)->Release(dev);
    }
    free(devices.devices);
    devices.devices = NULL;
}