/work/toxcore/events/group_voice_state.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* SPDX-License-Identifier: GPL-3.0-or-later |
2 | | * Copyright © 2023-2026 The TokTok team. |
3 | | */ |
4 | | |
5 | | #include "events_alloc.h" |
6 | | |
7 | | #include <assert.h> |
8 | | |
9 | | #include "../attributes.h" |
10 | | #include "../bin_pack.h" |
11 | | #include "../bin_unpack.h" |
12 | | #include "../ccompat.h" |
13 | | #include "../mem.h" |
14 | | #include "../tox.h" |
15 | | #include "../tox_event.h" |
16 | | #include "../tox_events.h" |
17 | | #include "../tox_pack.h" |
18 | | #include "../tox_struct.h" |
19 | | #include "../tox_unpack.h" |
20 | | |
21 | | /***************************************************** |
22 | | * |
23 | | * :: struct and accessors |
24 | | * |
25 | | *****************************************************/ |
26 | | |
27 | | struct Tox_Event_Group_Voice_State { |
28 | | uint32_t group_number; |
29 | | Tox_Group_Voice_State voice_state; |
30 | | }; |
31 | | |
32 | | static void tox_event_group_voice_state_set_group_number(Tox_Event_Group_Voice_State *_Nonnull group_voice_state, uint32_t group_number) |
33 | 16 | { |
34 | 16 | assert(group_voice_state != nullptr); |
35 | 16 | group_voice_state->group_number = group_number; |
36 | 16 | } |
37 | | uint32_t tox_event_group_voice_state_get_group_number(const Tox_Event_Group_Voice_State *group_voice_state) |
38 | 0 | { |
39 | 0 | assert(group_voice_state != nullptr); |
40 | 0 | return group_voice_state->group_number; |
41 | 0 | } |
42 | | |
43 | | static void tox_event_group_voice_state_set_voice_state(Tox_Event_Group_Voice_State *_Nonnull group_voice_state, Tox_Group_Voice_State voice_state) |
44 | 16 | { |
45 | 16 | assert(group_voice_state != nullptr); |
46 | 16 | group_voice_state->voice_state = voice_state; |
47 | 16 | } |
48 | | Tox_Group_Voice_State tox_event_group_voice_state_get_voice_state(const Tox_Event_Group_Voice_State *group_voice_state) |
49 | 16 | { |
50 | 16 | assert(group_voice_state != nullptr); |
51 | 16 | return group_voice_state->voice_state; |
52 | 16 | } |
53 | | |
54 | | static void tox_event_group_voice_state_construct(Tox_Event_Group_Voice_State *_Nonnull group_voice_state) |
55 | 61 | { |
56 | 61 | *group_voice_state = (Tox_Event_Group_Voice_State) { |
57 | 61 | 0 |
58 | 61 | }; |
59 | 61 | } |
60 | | static void tox_event_group_voice_state_destruct(Tox_Event_Group_Voice_State *_Nonnull group_voice_state, const Memory *_Nonnull mem) |
61 | 61 | { |
62 | 61 | return; |
63 | 61 | } |
64 | | |
65 | | bool tox_event_group_voice_state_pack( |
66 | | const Tox_Event_Group_Voice_State *event, Bin_Pack *bp) |
67 | 38 | { |
68 | 38 | return bin_pack_array(bp, 2) |
69 | 38 | && bin_pack_u32(bp, event->group_number) |
70 | 38 | && tox_group_voice_state_pack(event->voice_state, bp); |
71 | 38 | } |
72 | | |
73 | | static bool tox_event_group_voice_state_unpack_into(Tox_Event_Group_Voice_State *_Nonnull event, Bin_Unpack *_Nonnull bu) |
74 | 45 | { |
75 | 45 | assert(event != nullptr); |
76 | 45 | if (!bin_unpack_array_fixed(bu, 2, nullptr)) { |
77 | 4 | return false; |
78 | 4 | } |
79 | | |
80 | 41 | return bin_unpack_u32(bu, &event->group_number) |
81 | 41 | && tox_group_voice_state_unpack(&event->voice_state, bu); |
82 | 45 | } |
83 | | |
84 | | /***************************************************** |
85 | | * |
86 | | * :: new/free/add/get/size/unpack |
87 | | * |
88 | | *****************************************************/ |
89 | | |
90 | | const Tox_Event_Group_Voice_State *tox_event_get_group_voice_state(const Tox_Event *event) |
91 | 0 | { |
92 | 0 | return event->type == TOX_EVENT_GROUP_VOICE_STATE ? event->data.group_voice_state : nullptr; |
93 | 0 | } |
94 | | |
95 | | Tox_Event_Group_Voice_State *tox_event_group_voice_state_new(const Memory *mem) |
96 | 62 | { |
97 | 62 | Tox_Event_Group_Voice_State *const group_voice_state = |
98 | 62 | (Tox_Event_Group_Voice_State *)mem_alloc(mem, sizeof(Tox_Event_Group_Voice_State)); |
99 | | |
100 | 62 | if (group_voice_state == nullptr) { |
101 | 1 | return nullptr; |
102 | 1 | } |
103 | | |
104 | 61 | tox_event_group_voice_state_construct(group_voice_state); |
105 | 61 | return group_voice_state; |
106 | 62 | } |
107 | | |
108 | | void tox_event_group_voice_state_free(Tox_Event_Group_Voice_State *group_voice_state, const Memory *mem) |
109 | 62 | { |
110 | 62 | if (group_voice_state != nullptr) { |
111 | 61 | tox_event_group_voice_state_destruct(group_voice_state, mem); |
112 | 61 | } |
113 | 62 | mem_delete(mem, group_voice_state); |
114 | 62 | } |
115 | | |
116 | | static Tox_Event_Group_Voice_State *_Nullable tox_events_add_group_voice_state(Tox_Events *_Nonnull events, const Memory *_Nonnull mem) |
117 | 16 | { |
118 | 16 | Tox_Event_Group_Voice_State *const group_voice_state = tox_event_group_voice_state_new(mem); |
119 | | |
120 | 16 | if (group_voice_state == nullptr) { |
121 | 0 | return nullptr; |
122 | 0 | } |
123 | | |
124 | 16 | Tox_Event event; |
125 | 16 | event.type = TOX_EVENT_GROUP_VOICE_STATE; |
126 | 16 | event.data.group_voice_state = group_voice_state; |
127 | | |
128 | 16 | if (!tox_events_add(events, &event)) { |
129 | 0 | tox_event_group_voice_state_free(group_voice_state, mem); |
130 | 0 | return nullptr; |
131 | 0 | } |
132 | 16 | return group_voice_state; |
133 | 16 | } |
134 | | |
135 | | bool tox_event_group_voice_state_unpack( |
136 | | Tox_Event_Group_Voice_State **event, Bin_Unpack *bu, const Memory *mem) |
137 | 46 | { |
138 | 46 | assert(event != nullptr); |
139 | 46 | assert(*event == nullptr); |
140 | 46 | *event = tox_event_group_voice_state_new(mem); |
141 | | |
142 | 46 | if (*event == nullptr) { |
143 | 1 | return false; |
144 | 1 | } |
145 | | |
146 | 45 | return tox_event_group_voice_state_unpack_into(*event, bu); |
147 | 46 | } |
148 | | |
149 | | static Tox_Event_Group_Voice_State *_Nullable tox_event_group_voice_state_alloc(Tox_Events_State *_Nonnull state) |
150 | 16 | { |
151 | 16 | if (state->events == nullptr) { |
152 | 0 | return nullptr; |
153 | 0 | } |
154 | | |
155 | 16 | Tox_Event_Group_Voice_State *group_voice_state = tox_events_add_group_voice_state(state->events, state->mem); |
156 | | |
157 | 16 | if (group_voice_state == nullptr) { |
158 | 0 | state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; |
159 | 0 | return nullptr; |
160 | 0 | } |
161 | | |
162 | 16 | return group_voice_state; |
163 | 16 | } |
164 | | |
165 | | /***************************************************** |
166 | | * |
167 | | * :: event handler |
168 | | * |
169 | | *****************************************************/ |
170 | | |
171 | | void tox_events_handle_group_voice_state( |
172 | | Tox *_Nonnull tox, |
173 | | uint32_t group_number, |
174 | | Tox_Group_Voice_State voice_state, |
175 | | void *_Nullable user_data) |
176 | 16 | { |
177 | 16 | Tox_Events_State *state = tox_events_alloc(user_data); |
178 | 16 | Tox_Event_Group_Voice_State *group_voice_state = tox_event_group_voice_state_alloc(state); |
179 | | |
180 | 16 | if (group_voice_state == nullptr) { |
181 | 0 | return; |
182 | 0 | } |
183 | | |
184 | 16 | tox_event_group_voice_state_set_group_number(group_voice_state, group_number); |
185 | 16 | tox_event_group_voice_state_set_voice_state(group_voice_state, voice_state); |
186 | 16 | } |
187 | | |
188 | | void tox_events_handle_group_voice_state_dispatch(Tox *tox, const Tox_Event_Group_Voice_State *event, void *user_data) |
189 | 0 | { |
190 | 0 | if (tox->group_voice_state_callback == nullptr) { |
191 | 0 | return; |
192 | 0 | } |
193 | | |
194 | 0 | tox_unlock(tox); |
195 | 0 | tox->group_voice_state_callback(tox, event->group_number, event->voice_state, user_data); |
196 | 0 | tox_lock(tox); |
197 | 0 | } |