Recent

Author Topic: HID USB - "C" Translator help  (Read 4564 times)

JimKueneman

  • Full Member
  • ***
  • Posts: 220
HID USB - "C" Translator help
« on: August 20, 2014, 04:06:40 am »
Hi,  I am trying to use the cross platform hidapi and I am trying to translate the header file.  I am not fluent in C and as I look at the file it is clear I have not translated anything in a _long_ time. 

1) Does anyone know of a Lazarus translation of this header already?
2) Can someone help me understand what this is trying to say:

Code: [Select]
#ifdef _WIN32
      #define HID_API_EXPORT __declspec(dllexport)
      #define HID_API_CALL
#else
      #define HID_API_EXPORT /**< API export macro */
      #define HID_API_CALL /**< API call macro */
#endif

#define HID_API_EXPORT_CALL HID_API_EXPORT HID_API_CALL /**< API export and call macro*/

Code: [Select]
int HID_API_EXPORT HID_API_CALL hid_init(void);

For a more complete view:

Code: [Select]
/*******************************************************
 HIDAPI - Multi-Platform library for
 communication with HID devices.

 Alan Ott
 Signal 11 Software

 8/22/2009

 Copyright 2009, All Rights Reserved.

 At the discretion of the user of this library,
 this software may be licensed under the terms of the
 GNU Public License v3, a BSD-Style license, or the
 original HIDAPI license as outlined in the LICENSE.txt,
 LICENSE-gpl3.txt, LICENSE-bsd.txt, and LICENSE-orig.txt
 files located at the root of the source distribution.
 These files may also be found in the public source
 code repository located at:
        http://github.com/signal11/hidapi .
********************************************************/

/** @file
 * @defgroup API hidapi API
 */

#ifndef HIDAPI_H__
#define HIDAPI_H__

#include <wchar.h>

#ifdef _WIN32
      #define HID_API_EXPORT __declspec(dllexport)
      #define HID_API_CALL
#else
      #define HID_API_EXPORT /**< API export macro */
      #define HID_API_CALL /**< API call macro */
#endif

#define HID_API_EXPORT_CALL HID_API_EXPORT HID_API_CALL /**< API export and call macro*/

#ifdef __cplusplus
extern "C" {
#endif
struct hid_device_;
typedef struct hid_device_ hid_device; /**< opaque hidapi structure */

/** hidapi info structure */
struct hid_device_info {
/** Platform-specific device path */
char *path;
/** Device Vendor ID */
unsigned short vendor_id;
/** Device Product ID */
unsigned short product_id;
/** Serial Number */
wchar_t *serial_number;
/** Device Release Number in binary-coded decimal,
    also known as Device Version Number */
unsigned short release_number;
/** Manufacturer String */
wchar_t *manufacturer_string;
/** Product string */
wchar_t *product_string;
/** Usage Page for this Device/Interface
    (Windows/Mac only). */
unsigned short usage_page;
/** Usage for this Device/Interface
    (Windows/Mac only).*/
unsigned short usage;
/** The USB interface which this logical device
    represents. Valid on both Linux implementations
    in all cases, and valid on the Windows implementation
    only if the device contains more than one interface. */
int interface_number;

/** Pointer to the next device */
struct hid_device_info *next;
};


/** @brief Initialize the HIDAPI library.

This function initializes the HIDAPI library. Calling it is not
strictly necessary, as it will be called automatically by
hid_enumerate() and any of the hid_open_*() functions if it is
needed.  This function should be called at the beginning of
execution however, if there is a chance of HIDAPI handles
being opened by different threads simultaneously.

@ingroup API

@returns
This function returns 0 on success and -1 on error.
*/
int HID_API_EXPORT HID_API_CALL hid_init(void);

Can someone tell me what the

HID_API_EXPORT HID_API_CALL

are doing and how to deal with it in Pascal?

Oh, and the header 2 pascal translator fails to translate any of this.

Thanks,
Jim
« Last Edit: August 20, 2014, 04:10:03 am by JimKueneman »

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: HID USB - "C" Translator help
« Reply #1 on: August 20, 2014, 04:24:30 am »
2) Can someone help me understand what this is trying to say:

Code: [Select]
#ifdef _WIN32
      #define HID_API_EXPORT __declspec(dllexport)
      #define HID_API_CALL
#else
      #define HID_API_EXPORT /**< API export macro */
      #define HID_API_CALL /**< API call macro */
#endif

#define HID_API_EXPORT_CALL HID_API_EXPORT HID_API_CALL /**< API export and call macro*/
If on a 32bit Windows:
HID_API_EXPORT will be replaced with __declspec(dllexport) and you can use cdecl.
HID_API_CALL will be replaced with an empty string (as if does not exist).

On any other system, they both get replaced with comments.

The same logic applies to HID_API_EXPORT_CALL

Code: [Select]
int HID_API_EXPORT HID_API_CALL hid_init(void);
...
Can someone tell me what the

HID_API_EXPORT HID_API_CALL

are doing and how to deal with it in Pascal?
Code: [Select]
const
{$ifdef Windows}
  HIDLib='hid.dll';
{$else}
  HIDLib='hid.so';//<--- I guess?
{$endif}

function hid_init(): integer; cdecl; external HIDLib;

Edit:
Corrected the library name based on the link:
https://github.com/signal11/hidapi

2nd Edit:
I just noticed, it's a function, not a procedure.
« Last Edit: August 20, 2014, 04:32:38 am by engkin »

JimKueneman

  • Full Member
  • ***
  • Posts: 220
Re: HID USB - "C" Translator help
« Reply #2 on: August 20, 2014, 04:31:55 am »
Quote
const
  SomeLibName='test.dll';//<--- put the correct name here

procedure hid_init();cdecl; external SomeLibName;

so for non-windows

Code: [Select]
procedure hid_init(); external SomeLibName;
?

Jim

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: HID USB - "C" Translator help
« Reply #3 on: August 20, 2014, 04:34:33 am »
Based on the code yes. I can't be sure because I'm a Windows person.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: HID USB - "C" Translator help
« Reply #4 on: August 20, 2014, 04:40:43 am »
Notice that I corrected the code above.

Good luck!

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11449
  • FPC developer.
Re: HID USB - "C" Translator help
« Reply #5 on: August 20, 2014, 10:25:49 am »
Note that this is still within the limits of FPC macro usage, example from the mysql header:

Code: [Select]
{$macro on}

{$IFDEF Unix}
  {$DEFINE extdecl:=cdecl}
  const
    Mysqllib = 'mysqlclient';
{$ENDIF}
{$IFDEF Windows}
  {$DEFINE extdecl:=stdcall}
  const
    Mysqllib = 'libmysql';
{$ENDIF}

{$packrecords C}

Function mysql_num_rows (res : PMYSQL_RES) : my_ulonglong; extdecl; external mysqllib;


This avoids having to ifdef every line just because of the calling convention

 

TinyPortal © 2005-2018