-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathds4.c
159 lines (133 loc) · 3.83 KB
/
ds4.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
/* Copyright (C) 2020 effective-light
This file is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not see
<http://www.gnu.org/licenses/>. */
#include <errno.h>
#include <fcntl.h>
#include <libudev.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SONY_VID 0x054c
#define DELIM ":"
#define SIZE 32
/*
buf[4] // rumble min
buf[5] // rumble max
buf[6] // r
buf[7] // g
buf[8] // b
buf[9] // time to flash bright
buf[10] // time to flash dark
*/
unsigned char parse_num(char *str) {
char *endptr;
unsigned char num = strtol(str, &endptr, 10);
if (endptr[0] != '\0') {
printf("Invalid input\n");
exit(EXIT_FAILURE);
}
return num;
}
int main(int argc, char **argv) {
int opt = 0;
int v2 = 0;
int ret = 1;
unsigned char r = 0;
unsigned char g = 0;
unsigned char b = 0;
while ((opt = getopt(argc, argv, "vr:g:b:")) != -1) {
switch (opt) {
case 'v':
v2 = 0x01;
break;
case 'r':
r = parse_num(optarg);
break;
case 'g':
g = parse_num(optarg);
break;
case 'b':
b = parse_num(optarg);
break;
default:
fprintf(stderr, "Usage: %s [-v] [-r c] [-g c] [-b c]\n",
argv[0]);
exit(EXIT_FAILURE);
}
}
int pid;
if (v2) {
pid = 0x09cc; // ds4v2
} else {
pid = 0x05c4; // original ds4
}
struct udev *ctx = udev_new();
if (!ctx) {
fprintf(stderr, "error with udev!\n");
exit(EXIT_FAILURE);
}
struct udev_enumerate *iter = udev_enumerate_new(ctx);
udev_enumerate_add_match_subsystem(iter, "hidraw");
udev_enumerate_scan_devices(iter);
struct udev_list_entry *entry;
struct udev_device *dev = NULL;
udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(iter)) {
const char *path = udev_list_entry_get_name(entry);
dev = udev_device_new_from_syspath(ctx, path);
if (!dev) {
continue;
}
struct udev_device *hid_parent =
udev_device_get_parent_with_subsystem_devtype(dev, "hid", NULL);
if (!hid_parent) {
goto DEV_END;
}
char *id = strdup(udev_device_get_property_value(hid_parent, "HID_ID"));
strtok(id, DELIM);
int vid = strtol(strtok(NULL, DELIM), NULL, 16);
int tmp_pid = strtol(strtok(NULL, DELIM), NULL, 16);
free(id);
if (vid == SONY_VID && tmp_pid == pid) {
break;
}
DEV_END:
udev_device_unref(dev);
dev = NULL;
}
if (!dev) {
fprintf(stderr, "unable to find the device!\n");
goto CLEANUP;
}
int handle = open(udev_device_get_devnode(dev), O_WRONLY);
if (handle == -1) {
perror("open");
goto CLEANUP;
}
unsigned char buf[SIZE];
memset(buf, 0, SIZE);
buf[0] = 0x05; // report id
buf[1] = 0xFF;
buf[6] = r;
buf[7] = g;
buf[8] = b;
if (write(handle, buf, SIZE) == -1) {
perror("write");
}
close(handle);
ret = 0;
CLEANUP:
udev_device_unref(dev);
udev_enumerate_unref(iter);
udev_unref(ctx);
return ret;
}