summaryrefslogtreecommitdiff
path: root/test/mtdev-kernel.c
diff options
context:
space:
mode:
authorHenrik Rydberg <rydberg@euromail.se>2010-11-07 17:56:56 +0100
committerHenrik Rydberg <rydberg@euromail.se>2010-11-07 17:56:56 +0100
commit868f53c8a5684524bc6b1e7cd39aabed56fad9df (patch)
tree8897e21f91bb1679d7b3f6c5f2779fd7ef12670c /test/mtdev-kernel.c
parent561af7f537b80f3952ab228bcc992ad75ced280b (diff)
Generate kernel matcher data
This application generates the matcher tables of the tracking library in the linux kernel. Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Diffstat (limited to 'test/mtdev-kernel.c')
-rw-r--r--test/mtdev-kernel.c158
1 files changed, 158 insertions, 0 deletions
diff --git a/test/mtdev-kernel.c b/test/mtdev-kernel.c
new file mode 100644
index 0000000..1f33c3e
--- /dev/null
+++ b/test/mtdev-kernel.c
@@ -0,0 +1,158 @@
1/*****************************************************************************
2 *
3 * mtdev - Multitouch Protocol Translation Library (MIT license)
4 *
5 * Copyright (C) 2010 Henrik Rydberg <rydberg@euromail.se>
6 * Copyright (C) 2010 Canonical Ltd.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
17 * Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
26 *
27 ****************************************************************************/
28
29#include <../src/common.h>
30#include <stdio.h>
31#include <time.h>
32
33/*
34 * Combinatorial formulation
35 *
36 * x_ij = 1 if slot i and contact j are connected, zero otherwise
37 *
38 * sum_i x_ij <= 1 for all j; each contact picks at most one slot
39 *
40 * sum_j x_ij <= 1 for all i; each slot is picked by at most one contact
41 *
42 * sum_ij x_ij == min(nslot, npos); assign every contact possible
43 *
44 * Arrange x_ij as a bitmask; x_00 x_01 x_02.. x_10 x_11 x_12...
45 *
46 * Up to five slots, this is readily enumerable.
47 */
48
49#define SLOT_MAX 5
50#define SLOT_CNT (SLOT_MAX + 1)
51
52static int illegal(int nslot, int npos, unsigned x)
53{
54 int i, j, sum;
55
56 for (j = 0; j < npos; j++) {
57 sum = 0;
58 for (i = 0; i < nslot; i++)
59 sum += GETBIT(x, i * npos + j);
60 if (sum > 1)
61 return 1;
62 }
63 for (i = 0; i < nslot; i++) {
64 sum = 0;
65 for (j = 0; j < npos; j++)
66 sum += GETBIT(x, i * npos + j);
67 if (sum > 1)
68 return 1;
69 }
70
71 sum = bitcount(x);
72 return sum != minval(nslot, npos);
73}
74
75static void get_slots(int *slots, int nslot, int npos, unsigned x)
76{
77 int i;
78
79 memset(slots, -1, sizeof(slots[0]) * npos);
80 for (i = 0; i < nslot * npos; i++)
81 if (GETBIT(x, i))
82 slots[i % npos] = i / npos;
83 for (i = 0; i < npos; i++)
84 if (slots[i] < 0)
85 slots[i] = nslot++;
86}
87
88static int generate_assignments(int nslot, int npos)
89{
90 static int ncol;
91 unsigned x, nx = BITMASK(nslot * npos);
92 int slots[SLOT_MAX];
93 int i, n = 0;
94
95 for (x = 0; x < nx; x++) {
96 if (illegal(nslot, npos, x))
97 continue;
98 for (i = 0; i < nslot * npos; i++) {
99 if (GETBIT(x, i)) {
100 if (ncol++ % 16 == 0)
101 printf("\n\t%d,", i);
102 else
103 printf(" %d,", i);
104 n++;
105 }
106 }
107 get_slots(slots, nslot, npos, x);
108 for (i = 0; i < npos; i++) {
109 if (ncol++ % 16 == 0)
110 printf("\n\t%d,", slots[i]);
111 else
112 printf(" %d,", slots[i]);
113 n++;
114 }
115 }
116
117 return n;
118}
119
120int main(int argc, char *argv[])
121{
122 int ix[SLOT_CNT][SLOT_CNT], nix = 0;
123 int eslot, i, j;
124
125 if (argc < 2) {
126 fprintf(stderr, "usage: %s <num_slots>\n", argv[0]);
127 return 1;
128 }
129
130 eslot = atoi(argv[1]) + 1;
131 if (eslot > SLOT_CNT) {
132 fprintf(stderr, "allowed slot range: 2 - %d\n", SLOT_MAX);
133 return 1;
134 }
135
136 printf("\n/* generated by mtdev-kernel - do not edit */\n");
137 printf("static const u8 match_data[] = {");
138 for (i = 0; i < eslot; i++) {
139 for (j = 0; j < eslot; j++) {
140 ix[i][j] = nix;
141 nix += generate_assignments(i, j);
142 }
143 }
144 printf("\n};\n");
145
146 printf("\n/* generated by mtdev-kernel - do not edit */\n");
147 printf("static const int match_index[][%d] = {\n", eslot);
148 for (i = 0; i < eslot; i++) {
149 printf("\t{");
150 for (j = 0; j < eslot; j++)
151 printf(" %d%s", ix[i][j], j < eslot - 1 ? "," : "");
152 printf(" },\n");
153 }
154 printf("\t{ %d }\n", nix);
155 printf("};\n");
156
157 return 0;
158}