Skip to content

jardon/libflatpak.dart

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dart FFI Bindings for libflatpak

This project provides Dart FFI (Foreign Function Interface) bindings for the libflatpak C library, allowing Dart and Flutter applications to directly interact with the Flatpak system to query, install, and manage applications and runtimes on Linux.

Prerequisites

C Development Tools: You must have a C compiler and development headers installed (gcc, pkg-config, etc.).

libflatpak: The libflatpak-dev or equivalent package must be installed on your development system.

Dart SDK: A recent version of the Dart SDK is required.

Setup

  1. Update pubspec.yaml

Add the necessary dependencies to your project's pubspec.yaml file:

dependencies:
  ffi: ^2.1.0
dev_dependencies:
  ffigen: ^10.0.0

Then run dart pub get.

  1. Generate Bindings (ffigen)
dart run ffigen --config ffigen.yaml

This will generate the required libflatpak.dart file.

Basic Usage Example: Listing Installed Apps

This example demonstrates how to load the library and use the bindings to list installed applications, linking to the core functions we discussed.

final ffi.DynamicLibrary libflatpak = ffi.DynamicLibrary.open('/usr/lib/x86_64-linux-gnu/libflatpak.so');

final bindings = FlatpakBindings(libflatpak);
final ffi.Pointer<ffi.Pointer<GError>> error = pkg_ffi.calloc<ffi.Pointer<GError>>();
final ffi.Pointer<FlatpakInstallation> installationPtr = bindings.flatpak_installation_new_system(ffi.nullptr, error);

if (installationPtr == ffi.nullptr) {
    if (error.value != ffi.nullptr) {
        print(
            'Failed to get FlatpakInstallation. GError pointer received: ${error.value}');
    } else {
        print(
            'Failed to get FlatpakInstallation, but no explicit GError was returned.');
    }
} else {
    print('Successfully created FlatpakInstallation object at address: $installationPtr');
}

Map<String, Map<String, dynamic>> data;
final ffi.Pointer<GPtrArray> installed_refs =
    bindings.flatpak_installation_list_installed_refs(
        installationPtr, ffi.nullptr, error);

if (installed_refs != ffi.nullptr) {
    final GPtrArray installedRefs = installed_refs.ref;
    final int length = installedRefs.len;
    print('Found $length installed references (apps and runtimes).');
    final ffi.Pointer<gpointer> pdataPtr = installedRefs.pdata;

    for (int i = 0; i < length; i++) {
        final ffi.Pointer<ffi.Void> refVoidPtr = pdataPtr.elementAt(i).value;

        final ffi.Pointer<FlatpakRef> refPtr = refVoidPtr.cast<FlatpakRef>();

        final ffi.Pointer<ffi.Char> namePtr =
            bindings.flatpak_ref_get_name(refPtr);

        final String appId = namePtr.cast<pkg_ffi.Utf8>().toDartString();
        print('[$i] Installed App/Runtime ID: $appId');
    }
}

pkg_ffi.calloc.free(pdataPtr);
pkg_ffi.calloc.free(refVoidPtr);
pkg_ffi.calloc.free(refPtr);
pkg_ffi.calloc.free(namePtr);
pkg_ffi.calloc.free(error);
pkg_ffi.calloc.free(installationPtr);

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages