• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "linux/module.h"
2 
3 #define	GESTURE_LICH	1
4 
5 #ifndef GSL_VERSION
6 #define GSL_VERSION		0x20140829
7 #endif
8 #ifndef NULL
9 #define	NULL  ((void *)0)
10 #endif
11 #ifndef UINT
12 #define	UINT  unsigned int
13 #endif
14 
15 #define	POINT_MAX		10
16 #define	PP_DEEP			10
17 #define	PS_DEEP			10
18 #define PR_DEEP			10
19 #define POINT_DEEP		(PP_DEEP + PS_DEEP + PR_DEEP)
20 #define	PRESSURE_DEEP		8
21 #define	CONFIG_LENGTH		512
22 #define TRUE			1
23 #define FALSE			0
24 #define FLAG_ABLE		(0x4 << 12)
25 #define FLAG_FILL		(0x2 << 12)
26 #define	FLAG_KEY		(0x1 << 12)
27 #define	FLAG_COOR		(0x0fff0fff)
28 #define	FLAG_COOR_EX		(0xffff0fff)
29 #define	FLAG_ID			(0xf0000000)
30 
31 struct gsl_touch_info {
32 	int x[10];
33 	int y[10];
34 	int id[10];
35 	int finger_num;
36 };
37 
38 typedef struct {
39 	unsigned int i;
40 	unsigned int j;
41 	unsigned int min;
42 	unsigned int d[POINT_MAX][POINT_MAX];
43 } gsl_DISTANCE_TYPE;
44 
45 typedef union {
46 	struct {
47 		unsigned y:12;
48 		unsigned key:1;
49 		unsigned fill:1;
50 		unsigned able:1;
51 		unsigned predict:1;
52 		unsigned x:16;
53 	};
54 	struct {
55 		unsigned y:13;
56 		unsigned rev_2:3;
57 		unsigned x:16;
58 	} dis;
59 	unsigned int all;
60 } gsl_POINT_TYPE;
61 
62 typedef union {
63 	struct {
64 		unsigned delay:8;
65 		unsigned report:8;
66 		unsigned rev_1:14;
67 		unsigned able:1;
68 		unsigned init:1;
69 	};
70 	unsigned int all;
71 } gsl_DELAY_TYPE;
72 
73 typedef union {
74 	struct {
75 		unsigned rev_0:8;
76 		unsigned rev_1:8;
77 
78 		unsigned rev_2:7;
79 		unsigned ex:1;
80 
81 		unsigned interpolation:4;
82 		unsigned rev_3:1;
83 		unsigned only:1;
84 		unsigned mask:1;
85 		unsigned reset:1;
86 	};
87 	unsigned int all;
88 } gsl_STATE_TYPE;
89 
90 typedef struct {
91 	unsigned int rate;
92 	unsigned int dis;
93 	gsl_POINT_TYPE coor;
94 } gsl_EDGE_TYPE;
95 
96 typedef union {
97 	struct {
98 		short y;
99 		short x;
100 	};
101 	unsigned int all;
102 } gsl_DECIMAL_TYPE;
103 
104 typedef union {
105 	struct {
106 		unsigned over_report_mask:1;
107 		unsigned opposite_x:1;
108 		unsigned opposite_y:1;
109 		unsigned opposite_xy:1;
110 		unsigned line:1;
111 		unsigned line_neg:1;
112 		unsigned line_half:1;
113 		unsigned middle_drv:1;
114 
115 		unsigned key_only_one:1;
116 		unsigned key_line:1;
117 		unsigned refe_rt:1;
118 		unsigned refe_var:1;
119 		unsigned base_median:1;
120 		unsigned key_rt:1;
121 		unsigned refe_reset:1;
122 		unsigned sub_cross:1;
123 
124 		unsigned row_neg:1;
125 		unsigned sub_line_coe:1;
126 		unsigned sub_row_coe:1;
127 		unsigned c2f_able:1;
128 		unsigned thumb:1;
129 		unsigned graph_h:1;
130 		unsigned init_repeat:1;
131 		unsigned near_reset_able:1;
132 
133 		unsigned emb_dead:1;
134 		unsigned emb_point_mask:1;
135 		unsigned interpolation:1;
136 		unsigned sum2_able:1;
137 		unsigned reduce_pin:1;
138 		unsigned drv_order_ex:1;
139 		unsigned id_over:1;
140 		unsigned rev_1:1;
141 	};
142 	unsigned int all;
143 } gsl_FLAG_TYPE;
144 
145 static gsl_POINT_TYPE point_array[POINT_DEEP][POINT_MAX];
146 static gsl_POINT_TYPE *point_pointer[PP_DEEP];
147 static gsl_POINT_TYPE *point_stretch[PS_DEEP];
148 static gsl_POINT_TYPE *point_report[PR_DEEP];
149 static gsl_POINT_TYPE point_now[POINT_MAX];
150 static gsl_DELAY_TYPE point_delay[POINT_MAX];
151 static int filter_deep[POINT_MAX];
152 static gsl_EDGE_TYPE point_edge;
153 static gsl_DECIMAL_TYPE point_decimal[POINT_MAX];
154 
155 static unsigned int pressure_now[POINT_MAX];
156 static unsigned int pressure_array[PRESSURE_DEEP][POINT_MAX];
157 static unsigned int pressure_report[POINT_MAX];
158 static unsigned int *pressure_pointer[PRESSURE_DEEP];
159 
160 #define	pp							point_pointer
161 #define	ps							point_stretch
162 #define	pr							point_report
163 #define	point_predict						pp[0]
164 #define	pa							pressure_pointer
165 
166 static	gsl_STATE_TYPE global_state;
167 static	int inte_count;
168 static	unsigned int csensor_count;
169 static	unsigned int click_count[4];
170 static	gsl_POINT_TYPE point_click[4];
171 static	unsigned int double_click;
172 static	int point_n;
173 static	int point_num;
174 static	int prev_num;
175 static	int point_near;
176 static	unsigned int point_shake;
177 static	unsigned int reset_mask_send;
178 static	unsigned int reset_mask_max;
179 static	unsigned int reset_mask_count;
180 static	gsl_FLAG_TYPE global_flag;
181 static	unsigned int id_first_coe;
182 static	unsigned int id_speed_coe;
183 static	unsigned int id_static_coe;
184 static	unsigned int average;
185 static	unsigned int soft_average;
186 static	unsigned int report_delay;
187 static	unsigned int report_ahead;
188 static	unsigned char median_dis[4];
189 static	unsigned int shake_min;
190 static	int match_y[2];
191 static	int match_x[2];
192 static	int ignore_y[2];
193 static	int ignore_x[2];
194 static	int screen_y_max;
195 static	int screen_x_max;
196 static	int point_num_max;
197 static	unsigned int drv_num;
198 static	unsigned int sen_num;
199 static	unsigned int drv_num_nokey;
200 static	unsigned int sen_num_nokey;
201 static	unsigned int coordinate_correct_able;
202 static	unsigned int coordinate_correct_coe_x[64];
203 static	unsigned int coordinate_correct_coe_y[64];
204 static	unsigned int edge_cut[4];
205 static	unsigned int stretch_array[4*4*2];
206 static	unsigned int shake_all_array[2*8];
207 static	unsigned int reset_mask_dis;
208 static	unsigned int reset_mask_type;
209 static	unsigned int key_map_able;
210 static	unsigned int key_range_array[8*3];
211 static	int  filter_able;
212 static	unsigned int filter_coe[4];
213 static	unsigned int multi_x_array[4], multi_y_array[4];
214 static	unsigned int multi_group[4][64];
215 static	int ps_coe[4][8], pr_coe[4][8];
216 static	int point_repeat[2];
217 static	int near_set[2];
218 static	int diagonal;
219 static	int point_extend;
220 /* unsigned int key_dead_time;
221  * unsigned int point_dead_time;
222  * unsigned int point_dead_time2;
223  * unsigned int point_dead_distance;
224  * unsigned int point_dead_distance2;
225  * unsigned int pressure_able;
226  * unsigned int pressure_save[POINT_MAX];
227  */
228 static	unsigned int edge_first;
229 static	unsigned int edge_first_coe;
230 
231 static	unsigned int point_corner;
232 
233 static	unsigned int config_static[CONFIG_LENGTH];
234 
235 #ifdef GESTURE_LICH
236 #define	GESTURE_BUF_SIZE		256
237 #define	GESTURE_SIZE_REFE		255
238 #define	GESTURE_SIZE_NUM		32
239 #define	GESTURE_XY			0x1
240 #define	GESTURE_DEAL			0x2
241 #define	GESTURE_LRUD			0x4
242 #define	GESTURE_ALL			0x7fffffff
243 typedef union {
244 	struct {
245 		unsigned y:12;
246 		unsigned rev:4;
247 		unsigned x:16;
248 	};
249 	unsigned int all;
250 } GESTURE_POINT_TYPE;
251 typedef struct {
252 	int coe;
253 	int out;
254 	unsigned int coor[GESTURE_SIZE_NUM/2];
255 } GESTURE_MODEL_TYPE;
256 
257 #define	gesture_buf		((GESTURE_POINT_TYPE *)config_static)
258 #define	gesture_standard	((GESTURE_POINT_TYPE *)(&config_static[GESTURE_BUF_SIZE]))
259 static int  gesture_num, gesture_num_last;
260 static int  gesture_dis_min;
261 static int  gesture_deal;
262 static int  gesture_last = 0, gesture_value;
263 static int  gesture_threshold[2];
264 static  int x_scale;
265 static  int y_scale;
266 static int double_down, double_up;
267 typedef struct {
268 	int dis;
269 	int count;
270 } GESTURE_MULTI_TYPE;
271 static GESTURE_MULTI_TYPE multi_x[5];
272 static GESTURE_MULTI_TYPE multi_o[5];
273 static int gesture_multi;
274 static unsigned int multi_set;
275 static unsigned int gesture_flag;
276 static const GESTURE_MODEL_TYPE *model_extern;
277 static int model_extern_len;
278 static int GestureSqrt(int d);
279 static int GestureDistance(GESTURE_POINT_TYPE *d1, GESTURE_POINT_TYPE *d2,
280 								int sqrt_able);
281 static int GesturePush(GESTURE_POINT_TYPE *data);
282 static int GestureStretch(void);
283 static int GestureLength(void);
284 static int GestureDeal(void);
285 static int GestureModel(const GESTURE_MODEL_TYPE *model, int len,
286 						int threshold, int *out);
287 static int GestureMain(unsigned int data[], unsigned int pn);
288 static void GestureStandard(void);
289 static void GestureInit(void);
290 static void ChangeXY(void);
291 static int GestureLRUD(void);
292 static void GestureSet(unsigned int conf[]);
293 static int GestureMulti(unsigned int *data_in);
294 static void GestureOrientation(int ori);
295 static int GestureJoint(void);
296 static unsigned int (*ReadIICInt)(unsigned int *data, unsigned int addr,
297 							unsigned int len);
298 static const GESTURE_MODEL_TYPE model_default[] = {
299 		{0x10, '3', {
300 		0x37170105, 0x78580000, 0xba990a03, 0xedd92e14, 0xb9d85347,
301 		0x7798655b, 0x3657716b, 0x1f156d74, 0x60406969, 0xa2816f69,
302 		0xe3c28075, 0xf9fbb899, 0xc3e3e0d2, 0x83a4f6ee, 0x4262fffc,
303 		0x0021f7fd,} },
304 		{0x10, '6', {
305 		0xa2be0400, 0x70881f10, 0x4258402e, 0x1d2e6c54, 0x040ea084,
306 		0x0a01d6bc, 0x381df9ec, 0x7054fffd, 0xa88cfafe, 0xdac2ddef,
307 		0xfff0b2cb, 0xe2f78497, 0xaac7747a, 0x728e7472, 0x3b56817b,
308 		0x0420968b,} },
309 		{0x10, '7', {
310 		0x12000001, 0x37240000, 0x5b490000, 0x806e0000, 0xa5930000,
311 		0xcab70000, 0xefdc0300, 0xf9fd1f0e, 0xe2ee3d30, 0xc5d4564a,
312 		0xa7b76c61, 0x8c9a8579, 0x717e9f93, 0x5863bbad, 0x434cdbc9,
313 		0x3c3dffec,} },
314 		{0x10, '8', {
315 		0xdaff030c, 0x8eb40000, 0x41670c06, 0x001c3116, 0x431e5448,
316 		0x8f69635d, 0xd1b58a6f, 0xcedfd0af, 0x88acf5e6, 0x3c62fffd,
317 		0x0718cdf1, 0x341493aa, 0x7a556d7d, 0xc19e4f60, 0xf9e51c3c,
318 		0xb5dc0005,} },
319 		{0x10, '8', {
320 		0x627d231e, 0x2f49382c, 0x03175a48, 0x21098172, 0x563c958c,
321 		0x856eb0a2, 0x8f99dac4, 0x5b76eee5, 0x243ffdf5, 0x090ddbf4,
322 		0x2918acc2, 0x4d3a8497, 0x78636172, 0xa38e4050, 0xd0ba1f2e,
323 		0xffe7000f,} },
324 		{0x10, '9', {
325 		0xe8ff0715, 0xb4ce0001, 0x819a0500, 0x4f68150c, 0x1e362a1e,
326 		0x000c543c, 0x270d7169, 0x5b417273, 0x9076666d, 0xbda74a5a,
327 		0xddcf1e36, 0xc8d7321b, 0xb4be634b, 0xa4ac967d, 0x959ccab0,
328 		0x898fffe4,} },
329 		{0x10, 'A', {
330 		0xaeca000b, 0x74900e02, 0x41582d1b, 0x182a5942, 0x02099375,
331 		0x0600cfb1, 0x2c15fcea, 0x664af1fe, 0x957ec8dd, 0xb5a894b0,
332 		0xc9bf5876, 0xd7d31c3a, 0xd4d75134, 0xd3d38d6f, 0xdbd4c9ab,
333 		0xffe9fce6,} },
334 		{0x10, 'A', {
335 		0x8eab0102, 0x56711307, 0x2c3f3a25, 0x0e1b6b51, 0x0004a689,
336 		0x0e02ddc2, 0x3e22fbf2, 0x725be8fa, 0x9284b6d0, 0xa69d7f9b,
337 		0xb3ae4562, 0xb7b80b28, 0xa7a6290c, 0xb1aa6346, 0xd0be947d,
338 		0xffe7bba7,} },
339 		{0x10, 'B', {
340 		0x56591a00, 0x474e4e35, 0x343f8168, 0x242cb59c, 0x0f1be7ce,
341 		0x170ddbf4, 0x3c25b4c4, 0x6e549ca6, 0xa3889799, 0xd8bd9d96,
342 		0xfcf1bea8, 0xd3e9e4d4, 0xa0baf6f0, 0x6b85fcfa, 0x3650fffd,
343 		0x001bfbff,} },
344 		{0x10, 'C', {
345 		0xfaff2337, 0xdaec0913, 0xb0c50003, 0x879c0500, 0x5f720f09,
346 		0x3b4c271a, 0x1d2b4534, 0x08116a56, 0x0003937f, 0x0a03bca8,
347 		0x2515ddce, 0x4b38f2e8, 0x7560fff9, 0x9e89f7fd, 0xc6b2e8f0,
348 		0xeed9d7df,} },
349 		{0x10, 'C', {
350 		0xacbf0100, 0x86990a04, 0x64751b12, 0x45533225, 0x2b375141,
351 		0x17217160, 0x080f9582, 0x0103bba8, 0x0200e2cf, 0x200ff9f1,
352 		0x4633fefc, 0x6c59ffff, 0x9380fcfd, 0xb9a6f4f9, 0xdccbe4ed,
353 		0xffeeceda,} },
354 		{0x10, 'C', {
355 		0x57670a00, 0x3a492116, 0x222d3d2e, 0x0f175e4d, 0x0408816f,
356 		0x0001a693, 0x0300cab8, 0x0e07eddc, 0x2f1dfefb, 0x5241f5fc,
357 		0x7362e2ec, 0x8e80c9d6, 0xa89bafbc, 0xc3b594a1, 0xe1d27e89,
358 		0xfff06673,} },
359 		{0x10, 'D', {
360 		0x99b5858f, 0x5f7c8883, 0x28429c8f, 0x010fc6ab, 0x240cf4e1,
361 		0x5d41fefb, 0x957af1fc, 0xc1adcbe0, 0xd2cc92af, 0xd3d25875,
362 		0xd7d71f3b, 0xd7d71b02, 0xd4d75538, 0xd4d48f72, 0xe1d9c9ac,
363 		0xffe6f4e6,} },
364 		{0x10, 'E', {
365 		0x391c948f, 0x73569595, 0xad908a92, 0xddc8677e, 0xf1ee304d,
366 		0xc3dd0d1b, 0x89a70002, 0x536d1304, 0x233b3a25, 0x08137053,
367 		0x0301aa8d, 0x220edcc6, 0x573af7ee, 0x9174fffc, 0xcaaef6ff,
368 		0xffe5dbeb,} },
369 		{0x10, 'G', {
370 		0xaaca0000, 0x698a0000, 0x2a491106, 0x000f4226, 0x23067061,
371 		0x64437674, 0xa3836874, 0xdac04759, 0xfaec0b2a, 0xfefb401f,
372 		0xffff8160, 0xf5fdc0a1, 0xc9e9eedf, 0x89a9fff9, 0x4869faff,
373 		0x0928e3f3,} },
374 		{0x10, 'G', {
375 		0xeaff1421, 0xb9d20308, 0x88a00000, 0x57700f05, 0x2b3f2618,
376 		0x09174d37, 0x00037f66, 0x0d05af97, 0x2a1adac7, 0x5940ede5,
377 		0x8b72f2f3, 0xbca4e9ee, 0xe4d2cbde, 0xfbf09cb3, 0xf5f8ceb5,
378 		0xe9f1ffe6,} },
379 		{0x10, 'H', {
380 		0x03021300, 0x06053a26, 0x0b0a604d, 0x0b0b8774, 0x0a0bae9a,
381 		0x0506d4c1, 0x0002fbe8, 0x1104e0f0, 0x2e1ec3d1, 0x503dadb5,
382 		0x7764a5a7, 0x9e8aa1a2, 0xc4b1a3a0, 0xead8ada8, 0xfff8d1bd,
383 		0xfffff8e4,} },
384 		{0x10, 'K', {
385 		0x1d1a2000, 0x171a6040, 0x1114a080, 0x060edfc0, 0x1100e2ff,
386 		0x3420a8c5, 0x6f4f8b95, 0xaf8f8285, 0xefcf8683, 0xe1fcb3a0,
387 		0xa0c1c2bb, 0x6080c6c5, 0x2c40c9c7, 0x6c4cd8d1, 0xac8ceadf,
388 		0xedccfef3,} },
389 		{0x10, 'K', {
390 		0x22341900, 0x15185436, 0x0e119072, 0x0c0cccae, 0x0709f6ea,
391 		0x0a07b9d8, 0x2918859e, 0x5b406170, 0x90796658, 0x627c8b7c,
392 		0x2a47a79a, 0x110db8b1, 0x4d2fbfbc, 0x896bcac4, 0xc3a6d9d1,
393 		0xffe1ede2,} },
394 		{0x10, 'L', {
395 		0x3f4a0c00, 0x35372c1c, 0x2c314d3d, 0x26296e5d, 0x1b218e7e,
396 		0x1316af9f, 0x0910cfc0, 0x0004f1e0, 0x1605ffff, 0x3727ffff,
397 		0x5848ffff, 0x7a69fdff, 0x9b8afcfd, 0xbcabfcfc, 0xddcdfafb,
398 		0xffeef9f9,} },
399 		{0x10, 'M', {
400 		0x0900e0ff, 0x2017a0c0, 0x3a296381, 0x4e442443, 0x5a583010,
401 		0x6b5f6f4f, 0x7471ae8f, 0x7977eece, 0x8c80c5e5, 0xa19886a5,
402 		0xbaad4766, 0xd3c70a29, 0xddda3516, 0xe7e17555, 0xf4f0b494,
403 		0xfffaf4d4,} },
404 		{0x10, 'M', {
405 		0x0e00421a, 0x1414936a, 0x1416e3bb, 0x1813c0e8, 0x201b6f97,
406 		0x2f261e47, 0x664d2c09, 0x7a737c54, 0x7c7bcda5, 0x7e7accf5,
407 		0x8b847ba4, 0x9c932b53, 0xd5b30d09, 0xece25d35, 0xfaf5ae85,
408 		0xfffdffd6,} },
409 		{0x10, 'M'+0x100, {
410 		0xf1ffe0ff, 0xdce8a2c0, 0xc8d16282, 0xbbc12343, 0xaab22a0b,
411 		0x9fa46a4a, 0x9298a989, 0x808ae8c9, 0x6f74bfde, 0x61697f9f,
412 		0x555b4060, 0x464f0120, 0x363d3f1f, 0x282f7e5e, 0x151fbc9e,
413 		0x000afcdc,} },
414 		{0x10, 'N', {
415 		0x0400e7ff, 0x130bb8cf, 0x281e89a1, 0x38305a71, 0x51452c43,
416 		0x675d1d13, 0x68684e36, 0x6b697f66, 0x726fb097, 0x7875e0c8,
417 		0x907ee8f8, 0xa79ebbd2, 0xbfb38fa4, 0xd1c95f77, 0xe6da3148,
418 		0xfff20019,} },
419 		{0x0c, 'O', {
420 		0x2e3f311f, 0x101e5c46, 0x03088f76, 0x0001c2a8, 0x1e08e7da,
421 		0x4f35fdf4, 0x8168fcff, 0xb39beef5, 0xdac7cedf, 0xf3e9a0b8,
422 		0xfef96d87, 0xf9ff3c54, 0xdaec1326, 0xaac30108, 0x77900100,
423 		0x465e1407,} },
424 		{0x0c, 'O', {
425 		0x8670020b, 0xb8a01307, 0xe3ce3423, 0xf8f0664c, 0xfffc9b81,
426 		0xf1faceb5, 0xcee4f4e5, 0x9ab4fffb, 0x657fffff, 0x364ceaf8,
427 		0x1623c0d7, 0x00098ea7, 0x06015973, 0x24122e41, 0x4d380c1c,
428 		0x82670104,} },
429 		{0x0c, 'O', {
430 		0xd0e30213, 0x9cb60000, 0x68820c05, 0x384f2416, 0x17254c36,
431 		0x040c8066, 0x0001b49a, 0x1305e4ce, 0x442bfaf0, 0x785efffe,
432 		0xab92f1f9, 0xd3c1cfe3, 0xf1e3a1b9, 0xfffa6e88, 0xf8ff3b54,
433 		0xd5e81024,} },
434 		{0x0c, 'O', {
435 		0x000f768a, 0x0900455d, 0x2b171e30, 0x5a420611, 0x8d740100,
436 		0xbca5170b, 0xe3d23824, 0xfcf2644c, 0xfaff977e, 0xe3f0c4af,
437 		0xbdd2e6d7, 0x8ea7faf2, 0x5b74fefe, 0x2e44e9f6, 0x0a1bc2d6,
438 		0x02028fa9,} },
439 		{0x0c, 'O', {
440 		0x040e8a71, 0x0b03bca4, 0x2c19e7d4, 0x5d43fef5, 0x9077f9fe,
441 		0xbea8deee, 0xdfcfb6cb, 0xf7ed859f, 0xfefd516b, 0xe6f62237,
442 		0xb7d00812, 0x839d0002, 0x4f690701, 0x20371f11, 0x040e4e34,
443 		0x00028268,} },
444 		{0x0c, 'O', {
445 		0x829c0900, 0x4e682315, 0x24384a34, 0x08157c61, 0x0002b598,
446 		0x0d03edd1, 0x4326fffd, 0x7a5feef8, 0xab93cfe0, 0xd5c1a7bd,
447 		0xf4e67690, 0xfbff3d5a, 0xcfec1b26, 0x96b31818, 0x5e7a251d,
448 		0x28433b2f,} },
449 		{0x0c, 'O', {
450 		0x381e5e68, 0x6e535156, 0xa388504f, 0xd8be5e56, 0xf9ed876d,
451 		0xf9fcbaa1, 0xd8ece5d2, 0xa5c0f9f2, 0x708bfffd, 0x3b55fbfe,
452 		0x1423d7ed, 0x0006a4bf, 0x09027089, 0x26154157, 0x50391e2e,
453 		0x7e670010,} },
454 		{0x10, 'Q', {
455 		0xc9e4020f, 0x8dab0000, 0x516f0802, 0x18341f11, 0x02065537,
456 		0x35186f68, 0x71536b6f, 0xaa8e5963, 0xdec53a4b, 0xfef40924,
457 		0xe1ef3e24, 0xc0d07157, 0xabb4ac8e, 0x9da4e8ca, 0xc5aaeffd,
458 		0xfde0d6e2,} },
459 		{0x10, 'S', {
460 		0xb7cf0001, 0x869e0301, 0x556d0905, 0x273e1f12, 0x0311442c,
461 		0x2009665a, 0x5138726b, 0x826a7876, 0xb39b807b, 0xdfcb998b,
462 		0xfff5bea7, 0xdcf2ded0, 0xadc6f1e8, 0x7c94fbf7, 0x4b63fffd,
463 		0x1932ffff,} },
464 		{0x10, 'S', {
465 		0xcbde0200, 0xa8ba1209, 0x8597241a, 0x6a753f2f, 0x806c5751,
466 		0xa6935e5b, 0xccb96662, 0xf2e0746b, 0xfcff9482, 0xe0f0b2a6,
467 		0xbdcfc2bb, 0x97abd1c9, 0x7385ddd7, 0x4c60e8e3, 0x273af4ee,
468 		0x0014fff9,} },
469 		{0x10, 'U', {
470 		0x050d2209, 0x0001573c, 0x03008c71, 0x1106bfa6, 0x2f1bebda,
471 		0x604ae1f0, 0x8873bccd, 0xa59990a8, 0xbcb05f78, 0xcdc72c46,
472 		0xd0d00911, 0xc9cb3e24, 0xc6c87359, 0xc9c6a88d, 0xd8d2ddc2,
473 		0xffe4fff6,} },
474 		{0x10, 'V', {
475 		0x09000f00, 0x1911301f, 0x27205240, 0x342d7563, 0x413a9785,
476 		0x4f47b9a8, 0x6057d9c9, 0x7569f9ea, 0x9486f4ff, 0xa99fd5e5,
477 		0xb8b0b4c5, 0xc9c093a3, 0xdbd17484, 0xe9e35263, 0xf5ef2f41,
478 		0xfff90b1d,} },
479 		{0x10, 'V', {
480 		0x07008274, 0x170f9d90, 0x271fb6aa, 0x382fd0c2, 0x463febdd,
481 		0x534df7fa, 0x635bdbe9, 0x716ac1ce, 0x827aa7b4, 0x928a8e9a,
482 		0xa39a7682, 0xb6ac5e6a, 0xc8bf4753, 0xdad1303b, 0xede41924,
483 		0xfff4000d,} },
484 		{0x10, 'W', {
485 		0x06001f00, 0x110c5f3f, 0x1c189f7f, 0x2822debe, 0x4131e3fd,
486 		0x554ba4c3, 0x655c6484, 0x786f2444, 0x847f2f0f, 0x8a866f4f,
487 		0x928eae8f, 0x9e99eece, 0xbaacd0ee, 0xd5c893b3, 0xebe05373,
488 		0xfff61333,} },
489 		{0x10, 'W', {
490 		0x0c00240b, 0x2015563d, 0x332b8970, 0x4b416b84, 0x5b533851,
491 		0x6a63061f, 0x78722d14, 0x817c5f46, 0x8b869279, 0x9791c4ab,
492 		0xa39ef7de, 0xb6aed5ee, 0xc6bea2bb, 0xd8cf7089, 0xede33d56,
493 		0xfff60a24,} },
494 		{0x10, 'W', {
495 		0x0d003219, 0x1e15654b, 0x2c24987e, 0x3732cbb1, 0x453dfee4,
496 		0x564dcde7, 0x675d9bb4, 0x7b706882, 0x9084354f, 0xa39b021c,
497 		0xada83117, 0xbab2644a, 0xc8c0977d, 0xded47791, 0xf1e7445e,
498 		0xfff7112b,} },
499 		{0x10, 'W', {
500 		0x00002d0b, 0x0001714f, 0x0902b593, 0x2816f3d4, 0x5844d4f1,
501 		0x796997b6, 0x8a845375, 0x858a0f31, 0x7f804a28, 0x7e7e8e6c,
502 		0x8a82d2b0, 0xb596fcf4, 0xe4d0cae7, 0xfcf489ab, 0xfffe4567,
503 		0xf9fd0023,} },
504 		{0x10, 'W'+0x100, {
505 		0xf7ff2000, 0xe4ed6040, 0xd5dba181, 0xbdcbe0c0, 0xa0aad3f3,
506 		0x909892b3, 0x848a5272, 0x80811131, 0x777d3d1d, 0x636d7e5e,
507 		0x535bbf9e, 0x3c48fdde, 0x272fc1e2, 0x121a82a2, 0x030a4161,
508 		0x00020021,} },
509 		{0x10, 'Y', {
510 		0x16000b13, 0x442d0303, 0x4b48341b, 0x61505e4b, 0x91795c62,
511 		0xbca74551, 0xe1d02637, 0xfcf40c14, 0xe8f13922, 0xdbe16951,
512 		0xd6d99a82, 0xced3cbb2, 0xb0c4f0e1, 0x7f98fbf7, 0x4e67fefd,
513 		0x1d36fdff,} },
514 		{0x10, 'Z', {
515 		0x30160200, 0x644a0403, 0x997f0303, 0xcdb30202, 0xe7e80d00,
516 		0xc4d63622, 0x9bb05c4a, 0x73857f6c, 0x5061a893, 0x293eccbb,
517 		0x0013f2dd, 0x2d13fcfd, 0x6248f6f8, 0x967cf1f3, 0xcbb1edef,
518 		0xffe5f1ec,} },
519 		{0x10, '<', {
520 		0xf0ff0b00, 0xd1e11c14, 0xb0c02c24, 0x90a13c33, 0x71804c44,
521 		0x51605c54, 0x2f406d64, 0x0d1e7c75, 0x16058d86, 0x38279b94,
522 		0x5747a9a2, 0x7768b9b1, 0x9686c9c0, 0xb5a6dad1, 0xd5c6ece3,
523 		0xf5e4fff6,} },
524 		{0x10, '>', {
525 		0x17070600, 0x3928160f, 0x5949271f, 0x796a372f, 0x9a8a483f,
526 		0xb9aa5951, 0xd8c86b62, 0xfae97e74, 0xe3f48d85, 0xc1d29c95,
527 		0x9fb0aca4, 0x7e8ebab3, 0x5d6dcbc2, 0x3c4cdbd3, 0x1f2dece4,
528 		0x0010fff5,} },
529 		{0x10, 0x1001, {
530 		0x0003ecff, 0x0502c6d9, 0x0b099fb2, 0x1410788c, 0x221b5265,
531 		0x362b2f40, 0x5341121d, 0x7966020a, 0xa08d0100, 0xc3b3170b,
532 		0xd9ce3a28, 0xede25f4d, 0xf4f28672, 0xfaf6ac99, 0xfffdd3c0,
533 		0xfffffae6,} },
534 		{0x10, 0x1002, {
535 		0x1900847d, 0x4c328785, 0x7f658b88, 0xb198898b, 0xe3cb7f88,
536 		0xf8f24f68, 0xecf81d35, 0xbfd9010b, 0x90a61504, 0x8186462d,
537 		0x7c7d7960, 0x7f7bac93, 0x8883dfc6, 0xaf97fff4, 0xdcc7eafa,
538 		0xffeec1d7,} },
539 		{0x10, 0x1003, {
540 		0x7543141a, 0xd7a82e17, 0xf8f78f5c, 0xc6e6e9bf, 0x6497f8fa,
541 		0x1338b3db, 0x03024f82, 0x4a1c0620, 0xaf7c1001, 0xf8da582b,
542 		0xeafebc8b, 0x9acbfbe6, 0x3868eeff, 0x0a1892c4, 0x2712305f,
543 		0x85520e11,} },
544 		{0x10, 0x1004, {
545 		0x04003204, 0x120b8e60, 0x1714ebbd, 0x2f27b8e6, 0x43385b89,
546 		0x5b4c032d, 0x6a666032, 0x7770bc8e, 0x8c82b8e6, 0x9b945c8a,
547 		0xaea2012d, 0xbbb75d2f, 0xc9c1ba8c, 0xe3d5bce7, 0xf1ea5f8d,
548 		0xfff50231,} },
549 		{0x10, 0x1005, {
550 		0x1900020a, 0x4d330400, 0x7c65180a, 0xa18f3f2b, 0xb3ac7258,
551 		0xbcb8a58b, 0xb0b9d7bf, 0x8aa0fcec, 0x5971f2fd, 0x414bc2db,
552 		0x433f8ea8, 0x534a5d76, 0x71613246, 0x9d85101e, 0xd0b60609,
553 		0xffea1409,} },
554 		{0x10, 0x1006, {
555 		0xdeff0714, 0x9abc0002, 0x57790a02, 0x1d373018, 0x01076d4b,
556 		0x2f0f988a, 0x72519c9c, 0xb2948b9c, 0xa3bc576b, 0x60815653,
557 		0x26417963, 0x020eb191, 0x2005e7d3, 0x6442fbf4, 0xa785fbff,
558 		0xebc9e7f3,} },
559 		{0x10, 0x1007, {
560 		0xf6ff0600, 0xe4ed140c, 0xd5dd261d, 0xc5cd362e, 0xb3bc463e,
561 		0xa2aa574e, 0x939a6860, 0x83897970, 0x747c8a82, 0x646c9c93,
562 		0x525baca4, 0x434abdb4, 0x333bcfc6, 0x222addd5, 0x121beee6,
563 		0x0009fff6,} },
564 		{0x10, 0x1008, {
565 		0x09000900, 0x19111b13, 0x28202d24, 0x39303d35, 0x49424e45,
566 		0x5a515d56, 0x6b626d65, 0x7d747b74, 0x8e868b82, 0x9d969c93,
567 		0xaca4aea5, 0xbcb4bfb6, 0xcac4d1c7, 0xdbd2e1d9, 0xece3f1e9,
568 		0xfff5fff8,} },
569 		{0x10, 0x1009, {
570 		0x0a00faff, 0x1d14edf4, 0x2e25dce5, 0x3e36cbd4, 0x4e44bac2,
571 		0x5f57aab2, 0x6f679aa2, 0x7f778890, 0x8c85757e, 0x9c94636b,
572 		0xaba3515a, 0xbbb2414a, 0xcbc33039, 0xddd31f28, 0xeee51119,
573 		0xfff7000a,} },
574 		{0x10, 0x100a, {
575 		0xf4fffaff, 0xe1eaeff6, 0xd1d9dde6, 0xc1cacdd5, 0xb4bbbac3,
576 		0xa2aca8b0, 0x929a99a1, 0x83888791, 0x767d747d, 0x666e636b,
577 		0x535c545e, 0x444d424b, 0x333d323a, 0x242b212a, 0x141c0f18,
578 		0x000a0008,} },
579 		{0x10, 0x100b, {
580 		0x30000208, 0x90600200, 0xf1c11e0c, 0xb5e64d3f, 0x55855653,
581 		0x32245556, 0x93625756, 0xf1c3745e, 0xbaeaa997, 0x598ab2b0,
582 		0x0f29aab1, 0x7040a8a7, 0xd1a0b8ae, 0xd0f8ecd0, 0x6f9ffcf5,
583 		0x0e3ffcff,} },
584 		{0x10, 0x100c, {
585 		0x2600140c, 0x66494226, 0x827c8d66, 0x6d80d8b4, 0x284ffaf4,
586 		0x0a0ab9e0, 0x411f8097, 0x8e689182, 0xd6b47f94, 0xf9f23960,
587 		0xd5f80012, 0x91af270a, 0x7e83734c, 0x7e7dc19a, 0xb490fae5,
588 		0xffdbe9fe,} },
589 		{0x10, 0x100d, {
590 		0x768e0c00, 0x465e2619, 0x192f4635, 0x0107745a, 0x32178a85,
591 		0x674d858a, 0x9b81747d, 0xccb45c69, 0xf7e33b4d, 0xebfd0d21,
592 		0xb6d00004, 0x939e270d, 0x888d5d42, 0x83859378, 0x7c80c9ae,
593 		0x6c75ffe4,} },
594 		/*{0x10, 0x100e, {
595 		*0xb3bd1000, 0x9ea82d1e, 0x87924a3b, 0x6f7b6658, 0x56628375,
596 		*0x3e4a9e91, 0x2934bcad, 0x1720ddcd, 0x010bfced, 0x2513ffff,
597 		*0x4937fdff, 0x6d5bf9fa, 0x9280f4f7, 0xb6a4f0f1, 0xdac8eeef,
598 		*0xffececed,}},
599 		*/
600 };
601 #endif
SortBubble(int t[],int size)602 static void SortBubble(int t[], int size)
603 {
604 	int temp = 0;
605 	int m, n;
606 
607 	for (m = 0; m < size; m++) {
608 		for (n = m + 1; n < size; n++) {
609 			temp = t[m];
610 			if (temp > t[n]) {
611 				t[m] = t[n];
612 				t[n] = temp;
613 			}
614 		}
615 	}
616 }
617 
Sqrt(int d)618 static int Sqrt(int d)
619 {
620 	int ret = 0;
621 	int i;
622 
623 	for (i = 14; i >= 0; i--) {
624 		if ((ret + (0x1 << i)) * (ret + (0x1 << i)) <= d)
625 			ret |= (0x1 << i);
626 	}
627 	return ret;
628 }
629 
PointRange(int x0,int y0,int x1,int y1)630 static UINT PointRange(int x0, int y0, int x1, int y1)
631 {
632 	if (x0 < 1) {
633 		if (x0 != x1)
634 			y0 = y1 + (y0 - y1) * (1 - x1) / (x0 - x1);
635 		x0 = 1;
636 	}
637 	if (x0 >= (int)drv_num_nokey*64) {
638 		if (x0 != x1)
639 			y0 = y1 + (y0 - y1) * ((int)drv_num_nokey * 64 - x1) / (x0 - x1);
640 		x0 = drv_num_nokey * 64-1;
641 	}
642 	if (y0 < 1) {
643 		if (y0 != y1)
644 			x0 = x1 + (x0 - x1) * (1 - y1) / (y0 - y1);
645 		y0 = 1;
646 	}
647 	if (y0 >= (int)sen_num_nokey*64) {
648 		if (y0 != y1)
649 			x0 = x1 + (x0 - x1) * ((int)sen_num_nokey * 64 - y1) / (y0 - y1);
650 		y0 = sen_num_nokey * 64-1;
651 	}
652 	if (x0 < 1)
653 		x0 = 1;
654 	if (x0 >= (int)drv_num_nokey * 64)
655 		x0 = drv_num_nokey * 64 - 1;
656 	if (y0 < 1)
657 		y0 = 1;
658 	if (y0 >= (int)sen_num_nokey * 64)
659 		y0 = sen_num_nokey * 64 - 1;
660 
661 	return (x0 << 16) + y0;
662 }
663 
PointCoor(void)664 static void PointCoor(void)
665 {
666 	int i;
667 
668 	point_num &= 0xff;
669 	for (i = 0; i < point_num; i++) {
670 		if (global_state.ex)
671 			point_now[i].all &= (FLAG_COOR_EX | FLAG_KEY | FLAG_ABLE);
672 		else
673 			point_now[i].all &= (FLAG_COOR | FLAG_KEY | FLAG_ABLE);
674 	}
675 }
PointRepeat(void)676 static void PointRepeat(void)
677 {
678 	int i, j;
679 	int x, y;
680 	int x_min, x_max, y_min, y_max;
681 	int pn;
682 
683 	if (point_near)
684 		point_near--;
685 	if (prev_num > point_num)
686 		point_near = 8;
687 	if (point_repeat[0] == 0 || point_repeat[1] == 0) {
688 		if (point_near)
689 			pn = 96;
690 		else
691 			pn = 32;
692 	} else {
693 		if (point_near)
694 			pn = point_repeat[1];
695 		else
696 			pn = point_repeat[0];
697 	}
698 	for (i = 0; i < POINT_MAX; i++) {
699 		if (point_now[i].all == 0)
700 			continue;
701 		if (point_now[i].key)
702 			continue;
703 		x_min = point_now[i].x - pn;
704 		x_max = point_now[i].x + pn;
705 		y_min = point_now[i].y - pn;
706 		y_max = point_now[i].y + pn;
707 		for (j = i + 1; j < POINT_MAX; j++) {
708 			if (point_now[j].all == 0)
709 				continue;
710 			if (point_now[j].key)
711 				continue;
712 			x = point_now[j].x;
713 			y = point_now[j].y;
714 			if (x > x_min && x < x_max && y > y_min && y < y_max) {
715 				point_now[i].x = (point_now[i].x + point_now[j].x + 1) / 2;
716 				point_now[i].y = (point_now[i].y + point_now[j].y + 1) / 2;
717 				point_now[j].all = 0;
718 				i--;
719 				point_near = 8;
720 				break;
721 			}
722 		}
723 	}
724 }
725 
PointPointer(void)726 static void PointPointer(void)
727 {
728 	int i, pn;
729 
730 	point_n++;
731 
732 	if (point_n >= PP_DEEP * PS_DEEP * PR_DEEP * PRESSURE_DEEP)
733 		point_n = 0;
734 	pn = point_n % PP_DEEP;
735 	for (i = 0; i < PP_DEEP; i++) {
736 		pp[i] = point_array[pn];
737 		if (pn == 0)
738 			pn = PP_DEEP - 1;
739 		else
740 			pn--;
741 	}
742 	pn = point_n % PS_DEEP;
743 	for (i = 0; i < PS_DEEP; i++) {
744 		ps[i] = point_array[pn+PP_DEEP];
745 		if (pn == 0)
746 			pn = PS_DEEP - 1;
747 		else
748 			pn--;
749 	}
750 	pn = point_n % PR_DEEP;
751 	for (i = 0; i < PR_DEEP; i++) {
752 		pr[i] = point_array[pn+PP_DEEP+PS_DEEP];
753 		if (pn == 0)
754 			pn = PR_DEEP - 1;
755 		else
756 			pn--;
757 	}
758 	pn = point_n % PRESSURE_DEEP;
759 	for (i = 0; i < PRESSURE_DEEP; i++) {
760 		pa[i] = pressure_array[pn];
761 		if (pn == 0)
762 			pn = PRESSURE_DEEP - 1;
763 		else
764 			pn--;
765 	}
766 
767 	pn = 0;
768 	for (i = 0; i < POINT_MAX; i++) {
769 		if (point_now[i].all)
770 			point_now[pn++].all = point_now[i].all;
771 		pp[0][i].all = 0;
772 		ps[0][i].all = 0;
773 		pr[0][i].all = 0;
774 	}
775 	point_num = pn;
776 	for (i = pn; i < POINT_MAX; i++)
777 		point_now[i].all = 0;
778 }
779 
CCO(unsigned int x,unsigned int coe[],int k)780 static unsigned int CCO(unsigned int x, unsigned int coe[], int k)
781 {
782 	if (k == 0) {
783 		if (x & 32)
784 			return (x & ~31) + (31 - (coe[31 - (x & 31)] & 31));
785 		else
786 			return (x & ~31) + (coe[x & 31] & 31);
787 	}
788 	if (k == 1) {
789 		if (x & 64)
790 			return (x & ~63) + (63 - (coe[63 - (x & 63)] & 63));
791 		else
792 			return (x & ~63) + (coe[x & 63] & 63);
793 	}
794 	if (k == 2)
795 		return (x & ~63) + (coe[x & 63] & 63);
796 
797 	return 0;
798 }
799 
CoordinateCorrect(void)800 static void CoordinateCorrect(void)
801 {
802 	typedef struct {
803 		unsigned int range;
804 		unsigned int group;
805 	} MULTI_TYPE;
806 #ifdef LINE_MULTI_SIZE
807 	#define	LINE_SIZE	LINE_MULTI_SIZE
808 #else
809 	#define	LINE_SIZE		4
810 #endif
811 	int i, j;
812 	unsigned int *px[LINE_SIZE+1], *py[LINE_SIZE+1];
813 	MULTI_TYPE multi_x[LINE_SIZE], multi_y[LINE_SIZE];
814 	unsigned int edge_size = 64;
815 	int kx, ky;
816 
817 	if ((coordinate_correct_able & 0xf) == 0)
818 		return;
819 	kx = (coordinate_correct_able >> 4) & 0xf;
820 	ky = (coordinate_correct_able >> 8) & 0xf;
821 	px[0] = coordinate_correct_coe_x;
822 	py[0] = coordinate_correct_coe_y;
823 	for (i = 0; i < LINE_SIZE; i++) {
824 		px[i+1] = NULL;
825 		py[i+1] = NULL;
826 	}
827 	if (kx == 3 || ky == 3) {
828 		i = 0;
829 		if (((coordinate_correct_able >> 4) & 0xf) == 3)
830 			px[1] = multi_group[i++];
831 		if (((coordinate_correct_able >> 8) & 0xf) == 3)
832 			py[1] = multi_group[i++];
833 	} else {
834 		for (i = 0; i < LINE_SIZE; i++) {
835 			multi_x[i].range = multi_x_array[i] & 0xffff;
836 			multi_x[i].group = multi_x_array[i] >> 16;
837 			multi_y[i].range = multi_y_array[i] & 0xffff;
838 			multi_y[i].group = multi_y_array[i] >> 16;
839 		}
840 		j = 1;
841 		for (i = 0; i < LINE_SIZE; i++)
842 			if (multi_x[i].range && multi_x[i].group < LINE_SIZE)
843 				px[j++] = multi_group[multi_x[i].group];
844 		j = 1;
845 		for (i = 0; i < LINE_SIZE; i++)
846 			if (multi_y[i].range && multi_y[i].group < LINE_SIZE)
847 				py[j++] = multi_group[multi_y[i].group];
848 	}
849 	for (i = 0; i < (int)point_num && i < POINT_MAX; i++) {
850 		if (point_now[i].all == 0)
851 			break;
852 		if (point_now[i].key != 0)
853 			continue;
854 		if (point_now[i].x >= edge_size && point_now[i].x <= drv_num_nokey * 64 - edge_size) {
855 			if (kx == 3) {
856 				if (point_now[i].x & 64)
857 					point_now[i].x = CCO(point_now[i].x, px[0], 2);
858 				else
859 					point_now[i].x = CCO(point_now[i].x, px[1], 2);
860 			} else {
861 				for (j = 0; j < LINE_SIZE + 1; j++) {
862 					if (!(j >= LINE_SIZE || px[j+1] == NULL ||
863 						multi_x[j].range == 0 ||
864 						point_now[i].x < multi_x[j].range))
865 						continue;
866 					point_now[i].x = CCO(point_now[i].x, px[j], kx);
867 					break;
868 				}
869 			}
870 		}
871 		if (point_now[i].y >= edge_size && point_now[i].y <= sen_num_nokey * 64 - edge_size) {
872 			if (ky == 3) {
873 				if (point_now[i].y & 64)
874 					point_now[i].y = CCO(point_now[i].y, py[0], 2);
875 				else
876 					point_now[i].y = CCO(point_now[i].y, py[1], 2);
877 			} else {
878 				for (j = 0; j < LINE_SIZE + 1; j++) {
879 					if (!(j >= LINE_SIZE || py[j+1] == NULL ||
880 						multi_y[j].range == 0 ||
881 						point_now[i].y < multi_y[j].range))
882 						continue;
883 					point_now[i].y = CCO(point_now[i].y, py[j], ky);
884 					break;
885 				}
886 			}
887 		}
888 	}
889 #undef LINE_SIZE
890 }
891 
PointPredictOne(unsigned int n)892 static void PointPredictOne(unsigned int n)
893 {
894 	pp[0][n].all = pp[1][n].all & FLAG_COOR;
895 	pp[0][n].predict = 0;
896 }
897 
PointPredictTwo(unsigned int n)898 static void PointPredictTwo(unsigned int n)
899 {
900 	int x, y;
901 
902 	x = pp[1][n].x * 2 - pp[2][n].x;
903 	y = pp[1][n].y * 2 - pp[2][n].y;
904 	pp[0][n].all = PointRange(x, y, pp[1][n].x, pp[1][n].y);
905 	pp[0][n].predict = 1;
906 }
907 
PointPredictThree(unsigned int n)908 static void PointPredictThree(unsigned int n)
909 {
910 	int x, y;
911 
912 	x = pp[1][n].x * 5 + pp[3][n].x - pp[2][n].x * 4;
913 	x /= 2;
914 	y = pp[1][n].y * 5 + pp[3][n].y - pp[2][n].y * 4;
915 	y /= 2;
916 	pp[0][n].all = PointRange(x, y, pp[1][n].x, pp[1][n].y);
917 	pp[0][n].predict = 1;
918 }
919 
PointPredict(void)920 static void PointPredict(void)
921 {
922 	int i;
923 
924 	for (i = 0; i < POINT_MAX; i++) {
925 		if (pp[1][i].all != 0) {
926 			if (global_state.interpolation
927 				 || pp[2][i].all == 0
928 				 || pp[2][i].fill != 0
929 				 || pp[3][i].fill != 0
930 				 || pp[1][i].key != 0
931 				 || global_state.only) {
932 				PointPredictOne(i);
933 			} else if (pp[2][i].all != 0) {
934 				if (pp[3][i].all != 0)
935 					PointPredictThree(i);
936 				else
937 					PointPredictTwo(i);
938 			}
939 			pp[0][i].all |= FLAG_FILL;
940 			pa[0][i] = pa[1][i];
941 		} else
942 			pp[0][i].all = 0x0fff0fff;
943 
944 		if (pp[1][i].key)
945 			pp[0][i].all |= FLAG_KEY;
946 	}
947 }
948 
PointDistance(gsl_POINT_TYPE * p1,gsl_POINT_TYPE * p2)949 static unsigned int PointDistance(gsl_POINT_TYPE *p1, gsl_POINT_TYPE *p2)
950 {
951 	int a, b, ret;
952 
953 	a = p1->dis.x;
954 	b = p2->dis.x;
955 	ret = (a - b) * (a - b);
956 	a = p1->dis.y;
957 	b = p2->dis.y;
958 	ret += (a - b) * (a - b);
959 
960 	return ret;
961 }
962 
DistanceInit(gsl_DISTANCE_TYPE * p)963 static void DistanceInit(gsl_DISTANCE_TYPE *p)
964 {
965 	int i;
966 	unsigned int *p_int = &(p->d[0][0]);
967 
968 	for (i = 0; i < POINT_MAX * POINT_MAX; i++)
969 		*p_int++ = 0x7fffffff;
970 }
971 
DistanceMin(gsl_DISTANCE_TYPE * p)972 static int DistanceMin(gsl_DISTANCE_TYPE *p)
973 {
974 	int i, j;
975 
976 	p->min = 0x7fffffff;
977 	for (j = 0; j < POINT_MAX; j++) {
978 		for (i = 0; i < POINT_MAX; i++) {
979 			if (p->d[j][i] < p->min) {
980 				p->i = i;
981 				p->j = j;
982 				p->min = p->d[j][i];
983 			}
984 		}
985 	}
986 	if (p->min == 0x7fffffff)
987 		return 0;
988 	return 1;
989 }
990 
DistanceIgnore(gsl_DISTANCE_TYPE * p)991 static void DistanceIgnore(gsl_DISTANCE_TYPE *p)
992 {
993 	int i, j;
994 
995 	for (i = 0; i < POINT_MAX; i++)
996 		p->d[p->j][i] = 0x7fffffff;
997 	for (j = 0; j < POINT_MAX; j++)
998 		p->d[j][p->i] = 0x7fffffff;
999 }
1000 
SpeedGet(int d)1001 static int SpeedGet(int d)
1002 {
1003 	int i;
1004 
1005 	for (i = 8; i > 0; i--) {
1006 		if (d > 0x100 << i)
1007 			break;
1008 	}
1009 	return i;
1010 }
1011 
PointId(void)1012 static void PointId(void)
1013 {
1014 	int i, j;
1015 	gsl_DISTANCE_TYPE distance;
1016 	unsigned int id_speed[POINT_MAX];
1017 
1018 	DistanceInit(&distance);
1019 	for (i = 0; i < POINT_MAX; i++) {
1020 		if (pp[0][i].predict == 0 || pp[1][i].fill != 0)
1021 			id_speed[i] = id_first_coe;
1022 		else
1023 			id_speed[i] = SpeedGet(PointDistance(&pp[1][i], &pp[0][i]));
1024 	}
1025 	for (i = 0; i < POINT_MAX; i++) {
1026 		if (pp[0][i].all == FLAG_COOR)
1027 			continue;
1028 		for (j = 0; j < point_num && j < POINT_MAX; j++)
1029 			distance.d[j][i] = PointDistance(&point_now[j], &pp[0][i]);
1030 	}
1031 	if (point_num == 0)
1032 		return;
1033 	if (global_state.only) {
1034 		do {
1035 			if (DistanceMin(&distance)) {
1036 				if (pp[1][0].all != 0 && pp[1][0].key
1037 						!= point_now[distance.j].key) {
1038 					DistanceIgnore(&distance);
1039 					continue;
1040 				}
1041 				pp[0][0].all = point_now[distance.j].all;
1042 			} else
1043 				pp[0][0].all = point_now[0].all;
1044 			for (i = 0; i < POINT_MAX; i++)
1045 				point_now[i].all = 0;
1046 		} while (0);
1047 		point_num = 1;
1048 	} else {
1049 		for (j = 0; j < point_num && j < POINT_MAX; j++) {
1050 			if (DistanceMin(&distance) == 0)
1051 				break;
1052 			/* average/(soft_average+1) */
1053 			if (distance.min >= (id_static_coe +
1054 					id_speed[distance.i] * id_speed_coe))
1055 				/* point_now[distance.j].id = 0xf; new id */
1056 				continue;
1057 
1058 			pp[0][distance.i].all = point_now[distance.j].all;
1059 			pa[0][distance.i] = pressure_now[distance.j];
1060 			point_now[distance.j].all = 0;
1061 			DistanceIgnore(&distance);
1062 		}
1063 	}
1064 }
1065 
ClearLenPP(int i)1066 static int ClearLenPP(int i)
1067 {
1068 	int n;
1069 
1070 	for (n = 0; n < PP_DEEP; n++) {
1071 		if (pp[n][i].all)
1072 			break;
1073 	}
1074 	return n;
1075 }
PointNewId(void)1076 static void PointNewId(void)
1077 {
1078 	int id, j;
1079 
1080 	for (j = 0; j < POINT_MAX; j++)
1081 		if ((pp[0][j].all & FLAG_COOR) == FLAG_COOR)
1082 			pp[0][j].all = 0;
1083 	for (j = 0; j < POINT_MAX; j++) {
1084 		if (point_now[j].all != 0) {
1085 			if (point_now[j].able)
1086 				continue;
1087 			for (id = 1; id <= POINT_MAX; id++) {
1088 				if (ClearLenPP(id - 1) > (int)(1 + 1)) {
1089 					pp[0][id-1].all = point_now[j].all;
1090 					pa[0][id-1] = pressure_now[j];
1091 					point_now[j].all = 0;
1092 					break;
1093 				}
1094 			}
1095 		}
1096 	}
1097 }
1098 
PointOrder(void)1099 static void PointOrder(void)
1100 {
1101 	int i;
1102 
1103 	for (i = 0; i < POINT_MAX; i++) {
1104 		if (pp[0][i].fill == 0)
1105 			continue;
1106 		if (pp[1][i].all == 0 || pp[1][i].fill != 0 ||
1107 				filter_able == 0 || filter_able == 1) {
1108 			pp[0][i].all = 0;
1109 			pressure_now[i] = 0;
1110 		}
1111 	}
1112 }
1113 
PointCross(void)1114 static void PointCross(void)
1115 {
1116 	unsigned int i, j;
1117 	unsigned int t;
1118 
1119 	for (j = 0; j < POINT_MAX; j++) {
1120 		for (i = j + 1; i < POINT_MAX; i++) {
1121 			if (pp[0][i].all == 0 || pp[0][j].all == 0
1122 					|| pp[1][i].all == 0 || pp[1][j].all == 0)
1123 				continue;
1124 			if (((pp[0][j].x < pp[0][i].x && pp[1][j].x > pp[1][i].x)
1125 					|| (pp[0][j].x > pp[0][i].x
1126 					&& pp[1][j].x < pp[1][i].x))
1127 					&& ((pp[0][j].y < pp[0][i].y
1128 					&& pp[1][j].y > pp[1][i].y)
1129 					|| (pp[0][j].y > pp[0][i].y
1130 					&& pp[1][j].y < pp[1][i].y))) {
1131 				t = pp[0][i].x;
1132 				pp[0][i].x = pp[0][j].x;
1133 				pp[0][j].x = t;
1134 				t = pp[0][i].y;
1135 				pp[0][i].y = pp[0][j].y;
1136 				pp[0][j].y = t;
1137 			}
1138 		}
1139 	}
1140 }
1141 
GetPointNum(gsl_POINT_TYPE * pt)1142 static void GetPointNum(gsl_POINT_TYPE *pt)
1143 {
1144 	int i;
1145 
1146 	point_num = 0;
1147 	for (i = 0; i < POINT_MAX; i++)
1148 		if (pt[i].all != 0)
1149 			point_num++;
1150 }
1151 
PointDelay(void)1152 static void PointDelay(void)
1153 {
1154 	int i, j;
1155 
1156 	for (i = 0; i < POINT_MAX; i++) {
1157 		if (report_delay == 0) {
1158 			point_delay[i].all = 0;
1159 			if (pp[0][i].all)
1160 				point_delay[i].able = 1;
1161 			continue;
1162 		}
1163 		if (pp[0][i].all != 0 && point_delay[i].init == 0
1164 					&& point_delay[i].able == 0) {
1165 			if (point_num == 0)
1166 				continue;
1167 			point_delay[i].delay  = (report_delay >> 3*
1168 					((point_num > 10 ? 10 : point_num) - 1)) & 0x7;
1169 			point_delay[i].report = (report_ahead >> 3*
1170 					((point_num > 10 ? 10 : point_num) - 1)) & 0x7;
1171 			if (point_delay[i].report > point_delay[i].delay)
1172 				point_delay[i].report = point_delay[i].delay;
1173 			point_delay[i].init = 1;
1174 		}
1175 		if (pp[0][i].all == 0)
1176 			point_delay[i].init = 0;
1177 
1178 		if (point_delay[i].able == 0 && point_delay[i].init != 0) {
1179 			for (j = 0; j <= (int)point_delay[i].delay; j++)
1180 				if (pp[j][i].all == 0 || pp[j][i].fill != 0
1181 							|| pp[j][i].able != 0)
1182 					break;
1183 			if (j <= (int)point_delay[i].delay)
1184 				continue;
1185 			point_delay[i].able = 1;
1186 		}
1187 		if (pp[point_delay[i].report][i].all == 0) {
1188 			point_delay[i].able = 0;
1189 			continue;
1190 		}
1191 		if (point_delay[i].able == 0)
1192 			continue;
1193 		if (point_delay[i].report) {
1194 			if (PointDistance(&pp[point_delay[i].report][i],
1195 			&pp[point_delay[i].report - 1][i]) < 3 * 3)
1196 				point_delay[i].report--;
1197 		}
1198 	}
1199 }
1200 
FilterOne(int i,int * ps_c,int * pr_c,int denominator)1201 static void FilterOne(int i, int *ps_c, int *pr_c, int denominator)
1202 {
1203 	int j;
1204 	int x = 0, y = 0;
1205 
1206 	pr[0][i].all = ps[0][i].all;
1207 	if (pr[0][i].all == 0)
1208 		return;
1209 	if (denominator <= 0)
1210 		return;
1211 	for (j = 0; j < 8; j++) {
1212 		x += (int)pr[j][i].x * (int)pr_c[j] + (int)ps[j][i].x * (int)ps_c[j];
1213 		y += (int)pr[j][i].y * (int)pr_c[j] + (int)ps[j][i].y * (int)ps_c[j];
1214 	}
1215 	x = (x + denominator/2) / denominator;
1216 	y = (y + denominator/2) / denominator;
1217 	if (x < 0)
1218 		x = 0;
1219 	if (x > 0xffff)
1220 		x = 0xffff;
1221 	if (y < 0)
1222 		y = 0;
1223 	if (y > 0xfff)
1224 		y = 0xfff;
1225 	pr[0][i].x = x;
1226 	pr[0][i].y = y;
1227 }
1228 
FilterSpeed(int i)1229 static unsigned int FilterSpeed(int i)
1230 {
1231 	return (Sqrt(PointDistance(&ps[0][i], &ps[1][i])) +
1232 				Sqrt(PointDistance(&ps[1][i], &ps[2][i])))/2;
1233 }
1234 
MedianSpeedOver(int id,int deep)1235 static int MedianSpeedOver(int id, int deep)
1236 {
1237 	int i;
1238 	unsigned int dis;
1239 	int speed_over = 0;
1240 
1241 	deep = deep / 2 - 1;
1242 	if (deep < 0 || deep > 3)
1243 		return TRUE;
1244 	dis = median_dis[deep] * median_dis[deep];
1245 	for (i = 0; i <= deep && i < PS_DEEP; i++) {
1246 		if (PointDistance(&ps[i][id], &ps[i+1][id]) > dis)
1247 			speed_over++;
1248 	}
1249 	if (speed_over >= 2)
1250 		return TRUE;
1251 
1252 	return FALSE;
1253 }
1254 
PointMedian(void)1255 static void PointMedian(void)
1256 {
1257 	int i, j;
1258 	int deep;
1259 	int buf_x[PS_DEEP], buf_y[PS_DEEP];
1260 
1261 	for (i = 0; i < POINT_MAX; i++) {
1262 		if (filter_deep[i] < 3)
1263 			deep = 3;
1264 		else
1265 			deep = filter_deep[i] + 2;
1266 		if (deep >= PS_DEEP)
1267 			deep = PS_DEEP-1;
1268 		deep |= 1;
1269 		for (; deep >= 3; deep -= 2) {
1270 			if (MedianSpeedOver(i, deep))
1271 				continue;
1272 			for (j = 0; j < deep; j++) {
1273 				buf_x[j] = ps[j][i].x;
1274 				buf_y[j] = ps[j][i].y;
1275 			}
1276 			SortBubble(buf_x, deep);
1277 			SortBubble(buf_y, deep);
1278 			pr[0][i].x = buf_x[deep / 2];
1279 			pr[0][i].y = buf_y[deep / 2];
1280 		}
1281 		filter_deep[i] = deep;
1282 	}
1283 }
PointFilter(void)1284 static void PointFilter(void)
1285 {
1286 	int i, j;
1287 	int speed_now;
1288 	int filter_speed[6];
1289 	int ps_c[8];
1290 	int pr_c[8];
1291 
1292 	for (i = 0; i < POINT_MAX; i++)
1293 		pr[0][i].all = ps[0][i].all;
1294 
1295 	for (i = 0; i < POINT_MAX; i++) {
1296 		if (pr[0][i].all != 0 && pr[1][i].all == 0) {
1297 			for (j = 1; j < PR_DEEP; j++)
1298 				pr[j][i].all = ps[0][i].all;
1299 			for (j = 1; j < PS_DEEP; j++)
1300 				ps[j][i].all = ps[0][i].all;
1301 		}
1302 	}
1303 	if (filter_able >= 0 && filter_able <= 1)
1304 		return;
1305 	if (filter_able > 1) {
1306 		for (i = 0; i < 8; i++) {
1307 			ps_c[i] = (filter_coe[i / 4] >> ((i % 4) * 8)) & 0xff;
1308 			pr_c[i] = (filter_coe[i / 4 + 2] >> ((i % 4) * 8)) & 0xff;
1309 			if (ps_c[i] >= 0x80)
1310 				ps_c[i] |= 0xffffff00;
1311 			if (pr_c[i] >= 0x80)
1312 				pr_c[i] |= 0xffffff00;
1313 		}
1314 		for (i = 0; i < POINT_MAX; i++)
1315 			FilterOne(i, ps_c, pr_c, filter_able);
1316 
1317 	} else if (filter_able == -1)
1318 		PointMedian();
1319 	else if (filter_able < 0) {
1320 		for (i = 0; i < 4; i++)
1321 			filter_speed[i+1] = median_dis[i];
1322 		filter_speed[0] = median_dis[0] * 2 - median_dis[1];
1323 		filter_speed[5] = median_dis[3] / 2;
1324 		for (i = 0; i < POINT_MAX; i++) {
1325 			if (pr[0][i].all == 0) {
1326 				filter_deep[i] = 0;
1327 				continue;
1328 			}
1329 			speed_now = FilterSpeed(i);
1330 			if (filter_deep[i] > 0 && speed_now > filter_speed[filter_deep[i]+1 - 2])
1331 				filter_deep[i]--;
1332 			else if (filter_deep[i] < 3 && speed_now < filter_speed[filter_deep[i]+1 + 2])
1333 				filter_deep[i]++;
1334 
1335 			FilterOne(i, ps_coe[filter_deep[i]], pr_coe[filter_deep[i]], 0-filter_able);
1336 		}
1337 	}
1338 }
KeyMap(int * drv,int * sen)1339 static unsigned int KeyMap(int *drv, int *sen)
1340 {
1341 	typedef struct {
1342 		unsigned int up_down, left_right;
1343 		unsigned int coor;
1344 	} KEY_TYPE_RANGE;
1345 	KEY_TYPE_RANGE *key_range = (KEY_TYPE_RANGE *)key_range_array;
1346 	int i;
1347 
1348 	for (i = 0; i < 8; i++) {
1349 		if ((unsigned int)*drv >= (key_range[i].up_down >> 16)
1350 		&& (unsigned int)*drv <= (key_range[i].up_down & 0xffff)
1351 		&& (unsigned int)*sen >= (key_range[i].left_right >> 16)
1352 		&& (unsigned int)*sen <= (key_range[i].left_right & 0xffff)) {
1353 			*sen = key_range[i].coor >> 16;
1354 			*drv = key_range[i].coor & 0xffff;
1355 			return key_range[i].coor;
1356 		}
1357 	}
1358 
1359 	return 0;
1360 }
1361 
ScreenResolution(gsl_POINT_TYPE * p)1362 static unsigned int ScreenResolution(gsl_POINT_TYPE *p)
1363 {
1364 	int x, y;
1365 
1366 	x = p->x;
1367 	y = p->y;
1368 	if (p->key == FALSE) {
1369 		y = ((y - match_y[1]) * match_y[0] + 2048) / 4096;
1370 		x = ((x - match_x[1]) * match_x[0] + 2048) / 4096;
1371 	}
1372 	y = y * (int)screen_y_max / ((int)sen_num_nokey * 64);
1373 	x = x * (int)screen_x_max / ((int)drv_num_nokey * 64);
1374 	if (p->key == FALSE) {
1375 		if ((ignore_y[0] != 0 || ignore_y[1] != 0)) {
1376 			if (y < ignore_y[0])
1377 				return 0;
1378 			if (ignore_y[1] <= screen_y_max/2
1379 					&& y > screen_y_max - ignore_y[1])
1380 				return 0;
1381 			if (ignore_y[1] >= screen_y_max/2
1382 					&& y > ignore_y[1])
1383 				return 0;
1384 		}
1385 		if (ignore_x[0] != 0 || ignore_x[1] != 0) {
1386 			if (x < ignore_x[0])
1387 				return 0;
1388 			if (ignore_x[1] <= screen_y_max/2
1389 					&& x > screen_x_max - ignore_x[1])
1390 				return 0;
1391 			if (ignore_x[1] >= screen_y_max/2 && x > ignore_x[1])
1392 				return 0;
1393 		}
1394 		if (y <= (int)edge_cut[2])
1395 			y = (int)edge_cut[2] + 1;
1396 		if (y >= screen_y_max - (int)edge_cut[3])
1397 			y = screen_y_max - (int)edge_cut[3] - 1;
1398 		if (x <= (int)edge_cut[0])
1399 			x = (int)edge_cut[0] + 1;
1400 		if (x >= screen_x_max - (int)edge_cut[1])
1401 			x = screen_x_max - (int)edge_cut[1] - 1;
1402 		if (global_flag.opposite_x)
1403 			y = screen_y_max - y;
1404 		if (global_flag.opposite_y)
1405 			x = screen_x_max - x;
1406 		if (global_flag.opposite_xy) {
1407 			y ^= x;
1408 			x ^= y;
1409 			y ^= x;
1410 		}
1411 	} else {
1412 		if (y < 0)
1413 			y = 0;
1414 		if (x < 0)
1415 			x = 0;
1416 		if ((key_map_able & 0x1) != FALSE && KeyMap(&x, &y) == 0)
1417 			return 0;
1418 	}
1419 
1420 	return ((y << 16) & 0x0fff0000) + (x & 0x0000ffff);
1421 }
1422 
PointReport(struct gsl_touch_info * cinfo)1423 static void PointReport(struct gsl_touch_info *cinfo)
1424 {
1425 	int i;
1426 	unsigned int data[POINT_MAX];
1427 	int num = 0;
1428 
1429 	if (point_num > point_num_max && global_flag.over_report_mask != 0) {
1430 		point_num = 0;
1431 		cinfo->finger_num = 0;
1432 		return;
1433 	}
1434 	for (i = 0; i < POINT_MAX; i++)
1435 		data[i] = 0;
1436 	num = 0;
1437 	if (global_flag.id_over) {
1438 		for (i = 0; i < POINT_MAX && num < point_num_max; i++) {
1439 			if (point_delay[i].able == 0)
1440 				continue;
1441 			if (point_delay[i].report >= PR_DEEP-1)
1442 				continue;
1443 			if (pr[point_delay[i].report+1][i].able == 0)
1444 				continue;
1445 			if (pr[point_delay[i].report][i].all) {
1446 				pr[point_delay[i].report][i].able = 1;
1447 				data[i] = ScreenResolution(&pr[point_delay[i].report][i]);
1448 				if (data[i]) {
1449 					data[i] |= (i+1) << 28;
1450 					num++;
1451 				}
1452 			}
1453 		}
1454 		for (i = 0; i < POINT_MAX && num < point_num_max; i++) {
1455 			if (point_delay[i].able == 0)
1456 				continue;
1457 			if (point_delay[i].report >= PR_DEEP)
1458 				continue;
1459 			if (pr[point_delay[i].report][i].all == 0)
1460 				continue;
1461 			if (pr[point_delay[i].report][i].able == 0) {
1462 				pr[point_delay[i].report][i].able = 1;
1463 				data[i] = ScreenResolution(&pr[point_delay[i].report][i]);
1464 				if (data[i]) {
1465 					data[i] |= (i + 1) << 28;
1466 					num++;
1467 				}
1468 			}
1469 		}
1470 	} else {
1471 		num = 0;
1472 		for (i = 0; i < point_num_max && i < POINT_MAX; i++) {
1473 			if (point_delay[i].able == 0)
1474 				continue;
1475 			if (point_delay[i].report >= PR_DEEP)
1476 				continue;
1477 			data[num] = ScreenResolution(&pr[point_delay[i].report][i]);
1478 			if (data[num])
1479 				data[num++] |= (i + 1) << 28;
1480 		}
1481 	}
1482 	num = 0;
1483 	for (i = 0; i < POINT_MAX; i++) {
1484 		if (data[i] == 0)
1485 			continue;
1486 		point_now[num].all = data[i];
1487 		cinfo->x[num] = (data[i] >> 16) & 0xfff;
1488 		cinfo->y[num] = data[i] & 0xfff;
1489 		cinfo->id[num] = data[i] >> 28;
1490 		pressure_now[num] = pressure_report[i];
1491 		num++;
1492 	}
1493 	for (i = num; i < POINT_MAX; i++) {
1494 		point_now[i].all = 0;
1495 		pressure_now[i] = 0;
1496 	}
1497 	point_num = num;
1498 	cinfo->finger_num = num;
1499 }
1500 
1501 
1502 
PointEdge(void)1503 static void PointEdge(void)
1504 {
1505 	typedef struct {
1506 		int range;
1507 		int coe;
1508 	} STRETCH_TYPE;
1509 	typedef struct {
1510 		STRETCH_TYPE up[4];
1511 		STRETCH_TYPE down[4];
1512 		STRETCH_TYPE left[4];
1513 		STRETCH_TYPE right[4];
1514 	} STRETCH_TYPE_ALL;
1515 	STRETCH_TYPE_ALL *stretch;
1516 	int i, id;
1517 	int data[2];
1518 	int x, y;
1519 	int sac[4 * 4 * 2];
1520 
1521 	if (screen_x_max == 0 || screen_y_max == 0)
1522 		return;
1523 	id = 0;
1524 	for (i = 0; i < 4*4*2; i++) {
1525 		sac[i] = stretch_array[i];
1526 		if (sac[i])
1527 			id++;
1528 	}
1529 	if (id == 0)
1530 		return;
1531 	stretch = (STRETCH_TYPE_ALL *)sac;
1532 	for (i = 0; i < 4; i++) {
1533 		if (stretch->right[i].range > screen_y_max * 64 / 128
1534 		|| stretch->down[i].range > screen_x_max * 64 / 128) {
1535 			for (i = 0; i < 4; i++) {
1536 				if (stretch->up[i].range)
1537 					stretch->up[i].range = stretch->up[i].range *
1538 						drv_num_nokey * 64 / screen_x_max;
1539 				if (stretch->down[i].range)
1540 					stretch->down[i].range = (screen_x_max - stretch->down[i].range) * drv_num_nokey * 64 / screen_x_max;
1541 				if (stretch->left[i].range)
1542 					stretch->left[i].range = stretch->left[i].range * sen_num_nokey * 64 / screen_y_max;
1543 				if (stretch->right[i].range)
1544 					stretch->right[i].range = (screen_y_max - stretch->right[i].range) * sen_num_nokey * 64 / screen_y_max;
1545 			}
1546 			break;
1547 		}
1548 	}
1549 	for (id = 0; id < POINT_MAX; id++) {
1550 		if (point_now[id].all == 0 || point_now[id].key != 0)
1551 			continue;
1552 		x = point_now[id].x;
1553 		y = point_now[id].y;
1554 
1555 		data[0] = 0;
1556 		data[1] = y;
1557 		for (i = 0; i < 4; i++) {
1558 			if (stretch->left[i].range == 0)
1559 				break;
1560 			if (data[1] < stretch->left[i].range) {
1561 				data[0] += (stretch->left[i].range - data[1]) * stretch->left[i].coe / 128;
1562 				data[1] = stretch->left[i].range;
1563 			}
1564 		}
1565 		y = data[1] - data[0];
1566 		if (y <= 0)
1567 			y = 1;
1568 		if (y >= (int)sen_num_nokey * 64)
1569 			y = sen_num_nokey * 64 - 1;
1570 
1571 		data[0] = 0;
1572 		data[1] = sen_num_nokey * 64 - y;
1573 		for (i = 0; i < 4; i++) {
1574 			if (stretch->right[i].range == 0)
1575 				break;
1576 			if (data[1] < stretch->right[i].range) {
1577 				data[0] += (stretch->right[i].range - data[1]) * stretch->right[i].coe / 128;
1578 				data[1] = stretch->right[i].range;
1579 			}
1580 		}
1581 		y = sen_num_nokey * 64 - (data[1] - data[0]);
1582 		if (y <= 0)
1583 			y = 1;
1584 		if (y >= (int)sen_num_nokey * 64)
1585 			y = sen_num_nokey * 64 - 1;
1586 
1587 		data[0] = 0;
1588 		data[1] = x;
1589 		for (i = 0; i < 4; i++) {
1590 			if (stretch->up[i].range == 0)
1591 				break;
1592 			if (data[1] < stretch->up[i].range) {
1593 				data[0] += (stretch->up[i].range - data[1]) * stretch->up[i].coe / 128;
1594 				data[1] = stretch->up[i].range;
1595 			}
1596 		}
1597 		x = data[1] - data[0];
1598 		if (x <= 0)
1599 			x = 1;
1600 		if (x >= (int)drv_num_nokey * 64)
1601 			x = drv_num_nokey * 64 - 1;
1602 
1603 		data[0] = 0;
1604 		data[1] = drv_num_nokey * 64 - x;
1605 		for (i = 0; i < 4; i++) {
1606 			if (stretch->down[i].range == 0)
1607 				break;
1608 			if (data[1] < stretch->down[i].range) {
1609 				data[0] += (stretch->down[i].range - data[1]) * stretch->down[i].coe / 128;
1610 				data[1] = stretch->down[i].range;
1611 			}
1612 		}
1613 		x = drv_num_nokey * 64 - (data[1] - data[0]);
1614 		if (x <= 0)
1615 			x = 1;
1616 		if (x >= (int)drv_num_nokey * 64)
1617 			x = drv_num_nokey * 64 - 1;
1618 
1619 		point_now[id].x = x;
1620 		point_now[id].y = y;
1621 	}
1622 }
1623 
PointStretch(void)1624 static void PointStretch(void)
1625 {
1626 	static int save_dr[POINT_MAX], save_dn[POINT_MAX];
1627 	typedef struct {
1628 		int dis;
1629 		int coe;
1630 	} SHAKE_TYPE;
1631 	SHAKE_TYPE *shake_all = (SHAKE_TYPE *)shake_all_array;
1632 	int i, j;
1633 	int dn;
1634 	int dr;
1635 	int dc[9], ds[9];
1636 	int len = 8;
1637 	unsigned int temp;
1638 
1639 	for (i = 0; i < POINT_MAX; i++)
1640 		ps[0][i].all = pp[0][i].all;
1641 
1642 	for (i = 0; i < POINT_MAX; i++) {
1643 		if (pp[0][i].all == 0 || pp[0][i].key) {
1644 			point_shake &= ~(0x1 << i);
1645 			if (i == 0)
1646 				point_edge.rate = 0;
1647 			continue;
1648 		}
1649 		if (i == 0) {
1650 			if (edge_first != 0 && ps[1][i].all == 0) {
1651 				point_edge.coor.all = ps[0][i].all;
1652 				if (point_edge.coor.x < (unsigned int)((edge_first >> 24) & 0xff))
1653 					point_edge.coor.x = ((edge_first >> 24) & 0xff);
1654 
1655 				if (point_edge.coor.x > drv_num_nokey * 64 - ((edge_first >> 16) & 0xff))
1656 					point_edge.coor.x = drv_num_nokey*64 - ((edge_first >> 16) & 0xff);
1657 				if (point_edge.coor.y < (unsigned int)((edge_first >> 8) & 0xff))
1658 					point_edge.coor.y = ((edge_first >> 8) & 0xff);
1659 				if (point_edge.coor.y > sen_num_nokey * 64 - ((edge_first >> 0) & 0xff))
1660 					point_edge.coor.y = sen_num_nokey * 64 - ((edge_first >> 0) & 0xff);
1661 				if (point_edge.coor.all != ps[0][i].all) {
1662 					point_edge.dis = PointDistance(&ps[0][i], &point_edge.coor);
1663 					if (point_edge.dis)
1664 						point_edge.rate = 0x1000;
1665 				}
1666 			}
1667 			if (point_edge.rate != 0 && point_edge.dis != 0) {
1668 				temp = PointDistance(&ps[0][i], &point_edge.coor);
1669 				if (temp >= point_edge.dis * edge_first_coe / 0x80) {
1670 					point_edge.rate = 0;
1671 				} else if (temp > point_edge.dis) {
1672 					temp = (point_edge.dis * edge_first_coe / 0x80 - temp) *
1673 									0x1000/point_edge.dis;
1674 					if (temp < point_edge.rate)
1675 						point_edge.rate = temp;
1676 				}
1677 				ps[0][i].x = point_edge.coor.x + (ps[0][i].x - point_edge.coor.x) *
1678 								(0x1000 - point_edge.rate) / 0x1000;
1679 				ps[0][i].y = point_edge.coor.y + (ps[0][i].y - point_edge.coor.y) *
1680 								(0x1000 - point_edge.rate) / 0x1000;
1681 			}
1682 		}
1683 		if (ps[1][i].all == 0)
1684 			continue;
1685 		else if ((point_shake & (0x1 << i)) == 0) {
1686 			if (PointDistance(&ps[0][i], &ps[1][i]) < (unsigned int)shake_min) {
1687 				ps[0][i].all = ps[1][i].all;
1688 				continue;
1689 			} else
1690 				point_shake |= (0x1 << i);
1691 		}
1692 	}
1693 	for (i = 0; i < len; i++) {
1694 		if (shake_all[i].dis == 0) {
1695 			len = i;
1696 			break;
1697 		}
1698 	}
1699 	if (len == 1) {
1700 		ds[0] = shake_all[0].dis;
1701 		dc[0] = (shake_all[0].coe*100+64)/128;
1702 		for (i = 0; i < POINT_MAX; i++) {
1703 			if (ps[1][i].all == 0) {
1704 				for (j = 1; j < PS_DEEP; j++)
1705 					ps[j][i].all = ps[0][i].all;
1706 				continue;
1707 			}
1708 			if ((point_shake & (0x1 << i)) == 0)
1709 				continue;
1710 			dn = PointDistance(&pp[0][i], &ps[1][i]);
1711 			dn = Sqrt(dn);
1712 			dr = dn > ds[0] ? dn-ds[0] : 0;
1713 			temp = ps[0][i].all;
1714 			if (dn == 0 || dr == 0) {
1715 				ps[0][i].x = ps[1][i].x;
1716 				ps[0][i].y = ps[1][i].y;
1717 			} else {
1718 				ps[0][i].x = (int)ps[1][i].x + ((int)pp[0][i].x - (int)ps[1][i].x) * dr / dn;
1719 				ps[0][i].y = (int)ps[1][i].y + ((int)pp[0][i].y - (int)ps[1][i].y) * dr / dn;
1720 			}
1721 			if (dc[0] > 0) {
1722 				if (ps[0][i].all == ps[1][i].all
1723 						&& temp != ps[0][i].all) {
1724 					ps[0][i].all = temp;
1725 					point_decimal[i].x += ps[0][i].x - ps[1][i].x;
1726 					point_decimal[i].y += ps[0][i].y - ps[1][i].y;
1727 					ps[0][i].x = ps[1][i].x;
1728 					ps[0][i].y = ps[1][i].y;
1729 					if (point_decimal[i].x > dc[0]
1730 							&& ps[1][i].x < 0xffff) {
1731 						ps[0][i].x += 1;
1732 						point_decimal[i].x = 0;
1733 					}
1734 					if (point_decimal[i].x < -dc[0]
1735 							&& ps[1][i].x > 0) {
1736 						ps[0][i].x -=  1;
1737 						point_decimal[i].x = 0;
1738 					}
1739 					if (point_decimal[i].y > dc[0]
1740 							&& ps[1][i].y < 0xffff) {
1741 						ps[0][i].y += 1;
1742 						point_decimal[i].y = 0;
1743 					}
1744 					if (point_decimal[i].y < -dc[0]
1745 							&& ps[1][i].y > 0) {
1746 						ps[0][i].y -=  1;
1747 						point_decimal[i].y = 0;
1748 					}
1749 				} else {
1750 					point_decimal[i].x = 0;
1751 					point_decimal[i].y = 0;
1752 				}
1753 			}
1754 		}
1755 
1756 	} else if (len >= 2) {
1757 		for (i = 0; i < 8 && i < len; i++) {
1758 			ds[i+1] = shake_all[i].dis;
1759 			dc[i+1] = shake_all[i].coe;
1760 		}
1761 		if (shake_all[0].coe >= 128
1762 				|| shake_all[0].coe <= shake_all[1].coe) {
1763 			ds[0] = ds[1];
1764 			dc[0] = dc[1];
1765 		} else {
1766 			ds[0] = ds[1] + (128 - shake_all[0].coe) *
1767 				(ds[1]-ds[2]) / (shake_all[0].coe - shake_all[1].coe);
1768 			dc[0] = 128;
1769 		}
1770 		for (i = 0; i < POINT_MAX; i++) {
1771 			if (ps[1][i].all == 0) {
1772 				for (j = 1; j < PS_DEEP; j++)
1773 					ps[j][i].all = ps[0][i].all;
1774 				save_dr[i] = 128;
1775 				save_dn[i] = 0;
1776 				continue;
1777 			}
1778 			if ((point_shake & (0x1 << i)) == 0)
1779 				continue;
1780 			dn = PointDistance(&pp[0][i], &ps[1][i]);
1781 			dn = Sqrt(dn);
1782 			if (dn >= ds[0])
1783 				continue;
1784 
1785 			if (dn < save_dn[i]) {
1786 				dr = save_dr[i];
1787 				save_dn[i] = dn;
1788 				ps[0][i].x = (int)ps[1][i].x + (((int)pp[0][i].x
1789 						- (int)ps[1][i].x) * dr) / 128;
1790 				ps[0][i].y = (int)ps[1][i].y + (((int)pp[0][i].y
1791 						- (int)ps[1][i].y) * dr) / 128;
1792 				continue;
1793 			}
1794 			for (j = 0; j <= len; j++) {
1795 				if (j == len || dn == 0) {
1796 					ps[0][i].x = ps[1][i].x;
1797 					ps[0][i].y = ps[1][i].y;
1798 					break;
1799 				} else if (ds[j] > dn && dn >= ds[j+1]) {
1800 					dr = dc[j+1] + ((dn - ds[j+1]) * (dc[j] - dc[j+1])) / (ds[j] - ds[j+1]);
1801 					save_dr[i] = dr;
1802 					save_dn[i] = dn;
1803 					/* ps[0][i].x = (int)ps[1][i].x + ((int)pp[0][i].x - (int)ps[1][i].x) * dr / dn / 128;*/
1804 					/* ps[0][i].y = (int)ps[1][i].y + ((int)pp[0][i].y - (int)ps[1][i].y) * dr / dn / 128;*/
1805 					ps[0][i].x = (int)ps[1][i].x + (((int)pp[0][i].x - (int)ps[1][i].x) * dr+64) / 128;
1806 					ps[0][i].y = (int)ps[1][i].y + (((int)pp[0][i].y - (int)ps[1][i].y) * dr+64) / 128;
1807 					break;
1808 				}
1809 			}
1810 		}
1811 	} else
1812 		return;
1813 }
1814 
ResetMask(void)1815 static void ResetMask(void)
1816 {
1817 	if (reset_mask_send)
1818 		reset_mask_send = 0;
1819 
1820 	if (global_state.mask)
1821 		return;
1822 	if (reset_mask_dis == 0 || reset_mask_type == 0)
1823 		return;
1824 	if (reset_mask_max == 0xfffffff1) {
1825 		if (point_num == 0)
1826 			reset_mask_max = 0xf0000000 + 1;
1827 		return;
1828 	}
1829 	if (reset_mask_max >  0xf0000000) {
1830 		reset_mask_max--;
1831 		if (reset_mask_max == 0xf0000000) {
1832 			reset_mask_send = reset_mask_type;
1833 			global_state.mask = 1;
1834 		}
1835 		return;
1836 	}
1837 	if (point_num > 1 || pp[0][0].all == 0) {
1838 		reset_mask_count = 0;
1839 		reset_mask_max = 0;
1840 		reset_mask_count = 0;
1841 		return;
1842 	}
1843 	reset_mask_count++;
1844 	if (reset_mask_max == 0)
1845 		reset_mask_max = pp[0][0].all;
1846 	else
1847 		if (PointDistance((gsl_POINT_TYPE *)(&reset_mask_max),
1848 				pp[0]) > (((unsigned int)reset_mask_dis) & 0xffffff)
1849 				&& reset_mask_count > (((unsigned int)reset_mask_dis) >> 24))
1850 			reset_mask_max = 0xfffffff1;
1851 }
1852 
ConfigCoorMulti(int data[])1853 static int ConfigCoorMulti(int data[])
1854 {
1855 	int i, j;
1856 	int n = 0;
1857 
1858 	for (i = 0; i < 4; i++) {
1859 		if (data[247+i] != 0) {
1860 			if ((data[247 + i] & 63) == 0 && (data[247 + i] >> 16) < 4)
1861 				n++;
1862 			else
1863 				return FALSE;
1864 		}
1865 		if (data[251 + i] != 0) {
1866 			if ((data[251 + i] & 63) == 0 && (data[251 + i] >> 16) < 4)
1867 				n++;
1868 			else
1869 				return FALSE;
1870 		}
1871 	}
1872 	if (n == 0 || n > 4)
1873 		return FALSE;
1874 	for (j = 0; j < n; j++) {
1875 		for (i = 0; i < 64; i++) {
1876 			if (data[256 + j * 64 + i] >= 64)
1877 				return FALSE;
1878 			if (i) {
1879 				if (data[256 + j * 64 + i] < data[256 + j * 64 + i - 1])
1880 					return FALSE;
1881 			}
1882 		}
1883 	}
1884 	return TRUE;
1885 }
1886 
ConfigFilter(unsigned int data[])1887 static int ConfigFilter(unsigned int data[])
1888 {
1889 	int i;
1890 	unsigned int ps_c[8];
1891 	unsigned int pr_c[8];
1892 	unsigned int sum = 0;
1893 
1894 	if (data[242] > 1 && (data[255] >= 0 && data[255] <= 256)) {
1895 		for (i = 0; i < 8; i++) {
1896 			ps_c[i] = (data[243 + i / 4] >> ((i % 4) * 8)) & 0xff;
1897 			pr_c[i] = (data[243 + i / 4 + 2] >> ((i % 4) * 8)) & 0xff;
1898 			if (ps_c[i] >= 0x80)
1899 				ps_c[i] |= 0xffffff00;
1900 			if (pr_c[i] >= 0x80)
1901 				pr_c[i] |= 0xffffff00;
1902 			sum += ps_c[i];
1903 			sum += pr_c[i];
1904 		}
1905 		if (sum == data[242] || sum + data[242] == 0)
1906 			return TRUE;
1907 	}
1908 	return FALSE;
1909 }
1910 
ConfigKeyMap(int data[])1911 static int ConfigKeyMap(int data[])
1912 {
1913 	int i;
1914 
1915 	if (data[217] != 1)
1916 		return FALSE;
1917 	for (i = 0; i < 8; i++) {
1918 		if (data[218 + 2] == 0)
1919 			return FALSE;
1920 		if ((data[218+i*3+0] >> 16) > (data[218+i*3+0]&0xffff))
1921 			return FALSE;
1922 		if ((data[218+i*3+1] >> 16) > (data[218+i*3+1]&0xffff))
1923 			return FALSE;
1924 	}
1925 	return TRUE;
1926 }
1927 
DiagonalDistance(gsl_POINT_TYPE * p,int type)1928 static int DiagonalDistance(gsl_POINT_TYPE *p, int type)
1929 {
1930 	int divisor, square;
1931 
1932 	divisor = ((int)sen_num_nokey * (int)sen_num_nokey + (int)drv_num_nokey *
1933 						(int)drv_num_nokey) / 16;
1934 	if (divisor == 0)
1935 		divisor = 1;
1936 	if (type == 0)
1937 		square = ((int)sen_num_nokey * (int)(p->x) - (int)drv_num_nokey * (int)(p->y)) / 4;
1938 	else
1939 		square = ((int)sen_num_nokey * (int)(p->x) + (int)drv_num_nokey *
1940 				(int)(p->y) - (int)sen_num_nokey * (int)drv_num_nokey * 64) / 4;
1941 	return square * square / divisor;
1942 }
1943 
DiagonalCompress(gsl_POINT_TYPE * p,int type,int dis,int dis_max)1944 static void DiagonalCompress(gsl_POINT_TYPE *p, int type, int dis, int dis_max)
1945 {
1946 	int x, y;
1947 	int tx, ty;
1948 	int cp_ceof;
1949 
1950 	if (dis_max == 0)
1951 		return;
1952 	if (dis > dis_max)
1953 		cp_ceof = (dis - dis_max)*128/(3*dis_max) + 128;
1954 	else
1955 		cp_ceof = 128;
1956 	if (cp_ceof > 256)
1957 		cp_ceof = 256;
1958 	x = p->x;
1959 	y = p->y;
1960 	if (type)
1961 		y = (int)sen_num_nokey*64 - y;
1962 	x *=  (int)sen_num_nokey;
1963 	y *=  (int)drv_num_nokey;
1964 	tx = x;
1965 	ty = y;
1966 	x = ((tx+ty)+(tx-ty)*cp_ceof/256)/2;
1967 	y = ((tx+ty)+(ty-tx)*cp_ceof/256)/2;
1968 	x /= (int)sen_num_nokey;
1969 	y /= (int)drv_num_nokey;
1970 	if (type)
1971 		y = sen_num_nokey*64 - y;
1972 	if (x < 1)
1973 		x = 1;
1974 	if (y < 1)
1975 		y = 1;
1976 	if (x >= (int)drv_num_nokey*64)
1977 		x = drv_num_nokey*64 - 1;
1978 	if (y >= (int)sen_num_nokey*64)
1979 		y = (int)sen_num_nokey*64 - 1;
1980 	p->x = x;
1981 	p->y = y;
1982 }
1983 
PointDiagonal(void)1984 static void PointDiagonal(void)
1985 {
1986 	int i;
1987 	int diagonal_size;
1988 	int dis;
1989 	unsigned int diagonal_start;
1990 
1991 	if (diagonal == 0)
1992 		return;
1993 	diagonal_size = diagonal * diagonal;
1994 	diagonal_start = diagonal * 3 / 2;
1995 	for (i = 0; i < POINT_MAX; i++) {
1996 		if (ps[0][i].all == 0 || ps[0][i].key != 0) {
1997 			point_corner &= ~(0x3 << i*2);
1998 			continue;
1999 		} else if ((point_corner & (0x3 << i*2)) == 0) {
2000 			if ((ps[0][i].x <= diagonal_start && ps[0][i].y <= diagonal_start)
2001 					|| (ps[0][i].x >= drv_num_nokey * 64 - diagonal_start
2002 					&& ps[0][i].y >= sen_num_nokey*64 - diagonal_start))
2003 				point_corner |= 0x2 << i*2;
2004 			else if ((ps[0][i].x <= diagonal_start && ps[0][i].y >= sen_num_nokey * 64 - diagonal_start)
2005 					|| (ps[0][i].x >= drv_num_nokey*64 - diagonal_start
2006 					&& ps[0][i].y <= diagonal_start))
2007 				point_corner |= 0x3 << i*2;
2008 			else
2009 				point_corner |= 0x1 << i*2;
2010 		}
2011 		if (point_corner & (0x2 << i*2)) {
2012 			dis = DiagonalDistance(&(ps[0][i]), point_corner & (0x1 << i*2));
2013 			if (dis <= diagonal_size * 4) {
2014 				DiagonalCompress(&(ps[0][i]), point_corner &
2015 					(0x1 << i*2), dis, diagonal_size);
2016 			} else if (dis > diagonal_size * 4) {
2017 				point_corner &= ~(0x3 << i * 2);
2018 				point_corner |= 0x1 << i * 2;
2019 			}
2020 		}
2021 	}
2022 }
2023 
2024 
PointSlope(int i,int j)2025 static int PointSlope(int i, int j)
2026 {
2027 	int x, y;
2028 
2029 	x = pr[j][i].x - pr[j+1][i].x;
2030 	x = x*x;
2031 	y = pr[j][i].y - pr[j+1][i].y;
2032 	y = y * y;
2033 	if (x + y == 0)
2034 		return -1;
2035 	if (x > y)
2036 		return x * 1024 / (x + y);
2037 	else
2038 		return y * 1024 / (x + y);
2039 }
2040 
PointExtend(void)2041 static void PointExtend(void)
2042 {
2043 	int i, j;
2044 	int x, y;
2045 	int t, t2;
2046 	int extend_len = 5;
2047 
2048 	if (point_extend == 0)
2049 		return;
2050 	for (i = 0; i < POINT_MAX; i++) {
2051 		if (pr[0][i].fill == 0)
2052 			continue;
2053 		for (j = 0; j < extend_len; j++) {
2054 			if (pr[j][i].all == 0)
2055 				break;
2056 		}
2057 		if (j < extend_len)
2058 			continue;
2059 		if (PointDistance(&pr[1][i], &pr[2][i]) < 16*16)
2060 			continue;
2061 		t = PointSlope(i, 1);
2062 		for (j = 2; j < extend_len-1; j++) {
2063 			t2 = PointSlope(i, j);
2064 			if (t2 < 0 || t2 < t * (128 - point_extend) / 128 ||
2065 					t2 > t * (128 + point_extend) / 128)
2066 				break;
2067 		}
2068 		if (j < extend_len - 1)
2069 			continue;
2070 		x = 3*pr[1][i].x - 2*pr[2][i].x;
2071 		y = 3*pr[1][i].y - 2*pr[2][i].y;
2072 		pr[0][i].all = PointRange(x, y, pr[1][i].x, pr[1][i].y);
2073 	}
2074 }
2075 
PressureSave(void)2076 static void PressureSave(void)
2077 {
2078 	int i;
2079 
2080 	if ((point_num & 0x1000) == 0)
2081 		return;
2082 
2083 	for (i = 0; i < POINT_MAX; i++) {
2084 		pressure_now[i] = point_now[i].all >> 28;
2085 		point_now[i].all &= ~(0xf << 28);
2086 	}
2087 }
2088 
PointPressure(void)2089 static void PointPressure(void)
2090 {
2091 	int i, j;
2092 
2093 	for (i = 0; i < POINT_MAX; i++) {
2094 		if (pa[0][i] != 0 && pa[1][i] == 0) {
2095 			pressure_report[i] = pa[0][i] * 5;
2096 			for (j = 1; j < PRESSURE_DEEP; j++)
2097 				pa[j][i] = pa[0][i];
2098 			continue;
2099 		}
2100 		j = (pressure_report[i]+1) / 2 + pa[0][i] + pa[1][i] +
2101 				(pa[2][i]+1) / 2 - pressure_report[i];
2102 		if (j >= 2)
2103 			j -= 2;
2104 		else if (j <= -2)
2105 			j += 2;
2106 		else
2107 			j = 0;
2108 		pressure_report[i] = pressure_report[i]+j;
2109 	}
2110 }
2111 
gsl_ReportPressure(unsigned int * p)2112 void gsl_ReportPressure(unsigned int *p)
2113 {
2114 	int i;
2115 
2116 	for (i = 0; i < POINT_MAX; i++) {
2117 		if (i < point_num) {
2118 			if (pressure_now[i] == 0)
2119 				p[i] = 0;
2120 			else if (pressure_now[i] <= 7)
2121 				p[i] = 1;
2122 			else if (pressure_now[i] > 63+7)
2123 				p[i] = 63;
2124 			else
2125 				p[i] = pressure_now[i] - 7;
2126 		} else
2127 			p[i] = 0;
2128 	}
2129 }
2130 
gsl_TouchNear(void)2131 int  gsl_TouchNear(void)
2132 {
2133 		return 0;
2134 }
2135 
DoubleClick(void)2136 static void DoubleClick(void)
2137 {
2138 	int i;
2139 	unsigned int width[3];
2140 
2141 	double_click = 0;
2142 
2143 	if (point_num >= 2 || (point_num == 1 && pp[0][0].all == 0)
2144 							|| pp[0][0].key) {
2145 
2146 		for (i = 0; i < ARRAY_SIZE(click_count); i++)
2147 			click_count[i] = 0;
2148 		return;
2149 	}
2150 	if (point_num != 0 && prev_num == 0) {
2151 		for (i = ARRAY_SIZE(click_count) - 1; i > 0; i--)
2152 			click_count[i] = click_count[i-1];
2153 		click_count[0] = csensor_count;
2154 		for (i = ARRAY_SIZE(point_click) - 1; i > 1; i--)
2155 			point_click[i].all = point_click[i-2].all;
2156 		point_click[0].all = pp[0][0].all;
2157 		point_click[1].all = pp[0][0].all;
2158 	}
2159 	if (point_num != 0 && prev_num != 0) {
2160 		if (PointDistance(&point_click[1], &pp[0][0]) >
2161 				PointDistance(&point_click[1], &point_click[0]))
2162 			point_click[0].all = pp[0][0].all;
2163 	}
2164 	if (point_num == 0 && prev_num != 0) {
2165 		for (i = ARRAY_SIZE(click_count) - 1; i > 0; i--)
2166 			click_count[i] = click_count[i-1];
2167 		click_count[0] = csensor_count;
2168 		for (i = 0; i < ARRAY_SIZE(click_count) - 1; i++)
2169 			width[i] = (click_count[i] - click_count[i+1]) & 0xffff;
2170 		if (!(width[0] >= double_down*average && width[2] >= double_down
2171 				* average && width[1] <= double_up*average))
2172 			return;
2173 
2174 		if (PointDistance(&point_click[0], &point_click[1]) > 64*64
2175 		|| PointDistance(&point_click[2], &point_click[3]) > 64*64
2176 		|| PointDistance(&point_click[1], &point_click[3]) > 128*128)
2177 			return;
2178 
2179 		for (i = 0; i < ARRAY_SIZE(click_count); i++)
2180 			click_count[i] = 0;
2181 		double_click = '*';
2182 	}
2183 }
2184 
gsl_id_reg_init(int flag)2185 static void gsl_id_reg_init(int flag)
2186 {
2187 	int i, j;
2188 
2189 	for (j = 0; j < POINT_DEEP; j++)
2190 		for (i = 0; i < POINT_MAX; i++)
2191 			point_array[j][i].all = 0;
2192 	for (j = 0; j < PRESSURE_DEEP; j++)
2193 		for (i = 0; i < POINT_MAX; i++)
2194 			pressure_array[j][i] = 0;
2195 	for (i = 0; i < POINT_MAX; i++) {
2196 		point_delay[i].all = 0;
2197 		filter_deep[i] = 0;
2198 		point_decimal[i].all = 0;
2199 	}
2200 	point_edge.rate = 0;
2201 	point_n = 0;
2202 	if (flag)
2203 		point_num = 0;
2204 	prev_num = 0;
2205 	point_shake = 0;
2206 	reset_mask_send = 0;
2207 	reset_mask_max = 0;
2208 	reset_mask_count = 0;
2209 	point_near = 0;
2210 	point_corner = 0;
2211 	global_state.all = 0;
2212 	double_click = 0;
2213 	inte_count = 0;
2214 	csensor_count = 0;
2215 #ifdef GESTURE_LICH
2216 	GestureInit();
2217 #endif
2218 }
2219 
DataCheck(void)2220 static int DataCheck(void)
2221 {
2222 	if (drv_num == 0 || drv_num_nokey == 0
2223 			|| sen_num == 0 || sen_num_nokey == 0)
2224 		return 0;
2225 	if (screen_x_max == 0 || screen_y_max == 0)
2226 		return 0;
2227 	return 1;
2228 }
2229 
gsl_DataInit(unsigned int * conf_in)2230 void gsl_DataInit(unsigned int *conf_in)
2231 {
2232 	int i;
2233 	unsigned int *conf;
2234 	int len;
2235 
2236 	gsl_id_reg_init(1);
2237 	for (i = 0; i < POINT_MAX; i++)
2238 		point_now[i].all = 0;
2239 	conf = config_static;
2240 	coordinate_correct_able = 0;
2241 	for (i = 0; i < 32; i++) {
2242 		coordinate_correct_coe_x[i] = i;
2243 		coordinate_correct_coe_y[i] = i;
2244 	}
2245 	id_first_coe = 8;
2246 	id_speed_coe = 128 * 128;
2247 	id_static_coe = 64 * 64;
2248 	average = 3 + 1;
2249 	soft_average = 3;
2250 	report_delay = 0;
2251 	report_ahead = 0x9249249;
2252 	for (i = 0; i < 4; i++)
2253 		median_dis[i] = 0;
2254 	shake_min = 0*0;
2255 	for (i = 0; i < 2; i++) {
2256 		match_y[i] = 0;
2257 		match_x[i] = 0;
2258 		ignore_y[i] = 0;
2259 		ignore_x[i] = 0;
2260 	}
2261 	match_y[0] = 4096;
2262 	match_x[0] = 4096;
2263 	screen_y_max = 480;
2264 	screen_x_max = 800;
2265 	point_num_max = 10;
2266 	drv_num = 16;
2267 	sen_num = 10;
2268 	drv_num_nokey = 16;
2269 	sen_num_nokey = 10;
2270 	for (i = 0; i < 4; i++)
2271 		edge_cut[i] = 0;
2272 	for (i = 0; i < 32; i++)
2273 		stretch_array[i] = 0;
2274 	for (i = 0; i < 16; i++)
2275 		shake_all_array[i] = 0;
2276 	reset_mask_dis = 0;
2277 	reset_mask_type = 0;
2278 	diagonal = 0;
2279 	point_extend = 0;
2280 	key_map_able = 0;
2281 	for (i = 0; i < 8 * 3; i++)
2282 		key_range_array[i] = 0;
2283 	filter_able = 0;
2284 	filter_coe[0] = (0 << 6*4)+(0 << 6*3)+(0 << 6*2)+(40 << 6*1)+(24 << 6*0);
2285 	filter_coe[1] = (0 << 6*4)+(0 << 6*3)+(16 << 6*2)+(24 << 6*1)+(24 << 6*0);
2286 	filter_coe[2] = (0 << 6*4)+(16 << 6*3)+(24 << 6*2)+(16 << 6*1)+(8 << 6*0);
2287 	filter_coe[3] = (6 << 6*4)+(16 << 6*3)+(24 << 6*2)+(12 << 6*1)+(6 << 6*0);
2288 	for (i = 0; i < 4; i++) {
2289 		multi_x_array[i] = 0;
2290 		multi_y_array[i] = 0;
2291 	}
2292 	point_repeat[0] = 32;
2293 	point_repeat[1] = 96;
2294 	edge_first = 0;
2295 	edge_first_coe = 0x80;
2296 	if (conf_in == NULL)
2297 		return;
2298 
2299 	if (conf_in[0] <= 0xfff) {
2300 		if (ConfigCoorMulti(conf_in))
2301 			len = 512;
2302 		else if (ConfigFilter(conf_in))
2303 			len = 256;
2304 		else if (ConfigKeyMap(conf_in))
2305 			len = 241;
2306 		else
2307 			len = 215;
2308 	} else if (conf_in[1] <= CONFIG_LENGTH)
2309 		len = conf_in[1];
2310 	else
2311 		len = CONFIG_LENGTH;
2312 	for (i = 0; i < len; i++)
2313 		conf[i] = conf_in[i];
2314 	for (; i < CONFIG_LENGTH; i++)
2315 		conf[i] = 0;
2316 	if (conf_in[0] <= 0xfff) {
2317 		coordinate_correct_able = conf[0];
2318 		drv_num = conf[1];
2319 		sen_num = conf[2];
2320 		drv_num_nokey = conf[3];
2321 		sen_num_nokey = conf[4];
2322 		id_first_coe = conf[5];
2323 		id_speed_coe = conf[6];
2324 		id_static_coe = conf[7];
2325 		average = conf[8];
2326 		soft_average = conf[9];
2327 
2328 		report_delay = conf[13];
2329 		shake_min = conf[14];
2330 		screen_y_max = conf[15];
2331 		screen_x_max = conf[16];
2332 		point_num_max = conf[17];
2333 		global_flag.all = conf[18];
2334 		for (i = 0; i < 4; i++)
2335 			median_dis[i] = conf[19+i];
2336 		for (i = 0; i < 2; i++) {
2337 			match_y[i] = conf[23+i];
2338 			match_x[i] = conf[25+i];
2339 			ignore_y[i] = conf[27+i];
2340 			ignore_x[i] = conf[29+i];
2341 		}
2342 		for (i = 0; i < 64; i++) {
2343 			coordinate_correct_coe_x[i] = conf[31+i];
2344 			coordinate_correct_coe_y[i] = conf[95+i];
2345 		}
2346 		for (i = 0; i < 4; i++)
2347 			edge_cut[i] = conf[159+i];
2348 		for (i = 0; i < 32; i++)
2349 			stretch_array[i] = conf[163+i];
2350 		for (i = 0; i < 16; i++)
2351 			shake_all_array[i] = conf[195+i];
2352 		reset_mask_dis = conf[213];
2353 		reset_mask_type = conf[214];
2354 		key_map_able = conf[217];
2355 		for (i = 0; i < 8*3; i++)
2356 			key_range_array[i] = conf[218+i];
2357 		filter_able = conf[242];
2358 		for (i = 0; i < 4; i++)
2359 			filter_coe[i] = conf[243+i];
2360 		for (i = 0; i < 4; i++)
2361 			multi_x_array[i] = conf[247+i];
2362 		for (i = 0; i < 4; i++)
2363 			multi_y_array[i] = conf[251+i];
2364 		diagonal = conf[255];
2365 		for (i = 0; i < 256; i++)
2366 			*(&multi_group[0][0]+i) = conf[256+i];
2367 		for (i = 0; i < 32; i++) {
2368 			*(&ps_coe[0][0]+i) = conf[256 + 64 * 3 + i];
2369 			*(&pr_coe[0][0]+i) = conf[256 + 64 * 3 + i + 32];
2370 		}
2371 		near_set[0] = 0;
2372 		near_set[1] = 0;
2373 	} else {
2374 		global_flag.all = conf[0x10];
2375 		point_num_max = conf[0x11];
2376 		drv_num = conf[0x12] & 0xffff;
2377 		sen_num = conf[0x12] >> 16;
2378 		drv_num_nokey = conf[0x13] & 0xffff;
2379 		sen_num_nokey = conf[0x13] >> 16;
2380 		screen_x_max = conf[0x14] & 0xffff;
2381 		screen_y_max = conf[0x14] >> 16;
2382 		average = conf[0x15];
2383 		reset_mask_dis = conf[0x16];
2384 		reset_mask_type = conf[0x17];
2385 		point_repeat[0] = conf[0x18] >> 16;
2386 		point_repeat[1] = conf[0x18] & 0xffff;
2387 		near_set[0] = conf[0x19] >> 16;
2388 		near_set[1] = conf[0x19] & 0xffff;
2389 		diagonal = conf[0x1a];
2390 		point_extend = conf[0x1b];
2391 
2392 		id_first_coe = conf[0x20];
2393 		id_speed_coe = conf[0x21];
2394 		id_static_coe = conf[0x22];
2395 		match_y[0] = conf[0x23] >> 16;
2396 		match_y[1] = conf[0x23] & 0xffff;
2397 		match_x[0] = conf[0x24] >> 16;
2398 		match_x[1] = conf[0x24] & 0xffff;
2399 		ignore_y[0] = conf[0x25] >> 16;
2400 		ignore_y[1] = conf[0x25] & 0xffff;
2401 		ignore_x[0] = conf[0x26] >> 16;
2402 		ignore_x[1] = conf[0x26] & 0xffff;
2403 		edge_cut[0] = (conf[0x27] >> 24) & 0xff;
2404 		edge_cut[1] = (conf[0x27] >> 16) & 0xff;
2405 		edge_cut[2] = (conf[0x27] >> 8) & 0xff;
2406 		edge_cut[3] = (conf[0x27] >> 0) & 0xff;
2407 		report_delay = conf[0x28];
2408 		shake_min = conf[0x29];
2409 		for (i = 0; i < 16; i++) {
2410 			stretch_array[i * 2 + 0] = conf[0x2a+i] & 0xffff;
2411 			stretch_array[i * 2 + 1] = conf[0x2a+i] >> 16;
2412 		}
2413 		for (i = 0; i < 8; i++) {
2414 			shake_all_array[i * 2 + 0] = conf[0x3a + i] & 0xffff;
2415 			shake_all_array[i * 2 + 1] = conf[0x3a + i] >> 16;
2416 		}
2417 		report_ahead			 =  conf[0x42];
2418 		/* key_dead_time		 =  conf[0x43];
2419 		 * point_dead_time		 =  conf[0x44];
2420 		 * point_dead_time2		 =  conf[0x45];
2421 		 * point_dead_distance		 =  conf[0x46];
2422 		 * point_dead_distance2		 =  conf[0x47];
2423 		 */
2424 		edge_first			 =  conf[0x48];
2425 		edge_first_coe			 =  conf[0x49];
2426 
2427 		key_map_able = conf[0x60];
2428 		for (i = 0; i < 8 * 3; i++)
2429 			key_range_array[i] = conf[0x61+i];
2430 
2431 		coordinate_correct_able = conf[0x100];
2432 		for (i = 0; i < 4; i++) {
2433 			multi_x_array[i] = conf[0x101 + i];
2434 			multi_y_array[i] = conf[0x105 + i];
2435 		}
2436 		for (i = 0; i < 64; i++) {
2437 			coordinate_correct_coe_x[i] = (conf[0x109 + i / 4] >> (i % 4 * 8)) & 0xff;
2438 			coordinate_correct_coe_y[i] = (conf[0x109 + 64 / 4 + i / 4] >> (i % 4 * 8)) & 0xff;
2439 		}
2440 		for (i = 0; i < 256; i++) {
2441 			/*multi_group[0][i] = (conf[0x109+64/4*2+i/4] >> (i%4*8)) & 0xff;*/
2442 			*(&multi_group[0][0]+i) = (conf[0x109+64/4*2+i/4] >> (i%4*8)) & 0xff;
2443 		}
2444 
2445 		filter_able = conf[0x180];
2446 		for (i = 0; i < 4; i++)
2447 			filter_coe[i] = conf[0x181 + i];
2448 		for (i = 0; i < 4; i++)
2449 			median_dis[i] = conf[0x185 + i];
2450 		for (i = 0; i < 32; i++) {
2451 			*(&ps_coe[0][0]+i) = conf[0x189 + i];
2452 			*(&pr_coe[0][0]+i) = conf[0x189 + i + 32];
2453 		}
2454 #ifdef GESTURE_LICH
2455 		GestureSet(&conf[0x189 + 64]);
2456 #endif
2457 	}
2458 	if (average == 0)
2459 		average = 4;
2460 	for (i = 0; i < 8; i++) {
2461 		if (shake_all_array[i * 2] & 0x8000)
2462 			shake_all_array[i * 2] = shake_all_array[i * 2] & ~0x8000;
2463 		else
2464 			shake_all_array[i * 2] = Sqrt(shake_all_array[i * 2]);
2465 	}
2466 	for (i = 0; i < 2; i++) {
2467 		if (match_x[i] & 0x8000)
2468 			match_x[i] |= 0xffff0000;
2469 		if (match_y[i] & 0x8000)
2470 			match_y[i] |= 0xffff0000;
2471 		if (ignore_x[i] & 0x8000)
2472 			ignore_x[i] |= 0xffff0000;
2473 		if (ignore_y[i] & 0x8000)
2474 			ignore_y[i] |= 0xffff0000;
2475 	}
2476 	for (i = 0; i < CONFIG_LENGTH; i++)
2477 		config_static[i] = 0;
2478 }
2479 
gsl_version_id(void)2480 unsigned int gsl_version_id(void)
2481 {
2482 	return GSL_VERSION;
2483 }
2484 
gsl_mask_tiaoping(void)2485 unsigned int gsl_mask_tiaoping(void)
2486 {
2487 	return reset_mask_send;
2488 }
2489 
GetFlag(void)2490 static void GetFlag(void)
2491 {
2492 	int i = 0;
2493 	int num_save;
2494 
2495 	if ((point_num & 0x8000) != 0) {
2496 		if ((point_num&0xff000000) == 0x5a000000)
2497 			gesture_last = (point_num >> 16) & 0xff;
2498 	}
2499 	if (((point_num & 0x100) != 0)
2500 			|| ((point_num & 0x200)
2501 			!= 0 && global_state.reset == 1))
2502 		gsl_id_reg_init(0);
2503 
2504 	if ((point_num & 0x300) == 0)
2505 		global_state.reset = 1;
2506 
2507 	if (point_num & 0x400)
2508 		global_state.only = 1;
2509 	else
2510 		global_state.only = 0;
2511 	if (point_num & 0x2000)
2512 		global_state.interpolation = 0xf;
2513 	else if (global_state.interpolation)
2514 		global_state.interpolation--;
2515 	if (point_num & 0x4000)
2516 		global_state.ex = 1;
2517 	else
2518 		global_state.ex = 0;
2519 	inte_count++;
2520 	csensor_count = ((unsigned int)point_num) >> 16;
2521 	num_save = point_num & 0xff;
2522 	if (num_save > POINT_MAX)
2523 		num_save = POINT_MAX;
2524 	for (i = 0; i < POINT_MAX; i++) {
2525 		if (i >= num_save)
2526 			point_now[i].all = 0;
2527 	}
2528 	point_num = (point_num & (~0xff)) + num_save;
2529 }
2530 
gsl_FunIICRead(unsigned int (* fun)(unsigned int *,unsigned int,unsigned int))2531 void gsl_FunIICRead(unsigned int (*fun) (unsigned int *, unsigned int, unsigned int))
2532 {
2533 	ReadIICInt = fun;
2534 }
2535 
GestureJoint(void)2536 static int GestureJoint(void)
2537 {
2538 	unsigned int buf[128];
2539 	unsigned int len = 0;
2540 	unsigned int addr = 0;
2541 	unsigned int i;
2542 
2543 	if ((point_num & 0x8000) == 0)
2544 		return FALSE;
2545 	if ((point_num & 0xff000000) != 0x5b000000)
2546 		return FALSE;
2547 	if (ReadIICInt == NULL)
2548 		return FALSE;
2549 	addr = point_now[0].all;
2550 	len = point_now[1].all;
2551 	if (len > 128)
2552 		len = 128;
2553 	ReadIICInt(buf, addr, len);
2554 	if (len < 8) {
2555 		gesture_last = 0x7fffffff;
2556 		return TRUE;
2557 	}
2558 	for (i = 0; i < POINT_MAX; i++)
2559 		point_now[i].all = 0;
2560 	GestureInit();
2561 	for (i = 0; i < len+5; i++) {
2562 		if (i < len) {
2563 			point_num = 1;
2564 			point_now[0].all = buf[i];
2565 			if (buf[i] == 0 || (buf[i] & 0xe000b000) != 0) {
2566 				GestureInit();
2567 				break;
2568 			}
2569 		} else {
2570 			point_num = 0;
2571 			point_now[0].all = 0;
2572 		}
2573 		PointCoor();
2574 		CoordinateCorrect();
2575 		PointEdge();
2576 		PointPointer();
2577 		PointPredict();
2578 		PointId();
2579 		PointNewId();
2580 		PointOrder();
2581 		PointStretch();
2582 		PointFilter();
2583 		GetPointNum(pr[0]);
2584 		if (i < len + 2)
2585 			GestureMain(&(pr[0][0].all), point_num);
2586 	}
2587 	point_num = 0;
2588 	for (i = 0; i < POINT_MAX; i++)
2589 		point_now[i].all = 0;
2590 	return TRUE;
2591 }
gsl_alg_id_main(struct gsl_touch_info * cinfo)2592 void gsl_alg_id_main(struct gsl_touch_info *cinfo)
2593 {
2594 	int i;
2595 
2596 	point_num = cinfo->finger_num;
2597 	for (i = 0; i < POINT_MAX; i++)
2598 		point_now[i].all = (cinfo->id[i] << 28) | (cinfo->x[i] << 16)
2599 							| cinfo->y[i];
2600 
2601 	if (GestureJoint()) {
2602 		cinfo->finger_num = 0;
2603 		for (i = 0; i < POINT_MAX; i++) {
2604 			cinfo->id[i] = 0;
2605 			cinfo->x[i] = 0;
2606 			cinfo->y[i] = 0;
2607 		}
2608 		return;
2609 	}
2610 	GetFlag();
2611 	if (DataCheck() == 0) {
2612 		point_num = 0;
2613 		cinfo->finger_num = 0;
2614 		return;
2615 	}
2616 	PressureSave();
2617 	PointCoor();
2618 	CoordinateCorrect();
2619 	PointEdge();
2620 	PointRepeat();
2621 	GetPointNum(point_now);
2622 	PointPointer();
2623 	PointPredict();
2624 	PointId();
2625 	PointNewId();
2626 	PointOrder();
2627 	PointCross();
2628 	GetPointNum(pp[0]);
2629 	DoubleClick();
2630 	prev_num = point_num;
2631 	ResetMask();
2632 	PointStretch();
2633 	PointDiagonal();
2634 	PointFilter();
2635 	GetPointNum(pr[0]);
2636 #ifdef GESTURE_LICH
2637 	GestureMain(&(pr[0][0].all), point_num);
2638 #endif
2639 	PointDelay();
2640 	PointExtend();
2641 	PointPressure();
2642 	PointReport(cinfo);
2643 }
2644 
2645 #ifdef GESTURE_LICH
2646 
gsl_obtain_gesture(void)2647 int gsl_obtain_gesture(void)
2648 {
2649 	return GestureDeal();
2650 }
2651 
GestureMain(unsigned int data_coor[],unsigned int num)2652 static int GestureMain(unsigned int data_coor[], unsigned int num)
2653 {
2654 	gesture_deal = FALSE;
2655 	if (gesture_dis_min == 0)
2656 		return FALSE;
2657 	gesture_multi = GestureMulti(data_coor);
2658 	if (gesture_multi) {
2659 		gesture_deal = GESTURE_DEAL;
2660 		return TRUE;
2661 	}
2662 	if (num == 0) {
2663 		if (gesture_num == 0)
2664 			return FALSE;
2665 		if (gesture_num <= 8) {
2666 			GestureInit();
2667 			return FALSE;
2668 		}
2669 		gesture_deal = GESTURE_ALL;
2670 		return TRUE;
2671 	} else if (gesture_num < 0)
2672 		return FALSE;
2673 
2674 	else if (num == 1 && data_coor[0] != 0) {
2675 		GesturePush((GESTURE_POINT_TYPE *)data_coor);
2676 		return FALSE;
2677 	} else {
2678 		gesture_num = -1;
2679 		return FALSE;
2680 	}
2681 
2682 	return TRUE;
2683 }
2684 
GestureSqrt(int d)2685 static int GestureSqrt(int d)
2686 {
2687 	int ret = 0;
2688 	int i;
2689 
2690 	for (i = 14; i >= 0; i--) {
2691 		if ((ret + (0x1 << i)) * (ret + (0x1 << i)) <= d)
2692 			ret |= (0x1 << i);
2693 	}
2694 	return ret;
2695 }
2696 
GestureDistance(GESTURE_POINT_TYPE * d1,GESTURE_POINT_TYPE * d2,int sqrt_able)2697 static int GestureDistance(GESTURE_POINT_TYPE *d1, GESTURE_POINT_TYPE *d2, int sqrt_able)
2698 {
2699 	if (sqrt_able)
2700 		return GestureSqrt((d1->x-d2->x)*(d1->x-d2->x) + (d1->y-d2->y)*(d1->y-d2->y));
2701 	else
2702 		return (d1->x-d2->x)*(d1->x-d2->x) + (d1->y-d2->y)*(d1->y-d2->y);
2703 }
2704 
GesturePush(GESTURE_POINT_TYPE * data)2705 static int GesturePush(GESTURE_POINT_TYPE *data)
2706 {
2707 	unsigned int data_i = data->all & 0xffff0fff;
2708 
2709 	if (gesture_num >= GESTURE_BUF_SIZE)
2710 		return FALSE;
2711 	if (gesture_flag & 0x2)
2712 		data_i = ((drv_num_nokey * 64 - (data_i >> 16)) << 16) + (data_i & 0xffff);
2713 	if (gesture_flag & 0x4)
2714 		data_i = (data_i & 0xffff0000) + ((sen_num_nokey * 64 - (data_i & 0xffff)) & 0xffff);
2715 	if (gesture_flag & 0x8)
2716 		data_i = ((data_i & 0xfff) << 16) + ((data_i >> 16) & 0xfff);
2717 	if (gesture_num == 0) {
2718 		gesture_buf[gesture_num++].all = data_i;
2719 		return TRUE;
2720 	}
2721 	if (GestureDistance(data, &gesture_buf[gesture_num-1], TRUE) <= gesture_dis_min)
2722 		return FALSE;
2723 	gesture_buf[gesture_num++].all = data_i;
2724 		return TRUE;
2725 }
2726 
GestureInit(void)2727 static void GestureInit(void)
2728 {
2729 	int i;
2730 
2731 	gesture_num_last = gesture_num;
2732 	gesture_num = 0;
2733 	gesture_deal = FALSE;
2734 	if (gesture_dis_min < 0 || gesture_dis_min > 64)
2735 		gesture_dis_min = 2;
2736 	for (i = 0; i < 5; i++) {
2737 		multi_x[i].count = 0;
2738 		multi_o[i].count = 0;
2739 	}
2740 }
2741 
GestureStretch(void)2742 static int GestureStretch(void)
2743 {
2744 	unsigned int x_max = 0, x_min = 0xffff, y_max = 0, y_min = 0xffff;
2745 	int i;
2746 
2747 	/*if (gesture_num <= GESTURE_SIZE_NUM/2)*/
2748 		/*return FALSE; */
2749 	if (gesture_num >= GESTURE_BUF_SIZE)
2750 		return FALSE;
2751 	for (i = 0; i < gesture_num; i++) {
2752 		if (gesture_buf[i].x > x_max)
2753 			x_max = gesture_buf[i].x;
2754 		if (gesture_buf[i].x < x_min)
2755 			x_min = gesture_buf[i].x;
2756 		if (gesture_buf[i].y > y_max)
2757 			y_max = gesture_buf[i].y;
2758 		if (gesture_buf[i].y < y_min)
2759 			y_min = gesture_buf[i].y;
2760 	}
2761 	if (x_max < x_min+64*2 || y_max < y_min+64*3)
2762 		return FALSE;
2763 	for (i = 0; i < gesture_num; i++) {
2764 		gesture_buf[i].x = (gesture_buf[i].x - x_min) * GESTURE_SIZE_REFE / (x_max - x_min);
2765 		gesture_buf[i].y = (gesture_buf[i].y - y_min) * GESTURE_SIZE_REFE / (y_max - y_min);
2766 	}
2767 
2768 	return TRUE;
2769 }
2770 
GestureLength(void)2771 static int GestureLength(void)
2772 {
2773 	int i;
2774 	int len = 0;
2775 
2776 	for (i = 1; i < gesture_num; i++)
2777 		len += GestureDistance(&gesture_buf[i], &gesture_buf[i-1], TRUE);
2778 
2779 	return len;
2780 }
2781 
GestureStandard(void)2782 static void GestureStandard(void)
2783 {
2784 	int i, n, t;
2785 	int len_now = 0;
2786 	int len_his = 0;
2787 	int len_total = GestureLength();
2788 
2789 	gesture_standard[0].all = gesture_buf[0].all & 0x0fffffff;
2790 	gesture_standard[GESTURE_SIZE_NUM - 1].all = gesture_buf[gesture_num - 1].all & 0x0fffffff;
2791 	for (i = 1, n = 0; i < GESTURE_SIZE_NUM-1; i++) {
2792 		while (++n < gesture_num) {
2793 			len_now = GestureDistance(&gesture_buf[n], &gesture_buf[n - 1], TRUE);
2794 			len_his += len_now;
2795 			if (len_his*(GESTURE_SIZE_NUM-1) >= len_total*i)
2796 				break;
2797 		}
2798 		if (n >= gesture_num || len_now == 0)
2799 			break;
2800 		gesture_standard[i].all = 0;
2801 		t = (int)gesture_buf[n-1].x
2802 			+ ((int)gesture_buf[n].x -  (int)gesture_buf[n-1].x)
2803 			* ((int)len_total*i/(GESTURE_SIZE_NUM-1) - (int)len_his + (int)len_now)
2804 			/ (int)len_now
2805 			;
2806 		if (t < 0)
2807 			t = 0;
2808 		gesture_standard[i].x = t;
2809 		t = (int)gesture_buf[n-1].y
2810 			+ ((int)gesture_buf[n].y -  (int)gesture_buf[n-1].y)
2811 			* ((int)len_total*i/(GESTURE_SIZE_NUM-1) - (int)len_his + (int)len_now)
2812 			/ (int)len_now
2813 			;
2814 		if (t < 0)
2815 			t = 0;
2816 		gesture_standard[i].y = t;
2817 		n--;
2818 		len_his -= len_now;
2819 	}
2820 }
2821 
GestureModel(const GESTURE_MODEL_TYPE * model,int len,int threshold,int * out)2822 static int GestureModel(const GESTURE_MODEL_TYPE *model, int len, int threshold, int *out)
2823 {
2824 	int offset[] = {-2, -1, 0, 1, 2};
2825 	int ret_offset;
2826 	int i, j, k, n;
2827 	int min, min_n;
2828 	GESTURE_POINT_TYPE model_coor;
2829 
2830 	if (model == NULL || threshold <= 0 || len <= 0) {
2831 		*out = 0x7fffffff;
2832 		return 0x7fffffff;
2833 	}
2834 	min = 0x7fffffff;
2835 	min_n = 0;
2836 	for (j = 0; j < len; j++) {
2837 		for (k = 0; k < ARRAY_SIZE(offset); k++) {
2838 			n = 0;
2839 			ret_offset = 0;
2840 			for (i = 0; i < GESTURE_SIZE_NUM; i++) {
2841 				if (i + offset[k] < 0 || i+offset[k] >= GESTURE_SIZE_NUM)
2842 					continue;
2843 				if ((i&1) == 0)
2844 					model_coor.all = model[j].coor[i / 2] & 0x00ff00ff;
2845 				else
2846 					model_coor.all = (model[j].coor[i / 2] >> 8) & 0x00ff00ff;
2847 				ret_offset += GestureDistance(&gesture_standard[i+offset[k]], &model_coor, FALSE);
2848 				n++;
2849 			}
2850 			if (n == 0)
2851 				continue;
2852 			ret_offset = ret_offset / n * model[j].coe / 0x10;
2853 			if (ret_offset < min) {
2854 				min_n = j;
2855 				min = ret_offset;
2856 			}
2857 		}
2858 	}
2859 	if (min < threshold)
2860 		*out = model[min_n].out;
2861 	else
2862 		*out = 0x7fffffff;
2863 
2864 	return min;
2865 }
2866 
ChangeXY(void)2867 static void ChangeXY(void)
2868 {
2869 	int i;
2870 
2871 	for (i = 0; i < gesture_num && i < GESTURE_BUF_SIZE; i++)
2872 		gesture_buf[i].all = ((gesture_buf[i].all & 0xfff) << 16)
2873 					+ ((gesture_buf[i].all >> 16) & 0xffff);
2874 }
2875 
GestureSet(unsigned int conf[])2876 static void GestureSet(unsigned int conf[])
2877 {
2878 	if (conf == NULL)
2879 		return;
2880 	if (conf[0] >= 0 && conf[0] <= 64)
2881 		gesture_dis_min = conf[0];
2882 	else
2883 		gesture_dis_min = 0;
2884 	if (conf[1] != 0)
2885 		gesture_threshold[0] = conf[1];
2886 	else
2887 		gesture_threshold[0] = 0xfff;
2888 	gesture_threshold[1] = conf[2];
2889 	x_scale = (conf[3] == 0) ? 4 : conf[3];
2890 	y_scale = (conf[4] == 0) ? 4 : conf[4];
2891 	if (conf[5] == 0) {
2892 		double_down = 2;
2893 		double_up = 30;
2894 	} else {
2895 		double_down = conf[5] & 0xffff;
2896 		double_up = conf[5] >> 16;
2897 	}
2898 	multi_set = conf[6];
2899 	gesture_flag = conf[7];
2900 }
2901 
GestureOrientation(int ori)2902 static void GestureOrientation(int ori)
2903 {
2904 	int i, t;
2905 
2906 	if (ori == 0)
2907 		return;
2908 	if (ori == 2) {
2909 		for (i = 0; i < GESTURE_SIZE_NUM; i++) {
2910 			t = GESTURE_SIZE_REFE - gesture_standard[i].x;
2911 			gesture_standard[i].x = gesture_standard[i].y;
2912 			gesture_standard[i].y = t;
2913 		}
2914 	} else if (ori == 1 || ori == 3) {
2915 		for (i = 0; i < GESTURE_SIZE_NUM; i++) {
2916 			gesture_standard[i].x = GESTURE_SIZE_REFE - gesture_standard[i].x;
2917 			gesture_standard[i].y = GESTURE_SIZE_REFE - gesture_standard[i].y;
2918 		}
2919 	} else if (ori == 4) {
2920 		for (i = 0; i < GESTURE_SIZE_NUM; i++) {
2921 			t = GESTURE_SIZE_REFE - gesture_standard[i].y;
2922 			gesture_standard[i].y = gesture_standard[i].x;
2923 			gesture_standard[i].x = t;
2924 		}
2925 	}
2926 }
2927 
GestureDeal(void)2928 static int GestureDeal(void)
2929 {
2930 	int i, j;
2931 	int gesture_out[2];
2932 	int gesture_val[2];
2933 	int gl[4];
2934 	int gv[4];
2935 
2936 	while (1) {
2937 		if (gesture_last)
2938 			break;
2939 		gesture_last = gesture_multi;
2940 		if (gesture_last)
2941 			break;
2942 		gesture_last = double_click;
2943 		if (gesture_last)
2944 			break;
2945 		if (gesture_deal & GESTURE_XY) {
2946 			gesture_deal &= ~GESTURE_XY;
2947 			ChangeXY();
2948 		}
2949 		if ((gesture_deal & GESTURE_DEAL) == 0)
2950 			return FALSE;
2951 		gesture_deal &= ~GESTURE_DEAL;
2952 		gesture_last = GestureLRUD();
2953 		if (gesture_last)
2954 			break;
2955 		if (GestureStretch() == FALSE)
2956 			break;
2957 		GestureStandard();
2958 		for (j = 0; j < 4; j++) {
2959 			gv[j] = 0x7fffffff;
2960 			GestureOrientation(j);
2961 			if (((gesture_flag & 0xf0) == 0 && j == 0)
2962 				|| ((gesture_flag & 0xf0) != 0 && (gesture_flag & (0x10 << j)) != 0)) {
2963 				gesture_val[0] =  GestureModel(model_default, sizeof(model_default)
2964 						/ sizeof(model_default[0]), gesture_threshold[0],
2965 						&gesture_out[0]);
2966 				gesture_val[1] =  GestureModel(model_extern, model_extern_len,
2967 						gesture_threshold[1], &gesture_out[1]);
2968 				for (i = 0; i < 2; i++) {
2969 					if (gesture_val[i] <= gv[j]) {
2970 						gv[j] = gesture_val[i];
2971 						gl[j] = gesture_out[i];
2972 					}
2973 				}
2974 			}
2975 		}
2976 		gesture_value = 0x7fffffff;
2977 		for (j = 0; j < 4; j++) {
2978 			if (gl[j] >= 'A' && gl[j] <= 'Z' && gv[j] < gesture_value) {
2979 				gesture_value = gv[j];
2980 				gesture_last = gl[j];
2981 			}
2982 		}
2983 		if (gesture_value < 0x7fffffff)
2984 			break;
2985 		for (j = 0; j < 4; j++) {
2986 			if (gv[j] < gesture_value) {
2987 				gesture_value = gv[j];
2988 				gesture_last = gl[j];
2989 			}
2990 		}
2991 		break;
2992 	}
2993 	GestureInit();
2994 	i = gesture_last;
2995 	gesture_last = 0;
2996 
2997 	return i;
2998 }
2999 
gsl_GestureExtern(const GESTURE_MODEL_TYPE * model,int len)3000 void gsl_GestureExtern(const GESTURE_MODEL_TYPE *model, int len)
3001 {
3002 	model_extern = model;
3003 	model_extern_len = len;
3004 }
3005 
GestureLRUD(void)3006 static int GestureLRUD(void)
3007 {
3008 	int x1 = 0, y1 = 0, x2 = 0, y2 = 0, i = 0;
3009 	int flag3 = 0;
3010 	int middle_x;
3011 	int middle_y;
3012 	int min_scale = 5;
3013 
3014 	if (gesture_deal & GESTURE_XY) {
3015 		gesture_deal &= ~GESTURE_XY;
3016 		ChangeXY();
3017 	}
3018 	if ((gesture_deal & GESTURE_LRUD) == 0)
3019 		return FALSE;
3020 	gesture_deal &= ~GESTURE_LRUD;
3021 /*	int screen_x_max = 0, screen_y_max = 0;*/
3022 	x1 = gesture_buf[0].x;
3023 	y1 = gesture_buf[0].y;
3024 	x2 = gesture_buf[gesture_num - 1].x;
3025 	y2 = gesture_buf[gesture_num - 1].y;
3026 	middle_x  = (x1 + x2) / 2;
3027 	middle_y = (y1 + y2) / 2;
3028 	for (i = 1; i < gesture_num; i++) {
3029 		if (abs(gesture_buf[i].x-middle_x) < (int)sen_num_nokey * 64 / x_scale)
3030 			flag3 |= 0x1;
3031 		else
3032 			flag3 |= 0x2;
3033 		if (abs(gesture_buf[i].y-middle_y) < (int)drv_num_nokey*64/y_scale)
3034 			flag3 |= (0x1 << 4);
3035 		else
3036 			flag3 |= (0x2 << 4);
3037 		if ((int)gesture_buf[i].x-(int)gesture_buf[i-1].x > min_scale)
3038 			flag3 |= (0x1 << 8);
3039 		else if ((int)gesture_buf[i].x-(int)gesture_buf[i-1].x < -min_scale)
3040 			flag3 |= (0x2 << 8);
3041 		if ((int)gesture_buf[i].y-(int)gesture_buf[i-1].y > min_scale)
3042 			flag3 |= (0x1 << 12);
3043 		else if ((int)gesture_buf[i].y-(int)gesture_buf[i-1].y < -min_scale)
3044 			flag3 |= (0x2 << 12);
3045 	}
3046 	if (1) {
3047 		if ((flag3 == 0x2031 || flag3 == 0x2131
3048 					|| flag3 == 0x2231 || flag3 == 0x2331))
3049 			return 0xa1fc;
3050 		else if ((flag3 == 0x1031 || flag3 == 0x1131
3051 					|| flag3 == 0x1231 || flag3 == 0x1331))
3052 			return 0xa1fd;
3053 		else if ((flag3 == 0x213 || flag3 == 0x1213
3054 					|| flag3 == 0x2213 || flag3 == 0x2213))
3055 			return 0xa1fb;
3056 		else if ((flag3 == 0x113 || flag3 == 0x1113
3057 					|| flag3 == 0x2113 || flag3 == 0x3113))
3058 			return 0xa1fa;
3059 	}
3060 
3061 	return FALSE;
3062 }
3063 
GestureMulti(unsigned int * data_in)3064 static int GestureMulti(unsigned int *data_in)
3065 {
3066 #ifndef POINT_MAX
3067 #define	POINT_MAX		10
3068 #endif
3069 	GESTURE_POINT_TYPE *data = (GESTURE_POINT_TYPE *)data_in;
3070 	GESTURE_POINT_TYPE centre;
3071 	int n = 0;
3072 	int sum_x = 0, sum_y = 0, s = 0;
3073 	int i;
3074 	int msc, mse, msi; /*multi_set_count, effective, invalid*/
3075 
3076 	if (multi_set == 0) {
3077 		msc = 5;
3078 		mse = 8;
3079 		msi = 16;
3080 	} else {
3081 		msc = (multi_set >> 0) & 0xff;
3082 		mse = (multi_set >> 8) & 0xff;
3083 		msi = (multi_set >> 16) & 0xff;
3084 	}
3085 	for (i = 0; i < POINT_MAX; i++) {
3086 		if (data[i].all == 0)
3087 			continue;
3088 		n++;
3089 		sum_x += data[i].x;
3090 		sum_y += data[i].y;
3091 	}
3092 	if (n == 0) {
3093 		s = FALSE;
3094 		if (multi_x[5-1].count >= 0 && multi_x[4-1].count >= 0 &&
3095 				multi_x[5-1].count + multi_x[4-1].count >= msc)
3096 			s |= 0xbacf;
3097 		if (multi_o[5-1].count >= 0 && multi_o[4-1].count >= 0 &&
3098 				multi_o[5-1].count + multi_o[4-1].count >= msc)
3099 			s |= 0xb7d6;
3100 		if (s != 0xbacf && s != 0xb7d6)
3101 			s = FALSE;
3102 		for (i = 0; i < 5; i++) {
3103 			multi_x[i].count = 0;
3104 			multi_o[i].count = 0;
3105 		}
3106 		return s;
3107 	}
3108 	if (n == 1 || n > 5)
3109 		return FALSE;
3110 	sum_x /= n;
3111 	sum_y /= n;
3112 	centre.all = 0;
3113 	centre.x = sum_x;
3114 	centre.y = sum_y;
3115 	s = 0;
3116 	for (i = 0; i < POINT_MAX; i++) {
3117 		if (data[i].all == 0)
3118 			continue;
3119 		s += GestureDistance(&data[i], &centre, TRUE);
3120 	}
3121 	s /= n;
3122 	n--;
3123 	while (1) {
3124 		if (multi_x[n].count == 0) {
3125 			multi_x[n].count++;
3126 			multi_x[n].dis = s;
3127 			break;
3128 		}
3129 		if (multi_x[n].count < 0)
3130 			break;
3131 		if (s > multi_x[n].dis + msi) {
3132 			multi_x[n].count = -1;
3133 			break;
3134 		}
3135 		if (s + mse < multi_x[n].dis) {
3136 			multi_x[n].count++;
3137 			multi_x[n].dis = s;
3138 			break;
3139 		}
3140 		break;
3141 	}
3142 	while (1) {
3143 		if (multi_o[n].count == 0) {
3144 			multi_o[n].count++;
3145 			multi_o[n].dis = s;
3146 			break;
3147 		}
3148 		if (multi_o[n].count < 0)
3149 			break;
3150 		if (s + msi < multi_o[n].dis) {
3151 			multi_o[n].count = -1;
3152 			break;
3153 		}
3154 		if (s > multi_o[n].dis + mse) {
3155 			multi_o[n].count++;
3156 			multi_o[n].dis = s;
3157 			break;
3158 		}
3159 		break;
3160 	}
3161 
3162 	return FALSE;
3163 }
3164 
gsl_GestureBuffer(unsigned int ** buf)3165 unsigned int gsl_GestureBuffer(unsigned int **buf)
3166 {
3167 	int i;
3168 
3169 	if (gesture_num_last >= GESTURE_BUF_SIZE)
3170 		gesture_num_last = GESTURE_BUF_SIZE - 1;
3171 	for (i = 0; i < gesture_num_last; i++)
3172 		gesture_buf[i].all = ScreenResolution((gsl_POINT_TYPE *)(&gesture_buf[i].all));
3173 
3174 	*buf = &(gesture_buf[0].all);
3175 
3176 	return gesture_num_last;
3177 }
3178 #endif
3179