-
Notifications
You must be signed in to change notification settings - Fork 223
/
unixlib.c
529 lines (442 loc) · 15.1 KB
/
unixlib.c
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/*
* Copyright 2021 Rémi Bernon for CodeWeavers
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#if 0
#pragma makedep unix
#endif
#include "config.h"
#include <stdarg.h>
#include <stdlib.h>
#include "ntstatus.h"
#define WIN32_NO_STATUS
#include "windef.h"
#include "winbase.h"
#include "winternl.h"
#include "ddk/hidtypes.h"
#include "wine/debug.h"
#include "wine/list.h"
#include "wine/unixlib.h"
#include "unix_private.h"
BOOL is_wine_blacklisted(WORD vid, WORD pid)
{
if (vid == 0x056a) return TRUE; /* all Wacom devices */
return FALSE;
}
/* logic from SDL2's SDL_ShouldIgnoreGameController */
BOOL is_sdl_blacklisted(WORD vid, WORD pid)
{
const char *allow_virtual = getenv("SDL_GAMECONTROLLER_ALLOW_STEAM_VIRTUAL_GAMEPAD");
const char *whitelist = getenv("SDL_GAMECONTROLLER_IGNORE_DEVICES_EXCEPT");
const char *blacklist = getenv("SDL_GAMECONTROLLER_IGNORE_DEVICES");
char needle[16];
if (vid == 0x28de && pid == 0x11ff && allow_virtual && *allow_virtual &&
*allow_virtual != '0' && strcasecmp(allow_virtual, "false"))
return FALSE;
sprintf(needle, "0x%04x/0x%04x", vid, pid);
if (whitelist) return strcasestr(whitelist, needle) == NULL;
if (blacklist) return strcasestr(blacklist, needle) != NULL;
return FALSE;
}
BOOL is_dualshock4_gamepad(WORD vid, WORD pid)
{
if (vid != 0x054c) return FALSE;
if (pid == 0x05c4) return TRUE; /* DualShock 4 [CUH-ZCT1x] */
if (pid == 0x09cc) return TRUE; /* DualShock 4 [CUH-ZCT2x] */
if (pid == 0x0ba0) return TRUE; /* Dualshock 4 Wireless Adaptor */
return FALSE;
}
BOOL is_dualsense_gamepad(WORD vid, WORD pid)
{
if (vid == 0x054c && pid == 0x0ce6) return TRUE;
return FALSE;
}
BOOL is_logitech_g920(WORD vid, WORD pid)
{
return vid == 0x046D && pid == 0xC262;
}
static BOOL is_thrustmaster_hotas(WORD vid, WORD pid)
{
return vid == 0x044F && (pid == 0xB679 || pid == 0xB687 || pid == 0xB10A);
}
static BOOL is_simucube_wheel(WORD vid, WORD pid)
{
if (vid != 0x16D0) return FALSE;
if (pid == 0x0D61) return TRUE; /* Simucube 2 Sport */
if (pid == 0x0D60) return TRUE; /* Simucube 2 Pro */
if (pid == 0x0D5F) return TRUE; /* Simucube 2 Ultimate */
if (pid == 0x0D5A) return TRUE; /* Simucube 1 */
return FALSE;
}
static BOOL is_fanatec_pedals(WORD vid, WORD pid)
{
if (vid != 0x0EB7) return FALSE;
if (pid == 0x183B) return TRUE; /* Fanatec ClubSport Pedals v3 */
if (pid == 0x1839) return TRUE; /* Fanatec ClubSport Pedals v1/v2 */
return FALSE;
}
static BOOL is_vkb_controller(WORD vid, WORD pid, INT buttons)
{
if (vid != 0x231D) return FALSE;
/* comes with 128 buttons in the default configuration */
if (buttons == 128) return TRUE;
/* if customized, less than 128 buttons may be shown, decide by PID */
if (pid == 0x0200) return TRUE; /* VKBsim Gladiator EVO Right Grip */
if (pid == 0x0201) return TRUE; /* VKBsim Gladiator EVO Left Grip */
return FALSE;
}
static BOOL is_virpil_controller(WORD vid, WORD pid, INT buttons)
{
if (vid != 0x3344) return FALSE;
/* comes with 31 buttons in the default configuration, or 128 max */
if ((buttons == 31) || (buttons == 128)) return TRUE;
/* if customized, arbitrary amount of buttons may be shown, decide by PID */
if (pid == 0x412f) return TRUE; /* Virpil Constellation ALPHA-R */
return FALSE;
}
BOOL is_hidraw_enabled(WORD vid, WORD pid, INT axes, INT buttons)
{
const char *enabled = getenv("PROTON_ENABLE_HIDRAW");
char needle[16];
if (is_dualshock4_gamepad(vid, pid)) return TRUE;
if (is_dualsense_gamepad(vid, pid)) return TRUE;
if (is_thrustmaster_hotas(vid, pid)) return TRUE;
if (is_simucube_wheel(vid, pid)) return TRUE;
if (is_fanatec_pedals(vid, pid)) return TRUE;
if (is_vkb_controller(vid, pid, buttons)) return TRUE;
if (is_virpil_controller(vid, pid, buttons)) return TRUE;
sprintf(needle, "0x%04x/0x%04x", vid, pid);
if (enabled) return strcasestr(enabled, needle) != NULL;
return FALSE;
}
struct mouse_device
{
struct unix_device unix_device;
};
static void mouse_destroy(struct unix_device *iface)
{
}
static NTSTATUS mouse_start(struct unix_device *iface)
{
const USAGE_AND_PAGE device_usage = {.UsagePage = HID_USAGE_PAGE_GENERIC, .Usage = HID_USAGE_GENERIC_MOUSE};
if (!hid_device_begin_report_descriptor(iface, &device_usage))
return STATUS_NO_MEMORY;
if (!hid_device_add_buttons(iface, HID_USAGE_PAGE_BUTTON, 1, 3))
return STATUS_NO_MEMORY;
if (!hid_device_end_report_descriptor(iface))
return STATUS_NO_MEMORY;
return STATUS_SUCCESS;
}
static void mouse_stop(struct unix_device *iface)
{
}
static NTSTATUS mouse_haptics_start(struct unix_device *iface, UINT duration,
USHORT rumble_intensity, USHORT buzz_intensity,
USHORT left_intensity, USHORT right_intensity)
{
return STATUS_NOT_SUPPORTED;
}
static NTSTATUS mouse_haptics_stop(struct unix_device *iface)
{
return STATUS_NOT_SUPPORTED;
}
static NTSTATUS mouse_physical_device_control(struct unix_device *iface, USAGE control)
{
return STATUS_NOT_SUPPORTED;
}
static NTSTATUS mouse_physical_device_set_gain(struct unix_device *iface, BYTE percent)
{
return STATUS_NOT_SUPPORTED;
}
static NTSTATUS mouse_physical_effect_control(struct unix_device *iface, BYTE index,
USAGE control, BYTE iterations)
{
return STATUS_NOT_SUPPORTED;
}
static NTSTATUS mouse_physical_effect_update(struct unix_device *iface, BYTE index,
struct effect_params *params)
{
return STATUS_NOT_SUPPORTED;
}
static const struct hid_device_vtbl mouse_vtbl =
{
mouse_destroy,
mouse_start,
mouse_stop,
mouse_haptics_start,
mouse_haptics_stop,
mouse_physical_device_control,
mouse_physical_device_set_gain,
mouse_physical_effect_control,
mouse_physical_effect_update,
};
static const struct device_desc mouse_device_desc =
{
.vid = 0x845e,
.pid = 0x0001,
.input = -1,
.manufacturer = {'T','h','e',' ','W','i','n','e',' ','P','r','o','j','e','c','t',0},
.product = {'W','i','n','e',' ','H','I','D',' ','m','o','u','s','e',0},
.serialnumber = {'0','0','0','0',0},
};
static NTSTATUS mouse_device_create(void *args)
{
struct device_create_params *params = args;
params->desc = mouse_device_desc;
params->device = (UINT_PTR)hid_device_create(&mouse_vtbl, sizeof(struct mouse_device));
return STATUS_SUCCESS;
}
struct keyboard_device
{
struct unix_device unix_device;
};
static void keyboard_destroy(struct unix_device *iface)
{
}
static NTSTATUS keyboard_start(struct unix_device *iface)
{
const USAGE_AND_PAGE device_usage = {.UsagePage = HID_USAGE_PAGE_GENERIC, .Usage = HID_USAGE_GENERIC_KEYBOARD};
if (!hid_device_begin_report_descriptor(iface, &device_usage))
return STATUS_NO_MEMORY;
if (!hid_device_add_buttons(iface, HID_USAGE_PAGE_KEYBOARD, 0, 101))
return STATUS_NO_MEMORY;
if (!hid_device_end_report_descriptor(iface))
return STATUS_NO_MEMORY;
return STATUS_SUCCESS;
}
static void keyboard_stop(struct unix_device *iface)
{
}
static NTSTATUS keyboard_haptics_start(struct unix_device *iface, UINT duration,
USHORT rumble_intensity, USHORT buzz_intensity,
USHORT left_intensity, USHORT right_intensity)
{
return STATUS_NOT_SUPPORTED;
}
static NTSTATUS keyboard_haptics_stop(struct unix_device *iface)
{
return STATUS_NOT_SUPPORTED;
}
static NTSTATUS keyboard_physical_device_control(struct unix_device *iface, USAGE control)
{
return STATUS_NOT_SUPPORTED;
}
static NTSTATUS keyboard_physical_device_set_gain(struct unix_device *iface, BYTE percent)
{
return STATUS_NOT_SUPPORTED;
}
static NTSTATUS keyboard_physical_effect_control(struct unix_device *iface, BYTE index,
USAGE control, BYTE iterations)
{
return STATUS_NOT_SUPPORTED;
}
static NTSTATUS keyboard_physical_effect_update(struct unix_device *iface, BYTE index,
struct effect_params *params)
{
return STATUS_NOT_SUPPORTED;
}
static const struct hid_device_vtbl keyboard_vtbl =
{
keyboard_destroy,
keyboard_start,
keyboard_stop,
keyboard_haptics_start,
keyboard_haptics_stop,
keyboard_physical_device_control,
keyboard_physical_device_set_gain,
keyboard_physical_effect_control,
keyboard_physical_effect_update,
};
static const struct device_desc keyboard_device_desc =
{
.vid = 0x845e,
.pid = 0x0002,
.input = -1,
.manufacturer = {'T','h','e',' ','W','i','n','e',' ','P','r','o','j','e','c','t',0},
.product = {'W','i','n','e',' ','H','I','D',' ','k','e','y','b','o','a','r','d',0},
.serialnumber = {'0','0','0','0',0},
};
static NTSTATUS keyboard_device_create(void *args)
{
struct device_create_params *params = args;
params->desc = keyboard_device_desc;
params->device = (UINT_PTR)hid_device_create(&keyboard_vtbl, sizeof(struct keyboard_device));
return STATUS_SUCCESS;
}
void *raw_device_create(const struct raw_device_vtbl *vtbl, SIZE_T size)
{
struct unix_device *iface;
if (!(iface = calloc(1, size))) return NULL;
iface->vtbl = vtbl;
iface->ref = 1;
return iface;
}
static void unix_device_decref(struct unix_device *iface)
{
if (!InterlockedDecrement(&iface->ref))
{
iface->vtbl->destroy(iface);
free(iface);
}
}
static ULONG unix_device_incref(struct unix_device *iface)
{
return InterlockedIncrement(&iface->ref);
}
static NTSTATUS unix_device_remove(void *args)
{
struct device_remove_params *params = args;
struct unix_device *iface = (struct unix_device *)(UINT_PTR)params->device;
iface->vtbl->stop(iface);
unix_device_decref(iface);
return STATUS_SUCCESS;
}
static NTSTATUS unix_device_start(void *args)
{
struct device_start_params *params = args;
struct unix_device *iface = (struct unix_device *)(UINT_PTR)params->device;
return iface->vtbl->start(iface);
}
static NTSTATUS unix_device_get_report_descriptor(void *args)
{
struct device_descriptor_params *params = args;
struct unix_device *iface = (struct unix_device *)(UINT_PTR)params->device;
return iface->vtbl->get_report_descriptor(iface, params->buffer, params->length, params->out_length);
}
static NTSTATUS unix_device_set_output_report(void *args)
{
struct device_report_params *params = args;
struct unix_device *iface = (struct unix_device *)(UINT_PTR)params->device;
iface->vtbl->set_output_report(iface, params->packet, params->io);
return STATUS_SUCCESS;
}
static NTSTATUS unix_device_get_feature_report(void *args)
{
struct device_report_params *params = args;
struct unix_device *iface = (struct unix_device *)(UINT_PTR)params->device;
iface->vtbl->get_feature_report(iface, params->packet, params->io);
return STATUS_SUCCESS;
}
static NTSTATUS unix_device_set_feature_report(void *args)
{
struct device_report_params *params = args;
struct unix_device *iface = (struct unix_device *)(UINT_PTR)params->device;
iface->vtbl->set_feature_report(iface, params->packet, params->io);
return STATUS_SUCCESS;
}
const unixlib_entry_t __wine_unix_call_funcs[] =
{
sdl_bus_init,
sdl_bus_wait,
sdl_bus_stop,
udev_bus_init,
udev_bus_wait,
udev_bus_stop,
iohid_bus_init,
iohid_bus_wait,
iohid_bus_stop,
mouse_device_create,
keyboard_device_create,
unix_device_remove,
unix_device_start,
unix_device_get_report_descriptor,
unix_device_set_output_report,
unix_device_get_feature_report,
unix_device_set_feature_report,
};
void bus_event_cleanup(struct bus_event *event)
{
struct unix_device *iface = (struct unix_device *)(UINT_PTR)event->device;
if (event->type == BUS_EVENT_TYPE_NONE) return;
unix_device_decref(iface);
}
struct bus_event_entry
{
struct list entry;
struct bus_event event;
};
void bus_event_queue_destroy(struct list *queue)
{
struct bus_event_entry *entry, *next;
LIST_FOR_EACH_ENTRY_SAFE(entry, next, queue, struct bus_event_entry, entry)
{
bus_event_cleanup(&entry->event);
list_remove(&entry->entry);
free(entry);
}
}
BOOL bus_event_queue_device_removed(struct list *queue, struct unix_device *device)
{
ULONG size = sizeof(struct bus_event_entry);
struct bus_event_entry *entry = malloc(size);
if (!entry) return FALSE;
if (unix_device_incref(device) == 1) /* being destroyed */
{
free(entry);
return FALSE;
}
entry->event.type = BUS_EVENT_TYPE_DEVICE_REMOVED;
entry->event.device = (UINT_PTR)device;
list_add_tail(queue, &entry->entry);
return TRUE;
}
BOOL bus_event_queue_device_created(struct list *queue, struct unix_device *device, struct device_desc *desc)
{
ULONG size = sizeof(struct bus_event_entry);
struct bus_event_entry *entry = malloc(size);
if (!entry) return FALSE;
if (unix_device_incref(device) == 1) /* being destroyed */
{
free(entry);
return FALSE;
}
entry->event.type = BUS_EVENT_TYPE_DEVICE_CREATED;
entry->event.device = (UINT_PTR)device;
entry->event.device_created.desc = *desc;
list_add_tail(queue, &entry->entry);
return TRUE;
}
BOOL bus_event_queue_input_report(struct list *queue, struct unix_device *device, BYTE *report, USHORT length)
{
ULONG size = offsetof(struct bus_event_entry, event.input_report.buffer[length]);
struct bus_event_entry *entry = malloc(size);
if (!entry) return FALSE;
if (unix_device_incref(device) == 1) /* being destroyed */
{
free(entry);
return FALSE;
}
entry->event.type = BUS_EVENT_TYPE_INPUT_REPORT;
entry->event.device = (UINT_PTR)device;
entry->event.input_report.length = length;
memcpy(entry->event.input_report.buffer, report, length);
list_add_tail(queue, &entry->entry);
return TRUE;
}
BOOL bus_event_queue_pop(struct list *queue, struct bus_event *event)
{
struct list *head = list_head(queue);
struct bus_event_entry *entry;
ULONG size;
if (!head) return FALSE;
entry = LIST_ENTRY(head, struct bus_event_entry, entry);
list_remove(&entry->entry);
if (entry->event.type != BUS_EVENT_TYPE_INPUT_REPORT) size = sizeof(entry->event);
else size = offsetof(struct bus_event, input_report.buffer[entry->event.input_report.length]);
memcpy(event, &entry->event, size);
free(entry);
return TRUE;
}