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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
|
//*****************************************************************************
//
// widget.c - Generic widget tree handling code.
//
// Copyright (c) 2008-2014 Texas Instruments Incorporated. All rights reserved.
// Software License Agreement
//
// Texas Instruments (TI) is supplying this software for use solely and
// exclusively on TI's microcontroller products. The software is owned by
// TI and/or its suppliers, and is protected under applicable copyright
// laws. You may not combine this software with "viral" open-source
// software in order to form a larger program.
//
// THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
// NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
// NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
// CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
// DAMAGES, FOR ANY REASON WHATSOEVER.
//
// This is part of revision 2.1.0.12573 of the Tiva Graphics Library.
//
//*****************************************************************************
#include <stdint.h>
#include <stdbool.h>
#include "driverlib/debug.h"
#include "grlib/grlib.h"
#include "grlib/widget.h"
//*****************************************************************************
//
//! \addtogroup widget_api
//! @{
//
//*****************************************************************************
//*****************************************************************************
//
// Flags that indicate how messages from the message queue are processed. They
// can be sent via either a pre-order or post-order search, and can optionally
// be sent to no other widgets once one accepts the message.
//
//*****************************************************************************
#define MQ_FLAG_POST_ORDER 1
#define MQ_FLAG_STOP_ON_SUCCESS 2
//*****************************************************************************
//
// The size of the message queue. In order to make the queue pointer
// arithmetic more efficient, this should be a power of two.
//
//*****************************************************************************
#define QUEUE_SIZE 16
#ifdef DEBUG_MSGQ
//*****************************************************************************
//
// In debug builds, keep track of the number of cases where a message was
// lost due to the queue being full. We count the following occurrences:
//
// 1. All messages discarded due to queue overflow (g_ui32MQOverflow)
// 2. Messages other than WIDGET_MSG_PTR_MOVE discarded due to queue
// overflow (g_ui32MQNonMouseOverflow). In this case, we also remember the
// last message that was discarded (g_ui32MQLastLostMsg).
// 3. The number of calls to WidgetMessageQueueAdd that fail due to the queue
// mutex already being held.
// 4. The number of cases where WidgetMessageQueueAdd reused an unread
// WIDGET_MSG_PTR_MOVE message when a second one arrived before the previous
// one had been processed.
//
//*****************************************************************************
uint32_t g_ui32MQOverflow = 0;
uint32_t g_ui32MQNonMouseOverflow = 0;
uint32_t g_ui32MQLastLostMsg = 0;
uint32_t g_ui32MQMutexClash = 0;
uint32_t g_ui32MQMoveOverwrite = 0;
#endif
//*****************************************************************************
//
// This structure describes the message queue used to hold widget messages.
//
//*****************************************************************************
typedef struct
{
//
// The flags that describe how this message should be processed; this is
// defined by the MQ_FLAG_xxx flags.
//
uint32_t ui32Flags;
//
// The widget (or widget tree) to which the message should be sent.
//
tWidget *psWidget;
//
// The message to be sent.
//
uint32_t ui32Message;
//
// The first parameter to the message.
//
uint32_t ui32Param1;
//
// The second parameter to the message.
//
uint32_t ui32Param2;
}
tWidgetMessageQueue;
//*****************************************************************************
//
// The root of the widget tree. This is the widget used when no parent is
// specified when adding a widget, or when no widget is specified when sending
// a message. The parent and sibling of this widget are always zero. This
// should not be directly referenced by applications; WIDGET_ROOT should be
// used instead.
//
//*****************************************************************************
tWidget g_sRoot =
{
sizeof(tWidget),
0,
0,
0,
0,
{
0,
0,
0,
0,
},
WidgetDefaultMsgProc
};
//*****************************************************************************
//
// The widget that has captured pointer messages. When a pointer down message
// is accepted by a widget, that widget is saved in this variable and all
// subsequent pointer move and pointer up messages are sent directly to this
// widget.
//
//*****************************************************************************
static tWidget *g_psPointerWidget = 0;
//*****************************************************************************
//
// The message queue that holds messages that are waiting to be processed.
//
//*****************************************************************************
static volatile tWidgetMessageQueue g_psMQ[QUEUE_SIZE];
//*****************************************************************************
//
// The offset to the next message to be read from the message queue. The
// message queue is empty when this has the same value as g_ui32MQWrite.
//
//*****************************************************************************
static uint32_t g_ui32MQRead = 0;
//*****************************************************************************
//
// The offset to the next message to be written to the message queue. The
// message queue is full when this value is one less than g_ui32MQRead (modulo
// the queue size).
//
//*****************************************************************************
static volatile uint32_t g_ui32MQWrite = 0;
//*****************************************************************************
//
// The mutex used to protect access to the message queue.
//
//*****************************************************************************
static uint8_t g_ui8MQMutex = 0;
//*****************************************************************************
//
//! Initializes a mutex to the unowned state.
//!
//! \param pi8Mutex is a pointer to mutex that is to be initialized.
//!
//! This function initializes a mutual exclusion semaphore (mutex) to its
//! unowned state in preparation for use with WidgetMutexGet() and
//! WidgetMutexPut(). A mutex is a two state object typically used to
//! serialize access to a shared resource. An application will call
//! WidgetMutexGet() to request ownership of the mutex. If ownership is
//! granted, the caller may safely access the resource then release the mutex
//! using WidgetMutexPut() once it is finished. If ownership is not granted,
//! the caller knows that some other context is currently modifying the shared
//! resource and it must not access the resource at that time.
//!
//! Note that this function must not be called if the mutex passed in
//! \e pi8Mutex is already in use since this will have the effect of releasing
//! the lock even if some caller currently owns it.
//!
//! \return None.
//
//*****************************************************************************
void
WidgetMutexInit(uint8_t *pi8Mutex)
{
//
// Catch NULL pointers in a debug build.
//
ASSERT(pi8Mutex);
//
// Clear the mutex location to set it to the unowned state.
//
*pi8Mutex = 0;
}
//*****************************************************************************
//
//! Attempts to acquire a mutex.
//!
//! \param pi8Mutex is a pointer to mutex that is to be acquired.
//!
//! This function attempts to acquire a mutual exclusion semaphore (mutex) on
//! behalf of the caller. If the mutex is not already held, 0 is returned to
//! indicate that the caller may safely access whichever resource the mutex is
//! protecting. If the mutex is already held, 1 is returned and the caller
//! must not access the shared resource.
//!
//! When access to the shared resource is complete, the mutex owner should call
//! WidgetMutexPut() to release the mutex and relinquish ownership of the
//! shared resource.
//!
//! \return Returns 0 if the mutex is acquired successfully or 1 if it is
//! already held by another caller.
//
//*****************************************************************************
#if defined(ewarm) || defined(DOXYGEN)
uint32_t
WidgetMutexGet(uint8_t *pi8Mutex)
{
//
// Acquire the mutex if possible.
//
__asm(" mov r1, #1\n"
" ldrexb r2, [r0]\n"
" cmp r2, #0\n"
" it eq\n"
" strexb r2, r1, [r0]\n"
" mov r0, r2\n");
//
// "Warning[Pe940]: missing return statement at end of non-void function"
// is suppressed here to avoid putting a "bx lr" in the inline assembly
// above and a superfluous return statement here.
//
#pragma diag_suppress=Pe940
}
#pragma diag_default=Pe940
#endif
#if defined(codered) || defined(gcc) || defined(sourcerygxx)
uint32_t __attribute__((naked))
WidgetMutexGet(uint8_t *pi8Mutex)
{
uint32_t ui32Ret;
//
// Acquire the mutex if possible.
//
__asm(" mov r1, #1\n"
" ldrexb r2, [r0]\n"
" cmp r2, #0\n"
" it eq\n"
" strexbeq r2, r1, [r0]\n"
" mov r0, r2\n"
" bx lr\n"
: "=r" (ui32Ret));
//
// The return is handled in the inline assembly, but the compiler will
// still complain if there is not an explicit return here (despite the fact
// that this does not result in any code being produced because of the
// naked attribute).
//
return(ui32Ret);
}
#endif
#if defined(rvmdk) || defined(__ARMCC_VERSION)
__asm uint32_t
WidgetMutexGet(uint8_t *pi8Mutex)
{
mov r1, #1
ldrexb r2, [r0]
cmp r2, #0
it eq
strexbeq r2, r1, [r0]
mov r0, r2
bx lr
}
#endif
//
// For CCS implement this function in pure assembly. This prevents the TI
// compiler from doing funny things with the optimizer.
//
#if defined(ccs)
__asm(" .sect \".text:WidgetMutexGet\"\n"
" .clink\n"
" .thumbfunc WidgetMutexGet\n"
" .thumb\n"
" .global WidgetMutexGet\n"
"WidgetMutexGet:\n"
" mov r1, #1\n"
" ldrexb r2, [r0]\n"
" cmp r2, #0\n"
" it EQ\n" // TI assembler requires upper case cond
" strexbeq r2, r1, [r0]\n"
" mov r0, r2\n"
" bx lr\n");
#endif
//*****************************************************************************
//
//! Release a mutex.
//!
//! \param pi8Mutex is a pointer to mutex that is to be released.
//!
//! This function releases a mutual exclusion semaphore (mutex), leaving it in
//! the unowned state.
//!
//! \return None.
//
//*****************************************************************************
void
WidgetMutexPut(uint8_t *pi8Mutex)
{
//
// Release the mutex.
//
*pi8Mutex = 0;
}
//*****************************************************************************
//
// Determines if a widget exists in the tree below a given point.
//
// \param psWidget is a pointer to the widget tree.
// \param psFind is a pointer to the widget that is being searched for.
//
// This function searches the widget tree below psWidget to determine whether
// or not the widget pointed to by \e psFind exists in the subtree.
//
// \return Returns \b true if \e psFind exists in the subtree or \b false if it
// does not.
//
//*****************************************************************************
static bool
WidgetIsInTree(tWidget *psWidget, tWidget *psFind)
{
tWidget *psTemp;
//
// Check the arguments.
//
ASSERT(psWidget);
ASSERT(psFind);
//
// Loop through the tree under the widget until every widget is searched.
//
for(psTemp = psWidget; psTemp != psWidget->psParent; )
{
//
// See if this widget has a child.
//
if(psTemp->psChild)
{
//
// Go to this widget's child first.
//
psTemp = psTemp->psChild;
}
//
// This widget does not have a child, so either a sibling or a parent
// must be checked. When moving back to the parent, another move must
// be performed as well to avoid getting stuck in a loop (since the
// parent's children have already been searched.
//
else
{
//
// Loop until returning to the parent of the starting widget. This
// loop will be explicitly broken out of if an intervening widget
// is encountered that has not been searched.
//
while(psTemp != psWidget->psParent)
{
if(psTemp == psFind)
{
return(true);
}
//
// See if this widget has a sibling.
//
if(psTemp->psNext)
{
//
// Visit the sibling of this widget.
//
psTemp = psTemp->psNext;
//
// Since this widget has not been searched yet, break out
// of the controlling loop.
//
break;
}
else
{
//
// This widget has no siblings, so go to its parent. Since
// the parent has already been searched, the same sibling
// vs. parent decision must be made on this widget as well.
//
psTemp = psTemp->psParent;
}
}
}
}
//
// The widget could not be found.
//
return(false);
}
//*****************************************************************************
//
//! Handles widget messages.
//!
//! \param psWidget is a pointer to the widget.
//! \param ui32Message is the message to be processed.
//! \param ui32Param1 is the first parameter to the message.
//! \param ui32Param2 is the second parameter to the message.
//!
//! This function is a default handler for widget messages; it simply ignores
//! all messages sent to it. This is used as the message handler for the root
//! widget, and should be called by the message handler for other widgets when
//! they do not explicitly handle the provided message (in case new messages
//! are added that require some default but override-able processing).
//!
//! \return Always returns 0.
//
//*****************************************************************************
int32_t
WidgetDefaultMsgProc(tWidget *psWidget, uint32_t ui32Message,
uint32_t ui32Param1, uint32_t ui32Param2)
{
//
// Check the arguments.
//
ASSERT(psWidget);
//
// Return zero for all messages.
//
return(0);
}
//*****************************************************************************
//
//! Adds a widget to the widget tree.
//!
//! \param psParent is the parent for the widget. To add to the root of the tree
//! set this parameter to \b WIDGET_ROOT.
//! \param psWidget is the widget to add.
//!
//! This function adds a widget to the widget tree at the given position within
//! the tree. The widget will become the last child of its parent, and will
//! therefore be searched after the existing children.
//!
//! The added widget can be a full widget tree, allowing addition of an entire
//! heirarchy all at once (for example, adding an entire screen to the widget
//! tree all at once). In this case, it is the responsibility of the caller to
//! ensure that the psParent field of each widget in the added tree is correctly
//! set (in other words, only the widget pointed to by \e psWidget is updated to
//! properly reside in the tree).
//!
//! It is the responsibility of the caller to initialize the psNext and psChild
//! field of the added widget; either of these fields being non-zero results in
//! a pre-defined tree of widgets being added instead of a single one.
//!
//! \return None.
//
//*****************************************************************************
void
WidgetAdd(tWidget *psParent, tWidget *psWidget)
{
//
// Check the arguments.
//
ASSERT(psParent);
ASSERT(psWidget);
//
// Make this widget be a child of its parent.
//
psWidget->psParent = psParent;
//
// See if this parent already has children.
//
if(psParent->psChild)
{
//
// Find the last child of this parent and also check that widget is not
// already present at this level of the tree.
//
for(psParent = psParent->psChild; psParent->psNext;
psParent = psParent->psNext)
{
//
// If we find this widget here already, just return. If we don't
// do this, we allow errant programs to add the same child twice
// resulting in looping on message processing.
//
if(psParent == psWidget)
{
return;
}
}
//
// We perform one final check to see if we are about to add the widget
// twice. We need this to catch the case of a single child which
// causes the previous loop to exit before performing the widget check.
//
if(psParent == psWidget)
{
return;
}
//
// Add this widget to the end of the list of children of this parent.
//
psParent->psNext = psWidget;
}
else
{
//
// Make this widget be the first (and only) child of this parent.
//
psParent->psChild = psWidget;
}
}
//*****************************************************************************
//
//! Removes a widget from the widget tree.
//!
//! \param psWidget is the widget to be removed.
//!
//! This function removes a widget from the widget tree. The removed widget
//! can be a full widget tree, allowing removal of an entire heirarchy all at
//! once (for example, removing an entire screen from the widget tree).
//!
//! \return None.
//
//*****************************************************************************
void
WidgetRemove(tWidget *psWidget)
{
tWidget *psTemp;
//
// Check the argument.
//
ASSERT(psWidget);
//
// Make sure that the supplied widget is actually in the tree section
// owned by its parent and, hence, removeable.
//
if(!psWidget->psParent || !WidgetIsInTree(psWidget->psParent, psWidget))
{
return;
}
//
// See if this widget is the first child of its parent.
//
if(psWidget->psParent->psChild == psWidget)
{
//
// Make the first child of this widgets parent be this widget's
// sibling.
//
psWidget->psParent->psChild = psWidget->psNext;
}
else
{
//
// Find the sibling directly before this widget.
//
for(psTemp = psWidget->psParent->psChild; psTemp->psNext != psWidget;
psTemp = psTemp->psNext)
{
}
//
// Make the previous sibling point to the next sibling, removing this
// widget from the sibling chain.
//
psTemp->psNext = psWidget->psNext;
}
//
// Check to see if the widget which currently owns the pointer has just
// been removed and, if so, clear the pointer focus.
//
if(g_psPointerWidget && !WidgetIsInTree(&g_sRoot, g_psPointerWidget))
{
g_psPointerWidget = 0;
}
//
// Clear the next pointer of the widget.
//
psWidget->psNext = 0;
}
//*****************************************************************************
//
//! Sends a message to a widget tree via a pre-order, depth-first search.
//!
//! \param psWidget is a pointer to the widget tree.
//! \param ui32Message is the message to send.
//! \param ui32Param1 is the first parameter to the message.
//! \param ui32Param2 is the second parameter to the message.
//! \param bStopOnSuccess is \b true if the search should be stopped when the
//! first widget is found that returns success in response to the message.
//!
//! This function performs a pre-order, depth-first search of the widget tree,
//! sending a message to each widget encountered. In a depth-first search, the
//! children of a widget are searched before its siblings (preferring to go
//! deeper into the tree, hence the name depth-first). A pre-order search
//! means that the message is sent to a widget before any of its children are
//! searched.
//!
//! An example use of the pre-order search is for paint messages; the larger
//! enclosing widgets should be drawn on the screen before the smaller widgets
//! that reside within the parent widget (otherwise, the children would be
//! overwritten by the parent).
//!
//! \return Returns 0 if \e bStopOnSuccess is false or no widget returned
//! success in response to the message, or the value returned by the first
//! widget to successfully process the message.
//
//*****************************************************************************
uint32_t
WidgetMessageSendPreOrder(tWidget *psWidget, uint32_t ui32Message,
uint32_t ui32Param1, uint32_t ui32Param2,
bool bStopOnSuccess)
{
uint32_t ui32Ret;
tWidget *psTemp;
//
// Check the arguments.
//
ASSERT(psWidget);
//
// Send the message to the initial widget and return if it succeeded and
// the search should stop on success.
//
ui32Ret = psWidget->pfnMsgProc(psWidget, ui32Message, ui32Param1,
ui32Param2);
if((ui32Ret != 0) && bStopOnSuccess)
{
return(ui32Ret);
}
//
// Return if the widget does not have any children.
//
if(!psWidget->psChild)
{
return(0);
}
//
// Loop through the tree under the widget until every widget is searched.
//
for(psTemp = psWidget->psChild; psTemp != psWidget; )
{
//
// Send the message to this widget and return if it succeeded and the
// search should stop on success.
//
ui32Ret = psTemp->pfnMsgProc(psTemp, ui32Message, ui32Param1,
ui32Param2);
if((ui32Ret != 0) && bStopOnSuccess)
{
return(ui32Ret);
}
//
// Find the next widget to examine. If this widget has a child, then
// that is the next widget to examine.
//
if(psTemp->psChild)
{
psTemp = psTemp->psChild;
}
//
// This widget does not have a child, so either a sibling or a parent
// must be checked. When moving back to the parent, another move must
// be performed as well to avoid getting stuck in a loop (since the
// parent's children have already been searched).
//
else
{
//
// Loop until returning to the starting widget. This loop will be
// explicitly broken out of if an intervening widget is encountered
// that has not be searched.
//
while(psTemp != psWidget)
{
//
// See if this widget has a sibling.
//
if(psTemp->psNext)
{
//
// Visit the sibling of this widget.
//
psTemp = psTemp->psNext;
//
// Since this widget has not been searched yet, break out
// of the controlling loop.
//
break;
}
else
{
//
// This widget has no siblings, so go to its parent. Since
// the parent has already been searched, the same sibling
// vs. parent decision must be made on this widget as well.
//
psTemp = psTemp->psParent;
}
}
}
}
//
// No widget returned success for the message, or bStopOnSuccess was zero,
// so return zero.
//
return(0);
}
//*****************************************************************************
//
//! Sends a message to a widget tree via a post-order, depth-first search.
//!
//! \param psWidget is a pointer to the widget tree; if this is zero then the
//! root of the widget tree willb e used.
//! \param ui32Message is the message to send.
//! \param ui32Param1 is the first parameter to the message.
//! \param ui32Param2 is the second parameter to the message.
//! \param bStopOnSuccess is \b true if the search should be stopped when the
//! first widget is found that returns success in response to the message.
//!
//! This function performs a post-order, depth-first search of the widget tree,
//! sending a message to each widget encountered. In a depth-first search, the
//! children of a widget are searched before its sibling (preferring to go
//! deeper into the tree, hence the name depth-first). A post-order search
//! means that the message is sent to a widget after all of its children are
//! searched.
//!
//! An example use of the post-order search is for pointer-related messages;
//! those messages should be delivered to the lowest widget in the tree before
//! its parents (in other words, the widget deepest in the tree that has a hit
//! should get the message, not the higher up widgets that also include the hit
//! location).
//!
//! Special handling is performed for pointer-related messages. The widget
//! that accepts \b #WIDGET_MSG_PTR_DOWN is remembered and subsequent
//! \b #WIDGET_MSG_PTR_MOVE and \b #WIDGET_MSG_PTR_UP messages are sent
//! directly to that widget.
//!
//! \return Returns 0 if \e bStopOnSuccess is \b false or no widget returned
//! success in response to the message, or the value returned by the first
//! widget to successfully process the message.
//
//*****************************************************************************
uint32_t
WidgetMessageSendPostOrder(tWidget *psWidget, uint32_t ui32Message,
uint32_t ui32Param1, uint32_t ui32Param2,
bool bStopOnSuccess)
{
uint32_t ui32Ret;
tWidget *psTemp;
//
// Check the arguments.
//
ASSERT(psWidget);
//
// See if this is a pointer move or up message.
//
if((ui32Message == WIDGET_MSG_PTR_MOVE) ||
(ui32Message == WIDGET_MSG_PTR_UP))
{
//
// If there is not a widget that has captured pointer messages, then
// simply drop this message.
//
if(!g_psPointerWidget)
{
return(0);
}
//
// Send the message directly to the widget that has captured pointer
// messages.
//
ui32Ret = g_psPointerWidget->pfnMsgProc(g_psPointerWidget, ui32Message,
ui32Param1, ui32Param2);
//
// See if this is a pointer up message.
//
if(ui32Message == WIDGET_MSG_PTR_UP)
{
//
// Since this was a pointer up, the widget no longer has pointer
// messages captured.
//
g_psPointerWidget = 0;
}
//
// Return the value returned by the pointer capture widget.
//
return(ui32Ret);
}
//
// Loop through the tree under the widget until every widget is searched.
//
for(psTemp = psWidget; psTemp != psWidget->psParent; )
{
//
// See if this widget has a child.
//
if(psTemp->psChild)
{
//
// Go to this widget's child first.
//
psTemp = psTemp->psChild;
}
//
// This widget does not have a child, so either a sibling or a parent
// must be checked. When moving back to the parent, another move must
// be performed as well to avoid getting stuck in a loop (since the
// parent's children have already been searched.
//
else
{
//
// Loop until returning to the parent of the starting widget. This
// loop will be explicitly broken out of if an intervening widget
// is encountered that has not been searched.
//
while(psTemp != psWidget->psParent)
{
//
// Send the message to this widget.
//
ui32Ret = psTemp->pfnMsgProc(psTemp, ui32Message, ui32Param1,
ui32Param2);
//
// If this is a pointer down message, the widget accepted the
// message and the handler didn't modify the tree such that
// this widget is no longer present, then save a pointer to the
// widget for subsequent pointer move or pointer up messages.
//
if((ui32Message == WIDGET_MSG_PTR_DOWN) && (ui32Ret != 0))
{
//
// Is the current widget still in the tree?
//
if(WidgetIsInTree(&g_sRoot, psTemp))
{
//
// The widget is still in the tree so save it for later
// use.
//
g_psPointerWidget = psTemp;
}
else
{
//
// Although this widget handled the PTR_DOWN message,
// it's message handler rearranged the widget tree and
// removed itself so we don't want to send any more
// messages directly to it after all.
//
g_psPointerWidget = 0;
}
}
//
// If the widget returned success and the search should stop on
// success then return immediately.
//
if((ui32Ret != 0) && bStopOnSuccess)
{
return(ui32Ret);
}
//
// See if this widget has a sibling.
//
if(psTemp->psNext)
{
//
// Visit the sibling of this widget.
//
psTemp = psTemp->psNext;
//
// Since this widget has not been searched yet, break out
// of the controlling loop.
//
break;
}
else
{
//
// This widget has no siblings, so go to its parent. Since
// the parent has already been searched, the same sibling
// vs. parent decision must be made on this widget as well.
//
psTemp = psTemp->psParent;
}
}
}
}
//
// No widget returned success for the message, or bStopOnSuccess was zero,
// so return zero.
//
return(0);
}
//*****************************************************************************
//
//! Adds a message to the widget message queue.
//!
//! \param psWidget is the widget to which the message should be sent.
//! \param ui32Message is the message to be sent.
//! \param ui32Param1 is the first parameter to the message.
//! \param ui32Param2 is the second parameter to the message.
//! \param bPostOrder is \b true if the message should be sent via a post-order
//! search, and \b false if it should be sent via a pre-order search.
//! \param bStopOnSuccess is \b true if the message should be sent to widgets
//! until one returns success, and \b false if it should be sent to all
//! widgets.
//!
//! This function places a widget message into the message queue for later
//! processing. The messages are removed from the queue by
//! WidgetMessageQueueProcess() and sent to the appropriate place.
//!
//! It is safe for code which interrupts WidgetMessageQueueProcess() (or called
//! by it) to call this function to send a message. It is not safe for code
//! which interrupts this function to call this function as well; it is up to
//! the caller to guarantee that the later sequence never occurs.
//!
//! \return Returns 1 if the message was added to the queue, and 0 if it could
//! not be added since either the queue is full or another context is currently
//! adding a message to the queue.
//
//*****************************************************************************
int32_t
WidgetMessageQueueAdd(tWidget *psWidget, uint32_t ui32Message,
uint32_t ui32Param1, uint32_t ui32Param2,
bool bPostOrder, bool bStopOnSuccess)
{
uint32_t ui32Next;
uint32_t ui32Owned;
//
// Check the arguments.
//
ASSERT(psWidget);
//
// Get the mutex we use to protect access to the message queue.
//
ui32Owned = WidgetMutexGet(&g_ui8MQMutex);
if(ui32Owned)
{
//
// The mutex is already being held by some other caller so return a
// failure.
//
#ifdef DEBUG_MSGQ
g_ui32MQMutexClash++;
#endif
return(0);
}
//
// Compute the next value for the write pointer.
//
ui32Next = (g_ui32MQWrite + 1) % QUEUE_SIZE;
//
// If the queue is not empty, and this is a pointer move message, see if
// the previous message was also a move and, if so, replace the
// coordinates. Without this, the message queue can very quickly overflow
// if the application is busy doing something while the user keeps pressing
// the display.
//
if(ui32Message == WIDGET_MSG_PTR_MOVE)
{
//
// Is the message queue empty?
//
if(g_ui32MQRead != g_ui32MQWrite)
{
//
// No - what is the index of the previous message?
//
ui32Owned = (g_ui32MQWrite == 0) ? (QUEUE_SIZE - 1) :
(g_ui32MQWrite - 1);
//
// Was this a pointer move message?
//
if(g_psMQ[g_ui32MQWrite].ui32Message == WIDGET_MSG_PTR_MOVE)
{
//
// Yes - overwrite this message with the new
// coordinate information.
//
g_psMQ[ui32Owned].ui32Param1 = ui32Param1;
g_psMQ[ui32Owned].ui32Param2 = ui32Param2;
#ifdef DEBUG_MSGQ
g_ui32MQMoveOverwrite++;
#endif
//
// Release the message queue mutex.
//
WidgetMutexPut(&g_ui8MQMutex);
//
// Success.
//
return(1);
}
}
}
//
// Return a failure if the message queue is full.
//
if(ui32Next == g_ui32MQRead)
{
#ifdef DEBUG_MSGQ
g_ui32MQOverflow++;
if(ui32Message != WIDGET_MSG_PTR_MOVE)
{
g_ui32MQNonMouseOverflow++;
g_ui32MQLastLostMsg = ui32Message;
}
#endif
//
// Release the message queue mutex.
//
WidgetMutexPut(&g_ui8MQMutex);
return(0);
}
//
// Write this message into the next location in the message queue.
//
g_psMQ[g_ui32MQWrite].ui32Flags = ((bPostOrder ? MQ_FLAG_POST_ORDER : 0) |
(bStopOnSuccess ? MQ_FLAG_STOP_ON_SUCCESS :
0));
g_psMQ[g_ui32MQWrite].psWidget = psWidget;
g_psMQ[g_ui32MQWrite].ui32Message = ui32Message;
g_psMQ[g_ui32MQWrite].ui32Param1 = ui32Param1;
g_psMQ[g_ui32MQWrite].ui32Param2 = ui32Param2;
//
// Update the message queue write pointer.
//
g_ui32MQWrite = ui32Next;
//
// Release the message queue mutex.
//
WidgetMutexPut(&g_ui8MQMutex);
//
// Success.
//
return(1);
}
//*****************************************************************************
//
//! Processes the messages in the widget message queue.
//!
//! This function extracts messages from the widget message queue one at a time
//! and processes them. If the processing of a widget message requires that a
//! new message be sent, it is acceptable to call WidgetMessageQueueAdd(). It
//! is also acceptable for code which interrupts this function to call
//! WidgetMessageQueueAdd() to send more messages. In both cases, the newly
//! added message will also be processed before this function returns.
//!
//! \return None.
//
//*****************************************************************************
void
WidgetMessageQueueProcess(void)
{
tWidget *psWidget;
uint32_t ui32Flags, ui32Message, ui32Param1, ui32Param2;
//
// Loop while there are more messages in the message queue.
//
while(g_ui32MQRead != g_ui32MQWrite)
{
//
// Copy the contents of this message into local variables.
//
psWidget = g_psMQ[g_ui32MQRead].psWidget;
ui32Flags = g_psMQ[g_ui32MQRead].ui32Flags;
ui32Message = g_psMQ[g_ui32MQRead].ui32Message;
ui32Param1 = g_psMQ[g_ui32MQRead].ui32Param1;
ui32Param2 = g_psMQ[g_ui32MQRead].ui32Param2;
//
// Remove this message from the queue.
//
g_ui32MQRead = (g_ui32MQRead + 1) % QUEUE_SIZE;
//
// See if this message should be sent via a post-order or pre-order
// search.
//
if(ui32Flags & MQ_FLAG_POST_ORDER)
{
//
// Send this message with a post-order search of the widget tree.
//
WidgetMessageSendPostOrder(psWidget, ui32Message, ui32Param1,
ui32Param2,
((ui32Flags & MQ_FLAG_STOP_ON_SUCCESS) ?
true : false));
}
else
{
//
// Send this message with a pre-order search of the widget tree.
//
WidgetMessageSendPreOrder(psWidget, ui32Message, ui32Param1,
ui32Param2,
((ui32Flags & MQ_FLAG_STOP_ON_SUCCESS) ?
true : false));
}
}
}
//*****************************************************************************
//
//! Sends a pointer message.
//!
//! \param ui32Message is the pointer message to be sent.
//! \param i32X is the X coordinate associated with the message.
//! \param i32Y is the Y coordinate associated with the message.
//!
//! This function sends a pointer message to the root widget. A pointer driver
//! (such as a touch screen driver) can use this function to deliver pointer
//! activity to the widget tree without having to have direct knowledge of the
//! structure of the widget framework.
//!
//! \return Returns 1 if the message was added to the queue, and 0 if it could
//! not be added since the queue is full.
//
//*****************************************************************************
int32_t
WidgetPointerMessage(uint32_t ui32Message, int32_t i32X, int32_t i32Y)
{
//
// Add the message to the widget message queue.
//
return(WidgetMessageQueueAdd(WIDGET_ROOT, ui32Message, i32X, i32Y, true,
true));
}
//*****************************************************************************
//
// Close the Doxygen group.
//! @}
//
//*****************************************************************************
|