Coverage Report

Created: 2026-10-24 08:16

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