Unit ulibmtp_dynamic;

{$MODE ObjFPC}{$H+}

Interface

Uses
  Classes, SysUtils, ctypes, ulibmtp_common;

Type
  TLIBMTP_Init = Procedure(); cdecl;

  TLIBMTP_Detect_Raw_Devices = Function(Var rawdevices: PLIBMTP_raw_device_t; Var numrawdevices: cint): LIBMTP_error_number_t; cdecl;

  TLIBMTP_Open_Raw_Device_Uncached = Function(rawdevice: PLIBMTP_raw_device_t): PLIBMTP_mtpdevice_t; cdecl;

  TLIBMTP_Release_Device = Procedure(device: PLIBMTP_mtpdevice_t); cdecl;

  TLIBMTP_Get_Friendlyname = Function(device: PLIBMTP_mtpdevice_t): Pchar; cdecl;

  TLIBMTP_Clear_Errorstack = Procedure(device: PLIBMTP_mtpdevice_t); cdecl;
  TLIBMTP_Dump_Errorstack = Procedure(device: PLIBMTP_mtpdevice_t); cdecl;

  TLIBMTP_Get_Files_And_Folders = Function(device: PLIBMTP_mtpdevice_t; storageid: uint32_t; leaf: uint32_t): PLIBMTP_file_t; cdecl;
  TLIBMTP_destroy_file_t = Procedure(File_: PLIBMTP_file_t); cdecl;
  TLIBMTP_Get_Filetype_Description = Function(File_: LIBMTP_filetype_t): Pchar; cdecl;

Var
  LIBMTP_Init: TLIBMTP_Init = Nil;
  LIBMTP_Detect_Raw_Devices: TLIBMTP_Detect_Raw_Devices = Nil;
  LIBMTP_Open_Raw_Device_Uncached: TLIBMTP_Open_Raw_Device_Uncached = Nil;
  LIBMTP_Release_Device: TLIBMTP_Release_Device = Nil;
  LIBMTP_Get_Friendlyname: TLIBMTP_Get_Friendlyname = Nil;
  LIBMTP_Clear_Errorstack: TLIBMTP_Clear_Errorstack = Nil;
  LIBMTP_Dump_Errorstack: TLIBMTP_Dump_Errorstack = Nil;
  LIBMTP_Get_Files_And_Folders: TLIBMTP_Get_Files_And_Folders = Nil;
  LIBMTP_destroy_file_t: TLIBMTP_destroy_file_t = Nil;
  LIBMTP_Get_Filetype_Description: TLIBMTP_Get_Filetype_Description = Nil;

Function LoadLib(LibName: String): Boolean;
Procedure UnloadLib();

Implementation

Uses dynlibs;

Var
  lh: TLibHandle = 0;

Function LoadLib(LibName: String): Boolean;
Begin
  result := false;
  If lh <> 0 Then Begin
    result := true;
    exit;
  End;
  lh := LoadLibrary(LibName);
  If lh = 0 Then exit;

  LIBMTP_Init := TLIBMTP_Init(GetProcAddress(lh, 'LIBMTP_Init'));
  If Not assigned(LIBMTP_Init) Then exit;

  LIBMTP_Detect_Raw_Devices := TLIBMTP_Detect_Raw_Devices(GetProcAddress(lh, 'LIBMTP_Detect_Raw_Devices'));
  If Not assigned(LIBMTP_Detect_Raw_Devices) Then exit;

  LIBMTP_Open_Raw_Device_Uncached := TLIBMTP_Open_Raw_Device_Uncached(GetProcAddress(lh, 'LIBMTP_Open_Raw_Device_Uncached'));
  If Not assigned(LIBMTP_Open_Raw_Device_Uncached) Then exit;

  LIBMTP_Release_Device := TLIBMTP_Release_Device(GetProcAddress(lh, 'LIBMTP_Release_Device'));
  If Not assigned(LIBMTP_Release_Device) Then exit;

  LIBMTP_Get_Friendlyname := TLIBMTP_Get_Friendlyname(GetProcAddress(lh, 'LIBMTP_Get_Friendlyname'));
  If Not assigned(LIBMTP_Get_Friendlyname) Then exit;

  LIBMTP_Clear_Errorstack := TLIBMTP_Clear_Errorstack(GetProcAddress(lh, 'LIBMTP_Clear_Errorstack'));
  If Not assigned(LIBMTP_Clear_Errorstack) Then exit;

  LIBMTP_Dump_Errorstack := TLIBMTP_Dump_Errorstack(GetProcAddress(lh, 'LIBMTP_Dump_Errorstack'));
  If Not assigned(LIBMTP_Dump_Errorstack) Then exit;

  LIBMTP_Get_Files_And_Folders := TLIBMTP_Get_Files_And_Folders(GetProcAddress(lh, 'LIBMTP_Get_Files_And_Folders'));
  If Not assigned(LIBMTP_Get_Files_And_Folders) Then exit;

  LIBMTP_destroy_file_t := TLIBMTP_destroy_file_t(GetProcAddress(lh, 'LIBMTP_destroy_file_t'));
  If Not assigned(LIBMTP_destroy_file_t) Then exit;

  LIBMTP_Get_Filetype_Description := TLIBMTP_Get_Filetype_Description(GetProcAddress(lh, 'LIBMTP_Get_Filetype_Description'));
  If Not assigned(LIBMTP_Get_Filetype_Description) Then exit;

  result := true;
End;

Procedure UnloadLib;
Begin
  If lh <> 0 Then Begin
    UnloadLibrary(lh);
    LIBMTP_Init := Nil;
    lh := 0;
  End;
End;

End.

