1 /* WinRT Windows.Media.SpeechRecognition implementation
3 * Copyright 2022 Bernhard Kölbl
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 static CRITICAL_SECTION handlers_cs
;
23 static CRITICAL_SECTION_DEBUG handlers_cs_debug
=
26 { &handlers_cs_debug
.ProcessLocksList
, &handlers_cs_debug
.ProcessLocksList
},
27 0, 0, { (DWORD_PTR
)(__FILE__
": handlers_cs") }
29 static CRITICAL_SECTION handlers_cs
= { &handlers_cs_debug
, -1, 0, 0, 0, 0 };
30 static EventRegistrationToken next_token
= {.value
= 1};
32 struct typed_event_handler_entry
35 EventRegistrationToken token
;
36 ITypedEventHandler_IInspectable_IInspectable
*handler
;
39 HRESULT
typed_event_handlers_append( struct list
*list
, ITypedEventHandler_IInspectable_IInspectable
*handler
, EventRegistrationToken
*token
)
41 struct typed_event_handler_entry
*entry
;
43 if (!(entry
= calloc(1, sizeof(*entry
)))) return E_OUTOFMEMORY
;
44 ITypedEventHandler_IInspectable_IInspectable_AddRef((entry
->handler
= handler
));
46 EnterCriticalSection(&handlers_cs
);
48 *token
= entry
->token
= next_token
;
50 list_add_tail(list
, &entry
->entry
);
52 LeaveCriticalSection(&handlers_cs
);
57 HRESULT
typed_event_handlers_remove( struct list
*list
, EventRegistrationToken
*token
)
59 struct typed_event_handler_entry
*entry
;
62 EnterCriticalSection(&handlers_cs
);
64 LIST_FOR_EACH_ENTRY(entry
, list
, struct typed_event_handler_entry
, entry
)
65 if ((found
= !memcmp(&entry
->token
, token
, sizeof(*token
)))) break;
66 if (found
) list_remove(&entry
->entry
);
68 LeaveCriticalSection(&handlers_cs
);
72 ITypedEventHandler_IInspectable_IInspectable_Release(entry
->handler
);
79 HRESULT
typed_event_handlers_notify( struct list
*list
, IInspectable
*sender
, IInspectable
*args
)
81 struct typed_event_handler_entry
*entry
;
83 EnterCriticalSection(&handlers_cs
);
85 LIST_FOR_EACH_ENTRY(entry
, list
, struct typed_event_handler_entry
, entry
)
86 ITypedEventHandler_IInspectable_IInspectable_Invoke(entry
->handler
, sender
, args
);
88 LeaveCriticalSection(&handlers_cs
);
93 HRESULT
typed_event_handlers_clear( struct list
* list
)
95 struct typed_event_handler_entry
*entry
, *entry_cursor2
;
97 EnterCriticalSection(&handlers_cs
);
99 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry_cursor2
, list
, struct typed_event_handler_entry
, entry
)
101 list_remove(&entry
->entry
);
105 LeaveCriticalSection(&handlers_cs
);