summaryrefslogtreecommitdiffstats
path: root/librazer/util.c
blob: 9b8c0c23acea5181b96d6586ec293b1488d26a61 (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
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
/*
 *   Copyright (C) 2007-2010 Michael Buesch <m@bues.ch>
 *
 *   This program 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 2
 *   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.
 */

#include "util.h"
#include "razer_private.h"

#include <unistd.h>
#include <errno.h>
#include <ctype.h>
#include <stdarg.h>


void razer_free(void *ptr, size_t size)
{
	if (ptr) {
		memset(ptr, 0, size);
		free(ptr);
	}
}

char * razer_strsplit(char *str, char sep)
{
	char c;

	if (!str)
		return NULL;
	for (c = *str; c != '\0' && c != sep; c = *str)
		str++;
	if (c == sep) {
		*str = '\0';
		return str + 1;
	}

	return NULL;
}

int razer_split_tuple(const char *str, char sep,
		      size_t elems_max_len, ...)
{
	char *elem;
	va_list ap;
	int err = 0;

	if (!elems_max_len)
		return -EINVAL;
	if (strlen(str) >= elems_max_len)
		return -EINVAL;

	va_start(ap, elems_max_len);
	while (1) {
		elem = va_arg(ap, char *);
		if (!elem)
			break;
		elem[0] = '\0';
		if (!str) {
			err = -ENODATA;
			continue;
		}
		razer_strlcpy(elem, str, elems_max_len);
		str = razer_strsplit(elem, sep);
	}
	va_end(ap);

	return err;
}

int razer_string_to_int(const char *string, int *i)
{
	char *tailptr;
	long retval;

	retval = strtol(string, &tailptr, 0);
	if (tailptr == string || tailptr[0] != '\0')
		return -EINVAL;
	*i = retval;

	return 0;
}

int razer_string_to_bool(const char *string, bool *b)
{
	int i;

	if (strcasecmp(string, "yes") == 0 ||
	    strcasecmp(string, "true") == 0 ||
	    strcasecmp(string, "on") == 0) {
		*b = 1;
		return 0;
	}
	if (strcasecmp(string, "no") == 0 ||
	    strcasecmp(string, "false") == 0 ||
	    strcasecmp(string, "off") == 0) {
		*b = 0;
		return 0;
	}
	if (!razer_string_to_int(string, &i)) {
		*b = !!i;
		return 0;
	}

	return -EINVAL;
}

int razer_string_to_mode(const char *string, enum razer_led_mode *mode)
{
	if (strcasecmp(string, "static") == 0) {
		*mode = RAZER_LED_MODE_STATIC;
		return 0;
	}
	if (strcasecmp(string, "spectrum") == 0) {
		*mode = RAZER_LED_MODE_SPECTRUM;
		return 0;
	}
	if (strcasecmp(string, "breathing") == 0) {
		*mode = RAZER_LED_MODE_BREATHING;
		return 0;
	}
	if (strcasecmp(string, "wave") == 0) {
		*mode = RAZER_LED_MODE_WAVE;
		return 0;
	}
	if (strcasecmp(string, "reaction") == 0) {
		*mode = RAZER_LED_MODE_REACTION;
		return 0;
	}

	return -EINVAL;
}

int razer_string_to_color(const char *string, struct razer_rgb_color *color)
{
	uint32_t temp = (uint32_t)strtol(string, NULL, 16);

	color->r = (uint8_t)((temp >> 16) & 0xFF);
	color->g = (uint8_t)((temp >> 8) & 0xFF);
	color->b = (uint8_t)(temp & 0xFF);
	color->valid = 1;

	return 0;
}

char * razer_string_strip(char *str)
{
	char *start = str;
	size_t len;

	if (!str)
		return NULL;
	while (*start != '\0' && isspace(*start))
		start++;
	len = strlen(start);
	while (len && isspace(start[len - 1])) {
		start[len - 1] = '\0';
		len--;
	}

	return start;
}

void razer_strlcpy(char *dst, const char *src, size_t dst_size)
{
	size_t len;

	if (!dst_size)
		return;

	len = strlen(src);
	if (len >= dst_size)
		len = dst_size - 1;
	memcpy(dst, src, len);
	dst[len] = 0;
}

void razer_timeval_add_msec(struct timeval *tv, unsigned int msec)
{
	unsigned int seconds, usec;

	seconds = msec / 1000;
	msec = msec % 1000;
	usec = msec * 1000;

	tv->tv_usec += usec;
	while (tv->tv_usec >= 1000000) {
		tv->tv_sec++;
		tv->tv_usec -= 1000000;
	}
	tv->tv_sec += seconds;
}

/* Returns true, if a is after b. */
bool razer_timeval_after(const struct timeval *a, const struct timeval *b)
{
	if (a->tv_sec > b->tv_sec)
		return 1;
	if ((a->tv_sec == b->tv_sec) && (a->tv_usec > b->tv_usec))
		return 1;
	return 0;
}

/* Return a-b in milliseconds */
int razer_timeval_msec_diff(const struct timeval *a, const struct timeval *b)
{
	int64_t usec_a, usec_b, usec_diff;

	usec_a = (int64_t)a->tv_sec * 1000000;
	usec_a += (int64_t)a->tv_usec;

	usec_b = (int64_t)b->tv_sec * 1000000;
	usec_b += (int64_t)b->tv_usec;

	usec_diff = usec_a - usec_b;

	return usec_diff / 1000;
}

void razer_msleep(unsigned int msecs)
{
	int err;
	struct timespec time;

	time.tv_sec = 0;
	while (msecs >= 1000) {
		time.tv_sec++;
		msecs -= 1000;
	}
	time.tv_nsec = msecs;
	time.tv_nsec *= 1000000;
	do {
		err = nanosleep(&time, &time);
	} while (err && errno == EINTR);
	if (err) {
		razer_error("nanosleep() failed with: %s\n",
			strerror(errno));
	}
}

le16_t razer_xor16_checksum(const void *_buffer, size_t size)
{
	const uint8_t *buffer = _buffer;
	uint16_t sum = 0;
	size_t i;

	for (i = 0; i < size; i += 2) {
		sum ^= buffer[i];
		if (i < size - 1)
			sum ^= ((uint16_t)(buffer[i + 1])) << 8;
	}

	return cpu_to_le16(sum);
}

be16_t razer_xor16_checksum_be(const void *_buffer, size_t size)
{
	return (be16_t)bswap_16((uint16_t)razer_xor16_checksum(_buffer, size));
}

uint8_t razer_xor8_checksum(const void *_buffer, size_t size)
{
	const uint8_t *buffer = _buffer;
	uint8_t sum = 0;
	size_t i;

	for (i = 0; i < size; i++)
		sum ^= buffer[i];

	return sum;
}

static char razer_char_to_ascii(char c)
{
	if (c >= 32 && c <= 126)
		return c;
	return '.';
}

void razer_dump(const char *prefix, const void *_buf, size_t size)
{
	const unsigned char *buf = _buf;
	size_t i;
	char ascii[17] = { 0, };
	unsigned int ascii_idx = 0;

	for (i = 0; i < size; i++) {
		if (i % 16 == 0) {
			if (i != 0) {
				printf("  |%s|\n", ascii);
				memset(ascii, 0, sizeof(ascii));
				ascii_idx = 0;
			}
			printf("%s-[%04X]:  ", prefix, (unsigned int)i);
		}
		printf("%02X%s", buf[i], (i % 2) ? " " : "");
		ascii[ascii_idx++] = razer_char_to_ascii(buf[i]);
	}
	if (ascii[0]) {
		for (; i % 16; i++)
			printf((i % 2) ? "   " : "  ");
		printf("  |%s|", ascii);
	}
	printf("\n\n");
}

void razer_ascii_to_utf16(razer_utf16_t *dest, size_t dest_max_chars,
			  const char *src)
{
	size_t count = 0;

	if (!dest_max_chars)
		return;
	/* FIXME: This code is wrong.
	 * But it works for most strings in the current setup.
	 * So it probably won't blow up too often.
	 */
	while (count < dest_max_chars - 1) {
		if (!*src)
			break;
		*dest++ = *src++;
		count++;
	}
	*dest = 0;
}

int razer_utf16_cpy(razer_utf16_t *dest, const razer_utf16_t *src,
		    size_t max_chars)
{
	size_t i;

	for (i = 0; i < max_chars; i++, dest++, src++) {
		*dest = *src;
		if (!(*src))
			return 0;
	}

	return -ENOSPC;
}

size_t razer_utf16_strlen(const razer_utf16_t *str)
{
	size_t count = 0;

	while (*str) {
		str++;
		count++;
	}

	return count;
}
bues.ch cgit interface