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

pthread_mutex_t inflate_lock;

int update_current_folder (screen* folder);
int load_folder (); 
int load_key ();
int createreadthread (streamdeck* target);
void read_deck_stream (void* target);


int
init ()
{
    /*
     * Init the HIDAPI library
     */
    if (hid_init() < 0)
    {
	(void) fprintf(stderr, "Could not init the HID library\n");
	return -1;
    }

    /*
     * Initialize the lock for inflating the config file
     */
    if (pthread_mutex_init(&inflate_lock, NULL))
    {
	(void) fprintf(stderr, "Could not init lock\n");
	return -2;
    }

    return 0;
}

handler*
create_hid_handler ()
{
    handler* res = NULL;
    res = malloc(sizeof(handler));
    if (!res) {
	return NULL;
    }
    /*
     * Create the device handler
     */
    /* These are dev id and manufacturer id for the mini */
    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
     */
    /* We will likely only want to set this later, or maybe never. */
	//    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()
{
    streamdeck* res = malloc(sizeof(streamdeck));
    if (!res)
	return NULL;
    res->hid_handle = create_hid_handler ();
    if (1) /* Replace this with the actual ID of the different streamdecks */
    {
	screen* startscreen = malloc(sizeof(screen));
	startscreen->keys = malloc(sizeof(key) * STREAMDECK_MINI);
	for (int i = 0; i < STREAMDECK_MINI; i++)
	{
	    /* Upon second thought, this may actually not be necessary */
	    pthread_mutex_init(&(startscreen->keys + i)->key_mutex , NULL);
	}
	res->curr_screen = startscreen;
    }
    /* We will either to this before or after we initialize the streamdeck struct */
    if (resetkeystream(res->hid_handle) != 0)
    {
	fprintf (stderr, "Failed to reset key stream!\n");
    }
    return res;
}

int
createreadthread(streamdeck* target)
{
    if (pthread_create(pthread_t *restrict newthread, NULL, read_deck_stream, target))
	return 1;
    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) < 0)
    {
	printf("Unable to write()/2: %ls\n", hid_error(deck->handler));
	return -1;
    }
    return 0;
}

/* Clears the buttons and displays "standby image" */
int
reset(handler* deck)
{
    unsigned char data[17];
    if (set_data(data) == -1)
	return -2;
    data[0] = 0x0B;
    data[1] = 0x63;
    if (hid_send_feature_report(deck->handler, data, 17) == -1)
	return -1;
    return 0;
}

int
set_key_image(streamdeck* deck, int key, image* img)
{
    if (!(0 <= key && key < deck->numkeys))
	return -1;
    /* Pass NULL for blank key */
    if (!img)
    {
    }

    size_t remaining = img->size;
    int pages = 0;
    unsigned char data[17];
    while (remaining > 0)
    {
	size_t sent_len = (remaining > IMAGE_REPORT_LENGTH) ? IMAGE_REPORT_LENGTH : remaining;
	size_t total_sent = pages * IMAGE_REPORT_LENGTH;

	if (set_data(data) == -1)
	    return -1;

	data[0] = 0x02;
	data[1] = 0x01;
	data[2] = pages;
	data[4] = (remaining == sent_len) ? 1 : 0;
	data[5] = key + 1;

	unsigned char* payload = calloc (17 + sent_len, sizeof (unsigned char));
	memcpy(payload, data, 17);
	memcpy(payload + 17, img->data + total_sent, sent_len);

	if (hid_write(deck->hid_handle->handler, payload, IMAGE_REPORT_LENGTH + 17) == -1)
	    return -1;

	remaining = remaining - sent_len;
	pages++;
    }
}

int
update_current_folder (screen* folder)
{
    for (int i = 0; i < STREAMDECK_MINI; i++ )
	{
	    ;
	}
    return 0;
}

int
load_folder ()
{
}

int
load_key ()
{
}

void
destroy_streamdeck (streamdeck* target)
{
    hid_close (target->hid_handle->handler);
    free (target->hid_handle);
    free (target->curr_screen);
    free (target);
}

void
clean_exit ()
{
    hid_exit ();
}

/* Eventually want to change this to return the old
 * brightness level */
int
set_brightness (streamdeck* deck, int new_level)
{
    new_level = (new_level > 100) ? 100 : new_level;
    unsigned char data[17];
    if (set_data(data) == -1)
	return -2;
    data[0] = 0x05;
    data[1] = 0x55;
    data[2] = 0xaa;
    data[3] = 0xd1;
    data[4] = 0x01;
    data[5] = new_level;
    if (hid_send_feature_report(deck->hid_handle->handler, data, 17) == -1)
	return -1;
    return 0;
}

void
read_deck_stream (void* target)
{
    streamdeck* deck = (streamdeck*) target;
    if (!deck)
	return;
}