summaryrefslogtreecommitdiff
path: root/streamdeck.c
blob: 4ffe719e62953f84b3be66e810a9ad6384628f34 (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
#include "streamdeck.h"
#include <hidapi/hidapi.h>


struct Image {
    void* data;
    size_t size;
};

struct Key {
    bool isFolder;
    void* command;
    image* key;
};

struct Screen {
    key* keys;
};

struct Handler {
    hid_device* handler;
};

struct Streamdeck {
    handler* hid_handle;
};

handler* create_hid_handler() {
    handler* res = NULL;
    res = malloc(sizeof(handler));
    if (!res) {
	return NULL;
    }
    
    /*
     * Init the HIDAPI library
     */
    if (hid_init() < 0) {
	(void) fprintf(stderr, "Could not init the HID library\n");
	free(res);
	return NULL;
    }

    /*
     * Create the device handler
     */
    res->handler = hid_open(0x0fd9, 0x0063, NULL);
    if (!res->handler) {
	(void) fprintf(stderr, "Could not open Streamdeck!\n");
	free(res);
	hid_exit();
	return NULL;
    }

    /*
     * Set handler to be non-blocking
     */

    if (hid_set_nonblocking(res->handler, 1) < 0) {
	(void) fprintf(stderr, "Could not set HIDAPI handler to be non-blocking\n");
	free(res);
	hid_exit();
	return NULL;
    }

    return res;
}

streamdeck* connect(handler* deck)
{
    streamdeck* res = malloc(sizeof(streamdeck));
    res->hid_handle = deck;
    if (hid_open(unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number))
    {}
    if (resetkeystream(res->hid_handle) != 0)
    {}
    return res;
}

int close()
{}

int createreadthread()
{
    return 0;
}

int resetkeystream(handler* deck)
{
    unsigned char data[IMAGE_REPORT_LENGTH]; 
    if (!memset(data, 0x00, IMAGE_REPORT_LENGTH))
	return -2;
    data[0] = 0x02;
    if (hid_write(deck->handler, data, IMAGE_REPORT_LENGTH) == -1)
	return -1;
    return 0;
}

int reset(handler* deck)
{
    unsigned char data[17];
    if (!memset(data, 0x00, 17))
	return -2;
    data[0] = 0x0B;
    data[1] = 0x63;
    if (hid_send_feature_report(deck->handler, data, 17) == -1)
	return -1;
    return 0;
}