Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add toxcore FFI. #52

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ app.*.map.json
/android/app/release

# Generated files
**/generated/*
*.freezed.dart
*.g.dart
2 changes: 2 additions & 0 deletions .restyled.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
exclude:
- "LICENSE"
# Generated files.
- "**/CMakeLists.txt"
- "linux/**/*.cc"
- "linux/**/*.h"
- "tools/include/**/*.h"
- "web/flutter_bootstrap.js"
- "windows/**/*.cc"
- "windows/**/*.h"
1 change: 1 addition & 0 deletions android/app/src/main/jniLibs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.so
6 changes: 6 additions & 0 deletions cspell.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ ignorePaths:
- LICENSE*
- "*.json"
words:
- armeabi
- btox
- dylib
- ffigen
- iphydf
- kannywood
- libtoxcore
- malloc
- nospam
- robinlinden
- toxcore
- yanciman
3 changes: 3 additions & 0 deletions ios/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ Runner/GeneratedPluginRegistrant.*
!default.mode2v3
!default.pbxuser
!default.perspectivev3

# Vendored toxcore library.
/Tox.xcframework
18 changes: 18 additions & 0 deletions ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */ = {isa = PBXBuildFile; fileRef = 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */; };
331C808B294A63AB00263BE5 /* RunnerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 331C807B294A618700263BE5 /* RunnerTests.swift */; };
3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */; };
64A1840F2D49A02F00130ADA /* Tox.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = 64A1840D2D49A02500130ADA /* Tox.xcframework */; };
64A184102D49A02F00130ADA /* Tox.xcframework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 64A1840D2D49A02500130ADA /* Tox.xcframework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
74858FAF1ED2DC5600515810 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74858FAE1ED2DC5600515810 /* AppDelegate.swift */; };
97C146FC1CF9000F007C117D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FA1CF9000F007C117D /* Main.storyboard */; };
97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FD1CF9000F007C117D /* Assets.xcassets */; };
Expand All @@ -35,6 +37,7 @@
dstPath = "";
dstSubfolderSpec = 10;
files = (
64A184102D49A02F00130ADA /* Tox.xcframework in Embed Frameworks */,
);
name = "Embed Frameworks";
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -51,6 +54,7 @@
3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = "<group>"; };
5ADD64A60EF720FCF56734A0 /* Pods-Runner.profile.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.profile.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.profile.xcconfig"; sourceTree = "<group>"; };
5BC5ED0188408D84785CE7F3 /* Pods-RunnerTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RunnerTests.release.xcconfig"; path = "Target Support Files/Pods-RunnerTests/Pods-RunnerTests.release.xcconfig"; sourceTree = "<group>"; };
64A1840D2D49A02500130ADA /* Tox.xcframework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcframework; path = Tox.xcframework; sourceTree = "<group>"; };
74858FAD1ED2DC5600515810 /* Runner-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Runner-Bridging-Header.h"; sourceTree = "<group>"; };
74858FAE1ED2DC5600515810 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
7AFA3C8E1D35360C0083082E /* Release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = Release.xcconfig; path = Flutter/Release.xcconfig; sourceTree = "<group>"; };
Expand Down Expand Up @@ -81,6 +85,7 @@
buildActionMask = 2147483647;
files = (
033C5F025F913405F7C9F291 /* Pods_Runner.framework in Frameworks */,
64A1840F2D49A02F00130ADA /* Tox.xcframework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -113,6 +118,7 @@
children = (
AA2B125A12517F8F71057DEB /* Pods_Runner.framework */,
F10E7E459976E7D29922FCDB /* Pods_RunnerTests.framework */,
64A1840D2D49A02500130ADA /* Tox.xcframework */,
);
name = Frameworks;
sourceTree = "<group>";
Expand Down Expand Up @@ -476,6 +482,10 @@
"$(inherited)",
"@executable_path/Frameworks",
);
LIBRARY_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)",
);
PRODUCT_BUNDLE_IDENTIFIER = chat.tox.btox;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
Expand Down Expand Up @@ -658,6 +668,10 @@
"$(inherited)",
"@executable_path/Frameworks",
);
LIBRARY_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)",
);
PRODUCT_BUNDLE_IDENTIFIER = chat.tox.btox;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
Expand All @@ -681,6 +695,10 @@
"$(inherited)",
"@executable_path/Frameworks",
);
LIBRARY_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)",
);
PRODUCT_BUNDLE_IDENTIFIER = chat.tox.btox;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
Expand Down
82 changes: 82 additions & 0 deletions lib/api/toxcore/tox.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
final class ApiException<T extends Enum> implements Exception {
final T error;

const ApiException(this.error);

@override
String toString() {
return 'ApiException: $error';
}
}

abstract class Tox {
String get address;

bool get isAlive;

int get iterationInterval;

set name(String value);

set statusMessage(String value);

void addTcpRelay(String host, int port, String publicKey);

void bootstrap(String host, int port, String publicKey);

List<String> iterate();

void kill();
}

final class ToxConstants {
final int addressSize;
final int conferenceIdSize;
final int fileIdLength;
final int groupChatIdSize;
final int groupMaxCustomLosslessPacketLength;
final int groupMaxCustomLossyPacketLength;
final int groupMaxGroupNameLength;
final int groupMaxMessageLength;
final int groupMaxPartLength;
final int groupMaxPasswordSize;
final int groupMaxTopicLength;
final int groupPeerPublicKeySize;
final int hashLength;
final int maxCustomPacketSize;
final int maxFilenameLength;
final int maxFriendRequestLength;
final int maxHostnameLength;
final int maxMessageLength;
final int maxNameLength;
final int maxStatusMessageLength;
final int nospamSize;
final int publicKeySize;
final int secretKeySize;

const ToxConstants({
required this.addressSize,
required this.conferenceIdSize,
required this.fileIdLength,
required this.groupChatIdSize,
required this.groupMaxCustomLosslessPacketLength,
required this.groupMaxCustomLossyPacketLength,
required this.groupMaxGroupNameLength,
required this.groupMaxMessageLength,
required this.groupMaxPartLength,
required this.groupMaxPasswordSize,
required this.groupMaxTopicLength,
required this.groupPeerPublicKeySize,
required this.hashLength,
required this.maxCustomPacketSize,
required this.maxFilenameLength,
required this.maxFriendRequestLength,
required this.maxHostnameLength,
required this.maxMessageLength,
required this.maxNameLength,
required this.maxStatusMessageLength,
required this.nospamSize,
required this.publicKeySize,
required this.secretKeySize,
});
}
11 changes: 11 additions & 0 deletions lib/api/toxcore/tox_options.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
final class ToxOptions {
final bool ipv6Enabled;
final bool udpEnabled;
final bool localDiscoveryEnabled;

const ToxOptions({
this.ipv6Enabled = true,
this.udpEnabled = true,
this.localDiscoveryEnabled = true,
});
}
48 changes: 30 additions & 18 deletions lib/btox_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:btox/pages/contact_list_page.dart';
import 'package:btox/pages/create_profile_page.dart';
import 'package:btox/pages/select_profile_page.dart';
import 'package:btox/providers/database.dart';
import 'package:btox/providers/tox.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
Expand Down Expand Up @@ -31,26 +32,37 @@ final class BtoxApp extends ConsumerWidget {
child: Text('Error: $error'),
),
),
data: (db) => StreamBuilder<List<Profile>>(
stream: db.watchProfiles(),
builder: (context, snapshot) {
final profiles = snapshot.data ?? const [];
if (profiles.isEmpty) {
return const CreateProfilePage();
}
data: (database) {
final constants = ref.read(toxConstantsProvider);
return StreamBuilder<List<Profile>>(
stream: database.watchProfiles(),
builder: (context, snapshot) {
final profiles = snapshot.data ?? const [];
if (profiles.isEmpty) {
return CreateProfilePage(
constants: constants,
database: database,
);
}

final activeProfiles =
profiles.where((profile) => profile.active).toList();
if (activeProfiles.isEmpty) {
return SelectProfilePage(profiles: profiles);
}
final activeProfiles =
profiles.where((profile) => profile.active);
if (activeProfiles.isEmpty) {
return SelectProfilePage(
constants: constants,
database: database,
profiles: profiles,
);
}

return ContactListPage(
database: db,
profile: activeProfiles.first,
);
},
),
return ContactListPage(
constants: constants,
database: database,
profile: activeProfiles.first,
);
},
);
},
),
);
}
Expand Down
3 changes: 3 additions & 0 deletions lib/ffi/tox_constants.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export 'tox_constants.native.dart'
if (dart.library.html) 'tox_constants.web.dart'
if (dart.library.js) 'tox_constants.web.dart';
30 changes: 30 additions & 0 deletions lib/ffi/tox_constants.native.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'package:btox/api/toxcore/tox.dart';
import 'package:btox/ffi/generated/toxcore.native.dart';

ToxConstants toxcoreConstants(ToxFfi ffi) => ToxConstants(
addressSize: ffi.tox_address_size(),
conferenceIdSize: ffi.tox_conference_id_size(),
fileIdLength: ffi.tox_file_id_length(),
groupChatIdSize: ffi.tox_group_chat_id_size(),
groupMaxCustomLosslessPacketLength:
ffi.tox_group_max_custom_lossless_packet_length(),
groupMaxCustomLossyPacketLength:
ffi.tox_group_max_custom_lossy_packet_length(),
groupMaxGroupNameLength: ffi.tox_group_max_group_name_length(),
groupMaxMessageLength: ffi.tox_group_max_message_length(),
groupMaxPartLength: ffi.tox_group_max_part_length(),
groupMaxPasswordSize: ffi.tox_group_max_password_size(),
groupMaxTopicLength: ffi.tox_group_max_topic_length(),
groupPeerPublicKeySize: ffi.tox_group_peer_public_key_size(),
hashLength: ffi.tox_hash_length(),
maxCustomPacketSize: ffi.tox_max_custom_packet_size(),
maxFilenameLength: ffi.tox_max_filename_length(),
maxFriendRequestLength: ffi.tox_max_friend_request_length(),
maxHostnameLength: ffi.tox_max_hostname_length(),
maxMessageLength: ffi.tox_max_message_length(),
maxNameLength: ffi.tox_max_name_length(),
maxStatusMessageLength: ffi.tox_max_status_message_length(),
nospamSize: ffi.tox_nospam_size(),
publicKeySize: ffi.tox_public_key_size(),
secretKeySize: ffi.tox_secret_key_size(),
);
28 changes: 28 additions & 0 deletions lib/ffi/tox_constants.web.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'package:btox/api/toxcore/tox.dart';
import 'package:btox/ffi/tox_ffi.web.dart';

ToxConstants toxcoreConstants(ToxFfi ffi) => const ToxConstants(
addressSize: 38,
conferenceIdSize: 32,
fileIdLength: 32,
groupChatIdSize: 32,
groupMaxCustomLosslessPacketLength: 1373,
groupMaxCustomLossyPacketLength: 1373,
groupMaxGroupNameLength: 48,
groupMaxMessageLength: 1372,
groupMaxPartLength: 128,
groupMaxPasswordSize: 32,
groupMaxTopicLength: 512,
groupPeerPublicKeySize: 32,
hashLength: 32,
maxCustomPacketSize: 1373,
maxFilenameLength: 255,
maxFriendRequestLength: 921,
maxHostnameLength: 255,
maxMessageLength: 1372,
maxNameLength: 128,
maxStatusMessageLength: 1007,
nospamSize: 4,
publicKeySize: 32,
secretKeySize: 32,
);
3 changes: 3 additions & 0 deletions lib/ffi/tox_ffi.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export 'tox_ffi.native.dart'
if (dart.library.html) 'tox_ffi.web.dart'
if (dart.library.js) 'tox_ffi.web.dart';
18 changes: 18 additions & 0 deletions lib/ffi/tox_ffi.native.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'dart:ffi';
import 'dart:io';

import 'package:btox/ffi/generated/toxcore.native.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

export 'package:btox/ffi/generated/toxcore.native.dart' show ToxFfi;

part 'tox_ffi.native.g.dart';

@riverpod
ToxFfi toxFfi(Ref ref) {
if (Platform.isAndroid) {
return ToxFfi(DynamicLibrary.open('libtoxcore.so'));
}
return ToxFfi(DynamicLibrary.process());
}
13 changes: 13 additions & 0 deletions lib/ffi/tox_ffi.web.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'tox_ffi.web.g.dart';

final class ToxFfi {
const ToxFfi();
}

@riverpod
ToxFfi toxFfi(Ref ref) {
return const ToxFfi();
}
3 changes: 3 additions & 0 deletions lib/ffi/toxcore.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export 'toxcore.native.dart'
if (dart.library.html) 'toxcore.web.dart'
if (dart.library.js) 'toxcore.web.dart';
Loading
Loading