-
Notifications
You must be signed in to change notification settings - Fork 249
/
ratbag-command.c
1712 lines (1458 loc) · 39.8 KB
/
ratbag-command.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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright © 2015 Red Hat, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#define _GNU_SOURCE
#include <errno.h>
#include <dirent.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <sys/stat.h>
#include <linux/input.h>
#include "shared.h"
enum options {
OPT_VERBOSE,
OPT_HELP,
};
enum errors {
SUCCESS = 0,
ERR_UNSUPPORTED = 1, /* device doesn't support function, or
an index exceeds the device */
ERR_USAGE = 2, /* invalid commandline */
ERR_DEVICE = 3, /* invalid/missing device or command failed */
};
enum cmd_flags {
FLAG_VERBOSE = 1 << 0,
FLAG_VERBOSE_RAW = 1 << 1,
/* flags used in ratbag_cmd */
FLAG_NEED_DEVICE = 1 << 10,
FLAG_NEED_PROFILE = 1 << 11,
FLAG_NEED_RESOLUTION = 1 << 12,
FLAG_NEED_BUTTON = 1 << 13,
};
struct ratbag_cmd_options {
enum cmd_flags flags;
struct ratbag_device *device;
struct ratbag_profile *profile;
struct ratbag_resolution *resolution;
struct ratbag_button *button;
};
struct ratbag_cmd {
const char *name;
int (*cmd)(const struct ratbag_cmd *cmd,
struct ratbag *ratbag,
struct ratbag_cmd_options *options,
int argc, char **argv);
uint32_t flags;
const struct ratbag_cmd *subcommands[];
};
static const struct ratbag_cmd *ratbag_commands;
static void
usage(void)
{
printf("%s [OPTIONS] {COMMAND} ... /path/to/device\n"
"\n"
"Query or change a device's settings:\n"
"\n"
"Options:\n"
" --verbose Print debugging output\n"
" --verbose=raw Print debugging output with protocol output.\n"
" --help Print this help.\n"
"\n"
"General Commands:\n"
" list List supported devices (does not take a device argument)\n"
"\n"
"Device Commands:\n"
" info Print information about a device \n"
"\n"
"Profile Commands:\n"
" profile active get Print the currently active profile\n"
" profile active set N Set profile N as to the active profile\n"
" profile N {COMMAND} Use profile N for COMMAND\n"
"\n"
"Resolution Commands\n"
" Resolution commands work on the given profile, or on the\n"
" active profile if none is given.\n"
"\n"
" resolution active get Print the currently active resolution\n"
" resolution active set N Set resolution N as the active resolution\n"
" resolution N {COMMAND} Use resolution N for COMMAND\n"
"\n"
"DPI Commands:\n"
" DPI commands work on the given profile and resolution, or on the\n"
" active resolution of the active profile if none are given.\n"
"\n"
" dpi get Print the dpi value\n"
" dpi set N Set the dpi value to N\n"
" rate get Print the report rate in Hz\n"
" rate set N Set the report rate in N Hz\n"
"\n"
"Button Commands:\n"
" Button commands work on the given profile, or on the\n"
" active profile if none is given.\n"
"\n"
" button count Print the number of buttons\n"
" button N action get Print the button action\n"
" button N action set button B Set the button action to button B\n"
" button N action set special S Set the button action to special action S\n"
" button N action set macro ... Set the button action to the given macro \n"
"\n"
" Macro syntax:\n"
" A macro is a series of key events or waiting periods.\n"
" Keys must be specified in linux/input.h key names.\n"
" KEY_A Press and release 'a'\n"
" +KEY_A Press 'a'\n"
" -KEY_A Release 'a'\n"
" t300 Wait 300ms\n"
"\n"
"Special Commands:\n"
"These commands are for testing purposes and may be removed without notice\n"
"\n"
" switch-etekcity Switch the Etekcity mouse active profile\n"
"\n"
"Examples:\n"
" %s profile active get\n"
" %s profile 0 resolution active set 4\n"
" %s profile 0 resolution 1 dpi get\n"
" %s resolution 4 rate get\n"
" %s dpi set 800\n"
"\n"
"Exit codes:\n"
" 0 Success\n"
" 1 Unsupported feature or index out of available range\n"
" 2 Commandline arguments are invalid\n"
" 3 Invalid device or a command failed on the device\n"
"\n",
program_invocation_short_name,
program_invocation_short_name,
program_invocation_short_name,
program_invocation_short_name,
program_invocation_short_name,
program_invocation_short_name);
}
static inline int
ratbag_cmd_device_from_arg(struct ratbag *ratbag,
int *argc, char **argv,
struct ratbag_device **device_out)
{
struct ratbag_device *device;
const char *path;
if (*argc == 0) {
error("Missing device path.\n");
return ERR_USAGE;
}
path = argv[*argc - 1];
device = ratbag_cmd_open_device(ratbag, path);
if (!device) {
error("Device '%s' is not supported\n", path);
return ERR_DEVICE;
}
(*argc)--;
*device_out = device;
return SUCCESS;
}
static inline struct ratbag_profile *
ratbag_cmd_get_active_profile(struct ratbag_device *device)
{
struct ratbag_profile *profile = NULL;
int i;
for (i = 0; i < ratbag_device_get_num_profiles(device); i++) {
profile = ratbag_device_get_profile(device, i);
if (ratbag_profile_is_active(profile))
return profile;
ratbag_profile_unref(profile);
profile = NULL;
}
if (!profile)
error("Failed to retrieve the active profile\n");
return NULL;
}
static inline struct ratbag_resolution *
ratbag_cmd_get_active_resolution(struct ratbag_profile *profile)
{
struct ratbag_resolution *resolution = NULL;
int i;
for (i = 0; i < ratbag_profile_get_num_resolutions(profile); i++) {
resolution = ratbag_profile_get_resolution(profile, i);
if (ratbag_resolution_is_active(resolution))
return resolution;
ratbag_resolution_unref(resolution);
resolution = NULL;
}
if (!resolution)
error("Failed to retrieve the active resolution\n");
return NULL;
}
static inline int
fill_options(struct ratbag *ratbag,
struct ratbag_cmd_options *options,
uint32_t flags,
int *argc, char **argv)
{
struct ratbag_device *device = options->device;
struct ratbag_profile *profile = options->profile;
struct ratbag_resolution *resolution = options->resolution;
struct ratbag_button *button = options->button;
int rc;
if ((flags & (FLAG_NEED_DEVICE|FLAG_NEED_PROFILE|FLAG_NEED_RESOLUTION)) &&
device == NULL) {
rc = ratbag_cmd_device_from_arg(ratbag, argc, argv,
&device);
if (rc != SUCCESS)
return rc;
options->device = device;
}
if ((flags & (FLAG_NEED_PROFILE|FLAG_NEED_RESOLUTION)) &&
profile == NULL) {
profile = ratbag_cmd_get_active_profile(device);
if (!profile)
return ERR_DEVICE;
options->profile = profile;
}
if (flags & FLAG_NEED_RESOLUTION && resolution == NULL) {
resolution = ratbag_cmd_get_active_resolution(profile);
if (!resolution)
return ERR_DEVICE;
options->resolution = resolution;
}
if (flags & FLAG_NEED_BUTTON && button == NULL) {
error("Missing button identifier\n");
return ERR_USAGE;
}
return SUCCESS;
}
static int
run_subcommand(const char *command,
const struct ratbag_cmd *cmd,
struct ratbag *ratbag,
struct ratbag_cmd_options *options,
int argc, char **argv)
{
const struct ratbag_cmd *sub = cmd->subcommands[0];
int i = 0;
int rc;
while (sub) {
if (streq(command, sub->name)) {
rc = fill_options(ratbag, options,
sub->flags,
&argc,
argv);
if (rc != SUCCESS)
return rc;
argc--;
argv++;
return sub->cmd(sub, ratbag, options, argc, argv);
}
sub = cmd->subcommands[i++];
}
error("Invalid subcommand '%s'\n", command);
return ERR_USAGE;
}
static int
ratbag_cmd_info(const struct ratbag_cmd *cmd,
struct ratbag *ratbag,
struct ratbag_cmd_options *options,
int argc, char **argv)
{
struct ratbag_device *device;
struct ratbag_profile *profile;
struct ratbag_button *button;
char *action;
int num_profiles, num_buttons;
int i, j, b;
device = options->device;
printf("Device '%s'\n", ratbag_device_get_name(device));
printf("Capabilities:");
if (ratbag_device_has_capability(device,
RATBAG_DEVICE_CAP_SWITCHABLE_RESOLUTION))
printf(" res");
if (ratbag_device_has_capability(device,
RATBAG_DEVICE_CAP_SWITCHABLE_PROFILE))
printf(" profile");
if (ratbag_device_has_capability(device,
RATBAG_DEVICE_CAP_BUTTON_KEY))
printf(" btn-key");
if (ratbag_device_has_capability(device,
RATBAG_DEVICE_CAP_BUTTON_MACROS))
printf(" btn-macros");
printf("\n");
num_buttons = ratbag_device_get_num_buttons(device);
printf("Number of buttons: %d\n", num_buttons);
num_profiles = ratbag_device_get_num_profiles(device);
printf("Profiles supported: %d\n", num_profiles);
for (i = 0; i < num_profiles; i++) {
int dpi, rate;
profile = ratbag_device_get_profile(device, i);
if (!profile)
continue;
printf(" Profile %d%s%s\n", i,
ratbag_profile_is_active(profile) ? " (active)" : "",
ratbag_profile_is_default(profile) ? " (default)" : "");
printf(" Resolutions:\n");
for (j = 0; j < ratbag_profile_get_num_resolutions(profile); j++) {
struct ratbag_resolution *res;
res = ratbag_profile_get_resolution(profile, j);
dpi = ratbag_resolution_get_dpi(res);
rate = ratbag_resolution_get_report_rate(res);
if (dpi == 0)
printf(" %d: <disabled>\n", j);
else if (ratbag_resolution_has_capability(res,
RATBAG_RESOLUTION_CAP_SEPARATE_XY_RESOLUTION))
printf(" %d: %dx%ddpi @ %dHz%s%s\n", j,
ratbag_resolution_get_dpi_x(res),
ratbag_resolution_get_dpi_y(res),
rate,
ratbag_resolution_is_active(res) ? " (active)" : "",
ratbag_resolution_is_default(res) ? " (default)" : "");
else
printf(" %d: %ddpi @ %dHz%s%s\n", j, dpi, rate,
ratbag_resolution_is_active(res) ? " (active)" : "",
ratbag_resolution_is_default(res) ? " (default)" : "");
ratbag_resolution_unref(res);
}
for (b = 0; b < num_buttons; b++) {
enum ratbag_button_type type;
button = ratbag_profile_get_button(profile, b);
type = ratbag_button_get_type(button);
action = button_action_to_str(button);
printf(" Button: %d type %s is mapped to '%s'\n",
b, button_type_to_str(type), action);
free(action);
button = ratbag_button_unref(button);
}
profile = ratbag_profile_unref(profile);
}
return SUCCESS;
}
static const struct ratbag_cmd cmd_info = {
.name = "info",
.cmd = ratbag_cmd_info,
.flags = FLAG_NEED_DEVICE,
.subcommands = { NULL },
};
static int
ratbag_cmd_switch_etekcity(const struct ratbag_cmd *cmd,
struct ratbag *ratbag,
struct ratbag_cmd_options *options,
int argc, char **argv)
{
struct ratbag_device *device;
struct ratbag_button *button_6, *button_7;
struct ratbag_profile *profile = NULL;
int commit = 0;
unsigned int modifiers[10];
size_t modifiers_sz = 10;
device = options->device;
profile = options->profile;
if (!ratbag_device_has_capability(device,
RATBAG_DEVICE_CAP_SWITCHABLE_PROFILE)) {
error("Device '%s' has no switchable profiles\n",
ratbag_device_get_name(device));
return ERR_UNSUPPORTED;
}
button_6 = ratbag_profile_get_button(profile, 6);
button_7 = ratbag_profile_get_button(profile, 7);
if (ratbag_button_get_key(button_6, modifiers, &modifiers_sz) == KEY_VOLUMEUP &&
ratbag_button_get_key(button_7, modifiers, &modifiers_sz) == KEY_VOLUMEDOWN) {
ratbag_button_disable(button_6);
ratbag_button_disable(button_7);
commit = 1;
} else if (ratbag_button_get_action_type(button_6) == RATBAG_BUTTON_ACTION_TYPE_NONE &&
ratbag_button_get_action_type(button_7) == RATBAG_BUTTON_ACTION_TYPE_NONE) {
ratbag_button_set_key(button_6, KEY_VOLUMEUP, modifiers, 0);
ratbag_button_set_key(button_7, KEY_VOLUMEDOWN, modifiers, 0);
commit = 2;
}
button_6 = ratbag_button_unref(button_6);
button_7 = ratbag_button_unref(button_7);
printf("Switched the current profile of '%s' to %sreport the volume keys\n",
ratbag_device_get_name(device),
commit == 1 ? "not " : "");
return SUCCESS;
}
static const struct ratbag_cmd cmd_switch_etekcity = {
.name = "switch-etekcity",
.cmd = ratbag_cmd_switch_etekcity,
.flags = FLAG_NEED_DEVICE | FLAG_NEED_PROFILE,
.subcommands = { NULL },
};
struct macro {
const char *name;
struct {
enum ratbag_macro_event_type type;
unsigned data;
} events[64];
};
static int
str_to_macro(const char *action_arg, struct macro *m)
{
char *str, *s;
enum ratbag_macro_event_type type;
int code;
int idx = 0;
int rc = ERR_USAGE;
/* FIXME: handle per-device maximum lengths of macros */
if (!action_arg)
return -EINVAL;
str = strdup(action_arg);
s = str;
m->name = "<cmdline>";
while (idx < ARRAY_LENGTH(m->events)) {
char *token;
token = strsep(&s, " ");
if (!token)
break;
if (strlen(token) == 0)
continue;
switch(token[0]) {
case '+':
type = RATBAG_MACRO_EVENT_KEY_PRESSED;
token++;
break;
case '-':
type = RATBAG_MACRO_EVENT_KEY_RELEASED;
token++;
break;
case 't':
type = RATBAG_MACRO_EVENT_WAIT;
token++;
break;
default:
type = RATBAG_MACRO_EVENT_NONE;
break;
}
if (type == RATBAG_MACRO_EVENT_WAIT) {
char *endptr;
code = strtol(token, &endptr, 10);
if (*endptr != '\0') {
error("Invalid token name: %s\n", token);
goto out;
}
} else {
code = libevdev_event_code_from_name(EV_KEY, token);
if (code == -1) {
error("Invalid token name: %s\n", token);
goto out;
}
}
if (type == RATBAG_MACRO_EVENT_NONE) {
m->events[idx].type = RATBAG_MACRO_EVENT_KEY_PRESSED;
m->events[idx].data = code;
type = RATBAG_MACRO_EVENT_KEY_RELEASED;
idx++;
}
m->events[idx].data = code;
m->events[idx].type = type;
idx++;
}
rc = SUCCESS;
out:
free(str);
return rc;
}
static int
ratbag_cmd_change_button(const struct ratbag_cmd *cmd,
struct ratbag *ratbag,
struct ratbag_cmd_options *options,
int argc, char **argv)
{
const char *action_str, *action_arg;
struct ratbag_device *device;
struct ratbag_button *button = NULL;
struct ratbag_profile *profile = NULL;
int button_index;
enum ratbag_button_action_type action_type;
int rc = ERR_DEVICE;
unsigned int btnkey;
enum ratbag_button_action_special special;
struct macro macro = {0};
int i;
if (argc != 3)
return ERR_USAGE;
button_index = atoi(argv[0]);
action_str = argv[1];
action_arg = argv[2];
argc -= 3;
argv += 3;
if (streq(action_str, "button")) {
action_type = RATBAG_BUTTON_ACTION_TYPE_BUTTON;
btnkey = atoi(action_arg);
} else if (streq(action_str, "key")) {
action_type = RATBAG_BUTTON_ACTION_TYPE_KEY;
btnkey = libevdev_event_code_from_name(EV_KEY, action_arg);
if (!btnkey) {
error("Failed to resolve key %s\n", action_arg);
return ERR_USAGE;
}
} else if (streq(action_str, "special")) {
action_type = RATBAG_BUTTON_ACTION_TYPE_SPECIAL;
special = str_to_special_action(action_arg);
if (special == RATBAG_BUTTON_ACTION_SPECIAL_INVALID) {
error("Invalid special command '%s'\n", action_arg);
return ERR_USAGE;
}
} else if (streq(action_str, "macro")) {
action_type = RATBAG_BUTTON_ACTION_TYPE_MACRO;
if (str_to_macro(action_arg, ¯o)) {
error("Invalid special command '%s'\n", action_arg);
return ERR_USAGE;
}
} else {
return ERR_USAGE;
}
device = options->device;
profile = options->profile;
if (!ratbag_device_has_capability(device,
RATBAG_DEVICE_CAP_BUTTON_KEY)) {
error("Device '%s' has no programmable buttons\n",
ratbag_device_get_name(device));
rc = ERR_UNSUPPORTED;
goto out;
}
button = ratbag_profile_get_button(profile, button_index);
if (!button) {
error("Invalid button number %d\n", button_index);
rc = ERR_UNSUPPORTED;
goto out;
}
switch (action_type) {
case RATBAG_BUTTON_ACTION_TYPE_BUTTON:
rc = ratbag_button_set_button(button, btnkey);
break;
case RATBAG_BUTTON_ACTION_TYPE_KEY:
rc = ratbag_button_set_key(button, btnkey, NULL, 0);
break;
case RATBAG_BUTTON_ACTION_TYPE_SPECIAL:
rc = ratbag_button_set_special(button, special);
break;
case RATBAG_BUTTON_ACTION_TYPE_MACRO:
rc = ratbag_button_set_macro(button, macro.name);
for (i = 0; i < ARRAY_LENGTH(macro.events); i++) {
if (macro.events[i].type == RATBAG_MACRO_EVENT_NONE)
break;
ratbag_button_set_macro_event(button,
i,
macro.events[i].type,
macro.events[i].data);
}
rc = ratbag_button_write_macro(button);
break;
default:
error("well, that shouldn't have happened\n");
abort();
break;
}
if (rc) {
error("Unable to perform button %d mapping %s %s\n",
button_index,
action_str,
action_arg);
rc = ERR_UNSUPPORTED;
goto out;
}
rc = ratbag_profile_set_active(profile);
if (rc) {
error("Unable to apply the current profile: %s (%d)\n",
strerror(-rc),
rc);
rc = ERR_DEVICE;
goto out;
}
out:
button = ratbag_button_unref(button);
return rc;
}
static const struct ratbag_cmd cmd_change_button = {
.name = "change-button",
.cmd = ratbag_cmd_change_button,
.flags = FLAG_NEED_DEVICE | FLAG_NEED_PROFILE,
.subcommands = { NULL },
};
static int
filter_event_node(const struct dirent *input_entry)
{
return strneq(input_entry->d_name, "event", 5);
}
static int
ratbag_cmd_list_supported_devices(const struct ratbag_cmd *cmd,
struct ratbag *ratbag,
struct ratbag_cmd_options *options,
int argc, char **argv)
{
struct dirent **input_list;
struct ratbag_device *device;
char path[256];
int n, i;
int supported = 0;
if (argc != 0)
return ERR_USAGE;
n = scandir("/dev/input", &input_list, filter_event_node, alphasort);
if (n < 0)
return SUCCESS;
i = -1;
while (++i < n) {
sprintf(path, "/dev/input/%s", input_list[i]->d_name);
device = ratbag_cmd_open_device(ratbag, path);
if (device) {
printf("%s:\t%s\n", path, ratbag_device_get_name(device));
device = ratbag_device_unref(device);
supported++;
}
free(input_list[i]);
}
free(input_list);
if (!supported)
printf("No supported devices found\n");
return SUCCESS;
}
static const struct ratbag_cmd cmd_list = {
.name = "list",
.cmd = ratbag_cmd_list_supported_devices,
.flags = 0,
.subcommands = { NULL },
};
static int
ratbag_cmd_resolution_active_set(const struct ratbag_cmd *cmd,
struct ratbag *ratbag,
struct ratbag_cmd_options *options,
int argc, char **argv)
{
struct ratbag_resolution *resolution;
int rc;
resolution = options->resolution;
rc = ratbag_resolution_set_active(resolution);
if (rc != 0) {
error("Failed to to set resolution as active\n");
return ERR_DEVICE;
}
return SUCCESS;
}
static const struct ratbag_cmd cmd_resolution_active_set = {
.name = "set",
.cmd = ratbag_cmd_resolution_active_set,
.flags = FLAG_NEED_DEVICE | FLAG_NEED_PROFILE,
.subcommands = { NULL },
};
static int
ratbag_cmd_resolution_active_get(const struct ratbag_cmd *cmd,
struct ratbag *ratbag,
struct ratbag_cmd_options *options,
int argc, char **argv)
{
struct ratbag_profile *profile;
struct ratbag_resolution *resolution = NULL;
int num_resolutions;
int active_resolution = -1;
int i;
int rc = SUCCESS;
profile = options->profile;
num_resolutions = ratbag_profile_get_num_resolutions(profile);
for (i = 0; i < num_resolutions && active_resolution < 0; i++) {
resolution = ratbag_profile_get_resolution(profile, i);
if (ratbag_resolution_is_active(resolution))
active_resolution = i;
ratbag_resolution_unref(resolution);
}
if (active_resolution < 0) {
error("BUG: Unable to find active resolution\n");
rc = ERR_DEVICE;
}
if (rc == SUCCESS)
printf("%d\n", active_resolution);
return rc;
}
static const struct ratbag_cmd cmd_resolution_active_get = {
.name = "get",
.cmd = ratbag_cmd_resolution_active_get,
.flags = FLAG_NEED_DEVICE | FLAG_NEED_PROFILE,
.subcommands = { NULL },
};
static int
ratbag_cmd_resolution_active(const struct ratbag_cmd *cmd,
struct ratbag *ratbag,
struct ratbag_cmd_options *options,
int argc, char **argv)
{
if (argc < 1)
return ERR_USAGE;
return run_subcommand(argv[0],
cmd,
ratbag, options,
argc, argv);
}
static const struct ratbag_cmd cmd_resolution_active = {
.name = "active",
.cmd = ratbag_cmd_resolution_active,
.flags = FLAG_NEED_DEVICE | FLAG_NEED_PROFILE,
.subcommands = {
&cmd_resolution_active_get,
&cmd_resolution_active_set,
NULL,
},
};
static int
ratbag_cmd_resolution_dpi_get(const struct ratbag_cmd *cmd,
struct ratbag *ratbag,
struct ratbag_cmd_options *options,
int argc, char **argv)
{
struct ratbag_resolution *resolution;
int dpi;
resolution = options->resolution;
dpi = ratbag_resolution_get_dpi(resolution);
printf("%d\n", dpi);
return SUCCESS;
}
static const struct ratbag_cmd cmd_resolution_dpi_get = {
.name = "get",
.cmd = ratbag_cmd_resolution_dpi_get,
.flags = FLAG_NEED_DEVICE | FLAG_NEED_PROFILE | FLAG_NEED_RESOLUTION,
.subcommands = { NULL },
};
static int
ratbag_cmd_resolution_dpi_set(const struct ratbag_cmd *cmd,
struct ratbag *ratbag,
struct ratbag_cmd_options *options,
int argc, char **argv)
{
struct ratbag_device *device;
struct ratbag_resolution *resolution;
int rc = SUCCESS;
int dpi;
if (argc != 1)
return ERR_USAGE;
dpi = atoi(argv[0]);
argc--;
argv++;
device = options->device;
resolution = options->resolution;
if (!ratbag_device_has_capability(device,
RATBAG_DEVICE_CAP_SWITCHABLE_RESOLUTION)) {
error("Device '%s' has no switchable resolution\n",
ratbag_device_get_name(device));
rc = ERR_UNSUPPORTED;
goto out;
}
rc = ratbag_resolution_set_dpi(resolution, dpi);
if (rc) {
error("Failed to change the dpi: %s (%d)\n",
strerror(-rc),
rc);
rc = ERR_DEVICE;
}
out:
return rc;
}
static const struct ratbag_cmd cmd_resolution_dpi_set = {
.name = "set",
.cmd = ratbag_cmd_resolution_dpi_set,
.flags = FLAG_NEED_DEVICE | FLAG_NEED_PROFILE| FLAG_NEED_RESOLUTION,
.subcommands = { NULL },
};
static int
ratbag_cmd_resolution_dpi(const struct ratbag_cmd *cmd,
struct ratbag *ratbag,
struct ratbag_cmd_options *options,
int argc, char **argv)
{
if (argc < 1)
return ERR_USAGE;
return run_subcommand(argv[0],
cmd,
ratbag, options,
argc, argv);
}
static const struct ratbag_cmd cmd_resolution_dpi = {
.name = "dpi",
.cmd = ratbag_cmd_resolution_dpi,
.flags = FLAG_NEED_DEVICE | FLAG_NEED_PROFILE| FLAG_NEED_RESOLUTION,
.subcommands = {
&cmd_resolution_dpi_get,
&cmd_resolution_dpi_set,
NULL,
},
};
static int
ratbag_cmd_resolution_rate_get(const struct ratbag_cmd *cmd,
struct ratbag *ratbag,
struct ratbag_cmd_options *options,
int argc, char **argv)
{
struct ratbag_resolution *resolution;
int rate;
resolution = options->resolution;
rate = ratbag_resolution_get_report_rate(resolution);
printf("%d\n", rate);
return SUCCESS;
}
static const struct ratbag_cmd cmd_resolution_rate_get = {
.name = "get",
.cmd = ratbag_cmd_resolution_rate_get,
.flags = FLAG_NEED_DEVICE | FLAG_NEED_PROFILE | FLAG_NEED_RESOLUTION,
.subcommands = { NULL },
};
static int
ratbag_cmd_resolution_rate_set(const struct ratbag_cmd *cmd,
struct ratbag *ratbag,
struct ratbag_cmd_options *options,
int argc, char **argv)
{
struct ratbag_device *device;
struct ratbag_resolution *resolution;
int rc = SUCCESS;
int rate;
if (argc != 1)
return ERR_USAGE;
rate = atoi(argv[0]);
argc--;
argv++;
device = options->device;
resolution = options->resolution;
if (!ratbag_device_has_capability(device,
RATBAG_DEVICE_CAP_SWITCHABLE_RESOLUTION)) {
error("Device '%s' has no switchable resolution\n",
ratbag_device_get_name(device));
rc = ERR_UNSUPPORTED;
goto out;
}
rc = ratbag_resolution_set_report_rate(resolution, rate);
if (rc) {
error("Failed to change the rate: %s (%d)\n",
strerror(-rc),
rc);
rc = ERR_DEVICE;
}
out:
return rc;
}
static const struct ratbag_cmd cmd_resolution_rate_set = {
.name = "set",
.cmd = ratbag_cmd_resolution_rate_set,
.flags = FLAG_NEED_DEVICE | FLAG_NEED_PROFILE| FLAG_NEED_RESOLUTION,
.subcommands = { NULL },
};
static int
ratbag_cmd_resolution_rate(const struct ratbag_cmd *cmd,
struct ratbag *ratbag,
struct ratbag_cmd_options *options,