2023-07-25 11:53:09 +02:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
2024-01-09 16:39:05 +01:00
|
|
|
// Copyright © 2023-2024 The TokTok team.
|
2023-07-25 11:53:09 +02:00
|
|
|
|
|
|
|
// this file can be used to generate event.c files
|
|
|
|
// requires c++17
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <vector>
|
|
|
|
#include <variant>
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
std::string str_tolower(std::string s) {
|
|
|
|
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c){ return std::tolower(c); });
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string str_toupper(std::string s) {
|
|
|
|
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c){ return std::toupper(c); });
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string output_folder = "out";
|
|
|
|
|
|
|
|
struct EventTypeTrivial {
|
|
|
|
std::string type; // eg uint32_t
|
|
|
|
std::string name; // eg friend_number
|
|
|
|
};
|
|
|
|
|
|
|
|
struct EventTypeByteRange {
|
|
|
|
// type is always uint8_t * for data AND uint32_t for length
|
|
|
|
std::string name_data; // eg data
|
|
|
|
std::string name_length; // eg data_length
|
|
|
|
std::string name_length_cb; // eg length // TODO: merge with normal?
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: EventTypeByteArray
|
|
|
|
|
|
|
|
using EventType = std::variant<EventTypeTrivial, EventTypeByteRange>;
|
|
|
|
|
|
|
|
// helper type for the visitor #4
|
|
|
|
template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
|
|
|
|
// explicit deduction guide (not needed as of C++20)
|
|
|
|
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
|
|
|
|
|
|
|
|
std::string bin_pack_name_from_type(const std::string& type) {
|
|
|
|
if (type == "uint64_t") {
|
|
|
|
return "bin_pack_u64";
|
|
|
|
} else if (type == "uint32_t") {
|
|
|
|
return "bin_pack_u32";
|
|
|
|
} else if (type == "uint16_t") {
|
|
|
|
return "bin_pack_u16";
|
|
|
|
} else if (type == "uint8_t") {
|
|
|
|
return "bin_pack_u8";
|
|
|
|
} else if (type == "bool") {
|
|
|
|
return "bin_pack_bool";
|
|
|
|
// only unpack is special TODO(Green-Sky): should we change that?
|
|
|
|
//} else if (type == "Tox_User_Status") {
|
|
|
|
//return "tox_pack_user_status";
|
|
|
|
//} else if (type == "Tox_Conference_Type") {
|
|
|
|
//return "tox_pack_conference_type";
|
|
|
|
} else {
|
|
|
|
//std::cerr << "unknown type " << type << "\n";
|
|
|
|
//exit(1);
|
|
|
|
// assume enum -> u32
|
|
|
|
return "bin_pack_u32";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::string bin_unpack_name_from_type(const std::string& type) {
|
|
|
|
if (type == "uint64_t") {
|
|
|
|
return "bin_unpack_u64";
|
|
|
|
} else if (type == "uint32_t") {
|
|
|
|
return "bin_unpack_u32";
|
|
|
|
} else if (type == "uint16_t") {
|
|
|
|
return "bin_unpack_u16";
|
|
|
|
} else if (type == "uint8_t") {
|
|
|
|
return "bin_unpack_u8";
|
|
|
|
} else if (type == "bool") {
|
|
|
|
return "bin_unpack_bool";
|
|
|
|
} else if (type == "Tox_User_Status") {
|
2023-12-27 12:37:22 +01:00
|
|
|
return "tox_user_status_unpack";
|
2023-07-25 11:53:09 +02:00
|
|
|
} else if (type == "Tox_Conference_Type") {
|
2023-12-27 12:37:22 +01:00
|
|
|
return "tox_conference_type_unpack";
|
2023-07-25 11:53:09 +02:00
|
|
|
} else if (type == "Tox_Message_Type") {
|
2023-12-27 12:37:22 +01:00
|
|
|
return "tox_message_type_unpack";
|
2023-07-25 11:53:09 +02:00
|
|
|
} else if (type == "Tox_File_Control") {
|
2023-12-27 12:37:22 +01:00
|
|
|
return "tox_file_control_unpack";
|
2023-07-25 11:53:09 +02:00
|
|
|
} else if (type == "Tox_Connection") {
|
2023-12-27 12:37:22 +01:00
|
|
|
return "tox_connection_unpack";
|
2023-07-25 11:53:09 +02:00
|
|
|
} else if (type == "Tox_Group_Privacy_State") {
|
2023-12-27 12:37:22 +01:00
|
|
|
return "tox_group_privacy_state_unpack";
|
2023-07-25 11:53:09 +02:00
|
|
|
} else if (type == "Tox_Group_Voice_State") {
|
2023-12-27 12:37:22 +01:00
|
|
|
return "tox_group_voice_state_unpack";
|
2023-07-25 11:53:09 +02:00
|
|
|
} else if (type == "Tox_Group_Topic_Lock") {
|
2023-12-27 12:37:22 +01:00
|
|
|
return "tox_group_topic_lock_unpack";
|
2023-07-25 11:53:09 +02:00
|
|
|
} else if (type == "Tox_Group_Join_Fail") {
|
2023-12-27 12:37:22 +01:00
|
|
|
return "tox_group_join_fail_unpack";
|
2023-07-25 11:53:09 +02:00
|
|
|
} else if (type == "Tox_Group_Mod_Event") {
|
2023-12-27 12:37:22 +01:00
|
|
|
return "tox_group_mod_event_unpack";
|
2023-07-25 11:53:09 +02:00
|
|
|
} else if (type == "Tox_Group_Exit_Type") {
|
2023-12-27 12:37:22 +01:00
|
|
|
return "tox_group_exit_type_unpack";
|
2023-07-25 11:53:09 +02:00
|
|
|
} else {
|
|
|
|
std::cerr << "unknown type " << type << "\n";
|
|
|
|
exit(1);
|
|
|
|
// assume enum -> u32
|
|
|
|
return "bin_unpack_u32";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-27 12:37:22 +01:00
|
|
|
void generate_event_impl(const std::string& event_name, const std::vector<EventType>& event_types) {
|
2023-07-25 11:53:09 +02:00
|
|
|
const std::string event_name_l = str_tolower(event_name);
|
|
|
|
std::string file_name = output_folder + "/" + event_name_l + ".c";
|
|
|
|
|
|
|
|
std::ofstream f(file_name);
|
|
|
|
if (!f.good()) {
|
|
|
|
std::cerr << "error opening file to write " << file_name << "\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2024-01-09 16:39:05 +01:00
|
|
|
bool need_stdlib_h = false;
|
|
|
|
bool need_string_h = false;
|
|
|
|
bool need_tox_unpack_h = false;
|
|
|
|
for (const auto& t : event_types) {
|
|
|
|
std::visit(
|
|
|
|
overloaded{
|
|
|
|
[&](const EventTypeTrivial& t) {
|
|
|
|
if (bin_unpack_name_from_type(t.type).rfind("tox_", 0) == 0) {
|
|
|
|
need_tox_unpack_h = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[&](const EventTypeByteRange&) {
|
|
|
|
need_stdlib_h = true;
|
|
|
|
need_string_h = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
t
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-07-25 11:53:09 +02:00
|
|
|
f << R"(/* SPDX-License-Identifier: GPL-3.0-or-later
|
2024-01-09 16:39:05 +01:00
|
|
|
* Copyright © 2023-2024 The TokTok team.
|
2023-07-25 11:53:09 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "events_alloc.h"
|
|
|
|
|
2024-01-09 16:39:05 +01:00
|
|
|
#include <assert.h>)";
|
|
|
|
if (need_stdlib_h) {
|
|
|
|
f << R"(
|
|
|
|
#include <stdlib.h>)";
|
|
|
|
}
|
|
|
|
if (need_string_h) {
|
|
|
|
f << R"(
|
|
|
|
#include <string.h>)";
|
|
|
|
}
|
|
|
|
f << R"(
|
2023-07-25 11:53:09 +02:00
|
|
|
|
|
|
|
#include "../bin_pack.h"
|
|
|
|
#include "../bin_unpack.h"
|
|
|
|
#include "../ccompat.h"
|
2024-01-09 16:39:05 +01:00
|
|
|
#include "../mem.h"
|
2023-07-25 11:53:09 +02:00
|
|
|
#include "../tox.h"
|
2024-01-09 16:39:05 +01:00
|
|
|
#include "../tox_events.h")";
|
|
|
|
if (need_tox_unpack_h) {
|
|
|
|
f << R"(
|
|
|
|
#include "../tox_unpack.h")";
|
|
|
|
}
|
|
|
|
f << R"(
|
2023-07-25 11:53:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************
|
|
|
|
*
|
|
|
|
* :: struct and accessors
|
|
|
|
*
|
|
|
|
*****************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
)";
|
|
|
|
|
|
|
|
// gen struct
|
|
|
|
f << "struct Tox_Event_" << event_name << " {\n";
|
|
|
|
for (const auto& t : event_types) {
|
|
|
|
std::visit(
|
|
|
|
overloaded{
|
|
|
|
[&](const EventTypeTrivial& t) {
|
|
|
|
f << " " << t.type << " " << t.name << ";\n";
|
|
|
|
},
|
|
|
|
[&](const EventTypeByteRange& t) {
|
|
|
|
f << " " << "uint8_t" << " *" << t.name_data << ";\n";
|
|
|
|
f << " " << "uint32_t" << " " << t.name_length << ";\n";
|
|
|
|
}
|
|
|
|
},
|
|
|
|
t
|
|
|
|
);
|
|
|
|
}
|
|
|
|
f << "};\n\n";
|
|
|
|
|
|
|
|
// gen setters and getters
|
|
|
|
for (const auto& t : event_types) {
|
|
|
|
// setter
|
|
|
|
f << "non_null()\n";
|
|
|
|
f << "static " << (t.index() == 0 ? "void" : "bool") << " tox_event_" << event_name_l << "_set_";
|
|
|
|
std::visit(
|
|
|
|
overloaded{
|
|
|
|
[&](const EventTypeTrivial& t) {
|
|
|
|
f << t.name;
|
|
|
|
},
|
|
|
|
[&](const EventTypeByteRange& t) {
|
|
|
|
f << t.name_data;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
t
|
|
|
|
);
|
|
|
|
f << "(Tox_Event_" << event_name << " *" << event_name_l << ",\n";
|
|
|
|
f << " ";
|
|
|
|
std::visit(
|
|
|
|
overloaded{
|
|
|
|
[&](const EventTypeTrivial& t) {
|
|
|
|
f << t.type << " " << t.name << ")\n";
|
|
|
|
},
|
|
|
|
[&](const EventTypeByteRange& t) {
|
|
|
|
f << "const uint8_t *" << t.name_data << ", uint32_t " << t.name_length << ")\n";
|
|
|
|
}
|
|
|
|
},
|
|
|
|
t
|
|
|
|
);
|
|
|
|
f << "{\n assert(" << event_name_l << " != nullptr);\n";
|
|
|
|
std::visit(
|
|
|
|
overloaded{
|
|
|
|
[&](const EventTypeTrivial& t) {
|
|
|
|
f << " " << event_name_l << "->" << t.name << " = " << t.name << ";\n";
|
|
|
|
},
|
|
|
|
[&](const EventTypeByteRange& t) {
|
|
|
|
f << "\n if (" << event_name_l << "->" << t.name_data << " != nullptr) {\n";
|
|
|
|
f << " free(" << event_name_l << "->" << t.name_data << ");\n";
|
|
|
|
f << " " << event_name_l << "->" << t.name_data << " = nullptr;\n";
|
|
|
|
f << " " << event_name_l << "->" << t.name_length << " = 0;\n";
|
|
|
|
f << " }\n\n";
|
2024-01-12 21:30:48 +01:00
|
|
|
f << " uint8_t *" << t.name_data << "_copy = (uint8_t *)malloc(" << t.name_length << ");\n\n";
|
|
|
|
f << " if (" << t.name_data << "_copy == nullptr) {\n";
|
2023-07-25 11:53:09 +02:00
|
|
|
f << " return false;\n }\n\n";
|
2024-01-12 21:30:48 +01:00
|
|
|
f << " memcpy(" << t.name_data << "_copy, " << t.name_data << ", " << t.name_length << ");\n";
|
|
|
|
f << " " << event_name_l << "->" << t.name_data << " = " << t.name_data << "_copy;\n";
|
2023-07-25 11:53:09 +02:00
|
|
|
f << " " << event_name_l << "->" << t.name_length << " = " << t.name_length << ";\n";
|
|
|
|
f << " return true;\n";
|
|
|
|
}
|
|
|
|
},
|
|
|
|
t
|
|
|
|
);
|
|
|
|
f << "}\n";
|
|
|
|
|
|
|
|
// getter
|
|
|
|
std::visit(
|
|
|
|
overloaded{
|
|
|
|
[&](const EventTypeTrivial& t) {
|
|
|
|
//f << "non_null()\n"; // TODO: is this missing in the original?
|
|
|
|
f << t.type << " tox_event_" << event_name_l << "_get_" << t.name;
|
|
|
|
f << "(const Tox_Event_" << event_name << " *" << event_name_l << ")\n";
|
|
|
|
f << "{\n assert(" << event_name_l << " != nullptr);\n";
|
|
|
|
f << " return " << event_name_l << "->" << t.name << ";\n}\n\n";
|
|
|
|
},
|
|
|
|
[&](const EventTypeByteRange& t) {
|
|
|
|
//f << "non_null()\n"; // TODO: is this missing in the original?
|
2023-12-27 12:37:22 +01:00
|
|
|
f << "uint32_t tox_event_" << event_name_l << "_get_" << t.name_length;
|
2023-07-25 11:53:09 +02:00
|
|
|
f << "(const Tox_Event_" << event_name << " *" << event_name_l << ")\n";
|
|
|
|
f << "{\n assert(" << event_name_l << " != nullptr);\n";
|
|
|
|
f << " return " << event_name_l << "->" << t.name_length << ";\n}\n";
|
|
|
|
f << "const uint8_t *tox_event_" << event_name_l << "_get_" << t.name_data;
|
|
|
|
f << "(const Tox_Event_" << event_name << " *" << event_name_l << ")\n";
|
|
|
|
f << "{\n assert(" << event_name_l << " != nullptr);\n";
|
|
|
|
f << " return " << event_name_l << "->" << t.name_data << ";\n}\n\n";
|
|
|
|
}
|
|
|
|
},
|
|
|
|
t
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-12-27 12:37:22 +01:00
|
|
|
|
|
|
|
// gen contruct
|
2023-07-25 11:53:09 +02:00
|
|
|
f << "non_null()\n";
|
2023-12-27 12:37:22 +01:00
|
|
|
f << "static void tox_event_" << event_name_l << "_construct(Tox_Event_" << event_name << " *" << event_name_l << ")\n{\n";
|
|
|
|
// TODO: initialize all members properly
|
|
|
|
// TODO: check if _NONE is universal
|
|
|
|
// str_toupper(
|
|
|
|
f << " *" << event_name_l << " = (Tox_Event_" << event_name << ") {\n 0\n };\n}\n";
|
|
|
|
|
|
|
|
// gen destruct
|
|
|
|
f << "non_null()\n";
|
|
|
|
f << "static void tox_event_" << event_name_l << "_destruct(Tox_Event_" << event_name << " *" << event_name_l << ", const Memory *mem)\n{\n";
|
|
|
|
size_t data_count = 0;
|
|
|
|
for (const auto& t : event_types) {
|
|
|
|
std::visit(
|
|
|
|
overloaded{
|
|
|
|
[&](const EventTypeTrivial&) {},
|
|
|
|
[&](const EventTypeByteRange& t) {
|
|
|
|
f << " free(" << event_name_l << "->" << t.name_data << ");\n";
|
|
|
|
//f << " mem->funcs->free(mem->obj, " << event_name_l << "->" << t.name_data << ");\n";
|
|
|
|
data_count++;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
t
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (data_count == 0) {
|
|
|
|
f << " return;\n";
|
|
|
|
}
|
|
|
|
f << "}\n\n";
|
|
|
|
|
|
|
|
// pack
|
|
|
|
f << "bool tox_event_" << event_name_l << "_pack(\n";
|
2023-07-25 11:53:09 +02:00
|
|
|
f << " const Tox_Event_" << event_name << " *event, Bin_Pack *bp)\n{\n";
|
|
|
|
f << " assert(event != nullptr);\n";
|
|
|
|
f << " return bin_pack_array(bp, 2)\n";
|
2023-12-27 12:37:22 +01:00
|
|
|
f << " && bin_pack_u32(bp, TOX_EVENT_" << str_toupper(event_name) << ")";
|
2023-07-25 11:53:09 +02:00
|
|
|
if (event_types.size() > 1) {
|
2023-12-27 12:37:22 +01:00
|
|
|
f << "\n && bin_pack_array(bp, " << event_types.size() << ")";
|
2023-07-25 11:53:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& t : event_types) {
|
|
|
|
f << "\n && ";
|
|
|
|
std::visit(
|
|
|
|
overloaded{
|
|
|
|
[&](const EventTypeTrivial& t) {
|
|
|
|
f << bin_pack_name_from_type(t.type);
|
|
|
|
f << "(bp, event->" << t.name << ")";
|
|
|
|
},
|
|
|
|
[&](const EventTypeByteRange& t) {
|
|
|
|
f << "bin_pack_bin(bp, event->" << t.name_data << ", event->" << t.name_length << ")";
|
|
|
|
}
|
|
|
|
},
|
|
|
|
t
|
|
|
|
);
|
|
|
|
}
|
|
|
|
f << ";\n}\n\n";
|
|
|
|
|
|
|
|
// unpack
|
|
|
|
f << "non_null()\n";
|
2023-12-27 12:37:22 +01:00
|
|
|
f << "static bool tox_event_" << event_name_l << "_unpack_into(\n";
|
2023-07-25 11:53:09 +02:00
|
|
|
f << " Tox_Event_" << event_name << " *event, Bin_Unpack *bu)\n{\n";
|
|
|
|
f << " assert(event != nullptr);\n";
|
|
|
|
if (event_types.size() > 1) {
|
2023-11-13 15:03:22 +01:00
|
|
|
f << " if (!bin_unpack_array_fixed(bu, " << event_types.size() << ", nullptr)) {\n return false;\n }\n\n";
|
2023-07-25 11:53:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool first = true;
|
|
|
|
for (const auto& t : event_types) {
|
|
|
|
if (first) {
|
|
|
|
f << " return ";
|
|
|
|
} else {
|
|
|
|
f << "\n && ";
|
|
|
|
}
|
|
|
|
std::visit(
|
|
|
|
overloaded{
|
|
|
|
[&](const EventTypeTrivial& t) {
|
|
|
|
f << bin_unpack_name_from_type(t.type);
|
|
|
|
f << "(bu, &event->" << t.name << ")";
|
|
|
|
},
|
|
|
|
[&](const EventTypeByteRange& t) {
|
|
|
|
f << "bin_unpack_bin(bu, &event->" << t.name_data << ", &event->" << t.name_length << ")";
|
|
|
|
}
|
|
|
|
},
|
|
|
|
t
|
|
|
|
);
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
f << ";\n}\n\n";
|
|
|
|
|
|
|
|
f << R"(
|
|
|
|
/*****************************************************
|
|
|
|
*
|
2023-12-27 12:37:22 +01:00
|
|
|
* :: new/free/add/get/size/unpack
|
2023-07-25 11:53:09 +02:00
|
|
|
*
|
|
|
|
*****************************************************/
|
|
|
|
|
|
|
|
)";
|
|
|
|
|
2023-12-27 12:37:22 +01:00
|
|
|
f << "const Tox_Event_" << event_name << " *tox_event_get_" << event_name_l << "(const Tox_Event *event)\n{\n";
|
|
|
|
f << " return event->type == TOX_EVENT_" << str_toupper(event_name) << " ? event->data." << event_name_l << " : nullptr;\n}\n\n";
|
|
|
|
|
|
|
|
// new
|
|
|
|
f << "Tox_Event_" << event_name << " *tox_event_" << event_name_l << "_new(const Memory *mem)\n{\n";
|
2023-07-25 11:53:09 +02:00
|
|
|
f << " Tox_Event_" << event_name << " *const " << event_name_l << " =\n";
|
2023-12-27 12:37:22 +01:00
|
|
|
f << " (Tox_Event_" << event_name << " *)mem_alloc(mem, sizeof(Tox_Event_" << event_name << "));\n\n";
|
|
|
|
f << " if (" << event_name_l << " == nullptr) {\n return nullptr;\n }\n\n";
|
2023-07-25 11:53:09 +02:00
|
|
|
f << " tox_event_" << event_name_l << "_construct(" << event_name_l << ");\n";
|
|
|
|
f << " return " << event_name_l << ";\n}\n\n";
|
|
|
|
|
2023-12-27 12:37:22 +01:00
|
|
|
// free
|
|
|
|
f << "void tox_event_" << event_name_l << "_free(Tox_Event_" << event_name << " *" << event_name_l << ", const Memory *mem)\n{\n";
|
|
|
|
f << " if (" << event_name_l << " != nullptr) {\n";
|
|
|
|
f << " tox_event_" << event_name_l << "_destruct(" << event_name_l << ", mem);\n }\n";
|
|
|
|
f << " mem_delete(mem, " << event_name_l << ");\n}\n\n";
|
2023-07-25 11:53:09 +02:00
|
|
|
|
2023-12-27 12:37:22 +01:00
|
|
|
// add
|
|
|
|
f << "non_null()\n";
|
|
|
|
f << "static Tox_Event_" << event_name << " *tox_events_add_" << event_name_l << "(Tox_Events *events, const Memory *mem)\n{\n";
|
|
|
|
f << " Tox_Event_" << event_name << " *const " << event_name_l << " = tox_event_" << event_name_l << "_new(mem);\n\n";
|
|
|
|
f << " if (" << event_name_l << " == nullptr) {\n";
|
|
|
|
f << " return nullptr;\n }\n\n";
|
|
|
|
f << " Tox_Event event;\n";
|
|
|
|
f << " event.type = TOX_EVENT_" << str_toupper(event_name) << ";\n";
|
|
|
|
f << " event.data." << event_name_l << " = " << event_name_l << ";\n\n";
|
|
|
|
f << " tox_events_add(events, &event);\n";
|
|
|
|
f << " return " << event_name_l << ";\n}\n\n";
|
2023-07-25 11:53:09 +02:00
|
|
|
|
2023-12-27 12:37:22 +01:00
|
|
|
// unpack
|
|
|
|
f << "bool tox_event_" << event_name_l << "_unpack(\n";
|
|
|
|
f << " Tox_Event_" << event_name << " **event, Bin_Unpack *bu, const Memory *mem)\n{\n";
|
|
|
|
f << " assert(event != nullptr);\n";
|
|
|
|
f << " *event = tox_event_" << event_name_l << "_new(mem);\n\n";
|
|
|
|
f << " if (*event == nullptr) {\n return false;\n }\n\n";
|
|
|
|
f << " return tox_event_" << event_name_l << "_unpack_into(*event, bu);\n}\n\n";
|
|
|
|
|
|
|
|
// alloc
|
|
|
|
f << "non_null()\n";
|
|
|
|
f << "static Tox_Event_" << event_name << " *tox_event_" << event_name_l << "_alloc(void *user_data)\n{\n";
|
|
|
|
f << " Tox_Events_State *state = tox_events_alloc(user_data);\n";
|
|
|
|
f << " assert(state != nullptr);\n\n";
|
|
|
|
f << " if (state->events == nullptr) {\n return nullptr;\n }\n\n";
|
|
|
|
f << " Tox_Event_" << event_name << " *" << event_name_l << " = tox_events_add_" << event_name_l << "(state->events, state->mem);\n\n";
|
|
|
|
f << " if (" << event_name_l << " == nullptr) {\n";
|
|
|
|
f << " state->error = TOX_ERR_EVENTS_ITERATE_MALLOC;\n return nullptr;\n }\n\n";
|
|
|
|
f << " return " << event_name_l << ";\n}\n\n";
|
2023-07-25 11:53:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
f << R"(
|
|
|
|
/*****************************************************
|
|
|
|
*
|
|
|
|
* :: event handler
|
|
|
|
*
|
|
|
|
*****************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
)";
|
|
|
|
f << "void tox_events_handle_" << event_name_l << "(Tox *tox";
|
|
|
|
|
|
|
|
for (const auto& t : event_types) {
|
|
|
|
std::visit(
|
|
|
|
overloaded{
|
|
|
|
[&](const EventTypeTrivial& t) {
|
|
|
|
f << ", " << t.type << " " << t.name;
|
|
|
|
},
|
|
|
|
[&](const EventTypeByteRange& t) {
|
|
|
|
f << ", const uint8_t *" << t.name_data << ", size_t " << t.name_length_cb;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
t
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
f << ",\n void *user_data)\n{\n";
|
2023-12-27 12:37:22 +01:00
|
|
|
f << " Tox_Event_" << event_name << " *" << event_name_l << " = tox_event_" << event_name_l << "_alloc(user_data);\n\n";
|
|
|
|
f << " if (" << event_name_l << " == nullptr) {\n return;\n }\n\n";
|
2023-07-25 11:53:09 +02:00
|
|
|
|
|
|
|
for (const auto& t : event_types) {
|
|
|
|
std::visit(
|
|
|
|
overloaded{
|
|
|
|
[&](const EventTypeTrivial& t) {
|
|
|
|
f << " tox_event_" << event_name_l << "_set_" << t.name << "(" << event_name_l << ", " << t.name << ");\n";
|
|
|
|
},
|
|
|
|
[&](const EventTypeByteRange& t) {
|
|
|
|
f << " tox_event_" << event_name_l << "_set_" << t.name_data << "(" << event_name_l << ", " << t.name_data << ", " << t.name_length_cb << ");\n";
|
|
|
|
}
|
|
|
|
},
|
|
|
|
t
|
|
|
|
);
|
|
|
|
}
|
|
|
|
f << "}\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
// c++ generate_event_c.cpp -std=c++17 && ./a.out Friend_Lossy_Packet && diff --color ../../toxcore/events/friend_lossy_packet.c out/friend_lossy_packet.c
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
// TODO: json?
|
|
|
|
std::map<std::string, std::vector<EventType>> event_defs {
|
|
|
|
{
|
|
|
|
"Conference_Connected",
|
|
|
|
{
|
|
|
|
EventTypeTrivial{"uint32_t", "conference_number"},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Conference_Invite",
|
|
|
|
{
|
|
|
|
EventTypeTrivial{"uint32_t", "friend_number"},
|
|
|
|
EventTypeTrivial{"Tox_Conference_Type", "type"},
|
2023-12-27 12:37:22 +01:00
|
|
|
EventTypeByteRange{"cookie", "cookie_length", "length"}, // the latter two are ideally the same
|
2023-07-25 11:53:09 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Conference_Message",
|
|
|
|
{
|
|
|
|
EventTypeTrivial{"uint32_t", "conference_number"},
|
|
|
|
EventTypeTrivial{"uint32_t", "peer_number"},
|
|
|
|
EventTypeTrivial{"Tox_Message_Type", "type"},
|
2023-12-27 12:37:22 +01:00
|
|
|
EventTypeByteRange{"message", "message_length", "length"}, // the latter two are ideally the same
|
2023-07-25 11:53:09 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Conference_Peer_List_Changed",
|
|
|
|
{
|
|
|
|
EventTypeTrivial{"uint32_t", "conference_number"},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Conference_Peer_Name",
|
|
|
|
{
|
|
|
|
EventTypeTrivial{"uint32_t", "conference_number"},
|
|
|
|
EventTypeTrivial{"uint32_t", "peer_number"},
|
2023-12-27 12:37:22 +01:00
|
|
|
EventTypeByteRange{"name", "name_length", "length"}, // the latter two are ideally the same
|
2023-07-25 11:53:09 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Conference_Title",
|
|
|
|
{
|
|
|
|
EventTypeTrivial{"uint32_t", "conference_number"},
|
|
|
|
EventTypeTrivial{"uint32_t", "peer_number"},
|
2023-12-27 12:37:22 +01:00
|
|
|
EventTypeByteRange{"title", "title_length", "length"}, // the latter two are ideally the same
|
2023-07-25 11:53:09 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"File_Chunk_Request",
|
|
|
|
{
|
|
|
|
EventTypeTrivial{"uint32_t", "friend_number"},
|
|
|
|
EventTypeTrivial{"uint32_t", "file_number"},
|
|
|
|
EventTypeTrivial{"uint64_t", "position"},
|
|
|
|
EventTypeTrivial{"uint16_t", "length"},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"File_Recv",
|
|
|
|
{
|
|
|
|
EventTypeTrivial{"uint32_t", "friend_number"},
|
|
|
|
EventTypeTrivial{"uint32_t", "file_number"},
|
|
|
|
EventTypeTrivial{"uint32_t", "kind"},
|
|
|
|
EventTypeTrivial{"uint64_t", "file_size"},
|
2023-12-27 12:37:22 +01:00
|
|
|
EventTypeByteRange{"filename", "filename_length", "filename_length"},
|
2023-07-25 11:53:09 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"File_Recv_Chunk",
|
|
|
|
{
|
|
|
|
EventTypeTrivial{"uint32_t", "friend_number"},
|
|
|
|
EventTypeTrivial{"uint32_t", "file_number"},
|
|
|
|
EventTypeTrivial{"uint64_t", "position"},
|
2023-12-27 12:37:22 +01:00
|
|
|
EventTypeByteRange{"data", "data_length", "length"}, // the latter two are ideally the same
|
2023-07-25 11:53:09 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"File_Recv_Control",
|
|
|
|
{
|
|
|
|
EventTypeTrivial{"uint32_t", "friend_number"},
|
|
|
|
EventTypeTrivial{"uint32_t", "file_number"},
|
|
|
|
EventTypeTrivial{"Tox_File_Control", "control"},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"Friend_Connection_Status",
|
|
|
|
{
|
|
|
|
EventTypeTrivial{"uint32_t", "friend_number"},
|
|
|
|
EventTypeTrivial{"Tox_Connection", "connection_status"},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Friend_Lossless_Packet",
|
|
|
|
{
|
|
|
|
EventTypeTrivial{"uint32_t", "friend_number"},
|
2023-12-27 12:37:22 +01:00
|
|
|
EventTypeByteRange{"data", "data_length", "length"}, // the latter two are ideally the same
|
2023-07-25 11:53:09 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Friend_Lossy_Packet",
|
|
|
|
{
|
|
|
|
EventTypeTrivial{"uint32_t", "friend_number"},
|
2023-12-27 12:37:22 +01:00
|
|
|
EventTypeByteRange{"data", "data_length", "length"}, // the latter two are ideally the same
|
2023-07-25 11:53:09 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Friend_Message",
|
|
|
|
{
|
|
|
|
EventTypeTrivial{"uint32_t", "friend_number"},
|
|
|
|
EventTypeTrivial{"Tox_Message_Type", "type"},
|
2023-12-27 12:37:22 +01:00
|
|
|
EventTypeByteRange{"message", "message_length", "length"}, // the latter two are ideally the same
|
2023-07-25 11:53:09 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Friend_Name",
|
|
|
|
{
|
|
|
|
EventTypeTrivial{"uint32_t", "friend_number"},
|
2023-12-27 12:37:22 +01:00
|
|
|
EventTypeByteRange{"name", "name_length", "length"}, // the latter two are ideally the same
|
2023-07-25 11:53:09 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Friend_Read_Receipt",
|
|
|
|
{
|
|
|
|
EventTypeTrivial{"uint32_t", "friend_number"},
|
|
|
|
EventTypeTrivial{"uint32_t", "message_id"},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
#if 0
|
|
|
|
{
|
|
|
|
"Friend_Request",
|
|
|
|
{
|
|
|
|
//EventTypeTrivial{"uint32_t", "friend_number"}, // public_key ByteArray
|
2023-12-27 12:37:22 +01:00
|
|
|
EventTypeByteRange{"message", "message_length", "length"}, // the latter two are ideally the same
|
2023-07-25 11:53:09 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
"Friend_Status",
|
|
|
|
{
|
|
|
|
EventTypeTrivial{"uint32_t", "friend_number"},
|
|
|
|
EventTypeTrivial{"Tox_User_Status", "status"},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Friend_Status_Message",
|
|
|
|
{
|
|
|
|
EventTypeTrivial{"uint32_t", "friend_number"},
|
2023-12-27 12:37:22 +01:00
|
|
|
EventTypeByteRange{"message", "message_length", "length"}, // the latter two are ideally the same
|
2023-07-25 11:53:09 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Friend_Typing",
|
|
|
|
{
|
|
|
|
EventTypeTrivial{"uint32_t", "friend_number"},
|
|
|
|
EventTypeTrivial{"bool", "typing"},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"Self_Connection_Status",
|
|
|
|
{
|
|
|
|
EventTypeTrivial{"Tox_Connection", "connection_status"},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"Group_Peer_Name",
|
|
|
|
{
|
|
|
|
EventTypeTrivial{"uint32_t", "group_number"},
|
|
|
|
EventTypeTrivial{"uint32_t", "peer_id"},
|
2023-12-27 12:37:22 +01:00
|
|
|
EventTypeByteRange{"name", "name_length", "length"}, // the latter two are ideally the same
|
2023-07-25 11:53:09 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Group_Peer_Status",
|
|
|
|
{
|
|
|
|
EventTypeTrivial{"uint32_t", "group_number"},
|
|
|
|
EventTypeTrivial{"uint32_t", "peer_id"},
|
|
|
|
EventTypeTrivial{"Tox_User_Status", "status"},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Group_Topic",
|
|
|
|
{
|
|
|
|
EventTypeTrivial{"uint32_t", "group_number"},
|
|
|
|
EventTypeTrivial{"uint32_t", "peer_id"},
|
2023-12-27 12:37:22 +01:00
|
|
|
EventTypeByteRange{"topic", "topic_length", "length"}, // the latter two are ideally the same
|
2023-07-25 11:53:09 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Group_Privacy_State",
|
|
|
|
{
|
|
|
|
EventTypeTrivial{"uint32_t", "group_number"},
|
|
|
|
EventTypeTrivial{"Tox_Group_Privacy_State", "privacy_state"},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Group_Voice_State",
|
|
|
|
{
|
|
|
|
EventTypeTrivial{"uint32_t", "group_number"},
|
|
|
|
EventTypeTrivial{"Tox_Group_Voice_State", "voice_state"},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Group_Topic_Lock",
|
|
|
|
{
|
|
|
|
EventTypeTrivial{"uint32_t", "group_number"},
|
|
|
|
EventTypeTrivial{"Tox_Group_Topic_Lock", "topic_lock"},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Group_Peer_Limit",
|
|
|
|
{
|
|
|
|
EventTypeTrivial{"uint32_t", "group_number"},
|
|
|
|
EventTypeTrivial{"uint32_t", "peer_limit"},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Group_Password",
|
|
|
|
{
|
|
|
|
EventTypeTrivial{"uint32_t", "group_number"},
|
2023-12-27 12:37:22 +01:00
|
|
|
EventTypeByteRange{"password", "password_length", "length"}, // the latter two are ideally the same
|
2023-07-25 11:53:09 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Group_Message",
|
|
|
|
{
|
|
|
|
EventTypeTrivial{"uint32_t", "group_number"},
|
|
|
|
EventTypeTrivial{"uint32_t", "peer_id"},
|
|
|
|
EventTypeTrivial{"Tox_Message_Type", "type"},
|
2023-12-27 12:37:22 +01:00
|
|
|
EventTypeByteRange{"message", "message_length", "length"}, // the latter two are ideally the same
|
2023-07-25 11:53:09 +02:00
|
|
|
EventTypeTrivial{"uint32_t", "message_id"},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Group_Private_Message",
|
|
|
|
{
|
|
|
|
EventTypeTrivial{"uint32_t", "group_number"},
|
|
|
|
EventTypeTrivial{"uint32_t", "peer_id"},
|
|
|
|
EventTypeTrivial{"Tox_Message_Type", "type"},
|
2023-12-27 12:37:22 +01:00
|
|
|
EventTypeByteRange{"message", "message_length", "length"}, // the latter two are ideally the same
|
2023-07-25 11:53:09 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Group_Custom_Packet",
|
|
|
|
{
|
|
|
|
EventTypeTrivial{"uint32_t", "group_number"},
|
|
|
|
EventTypeTrivial{"uint32_t", "peer_id"},
|
2023-12-27 12:37:22 +01:00
|
|
|
EventTypeByteRange{"data", "data_length", "length"}, // the latter two are ideally the same
|
2023-07-25 11:53:09 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Group_Custom_Private_Packet",
|
|
|
|
{
|
|
|
|
EventTypeTrivial{"uint32_t", "group_number"},
|
|
|
|
EventTypeTrivial{"uint32_t", "peer_id"},
|
2023-12-27 12:37:22 +01:00
|
|
|
EventTypeByteRange{"data", "data_length", "length"}, // the latter two are ideally the same
|
2023-07-25 11:53:09 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Group_Invite",
|
|
|
|
{
|
|
|
|
EventTypeTrivial{"uint32_t", "friend_number"},
|
2023-12-27 12:37:22 +01:00
|
|
|
EventTypeByteRange{"invite_data", "invite_data_length", "length"}, // the latter two are ideally the same
|
2023-07-25 11:53:09 +02:00
|
|
|
EventTypeByteRange{"group_name", "group_name_length", "group_name_length"}, // they are :)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Group_Peer_Join",
|
|
|
|
{
|
|
|
|
EventTypeTrivial{"uint32_t", "group_number"},
|
|
|
|
EventTypeTrivial{"uint32_t", "peer_id"},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Group_Peer_Exit",
|
|
|
|
{
|
|
|
|
EventTypeTrivial{"uint32_t", "group_number"},
|
|
|
|
EventTypeTrivial{"uint32_t", "peer_id"},
|
|
|
|
EventTypeTrivial{"Tox_Group_Exit_Type", "exit_type"},
|
|
|
|
EventTypeByteRange{"name", "name_length", "name_length"}, // they are :)
|
|
|
|
EventTypeByteRange{"part_message", "part_message_length", "part_message_length"}, // they are :)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Group_Self_Join",
|
|
|
|
{
|
|
|
|
EventTypeTrivial{"uint32_t", "group_number"},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Group_Join_Fail",
|
|
|
|
{
|
|
|
|
EventTypeTrivial{"uint32_t", "group_number"},
|
|
|
|
EventTypeTrivial{"Tox_Group_Join_Fail", "fail_type"},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Group_Moderation",
|
|
|
|
{
|
|
|
|
EventTypeTrivial{"uint32_t", "group_number"},
|
|
|
|
EventTypeTrivial{"uint32_t", "source_peer_id"},
|
|
|
|
EventTypeTrivial{"uint32_t", "target_peer_id"},
|
|
|
|
EventTypeTrivial{"Tox_Group_Mod_Event", "mod_type"},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
if (argc < 2) {
|
|
|
|
for (const auto& [event, event_types] : event_defs) {
|
|
|
|
generate_event_impl(event, event_types);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (event_defs.count(argv[1])) {
|
|
|
|
generate_event_impl(argv[1], event_defs[argv[1]]);
|
|
|
|
} else {
|
|
|
|
std::cerr << "error: unknown event " << argv[1] << "\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|