/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-2025 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 | | |
18 | | /***************************************************** |
19 | | * |
20 | | * :: struct and accessors |
21 | | * |
22 | | *****************************************************/ |
23 | | |
24 | | struct Tox_Event_Friend_Typing { |
25 | | uint32_t friend_number; |
26 | | bool typing; |
27 | | }; |
28 | | |
29 | | static void tox_event_friend_typing_set_friend_number(Tox_Event_Friend_Typing *_Nonnull friend_typing, uint32_t friend_number) |
30 | 999 | { |
31 | 999 | assert(friend_typing != nullptr); |
32 | 999 | friend_typing->friend_number = friend_number; |
33 | 999 | } |
34 | | uint32_t tox_event_friend_typing_get_friend_number(const Tox_Event_Friend_Typing *friend_typing) |
35 | 0 | { |
36 | 0 | assert(friend_typing != nullptr); |
37 | 0 | return friend_typing->friend_number; |
38 | 0 | } |
39 | | |
40 | | static void tox_event_friend_typing_set_typing(Tox_Event_Friend_Typing *_Nonnull friend_typing, bool typing) |
41 | 999 | { |
42 | 999 | assert(friend_typing != nullptr); |
43 | 999 | friend_typing->typing = typing; |
44 | 999 | } |
45 | | bool tox_event_friend_typing_get_typing(const Tox_Event_Friend_Typing *friend_typing) |
46 | 22 | { |
47 | 22 | assert(friend_typing != nullptr); |
48 | 22 | return friend_typing->typing; |
49 | 22 | } |
50 | | |
51 | | static void tox_event_friend_typing_construct(Tox_Event_Friend_Typing *_Nonnull friend_typing) |
52 | 1.05k | { |
53 | 1.05k | *friend_typing = (Tox_Event_Friend_Typing) { |
54 | 1.05k | 0 |
55 | 1.05k | }; |
56 | 1.05k | } |
57 | | static void tox_event_friend_typing_destruct(Tox_Event_Friend_Typing *_Nonnull friend_typing, const Memory *_Nonnull mem) |
58 | 1.05k | { |
59 | 1.05k | return; |
60 | 1.05k | } |
61 | | |
62 | | bool tox_event_friend_typing_pack( |
63 | | const Tox_Event_Friend_Typing *event, Bin_Pack *bp) |
64 | 44 | { |
65 | 44 | return bin_pack_array(bp, 2) |
66 | 44 | && bin_pack_u32(bp, event->friend_number) |
67 | 44 | && bin_pack_bool(bp, event->typing); |
68 | 44 | } |
69 | | |
70 | | static bool tox_event_friend_typing_unpack_into(Tox_Event_Friend_Typing *_Nonnull event, Bin_Unpack *_Nonnull bu) |
71 | 41 | { |
72 | 41 | assert(event != nullptr); |
73 | 41 | if (!bin_unpack_array_fixed(bu, 2, nullptr)) { |
74 | 2 | return false; |
75 | 2 | } |
76 | | |
77 | 39 | return bin_unpack_u32(bu, &event->friend_number) |
78 | 39 | && bin_unpack_bool(bu, &event->typing); |
79 | 41 | } |
80 | | |
81 | | /***************************************************** |
82 | | * |
83 | | * :: new/free/add/get/size/unpack |
84 | | * |
85 | | *****************************************************/ |
86 | | |
87 | | const Tox_Event_Friend_Typing *tox_event_get_friend_typing(const Tox_Event *event) |
88 | 0 | { |
89 | 0 | return event->type == TOX_EVENT_FRIEND_TYPING ? event->data.friend_typing : nullptr; |
90 | 0 | } |
91 | | |
92 | | Tox_Event_Friend_Typing *tox_event_friend_typing_new(const Memory *mem) |
93 | 1.06k | { |
94 | 1.06k | Tox_Event_Friend_Typing *const friend_typing = |
95 | 1.06k | (Tox_Event_Friend_Typing *)mem_alloc(mem, sizeof(Tox_Event_Friend_Typing)); |
96 | | |
97 | 1.06k | if (friend_typing == nullptr) { |
98 | 13 | return nullptr; |
99 | 13 | } |
100 | | |
101 | 1.05k | tox_event_friend_typing_construct(friend_typing); |
102 | 1.05k | return friend_typing; |
103 | 1.06k | } |
104 | | |
105 | | void tox_event_friend_typing_free(Tox_Event_Friend_Typing *friend_typing, const Memory *mem) |
106 | 1.05k | { |
107 | 1.05k | if (friend_typing != nullptr) { |
108 | 1.05k | tox_event_friend_typing_destruct(friend_typing, mem); |
109 | 1.05k | } |
110 | 1.05k | mem_delete(mem, friend_typing); |
111 | 1.05k | } |
112 | | |
113 | | static Tox_Event_Friend_Typing *tox_events_add_friend_typing(Tox_Events *_Nonnull events, const Memory *_Nonnull mem) |
114 | 1.02k | { |
115 | 1.02k | Tox_Event_Friend_Typing *const friend_typing = tox_event_friend_typing_new(mem); |
116 | | |
117 | 1.02k | if (friend_typing == nullptr) { |
118 | 12 | return nullptr; |
119 | 12 | } |
120 | | |
121 | 1.01k | Tox_Event event; |
122 | 1.01k | event.type = TOX_EVENT_FRIEND_TYPING; |
123 | 1.01k | event.data.friend_typing = friend_typing; |
124 | | |
125 | 1.01k | if (!tox_events_add(events, &event)) { |
126 | 11 | tox_event_friend_typing_free(friend_typing, mem); |
127 | 11 | return nullptr; |
128 | 11 | } |
129 | 999 | return friend_typing; |
130 | 1.01k | } |
131 | | |
132 | | bool tox_event_friend_typing_unpack( |
133 | | Tox_Event_Friend_Typing **event, Bin_Unpack *bu, const Memory *mem) |
134 | 42 | { |
135 | 42 | assert(event != nullptr); |
136 | 42 | assert(*event == nullptr); |
137 | 42 | *event = tox_event_friend_typing_new(mem); |
138 | | |
139 | 42 | if (*event == nullptr) { |
140 | 1 | return false; |
141 | 1 | } |
142 | | |
143 | 41 | return tox_event_friend_typing_unpack_into(*event, bu); |
144 | 42 | } |
145 | | |
146 | | static Tox_Event_Friend_Typing *tox_event_friend_typing_alloc(void *_Nonnull user_data) |
147 | 1.03k | { |
148 | 1.03k | Tox_Events_State *state = tox_events_alloc(user_data); |
149 | 1.03k | assert(state != nullptr); |
150 | | |
151 | 1.03k | if (state->events == nullptr) { |
152 | 9 | return nullptr; |
153 | 9 | } |
154 | | |
155 | 1.02k | Tox_Event_Friend_Typing *friend_typing = tox_events_add_friend_typing(state->events, state->mem); |
156 | | |
157 | 1.02k | if (friend_typing == nullptr) { |
158 | 23 | state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; |
159 | 23 | return nullptr; |
160 | 23 | } |
161 | | |
162 | 999 | return friend_typing; |
163 | 1.02k | } |
164 | | |
165 | | /***************************************************** |
166 | | * |
167 | | * :: event handler |
168 | | * |
169 | | *****************************************************/ |
170 | | |
171 | | void tox_events_handle_friend_typing( |
172 | | Tox *tox, uint32_t friend_number, bool typing, |
173 | | void *user_data) |
174 | 1.03k | { |
175 | 1.03k | Tox_Event_Friend_Typing *friend_typing = tox_event_friend_typing_alloc(user_data); |
176 | | |
177 | 1.03k | if (friend_typing == nullptr) { |
178 | 32 | return; |
179 | 32 | } |
180 | | |
181 | 999 | tox_event_friend_typing_set_friend_number(friend_typing, friend_number); |
182 | 999 | tox_event_friend_typing_set_typing(friend_typing, typing); |
183 | 999 | } |