/work/toxcore/events/conference_connected.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_struct.h" |
18 | | |
19 | | /***************************************************** |
20 | | * |
21 | | * :: struct and accessors |
22 | | * |
23 | | *****************************************************/ |
24 | | |
25 | | struct Tox_Event_Conference_Connected { |
26 | | uint32_t conference_number; |
27 | | }; |
28 | | |
29 | | static void tox_event_conference_connected_set_conference_number(Tox_Event_Conference_Connected *_Nonnull conference_connected, uint32_t conference_number) |
30 | 28 | { |
31 | 28 | assert(conference_connected != nullptr); |
32 | 28 | conference_connected->conference_number = conference_number; |
33 | 28 | } |
34 | | uint32_t tox_event_conference_connected_get_conference_number(const Tox_Event_Conference_Connected *conference_connected) |
35 | 19 | { |
36 | 19 | assert(conference_connected != nullptr); |
37 | 19 | return conference_connected->conference_number; |
38 | 19 | } |
39 | | |
40 | | static void tox_event_conference_connected_construct(Tox_Event_Conference_Connected *_Nonnull conference_connected) |
41 | 50 | { |
42 | 50 | *conference_connected = (Tox_Event_Conference_Connected) { |
43 | 50 | 0 |
44 | 50 | }; |
45 | 50 | } |
46 | | static void tox_event_conference_connected_destruct(Tox_Event_Conference_Connected *_Nonnull conference_connected, const Memory *_Nonnull mem) |
47 | 50 | { |
48 | 50 | return; |
49 | 50 | } |
50 | | |
51 | | bool tox_event_conference_connected_pack( |
52 | | const Tox_Event_Conference_Connected *event, Bin_Pack *bp) |
53 | 44 | { |
54 | 44 | return bin_pack_u32(bp, event->conference_number); |
55 | 44 | } |
56 | | |
57 | | static bool tox_event_conference_connected_unpack_into(Tox_Event_Conference_Connected *_Nonnull event, Bin_Unpack *_Nonnull bu) |
58 | 22 | { |
59 | 22 | assert(event != nullptr); |
60 | 22 | return bin_unpack_u32(bu, &event->conference_number); |
61 | 22 | } |
62 | | |
63 | | /***************************************************** |
64 | | * |
65 | | * :: new/free/add/get/size/unpack |
66 | | * |
67 | | *****************************************************/ |
68 | | |
69 | | const Tox_Event_Conference_Connected *tox_event_get_conference_connected(const Tox_Event *event) |
70 | 0 | { |
71 | 0 | return event->type == TOX_EVENT_CONFERENCE_CONNECTED ? event->data.conference_connected : nullptr; |
72 | 0 | } |
73 | | |
74 | | Tox_Event_Conference_Connected *tox_event_conference_connected_new(const Memory *mem) |
75 | 51 | { |
76 | 51 | Tox_Event_Conference_Connected *const conference_connected = |
77 | 51 | (Tox_Event_Conference_Connected *)mem_alloc(mem, sizeof(Tox_Event_Conference_Connected)); |
78 | | |
79 | 51 | if (conference_connected == nullptr) { |
80 | 1 | return nullptr; |
81 | 1 | } |
82 | | |
83 | 50 | tox_event_conference_connected_construct(conference_connected); |
84 | 50 | return conference_connected; |
85 | 51 | } |
86 | | |
87 | | void tox_event_conference_connected_free(Tox_Event_Conference_Connected *conference_connected, const Memory *mem) |
88 | 51 | { |
89 | 51 | if (conference_connected != nullptr) { |
90 | 50 | tox_event_conference_connected_destruct(conference_connected, mem); |
91 | 50 | } |
92 | 51 | mem_delete(mem, conference_connected); |
93 | 51 | } |
94 | | |
95 | | static Tox_Event_Conference_Connected *_Nullable tox_events_add_conference_connected(Tox_Events *_Nonnull events, const Memory *_Nonnull mem) |
96 | 28 | { |
97 | 28 | Tox_Event_Conference_Connected *const conference_connected = tox_event_conference_connected_new(mem); |
98 | | |
99 | 28 | if (conference_connected == nullptr) { |
100 | 0 | return nullptr; |
101 | 0 | } |
102 | | |
103 | 28 | Tox_Event event; |
104 | 28 | event.type = TOX_EVENT_CONFERENCE_CONNECTED; |
105 | 28 | event.data.conference_connected = conference_connected; |
106 | | |
107 | 28 | if (!tox_events_add(events, &event)) { |
108 | 0 | tox_event_conference_connected_free(conference_connected, mem); |
109 | 0 | return nullptr; |
110 | 0 | } |
111 | 28 | return conference_connected; |
112 | 28 | } |
113 | | |
114 | | bool tox_event_conference_connected_unpack( |
115 | | Tox_Event_Conference_Connected **event, Bin_Unpack *bu, const Memory *mem) |
116 | 23 | { |
117 | 23 | assert(event != nullptr); |
118 | 23 | assert(*event == nullptr); |
119 | 23 | *event = tox_event_conference_connected_new(mem); |
120 | | |
121 | 23 | if (*event == nullptr) { |
122 | 1 | return false; |
123 | 1 | } |
124 | | |
125 | 22 | return tox_event_conference_connected_unpack_into(*event, bu); |
126 | 23 | } |
127 | | |
128 | | static Tox_Event_Conference_Connected *_Nullable tox_event_conference_connected_alloc(Tox_Events_State *_Nonnull state) |
129 | 28 | { |
130 | 28 | if (state->events == nullptr) { |
131 | 0 | return nullptr; |
132 | 0 | } |
133 | | |
134 | 28 | Tox_Event_Conference_Connected *conference_connected = tox_events_add_conference_connected(state->events, state->mem); |
135 | | |
136 | 28 | if (conference_connected == nullptr) { |
137 | 0 | state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; |
138 | 0 | return nullptr; |
139 | 0 | } |
140 | | |
141 | 28 | return conference_connected; |
142 | 28 | } |
143 | | |
144 | | /***************************************************** |
145 | | * |
146 | | * :: event handler |
147 | | * |
148 | | *****************************************************/ |
149 | | |
150 | | void tox_events_handle_conference_connected( |
151 | | Tox *_Nonnull tox, |
152 | | uint32_t conference_number, |
153 | | void *_Nullable user_data) |
154 | 28 | { |
155 | 28 | Tox_Events_State *state = tox_events_alloc(user_data); |
156 | 28 | Tox_Event_Conference_Connected *conference_connected = tox_event_conference_connected_alloc(state); |
157 | | |
158 | 28 | if (conference_connected == nullptr) { |
159 | 0 | return; |
160 | 0 | } |
161 | | |
162 | 28 | tox_event_conference_connected_set_conference_number(conference_connected, conference_number); |
163 | 28 | } |
164 | | |
165 | | void tox_events_handle_conference_connected_dispatch(Tox *tox, const Tox_Event_Conference_Connected *event, void *user_data) |
166 | 0 | { |
167 | 0 | if (tox->conference_connected_callback == nullptr) { |
168 | 0 | return; |
169 | 0 | } |
170 | | |
171 | 0 | tox_unlock(tox); |
172 | 0 | tox->conference_connected_callback(tox, event->conference_number, user_data); |
173 | 0 | tox_lock(tox); |
174 | 0 | } |