forked from wayland-tablet/libinput
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
quirks.c
1531 lines (1307 loc) · 34.2 KB
/
quirks.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 © 2018 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.
*/
#include "config.h"
/* This has the hallmarks of a library to make it re-usable from the tests
* and from the list-quirks tool. It doesn't have all of the features from a
* library you'd expect though
*/
#undef NDEBUG /* You don't get to disable asserts here */
#include <assert.h>
#include <stdlib.h>
#include <libudev.h>
#include <dirent.h>
#include <fnmatch.h>
#include "libinput-util.h"
#include "libinput-private.h"
#include "quirks.h"
/* Custom logging so we can have detailed output for the tool but minimal
* logging for libinput itself. */
#define qlog_debug(ctx_, ...) quirk_log_msg((ctx_), QLOG_NOISE, __VA_ARGS__)
#define qlog_info(ctx_, ...) quirk_log_msg((ctx_), QLOG_INFO, __VA_ARGS__)
#define qlog_error(ctx_, ...) quirk_log_msg((ctx_), QLOG_ERROR, __VA_ARGS__)
#define qlog_parser(ctx_, ...) quirk_log_msg((ctx_), QLOG_PARSER_ERROR, __VA_ARGS__)
enum property_type {
PT_UINT,
PT_INT,
PT_STRING,
PT_BOOL,
PT_DIMENSION,
PT_RANGE,
PT_DOUBLE,
};
/**
* Generic value holder for the property types we support. The type
* identifies which value in the union is defined and we expect callers to
* already know which type yields which value.
*/
struct property {
size_t refcount;
struct list link; /* struct sections.properties */
enum quirk id;
enum property_type type;
union {
bool b;
uint32_t u;
int32_t i;
char *s;
struct quirk_dimensions dim;
struct quirk_range range;
double d;
} value;
};
enum match_flags {
M_NAME = (1 << 0),
M_BUS = (1 << 1),
M_VID = (1 << 2),
M_PID = (1 << 3),
M_DMI = (1 << 4),
M_UDEV_TYPE = (1 << 5),
M_DT = (1 << 6),
M_VERSION = (1 << 7),
M_LAST = M_VERSION,
};
enum bustype {
BT_UNKNOWN,
BT_USB,
BT_BLUETOOTH,
BT_PS2,
BT_RMI,
BT_I2C,
};
enum udev_type {
UDEV_MOUSE = (1 << 1),
UDEV_POINTINGSTICK = (1 << 2),
UDEV_TOUCHPAD = (1 << 3),
UDEV_TABLET = (1 << 4),
UDEV_TABLET_PAD = (1 << 5),
UDEV_JOYSTICK = (1 << 6),
UDEV_KEYBOARD = (1 << 7),
};
/**
* Contains the combined set of matches for one section or the values for
* one device.
*
* bits defines which fields are set, the rest is zero.
*/
struct match {
uint32_t bits;
char *name;
enum bustype bus;
uint32_t vendor;
uint32_t product;
uint32_t version;
char *dmi; /* dmi modalias with preceding "dmi:" */
/* We can have more than one type set, so this is a bitfield */
uint32_t udev_type;
char *dt; /* device tree compatible (first) string */
};
/**
* Represents one section in the .quirks file.
*/
struct section {
struct list link;
bool has_match; /* to check for empty sections */
bool has_property; /* to check for empty sections */
char *name; /* the [Section Name] */
struct match match;
struct list properties;
};
/**
* The struct returned to the caller. It contains the
* properties for a given device.
*/
struct quirks {
size_t refcount;
struct list link; /* struct quirks_context.quirks */
/* These are not ref'd, just a collection of pointers */
struct property **properties;
size_t nproperties;
};
/**
* Quirk matching context, initialized once with quirks_init_subsystem()
*/
struct quirks_context {
size_t refcount;
libinput_log_handler log_handler;
enum quirks_log_type log_type;
struct libinput *libinput; /* for logging */
char *dmi;
char *dt;
struct list sections;
/* list of quirks handed to libinput, just for bookkeeping */
struct list quirks;
};
LIBINPUT_ATTRIBUTE_PRINTF(3, 0)
static inline void
quirk_log_msg_va(struct quirks_context *ctx,
enum quirks_log_priorities priority,
const char *format,
va_list args)
{
switch (priority) {
/* We don't use this if we're logging through libinput */
default:
case QLOG_NOISE:
case QLOG_PARSER_ERROR:
if (ctx->log_type == QLOG_LIBINPUT_LOGGING)
return;
break;
case QLOG_DEBUG: /* These map straight to libinput priorities */
case QLOG_INFO:
case QLOG_ERROR:
break;
}
ctx->log_handler(ctx->libinput,
(enum libinput_log_priority)priority,
format,
args);
}
LIBINPUT_ATTRIBUTE_PRINTF(3, 4)
static inline void
quirk_log_msg(struct quirks_context *ctx,
enum quirks_log_priorities priority,
const char *format,
...)
{
va_list args;
va_start(args, format);
quirk_log_msg_va(ctx, priority, format, args);
va_end(args);
}
const char *
quirk_get_name(enum quirk q)
{
switch(q) {
case QUIRK_MODEL_ALPS_TOUCHPAD: return "ModelALPSTouchpad";
case QUIRK_MODEL_APPLE_TOUCHPAD: return "ModelAppleTouchpad";
case QUIRK_MODEL_APPLE_MAGICMOUSE: return "ModelAppleMagicMouse";
case QUIRK_MODEL_TABLET_NO_TILT: return "ModelTabletNoTilt";
case QUIRK_MODEL_APPLE_TOUCHPAD_ONEBUTTON: return "ModelAppleTouchpadOneButton";
case QUIRK_MODEL_TOUCHPAD_VISIBLE_MARKER: return "ModelTouchpadVisibleMarker";
case QUIRK_MODEL_CYBORG_RAT: return "ModelCyborgRat";
case QUIRK_MODEL_CHROMEBOOK: return "ModelChromebook";
case QUIRK_MODEL_HP6910_TOUCHPAD: return "ModelHP6910Touchpad";
case QUIRK_MODEL_HP8510_TOUCHPAD: return "ModelHP8510Touchpad";
case QUIRK_MODEL_HP_PAVILION_DM4_TOUCHPAD: return "ModelHPPavilionDM4Touchpad";
case QUIRK_MODEL_HP_STREAM11_TOUCHPAD: return "ModelHPStream11Touchpad";
case QUIRK_MODEL_HP_ZBOOK_STUDIO_G3: return "ModelHPZBookStudioG3";
case QUIRK_MODEL_TABLET_NO_PROXIMITY_OUT: return "ModelTabletNoProximityOut";
case QUIRK_MODEL_LENOVO_SCROLLPOINT: return "ModelLenovoScrollPoint";
case QUIRK_MODEL_LENOVO_X230: return "ModelLenovoX230";
case QUIRK_MODEL_LENOVO_T450_TOUCHPAD: return "ModelLenovoT450Touchpad";
case QUIRK_MODEL_TABLET_MODE_NO_SUSPEND: return "ModelTabletModeNoSuspend";
case QUIRK_MODEL_LENOVO_CARBON_X1_6TH: return "ModelLenovoCarbonX16th";
case QUIRK_MODEL_TRACKBALL: return "ModelTrackball";
case QUIRK_MODEL_LOGITECH_MARBLE_MOUSE: return "ModelLogitechMarbleMouse";
case QUIRK_MODEL_BOUNCING_KEYS: return "ModelBouncingKeys";
case QUIRK_MODEL_SYNAPTICS_SERIAL_TOUCHPAD: return "ModelSynapticsSerialTouchpad";
case QUIRK_MODEL_SYSTEM76_BONOBO: return "ModelSystem76Bonobo";
case QUIRK_MODEL_CLEVO_W740SU: return "ModelClevoW740SU";
case QUIRK_MODEL_SYSTEM76_GALAGO: return "ModelSystem76Galago";
case QUIRK_MODEL_SYSTEM76_KUDU: return "ModelSystem76Kudu";
case QUIRK_MODEL_WACOM_TOUCHPAD: return "ModelWacomTouchpad";
case QUIRK_ATTR_SIZE_HINT: return "AttrSizeHint";
case QUIRK_ATTR_TOUCH_SIZE_RANGE: return "AttrTouchSizeRange";
case QUIRK_ATTR_PALM_SIZE_THRESHOLD: return "AttrPalmSizeThreshold";
case QUIRK_ATTR_LID_SWITCH_RELIABILITY: return "AttrLidSwitchReliability";
case QUIRK_ATTR_KEYBOARD_INTEGRATION: return "AttrKeyboardIntegration";
case QUIRK_ATTR_TPKBCOMBO_LAYOUT: return "AttrTPKComboLayout";
case QUIRK_ATTR_PRESSURE_RANGE: return "AttrPressureRange";
case QUIRK_ATTR_PALM_PRESSURE_THRESHOLD: return "AttrPalmPressureThreshold";
case QUIRK_ATTR_RESOLUTION_HINT: return "AttrResolutionHint";
case QUIRK_ATTR_TRACKPOINT_RANGE: return "AttrTrackpointRange";
case QUIRK_ATTR_THUMB_PRESSURE_THRESHOLD: return "AttrThumbPressureThreshold";
default:
abort();
}
}
static inline const char *
matchflagname(enum match_flags f)
{
switch(f) {
case M_NAME: return "MatchName"; break;
case M_BUS: return "MatchBus"; break;
case M_VID: return "MatchVendor"; break;
case M_PID: return "MatchProduct"; break;
case M_VERSION: return "MatchVersion"; break;
case M_DMI: return "MatchDMIModalias"; break;
case M_UDEV_TYPE: return "MatchUdevType"; break;
case M_DT: return "MatchDeviceTree"; break;
default:
abort();
}
};
static inline struct property *
property_new(void)
{
struct property *p;
p = zalloc(sizeof *p);
p->refcount = 1;
list_init(&p->link);
return p;
}
static inline struct property *
property_ref(struct property *p)
{
assert(p->refcount > 0);
p->refcount++;
return p;
};
static inline struct property *
property_unref(struct property *p)
{
/* Note: we don't cleanup here, that is a separate call so we
can abort if we haven't cleaned up correctly. */
assert(p->refcount > 0);
p->refcount--;
return NULL;
};
/* Separate call so we can verify that the caller unrefs the property
* before shutting down the subsystem.
*/
static inline void
property_cleanup(struct property *p)
{
/* If we get here, the quirks must've been removed already */
property_unref(p);
assert(p->refcount == 0);
list_remove(&p->link);
if (p->type == PT_STRING)
free(p->value.s);
free(p);
}
/**
* Return the dmi modalias from the udev device.
*/
static inline char *
init_dmi(void)
{
struct udev *udev;
struct udev_device *udev_device;
const char *modalias = NULL;
char *copy = NULL;
const char *syspath = "/sys/devices/virtual/dmi/id";
if (getenv("LIBINPUT_RUNNING_TEST_SUITE"))
return safe_strdup("dmi:");
udev = udev_new();
if (!udev)
return NULL;
udev_device = udev_device_new_from_syspath(udev, syspath);
if (udev_device)
modalias = udev_device_get_property_value(udev_device,
"MODALIAS");
/* Not sure whether this could ever really fail, if so we should
* open the sysfs file directly. But then udev wouldn't have failed,
* so... */
if (!modalias)
modalias = "dmi:*";
copy = safe_strdup(modalias);
udev_device_unref(udev_device);
udev_unref(udev);
return copy;
}
/**
* Return the dt compatible string
*/
static inline char *
init_dt(void)
{
char compatible[1024];
char *copy = NULL;
const char *syspath = "/sys/firmware/devicetree/base/compatible";
FILE *fp;
if (getenv("LIBINPUT_RUNNING_TEST_SUITE"))
return safe_strdup("");
fp = fopen(syspath, "r");
if (!fp)
return NULL;
/* devicetree/base/compatible has multiple null-terminated entries
but we only care about the first one here, so strdup is enough */
if (fgets(compatible, sizeof(compatible), fp)) {
copy = safe_strdup(compatible);
}
fclose(fp);
return copy;
}
static inline struct section *
section_new(const char *path, const char *name)
{
struct section *s = zalloc(sizeof(*s));
xasprintf(&s->name, "%s (%s)", name, basename(path));
list_init(&s->link);
list_init(&s->properties);
return s;
}
static inline void
section_destroy(struct section *s)
{
struct property *p, *tmp;
free(s->name);
free(s->match.name);
free(s->match.dmi);
free(s->match.dt);
list_for_each_safe(p, tmp, &s->properties, link)
property_cleanup(p);
assert(list_empty(&s->properties));
list_remove(&s->link);
free(s);
}
/**
* Parse a MatchFooBar=banana line.
*
* @param section The section struct to be filled in
* @param key The MatchFooBar part of the line
* @param value The banana part of the line.
*
* @return true on success, false otherwise.
*/
static bool
parse_match(struct quirks_context *ctx,
struct section *s,
const char *key,
const char *value)
{
int rc = false;
#define check_set_bit(s_, bit_) { \
if ((s_)->match.bits & (bit_)) goto out; \
(s_)->match.bits |= (bit_); \
}
assert(strlen(value) >= 1);
if (streq(key, "MatchName")) {
check_set_bit(s, M_NAME);
s->match.name = safe_strdup(value);
} else if (streq(key, "MatchBus")) {
check_set_bit(s, M_BUS);
if (streq(value, "usb"))
s->match.bus = BT_USB;
else if (streq(value, "bluetooth"))
s->match.bus = BT_BLUETOOTH;
else if (streq(value, "ps2"))
s->match.bus = BT_PS2;
else if (streq(value, "rmi"))
s->match.bus = BT_RMI;
else if (streq(value, "i2c"))
s->match.bus = BT_I2C;
else
goto out;
} else if (streq(key, "MatchVendor")) {
unsigned int vendor;
check_set_bit(s, M_VID);
if (!strneq(value, "0x", 2) ||
!safe_atou_base(value, &vendor, 16) ||
vendor > 0xFFFF)
goto out;
s->match.vendor = vendor;
} else if (streq(key, "MatchProduct")) {
unsigned int product;
check_set_bit(s, M_PID);
if (!strneq(value, "0x", 2) ||
!safe_atou_base(value, &product, 16) ||
product > 0xFFFF)
goto out;
s->match.product = product;
} else if (streq(key, "MatchVersion")) {
unsigned int version;
check_set_bit(s, M_VERSION);
if (!strneq(value, "0x", 2) ||
!safe_atou_base(value, &version, 16) ||
version > 0xFFFF)
goto out;
s->match.version = version;
} else if (streq(key, "MatchDMIModalias")) {
check_set_bit(s, M_DMI);
if (!strneq(value, "dmi:", 4)) {
qlog_parser(ctx,
"%s: MatchDMIModalias must start with 'dmi:'\n",
s->name);
goto out;
}
s->match.dmi = safe_strdup(value);
} else if (streq(key, "MatchUdevType")) {
check_set_bit(s, M_UDEV_TYPE);
if (streq(value, "touchpad"))
s->match.udev_type = UDEV_TOUCHPAD;
else if (streq(value, "mouse"))
s->match.udev_type = UDEV_MOUSE;
else if (streq(value, "pointingstick"))
s->match.udev_type = UDEV_POINTINGSTICK;
else if (streq(value, "keyboard"))
s->match.udev_type = UDEV_KEYBOARD;
else if (streq(value, "joystick"))
s->match.udev_type = UDEV_JOYSTICK;
else if (streq(value, "tablet"))
s->match.udev_type = UDEV_TABLET;
else if (streq(value, "tablet-pad"))
s->match.udev_type = UDEV_TABLET_PAD;
else
goto out;
} else if (streq(key, "MatchDeviceTree")) {
check_set_bit(s, M_DT);
s->match.dt = safe_strdup(value);
} else {
qlog_error(ctx, "Unknown match key '%s'\n", key);
goto out;
}
#undef check_set_bit
s->has_match = true;
rc = true;
out:
return rc;
}
/**
* Parse a ModelFooBar=1 line.
*
* @param section The section struct to be filled in
* @param key The ModelFooBar part of the line
* @param value The value after the =, must be 1 or 0.
*
* @return true on success, false otherwise.
*/
static bool
parse_model(struct quirks_context *ctx,
struct section *s,
const char *key,
const char *value)
{
enum quirk quirks[] = {
QUIRK_MODEL_ALPS_TOUCHPAD,
QUIRK_MODEL_APPLE_TOUCHPAD,
QUIRK_MODEL_APPLE_MAGICMOUSE,
QUIRK_MODEL_TABLET_NO_TILT,
QUIRK_MODEL_APPLE_TOUCHPAD_ONEBUTTON,
QUIRK_MODEL_TOUCHPAD_VISIBLE_MARKER,
QUIRK_MODEL_CYBORG_RAT,
QUIRK_MODEL_CHROMEBOOK,
QUIRK_MODEL_HP6910_TOUCHPAD,
QUIRK_MODEL_HP8510_TOUCHPAD,
QUIRK_MODEL_HP_PAVILION_DM4_TOUCHPAD,
QUIRK_MODEL_HP_STREAM11_TOUCHPAD,
QUIRK_MODEL_HP_ZBOOK_STUDIO_G3,
QUIRK_MODEL_TABLET_NO_PROXIMITY_OUT,
QUIRK_MODEL_LENOVO_SCROLLPOINT,
QUIRK_MODEL_LENOVO_X230,
QUIRK_MODEL_LENOVO_T450_TOUCHPAD,
QUIRK_MODEL_TABLET_MODE_NO_SUSPEND,
QUIRK_MODEL_LENOVO_CARBON_X1_6TH,
QUIRK_MODEL_TRACKBALL,
QUIRK_MODEL_LOGITECH_MARBLE_MOUSE,
QUIRK_MODEL_BOUNCING_KEYS,
QUIRK_MODEL_SYNAPTICS_SERIAL_TOUCHPAD,
QUIRK_MODEL_SYSTEM76_BONOBO,
QUIRK_MODEL_CLEVO_W740SU,
QUIRK_MODEL_SYSTEM76_GALAGO,
QUIRK_MODEL_SYSTEM76_KUDU,
QUIRK_MODEL_WACOM_TOUCHPAD,
};
bool b;
enum quirk *q;
assert(strneq(key, "Model", 5));
if (streq(value, "1"))
b = true;
else if (streq(value, "0"))
b = false;
else
return false;
ARRAY_FOR_EACH(quirks, q) {
if (streq(key, quirk_get_name(*q))) {
struct property *p = property_new();
p->id = *q,
p->type = PT_BOOL;
p->value.b = b;
list_append(&s->properties, &p->link);
s->has_property = true;
return true;
}
}
qlog_error(ctx, "Unknown key %s in %s\n", key, s->name);
return false;
}
/**
* Parse a AttrFooBar=banana line.
*
* @param section The section struct to be filled in
* @param key The AttrFooBar part of the line
* @param value The banana part of the line.
*
* Value parsing depends on the attribute type.
*
* @return true on success, false otherwise.
*/
static inline bool
parse_attr(struct quirks_context *ctx,
struct section *s,
const char *key,
const char *value)
{
struct property *p = property_new();
bool rc = false;
struct quirk_dimensions dim;
struct quirk_range range;
unsigned int v;
if (streq(key, quirk_get_name(QUIRK_ATTR_SIZE_HINT))) {
p->id = QUIRK_ATTR_SIZE_HINT;
if (!parse_dimension_property(value, &dim.x, &dim.y))
goto out;
p->type = PT_DIMENSION;
p->value.dim = dim;
rc = true;
} else if (streq(key, quirk_get_name(QUIRK_ATTR_TOUCH_SIZE_RANGE))) {
p->id = QUIRK_ATTR_TOUCH_SIZE_RANGE;
if (!parse_range_property(value, &range.upper, &range.lower))
goto out;
p->type = PT_RANGE;
p->value.range = range;
rc = true;
} else if (streq(key, quirk_get_name(QUIRK_ATTR_PALM_SIZE_THRESHOLD))) {
p->id = QUIRK_ATTR_PALM_SIZE_THRESHOLD;
if (!safe_atou(value, &v))
goto out;
p->type = PT_UINT;
p->value.u = v;
rc = true;
} else if (streq(key, quirk_get_name(QUIRK_ATTR_LID_SWITCH_RELIABILITY))) {
p->id = QUIRK_ATTR_LID_SWITCH_RELIABILITY;
if (!streq(value, "reliable") &&
!streq(value, "write_open"))
goto out;
p->type = PT_STRING;
p->value.s = safe_strdup(value);
rc = true;
} else if (streq(key, quirk_get_name(QUIRK_ATTR_KEYBOARD_INTEGRATION))) {
p->id = QUIRK_ATTR_KEYBOARD_INTEGRATION;
if (!streq(value, "internal") && !streq(value, "external"))
goto out;
p->type = PT_STRING;
p->value.s = safe_strdup(value);
rc = true;
} else if (streq(key, quirk_get_name(QUIRK_ATTR_TPKBCOMBO_LAYOUT))) {
p->id = QUIRK_ATTR_TPKBCOMBO_LAYOUT;
if (!streq(value, "below"))
goto out;
p->type = PT_STRING;
p->value.s = safe_strdup(value);
rc = true;
} else if (streq(key, quirk_get_name(QUIRK_ATTR_PRESSURE_RANGE))) {
p->id = QUIRK_ATTR_PRESSURE_RANGE;
if (!parse_range_property(value, &range.upper, &range.lower))
goto out;
p->type = PT_RANGE;
p->value.range = range;
rc = true;
} else if (streq(key, quirk_get_name(QUIRK_ATTR_PALM_PRESSURE_THRESHOLD))) {
p->id = QUIRK_ATTR_PALM_PRESSURE_THRESHOLD;
if (!safe_atou(value, &v))
goto out;
p->type = PT_UINT;
p->value.u = v;
rc = true;
} else if (streq(key, quirk_get_name(QUIRK_ATTR_RESOLUTION_HINT))) {
p->id = QUIRK_ATTR_RESOLUTION_HINT;
if (!parse_dimension_property(value, &dim.x, &dim.y))
goto out;
p->type = PT_DIMENSION;
p->value.dim = dim;
rc = true;
} else if (streq(key, quirk_get_name(QUIRK_ATTR_TRACKPOINT_RANGE))) {
p->id = QUIRK_ATTR_TRACKPOINT_RANGE;
if (!safe_atou(value, &v))
goto out;
p->type = PT_UINT;
p->value.u = v;
rc = true;
} else if (streq(key, quirk_get_name(QUIRK_ATTR_THUMB_PRESSURE_THRESHOLD))) {
p->id = QUIRK_ATTR_THUMB_PRESSURE_THRESHOLD;
if (!safe_atou(value, &v))
goto out;
p->type = PT_UINT;
p->value.u = v;
rc = true;
} else {
qlog_error(ctx, "Unknown key %s in %s\n", key, s->name);
}
out:
if (rc) {
list_append(&s->properties, &p->link);
s->has_property = true;
} else {
property_cleanup(p);
}
return rc;
}
/**
* Parse a single line, expected to be in the format Key=value. Anything
* else will be rejected with a failure.
*
* Our data files can only have Match, Model and Attr, so let's check for
* those too.
*/
static bool
parse_value_line(struct quirks_context *ctx, struct section *s, const char *line)
{
char **strv;
const char *key, *value;
bool rc = false;
strv = strv_from_string(line, "=");
if (strv[0] == NULL || strv[1] == NULL || strv[2] != NULL) {
goto out;
}
key = strv[0];
value = strv[1];
if (strlen(key) == 0 || strlen(value) == 0)
goto out;
/* Whatever the value is, it's not supposed to be in quotes */
if (value[0] == '"')
goto out;
if (strneq(key, "Match", 5))
rc = parse_match(ctx, s, key, value);
else if (strneq(key, "Model", 5))
rc = parse_model(ctx, s, key, value);
else if (strneq(key, "Attr", 4))
rc = parse_attr(ctx, s, key, value);
else
qlog_error(ctx, "Unknown value prefix %s\n", line);
out:
strv_free(strv);
return rc;
}
static inline bool
parse_file(struct quirks_context *ctx, const char *path)
{
enum state {
STATE_SECTION,
STATE_MATCH,
STATE_MATCH_OR_VALUE,
STATE_VALUE_OR_SECTION,
STATE_ANY,
};
FILE *fp;
char line[512];
bool rc = false;
enum state state = STATE_SECTION;
struct section *section = NULL;
int lineno = -1;
qlog_debug(ctx, "%s\n", path);
/* Not using open_restricted here, if we can't access
* our own data files, our installation is screwed up.
*/
fp = fopen(path, "r");
if (!fp) {
/* If the file doesn't exist that's fine. Only way this can
* happen is for the custom override file, all others are
* provided by scandir so they do exist. Short of races we
* don't care about. */
if (errno == ENOENT)
return true;
qlog_error(ctx, "%s: failed to open file\n", path);
goto out;
}
while (fgets(line, sizeof(line), fp)) {
char *comment;
lineno++;
comment = strstr(line, "#");
if (comment) {
/* comment points to # but we need to remove the
* preceding whitespaces too */
comment--;
while (comment >= line) {
if (*comment != ' ' && *comment != '\t')
break;
comment--;
}
*(comment + 1) = '\0';
} else { /* strip the trailing newline */
comment = strstr(line, "\n");
if (comment)
*comment = '\0';
}
if (strlen(line) == 0)
continue;
/* We don't use quotes for strings, so we really don't want
* erroneous trailing whitespaces */
switch (line[strlen(line) - 1]) {
case ' ':
case '\t':
qlog_parser(ctx,
"%s:%d: Trailing whitespace '%s'\n",
path, lineno, line);
goto out;
}
switch (line[0]) {
case '\0':
case '\n':
case '#':
break;
/* white space not allowed */
case ' ':
case '\t':
qlog_parser(ctx, "%s:%d: Preceding whitespace '%s'\n",
path, lineno, line);
goto out;
/* section title */
case '[':
if (line[strlen(line) - 1] != ']') {
qlog_parser(ctx, "%s:%d: Closing ] missing '%s'\n",
path, lineno, line);
goto out;
}
if (state != STATE_SECTION &&
state != STATE_VALUE_OR_SECTION) {
qlog_parser(ctx, "%s:%d: expected section before %s\n",
path, lineno, line);
goto out;
}
if (section &&
(!section->has_match || !section->has_property)) {
qlog_parser(ctx, "%s:%d: previous section %s was empty\n",
path, lineno, section->name);
goto out; /* Previous section was empty */
}
state = STATE_MATCH;
section = section_new(path, line);
list_append(&ctx->sections, §ion->link);
break;
/* entries must start with A-Z */
case 'A'...'Z':
switch (state) {
case STATE_SECTION:
qlog_parser(ctx, "%s:%d: expected [Section], got %s\n",
path, lineno, line);
goto out;
case STATE_MATCH:
if (!strneq(line, "Match", 5)) {
qlog_parser(ctx, "%s:%d: expected MatchFoo=bar, have %s\n",
path, lineno, line);
goto out;
}
state = STATE_MATCH_OR_VALUE;
break;
case STATE_MATCH_OR_VALUE:
if (!strneq(line, "Match", 5))
state = STATE_VALUE_OR_SECTION;
break;
case STATE_VALUE_OR_SECTION:
if (strneq(line, "Match", 5)) {
qlog_parser(ctx, "%s:%d: expected value or [Section], have %s\n",
path, lineno, line);
goto out;
}
break;
case STATE_ANY:
break;
}
if (!parse_value_line(ctx, section, line)) {
qlog_parser(ctx, "%s:%d: failed to parse %s\n",
path, lineno, line);
goto out;
}
break;
default:
qlog_parser(ctx, "%s:%d: Unexpected line %s\n",
path, lineno, line);
goto out;
}
}
if (!section) {
qlog_parser(ctx, "%s: is an empty file\n", path);
goto out;
}
if ((!section->has_match || !section->has_property)) {
qlog_parser(ctx, "%s:%d: previous section %s was empty\n",
path, lineno, section->name);
goto out; /* Previous section was empty */
}
rc = true;
out:
if (fp)
fclose(fp);
return rc;
}
static int
is_data_file(const struct dirent *dir) {
const char *suffix = ".quirks";
const int slen = strlen(suffix);
int offset;
offset = strlen(dir->d_name) - slen;
if (offset <= 0)
return 0;
return strneq(&dir->d_name[offset], suffix, slen);
}
static inline bool
parse_files(struct quirks_context *ctx, const char *data_path)
{
struct dirent **namelist;
int ndev = -1;
int idx = 0;
ndev = scandir(data_path, &namelist, is_data_file, versionsort);
if (ndev <= 0) {
qlog_error(ctx,
"%s: failed to find data files\n",
data_path);
return false;
}
for (idx = 0; idx < ndev; idx++) {
char path[PATH_MAX];
snprintf(path,
sizeof(path),
"%s/%s",
data_path,
namelist[idx]->d_name);
if (!parse_file(ctx, path))
break;
}
for (int i = 0; i < ndev; i++)
free(namelist[i]);
free(namelist);
return idx == ndev;
}
struct quirks_context *
quirks_init_subsystem(const char *data_path,
const char *override_file,
libinput_log_handler log_handler,
struct libinput *libinput,
enum quirks_log_type log_type)