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 #include "gx_widget.h"
29 
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _gx_system_input_release                            PORTABLE C      */
36 /*                                                           6.2.1        */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Kenneth Maxwell, Microsoft Corporation                              */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    Release previously captured input events                            */
44 /*                                                                        */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    owner                                 Widget that releases input    */
49 /*                                                                        */
50 /*  OUTPUT                                                                */
51 /*                                                                        */
52 /*    status                                Return status                 */
53 /*                                                                        */
54 /*  CALLS                                                                 */
55 /*                                                                        */
56 /*    _GX_ENTER_CRITICAL                    Enter critical section        */
57 /*    _gx_widget_status_remove              Remove a flag from the widget */
58 /*    _GX_EXIT_CRITICAL                     Exit critical section         */
59 /*                                                                        */
60 /*  CALLED BY                                                             */
61 /*                                                                        */
62 /*    GUIX Internal Code                                                  */
63 /*                                                                        */
64 /*  RELEASE HISTORY                                                       */
65 /*                                                                        */
66 /*    DATE              NAME                      DESCRIPTION             */
67 /*                                                                        */
68 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
69 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
70 /*                                            resulting in version 6.1    */
71 /*  07-29-2022     Kenneth Maxwell          Modified comment(s),          */
72 /*                                            optimized logic and ensured */
73 /*                                            released stack entries are  */
74 /*                                            reset to NULL,              */
75 /*                                            resulting in version 6.1.12 */
76 /*  03-08-2023     Ting Zhu                 Modified comment(s),          */
77 /*                                            improved logic,             */
78 /*                                            resulting in version 6.2.1  */
79 /*                                                                        */
80 /**************************************************************************/
81 
_gx_system_input_release(GX_WIDGET * owner)82 UINT _gx_system_input_release(GX_WIDGET *owner)
83 {
84 UINT        status = GX_PTR_ERROR;
85 GX_WIDGET **stack;
86 INT         current;
87 INT         top;
88 
89     GX_ENTER_CRITICAL
90     if (_gx_system_capture_count > 0)
91     {
92         if (owner -> gx_widget_status & GX_STATUS_OWNS_INPUT)
93         {
94             stack = _gx_system_input_capture_stack;
95             top = _gx_system_capture_count - 1;
96             current = 0;
97 
98             while (current <= top)
99             {
100                 if (stack[current] == owner)
101                 {
102                     _gx_widget_status_remove(owner, GX_STATUS_OWNS_INPUT);
103                     _gx_system_capture_count--;
104                     status = GX_SUCCESS;
105                     break;
106                 }
107                 current++;
108             }
109 
110             if (status == GX_SUCCESS)
111             {
112                 /* collapse the stack if this entry was in the middle */
113                 while (current < top)
114                 {
115                     stack[current] = stack[current + 1];
116                     current++;
117                 }
118                 stack[current] = GX_NULL;
119 
120                 if (top > 0)
121                 {
122                     _gx_system_input_owner = stack[top - 1];
123                 }
124                 else
125                 {
126                     _gx_system_input_owner = GX_NULL;
127                 }
128             }
129         }
130         else
131         {
132             status = GX_NO_CHANGE;
133         }
134     }
135     else
136     {
137         status = GX_NO_CHANGE;
138     }
139 
140     GX_EXIT_CRITICAL
141     return status;
142 }
143 
144