-
Notifications
You must be signed in to change notification settings - Fork 0
/
ppd.c
58 lines (47 loc) · 1.37 KB
/
ppd.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
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <xdo.h>
#include <pulse/simple.h>
#include <pulse/error.h>
#define THRESHOLD 30
#define SAMPLE_RATE 1000
#define BUFSIZE 20
int main() {
static const pa_sample_spec sample_spec = {
.format = PA_SAMPLE_U8,
.rate = SAMPLE_RATE,
.channels = 2
};
pa_simple *pulse_socket = NULL;
int error;
if (!(pulse_socket = pa_simple_new(NULL, "piano-pedal-driver", PA_STREAM_RECORD, NULL, "record", &sample_spec, NULL, NULL, &error))) {
fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));
return 1;
}
uint8_t buffer[BUFSIZE];
xdo_t *xdo = xdo_new(NULL);
int ctrl_on = 0;
while (1) {
if (pa_simple_read(pulse_socket, buffer, BUFSIZE, &error) < 0) {
fprintf(stderr, __FILE__": pa_simple_read() failed: %s\n", pa_strerror(error));
pa_simple_free(pulse_socket);
return 1;
}
for (int i = 1; i < BUFSIZE; i += 2) {
if (buffer[i] - 128 > THRESHOLD && !ctrl_on) {
printf("on\n");
ctrl_on = 1;
xdo_send_keysequence_window_down(xdo, CURRENTWINDOW, "37", 0);
} else if (buffer[i] - 128 < - THRESHOLD && ctrl_on) {
printf("off\n");
ctrl_on = 0;
xdo_send_keysequence_window_up(xdo, CURRENTWINDOW, "37", 0);
}
}
}
if (pulse_socket) {
pa_simple_free(pulse_socket);
}
return 0;
}