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