1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** GUIX Component                                                        */
16 /**                                                                       */
17 /**   System Management (System)                                          */
18 /**                                                                       */
19 /**************************************************************************/
20 
21 #define GX_SOURCE_CODE
22 
23 
24 /* Include necessary system files.  */
25 
26 #include "gx_api.h"
27 #include "gx_system.h"
28 
29 #if !defined(GX_THREAD_SLEEPING)
30 #define GX_THREAD_SLEEPING
31 #endif
32 
33 #if !defined(GX_THREAD_AWAKE)
34 #define GX_THREAD_AWAKE
35 #endif
36 
37 /**************************************************************************/
38 /*                                                                        */
39 /*  FUNCTION                                               RELEASE        */
40 /*                                                                        */
41 /*    _gx_system_thread_entry                             PORTABLE C      */
42 /*                                                           6.1          */
43 /*  AUTHOR                                                                */
44 /*                                                                        */
45 /*    Kenneth Maxwell, Microsoft Corporation                              */
46 /*                                                                        */
47 /*  DESCRIPTION                                                           */
48 /*                                                                        */
49 /*    This is the main processing thread for GUIX. All events and drawing */
50 /*    are done from the context of this thread.                           */
51 /*                                                                        */
52 /*  INPUT                                                                 */
53 /*                                                                        */
54 /*    id                                    Thread ID                     */
55 /*                                                                        */
56 /*  OUTPUT                                                                */
57 /*                                                                        */
58 /*    None                                                                */
59 /*                                                                        */
60 /*  CALLS                                                                 */
61 /*                                                                        */
62 /*    tx_mutex_get                          Get protection mutex          */
63 /*    tx_mutex_put                          Release protection mutex      */
64 /*    tx_queue_receive                      Receive GUIX events from queue*/
65 /*    _gx_system_dirty_mark                 Mark a widget as dirty        */
66 /*    _gx_system_error_process              Process system errors         */
67 /*    _gx_system_event_dispatch             Dispatch GUIX events          */
68 /*    _gx_system_canvas_refresh             Refresh the canvas            */
69 /*    _gx_system_timer_update               Update active system timers   */
70 /*                                                                        */
71 /*  CALLED BY                                                             */
72 /*                                                                        */
73 /*    ThreadX                                                             */
74 /*                                                                        */
75 /*  RELEASE HISTORY                                                       */
76 /*                                                                        */
77 /*    DATE              NAME                      DESCRIPTION             */
78 /*                                                                        */
79 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
80 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
81 /*                                            resulting in version 6.1    */
82 /*                                                                        */
83 /**************************************************************************/
_gx_system_thread_entry(ULONG id)84 VOID  _gx_system_thread_entry(ULONG id)
85 {
86 
87 UINT status = GX_FAILURE;
88 
89 #ifdef GX_THREADX_BINDING
90 ULONG    event_memory[GX_EVENT_ULONGS];
91 #else
92 GX_EVENT event_memory;
93 #endif
94 
95 GX_EVENT  *event_ptr;
96 GX_WIDGET *widget;
97 
98     GX_PARAMETER_NOT_USED(id);
99 
100     /* Loop to process GUIX events.  */
101     while (1)
102     {
103 #ifdef GX_THREADX_BINDING
104         /* Pickup event from event queue.  */
105         status =  tx_queue_receive(&_gx_system_event_queue, &event_memory[0], TX_NO_WAIT);
106 
107         /* Was there an event?  */
108         if (status == TX_QUEUE_EMPTY)
109         {
110             /* No event to process, so re-draw dirty widgets */
111             _gx_system_canvas_refresh();
112 
113             /* Now block this thread until an event is received.  */
114             GX_THREAD_SLEEPING
115             status = tx_queue_receive(&_gx_system_event_queue, &event_memory[0], TX_WAIT_FOREVER);
116             if (status == TX_SUCCESS)
117             {
118                 status = GX_SUCCESS;
119             }
120         }
121 #else
122         /* here for generic RTOS binding */
123         status = GX_EVENT_POP(&event_memory, GX_FALSE);
124         if (status == GX_FAILURE)
125         {
126             _gx_system_canvas_refresh();
127             GX_THREAD_SLEEPING
128             status = GX_EVENT_POP(&event_memory, GX_TRUE);
129         }
130 #endif
131 
132         /* Check for a successful status.  */
133         if (status == GX_SUCCESS)
134         {
135             GX_THREAD_AWAKE
136             /* Setup a pointer to the event.  */
137             event_ptr =  (GX_EVENT *)(&event_memory);
138 
139             switch (event_ptr -> gx_event_type)
140             {
141             /* Determine if a redraw event is present.  */
142             case GX_EVENT_REDRAW:
143                 /* Yes, a redraw event is present, detect and process the dirty area(s).  */
144                 widget = (GX_WIDGET *)_gx_system_root_window_created_list;
145                 while (widget)
146                 {
147                     _gx_system_dirty_mark(widget);
148                     widget = widget -> gx_widget_next;
149                 }
150                 break;
151 
152             case GX_EVENT_TERMINATE:
153                 return;
154 
155             case GX_EVENT_TIMER:
156                 if (event_ptr ->gx_event_target == GX_NULL)
157                 {
158                     /* the event is from gx_system_timer_expiration */
159                     _gx_system_timer_update(event_ptr -> gx_event_payload.gx_event_ulongdata);
160                 }
161                 else
162                 {
163                     _gx_system_event_dispatch(event_ptr);
164                 }
165                 break;
166 
167             case 0:
168                 /* the event has been purged */
169                 break;
170 
171             default:
172                 /* Dispatch the event to GUIX proper window/widget.  */
173                 _gx_system_event_dispatch(event_ptr);
174                 break;
175             }
176         }
177         else
178         {
179             /* Error receiving event - call system error handler.  */
180             _gx_system_error_process(GX_SYSTEM_EVENT_RECEIVE_ERROR);
181 
182             /* Return to exit the system thread.  */
183             return;
184         }
185     }
186 }
187 
188