-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathrandom.c
More file actions
1670 lines (1407 loc) · 48.2 KB
/
Copy pathrandom.c
File metadata and controls
1670 lines (1407 loc) · 48.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
///////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT NOTICE
//
// The following open source license statement does not apply to any
// entity in the Exception List published by FMSoft.
//
// For more information, please visit:
//
// https://www.fmsoft.cn/exception-list
//
//////////////////////////////////////////////////////////////////////////////
/*
* This file is part of MiniGUI, a mature cross-platform windowing
* and Graphics User Interface (GUI) support system for embedded systems
* and smart IoT devices.
*
* Copyright (C) 2002~2019, Beijing FMSoft Technologies Co., Ltd.
* Copyright (C) 1998~2002, WEI Yongming
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Or,
*
* As this program is a library, any link to this program must follow
* GNU General Public License version 3 (GPLv3). If you cannot accept
* GPLv3, you need to be licensed from FMSoft.
*
* If you have got a commercial license of this program, please use it
* under the terms and conditions of the commercial license.
*
* For more information about the commercial license, please refer to
* <http://www.minigui.com/blog/minigui-licensing-policy/>.
*/
/*
** random.c: the random IAL Engine.
**
** Created by WEI Yongming, 2005/06/08
**
** Enhanced by WEI Yongming to support extra input events in June, 2019.
**
** The MiniGUI runtime configuration key `random.logfile` specifies
** the log file which will store the input events generated by this engine.
** If MiniGUI failed to open the log file, the log feature will be disabled.
**
** The MiniGUI runtime configuration key `random.eventtypes` specifies
** the input event types which will be generated by this IAL engine,
** in the following pattern:
**
** <event-type>[-<event-type>]*
**
** The <event-type> can be one of the following values:
**
** - mouse: mouse.
** - keyboard: keyboard.
** - button: buttons.
** - single_touch: touch pen or single touch panel.
** - multi_touch: multiple touch panel.
** - gesture: gesture.
** - tablet_tool: tablet tool.
** - tablet_pad: tablet pad.
** - switch: switch.
**
** The MiniGUI ETC key `random.minkeycode` specifies the minimal key code
** which can be generated by the engine if `keyboard` is included.
**
** The MiniGUI ETC key `random.maxkeycode` specifies the maximal key code
** which can be generated by the engine if `keyboard` is included.
**
** The MiniGUI ETC key `random.minbtncode` specifies the minimal button code
** which can be generated by the engine if `button` is included.
**
** The MiniGUI ETC key `random.maxbtncode` specifies the maximal key code
** which can be generated by the engine if `button` is included.
**
** For invalid `random.eventtyps`, use `mouse` as default.
**
** For invalid `random.minkeycode`, and/or `random.maxkeycode key values, use
** `SCANCODE_ESCAPE`, and `SCANCODE_MICMUTE` respectively.
**
** For invalid `random.minbtncode`, and/or `random.maxbtncode key values, use
** `0x100` (BTN_MISC defined by Linux kernel), and `0x2ff` (KEY_MAX defined by
** Linux kernel) respectively.
**
** This engine maintains a state machine for each input event type, and
** generates a reasonable event sequence for each type. If and only if
** an event sequence finished or cancelled, the engine switch to another
** event type randomly.
**
** Note that currently, the following event types are not implemented:
**
** - multi_touch
** - tablet_tool
** - tablet_pad
** - switch
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#define _DEBUG
#include "common.h"
#ifdef _MGIAL_RANDOM
#include <time.h>
#include <sys/time.h>
#include "minigui.h"
#include "misc.h"
#include "ial.h"
#include "random.h"
#define BTN_NONE 0
#ifdef __LINUX__
#include <linux/input-event-codes.h>
#else
#define BTN_LEFT 0x110
#define BTN_RIGHT 0x111
#define BTN_MIDDLE 0x112
#endif
#define MIN_SCANCODE SCANCODE_ESCAPE
#define MAX_SCANCODE SCANCODE_MICMUTE
#define MIN_BTNCODE 0x100
#define MAX_BTNCODE 0x2ff
#define MAX_PRESSED_BTNS 10
#define MAX_ABS_X 2048
#define MAX_ABS_Y 2048
enum _random_event_type {
RANDOM_EVENT_NONE,
RANDOM_EVENT_WAIT,
RANDOM_EVENT_MOUSE_MOTION,
RANDOM_EVENT_MOUSE_BUTTON,
RANDOM_EVENT_MOUSE_WHEEL,
RANDOM_EVENT_KEYBOARD_KEY,
RANDOM_EVENT_BUTTON,
RANDOM_EVENT_STOUCH_DOWN,
RANDOM_EVENT_STOUCH_UP,
RANDOM_EVENT_STOUCH_MOTION,
RANDOM_EVENT_MTOUCH_DOWN,
RANDOM_EVENT_MTOUCH_UP,
RANDOM_EVENT_MTOUCH_MOTION,
RANDOM_EVENT_MTOUCH_CANCEL,
RANDOM_EVENT_MTOUCH_FRAME,
RANDOM_EVENT_GESTURE_SWIPE_BEGIN,
RANDOM_EVENT_GESTURE_SWIPE_UPDATE,
RANDOM_EVENT_GESTURE_SWIPE_END,
RANDOM_EVENT_GESTURE_PINCH_BEGIN,
RANDOM_EVENT_GESTURE_PINCH_UPDATE,
RANDOM_EVENT_GESTURE_PINCH_END,
};
struct _random_event {
enum _random_event_type type;
};
struct _event_state_machine;
#define NR_EVENT_TYPES 10
struct _random_input_context {
FILE* log_fp;
int min_x, max_x, min_y, max_y;
int mouse_x, mouse_y, mouse_button;
int min_keycode, max_keycode;
int min_btncode, max_btncode;
int last_keycode;
char* kbd_state;
struct _event_state_machine *esm [NR_EVENT_TYPES];
int nr_machines;
int curr_machine;
};
typedef void* (*op_start_event_machine)(const struct _random_input_context* ctxt);
typedef void (*op_stop_event_machine)(void* machine);
typedef void (*op_resume_event_machine)(void* machine);
typedef const struct _random_event* (*op_generate_event)(void* machine);
enum _button_state {
BUTTON_STATE_RELEASED,
BUTTON_STATE_PRESSED,
};
enum _key_state {
KEY_STATE_RELEASED,
KEY_STATE_PRESSED,
};
/* implementation of state machine for mouse */
struct _mouse_event {
struct _random_event event;
uint32_t button;
enum _button_state state;
int dx, dy;
int sv; // scroll value
int dsv; // discrete scroll value
};
enum _mouse_op_type {
MOUSE_OP_IDLE = 0,
MOUSE_OP_MOVE,
MOUSE_OP_CLICK,
MOUSE_OP_PRESSED_MOVE,
MOUSE_OP_WHEEL,
MOUSE_OP_COUNT,
};
struct _mouse_event_machine {
struct _mouse_event event;
enum _mouse_op_type op_type;
int op_sec;
time_t op_start;
uint32_t btn_sel;
uint32_t btn_down;
};
static void* start_event_machine_mouse(const struct _random_input_context* ctxt)
{
struct _mouse_event_machine* machine;
machine = calloc(1, sizeof (struct _mouse_event_machine));
return machine;
}
static void stop_event_machine_mouse(void* machine)
{
if (machine)
free(machine);
}
static void resume_event_machine_mouse(void* machine)
{
struct _mouse_event_machine* my_machine =
(struct _mouse_event_machine*)machine;
my_machine->op_type = MOUSE_OP_IDLE;
my_machine->op_sec = random() % 5;
my_machine->op_start = time(NULL);
}
static const struct _random_event* generate_event_mouse(void* machine)
{
struct _mouse_event_machine* my_machine =
(struct _mouse_event_machine*)machine;
time_t now = time(NULL);
memset(&my_machine->event, 0, sizeof(my_machine->event));
if ((now - my_machine->op_start) > my_machine->op_sec) {
my_machine->op_type = now % MOUSE_OP_COUNT;
my_machine->op_sec = random() % 5 + 1;
my_machine->op_start = time(NULL);
switch (now % 5) {
case 0:
my_machine->btn_sel = BTN_RIGHT;
break;
case 1:
my_machine->btn_sel = BTN_MIDDLE;
break;
default: /* 60% possibility to choose the left button. */
my_machine->btn_sel = BTN_LEFT;
break;
}
my_machine->event.event.type = RANDOM_EVENT_NONE;
goto ret;
}
/* valid operation */
if ((GetTickCount() % 5)) {
// 80% possibility to wait for a new event.
__mg_os_time_delay(11);
my_machine->event.event.type = RANDOM_EVENT_WAIT;
goto ret;
}
switch (my_machine->op_type) {
case MOUSE_OP_IDLE:
my_machine->event.event.type = RANDOM_EVENT_WAIT;
break;
case MOUSE_OP_MOVE:
my_machine->event.event.type = RANDOM_EVENT_MOUSE_MOTION;
my_machine->event.dx = (random() % 100) - 50;
my_machine->event.dy = (random() % 100) - 50;
break;
case MOUSE_OP_CLICK:
if (my_machine->btn_down == BTN_NONE) {
my_machine->btn_down = my_machine->btn_sel;
my_machine->event.event.type = RANDOM_EVENT_MOUSE_BUTTON;
my_machine->event.button = my_machine->btn_down;
my_machine->event.state = BUTTON_STATE_PRESSED;
}
else {
my_machine->event.event.type = RANDOM_EVENT_MOUSE_BUTTON;
my_machine->event.button = my_machine->btn_down;
my_machine->event.state = BUTTON_STATE_RELEASED;
my_machine->btn_down = BTN_NONE;
}
break;
case MOUSE_OP_PRESSED_MOVE:
if (my_machine->btn_down == BTN_NONE) {
my_machine->btn_down = my_machine->btn_sel;
my_machine->event.event.type = RANDOM_EVENT_MOUSE_BUTTON;
my_machine->event.button = my_machine->btn_down;
my_machine->event.state = BUTTON_STATE_PRESSED;
}
else if ((GetTickCount() % 5) == 0) {
my_machine->event.event.type = RANDOM_EVENT_MOUSE_BUTTON;
my_machine->event.button = my_machine->btn_down;
my_machine->event.state = BUTTON_STATE_RELEASED;
my_machine->btn_down = BTN_NONE;
}
else {
my_machine->event.event.type = RANDOM_EVENT_MOUSE_MOTION;
my_machine->event.dx = (random() % 100) - 50;
my_machine->event.dy = (random() % 100) - 50;
}
break;
case MOUSE_OP_WHEEL:
my_machine->event.event.type = RANDOM_EVENT_MOUSE_WHEEL;
my_machine->event.dsv = (random() % 10) - 5;
my_machine->event.sv = 15 * my_machine->event.dsv; // 15 degrees per tick
break;
default:
assert(0);
break;
}
ret:
return &my_machine->event.event;
}
static inline const
struct _mouse_event* get_mouse_event(const struct _random_event* event)
{
if (event->type != RANDOM_EVENT_MOUSE_MOTION &&
event->type != RANDOM_EVENT_MOUSE_BUTTON &&
event->type != RANDOM_EVENT_MOUSE_WHEEL)
return NULL;
return (struct _mouse_event*)event;
}
/* implementation of state machine for keyboard */
struct _keyboard_event {
struct _random_event event;
int keycode;
enum _key_state state;
};
enum _keyboard_op_type {
KEYBOARD_OP_IDLE = 0,
KEYBOARD_OP_NORMAL_KEY,
KEYBOARD_OP_SHIFT_KEYS,
KEYBOARD_OP_COUNT,
};
struct _keyboard_event_machine {
struct _keyboard_event event;
enum _keyboard_op_type op_type;
int op_sec;
time_t op_start;
int min_keycode, max_keycode;
uint32_t pressed_shift_keys;
int pressed_normal_key;
};
static struct _keyboard_shift_key {
int keycode;
uint32_t flag;
} keyboard_shift_keys[] = {
{ SCANCODE_CAPSLOCK, KS_CAPSLOCK },
{ SCANCODE_NUMLOCK, KS_NUMLOCK },
{ SCANCODE_SCROLLLOCK, KS_SCROLLLOCK },
{ SCANCODE_LEFTCTRL, KS_LEFTCTRL },
{ SCANCODE_RIGHTCTRL, KS_RIGHTCTRL },
{ SCANCODE_LEFTALT, KS_LEFTALT },
{ SCANCODE_RIGHTALT, KS_RIGHTALT },
{ SCANCODE_LEFTSHIFT, KS_LEFTSHIFT },
{ SCANCODE_RIGHTSHIFT, KS_RIGHTSHIFT },
{ SCANCODE_LEFTMETA, KS_LEFTMETA },
{ SCANCODE_RIGHTMETA, KS_RIGHTMETA },
};
static void* start_event_machine_keyboard(const struct _random_input_context* ctxt)
{
struct _keyboard_event_machine* machine;
machine = calloc(1, sizeof (struct _keyboard_event_machine));
machine->min_keycode = ctxt->min_keycode;
machine->max_keycode = ctxt->max_keycode;
return machine;
}
static void stop_event_machine_keyboard(void* machine)
{
if (machine)
free(machine);
}
static void resume_event_machine_keyboard(void* machine)
{
struct _keyboard_event_machine* my_machine =
(struct _keyboard_event_machine*)machine;
my_machine->op_type = KEYBOARD_OP_IDLE;
my_machine->op_sec = random() % 5;
my_machine->op_start = time(NULL);
}
static const struct _random_event* generate_event_keyboard(void* machine)
{
struct _keyboard_event_machine* my_machine =
(struct _keyboard_event_machine*)machine;
time_t now = time(NULL);
memset(&my_machine->event, 0, sizeof(my_machine->event));
if ((now - my_machine->op_start) > my_machine->op_sec) {
my_machine->op_type = now % KEYBOARD_OP_COUNT;
my_machine->op_sec = random() % 5;
my_machine->op_start = time(NULL);
my_machine->event.event.type = RANDOM_EVENT_NONE;
goto ret;
}
/* valid operation */
if ((GetTickCount() % 5)) {
// 80% possibility to wait for a new event.
__mg_os_time_delay(11);
my_machine->event.event.type = RANDOM_EVENT_WAIT;
goto ret;
}
switch (my_machine->op_type) {
case KEYBOARD_OP_IDLE:
my_machine->event.event.type = RANDOM_EVENT_WAIT;
break;
case KEYBOARD_OP_NORMAL_KEY:
if (my_machine->pressed_normal_key == SCANCODE_RESERVED) {
my_machine->pressed_normal_key =
(random() %
(my_machine->max_keycode - my_machine->min_keycode + 1)) +
my_machine->min_keycode;
my_machine->event.event.type = RANDOM_EVENT_KEYBOARD_KEY;
my_machine->event.keycode = my_machine->pressed_normal_key;
my_machine->event.state = KEY_STATE_PRESSED;
}
else {
my_machine->event.event.type = RANDOM_EVENT_KEYBOARD_KEY;
my_machine->event.keycode = my_machine->pressed_normal_key;
my_machine->event.state = KEY_STATE_RELEASED;
my_machine->pressed_normal_key = SCANCODE_RESERVED;
}
break;
case KEYBOARD_OP_SHIFT_KEYS: {
int i = random() % (TABLESIZE(keyboard_shift_keys));
if (keyboard_shift_keys[i].flag & my_machine->pressed_shift_keys) {
my_machine->event.event.type = RANDOM_EVENT_KEYBOARD_KEY;
my_machine->event.keycode = keyboard_shift_keys[i].keycode;
my_machine->event.state = KEY_STATE_RELEASED;
my_machine->pressed_shift_keys &= ~keyboard_shift_keys[i].flag;
}
else {
my_machine->event.event.type = RANDOM_EVENT_KEYBOARD_KEY;
my_machine->event.keycode = keyboard_shift_keys[i].keycode;
my_machine->event.state = KEY_STATE_PRESSED;
my_machine->pressed_shift_keys |= keyboard_shift_keys[i].flag;
}
break;
}
default:
assert(0);
break;
}
ret:
return &my_machine->event.event;
}
static inline const
struct _keyboard_event* get_keyboard_event(const struct _random_event* event)
{
if (event->type != RANDOM_EVENT_KEYBOARD_KEY)
return NULL;
return (struct _keyboard_event*)event;
}
/* implementation of state machine for button */
struct _button_event {
struct _random_event event;
int btncode;
enum _button_state state;
int nr_down;
};
enum _button_op_type {
BUTTON_OP_IDLE = 0,
BUTTON_OP_NORMAL,
BUTTON_OP_COUNT,
};
struct _button_event_machine {
struct _button_event event;
enum _button_op_type op_type;
int op_sec;
time_t op_start;
int min_btncode, max_btncode;
int last_btn;
int pressed_btns[MAX_PRESSED_BTNS];
};
static inline
int button_pressed_count(struct _button_event_machine* machine)
{
int i, n = 0;
for (i = 0; i < MAX_PRESSED_BTNS; i++) {
if (machine->pressed_btns[i]) {
n++;
}
}
return n;
}
static inline
int button_is_pressed(struct _button_event_machine* machine, int btn)
{
int i;
for (i = 0; i < MAX_PRESSED_BTNS; i++) {
if (btn == machine->pressed_btns[i]) {
return i;
}
}
return -1;
}
static inline
int button_clear(struct _button_event_machine* machine, int btn)
{
int i;
for (i = 0; i < MAX_PRESSED_BTNS; i++) {
if (btn == machine->pressed_btns[i]) {
machine->pressed_btns[i] = BTN_NONE;
return i;
}
}
return -1;
}
static inline
int button_set(struct _button_event_machine* machine, int btn)
{
int i;
for (i = 0; i < MAX_PRESSED_BTNS; i++) {
if (machine->pressed_btns[i] == BTN_NONE) {
machine->pressed_btns[i] = btn;
return i;
}
}
return -1;
}
static void* start_event_machine_button(const struct _random_input_context* ctxt)
{
struct _button_event_machine* machine;
machine = calloc(1, sizeof (struct _button_event_machine));
machine->min_btncode = ctxt->min_btncode;
machine->max_btncode = ctxt->max_btncode;
return machine;
}
static void stop_event_machine_button(void* machine)
{
if (machine)
free(machine);
}
static void resume_event_machine_button(void* machine)
{
struct _button_event_machine* my_machine =
(struct _button_event_machine*)machine;
my_machine->op_type = BUTTON_OP_IDLE;
my_machine->op_sec = random() % 5;
my_machine->op_start = time(NULL);
}
static const struct _random_event* generate_event_button(void* machine)
{
struct _button_event_machine* my_machine =
(struct _button_event_machine*)machine;
time_t now = time(NULL);
memset(&my_machine->event, 0, sizeof(my_machine->event));
if ((now - my_machine->op_start) > my_machine->op_sec) {
my_machine->op_type = now % BUTTON_OP_COUNT;
my_machine->op_sec = random() % 5;
my_machine->op_start = time(NULL);
my_machine->event.event.type = RANDOM_EVENT_NONE;
goto ret;
}
/* valid operation */
if ((GetTickCount() % 5)) {
// 80% possibility to wait for a new event.
__mg_os_time_delay(11);
my_machine->event.event.type = RANDOM_EVENT_WAIT;
goto ret;
}
switch (my_machine->op_type) {
case BUTTON_OP_IDLE:
my_machine->event.event.type = RANDOM_EVENT_WAIT;
break;
case BUTTON_OP_NORMAL: {
if (my_machine->last_btn != BTN_NONE && (GetTickCount() % 5)) {
// 80% possibility to release last button.
int idx = button_clear(my_machine, my_machine->last_btn);
assert (idx >= 0);
my_machine->event.event.type = RANDOM_EVENT_BUTTON;
my_machine->event.btncode = my_machine->last_btn;
my_machine->event.state = BUTTON_STATE_RELEASED;
my_machine->last_btn = BTN_NONE;
}
else {
int btn =
(random() %
(my_machine->max_btncode - my_machine->min_btncode + 1)) +
my_machine->min_btncode;
int idx = button_is_pressed(my_machine, btn);
if (idx < 0) {
idx = button_set(my_machine, btn);
if (idx < 0) {
// full pressed buttons; release a random button
idx = random() % MAX_PRESSED_BTNS;
my_machine->event.event.type = RANDOM_EVENT_BUTTON;
my_machine->event.btncode = my_machine->pressed_btns[idx];
my_machine->event.state = BUTTON_STATE_RELEASED;
my_machine->last_btn = BTN_NONE;
my_machine->pressed_btns[idx] = BTN_NONE;
}
else {
my_machine->event.event.type = RANDOM_EVENT_BUTTON;
my_machine->event.btncode = btn;
my_machine->event.state = BUTTON_STATE_PRESSED;
my_machine->last_btn = btn;
}
}
else {
my_machine->event.event.type = RANDOM_EVENT_BUTTON;
my_machine->event.btncode = my_machine->pressed_btns[idx];
my_machine->event.state = BUTTON_STATE_RELEASED;
my_machine->last_btn = BTN_NONE;
my_machine->pressed_btns[idx] = BTN_NONE;
}
}
my_machine->event.nr_down = button_pressed_count(my_machine);
break;
}
default:
assert(0);
break;
}
ret:
return &my_machine->event.event;
}
static inline const
struct _button_event* get_button_event(const struct _random_event* event)
{
if (event->type != RANDOM_EVENT_BUTTON)
return NULL;
return (struct _button_event*)event;
}
/* implementation of state machine for stouch */
struct _stouch_event {
struct _random_event event;
enum _button_state state;
int x, y;
};
enum _stouch_op_type {
STOUCH_OP_IDLE = 0,
STOUCH_OP_DOWN,
STOUCH_OP_DOWN_MOVE,
STOUCH_OP_COUNT,
};
struct _stouch_event_machine {
struct _stouch_event event;
enum _stouch_op_type op_type;
int op_sec;
time_t op_start;
int down;
};
static void* start_event_machine_stouch(const struct _random_input_context* ctxt)
{
struct _stouch_event_machine* machine;
machine = calloc(1, sizeof (struct _stouch_event_machine));
return machine;
}
static void stop_event_machine_stouch(void* machine)
{
if (machine)
free(machine);
}
static void resume_event_machine_stouch(void* machine)
{
struct _stouch_event_machine* my_machine =
(struct _stouch_event_machine*)machine;
my_machine->op_type = STOUCH_OP_IDLE;
my_machine->op_sec = random() % 5;
my_machine->op_start = time(NULL);
}
static const struct _random_event* generate_event_stouch(void* machine)
{
struct _stouch_event_machine* my_machine =
(struct _stouch_event_machine*)machine;
time_t now = time(NULL);
memset(&my_machine->event, 0, sizeof(my_machine->event));
if ((now - my_machine->op_start) > my_machine->op_sec) {
my_machine->op_type = now % STOUCH_OP_COUNT;
my_machine->op_sec = random() % 5 + 1;
my_machine->op_start = time(NULL);
my_machine->event.event.type = RANDOM_EVENT_NONE;
goto ret;
}
/* valid operation */
if ((GetTickCount() % 5)) {
// 80% possibility to wait for a new event.
__mg_os_time_delay(11);
my_machine->event.event.type = RANDOM_EVENT_WAIT;
goto ret;
}
switch (my_machine->op_type) {
case STOUCH_OP_IDLE:
my_machine->event.event.type = RANDOM_EVENT_WAIT;
break;
case STOUCH_OP_DOWN:
if (my_machine->down) {
my_machine->event.event.type = RANDOM_EVENT_STOUCH_DOWN;
my_machine->event.state = BUTTON_STATE_RELEASED;
my_machine->down = 0;
}
else {
my_machine->event.event.type = RANDOM_EVENT_STOUCH_DOWN;
my_machine->event.state = BUTTON_STATE_PRESSED;
my_machine->event.x = random() % MAX_ABS_X;
my_machine->event.y = random() % MAX_ABS_Y;
my_machine->down = 1;
}
break;
case STOUCH_OP_DOWN_MOVE:
if (my_machine->down == 0) {
my_machine->down = 1;
my_machine->event.event.type = RANDOM_EVENT_STOUCH_DOWN;
my_machine->event.state = BUTTON_STATE_PRESSED;
my_machine->event.x = random() % MAX_ABS_X;
my_machine->event.y = random() % MAX_ABS_Y;
}
else if ((GetTickCount() % 5) == 0) {
my_machine->event.event.type = RANDOM_EVENT_STOUCH_DOWN;
my_machine->event.state = BUTTON_STATE_RELEASED;
my_machine->down = 0;
}
else {
my_machine->event.event.type = RANDOM_EVENT_STOUCH_MOTION;
my_machine->event.x += (random() % 10) - 5;
my_machine->event.y += (random() % 10) - 5;
}
break;
default:
assert(0);
break;
}
ret:
return &my_machine->event.event;
}
static inline const
struct _stouch_event* get_stouch_event(const struct _random_event* event)
{
if (event->type != RANDOM_EVENT_STOUCH_MOTION &&
event->type != RANDOM_EVENT_STOUCH_DOWN &&
event->type != RANDOM_EVENT_STOUCH_UP)
return NULL;
return (struct _stouch_event*)event;
}
/* implementation of state machine for gesture */
struct _gesture_event {
struct _random_event event;
int nr_figs;
int dx, dy;
int is_cancelled;
int scale;
int da;
};
enum _gesture_op_type {
GESTURE_OP_IDLE = 0,
GESTURE_OP_SWIPE,
GESTURE_OP_PINCH,
GESTURE_OP_COUNT,
};
struct _gesture_event_machine {
struct _gesture_event event;
enum _gesture_op_type op_type;
int op_sec;
time_t op_start;
int started;
};
static void* start_event_machine_gesture(const struct _random_input_context* ctxt)
{
struct _gesture_event_machine* machine;
machine = calloc(1, sizeof (struct _gesture_event_machine));
return machine;
}
static void stop_event_machine_gesture(void* machine)
{
if (machine)
free(machine);
}
static void resume_event_machine_gesture(void* machine)
{
struct _gesture_event_machine* my_machine =
(struct _gesture_event_machine*)machine;
my_machine->op_type = GESTURE_OP_IDLE;
my_machine->op_sec = random() % 5;
my_machine->op_start = time(NULL);
}
static const struct _random_event* generate_event_gesture(void* machine)
{
struct _gesture_event_machine* my_machine =
(struct _gesture_event_machine*)machine;
time_t now = time(NULL);
memset(&my_machine->event, 0, sizeof(my_machine->event));
if ((now - my_machine->op_start) > my_machine->op_sec &&
my_machine->started == 0) {
my_machine->op_type = now % GESTURE_OP_COUNT;
my_machine->op_sec = random() % 5 + 1;
my_machine->op_start = time(NULL);
my_machine->event.event.type = RANDOM_EVENT_NONE;
memset(&my_machine->event, 0, sizeof(struct _gesture_event));
goto ret;
}
/* valid operation */
if ((GetTickCount() % 2)) {
// 50% possibility to wait for a new event.
__mg_os_time_delay(11);
my_machine->event.event.type = RANDOM_EVENT_WAIT;
goto ret;
}
switch (my_machine->op_type) {
case GESTURE_OP_IDLE:
my_machine->event.event.type = RANDOM_EVENT_WAIT;
break;
case GESTURE_OP_SWIPE:
if (my_machine->started == 0) {
my_machine->event.event.type = RANDOM_EVENT_GESTURE_SWIPE_BEGIN;
my_machine->event.nr_figs = random() % 5 + 1;
my_machine->started = 1;
}
else if (GetTickCount() % 10) {
// 90% possibility to wait update
my_machine->event.event.type = RANDOM_EVENT_GESTURE_SWIPE_UPDATE;
my_machine->event.dx = (random() % 100) - 50;
my_machine->event.dy = (random() % 100) - 50;
}
else {
my_machine->event.event.type = RANDOM_EVENT_GESTURE_SWIPE_END;
if (GetTickCount() % 3)
my_machine->event.is_cancelled = 0;
else
my_machine->event.is_cancelled = 1;
my_machine->started = 0;
}
break;
case GESTURE_OP_PINCH:
if (my_machine->started == 0) {
my_machine->event.event.type = RANDOM_EVENT_GESTURE_PINCH_BEGIN;
my_machine->event.nr_figs = random() % 5 + 1;
my_machine->event.scale = 100;
my_machine->started = 1;
}
else if (GetTickCount() % 10) {
// 90% possibility to wait update
my_machine->event.event.type = RANDOM_EVENT_GESTURE_PINCH_UPDATE;
if (GetTickCount() % 2)
my_machine->event.scale *= (random() % 5) + 1;
else
my_machine->event.scale /= (random() % 5) + 1;
if (my_machine->event.scale == 0)
my_machine->event.scale = 1;
my_machine->event.da = (random() % 10) * 25;