-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
234 lines (217 loc) · 8.04 KB
/
main.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
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
/*
* Copyright 2021 paulguy <[email protected]>
*
* This file is part of gmmkctl.
*
* gmmkctl 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.
*
* gmmkctl 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 gmmkctl. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "gmmk.h"
unsigned int setBits(unsigned int bits);
int readNumber();
int main(int argc, char **argv) {
int i;
unsigned int j;
GMMKState *s;
unsigned int argInt1, argInt2, argInt3;
GMMKColor c[GMMK_MAX_KEY];
unsigned int activeDevs;
unsigned int devMask;
for (i = 1; i < argc; i++){
if(strcmp(argv[i], "help") == 0) {
char *help = "Commands:\n"
" help - Shows this list of commands.\n"
" devmask <mask> - Set the mask of detected devices which should be controlled by the following commands. By default, all are selected.\n"
" mode <num> - Set the mode. 20 is the mode for freely programming the keys, and the highest meaningful value.\n"
" brightness <num> - Set the brightness. 0 - 4 are meaningful.\n"
" delay <num> - Set the delay between animation frames. Very large values seem to be meaningful. 0 - 255\n"
" left - Set animation to proceed towards the left.\n"
" right - Set animation to proceed towards the right.\n"
" colorful - Turn on 'colorful' mode.\n"
" single - Turn on single color (not colorful) mode.\n"
" rate - Adjust polling rate. Not really tested. 0:125, 1:250, 2:500, 3:1000, 4+:???\n"
" color <r> <g> <b> - Set color for single color mode. 0 - 255\n"
" keys - Read in human readable numbers from standard input and set key colors.\n"
" Values may be separated by any whitespace. Seems to work by feeding in a\n"
" file from standard input or with a here word/document.\n"
" first value - Offset key to start programming colors.\n"
" second value - Number of values to follow.\n"
" N additional triplets - R, G and B values for each key color. 0 - 255\n";
fprintf(stderr, help);
exit (EXIT_SUCCESS);
}
}
s = gmmk_open();
if(s == NULL) {
fprintf(stderr, "Failed to open any device.\n");
exit(EXIT_SUCCESS);
}
fprintf(stderr, "Found %u devices.\n", s->devices);
activeDevs = devMask = setBits(s->devices);
for(i = 1; i < argc; i++) {
if(strcmp(argv[i], "devmask") == 0) {
if(i < argc - 1) {
i++;
devMask = atoi(argv[i]);
if((activeDevs & devMask & activeDevs) == 0) {
fprintf(stderr, "WARNING: No devices selected!\n");
}
}
} else if(strcmp(argv[i], "mode") == 0) {
if(i < argc - 1) {
i++;
argInt1 = atoi(argv[i]);
for(j = 0; j < GMMK_MAX_DEVS; j++) {
if(activeDevs & devMask & (1 << j)) {
gmmk_setMode(s, j, argInt1);
}
}
}
} else if(strcmp(argv[i], "brightness") == 0) {
if(i < argc - 1) {
i++;
argInt1 = atoi(argv[i]);
for(j = 0; j < GMMK_MAX_DEVS; j++) {
if(activeDevs & devMask & (1 << j)) {
gmmk_setBrightness(s, j, argInt1);
}
}
}
} else if(strcmp(argv[i], "delay") == 0) {
if(i < argc - 1) {
i++;
argInt1 = atoi(argv[i]);
for(j = 0; j < GMMK_MAX_DEVS; j++) {
if(activeDevs & devMask & (1 << j)) {
gmmk_setDelay(s, j, argInt1);
}
}
}
} else if(strcmp(argv[i], "left") == 0) {
for(j = 0; j < GMMK_MAX_DEVS; j++) {
if(activeDevs & devMask & (1 << j)) {
gmmk_setDirLeft(s, j);
}
}
} else if(strcmp(argv[i], "right") == 0) {
for(j = 0; j < GMMK_MAX_DEVS; j++) {
if(activeDevs & devMask & (1 << j)) {
gmmk_setDirRight(s, j);
}
}
} else if(strcmp(argv[i], "colorful") == 0) {
for(j = 0; j < GMMK_MAX_DEVS; j++) {
if(activeDevs & devMask & (1 << j)) {
gmmk_setColorful(s, j);
}
}
} else if(strcmp(argv[i], "single") == 0) {
for(j = 0; j < GMMK_MAX_DEVS; j++) {
if(activeDevs & devMask & (1 << j)) {
gmmk_unsetColorful(s, j);
}
}
} else if(strcmp(argv[i], "rate") == 0) {
if(i < argc - 1) {
i++;
argInt1 = atoi(argv[i]);
for(j = 0; j < GMMK_MAX_DEVS; j++) {
if(activeDevs & devMask & (1 << j)) {
gmmk_setRate(s, j, argInt1);
}
}
}
} else if(strcmp(argv[i], "color") == 0) {
if(i < argc - 3) {
i++;
argInt1 = atoi(argv[i]);
i++;
argInt2 = atoi(argv[i]);
i++;
argInt3 = atoi(argv[i]);
for(j = 0; j < GMMK_MAX_DEVS; j++) {
if(activeDevs & devMask & (1 << j)) {
gmmk_setAnimationColor(s, j, argInt1, argInt2, argInt3);
}
}
}
} else if(strcmp(argv[i], "keys") == 0) {
argInt1 = readNumber();
argInt2 = readNumber();
if(argInt1 + argInt2 > GMMK_MAX_KEY) {
fprintf(stderr, "Count too high.\n");
break;
}
for(j = 0; j < argInt2; j++) {
c[j].r = readNumber();
c[j].g = readNumber();
c[j].b = readNumber();
}
for(j = 0; j < GMMK_MAX_DEVS; j++) {
if(activeDevs & devMask & (1 << j)) {
gmmk_setKeys(s, j, argInt1, argInt2, c);
}
}
}
}
gmmk_close(s);
exit(EXIT_SUCCESS);
}
unsigned int setBits(unsigned int bits) {
unsigned int val = 0;
for(; bits > 0; bits--) {
val <<= 1;
val |= 1;
}
return(val);
}
int readNumber() {
char buffer[4];
int c;
unsigned int i;
/* gross */
for(c = getchar();
c == ' ' ||
c == '\r' ||
c == '\n' ||
c == '\t';
c = getchar());
ungetc(c, stdin);
for(i = 0; i < sizeof(buffer); i++) {
c = getchar();
if(c == EOF) {
buffer[i] = ' ';
break;
}
buffer[i] = c;
if(c == ' ' ||
c == '\r' ||
c == '\n' ||
c == '\t') {
break;
}
}
if((i == sizeof(buffer)) ||
((i == sizeof(buffer) - 1) &&
!(buffer[i] == ' ' ||
buffer[i] == '\r' ||
buffer[i] == '\n' ||
buffer[i] == '\t'))) {
return(0);
}
buffer[i] = '\0';
return(atoi(buffer));
}